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;
150 * We need to protect against other threads doing probe and
153 USB_XFER_UNLOCK(xfer);
155 usbd_enum_lock(udev);
157 if (conf_no == USB_UNCONFIG_NO) {
158 conf_no = USB_UNCONFIG_INDEX;
161 * The relationship between config number and config index
162 * is very simple in our case:
167 if (usbd_set_config_index(udev, conf_no)) {
168 DPRINTF("set config %d failed\n", conf_no);
169 err = USB_ERR_STALLED;
172 if (usb_probe_and_attach(udev, USB_IFACE_INDEX_ANY)) {
173 DPRINTF("probe and attach failed\n");
174 err = USB_ERR_STALLED;
178 usbd_enum_unlock(udev);
184 usb_check_alt_setting(struct usb_device *udev,
185 struct usb_interface *iface, uint8_t alt_index)
190 /* automatic locking */
191 if (usbd_enum_is_locked(udev)) {
195 usbd_enum_lock(udev);
198 if (alt_index >= usbd_get_no_alts(udev->cdesc, iface->idesc))
202 usbd_enum_unlock(udev);
207 /*------------------------------------------------------------------------*
208 * usb_handle_iface_request
213 *------------------------------------------------------------------------*/
215 usb_handle_iface_request(struct usb_xfer *xfer,
216 void **ppdata, uint16_t *plen,
217 struct usb_device_request req, uint16_t off, uint8_t state)
219 struct usb_interface *iface;
220 struct usb_interface *iface_parent; /* parent interface */
221 struct usb_device *udev = xfer->xroot->udev;
226 if ((req.bmRequestType & 0x1F) == UT_INTERFACE) {
227 iface_index = req.wIndex[0]; /* unicast */
229 iface_index = 0; /* broadcast */
233 * We need to protect against other threads doing probe and
236 USB_XFER_UNLOCK(xfer);
238 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;
354 usbd_enum_unlock(udev);
359 usbd_enum_unlock(udev);
361 return (USB_ERR_SHORT_XFER);
364 usbd_enum_unlock(udev);
366 return (USB_ERR_STALLED);
369 /*------------------------------------------------------------------------*
375 *------------------------------------------------------------------------*/
377 usb_handle_set_stall(struct usb_xfer *xfer, uint8_t ep, uint8_t do_stall)
379 struct usb_device *udev = xfer->xroot->udev;
382 USB_XFER_UNLOCK(xfer);
383 err = usbd_set_endpoint_stall(udev,
384 usbd_get_ep_by_addr(udev, ep), do_stall);
389 /*------------------------------------------------------------------------*
390 * usb_handle_get_stall
395 *------------------------------------------------------------------------*/
397 usb_handle_get_stall(struct usb_device *udev, uint8_t ea_val)
399 struct usb_endpoint *ep;
402 ep = usbd_get_ep_by_addr(udev, ea_val);
407 USB_BUS_LOCK(udev->bus);
408 halted = ep->is_stalled;
409 USB_BUS_UNLOCK(udev->bus);
414 /*------------------------------------------------------------------------*
415 * usb_handle_remote_wakeup
420 *------------------------------------------------------------------------*/
422 usb_handle_remote_wakeup(struct usb_xfer *xfer, uint8_t is_on)
424 struct usb_device *udev;
427 udev = xfer->xroot->udev;
433 udev->flags.remote_wakeup = 1;
435 udev->flags.remote_wakeup = 0;
441 /* In case we are out of sync, update the power state. */
442 usb_bus_power_update(udev->bus);
444 return (0); /* success */
447 /*------------------------------------------------------------------------*
450 * Internal state sequence:
452 * USB_HR_NOT_COMPLETE -> USB_HR_COMPLETE_OK v USB_HR_COMPLETE_ERR
455 * 0: Ready to start hardware
456 * Else: Stall current transfer, if any
457 *------------------------------------------------------------------------*/
459 usb_handle_request(struct usb_xfer *xfer)
461 struct usb_device_request req;
462 struct usb_device *udev;
463 const void *src_zcopy; /* zero-copy source pointer */
464 const void *src_mcopy; /* non zero-copy source pointer */
465 uint16_t off; /* data offset */
466 uint16_t rem; /* data remainder */
467 uint16_t max_len; /* max fragment length */
471 uint8_t is_complete = 1;
479 * Filter the USB transfer state into
480 * something which we understand:
483 switch (USB_GET_STATE(xfer)) {
485 state = USB_HR_NOT_COMPLETE;
487 if (!xfer->flags_int.control_act) {
492 case USB_ST_TRANSFERRED:
493 if (!xfer->flags_int.control_act) {
494 state = USB_HR_COMPLETE_OK;
496 state = USB_HR_NOT_COMPLETE;
500 state = USB_HR_COMPLETE_ERR;
504 /* reset frame stuff */
506 usbd_xfer_set_frame_len(xfer, 0, 0);
508 usbd_xfer_set_frame_offset(xfer, 0, 0);
509 usbd_xfer_set_frame_offset(xfer, sizeof(req), 1);
511 /* get the current request, if any */
513 usbd_copy_out(xfer->frbuffers, 0, &req, sizeof(req));
515 if (xfer->flags_int.control_rem == 0xFFFF) {
516 /* first time - not initialised */
517 rem = UGETW(req.wLength);
520 /* not first time - initialised */
521 rem = xfer->flags_int.control_rem;
522 off = UGETW(req.wLength) - rem;
525 /* set some defaults */
530 udev = xfer->xroot->udev;
532 /* get some request fields decoded */
534 wValue = UGETW(req.wValue);
535 wIndex = UGETW(req.wIndex);
537 DPRINTF("req 0x%02x 0x%02x 0x%04x 0x%04x "
538 "off=0x%x rem=0x%x, state=%d\n", req.bmRequestType,
539 req.bRequest, wValue, wIndex, off, rem, state);
541 /* demultiplex the control request */
543 switch (req.bmRequestType) {
545 if (state != USB_HR_NOT_COMPLETE) {
548 switch (req.bRequest) {
549 case UR_GET_DESCRIPTOR:
550 goto tr_handle_get_descriptor;
552 goto tr_handle_get_config;
554 goto tr_handle_get_status;
560 case UT_WRITE_DEVICE:
561 switch (req.bRequest) {
563 goto tr_handle_set_address;
565 goto tr_handle_set_config;
566 case UR_CLEAR_FEATURE:
568 case UF_DEVICE_REMOTE_WAKEUP:
569 goto tr_handle_clear_wakeup;
576 case UF_DEVICE_REMOTE_WAKEUP:
577 goto tr_handle_set_wakeup;
587 case UT_WRITE_ENDPOINT:
588 switch (req.bRequest) {
589 case UR_CLEAR_FEATURE:
591 case UF_ENDPOINT_HALT:
592 goto tr_handle_clear_halt;
599 case UF_ENDPOINT_HALT:
600 goto tr_handle_set_halt;
610 case UT_READ_ENDPOINT:
611 switch (req.bRequest) {
613 goto tr_handle_get_ep_status;
619 /* we use "USB_ADD_BYTES" to de-const the src_zcopy */
620 err = usb_handle_iface_request(xfer,
621 USB_ADD_BYTES(&src_zcopy, 0),
622 &max_len, req, off, state);
626 } else if (err == USB_ERR_SHORT_XFER) {
630 * Reset zero-copy pointer and max length
631 * variable in case they were unintentionally
638 * Check if we have a vendor specific
641 goto tr_handle_get_descriptor;
645 tr_handle_get_descriptor:
646 err = (usb_temp_get_desc_p) (udev, &req, &src_zcopy, &max_len);
649 if (src_zcopy == NULL)
653 tr_handle_get_config:
654 temp.buf[0] = udev->curr_config_no;
655 src_mcopy = temp.buf;
659 tr_handle_get_status:
663 USB_BUS_LOCK(udev->bus);
664 if (udev->flags.remote_wakeup) {
665 wValue |= UDS_REMOTE_WAKEUP;
667 if (udev->flags.self_powered) {
668 wValue |= UDS_SELF_POWERED;
670 USB_BUS_UNLOCK(udev->bus);
672 USETW(temp.wStatus, wValue);
673 src_mcopy = temp.wStatus;
674 max_len = sizeof(temp.wStatus);
677 tr_handle_set_address:
678 if (state == USB_HR_NOT_COMPLETE) {
679 if (wValue >= 0x80) {
682 } else if (udev->curr_config_no != 0) {
683 /* we are configured ! */
686 } else if (state != USB_HR_NOT_COMPLETE) {
687 udev->address = (wValue & 0x7F);
692 tr_handle_set_config:
693 if (state == USB_HR_NOT_COMPLETE) {
694 if (usb_handle_set_config(xfer, req.wValue[0])) {
700 tr_handle_clear_halt:
701 if (state == USB_HR_NOT_COMPLETE) {
702 if (usb_handle_set_stall(xfer, req.wIndex[0], 0)) {
708 tr_handle_clear_wakeup:
709 if (state == USB_HR_NOT_COMPLETE) {
710 if (usb_handle_remote_wakeup(xfer, 0)) {
717 if (state == USB_HR_NOT_COMPLETE) {
718 if (usb_handle_set_stall(xfer, req.wIndex[0], 1)) {
724 tr_handle_set_wakeup:
725 if (state == USB_HR_NOT_COMPLETE) {
726 if (usb_handle_remote_wakeup(xfer, 1)) {
732 tr_handle_get_ep_status:
733 if (state == USB_HR_NOT_COMPLETE) {
735 usb_handle_get_stall(udev, req.wIndex[0]);
737 src_mcopy = temp.wStatus;
738 max_len = sizeof(temp.wStatus);
743 if (state != USB_HR_NOT_COMPLETE) {
746 /* subtract offset from length */
750 /* Compute the real maximum data length */
752 if (max_len > xfer->max_data_length) {
753 max_len = usbd_xfer_max_len(xfer);
759 * If the remainder is greater than the maximum data length,
760 * we need to truncate the value for the sake of the
763 if (rem > xfer->max_data_length) {
764 rem = usbd_xfer_max_len(xfer);
766 if ((rem != max_len) && (is_complete != 0)) {
768 * If we don't transfer the data we can transfer, then
769 * the transfer is short !
771 xfer->flags.force_short_xfer = 1;
777 xfer->flags.force_short_xfer = 0;
778 xfer->nframes = max_len ? 2 : 1;
782 src_mcopy = USB_ADD_BYTES(src_mcopy, off);
783 usbd_copy_in(xfer->frbuffers + 1, 0,
785 usbd_xfer_set_frame_len(xfer, 1, max_len);
787 usbd_xfer_set_frame_data(xfer, 1,
788 USB_ADD_BYTES(src_zcopy, off), max_len);
791 /* the end is reached, send status */
792 xfer->flags.manual_status = 0;
793 usbd_xfer_set_frame_len(xfer, 1, 0);
795 DPRINTF("success\n");
796 return (0); /* success */
799 DPRINTF("%s\n", (state != USB_HR_NOT_COMPLETE) ?
800 "complete" : "stalled");
801 return (USB_ERR_STALLED);
804 DPRINTF("bad context\n");
805 return (USB_ERR_BAD_CONTEXT);