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