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