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 #ifdef USB_GLOBAL_INCLUDE_FILE
28 #include USB_GLOBAL_INCLUDE_FILE
30 #include <sys/stdint.h>
31 #include <sys/stddef.h>
32 #include <sys/param.h>
33 #include <sys/queue.h>
34 #include <sys/types.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
38 #include <sys/module.h>
40 #include <sys/mutex.h>
41 #include <sys/condvar.h>
42 #include <sys/sysctl.h>
44 #include <sys/unistd.h>
45 #include <sys/callout.h>
46 #include <sys/malloc.h>
49 #include <dev/usb/usb.h>
50 #include <dev/usb/usbdi.h>
51 #include <dev/usb/usbdi_util.h>
54 #define USB_DEBUG_VAR usb_debug
56 #include <dev/usb/usb_core.h>
57 #include <dev/usb/usb_process.h>
58 #include <dev/usb/usb_busdma.h>
59 #include <dev/usb/usb_transfer.h>
60 #include <dev/usb/usb_device.h>
61 #include <dev/usb/usb_debug.h>
62 #include <dev/usb/usb_dynamic.h>
63 #include <dev/usb/usb_hub.h>
65 #include <dev/usb/usb_controller.h>
66 #include <dev/usb/usb_bus.h>
67 #endif /* USB_GLOBAL_INCLUDE_FILE */
69 /* function prototypes */
71 static uint8_t usb_handle_get_stall(struct usb_device *, uint8_t);
72 static usb_error_t usb_handle_remote_wakeup(struct usb_xfer *, uint8_t);
73 static usb_error_t usb_handle_request(struct usb_xfer *);
74 static usb_error_t usb_handle_set_config(struct usb_xfer *, uint8_t);
75 static usb_error_t usb_handle_set_stall(struct usb_xfer *, uint8_t,
77 static usb_error_t usb_handle_iface_request(struct usb_xfer *, void **,
78 uint16_t *, struct usb_device_request, uint16_t,
81 /*------------------------------------------------------------------------*
82 * usb_handle_request_callback
84 * This function is the USB callback for generic USB Device control
86 *------------------------------------------------------------------------*/
88 usb_handle_request_callback(struct usb_xfer *xfer, usb_error_t error)
92 /* check the current transfer state */
94 switch (USB_GET_STATE(xfer)) {
96 case USB_ST_TRANSFERRED:
98 /* handle the request */
99 err = usb_handle_request(xfer);
103 if (err == USB_ERR_BAD_CONTEXT) {
104 /* we need to re-setup the control transfer */
105 usb_needs_explore(xfer->xroot->bus, 0);
110 usbd_transfer_submit(xfer);
114 /* check if a control transfer is active */
115 if (xfer->flags_int.control_rem != 0xFFFF) {
116 /* handle the request */
117 err = usb_handle_request(xfer);
119 if (xfer->error != USB_ERR_CANCELLED) {
120 /* should not happen - try stalling */
129 * If a control transfer is active, stall it, and wait for the
130 * next control transfer.
132 usbd_xfer_set_frame_len(xfer, 0, sizeof(struct usb_device_request));
134 xfer->flags.manual_status = 1;
135 xfer->flags.force_short_xfer = 0;
136 usbd_xfer_set_stall(xfer); /* cancel previous transfer, if any */
137 usbd_transfer_submit(xfer);
140 /*------------------------------------------------------------------------*
141 * usb_handle_set_config
146 *------------------------------------------------------------------------*/
148 usb_handle_set_config(struct usb_xfer *xfer, uint8_t conf_no)
150 struct usb_device *udev = xfer->xroot->udev;
155 * We need to protect against other threads doing probe and
158 USB_XFER_UNLOCK(xfer);
160 /* Prevent re-enumeration */
161 do_unlock = usbd_enum_lock(udev);
163 if (conf_no == USB_UNCONFIG_NO) {
164 conf_no = USB_UNCONFIG_INDEX;
167 * The relationship between config number and config index
168 * is very simple in our case:
173 if (usbd_set_config_index(udev, conf_no)) {
174 DPRINTF("set config %d failed\n", conf_no);
175 err = USB_ERR_STALLED;
178 if (usb_probe_and_attach(udev, USB_IFACE_INDEX_ANY)) {
179 DPRINTF("probe and attach failed\n");
180 err = USB_ERR_STALLED;
185 usbd_enum_unlock(udev);
191 usb_check_alt_setting(struct usb_device *udev,
192 struct usb_interface *iface, uint8_t alt_index)
197 /* Prevent re-enumeration */
198 do_unlock = usbd_enum_lock(udev);
200 if (alt_index >= usbd_get_no_alts(udev->cdesc, iface->idesc))
204 usbd_enum_unlock(udev);
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;
229 if ((req.bmRequestType & 0x1F) == UT_INTERFACE) {
230 iface_index = req.wIndex[0]; /* unicast */
232 iface_index = 0; /* broadcast */
236 * We need to protect against other threads doing probe and
239 USB_XFER_UNLOCK(xfer);
241 /* Prevent re-enumeration */
242 do_unlock = usbd_enum_lock(udev);
247 iface = usbd_get_iface(udev, iface_index);
248 if ((iface == NULL) ||
249 (iface->idesc == NULL)) {
250 /* end of interfaces non-existing interface */
253 /* set initial state */
257 /* forward request to interface, if any */
261 (iface->subdev != NULL) &&
262 device_is_attached(iface->subdev)) {
264 DEVMETHOD(usb_handle_request, NULL); /* dummy */
266 error = USB_HANDLE_REQUEST(iface->subdev,
270 iface_parent = usbd_get_iface(udev, iface->parent_iface_index);
272 if ((iface_parent == NULL) ||
273 (iface_parent->idesc == NULL)) {
274 /* non-existing interface */
277 /* forward request to parent interface, if any */
281 (iface_parent != NULL) &&
282 (iface_parent->subdev != NULL) &&
283 ((req.bmRequestType & 0x1F) == UT_INTERFACE) &&
284 (iface_parent->subdev != iface->subdev) &&
285 device_is_attached(iface_parent->subdev)) {
286 error = USB_HANDLE_REQUEST(iface_parent->subdev,
287 &req, ppdata, plen, off, &temp_state);
290 /* negativly adjust pointer and length */
291 *ppdata = ((uint8_t *)(*ppdata)) - off;
294 if ((state == USB_HR_NOT_COMPLETE) &&
295 (temp_state == USB_HR_COMPLETE_OK))
299 } else if (error == ENOTTY) {
302 if ((req.bmRequestType & 0x1F) != UT_INTERFACE) {
303 iface_index++; /* iterate */
306 if (state != USB_HR_NOT_COMPLETE) {
307 /* we are complete */
310 switch (req.bmRequestType) {
311 case UT_WRITE_INTERFACE:
312 switch (req.bRequest) {
313 case UR_SET_INTERFACE:
315 * We assume that the endpoints are the same
316 * accross the alternate settings.
318 * Reset the endpoints, because re-attaching
319 * only a part of the device is not possible.
321 error = usb_check_alt_setting(udev,
322 iface, req.wValue[0]);
324 DPRINTF("alt setting does not exist %s\n",
328 error = usb_reset_iface_endpoints(udev, iface_index);
330 DPRINTF("alt setting failed %s\n",
334 /* update the current alternate setting */
335 iface->alt_index = req.wValue[0];
343 case UT_READ_INTERFACE:
344 switch (req.bRequest) {
345 case UR_GET_INTERFACE:
346 *ppdata = &iface->alt_index;
359 usbd_enum_unlock(udev);
365 usbd_enum_unlock(udev);
367 return (USB_ERR_SHORT_XFER);
371 usbd_enum_unlock(udev);
373 return (USB_ERR_STALLED);
376 /*------------------------------------------------------------------------*
382 *------------------------------------------------------------------------*/
384 usb_handle_set_stall(struct usb_xfer *xfer, uint8_t ep, uint8_t do_stall)
386 struct usb_device *udev = xfer->xroot->udev;
389 USB_XFER_UNLOCK(xfer);
390 err = usbd_set_endpoint_stall(udev,
391 usbd_get_ep_by_addr(udev, ep), do_stall);
396 /*------------------------------------------------------------------------*
397 * usb_handle_get_stall
402 *------------------------------------------------------------------------*/
404 usb_handle_get_stall(struct usb_device *udev, uint8_t ea_val)
406 struct usb_endpoint *ep;
409 ep = usbd_get_ep_by_addr(udev, ea_val);
414 USB_BUS_LOCK(udev->bus);
415 halted = ep->is_stalled;
416 USB_BUS_UNLOCK(udev->bus);
421 /*------------------------------------------------------------------------*
422 * usb_handle_remote_wakeup
427 *------------------------------------------------------------------------*/
429 usb_handle_remote_wakeup(struct usb_xfer *xfer, uint8_t is_on)
431 struct usb_device *udev;
434 udev = xfer->xroot->udev;
440 udev->flags.remote_wakeup = 1;
442 udev->flags.remote_wakeup = 0;
448 /* In case we are out of sync, update the power state. */
449 usb_bus_power_update(udev->bus);
451 return (0); /* success */
454 /*------------------------------------------------------------------------*
457 * Internal state sequence:
459 * USB_HR_NOT_COMPLETE -> USB_HR_COMPLETE_OK v USB_HR_COMPLETE_ERR
462 * 0: Ready to start hardware
463 * Else: Stall current transfer, if any
464 *------------------------------------------------------------------------*/
466 usb_handle_request(struct usb_xfer *xfer)
468 struct usb_device_request req;
469 struct usb_device *udev;
470 const void *src_zcopy; /* zero-copy source pointer */
471 const void *src_mcopy; /* non zero-copy source pointer */
472 uint16_t off; /* data offset */
473 uint16_t rem; /* data remainder */
474 uint16_t max_len; /* max fragment length */
477 uint8_t is_complete = 1;
485 * Filter the USB transfer state into
486 * something which we understand:
489 switch (USB_GET_STATE(xfer)) {
491 state = USB_HR_NOT_COMPLETE;
493 if (!xfer->flags_int.control_act) {
498 case USB_ST_TRANSFERRED:
499 if (!xfer->flags_int.control_act) {
500 state = USB_HR_COMPLETE_OK;
502 state = USB_HR_NOT_COMPLETE;
506 state = USB_HR_COMPLETE_ERR;
510 /* reset frame stuff */
512 usbd_xfer_set_frame_len(xfer, 0, 0);
514 usbd_xfer_set_frame_offset(xfer, 0, 0);
515 usbd_xfer_set_frame_offset(xfer, sizeof(req), 1);
517 /* get the current request, if any */
519 usbd_copy_out(xfer->frbuffers, 0, &req, sizeof(req));
521 if (xfer->flags_int.control_rem == 0xFFFF) {
522 /* first time - not initialised */
523 rem = UGETW(req.wLength);
526 /* not first time - initialised */
527 rem = xfer->flags_int.control_rem;
528 off = UGETW(req.wLength) - rem;
531 /* set some defaults */
536 udev = xfer->xroot->udev;
538 /* get some request fields decoded */
540 wValue = UGETW(req.wValue);
542 DPRINTF("req 0x%02x 0x%02x 0x%04x 0x%04x "
543 "off=0x%x rem=0x%x, state=%d\n", req.bmRequestType,
544 req.bRequest, wValue, UGETW(req.wIndex), off, rem, state);
546 /* demultiplex the control request */
548 switch (req.bmRequestType) {
550 if (state != USB_HR_NOT_COMPLETE) {
553 switch (req.bRequest) {
554 case UR_GET_DESCRIPTOR:
555 goto tr_handle_get_descriptor;
557 goto tr_handle_get_config;
559 goto tr_handle_get_status;
565 case UT_WRITE_DEVICE:
566 switch (req.bRequest) {
568 goto tr_handle_set_address;
570 goto tr_handle_set_config;
571 case UR_CLEAR_FEATURE:
573 case UF_DEVICE_REMOTE_WAKEUP:
574 goto tr_handle_clear_wakeup;
581 case UF_DEVICE_REMOTE_WAKEUP:
582 goto tr_handle_set_wakeup;
592 case UT_WRITE_ENDPOINT:
593 switch (req.bRequest) {
594 case UR_CLEAR_FEATURE:
596 case UF_ENDPOINT_HALT:
597 goto tr_handle_clear_halt;
604 case UF_ENDPOINT_HALT:
605 goto tr_handle_set_halt;
615 case UT_READ_ENDPOINT:
616 switch (req.bRequest) {
618 goto tr_handle_get_ep_status;
624 /* we use "USB_ADD_BYTES" to de-const the src_zcopy */
625 err = usb_handle_iface_request(xfer,
626 USB_ADD_BYTES(&src_zcopy, 0),
627 &max_len, req, off, state);
631 } else if (err == USB_ERR_SHORT_XFER) {
635 * Reset zero-copy pointer and max length
636 * variable in case they were unintentionally
643 * Check if we have a vendor specific
646 goto tr_handle_get_descriptor;
650 tr_handle_get_descriptor:
651 err = (usb_temp_get_desc_p) (udev, &req, &src_zcopy, &max_len);
654 if (src_zcopy == NULL)
658 tr_handle_get_config:
659 temp.buf[0] = udev->curr_config_no;
660 src_mcopy = temp.buf;
664 tr_handle_get_status:
668 USB_BUS_LOCK(udev->bus);
669 if (udev->flags.remote_wakeup) {
670 wValue |= UDS_REMOTE_WAKEUP;
672 if (udev->flags.self_powered) {
673 wValue |= UDS_SELF_POWERED;
675 USB_BUS_UNLOCK(udev->bus);
677 USETW(temp.wStatus, wValue);
678 src_mcopy = temp.wStatus;
679 max_len = sizeof(temp.wStatus);
682 tr_handle_set_address:
683 if (state == USB_HR_NOT_COMPLETE) {
684 if (wValue >= 0x80) {
687 } else if (udev->curr_config_no != 0) {
688 /* we are configured ! */
691 } else if (state != USB_HR_NOT_COMPLETE) {
692 udev->address = (wValue & 0x7F);
697 tr_handle_set_config:
698 if (state == USB_HR_NOT_COMPLETE) {
699 if (usb_handle_set_config(xfer, req.wValue[0])) {
705 tr_handle_clear_halt:
706 if (state == USB_HR_NOT_COMPLETE) {
707 if (usb_handle_set_stall(xfer, req.wIndex[0], 0)) {
713 tr_handle_clear_wakeup:
714 if (state == USB_HR_NOT_COMPLETE) {
715 if (usb_handle_remote_wakeup(xfer, 0)) {
722 if (state == USB_HR_NOT_COMPLETE) {
723 if (usb_handle_set_stall(xfer, req.wIndex[0], 1)) {
729 tr_handle_set_wakeup:
730 if (state == USB_HR_NOT_COMPLETE) {
731 if (usb_handle_remote_wakeup(xfer, 1)) {
737 tr_handle_get_ep_status:
738 if (state == USB_HR_NOT_COMPLETE) {
740 usb_handle_get_stall(udev, req.wIndex[0]);
742 src_mcopy = temp.wStatus;
743 max_len = sizeof(temp.wStatus);
748 if (state != USB_HR_NOT_COMPLETE) {
751 /* subtract offset from length */
755 /* Compute the real maximum data length */
757 if (max_len > xfer->max_data_length) {
758 max_len = usbd_xfer_max_len(xfer);
764 * If the remainder is greater than the maximum data length,
765 * we need to truncate the value for the sake of the
768 if (rem > xfer->max_data_length) {
769 rem = usbd_xfer_max_len(xfer);
771 if ((rem != max_len) && (is_complete != 0)) {
773 * If we don't transfer the data we can transfer, then
774 * the transfer is short !
776 xfer->flags.force_short_xfer = 1;
782 xfer->flags.force_short_xfer = 0;
783 xfer->nframes = max_len ? 2 : 1;
787 src_mcopy = USB_ADD_BYTES(src_mcopy, off);
788 usbd_copy_in(xfer->frbuffers + 1, 0,
790 usbd_xfer_set_frame_len(xfer, 1, max_len);
792 usbd_xfer_set_frame_data(xfer, 1,
793 USB_ADD_BYTES(src_zcopy, off), max_len);
796 /* the end is reached, send status */
797 xfer->flags.manual_status = 0;
798 usbd_xfer_set_frame_len(xfer, 1, 0);
800 DPRINTF("success\n");
801 return (0); /* success */
804 DPRINTF("%s\n", (state != USB_HR_NOT_COMPLETE) ?
805 "complete" : "stalled");
806 return (USB_ERR_STALLED);
809 DPRINTF("bad context\n");
810 return (USB_ERR_BAD_CONTEXT);