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