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;
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");
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;
93 struct usb_ctrl_debug_bits {
101 /* The default is to disable fault injection. */
103 static struct usb_ctrl_debug usb_ctrl_debug = {
106 .bmRequestType_value = -1,
107 .bRequest_value = -1,
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");
127 /*------------------------------------------------------------------------*
128 * usbd_get_debug_bits
130 * This function is only useful in USB host mode.
131 *------------------------------------------------------------------------*/
133 usbd_get_debug_bits(struct usb_device *udev, struct usb_device_request *req,
134 struct usb_ctrl_debug_bits *dbg)
138 memset(dbg, 0, sizeof(*dbg));
140 /* Compute data stage delay */
142 temp = usb_ctrl_debug.ds_delay;
145 else if (temp > (16*1024))
148 dbg->ds_delay = temp;
150 /* Compute status stage delay */
152 temp = usb_ctrl_debug.ss_delay;
155 else if (temp > (16*1024))
158 dbg->ss_delay = temp;
160 /* Check if this control request should be failed */
162 if (usbd_get_bus_index(udev) != usb_ctrl_debug.bus_index)
165 if (usbd_get_device_index(udev) != usb_ctrl_debug.dev_index)
168 temp = usb_ctrl_debug.bmRequestType_value;
170 if ((temp != req->bmRequestType) && (temp >= 0) && (temp <= 255))
173 temp = usb_ctrl_debug.bRequest_value;
175 if ((temp != req->bRequest) && (temp >= 0) && (temp <= 255))
178 temp = usb_ctrl_debug.ds_fail;
182 temp = usb_ctrl_debug.ss_fail;
188 #endif /* USB_REQ_DEBUG */
189 #endif /* USB_DEBUG */
191 /*------------------------------------------------------------------------*
192 * usbd_do_request_callback
194 * This function is the USB callback for generic USB Host control
196 *------------------------------------------------------------------------*/
198 usbd_do_request_callback(struct usb_xfer *xfer, usb_error_t error)
200 ; /* workaround for a bug in "indent" */
202 DPRINTF("st=%u\n", USB_GET_STATE(xfer));
204 switch (USB_GET_STATE(xfer)) {
206 usbd_transfer_submit(xfer);
209 cv_signal(&xfer->xroot->udev->ctrlreq_cv);
214 /*------------------------------------------------------------------------*
215 * usb_do_clear_stall_callback
217 * This function is the USB callback for generic clear stall requests.
218 *------------------------------------------------------------------------*/
220 usb_do_clear_stall_callback(struct usb_xfer *xfer, usb_error_t error)
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;
229 udev = xfer->xroot->udev;
231 USB_BUS_LOCK(udev->bus);
233 /* round robin endpoint clear stall */
236 ep_end = udev->endpoints + udev->endpoints_max;
237 ep_first = udev->endpoints;
238 to = udev->endpoints_max;
240 switch (USB_GET_STATE(xfer)) {
241 case USB_ST_TRANSFERRED:
243 goto tr_setup; /* device was unconfigured */
248 /* start up the current or next transfer, if any */
249 usb_command_wrapper(&ep->endpoint_q,
250 ep->endpoint_q.curr);
257 break; /* no endpoints - nothing to do */
258 if ((ep < ep_first) || (ep >= ep_end))
259 ep = ep_first; /* endpoint wrapped around */
263 /* setup a clear-stall packet */
265 req.bmRequestType = UT_WRITE_ENDPOINT;
266 req.bRequest = UR_CLEAR_FEATURE;
267 USETW(req.wValue, UF_ENDPOINT_HALT);
268 req.wIndex[0] = ep->edesc->bEndpointAddress;
270 USETW(req.wLength, 0);
272 /* copy in the transfer */
274 usbd_copy_in(xfer->frbuffers, 0, &req, sizeof(req));
277 usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
279 USB_BUS_UNLOCK(udev->bus);
281 usbd_transfer_submit(xfer);
283 USB_BUS_LOCK(udev->bus);
291 if (xfer->error == USB_ERR_CANCELLED) {
297 /* store current endpoint */
299 USB_BUS_UNLOCK(udev->bus);
302 static usb_handle_req_t *
303 usbd_get_hr_func(struct usb_device *udev)
305 /* figure out if there is a Handle Request function */
306 if (udev->flags.usb_mode == USB_MODE_DEVICE)
307 return (usb_temp_get_desc_p);
308 else if (udev->parent_hub == NULL)
309 return (udev->bus->methods->roothub_exec);
314 /*------------------------------------------------------------------------*
315 * usbd_do_request_flags and usbd_do_request
317 * Description of arguments passed to these functions:
319 * "udev" - this is the "usb_device" structure pointer on which the
320 * request should be performed. It is possible to call this function
321 * in both Host Side mode and Device Side mode.
323 * "mtx" - if this argument is non-NULL the mutex pointed to by it
324 * will get dropped and picked up during the execution of this
325 * function, hence this function sometimes needs to sleep. If this
326 * argument is NULL it has no effect.
328 * "req" - this argument must always be non-NULL and points to an
329 * 8-byte structure holding the USB request to be done. The USB
330 * request structure has a bit telling the direction of the USB
331 * request, if it is a read or a write.
333 * "data" - if the "wLength" part of the structure pointed to by "req"
334 * is non-zero this argument must point to a valid kernel buffer which
335 * can hold at least "wLength" bytes. If "wLength" is zero "data" can
338 * "flags" - here is a list of valid flags:
340 * o USB_SHORT_XFER_OK: allows the data transfer to be shorter than
343 * o USB_DELAY_STATUS_STAGE: allows the status stage to be performed
344 * at a later point in time. This is tunable by the "hw.usb.ss_delay"
345 * sysctl. This flag is mostly useful for debugging.
347 * o USB_USER_DATA_PTR: treat the "data" pointer like a userland
350 * "actlen" - if non-NULL the actual transfer length will be stored in
351 * the 16-bit unsigned integer pointed to by "actlen". This
352 * information is mostly useful when the "USB_SHORT_XFER_OK" flag is
355 * "timeout" - gives the timeout for the control transfer in
356 * milliseconds. A "timeout" value less than 50 milliseconds is
357 * treated like a 50 millisecond timeout. A "timeout" value greater
358 * than 30 seconds is treated like a 30 second timeout. This USB stack
359 * does not allow control requests without a timeout.
361 * NOTE: This function is thread safe. All calls to
362 * "usbd_do_request_flags" will be serialised by the use of an
363 * internal "sx_lock".
368 *------------------------------------------------------------------------*/
370 usbd_do_request_flags(struct usb_device *udev, struct mtx *mtx,
371 struct usb_device_request *req, void *data, uint16_t flags,
372 uint16_t *actlen, usb_timeout_t timeout)
375 struct usb_ctrl_debug_bits dbg;
377 usb_handle_req_t *hr_func;
378 struct usb_xfer *xfer;
381 usb_ticks_t start_ticks;
382 usb_ticks_t delta_ticks;
383 usb_ticks_t max_ticks;
390 /* timeout is too small */
393 if (timeout > 30000) {
394 /* timeout is too big */
397 length = UGETW(req->wLength);
399 enum_locked = usbd_enum_is_locked(udev);
401 DPRINTFN(5, "udev=%p bmRequestType=0x%02x bRequest=0x%02x "
402 "wValue=0x%02x%02x wIndex=0x%02x%02x wLength=0x%02x%02x\n",
403 udev, req->bmRequestType, req->bRequest,
404 req->wValue[1], req->wValue[0],
405 req->wIndex[1], req->wIndex[0],
406 req->wLength[1], req->wLength[0]);
408 /* Check if the device is still alive */
409 if (udev->state < USB_STATE_POWERED) {
410 DPRINTF("usb device has gone\n");
411 return (USB_ERR_NOT_CONFIGURED);
415 * Set "actlen" to a known value in case the caller does not
416 * check the return value:
421 #if (USB_HAVE_USER_IO == 0)
422 if (flags & USB_USER_DATA_PTR)
423 return (USB_ERR_INVAL);
425 if ((mtx != NULL) && (mtx != &Giant)) {
427 mtx_assert(mtx, MA_NOTOWNED);
431 * We need to allow suspend and resume at this point, else the
432 * control transfer will timeout if the device is suspended!
435 usbd_sr_unlock(udev);
438 * Grab the default sx-lock so that serialisation
439 * is achieved when multiple threads are involved:
441 sx_xlock(&udev->ctrl_sx);
443 hr_func = usbd_get_hr_func(udev);
445 if (hr_func != NULL) {
446 DPRINTF("Handle Request function is set\n");
451 if (!(req->bmRequestType & UT_READ)) {
453 DPRINTFN(1, "The handle request function "
454 "does not support writing data!\n");
460 /* The root HUB code needs the BUS lock locked */
462 USB_BUS_LOCK(udev->bus);
463 err = (hr_func) (udev, req, &desc, &temp);
464 USB_BUS_UNLOCK(udev->bus);
470 if (!(flags & USB_SHORT_XFER_OK)) {
471 err = USB_ERR_SHORT_XFER;
481 if (flags & USB_USER_DATA_PTR) {
482 if (copyout(desc, data, length)) {
488 bcopy(desc, data, length);
490 goto done; /* success */
494 * Setup a new USB transfer or use the existing one, if any:
496 usbd_ctrl_transfer_setup(udev);
498 xfer = udev->ctrl_xfer[0];
500 /* most likely out of memory */
507 usbd_get_debug_bits(udev, req, &dbg);
509 /* Check for fault injection */
511 flags |= USB_DELAY_STATUS_STAGE;
515 if (flags & USB_DELAY_STATUS_STAGE)
516 xfer->flags.manual_status = 1;
518 xfer->flags.manual_status = 0;
520 if (flags & USB_SHORT_XFER_OK)
521 xfer->flags.short_xfer_ok = 1;
523 xfer->flags.short_xfer_ok = 0;
525 xfer->timeout = timeout;
529 max_ticks = USB_MS_TO_TICKS(timeout);
531 usbd_copy_in(xfer->frbuffers, 0, req, sizeof(*req));
533 usbd_xfer_set_frame_len(xfer, 0, sizeof(*req));
537 if (temp > usbd_xfer_max_len(xfer)) {
538 temp = usbd_xfer_max_len(xfer);
541 if (xfer->flags.manual_status) {
542 if (usbd_xfer_frame_len(xfer, 0) != 0) {
543 /* Execute data stage separately */
545 } else if (temp > 0) {
550 if (dbg.ds_delay > 0) {
552 xfer->xroot->xfer_mtx,
553 USB_MS_TO_TICKS(dbg.ds_delay));
554 /* make sure we don't time out */
560 usbd_xfer_set_frame_len(xfer, 1, temp);
563 if (!(req->bmRequestType & UT_READ)) {
565 if (flags & USB_USER_DATA_PTR) {
566 USB_XFER_UNLOCK(xfer);
567 err = usbd_copy_in_user(xfer->frbuffers + 1,
576 usbd_copy_in(xfer->frbuffers + 1,
579 usbd_xfer_set_frames(xfer, 2);
581 if (usbd_xfer_frame_len(xfer, 0) == 0) {
582 if (xfer->flags.manual_status) {
588 if (dbg.ss_delay > 0) {
590 xfer->xroot->xfer_mtx,
591 USB_MS_TO_TICKS(dbg.ss_delay));
592 /* make sure we don't time out */
596 xfer->flags.manual_status = 0;
601 usbd_xfer_set_frames(xfer, 1);
604 usbd_transfer_start(xfer);
606 while (usbd_transfer_pending(xfer)) {
607 cv_wait(&udev->ctrlreq_cv,
608 xfer->xroot->xfer_mtx);
617 /* get actual length of DATA stage */
619 if (xfer->aframes < 2) {
622 acttemp = usbd_xfer_frame_len(xfer, 1);
625 /* check for short packet */
627 if (temp > acttemp) {
632 if (req->bmRequestType & UT_READ) {
634 if (flags & USB_USER_DATA_PTR) {
635 USB_XFER_UNLOCK(xfer);
636 err = usbd_copy_out_user(xfer->frbuffers + 1,
645 usbd_copy_out(xfer->frbuffers + 1,
650 * Clear "frlengths[0]" so that we don't send the setup
653 usbd_xfer_set_frame_len(xfer, 0, 0);
655 /* update length and data pointer */
657 data = USB_ADD_BYTES(data, temp);
662 /* check for timeout */
664 delta_ticks = ticks - start_ticks;
665 if (delta_ticks > max_ticks) {
667 err = USB_ERR_TIMEOUT;
677 * Make sure that the control endpoint is no longer
678 * blocked in case of a non-transfer related error:
680 usbd_transfer_stop(xfer);
682 USB_XFER_UNLOCK(xfer);
685 sx_xunlock(&udev->ctrl_sx);
690 if ((mtx != NULL) && (mtx != &Giant))
693 return ((usb_error_t)err);
696 /*------------------------------------------------------------------------*
697 * usbd_do_request_proc - factored out code
699 * This function is factored out code. It does basically the same like
700 * usbd_do_request_flags, except it will check the status of the
701 * passed process argument before doing the USB request. If the
702 * process is draining the USB_ERR_IOERROR code will be returned. It
703 * is assumed that the mutex associated with the process is locked
704 * when calling this function.
705 *------------------------------------------------------------------------*/
707 usbd_do_request_proc(struct usb_device *udev, struct usb_process *pproc,
708 struct usb_device_request *req, void *data, uint16_t flags,
709 uint16_t *actlen, usb_timeout_t timeout)
714 /* get request data length */
715 len = UGETW(req->wLength);
717 /* check if the device is being detached */
718 if (usb_proc_is_gone(pproc)) {
719 err = USB_ERR_IOERROR;
723 /* forward the USB request */
724 err = usbd_do_request_flags(udev, pproc->up_mtx,
725 req, data, flags, actlen, timeout);
728 /* on failure we zero the data */
729 /* on short packet we zero the unused data */
730 if ((len != 0) && (req->bmRequestType & UE_DIR_IN)) {
732 memset(data, 0, len);
733 else if (actlen && *actlen != len)
734 memset(((uint8_t *)data) + *actlen, 0, len - *actlen);
739 /*------------------------------------------------------------------------*
740 * usbd_req_reset_port
742 * This function will instruct an USB HUB to perform a reset sequence
743 * on the specified port number.
746 * 0: Success. The USB device should now be at address zero.
747 * Else: Failure. No USB device is present and the USB port should be
749 *------------------------------------------------------------------------*/
751 usbd_req_reset_port(struct usb_device *udev, struct mtx *mtx, uint8_t port)
753 struct usb_port_status ps;
758 uint16_t pr_poll_delay;
759 uint16_t pr_recovery_delay;
762 err = usbd_req_set_port_feature(udev, mtx, port, UHF_PORT_RESET);
767 /* range check input parameters */
768 pr_poll_delay = usb_pr_poll_delay;
769 if (pr_poll_delay < 1) {
771 } else if (pr_poll_delay > 1000) {
772 pr_poll_delay = 1000;
774 pr_recovery_delay = usb_pr_recovery_delay;
775 if (pr_recovery_delay > 1000) {
776 pr_recovery_delay = 1000;
782 /* wait for the device to recover from reset */
783 usb_pause_mtx(mtx, USB_MS_TO_TICKS(pr_poll_delay));
786 /* wait for the device to recover from reset */
787 usb_pause_mtx(mtx, USB_MS_TO_TICKS(USB_PORT_RESET_DELAY));
788 n += USB_PORT_RESET_DELAY;
790 err = usbd_req_get_port_status(udev, mtx, &ps, port);
794 /* if the device disappeared, just give up */
795 if (!(UGETW(ps.wPortStatus) & UPS_CURRENT_CONNECT_STATUS)) {
798 /* check if reset is complete */
799 if (UGETW(ps.wPortChange) & UPS_C_PORT_RESET) {
802 /* check for timeout */
809 /* clear port reset first */
810 err = usbd_req_clear_port_feature(
811 udev, mtx, port, UHF_C_PORT_RESET);
815 /* check for timeout */
817 err = USB_ERR_TIMEOUT;
821 /* wait for the device to recover from reset */
822 usb_pause_mtx(mtx, USB_MS_TO_TICKS(pr_recovery_delay));
824 /* wait for the device to recover from reset */
825 usb_pause_mtx(mtx, USB_MS_TO_TICKS(USB_PORT_RESET_RECOVERY));
829 DPRINTFN(2, "port %d reset returning error=%s\n",
830 port, usbd_errstr(err));
834 /*------------------------------------------------------------------------*
837 * This function can be used to retrieve USB descriptors. It contains
838 * some additional logic like zeroing of missing descriptor bytes and
839 * retrying an USB descriptor in case of failure. The "min_len"
840 * argument specifies the minimum descriptor length. The "max_len"
841 * argument specifies the maximum descriptor length. If the real
842 * descriptor length is less than the minimum length the missing
843 * byte(s) will be zeroed. The type field, the second byte of the USB
844 * descriptor, will get forced to the correct type. If the "actlen"
845 * pointer is non-NULL, the actual length of the transfer will get
846 * stored in the 16-bit unsigned integer which it is pointing to. The
847 * first byte of the descriptor will not get updated. If the "actlen"
848 * pointer is NULL the first byte of the descriptor will get updated
849 * to reflect the actual length instead. If "min_len" is not equal to
850 * "max_len" then this function will try to retrive the beginning of
851 * the descriptor and base the maximum length on the first byte of the
857 *------------------------------------------------------------------------*/
859 usbd_req_get_desc(struct usb_device *udev,
860 struct mtx *mtx, uint16_t *actlen, void *desc,
861 uint16_t min_len, uint16_t max_len,
862 uint16_t id, uint8_t type, uint8_t index,
865 struct usb_device_request req;
869 DPRINTFN(4, "id=%d, type=%d, index=%d, max_len=%d\n",
870 id, type, index, max_len);
872 req.bmRequestType = UT_READ_DEVICE;
873 req.bRequest = UR_GET_DESCRIPTOR;
874 USETW2(req.wValue, type, index);
875 USETW(req.wIndex, id);
879 if ((min_len < 2) || (max_len < 2)) {
883 USETW(req.wLength, min_len);
885 err = usbd_do_request_flags(udev, mtx, &req,
886 desc, 0, NULL, 1000);
894 usb_pause_mtx(mtx, hz / 5);
900 if (min_len == max_len) {
902 /* enforce correct length */
903 if ((buf[0] > min_len) && (actlen == NULL))
906 /* enforce correct type */
913 if (max_len > buf[0]) {
916 /* zero minimum data */
918 while (min_len > max_len) {
923 /* set new minimum length */
928 if (actlen != NULL) {
937 /*------------------------------------------------------------------------*
938 * usbd_req_get_string_any
940 * This function will return the string given by "string_index"
941 * using the first language ID. The maximum length "len" includes
942 * the terminating zero. The "len" argument should be twice as
943 * big pluss 2 bytes, compared with the actual maximum string length !
948 *------------------------------------------------------------------------*/
950 usbd_req_get_string_any(struct usb_device *udev, struct mtx *mtx, char *buf,
951 uint16_t len, uint8_t string_index)
962 /* should not happen */
963 return (USB_ERR_NORMAL_COMPLETION);
965 if (string_index == 0) {
966 /* this is the language table */
968 return (USB_ERR_INVAL);
970 if (udev->flags.no_strings) {
972 return (USB_ERR_STALLED);
974 err = usbd_req_get_string_desc
975 (udev, mtx, buf, len, udev->langid, string_index);
980 temp = (uint8_t *)buf;
983 /* string length is too short */
985 return (USB_ERR_INVAL);
987 /* reserve one byte for terminating zero */
990 /* find maximum length */
992 n = (temp[0] / 2) - 1;
996 /* skip descriptor header */
999 /* reset swap state */
1002 /* convert and filter */
1003 for (i = 0; (i != n); i++) {
1004 c = UGETW(temp + (2 * i));
1006 /* convert from Unicode, handle buggy strings */
1007 if (((c & 0xff00) == 0) && (swap & 1)) {
1008 /* Little Endian, default */
1011 } else if (((c & 0x00ff) == 0) && (swap & 2)) {
1016 /* silently skip bad character */
1021 * Filter by default - we don't allow greater and less than
1022 * signs because they might confuse the dmesg printouts!
1024 if ((*s == '<') || (*s == '>') || (!isprint(*s))) {
1025 /* silently skip bad character */
1030 *s = 0; /* zero terminate resulting string */
1031 return (USB_ERR_NORMAL_COMPLETION);
1034 /*------------------------------------------------------------------------*
1035 * usbd_req_get_string_desc
1037 * If you don't know the language ID, consider using
1038 * "usbd_req_get_string_any()".
1043 *------------------------------------------------------------------------*/
1045 usbd_req_get_string_desc(struct usb_device *udev, struct mtx *mtx, void *sdesc,
1046 uint16_t max_len, uint16_t lang_id,
1047 uint8_t string_index)
1049 return (usbd_req_get_desc(udev, mtx, NULL, sdesc, 2, max_len, lang_id,
1050 UDESC_STRING, string_index, 0));
1053 /*------------------------------------------------------------------------*
1054 * usbd_req_get_config_desc_ptr
1056 * This function is used in device side mode to retrieve the pointer
1057 * to the generated config descriptor. This saves allocating space for
1058 * an additional config descriptor when setting the configuration.
1063 *------------------------------------------------------------------------*/
1065 usbd_req_get_descriptor_ptr(struct usb_device *udev,
1066 struct usb_config_descriptor **ppcd, uint16_t wValue)
1068 struct usb_device_request req;
1069 usb_handle_req_t *hr_func;
1074 req.bmRequestType = UT_READ_DEVICE;
1075 req.bRequest = UR_GET_DESCRIPTOR;
1076 USETW(req.wValue, wValue);
1077 USETW(req.wIndex, 0);
1078 USETW(req.wLength, 0);
1083 hr_func = usbd_get_hr_func(udev);
1085 if (hr_func == NULL)
1086 err = USB_ERR_INVAL;
1088 USB_BUS_LOCK(udev->bus);
1089 err = (hr_func) (udev, &req, &ptr, &len);
1090 USB_BUS_UNLOCK(udev->bus);
1095 else if (ptr == NULL)
1096 err = USB_ERR_INVAL;
1098 *ppcd = __DECONST(struct usb_config_descriptor *, ptr);
1103 /*------------------------------------------------------------------------*
1104 * usbd_req_get_config_desc
1109 *------------------------------------------------------------------------*/
1111 usbd_req_get_config_desc(struct usb_device *udev, struct mtx *mtx,
1112 struct usb_config_descriptor *d, uint8_t conf_index)
1116 DPRINTFN(4, "confidx=%d\n", conf_index);
1118 err = usbd_req_get_desc(udev, mtx, NULL, d, sizeof(*d),
1119 sizeof(*d), 0, UDESC_CONFIG, conf_index, 0);
1123 /* Extra sanity checking */
1124 if (UGETW(d->wTotalLength) < sizeof(*d)) {
1125 err = USB_ERR_INVAL;
1131 /*------------------------------------------------------------------------*
1132 * usbd_req_get_config_desc_full
1134 * This function gets the complete USB configuration descriptor and
1135 * ensures that "wTotalLength" is correct.
1140 *------------------------------------------------------------------------*/
1142 usbd_req_get_config_desc_full(struct usb_device *udev, struct mtx *mtx,
1143 struct usb_config_descriptor **ppcd, struct malloc_type *mtype,
1146 struct usb_config_descriptor cd;
1147 struct usb_config_descriptor *cdesc;
1151 DPRINTFN(4, "index=%d\n", index);
1155 err = usbd_req_get_config_desc(udev, mtx, &cd, index);
1159 /* get full descriptor */
1160 len = UGETW(cd.wTotalLength);
1161 if (len < sizeof(*cdesc)) {
1162 /* corrupt descriptor */
1163 return (USB_ERR_INVAL);
1165 cdesc = malloc(len, mtype, M_WAITOK);
1166 if (cdesc == NULL) {
1167 return (USB_ERR_NOMEM);
1169 err = usbd_req_get_desc(udev, mtx, NULL, cdesc, len, len, 0,
1170 UDESC_CONFIG, index, 3);
1175 /* make sure that the device is not fooling us: */
1176 USETW(cdesc->wTotalLength, len);
1180 return (0); /* success */
1183 /*------------------------------------------------------------------------*
1184 * usbd_req_get_device_desc
1189 *------------------------------------------------------------------------*/
1191 usbd_req_get_device_desc(struct usb_device *udev, struct mtx *mtx,
1192 struct usb_device_descriptor *d)
1195 return (usbd_req_get_desc(udev, mtx, NULL, d, sizeof(*d),
1196 sizeof(*d), 0, UDESC_DEVICE, 0, 3));
1199 /*------------------------------------------------------------------------*
1200 * usbd_req_get_alt_interface_no
1205 *------------------------------------------------------------------------*/
1207 usbd_req_get_alt_interface_no(struct usb_device *udev, struct mtx *mtx,
1208 uint8_t *alt_iface_no, uint8_t iface_index)
1210 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1211 struct usb_device_request req;
1213 if ((iface == NULL) || (iface->idesc == NULL))
1214 return (USB_ERR_INVAL);
1216 req.bmRequestType = UT_READ_INTERFACE;
1217 req.bRequest = UR_GET_INTERFACE;
1218 USETW(req.wValue, 0);
1219 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1221 USETW(req.wLength, 1);
1222 return (usbd_do_request(udev, mtx, &req, alt_iface_no));
1225 /*------------------------------------------------------------------------*
1226 * usbd_req_set_alt_interface_no
1231 *------------------------------------------------------------------------*/
1233 usbd_req_set_alt_interface_no(struct usb_device *udev, struct mtx *mtx,
1234 uint8_t iface_index, uint8_t alt_no)
1236 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1237 struct usb_device_request req;
1239 if ((iface == NULL) || (iface->idesc == NULL))
1240 return (USB_ERR_INVAL);
1242 req.bmRequestType = UT_WRITE_INTERFACE;
1243 req.bRequest = UR_SET_INTERFACE;
1244 req.wValue[0] = alt_no;
1246 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1248 USETW(req.wLength, 0);
1249 return (usbd_do_request(udev, mtx, &req, 0));
1252 /*------------------------------------------------------------------------*
1253 * usbd_req_get_device_status
1258 *------------------------------------------------------------------------*/
1260 usbd_req_get_device_status(struct usb_device *udev, struct mtx *mtx,
1261 struct usb_status *st)
1263 struct usb_device_request req;
1265 req.bmRequestType = UT_READ_DEVICE;
1266 req.bRequest = UR_GET_STATUS;
1267 USETW(req.wValue, 0);
1268 USETW(req.wIndex, 0);
1269 USETW(req.wLength, sizeof(*st));
1270 return (usbd_do_request(udev, mtx, &req, st));
1273 /*------------------------------------------------------------------------*
1274 * usbd_req_get_hub_descriptor
1279 *------------------------------------------------------------------------*/
1281 usbd_req_get_hub_descriptor(struct usb_device *udev, struct mtx *mtx,
1282 struct usb_hub_descriptor *hd, uint8_t nports)
1284 struct usb_device_request req;
1285 uint16_t len = (nports + 7 + (8 * 8)) / 8;
1287 req.bmRequestType = UT_READ_CLASS_DEVICE;
1288 req.bRequest = UR_GET_DESCRIPTOR;
1289 USETW2(req.wValue, UDESC_HUB, 0);
1290 USETW(req.wIndex, 0);
1291 USETW(req.wLength, len);
1292 return (usbd_do_request(udev, mtx, &req, hd));
1295 /*------------------------------------------------------------------------*
1296 * usbd_req_get_hub_status
1301 *------------------------------------------------------------------------*/
1303 usbd_req_get_hub_status(struct usb_device *udev, struct mtx *mtx,
1304 struct usb_hub_status *st)
1306 struct usb_device_request req;
1308 req.bmRequestType = UT_READ_CLASS_DEVICE;
1309 req.bRequest = UR_GET_STATUS;
1310 USETW(req.wValue, 0);
1311 USETW(req.wIndex, 0);
1312 USETW(req.wLength, sizeof(struct usb_hub_status));
1313 return (usbd_do_request(udev, mtx, &req, st));
1316 /*------------------------------------------------------------------------*
1317 * usbd_req_set_address
1319 * This function is used to set the address for an USB device. After
1320 * port reset the USB device will respond at address zero.
1325 *------------------------------------------------------------------------*/
1327 usbd_req_set_address(struct usb_device *udev, struct mtx *mtx, uint16_t addr)
1329 struct usb_device_request req;
1331 DPRINTFN(6, "setting device address=%d\n", addr);
1333 req.bmRequestType = UT_WRITE_DEVICE;
1334 req.bRequest = UR_SET_ADDRESS;
1335 USETW(req.wValue, addr);
1336 USETW(req.wIndex, 0);
1337 USETW(req.wLength, 0);
1339 /* Setting the address should not take more than 1 second ! */
1340 return (usbd_do_request_flags(udev, mtx, &req, NULL,
1341 USB_DELAY_STATUS_STAGE, NULL, 1000));
1344 /*------------------------------------------------------------------------*
1345 * usbd_req_get_port_status
1350 *------------------------------------------------------------------------*/
1352 usbd_req_get_port_status(struct usb_device *udev, struct mtx *mtx,
1353 struct usb_port_status *ps, uint8_t port)
1355 struct usb_device_request req;
1357 req.bmRequestType = UT_READ_CLASS_OTHER;
1358 req.bRequest = UR_GET_STATUS;
1359 USETW(req.wValue, 0);
1360 req.wIndex[0] = port;
1362 USETW(req.wLength, sizeof *ps);
1363 return (usbd_do_request(udev, mtx, &req, ps));
1366 /*------------------------------------------------------------------------*
1367 * usbd_req_clear_hub_feature
1372 *------------------------------------------------------------------------*/
1374 usbd_req_clear_hub_feature(struct usb_device *udev, struct mtx *mtx,
1377 struct usb_device_request req;
1379 req.bmRequestType = UT_WRITE_CLASS_DEVICE;
1380 req.bRequest = UR_CLEAR_FEATURE;
1381 USETW(req.wValue, sel);
1382 USETW(req.wIndex, 0);
1383 USETW(req.wLength, 0);
1384 return (usbd_do_request(udev, mtx, &req, 0));
1387 /*------------------------------------------------------------------------*
1388 * usbd_req_set_hub_feature
1393 *------------------------------------------------------------------------*/
1395 usbd_req_set_hub_feature(struct usb_device *udev, struct mtx *mtx,
1398 struct usb_device_request req;
1400 req.bmRequestType = UT_WRITE_CLASS_DEVICE;
1401 req.bRequest = UR_SET_FEATURE;
1402 USETW(req.wValue, sel);
1403 USETW(req.wIndex, 0);
1404 USETW(req.wLength, 0);
1405 return (usbd_do_request(udev, mtx, &req, 0));
1408 /*------------------------------------------------------------------------*
1409 * usbd_req_clear_port_feature
1414 *------------------------------------------------------------------------*/
1416 usbd_req_clear_port_feature(struct usb_device *udev, struct mtx *mtx,
1417 uint8_t port, uint16_t sel)
1419 struct usb_device_request req;
1421 req.bmRequestType = UT_WRITE_CLASS_OTHER;
1422 req.bRequest = UR_CLEAR_FEATURE;
1423 USETW(req.wValue, sel);
1424 req.wIndex[0] = port;
1426 USETW(req.wLength, 0);
1427 return (usbd_do_request(udev, mtx, &req, 0));
1430 /*------------------------------------------------------------------------*
1431 * usbd_req_set_port_feature
1436 *------------------------------------------------------------------------*/
1438 usbd_req_set_port_feature(struct usb_device *udev, struct mtx *mtx,
1439 uint8_t port, uint16_t sel)
1441 struct usb_device_request req;
1443 req.bmRequestType = UT_WRITE_CLASS_OTHER;
1444 req.bRequest = UR_SET_FEATURE;
1445 USETW(req.wValue, sel);
1446 req.wIndex[0] = port;
1448 USETW(req.wLength, 0);
1449 return (usbd_do_request(udev, mtx, &req, 0));
1452 /*------------------------------------------------------------------------*
1453 * usbd_req_set_protocol
1458 *------------------------------------------------------------------------*/
1460 usbd_req_set_protocol(struct usb_device *udev, struct mtx *mtx,
1461 uint8_t iface_index, uint16_t report)
1463 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1464 struct usb_device_request req;
1466 if ((iface == NULL) || (iface->idesc == NULL)) {
1467 return (USB_ERR_INVAL);
1469 DPRINTFN(5, "iface=%p, report=%d, endpt=%d\n",
1470 iface, report, iface->idesc->bInterfaceNumber);
1472 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1473 req.bRequest = UR_SET_PROTOCOL;
1474 USETW(req.wValue, report);
1475 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1477 USETW(req.wLength, 0);
1478 return (usbd_do_request(udev, mtx, &req, 0));
1481 /*------------------------------------------------------------------------*
1482 * usbd_req_set_report
1487 *------------------------------------------------------------------------*/
1489 usbd_req_set_report(struct usb_device *udev, struct mtx *mtx, void *data, uint16_t len,
1490 uint8_t iface_index, uint8_t type, uint8_t id)
1492 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1493 struct usb_device_request req;
1495 if ((iface == NULL) || (iface->idesc == NULL)) {
1496 return (USB_ERR_INVAL);
1498 DPRINTFN(5, "len=%d\n", len);
1500 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1501 req.bRequest = UR_SET_REPORT;
1502 USETW2(req.wValue, type, id);
1503 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1505 USETW(req.wLength, len);
1506 return (usbd_do_request(udev, mtx, &req, data));
1509 /*------------------------------------------------------------------------*
1510 * usbd_req_get_report
1515 *------------------------------------------------------------------------*/
1517 usbd_req_get_report(struct usb_device *udev, struct mtx *mtx, void *data,
1518 uint16_t len, uint8_t iface_index, uint8_t type, uint8_t id)
1520 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1521 struct usb_device_request req;
1523 if ((iface == NULL) || (iface->idesc == NULL) || (id == 0)) {
1524 return (USB_ERR_INVAL);
1526 DPRINTFN(5, "len=%d\n", len);
1528 req.bmRequestType = UT_READ_CLASS_INTERFACE;
1529 req.bRequest = UR_GET_REPORT;
1530 USETW2(req.wValue, type, id);
1531 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1533 USETW(req.wLength, len);
1534 return (usbd_do_request(udev, mtx, &req, data));
1537 /*------------------------------------------------------------------------*
1543 *------------------------------------------------------------------------*/
1545 usbd_req_set_idle(struct usb_device *udev, struct mtx *mtx,
1546 uint8_t iface_index, uint8_t duration, uint8_t id)
1548 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1549 struct usb_device_request req;
1551 if ((iface == NULL) || (iface->idesc == NULL)) {
1552 return (USB_ERR_INVAL);
1554 DPRINTFN(5, "%d %d\n", duration, id);
1556 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1557 req.bRequest = UR_SET_IDLE;
1558 USETW2(req.wValue, duration, id);
1559 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1561 USETW(req.wLength, 0);
1562 return (usbd_do_request(udev, mtx, &req, 0));
1565 /*------------------------------------------------------------------------*
1566 * usbd_req_get_report_descriptor
1571 *------------------------------------------------------------------------*/
1573 usbd_req_get_report_descriptor(struct usb_device *udev, struct mtx *mtx,
1574 void *d, uint16_t size, uint8_t iface_index)
1576 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1577 struct usb_device_request req;
1579 if ((iface == NULL) || (iface->idesc == NULL)) {
1580 return (USB_ERR_INVAL);
1582 req.bmRequestType = UT_READ_INTERFACE;
1583 req.bRequest = UR_GET_DESCRIPTOR;
1584 USETW2(req.wValue, UDESC_REPORT, 0); /* report id should be 0 */
1585 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1587 USETW(req.wLength, size);
1588 return (usbd_do_request(udev, mtx, &req, d));
1591 /*------------------------------------------------------------------------*
1592 * usbd_req_set_config
1594 * This function is used to select the current configuration number in
1595 * both USB device side mode and USB host side mode. When setting the
1596 * configuration the function of the interfaces can change.
1601 *------------------------------------------------------------------------*/
1603 usbd_req_set_config(struct usb_device *udev, struct mtx *mtx, uint8_t conf)
1605 struct usb_device_request req;
1607 DPRINTF("setting config %d\n", conf);
1609 /* do "set configuration" request */
1611 req.bmRequestType = UT_WRITE_DEVICE;
1612 req.bRequest = UR_SET_CONFIG;
1613 req.wValue[0] = conf;
1615 USETW(req.wIndex, 0);
1616 USETW(req.wLength, 0);
1617 return (usbd_do_request(udev, mtx, &req, 0));
1620 /*------------------------------------------------------------------------*
1621 * usbd_req_get_config
1626 *------------------------------------------------------------------------*/
1628 usbd_req_get_config(struct usb_device *udev, struct mtx *mtx, uint8_t *pconf)
1630 struct usb_device_request req;
1632 req.bmRequestType = UT_READ_DEVICE;
1633 req.bRequest = UR_GET_CONFIG;
1634 USETW(req.wValue, 0);
1635 USETW(req.wIndex, 0);
1636 USETW(req.wLength, 1);
1637 return (usbd_do_request(udev, mtx, &req, pconf));
1640 /*------------------------------------------------------------------------*
1641 * usbd_req_re_enumerate
1643 * NOTE: After this function returns the hardware is in the
1644 * unconfigured state! The application is responsible for setting a
1645 * new configuration.
1650 *------------------------------------------------------------------------*/
1652 usbd_req_re_enumerate(struct usb_device *udev, struct mtx *mtx)
1654 struct usb_device *parent_hub;
1657 uint8_t do_retry = 1;
1659 if (udev->flags.usb_mode != USB_MODE_HOST) {
1660 return (USB_ERR_INVAL);
1662 old_addr = udev->address;
1663 parent_hub = udev->parent_hub;
1664 if (parent_hub == NULL) {
1665 return (USB_ERR_INVAL);
1668 err = usbd_req_reset_port(parent_hub, mtx, udev->port_no);
1670 DPRINTFN(0, "addr=%d, port reset failed, %s\n",
1671 old_addr, usbd_errstr(err));
1675 * After that the port has been reset our device should be at
1678 udev->address = USB_START_ADDR;
1680 /* reset "bMaxPacketSize" */
1681 udev->ddesc.bMaxPacketSize = USB_MAX_IPACKET;
1684 * Restore device address:
1686 err = usbd_req_set_address(udev, mtx, old_addr);
1688 /* XXX ignore any errors! */
1689 DPRINTFN(0, "addr=%d, set address failed! (%s, ignored)\n",
1690 old_addr, usbd_errstr(err));
1692 /* restore device address */
1693 udev->address = old_addr;
1695 /* allow device time to set new address */
1696 usb_pause_mtx(mtx, USB_MS_TO_TICKS(USB_SET_ADDRESS_SETTLE));
1698 /* get the device descriptor */
1699 err = usbd_req_get_desc(udev, mtx, NULL, &udev->ddesc,
1700 USB_MAX_IPACKET, USB_MAX_IPACKET, 0, UDESC_DEVICE, 0, 0);
1702 DPRINTFN(0, "getting device descriptor "
1703 "at addr %d failed, %s\n", udev->address,
1707 /* get the full device descriptor */
1708 err = usbd_req_get_device_desc(udev, mtx, &udev->ddesc);
1710 DPRINTFN(0, "addr=%d, getting device "
1711 "descriptor failed, %s\n", old_addr,
1716 if (err && do_retry) {
1717 /* give the USB firmware some time to load */
1718 usb_pause_mtx(mtx, hz / 2);
1719 /* no more retries after this retry */
1724 /* restore address */
1725 udev->address = old_addr;
1729 /*------------------------------------------------------------------------*
1730 * usbd_req_clear_device_feature
1735 *------------------------------------------------------------------------*/
1737 usbd_req_clear_device_feature(struct usb_device *udev, struct mtx *mtx,
1740 struct usb_device_request req;
1742 req.bmRequestType = UT_WRITE_DEVICE;
1743 req.bRequest = UR_CLEAR_FEATURE;
1744 USETW(req.wValue, sel);
1745 USETW(req.wIndex, 0);
1746 USETW(req.wLength, 0);
1747 return (usbd_do_request(udev, mtx, &req, 0));
1750 /*------------------------------------------------------------------------*
1751 * usbd_req_set_device_feature
1756 *------------------------------------------------------------------------*/
1758 usbd_req_set_device_feature(struct usb_device *udev, struct mtx *mtx,
1761 struct usb_device_request req;
1763 req.bmRequestType = UT_WRITE_DEVICE;
1764 req.bRequest = UR_SET_FEATURE;
1765 USETW(req.wValue, sel);
1766 USETW(req.wIndex, 0);
1767 USETW(req.wLength, 0);
1768 return (usbd_do_request(udev, mtx, &req, 0));