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