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 sx_xlock(udev->default_sx + 1);
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 sx_unlock(udev->default_sx + 1);
186 usb_check_alt_setting(struct usb_device *udev,
187 struct usb_interface *iface, uint8_t alt_index)
192 /* automatic locking */
193 if (sx_xlocked(udev->default_sx + 1)) {
197 sx_xlock(udev->default_sx + 1);
200 if (alt_index >= usbd_get_no_alts(udev->cdesc, iface->idesc))
204 sx_unlock(udev->default_sx + 1);
209 /*------------------------------------------------------------------------*
210 * usb_handle_iface_request
215 *------------------------------------------------------------------------*/
217 usb_handle_iface_request(struct usb_xfer *xfer,
218 void **ppdata, uint16_t *plen,
219 struct usb_device_request req, uint16_t off, uint8_t state)
221 struct usb_interface *iface;
222 struct usb_interface *iface_parent; /* parent interface */
223 struct usb_device *udev = xfer->xroot->udev;
228 if ((req.bmRequestType & 0x1F) == UT_INTERFACE) {
229 iface_index = req.wIndex[0]; /* unicast */
231 iface_index = 0; /* broadcast */
235 * We need to protect against other threads doing probe and
238 USB_XFER_UNLOCK(xfer);
240 sx_xlock(udev->default_sx + 1);
245 iface = usbd_get_iface(udev, iface_index);
246 if ((iface == NULL) ||
247 (iface->idesc == NULL)) {
248 /* end of interfaces non-existing interface */
251 /* set initial state */
255 /* forward request to interface, if any */
259 (iface->subdev != NULL) &&
260 device_is_attached(iface->subdev)) {
262 DEVMETHOD(usb_handle_request, NULL); /* dummy */
264 error = USB_HANDLE_REQUEST(iface->subdev,
268 iface_parent = usbd_get_iface(udev, iface->parent_iface_index);
270 if ((iface_parent == NULL) ||
271 (iface_parent->idesc == NULL)) {
272 /* non-existing interface */
275 /* forward request to parent interface, if any */
279 (iface_parent != NULL) &&
280 (iface_parent->subdev != NULL) &&
281 ((req.bmRequestType & 0x1F) == UT_INTERFACE) &&
282 (iface_parent->subdev != iface->subdev) &&
283 device_is_attached(iface_parent->subdev)) {
284 error = USB_HANDLE_REQUEST(iface_parent->subdev,
285 &req, ppdata, plen, off, &temp_state);
288 /* negativly adjust pointer and length */
289 *ppdata = ((uint8_t *)(*ppdata)) - off;
292 if ((state == USB_HR_NOT_COMPLETE) &&
293 (temp_state == USB_HR_COMPLETE_OK))
297 } else if (error == ENOTTY) {
300 if ((req.bmRequestType & 0x1F) != UT_INTERFACE) {
301 iface_index++; /* iterate */
304 if (state != USB_HR_NOT_COMPLETE) {
305 /* we are complete */
308 switch (req.bmRequestType) {
309 case UT_WRITE_INTERFACE:
310 switch (req.bRequest) {
311 case UR_SET_INTERFACE:
313 * We assume that the endpoints are the same
314 * accross the alternate settings.
316 * Reset the endpoints, because re-attaching
317 * only a part of the device is not possible.
319 error = usb_check_alt_setting(udev,
320 iface, req.wValue[0]);
322 DPRINTF("alt setting does not exist %s\n",
326 error = usb_reset_iface_endpoints(udev, iface_index);
328 DPRINTF("alt setting failed %s\n",
332 /* update the current alternate setting */
333 iface->alt_index = req.wValue[0];
341 case UT_READ_INTERFACE:
342 switch (req.bRequest) {
343 case UR_GET_INTERFACE:
344 *ppdata = &iface->alt_index;
356 sx_unlock(udev->default_sx + 1);
362 sx_unlock(udev->default_sx + 1);
365 return (USB_ERR_SHORT_XFER);
368 sx_unlock(udev->default_sx + 1);
371 return (USB_ERR_STALLED);
374 /*------------------------------------------------------------------------*
380 *------------------------------------------------------------------------*/
382 usb_handle_set_stall(struct usb_xfer *xfer, uint8_t ep, uint8_t do_stall)
384 struct usb_device *udev = xfer->xroot->udev;
387 USB_XFER_UNLOCK(xfer);
388 err = usbd_set_endpoint_stall(udev,
389 usbd_get_ep_by_addr(udev, ep), do_stall);
394 /*------------------------------------------------------------------------*
395 * usb_handle_get_stall
400 *------------------------------------------------------------------------*/
402 usb_handle_get_stall(struct usb_device *udev, uint8_t ea_val)
404 struct usb_endpoint *ep;
407 ep = usbd_get_ep_by_addr(udev, ea_val);
412 USB_BUS_LOCK(udev->bus);
413 halted = ep->is_stalled;
414 USB_BUS_UNLOCK(udev->bus);
419 /*------------------------------------------------------------------------*
420 * usb_handle_remote_wakeup
425 *------------------------------------------------------------------------*/
427 usb_handle_remote_wakeup(struct usb_xfer *xfer, uint8_t is_on)
429 struct usb_device *udev;
432 udev = xfer->xroot->udev;
438 udev->flags.remote_wakeup = 1;
440 udev->flags.remote_wakeup = 0;
445 /* In case we are out of sync, update the power state. */
446 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 */
474 uint8_t is_complete = 1;
482 * Filter the USB transfer state into
483 * something which we understand:
486 switch (USB_GET_STATE(xfer)) {
488 state = USB_HR_NOT_COMPLETE;
490 if (!xfer->flags_int.control_act) {
495 case USB_ST_TRANSFERRED:
496 if (!xfer->flags_int.control_act) {
497 state = USB_HR_COMPLETE_OK;
499 state = USB_HR_NOT_COMPLETE;
503 state = USB_HR_COMPLETE_ERR;
507 /* reset frame stuff */
509 usbd_xfer_set_frame_len(xfer, 0, 0);
511 usbd_xfer_set_frame_offset(xfer, 0, 0);
512 usbd_xfer_set_frame_offset(xfer, sizeof(req), 1);
514 /* get the current request, if any */
516 usbd_copy_out(xfer->frbuffers, 0, &req, sizeof(req));
518 if (xfer->flags_int.control_rem == 0xFFFF) {
519 /* first time - not initialised */
520 rem = UGETW(req.wLength);
523 /* not first time - initialised */
524 rem = xfer->flags_int.control_rem;
525 off = UGETW(req.wLength) - rem;
528 /* set some defaults */
533 udev = xfer->xroot->udev;
535 /* get some request fields decoded */
537 wValue = UGETW(req.wValue);
538 wIndex = UGETW(req.wIndex);
540 DPRINTF("req 0x%02x 0x%02x 0x%04x 0x%04x "
541 "off=0x%x rem=0x%x, state=%d\n", req.bmRequestType,
542 req.bRequest, wValue, wIndex, off, rem, state);
544 /* demultiplex the control request */
546 switch (req.bmRequestType) {
548 if (state != USB_HR_NOT_COMPLETE) {
551 switch (req.bRequest) {
552 case UR_GET_DESCRIPTOR:
553 goto tr_handle_get_descriptor;
555 goto tr_handle_get_config;
557 goto tr_handle_get_status;
563 case UT_WRITE_DEVICE:
564 switch (req.bRequest) {
566 goto tr_handle_set_address;
568 goto tr_handle_set_config;
569 case UR_CLEAR_FEATURE:
571 case UF_DEVICE_REMOTE_WAKEUP:
572 goto tr_handle_clear_wakeup;
579 case UF_DEVICE_REMOTE_WAKEUP:
580 goto tr_handle_set_wakeup;
590 case UT_WRITE_ENDPOINT:
591 switch (req.bRequest) {
592 case UR_CLEAR_FEATURE:
594 case UF_ENDPOINT_HALT:
595 goto tr_handle_clear_halt;
602 case UF_ENDPOINT_HALT:
603 goto tr_handle_set_halt;
613 case UT_READ_ENDPOINT:
614 switch (req.bRequest) {
616 goto tr_handle_get_ep_status;
622 /* we use "USB_ADD_BYTES" to de-const the src_zcopy */
623 err = usb_handle_iface_request(xfer,
624 USB_ADD_BYTES(&src_zcopy, 0),
625 &max_len, req, off, state);
629 } else if (err == USB_ERR_SHORT_XFER) {
633 * Reset zero-copy pointer and max length
634 * variable in case they were unintentionally
641 * Check if we have a vendor specific
644 goto tr_handle_get_descriptor;
648 tr_handle_get_descriptor:
649 err = (usb_temp_get_desc_p) (udev, &req, &src_zcopy, &max_len);
652 if (src_zcopy == NULL)
656 tr_handle_get_config:
657 temp.buf[0] = udev->curr_config_no;
658 src_mcopy = temp.buf;
662 tr_handle_get_status:
666 USB_BUS_LOCK(udev->bus);
667 if (udev->flags.remote_wakeup) {
668 wValue |= UDS_REMOTE_WAKEUP;
670 if (udev->flags.self_powered) {
671 wValue |= UDS_SELF_POWERED;
673 USB_BUS_UNLOCK(udev->bus);
675 USETW(temp.wStatus, wValue);
676 src_mcopy = temp.wStatus;
677 max_len = sizeof(temp.wStatus);
680 tr_handle_set_address:
681 if (state == USB_HR_NOT_COMPLETE) {
682 if (wValue >= 0x80) {
685 } else if (udev->curr_config_no != 0) {
686 /* we are configured ! */
689 } else if (state != USB_HR_NOT_COMPLETE) {
690 udev->address = (wValue & 0x7F);
695 tr_handle_set_config:
696 if (state == USB_HR_NOT_COMPLETE) {
697 if (usb_handle_set_config(xfer, req.wValue[0])) {
703 tr_handle_clear_halt:
704 if (state == USB_HR_NOT_COMPLETE) {
705 if (usb_handle_set_stall(xfer, req.wIndex[0], 0)) {
711 tr_handle_clear_wakeup:
712 if (state == USB_HR_NOT_COMPLETE) {
713 if (usb_handle_remote_wakeup(xfer, 0)) {
720 if (state == USB_HR_NOT_COMPLETE) {
721 if (usb_handle_set_stall(xfer, req.wIndex[0], 1)) {
727 tr_handle_set_wakeup:
728 if (state == USB_HR_NOT_COMPLETE) {
729 if (usb_handle_remote_wakeup(xfer, 1)) {
735 tr_handle_get_ep_status:
736 if (state == USB_HR_NOT_COMPLETE) {
738 usb_handle_get_stall(udev, req.wIndex[0]);
740 src_mcopy = temp.wStatus;
741 max_len = sizeof(temp.wStatus);
746 if (state != USB_HR_NOT_COMPLETE) {
749 /* subtract offset from length */
753 /* Compute the real maximum data length */
755 if (max_len > xfer->max_data_length) {
756 max_len = usbd_xfer_max_len(xfer);
762 * If the remainder is greater than the maximum data length,
763 * we need to truncate the value for the sake of the
766 if (rem > xfer->max_data_length) {
767 rem = usbd_xfer_max_len(xfer);
769 if ((rem != max_len) && (is_complete != 0)) {
771 * If we don't transfer the data we can transfer, then
772 * the transfer is short !
774 xfer->flags.force_short_xfer = 1;
780 xfer->flags.force_short_xfer = 0;
781 xfer->nframes = max_len ? 2 : 1;
785 src_mcopy = USB_ADD_BYTES(src_mcopy, off);
786 usbd_copy_in(xfer->frbuffers + 1, 0,
788 usbd_xfer_set_frame_len(xfer, 1, max_len);
790 usbd_xfer_set_frame_data(xfer, 1,
791 USB_ADD_BYTES(src_zcopy, off), max_len);
794 /* the end is reached, send status */
795 xfer->flags.manual_status = 0;
796 usbd_xfer_set_frame_len(xfer, 1, 0);
798 DPRINTF("success\n");
799 return (0); /* success */
802 DPRINTF("%s\n", (state != USB_HR_NOT_COMPLETE) ?
803 "complete" : "stalled");
804 return (USB_ERR_STALLED);
807 DPRINTF("bad context\n");
808 return (USB_ERR_BAD_CONTEXT);