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 - https://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, n) (((d)->frame_number + ((d)->interval * (n))) \ 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, DWC3_DCTL); 46 reg &= ~DWC3_DCTL_TSTCTRL_MASK; 47 48 switch (mode) { 49 case USB_TEST_J: 50 case USB_TEST_K: 51 case USB_TEST_SE0_NAK: 52 case USB_TEST_PACKET: 53 case USB_TEST_FORCE_ENABLE: 54 reg |= mode << 1; 55 break; 56 default: 57 return -EINVAL; 58 } 59 60 dwc3_gadget_dctl_write_safe(dwc, 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, 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 (!DWC3_VER_IS_PRIOR(DWC3, 194A)) { 99 while (--retries) { 100 reg = dwc3_readl(dwc, 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, DWC3_DCTL); 112 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK; 113 114 /* set no action before sending new link state change */ 115 dwc3_writel(dwc, DWC3_DCTL, reg); 116 117 /* set requested state */ 118 reg |= DWC3_DCTL_ULSTCHNGREQ(state); 119 dwc3_writel(dwc, DWC3_DCTL, reg); 120 121 /* 122 * The following code is racy when called from dwc3_gadget_wakeup, 123 * and is not needed, at least on newer versions 124 */ 125 if (!DWC3_VER_IS_PRIOR(DWC3, 194A)) 126 return 0; 127 128 /* wait for a change in DSTS */ 129 retries = 10000; 130 while (--retries) { 131 reg = dwc3_readl(dwc, DWC3_DSTS); 132 133 if (DWC3_DSTS_USBLNKST(reg) == state) 134 return 0; 135 136 udelay(5); 137 } 138 139 return -ETIMEDOUT; 140 } 141 142 static void dwc3_ep0_reset_state(struct dwc3 *dwc) 143 { 144 unsigned int dir; 145 146 if (dwc->ep0state != EP0_SETUP_PHASE) { 147 dir = !!dwc->ep0_expect_in; 148 if (dwc->ep0state == EP0_DATA_PHASE) 149 dwc3_ep0_end_control_data(dwc, dwc->eps[dir]); 150 else 151 dwc3_ep0_end_control_data(dwc, dwc->eps[!dir]); 152 153 dwc->eps[0]->trb_enqueue = 0; 154 dwc->eps[1]->trb_enqueue = 0; 155 156 dwc3_ep0_stall_and_restart(dwc); 157 } 158 } 159 160 /** 161 * dwc3_ep_inc_trb - increment a trb index. 162 * @index: Pointer to the TRB index to increment. 163 * 164 * The index should never point to the link TRB. After incrementing, 165 * if it is point to the link TRB, wrap around to the beginning. The 166 * link TRB is always at the last TRB entry. 167 */ 168 static void dwc3_ep_inc_trb(u8 *index) 169 { 170 (*index)++; 171 if (*index == (DWC3_TRB_NUM - 1)) 172 *index = 0; 173 } 174 175 /** 176 * dwc3_ep_inc_enq - increment endpoint's enqueue pointer 177 * @dep: The endpoint whose enqueue pointer we're incrementing 178 */ 179 static void dwc3_ep_inc_enq(struct dwc3_ep *dep) 180 { 181 dwc3_ep_inc_trb(&dep->trb_enqueue); 182 } 183 184 /** 185 * dwc3_ep_inc_deq - increment endpoint's dequeue pointer 186 * @dep: The endpoint whose enqueue pointer we're incrementing 187 */ 188 static void dwc3_ep_inc_deq(struct dwc3_ep *dep) 189 { 190 dwc3_ep_inc_trb(&dep->trb_dequeue); 191 } 192 193 static void dwc3_gadget_del_and_unmap_request(struct dwc3_ep *dep, 194 struct dwc3_request *req, int status) 195 { 196 struct dwc3 *dwc = dep->dwc; 197 198 list_del(&req->list); 199 req->remaining = 0; 200 req->num_trbs = 0; 201 202 if (req->request.status == -EINPROGRESS) 203 req->request.status = status; 204 205 if (req->trb) 206 usb_gadget_unmap_request_by_dev(dwc->sysdev, 207 &req->request, req->direction); 208 209 req->trb = NULL; 210 trace_dwc3_gadget_giveback(req); 211 212 if (dep->number > 1) 213 pm_runtime_put(dwc->dev); 214 } 215 216 /** 217 * dwc3_gadget_giveback - call struct usb_request's ->complete callback 218 * @dep: The endpoint to whom the request belongs to 219 * @req: The request we're giving back 220 * @status: completion code for the request 221 * 222 * Must be called with controller's lock held and interrupts disabled. This 223 * function will unmap @req and call its ->complete() callback to notify upper 224 * layers that it has completed. 225 */ 226 void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req, 227 int status) 228 { 229 struct dwc3 *dwc = dep->dwc; 230 231 /* 232 * The request might have been processed and completed while the 233 * spinlock was released. Skip processing if already completed. 234 */ 235 if (req->status == DWC3_REQUEST_STATUS_COMPLETED) 236 return; 237 238 dwc3_gadget_del_and_unmap_request(dep, req, status); 239 req->status = DWC3_REQUEST_STATUS_COMPLETED; 240 241 spin_unlock(&dwc->lock); 242 usb_gadget_giveback_request(&dep->endpoint, &req->request); 243 spin_lock(&dwc->lock); 244 } 245 246 /** 247 * dwc3_send_gadget_generic_command - issue a generic command for the controller 248 * @dwc: pointer to the controller context 249 * @cmd: the command to be issued 250 * @param: command parameter 251 * 252 * Caller should take care of locking. Issue @cmd with a given @param to @dwc 253 * and wait for its completion. 254 */ 255 int dwc3_send_gadget_generic_command(struct dwc3 *dwc, unsigned int cmd, 256 u32 param) 257 { 258 u32 timeout = 500; 259 int status = 0; 260 int ret = 0; 261 u32 reg; 262 263 dwc3_writel(dwc, DWC3_DGCMDPAR, param); 264 dwc3_writel(dwc, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT); 265 266 do { 267 reg = dwc3_readl(dwc, DWC3_DGCMD); 268 if (!(reg & DWC3_DGCMD_CMDACT)) { 269 status = DWC3_DGCMD_STATUS(reg); 270 if (status) 271 ret = -EINVAL; 272 break; 273 } 274 } while (--timeout); 275 276 if (!timeout) { 277 ret = -ETIMEDOUT; 278 status = -ETIMEDOUT; 279 } 280 281 trace_dwc3_gadget_generic_cmd(dwc, cmd, param, status); 282 283 return ret; 284 } 285 286 /** 287 * dwc3_send_gadget_ep_cmd - issue an endpoint command 288 * @dep: the endpoint to which the command is going to be issued 289 * @cmd: the command to be issued 290 * @params: parameters to the command 291 * 292 * Caller should handle locking. This function will issue @cmd with given 293 * @params to @dep and wait for its completion. 294 * 295 * According to the programming guide, if the link state is in L1/L2/U3, 296 * then sending the Start Transfer command may not complete. The 297 * programming guide suggested to bring the link state back to ON/U0 by 298 * performing remote wakeup prior to sending the command. However, don't 299 * initiate remote wakeup when the user/function does not send wakeup 300 * request via wakeup ops. Send the command when it's allowed. 301 * 302 * Notes: 303 * For L1 link state, issuing a command requires the clearing of 304 * GUSB2PHYCFG.SUSPENDUSB2, which turns on the signal required to complete 305 * the given command (usually within 50us). This should happen within the 306 * command timeout set by driver. No additional step is needed. 307 * 308 * For L2 or U3 link state, the gadget is in USB suspend. Care should be 309 * taken when sending Start Transfer command to ensure that it's done after 310 * USB resume. 311 */ 312 int dwc3_send_gadget_ep_cmd(struct dwc3_ep *dep, unsigned int cmd, 313 struct dwc3_gadget_ep_cmd_params *params) 314 { 315 const struct usb_endpoint_descriptor *desc = dep->endpoint.desc; 316 struct dwc3 *dwc = dep->dwc; 317 u32 timeout = 5000; 318 u32 saved_config = 0; 319 u32 reg; 320 321 int cmd_status = 0; 322 int ret = -EINVAL; 323 u8 epnum = dep->number; 324 325 /* 326 * When operating in USB 2.0 speeds (HS/FS), if GUSB2PHYCFG.ENBLSLPM or 327 * GUSB2PHYCFG.SUSPHY is set, it must be cleared before issuing an 328 * endpoint command. 329 * 330 * Save and clear both GUSB2PHYCFG.ENBLSLPM and GUSB2PHYCFG.SUSPHY 331 * settings. Restore them after the command is completed. 332 * 333 * DWC_usb3 3.30a and DWC_usb31 1.90a programming guide section 3.2.2 334 */ 335 if (dwc->gadget->speed <= USB_SPEED_HIGH || 336 DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_ENDTRANSFER) { 337 reg = dwc3_readl(dwc, DWC3_GUSB2PHYCFG(0)); 338 if (unlikely(reg & DWC3_GUSB2PHYCFG_SUSPHY)) { 339 saved_config |= DWC3_GUSB2PHYCFG_SUSPHY; 340 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY; 341 } 342 343 if (reg & DWC3_GUSB2PHYCFG_ENBLSLPM) { 344 saved_config |= DWC3_GUSB2PHYCFG_ENBLSLPM; 345 reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM; 346 } 347 348 if (saved_config) 349 dwc3_writel(dwc, DWC3_GUSB2PHYCFG(0), reg); 350 } 351 352 /* 353 * For some commands such as Update Transfer command, DEPCMDPARn 354 * registers are reserved. Since the driver often sends Update Transfer 355 * command, don't write to DEPCMDPARn to avoid register write delays and 356 * improve performance. 357 */ 358 if (DWC3_DEPCMD_CMD(cmd) != DWC3_DEPCMD_UPDATETRANSFER) { 359 dwc3_writel(dwc, DWC3_DEPCMDPAR0(epnum), params->param0); 360 dwc3_writel(dwc, DWC3_DEPCMDPAR1(epnum), params->param1); 361 dwc3_writel(dwc, DWC3_DEPCMDPAR2(epnum), params->param2); 362 } 363 364 /* 365 * Synopsys Databook 2.60a states in section 6.3.2.5.6 of that if we're 366 * not relying on XferNotReady, we can make use of a special "No 367 * Response Update Transfer" command where we should clear both CmdAct 368 * and CmdIOC bits. 369 * 370 * With this, we don't need to wait for command completion and can 371 * straight away issue further commands to the endpoint. 372 * 373 * NOTICE: We're making an assumption that control endpoints will never 374 * make use of Update Transfer command. This is a safe assumption 375 * because we can never have more than one request at a time with 376 * Control Endpoints. If anybody changes that assumption, this chunk 377 * needs to be updated accordingly. 378 */ 379 if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_UPDATETRANSFER && 380 !usb_endpoint_xfer_isoc(desc)) 381 cmd &= ~(DWC3_DEPCMD_CMDIOC | DWC3_DEPCMD_CMDACT); 382 else 383 cmd |= DWC3_DEPCMD_CMDACT; 384 385 dwc3_writel(dwc, DWC3_DEPCMD(epnum), cmd); 386 387 if (!(cmd & DWC3_DEPCMD_CMDACT) || 388 (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_ENDTRANSFER && 389 !(cmd & DWC3_DEPCMD_CMDIOC))) { 390 ret = 0; 391 goto skip_status; 392 } 393 394 do { 395 reg = dwc3_readl(dwc, DWC3_DEPCMD(epnum)); 396 if (!(reg & DWC3_DEPCMD_CMDACT)) { 397 cmd_status = DWC3_DEPCMD_STATUS(reg); 398 399 switch (cmd_status) { 400 case 0: 401 ret = 0; 402 break; 403 case DEPEVT_TRANSFER_NO_RESOURCE: 404 dev_WARN(dwc->dev, "No resource for %s\n", 405 dep->name); 406 ret = -EINVAL; 407 break; 408 case DEPEVT_TRANSFER_BUS_EXPIRY: 409 /* 410 * SW issues START TRANSFER command to 411 * isochronous ep with future frame interval. If 412 * future interval time has already passed when 413 * core receives the command, it will respond 414 * with an error status of 'Bus Expiry'. 415 * 416 * Instead of always returning -EINVAL, let's 417 * give a hint to the gadget driver that this is 418 * the case by returning -EAGAIN. 419 */ 420 ret = -EAGAIN; 421 break; 422 default: 423 dev_WARN(dwc->dev, "UNKNOWN cmd status\n"); 424 } 425 426 break; 427 } 428 } while (--timeout); 429 430 if (timeout == 0) { 431 ret = -ETIMEDOUT; 432 cmd_status = -ETIMEDOUT; 433 } 434 435 skip_status: 436 trace_dwc3_gadget_ep_cmd(dep, cmd, params, cmd_status); 437 438 if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_STARTTRANSFER) { 439 if (ret == 0) 440 dep->flags |= DWC3_EP_TRANSFER_STARTED; 441 442 if (ret != -ETIMEDOUT) 443 dwc3_gadget_ep_get_transfer_index(dep); 444 } 445 446 if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_ENDTRANSFER && 447 !(cmd & DWC3_DEPCMD_CMDIOC)) 448 mdelay(1); 449 450 if (saved_config) { 451 reg = dwc3_readl(dwc, DWC3_GUSB2PHYCFG(0)); 452 reg |= saved_config; 453 dwc3_writel(dwc, DWC3_GUSB2PHYCFG(0), reg); 454 } 455 456 return ret; 457 } 458 459 static int dwc3_send_clear_stall_ep_cmd(struct dwc3_ep *dep) 460 { 461 struct dwc3 *dwc = dep->dwc; 462 struct dwc3_gadget_ep_cmd_params params; 463 u32 cmd = DWC3_DEPCMD_CLEARSTALL; 464 465 /* 466 * As of core revision 2.60a the recommended programming model 467 * is to set the ClearPendIN bit when issuing a Clear Stall EP 468 * command for IN endpoints. This is to prevent an issue where 469 * some (non-compliant) hosts may not send ACK TPs for pending 470 * IN transfers due to a mishandled error condition. Synopsys 471 * STAR 9000614252. 472 */ 473 if (dep->direction && 474 !DWC3_VER_IS_PRIOR(DWC3, 260A) && 475 (dwc->gadget->speed >= USB_SPEED_SUPER)) 476 cmd |= DWC3_DEPCMD_CLEARPENDIN; 477 478 memset(¶ms, 0, sizeof(params)); 479 480 return dwc3_send_gadget_ep_cmd(dep, cmd, ¶ms); 481 } 482 483 static dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep, 484 struct dwc3_trb *trb) 485 { 486 u32 offset = (char *) trb - (char *) dep->trb_pool; 487 488 return dep->trb_pool_dma + offset; 489 } 490 491 static int dwc3_alloc_trb_pool(struct dwc3_ep *dep) 492 { 493 struct dwc3 *dwc = dep->dwc; 494 495 if (dep->trb_pool) 496 return 0; 497 498 dep->trb_pool = dma_alloc_coherent(dwc->sysdev, 499 sizeof(struct dwc3_trb) * DWC3_TRB_NUM, 500 &dep->trb_pool_dma, GFP_KERNEL); 501 if (!dep->trb_pool) { 502 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n", 503 dep->name); 504 return -ENOMEM; 505 } 506 507 return 0; 508 } 509 510 static void dwc3_free_trb_pool(struct dwc3_ep *dep) 511 { 512 struct dwc3 *dwc = dep->dwc; 513 514 dma_free_coherent(dwc->sysdev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM, 515 dep->trb_pool, dep->trb_pool_dma); 516 517 dep->trb_pool = NULL; 518 dep->trb_pool_dma = 0; 519 } 520 521 static int dwc3_gadget_set_xfer_resource(struct dwc3_ep *dep) 522 { 523 struct dwc3_gadget_ep_cmd_params params; 524 int ret; 525 526 if (dep->flags & DWC3_EP_RESOURCE_ALLOCATED) 527 return 0; 528 529 memset(¶ms, 0x00, sizeof(params)); 530 531 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1); 532 533 ret = dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETTRANSFRESOURCE, 534 ¶ms); 535 if (ret) 536 return ret; 537 538 dep->flags |= DWC3_EP_RESOURCE_ALLOCATED; 539 return 0; 540 } 541 542 /** 543 * dwc3_gadget_start_config - reset endpoint resources 544 * @dwc: pointer to the DWC3 context 545 * @resource_index: DEPSTARTCFG.XferRscIdx value (must be 0 or 2) 546 * 547 * Set resource_index=0 to reset all endpoints' resources allocation. Do this as 548 * part of the power-on/soft-reset initialization. 549 * 550 * Set resource_index=2 to reset only non-control endpoints' resources. Do this 551 * on receiving the SET_CONFIGURATION request or hibernation resume. 552 */ 553 int dwc3_gadget_start_config(struct dwc3 *dwc, unsigned int resource_index) 554 { 555 struct dwc3_gadget_ep_cmd_params params; 556 struct dwc3_ep *dep; 557 u32 cmd; 558 int i; 559 int ret; 560 561 if (resource_index != 0 && resource_index != 2) 562 return -EINVAL; 563 564 memset(¶ms, 0x00, sizeof(params)); 565 cmd = DWC3_DEPCMD_DEPSTARTCFG; 566 cmd |= DWC3_DEPCMD_PARAM(resource_index); 567 568 ret = dwc3_send_gadget_ep_cmd(dwc->eps[0], cmd, ¶ms); 569 if (ret) 570 return ret; 571 572 /* Reset resource allocation flags */ 573 for (i = resource_index; i < dwc->num_eps; i++) { 574 dep = dwc->eps[i]; 575 if (!dep) 576 continue; 577 578 dep->flags &= ~DWC3_EP_RESOURCE_ALLOCATED; 579 } 580 581 return 0; 582 } 583 584 static int dwc3_gadget_set_ep_config(struct dwc3_ep *dep, unsigned int action) 585 { 586 const struct usb_ss_ep_comp_descriptor *comp_desc; 587 const struct usb_endpoint_descriptor *desc; 588 struct dwc3_gadget_ep_cmd_params params; 589 struct dwc3 *dwc = dep->dwc; 590 591 comp_desc = dep->endpoint.comp_desc; 592 desc = dep->endpoint.desc; 593 594 memset(¶ms, 0x00, sizeof(params)); 595 596 params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc)) 597 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc)); 598 599 /* Burst size is only needed in SuperSpeed mode */ 600 if (dwc->gadget->speed >= USB_SPEED_SUPER) { 601 u32 burst = dep->endpoint.maxburst; 602 603 params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst - 1); 604 } 605 606 params.param0 |= action; 607 if (action == DWC3_DEPCFG_ACTION_RESTORE) 608 params.param2 |= dep->saved_state; 609 610 if (usb_endpoint_xfer_control(desc)) 611 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN; 612 613 if (dep->number <= 1 || usb_endpoint_xfer_isoc(desc)) 614 params.param1 |= DWC3_DEPCFG_XFER_NOT_READY_EN; 615 616 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) { 617 params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE 618 | DWC3_DEPCFG_XFER_COMPLETE_EN 619 | DWC3_DEPCFG_STREAM_EVENT_EN; 620 dep->stream_capable = true; 621 } 622 623 if (!usb_endpoint_xfer_control(desc)) 624 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN; 625 626 /* 627 * We are doing 1:1 mapping for endpoints, meaning 628 * Physical Endpoints 2 maps to Logical Endpoint 2 and 629 * so on. We consider the direction bit as part of the physical 630 * endpoint number. So USB endpoint 0x81 is 0x03. 631 */ 632 params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number); 633 634 /* 635 * We must use the lower 16 TX FIFOs even though 636 * HW might have more 637 */ 638 if (dep->direction) 639 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1); 640 641 if (desc->bInterval) { 642 u8 bInterval_m1; 643 644 /* 645 * Valid range for DEPCFG.bInterval_m1 is from 0 to 13. 646 * 647 * NOTE: The programming guide incorrectly stated bInterval_m1 648 * must be set to 0 when operating in fullspeed. Internally the 649 * controller does not have this limitation. See DWC_usb3x 650 * programming guide section 3.2.2.1. 651 */ 652 bInterval_m1 = min_t(u8, desc->bInterval - 1, 13); 653 654 if (usb_endpoint_type(desc) == USB_ENDPOINT_XFER_INT && 655 dwc->gadget->speed == USB_SPEED_FULL) 656 dep->interval = desc->bInterval; 657 else 658 dep->interval = 1 << (desc->bInterval - 1); 659 660 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(bInterval_m1); 661 } 662 663 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETEPCONFIG, ¶ms); 664 } 665 666 /** 667 * dwc3_gadget_calc_tx_fifo_size - calculates the txfifo size value 668 * @dwc: pointer to the DWC3 context 669 * @mult: multiplier to be used when calculating the fifo_size 670 * 671 * Calculates the size value based on the equation below: 672 * 673 * DWC3 revision 280A and prior: 674 * fifo_size = mult * (max_packet / mdwidth) + 1; 675 * 676 * DWC3 revision 290A and onwards: 677 * fifo_size = mult * ((max_packet + mdwidth)/mdwidth + 1) + 1 678 * 679 * The max packet size is set to 1024, as the txfifo requirements mainly apply 680 * to super speed USB use cases. However, it is safe to overestimate the fifo 681 * allocations for other scenarios, i.e. high speed USB. 682 */ 683 static int dwc3_gadget_calc_tx_fifo_size(struct dwc3 *dwc, int mult) 684 { 685 int max_packet = 1024; 686 int fifo_size; 687 int mdwidth; 688 689 mdwidth = dwc3_mdwidth(dwc); 690 691 /* MDWIDTH is represented in bits, we need it in bytes */ 692 mdwidth >>= 3; 693 694 if (DWC3_VER_IS_PRIOR(DWC3, 290A)) 695 fifo_size = mult * (max_packet / mdwidth) + 1; 696 else 697 fifo_size = mult * ((max_packet + mdwidth) / mdwidth) + 1; 698 return fifo_size; 699 } 700 701 /** 702 * dwc3_gadget_calc_ram_depth - calculates the ram depth for txfifo 703 * @dwc: pointer to the DWC3 context 704 */ 705 static int dwc3_gadget_calc_ram_depth(struct dwc3 *dwc) 706 { 707 int ram_depth; 708 int fifo_0_start; 709 bool is_single_port_ram; 710 711 /* Check supporting RAM type by HW */ 712 is_single_port_ram = DWC3_SPRAM_TYPE(dwc->hwparams.hwparams1); 713 714 /* 715 * If a single port RAM is utilized, then allocate TxFIFOs from 716 * RAM0. otherwise, allocate them from RAM1. 717 */ 718 ram_depth = is_single_port_ram ? DWC3_RAM0_DEPTH(dwc->hwparams.hwparams6) : 719 DWC3_RAM1_DEPTH(dwc->hwparams.hwparams7); 720 721 /* 722 * In a single port RAM configuration, the available RAM is shared 723 * between the RX and TX FIFOs. This means that the txfifo can begin 724 * at a non-zero address. 725 */ 726 if (is_single_port_ram) { 727 u32 reg; 728 729 /* Check if TXFIFOs start at non-zero addr */ 730 reg = dwc3_readl(dwc, DWC3_GTXFIFOSIZ(0)); 731 fifo_0_start = DWC3_GTXFIFOSIZ_TXFSTADDR(reg); 732 733 ram_depth -= (fifo_0_start >> 16); 734 } 735 736 return ram_depth; 737 } 738 739 /** 740 * dwc3_gadget_clear_tx_fifos - Clears txfifo allocation 741 * @dwc: pointer to the DWC3 context 742 * 743 * Iterates through all the endpoint registers and clears the previous txfifo 744 * allocations. 745 */ 746 void dwc3_gadget_clear_tx_fifos(struct dwc3 *dwc) 747 { 748 struct dwc3_ep *dep; 749 int fifo_depth; 750 int size; 751 int num; 752 753 if (!dwc->do_fifo_resize) 754 return; 755 756 /* Read ep0IN related TXFIFO size */ 757 dep = dwc->eps[1]; 758 size = dwc3_readl(dwc, DWC3_GTXFIFOSIZ(0)); 759 if (DWC3_IP_IS(DWC3)) 760 fifo_depth = DWC3_GTXFIFOSIZ_TXFDEP(size); 761 else 762 fifo_depth = DWC31_GTXFIFOSIZ_TXFDEP(size); 763 764 dwc->last_fifo_depth = fifo_depth; 765 /* Clear existing TXFIFO for all IN eps except ep0 */ 766 for (num = 3; num < min_t(int, dwc->num_eps, DWC3_ENDPOINTS_NUM); num += 2) { 767 dep = dwc->eps[num]; 768 if (!dep) 769 continue; 770 771 /* Don't change TXFRAMNUM on usb31 version */ 772 size = DWC3_IP_IS(DWC3) ? 0 : 773 dwc3_readl(dwc, DWC3_GTXFIFOSIZ(num >> 1)) & 774 DWC31_GTXFIFOSIZ_TXFRAMNUM; 775 776 dwc3_writel(dwc, DWC3_GTXFIFOSIZ(num >> 1), size); 777 dep->flags &= ~DWC3_EP_TXFIFO_RESIZED; 778 } 779 dwc->num_ep_resized = 0; 780 } 781 782 /* 783 * dwc3_gadget_resize_tx_fifos - reallocate fifo spaces for current use-case 784 * @dwc: pointer to our context structure 785 * 786 * This function will a best effort FIFO allocation in order 787 * to improve FIFO usage and throughput, while still allowing 788 * us to enable as many endpoints as possible. 789 * 790 * Keep in mind that this operation will be highly dependent 791 * on the configured size for RAM1 - which contains TxFifo -, 792 * the amount of endpoints enabled on coreConsultant tool, and 793 * the width of the Master Bus. 794 * 795 * In general, FIFO depths are represented with the following equation: 796 * 797 * fifo_size = mult * ((max_packet + mdwidth)/mdwidth + 1) + 1 798 * 799 * In conjunction with dwc3_gadget_check_config(), this resizing logic will 800 * ensure that all endpoints will have enough internal memory for one max 801 * packet per endpoint. 802 */ 803 static int dwc3_gadget_resize_tx_fifos(struct dwc3_ep *dep) 804 { 805 struct dwc3 *dwc = dep->dwc; 806 int fifo_0_start; 807 int ram_depth; 808 int fifo_size; 809 int min_depth; 810 int num_in_ep; 811 int remaining; 812 int num_fifos = 1; 813 int fifo; 814 int tmp; 815 816 if (!dwc->do_fifo_resize) 817 return 0; 818 819 /* resize IN endpoints except ep0 */ 820 if (!usb_endpoint_dir_in(dep->endpoint.desc) || dep->number <= 1) 821 return 0; 822 823 /* bail if already resized */ 824 if (dep->flags & DWC3_EP_TXFIFO_RESIZED) 825 return 0; 826 827 ram_depth = dwc3_gadget_calc_ram_depth(dwc); 828 829 switch (dwc->gadget->speed) { 830 case USB_SPEED_SUPER_PLUS: 831 case USB_SPEED_SUPER: 832 if (usb_endpoint_xfer_bulk(dep->endpoint.desc) || 833 usb_endpoint_xfer_isoc(dep->endpoint.desc)) 834 num_fifos = min_t(unsigned int, 835 dep->endpoint.maxburst, 836 dwc->tx_fifo_resize_max_num); 837 break; 838 case USB_SPEED_HIGH: 839 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) { 840 num_fifos = min_t(unsigned int, 841 usb_endpoint_maxp_mult(dep->endpoint.desc) + 1, 842 dwc->tx_fifo_resize_max_num); 843 break; 844 } 845 fallthrough; 846 case USB_SPEED_FULL: 847 if (usb_endpoint_xfer_bulk(dep->endpoint.desc)) 848 num_fifos = 2; 849 break; 850 default: 851 break; 852 } 853 854 /* FIFO size for a single buffer */ 855 fifo = dwc3_gadget_calc_tx_fifo_size(dwc, 1); 856 857 /* Calculate the number of remaining EPs w/o any FIFO */ 858 num_in_ep = dwc->max_cfg_eps; 859 num_in_ep -= dwc->num_ep_resized; 860 861 /* Reserve at least one FIFO for the number of IN EPs */ 862 min_depth = num_in_ep * (fifo + 1); 863 remaining = ram_depth - min_depth - dwc->last_fifo_depth; 864 remaining = max_t(int, 0, remaining); 865 /* 866 * We've already reserved 1 FIFO per EP, so check what we can fit in 867 * addition to it. If there is not enough remaining space, allocate 868 * all the remaining space to the EP. 869 */ 870 fifo_size = (num_fifos - 1) * fifo; 871 if (remaining < fifo_size) 872 fifo_size = remaining; 873 874 fifo_size += fifo; 875 /* Last increment according to the TX FIFO size equation */ 876 fifo_size++; 877 878 /* Check if TXFIFOs start at non-zero addr */ 879 tmp = dwc3_readl(dwc, DWC3_GTXFIFOSIZ(0)); 880 fifo_0_start = DWC3_GTXFIFOSIZ_TXFSTADDR(tmp); 881 882 fifo_size |= (fifo_0_start + (dwc->last_fifo_depth << 16)); 883 if (DWC3_IP_IS(DWC3)) 884 dwc->last_fifo_depth += DWC3_GTXFIFOSIZ_TXFDEP(fifo_size); 885 else 886 dwc->last_fifo_depth += DWC31_GTXFIFOSIZ_TXFDEP(fifo_size); 887 888 /* Check fifo size allocation doesn't exceed available RAM size. */ 889 if (dwc->last_fifo_depth >= ram_depth) { 890 dev_err(dwc->dev, "Fifosize(%d) > RAM size(%d) %s depth:%d\n", 891 dwc->last_fifo_depth, ram_depth, 892 dep->endpoint.name, fifo_size); 893 if (DWC3_IP_IS(DWC3)) 894 fifo_size = DWC3_GTXFIFOSIZ_TXFDEP(fifo_size); 895 else 896 fifo_size = DWC31_GTXFIFOSIZ_TXFDEP(fifo_size); 897 898 dwc->last_fifo_depth -= fifo_size; 899 return -ENOMEM; 900 } 901 902 dwc3_writel(dwc, DWC3_GTXFIFOSIZ(dep->number >> 1), fifo_size); 903 dep->flags |= DWC3_EP_TXFIFO_RESIZED; 904 dwc->num_ep_resized++; 905 906 return 0; 907 } 908 909 /** 910 * __dwc3_gadget_ep_enable - initializes a hw endpoint 911 * @dep: endpoint to be initialized 912 * @action: one of INIT, MODIFY or RESTORE 913 * 914 * Caller should take care of locking. Execute all necessary commands to 915 * initialize a HW endpoint so it can be used by a gadget driver. 916 */ 917 static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep, unsigned int action) 918 { 919 const struct usb_endpoint_descriptor *desc = dep->endpoint.desc; 920 struct dwc3 *dwc = dep->dwc; 921 922 u32 reg; 923 int ret; 924 925 if (!(dep->flags & DWC3_EP_ENABLED)) { 926 ret = dwc3_gadget_resize_tx_fifos(dep); 927 if (ret) 928 return ret; 929 } 930 931 ret = dwc3_gadget_set_ep_config(dep, action); 932 if (ret) 933 return ret; 934 935 ret = dwc3_gadget_set_xfer_resource(dep); 936 if (ret) 937 return ret; 938 939 if (!(dep->flags & DWC3_EP_ENABLED)) { 940 struct dwc3_trb *trb_st_hw; 941 struct dwc3_trb *trb_link; 942 943 dep->type = usb_endpoint_type(desc); 944 dep->flags |= DWC3_EP_ENABLED; 945 946 reg = dwc3_readl(dwc, DWC3_DALEPENA); 947 reg |= DWC3_DALEPENA_EP(dep->number); 948 dwc3_writel(dwc, DWC3_DALEPENA, reg); 949 950 dep->trb_dequeue = 0; 951 dep->trb_enqueue = 0; 952 953 if (usb_endpoint_xfer_control(desc)) 954 goto out; 955 956 /* Initialize the TRB ring */ 957 memset(dep->trb_pool, 0, 958 sizeof(struct dwc3_trb) * DWC3_TRB_NUM); 959 960 /* Link TRB. The HWO bit is never reset */ 961 trb_st_hw = &dep->trb_pool[0]; 962 963 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1]; 964 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw)); 965 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw)); 966 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB; 967 trb_link->ctrl |= DWC3_TRB_CTRL_HWO; 968 } 969 970 /* 971 * Issue StartTransfer here with no-op TRB so we can always rely on No 972 * Response Update Transfer command. 973 */ 974 if (usb_endpoint_xfer_bulk(desc) || 975 usb_endpoint_xfer_int(desc)) { 976 struct dwc3_gadget_ep_cmd_params params; 977 struct dwc3_trb *trb; 978 dma_addr_t trb_dma; 979 u32 cmd; 980 981 memset(¶ms, 0, sizeof(params)); 982 trb = &dep->trb_pool[0]; 983 trb_dma = dwc3_trb_dma_offset(dep, trb); 984 985 params.param0 = upper_32_bits(trb_dma); 986 params.param1 = lower_32_bits(trb_dma); 987 988 cmd = DWC3_DEPCMD_STARTTRANSFER; 989 990 ret = dwc3_send_gadget_ep_cmd(dep, cmd, ¶ms); 991 if (ret < 0) 992 return ret; 993 994 if (dep->stream_capable) { 995 /* 996 * For streams, at start, there maybe a race where the 997 * host primes the endpoint before the function driver 998 * queues a request to initiate a stream. In that case, 999 * the controller will not see the prime to generate the 1000 * ERDY and start stream. To workaround this, issue a 1001 * no-op TRB as normal, but end it immediately. As a 1002 * result, when the function driver queues the request, 1003 * the next START_TRANSFER command will cause the 1004 * controller to generate an ERDY to initiate the 1005 * stream. 1006 */ 1007 dwc3_stop_active_transfer(dep, false, true); 1008 1009 /* 1010 * All stream eps will reinitiate stream on NoStream 1011 * rejection. 1012 * 1013 * However, if the controller is capable of 1014 * TXF_FLUSH_BYPASS, then IN direction endpoints will 1015 * automatically restart the stream without the driver 1016 * initiation. 1017 */ 1018 if (!dep->direction || 1019 !(dwc->hwparams.hwparams9 & 1020 DWC3_GHWPARAMS9_DEV_TXF_FLUSH_BYPASS)) 1021 dep->flags |= DWC3_EP_FORCE_RESTART_STREAM; 1022 } 1023 } 1024 1025 out: 1026 trace_dwc3_gadget_ep_enable(dep); 1027 1028 return 0; 1029 } 1030 1031 void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep, int status) 1032 { 1033 struct dwc3_request *req; 1034 1035 dwc3_stop_active_transfer(dep, false, false); 1036 1037 /* If endxfer is delayed, avoid unmapping requests */ 1038 if (dep->flags & DWC3_EP_DELAY_STOP) 1039 return; 1040 1041 /* - giveback all requests to gadget driver */ 1042 while (!list_empty(&dep->started_list)) { 1043 req = next_request(&dep->started_list); 1044 1045 dwc3_gadget_giveback(dep, req, status); 1046 } 1047 1048 while (!list_empty(&dep->pending_list)) { 1049 req = next_request(&dep->pending_list); 1050 1051 dwc3_gadget_giveback(dep, req, status); 1052 } 1053 1054 while (!list_empty(&dep->cancelled_list)) { 1055 req = next_request(&dep->cancelled_list); 1056 1057 dwc3_gadget_giveback(dep, req, status); 1058 } 1059 } 1060 1061 /** 1062 * __dwc3_gadget_ep_disable - disables a hw endpoint 1063 * @dep: the endpoint to disable 1064 * 1065 * This function undoes what __dwc3_gadget_ep_enable did and also removes 1066 * requests which are currently being processed by the hardware and those which 1067 * are not yet scheduled. 1068 * 1069 * Caller should take care of locking. 1070 */ 1071 static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep) 1072 { 1073 struct dwc3 *dwc = dep->dwc; 1074 u32 reg; 1075 u32 mask; 1076 1077 trace_dwc3_gadget_ep_disable(dep); 1078 1079 /* make sure HW endpoint isn't stalled */ 1080 if (dep->flags & DWC3_EP_STALL) 1081 __dwc3_gadget_ep_set_halt(dep, 0, false); 1082 1083 reg = dwc3_readl(dwc, DWC3_DALEPENA); 1084 reg &= ~DWC3_DALEPENA_EP(dep->number); 1085 dwc3_writel(dwc, DWC3_DALEPENA, reg); 1086 1087 dwc3_remove_requests(dwc, dep, -ESHUTDOWN); 1088 1089 dep->stream_capable = false; 1090 dep->type = 0; 1091 mask = DWC3_EP_TXFIFO_RESIZED | DWC3_EP_RESOURCE_ALLOCATED; 1092 /* 1093 * dwc3_remove_requests() can exit early if DWC3 EP delayed stop is 1094 * set. Do not clear DEP flags, so that the end transfer command will 1095 * be reattempted during the next SETUP stage. 1096 */ 1097 if (dep->flags & DWC3_EP_DELAY_STOP) 1098 mask |= (DWC3_EP_DELAY_STOP | DWC3_EP_TRANSFER_STARTED); 1099 dep->flags &= mask; 1100 1101 /* Clear out the ep descriptors for non-ep0 */ 1102 if (dep->number > 1) { 1103 dep->endpoint.comp_desc = NULL; 1104 dep->endpoint.desc = NULL; 1105 } 1106 1107 return 0; 1108 } 1109 1110 /* -------------------------------------------------------------------------- */ 1111 1112 static int dwc3_gadget_ep0_enable(struct usb_ep *ep, 1113 const struct usb_endpoint_descriptor *desc) 1114 { 1115 return -EINVAL; 1116 } 1117 1118 static int dwc3_gadget_ep0_disable(struct usb_ep *ep) 1119 { 1120 return -EINVAL; 1121 } 1122 1123 /* -------------------------------------------------------------------------- */ 1124 1125 static int dwc3_gadget_ep_enable(struct usb_ep *ep, 1126 const struct usb_endpoint_descriptor *desc) 1127 { 1128 struct dwc3_ep *dep; 1129 struct dwc3 *dwc; 1130 unsigned long flags; 1131 int ret; 1132 1133 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) { 1134 pr_debug("dwc3: invalid parameters\n"); 1135 return -EINVAL; 1136 } 1137 1138 if (!desc->wMaxPacketSize) { 1139 pr_debug("dwc3: missing wMaxPacketSize\n"); 1140 return -EINVAL; 1141 } 1142 1143 dep = to_dwc3_ep(ep); 1144 dwc = dep->dwc; 1145 1146 if (dev_WARN_ONCE(dwc->dev, dep->flags & DWC3_EP_ENABLED, 1147 "%s is already enabled\n", 1148 dep->name)) 1149 return 0; 1150 1151 spin_lock_irqsave(&dwc->lock, flags); 1152 if (dwc->err_state != DWC3_ERR_NONE) { 1153 spin_unlock_irqrestore(&dwc->lock, flags); 1154 return -ESHUTDOWN; 1155 } 1156 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT); 1157 spin_unlock_irqrestore(&dwc->lock, flags); 1158 1159 return ret; 1160 } 1161 1162 static int dwc3_gadget_ep_disable(struct usb_ep *ep) 1163 { 1164 struct dwc3_ep *dep; 1165 struct dwc3 *dwc; 1166 unsigned long flags; 1167 int ret; 1168 1169 if (!ep) { 1170 pr_debug("dwc3: invalid parameters\n"); 1171 return -EINVAL; 1172 } 1173 1174 dep = to_dwc3_ep(ep); 1175 dwc = dep->dwc; 1176 1177 if (dev_WARN_ONCE(dwc->dev, !(dep->flags & DWC3_EP_ENABLED), 1178 "%s is already disabled\n", 1179 dep->name)) 1180 return 0; 1181 1182 spin_lock_irqsave(&dwc->lock, flags); 1183 ret = __dwc3_gadget_ep_disable(dep); 1184 spin_unlock_irqrestore(&dwc->lock, flags); 1185 1186 return ret; 1187 } 1188 1189 static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep, 1190 gfp_t gfp_flags) 1191 { 1192 struct dwc3_request *req; 1193 struct dwc3_ep *dep = to_dwc3_ep(ep); 1194 1195 req = kzalloc_obj(*req, gfp_flags); 1196 if (!req) 1197 return NULL; 1198 1199 req->direction = dep->direction; 1200 req->epnum = dep->number; 1201 req->dep = dep; 1202 req->status = DWC3_REQUEST_STATUS_UNKNOWN; 1203 1204 trace_dwc3_alloc_request(req); 1205 1206 return &req->request; 1207 } 1208 1209 static void dwc3_gadget_ep_free_request(struct usb_ep *ep, 1210 struct usb_request *request) 1211 { 1212 struct dwc3_request *req = to_dwc3_request(request); 1213 1214 trace_dwc3_free_request(req); 1215 kfree(req); 1216 } 1217 1218 /** 1219 * dwc3_ep_prev_trb - returns the previous TRB in the ring 1220 * @dep: The endpoint with the TRB ring 1221 * @index: The index of the current TRB in the ring 1222 * 1223 * Returns the TRB prior to the one pointed to by the index. If the 1224 * index is 0, we will wrap backwards, skip the link TRB, and return 1225 * the one just before that. 1226 */ 1227 static struct dwc3_trb *dwc3_ep_prev_trb(struct dwc3_ep *dep, u8 index) 1228 { 1229 u8 tmp = index; 1230 1231 if (!tmp) 1232 tmp = DWC3_TRB_NUM - 1; 1233 1234 return &dep->trb_pool[tmp - 1]; 1235 } 1236 1237 static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep) 1238 { 1239 u8 trbs_left; 1240 1241 /* 1242 * If the enqueue & dequeue are equal then the TRB ring is either full 1243 * or empty. It's considered full when there are DWC3_TRB_NUM-1 of TRBs 1244 * pending to be processed by the driver. 1245 */ 1246 if (dep->trb_enqueue == dep->trb_dequeue) { 1247 struct dwc3_request *req; 1248 1249 /* 1250 * If there is any request remained in the started_list with 1251 * active TRBs at this point, then there is no TRB available. 1252 */ 1253 req = next_request(&dep->started_list); 1254 if (req && req->num_trbs) 1255 return 0; 1256 1257 return DWC3_TRB_NUM - 1; 1258 } 1259 1260 trbs_left = dep->trb_dequeue - dep->trb_enqueue; 1261 trbs_left &= (DWC3_TRB_NUM - 1); 1262 1263 if (dep->trb_dequeue < dep->trb_enqueue) 1264 trbs_left--; 1265 1266 return trbs_left; 1267 } 1268 1269 /** 1270 * dwc3_prepare_one_trb - setup one TRB from one request 1271 * @dep: endpoint for which this request is prepared 1272 * @req: dwc3_request pointer 1273 * @trb_length: buffer size of the TRB 1274 * @chain: should this TRB be chained to the next? 1275 * @node: only for isochronous endpoints. First TRB needs different type. 1276 * @use_bounce_buffer: set to use bounce buffer 1277 * @must_interrupt: set to interrupt on TRB completion 1278 */ 1279 static void dwc3_prepare_one_trb(struct dwc3_ep *dep, 1280 struct dwc3_request *req, unsigned int trb_length, 1281 unsigned int chain, unsigned int node, bool use_bounce_buffer, 1282 bool must_interrupt) 1283 { 1284 struct dwc3_trb *trb; 1285 dma_addr_t dma; 1286 unsigned int stream_id = req->request.stream_id; 1287 unsigned int short_not_ok = req->request.short_not_ok; 1288 unsigned int no_interrupt = req->request.no_interrupt; 1289 unsigned int is_last = req->request.is_last; 1290 struct dwc3 *dwc = dep->dwc; 1291 struct usb_gadget *gadget = dwc->gadget; 1292 enum usb_device_speed speed = gadget->speed; 1293 1294 if (use_bounce_buffer) 1295 dma = dep->dwc->bounce_addr; 1296 else if (req->request.num_sgs > 0) 1297 dma = sg_dma_address(req->start_sg); 1298 else 1299 dma = req->request.dma; 1300 1301 trb = &dep->trb_pool[dep->trb_enqueue]; 1302 1303 if (!req->trb) { 1304 dwc3_gadget_move_started_request(req); 1305 req->trb = trb; 1306 req->trb_dma = dwc3_trb_dma_offset(dep, trb); 1307 } 1308 1309 req->num_trbs++; 1310 1311 trb->size = DWC3_TRB_SIZE_LENGTH(trb_length); 1312 trb->bpl = lower_32_bits(dma); 1313 trb->bph = upper_32_bits(dma); 1314 1315 switch (usb_endpoint_type(dep->endpoint.desc)) { 1316 case USB_ENDPOINT_XFER_CONTROL: 1317 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP; 1318 break; 1319 1320 case USB_ENDPOINT_XFER_ISOC: 1321 if (!node) { 1322 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST; 1323 1324 /* 1325 * USB Specification 2.0 Section 5.9.2 states that: "If 1326 * there is only a single transaction in the microframe, 1327 * only a DATA0 data packet PID is used. If there are 1328 * two transactions per microframe, DATA1 is used for 1329 * the first transaction data packet and DATA0 is used 1330 * for the second transaction data packet. If there are 1331 * three transactions per microframe, DATA2 is used for 1332 * the first transaction data packet, DATA1 is used for 1333 * the second, and DATA0 is used for the third." 1334 * 1335 * IOW, we should satisfy the following cases: 1336 * 1337 * 1) length <= maxpacket 1338 * - DATA0 1339 * 1340 * 2) maxpacket < length <= (2 * maxpacket) 1341 * - DATA1, DATA0 1342 * 1343 * 3) (2 * maxpacket) < length <= (3 * maxpacket) 1344 * - DATA2, DATA1, DATA0 1345 */ 1346 if (speed == USB_SPEED_HIGH) { 1347 struct usb_ep *ep = &dep->endpoint; 1348 unsigned int mult = 2; 1349 unsigned int maxp = usb_endpoint_maxp(ep->desc); 1350 1351 if (req->request.length <= (2 * maxp)) 1352 mult--; 1353 1354 if (req->request.length <= maxp) 1355 mult--; 1356 1357 trb->size |= DWC3_TRB_SIZE_PCM1(mult); 1358 } 1359 } else { 1360 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS; 1361 } 1362 1363 if (!no_interrupt && !chain) 1364 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI; 1365 break; 1366 1367 case USB_ENDPOINT_XFER_BULK: 1368 case USB_ENDPOINT_XFER_INT: 1369 trb->ctrl = DWC3_TRBCTL_NORMAL; 1370 break; 1371 default: 1372 /* 1373 * This is only possible with faulty memory because we 1374 * checked it already :) 1375 */ 1376 dev_WARN(dwc->dev, "Unknown endpoint type %d\n", 1377 usb_endpoint_type(dep->endpoint.desc)); 1378 } 1379 1380 /* 1381 * Enable Continue on Short Packet 1382 * when endpoint is not a stream capable 1383 */ 1384 if (usb_endpoint_dir_out(dep->endpoint.desc)) { 1385 if (!dep->stream_capable) 1386 trb->ctrl |= DWC3_TRB_CTRL_CSP; 1387 1388 if (short_not_ok) 1389 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI; 1390 } 1391 1392 /* All TRBs setup for MST must set CSP=1 when LST=0 */ 1393 if (dep->stream_capable && DWC3_MST_CAPABLE(&dwc->hwparams)) 1394 trb->ctrl |= DWC3_TRB_CTRL_CSP; 1395 1396 if ((!no_interrupt && !chain) || must_interrupt) 1397 trb->ctrl |= DWC3_TRB_CTRL_IOC; 1398 1399 if (chain) 1400 trb->ctrl |= DWC3_TRB_CTRL_CHN; 1401 else if (dep->stream_capable && is_last && 1402 !DWC3_MST_CAPABLE(&dwc->hwparams)) 1403 trb->ctrl |= DWC3_TRB_CTRL_LST; 1404 1405 if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable) 1406 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(stream_id); 1407 1408 /* 1409 * As per data book 4.2.3.2TRB Control Bit Rules section 1410 * 1411 * The controller autonomously checks the HWO field of a TRB to determine if the 1412 * entire TRB is valid. Therefore, software must ensure that the rest of the TRB 1413 * is valid before setting the HWO field to '1'. In most systems, this means that 1414 * software must update the fourth DWORD of a TRB last. 1415 * 1416 * However there is a possibility of CPU re-ordering here which can cause 1417 * controller to observe the HWO bit set prematurely. 1418 * Add a write memory barrier to prevent CPU re-ordering. 1419 */ 1420 wmb(); 1421 trb->ctrl |= DWC3_TRB_CTRL_HWO; 1422 1423 dwc3_ep_inc_enq(dep); 1424 1425 trace_dwc3_prepare_trb(dep, trb); 1426 } 1427 1428 static bool dwc3_needs_extra_trb(struct dwc3_ep *dep, struct dwc3_request *req) 1429 { 1430 unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc); 1431 unsigned int rem = req->request.length % maxp; 1432 1433 if ((req->request.length && req->request.zero && !rem && 1434 !usb_endpoint_xfer_isoc(dep->endpoint.desc)) || 1435 (!req->direction && rem)) 1436 return true; 1437 1438 return false; 1439 } 1440 1441 /** 1442 * dwc3_prepare_last_sg - prepare TRBs for the last SG entry 1443 * @dep: The endpoint that the request belongs to 1444 * @req: The request to prepare 1445 * @entry_length: The last SG entry size 1446 * @node: Indicates whether this is not the first entry (for isoc only) 1447 * 1448 * Return the number of TRBs prepared. 1449 */ 1450 static int dwc3_prepare_last_sg(struct dwc3_ep *dep, 1451 struct dwc3_request *req, unsigned int entry_length, 1452 unsigned int node) 1453 { 1454 unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc); 1455 unsigned int rem = req->request.length % maxp; 1456 unsigned int num_trbs = 1; 1457 bool needs_extra_trb; 1458 1459 if (dwc3_needs_extra_trb(dep, req)) 1460 num_trbs++; 1461 1462 if (dwc3_calc_trbs_left(dep) < num_trbs) 1463 return 0; 1464 1465 needs_extra_trb = num_trbs > 1; 1466 1467 /* Prepare a normal TRB */ 1468 if (req->direction || req->request.length) 1469 dwc3_prepare_one_trb(dep, req, entry_length, 1470 needs_extra_trb, node, false, false); 1471 1472 /* Prepare extra TRBs for ZLP and MPS OUT transfer alignment */ 1473 if ((!req->direction && !req->request.length) || needs_extra_trb) 1474 dwc3_prepare_one_trb(dep, req, 1475 req->direction ? 0 : maxp - rem, 1476 false, 1, true, false); 1477 1478 return num_trbs; 1479 } 1480 1481 static int dwc3_prepare_trbs_sg(struct dwc3_ep *dep, 1482 struct dwc3_request *req) 1483 { 1484 struct scatterlist *sg = req->start_sg; 1485 struct scatterlist *s; 1486 int i; 1487 unsigned int length = req->request.length; 1488 unsigned int remaining = req->num_pending_sgs; 1489 unsigned int num_queued_sgs = req->request.num_mapped_sgs - remaining; 1490 unsigned int num_trbs = req->num_trbs; 1491 bool needs_extra_trb = dwc3_needs_extra_trb(dep, req); 1492 1493 /* 1494 * If we resume preparing the request, then get the remaining length of 1495 * the request and resume where we left off. 1496 */ 1497 for_each_sg(req->request.sg, s, num_queued_sgs, i) 1498 length -= sg_dma_len(s); 1499 1500 for_each_sg(sg, s, remaining, i) { 1501 unsigned int num_trbs_left = dwc3_calc_trbs_left(dep); 1502 unsigned int trb_length; 1503 bool must_interrupt = false; 1504 bool last_sg = false; 1505 1506 trb_length = min_t(unsigned int, length, sg_dma_len(s)); 1507 1508 length -= trb_length; 1509 1510 /* 1511 * IOMMU driver is coalescing the list of sgs which shares a 1512 * page boundary into one and giving it to USB driver. With 1513 * this the number of sgs mapped is not equal to the number of 1514 * sgs passed. So mark the chain bit to false if it isthe last 1515 * mapped sg. 1516 */ 1517 if ((i == remaining - 1) || !length) 1518 last_sg = true; 1519 1520 if (!num_trbs_left) 1521 break; 1522 1523 if (last_sg) { 1524 if (!dwc3_prepare_last_sg(dep, req, trb_length, i)) 1525 break; 1526 } else { 1527 /* 1528 * Look ahead to check if we have enough TRBs for the 1529 * next SG entry. If not, set interrupt on this TRB to 1530 * resume preparing the next SG entry when more TRBs are 1531 * free. 1532 */ 1533 if (num_trbs_left == 1 || (needs_extra_trb && 1534 num_trbs_left <= 2 && 1535 sg_dma_len(sg_next(s)) >= length)) { 1536 struct dwc3_request *r; 1537 1538 /* Check if previous requests already set IOC */ 1539 list_for_each_entry(r, &dep->started_list, list) { 1540 if (r != req && !r->request.no_interrupt) 1541 break; 1542 1543 if (r == req) 1544 must_interrupt = true; 1545 } 1546 } 1547 1548 dwc3_prepare_one_trb(dep, req, trb_length, 1, i, false, 1549 must_interrupt); 1550 } 1551 1552 /* 1553 * There can be a situation where all sgs in sglist are not 1554 * queued because of insufficient trb number. To handle this 1555 * case, update start_sg to next sg to be queued, so that 1556 * we have free trbs we can continue queuing from where we 1557 * previously stopped 1558 */ 1559 if (!last_sg) 1560 req->start_sg = sg_next(s); 1561 1562 req->num_pending_sgs--; 1563 1564 /* 1565 * The number of pending SG entries may not correspond to the 1566 * number of mapped SG entries. If all the data are queued, then 1567 * don't include unused SG entries. 1568 */ 1569 if (length == 0) { 1570 req->num_pending_sgs = 0; 1571 break; 1572 } 1573 1574 if (must_interrupt) 1575 break; 1576 } 1577 1578 return req->num_trbs - num_trbs; 1579 } 1580 1581 static int dwc3_prepare_trbs_linear(struct dwc3_ep *dep, 1582 struct dwc3_request *req) 1583 { 1584 return dwc3_prepare_last_sg(dep, req, req->request.length, 0); 1585 } 1586 1587 /* 1588 * dwc3_prepare_trbs - setup TRBs from requests 1589 * @dep: endpoint for which requests are being prepared 1590 * 1591 * The function goes through the requests list and sets up TRBs for the 1592 * transfers. The function returns once there are no more TRBs available or 1593 * it runs out of requests. 1594 * 1595 * Returns the number of TRBs prepared or negative errno. 1596 */ 1597 static int dwc3_prepare_trbs(struct dwc3_ep *dep) 1598 { 1599 struct dwc3_request *req, *n; 1600 int ret = 0; 1601 1602 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM); 1603 1604 /* 1605 * We can get in a situation where there's a request in the started list 1606 * but there weren't enough TRBs to fully kick it in the first time 1607 * around, so it has been waiting for more TRBs to be freed up. 1608 * 1609 * In that case, we should check if we have a request with pending_sgs 1610 * in the started list and prepare TRBs for that request first, 1611 * otherwise we will prepare TRBs completely out of order and that will 1612 * break things. 1613 */ 1614 list_for_each_entry(req, &dep->started_list, list) { 1615 if (req->num_pending_sgs > 0) { 1616 ret = dwc3_prepare_trbs_sg(dep, req); 1617 if (!ret || req->num_pending_sgs) 1618 return ret; 1619 } 1620 1621 if (!dwc3_calc_trbs_left(dep)) 1622 return ret; 1623 1624 /* 1625 * Don't prepare beyond a transfer. In DWC_usb32, its transfer 1626 * burst capability may try to read and use TRBs beyond the 1627 * active transfer instead of stopping. 1628 */ 1629 if (dep->stream_capable && req->request.is_last && 1630 !DWC3_MST_CAPABLE(&dep->dwc->hwparams)) 1631 return ret; 1632 } 1633 1634 list_for_each_entry_safe(req, n, &dep->pending_list, list) { 1635 struct dwc3 *dwc = dep->dwc; 1636 1637 ret = usb_gadget_map_request_by_dev(dwc->sysdev, &req->request, 1638 dep->direction); 1639 if (ret) 1640 return ret; 1641 1642 req->start_sg = req->request.sg; 1643 req->num_pending_sgs = req->request.num_mapped_sgs; 1644 1645 if (req->num_pending_sgs > 0) { 1646 ret = dwc3_prepare_trbs_sg(dep, req); 1647 if (req->num_pending_sgs) 1648 return ret; 1649 } else { 1650 ret = dwc3_prepare_trbs_linear(dep, req); 1651 } 1652 1653 if (!ret || !dwc3_calc_trbs_left(dep)) 1654 return ret; 1655 1656 /* 1657 * Don't prepare beyond a transfer. In DWC_usb32, its transfer 1658 * burst capability may try to read and use TRBs beyond the 1659 * active transfer instead of stopping. 1660 */ 1661 if (dep->stream_capable && req->request.is_last && 1662 !DWC3_MST_CAPABLE(&dwc->hwparams)) 1663 return ret; 1664 } 1665 1666 return ret; 1667 } 1668 1669 static void dwc3_gadget_ep_cleanup_cancelled_requests(struct dwc3_ep *dep); 1670 1671 static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep) 1672 { 1673 struct dwc3_gadget_ep_cmd_params params; 1674 struct dwc3_request *req; 1675 int starting; 1676 int ret; 1677 u32 cmd; 1678 1679 /* 1680 * Note that it's normal to have no new TRBs prepared (i.e. ret == 0). 1681 * This happens when we need to stop and restart a transfer such as in 1682 * the case of reinitiating a stream or retrying an isoc transfer. 1683 */ 1684 ret = dwc3_prepare_trbs(dep); 1685 if (ret < 0) 1686 return ret; 1687 1688 starting = !(dep->flags & DWC3_EP_TRANSFER_STARTED); 1689 1690 /* 1691 * If there's no new TRB prepared and we don't need to restart a 1692 * transfer, there's no need to update the transfer. 1693 */ 1694 if (!ret && !starting) 1695 return 0; 1696 1697 req = next_request(&dep->started_list); 1698 if (!req) { 1699 dep->flags |= DWC3_EP_PENDING_REQUEST; 1700 return 0; 1701 } 1702 1703 memset(¶ms, 0, sizeof(params)); 1704 1705 if (starting) { 1706 params.param0 = upper_32_bits(req->trb_dma); 1707 params.param1 = lower_32_bits(req->trb_dma); 1708 cmd = DWC3_DEPCMD_STARTTRANSFER; 1709 1710 if (dep->stream_capable) 1711 cmd |= DWC3_DEPCMD_PARAM(req->request.stream_id); 1712 1713 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) 1714 cmd |= DWC3_DEPCMD_PARAM(dep->frame_number); 1715 } else { 1716 cmd = DWC3_DEPCMD_UPDATETRANSFER | 1717 DWC3_DEPCMD_PARAM(dep->resource_index); 1718 } 1719 1720 ret = dwc3_send_gadget_ep_cmd(dep, cmd, ¶ms); 1721 if (ret < 0) { 1722 struct dwc3_request *tmp; 1723 1724 if (ret == -EAGAIN) 1725 return ret; 1726 1727 dwc3_stop_active_transfer(dep, false, true); 1728 1729 list_for_each_entry_safe(req, tmp, &dep->started_list, list) 1730 dwc3_gadget_move_cancelled_request(req, DWC3_REQUEST_STATUS_DEQUEUED); 1731 1732 /* If ep isn't started, then there's no end transfer pending */ 1733 if (!(dep->flags & DWC3_EP_END_TRANSFER_PENDING)) 1734 dwc3_gadget_ep_cleanup_cancelled_requests(dep); 1735 1736 return ret; 1737 } 1738 1739 if (dep->stream_capable && req->request.is_last && 1740 !DWC3_MST_CAPABLE(&dep->dwc->hwparams)) 1741 dep->flags |= DWC3_EP_WAIT_TRANSFER_COMPLETE; 1742 1743 return 0; 1744 } 1745 1746 static int __dwc3_gadget_get_frame(struct dwc3 *dwc) 1747 { 1748 u32 reg; 1749 1750 reg = dwc3_readl(dwc, DWC3_DSTS); 1751 return DWC3_DSTS_SOFFN(reg); 1752 } 1753 1754 /** 1755 * __dwc3_stop_active_transfer - stop the current active transfer 1756 * @dep: isoc endpoint 1757 * @force: set forcerm bit in the command 1758 * @interrupt: command complete interrupt after End Transfer command 1759 * 1760 * When setting force, the ForceRM bit will be set. In that case 1761 * the controller won't update the TRB progress on command 1762 * completion. It also won't clear the HWO bit in the TRB. 1763 * The command will also not complete immediately in that case. 1764 * 1765 * Older programming guide revisions recommended setting ForceRM to 1 1766 * when ending a transfer. Newer programming guide revisions now 1767 * recommend keeping ForceRM cleared, and TRBs are properly updated 1768 * on command completion. 1769 */ 1770 static int __dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force, bool interrupt) 1771 { 1772 struct dwc3_gadget_ep_cmd_params params; 1773 u32 cmd; 1774 int ret; 1775 1776 cmd = DWC3_DEPCMD_ENDTRANSFER; 1777 cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0; 1778 cmd |= interrupt ? DWC3_DEPCMD_CMDIOC : 0; 1779 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index); 1780 memset(¶ms, 0, sizeof(params)); 1781 ret = dwc3_send_gadget_ep_cmd(dep, cmd, ¶ms); 1782 /* 1783 * If the End Transfer command was timed out while the device is 1784 * not in SETUP phase, it's possible that an incoming Setup packet 1785 * may prevent the command's completion. Let's retry when the 1786 * ep0state returns to EP0_SETUP_PHASE. 1787 */ 1788 if (ret == -ETIMEDOUT && dep->dwc->ep0state != EP0_SETUP_PHASE) { 1789 dep->flags |= DWC3_EP_DELAY_STOP; 1790 return 0; 1791 } 1792 1793 if (ret) 1794 dev_err_ratelimited(dep->dwc->dev, 1795 "end transfer failed: %d\n", ret); 1796 1797 dep->resource_index = 0; 1798 1799 if (!interrupt) 1800 dep->flags &= ~DWC3_EP_TRANSFER_STARTED; 1801 else if (!ret) 1802 dep->flags |= DWC3_EP_END_TRANSFER_PENDING; 1803 1804 dep->flags &= ~DWC3_EP_DELAY_STOP; 1805 return ret; 1806 } 1807 1808 /** 1809 * dwc3_gadget_start_isoc_quirk - workaround invalid frame number 1810 * @dep: isoc endpoint 1811 * 1812 * This function tests for the correct combination of BIT[15:14] from the 16-bit 1813 * microframe number reported by the XferNotReady event for the future frame 1814 * number to start the isoc transfer. 1815 * 1816 * In DWC_usb31 version 1.70a-ea06 and prior, for highspeed and fullspeed 1817 * isochronous IN, BIT[15:14] of the 16-bit microframe number reported by the 1818 * XferNotReady event are invalid. The driver uses this number to schedule the 1819 * isochronous transfer and passes it to the START TRANSFER command. Because 1820 * this number is invalid, the command may fail. If BIT[15:14] matches the 1821 * internal 16-bit microframe, the START TRANSFER command will pass and the 1822 * transfer will start at the scheduled time, if it is off by 1, the command 1823 * will still pass, but the transfer will start 2 seconds in the future. For all 1824 * other conditions, the START TRANSFER command will fail with bus-expiry. 1825 * 1826 * In order to workaround this issue, we can test for the correct combination of 1827 * BIT[15:14] by sending START TRANSFER commands with different values of 1828 * BIT[15:14]: 'b00, 'b01, 'b10, and 'b11. Each combination is 2^14 uframe apart 1829 * (or 2 seconds). 4 seconds into the future will result in a bus-expiry status. 1830 * As the result, within the 4 possible combinations for BIT[15:14], there will 1831 * be 2 successful and 2 failure START COMMAND status. One of the 2 successful 1832 * command status will result in a 2-second delay start. The smaller BIT[15:14] 1833 * value is the correct combination. 1834 * 1835 * Since there are only 4 outcomes and the results are ordered, we can simply 1836 * test 2 START TRANSFER commands with BIT[15:14] combinations 'b00 and 'b01 to 1837 * deduce the smaller successful combination. 1838 * 1839 * Let test0 = test status for combination 'b00 and test1 = test status for 'b01 1840 * of BIT[15:14]. The correct combination is as follow: 1841 * 1842 * if test0 fails and test1 passes, BIT[15:14] is 'b01 1843 * if test0 fails and test1 fails, BIT[15:14] is 'b10 1844 * if test0 passes and test1 fails, BIT[15:14] is 'b11 1845 * if test0 passes and test1 passes, BIT[15:14] is 'b00 1846 * 1847 * Synopsys STAR 9001202023: Wrong microframe number for isochronous IN 1848 * endpoints. 1849 */ 1850 static int dwc3_gadget_start_isoc_quirk(struct dwc3_ep *dep) 1851 { 1852 int cmd_status = 0; 1853 bool test0; 1854 bool test1; 1855 1856 while (dep->combo_num < 2) { 1857 struct dwc3_gadget_ep_cmd_params params; 1858 u32 test_frame_number; 1859 u32 cmd; 1860 1861 /* 1862 * Check if we can start isoc transfer on the next interval or 1863 * 4 uframes in the future with BIT[15:14] as dep->combo_num 1864 */ 1865 test_frame_number = dep->frame_number & DWC3_FRNUMBER_MASK; 1866 test_frame_number |= dep->combo_num << 14; 1867 test_frame_number += max_t(u32, 4, dep->interval); 1868 1869 params.param0 = upper_32_bits(dep->dwc->bounce_addr); 1870 params.param1 = lower_32_bits(dep->dwc->bounce_addr); 1871 1872 cmd = DWC3_DEPCMD_STARTTRANSFER; 1873 cmd |= DWC3_DEPCMD_PARAM(test_frame_number); 1874 cmd_status = dwc3_send_gadget_ep_cmd(dep, cmd, ¶ms); 1875 1876 /* Redo if some other failure beside bus-expiry is received */ 1877 if (cmd_status && cmd_status != -EAGAIN) { 1878 dep->start_cmd_status = 0; 1879 dep->combo_num = 0; 1880 return 0; 1881 } 1882 1883 /* Store the first test status */ 1884 if (dep->combo_num == 0) 1885 dep->start_cmd_status = cmd_status; 1886 1887 dep->combo_num++; 1888 1889 /* 1890 * End the transfer if the START_TRANSFER command is successful 1891 * to wait for the next XferNotReady to test the command again 1892 */ 1893 if (cmd_status == 0) { 1894 dwc3_stop_active_transfer(dep, false, true); 1895 return 0; 1896 } 1897 } 1898 1899 /* test0 and test1 are both completed at this point */ 1900 test0 = (dep->start_cmd_status == 0); 1901 test1 = (cmd_status == 0); 1902 1903 if (!test0 && test1) 1904 dep->combo_num = 1; 1905 else if (!test0 && !test1) 1906 dep->combo_num = 2; 1907 else if (test0 && !test1) 1908 dep->combo_num = 3; 1909 else if (test0 && test1) 1910 dep->combo_num = 0; 1911 1912 dep->frame_number &= DWC3_FRNUMBER_MASK; 1913 dep->frame_number |= dep->combo_num << 14; 1914 dep->frame_number += max_t(u32, 4, dep->interval); 1915 1916 /* Reinitialize test variables */ 1917 dep->start_cmd_status = 0; 1918 dep->combo_num = 0; 1919 1920 return __dwc3_gadget_kick_transfer(dep); 1921 } 1922 1923 static int __dwc3_gadget_start_isoc(struct dwc3_ep *dep) 1924 { 1925 const struct usb_endpoint_descriptor *desc = dep->endpoint.desc; 1926 struct dwc3 *dwc = dep->dwc; 1927 int ret; 1928 int i; 1929 1930 if (list_empty(&dep->pending_list) && 1931 list_empty(&dep->started_list)) { 1932 dep->flags |= DWC3_EP_PENDING_REQUEST; 1933 return -EAGAIN; 1934 } 1935 1936 if (!dwc->dis_start_transfer_quirk && 1937 (DWC3_VER_IS_PRIOR(DWC31, 170A) || 1938 DWC3_VER_TYPE_IS_WITHIN(DWC31, 170A, EA01, EA06))) { 1939 if (dwc->gadget->speed <= USB_SPEED_HIGH && dep->direction) 1940 return dwc3_gadget_start_isoc_quirk(dep); 1941 } 1942 1943 if (desc->bInterval <= 14 && 1944 dwc->gadget->speed >= USB_SPEED_HIGH) { 1945 u32 frame = __dwc3_gadget_get_frame(dwc); 1946 bool rollover = frame < 1947 (dep->frame_number & DWC3_FRNUMBER_MASK); 1948 1949 /* 1950 * frame_number is set from XferNotReady and may be already 1951 * out of date. DSTS only provides the lower 14 bit of the 1952 * current frame number. So add the upper two bits of 1953 * frame_number and handle a possible rollover. 1954 * This will provide the correct frame_number unless more than 1955 * rollover has happened since XferNotReady. 1956 */ 1957 1958 dep->frame_number = (dep->frame_number & ~DWC3_FRNUMBER_MASK) | 1959 frame; 1960 if (rollover) 1961 dep->frame_number += BIT(14); 1962 } 1963 1964 for (i = 0; i < DWC3_ISOC_MAX_RETRIES; i++) { 1965 int future_interval = i + 1; 1966 1967 /* Give the controller at least 500us to schedule transfers */ 1968 if (desc->bInterval < 3) 1969 future_interval += 3 - desc->bInterval; 1970 1971 dep->frame_number = DWC3_ALIGN_FRAME(dep, future_interval); 1972 1973 ret = __dwc3_gadget_kick_transfer(dep); 1974 if (ret != -EAGAIN) 1975 break; 1976 } 1977 1978 /* 1979 * After a number of unsuccessful start attempts due to bus-expiry 1980 * status, issue END_TRANSFER command and retry on the next XferNotReady 1981 * event. 1982 */ 1983 if (ret == -EAGAIN) 1984 ret = __dwc3_stop_active_transfer(dep, false, true); 1985 1986 return ret; 1987 } 1988 1989 static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req) 1990 { 1991 struct dwc3 *dwc = dep->dwc; 1992 1993 if (!dep->endpoint.desc || !dwc->pullups_connected || !dwc->connected) { 1994 dev_dbg(dwc->dev, "%s: can't queue to disabled endpoint\n", 1995 dep->name); 1996 return -ESHUTDOWN; 1997 } 1998 1999 if (WARN(req->dep != dep, "request %p belongs to '%s'\n", 2000 &req->request, req->dep->name)) 2001 return -EINVAL; 2002 2003 if (WARN(req->status < DWC3_REQUEST_STATUS_COMPLETED, 2004 "%s: request %p already in flight\n", 2005 dep->name, &req->request)) 2006 return -EINVAL; 2007 2008 pm_runtime_get(dwc->dev); 2009 2010 req->request.actual = 0; 2011 req->request.status = -EINPROGRESS; 2012 2013 trace_dwc3_ep_queue(req); 2014 2015 list_add_tail(&req->list, &dep->pending_list); 2016 req->status = DWC3_REQUEST_STATUS_QUEUED; 2017 2018 if (dep->flags & DWC3_EP_WAIT_TRANSFER_COMPLETE) 2019 return 0; 2020 2021 /* 2022 * Start the transfer only after the END_TRANSFER is completed 2023 * and endpoint STALL is cleared. 2024 */ 2025 if ((dep->flags & DWC3_EP_END_TRANSFER_PENDING) || 2026 (dep->flags & DWC3_EP_WEDGE) || 2027 (dep->flags & DWC3_EP_DELAY_STOP) || 2028 (dep->flags & DWC3_EP_STALL)) { 2029 dep->flags |= DWC3_EP_DELAY_START; 2030 return 0; 2031 } 2032 2033 /* 2034 * NOTICE: Isochronous endpoints should NEVER be prestarted. We must 2035 * wait for a XferNotReady event so we will know what's the current 2036 * (micro-)frame number. 2037 * 2038 * Without this trick, we are very, very likely gonna get Bus Expiry 2039 * errors which will force us issue EndTransfer command. 2040 */ 2041 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) { 2042 if (!(dep->flags & DWC3_EP_TRANSFER_STARTED)) { 2043 if ((dep->flags & DWC3_EP_PENDING_REQUEST)) 2044 return __dwc3_gadget_start_isoc(dep); 2045 2046 return 0; 2047 } 2048 } 2049 2050 __dwc3_gadget_kick_transfer(dep); 2051 2052 return 0; 2053 } 2054 2055 static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request, 2056 gfp_t gfp_flags) 2057 { 2058 struct dwc3_request *req = to_dwc3_request(request); 2059 struct dwc3_ep *dep = to_dwc3_ep(ep); 2060 struct dwc3 *dwc = dep->dwc; 2061 2062 unsigned long flags; 2063 2064 int ret; 2065 2066 spin_lock_irqsave(&dwc->lock, flags); 2067 if (dwc->err_state != DWC3_ERR_NONE) { 2068 spin_unlock_irqrestore(&dwc->lock, flags); 2069 return -ESHUTDOWN; 2070 } 2071 ret = __dwc3_gadget_ep_queue(dep, req); 2072 spin_unlock_irqrestore(&dwc->lock, flags); 2073 2074 return ret; 2075 } 2076 2077 static void dwc3_gadget_ep_skip_trbs(struct dwc3_ep *dep, struct dwc3_request *req) 2078 { 2079 int i; 2080 2081 /* If req->trb is not set, then the request has not started */ 2082 if (!req->trb) 2083 return; 2084 2085 /* 2086 * If request was already started, this means we had to 2087 * stop the transfer. With that we also need to ignore 2088 * all TRBs used by the request, however TRBs can only 2089 * be modified after completion of END_TRANSFER 2090 * command. So what we do here is that we wait for 2091 * END_TRANSFER completion and only after that, we jump 2092 * over TRBs by clearing HWO and incrementing dequeue 2093 * pointer. 2094 */ 2095 for (i = 0; i < req->num_trbs; i++) { 2096 struct dwc3_trb *trb; 2097 2098 trb = &dep->trb_pool[dep->trb_dequeue]; 2099 trb->ctrl &= ~DWC3_TRB_CTRL_HWO; 2100 dwc3_ep_inc_deq(dep); 2101 } 2102 2103 req->num_trbs = 0; 2104 } 2105 2106 static void dwc3_gadget_ep_cleanup_cancelled_requests(struct dwc3_ep *dep) 2107 { 2108 struct dwc3_request *req; 2109 struct dwc3 *dwc = dep->dwc; 2110 2111 while (!list_empty(&dep->cancelled_list)) { 2112 req = next_request(&dep->cancelled_list); 2113 dwc3_gadget_ep_skip_trbs(dep, req); 2114 switch (req->status) { 2115 case DWC3_REQUEST_STATUS_DISCONNECTED: 2116 dwc3_gadget_giveback(dep, req, -ESHUTDOWN); 2117 break; 2118 case DWC3_REQUEST_STATUS_DEQUEUED: 2119 dwc3_gadget_giveback(dep, req, -ECONNRESET); 2120 break; 2121 case DWC3_REQUEST_STATUS_STALLED: 2122 dwc3_gadget_giveback(dep, req, -EPIPE); 2123 break; 2124 default: 2125 dev_err(dwc->dev, "request cancelled with wrong reason:%d\n", req->status); 2126 dwc3_gadget_giveback(dep, req, -ECONNRESET); 2127 break; 2128 } 2129 /* 2130 * The endpoint is disabled, let the dwc3_remove_requests() 2131 * handle the cleanup. 2132 */ 2133 if (!dep->endpoint.desc) 2134 break; 2135 } 2136 } 2137 2138 static int dwc3_gadget_ep_dequeue(struct usb_ep *ep, 2139 struct usb_request *request) 2140 { 2141 struct dwc3_request *req = to_dwc3_request(request); 2142 struct dwc3_request *r = NULL; 2143 2144 struct dwc3_ep *dep = to_dwc3_ep(ep); 2145 struct dwc3 *dwc = dep->dwc; 2146 2147 unsigned long flags; 2148 int ret = 0; 2149 2150 trace_dwc3_ep_dequeue(req); 2151 2152 spin_lock_irqsave(&dwc->lock, flags); 2153 2154 list_for_each_entry(r, &dep->cancelled_list, list) { 2155 if (r == req) 2156 goto out; 2157 } 2158 2159 list_for_each_entry(r, &dep->pending_list, list) { 2160 if (r == req) { 2161 /* 2162 * Explicitly check for EP0/1 as dequeue for those 2163 * EPs need to be handled differently. Control EP 2164 * only deals with one USB req, and giveback will 2165 * occur during dwc3_ep0_stall_and_restart(). EP0 2166 * requests are never added to started_list. 2167 */ 2168 if (dep->number > 1) 2169 dwc3_gadget_giveback(dep, req, -ECONNRESET); 2170 else 2171 dwc3_ep0_reset_state(dwc); 2172 goto out; 2173 } 2174 } 2175 2176 list_for_each_entry(r, &dep->started_list, list) { 2177 if (r == req) { 2178 struct dwc3_request *t; 2179 2180 /* wait until it is processed */ 2181 dwc3_stop_active_transfer(dep, false, true); 2182 2183 /* 2184 * Remove any started request if the transfer is 2185 * cancelled. 2186 */ 2187 list_for_each_entry_safe(r, t, &dep->started_list, list) 2188 dwc3_gadget_move_cancelled_request(r, 2189 DWC3_REQUEST_STATUS_DEQUEUED); 2190 2191 dep->flags &= ~DWC3_EP_WAIT_TRANSFER_COMPLETE; 2192 2193 goto out; 2194 } 2195 } 2196 2197 dev_err(dwc->dev, "request %p was not queued to %s\n", 2198 request, ep->name); 2199 ret = -EINVAL; 2200 out: 2201 spin_unlock_irqrestore(&dwc->lock, flags); 2202 2203 return ret; 2204 } 2205 2206 int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol) 2207 { 2208 struct dwc3_gadget_ep_cmd_params params; 2209 struct dwc3 *dwc = dep->dwc; 2210 struct dwc3_request *req; 2211 struct dwc3_request *tmp; 2212 int ret; 2213 2214 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) { 2215 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name); 2216 return -EINVAL; 2217 } 2218 2219 memset(¶ms, 0x00, sizeof(params)); 2220 2221 if (value) { 2222 struct dwc3_trb *trb; 2223 2224 unsigned int transfer_in_flight; 2225 unsigned int started; 2226 2227 if (dep->number > 1) 2228 trb = dwc3_ep_prev_trb(dep, dep->trb_enqueue); 2229 else 2230 trb = &dwc->ep0_trb[dep->trb_enqueue]; 2231 2232 transfer_in_flight = trb->ctrl & DWC3_TRB_CTRL_HWO; 2233 started = !list_empty(&dep->started_list); 2234 2235 if (!protocol && ((dep->direction && transfer_in_flight) || 2236 (!dep->direction && started))) { 2237 return -EAGAIN; 2238 } 2239 2240 ret = dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETSTALL, 2241 ¶ms); 2242 if (ret) 2243 dev_err(dwc->dev, "failed to set STALL on %s\n", 2244 dep->name); 2245 else 2246 dep->flags |= DWC3_EP_STALL; 2247 } else { 2248 /* 2249 * Don't issue CLEAR_STALL command to control endpoints. The 2250 * controller automatically clears the STALL when it receives 2251 * the SETUP token. 2252 */ 2253 if (dep->number <= 1) { 2254 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE); 2255 return 0; 2256 } 2257 2258 dwc3_stop_active_transfer(dep, false, true); 2259 2260 list_for_each_entry_safe(req, tmp, &dep->started_list, list) 2261 dwc3_gadget_move_cancelled_request(req, DWC3_REQUEST_STATUS_STALLED); 2262 2263 if (dep->flags & DWC3_EP_END_TRANSFER_PENDING || 2264 (dep->flags & DWC3_EP_DELAY_STOP)) { 2265 dep->flags |= DWC3_EP_PENDING_CLEAR_STALL; 2266 if (protocol) 2267 dwc->clear_stall_protocol = dep->number; 2268 2269 return 0; 2270 } 2271 2272 dwc3_gadget_ep_cleanup_cancelled_requests(dep); 2273 2274 ret = dwc3_send_clear_stall_ep_cmd(dep); 2275 if (ret) { 2276 dev_err(dwc->dev, "failed to clear STALL on %s\n", 2277 dep->name); 2278 return ret; 2279 } 2280 2281 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE); 2282 2283 if ((dep->flags & DWC3_EP_DELAY_START) && 2284 !usb_endpoint_xfer_isoc(dep->endpoint.desc)) 2285 __dwc3_gadget_kick_transfer(dep); 2286 2287 dep->flags &= ~DWC3_EP_DELAY_START; 2288 } 2289 2290 return ret; 2291 } 2292 2293 static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value) 2294 { 2295 struct dwc3_ep *dep = to_dwc3_ep(ep); 2296 struct dwc3 *dwc = dep->dwc; 2297 2298 unsigned long flags; 2299 2300 int ret; 2301 2302 spin_lock_irqsave(&dwc->lock, flags); 2303 if (dwc->err_state != DWC3_ERR_NONE) { 2304 spin_unlock_irqrestore(&dwc->lock, flags); 2305 return -ESHUTDOWN; 2306 } 2307 ret = __dwc3_gadget_ep_set_halt(dep, value, false); 2308 spin_unlock_irqrestore(&dwc->lock, flags); 2309 2310 return ret; 2311 } 2312 2313 static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep) 2314 { 2315 struct dwc3_ep *dep = to_dwc3_ep(ep); 2316 struct dwc3 *dwc = dep->dwc; 2317 unsigned long flags; 2318 int ret; 2319 2320 spin_lock_irqsave(&dwc->lock, flags); 2321 if (dwc->err_state != DWC3_ERR_NONE) { 2322 spin_unlock_irqrestore(&dwc->lock, flags); 2323 return -ESHUTDOWN; 2324 } 2325 dep->flags |= DWC3_EP_WEDGE; 2326 2327 if (dep->number == 0 || dep->number == 1) 2328 ret = __dwc3_gadget_ep0_set_halt(ep, 1); 2329 else 2330 ret = __dwc3_gadget_ep_set_halt(dep, 1, false); 2331 spin_unlock_irqrestore(&dwc->lock, flags); 2332 2333 return ret; 2334 } 2335 2336 /* -------------------------------------------------------------------------- */ 2337 2338 static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = { 2339 .bLength = USB_DT_ENDPOINT_SIZE, 2340 .bDescriptorType = USB_DT_ENDPOINT, 2341 .bmAttributes = USB_ENDPOINT_XFER_CONTROL, 2342 }; 2343 2344 static const struct usb_ep_ops dwc3_gadget_ep0_ops = { 2345 .enable = dwc3_gadget_ep0_enable, 2346 .disable = dwc3_gadget_ep0_disable, 2347 .alloc_request = dwc3_gadget_ep_alloc_request, 2348 .free_request = dwc3_gadget_ep_free_request, 2349 .queue = dwc3_gadget_ep0_queue, 2350 .dequeue = dwc3_gadget_ep_dequeue, 2351 .set_halt = dwc3_gadget_ep0_set_halt, 2352 .set_wedge = dwc3_gadget_ep_set_wedge, 2353 }; 2354 2355 static const struct usb_ep_ops dwc3_gadget_ep_ops = { 2356 .enable = dwc3_gadget_ep_enable, 2357 .disable = dwc3_gadget_ep_disable, 2358 .alloc_request = dwc3_gadget_ep_alloc_request, 2359 .free_request = dwc3_gadget_ep_free_request, 2360 .queue = dwc3_gadget_ep_queue, 2361 .dequeue = dwc3_gadget_ep_dequeue, 2362 .set_halt = dwc3_gadget_ep_set_halt, 2363 .set_wedge = dwc3_gadget_ep_set_wedge, 2364 }; 2365 2366 /* -------------------------------------------------------------------------- */ 2367 2368 static void dwc3_gadget_enable_linksts_evts(struct dwc3 *dwc, bool set) 2369 { 2370 u32 reg; 2371 2372 if (DWC3_VER_IS_PRIOR(DWC3, 250A)) 2373 return; 2374 2375 reg = dwc3_readl(dwc, DWC3_DEVTEN); 2376 if (set) 2377 reg |= DWC3_DEVTEN_ULSTCNGEN; 2378 else 2379 reg &= ~DWC3_DEVTEN_ULSTCNGEN; 2380 2381 dwc3_writel(dwc, DWC3_DEVTEN, reg); 2382 } 2383 2384 static int dwc3_gadget_get_frame(struct usb_gadget *g) 2385 { 2386 struct dwc3 *dwc = gadget_to_dwc(g); 2387 2388 return __dwc3_gadget_get_frame(dwc); 2389 } 2390 2391 static int __dwc3_gadget_wakeup(struct dwc3 *dwc) 2392 { 2393 int ret; 2394 u32 reg; 2395 2396 u8 link_state; 2397 2398 /* 2399 * According to the Databook Remote wakeup request should 2400 * be issued only when the device is in early suspend state. 2401 * 2402 * We can check that via USB Link State bits in DSTS register. 2403 */ 2404 reg = dwc3_readl(dwc, DWC3_DSTS); 2405 2406 link_state = DWC3_DSTS_USBLNKST(reg); 2407 2408 switch (link_state) { 2409 case DWC3_LINK_STATE_RESET: 2410 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */ 2411 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */ 2412 case DWC3_LINK_STATE_U2: /* in HS, means Sleep (L1) */ 2413 case DWC3_LINK_STATE_U1: 2414 case DWC3_LINK_STATE_RESUME: 2415 break; 2416 default: 2417 return -EINVAL; 2418 } 2419 2420 dwc3_gadget_enable_linksts_evts(dwc, true); 2421 2422 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV); 2423 if (ret < 0) { 2424 dev_err(dwc->dev, "failed to put link in Recovery\n"); 2425 dwc3_gadget_enable_linksts_evts(dwc, false); 2426 return ret; 2427 } 2428 2429 /* Recent versions do this automatically */ 2430 if (DWC3_VER_IS_PRIOR(DWC3, 194A)) { 2431 /* write zeroes to Link Change Request */ 2432 reg = dwc3_readl(dwc, DWC3_DCTL); 2433 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK; 2434 dwc3_writel(dwc, DWC3_DCTL, reg); 2435 } 2436 2437 /* 2438 * Since link status change events are enabled we will receive 2439 * an U0 event when wakeup is successful. 2440 */ 2441 return 0; 2442 } 2443 2444 static int dwc3_gadget_wakeup(struct usb_gadget *g) 2445 { 2446 struct dwc3 *dwc = gadget_to_dwc(g); 2447 unsigned long flags; 2448 int ret; 2449 2450 if (!dwc->wakeup_configured) { 2451 dev_err(dwc->dev, "remote wakeup not configured\n"); 2452 return -EINVAL; 2453 } 2454 2455 spin_lock_irqsave(&dwc->lock, flags); 2456 if (dwc->err_state != DWC3_ERR_NONE) { 2457 spin_unlock_irqrestore(&dwc->lock, flags); 2458 return -ESHUTDOWN; 2459 } 2460 if (!dwc->gadget->wakeup_armed) { 2461 dev_err(dwc->dev, "not armed for remote wakeup\n"); 2462 spin_unlock_irqrestore(&dwc->lock, flags); 2463 return -EINVAL; 2464 } 2465 ret = __dwc3_gadget_wakeup(dwc); 2466 2467 spin_unlock_irqrestore(&dwc->lock, flags); 2468 2469 return ret; 2470 } 2471 2472 static void dwc3_resume_gadget(struct dwc3 *dwc); 2473 2474 static int dwc3_gadget_func_wakeup(struct usb_gadget *g, int intf_id) 2475 { 2476 struct dwc3 *dwc = gadget_to_dwc(g); 2477 unsigned long flags; 2478 int ret; 2479 int link_state; 2480 2481 if (!dwc->wakeup_configured) { 2482 dev_err(dwc->dev, "remote wakeup not configured\n"); 2483 return -EINVAL; 2484 } 2485 2486 spin_lock_irqsave(&dwc->lock, flags); 2487 if (dwc->err_state != DWC3_ERR_NONE) { 2488 spin_unlock_irqrestore(&dwc->lock, flags); 2489 return -ESHUTDOWN; 2490 } 2491 /* 2492 * If the link is in U3, signal for remote wakeup and wait for the 2493 * link to transition to U0 before sending device notification. 2494 */ 2495 link_state = dwc3_gadget_get_link_state(dwc); 2496 if (link_state == DWC3_LINK_STATE_U3) { 2497 dwc->wakeup_pending_funcs |= BIT(intf_id); 2498 ret = __dwc3_gadget_wakeup(dwc); 2499 spin_unlock_irqrestore(&dwc->lock, flags); 2500 return ret; 2501 } 2502 2503 ret = dwc3_send_gadget_generic_command(dwc, DWC3_DGCMD_DEV_NOTIFICATION, 2504 DWC3_DGCMDPAR_DN_FUNC_WAKE | 2505 DWC3_DGCMDPAR_INTF_SEL(intf_id)); 2506 if (ret) 2507 dev_err(dwc->dev, "function remote wakeup failed, ret:%d\n", ret); 2508 2509 spin_unlock_irqrestore(&dwc->lock, flags); 2510 2511 return ret; 2512 } 2513 2514 static int dwc3_gadget_set_remote_wakeup(struct usb_gadget *g, int set) 2515 { 2516 struct dwc3 *dwc = gadget_to_dwc(g); 2517 unsigned long flags; 2518 2519 spin_lock_irqsave(&dwc->lock, flags); 2520 dwc->wakeup_configured = !!set; 2521 spin_unlock_irqrestore(&dwc->lock, flags); 2522 2523 return 0; 2524 } 2525 2526 static int dwc3_gadget_set_selfpowered(struct usb_gadget *g, 2527 int is_selfpowered) 2528 { 2529 struct dwc3 *dwc = gadget_to_dwc(g); 2530 unsigned long flags; 2531 2532 spin_lock_irqsave(&dwc->lock, flags); 2533 g->is_selfpowered = !!is_selfpowered; 2534 spin_unlock_irqrestore(&dwc->lock, flags); 2535 2536 return 0; 2537 } 2538 2539 static void dwc3_stop_active_transfers(struct dwc3 *dwc) 2540 { 2541 u32 epnum; 2542 2543 for (epnum = 2; epnum < dwc->num_eps; epnum++) { 2544 struct dwc3_ep *dep; 2545 2546 dep = dwc->eps[epnum]; 2547 if (!dep) 2548 continue; 2549 2550 dwc3_remove_requests(dwc, dep, -ESHUTDOWN); 2551 } 2552 } 2553 2554 static void __dwc3_gadget_set_ssp_rate(struct dwc3 *dwc) 2555 { 2556 enum usb_ssp_rate ssp_rate = dwc->gadget_ssp_rate; 2557 u32 reg; 2558 2559 if (ssp_rate == USB_SSP_GEN_UNKNOWN) 2560 ssp_rate = dwc->max_ssp_rate; 2561 2562 reg = dwc3_readl(dwc, DWC3_DCFG); 2563 reg &= ~DWC3_DCFG_SPEED_MASK; 2564 reg &= ~DWC3_DCFG_NUMLANES(~0); 2565 2566 if (ssp_rate == USB_SSP_GEN_1x2) 2567 reg |= DWC3_DCFG_SUPERSPEED; 2568 else if (dwc->max_ssp_rate != USB_SSP_GEN_1x2) 2569 reg |= DWC3_DCFG_SUPERSPEED_PLUS; 2570 2571 if (ssp_rate != USB_SSP_GEN_2x1 && 2572 dwc->max_ssp_rate != USB_SSP_GEN_2x1) 2573 reg |= DWC3_DCFG_NUMLANES(1); 2574 2575 dwc3_writel(dwc, DWC3_DCFG, reg); 2576 } 2577 2578 static void __dwc3_gadget_set_speed(struct dwc3 *dwc) 2579 { 2580 enum usb_device_speed speed; 2581 u32 reg; 2582 2583 speed = dwc->gadget_max_speed; 2584 if (speed == USB_SPEED_UNKNOWN || speed > dwc->maximum_speed) 2585 speed = dwc->maximum_speed; 2586 2587 if (speed == USB_SPEED_SUPER_PLUS && 2588 DWC3_IP_IS(DWC32)) { 2589 __dwc3_gadget_set_ssp_rate(dwc); 2590 return; 2591 } 2592 2593 reg = dwc3_readl(dwc, DWC3_DCFG); 2594 reg &= ~(DWC3_DCFG_SPEED_MASK); 2595 2596 /* 2597 * WORKAROUND: DWC3 revision < 2.20a have an issue 2598 * which would cause metastability state on Run/Stop 2599 * bit if we try to force the IP to USB2-only mode. 2600 * 2601 * Because of that, we cannot configure the IP to any 2602 * speed other than the SuperSpeed 2603 * 2604 * Refers to: 2605 * 2606 * STAR#9000525659: Clock Domain Crossing on DCTL in 2607 * USB 2.0 Mode 2608 */ 2609 if (DWC3_VER_IS_PRIOR(DWC3, 220A) && 2610 !dwc->dis_metastability_quirk) { 2611 reg |= DWC3_DCFG_SUPERSPEED; 2612 } else { 2613 switch (speed) { 2614 case USB_SPEED_FULL: 2615 reg |= DWC3_DCFG_FULLSPEED; 2616 break; 2617 case USB_SPEED_HIGH: 2618 reg |= DWC3_DCFG_HIGHSPEED; 2619 break; 2620 case USB_SPEED_SUPER: 2621 reg |= DWC3_DCFG_SUPERSPEED; 2622 break; 2623 case USB_SPEED_SUPER_PLUS: 2624 if (DWC3_IP_IS(DWC3)) 2625 reg |= DWC3_DCFG_SUPERSPEED; 2626 else 2627 reg |= DWC3_DCFG_SUPERSPEED_PLUS; 2628 break; 2629 default: 2630 dev_err(dwc->dev, "invalid speed (%d)\n", speed); 2631 2632 if (DWC3_IP_IS(DWC3)) 2633 reg |= DWC3_DCFG_SUPERSPEED; 2634 else 2635 reg |= DWC3_DCFG_SUPERSPEED_PLUS; 2636 } 2637 } 2638 2639 if (DWC3_IP_IS(DWC32) && 2640 speed > USB_SPEED_UNKNOWN && 2641 speed < USB_SPEED_SUPER_PLUS) 2642 reg &= ~DWC3_DCFG_NUMLANES(~0); 2643 2644 dwc3_writel(dwc, DWC3_DCFG, reg); 2645 } 2646 2647 static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on) 2648 { 2649 u32 reg; 2650 u32 timeout = 2000; 2651 u32 saved_config = 0; 2652 2653 if (pm_runtime_suspended(dwc->dev)) 2654 return 0; 2655 2656 /* 2657 * When operating in USB 2.0 speeds (HS/FS), ensure that 2658 * GUSB2PHYCFG.ENBLSLPM and GUSB2PHYCFG.SUSPHY are cleared before starting 2659 * or stopping the controller. This resolves timeout issues that occur 2660 * during frequent role switches between host and device modes. 2661 * 2662 * Save and clear these settings, then restore them after completing the 2663 * controller start or stop sequence. 2664 * 2665 * This solution was discovered through experimentation as it is not 2666 * mentioned in the dwc3 programming guide. It has been tested on an 2667 * Exynos platforms. 2668 */ 2669 reg = dwc3_readl(dwc, DWC3_GUSB2PHYCFG(0)); 2670 if (reg & DWC3_GUSB2PHYCFG_SUSPHY) { 2671 saved_config |= DWC3_GUSB2PHYCFG_SUSPHY; 2672 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY; 2673 } 2674 2675 if (reg & DWC3_GUSB2PHYCFG_ENBLSLPM) { 2676 saved_config |= DWC3_GUSB2PHYCFG_ENBLSLPM; 2677 reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM; 2678 } 2679 2680 if (saved_config) 2681 dwc3_writel(dwc, DWC3_GUSB2PHYCFG(0), reg); 2682 2683 reg = dwc3_readl(dwc, DWC3_DCTL); 2684 if (is_on) { 2685 if (DWC3_VER_IS_WITHIN(DWC3, ANY, 187A)) { 2686 reg &= ~DWC3_DCTL_TRGTULST_MASK; 2687 reg |= DWC3_DCTL_TRGTULST_RX_DET; 2688 } 2689 2690 if (!DWC3_VER_IS_PRIOR(DWC3, 194A)) 2691 reg &= ~DWC3_DCTL_KEEP_CONNECT; 2692 reg |= DWC3_DCTL_RUN_STOP; 2693 2694 __dwc3_gadget_set_speed(dwc); 2695 dwc->pullups_connected = true; 2696 } else { 2697 reg &= ~DWC3_DCTL_RUN_STOP; 2698 2699 dwc->pullups_connected = false; 2700 } 2701 2702 dwc3_pre_run_stop(dwc, is_on); 2703 dwc3_gadget_dctl_write_safe(dwc, reg); 2704 2705 do { 2706 usleep_range(1000, 2000); 2707 reg = dwc3_readl(dwc, DWC3_DSTS); 2708 reg &= DWC3_DSTS_DEVCTRLHLT; 2709 } while (--timeout && !(!is_on ^ !reg)); 2710 2711 if (saved_config) { 2712 reg = dwc3_readl(dwc, DWC3_GUSB2PHYCFG(0)); 2713 reg |= saved_config; 2714 dwc3_writel(dwc, DWC3_GUSB2PHYCFG(0), reg); 2715 } 2716 2717 if (!timeout) 2718 return -ETIMEDOUT; 2719 2720 return 0; 2721 } 2722 2723 static void dwc3_gadget_disable_irq(struct dwc3 *dwc); 2724 static void __dwc3_gadget_stop(struct dwc3 *dwc); 2725 static int __dwc3_gadget_start(struct dwc3 *dwc); 2726 2727 static int dwc3_gadget_soft_disconnect(struct dwc3 *dwc) 2728 { 2729 unsigned long flags; 2730 int ret; 2731 2732 spin_lock_irqsave(&dwc->lock, flags); 2733 if (!dwc->pullups_connected) { 2734 spin_unlock_irqrestore(&dwc->lock, flags); 2735 return 0; 2736 } 2737 2738 dwc->connected = false; 2739 2740 /* 2741 * Attempt to end pending SETUP status phase, and not wait for the 2742 * function to do so. 2743 */ 2744 if (dwc->delayed_status) 2745 dwc3_ep0_send_delayed_status(dwc); 2746 2747 /* 2748 * In the Synopsys DesignWare Cores USB3 Databook Rev. 3.30a 2749 * Section 4.1.8 Table 4-7, it states that for a device-initiated 2750 * disconnect, the SW needs to ensure that it sends "a DEPENDXFER 2751 * command for any active transfers" before clearing the RunStop 2752 * bit. 2753 */ 2754 dwc3_stop_active_transfers(dwc); 2755 spin_unlock_irqrestore(&dwc->lock, flags); 2756 2757 /* 2758 * Per databook, when we want to stop the gadget, if a control transfer 2759 * is still in process, complete it and get the core into setup phase. 2760 * In case the host is unresponsive to a SETUP transaction, forcefully 2761 * stall the transfer, and move back to the SETUP phase, so that any 2762 * pending endxfers can be executed. 2763 */ 2764 if (dwc->ep0state != EP0_SETUP_PHASE) { 2765 reinit_completion(&dwc->ep0_in_setup); 2766 2767 ret = wait_for_completion_timeout(&dwc->ep0_in_setup, 2768 msecs_to_jiffies(DWC3_PULL_UP_TIMEOUT)); 2769 if (ret == 0) { 2770 dev_warn(dwc->dev, "wait for SETUP phase timed out\n"); 2771 spin_lock_irqsave(&dwc->lock, flags); 2772 dwc3_ep0_reset_state(dwc); 2773 spin_unlock_irqrestore(&dwc->lock, flags); 2774 } 2775 } 2776 2777 /* 2778 * Note: if the GEVNTCOUNT indicates events in the event buffer, the 2779 * driver needs to acknowledge them before the controller can halt. 2780 * Simply let the interrupt handler acknowledges and handle the 2781 * remaining event generated by the controller while polling for 2782 * DSTS.DEVCTLHLT. 2783 */ 2784 ret = dwc3_gadget_run_stop(dwc, false); 2785 2786 /* 2787 * Stop the gadget after controller is halted, so that if needed, the 2788 * events to update EP0 state can still occur while the run/stop 2789 * routine polls for the halted state. DEVTEN is cleared as part of 2790 * gadget stop. 2791 */ 2792 spin_lock_irqsave(&dwc->lock, flags); 2793 __dwc3_gadget_stop(dwc); 2794 spin_unlock_irqrestore(&dwc->lock, flags); 2795 2796 usb_gadget_set_state(dwc->gadget, USB_STATE_NOTATTACHED); 2797 2798 return ret; 2799 } 2800 2801 static int dwc3_gadget_soft_connect(struct dwc3 *dwc) 2802 { 2803 int ret; 2804 2805 /* 2806 * In the Synopsys DWC_usb31 1.90a programming guide section 2807 * 4.1.9, it specifies that for a reconnect after a 2808 * device-initiated disconnect requires a core soft reset 2809 * (DCTL.CSftRst) before enabling the run/stop bit. 2810 */ 2811 ret = dwc3_core_soft_reset(dwc); 2812 if (ret) 2813 return ret; 2814 2815 dwc3_event_buffers_setup(dwc); 2816 __dwc3_gadget_start(dwc); 2817 return dwc3_gadget_run_stop(dwc, true); 2818 } 2819 2820 static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on) 2821 { 2822 struct dwc3 *dwc = gadget_to_dwc(g); 2823 int ret; 2824 2825 is_on = !!is_on; 2826 2827 dwc->softconnect = is_on; 2828 2829 /* 2830 * Avoid issuing a runtime resume if the device is already in the 2831 * suspended state during gadget disconnect. DWC3 gadget was already 2832 * halted/stopped during runtime suspend. 2833 */ 2834 if (!is_on) { 2835 pm_runtime_barrier(dwc->dev); 2836 if (pm_runtime_suspended(dwc->dev)) 2837 return 0; 2838 } 2839 2840 /* 2841 * Check the return value for successful resume, or error. For a 2842 * successful resume, the DWC3 runtime PM resume routine will handle 2843 * the run stop sequence, so avoid duplicate operations here. 2844 */ 2845 ret = pm_runtime_get_sync(dwc->dev); 2846 if (!ret || ret < 0) { 2847 pm_runtime_put(dwc->dev); 2848 if (ret < 0) 2849 pm_runtime_set_suspended(dwc->dev); 2850 return ret; 2851 } 2852 2853 if (dwc->pullups_connected == is_on) { 2854 pm_runtime_put(dwc->dev); 2855 return 0; 2856 } 2857 2858 synchronize_irq(dwc->irq_gadget); 2859 2860 /* Serialize against dwc3_err_recovery_work() */ 2861 mutex_lock(&dwc->connect_mutex); 2862 if (!is_on) 2863 ret = dwc3_gadget_soft_disconnect(dwc); 2864 else 2865 ret = dwc3_gadget_soft_connect(dwc); 2866 mutex_unlock(&dwc->connect_mutex); 2867 2868 pm_runtime_put(dwc->dev); 2869 2870 return ret; 2871 } 2872 2873 static void dwc3_gadget_enable_irq(struct dwc3 *dwc) 2874 { 2875 u32 reg; 2876 2877 /* Enable all but Start and End of Frame IRQs */ 2878 reg = (DWC3_DEVTEN_EVNTOVERFLOWEN | 2879 DWC3_DEVTEN_CMDCMPLTEN | 2880 DWC3_DEVTEN_ERRTICERREN | 2881 DWC3_DEVTEN_WKUPEVTEN | 2882 DWC3_DEVTEN_CONNECTDONEEN | 2883 DWC3_DEVTEN_USBRSTEN | 2884 DWC3_DEVTEN_DISCONNEVTEN); 2885 2886 if (DWC3_VER_IS_PRIOR(DWC3, 250A)) 2887 reg |= DWC3_DEVTEN_ULSTCNGEN; 2888 2889 /* On 2.30a and above this bit enables U3/L2-L1 Suspend Events */ 2890 if (!DWC3_VER_IS_PRIOR(DWC3, 230A)) 2891 reg |= DWC3_DEVTEN_U3L2L1SUSPEN; 2892 2893 dwc3_writel(dwc, DWC3_DEVTEN, reg); 2894 } 2895 2896 static void dwc3_gadget_disable_irq(struct dwc3 *dwc) 2897 { 2898 /* mask all interrupts */ 2899 dwc3_writel(dwc, DWC3_DEVTEN, 0x00); 2900 } 2901 2902 static irqreturn_t dwc3_interrupt(int irq, void *_dwc); 2903 static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc); 2904 2905 /** 2906 * dwc3_gadget_setup_nump - calculate and initialize NUMP field of %DWC3_DCFG 2907 * @dwc: pointer to our context structure 2908 * 2909 * The following looks like complex but it's actually very simple. In order to 2910 * calculate the number of packets we can burst at once on OUT transfers, we're 2911 * gonna use RxFIFO size. 2912 * 2913 * To calculate RxFIFO size we need two numbers: 2914 * MDWIDTH = size, in bits, of the internal memory bus 2915 * RAM2_DEPTH = depth, in MDWIDTH, of internal RAM2 (where RxFIFO sits) 2916 * 2917 * Given these two numbers, the formula is simple: 2918 * 2919 * RxFIFO Size = (RAM2_DEPTH * MDWIDTH / 8) - 24 - 16; 2920 * 2921 * 24 bytes is for 3x SETUP packets 2922 * 16 bytes is a clock domain crossing tolerance 2923 * 2924 * Given RxFIFO Size, NUMP = RxFIFOSize / 1024; 2925 */ 2926 static void dwc3_gadget_setup_nump(struct dwc3 *dwc) 2927 { 2928 u32 ram2_depth; 2929 u32 mdwidth; 2930 u32 nump; 2931 u32 reg; 2932 2933 ram2_depth = DWC3_GHWPARAMS7_RAM2_DEPTH(dwc->hwparams.hwparams7); 2934 mdwidth = dwc3_mdwidth(dwc); 2935 2936 nump = ((ram2_depth * mdwidth / 8) - 24 - 16) / 1024; 2937 nump = min_t(u32, nump, 16); 2938 2939 /* update NumP */ 2940 reg = dwc3_readl(dwc, DWC3_DCFG); 2941 reg &= ~DWC3_DCFG_NUMP_MASK; 2942 reg |= nump << DWC3_DCFG_NUMP_SHIFT; 2943 dwc3_writel(dwc, DWC3_DCFG, reg); 2944 } 2945 2946 static int __dwc3_gadget_start(struct dwc3 *dwc) 2947 { 2948 struct dwc3_ep *dep; 2949 int ret = 0; 2950 u32 reg; 2951 2952 /* 2953 * Use IMOD if enabled via dwc->imod_interval. Otherwise, if 2954 * the core supports IMOD, disable it. 2955 */ 2956 if (dwc->imod_interval) { 2957 dwc3_writel(dwc, DWC3_DEV_IMOD(0), dwc->imod_interval); 2958 dwc3_writel(dwc, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB); 2959 } else if (dwc3_has_imod(dwc)) { 2960 dwc3_writel(dwc, DWC3_DEV_IMOD(0), 0); 2961 } 2962 2963 /* 2964 * We are telling dwc3 that we want to use DCFG.NUMP as ACK TP's NUMP 2965 * field instead of letting dwc3 itself calculate that automatically. 2966 * 2967 * This way, we maximize the chances that we'll be able to get several 2968 * bursts of data without going through any sort of endpoint throttling. 2969 */ 2970 reg = dwc3_readl(dwc, DWC3_GRXTHRCFG); 2971 if (DWC3_IP_IS(DWC3)) 2972 reg &= ~DWC3_GRXTHRCFG_PKTCNTSEL; 2973 else 2974 reg &= ~DWC31_GRXTHRCFG_PKTCNTSEL; 2975 2976 dwc3_writel(dwc, DWC3_GRXTHRCFG, reg); 2977 2978 dwc3_gadget_setup_nump(dwc); 2979 2980 /* 2981 * Currently the controller handles single stream only. So, Ignore 2982 * Packet Pending bit for stream selection and don't search for another 2983 * stream if the host sends Data Packet with PP=0 (for OUT direction) or 2984 * ACK with NumP=0 and PP=0 (for IN direction). This slightly improves 2985 * the stream performance. 2986 */ 2987 reg = dwc3_readl(dwc, DWC3_DCFG); 2988 reg |= DWC3_DCFG_IGNSTRMPP; 2989 dwc3_writel(dwc, DWC3_DCFG, reg); 2990 2991 /* Enable MST by default if the device is capable of MST */ 2992 if (DWC3_MST_CAPABLE(&dwc->hwparams)) { 2993 reg = dwc3_readl(dwc, DWC3_DCFG1); 2994 reg &= ~DWC3_DCFG1_DIS_MST_ENH; 2995 dwc3_writel(dwc, DWC3_DCFG1, reg); 2996 } 2997 2998 /* Start with SuperSpeed Default */ 2999 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512); 3000 3001 ret = dwc3_gadget_start_config(dwc, 0); 3002 if (ret) { 3003 dev_err(dwc->dev, "failed to config endpoints\n"); 3004 return ret; 3005 } 3006 3007 dep = dwc->eps[0]; 3008 dep->flags = 0; 3009 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT); 3010 if (ret) { 3011 dev_err(dwc->dev, "failed to enable %s\n", dep->name); 3012 goto err0; 3013 } 3014 3015 dep = dwc->eps[1]; 3016 dep->flags = 0; 3017 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT); 3018 if (ret) { 3019 dev_err(dwc->dev, "failed to enable %s\n", dep->name); 3020 goto err1; 3021 } 3022 3023 /* begin to receive SETUP packets */ 3024 dwc->ep0state = EP0_SETUP_PHASE; 3025 dwc->ep0_bounced = false; 3026 dwc->link_state = DWC3_LINK_STATE_SS_DIS; 3027 dwc->delayed_status = false; 3028 dwc3_ep0_out_start(dwc); 3029 3030 dwc3_gadget_enable_irq(dwc); 3031 dwc3_enable_susphy(dwc, true); 3032 3033 return 0; 3034 3035 err1: 3036 __dwc3_gadget_ep_disable(dwc->eps[0]); 3037 3038 err0: 3039 return ret; 3040 } 3041 3042 static int dwc3_gadget_start(struct usb_gadget *g, 3043 struct usb_gadget_driver *driver) 3044 { 3045 struct dwc3 *dwc = gadget_to_dwc(g); 3046 unsigned long flags; 3047 int ret; 3048 int irq; 3049 3050 irq = dwc->irq_gadget; 3051 ret = request_threaded_irq(irq, dwc3_interrupt, dwc3_thread_interrupt, 3052 IRQF_SHARED, "dwc3", dwc->ev_buf); 3053 if (ret) { 3054 dev_err(dwc->dev, "failed to request irq #%d --> %d\n", 3055 irq, ret); 3056 return ret; 3057 } 3058 3059 spin_lock_irqsave(&dwc->lock, flags); 3060 dwc->gadget_driver = driver; 3061 spin_unlock_irqrestore(&dwc->lock, flags); 3062 3063 if (dwc->sys_wakeup) 3064 device_wakeup_enable(dwc->sysdev); 3065 3066 return 0; 3067 } 3068 3069 static void __dwc3_gadget_stop(struct dwc3 *dwc) 3070 { 3071 dwc3_gadget_disable_irq(dwc); 3072 __dwc3_gadget_ep_disable(dwc->eps[0]); 3073 __dwc3_gadget_ep_disable(dwc->eps[1]); 3074 } 3075 3076 static int dwc3_gadget_stop(struct usb_gadget *g) 3077 { 3078 struct dwc3 *dwc = gadget_to_dwc(g); 3079 unsigned long flags; 3080 3081 if (dwc->sys_wakeup) 3082 device_wakeup_disable(dwc->sysdev); 3083 3084 spin_lock_irqsave(&dwc->lock, flags); 3085 dwc->gadget_driver = NULL; 3086 dwc->max_cfg_eps = 0; 3087 spin_unlock_irqrestore(&dwc->lock, flags); 3088 3089 free_irq(dwc->irq_gadget, dwc->ev_buf); 3090 3091 return 0; 3092 } 3093 3094 static void dwc3_gadget_config_params(struct usb_gadget *g, 3095 struct usb_dcd_config_params *params) 3096 { 3097 struct dwc3 *dwc = gadget_to_dwc(g); 3098 3099 params->besl_baseline = USB_DEFAULT_BESL_UNSPECIFIED; 3100 params->besl_deep = USB_DEFAULT_BESL_UNSPECIFIED; 3101 3102 /* Recommended BESL */ 3103 if (!dwc->dis_enblslpm_quirk) { 3104 /* 3105 * If the recommended BESL baseline is 0 or if the BESL deep is 3106 * less than 2, Microsoft's Windows 10 host usb stack will issue 3107 * a usb reset immediately after it receives the extended BOS 3108 * descriptor and the enumeration will fail. To maintain 3109 * compatibility with the Windows' usb stack, let's set the 3110 * recommended BESL baseline to 1 and clamp the BESL deep to be 3111 * within 2 to 15. 3112 */ 3113 params->besl_baseline = 1; 3114 if (dwc->is_utmi_l1_suspend) 3115 params->besl_deep = 3116 clamp_t(u8, dwc->hird_threshold, 2, 15); 3117 } 3118 3119 /* U1 Device exit Latency */ 3120 if (dwc->dis_u1_entry_quirk) 3121 params->bU1devExitLat = 0; 3122 else 3123 params->bU1devExitLat = DWC3_DEFAULT_U1_DEV_EXIT_LAT; 3124 3125 /* U2 Device exit Latency */ 3126 if (dwc->dis_u2_entry_quirk) 3127 params->bU2DevExitLat = 0; 3128 else 3129 params->bU2DevExitLat = 3130 cpu_to_le16(DWC3_DEFAULT_U2_DEV_EXIT_LAT); 3131 } 3132 3133 static void dwc3_gadget_set_speed(struct usb_gadget *g, 3134 enum usb_device_speed speed) 3135 { 3136 struct dwc3 *dwc = gadget_to_dwc(g); 3137 unsigned long flags; 3138 3139 spin_lock_irqsave(&dwc->lock, flags); 3140 dwc->gadget_max_speed = speed; 3141 spin_unlock_irqrestore(&dwc->lock, flags); 3142 } 3143 3144 static void dwc3_gadget_set_ssp_rate(struct usb_gadget *g, 3145 enum usb_ssp_rate rate) 3146 { 3147 struct dwc3 *dwc = gadget_to_dwc(g); 3148 unsigned long flags; 3149 3150 spin_lock_irqsave(&dwc->lock, flags); 3151 dwc->gadget_max_speed = USB_SPEED_SUPER_PLUS; 3152 dwc->gadget_ssp_rate = rate; 3153 spin_unlock_irqrestore(&dwc->lock, flags); 3154 } 3155 3156 static int dwc3_gadget_vbus_draw(struct usb_gadget *g, unsigned int mA) 3157 { 3158 struct dwc3 *dwc = gadget_to_dwc(g); 3159 unsigned long flags; 3160 3161 if (dwc->usb2_phy) 3162 return usb_phy_set_power(dwc->usb2_phy, mA); 3163 3164 spin_lock_irqsave(&dwc->lock, flags); 3165 if (!dwc->usb_psy) { 3166 if (!dwc->psy_nb.notifier_call) { 3167 spin_unlock_irqrestore(&dwc->lock, flags); 3168 return -EOPNOTSUPP; 3169 } 3170 dwc->current_limit = mA; 3171 spin_unlock_irqrestore(&dwc->lock, flags); 3172 dev_dbg(dwc->dev, "Stored VBUS draw: %u mA (power supply not ready)\n", mA); 3173 return 0; 3174 } 3175 3176 dwc->current_limit = mA; 3177 schedule_work(&dwc->vbus_draw_work); 3178 spin_unlock_irqrestore(&dwc->lock, flags); 3179 3180 return 0; 3181 } 3182 3183 /** 3184 * dwc3_gadget_check_config - ensure dwc3 can support the USB configuration 3185 * @g: pointer to the USB gadget 3186 * 3187 * Used to record the maximum number of endpoints being used in a USB composite 3188 * device. (across all configurations) This is to be used in the calculation 3189 * of the TXFIFO sizes when resizing internal memory for individual endpoints. 3190 * It will help ensured that the resizing logic reserves enough space for at 3191 * least one max packet. 3192 */ 3193 static int dwc3_gadget_check_config(struct usb_gadget *g) 3194 { 3195 struct dwc3 *dwc = gadget_to_dwc(g); 3196 struct usb_ep *ep; 3197 int fifo_size = 0; 3198 int ram_depth; 3199 int ep_num = 0; 3200 3201 if (!dwc->do_fifo_resize) 3202 return 0; 3203 3204 list_for_each_entry(ep, &g->ep_list, ep_list) { 3205 /* Only interested in the IN endpoints */ 3206 if (ep->claimed && (ep->address & USB_DIR_IN)) 3207 ep_num++; 3208 } 3209 3210 if (ep_num <= dwc->max_cfg_eps) 3211 return 0; 3212 3213 /* Update the max number of eps in the composition */ 3214 dwc->max_cfg_eps = ep_num; 3215 3216 fifo_size = dwc3_gadget_calc_tx_fifo_size(dwc, dwc->max_cfg_eps); 3217 /* Based on the equation, increment by one for every ep */ 3218 fifo_size += dwc->max_cfg_eps; 3219 3220 /* Check if we can fit a single fifo per endpoint */ 3221 ram_depth = dwc3_gadget_calc_ram_depth(dwc); 3222 if (fifo_size > ram_depth) 3223 return -ENOMEM; 3224 3225 return 0; 3226 } 3227 3228 static void dwc3_gadget_async_callbacks(struct usb_gadget *g, bool enable) 3229 { 3230 struct dwc3 *dwc = gadget_to_dwc(g); 3231 unsigned long flags; 3232 3233 spin_lock_irqsave(&dwc->lock, flags); 3234 dwc->async_callbacks = enable; 3235 spin_unlock_irqrestore(&dwc->lock, flags); 3236 } 3237 3238 static const struct usb_gadget_ops dwc3_gadget_ops = { 3239 .get_frame = dwc3_gadget_get_frame, 3240 .wakeup = dwc3_gadget_wakeup, 3241 .func_wakeup = dwc3_gadget_func_wakeup, 3242 .set_remote_wakeup = dwc3_gadget_set_remote_wakeup, 3243 .set_selfpowered = dwc3_gadget_set_selfpowered, 3244 .pullup = dwc3_gadget_pullup, 3245 .udc_start = dwc3_gadget_start, 3246 .udc_stop = dwc3_gadget_stop, 3247 .udc_set_speed = dwc3_gadget_set_speed, 3248 .udc_set_ssp_rate = dwc3_gadget_set_ssp_rate, 3249 .get_config_params = dwc3_gadget_config_params, 3250 .vbus_draw = dwc3_gadget_vbus_draw, 3251 .check_config = dwc3_gadget_check_config, 3252 .udc_async_callbacks = dwc3_gadget_async_callbacks, 3253 }; 3254 3255 /* -------------------------------------------------------------------------- */ 3256 3257 static int dwc3_gadget_init_control_endpoint(struct dwc3_ep *dep) 3258 { 3259 struct dwc3 *dwc = dep->dwc; 3260 3261 usb_ep_set_maxpacket_limit(&dep->endpoint, 512); 3262 dep->endpoint.maxburst = 1; 3263 dep->endpoint.ops = &dwc3_gadget_ep0_ops; 3264 if (!dep->direction) 3265 dwc->gadget->ep0 = &dep->endpoint; 3266 3267 dep->endpoint.caps.type_control = true; 3268 3269 return 0; 3270 } 3271 3272 static int dwc3_gadget_init_in_endpoint(struct dwc3_ep *dep) 3273 { 3274 struct dwc3 *dwc = dep->dwc; 3275 u32 mdwidth; 3276 int size; 3277 int maxpacket; 3278 3279 mdwidth = dwc3_mdwidth(dwc); 3280 3281 /* MDWIDTH is represented in bits, we need it in bytes */ 3282 mdwidth /= 8; 3283 3284 size = dwc3_readl(dwc, DWC3_GTXFIFOSIZ(dep->number >> 1)); 3285 if (DWC3_IP_IS(DWC3)) 3286 size = DWC3_GTXFIFOSIZ_TXFDEP(size); 3287 else 3288 size = DWC31_GTXFIFOSIZ_TXFDEP(size); 3289 3290 /* 3291 * maxpacket size is determined as part of the following, after assuming 3292 * a mult value of one maxpacket: 3293 * DWC3 revision 280A and prior: 3294 * fifo_size = mult * (max_packet / mdwidth) + 1; 3295 * maxpacket = mdwidth * (fifo_size - 1); 3296 * 3297 * DWC3 revision 290A and onwards: 3298 * fifo_size = mult * ((max_packet + mdwidth)/mdwidth + 1) + 1 3299 * maxpacket = mdwidth * ((fifo_size - 1) - 1) - mdwidth; 3300 */ 3301 if (DWC3_VER_IS_PRIOR(DWC3, 290A)) 3302 maxpacket = mdwidth * (size - 1); 3303 else 3304 maxpacket = mdwidth * ((size - 1) - 1) - mdwidth; 3305 3306 /* Functionally, space for one max packet is sufficient */ 3307 size = min_t(int, maxpacket, 1024); 3308 usb_ep_set_maxpacket_limit(&dep->endpoint, size); 3309 3310 dep->endpoint.max_streams = 16; 3311 dep->endpoint.ops = &dwc3_gadget_ep_ops; 3312 list_add_tail(&dep->endpoint.ep_list, 3313 &dwc->gadget->ep_list); 3314 dep->endpoint.caps.type_iso = true; 3315 dep->endpoint.caps.type_bulk = true; 3316 dep->endpoint.caps.type_int = true; 3317 3318 return dwc3_alloc_trb_pool(dep); 3319 } 3320 3321 static int dwc3_gadget_init_out_endpoint(struct dwc3_ep *dep) 3322 { 3323 struct dwc3 *dwc = dep->dwc; 3324 u32 mdwidth; 3325 int size; 3326 3327 mdwidth = dwc3_mdwidth(dwc); 3328 3329 /* MDWIDTH is represented in bits, convert to bytes */ 3330 mdwidth /= 8; 3331 3332 /* All OUT endpoints share a single RxFIFO space */ 3333 size = dwc3_readl(dwc, DWC3_GRXFIFOSIZ(0)); 3334 if (DWC3_IP_IS(DWC3)) 3335 size = DWC3_GRXFIFOSIZ_RXFDEP(size); 3336 else 3337 size = DWC31_GRXFIFOSIZ_RXFDEP(size); 3338 3339 /* FIFO depth is in MDWDITH bytes */ 3340 size *= mdwidth; 3341 3342 /* 3343 * To meet performance requirement, a minimum recommended RxFIFO size 3344 * is defined as follow: 3345 * RxFIFO size >= (3 x MaxPacketSize) + 3346 * (3 x 8 bytes setup packets size) + (16 bytes clock crossing margin) 3347 * 3348 * Then calculate the max packet limit as below. 3349 */ 3350 size -= (3 * 8) + 16; 3351 if (size < 0) 3352 size = 0; 3353 else 3354 size /= 3; 3355 3356 usb_ep_set_maxpacket_limit(&dep->endpoint, size); 3357 dep->endpoint.max_streams = 16; 3358 dep->endpoint.ops = &dwc3_gadget_ep_ops; 3359 list_add_tail(&dep->endpoint.ep_list, 3360 &dwc->gadget->ep_list); 3361 dep->endpoint.caps.type_iso = true; 3362 dep->endpoint.caps.type_bulk = true; 3363 dep->endpoint.caps.type_int = true; 3364 3365 return dwc3_alloc_trb_pool(dep); 3366 } 3367 3368 #define nostream_work_to_dep(w) (container_of(to_delayed_work(w), struct dwc3_ep, nostream_work)) 3369 static void dwc3_nostream_work(struct work_struct *work) 3370 { 3371 struct dwc3_ep *dep = nostream_work_to_dep(work); 3372 struct dwc3 *dwc = dep->dwc; 3373 unsigned long flags; 3374 3375 spin_lock_irqsave(&dwc->lock, flags); 3376 if (dep->flags & DWC3_EP_STREAM_PRIMED) 3377 goto out; 3378 3379 if ((dep->flags & DWC3_EP_IGNORE_NEXT_NOSTREAM) || 3380 (!DWC3_MST_CAPABLE(&dwc->hwparams) && 3381 !(dep->flags & DWC3_EP_WAIT_TRANSFER_COMPLETE))) 3382 goto out; 3383 /* 3384 * If the host rejects a stream due to no active stream, by the 3385 * USB and xHCI spec, the endpoint will be put back to idle 3386 * state. When the host is ready (buffer added/updated), it will 3387 * prime the endpoint to inform the usb device controller. This 3388 * triggers the device controller to issue ERDY to restart the 3389 * stream. However, some hosts don't follow this and keep the 3390 * endpoint in the idle state. No prime will come despite host 3391 * streams are updated, and the device controller will not be 3392 * triggered to generate ERDY to move the next stream data. To 3393 * workaround this and maintain compatibility with various 3394 * hosts, force to reinitiate the stream until the host is ready 3395 * instead of waiting for the host to prime the endpoint. 3396 */ 3397 if (DWC3_VER_IS_WITHIN(DWC32, 100A, ANY)) { 3398 unsigned int cmd = DWC3_DGCMD_SET_ENDPOINT_PRIME; 3399 3400 dwc3_send_gadget_generic_command(dwc, cmd, dep->number); 3401 } else { 3402 dep->flags |= DWC3_EP_DELAY_START; 3403 dwc3_stop_active_transfer(dep, false, true); 3404 spin_unlock_irqrestore(&dwc->lock, flags); 3405 return; 3406 } 3407 out: 3408 dep->flags &= ~DWC3_EP_IGNORE_NEXT_NOSTREAM; 3409 spin_unlock_irqrestore(&dwc->lock, flags); 3410 } 3411 3412 static int dwc3_gadget_init_endpoint(struct dwc3 *dwc, u8 epnum) 3413 { 3414 struct dwc3_ep *dep; 3415 bool direction = epnum & 1; 3416 int ret; 3417 u8 num = epnum >> 1; 3418 3419 dep = kzalloc_obj(*dep); 3420 if (!dep) 3421 return -ENOMEM; 3422 3423 dep->dwc = dwc; 3424 dep->number = epnum; 3425 dep->direction = direction; 3426 dwc->eps[epnum] = dep; 3427 dep->combo_num = 0; 3428 dep->start_cmd_status = 0; 3429 3430 snprintf(dep->name, sizeof(dep->name), "ep%u%s", num, 3431 direction ? "in" : "out"); 3432 3433 dep->endpoint.name = dep->name; 3434 3435 if (!(dep->number > 1)) { 3436 dep->endpoint.desc = &dwc3_gadget_ep0_desc; 3437 dep->endpoint.comp_desc = NULL; 3438 } 3439 3440 if (num == 0) 3441 ret = dwc3_gadget_init_control_endpoint(dep); 3442 else if (direction) 3443 ret = dwc3_gadget_init_in_endpoint(dep); 3444 else 3445 ret = dwc3_gadget_init_out_endpoint(dep); 3446 3447 if (ret) 3448 return ret; 3449 3450 dep->endpoint.caps.dir_in = direction; 3451 dep->endpoint.caps.dir_out = !direction; 3452 3453 INIT_LIST_HEAD(&dep->pending_list); 3454 INIT_LIST_HEAD(&dep->started_list); 3455 INIT_LIST_HEAD(&dep->cancelled_list); 3456 INIT_DELAYED_WORK(&dep->nostream_work, dwc3_nostream_work); 3457 3458 dwc3_debugfs_create_endpoint_dir(dep); 3459 3460 return 0; 3461 } 3462 3463 static int dwc3_gadget_get_reserved_endpoints(struct dwc3 *dwc, const char *propname, 3464 u8 *eps, u8 num) 3465 { 3466 u8 count; 3467 int ret; 3468 3469 if (!device_property_present(dwc->dev, propname)) 3470 return 0; 3471 3472 ret = device_property_count_u8(dwc->dev, propname); 3473 if (ret < 0) 3474 return ret; 3475 count = ret; 3476 3477 ret = device_property_read_u8_array(dwc->dev, propname, eps, min(num, count)); 3478 if (ret) 3479 return ret; 3480 3481 return count; 3482 } 3483 3484 static int dwc3_gadget_init_endpoints(struct dwc3 *dwc, u8 total) 3485 { 3486 const char *propname = "snps,reserved-endpoints"; 3487 u8 epnum; 3488 u8 reserved_eps[DWC3_ENDPOINTS_NUM]; 3489 u8 count; 3490 u8 num; 3491 int ret; 3492 3493 INIT_LIST_HEAD(&dwc->gadget->ep_list); 3494 3495 ret = dwc3_gadget_get_reserved_endpoints(dwc, propname, 3496 reserved_eps, ARRAY_SIZE(reserved_eps)); 3497 if (ret < 0) { 3498 dev_err(dwc->dev, "failed to read %s\n", propname); 3499 return ret; 3500 } 3501 count = ret; 3502 3503 for (epnum = 0; epnum < total; epnum++) { 3504 for (num = 0; num < count; num++) { 3505 if (epnum == reserved_eps[num]) 3506 break; 3507 } 3508 if (num < count) 3509 continue; 3510 3511 ret = dwc3_gadget_init_endpoint(dwc, epnum); 3512 if (ret) 3513 return ret; 3514 } 3515 3516 return 0; 3517 } 3518 3519 static void dwc3_gadget_free_endpoints(struct dwc3 *dwc) 3520 { 3521 struct dwc3_ep *dep; 3522 u8 epnum; 3523 3524 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) { 3525 dep = dwc->eps[epnum]; 3526 if (!dep) 3527 continue; 3528 /* 3529 * Physical endpoints 0 and 1 are special; they form the 3530 * bi-directional USB endpoint 0. 3531 * 3532 * For those two physical endpoints, we don't allocate a TRB 3533 * pool nor do we add them the endpoints list. Due to that, we 3534 * shouldn't do these two operations otherwise we would end up 3535 * with all sorts of bugs when removing dwc3.ko. 3536 */ 3537 if (epnum != 0 && epnum != 1) { 3538 dwc3_free_trb_pool(dep); 3539 list_del(&dep->endpoint.ep_list); 3540 } 3541 3542 dwc3_debugfs_remove_endpoint_dir(dep); 3543 cancel_delayed_work_sync(&dep->nostream_work); 3544 kfree(dep); 3545 } 3546 } 3547 3548 /* -------------------------------------------------------------------------- */ 3549 3550 static int dwc3_gadget_ep_reclaim_completed_trb(struct dwc3_ep *dep, 3551 struct dwc3_request *req, struct dwc3_trb *trb, 3552 const struct dwc3_event_depevt *event, int status) 3553 { 3554 unsigned int count; 3555 3556 dwc3_ep_inc_deq(dep); 3557 3558 trace_dwc3_complete_trb(dep, trb); 3559 req->num_trbs--; 3560 3561 /* 3562 * If we're in the middle of series of chained TRBs and we 3563 * receive a short transfer along the way, DWC3 will skip 3564 * through all TRBs including the last TRB in the chain (the 3565 * where CHN bit is zero. DWC3 will also avoid clearing HWO 3566 * bit and SW has to do it manually. 3567 * 3568 * We're going to do that here to avoid problems of HW trying 3569 * to use bogus TRBs for transfers. 3570 */ 3571 if (trb->ctrl & DWC3_TRB_CTRL_HWO) 3572 trb->ctrl &= ~DWC3_TRB_CTRL_HWO; 3573 3574 /* 3575 * For isochronous transfers, the first TRB in a service interval must 3576 * have the Isoc-First type. Track and report its interval frame number. 3577 */ 3578 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) && 3579 (trb->ctrl & DWC3_TRBCTL_ISOCHRONOUS_FIRST)) { 3580 unsigned int frame_number; 3581 3582 frame_number = DWC3_TRB_CTRL_GET_SID_SOFN(trb->ctrl); 3583 frame_number &= ~(dep->interval - 1); 3584 req->request.frame_number = frame_number; 3585 } 3586 3587 /* 3588 * We use bounce buffer for requests that needs extra TRB or OUT ZLP. If 3589 * this TRB points to the bounce buffer address, it's a MPS alignment 3590 * TRB. Don't add it to req->remaining calculation. 3591 */ 3592 if (trb->bpl == lower_32_bits(dep->dwc->bounce_addr) && 3593 trb->bph == upper_32_bits(dep->dwc->bounce_addr)) { 3594 trb->ctrl &= ~DWC3_TRB_CTRL_HWO; 3595 return 1; 3596 } 3597 3598 count = trb->size & DWC3_TRB_SIZE_MASK; 3599 req->remaining += count; 3600 3601 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN) 3602 return 1; 3603 3604 if (event->status & DEPEVT_STATUS_SHORT && 3605 !(trb->ctrl & DWC3_TRB_CTRL_CHN)) 3606 return 1; 3607 3608 if ((trb->ctrl & DWC3_TRB_CTRL_ISP_IMI) && 3609 DWC3_TRB_SIZE_TRBSTS(trb->size) == DWC3_TRBSTS_MISSED_ISOC) 3610 return 1; 3611 3612 if ((trb->ctrl & DWC3_TRB_CTRL_IOC) || 3613 (trb->ctrl & DWC3_TRB_CTRL_LST)) 3614 return 1; 3615 3616 return 0; 3617 } 3618 3619 static int dwc3_gadget_ep_reclaim_trb_sg(struct dwc3_ep *dep, 3620 struct dwc3_request *req, const struct dwc3_event_depevt *event, 3621 int status) 3622 { 3623 struct dwc3_trb *trb; 3624 unsigned int num_completed_trbs = req->num_trbs; 3625 unsigned int i; 3626 int ret = 0; 3627 3628 for (i = 0; i < num_completed_trbs; i++) { 3629 trb = &dep->trb_pool[dep->trb_dequeue]; 3630 3631 ret = dwc3_gadget_ep_reclaim_completed_trb(dep, req, 3632 trb, event, status); 3633 if (ret) 3634 break; 3635 } 3636 3637 return ret; 3638 } 3639 3640 static bool dwc3_gadget_ep_request_completed(struct dwc3_request *req) 3641 { 3642 return req->num_pending_sgs == 0 && req->num_trbs == 0; 3643 } 3644 3645 static int dwc3_gadget_ep_cleanup_completed_request(struct dwc3_ep *dep, 3646 const struct dwc3_event_depevt *event, 3647 struct dwc3_request *req, int status) 3648 { 3649 int request_status; 3650 int ret; 3651 3652 ret = dwc3_gadget_ep_reclaim_trb_sg(dep, req, event, status); 3653 3654 req->request.actual = req->request.length - req->remaining; 3655 3656 if (!dwc3_gadget_ep_request_completed(req)) 3657 goto out; 3658 3659 /* 3660 * The event status only reflects the status of the TRB with IOC set. 3661 * For the requests that don't set interrupt on completion, the driver 3662 * needs to check and return the status of the completed TRBs associated 3663 * with the request. Use the status of the last TRB of the request. 3664 */ 3665 if (req->request.no_interrupt) { 3666 struct dwc3_trb *trb; 3667 3668 trb = dwc3_ep_prev_trb(dep, dep->trb_dequeue); 3669 switch (DWC3_TRB_SIZE_TRBSTS(trb->size)) { 3670 case DWC3_TRBSTS_MISSED_ISOC: 3671 /* Isoc endpoint only */ 3672 request_status = -EXDEV; 3673 break; 3674 case DWC3_TRB_STS_XFER_IN_PROG: 3675 /* Applicable when End Transfer with ForceRM=0 */ 3676 case DWC3_TRBSTS_SETUP_PENDING: 3677 /* Control endpoint only */ 3678 case DWC3_TRBSTS_OK: 3679 default: 3680 request_status = 0; 3681 break; 3682 } 3683 } else { 3684 request_status = status; 3685 } 3686 3687 dwc3_gadget_giveback(dep, req, request_status); 3688 3689 out: 3690 return ret; 3691 } 3692 3693 static void dwc3_gadget_ep_cleanup_completed_requests(struct dwc3_ep *dep, 3694 const struct dwc3_event_depevt *event, int status) 3695 { 3696 struct dwc3_request *req; 3697 3698 while (!list_empty(&dep->started_list)) { 3699 int ret; 3700 3701 req = next_request(&dep->started_list); 3702 ret = dwc3_gadget_ep_cleanup_completed_request(dep, event, 3703 req, status); 3704 if (ret) 3705 break; 3706 /* 3707 * The endpoint is disabled, let the dwc3_remove_requests() 3708 * handle the cleanup. 3709 */ 3710 if (!dep->endpoint.desc) 3711 break; 3712 } 3713 } 3714 3715 static bool dwc3_gadget_ep_should_continue(struct dwc3_ep *dep) 3716 { 3717 struct dwc3_request *req; 3718 struct dwc3 *dwc = dep->dwc; 3719 3720 if (!dep->endpoint.desc || !dwc->pullups_connected || 3721 !dwc->connected) 3722 return false; 3723 3724 if (!list_empty(&dep->pending_list)) 3725 return true; 3726 3727 /* 3728 * We only need to check the first entry of the started list. We can 3729 * assume the completed requests are removed from the started list. 3730 */ 3731 req = next_request(&dep->started_list); 3732 if (!req) 3733 return false; 3734 3735 return !dwc3_gadget_ep_request_completed(req); 3736 } 3737 3738 static void dwc3_gadget_endpoint_frame_from_event(struct dwc3_ep *dep, 3739 const struct dwc3_event_depevt *event) 3740 { 3741 dep->frame_number = event->parameters; 3742 } 3743 3744 static bool dwc3_gadget_endpoint_trbs_complete(struct dwc3_ep *dep, 3745 const struct dwc3_event_depevt *event, int status) 3746 { 3747 struct dwc3 *dwc = dep->dwc; 3748 bool no_started_trb = true; 3749 3750 dwc3_gadget_ep_cleanup_completed_requests(dep, event, status); 3751 3752 if (dep->flags & DWC3_EP_END_TRANSFER_PENDING) 3753 goto out; 3754 3755 if (!dep->endpoint.desc) 3756 return no_started_trb; 3757 3758 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) && 3759 list_empty(&dep->started_list) && 3760 (list_empty(&dep->pending_list) || status == -EXDEV)) 3761 dwc3_stop_active_transfer(dep, false, true); 3762 else if (dwc3_gadget_ep_should_continue(dep)) 3763 if (__dwc3_gadget_kick_transfer(dep) == 0) 3764 no_started_trb = false; 3765 3766 out: 3767 /* 3768 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround. 3769 * See dwc3_gadget_linksts_change_interrupt() for 1st half. 3770 */ 3771 if (DWC3_VER_IS_PRIOR(DWC3, 183A)) { 3772 u32 reg; 3773 int i; 3774 3775 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) { 3776 dep = dwc->eps[i]; 3777 if (!dep) 3778 continue; 3779 3780 if (!(dep->flags & DWC3_EP_ENABLED)) 3781 continue; 3782 3783 if (!list_empty(&dep->started_list)) 3784 return no_started_trb; 3785 } 3786 3787 reg = dwc3_readl(dwc, DWC3_DCTL); 3788 reg |= dwc->u1u2; 3789 dwc3_writel(dwc, DWC3_DCTL, reg); 3790 3791 dwc->u1u2 = 0; 3792 } 3793 3794 return no_started_trb; 3795 } 3796 3797 static void dwc3_gadget_endpoint_transfer_in_progress(struct dwc3_ep *dep, 3798 const struct dwc3_event_depevt *event) 3799 { 3800 int status = 0; 3801 3802 if (!dep->endpoint.desc) 3803 return; 3804 3805 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) 3806 dwc3_gadget_endpoint_frame_from_event(dep, event); 3807 3808 if (event->status & DEPEVT_STATUS_BUSERR) 3809 status = -ECONNRESET; 3810 3811 if (event->status & DEPEVT_STATUS_MISSED_ISOC) 3812 status = -EXDEV; 3813 3814 dwc3_gadget_endpoint_trbs_complete(dep, event, status); 3815 } 3816 3817 static void dwc3_gadget_endpoint_transfer_complete(struct dwc3_ep *dep, 3818 const struct dwc3_event_depevt *event) 3819 { 3820 int status = 0; 3821 3822 dep->flags &= ~DWC3_EP_TRANSFER_STARTED; 3823 3824 if (event->status & DEPEVT_STATUS_BUSERR) 3825 status = -ECONNRESET; 3826 3827 if (dwc3_gadget_endpoint_trbs_complete(dep, event, status)) 3828 dep->flags &= ~DWC3_EP_WAIT_TRANSFER_COMPLETE; 3829 } 3830 3831 static void dwc3_gadget_endpoint_transfer_not_ready(struct dwc3_ep *dep, 3832 const struct dwc3_event_depevt *event) 3833 { 3834 /* 3835 * During a device-initiated disconnect, a late xferNotReady event can 3836 * be generated after the End Transfer command resets the event filter, 3837 * but before the controller is halted. Ignore it to prevent a new 3838 * transfer from starting. 3839 */ 3840 if (!dep->dwc->connected) 3841 return; 3842 3843 dwc3_gadget_endpoint_frame_from_event(dep, event); 3844 3845 /* 3846 * The XferNotReady event is generated only once before the endpoint 3847 * starts. It will be generated again when END_TRANSFER command is 3848 * issued. For some controller versions, the XferNotReady event may be 3849 * generated while the END_TRANSFER command is still in process. Ignore 3850 * it and wait for the next XferNotReady event after the command is 3851 * completed. 3852 */ 3853 if (dep->flags & DWC3_EP_END_TRANSFER_PENDING) 3854 return; 3855 3856 (void) __dwc3_gadget_start_isoc(dep); 3857 } 3858 3859 static void dwc3_gadget_endpoint_command_complete(struct dwc3_ep *dep, 3860 const struct dwc3_event_depevt *event) 3861 { 3862 u8 cmd = DEPEVT_PARAMETER_CMD(event->parameters); 3863 3864 if (cmd != DWC3_DEPCMD_ENDTRANSFER) 3865 return; 3866 3867 /* 3868 * The END_TRANSFER command will cause the controller to generate a 3869 * NoStream Event, and it's not due to the host DP NoStream rejection. 3870 * Ignore the next NoStream event. 3871 */ 3872 if (dep->stream_capable) 3873 dep->flags |= DWC3_EP_IGNORE_NEXT_NOSTREAM; 3874 3875 dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING; 3876 dep->flags &= ~DWC3_EP_TRANSFER_STARTED; 3877 dwc3_gadget_ep_cleanup_cancelled_requests(dep); 3878 3879 if (dep->flags & DWC3_EP_PENDING_CLEAR_STALL) { 3880 struct dwc3 *dwc = dep->dwc; 3881 3882 dep->flags &= ~DWC3_EP_PENDING_CLEAR_STALL; 3883 if (dwc3_send_clear_stall_ep_cmd(dep)) { 3884 struct usb_ep *ep0 = &dwc->eps[0]->endpoint; 3885 3886 dev_err(dwc->dev, "failed to clear STALL on %s\n", dep->name); 3887 if (dwc->delayed_status) 3888 __dwc3_gadget_ep0_set_halt(ep0, 1); 3889 return; 3890 } 3891 3892 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE); 3893 if (dwc->clear_stall_protocol == dep->number) 3894 dwc3_ep0_send_delayed_status(dwc); 3895 } 3896 3897 if ((dep->flags & DWC3_EP_DELAY_START) && 3898 !usb_endpoint_xfer_isoc(dep->endpoint.desc)) 3899 __dwc3_gadget_kick_transfer(dep); 3900 3901 dep->flags &= ~DWC3_EP_DELAY_START; 3902 } 3903 3904 static void dwc3_gadget_endpoint_stream_event(struct dwc3_ep *dep, 3905 const struct dwc3_event_depevt *event) 3906 { 3907 if (event->status == DEPEVT_STREAMEVT_FOUND) { 3908 cancel_delayed_work(&dep->nostream_work); 3909 dep->flags |= DWC3_EP_STREAM_PRIMED; 3910 dep->flags &= ~DWC3_EP_IGNORE_NEXT_NOSTREAM; 3911 return; 3912 } 3913 3914 /* Note: NoStream rejection event param value is 0 and not 0xFFFF */ 3915 switch (event->parameters) { 3916 case DEPEVT_STREAM_PRIME: 3917 cancel_delayed_work(&dep->nostream_work); 3918 dep->flags |= DWC3_EP_STREAM_PRIMED; 3919 dep->flags &= ~DWC3_EP_IGNORE_NEXT_NOSTREAM; 3920 break; 3921 case DEPEVT_STREAM_NOSTREAM: 3922 dep->flags &= ~DWC3_EP_STREAM_PRIMED; 3923 if (dep->flags & DWC3_EP_FORCE_RESTART_STREAM) 3924 queue_delayed_work(system_percpu_wq, &dep->nostream_work, 3925 msecs_to_jiffies(100)); 3926 break; 3927 } 3928 } 3929 3930 static void dwc3_endpoint_interrupt(struct dwc3 *dwc, 3931 const struct dwc3_event_depevt *event) 3932 { 3933 struct dwc3_ep *dep; 3934 u8 epnum = event->endpoint_number; 3935 3936 dep = dwc->eps[epnum]; 3937 if (!dep) { 3938 dev_warn(dwc->dev, "spurious event, endpoint %u is not allocated\n", epnum); 3939 return; 3940 } 3941 3942 if (!(dep->flags & DWC3_EP_ENABLED)) { 3943 if ((epnum > 1) && !(dep->flags & DWC3_EP_TRANSFER_STARTED)) 3944 return; 3945 3946 /* Handle only EPCMDCMPLT when EP disabled */ 3947 if ((event->endpoint_event != DWC3_DEPEVT_EPCMDCMPLT) && 3948 !(epnum <= 1 && event->endpoint_event == DWC3_DEPEVT_XFERCOMPLETE)) 3949 return; 3950 } 3951 3952 if (epnum == 0 || epnum == 1) { 3953 dwc3_ep0_interrupt(dwc, event); 3954 return; 3955 } 3956 3957 switch (event->endpoint_event) { 3958 case DWC3_DEPEVT_XFERINPROGRESS: 3959 dwc3_gadget_endpoint_transfer_in_progress(dep, event); 3960 break; 3961 case DWC3_DEPEVT_XFERNOTREADY: 3962 dwc3_gadget_endpoint_transfer_not_ready(dep, event); 3963 break; 3964 case DWC3_DEPEVT_EPCMDCMPLT: 3965 dwc3_gadget_endpoint_command_complete(dep, event); 3966 break; 3967 case DWC3_DEPEVT_XFERCOMPLETE: 3968 dwc3_gadget_endpoint_transfer_complete(dep, event); 3969 break; 3970 case DWC3_DEPEVT_STREAMEVT: 3971 dwc3_gadget_endpoint_stream_event(dep, event); 3972 break; 3973 case DWC3_DEPEVT_RXTXFIFOEVT: 3974 break; 3975 default: 3976 dev_err(dwc->dev, "unknown endpoint event %d\n", event->endpoint_event); 3977 break; 3978 } 3979 } 3980 3981 static bool dwc3_prepare_disconnect_gadget(struct dwc3 *dwc, 3982 struct usb_gadget_driver **driver, 3983 struct usb_gadget **gadget) 3984 { 3985 if (!dwc->async_callbacks || !dwc->gadget_driver || 3986 !dwc->gadget_driver->disconnect) 3987 return false; 3988 3989 *driver = dwc->gadget_driver; 3990 *gadget = dwc->gadget; 3991 3992 return true; 3993 } 3994 3995 static void dwc3_disconnect_gadget(struct dwc3 *dwc) 3996 { 3997 struct usb_gadget_driver *driver; 3998 struct usb_gadget *gadget; 3999 4000 if (dwc3_prepare_disconnect_gadget(dwc, &driver, &gadget)) { 4001 spin_unlock(&dwc->lock); 4002 driver->disconnect(gadget); 4003 spin_lock(&dwc->lock); 4004 } 4005 } 4006 4007 static void dwc3_disconnect_gadget_sleepable(struct dwc3 *dwc) 4008 { 4009 struct usb_gadget_driver *driver; 4010 struct usb_gadget *gadget; 4011 unsigned long flags; 4012 4013 spin_lock_irqsave(&dwc->lock, flags); 4014 if (!dwc3_prepare_disconnect_gadget(dwc, &driver, &gadget)) { 4015 spin_unlock_irqrestore(&dwc->lock, flags); 4016 return; 4017 } 4018 4019 spin_unlock_irqrestore(&dwc->lock, flags); 4020 driver->disconnect(gadget); 4021 } 4022 4023 static void dwc3_suspend_gadget(struct dwc3 *dwc) 4024 { 4025 if (dwc->async_callbacks && dwc->gadget_driver->suspend) { 4026 spin_unlock(&dwc->lock); 4027 dwc->gadget_driver->suspend(dwc->gadget); 4028 spin_lock(&dwc->lock); 4029 } 4030 } 4031 4032 static void dwc3_resume_gadget(struct dwc3 *dwc) 4033 { 4034 if (dwc->async_callbacks && dwc->gadget_driver->resume) { 4035 spin_unlock(&dwc->lock); 4036 dwc->gadget_driver->resume(dwc->gadget); 4037 spin_lock(&dwc->lock); 4038 } 4039 } 4040 4041 static void dwc3_reset_gadget(struct dwc3 *dwc) 4042 { 4043 if (!dwc->gadget_driver) 4044 return; 4045 4046 if (dwc->async_callbacks && dwc->gadget->speed != USB_SPEED_UNKNOWN) { 4047 spin_unlock(&dwc->lock); 4048 usb_gadget_udc_reset(dwc->gadget, dwc->gadget_driver); 4049 spin_lock(&dwc->lock); 4050 } 4051 } 4052 4053 void dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force, 4054 bool interrupt) 4055 { 4056 struct dwc3 *dwc = dep->dwc; 4057 4058 /* 4059 * Only issue End Transfer command to the control endpoint of a started 4060 * Data Phase. Typically we should only do so in error cases such as 4061 * invalid/unexpected direction as described in the control transfer 4062 * flow of the programming guide. 4063 */ 4064 if (dep->number <= 1 && dwc->ep0state != EP0_DATA_PHASE) 4065 return; 4066 4067 if (interrupt && (dep->flags & DWC3_EP_DELAY_STOP)) 4068 return; 4069 4070 if (!(dep->flags & DWC3_EP_TRANSFER_STARTED) || 4071 (dep->flags & DWC3_EP_END_TRANSFER_PENDING)) 4072 return; 4073 4074 /* 4075 * If a Setup packet is received but yet to DMA out, the controller will 4076 * not process the End Transfer command of any endpoint. Polling of its 4077 * DEPCMD.CmdAct may block setting up TRB for Setup packet, causing a 4078 * timeout. Delay issuing the End Transfer command until the Setup TRB is 4079 * prepared. 4080 */ 4081 if (dwc->ep0state != EP0_SETUP_PHASE && !dwc->delayed_status) { 4082 dep->flags |= DWC3_EP_DELAY_STOP; 4083 return; 4084 } 4085 4086 /* 4087 * NOTICE: We are violating what the Databook says about the 4088 * EndTransfer command. Ideally we would _always_ wait for the 4089 * EndTransfer Command Completion IRQ, but that's causing too 4090 * much trouble synchronizing between us and gadget driver. 4091 * 4092 * We have discussed this with the IP Provider and it was 4093 * suggested to giveback all requests here. 4094 * 4095 * Note also that a similar handling was tested by Synopsys 4096 * (thanks a lot Paul) and nothing bad has come out of it. 4097 * In short, what we're doing is issuing EndTransfer with 4098 * CMDIOC bit set and delay kicking transfer until the 4099 * EndTransfer command had completed. 4100 * 4101 * As of IP version 3.10a of the DWC_usb3 IP, the controller 4102 * supports a mode to work around the above limitation. The 4103 * software can poll the CMDACT bit in the DEPCMD register 4104 * after issuing a EndTransfer command. This mode is enabled 4105 * by writing GUCTL2[14]. This polling is already done in the 4106 * dwc3_send_gadget_ep_cmd() function so if the mode is 4107 * enabled, the EndTransfer command will have completed upon 4108 * returning from this function. 4109 * 4110 * This mode is NOT available on the DWC_usb31 IP. In this 4111 * case, if the IOC bit is not set, then delay by 1ms 4112 * after issuing the EndTransfer command. This allows for the 4113 * controller to handle the command completely before DWC3 4114 * remove requests attempts to unmap USB request buffers. 4115 */ 4116 4117 __dwc3_stop_active_transfer(dep, force, interrupt); 4118 } 4119 4120 static void dwc3_clear_stall_all_ep(struct dwc3 *dwc) 4121 { 4122 u32 epnum; 4123 4124 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) { 4125 struct dwc3_ep *dep; 4126 int ret; 4127 4128 dep = dwc->eps[epnum]; 4129 if (!dep) 4130 continue; 4131 4132 if (!(dep->flags & DWC3_EP_STALL)) 4133 continue; 4134 4135 dep->flags &= ~DWC3_EP_STALL; 4136 4137 ret = dwc3_send_clear_stall_ep_cmd(dep); 4138 if (ret) 4139 dev_err_ratelimited(dwc->dev, 4140 "failed to clear STALL on %s\n", dep->name); 4141 } 4142 } 4143 4144 static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc) 4145 { 4146 int reg; 4147 4148 dwc->suspended = false; 4149 4150 dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RX_DET); 4151 4152 reg = dwc3_readl(dwc, DWC3_DCTL); 4153 reg &= ~DWC3_DCTL_INITU1ENA; 4154 reg &= ~DWC3_DCTL_INITU2ENA; 4155 dwc3_gadget_dctl_write_safe(dwc, reg); 4156 4157 dwc->connected = false; 4158 4159 dwc3_disconnect_gadget(dwc); 4160 4161 dwc->gadget->speed = USB_SPEED_UNKNOWN; 4162 dwc->setup_packet_pending = false; 4163 dwc->gadget->wakeup_armed = false; 4164 dwc3_gadget_enable_linksts_evts(dwc, false); 4165 usb_gadget_set_state(dwc->gadget, USB_STATE_NOTATTACHED); 4166 4167 dwc3_ep0_reset_state(dwc); 4168 4169 /* 4170 * Request PM idle to address condition where usage count is 4171 * already decremented to zero, but waiting for the disconnect 4172 * interrupt to set dwc->connected to FALSE. 4173 */ 4174 pm_request_idle(dwc->dev); 4175 } 4176 4177 static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc) 4178 { 4179 u32 reg; 4180 4181 dwc->suspended = false; 4182 4183 /* The controller is recovered. */ 4184 if (dwc->err_state == DWC3_ERR_NONE) 4185 dwc->err_recovery_count = 0; 4186 4187 /* 4188 * Ideally, dwc3_reset_gadget() would trigger the function 4189 * drivers to stop any active transfers through ep disable. 4190 * However, for functions which defer ep disable, such as mass 4191 * storage, we will need to rely on the call to stop active 4192 * transfers here, and avoid allowing of request queuing. 4193 */ 4194 dwc->connected = false; 4195 4196 /* 4197 * WORKAROUND: DWC3 revisions <1.88a have an issue which 4198 * would cause a missing Disconnect Event if there's a 4199 * pending Setup Packet in the FIFO. 4200 * 4201 * There's no suggested workaround on the official Bug 4202 * report, which states that "unless the driver/application 4203 * is doing any special handling of a disconnect event, 4204 * there is no functional issue". 4205 * 4206 * Unfortunately, it turns out that we _do_ some special 4207 * handling of a disconnect event, namely complete all 4208 * pending transfers, notify gadget driver of the 4209 * disconnection, and so on. 4210 * 4211 * Our suggested workaround is to follow the Disconnect 4212 * Event steps here, instead, based on a setup_packet_pending 4213 * flag. Such flag gets set whenever we have a SETUP_PENDING 4214 * status for EP0 TRBs and gets cleared on XferComplete for the 4215 * same endpoint. 4216 * 4217 * Refers to: 4218 * 4219 * STAR#9000466709: RTL: Device : Disconnect event not 4220 * generated if setup packet pending in FIFO 4221 */ 4222 if (DWC3_VER_IS_PRIOR(DWC3, 188A)) { 4223 if (dwc->setup_packet_pending) 4224 dwc3_gadget_disconnect_interrupt(dwc); 4225 } 4226 4227 dwc3_reset_gadget(dwc); 4228 4229 /* 4230 * From SNPS databook section 8.1.2, the EP0 should be in setup 4231 * phase. So ensure that EP0 is in setup phase by issuing a stall 4232 * and restart if EP0 is not in setup phase. 4233 */ 4234 dwc3_ep0_reset_state(dwc); 4235 4236 /* 4237 * In the Synopsis DesignWare Cores USB3 Databook Rev. 3.30a 4238 * Section 4.1.2 Table 4-2, it states that during a USB reset, the SW 4239 * needs to ensure that it sends "a DEPENDXFER command for any active 4240 * transfers." 4241 */ 4242 dwc3_stop_active_transfers(dwc); 4243 dwc->connected = true; 4244 4245 reg = dwc3_readl(dwc, DWC3_DCTL); 4246 reg &= ~DWC3_DCTL_TSTCTRL_MASK; 4247 dwc3_gadget_dctl_write_safe(dwc, reg); 4248 dwc->test_mode = false; 4249 dwc->gadget->wakeup_armed = false; 4250 dwc3_gadget_enable_linksts_evts(dwc, false); 4251 dwc3_clear_stall_all_ep(dwc); 4252 4253 /* Reset device address to zero */ 4254 reg = dwc3_readl(dwc, DWC3_DCFG); 4255 reg &= ~(DWC3_DCFG_DEVADDR_MASK); 4256 dwc3_writel(dwc, DWC3_DCFG, reg); 4257 } 4258 4259 static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc) 4260 { 4261 struct dwc3_ep *dep; 4262 int ret; 4263 u32 reg; 4264 u8 lanes = 1; 4265 u8 speed; 4266 4267 if (!dwc->softconnect) 4268 return; 4269 4270 reg = dwc3_readl(dwc, DWC3_DSTS); 4271 speed = reg & DWC3_DSTS_CONNECTSPD; 4272 dwc->speed = speed; 4273 4274 if (DWC3_IP_IS(DWC32)) 4275 lanes = DWC3_DSTS_CONNLANES(reg) + 1; 4276 4277 dwc->gadget->ssp_rate = USB_SSP_GEN_UNKNOWN; 4278 4279 /* 4280 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed 4281 * each time on Connect Done. 4282 * 4283 * Currently we always use the reset value. If any platform 4284 * wants to set this to a different value, we need to add a 4285 * setting and update GCTL.RAMCLKSEL here. 4286 */ 4287 4288 switch (speed) { 4289 case DWC3_DSTS_SUPERSPEED_PLUS: 4290 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512); 4291 dwc->gadget->ep0->maxpacket = 512; 4292 dwc->gadget->speed = USB_SPEED_SUPER_PLUS; 4293 4294 if (lanes > 1) 4295 dwc->gadget->ssp_rate = USB_SSP_GEN_2x2; 4296 else 4297 dwc->gadget->ssp_rate = USB_SSP_GEN_2x1; 4298 break; 4299 case DWC3_DSTS_SUPERSPEED: 4300 /* 4301 * WORKAROUND: DWC3 revisions <1.90a have an issue which 4302 * would cause a missing USB3 Reset event. 4303 * 4304 * In such situations, we should force a USB3 Reset 4305 * event by calling our dwc3_gadget_reset_interrupt() 4306 * routine. 4307 * 4308 * Refers to: 4309 * 4310 * STAR#9000483510: RTL: SS : USB3 reset event may 4311 * not be generated always when the link enters poll 4312 */ 4313 if (DWC3_VER_IS_PRIOR(DWC3, 190A)) 4314 dwc3_gadget_reset_interrupt(dwc); 4315 4316 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512); 4317 dwc->gadget->ep0->maxpacket = 512; 4318 dwc->gadget->speed = USB_SPEED_SUPER; 4319 4320 if (lanes > 1) { 4321 dwc->gadget->speed = USB_SPEED_SUPER_PLUS; 4322 dwc->gadget->ssp_rate = USB_SSP_GEN_1x2; 4323 } 4324 break; 4325 case DWC3_DSTS_HIGHSPEED: 4326 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64); 4327 dwc->gadget->ep0->maxpacket = 64; 4328 dwc->gadget->speed = USB_SPEED_HIGH; 4329 break; 4330 case DWC3_DSTS_FULLSPEED: 4331 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64); 4332 dwc->gadget->ep0->maxpacket = 64; 4333 dwc->gadget->speed = USB_SPEED_FULL; 4334 break; 4335 } 4336 4337 dwc->eps[1]->endpoint.maxpacket = dwc->gadget->ep0->maxpacket; 4338 4339 /* Enable USB2 LPM Capability */ 4340 4341 if (!DWC3_VER_IS_WITHIN(DWC3, ANY, 194A) && 4342 !dwc->usb2_gadget_lpm_disable && 4343 (speed != DWC3_DSTS_SUPERSPEED) && 4344 (speed != DWC3_DSTS_SUPERSPEED_PLUS)) { 4345 reg = dwc3_readl(dwc, DWC3_DCFG); 4346 reg |= DWC3_DCFG_LPM_CAP; 4347 dwc3_writel(dwc, DWC3_DCFG, reg); 4348 4349 reg = dwc3_readl(dwc, DWC3_DCTL); 4350 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN); 4351 4352 reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold | 4353 (dwc->is_utmi_l1_suspend << 4)); 4354 4355 /* 4356 * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and 4357 * DCFG.LPMCap is set, core responses with an ACK and the 4358 * BESL value in the LPM token is less than or equal to LPM 4359 * NYET threshold. 4360 */ 4361 WARN_ONCE(DWC3_VER_IS_PRIOR(DWC3, 240A) && dwc->has_lpm_erratum, 4362 "LPM Erratum not available on dwc3 revisions < 2.40a\n"); 4363 4364 if (dwc->has_lpm_erratum && !DWC3_VER_IS_PRIOR(DWC3, 240A)) { 4365 reg &= ~DWC3_DCTL_NYET_THRES_MASK; 4366 reg |= DWC3_DCTL_NYET_THRES(dwc->lpm_nyet_threshold); 4367 } 4368 4369 dwc3_gadget_dctl_write_safe(dwc, reg); 4370 } else { 4371 if (dwc->usb2_gadget_lpm_disable) { 4372 reg = dwc3_readl(dwc, DWC3_DCFG); 4373 reg &= ~DWC3_DCFG_LPM_CAP; 4374 dwc3_writel(dwc, DWC3_DCFG, reg); 4375 } 4376 4377 reg = dwc3_readl(dwc, DWC3_DCTL); 4378 reg &= ~DWC3_DCTL_HIRD_THRES_MASK; 4379 dwc3_gadget_dctl_write_safe(dwc, reg); 4380 } 4381 4382 dep = dwc->eps[0]; 4383 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_MODIFY); 4384 if (ret) { 4385 dev_err(dwc->dev, "failed to enable %s\n", dep->name); 4386 return; 4387 } 4388 4389 dep = dwc->eps[1]; 4390 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_MODIFY); 4391 if (ret) { 4392 dev_err(dwc->dev, "failed to enable %s\n", dep->name); 4393 return; 4394 } 4395 4396 /* 4397 * Configure PHY via GUSB3PIPECTLn if required. 4398 * 4399 * Update GTXFIFOSIZn 4400 * 4401 * In both cases reset values should be sufficient. 4402 */ 4403 } 4404 4405 static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc, unsigned int evtinfo) 4406 { 4407 dwc->suspended = false; 4408 4409 /* 4410 * TODO take core out of low power mode when that's 4411 * implemented. 4412 */ 4413 4414 if (dwc->async_callbacks && dwc->gadget_driver->resume) { 4415 spin_unlock(&dwc->lock); 4416 dwc->gadget_driver->resume(dwc->gadget); 4417 spin_lock(&dwc->lock); 4418 } 4419 4420 dwc->link_state = evtinfo & DWC3_LINK_STATE_MASK; 4421 } 4422 4423 static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc, 4424 unsigned int evtinfo) 4425 { 4426 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK; 4427 unsigned int pwropt; 4428 int ret; 4429 int intf_id; 4430 4431 /* 4432 * WORKAROUND: DWC3 < 2.50a have an issue when configured without 4433 * Hibernation mode enabled which would show up when device detects 4434 * host-initiated U3 exit. 4435 * 4436 * In that case, device will generate a Link State Change Interrupt 4437 * from U3 to RESUME which is only necessary if Hibernation is 4438 * configured in. 4439 * 4440 * There are no functional changes due to such spurious event and we 4441 * just need to ignore it. 4442 * 4443 * Refers to: 4444 * 4445 * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation 4446 * operational mode 4447 */ 4448 pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1); 4449 if (DWC3_VER_IS_PRIOR(DWC3, 250A) && 4450 (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) { 4451 if ((dwc->link_state == DWC3_LINK_STATE_U3) && 4452 (next == DWC3_LINK_STATE_RESUME)) { 4453 return; 4454 } 4455 } 4456 4457 /* 4458 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending 4459 * on the link partner, the USB session might do multiple entry/exit 4460 * of low power states before a transfer takes place. 4461 * 4462 * Due to this problem, we might experience lower throughput. The 4463 * suggested workaround is to disable DCTL[12:9] bits if we're 4464 * transitioning from U1/U2 to U0 and enable those bits again 4465 * after a transfer completes and there are no pending transfers 4466 * on any of the enabled endpoints. 4467 * 4468 * This is the first half of that workaround. 4469 * 4470 * Refers to: 4471 * 4472 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us 4473 * core send LGO_Ux entering U0 4474 */ 4475 if (DWC3_VER_IS_PRIOR(DWC3, 183A)) { 4476 if (next == DWC3_LINK_STATE_U0) { 4477 u32 u1u2; 4478 u32 reg; 4479 4480 switch (dwc->link_state) { 4481 case DWC3_LINK_STATE_U1: 4482 case DWC3_LINK_STATE_U2: 4483 reg = dwc3_readl(dwc, DWC3_DCTL); 4484 u1u2 = reg & (DWC3_DCTL_INITU2ENA 4485 | DWC3_DCTL_ACCEPTU2ENA 4486 | DWC3_DCTL_INITU1ENA 4487 | DWC3_DCTL_ACCEPTU1ENA); 4488 4489 if (!dwc->u1u2) 4490 dwc->u1u2 = reg & u1u2; 4491 4492 reg &= ~u1u2; 4493 4494 dwc3_gadget_dctl_write_safe(dwc, reg); 4495 break; 4496 default: 4497 /* do nothing */ 4498 break; 4499 } 4500 } 4501 } 4502 4503 switch (next) { 4504 case DWC3_LINK_STATE_U0: 4505 if (dwc->gadget->wakeup_armed || dwc->wakeup_pending_funcs) { 4506 dwc3_gadget_enable_linksts_evts(dwc, false); 4507 dwc3_resume_gadget(dwc); 4508 dwc->suspended = false; 4509 } 4510 break; 4511 case DWC3_LINK_STATE_U1: 4512 if (dwc->speed == USB_SPEED_SUPER) 4513 dwc3_suspend_gadget(dwc); 4514 break; 4515 case DWC3_LINK_STATE_U2: 4516 case DWC3_LINK_STATE_U3: 4517 dwc3_suspend_gadget(dwc); 4518 break; 4519 case DWC3_LINK_STATE_RESUME: 4520 dwc3_resume_gadget(dwc); 4521 break; 4522 default: 4523 /* do nothing */ 4524 break; 4525 } 4526 4527 dwc->link_state = next; 4528 4529 /* Proceed with func wakeup if any interfaces that has requested */ 4530 while (dwc->wakeup_pending_funcs && (next == DWC3_LINK_STATE_U0)) { 4531 intf_id = ffs(dwc->wakeup_pending_funcs) - 1; 4532 ret = dwc3_send_gadget_generic_command(dwc, DWC3_DGCMD_DEV_NOTIFICATION, 4533 DWC3_DGCMDPAR_DN_FUNC_WAKE | 4534 DWC3_DGCMDPAR_INTF_SEL(intf_id)); 4535 if (ret) 4536 dev_err(dwc->dev, "Failed to send DN wake for intf %d\n", intf_id); 4537 4538 dwc->wakeup_pending_funcs &= ~BIT(intf_id); 4539 } 4540 } 4541 4542 static void dwc3_gadget_suspend_interrupt(struct dwc3 *dwc, 4543 unsigned int evtinfo) 4544 { 4545 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK; 4546 4547 if (!dwc->suspended && next == DWC3_LINK_STATE_U3) { 4548 dwc->suspended = true; 4549 dwc3_suspend_gadget(dwc); 4550 } 4551 4552 dwc->link_state = next; 4553 } 4554 4555 static void dwc3_gadget_interrupt(struct dwc3 *dwc, 4556 const struct dwc3_event_devt *event) 4557 { 4558 switch (event->type) { 4559 case DWC3_DEVICE_EVENT_DISCONNECT: 4560 dwc3_gadget_disconnect_interrupt(dwc); 4561 break; 4562 case DWC3_DEVICE_EVENT_RESET: 4563 dwc3_gadget_reset_interrupt(dwc); 4564 break; 4565 case DWC3_DEVICE_EVENT_CONNECT_DONE: 4566 dwc3_gadget_conndone_interrupt(dwc); 4567 break; 4568 case DWC3_DEVICE_EVENT_WAKEUP: 4569 dwc3_gadget_wakeup_interrupt(dwc, event->event_info); 4570 break; 4571 case DWC3_DEVICE_EVENT_HIBER_REQ: 4572 dev_WARN_ONCE(dwc->dev, true, "unexpected hibernation event\n"); 4573 break; 4574 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE: 4575 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info); 4576 break; 4577 case DWC3_DEVICE_EVENT_SUSPEND: 4578 /* It changed to be suspend event for version 2.30a and above */ 4579 if (!DWC3_VER_IS_PRIOR(DWC3, 230A)) 4580 dwc3_gadget_suspend_interrupt(dwc, event->event_info); 4581 break; 4582 case DWC3_DEVICE_EVENT_SOF: 4583 case DWC3_DEVICE_EVENT_ERRATIC_ERROR: 4584 case DWC3_DEVICE_EVENT_CMD_CMPL: 4585 case DWC3_DEVICE_EVENT_OVERFLOW: 4586 break; 4587 default: 4588 dev_WARN(dwc->dev, "UNKNOWN IRQ %d\n", event->type); 4589 } 4590 } 4591 4592 static void dwc3_process_event_entry(struct dwc3 *dwc, 4593 const union dwc3_event *event) 4594 { 4595 trace_dwc3_event(event->raw, dwc); 4596 4597 if (!event->type.is_devspec) 4598 dwc3_endpoint_interrupt(dwc, &event->depevt); 4599 else if (event->type.type == DWC3_EVENT_TYPE_DEV) 4600 dwc3_gadget_interrupt(dwc, &event->devt); 4601 else 4602 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw); 4603 } 4604 4605 static irqreturn_t dwc3_process_event_buf(struct dwc3_event_buffer *evt) 4606 { 4607 struct dwc3 *dwc = evt->dwc; 4608 irqreturn_t ret = IRQ_NONE; 4609 int left; 4610 4611 left = evt->count; 4612 4613 if (!(evt->flags & DWC3_EVENT_PENDING)) 4614 return IRQ_NONE; 4615 4616 while (left > 0) { 4617 union dwc3_event event; 4618 4619 event.raw = *(u32 *) (evt->cache + evt->lpos); 4620 4621 dwc3_process_event_entry(dwc, &event); 4622 4623 /* 4624 * FIXME we wrap around correctly to the next entry as 4625 * almost all entries are 4 bytes in size. There is one 4626 * entry which has 12 bytes which is a regular entry 4627 * followed by 8 bytes data. ATM I don't know how 4628 * things are organized if we get next to the a 4629 * boundary so I worry about that once we try to handle 4630 * that. 4631 */ 4632 evt->lpos = (evt->lpos + 4) % evt->length; 4633 left -= 4; 4634 } 4635 4636 evt->count = 0; 4637 ret = IRQ_HANDLED; 4638 4639 /* Unmask interrupt */ 4640 dwc3_writel(dwc, DWC3_GEVNTSIZ(0), 4641 DWC3_GEVNTSIZ_SIZE(evt->length)); 4642 4643 evt->flags &= ~DWC3_EVENT_PENDING; 4644 /* 4645 * Add an explicit write memory barrier to make sure that the update of 4646 * clearing DWC3_EVENT_PENDING is observed in dwc3_check_event_buf() 4647 */ 4648 wmb(); 4649 4650 if (dwc->imod_interval) { 4651 dwc3_writel(dwc, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB); 4652 dwc3_writel(dwc, DWC3_DEV_IMOD(0), dwc->imod_interval); 4653 } 4654 4655 return ret; 4656 } 4657 4658 static irqreturn_t dwc3_thread_interrupt(int irq, void *_evt) 4659 { 4660 struct dwc3_event_buffer *evt = _evt; 4661 struct dwc3 *dwc = evt->dwc; 4662 unsigned long flags; 4663 irqreturn_t ret = IRQ_NONE; 4664 4665 local_bh_disable(); 4666 spin_lock_irqsave(&dwc->lock, flags); 4667 ret = dwc3_process_event_buf(evt); 4668 spin_unlock_irqrestore(&dwc->lock, flags); 4669 local_bh_enable(); 4670 4671 return ret; 4672 } 4673 4674 static void dwc3_schedule_err_recovery(struct dwc3 *dwc) 4675 { 4676 dwc->err_state = DWC3_ERR_RECOVERY; 4677 schedule_work(&dwc->err_recovery_work); 4678 } 4679 4680 static irqreturn_t dwc3_check_event_buf(struct dwc3_event_buffer *evt) 4681 { 4682 struct dwc3 *dwc = evt->dwc; 4683 u32 amount; 4684 u32 count; 4685 4686 if (pm_runtime_suspended(dwc->dev)) { 4687 dwc->pending_events = true; 4688 /* 4689 * Trigger runtime resume. The get() function will be balanced 4690 * after processing the pending events in dwc3_process_pending 4691 * events(). 4692 */ 4693 pm_runtime_get(dwc->dev); 4694 disable_irq_nosync(dwc->irq_gadget); 4695 return IRQ_HANDLED; 4696 } 4697 4698 /* 4699 * With PCIe legacy interrupt, test shows that top-half irq handler can 4700 * be called again after HW interrupt deassertion. Check if bottom-half 4701 * irq event handler completes before caching new event to prevent 4702 * losing events. 4703 */ 4704 if (evt->flags & DWC3_EVENT_PENDING) 4705 return IRQ_HANDLED; 4706 4707 count = dwc3_readl(dwc, DWC3_GEVNTCOUNT(0)); 4708 count &= DWC3_GEVNTCOUNT_MASK; 4709 if (!count) 4710 return IRQ_NONE; 4711 4712 if (count > evt->length) { 4713 dev_err(dwc->dev, "invalid count(%u) > evt->length(%u)\n", 4714 count, evt->length); 4715 /* 4716 * This is a fatal error - the driver and controller are out of 4717 * sync on which event has been consumed. Reinitializing the 4718 * controller is required to recover. Write the bogus count back 4719 * to GEVNTCOUNT to clear the IRQ source, consistent with the 4720 * stale event clearing in dwc3_event_buffers_setup(), then 4721 * schedule error recovery. 4722 */ 4723 spin_lock(&dwc->lock); 4724 dwc3_schedule_err_recovery(dwc); 4725 spin_unlock(&dwc->lock); 4726 dwc3_writel(dwc, DWC3_GEVNTCOUNT(0), count); 4727 return IRQ_HANDLED; 4728 } 4729 4730 evt->count = count; 4731 evt->flags |= DWC3_EVENT_PENDING; 4732 4733 /* Mask interrupt */ 4734 dwc3_writel(dwc, DWC3_GEVNTSIZ(0), 4735 DWC3_GEVNTSIZ_INTMASK | DWC3_GEVNTSIZ_SIZE(evt->length)); 4736 4737 amount = min(count, evt->length - evt->lpos); 4738 memcpy(evt->cache + evt->lpos, evt->buf + evt->lpos, amount); 4739 4740 if (amount < count) 4741 memcpy(evt->cache, evt->buf, count - amount); 4742 4743 dwc3_writel(dwc, DWC3_GEVNTCOUNT(0), count); 4744 4745 return IRQ_WAKE_THREAD; 4746 } 4747 4748 static irqreturn_t dwc3_interrupt(int irq, void *_evt) 4749 { 4750 struct dwc3_event_buffer *evt = _evt; 4751 4752 return dwc3_check_event_buf(evt); 4753 } 4754 4755 static int dwc3_gadget_get_irq(struct dwc3 *dwc) 4756 { 4757 struct platform_device *dwc3_pdev = to_platform_device(dwc->dev); 4758 int irq; 4759 4760 irq = platform_get_irq_byname_optional(dwc3_pdev, "peripheral"); 4761 if (irq > 0) 4762 goto out; 4763 4764 if (irq == -EPROBE_DEFER) 4765 goto out; 4766 4767 irq = platform_get_irq_byname_optional(dwc3_pdev, "dwc_usb3"); 4768 if (irq > 0) 4769 goto out; 4770 4771 if (irq == -EPROBE_DEFER) 4772 goto out; 4773 4774 irq = platform_get_irq(dwc3_pdev, 0); 4775 4776 out: 4777 return irq; 4778 } 4779 4780 static void dwc_gadget_release(struct device *dev) 4781 { 4782 struct usb_gadget *gadget = container_of(dev, struct usb_gadget, dev); 4783 4784 kfree(gadget); 4785 } 4786 4787 static void dwc3_err_recovery_work(struct work_struct *work) 4788 { 4789 struct dwc3 *dwc = container_of(work, struct dwc3, err_recovery_work); 4790 unsigned long flags; 4791 int ret; 4792 4793 /* serializes against dwc3_gadget_pullup() */ 4794 mutex_lock(&dwc->connect_mutex); 4795 4796 ret = dwc3_gadget_soft_disconnect(dwc); 4797 4798 dwc3_disconnect_gadget_sleepable(dwc); 4799 4800 if (ret) 4801 goto err_unrecoverable; 4802 4803 if (dwc->softconnect) { 4804 u32 count; 4805 /* 4806 * Wait irq to finish before soft_connect resets evt->lpos and 4807 * the event buffer registers to avoid racing with 4808 * dwc3_process_event_buf(). 4809 */ 4810 synchronize_irq(dwc->irq_gadget); 4811 4812 spin_lock_irqsave(&dwc->lock, flags); 4813 count = ++dwc->err_recovery_count; 4814 spin_unlock_irqrestore(&dwc->lock, flags); 4815 4816 if (count > DWC3_ERR_RECOVERY_MAX) 4817 goto err_unrecoverable; 4818 4819 ret = dwc3_gadget_soft_connect(dwc); 4820 if (ret) 4821 goto err_unrecoverable; 4822 } 4823 mutex_unlock(&dwc->connect_mutex); 4824 4825 spin_lock_irqsave(&dwc->lock, flags); 4826 dwc->err_state = DWC3_ERR_NONE; 4827 spin_unlock_irqrestore(&dwc->lock, flags); 4828 return; 4829 4830 err_unrecoverable: 4831 dev_err(dwc->dev, "Unable to recover the controller\n"); 4832 mutex_unlock(&dwc->connect_mutex); 4833 spin_lock_irqsave(&dwc->lock, flags); 4834 dwc->err_state = DWC3_ERR_UNRECOVERABLE; 4835 spin_unlock_irqrestore(&dwc->lock, flags); 4836 } 4837 4838 /** 4839 * dwc3_gadget_init - initializes gadget related registers 4840 * @dwc: pointer to our controller context structure 4841 * 4842 * Returns 0 on success otherwise negative errno. 4843 */ 4844 int dwc3_gadget_init(struct dwc3 *dwc) 4845 { 4846 int ret; 4847 int irq; 4848 struct device *dev; 4849 4850 irq = dwc3_gadget_get_irq(dwc); 4851 if (irq < 0) { 4852 ret = irq; 4853 goto err0; 4854 } 4855 4856 dwc->irq_gadget = irq; 4857 4858 dwc->ep0_trb = dma_alloc_coherent(dwc->sysdev, 4859 sizeof(*dwc->ep0_trb) * 2, 4860 &dwc->ep0_trb_addr, GFP_KERNEL); 4861 if (!dwc->ep0_trb) { 4862 dev_err(dwc->dev, "failed to allocate ep0 trb\n"); 4863 ret = -ENOMEM; 4864 goto err0; 4865 } 4866 4867 dwc->setup_buf = kzalloc(DWC3_EP0_SETUP_SIZE, GFP_KERNEL); 4868 if (!dwc->setup_buf) { 4869 ret = -ENOMEM; 4870 goto err1; 4871 } 4872 4873 dwc->bounce = dma_alloc_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, 4874 &dwc->bounce_addr, GFP_KERNEL); 4875 if (!dwc->bounce) { 4876 ret = -ENOMEM; 4877 goto err2; 4878 } 4879 4880 init_completion(&dwc->ep0_in_setup); 4881 INIT_WORK(&dwc->err_recovery_work, dwc3_err_recovery_work); 4882 mutex_init(&dwc->connect_mutex); 4883 dwc->gadget = kzalloc_obj(struct usb_gadget); 4884 if (!dwc->gadget) { 4885 ret = -ENOMEM; 4886 goto err3; 4887 } 4888 4889 4890 usb_initialize_gadget(dwc->dev, dwc->gadget, dwc_gadget_release); 4891 dev = &dwc->gadget->dev; 4892 dev->platform_data = dwc; 4893 dwc->gadget->ops = &dwc3_gadget_ops; 4894 dwc->gadget->speed = USB_SPEED_UNKNOWN; 4895 dwc->gadget->ssp_rate = USB_SSP_GEN_UNKNOWN; 4896 dwc->gadget->sg_supported = true; 4897 dwc->gadget->name = "dwc3-gadget"; 4898 dwc->gadget->lpm_capable = !dwc->usb2_gadget_lpm_disable; 4899 dwc->gadget->wakeup_capable = true; 4900 4901 /* 4902 * FIXME We might be setting max_speed to <SUPER, however versions 4903 * <2.20a of dwc3 have an issue with metastability (documented 4904 * elsewhere in this driver) which tells us we can't set max speed to 4905 * anything lower than SUPER. 4906 * 4907 * Because gadget.max_speed is only used by composite.c and function 4908 * drivers (i.e. it won't go into dwc3's registers) we are allowing this 4909 * to happen so we avoid sending SuperSpeed Capability descriptor 4910 * together with our BOS descriptor as that could confuse host into 4911 * thinking we can handle super speed. 4912 * 4913 * Note that, in fact, we won't even support GetBOS requests when speed 4914 * is less than super speed because we don't have means, yet, to tell 4915 * composite.c that we are USB 2.0 + LPM ECN. 4916 */ 4917 if (DWC3_VER_IS_PRIOR(DWC3, 220A) && 4918 !dwc->dis_metastability_quirk) 4919 dev_info(dwc->dev, "changing max_speed on rev %08x\n", 4920 dwc->revision); 4921 4922 dwc->gadget->max_speed = dwc->maximum_speed; 4923 dwc->gadget->max_ssp_rate = dwc->max_ssp_rate; 4924 4925 /* 4926 * REVISIT: Here we should clear all pending IRQs to be 4927 * sure we're starting from a well known location. 4928 */ 4929 4930 ret = dwc3_gadget_init_endpoints(dwc, dwc->num_eps); 4931 if (ret) 4932 goto err4; 4933 4934 ret = usb_add_gadget(dwc->gadget); 4935 if (ret) { 4936 dev_err(dwc->dev, "failed to add gadget\n"); 4937 goto err5; 4938 } 4939 4940 if (DWC3_IP_IS(DWC32) && dwc->maximum_speed == USB_SPEED_SUPER_PLUS) 4941 dwc3_gadget_set_ssp_rate(dwc->gadget, dwc->max_ssp_rate); 4942 else 4943 dwc3_gadget_set_speed(dwc->gadget, dwc->maximum_speed); 4944 4945 /* No system wakeup if no gadget driver bound */ 4946 if (dwc->sys_wakeup) 4947 device_wakeup_disable(dwc->sysdev); 4948 4949 return 0; 4950 4951 err5: 4952 dwc3_gadget_free_endpoints(dwc); 4953 err4: 4954 usb_put_gadget(dwc->gadget); 4955 dwc->gadget = NULL; 4956 err3: 4957 dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce, 4958 dwc->bounce_addr); 4959 4960 err2: 4961 kfree(dwc->setup_buf); 4962 4963 err1: 4964 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2, 4965 dwc->ep0_trb, dwc->ep0_trb_addr); 4966 4967 err0: 4968 return ret; 4969 } 4970 EXPORT_SYMBOL_GPL(dwc3_gadget_init); 4971 4972 /* -------------------------------------------------------------------------- */ 4973 4974 void dwc3_gadget_exit(struct dwc3 *dwc) 4975 { 4976 if (!dwc->gadget) 4977 return; 4978 4979 cancel_work_sync(&dwc->err_recovery_work); 4980 mutex_destroy(&dwc->connect_mutex); 4981 dwc3_enable_susphy(dwc, true); 4982 usb_del_gadget(dwc->gadget); 4983 dwc3_gadget_free_endpoints(dwc); 4984 usb_put_gadget(dwc->gadget); 4985 dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce, 4986 dwc->bounce_addr); 4987 kfree(dwc->setup_buf); 4988 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2, 4989 dwc->ep0_trb, dwc->ep0_trb_addr); 4990 } 4991 EXPORT_SYMBOL_GPL(dwc3_gadget_exit); 4992 4993 int dwc3_gadget_suspend(struct dwc3 *dwc) 4994 { 4995 int ret; 4996 4997 ret = dwc3_gadget_soft_disconnect(dwc); 4998 /* 4999 * Attempt to reset the controller's state. Likely no 5000 * communication can be established until the host 5001 * performs a port reset. 5002 */ 5003 if (ret && dwc->softconnect) { 5004 dwc3_gadget_soft_connect(dwc); 5005 return -EAGAIN; 5006 } 5007 5008 dwc3_disconnect_gadget_sleepable(dwc); 5009 5010 return 0; 5011 } 5012 5013 int dwc3_gadget_resume(struct dwc3 *dwc) 5014 { 5015 if (!dwc->gadget_driver || !dwc->softconnect) 5016 return 0; 5017 5018 return dwc3_gadget_soft_connect(dwc); 5019 } 5020