1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * mtu3_gadget.c - MediaTek usb3 DRD peripheral support 4 * 5 * Copyright (C) 2016 MediaTek Inc. 6 * 7 * Author: Chunfeng Yun <chunfeng.yun@mediatek.com> 8 */ 9 10 #include "mtu3.h" 11 #include "mtu3_trace.h" 12 13 void mtu3_req_complete(struct mtu3_ep *mep, 14 struct usb_request *req, int status) 15 __releases(mep->mtu->lock) 16 __acquires(mep->mtu->lock) 17 { 18 struct mtu3_request *mreq = to_mtu3_request(req); 19 struct mtu3 *mtu = mreq->mtu; 20 21 list_del(&mreq->list); 22 if (req->status == -EINPROGRESS) 23 req->status = status; 24 25 trace_mtu3_req_complete(mreq); 26 spin_unlock(&mtu->lock); 27 28 /* ep0 makes use of PIO, needn't unmap it */ 29 if (mep->epnum) 30 usb_gadget_unmap_request(&mtu->g, req, mep->is_in); 31 32 dev_dbg(mtu->dev, "%s complete req: %p, sts %d, %d/%d\n", 33 mep->name, req, req->status, req->actual, req->length); 34 35 usb_gadget_giveback_request(&mep->ep, req); 36 spin_lock(&mtu->lock); 37 } 38 39 static void nuke(struct mtu3_ep *mep, const int status) 40 { 41 struct mtu3_request *mreq = NULL; 42 43 if (list_empty(&mep->req_list)) 44 return; 45 46 dev_dbg(mep->mtu->dev, "abort %s's req: sts %d\n", mep->name, status); 47 48 /* exclude EP0 */ 49 if (mep->epnum) 50 mtu3_qmu_flush(mep); 51 52 while (!list_empty(&mep->req_list)) { 53 mreq = list_first_entry(&mep->req_list, 54 struct mtu3_request, list); 55 mtu3_req_complete(mep, &mreq->request, status); 56 } 57 } 58 59 static int mtu3_ep_enable(struct mtu3_ep *mep) 60 { 61 const struct usb_endpoint_descriptor *desc; 62 const struct usb_ss_ep_comp_descriptor *comp_desc; 63 struct mtu3 *mtu = mep->mtu; 64 u32 interval = 0; 65 u32 mult = 0; 66 u32 burst = 0; 67 int ret; 68 69 desc = mep->desc; 70 comp_desc = mep->comp_desc; 71 mep->type = usb_endpoint_type(desc); 72 mep->maxp = usb_endpoint_maxp(desc); 73 74 switch (mtu->g.speed) { 75 case USB_SPEED_SUPER: 76 case USB_SPEED_SUPER_PLUS: 77 if (usb_endpoint_xfer_int(desc) || 78 usb_endpoint_xfer_isoc(desc)) { 79 interval = desc->bInterval; 80 interval = clamp_val(interval, 1, 16); 81 if (usb_endpoint_xfer_isoc(desc) && comp_desc) 82 mult = comp_desc->bmAttributes; 83 } 84 if (comp_desc) 85 burst = comp_desc->bMaxBurst; 86 87 break; 88 case USB_SPEED_HIGH: 89 if (usb_endpoint_xfer_isoc(desc) || 90 usb_endpoint_xfer_int(desc)) { 91 interval = desc->bInterval; 92 interval = clamp_val(interval, 1, 16); 93 mult = usb_endpoint_maxp_mult(desc) - 1; 94 } 95 break; 96 case USB_SPEED_FULL: 97 if (usb_endpoint_xfer_isoc(desc)) 98 interval = clamp_val(desc->bInterval, 1, 16); 99 else if (usb_endpoint_xfer_int(desc)) 100 interval = clamp_val(desc->bInterval, 1, 255); 101 102 break; 103 default: 104 break; /*others are ignored */ 105 } 106 107 dev_dbg(mtu->dev, "%s maxp:%d, interval:%d, burst:%d, mult:%d\n", 108 __func__, mep->maxp, interval, burst, mult); 109 110 mep->ep.maxpacket = mep->maxp; 111 mep->ep.desc = desc; 112 mep->ep.comp_desc = comp_desc; 113 114 /* slot mainly affects bulk/isoc transfer, so ignore int */ 115 mep->slot = usb_endpoint_xfer_int(desc) ? 0 : mtu->slot; 116 117 ret = mtu3_config_ep(mtu, mep, interval, burst, mult); 118 if (ret < 0) 119 return ret; 120 121 ret = mtu3_gpd_ring_alloc(mep); 122 if (ret < 0) { 123 mtu3_deconfig_ep(mtu, mep); 124 return ret; 125 } 126 127 mtu3_qmu_start(mep); 128 129 return 0; 130 } 131 132 static int mtu3_ep_disable(struct mtu3_ep *mep) 133 { 134 struct mtu3 *mtu = mep->mtu; 135 136 mtu3_qmu_stop(mep); 137 138 /* abort all pending requests */ 139 nuke(mep, -ESHUTDOWN); 140 mtu3_deconfig_ep(mtu, mep); 141 mtu3_gpd_ring_free(mep); 142 143 mep->desc = NULL; 144 mep->ep.desc = NULL; 145 mep->comp_desc = NULL; 146 mep->type = 0; 147 mep->flags = 0; 148 149 return 0; 150 } 151 152 static int mtu3_gadget_ep_enable(struct usb_ep *ep, 153 const struct usb_endpoint_descriptor *desc) 154 { 155 struct mtu3_ep *mep; 156 struct mtu3 *mtu; 157 unsigned long flags; 158 int ret = -EINVAL; 159 160 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) { 161 pr_debug("%s invalid parameters\n", __func__); 162 return -EINVAL; 163 } 164 165 if (!desc->wMaxPacketSize) { 166 pr_debug("%s missing wMaxPacketSize\n", __func__); 167 return -EINVAL; 168 } 169 mep = to_mtu3_ep(ep); 170 mtu = mep->mtu; 171 172 /* check ep number and direction against endpoint */ 173 if (usb_endpoint_num(desc) != mep->epnum) 174 return -EINVAL; 175 176 if (!!usb_endpoint_dir_in(desc) ^ !!mep->is_in) 177 return -EINVAL; 178 179 dev_dbg(mtu->dev, "%s %s\n", __func__, ep->name); 180 181 if (mep->flags & MTU3_EP_ENABLED) { 182 dev_WARN_ONCE(mtu->dev, true, "%s is already enabled\n", 183 mep->name); 184 return 0; 185 } 186 187 spin_lock_irqsave(&mtu->lock, flags); 188 mep->desc = desc; 189 mep->comp_desc = ep->comp_desc; 190 191 ret = mtu3_ep_enable(mep); 192 if (ret) 193 goto error; 194 195 mep->flags = MTU3_EP_ENABLED; 196 mtu->active_ep++; 197 198 error: 199 spin_unlock_irqrestore(&mtu->lock, flags); 200 201 dev_dbg(mtu->dev, "%s active_ep=%d\n", __func__, mtu->active_ep); 202 trace_mtu3_gadget_ep_enable(mep); 203 204 return ret; 205 } 206 207 static int mtu3_gadget_ep_disable(struct usb_ep *ep) 208 { 209 struct mtu3_ep *mep = to_mtu3_ep(ep); 210 struct mtu3 *mtu = mep->mtu; 211 unsigned long flags; 212 213 dev_dbg(mtu->dev, "%s %s\n", __func__, mep->name); 214 trace_mtu3_gadget_ep_disable(mep); 215 216 if (!(mep->flags & MTU3_EP_ENABLED)) { 217 dev_warn(mtu->dev, "%s is already disabled\n", mep->name); 218 return 0; 219 } 220 221 spin_lock_irqsave(&mtu->lock, flags); 222 mtu3_ep_disable(mep); 223 mep->flags = 0; 224 mtu->active_ep--; 225 spin_unlock_irqrestore(&(mtu->lock), flags); 226 227 dev_dbg(mtu->dev, "%s active_ep=%d, mtu3 is_active=%d\n", 228 __func__, mtu->active_ep, mtu->is_active); 229 230 return 0; 231 } 232 233 struct usb_request *mtu3_alloc_request(struct usb_ep *ep, gfp_t gfp_flags) 234 { 235 struct mtu3_ep *mep = to_mtu3_ep(ep); 236 struct mtu3_request *mreq; 237 238 mreq = kzalloc(sizeof(*mreq), gfp_flags); 239 if (!mreq) 240 return NULL; 241 242 mreq->request.dma = DMA_ADDR_INVALID; 243 mreq->epnum = mep->epnum; 244 mreq->mep = mep; 245 INIT_LIST_HEAD(&mreq->list); 246 trace_mtu3_alloc_request(mreq); 247 248 return &mreq->request; 249 } 250 251 void mtu3_free_request(struct usb_ep *ep, struct usb_request *req) 252 { 253 struct mtu3_request *mreq = to_mtu3_request(req); 254 255 trace_mtu3_free_request(mreq); 256 kfree(mreq); 257 } 258 259 static int mtu3_gadget_queue(struct usb_ep *ep, 260 struct usb_request *req, gfp_t gfp_flags) 261 { 262 struct mtu3_ep *mep = to_mtu3_ep(ep); 263 struct mtu3_request *mreq = to_mtu3_request(req); 264 struct mtu3 *mtu = mep->mtu; 265 unsigned long flags; 266 int ret = 0; 267 268 if (!req->buf) 269 return -ENODATA; 270 271 if (mreq->mep != mep) 272 return -EINVAL; 273 274 dev_dbg(mtu->dev, "%s %s EP%d(%s), req=%p, maxp=%d, len#%d\n", 275 __func__, mep->is_in ? "TX" : "RX", mreq->epnum, ep->name, 276 mreq, ep->maxpacket, mreq->request.length); 277 278 if (req->length > GPD_BUF_SIZE || 279 (mtu->gen2cp && req->length > GPD_BUF_SIZE_EL)) { 280 dev_warn(mtu->dev, 281 "req length > supported MAX:%d requested:%d\n", 282 mtu->gen2cp ? GPD_BUF_SIZE_EL : GPD_BUF_SIZE, 283 req->length); 284 return -EOPNOTSUPP; 285 } 286 287 /* don't queue if the ep is down */ 288 if (!mep->desc) { 289 dev_dbg(mtu->dev, "req=%p queued to %s while it's disabled\n", 290 req, ep->name); 291 return -ESHUTDOWN; 292 } 293 294 mreq->mtu = mtu; 295 mreq->request.actual = 0; 296 mreq->request.status = -EINPROGRESS; 297 298 ret = usb_gadget_map_request(&mtu->g, req, mep->is_in); 299 if (ret) { 300 dev_err(mtu->dev, "dma mapping failed\n"); 301 return ret; 302 } 303 304 spin_lock_irqsave(&mtu->lock, flags); 305 306 if (mtu3_prepare_transfer(mep)) { 307 ret = -EAGAIN; 308 goto error; 309 } 310 311 list_add_tail(&mreq->list, &mep->req_list); 312 mtu3_insert_gpd(mep, mreq); 313 mtu3_qmu_resume(mep); 314 315 error: 316 spin_unlock_irqrestore(&mtu->lock, flags); 317 trace_mtu3_gadget_queue(mreq); 318 319 return ret; 320 } 321 322 static int mtu3_gadget_dequeue(struct usb_ep *ep, struct usb_request *req) 323 { 324 struct mtu3_ep *mep = to_mtu3_ep(ep); 325 struct mtu3_request *mreq = to_mtu3_request(req); 326 struct mtu3_request *r; 327 struct mtu3 *mtu = mep->mtu; 328 unsigned long flags; 329 int ret = 0; 330 331 if (mreq->mep != mep) 332 return -EINVAL; 333 334 dev_dbg(mtu->dev, "%s : req=%p\n", __func__, req); 335 trace_mtu3_gadget_dequeue(mreq); 336 337 spin_lock_irqsave(&mtu->lock, flags); 338 339 list_for_each_entry(r, &mep->req_list, list) { 340 if (r == mreq) 341 break; 342 } 343 if (r != mreq) { 344 dev_dbg(mtu->dev, "req=%p not queued to %s\n", req, ep->name); 345 ret = -EINVAL; 346 goto done; 347 } 348 349 mtu3_qmu_flush(mep); /* REVISIT: set BPS ?? */ 350 mtu3_req_complete(mep, req, -ECONNRESET); 351 mtu3_qmu_start(mep); 352 353 done: 354 spin_unlock_irqrestore(&mtu->lock, flags); 355 356 return ret; 357 } 358 359 /* 360 * Set or clear the halt bit of an EP. 361 * A halted EP won't TX/RX any data but will queue requests. 362 */ 363 static int mtu3_gadget_ep_set_halt(struct usb_ep *ep, int value) 364 { 365 struct mtu3_ep *mep = to_mtu3_ep(ep); 366 struct mtu3 *mtu = mep->mtu; 367 struct mtu3_request *mreq; 368 unsigned long flags; 369 int ret = 0; 370 371 dev_dbg(mtu->dev, "%s : %s...", __func__, ep->name); 372 373 spin_lock_irqsave(&mtu->lock, flags); 374 375 if (mep->type == USB_ENDPOINT_XFER_ISOC) { 376 ret = -EINVAL; 377 goto done; 378 } 379 380 mreq = next_request(mep); 381 if (value) { 382 /* 383 * If there is not request for TX-EP, QMU will not transfer 384 * data to TX-FIFO, so no need check whether TX-FIFO 385 * holds bytes or not here 386 */ 387 if (mreq) { 388 dev_dbg(mtu->dev, "req in progress, cannot halt %s\n", 389 ep->name); 390 ret = -EAGAIN; 391 goto done; 392 } 393 } else { 394 mep->flags &= ~MTU3_EP_WEDGE; 395 } 396 397 dev_dbg(mtu->dev, "%s %s stall\n", ep->name, value ? "set" : "clear"); 398 399 mtu3_ep_stall_set(mep, value); 400 401 done: 402 spin_unlock_irqrestore(&mtu->lock, flags); 403 trace_mtu3_gadget_ep_set_halt(mep); 404 405 return ret; 406 } 407 408 /* Sets the halt feature with the clear requests ignored */ 409 static int mtu3_gadget_ep_set_wedge(struct usb_ep *ep) 410 { 411 struct mtu3_ep *mep = to_mtu3_ep(ep); 412 413 mep->flags |= MTU3_EP_WEDGE; 414 415 return usb_ep_set_halt(ep); 416 } 417 418 static const struct usb_ep_ops mtu3_ep_ops = { 419 .enable = mtu3_gadget_ep_enable, 420 .disable = mtu3_gadget_ep_disable, 421 .alloc_request = mtu3_alloc_request, 422 .free_request = mtu3_free_request, 423 .queue = mtu3_gadget_queue, 424 .dequeue = mtu3_gadget_dequeue, 425 .set_halt = mtu3_gadget_ep_set_halt, 426 .set_wedge = mtu3_gadget_ep_set_wedge, 427 }; 428 429 static int mtu3_gadget_get_frame(struct usb_gadget *gadget) 430 { 431 struct mtu3 *mtu = gadget_to_mtu3(gadget); 432 433 return (int)mtu3_readl(mtu->mac_base, U3D_USB20_FRAME_NUM); 434 } 435 436 static void function_wake_notif(struct mtu3 *mtu, u8 intf) 437 { 438 mtu3_writel(mtu->mac_base, U3D_DEV_NOTIF_0, 439 TYPE_FUNCTION_WAKE | DEV_NOTIF_VAL_FW(intf)); 440 mtu3_setbits(mtu->mac_base, U3D_DEV_NOTIF_0, SEND_DEV_NOTIF); 441 } 442 443 static int mtu3_gadget_wakeup(struct usb_gadget *gadget) 444 { 445 struct mtu3 *mtu = gadget_to_mtu3(gadget); 446 unsigned long flags; 447 448 dev_dbg(mtu->dev, "%s\n", __func__); 449 450 /* remote wakeup feature is not enabled by host */ 451 if (!mtu->may_wakeup) 452 return -EOPNOTSUPP; 453 454 spin_lock_irqsave(&mtu->lock, flags); 455 if (mtu->g.speed >= USB_SPEED_SUPER) { 456 /* 457 * class driver may do function wakeup even UFP is in U0, 458 * and UX_EXIT only takes effect in U1/U2/U3; 459 */ 460 mtu3_setbits(mtu->mac_base, U3D_LINK_POWER_CONTROL, UX_EXIT); 461 /* 462 * Assume there's only one function on the composite device 463 * and enable remote wake for the first interface. 464 * FIXME if the IAD (interface association descriptor) shows 465 * there is more than one function. 466 */ 467 function_wake_notif(mtu, 0); 468 } else { 469 mtu3_setbits(mtu->mac_base, U3D_POWER_MANAGEMENT, RESUME); 470 spin_unlock_irqrestore(&mtu->lock, flags); 471 usleep_range(10000, 11000); 472 spin_lock_irqsave(&mtu->lock, flags); 473 mtu3_clrbits(mtu->mac_base, U3D_POWER_MANAGEMENT, RESUME); 474 } 475 spin_unlock_irqrestore(&mtu->lock, flags); 476 return 0; 477 } 478 479 static int mtu3_gadget_set_self_powered(struct usb_gadget *gadget, 480 int is_selfpowered) 481 { 482 struct mtu3 *mtu = gadget_to_mtu3(gadget); 483 484 mtu->is_self_powered = !!is_selfpowered; 485 return 0; 486 } 487 488 static int mtu3_gadget_pullup(struct usb_gadget *gadget, int is_on) 489 { 490 struct mtu3 *mtu = gadget_to_mtu3(gadget); 491 unsigned long flags; 492 493 dev_dbg(mtu->dev, "%s (%s) for %sactive device\n", __func__, 494 is_on ? "on" : "off", mtu->is_active ? "" : "in"); 495 496 pm_runtime_get_sync(mtu->dev); 497 498 /* we'd rather not pullup unless the device is active. */ 499 spin_lock_irqsave(&mtu->lock, flags); 500 501 is_on = !!is_on; 502 if (!mtu->is_active) { 503 /* save it for mtu3_start() to process the request */ 504 mtu->softconnect = is_on; 505 } else if (is_on != mtu->softconnect) { 506 mtu->softconnect = is_on; 507 mtu3_dev_on_off(mtu, is_on); 508 } 509 510 spin_unlock_irqrestore(&mtu->lock, flags); 511 pm_runtime_put(mtu->dev); 512 513 return 0; 514 } 515 516 static int mtu3_gadget_start(struct usb_gadget *gadget, 517 struct usb_gadget_driver *driver) 518 { 519 struct mtu3 *mtu = gadget_to_mtu3(gadget); 520 unsigned long flags; 521 522 if (mtu->gadget_driver) { 523 dev_err(mtu->dev, "%s is already bound to %s\n", 524 mtu->g.name, mtu->gadget_driver->driver.name); 525 return -EBUSY; 526 } 527 528 dev_dbg(mtu->dev, "bind driver %s\n", driver->function); 529 pm_runtime_get_sync(mtu->dev); 530 531 spin_lock_irqsave(&mtu->lock, flags); 532 533 mtu->softconnect = 0; 534 mtu->gadget_driver = driver; 535 536 if (mtu->ssusb->dr_mode == USB_DR_MODE_PERIPHERAL) 537 mtu3_start(mtu); 538 539 spin_unlock_irqrestore(&mtu->lock, flags); 540 pm_runtime_put(mtu->dev); 541 542 return 0; 543 } 544 545 static void stop_activity(struct mtu3 *mtu) 546 { 547 struct usb_gadget_driver *driver = mtu->gadget_driver; 548 int i; 549 550 /* don't disconnect if it's not connected */ 551 if (mtu->g.speed == USB_SPEED_UNKNOWN) 552 driver = NULL; 553 else 554 mtu->g.speed = USB_SPEED_UNKNOWN; 555 556 /* deactivate the hardware */ 557 if (mtu->softconnect) { 558 mtu->softconnect = 0; 559 mtu3_dev_on_off(mtu, 0); 560 } 561 562 /* 563 * killing any outstanding requests will quiesce the driver; 564 * then report disconnect 565 */ 566 nuke(mtu->ep0, -ESHUTDOWN); 567 for (i = 1; i < mtu->num_eps; i++) { 568 nuke(mtu->in_eps + i, -ESHUTDOWN); 569 nuke(mtu->out_eps + i, -ESHUTDOWN); 570 } 571 572 if (driver) { 573 spin_unlock(&mtu->lock); 574 driver->disconnect(&mtu->g); 575 spin_lock(&mtu->lock); 576 } 577 } 578 579 static int mtu3_gadget_stop(struct usb_gadget *g) 580 { 581 struct mtu3 *mtu = gadget_to_mtu3(g); 582 unsigned long flags; 583 584 dev_dbg(mtu->dev, "%s\n", __func__); 585 586 spin_lock_irqsave(&mtu->lock, flags); 587 588 stop_activity(mtu); 589 mtu->gadget_driver = NULL; 590 591 if (mtu->ssusb->dr_mode == USB_DR_MODE_PERIPHERAL) 592 mtu3_stop(mtu); 593 594 spin_unlock_irqrestore(&mtu->lock, flags); 595 596 synchronize_irq(mtu->irq); 597 return 0; 598 } 599 600 static void 601 mtu3_gadget_set_speed(struct usb_gadget *g, enum usb_device_speed speed) 602 { 603 struct mtu3 *mtu = gadget_to_mtu3(g); 604 unsigned long flags; 605 606 dev_dbg(mtu->dev, "%s %s\n", __func__, usb_speed_string(speed)); 607 608 spin_lock_irqsave(&mtu->lock, flags); 609 mtu->speed = speed; 610 spin_unlock_irqrestore(&mtu->lock, flags); 611 } 612 613 static void mtu3_gadget_async_callbacks(struct usb_gadget *g, bool enable) 614 { 615 struct mtu3 *mtu = gadget_to_mtu3(g); 616 unsigned long flags; 617 618 dev_dbg(mtu->dev, "%s %s\n", __func__, enable ? "en" : "dis"); 619 620 spin_lock_irqsave(&mtu->lock, flags); 621 mtu->async_callbacks = enable; 622 spin_unlock_irqrestore(&mtu->lock, flags); 623 } 624 625 static const struct usb_gadget_ops mtu3_gadget_ops = { 626 .get_frame = mtu3_gadget_get_frame, 627 .wakeup = mtu3_gadget_wakeup, 628 .set_selfpowered = mtu3_gadget_set_self_powered, 629 .pullup = mtu3_gadget_pullup, 630 .udc_start = mtu3_gadget_start, 631 .udc_stop = mtu3_gadget_stop, 632 .udc_set_speed = mtu3_gadget_set_speed, 633 .udc_async_callbacks = mtu3_gadget_async_callbacks, 634 }; 635 636 static void mtu3_state_reset(struct mtu3 *mtu) 637 { 638 mtu->address = 0; 639 mtu->ep0_state = MU3D_EP0_STATE_SETUP; 640 mtu->may_wakeup = 0; 641 mtu->u1_enable = 0; 642 mtu->u2_enable = 0; 643 mtu->delayed_status = false; 644 mtu->test_mode = false; 645 } 646 647 static void init_hw_ep(struct mtu3 *mtu, struct mtu3_ep *mep, 648 u32 epnum, u32 is_in) 649 { 650 mep->epnum = epnum; 651 mep->mtu = mtu; 652 mep->is_in = is_in; 653 654 INIT_LIST_HEAD(&mep->req_list); 655 656 sprintf(mep->name, "ep%d%s", epnum, 657 !epnum ? "" : (is_in ? "in" : "out")); 658 659 mep->ep.name = mep->name; 660 INIT_LIST_HEAD(&mep->ep.ep_list); 661 662 /* initialize maxpacket as SS */ 663 if (!epnum) { 664 usb_ep_set_maxpacket_limit(&mep->ep, 512); 665 mep->ep.caps.type_control = true; 666 mep->ep.ops = &mtu3_ep0_ops; 667 mtu->g.ep0 = &mep->ep; 668 } else { 669 usb_ep_set_maxpacket_limit(&mep->ep, 1024); 670 mep->ep.caps.type_iso = true; 671 mep->ep.caps.type_bulk = true; 672 mep->ep.caps.type_int = true; 673 mep->ep.ops = &mtu3_ep_ops; 674 list_add_tail(&mep->ep.ep_list, &mtu->g.ep_list); 675 } 676 677 dev_dbg(mtu->dev, "%s, name=%s, maxp=%d\n", __func__, mep->ep.name, 678 mep->ep.maxpacket); 679 680 if (!epnum) { 681 mep->ep.caps.dir_in = true; 682 mep->ep.caps.dir_out = true; 683 } else if (is_in) { 684 mep->ep.caps.dir_in = true; 685 } else { 686 mep->ep.caps.dir_out = true; 687 } 688 } 689 690 static void mtu3_gadget_init_eps(struct mtu3 *mtu) 691 { 692 u8 epnum; 693 694 /* initialize endpoint list just once */ 695 INIT_LIST_HEAD(&(mtu->g.ep_list)); 696 697 dev_dbg(mtu->dev, "%s num_eps(1 for a pair of tx&rx ep)=%d\n", 698 __func__, mtu->num_eps); 699 700 init_hw_ep(mtu, mtu->ep0, 0, 0); 701 for (epnum = 1; epnum < mtu->num_eps; epnum++) { 702 init_hw_ep(mtu, mtu->in_eps + epnum, epnum, 1); 703 init_hw_ep(mtu, mtu->out_eps + epnum, epnum, 0); 704 } 705 } 706 707 int mtu3_gadget_setup(struct mtu3 *mtu) 708 { 709 mtu->g.ops = &mtu3_gadget_ops; 710 mtu->g.max_speed = mtu->max_speed; 711 mtu->g.speed = USB_SPEED_UNKNOWN; 712 mtu->g.sg_supported = 0; 713 mtu->g.name = MTU3_DRIVER_NAME; 714 mtu->g.irq = mtu->irq; 715 mtu->is_active = 0; 716 mtu->delayed_status = false; 717 718 mtu3_gadget_init_eps(mtu); 719 720 return usb_add_gadget_udc(mtu->dev, &mtu->g); 721 } 722 723 void mtu3_gadget_cleanup(struct mtu3 *mtu) 724 { 725 usb_del_gadget_udc(&mtu->g); 726 } 727 728 void mtu3_gadget_resume(struct mtu3 *mtu) 729 { 730 dev_dbg(mtu->dev, "gadget RESUME\n"); 731 if (mtu->async_callbacks && mtu->gadget_driver && mtu->gadget_driver->resume) { 732 spin_unlock(&mtu->lock); 733 mtu->gadget_driver->resume(&mtu->g); 734 spin_lock(&mtu->lock); 735 } 736 } 737 738 /* called when SOF packets stop for 3+ msec or enters U3 */ 739 void mtu3_gadget_suspend(struct mtu3 *mtu) 740 { 741 dev_dbg(mtu->dev, "gadget SUSPEND\n"); 742 if (mtu->async_callbacks && mtu->gadget_driver && mtu->gadget_driver->suspend) { 743 spin_unlock(&mtu->lock); 744 mtu->gadget_driver->suspend(&mtu->g); 745 spin_lock(&mtu->lock); 746 } 747 } 748 749 /* called when VBUS drops below session threshold, and in other cases */ 750 void mtu3_gadget_disconnect(struct mtu3 *mtu) 751 { 752 dev_dbg(mtu->dev, "gadget DISCONNECT\n"); 753 if (mtu->async_callbacks && mtu->gadget_driver && mtu->gadget_driver->disconnect) { 754 spin_unlock(&mtu->lock); 755 mtu->gadget_driver->disconnect(&mtu->g); 756 spin_lock(&mtu->lock); 757 } 758 759 mtu3_state_reset(mtu); 760 usb_gadget_set_state(&mtu->g, USB_STATE_NOTATTACHED); 761 } 762 763 void mtu3_gadget_reset(struct mtu3 *mtu) 764 { 765 dev_dbg(mtu->dev, "gadget RESET\n"); 766 767 /* report disconnect, if we didn't flush EP state */ 768 if (mtu->g.speed != USB_SPEED_UNKNOWN) 769 mtu3_gadget_disconnect(mtu); 770 else 771 mtu3_state_reset(mtu); 772 } 773