usb: dwc3: Add function suspend and function wakeup support
authorElson Roy Serrao <quic_eserrao@quicinc.com>
Fri, 24 Mar 2023 21:48:00 +0000 (14:48 -0700)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Wed, 29 Mar 2023 08:27:01 +0000 (10:27 +0200)
USB host sends function suspend and function resume notifications to
the interface through SET_FEATURE/CLEAR_FEATURE setup packets.
Add support to handle these packets by delegating the requests to
composite layer. Also add support to handle function wake notification
requests to exit from function suspend state.

Acked-by: Thinh Nguyen <Thinh.Nguyen@synopsys.com>
Signed-off-by: Elson Roy Serrao <quic_eserrao@quicinc.com>
Link: https://lore.kernel.org/r/1679694482-16430-5-git-send-email-quic_eserrao@quicinc.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/usb/dwc3/core.h
drivers/usb/dwc3/debug.h
drivers/usb/dwc3/ep0.c
drivers/usb/dwc3/gadget.c

index 8541708..26c8efe 100644 (file)
 #define DWC3_DGCMD_SET_ENDPOINT_NRDY   0x0c
 #define DWC3_DGCMD_SET_ENDPOINT_PRIME  0x0d
 #define DWC3_DGCMD_RUN_SOC_BUS_LOOPBACK        0x10
+#define DWC3_DGCMD_DEV_NOTIFICATION    0x07
 
 #define DWC3_DGCMD_STATUS(n)           (((n) >> 12) & 0x0F)
 #define DWC3_DGCMD_CMDACT              BIT(10)
 #define DWC3_DGCMDPAR_TX_FIFO                  BIT(5)
 #define DWC3_DGCMDPAR_LOOPBACK_DIS             (0 << 0)
 #define DWC3_DGCMDPAR_LOOPBACK_ENA             BIT(0)
+#define DWC3_DGCMDPAR_DN_FUNC_WAKE             BIT(0)
+#define DWC3_DGCMDPAR_INTF_SEL(n)              ((n) << 4)
 
 /* Device Endpoint Command Register */
 #define DWC3_DEPCMD_PARAM_SHIFT                16
index 8bb2c9e..09d7038 100644 (file)
@@ -72,6 +72,8 @@ dwc3_gadget_generic_cmd_string(u8 cmd)
                return "Set Endpoint Prime";
        case DWC3_DGCMD_RUN_SOC_BUS_LOOPBACK:
                return "Run SoC Bus Loopback Test";
+       case DWC3_DGCMD_DEV_NOTIFICATION:
+               return "Device Notification";
        default:
                return "UNKNOWN";
        }
index 8aa14a5..953b752 100644 (file)
@@ -30,6 +30,8 @@
 static void __dwc3_ep0_do_control_status(struct dwc3 *dwc, struct dwc3_ep *dep);
 static void __dwc3_ep0_do_control_data(struct dwc3 *dwc,
                struct dwc3_ep *dep, struct dwc3_request *req);
+static int dwc3_ep0_delegate_req(struct dwc3 *dwc,
+                                struct usb_ctrlrequest *ctrl);
 
 static void dwc3_ep0_prepare_one_trb(struct dwc3_ep *dep,
                dma_addr_t buf_dma, u32 len, u32 type, bool chain)
@@ -368,7 +370,7 @@ static int dwc3_ep0_handle_status(struct dwc3 *dwc,
                 * Function Remote Wake Capable D0
                 * Function Remote Wakeup       D1
                 */
-               break;
+               return dwc3_ep0_delegate_req(dwc, ctrl);
 
        case USB_RECIP_ENDPOINT:
                dep = dwc3_wIndex_to_dep(dwc, ctrl->wIndex);
@@ -517,13 +519,7 @@ static int dwc3_ep0_handle_intf(struct dwc3 *dwc,
 
        switch (wValue) {
        case USB_INTRF_FUNC_SUSPEND:
-               /*
-                * REVISIT: Ideally we would enable some low power mode here,
-                * however it's unclear what we should be doing here.
-                *
-                * For now, we're not doing anything, just making sure we return
-                * 0 so USB Command Verifier tests pass without any errors.
-                */
+               ret = dwc3_ep0_delegate_req(dwc, ctrl);
                break;
        default:
                ret = -EINVAL;
index 7ceff08..287ea6e 100644 (file)
@@ -2395,6 +2395,47 @@ static int dwc3_gadget_wakeup(struct usb_gadget *g)
        return ret;
 }
 
+static void dwc3_resume_gadget(struct dwc3 *dwc);
+
+static int dwc3_gadget_func_wakeup(struct usb_gadget *g, int intf_id)
+{
+       struct  dwc3            *dwc = gadget_to_dwc(g);
+       unsigned long           flags;
+       int                     ret;
+       int                     link_state;
+
+       if (!dwc->wakeup_configured) {
+               dev_err(dwc->dev, "remote wakeup not configured\n");
+               return -EINVAL;
+       }
+
+       spin_lock_irqsave(&dwc->lock, flags);
+       /*
+        * If the link is in U3, signal for remote wakeup and wait for the
+        * link to transition to U0 before sending device notification.
+        */
+       link_state = dwc3_gadget_get_link_state(dwc);
+       if (link_state == DWC3_LINK_STATE_U3) {
+               ret = __dwc3_gadget_wakeup(dwc, false);
+               if (ret) {
+                       spin_unlock_irqrestore(&dwc->lock, flags);
+                       return -EINVAL;
+               }
+               dwc3_resume_gadget(dwc);
+               dwc->link_state = DWC3_LINK_STATE_U0;
+       }
+
+       ret = dwc3_send_gadget_generic_command(dwc, DWC3_DGCMD_DEV_NOTIFICATION,
+                                              DWC3_DGCMDPAR_DN_FUNC_WAKE |
+                                              DWC3_DGCMDPAR_INTF_SEL(intf_id));
+       if (ret)
+               dev_err(dwc->dev, "function remote wakeup failed, ret:%d\n", ret);
+
+       spin_unlock_irqrestore(&dwc->lock, flags);
+
+       return ret;
+}
+
 static int dwc3_gadget_set_remote_wakeup(struct usb_gadget *g, int set)
 {
        struct dwc3             *dwc = gadget_to_dwc(g);
@@ -3032,6 +3073,7 @@ static void dwc3_gadget_async_callbacks(struct usb_gadget *g, bool enable)
 static const struct usb_gadget_ops dwc3_gadget_ops = {
        .get_frame              = dwc3_gadget_get_frame,
        .wakeup                 = dwc3_gadget_wakeup,
+       .func_wakeup            = dwc3_gadget_func_wakeup,
        .set_remote_wakeup      = dwc3_gadget_set_remote_wakeup,
        .set_selfpowered        = dwc3_gadget_set_selfpowered,
        .pullup                 = dwc3_gadget_pullup,