Implement HID over SPI driver power management callbacks.

Tested-by: Dale Whinham <[email protected]>
Signed-off-by: Jingyuan Liang <[email protected]>
---
 drivers/hid/spi-hid/spi-hid-acpi.c |   1 +
 drivers/hid/spi-hid/spi-hid-core.c | 185 +++++++++++++++++++++++++++++++++++++
 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      |   1 +
 5 files changed, 190 insertions(+)

diff --git a/drivers/hid/spi-hid/spi-hid-acpi.c 
b/drivers/hid/spi-hid/spi-hid-acpi.c
index 31ca3fa336cd..a52cafe86c05 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,
                .dev_groups = spi_hid_groups,
diff --git a/drivers/hid/spi-hid/spi-hid-core.c 
b/drivers/hid/spi-hid/spi-hid-core.c
index f4e9b4e1294e..66bfc251bd91 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 {
@@ -242,6 +250,148 @@ 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 void 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;
+       }
+
+       set_bit(SPI_HID_READY, &shid->flags);
+}
+
+static int spi_hid_suspend(struct spi_hid *shid)
+{
+       int error;
+       struct device *dev = &shid->spi->dev;
+
+       /* 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);
+                               return error;
+                       }
+               }
+       }
+
+       /*
+        * disable_irq() waits for a running IRQ thread, so no new reset_work
+        * can be queued after it returns. Then wait for any queued or running
+        * instance. Must not hold power_lock here: reset_work takes it.
+        */
+       disable_irq(shid->spi->irq);
+       cancel_work_sync(&shid->reset_work);
+
+       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\n", 
__func__);
+                               shid->regulator_error_count++;
+                               shid->regulator_last_error = error;
+                               /*
+                                * Undo partial suspend before returning error.
+                                * Complete the reset; RESET_PENDING stays set
+                                * until the device sends its reset response.
+                                */
+                               shid->ops->sleep_minimal_reset_delay(shid->ops);
+                               reinit_completion(&shid->reset_done);
+                               shid->ops->deassert_reset(shid->ops);
+                               enable_irq(shid->spi->irq);
+                               if (shid->hid) {
+                                       spi_hid_wait_for_reset(shid);
+                                       hid_driver_reset_resume(shid->hid);
+                               }
+                               return error;
+                       }
+
+                       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);
+       if (test_bit(SPI_HID_DEAD, &shid->flags)) {
+               /* Stay off; only balance disable_irq() 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\n", 
__func__);
+                               shid->regulator_error_count++;
+                               shid->regulator_last_error = 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);
+                       shid->ops->deassert_reset(shid->ops);
+                       was_reset = true;
+               }
+       }
+
+       enable_irq(shid->spi->irq);
+
+       /*
+        * For devices that may wake up, process work that was pending when
+        * suspend cancelled reset_work. 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);
+               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;
@@ -280,6 +430,7 @@ static void spi_hid_error_handler(struct spi_hid *shid)
                        shid->regulator_last_error = error;
                }
                shid->power_state = HIDSPI_OFF;
+               set_bit(SPI_HID_DEAD, &shid->flags);
                /* Dead device: leave the IRQ permanently disabled. */
                return;
        }
@@ -561,6 +712,8 @@ static int spi_hid_dev_desc_response(struct spi_hid *shid,
        shid->reset_attempts = 0;
        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;
 }
@@ -1378,6 +1531,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);
 
@@ -1411,6 +1565,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\n", __func__);
@@ -1451,6 +1617,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 1d8356f787f3..0b3248aedfce 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 78d0530d2473..605edecc4f7e 100644
--- a/drivers/hid/spi-hid/spi-hid-of.c
+++ b/drivers/hid/spi-hid/spi-hid-of.c
@@ -226,6 +226,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,
                .dev_groups = spi_hid_groups,
diff --git a/drivers/hid/spi-hid/spi-hid.h b/drivers/hid/spi-hid/spi-hid.h
index f5a5f4d54beb..17b2fdf192ed 100644
--- a/drivers/hid/spi-hid/spi-hid.h
+++ b/drivers/hid/spi-hid/spi-hid.h
@@ -41,5 +41,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 attribute_group *spi_hid_groups[];
+extern const struct dev_pm_ops spi_hid_core_pm;
 
 #endif /* SPI_HID_H */

-- 
2.56.0.385.gd3acb90ef8-goog


Reply via email to