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