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