1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * gadget.c - DesignWare USB3 DRD Controller Gadget Framework Link 4 * 5 * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com 6 * 7 * Authors: Felipe Balbi <balbi@ti.com>, 8 * Sebastian Andrzej Siewior <bigeasy@linutronix.de> 9 */ 10 11 #include <linux/kernel.h> 12 #include <linux/delay.h> 13 #include <linux/slab.h> 14 #include <linux/spinlock.h> 15 #include <linux/platform_device.h> 16 #include <linux/pm_runtime.h> 17 #include <linux/interrupt.h> 18 #include <linux/io.h> 19 #include <linux/list.h> 20 #include <linux/dma-mapping.h> 21 22 #include <linux/usb/ch9.h> 23 #include <linux/usb/gadget.h> 24 25 #include "debug.h" 26 #include "core.h" 27 #include "gadget.h" 28 #include "io.h" 29 30 #define DWC3_ALIGN_FRAME(d) (((d)->frame_number + (d)->interval) \ 31 & ~((d)->interval - 1)) 32 33 /** 34 * dwc3_gadget_set_test_mode - enables usb2 test modes 35 * @dwc: pointer to our context structure 36 * @mode: the mode to set (J, K SE0 NAK, Force Enable) 37 * 38 * Caller should take care of locking. This function will return 0 on 39 * success or -EINVAL if wrong Test Selector is passed. 40 */ 41 int dwc3_gadget_set_test_mode(struct dwc3 *dwc, int mode) 42 { 43 u32 reg; 44 45 reg = dwc3_readl(dwc->regs, DWC3_DCTL); 46 reg &= ~DWC3_DCTL_TSTCTRL_MASK; 47 48 switch (mode) { 49 case TEST_J: 50 case TEST_K: 51 case TEST_SE0_NAK: 52 case TEST_PACKET: 53 case TEST_FORCE_EN: 54 reg |= mode << 1; 55 break; 56 default: 57 return -EINVAL; 58 } 59 60 dwc3_writel(dwc->regs, DWC3_DCTL, reg); 61 62 return 0; 63 } 64 65 /** 66 * dwc3_gadget_get_link_state - gets current state of usb link 67 * @dwc: pointer to our context structure 68 * 69 * Caller should take care of locking. This function will 70 * return the link state on success (>= 0) or -ETIMEDOUT. 71 */ 72 int dwc3_gadget_get_link_state(struct dwc3 *dwc) 73 { 74 u32 reg; 75 76 reg = dwc3_readl(dwc->regs, DWC3_DSTS); 77 78 return DWC3_DSTS_USBLNKST(reg); 79 } 80 81 /** 82 * dwc3_gadget_set_link_state - sets usb link to a particular state 83 * @dwc: pointer to our context structure 84 * @state: the state to put link into 85 * 86 * Caller should take care of locking. This function will 87 * return 0 on success or -ETIMEDOUT. 88 */ 89 int dwc3_gadget_set_link_state(struct dwc3 *dwc, enum dwc3_link_state state) 90 { 91 int retries = 10000; 92 u32 reg; 93 94 /* 95 * Wait until device controller is ready. Only applies to 1.94a and 96 * later RTL. 97 */ 98 if (dwc->revision >= DWC3_REVISION_194A) { 99 while (--retries) { 100 reg = dwc3_readl(dwc->regs, DWC3_DSTS); 101 if (reg & DWC3_DSTS_DCNRD) 102 udelay(5); 103 else 104 break; 105 } 106 107 if (retries <= 0) 108 return -ETIMEDOUT; 109 } 110 111 reg = dwc3_readl(dwc->regs, DWC3_DCTL); 112 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK; 113 114 /* set requested state */ 115 reg |= DWC3_DCTL_ULSTCHNGREQ(state); 116 dwc3_writel(dwc->regs, DWC3_DCTL, reg); 117 118 /* 119 * The following code is racy when called from dwc3_gadget_wakeup, 120 * and is not needed, at least on newer versions 121 */ 122 if (dwc->revision >= DWC3_REVISION_194A) 123 return 0; 124 125 /* wait for a change in DSTS */ 126 retries = 10000; 127 while (--retries) { 128 reg = dwc3_readl(dwc->regs, DWC3_DSTS); 129 130 if (DWC3_DSTS_USBLNKST(reg) == state) 131 return 0; 132 133 udelay(5); 134 } 135 136 return -ETIMEDOUT; 137 } 138 139 /** 140 * dwc3_ep_inc_trb - increment a trb index. 141 * @index: Pointer to the TRB index to increment. 142 * 143 * The index should never point to the link TRB. After incrementing, 144 * if it is point to the link TRB, wrap around to the beginning. The 145 * link TRB is always at the last TRB entry. 146 */ 147 static void dwc3_ep_inc_trb(u8 *index) 148 { 149 (*index)++; 150 if (*index == (DWC3_TRB_NUM - 1)) 151 *index = 0; 152 } 153 154 /** 155 * dwc3_ep_inc_enq - increment endpoint's enqueue pointer 156 * @dep: The endpoint whose enqueue pointer we're incrementing 157 */ 158 static void dwc3_ep_inc_enq(struct dwc3_ep *dep) 159 { 160 dwc3_ep_inc_trb(&dep->trb_enqueue); 161 } 162 163 /** 164 * dwc3_ep_inc_deq - increment endpoint's dequeue pointer 165 * @dep: The endpoint whose enqueue pointer we're incrementing 166 */ 167 static void dwc3_ep_inc_deq(struct dwc3_ep *dep) 168 { 169 dwc3_ep_inc_trb(&dep->trb_dequeue); 170 } 171 172 static void dwc3_gadget_del_and_unmap_request(struct dwc3_ep *dep, 173 struct dwc3_request *req, int status) 174 { 175 struct dwc3 *dwc = dep->dwc; 176 177 req->started = false; 178 list_del(&req->list); 179 req->remaining = 0; 180 181 if (req->request.status == -EINPROGRESS) 182 req->request.status = status; 183 184 if (req->trb) 185 usb_gadget_unmap_request_by_dev(dwc->sysdev, 186 &req->request, req->direction); 187 188 req->trb = NULL; 189 trace_dwc3_gadget_giveback(req); 190 191 if (dep->number > 1) 192 pm_runtime_put(dwc->dev); 193 } 194 195 /** 196 * dwc3_gadget_giveback - call struct usb_request's ->complete callback 197 * @dep: The endpoint to whom the request belongs to 198 * @req: The request we're giving back 199 * @status: completion code for the request 200 * 201 * Must be called with controller's lock held and interrupts disabled. This 202 * function will unmap @req and call its ->complete() callback to notify upper 203 * layers that it has completed. 204 */ 205 void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req, 206 int status) 207 { 208 struct dwc3 *dwc = dep->dwc; 209 210 dwc3_gadget_del_and_unmap_request(dep, req, status); 211 212 spin_unlock(&dwc->lock); 213 usb_gadget_giveback_request(&dep->endpoint, &req->request); 214 spin_lock(&dwc->lock); 215 } 216 217 /** 218 * dwc3_send_gadget_generic_command - issue a generic command for the controller 219 * @dwc: pointer to the controller context 220 * @cmd: the command to be issued 221 * @param: command parameter 222 * 223 * Caller should take care of locking. Issue @cmd with a given @param to @dwc 224 * and wait for its completion. 225 */ 226 int dwc3_send_gadget_generic_command(struct dwc3 *dwc, unsigned cmd, u32 param) 227 { 228 u32 timeout = 500; 229 int status = 0; 230 int ret = 0; 231 u32 reg; 232 233 dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param); 234 dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT); 235 236 do { 237 reg = dwc3_readl(dwc->regs, DWC3_DGCMD); 238 if (!(reg & DWC3_DGCMD_CMDACT)) { 239 status = DWC3_DGCMD_STATUS(reg); 240 if (status) 241 ret = -EINVAL; 242 break; 243 } 244 } while (--timeout); 245 246 if (!timeout) { 247 ret = -ETIMEDOUT; 248 status = -ETIMEDOUT; 249 } 250 251 trace_dwc3_gadget_generic_cmd(cmd, param, status); 252 253 return ret; 254 } 255 256 static int __dwc3_gadget_wakeup(struct dwc3 *dwc); 257 258 /** 259 * dwc3_send_gadget_ep_cmd - issue an endpoint command 260 * @dep: the endpoint to which the command is going to be issued 261 * @cmd: the command to be issued 262 * @params: parameters to the command 263 * 264 * Caller should handle locking. This function will issue @cmd with given 265 * @params to @dep and wait for its completion. 266 */ 267 int dwc3_send_gadget_ep_cmd(struct dwc3_ep *dep, unsigned cmd, 268 struct dwc3_gadget_ep_cmd_params *params) 269 { 270 const struct usb_endpoint_descriptor *desc = dep->endpoint.desc; 271 struct dwc3 *dwc = dep->dwc; 272 u32 timeout = 1000; 273 u32 reg; 274 275 int cmd_status = 0; 276 int susphy = false; 277 int ret = -EINVAL; 278 279 /* 280 * Synopsys Databook 2.60a states, on section 6.3.2.5.[1-8], that if 281 * we're issuing an endpoint command, we must check if 282 * GUSB2PHYCFG.SUSPHY bit is set. If it is, then we need to clear it. 283 * 284 * We will also set SUSPHY bit to what it was before returning as stated 285 * by the same section on Synopsys databook. 286 */ 287 if (dwc->gadget.speed <= USB_SPEED_HIGH) { 288 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0)); 289 if (unlikely(reg & DWC3_GUSB2PHYCFG_SUSPHY)) { 290 susphy = true; 291 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY; 292 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg); 293 } 294 } 295 296 if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_STARTTRANSFER) { 297 int needs_wakeup; 298 299 needs_wakeup = (dwc->link_state == DWC3_LINK_STATE_U1 || 300 dwc->link_state == DWC3_LINK_STATE_U2 || 301 dwc->link_state == DWC3_LINK_STATE_U3); 302 303 if (unlikely(needs_wakeup)) { 304 ret = __dwc3_gadget_wakeup(dwc); 305 dev_WARN_ONCE(dwc->dev, ret, "wakeup failed --> %d\n", 306 ret); 307 } 308 } 309 310 dwc3_writel(dep->regs, DWC3_DEPCMDPAR0, params->param0); 311 dwc3_writel(dep->regs, DWC3_DEPCMDPAR1, params->param1); 312 dwc3_writel(dep->regs, DWC3_DEPCMDPAR2, params->param2); 313 314 /* 315 * Synopsys Databook 2.60a states in section 6.3.2.5.6 of that if we're 316 * not relying on XferNotReady, we can make use of a special "No 317 * Response Update Transfer" command where we should clear both CmdAct 318 * and CmdIOC bits. 319 * 320 * With this, we don't need to wait for command completion and can 321 * straight away issue further commands to the endpoint. 322 * 323 * NOTICE: We're making an assumption that control endpoints will never 324 * make use of Update Transfer command. This is a safe assumption 325 * because we can never have more than one request at a time with 326 * Control Endpoints. If anybody changes that assumption, this chunk 327 * needs to be updated accordingly. 328 */ 329 if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_UPDATETRANSFER && 330 !usb_endpoint_xfer_isoc(desc)) 331 cmd &= ~(DWC3_DEPCMD_CMDIOC | DWC3_DEPCMD_CMDACT); 332 else 333 cmd |= DWC3_DEPCMD_CMDACT; 334 335 dwc3_writel(dep->regs, DWC3_DEPCMD, cmd); 336 do { 337 reg = dwc3_readl(dep->regs, DWC3_DEPCMD); 338 if (!(reg & DWC3_DEPCMD_CMDACT)) { 339 cmd_status = DWC3_DEPCMD_STATUS(reg); 340 341 switch (cmd_status) { 342 case 0: 343 ret = 0; 344 break; 345 case DEPEVT_TRANSFER_NO_RESOURCE: 346 ret = -EINVAL; 347 break; 348 case DEPEVT_TRANSFER_BUS_EXPIRY: 349 /* 350 * SW issues START TRANSFER command to 351 * isochronous ep with future frame interval. If 352 * future interval time has already passed when 353 * core receives the command, it will respond 354 * with an error status of 'Bus Expiry'. 355 * 356 * Instead of always returning -EINVAL, let's 357 * give a hint to the gadget driver that this is 358 * the case by returning -EAGAIN. 359 */ 360 ret = -EAGAIN; 361 break; 362 default: 363 dev_WARN(dwc->dev, "UNKNOWN cmd status\n"); 364 } 365 366 break; 367 } 368 } while (--timeout); 369 370 if (timeout == 0) { 371 ret = -ETIMEDOUT; 372 cmd_status = -ETIMEDOUT; 373 } 374 375 trace_dwc3_gadget_ep_cmd(dep, cmd, params, cmd_status); 376 377 if (ret == 0) { 378 switch (DWC3_DEPCMD_CMD(cmd)) { 379 case DWC3_DEPCMD_STARTTRANSFER: 380 dep->flags |= DWC3_EP_TRANSFER_STARTED; 381 dwc3_gadget_ep_get_transfer_index(dep); 382 break; 383 case DWC3_DEPCMD_ENDTRANSFER: 384 dep->flags &= ~DWC3_EP_TRANSFER_STARTED; 385 break; 386 default: 387 /* nothing */ 388 break; 389 } 390 } 391 392 if (unlikely(susphy)) { 393 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0)); 394 reg |= DWC3_GUSB2PHYCFG_SUSPHY; 395 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg); 396 } 397 398 return ret; 399 } 400 401 static int dwc3_send_clear_stall_ep_cmd(struct dwc3_ep *dep) 402 { 403 struct dwc3 *dwc = dep->dwc; 404 struct dwc3_gadget_ep_cmd_params params; 405 u32 cmd = DWC3_DEPCMD_CLEARSTALL; 406 407 /* 408 * As of core revision 2.60a the recommended programming model 409 * is to set the ClearPendIN bit when issuing a Clear Stall EP 410 * command for IN endpoints. This is to prevent an issue where 411 * some (non-compliant) hosts may not send ACK TPs for pending 412 * IN transfers due to a mishandled error condition. Synopsys 413 * STAR 9000614252. 414 */ 415 if (dep->direction && (dwc->revision >= DWC3_REVISION_260A) && 416 (dwc->gadget.speed >= USB_SPEED_SUPER)) 417 cmd |= DWC3_DEPCMD_CLEARPENDIN; 418 419 memset(¶ms, 0, sizeof(params)); 420 421 return dwc3_send_gadget_ep_cmd(dep, cmd, ¶ms); 422 } 423 424 static dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep, 425 struct dwc3_trb *trb) 426 { 427 u32 offset = (char *) trb - (char *) dep->trb_pool; 428 429 return dep->trb_pool_dma + offset; 430 } 431 432 static int dwc3_alloc_trb_pool(struct dwc3_ep *dep) 433 { 434 struct dwc3 *dwc = dep->dwc; 435 436 if (dep->trb_pool) 437 return 0; 438 439 dep->trb_pool = dma_alloc_coherent(dwc->sysdev, 440 sizeof(struct dwc3_trb) * DWC3_TRB_NUM, 441 &dep->trb_pool_dma, GFP_KERNEL); 442 if (!dep->trb_pool) { 443 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n", 444 dep->name); 445 return -ENOMEM; 446 } 447 448 return 0; 449 } 450 451 static void dwc3_free_trb_pool(struct dwc3_ep *dep) 452 { 453 struct dwc3 *dwc = dep->dwc; 454 455 dma_free_coherent(dwc->sysdev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM, 456 dep->trb_pool, dep->trb_pool_dma); 457 458 dep->trb_pool = NULL; 459 dep->trb_pool_dma = 0; 460 } 461 462 static int dwc3_gadget_set_xfer_resource(struct dwc3_ep *dep) 463 { 464 struct dwc3_gadget_ep_cmd_params params; 465 466 memset(¶ms, 0x00, sizeof(params)); 467 468 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1); 469 470 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETTRANSFRESOURCE, 471 ¶ms); 472 } 473 474 /** 475 * dwc3_gadget_start_config - configure ep resources 476 * @dwc: pointer to our controller context structure 477 * @dep: endpoint that is being enabled 478 * 479 * Issue a %DWC3_DEPCMD_DEPSTARTCFG command to @dep. After the command's 480 * completion, it will set Transfer Resource for all available endpoints. 481 * 482 * The assignment of transfer resources cannot perfectly follow the data book 483 * due to the fact that the controller driver does not have all knowledge of the 484 * configuration in advance. It is given this information piecemeal by the 485 * composite gadget framework after every SET_CONFIGURATION and 486 * SET_INTERFACE. Trying to follow the databook programming model in this 487 * scenario can cause errors. For two reasons: 488 * 489 * 1) The databook says to do %DWC3_DEPCMD_DEPSTARTCFG for every 490 * %USB_REQ_SET_CONFIGURATION and %USB_REQ_SET_INTERFACE (8.1.5). This is 491 * incorrect in the scenario of multiple interfaces. 492 * 493 * 2) The databook does not mention doing more %DWC3_DEPCMD_DEPXFERCFG for new 494 * endpoint on alt setting (8.1.6). 495 * 496 * The following simplified method is used instead: 497 * 498 * All hardware endpoints can be assigned a transfer resource and this setting 499 * will stay persistent until either a core reset or hibernation. So whenever we 500 * do a %DWC3_DEPCMD_DEPSTARTCFG(0) we can go ahead and do 501 * %DWC3_DEPCMD_DEPXFERCFG for every hardware endpoint as well. We are 502 * guaranteed that there are as many transfer resources as endpoints. 503 * 504 * This function is called for each endpoint when it is being enabled but is 505 * triggered only when called for EP0-out, which always happens first, and which 506 * should only happen in one of the above conditions. 507 */ 508 static int dwc3_gadget_start_config(struct dwc3_ep *dep) 509 { 510 struct dwc3_gadget_ep_cmd_params params; 511 struct dwc3 *dwc; 512 u32 cmd; 513 int i; 514 int ret; 515 516 if (dep->number) 517 return 0; 518 519 memset(¶ms, 0x00, sizeof(params)); 520 cmd = DWC3_DEPCMD_DEPSTARTCFG; 521 dwc = dep->dwc; 522 523 ret = dwc3_send_gadget_ep_cmd(dep, cmd, ¶ms); 524 if (ret) 525 return ret; 526 527 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) { 528 struct dwc3_ep *dep = dwc->eps[i]; 529 530 if (!dep) 531 continue; 532 533 ret = dwc3_gadget_set_xfer_resource(dep); 534 if (ret) 535 return ret; 536 } 537 538 return 0; 539 } 540 541 static int dwc3_gadget_set_ep_config(struct dwc3_ep *dep, unsigned int action) 542 { 543 const struct usb_ss_ep_comp_descriptor *comp_desc; 544 const struct usb_endpoint_descriptor *desc; 545 struct dwc3_gadget_ep_cmd_params params; 546 struct dwc3 *dwc = dep->dwc; 547 548 comp_desc = dep->endpoint.comp_desc; 549 desc = dep->endpoint.desc; 550 551 memset(¶ms, 0x00, sizeof(params)); 552 553 params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc)) 554 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc)); 555 556 /* Burst size is only needed in SuperSpeed mode */ 557 if (dwc->gadget.speed >= USB_SPEED_SUPER) { 558 u32 burst = dep->endpoint.maxburst; 559 params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst - 1); 560 } 561 562 params.param0 |= action; 563 if (action == DWC3_DEPCFG_ACTION_RESTORE) 564 params.param2 |= dep->saved_state; 565 566 if (usb_endpoint_xfer_control(desc)) 567 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN; 568 569 if (dep->number <= 1 || usb_endpoint_xfer_isoc(desc)) 570 params.param1 |= DWC3_DEPCFG_XFER_NOT_READY_EN; 571 572 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) { 573 params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE 574 | DWC3_DEPCFG_STREAM_EVENT_EN; 575 dep->stream_capable = true; 576 } 577 578 if (!usb_endpoint_xfer_control(desc)) 579 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN; 580 581 /* 582 * We are doing 1:1 mapping for endpoints, meaning 583 * Physical Endpoints 2 maps to Logical Endpoint 2 and 584 * so on. We consider the direction bit as part of the physical 585 * endpoint number. So USB endpoint 0x81 is 0x03. 586 */ 587 params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number); 588 589 /* 590 * We must use the lower 16 TX FIFOs even though 591 * HW might have more 592 */ 593 if (dep->direction) 594 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1); 595 596 if (desc->bInterval) { 597 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1); 598 dep->interval = 1 << (desc->bInterval - 1); 599 } 600 601 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETEPCONFIG, ¶ms); 602 } 603 604 /** 605 * __dwc3_gadget_ep_enable - initializes a hw endpoint 606 * @dep: endpoint to be initialized 607 * @action: one of INIT, MODIFY or RESTORE 608 * 609 * Caller should take care of locking. Execute all necessary commands to 610 * initialize a HW endpoint so it can be used by a gadget driver. 611 */ 612 static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep, unsigned int action) 613 { 614 const struct usb_endpoint_descriptor *desc = dep->endpoint.desc; 615 struct dwc3 *dwc = dep->dwc; 616 617 u32 reg; 618 int ret; 619 620 if (!(dep->flags & DWC3_EP_ENABLED)) { 621 ret = dwc3_gadget_start_config(dep); 622 if (ret) 623 return ret; 624 } 625 626 ret = dwc3_gadget_set_ep_config(dep, action); 627 if (ret) 628 return ret; 629 630 if (!(dep->flags & DWC3_EP_ENABLED)) { 631 struct dwc3_trb *trb_st_hw; 632 struct dwc3_trb *trb_link; 633 634 dep->type = usb_endpoint_type(desc); 635 dep->flags |= DWC3_EP_ENABLED; 636 dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING; 637 638 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA); 639 reg |= DWC3_DALEPENA_EP(dep->number); 640 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg); 641 642 init_waitqueue_head(&dep->wait_end_transfer); 643 644 if (usb_endpoint_xfer_control(desc)) 645 goto out; 646 647 /* Initialize the TRB ring */ 648 dep->trb_dequeue = 0; 649 dep->trb_enqueue = 0; 650 memset(dep->trb_pool, 0, 651 sizeof(struct dwc3_trb) * DWC3_TRB_NUM); 652 653 /* Link TRB. The HWO bit is never reset */ 654 trb_st_hw = &dep->trb_pool[0]; 655 656 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1]; 657 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw)); 658 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw)); 659 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB; 660 trb_link->ctrl |= DWC3_TRB_CTRL_HWO; 661 } 662 663 /* 664 * Issue StartTransfer here with no-op TRB so we can always rely on No 665 * Response Update Transfer command. 666 */ 667 if (usb_endpoint_xfer_bulk(desc) || 668 usb_endpoint_xfer_int(desc)) { 669 struct dwc3_gadget_ep_cmd_params params; 670 struct dwc3_trb *trb; 671 dma_addr_t trb_dma; 672 u32 cmd; 673 674 memset(¶ms, 0, sizeof(params)); 675 trb = &dep->trb_pool[0]; 676 trb_dma = dwc3_trb_dma_offset(dep, trb); 677 678 params.param0 = upper_32_bits(trb_dma); 679 params.param1 = lower_32_bits(trb_dma); 680 681 cmd = DWC3_DEPCMD_STARTTRANSFER; 682 683 ret = dwc3_send_gadget_ep_cmd(dep, cmd, ¶ms); 684 if (ret < 0) 685 return ret; 686 } 687 688 out: 689 trace_dwc3_gadget_ep_enable(dep); 690 691 return 0; 692 } 693 694 static void dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force); 695 static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep) 696 { 697 struct dwc3_request *req; 698 699 dwc3_stop_active_transfer(dep, true); 700 701 /* - giveback all requests to gadget driver */ 702 while (!list_empty(&dep->started_list)) { 703 req = next_request(&dep->started_list); 704 705 dwc3_gadget_giveback(dep, req, -ESHUTDOWN); 706 } 707 708 while (!list_empty(&dep->pending_list)) { 709 req = next_request(&dep->pending_list); 710 711 dwc3_gadget_giveback(dep, req, -ESHUTDOWN); 712 } 713 } 714 715 /** 716 * __dwc3_gadget_ep_disable - disables a hw endpoint 717 * @dep: the endpoint to disable 718 * 719 * This function undoes what __dwc3_gadget_ep_enable did and also removes 720 * requests which are currently being processed by the hardware and those which 721 * are not yet scheduled. 722 * 723 * Caller should take care of locking. 724 */ 725 static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep) 726 { 727 struct dwc3 *dwc = dep->dwc; 728 u32 reg; 729 730 trace_dwc3_gadget_ep_disable(dep); 731 732 dwc3_remove_requests(dwc, dep); 733 734 /* make sure HW endpoint isn't stalled */ 735 if (dep->flags & DWC3_EP_STALL) 736 __dwc3_gadget_ep_set_halt(dep, 0, false); 737 738 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA); 739 reg &= ~DWC3_DALEPENA_EP(dep->number); 740 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg); 741 742 dep->stream_capable = false; 743 dep->type = 0; 744 dep->flags &= DWC3_EP_END_TRANSFER_PENDING; 745 746 /* Clear out the ep descriptors for non-ep0 */ 747 if (dep->number > 1) { 748 dep->endpoint.comp_desc = NULL; 749 dep->endpoint.desc = NULL; 750 } 751 752 return 0; 753 } 754 755 /* -------------------------------------------------------------------------- */ 756 757 static int dwc3_gadget_ep0_enable(struct usb_ep *ep, 758 const struct usb_endpoint_descriptor *desc) 759 { 760 return -EINVAL; 761 } 762 763 static int dwc3_gadget_ep0_disable(struct usb_ep *ep) 764 { 765 return -EINVAL; 766 } 767 768 /* -------------------------------------------------------------------------- */ 769 770 static int dwc3_gadget_ep_enable(struct usb_ep *ep, 771 const struct usb_endpoint_descriptor *desc) 772 { 773 struct dwc3_ep *dep; 774 struct dwc3 *dwc; 775 unsigned long flags; 776 int ret; 777 778 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) { 779 pr_debug("dwc3: invalid parameters\n"); 780 return -EINVAL; 781 } 782 783 if (!desc->wMaxPacketSize) { 784 pr_debug("dwc3: missing wMaxPacketSize\n"); 785 return -EINVAL; 786 } 787 788 dep = to_dwc3_ep(ep); 789 dwc = dep->dwc; 790 791 if (dev_WARN_ONCE(dwc->dev, dep->flags & DWC3_EP_ENABLED, 792 "%s is already enabled\n", 793 dep->name)) 794 return 0; 795 796 spin_lock_irqsave(&dwc->lock, flags); 797 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT); 798 spin_unlock_irqrestore(&dwc->lock, flags); 799 800 return ret; 801 } 802 803 static int dwc3_gadget_ep_disable(struct usb_ep *ep) 804 { 805 struct dwc3_ep *dep; 806 struct dwc3 *dwc; 807 unsigned long flags; 808 int ret; 809 810 if (!ep) { 811 pr_debug("dwc3: invalid parameters\n"); 812 return -EINVAL; 813 } 814 815 dep = to_dwc3_ep(ep); 816 dwc = dep->dwc; 817 818 if (dev_WARN_ONCE(dwc->dev, !(dep->flags & DWC3_EP_ENABLED), 819 "%s is already disabled\n", 820 dep->name)) 821 return 0; 822 823 spin_lock_irqsave(&dwc->lock, flags); 824 ret = __dwc3_gadget_ep_disable(dep); 825 spin_unlock_irqrestore(&dwc->lock, flags); 826 827 return ret; 828 } 829 830 static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep, 831 gfp_t gfp_flags) 832 { 833 struct dwc3_request *req; 834 struct dwc3_ep *dep = to_dwc3_ep(ep); 835 836 req = kzalloc(sizeof(*req), gfp_flags); 837 if (!req) 838 return NULL; 839 840 req->direction = dep->direction; 841 req->epnum = dep->number; 842 req->dep = dep; 843 844 trace_dwc3_alloc_request(req); 845 846 return &req->request; 847 } 848 849 static void dwc3_gadget_ep_free_request(struct usb_ep *ep, 850 struct usb_request *request) 851 { 852 struct dwc3_request *req = to_dwc3_request(request); 853 854 trace_dwc3_free_request(req); 855 kfree(req); 856 } 857 858 /** 859 * dwc3_ep_prev_trb - returns the previous TRB in the ring 860 * @dep: The endpoint with the TRB ring 861 * @index: The index of the current TRB in the ring 862 * 863 * Returns the TRB prior to the one pointed to by the index. If the 864 * index is 0, we will wrap backwards, skip the link TRB, and return 865 * the one just before that. 866 */ 867 static struct dwc3_trb *dwc3_ep_prev_trb(struct dwc3_ep *dep, u8 index) 868 { 869 u8 tmp = index; 870 871 if (!tmp) 872 tmp = DWC3_TRB_NUM - 1; 873 874 return &dep->trb_pool[tmp - 1]; 875 } 876 877 static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep) 878 { 879 struct dwc3_trb *tmp; 880 u8 trbs_left; 881 882 /* 883 * If enqueue & dequeue are equal than it is either full or empty. 884 * 885 * One way to know for sure is if the TRB right before us has HWO bit 886 * set or not. If it has, then we're definitely full and can't fit any 887 * more transfers in our ring. 888 */ 889 if (dep->trb_enqueue == dep->trb_dequeue) { 890 tmp = dwc3_ep_prev_trb(dep, dep->trb_enqueue); 891 if (tmp->ctrl & DWC3_TRB_CTRL_HWO) 892 return 0; 893 894 return DWC3_TRB_NUM - 1; 895 } 896 897 trbs_left = dep->trb_dequeue - dep->trb_enqueue; 898 trbs_left &= (DWC3_TRB_NUM - 1); 899 900 if (dep->trb_dequeue < dep->trb_enqueue) 901 trbs_left--; 902 903 return trbs_left; 904 } 905 906 static void __dwc3_prepare_one_trb(struct dwc3_ep *dep, struct dwc3_trb *trb, 907 dma_addr_t dma, unsigned length, unsigned chain, unsigned node, 908 unsigned stream_id, unsigned short_not_ok, unsigned no_interrupt) 909 { 910 struct dwc3 *dwc = dep->dwc; 911 struct usb_gadget *gadget = &dwc->gadget; 912 enum usb_device_speed speed = gadget->speed; 913 914 dwc3_ep_inc_enq(dep); 915 916 trb->size = DWC3_TRB_SIZE_LENGTH(length); 917 trb->bpl = lower_32_bits(dma); 918 trb->bph = upper_32_bits(dma); 919 920 switch (usb_endpoint_type(dep->endpoint.desc)) { 921 case USB_ENDPOINT_XFER_CONTROL: 922 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP; 923 break; 924 925 case USB_ENDPOINT_XFER_ISOC: 926 if (!node) { 927 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST; 928 929 /* 930 * USB Specification 2.0 Section 5.9.2 states that: "If 931 * there is only a single transaction in the microframe, 932 * only a DATA0 data packet PID is used. If there are 933 * two transactions per microframe, DATA1 is used for 934 * the first transaction data packet and DATA0 is used 935 * for the second transaction data packet. If there are 936 * three transactions per microframe, DATA2 is used for 937 * the first transaction data packet, DATA1 is used for 938 * the second, and DATA0 is used for the third." 939 * 940 * IOW, we should satisfy the following cases: 941 * 942 * 1) length <= maxpacket 943 * - DATA0 944 * 945 * 2) maxpacket < length <= (2 * maxpacket) 946 * - DATA1, DATA0 947 * 948 * 3) (2 * maxpacket) < length <= (3 * maxpacket) 949 * - DATA2, DATA1, DATA0 950 */ 951 if (speed == USB_SPEED_HIGH) { 952 struct usb_ep *ep = &dep->endpoint; 953 unsigned int mult = 2; 954 unsigned int maxp = usb_endpoint_maxp(ep->desc); 955 956 if (length <= (2 * maxp)) 957 mult--; 958 959 if (length <= maxp) 960 mult--; 961 962 trb->size |= DWC3_TRB_SIZE_PCM1(mult); 963 } 964 } else { 965 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS; 966 } 967 968 /* always enable Interrupt on Missed ISOC */ 969 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI; 970 break; 971 972 case USB_ENDPOINT_XFER_BULK: 973 case USB_ENDPOINT_XFER_INT: 974 trb->ctrl = DWC3_TRBCTL_NORMAL; 975 break; 976 default: 977 /* 978 * This is only possible with faulty memory because we 979 * checked it already :) 980 */ 981 dev_WARN(dwc->dev, "Unknown endpoint type %d\n", 982 usb_endpoint_type(dep->endpoint.desc)); 983 } 984 985 /* always enable Continue on Short Packet */ 986 if (usb_endpoint_dir_out(dep->endpoint.desc)) { 987 trb->ctrl |= DWC3_TRB_CTRL_CSP; 988 989 if (short_not_ok) 990 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI; 991 } 992 993 if ((!no_interrupt && !chain) || 994 (dwc3_calc_trbs_left(dep) == 0)) 995 trb->ctrl |= DWC3_TRB_CTRL_IOC; 996 997 if (chain) 998 trb->ctrl |= DWC3_TRB_CTRL_CHN; 999 1000 if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable) 1001 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(stream_id); 1002 1003 trb->ctrl |= DWC3_TRB_CTRL_HWO; 1004 1005 trace_dwc3_prepare_trb(dep, trb); 1006 } 1007 1008 /** 1009 * dwc3_prepare_one_trb - setup one TRB from one request 1010 * @dep: endpoint for which this request is prepared 1011 * @req: dwc3_request pointer 1012 * @chain: should this TRB be chained to the next? 1013 * @node: only for isochronous endpoints. First TRB needs different type. 1014 */ 1015 static void dwc3_prepare_one_trb(struct dwc3_ep *dep, 1016 struct dwc3_request *req, unsigned chain, unsigned node) 1017 { 1018 struct dwc3_trb *trb; 1019 unsigned int length; 1020 dma_addr_t dma; 1021 unsigned stream_id = req->request.stream_id; 1022 unsigned short_not_ok = req->request.short_not_ok; 1023 unsigned no_interrupt = req->request.no_interrupt; 1024 1025 if (req->request.num_sgs > 0) { 1026 length = sg_dma_len(req->start_sg); 1027 dma = sg_dma_address(req->start_sg); 1028 } else { 1029 length = req->request.length; 1030 dma = req->request.dma; 1031 } 1032 1033 trb = &dep->trb_pool[dep->trb_enqueue]; 1034 1035 if (!req->trb) { 1036 dwc3_gadget_move_started_request(req); 1037 req->trb = trb; 1038 req->trb_dma = dwc3_trb_dma_offset(dep, trb); 1039 } 1040 1041 __dwc3_prepare_one_trb(dep, trb, dma, length, chain, node, 1042 stream_id, short_not_ok, no_interrupt); 1043 } 1044 1045 static void dwc3_prepare_one_trb_sg(struct dwc3_ep *dep, 1046 struct dwc3_request *req) 1047 { 1048 struct scatterlist *sg = req->start_sg; 1049 struct scatterlist *s; 1050 int i; 1051 1052 unsigned int remaining = req->request.num_mapped_sgs 1053 - req->num_queued_sgs; 1054 1055 for_each_sg(sg, s, remaining, i) { 1056 unsigned int length = req->request.length; 1057 unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc); 1058 unsigned int rem = length % maxp; 1059 unsigned chain = true; 1060 1061 if (sg_is_last(s)) 1062 chain = false; 1063 1064 if (rem && usb_endpoint_dir_out(dep->endpoint.desc) && !chain) { 1065 struct dwc3 *dwc = dep->dwc; 1066 struct dwc3_trb *trb; 1067 1068 req->unaligned = true; 1069 1070 /* prepare normal TRB */ 1071 dwc3_prepare_one_trb(dep, req, true, i); 1072 1073 /* Now prepare one extra TRB to align transfer size */ 1074 trb = &dep->trb_pool[dep->trb_enqueue]; 1075 __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr, 1076 maxp - rem, false, 0, 1077 req->request.stream_id, 1078 req->request.short_not_ok, 1079 req->request.no_interrupt); 1080 } else { 1081 dwc3_prepare_one_trb(dep, req, chain, i); 1082 } 1083 1084 /* 1085 * There can be a situation where all sgs in sglist are not 1086 * queued because of insufficient trb number. To handle this 1087 * case, update start_sg to next sg to be queued, so that 1088 * we have free trbs we can continue queuing from where we 1089 * previously stopped 1090 */ 1091 if (chain) 1092 req->start_sg = sg_next(s); 1093 1094 req->num_queued_sgs++; 1095 1096 if (!dwc3_calc_trbs_left(dep)) 1097 break; 1098 } 1099 } 1100 1101 static void dwc3_prepare_one_trb_linear(struct dwc3_ep *dep, 1102 struct dwc3_request *req) 1103 { 1104 unsigned int length = req->request.length; 1105 unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc); 1106 unsigned int rem = length % maxp; 1107 1108 if (rem && usb_endpoint_dir_out(dep->endpoint.desc)) { 1109 struct dwc3 *dwc = dep->dwc; 1110 struct dwc3_trb *trb; 1111 1112 req->unaligned = true; 1113 1114 /* prepare normal TRB */ 1115 dwc3_prepare_one_trb(dep, req, true, 0); 1116 1117 /* Now prepare one extra TRB to align transfer size */ 1118 trb = &dep->trb_pool[dep->trb_enqueue]; 1119 __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr, maxp - rem, 1120 false, 0, req->request.stream_id, 1121 req->request.short_not_ok, 1122 req->request.no_interrupt); 1123 } else if (req->request.zero && req->request.length && 1124 (IS_ALIGNED(req->request.length, maxp))) { 1125 struct dwc3 *dwc = dep->dwc; 1126 struct dwc3_trb *trb; 1127 1128 req->zero = true; 1129 1130 /* prepare normal TRB */ 1131 dwc3_prepare_one_trb(dep, req, true, 0); 1132 1133 /* Now prepare one extra TRB to handle ZLP */ 1134 trb = &dep->trb_pool[dep->trb_enqueue]; 1135 __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr, 0, 1136 false, 0, req->request.stream_id, 1137 req->request.short_not_ok, 1138 req->request.no_interrupt); 1139 } else { 1140 dwc3_prepare_one_trb(dep, req, false, 0); 1141 } 1142 } 1143 1144 /* 1145 * dwc3_prepare_trbs - setup TRBs from requests 1146 * @dep: endpoint for which requests are being prepared 1147 * 1148 * The function goes through the requests list and sets up TRBs for the 1149 * transfers. The function returns once there are no more TRBs available or 1150 * it runs out of requests. 1151 */ 1152 static void dwc3_prepare_trbs(struct dwc3_ep *dep) 1153 { 1154 struct dwc3_request *req, *n; 1155 1156 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM); 1157 1158 /* 1159 * We can get in a situation where there's a request in the started list 1160 * but there weren't enough TRBs to fully kick it in the first time 1161 * around, so it has been waiting for more TRBs to be freed up. 1162 * 1163 * In that case, we should check if we have a request with pending_sgs 1164 * in the started list and prepare TRBs for that request first, 1165 * otherwise we will prepare TRBs completely out of order and that will 1166 * break things. 1167 */ 1168 list_for_each_entry(req, &dep->started_list, list) { 1169 if (req->num_pending_sgs > 0) 1170 dwc3_prepare_one_trb_sg(dep, req); 1171 1172 if (!dwc3_calc_trbs_left(dep)) 1173 return; 1174 } 1175 1176 list_for_each_entry_safe(req, n, &dep->pending_list, list) { 1177 struct dwc3 *dwc = dep->dwc; 1178 int ret; 1179 1180 ret = usb_gadget_map_request_by_dev(dwc->sysdev, &req->request, 1181 dep->direction); 1182 if (ret) 1183 return; 1184 1185 req->sg = req->request.sg; 1186 req->start_sg = req->sg; 1187 req->num_queued_sgs = 0; 1188 req->num_pending_sgs = req->request.num_mapped_sgs; 1189 1190 if (req->num_pending_sgs > 0) 1191 dwc3_prepare_one_trb_sg(dep, req); 1192 else 1193 dwc3_prepare_one_trb_linear(dep, req); 1194 1195 if (!dwc3_calc_trbs_left(dep)) 1196 return; 1197 } 1198 } 1199 1200 static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep) 1201 { 1202 struct dwc3_gadget_ep_cmd_params params; 1203 struct dwc3_request *req; 1204 int starting; 1205 int ret; 1206 u32 cmd; 1207 1208 if (!dwc3_calc_trbs_left(dep)) 1209 return 0; 1210 1211 starting = !(dep->flags & DWC3_EP_TRANSFER_STARTED); 1212 1213 dwc3_prepare_trbs(dep); 1214 req = next_request(&dep->started_list); 1215 if (!req) { 1216 dep->flags |= DWC3_EP_PENDING_REQUEST; 1217 return 0; 1218 } 1219 1220 memset(¶ms, 0, sizeof(params)); 1221 1222 if (starting) { 1223 params.param0 = upper_32_bits(req->trb_dma); 1224 params.param1 = lower_32_bits(req->trb_dma); 1225 cmd = DWC3_DEPCMD_STARTTRANSFER; 1226 1227 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) 1228 cmd |= DWC3_DEPCMD_PARAM(dep->frame_number); 1229 } else { 1230 cmd = DWC3_DEPCMD_UPDATETRANSFER | 1231 DWC3_DEPCMD_PARAM(dep->resource_index); 1232 } 1233 1234 ret = dwc3_send_gadget_ep_cmd(dep, cmd, ¶ms); 1235 if (ret < 0) { 1236 /* 1237 * FIXME we need to iterate over the list of requests 1238 * here and stop, unmap, free and del each of the linked 1239 * requests instead of what we do now. 1240 */ 1241 if (req->trb) 1242 memset(req->trb, 0, sizeof(struct dwc3_trb)); 1243 dwc3_gadget_del_and_unmap_request(dep, req, ret); 1244 return ret; 1245 } 1246 1247 return 0; 1248 } 1249 1250 static int __dwc3_gadget_get_frame(struct dwc3 *dwc) 1251 { 1252 u32 reg; 1253 1254 reg = dwc3_readl(dwc->regs, DWC3_DSTS); 1255 return DWC3_DSTS_SOFFN(reg); 1256 } 1257 1258 static void __dwc3_gadget_start_isoc(struct dwc3_ep *dep) 1259 { 1260 if (list_empty(&dep->pending_list)) { 1261 dev_info(dep->dwc->dev, "%s: ran out of requests\n", 1262 dep->name); 1263 dep->flags |= DWC3_EP_PENDING_REQUEST; 1264 return; 1265 } 1266 1267 dep->frame_number = DWC3_ALIGN_FRAME(dep); 1268 __dwc3_gadget_kick_transfer(dep); 1269 } 1270 1271 static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req) 1272 { 1273 struct dwc3 *dwc = dep->dwc; 1274 1275 if (!dep->endpoint.desc) { 1276 dev_err(dwc->dev, "%s: can't queue to disabled endpoint\n", 1277 dep->name); 1278 return -ESHUTDOWN; 1279 } 1280 1281 if (WARN(req->dep != dep, "request %pK belongs to '%s'\n", 1282 &req->request, req->dep->name)) 1283 return -EINVAL; 1284 1285 pm_runtime_get(dwc->dev); 1286 1287 req->request.actual = 0; 1288 req->request.status = -EINPROGRESS; 1289 1290 trace_dwc3_ep_queue(req); 1291 1292 list_add_tail(&req->list, &dep->pending_list); 1293 1294 /* 1295 * NOTICE: Isochronous endpoints should NEVER be prestarted. We must 1296 * wait for a XferNotReady event so we will know what's the current 1297 * (micro-)frame number. 1298 * 1299 * Without this trick, we are very, very likely gonna get Bus Expiry 1300 * errors which will force us issue EndTransfer command. 1301 */ 1302 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) { 1303 if (!(dep->flags & DWC3_EP_PENDING_REQUEST) && 1304 !(dep->flags & DWC3_EP_TRANSFER_STARTED)) 1305 return 0; 1306 1307 if ((dep->flags & DWC3_EP_PENDING_REQUEST)) { 1308 if (!(dep->flags & DWC3_EP_TRANSFER_STARTED)) { 1309 __dwc3_gadget_start_isoc(dep); 1310 return 0; 1311 } 1312 } 1313 } 1314 1315 return __dwc3_gadget_kick_transfer(dep); 1316 } 1317 1318 static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request, 1319 gfp_t gfp_flags) 1320 { 1321 struct dwc3_request *req = to_dwc3_request(request); 1322 struct dwc3_ep *dep = to_dwc3_ep(ep); 1323 struct dwc3 *dwc = dep->dwc; 1324 1325 unsigned long flags; 1326 1327 int ret; 1328 1329 spin_lock_irqsave(&dwc->lock, flags); 1330 ret = __dwc3_gadget_ep_queue(dep, req); 1331 spin_unlock_irqrestore(&dwc->lock, flags); 1332 1333 return ret; 1334 } 1335 1336 static int dwc3_gadget_ep_dequeue(struct usb_ep *ep, 1337 struct usb_request *request) 1338 { 1339 struct dwc3_request *req = to_dwc3_request(request); 1340 struct dwc3_request *r = NULL; 1341 1342 struct dwc3_ep *dep = to_dwc3_ep(ep); 1343 struct dwc3 *dwc = dep->dwc; 1344 1345 unsigned long flags; 1346 int ret = 0; 1347 1348 trace_dwc3_ep_dequeue(req); 1349 1350 spin_lock_irqsave(&dwc->lock, flags); 1351 1352 list_for_each_entry(r, &dep->pending_list, list) { 1353 if (r == req) 1354 break; 1355 } 1356 1357 if (r != req) { 1358 list_for_each_entry(r, &dep->started_list, list) { 1359 if (r == req) 1360 break; 1361 } 1362 if (r == req) { 1363 /* wait until it is processed */ 1364 dwc3_stop_active_transfer(dep, true); 1365 1366 /* 1367 * If request was already started, this means we had to 1368 * stop the transfer. With that we also need to ignore 1369 * all TRBs used by the request, however TRBs can only 1370 * be modified after completion of END_TRANSFER 1371 * command. So what we do here is that we wait for 1372 * END_TRANSFER completion and only after that, we jump 1373 * over TRBs by clearing HWO and incrementing dequeue 1374 * pointer. 1375 * 1376 * Note that we have 2 possible types of transfers here: 1377 * 1378 * i) Linear buffer request 1379 * ii) SG-list based request 1380 * 1381 * SG-list based requests will have r->num_pending_sgs 1382 * set to a valid number (> 0). Linear requests, 1383 * normally use a single TRB. 1384 * 1385 * For each of these two cases, if r->unaligned flag is 1386 * set, one extra TRB has been used to align transfer 1387 * size to wMaxPacketSize. 1388 * 1389 * All of these cases need to be taken into 1390 * consideration so we don't mess up our TRB ring 1391 * pointers. 1392 */ 1393 wait_event_lock_irq(dep->wait_end_transfer, 1394 !(dep->flags & DWC3_EP_END_TRANSFER_PENDING), 1395 dwc->lock); 1396 1397 if (!r->trb) 1398 goto out0; 1399 1400 if (r->num_pending_sgs) { 1401 struct dwc3_trb *trb; 1402 int i = 0; 1403 1404 for (i = 0; i < r->num_pending_sgs; i++) { 1405 trb = r->trb + i; 1406 trb->ctrl &= ~DWC3_TRB_CTRL_HWO; 1407 dwc3_ep_inc_deq(dep); 1408 } 1409 1410 if (r->unaligned || r->zero) { 1411 trb = r->trb + r->num_pending_sgs + 1; 1412 trb->ctrl &= ~DWC3_TRB_CTRL_HWO; 1413 dwc3_ep_inc_deq(dep); 1414 } 1415 } else { 1416 struct dwc3_trb *trb = r->trb; 1417 1418 trb->ctrl &= ~DWC3_TRB_CTRL_HWO; 1419 dwc3_ep_inc_deq(dep); 1420 1421 if (r->unaligned || r->zero) { 1422 trb = r->trb + 1; 1423 trb->ctrl &= ~DWC3_TRB_CTRL_HWO; 1424 dwc3_ep_inc_deq(dep); 1425 } 1426 } 1427 goto out1; 1428 } 1429 dev_err(dwc->dev, "request %pK was not queued to %s\n", 1430 request, ep->name); 1431 ret = -EINVAL; 1432 goto out0; 1433 } 1434 1435 out1: 1436 /* giveback the request */ 1437 1438 dwc3_gadget_giveback(dep, req, -ECONNRESET); 1439 1440 out0: 1441 spin_unlock_irqrestore(&dwc->lock, flags); 1442 1443 return ret; 1444 } 1445 1446 int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol) 1447 { 1448 struct dwc3_gadget_ep_cmd_params params; 1449 struct dwc3 *dwc = dep->dwc; 1450 int ret; 1451 1452 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) { 1453 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name); 1454 return -EINVAL; 1455 } 1456 1457 memset(¶ms, 0x00, sizeof(params)); 1458 1459 if (value) { 1460 struct dwc3_trb *trb; 1461 1462 unsigned transfer_in_flight; 1463 unsigned started; 1464 1465 if (dep->flags & DWC3_EP_STALL) 1466 return 0; 1467 1468 if (dep->number > 1) 1469 trb = dwc3_ep_prev_trb(dep, dep->trb_enqueue); 1470 else 1471 trb = &dwc->ep0_trb[dep->trb_enqueue]; 1472 1473 transfer_in_flight = trb->ctrl & DWC3_TRB_CTRL_HWO; 1474 started = !list_empty(&dep->started_list); 1475 1476 if (!protocol && ((dep->direction && transfer_in_flight) || 1477 (!dep->direction && started))) { 1478 return -EAGAIN; 1479 } 1480 1481 ret = dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETSTALL, 1482 ¶ms); 1483 if (ret) 1484 dev_err(dwc->dev, "failed to set STALL on %s\n", 1485 dep->name); 1486 else 1487 dep->flags |= DWC3_EP_STALL; 1488 } else { 1489 if (!(dep->flags & DWC3_EP_STALL)) 1490 return 0; 1491 1492 ret = dwc3_send_clear_stall_ep_cmd(dep); 1493 if (ret) 1494 dev_err(dwc->dev, "failed to clear STALL on %s\n", 1495 dep->name); 1496 else 1497 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE); 1498 } 1499 1500 return ret; 1501 } 1502 1503 static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value) 1504 { 1505 struct dwc3_ep *dep = to_dwc3_ep(ep); 1506 struct dwc3 *dwc = dep->dwc; 1507 1508 unsigned long flags; 1509 1510 int ret; 1511 1512 spin_lock_irqsave(&dwc->lock, flags); 1513 ret = __dwc3_gadget_ep_set_halt(dep, value, false); 1514 spin_unlock_irqrestore(&dwc->lock, flags); 1515 1516 return ret; 1517 } 1518 1519 static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep) 1520 { 1521 struct dwc3_ep *dep = to_dwc3_ep(ep); 1522 struct dwc3 *dwc = dep->dwc; 1523 unsigned long flags; 1524 int ret; 1525 1526 spin_lock_irqsave(&dwc->lock, flags); 1527 dep->flags |= DWC3_EP_WEDGE; 1528 1529 if (dep->number == 0 || dep->number == 1) 1530 ret = __dwc3_gadget_ep0_set_halt(ep, 1); 1531 else 1532 ret = __dwc3_gadget_ep_set_halt(dep, 1, false); 1533 spin_unlock_irqrestore(&dwc->lock, flags); 1534 1535 return ret; 1536 } 1537 1538 /* -------------------------------------------------------------------------- */ 1539 1540 static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = { 1541 .bLength = USB_DT_ENDPOINT_SIZE, 1542 .bDescriptorType = USB_DT_ENDPOINT, 1543 .bmAttributes = USB_ENDPOINT_XFER_CONTROL, 1544 }; 1545 1546 static const struct usb_ep_ops dwc3_gadget_ep0_ops = { 1547 .enable = dwc3_gadget_ep0_enable, 1548 .disable = dwc3_gadget_ep0_disable, 1549 .alloc_request = dwc3_gadget_ep_alloc_request, 1550 .free_request = dwc3_gadget_ep_free_request, 1551 .queue = dwc3_gadget_ep0_queue, 1552 .dequeue = dwc3_gadget_ep_dequeue, 1553 .set_halt = dwc3_gadget_ep0_set_halt, 1554 .set_wedge = dwc3_gadget_ep_set_wedge, 1555 }; 1556 1557 static const struct usb_ep_ops dwc3_gadget_ep_ops = { 1558 .enable = dwc3_gadget_ep_enable, 1559 .disable = dwc3_gadget_ep_disable, 1560 .alloc_request = dwc3_gadget_ep_alloc_request, 1561 .free_request = dwc3_gadget_ep_free_request, 1562 .queue = dwc3_gadget_ep_queue, 1563 .dequeue = dwc3_gadget_ep_dequeue, 1564 .set_halt = dwc3_gadget_ep_set_halt, 1565 .set_wedge = dwc3_gadget_ep_set_wedge, 1566 }; 1567 1568 /* -------------------------------------------------------------------------- */ 1569 1570 static int dwc3_gadget_get_frame(struct usb_gadget *g) 1571 { 1572 struct dwc3 *dwc = gadget_to_dwc(g); 1573 1574 return __dwc3_gadget_get_frame(dwc); 1575 } 1576 1577 static int __dwc3_gadget_wakeup(struct dwc3 *dwc) 1578 { 1579 int retries; 1580 1581 int ret; 1582 u32 reg; 1583 1584 u8 link_state; 1585 u8 speed; 1586 1587 /* 1588 * According to the Databook Remote wakeup request should 1589 * be issued only when the device is in early suspend state. 1590 * 1591 * We can check that via USB Link State bits in DSTS register. 1592 */ 1593 reg = dwc3_readl(dwc->regs, DWC3_DSTS); 1594 1595 speed = reg & DWC3_DSTS_CONNECTSPD; 1596 if ((speed == DWC3_DSTS_SUPERSPEED) || 1597 (speed == DWC3_DSTS_SUPERSPEED_PLUS)) 1598 return 0; 1599 1600 link_state = DWC3_DSTS_USBLNKST(reg); 1601 1602 switch (link_state) { 1603 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */ 1604 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */ 1605 break; 1606 default: 1607 return -EINVAL; 1608 } 1609 1610 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV); 1611 if (ret < 0) { 1612 dev_err(dwc->dev, "failed to put link in Recovery\n"); 1613 return ret; 1614 } 1615 1616 /* Recent versions do this automatically */ 1617 if (dwc->revision < DWC3_REVISION_194A) { 1618 /* write zeroes to Link Change Request */ 1619 reg = dwc3_readl(dwc->regs, DWC3_DCTL); 1620 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK; 1621 dwc3_writel(dwc->regs, DWC3_DCTL, reg); 1622 } 1623 1624 /* poll until Link State changes to ON */ 1625 retries = 20000; 1626 1627 while (retries--) { 1628 reg = dwc3_readl(dwc->regs, DWC3_DSTS); 1629 1630 /* in HS, means ON */ 1631 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0) 1632 break; 1633 } 1634 1635 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) { 1636 dev_err(dwc->dev, "failed to send remote wakeup\n"); 1637 return -EINVAL; 1638 } 1639 1640 return 0; 1641 } 1642 1643 static int dwc3_gadget_wakeup(struct usb_gadget *g) 1644 { 1645 struct dwc3 *dwc = gadget_to_dwc(g); 1646 unsigned long flags; 1647 int ret; 1648 1649 spin_lock_irqsave(&dwc->lock, flags); 1650 ret = __dwc3_gadget_wakeup(dwc); 1651 spin_unlock_irqrestore(&dwc->lock, flags); 1652 1653 return ret; 1654 } 1655 1656 static int dwc3_gadget_set_selfpowered(struct usb_gadget *g, 1657 int is_selfpowered) 1658 { 1659 struct dwc3 *dwc = gadget_to_dwc(g); 1660 unsigned long flags; 1661 1662 spin_lock_irqsave(&dwc->lock, flags); 1663 g->is_selfpowered = !!is_selfpowered; 1664 spin_unlock_irqrestore(&dwc->lock, flags); 1665 1666 return 0; 1667 } 1668 1669 static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on, int suspend) 1670 { 1671 u32 reg; 1672 u32 timeout = 500; 1673 1674 if (pm_runtime_suspended(dwc->dev)) 1675 return 0; 1676 1677 reg = dwc3_readl(dwc->regs, DWC3_DCTL); 1678 if (is_on) { 1679 if (dwc->revision <= DWC3_REVISION_187A) { 1680 reg &= ~DWC3_DCTL_TRGTULST_MASK; 1681 reg |= DWC3_DCTL_TRGTULST_RX_DET; 1682 } 1683 1684 if (dwc->revision >= DWC3_REVISION_194A) 1685 reg &= ~DWC3_DCTL_KEEP_CONNECT; 1686 reg |= DWC3_DCTL_RUN_STOP; 1687 1688 if (dwc->has_hibernation) 1689 reg |= DWC3_DCTL_KEEP_CONNECT; 1690 1691 dwc->pullups_connected = true; 1692 } else { 1693 reg &= ~DWC3_DCTL_RUN_STOP; 1694 1695 if (dwc->has_hibernation && !suspend) 1696 reg &= ~DWC3_DCTL_KEEP_CONNECT; 1697 1698 dwc->pullups_connected = false; 1699 } 1700 1701 dwc3_writel(dwc->regs, DWC3_DCTL, reg); 1702 1703 do { 1704 reg = dwc3_readl(dwc->regs, DWC3_DSTS); 1705 reg &= DWC3_DSTS_DEVCTRLHLT; 1706 } while (--timeout && !(!is_on ^ !reg)); 1707 1708 if (!timeout) 1709 return -ETIMEDOUT; 1710 1711 return 0; 1712 } 1713 1714 static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on) 1715 { 1716 struct dwc3 *dwc = gadget_to_dwc(g); 1717 unsigned long flags; 1718 int ret; 1719 1720 is_on = !!is_on; 1721 1722 /* 1723 * Per databook, when we want to stop the gadget, if a control transfer 1724 * is still in process, complete it and get the core into setup phase. 1725 */ 1726 if (!is_on && dwc->ep0state != EP0_SETUP_PHASE) { 1727 reinit_completion(&dwc->ep0_in_setup); 1728 1729 ret = wait_for_completion_timeout(&dwc->ep0_in_setup, 1730 msecs_to_jiffies(DWC3_PULL_UP_TIMEOUT)); 1731 if (ret == 0) { 1732 dev_err(dwc->dev, "timed out waiting for SETUP phase\n"); 1733 return -ETIMEDOUT; 1734 } 1735 } 1736 1737 spin_lock_irqsave(&dwc->lock, flags); 1738 ret = dwc3_gadget_run_stop(dwc, is_on, false); 1739 spin_unlock_irqrestore(&dwc->lock, flags); 1740 1741 return ret; 1742 } 1743 1744 static void dwc3_gadget_enable_irq(struct dwc3 *dwc) 1745 { 1746 u32 reg; 1747 1748 /* Enable all but Start and End of Frame IRQs */ 1749 reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN | 1750 DWC3_DEVTEN_EVNTOVERFLOWEN | 1751 DWC3_DEVTEN_CMDCMPLTEN | 1752 DWC3_DEVTEN_ERRTICERREN | 1753 DWC3_DEVTEN_WKUPEVTEN | 1754 DWC3_DEVTEN_CONNECTDONEEN | 1755 DWC3_DEVTEN_USBRSTEN | 1756 DWC3_DEVTEN_DISCONNEVTEN); 1757 1758 if (dwc->revision < DWC3_REVISION_250A) 1759 reg |= DWC3_DEVTEN_ULSTCNGEN; 1760 1761 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg); 1762 } 1763 1764 static void dwc3_gadget_disable_irq(struct dwc3 *dwc) 1765 { 1766 /* mask all interrupts */ 1767 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00); 1768 } 1769 1770 static irqreturn_t dwc3_interrupt(int irq, void *_dwc); 1771 static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc); 1772 1773 /** 1774 * dwc3_gadget_setup_nump - calculate and initialize NUMP field of %DWC3_DCFG 1775 * @dwc: pointer to our context structure 1776 * 1777 * The following looks like complex but it's actually very simple. In order to 1778 * calculate the number of packets we can burst at once on OUT transfers, we're 1779 * gonna use RxFIFO size. 1780 * 1781 * To calculate RxFIFO size we need two numbers: 1782 * MDWIDTH = size, in bits, of the internal memory bus 1783 * RAM2_DEPTH = depth, in MDWIDTH, of internal RAM2 (where RxFIFO sits) 1784 * 1785 * Given these two numbers, the formula is simple: 1786 * 1787 * RxFIFO Size = (RAM2_DEPTH * MDWIDTH / 8) - 24 - 16; 1788 * 1789 * 24 bytes is for 3x SETUP packets 1790 * 16 bytes is a clock domain crossing tolerance 1791 * 1792 * Given RxFIFO Size, NUMP = RxFIFOSize / 1024; 1793 */ 1794 static void dwc3_gadget_setup_nump(struct dwc3 *dwc) 1795 { 1796 u32 ram2_depth; 1797 u32 mdwidth; 1798 u32 nump; 1799 u32 reg; 1800 1801 ram2_depth = DWC3_GHWPARAMS7_RAM2_DEPTH(dwc->hwparams.hwparams7); 1802 mdwidth = DWC3_GHWPARAMS0_MDWIDTH(dwc->hwparams.hwparams0); 1803 1804 nump = ((ram2_depth * mdwidth / 8) - 24 - 16) / 1024; 1805 nump = min_t(u32, nump, 16); 1806 1807 /* update NumP */ 1808 reg = dwc3_readl(dwc->regs, DWC3_DCFG); 1809 reg &= ~DWC3_DCFG_NUMP_MASK; 1810 reg |= nump << DWC3_DCFG_NUMP_SHIFT; 1811 dwc3_writel(dwc->regs, DWC3_DCFG, reg); 1812 } 1813 1814 static int __dwc3_gadget_start(struct dwc3 *dwc) 1815 { 1816 struct dwc3_ep *dep; 1817 int ret = 0; 1818 u32 reg; 1819 1820 /* 1821 * Use IMOD if enabled via dwc->imod_interval. Otherwise, if 1822 * the core supports IMOD, disable it. 1823 */ 1824 if (dwc->imod_interval) { 1825 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval); 1826 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB); 1827 } else if (dwc3_has_imod(dwc)) { 1828 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), 0); 1829 } 1830 1831 /* 1832 * We are telling dwc3 that we want to use DCFG.NUMP as ACK TP's NUMP 1833 * field instead of letting dwc3 itself calculate that automatically. 1834 * 1835 * This way, we maximize the chances that we'll be able to get several 1836 * bursts of data without going through any sort of endpoint throttling. 1837 */ 1838 reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG); 1839 if (dwc3_is_usb31(dwc)) 1840 reg &= ~DWC31_GRXTHRCFG_PKTCNTSEL; 1841 else 1842 reg &= ~DWC3_GRXTHRCFG_PKTCNTSEL; 1843 1844 dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg); 1845 1846 dwc3_gadget_setup_nump(dwc); 1847 1848 /* Start with SuperSpeed Default */ 1849 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512); 1850 1851 dep = dwc->eps[0]; 1852 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT); 1853 if (ret) { 1854 dev_err(dwc->dev, "failed to enable %s\n", dep->name); 1855 goto err0; 1856 } 1857 1858 dep = dwc->eps[1]; 1859 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT); 1860 if (ret) { 1861 dev_err(dwc->dev, "failed to enable %s\n", dep->name); 1862 goto err1; 1863 } 1864 1865 /* begin to receive SETUP packets */ 1866 dwc->ep0state = EP0_SETUP_PHASE; 1867 dwc3_ep0_out_start(dwc); 1868 1869 dwc3_gadget_enable_irq(dwc); 1870 1871 return 0; 1872 1873 err1: 1874 __dwc3_gadget_ep_disable(dwc->eps[0]); 1875 1876 err0: 1877 return ret; 1878 } 1879 1880 static int dwc3_gadget_start(struct usb_gadget *g, 1881 struct usb_gadget_driver *driver) 1882 { 1883 struct dwc3 *dwc = gadget_to_dwc(g); 1884 unsigned long flags; 1885 int ret = 0; 1886 int irq; 1887 1888 irq = dwc->irq_gadget; 1889 ret = request_threaded_irq(irq, dwc3_interrupt, dwc3_thread_interrupt, 1890 IRQF_SHARED, "dwc3", dwc->ev_buf); 1891 if (ret) { 1892 dev_err(dwc->dev, "failed to request irq #%d --> %d\n", 1893 irq, ret); 1894 goto err0; 1895 } 1896 1897 spin_lock_irqsave(&dwc->lock, flags); 1898 if (dwc->gadget_driver) { 1899 dev_err(dwc->dev, "%s is already bound to %s\n", 1900 dwc->gadget.name, 1901 dwc->gadget_driver->driver.name); 1902 ret = -EBUSY; 1903 goto err1; 1904 } 1905 1906 dwc->gadget_driver = driver; 1907 1908 if (pm_runtime_active(dwc->dev)) 1909 __dwc3_gadget_start(dwc); 1910 1911 spin_unlock_irqrestore(&dwc->lock, flags); 1912 1913 return 0; 1914 1915 err1: 1916 spin_unlock_irqrestore(&dwc->lock, flags); 1917 free_irq(irq, dwc); 1918 1919 err0: 1920 return ret; 1921 } 1922 1923 static void __dwc3_gadget_stop(struct dwc3 *dwc) 1924 { 1925 dwc3_gadget_disable_irq(dwc); 1926 __dwc3_gadget_ep_disable(dwc->eps[0]); 1927 __dwc3_gadget_ep_disable(dwc->eps[1]); 1928 } 1929 1930 static int dwc3_gadget_stop(struct usb_gadget *g) 1931 { 1932 struct dwc3 *dwc = gadget_to_dwc(g); 1933 unsigned long flags; 1934 int epnum; 1935 u32 tmo_eps = 0; 1936 1937 spin_lock_irqsave(&dwc->lock, flags); 1938 1939 if (pm_runtime_suspended(dwc->dev)) 1940 goto out; 1941 1942 __dwc3_gadget_stop(dwc); 1943 1944 for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) { 1945 struct dwc3_ep *dep = dwc->eps[epnum]; 1946 int ret; 1947 1948 if (!dep) 1949 continue; 1950 1951 if (!(dep->flags & DWC3_EP_END_TRANSFER_PENDING)) 1952 continue; 1953 1954 ret = wait_event_interruptible_lock_irq_timeout(dep->wait_end_transfer, 1955 !(dep->flags & DWC3_EP_END_TRANSFER_PENDING), 1956 dwc->lock, msecs_to_jiffies(5)); 1957 1958 if (ret <= 0) { 1959 /* Timed out or interrupted! There's nothing much 1960 * we can do so we just log here and print which 1961 * endpoints timed out at the end. 1962 */ 1963 tmo_eps |= 1 << epnum; 1964 dep->flags &= DWC3_EP_END_TRANSFER_PENDING; 1965 } 1966 } 1967 1968 if (tmo_eps) { 1969 dev_err(dwc->dev, 1970 "end transfer timed out on endpoints 0x%x [bitmap]\n", 1971 tmo_eps); 1972 } 1973 1974 out: 1975 dwc->gadget_driver = NULL; 1976 spin_unlock_irqrestore(&dwc->lock, flags); 1977 1978 free_irq(dwc->irq_gadget, dwc->ev_buf); 1979 1980 return 0; 1981 } 1982 1983 static void dwc3_gadget_set_speed(struct usb_gadget *g, 1984 enum usb_device_speed speed) 1985 { 1986 struct dwc3 *dwc = gadget_to_dwc(g); 1987 unsigned long flags; 1988 u32 reg; 1989 1990 spin_lock_irqsave(&dwc->lock, flags); 1991 reg = dwc3_readl(dwc->regs, DWC3_DCFG); 1992 reg &= ~(DWC3_DCFG_SPEED_MASK); 1993 1994 /* 1995 * WORKAROUND: DWC3 revision < 2.20a have an issue 1996 * which would cause metastability state on Run/Stop 1997 * bit if we try to force the IP to USB2-only mode. 1998 * 1999 * Because of that, we cannot configure the IP to any 2000 * speed other than the SuperSpeed 2001 * 2002 * Refers to: 2003 * 2004 * STAR#9000525659: Clock Domain Crossing on DCTL in 2005 * USB 2.0 Mode 2006 */ 2007 if (dwc->revision < DWC3_REVISION_220A && 2008 !dwc->dis_metastability_quirk) { 2009 reg |= DWC3_DCFG_SUPERSPEED; 2010 } else { 2011 switch (speed) { 2012 case USB_SPEED_LOW: 2013 reg |= DWC3_DCFG_LOWSPEED; 2014 break; 2015 case USB_SPEED_FULL: 2016 reg |= DWC3_DCFG_FULLSPEED; 2017 break; 2018 case USB_SPEED_HIGH: 2019 reg |= DWC3_DCFG_HIGHSPEED; 2020 break; 2021 case USB_SPEED_SUPER: 2022 reg |= DWC3_DCFG_SUPERSPEED; 2023 break; 2024 case USB_SPEED_SUPER_PLUS: 2025 if (dwc3_is_usb31(dwc)) 2026 reg |= DWC3_DCFG_SUPERSPEED_PLUS; 2027 else 2028 reg |= DWC3_DCFG_SUPERSPEED; 2029 break; 2030 default: 2031 dev_err(dwc->dev, "invalid speed (%d)\n", speed); 2032 2033 if (dwc->revision & DWC3_REVISION_IS_DWC31) 2034 reg |= DWC3_DCFG_SUPERSPEED_PLUS; 2035 else 2036 reg |= DWC3_DCFG_SUPERSPEED; 2037 } 2038 } 2039 dwc3_writel(dwc->regs, DWC3_DCFG, reg); 2040 2041 spin_unlock_irqrestore(&dwc->lock, flags); 2042 } 2043 2044 static const struct usb_gadget_ops dwc3_gadget_ops = { 2045 .get_frame = dwc3_gadget_get_frame, 2046 .wakeup = dwc3_gadget_wakeup, 2047 .set_selfpowered = dwc3_gadget_set_selfpowered, 2048 .pullup = dwc3_gadget_pullup, 2049 .udc_start = dwc3_gadget_start, 2050 .udc_stop = dwc3_gadget_stop, 2051 .udc_set_speed = dwc3_gadget_set_speed, 2052 }; 2053 2054 /* -------------------------------------------------------------------------- */ 2055 2056 static int dwc3_gadget_init_control_endpoint(struct dwc3_ep *dep) 2057 { 2058 struct dwc3 *dwc = dep->dwc; 2059 2060 usb_ep_set_maxpacket_limit(&dep->endpoint, 512); 2061 dep->endpoint.maxburst = 1; 2062 dep->endpoint.ops = &dwc3_gadget_ep0_ops; 2063 if (!dep->direction) 2064 dwc->gadget.ep0 = &dep->endpoint; 2065 2066 dep->endpoint.caps.type_control = true; 2067 2068 return 0; 2069 } 2070 2071 static int dwc3_gadget_init_in_endpoint(struct dwc3_ep *dep) 2072 { 2073 struct dwc3 *dwc = dep->dwc; 2074 int mdwidth; 2075 int kbytes; 2076 int size; 2077 2078 mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0); 2079 /* MDWIDTH is represented in bits, we need it in bytes */ 2080 mdwidth /= 8; 2081 2082 size = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(dep->number >> 1)); 2083 if (dwc3_is_usb31(dwc)) 2084 size = DWC31_GTXFIFOSIZ_TXFDEF(size); 2085 else 2086 size = DWC3_GTXFIFOSIZ_TXFDEF(size); 2087 2088 /* FIFO Depth is in MDWDITH bytes. Multiply */ 2089 size *= mdwidth; 2090 2091 kbytes = size / 1024; 2092 if (kbytes == 0) 2093 kbytes = 1; 2094 2095 /* 2096 * FIFO sizes account an extra MDWIDTH * (kbytes + 1) bytes for 2097 * internal overhead. We don't really know how these are used, 2098 * but documentation say it exists. 2099 */ 2100 size -= mdwidth * (kbytes + 1); 2101 size /= kbytes; 2102 2103 usb_ep_set_maxpacket_limit(&dep->endpoint, size); 2104 2105 dep->endpoint.max_streams = 15; 2106 dep->endpoint.ops = &dwc3_gadget_ep_ops; 2107 list_add_tail(&dep->endpoint.ep_list, 2108 &dwc->gadget.ep_list); 2109 dep->endpoint.caps.type_iso = true; 2110 dep->endpoint.caps.type_bulk = true; 2111 dep->endpoint.caps.type_int = true; 2112 2113 return dwc3_alloc_trb_pool(dep); 2114 } 2115 2116 static int dwc3_gadget_init_out_endpoint(struct dwc3_ep *dep) 2117 { 2118 struct dwc3 *dwc = dep->dwc; 2119 2120 usb_ep_set_maxpacket_limit(&dep->endpoint, 1024); 2121 dep->endpoint.max_streams = 15; 2122 dep->endpoint.ops = &dwc3_gadget_ep_ops; 2123 list_add_tail(&dep->endpoint.ep_list, 2124 &dwc->gadget.ep_list); 2125 dep->endpoint.caps.type_iso = true; 2126 dep->endpoint.caps.type_bulk = true; 2127 dep->endpoint.caps.type_int = true; 2128 2129 return dwc3_alloc_trb_pool(dep); 2130 } 2131 2132 static int dwc3_gadget_init_endpoint(struct dwc3 *dwc, u8 epnum) 2133 { 2134 struct dwc3_ep *dep; 2135 bool direction = epnum & 1; 2136 int ret; 2137 u8 num = epnum >> 1; 2138 2139 dep = kzalloc(sizeof(*dep), GFP_KERNEL); 2140 if (!dep) 2141 return -ENOMEM; 2142 2143 dep->dwc = dwc; 2144 dep->number = epnum; 2145 dep->direction = direction; 2146 dep->regs = dwc->regs + DWC3_DEP_BASE(epnum); 2147 dwc->eps[epnum] = dep; 2148 2149 snprintf(dep->name, sizeof(dep->name), "ep%u%s", num, 2150 direction ? "in" : "out"); 2151 2152 dep->endpoint.name = dep->name; 2153 2154 if (!(dep->number > 1)) { 2155 dep->endpoint.desc = &dwc3_gadget_ep0_desc; 2156 dep->endpoint.comp_desc = NULL; 2157 } 2158 2159 spin_lock_init(&dep->lock); 2160 2161 if (num == 0) 2162 ret = dwc3_gadget_init_control_endpoint(dep); 2163 else if (direction) 2164 ret = dwc3_gadget_init_in_endpoint(dep); 2165 else 2166 ret = dwc3_gadget_init_out_endpoint(dep); 2167 2168 if (ret) 2169 return ret; 2170 2171 dep->endpoint.caps.dir_in = direction; 2172 dep->endpoint.caps.dir_out = !direction; 2173 2174 INIT_LIST_HEAD(&dep->pending_list); 2175 INIT_LIST_HEAD(&dep->started_list); 2176 2177 return 0; 2178 } 2179 2180 static int dwc3_gadget_init_endpoints(struct dwc3 *dwc, u8 total) 2181 { 2182 u8 epnum; 2183 2184 INIT_LIST_HEAD(&dwc->gadget.ep_list); 2185 2186 for (epnum = 0; epnum < total; epnum++) { 2187 int ret; 2188 2189 ret = dwc3_gadget_init_endpoint(dwc, epnum); 2190 if (ret) 2191 return ret; 2192 } 2193 2194 return 0; 2195 } 2196 2197 static void dwc3_gadget_free_endpoints(struct dwc3 *dwc) 2198 { 2199 struct dwc3_ep *dep; 2200 u8 epnum; 2201 2202 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) { 2203 dep = dwc->eps[epnum]; 2204 if (!dep) 2205 continue; 2206 /* 2207 * Physical endpoints 0 and 1 are special; they form the 2208 * bi-directional USB endpoint 0. 2209 * 2210 * For those two physical endpoints, we don't allocate a TRB 2211 * pool nor do we add them the endpoints list. Due to that, we 2212 * shouldn't do these two operations otherwise we would end up 2213 * with all sorts of bugs when removing dwc3.ko. 2214 */ 2215 if (epnum != 0 && epnum != 1) { 2216 dwc3_free_trb_pool(dep); 2217 list_del(&dep->endpoint.ep_list); 2218 } 2219 2220 kfree(dep); 2221 } 2222 } 2223 2224 /* -------------------------------------------------------------------------- */ 2225 2226 static int dwc3_gadget_ep_reclaim_completed_trb(struct dwc3_ep *dep, 2227 struct dwc3_request *req, struct dwc3_trb *trb, 2228 const struct dwc3_event_depevt *event, int status, int chain) 2229 { 2230 unsigned int count; 2231 2232 dwc3_ep_inc_deq(dep); 2233 2234 trace_dwc3_complete_trb(dep, trb); 2235 2236 /* 2237 * If we're in the middle of series of chained TRBs and we 2238 * receive a short transfer along the way, DWC3 will skip 2239 * through all TRBs including the last TRB in the chain (the 2240 * where CHN bit is zero. DWC3 will also avoid clearing HWO 2241 * bit and SW has to do it manually. 2242 * 2243 * We're going to do that here to avoid problems of HW trying 2244 * to use bogus TRBs for transfers. 2245 */ 2246 if (chain && (trb->ctrl & DWC3_TRB_CTRL_HWO)) 2247 trb->ctrl &= ~DWC3_TRB_CTRL_HWO; 2248 2249 /* 2250 * If we're dealing with unaligned size OUT transfer, we will be left 2251 * with one TRB pending in the ring. We need to manually clear HWO bit 2252 * from that TRB. 2253 */ 2254 if ((req->zero || req->unaligned) && (trb->ctrl & DWC3_TRB_CTRL_HWO)) { 2255 trb->ctrl &= ~DWC3_TRB_CTRL_HWO; 2256 return 1; 2257 } 2258 2259 count = trb->size & DWC3_TRB_SIZE_MASK; 2260 req->remaining += count; 2261 2262 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN) 2263 return 1; 2264 2265 if (event->status & DEPEVT_STATUS_SHORT && !chain) 2266 return 1; 2267 2268 if (event->status & DEPEVT_STATUS_IOC) 2269 return 1; 2270 2271 return 0; 2272 } 2273 2274 static int dwc3_gadget_ep_reclaim_trb_sg(struct dwc3_ep *dep, 2275 struct dwc3_request *req, const struct dwc3_event_depevt *event, 2276 int status) 2277 { 2278 struct dwc3_trb *trb = &dep->trb_pool[dep->trb_dequeue]; 2279 struct scatterlist *sg = req->sg; 2280 struct scatterlist *s; 2281 unsigned int pending = req->num_pending_sgs; 2282 unsigned int i; 2283 int ret = 0; 2284 2285 for_each_sg(sg, s, pending, i) { 2286 trb = &dep->trb_pool[dep->trb_dequeue]; 2287 2288 if (trb->ctrl & DWC3_TRB_CTRL_HWO) 2289 break; 2290 2291 req->sg = sg_next(s); 2292 req->num_pending_sgs--; 2293 2294 ret = dwc3_gadget_ep_reclaim_completed_trb(dep, req, 2295 trb, event, status, true); 2296 if (ret) 2297 break; 2298 } 2299 2300 return ret; 2301 } 2302 2303 static int dwc3_gadget_ep_reclaim_trb_linear(struct dwc3_ep *dep, 2304 struct dwc3_request *req, const struct dwc3_event_depevt *event, 2305 int status) 2306 { 2307 struct dwc3_trb *trb = &dep->trb_pool[dep->trb_dequeue]; 2308 2309 return dwc3_gadget_ep_reclaim_completed_trb(dep, req, trb, 2310 event, status, false); 2311 } 2312 2313 static bool dwc3_gadget_ep_request_completed(struct dwc3_request *req) 2314 { 2315 return req->request.actual == req->request.length; 2316 } 2317 2318 static int dwc3_gadget_ep_cleanup_completed_request(struct dwc3_ep *dep, 2319 const struct dwc3_event_depevt *event, 2320 struct dwc3_request *req, int status) 2321 { 2322 int ret; 2323 2324 if (req->num_pending_sgs) 2325 ret = dwc3_gadget_ep_reclaim_trb_sg(dep, req, event, 2326 status); 2327 else 2328 ret = dwc3_gadget_ep_reclaim_trb_linear(dep, req, event, 2329 status); 2330 2331 if (req->unaligned || req->zero) { 2332 ret = dwc3_gadget_ep_reclaim_trb_linear(dep, req, event, 2333 status); 2334 req->unaligned = false; 2335 req->zero = false; 2336 } 2337 2338 req->request.actual = req->request.length - req->remaining; 2339 2340 if (!dwc3_gadget_ep_request_completed(req) && 2341 req->num_pending_sgs) { 2342 __dwc3_gadget_kick_transfer(dep); 2343 goto out; 2344 } 2345 2346 dwc3_gadget_giveback(dep, req, status); 2347 2348 out: 2349 return ret; 2350 } 2351 2352 static void dwc3_gadget_ep_cleanup_completed_requests(struct dwc3_ep *dep, 2353 const struct dwc3_event_depevt *event, int status) 2354 { 2355 struct dwc3_request *req; 2356 struct dwc3_request *tmp; 2357 2358 list_for_each_entry_safe(req, tmp, &dep->started_list, list) { 2359 int ret; 2360 2361 ret = dwc3_gadget_ep_cleanup_completed_request(dep, event, 2362 req, status); 2363 if (ret) 2364 break; 2365 } 2366 } 2367 2368 static void dwc3_gadget_endpoint_frame_from_event(struct dwc3_ep *dep, 2369 const struct dwc3_event_depevt *event) 2370 { 2371 dep->frame_number = event->parameters; 2372 } 2373 2374 static void dwc3_gadget_endpoint_transfer_in_progress(struct dwc3_ep *dep, 2375 const struct dwc3_event_depevt *event) 2376 { 2377 struct dwc3 *dwc = dep->dwc; 2378 unsigned status = 0; 2379 bool stop = false; 2380 2381 dwc3_gadget_endpoint_frame_from_event(dep, event); 2382 2383 if (event->status & DEPEVT_STATUS_BUSERR) 2384 status = -ECONNRESET; 2385 2386 if (event->status & DEPEVT_STATUS_MISSED_ISOC) { 2387 status = -EXDEV; 2388 2389 if (list_empty(&dep->started_list)) 2390 stop = true; 2391 } 2392 2393 dwc3_gadget_ep_cleanup_completed_requests(dep, event, status); 2394 2395 if (stop) { 2396 dwc3_stop_active_transfer(dep, true); 2397 dep->flags = DWC3_EP_ENABLED; 2398 } 2399 2400 /* 2401 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround. 2402 * See dwc3_gadget_linksts_change_interrupt() for 1st half. 2403 */ 2404 if (dwc->revision < DWC3_REVISION_183A) { 2405 u32 reg; 2406 int i; 2407 2408 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) { 2409 dep = dwc->eps[i]; 2410 2411 if (!(dep->flags & DWC3_EP_ENABLED)) 2412 continue; 2413 2414 if (!list_empty(&dep->started_list)) 2415 return; 2416 } 2417 2418 reg = dwc3_readl(dwc->regs, DWC3_DCTL); 2419 reg |= dwc->u1u2; 2420 dwc3_writel(dwc->regs, DWC3_DCTL, reg); 2421 2422 dwc->u1u2 = 0; 2423 } 2424 } 2425 2426 static void dwc3_gadget_endpoint_transfer_not_ready(struct dwc3_ep *dep, 2427 const struct dwc3_event_depevt *event) 2428 { 2429 dwc3_gadget_endpoint_frame_from_event(dep, event); 2430 __dwc3_gadget_start_isoc(dep); 2431 } 2432 2433 static void dwc3_endpoint_interrupt(struct dwc3 *dwc, 2434 const struct dwc3_event_depevt *event) 2435 { 2436 struct dwc3_ep *dep; 2437 u8 epnum = event->endpoint_number; 2438 u8 cmd; 2439 2440 dep = dwc->eps[epnum]; 2441 2442 if (!(dep->flags & DWC3_EP_ENABLED)) { 2443 if (!(dep->flags & DWC3_EP_END_TRANSFER_PENDING)) 2444 return; 2445 2446 /* Handle only EPCMDCMPLT when EP disabled */ 2447 if (event->endpoint_event != DWC3_DEPEVT_EPCMDCMPLT) 2448 return; 2449 } 2450 2451 if (epnum == 0 || epnum == 1) { 2452 dwc3_ep0_interrupt(dwc, event); 2453 return; 2454 } 2455 2456 switch (event->endpoint_event) { 2457 case DWC3_DEPEVT_XFERINPROGRESS: 2458 dwc3_gadget_endpoint_transfer_in_progress(dep, event); 2459 break; 2460 case DWC3_DEPEVT_XFERNOTREADY: 2461 dwc3_gadget_endpoint_transfer_not_ready(dep, event); 2462 break; 2463 case DWC3_DEPEVT_EPCMDCMPLT: 2464 cmd = DEPEVT_PARAMETER_CMD(event->parameters); 2465 2466 if (cmd == DWC3_DEPCMD_ENDTRANSFER) { 2467 dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING; 2468 wake_up(&dep->wait_end_transfer); 2469 } 2470 break; 2471 case DWC3_DEPEVT_STREAMEVT: 2472 case DWC3_DEPEVT_XFERCOMPLETE: 2473 case DWC3_DEPEVT_RXTXFIFOEVT: 2474 break; 2475 } 2476 } 2477 2478 static void dwc3_disconnect_gadget(struct dwc3 *dwc) 2479 { 2480 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) { 2481 spin_unlock(&dwc->lock); 2482 dwc->gadget_driver->disconnect(&dwc->gadget); 2483 spin_lock(&dwc->lock); 2484 } 2485 } 2486 2487 static void dwc3_suspend_gadget(struct dwc3 *dwc) 2488 { 2489 if (dwc->gadget_driver && dwc->gadget_driver->suspend) { 2490 spin_unlock(&dwc->lock); 2491 dwc->gadget_driver->suspend(&dwc->gadget); 2492 spin_lock(&dwc->lock); 2493 } 2494 } 2495 2496 static void dwc3_resume_gadget(struct dwc3 *dwc) 2497 { 2498 if (dwc->gadget_driver && dwc->gadget_driver->resume) { 2499 spin_unlock(&dwc->lock); 2500 dwc->gadget_driver->resume(&dwc->gadget); 2501 spin_lock(&dwc->lock); 2502 } 2503 } 2504 2505 static void dwc3_reset_gadget(struct dwc3 *dwc) 2506 { 2507 if (!dwc->gadget_driver) 2508 return; 2509 2510 if (dwc->gadget.speed != USB_SPEED_UNKNOWN) { 2511 spin_unlock(&dwc->lock); 2512 usb_gadget_udc_reset(&dwc->gadget, dwc->gadget_driver); 2513 spin_lock(&dwc->lock); 2514 } 2515 } 2516 2517 static void dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force) 2518 { 2519 struct dwc3 *dwc = dep->dwc; 2520 struct dwc3_gadget_ep_cmd_params params; 2521 u32 cmd; 2522 int ret; 2523 2524 if ((dep->flags & DWC3_EP_END_TRANSFER_PENDING) || 2525 !dep->resource_index) 2526 return; 2527 2528 /* 2529 * NOTICE: We are violating what the Databook says about the 2530 * EndTransfer command. Ideally we would _always_ wait for the 2531 * EndTransfer Command Completion IRQ, but that's causing too 2532 * much trouble synchronizing between us and gadget driver. 2533 * 2534 * We have discussed this with the IP Provider and it was 2535 * suggested to giveback all requests here, but give HW some 2536 * extra time to synchronize with the interconnect. We're using 2537 * an arbitrary 100us delay for that. 2538 * 2539 * Note also that a similar handling was tested by Synopsys 2540 * (thanks a lot Paul) and nothing bad has come out of it. 2541 * In short, what we're doing is: 2542 * 2543 * - Issue EndTransfer WITH CMDIOC bit set 2544 * - Wait 100us 2545 * 2546 * As of IP version 3.10a of the DWC_usb3 IP, the controller 2547 * supports a mode to work around the above limitation. The 2548 * software can poll the CMDACT bit in the DEPCMD register 2549 * after issuing a EndTransfer command. This mode is enabled 2550 * by writing GUCTL2[14]. This polling is already done in the 2551 * dwc3_send_gadget_ep_cmd() function so if the mode is 2552 * enabled, the EndTransfer command will have completed upon 2553 * returning from this function and we don't need to delay for 2554 * 100us. 2555 * 2556 * This mode is NOT available on the DWC_usb31 IP. 2557 */ 2558 2559 cmd = DWC3_DEPCMD_ENDTRANSFER; 2560 cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0; 2561 cmd |= DWC3_DEPCMD_CMDIOC; 2562 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index); 2563 memset(¶ms, 0, sizeof(params)); 2564 ret = dwc3_send_gadget_ep_cmd(dep, cmd, ¶ms); 2565 WARN_ON_ONCE(ret); 2566 dep->resource_index = 0; 2567 2568 if (dwc3_is_usb31(dwc) || dwc->revision < DWC3_REVISION_310A) { 2569 dep->flags |= DWC3_EP_END_TRANSFER_PENDING; 2570 udelay(100); 2571 } 2572 } 2573 2574 static void dwc3_clear_stall_all_ep(struct dwc3 *dwc) 2575 { 2576 u32 epnum; 2577 2578 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) { 2579 struct dwc3_ep *dep; 2580 int ret; 2581 2582 dep = dwc->eps[epnum]; 2583 if (!dep) 2584 continue; 2585 2586 if (!(dep->flags & DWC3_EP_STALL)) 2587 continue; 2588 2589 dep->flags &= ~DWC3_EP_STALL; 2590 2591 ret = dwc3_send_clear_stall_ep_cmd(dep); 2592 WARN_ON_ONCE(ret); 2593 } 2594 } 2595 2596 static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc) 2597 { 2598 int reg; 2599 2600 reg = dwc3_readl(dwc->regs, DWC3_DCTL); 2601 reg &= ~DWC3_DCTL_INITU1ENA; 2602 dwc3_writel(dwc->regs, DWC3_DCTL, reg); 2603 2604 reg &= ~DWC3_DCTL_INITU2ENA; 2605 dwc3_writel(dwc->regs, DWC3_DCTL, reg); 2606 2607 dwc3_disconnect_gadget(dwc); 2608 2609 dwc->gadget.speed = USB_SPEED_UNKNOWN; 2610 dwc->setup_packet_pending = false; 2611 usb_gadget_set_state(&dwc->gadget, USB_STATE_NOTATTACHED); 2612 2613 dwc->connected = false; 2614 } 2615 2616 static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc) 2617 { 2618 u32 reg; 2619 2620 dwc->connected = true; 2621 2622 /* 2623 * WORKAROUND: DWC3 revisions <1.88a have an issue which 2624 * would cause a missing Disconnect Event if there's a 2625 * pending Setup Packet in the FIFO. 2626 * 2627 * There's no suggested workaround on the official Bug 2628 * report, which states that "unless the driver/application 2629 * is doing any special handling of a disconnect event, 2630 * there is no functional issue". 2631 * 2632 * Unfortunately, it turns out that we _do_ some special 2633 * handling of a disconnect event, namely complete all 2634 * pending transfers, notify gadget driver of the 2635 * disconnection, and so on. 2636 * 2637 * Our suggested workaround is to follow the Disconnect 2638 * Event steps here, instead, based on a setup_packet_pending 2639 * flag. Such flag gets set whenever we have a SETUP_PENDING 2640 * status for EP0 TRBs and gets cleared on XferComplete for the 2641 * same endpoint. 2642 * 2643 * Refers to: 2644 * 2645 * STAR#9000466709: RTL: Device : Disconnect event not 2646 * generated if setup packet pending in FIFO 2647 */ 2648 if (dwc->revision < DWC3_REVISION_188A) { 2649 if (dwc->setup_packet_pending) 2650 dwc3_gadget_disconnect_interrupt(dwc); 2651 } 2652 2653 dwc3_reset_gadget(dwc); 2654 2655 reg = dwc3_readl(dwc->regs, DWC3_DCTL); 2656 reg &= ~DWC3_DCTL_TSTCTRL_MASK; 2657 dwc3_writel(dwc->regs, DWC3_DCTL, reg); 2658 dwc->test_mode = false; 2659 dwc3_clear_stall_all_ep(dwc); 2660 2661 /* Reset device address to zero */ 2662 reg = dwc3_readl(dwc->regs, DWC3_DCFG); 2663 reg &= ~(DWC3_DCFG_DEVADDR_MASK); 2664 dwc3_writel(dwc->regs, DWC3_DCFG, reg); 2665 } 2666 2667 static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc) 2668 { 2669 struct dwc3_ep *dep; 2670 int ret; 2671 u32 reg; 2672 u8 speed; 2673 2674 reg = dwc3_readl(dwc->regs, DWC3_DSTS); 2675 speed = reg & DWC3_DSTS_CONNECTSPD; 2676 dwc->speed = speed; 2677 2678 /* 2679 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed 2680 * each time on Connect Done. 2681 * 2682 * Currently we always use the reset value. If any platform 2683 * wants to set this to a different value, we need to add a 2684 * setting and update GCTL.RAMCLKSEL here. 2685 */ 2686 2687 switch (speed) { 2688 case DWC3_DSTS_SUPERSPEED_PLUS: 2689 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512); 2690 dwc->gadget.ep0->maxpacket = 512; 2691 dwc->gadget.speed = USB_SPEED_SUPER_PLUS; 2692 break; 2693 case DWC3_DSTS_SUPERSPEED: 2694 /* 2695 * WORKAROUND: DWC3 revisions <1.90a have an issue which 2696 * would cause a missing USB3 Reset event. 2697 * 2698 * In such situations, we should force a USB3 Reset 2699 * event by calling our dwc3_gadget_reset_interrupt() 2700 * routine. 2701 * 2702 * Refers to: 2703 * 2704 * STAR#9000483510: RTL: SS : USB3 reset event may 2705 * not be generated always when the link enters poll 2706 */ 2707 if (dwc->revision < DWC3_REVISION_190A) 2708 dwc3_gadget_reset_interrupt(dwc); 2709 2710 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512); 2711 dwc->gadget.ep0->maxpacket = 512; 2712 dwc->gadget.speed = USB_SPEED_SUPER; 2713 break; 2714 case DWC3_DSTS_HIGHSPEED: 2715 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64); 2716 dwc->gadget.ep0->maxpacket = 64; 2717 dwc->gadget.speed = USB_SPEED_HIGH; 2718 break; 2719 case DWC3_DSTS_FULLSPEED: 2720 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64); 2721 dwc->gadget.ep0->maxpacket = 64; 2722 dwc->gadget.speed = USB_SPEED_FULL; 2723 break; 2724 case DWC3_DSTS_LOWSPEED: 2725 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8); 2726 dwc->gadget.ep0->maxpacket = 8; 2727 dwc->gadget.speed = USB_SPEED_LOW; 2728 break; 2729 } 2730 2731 dwc->eps[1]->endpoint.maxpacket = dwc->gadget.ep0->maxpacket; 2732 2733 /* Enable USB2 LPM Capability */ 2734 2735 if ((dwc->revision > DWC3_REVISION_194A) && 2736 (speed != DWC3_DSTS_SUPERSPEED) && 2737 (speed != DWC3_DSTS_SUPERSPEED_PLUS)) { 2738 reg = dwc3_readl(dwc->regs, DWC3_DCFG); 2739 reg |= DWC3_DCFG_LPM_CAP; 2740 dwc3_writel(dwc->regs, DWC3_DCFG, reg); 2741 2742 reg = dwc3_readl(dwc->regs, DWC3_DCTL); 2743 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN); 2744 2745 reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold); 2746 2747 /* 2748 * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and 2749 * DCFG.LPMCap is set, core responses with an ACK and the 2750 * BESL value in the LPM token is less than or equal to LPM 2751 * NYET threshold. 2752 */ 2753 WARN_ONCE(dwc->revision < DWC3_REVISION_240A 2754 && dwc->has_lpm_erratum, 2755 "LPM Erratum not available on dwc3 revisions < 2.40a\n"); 2756 2757 if (dwc->has_lpm_erratum && dwc->revision >= DWC3_REVISION_240A) 2758 reg |= DWC3_DCTL_LPM_ERRATA(dwc->lpm_nyet_threshold); 2759 2760 dwc3_writel(dwc->regs, DWC3_DCTL, reg); 2761 } else { 2762 reg = dwc3_readl(dwc->regs, DWC3_DCTL); 2763 reg &= ~DWC3_DCTL_HIRD_THRES_MASK; 2764 dwc3_writel(dwc->regs, DWC3_DCTL, reg); 2765 } 2766 2767 dep = dwc->eps[0]; 2768 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_MODIFY); 2769 if (ret) { 2770 dev_err(dwc->dev, "failed to enable %s\n", dep->name); 2771 return; 2772 } 2773 2774 dep = dwc->eps[1]; 2775 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_MODIFY); 2776 if (ret) { 2777 dev_err(dwc->dev, "failed to enable %s\n", dep->name); 2778 return; 2779 } 2780 2781 /* 2782 * Configure PHY via GUSB3PIPECTLn if required. 2783 * 2784 * Update GTXFIFOSIZn 2785 * 2786 * In both cases reset values should be sufficient. 2787 */ 2788 } 2789 2790 static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc) 2791 { 2792 /* 2793 * TODO take core out of low power mode when that's 2794 * implemented. 2795 */ 2796 2797 if (dwc->gadget_driver && dwc->gadget_driver->resume) { 2798 spin_unlock(&dwc->lock); 2799 dwc->gadget_driver->resume(&dwc->gadget); 2800 spin_lock(&dwc->lock); 2801 } 2802 } 2803 2804 static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc, 2805 unsigned int evtinfo) 2806 { 2807 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK; 2808 unsigned int pwropt; 2809 2810 /* 2811 * WORKAROUND: DWC3 < 2.50a have an issue when configured without 2812 * Hibernation mode enabled which would show up when device detects 2813 * host-initiated U3 exit. 2814 * 2815 * In that case, device will generate a Link State Change Interrupt 2816 * from U3 to RESUME which is only necessary if Hibernation is 2817 * configured in. 2818 * 2819 * There are no functional changes due to such spurious event and we 2820 * just need to ignore it. 2821 * 2822 * Refers to: 2823 * 2824 * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation 2825 * operational mode 2826 */ 2827 pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1); 2828 if ((dwc->revision < DWC3_REVISION_250A) && 2829 (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) { 2830 if ((dwc->link_state == DWC3_LINK_STATE_U3) && 2831 (next == DWC3_LINK_STATE_RESUME)) { 2832 return; 2833 } 2834 } 2835 2836 /* 2837 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending 2838 * on the link partner, the USB session might do multiple entry/exit 2839 * of low power states before a transfer takes place. 2840 * 2841 * Due to this problem, we might experience lower throughput. The 2842 * suggested workaround is to disable DCTL[12:9] bits if we're 2843 * transitioning from U1/U2 to U0 and enable those bits again 2844 * after a transfer completes and there are no pending transfers 2845 * on any of the enabled endpoints. 2846 * 2847 * This is the first half of that workaround. 2848 * 2849 * Refers to: 2850 * 2851 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us 2852 * core send LGO_Ux entering U0 2853 */ 2854 if (dwc->revision < DWC3_REVISION_183A) { 2855 if (next == DWC3_LINK_STATE_U0) { 2856 u32 u1u2; 2857 u32 reg; 2858 2859 switch (dwc->link_state) { 2860 case DWC3_LINK_STATE_U1: 2861 case DWC3_LINK_STATE_U2: 2862 reg = dwc3_readl(dwc->regs, DWC3_DCTL); 2863 u1u2 = reg & (DWC3_DCTL_INITU2ENA 2864 | DWC3_DCTL_ACCEPTU2ENA 2865 | DWC3_DCTL_INITU1ENA 2866 | DWC3_DCTL_ACCEPTU1ENA); 2867 2868 if (!dwc->u1u2) 2869 dwc->u1u2 = reg & u1u2; 2870 2871 reg &= ~u1u2; 2872 2873 dwc3_writel(dwc->regs, DWC3_DCTL, reg); 2874 break; 2875 default: 2876 /* do nothing */ 2877 break; 2878 } 2879 } 2880 } 2881 2882 switch (next) { 2883 case DWC3_LINK_STATE_U1: 2884 if (dwc->speed == USB_SPEED_SUPER) 2885 dwc3_suspend_gadget(dwc); 2886 break; 2887 case DWC3_LINK_STATE_U2: 2888 case DWC3_LINK_STATE_U3: 2889 dwc3_suspend_gadget(dwc); 2890 break; 2891 case DWC3_LINK_STATE_RESUME: 2892 dwc3_resume_gadget(dwc); 2893 break; 2894 default: 2895 /* do nothing */ 2896 break; 2897 } 2898 2899 dwc->link_state = next; 2900 } 2901 2902 static void dwc3_gadget_suspend_interrupt(struct dwc3 *dwc, 2903 unsigned int evtinfo) 2904 { 2905 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK; 2906 2907 if (dwc->link_state != next && next == DWC3_LINK_STATE_U3) 2908 dwc3_suspend_gadget(dwc); 2909 2910 dwc->link_state = next; 2911 } 2912 2913 static void dwc3_gadget_hibernation_interrupt(struct dwc3 *dwc, 2914 unsigned int evtinfo) 2915 { 2916 unsigned int is_ss = evtinfo & BIT(4); 2917 2918 /* 2919 * WORKAROUND: DWC3 revison 2.20a with hibernation support 2920 * have a known issue which can cause USB CV TD.9.23 to fail 2921 * randomly. 2922 * 2923 * Because of this issue, core could generate bogus hibernation 2924 * events which SW needs to ignore. 2925 * 2926 * Refers to: 2927 * 2928 * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0 2929 * Device Fallback from SuperSpeed 2930 */ 2931 if (is_ss ^ (dwc->speed == USB_SPEED_SUPER)) 2932 return; 2933 2934 /* enter hibernation here */ 2935 } 2936 2937 static void dwc3_gadget_interrupt(struct dwc3 *dwc, 2938 const struct dwc3_event_devt *event) 2939 { 2940 switch (event->type) { 2941 case DWC3_DEVICE_EVENT_DISCONNECT: 2942 dwc3_gadget_disconnect_interrupt(dwc); 2943 break; 2944 case DWC3_DEVICE_EVENT_RESET: 2945 dwc3_gadget_reset_interrupt(dwc); 2946 break; 2947 case DWC3_DEVICE_EVENT_CONNECT_DONE: 2948 dwc3_gadget_conndone_interrupt(dwc); 2949 break; 2950 case DWC3_DEVICE_EVENT_WAKEUP: 2951 dwc3_gadget_wakeup_interrupt(dwc); 2952 break; 2953 case DWC3_DEVICE_EVENT_HIBER_REQ: 2954 if (dev_WARN_ONCE(dwc->dev, !dwc->has_hibernation, 2955 "unexpected hibernation event\n")) 2956 break; 2957 2958 dwc3_gadget_hibernation_interrupt(dwc, event->event_info); 2959 break; 2960 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE: 2961 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info); 2962 break; 2963 case DWC3_DEVICE_EVENT_EOPF: 2964 /* It changed to be suspend event for version 2.30a and above */ 2965 if (dwc->revision >= DWC3_REVISION_230A) { 2966 /* 2967 * Ignore suspend event until the gadget enters into 2968 * USB_STATE_CONFIGURED state. 2969 */ 2970 if (dwc->gadget.state >= USB_STATE_CONFIGURED) 2971 dwc3_gadget_suspend_interrupt(dwc, 2972 event->event_info); 2973 } 2974 break; 2975 case DWC3_DEVICE_EVENT_SOF: 2976 case DWC3_DEVICE_EVENT_ERRATIC_ERROR: 2977 case DWC3_DEVICE_EVENT_CMD_CMPL: 2978 case DWC3_DEVICE_EVENT_OVERFLOW: 2979 break; 2980 default: 2981 dev_WARN(dwc->dev, "UNKNOWN IRQ %d\n", event->type); 2982 } 2983 } 2984 2985 static void dwc3_process_event_entry(struct dwc3 *dwc, 2986 const union dwc3_event *event) 2987 { 2988 trace_dwc3_event(event->raw, dwc); 2989 2990 if (!event->type.is_devspec) 2991 dwc3_endpoint_interrupt(dwc, &event->depevt); 2992 else if (event->type.type == DWC3_EVENT_TYPE_DEV) 2993 dwc3_gadget_interrupt(dwc, &event->devt); 2994 else 2995 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw); 2996 } 2997 2998 static irqreturn_t dwc3_process_event_buf(struct dwc3_event_buffer *evt) 2999 { 3000 struct dwc3 *dwc = evt->dwc; 3001 irqreturn_t ret = IRQ_NONE; 3002 int left; 3003 u32 reg; 3004 3005 left = evt->count; 3006 3007 if (!(evt->flags & DWC3_EVENT_PENDING)) 3008 return IRQ_NONE; 3009 3010 while (left > 0) { 3011 union dwc3_event event; 3012 3013 event.raw = *(u32 *) (evt->cache + evt->lpos); 3014 3015 dwc3_process_event_entry(dwc, &event); 3016 3017 /* 3018 * FIXME we wrap around correctly to the next entry as 3019 * almost all entries are 4 bytes in size. There is one 3020 * entry which has 12 bytes which is a regular entry 3021 * followed by 8 bytes data. ATM I don't know how 3022 * things are organized if we get next to the a 3023 * boundary so I worry about that once we try to handle 3024 * that. 3025 */ 3026 evt->lpos = (evt->lpos + 4) % evt->length; 3027 left -= 4; 3028 } 3029 3030 evt->count = 0; 3031 evt->flags &= ~DWC3_EVENT_PENDING; 3032 ret = IRQ_HANDLED; 3033 3034 /* Unmask interrupt */ 3035 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0)); 3036 reg &= ~DWC3_GEVNTSIZ_INTMASK; 3037 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg); 3038 3039 if (dwc->imod_interval) { 3040 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB); 3041 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval); 3042 } 3043 3044 return ret; 3045 } 3046 3047 static irqreturn_t dwc3_thread_interrupt(int irq, void *_evt) 3048 { 3049 struct dwc3_event_buffer *evt = _evt; 3050 struct dwc3 *dwc = evt->dwc; 3051 unsigned long flags; 3052 irqreturn_t ret = IRQ_NONE; 3053 3054 spin_lock_irqsave(&dwc->lock, flags); 3055 ret = dwc3_process_event_buf(evt); 3056 spin_unlock_irqrestore(&dwc->lock, flags); 3057 3058 return ret; 3059 } 3060 3061 static irqreturn_t dwc3_check_event_buf(struct dwc3_event_buffer *evt) 3062 { 3063 struct dwc3 *dwc = evt->dwc; 3064 u32 amount; 3065 u32 count; 3066 u32 reg; 3067 3068 if (pm_runtime_suspended(dwc->dev)) { 3069 pm_runtime_get(dwc->dev); 3070 disable_irq_nosync(dwc->irq_gadget); 3071 dwc->pending_events = true; 3072 return IRQ_HANDLED; 3073 } 3074 3075 /* 3076 * With PCIe legacy interrupt, test shows that top-half irq handler can 3077 * be called again after HW interrupt deassertion. Check if bottom-half 3078 * irq event handler completes before caching new event to prevent 3079 * losing events. 3080 */ 3081 if (evt->flags & DWC3_EVENT_PENDING) 3082 return IRQ_HANDLED; 3083 3084 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0)); 3085 count &= DWC3_GEVNTCOUNT_MASK; 3086 if (!count) 3087 return IRQ_NONE; 3088 3089 evt->count = count; 3090 evt->flags |= DWC3_EVENT_PENDING; 3091 3092 /* Mask interrupt */ 3093 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0)); 3094 reg |= DWC3_GEVNTSIZ_INTMASK; 3095 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg); 3096 3097 amount = min(count, evt->length - evt->lpos); 3098 memcpy(evt->cache + evt->lpos, evt->buf + evt->lpos, amount); 3099 3100 if (amount < count) 3101 memcpy(evt->cache, evt->buf, count - amount); 3102 3103 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), count); 3104 3105 return IRQ_WAKE_THREAD; 3106 } 3107 3108 static irqreturn_t dwc3_interrupt(int irq, void *_evt) 3109 { 3110 struct dwc3_event_buffer *evt = _evt; 3111 3112 return dwc3_check_event_buf(evt); 3113 } 3114 3115 static int dwc3_gadget_get_irq(struct dwc3 *dwc) 3116 { 3117 struct platform_device *dwc3_pdev = to_platform_device(dwc->dev); 3118 int irq; 3119 3120 irq = platform_get_irq_byname(dwc3_pdev, "peripheral"); 3121 if (irq > 0) 3122 goto out; 3123 3124 if (irq == -EPROBE_DEFER) 3125 goto out; 3126 3127 irq = platform_get_irq_byname(dwc3_pdev, "dwc_usb3"); 3128 if (irq > 0) 3129 goto out; 3130 3131 if (irq == -EPROBE_DEFER) 3132 goto out; 3133 3134 irq = platform_get_irq(dwc3_pdev, 0); 3135 if (irq > 0) 3136 goto out; 3137 3138 if (irq != -EPROBE_DEFER) 3139 dev_err(dwc->dev, "missing peripheral IRQ\n"); 3140 3141 if (!irq) 3142 irq = -EINVAL; 3143 3144 out: 3145 return irq; 3146 } 3147 3148 /** 3149 * dwc3_gadget_init - initializes gadget related registers 3150 * @dwc: pointer to our controller context structure 3151 * 3152 * Returns 0 on success otherwise negative errno. 3153 */ 3154 int dwc3_gadget_init(struct dwc3 *dwc) 3155 { 3156 int ret; 3157 int irq; 3158 3159 irq = dwc3_gadget_get_irq(dwc); 3160 if (irq < 0) { 3161 ret = irq; 3162 goto err0; 3163 } 3164 3165 dwc->irq_gadget = irq; 3166 3167 dwc->ep0_trb = dma_alloc_coherent(dwc->sysdev, 3168 sizeof(*dwc->ep0_trb) * 2, 3169 &dwc->ep0_trb_addr, GFP_KERNEL); 3170 if (!dwc->ep0_trb) { 3171 dev_err(dwc->dev, "failed to allocate ep0 trb\n"); 3172 ret = -ENOMEM; 3173 goto err0; 3174 } 3175 3176 dwc->setup_buf = kzalloc(DWC3_EP0_SETUP_SIZE, GFP_KERNEL); 3177 if (!dwc->setup_buf) { 3178 ret = -ENOMEM; 3179 goto err1; 3180 } 3181 3182 dwc->bounce = dma_alloc_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, 3183 &dwc->bounce_addr, GFP_KERNEL); 3184 if (!dwc->bounce) { 3185 ret = -ENOMEM; 3186 goto err2; 3187 } 3188 3189 init_completion(&dwc->ep0_in_setup); 3190 3191 dwc->gadget.ops = &dwc3_gadget_ops; 3192 dwc->gadget.speed = USB_SPEED_UNKNOWN; 3193 dwc->gadget.sg_supported = true; 3194 dwc->gadget.name = "dwc3-gadget"; 3195 dwc->gadget.is_otg = dwc->dr_mode == USB_DR_MODE_OTG; 3196 3197 /* 3198 * FIXME We might be setting max_speed to <SUPER, however versions 3199 * <2.20a of dwc3 have an issue with metastability (documented 3200 * elsewhere in this driver) which tells us we can't set max speed to 3201 * anything lower than SUPER. 3202 * 3203 * Because gadget.max_speed is only used by composite.c and function 3204 * drivers (i.e. it won't go into dwc3's registers) we are allowing this 3205 * to happen so we avoid sending SuperSpeed Capability descriptor 3206 * together with our BOS descriptor as that could confuse host into 3207 * thinking we can handle super speed. 3208 * 3209 * Note that, in fact, we won't even support GetBOS requests when speed 3210 * is less than super speed because we don't have means, yet, to tell 3211 * composite.c that we are USB 2.0 + LPM ECN. 3212 */ 3213 if (dwc->revision < DWC3_REVISION_220A && 3214 !dwc->dis_metastability_quirk) 3215 dev_info(dwc->dev, "changing max_speed on rev %08x\n", 3216 dwc->revision); 3217 3218 dwc->gadget.max_speed = dwc->maximum_speed; 3219 3220 /* 3221 * REVISIT: Here we should clear all pending IRQs to be 3222 * sure we're starting from a well known location. 3223 */ 3224 3225 ret = dwc3_gadget_init_endpoints(dwc, dwc->num_eps); 3226 if (ret) 3227 goto err3; 3228 3229 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget); 3230 if (ret) { 3231 dev_err(dwc->dev, "failed to register udc\n"); 3232 goto err4; 3233 } 3234 3235 return 0; 3236 3237 err4: 3238 dwc3_gadget_free_endpoints(dwc); 3239 3240 err3: 3241 dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce, 3242 dwc->bounce_addr); 3243 3244 err2: 3245 kfree(dwc->setup_buf); 3246 3247 err1: 3248 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2, 3249 dwc->ep0_trb, dwc->ep0_trb_addr); 3250 3251 err0: 3252 return ret; 3253 } 3254 3255 /* -------------------------------------------------------------------------- */ 3256 3257 void dwc3_gadget_exit(struct dwc3 *dwc) 3258 { 3259 usb_del_gadget_udc(&dwc->gadget); 3260 dwc3_gadget_free_endpoints(dwc); 3261 dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce, 3262 dwc->bounce_addr); 3263 kfree(dwc->setup_buf); 3264 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2, 3265 dwc->ep0_trb, dwc->ep0_trb_addr); 3266 } 3267 3268 int dwc3_gadget_suspend(struct dwc3 *dwc) 3269 { 3270 if (!dwc->gadget_driver) 3271 return 0; 3272 3273 dwc3_gadget_run_stop(dwc, false, false); 3274 dwc3_disconnect_gadget(dwc); 3275 __dwc3_gadget_stop(dwc); 3276 3277 return 0; 3278 } 3279 3280 int dwc3_gadget_resume(struct dwc3 *dwc) 3281 { 3282 int ret; 3283 3284 if (!dwc->gadget_driver) 3285 return 0; 3286 3287 ret = __dwc3_gadget_start(dwc); 3288 if (ret < 0) 3289 goto err0; 3290 3291 ret = dwc3_gadget_run_stop(dwc, true, false); 3292 if (ret < 0) 3293 goto err1; 3294 3295 return 0; 3296 3297 err1: 3298 __dwc3_gadget_stop(dwc); 3299 3300 err0: 3301 return ret; 3302 } 3303 3304 void dwc3_gadget_process_pending_events(struct dwc3 *dwc) 3305 { 3306 if (dwc->pending_events) { 3307 dwc3_interrupt(dwc->irq_gadget, dwc->ev_buf); 3308 dwc->pending_events = false; 3309 enable_irq(dwc->irq_gadget); 3310 } 3311 } 3312