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