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 | |
Felipe Balbi | 788a23f | 2012-05-21 14:22:41 +0300 | [diff] [blame] | 57 | static void __dwc3_ep0_do_control_status(struct dwc3 *dwc, struct dwc3_ep *dep); |
Felipe Balbi | a080788 | 2012-05-04 13:03:54 +0300 | [diff] [blame] | 58 | static void __dwc3_ep0_do_control_data(struct dwc3 *dwc, |
| 59 | struct dwc3_ep *dep, struct dwc3_request *req); |
Sebastian Andrzej Siewior | 5bdb1dc | 2011-11-02 13:30:45 +0100 | [diff] [blame] | 60 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 61 | static const char *dwc3_ep0_state_string(enum dwc3_ep0_state state) |
| 62 | { |
| 63 | switch (state) { |
| 64 | case EP0_UNCONNECTED: |
| 65 | return "Unconnected"; |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 66 | case EP0_SETUP_PHASE: |
| 67 | return "Setup Phase"; |
| 68 | case EP0_DATA_PHASE: |
| 69 | return "Data Phase"; |
| 70 | case EP0_STATUS_PHASE: |
| 71 | return "Status Phase"; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 72 | default: |
| 73 | return "UNKNOWN"; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | 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] | 78 | u32 len, u32 type) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 79 | { |
| 80 | struct dwc3_gadget_ep_cmd_params params; |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 81 | struct dwc3_trb *trb; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 82 | struct dwc3_ep *dep; |
| 83 | |
| 84 | int ret; |
| 85 | |
| 86 | dep = dwc->eps[epnum]; |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 87 | if (dep->flags & DWC3_EP_BUSY) { |
| 88 | dev_vdbg(dwc->dev, "%s: still busy\n", dep->name); |
| 89 | return 0; |
| 90 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 91 | |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 92 | trb = dwc->ep0_trb; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 93 | |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 94 | trb->bpl = lower_32_bits(buf_dma); |
| 95 | trb->bph = upper_32_bits(buf_dma); |
| 96 | trb->size = len; |
| 97 | trb->ctrl = type; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 98 | |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 99 | trb->ctrl |= (DWC3_TRB_CTRL_HWO |
| 100 | | DWC3_TRB_CTRL_LST |
| 101 | | DWC3_TRB_CTRL_IOC |
| 102 | | DWC3_TRB_CTRL_ISP_IMI); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 103 | |
| 104 | memset(¶ms, 0, sizeof(params)); |
Felipe Balbi | dc1c70a | 2011-09-30 10:58:51 +0300 | [diff] [blame] | 105 | params.param0 = upper_32_bits(dwc->ep0_trb_addr); |
| 106 | params.param1 = lower_32_bits(dwc->ep0_trb_addr); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 107 | |
| 108 | ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, |
| 109 | DWC3_DEPCMD_STARTTRANSFER, ¶ms); |
| 110 | if (ret < 0) { |
| 111 | dev_dbg(dwc->dev, "failed to send STARTTRANSFER command\n"); |
| 112 | return ret; |
| 113 | } |
| 114 | |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 115 | dep->flags |= DWC3_EP_BUSY; |
Felipe Balbi | b4996a8 | 2012-06-06 12:04:13 +0300 | [diff] [blame] | 116 | dep->resource_index = dwc3_gadget_ep_get_transfer_index(dwc, |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 117 | dep->number); |
| 118 | |
Felipe Balbi | 1ddcb21 | 2011-08-30 15:52:17 +0300 | [diff] [blame] | 119 | dwc->ep0_next_event = DWC3_EP0_COMPLETE; |
| 120 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | static int __dwc3_gadget_ep0_queue(struct dwc3_ep *dep, |
| 125 | struct dwc3_request *req) |
| 126 | { |
Sebastian Andrzej Siewior | 5bdb1dc | 2011-11-02 13:30:45 +0100 | [diff] [blame] | 127 | struct dwc3 *dwc = dep->dwc; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 128 | |
| 129 | req->request.actual = 0; |
| 130 | req->request.status = -EINPROGRESS; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 131 | req->epnum = dep->number; |
| 132 | |
| 133 | list_add_tail(&req->list, &dep->request_list); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 134 | |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 135 | /* |
| 136 | * Gadget driver might not be quick enough to queue a request |
| 137 | * before we get a Transfer Not Ready event on this endpoint. |
| 138 | * |
| 139 | * In that case, we will set DWC3_EP_PENDING_REQUEST. When that |
| 140 | * flag is set, it's telling us that as soon as Gadget queues the |
| 141 | * required request, we should kick the transfer here because the |
| 142 | * IRQ we were waiting for is long gone. |
| 143 | */ |
| 144 | if (dep->flags & DWC3_EP_PENDING_REQUEST) { |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 145 | unsigned direction; |
Felipe Balbi | a682970 | 2011-08-27 22:18:09 +0300 | [diff] [blame] | 146 | |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 147 | direction = !!(dep->flags & DWC3_EP0_DIR_IN); |
Felipe Balbi | a682970 | 2011-08-27 22:18:09 +0300 | [diff] [blame] | 148 | |
Felipe Balbi | 68d8a78 | 2011-12-29 06:32:29 +0200 | [diff] [blame] | 149 | if (dwc->ep0state != EP0_DATA_PHASE) { |
| 150 | dev_WARN(dwc->dev, "Unexpected pending request\n"); |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 151 | return 0; |
| 152 | } |
Felipe Balbi | a682970 | 2011-08-27 22:18:09 +0300 | [diff] [blame] | 153 | |
Felipe Balbi | a080788 | 2012-05-04 13:03:54 +0300 | [diff] [blame] | 154 | __dwc3_ep0_do_control_data(dwc, dwc->eps[direction], req); |
| 155 | |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 156 | dep->flags &= ~(DWC3_EP_PENDING_REQUEST | |
| 157 | DWC3_EP0_DIR_IN); |
Felipe Balbi | d9b33c6 | 2012-07-19 08:51:13 +0300 | [diff] [blame^] | 158 | |
| 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | /* |
| 163 | * In case gadget driver asked us to delay the STATUS phase, |
| 164 | * handle it here. |
| 165 | */ |
| 166 | if (dwc->delayed_status) { |
Sebastian Andrzej Siewior | 5bdb1dc | 2011-11-02 13:30:45 +0100 | [diff] [blame] | 167 | dwc->delayed_status = false; |
Felipe Balbi | 68d3e66 | 2011-12-08 13:56:27 +0200 | [diff] [blame] | 168 | |
| 169 | if (dwc->ep0state == EP0_STATUS_PHASE) |
Felipe Balbi | 788a23f | 2012-05-21 14:22:41 +0300 | [diff] [blame] | 170 | __dwc3_ep0_do_control_status(dwc, dwc->eps[1]); |
Felipe Balbi | 68d3e66 | 2011-12-08 13:56:27 +0200 | [diff] [blame] | 171 | else |
| 172 | dev_dbg(dwc->dev, "too early for delayed status\n"); |
Felipe Balbi | d9b33c6 | 2012-07-19 08:51:13 +0300 | [diff] [blame^] | 173 | |
| 174 | return 0; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 175 | } |
| 176 | |
Felipe Balbi | 35f7569 | 2012-07-19 08:49:01 +0300 | [diff] [blame] | 177 | return 0; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | int dwc3_gadget_ep0_queue(struct usb_ep *ep, struct usb_request *request, |
| 181 | gfp_t gfp_flags) |
| 182 | { |
| 183 | struct dwc3_request *req = to_dwc3_request(request); |
| 184 | struct dwc3_ep *dep = to_dwc3_ep(ep); |
| 185 | struct dwc3 *dwc = dep->dwc; |
| 186 | |
| 187 | unsigned long flags; |
| 188 | |
| 189 | int ret; |
| 190 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 191 | spin_lock_irqsave(&dwc->lock, flags); |
Ido Shayevitz | 16e78db | 2012-03-12 20:25:24 +0200 | [diff] [blame] | 192 | if (!dep->endpoint.desc) { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 193 | dev_dbg(dwc->dev, "trying to queue request %p to disabled %s\n", |
| 194 | request, dep->name); |
| 195 | ret = -ESHUTDOWN; |
| 196 | goto out; |
| 197 | } |
| 198 | |
| 199 | /* we share one TRB for ep0/1 */ |
Sebastian Andrzej Siewior | c2da2ff | 2011-10-20 19:04:16 +0200 | [diff] [blame] | 200 | if (!list_empty(&dep->request_list)) { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 201 | ret = -EBUSY; |
| 202 | goto out; |
| 203 | } |
| 204 | |
| 205 | dev_vdbg(dwc->dev, "queueing request %p to %s length %d, state '%s'\n", |
| 206 | request, dep->name, request->length, |
| 207 | dwc3_ep0_state_string(dwc->ep0state)); |
| 208 | |
| 209 | ret = __dwc3_gadget_ep0_queue(dep, req); |
| 210 | |
| 211 | out: |
| 212 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 213 | |
| 214 | return ret; |
| 215 | } |
| 216 | |
| 217 | static void dwc3_ep0_stall_and_restart(struct dwc3 *dwc) |
| 218 | { |
Felipe Balbi | d742220 | 2011-09-08 18:17:12 +0300 | [diff] [blame] | 219 | struct dwc3_ep *dep = dwc->eps[0]; |
| 220 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 221 | /* stall is always issued on EP0 */ |
Sebastian Andrzej Siewior | c2da2ff | 2011-10-20 19:04:16 +0200 | [diff] [blame] | 222 | __dwc3_gadget_ep_set_halt(dep, 1); |
| 223 | dep->flags = DWC3_EP_ENABLED; |
Sebastian Andrzej Siewior | 5bdb1dc | 2011-11-02 13:30:45 +0100 | [diff] [blame] | 224 | dwc->delayed_status = false; |
Felipe Balbi | d742220 | 2011-09-08 18:17:12 +0300 | [diff] [blame] | 225 | |
| 226 | if (!list_empty(&dep->request_list)) { |
| 227 | struct dwc3_request *req; |
| 228 | |
| 229 | req = next_request(&dep->request_list); |
| 230 | dwc3_gadget_giveback(dep, req, -ECONNRESET); |
| 231 | } |
| 232 | |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 233 | dwc->ep0state = EP0_SETUP_PHASE; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 234 | dwc3_ep0_out_start(dwc); |
| 235 | } |
| 236 | |
Pratyush Anand | 08f0d96 | 2012-06-25 22:40:43 +0530 | [diff] [blame] | 237 | int dwc3_gadget_ep0_set_halt(struct usb_ep *ep, int value) |
| 238 | { |
| 239 | struct dwc3_ep *dep = to_dwc3_ep(ep); |
| 240 | struct dwc3 *dwc = dep->dwc; |
| 241 | |
| 242 | dwc3_ep0_stall_and_restart(dwc); |
| 243 | |
| 244 | return 0; |
| 245 | } |
| 246 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 247 | void dwc3_ep0_out_start(struct dwc3 *dwc) |
| 248 | { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 249 | int ret; |
| 250 | |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 251 | ret = dwc3_ep0_start_trans(dwc, 0, dwc->ctrl_req_addr, 8, |
| 252 | DWC3_TRBCTL_CONTROL_SETUP); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 253 | WARN_ON(ret < 0); |
| 254 | } |
| 255 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 256 | static struct dwc3_ep *dwc3_wIndex_to_dep(struct dwc3 *dwc, __le16 wIndex_le) |
| 257 | { |
| 258 | struct dwc3_ep *dep; |
| 259 | u32 windex = le16_to_cpu(wIndex_le); |
| 260 | u32 epnum; |
| 261 | |
| 262 | epnum = (windex & USB_ENDPOINT_NUMBER_MASK) << 1; |
| 263 | if ((windex & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) |
| 264 | epnum |= 1; |
| 265 | |
| 266 | dep = dwc->eps[epnum]; |
| 267 | if (dep->flags & DWC3_EP_ENABLED) |
| 268 | return dep; |
| 269 | |
| 270 | return NULL; |
| 271 | } |
| 272 | |
Sebastian Andrzej Siewior | 8ee6270 | 2011-10-18 19:13:29 +0200 | [diff] [blame] | 273 | 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] | 274 | { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 275 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 276 | /* |
| 277 | * ch 9.4.5 |
| 278 | */ |
Felipe Balbi | 25b8ff6 | 2011-11-04 12:32:47 +0200 | [diff] [blame] | 279 | static int dwc3_ep0_handle_status(struct dwc3 *dwc, |
| 280 | struct usb_ctrlrequest *ctrl) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 281 | { |
| 282 | struct dwc3_ep *dep; |
| 283 | u32 recip; |
Sebastian Andrzej Siewior | e6a3b5e | 2011-09-13 17:54:39 +0200 | [diff] [blame] | 284 | u32 reg; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 285 | u16 usb_status = 0; |
| 286 | __le16 *response_pkt; |
| 287 | |
| 288 | recip = ctrl->bRequestType & USB_RECIP_MASK; |
| 289 | switch (recip) { |
| 290 | case USB_RECIP_DEVICE: |
| 291 | /* |
Sebastian Andrzej Siewior | e6a3b5e | 2011-09-13 17:54:39 +0200 | [diff] [blame] | 292 | * LTM will be set once we know how to set this in HW. |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 293 | */ |
| 294 | usb_status |= dwc->is_selfpowered << USB_DEVICE_SELF_POWERED; |
Sebastian Andrzej Siewior | e6a3b5e | 2011-09-13 17:54:39 +0200 | [diff] [blame] | 295 | |
| 296 | if (dwc->speed == DWC3_DSTS_SUPERSPEED) { |
| 297 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
| 298 | if (reg & DWC3_DCTL_INITU1ENA) |
| 299 | usb_status |= 1 << USB_DEV_STAT_U1_ENABLED; |
| 300 | if (reg & DWC3_DCTL_INITU2ENA) |
| 301 | usb_status |= 1 << USB_DEV_STAT_U2_ENABLED; |
| 302 | } |
| 303 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 304 | break; |
| 305 | |
| 306 | case USB_RECIP_INTERFACE: |
| 307 | /* |
| 308 | * Function Remote Wake Capable D0 |
| 309 | * Function Remote Wakeup D1 |
| 310 | */ |
| 311 | break; |
| 312 | |
| 313 | case USB_RECIP_ENDPOINT: |
| 314 | dep = dwc3_wIndex_to_dep(dwc, ctrl->wIndex); |
| 315 | if (!dep) |
Felipe Balbi | 25b8ff6 | 2011-11-04 12:32:47 +0200 | [diff] [blame] | 316 | return -EINVAL; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 317 | |
| 318 | if (dep->flags & DWC3_EP_STALL) |
| 319 | usb_status = 1 << USB_ENDPOINT_HALT; |
| 320 | break; |
| 321 | default: |
| 322 | return -EINVAL; |
| 323 | }; |
| 324 | |
| 325 | response_pkt = (__le16 *) dwc->setup_buf; |
| 326 | *response_pkt = cpu_to_le16(usb_status); |
Felipe Balbi | e261779 | 2011-11-29 10:35:47 +0200 | [diff] [blame] | 327 | |
| 328 | dep = dwc->eps[0]; |
| 329 | dwc->ep0_usb_req.dep = dep; |
Sebastian Andrzej Siewior | e0ce0b0 | 2011-11-25 12:03:46 +0100 | [diff] [blame] | 330 | dwc->ep0_usb_req.request.length = sizeof(*response_pkt); |
Felipe Balbi | 0fc9a1b | 2011-12-19 11:32:34 +0200 | [diff] [blame] | 331 | dwc->ep0_usb_req.request.buf = dwc->setup_buf; |
Sebastian Andrzej Siewior | e0ce0b0 | 2011-11-25 12:03:46 +0100 | [diff] [blame] | 332 | dwc->ep0_usb_req.request.complete = dwc3_ep0_status_cmpl; |
Felipe Balbi | e261779 | 2011-11-29 10:35:47 +0200 | [diff] [blame] | 333 | |
| 334 | return __dwc3_gadget_ep0_queue(dep, &dwc->ep0_usb_req); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | static int dwc3_ep0_handle_feature(struct dwc3 *dwc, |
| 338 | struct usb_ctrlrequest *ctrl, int set) |
| 339 | { |
| 340 | struct dwc3_ep *dep; |
| 341 | u32 recip; |
| 342 | u32 wValue; |
| 343 | u32 wIndex; |
Sebastian Andrzej Siewior | e6a3b5e | 2011-09-13 17:54:39 +0200 | [diff] [blame] | 344 | u32 reg; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 345 | int ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 346 | |
| 347 | wValue = le16_to_cpu(ctrl->wValue); |
| 348 | wIndex = le16_to_cpu(ctrl->wIndex); |
| 349 | recip = ctrl->bRequestType & USB_RECIP_MASK; |
| 350 | switch (recip) { |
| 351 | case USB_RECIP_DEVICE: |
| 352 | |
Sebastian Andrzej Siewior | e6a3b5e | 2011-09-13 17:54:39 +0200 | [diff] [blame] | 353 | switch (wValue) { |
| 354 | case USB_DEVICE_REMOTE_WAKEUP: |
| 355 | break; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 356 | /* |
| 357 | * 9.4.1 says only only for SS, in AddressState only for |
| 358 | * default control pipe |
| 359 | */ |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 360 | case USB_DEVICE_U1_ENABLE: |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 361 | if (dwc->dev_state != DWC3_CONFIGURED_STATE) |
| 362 | return -EINVAL; |
| 363 | if (dwc->speed != DWC3_DSTS_SUPERSPEED) |
| 364 | return -EINVAL; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 365 | |
Sebastian Andrzej Siewior | e6a3b5e | 2011-09-13 17:54:39 +0200 | [diff] [blame] | 366 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
| 367 | if (set) |
| 368 | reg |= DWC3_DCTL_INITU1ENA; |
| 369 | else |
| 370 | reg &= ~DWC3_DCTL_INITU1ENA; |
| 371 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 372 | break; |
Sebastian Andrzej Siewior | e6a3b5e | 2011-09-13 17:54:39 +0200 | [diff] [blame] | 373 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 374 | case USB_DEVICE_U2_ENABLE: |
Sebastian Andrzej Siewior | e6a3b5e | 2011-09-13 17:54:39 +0200 | [diff] [blame] | 375 | if (dwc->dev_state != DWC3_CONFIGURED_STATE) |
| 376 | return -EINVAL; |
| 377 | if (dwc->speed != DWC3_DSTS_SUPERSPEED) |
| 378 | return -EINVAL; |
| 379 | |
| 380 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
| 381 | if (set) |
| 382 | reg |= DWC3_DCTL_INITU2ENA; |
| 383 | else |
| 384 | reg &= ~DWC3_DCTL_INITU2ENA; |
| 385 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 386 | break; |
Sebastian Andrzej Siewior | e6a3b5e | 2011-09-13 17:54:39 +0200 | [diff] [blame] | 387 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 388 | case USB_DEVICE_LTM_ENABLE: |
Sebastian Andrzej Siewior | e6a3b5e | 2011-09-13 17:54:39 +0200 | [diff] [blame] | 389 | return -EINVAL; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 390 | break; |
| 391 | |
| 392 | case USB_DEVICE_TEST_MODE: |
| 393 | if ((wIndex & 0xff) != 0) |
| 394 | return -EINVAL; |
| 395 | if (!set) |
| 396 | return -EINVAL; |
| 397 | |
Gerard Cauvy | 3b63736 | 2012-02-10 12:21:18 +0200 | [diff] [blame] | 398 | dwc->test_mode_nr = wIndex >> 8; |
| 399 | dwc->test_mode = true; |
Gerard Cauvy | ecb0779 | 2012-03-16 16:20:10 +0200 | [diff] [blame] | 400 | break; |
| 401 | default: |
| 402 | return -EINVAL; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 403 | } |
| 404 | break; |
| 405 | |
| 406 | case USB_RECIP_INTERFACE: |
| 407 | switch (wValue) { |
| 408 | case USB_INTRF_FUNC_SUSPEND: |
| 409 | if (wIndex & USB_INTRF_FUNC_SUSPEND_LP) |
| 410 | /* XXX enable Low power suspend */ |
| 411 | ; |
| 412 | if (wIndex & USB_INTRF_FUNC_SUSPEND_RW) |
| 413 | /* XXX enable remote wakeup */ |
| 414 | ; |
| 415 | break; |
| 416 | default: |
| 417 | return -EINVAL; |
| 418 | } |
| 419 | break; |
| 420 | |
| 421 | case USB_RECIP_ENDPOINT: |
| 422 | switch (wValue) { |
| 423 | case USB_ENDPOINT_HALT: |
Paul Zimmerman | 1d04679 | 2012-02-15 18:56:56 -0800 | [diff] [blame] | 424 | dep = dwc3_wIndex_to_dep(dwc, wIndex); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 425 | if (!dep) |
| 426 | return -EINVAL; |
| 427 | ret = __dwc3_gadget_ep_set_halt(dep, set); |
| 428 | if (ret) |
| 429 | return -EINVAL; |
| 430 | break; |
| 431 | default: |
| 432 | return -EINVAL; |
| 433 | } |
| 434 | break; |
| 435 | |
| 436 | default: |
| 437 | return -EINVAL; |
| 438 | }; |
| 439 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 440 | return 0; |
| 441 | } |
| 442 | |
| 443 | static int dwc3_ep0_set_address(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl) |
| 444 | { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 445 | u32 addr; |
| 446 | u32 reg; |
| 447 | |
| 448 | addr = le16_to_cpu(ctrl->wValue); |
Felipe Balbi | f96a6ec | 2011-10-15 21:37:35 +0300 | [diff] [blame] | 449 | if (addr > 127) { |
| 450 | dev_dbg(dwc->dev, "invalid device address %d\n", addr); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 451 | return -EINVAL; |
Felipe Balbi | f96a6ec | 2011-10-15 21:37:35 +0300 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | if (dwc->dev_state == DWC3_CONFIGURED_STATE) { |
| 455 | dev_dbg(dwc->dev, "trying to set address when configured\n"); |
| 456 | return -EINVAL; |
| 457 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 458 | |
Felipe Balbi | 2646021 | 2011-09-30 10:58:36 +0300 | [diff] [blame] | 459 | reg = dwc3_readl(dwc->regs, DWC3_DCFG); |
| 460 | reg &= ~(DWC3_DCFG_DEVADDR_MASK); |
| 461 | reg |= DWC3_DCFG_DEVADDR(addr); |
| 462 | dwc3_writel(dwc->regs, DWC3_DCFG, reg); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 463 | |
Felipe Balbi | 2646021 | 2011-09-30 10:58:36 +0300 | [diff] [blame] | 464 | if (addr) |
| 465 | dwc->dev_state = DWC3_ADDRESS_STATE; |
| 466 | else |
| 467 | dwc->dev_state = DWC3_DEFAULT_STATE; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 468 | |
Felipe Balbi | 2646021 | 2011-09-30 10:58:36 +0300 | [diff] [blame] | 469 | return 0; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | static int dwc3_ep0_delegate_req(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl) |
| 473 | { |
| 474 | int ret; |
| 475 | |
| 476 | spin_unlock(&dwc->lock); |
| 477 | ret = dwc->gadget_driver->setup(&dwc->gadget, ctrl); |
| 478 | spin_lock(&dwc->lock); |
| 479 | return ret; |
| 480 | } |
| 481 | |
| 482 | static int dwc3_ep0_set_config(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl) |
| 483 | { |
| 484 | u32 cfg; |
| 485 | int ret; |
Pratyush Anand | e274a31 | 2012-07-02 10:21:54 +0530 | [diff] [blame] | 486 | u32 reg; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 487 | |
Paul Zimmerman | b23c843 | 2011-09-30 10:58:42 +0300 | [diff] [blame] | 488 | dwc->start_config_issued = false; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 489 | cfg = le16_to_cpu(ctrl->wValue); |
| 490 | |
| 491 | switch (dwc->dev_state) { |
| 492 | case DWC3_DEFAULT_STATE: |
| 493 | return -EINVAL; |
| 494 | break; |
| 495 | |
| 496 | case DWC3_ADDRESS_STATE: |
| 497 | ret = dwc3_ep0_delegate_req(dwc, ctrl); |
| 498 | /* if the cfg matches and the cfg is non zero */ |
Felipe Balbi | 457e84b | 2012-01-18 18:04:09 +0200 | [diff] [blame] | 499 | if (cfg && (!ret || (ret == USB_GADGET_DELAYED_STATUS))) { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 500 | dwc->dev_state = DWC3_CONFIGURED_STATE; |
Pratyush Anand | e274a31 | 2012-07-02 10:21:54 +0530 | [diff] [blame] | 501 | /* |
| 502 | * Enable transition to U1/U2 state when |
| 503 | * nothing is pending from application. |
| 504 | */ |
| 505 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
| 506 | reg |= (DWC3_DCTL_ACCEPTU1ENA | DWC3_DCTL_ACCEPTU2ENA); |
| 507 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); |
| 508 | |
Felipe Balbi | 457e84b | 2012-01-18 18:04:09 +0200 | [diff] [blame] | 509 | dwc->resize_fifos = true; |
| 510 | dev_dbg(dwc->dev, "resize fifos flag SET\n"); |
| 511 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 512 | break; |
| 513 | |
| 514 | case DWC3_CONFIGURED_STATE: |
| 515 | ret = dwc3_ep0_delegate_req(dwc, ctrl); |
| 516 | if (!cfg) |
| 517 | dwc->dev_state = DWC3_ADDRESS_STATE; |
| 518 | break; |
Sebastian Andrzej Siewior | 5bdb1dc | 2011-11-02 13:30:45 +0100 | [diff] [blame] | 519 | default: |
| 520 | ret = -EINVAL; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 521 | } |
Sebastian Andrzej Siewior | 5bdb1dc | 2011-11-02 13:30:45 +0100 | [diff] [blame] | 522 | return ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 523 | } |
| 524 | |
Felipe Balbi | 865e09e | 2012-04-24 16:19:49 +0300 | [diff] [blame] | 525 | static void dwc3_ep0_set_sel_cmpl(struct usb_ep *ep, struct usb_request *req) |
| 526 | { |
| 527 | struct dwc3_ep *dep = to_dwc3_ep(ep); |
| 528 | struct dwc3 *dwc = dep->dwc; |
| 529 | |
| 530 | u32 param = 0; |
| 531 | u32 reg; |
| 532 | |
| 533 | struct timing { |
| 534 | u8 u1sel; |
| 535 | u8 u1pel; |
| 536 | u16 u2sel; |
| 537 | u16 u2pel; |
| 538 | } __packed timing; |
| 539 | |
| 540 | int ret; |
| 541 | |
| 542 | memcpy(&timing, req->buf, sizeof(timing)); |
| 543 | |
| 544 | dwc->u1sel = timing.u1sel; |
| 545 | dwc->u1pel = timing.u1pel; |
Felipe Balbi | c8cf7af | 2012-05-31 11:00:28 +0300 | [diff] [blame] | 546 | dwc->u2sel = le16_to_cpu(timing.u2sel); |
| 547 | dwc->u2pel = le16_to_cpu(timing.u2pel); |
Felipe Balbi | 865e09e | 2012-04-24 16:19:49 +0300 | [diff] [blame] | 548 | |
| 549 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
| 550 | if (reg & DWC3_DCTL_INITU2ENA) |
| 551 | param = dwc->u2pel; |
| 552 | if (reg & DWC3_DCTL_INITU1ENA) |
| 553 | param = dwc->u1pel; |
| 554 | |
| 555 | /* |
| 556 | * According to Synopsys Databook, if parameter is |
| 557 | * greater than 125, a value of zero should be |
| 558 | * programmed in the register. |
| 559 | */ |
| 560 | if (param > 125) |
| 561 | param = 0; |
| 562 | |
| 563 | /* now that we have the time, issue DGCMD Set Sel */ |
| 564 | ret = dwc3_send_gadget_generic_command(dwc, |
| 565 | DWC3_DGCMD_SET_PERIODIC_PAR, param); |
| 566 | WARN_ON(ret < 0); |
| 567 | } |
| 568 | |
| 569 | static int dwc3_ep0_set_sel(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl) |
| 570 | { |
| 571 | struct dwc3_ep *dep; |
| 572 | u16 wLength; |
| 573 | u16 wValue; |
| 574 | |
| 575 | if (dwc->dev_state == DWC3_DEFAULT_STATE) |
| 576 | return -EINVAL; |
| 577 | |
| 578 | wValue = le16_to_cpu(ctrl->wValue); |
| 579 | wLength = le16_to_cpu(ctrl->wLength); |
| 580 | |
| 581 | if (wLength != 6) { |
| 582 | dev_err(dwc->dev, "Set SEL should be 6 bytes, got %d\n", |
| 583 | wLength); |
| 584 | return -EINVAL; |
| 585 | } |
| 586 | |
| 587 | /* |
| 588 | * To handle Set SEL we need to receive 6 bytes from Host. So let's |
| 589 | * queue a usb_request for 6 bytes. |
| 590 | * |
| 591 | * Remember, though, this controller can't handle non-wMaxPacketSize |
| 592 | * aligned transfers on the OUT direction, so we queue a request for |
| 593 | * wMaxPacketSize instead. |
| 594 | */ |
| 595 | dep = dwc->eps[0]; |
| 596 | dwc->ep0_usb_req.dep = dep; |
| 597 | dwc->ep0_usb_req.request.length = dep->endpoint.maxpacket; |
| 598 | dwc->ep0_usb_req.request.buf = dwc->setup_buf; |
| 599 | dwc->ep0_usb_req.request.complete = dwc3_ep0_set_sel_cmpl; |
| 600 | |
| 601 | return __dwc3_gadget_ep0_queue(dep, &dwc->ep0_usb_req); |
| 602 | } |
| 603 | |
Felipe Balbi | c12a0d8 | 2012-04-25 10:45:05 +0300 | [diff] [blame] | 604 | static int dwc3_ep0_set_isoch_delay(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl) |
| 605 | { |
| 606 | u16 wLength; |
| 607 | u16 wValue; |
| 608 | u16 wIndex; |
| 609 | |
| 610 | wValue = le16_to_cpu(ctrl->wValue); |
| 611 | wLength = le16_to_cpu(ctrl->wLength); |
| 612 | wIndex = le16_to_cpu(ctrl->wIndex); |
| 613 | |
| 614 | if (wIndex || wLength) |
| 615 | return -EINVAL; |
| 616 | |
| 617 | /* |
| 618 | * REVISIT It's unclear from Databook what to do with this |
| 619 | * value. For now, just cache it. |
| 620 | */ |
| 621 | dwc->isoch_delay = wValue; |
| 622 | |
| 623 | return 0; |
| 624 | } |
| 625 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 626 | static int dwc3_ep0_std_request(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl) |
| 627 | { |
| 628 | int ret; |
| 629 | |
| 630 | switch (ctrl->bRequest) { |
| 631 | case USB_REQ_GET_STATUS: |
| 632 | dev_vdbg(dwc->dev, "USB_REQ_GET_STATUS\n"); |
| 633 | ret = dwc3_ep0_handle_status(dwc, ctrl); |
| 634 | break; |
| 635 | case USB_REQ_CLEAR_FEATURE: |
| 636 | dev_vdbg(dwc->dev, "USB_REQ_CLEAR_FEATURE\n"); |
| 637 | ret = dwc3_ep0_handle_feature(dwc, ctrl, 0); |
| 638 | break; |
| 639 | case USB_REQ_SET_FEATURE: |
| 640 | dev_vdbg(dwc->dev, "USB_REQ_SET_FEATURE\n"); |
| 641 | ret = dwc3_ep0_handle_feature(dwc, ctrl, 1); |
| 642 | break; |
| 643 | case USB_REQ_SET_ADDRESS: |
| 644 | dev_vdbg(dwc->dev, "USB_REQ_SET_ADDRESS\n"); |
| 645 | ret = dwc3_ep0_set_address(dwc, ctrl); |
| 646 | break; |
| 647 | case USB_REQ_SET_CONFIGURATION: |
| 648 | dev_vdbg(dwc->dev, "USB_REQ_SET_CONFIGURATION\n"); |
| 649 | ret = dwc3_ep0_set_config(dwc, ctrl); |
| 650 | break; |
Felipe Balbi | 865e09e | 2012-04-24 16:19:49 +0300 | [diff] [blame] | 651 | case USB_REQ_SET_SEL: |
| 652 | dev_vdbg(dwc->dev, "USB_REQ_SET_SEL\n"); |
| 653 | ret = dwc3_ep0_set_sel(dwc, ctrl); |
| 654 | break; |
Felipe Balbi | c12a0d8 | 2012-04-25 10:45:05 +0300 | [diff] [blame] | 655 | case USB_REQ_SET_ISOCH_DELAY: |
| 656 | dev_vdbg(dwc->dev, "USB_REQ_SET_ISOCH_DELAY\n"); |
| 657 | ret = dwc3_ep0_set_isoch_delay(dwc, ctrl); |
| 658 | break; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 659 | default: |
| 660 | dev_vdbg(dwc->dev, "Forwarding to gadget driver\n"); |
| 661 | ret = dwc3_ep0_delegate_req(dwc, ctrl); |
| 662 | break; |
| 663 | }; |
| 664 | |
| 665 | return ret; |
| 666 | } |
| 667 | |
| 668 | static void dwc3_ep0_inspect_setup(struct dwc3 *dwc, |
| 669 | const struct dwc3_event_depevt *event) |
| 670 | { |
| 671 | struct usb_ctrlrequest *ctrl = dwc->ctrl_req; |
Felipe Balbi | ef21ede | 2012-05-31 10:29:49 +0300 | [diff] [blame] | 672 | int ret = -EINVAL; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 673 | u32 len; |
| 674 | |
| 675 | if (!dwc->gadget_driver) |
Felipe Balbi | ef21ede | 2012-05-31 10:29:49 +0300 | [diff] [blame] | 676 | goto out; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 677 | |
| 678 | len = le16_to_cpu(ctrl->wLength); |
Felipe Balbi | 1ddcb21 | 2011-08-30 15:52:17 +0300 | [diff] [blame] | 679 | if (!len) { |
Felipe Balbi | d95b09b | 2011-09-30 10:58:37 +0300 | [diff] [blame] | 680 | dwc->three_stage_setup = false; |
| 681 | dwc->ep0_expect_in = false; |
Felipe Balbi | 1ddcb21 | 2011-08-30 15:52:17 +0300 | [diff] [blame] | 682 | dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS; |
| 683 | } else { |
Felipe Balbi | d95b09b | 2011-09-30 10:58:37 +0300 | [diff] [blame] | 684 | dwc->three_stage_setup = true; |
| 685 | dwc->ep0_expect_in = !!(ctrl->bRequestType & USB_DIR_IN); |
Felipe Balbi | 1ddcb21 | 2011-08-30 15:52:17 +0300 | [diff] [blame] | 686 | dwc->ep0_next_event = DWC3_EP0_NRDY_DATA; |
| 687 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 688 | |
| 689 | if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) |
| 690 | ret = dwc3_ep0_std_request(dwc, ctrl); |
| 691 | else |
| 692 | ret = dwc3_ep0_delegate_req(dwc, ctrl); |
| 693 | |
Sebastian Andrzej Siewior | 5bdb1dc | 2011-11-02 13:30:45 +0100 | [diff] [blame] | 694 | if (ret == USB_GADGET_DELAYED_STATUS) |
| 695 | dwc->delayed_status = true; |
| 696 | |
Felipe Balbi | ef21ede | 2012-05-31 10:29:49 +0300 | [diff] [blame] | 697 | out: |
| 698 | if (ret < 0) |
| 699 | dwc3_ep0_stall_and_restart(dwc); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | static void dwc3_ep0_complete_data(struct dwc3 *dwc, |
| 703 | const struct dwc3_event_depevt *event) |
| 704 | { |
| 705 | struct dwc3_request *r = NULL; |
| 706 | struct usb_request *ur; |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 707 | struct dwc3_trb *trb; |
Sebastian Andrzej Siewior | c2da2ff | 2011-10-20 19:04:16 +0200 | [diff] [blame] | 708 | struct dwc3_ep *ep0; |
Felipe Balbi | c611ccb | 2011-08-27 02:30:33 +0300 | [diff] [blame] | 709 | u32 transferred; |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 710 | u32 length; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 711 | u8 epnum; |
| 712 | |
| 713 | epnum = event->endpoint_number; |
Sebastian Andrzej Siewior | c2da2ff | 2011-10-20 19:04:16 +0200 | [diff] [blame] | 714 | ep0 = dwc->eps[0]; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 715 | |
Felipe Balbi | 1ddcb21 | 2011-08-30 15:52:17 +0300 | [diff] [blame] | 716 | dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS; |
| 717 | |
Sebastian Andrzej Siewior | c2da2ff | 2011-10-20 19:04:16 +0200 | [diff] [blame] | 718 | r = next_request(&ep0->request_list); |
Sebastian Andrzej Siewior | 8ee6270 | 2011-10-18 19:13:29 +0200 | [diff] [blame] | 719 | ur = &r->request; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 720 | |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 721 | trb = dwc->ep0_trb; |
| 722 | length = trb->size & DWC3_TRB_SIZE_MASK; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 723 | |
Felipe Balbi | a682970 | 2011-08-27 22:18:09 +0300 | [diff] [blame] | 724 | if (dwc->ep0_bounced) { |
Moiz Sonasath | 566ccdd | 2012-03-14 00:44:56 -0500 | [diff] [blame] | 725 | unsigned transfer_size = ur->length; |
| 726 | unsigned maxp = ep0->endpoint.maxpacket; |
| 727 | |
| 728 | transfer_size += (maxp - (transfer_size % maxp)); |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 729 | transferred = min_t(u32, ur->length, |
Moiz Sonasath | 566ccdd | 2012-03-14 00:44:56 -0500 | [diff] [blame] | 730 | transfer_size - length); |
Felipe Balbi | a682970 | 2011-08-27 22:18:09 +0300 | [diff] [blame] | 731 | memcpy(ur->buf, dwc->ep0_bounce, transferred); |
| 732 | dwc->ep0_bounced = false; |
| 733 | } else { |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 734 | transferred = ur->length - length; |
Felipe Balbi | a682970 | 2011-08-27 22:18:09 +0300 | [diff] [blame] | 735 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 736 | |
Felipe Balbi | cd423dd | 2012-03-21 11:44:00 +0200 | [diff] [blame] | 737 | ur->actual += transferred; |
| 738 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 739 | if ((epnum & 1) && ur->actual < ur->length) { |
| 740 | /* for some reason we did not get everything out */ |
| 741 | |
| 742 | dwc3_ep0_stall_and_restart(dwc); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 743 | } else { |
| 744 | /* |
| 745 | * handle the case where we have to send a zero packet. This |
| 746 | * seems to be case when req.length > maxpacket. Could it be? |
| 747 | */ |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 748 | if (r) |
Sebastian Andrzej Siewior | c2da2ff | 2011-10-20 19:04:16 +0200 | [diff] [blame] | 749 | dwc3_gadget_giveback(ep0, r, 0); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 750 | } |
| 751 | } |
| 752 | |
Felipe Balbi | 85a7810 | 2012-05-31 12:32:37 +0300 | [diff] [blame] | 753 | static void dwc3_ep0_complete_status(struct dwc3 *dwc, |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 754 | const struct dwc3_event_depevt *event) |
| 755 | { |
| 756 | struct dwc3_request *r; |
| 757 | struct dwc3_ep *dep; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 758 | |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 759 | dep = dwc->eps[0]; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 760 | |
| 761 | if (!list_empty(&dep->request_list)) { |
| 762 | r = next_request(&dep->request_list); |
| 763 | |
| 764 | dwc3_gadget_giveback(dep, r, 0); |
| 765 | } |
| 766 | |
Gerard Cauvy | 3b63736 | 2012-02-10 12:21:18 +0200 | [diff] [blame] | 767 | if (dwc->test_mode) { |
| 768 | int ret; |
| 769 | |
| 770 | ret = dwc3_gadget_set_test_mode(dwc, dwc->test_mode_nr); |
| 771 | if (ret < 0) { |
| 772 | dev_dbg(dwc->dev, "Invalid Test #%d\n", |
| 773 | dwc->test_mode_nr); |
| 774 | dwc3_ep0_stall_and_restart(dwc); |
Felipe Balbi | 5c81aba | 2012-06-25 19:30:49 +0300 | [diff] [blame] | 775 | return; |
Gerard Cauvy | 3b63736 | 2012-02-10 12:21:18 +0200 | [diff] [blame] | 776 | } |
| 777 | } |
| 778 | |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 779 | dwc->ep0state = EP0_SETUP_PHASE; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 780 | dwc3_ep0_out_start(dwc); |
| 781 | } |
| 782 | |
| 783 | static void dwc3_ep0_xfer_complete(struct dwc3 *dwc, |
| 784 | const struct dwc3_event_depevt *event) |
| 785 | { |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 786 | struct dwc3_ep *dep = dwc->eps[event->endpoint_number]; |
| 787 | |
| 788 | dep->flags &= ~DWC3_EP_BUSY; |
Felipe Balbi | b4996a8 | 2012-06-06 12:04:13 +0300 | [diff] [blame] | 789 | dep->resource_index = 0; |
Felipe Balbi | df62df5 | 2011-10-14 15:11:49 +0300 | [diff] [blame] | 790 | dwc->setup_packet_pending = false; |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 791 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 792 | switch (dwc->ep0state) { |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 793 | case EP0_SETUP_PHASE: |
| 794 | dev_vdbg(dwc->dev, "Inspecting Setup Bytes\n"); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 795 | dwc3_ep0_inspect_setup(dwc, event); |
| 796 | break; |
| 797 | |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 798 | case EP0_DATA_PHASE: |
| 799 | dev_vdbg(dwc->dev, "Data Phase\n"); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 800 | dwc3_ep0_complete_data(dwc, event); |
| 801 | break; |
| 802 | |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 803 | case EP0_STATUS_PHASE: |
| 804 | dev_vdbg(dwc->dev, "Status Phase\n"); |
Felipe Balbi | 85a7810 | 2012-05-31 12:32:37 +0300 | [diff] [blame] | 805 | dwc3_ep0_complete_status(dwc, event); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 806 | break; |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 807 | default: |
| 808 | WARN(true, "UNKNOWN ep0state %d\n", dwc->ep0state); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 809 | } |
| 810 | } |
| 811 | |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 812 | static void dwc3_ep0_do_control_setup(struct dwc3 *dwc, |
| 813 | const struct dwc3_event_depevt *event) |
| 814 | { |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 815 | dwc3_ep0_out_start(dwc); |
| 816 | } |
| 817 | |
Felipe Balbi | a080788 | 2012-05-04 13:03:54 +0300 | [diff] [blame] | 818 | static void __dwc3_ep0_do_control_data(struct dwc3 *dwc, |
| 819 | struct dwc3_ep *dep, struct dwc3_request *req) |
| 820 | { |
| 821 | int ret; |
| 822 | |
| 823 | req->direction = !!dep->number; |
| 824 | |
| 825 | if (req->request.length == 0) { |
| 826 | ret = dwc3_ep0_start_trans(dwc, dep->number, |
| 827 | dwc->ctrl_req_addr, 0, |
| 828 | DWC3_TRBCTL_CONTROL_DATA); |
Felipe Balbi | c74c6d4 | 2012-05-04 13:08:22 +0300 | [diff] [blame] | 829 | } else if (!IS_ALIGNED(req->request.length, dep->endpoint.maxpacket) |
Felipe Balbi | a080788 | 2012-05-04 13:03:54 +0300 | [diff] [blame] | 830 | && (dep->number == 0)) { |
| 831 | u32 transfer_size; |
| 832 | |
| 833 | ret = usb_gadget_map_request(&dwc->gadget, &req->request, |
| 834 | dep->number); |
| 835 | if (ret) { |
| 836 | dev_dbg(dwc->dev, "failed to map request\n"); |
| 837 | return; |
| 838 | } |
| 839 | |
| 840 | WARN_ON(req->request.length > DWC3_EP0_BOUNCE_SIZE); |
| 841 | |
| 842 | transfer_size = roundup(req->request.length, |
| 843 | (u32) dep->endpoint.maxpacket); |
| 844 | |
| 845 | dwc->ep0_bounced = true; |
| 846 | |
| 847 | /* |
| 848 | * REVISIT in case request length is bigger than |
| 849 | * DWC3_EP0_BOUNCE_SIZE we will need two chained |
| 850 | * TRBs to handle the transfer. |
| 851 | */ |
| 852 | ret = dwc3_ep0_start_trans(dwc, dep->number, |
| 853 | dwc->ep0_bounce_addr, transfer_size, |
| 854 | DWC3_TRBCTL_CONTROL_DATA); |
| 855 | } else { |
| 856 | ret = usb_gadget_map_request(&dwc->gadget, &req->request, |
| 857 | dep->number); |
| 858 | if (ret) { |
| 859 | dev_dbg(dwc->dev, "failed to map request\n"); |
| 860 | return; |
| 861 | } |
| 862 | |
| 863 | ret = dwc3_ep0_start_trans(dwc, dep->number, req->request.dma, |
| 864 | req->request.length, DWC3_TRBCTL_CONTROL_DATA); |
| 865 | } |
| 866 | |
| 867 | WARN_ON(ret < 0); |
| 868 | } |
| 869 | |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 870 | static void dwc3_ep0_do_control_data(struct dwc3 *dwc, |
| 871 | const struct dwc3_event_depevt *event) |
| 872 | { |
| 873 | struct dwc3_ep *dep; |
| 874 | struct dwc3_request *req; |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 875 | |
| 876 | dep = dwc->eps[0]; |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 877 | |
| 878 | if (list_empty(&dep->request_list)) { |
| 879 | dev_vdbg(dwc->dev, "pending request for EP0 Data phase\n"); |
| 880 | dep->flags |= DWC3_EP_PENDING_REQUEST; |
| 881 | |
| 882 | if (event->endpoint_number) |
| 883 | dep->flags |= DWC3_EP0_DIR_IN; |
| 884 | return; |
| 885 | } |
| 886 | |
| 887 | req = next_request(&dep->request_list); |
Felipe Balbi | a080788 | 2012-05-04 13:03:54 +0300 | [diff] [blame] | 888 | dep = dwc->eps[event->endpoint_number]; |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 889 | |
Felipe Balbi | a080788 | 2012-05-04 13:03:54 +0300 | [diff] [blame] | 890 | __dwc3_ep0_do_control_data(dwc, dep, req); |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 891 | } |
| 892 | |
Sebastian Andrzej Siewior | f0f2b2a | 2011-11-02 13:30:44 +0100 | [diff] [blame] | 893 | static int dwc3_ep0_start_control_status(struct dwc3_ep *dep) |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 894 | { |
Sebastian Andrzej Siewior | f0f2b2a | 2011-11-02 13:30:44 +0100 | [diff] [blame] | 895 | struct dwc3 *dwc = dep->dwc; |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 896 | u32 type; |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 897 | |
| 898 | type = dwc->three_stage_setup ? DWC3_TRBCTL_CONTROL_STATUS3 |
| 899 | : DWC3_TRBCTL_CONTROL_STATUS2; |
| 900 | |
Sebastian Andrzej Siewior | f0f2b2a | 2011-11-02 13:30:44 +0100 | [diff] [blame] | 901 | return dwc3_ep0_start_trans(dwc, dep->number, |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 902 | dwc->ctrl_req_addr, 0, type); |
Sebastian Andrzej Siewior | f0f2b2a | 2011-11-02 13:30:44 +0100 | [diff] [blame] | 903 | } |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 904 | |
Felipe Balbi | 788a23f | 2012-05-21 14:22:41 +0300 | [diff] [blame] | 905 | static void __dwc3_ep0_do_control_status(struct dwc3 *dwc, struct dwc3_ep *dep) |
Sebastian Andrzej Siewior | f0f2b2a | 2011-11-02 13:30:44 +0100 | [diff] [blame] | 906 | { |
Felipe Balbi | 457e84b | 2012-01-18 18:04:09 +0200 | [diff] [blame] | 907 | if (dwc->resize_fifos) { |
| 908 | dev_dbg(dwc->dev, "starting to resize fifos\n"); |
| 909 | dwc3_gadget_resize_tx_fifos(dwc); |
| 910 | dwc->resize_fifos = 0; |
| 911 | } |
| 912 | |
Sebastian Andrzej Siewior | f0f2b2a | 2011-11-02 13:30:44 +0100 | [diff] [blame] | 913 | WARN_ON(dwc3_ep0_start_control_status(dep)); |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 914 | } |
| 915 | |
Felipe Balbi | 788a23f | 2012-05-21 14:22:41 +0300 | [diff] [blame] | 916 | static void dwc3_ep0_do_control_status(struct dwc3 *dwc, |
| 917 | const struct dwc3_event_depevt *event) |
| 918 | { |
| 919 | struct dwc3_ep *dep = dwc->eps[event->endpoint_number]; |
| 920 | |
| 921 | __dwc3_ep0_do_control_status(dwc, dep); |
| 922 | } |
| 923 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 924 | static void dwc3_ep0_xfernotready(struct dwc3 *dwc, |
| 925 | const struct dwc3_event_depevt *event) |
| 926 | { |
Felipe Balbi | df62df5 | 2011-10-14 15:11:49 +0300 | [diff] [blame] | 927 | dwc->setup_packet_pending = true; |
| 928 | |
Felipe Balbi | 9cc9bcd | 2011-10-18 18:00:26 +0300 | [diff] [blame] | 929 | /* |
Felipe Balbi | 33b84c2 | 2012-05-21 14:35:17 +0300 | [diff] [blame] | 930 | * This part is very tricky: If we have just handled |
Felipe Balbi | 9cc9bcd | 2011-10-18 18:00:26 +0300 | [diff] [blame] | 931 | * XferNotReady(Setup) and we're now expecting a |
| 932 | * XferComplete but, instead, we receive another |
| 933 | * XferNotReady(Setup), we should STALL and restart |
| 934 | * the state machine. |
| 935 | * |
| 936 | * In all other cases, we just continue waiting |
| 937 | * for the XferComplete event. |
| 938 | * |
| 939 | * We are a little bit unsafe here because we're |
| 940 | * not trying to ensure that last event was, indeed, |
| 941 | * XferNotReady(Setup). |
| 942 | * |
| 943 | * Still, we don't expect any condition where that |
| 944 | * should happen and, even if it does, it would be |
| 945 | * another error condition. |
| 946 | */ |
| 947 | if (dwc->ep0_next_event == DWC3_EP0_COMPLETE) { |
| 948 | switch (event->status) { |
| 949 | case DEPEVT_STATUS_CONTROL_SETUP: |
| 950 | dev_vdbg(dwc->dev, "Unexpected XferNotReady(Setup)\n"); |
| 951 | dwc3_ep0_stall_and_restart(dwc); |
| 952 | break; |
| 953 | case DEPEVT_STATUS_CONTROL_DATA: |
| 954 | /* FALLTHROUGH */ |
| 955 | case DEPEVT_STATUS_CONTROL_STATUS: |
| 956 | /* FALLTHROUGH */ |
| 957 | default: |
| 958 | dev_vdbg(dwc->dev, "waiting for XferComplete\n"); |
| 959 | } |
| 960 | |
| 961 | return; |
| 962 | } |
| 963 | |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 964 | switch (event->status) { |
| 965 | case DEPEVT_STATUS_CONTROL_SETUP: |
| 966 | dev_vdbg(dwc->dev, "Control Setup\n"); |
Sebastian Andrzej Siewior | f0f2b2a | 2011-11-02 13:30:44 +0100 | [diff] [blame] | 967 | |
| 968 | dwc->ep0state = EP0_SETUP_PHASE; |
| 969 | |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 970 | dwc3_ep0_do_control_setup(dwc, event); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 971 | break; |
Felipe Balbi | 1ddcb21 | 2011-08-30 15:52:17 +0300 | [diff] [blame] | 972 | |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 973 | case DEPEVT_STATUS_CONTROL_DATA: |
| 974 | dev_vdbg(dwc->dev, "Control Data\n"); |
Felipe Balbi | 1ddcb21 | 2011-08-30 15:52:17 +0300 | [diff] [blame] | 975 | |
Sebastian Andrzej Siewior | f0f2b2a | 2011-11-02 13:30:44 +0100 | [diff] [blame] | 976 | dwc->ep0state = EP0_DATA_PHASE; |
| 977 | |
Felipe Balbi | 1ddcb21 | 2011-08-30 15:52:17 +0300 | [diff] [blame] | 978 | if (dwc->ep0_next_event != DWC3_EP0_NRDY_DATA) { |
| 979 | dev_vdbg(dwc->dev, "Expected %d got %d\n", |
Felipe Balbi | 25355be | 2011-09-30 10:58:38 +0300 | [diff] [blame] | 980 | dwc->ep0_next_event, |
| 981 | DWC3_EP0_NRDY_DATA); |
Felipe Balbi | 1ddcb21 | 2011-08-30 15:52:17 +0300 | [diff] [blame] | 982 | |
| 983 | dwc3_ep0_stall_and_restart(dwc); |
| 984 | return; |
| 985 | } |
| 986 | |
Felipe Balbi | 55f3fba | 2011-09-08 18:27:33 +0300 | [diff] [blame] | 987 | /* |
| 988 | * One of the possible error cases is when Host _does_ |
| 989 | * request for Data Phase, but it does so on the wrong |
| 990 | * direction. |
| 991 | * |
| 992 | * Here, we already know ep0_next_event is DATA (see above), |
| 993 | * so we only need to check for direction. |
| 994 | */ |
| 995 | if (dwc->ep0_expect_in != event->endpoint_number) { |
| 996 | dev_vdbg(dwc->dev, "Wrong direction for Data phase\n"); |
| 997 | dwc3_ep0_stall_and_restart(dwc); |
| 998 | return; |
| 999 | } |
| 1000 | |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 1001 | dwc3_ep0_do_control_data(dwc, event); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1002 | break; |
Felipe Balbi | 1ddcb21 | 2011-08-30 15:52:17 +0300 | [diff] [blame] | 1003 | |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 1004 | case DEPEVT_STATUS_CONTROL_STATUS: |
| 1005 | dev_vdbg(dwc->dev, "Control Status\n"); |
Felipe Balbi | 1ddcb21 | 2011-08-30 15:52:17 +0300 | [diff] [blame] | 1006 | |
Sebastian Andrzej Siewior | f0f2b2a | 2011-11-02 13:30:44 +0100 | [diff] [blame] | 1007 | dwc->ep0state = EP0_STATUS_PHASE; |
| 1008 | |
Felipe Balbi | 1ddcb21 | 2011-08-30 15:52:17 +0300 | [diff] [blame] | 1009 | if (dwc->ep0_next_event != DWC3_EP0_NRDY_STATUS) { |
| 1010 | dev_vdbg(dwc->dev, "Expected %d got %d\n", |
Felipe Balbi | 25355be | 2011-09-30 10:58:38 +0300 | [diff] [blame] | 1011 | dwc->ep0_next_event, |
| 1012 | DWC3_EP0_NRDY_STATUS); |
Felipe Balbi | 1ddcb21 | 2011-08-30 15:52:17 +0300 | [diff] [blame] | 1013 | |
| 1014 | dwc3_ep0_stall_and_restart(dwc); |
| 1015 | return; |
| 1016 | } |
Sebastian Andrzej Siewior | 5bdb1dc | 2011-11-02 13:30:45 +0100 | [diff] [blame] | 1017 | |
| 1018 | if (dwc->delayed_status) { |
| 1019 | WARN_ON_ONCE(event->endpoint_number != 1); |
| 1020 | dev_vdbg(dwc->dev, "Mass Storage delayed status\n"); |
| 1021 | return; |
| 1022 | } |
| 1023 | |
Felipe Balbi | 788a23f | 2012-05-21 14:22:41 +0300 | [diff] [blame] | 1024 | dwc3_ep0_do_control_status(dwc, event); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1025 | } |
| 1026 | } |
| 1027 | |
| 1028 | void dwc3_ep0_interrupt(struct dwc3 *dwc, |
Felipe Balbi | 8becf27 | 2011-11-04 12:40:05 +0200 | [diff] [blame] | 1029 | const struct dwc3_event_depevt *event) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1030 | { |
| 1031 | u8 epnum = event->endpoint_number; |
| 1032 | |
| 1033 | dev_dbg(dwc->dev, "%s while ep%d%s in state '%s'\n", |
| 1034 | dwc3_ep_event_string(event->endpoint_event), |
Sebastian Andrzej Siewior | b147f35 | 2011-09-30 10:58:40 +0300 | [diff] [blame] | 1035 | epnum >> 1, (epnum & 1) ? "in" : "out", |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1036 | dwc3_ep0_state_string(dwc->ep0state)); |
| 1037 | |
| 1038 | switch (event->endpoint_event) { |
| 1039 | case DWC3_DEPEVT_XFERCOMPLETE: |
| 1040 | dwc3_ep0_xfer_complete(dwc, event); |
| 1041 | break; |
| 1042 | |
| 1043 | case DWC3_DEPEVT_XFERNOTREADY: |
| 1044 | dwc3_ep0_xfernotready(dwc, event); |
| 1045 | break; |
| 1046 | |
| 1047 | case DWC3_DEPEVT_XFERINPROGRESS: |
| 1048 | case DWC3_DEPEVT_RXTXFIFOEVT: |
| 1049 | case DWC3_DEPEVT_STREAMEVT: |
| 1050 | case DWC3_DEPEVT_EPCMDCMPLT: |
| 1051 | break; |
| 1052 | } |
| 1053 | } |