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