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