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/param.h>
29 #include <sys/queue.h>
30 #include <sys/types.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
34 #include <sys/module.h>
36 #include <sys/mutex.h>
37 #include <sys/condvar.h>
38 #include <sys/sysctl.h>
39 #include <sys/unistd.h>
40 #include <sys/callout.h>
41 #include <sys/malloc.h>
44 #include <bus/u4b/usb.h>
45 #include <bus/u4b/usbdi.h>
46 #include <bus/u4b/usbdi_util.h>
49 #define USB_DEBUG_VAR usb_debug
51 #include <bus/u4b/usb_core.h>
52 #include <bus/u4b/usb_process.h>
53 #include <bus/u4b/usb_busdma.h>
54 #include <bus/u4b/usb_transfer.h>
55 #include <bus/u4b/usb_device.h>
56 #include <bus/u4b/usb_debug.h>
57 #include <bus/u4b/usb_dynamic.h>
58 #include <bus/u4b/usb_hub.h>
60 #include <bus/u4b/usb_controller.h>
61 #include <bus/u4b/usb_bus.h>
63 /* function prototypes */
65 static uint8_t usb_handle_get_stall(struct usb_device *, uint8_t);
66 static usb_error_t usb_handle_remote_wakeup(struct usb_xfer *, uint8_t);
67 static usb_error_t usb_handle_request(struct usb_xfer *);
68 static usb_error_t usb_handle_set_config(struct usb_xfer *, uint8_t);
69 static usb_error_t usb_handle_set_stall(struct usb_xfer *, uint8_t,
71 static usb_error_t usb_handle_iface_request(struct usb_xfer *, void **,
72 uint16_t *, struct usb_device_request, uint16_t,
75 /*------------------------------------------------------------------------*
76 * usb_handle_request_callback
78 * This function is the USB callback for generic USB Device control
80 *------------------------------------------------------------------------*/
82 usb_handle_request_callback(struct usb_xfer *xfer, usb_error_t error)
86 /* check the current transfer state */
88 switch (USB_GET_STATE(xfer)) {
90 case USB_ST_TRANSFERRED:
92 /* handle the request */
93 err = usb_handle_request(xfer);
97 if (err == USB_ERR_BAD_CONTEXT) {
98 /* we need to re-setup the control transfer */
99 usb_needs_explore(xfer->xroot->bus, 0);
104 usbd_transfer_submit(xfer);
108 /* check if a control transfer is active */
109 if (xfer->flags_int.control_rem != 0xFFFF) {
110 /* handle the request */
111 err = usb_handle_request(xfer);
113 if (xfer->error != USB_ERR_CANCELLED) {
114 /* should not happen - try stalling */
123 * If a control transfer is active, stall it, and wait for the
124 * next control transfer.
126 usbd_xfer_set_frame_len(xfer, 0, sizeof(struct usb_device_request));
128 xfer->flags.manual_status = 1;
129 xfer->flags.force_short_xfer = 0;
130 usbd_xfer_set_stall(xfer); /* cancel previous transfer, if any */
131 usbd_transfer_submit(xfer);
134 /*------------------------------------------------------------------------*
135 * usb_handle_set_config
140 *------------------------------------------------------------------------*/
142 usb_handle_set_config(struct usb_xfer *xfer, uint8_t conf_no)
144 struct usb_device *udev = xfer->xroot->udev;
148 * We need to protect against other threads doing probe and
151 USB_XFER_UNLOCK(xfer);
153 usbd_enum_lock(udev);
155 if (conf_no == USB_UNCONFIG_NO) {
156 conf_no = USB_UNCONFIG_INDEX;
159 * The relationship between config number and config index
160 * is very simple in our case:
165 if (usbd_set_config_index(udev, conf_no)) {
166 DPRINTF("set config %d failed\n", conf_no);
167 err = USB_ERR_STALLED;
170 if (usb_probe_and_attach(udev, USB_IFACE_INDEX_ANY)) {
171 DPRINTF("probe and attach failed\n");
172 err = USB_ERR_STALLED;
176 usbd_enum_unlock(udev);
182 usb_check_alt_setting(struct usb_device *udev,
183 struct usb_interface *iface, uint8_t alt_index)
188 /* automatic locking */
189 if (usbd_enum_is_locked(udev)) {
193 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;
224 if ((req.bmRequestType & 0x1F) == UT_INTERFACE) {
225 iface_index = req.wIndex[0]; /* unicast */
227 iface_index = 0; /* broadcast */
231 * We need to protect against other threads doing probe and
234 USB_XFER_UNLOCK(xfer);
236 usbd_enum_lock(udev);
241 iface = usbd_get_iface(udev, iface_index);
242 if ((iface == NULL) ||
243 (iface->idesc == NULL)) {
244 /* end of interfaces non-existing interface */
247 /* set initial state */
251 /* forward request to interface, if any */
255 (iface->subdev != NULL) &&
256 device_is_attached(iface->subdev)) {
258 DEVMETHOD(usb_handle_request, NULL); /* dummy */
260 error = USB_HANDLE_REQUEST(iface->subdev,
264 iface_parent = usbd_get_iface(udev, iface->parent_iface_index);
266 if ((iface_parent == NULL) ||
267 (iface_parent->idesc == NULL)) {
268 /* non-existing interface */
271 /* forward request to parent interface, if any */
275 (iface_parent != NULL) &&
276 (iface_parent->subdev != NULL) &&
277 ((req.bmRequestType & 0x1F) == UT_INTERFACE) &&
278 (iface_parent->subdev != iface->subdev) &&
279 device_is_attached(iface_parent->subdev)) {
280 error = USB_HANDLE_REQUEST(iface_parent->subdev,
281 &req, ppdata, plen, off, &temp_state);
284 /* negativly adjust pointer and length */
285 *ppdata = ((uint8_t *)(*ppdata)) - off;
288 if ((state == USB_HR_NOT_COMPLETE) &&
289 (temp_state == USB_HR_COMPLETE_OK))
293 } else if (error == ENOTTY) {
296 if ((req.bmRequestType & 0x1F) != UT_INTERFACE) {
297 iface_index++; /* iterate */
300 if (state != USB_HR_NOT_COMPLETE) {
301 /* we are complete */
304 switch (req.bmRequestType) {
305 case UT_WRITE_INTERFACE:
306 switch (req.bRequest) {
307 case UR_SET_INTERFACE:
309 * We assume that the endpoints are the same
310 * accross the alternate settings.
312 * Reset the endpoints, because re-attaching
313 * only a part of the device is not possible.
315 error = usb_check_alt_setting(udev,
316 iface, req.wValue[0]);
318 DPRINTF("alt setting does not exist %s\n",
322 error = usb_reset_iface_endpoints(udev, iface_index);
324 DPRINTF("alt setting failed %s\n",
328 /* update the current alternate setting */
329 iface->alt_index = req.wValue[0];
337 case UT_READ_INTERFACE:
338 switch (req.bRequest) {
339 case UR_GET_INTERFACE:
340 *ppdata = &iface->alt_index;
352 usbd_enum_unlock(udev);
357 usbd_enum_unlock(udev);
359 return (USB_ERR_SHORT_XFER);
362 usbd_enum_unlock(udev);
364 return (USB_ERR_STALLED);
367 /*------------------------------------------------------------------------*
373 *------------------------------------------------------------------------*/
375 usb_handle_set_stall(struct usb_xfer *xfer, uint8_t ep, uint8_t do_stall)
377 struct usb_device *udev = xfer->xroot->udev;
380 USB_XFER_UNLOCK(xfer);
381 err = usbd_set_endpoint_stall(udev,
382 usbd_get_ep_by_addr(udev, ep), do_stall);
387 /*------------------------------------------------------------------------*
388 * usb_handle_get_stall
393 *------------------------------------------------------------------------*/
395 usb_handle_get_stall(struct usb_device *udev, uint8_t ea_val)
397 struct usb_endpoint *ep;
400 ep = usbd_get_ep_by_addr(udev, ea_val);
405 USB_BUS_LOCK(udev->bus);
406 halted = ep->is_stalled;
407 USB_BUS_UNLOCK(udev->bus);
412 /*------------------------------------------------------------------------*
413 * usb_handle_remote_wakeup
418 *------------------------------------------------------------------------*/
420 usb_handle_remote_wakeup(struct usb_xfer *xfer, uint8_t is_on)
422 struct usb_device *udev;
425 udev = xfer->xroot->udev;
431 udev->flags.remote_wakeup = 1;
433 udev->flags.remote_wakeup = 0;
439 /* In case we are out of sync, update the power state. */
440 usb_bus_power_update(udev->bus);
442 return (0); /* success */
445 /*------------------------------------------------------------------------*
448 * Internal state sequence:
450 * USB_HR_NOT_COMPLETE -> USB_HR_COMPLETE_OK v USB_HR_COMPLETE_ERR
453 * 0: Ready to start hardware
454 * Else: Stall current transfer, if any
455 *------------------------------------------------------------------------*/
457 usb_handle_request(struct usb_xfer *xfer)
459 struct usb_device_request req;
460 struct usb_device *udev;
461 const void *src_zcopy; /* zero-copy source pointer */
462 const void *src_mcopy; /* non zero-copy source pointer */
463 uint16_t off; /* data offset */
464 uint16_t rem; /* data remainder */
465 uint16_t max_len; /* max fragment length */
469 uint8_t is_complete = 1;
477 * Filter the USB transfer state into
478 * something which we understand:
481 switch (USB_GET_STATE(xfer)) {
483 state = USB_HR_NOT_COMPLETE;
485 if (!xfer->flags_int.control_act) {
490 case USB_ST_TRANSFERRED:
491 if (!xfer->flags_int.control_act) {
492 state = USB_HR_COMPLETE_OK;
494 state = USB_HR_NOT_COMPLETE;
498 state = USB_HR_COMPLETE_ERR;
502 /* reset frame stuff */
504 usbd_xfer_set_frame_len(xfer, 0, 0);
506 usbd_xfer_set_frame_offset(xfer, 0, 0);
507 usbd_xfer_set_frame_offset(xfer, sizeof(req), 1);
509 /* get the current request, if any */
511 usbd_copy_out(xfer->frbuffers, 0, &req, sizeof(req));
513 if (xfer->flags_int.control_rem == 0xFFFF) {
514 /* first time - not initialised */
515 rem = UGETW(req.wLength);
518 /* not first time - initialised */
519 rem = xfer->flags_int.control_rem;
520 off = UGETW(req.wLength) - rem;
523 /* set some defaults */
528 udev = xfer->xroot->udev;
530 /* get some request fields decoded */
532 wValue = UGETW(req.wValue);
533 wIndex = UGETW(req.wIndex);
535 DPRINTF("req 0x%02x 0x%02x 0x%04x 0x%04x "
536 "off=0x%x rem=0x%x, state=%d\n", req.bmRequestType,
537 req.bRequest, wValue, wIndex, off, rem, state);
539 /* demultiplex the control request */
541 switch (req.bmRequestType) {
543 if (state != USB_HR_NOT_COMPLETE) {
546 switch (req.bRequest) {
547 case UR_GET_DESCRIPTOR:
548 goto tr_handle_get_descriptor;
550 goto tr_handle_get_config;
552 goto tr_handle_get_status;
558 case UT_WRITE_DEVICE:
559 switch (req.bRequest) {
561 goto tr_handle_set_address;
563 goto tr_handle_set_config;
564 case UR_CLEAR_FEATURE:
566 case UF_DEVICE_REMOTE_WAKEUP:
567 goto tr_handle_clear_wakeup;
574 case UF_DEVICE_REMOTE_WAKEUP:
575 goto tr_handle_set_wakeup;
585 case UT_WRITE_ENDPOINT:
586 switch (req.bRequest) {
587 case UR_CLEAR_FEATURE:
589 case UF_ENDPOINT_HALT:
590 goto tr_handle_clear_halt;
597 case UF_ENDPOINT_HALT:
598 goto tr_handle_set_halt;
608 case UT_READ_ENDPOINT:
609 switch (req.bRequest) {
611 goto tr_handle_get_ep_status;
617 /* we use "USB_ADD_BYTES" to de-const the src_zcopy */
618 err = usb_handle_iface_request(xfer,
619 USB_ADD_BYTES(&src_zcopy, 0),
620 &max_len, req, off, state);
624 } else if (err == USB_ERR_SHORT_XFER) {
628 * Reset zero-copy pointer and max length
629 * variable in case they were unintentionally
636 * Check if we have a vendor specific
639 goto tr_handle_get_descriptor;
643 tr_handle_get_descriptor:
644 err = (usb_temp_get_desc_p) (udev, &req, &src_zcopy, &max_len);
647 if (src_zcopy == NULL)
651 tr_handle_get_config:
652 temp.buf[0] = udev->curr_config_no;
653 src_mcopy = temp.buf;
657 tr_handle_get_status:
661 USB_BUS_LOCK(udev->bus);
662 if (udev->flags.remote_wakeup) {
663 wValue |= UDS_REMOTE_WAKEUP;
665 if (udev->flags.self_powered) {
666 wValue |= UDS_SELF_POWERED;
668 USB_BUS_UNLOCK(udev->bus);
670 USETW(temp.wStatus, wValue);
671 src_mcopy = temp.wStatus;
672 max_len = sizeof(temp.wStatus);
675 tr_handle_set_address:
676 if (state == USB_HR_NOT_COMPLETE) {
677 if (wValue >= 0x80) {
680 } else if (udev->curr_config_no != 0) {
681 /* we are configured ! */
684 } else if (state != USB_HR_NOT_COMPLETE) {
685 udev->address = (wValue & 0x7F);
690 tr_handle_set_config:
691 if (state == USB_HR_NOT_COMPLETE) {
692 if (usb_handle_set_config(xfer, req.wValue[0])) {
698 tr_handle_clear_halt:
699 if (state == USB_HR_NOT_COMPLETE) {
700 if (usb_handle_set_stall(xfer, req.wIndex[0], 0)) {
706 tr_handle_clear_wakeup:
707 if (state == USB_HR_NOT_COMPLETE) {
708 if (usb_handle_remote_wakeup(xfer, 0)) {
715 if (state == USB_HR_NOT_COMPLETE) {
716 if (usb_handle_set_stall(xfer, req.wIndex[0], 1)) {
722 tr_handle_set_wakeup:
723 if (state == USB_HR_NOT_COMPLETE) {
724 if (usb_handle_remote_wakeup(xfer, 1)) {
730 tr_handle_get_ep_status:
731 if (state == USB_HR_NOT_COMPLETE) {
733 usb_handle_get_stall(udev, req.wIndex[0]);
735 src_mcopy = temp.wStatus;
736 max_len = sizeof(temp.wStatus);
741 if (state != USB_HR_NOT_COMPLETE) {
744 /* subtract offset from length */
748 /* Compute the real maximum data length */
750 if (max_len > xfer->max_data_length) {
751 max_len = usbd_xfer_max_len(xfer);
757 * If the remainder is greater than the maximum data length,
758 * we need to truncate the value for the sake of the
761 if (rem > xfer->max_data_length) {
762 rem = usbd_xfer_max_len(xfer);
764 if ((rem != max_len) && (is_complete != 0)) {
766 * If we don't transfer the data we can transfer, then
767 * the transfer is short !
769 xfer->flags.force_short_xfer = 1;
775 xfer->flags.force_short_xfer = 0;
776 xfer->nframes = max_len ? 2 : 1;
780 src_mcopy = USB_ADD_BYTES(src_mcopy, off);
781 usbd_copy_in(xfer->frbuffers + 1, 0,
783 usbd_xfer_set_frame_len(xfer, 1, max_len);
785 usbd_xfer_set_frame_data(xfer, 1,
786 USB_ADD_BYTES(src_zcopy, off), max_len);
789 /* the end is reached, send status */
790 xfer->flags.manual_status = 0;
791 usbd_xfer_set_frame_len(xfer, 1, 0);
793 DPRINTF("success\n");
794 return (0); /* success */
797 DPRINTF("%s\n", (state != USB_HR_NOT_COMPLETE) ?
798 "complete" : "stalled");
799 return (USB_ERR_STALLED);
802 DPRINTF("bad context\n");
803 return (USB_ERR_BAD_CONTEXT);