Lines Matching +full:tx +full:- +full:burst +full:- +full:length

1 // SPDX-License-Identifier: GPL-2.0
3 * mtu3_gadget.c - MediaTek usb3 DRD peripheral support
15 __releases(mep->mtu->lock) in mtu3_req_complete()
16 __acquires(mep->mtu->lock) in mtu3_req_complete()
19 struct mtu3 *mtu = mreq->mtu; in mtu3_req_complete()
21 list_del(&mreq->list); in mtu3_req_complete()
22 if (req->status == -EINPROGRESS) in mtu3_req_complete()
23 req->status = status; in mtu3_req_complete()
28 if (mep->epnum) in mtu3_req_complete()
29 usb_gadget_unmap_request(&mtu->g, req, mep->is_in); in mtu3_req_complete()
31 dev_dbg(mtu->dev, "%s complete req: %p, sts %d, %d/%d\n", in mtu3_req_complete()
32 mep->name, req, req->status, req->actual, req->length); in mtu3_req_complete()
34 spin_unlock(&mtu->lock); in mtu3_req_complete()
35 usb_gadget_giveback_request(&mep->ep, req); in mtu3_req_complete()
36 spin_lock(&mtu->lock); in mtu3_req_complete()
43 if (list_empty(&mep->req_list)) in nuke()
46 dev_dbg(mep->mtu->dev, "abort %s's req: sts %d\n", mep->name, status); in nuke()
49 if (mep->epnum) in nuke()
52 while (!list_empty(&mep->req_list)) { in nuke()
53 mreq = list_first_entry(&mep->req_list, in nuke()
55 mtu3_req_complete(mep, &mreq->request, status); in nuke()
63 struct mtu3 *mtu = mep->mtu; in mtu3_ep_enable()
66 u32 burst = 0; in mtu3_ep_enable() local
69 desc = mep->desc; in mtu3_ep_enable()
70 comp_desc = mep->comp_desc; in mtu3_ep_enable()
71 mep->type = usb_endpoint_type(desc); in mtu3_ep_enable()
72 mep->maxp = usb_endpoint_maxp(desc); in mtu3_ep_enable()
74 switch (mtu->g.speed) { in mtu3_ep_enable()
79 interval = desc->bInterval; in mtu3_ep_enable()
82 mult = comp_desc->bmAttributes; in mtu3_ep_enable()
85 burst = comp_desc->bMaxBurst; in mtu3_ep_enable()
91 interval = desc->bInterval; in mtu3_ep_enable()
93 mult = usb_endpoint_maxp_mult(desc) - 1; in mtu3_ep_enable()
98 interval = clamp_val(desc->bInterval, 1, 16); in mtu3_ep_enable()
100 interval = clamp_val(desc->bInterval, 1, 255); in mtu3_ep_enable()
107 dev_dbg(mtu->dev, "%s maxp:%d, interval:%d, burst:%d, mult:%d\n", in mtu3_ep_enable()
108 __func__, mep->maxp, interval, burst, mult); in mtu3_ep_enable()
110 mep->ep.maxpacket = mep->maxp; in mtu3_ep_enable()
111 mep->ep.desc = desc; in mtu3_ep_enable()
112 mep->ep.comp_desc = comp_desc; in mtu3_ep_enable()
115 mep->slot = usb_endpoint_xfer_int(desc) ? 0 : mtu->slot; in mtu3_ep_enable()
117 ret = mtu3_config_ep(mtu, mep, interval, burst, mult); in mtu3_ep_enable()
134 struct mtu3 *mtu = mep->mtu; in mtu3_ep_disable()
137 nuke(mep, -ESHUTDOWN); in mtu3_ep_disable()
142 mep->desc = NULL; in mtu3_ep_disable()
143 mep->ep.desc = NULL; in mtu3_ep_disable()
144 mep->comp_desc = NULL; in mtu3_ep_disable()
145 mep->type = 0; in mtu3_ep_disable()
146 mep->flags = 0; in mtu3_ep_disable()
157 int ret = -EINVAL; in mtu3_gadget_ep_enable()
159 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) { in mtu3_gadget_ep_enable()
161 return -EINVAL; in mtu3_gadget_ep_enable()
164 if (!desc->wMaxPacketSize) { in mtu3_gadget_ep_enable()
166 return -EINVAL; in mtu3_gadget_ep_enable()
169 mtu = mep->mtu; in mtu3_gadget_ep_enable()
172 if (usb_endpoint_num(desc) != mep->epnum) in mtu3_gadget_ep_enable()
173 return -EINVAL; in mtu3_gadget_ep_enable()
175 if (!!usb_endpoint_dir_in(desc) ^ !!mep->is_in) in mtu3_gadget_ep_enable()
176 return -EINVAL; in mtu3_gadget_ep_enable()
178 dev_dbg(mtu->dev, "%s %s\n", __func__, ep->name); in mtu3_gadget_ep_enable()
180 if (mep->flags & MTU3_EP_ENABLED) { in mtu3_gadget_ep_enable()
181 dev_WARN_ONCE(mtu->dev, true, "%s is already enabled\n", in mtu3_gadget_ep_enable()
182 mep->name); in mtu3_gadget_ep_enable()
186 spin_lock_irqsave(&mtu->lock, flags); in mtu3_gadget_ep_enable()
187 mep->desc = desc; in mtu3_gadget_ep_enable()
188 mep->comp_desc = ep->comp_desc; in mtu3_gadget_ep_enable()
194 mep->flags = MTU3_EP_ENABLED; in mtu3_gadget_ep_enable()
195 mtu->active_ep++; in mtu3_gadget_ep_enable()
198 spin_unlock_irqrestore(&mtu->lock, flags); in mtu3_gadget_ep_enable()
200 dev_dbg(mtu->dev, "%s active_ep=%d\n", __func__, mtu->active_ep); in mtu3_gadget_ep_enable()
209 struct mtu3 *mtu = mep->mtu; in mtu3_gadget_ep_disable()
212 dev_dbg(mtu->dev, "%s %s\n", __func__, mep->name); in mtu3_gadget_ep_disable()
215 if (!(mep->flags & MTU3_EP_ENABLED)) { in mtu3_gadget_ep_disable()
216 dev_warn(mtu->dev, "%s is already disabled\n", mep->name); in mtu3_gadget_ep_disable()
220 spin_lock_irqsave(&mtu->lock, flags); in mtu3_gadget_ep_disable()
222 mep->flags = 0; in mtu3_gadget_ep_disable()
223 mtu->active_ep--; in mtu3_gadget_ep_disable()
224 spin_unlock_irqrestore(&(mtu->lock), flags); in mtu3_gadget_ep_disable()
226 dev_dbg(mtu->dev, "%s active_ep=%d, mtu3 is_active=%d\n", in mtu3_gadget_ep_disable()
227 __func__, mtu->active_ep, mtu->is_active); in mtu3_gadget_ep_disable()
241 mreq->request.dma = DMA_ADDR_INVALID; in mtu3_alloc_request()
242 mreq->epnum = mep->epnum; in mtu3_alloc_request()
243 mreq->mep = mep; in mtu3_alloc_request()
244 INIT_LIST_HEAD(&mreq->list); in mtu3_alloc_request()
247 return &mreq->request; in mtu3_alloc_request()
263 struct mtu3 *mtu = mep->mtu; in mtu3_gadget_queue()
267 if (!req->buf) in mtu3_gadget_queue()
268 return -ENODATA; in mtu3_gadget_queue()
270 if (mreq->mep != mep) in mtu3_gadget_queue()
271 return -EINVAL; in mtu3_gadget_queue()
273 dev_dbg(mtu->dev, "%s %s EP%d(%s), req=%p, maxp=%d, len#%d\n", in mtu3_gadget_queue()
274 __func__, mep->is_in ? "TX" : "RX", mreq->epnum, ep->name, in mtu3_gadget_queue()
275 mreq, ep->maxpacket, mreq->request.length); in mtu3_gadget_queue()
277 if (req->length > GPD_BUF_SIZE || in mtu3_gadget_queue()
278 (mtu->gen2cp && req->length > GPD_BUF_SIZE_EL)) { in mtu3_gadget_queue()
279 dev_warn(mtu->dev, in mtu3_gadget_queue()
280 "req length > supported MAX:%d requested:%d\n", in mtu3_gadget_queue()
281 mtu->gen2cp ? GPD_BUF_SIZE_EL : GPD_BUF_SIZE, in mtu3_gadget_queue()
282 req->length); in mtu3_gadget_queue()
283 return -EOPNOTSUPP; in mtu3_gadget_queue()
287 if (!mep->desc) { in mtu3_gadget_queue()
288 dev_dbg(mtu->dev, "req=%p queued to %s while it's disabled\n", in mtu3_gadget_queue()
289 req, ep->name); in mtu3_gadget_queue()
290 return -ESHUTDOWN; in mtu3_gadget_queue()
293 mreq->mtu = mtu; in mtu3_gadget_queue()
294 mreq->request.actual = 0; in mtu3_gadget_queue()
295 mreq->request.status = -EINPROGRESS; in mtu3_gadget_queue()
297 ret = usb_gadget_map_request(&mtu->g, req, mep->is_in); in mtu3_gadget_queue()
299 dev_err(mtu->dev, "dma mapping failed\n"); in mtu3_gadget_queue()
303 spin_lock_irqsave(&mtu->lock, flags); in mtu3_gadget_queue()
306 ret = -EAGAIN; in mtu3_gadget_queue()
310 list_add_tail(&mreq->list, &mep->req_list); in mtu3_gadget_queue()
315 spin_unlock_irqrestore(&mtu->lock, flags); in mtu3_gadget_queue()
326 struct mtu3 *mtu = mep->mtu; in mtu3_gadget_dequeue()
330 if (mreq->mep != mep) in mtu3_gadget_dequeue()
331 return -EINVAL; in mtu3_gadget_dequeue()
333 dev_dbg(mtu->dev, "%s : req=%p\n", __func__, req); in mtu3_gadget_dequeue()
336 spin_lock_irqsave(&mtu->lock, flags); in mtu3_gadget_dequeue()
338 list_for_each_entry(r, &mep->req_list, list) { in mtu3_gadget_dequeue()
343 dev_dbg(mtu->dev, "req=%p not queued to %s\n", req, ep->name); in mtu3_gadget_dequeue()
344 ret = -EINVAL; in mtu3_gadget_dequeue()
349 mtu3_req_complete(mep, req, -ECONNRESET); in mtu3_gadget_dequeue()
353 spin_unlock_irqrestore(&mtu->lock, flags); in mtu3_gadget_dequeue()
360 * A halted EP won't TX/RX any data but will queue requests.
365 struct mtu3 *mtu = mep->mtu; in mtu3_gadget_ep_set_halt()
370 dev_dbg(mtu->dev, "%s : %s...", __func__, ep->name); in mtu3_gadget_ep_set_halt()
372 spin_lock_irqsave(&mtu->lock, flags); in mtu3_gadget_ep_set_halt()
374 if (mep->type == USB_ENDPOINT_XFER_ISOC) { in mtu3_gadget_ep_set_halt()
375 ret = -EINVAL; in mtu3_gadget_ep_set_halt()
382 * If there is not request for TX-EP, QMU will not transfer in mtu3_gadget_ep_set_halt()
383 * data to TX-FIFO, so no need check whether TX-FIFO in mtu3_gadget_ep_set_halt()
387 dev_dbg(mtu->dev, "req in progress, cannot halt %s\n", in mtu3_gadget_ep_set_halt()
388 ep->name); in mtu3_gadget_ep_set_halt()
389 ret = -EAGAIN; in mtu3_gadget_ep_set_halt()
393 mep->flags &= ~MTU3_EP_WEDGE; in mtu3_gadget_ep_set_halt()
396 dev_dbg(mtu->dev, "%s %s stall\n", ep->name, value ? "set" : "clear"); in mtu3_gadget_ep_set_halt()
401 spin_unlock_irqrestore(&mtu->lock, flags); in mtu3_gadget_ep_set_halt()
412 mep->flags |= MTU3_EP_WEDGE; in mtu3_gadget_ep_set_wedge()
432 return (int)mtu3_readl(mtu->mac_base, U3D_USB20_FRAME_NUM); in mtu3_gadget_get_frame()
437 mtu3_writel(mtu->mac_base, U3D_DEV_NOTIF_0, in function_wake_notif()
439 mtu3_setbits(mtu->mac_base, U3D_DEV_NOTIF_0, SEND_DEV_NOTIF); in function_wake_notif()
447 dev_dbg(mtu->dev, "%s\n", __func__); in mtu3_gadget_wakeup()
450 if (!mtu->may_wakeup) in mtu3_gadget_wakeup()
451 return -EOPNOTSUPP; in mtu3_gadget_wakeup()
453 spin_lock_irqsave(&mtu->lock, flags); in mtu3_gadget_wakeup()
454 if (mtu->g.speed >= USB_SPEED_SUPER) { in mtu3_gadget_wakeup()
459 mtu3_setbits(mtu->mac_base, U3D_LINK_POWER_CONTROL, UX_EXIT); in mtu3_gadget_wakeup()
468 mtu3_setbits(mtu->mac_base, U3D_POWER_MANAGEMENT, RESUME); in mtu3_gadget_wakeup()
469 spin_unlock_irqrestore(&mtu->lock, flags); in mtu3_gadget_wakeup()
471 spin_lock_irqsave(&mtu->lock, flags); in mtu3_gadget_wakeup()
472 mtu3_clrbits(mtu->mac_base, U3D_POWER_MANAGEMENT, RESUME); in mtu3_gadget_wakeup()
474 spin_unlock_irqrestore(&mtu->lock, flags); in mtu3_gadget_wakeup()
483 mtu->is_self_powered = !!is_selfpowered; in mtu3_gadget_set_self_powered()
492 dev_dbg(mtu->dev, "%s (%s) for %sactive device\n", __func__, in mtu3_gadget_pullup()
493 is_on ? "on" : "off", mtu->is_active ? "" : "in"); in mtu3_gadget_pullup()
495 pm_runtime_get_sync(mtu->dev); in mtu3_gadget_pullup()
498 spin_lock_irqsave(&mtu->lock, flags); in mtu3_gadget_pullup()
501 if (!mtu->is_active) { in mtu3_gadget_pullup()
503 mtu->softconnect = is_on; in mtu3_gadget_pullup()
504 } else if (is_on != mtu->softconnect) { in mtu3_gadget_pullup()
505 mtu->softconnect = is_on; in mtu3_gadget_pullup()
509 spin_unlock_irqrestore(&mtu->lock, flags); in mtu3_gadget_pullup()
510 pm_runtime_put(mtu->dev); in mtu3_gadget_pullup()
521 if (mtu->gadget_driver) { in mtu3_gadget_start()
522 dev_err(mtu->dev, "%s is already bound to %s\n", in mtu3_gadget_start()
523 mtu->g.name, mtu->gadget_driver->driver.name); in mtu3_gadget_start()
524 return -EBUSY; in mtu3_gadget_start()
527 dev_dbg(mtu->dev, "bind driver %s\n", driver->function); in mtu3_gadget_start()
528 pm_runtime_get_sync(mtu->dev); in mtu3_gadget_start()
530 spin_lock_irqsave(&mtu->lock, flags); in mtu3_gadget_start()
532 mtu->softconnect = 0; in mtu3_gadget_start()
533 mtu->gadget_driver = driver; in mtu3_gadget_start()
535 if (mtu->ssusb->dr_mode == USB_DR_MODE_PERIPHERAL) in mtu3_gadget_start()
538 spin_unlock_irqrestore(&mtu->lock, flags); in mtu3_gadget_start()
539 pm_runtime_put(mtu->dev); in mtu3_gadget_start()
546 struct usb_gadget_driver *driver = mtu->gadget_driver; in stop_activity()
550 if (mtu->g.speed == USB_SPEED_UNKNOWN) in stop_activity()
553 mtu->g.speed = USB_SPEED_UNKNOWN; in stop_activity()
556 if (mtu->softconnect) { in stop_activity()
557 mtu->softconnect = 0; in stop_activity()
565 nuke(mtu->ep0, -ESHUTDOWN); in stop_activity()
566 for (i = 1; i < mtu->num_eps; i++) { in stop_activity()
567 nuke(mtu->in_eps + i, -ESHUTDOWN); in stop_activity()
568 nuke(mtu->out_eps + i, -ESHUTDOWN); in stop_activity()
572 spin_unlock(&mtu->lock); in stop_activity()
573 driver->disconnect(&mtu->g); in stop_activity()
574 spin_lock(&mtu->lock); in stop_activity()
583 dev_dbg(mtu->dev, "%s\n", __func__); in mtu3_gadget_stop()
585 spin_lock_irqsave(&mtu->lock, flags); in mtu3_gadget_stop()
588 mtu->gadget_driver = NULL; in mtu3_gadget_stop()
590 if (mtu->ssusb->dr_mode == USB_DR_MODE_PERIPHERAL) in mtu3_gadget_stop()
593 spin_unlock_irqrestore(&mtu->lock, flags); in mtu3_gadget_stop()
595 synchronize_irq(mtu->irq); in mtu3_gadget_stop()
605 dev_dbg(mtu->dev, "%s %s\n", __func__, usb_speed_string(speed)); in mtu3_gadget_set_speed()
607 spin_lock_irqsave(&mtu->lock, flags); in mtu3_gadget_set_speed()
608 mtu->speed = speed; in mtu3_gadget_set_speed()
609 spin_unlock_irqrestore(&mtu->lock, flags); in mtu3_gadget_set_speed()
617 dev_dbg(mtu->dev, "%s %s\n", __func__, enable ? "en" : "dis"); in mtu3_gadget_async_callbacks()
619 spin_lock_irqsave(&mtu->lock, flags); in mtu3_gadget_async_callbacks()
620 mtu->async_callbacks = enable; in mtu3_gadget_async_callbacks()
621 spin_unlock_irqrestore(&mtu->lock, flags); in mtu3_gadget_async_callbacks()
637 mtu->address = 0; in mtu3_state_reset()
638 mtu->ep0_state = MU3D_EP0_STATE_SETUP; in mtu3_state_reset()
639 mtu->may_wakeup = 0; in mtu3_state_reset()
640 mtu->u1_enable = 0; in mtu3_state_reset()
641 mtu->u2_enable = 0; in mtu3_state_reset()
642 mtu->delayed_status = false; in mtu3_state_reset()
643 mtu->test_mode = false; in mtu3_state_reset()
649 mep->epnum = epnum; in init_hw_ep()
650 mep->mtu = mtu; in init_hw_ep()
651 mep->is_in = is_in; in init_hw_ep()
653 INIT_LIST_HEAD(&mep->req_list); in init_hw_ep()
655 sprintf(mep->name, "ep%d%s", epnum, in init_hw_ep()
658 mep->ep.name = mep->name; in init_hw_ep()
659 INIT_LIST_HEAD(&mep->ep.ep_list); in init_hw_ep()
663 usb_ep_set_maxpacket_limit(&mep->ep, 512); in init_hw_ep()
664 mep->ep.caps.type_control = true; in init_hw_ep()
665 mep->ep.ops = &mtu3_ep0_ops; in init_hw_ep()
666 mtu->g.ep0 = &mep->ep; in init_hw_ep()
668 usb_ep_set_maxpacket_limit(&mep->ep, 1024); in init_hw_ep()
669 mep->ep.caps.type_iso = true; in init_hw_ep()
670 mep->ep.caps.type_bulk = true; in init_hw_ep()
671 mep->ep.caps.type_int = true; in init_hw_ep()
672 mep->ep.ops = &mtu3_ep_ops; in init_hw_ep()
673 list_add_tail(&mep->ep.ep_list, &mtu->g.ep_list); in init_hw_ep()
676 dev_dbg(mtu->dev, "%s, name=%s, maxp=%d\n", __func__, mep->ep.name, in init_hw_ep()
677 mep->ep.maxpacket); in init_hw_ep()
680 mep->ep.caps.dir_in = true; in init_hw_ep()
681 mep->ep.caps.dir_out = true; in init_hw_ep()
683 mep->ep.caps.dir_in = true; in init_hw_ep()
685 mep->ep.caps.dir_out = true; in init_hw_ep()
694 INIT_LIST_HEAD(&(mtu->g.ep_list)); in mtu3_gadget_init_eps()
696 dev_dbg(mtu->dev, "%s num_eps(1 for a pair of tx&rx ep)=%d\n", in mtu3_gadget_init_eps()
697 __func__, mtu->num_eps); in mtu3_gadget_init_eps()
699 init_hw_ep(mtu, mtu->ep0, 0, 0); in mtu3_gadget_init_eps()
700 for (epnum = 1; epnum < mtu->num_eps; epnum++) { in mtu3_gadget_init_eps()
701 init_hw_ep(mtu, mtu->in_eps + epnum, epnum, 1); in mtu3_gadget_init_eps()
702 init_hw_ep(mtu, mtu->out_eps + epnum, epnum, 0); in mtu3_gadget_init_eps()
708 mtu->g.ops = &mtu3_gadget_ops; in mtu3_gadget_setup()
709 mtu->g.max_speed = mtu->max_speed; in mtu3_gadget_setup()
710 mtu->g.speed = USB_SPEED_UNKNOWN; in mtu3_gadget_setup()
711 mtu->g.sg_supported = 0; in mtu3_gadget_setup()
712 mtu->g.name = MTU3_DRIVER_NAME; in mtu3_gadget_setup()
713 mtu->g.irq = mtu->irq; in mtu3_gadget_setup()
714 mtu->is_active = 0; in mtu3_gadget_setup()
715 mtu->delayed_status = false; in mtu3_gadget_setup()
719 return usb_add_gadget_udc(mtu->dev, &mtu->g); in mtu3_gadget_setup()
724 usb_del_gadget_udc(&mtu->g); in mtu3_gadget_cleanup()
729 dev_dbg(mtu->dev, "gadget RESUME\n"); in mtu3_gadget_resume()
730 if (mtu->async_callbacks && mtu->gadget_driver && mtu->gadget_driver->resume) { in mtu3_gadget_resume()
731 spin_unlock(&mtu->lock); in mtu3_gadget_resume()
732 mtu->gadget_driver->resume(&mtu->g); in mtu3_gadget_resume()
733 spin_lock(&mtu->lock); in mtu3_gadget_resume()
740 dev_dbg(mtu->dev, "gadget SUSPEND\n"); in mtu3_gadget_suspend()
741 if (mtu->async_callbacks && mtu->gadget_driver && mtu->gadget_driver->suspend) { in mtu3_gadget_suspend()
742 spin_unlock(&mtu->lock); in mtu3_gadget_suspend()
743 mtu->gadget_driver->suspend(&mtu->g); in mtu3_gadget_suspend()
744 spin_lock(&mtu->lock); in mtu3_gadget_suspend()
751 dev_dbg(mtu->dev, "gadget DISCONNECT\n"); in mtu3_gadget_disconnect()
752 if (mtu->async_callbacks && mtu->gadget_driver && mtu->gadget_driver->disconnect) { in mtu3_gadget_disconnect()
753 spin_unlock(&mtu->lock); in mtu3_gadget_disconnect()
754 mtu->gadget_driver->disconnect(&mtu->g); in mtu3_gadget_disconnect()
755 spin_lock(&mtu->lock); in mtu3_gadget_disconnect()
759 usb_gadget_set_state(&mtu->g, USB_STATE_NOTATTACHED); in mtu3_gadget_disconnect()
764 dev_dbg(mtu->dev, "gadget RESET\n"); in mtu3_gadget_reset()
767 if (mtu->g.speed != USB_SPEED_UNKNOWN) in mtu3_gadget_reset()