usb: gadget: Properly configure the device for remote wakeup
authorElson Roy Serrao <quic_eserrao@quicinc.com>
Fri, 24 Mar 2023 21:47:57 +0000 (14:47 -0700)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Wed, 29 Mar 2023 08:27:00 +0000 (10:27 +0200)
The wakeup bit in the bmAttributes field indicates whether the device
is configured for remote wakeup. But this field should be allowed to
set only if the UDC supports such wakeup mechanism. So configure this
field based on UDC capability. Also inform the UDC whether the device
is configured for remote wakeup by implementing a gadget op.

Reviewed-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-2-git-send-email-quic_eserrao@quicinc.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/usb/gadget/composite.c
drivers/usb/gadget/configfs.c
drivers/usb/gadget/udc/core.c
drivers/usb/gadget/udc/trace.h
include/linux/usb/composite.h
include/linux/usb/gadget.h

index f1ecd12b62c3a8f6accf3bcddd4461c8265ecc48..00c89571de2a04e9d70c93a868f9340024754b76 100644 (file)
@@ -513,6 +513,19 @@ static u8 encode_bMaxPower(enum usb_device_speed speed,
                return min(val, 900U) / 8;
 }
 
+void check_remote_wakeup_config(struct usb_gadget *g,
+                               struct usb_configuration *c)
+{
+       if (USB_CONFIG_ATT_WAKEUP & c->bmAttributes) {
+               /* Reset the rw bit if gadget is not capable of it */
+               if (!g->wakeup_capable && g->ops->set_remote_wakeup) {
+                       WARN(c->cdev, "Clearing wakeup bit for config c.%d\n",
+                            c->bConfigurationValue);
+                       c->bmAttributes &= ~USB_CONFIG_ATT_WAKEUP;
+               }
+       }
+}
+
 static int config_buf(struct usb_configuration *config,
                enum usb_device_speed speed, void *buf, u8 type)
 {
@@ -994,6 +1007,11 @@ static int set_config(struct usb_composite_dev *cdev,
                power = min(power, 500U);
        else
                power = min(power, 900U);
+
+       if (USB_CONFIG_ATT_WAKEUP & c->bmAttributes)
+               usb_gadget_set_remote_wakeup(gadget, 1);
+       else
+               usb_gadget_set_remote_wakeup(gadget, 0);
 done:
        if (power <= USB_SELF_POWER_VBUS_MAX_DRAW)
                usb_gadget_set_selfpowered(gadget);
index b9f1136aa0a268a7c526e59f450b2126b719f5d7..4c639e9ddedc0a0bc226795cd8fbd2e0dadbf70b 100644 (file)
@@ -1761,6 +1761,9 @@ static int configfs_composite_bind(struct usb_gadget *gadget,
                if (gadget_is_otg(gadget))
                        c->descriptors = otg_desc;
 
+               /* Properly configure the bmAttributes wakeup bit */
+               check_remote_wakeup_config(gadget, c);
+
                cfg = container_of(c, struct config_usb_cfg, c);
                if (!list_empty(&cfg->string_list)) {
                        i = 0;
index 23b0629a877431325c3f2f35f900388d9494d2cb..3dcbba739db6c5913002bd86dd7a2205496aafed 100644 (file)
@@ -513,6 +513,33 @@ out:
 }
 EXPORT_SYMBOL_GPL(usb_gadget_wakeup);
 
+/**
+ * usb_gadget_set_remote_wakeup - configures the device remote wakeup feature.
+ * @gadget:the device being configured for remote wakeup
+ * @set:value to be configured.
+ *
+ * set to one to enable remote wakeup feature and zero to disable it.
+ *
+ * returns zero on success, else negative errno.
+ */
+int usb_gadget_set_remote_wakeup(struct usb_gadget *gadget, int set)
+{
+       int ret = 0;
+
+       if (!gadget->ops->set_remote_wakeup) {
+               ret = -EOPNOTSUPP;
+               goto out;
+       }
+
+       ret = gadget->ops->set_remote_wakeup(gadget, set);
+
+out:
+       trace_usb_gadget_set_remote_wakeup(gadget, ret);
+
+       return ret;
+}
+EXPORT_SYMBOL_GPL(usb_gadget_set_remote_wakeup);
+
 /**
  * usb_gadget_set_selfpowered - sets the device selfpowered feature.
  * @gadget:the device being declared as self-powered
index abdbcb1bacb0bb01625e47e5ddc91b758b06f57b..a5ed26fbc2dad698c896e817fb8266bbaa6d29a9 100644 (file)
@@ -91,6 +91,11 @@ DEFINE_EVENT(udc_log_gadget, usb_gadget_wakeup,
        TP_ARGS(g, ret)
 );
 
+DEFINE_EVENT(udc_log_gadget, usb_gadget_set_remote_wakeup,
+       TP_PROTO(struct usb_gadget *g, int ret),
+       TP_ARGS(g, ret)
+);
+
 DEFINE_EVENT(udc_log_gadget, usb_gadget_set_selfpowered,
        TP_PROTO(struct usb_gadget *g, int ret),
        TP_ARGS(g, ret)
index 608dc962748bcb3c301ad603ad927d1f3fc93ff9..d949e91ceb48031fa138870078589dae4617dd78 100644 (file)
@@ -413,6 +413,8 @@ extern int composite_dev_prepare(struct usb_composite_driver *composite,
 extern int composite_os_desc_req_prepare(struct usb_composite_dev *cdev,
                                         struct usb_ep *ep0);
 void composite_dev_cleanup(struct usb_composite_dev *cdev);
+void check_remote_wakeup_config(struct usb_gadget *g,
+                               struct usb_configuration *c);
 
 static inline struct usb_composite_driver *to_cdriver(
                struct usb_gadget_driver *gdrv)
index 00750f7020f3bfe13373fe455bac4c1859643681..1d796128030b1c481d46c1950d7c29866947154e 100644 (file)
@@ -310,6 +310,7 @@ struct usb_udc;
 struct usb_gadget_ops {
        int     (*get_frame)(struct usb_gadget *);
        int     (*wakeup)(struct usb_gadget *);
+       int     (*set_remote_wakeup)(struct usb_gadget *, int set);
        int     (*set_selfpowered) (struct usb_gadget *, int is_selfpowered);
        int     (*vbus_session) (struct usb_gadget *, int is_active);
        int     (*vbus_draw) (struct usb_gadget *, unsigned mA);
@@ -384,6 +385,8 @@ struct usb_gadget_ops {
  * @connected: True if gadget is connected.
  * @lpm_capable: If the gadget max_speed is FULL or HIGH, this flag
  *     indicates that it supports LPM as per the LPM ECN & errata.
+ * @wakeup_capable: True if gadget is capable of sending remote wakeup.
+ * @wakeup_armed: True if gadget is armed by the host for remote wakeup.
  * @irq: the interrupt number for device controller.
  * @id_number: a unique ID number for ensuring that gadget names are distinct
  *
@@ -445,6 +448,8 @@ struct usb_gadget {
        unsigned                        deactivated:1;
        unsigned                        connected:1;
        unsigned                        lpm_capable:1;
+       unsigned                        wakeup_capable:1;
+       unsigned                        wakeup_armed:1;
        int                             irq;
        int                             id_number;
 };
@@ -601,6 +606,7 @@ static inline int gadget_is_otg(struct usb_gadget *g)
 #if IS_ENABLED(CONFIG_USB_GADGET)
 int usb_gadget_frame_number(struct usb_gadget *gadget);
 int usb_gadget_wakeup(struct usb_gadget *gadget);
+int usb_gadget_set_remote_wakeup(struct usb_gadget *gadget, int set);
 int usb_gadget_set_selfpowered(struct usb_gadget *gadget);
 int usb_gadget_clear_selfpowered(struct usb_gadget *gadget);
 int usb_gadget_vbus_connect(struct usb_gadget *gadget);
@@ -616,6 +622,8 @@ static inline int usb_gadget_frame_number(struct usb_gadget *gadget)
 { return 0; }
 static inline int usb_gadget_wakeup(struct usb_gadget *gadget)
 { return 0; }
+static inline int usb_gadget_set_remote_wakeup(struct usb_gadget *gadget, int set)
+{ return 0; }
 static inline int usb_gadget_set_selfpowered(struct usb_gadget *gadget)
 { return 0; }
 static inline int usb_gadget_clear_selfpowered(struct usb_gadget *gadget)