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