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 | * |
| 9 | * Redistribution and use in source and binary forms, with or without |
| 10 | * modification, are permitted provided that the following conditions |
| 11 | * are met: |
| 12 | * 1. Redistributions of source code must retain the above copyright |
| 13 | * notice, this list of conditions, and the following disclaimer, |
| 14 | * without modification. |
| 15 | * 2. Redistributions in binary form must reproduce the above copyright |
| 16 | * notice, this list of conditions and the following disclaimer in the |
| 17 | * documentation and/or other materials provided with the distribution. |
| 18 | * 3. The names of the above-listed copyright holders may not be used |
| 19 | * to endorse or promote products derived from this software without |
| 20 | * specific prior written permission. |
| 21 | * |
| 22 | * ALTERNATIVELY, this software may be distributed under the terms of the |
| 23 | * GNU General Public License ("GPL") version 2, as published by the Free |
| 24 | * Software Foundation. |
| 25 | * |
| 26 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
| 27 | * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 28 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 29 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 30 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 31 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 32 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 33 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 34 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 35 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 36 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 37 | */ |
| 38 | |
| 39 | #include <linux/kernel.h> |
| 40 | #include <linux/delay.h> |
| 41 | #include <linux/slab.h> |
| 42 | #include <linux/spinlock.h> |
| 43 | #include <linux/platform_device.h> |
| 44 | #include <linux/pm_runtime.h> |
| 45 | #include <linux/interrupt.h> |
| 46 | #include <linux/io.h> |
| 47 | #include <linux/list.h> |
| 48 | #include <linux/dma-mapping.h> |
| 49 | |
| 50 | #include <linux/usb/ch9.h> |
| 51 | #include <linux/usb/gadget.h> |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 52 | #include <linux/usb/otg.h> |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 53 | |
| 54 | #include "core.h" |
| 55 | #include "gadget.h" |
| 56 | #include "io.h" |
| 57 | |
Felipe Balbi | 04a9bfc | 2012-01-02 18:25:43 +0200 | [diff] [blame] | 58 | /** |
| 59 | * dwc3_gadget_set_test_mode - Enables USB2 Test Modes |
| 60 | * @dwc: pointer to our context structure |
| 61 | * @mode: the mode to set (J, K SE0 NAK, Force Enable) |
| 62 | * |
| 63 | * Caller should take care of locking. This function will |
| 64 | * return 0 on success or -EINVAL if wrong Test Selector |
| 65 | * is passed |
| 66 | */ |
| 67 | int dwc3_gadget_set_test_mode(struct dwc3 *dwc, int mode) |
| 68 | { |
| 69 | u32 reg; |
| 70 | |
| 71 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
| 72 | reg &= ~DWC3_DCTL_TSTCTRL_MASK; |
| 73 | |
| 74 | switch (mode) { |
| 75 | case TEST_J: |
| 76 | case TEST_K: |
| 77 | case TEST_SE0_NAK: |
| 78 | case TEST_PACKET: |
| 79 | case TEST_FORCE_EN: |
| 80 | reg |= mode << 1; |
| 81 | break; |
| 82 | default: |
| 83 | return -EINVAL; |
| 84 | } |
| 85 | |
| 86 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); |
| 87 | |
| 88 | return 0; |
| 89 | } |
| 90 | |
Felipe Balbi | 8598bde | 2012-01-02 18:55:57 +0200 | [diff] [blame] | 91 | /** |
| 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 | 88df427 | 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 | 88df427 | 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 | 8b9388f | 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 | |
| 146 | dev_vdbg(dwc->dev, "link state change request timed out\n"); |
| 147 | |
| 148 | return -ETIMEDOUT; |
| 149 | } |
| 150 | |
Felipe Balbi | 457e84b | 2012-01-18 18:04:09 +0200 | [diff] [blame] | 151 | /** |
| 152 | * dwc3_gadget_resize_tx_fifos - reallocate fifo spaces for current use-case |
| 153 | * @dwc: pointer to our context structure |
| 154 | * |
| 155 | * This function will a best effort FIFO allocation in order |
| 156 | * to improve FIFO usage and throughput, while still allowing |
| 157 | * us to enable as many endpoints as possible. |
| 158 | * |
| 159 | * Keep in mind that this operation will be highly dependent |
| 160 | * on the configured size for RAM1 - which contains TxFifo -, |
| 161 | * the amount of endpoints enabled on coreConsultant tool, and |
| 162 | * the width of the Master Bus. |
| 163 | * |
| 164 | * In the ideal world, we would always be able to satisfy the |
| 165 | * following equation: |
| 166 | * |
| 167 | * ((512 + 2 * MDWIDTH-Bytes) + (Number of IN Endpoints - 1) * \ |
| 168 | * (3 * (1024 + MDWIDTH-Bytes) + MDWIDTH-Bytes)) / MDWIDTH-Bytes |
| 169 | * |
| 170 | * Unfortunately, due to many variables that's not always the case. |
| 171 | */ |
| 172 | int dwc3_gadget_resize_tx_fifos(struct dwc3 *dwc) |
| 173 | { |
| 174 | int last_fifo_depth = 0; |
| 175 | int ram1_depth; |
| 176 | int fifo_size; |
| 177 | int mdwidth; |
| 178 | int num; |
| 179 | |
| 180 | if (!dwc->needs_fifo_resize) |
| 181 | return 0; |
| 182 | |
| 183 | ram1_depth = DWC3_RAM1_DEPTH(dwc->hwparams.hwparams7); |
| 184 | mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0); |
| 185 | |
| 186 | /* MDWIDTH is represented in bits, we need it in bytes */ |
| 187 | mdwidth >>= 3; |
| 188 | |
| 189 | /* |
| 190 | * FIXME For now we will only allocate 1 wMaxPacketSize space |
| 191 | * for each enabled endpoint, later patches will come to |
| 192 | * improve this algorithm so that we better use the internal |
| 193 | * FIFO space |
| 194 | */ |
| 195 | for (num = 0; num < DWC3_ENDPOINTS_NUM; num++) { |
| 196 | struct dwc3_ep *dep = dwc->eps[num]; |
| 197 | int fifo_number = dep->number >> 1; |
Felipe Balbi | 2e81c36 | 2012-02-02 13:01:12 +0200 | [diff] [blame] | 198 | int mult = 1; |
Felipe Balbi | 457e84b | 2012-01-18 18:04:09 +0200 | [diff] [blame] | 199 | int tmp; |
| 200 | |
| 201 | if (!(dep->number & 1)) |
| 202 | continue; |
| 203 | |
| 204 | if (!(dep->flags & DWC3_EP_ENABLED)) |
| 205 | continue; |
| 206 | |
Ido Shayevitz | 57cdac1 | 2012-03-12 20:25:24 +0200 | [diff] [blame] | 207 | if (usb_endpoint_xfer_bulk(dep->endpoint.desc) |
| 208 | || usb_endpoint_xfer_isoc(dep->endpoint.desc)) |
Felipe Balbi | 2e81c36 | 2012-02-02 13:01:12 +0200 | [diff] [blame] | 209 | mult = 3; |
| 210 | |
| 211 | /* |
| 212 | * REVISIT: the following assumes we will always have enough |
| 213 | * space available on the FIFO RAM for all possible use cases. |
| 214 | * Make sure that's true somehow and change FIFO allocation |
| 215 | * accordingly. |
| 216 | * |
| 217 | * If we have Bulk or Isochronous endpoints, we want |
| 218 | * them to be able to be very, very fast. So we're giving |
| 219 | * those endpoints a fifo_size which is enough for 3 full |
| 220 | * packets |
| 221 | */ |
| 222 | tmp = mult * (dep->endpoint.maxpacket + mdwidth); |
Felipe Balbi | 457e84b | 2012-01-18 18:04:09 +0200 | [diff] [blame] | 223 | tmp += mdwidth; |
| 224 | |
| 225 | fifo_size = DIV_ROUND_UP(tmp, mdwidth); |
Felipe Balbi | 2e81c36 | 2012-02-02 13:01:12 +0200 | [diff] [blame] | 226 | |
Felipe Balbi | 457e84b | 2012-01-18 18:04:09 +0200 | [diff] [blame] | 227 | fifo_size |= (last_fifo_depth << 16); |
| 228 | |
| 229 | dev_vdbg(dwc->dev, "%s: Fifo Addr %04x Size %d\n", |
| 230 | dep->name, last_fifo_depth, fifo_size & 0xffff); |
| 231 | |
| 232 | dwc3_writel(dwc->regs, DWC3_GTXFIFOSIZ(fifo_number), |
| 233 | fifo_size); |
| 234 | |
| 235 | last_fifo_depth += (fifo_size & 0xffff); |
| 236 | } |
| 237 | |
| 238 | return 0; |
| 239 | } |
| 240 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 241 | void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req, |
| 242 | int status) |
| 243 | { |
| 244 | struct dwc3 *dwc = dep->dwc; |
| 245 | |
| 246 | if (req->queued) { |
Manu Gautam | 55d3422 | 2012-12-19 16:49:47 +0530 | [diff] [blame] | 247 | req->queued = false; |
| 248 | |
Felipe Balbi | eeb720f | 2011-11-28 12:46:59 +0200 | [diff] [blame] | 249 | if (req->request.num_mapped_sgs) |
| 250 | dep->busy_slot += req->request.num_mapped_sgs; |
| 251 | else |
| 252 | dep->busy_slot++; |
| 253 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 254 | /* |
| 255 | * Skip LINK TRB. We can't use req->trb and check for |
| 256 | * DWC3_TRBCTL_LINK_TRB because it points the TRB we just |
| 257 | * completed (not the LINK TRB). |
| 258 | */ |
| 259 | if (((dep->busy_slot & DWC3_TRB_MASK) == DWC3_TRB_NUM - 1) && |
Ido Shayevitz | 57cdac1 | 2012-03-12 20:25:24 +0200 | [diff] [blame] | 260 | usb_endpoint_xfer_isoc(dep->endpoint.desc)) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 261 | dep->busy_slot++; |
| 262 | } |
| 263 | list_del(&req->list); |
Felipe Balbi | eeb720f | 2011-11-28 12:46:59 +0200 | [diff] [blame] | 264 | req->trb = NULL; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 265 | |
| 266 | if (req->request.status == -EINPROGRESS) |
| 267 | req->request.status = status; |
| 268 | |
Pratyush Anand | 8d7bf59 | 2012-08-10 13:42:16 +0530 | [diff] [blame] | 269 | if (dwc->ep0_bounced && dep->number == 0) |
| 270 | dwc->ep0_bounced = false; |
| 271 | else |
| 272 | usb_gadget_unmap_request(&dwc->gadget, &req->request, |
| 273 | req->direction); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 274 | |
| 275 | dev_dbg(dwc->dev, "request %p from %s completed %d/%d ===> %d\n", |
| 276 | req, dep->name, req->request.actual, |
| 277 | req->request.length, status); |
| 278 | |
| 279 | spin_unlock(&dwc->lock); |
Felipe Balbi | 0fc9a1b | 2011-12-19 11:32:34 +0200 | [diff] [blame] | 280 | req->request.complete(&dep->endpoint, &req->request); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 281 | spin_lock(&dwc->lock); |
| 282 | } |
| 283 | |
| 284 | static const char *dwc3_gadget_ep_cmd_string(u8 cmd) |
| 285 | { |
| 286 | switch (cmd) { |
| 287 | case DWC3_DEPCMD_DEPSTARTCFG: |
| 288 | return "Start New Configuration"; |
| 289 | case DWC3_DEPCMD_ENDTRANSFER: |
| 290 | return "End Transfer"; |
| 291 | case DWC3_DEPCMD_UPDATETRANSFER: |
| 292 | return "Update Transfer"; |
| 293 | case DWC3_DEPCMD_STARTTRANSFER: |
| 294 | return "Start Transfer"; |
| 295 | case DWC3_DEPCMD_CLEARSTALL: |
| 296 | return "Clear Stall"; |
| 297 | case DWC3_DEPCMD_SETSTALL: |
| 298 | return "Set Stall"; |
Paul Zimmerman | 88df427 | 2012-04-27 13:10:52 +0300 | [diff] [blame] | 299 | case DWC3_DEPCMD_GETEPSTATE: |
| 300 | return "Get Endpoint State"; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 301 | case DWC3_DEPCMD_SETTRANSFRESOURCE: |
| 302 | return "Set Endpoint Transfer Resource"; |
| 303 | case DWC3_DEPCMD_SETEPCONFIG: |
| 304 | return "Set Endpoint Configuration"; |
| 305 | default: |
| 306 | return "UNKNOWN command"; |
| 307 | } |
| 308 | } |
| 309 | |
Felipe Balbi | 573c276 | 2012-04-24 16:19:11 +0300 | [diff] [blame] | 310 | int dwc3_send_gadget_generic_command(struct dwc3 *dwc, int cmd, u32 param) |
| 311 | { |
| 312 | u32 timeout = 500; |
| 313 | u32 reg; |
| 314 | |
| 315 | dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param); |
| 316 | dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT); |
| 317 | |
| 318 | do { |
| 319 | reg = dwc3_readl(dwc->regs, DWC3_DGCMD); |
| 320 | if (!(reg & DWC3_DGCMD_CMDACT)) { |
| 321 | dev_vdbg(dwc->dev, "Command Complete --> %d\n", |
| 322 | DWC3_DGCMD_STATUS(reg)); |
| 323 | return 0; |
| 324 | } |
| 325 | |
| 326 | /* |
| 327 | * We can't sleep here, because it's also called from |
| 328 | * interrupt context. |
| 329 | */ |
| 330 | timeout--; |
| 331 | if (!timeout) |
| 332 | return -ETIMEDOUT; |
| 333 | udelay(1); |
| 334 | } while (1); |
| 335 | } |
| 336 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 337 | int dwc3_send_gadget_ep_cmd(struct dwc3 *dwc, unsigned ep, |
| 338 | unsigned cmd, struct dwc3_gadget_ep_cmd_params *params) |
| 339 | { |
| 340 | struct dwc3_ep *dep = dwc->eps[ep]; |
Sebastian Andrzej Siewior | 61d5824 | 2011-08-29 16:46:38 +0200 | [diff] [blame] | 341 | u32 timeout = 500; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 342 | u32 reg; |
| 343 | |
| 344 | dev_vdbg(dwc->dev, "%s: cmd '%s' params %08x %08x %08x\n", |
| 345 | dep->name, |
Felipe Balbi | dc1c70a | 2011-09-30 10:58:51 +0300 | [diff] [blame] | 346 | dwc3_gadget_ep_cmd_string(cmd), params->param0, |
| 347 | params->param1, params->param2); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 348 | |
Felipe Balbi | dc1c70a | 2011-09-30 10:58:51 +0300 | [diff] [blame] | 349 | dwc3_writel(dwc->regs, DWC3_DEPCMDPAR0(ep), params->param0); |
| 350 | dwc3_writel(dwc->regs, DWC3_DEPCMDPAR1(ep), params->param1); |
| 351 | dwc3_writel(dwc->regs, DWC3_DEPCMDPAR2(ep), params->param2); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 352 | |
| 353 | dwc3_writel(dwc->regs, DWC3_DEPCMD(ep), cmd | DWC3_DEPCMD_CMDACT); |
| 354 | do { |
| 355 | reg = dwc3_readl(dwc->regs, DWC3_DEPCMD(ep)); |
| 356 | if (!(reg & DWC3_DEPCMD_CMDACT)) { |
Felipe Balbi | 164f6e1 | 2011-08-27 20:29:58 +0300 | [diff] [blame] | 357 | dev_vdbg(dwc->dev, "Command Complete --> %d\n", |
| 358 | DWC3_DEPCMD_STATUS(reg)); |
Vijayavardhan Vennapusa | 91ba653 | 2013-01-30 17:35:45 +0530 | [diff] [blame] | 359 | /* SW issues START TRANSFER command to isochronous ep |
| 360 | * with future frame interval. If future interval time |
| 361 | * has already passed when core recieves command, core |
| 362 | * will respond with an error(bit13 in Command complete |
| 363 | * event. Hence return error in this case. |
| 364 | */ |
| 365 | if (reg & 0x2000) |
| 366 | return -EAGAIN; |
| 367 | else |
| 368 | return 0; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | /* |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 372 | * We can't sleep here, because it is also called from |
| 373 | * interrupt context. |
| 374 | */ |
| 375 | timeout--; |
| 376 | if (!timeout) |
| 377 | return -ETIMEDOUT; |
| 378 | |
Sebastian Andrzej Siewior | 61d5824 | 2011-08-29 16:46:38 +0200 | [diff] [blame] | 379 | udelay(1); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 380 | } while (1); |
| 381 | } |
| 382 | |
Ido Shayevitz | fa65a58 | 2012-06-06 14:39:54 +0300 | [diff] [blame] | 383 | dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep, |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 384 | struct dwc3_trb *trb) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 385 | { |
Paul Zimmerman | c439ef8 | 2011-09-30 10:58:45 +0300 | [diff] [blame] | 386 | u32 offset = (char *) trb - (char *) dep->trb_pool; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 387 | |
| 388 | return dep->trb_pool_dma + offset; |
| 389 | } |
| 390 | |
| 391 | static int dwc3_alloc_trb_pool(struct dwc3_ep *dep) |
| 392 | { |
| 393 | struct dwc3 *dwc = dep->dwc; |
| 394 | |
| 395 | if (dep->trb_pool) |
| 396 | return 0; |
| 397 | |
| 398 | if (dep->number == 0 || dep->number == 1) |
| 399 | return 0; |
| 400 | |
| 401 | dep->trb_pool = dma_alloc_coherent(dwc->dev, |
| 402 | sizeof(struct dwc3_trb) * DWC3_TRB_NUM, |
| 403 | &dep->trb_pool_dma, GFP_KERNEL); |
| 404 | if (!dep->trb_pool) { |
| 405 | dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n", |
| 406 | dep->name); |
| 407 | return -ENOMEM; |
| 408 | } |
| 409 | |
| 410 | return 0; |
| 411 | } |
| 412 | |
| 413 | static void dwc3_free_trb_pool(struct dwc3_ep *dep) |
| 414 | { |
| 415 | struct dwc3 *dwc = dep->dwc; |
| 416 | |
| 417 | dma_free_coherent(dwc->dev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM, |
| 418 | dep->trb_pool, dep->trb_pool_dma); |
| 419 | |
| 420 | dep->trb_pool = NULL; |
| 421 | dep->trb_pool_dma = 0; |
| 422 | } |
| 423 | |
| 424 | static int dwc3_gadget_start_config(struct dwc3 *dwc, struct dwc3_ep *dep) |
| 425 | { |
| 426 | struct dwc3_gadget_ep_cmd_params params; |
| 427 | u32 cmd; |
| 428 | |
| 429 | memset(¶ms, 0x00, sizeof(params)); |
| 430 | |
| 431 | if (dep->number != 1) { |
| 432 | cmd = DWC3_DEPCMD_DEPSTARTCFG; |
| 433 | /* XferRscIdx == 0 for ep0 and 2 for the remaining */ |
Paul Zimmerman | b23c843 | 2011-09-30 10:58:42 +0300 | [diff] [blame] | 434 | if (dep->number > 1) { |
| 435 | if (dwc->start_config_issued) |
| 436 | return 0; |
| 437 | dwc->start_config_issued = true; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 438 | cmd |= DWC3_DEPCMD_PARAM(2); |
Paul Zimmerman | b23c843 | 2011-09-30 10:58:42 +0300 | [diff] [blame] | 439 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 440 | |
| 441 | return dwc3_send_gadget_ep_cmd(dwc, 0, cmd, ¶ms); |
| 442 | } |
| 443 | |
| 444 | return 0; |
| 445 | } |
| 446 | |
| 447 | 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] | 448 | const struct usb_endpoint_descriptor *desc, |
Felipe Balbi | 07e0ee8 | 2012-07-16 14:08:16 +0300 | [diff] [blame] | 449 | const struct usb_ss_ep_comp_descriptor *comp_desc, |
| 450 | bool ignore) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 451 | { |
| 452 | struct dwc3_gadget_ep_cmd_params params; |
| 453 | |
| 454 | memset(¶ms, 0x00, sizeof(params)); |
| 455 | |
Felipe Balbi | dc1c70a | 2011-09-30 10:58:51 +0300 | [diff] [blame] | 456 | params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc)) |
Chanho Park | f0ee606 | 2012-08-31 16:54:07 +0900 | [diff] [blame] | 457 | | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc)); |
| 458 | |
| 459 | /* Burst size is only needed in SuperSpeed mode */ |
| 460 | if (dwc->gadget.speed == USB_SPEED_SUPER) { |
| 461 | u32 burst = dep->endpoint.maxburst - 1; |
| 462 | |
| 463 | params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst); |
| 464 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 465 | |
Felipe Balbi | 07e0ee8 | 2012-07-16 14:08:16 +0300 | [diff] [blame] | 466 | if (ignore) |
| 467 | params.param0 |= DWC3_DEPCFG_IGN_SEQ_NUM; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 468 | |
Felipe Balbi | dc1c70a | 2011-09-30 10:58:51 +0300 | [diff] [blame] | 469 | params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN |
| 470 | | DWC3_DEPCFG_XFER_NOT_READY_EN; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 471 | |
Felipe Balbi | 18b7ede | 2012-01-02 13:35:41 +0200 | [diff] [blame] | 472 | if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) { |
Felipe Balbi | dc1c70a | 2011-09-30 10:58:51 +0300 | [diff] [blame] | 473 | params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE |
| 474 | | DWC3_DEPCFG_STREAM_EVENT_EN; |
Felipe Balbi | 879631a | 2011-09-30 10:58:47 +0300 | [diff] [blame] | 475 | dep->stream_capable = true; |
| 476 | } |
| 477 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 478 | if (usb_endpoint_xfer_isoc(desc)) |
Felipe Balbi | dc1c70a | 2011-09-30 10:58:51 +0300 | [diff] [blame] | 479 | params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 480 | |
| 481 | /* |
| 482 | * We are doing 1:1 mapping for endpoints, meaning |
| 483 | * Physical Endpoints 2 maps to Logical Endpoint 2 and |
| 484 | * so on. We consider the direction bit as part of the physical |
| 485 | * endpoint number. So USB endpoint 0x81 is 0x03. |
| 486 | */ |
Felipe Balbi | dc1c70a | 2011-09-30 10:58:51 +0300 | [diff] [blame] | 487 | params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 488 | |
| 489 | /* |
| 490 | * We must use the lower 16 TX FIFOs even though |
| 491 | * HW might have more |
| 492 | */ |
| 493 | if (dep->direction) |
Felipe Balbi | dc1c70a | 2011-09-30 10:58:51 +0300 | [diff] [blame] | 494 | params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 495 | |
| 496 | if (desc->bInterval) { |
Felipe Balbi | dc1c70a | 2011-09-30 10:58:51 +0300 | [diff] [blame] | 497 | params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 498 | dep->interval = 1 << (desc->bInterval - 1); |
| 499 | } |
| 500 | |
| 501 | return dwc3_send_gadget_ep_cmd(dwc, dep->number, |
| 502 | DWC3_DEPCMD_SETEPCONFIG, ¶ms); |
| 503 | } |
| 504 | |
| 505 | static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep) |
| 506 | { |
| 507 | struct dwc3_gadget_ep_cmd_params params; |
| 508 | |
| 509 | memset(¶ms, 0x00, sizeof(params)); |
| 510 | |
Felipe Balbi | dc1c70a | 2011-09-30 10:58:51 +0300 | [diff] [blame] | 511 | params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 512 | |
| 513 | return dwc3_send_gadget_ep_cmd(dwc, dep->number, |
| 514 | DWC3_DEPCMD_SETTRANSFRESOURCE, ¶ms); |
| 515 | } |
| 516 | |
| 517 | /** |
| 518 | * __dwc3_gadget_ep_enable - Initializes a HW endpoint |
| 519 | * @dep: endpoint to be initialized |
| 520 | * @desc: USB Endpoint Descriptor |
| 521 | * |
| 522 | * Caller should take care of locking |
| 523 | */ |
| 524 | static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep, |
Felipe Balbi | c90bfae | 2011-11-29 13:11:21 +0200 | [diff] [blame] | 525 | const struct usb_endpoint_descriptor *desc, |
Felipe Balbi | 07e0ee8 | 2012-07-16 14:08:16 +0300 | [diff] [blame] | 526 | const struct usb_ss_ep_comp_descriptor *comp_desc, |
| 527 | bool ignore) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 528 | { |
| 529 | struct dwc3 *dwc = dep->dwc; |
| 530 | u32 reg; |
| 531 | int ret = -ENOMEM; |
| 532 | |
| 533 | if (!(dep->flags & DWC3_EP_ENABLED)) { |
| 534 | ret = dwc3_gadget_start_config(dwc, dep); |
| 535 | if (ret) |
| 536 | return ret; |
| 537 | } |
| 538 | |
Felipe Balbi | 07e0ee8 | 2012-07-16 14:08:16 +0300 | [diff] [blame] | 539 | ret = dwc3_gadget_set_ep_config(dwc, dep, desc, comp_desc, ignore); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 540 | if (ret) |
| 541 | return ret; |
| 542 | |
| 543 | if (!(dep->flags & DWC3_EP_ENABLED)) { |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 544 | struct dwc3_trb *trb_st_hw; |
| 545 | struct dwc3_trb *trb_link; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 546 | |
| 547 | ret = dwc3_gadget_set_xfer_resource(dwc, dep); |
| 548 | if (ret) |
| 549 | return ret; |
| 550 | |
Ido Shayevitz | 57cdac1 | 2012-03-12 20:25:24 +0200 | [diff] [blame] | 551 | dep->endpoint.desc = desc; |
Felipe Balbi | c90bfae | 2011-11-29 13:11:21 +0200 | [diff] [blame] | 552 | dep->comp_desc = comp_desc; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 553 | dep->type = usb_endpoint_type(desc); |
| 554 | dep->flags |= DWC3_EP_ENABLED; |
| 555 | |
| 556 | reg = dwc3_readl(dwc->regs, DWC3_DALEPENA); |
| 557 | reg |= DWC3_DALEPENA_EP(dep->number); |
| 558 | dwc3_writel(dwc->regs, DWC3_DALEPENA, reg); |
| 559 | |
| 560 | if (!usb_endpoint_xfer_isoc(desc)) |
| 561 | return 0; |
| 562 | |
| 563 | memset(&trb_link, 0, sizeof(trb_link)); |
| 564 | |
Paul Zimmerman | 1d04679 | 2012-02-15 18:56:56 -0800 | [diff] [blame] | 565 | /* Link TRB for ISOC. The HWO bit is never reset */ |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 566 | trb_st_hw = &dep->trb_pool[0]; |
| 567 | |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 568 | trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1]; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 569 | |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 570 | trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw)); |
| 571 | trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw)); |
| 572 | trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB; |
| 573 | trb_link->ctrl |= DWC3_TRB_CTRL_HWO; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | return 0; |
| 577 | } |
| 578 | |
Sebastian Andrzej Siewior | 624407f | 2011-08-29 13:56:37 +0200 | [diff] [blame] | 579 | static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum); |
| 580 | static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 581 | { |
| 582 | struct dwc3_request *req; |
| 583 | |
Felipe Balbi | b129eb7 | 2012-02-17 12:10:04 +0200 | [diff] [blame] | 584 | if (!list_empty(&dep->req_queued)) { |
Sebastian Andrzej Siewior | 624407f | 2011-08-29 13:56:37 +0200 | [diff] [blame] | 585 | dwc3_stop_active_transfer(dwc, dep->number); |
| 586 | |
Pratyush Anand | e67fdeb | 2012-07-06 15:19:10 +0530 | [diff] [blame] | 587 | /* - giveback all requests to gadget driver */ |
Pratyush Anand | 110ff60 | 2012-06-15 11:54:36 +0530 | [diff] [blame] | 588 | while (!list_empty(&dep->req_queued)) { |
| 589 | req = next_request(&dep->req_queued); |
| 590 | |
| 591 | dwc3_gadget_giveback(dep, req, -ESHUTDOWN); |
| 592 | } |
Felipe Balbi | b129eb7 | 2012-02-17 12:10:04 +0200 | [diff] [blame] | 593 | } |
| 594 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 595 | while (!list_empty(&dep->request_list)) { |
| 596 | req = next_request(&dep->request_list); |
| 597 | |
Sebastian Andrzej Siewior | 624407f | 2011-08-29 13:56:37 +0200 | [diff] [blame] | 598 | dwc3_gadget_giveback(dep, req, -ESHUTDOWN); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 599 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | /** |
| 603 | * __dwc3_gadget_ep_disable - Disables a HW endpoint |
| 604 | * @dep: the endpoint to disable |
| 605 | * |
Sebastian Andrzej Siewior | 624407f | 2011-08-29 13:56:37 +0200 | [diff] [blame] | 606 | * This function also removes requests which are currently processed ny the |
| 607 | * hardware and those which are not yet scheduled. |
| 608 | * Caller should take care of locking. |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 609 | */ |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 610 | static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep) |
| 611 | { |
| 612 | struct dwc3 *dwc = dep->dwc; |
| 613 | u32 reg; |
| 614 | |
Sebastian Andrzej Siewior | 624407f | 2011-08-29 13:56:37 +0200 | [diff] [blame] | 615 | dwc3_remove_requests(dwc, dep); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 616 | |
| 617 | reg = dwc3_readl(dwc->regs, DWC3_DALEPENA); |
| 618 | reg &= ~DWC3_DALEPENA_EP(dep->number); |
| 619 | dwc3_writel(dwc->regs, DWC3_DALEPENA, reg); |
| 620 | |
Felipe Balbi | 879631a | 2011-09-30 10:58:47 +0300 | [diff] [blame] | 621 | dep->stream_capable = false; |
Ido Shayevitz | f9c56cd | 2012-02-08 13:56:48 +0200 | [diff] [blame] | 622 | dep->endpoint.desc = NULL; |
Felipe Balbi | c90bfae | 2011-11-29 13:11:21 +0200 | [diff] [blame] | 623 | dep->comp_desc = NULL; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 624 | dep->type = 0; |
Felipe Balbi | 879631a | 2011-09-30 10:58:47 +0300 | [diff] [blame] | 625 | dep->flags = 0; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 626 | |
| 627 | return 0; |
| 628 | } |
| 629 | |
| 630 | /* -------------------------------------------------------------------------- */ |
| 631 | |
| 632 | static int dwc3_gadget_ep0_enable(struct usb_ep *ep, |
| 633 | const struct usb_endpoint_descriptor *desc) |
| 634 | { |
| 635 | return -EINVAL; |
| 636 | } |
| 637 | |
| 638 | static int dwc3_gadget_ep0_disable(struct usb_ep *ep) |
| 639 | { |
| 640 | return -EINVAL; |
| 641 | } |
| 642 | |
| 643 | /* -------------------------------------------------------------------------- */ |
| 644 | |
| 645 | static int dwc3_gadget_ep_enable(struct usb_ep *ep, |
| 646 | const struct usb_endpoint_descriptor *desc) |
| 647 | { |
| 648 | struct dwc3_ep *dep; |
| 649 | struct dwc3 *dwc; |
| 650 | unsigned long flags; |
| 651 | int ret; |
| 652 | |
| 653 | if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) { |
| 654 | pr_debug("dwc3: invalid parameters\n"); |
| 655 | return -EINVAL; |
| 656 | } |
| 657 | |
| 658 | if (!desc->wMaxPacketSize) { |
| 659 | pr_debug("dwc3: missing wMaxPacketSize\n"); |
| 660 | return -EINVAL; |
| 661 | } |
| 662 | |
| 663 | dep = to_dwc3_ep(ep); |
| 664 | dwc = dep->dwc; |
| 665 | |
Felipe Balbi | 1439507 | 2012-08-15 12:28:29 +0300 | [diff] [blame] | 666 | if (dep->flags & DWC3_EP_ENABLED) { |
| 667 | dev_WARN_ONCE(dwc->dev, true, "%s is already enabled\n", |
| 668 | dep->name); |
| 669 | return 0; |
| 670 | } |
| 671 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 672 | switch (usb_endpoint_type(desc)) { |
| 673 | case USB_ENDPOINT_XFER_CONTROL: |
Anton Tikhomirov | 27a78d6 | 2012-02-23 15:38:46 +0900 | [diff] [blame] | 674 | strlcat(dep->name, "-control", sizeof(dep->name)); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 675 | break; |
| 676 | case USB_ENDPOINT_XFER_ISOC: |
Anton Tikhomirov | 27a78d6 | 2012-02-23 15:38:46 +0900 | [diff] [blame] | 677 | strlcat(dep->name, "-isoc", sizeof(dep->name)); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 678 | break; |
| 679 | case USB_ENDPOINT_XFER_BULK: |
Anton Tikhomirov | 27a78d6 | 2012-02-23 15:38:46 +0900 | [diff] [blame] | 680 | strlcat(dep->name, "-bulk", sizeof(dep->name)); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 681 | break; |
| 682 | case USB_ENDPOINT_XFER_INT: |
Anton Tikhomirov | 27a78d6 | 2012-02-23 15:38:46 +0900 | [diff] [blame] | 683 | strlcat(dep->name, "-int", sizeof(dep->name)); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 684 | break; |
| 685 | default: |
| 686 | dev_err(dwc->dev, "invalid endpoint transfer type\n"); |
| 687 | } |
| 688 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 689 | dev_vdbg(dwc->dev, "Enabling %s\n", dep->name); |
| 690 | |
| 691 | spin_lock_irqsave(&dwc->lock, flags); |
Felipe Balbi | 07e0ee8 | 2012-07-16 14:08:16 +0300 | [diff] [blame] | 692 | ret = __dwc3_gadget_ep_enable(dep, desc, ep->comp_desc, false); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 693 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 694 | |
| 695 | return ret; |
| 696 | } |
| 697 | |
| 698 | static int dwc3_gadget_ep_disable(struct usb_ep *ep) |
| 699 | { |
| 700 | struct dwc3_ep *dep; |
| 701 | struct dwc3 *dwc; |
| 702 | unsigned long flags; |
| 703 | int ret; |
| 704 | |
| 705 | if (!ep) { |
| 706 | pr_debug("dwc3: invalid parameters\n"); |
| 707 | return -EINVAL; |
| 708 | } |
| 709 | |
| 710 | dep = to_dwc3_ep(ep); |
| 711 | dwc = dep->dwc; |
| 712 | |
| 713 | if (!(dep->flags & DWC3_EP_ENABLED)) { |
| 714 | dev_WARN_ONCE(dwc->dev, true, "%s is already disabled\n", |
| 715 | dep->name); |
| 716 | return 0; |
| 717 | } |
| 718 | |
| 719 | snprintf(dep->name, sizeof(dep->name), "ep%d%s", |
| 720 | dep->number >> 1, |
| 721 | (dep->number & 1) ? "in" : "out"); |
| 722 | |
| 723 | spin_lock_irqsave(&dwc->lock, flags); |
| 724 | ret = __dwc3_gadget_ep_disable(dep); |
| 725 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 726 | |
| 727 | return ret; |
| 728 | } |
| 729 | |
| 730 | static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep, |
| 731 | gfp_t gfp_flags) |
| 732 | { |
| 733 | struct dwc3_request *req; |
| 734 | struct dwc3_ep *dep = to_dwc3_ep(ep); |
| 735 | struct dwc3 *dwc = dep->dwc; |
| 736 | |
| 737 | req = kzalloc(sizeof(*req), gfp_flags); |
| 738 | if (!req) { |
| 739 | dev_err(dwc->dev, "not enough memory\n"); |
| 740 | return NULL; |
| 741 | } |
| 742 | |
| 743 | req->epnum = dep->number; |
| 744 | req->dep = dep; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 745 | |
| 746 | return &req->request; |
| 747 | } |
| 748 | |
| 749 | static void dwc3_gadget_ep_free_request(struct usb_ep *ep, |
| 750 | struct usb_request *request) |
| 751 | { |
| 752 | struct dwc3_request *req = to_dwc3_request(request); |
| 753 | |
| 754 | kfree(req); |
| 755 | } |
| 756 | |
Felipe Balbi | c71fc37 | 2011-11-22 11:37:34 +0200 | [diff] [blame] | 757 | /** |
| 758 | * dwc3_prepare_one_trb - setup one TRB from one request |
| 759 | * @dep: endpoint for which this request is prepared |
| 760 | * @req: dwc3_request pointer |
| 761 | */ |
Felipe Balbi | 68e823e | 2011-11-28 12:25:01 +0200 | [diff] [blame] | 762 | static void dwc3_prepare_one_trb(struct dwc3_ep *dep, |
Felipe Balbi | eeb720f | 2011-11-28 12:46:59 +0200 | [diff] [blame] | 763 | struct dwc3_request *req, dma_addr_t dma, |
| 764 | unsigned length, unsigned last, unsigned chain) |
Felipe Balbi | c71fc37 | 2011-11-22 11:37:34 +0200 | [diff] [blame] | 765 | { |
Felipe Balbi | eeb720f | 2011-11-28 12:46:59 +0200 | [diff] [blame] | 766 | struct dwc3 *dwc = dep->dwc; |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 767 | struct dwc3_trb *trb; |
Felipe Balbi | c71fc37 | 2011-11-22 11:37:34 +0200 | [diff] [blame] | 768 | |
| 769 | unsigned int cur_slot; |
| 770 | |
Felipe Balbi | eeb720f | 2011-11-28 12:46:59 +0200 | [diff] [blame] | 771 | dev_vdbg(dwc->dev, "%s: req %p dma %08llx length %d%s%s\n", |
| 772 | dep->name, req, (unsigned long long) dma, |
| 773 | length, last ? " last" : "", |
| 774 | chain ? " chain" : ""); |
| 775 | |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 776 | trb = &dep->trb_pool[dep->free_slot & DWC3_TRB_MASK]; |
Felipe Balbi | c71fc37 | 2011-11-22 11:37:34 +0200 | [diff] [blame] | 777 | cur_slot = dep->free_slot; |
| 778 | dep->free_slot++; |
| 779 | |
| 780 | /* Skip the LINK-TRB on ISOC */ |
Vijayavardhan Vennapusa | 2a444ad | 2013-02-01 13:20:59 +0530 | [diff] [blame] | 781 | if (((dep->free_slot & DWC3_TRB_MASK) == DWC3_TRB_NUM - 1) && |
Ido Shayevitz | 57cdac1 | 2012-03-12 20:25:24 +0200 | [diff] [blame] | 782 | usb_endpoint_xfer_isoc(dep->endpoint.desc)) |
Vijayavardhan Vennapusa | 2a444ad | 2013-02-01 13:20:59 +0530 | [diff] [blame] | 783 | dep->free_slot++; |
Felipe Balbi | c71fc37 | 2011-11-22 11:37:34 +0200 | [diff] [blame] | 784 | |
Felipe Balbi | eeb720f | 2011-11-28 12:46:59 +0200 | [diff] [blame] | 785 | if (!req->trb) { |
| 786 | dwc3_gadget_move_request_queued(req); |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 787 | req->trb = trb; |
| 788 | req->trb_dma = dwc3_trb_dma_offset(dep, trb); |
Felipe Balbi | eeb720f | 2011-11-28 12:46:59 +0200 | [diff] [blame] | 789 | } |
Felipe Balbi | c71fc37 | 2011-11-22 11:37:34 +0200 | [diff] [blame] | 790 | |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 791 | trb->size = DWC3_TRB_SIZE_LENGTH(length); |
| 792 | trb->bpl = lower_32_bits(dma); |
| 793 | trb->bph = upper_32_bits(dma); |
Felipe Balbi | c71fc37 | 2011-11-22 11:37:34 +0200 | [diff] [blame] | 794 | |
Ido Shayevitz | 57cdac1 | 2012-03-12 20:25:24 +0200 | [diff] [blame] | 795 | switch (usb_endpoint_type(dep->endpoint.desc)) { |
Felipe Balbi | c71fc37 | 2011-11-22 11:37:34 +0200 | [diff] [blame] | 796 | case USB_ENDPOINT_XFER_CONTROL: |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 797 | trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP; |
Felipe Balbi | c71fc37 | 2011-11-22 11:37:34 +0200 | [diff] [blame] | 798 | break; |
| 799 | |
| 800 | case USB_ENDPOINT_XFER_ISOC: |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 801 | trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST; |
Felipe Balbi | c71fc37 | 2011-11-22 11:37:34 +0200 | [diff] [blame] | 802 | |
Pratyush Anand | df02342 | 2012-05-21 12:42:54 +0530 | [diff] [blame] | 803 | if (!req->request.no_interrupt) |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 804 | trb->ctrl |= DWC3_TRB_CTRL_IOC; |
Felipe Balbi | c71fc37 | 2011-11-22 11:37:34 +0200 | [diff] [blame] | 805 | break; |
| 806 | |
| 807 | case USB_ENDPOINT_XFER_BULK: |
| 808 | case USB_ENDPOINT_XFER_INT: |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 809 | trb->ctrl = DWC3_TRBCTL_NORMAL; |
Felipe Balbi | c71fc37 | 2011-11-22 11:37:34 +0200 | [diff] [blame] | 810 | break; |
| 811 | default: |
| 812 | /* |
| 813 | * This is only possible with faulty memory because we |
| 814 | * checked it already :) |
| 815 | */ |
| 816 | BUG(); |
| 817 | } |
| 818 | |
Ido Shayevitz | 57cdac1 | 2012-03-12 20:25:24 +0200 | [diff] [blame] | 819 | if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) { |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 820 | trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI; |
| 821 | trb->ctrl |= DWC3_TRB_CTRL_CSP; |
| 822 | } else { |
| 823 | if (chain) |
| 824 | trb->ctrl |= DWC3_TRB_CTRL_CHN; |
Felipe Balbi | c71fc37 | 2011-11-22 11:37:34 +0200 | [diff] [blame] | 825 | |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 826 | if (last) |
| 827 | trb->ctrl |= DWC3_TRB_CTRL_LST; |
| 828 | } |
| 829 | |
Ido Shayevitz | 57cdac1 | 2012-03-12 20:25:24 +0200 | [diff] [blame] | 830 | if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable) |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 831 | trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(req->request.stream_id); |
| 832 | |
| 833 | trb->ctrl |= DWC3_TRB_CTRL_HWO; |
Felipe Balbi | c71fc37 | 2011-11-22 11:37:34 +0200 | [diff] [blame] | 834 | } |
| 835 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 836 | /* |
| 837 | * dwc3_prepare_trbs - setup TRBs from requests |
| 838 | * @dep: endpoint for which requests are being prepared |
| 839 | * @starting: true if the endpoint is idle and no requests are queued. |
| 840 | * |
Paul Zimmerman | 1d04679 | 2012-02-15 18:56:56 -0800 | [diff] [blame] | 841 | * The function goes through the requests list and sets up TRBs for the |
| 842 | * transfers. The function returns once there are no more TRBs available or |
| 843 | * it runs out of requests. |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 844 | */ |
Felipe Balbi | 68e823e | 2011-11-28 12:25:01 +0200 | [diff] [blame] | 845 | static void dwc3_prepare_trbs(struct dwc3_ep *dep, bool starting) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 846 | { |
Felipe Balbi | 68e823e | 2011-11-28 12:25:01 +0200 | [diff] [blame] | 847 | struct dwc3_request *req, *n; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 848 | u32 trbs_left; |
Paul Zimmerman | 8d62cd6 | 2012-02-15 13:35:06 +0200 | [diff] [blame] | 849 | u32 max; |
Felipe Balbi | c71fc37 | 2011-11-22 11:37:34 +0200 | [diff] [blame] | 850 | unsigned int last_one = 0; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 851 | |
| 852 | BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM); |
| 853 | |
| 854 | /* the first request must not be queued */ |
| 855 | trbs_left = (dep->busy_slot - dep->free_slot) & DWC3_TRB_MASK; |
Felipe Balbi | c71fc37 | 2011-11-22 11:37:34 +0200 | [diff] [blame] | 856 | |
Paul Zimmerman | 8d62cd6 | 2012-02-15 13:35:06 +0200 | [diff] [blame] | 857 | /* Can't wrap around on a non-isoc EP since there's no link TRB */ |
Ido Shayevitz | 57cdac1 | 2012-03-12 20:25:24 +0200 | [diff] [blame] | 858 | if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) { |
Paul Zimmerman | 8d62cd6 | 2012-02-15 13:35:06 +0200 | [diff] [blame] | 859 | max = DWC3_TRB_NUM - (dep->free_slot & DWC3_TRB_MASK); |
| 860 | if (trbs_left > max) |
| 861 | trbs_left = max; |
| 862 | } |
| 863 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 864 | /* |
Paul Zimmerman | 1d04679 | 2012-02-15 18:56:56 -0800 | [diff] [blame] | 865 | * If busy & slot are equal than it is either full or empty. If we are |
| 866 | * starting to process requests then we are empty. Otherwise we are |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 867 | * full and don't do anything |
| 868 | */ |
| 869 | if (!trbs_left) { |
| 870 | if (!starting) |
Felipe Balbi | 68e823e | 2011-11-28 12:25:01 +0200 | [diff] [blame] | 871 | return; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 872 | trbs_left = DWC3_TRB_NUM; |
| 873 | /* |
| 874 | * In case we start from scratch, we queue the ISOC requests |
| 875 | * starting from slot 1. This is done because we use ring |
| 876 | * buffer and have no LST bit to stop us. Instead, we place |
Paul Zimmerman | 1d04679 | 2012-02-15 18:56:56 -0800 | [diff] [blame] | 877 | * IOC bit every TRB_NUM/4. We try to avoid having an interrupt |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 878 | * after the first request so we start at slot 1 and have |
| 879 | * 7 requests proceed before we hit the first IOC. |
| 880 | * Other transfer types don't use the ring buffer and are |
| 881 | * processed from the first TRB until the last one. Since we |
| 882 | * don't wrap around we have to start at the beginning. |
| 883 | */ |
Ido Shayevitz | 57cdac1 | 2012-03-12 20:25:24 +0200 | [diff] [blame] | 884 | if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 885 | dep->busy_slot = 1; |
| 886 | dep->free_slot = 1; |
| 887 | } else { |
| 888 | dep->busy_slot = 0; |
| 889 | dep->free_slot = 0; |
| 890 | } |
| 891 | } |
| 892 | |
| 893 | /* The last TRB is a link TRB, not used for xfer */ |
Ido Shayevitz | 57cdac1 | 2012-03-12 20:25:24 +0200 | [diff] [blame] | 894 | if ((trbs_left <= 1) && usb_endpoint_xfer_isoc(dep->endpoint.desc)) |
Felipe Balbi | 68e823e | 2011-11-28 12:25:01 +0200 | [diff] [blame] | 895 | return; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 896 | |
| 897 | list_for_each_entry_safe(req, n, &dep->request_list, list) { |
Felipe Balbi | eeb720f | 2011-11-28 12:46:59 +0200 | [diff] [blame] | 898 | unsigned length; |
| 899 | dma_addr_t dma; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 900 | |
Felipe Balbi | eeb720f | 2011-11-28 12:46:59 +0200 | [diff] [blame] | 901 | if (req->request.num_mapped_sgs > 0) { |
| 902 | struct usb_request *request = &req->request; |
| 903 | struct scatterlist *sg = request->sg; |
| 904 | struct scatterlist *s; |
| 905 | int i; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 906 | |
Felipe Balbi | eeb720f | 2011-11-28 12:46:59 +0200 | [diff] [blame] | 907 | for_each_sg(sg, s, request->num_mapped_sgs, i) { |
| 908 | unsigned chain = true; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 909 | |
Felipe Balbi | eeb720f | 2011-11-28 12:46:59 +0200 | [diff] [blame] | 910 | length = sg_dma_len(s); |
| 911 | dma = sg_dma_address(s); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 912 | |
Paul Zimmerman | 1d04679 | 2012-02-15 18:56:56 -0800 | [diff] [blame] | 913 | if (i == (request->num_mapped_sgs - 1) || |
| 914 | sg_is_last(s)) { |
Felipe Balbi | eeb720f | 2011-11-28 12:46:59 +0200 | [diff] [blame] | 915 | last_one = true; |
| 916 | chain = false; |
| 917 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 918 | |
Felipe Balbi | eeb720f | 2011-11-28 12:46:59 +0200 | [diff] [blame] | 919 | trbs_left--; |
| 920 | if (!trbs_left) |
| 921 | last_one = true; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 922 | |
Felipe Balbi | eeb720f | 2011-11-28 12:46:59 +0200 | [diff] [blame] | 923 | if (last_one) |
| 924 | chain = false; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 925 | |
Felipe Balbi | eeb720f | 2011-11-28 12:46:59 +0200 | [diff] [blame] | 926 | dwc3_prepare_one_trb(dep, req, dma, length, |
| 927 | last_one, chain); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 928 | |
Felipe Balbi | eeb720f | 2011-11-28 12:46:59 +0200 | [diff] [blame] | 929 | if (last_one) |
| 930 | break; |
| 931 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 932 | } else { |
Felipe Balbi | eeb720f | 2011-11-28 12:46:59 +0200 | [diff] [blame] | 933 | dma = req->request.dma; |
| 934 | length = req->request.length; |
| 935 | trbs_left--; |
| 936 | |
| 937 | if (!trbs_left) |
| 938 | last_one = 1; |
| 939 | |
| 940 | /* Is this the last request? */ |
| 941 | if (list_is_last(&req->list, &dep->request_list)) |
| 942 | last_one = 1; |
| 943 | |
| 944 | dwc3_prepare_one_trb(dep, req, dma, length, |
| 945 | last_one, false); |
| 946 | |
| 947 | if (last_one) |
| 948 | break; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 949 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 950 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 951 | } |
| 952 | |
| 953 | static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep, u16 cmd_param, |
| 954 | int start_new) |
| 955 | { |
| 956 | struct dwc3_gadget_ep_cmd_params params; |
Vijayavardhan Vennapusa | 91ba653 | 2013-01-30 17:35:45 +0530 | [diff] [blame] | 957 | struct dwc3_request *req, *req1, *n; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 958 | struct dwc3 *dwc = dep->dwc; |
| 959 | int ret; |
| 960 | u32 cmd; |
| 961 | |
| 962 | if (start_new && (dep->flags & DWC3_EP_BUSY)) { |
| 963 | dev_vdbg(dwc->dev, "%s: endpoint busy\n", dep->name); |
| 964 | return -EBUSY; |
| 965 | } |
| 966 | dep->flags &= ~DWC3_EP_PENDING_REQUEST; |
| 967 | |
| 968 | /* |
| 969 | * If we are getting here after a short-out-packet we don't enqueue any |
| 970 | * new requests as we try to set the IOC bit only on the last request. |
| 971 | */ |
| 972 | if (start_new) { |
| 973 | if (list_empty(&dep->req_queued)) |
| 974 | dwc3_prepare_trbs(dep, start_new); |
| 975 | |
| 976 | /* req points to the first request which will be sent */ |
| 977 | req = next_request(&dep->req_queued); |
| 978 | } else { |
Felipe Balbi | 68e823e | 2011-11-28 12:25:01 +0200 | [diff] [blame] | 979 | dwc3_prepare_trbs(dep, start_new); |
| 980 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 981 | /* |
Paul Zimmerman | 1d04679 | 2012-02-15 18:56:56 -0800 | [diff] [blame] | 982 | * req points to the first request where HWO changed from 0 to 1 |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 983 | */ |
Felipe Balbi | 68e823e | 2011-11-28 12:25:01 +0200 | [diff] [blame] | 984 | req = next_request(&dep->req_queued); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 985 | } |
| 986 | if (!req) { |
| 987 | dep->flags |= DWC3_EP_PENDING_REQUEST; |
| 988 | return 0; |
| 989 | } |
| 990 | |
| 991 | memset(¶ms, 0, sizeof(params)); |
Felipe Balbi | dc1c70a | 2011-09-30 10:58:51 +0300 | [diff] [blame] | 992 | params.param0 = upper_32_bits(req->trb_dma); |
| 993 | params.param1 = lower_32_bits(req->trb_dma); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 994 | |
| 995 | if (start_new) |
| 996 | cmd = DWC3_DEPCMD_STARTTRANSFER; |
| 997 | else |
| 998 | cmd = DWC3_DEPCMD_UPDATETRANSFER; |
| 999 | |
| 1000 | cmd |= DWC3_DEPCMD_PARAM(cmd_param); |
| 1001 | ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, ¶ms); |
| 1002 | if (ret < 0) { |
| 1003 | dev_dbg(dwc->dev, "failed to send STARTTRANSFER command\n"); |
| 1004 | |
Vijayavardhan Vennapusa | 91ba653 | 2013-01-30 17:35:45 +0530 | [diff] [blame] | 1005 | if ((ret == -EAGAIN) && start_new && |
| 1006 | usb_endpoint_xfer_isoc(dep->endpoint.desc)) { |
| 1007 | /* If bit13 in Command complete event is set, software |
| 1008 | * must issue ENDTRANDFER command and wait for |
| 1009 | * Xfernotready event to queue the requests again. |
| 1010 | */ |
| 1011 | if (!dep->resource_index) { |
| 1012 | dep->resource_index = |
| 1013 | dwc3_gadget_ep_get_transfer_index(dwc, |
| 1014 | dep->number); |
| 1015 | WARN_ON_ONCE(!dep->resource_index); |
| 1016 | } |
| 1017 | dwc3_stop_active_transfer(dwc, dep->number); |
| 1018 | list_for_each_entry_safe_reverse(req1, n, |
| 1019 | &dep->req_queued, list) { |
| 1020 | req1->trb = NULL; |
| 1021 | dwc3_gadget_move_request_list_front(req1); |
| 1022 | if (req->request.num_mapped_sgs) |
| 1023 | dep->busy_slot += |
| 1024 | req->request.num_mapped_sgs; |
| 1025 | else |
| 1026 | dep->busy_slot++; |
| 1027 | if ((dep->busy_slot & DWC3_TRB_MASK) == |
| 1028 | DWC3_TRB_NUM - 1) |
| 1029 | dep->busy_slot++; |
| 1030 | } |
| 1031 | return ret; |
| 1032 | } else { |
| 1033 | /* |
| 1034 | * FIXME we need to iterate over the list of requests |
| 1035 | * here and stop, unmap, free and del each of the linked |
| 1036 | * requests instead of what we do now. |
| 1037 | */ |
| 1038 | usb_gadget_unmap_request(&dwc->gadget, &req->request, |
Felipe Balbi | 0fc9a1b | 2011-12-19 11:32:34 +0200 | [diff] [blame] | 1039 | req->direction); |
Vijayavardhan Vennapusa | 91ba653 | 2013-01-30 17:35:45 +0530 | [diff] [blame] | 1040 | list_del(&req->list); |
| 1041 | return ret; |
| 1042 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1043 | } |
| 1044 | |
| 1045 | dep->flags |= DWC3_EP_BUSY; |
Felipe Balbi | 25b8ff6 | 2011-11-04 12:32:47 +0200 | [diff] [blame] | 1046 | |
Paul Zimmerman | f39a37f | 2012-03-29 18:16:54 +0000 | [diff] [blame] | 1047 | if (start_new) { |
Felipe Balbi | 4959cfc | 2012-06-06 12:04:13 +0300 | [diff] [blame] | 1048 | dep->resource_index = dwc3_gadget_ep_get_transfer_index(dwc, |
Paul Zimmerman | f39a37f | 2012-03-29 18:16:54 +0000 | [diff] [blame] | 1049 | dep->number); |
Felipe Balbi | 4959cfc | 2012-06-06 12:04:13 +0300 | [diff] [blame] | 1050 | WARN_ON_ONCE(!dep->resource_index); |
Paul Zimmerman | f39a37f | 2012-03-29 18:16:54 +0000 | [diff] [blame] | 1051 | } |
Felipe Balbi | 25b8ff6 | 2011-11-04 12:32:47 +0200 | [diff] [blame] | 1052 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1053 | return 0; |
| 1054 | } |
| 1055 | |
Pratyush Anand | 73939b0 | 2012-05-25 18:54:56 +0530 | [diff] [blame] | 1056 | static void __dwc3_gadget_start_isoc(struct dwc3 *dwc, |
| 1057 | struct dwc3_ep *dep, u32 cur_uf) |
| 1058 | { |
| 1059 | u32 uf; |
| 1060 | |
Vijayavardhan Vennapusa | 91ba653 | 2013-01-30 17:35:45 +0530 | [diff] [blame] | 1061 | dep->current_uf = cur_uf; |
| 1062 | |
Pratyush Anand | 73939b0 | 2012-05-25 18:54:56 +0530 | [diff] [blame] | 1063 | if (list_empty(&dep->request_list)) { |
| 1064 | dev_vdbg(dwc->dev, "ISOC ep %s run out for requests.\n", |
| 1065 | dep->name); |
Pratyush Anand | ac41760 | 2012-08-30 12:21:43 +0530 | [diff] [blame] | 1066 | dep->flags |= DWC3_EP_PENDING_REQUEST; |
Pratyush Anand | 73939b0 | 2012-05-25 18:54:56 +0530 | [diff] [blame] | 1067 | return; |
| 1068 | } |
| 1069 | |
| 1070 | /* 4 micro frames in the future */ |
| 1071 | uf = cur_uf + dep->interval * 4; |
| 1072 | |
| 1073 | __dwc3_gadget_kick_transfer(dep, uf, 1); |
| 1074 | } |
| 1075 | |
| 1076 | static void dwc3_gadget_start_isoc(struct dwc3 *dwc, |
| 1077 | struct dwc3_ep *dep, const struct dwc3_event_depevt *event) |
| 1078 | { |
| 1079 | u32 cur_uf, mask; |
| 1080 | |
| 1081 | mask = ~(dep->interval - 1); |
| 1082 | cur_uf = event->parameters & mask; |
| 1083 | |
| 1084 | __dwc3_gadget_start_isoc(dwc, dep, cur_uf); |
| 1085 | } |
| 1086 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1087 | static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req) |
| 1088 | { |
Felipe Balbi | 0fc9a1b | 2011-12-19 11:32:34 +0200 | [diff] [blame] | 1089 | struct dwc3 *dwc = dep->dwc; |
| 1090 | int ret; |
| 1091 | |
Manu Gautam | d2b99e1 | 2013-02-11 15:53:34 +0530 | [diff] [blame] | 1092 | if (req->request.status == -EINPROGRESS) { |
| 1093 | ret = -EBUSY; |
| 1094 | dev_err(dwc->dev, "%s: %p request already in queue", |
| 1095 | dep->name, req); |
| 1096 | return ret; |
| 1097 | } |
| 1098 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1099 | req->request.actual = 0; |
| 1100 | req->request.status = -EINPROGRESS; |
| 1101 | req->direction = dep->direction; |
| 1102 | req->epnum = dep->number; |
| 1103 | |
| 1104 | /* |
| 1105 | * We only add to our list of requests now and |
| 1106 | * start consuming the list once we get XferNotReady |
| 1107 | * IRQ. |
| 1108 | * |
| 1109 | * That way, we avoid doing anything that we don't need |
| 1110 | * to do now and defer it until the point we receive a |
| 1111 | * particular token from the Host side. |
| 1112 | * |
| 1113 | * This will also avoid Host cancelling URBs due to too |
Paul Zimmerman | 1d04679 | 2012-02-15 18:56:56 -0800 | [diff] [blame] | 1114 | * many NAKs. |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1115 | */ |
Felipe Balbi | 0fc9a1b | 2011-12-19 11:32:34 +0200 | [diff] [blame] | 1116 | ret = usb_gadget_map_request(&dwc->gadget, &req->request, |
| 1117 | dep->direction); |
| 1118 | if (ret) |
| 1119 | return ret; |
| 1120 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1121 | list_add_tail(&req->list, &dep->request_list); |
| 1122 | |
| 1123 | /* |
Felipe Balbi | 46485a0 | 2012-06-06 12:00:50 +0300 | [diff] [blame] | 1124 | * There are a few special cases: |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1125 | * |
Paul Zimmerman | f39a37f | 2012-03-29 18:16:54 +0000 | [diff] [blame] | 1126 | * 1. XferNotReady with empty list of requests. We need to kick the |
| 1127 | * transfer here in that situation, otherwise we will be NAKing |
| 1128 | * forever. If we get XferNotReady before gadget driver has a |
| 1129 | * chance to queue a request, we will ACK the IRQ but won't be |
| 1130 | * able to receive the data until the next request is queued. |
| 1131 | * The following code is handling exactly that. |
| 1132 | * |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1133 | */ |
| 1134 | if (dep->flags & DWC3_EP_PENDING_REQUEST) { |
Pratyush Anand | ac41760 | 2012-08-30 12:21:43 +0530 | [diff] [blame] | 1135 | int ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1136 | |
Pratyush Anand | ac41760 | 2012-08-30 12:21:43 +0530 | [diff] [blame] | 1137 | /* |
| 1138 | * If xfernotready is already elapsed and it is a case |
| 1139 | * of isoc transfer, then issue END TRANSFER, so that |
| 1140 | * you can receive xfernotready again and can have |
| 1141 | * notion of current microframe. |
| 1142 | */ |
| 1143 | if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) { |
Vijayavardhan Vennapusa | 91ba653 | 2013-01-30 17:35:45 +0530 | [diff] [blame] | 1144 | /* If xfernotready event is recieved before issuing |
| 1145 | * START TRANSFER command, don't issue END TRANSFER. |
| 1146 | * Rather start queueing the requests by issuing START |
| 1147 | * TRANSFER command. |
| 1148 | */ |
| 1149 | if (list_empty(&dep->req_queued) && dep->resource_index) |
Pratyush Anand | 18bbcb0 | 2013-01-14 15:59:34 +0530 | [diff] [blame] | 1150 | dwc3_stop_active_transfer(dwc, dep->number); |
Vijayavardhan Vennapusa | 91ba653 | 2013-01-30 17:35:45 +0530 | [diff] [blame] | 1151 | else |
| 1152 | __dwc3_gadget_start_isoc(dwc, dep, |
| 1153 | dep->current_uf); |
| 1154 | dep->flags &= ~DWC3_EP_PENDING_REQUEST; |
Pratyush Anand | ac41760 | 2012-08-30 12:21:43 +0530 | [diff] [blame] | 1155 | return 0; |
| 1156 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1157 | |
Felipe Balbi | 46485a0 | 2012-06-06 12:00:50 +0300 | [diff] [blame] | 1158 | ret = __dwc3_gadget_kick_transfer(dep, 0, true); |
Moiz Sonasath | eed03f1 | 2012-08-01 14:08:30 -0500 | [diff] [blame] | 1159 | if (ret && ret != -EBUSY) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1160 | dev_dbg(dwc->dev, "%s: failed to kick transfers\n", |
| 1161 | dep->name); |
Felipe Balbi | 5d409eb | 2012-05-22 10:24:11 +0300 | [diff] [blame] | 1162 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1163 | |
Felipe Balbi | 46485a0 | 2012-06-06 12:00:50 +0300 | [diff] [blame] | 1164 | /* |
| 1165 | * 2. XferInProgress on Isoc EP with an active transfer. We need to |
| 1166 | * kick the transfer here after queuing a request, otherwise the |
| 1167 | * core may not see the modified TRB(s). |
| 1168 | */ |
| 1169 | if (usb_endpoint_xfer_isoc(dep->endpoint.desc) && |
Pratyush Anand | 053d3e5 | 2012-08-07 16:54:18 +0530 | [diff] [blame] | 1170 | (dep->flags & DWC3_EP_BUSY) && |
| 1171 | !(dep->flags & DWC3_EP_MISSED_ISOC)) { |
Felipe Balbi | 4959cfc | 2012-06-06 12:04:13 +0300 | [diff] [blame] | 1172 | WARN_ON_ONCE(!dep->resource_index); |
| 1173 | ret = __dwc3_gadget_kick_transfer(dep, dep->resource_index, |
Felipe Balbi | 46485a0 | 2012-06-06 12:00:50 +0300 | [diff] [blame] | 1174 | false); |
Moiz Sonasath | eed03f1 | 2012-08-01 14:08:30 -0500 | [diff] [blame] | 1175 | if (ret && ret != -EBUSY) |
Felipe Balbi | 46485a0 | 2012-06-06 12:00:50 +0300 | [diff] [blame] | 1176 | dev_dbg(dwc->dev, "%s: failed to kick transfers\n", |
| 1177 | dep->name); |
Felipe Balbi | 46485a0 | 2012-06-06 12:00:50 +0300 | [diff] [blame] | 1178 | } |
| 1179 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1180 | return 0; |
| 1181 | } |
| 1182 | |
| 1183 | static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request, |
| 1184 | gfp_t gfp_flags) |
| 1185 | { |
| 1186 | struct dwc3_request *req = to_dwc3_request(request); |
| 1187 | struct dwc3_ep *dep = to_dwc3_ep(ep); |
| 1188 | struct dwc3 *dwc = dep->dwc; |
| 1189 | |
| 1190 | unsigned long flags; |
| 1191 | |
| 1192 | int ret; |
| 1193 | |
Ido Shayevitz | 57cdac1 | 2012-03-12 20:25:24 +0200 | [diff] [blame] | 1194 | if (!dep->endpoint.desc) { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1195 | dev_dbg(dwc->dev, "trying to queue request %p to disabled %s\n", |
| 1196 | request, ep->name); |
| 1197 | return -ESHUTDOWN; |
| 1198 | } |
| 1199 | |
| 1200 | dev_vdbg(dwc->dev, "queing request %p to %s length %d\n", |
| 1201 | request, ep->name, request->length); |
| 1202 | |
Manu Gautam | 1c4dbcb | 2012-10-05 13:16:00 +0530 | [diff] [blame] | 1203 | WARN(!dep->direction && (request->length % ep->desc->wMaxPacketSize), |
| 1204 | "trying to queue unaligned request (%d)\n", request->length); |
| 1205 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1206 | spin_lock_irqsave(&dwc->lock, flags); |
| 1207 | ret = __dwc3_gadget_ep_queue(dep, req); |
| 1208 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 1209 | |
| 1210 | return ret; |
| 1211 | } |
| 1212 | |
| 1213 | static int dwc3_gadget_ep_dequeue(struct usb_ep *ep, |
| 1214 | struct usb_request *request) |
| 1215 | { |
| 1216 | struct dwc3_request *req = to_dwc3_request(request); |
| 1217 | struct dwc3_request *r = NULL; |
| 1218 | |
| 1219 | struct dwc3_ep *dep = to_dwc3_ep(ep); |
| 1220 | struct dwc3 *dwc = dep->dwc; |
| 1221 | |
| 1222 | unsigned long flags; |
| 1223 | int ret = 0; |
| 1224 | |
| 1225 | spin_lock_irqsave(&dwc->lock, flags); |
| 1226 | |
| 1227 | list_for_each_entry(r, &dep->request_list, list) { |
| 1228 | if (r == req) |
| 1229 | break; |
| 1230 | } |
| 1231 | |
| 1232 | if (r != req) { |
| 1233 | list_for_each_entry(r, &dep->req_queued, list) { |
| 1234 | if (r == req) |
| 1235 | break; |
| 1236 | } |
| 1237 | if (r == req) { |
| 1238 | /* wait until it is processed */ |
| 1239 | dwc3_stop_active_transfer(dwc, dep->number); |
Pratyush Anand | eaec3e9 | 2012-06-15 11:54:00 +0530 | [diff] [blame] | 1240 | goto out1; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1241 | } |
| 1242 | dev_err(dwc->dev, "request %p was not queued to %s\n", |
| 1243 | request, ep->name); |
| 1244 | ret = -EINVAL; |
| 1245 | goto out0; |
| 1246 | } |
| 1247 | |
Pratyush Anand | eaec3e9 | 2012-06-15 11:54:00 +0530 | [diff] [blame] | 1248 | out1: |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1249 | /* giveback the request */ |
| 1250 | dwc3_gadget_giveback(dep, req, -ECONNRESET); |
| 1251 | |
| 1252 | out0: |
| 1253 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 1254 | |
| 1255 | return ret; |
| 1256 | } |
| 1257 | |
| 1258 | int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value) |
| 1259 | { |
| 1260 | struct dwc3_gadget_ep_cmd_params params; |
| 1261 | struct dwc3 *dwc = dep->dwc; |
| 1262 | int ret; |
| 1263 | |
| 1264 | memset(¶ms, 0x00, sizeof(params)); |
| 1265 | |
| 1266 | if (value) { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1267 | ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, |
| 1268 | DWC3_DEPCMD_SETSTALL, ¶ms); |
| 1269 | if (ret) |
| 1270 | dev_err(dwc->dev, "failed to %s STALL on %s\n", |
| 1271 | value ? "set" : "clear", |
| 1272 | dep->name); |
| 1273 | else |
| 1274 | dep->flags |= DWC3_EP_STALL; |
| 1275 | } else { |
| 1276 | ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, |
| 1277 | DWC3_DEPCMD_CLEARSTALL, ¶ms); |
| 1278 | if (ret) |
| 1279 | dev_err(dwc->dev, "failed to %s STALL on %s\n", |
| 1280 | value ? "set" : "clear", |
| 1281 | dep->name); |
| 1282 | else |
Vijayavardhan Vennapusa | 6008e26 | 2012-10-19 15:57:56 +0530 | [diff] [blame] | 1283 | dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1284 | } |
Paul Zimmerman | 5275455 | 2011-09-30 10:58:44 +0300 | [diff] [blame] | 1285 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1286 | return ret; |
| 1287 | } |
| 1288 | |
| 1289 | static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value) |
| 1290 | { |
| 1291 | struct dwc3_ep *dep = to_dwc3_ep(ep); |
| 1292 | struct dwc3 *dwc = dep->dwc; |
| 1293 | |
| 1294 | unsigned long flags; |
| 1295 | |
| 1296 | int ret; |
| 1297 | |
| 1298 | spin_lock_irqsave(&dwc->lock, flags); |
| 1299 | |
Ido Shayevitz | 57cdac1 | 2012-03-12 20:25:24 +0200 | [diff] [blame] | 1300 | if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1301 | dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name); |
| 1302 | ret = -EINVAL; |
| 1303 | goto out; |
| 1304 | } |
| 1305 | |
| 1306 | ret = __dwc3_gadget_ep_set_halt(dep, value); |
| 1307 | out: |
| 1308 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 1309 | |
| 1310 | return ret; |
| 1311 | } |
| 1312 | |
| 1313 | static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep) |
| 1314 | { |
| 1315 | struct dwc3_ep *dep = to_dwc3_ep(ep); |
Paul Zimmerman | 249a456 | 2012-02-24 17:32:16 -0800 | [diff] [blame] | 1316 | struct dwc3 *dwc = dep->dwc; |
| 1317 | unsigned long flags; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1318 | |
Paul Zimmerman | 249a456 | 2012-02-24 17:32:16 -0800 | [diff] [blame] | 1319 | spin_lock_irqsave(&dwc->lock, flags); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1320 | dep->flags |= DWC3_EP_WEDGE; |
Paul Zimmerman | 249a456 | 2012-02-24 17:32:16 -0800 | [diff] [blame] | 1321 | spin_unlock_irqrestore(&dwc->lock, flags); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1322 | |
Pratyush Anand | eb84075 | 2012-06-25 22:40:43 +0530 | [diff] [blame] | 1323 | if (dep->number == 0 || dep->number == 1) |
| 1324 | return dwc3_gadget_ep0_set_halt(ep, 1); |
| 1325 | else |
| 1326 | return dwc3_gadget_ep_set_halt(ep, 1); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1327 | } |
| 1328 | |
| 1329 | /* -------------------------------------------------------------------------- */ |
| 1330 | |
| 1331 | static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = { |
| 1332 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 1333 | .bDescriptorType = USB_DT_ENDPOINT, |
| 1334 | .bmAttributes = USB_ENDPOINT_XFER_CONTROL, |
| 1335 | }; |
| 1336 | |
| 1337 | static const struct usb_ep_ops dwc3_gadget_ep0_ops = { |
| 1338 | .enable = dwc3_gadget_ep0_enable, |
| 1339 | .disable = dwc3_gadget_ep0_disable, |
| 1340 | .alloc_request = dwc3_gadget_ep_alloc_request, |
| 1341 | .free_request = dwc3_gadget_ep_free_request, |
| 1342 | .queue = dwc3_gadget_ep0_queue, |
| 1343 | .dequeue = dwc3_gadget_ep_dequeue, |
Pratyush Anand | eb84075 | 2012-06-25 22:40:43 +0530 | [diff] [blame] | 1344 | .set_halt = dwc3_gadget_ep0_set_halt, |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1345 | .set_wedge = dwc3_gadget_ep_set_wedge, |
| 1346 | }; |
| 1347 | |
| 1348 | static const struct usb_ep_ops dwc3_gadget_ep_ops = { |
| 1349 | .enable = dwc3_gadget_ep_enable, |
| 1350 | .disable = dwc3_gadget_ep_disable, |
| 1351 | .alloc_request = dwc3_gadget_ep_alloc_request, |
| 1352 | .free_request = dwc3_gadget_ep_free_request, |
| 1353 | .queue = dwc3_gadget_ep_queue, |
| 1354 | .dequeue = dwc3_gadget_ep_dequeue, |
| 1355 | .set_halt = dwc3_gadget_ep_set_halt, |
| 1356 | .set_wedge = dwc3_gadget_ep_set_wedge, |
| 1357 | }; |
| 1358 | |
| 1359 | /* -------------------------------------------------------------------------- */ |
| 1360 | |
| 1361 | static int dwc3_gadget_get_frame(struct usb_gadget *g) |
| 1362 | { |
| 1363 | struct dwc3 *dwc = gadget_to_dwc(g); |
| 1364 | u32 reg; |
| 1365 | |
| 1366 | reg = dwc3_readl(dwc->regs, DWC3_DSTS); |
| 1367 | return DWC3_DSTS_SOFFN(reg); |
| 1368 | } |
| 1369 | |
| 1370 | static int dwc3_gadget_wakeup(struct usb_gadget *g) |
| 1371 | { |
| 1372 | struct dwc3 *dwc = gadget_to_dwc(g); |
| 1373 | |
| 1374 | unsigned long timeout; |
| 1375 | unsigned long flags; |
| 1376 | |
| 1377 | u32 reg; |
| 1378 | |
| 1379 | int ret = 0; |
| 1380 | |
| 1381 | u8 link_state; |
| 1382 | u8 speed; |
| 1383 | |
| 1384 | spin_lock_irqsave(&dwc->lock, flags); |
| 1385 | |
| 1386 | /* |
| 1387 | * According to the Databook Remote wakeup request should |
| 1388 | * be issued only when the device is in early suspend state. |
| 1389 | * |
| 1390 | * We can check that via USB Link State bits in DSTS register. |
| 1391 | */ |
| 1392 | reg = dwc3_readl(dwc->regs, DWC3_DSTS); |
| 1393 | |
| 1394 | speed = reg & DWC3_DSTS_CONNECTSPD; |
| 1395 | if (speed == DWC3_DSTS_SUPERSPEED) { |
| 1396 | dev_dbg(dwc->dev, "no wakeup on SuperSpeed\n"); |
| 1397 | ret = -EINVAL; |
| 1398 | goto out; |
| 1399 | } |
| 1400 | |
| 1401 | link_state = DWC3_DSTS_USBLNKST(reg); |
| 1402 | |
| 1403 | switch (link_state) { |
| 1404 | case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */ |
| 1405 | case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */ |
| 1406 | break; |
| 1407 | default: |
| 1408 | dev_dbg(dwc->dev, "can't wakeup from link state %d\n", |
| 1409 | link_state); |
| 1410 | ret = -EINVAL; |
| 1411 | goto out; |
| 1412 | } |
| 1413 | |
Felipe Balbi | 8598bde | 2012-01-02 18:55:57 +0200 | [diff] [blame] | 1414 | ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV); |
| 1415 | if (ret < 0) { |
| 1416 | dev_err(dwc->dev, "failed to put link in Recovery\n"); |
| 1417 | goto out; |
| 1418 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1419 | |
Paul Zimmerman | 88df427 | 2012-04-27 13:10:52 +0300 | [diff] [blame] | 1420 | /* Recent versions do this automatically */ |
| 1421 | if (dwc->revision < DWC3_REVISION_194A) { |
| 1422 | /* write zeroes to Link Change Request */ |
Felipe Balbi | b4d0435 | 2012-05-24 10:27:56 +0300 | [diff] [blame] | 1423 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
Paul Zimmerman | 88df427 | 2012-04-27 13:10:52 +0300 | [diff] [blame] | 1424 | reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK; |
| 1425 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); |
| 1426 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1427 | |
Paul Zimmerman | 1d04679 | 2012-02-15 18:56:56 -0800 | [diff] [blame] | 1428 | /* poll until Link State changes to ON */ |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1429 | timeout = jiffies + msecs_to_jiffies(100); |
| 1430 | |
Paul Zimmerman | 1d04679 | 2012-02-15 18:56:56 -0800 | [diff] [blame] | 1431 | while (!time_after(jiffies, timeout)) { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1432 | reg = dwc3_readl(dwc->regs, DWC3_DSTS); |
| 1433 | |
| 1434 | /* in HS, means ON */ |
| 1435 | if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0) |
| 1436 | break; |
| 1437 | } |
| 1438 | |
| 1439 | if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) { |
| 1440 | dev_err(dwc->dev, "failed to send remote wakeup\n"); |
| 1441 | ret = -EINVAL; |
| 1442 | } |
| 1443 | |
| 1444 | out: |
| 1445 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 1446 | |
| 1447 | return ret; |
| 1448 | } |
| 1449 | |
| 1450 | static int dwc3_gadget_set_selfpowered(struct usb_gadget *g, |
| 1451 | int is_selfpowered) |
| 1452 | { |
| 1453 | struct dwc3 *dwc = gadget_to_dwc(g); |
Paul Zimmerman | 249a456 | 2012-02-24 17:32:16 -0800 | [diff] [blame] | 1454 | unsigned long flags; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1455 | |
Paul Zimmerman | 249a456 | 2012-02-24 17:32:16 -0800 | [diff] [blame] | 1456 | spin_lock_irqsave(&dwc->lock, flags); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1457 | dwc->is_selfpowered = !!is_selfpowered; |
Paul Zimmerman | 249a456 | 2012-02-24 17:32:16 -0800 | [diff] [blame] | 1458 | spin_unlock_irqrestore(&dwc->lock, flags); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1459 | |
| 1460 | return 0; |
| 1461 | } |
| 1462 | |
Pratyush Anand | 77473f7 | 2012-07-02 10:21:55 +0530 | [diff] [blame] | 1463 | static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1464 | { |
| 1465 | u32 reg; |
Sebastian Andrzej Siewior | 61d5824 | 2011-08-29 16:46:38 +0200 | [diff] [blame] | 1466 | u32 timeout = 500; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1467 | |
| 1468 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
Felipe Balbi | 8db7ed1 | 2012-01-18 18:32:29 +0200 | [diff] [blame] | 1469 | if (is_on) { |
Paul Zimmerman | 88df427 | 2012-04-27 13:10:52 +0300 | [diff] [blame] | 1470 | if (dwc->revision <= DWC3_REVISION_187A) { |
| 1471 | reg &= ~DWC3_DCTL_TRGTULST_MASK; |
| 1472 | reg |= DWC3_DCTL_TRGTULST_RX_DET; |
| 1473 | } |
| 1474 | |
| 1475 | if (dwc->revision >= DWC3_REVISION_194A) |
| 1476 | reg &= ~DWC3_DCTL_KEEP_CONNECT; |
| 1477 | reg |= DWC3_DCTL_RUN_STOP; |
Felipe Balbi | 8db7ed1 | 2012-01-18 18:32:29 +0200 | [diff] [blame] | 1478 | } else { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1479 | reg &= ~DWC3_DCTL_RUN_STOP; |
Felipe Balbi | 8db7ed1 | 2012-01-18 18:32:29 +0200 | [diff] [blame] | 1480 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1481 | |
| 1482 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); |
| 1483 | |
| 1484 | do { |
| 1485 | reg = dwc3_readl(dwc->regs, DWC3_DSTS); |
| 1486 | if (is_on) { |
| 1487 | if (!(reg & DWC3_DSTS_DEVCTRLHLT)) |
| 1488 | break; |
| 1489 | } else { |
| 1490 | if (reg & DWC3_DSTS_DEVCTRLHLT) |
| 1491 | break; |
| 1492 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1493 | timeout--; |
| 1494 | if (!timeout) |
Pratyush Anand | 77473f7 | 2012-07-02 10:21:55 +0530 | [diff] [blame] | 1495 | return -ETIMEDOUT; |
Sebastian Andrzej Siewior | 61d5824 | 2011-08-29 16:46:38 +0200 | [diff] [blame] | 1496 | udelay(1); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1497 | } while (1); |
| 1498 | |
| 1499 | dev_vdbg(dwc->dev, "gadget %s data soft-%s\n", |
| 1500 | dwc->gadget_driver |
| 1501 | ? dwc->gadget_driver->function : "no-function", |
| 1502 | is_on ? "connect" : "disconnect"); |
Pratyush Anand | 77473f7 | 2012-07-02 10:21:55 +0530 | [diff] [blame] | 1503 | |
| 1504 | return 0; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1505 | } |
| 1506 | |
Vijayavardhan Vennapusa | 908f1ed | 2012-10-19 19:51:48 +0530 | [diff] [blame] | 1507 | static int dwc3_gadget_vbus_draw(struct usb_gadget *g, unsigned mA) |
| 1508 | { |
| 1509 | struct dwc3 *dwc = gadget_to_dwc(g); |
| 1510 | struct dwc3_otg *dotg = dwc->dotg; |
| 1511 | |
| 1512 | if (dotg && dotg->otg.phy) |
| 1513 | return usb_phy_set_power(dotg->otg.phy, mA); |
| 1514 | |
| 1515 | return -ENOTSUPP; |
| 1516 | } |
| 1517 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1518 | static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on) |
| 1519 | { |
| 1520 | struct dwc3 *dwc = gadget_to_dwc(g); |
| 1521 | unsigned long flags; |
Pratyush Anand | 77473f7 | 2012-07-02 10:21:55 +0530 | [diff] [blame] | 1522 | int ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1523 | |
| 1524 | is_on = !!is_on; |
| 1525 | |
| 1526 | spin_lock_irqsave(&dwc->lock, flags); |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 1527 | |
| 1528 | dwc->softconnect = is_on; |
| 1529 | |
| 1530 | if ((dwc->dotg && !dwc->vbus_active) || |
| 1531 | !dwc->gadget_driver) { |
| 1532 | |
| 1533 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 1534 | |
| 1535 | /* |
| 1536 | * Need to wait for vbus_session(on) from otg driver or to |
| 1537 | * the udc_start. |
| 1538 | */ |
| 1539 | return 0; |
| 1540 | } |
| 1541 | |
Pratyush Anand | 77473f7 | 2012-07-02 10:21:55 +0530 | [diff] [blame] | 1542 | ret = dwc3_gadget_run_stop(dwc, is_on); |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 1543 | |
| 1544 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 1545 | |
Pratyush Anand | 77473f7 | 2012-07-02 10:21:55 +0530 | [diff] [blame] | 1546 | return ret; |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 1547 | } |
| 1548 | |
| 1549 | static int dwc3_gadget_vbus_session(struct usb_gadget *_gadget, int is_active) |
| 1550 | { |
| 1551 | struct dwc3 *dwc = gadget_to_dwc(_gadget); |
| 1552 | unsigned long flags; |
Vijayavardhan Vennapusa | 8ec31d2 | 2012-10-23 08:44:48 +0530 | [diff] [blame] | 1553 | int ret = 0; |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 1554 | |
| 1555 | if (!dwc->dotg) |
| 1556 | return -EPERM; |
| 1557 | |
| 1558 | is_active = !!is_active; |
| 1559 | |
| 1560 | spin_lock_irqsave(&dwc->lock, flags); |
| 1561 | |
| 1562 | /* Mark that the vbus was powered */ |
| 1563 | dwc->vbus_active = is_active; |
| 1564 | |
| 1565 | /* |
| 1566 | * Check if upper level usb_gadget_driver was already registerd with |
| 1567 | * this udc controller driver (if dwc3_gadget_start was called) |
| 1568 | */ |
| 1569 | if (dwc->gadget_driver && dwc->softconnect) { |
| 1570 | if (dwc->vbus_active) { |
| 1571 | /* |
| 1572 | * Both vbus was activated by otg and pullup was |
| 1573 | * signaled by the gadget driver. |
| 1574 | */ |
Pratyush Anand | 77473f7 | 2012-07-02 10:21:55 +0530 | [diff] [blame] | 1575 | ret = dwc3_gadget_run_stop(dwc, 1); |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 1576 | } else { |
Pratyush Anand | 77473f7 | 2012-07-02 10:21:55 +0530 | [diff] [blame] | 1577 | ret = dwc3_gadget_run_stop(dwc, 0); |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 1578 | } |
Vijayavardhan Vennapusa | b666fb8 | 2012-11-08 16:02:51 +0530 | [diff] [blame] | 1579 | } else if (dwc->gadget_driver && !dwc->softconnect && |
| 1580 | !dwc->vbus_active) { |
| 1581 | if (dwc->gadget_driver->disconnect) { |
| 1582 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 1583 | dwc->gadget_driver->disconnect(&dwc->gadget); |
| 1584 | return 0; |
| 1585 | } |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 1586 | } |
| 1587 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1588 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 1589 | |
Pratyush Anand | 77473f7 | 2012-07-02 10:21:55 +0530 | [diff] [blame] | 1590 | return ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1591 | } |
| 1592 | |
Manu Gautam | f1fceddf | 2012-10-12 14:02:50 +0530 | [diff] [blame] | 1593 | /* Required gadget re-initialization before switching to gadget in OTG mode */ |
| 1594 | void dwc3_gadget_restart(struct dwc3 *dwc) |
| 1595 | { |
| 1596 | struct dwc3_ep *dep; |
| 1597 | int ret = 0; |
Vijayavardhan Vennapusa | b743456 | 2012-12-12 16:48:49 +0530 | [diff] [blame] | 1598 | u32 reg; |
Manu Gautam | f1fceddf | 2012-10-12 14:02:50 +0530 | [diff] [blame] | 1599 | |
Vijayavardhan Vennapusa | b743456 | 2012-12-12 16:48:49 +0530 | [diff] [blame] | 1600 | /* Enable all but Start and End of Frame IRQs */ |
| 1601 | reg = (DWC3_DEVTEN_EVNTOVERFLOWEN | |
| 1602 | DWC3_DEVTEN_CMDCMPLTEN | |
| 1603 | DWC3_DEVTEN_ERRTICERREN | |
| 1604 | DWC3_DEVTEN_WKUPEVTEN | |
| 1605 | DWC3_DEVTEN_ULSTCNGEN | |
| 1606 | DWC3_DEVTEN_CONNECTDONEEN | |
| 1607 | DWC3_DEVTEN_USBRSTEN | |
| 1608 | DWC3_DEVTEN_DISCONNEVTEN); |
| 1609 | dwc3_writel(dwc->regs, DWC3_DEVTEN, reg); |
| 1610 | |
| 1611 | /* Enable USB2 LPM and automatic phy suspend only on recent versions */ |
| 1612 | if (dwc->revision >= DWC3_REVISION_194A) { |
| 1613 | reg = dwc3_readl(dwc->regs, DWC3_DCFG); |
| 1614 | reg |= DWC3_DCFG_LPM_CAP; |
| 1615 | dwc3_writel(dwc->regs, DWC3_DCFG, reg); |
| 1616 | |
| 1617 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
| 1618 | reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN); |
| 1619 | |
| 1620 | /* TODO: This should be configurable */ |
| 1621 | reg |= DWC3_DCTL_HIRD_THRES(28); |
| 1622 | |
| 1623 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); |
| 1624 | } |
| 1625 | |
| 1626 | reg = dwc3_readl(dwc->regs, DWC3_DCFG); |
| 1627 | reg &= ~(DWC3_DCFG_SPEED_MASK); |
| 1628 | |
| 1629 | /** |
| 1630 | * WORKAROUND: DWC3 revision < 2.20a have an issue |
| 1631 | * which would cause metastability state on Run/Stop |
| 1632 | * bit if we try to force the IP to USB2-only mode. |
| 1633 | * |
| 1634 | * Because of that, we cannot configure the IP to any |
| 1635 | * speed other than the SuperSpeed |
| 1636 | * |
| 1637 | * Refers to: |
| 1638 | * |
| 1639 | * STAR#9000525659: Clock Domain Crossing on DCTL in |
| 1640 | * USB 2.0 Mode |
| 1641 | */ |
| 1642 | if (dwc->revision < DWC3_REVISION_220A) |
| 1643 | reg |= DWC3_DCFG_SUPERSPEED; |
| 1644 | else |
| 1645 | reg |= dwc->maximum_speed; |
| 1646 | dwc3_writel(dwc->regs, DWC3_DCFG, reg); |
| 1647 | |
| 1648 | dwc->start_config_issued = false; |
| 1649 | |
| 1650 | /* Start with SuperSpeed Default */ |
| 1651 | dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512); |
Manu Gautam | f1fceddf | 2012-10-12 14:02:50 +0530 | [diff] [blame] | 1652 | |
| 1653 | dwc->delayed_status = false; |
Vijayavardhan Vennapusa | b743456 | 2012-12-12 16:48:49 +0530 | [diff] [blame] | 1654 | /* reinitialize physical ep0-1 */ |
Manu Gautam | f1fceddf | 2012-10-12 14:02:50 +0530 | [diff] [blame] | 1655 | dep = dwc->eps[0]; |
| 1656 | dep->flags = 0; |
| 1657 | ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false); |
| 1658 | if (ret) { |
| 1659 | dev_err(dwc->dev, "failed to enable %s\n", dep->name); |
| 1660 | return; |
| 1661 | } |
| 1662 | |
| 1663 | dep = dwc->eps[1]; |
| 1664 | dep->flags = 0; |
| 1665 | ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false); |
| 1666 | if (ret) { |
| 1667 | dev_err(dwc->dev, "failed to enable %s\n", dep->name); |
| 1668 | return; |
| 1669 | } |
| 1670 | |
| 1671 | /* begin to receive SETUP packets */ |
| 1672 | dwc->ep0state = EP0_SETUP_PHASE; |
| 1673 | dwc3_ep0_out_start(dwc); |
| 1674 | } |
| 1675 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1676 | static int dwc3_gadget_start(struct usb_gadget *g, |
| 1677 | struct usb_gadget_driver *driver) |
| 1678 | { |
| 1679 | struct dwc3 *dwc = gadget_to_dwc(g); |
| 1680 | struct dwc3_ep *dep; |
| 1681 | unsigned long flags; |
| 1682 | int ret = 0; |
| 1683 | u32 reg; |
| 1684 | |
| 1685 | spin_lock_irqsave(&dwc->lock, flags); |
| 1686 | |
| 1687 | if (dwc->gadget_driver) { |
| 1688 | dev_err(dwc->dev, "%s is already bound to %s\n", |
| 1689 | dwc->gadget.name, |
| 1690 | dwc->gadget_driver->driver.name); |
| 1691 | ret = -EBUSY; |
| 1692 | goto err0; |
| 1693 | } |
| 1694 | |
| 1695 | dwc->gadget_driver = driver; |
| 1696 | dwc->gadget.dev.driver = &driver->driver; |
| 1697 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1698 | reg = dwc3_readl(dwc->regs, DWC3_DCFG); |
| 1699 | reg &= ~(DWC3_DCFG_SPEED_MASK); |
Felipe Balbi | 38d2c6c | 2012-03-23 12:20:31 +0200 | [diff] [blame] | 1700 | |
| 1701 | /** |
| 1702 | * WORKAROUND: DWC3 revision < 2.20a have an issue |
| 1703 | * which would cause metastability state on Run/Stop |
| 1704 | * bit if we try to force the IP to USB2-only mode. |
| 1705 | * |
| 1706 | * Because of that, we cannot configure the IP to any |
| 1707 | * speed other than the SuperSpeed |
| 1708 | * |
| 1709 | * Refers to: |
| 1710 | * |
| 1711 | * STAR#9000525659: Clock Domain Crossing on DCTL in |
| 1712 | * USB 2.0 Mode |
| 1713 | */ |
| 1714 | if (dwc->revision < DWC3_REVISION_220A) |
| 1715 | reg |= DWC3_DCFG_SUPERSPEED; |
| 1716 | else |
| 1717 | reg |= dwc->maximum_speed; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1718 | dwc3_writel(dwc->regs, DWC3_DCFG, reg); |
| 1719 | |
Paul Zimmerman | b23c843 | 2011-09-30 10:58:42 +0300 | [diff] [blame] | 1720 | dwc->start_config_issued = false; |
| 1721 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1722 | /* Start with SuperSpeed Default */ |
| 1723 | dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512); |
| 1724 | |
| 1725 | dep = dwc->eps[0]; |
Felipe Balbi | 07e0ee8 | 2012-07-16 14:08:16 +0300 | [diff] [blame] | 1726 | ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1727 | if (ret) { |
| 1728 | dev_err(dwc->dev, "failed to enable %s\n", dep->name); |
| 1729 | goto err0; |
| 1730 | } |
| 1731 | |
| 1732 | dep = dwc->eps[1]; |
Felipe Balbi | 07e0ee8 | 2012-07-16 14:08:16 +0300 | [diff] [blame] | 1733 | ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1734 | if (ret) { |
| 1735 | dev_err(dwc->dev, "failed to enable %s\n", dep->name); |
| 1736 | goto err1; |
| 1737 | } |
| 1738 | |
| 1739 | /* begin to receive SETUP packets */ |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 1740 | dwc->ep0state = EP0_SETUP_PHASE; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1741 | dwc3_ep0_out_start(dwc); |
| 1742 | |
| 1743 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 1744 | |
| 1745 | return 0; |
| 1746 | |
| 1747 | err1: |
| 1748 | __dwc3_gadget_ep_disable(dwc->eps[0]); |
| 1749 | |
| 1750 | err0: |
| 1751 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 1752 | |
| 1753 | return ret; |
| 1754 | } |
| 1755 | |
| 1756 | static int dwc3_gadget_stop(struct usb_gadget *g, |
| 1757 | struct usb_gadget_driver *driver) |
| 1758 | { |
| 1759 | struct dwc3 *dwc = gadget_to_dwc(g); |
| 1760 | unsigned long flags; |
| 1761 | |
| 1762 | spin_lock_irqsave(&dwc->lock, flags); |
| 1763 | |
| 1764 | __dwc3_gadget_ep_disable(dwc->eps[0]); |
| 1765 | __dwc3_gadget_ep_disable(dwc->eps[1]); |
| 1766 | |
| 1767 | dwc->gadget_driver = NULL; |
| 1768 | dwc->gadget.dev.driver = NULL; |
| 1769 | |
| 1770 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 1771 | |
| 1772 | return 0; |
| 1773 | } |
Paul Zimmerman | 88df427 | 2012-04-27 13:10:52 +0300 | [diff] [blame] | 1774 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1775 | static const struct usb_gadget_ops dwc3_gadget_ops = { |
| 1776 | .get_frame = dwc3_gadget_get_frame, |
| 1777 | .wakeup = dwc3_gadget_wakeup, |
| 1778 | .set_selfpowered = dwc3_gadget_set_selfpowered, |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 1779 | .vbus_session = dwc3_gadget_vbus_session, |
Vijayavardhan Vennapusa | 908f1ed | 2012-10-19 19:51:48 +0530 | [diff] [blame] | 1780 | .vbus_draw = dwc3_gadget_vbus_draw, |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1781 | .pullup = dwc3_gadget_pullup, |
| 1782 | .udc_start = dwc3_gadget_start, |
| 1783 | .udc_stop = dwc3_gadget_stop, |
| 1784 | }; |
| 1785 | |
| 1786 | /* -------------------------------------------------------------------------- */ |
| 1787 | |
| 1788 | static int __devinit dwc3_gadget_init_endpoints(struct dwc3 *dwc) |
| 1789 | { |
| 1790 | struct dwc3_ep *dep; |
| 1791 | u8 epnum; |
| 1792 | |
| 1793 | INIT_LIST_HEAD(&dwc->gadget.ep_list); |
| 1794 | |
| 1795 | for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) { |
| 1796 | dep = kzalloc(sizeof(*dep), GFP_KERNEL); |
| 1797 | if (!dep) { |
| 1798 | dev_err(dwc->dev, "can't allocate endpoint %d\n", |
| 1799 | epnum); |
| 1800 | return -ENOMEM; |
| 1801 | } |
| 1802 | |
| 1803 | dep->dwc = dwc; |
| 1804 | dep->number = epnum; |
| 1805 | dwc->eps[epnum] = dep; |
| 1806 | |
| 1807 | snprintf(dep->name, sizeof(dep->name), "ep%d%s", epnum >> 1, |
| 1808 | (epnum & 1) ? "in" : "out"); |
| 1809 | dep->endpoint.name = dep->name; |
| 1810 | dep->direction = (epnum & 1); |
| 1811 | |
| 1812 | if (epnum == 0 || epnum == 1) { |
| 1813 | dep->endpoint.maxpacket = 512; |
| 1814 | dep->endpoint.ops = &dwc3_gadget_ep0_ops; |
| 1815 | if (!epnum) |
| 1816 | dwc->gadget.ep0 = &dep->endpoint; |
| 1817 | } else { |
| 1818 | int ret; |
| 1819 | |
| 1820 | dep->endpoint.maxpacket = 1024; |
Sebastian Andrzej Siewior | 12d36c1 | 2011-11-03 20:27:50 +0100 | [diff] [blame] | 1821 | dep->endpoint.max_streams = 15; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1822 | dep->endpoint.ops = &dwc3_gadget_ep_ops; |
| 1823 | list_add_tail(&dep->endpoint.ep_list, |
| 1824 | &dwc->gadget.ep_list); |
| 1825 | |
| 1826 | ret = dwc3_alloc_trb_pool(dep); |
Felipe Balbi | 25b8ff6 | 2011-11-04 12:32:47 +0200 | [diff] [blame] | 1827 | if (ret) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1828 | return ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1829 | } |
Felipe Balbi | 25b8ff6 | 2011-11-04 12:32:47 +0200 | [diff] [blame] | 1830 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1831 | INIT_LIST_HEAD(&dep->request_list); |
| 1832 | INIT_LIST_HEAD(&dep->req_queued); |
| 1833 | } |
| 1834 | |
| 1835 | return 0; |
| 1836 | } |
| 1837 | |
| 1838 | static void dwc3_gadget_free_endpoints(struct dwc3 *dwc) |
| 1839 | { |
| 1840 | struct dwc3_ep *dep; |
| 1841 | u8 epnum; |
| 1842 | |
| 1843 | for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) { |
| 1844 | dep = dwc->eps[epnum]; |
| 1845 | dwc3_free_trb_pool(dep); |
| 1846 | |
| 1847 | if (epnum != 0 && epnum != 1) |
| 1848 | list_del(&dep->endpoint.ep_list); |
| 1849 | |
| 1850 | kfree(dep); |
| 1851 | } |
| 1852 | } |
| 1853 | |
| 1854 | static void dwc3_gadget_release(struct device *dev) |
| 1855 | { |
| 1856 | dev_dbg(dev, "%s\n", __func__); |
| 1857 | } |
| 1858 | |
| 1859 | /* -------------------------------------------------------------------------- */ |
| 1860 | static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep, |
| 1861 | const struct dwc3_event_depevt *event, int status) |
| 1862 | { |
| 1863 | struct dwc3_request *req; |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 1864 | struct dwc3_trb *trb; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1865 | unsigned int count; |
| 1866 | unsigned int s_pkt = 0; |
Pratyush Anand | 73939b0 | 2012-05-25 18:54:56 +0530 | [diff] [blame] | 1867 | unsigned int trb_status; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1868 | |
| 1869 | do { |
| 1870 | req = next_request(&dep->req_queued); |
Sebastian Andrzej Siewior | d39ee7b | 2011-11-03 10:32:20 +0100 | [diff] [blame] | 1871 | if (!req) { |
| 1872 | WARN_ON_ONCE(1); |
| 1873 | return 1; |
| 1874 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1875 | |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 1876 | trb = req->trb; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1877 | |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 1878 | if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN) |
Sebastian Andrzej Siewior | 0d2f475 | 2011-08-19 19:59:12 +0200 | [diff] [blame] | 1879 | /* |
| 1880 | * We continue despite the error. There is not much we |
Paul Zimmerman | 1d04679 | 2012-02-15 18:56:56 -0800 | [diff] [blame] | 1881 | * can do. If we don't clean it up we loop forever. If |
| 1882 | * we skip the TRB then it gets overwritten after a |
| 1883 | * while since we use them in a ring buffer. A BUG() |
| 1884 | * would help. Lets hope that if this occurs, someone |
Sebastian Andrzej Siewior | 0d2f475 | 2011-08-19 19:59:12 +0200 | [diff] [blame] | 1885 | * fixes the root cause instead of looking away :) |
| 1886 | */ |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1887 | dev_err(dwc->dev, "%s's TRB (%p) still owned by HW\n", |
| 1888 | dep->name, req->trb); |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 1889 | count = trb->size & DWC3_TRB_SIZE_MASK; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1890 | |
| 1891 | if (dep->direction) { |
| 1892 | if (count) { |
Pratyush Anand | 73939b0 | 2012-05-25 18:54:56 +0530 | [diff] [blame] | 1893 | trb_status = DWC3_TRB_SIZE_TRBSTS(trb->size); |
| 1894 | if (trb_status == DWC3_TRBSTS_MISSED_ISOC) { |
| 1895 | dev_dbg(dwc->dev, "incomplete IN transfer %s\n", |
| 1896 | dep->name); |
Pratyush Anand | 921b0b8 | 2013-01-14 15:59:32 +0530 | [diff] [blame] | 1897 | /* |
| 1898 | * If missed isoc occurred and there is |
| 1899 | * no request queued then issue END |
| 1900 | * TRANSFER, so that core generates |
| 1901 | * next xfernotready and we will issue |
| 1902 | * a fresh START TRANSFER. |
| 1903 | * If there are still queued request |
| 1904 | * then wait, do not issue either END |
| 1905 | * or UPDATE TRANSFER, just attach next |
| 1906 | * request in request_list during |
| 1907 | * giveback.If any future queued request |
| 1908 | * is successfully transferred then we |
| 1909 | * will issue UPDATE TRANSFER for all |
| 1910 | * request in the request_list. |
| 1911 | */ |
Pratyush Anand | 73939b0 | 2012-05-25 18:54:56 +0530 | [diff] [blame] | 1912 | dep->flags |= DWC3_EP_MISSED_ISOC; |
| 1913 | } else { |
| 1914 | dev_err(dwc->dev, "incomplete IN transfer %s\n", |
| 1915 | dep->name); |
| 1916 | status = -ECONNRESET; |
| 1917 | } |
Pratyush Anand | 921b0b8 | 2013-01-14 15:59:32 +0530 | [diff] [blame] | 1918 | } else { |
| 1919 | dep->flags &= ~DWC3_EP_MISSED_ISOC; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1920 | } |
| 1921 | } else { |
| 1922 | if (count && (event->status & DEPEVT_STATUS_SHORT)) |
| 1923 | s_pkt = 1; |
| 1924 | } |
| 1925 | |
| 1926 | /* |
| 1927 | * We assume here we will always receive the entire data block |
| 1928 | * which we should receive. Meaning, if we program RX to |
| 1929 | * receive 4K but we receive only 2K, we assume that's all we |
| 1930 | * should receive and we simply bounce the request back to the |
| 1931 | * gadget driver for further processing. |
| 1932 | */ |
| 1933 | req->request.actual += req->request.length - count; |
| 1934 | dwc3_gadget_giveback(dep, req, status); |
| 1935 | if (s_pkt) |
| 1936 | break; |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 1937 | if ((event->status & DEPEVT_STATUS_LST) && |
Pratyush Anand | 413dba6 | 2012-06-03 19:43:19 +0530 | [diff] [blame] | 1938 | (trb->ctrl & (DWC3_TRB_CTRL_LST | |
| 1939 | DWC3_TRB_CTRL_HWO))) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1940 | break; |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 1941 | if ((event->status & DEPEVT_STATUS_IOC) && |
| 1942 | (trb->ctrl & DWC3_TRB_CTRL_IOC)) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1943 | break; |
| 1944 | } while (1); |
| 1945 | |
Pratyush Anand | 18bbcb0 | 2013-01-14 15:59:34 +0530 | [diff] [blame] | 1946 | if (usb_endpoint_xfer_isoc(dep->endpoint.desc) && |
| 1947 | list_empty(&dep->req_queued)) { |
Vijayavardhan Vennapusa | 91ba653 | 2013-01-30 17:35:45 +0530 | [diff] [blame] | 1948 | if (list_empty(&dep->request_list)) |
Pratyush Anand | 18bbcb0 | 2013-01-14 15:59:34 +0530 | [diff] [blame] | 1949 | /* |
| 1950 | * If there is no entry in request list then do |
| 1951 | * not issue END TRANSFER now. Just set PENDING |
| 1952 | * flag, so that END TRANSFER is issued when an |
| 1953 | * entry is added into request list. |
| 1954 | */ |
Vijayavardhan Vennapusa | 91ba653 | 2013-01-30 17:35:45 +0530 | [diff] [blame] | 1955 | dep->flags |= DWC3_EP_PENDING_REQUEST; |
| 1956 | else |
Pratyush Anand | 18bbcb0 | 2013-01-14 15:59:34 +0530 | [diff] [blame] | 1957 | dwc3_stop_active_transfer(dwc, dep->number); |
Vijayavardhan Vennapusa | 91ba653 | 2013-01-30 17:35:45 +0530 | [diff] [blame] | 1958 | dep->flags &= ~DWC3_EP_MISSED_ISOC; |
Pratyush Anand | 921b0b8 | 2013-01-14 15:59:32 +0530 | [diff] [blame] | 1959 | return 1; |
| 1960 | } |
| 1961 | |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 1962 | if ((event->status & DEPEVT_STATUS_IOC) && |
| 1963 | (trb->ctrl & DWC3_TRB_CTRL_IOC)) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1964 | return 0; |
| 1965 | return 1; |
| 1966 | } |
| 1967 | |
| 1968 | static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc, |
| 1969 | struct dwc3_ep *dep, const struct dwc3_event_depevt *event, |
| 1970 | int start_new) |
| 1971 | { |
| 1972 | unsigned status = 0; |
| 1973 | int clean_busy; |
| 1974 | |
| 1975 | if (event->status & DEPEVT_STATUS_BUSERR) |
| 1976 | status = -ECONNRESET; |
| 1977 | |
Paul Zimmerman | 1d04679 | 2012-02-15 18:56:56 -0800 | [diff] [blame] | 1978 | clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status); |
Paul Zimmerman | c2df85c | 2012-02-24 17:32:18 -0800 | [diff] [blame] | 1979 | if (clean_busy) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1980 | dep->flags &= ~DWC3_EP_BUSY; |
Felipe Balbi | fae2b90 | 2011-10-14 13:00:30 +0300 | [diff] [blame] | 1981 | |
| 1982 | /* |
| 1983 | * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround. |
| 1984 | * See dwc3_gadget_linksts_change_interrupt() for 1st half. |
| 1985 | */ |
| 1986 | if (dwc->revision < DWC3_REVISION_183A) { |
| 1987 | u32 reg; |
| 1988 | int i; |
| 1989 | |
| 1990 | for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) { |
Moiz Sonasath | eed03f1 | 2012-08-01 14:08:30 -0500 | [diff] [blame] | 1991 | dep = dwc->eps[i]; |
Felipe Balbi | fae2b90 | 2011-10-14 13:00:30 +0300 | [diff] [blame] | 1992 | |
| 1993 | if (!(dep->flags & DWC3_EP_ENABLED)) |
| 1994 | continue; |
| 1995 | |
| 1996 | if (!list_empty(&dep->req_queued)) |
| 1997 | return; |
| 1998 | } |
| 1999 | |
| 2000 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
| 2001 | reg |= dwc->u1u2; |
| 2002 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); |
| 2003 | |
| 2004 | dwc->u1u2 = 0; |
| 2005 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2006 | } |
| 2007 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2008 | static void dwc3_endpoint_interrupt(struct dwc3 *dwc, |
| 2009 | const struct dwc3_event_depevt *event) |
| 2010 | { |
| 2011 | struct dwc3_ep *dep; |
| 2012 | u8 epnum = event->endpoint_number; |
| 2013 | |
| 2014 | dep = dwc->eps[epnum]; |
| 2015 | |
Felipe Balbi | a09be0a | 2012-06-06 09:19:35 +0300 | [diff] [blame] | 2016 | if (!(dep->flags & DWC3_EP_ENABLED)) |
| 2017 | return; |
| 2018 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2019 | dev_vdbg(dwc->dev, "%s: %s\n", dep->name, |
| 2020 | dwc3_ep_event_string(event->endpoint_event)); |
| 2021 | |
| 2022 | if (epnum == 0 || epnum == 1) { |
| 2023 | dwc3_ep0_interrupt(dwc, event); |
| 2024 | return; |
| 2025 | } |
| 2026 | |
| 2027 | switch (event->endpoint_event) { |
| 2028 | case DWC3_DEPEVT_XFERCOMPLETE: |
Felipe Balbi | 4959cfc | 2012-06-06 12:04:13 +0300 | [diff] [blame] | 2029 | dep->resource_index = 0; |
Paul Zimmerman | c2df85c | 2012-02-24 17:32:18 -0800 | [diff] [blame] | 2030 | |
Ido Shayevitz | 57cdac1 | 2012-03-12 20:25:24 +0200 | [diff] [blame] | 2031 | if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2032 | dev_dbg(dwc->dev, "%s is an Isochronous endpoint\n", |
| 2033 | dep->name); |
| 2034 | return; |
| 2035 | } |
| 2036 | |
| 2037 | dwc3_endpoint_transfer_complete(dwc, dep, event, 1); |
| 2038 | break; |
| 2039 | case DWC3_DEPEVT_XFERINPROGRESS: |
Ido Shayevitz | 57cdac1 | 2012-03-12 20:25:24 +0200 | [diff] [blame] | 2040 | if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2041 | dev_dbg(dwc->dev, "%s is not an Isochronous endpoint\n", |
| 2042 | dep->name); |
| 2043 | return; |
| 2044 | } |
| 2045 | |
| 2046 | dwc3_endpoint_transfer_complete(dwc, dep, event, 0); |
| 2047 | break; |
| 2048 | case DWC3_DEPEVT_XFERNOTREADY: |
Ido Shayevitz | 57cdac1 | 2012-03-12 20:25:24 +0200 | [diff] [blame] | 2049 | if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2050 | dwc3_gadget_start_isoc(dwc, dep, event); |
| 2051 | } else { |
| 2052 | int ret; |
| 2053 | |
| 2054 | dev_vdbg(dwc->dev, "%s: reason %s\n", |
Felipe Balbi | 40aa41f | 2012-01-18 17:06:03 +0200 | [diff] [blame] | 2055 | dep->name, event->status & |
| 2056 | DEPEVT_STATUS_TRANSFER_ACTIVE |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2057 | ? "Transfer Active" |
| 2058 | : "Transfer Not Active"); |
| 2059 | |
| 2060 | ret = __dwc3_gadget_kick_transfer(dep, 0, 1); |
| 2061 | if (!ret || ret == -EBUSY) |
| 2062 | return; |
| 2063 | |
| 2064 | dev_dbg(dwc->dev, "%s: failed to kick transfers\n", |
| 2065 | dep->name); |
| 2066 | } |
| 2067 | |
| 2068 | break; |
Felipe Balbi | 879631a | 2011-09-30 10:58:47 +0300 | [diff] [blame] | 2069 | case DWC3_DEPEVT_STREAMEVT: |
Ido Shayevitz | 57cdac1 | 2012-03-12 20:25:24 +0200 | [diff] [blame] | 2070 | if (!usb_endpoint_xfer_bulk(dep->endpoint.desc)) { |
Felipe Balbi | 879631a | 2011-09-30 10:58:47 +0300 | [diff] [blame] | 2071 | dev_err(dwc->dev, "Stream event for non-Bulk %s\n", |
| 2072 | dep->name); |
| 2073 | return; |
| 2074 | } |
| 2075 | |
| 2076 | switch (event->status) { |
| 2077 | case DEPEVT_STREAMEVT_FOUND: |
| 2078 | dev_vdbg(dwc->dev, "Stream %d found and started\n", |
| 2079 | event->parameters); |
| 2080 | |
| 2081 | break; |
| 2082 | case DEPEVT_STREAMEVT_NOTFOUND: |
| 2083 | /* FALLTHROUGH */ |
| 2084 | default: |
| 2085 | dev_dbg(dwc->dev, "Couldn't find suitable stream\n"); |
| 2086 | } |
| 2087 | break; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2088 | case DWC3_DEPEVT_RXTXFIFOEVT: |
| 2089 | dev_dbg(dwc->dev, "%s FIFO Overrun\n", dep->name); |
| 2090 | break; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2091 | case DWC3_DEPEVT_EPCMDCMPLT: |
Felipe Balbi | b129eb7 | 2012-02-17 12:10:04 +0200 | [diff] [blame] | 2092 | dev_vdbg(dwc->dev, "Endpoint Command Complete\n"); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2093 | break; |
| 2094 | } |
| 2095 | } |
| 2096 | |
| 2097 | static void dwc3_disconnect_gadget(struct dwc3 *dwc) |
| 2098 | { |
| 2099 | if (dwc->gadget_driver && dwc->gadget_driver->disconnect) { |
| 2100 | spin_unlock(&dwc->lock); |
| 2101 | dwc->gadget_driver->disconnect(&dwc->gadget); |
| 2102 | spin_lock(&dwc->lock); |
| 2103 | } |
| 2104 | } |
| 2105 | |
| 2106 | static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum) |
| 2107 | { |
| 2108 | struct dwc3_ep *dep; |
| 2109 | struct dwc3_gadget_ep_cmd_params params; |
| 2110 | u32 cmd; |
| 2111 | int ret; |
| 2112 | |
| 2113 | dep = dwc->eps[epnum]; |
| 2114 | |
Felipe Balbi | 4959cfc | 2012-06-06 12:04:13 +0300 | [diff] [blame] | 2115 | if (!dep->resource_index) |
Pratyush Anand | 6263ebe | 2012-06-23 02:23:08 +0530 | [diff] [blame] | 2116 | return; |
| 2117 | |
Pratyush Anand | e67fdeb | 2012-07-06 15:19:10 +0530 | [diff] [blame] | 2118 | /* |
| 2119 | * NOTICE: We are violating what the Databook says about the |
| 2120 | * EndTransfer command. Ideally we would _always_ wait for the |
| 2121 | * EndTransfer Command Completion IRQ, but that's causing too |
| 2122 | * much trouble synchronizing between us and gadget driver. |
| 2123 | * |
| 2124 | * We have discussed this with the IP Provider and it was |
| 2125 | * suggested to giveback all requests here, but give HW some |
| 2126 | * extra time to synchronize with the interconnect. We're using |
| 2127 | * an arbitraty 100us delay for that. |
| 2128 | * |
| 2129 | * Note also that a similar handling was tested by Synopsys |
| 2130 | * (thanks a lot Paul) and nothing bad has come out of it. |
| 2131 | * In short, what we're doing is: |
| 2132 | * |
| 2133 | * - Issue EndTransfer WITH CMDIOC bit set |
| 2134 | * - Wait 100us |
| 2135 | */ |
| 2136 | |
Pratyush Anand | 6263ebe | 2012-06-23 02:23:08 +0530 | [diff] [blame] | 2137 | cmd = DWC3_DEPCMD_ENDTRANSFER; |
| 2138 | cmd |= DWC3_DEPCMD_HIPRI_FORCERM | DWC3_DEPCMD_CMDIOC; |
Felipe Balbi | 4959cfc | 2012-06-06 12:04:13 +0300 | [diff] [blame] | 2139 | cmd |= DWC3_DEPCMD_PARAM(dep->resource_index); |
Pratyush Anand | 6263ebe | 2012-06-23 02:23:08 +0530 | [diff] [blame] | 2140 | memset(¶ms, 0, sizeof(params)); |
| 2141 | ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, ¶ms); |
| 2142 | WARN_ON_ONCE(ret); |
Felipe Balbi | 4959cfc | 2012-06-06 12:04:13 +0300 | [diff] [blame] | 2143 | dep->resource_index = 0; |
Pratyush Anand | e67fdeb | 2012-07-06 15:19:10 +0530 | [diff] [blame] | 2144 | |
| 2145 | udelay(100); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2146 | } |
| 2147 | |
| 2148 | static void dwc3_stop_active_transfers(struct dwc3 *dwc) |
| 2149 | { |
| 2150 | u32 epnum; |
| 2151 | |
| 2152 | for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) { |
| 2153 | struct dwc3_ep *dep; |
| 2154 | |
| 2155 | dep = dwc->eps[epnum]; |
| 2156 | if (!(dep->flags & DWC3_EP_ENABLED)) |
| 2157 | continue; |
| 2158 | |
Sebastian Andrzej Siewior | 624407f | 2011-08-29 13:56:37 +0200 | [diff] [blame] | 2159 | dwc3_remove_requests(dwc, dep); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2160 | } |
| 2161 | } |
| 2162 | |
| 2163 | static void dwc3_clear_stall_all_ep(struct dwc3 *dwc) |
| 2164 | { |
| 2165 | u32 epnum; |
| 2166 | |
| 2167 | for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) { |
| 2168 | struct dwc3_ep *dep; |
| 2169 | struct dwc3_gadget_ep_cmd_params params; |
| 2170 | int ret; |
| 2171 | |
| 2172 | dep = dwc->eps[epnum]; |
| 2173 | |
| 2174 | if (!(dep->flags & DWC3_EP_STALL)) |
| 2175 | continue; |
| 2176 | |
| 2177 | dep->flags &= ~DWC3_EP_STALL; |
| 2178 | |
| 2179 | memset(¶ms, 0, sizeof(params)); |
| 2180 | ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, |
| 2181 | DWC3_DEPCMD_CLEARSTALL, ¶ms); |
| 2182 | WARN_ON_ONCE(ret); |
| 2183 | } |
| 2184 | } |
| 2185 | |
| 2186 | static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc) |
| 2187 | { |
Felipe Balbi | 34d548c | 2012-05-24 10:30:01 +0300 | [diff] [blame] | 2188 | int reg; |
| 2189 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2190 | dev_vdbg(dwc->dev, "%s\n", __func__); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2191 | |
| 2192 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
| 2193 | reg &= ~DWC3_DCTL_INITU1ENA; |
| 2194 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); |
| 2195 | |
| 2196 | reg &= ~DWC3_DCTL_INITU2ENA; |
| 2197 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2198 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2199 | dwc3_disconnect_gadget(dwc); |
Paul Zimmerman | b23c843 | 2011-09-30 10:58:42 +0300 | [diff] [blame] | 2200 | dwc->start_config_issued = false; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2201 | |
| 2202 | dwc->gadget.speed = USB_SPEED_UNKNOWN; |
Felipe Balbi | df62df5 | 2011-10-14 15:11:49 +0300 | [diff] [blame] | 2203 | dwc->setup_packet_pending = false; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2204 | } |
| 2205 | |
Paul Zimmerman | dffb81b | 2012-04-27 12:54:05 +0300 | [diff] [blame] | 2206 | static void dwc3_gadget_usb3_phy_suspend(struct dwc3 *dwc, int suspend) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2207 | { |
| 2208 | u32 reg; |
| 2209 | |
| 2210 | reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0)); |
| 2211 | |
Paul Zimmerman | dffb81b | 2012-04-27 12:54:05 +0300 | [diff] [blame] | 2212 | if (suspend) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2213 | reg |= DWC3_GUSB3PIPECTL_SUSPHY; |
Paul Zimmerman | dffb81b | 2012-04-27 12:54:05 +0300 | [diff] [blame] | 2214 | else |
| 2215 | reg &= ~DWC3_GUSB3PIPECTL_SUSPHY; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2216 | |
| 2217 | dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg); |
| 2218 | } |
| 2219 | |
Paul Zimmerman | dffb81b | 2012-04-27 12:54:05 +0300 | [diff] [blame] | 2220 | static void dwc3_gadget_usb2_phy_suspend(struct dwc3 *dwc, int suspend) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2221 | { |
| 2222 | u32 reg; |
| 2223 | |
| 2224 | reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0)); |
| 2225 | |
Paul Zimmerman | dffb81b | 2012-04-27 12:54:05 +0300 | [diff] [blame] | 2226 | if (suspend) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2227 | reg |= DWC3_GUSB2PHYCFG_SUSPHY; |
Paul Zimmerman | dffb81b | 2012-04-27 12:54:05 +0300 | [diff] [blame] | 2228 | else |
| 2229 | reg &= ~DWC3_GUSB2PHYCFG_SUSPHY; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2230 | |
| 2231 | dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg); |
| 2232 | } |
| 2233 | |
| 2234 | static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc) |
| 2235 | { |
| 2236 | u32 reg; |
Vijayavardhan Vennapusa | 908f1ed | 2012-10-19 19:51:48 +0530 | [diff] [blame] | 2237 | struct dwc3_otg *dotg = dwc->dotg; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2238 | |
| 2239 | dev_vdbg(dwc->dev, "%s\n", __func__); |
| 2240 | |
Felipe Balbi | df62df5 | 2011-10-14 15:11:49 +0300 | [diff] [blame] | 2241 | /* |
| 2242 | * WORKAROUND: DWC3 revisions <1.88a have an issue which |
| 2243 | * would cause a missing Disconnect Event if there's a |
| 2244 | * pending Setup Packet in the FIFO. |
| 2245 | * |
| 2246 | * There's no suggested workaround on the official Bug |
| 2247 | * report, which states that "unless the driver/application |
| 2248 | * is doing any special handling of a disconnect event, |
| 2249 | * there is no functional issue". |
| 2250 | * |
| 2251 | * Unfortunately, it turns out that we _do_ some special |
| 2252 | * handling of a disconnect event, namely complete all |
| 2253 | * pending transfers, notify gadget driver of the |
| 2254 | * disconnection, and so on. |
| 2255 | * |
| 2256 | * Our suggested workaround is to follow the Disconnect |
| 2257 | * Event steps here, instead, based on a setup_packet_pending |
| 2258 | * flag. Such flag gets set whenever we have a XferNotReady |
| 2259 | * event on EP0 and gets cleared on XferComplete for the |
| 2260 | * same endpoint. |
| 2261 | * |
| 2262 | * Refers to: |
| 2263 | * |
| 2264 | * STAR#9000466709: RTL: Device : Disconnect event not |
| 2265 | * generated if setup packet pending in FIFO |
| 2266 | */ |
| 2267 | if (dwc->revision < DWC3_REVISION_188A) { |
| 2268 | if (dwc->setup_packet_pending) |
| 2269 | dwc3_gadget_disconnect_interrupt(dwc); |
| 2270 | } |
| 2271 | |
Felipe Balbi | 961906e | 2011-12-20 15:37:21 +0200 | [diff] [blame] | 2272 | /* after reset -> Default State */ |
| 2273 | dwc->dev_state = DWC3_DEFAULT_STATE; |
| 2274 | |
Paul Zimmerman | 88df427 | 2012-04-27 13:10:52 +0300 | [diff] [blame] | 2275 | /* Recent versions support automatic phy suspend and don't need this */ |
| 2276 | if (dwc->revision < DWC3_REVISION_194A) { |
| 2277 | /* Resume PHYs */ |
| 2278 | dwc3_gadget_usb2_phy_suspend(dwc, false); |
| 2279 | dwc3_gadget_usb3_phy_suspend(dwc, false); |
| 2280 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2281 | |
Vijayavardhan Vennapusa | 908f1ed | 2012-10-19 19:51:48 +0530 | [diff] [blame] | 2282 | if (dotg && dotg->otg.phy) |
| 2283 | usb_phy_set_power(dotg->otg.phy, 0); |
| 2284 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2285 | if (dwc->gadget.speed != USB_SPEED_UNKNOWN) |
| 2286 | dwc3_disconnect_gadget(dwc); |
| 2287 | |
| 2288 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
| 2289 | reg &= ~DWC3_DCTL_TSTCTRL_MASK; |
| 2290 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); |
Gerard Cauvy | 3b63736 | 2012-02-10 12:21:18 +0200 | [diff] [blame] | 2291 | dwc->test_mode = false; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2292 | |
| 2293 | dwc3_stop_active_transfers(dwc); |
| 2294 | dwc3_clear_stall_all_ep(dwc); |
Paul Zimmerman | b23c843 | 2011-09-30 10:58:42 +0300 | [diff] [blame] | 2295 | dwc->start_config_issued = false; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2296 | |
| 2297 | /* Reset device address to zero */ |
| 2298 | reg = dwc3_readl(dwc->regs, DWC3_DCFG); |
| 2299 | reg &= ~(DWC3_DCFG_DEVADDR_MASK); |
| 2300 | dwc3_writel(dwc->regs, DWC3_DCFG, reg); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2301 | } |
| 2302 | |
| 2303 | static void dwc3_update_ram_clk_sel(struct dwc3 *dwc, u32 speed) |
| 2304 | { |
| 2305 | u32 reg; |
| 2306 | u32 usb30_clock = DWC3_GCTL_CLK_BUS; |
| 2307 | |
| 2308 | /* |
| 2309 | * We change the clock only at SS but I dunno why I would want to do |
| 2310 | * this. Maybe it becomes part of the power saving plan. |
| 2311 | */ |
| 2312 | |
| 2313 | if (speed != DWC3_DSTS_SUPERSPEED) |
| 2314 | return; |
| 2315 | |
| 2316 | /* |
| 2317 | * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed |
| 2318 | * each time on Connect Done. |
| 2319 | */ |
| 2320 | if (!usb30_clock) |
| 2321 | return; |
| 2322 | |
| 2323 | reg = dwc3_readl(dwc->regs, DWC3_GCTL); |
| 2324 | reg |= DWC3_GCTL_RAMCLKSEL(usb30_clock); |
| 2325 | dwc3_writel(dwc->regs, DWC3_GCTL, reg); |
| 2326 | } |
| 2327 | |
Paul Zimmerman | dffb81b | 2012-04-27 12:54:05 +0300 | [diff] [blame] | 2328 | static void dwc3_gadget_phy_suspend(struct dwc3 *dwc, u8 speed) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2329 | { |
| 2330 | switch (speed) { |
| 2331 | case USB_SPEED_SUPER: |
Paul Zimmerman | dffb81b | 2012-04-27 12:54:05 +0300 | [diff] [blame] | 2332 | dwc3_gadget_usb2_phy_suspend(dwc, true); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2333 | break; |
| 2334 | case USB_SPEED_HIGH: |
| 2335 | case USB_SPEED_FULL: |
| 2336 | case USB_SPEED_LOW: |
Paul Zimmerman | dffb81b | 2012-04-27 12:54:05 +0300 | [diff] [blame] | 2337 | dwc3_gadget_usb3_phy_suspend(dwc, true); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2338 | break; |
| 2339 | } |
| 2340 | } |
| 2341 | |
| 2342 | static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc) |
| 2343 | { |
| 2344 | struct dwc3_gadget_ep_cmd_params params; |
| 2345 | struct dwc3_ep *dep; |
| 2346 | int ret; |
| 2347 | u32 reg; |
| 2348 | u8 speed; |
| 2349 | |
| 2350 | dev_vdbg(dwc->dev, "%s\n", __func__); |
| 2351 | |
| 2352 | memset(¶ms, 0x00, sizeof(params)); |
| 2353 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2354 | reg = dwc3_readl(dwc->regs, DWC3_DSTS); |
| 2355 | speed = reg & DWC3_DSTS_CONNECTSPD; |
| 2356 | dwc->speed = speed; |
| 2357 | |
| 2358 | dwc3_update_ram_clk_sel(dwc, speed); |
| 2359 | |
| 2360 | switch (speed) { |
| 2361 | case DWC3_DCFG_SUPERSPEED: |
Felipe Balbi | 05870c5 | 2011-10-14 14:51:38 +0300 | [diff] [blame] | 2362 | /* |
| 2363 | * WORKAROUND: DWC3 revisions <1.90a have an issue which |
| 2364 | * would cause a missing USB3 Reset event. |
| 2365 | * |
| 2366 | * In such situations, we should force a USB3 Reset |
| 2367 | * event by calling our dwc3_gadget_reset_interrupt() |
| 2368 | * routine. |
| 2369 | * |
| 2370 | * Refers to: |
| 2371 | * |
| 2372 | * STAR#9000483510: RTL: SS : USB3 reset event may |
| 2373 | * not be generated always when the link enters poll |
| 2374 | */ |
| 2375 | if (dwc->revision < DWC3_REVISION_190A) |
| 2376 | dwc3_gadget_reset_interrupt(dwc); |
| 2377 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2378 | dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512); |
| 2379 | dwc->gadget.ep0->maxpacket = 512; |
| 2380 | dwc->gadget.speed = USB_SPEED_SUPER; |
| 2381 | break; |
| 2382 | case DWC3_DCFG_HIGHSPEED: |
| 2383 | dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64); |
| 2384 | dwc->gadget.ep0->maxpacket = 64; |
| 2385 | dwc->gadget.speed = USB_SPEED_HIGH; |
| 2386 | break; |
| 2387 | case DWC3_DCFG_FULLSPEED2: |
| 2388 | case DWC3_DCFG_FULLSPEED1: |
| 2389 | dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64); |
| 2390 | dwc->gadget.ep0->maxpacket = 64; |
| 2391 | dwc->gadget.speed = USB_SPEED_FULL; |
| 2392 | break; |
| 2393 | case DWC3_DCFG_LOWSPEED: |
| 2394 | dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8); |
| 2395 | dwc->gadget.ep0->maxpacket = 8; |
| 2396 | dwc->gadget.speed = USB_SPEED_LOW; |
| 2397 | break; |
| 2398 | } |
| 2399 | |
Paul Zimmerman | 88df427 | 2012-04-27 13:10:52 +0300 | [diff] [blame] | 2400 | /* Recent versions support automatic phy suspend and don't need this */ |
| 2401 | if (dwc->revision < DWC3_REVISION_194A) { |
| 2402 | /* Suspend unneeded PHY */ |
| 2403 | dwc3_gadget_phy_suspend(dwc, dwc->gadget.speed); |
| 2404 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2405 | |
| 2406 | dep = dwc->eps[0]; |
Felipe Balbi | 07e0ee8 | 2012-07-16 14:08:16 +0300 | [diff] [blame] | 2407 | ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2408 | if (ret) { |
| 2409 | dev_err(dwc->dev, "failed to enable %s\n", dep->name); |
| 2410 | return; |
| 2411 | } |
| 2412 | |
| 2413 | dep = dwc->eps[1]; |
Felipe Balbi | 07e0ee8 | 2012-07-16 14:08:16 +0300 | [diff] [blame] | 2414 | ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2415 | if (ret) { |
| 2416 | dev_err(dwc->dev, "failed to enable %s\n", dep->name); |
| 2417 | return; |
| 2418 | } |
| 2419 | |
| 2420 | /* |
| 2421 | * Configure PHY via GUSB3PIPECTLn if required. |
| 2422 | * |
| 2423 | * Update GTXFIFOSIZn |
| 2424 | * |
| 2425 | * In both cases reset values should be sufficient. |
| 2426 | */ |
| 2427 | } |
| 2428 | |
| 2429 | static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc) |
| 2430 | { |
| 2431 | dev_vdbg(dwc->dev, "%s\n", __func__); |
| 2432 | |
| 2433 | /* |
| 2434 | * TODO take core out of low power mode when that's |
| 2435 | * implemented. |
| 2436 | */ |
| 2437 | |
| 2438 | dwc->gadget_driver->resume(&dwc->gadget); |
| 2439 | } |
| 2440 | |
| 2441 | static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc, |
| 2442 | unsigned int evtinfo) |
| 2443 | { |
Felipe Balbi | fae2b90 | 2011-10-14 13:00:30 +0300 | [diff] [blame] | 2444 | enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK; |
| 2445 | |
| 2446 | /* |
| 2447 | * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending |
| 2448 | * on the link partner, the USB session might do multiple entry/exit |
| 2449 | * of low power states before a transfer takes place. |
| 2450 | * |
| 2451 | * Due to this problem, we might experience lower throughput. The |
| 2452 | * suggested workaround is to disable DCTL[12:9] bits if we're |
| 2453 | * transitioning from U1/U2 to U0 and enable those bits again |
| 2454 | * after a transfer completes and there are no pending transfers |
| 2455 | * on any of the enabled endpoints. |
| 2456 | * |
| 2457 | * This is the first half of that workaround. |
| 2458 | * |
| 2459 | * Refers to: |
| 2460 | * |
| 2461 | * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us |
| 2462 | * core send LGO_Ux entering U0 |
| 2463 | */ |
| 2464 | if (dwc->revision < DWC3_REVISION_183A) { |
| 2465 | if (next == DWC3_LINK_STATE_U0) { |
| 2466 | u32 u1u2; |
| 2467 | u32 reg; |
| 2468 | |
| 2469 | switch (dwc->link_state) { |
| 2470 | case DWC3_LINK_STATE_U1: |
| 2471 | case DWC3_LINK_STATE_U2: |
| 2472 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
| 2473 | u1u2 = reg & (DWC3_DCTL_INITU2ENA |
| 2474 | | DWC3_DCTL_ACCEPTU2ENA |
| 2475 | | DWC3_DCTL_INITU1ENA |
| 2476 | | DWC3_DCTL_ACCEPTU1ENA); |
| 2477 | |
| 2478 | if (!dwc->u1u2) |
| 2479 | dwc->u1u2 = reg & u1u2; |
| 2480 | |
| 2481 | reg &= ~u1u2; |
| 2482 | |
| 2483 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); |
| 2484 | break; |
| 2485 | default: |
| 2486 | /* do nothing */ |
| 2487 | break; |
| 2488 | } |
| 2489 | } |
| 2490 | } |
| 2491 | |
Vijayavardhan Vennapusa | 54be1d6 | 2012-10-06 18:32:06 +0530 | [diff] [blame] | 2492 | if (next == DWC3_LINK_STATE_U0) { |
| 2493 | if (dwc->link_state == DWC3_LINK_STATE_U3) |
| 2494 | dwc->gadget_driver->resume(&dwc->gadget); |
| 2495 | } else if (next == DWC3_LINK_STATE_U3) { |
| 2496 | dwc->gadget_driver->suspend(&dwc->gadget); |
| 2497 | } |
| 2498 | |
Felipe Balbi | fae2b90 | 2011-10-14 13:00:30 +0300 | [diff] [blame] | 2499 | dwc->link_state = next; |
Felipe Balbi | 019ac83 | 2011-09-08 21:18:47 +0300 | [diff] [blame] | 2500 | |
| 2501 | dev_vdbg(dwc->dev, "%s link %d\n", __func__, dwc->link_state); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2502 | } |
| 2503 | |
| 2504 | static void dwc3_gadget_interrupt(struct dwc3 *dwc, |
| 2505 | const struct dwc3_event_devt *event) |
| 2506 | { |
| 2507 | switch (event->type) { |
| 2508 | case DWC3_DEVICE_EVENT_DISCONNECT: |
| 2509 | dwc3_gadget_disconnect_interrupt(dwc); |
| 2510 | break; |
| 2511 | case DWC3_DEVICE_EVENT_RESET: |
| 2512 | dwc3_gadget_reset_interrupt(dwc); |
| 2513 | break; |
| 2514 | case DWC3_DEVICE_EVENT_CONNECT_DONE: |
| 2515 | dwc3_gadget_conndone_interrupt(dwc); |
| 2516 | break; |
| 2517 | case DWC3_DEVICE_EVENT_WAKEUP: |
| 2518 | dwc3_gadget_wakeup_interrupt(dwc); |
| 2519 | break; |
| 2520 | case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE: |
| 2521 | dwc3_gadget_linksts_change_interrupt(dwc, event->event_info); |
| 2522 | break; |
| 2523 | case DWC3_DEVICE_EVENT_EOPF: |
| 2524 | dev_vdbg(dwc->dev, "End of Periodic Frame\n"); |
| 2525 | break; |
| 2526 | case DWC3_DEVICE_EVENT_SOF: |
| 2527 | dev_vdbg(dwc->dev, "Start of Periodic Frame\n"); |
| 2528 | break; |
| 2529 | case DWC3_DEVICE_EVENT_ERRATIC_ERROR: |
| 2530 | dev_vdbg(dwc->dev, "Erratic Error\n"); |
| 2531 | break; |
| 2532 | case DWC3_DEVICE_EVENT_CMD_CMPL: |
| 2533 | dev_vdbg(dwc->dev, "Command Complete\n"); |
| 2534 | break; |
| 2535 | case DWC3_DEVICE_EVENT_OVERFLOW: |
| 2536 | dev_vdbg(dwc->dev, "Overflow\n"); |
Pavankumar Kondeti | d393e17 | 2012-06-12 16:07:29 +0530 | [diff] [blame] | 2537 | /* |
| 2538 | * Controllers prior to 2.30a revision has a bug where |
| 2539 | * Overflow Event may overwrite an unacknowledged event |
| 2540 | * in the event buffer. The severity of the issue depends |
| 2541 | * on the overwritten event type. Add a warning message |
| 2542 | * saying that an event is overwritten. |
| 2543 | * |
| 2544 | * TODO: In future we may need to see if we can re-enumerate |
| 2545 | * with host. |
| 2546 | */ |
| 2547 | if (dwc->revision < DWC3_REVISION_230A) |
| 2548 | dev_warn(dwc->dev, "Unacknowledged event overwritten\n"); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2549 | break; |
Pavankumar Kondeti | 33fe6f1 | 2012-06-12 16:21:46 +0530 | [diff] [blame] | 2550 | case DWC3_DEVICE_EVENT_VENDOR_DEV_TEST_LMP: |
| 2551 | /* |
| 2552 | * Controllers prior to 2.30a revision has a bug, due to which |
| 2553 | * a vendor device test LMP event can not be filtered. But |
| 2554 | * this event is not handled in the current code. This is a |
| 2555 | * special event and 8 bytes of data will follow the event. |
| 2556 | * Handling this event is tricky when event buffer is almost |
| 2557 | * full. Moreover this event will not occur in normal scenario |
| 2558 | * and can only happen with special hosts in testing scenarios. |
| 2559 | * Add a warning message to indicate that this event is received |
| 2560 | * which means that event buffer might have corrupted. |
| 2561 | */ |
| 2562 | if (dwc->revision < DWC3_REVISION_230A) |
| 2563 | dev_warn(dwc->dev, "Vendor Device Test LMP Received\n"); |
| 2564 | break; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2565 | default: |
| 2566 | dev_dbg(dwc->dev, "UNKNOWN IRQ %d\n", event->type); |
| 2567 | } |
| 2568 | } |
| 2569 | |
| 2570 | static void dwc3_process_event_entry(struct dwc3 *dwc, |
| 2571 | const union dwc3_event *event) |
| 2572 | { |
| 2573 | /* Endpoint IRQ, handle it and return early */ |
| 2574 | if (event->type.is_devspec == 0) { |
| 2575 | /* depevt */ |
| 2576 | return dwc3_endpoint_interrupt(dwc, &event->depevt); |
| 2577 | } |
| 2578 | |
| 2579 | switch (event->type.type) { |
| 2580 | case DWC3_EVENT_TYPE_DEV: |
| 2581 | dwc3_gadget_interrupt(dwc, &event->devt); |
| 2582 | break; |
| 2583 | /* REVISIT what to do with Carkit and I2C events ? */ |
| 2584 | default: |
| 2585 | dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw); |
| 2586 | } |
| 2587 | } |
| 2588 | |
| 2589 | static irqreturn_t dwc3_process_event_buf(struct dwc3 *dwc, u32 buf) |
| 2590 | { |
| 2591 | struct dwc3_event_buffer *evt; |
| 2592 | int left; |
| 2593 | u32 count; |
| 2594 | |
| 2595 | count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(buf)); |
| 2596 | count &= DWC3_GEVNTCOUNT_MASK; |
| 2597 | if (!count) |
| 2598 | return IRQ_NONE; |
| 2599 | |
| 2600 | evt = dwc->ev_buffs[buf]; |
| 2601 | left = count; |
| 2602 | |
| 2603 | while (left > 0) { |
| 2604 | union dwc3_event event; |
| 2605 | |
Felipe Balbi | d70d844 | 2012-02-06 13:40:17 +0200 | [diff] [blame] | 2606 | event.raw = *(u32 *) (evt->buf + evt->lpos); |
| 2607 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2608 | dwc3_process_event_entry(dwc, &event); |
| 2609 | /* |
| 2610 | * XXX we wrap around correctly to the next entry as almost all |
| 2611 | * entries are 4 bytes in size. There is one entry which has 12 |
| 2612 | * bytes which is a regular entry followed by 8 bytes data. ATM |
| 2613 | * I don't know how things are organized if were get next to the |
| 2614 | * a boundary so I worry about that once we try to handle that. |
| 2615 | */ |
| 2616 | evt->lpos = (evt->lpos + 4) % DWC3_EVENT_BUFFERS_SIZE; |
| 2617 | left -= 4; |
| 2618 | |
| 2619 | dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(buf), 4); |
| 2620 | } |
| 2621 | |
| 2622 | return IRQ_HANDLED; |
| 2623 | } |
| 2624 | |
| 2625 | static irqreturn_t dwc3_interrupt(int irq, void *_dwc) |
| 2626 | { |
| 2627 | struct dwc3 *dwc = _dwc; |
| 2628 | int i; |
| 2629 | irqreturn_t ret = IRQ_NONE; |
| 2630 | |
| 2631 | spin_lock(&dwc->lock); |
| 2632 | |
Felipe Balbi | 9f622b2 | 2011-10-12 10:31:04 +0300 | [diff] [blame] | 2633 | for (i = 0; i < dwc->num_event_buffers; i++) { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2634 | irqreturn_t status; |
| 2635 | |
| 2636 | status = dwc3_process_event_buf(dwc, i); |
| 2637 | if (status == IRQ_HANDLED) |
| 2638 | ret = status; |
| 2639 | } |
| 2640 | |
| 2641 | spin_unlock(&dwc->lock); |
| 2642 | |
| 2643 | return ret; |
| 2644 | } |
| 2645 | |
| 2646 | /** |
| 2647 | * dwc3_gadget_init - Initializes gadget related registers |
Paul Zimmerman | 1d04679 | 2012-02-15 18:56:56 -0800 | [diff] [blame] | 2648 | * @dwc: pointer to our controller context structure |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2649 | * |
| 2650 | * Returns 0 on success otherwise negative errno. |
| 2651 | */ |
| 2652 | int __devinit dwc3_gadget_init(struct dwc3 *dwc) |
| 2653 | { |
| 2654 | u32 reg; |
| 2655 | int ret; |
| 2656 | int irq; |
| 2657 | |
| 2658 | dwc->ctrl_req = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ctrl_req), |
| 2659 | &dwc->ctrl_req_addr, GFP_KERNEL); |
| 2660 | if (!dwc->ctrl_req) { |
| 2661 | dev_err(dwc->dev, "failed to allocate ctrl request\n"); |
| 2662 | ret = -ENOMEM; |
| 2663 | goto err0; |
| 2664 | } |
| 2665 | |
| 2666 | dwc->ep0_trb = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ep0_trb), |
| 2667 | &dwc->ep0_trb_addr, GFP_KERNEL); |
| 2668 | if (!dwc->ep0_trb) { |
| 2669 | dev_err(dwc->dev, "failed to allocate ep0 trb\n"); |
| 2670 | ret = -ENOMEM; |
| 2671 | goto err1; |
| 2672 | } |
| 2673 | |
Felipe Balbi | b0791fb | 2012-05-04 12:58:14 +0300 | [diff] [blame] | 2674 | dwc->setup_buf = kzalloc(DWC3_EP0_BOUNCE_SIZE, GFP_KERNEL); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2675 | if (!dwc->setup_buf) { |
| 2676 | dev_err(dwc->dev, "failed to allocate setup buffer\n"); |
| 2677 | ret = -ENOMEM; |
| 2678 | goto err2; |
| 2679 | } |
| 2680 | |
Felipe Balbi | 5812b1c | 2011-08-27 22:07:53 +0300 | [diff] [blame] | 2681 | dwc->ep0_bounce = dma_alloc_coherent(dwc->dev, |
Felipe Balbi | b0791fb | 2012-05-04 12:58:14 +0300 | [diff] [blame] | 2682 | DWC3_EP0_BOUNCE_SIZE, &dwc->ep0_bounce_addr, |
| 2683 | GFP_KERNEL); |
Felipe Balbi | 5812b1c | 2011-08-27 22:07:53 +0300 | [diff] [blame] | 2684 | if (!dwc->ep0_bounce) { |
| 2685 | dev_err(dwc->dev, "failed to allocate ep0 bounce buffer\n"); |
| 2686 | ret = -ENOMEM; |
| 2687 | goto err3; |
| 2688 | } |
| 2689 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2690 | dev_set_name(&dwc->gadget.dev, "gadget"); |
| 2691 | |
| 2692 | dwc->gadget.ops = &dwc3_gadget_ops; |
Manu Gautam | a7b082a | 2012-11-06 09:50:09 +0530 | [diff] [blame] | 2693 | dwc->gadget.max_speed = USB_SPEED_SUPER; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2694 | dwc->gadget.speed = USB_SPEED_UNKNOWN; |
| 2695 | dwc->gadget.dev.parent = dwc->dev; |
Felipe Balbi | eeb720f | 2011-11-28 12:46:59 +0200 | [diff] [blame] | 2696 | dwc->gadget.sg_supported = true; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2697 | |
| 2698 | dma_set_coherent_mask(&dwc->gadget.dev, dwc->dev->coherent_dma_mask); |
| 2699 | |
| 2700 | dwc->gadget.dev.dma_parms = dwc->dev->dma_parms; |
| 2701 | dwc->gadget.dev.dma_mask = dwc->dev->dma_mask; |
| 2702 | dwc->gadget.dev.release = dwc3_gadget_release; |
| 2703 | dwc->gadget.name = "dwc3-gadget"; |
| 2704 | |
| 2705 | /* |
| 2706 | * REVISIT: Here we should clear all pending IRQs to be |
| 2707 | * sure we're starting from a well known location. |
| 2708 | */ |
| 2709 | |
| 2710 | ret = dwc3_gadget_init_endpoints(dwc); |
| 2711 | if (ret) |
Felipe Balbi | 5812b1c | 2011-08-27 22:07:53 +0300 | [diff] [blame] | 2712 | goto err4; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2713 | |
| 2714 | irq = platform_get_irq(to_platform_device(dwc->dev), 0); |
| 2715 | |
| 2716 | ret = request_irq(irq, dwc3_interrupt, IRQF_SHARED, |
| 2717 | "dwc3", dwc); |
| 2718 | if (ret) { |
| 2719 | dev_err(dwc->dev, "failed to request irq #%d --> %d\n", |
| 2720 | irq, ret); |
Felipe Balbi | 5812b1c | 2011-08-27 22:07:53 +0300 | [diff] [blame] | 2721 | goto err5; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2722 | } |
| 2723 | |
Sebastian Andrzej Siewior | bb8b8a3 | 2011-09-13 17:54:39 +0200 | [diff] [blame] | 2724 | reg = dwc3_readl(dwc->regs, DWC3_DCFG); |
| 2725 | reg |= DWC3_DCFG_LPM_CAP; |
| 2726 | dwc3_writel(dwc->regs, DWC3_DCFG, reg); |
| 2727 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2728 | /* Enable all but Start and End of Frame IRQs */ |
Pavankumar Kondeti | 33fe6f1 | 2012-06-12 16:21:46 +0530 | [diff] [blame] | 2729 | reg = (DWC3_DEVTEN_EVNTOVERFLOWEN | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2730 | DWC3_DEVTEN_CMDCMPLTEN | |
| 2731 | DWC3_DEVTEN_ERRTICERREN | |
| 2732 | DWC3_DEVTEN_WKUPEVTEN | |
| 2733 | DWC3_DEVTEN_ULSTCNGEN | |
| 2734 | DWC3_DEVTEN_CONNECTDONEEN | |
| 2735 | DWC3_DEVTEN_USBRSTEN | |
| 2736 | DWC3_DEVTEN_DISCONNEVTEN); |
| 2737 | dwc3_writel(dwc->regs, DWC3_DEVTEN, reg); |
| 2738 | |
Paul Zimmerman | 88df427 | 2012-04-27 13:10:52 +0300 | [diff] [blame] | 2739 | /* Enable USB2 LPM and automatic phy suspend only on recent versions */ |
| 2740 | if (dwc->revision >= DWC3_REVISION_194A) { |
| 2741 | reg = dwc3_readl(dwc->regs, DWC3_DCFG); |
| 2742 | reg |= DWC3_DCFG_LPM_CAP; |
| 2743 | dwc3_writel(dwc->regs, DWC3_DCFG, reg); |
| 2744 | |
| 2745 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
| 2746 | reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN); |
| 2747 | |
| 2748 | /* TODO: This should be configurable */ |
Pratyush Anand | d69dcdd | 2012-07-02 10:21:52 +0530 | [diff] [blame] | 2749 | reg |= DWC3_DCTL_HIRD_THRES(28); |
Paul Zimmerman | 88df427 | 2012-04-27 13:10:52 +0300 | [diff] [blame] | 2750 | |
| 2751 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); |
| 2752 | |
Pratyush Anand | 50ed834 | 2012-06-06 19:36:17 +0530 | [diff] [blame] | 2753 | dwc3_gadget_usb2_phy_suspend(dwc, false); |
| 2754 | dwc3_gadget_usb3_phy_suspend(dwc, false); |
Paul Zimmerman | 88df427 | 2012-04-27 13:10:52 +0300 | [diff] [blame] | 2755 | } |
| 2756 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2757 | ret = device_register(&dwc->gadget.dev); |
| 2758 | if (ret) { |
| 2759 | dev_err(dwc->dev, "failed to register gadget device\n"); |
| 2760 | put_device(&dwc->gadget.dev); |
Felipe Balbi | 5812b1c | 2011-08-27 22:07:53 +0300 | [diff] [blame] | 2761 | goto err6; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2762 | } |
| 2763 | |
| 2764 | ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget); |
| 2765 | if (ret) { |
| 2766 | dev_err(dwc->dev, "failed to register udc\n"); |
Felipe Balbi | 5812b1c | 2011-08-27 22:07:53 +0300 | [diff] [blame] | 2767 | goto err7; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2768 | } |
| 2769 | |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 2770 | if (dwc->dotg) { |
| 2771 | /* dwc3 otg driver is active (DRD mode + SRPSupport=1) */ |
| 2772 | ret = otg_set_peripheral(&dwc->dotg->otg, &dwc->gadget); |
| 2773 | if (ret) { |
| 2774 | dev_err(dwc->dev, "failed to set peripheral to otg\n"); |
| 2775 | goto err7; |
| 2776 | } |
Manu Gautam | b506727 | 2012-07-02 09:53:41 +0530 | [diff] [blame] | 2777 | } else { |
| 2778 | pm_runtime_no_callbacks(&dwc->gadget.dev); |
| 2779 | pm_runtime_set_active(&dwc->gadget.dev); |
| 2780 | pm_runtime_enable(&dwc->gadget.dev); |
| 2781 | pm_runtime_get(&dwc->gadget.dev); |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 2782 | } |
| 2783 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2784 | return 0; |
| 2785 | |
Felipe Balbi | 5812b1c | 2011-08-27 22:07:53 +0300 | [diff] [blame] | 2786 | err7: |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2787 | device_unregister(&dwc->gadget.dev); |
| 2788 | |
Felipe Balbi | 5812b1c | 2011-08-27 22:07:53 +0300 | [diff] [blame] | 2789 | err6: |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2790 | dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00); |
| 2791 | free_irq(irq, dwc); |
| 2792 | |
Felipe Balbi | 5812b1c | 2011-08-27 22:07:53 +0300 | [diff] [blame] | 2793 | err5: |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2794 | dwc3_gadget_free_endpoints(dwc); |
| 2795 | |
Felipe Balbi | 5812b1c | 2011-08-27 22:07:53 +0300 | [diff] [blame] | 2796 | err4: |
Felipe Balbi | b0791fb | 2012-05-04 12:58:14 +0300 | [diff] [blame] | 2797 | dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE, |
| 2798 | dwc->ep0_bounce, dwc->ep0_bounce_addr); |
Felipe Balbi | 5812b1c | 2011-08-27 22:07:53 +0300 | [diff] [blame] | 2799 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2800 | err3: |
Felipe Balbi | 0fc9a1b | 2011-12-19 11:32:34 +0200 | [diff] [blame] | 2801 | kfree(dwc->setup_buf); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2802 | |
| 2803 | err2: |
| 2804 | dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb), |
| 2805 | dwc->ep0_trb, dwc->ep0_trb_addr); |
| 2806 | |
| 2807 | err1: |
| 2808 | dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req), |
| 2809 | dwc->ctrl_req, dwc->ctrl_req_addr); |
| 2810 | |
| 2811 | err0: |
| 2812 | return ret; |
| 2813 | } |
| 2814 | |
| 2815 | void dwc3_gadget_exit(struct dwc3 *dwc) |
| 2816 | { |
| 2817 | int irq; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2818 | |
Manu Gautam | b506727 | 2012-07-02 09:53:41 +0530 | [diff] [blame] | 2819 | if (dwc->dotg) { |
| 2820 | pm_runtime_put(&dwc->gadget.dev); |
| 2821 | pm_runtime_disable(&dwc->gadget.dev); |
| 2822 | } |
| 2823 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2824 | usb_del_gadget_udc(&dwc->gadget); |
| 2825 | irq = platform_get_irq(to_platform_device(dwc->dev), 0); |
| 2826 | |
| 2827 | dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00); |
| 2828 | free_irq(irq, dwc); |
| 2829 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2830 | dwc3_gadget_free_endpoints(dwc); |
| 2831 | |
Felipe Balbi | b0791fb | 2012-05-04 12:58:14 +0300 | [diff] [blame] | 2832 | dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE, |
| 2833 | dwc->ep0_bounce, dwc->ep0_bounce_addr); |
Felipe Balbi | 5812b1c | 2011-08-27 22:07:53 +0300 | [diff] [blame] | 2834 | |
Felipe Balbi | 0fc9a1b | 2011-12-19 11:32:34 +0200 | [diff] [blame] | 2835 | kfree(dwc->setup_buf); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2836 | |
| 2837 | dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb), |
| 2838 | dwc->ep0_trb, dwc->ep0_trb_addr); |
| 2839 | |
| 2840 | dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req), |
| 2841 | dwc->ctrl_req, dwc->ctrl_req_addr); |
| 2842 | |
| 2843 | device_unregister(&dwc->gadget.dev); |
| 2844 | } |