Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1 | /** |
| 2 | * ep0.c - DesignWare USB3 DRD Controller Endpoint 0 Handling |
| 3 | * |
| 4 | * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 5 | * |
| 6 | * Authors: Felipe Balbi <balbi@ti.com>, |
| 7 | * Sebastian Andrzej Siewior <bigeasy@linutronix.de> |
| 8 | * |
| 9 | * Redistribution and use in source and binary forms, with or without |
| 10 | * modification, are permitted provided that the following conditions |
| 11 | * are met: |
| 12 | * 1. Redistributions of source code must retain the above copyright |
| 13 | * notice, this list of conditions, and the following disclaimer, |
| 14 | * without modification. |
| 15 | * 2. Redistributions in binary form must reproduce the above copyright |
| 16 | * notice, this list of conditions and the following disclaimer in the |
| 17 | * documentation and/or other materials provided with the distribution. |
| 18 | * 3. The names of the above-listed copyright holders may not be used |
| 19 | * to endorse or promote products derived from this software without |
| 20 | * specific prior written permission. |
| 21 | * |
| 22 | * ALTERNATIVELY, this software may be distributed under the terms of the |
| 23 | * GNU General Public License ("GPL") version 2, as published by the Free |
| 24 | * Software Foundation. |
| 25 | * |
| 26 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
| 27 | * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 28 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 29 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 30 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 31 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 32 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 33 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 34 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 35 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 36 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 37 | */ |
| 38 | |
| 39 | #include <linux/kernel.h> |
| 40 | #include <linux/slab.h> |
| 41 | #include <linux/spinlock.h> |
| 42 | #include <linux/platform_device.h> |
| 43 | #include <linux/pm_runtime.h> |
| 44 | #include <linux/interrupt.h> |
| 45 | #include <linux/io.h> |
| 46 | #include <linux/list.h> |
| 47 | #include <linux/dma-mapping.h> |
| 48 | |
| 49 | #include <linux/usb/ch9.h> |
| 50 | #include <linux/usb/gadget.h> |
Sebastian Andrzej Siewior | 5bdb1dc | 2011-11-02 13:30:45 +0100 | [diff] [blame] | 51 | #include <linux/usb/composite.h> |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 52 | |
| 53 | #include "core.h" |
| 54 | #include "gadget.h" |
| 55 | #include "io.h" |
| 56 | |
Sebastian Andrzej Siewior | 5bdb1dc | 2011-11-02 13:30:45 +0100 | [diff] [blame] | 57 | static void dwc3_ep0_do_control_status(struct dwc3 *dwc, u32 epnum); |
| 58 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 59 | static const char *dwc3_ep0_state_string(enum dwc3_ep0_state state) |
| 60 | { |
| 61 | switch (state) { |
| 62 | case EP0_UNCONNECTED: |
| 63 | return "Unconnected"; |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 64 | case EP0_SETUP_PHASE: |
| 65 | return "Setup Phase"; |
| 66 | case EP0_DATA_PHASE: |
| 67 | return "Data Phase"; |
| 68 | case EP0_STATUS_PHASE: |
| 69 | return "Status Phase"; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 70 | default: |
| 71 | return "UNKNOWN"; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | static int dwc3_ep0_start_trans(struct dwc3 *dwc, u8 epnum, dma_addr_t buf_dma, |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 76 | u32 len, u32 type) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 77 | { |
| 78 | struct dwc3_gadget_ep_cmd_params params; |
| 79 | struct dwc3_trb_hw *trb_hw; |
| 80 | struct dwc3_trb trb; |
| 81 | struct dwc3_ep *dep; |
| 82 | |
| 83 | int ret; |
| 84 | |
| 85 | dep = dwc->eps[epnum]; |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 86 | if (dep->flags & DWC3_EP_BUSY) { |
| 87 | dev_vdbg(dwc->dev, "%s: still busy\n", dep->name); |
| 88 | return 0; |
| 89 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 90 | |
| 91 | trb_hw = dwc->ep0_trb; |
| 92 | memset(&trb, 0, sizeof(trb)); |
| 93 | |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 94 | trb.trbctl = type; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 95 | trb.bplh = buf_dma; |
| 96 | trb.length = len; |
| 97 | |
| 98 | trb.hwo = 1; |
| 99 | trb.lst = 1; |
| 100 | trb.ioc = 1; |
| 101 | trb.isp_imi = 1; |
| 102 | |
| 103 | dwc3_trb_to_hw(&trb, trb_hw); |
| 104 | |
| 105 | memset(¶ms, 0, sizeof(params)); |
Felipe Balbi | dc1c70a | 2011-09-30 10:58:51 +0300 | [diff] [blame] | 106 | params.param0 = upper_32_bits(dwc->ep0_trb_addr); |
| 107 | params.param1 = lower_32_bits(dwc->ep0_trb_addr); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 108 | |
| 109 | ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, |
| 110 | DWC3_DEPCMD_STARTTRANSFER, ¶ms); |
| 111 | if (ret < 0) { |
| 112 | dev_dbg(dwc->dev, "failed to send STARTTRANSFER command\n"); |
| 113 | return ret; |
| 114 | } |
| 115 | |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 116 | dep->flags |= DWC3_EP_BUSY; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 117 | dep->res_trans_idx = dwc3_gadget_ep_get_transfer_index(dwc, |
| 118 | dep->number); |
| 119 | |
Felipe Balbi | 1ddcb21 | 2011-08-30 15:52:17 +0300 | [diff] [blame] | 120 | dwc->ep0_next_event = DWC3_EP0_COMPLETE; |
| 121 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | static int __dwc3_gadget_ep0_queue(struct dwc3_ep *dep, |
| 126 | struct dwc3_request *req) |
| 127 | { |
Sebastian Andrzej Siewior | 5bdb1dc | 2011-11-02 13:30:45 +0100 | [diff] [blame] | 128 | struct dwc3 *dwc = dep->dwc; |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 129 | int ret = 0; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 130 | |
| 131 | req->request.actual = 0; |
| 132 | req->request.status = -EINPROGRESS; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 133 | req->epnum = dep->number; |
| 134 | |
| 135 | list_add_tail(&req->list, &dep->request_list); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 136 | |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 137 | /* |
| 138 | * Gadget driver might not be quick enough to queue a request |
| 139 | * before we get a Transfer Not Ready event on this endpoint. |
| 140 | * |
| 141 | * In that case, we will set DWC3_EP_PENDING_REQUEST. When that |
| 142 | * flag is set, it's telling us that as soon as Gadget queues the |
| 143 | * required request, we should kick the transfer here because the |
| 144 | * IRQ we were waiting for is long gone. |
| 145 | */ |
| 146 | if (dep->flags & DWC3_EP_PENDING_REQUEST) { |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 147 | unsigned direction; |
Felipe Balbi | a682970 | 2011-08-27 22:18:09 +0300 | [diff] [blame] | 148 | |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 149 | direction = !!(dep->flags & DWC3_EP0_DIR_IN); |
Felipe Balbi | a682970 | 2011-08-27 22:18:09 +0300 | [diff] [blame] | 150 | |
Felipe Balbi | 68d8a78 | 2011-12-29 06:32:29 +0200 | [diff] [blame] | 151 | if (dwc->ep0state != EP0_DATA_PHASE) { |
| 152 | dev_WARN(dwc->dev, "Unexpected pending request\n"); |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 153 | return 0; |
| 154 | } |
Felipe Balbi | a682970 | 2011-08-27 22:18:09 +0300 | [diff] [blame] | 155 | |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 156 | ret = dwc3_ep0_start_trans(dwc, direction, |
Felipe Balbi | 68d8a78 | 2011-12-29 06:32:29 +0200 | [diff] [blame] | 157 | req->request.dma, req->request.length, |
| 158 | DWC3_TRBCTL_CONTROL_DATA); |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 159 | dep->flags &= ~(DWC3_EP_PENDING_REQUEST | |
| 160 | DWC3_EP0_DIR_IN); |
Felipe Balbi | 68d3e66 | 2011-12-08 13:56:27 +0200 | [diff] [blame] | 161 | } else if (dwc->delayed_status) { |
Sebastian Andrzej Siewior | 5bdb1dc | 2011-11-02 13:30:45 +0100 | [diff] [blame] | 162 | dwc->delayed_status = false; |
Felipe Balbi | 68d3e66 | 2011-12-08 13:56:27 +0200 | [diff] [blame] | 163 | |
| 164 | if (dwc->ep0state == EP0_STATUS_PHASE) |
| 165 | dwc3_ep0_do_control_status(dwc, 1); |
| 166 | else |
| 167 | dev_dbg(dwc->dev, "too early for delayed status\n"); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | return ret; |
| 171 | } |
| 172 | |
| 173 | int dwc3_gadget_ep0_queue(struct usb_ep *ep, struct usb_request *request, |
| 174 | gfp_t gfp_flags) |
| 175 | { |
| 176 | struct dwc3_request *req = to_dwc3_request(request); |
| 177 | struct dwc3_ep *dep = to_dwc3_ep(ep); |
| 178 | struct dwc3 *dwc = dep->dwc; |
| 179 | |
| 180 | unsigned long flags; |
| 181 | |
| 182 | int ret; |
| 183 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 184 | spin_lock_irqsave(&dwc->lock, flags); |
| 185 | if (!dep->desc) { |
| 186 | dev_dbg(dwc->dev, "trying to queue request %p to disabled %s\n", |
| 187 | request, dep->name); |
| 188 | ret = -ESHUTDOWN; |
| 189 | goto out; |
| 190 | } |
| 191 | |
| 192 | /* we share one TRB for ep0/1 */ |
Sebastian Andrzej Siewior | c2da2ff | 2011-10-20 19:04:16 +0200 | [diff] [blame] | 193 | if (!list_empty(&dep->request_list)) { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 194 | ret = -EBUSY; |
| 195 | goto out; |
| 196 | } |
| 197 | |
| 198 | dev_vdbg(dwc->dev, "queueing request %p to %s length %d, state '%s'\n", |
| 199 | request, dep->name, request->length, |
| 200 | dwc3_ep0_state_string(dwc->ep0state)); |
| 201 | |
| 202 | ret = __dwc3_gadget_ep0_queue(dep, req); |
| 203 | |
| 204 | out: |
| 205 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 206 | |
| 207 | return ret; |
| 208 | } |
| 209 | |
| 210 | static void dwc3_ep0_stall_and_restart(struct dwc3 *dwc) |
| 211 | { |
Felipe Balbi | d742220 | 2011-09-08 18:17:12 +0300 | [diff] [blame] | 212 | struct dwc3_ep *dep = dwc->eps[0]; |
| 213 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 214 | /* stall is always issued on EP0 */ |
Sebastian Andrzej Siewior | c2da2ff | 2011-10-20 19:04:16 +0200 | [diff] [blame] | 215 | __dwc3_gadget_ep_set_halt(dep, 1); |
| 216 | dep->flags = DWC3_EP_ENABLED; |
Sebastian Andrzej Siewior | 5bdb1dc | 2011-11-02 13:30:45 +0100 | [diff] [blame] | 217 | dwc->delayed_status = false; |
Felipe Balbi | d742220 | 2011-09-08 18:17:12 +0300 | [diff] [blame] | 218 | |
| 219 | if (!list_empty(&dep->request_list)) { |
| 220 | struct dwc3_request *req; |
| 221 | |
| 222 | req = next_request(&dep->request_list); |
| 223 | dwc3_gadget_giveback(dep, req, -ECONNRESET); |
| 224 | } |
| 225 | |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 226 | dwc->ep0state = EP0_SETUP_PHASE; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 227 | dwc3_ep0_out_start(dwc); |
| 228 | } |
| 229 | |
| 230 | void dwc3_ep0_out_start(struct dwc3 *dwc) |
| 231 | { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 232 | int ret; |
| 233 | |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 234 | ret = dwc3_ep0_start_trans(dwc, 0, dwc->ctrl_req_addr, 8, |
| 235 | DWC3_TRBCTL_CONTROL_SETUP); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 236 | WARN_ON(ret < 0); |
| 237 | } |
| 238 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 239 | static struct dwc3_ep *dwc3_wIndex_to_dep(struct dwc3 *dwc, __le16 wIndex_le) |
| 240 | { |
| 241 | struct dwc3_ep *dep; |
| 242 | u32 windex = le16_to_cpu(wIndex_le); |
| 243 | u32 epnum; |
| 244 | |
| 245 | epnum = (windex & USB_ENDPOINT_NUMBER_MASK) << 1; |
| 246 | if ((windex & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) |
| 247 | epnum |= 1; |
| 248 | |
| 249 | dep = dwc->eps[epnum]; |
| 250 | if (dep->flags & DWC3_EP_ENABLED) |
| 251 | return dep; |
| 252 | |
| 253 | return NULL; |
| 254 | } |
| 255 | |
Sebastian Andrzej Siewior | 8ee6270 | 2011-10-18 19:13:29 +0200 | [diff] [blame] | 256 | static void dwc3_ep0_status_cmpl(struct usb_ep *ep, struct usb_request *req) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 257 | { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 258 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 259 | /* |
| 260 | * ch 9.4.5 |
| 261 | */ |
Felipe Balbi | 25b8ff6 | 2011-11-04 12:32:47 +0200 | [diff] [blame] | 262 | static int dwc3_ep0_handle_status(struct dwc3 *dwc, |
| 263 | struct usb_ctrlrequest *ctrl) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 264 | { |
| 265 | struct dwc3_ep *dep; |
| 266 | u32 recip; |
| 267 | u16 usb_status = 0; |
| 268 | __le16 *response_pkt; |
| 269 | |
| 270 | recip = ctrl->bRequestType & USB_RECIP_MASK; |
| 271 | switch (recip) { |
| 272 | case USB_RECIP_DEVICE: |
| 273 | /* |
| 274 | * We are self-powered. U1/U2/LTM will be set later |
| 275 | * once we handle this states. RemoteWakeup is 0 on SS |
| 276 | */ |
| 277 | usb_status |= dwc->is_selfpowered << USB_DEVICE_SELF_POWERED; |
| 278 | break; |
| 279 | |
| 280 | case USB_RECIP_INTERFACE: |
| 281 | /* |
| 282 | * Function Remote Wake Capable D0 |
| 283 | * Function Remote Wakeup D1 |
| 284 | */ |
| 285 | break; |
| 286 | |
| 287 | case USB_RECIP_ENDPOINT: |
| 288 | dep = dwc3_wIndex_to_dep(dwc, ctrl->wIndex); |
| 289 | if (!dep) |
Felipe Balbi | 25b8ff6 | 2011-11-04 12:32:47 +0200 | [diff] [blame] | 290 | return -EINVAL; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 291 | |
| 292 | if (dep->flags & DWC3_EP_STALL) |
| 293 | usb_status = 1 << USB_ENDPOINT_HALT; |
| 294 | break; |
| 295 | default: |
| 296 | return -EINVAL; |
| 297 | }; |
| 298 | |
| 299 | response_pkt = (__le16 *) dwc->setup_buf; |
| 300 | *response_pkt = cpu_to_le16(usb_status); |
Felipe Balbi | e261779 | 2011-11-29 10:35:47 +0200 | [diff] [blame] | 301 | |
| 302 | dep = dwc->eps[0]; |
| 303 | dwc->ep0_usb_req.dep = dep; |
Sebastian Andrzej Siewior | e0ce0b0 | 2011-11-25 12:03:46 +0100 | [diff] [blame] | 304 | dwc->ep0_usb_req.request.length = sizeof(*response_pkt); |
| 305 | dwc->ep0_usb_req.request.dma = dwc->setup_buf_addr; |
| 306 | dwc->ep0_usb_req.request.complete = dwc3_ep0_status_cmpl; |
Felipe Balbi | e261779 | 2011-11-29 10:35:47 +0200 | [diff] [blame] | 307 | |
| 308 | return __dwc3_gadget_ep0_queue(dep, &dwc->ep0_usb_req); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | static int dwc3_ep0_handle_feature(struct dwc3 *dwc, |
| 312 | struct usb_ctrlrequest *ctrl, int set) |
| 313 | { |
| 314 | struct dwc3_ep *dep; |
| 315 | u32 recip; |
| 316 | u32 wValue; |
| 317 | u32 wIndex; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 318 | int ret; |
| 319 | u32 mode; |
| 320 | |
| 321 | wValue = le16_to_cpu(ctrl->wValue); |
| 322 | wIndex = le16_to_cpu(ctrl->wIndex); |
| 323 | recip = ctrl->bRequestType & USB_RECIP_MASK; |
| 324 | switch (recip) { |
| 325 | case USB_RECIP_DEVICE: |
| 326 | |
| 327 | /* |
| 328 | * 9.4.1 says only only for SS, in AddressState only for |
| 329 | * default control pipe |
| 330 | */ |
| 331 | switch (wValue) { |
| 332 | case USB_DEVICE_U1_ENABLE: |
| 333 | case USB_DEVICE_U2_ENABLE: |
| 334 | case USB_DEVICE_LTM_ENABLE: |
| 335 | if (dwc->dev_state != DWC3_CONFIGURED_STATE) |
| 336 | return -EINVAL; |
| 337 | if (dwc->speed != DWC3_DSTS_SUPERSPEED) |
| 338 | return -EINVAL; |
| 339 | } |
| 340 | |
| 341 | /* XXX add U[12] & LTM */ |
| 342 | switch (wValue) { |
| 343 | case USB_DEVICE_REMOTE_WAKEUP: |
| 344 | break; |
| 345 | case USB_DEVICE_U1_ENABLE: |
| 346 | break; |
| 347 | case USB_DEVICE_U2_ENABLE: |
| 348 | break; |
| 349 | case USB_DEVICE_LTM_ENABLE: |
| 350 | break; |
| 351 | |
| 352 | case USB_DEVICE_TEST_MODE: |
| 353 | if ((wIndex & 0xff) != 0) |
| 354 | return -EINVAL; |
| 355 | if (!set) |
| 356 | return -EINVAL; |
| 357 | |
| 358 | mode = wIndex >> 8; |
Felipe Balbi | 04a9bfc | 2012-01-02 18:25:43 +0200 | [diff] [blame] | 359 | ret = dwc3_gadget_set_test_mode(dwc, mode); |
| 360 | if (ret < 0) { |
| 361 | dev_dbg(dwc->dev, "Invalid Test #%d\n", |
| 362 | mode); |
| 363 | return ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 364 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 365 | } |
| 366 | break; |
| 367 | |
| 368 | case USB_RECIP_INTERFACE: |
| 369 | switch (wValue) { |
| 370 | case USB_INTRF_FUNC_SUSPEND: |
| 371 | if (wIndex & USB_INTRF_FUNC_SUSPEND_LP) |
| 372 | /* XXX enable Low power suspend */ |
| 373 | ; |
| 374 | if (wIndex & USB_INTRF_FUNC_SUSPEND_RW) |
| 375 | /* XXX enable remote wakeup */ |
| 376 | ; |
| 377 | break; |
| 378 | default: |
| 379 | return -EINVAL; |
| 380 | } |
| 381 | break; |
| 382 | |
| 383 | case USB_RECIP_ENDPOINT: |
| 384 | switch (wValue) { |
| 385 | case USB_ENDPOINT_HALT: |
Sebastian Andrzej Siewior | 1e7618d | 2011-10-24 12:09:39 +0300 | [diff] [blame] | 386 | dep = dwc3_wIndex_to_dep(dwc, wIndex); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 387 | if (!dep) |
| 388 | return -EINVAL; |
| 389 | ret = __dwc3_gadget_ep_set_halt(dep, set); |
| 390 | if (ret) |
| 391 | return -EINVAL; |
| 392 | break; |
| 393 | default: |
| 394 | return -EINVAL; |
| 395 | } |
| 396 | break; |
| 397 | |
| 398 | default: |
| 399 | return -EINVAL; |
| 400 | }; |
| 401 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 402 | return 0; |
| 403 | } |
| 404 | |
| 405 | static int dwc3_ep0_set_address(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl) |
| 406 | { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 407 | u32 addr; |
| 408 | u32 reg; |
| 409 | |
| 410 | addr = le16_to_cpu(ctrl->wValue); |
Felipe Balbi | f96a6ec | 2011-10-15 21:37:35 +0300 | [diff] [blame] | 411 | if (addr > 127) { |
| 412 | dev_dbg(dwc->dev, "invalid device address %d\n", addr); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 413 | return -EINVAL; |
Felipe Balbi | f96a6ec | 2011-10-15 21:37:35 +0300 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | if (dwc->dev_state == DWC3_CONFIGURED_STATE) { |
| 417 | dev_dbg(dwc->dev, "trying to set address when configured\n"); |
| 418 | return -EINVAL; |
| 419 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 420 | |
Felipe Balbi | 2646021 | 2011-09-30 10:58:36 +0300 | [diff] [blame] | 421 | reg = dwc3_readl(dwc->regs, DWC3_DCFG); |
| 422 | reg &= ~(DWC3_DCFG_DEVADDR_MASK); |
| 423 | reg |= DWC3_DCFG_DEVADDR(addr); |
| 424 | dwc3_writel(dwc->regs, DWC3_DCFG, reg); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 425 | |
Felipe Balbi | 2646021 | 2011-09-30 10:58:36 +0300 | [diff] [blame] | 426 | if (addr) |
| 427 | dwc->dev_state = DWC3_ADDRESS_STATE; |
| 428 | else |
| 429 | dwc->dev_state = DWC3_DEFAULT_STATE; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 430 | |
Felipe Balbi | 2646021 | 2011-09-30 10:58:36 +0300 | [diff] [blame] | 431 | return 0; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | static int dwc3_ep0_delegate_req(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl) |
| 435 | { |
| 436 | int ret; |
| 437 | |
| 438 | spin_unlock(&dwc->lock); |
| 439 | ret = dwc->gadget_driver->setup(&dwc->gadget, ctrl); |
| 440 | spin_lock(&dwc->lock); |
| 441 | return ret; |
| 442 | } |
| 443 | |
| 444 | static int dwc3_ep0_set_config(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl) |
| 445 | { |
| 446 | u32 cfg; |
| 447 | int ret; |
| 448 | |
Paul Zimmerman | b23c843 | 2011-09-30 10:58:42 +0300 | [diff] [blame] | 449 | dwc->start_config_issued = false; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 450 | cfg = le16_to_cpu(ctrl->wValue); |
| 451 | |
| 452 | switch (dwc->dev_state) { |
| 453 | case DWC3_DEFAULT_STATE: |
| 454 | return -EINVAL; |
| 455 | break; |
| 456 | |
| 457 | case DWC3_ADDRESS_STATE: |
| 458 | ret = dwc3_ep0_delegate_req(dwc, ctrl); |
| 459 | /* if the cfg matches and the cfg is non zero */ |
Felipe Balbi | 457e84b | 2012-01-18 18:04:09 +0200 | [diff] [blame^] | 460 | if (cfg && (!ret || (ret == USB_GADGET_DELAYED_STATUS))) { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 461 | dwc->dev_state = DWC3_CONFIGURED_STATE; |
Felipe Balbi | 457e84b | 2012-01-18 18:04:09 +0200 | [diff] [blame^] | 462 | dwc->resize_fifos = true; |
| 463 | dev_dbg(dwc->dev, "resize fifos flag SET\n"); |
| 464 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 465 | break; |
| 466 | |
| 467 | case DWC3_CONFIGURED_STATE: |
| 468 | ret = dwc3_ep0_delegate_req(dwc, ctrl); |
| 469 | if (!cfg) |
| 470 | dwc->dev_state = DWC3_ADDRESS_STATE; |
| 471 | break; |
Sebastian Andrzej Siewior | 5bdb1dc | 2011-11-02 13:30:45 +0100 | [diff] [blame] | 472 | default: |
| 473 | ret = -EINVAL; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 474 | } |
Sebastian Andrzej Siewior | 5bdb1dc | 2011-11-02 13:30:45 +0100 | [diff] [blame] | 475 | return ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | static int dwc3_ep0_std_request(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl) |
| 479 | { |
| 480 | int ret; |
| 481 | |
| 482 | switch (ctrl->bRequest) { |
| 483 | case USB_REQ_GET_STATUS: |
| 484 | dev_vdbg(dwc->dev, "USB_REQ_GET_STATUS\n"); |
| 485 | ret = dwc3_ep0_handle_status(dwc, ctrl); |
| 486 | break; |
| 487 | case USB_REQ_CLEAR_FEATURE: |
| 488 | dev_vdbg(dwc->dev, "USB_REQ_CLEAR_FEATURE\n"); |
| 489 | ret = dwc3_ep0_handle_feature(dwc, ctrl, 0); |
| 490 | break; |
| 491 | case USB_REQ_SET_FEATURE: |
| 492 | dev_vdbg(dwc->dev, "USB_REQ_SET_FEATURE\n"); |
| 493 | ret = dwc3_ep0_handle_feature(dwc, ctrl, 1); |
| 494 | break; |
| 495 | case USB_REQ_SET_ADDRESS: |
| 496 | dev_vdbg(dwc->dev, "USB_REQ_SET_ADDRESS\n"); |
| 497 | ret = dwc3_ep0_set_address(dwc, ctrl); |
| 498 | break; |
| 499 | case USB_REQ_SET_CONFIGURATION: |
| 500 | dev_vdbg(dwc->dev, "USB_REQ_SET_CONFIGURATION\n"); |
| 501 | ret = dwc3_ep0_set_config(dwc, ctrl); |
| 502 | break; |
| 503 | default: |
| 504 | dev_vdbg(dwc->dev, "Forwarding to gadget driver\n"); |
| 505 | ret = dwc3_ep0_delegate_req(dwc, ctrl); |
| 506 | break; |
| 507 | }; |
| 508 | |
| 509 | return ret; |
| 510 | } |
| 511 | |
| 512 | static void dwc3_ep0_inspect_setup(struct dwc3 *dwc, |
| 513 | const struct dwc3_event_depevt *event) |
| 514 | { |
| 515 | struct usb_ctrlrequest *ctrl = dwc->ctrl_req; |
| 516 | int ret; |
| 517 | u32 len; |
| 518 | |
| 519 | if (!dwc->gadget_driver) |
| 520 | goto err; |
| 521 | |
| 522 | len = le16_to_cpu(ctrl->wLength); |
Felipe Balbi | 1ddcb21 | 2011-08-30 15:52:17 +0300 | [diff] [blame] | 523 | if (!len) { |
Felipe Balbi | d95b09b | 2011-09-30 10:58:37 +0300 | [diff] [blame] | 524 | dwc->three_stage_setup = false; |
| 525 | dwc->ep0_expect_in = false; |
Felipe Balbi | 1ddcb21 | 2011-08-30 15:52:17 +0300 | [diff] [blame] | 526 | dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS; |
| 527 | } else { |
Felipe Balbi | d95b09b | 2011-09-30 10:58:37 +0300 | [diff] [blame] | 528 | dwc->three_stage_setup = true; |
| 529 | dwc->ep0_expect_in = !!(ctrl->bRequestType & USB_DIR_IN); |
Felipe Balbi | 1ddcb21 | 2011-08-30 15:52:17 +0300 | [diff] [blame] | 530 | dwc->ep0_next_event = DWC3_EP0_NRDY_DATA; |
| 531 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 532 | |
| 533 | if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) |
| 534 | ret = dwc3_ep0_std_request(dwc, ctrl); |
| 535 | else |
| 536 | ret = dwc3_ep0_delegate_req(dwc, ctrl); |
| 537 | |
Sebastian Andrzej Siewior | 5bdb1dc | 2011-11-02 13:30:45 +0100 | [diff] [blame] | 538 | if (ret == USB_GADGET_DELAYED_STATUS) |
| 539 | dwc->delayed_status = true; |
| 540 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 541 | if (ret >= 0) |
| 542 | return; |
| 543 | |
| 544 | err: |
| 545 | dwc3_ep0_stall_and_restart(dwc); |
| 546 | } |
| 547 | |
| 548 | static void dwc3_ep0_complete_data(struct dwc3 *dwc, |
| 549 | const struct dwc3_event_depevt *event) |
| 550 | { |
| 551 | struct dwc3_request *r = NULL; |
| 552 | struct usb_request *ur; |
| 553 | struct dwc3_trb trb; |
Sebastian Andrzej Siewior | c2da2ff | 2011-10-20 19:04:16 +0200 | [diff] [blame] | 554 | struct dwc3_ep *ep0; |
Felipe Balbi | c611ccb | 2011-08-27 02:30:33 +0300 | [diff] [blame] | 555 | u32 transferred; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 556 | u8 epnum; |
| 557 | |
| 558 | epnum = event->endpoint_number; |
Sebastian Andrzej Siewior | c2da2ff | 2011-10-20 19:04:16 +0200 | [diff] [blame] | 559 | ep0 = dwc->eps[0]; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 560 | |
Felipe Balbi | 1ddcb21 | 2011-08-30 15:52:17 +0300 | [diff] [blame] | 561 | dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS; |
| 562 | |
Sebastian Andrzej Siewior | c2da2ff | 2011-10-20 19:04:16 +0200 | [diff] [blame] | 563 | r = next_request(&ep0->request_list); |
Sebastian Andrzej Siewior | 8ee6270 | 2011-10-18 19:13:29 +0200 | [diff] [blame] | 564 | ur = &r->request; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 565 | |
| 566 | dwc3_trb_to_nat(dwc->ep0_trb, &trb); |
| 567 | |
Felipe Balbi | a682970 | 2011-08-27 22:18:09 +0300 | [diff] [blame] | 568 | if (dwc->ep0_bounced) { |
Felipe Balbi | a682970 | 2011-08-27 22:18:09 +0300 | [diff] [blame] | 569 | |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 570 | transferred = min_t(u32, ur->length, |
| 571 | ep0->endpoint.maxpacket - trb.length); |
Felipe Balbi | a682970 | 2011-08-27 22:18:09 +0300 | [diff] [blame] | 572 | memcpy(ur->buf, dwc->ep0_bounce, transferred); |
| 573 | dwc->ep0_bounced = false; |
| 574 | } else { |
| 575 | transferred = ur->length - trb.length; |
| 576 | ur->actual += transferred; |
| 577 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 578 | |
| 579 | if ((epnum & 1) && ur->actual < ur->length) { |
| 580 | /* for some reason we did not get everything out */ |
| 581 | |
| 582 | dwc3_ep0_stall_and_restart(dwc); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 583 | } else { |
| 584 | /* |
| 585 | * handle the case where we have to send a zero packet. This |
| 586 | * seems to be case when req.length > maxpacket. Could it be? |
| 587 | */ |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 588 | if (r) |
Sebastian Andrzej Siewior | c2da2ff | 2011-10-20 19:04:16 +0200 | [diff] [blame] | 589 | dwc3_gadget_giveback(ep0, r, 0); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 590 | } |
| 591 | } |
| 592 | |
| 593 | static void dwc3_ep0_complete_req(struct dwc3 *dwc, |
| 594 | const struct dwc3_event_depevt *event) |
| 595 | { |
| 596 | struct dwc3_request *r; |
| 597 | struct dwc3_ep *dep; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 598 | |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 599 | dep = dwc->eps[0]; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 600 | |
| 601 | if (!list_empty(&dep->request_list)) { |
| 602 | r = next_request(&dep->request_list); |
| 603 | |
| 604 | dwc3_gadget_giveback(dep, r, 0); |
| 605 | } |
| 606 | |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 607 | dwc->ep0state = EP0_SETUP_PHASE; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 608 | dwc3_ep0_out_start(dwc); |
| 609 | } |
| 610 | |
| 611 | static void dwc3_ep0_xfer_complete(struct dwc3 *dwc, |
| 612 | const struct dwc3_event_depevt *event) |
| 613 | { |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 614 | struct dwc3_ep *dep = dwc->eps[event->endpoint_number]; |
| 615 | |
| 616 | dep->flags &= ~DWC3_EP_BUSY; |
Felipe Balbi | df62df5 | 2011-10-14 15:11:49 +0300 | [diff] [blame] | 617 | dwc->setup_packet_pending = false; |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 618 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 619 | switch (dwc->ep0state) { |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 620 | case EP0_SETUP_PHASE: |
| 621 | dev_vdbg(dwc->dev, "Inspecting Setup Bytes\n"); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 622 | dwc3_ep0_inspect_setup(dwc, event); |
| 623 | break; |
| 624 | |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 625 | case EP0_DATA_PHASE: |
| 626 | dev_vdbg(dwc->dev, "Data Phase\n"); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 627 | dwc3_ep0_complete_data(dwc, event); |
| 628 | break; |
| 629 | |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 630 | case EP0_STATUS_PHASE: |
| 631 | dev_vdbg(dwc->dev, "Status Phase\n"); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 632 | dwc3_ep0_complete_req(dwc, event); |
| 633 | break; |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 634 | default: |
| 635 | WARN(true, "UNKNOWN ep0state %d\n", dwc->ep0state); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 636 | } |
| 637 | } |
| 638 | |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 639 | static void dwc3_ep0_do_control_setup(struct dwc3 *dwc, |
| 640 | const struct dwc3_event_depevt *event) |
| 641 | { |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 642 | dwc3_ep0_out_start(dwc); |
| 643 | } |
| 644 | |
| 645 | static void dwc3_ep0_do_control_data(struct dwc3 *dwc, |
| 646 | const struct dwc3_event_depevt *event) |
| 647 | { |
| 648 | struct dwc3_ep *dep; |
| 649 | struct dwc3_request *req; |
| 650 | int ret; |
| 651 | |
| 652 | dep = dwc->eps[0]; |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 653 | |
| 654 | if (list_empty(&dep->request_list)) { |
| 655 | dev_vdbg(dwc->dev, "pending request for EP0 Data phase\n"); |
| 656 | dep->flags |= DWC3_EP_PENDING_REQUEST; |
| 657 | |
| 658 | if (event->endpoint_number) |
| 659 | dep->flags |= DWC3_EP0_DIR_IN; |
| 660 | return; |
| 661 | } |
| 662 | |
| 663 | req = next_request(&dep->request_list); |
| 664 | req->direction = !!event->endpoint_number; |
| 665 | |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 666 | if (req->request.length == 0) { |
| 667 | ret = dwc3_ep0_start_trans(dwc, event->endpoint_number, |
| 668 | dwc->ctrl_req_addr, 0, |
| 669 | DWC3_TRBCTL_CONTROL_DATA); |
| 670 | } else if ((req->request.length % dep->endpoint.maxpacket) |
| 671 | && (event->endpoint_number == 0)) { |
| 672 | dwc3_map_buffer_to_dma(req); |
| 673 | |
| 674 | WARN_ON(req->request.length > dep->endpoint.maxpacket); |
| 675 | |
| 676 | dwc->ep0_bounced = true; |
| 677 | |
| 678 | /* |
| 679 | * REVISIT in case request length is bigger than EP0 |
| 680 | * wMaxPacketSize, we will need two chained TRBs to handle |
| 681 | * the transfer. |
| 682 | */ |
| 683 | ret = dwc3_ep0_start_trans(dwc, event->endpoint_number, |
| 684 | dwc->ep0_bounce_addr, dep->endpoint.maxpacket, |
| 685 | DWC3_TRBCTL_CONTROL_DATA); |
| 686 | } else { |
| 687 | dwc3_map_buffer_to_dma(req); |
| 688 | |
| 689 | ret = dwc3_ep0_start_trans(dwc, event->endpoint_number, |
| 690 | req->request.dma, req->request.length, |
| 691 | DWC3_TRBCTL_CONTROL_DATA); |
| 692 | } |
| 693 | |
| 694 | WARN_ON(ret < 0); |
| 695 | } |
| 696 | |
Sebastian Andrzej Siewior | f0f2b2a | 2011-11-02 13:30:44 +0100 | [diff] [blame] | 697 | static int dwc3_ep0_start_control_status(struct dwc3_ep *dep) |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 698 | { |
Sebastian Andrzej Siewior | f0f2b2a | 2011-11-02 13:30:44 +0100 | [diff] [blame] | 699 | struct dwc3 *dwc = dep->dwc; |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 700 | u32 type; |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 701 | |
| 702 | type = dwc->three_stage_setup ? DWC3_TRBCTL_CONTROL_STATUS3 |
| 703 | : DWC3_TRBCTL_CONTROL_STATUS2; |
| 704 | |
Sebastian Andrzej Siewior | f0f2b2a | 2011-11-02 13:30:44 +0100 | [diff] [blame] | 705 | return dwc3_ep0_start_trans(dwc, dep->number, |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 706 | dwc->ctrl_req_addr, 0, type); |
Sebastian Andrzej Siewior | f0f2b2a | 2011-11-02 13:30:44 +0100 | [diff] [blame] | 707 | } |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 708 | |
Sebastian Andrzej Siewior | f0f2b2a | 2011-11-02 13:30:44 +0100 | [diff] [blame] | 709 | static void dwc3_ep0_do_control_status(struct dwc3 *dwc, u32 epnum) |
| 710 | { |
| 711 | struct dwc3_ep *dep = dwc->eps[epnum]; |
| 712 | |
Felipe Balbi | 457e84b | 2012-01-18 18:04:09 +0200 | [diff] [blame^] | 713 | if (dwc->resize_fifos) { |
| 714 | dev_dbg(dwc->dev, "starting to resize fifos\n"); |
| 715 | dwc3_gadget_resize_tx_fifos(dwc); |
| 716 | dwc->resize_fifos = 0; |
| 717 | } |
| 718 | |
Sebastian Andrzej Siewior | f0f2b2a | 2011-11-02 13:30:44 +0100 | [diff] [blame] | 719 | WARN_ON(dwc3_ep0_start_control_status(dep)); |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 720 | } |
| 721 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 722 | static void dwc3_ep0_xfernotready(struct dwc3 *dwc, |
| 723 | const struct dwc3_event_depevt *event) |
| 724 | { |
Felipe Balbi | df62df5 | 2011-10-14 15:11:49 +0300 | [diff] [blame] | 725 | dwc->setup_packet_pending = true; |
| 726 | |
Felipe Balbi | 9cc9bcd | 2011-10-18 18:00:26 +0300 | [diff] [blame] | 727 | /* |
| 728 | * This part is very tricky: If we has just handled |
| 729 | * XferNotReady(Setup) and we're now expecting a |
| 730 | * XferComplete but, instead, we receive another |
| 731 | * XferNotReady(Setup), we should STALL and restart |
| 732 | * the state machine. |
| 733 | * |
| 734 | * In all other cases, we just continue waiting |
| 735 | * for the XferComplete event. |
| 736 | * |
| 737 | * We are a little bit unsafe here because we're |
| 738 | * not trying to ensure that last event was, indeed, |
| 739 | * XferNotReady(Setup). |
| 740 | * |
| 741 | * Still, we don't expect any condition where that |
| 742 | * should happen and, even if it does, it would be |
| 743 | * another error condition. |
| 744 | */ |
| 745 | if (dwc->ep0_next_event == DWC3_EP0_COMPLETE) { |
| 746 | switch (event->status) { |
| 747 | case DEPEVT_STATUS_CONTROL_SETUP: |
| 748 | dev_vdbg(dwc->dev, "Unexpected XferNotReady(Setup)\n"); |
| 749 | dwc3_ep0_stall_and_restart(dwc); |
| 750 | break; |
| 751 | case DEPEVT_STATUS_CONTROL_DATA: |
| 752 | /* FALLTHROUGH */ |
| 753 | case DEPEVT_STATUS_CONTROL_STATUS: |
| 754 | /* FALLTHROUGH */ |
| 755 | default: |
| 756 | dev_vdbg(dwc->dev, "waiting for XferComplete\n"); |
| 757 | } |
| 758 | |
| 759 | return; |
| 760 | } |
| 761 | |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 762 | switch (event->status) { |
| 763 | case DEPEVT_STATUS_CONTROL_SETUP: |
| 764 | dev_vdbg(dwc->dev, "Control Setup\n"); |
Sebastian Andrzej Siewior | f0f2b2a | 2011-11-02 13:30:44 +0100 | [diff] [blame] | 765 | |
| 766 | dwc->ep0state = EP0_SETUP_PHASE; |
| 767 | |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 768 | dwc3_ep0_do_control_setup(dwc, event); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 769 | break; |
Felipe Balbi | 1ddcb21 | 2011-08-30 15:52:17 +0300 | [diff] [blame] | 770 | |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 771 | case DEPEVT_STATUS_CONTROL_DATA: |
| 772 | dev_vdbg(dwc->dev, "Control Data\n"); |
Felipe Balbi | 1ddcb21 | 2011-08-30 15:52:17 +0300 | [diff] [blame] | 773 | |
Sebastian Andrzej Siewior | f0f2b2a | 2011-11-02 13:30:44 +0100 | [diff] [blame] | 774 | dwc->ep0state = EP0_DATA_PHASE; |
| 775 | |
Felipe Balbi | 1ddcb21 | 2011-08-30 15:52:17 +0300 | [diff] [blame] | 776 | if (dwc->ep0_next_event != DWC3_EP0_NRDY_DATA) { |
| 777 | dev_vdbg(dwc->dev, "Expected %d got %d\n", |
Felipe Balbi | 25355be | 2011-09-30 10:58:38 +0300 | [diff] [blame] | 778 | dwc->ep0_next_event, |
| 779 | DWC3_EP0_NRDY_DATA); |
Felipe Balbi | 1ddcb21 | 2011-08-30 15:52:17 +0300 | [diff] [blame] | 780 | |
| 781 | dwc3_ep0_stall_and_restart(dwc); |
| 782 | return; |
| 783 | } |
| 784 | |
Felipe Balbi | 55f3fba | 2011-09-08 18:27:33 +0300 | [diff] [blame] | 785 | /* |
| 786 | * One of the possible error cases is when Host _does_ |
| 787 | * request for Data Phase, but it does so on the wrong |
| 788 | * direction. |
| 789 | * |
| 790 | * Here, we already know ep0_next_event is DATA (see above), |
| 791 | * so we only need to check for direction. |
| 792 | */ |
| 793 | if (dwc->ep0_expect_in != event->endpoint_number) { |
| 794 | dev_vdbg(dwc->dev, "Wrong direction for Data phase\n"); |
| 795 | dwc3_ep0_stall_and_restart(dwc); |
| 796 | return; |
| 797 | } |
| 798 | |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 799 | dwc3_ep0_do_control_data(dwc, event); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 800 | break; |
Felipe Balbi | 1ddcb21 | 2011-08-30 15:52:17 +0300 | [diff] [blame] | 801 | |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 802 | case DEPEVT_STATUS_CONTROL_STATUS: |
| 803 | dev_vdbg(dwc->dev, "Control Status\n"); |
Felipe Balbi | 1ddcb21 | 2011-08-30 15:52:17 +0300 | [diff] [blame] | 804 | |
Sebastian Andrzej Siewior | f0f2b2a | 2011-11-02 13:30:44 +0100 | [diff] [blame] | 805 | dwc->ep0state = EP0_STATUS_PHASE; |
| 806 | |
Felipe Balbi | 1ddcb21 | 2011-08-30 15:52:17 +0300 | [diff] [blame] | 807 | if (dwc->ep0_next_event != DWC3_EP0_NRDY_STATUS) { |
| 808 | dev_vdbg(dwc->dev, "Expected %d got %d\n", |
Felipe Balbi | 25355be | 2011-09-30 10:58:38 +0300 | [diff] [blame] | 809 | dwc->ep0_next_event, |
| 810 | DWC3_EP0_NRDY_STATUS); |
Felipe Balbi | 1ddcb21 | 2011-08-30 15:52:17 +0300 | [diff] [blame] | 811 | |
| 812 | dwc3_ep0_stall_and_restart(dwc); |
| 813 | return; |
| 814 | } |
Sebastian Andrzej Siewior | 5bdb1dc | 2011-11-02 13:30:45 +0100 | [diff] [blame] | 815 | |
| 816 | if (dwc->delayed_status) { |
| 817 | WARN_ON_ONCE(event->endpoint_number != 1); |
| 818 | dev_vdbg(dwc->dev, "Mass Storage delayed status\n"); |
| 819 | return; |
| 820 | } |
| 821 | |
Sebastian Andrzej Siewior | f0f2b2a | 2011-11-02 13:30:44 +0100 | [diff] [blame] | 822 | dwc3_ep0_do_control_status(dwc, event->endpoint_number); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 823 | } |
| 824 | } |
| 825 | |
| 826 | void dwc3_ep0_interrupt(struct dwc3 *dwc, |
Felipe Balbi | 8becf27 | 2011-11-04 12:40:05 +0200 | [diff] [blame] | 827 | const struct dwc3_event_depevt *event) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 828 | { |
| 829 | u8 epnum = event->endpoint_number; |
| 830 | |
| 831 | dev_dbg(dwc->dev, "%s while ep%d%s in state '%s'\n", |
| 832 | dwc3_ep_event_string(event->endpoint_event), |
Sebastian Andrzej Siewior | b147f35 | 2011-09-30 10:58:40 +0300 | [diff] [blame] | 833 | epnum >> 1, (epnum & 1) ? "in" : "out", |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 834 | dwc3_ep0_state_string(dwc->ep0state)); |
| 835 | |
| 836 | switch (event->endpoint_event) { |
| 837 | case DWC3_DEPEVT_XFERCOMPLETE: |
| 838 | dwc3_ep0_xfer_complete(dwc, event); |
| 839 | break; |
| 840 | |
| 841 | case DWC3_DEPEVT_XFERNOTREADY: |
| 842 | dwc3_ep0_xfernotready(dwc, event); |
| 843 | break; |
| 844 | |
| 845 | case DWC3_DEPEVT_XFERINPROGRESS: |
| 846 | case DWC3_DEPEVT_RXTXFIFOEVT: |
| 847 | case DWC3_DEPEVT_STREAMEVT: |
| 848 | case DWC3_DEPEVT_EPCMDCMPLT: |
| 849 | break; |
| 850 | } |
| 851 | } |