hc
2024-02-19 1c055e55a242a33e574e48be530e06770a210dcd
kernel/drivers/usb/dwc3/gadget.c
....@@ -180,6 +180,7 @@
180180 list_del(&req->list);
181181 req->remaining = 0;
182182 req->needs_extra_trb = false;
183
+ req->num_trbs = 0;
183184
184185 if (req->request.status == -EINPROGRESS)
185186 req->request.status = status;
....@@ -1823,6 +1824,7 @@
18231824 */
18241825 static int __dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force, bool interrupt)
18251826 {
1827
+ struct dwc3 *dwc = dep->dwc;
18261828 struct dwc3_gadget_ep_cmd_params params;
18271829 u32 cmd;
18281830 int ret;
....@@ -1846,11 +1848,15 @@
18461848 WARN_ON_ONCE(ret);
18471849 dep->resource_index = 0;
18481850
1849
- if (!interrupt)
1851
+ if (!interrupt) {
1852
+ if (!DWC3_IP_IS(DWC3) || DWC3_VER_IS_PRIOR(DWC3, 310A))
1853
+ mdelay(1);
18501854 dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
1851
- else
1855
+ } else {
18521856 dep->flags |= DWC3_EP_END_TRANSFER_PENDING;
1857
+ }
18531858
1859
+ dep->flags &= ~DWC3_EP_DELAY_STOP;
18541860 return ret;
18551861 }
18561862
....@@ -2656,30 +2662,17 @@
26562662 static int dwc3_gadget_soft_disconnect(struct dwc3 *dwc)
26572663 {
26582664 unsigned long flags;
2665
+ int ret;
26592666
26602667 spin_lock_irqsave(&dwc->lock, flags);
26612668 dwc->connected = false;
26622669
26632670 /*
2664
- * Per databook, when we want to stop the gadget, if a control transfer
2665
- * is still in process, complete it and get the core into setup phase.
2671
+ * Attempt to end pending SETUP status phase, and not wait for the
2672
+ * function to do so.
26662673 */
2667
- if (dwc->ep0state != EP0_SETUP_PHASE &&
2668
- dwc->ep0state != EP0_UNCONNECTED) {
2669
- int ret;
2670
-
2671
- if (dwc->delayed_status)
2672
- dwc3_ep0_send_delayed_status(dwc);
2673
-
2674
- reinit_completion(&dwc->ep0_in_setup);
2675
-
2676
- spin_unlock_irqrestore(&dwc->lock, flags);
2677
- ret = wait_for_completion_timeout(&dwc->ep0_in_setup,
2678
- msecs_to_jiffies(DWC3_PULL_UP_TIMEOUT));
2679
- spin_lock_irqsave(&dwc->lock, flags);
2680
- if (ret == 0)
2681
- dev_warn(dwc->dev, "timed out waiting for SETUP phase\n");
2682
- }
2674
+ if (dwc->delayed_status)
2675
+ dwc3_ep0_send_delayed_status(dwc);
26832676
26842677 /*
26852678 * In the Synopsys DesignWare Cores USB3 Databook Rev. 3.30a
....@@ -2689,8 +2682,35 @@
26892682 * bit.
26902683 */
26912684 dwc3_stop_active_transfers(dwc);
2692
- __dwc3_gadget_stop(dwc);
26932685 spin_unlock_irqrestore(&dwc->lock, flags);
2686
+
2687
+ /*
2688
+ * Per databook, when we want to stop the gadget, if a control transfer
2689
+ * is still in process, complete it and get the core into setup phase.
2690
+ * In case the host is unresponsive to a SETUP transaction, forcefully
2691
+ * stall the transfer, and move back to the SETUP phase, so that any
2692
+ * pending endxfers can be executed.
2693
+ */
2694
+ if (dwc->ep0state != EP0_SETUP_PHASE &&
2695
+ dwc->ep0state != EP0_UNCONNECTED) {
2696
+ reinit_completion(&dwc->ep0_in_setup);
2697
+
2698
+ ret = wait_for_completion_timeout(&dwc->ep0_in_setup,
2699
+ msecs_to_jiffies(DWC3_PULL_UP_TIMEOUT));
2700
+ if (ret == 0) {
2701
+ unsigned int dir;
2702
+
2703
+ dev_warn(dwc->dev, "wait for SETUP phase timed out\n");
2704
+ spin_lock_irqsave(&dwc->lock, flags);
2705
+ dir = !!dwc->ep0_expect_in;
2706
+ if (dwc->ep0state == EP0_DATA_PHASE)
2707
+ dwc3_ep0_end_control_data(dwc, dwc->eps[dir]);
2708
+ else
2709
+ dwc3_ep0_end_control_data(dwc, dwc->eps[!dir]);
2710
+ dwc3_ep0_stall_and_restart(dwc);
2711
+ spin_unlock_irqrestore(&dwc->lock, flags);
2712
+ }
2713
+ }
26942714
26952715 /*
26962716 * Note: if the GEVNTCOUNT indicates events in the event buffer, the
....@@ -2699,7 +2719,19 @@
26992719 * remaining event generated by the controller while polling for
27002720 * DSTS.DEVCTLHLT.
27012721 */
2702
- return dwc3_gadget_run_stop(dwc, false, false);
2722
+ ret = dwc3_gadget_run_stop(dwc, false, false);
2723
+
2724
+ /*
2725
+ * Stop the gadget after controller is halted, so that if needed, the
2726
+ * events to update EP0 state can still occur while the run/stop
2727
+ * routine polls for the halted state. DEVTEN is cleared as part of
2728
+ * gadget stop.
2729
+ */
2730
+ spin_lock_irqsave(&dwc->lock, flags);
2731
+ __dwc3_gadget_stop(dwc);
2732
+ spin_unlock_irqrestore(&dwc->lock, flags);
2733
+
2734
+ return ret;
27032735 }
27042736
27052737 static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
....@@ -2731,7 +2763,9 @@
27312763 ret = pm_runtime_get_sync(dwc->dev);
27322764 if (!ret || ret < 0) {
27332765 pm_runtime_put(dwc->dev);
2734
- return 0;
2766
+ if (ret < 0)
2767
+ pm_runtime_set_suspended(dwc->dev);
2768
+ return ret;
27352769 }
27362770
27372771 if (dwc->pullups_connected == is_on) {
....@@ -2750,13 +2784,16 @@
27502784 * device-initiated disconnect requires a core soft reset
27512785 * (DCTL.CSftRst) before enabling the run/stop bit.
27522786 */
2753
- dwc3_core_soft_reset(dwc);
2787
+ ret = dwc3_core_soft_reset(dwc);
2788
+ if (ret)
2789
+ goto done;
27542790
27552791 dwc3_event_buffers_setup(dwc);
27562792 __dwc3_gadget_start(dwc);
27572793 ret = dwc3_gadget_run_stop(dwc, true, false);
27582794 }
27592795
2796
+done:
27602797 pm_runtime_put(dwc->dev);
27612798
27622799 return ret;
....@@ -3960,8 +3997,10 @@
39603997 if (dep->number <= 1 && dwc->ep0state != EP0_DATA_PHASE)
39613998 return;
39623999
4000
+ if (interrupt && (dep->flags & DWC3_EP_DELAY_STOP))
4001
+ return;
4002
+
39634003 if (!(dep->flags & DWC3_EP_TRANSFER_STARTED) ||
3964
- (dep->flags & DWC3_EP_DELAY_STOP) ||
39654004 (dep->flags & DWC3_EP_END_TRANSFER_PENDING))
39664005 return;
39674006
....@@ -4001,7 +4040,11 @@
40014040 * enabled, the EndTransfer command will have completed upon
40024041 * returning from this function.
40034042 *
4004
- * This mode is NOT available on the DWC_usb31 IP.
4043
+ * This mode is NOT available on the DWC_usb31 IP. In this
4044
+ * case, if the IOC bit is not set, then delay by 1ms
4045
+ * after issuing the EndTransfer command. This allows for the
4046
+ * controller to handle the command completely before DWC3
4047
+ * remove requests attempts to unmap USB request buffers.
40054048 */
40064049
40074050 __dwc3_stop_active_transfer(dep, force, interrupt);
....@@ -4475,15 +4518,8 @@
44754518 break;
44764519 case DWC3_DEVICE_EVENT_SUSPEND:
44774520 /* It changed to be suspend event for version 2.30a and above */
4478
- if (!DWC3_VER_IS_PRIOR(DWC3, 230A)) {
4479
- /*
4480
- * Ignore suspend event until the gadget enters into
4481
- * USB_STATE_CONFIGURED state.
4482
- */
4483
- if (dwc->gadget->state >= USB_STATE_CONFIGURED)
4484
- dwc3_gadget_suspend_interrupt(dwc,
4485
- event->event_info);
4486
- }
4521
+ if (!DWC3_VER_IS_PRIOR(DWC3, 230A))
4522
+ dwc3_gadget_suspend_interrupt(dwc, event->event_info);
44874523 break;
44884524 case DWC3_DEVICE_EVENT_SOF:
44894525 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
....@@ -4580,9 +4616,14 @@
45804616 u32 count;
45814617
45824618 if (pm_runtime_suspended(dwc->dev)) {
4619
+ dwc->pending_events = true;
4620
+ /*
4621
+ * Trigger runtime resume. The get() function will be balanced
4622
+ * after processing the pending events in dwc3_process_pending
4623
+ * events().
4624
+ */
45834625 pm_runtime_get(dwc->dev);
45844626 disable_irq_nosync(dwc->irq_gadget);
4585
- dwc->pending_events = true;
45864627 return IRQ_HANDLED;
45874628 }
45884629
....@@ -4851,6 +4892,8 @@
48514892 {
48524893 if (dwc->pending_events) {
48534894 dwc3_interrupt(dwc->irq_gadget, dwc->ev_buf);
4895
+ dwc3_thread_interrupt(dwc->irq_gadget, dwc->ev_buf);
4896
+ pm_runtime_put(dwc->dev);
48544897 dwc->pending_events = false;
48554898 enable_irq(dwc->irq_gadget);
48564899 }