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/module.h>
37 #include <sys/mutex.h>
38 #include <sys/condvar.h>
39 #include <sys/sysctl.h>
41 #include <sys/unistd.h>
42 #include <sys/callout.h>
43 #include <sys/malloc.h>
46 #include <dev/usb/usb.h>
47 #include <dev/usb/usbdi.h>
48 #include <dev/usb/usbdi_util.h>
51 #define USB_DEBUG_VAR usb_debug
53 #include <dev/usb/usb_core.h>
54 #include <dev/usb/usb_process.h>
55 #include <dev/usb/usb_busdma.h>
56 #include <dev/usb/usb_transfer.h>
57 #include <dev/usb/usb_device.h>
58 #include <dev/usb/usb_debug.h>
59 #include <dev/usb/usb_dynamic.h>
60 #include <dev/usb/usb_hub.h>
62 #include <dev/usb/usb_controller.h>
63 #include <dev/usb/usb_bus.h>
65 /* function prototypes */
67 static uint8_t usb_handle_get_stall(struct usb_device *, uint8_t);
68 static usb_error_t usb_handle_remote_wakeup(struct usb_xfer *, uint8_t);
69 static usb_error_t usb_handle_request(struct usb_xfer *);
70 static usb_error_t usb_handle_set_config(struct usb_xfer *, uint8_t);
71 static usb_error_t usb_handle_set_stall(struct usb_xfer *, uint8_t,
73 static usb_error_t usb_handle_iface_request(struct usb_xfer *, void **,
74 uint16_t *, struct usb_device_request, uint16_t,
77 /*------------------------------------------------------------------------*
78 * usb_handle_request_callback
80 * This function is the USB callback for generic USB Device control
82 *------------------------------------------------------------------------*/
84 usb_handle_request_callback(struct usb_xfer *xfer, usb_error_t error)
88 /* check the current transfer state */
90 switch (USB_GET_STATE(xfer)) {
92 case USB_ST_TRANSFERRED:
94 /* handle the request */
95 err = usb_handle_request(xfer);
99 if (err == USB_ERR_BAD_CONTEXT) {
100 /* we need to re-setup the control transfer */
101 usb_needs_explore(xfer->xroot->bus, 0);
106 usbd_transfer_submit(xfer);
110 /* check if a control transfer is active */
111 if (xfer->flags_int.control_rem != 0xFFFF) {
112 /* handle the request */
113 err = usb_handle_request(xfer);
115 if (xfer->error != USB_ERR_CANCELLED) {
116 /* should not happen - try stalling */
125 * If a control transfer is active, stall it, and wait for the
126 * next control transfer.
128 usbd_xfer_set_frame_len(xfer, 0, sizeof(struct usb_device_request));
130 xfer->flags.manual_status = 1;
131 xfer->flags.force_short_xfer = 0;
132 usbd_xfer_set_stall(xfer); /* cancel previous transfer, if any */
133 usbd_transfer_submit(xfer);
136 /*------------------------------------------------------------------------*
137 * usb_handle_set_config
142 *------------------------------------------------------------------------*/
144 usb_handle_set_config(struct usb_xfer *xfer, uint8_t conf_no)
146 struct usb_device *udev = xfer->xroot->udev;
151 * We need to protect against other threads doing probe and
154 USB_XFER_UNLOCK(xfer);
156 /* Prevent re-enumeration */
157 do_unlock = usbd_enum_lock(udev);
159 if (conf_no == USB_UNCONFIG_NO) {
160 conf_no = USB_UNCONFIG_INDEX;
163 * The relationship between config number and config index
164 * is very simple in our case:
169 if (usbd_set_config_index(udev, conf_no)) {
170 DPRINTF("set config %d failed\n", conf_no);
171 err = USB_ERR_STALLED;
174 if (usb_probe_and_attach(udev, USB_IFACE_INDEX_ANY)) {
175 DPRINTF("probe and attach failed\n");
176 err = USB_ERR_STALLED;
181 usbd_enum_unlock(udev);
187 usb_check_alt_setting(struct usb_device *udev,
188 struct usb_interface *iface, uint8_t alt_index)
193 /* Prevent re-enumeration */
194 do_unlock = usbd_enum_lock(udev);
196 if (alt_index >= usbd_get_no_alts(udev->cdesc, iface->idesc))
200 usbd_enum_unlock(udev);
205 /*------------------------------------------------------------------------*
206 * usb_handle_iface_request
211 *------------------------------------------------------------------------*/
213 usb_handle_iface_request(struct usb_xfer *xfer,
214 void **ppdata, uint16_t *plen,
215 struct usb_device_request req, uint16_t off, uint8_t state)
217 struct usb_interface *iface;
218 struct usb_interface *iface_parent; /* parent interface */
219 struct usb_device *udev = xfer->xroot->udev;
225 if ((req.bmRequestType & 0x1F) == UT_INTERFACE) {
226 iface_index = req.wIndex[0]; /* unicast */
228 iface_index = 0; /* broadcast */
232 * We need to protect against other threads doing probe and
235 USB_XFER_UNLOCK(xfer);
237 /* Prevent re-enumeration */
238 do_unlock = usbd_enum_lock(udev);
243 iface = usbd_get_iface(udev, iface_index);
244 if ((iface == NULL) ||
245 (iface->idesc == NULL)) {
246 /* end of interfaces non-existing interface */
249 /* set initial state */
253 /* forward request to interface, if any */
257 (iface->subdev != NULL) &&
258 device_is_attached(iface->subdev)) {
260 DEVMETHOD(usb_handle_request, NULL); /* dummy */
262 error = USB_HANDLE_REQUEST(iface->subdev,
266 iface_parent = usbd_get_iface(udev, iface->parent_iface_index);
268 if ((iface_parent == NULL) ||
269 (iface_parent->idesc == NULL)) {
270 /* non-existing interface */
273 /* forward request to parent interface, if any */
277 (iface_parent != NULL) &&
278 (iface_parent->subdev != NULL) &&
279 ((req.bmRequestType & 0x1F) == UT_INTERFACE) &&
280 (iface_parent->subdev != iface->subdev) &&
281 device_is_attached(iface_parent->subdev)) {
282 error = USB_HANDLE_REQUEST(iface_parent->subdev,
283 &req, ppdata, plen, off, &temp_state);
286 /* negativly adjust pointer and length */
287 *ppdata = ((uint8_t *)(*ppdata)) - off;
290 if ((state == USB_HR_NOT_COMPLETE) &&
291 (temp_state == USB_HR_COMPLETE_OK))
295 } else if (error == ENOTTY) {
298 if ((req.bmRequestType & 0x1F) != UT_INTERFACE) {
299 iface_index++; /* iterate */
302 if (state != USB_HR_NOT_COMPLETE) {
303 /* we are complete */
306 switch (req.bmRequestType) {
307 case UT_WRITE_INTERFACE:
308 switch (req.bRequest) {
309 case UR_SET_INTERFACE:
311 * We assume that the endpoints are the same
312 * accross the alternate settings.
314 * Reset the endpoints, because re-attaching
315 * only a part of the device is not possible.
317 error = usb_check_alt_setting(udev,
318 iface, req.wValue[0]);
320 DPRINTF("alt setting does not exist %s\n",
324 error = usb_reset_iface_endpoints(udev, iface_index);
326 DPRINTF("alt setting failed %s\n",
330 /* update the current alternate setting */
331 iface->alt_index = req.wValue[0];
339 case UT_READ_INTERFACE:
340 switch (req.bRequest) {
341 case UR_GET_INTERFACE:
342 *ppdata = &iface->alt_index;
355 usbd_enum_unlock(udev);
361 usbd_enum_unlock(udev);
363 return (USB_ERR_SHORT_XFER);
367 usbd_enum_unlock(udev);
369 return (USB_ERR_STALLED);
372 /*------------------------------------------------------------------------*
378 *------------------------------------------------------------------------*/
380 usb_handle_set_stall(struct usb_xfer *xfer, uint8_t ep, uint8_t do_stall)
382 struct usb_device *udev = xfer->xroot->udev;
385 USB_XFER_UNLOCK(xfer);
386 err = usbd_set_endpoint_stall(udev,
387 usbd_get_ep_by_addr(udev, ep), do_stall);
392 /*------------------------------------------------------------------------*
393 * usb_handle_get_stall
398 *------------------------------------------------------------------------*/
400 usb_handle_get_stall(struct usb_device *udev, uint8_t ea_val)
402 struct usb_endpoint *ep;
405 ep = usbd_get_ep_by_addr(udev, ea_val);
410 USB_BUS_LOCK(udev->bus);
411 halted = ep->is_stalled;
412 USB_BUS_UNLOCK(udev->bus);
417 /*------------------------------------------------------------------------*
418 * usb_handle_remote_wakeup
423 *------------------------------------------------------------------------*/
425 usb_handle_remote_wakeup(struct usb_xfer *xfer, uint8_t is_on)
427 struct usb_device *udev;
430 udev = xfer->xroot->udev;
436 udev->flags.remote_wakeup = 1;
438 udev->flags.remote_wakeup = 0;
444 /* In case we are out of sync, update the power state. */
445 usb_bus_power_update(udev->bus);
447 return (0); /* success */
450 /*------------------------------------------------------------------------*
453 * Internal state sequence:
455 * USB_HR_NOT_COMPLETE -> USB_HR_COMPLETE_OK v USB_HR_COMPLETE_ERR
458 * 0: Ready to start hardware
459 * Else: Stall current transfer, if any
460 *------------------------------------------------------------------------*/
462 usb_handle_request(struct usb_xfer *xfer)
464 struct usb_device_request req;
465 struct usb_device *udev;
466 const void *src_zcopy; /* zero-copy source pointer */
467 const void *src_mcopy; /* non zero-copy source pointer */
468 uint16_t off; /* data offset */
469 uint16_t rem; /* data remainder */
470 uint16_t max_len; /* max fragment length */
473 uint8_t is_complete = 1;
481 * Filter the USB transfer state into
482 * something which we understand:
485 switch (USB_GET_STATE(xfer)) {
487 state = USB_HR_NOT_COMPLETE;
489 if (!xfer->flags_int.control_act) {
494 case USB_ST_TRANSFERRED:
495 if (!xfer->flags_int.control_act) {
496 state = USB_HR_COMPLETE_OK;
498 state = USB_HR_NOT_COMPLETE;
502 state = USB_HR_COMPLETE_ERR;
506 /* reset frame stuff */
508 usbd_xfer_set_frame_len(xfer, 0, 0);
510 usbd_xfer_set_frame_offset(xfer, 0, 0);
511 usbd_xfer_set_frame_offset(xfer, sizeof(req), 1);
513 /* get the current request, if any */
515 usbd_copy_out(xfer->frbuffers, 0, &req, sizeof(req));
517 if (xfer->flags_int.control_rem == 0xFFFF) {
518 /* first time - not initialised */
519 rem = UGETW(req.wLength);
522 /* not first time - initialised */
523 rem = xfer->flags_int.control_rem;
524 off = UGETW(req.wLength) - rem;
527 /* set some defaults */
532 udev = xfer->xroot->udev;
534 /* get some request fields decoded */
536 wValue = UGETW(req.wValue);
538 DPRINTF("req 0x%02x 0x%02x 0x%04x 0x%04x "
539 "off=0x%x rem=0x%x, state=%d\n", req.bmRequestType,
540 req.bRequest, wValue, UGETW(req.wIndex), off, rem, state);
542 /* demultiplex the control request */
544 switch (req.bmRequestType) {
546 if (state != USB_HR_NOT_COMPLETE) {
549 switch (req.bRequest) {
550 case UR_GET_DESCRIPTOR:
551 goto tr_handle_get_descriptor;
553 goto tr_handle_get_config;
555 goto tr_handle_get_status;
561 case UT_WRITE_DEVICE:
562 switch (req.bRequest) {
564 goto tr_handle_set_address;
566 goto tr_handle_set_config;
567 case UR_CLEAR_FEATURE:
569 case UF_DEVICE_REMOTE_WAKEUP:
570 goto tr_handle_clear_wakeup;
577 case UF_DEVICE_REMOTE_WAKEUP:
578 goto tr_handle_set_wakeup;
588 case UT_WRITE_ENDPOINT:
589 switch (req.bRequest) {
590 case UR_CLEAR_FEATURE:
592 case UF_ENDPOINT_HALT:
593 goto tr_handle_clear_halt;
600 case UF_ENDPOINT_HALT:
601 goto tr_handle_set_halt;
611 case UT_READ_ENDPOINT:
612 switch (req.bRequest) {
614 goto tr_handle_get_ep_status;
620 /* we use "USB_ADD_BYTES" to de-const the src_zcopy */
621 err = usb_handle_iface_request(xfer,
622 USB_ADD_BYTES(&src_zcopy, 0),
623 &max_len, req, off, state);
627 } else if (err == USB_ERR_SHORT_XFER) {
631 * Reset zero-copy pointer and max length
632 * variable in case they were unintentionally
639 * Check if we have a vendor specific
642 goto tr_handle_get_descriptor;
646 tr_handle_get_descriptor:
647 err = (usb_temp_get_desc_p) (udev, &req, &src_zcopy, &max_len);
650 if (src_zcopy == NULL)
654 tr_handle_get_config:
655 temp.buf[0] = udev->curr_config_no;
656 src_mcopy = temp.buf;
660 tr_handle_get_status:
664 USB_BUS_LOCK(udev->bus);
665 if (udev->flags.remote_wakeup) {
666 wValue |= UDS_REMOTE_WAKEUP;
668 if (udev->flags.self_powered) {
669 wValue |= UDS_SELF_POWERED;
671 USB_BUS_UNLOCK(udev->bus);
673 USETW(temp.wStatus, wValue);
674 src_mcopy = temp.wStatus;
675 max_len = sizeof(temp.wStatus);
678 tr_handle_set_address:
679 if (state == USB_HR_NOT_COMPLETE) {
680 if (wValue >= 0x80) {
683 } else if (udev->curr_config_no != 0) {
684 /* we are configured ! */
687 } else if (state != USB_HR_NOT_COMPLETE) {
688 udev->address = (wValue & 0x7F);
693 tr_handle_set_config:
694 if (state == USB_HR_NOT_COMPLETE) {
695 if (usb_handle_set_config(xfer, req.wValue[0])) {
701 tr_handle_clear_halt:
702 if (state == USB_HR_NOT_COMPLETE) {
703 if (usb_handle_set_stall(xfer, req.wIndex[0], 0)) {
709 tr_handle_clear_wakeup:
710 if (state == USB_HR_NOT_COMPLETE) {
711 if (usb_handle_remote_wakeup(xfer, 0)) {
718 if (state == USB_HR_NOT_COMPLETE) {
719 if (usb_handle_set_stall(xfer, req.wIndex[0], 1)) {
725 tr_handle_set_wakeup:
726 if (state == USB_HR_NOT_COMPLETE) {
727 if (usb_handle_remote_wakeup(xfer, 1)) {
733 tr_handle_get_ep_status:
734 if (state == USB_HR_NOT_COMPLETE) {
736 usb_handle_get_stall(udev, req.wIndex[0]);
738 src_mcopy = temp.wStatus;
739 max_len = sizeof(temp.wStatus);
744 if (state != USB_HR_NOT_COMPLETE) {
747 /* subtract offset from length */
751 /* Compute the real maximum data length */
753 if (max_len > xfer->max_data_length) {
754 max_len = usbd_xfer_max_len(xfer);
760 * If the remainder is greater than the maximum data length,
761 * we need to truncate the value for the sake of the
764 if (rem > xfer->max_data_length) {
765 rem = usbd_xfer_max_len(xfer);
767 if ((rem != max_len) && (is_complete != 0)) {
769 * If we don't transfer the data we can transfer, then
770 * the transfer is short !
772 xfer->flags.force_short_xfer = 1;
778 xfer->flags.force_short_xfer = 0;
779 xfer->nframes = max_len ? 2 : 1;
783 src_mcopy = USB_ADD_BYTES(src_mcopy, off);
784 usbd_copy_in(xfer->frbuffers + 1, 0,
786 usbd_xfer_set_frame_len(xfer, 1, max_len);
788 usbd_xfer_set_frame_data(xfer, 1,
789 USB_ADD_BYTES(src_zcopy, off), max_len);
792 /* the end is reached, send status */
793 xfer->flags.manual_status = 0;
794 usbd_xfer_set_frame_len(xfer, 1, 0);
796 DPRINTF("success\n");
797 return (0); /* success */
800 DPRINTF("%s\n", (state != USB_HR_NOT_COMPLETE) ?
801 "complete" : "stalled");
802 return (USB_ERR_STALLED);
805 DPRINTF("bad context\n");
806 return (USB_ERR_BAD_CONTEXT);