1 // SPDX-License-Identifier: GPL-2.0
3 * mtu3_gadget.c - MediaTek usb3 DRD peripheral support
5 * Copyright (C) 2016 MediaTek Inc.
7 * Author: Chunfeng Yun <chunfeng.yun@mediatek.com>
12 void mtu3_req_complete(struct mtu3_ep
*mep
,
13 struct usb_request
*req
, int status
)
14 __releases(mep
->mtu
->lock
)
15 __acquires(mep
->mtu
->lock
)
17 struct mtu3_request
*mreq
;
21 mreq
= to_mtu3_request(req
);
22 list_del(&mreq
->list
);
23 if (mreq
->request
.status
== -EINPROGRESS
)
24 mreq
->request
.status
= status
;
28 spin_unlock(&mtu
->lock
);
30 /* ep0 makes use of PIO, needn't unmap it */
32 usb_gadget_unmap_request(&mtu
->g
, req
, mep
->is_in
);
34 dev_dbg(mtu
->dev
, "%s complete req: %p, sts %d, %d/%d\n", mep
->name
,
35 req
, req
->status
, mreq
->request
.actual
, mreq
->request
.length
);
37 usb_gadget_giveback_request(&mep
->ep
, &mreq
->request
);
39 spin_lock(&mtu
->lock
);
43 static void nuke(struct mtu3_ep
*mep
, const int status
)
45 struct mtu3_request
*mreq
= NULL
;
48 if (list_empty(&mep
->req_list
))
51 dev_dbg(mep
->mtu
->dev
, "abort %s's req: sts %d\n", mep
->name
, status
);
57 while (!list_empty(&mep
->req_list
)) {
58 mreq
= list_first_entry(&mep
->req_list
,
59 struct mtu3_request
, list
);
60 mtu3_req_complete(mep
, &mreq
->request
, status
);
64 static int mtu3_ep_enable(struct mtu3_ep
*mep
)
66 const struct usb_endpoint_descriptor
*desc
;
67 const struct usb_ss_ep_comp_descriptor
*comp_desc
;
68 struct mtu3
*mtu
= mep
->mtu
;
76 comp_desc
= mep
->comp_desc
;
77 mep
->type
= usb_endpoint_type(desc
);
78 max_packet
= usb_endpoint_maxp(desc
);
79 mep
->maxp
= max_packet
& GENMASK(10, 0);
81 switch (mtu
->g
.speed
) {
83 case USB_SPEED_SUPER_PLUS
:
84 if (usb_endpoint_xfer_int(desc
) ||
85 usb_endpoint_xfer_isoc(desc
)) {
86 interval
= desc
->bInterval
;
87 interval
= clamp_val(interval
, 1, 16) - 1;
88 if (usb_endpoint_xfer_isoc(desc
) && comp_desc
)
89 mult
= comp_desc
->bmAttributes
;
92 burst
= comp_desc
->bMaxBurst
;
96 if (usb_endpoint_xfer_isoc(desc
) ||
97 usb_endpoint_xfer_int(desc
)) {
98 interval
= desc
->bInterval
;
99 interval
= clamp_val(interval
, 1, 16) - 1;
100 burst
= (max_packet
& GENMASK(12, 11)) >> 11;
104 break; /*others are ignored */
107 dev_dbg(mtu
->dev
, "%s maxp:%d, interval:%d, burst:%d, mult:%d\n",
108 __func__
, mep
->maxp
, interval
, burst
, mult
);
110 mep
->ep
.maxpacket
= mep
->maxp
;
112 mep
->ep
.comp_desc
= comp_desc
;
114 /* slot mainly affects bulk/isoc transfer, so ignore int */
115 mep
->slot
= usb_endpoint_xfer_int(desc
) ? 0 : mtu
->slot
;
117 ret
= mtu3_config_ep(mtu
, mep
, interval
, burst
, mult
);
121 ret
= mtu3_gpd_ring_alloc(mep
);
123 mtu3_deconfig_ep(mtu
, mep
);
132 static int mtu3_ep_disable(struct mtu3_ep
*mep
)
134 struct mtu3
*mtu
= mep
->mtu
;
138 /* abort all pending requests */
139 nuke(mep
, -ESHUTDOWN
);
140 mtu3_deconfig_ep(mtu
, mep
);
141 mtu3_gpd_ring_free(mep
);
145 mep
->comp_desc
= NULL
;
152 static int mtu3_gadget_ep_enable(struct usb_ep
*ep
,
153 const struct usb_endpoint_descriptor
*desc
)
160 if (!ep
|| !desc
|| desc
->bDescriptorType
!= USB_DT_ENDPOINT
) {
161 pr_debug("%s invalid parameters\n", __func__
);
165 if (!desc
->wMaxPacketSize
) {
166 pr_debug("%s missing wMaxPacketSize\n", __func__
);
169 mep
= to_mtu3_ep(ep
);
172 /* check ep number and direction against endpoint */
173 if (usb_endpoint_num(desc
) != mep
->epnum
)
176 if (!!usb_endpoint_dir_in(desc
) ^ !!mep
->is_in
)
179 dev_dbg(mtu
->dev
, "%s %s\n", __func__
, ep
->name
);
181 if (mep
->flags
& MTU3_EP_ENABLED
) {
182 dev_WARN_ONCE(mtu
->dev
, true, "%s is already enabled\n",
187 spin_lock_irqsave(&mtu
->lock
, flags
);
189 mep
->comp_desc
= ep
->comp_desc
;
191 ret
= mtu3_ep_enable(mep
);
197 mep
->flags
|= MTU3_EP_ENABLED
;
201 spin_unlock_irqrestore(&mtu
->lock
, flags
);
203 dev_dbg(mtu
->dev
, "%s active_ep=%d\n", __func__
, mtu
->active_ep
);
208 static int mtu3_gadget_ep_disable(struct usb_ep
*ep
)
210 struct mtu3_ep
*mep
= to_mtu3_ep(ep
);
211 struct mtu3
*mtu
= mep
->mtu
;
214 dev_dbg(mtu
->dev
, "%s %s\n", __func__
, mep
->name
);
216 if (!(mep
->flags
& MTU3_EP_ENABLED
)) {
217 dev_warn(mtu
->dev
, "%s is already disabled\n", mep
->name
);
221 spin_lock_irqsave(&mtu
->lock
, flags
);
222 mtu3_ep_disable(mep
);
223 mep
->flags
&= ~MTU3_EP_ENABLED
;
225 spin_unlock_irqrestore(&(mtu
->lock
), flags
);
227 dev_dbg(mtu
->dev
, "%s active_ep=%d, mtu3 is_active=%d\n",
228 __func__
, mtu
->active_ep
, mtu
->is_active
);
233 struct usb_request
*mtu3_alloc_request(struct usb_ep
*ep
, gfp_t gfp_flags
)
235 struct mtu3_ep
*mep
= to_mtu3_ep(ep
);
236 struct mtu3_request
*mreq
;
238 mreq
= kzalloc(sizeof(*mreq
), gfp_flags
);
242 mreq
->request
.dma
= DMA_ADDR_INVALID
;
243 mreq
->epnum
= mep
->epnum
;
246 return &mreq
->request
;
249 void mtu3_free_request(struct usb_ep
*ep
, struct usb_request
*req
)
251 kfree(to_mtu3_request(req
));
254 static int mtu3_gadget_queue(struct usb_ep
*ep
,
255 struct usb_request
*req
, gfp_t gfp_flags
)
258 struct mtu3_request
*mreq
;
269 mep
= to_mtu3_ep(ep
);
271 mreq
= to_mtu3_request(req
);
274 if (mreq
->mep
!= mep
)
277 dev_dbg(mtu
->dev
, "%s %s EP%d(%s), req=%p, maxp=%d, len#%d\n",
278 __func__
, mep
->is_in
? "TX" : "RX", mreq
->epnum
, ep
->name
,
279 mreq
, ep
->maxpacket
, mreq
->request
.length
);
281 if (req
->length
> GPD_BUF_SIZE
) {
283 "req length > supported MAX:%d requested:%d\n",
284 GPD_BUF_SIZE
, req
->length
);
288 /* don't queue if the ep is down */
290 dev_dbg(mtu
->dev
, "req=%p queued to %s while it's disabled\n",
295 mreq
->request
.actual
= 0;
296 mreq
->request
.status
= -EINPROGRESS
;
298 ret
= usb_gadget_map_request(&mtu
->g
, req
, mep
->is_in
);
300 dev_err(mtu
->dev
, "dma mapping failed\n");
304 spin_lock_irqsave(&mtu
->lock
, flags
);
306 if (mtu3_prepare_transfer(mep
)) {
311 list_add_tail(&mreq
->list
, &mep
->req_list
);
312 mtu3_insert_gpd(mep
, mreq
);
313 mtu3_qmu_resume(mep
);
316 spin_unlock_irqrestore(&mtu
->lock
, flags
);
321 static int mtu3_gadget_dequeue(struct usb_ep
*ep
, struct usb_request
*req
)
323 struct mtu3_ep
*mep
= to_mtu3_ep(ep
);
324 struct mtu3_request
*mreq
= to_mtu3_request(req
);
325 struct mtu3_request
*r
;
328 struct mtu3
*mtu
= mep
->mtu
;
330 if (!ep
|| !req
|| mreq
->mep
!= mep
)
333 dev_dbg(mtu
->dev
, "%s : req=%p\n", __func__
, req
);
335 spin_lock_irqsave(&mtu
->lock
, flags
);
337 list_for_each_entry(r
, &mep
->req_list
, list
) {
342 dev_dbg(mtu
->dev
, "req=%p not queued to %s\n", req
, ep
->name
);
347 mtu3_qmu_flush(mep
); /* REVISIT: set BPS ?? */
348 mtu3_req_complete(mep
, req
, -ECONNRESET
);
352 spin_unlock_irqrestore(&mtu
->lock
, flags
);
358 * Set or clear the halt bit of an EP.
359 * A halted EP won't TX/RX any data but will queue requests.
361 static int mtu3_gadget_ep_set_halt(struct usb_ep
*ep
, int value
)
363 struct mtu3_ep
*mep
= to_mtu3_ep(ep
);
364 struct mtu3
*mtu
= mep
->mtu
;
365 struct mtu3_request
*mreq
;
372 dev_dbg(mtu
->dev
, "%s : %s...", __func__
, ep
->name
);
374 spin_lock_irqsave(&mtu
->lock
, flags
);
376 if (mep
->type
== USB_ENDPOINT_XFER_ISOC
) {
381 mreq
= next_request(mep
);
384 * If there is not request for TX-EP, QMU will not transfer
385 * data to TX-FIFO, so no need check whether TX-FIFO
386 * holds bytes or not here
389 dev_dbg(mtu
->dev
, "req in progress, cannot halt %s\n",
398 dev_dbg(mtu
->dev
, "%s %s stall\n", ep
->name
, value
? "set" : "clear");
400 mtu3_ep_stall_set(mep
, value
);
403 spin_unlock_irqrestore(&mtu
->lock
, flags
);
408 /* Sets the halt feature with the clear requests ignored */
409 static int mtu3_gadget_ep_set_wedge(struct usb_ep
*ep
)
411 struct mtu3_ep
*mep
= to_mtu3_ep(ep
);
418 return usb_ep_set_halt(ep
);
421 static const struct usb_ep_ops mtu3_ep_ops
= {
422 .enable
= mtu3_gadget_ep_enable
,
423 .disable
= mtu3_gadget_ep_disable
,
424 .alloc_request
= mtu3_alloc_request
,
425 .free_request
= mtu3_free_request
,
426 .queue
= mtu3_gadget_queue
,
427 .dequeue
= mtu3_gadget_dequeue
,
428 .set_halt
= mtu3_gadget_ep_set_halt
,
429 .set_wedge
= mtu3_gadget_ep_set_wedge
,
432 static int mtu3_gadget_get_frame(struct usb_gadget
*gadget
)
434 struct mtu3
*mtu
= gadget_to_mtu3(gadget
);
436 return (int)mtu3_readl(mtu
->mac_base
, U3D_USB20_FRAME_NUM
);
439 static int mtu3_gadget_wakeup(struct usb_gadget
*gadget
)
441 struct mtu3
*mtu
= gadget_to_mtu3(gadget
);
444 dev_dbg(mtu
->dev
, "%s\n", __func__
);
446 /* remote wakeup feature is not enabled by host */
447 if (!mtu
->may_wakeup
)
450 spin_lock_irqsave(&mtu
->lock
, flags
);
451 if (mtu
->g
.speed
>= USB_SPEED_SUPER
) {
452 mtu3_setbits(mtu
->mac_base
, U3D_LINK_POWER_CONTROL
, UX_EXIT
);
454 mtu3_setbits(mtu
->mac_base
, U3D_POWER_MANAGEMENT
, RESUME
);
455 spin_unlock_irqrestore(&mtu
->lock
, flags
);
456 usleep_range(10000, 11000);
457 spin_lock_irqsave(&mtu
->lock
, flags
);
458 mtu3_clrbits(mtu
->mac_base
, U3D_POWER_MANAGEMENT
, RESUME
);
460 spin_unlock_irqrestore(&mtu
->lock
, flags
);
464 static int mtu3_gadget_set_self_powered(struct usb_gadget
*gadget
,
467 struct mtu3
*mtu
= gadget_to_mtu3(gadget
);
469 mtu
->is_self_powered
= !!is_selfpowered
;
473 static int mtu3_gadget_pullup(struct usb_gadget
*gadget
, int is_on
)
475 struct mtu3
*mtu
= gadget_to_mtu3(gadget
);
478 dev_dbg(mtu
->dev
, "%s (%s) for %sactive device\n", __func__
,
479 is_on
? "on" : "off", mtu
->is_active
? "" : "in");
481 /* we'd rather not pullup unless the device is active. */
482 spin_lock_irqsave(&mtu
->lock
, flags
);
485 if (!mtu
->is_active
) {
486 /* save it for mtu3_start() to process the request */
487 mtu
->softconnect
= is_on
;
488 } else if (is_on
!= mtu
->softconnect
) {
489 mtu
->softconnect
= is_on
;
490 mtu3_dev_on_off(mtu
, is_on
);
493 spin_unlock_irqrestore(&mtu
->lock
, flags
);
498 static int mtu3_gadget_start(struct usb_gadget
*gadget
,
499 struct usb_gadget_driver
*driver
)
501 struct mtu3
*mtu
= gadget_to_mtu3(gadget
);
504 if (mtu
->gadget_driver
) {
505 dev_err(mtu
->dev
, "%s is already bound to %s\n",
506 mtu
->g
.name
, mtu
->gadget_driver
->driver
.name
);
510 dev_dbg(mtu
->dev
, "bind driver %s\n", driver
->function
);
512 spin_lock_irqsave(&mtu
->lock
, flags
);
514 mtu
->softconnect
= 0;
515 mtu
->gadget_driver
= driver
;
517 if (mtu
->ssusb
->dr_mode
== USB_DR_MODE_PERIPHERAL
)
520 spin_unlock_irqrestore(&mtu
->lock
, flags
);
525 static void stop_activity(struct mtu3
*mtu
)
527 struct usb_gadget_driver
*driver
= mtu
->gadget_driver
;
530 /* don't disconnect if it's not connected */
531 if (mtu
->g
.speed
== USB_SPEED_UNKNOWN
)
534 mtu
->g
.speed
= USB_SPEED_UNKNOWN
;
536 /* deactivate the hardware */
537 if (mtu
->softconnect
) {
538 mtu
->softconnect
= 0;
539 mtu3_dev_on_off(mtu
, 0);
543 * killing any outstanding requests will quiesce the driver;
544 * then report disconnect
546 nuke(mtu
->ep0
, -ESHUTDOWN
);
547 for (i
= 1; i
< mtu
->num_eps
; i
++) {
548 nuke(mtu
->in_eps
+ i
, -ESHUTDOWN
);
549 nuke(mtu
->out_eps
+ i
, -ESHUTDOWN
);
553 spin_unlock(&mtu
->lock
);
554 driver
->disconnect(&mtu
->g
);
555 spin_lock(&mtu
->lock
);
559 static int mtu3_gadget_stop(struct usb_gadget
*g
)
561 struct mtu3
*mtu
= gadget_to_mtu3(g
);
564 dev_dbg(mtu
->dev
, "%s\n", __func__
);
566 spin_lock_irqsave(&mtu
->lock
, flags
);
569 mtu
->gadget_driver
= NULL
;
571 if (mtu
->ssusb
->dr_mode
== USB_DR_MODE_PERIPHERAL
)
574 spin_unlock_irqrestore(&mtu
->lock
, flags
);
579 static const struct usb_gadget_ops mtu3_gadget_ops
= {
580 .get_frame
= mtu3_gadget_get_frame
,
581 .wakeup
= mtu3_gadget_wakeup
,
582 .set_selfpowered
= mtu3_gadget_set_self_powered
,
583 .pullup
= mtu3_gadget_pullup
,
584 .udc_start
= mtu3_gadget_start
,
585 .udc_stop
= mtu3_gadget_stop
,
588 static void mtu3_state_reset(struct mtu3
*mtu
)
591 mtu
->ep0_state
= MU3D_EP0_STATE_SETUP
;
595 mtu
->delayed_status
= false;
596 mtu
->test_mode
= false;
599 static void init_hw_ep(struct mtu3
*mtu
, struct mtu3_ep
*mep
,
600 u32 epnum
, u32 is_in
)
606 INIT_LIST_HEAD(&mep
->req_list
);
608 sprintf(mep
->name
, "ep%d%s", epnum
,
609 !epnum
? "" : (is_in
? "in" : "out"));
611 mep
->ep
.name
= mep
->name
;
612 INIT_LIST_HEAD(&mep
->ep
.ep_list
);
614 /* initialize maxpacket as SS */
616 usb_ep_set_maxpacket_limit(&mep
->ep
, 512);
617 mep
->ep
.caps
.type_control
= true;
618 mep
->ep
.ops
= &mtu3_ep0_ops
;
619 mtu
->g
.ep0
= &mep
->ep
;
621 usb_ep_set_maxpacket_limit(&mep
->ep
, 1024);
622 mep
->ep
.caps
.type_iso
= true;
623 mep
->ep
.caps
.type_bulk
= true;
624 mep
->ep
.caps
.type_int
= true;
625 mep
->ep
.ops
= &mtu3_ep_ops
;
626 list_add_tail(&mep
->ep
.ep_list
, &mtu
->g
.ep_list
);
629 dev_dbg(mtu
->dev
, "%s, name=%s, maxp=%d\n", __func__
, mep
->ep
.name
,
633 mep
->ep
.caps
.dir_in
= true;
634 mep
->ep
.caps
.dir_out
= true;
636 mep
->ep
.caps
.dir_in
= true;
638 mep
->ep
.caps
.dir_out
= true;
642 static void mtu3_gadget_init_eps(struct mtu3
*mtu
)
646 /* initialize endpoint list just once */
647 INIT_LIST_HEAD(&(mtu
->g
.ep_list
));
649 dev_dbg(mtu
->dev
, "%s num_eps(1 for a pair of tx&rx ep)=%d\n",
650 __func__
, mtu
->num_eps
);
652 init_hw_ep(mtu
, mtu
->ep0
, 0, 0);
653 for (epnum
= 1; epnum
< mtu
->num_eps
; epnum
++) {
654 init_hw_ep(mtu
, mtu
->in_eps
+ epnum
, epnum
, 1);
655 init_hw_ep(mtu
, mtu
->out_eps
+ epnum
, epnum
, 0);
659 int mtu3_gadget_setup(struct mtu3
*mtu
)
663 mtu
->g
.ops
= &mtu3_gadget_ops
;
664 mtu
->g
.max_speed
= mtu
->max_speed
;
665 mtu
->g
.speed
= USB_SPEED_UNKNOWN
;
666 mtu
->g
.sg_supported
= 0;
667 mtu
->g
.name
= MTU3_DRIVER_NAME
;
669 mtu
->delayed_status
= false;
671 mtu3_gadget_init_eps(mtu
);
673 ret
= usb_add_gadget_udc(mtu
->dev
, &mtu
->g
);
675 dev_err(mtu
->dev
, "failed to register udc\n");
680 void mtu3_gadget_cleanup(struct mtu3
*mtu
)
682 usb_del_gadget_udc(&mtu
->g
);
685 void mtu3_gadget_resume(struct mtu3
*mtu
)
687 dev_dbg(mtu
->dev
, "gadget RESUME\n");
688 if (mtu
->gadget_driver
&& mtu
->gadget_driver
->resume
) {
689 spin_unlock(&mtu
->lock
);
690 mtu
->gadget_driver
->resume(&mtu
->g
);
691 spin_lock(&mtu
->lock
);
695 /* called when SOF packets stop for 3+ msec or enters U3 */
696 void mtu3_gadget_suspend(struct mtu3
*mtu
)
698 dev_dbg(mtu
->dev
, "gadget SUSPEND\n");
699 if (mtu
->gadget_driver
&& mtu
->gadget_driver
->suspend
) {
700 spin_unlock(&mtu
->lock
);
701 mtu
->gadget_driver
->suspend(&mtu
->g
);
702 spin_lock(&mtu
->lock
);
706 /* called when VBUS drops below session threshold, and in other cases */
707 void mtu3_gadget_disconnect(struct mtu3
*mtu
)
709 dev_dbg(mtu
->dev
, "gadget DISCONNECT\n");
710 if (mtu
->gadget_driver
&& mtu
->gadget_driver
->disconnect
) {
711 spin_unlock(&mtu
->lock
);
712 mtu
->gadget_driver
->disconnect(&mtu
->g
);
713 spin_lock(&mtu
->lock
);
716 mtu3_state_reset(mtu
);
717 usb_gadget_set_state(&mtu
->g
, USB_STATE_NOTATTACHED
);
720 void mtu3_gadget_reset(struct mtu3
*mtu
)
722 dev_dbg(mtu
->dev
, "gadget RESET\n");
724 /* report disconnect, if we didn't flush EP state */
725 if (mtu
->g
.speed
!= USB_SPEED_UNKNOWN
)
726 mtu3_gadget_disconnect(mtu
);
728 mtu3_state_reset(mtu
);