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