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