Implement HID over SPI driver power management callbacks.

Signed-off-by: Jingyuan Liang <[email protected]>
---
 drivers/hid/spi-hid/spi-hid-acpi.c |   1 +
 drivers/hid/spi-hid/spi-hid-core.c | 188 ++++++++++++++++++++++++++++++++++++-
 drivers/hid/spi-hid/spi-hid-core.h |   2 +
 drivers/hid/spi-hid/spi-hid-of.c   |   1 +
 drivers/hid/spi-hid/spi-hid.h      |   2 +
 5 files changed, 192 insertions(+), 2 deletions(-)

diff --git a/drivers/hid/spi-hid/spi-hid-acpi.c 
b/drivers/hid/spi-hid/spi-hid-acpi.c
index df459c71e209..ea291ca269d4 100644
--- a/drivers/hid/spi-hid/spi-hid-acpi.c
+++ b/drivers/hid/spi-hid/spi-hid-acpi.c
@@ -246,6 +246,7 @@ static struct spi_driver spi_hid_acpi_driver = {
        .driver = {
                .name   = "spi_hid_acpi",
                .owner  = THIS_MODULE,
+               .pm     = &spi_hid_core_pm,
                .acpi_match_table = spi_hid_acpi_match,
                .probe_type = PROBE_PREFER_ASYNCHRONOUS,
        },
diff --git a/drivers/hid/spi-hid/spi-hid-core.c 
b/drivers/hid/spi-hid/spi-hid-core.c
index 4ff93d22715c..3b8874ca0e1c 100644
--- a/drivers/hid/spi-hid/spi-hid-core.c
+++ b/drivers/hid/spi-hid/spi-hid-core.c
@@ -36,6 +36,8 @@
 #include <linux/list.h>
 #include <linux/module.h>
 #include <linux/mutex.h>
+#include <linux/pm.h>
+#include <linux/pm_wakeirq.h>
 #include <linux/slab.h>
 #include <linux/spi/spi.h>
 #include <linux/string.h>
@@ -60,6 +62,7 @@
 
 #define SPI_HID_MAX_RESET_ATTEMPTS     3
 #define SPI_HID_RESP_TIMEOUT           1000
+#define SPI_HID_RESET_TIMEOUT          1000
 
 /* Protocol message size constants */
 #define SPI_HID_READ_APPROVAL_LEN              5
@@ -91,6 +94,11 @@
  * ll_close. Input reports are only forwarded while this flag is set.
  */
 #define SPI_HID_STARTED        6
+/*
+ * dead indicates that the device was powered off after too many failed
+ * resets. It stays off until the driver is rebound, even across resume.
+ */
+#define SPI_HID_DEAD   7
 
 /* Processed data from input report header */
 struct spi_hid_input_header {
@@ -231,6 +239,147 @@ static const char *spi_hid_power_mode_string(enum 
hidspi_power_state power_state
        }
 }
 
+/*
+ * Wait for the device descriptor response after reset; the device then takes
+ * requests. Set READY here: the report descriptor check that normally sets it
+ * needs power_lock, which callers hold.
+ */
+static int spi_hid_wait_for_reset(struct spi_hid *shid)
+{
+       unsigned long timeout = msecs_to_jiffies(SPI_HID_RESET_TIMEOUT +
+                                                SPI_HID_RESP_TIMEOUT);
+
+       if (!wait_for_completion_timeout(&shid->reset_done, timeout)) {
+               dev_warn(&shid->spi->dev,
+                        "Device descriptor not received after reset\n");
+               return -ETIMEDOUT;
+       }
+
+       set_bit(SPI_HID_READY, &shid->flags);
+       return 0;
+}
+
+static int spi_hid_suspend(struct spi_hid *shid)
+{
+       int error;
+       struct device *dev = &shid->spi->dev;
+
+       /*
+        * Stop reset_work until resume; pending steps keep their flags. The
+        * IRQ stays on so a request in flight gets its response.
+        */
+       disable_work_sync(&shid->reset_work);
+
+       /* HID driver suspend may talk to the device; responses need the IRQ. */
+       scoped_guard(mutex, &shid->power_lock) {
+               if (shid->power_state != HIDSPI_OFF && shid->hid) {
+                       error = hid_driver_suspend(shid->hid, PMSG_SUSPEND);
+                       if (error) {
+                               dev_err(dev, "%s failed to suspend hid driver: 
%d\n",
+                                       __func__, error);
+                               enable_work(&shid->reset_work);
+                               /* Redo a schedule_work() dropped while 
disabled. */
+                               schedule_work(&shid->reset_work);
+                               return error;
+                       }
+               }
+       }
+
+       /* disable_irq() waits for a running IRQ thread. */
+       disable_irq(shid->spi->irq);
+
+       guard(mutex)(&shid->power_lock);
+       if (!device_may_wakeup(dev)) {
+               if (shid->power_state != HIDSPI_OFF) {
+                       /* Device is fully reset on resume; drop stale state. */
+                       clear_bit(SPI_HID_READY, &shid->flags);
+                       clear_bit(SPI_HID_RESET_RESPONSE, &shid->flags);
+                       clear_bit(SPI_HID_CREATE_DEVICE, &shid->flags);
+                       clear_bit(SPI_HID_ERROR, &shid->flags);
+                       set_bit(SPI_HID_RESET_PENDING, &shid->flags);
+
+                       shid->ops->assert_reset(shid->ops);
+
+                       error = shid->ops->power_down(shid->ops);
+                       if (error)
+                               dev_err(dev, "%s: could not power down: %d\n", 
__func__, error);
+
+                       /* Held in reset either way; resume resets it. */
+                       shid->power_state = HIDSPI_OFF;
+               }
+       }
+       return 0;
+}
+
+static int spi_hid_resume(struct spi_hid *shid)
+{
+       int error;
+       struct device *dev = &shid->spi->dev;
+       bool was_reset = false;
+
+       guard(mutex)(&shid->power_lock);
+       enable_work(&shid->reset_work);
+       if (test_bit(SPI_HID_DEAD, &shid->flags)) {
+               /* Stay off; only balance disable_*() in suspend. */
+               enable_irq(shid->spi->irq);
+               return 0;
+       }
+
+       if (!device_may_wakeup(dev)) {
+               if (shid->power_state == HIDSPI_OFF) {
+                       shid->ops->assert_reset(shid->ops);
+
+                       shid->ops->sleep_minimal_reset_delay(shid->ops);
+
+                       error = shid->ops->power_up(shid->ops);
+                       if (error) {
+                               dev_err(dev, "%s: could not power up: %d\n", 
__func__, error);
+                               /* Suspend runs next even if resume fails. */
+                               enable_irq(shid->spi->irq);
+                               return error;
+                       }
+                       shid->power_state = HIDSPI_ON;
+                       reinit_completion(&shid->reset_done);
+                       error = shid->ops->deassert_reset(shid->ops);
+                       if (error)
+                               dev_err(dev, "%s: failed to deassert reset: 
%d\n",
+                                       __func__, error);
+                       else
+                               was_reset = true;
+               }
+       }
+
+       enable_irq(shid->spi->irq);
+
+       /*
+        * For devices that may wake up, process work that was pending while
+        * suspend had reset_work disabled. Otherwise suspend already dropped 
it.
+        */
+       if (test_bit(SPI_HID_RESET_RESPONSE, &shid->flags) ||
+           test_bit(SPI_HID_CREATE_DEVICE, &shid->flags) ||
+           test_bit(SPI_HID_ERROR, &shid->flags))
+               schedule_work(&shid->reset_work);
+
+       if (shid->hid) {
+               if (was_reset && spi_hid_wait_for_reset(shid)) {
+                       /* No reset response: let recovery retry. */
+                       set_bit(SPI_HID_ERROR, &shid->flags);
+                       schedule_work(&shid->reset_work);
+                       return -ETIMEDOUT;
+               }
+               /* HID requests would fail while the device is not ready. */
+               if (!test_bit(SPI_HID_READY, &shid->flags))
+                       return 0;
+               error = hid_driver_reset_resume(shid->hid);
+               if (error) {
+                       dev_err(dev, "%s: failed to reset resume hid driver: 
%d\n",
+                               __func__, error);
+                       return error;
+               }
+       }
+       return 0;
+}
+
 static void spi_hid_stop_hid(struct spi_hid *shid)
 {
        struct hid_device *hid;
@@ -266,6 +415,7 @@ static void spi_hid_error_handler(struct spi_hid *shid)
                if (error)
                        dev_err(dev, "failed to disable regulator: %d\n", 
error);
                shid->power_state = HIDSPI_OFF;
+               set_bit(SPI_HID_DEAD, &shid->flags);
                /* Dead device: leave the IRQ permanently disabled. */
                return;
        }
@@ -454,8 +604,8 @@ static void spi_hid_reset_response(struct spi_hid *shid)
 
        error = spi_hid_sync_request(shid, &report);
        if (error) {
-               dev_WARN_ONCE(dev, true,
-                             "Failed to send device descriptor request: %d\n", 
error);
+               dev_err(dev, "Failed to send device descriptor request: %d\n",
+                       error);
                set_bit(SPI_HID_ERROR, &shid->flags);
                schedule_work(&shid->reset_work);
        }
@@ -546,6 +696,8 @@ static int spi_hid_dev_desc_response(struct spi_hid *shid,
 
        set_bit(SPI_HID_CREATE_DEVICE, &shid->flags);
        schedule_work(&shid->reset_work);
+       /* The device can take requests now; see spi_hid_wait_for_reset(). */
+       complete(&shid->reset_done);
 
        return 0;
 }
@@ -1325,6 +1477,7 @@ int spi_hid_core_probe(struct spi_device *spi, struct 
spihid_ops *ops,
        mutex_init(&shid->power_lock);
        mutex_init(&shid->io_lock);
        init_completion(&shid->output_done);
+       init_completion(&shid->reset_done);
 
        INIT_WORK(&shid->reset_work, spi_hid_reset_work);
 
@@ -1358,6 +1511,18 @@ int spi_hid_core_probe(struct spi_device *spi, struct 
spihid_ops *ops,
                return error;
        }
 
+       /*
+        * Use device_can_wakeup(), not device_may_wakeup(): power/wakeup can
+        * change after probe, and the PM core checks it at suspend time.
+        */
+       if (device_can_wakeup(dev)) {
+               error = devm_pm_set_wake_irq(dev, spi->irq);
+               if (error) {
+                       dev_err(dev, "%s: failed to set wake IRQ\n", __func__);
+                       return error;
+               }
+       }
+
        error = shid->ops->power_up(shid->ops);
        if (error) {
                dev_err(dev, "%s: could not power up: %d\n", __func__, error);
@@ -1398,6 +1563,25 @@ void spi_hid_core_remove(struct spi_device *spi)
 }
 EXPORT_SYMBOL_GPL(spi_hid_core_remove);
 
+static int spi_hid_core_pm_suspend(struct device *dev)
+{
+       struct spi_hid *shid = dev_get_drvdata(dev);
+
+       return spi_hid_suspend(shid);
+}
+
+static int spi_hid_core_pm_resume(struct device *dev)
+{
+       struct spi_hid *shid = dev_get_drvdata(dev);
+
+       return spi_hid_resume(shid);
+}
+
+const struct dev_pm_ops spi_hid_core_pm = {
+       SYSTEM_SLEEP_PM_OPS(spi_hid_core_pm_suspend, spi_hid_core_pm_resume)
+};
+EXPORT_SYMBOL_GPL(spi_hid_core_pm);
+
 MODULE_DESCRIPTION("HID over SPI transport driver");
 MODULE_AUTHOR("Dmitry Antipov <[email protected]>");
 MODULE_LICENSE("GPL");
diff --git a/drivers/hid/spi-hid/spi-hid-core.h 
b/drivers/hid/spi-hid/spi-hid-core.h
index e66594b7a8cb..060272353216 100644
--- a/drivers/hid/spi-hid/spi-hid-core.h
+++ b/drivers/hid/spi-hid/spi-hid-core.h
@@ -92,6 +92,8 @@ struct spi_hid {
        struct mutex io_lock;
 
        struct completion output_done;
+       /* Completed once the device descriptor is received after a reset. */
+       struct completion reset_done;
 
        u32 report_descriptor_crc32;    /* HID report descriptor crc32 
checksum. */
 
diff --git a/drivers/hid/spi-hid/spi-hid-of.c b/drivers/hid/spi-hid/spi-hid-of.c
index d1bbdc17102e..6de9affbebe9 100644
--- a/drivers/hid/spi-hid/spi-hid-of.c
+++ b/drivers/hid/spi-hid/spi-hid-of.c
@@ -195,6 +195,7 @@ static struct spi_driver spi_hid_of_driver = {
        .driver = {
                .name = "spi_hid_of",
                .owner = THIS_MODULE,
+               .pm = &spi_hid_core_pm,
                .of_match_table = spi_hid_of_match,
                .probe_type = PROBE_PREFER_ASYNCHRONOUS,
        },
diff --git a/drivers/hid/spi-hid/spi-hid.h b/drivers/hid/spi-hid/spi-hid.h
index a31956ef24ef..95acb2845246 100644
--- a/drivers/hid/spi-hid/spi-hid.h
+++ b/drivers/hid/spi-hid/spi-hid.h
@@ -40,4 +40,6 @@ int spi_hid_core_probe(struct spi_device *spi, struct 
spihid_ops *ops,
 
 void spi_hid_core_remove(struct spi_device *spi);
 
+extern const struct dev_pm_ops spi_hid_core_pm;
+
 #endif /* SPI_HID_H */

-- 
2.56.0.257.gf7497c7d27-goog


Reply via email to