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