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