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