]> CyberLeo.Net >> Repos - FreeBSD/releng/8.2.git/blob - sys/dev/usb/usb_request.c
MFC r216249
[FreeBSD/releng/8.2.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                 /* if the device disappeared, just give up */
797                 if (!(UGETW(ps.wPortStatus) & UPS_CURRENT_CONNECT_STATUS)) {
798                         goto done;
799                 }
800                 /* check if reset is complete */
801                 if (UGETW(ps.wPortChange) & UPS_C_PORT_RESET) {
802                         break;
803                 }
804                 /* check for timeout */
805                 if (n > 1000) {
806                         n = 0;
807                         break;
808                 }
809         }
810
811         /* clear port reset first */
812         err = usbd_req_clear_port_feature(
813             udev, mtx, port, UHF_C_PORT_RESET);
814         if (err) {
815                 goto done;
816         }
817         /* check for timeout */
818         if (n == 0) {
819                 err = USB_ERR_TIMEOUT;
820                 goto done;
821         }
822 #ifdef USB_DEBUG
823         /* wait for the device to recover from reset */
824         usb_pause_mtx(mtx, USB_MS_TO_TICKS(pr_recovery_delay));
825 #else
826         /* wait for the device to recover from reset */
827         usb_pause_mtx(mtx, USB_MS_TO_TICKS(USB_PORT_RESET_RECOVERY));
828 #endif
829
830 done:
831         DPRINTFN(2, "port %d reset returning error=%s\n",
832             port, usbd_errstr(err));
833         return (err);
834 }
835
836 /*------------------------------------------------------------------------*
837  *      usbd_req_warm_reset_port
838  *
839  * This function will instruct an USB HUB to perform a warm reset
840  * sequence on the specified port number. This kind of reset is not
841  * mandatory for LOW-, FULL- and HIGH-speed USB HUBs and is targeted
842  * for SUPER-speed USB HUBs.
843  *
844  * Returns:
845  *    0: Success. The USB device should now be available again.
846  * Else: Failure. No USB device is present and the USB port should be
847  *       disabled.
848  *------------------------------------------------------------------------*/
849 usb_error_t
850 usbd_req_warm_reset_port(struct usb_device *udev, struct mtx *mtx, uint8_t port)
851 {
852         struct usb_port_status ps;
853         usb_error_t err;
854         uint16_t n;
855
856 #ifdef USB_DEBUG
857         uint16_t pr_poll_delay;
858         uint16_t pr_recovery_delay;
859
860 #endif
861         err = usbd_req_set_port_feature(udev, mtx, port, UHF_BH_PORT_RESET);
862         if (err) {
863                 goto done;
864         }
865 #ifdef USB_DEBUG
866         /* range check input parameters */
867         pr_poll_delay = usb_pr_poll_delay;
868         if (pr_poll_delay < 1) {
869                 pr_poll_delay = 1;
870         } else if (pr_poll_delay > 1000) {
871                 pr_poll_delay = 1000;
872         }
873         pr_recovery_delay = usb_pr_recovery_delay;
874         if (pr_recovery_delay > 1000) {
875                 pr_recovery_delay = 1000;
876         }
877 #endif
878         n = 0;
879         while (1) {
880 #ifdef USB_DEBUG
881                 /* wait for the device to recover from reset */
882                 usb_pause_mtx(mtx, USB_MS_TO_TICKS(pr_poll_delay));
883                 n += pr_poll_delay;
884 #else
885                 /* wait for the device to recover from reset */
886                 usb_pause_mtx(mtx, USB_MS_TO_TICKS(USB_PORT_RESET_DELAY));
887                 n += USB_PORT_RESET_DELAY;
888 #endif
889                 err = usbd_req_get_port_status(udev, mtx, &ps, port);
890                 if (err) {
891                         goto done;
892                 }
893                 /* if the device disappeared, just give up */
894                 if (!(UGETW(ps.wPortStatus) & UPS_CURRENT_CONNECT_STATUS)) {
895                         goto done;
896                 }
897                 /* check if reset is complete */
898                 if (UGETW(ps.wPortChange) & UPS_C_BH_PORT_RESET) {
899                         break;
900                 }
901                 /* check for timeout */
902                 if (n > 1000) {
903                         n = 0;
904                         break;
905                 }
906         }
907
908         /* clear port reset first */
909         err = usbd_req_clear_port_feature(
910             udev, mtx, port, UHF_C_BH_PORT_RESET);
911         if (err) {
912                 goto done;
913         }
914         /* check for timeout */
915         if (n == 0) {
916                 err = USB_ERR_TIMEOUT;
917                 goto done;
918         }
919 #ifdef USB_DEBUG
920         /* wait for the device to recover from reset */
921         usb_pause_mtx(mtx, USB_MS_TO_TICKS(pr_recovery_delay));
922 #else
923         /* wait for the device to recover from reset */
924         usb_pause_mtx(mtx, USB_MS_TO_TICKS(USB_PORT_RESET_RECOVERY));
925 #endif
926
927 done:
928         DPRINTFN(2, "port %d warm reset returning error=%s\n",
929             port, usbd_errstr(err));
930         return (err);
931 }
932
933 /*------------------------------------------------------------------------*
934  *      usbd_req_get_desc
935  *
936  * This function can be used to retrieve USB descriptors. It contains
937  * some additional logic like zeroing of missing descriptor bytes and
938  * retrying an USB descriptor in case of failure. The "min_len"
939  * argument specifies the minimum descriptor length. The "max_len"
940  * argument specifies the maximum descriptor length. If the real
941  * descriptor length is less than the minimum length the missing
942  * byte(s) will be zeroed. The type field, the second byte of the USB
943  * descriptor, will get forced to the correct type. If the "actlen"
944  * pointer is non-NULL, the actual length of the transfer will get
945  * stored in the 16-bit unsigned integer which it is pointing to. The
946  * first byte of the descriptor will not get updated. If the "actlen"
947  * pointer is NULL the first byte of the descriptor will get updated
948  * to reflect the actual length instead. If "min_len" is not equal to
949  * "max_len" then this function will try to retrive the beginning of
950  * the descriptor and base the maximum length on the first byte of the
951  * descriptor.
952  *
953  * Returns:
954  *    0: Success
955  * Else: Failure
956  *------------------------------------------------------------------------*/
957 usb_error_t
958 usbd_req_get_desc(struct usb_device *udev,
959     struct mtx *mtx, uint16_t *actlen, void *desc,
960     uint16_t min_len, uint16_t max_len,
961     uint16_t id, uint8_t type, uint8_t index,
962     uint8_t retries)
963 {
964         struct usb_device_request req;
965         uint8_t *buf;
966         usb_error_t err;
967
968         DPRINTFN(4, "id=%d, type=%d, index=%d, max_len=%d\n",
969             id, type, index, max_len);
970
971         req.bmRequestType = UT_READ_DEVICE;
972         req.bRequest = UR_GET_DESCRIPTOR;
973         USETW2(req.wValue, type, index);
974         USETW(req.wIndex, id);
975
976         while (1) {
977
978                 if ((min_len < 2) || (max_len < 2)) {
979                         err = USB_ERR_INVAL;
980                         goto done;
981                 }
982                 USETW(req.wLength, min_len);
983
984                 err = usbd_do_request_flags(udev, mtx, &req,
985                     desc, 0, NULL, 1000);
986
987                 if (err) {
988                         if (!retries) {
989                                 goto done;
990                         }
991                         retries--;
992
993                         usb_pause_mtx(mtx, hz / 5);
994
995                         continue;
996                 }
997                 buf = desc;
998
999                 if (min_len == max_len) {
1000
1001                         /* enforce correct length */
1002                         if ((buf[0] > min_len) && (actlen == NULL))
1003                                 buf[0] = min_len;
1004
1005                         /* enforce correct type */
1006                         buf[1] = type;
1007
1008                         goto done;
1009                 }
1010                 /* range check */
1011
1012                 if (max_len > buf[0]) {
1013                         max_len = buf[0];
1014                 }
1015                 /* zero minimum data */
1016
1017                 while (min_len > max_len) {
1018                         min_len--;
1019                         buf[min_len] = 0;
1020                 }
1021
1022                 /* set new minimum length */
1023
1024                 min_len = max_len;
1025         }
1026 done:
1027         if (actlen != NULL) {
1028                 if (err)
1029                         *actlen = 0;
1030                 else
1031                         *actlen = min_len;
1032         }
1033         return (err);
1034 }
1035
1036 /*------------------------------------------------------------------------*
1037  *      usbd_req_get_string_any
1038  *
1039  * This function will return the string given by "string_index"
1040  * using the first language ID. The maximum length "len" includes
1041  * the terminating zero. The "len" argument should be twice as
1042  * big pluss 2 bytes, compared with the actual maximum string length !
1043  *
1044  * Returns:
1045  *    0: Success
1046  * Else: Failure
1047  *------------------------------------------------------------------------*/
1048 usb_error_t
1049 usbd_req_get_string_any(struct usb_device *udev, struct mtx *mtx, char *buf,
1050     uint16_t len, uint8_t string_index)
1051 {
1052         char *s;
1053         uint8_t *temp;
1054         uint16_t i;
1055         uint16_t n;
1056         uint16_t c;
1057         uint8_t swap;
1058         usb_error_t err;
1059
1060         if (len == 0) {
1061                 /* should not happen */
1062                 return (USB_ERR_NORMAL_COMPLETION);
1063         }
1064         if (string_index == 0) {
1065                 /* this is the language table */
1066                 buf[0] = 0;
1067                 return (USB_ERR_INVAL);
1068         }
1069         if (udev->flags.no_strings) {
1070                 buf[0] = 0;
1071                 return (USB_ERR_STALLED);
1072         }
1073         err = usbd_req_get_string_desc
1074             (udev, mtx, buf, len, udev->langid, string_index);
1075         if (err) {
1076                 buf[0] = 0;
1077                 return (err);
1078         }
1079         temp = (uint8_t *)buf;
1080
1081         if (temp[0] < 2) {
1082                 /* string length is too short */
1083                 buf[0] = 0;
1084                 return (USB_ERR_INVAL);
1085         }
1086         /* reserve one byte for terminating zero */
1087         len--;
1088
1089         /* find maximum length */
1090         s = buf;
1091         n = (temp[0] / 2) - 1;
1092         if (n > len) {
1093                 n = len;
1094         }
1095         /* skip descriptor header */
1096         temp += 2;
1097
1098         /* reset swap state */
1099         swap = 3;
1100
1101         /* convert and filter */
1102         for (i = 0; (i != n); i++) {
1103                 c = UGETW(temp + (2 * i));
1104
1105                 /* convert from Unicode, handle buggy strings */
1106                 if (((c & 0xff00) == 0) && (swap & 1)) {
1107                         /* Little Endian, default */
1108                         *s = c;
1109                         swap = 1;
1110                 } else if (((c & 0x00ff) == 0) && (swap & 2)) {
1111                         /* Big Endian */
1112                         *s = c >> 8;
1113                         swap = 2;
1114                 } else {
1115                         /* silently skip bad character */
1116                         continue;
1117                 }
1118
1119                 /*
1120                  * Filter by default - we don't allow greater and less than
1121                  * signs because they might confuse the dmesg printouts!
1122                  */
1123                 if ((*s == '<') || (*s == '>') || (!isprint(*s))) {
1124                         /* silently skip bad character */
1125                         continue;
1126                 }
1127                 s++;
1128         }
1129         *s = 0;                         /* zero terminate resulting string */
1130         return (USB_ERR_NORMAL_COMPLETION);
1131 }
1132
1133 /*------------------------------------------------------------------------*
1134  *      usbd_req_get_string_desc
1135  *
1136  * If you don't know the language ID, consider using
1137  * "usbd_req_get_string_any()".
1138  *
1139  * Returns:
1140  *    0: Success
1141  * Else: Failure
1142  *------------------------------------------------------------------------*/
1143 usb_error_t
1144 usbd_req_get_string_desc(struct usb_device *udev, struct mtx *mtx, void *sdesc,
1145     uint16_t max_len, uint16_t lang_id,
1146     uint8_t string_index)
1147 {
1148         return (usbd_req_get_desc(udev, mtx, NULL, sdesc, 2, max_len, lang_id,
1149             UDESC_STRING, string_index, 0));
1150 }
1151
1152 /*------------------------------------------------------------------------*
1153  *      usbd_req_get_config_desc_ptr
1154  *
1155  * This function is used in device side mode to retrieve the pointer
1156  * to the generated config descriptor. This saves allocating space for
1157  * an additional config descriptor when setting the configuration.
1158  *
1159  * Returns:
1160  *    0: Success
1161  * Else: Failure
1162  *------------------------------------------------------------------------*/
1163 usb_error_t
1164 usbd_req_get_descriptor_ptr(struct usb_device *udev,
1165     struct usb_config_descriptor **ppcd, uint16_t wValue)
1166 {
1167         struct usb_device_request req;
1168         usb_handle_req_t *hr_func;
1169         const void *ptr;
1170         uint16_t len;
1171         usb_error_t err;
1172
1173         req.bmRequestType = UT_READ_DEVICE;
1174         req.bRequest = UR_GET_DESCRIPTOR;
1175         USETW(req.wValue, wValue);
1176         USETW(req.wIndex, 0);
1177         USETW(req.wLength, 0);
1178
1179         ptr = NULL;
1180         len = 0;
1181
1182         hr_func = usbd_get_hr_func(udev);
1183
1184         if (hr_func == NULL)
1185                 err = USB_ERR_INVAL;
1186         else {
1187                 USB_BUS_LOCK(udev->bus);
1188                 err = (hr_func) (udev, &req, &ptr, &len);
1189                 USB_BUS_UNLOCK(udev->bus);
1190         }
1191
1192         if (err)
1193                 ptr = NULL;
1194         else if (ptr == NULL)
1195                 err = USB_ERR_INVAL;
1196
1197         *ppcd = __DECONST(struct usb_config_descriptor *, ptr);
1198
1199         return (err);
1200 }
1201
1202 /*------------------------------------------------------------------------*
1203  *      usbd_req_get_config_desc
1204  *
1205  * Returns:
1206  *    0: Success
1207  * Else: Failure
1208  *------------------------------------------------------------------------*/
1209 usb_error_t
1210 usbd_req_get_config_desc(struct usb_device *udev, struct mtx *mtx,
1211     struct usb_config_descriptor *d, uint8_t conf_index)
1212 {
1213         usb_error_t err;
1214
1215         DPRINTFN(4, "confidx=%d\n", conf_index);
1216
1217         err = usbd_req_get_desc(udev, mtx, NULL, d, sizeof(*d),
1218             sizeof(*d), 0, UDESC_CONFIG, conf_index, 0);
1219         if (err) {
1220                 goto done;
1221         }
1222         /* Extra sanity checking */
1223         if (UGETW(d->wTotalLength) < sizeof(*d)) {
1224                 err = USB_ERR_INVAL;
1225         }
1226 done:
1227         return (err);
1228 }
1229
1230 /*------------------------------------------------------------------------*
1231  *      usbd_req_get_config_desc_full
1232  *
1233  * This function gets the complete USB configuration descriptor and
1234  * ensures that "wTotalLength" is correct.
1235  *
1236  * Returns:
1237  *    0: Success
1238  * Else: Failure
1239  *------------------------------------------------------------------------*/
1240 usb_error_t
1241 usbd_req_get_config_desc_full(struct usb_device *udev, struct mtx *mtx,
1242     struct usb_config_descriptor **ppcd, struct malloc_type *mtype,
1243     uint8_t index)
1244 {
1245         struct usb_config_descriptor cd;
1246         struct usb_config_descriptor *cdesc;
1247         uint16_t len;
1248         usb_error_t err;
1249
1250         DPRINTFN(4, "index=%d\n", index);
1251
1252         *ppcd = NULL;
1253
1254         err = usbd_req_get_config_desc(udev, mtx, &cd, index);
1255         if (err) {
1256                 return (err);
1257         }
1258         /* get full descriptor */
1259         len = UGETW(cd.wTotalLength);
1260         if (len < sizeof(*cdesc)) {
1261                 /* corrupt descriptor */
1262                 return (USB_ERR_INVAL);
1263         }
1264         cdesc = malloc(len, mtype, M_WAITOK);
1265         if (cdesc == NULL) {
1266                 return (USB_ERR_NOMEM);
1267         }
1268         err = usbd_req_get_desc(udev, mtx, NULL, cdesc, len, len, 0,
1269             UDESC_CONFIG, index, 3);
1270         if (err) {
1271                 free(cdesc, mtype);
1272                 return (err);
1273         }
1274         /* make sure that the device is not fooling us: */
1275         USETW(cdesc->wTotalLength, len);
1276
1277         *ppcd = cdesc;
1278
1279         return (0);                     /* success */
1280 }
1281
1282 /*------------------------------------------------------------------------*
1283  *      usbd_req_get_device_desc
1284  *
1285  * Returns:
1286  *    0: Success
1287  * Else: Failure
1288  *------------------------------------------------------------------------*/
1289 usb_error_t
1290 usbd_req_get_device_desc(struct usb_device *udev, struct mtx *mtx,
1291     struct usb_device_descriptor *d)
1292 {
1293         DPRINTFN(4, "\n");
1294         return (usbd_req_get_desc(udev, mtx, NULL, d, sizeof(*d),
1295             sizeof(*d), 0, UDESC_DEVICE, 0, 3));
1296 }
1297
1298 /*------------------------------------------------------------------------*
1299  *      usbd_req_get_alt_interface_no
1300  *
1301  * Returns:
1302  *    0: Success
1303  * Else: Failure
1304  *------------------------------------------------------------------------*/
1305 usb_error_t
1306 usbd_req_get_alt_interface_no(struct usb_device *udev, struct mtx *mtx,
1307     uint8_t *alt_iface_no, uint8_t iface_index)
1308 {
1309         struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1310         struct usb_device_request req;
1311
1312         if ((iface == NULL) || (iface->idesc == NULL))
1313                 return (USB_ERR_INVAL);
1314
1315         req.bmRequestType = UT_READ_INTERFACE;
1316         req.bRequest = UR_GET_INTERFACE;
1317         USETW(req.wValue, 0);
1318         req.wIndex[0] = iface->idesc->bInterfaceNumber;
1319         req.wIndex[1] = 0;
1320         USETW(req.wLength, 1);
1321         return (usbd_do_request(udev, mtx, &req, alt_iface_no));
1322 }
1323
1324 /*------------------------------------------------------------------------*
1325  *      usbd_req_set_alt_interface_no
1326  *
1327  * Returns:
1328  *    0: Success
1329  * Else: Failure
1330  *------------------------------------------------------------------------*/
1331 usb_error_t
1332 usbd_req_set_alt_interface_no(struct usb_device *udev, struct mtx *mtx,
1333     uint8_t iface_index, uint8_t alt_no)
1334 {
1335         struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1336         struct usb_device_request req;
1337
1338         if ((iface == NULL) || (iface->idesc == NULL))
1339                 return (USB_ERR_INVAL);
1340
1341         req.bmRequestType = UT_WRITE_INTERFACE;
1342         req.bRequest = UR_SET_INTERFACE;
1343         req.wValue[0] = alt_no;
1344         req.wValue[1] = 0;
1345         req.wIndex[0] = iface->idesc->bInterfaceNumber;
1346         req.wIndex[1] = 0;
1347         USETW(req.wLength, 0);
1348         return (usbd_do_request(udev, mtx, &req, 0));
1349 }
1350
1351 /*------------------------------------------------------------------------*
1352  *      usbd_req_get_device_status
1353  *
1354  * Returns:
1355  *    0: Success
1356  * Else: Failure
1357  *------------------------------------------------------------------------*/
1358 usb_error_t
1359 usbd_req_get_device_status(struct usb_device *udev, struct mtx *mtx,
1360     struct usb_status *st)
1361 {
1362         struct usb_device_request req;
1363
1364         req.bmRequestType = UT_READ_DEVICE;
1365         req.bRequest = UR_GET_STATUS;
1366         USETW(req.wValue, 0);
1367         USETW(req.wIndex, 0);
1368         USETW(req.wLength, sizeof(*st));
1369         return (usbd_do_request(udev, mtx, &req, st));
1370 }
1371
1372 /*------------------------------------------------------------------------*
1373  *      usbd_req_get_hub_descriptor
1374  *
1375  * Returns:
1376  *    0: Success
1377  * Else: Failure
1378  *------------------------------------------------------------------------*/
1379 usb_error_t
1380 usbd_req_get_hub_descriptor(struct usb_device *udev, struct mtx *mtx,
1381     struct usb_hub_descriptor *hd, uint8_t nports)
1382 {
1383         struct usb_device_request req;
1384         uint16_t len = (nports + 7 + (8 * 8)) / 8;
1385
1386         req.bmRequestType = UT_READ_CLASS_DEVICE;
1387         req.bRequest = UR_GET_DESCRIPTOR;
1388         USETW2(req.wValue, UDESC_HUB, 0);
1389         USETW(req.wIndex, 0);
1390         USETW(req.wLength, len);
1391         return (usbd_do_request(udev, mtx, &req, hd));
1392 }
1393
1394 /*------------------------------------------------------------------------*
1395  *      usbd_req_get_ss_hub_descriptor
1396  *
1397  * Returns:
1398  *    0: Success
1399  * Else: Failure
1400  *------------------------------------------------------------------------*/
1401 usb_error_t
1402 usbd_req_get_ss_hub_descriptor(struct usb_device *udev, struct mtx *mtx,
1403     struct usb_hub_ss_descriptor *hd, uint8_t nports)
1404 {
1405         struct usb_device_request req;
1406         uint16_t len = sizeof(*hd) - 32 + 1 + ((nports + 7) / 8);
1407
1408         req.bmRequestType = UT_READ_CLASS_DEVICE;
1409         req.bRequest = UR_GET_DESCRIPTOR;
1410         USETW2(req.wValue, UDESC_SS_HUB, 0);
1411         USETW(req.wIndex, 0);
1412         USETW(req.wLength, len);
1413         return (usbd_do_request(udev, mtx, &req, hd));
1414 }
1415
1416 /*------------------------------------------------------------------------*
1417  *      usbd_req_get_hub_status
1418  *
1419  * Returns:
1420  *    0: Success
1421  * Else: Failure
1422  *------------------------------------------------------------------------*/
1423 usb_error_t
1424 usbd_req_get_hub_status(struct usb_device *udev, struct mtx *mtx,
1425     struct usb_hub_status *st)
1426 {
1427         struct usb_device_request req;
1428
1429         req.bmRequestType = UT_READ_CLASS_DEVICE;
1430         req.bRequest = UR_GET_STATUS;
1431         USETW(req.wValue, 0);
1432         USETW(req.wIndex, 0);
1433         USETW(req.wLength, sizeof(struct usb_hub_status));
1434         return (usbd_do_request(udev, mtx, &req, st));
1435 }
1436
1437 /*------------------------------------------------------------------------*
1438  *      usbd_req_set_address
1439  *
1440  * This function is used to set the address for an USB device. After
1441  * port reset the USB device will respond at address zero.
1442  *
1443  * Returns:
1444  *    0: Success
1445  * Else: Failure
1446  *------------------------------------------------------------------------*/
1447 usb_error_t
1448 usbd_req_set_address(struct usb_device *udev, struct mtx *mtx, uint16_t addr)
1449 {
1450         struct usb_device_request req;
1451         usb_error_t err;
1452
1453         DPRINTFN(6, "setting device address=%d\n", addr);
1454
1455         req.bmRequestType = UT_WRITE_DEVICE;
1456         req.bRequest = UR_SET_ADDRESS;
1457         USETW(req.wValue, addr);
1458         USETW(req.wIndex, 0);
1459         USETW(req.wLength, 0);
1460
1461         err = USB_ERR_INVAL;
1462
1463         /* check if USB controller handles set address */
1464         if (udev->bus->methods->set_address != NULL)
1465                 err = (udev->bus->methods->set_address) (udev, mtx, addr);
1466
1467         if (err != USB_ERR_INVAL)
1468                 goto done;
1469
1470         /* Setting the address should not take more than 1 second ! */
1471         err = usbd_do_request_flags(udev, mtx, &req, NULL,
1472             USB_DELAY_STATUS_STAGE, NULL, 1000);
1473
1474 done:
1475         /* allow device time to set new address */
1476         usb_pause_mtx(mtx,
1477             USB_MS_TO_TICKS(USB_SET_ADDRESS_SETTLE));
1478
1479         return (err);
1480 }
1481
1482 /*------------------------------------------------------------------------*
1483  *      usbd_req_get_port_status
1484  *
1485  * Returns:
1486  *    0: Success
1487  * Else: Failure
1488  *------------------------------------------------------------------------*/
1489 usb_error_t
1490 usbd_req_get_port_status(struct usb_device *udev, struct mtx *mtx,
1491     struct usb_port_status *ps, uint8_t port)
1492 {
1493         struct usb_device_request req;
1494
1495         req.bmRequestType = UT_READ_CLASS_OTHER;
1496         req.bRequest = UR_GET_STATUS;
1497         USETW(req.wValue, 0);
1498         req.wIndex[0] = port;
1499         req.wIndex[1] = 0;
1500         USETW(req.wLength, sizeof *ps);
1501         return (usbd_do_request(udev, mtx, &req, ps));
1502 }
1503
1504 /*------------------------------------------------------------------------*
1505  *      usbd_req_clear_hub_feature
1506  *
1507  * Returns:
1508  *    0: Success
1509  * Else: Failure
1510  *------------------------------------------------------------------------*/
1511 usb_error_t
1512 usbd_req_clear_hub_feature(struct usb_device *udev, struct mtx *mtx,
1513     uint16_t sel)
1514 {
1515         struct usb_device_request req;
1516
1517         req.bmRequestType = UT_WRITE_CLASS_DEVICE;
1518         req.bRequest = UR_CLEAR_FEATURE;
1519         USETW(req.wValue, sel);
1520         USETW(req.wIndex, 0);
1521         USETW(req.wLength, 0);
1522         return (usbd_do_request(udev, mtx, &req, 0));
1523 }
1524
1525 /*------------------------------------------------------------------------*
1526  *      usbd_req_set_hub_feature
1527  *
1528  * Returns:
1529  *    0: Success
1530  * Else: Failure
1531  *------------------------------------------------------------------------*/
1532 usb_error_t
1533 usbd_req_set_hub_feature(struct usb_device *udev, struct mtx *mtx,
1534     uint16_t sel)
1535 {
1536         struct usb_device_request req;
1537
1538         req.bmRequestType = UT_WRITE_CLASS_DEVICE;
1539         req.bRequest = UR_SET_FEATURE;
1540         USETW(req.wValue, sel);
1541         USETW(req.wIndex, 0);
1542         USETW(req.wLength, 0);
1543         return (usbd_do_request(udev, mtx, &req, 0));
1544 }
1545
1546 /*------------------------------------------------------------------------*
1547  *      usbd_req_set_hub_u1_timeout
1548  *
1549  * Returns:
1550  *    0: Success
1551  * Else: Failure
1552  *------------------------------------------------------------------------*/
1553 usb_error_t
1554 usbd_req_set_hub_u1_timeout(struct usb_device *udev, struct mtx *mtx,
1555     uint8_t port, uint8_t timeout)
1556 {
1557         struct usb_device_request req;
1558
1559         req.bmRequestType = UT_WRITE_CLASS_OTHER;
1560         req.bRequest = UR_SET_FEATURE;
1561         USETW(req.wValue, UHF_PORT_U1_TIMEOUT);
1562         req.wIndex[0] = port;
1563         req.wIndex[1] = timeout;
1564         USETW(req.wLength, 0);
1565         return (usbd_do_request(udev, mtx, &req, 0));
1566 }
1567
1568 /*------------------------------------------------------------------------*
1569  *      usbd_req_set_hub_u2_timeout
1570  *
1571  * Returns:
1572  *    0: Success
1573  * Else: Failure
1574  *------------------------------------------------------------------------*/
1575 usb_error_t
1576 usbd_req_set_hub_u2_timeout(struct usb_device *udev, struct mtx *mtx,
1577     uint8_t port, uint8_t timeout)
1578 {
1579         struct usb_device_request req;
1580
1581         req.bmRequestType = UT_WRITE_CLASS_OTHER;
1582         req.bRequest = UR_SET_FEATURE;
1583         USETW(req.wValue, UHF_PORT_U2_TIMEOUT);
1584         req.wIndex[0] = port;
1585         req.wIndex[1] = timeout;
1586         USETW(req.wLength, 0);
1587         return (usbd_do_request(udev, mtx, &req, 0));
1588 }
1589
1590 /*------------------------------------------------------------------------*
1591  *      usbd_req_set_hub_depth
1592  *
1593  * Returns:
1594  *    0: Success
1595  * Else: Failure
1596  *------------------------------------------------------------------------*/
1597 usb_error_t
1598 usbd_req_set_hub_depth(struct usb_device *udev, struct mtx *mtx,
1599     uint16_t depth)
1600 {
1601         struct usb_device_request req;
1602
1603         req.bmRequestType = UT_WRITE_CLASS_DEVICE;
1604         req.bRequest = UR_SET_HUB_DEPTH;
1605         USETW(req.wValue, depth);
1606         USETW(req.wIndex, 0);
1607         USETW(req.wLength, 0);
1608         return (usbd_do_request(udev, mtx, &req, 0));
1609 }
1610
1611 /*------------------------------------------------------------------------*
1612  *      usbd_req_clear_port_feature
1613  *
1614  * Returns:
1615  *    0: Success
1616  * Else: Failure
1617  *------------------------------------------------------------------------*/
1618 usb_error_t
1619 usbd_req_clear_port_feature(struct usb_device *udev, struct mtx *mtx,
1620     uint8_t port, uint16_t sel)
1621 {
1622         struct usb_device_request req;
1623
1624         req.bmRequestType = UT_WRITE_CLASS_OTHER;
1625         req.bRequest = UR_CLEAR_FEATURE;
1626         USETW(req.wValue, sel);
1627         req.wIndex[0] = port;
1628         req.wIndex[1] = 0;
1629         USETW(req.wLength, 0);
1630         return (usbd_do_request(udev, mtx, &req, 0));
1631 }
1632
1633 /*------------------------------------------------------------------------*
1634  *      usbd_req_set_port_feature
1635  *
1636  * Returns:
1637  *    0: Success
1638  * Else: Failure
1639  *------------------------------------------------------------------------*/
1640 usb_error_t
1641 usbd_req_set_port_feature(struct usb_device *udev, struct mtx *mtx,
1642     uint8_t port, uint16_t sel)
1643 {
1644         struct usb_device_request req;
1645
1646         req.bmRequestType = UT_WRITE_CLASS_OTHER;
1647         req.bRequest = UR_SET_FEATURE;
1648         USETW(req.wValue, sel);
1649         req.wIndex[0] = port;
1650         req.wIndex[1] = 0;
1651         USETW(req.wLength, 0);
1652         return (usbd_do_request(udev, mtx, &req, 0));
1653 }
1654
1655 /*------------------------------------------------------------------------*
1656  *      usbd_req_set_protocol
1657  *
1658  * Returns:
1659  *    0: Success
1660  * Else: Failure
1661  *------------------------------------------------------------------------*/
1662 usb_error_t
1663 usbd_req_set_protocol(struct usb_device *udev, struct mtx *mtx,
1664     uint8_t iface_index, uint16_t report)
1665 {
1666         struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1667         struct usb_device_request req;
1668
1669         if ((iface == NULL) || (iface->idesc == NULL)) {
1670                 return (USB_ERR_INVAL);
1671         }
1672         DPRINTFN(5, "iface=%p, report=%d, endpt=%d\n",
1673             iface, report, iface->idesc->bInterfaceNumber);
1674
1675         req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1676         req.bRequest = UR_SET_PROTOCOL;
1677         USETW(req.wValue, report);
1678         req.wIndex[0] = iface->idesc->bInterfaceNumber;
1679         req.wIndex[1] = 0;
1680         USETW(req.wLength, 0);
1681         return (usbd_do_request(udev, mtx, &req, 0));
1682 }
1683
1684 /*------------------------------------------------------------------------*
1685  *      usbd_req_set_report
1686  *
1687  * Returns:
1688  *    0: Success
1689  * Else: Failure
1690  *------------------------------------------------------------------------*/
1691 usb_error_t
1692 usbd_req_set_report(struct usb_device *udev, struct mtx *mtx, void *data, uint16_t len,
1693     uint8_t iface_index, uint8_t type, uint8_t id)
1694 {
1695         struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1696         struct usb_device_request req;
1697
1698         if ((iface == NULL) || (iface->idesc == NULL)) {
1699                 return (USB_ERR_INVAL);
1700         }
1701         DPRINTFN(5, "len=%d\n", len);
1702
1703         req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1704         req.bRequest = UR_SET_REPORT;
1705         USETW2(req.wValue, type, id);
1706         req.wIndex[0] = iface->idesc->bInterfaceNumber;
1707         req.wIndex[1] = 0;
1708         USETW(req.wLength, len);
1709         return (usbd_do_request(udev, mtx, &req, data));
1710 }
1711
1712 /*------------------------------------------------------------------------*
1713  *      usbd_req_get_report
1714  *
1715  * Returns:
1716  *    0: Success
1717  * Else: Failure
1718  *------------------------------------------------------------------------*/
1719 usb_error_t
1720 usbd_req_get_report(struct usb_device *udev, struct mtx *mtx, void *data,
1721     uint16_t len, uint8_t iface_index, uint8_t type, uint8_t id)
1722 {
1723         struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1724         struct usb_device_request req;
1725
1726         if ((iface == NULL) || (iface->idesc == NULL) || (id == 0)) {
1727                 return (USB_ERR_INVAL);
1728         }
1729         DPRINTFN(5, "len=%d\n", len);
1730
1731         req.bmRequestType = UT_READ_CLASS_INTERFACE;
1732         req.bRequest = UR_GET_REPORT;
1733         USETW2(req.wValue, type, id);
1734         req.wIndex[0] = iface->idesc->bInterfaceNumber;
1735         req.wIndex[1] = 0;
1736         USETW(req.wLength, len);
1737         return (usbd_do_request(udev, mtx, &req, data));
1738 }
1739
1740 /*------------------------------------------------------------------------*
1741  *      usbd_req_set_idle
1742  *
1743  * Returns:
1744  *    0: Success
1745  * Else: Failure
1746  *------------------------------------------------------------------------*/
1747 usb_error_t
1748 usbd_req_set_idle(struct usb_device *udev, struct mtx *mtx,
1749     uint8_t iface_index, uint8_t duration, uint8_t id)
1750 {
1751         struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1752         struct usb_device_request req;
1753
1754         if ((iface == NULL) || (iface->idesc == NULL)) {
1755                 return (USB_ERR_INVAL);
1756         }
1757         DPRINTFN(5, "%d %d\n", duration, id);
1758
1759         req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1760         req.bRequest = UR_SET_IDLE;
1761         USETW2(req.wValue, duration, id);
1762         req.wIndex[0] = iface->idesc->bInterfaceNumber;
1763         req.wIndex[1] = 0;
1764         USETW(req.wLength, 0);
1765         return (usbd_do_request(udev, mtx, &req, 0));
1766 }
1767
1768 /*------------------------------------------------------------------------*
1769  *      usbd_req_get_report_descriptor
1770  *
1771  * Returns:
1772  *    0: Success
1773  * Else: Failure
1774  *------------------------------------------------------------------------*/
1775 usb_error_t
1776 usbd_req_get_report_descriptor(struct usb_device *udev, struct mtx *mtx,
1777     void *d, uint16_t size, uint8_t iface_index)
1778 {
1779         struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1780         struct usb_device_request req;
1781
1782         if ((iface == NULL) || (iface->idesc == NULL)) {
1783                 return (USB_ERR_INVAL);
1784         }
1785         req.bmRequestType = UT_READ_INTERFACE;
1786         req.bRequest = UR_GET_DESCRIPTOR;
1787         USETW2(req.wValue, UDESC_REPORT, 0);    /* report id should be 0 */
1788         req.wIndex[0] = iface->idesc->bInterfaceNumber;
1789         req.wIndex[1] = 0;
1790         USETW(req.wLength, size);
1791         return (usbd_do_request(udev, mtx, &req, d));
1792 }
1793
1794 /*------------------------------------------------------------------------*
1795  *      usbd_req_set_config
1796  *
1797  * This function is used to select the current configuration number in
1798  * both USB device side mode and USB host side mode. When setting the
1799  * configuration the function of the interfaces can change.
1800  *
1801  * Returns:
1802  *    0: Success
1803  * Else: Failure
1804  *------------------------------------------------------------------------*/
1805 usb_error_t
1806 usbd_req_set_config(struct usb_device *udev, struct mtx *mtx, uint8_t conf)
1807 {
1808         struct usb_device_request req;
1809
1810         DPRINTF("setting config %d\n", conf);
1811
1812         /* do "set configuration" request */
1813
1814         req.bmRequestType = UT_WRITE_DEVICE;
1815         req.bRequest = UR_SET_CONFIG;
1816         req.wValue[0] = conf;
1817         req.wValue[1] = 0;
1818         USETW(req.wIndex, 0);
1819         USETW(req.wLength, 0);
1820         return (usbd_do_request(udev, mtx, &req, 0));
1821 }
1822
1823 /*------------------------------------------------------------------------*
1824  *      usbd_req_get_config
1825  *
1826  * Returns:
1827  *    0: Success
1828  * Else: Failure
1829  *------------------------------------------------------------------------*/
1830 usb_error_t
1831 usbd_req_get_config(struct usb_device *udev, struct mtx *mtx, uint8_t *pconf)
1832 {
1833         struct usb_device_request req;
1834
1835         req.bmRequestType = UT_READ_DEVICE;
1836         req.bRequest = UR_GET_CONFIG;
1837         USETW(req.wValue, 0);
1838         USETW(req.wIndex, 0);
1839         USETW(req.wLength, 1);
1840         return (usbd_do_request(udev, mtx, &req, pconf));
1841 }
1842
1843 /*------------------------------------------------------------------------*
1844  *      usbd_setup_device_desc
1845  *------------------------------------------------------------------------*/
1846 usb_error_t
1847 usbd_setup_device_desc(struct usb_device *udev, struct mtx *mtx)
1848 {
1849         usb_error_t err;
1850
1851         /*
1852          * Get the first 8 bytes of the device descriptor !
1853          *
1854          * NOTE: "usbd_do_request()" will check the device descriptor
1855          * next time we do a request to see if the maximum packet size
1856          * changed! The 8 first bytes of the device descriptor
1857          * contains the maximum packet size to use on control endpoint
1858          * 0. If this value is different from "USB_MAX_IPACKET" a new
1859          * USB control request will be setup!
1860          */
1861         switch (udev->speed) {
1862         case USB_SPEED_FULL:
1863         case USB_SPEED_LOW:
1864                 err = usbd_req_get_desc(udev, mtx, NULL, &udev->ddesc,
1865                     USB_MAX_IPACKET, USB_MAX_IPACKET, 0, UDESC_DEVICE, 0, 0);
1866                 if (err != 0) {
1867                         DPRINTFN(0, "getting device descriptor "
1868                             "at addr %d failed, %s\n", udev->address,
1869                             usbd_errstr(err));
1870                         return (err);
1871                 }
1872                 break;
1873         default:
1874                 DPRINTF("Minimum MaxPacketSize is large enough "
1875                     "to hold the complete device descriptor\n");
1876                 break;
1877         }
1878
1879         /* get the full device descriptor */
1880         err = usbd_req_get_device_desc(udev, mtx, &udev->ddesc);
1881
1882         /* try one more time, if error */
1883         if (err)
1884                 err = usbd_req_get_device_desc(udev, mtx, &udev->ddesc);
1885
1886         if (err) {
1887                 DPRINTF("addr=%d, getting full desc failed\n",
1888                     udev->address);
1889                 return (err);
1890         }
1891
1892         DPRINTF("adding unit addr=%d, rev=%02x, class=%d, "
1893             "subclass=%d, protocol=%d, maxpacket=%d, len=%d, speed=%d\n",
1894             udev->address, UGETW(udev->ddesc.bcdUSB),
1895             udev->ddesc.bDeviceClass,
1896             udev->ddesc.bDeviceSubClass,
1897             udev->ddesc.bDeviceProtocol,
1898             udev->ddesc.bMaxPacketSize,
1899             udev->ddesc.bLength,
1900             udev->speed);
1901
1902         return (err);
1903 }
1904
1905 /*------------------------------------------------------------------------*
1906  *      usbd_req_re_enumerate
1907  *
1908  * NOTE: After this function returns the hardware is in the
1909  * unconfigured state! The application is responsible for setting a
1910  * new configuration.
1911  *
1912  * Returns:
1913  *    0: Success
1914  * Else: Failure
1915  *------------------------------------------------------------------------*/
1916 usb_error_t
1917 usbd_req_re_enumerate(struct usb_device *udev, struct mtx *mtx)
1918 {
1919         struct usb_device *parent_hub;
1920         usb_error_t err;
1921         uint8_t old_addr;
1922         uint8_t do_retry = 1;
1923
1924         if (udev->flags.usb_mode != USB_MODE_HOST) {
1925                 return (USB_ERR_INVAL);
1926         }
1927         old_addr = udev->address;
1928         parent_hub = udev->parent_hub;
1929         if (parent_hub == NULL) {
1930                 return (USB_ERR_INVAL);
1931         }
1932 retry:
1933         err = usbd_req_reset_port(parent_hub, mtx, udev->port_no);
1934         if (err) {
1935                 DPRINTFN(0, "addr=%d, port reset failed, %s\n", 
1936                     old_addr, usbd_errstr(err));
1937                 goto done;
1938         }
1939
1940         /*
1941          * After that the port has been reset our device should be at
1942          * address zero:
1943          */
1944         udev->address = USB_START_ADDR;
1945
1946         /* reset "bMaxPacketSize" */
1947         udev->ddesc.bMaxPacketSize = USB_MAX_IPACKET;
1948
1949         /* reset USB state */
1950         usb_set_device_state(udev, USB_STATE_POWERED);
1951
1952         /*
1953          * Restore device address:
1954          */
1955         err = usbd_req_set_address(udev, mtx, old_addr);
1956         if (err) {
1957                 /* XXX ignore any errors! */
1958                 DPRINTFN(0, "addr=%d, set address failed! (%s, ignored)\n",
1959                     old_addr, usbd_errstr(err));
1960         }
1961         /*
1962          * Restore device address, if the controller driver did not
1963          * set a new one:
1964          */
1965         if (udev->address == USB_START_ADDR)
1966                 udev->address = old_addr;
1967
1968         /* setup the device descriptor and the initial "wMaxPacketSize" */
1969         err = usbd_setup_device_desc(udev, mtx);
1970
1971 done:
1972         if (err && do_retry) {
1973                 /* give the USB firmware some time to load */
1974                 usb_pause_mtx(mtx, hz / 2);
1975                 /* no more retries after this retry */
1976                 do_retry = 0;
1977                 /* try again */
1978                 goto retry;
1979         }
1980         /* restore address */
1981         if (udev->address == USB_START_ADDR)
1982                 udev->address = old_addr;
1983         /* update state, if successful */
1984         if (err == 0)
1985                 usb_set_device_state(udev, USB_STATE_ADDRESSED);
1986         return (err);
1987 }
1988
1989 /*------------------------------------------------------------------------*
1990  *      usbd_req_clear_device_feature
1991  *
1992  * Returns:
1993  *    0: Success
1994  * Else: Failure
1995  *------------------------------------------------------------------------*/
1996 usb_error_t
1997 usbd_req_clear_device_feature(struct usb_device *udev, struct mtx *mtx,
1998     uint16_t sel)
1999 {
2000         struct usb_device_request req;
2001
2002         req.bmRequestType = UT_WRITE_DEVICE;
2003         req.bRequest = UR_CLEAR_FEATURE;
2004         USETW(req.wValue, sel);
2005         USETW(req.wIndex, 0);
2006         USETW(req.wLength, 0);
2007         return (usbd_do_request(udev, mtx, &req, 0));
2008 }
2009
2010 /*------------------------------------------------------------------------*
2011  *      usbd_req_set_device_feature
2012  *
2013  * Returns:
2014  *    0: Success
2015  * Else: Failure
2016  *------------------------------------------------------------------------*/
2017 usb_error_t
2018 usbd_req_set_device_feature(struct usb_device *udev, struct mtx *mtx,
2019     uint16_t sel)
2020 {
2021         struct usb_device_request req;
2022
2023         req.bmRequestType = UT_WRITE_DEVICE;
2024         req.bRequest = UR_SET_FEATURE;
2025         USETW(req.wValue, sel);
2026         USETW(req.wIndex, 0);
2027         USETW(req.wLength, 0);
2028         return (usbd_do_request(udev, mtx, &req, 0));
2029 }