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