3 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 #ifdef USB_GLOBAL_INCLUDE_FILE
30 #include USB_GLOBAL_INCLUDE_FILE
32 #include <sys/stdint.h>
33 #include <sys/stddef.h>
34 #include <sys/param.h>
35 #include <sys/queue.h>
36 #include <sys/types.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
40 #include <sys/module.h>
42 #include <sys/mutex.h>
43 #include <sys/condvar.h>
44 #include <sys/sysctl.h>
46 #include <sys/unistd.h>
47 #include <sys/callout.h>
48 #include <sys/malloc.h>
51 #include <dev/usb/usb.h>
52 #include <dev/usb/usbdi.h>
53 #include <dev/usb/usbdi_util.h>
56 #define USB_DEBUG_VAR usb_debug
58 #include <dev/usb/usb_core.h>
59 #include <dev/usb/usb_process.h>
60 #include <dev/usb/usb_busdma.h>
61 #include <dev/usb/usb_transfer.h>
62 #include <dev/usb/usb_device.h>
63 #include <dev/usb/usb_debug.h>
64 #include <dev/usb/usb_dynamic.h>
65 #include <dev/usb/usb_hub.h>
67 #include <dev/usb/usb_controller.h>
68 #include <dev/usb/usb_bus.h>
69 #endif /* USB_GLOBAL_INCLUDE_FILE */
71 /* function prototypes */
73 static uint8_t usb_handle_get_stall(struct usb_device *, uint8_t);
74 static usb_error_t usb_handle_remote_wakeup(struct usb_xfer *, uint8_t);
75 static usb_error_t usb_handle_request(struct usb_xfer *);
76 static usb_error_t usb_handle_set_config(struct usb_xfer *, uint8_t);
77 static usb_error_t usb_handle_set_stall(struct usb_xfer *, uint8_t,
79 static usb_error_t usb_handle_iface_request(struct usb_xfer *, void **,
80 uint16_t *, struct usb_device_request, uint16_t,
83 /*------------------------------------------------------------------------*
84 * usb_handle_request_callback
86 * This function is the USB callback for generic USB Device control
88 *------------------------------------------------------------------------*/
90 usb_handle_request_callback(struct usb_xfer *xfer, usb_error_t error)
94 /* check the current transfer state */
96 switch (USB_GET_STATE(xfer)) {
98 case USB_ST_TRANSFERRED:
100 /* handle the request */
101 err = usb_handle_request(xfer);
105 if (err == USB_ERR_BAD_CONTEXT) {
106 /* we need to re-setup the control transfer */
107 usb_needs_explore(xfer->xroot->bus, 0);
112 usbd_transfer_submit(xfer);
116 /* check if a control transfer is active */
117 if (xfer->flags_int.control_rem != 0xFFFF) {
118 /* handle the request */
119 err = usb_handle_request(xfer);
121 if (xfer->error != USB_ERR_CANCELLED) {
122 /* should not happen - try stalling */
131 * If a control transfer is active, stall it, and wait for the
132 * next control transfer.
134 usbd_xfer_set_frame_len(xfer, 0, sizeof(struct usb_device_request));
136 xfer->flags.manual_status = 1;
137 xfer->flags.force_short_xfer = 0;
138 usbd_xfer_set_stall(xfer); /* cancel previous transfer, if any */
139 usbd_transfer_submit(xfer);
142 /*------------------------------------------------------------------------*
143 * usb_handle_set_config
148 *------------------------------------------------------------------------*/
150 usb_handle_set_config(struct usb_xfer *xfer, uint8_t conf_no)
152 struct usb_device *udev = xfer->xroot->udev;
157 * We need to protect against other threads doing probe and
160 USB_XFER_UNLOCK(xfer);
162 /* Prevent re-enumeration */
163 do_unlock = usbd_enum_lock(udev);
165 if (conf_no == USB_UNCONFIG_NO) {
166 conf_no = USB_UNCONFIG_INDEX;
169 * The relationship between config number and config index
170 * is very simple in our case:
175 if (usbd_set_config_index(udev, conf_no)) {
176 DPRINTF("set config %d failed\n", conf_no);
177 err = USB_ERR_STALLED;
180 if (usb_probe_and_attach(udev, USB_IFACE_INDEX_ANY)) {
181 DPRINTF("probe and attach failed\n");
182 err = USB_ERR_STALLED;
187 usbd_enum_unlock(udev);
193 usb_check_alt_setting(struct usb_device *udev,
194 struct usb_interface *iface, uint8_t alt_index)
199 /* Prevent re-enumeration */
200 do_unlock = usbd_enum_lock(udev);
202 if (alt_index >= usbd_get_no_alts(udev->cdesc, iface->idesc))
206 usbd_enum_unlock(udev);
211 /*------------------------------------------------------------------------*
212 * usb_handle_iface_request
217 *------------------------------------------------------------------------*/
219 usb_handle_iface_request(struct usb_xfer *xfer,
220 void **ppdata, uint16_t *plen,
221 struct usb_device_request req, uint16_t off, uint8_t state)
223 struct usb_interface *iface;
224 struct usb_interface *iface_parent; /* parent interface */
225 struct usb_device *udev = xfer->xroot->udev;
231 if ((req.bmRequestType & 0x1F) == UT_INTERFACE) {
232 iface_index = req.wIndex[0]; /* unicast */
234 iface_index = 0; /* broadcast */
238 * We need to protect against other threads doing probe and
241 USB_XFER_UNLOCK(xfer);
243 /* Prevent re-enumeration */
244 do_unlock = usbd_enum_lock(udev);
249 iface = usbd_get_iface(udev, iface_index);
250 if ((iface == NULL) ||
251 (iface->idesc == NULL)) {
252 /* end of interfaces non-existing interface */
255 /* set initial state */
259 /* forward request to interface, if any */
263 (iface->subdev != NULL) &&
264 device_is_attached(iface->subdev)) {
266 DEVMETHOD(usb_handle_request, NULL); /* dummy */
268 error = USB_HANDLE_REQUEST(iface->subdev,
272 iface_parent = usbd_get_iface(udev, iface->parent_iface_index);
274 if ((iface_parent == NULL) ||
275 (iface_parent->idesc == NULL)) {
276 /* non-existing interface */
279 /* forward request to parent interface, if any */
283 (iface_parent != NULL) &&
284 (iface_parent->subdev != NULL) &&
285 ((req.bmRequestType & 0x1F) == UT_INTERFACE) &&
286 (iface_parent->subdev != iface->subdev) &&
287 device_is_attached(iface_parent->subdev)) {
288 error = USB_HANDLE_REQUEST(iface_parent->subdev,
289 &req, ppdata, plen, off, &temp_state);
292 /* negativly adjust pointer and length */
293 *ppdata = ((uint8_t *)(*ppdata)) - off;
296 if ((state == USB_HR_NOT_COMPLETE) &&
297 (temp_state == USB_HR_COMPLETE_OK))
301 } else if (error == ENOTTY) {
304 if ((req.bmRequestType & 0x1F) != UT_INTERFACE) {
305 iface_index++; /* iterate */
308 if (state != USB_HR_NOT_COMPLETE) {
309 /* we are complete */
312 switch (req.bmRequestType) {
313 case UT_WRITE_INTERFACE:
314 switch (req.bRequest) {
315 case UR_SET_INTERFACE:
317 * We assume that the endpoints are the same
318 * across the alternate settings.
320 * Reset the endpoints, because re-attaching
321 * only a part of the device is not possible.
323 error = usb_check_alt_setting(udev,
324 iface, req.wValue[0]);
326 DPRINTF("alt setting does not exist %s\n",
330 error = usb_reset_iface_endpoints(udev, iface_index);
332 DPRINTF("alt setting failed %s\n",
336 /* update the current alternate setting */
337 iface->alt_index = req.wValue[0];
345 case UT_READ_INTERFACE:
346 switch (req.bRequest) {
347 case UR_GET_INTERFACE:
348 *ppdata = &iface->alt_index;
361 usbd_enum_unlock(udev);
367 usbd_enum_unlock(udev);
369 return (USB_ERR_SHORT_XFER);
373 usbd_enum_unlock(udev);
375 return (USB_ERR_STALLED);
378 /*------------------------------------------------------------------------*
384 *------------------------------------------------------------------------*/
386 usb_handle_set_stall(struct usb_xfer *xfer, uint8_t ep, uint8_t do_stall)
388 struct usb_device *udev = xfer->xroot->udev;
391 USB_XFER_UNLOCK(xfer);
392 err = usbd_set_endpoint_stall(udev,
393 usbd_get_ep_by_addr(udev, ep), do_stall);
398 /*------------------------------------------------------------------------*
399 * usb_handle_get_stall
404 *------------------------------------------------------------------------*/
406 usb_handle_get_stall(struct usb_device *udev, uint8_t ea_val)
408 struct usb_endpoint *ep;
411 ep = usbd_get_ep_by_addr(udev, ea_val);
416 USB_BUS_LOCK(udev->bus);
417 halted = ep->is_stalled;
418 USB_BUS_UNLOCK(udev->bus);
423 /*------------------------------------------------------------------------*
424 * usb_handle_remote_wakeup
429 *------------------------------------------------------------------------*/
431 usb_handle_remote_wakeup(struct usb_xfer *xfer, uint8_t is_on)
433 struct usb_device *udev;
436 udev = xfer->xroot->udev;
442 udev->flags.remote_wakeup = 1;
444 udev->flags.remote_wakeup = 0;
450 /* In case we are out of sync, update the power state. */
451 usb_bus_power_update(udev->bus);
453 return (0); /* success */
456 /*------------------------------------------------------------------------*
459 * Internal state sequence:
461 * USB_HR_NOT_COMPLETE -> USB_HR_COMPLETE_OK v USB_HR_COMPLETE_ERR
464 * 0: Ready to start hardware
465 * Else: Stall current transfer, if any
466 *------------------------------------------------------------------------*/
468 usb_handle_request(struct usb_xfer *xfer)
470 struct usb_device_request req;
471 struct usb_device *udev;
472 const void *src_zcopy; /* zero-copy source pointer */
473 const void *src_mcopy; /* non zero-copy source pointer */
474 uint16_t off; /* data offset */
475 uint16_t rem; /* data remainder */
476 uint16_t max_len; /* max fragment length */
479 uint8_t is_complete = 1;
487 * Filter the USB transfer state into
488 * something which we understand:
491 switch (USB_GET_STATE(xfer)) {
493 state = USB_HR_NOT_COMPLETE;
495 if (!xfer->flags_int.control_act) {
500 case USB_ST_TRANSFERRED:
501 if (!xfer->flags_int.control_act) {
502 state = USB_HR_COMPLETE_OK;
504 state = USB_HR_NOT_COMPLETE;
508 state = USB_HR_COMPLETE_ERR;
512 /* reset frame stuff */
514 usbd_xfer_set_frame_len(xfer, 0, 0);
516 usbd_xfer_set_frame_offset(xfer, 0, 0);
517 usbd_xfer_set_frame_offset(xfer, sizeof(req), 1);
519 /* get the current request, if any */
521 usbd_copy_out(xfer->frbuffers, 0, &req, sizeof(req));
523 if (xfer->flags_int.control_rem == 0xFFFF) {
524 /* first time - not initialised */
525 rem = UGETW(req.wLength);
528 /* not first time - initialised */
529 rem = xfer->flags_int.control_rem;
530 off = UGETW(req.wLength) - rem;
533 /* set some defaults */
538 udev = xfer->xroot->udev;
540 /* get some request fields decoded */
542 wValue = UGETW(req.wValue);
544 DPRINTF("req 0x%02x 0x%02x 0x%04x 0x%04x "
545 "off=0x%x rem=0x%x, state=%d\n", req.bmRequestType,
546 req.bRequest, wValue, UGETW(req.wIndex), off, rem, state);
548 /* demultiplex the control request */
550 switch (req.bmRequestType) {
552 if (state != USB_HR_NOT_COMPLETE) {
555 switch (req.bRequest) {
556 case UR_GET_DESCRIPTOR:
557 goto tr_handle_get_descriptor;
559 goto tr_handle_get_config;
561 goto tr_handle_get_status;
567 case UT_WRITE_DEVICE:
568 switch (req.bRequest) {
570 goto tr_handle_set_address;
572 goto tr_handle_set_config;
573 case UR_CLEAR_FEATURE:
575 case UF_DEVICE_REMOTE_WAKEUP:
576 goto tr_handle_clear_wakeup;
583 case UF_DEVICE_REMOTE_WAKEUP:
584 goto tr_handle_set_wakeup;
594 case UT_WRITE_ENDPOINT:
595 switch (req.bRequest) {
596 case UR_CLEAR_FEATURE:
598 case UF_ENDPOINT_HALT:
599 goto tr_handle_clear_halt;
606 case UF_ENDPOINT_HALT:
607 goto tr_handle_set_halt;
617 case UT_READ_ENDPOINT:
618 switch (req.bRequest) {
620 goto tr_handle_get_ep_status;
626 /* we use "USB_ADD_BYTES" to de-const the src_zcopy */
627 err = usb_handle_iface_request(xfer,
628 USB_ADD_BYTES(&src_zcopy, 0),
629 &max_len, req, off, state);
633 } else if (err == USB_ERR_SHORT_XFER) {
637 * Reset zero-copy pointer and max length
638 * variable in case they were unintentionally
645 * Check if we have a vendor specific
648 goto tr_handle_get_descriptor;
652 tr_handle_get_descriptor:
653 err = (usb_temp_get_desc_p) (udev, &req, &src_zcopy, &max_len);
656 if (src_zcopy == NULL)
660 tr_handle_get_config:
661 temp.buf[0] = udev->curr_config_no;
662 src_mcopy = temp.buf;
666 tr_handle_get_status:
670 USB_BUS_LOCK(udev->bus);
671 if (udev->flags.remote_wakeup) {
672 wValue |= UDS_REMOTE_WAKEUP;
674 if (udev->flags.self_powered) {
675 wValue |= UDS_SELF_POWERED;
677 USB_BUS_UNLOCK(udev->bus);
679 USETW(temp.wStatus, wValue);
680 src_mcopy = temp.wStatus;
681 max_len = sizeof(temp.wStatus);
684 tr_handle_set_address:
685 if (state == USB_HR_NOT_COMPLETE) {
686 if (wValue >= 0x80) {
689 } else if (udev->curr_config_no != 0) {
690 /* we are configured ! */
693 } else if (state != USB_HR_NOT_COMPLETE) {
694 udev->address = (wValue & 0x7F);
699 tr_handle_set_config:
700 if (state == USB_HR_NOT_COMPLETE) {
701 if (usb_handle_set_config(xfer, req.wValue[0])) {
707 tr_handle_clear_halt:
708 if (state == USB_HR_NOT_COMPLETE) {
709 if (usb_handle_set_stall(xfer, req.wIndex[0], 0)) {
715 tr_handle_clear_wakeup:
716 if (state == USB_HR_NOT_COMPLETE) {
717 if (usb_handle_remote_wakeup(xfer, 0)) {
724 if (state == USB_HR_NOT_COMPLETE) {
725 if (usb_handle_set_stall(xfer, req.wIndex[0], 1)) {
731 tr_handle_set_wakeup:
732 if (state == USB_HR_NOT_COMPLETE) {
733 if (usb_handle_remote_wakeup(xfer, 1)) {
739 tr_handle_get_ep_status:
740 if (state == USB_HR_NOT_COMPLETE) {
742 usb_handle_get_stall(udev, req.wIndex[0]);
744 src_mcopy = temp.wStatus;
745 max_len = sizeof(temp.wStatus);
750 if (state != USB_HR_NOT_COMPLETE) {
753 /* subtract offset from length */
757 /* Compute the real maximum data length */
759 if (max_len > xfer->max_data_length) {
760 max_len = usbd_xfer_max_len(xfer);
766 * If the remainder is greater than the maximum data length,
767 * we need to truncate the value for the sake of the
770 if (rem > xfer->max_data_length) {
771 rem = usbd_xfer_max_len(xfer);
773 if ((rem != max_len) && (is_complete != 0)) {
775 * If we don't transfer the data we can transfer, then
776 * the transfer is short !
778 xfer->flags.force_short_xfer = 1;
784 xfer->flags.force_short_xfer = 0;
785 xfer->nframes = max_len ? 2 : 1;
789 src_mcopy = USB_ADD_BYTES(src_mcopy, off);
790 usbd_copy_in(xfer->frbuffers + 1, 0,
792 usbd_xfer_set_frame_len(xfer, 1, max_len);
794 usbd_xfer_set_frame_data(xfer, 1,
795 USB_ADD_BYTES(src_zcopy, off), max_len);
798 /* the end is reached, send status */
799 xfer->flags.manual_status = 0;
800 usbd_xfer_set_frame_len(xfer, 1, 0);
802 DPRINTF("success\n");
803 return (0); /* success */
806 DPRINTF("%s\n", (state != USB_HR_NOT_COMPLETE) ?
807 "complete" : "stalled");
808 return (USB_ERR_STALLED);
811 DPRINTF("bad context\n");
812 return (USB_ERR_BAD_CONTEXT);