Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1 | /** |
| 2 | * gadget.c - DesignWare USB3 DRD Controller Gadget Framework Link |
| 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 | * |
Felipe Balbi | 5945f78 | 2013-06-30 14:15:11 +0300 | [diff] [blame] | 9 | * This program is free software: you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License version 2 of |
| 11 | * the License as published by the Free Software Foundation. |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 12 | * |
Felipe Balbi | 5945f78 | 2013-06-30 14:15:11 +0300 | [diff] [blame] | 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 17 | */ |
| 18 | |
| 19 | #include <linux/kernel.h> |
| 20 | #include <linux/delay.h> |
| 21 | #include <linux/slab.h> |
| 22 | #include <linux/spinlock.h> |
| 23 | #include <linux/platform_device.h> |
| 24 | #include <linux/pm_runtime.h> |
| 25 | #include <linux/interrupt.h> |
| 26 | #include <linux/io.h> |
| 27 | #include <linux/list.h> |
| 28 | #include <linux/dma-mapping.h> |
| 29 | |
| 30 | #include <linux/usb/ch9.h> |
| 31 | #include <linux/usb/gadget.h> |
| 32 | |
Felipe Balbi | 80977dc | 2014-08-19 16:37:22 -0500 | [diff] [blame] | 33 | #include "debug.h" |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 34 | #include "core.h" |
| 35 | #include "gadget.h" |
| 36 | #include "io.h" |
| 37 | |
Felipe Balbi | 04a9bfc | 2012-01-02 18:25:43 +0200 | [diff] [blame] | 38 | /** |
| 39 | * dwc3_gadget_set_test_mode - Enables USB2 Test Modes |
| 40 | * @dwc: pointer to our context structure |
| 41 | * @mode: the mode to set (J, K SE0 NAK, Force Enable) |
| 42 | * |
| 43 | * Caller should take care of locking. This function will |
| 44 | * return 0 on success or -EINVAL if wrong Test Selector |
| 45 | * is passed |
| 46 | */ |
| 47 | int dwc3_gadget_set_test_mode(struct dwc3 *dwc, int mode) |
| 48 | { |
| 49 | u32 reg; |
| 50 | |
| 51 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
| 52 | reg &= ~DWC3_DCTL_TSTCTRL_MASK; |
| 53 | |
| 54 | switch (mode) { |
| 55 | case TEST_J: |
| 56 | case TEST_K: |
| 57 | case TEST_SE0_NAK: |
| 58 | case TEST_PACKET: |
| 59 | case TEST_FORCE_EN: |
| 60 | reg |= mode << 1; |
| 61 | break; |
| 62 | default: |
| 63 | return -EINVAL; |
| 64 | } |
| 65 | |
| 66 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); |
| 67 | |
| 68 | return 0; |
| 69 | } |
| 70 | |
Felipe Balbi | 8598bde | 2012-01-02 18:55:57 +0200 | [diff] [blame] | 71 | /** |
Paul Zimmerman | 911f1f8 | 2012-04-27 13:35:15 +0300 | [diff] [blame] | 72 | * dwc3_gadget_get_link_state - Gets current state of USB Link |
| 73 | * @dwc: pointer to our context structure |
| 74 | * |
| 75 | * Caller should take care of locking. This function will |
| 76 | * return the link state on success (>= 0) or -ETIMEDOUT. |
| 77 | */ |
| 78 | int dwc3_gadget_get_link_state(struct dwc3 *dwc) |
| 79 | { |
| 80 | u32 reg; |
| 81 | |
| 82 | reg = dwc3_readl(dwc->regs, DWC3_DSTS); |
| 83 | |
| 84 | return DWC3_DSTS_USBLNKST(reg); |
| 85 | } |
| 86 | |
| 87 | /** |
Felipe Balbi | 8598bde | 2012-01-02 18:55:57 +0200 | [diff] [blame] | 88 | * dwc3_gadget_set_link_state - Sets USB Link to a particular State |
| 89 | * @dwc: pointer to our context structure |
| 90 | * @state: the state to put link into |
| 91 | * |
| 92 | * Caller should take care of locking. This function will |
Paul Zimmerman | aee63e3 | 2012-02-24 17:32:15 -0800 | [diff] [blame] | 93 | * return 0 on success or -ETIMEDOUT. |
Felipe Balbi | 8598bde | 2012-01-02 18:55:57 +0200 | [diff] [blame] | 94 | */ |
| 95 | int dwc3_gadget_set_link_state(struct dwc3 *dwc, enum dwc3_link_state state) |
| 96 | { |
Paul Zimmerman | aee63e3 | 2012-02-24 17:32:15 -0800 | [diff] [blame] | 97 | int retries = 10000; |
Felipe Balbi | 8598bde | 2012-01-02 18:55:57 +0200 | [diff] [blame] | 98 | u32 reg; |
| 99 | |
Paul Zimmerman | 802fde9 | 2012-04-27 13:10:52 +0300 | [diff] [blame] | 100 | /* |
| 101 | * Wait until device controller is ready. Only applies to 1.94a and |
| 102 | * later RTL. |
| 103 | */ |
| 104 | if (dwc->revision >= DWC3_REVISION_194A) { |
| 105 | while (--retries) { |
| 106 | reg = dwc3_readl(dwc->regs, DWC3_DSTS); |
| 107 | if (reg & DWC3_DSTS_DCNRD) |
| 108 | udelay(5); |
| 109 | else |
| 110 | break; |
| 111 | } |
| 112 | |
| 113 | if (retries <= 0) |
| 114 | return -ETIMEDOUT; |
| 115 | } |
| 116 | |
Felipe Balbi | 8598bde | 2012-01-02 18:55:57 +0200 | [diff] [blame] | 117 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
| 118 | reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK; |
| 119 | |
| 120 | /* set requested state */ |
| 121 | reg |= DWC3_DCTL_ULSTCHNGREQ(state); |
| 122 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); |
| 123 | |
Paul Zimmerman | 802fde9 | 2012-04-27 13:10:52 +0300 | [diff] [blame] | 124 | /* |
| 125 | * The following code is racy when called from dwc3_gadget_wakeup, |
| 126 | * and is not needed, at least on newer versions |
| 127 | */ |
| 128 | if (dwc->revision >= DWC3_REVISION_194A) |
| 129 | return 0; |
| 130 | |
Felipe Balbi | 8598bde | 2012-01-02 18:55:57 +0200 | [diff] [blame] | 131 | /* wait for a change in DSTS */ |
Paul Zimmerman | aed430e | 2012-04-27 12:52:01 +0300 | [diff] [blame] | 132 | retries = 10000; |
Felipe Balbi | 8598bde | 2012-01-02 18:55:57 +0200 | [diff] [blame] | 133 | while (--retries) { |
| 134 | reg = dwc3_readl(dwc->regs, DWC3_DSTS); |
| 135 | |
Felipe Balbi | 8598bde | 2012-01-02 18:55:57 +0200 | [diff] [blame] | 136 | if (DWC3_DSTS_USBLNKST(reg) == state) |
| 137 | return 0; |
| 138 | |
Paul Zimmerman | aee63e3 | 2012-02-24 17:32:15 -0800 | [diff] [blame] | 139 | udelay(5); |
Felipe Balbi | 8598bde | 2012-01-02 18:55:57 +0200 | [diff] [blame] | 140 | } |
| 141 | |
Felipe Balbi | 7381528 | 2015-01-27 13:48:14 -0600 | [diff] [blame] | 142 | dwc3_trace(trace_dwc3_gadget, |
| 143 | "link state change request timed out"); |
Felipe Balbi | 8598bde | 2012-01-02 18:55:57 +0200 | [diff] [blame] | 144 | |
| 145 | return -ETIMEDOUT; |
| 146 | } |
| 147 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 148 | void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req, |
| 149 | int status) |
| 150 | { |
| 151 | struct dwc3 *dwc = dep->dwc; |
Pratyush Anand | e5ba5ec | 2013-01-14 15:59:37 +0530 | [diff] [blame] | 152 | int i; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 153 | |
Felipe Balbi | aa3342c | 2016-03-14 11:01:31 +0200 | [diff] [blame] | 154 | if (req->started) { |
Pratyush Anand | e5ba5ec | 2013-01-14 15:59:37 +0530 | [diff] [blame] | 155 | i = 0; |
| 156 | do { |
Felipe Balbi | eeb720f | 2011-11-28 12:46:59 +0200 | [diff] [blame] | 157 | dep->busy_slot++; |
Pratyush Anand | e5ba5ec | 2013-01-14 15:59:37 +0530 | [diff] [blame] | 158 | /* |
| 159 | * Skip LINK TRB. We can't use req->trb and check for |
| 160 | * DWC3_TRBCTL_LINK_TRB because it points the TRB we |
| 161 | * just completed (not the LINK TRB). |
| 162 | */ |
| 163 | if (((dep->busy_slot & DWC3_TRB_MASK) == |
| 164 | DWC3_TRB_NUM- 1) && |
Ido Shayevitz | 16e78db | 2012-03-12 20:25:24 +0200 | [diff] [blame] | 165 | usb_endpoint_xfer_isoc(dep->endpoint.desc)) |
Pratyush Anand | e5ba5ec | 2013-01-14 15:59:37 +0530 | [diff] [blame] | 166 | dep->busy_slot++; |
| 167 | } while(++i < req->request.num_mapped_sgs); |
Felipe Balbi | aa3342c | 2016-03-14 11:01:31 +0200 | [diff] [blame] | 168 | req->started = false; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 169 | } |
| 170 | list_del(&req->list); |
Felipe Balbi | eeb720f | 2011-11-28 12:46:59 +0200 | [diff] [blame] | 171 | req->trb = NULL; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 172 | |
| 173 | if (req->request.status == -EINPROGRESS) |
| 174 | req->request.status = status; |
| 175 | |
Pratyush Anand | 0416e49 | 2012-08-10 13:42:16 +0530 | [diff] [blame] | 176 | if (dwc->ep0_bounced && dep->number == 0) |
| 177 | dwc->ep0_bounced = false; |
| 178 | else |
| 179 | usb_gadget_unmap_request(&dwc->gadget, &req->request, |
| 180 | req->direction); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 181 | |
Felipe Balbi | 2c4cbe6e5 | 2014-04-30 17:45:10 -0500 | [diff] [blame] | 182 | trace_dwc3_gadget_giveback(req); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 183 | |
| 184 | spin_unlock(&dwc->lock); |
Michal Sojka | 304f7e5 | 2014-09-24 22:43:19 +0200 | [diff] [blame] | 185 | usb_gadget_giveback_request(&dep->endpoint, &req->request); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 186 | spin_lock(&dwc->lock); |
| 187 | } |
| 188 | |
Felipe Balbi | 3ece0ec | 2014-09-05 09:47:44 -0500 | [diff] [blame] | 189 | int dwc3_send_gadget_generic_command(struct dwc3 *dwc, unsigned cmd, u32 param) |
Felipe Balbi | b09bb64 | 2012-04-24 16:19:11 +0300 | [diff] [blame] | 190 | { |
| 191 | u32 timeout = 500; |
| 192 | u32 reg; |
| 193 | |
Felipe Balbi | 2c4cbe6e5 | 2014-04-30 17:45:10 -0500 | [diff] [blame] | 194 | trace_dwc3_gadget_generic_cmd(cmd, param); |
Felipe Balbi | 427c3df | 2014-04-25 14:14:14 -0500 | [diff] [blame] | 195 | |
Felipe Balbi | b09bb64 | 2012-04-24 16:19:11 +0300 | [diff] [blame] | 196 | dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param); |
| 197 | dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT); |
| 198 | |
| 199 | do { |
| 200 | reg = dwc3_readl(dwc->regs, DWC3_DGCMD); |
| 201 | if (!(reg & DWC3_DGCMD_CMDACT)) { |
Felipe Balbi | 7381528 | 2015-01-27 13:48:14 -0600 | [diff] [blame] | 202 | dwc3_trace(trace_dwc3_gadget, |
| 203 | "Command Complete --> %d", |
Felipe Balbi | b09bb64 | 2012-04-24 16:19:11 +0300 | [diff] [blame] | 204 | DWC3_DGCMD_STATUS(reg)); |
Subbaraya Sundeep Bhatta | 891b1dc | 2015-05-21 15:46:47 +0530 | [diff] [blame] | 205 | if (DWC3_DGCMD_STATUS(reg)) |
| 206 | return -EINVAL; |
Felipe Balbi | b09bb64 | 2012-04-24 16:19:11 +0300 | [diff] [blame] | 207 | return 0; |
| 208 | } |
| 209 | |
| 210 | /* |
| 211 | * We can't sleep here, because it's also called from |
| 212 | * interrupt context. |
| 213 | */ |
| 214 | timeout--; |
Felipe Balbi | 7381528 | 2015-01-27 13:48:14 -0600 | [diff] [blame] | 215 | if (!timeout) { |
| 216 | dwc3_trace(trace_dwc3_gadget, |
| 217 | "Command Timed Out"); |
Felipe Balbi | b09bb64 | 2012-04-24 16:19:11 +0300 | [diff] [blame] | 218 | return -ETIMEDOUT; |
Felipe Balbi | 7381528 | 2015-01-27 13:48:14 -0600 | [diff] [blame] | 219 | } |
Felipe Balbi | b09bb64 | 2012-04-24 16:19:11 +0300 | [diff] [blame] | 220 | udelay(1); |
| 221 | } while (1); |
| 222 | } |
| 223 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 224 | int dwc3_send_gadget_ep_cmd(struct dwc3 *dwc, unsigned ep, |
| 225 | unsigned cmd, struct dwc3_gadget_ep_cmd_params *params) |
| 226 | { |
| 227 | struct dwc3_ep *dep = dwc->eps[ep]; |
Sebastian Andrzej Siewior | 61d5824 | 2011-08-29 16:46:38 +0200 | [diff] [blame] | 228 | u32 timeout = 500; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 229 | u32 reg; |
Felipe Balbi | 2b0f11d | 2016-04-04 09:19:17 +0300 | [diff] [blame^] | 230 | |
| 231 | int susphy = false; |
Felipe Balbi | c0ca324 | 2016-04-04 09:11:51 +0300 | [diff] [blame] | 232 | int ret = -EINVAL; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 233 | |
Felipe Balbi | 2c4cbe6e5 | 2014-04-30 17:45:10 -0500 | [diff] [blame] | 234 | trace_dwc3_gadget_ep_cmd(dep, cmd, params); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 235 | |
Felipe Balbi | 2b0f11d | 2016-04-04 09:19:17 +0300 | [diff] [blame^] | 236 | /* |
| 237 | * Synopsys Databook 2.60a states, on section 6.3.2.5.[1-8], that if |
| 238 | * we're issuing an endpoint command, we must check if |
| 239 | * GUSB2PHYCFG.SUSPHY bit is set. If it is, then we need to clear it. |
| 240 | * |
| 241 | * We will also set SUSPHY bit to what it was before returning as stated |
| 242 | * by the same section on Synopsys databook. |
| 243 | */ |
| 244 | reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0)); |
| 245 | if (unlikely(reg & DWC3_GUSB2PHYCFG_SUSPHY)) { |
| 246 | susphy = true; |
| 247 | reg &= ~DWC3_GUSB2PHYCFG_SUSPHY; |
| 248 | dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg); |
| 249 | } |
| 250 | |
Felipe Balbi | dc1c70a | 2011-09-30 10:58:51 +0300 | [diff] [blame] | 251 | dwc3_writel(dwc->regs, DWC3_DEPCMDPAR0(ep), params->param0); |
| 252 | dwc3_writel(dwc->regs, DWC3_DEPCMDPAR1(ep), params->param1); |
| 253 | dwc3_writel(dwc->regs, DWC3_DEPCMDPAR2(ep), params->param2); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 254 | |
| 255 | dwc3_writel(dwc->regs, DWC3_DEPCMD(ep), cmd | DWC3_DEPCMD_CMDACT); |
| 256 | do { |
| 257 | reg = dwc3_readl(dwc->regs, DWC3_DEPCMD(ep)); |
| 258 | if (!(reg & DWC3_DEPCMD_CMDACT)) { |
Felipe Balbi | 7381528 | 2015-01-27 13:48:14 -0600 | [diff] [blame] | 259 | dwc3_trace(trace_dwc3_gadget, |
| 260 | "Command Complete --> %d", |
Felipe Balbi | 164f6e1 | 2011-08-27 20:29:58 +0300 | [diff] [blame] | 261 | DWC3_DEPCMD_STATUS(reg)); |
Subbaraya Sundeep Bhatta | 76e838c | 2015-05-21 15:46:48 +0530 | [diff] [blame] | 262 | if (DWC3_DEPCMD_STATUS(reg)) |
Felipe Balbi | c0ca324 | 2016-04-04 09:11:51 +0300 | [diff] [blame] | 263 | break; |
| 264 | ret = 0; |
| 265 | break; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | /* |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 269 | * We can't sleep here, because it is also called from |
| 270 | * interrupt context. |
| 271 | */ |
| 272 | timeout--; |
Felipe Balbi | 7381528 | 2015-01-27 13:48:14 -0600 | [diff] [blame] | 273 | if (!timeout) { |
| 274 | dwc3_trace(trace_dwc3_gadget, |
| 275 | "Command Timed Out"); |
Felipe Balbi | c0ca324 | 2016-04-04 09:11:51 +0300 | [diff] [blame] | 276 | ret = -ETIMEDOUT; |
| 277 | break; |
Felipe Balbi | 7381528 | 2015-01-27 13:48:14 -0600 | [diff] [blame] | 278 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 279 | |
Sebastian Andrzej Siewior | 61d5824 | 2011-08-29 16:46:38 +0200 | [diff] [blame] | 280 | udelay(1); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 281 | } while (1); |
Felipe Balbi | c0ca324 | 2016-04-04 09:11:51 +0300 | [diff] [blame] | 282 | |
Felipe Balbi | 2b0f11d | 2016-04-04 09:19:17 +0300 | [diff] [blame^] | 283 | if (unlikely(susphy)) { |
| 284 | reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0)); |
| 285 | reg |= DWC3_GUSB2PHYCFG_SUSPHY; |
| 286 | dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg); |
| 287 | } |
| 288 | |
Felipe Balbi | c0ca324 | 2016-04-04 09:11:51 +0300 | [diff] [blame] | 289 | return ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | static dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep, |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 293 | struct dwc3_trb *trb) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 294 | { |
Paul Zimmerman | c439ef8 | 2011-09-30 10:58:45 +0300 | [diff] [blame] | 295 | u32 offset = (char *) trb - (char *) dep->trb_pool; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 296 | |
| 297 | return dep->trb_pool_dma + offset; |
| 298 | } |
| 299 | |
| 300 | static int dwc3_alloc_trb_pool(struct dwc3_ep *dep) |
| 301 | { |
| 302 | struct dwc3 *dwc = dep->dwc; |
| 303 | |
| 304 | if (dep->trb_pool) |
| 305 | return 0; |
| 306 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 307 | dep->trb_pool = dma_alloc_coherent(dwc->dev, |
| 308 | sizeof(struct dwc3_trb) * DWC3_TRB_NUM, |
| 309 | &dep->trb_pool_dma, GFP_KERNEL); |
| 310 | if (!dep->trb_pool) { |
| 311 | dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n", |
| 312 | dep->name); |
| 313 | return -ENOMEM; |
| 314 | } |
| 315 | |
| 316 | return 0; |
| 317 | } |
| 318 | |
| 319 | static void dwc3_free_trb_pool(struct dwc3_ep *dep) |
| 320 | { |
| 321 | struct dwc3 *dwc = dep->dwc; |
| 322 | |
| 323 | dma_free_coherent(dwc->dev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM, |
| 324 | dep->trb_pool, dep->trb_pool_dma); |
| 325 | |
| 326 | dep->trb_pool = NULL; |
| 327 | dep->trb_pool_dma = 0; |
| 328 | } |
| 329 | |
John Youn | c450960 | 2016-02-16 20:10:53 -0800 | [diff] [blame] | 330 | static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep); |
| 331 | |
| 332 | /** |
| 333 | * dwc3_gadget_start_config - Configure EP resources |
| 334 | * @dwc: pointer to our controller context structure |
| 335 | * @dep: endpoint that is being enabled |
| 336 | * |
| 337 | * The assignment of transfer resources cannot perfectly follow the |
| 338 | * data book due to the fact that the controller driver does not have |
| 339 | * all knowledge of the configuration in advance. It is given this |
| 340 | * information piecemeal by the composite gadget framework after every |
| 341 | * SET_CONFIGURATION and SET_INTERFACE. Trying to follow the databook |
| 342 | * programming model in this scenario can cause errors. For two |
| 343 | * reasons: |
| 344 | * |
| 345 | * 1) The databook says to do DEPSTARTCFG for every SET_CONFIGURATION |
| 346 | * and SET_INTERFACE (8.1.5). This is incorrect in the scenario of |
| 347 | * multiple interfaces. |
| 348 | * |
| 349 | * 2) The databook does not mention doing more DEPXFERCFG for new |
| 350 | * endpoint on alt setting (8.1.6). |
| 351 | * |
| 352 | * The following simplified method is used instead: |
| 353 | * |
| 354 | * All hardware endpoints can be assigned a transfer resource and this |
| 355 | * setting will stay persistent until either a core reset or |
| 356 | * hibernation. So whenever we do a DEPSTARTCFG(0) we can go ahead and |
| 357 | * do DEPXFERCFG for every hardware endpoint as well. We are |
| 358 | * guaranteed that there are as many transfer resources as endpoints. |
| 359 | * |
| 360 | * This function is called for each endpoint when it is being enabled |
| 361 | * but is triggered only when called for EP0-out, which always happens |
| 362 | * first, and which should only happen in one of the above conditions. |
| 363 | */ |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 364 | static int dwc3_gadget_start_config(struct dwc3 *dwc, struct dwc3_ep *dep) |
| 365 | { |
| 366 | struct dwc3_gadget_ep_cmd_params params; |
| 367 | u32 cmd; |
John Youn | c450960 | 2016-02-16 20:10:53 -0800 | [diff] [blame] | 368 | int i; |
| 369 | int ret; |
| 370 | |
| 371 | if (dep->number) |
| 372 | return 0; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 373 | |
| 374 | memset(¶ms, 0x00, sizeof(params)); |
John Youn | c450960 | 2016-02-16 20:10:53 -0800 | [diff] [blame] | 375 | cmd = DWC3_DEPCMD_DEPSTARTCFG; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 376 | |
John Youn | c450960 | 2016-02-16 20:10:53 -0800 | [diff] [blame] | 377 | ret = dwc3_send_gadget_ep_cmd(dwc, 0, cmd, ¶ms); |
| 378 | if (ret) |
| 379 | return ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 380 | |
John Youn | c450960 | 2016-02-16 20:10:53 -0800 | [diff] [blame] | 381 | for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) { |
| 382 | struct dwc3_ep *dep = dwc->eps[i]; |
| 383 | |
| 384 | if (!dep) |
| 385 | continue; |
| 386 | |
| 387 | ret = dwc3_gadget_set_xfer_resource(dwc, dep); |
| 388 | if (ret) |
| 389 | return ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | return 0; |
| 393 | } |
| 394 | |
| 395 | static int dwc3_gadget_set_ep_config(struct dwc3 *dwc, struct dwc3_ep *dep, |
Felipe Balbi | c90bfae | 2011-11-29 13:11:21 +0200 | [diff] [blame] | 396 | const struct usb_endpoint_descriptor *desc, |
Felipe Balbi | 4b345c9 | 2012-07-16 14:08:16 +0300 | [diff] [blame] | 397 | const struct usb_ss_ep_comp_descriptor *comp_desc, |
Paul Zimmerman | 265b70a | 2013-12-19 12:38:49 -0600 | [diff] [blame] | 398 | bool ignore, bool restore) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 399 | { |
| 400 | struct dwc3_gadget_ep_cmd_params params; |
| 401 | |
| 402 | memset(¶ms, 0x00, sizeof(params)); |
| 403 | |
Felipe Balbi | dc1c70a | 2011-09-30 10:58:51 +0300 | [diff] [blame] | 404 | params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc)) |
Chanho Park | d2e9a13 | 2012-08-31 16:54:07 +0900 | [diff] [blame] | 405 | | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc)); |
| 406 | |
| 407 | /* Burst size is only needed in SuperSpeed mode */ |
John Youn | ee5cd41 | 2016-02-05 17:08:45 -0800 | [diff] [blame] | 408 | if (dwc->gadget.speed >= USB_SPEED_SUPER) { |
Chanho Park | d2e9a13 | 2012-08-31 16:54:07 +0900 | [diff] [blame] | 409 | u32 burst = dep->endpoint.maxburst - 1; |
| 410 | |
| 411 | params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst); |
| 412 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 413 | |
Felipe Balbi | 4b345c9 | 2012-07-16 14:08:16 +0300 | [diff] [blame] | 414 | if (ignore) |
| 415 | params.param0 |= DWC3_DEPCFG_IGN_SEQ_NUM; |
| 416 | |
Paul Zimmerman | 265b70a | 2013-12-19 12:38:49 -0600 | [diff] [blame] | 417 | if (restore) { |
| 418 | params.param0 |= DWC3_DEPCFG_ACTION_RESTORE; |
| 419 | params.param2 |= dep->saved_state; |
| 420 | } |
| 421 | |
Felipe Balbi | dc1c70a | 2011-09-30 10:58:51 +0300 | [diff] [blame] | 422 | params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN |
| 423 | | DWC3_DEPCFG_XFER_NOT_READY_EN; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 424 | |
Felipe Balbi | 18b7ede | 2012-01-02 13:35:41 +0200 | [diff] [blame] | 425 | if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) { |
Felipe Balbi | dc1c70a | 2011-09-30 10:58:51 +0300 | [diff] [blame] | 426 | params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE |
| 427 | | DWC3_DEPCFG_STREAM_EVENT_EN; |
Felipe Balbi | 879631a | 2011-09-30 10:58:47 +0300 | [diff] [blame] | 428 | dep->stream_capable = true; |
| 429 | } |
| 430 | |
Felipe Balbi | 0b93a4c | 2014-09-04 10:28:10 -0500 | [diff] [blame] | 431 | if (!usb_endpoint_xfer_control(desc)) |
Felipe Balbi | dc1c70a | 2011-09-30 10:58:51 +0300 | [diff] [blame] | 432 | params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 433 | |
| 434 | /* |
| 435 | * We are doing 1:1 mapping for endpoints, meaning |
| 436 | * Physical Endpoints 2 maps to Logical Endpoint 2 and |
| 437 | * so on. We consider the direction bit as part of the physical |
| 438 | * endpoint number. So USB endpoint 0x81 is 0x03. |
| 439 | */ |
Felipe Balbi | dc1c70a | 2011-09-30 10:58:51 +0300 | [diff] [blame] | 440 | params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 441 | |
| 442 | /* |
| 443 | * We must use the lower 16 TX FIFOs even though |
| 444 | * HW might have more |
| 445 | */ |
| 446 | if (dep->direction) |
Felipe Balbi | dc1c70a | 2011-09-30 10:58:51 +0300 | [diff] [blame] | 447 | params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 448 | |
| 449 | if (desc->bInterval) { |
Felipe Balbi | dc1c70a | 2011-09-30 10:58:51 +0300 | [diff] [blame] | 450 | params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 451 | dep->interval = 1 << (desc->bInterval - 1); |
| 452 | } |
| 453 | |
| 454 | return dwc3_send_gadget_ep_cmd(dwc, dep->number, |
| 455 | DWC3_DEPCMD_SETEPCONFIG, ¶ms); |
| 456 | } |
| 457 | |
| 458 | static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep) |
| 459 | { |
| 460 | struct dwc3_gadget_ep_cmd_params params; |
| 461 | |
| 462 | memset(¶ms, 0x00, sizeof(params)); |
| 463 | |
Felipe Balbi | dc1c70a | 2011-09-30 10:58:51 +0300 | [diff] [blame] | 464 | params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 465 | |
| 466 | return dwc3_send_gadget_ep_cmd(dwc, dep->number, |
| 467 | DWC3_DEPCMD_SETTRANSFRESOURCE, ¶ms); |
| 468 | } |
| 469 | |
| 470 | /** |
| 471 | * __dwc3_gadget_ep_enable - Initializes a HW endpoint |
| 472 | * @dep: endpoint to be initialized |
| 473 | * @desc: USB Endpoint Descriptor |
| 474 | * |
| 475 | * Caller should take care of locking |
| 476 | */ |
| 477 | static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep, |
Felipe Balbi | c90bfae | 2011-11-29 13:11:21 +0200 | [diff] [blame] | 478 | const struct usb_endpoint_descriptor *desc, |
Felipe Balbi | 4b345c9 | 2012-07-16 14:08:16 +0300 | [diff] [blame] | 479 | const struct usb_ss_ep_comp_descriptor *comp_desc, |
Paul Zimmerman | 265b70a | 2013-12-19 12:38:49 -0600 | [diff] [blame] | 480 | bool ignore, bool restore) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 481 | { |
| 482 | struct dwc3 *dwc = dep->dwc; |
| 483 | u32 reg; |
Andy Shevchenko | b09e99e | 2014-05-15 15:53:32 +0300 | [diff] [blame] | 484 | int ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 485 | |
Felipe Balbi | 7381528 | 2015-01-27 13:48:14 -0600 | [diff] [blame] | 486 | dwc3_trace(trace_dwc3_gadget, "Enabling %s", dep->name); |
Felipe Balbi | ff62d6b | 2013-07-12 19:09:39 +0300 | [diff] [blame] | 487 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 488 | if (!(dep->flags & DWC3_EP_ENABLED)) { |
| 489 | ret = dwc3_gadget_start_config(dwc, dep); |
| 490 | if (ret) |
| 491 | return ret; |
| 492 | } |
| 493 | |
Paul Zimmerman | 265b70a | 2013-12-19 12:38:49 -0600 | [diff] [blame] | 494 | ret = dwc3_gadget_set_ep_config(dwc, dep, desc, comp_desc, ignore, |
| 495 | restore); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 496 | if (ret) |
| 497 | return ret; |
| 498 | |
| 499 | if (!(dep->flags & DWC3_EP_ENABLED)) { |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 500 | struct dwc3_trb *trb_st_hw; |
| 501 | struct dwc3_trb *trb_link; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 502 | |
Ido Shayevitz | 16e78db | 2012-03-12 20:25:24 +0200 | [diff] [blame] | 503 | dep->endpoint.desc = desc; |
Felipe Balbi | c90bfae | 2011-11-29 13:11:21 +0200 | [diff] [blame] | 504 | dep->comp_desc = comp_desc; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 505 | dep->type = usb_endpoint_type(desc); |
| 506 | dep->flags |= DWC3_EP_ENABLED; |
| 507 | |
| 508 | reg = dwc3_readl(dwc->regs, DWC3_DALEPENA); |
| 509 | reg |= DWC3_DALEPENA_EP(dep->number); |
| 510 | dwc3_writel(dwc->regs, DWC3_DALEPENA, reg); |
| 511 | |
| 512 | if (!usb_endpoint_xfer_isoc(desc)) |
Felipe Balbi | e901aa1 | 2016-03-16 14:01:37 +0200 | [diff] [blame] | 513 | goto out; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 514 | |
Paul Zimmerman | 1d04679 | 2012-02-15 18:56:56 -0800 | [diff] [blame] | 515 | /* Link TRB for ISOC. The HWO bit is never reset */ |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 516 | trb_st_hw = &dep->trb_pool[0]; |
| 517 | |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 518 | trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1]; |
Jack Pham | 1200a82 | 2014-10-21 16:31:10 -0700 | [diff] [blame] | 519 | memset(trb_link, 0, sizeof(*trb_link)); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 520 | |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 521 | trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw)); |
| 522 | trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw)); |
| 523 | trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB; |
| 524 | trb_link->ctrl |= DWC3_TRB_CTRL_HWO; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 525 | } |
| 526 | |
Felipe Balbi | e901aa1 | 2016-03-16 14:01:37 +0200 | [diff] [blame] | 527 | out: |
Felipe Balbi | aa73997 | 2015-07-20 14:48:13 -0500 | [diff] [blame] | 528 | switch (usb_endpoint_type(desc)) { |
| 529 | case USB_ENDPOINT_XFER_CONTROL: |
Felipe Balbi | e901aa1 | 2016-03-16 14:01:37 +0200 | [diff] [blame] | 530 | /* don't change name */ |
Felipe Balbi | aa73997 | 2015-07-20 14:48:13 -0500 | [diff] [blame] | 531 | break; |
| 532 | case USB_ENDPOINT_XFER_ISOC: |
| 533 | strlcat(dep->name, "-isoc", sizeof(dep->name)); |
| 534 | break; |
| 535 | case USB_ENDPOINT_XFER_BULK: |
| 536 | strlcat(dep->name, "-bulk", sizeof(dep->name)); |
| 537 | break; |
| 538 | case USB_ENDPOINT_XFER_INT: |
| 539 | strlcat(dep->name, "-int", sizeof(dep->name)); |
| 540 | break; |
| 541 | default: |
| 542 | dev_err(dwc->dev, "invalid endpoint transfer type\n"); |
| 543 | } |
| 544 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 545 | return 0; |
| 546 | } |
| 547 | |
Paul Zimmerman | b992e68 | 2012-04-27 14:17:35 +0300 | [diff] [blame] | 548 | static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force); |
Sebastian Andrzej Siewior | 624407f | 2011-08-29 13:56:37 +0200 | [diff] [blame] | 549 | static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 550 | { |
| 551 | struct dwc3_request *req; |
| 552 | |
Felipe Balbi | aa3342c | 2016-03-14 11:01:31 +0200 | [diff] [blame] | 553 | if (!list_empty(&dep->started_list)) { |
Paul Zimmerman | b992e68 | 2012-04-27 14:17:35 +0300 | [diff] [blame] | 554 | dwc3_stop_active_transfer(dwc, dep->number, true); |
Sebastian Andrzej Siewior | 624407f | 2011-08-29 13:56:37 +0200 | [diff] [blame] | 555 | |
Pratyush Anand | 5791150 | 2012-07-06 15:19:10 +0530 | [diff] [blame] | 556 | /* - giveback all requests to gadget driver */ |
Felipe Balbi | aa3342c | 2016-03-14 11:01:31 +0200 | [diff] [blame] | 557 | while (!list_empty(&dep->started_list)) { |
| 558 | req = next_request(&dep->started_list); |
Pratyush Anand | 1591633 | 2012-06-15 11:54:36 +0530 | [diff] [blame] | 559 | |
| 560 | dwc3_gadget_giveback(dep, req, -ESHUTDOWN); |
| 561 | } |
Felipe Balbi | ea53b88 | 2012-02-17 12:10:04 +0200 | [diff] [blame] | 562 | } |
| 563 | |
Felipe Balbi | aa3342c | 2016-03-14 11:01:31 +0200 | [diff] [blame] | 564 | while (!list_empty(&dep->pending_list)) { |
| 565 | req = next_request(&dep->pending_list); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 566 | |
Sebastian Andrzej Siewior | 624407f | 2011-08-29 13:56:37 +0200 | [diff] [blame] | 567 | dwc3_gadget_giveback(dep, req, -ESHUTDOWN); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 568 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | /** |
| 572 | * __dwc3_gadget_ep_disable - Disables a HW endpoint |
| 573 | * @dep: the endpoint to disable |
| 574 | * |
Sebastian Andrzej Siewior | 624407f | 2011-08-29 13:56:37 +0200 | [diff] [blame] | 575 | * This function also removes requests which are currently processed ny the |
| 576 | * hardware and those which are not yet scheduled. |
| 577 | * Caller should take care of locking. |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 578 | */ |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 579 | static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep) |
| 580 | { |
| 581 | struct dwc3 *dwc = dep->dwc; |
| 582 | u32 reg; |
| 583 | |
Felipe Balbi | 7eaeac5 | 2015-07-20 14:46:15 -0500 | [diff] [blame] | 584 | dwc3_trace(trace_dwc3_gadget, "Disabling %s", dep->name); |
| 585 | |
Sebastian Andrzej Siewior | 624407f | 2011-08-29 13:56:37 +0200 | [diff] [blame] | 586 | dwc3_remove_requests(dwc, dep); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 587 | |
Felipe Balbi | 687ef98 | 2014-04-16 10:30:33 -0500 | [diff] [blame] | 588 | /* make sure HW endpoint isn't stalled */ |
| 589 | if (dep->flags & DWC3_EP_STALL) |
Felipe Balbi | 7a60855 | 2014-09-24 14:19:52 -0500 | [diff] [blame] | 590 | __dwc3_gadget_ep_set_halt(dep, 0, false); |
Felipe Balbi | 687ef98 | 2014-04-16 10:30:33 -0500 | [diff] [blame] | 591 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 592 | reg = dwc3_readl(dwc->regs, DWC3_DALEPENA); |
| 593 | reg &= ~DWC3_DALEPENA_EP(dep->number); |
| 594 | dwc3_writel(dwc->regs, DWC3_DALEPENA, reg); |
| 595 | |
Felipe Balbi | 879631a | 2011-09-30 10:58:47 +0300 | [diff] [blame] | 596 | dep->stream_capable = false; |
Ido Shayevitz | f9c56cd | 2012-02-08 13:56:48 +0200 | [diff] [blame] | 597 | dep->endpoint.desc = NULL; |
Felipe Balbi | c90bfae | 2011-11-29 13:11:21 +0200 | [diff] [blame] | 598 | dep->comp_desc = NULL; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 599 | dep->type = 0; |
Felipe Balbi | 879631a | 2011-09-30 10:58:47 +0300 | [diff] [blame] | 600 | dep->flags = 0; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 601 | |
Felipe Balbi | aa73997 | 2015-07-20 14:48:13 -0500 | [diff] [blame] | 602 | snprintf(dep->name, sizeof(dep->name), "ep%d%s", |
| 603 | dep->number >> 1, |
| 604 | (dep->number & 1) ? "in" : "out"); |
| 605 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 606 | return 0; |
| 607 | } |
| 608 | |
| 609 | /* -------------------------------------------------------------------------- */ |
| 610 | |
| 611 | static int dwc3_gadget_ep0_enable(struct usb_ep *ep, |
| 612 | const struct usb_endpoint_descriptor *desc) |
| 613 | { |
| 614 | return -EINVAL; |
| 615 | } |
| 616 | |
| 617 | static int dwc3_gadget_ep0_disable(struct usb_ep *ep) |
| 618 | { |
| 619 | return -EINVAL; |
| 620 | } |
| 621 | |
| 622 | /* -------------------------------------------------------------------------- */ |
| 623 | |
| 624 | static int dwc3_gadget_ep_enable(struct usb_ep *ep, |
| 625 | const struct usb_endpoint_descriptor *desc) |
| 626 | { |
| 627 | struct dwc3_ep *dep; |
| 628 | struct dwc3 *dwc; |
| 629 | unsigned long flags; |
| 630 | int ret; |
| 631 | |
| 632 | if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) { |
| 633 | pr_debug("dwc3: invalid parameters\n"); |
| 634 | return -EINVAL; |
| 635 | } |
| 636 | |
| 637 | if (!desc->wMaxPacketSize) { |
| 638 | pr_debug("dwc3: missing wMaxPacketSize\n"); |
| 639 | return -EINVAL; |
| 640 | } |
| 641 | |
| 642 | dep = to_dwc3_ep(ep); |
| 643 | dwc = dep->dwc; |
| 644 | |
Felipe Balbi | 95ca961 | 2015-12-10 13:08:20 -0600 | [diff] [blame] | 645 | if (dev_WARN_ONCE(dwc->dev, dep->flags & DWC3_EP_ENABLED, |
| 646 | "%s is already enabled\n", |
| 647 | dep->name)) |
Felipe Balbi | c6f83f3 | 2012-08-15 12:28:29 +0300 | [diff] [blame] | 648 | return 0; |
Felipe Balbi | c6f83f3 | 2012-08-15 12:28:29 +0300 | [diff] [blame] | 649 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 650 | spin_lock_irqsave(&dwc->lock, flags); |
Paul Zimmerman | 265b70a | 2013-12-19 12:38:49 -0600 | [diff] [blame] | 651 | ret = __dwc3_gadget_ep_enable(dep, desc, ep->comp_desc, false, false); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 652 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 653 | |
| 654 | return ret; |
| 655 | } |
| 656 | |
| 657 | static int dwc3_gadget_ep_disable(struct usb_ep *ep) |
| 658 | { |
| 659 | struct dwc3_ep *dep; |
| 660 | struct dwc3 *dwc; |
| 661 | unsigned long flags; |
| 662 | int ret; |
| 663 | |
| 664 | if (!ep) { |
| 665 | pr_debug("dwc3: invalid parameters\n"); |
| 666 | return -EINVAL; |
| 667 | } |
| 668 | |
| 669 | dep = to_dwc3_ep(ep); |
| 670 | dwc = dep->dwc; |
| 671 | |
Felipe Balbi | 95ca961 | 2015-12-10 13:08:20 -0600 | [diff] [blame] | 672 | if (dev_WARN_ONCE(dwc->dev, !(dep->flags & DWC3_EP_ENABLED), |
| 673 | "%s is already disabled\n", |
| 674 | dep->name)) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 675 | return 0; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 676 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 677 | spin_lock_irqsave(&dwc->lock, flags); |
| 678 | ret = __dwc3_gadget_ep_disable(dep); |
| 679 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 680 | |
| 681 | return ret; |
| 682 | } |
| 683 | |
| 684 | static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep, |
| 685 | gfp_t gfp_flags) |
| 686 | { |
| 687 | struct dwc3_request *req; |
| 688 | struct dwc3_ep *dep = to_dwc3_ep(ep); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 689 | |
| 690 | req = kzalloc(sizeof(*req), gfp_flags); |
Jingoo Han | 734d5a5 | 2014-07-17 12:45:11 +0900 | [diff] [blame] | 691 | if (!req) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 692 | return NULL; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 693 | |
| 694 | req->epnum = dep->number; |
| 695 | req->dep = dep; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 696 | |
Felipe Balbi | 2c4cbe6e5 | 2014-04-30 17:45:10 -0500 | [diff] [blame] | 697 | trace_dwc3_alloc_request(req); |
| 698 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 699 | return &req->request; |
| 700 | } |
| 701 | |
| 702 | static void dwc3_gadget_ep_free_request(struct usb_ep *ep, |
| 703 | struct usb_request *request) |
| 704 | { |
| 705 | struct dwc3_request *req = to_dwc3_request(request); |
| 706 | |
Felipe Balbi | 2c4cbe6e5 | 2014-04-30 17:45:10 -0500 | [diff] [blame] | 707 | trace_dwc3_free_request(req); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 708 | kfree(req); |
| 709 | } |
| 710 | |
Felipe Balbi | c71fc37 | 2011-11-22 11:37:34 +0200 | [diff] [blame] | 711 | /** |
| 712 | * dwc3_prepare_one_trb - setup one TRB from one request |
| 713 | * @dep: endpoint for which this request is prepared |
| 714 | * @req: dwc3_request pointer |
| 715 | */ |
Felipe Balbi | 68e823e | 2011-11-28 12:25:01 +0200 | [diff] [blame] | 716 | static void dwc3_prepare_one_trb(struct dwc3_ep *dep, |
Felipe Balbi | eeb720f | 2011-11-28 12:46:59 +0200 | [diff] [blame] | 717 | struct dwc3_request *req, dma_addr_t dma, |
Pratyush Anand | e5ba5ec | 2013-01-14 15:59:37 +0530 | [diff] [blame] | 718 | unsigned length, unsigned last, unsigned chain, unsigned node) |
Felipe Balbi | c71fc37 | 2011-11-22 11:37:34 +0200 | [diff] [blame] | 719 | { |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 720 | struct dwc3_trb *trb; |
Felipe Balbi | c71fc37 | 2011-11-22 11:37:34 +0200 | [diff] [blame] | 721 | |
Felipe Balbi | 7381528 | 2015-01-27 13:48:14 -0600 | [diff] [blame] | 722 | dwc3_trace(trace_dwc3_gadget, "%s: req %p dma %08llx length %d%s%s", |
Felipe Balbi | eeb720f | 2011-11-28 12:46:59 +0200 | [diff] [blame] | 723 | dep->name, req, (unsigned long long) dma, |
| 724 | length, last ? " last" : "", |
| 725 | chain ? " chain" : ""); |
| 726 | |
Pratyush Anand | 915e202 | 2013-01-14 15:59:35 +0530 | [diff] [blame] | 727 | |
| 728 | trb = &dep->trb_pool[dep->free_slot & DWC3_TRB_MASK]; |
Felipe Balbi | c71fc37 | 2011-11-22 11:37:34 +0200 | [diff] [blame] | 729 | |
Felipe Balbi | eeb720f | 2011-11-28 12:46:59 +0200 | [diff] [blame] | 730 | if (!req->trb) { |
Felipe Balbi | aa3342c | 2016-03-14 11:01:31 +0200 | [diff] [blame] | 731 | dwc3_gadget_move_started_request(req); |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 732 | req->trb = trb; |
| 733 | req->trb_dma = dwc3_trb_dma_offset(dep, trb); |
Pratyush Anand | e5ba5ec | 2013-01-14 15:59:37 +0530 | [diff] [blame] | 734 | req->start_slot = dep->free_slot & DWC3_TRB_MASK; |
Felipe Balbi | eeb720f | 2011-11-28 12:46:59 +0200 | [diff] [blame] | 735 | } |
Felipe Balbi | c71fc37 | 2011-11-22 11:37:34 +0200 | [diff] [blame] | 736 | |
Pratyush Anand | e5ba5ec | 2013-01-14 15:59:37 +0530 | [diff] [blame] | 737 | dep->free_slot++; |
Zhuang Jin Can | 5cd8c48 | 2014-05-16 05:57:57 +0800 | [diff] [blame] | 738 | /* Skip the LINK-TRB on ISOC */ |
| 739 | if (((dep->free_slot & DWC3_TRB_MASK) == DWC3_TRB_NUM - 1) && |
| 740 | usb_endpoint_xfer_isoc(dep->endpoint.desc)) |
| 741 | dep->free_slot++; |
Pratyush Anand | e5ba5ec | 2013-01-14 15:59:37 +0530 | [diff] [blame] | 742 | |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 743 | trb->size = DWC3_TRB_SIZE_LENGTH(length); |
| 744 | trb->bpl = lower_32_bits(dma); |
| 745 | trb->bph = upper_32_bits(dma); |
Felipe Balbi | c71fc37 | 2011-11-22 11:37:34 +0200 | [diff] [blame] | 746 | |
Ido Shayevitz | 16e78db | 2012-03-12 20:25:24 +0200 | [diff] [blame] | 747 | switch (usb_endpoint_type(dep->endpoint.desc)) { |
Felipe Balbi | c71fc37 | 2011-11-22 11:37:34 +0200 | [diff] [blame] | 748 | case USB_ENDPOINT_XFER_CONTROL: |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 749 | trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP; |
Felipe Balbi | c71fc37 | 2011-11-22 11:37:34 +0200 | [diff] [blame] | 750 | break; |
| 751 | |
| 752 | case USB_ENDPOINT_XFER_ISOC: |
Pratyush Anand | e5ba5ec | 2013-01-14 15:59:37 +0530 | [diff] [blame] | 753 | if (!node) |
| 754 | trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST; |
| 755 | else |
| 756 | trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS; |
Felipe Balbi | ca4d44e | 2016-03-10 13:53:27 +0200 | [diff] [blame] | 757 | |
| 758 | /* always enable Interrupt on Missed ISOC */ |
| 759 | trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI; |
Felipe Balbi | c71fc37 | 2011-11-22 11:37:34 +0200 | [diff] [blame] | 760 | break; |
| 761 | |
| 762 | case USB_ENDPOINT_XFER_BULK: |
| 763 | case USB_ENDPOINT_XFER_INT: |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 764 | trb->ctrl = DWC3_TRBCTL_NORMAL; |
Felipe Balbi | c71fc37 | 2011-11-22 11:37:34 +0200 | [diff] [blame] | 765 | break; |
| 766 | default: |
| 767 | /* |
| 768 | * This is only possible with faulty memory because we |
| 769 | * checked it already :) |
| 770 | */ |
| 771 | BUG(); |
| 772 | } |
| 773 | |
Felipe Balbi | ca4d44e | 2016-03-10 13:53:27 +0200 | [diff] [blame] | 774 | /* always enable Continue on Short Packet */ |
| 775 | trb->ctrl |= DWC3_TRB_CTRL_CSP; |
Felipe Balbi | f3af365 | 2013-12-13 14:19:33 -0600 | [diff] [blame] | 776 | |
Felipe Balbi | ca4d44e | 2016-03-10 13:53:27 +0200 | [diff] [blame] | 777 | if (!req->request.no_interrupt) |
| 778 | trb->ctrl |= DWC3_TRB_CTRL_IOC | DWC3_TRB_CTRL_ISP_IMI; |
| 779 | |
| 780 | if (last) |
Pratyush Anand | e5ba5ec | 2013-01-14 15:59:37 +0530 | [diff] [blame] | 781 | trb->ctrl |= DWC3_TRB_CTRL_LST; |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 782 | |
Pratyush Anand | e5ba5ec | 2013-01-14 15:59:37 +0530 | [diff] [blame] | 783 | if (chain) |
| 784 | trb->ctrl |= DWC3_TRB_CTRL_CHN; |
| 785 | |
Ido Shayevitz | 16e78db | 2012-03-12 20:25:24 +0200 | [diff] [blame] | 786 | if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable) |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 787 | trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(req->request.stream_id); |
| 788 | |
| 789 | trb->ctrl |= DWC3_TRB_CTRL_HWO; |
Felipe Balbi | 2c4cbe6e5 | 2014-04-30 17:45:10 -0500 | [diff] [blame] | 790 | |
| 791 | trace_dwc3_prepare_trb(dep, trb); |
Felipe Balbi | c71fc37 | 2011-11-22 11:37:34 +0200 | [diff] [blame] | 792 | } |
| 793 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 794 | /* |
| 795 | * dwc3_prepare_trbs - setup TRBs from requests |
| 796 | * @dep: endpoint for which requests are being prepared |
| 797 | * @starting: true if the endpoint is idle and no requests are queued. |
| 798 | * |
Paul Zimmerman | 1d04679 | 2012-02-15 18:56:56 -0800 | [diff] [blame] | 799 | * The function goes through the requests list and sets up TRBs for the |
| 800 | * transfers. The function returns once there are no more TRBs available or |
| 801 | * it runs out of requests. |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 802 | */ |
Felipe Balbi | 68e823e | 2011-11-28 12:25:01 +0200 | [diff] [blame] | 803 | static void dwc3_prepare_trbs(struct dwc3_ep *dep, bool starting) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 804 | { |
Felipe Balbi | 68e823e | 2011-11-28 12:25:01 +0200 | [diff] [blame] | 805 | struct dwc3_request *req, *n; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 806 | u32 trbs_left; |
Paul Zimmerman | 8d62cd6 | 2012-02-15 13:35:06 +0200 | [diff] [blame] | 807 | u32 max; |
Felipe Balbi | c71fc37 | 2011-11-22 11:37:34 +0200 | [diff] [blame] | 808 | unsigned int last_one = 0; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 809 | |
| 810 | BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM); |
| 811 | |
| 812 | /* the first request must not be queued */ |
| 813 | trbs_left = (dep->busy_slot - dep->free_slot) & DWC3_TRB_MASK; |
Felipe Balbi | c71fc37 | 2011-11-22 11:37:34 +0200 | [diff] [blame] | 814 | |
Paul Zimmerman | 8d62cd6 | 2012-02-15 13:35:06 +0200 | [diff] [blame] | 815 | /* Can't wrap around on a non-isoc EP since there's no link TRB */ |
Ido Shayevitz | 16e78db | 2012-03-12 20:25:24 +0200 | [diff] [blame] | 816 | if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) { |
Paul Zimmerman | 8d62cd6 | 2012-02-15 13:35:06 +0200 | [diff] [blame] | 817 | max = DWC3_TRB_NUM - (dep->free_slot & DWC3_TRB_MASK); |
| 818 | if (trbs_left > max) |
| 819 | trbs_left = max; |
| 820 | } |
| 821 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 822 | /* |
Paul Zimmerman | 1d04679 | 2012-02-15 18:56:56 -0800 | [diff] [blame] | 823 | * If busy & slot are equal than it is either full or empty. If we are |
| 824 | * starting to process requests then we are empty. Otherwise we are |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 825 | * full and don't do anything |
| 826 | */ |
| 827 | if (!trbs_left) { |
| 828 | if (!starting) |
Felipe Balbi | 68e823e | 2011-11-28 12:25:01 +0200 | [diff] [blame] | 829 | return; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 830 | trbs_left = DWC3_TRB_NUM; |
| 831 | /* |
| 832 | * In case we start from scratch, we queue the ISOC requests |
| 833 | * starting from slot 1. This is done because we use ring |
| 834 | * buffer and have no LST bit to stop us. Instead, we place |
Paul Zimmerman | 1d04679 | 2012-02-15 18:56:56 -0800 | [diff] [blame] | 835 | * IOC bit every TRB_NUM/4. We try to avoid having an interrupt |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 836 | * after the first request so we start at slot 1 and have |
| 837 | * 7 requests proceed before we hit the first IOC. |
| 838 | * Other transfer types don't use the ring buffer and are |
| 839 | * processed from the first TRB until the last one. Since we |
| 840 | * don't wrap around we have to start at the beginning. |
| 841 | */ |
Ido Shayevitz | 16e78db | 2012-03-12 20:25:24 +0200 | [diff] [blame] | 842 | if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 843 | dep->busy_slot = 1; |
| 844 | dep->free_slot = 1; |
| 845 | } else { |
| 846 | dep->busy_slot = 0; |
| 847 | dep->free_slot = 0; |
| 848 | } |
| 849 | } |
| 850 | |
| 851 | /* The last TRB is a link TRB, not used for xfer */ |
Ido Shayevitz | 16e78db | 2012-03-12 20:25:24 +0200 | [diff] [blame] | 852 | if ((trbs_left <= 1) && usb_endpoint_xfer_isoc(dep->endpoint.desc)) |
Felipe Balbi | 68e823e | 2011-11-28 12:25:01 +0200 | [diff] [blame] | 853 | return; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 854 | |
Felipe Balbi | aa3342c | 2016-03-14 11:01:31 +0200 | [diff] [blame] | 855 | list_for_each_entry_safe(req, n, &dep->pending_list, list) { |
Felipe Balbi | eeb720f | 2011-11-28 12:46:59 +0200 | [diff] [blame] | 856 | unsigned length; |
| 857 | dma_addr_t dma; |
Pratyush Anand | e5ba5ec | 2013-01-14 15:59:37 +0530 | [diff] [blame] | 858 | last_one = false; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 859 | |
Felipe Balbi | eeb720f | 2011-11-28 12:46:59 +0200 | [diff] [blame] | 860 | if (req->request.num_mapped_sgs > 0) { |
| 861 | struct usb_request *request = &req->request; |
| 862 | struct scatterlist *sg = request->sg; |
| 863 | struct scatterlist *s; |
| 864 | int i; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 865 | |
Felipe Balbi | eeb720f | 2011-11-28 12:46:59 +0200 | [diff] [blame] | 866 | for_each_sg(sg, s, request->num_mapped_sgs, i) { |
| 867 | unsigned chain = true; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 868 | |
Felipe Balbi | eeb720f | 2011-11-28 12:46:59 +0200 | [diff] [blame] | 869 | length = sg_dma_len(s); |
| 870 | dma = sg_dma_address(s); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 871 | |
Paul Zimmerman | 1d04679 | 2012-02-15 18:56:56 -0800 | [diff] [blame] | 872 | if (i == (request->num_mapped_sgs - 1) || |
| 873 | sg_is_last(s)) { |
Felipe Balbi | aa3342c | 2016-03-14 11:01:31 +0200 | [diff] [blame] | 874 | if (list_empty(&dep->pending_list)) |
Pratyush Anand | e5ba5ec | 2013-01-14 15:59:37 +0530 | [diff] [blame] | 875 | last_one = true; |
Felipe Balbi | eeb720f | 2011-11-28 12:46:59 +0200 | [diff] [blame] | 876 | chain = false; |
| 877 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 878 | |
Felipe Balbi | eeb720f | 2011-11-28 12:46:59 +0200 | [diff] [blame] | 879 | trbs_left--; |
| 880 | if (!trbs_left) |
| 881 | last_one = true; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 882 | |
Felipe Balbi | eeb720f | 2011-11-28 12:46:59 +0200 | [diff] [blame] | 883 | if (last_one) |
| 884 | chain = false; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 885 | |
Felipe Balbi | eeb720f | 2011-11-28 12:46:59 +0200 | [diff] [blame] | 886 | dwc3_prepare_one_trb(dep, req, dma, length, |
Pratyush Anand | e5ba5ec | 2013-01-14 15:59:37 +0530 | [diff] [blame] | 887 | last_one, chain, i); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 888 | |
Felipe Balbi | eeb720f | 2011-11-28 12:46:59 +0200 | [diff] [blame] | 889 | if (last_one) |
| 890 | break; |
| 891 | } |
Amit Virdi | 39e6063 | 2015-01-13 14:27:21 +0530 | [diff] [blame] | 892 | |
| 893 | if (last_one) |
| 894 | break; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 895 | } else { |
Felipe Balbi | eeb720f | 2011-11-28 12:46:59 +0200 | [diff] [blame] | 896 | dma = req->request.dma; |
| 897 | length = req->request.length; |
| 898 | trbs_left--; |
| 899 | |
| 900 | if (!trbs_left) |
| 901 | last_one = 1; |
| 902 | |
| 903 | /* Is this the last request? */ |
Felipe Balbi | aa3342c | 2016-03-14 11:01:31 +0200 | [diff] [blame] | 904 | if (list_is_last(&req->list, &dep->pending_list)) |
Felipe Balbi | eeb720f | 2011-11-28 12:46:59 +0200 | [diff] [blame] | 905 | last_one = 1; |
| 906 | |
| 907 | dwc3_prepare_one_trb(dep, req, dma, length, |
Pratyush Anand | e5ba5ec | 2013-01-14 15:59:37 +0530 | [diff] [blame] | 908 | last_one, false, 0); |
Felipe Balbi | eeb720f | 2011-11-28 12:46:59 +0200 | [diff] [blame] | 909 | |
| 910 | if (last_one) |
| 911 | break; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 912 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 913 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 914 | } |
| 915 | |
| 916 | static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep, u16 cmd_param, |
| 917 | int start_new) |
| 918 | { |
| 919 | struct dwc3_gadget_ep_cmd_params params; |
| 920 | struct dwc3_request *req; |
| 921 | struct dwc3 *dwc = dep->dwc; |
| 922 | int ret; |
| 923 | u32 cmd; |
| 924 | |
| 925 | if (start_new && (dep->flags & DWC3_EP_BUSY)) { |
Felipe Balbi | 7381528 | 2015-01-27 13:48:14 -0600 | [diff] [blame] | 926 | dwc3_trace(trace_dwc3_gadget, "%s: endpoint busy", dep->name); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 927 | return -EBUSY; |
| 928 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 929 | |
| 930 | /* |
| 931 | * If we are getting here after a short-out-packet we don't enqueue any |
| 932 | * new requests as we try to set the IOC bit only on the last request. |
| 933 | */ |
| 934 | if (start_new) { |
Felipe Balbi | aa3342c | 2016-03-14 11:01:31 +0200 | [diff] [blame] | 935 | if (list_empty(&dep->started_list)) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 936 | dwc3_prepare_trbs(dep, start_new); |
| 937 | |
| 938 | /* req points to the first request which will be sent */ |
Felipe Balbi | aa3342c | 2016-03-14 11:01:31 +0200 | [diff] [blame] | 939 | req = next_request(&dep->started_list); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 940 | } else { |
Felipe Balbi | 68e823e | 2011-11-28 12:25:01 +0200 | [diff] [blame] | 941 | dwc3_prepare_trbs(dep, start_new); |
| 942 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 943 | /* |
Paul Zimmerman | 1d04679 | 2012-02-15 18:56:56 -0800 | [diff] [blame] | 944 | * req points to the first request where HWO changed from 0 to 1 |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 945 | */ |
Felipe Balbi | aa3342c | 2016-03-14 11:01:31 +0200 | [diff] [blame] | 946 | req = next_request(&dep->started_list); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 947 | } |
| 948 | if (!req) { |
| 949 | dep->flags |= DWC3_EP_PENDING_REQUEST; |
| 950 | return 0; |
| 951 | } |
| 952 | |
| 953 | memset(¶ms, 0, sizeof(params)); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 954 | |
Pratyush Anand | 1877d6c | 2013-01-14 15:59:36 +0530 | [diff] [blame] | 955 | if (start_new) { |
| 956 | params.param0 = upper_32_bits(req->trb_dma); |
| 957 | params.param1 = lower_32_bits(req->trb_dma); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 958 | cmd = DWC3_DEPCMD_STARTTRANSFER; |
Pratyush Anand | 1877d6c | 2013-01-14 15:59:36 +0530 | [diff] [blame] | 959 | } else { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 960 | cmd = DWC3_DEPCMD_UPDATETRANSFER; |
Pratyush Anand | 1877d6c | 2013-01-14 15:59:36 +0530 | [diff] [blame] | 961 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 962 | |
| 963 | cmd |= DWC3_DEPCMD_PARAM(cmd_param); |
| 964 | ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, ¶ms); |
| 965 | if (ret < 0) { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 966 | /* |
| 967 | * FIXME we need to iterate over the list of requests |
| 968 | * here and stop, unmap, free and del each of the linked |
Paul Zimmerman | 1d04679 | 2012-02-15 18:56:56 -0800 | [diff] [blame] | 969 | * requests instead of what we do now. |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 970 | */ |
Felipe Balbi | 0fc9a1b | 2011-12-19 11:32:34 +0200 | [diff] [blame] | 971 | usb_gadget_unmap_request(&dwc->gadget, &req->request, |
| 972 | req->direction); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 973 | list_del(&req->list); |
| 974 | return ret; |
| 975 | } |
| 976 | |
| 977 | dep->flags |= DWC3_EP_BUSY; |
Felipe Balbi | 25b8ff6 | 2011-11-04 12:32:47 +0200 | [diff] [blame] | 978 | |
Paul Zimmerman | f898ae0 | 2012-03-29 18:16:54 +0000 | [diff] [blame] | 979 | if (start_new) { |
Felipe Balbi | b4996a8 | 2012-06-06 12:04:13 +0300 | [diff] [blame] | 980 | dep->resource_index = dwc3_gadget_ep_get_transfer_index(dwc, |
Paul Zimmerman | f898ae0 | 2012-03-29 18:16:54 +0000 | [diff] [blame] | 981 | dep->number); |
Felipe Balbi | b4996a8 | 2012-06-06 12:04:13 +0300 | [diff] [blame] | 982 | WARN_ON_ONCE(!dep->resource_index); |
Paul Zimmerman | f898ae0 | 2012-03-29 18:16:54 +0000 | [diff] [blame] | 983 | } |
Felipe Balbi | 25b8ff6 | 2011-11-04 12:32:47 +0200 | [diff] [blame] | 984 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 985 | return 0; |
| 986 | } |
| 987 | |
Pratyush Anand | d6d6ec7 | 2012-05-25 18:54:56 +0530 | [diff] [blame] | 988 | static void __dwc3_gadget_start_isoc(struct dwc3 *dwc, |
| 989 | struct dwc3_ep *dep, u32 cur_uf) |
| 990 | { |
| 991 | u32 uf; |
| 992 | |
Felipe Balbi | aa3342c | 2016-03-14 11:01:31 +0200 | [diff] [blame] | 993 | if (list_empty(&dep->pending_list)) { |
Felipe Balbi | 7381528 | 2015-01-27 13:48:14 -0600 | [diff] [blame] | 994 | dwc3_trace(trace_dwc3_gadget, |
| 995 | "ISOC ep %s run out for requests", |
| 996 | dep->name); |
Pratyush Anand | f4a53c5 | 2012-08-30 12:21:43 +0530 | [diff] [blame] | 997 | dep->flags |= DWC3_EP_PENDING_REQUEST; |
Pratyush Anand | d6d6ec7 | 2012-05-25 18:54:56 +0530 | [diff] [blame] | 998 | return; |
| 999 | } |
| 1000 | |
| 1001 | /* 4 micro frames in the future */ |
| 1002 | uf = cur_uf + dep->interval * 4; |
| 1003 | |
| 1004 | __dwc3_gadget_kick_transfer(dep, uf, 1); |
| 1005 | } |
| 1006 | |
| 1007 | static void dwc3_gadget_start_isoc(struct dwc3 *dwc, |
| 1008 | struct dwc3_ep *dep, const struct dwc3_event_depevt *event) |
| 1009 | { |
| 1010 | u32 cur_uf, mask; |
| 1011 | |
| 1012 | mask = ~(dep->interval - 1); |
| 1013 | cur_uf = event->parameters & mask; |
| 1014 | |
| 1015 | __dwc3_gadget_start_isoc(dwc, dep, cur_uf); |
| 1016 | } |
| 1017 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1018 | static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req) |
| 1019 | { |
Felipe Balbi | 0fc9a1b | 2011-12-19 11:32:34 +0200 | [diff] [blame] | 1020 | struct dwc3 *dwc = dep->dwc; |
| 1021 | int ret; |
| 1022 | |
Felipe Balbi | bb42398 | 2015-11-16 15:31:21 -0600 | [diff] [blame] | 1023 | if (!dep->endpoint.desc) { |
Felipe Balbi | ec5e795 | 2015-11-16 16:04:13 -0600 | [diff] [blame] | 1024 | dwc3_trace(trace_dwc3_gadget, |
| 1025 | "trying to queue request %p to disabled %s\n", |
Felipe Balbi | bb42398 | 2015-11-16 15:31:21 -0600 | [diff] [blame] | 1026 | &req->request, dep->endpoint.name); |
| 1027 | return -ESHUTDOWN; |
| 1028 | } |
| 1029 | |
| 1030 | if (WARN(req->dep != dep, "request %p belongs to '%s'\n", |
| 1031 | &req->request, req->dep->name)) { |
Felipe Balbi | ec5e795 | 2015-11-16 16:04:13 -0600 | [diff] [blame] | 1032 | dwc3_trace(trace_dwc3_gadget, "request %p belongs to '%s'\n", |
| 1033 | &req->request, req->dep->name); |
Felipe Balbi | bb42398 | 2015-11-16 15:31:21 -0600 | [diff] [blame] | 1034 | return -EINVAL; |
| 1035 | } |
| 1036 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1037 | req->request.actual = 0; |
| 1038 | req->request.status = -EINPROGRESS; |
| 1039 | req->direction = dep->direction; |
| 1040 | req->epnum = dep->number; |
| 1041 | |
Felipe Balbi | fe84f52 | 2015-09-01 09:01:38 -0500 | [diff] [blame] | 1042 | trace_dwc3_ep_queue(req); |
| 1043 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1044 | /* |
| 1045 | * We only add to our list of requests now and |
| 1046 | * start consuming the list once we get XferNotReady |
| 1047 | * IRQ. |
| 1048 | * |
| 1049 | * That way, we avoid doing anything that we don't need |
| 1050 | * to do now and defer it until the point we receive a |
| 1051 | * particular token from the Host side. |
| 1052 | * |
| 1053 | * This will also avoid Host cancelling URBs due to too |
Paul Zimmerman | 1d04679 | 2012-02-15 18:56:56 -0800 | [diff] [blame] | 1054 | * many NAKs. |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1055 | */ |
Felipe Balbi | 0fc9a1b | 2011-12-19 11:32:34 +0200 | [diff] [blame] | 1056 | ret = usb_gadget_map_request(&dwc->gadget, &req->request, |
| 1057 | dep->direction); |
| 1058 | if (ret) |
| 1059 | return ret; |
| 1060 | |
Felipe Balbi | aa3342c | 2016-03-14 11:01:31 +0200 | [diff] [blame] | 1061 | list_add_tail(&req->list, &dep->pending_list); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1062 | |
| 1063 | /* |
Felipe Balbi | 1d6a391 | 2015-09-14 11:27:46 -0500 | [diff] [blame] | 1064 | * If there are no pending requests and the endpoint isn't already |
| 1065 | * busy, we will just start the request straight away. |
| 1066 | * |
| 1067 | * This will save one IRQ (XFER_NOT_READY) and possibly make it a |
| 1068 | * little bit faster. |
| 1069 | */ |
| 1070 | if (!usb_endpoint_xfer_isoc(dep->endpoint.desc) && |
Felipe Balbi | 62e345a | 2015-11-30 15:24:29 -0600 | [diff] [blame] | 1071 | !usb_endpoint_xfer_int(dep->endpoint.desc) && |
Felipe Balbi | 1d6a391 | 2015-09-14 11:27:46 -0500 | [diff] [blame] | 1072 | !(dep->flags & DWC3_EP_BUSY)) { |
| 1073 | ret = __dwc3_gadget_kick_transfer(dep, 0, true); |
Felipe Balbi | a8f3281 | 2015-09-16 10:40:07 -0500 | [diff] [blame] | 1074 | goto out; |
Felipe Balbi | 1d6a391 | 2015-09-14 11:27:46 -0500 | [diff] [blame] | 1075 | } |
| 1076 | |
| 1077 | /* |
Felipe Balbi | b511e5e | 2012-06-06 12:00:50 +0300 | [diff] [blame] | 1078 | * There are a few special cases: |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1079 | * |
Paul Zimmerman | f898ae0 | 2012-03-29 18:16:54 +0000 | [diff] [blame] | 1080 | * 1. XferNotReady with empty list of requests. We need to kick the |
| 1081 | * transfer here in that situation, otherwise we will be NAKing |
| 1082 | * forever. If we get XferNotReady before gadget driver has a |
| 1083 | * chance to queue a request, we will ACK the IRQ but won't be |
| 1084 | * able to receive the data until the next request is queued. |
| 1085 | * The following code is handling exactly that. |
| 1086 | * |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1087 | */ |
| 1088 | if (dep->flags & DWC3_EP_PENDING_REQUEST) { |
Pratyush Anand | f4a53c5 | 2012-08-30 12:21:43 +0530 | [diff] [blame] | 1089 | /* |
| 1090 | * If xfernotready is already elapsed and it is a case |
| 1091 | * of isoc transfer, then issue END TRANSFER, so that |
| 1092 | * you can receive xfernotready again and can have |
| 1093 | * notion of current microframe. |
| 1094 | */ |
| 1095 | if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) { |
Felipe Balbi | aa3342c | 2016-03-14 11:01:31 +0200 | [diff] [blame] | 1096 | if (list_empty(&dep->started_list)) { |
Paul Zimmerman | b992e68 | 2012-04-27 14:17:35 +0300 | [diff] [blame] | 1097 | dwc3_stop_active_transfer(dwc, dep->number, true); |
Pratyush Anand | cdc359d | 2013-01-14 15:59:34 +0530 | [diff] [blame] | 1098 | dep->flags = DWC3_EP_ENABLED; |
| 1099 | } |
Pratyush Anand | f4a53c5 | 2012-08-30 12:21:43 +0530 | [diff] [blame] | 1100 | return 0; |
| 1101 | } |
| 1102 | |
Felipe Balbi | b511e5e | 2012-06-06 12:00:50 +0300 | [diff] [blame] | 1103 | ret = __dwc3_gadget_kick_transfer(dep, 0, true); |
Felipe Balbi | 8918591 | 2015-09-15 09:49:14 -0500 | [diff] [blame] | 1104 | if (!ret) |
| 1105 | dep->flags &= ~DWC3_EP_PENDING_REQUEST; |
| 1106 | |
Felipe Balbi | a8f3281 | 2015-09-16 10:40:07 -0500 | [diff] [blame] | 1107 | goto out; |
Felipe Balbi | a092532 | 2012-05-22 10:24:11 +0300 | [diff] [blame] | 1108 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1109 | |
Felipe Balbi | b511e5e | 2012-06-06 12:00:50 +0300 | [diff] [blame] | 1110 | /* |
| 1111 | * 2. XferInProgress on Isoc EP with an active transfer. We need to |
| 1112 | * kick the transfer here after queuing a request, otherwise the |
| 1113 | * core may not see the modified TRB(s). |
| 1114 | */ |
| 1115 | if (usb_endpoint_xfer_isoc(dep->endpoint.desc) && |
Pratyush Anand | 79c9046 | 2012-08-07 16:54:18 +0530 | [diff] [blame] | 1116 | (dep->flags & DWC3_EP_BUSY) && |
| 1117 | !(dep->flags & DWC3_EP_MISSED_ISOC)) { |
Felipe Balbi | b4996a8 | 2012-06-06 12:04:13 +0300 | [diff] [blame] | 1118 | WARN_ON_ONCE(!dep->resource_index); |
| 1119 | ret = __dwc3_gadget_kick_transfer(dep, dep->resource_index, |
Felipe Balbi | b511e5e | 2012-06-06 12:00:50 +0300 | [diff] [blame] | 1120 | false); |
Felipe Balbi | a8f3281 | 2015-09-16 10:40:07 -0500 | [diff] [blame] | 1121 | goto out; |
Felipe Balbi | b511e5e | 2012-06-06 12:00:50 +0300 | [diff] [blame] | 1122 | } |
| 1123 | |
Felipe Balbi | b997ada | 2012-07-26 13:26:50 +0300 | [diff] [blame] | 1124 | /* |
| 1125 | * 4. Stream Capable Bulk Endpoints. We need to start the transfer |
| 1126 | * right away, otherwise host will not know we have streams to be |
| 1127 | * handled. |
| 1128 | */ |
Felipe Balbi | a8f3281 | 2015-09-16 10:40:07 -0500 | [diff] [blame] | 1129 | if (dep->stream_capable) |
Felipe Balbi | b997ada | 2012-07-26 13:26:50 +0300 | [diff] [blame] | 1130 | ret = __dwc3_gadget_kick_transfer(dep, 0, true); |
Felipe Balbi | b997ada | 2012-07-26 13:26:50 +0300 | [diff] [blame] | 1131 | |
Felipe Balbi | a8f3281 | 2015-09-16 10:40:07 -0500 | [diff] [blame] | 1132 | out: |
| 1133 | if (ret && ret != -EBUSY) |
Felipe Balbi | ec5e795 | 2015-11-16 16:04:13 -0600 | [diff] [blame] | 1134 | dwc3_trace(trace_dwc3_gadget, |
| 1135 | "%s: failed to kick transfers\n", |
Felipe Balbi | a8f3281 | 2015-09-16 10:40:07 -0500 | [diff] [blame] | 1136 | dep->name); |
| 1137 | if (ret == -EBUSY) |
| 1138 | ret = 0; |
| 1139 | |
| 1140 | return ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1141 | } |
| 1142 | |
Felipe Balbi | 04c03d1 | 2015-12-02 10:06:45 -0600 | [diff] [blame] | 1143 | static void __dwc3_gadget_ep_zlp_complete(struct usb_ep *ep, |
| 1144 | struct usb_request *request) |
| 1145 | { |
| 1146 | dwc3_gadget_ep_free_request(ep, request); |
| 1147 | } |
| 1148 | |
| 1149 | static int __dwc3_gadget_ep_queue_zlp(struct dwc3 *dwc, struct dwc3_ep *dep) |
| 1150 | { |
| 1151 | struct dwc3_request *req; |
| 1152 | struct usb_request *request; |
| 1153 | struct usb_ep *ep = &dep->endpoint; |
| 1154 | |
| 1155 | dwc3_trace(trace_dwc3_gadget, "queueing ZLP\n"); |
| 1156 | request = dwc3_gadget_ep_alloc_request(ep, GFP_ATOMIC); |
| 1157 | if (!request) |
| 1158 | return -ENOMEM; |
| 1159 | |
| 1160 | request->length = 0; |
| 1161 | request->buf = dwc->zlp_buf; |
| 1162 | request->complete = __dwc3_gadget_ep_zlp_complete; |
| 1163 | |
| 1164 | req = to_dwc3_request(request); |
| 1165 | |
| 1166 | return __dwc3_gadget_ep_queue(dep, req); |
| 1167 | } |
| 1168 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1169 | static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request, |
| 1170 | gfp_t gfp_flags) |
| 1171 | { |
| 1172 | struct dwc3_request *req = to_dwc3_request(request); |
| 1173 | struct dwc3_ep *dep = to_dwc3_ep(ep); |
| 1174 | struct dwc3 *dwc = dep->dwc; |
| 1175 | |
| 1176 | unsigned long flags; |
| 1177 | |
| 1178 | int ret; |
| 1179 | |
Zhuang Jin Can | fdee4eb | 2014-09-03 14:26:34 +0800 | [diff] [blame] | 1180 | spin_lock_irqsave(&dwc->lock, flags); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1181 | ret = __dwc3_gadget_ep_queue(dep, req); |
Felipe Balbi | 04c03d1 | 2015-12-02 10:06:45 -0600 | [diff] [blame] | 1182 | |
| 1183 | /* |
| 1184 | * Okay, here's the thing, if gadget driver has requested for a ZLP by |
| 1185 | * setting request->zero, instead of doing magic, we will just queue an |
| 1186 | * extra usb_request ourselves so that it gets handled the same way as |
| 1187 | * any other request. |
| 1188 | */ |
John Youn | d9261898 | 2015-12-22 12:23:20 -0800 | [diff] [blame] | 1189 | if (ret == 0 && request->zero && request->length && |
| 1190 | (request->length % ep->maxpacket == 0)) |
Felipe Balbi | 04c03d1 | 2015-12-02 10:06:45 -0600 | [diff] [blame] | 1191 | ret = __dwc3_gadget_ep_queue_zlp(dwc, dep); |
| 1192 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1193 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 1194 | |
| 1195 | return ret; |
| 1196 | } |
| 1197 | |
| 1198 | static int dwc3_gadget_ep_dequeue(struct usb_ep *ep, |
| 1199 | struct usb_request *request) |
| 1200 | { |
| 1201 | struct dwc3_request *req = to_dwc3_request(request); |
| 1202 | struct dwc3_request *r = NULL; |
| 1203 | |
| 1204 | struct dwc3_ep *dep = to_dwc3_ep(ep); |
| 1205 | struct dwc3 *dwc = dep->dwc; |
| 1206 | |
| 1207 | unsigned long flags; |
| 1208 | int ret = 0; |
| 1209 | |
Felipe Balbi | 2c4cbe6e5 | 2014-04-30 17:45:10 -0500 | [diff] [blame] | 1210 | trace_dwc3_ep_dequeue(req); |
| 1211 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1212 | spin_lock_irqsave(&dwc->lock, flags); |
| 1213 | |
Felipe Balbi | aa3342c | 2016-03-14 11:01:31 +0200 | [diff] [blame] | 1214 | list_for_each_entry(r, &dep->pending_list, list) { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1215 | if (r == req) |
| 1216 | break; |
| 1217 | } |
| 1218 | |
| 1219 | if (r != req) { |
Felipe Balbi | aa3342c | 2016-03-14 11:01:31 +0200 | [diff] [blame] | 1220 | list_for_each_entry(r, &dep->started_list, list) { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1221 | if (r == req) |
| 1222 | break; |
| 1223 | } |
| 1224 | if (r == req) { |
| 1225 | /* wait until it is processed */ |
Paul Zimmerman | b992e68 | 2012-04-27 14:17:35 +0300 | [diff] [blame] | 1226 | dwc3_stop_active_transfer(dwc, dep->number, true); |
Pratyush Anand | e8d4e8b | 2012-06-15 11:54:00 +0530 | [diff] [blame] | 1227 | goto out1; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1228 | } |
| 1229 | dev_err(dwc->dev, "request %p was not queued to %s\n", |
| 1230 | request, ep->name); |
| 1231 | ret = -EINVAL; |
| 1232 | goto out0; |
| 1233 | } |
| 1234 | |
Pratyush Anand | e8d4e8b | 2012-06-15 11:54:00 +0530 | [diff] [blame] | 1235 | out1: |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1236 | /* giveback the request */ |
| 1237 | dwc3_gadget_giveback(dep, req, -ECONNRESET); |
| 1238 | |
| 1239 | out0: |
| 1240 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 1241 | |
| 1242 | return ret; |
| 1243 | } |
| 1244 | |
Felipe Balbi | 7a60855 | 2014-09-24 14:19:52 -0500 | [diff] [blame] | 1245 | int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1246 | { |
| 1247 | struct dwc3_gadget_ep_cmd_params params; |
| 1248 | struct dwc3 *dwc = dep->dwc; |
| 1249 | int ret; |
| 1250 | |
Felipe Balbi | 5ad02fb | 2014-09-24 10:48:26 -0500 | [diff] [blame] | 1251 | if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) { |
| 1252 | dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name); |
| 1253 | return -EINVAL; |
| 1254 | } |
| 1255 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1256 | memset(¶ms, 0x00, sizeof(params)); |
| 1257 | |
| 1258 | if (value) { |
Felipe Balbi | 7a60855 | 2014-09-24 14:19:52 -0500 | [diff] [blame] | 1259 | if (!protocol && ((dep->direction && dep->flags & DWC3_EP_BUSY) || |
Felipe Balbi | aa3342c | 2016-03-14 11:01:31 +0200 | [diff] [blame] | 1260 | (!list_empty(&dep->started_list) || |
| 1261 | !list_empty(&dep->pending_list)))) { |
Felipe Balbi | ec5e795 | 2015-11-16 16:04:13 -0600 | [diff] [blame] | 1262 | dwc3_trace(trace_dwc3_gadget, |
| 1263 | "%s: pending request, cannot halt\n", |
Felipe Balbi | 7a60855 | 2014-09-24 14:19:52 -0500 | [diff] [blame] | 1264 | dep->name); |
| 1265 | return -EAGAIN; |
| 1266 | } |
| 1267 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1268 | ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, |
| 1269 | DWC3_DEPCMD_SETSTALL, ¶ms); |
| 1270 | if (ret) |
Dan Carpenter | 3f89204 | 2014-03-07 14:20:22 +0300 | [diff] [blame] | 1271 | dev_err(dwc->dev, "failed to set STALL on %s\n", |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1272 | dep->name); |
| 1273 | else |
| 1274 | dep->flags |= DWC3_EP_STALL; |
| 1275 | } else { |
| 1276 | ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, |
| 1277 | DWC3_DEPCMD_CLEARSTALL, ¶ms); |
| 1278 | if (ret) |
Dan Carpenter | 3f89204 | 2014-03-07 14:20:22 +0300 | [diff] [blame] | 1279 | dev_err(dwc->dev, "failed to clear STALL on %s\n", |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1280 | dep->name); |
| 1281 | else |
Alan Stern | a535d81 | 2013-11-01 12:05:12 -0400 | [diff] [blame] | 1282 | dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1283 | } |
Paul Zimmerman | 5275455 | 2011-09-30 10:58:44 +0300 | [diff] [blame] | 1284 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1285 | return ret; |
| 1286 | } |
| 1287 | |
| 1288 | static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value) |
| 1289 | { |
| 1290 | struct dwc3_ep *dep = to_dwc3_ep(ep); |
| 1291 | struct dwc3 *dwc = dep->dwc; |
| 1292 | |
| 1293 | unsigned long flags; |
| 1294 | |
| 1295 | int ret; |
| 1296 | |
| 1297 | spin_lock_irqsave(&dwc->lock, flags); |
Felipe Balbi | 7a60855 | 2014-09-24 14:19:52 -0500 | [diff] [blame] | 1298 | ret = __dwc3_gadget_ep_set_halt(dep, value, false); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1299 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 1300 | |
| 1301 | return ret; |
| 1302 | } |
| 1303 | |
| 1304 | static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep) |
| 1305 | { |
| 1306 | struct dwc3_ep *dep = to_dwc3_ep(ep); |
Paul Zimmerman | 249a456 | 2012-02-24 17:32:16 -0800 | [diff] [blame] | 1307 | struct dwc3 *dwc = dep->dwc; |
| 1308 | unsigned long flags; |
Felipe Balbi | 95aa4e8 | 2014-09-24 10:50:14 -0500 | [diff] [blame] | 1309 | int ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1310 | |
Paul Zimmerman | 249a456 | 2012-02-24 17:32:16 -0800 | [diff] [blame] | 1311 | spin_lock_irqsave(&dwc->lock, flags); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1312 | dep->flags |= DWC3_EP_WEDGE; |
| 1313 | |
Pratyush Anand | 08f0d96 | 2012-06-25 22:40:43 +0530 | [diff] [blame] | 1314 | if (dep->number == 0 || dep->number == 1) |
Felipe Balbi | 95aa4e8 | 2014-09-24 10:50:14 -0500 | [diff] [blame] | 1315 | ret = __dwc3_gadget_ep0_set_halt(ep, 1); |
Pratyush Anand | 08f0d96 | 2012-06-25 22:40:43 +0530 | [diff] [blame] | 1316 | else |
Felipe Balbi | 7a60855 | 2014-09-24 14:19:52 -0500 | [diff] [blame] | 1317 | ret = __dwc3_gadget_ep_set_halt(dep, 1, false); |
Felipe Balbi | 95aa4e8 | 2014-09-24 10:50:14 -0500 | [diff] [blame] | 1318 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 1319 | |
| 1320 | return ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1321 | } |
| 1322 | |
| 1323 | /* -------------------------------------------------------------------------- */ |
| 1324 | |
| 1325 | static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = { |
| 1326 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 1327 | .bDescriptorType = USB_DT_ENDPOINT, |
| 1328 | .bmAttributes = USB_ENDPOINT_XFER_CONTROL, |
| 1329 | }; |
| 1330 | |
| 1331 | static const struct usb_ep_ops dwc3_gadget_ep0_ops = { |
| 1332 | .enable = dwc3_gadget_ep0_enable, |
| 1333 | .disable = dwc3_gadget_ep0_disable, |
| 1334 | .alloc_request = dwc3_gadget_ep_alloc_request, |
| 1335 | .free_request = dwc3_gadget_ep_free_request, |
| 1336 | .queue = dwc3_gadget_ep0_queue, |
| 1337 | .dequeue = dwc3_gadget_ep_dequeue, |
Pratyush Anand | 08f0d96 | 2012-06-25 22:40:43 +0530 | [diff] [blame] | 1338 | .set_halt = dwc3_gadget_ep0_set_halt, |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1339 | .set_wedge = dwc3_gadget_ep_set_wedge, |
| 1340 | }; |
| 1341 | |
| 1342 | static const struct usb_ep_ops dwc3_gadget_ep_ops = { |
| 1343 | .enable = dwc3_gadget_ep_enable, |
| 1344 | .disable = dwc3_gadget_ep_disable, |
| 1345 | .alloc_request = dwc3_gadget_ep_alloc_request, |
| 1346 | .free_request = dwc3_gadget_ep_free_request, |
| 1347 | .queue = dwc3_gadget_ep_queue, |
| 1348 | .dequeue = dwc3_gadget_ep_dequeue, |
| 1349 | .set_halt = dwc3_gadget_ep_set_halt, |
| 1350 | .set_wedge = dwc3_gadget_ep_set_wedge, |
| 1351 | }; |
| 1352 | |
| 1353 | /* -------------------------------------------------------------------------- */ |
| 1354 | |
| 1355 | static int dwc3_gadget_get_frame(struct usb_gadget *g) |
| 1356 | { |
| 1357 | struct dwc3 *dwc = gadget_to_dwc(g); |
| 1358 | u32 reg; |
| 1359 | |
| 1360 | reg = dwc3_readl(dwc->regs, DWC3_DSTS); |
| 1361 | return DWC3_DSTS_SOFFN(reg); |
| 1362 | } |
| 1363 | |
| 1364 | static int dwc3_gadget_wakeup(struct usb_gadget *g) |
| 1365 | { |
| 1366 | struct dwc3 *dwc = gadget_to_dwc(g); |
| 1367 | |
| 1368 | unsigned long timeout; |
| 1369 | unsigned long flags; |
| 1370 | |
| 1371 | u32 reg; |
| 1372 | |
| 1373 | int ret = 0; |
| 1374 | |
| 1375 | u8 link_state; |
| 1376 | u8 speed; |
| 1377 | |
| 1378 | spin_lock_irqsave(&dwc->lock, flags); |
| 1379 | |
| 1380 | /* |
| 1381 | * According to the Databook Remote wakeup request should |
| 1382 | * be issued only when the device is in early suspend state. |
| 1383 | * |
| 1384 | * We can check that via USB Link State bits in DSTS register. |
| 1385 | */ |
| 1386 | reg = dwc3_readl(dwc->regs, DWC3_DSTS); |
| 1387 | |
| 1388 | speed = reg & DWC3_DSTS_CONNECTSPD; |
John Youn | ee5cd41 | 2016-02-05 17:08:45 -0800 | [diff] [blame] | 1389 | if ((speed == DWC3_DSTS_SUPERSPEED) || |
| 1390 | (speed == DWC3_DSTS_SUPERSPEED_PLUS)) { |
Felipe Balbi | ec5e795 | 2015-11-16 16:04:13 -0600 | [diff] [blame] | 1391 | dwc3_trace(trace_dwc3_gadget, "no wakeup on SuperSpeed\n"); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1392 | ret = -EINVAL; |
| 1393 | goto out; |
| 1394 | } |
| 1395 | |
| 1396 | link_state = DWC3_DSTS_USBLNKST(reg); |
| 1397 | |
| 1398 | switch (link_state) { |
| 1399 | case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */ |
| 1400 | case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */ |
| 1401 | break; |
| 1402 | default: |
Felipe Balbi | ec5e795 | 2015-11-16 16:04:13 -0600 | [diff] [blame] | 1403 | dwc3_trace(trace_dwc3_gadget, |
| 1404 | "can't wakeup from '%s'\n", |
| 1405 | dwc3_gadget_link_string(link_state)); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1406 | ret = -EINVAL; |
| 1407 | goto out; |
| 1408 | } |
| 1409 | |
Felipe Balbi | 8598bde | 2012-01-02 18:55:57 +0200 | [diff] [blame] | 1410 | ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV); |
| 1411 | if (ret < 0) { |
| 1412 | dev_err(dwc->dev, "failed to put link in Recovery\n"); |
| 1413 | goto out; |
| 1414 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1415 | |
Paul Zimmerman | 802fde9 | 2012-04-27 13:10:52 +0300 | [diff] [blame] | 1416 | /* Recent versions do this automatically */ |
| 1417 | if (dwc->revision < DWC3_REVISION_194A) { |
| 1418 | /* write zeroes to Link Change Request */ |
Felipe Balbi | fcc023c | 2012-05-24 10:27:56 +0300 | [diff] [blame] | 1419 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
Paul Zimmerman | 802fde9 | 2012-04-27 13:10:52 +0300 | [diff] [blame] | 1420 | reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK; |
| 1421 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); |
| 1422 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1423 | |
Paul Zimmerman | 1d04679 | 2012-02-15 18:56:56 -0800 | [diff] [blame] | 1424 | /* poll until Link State changes to ON */ |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1425 | timeout = jiffies + msecs_to_jiffies(100); |
| 1426 | |
Paul Zimmerman | 1d04679 | 2012-02-15 18:56:56 -0800 | [diff] [blame] | 1427 | while (!time_after(jiffies, timeout)) { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1428 | reg = dwc3_readl(dwc->regs, DWC3_DSTS); |
| 1429 | |
| 1430 | /* in HS, means ON */ |
| 1431 | if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0) |
| 1432 | break; |
| 1433 | } |
| 1434 | |
| 1435 | if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) { |
| 1436 | dev_err(dwc->dev, "failed to send remote wakeup\n"); |
| 1437 | ret = -EINVAL; |
| 1438 | } |
| 1439 | |
| 1440 | out: |
| 1441 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 1442 | |
| 1443 | return ret; |
| 1444 | } |
| 1445 | |
| 1446 | static int dwc3_gadget_set_selfpowered(struct usb_gadget *g, |
| 1447 | int is_selfpowered) |
| 1448 | { |
| 1449 | struct dwc3 *dwc = gadget_to_dwc(g); |
Paul Zimmerman | 249a456 | 2012-02-24 17:32:16 -0800 | [diff] [blame] | 1450 | unsigned long flags; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1451 | |
Paul Zimmerman | 249a456 | 2012-02-24 17:32:16 -0800 | [diff] [blame] | 1452 | spin_lock_irqsave(&dwc->lock, flags); |
Peter Chen | bcdea50 | 2015-01-28 16:32:40 +0800 | [diff] [blame] | 1453 | g->is_selfpowered = !!is_selfpowered; |
Paul Zimmerman | 249a456 | 2012-02-24 17:32:16 -0800 | [diff] [blame] | 1454 | spin_unlock_irqrestore(&dwc->lock, flags); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1455 | |
| 1456 | return 0; |
| 1457 | } |
| 1458 | |
Felipe Balbi | 7b2a036 | 2013-12-19 13:43:19 -0600 | [diff] [blame] | 1459 | static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on, int suspend) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1460 | { |
| 1461 | u32 reg; |
Sebastian Andrzej Siewior | 61d5824 | 2011-08-29 16:46:38 +0200 | [diff] [blame] | 1462 | u32 timeout = 500; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1463 | |
| 1464 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
Felipe Balbi | 8db7ed1 | 2012-01-18 18:32:29 +0200 | [diff] [blame] | 1465 | if (is_on) { |
Paul Zimmerman | 802fde9 | 2012-04-27 13:10:52 +0300 | [diff] [blame] | 1466 | if (dwc->revision <= DWC3_REVISION_187A) { |
| 1467 | reg &= ~DWC3_DCTL_TRGTULST_MASK; |
| 1468 | reg |= DWC3_DCTL_TRGTULST_RX_DET; |
| 1469 | } |
| 1470 | |
| 1471 | if (dwc->revision >= DWC3_REVISION_194A) |
| 1472 | reg &= ~DWC3_DCTL_KEEP_CONNECT; |
| 1473 | reg |= DWC3_DCTL_RUN_STOP; |
Felipe Balbi | 7b2a036 | 2013-12-19 13:43:19 -0600 | [diff] [blame] | 1474 | |
| 1475 | if (dwc->has_hibernation) |
| 1476 | reg |= DWC3_DCTL_KEEP_CONNECT; |
| 1477 | |
Felipe Balbi | 9fcb3bd | 2013-02-08 17:55:58 +0200 | [diff] [blame] | 1478 | dwc->pullups_connected = true; |
Felipe Balbi | 8db7ed1 | 2012-01-18 18:32:29 +0200 | [diff] [blame] | 1479 | } else { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1480 | reg &= ~DWC3_DCTL_RUN_STOP; |
Felipe Balbi | 7b2a036 | 2013-12-19 13:43:19 -0600 | [diff] [blame] | 1481 | |
| 1482 | if (dwc->has_hibernation && !suspend) |
| 1483 | reg &= ~DWC3_DCTL_KEEP_CONNECT; |
| 1484 | |
Felipe Balbi | 9fcb3bd | 2013-02-08 17:55:58 +0200 | [diff] [blame] | 1485 | dwc->pullups_connected = false; |
Felipe Balbi | 8db7ed1 | 2012-01-18 18:32:29 +0200 | [diff] [blame] | 1486 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1487 | |
| 1488 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); |
| 1489 | |
| 1490 | do { |
| 1491 | reg = dwc3_readl(dwc->regs, DWC3_DSTS); |
| 1492 | if (is_on) { |
| 1493 | if (!(reg & DWC3_DSTS_DEVCTRLHLT)) |
| 1494 | break; |
| 1495 | } else { |
| 1496 | if (reg & DWC3_DSTS_DEVCTRLHLT) |
| 1497 | break; |
| 1498 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1499 | timeout--; |
| 1500 | if (!timeout) |
Pratyush Anand | 6f17f74 | 2012-07-02 10:21:55 +0530 | [diff] [blame] | 1501 | return -ETIMEDOUT; |
Sebastian Andrzej Siewior | 61d5824 | 2011-08-29 16:46:38 +0200 | [diff] [blame] | 1502 | udelay(1); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1503 | } while (1); |
| 1504 | |
Felipe Balbi | 7381528 | 2015-01-27 13:48:14 -0600 | [diff] [blame] | 1505 | dwc3_trace(trace_dwc3_gadget, "gadget %s data soft-%s", |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1506 | dwc->gadget_driver |
| 1507 | ? dwc->gadget_driver->function : "no-function", |
| 1508 | is_on ? "connect" : "disconnect"); |
Pratyush Anand | 6f17f74 | 2012-07-02 10:21:55 +0530 | [diff] [blame] | 1509 | |
| 1510 | return 0; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1511 | } |
| 1512 | |
| 1513 | static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on) |
| 1514 | { |
| 1515 | struct dwc3 *dwc = gadget_to_dwc(g); |
| 1516 | unsigned long flags; |
Pratyush Anand | 6f17f74 | 2012-07-02 10:21:55 +0530 | [diff] [blame] | 1517 | int ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1518 | |
| 1519 | is_on = !!is_on; |
| 1520 | |
| 1521 | spin_lock_irqsave(&dwc->lock, flags); |
Felipe Balbi | 7b2a036 | 2013-12-19 13:43:19 -0600 | [diff] [blame] | 1522 | ret = dwc3_gadget_run_stop(dwc, is_on, false); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1523 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 1524 | |
Pratyush Anand | 6f17f74 | 2012-07-02 10:21:55 +0530 | [diff] [blame] | 1525 | return ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1526 | } |
| 1527 | |
Felipe Balbi | 8698e2a | 2013-02-08 15:24:04 +0200 | [diff] [blame] | 1528 | static void dwc3_gadget_enable_irq(struct dwc3 *dwc) |
| 1529 | { |
| 1530 | u32 reg; |
| 1531 | |
| 1532 | /* Enable all but Start and End of Frame IRQs */ |
| 1533 | reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN | |
| 1534 | DWC3_DEVTEN_EVNTOVERFLOWEN | |
| 1535 | DWC3_DEVTEN_CMDCMPLTEN | |
| 1536 | DWC3_DEVTEN_ERRTICERREN | |
| 1537 | DWC3_DEVTEN_WKUPEVTEN | |
| 1538 | DWC3_DEVTEN_ULSTCNGEN | |
| 1539 | DWC3_DEVTEN_CONNECTDONEEN | |
| 1540 | DWC3_DEVTEN_USBRSTEN | |
| 1541 | DWC3_DEVTEN_DISCONNEVTEN); |
| 1542 | |
| 1543 | dwc3_writel(dwc->regs, DWC3_DEVTEN, reg); |
| 1544 | } |
| 1545 | |
| 1546 | static void dwc3_gadget_disable_irq(struct dwc3 *dwc) |
| 1547 | { |
| 1548 | /* mask all interrupts */ |
| 1549 | dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00); |
| 1550 | } |
| 1551 | |
| 1552 | static irqreturn_t dwc3_interrupt(int irq, void *_dwc); |
Felipe Balbi | b15a762 | 2011-06-30 16:57:15 +0300 | [diff] [blame] | 1553 | static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc); |
Felipe Balbi | 8698e2a | 2013-02-08 15:24:04 +0200 | [diff] [blame] | 1554 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1555 | static int dwc3_gadget_start(struct usb_gadget *g, |
| 1556 | struct usb_gadget_driver *driver) |
| 1557 | { |
| 1558 | struct dwc3 *dwc = gadget_to_dwc(g); |
| 1559 | struct dwc3_ep *dep; |
| 1560 | unsigned long flags; |
| 1561 | int ret = 0; |
Felipe Balbi | 8698e2a | 2013-02-08 15:24:04 +0200 | [diff] [blame] | 1562 | int irq; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1563 | u32 reg; |
| 1564 | |
Felipe Balbi | b0d7ffd | 2013-06-27 10:00:18 +0300 | [diff] [blame] | 1565 | irq = platform_get_irq(to_platform_device(dwc->dev), 0); |
| 1566 | ret = request_threaded_irq(irq, dwc3_interrupt, dwc3_thread_interrupt, |
Felipe Balbi | dea520a | 2016-03-30 09:39:34 +0300 | [diff] [blame] | 1567 | IRQF_SHARED, "dwc3", dwc->ev_buf); |
Felipe Balbi | b0d7ffd | 2013-06-27 10:00:18 +0300 | [diff] [blame] | 1568 | if (ret) { |
| 1569 | dev_err(dwc->dev, "failed to request irq #%d --> %d\n", |
| 1570 | irq, ret); |
| 1571 | goto err0; |
| 1572 | } |
| 1573 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1574 | spin_lock_irqsave(&dwc->lock, flags); |
| 1575 | |
| 1576 | if (dwc->gadget_driver) { |
| 1577 | dev_err(dwc->dev, "%s is already bound to %s\n", |
| 1578 | dwc->gadget.name, |
| 1579 | dwc->gadget_driver->driver.name); |
| 1580 | ret = -EBUSY; |
Felipe Balbi | b0d7ffd | 2013-06-27 10:00:18 +0300 | [diff] [blame] | 1581 | goto err1; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1582 | } |
| 1583 | |
| 1584 | dwc->gadget_driver = driver; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1585 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1586 | reg = dwc3_readl(dwc->regs, DWC3_DCFG); |
| 1587 | reg &= ~(DWC3_DCFG_SPEED_MASK); |
Felipe Balbi | 07e7f47 | 2012-03-23 12:20:31 +0200 | [diff] [blame] | 1588 | |
| 1589 | /** |
| 1590 | * WORKAROUND: DWC3 revision < 2.20a have an issue |
| 1591 | * which would cause metastability state on Run/Stop |
| 1592 | * bit if we try to force the IP to USB2-only mode. |
| 1593 | * |
| 1594 | * Because of that, we cannot configure the IP to any |
| 1595 | * speed other than the SuperSpeed |
| 1596 | * |
| 1597 | * Refers to: |
| 1598 | * |
| 1599 | * STAR#9000525659: Clock Domain Crossing on DCTL in |
| 1600 | * USB 2.0 Mode |
| 1601 | */ |
Felipe Balbi | f7e846f | 2013-06-30 14:29:51 +0300 | [diff] [blame] | 1602 | if (dwc->revision < DWC3_REVISION_220A) { |
Felipe Balbi | 07e7f47 | 2012-03-23 12:20:31 +0200 | [diff] [blame] | 1603 | reg |= DWC3_DCFG_SUPERSPEED; |
Felipe Balbi | f7e846f | 2013-06-30 14:29:51 +0300 | [diff] [blame] | 1604 | } else { |
| 1605 | switch (dwc->maximum_speed) { |
| 1606 | case USB_SPEED_LOW: |
| 1607 | reg |= DWC3_DSTS_LOWSPEED; |
| 1608 | break; |
| 1609 | case USB_SPEED_FULL: |
| 1610 | reg |= DWC3_DSTS_FULLSPEED1; |
| 1611 | break; |
| 1612 | case USB_SPEED_HIGH: |
| 1613 | reg |= DWC3_DSTS_HIGHSPEED; |
| 1614 | break; |
John Youn | 7580862 | 2016-02-05 17:09:13 -0800 | [diff] [blame] | 1615 | case USB_SPEED_SUPER_PLUS: |
| 1616 | reg |= DWC3_DSTS_SUPERSPEED_PLUS; |
| 1617 | break; |
Felipe Balbi | f7e846f | 2013-06-30 14:29:51 +0300 | [diff] [blame] | 1618 | default: |
John Youn | 77966eb | 2016-02-19 17:31:01 -0800 | [diff] [blame] | 1619 | dev_err(dwc->dev, "invalid dwc->maximum_speed (%d)\n", |
| 1620 | dwc->maximum_speed); |
| 1621 | /* fall through */ |
| 1622 | case USB_SPEED_SUPER: |
| 1623 | reg |= DWC3_DCFG_SUPERSPEED; |
| 1624 | break; |
Felipe Balbi | f7e846f | 2013-06-30 14:29:51 +0300 | [diff] [blame] | 1625 | } |
| 1626 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1627 | dwc3_writel(dwc->regs, DWC3_DCFG, reg); |
| 1628 | |
| 1629 | /* Start with SuperSpeed Default */ |
| 1630 | dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512); |
| 1631 | |
| 1632 | dep = dwc->eps[0]; |
Paul Zimmerman | 265b70a | 2013-12-19 12:38:49 -0600 | [diff] [blame] | 1633 | ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false, |
| 1634 | false); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1635 | if (ret) { |
| 1636 | dev_err(dwc->dev, "failed to enable %s\n", dep->name); |
Felipe Balbi | b0d7ffd | 2013-06-27 10:00:18 +0300 | [diff] [blame] | 1637 | goto err2; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1638 | } |
| 1639 | |
| 1640 | dep = dwc->eps[1]; |
Paul Zimmerman | 265b70a | 2013-12-19 12:38:49 -0600 | [diff] [blame] | 1641 | ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false, |
| 1642 | false); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1643 | if (ret) { |
| 1644 | dev_err(dwc->dev, "failed to enable %s\n", dep->name); |
Felipe Balbi | b0d7ffd | 2013-06-27 10:00:18 +0300 | [diff] [blame] | 1645 | goto err3; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1646 | } |
| 1647 | |
| 1648 | /* begin to receive SETUP packets */ |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 1649 | dwc->ep0state = EP0_SETUP_PHASE; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1650 | dwc3_ep0_out_start(dwc); |
| 1651 | |
Felipe Balbi | 8698e2a | 2013-02-08 15:24:04 +0200 | [diff] [blame] | 1652 | dwc3_gadget_enable_irq(dwc); |
| 1653 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1654 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 1655 | |
| 1656 | return 0; |
| 1657 | |
Felipe Balbi | b0d7ffd | 2013-06-27 10:00:18 +0300 | [diff] [blame] | 1658 | err3: |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1659 | __dwc3_gadget_ep_disable(dwc->eps[0]); |
| 1660 | |
Felipe Balbi | b0d7ffd | 2013-06-27 10:00:18 +0300 | [diff] [blame] | 1661 | err2: |
Felipe Balbi | cdcedd6 | 2013-07-15 12:36:35 +0300 | [diff] [blame] | 1662 | dwc->gadget_driver = NULL; |
Felipe Balbi | b0d7ffd | 2013-06-27 10:00:18 +0300 | [diff] [blame] | 1663 | |
| 1664 | err1: |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1665 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 1666 | |
Felipe Balbi | dea520a | 2016-03-30 09:39:34 +0300 | [diff] [blame] | 1667 | free_irq(irq, dwc->ev_buf); |
Felipe Balbi | b0d7ffd | 2013-06-27 10:00:18 +0300 | [diff] [blame] | 1668 | |
| 1669 | err0: |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1670 | return ret; |
| 1671 | } |
| 1672 | |
Felipe Balbi | 22835b8 | 2014-10-17 12:05:12 -0500 | [diff] [blame] | 1673 | static int dwc3_gadget_stop(struct usb_gadget *g) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1674 | { |
| 1675 | struct dwc3 *dwc = gadget_to_dwc(g); |
| 1676 | unsigned long flags; |
Felipe Balbi | 8698e2a | 2013-02-08 15:24:04 +0200 | [diff] [blame] | 1677 | int irq; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1678 | |
| 1679 | spin_lock_irqsave(&dwc->lock, flags); |
| 1680 | |
Felipe Balbi | 8698e2a | 2013-02-08 15:24:04 +0200 | [diff] [blame] | 1681 | dwc3_gadget_disable_irq(dwc); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1682 | __dwc3_gadget_ep_disable(dwc->eps[0]); |
| 1683 | __dwc3_gadget_ep_disable(dwc->eps[1]); |
| 1684 | |
| 1685 | dwc->gadget_driver = NULL; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1686 | |
| 1687 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 1688 | |
Felipe Balbi | b0d7ffd | 2013-06-27 10:00:18 +0300 | [diff] [blame] | 1689 | irq = platform_get_irq(to_platform_device(dwc->dev), 0); |
Felipe Balbi | dea520a | 2016-03-30 09:39:34 +0300 | [diff] [blame] | 1690 | free_irq(irq, dwc->ev_buf); |
Felipe Balbi | b0d7ffd | 2013-06-27 10:00:18 +0300 | [diff] [blame] | 1691 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1692 | return 0; |
| 1693 | } |
Paul Zimmerman | 802fde9 | 2012-04-27 13:10:52 +0300 | [diff] [blame] | 1694 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1695 | static const struct usb_gadget_ops dwc3_gadget_ops = { |
| 1696 | .get_frame = dwc3_gadget_get_frame, |
| 1697 | .wakeup = dwc3_gadget_wakeup, |
| 1698 | .set_selfpowered = dwc3_gadget_set_selfpowered, |
| 1699 | .pullup = dwc3_gadget_pullup, |
| 1700 | .udc_start = dwc3_gadget_start, |
| 1701 | .udc_stop = dwc3_gadget_stop, |
| 1702 | }; |
| 1703 | |
| 1704 | /* -------------------------------------------------------------------------- */ |
| 1705 | |
Felipe Balbi | 6a1e3ef | 2011-05-05 16:21:59 +0300 | [diff] [blame] | 1706 | static int dwc3_gadget_init_hw_endpoints(struct dwc3 *dwc, |
| 1707 | u8 num, u32 direction) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1708 | { |
| 1709 | struct dwc3_ep *dep; |
Felipe Balbi | 6a1e3ef | 2011-05-05 16:21:59 +0300 | [diff] [blame] | 1710 | u8 i; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1711 | |
Felipe Balbi | 6a1e3ef | 2011-05-05 16:21:59 +0300 | [diff] [blame] | 1712 | for (i = 0; i < num; i++) { |
| 1713 | u8 epnum = (i << 1) | (!!direction); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1714 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1715 | dep = kzalloc(sizeof(*dep), GFP_KERNEL); |
Jingoo Han | 734d5a5 | 2014-07-17 12:45:11 +0900 | [diff] [blame] | 1716 | if (!dep) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1717 | return -ENOMEM; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1718 | |
| 1719 | dep->dwc = dwc; |
| 1720 | dep->number = epnum; |
Felipe Balbi | 9aa62ae | 2013-07-12 19:10:59 +0300 | [diff] [blame] | 1721 | dep->direction = !!direction; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1722 | dwc->eps[epnum] = dep; |
| 1723 | |
| 1724 | snprintf(dep->name, sizeof(dep->name), "ep%d%s", epnum >> 1, |
| 1725 | (epnum & 1) ? "in" : "out"); |
Felipe Balbi | 6a1e3ef | 2011-05-05 16:21:59 +0300 | [diff] [blame] | 1726 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1727 | dep->endpoint.name = dep->name; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1728 | |
Felipe Balbi | 7381528 | 2015-01-27 13:48:14 -0600 | [diff] [blame] | 1729 | dwc3_trace(trace_dwc3_gadget, "initializing %s", dep->name); |
Felipe Balbi | 653df35 | 2013-07-12 19:11:57 +0300 | [diff] [blame] | 1730 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1731 | if (epnum == 0 || epnum == 1) { |
Robert Baldyga | e117e74 | 2013-12-13 12:23:38 +0100 | [diff] [blame] | 1732 | usb_ep_set_maxpacket_limit(&dep->endpoint, 512); |
Pratyush Anand | 6048e4c | 2013-01-18 16:53:56 +0530 | [diff] [blame] | 1733 | dep->endpoint.maxburst = 1; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1734 | dep->endpoint.ops = &dwc3_gadget_ep0_ops; |
| 1735 | if (!epnum) |
| 1736 | dwc->gadget.ep0 = &dep->endpoint; |
| 1737 | } else { |
| 1738 | int ret; |
| 1739 | |
Robert Baldyga | e117e74 | 2013-12-13 12:23:38 +0100 | [diff] [blame] | 1740 | usb_ep_set_maxpacket_limit(&dep->endpoint, 1024); |
Sebastian Andrzej Siewior | 12d36c1 | 2011-11-03 20:27:50 +0100 | [diff] [blame] | 1741 | dep->endpoint.max_streams = 15; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1742 | dep->endpoint.ops = &dwc3_gadget_ep_ops; |
| 1743 | list_add_tail(&dep->endpoint.ep_list, |
| 1744 | &dwc->gadget.ep_list); |
| 1745 | |
| 1746 | ret = dwc3_alloc_trb_pool(dep); |
Felipe Balbi | 25b8ff6 | 2011-11-04 12:32:47 +0200 | [diff] [blame] | 1747 | if (ret) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1748 | return ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1749 | } |
Felipe Balbi | 25b8ff6 | 2011-11-04 12:32:47 +0200 | [diff] [blame] | 1750 | |
Robert Baldyga | a474d3b | 2015-07-31 16:00:19 +0200 | [diff] [blame] | 1751 | if (epnum == 0 || epnum == 1) { |
| 1752 | dep->endpoint.caps.type_control = true; |
| 1753 | } else { |
| 1754 | dep->endpoint.caps.type_iso = true; |
| 1755 | dep->endpoint.caps.type_bulk = true; |
| 1756 | dep->endpoint.caps.type_int = true; |
| 1757 | } |
| 1758 | |
| 1759 | dep->endpoint.caps.dir_in = !!direction; |
| 1760 | dep->endpoint.caps.dir_out = !direction; |
| 1761 | |
Felipe Balbi | aa3342c | 2016-03-14 11:01:31 +0200 | [diff] [blame] | 1762 | INIT_LIST_HEAD(&dep->pending_list); |
| 1763 | INIT_LIST_HEAD(&dep->started_list); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1764 | } |
| 1765 | |
| 1766 | return 0; |
| 1767 | } |
| 1768 | |
Felipe Balbi | 6a1e3ef | 2011-05-05 16:21:59 +0300 | [diff] [blame] | 1769 | static int dwc3_gadget_init_endpoints(struct dwc3 *dwc) |
| 1770 | { |
| 1771 | int ret; |
| 1772 | |
| 1773 | INIT_LIST_HEAD(&dwc->gadget.ep_list); |
| 1774 | |
| 1775 | ret = dwc3_gadget_init_hw_endpoints(dwc, dwc->num_out_eps, 0); |
| 1776 | if (ret < 0) { |
Felipe Balbi | 7381528 | 2015-01-27 13:48:14 -0600 | [diff] [blame] | 1777 | dwc3_trace(trace_dwc3_gadget, |
| 1778 | "failed to allocate OUT endpoints"); |
Felipe Balbi | 6a1e3ef | 2011-05-05 16:21:59 +0300 | [diff] [blame] | 1779 | return ret; |
| 1780 | } |
| 1781 | |
| 1782 | ret = dwc3_gadget_init_hw_endpoints(dwc, dwc->num_in_eps, 1); |
| 1783 | if (ret < 0) { |
Felipe Balbi | 7381528 | 2015-01-27 13:48:14 -0600 | [diff] [blame] | 1784 | dwc3_trace(trace_dwc3_gadget, |
| 1785 | "failed to allocate IN endpoints"); |
Felipe Balbi | 6a1e3ef | 2011-05-05 16:21:59 +0300 | [diff] [blame] | 1786 | return ret; |
| 1787 | } |
| 1788 | |
| 1789 | return 0; |
| 1790 | } |
| 1791 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1792 | static void dwc3_gadget_free_endpoints(struct dwc3 *dwc) |
| 1793 | { |
| 1794 | struct dwc3_ep *dep; |
| 1795 | u8 epnum; |
| 1796 | |
| 1797 | for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) { |
| 1798 | dep = dwc->eps[epnum]; |
Felipe Balbi | 6a1e3ef | 2011-05-05 16:21:59 +0300 | [diff] [blame] | 1799 | if (!dep) |
| 1800 | continue; |
George Cherian | 5bf8fae | 2013-05-27 14:35:49 +0530 | [diff] [blame] | 1801 | /* |
| 1802 | * Physical endpoints 0 and 1 are special; they form the |
| 1803 | * bi-directional USB endpoint 0. |
| 1804 | * |
| 1805 | * For those two physical endpoints, we don't allocate a TRB |
| 1806 | * pool nor do we add them the endpoints list. Due to that, we |
| 1807 | * shouldn't do these two operations otherwise we would end up |
| 1808 | * with all sorts of bugs when removing dwc3.ko. |
| 1809 | */ |
| 1810 | if (epnum != 0 && epnum != 1) { |
| 1811 | dwc3_free_trb_pool(dep); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1812 | list_del(&dep->endpoint.ep_list); |
George Cherian | 5bf8fae | 2013-05-27 14:35:49 +0530 | [diff] [blame] | 1813 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1814 | |
| 1815 | kfree(dep); |
| 1816 | } |
| 1817 | } |
| 1818 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1819 | /* -------------------------------------------------------------------------- */ |
Felipe Balbi | e5caff6 | 2013-02-26 15:11:05 +0200 | [diff] [blame] | 1820 | |
Pratyush Anand | e5ba5ec | 2013-01-14 15:59:37 +0530 | [diff] [blame] | 1821 | static int __dwc3_cleanup_done_trbs(struct dwc3 *dwc, struct dwc3_ep *dep, |
| 1822 | struct dwc3_request *req, struct dwc3_trb *trb, |
| 1823 | const struct dwc3_event_depevt *event, int status) |
| 1824 | { |
| 1825 | unsigned int count; |
| 1826 | unsigned int s_pkt = 0; |
| 1827 | unsigned int trb_status; |
| 1828 | |
Felipe Balbi | 2c4cbe6e5 | 2014-04-30 17:45:10 -0500 | [diff] [blame] | 1829 | trace_dwc3_complete_trb(dep, trb); |
| 1830 | |
Pratyush Anand | e5ba5ec | 2013-01-14 15:59:37 +0530 | [diff] [blame] | 1831 | if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN) |
| 1832 | /* |
| 1833 | * We continue despite the error. There is not much we |
| 1834 | * can do. If we don't clean it up we loop forever. If |
| 1835 | * we skip the TRB then it gets overwritten after a |
| 1836 | * while since we use them in a ring buffer. A BUG() |
| 1837 | * would help. Lets hope that if this occurs, someone |
| 1838 | * fixes the root cause instead of looking away :) |
| 1839 | */ |
| 1840 | dev_err(dwc->dev, "%s's TRB (%p) still owned by HW\n", |
| 1841 | dep->name, trb); |
| 1842 | count = trb->size & DWC3_TRB_SIZE_MASK; |
| 1843 | |
| 1844 | if (dep->direction) { |
| 1845 | if (count) { |
| 1846 | trb_status = DWC3_TRB_SIZE_TRBSTS(trb->size); |
| 1847 | if (trb_status == DWC3_TRBSTS_MISSED_ISOC) { |
Felipe Balbi | ec5e795 | 2015-11-16 16:04:13 -0600 | [diff] [blame] | 1848 | dwc3_trace(trace_dwc3_gadget, |
| 1849 | "%s: incomplete IN transfer\n", |
Pratyush Anand | e5ba5ec | 2013-01-14 15:59:37 +0530 | [diff] [blame] | 1850 | dep->name); |
| 1851 | /* |
| 1852 | * If missed isoc occurred and there is |
| 1853 | * no request queued then issue END |
| 1854 | * TRANSFER, so that core generates |
| 1855 | * next xfernotready and we will issue |
| 1856 | * a fresh START TRANSFER. |
| 1857 | * If there are still queued request |
| 1858 | * then wait, do not issue either END |
| 1859 | * or UPDATE TRANSFER, just attach next |
Felipe Balbi | aa3342c | 2016-03-14 11:01:31 +0200 | [diff] [blame] | 1860 | * request in pending_list during |
Pratyush Anand | e5ba5ec | 2013-01-14 15:59:37 +0530 | [diff] [blame] | 1861 | * giveback.If any future queued request |
| 1862 | * is successfully transferred then we |
| 1863 | * will issue UPDATE TRANSFER for all |
Felipe Balbi | aa3342c | 2016-03-14 11:01:31 +0200 | [diff] [blame] | 1864 | * request in the pending_list. |
Pratyush Anand | e5ba5ec | 2013-01-14 15:59:37 +0530 | [diff] [blame] | 1865 | */ |
| 1866 | dep->flags |= DWC3_EP_MISSED_ISOC; |
| 1867 | } else { |
| 1868 | dev_err(dwc->dev, "incomplete IN transfer %s\n", |
| 1869 | dep->name); |
| 1870 | status = -ECONNRESET; |
| 1871 | } |
| 1872 | } else { |
| 1873 | dep->flags &= ~DWC3_EP_MISSED_ISOC; |
| 1874 | } |
| 1875 | } else { |
| 1876 | if (count && (event->status & DEPEVT_STATUS_SHORT)) |
| 1877 | s_pkt = 1; |
| 1878 | } |
| 1879 | |
| 1880 | /* |
| 1881 | * We assume here we will always receive the entire data block |
| 1882 | * which we should receive. Meaning, if we program RX to |
| 1883 | * receive 4K but we receive only 2K, we assume that's all we |
| 1884 | * should receive and we simply bounce the request back to the |
| 1885 | * gadget driver for further processing. |
| 1886 | */ |
| 1887 | req->request.actual += req->request.length - count; |
| 1888 | if (s_pkt) |
| 1889 | return 1; |
| 1890 | if ((event->status & DEPEVT_STATUS_LST) && |
| 1891 | (trb->ctrl & (DWC3_TRB_CTRL_LST | |
| 1892 | DWC3_TRB_CTRL_HWO))) |
| 1893 | return 1; |
| 1894 | if ((event->status & DEPEVT_STATUS_IOC) && |
| 1895 | (trb->ctrl & DWC3_TRB_CTRL_IOC)) |
| 1896 | return 1; |
| 1897 | return 0; |
| 1898 | } |
| 1899 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1900 | static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep, |
| 1901 | const struct dwc3_event_depevt *event, int status) |
| 1902 | { |
| 1903 | struct dwc3_request *req; |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 1904 | struct dwc3_trb *trb; |
Pratyush Anand | e5ba5ec | 2013-01-14 15:59:37 +0530 | [diff] [blame] | 1905 | unsigned int slot; |
| 1906 | unsigned int i; |
| 1907 | int ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1908 | |
| 1909 | do { |
Felipe Balbi | aa3342c | 2016-03-14 11:01:31 +0200 | [diff] [blame] | 1910 | req = next_request(&dep->started_list); |
Felipe Balbi | ac7bdcc | 2015-11-16 16:13:57 -0600 | [diff] [blame] | 1911 | if (WARN_ON_ONCE(!req)) |
Ville Syrjälä | d115d70 | 2015-08-31 19:48:28 +0300 | [diff] [blame] | 1912 | return 1; |
Felipe Balbi | ac7bdcc | 2015-11-16 16:13:57 -0600 | [diff] [blame] | 1913 | |
Ville Syrjälä | d115d70 | 2015-08-31 19:48:28 +0300 | [diff] [blame] | 1914 | i = 0; |
| 1915 | do { |
| 1916 | slot = req->start_slot + i; |
| 1917 | if ((slot == DWC3_TRB_NUM - 1) && |
Pratyush Anand | e5ba5ec | 2013-01-14 15:59:37 +0530 | [diff] [blame] | 1918 | usb_endpoint_xfer_isoc(dep->endpoint.desc)) |
Ville Syrjälä | d115d70 | 2015-08-31 19:48:28 +0300 | [diff] [blame] | 1919 | slot++; |
| 1920 | slot %= DWC3_TRB_NUM; |
| 1921 | trb = &dep->trb_pool[slot]; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1922 | |
Ville Syrjälä | d115d70 | 2015-08-31 19:48:28 +0300 | [diff] [blame] | 1923 | ret = __dwc3_cleanup_done_trbs(dwc, dep, req, trb, |
| 1924 | event, status); |
| 1925 | if (ret) |
| 1926 | break; |
| 1927 | } while (++i < req->request.num_mapped_sgs); |
| 1928 | |
| 1929 | dwc3_gadget_giveback(dep, req, status); |
| 1930 | |
Pratyush Anand | e5ba5ec | 2013-01-14 15:59:37 +0530 | [diff] [blame] | 1931 | if (ret) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1932 | break; |
Ville Syrjälä | d115d70 | 2015-08-31 19:48:28 +0300 | [diff] [blame] | 1933 | } while (1); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1934 | |
Pratyush Anand | cdc359d | 2013-01-14 15:59:34 +0530 | [diff] [blame] | 1935 | if (usb_endpoint_xfer_isoc(dep->endpoint.desc) && |
Felipe Balbi | aa3342c | 2016-03-14 11:01:31 +0200 | [diff] [blame] | 1936 | list_empty(&dep->started_list)) { |
| 1937 | if (list_empty(&dep->pending_list)) { |
Pratyush Anand | cdc359d | 2013-01-14 15:59:34 +0530 | [diff] [blame] | 1938 | /* |
| 1939 | * If there is no entry in request list then do |
| 1940 | * not issue END TRANSFER now. Just set PENDING |
| 1941 | * flag, so that END TRANSFER is issued when an |
| 1942 | * entry is added into request list. |
| 1943 | */ |
| 1944 | dep->flags = DWC3_EP_PENDING_REQUEST; |
| 1945 | } else { |
Paul Zimmerman | b992e68 | 2012-04-27 14:17:35 +0300 | [diff] [blame] | 1946 | dwc3_stop_active_transfer(dwc, dep->number, true); |
Pratyush Anand | cdc359d | 2013-01-14 15:59:34 +0530 | [diff] [blame] | 1947 | dep->flags = DWC3_EP_ENABLED; |
| 1948 | } |
Pratyush Anand | 7efea86 | 2013-01-14 15:59:32 +0530 | [diff] [blame] | 1949 | return 1; |
| 1950 | } |
| 1951 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1952 | return 1; |
| 1953 | } |
| 1954 | |
| 1955 | static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc, |
Jingoo Han | 029d97f | 2014-07-04 15:00:51 +0900 | [diff] [blame] | 1956 | struct dwc3_ep *dep, const struct dwc3_event_depevt *event) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1957 | { |
| 1958 | unsigned status = 0; |
| 1959 | int clean_busy; |
Felipe Balbi | e18b797 | 2015-05-29 10:06:38 -0500 | [diff] [blame] | 1960 | u32 is_xfer_complete; |
| 1961 | |
| 1962 | is_xfer_complete = (event->endpoint_event == DWC3_DEPEVT_XFERCOMPLETE); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1963 | |
| 1964 | if (event->status & DEPEVT_STATUS_BUSERR) |
| 1965 | status = -ECONNRESET; |
| 1966 | |
Paul Zimmerman | 1d04679 | 2012-02-15 18:56:56 -0800 | [diff] [blame] | 1967 | clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status); |
Felipe Balbi | e18b797 | 2015-05-29 10:06:38 -0500 | [diff] [blame] | 1968 | if (clean_busy && (is_xfer_complete || |
| 1969 | usb_endpoint_xfer_isoc(dep->endpoint.desc))) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1970 | dep->flags &= ~DWC3_EP_BUSY; |
Felipe Balbi | fae2b90 | 2011-10-14 13:00:30 +0300 | [diff] [blame] | 1971 | |
| 1972 | /* |
| 1973 | * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround. |
| 1974 | * See dwc3_gadget_linksts_change_interrupt() for 1st half. |
| 1975 | */ |
| 1976 | if (dwc->revision < DWC3_REVISION_183A) { |
| 1977 | u32 reg; |
| 1978 | int i; |
| 1979 | |
| 1980 | for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) { |
Moiz Sonasath | 348e026 | 2012-08-01 14:08:30 -0500 | [diff] [blame] | 1981 | dep = dwc->eps[i]; |
Felipe Balbi | fae2b90 | 2011-10-14 13:00:30 +0300 | [diff] [blame] | 1982 | |
| 1983 | if (!(dep->flags & DWC3_EP_ENABLED)) |
| 1984 | continue; |
| 1985 | |
Felipe Balbi | aa3342c | 2016-03-14 11:01:31 +0200 | [diff] [blame] | 1986 | if (!list_empty(&dep->started_list)) |
Felipe Balbi | fae2b90 | 2011-10-14 13:00:30 +0300 | [diff] [blame] | 1987 | return; |
| 1988 | } |
| 1989 | |
| 1990 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
| 1991 | reg |= dwc->u1u2; |
| 1992 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); |
| 1993 | |
| 1994 | dwc->u1u2 = 0; |
| 1995 | } |
Felipe Balbi | 8a1a9c9 | 2015-09-21 14:32:00 -0500 | [diff] [blame] | 1996 | |
Felipe Balbi | e6e709b | 2015-09-28 15:16:56 -0500 | [diff] [blame] | 1997 | if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) { |
Felipe Balbi | 8a1a9c9 | 2015-09-21 14:32:00 -0500 | [diff] [blame] | 1998 | int ret; |
| 1999 | |
Felipe Balbi | e6e709b | 2015-09-28 15:16:56 -0500 | [diff] [blame] | 2000 | ret = __dwc3_gadget_kick_transfer(dep, 0, is_xfer_complete); |
Felipe Balbi | 8a1a9c9 | 2015-09-21 14:32:00 -0500 | [diff] [blame] | 2001 | if (!ret || ret == -EBUSY) |
| 2002 | return; |
| 2003 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2004 | } |
| 2005 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2006 | static void dwc3_endpoint_interrupt(struct dwc3 *dwc, |
| 2007 | const struct dwc3_event_depevt *event) |
| 2008 | { |
| 2009 | struct dwc3_ep *dep; |
| 2010 | u8 epnum = event->endpoint_number; |
| 2011 | |
| 2012 | dep = dwc->eps[epnum]; |
| 2013 | |
Felipe Balbi | 3336abb | 2012-06-06 09:19:35 +0300 | [diff] [blame] | 2014 | if (!(dep->flags & DWC3_EP_ENABLED)) |
| 2015 | return; |
| 2016 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2017 | if (epnum == 0 || epnum == 1) { |
| 2018 | dwc3_ep0_interrupt(dwc, event); |
| 2019 | return; |
| 2020 | } |
| 2021 | |
| 2022 | switch (event->endpoint_event) { |
| 2023 | case DWC3_DEPEVT_XFERCOMPLETE: |
Felipe Balbi | b4996a8 | 2012-06-06 12:04:13 +0300 | [diff] [blame] | 2024 | dep->resource_index = 0; |
Paul Zimmerman | c2df85c | 2012-02-24 17:32:18 -0800 | [diff] [blame] | 2025 | |
Ido Shayevitz | 16e78db | 2012-03-12 20:25:24 +0200 | [diff] [blame] | 2026 | if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) { |
Felipe Balbi | ec5e795 | 2015-11-16 16:04:13 -0600 | [diff] [blame] | 2027 | dwc3_trace(trace_dwc3_gadget, |
| 2028 | "%s is an Isochronous endpoint\n", |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2029 | dep->name); |
| 2030 | return; |
| 2031 | } |
| 2032 | |
Jingoo Han | 029d97f | 2014-07-04 15:00:51 +0900 | [diff] [blame] | 2033 | dwc3_endpoint_transfer_complete(dwc, dep, event); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2034 | break; |
| 2035 | case DWC3_DEPEVT_XFERINPROGRESS: |
Jingoo Han | 029d97f | 2014-07-04 15:00:51 +0900 | [diff] [blame] | 2036 | dwc3_endpoint_transfer_complete(dwc, dep, event); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2037 | break; |
| 2038 | case DWC3_DEPEVT_XFERNOTREADY: |
Ido Shayevitz | 16e78db | 2012-03-12 20:25:24 +0200 | [diff] [blame] | 2039 | if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2040 | dwc3_gadget_start_isoc(dwc, dep, event); |
| 2041 | } else { |
Felipe Balbi | 6bb4fe1 | 2015-09-28 14:49:02 -0500 | [diff] [blame] | 2042 | int active; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2043 | int ret; |
| 2044 | |
Felipe Balbi | 6bb4fe1 | 2015-09-28 14:49:02 -0500 | [diff] [blame] | 2045 | active = event->status & DEPEVT_STATUS_TRANSFER_ACTIVE; |
| 2046 | |
Felipe Balbi | 7381528 | 2015-01-27 13:48:14 -0600 | [diff] [blame] | 2047 | dwc3_trace(trace_dwc3_gadget, "%s: reason %s", |
Felipe Balbi | 6bb4fe1 | 2015-09-28 14:49:02 -0500 | [diff] [blame] | 2048 | dep->name, active ? "Transfer Active" |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2049 | : "Transfer Not Active"); |
| 2050 | |
Felipe Balbi | 6bb4fe1 | 2015-09-28 14:49:02 -0500 | [diff] [blame] | 2051 | ret = __dwc3_gadget_kick_transfer(dep, 0, !active); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2052 | if (!ret || ret == -EBUSY) |
| 2053 | return; |
| 2054 | |
Felipe Balbi | ec5e795 | 2015-11-16 16:04:13 -0600 | [diff] [blame] | 2055 | dwc3_trace(trace_dwc3_gadget, |
| 2056 | "%s: failed to kick transfers\n", |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2057 | dep->name); |
| 2058 | } |
| 2059 | |
| 2060 | break; |
Felipe Balbi | 879631a | 2011-09-30 10:58:47 +0300 | [diff] [blame] | 2061 | case DWC3_DEPEVT_STREAMEVT: |
Ido Shayevitz | 16e78db | 2012-03-12 20:25:24 +0200 | [diff] [blame] | 2062 | if (!usb_endpoint_xfer_bulk(dep->endpoint.desc)) { |
Felipe Balbi | 879631a | 2011-09-30 10:58:47 +0300 | [diff] [blame] | 2063 | dev_err(dwc->dev, "Stream event for non-Bulk %s\n", |
| 2064 | dep->name); |
| 2065 | return; |
| 2066 | } |
| 2067 | |
| 2068 | switch (event->status) { |
| 2069 | case DEPEVT_STREAMEVT_FOUND: |
Felipe Balbi | 7381528 | 2015-01-27 13:48:14 -0600 | [diff] [blame] | 2070 | dwc3_trace(trace_dwc3_gadget, |
| 2071 | "Stream %d found and started", |
Felipe Balbi | 879631a | 2011-09-30 10:58:47 +0300 | [diff] [blame] | 2072 | event->parameters); |
| 2073 | |
| 2074 | break; |
| 2075 | case DEPEVT_STREAMEVT_NOTFOUND: |
| 2076 | /* FALLTHROUGH */ |
| 2077 | default: |
Felipe Balbi | ec5e795 | 2015-11-16 16:04:13 -0600 | [diff] [blame] | 2078 | dwc3_trace(trace_dwc3_gadget, |
| 2079 | "unable to find suitable stream\n"); |
Felipe Balbi | 879631a | 2011-09-30 10:58:47 +0300 | [diff] [blame] | 2080 | } |
| 2081 | break; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2082 | case DWC3_DEPEVT_RXTXFIFOEVT: |
Felipe Balbi | ec5e795 | 2015-11-16 16:04:13 -0600 | [diff] [blame] | 2083 | dwc3_trace(trace_dwc3_gadget, "%s FIFO Overrun\n", dep->name); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2084 | break; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2085 | case DWC3_DEPEVT_EPCMDCMPLT: |
Felipe Balbi | 7381528 | 2015-01-27 13:48:14 -0600 | [diff] [blame] | 2086 | dwc3_trace(trace_dwc3_gadget, "Endpoint Command Complete"); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2087 | break; |
| 2088 | } |
| 2089 | } |
| 2090 | |
| 2091 | static void dwc3_disconnect_gadget(struct dwc3 *dwc) |
| 2092 | { |
| 2093 | if (dwc->gadget_driver && dwc->gadget_driver->disconnect) { |
| 2094 | spin_unlock(&dwc->lock); |
| 2095 | dwc->gadget_driver->disconnect(&dwc->gadget); |
| 2096 | spin_lock(&dwc->lock); |
| 2097 | } |
| 2098 | } |
| 2099 | |
Felipe Balbi | bc5ba2e | 2014-02-26 10:17:07 -0600 | [diff] [blame] | 2100 | static void dwc3_suspend_gadget(struct dwc3 *dwc) |
| 2101 | { |
Dan Carpenter | 73a30bf | 2014-03-07 14:19:57 +0300 | [diff] [blame] | 2102 | if (dwc->gadget_driver && dwc->gadget_driver->suspend) { |
Felipe Balbi | bc5ba2e | 2014-02-26 10:17:07 -0600 | [diff] [blame] | 2103 | spin_unlock(&dwc->lock); |
| 2104 | dwc->gadget_driver->suspend(&dwc->gadget); |
| 2105 | spin_lock(&dwc->lock); |
| 2106 | } |
| 2107 | } |
| 2108 | |
| 2109 | static void dwc3_resume_gadget(struct dwc3 *dwc) |
| 2110 | { |
Dan Carpenter | 73a30bf | 2014-03-07 14:19:57 +0300 | [diff] [blame] | 2111 | if (dwc->gadget_driver && dwc->gadget_driver->resume) { |
Felipe Balbi | bc5ba2e | 2014-02-26 10:17:07 -0600 | [diff] [blame] | 2112 | spin_unlock(&dwc->lock); |
| 2113 | dwc->gadget_driver->resume(&dwc->gadget); |
Felipe Balbi | 5c7b3b0 | 2015-01-29 10:29:18 -0600 | [diff] [blame] | 2114 | spin_lock(&dwc->lock); |
Felipe Balbi | 8e74475 | 2014-11-06 14:27:53 +0800 | [diff] [blame] | 2115 | } |
| 2116 | } |
| 2117 | |
| 2118 | static void dwc3_reset_gadget(struct dwc3 *dwc) |
| 2119 | { |
| 2120 | if (!dwc->gadget_driver) |
| 2121 | return; |
| 2122 | |
| 2123 | if (dwc->gadget.speed != USB_SPEED_UNKNOWN) { |
| 2124 | spin_unlock(&dwc->lock); |
| 2125 | usb_gadget_udc_reset(&dwc->gadget, dwc->gadget_driver); |
Felipe Balbi | bc5ba2e | 2014-02-26 10:17:07 -0600 | [diff] [blame] | 2126 | spin_lock(&dwc->lock); |
| 2127 | } |
| 2128 | } |
| 2129 | |
Paul Zimmerman | b992e68 | 2012-04-27 14:17:35 +0300 | [diff] [blame] | 2130 | static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2131 | { |
| 2132 | struct dwc3_ep *dep; |
| 2133 | struct dwc3_gadget_ep_cmd_params params; |
| 2134 | u32 cmd; |
| 2135 | int ret; |
| 2136 | |
| 2137 | dep = dwc->eps[epnum]; |
| 2138 | |
Felipe Balbi | b4996a8 | 2012-06-06 12:04:13 +0300 | [diff] [blame] | 2139 | if (!dep->resource_index) |
Pratyush Anand | 3daf74d | 2012-06-23 02:23:08 +0530 | [diff] [blame] | 2140 | return; |
| 2141 | |
Pratyush Anand | 5791150 | 2012-07-06 15:19:10 +0530 | [diff] [blame] | 2142 | /* |
| 2143 | * NOTICE: We are violating what the Databook says about the |
| 2144 | * EndTransfer command. Ideally we would _always_ wait for the |
| 2145 | * EndTransfer Command Completion IRQ, but that's causing too |
| 2146 | * much trouble synchronizing between us and gadget driver. |
| 2147 | * |
| 2148 | * We have discussed this with the IP Provider and it was |
| 2149 | * suggested to giveback all requests here, but give HW some |
| 2150 | * extra time to synchronize with the interconnect. We're using |
Mickael Maison | dc93b41 | 2014-12-23 17:34:43 +0100 | [diff] [blame] | 2151 | * an arbitrary 100us delay for that. |
Pratyush Anand | 5791150 | 2012-07-06 15:19:10 +0530 | [diff] [blame] | 2152 | * |
| 2153 | * Note also that a similar handling was tested by Synopsys |
| 2154 | * (thanks a lot Paul) and nothing bad has come out of it. |
| 2155 | * In short, what we're doing is: |
| 2156 | * |
| 2157 | * - Issue EndTransfer WITH CMDIOC bit set |
| 2158 | * - Wait 100us |
| 2159 | */ |
| 2160 | |
Pratyush Anand | 3daf74d | 2012-06-23 02:23:08 +0530 | [diff] [blame] | 2161 | cmd = DWC3_DEPCMD_ENDTRANSFER; |
Paul Zimmerman | b992e68 | 2012-04-27 14:17:35 +0300 | [diff] [blame] | 2162 | cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0; |
| 2163 | cmd |= DWC3_DEPCMD_CMDIOC; |
Felipe Balbi | b4996a8 | 2012-06-06 12:04:13 +0300 | [diff] [blame] | 2164 | cmd |= DWC3_DEPCMD_PARAM(dep->resource_index); |
Pratyush Anand | 3daf74d | 2012-06-23 02:23:08 +0530 | [diff] [blame] | 2165 | memset(¶ms, 0, sizeof(params)); |
| 2166 | ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, ¶ms); |
| 2167 | WARN_ON_ONCE(ret); |
Felipe Balbi | b4996a8 | 2012-06-06 12:04:13 +0300 | [diff] [blame] | 2168 | dep->resource_index = 0; |
Felipe Balbi | 041d81f | 2012-10-04 11:58:00 +0300 | [diff] [blame] | 2169 | dep->flags &= ~DWC3_EP_BUSY; |
Pratyush Anand | 5791150 | 2012-07-06 15:19:10 +0530 | [diff] [blame] | 2170 | udelay(100); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2171 | } |
| 2172 | |
| 2173 | static void dwc3_stop_active_transfers(struct dwc3 *dwc) |
| 2174 | { |
| 2175 | u32 epnum; |
| 2176 | |
| 2177 | for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) { |
| 2178 | struct dwc3_ep *dep; |
| 2179 | |
| 2180 | dep = dwc->eps[epnum]; |
Felipe Balbi | 6a1e3ef | 2011-05-05 16:21:59 +0300 | [diff] [blame] | 2181 | if (!dep) |
| 2182 | continue; |
| 2183 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2184 | if (!(dep->flags & DWC3_EP_ENABLED)) |
| 2185 | continue; |
| 2186 | |
Sebastian Andrzej Siewior | 624407f | 2011-08-29 13:56:37 +0200 | [diff] [blame] | 2187 | dwc3_remove_requests(dwc, dep); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2188 | } |
| 2189 | } |
| 2190 | |
| 2191 | static void dwc3_clear_stall_all_ep(struct dwc3 *dwc) |
| 2192 | { |
| 2193 | u32 epnum; |
| 2194 | |
| 2195 | for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) { |
| 2196 | struct dwc3_ep *dep; |
| 2197 | struct dwc3_gadget_ep_cmd_params params; |
| 2198 | int ret; |
| 2199 | |
| 2200 | dep = dwc->eps[epnum]; |
Felipe Balbi | 6a1e3ef | 2011-05-05 16:21:59 +0300 | [diff] [blame] | 2201 | if (!dep) |
| 2202 | continue; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2203 | |
| 2204 | if (!(dep->flags & DWC3_EP_STALL)) |
| 2205 | continue; |
| 2206 | |
| 2207 | dep->flags &= ~DWC3_EP_STALL; |
| 2208 | |
| 2209 | memset(¶ms, 0, sizeof(params)); |
| 2210 | ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, |
| 2211 | DWC3_DEPCMD_CLEARSTALL, ¶ms); |
| 2212 | WARN_ON_ONCE(ret); |
| 2213 | } |
| 2214 | } |
| 2215 | |
| 2216 | static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc) |
| 2217 | { |
Felipe Balbi | c4430a2 | 2012-05-24 10:30:01 +0300 | [diff] [blame] | 2218 | int reg; |
| 2219 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2220 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
| 2221 | reg &= ~DWC3_DCTL_INITU1ENA; |
| 2222 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); |
| 2223 | |
| 2224 | reg &= ~DWC3_DCTL_INITU2ENA; |
| 2225 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2226 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2227 | dwc3_disconnect_gadget(dwc); |
| 2228 | |
| 2229 | dwc->gadget.speed = USB_SPEED_UNKNOWN; |
Felipe Balbi | df62df5 | 2011-10-14 15:11:49 +0300 | [diff] [blame] | 2230 | dwc->setup_packet_pending = false; |
Felipe Balbi | 06a374e | 2014-10-10 15:24:00 -0500 | [diff] [blame] | 2231 | usb_gadget_set_state(&dwc->gadget, USB_STATE_NOTATTACHED); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2232 | } |
| 2233 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2234 | static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc) |
| 2235 | { |
| 2236 | u32 reg; |
| 2237 | |
Felipe Balbi | df62df5 | 2011-10-14 15:11:49 +0300 | [diff] [blame] | 2238 | /* |
| 2239 | * WORKAROUND: DWC3 revisions <1.88a have an issue which |
| 2240 | * would cause a missing Disconnect Event if there's a |
| 2241 | * pending Setup Packet in the FIFO. |
| 2242 | * |
| 2243 | * There's no suggested workaround on the official Bug |
| 2244 | * report, which states that "unless the driver/application |
| 2245 | * is doing any special handling of a disconnect event, |
| 2246 | * there is no functional issue". |
| 2247 | * |
| 2248 | * Unfortunately, it turns out that we _do_ some special |
| 2249 | * handling of a disconnect event, namely complete all |
| 2250 | * pending transfers, notify gadget driver of the |
| 2251 | * disconnection, and so on. |
| 2252 | * |
| 2253 | * Our suggested workaround is to follow the Disconnect |
| 2254 | * Event steps here, instead, based on a setup_packet_pending |
Felipe Balbi | b5d335e | 2015-11-16 16:20:34 -0600 | [diff] [blame] | 2255 | * flag. Such flag gets set whenever we have a SETUP_PENDING |
| 2256 | * status for EP0 TRBs and gets cleared on XferComplete for the |
Felipe Balbi | df62df5 | 2011-10-14 15:11:49 +0300 | [diff] [blame] | 2257 | * same endpoint. |
| 2258 | * |
| 2259 | * Refers to: |
| 2260 | * |
| 2261 | * STAR#9000466709: RTL: Device : Disconnect event not |
| 2262 | * generated if setup packet pending in FIFO |
| 2263 | */ |
| 2264 | if (dwc->revision < DWC3_REVISION_188A) { |
| 2265 | if (dwc->setup_packet_pending) |
| 2266 | dwc3_gadget_disconnect_interrupt(dwc); |
| 2267 | } |
| 2268 | |
Felipe Balbi | 8e74475 | 2014-11-06 14:27:53 +0800 | [diff] [blame] | 2269 | dwc3_reset_gadget(dwc); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2270 | |
| 2271 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
| 2272 | reg &= ~DWC3_DCTL_TSTCTRL_MASK; |
| 2273 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); |
Gerard Cauvy | 3b63736 | 2012-02-10 12:21:18 +0200 | [diff] [blame] | 2274 | dwc->test_mode = false; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2275 | |
| 2276 | dwc3_stop_active_transfers(dwc); |
| 2277 | dwc3_clear_stall_all_ep(dwc); |
| 2278 | |
| 2279 | /* Reset device address to zero */ |
| 2280 | reg = dwc3_readl(dwc->regs, DWC3_DCFG); |
| 2281 | reg &= ~(DWC3_DCFG_DEVADDR_MASK); |
| 2282 | dwc3_writel(dwc->regs, DWC3_DCFG, reg); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2283 | } |
| 2284 | |
| 2285 | static void dwc3_update_ram_clk_sel(struct dwc3 *dwc, u32 speed) |
| 2286 | { |
| 2287 | u32 reg; |
| 2288 | u32 usb30_clock = DWC3_GCTL_CLK_BUS; |
| 2289 | |
| 2290 | /* |
| 2291 | * We change the clock only at SS but I dunno why I would want to do |
| 2292 | * this. Maybe it becomes part of the power saving plan. |
| 2293 | */ |
| 2294 | |
John Youn | ee5cd41 | 2016-02-05 17:08:45 -0800 | [diff] [blame] | 2295 | if ((speed != DWC3_DSTS_SUPERSPEED) && |
| 2296 | (speed != DWC3_DSTS_SUPERSPEED_PLUS)) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2297 | return; |
| 2298 | |
| 2299 | /* |
| 2300 | * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed |
| 2301 | * each time on Connect Done. |
| 2302 | */ |
| 2303 | if (!usb30_clock) |
| 2304 | return; |
| 2305 | |
| 2306 | reg = dwc3_readl(dwc->regs, DWC3_GCTL); |
| 2307 | reg |= DWC3_GCTL_RAMCLKSEL(usb30_clock); |
| 2308 | dwc3_writel(dwc->regs, DWC3_GCTL, reg); |
| 2309 | } |
| 2310 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2311 | static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc) |
| 2312 | { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2313 | struct dwc3_ep *dep; |
| 2314 | int ret; |
| 2315 | u32 reg; |
| 2316 | u8 speed; |
| 2317 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2318 | reg = dwc3_readl(dwc->regs, DWC3_DSTS); |
| 2319 | speed = reg & DWC3_DSTS_CONNECTSPD; |
| 2320 | dwc->speed = speed; |
| 2321 | |
| 2322 | dwc3_update_ram_clk_sel(dwc, speed); |
| 2323 | |
| 2324 | switch (speed) { |
John Youn | 7580862 | 2016-02-05 17:09:13 -0800 | [diff] [blame] | 2325 | case DWC3_DCFG_SUPERSPEED_PLUS: |
| 2326 | dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512); |
| 2327 | dwc->gadget.ep0->maxpacket = 512; |
| 2328 | dwc->gadget.speed = USB_SPEED_SUPER_PLUS; |
| 2329 | break; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2330 | case DWC3_DCFG_SUPERSPEED: |
Felipe Balbi | 05870c5 | 2011-10-14 14:51:38 +0300 | [diff] [blame] | 2331 | /* |
| 2332 | * WORKAROUND: DWC3 revisions <1.90a have an issue which |
| 2333 | * would cause a missing USB3 Reset event. |
| 2334 | * |
| 2335 | * In such situations, we should force a USB3 Reset |
| 2336 | * event by calling our dwc3_gadget_reset_interrupt() |
| 2337 | * routine. |
| 2338 | * |
| 2339 | * Refers to: |
| 2340 | * |
| 2341 | * STAR#9000483510: RTL: SS : USB3 reset event may |
| 2342 | * not be generated always when the link enters poll |
| 2343 | */ |
| 2344 | if (dwc->revision < DWC3_REVISION_190A) |
| 2345 | dwc3_gadget_reset_interrupt(dwc); |
| 2346 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2347 | dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512); |
| 2348 | dwc->gadget.ep0->maxpacket = 512; |
| 2349 | dwc->gadget.speed = USB_SPEED_SUPER; |
| 2350 | break; |
| 2351 | case DWC3_DCFG_HIGHSPEED: |
| 2352 | dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64); |
| 2353 | dwc->gadget.ep0->maxpacket = 64; |
| 2354 | dwc->gadget.speed = USB_SPEED_HIGH; |
| 2355 | break; |
| 2356 | case DWC3_DCFG_FULLSPEED2: |
| 2357 | case DWC3_DCFG_FULLSPEED1: |
| 2358 | dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64); |
| 2359 | dwc->gadget.ep0->maxpacket = 64; |
| 2360 | dwc->gadget.speed = USB_SPEED_FULL; |
| 2361 | break; |
| 2362 | case DWC3_DCFG_LOWSPEED: |
| 2363 | dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8); |
| 2364 | dwc->gadget.ep0->maxpacket = 8; |
| 2365 | dwc->gadget.speed = USB_SPEED_LOW; |
| 2366 | break; |
| 2367 | } |
| 2368 | |
Pratyush Anand | 2b75835 | 2013-01-14 15:59:31 +0530 | [diff] [blame] | 2369 | /* Enable USB2 LPM Capability */ |
| 2370 | |
John Youn | ee5cd41 | 2016-02-05 17:08:45 -0800 | [diff] [blame] | 2371 | if ((dwc->revision > DWC3_REVISION_194A) && |
| 2372 | (speed != DWC3_DCFG_SUPERSPEED) && |
| 2373 | (speed != DWC3_DCFG_SUPERSPEED_PLUS)) { |
Pratyush Anand | 2b75835 | 2013-01-14 15:59:31 +0530 | [diff] [blame] | 2374 | reg = dwc3_readl(dwc->regs, DWC3_DCFG); |
| 2375 | reg |= DWC3_DCFG_LPM_CAP; |
| 2376 | dwc3_writel(dwc->regs, DWC3_DCFG, reg); |
| 2377 | |
| 2378 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
| 2379 | reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN); |
| 2380 | |
Huang Rui | 460d098 | 2014-10-31 11:11:18 +0800 | [diff] [blame] | 2381 | reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold); |
Pratyush Anand | 2b75835 | 2013-01-14 15:59:31 +0530 | [diff] [blame] | 2382 | |
Huang Rui | 80caf7d | 2014-10-28 19:54:26 +0800 | [diff] [blame] | 2383 | /* |
| 2384 | * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and |
| 2385 | * DCFG.LPMCap is set, core responses with an ACK and the |
| 2386 | * BESL value in the LPM token is less than or equal to LPM |
| 2387 | * NYET threshold. |
| 2388 | */ |
| 2389 | WARN_ONCE(dwc->revision < DWC3_REVISION_240A |
| 2390 | && dwc->has_lpm_erratum, |
| 2391 | "LPM Erratum not available on dwc3 revisisions < 2.40a\n"); |
| 2392 | |
| 2393 | if (dwc->has_lpm_erratum && dwc->revision >= DWC3_REVISION_240A) |
| 2394 | reg |= DWC3_DCTL_LPM_ERRATA(dwc->lpm_nyet_threshold); |
| 2395 | |
Pratyush Anand | 2b75835 | 2013-01-14 15:59:31 +0530 | [diff] [blame] | 2396 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); |
Felipe Balbi | 356363b | 2013-12-19 16:37:05 -0600 | [diff] [blame] | 2397 | } else { |
| 2398 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
| 2399 | reg &= ~DWC3_DCTL_HIRD_THRES_MASK; |
| 2400 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); |
Pratyush Anand | 2b75835 | 2013-01-14 15:59:31 +0530 | [diff] [blame] | 2401 | } |
| 2402 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2403 | dep = dwc->eps[0]; |
Paul Zimmerman | 265b70a | 2013-12-19 12:38:49 -0600 | [diff] [blame] | 2404 | ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true, |
| 2405 | false); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2406 | if (ret) { |
| 2407 | dev_err(dwc->dev, "failed to enable %s\n", dep->name); |
| 2408 | return; |
| 2409 | } |
| 2410 | |
| 2411 | dep = dwc->eps[1]; |
Paul Zimmerman | 265b70a | 2013-12-19 12:38:49 -0600 | [diff] [blame] | 2412 | ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true, |
| 2413 | false); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2414 | if (ret) { |
| 2415 | dev_err(dwc->dev, "failed to enable %s\n", dep->name); |
| 2416 | return; |
| 2417 | } |
| 2418 | |
| 2419 | /* |
| 2420 | * Configure PHY via GUSB3PIPECTLn if required. |
| 2421 | * |
| 2422 | * Update GTXFIFOSIZn |
| 2423 | * |
| 2424 | * In both cases reset values should be sufficient. |
| 2425 | */ |
| 2426 | } |
| 2427 | |
| 2428 | static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc) |
| 2429 | { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2430 | /* |
| 2431 | * TODO take core out of low power mode when that's |
| 2432 | * implemented. |
| 2433 | */ |
| 2434 | |
Jiebing Li | ad14d4e | 2014-12-11 13:26:29 +0800 | [diff] [blame] | 2435 | if (dwc->gadget_driver && dwc->gadget_driver->resume) { |
| 2436 | spin_unlock(&dwc->lock); |
| 2437 | dwc->gadget_driver->resume(&dwc->gadget); |
| 2438 | spin_lock(&dwc->lock); |
| 2439 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2440 | } |
| 2441 | |
| 2442 | static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc, |
| 2443 | unsigned int evtinfo) |
| 2444 | { |
Felipe Balbi | fae2b90 | 2011-10-14 13:00:30 +0300 | [diff] [blame] | 2445 | enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK; |
Felipe Balbi | 0b0cc1c | 2012-09-18 21:39:24 +0300 | [diff] [blame] | 2446 | unsigned int pwropt; |
| 2447 | |
| 2448 | /* |
| 2449 | * WORKAROUND: DWC3 < 2.50a have an issue when configured without |
| 2450 | * Hibernation mode enabled which would show up when device detects |
| 2451 | * host-initiated U3 exit. |
| 2452 | * |
| 2453 | * In that case, device will generate a Link State Change Interrupt |
| 2454 | * from U3 to RESUME which is only necessary if Hibernation is |
| 2455 | * configured in. |
| 2456 | * |
| 2457 | * There are no functional changes due to such spurious event and we |
| 2458 | * just need to ignore it. |
| 2459 | * |
| 2460 | * Refers to: |
| 2461 | * |
| 2462 | * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation |
| 2463 | * operational mode |
| 2464 | */ |
| 2465 | pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1); |
| 2466 | if ((dwc->revision < DWC3_REVISION_250A) && |
| 2467 | (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) { |
| 2468 | if ((dwc->link_state == DWC3_LINK_STATE_U3) && |
| 2469 | (next == DWC3_LINK_STATE_RESUME)) { |
Felipe Balbi | 7381528 | 2015-01-27 13:48:14 -0600 | [diff] [blame] | 2470 | dwc3_trace(trace_dwc3_gadget, |
| 2471 | "ignoring transition U3 -> Resume"); |
Felipe Balbi | 0b0cc1c | 2012-09-18 21:39:24 +0300 | [diff] [blame] | 2472 | return; |
| 2473 | } |
| 2474 | } |
Felipe Balbi | fae2b90 | 2011-10-14 13:00:30 +0300 | [diff] [blame] | 2475 | |
| 2476 | /* |
| 2477 | * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending |
| 2478 | * on the link partner, the USB session might do multiple entry/exit |
| 2479 | * of low power states before a transfer takes place. |
| 2480 | * |
| 2481 | * Due to this problem, we might experience lower throughput. The |
| 2482 | * suggested workaround is to disable DCTL[12:9] bits if we're |
| 2483 | * transitioning from U1/U2 to U0 and enable those bits again |
| 2484 | * after a transfer completes and there are no pending transfers |
| 2485 | * on any of the enabled endpoints. |
| 2486 | * |
| 2487 | * This is the first half of that workaround. |
| 2488 | * |
| 2489 | * Refers to: |
| 2490 | * |
| 2491 | * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us |
| 2492 | * core send LGO_Ux entering U0 |
| 2493 | */ |
| 2494 | if (dwc->revision < DWC3_REVISION_183A) { |
| 2495 | if (next == DWC3_LINK_STATE_U0) { |
| 2496 | u32 u1u2; |
| 2497 | u32 reg; |
| 2498 | |
| 2499 | switch (dwc->link_state) { |
| 2500 | case DWC3_LINK_STATE_U1: |
| 2501 | case DWC3_LINK_STATE_U2: |
| 2502 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
| 2503 | u1u2 = reg & (DWC3_DCTL_INITU2ENA |
| 2504 | | DWC3_DCTL_ACCEPTU2ENA |
| 2505 | | DWC3_DCTL_INITU1ENA |
| 2506 | | DWC3_DCTL_ACCEPTU1ENA); |
| 2507 | |
| 2508 | if (!dwc->u1u2) |
| 2509 | dwc->u1u2 = reg & u1u2; |
| 2510 | |
| 2511 | reg &= ~u1u2; |
| 2512 | |
| 2513 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); |
| 2514 | break; |
| 2515 | default: |
| 2516 | /* do nothing */ |
| 2517 | break; |
| 2518 | } |
| 2519 | } |
| 2520 | } |
| 2521 | |
Felipe Balbi | bc5ba2e | 2014-02-26 10:17:07 -0600 | [diff] [blame] | 2522 | switch (next) { |
| 2523 | case DWC3_LINK_STATE_U1: |
| 2524 | if (dwc->speed == USB_SPEED_SUPER) |
| 2525 | dwc3_suspend_gadget(dwc); |
| 2526 | break; |
| 2527 | case DWC3_LINK_STATE_U2: |
| 2528 | case DWC3_LINK_STATE_U3: |
| 2529 | dwc3_suspend_gadget(dwc); |
| 2530 | break; |
| 2531 | case DWC3_LINK_STATE_RESUME: |
| 2532 | dwc3_resume_gadget(dwc); |
| 2533 | break; |
| 2534 | default: |
| 2535 | /* do nothing */ |
| 2536 | break; |
| 2537 | } |
| 2538 | |
Felipe Balbi | e57ebc1 | 2014-04-22 13:20:12 -0500 | [diff] [blame] | 2539 | dwc->link_state = next; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2540 | } |
| 2541 | |
Felipe Balbi | e1dadd3 | 2014-02-25 14:47:54 -0600 | [diff] [blame] | 2542 | static void dwc3_gadget_hibernation_interrupt(struct dwc3 *dwc, |
| 2543 | unsigned int evtinfo) |
| 2544 | { |
| 2545 | unsigned int is_ss = evtinfo & BIT(4); |
| 2546 | |
| 2547 | /** |
| 2548 | * WORKAROUND: DWC3 revison 2.20a with hibernation support |
| 2549 | * have a known issue which can cause USB CV TD.9.23 to fail |
| 2550 | * randomly. |
| 2551 | * |
| 2552 | * Because of this issue, core could generate bogus hibernation |
| 2553 | * events which SW needs to ignore. |
| 2554 | * |
| 2555 | * Refers to: |
| 2556 | * |
| 2557 | * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0 |
| 2558 | * Device Fallback from SuperSpeed |
| 2559 | */ |
| 2560 | if (is_ss ^ (dwc->speed == USB_SPEED_SUPER)) |
| 2561 | return; |
| 2562 | |
| 2563 | /* enter hibernation here */ |
| 2564 | } |
| 2565 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2566 | static void dwc3_gadget_interrupt(struct dwc3 *dwc, |
| 2567 | const struct dwc3_event_devt *event) |
| 2568 | { |
| 2569 | switch (event->type) { |
| 2570 | case DWC3_DEVICE_EVENT_DISCONNECT: |
| 2571 | dwc3_gadget_disconnect_interrupt(dwc); |
| 2572 | break; |
| 2573 | case DWC3_DEVICE_EVENT_RESET: |
| 2574 | dwc3_gadget_reset_interrupt(dwc); |
| 2575 | break; |
| 2576 | case DWC3_DEVICE_EVENT_CONNECT_DONE: |
| 2577 | dwc3_gadget_conndone_interrupt(dwc); |
| 2578 | break; |
| 2579 | case DWC3_DEVICE_EVENT_WAKEUP: |
| 2580 | dwc3_gadget_wakeup_interrupt(dwc); |
| 2581 | break; |
Felipe Balbi | e1dadd3 | 2014-02-25 14:47:54 -0600 | [diff] [blame] | 2582 | case DWC3_DEVICE_EVENT_HIBER_REQ: |
| 2583 | if (dev_WARN_ONCE(dwc->dev, !dwc->has_hibernation, |
| 2584 | "unexpected hibernation event\n")) |
| 2585 | break; |
| 2586 | |
| 2587 | dwc3_gadget_hibernation_interrupt(dwc, event->event_info); |
| 2588 | break; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2589 | case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE: |
| 2590 | dwc3_gadget_linksts_change_interrupt(dwc, event->event_info); |
| 2591 | break; |
| 2592 | case DWC3_DEVICE_EVENT_EOPF: |
Felipe Balbi | 7381528 | 2015-01-27 13:48:14 -0600 | [diff] [blame] | 2593 | dwc3_trace(trace_dwc3_gadget, "End of Periodic Frame"); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2594 | break; |
| 2595 | case DWC3_DEVICE_EVENT_SOF: |
Felipe Balbi | 7381528 | 2015-01-27 13:48:14 -0600 | [diff] [blame] | 2596 | dwc3_trace(trace_dwc3_gadget, "Start of Periodic Frame"); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2597 | break; |
| 2598 | case DWC3_DEVICE_EVENT_ERRATIC_ERROR: |
Felipe Balbi | 7381528 | 2015-01-27 13:48:14 -0600 | [diff] [blame] | 2599 | dwc3_trace(trace_dwc3_gadget, "Erratic Error"); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2600 | break; |
| 2601 | case DWC3_DEVICE_EVENT_CMD_CMPL: |
Felipe Balbi | 7381528 | 2015-01-27 13:48:14 -0600 | [diff] [blame] | 2602 | dwc3_trace(trace_dwc3_gadget, "Command Complete"); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2603 | break; |
| 2604 | case DWC3_DEVICE_EVENT_OVERFLOW: |
Felipe Balbi | 7381528 | 2015-01-27 13:48:14 -0600 | [diff] [blame] | 2605 | dwc3_trace(trace_dwc3_gadget, "Overflow"); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2606 | break; |
| 2607 | default: |
Felipe Balbi | e9f2aa8 | 2015-01-27 13:49:28 -0600 | [diff] [blame] | 2608 | dev_WARN(dwc->dev, "UNKNOWN IRQ %d\n", event->type); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2609 | } |
| 2610 | } |
| 2611 | |
| 2612 | static void dwc3_process_event_entry(struct dwc3 *dwc, |
| 2613 | const union dwc3_event *event) |
| 2614 | { |
Felipe Balbi | 2c4cbe6e5 | 2014-04-30 17:45:10 -0500 | [diff] [blame] | 2615 | trace_dwc3_event(event->raw); |
| 2616 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2617 | /* Endpoint IRQ, handle it and return early */ |
| 2618 | if (event->type.is_devspec == 0) { |
| 2619 | /* depevt */ |
| 2620 | return dwc3_endpoint_interrupt(dwc, &event->depevt); |
| 2621 | } |
| 2622 | |
| 2623 | switch (event->type.type) { |
| 2624 | case DWC3_EVENT_TYPE_DEV: |
| 2625 | dwc3_gadget_interrupt(dwc, &event->devt); |
| 2626 | break; |
| 2627 | /* REVISIT what to do with Carkit and I2C events ? */ |
| 2628 | default: |
| 2629 | dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw); |
| 2630 | } |
| 2631 | } |
| 2632 | |
Felipe Balbi | dea520a | 2016-03-30 09:39:34 +0300 | [diff] [blame] | 2633 | static irqreturn_t dwc3_process_event_buf(struct dwc3_event_buffer *evt) |
Felipe Balbi | f42f244 | 2013-06-12 21:25:08 +0300 | [diff] [blame] | 2634 | { |
Felipe Balbi | dea520a | 2016-03-30 09:39:34 +0300 | [diff] [blame] | 2635 | struct dwc3 *dwc = evt->dwc; |
Felipe Balbi | f42f244 | 2013-06-12 21:25:08 +0300 | [diff] [blame] | 2636 | irqreturn_t ret = IRQ_NONE; |
| 2637 | int left; |
| 2638 | u32 reg; |
| 2639 | |
Felipe Balbi | f42f244 | 2013-06-12 21:25:08 +0300 | [diff] [blame] | 2640 | left = evt->count; |
| 2641 | |
| 2642 | if (!(evt->flags & DWC3_EVENT_PENDING)) |
| 2643 | return IRQ_NONE; |
| 2644 | |
| 2645 | while (left > 0) { |
| 2646 | union dwc3_event event; |
| 2647 | |
| 2648 | event.raw = *(u32 *) (evt->buf + evt->lpos); |
| 2649 | |
| 2650 | dwc3_process_event_entry(dwc, &event); |
| 2651 | |
| 2652 | /* |
| 2653 | * FIXME we wrap around correctly to the next entry as |
| 2654 | * almost all entries are 4 bytes in size. There is one |
| 2655 | * entry which has 12 bytes which is a regular entry |
| 2656 | * followed by 8 bytes data. ATM I don't know how |
| 2657 | * things are organized if we get next to the a |
| 2658 | * boundary so I worry about that once we try to handle |
| 2659 | * that. |
| 2660 | */ |
| 2661 | evt->lpos = (evt->lpos + 4) % DWC3_EVENT_BUFFERS_SIZE; |
| 2662 | left -= 4; |
| 2663 | |
Felipe Balbi | 660e9bd | 2016-03-30 09:26:24 +0300 | [diff] [blame] | 2664 | dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), 4); |
Felipe Balbi | f42f244 | 2013-06-12 21:25:08 +0300 | [diff] [blame] | 2665 | } |
| 2666 | |
| 2667 | evt->count = 0; |
| 2668 | evt->flags &= ~DWC3_EVENT_PENDING; |
| 2669 | ret = IRQ_HANDLED; |
| 2670 | |
| 2671 | /* Unmask interrupt */ |
Felipe Balbi | 660e9bd | 2016-03-30 09:26:24 +0300 | [diff] [blame] | 2672 | reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0)); |
Felipe Balbi | f42f244 | 2013-06-12 21:25:08 +0300 | [diff] [blame] | 2673 | reg &= ~DWC3_GEVNTSIZ_INTMASK; |
Felipe Balbi | 660e9bd | 2016-03-30 09:26:24 +0300 | [diff] [blame] | 2674 | dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg); |
Felipe Balbi | f42f244 | 2013-06-12 21:25:08 +0300 | [diff] [blame] | 2675 | |
| 2676 | return ret; |
| 2677 | } |
| 2678 | |
Felipe Balbi | dea520a | 2016-03-30 09:39:34 +0300 | [diff] [blame] | 2679 | static irqreturn_t dwc3_thread_interrupt(int irq, void *_evt) |
Felipe Balbi | b15a762 | 2011-06-30 16:57:15 +0300 | [diff] [blame] | 2680 | { |
Felipe Balbi | dea520a | 2016-03-30 09:39:34 +0300 | [diff] [blame] | 2681 | struct dwc3_event_buffer *evt = _evt; |
| 2682 | struct dwc3 *dwc = evt->dwc; |
Felipe Balbi | e5f68b4a | 2015-10-12 13:25:44 -0500 | [diff] [blame] | 2683 | unsigned long flags; |
Felipe Balbi | b15a762 | 2011-06-30 16:57:15 +0300 | [diff] [blame] | 2684 | irqreturn_t ret = IRQ_NONE; |
Felipe Balbi | b15a762 | 2011-06-30 16:57:15 +0300 | [diff] [blame] | 2685 | |
Felipe Balbi | e5f68b4a | 2015-10-12 13:25:44 -0500 | [diff] [blame] | 2686 | spin_lock_irqsave(&dwc->lock, flags); |
Felipe Balbi | dea520a | 2016-03-30 09:39:34 +0300 | [diff] [blame] | 2687 | ret = dwc3_process_event_buf(evt); |
Felipe Balbi | e5f68b4a | 2015-10-12 13:25:44 -0500 | [diff] [blame] | 2688 | spin_unlock_irqrestore(&dwc->lock, flags); |
Felipe Balbi | b15a762 | 2011-06-30 16:57:15 +0300 | [diff] [blame] | 2689 | |
| 2690 | return ret; |
| 2691 | } |
| 2692 | |
Felipe Balbi | dea520a | 2016-03-30 09:39:34 +0300 | [diff] [blame] | 2693 | static irqreturn_t dwc3_check_event_buf(struct dwc3_event_buffer *evt) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2694 | { |
Felipe Balbi | dea520a | 2016-03-30 09:39:34 +0300 | [diff] [blame] | 2695 | struct dwc3 *dwc = evt->dwc; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2696 | u32 count; |
Felipe Balbi | e8adfc3 | 2013-06-12 21:11:14 +0300 | [diff] [blame] | 2697 | u32 reg; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2698 | |
Felipe Balbi | 660e9bd | 2016-03-30 09:26:24 +0300 | [diff] [blame] | 2699 | count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0)); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2700 | count &= DWC3_GEVNTCOUNT_MASK; |
| 2701 | if (!count) |
| 2702 | return IRQ_NONE; |
| 2703 | |
Felipe Balbi | b15a762 | 2011-06-30 16:57:15 +0300 | [diff] [blame] | 2704 | evt->count = count; |
| 2705 | evt->flags |= DWC3_EVENT_PENDING; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2706 | |
Felipe Balbi | e8adfc3 | 2013-06-12 21:11:14 +0300 | [diff] [blame] | 2707 | /* Mask interrupt */ |
Felipe Balbi | 660e9bd | 2016-03-30 09:26:24 +0300 | [diff] [blame] | 2708 | reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0)); |
Felipe Balbi | e8adfc3 | 2013-06-12 21:11:14 +0300 | [diff] [blame] | 2709 | reg |= DWC3_GEVNTSIZ_INTMASK; |
Felipe Balbi | 660e9bd | 2016-03-30 09:26:24 +0300 | [diff] [blame] | 2710 | dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg); |
Felipe Balbi | e8adfc3 | 2013-06-12 21:11:14 +0300 | [diff] [blame] | 2711 | |
Felipe Balbi | b15a762 | 2011-06-30 16:57:15 +0300 | [diff] [blame] | 2712 | return IRQ_WAKE_THREAD; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2713 | } |
| 2714 | |
Felipe Balbi | dea520a | 2016-03-30 09:39:34 +0300 | [diff] [blame] | 2715 | static irqreturn_t dwc3_interrupt(int irq, void *_evt) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2716 | { |
Felipe Balbi | dea520a | 2016-03-30 09:39:34 +0300 | [diff] [blame] | 2717 | struct dwc3_event_buffer *evt = _evt; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2718 | |
Felipe Balbi | dea520a | 2016-03-30 09:39:34 +0300 | [diff] [blame] | 2719 | return dwc3_check_event_buf(evt); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2720 | } |
| 2721 | |
| 2722 | /** |
| 2723 | * dwc3_gadget_init - Initializes gadget related registers |
Paul Zimmerman | 1d04679 | 2012-02-15 18:56:56 -0800 | [diff] [blame] | 2724 | * @dwc: pointer to our controller context structure |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2725 | * |
| 2726 | * Returns 0 on success otherwise negative errno. |
| 2727 | */ |
Bill Pemberton | 41ac7b3 | 2012-11-19 13:21:48 -0500 | [diff] [blame] | 2728 | int dwc3_gadget_init(struct dwc3 *dwc) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2729 | { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2730 | int ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2731 | |
| 2732 | dwc->ctrl_req = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ctrl_req), |
| 2733 | &dwc->ctrl_req_addr, GFP_KERNEL); |
| 2734 | if (!dwc->ctrl_req) { |
| 2735 | dev_err(dwc->dev, "failed to allocate ctrl request\n"); |
| 2736 | ret = -ENOMEM; |
| 2737 | goto err0; |
| 2738 | } |
| 2739 | |
Kishon Vijay Abraham I | 2abd9d5 | 2015-07-27 12:25:31 +0530 | [diff] [blame] | 2740 | dwc->ep0_trb = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ep0_trb) * 2, |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2741 | &dwc->ep0_trb_addr, GFP_KERNEL); |
| 2742 | if (!dwc->ep0_trb) { |
| 2743 | dev_err(dwc->dev, "failed to allocate ep0 trb\n"); |
| 2744 | ret = -ENOMEM; |
| 2745 | goto err1; |
| 2746 | } |
| 2747 | |
Felipe Balbi | 3ef35fa | 2012-05-04 12:58:14 +0300 | [diff] [blame] | 2748 | dwc->setup_buf = kzalloc(DWC3_EP0_BOUNCE_SIZE, GFP_KERNEL); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2749 | if (!dwc->setup_buf) { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2750 | ret = -ENOMEM; |
| 2751 | goto err2; |
| 2752 | } |
| 2753 | |
Felipe Balbi | 5812b1c | 2011-08-27 22:07:53 +0300 | [diff] [blame] | 2754 | dwc->ep0_bounce = dma_alloc_coherent(dwc->dev, |
Felipe Balbi | 3ef35fa | 2012-05-04 12:58:14 +0300 | [diff] [blame] | 2755 | DWC3_EP0_BOUNCE_SIZE, &dwc->ep0_bounce_addr, |
| 2756 | GFP_KERNEL); |
Felipe Balbi | 5812b1c | 2011-08-27 22:07:53 +0300 | [diff] [blame] | 2757 | if (!dwc->ep0_bounce) { |
| 2758 | dev_err(dwc->dev, "failed to allocate ep0 bounce buffer\n"); |
| 2759 | ret = -ENOMEM; |
| 2760 | goto err3; |
| 2761 | } |
| 2762 | |
Felipe Balbi | 04c03d1 | 2015-12-02 10:06:45 -0600 | [diff] [blame] | 2763 | dwc->zlp_buf = kzalloc(DWC3_ZLP_BUF_SIZE, GFP_KERNEL); |
| 2764 | if (!dwc->zlp_buf) { |
| 2765 | ret = -ENOMEM; |
| 2766 | goto err4; |
| 2767 | } |
| 2768 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2769 | dwc->gadget.ops = &dwc3_gadget_ops; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2770 | dwc->gadget.speed = USB_SPEED_UNKNOWN; |
Felipe Balbi | eeb720f | 2011-11-28 12:46:59 +0200 | [diff] [blame] | 2771 | dwc->gadget.sg_supported = true; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2772 | dwc->gadget.name = "dwc3-gadget"; |
Jianqiang Tang | 6a4290c | 2016-01-20 14:09:39 +0800 | [diff] [blame] | 2773 | dwc->gadget.is_otg = dwc->dr_mode == USB_DR_MODE_OTG; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2774 | |
| 2775 | /* |
Ben McCauley | b9e51b2 | 2015-11-16 10:47:24 -0600 | [diff] [blame] | 2776 | * FIXME We might be setting max_speed to <SUPER, however versions |
| 2777 | * <2.20a of dwc3 have an issue with metastability (documented |
| 2778 | * elsewhere in this driver) which tells us we can't set max speed to |
| 2779 | * anything lower than SUPER. |
| 2780 | * |
| 2781 | * Because gadget.max_speed is only used by composite.c and function |
| 2782 | * drivers (i.e. it won't go into dwc3's registers) we are allowing this |
| 2783 | * to happen so we avoid sending SuperSpeed Capability descriptor |
| 2784 | * together with our BOS descriptor as that could confuse host into |
| 2785 | * thinking we can handle super speed. |
| 2786 | * |
| 2787 | * Note that, in fact, we won't even support GetBOS requests when speed |
| 2788 | * is less than super speed because we don't have means, yet, to tell |
| 2789 | * composite.c that we are USB 2.0 + LPM ECN. |
| 2790 | */ |
| 2791 | if (dwc->revision < DWC3_REVISION_220A) |
| 2792 | dwc3_trace(trace_dwc3_gadget, |
| 2793 | "Changing max_speed on rev %08x\n", |
| 2794 | dwc->revision); |
| 2795 | |
| 2796 | dwc->gadget.max_speed = dwc->maximum_speed; |
| 2797 | |
| 2798 | /* |
David Cohen | a4b9d94 | 2013-12-09 15:55:38 -0800 | [diff] [blame] | 2799 | * Per databook, DWC3 needs buffer size to be aligned to MaxPacketSize |
| 2800 | * on ep out. |
| 2801 | */ |
| 2802 | dwc->gadget.quirk_ep_out_aligned_size = true; |
| 2803 | |
| 2804 | /* |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2805 | * REVISIT: Here we should clear all pending IRQs to be |
| 2806 | * sure we're starting from a well known location. |
| 2807 | */ |
| 2808 | |
| 2809 | ret = dwc3_gadget_init_endpoints(dwc); |
| 2810 | if (ret) |
Felipe Balbi | 04c03d1 | 2015-12-02 10:06:45 -0600 | [diff] [blame] | 2811 | goto err5; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2812 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2813 | ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget); |
| 2814 | if (ret) { |
| 2815 | dev_err(dwc->dev, "failed to register udc\n"); |
Felipe Balbi | 04c03d1 | 2015-12-02 10:06:45 -0600 | [diff] [blame] | 2816 | goto err5; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2817 | } |
| 2818 | |
| 2819 | return 0; |
| 2820 | |
Felipe Balbi | 04c03d1 | 2015-12-02 10:06:45 -0600 | [diff] [blame] | 2821 | err5: |
| 2822 | kfree(dwc->zlp_buf); |
| 2823 | |
Felipe Balbi | 5812b1c | 2011-08-27 22:07:53 +0300 | [diff] [blame] | 2824 | err4: |
David Cohen | e1f8046 | 2013-09-11 17:42:47 -0700 | [diff] [blame] | 2825 | dwc3_gadget_free_endpoints(dwc); |
Felipe Balbi | 3ef35fa | 2012-05-04 12:58:14 +0300 | [diff] [blame] | 2826 | dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE, |
| 2827 | dwc->ep0_bounce, dwc->ep0_bounce_addr); |
Felipe Balbi | 5812b1c | 2011-08-27 22:07:53 +0300 | [diff] [blame] | 2828 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2829 | err3: |
Felipe Balbi | 0fc9a1b | 2011-12-19 11:32:34 +0200 | [diff] [blame] | 2830 | kfree(dwc->setup_buf); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2831 | |
| 2832 | err2: |
| 2833 | dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb), |
| 2834 | dwc->ep0_trb, dwc->ep0_trb_addr); |
| 2835 | |
| 2836 | err1: |
| 2837 | dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req), |
| 2838 | dwc->ctrl_req, dwc->ctrl_req_addr); |
| 2839 | |
| 2840 | err0: |
| 2841 | return ret; |
| 2842 | } |
| 2843 | |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 2844 | /* -------------------------------------------------------------------------- */ |
| 2845 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2846 | void dwc3_gadget_exit(struct dwc3 *dwc) |
| 2847 | { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2848 | usb_del_gadget_udc(&dwc->gadget); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2849 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2850 | dwc3_gadget_free_endpoints(dwc); |
| 2851 | |
Felipe Balbi | 3ef35fa | 2012-05-04 12:58:14 +0300 | [diff] [blame] | 2852 | dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE, |
| 2853 | dwc->ep0_bounce, dwc->ep0_bounce_addr); |
Felipe Balbi | 5812b1c | 2011-08-27 22:07:53 +0300 | [diff] [blame] | 2854 | |
Felipe Balbi | 0fc9a1b | 2011-12-19 11:32:34 +0200 | [diff] [blame] | 2855 | kfree(dwc->setup_buf); |
Felipe Balbi | 04c03d1 | 2015-12-02 10:06:45 -0600 | [diff] [blame] | 2856 | kfree(dwc->zlp_buf); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2857 | |
| 2858 | dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb), |
| 2859 | dwc->ep0_trb, dwc->ep0_trb_addr); |
| 2860 | |
| 2861 | dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req), |
| 2862 | dwc->ctrl_req, dwc->ctrl_req_addr); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2863 | } |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 2864 | |
Felipe Balbi | 0b0231a | 2014-10-07 10:19:23 -0500 | [diff] [blame] | 2865 | int dwc3_gadget_suspend(struct dwc3 *dwc) |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 2866 | { |
Felipe Balbi | 7b2a036 | 2013-12-19 13:43:19 -0600 | [diff] [blame] | 2867 | if (dwc->pullups_connected) { |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 2868 | dwc3_gadget_disable_irq(dwc); |
Felipe Balbi | 7b2a036 | 2013-12-19 13:43:19 -0600 | [diff] [blame] | 2869 | dwc3_gadget_run_stop(dwc, true, true); |
| 2870 | } |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 2871 | |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 2872 | __dwc3_gadget_ep_disable(dwc->eps[0]); |
| 2873 | __dwc3_gadget_ep_disable(dwc->eps[1]); |
| 2874 | |
| 2875 | dwc->dcfg = dwc3_readl(dwc->regs, DWC3_DCFG); |
| 2876 | |
| 2877 | return 0; |
| 2878 | } |
| 2879 | |
| 2880 | int dwc3_gadget_resume(struct dwc3 *dwc) |
| 2881 | { |
| 2882 | struct dwc3_ep *dep; |
| 2883 | int ret; |
| 2884 | |
| 2885 | /* Start with SuperSpeed Default */ |
| 2886 | dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512); |
| 2887 | |
| 2888 | dep = dwc->eps[0]; |
Paul Zimmerman | 265b70a | 2013-12-19 12:38:49 -0600 | [diff] [blame] | 2889 | ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false, |
| 2890 | false); |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 2891 | if (ret) |
| 2892 | goto err0; |
| 2893 | |
| 2894 | dep = dwc->eps[1]; |
Paul Zimmerman | 265b70a | 2013-12-19 12:38:49 -0600 | [diff] [blame] | 2895 | ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false, |
| 2896 | false); |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 2897 | if (ret) |
| 2898 | goto err1; |
| 2899 | |
| 2900 | /* begin to receive SETUP packets */ |
| 2901 | dwc->ep0state = EP0_SETUP_PHASE; |
| 2902 | dwc3_ep0_out_start(dwc); |
| 2903 | |
| 2904 | dwc3_writel(dwc->regs, DWC3_DCFG, dwc->dcfg); |
| 2905 | |
Felipe Balbi | 0b0231a | 2014-10-07 10:19:23 -0500 | [diff] [blame] | 2906 | if (dwc->pullups_connected) { |
| 2907 | dwc3_gadget_enable_irq(dwc); |
| 2908 | dwc3_gadget_run_stop(dwc, true, false); |
| 2909 | } |
| 2910 | |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 2911 | return 0; |
| 2912 | |
| 2913 | err1: |
| 2914 | __dwc3_gadget_ep_disable(dwc->eps[0]); |
| 2915 | |
| 2916 | err0: |
| 2917 | return ret; |
| 2918 | } |