3 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/stdint.h>
28 #include <sys/stddef.h>
29 #include <sys/param.h>
30 #include <sys/queue.h>
31 #include <sys/types.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
35 #include <sys/linker_set.h>
36 #include <sys/module.h>
38 #include <sys/mutex.h>
39 #include <sys/condvar.h>
40 #include <sys/sysctl.h>
42 #include <sys/unistd.h>
43 #include <sys/callout.h>
44 #include <sys/malloc.h>
47 #include <dev/usb/usb.h>
48 #include <dev/usb/usbdi.h>
49 #include <dev/usb/usbdi_util.h>
52 #define USB_DEBUG_VAR usb_debug
54 #include <dev/usb/usb_core.h>
55 #include <dev/usb/usb_process.h>
56 #include <dev/usb/usb_busdma.h>
57 #include <dev/usb/usb_transfer.h>
58 #include <dev/usb/usb_device.h>
59 #include <dev/usb/usb_debug.h>
60 #include <dev/usb/usb_dynamic.h>
61 #include <dev/usb/usb_hub.h>
63 #include <dev/usb/usb_controller.h>
64 #include <dev/usb/usb_bus.h>
66 /* function prototypes */
68 static uint8_t usb_handle_get_stall(struct usb_device *, uint8_t);
69 static usb_error_t usb_handle_remote_wakeup(struct usb_xfer *, uint8_t);
70 static usb_error_t usb_handle_request(struct usb_xfer *);
71 static usb_error_t usb_handle_set_config(struct usb_xfer *, uint8_t);
72 static usb_error_t usb_handle_set_stall(struct usb_xfer *, uint8_t,
74 static usb_error_t usb_handle_iface_request(struct usb_xfer *, void **,
75 uint16_t *, struct usb_device_request, uint16_t,
78 /*------------------------------------------------------------------------*
79 * usb_handle_request_callback
81 * This function is the USB callback for generic USB Device control
83 *------------------------------------------------------------------------*/
85 usb_handle_request_callback(struct usb_xfer *xfer, usb_error_t error)
89 /* check the current transfer state */
91 switch (USB_GET_STATE(xfer)) {
93 case USB_ST_TRANSFERRED:
95 /* handle the request */
96 err = usb_handle_request(xfer);
100 if (err == USB_ERR_BAD_CONTEXT) {
101 /* we need to re-setup the control transfer */
102 usb_needs_explore(xfer->xroot->bus, 0);
107 usbd_transfer_submit(xfer);
111 /* check if a control transfer is active */
112 if (xfer->flags_int.control_rem != 0xFFFF) {
113 /* handle the request */
114 err = usb_handle_request(xfer);
116 if (xfer->error != USB_ERR_CANCELLED) {
117 /* should not happen - try stalling */
126 * If a control transfer is active, stall it, and wait for the
127 * next control transfer.
129 usbd_xfer_set_frame_len(xfer, 0, sizeof(struct usb_device_request));
131 xfer->flags.manual_status = 1;
132 xfer->flags.force_short_xfer = 0;
133 usbd_xfer_set_stall(xfer); /* cancel previous transfer, if any */
134 usbd_transfer_submit(xfer);
137 /*------------------------------------------------------------------------*
138 * usb_handle_set_config
143 *------------------------------------------------------------------------*/
145 usb_handle_set_config(struct usb_xfer *xfer, uint8_t conf_no)
147 struct usb_device *udev = xfer->xroot->udev;
151 * We need to protect against other threads doing probe and
154 USB_XFER_UNLOCK(xfer);
156 usbd_enum_lock(udev);
158 if (conf_no == USB_UNCONFIG_NO) {
159 conf_no = USB_UNCONFIG_INDEX;
162 * The relationship between config number and config index
163 * is very simple in our case:
168 if (usbd_set_config_index(udev, conf_no)) {
169 DPRINTF("set config %d failed\n", conf_no);
170 err = USB_ERR_STALLED;
173 if (usb_probe_and_attach(udev, USB_IFACE_INDEX_ANY)) {
174 DPRINTF("probe and attach failed\n");
175 err = USB_ERR_STALLED;
179 usbd_enum_unlock(udev);
185 usb_check_alt_setting(struct usb_device *udev,
186 struct usb_interface *iface, uint8_t alt_index)
191 /* automatic locking */
192 if (usbd_enum_is_locked(udev)) {
196 usbd_enum_lock(udev);
199 if (alt_index >= usbd_get_no_alts(udev->cdesc, iface->idesc))
203 usbd_enum_unlock(udev);
208 /*------------------------------------------------------------------------*
209 * usb_handle_iface_request
214 *------------------------------------------------------------------------*/
216 usb_handle_iface_request(struct usb_xfer *xfer,
217 void **ppdata, uint16_t *plen,
218 struct usb_device_request req, uint16_t off, uint8_t state)
220 struct usb_interface *iface;
221 struct usb_interface *iface_parent; /* parent interface */
222 struct usb_device *udev = xfer->xroot->udev;
227 if ((req.bmRequestType & 0x1F) == UT_INTERFACE) {
228 iface_index = req.wIndex[0]; /* unicast */
230 iface_index = 0; /* broadcast */
234 * We need to protect against other threads doing probe and
237 USB_XFER_UNLOCK(xfer);
239 usbd_enum_lock(udev);
244 iface = usbd_get_iface(udev, iface_index);
245 if ((iface == NULL) ||
246 (iface->idesc == NULL)) {
247 /* end of interfaces non-existing interface */
250 /* set initial state */
254 /* forward request to interface, if any */
258 (iface->subdev != NULL) &&
259 device_is_attached(iface->subdev)) {
261 DEVMETHOD(usb_handle_request, NULL); /* dummy */
263 error = USB_HANDLE_REQUEST(iface->subdev,
267 iface_parent = usbd_get_iface(udev, iface->parent_iface_index);
269 if ((iface_parent == NULL) ||
270 (iface_parent->idesc == NULL)) {
271 /* non-existing interface */
274 /* forward request to parent interface, if any */
278 (iface_parent != NULL) &&
279 (iface_parent->subdev != NULL) &&
280 ((req.bmRequestType & 0x1F) == UT_INTERFACE) &&
281 (iface_parent->subdev != iface->subdev) &&
282 device_is_attached(iface_parent->subdev)) {
283 error = USB_HANDLE_REQUEST(iface_parent->subdev,
284 &req, ppdata, plen, off, &temp_state);
287 /* negativly adjust pointer and length */
288 *ppdata = ((uint8_t *)(*ppdata)) - off;
291 if ((state == USB_HR_NOT_COMPLETE) &&
292 (temp_state == USB_HR_COMPLETE_OK))
296 } else if (error == ENOTTY) {
299 if ((req.bmRequestType & 0x1F) != UT_INTERFACE) {
300 iface_index++; /* iterate */
303 if (state != USB_HR_NOT_COMPLETE) {
304 /* we are complete */
307 switch (req.bmRequestType) {
308 case UT_WRITE_INTERFACE:
309 switch (req.bRequest) {
310 case UR_SET_INTERFACE:
312 * We assume that the endpoints are the same
313 * accross the alternate settings.
315 * Reset the endpoints, because re-attaching
316 * only a part of the device is not possible.
318 error = usb_check_alt_setting(udev,
319 iface, req.wValue[0]);
321 DPRINTF("alt setting does not exist %s\n",
325 error = usb_reset_iface_endpoints(udev, iface_index);
327 DPRINTF("alt setting failed %s\n",
331 /* update the current alternate setting */
332 iface->alt_index = req.wValue[0];
340 case UT_READ_INTERFACE:
341 switch (req.bRequest) {
342 case UR_GET_INTERFACE:
343 *ppdata = &iface->alt_index;
355 usbd_enum_unlock(udev);
360 usbd_enum_unlock(udev);
362 return (USB_ERR_SHORT_XFER);
365 usbd_enum_unlock(udev);
367 return (USB_ERR_STALLED);
370 /*------------------------------------------------------------------------*
376 *------------------------------------------------------------------------*/
378 usb_handle_set_stall(struct usb_xfer *xfer, uint8_t ep, uint8_t do_stall)
380 struct usb_device *udev = xfer->xroot->udev;
383 USB_XFER_UNLOCK(xfer);
384 err = usbd_set_endpoint_stall(udev,
385 usbd_get_ep_by_addr(udev, ep), do_stall);
390 /*------------------------------------------------------------------------*
391 * usb_handle_get_stall
396 *------------------------------------------------------------------------*/
398 usb_handle_get_stall(struct usb_device *udev, uint8_t ea_val)
400 struct usb_endpoint *ep;
403 ep = usbd_get_ep_by_addr(udev, ea_val);
408 USB_BUS_LOCK(udev->bus);
409 halted = ep->is_stalled;
410 USB_BUS_UNLOCK(udev->bus);
415 /*------------------------------------------------------------------------*
416 * usb_handle_remote_wakeup
421 *------------------------------------------------------------------------*/
423 usb_handle_remote_wakeup(struct usb_xfer *xfer, uint8_t is_on)
425 struct usb_device *udev;
428 udev = xfer->xroot->udev;
434 udev->flags.remote_wakeup = 1;
436 udev->flags.remote_wakeup = 0;
441 /* In case we are out of sync, update the power state. */
442 usb_bus_power_update(udev->bus);
443 return (0); /* success */
446 /*------------------------------------------------------------------------*
449 * Internal state sequence:
451 * USB_HR_NOT_COMPLETE -> USB_HR_COMPLETE_OK v USB_HR_COMPLETE_ERR
454 * 0: Ready to start hardware
455 * Else: Stall current transfer, if any
456 *------------------------------------------------------------------------*/
458 usb_handle_request(struct usb_xfer *xfer)
460 struct usb_device_request req;
461 struct usb_device *udev;
462 const void *src_zcopy; /* zero-copy source pointer */
463 const void *src_mcopy; /* non zero-copy source pointer */
464 uint16_t off; /* data offset */
465 uint16_t rem; /* data remainder */
466 uint16_t max_len; /* max fragment length */
470 uint8_t is_complete = 1;
478 * Filter the USB transfer state into
479 * something which we understand:
482 switch (USB_GET_STATE(xfer)) {
484 state = USB_HR_NOT_COMPLETE;
486 if (!xfer->flags_int.control_act) {
491 case USB_ST_TRANSFERRED:
492 if (!xfer->flags_int.control_act) {
493 state = USB_HR_COMPLETE_OK;
495 state = USB_HR_NOT_COMPLETE;
499 state = USB_HR_COMPLETE_ERR;
503 /* reset frame stuff */
505 usbd_xfer_set_frame_len(xfer, 0, 0);
507 usbd_xfer_set_frame_offset(xfer, 0, 0);
508 usbd_xfer_set_frame_offset(xfer, sizeof(req), 1);
510 /* get the current request, if any */
512 usbd_copy_out(xfer->frbuffers, 0, &req, sizeof(req));
514 if (xfer->flags_int.control_rem == 0xFFFF) {
515 /* first time - not initialised */
516 rem = UGETW(req.wLength);
519 /* not first time - initialised */
520 rem = xfer->flags_int.control_rem;
521 off = UGETW(req.wLength) - rem;
524 /* set some defaults */
529 udev = xfer->xroot->udev;
531 /* get some request fields decoded */
533 wValue = UGETW(req.wValue);
534 wIndex = UGETW(req.wIndex);
536 DPRINTF("req 0x%02x 0x%02x 0x%04x 0x%04x "
537 "off=0x%x rem=0x%x, state=%d\n", req.bmRequestType,
538 req.bRequest, wValue, wIndex, off, rem, state);
540 /* demultiplex the control request */
542 switch (req.bmRequestType) {
544 if (state != USB_HR_NOT_COMPLETE) {
547 switch (req.bRequest) {
548 case UR_GET_DESCRIPTOR:
549 goto tr_handle_get_descriptor;
551 goto tr_handle_get_config;
553 goto tr_handle_get_status;
559 case UT_WRITE_DEVICE:
560 switch (req.bRequest) {
562 goto tr_handle_set_address;
564 goto tr_handle_set_config;
565 case UR_CLEAR_FEATURE:
567 case UF_DEVICE_REMOTE_WAKEUP:
568 goto tr_handle_clear_wakeup;
575 case UF_DEVICE_REMOTE_WAKEUP:
576 goto tr_handle_set_wakeup;
586 case UT_WRITE_ENDPOINT:
587 switch (req.bRequest) {
588 case UR_CLEAR_FEATURE:
590 case UF_ENDPOINT_HALT:
591 goto tr_handle_clear_halt;
598 case UF_ENDPOINT_HALT:
599 goto tr_handle_set_halt;
609 case UT_READ_ENDPOINT:
610 switch (req.bRequest) {
612 goto tr_handle_get_ep_status;
618 /* we use "USB_ADD_BYTES" to de-const the src_zcopy */
619 err = usb_handle_iface_request(xfer,
620 USB_ADD_BYTES(&src_zcopy, 0),
621 &max_len, req, off, state);
625 } else if (err == USB_ERR_SHORT_XFER) {
629 * Reset zero-copy pointer and max length
630 * variable in case they were unintentionally
637 * Check if we have a vendor specific
640 goto tr_handle_get_descriptor;
644 tr_handle_get_descriptor:
645 err = (usb_temp_get_desc_p) (udev, &req, &src_zcopy, &max_len);
648 if (src_zcopy == NULL)
652 tr_handle_get_config:
653 temp.buf[0] = udev->curr_config_no;
654 src_mcopy = temp.buf;
658 tr_handle_get_status:
662 USB_BUS_LOCK(udev->bus);
663 if (udev->flags.remote_wakeup) {
664 wValue |= UDS_REMOTE_WAKEUP;
666 if (udev->flags.self_powered) {
667 wValue |= UDS_SELF_POWERED;
669 USB_BUS_UNLOCK(udev->bus);
671 USETW(temp.wStatus, wValue);
672 src_mcopy = temp.wStatus;
673 max_len = sizeof(temp.wStatus);
676 tr_handle_set_address:
677 if (state == USB_HR_NOT_COMPLETE) {
678 if (wValue >= 0x80) {
681 } else if (udev->curr_config_no != 0) {
682 /* we are configured ! */
685 } else if (state != USB_HR_NOT_COMPLETE) {
686 udev->address = (wValue & 0x7F);
691 tr_handle_set_config:
692 if (state == USB_HR_NOT_COMPLETE) {
693 if (usb_handle_set_config(xfer, req.wValue[0])) {
699 tr_handle_clear_halt:
700 if (state == USB_HR_NOT_COMPLETE) {
701 if (usb_handle_set_stall(xfer, req.wIndex[0], 0)) {
707 tr_handle_clear_wakeup:
708 if (state == USB_HR_NOT_COMPLETE) {
709 if (usb_handle_remote_wakeup(xfer, 0)) {
716 if (state == USB_HR_NOT_COMPLETE) {
717 if (usb_handle_set_stall(xfer, req.wIndex[0], 1)) {
723 tr_handle_set_wakeup:
724 if (state == USB_HR_NOT_COMPLETE) {
725 if (usb_handle_remote_wakeup(xfer, 1)) {
731 tr_handle_get_ep_status:
732 if (state == USB_HR_NOT_COMPLETE) {
734 usb_handle_get_stall(udev, req.wIndex[0]);
736 src_mcopy = temp.wStatus;
737 max_len = sizeof(temp.wStatus);
742 if (state != USB_HR_NOT_COMPLETE) {
745 /* subtract offset from length */
749 /* Compute the real maximum data length */
751 if (max_len > xfer->max_data_length) {
752 max_len = usbd_xfer_max_len(xfer);
758 * If the remainder is greater than the maximum data length,
759 * we need to truncate the value for the sake of the
762 if (rem > xfer->max_data_length) {
763 rem = usbd_xfer_max_len(xfer);
765 if ((rem != max_len) && (is_complete != 0)) {
767 * If we don't transfer the data we can transfer, then
768 * the transfer is short !
770 xfer->flags.force_short_xfer = 1;
776 xfer->flags.force_short_xfer = 0;
777 xfer->nframes = max_len ? 2 : 1;
781 src_mcopy = USB_ADD_BYTES(src_mcopy, off);
782 usbd_copy_in(xfer->frbuffers + 1, 0,
784 usbd_xfer_set_frame_len(xfer, 1, max_len);
786 usbd_xfer_set_frame_data(xfer, 1,
787 USB_ADD_BYTES(src_zcopy, off), max_len);
790 /* the end is reached, send status */
791 xfer->flags.manual_status = 0;
792 usbd_xfer_set_frame_len(xfer, 1, 0);
794 DPRINTF("success\n");
795 return (0); /* success */
798 DPRINTF("%s\n", (state != USB_HR_NOT_COMPLETE) ?
799 "complete" : "stalled");
800 return (USB_ERR_STALLED);
803 DPRINTF("bad context\n");
804 return (USB_ERR_BAD_CONTEXT);