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