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