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