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