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