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