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.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
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>
37 #include <sys/linker_set.h>
38 #include <sys/module.h>
40 #include <sys/mutex.h>
41 #include <sys/condvar.h>
42 #include <sys/sysctl.h>
44 #include <sys/unistd.h>
45 #include <sys/callout.h>
46 #include <sys/malloc.h>
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>
55 #define USB_DEBUG_VAR usb_debug
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>
67 #include <dev/usb/usb_controller.h>
68 #include <dev/usb/usb_bus.h>
69 #include <sys/ctype.h>
72 static int usb_pr_poll_delay = USB_PORT_RESET_DELAY;
73 static int usb_pr_recovery_delay = USB_PORT_RESET_RECOVERY;
74 static int usb_ss_delay = 0;
76 SYSCTL_INT(_hw_usb, OID_AUTO, pr_poll_delay, CTLFLAG_RW,
77 &usb_pr_poll_delay, 0, "USB port reset poll delay in ms");
78 SYSCTL_INT(_hw_usb, OID_AUTO, pr_recovery_delay, CTLFLAG_RW,
79 &usb_pr_recovery_delay, 0, "USB port reset recovery delay in ms");
80 SYSCTL_INT(_hw_usb, OID_AUTO, ss_delay, CTLFLAG_RW,
81 &usb_ss_delay, 0, "USB status stage delay in ms");
84 /*------------------------------------------------------------------------*
85 * usbd_do_request_callback
87 * This function is the USB callback for generic USB Host control
89 *------------------------------------------------------------------------*/
91 usbd_do_request_callback(struct usb_xfer *xfer, usb_error_t error)
93 ; /* workaround for a bug in "indent" */
95 DPRINTF("st=%u\n", USB_GET_STATE(xfer));
97 switch (USB_GET_STATE(xfer)) {
99 usbd_transfer_submit(xfer);
102 cv_signal(xfer->xroot->udev->default_cv);
107 /*------------------------------------------------------------------------*
108 * usb_do_clear_stall_callback
110 * This function is the USB callback for generic clear stall requests.
111 *------------------------------------------------------------------------*/
113 usb_do_clear_stall_callback(struct usb_xfer *xfer, usb_error_t error)
115 struct usb_device_request req;
116 struct usb_device *udev;
117 struct usb_endpoint *ep;
118 struct usb_endpoint *ep_end;
119 struct usb_endpoint *ep_first;
122 udev = xfer->xroot->udev;
124 USB_BUS_LOCK(udev->bus);
126 /* round robin endpoint clear stall */
129 ep_end = udev->endpoints + udev->endpoints_max;
130 ep_first = udev->endpoints;
131 to = udev->endpoints_max;
133 switch (USB_GET_STATE(xfer)) {
134 case USB_ST_TRANSFERRED:
136 goto tr_setup; /* device was unconfigured */
141 /* start up the current or next transfer, if any */
142 usb_command_wrapper(&ep->endpoint_q,
143 ep->endpoint_q.curr);
150 break; /* no endpoints - nothing to do */
151 if ((ep < ep_first) || (ep >= ep_end))
152 ep = ep_first; /* endpoint wrapped around */
156 /* setup a clear-stall packet */
158 req.bmRequestType = UT_WRITE_ENDPOINT;
159 req.bRequest = UR_CLEAR_FEATURE;
160 USETW(req.wValue, UF_ENDPOINT_HALT);
161 req.wIndex[0] = ep->edesc->bEndpointAddress;
163 USETW(req.wLength, 0);
165 /* copy in the transfer */
167 usbd_copy_in(xfer->frbuffers, 0, &req, sizeof(req));
170 usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
172 USB_BUS_UNLOCK(udev->bus);
174 usbd_transfer_submit(xfer);
176 USB_BUS_LOCK(udev->bus);
184 if (xfer->error == USB_ERR_CANCELLED) {
190 /* store current endpoint */
192 USB_BUS_UNLOCK(udev->bus);
195 static usb_handle_req_t *
196 usbd_get_hr_func(struct usb_device *udev)
198 /* figure out if there is a Handle Request function */
199 if (udev->flags.usb_mode == USB_MODE_DEVICE)
200 return (usb_temp_get_desc_p);
201 else if (udev->parent_hub == NULL)
202 return (udev->bus->methods->roothub_exec);
207 /*------------------------------------------------------------------------*
208 * usbd_do_request_flags and usbd_do_request
210 * Description of arguments passed to these functions:
212 * "udev" - this is the "usb_device" structure pointer on which the
213 * request should be performed. It is possible to call this function
214 * in both Host Side mode and Device Side mode.
216 * "mtx" - if this argument is non-NULL the mutex pointed to by it
217 * will get dropped and picked up during the execution of this
218 * function, hence this function sometimes needs to sleep. If this
219 * argument is NULL it has no effect.
221 * "req" - this argument must always be non-NULL and points to an
222 * 8-byte structure holding the USB request to be done. The USB
223 * request structure has a bit telling the direction of the USB
224 * request, if it is a read or a write.
226 * "data" - if the "wLength" part of the structure pointed to by "req"
227 * is non-zero this argument must point to a valid kernel buffer which
228 * can hold at least "wLength" bytes. If "wLength" is zero "data" can
231 * "flags" - here is a list of valid flags:
233 * o USB_SHORT_XFER_OK: allows the data transfer to be shorter than
236 * o USB_DELAY_STATUS_STAGE: allows the status stage to be performed
237 * at a later point in time. This is tunable by the "hw.usb.ss_delay"
238 * sysctl. This flag is mostly useful for debugging.
240 * o USB_USER_DATA_PTR: treat the "data" pointer like a userland
243 * "actlen" - if non-NULL the actual transfer length will be stored in
244 * the 16-bit unsigned integer pointed to by "actlen". This
245 * information is mostly useful when the "USB_SHORT_XFER_OK" flag is
248 * "timeout" - gives the timeout for the control transfer in
249 * milliseconds. A "timeout" value less than 50 milliseconds is
250 * treated like a 50 millisecond timeout. A "timeout" value greater
251 * than 30 seconds is treated like a 30 second timeout. This USB stack
252 * does not allow control requests without a timeout.
254 * NOTE: This function is thread safe. All calls to
255 * "usbd_do_request_flags" will be serialised by the use of an
256 * internal "sx_lock".
261 *------------------------------------------------------------------------*/
263 usbd_do_request_flags(struct usb_device *udev, struct mtx *mtx,
264 struct usb_device_request *req, void *data, uint16_t flags,
265 uint16_t *actlen, usb_timeout_t timeout)
267 usb_handle_req_t *hr_func;
268 struct usb_xfer *xfer;
271 usb_ticks_t start_ticks;
272 usb_ticks_t delta_ticks;
273 usb_ticks_t max_ticks;
278 /* timeout is too small */
281 if (timeout > 30000) {
282 /* timeout is too big */
285 length = UGETW(req->wLength);
287 DPRINTFN(5, "udev=%p bmRequestType=0x%02x bRequest=0x%02x "
288 "wValue=0x%02x%02x wIndex=0x%02x%02x wLength=0x%02x%02x\n",
289 udev, req->bmRequestType, req->bRequest,
290 req->wValue[1], req->wValue[0],
291 req->wIndex[1], req->wIndex[0],
292 req->wLength[1], req->wLength[0]);
294 /* Check if the device is still alive */
295 if (udev->state < USB_STATE_POWERED) {
296 DPRINTF("usb device has gone\n");
297 return (USB_ERR_NOT_CONFIGURED);
301 * Set "actlen" to a known value in case the caller does not
302 * check the return value:
307 #if (USB_HAVE_USER_IO == 0)
308 if (flags & USB_USER_DATA_PTR)
309 return (USB_ERR_INVAL);
314 mtx_assert(mtx, MA_NOTOWNED);
318 * Grab the default sx-lock so that serialisation
319 * is achieved when multiple threads are involved:
322 sx_xlock(udev->default_sx);
324 hr_func = usbd_get_hr_func(udev);
326 if (hr_func != NULL) {
327 DPRINTF("Handle Request function is set\n");
332 if (!(req->bmRequestType & UT_READ)) {
334 DPRINTFN(1, "The handle request function "
335 "does not support writing data!\n");
341 /* The root HUB code needs the BUS lock locked */
343 USB_BUS_LOCK(udev->bus);
344 err = (hr_func) (udev, req, &desc, &temp);
345 USB_BUS_UNLOCK(udev->bus);
351 if (!(flags & USB_SHORT_XFER_OK)) {
352 err = USB_ERR_SHORT_XFER;
362 if (flags & USB_USER_DATA_PTR) {
363 if (copyout(desc, data, length)) {
369 bcopy(desc, data, length);
371 goto done; /* success */
375 * Setup a new USB transfer or use the existing one, if any:
377 usbd_default_transfer_setup(udev);
379 xfer = udev->default_xfer[0];
381 /* most likely out of memory */
387 if (flags & USB_DELAY_STATUS_STAGE)
388 xfer->flags.manual_status = 1;
390 xfer->flags.manual_status = 0;
392 if (flags & USB_SHORT_XFER_OK)
393 xfer->flags.short_xfer_ok = 1;
395 xfer->flags.short_xfer_ok = 0;
397 xfer->timeout = timeout;
401 max_ticks = USB_MS_TO_TICKS(timeout);
403 usbd_copy_in(xfer->frbuffers, 0, req, sizeof(*req));
405 usbd_xfer_set_frame_len(xfer, 0, sizeof(*req));
410 if (temp > xfer->max_data_length) {
411 temp = usbd_xfer_max_len(xfer);
413 usbd_xfer_set_frame_len(xfer, 1, temp);
416 if (!(req->bmRequestType & UT_READ)) {
418 if (flags & USB_USER_DATA_PTR) {
419 USB_XFER_UNLOCK(xfer);
420 err = usbd_copy_in_user(xfer->frbuffers + 1,
429 usbd_copy_in(xfer->frbuffers + 1,
434 if (xfer->frlengths[0] == 0) {
435 if (xfer->flags.manual_status) {
445 xfer->xroot->xfer_mtx,
446 USB_MS_TO_TICKS(temp));
449 xfer->flags.manual_status = 0;
457 usbd_transfer_start(xfer);
459 while (usbd_transfer_pending(xfer)) {
460 cv_wait(udev->default_cv,
461 xfer->xroot->xfer_mtx);
469 /* subtract length of SETUP packet, if any */
471 if (xfer->aframes > 0) {
472 xfer->actlen -= xfer->frlengths[0];
477 /* check for short packet */
479 if (temp > xfer->actlen) {
484 if (req->bmRequestType & UT_READ) {
486 if (flags & USB_USER_DATA_PTR) {
487 USB_XFER_UNLOCK(xfer);
488 err = usbd_copy_out_user(xfer->frbuffers + 1,
497 usbd_copy_out(xfer->frbuffers + 1,
502 * Clear "frlengths[0]" so that we don't send the setup
505 usbd_xfer_set_frame_len(xfer, 0, 0);
507 /* update length and data pointer */
509 data = USB_ADD_BYTES(data, temp);
514 /* check for timeout */
516 delta_ticks = ticks - start_ticks;
517 if (delta_ticks > max_ticks) {
519 err = USB_ERR_TIMEOUT;
529 * Make sure that the control endpoint is no longer
530 * blocked in case of a non-transfer related error:
532 usbd_transfer_stop(xfer);
534 USB_XFER_UNLOCK(xfer);
537 sx_xunlock(udev->default_sx);
542 return ((usb_error_t)err);
545 /*------------------------------------------------------------------------*
546 * usbd_do_request_proc - factored out code
548 * This function is factored out code. It does basically the same like
549 * usbd_do_request_flags, except it will check the status of the
550 * passed process argument before doing the USB request. If the
551 * process is draining the USB_ERR_IOERROR code will be returned. It
552 * is assumed that the mutex associated with the process is locked
553 * when calling this function.
554 *------------------------------------------------------------------------*/
556 usbd_do_request_proc(struct usb_device *udev, struct usb_process *pproc,
557 struct usb_device_request *req, void *data, uint16_t flags,
558 uint16_t *actlen, usb_timeout_t timeout)
563 /* get request data length */
564 len = UGETW(req->wLength);
566 /* check if the device is being detached */
567 if (usb_proc_is_gone(pproc)) {
568 err = USB_ERR_IOERROR;
572 /* forward the USB request */
573 err = usbd_do_request_flags(udev, pproc->up_mtx,
574 req, data, flags, actlen, timeout);
577 /* on failure we zero the data */
578 /* on short packet we zero the unused data */
579 if ((len != 0) && (req->bmRequestType & UE_DIR_IN)) {
581 memset(data, 0, len);
582 else if (actlen && *actlen != len)
583 memset(((uint8_t *)data) + *actlen, 0, len - *actlen);
588 /*------------------------------------------------------------------------*
589 * usbd_req_reset_port
591 * This function will instruct an USB HUB to perform a reset sequence
592 * on the specified port number.
595 * 0: Success. The USB device should now be at address zero.
596 * Else: Failure. No USB device is present and the USB port should be
598 *------------------------------------------------------------------------*/
600 usbd_req_reset_port(struct usb_device *udev, struct mtx *mtx, uint8_t port)
602 struct usb_port_status ps;
607 uint16_t pr_poll_delay;
608 uint16_t pr_recovery_delay;
611 err = usbd_req_set_port_feature(udev, mtx, port, UHF_PORT_RESET);
616 /* range check input parameters */
617 pr_poll_delay = usb_pr_poll_delay;
618 if (pr_poll_delay < 1) {
620 } else if (pr_poll_delay > 1000) {
621 pr_poll_delay = 1000;
623 pr_recovery_delay = usb_pr_recovery_delay;
624 if (pr_recovery_delay > 1000) {
625 pr_recovery_delay = 1000;
631 /* wait for the device to recover from reset */
632 usb_pause_mtx(mtx, USB_MS_TO_TICKS(pr_poll_delay));
635 /* wait for the device to recover from reset */
636 usb_pause_mtx(mtx, USB_MS_TO_TICKS(USB_PORT_RESET_DELAY));
637 n += USB_PORT_RESET_DELAY;
639 err = usbd_req_get_port_status(udev, mtx, &ps, port);
643 /* if the device disappeared, just give up */
644 if (!(UGETW(ps.wPortStatus) & UPS_CURRENT_CONNECT_STATUS)) {
647 /* check if reset is complete */
648 if (UGETW(ps.wPortChange) & UPS_C_PORT_RESET) {
651 /* check for timeout */
658 /* clear port reset first */
659 err = usbd_req_clear_port_feature(
660 udev, mtx, port, UHF_C_PORT_RESET);
664 /* check for timeout */
666 err = USB_ERR_TIMEOUT;
670 /* wait for the device to recover from reset */
671 usb_pause_mtx(mtx, USB_MS_TO_TICKS(pr_recovery_delay));
673 /* wait for the device to recover from reset */
674 usb_pause_mtx(mtx, USB_MS_TO_TICKS(USB_PORT_RESET_RECOVERY));
678 DPRINTFN(2, "port %d reset returning error=%s\n",
679 port, usbd_errstr(err));
683 /*------------------------------------------------------------------------*
686 * This function can be used to retrieve USB descriptors. It contains
687 * some additional logic like zeroing of missing descriptor bytes and
688 * retrying an USB descriptor in case of failure. The "min_len"
689 * argument specifies the minimum descriptor length. The "max_len"
690 * argument specifies the maximum descriptor length. If the real
691 * descriptor length is less than the minimum length the missing
692 * byte(s) will be zeroed. The type field, the second byte of the USB
693 * descriptor, will get forced to the correct type. If the "actlen"
694 * pointer is non-NULL, the actual length of the transfer will get
695 * stored in the 16-bit unsigned integer which it is pointing to. The
696 * first byte of the descriptor will not get updated. If the "actlen"
697 * pointer is NULL the first byte of the descriptor will get updated
698 * to reflect the actual length instead. If "min_len" is not equal to
699 * "max_len" then this function will try to retrive the beginning of
700 * the descriptor and base the maximum length on the first byte of the
706 *------------------------------------------------------------------------*/
708 usbd_req_get_desc(struct usb_device *udev,
709 struct mtx *mtx, uint16_t *actlen, void *desc,
710 uint16_t min_len, uint16_t max_len,
711 uint16_t id, uint8_t type, uint8_t index,
714 struct usb_device_request req;
718 DPRINTFN(4, "id=%d, type=%d, index=%d, max_len=%d\n",
719 id, type, index, max_len);
721 req.bmRequestType = UT_READ_DEVICE;
722 req.bRequest = UR_GET_DESCRIPTOR;
723 USETW2(req.wValue, type, index);
724 USETW(req.wIndex, id);
728 if ((min_len < 2) || (max_len < 2)) {
732 USETW(req.wLength, min_len);
734 err = usbd_do_request_flags(udev, mtx, &req,
735 desc, 0, NULL, 1000);
743 usb_pause_mtx(mtx, hz / 5);
749 if (min_len == max_len) {
751 /* enforce correct length */
752 if ((buf[0] > min_len) && (actlen == NULL))
755 /* enforce correct type */
762 if (max_len > buf[0]) {
765 /* zero minimum data */
767 while (min_len > max_len) {
772 /* set new minimum length */
777 if (actlen != NULL) {
786 /*------------------------------------------------------------------------*
787 * usbd_req_get_string_any
789 * This function will return the string given by "string_index"
790 * using the first language ID. The maximum length "len" includes
791 * the terminating zero. The "len" argument should be twice as
792 * big pluss 2 bytes, compared with the actual maximum string length !
797 *------------------------------------------------------------------------*/
799 usbd_req_get_string_any(struct usb_device *udev, struct mtx *mtx, char *buf,
800 uint16_t len, uint8_t string_index)
811 /* should not happen */
812 return (USB_ERR_NORMAL_COMPLETION);
814 if (string_index == 0) {
815 /* this is the language table */
817 return (USB_ERR_INVAL);
819 if (udev->flags.no_strings) {
821 return (USB_ERR_STALLED);
823 err = usbd_req_get_string_desc
824 (udev, mtx, buf, len, udev->langid, string_index);
829 temp = (uint8_t *)buf;
832 /* string length is too short */
834 return (USB_ERR_INVAL);
836 /* reserve one byte for terminating zero */
839 /* find maximum length */
841 n = (temp[0] / 2) - 1;
845 /* skip descriptor header */
848 /* reset swap state */
851 /* convert and filter */
852 for (i = 0; (i != n); i++) {
853 c = UGETW(temp + (2 * i));
855 /* convert from Unicode, handle buggy strings */
856 if (((c & 0xff00) == 0) && (swap & 1)) {
857 /* Little Endian, default */
860 } else if (((c & 0x00ff) == 0) && (swap & 2)) {
865 /* silently skip bad character */
870 * Filter by default - we don't allow greater and less than
871 * signs because they might confuse the dmesg printouts!
873 if ((*s == '<') || (*s == '>') || (!isprint(*s))) {
874 /* silently skip bad character */
879 *s = 0; /* zero terminate resulting string */
880 return (USB_ERR_NORMAL_COMPLETION);
883 /*------------------------------------------------------------------------*
884 * usbd_req_get_string_desc
886 * If you don't know the language ID, consider using
887 * "usbd_req_get_string_any()".
892 *------------------------------------------------------------------------*/
894 usbd_req_get_string_desc(struct usb_device *udev, struct mtx *mtx, void *sdesc,
895 uint16_t max_len, uint16_t lang_id,
896 uint8_t string_index)
898 return (usbd_req_get_desc(udev, mtx, NULL, sdesc, 2, max_len, lang_id,
899 UDESC_STRING, string_index, 0));
902 /*------------------------------------------------------------------------*
903 * usbd_req_get_config_desc_ptr
905 * This function is used in device side mode to retrieve the pointer
906 * to the generated config descriptor. This saves allocating space for
907 * an additional config descriptor when setting the configuration.
912 *------------------------------------------------------------------------*/
914 usbd_req_get_descriptor_ptr(struct usb_device *udev,
915 struct usb_config_descriptor **ppcd, uint16_t wValue)
917 struct usb_device_request req;
918 usb_handle_req_t *hr_func;
923 req.bmRequestType = UT_READ_DEVICE;
924 req.bRequest = UR_GET_DESCRIPTOR;
925 USETW(req.wValue, wValue);
926 USETW(req.wIndex, 0);
927 USETW(req.wLength, 0);
932 hr_func = usbd_get_hr_func(udev);
937 USB_BUS_LOCK(udev->bus);
938 err = (hr_func) (udev, &req, &ptr, &len);
939 USB_BUS_UNLOCK(udev->bus);
944 else if (ptr == NULL)
947 *ppcd = __DECONST(struct usb_config_descriptor *, ptr);
952 /*------------------------------------------------------------------------*
953 * usbd_req_get_config_desc
958 *------------------------------------------------------------------------*/
960 usbd_req_get_config_desc(struct usb_device *udev, struct mtx *mtx,
961 struct usb_config_descriptor *d, uint8_t conf_index)
965 DPRINTFN(4, "confidx=%d\n", conf_index);
967 err = usbd_req_get_desc(udev, mtx, NULL, d, sizeof(*d),
968 sizeof(*d), 0, UDESC_CONFIG, conf_index, 0);
972 /* Extra sanity checking */
973 if (UGETW(d->wTotalLength) < sizeof(*d)) {
980 /*------------------------------------------------------------------------*
981 * usbd_req_get_config_desc_full
983 * This function gets the complete USB configuration descriptor and
984 * ensures that "wTotalLength" is correct.
989 *------------------------------------------------------------------------*/
991 usbd_req_get_config_desc_full(struct usb_device *udev, struct mtx *mtx,
992 struct usb_config_descriptor **ppcd, struct malloc_type *mtype,
995 struct usb_config_descriptor cd;
996 struct usb_config_descriptor *cdesc;
1000 DPRINTFN(4, "index=%d\n", index);
1004 err = usbd_req_get_config_desc(udev, mtx, &cd, index);
1008 /* get full descriptor */
1009 len = UGETW(cd.wTotalLength);
1010 if (len < sizeof(*cdesc)) {
1011 /* corrupt descriptor */
1012 return (USB_ERR_INVAL);
1014 cdesc = malloc(len, mtype, M_WAITOK);
1015 if (cdesc == NULL) {
1016 return (USB_ERR_NOMEM);
1018 err = usbd_req_get_desc(udev, mtx, NULL, cdesc, len, len, 0,
1019 UDESC_CONFIG, index, 3);
1024 /* make sure that the device is not fooling us: */
1025 USETW(cdesc->wTotalLength, len);
1029 return (0); /* success */
1032 /*------------------------------------------------------------------------*
1033 * usbd_req_get_device_desc
1038 *------------------------------------------------------------------------*/
1040 usbd_req_get_device_desc(struct usb_device *udev, struct mtx *mtx,
1041 struct usb_device_descriptor *d)
1044 return (usbd_req_get_desc(udev, mtx, NULL, d, sizeof(*d),
1045 sizeof(*d), 0, UDESC_DEVICE, 0, 3));
1048 /*------------------------------------------------------------------------*
1049 * usbd_req_get_alt_interface_no
1054 *------------------------------------------------------------------------*/
1056 usbd_req_get_alt_interface_no(struct usb_device *udev, struct mtx *mtx,
1057 uint8_t *alt_iface_no, uint8_t iface_index)
1059 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1060 struct usb_device_request req;
1062 if ((iface == NULL) || (iface->idesc == NULL))
1063 return (USB_ERR_INVAL);
1065 req.bmRequestType = UT_READ_INTERFACE;
1066 req.bRequest = UR_GET_INTERFACE;
1067 USETW(req.wValue, 0);
1068 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1070 USETW(req.wLength, 1);
1071 return (usbd_do_request(udev, mtx, &req, alt_iface_no));
1074 /*------------------------------------------------------------------------*
1075 * usbd_req_set_alt_interface_no
1080 *------------------------------------------------------------------------*/
1082 usbd_req_set_alt_interface_no(struct usb_device *udev, struct mtx *mtx,
1083 uint8_t iface_index, uint8_t alt_no)
1085 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1086 struct usb_device_request req;
1088 if ((iface == NULL) || (iface->idesc == NULL))
1089 return (USB_ERR_INVAL);
1091 req.bmRequestType = UT_WRITE_INTERFACE;
1092 req.bRequest = UR_SET_INTERFACE;
1093 req.wValue[0] = alt_no;
1095 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1097 USETW(req.wLength, 0);
1098 return (usbd_do_request(udev, mtx, &req, 0));
1101 /*------------------------------------------------------------------------*
1102 * usbd_req_get_device_status
1107 *------------------------------------------------------------------------*/
1109 usbd_req_get_device_status(struct usb_device *udev, struct mtx *mtx,
1110 struct usb_status *st)
1112 struct usb_device_request req;
1114 req.bmRequestType = UT_READ_DEVICE;
1115 req.bRequest = UR_GET_STATUS;
1116 USETW(req.wValue, 0);
1117 USETW(req.wIndex, 0);
1118 USETW(req.wLength, sizeof(*st));
1119 return (usbd_do_request(udev, mtx, &req, st));
1122 /*------------------------------------------------------------------------*
1123 * usbd_req_get_hub_descriptor
1128 *------------------------------------------------------------------------*/
1130 usbd_req_get_hub_descriptor(struct usb_device *udev, struct mtx *mtx,
1131 struct usb_hub_descriptor *hd, uint8_t nports)
1133 struct usb_device_request req;
1134 uint16_t len = (nports + 7 + (8 * 8)) / 8;
1136 req.bmRequestType = UT_READ_CLASS_DEVICE;
1137 req.bRequest = UR_GET_DESCRIPTOR;
1138 USETW2(req.wValue, UDESC_HUB, 0);
1139 USETW(req.wIndex, 0);
1140 USETW(req.wLength, len);
1141 return (usbd_do_request(udev, mtx, &req, hd));
1144 /*------------------------------------------------------------------------*
1145 * usbd_req_get_hub_status
1150 *------------------------------------------------------------------------*/
1152 usbd_req_get_hub_status(struct usb_device *udev, struct mtx *mtx,
1153 struct usb_hub_status *st)
1155 struct usb_device_request req;
1157 req.bmRequestType = UT_READ_CLASS_DEVICE;
1158 req.bRequest = UR_GET_STATUS;
1159 USETW(req.wValue, 0);
1160 USETW(req.wIndex, 0);
1161 USETW(req.wLength, sizeof(struct usb_hub_status));
1162 return (usbd_do_request(udev, mtx, &req, st));
1165 /*------------------------------------------------------------------------*
1166 * usbd_req_set_address
1168 * This function is used to set the address for an USB device. After
1169 * port reset the USB device will respond at address zero.
1174 *------------------------------------------------------------------------*/
1176 usbd_req_set_address(struct usb_device *udev, struct mtx *mtx, uint16_t addr)
1178 struct usb_device_request req;
1180 DPRINTFN(6, "setting device address=%d\n", addr);
1182 req.bmRequestType = UT_WRITE_DEVICE;
1183 req.bRequest = UR_SET_ADDRESS;
1184 USETW(req.wValue, addr);
1185 USETW(req.wIndex, 0);
1186 USETW(req.wLength, 0);
1188 /* Setting the address should not take more than 1 second ! */
1189 return (usbd_do_request_flags(udev, mtx, &req, NULL,
1190 USB_DELAY_STATUS_STAGE, NULL, 1000));
1193 /*------------------------------------------------------------------------*
1194 * usbd_req_get_port_status
1199 *------------------------------------------------------------------------*/
1201 usbd_req_get_port_status(struct usb_device *udev, struct mtx *mtx,
1202 struct usb_port_status *ps, uint8_t port)
1204 struct usb_device_request req;
1206 req.bmRequestType = UT_READ_CLASS_OTHER;
1207 req.bRequest = UR_GET_STATUS;
1208 USETW(req.wValue, 0);
1209 req.wIndex[0] = port;
1211 USETW(req.wLength, sizeof *ps);
1212 return (usbd_do_request(udev, mtx, &req, ps));
1215 /*------------------------------------------------------------------------*
1216 * usbd_req_clear_hub_feature
1221 *------------------------------------------------------------------------*/
1223 usbd_req_clear_hub_feature(struct usb_device *udev, struct mtx *mtx,
1226 struct usb_device_request req;
1228 req.bmRequestType = UT_WRITE_CLASS_DEVICE;
1229 req.bRequest = UR_CLEAR_FEATURE;
1230 USETW(req.wValue, sel);
1231 USETW(req.wIndex, 0);
1232 USETW(req.wLength, 0);
1233 return (usbd_do_request(udev, mtx, &req, 0));
1236 /*------------------------------------------------------------------------*
1237 * usbd_req_set_hub_feature
1242 *------------------------------------------------------------------------*/
1244 usbd_req_set_hub_feature(struct usb_device *udev, struct mtx *mtx,
1247 struct usb_device_request req;
1249 req.bmRequestType = UT_WRITE_CLASS_DEVICE;
1250 req.bRequest = UR_SET_FEATURE;
1251 USETW(req.wValue, sel);
1252 USETW(req.wIndex, 0);
1253 USETW(req.wLength, 0);
1254 return (usbd_do_request(udev, mtx, &req, 0));
1257 /*------------------------------------------------------------------------*
1258 * usbd_req_clear_port_feature
1263 *------------------------------------------------------------------------*/
1265 usbd_req_clear_port_feature(struct usb_device *udev, struct mtx *mtx,
1266 uint8_t port, uint16_t sel)
1268 struct usb_device_request req;
1270 req.bmRequestType = UT_WRITE_CLASS_OTHER;
1271 req.bRequest = UR_CLEAR_FEATURE;
1272 USETW(req.wValue, sel);
1273 req.wIndex[0] = port;
1275 USETW(req.wLength, 0);
1276 return (usbd_do_request(udev, mtx, &req, 0));
1279 /*------------------------------------------------------------------------*
1280 * usbd_req_set_port_feature
1285 *------------------------------------------------------------------------*/
1287 usbd_req_set_port_feature(struct usb_device *udev, struct mtx *mtx,
1288 uint8_t port, uint16_t sel)
1290 struct usb_device_request req;
1292 req.bmRequestType = UT_WRITE_CLASS_OTHER;
1293 req.bRequest = UR_SET_FEATURE;
1294 USETW(req.wValue, sel);
1295 req.wIndex[0] = port;
1297 USETW(req.wLength, 0);
1298 return (usbd_do_request(udev, mtx, &req, 0));
1301 /*------------------------------------------------------------------------*
1302 * usbd_req_set_protocol
1307 *------------------------------------------------------------------------*/
1309 usbd_req_set_protocol(struct usb_device *udev, struct mtx *mtx,
1310 uint8_t iface_index, uint16_t report)
1312 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1313 struct usb_device_request req;
1315 if ((iface == NULL) || (iface->idesc == NULL)) {
1316 return (USB_ERR_INVAL);
1318 DPRINTFN(5, "iface=%p, report=%d, endpt=%d\n",
1319 iface, report, iface->idesc->bInterfaceNumber);
1321 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1322 req.bRequest = UR_SET_PROTOCOL;
1323 USETW(req.wValue, report);
1324 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1326 USETW(req.wLength, 0);
1327 return (usbd_do_request(udev, mtx, &req, 0));
1330 /*------------------------------------------------------------------------*
1331 * usbd_req_set_report
1336 *------------------------------------------------------------------------*/
1338 usbd_req_set_report(struct usb_device *udev, struct mtx *mtx, void *data, uint16_t len,
1339 uint8_t iface_index, uint8_t type, uint8_t id)
1341 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1342 struct usb_device_request req;
1344 if ((iface == NULL) || (iface->idesc == NULL)) {
1345 return (USB_ERR_INVAL);
1347 DPRINTFN(5, "len=%d\n", len);
1349 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1350 req.bRequest = UR_SET_REPORT;
1351 USETW2(req.wValue, type, id);
1352 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1354 USETW(req.wLength, len);
1355 return (usbd_do_request(udev, mtx, &req, data));
1358 /*------------------------------------------------------------------------*
1359 * usbd_req_get_report
1364 *------------------------------------------------------------------------*/
1366 usbd_req_get_report(struct usb_device *udev, struct mtx *mtx, void *data,
1367 uint16_t len, uint8_t iface_index, uint8_t type, uint8_t id)
1369 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1370 struct usb_device_request req;
1372 if ((iface == NULL) || (iface->idesc == NULL) || (id == 0)) {
1373 return (USB_ERR_INVAL);
1375 DPRINTFN(5, "len=%d\n", len);
1377 req.bmRequestType = UT_READ_CLASS_INTERFACE;
1378 req.bRequest = UR_GET_REPORT;
1379 USETW2(req.wValue, type, id);
1380 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1382 USETW(req.wLength, len);
1383 return (usbd_do_request(udev, mtx, &req, data));
1386 /*------------------------------------------------------------------------*
1392 *------------------------------------------------------------------------*/
1394 usbd_req_set_idle(struct usb_device *udev, struct mtx *mtx,
1395 uint8_t iface_index, uint8_t duration, uint8_t id)
1397 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1398 struct usb_device_request req;
1400 if ((iface == NULL) || (iface->idesc == NULL)) {
1401 return (USB_ERR_INVAL);
1403 DPRINTFN(5, "%d %d\n", duration, id);
1405 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1406 req.bRequest = UR_SET_IDLE;
1407 USETW2(req.wValue, duration, id);
1408 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1410 USETW(req.wLength, 0);
1411 return (usbd_do_request(udev, mtx, &req, 0));
1414 /*------------------------------------------------------------------------*
1415 * usbd_req_get_report_descriptor
1420 *------------------------------------------------------------------------*/
1422 usbd_req_get_report_descriptor(struct usb_device *udev, struct mtx *mtx,
1423 void *d, uint16_t size, uint8_t iface_index)
1425 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1426 struct usb_device_request req;
1428 if ((iface == NULL) || (iface->idesc == NULL)) {
1429 return (USB_ERR_INVAL);
1431 req.bmRequestType = UT_READ_INTERFACE;
1432 req.bRequest = UR_GET_DESCRIPTOR;
1433 USETW2(req.wValue, UDESC_REPORT, 0); /* report id should be 0 */
1434 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1436 USETW(req.wLength, size);
1437 return (usbd_do_request(udev, mtx, &req, d));
1440 /*------------------------------------------------------------------------*
1441 * usbd_req_set_config
1443 * This function is used to select the current configuration number in
1444 * both USB device side mode and USB host side mode. When setting the
1445 * configuration the function of the interfaces can change.
1450 *------------------------------------------------------------------------*/
1452 usbd_req_set_config(struct usb_device *udev, struct mtx *mtx, uint8_t conf)
1454 struct usb_device_request req;
1456 DPRINTF("setting config %d\n", conf);
1458 /* do "set configuration" request */
1460 req.bmRequestType = UT_WRITE_DEVICE;
1461 req.bRequest = UR_SET_CONFIG;
1462 req.wValue[0] = conf;
1464 USETW(req.wIndex, 0);
1465 USETW(req.wLength, 0);
1466 return (usbd_do_request(udev, mtx, &req, 0));
1469 /*------------------------------------------------------------------------*
1470 * usbd_req_get_config
1475 *------------------------------------------------------------------------*/
1477 usbd_req_get_config(struct usb_device *udev, struct mtx *mtx, uint8_t *pconf)
1479 struct usb_device_request req;
1481 req.bmRequestType = UT_READ_DEVICE;
1482 req.bRequest = UR_GET_CONFIG;
1483 USETW(req.wValue, 0);
1484 USETW(req.wIndex, 0);
1485 USETW(req.wLength, 1);
1486 return (usbd_do_request(udev, mtx, &req, pconf));
1489 /*------------------------------------------------------------------------*
1490 * usbd_req_re_enumerate
1492 * NOTE: After this function returns the hardware is in the
1493 * unconfigured state! The application is responsible for setting a
1494 * new configuration.
1499 *------------------------------------------------------------------------*/
1501 usbd_req_re_enumerate(struct usb_device *udev, struct mtx *mtx)
1503 struct usb_device *parent_hub;
1506 uint8_t do_retry = 1;
1508 if (udev->flags.usb_mode != USB_MODE_HOST) {
1509 return (USB_ERR_INVAL);
1511 old_addr = udev->address;
1512 parent_hub = udev->parent_hub;
1513 if (parent_hub == NULL) {
1514 return (USB_ERR_INVAL);
1517 err = usbd_req_reset_port(parent_hub, mtx, udev->port_no);
1519 DPRINTFN(0, "addr=%d, port reset failed, %s\n",
1520 old_addr, usbd_errstr(err));
1524 * After that the port has been reset our device should be at
1527 udev->address = USB_START_ADDR;
1529 /* reset "bMaxPacketSize" */
1530 udev->ddesc.bMaxPacketSize = USB_MAX_IPACKET;
1533 * Restore device address:
1535 err = usbd_req_set_address(udev, mtx, old_addr);
1537 /* XXX ignore any errors! */
1538 DPRINTFN(0, "addr=%d, set address failed! (%s, ignored)\n",
1539 old_addr, usbd_errstr(err));
1541 /* restore device address */
1542 udev->address = old_addr;
1544 /* allow device time to set new address */
1545 usb_pause_mtx(mtx, USB_MS_TO_TICKS(USB_SET_ADDRESS_SETTLE));
1547 /* get the device descriptor */
1548 err = usbd_req_get_desc(udev, mtx, NULL, &udev->ddesc,
1549 USB_MAX_IPACKET, USB_MAX_IPACKET, 0, UDESC_DEVICE, 0, 0);
1551 DPRINTFN(0, "getting device descriptor "
1552 "at addr %d failed, %s!\n", udev->address,
1556 /* get the full device descriptor */
1557 err = usbd_req_get_device_desc(udev, mtx, &udev->ddesc);
1559 DPRINTFN(0, "addr=%d, getting device "
1560 "descriptor failed, %s!\n", old_addr,
1565 if (err && do_retry) {
1566 /* give the USB firmware some time to load */
1567 usb_pause_mtx(mtx, hz / 2);
1568 /* no more retries after this retry */
1573 /* restore address */
1574 udev->address = old_addr;
1578 /*------------------------------------------------------------------------*
1579 * usbd_req_clear_device_feature
1584 *------------------------------------------------------------------------*/
1586 usbd_req_clear_device_feature(struct usb_device *udev, struct mtx *mtx,
1589 struct usb_device_request req;
1591 req.bmRequestType = UT_WRITE_DEVICE;
1592 req.bRequest = UR_CLEAR_FEATURE;
1593 USETW(req.wValue, sel);
1594 USETW(req.wIndex, 0);
1595 USETW(req.wLength, 0);
1596 return (usbd_do_request(udev, mtx, &req, 0));
1599 /*------------------------------------------------------------------------*
1600 * usbd_req_set_device_feature
1605 *------------------------------------------------------------------------*/
1607 usbd_req_set_device_feature(struct usb_device *udev, struct mtx *mtx,
1610 struct usb_device_request req;
1612 req.bmRequestType = UT_WRITE_DEVICE;
1613 req.bRequest = UR_SET_FEATURE;
1614 USETW(req.wValue, sel);
1615 USETW(req.wIndex, 0);
1616 USETW(req.wLength, 0);
1617 return (usbd_do_request(udev, mtx, &req, 0));