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