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