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