spi-slave-system-control.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * SPI slave handler controlling system state
  3. *
  4. * This SPI slave handler allows remote control of system reboot, power off,
  5. * halt, and suspend.
  6. *
  7. * Copyright (C) 2016-2017 Glider bvba
  8. *
  9. * This file is subject to the terms and conditions of the GNU General Public
  10. * License. See the file "COPYING" in the main directory of this archive
  11. * for more details.
  12. *
  13. * Usage (assuming /dev/spidev2.0 corresponds to the SPI master on the remote
  14. * system):
  15. *
  16. * # reboot='\x7c\x50'
  17. * # poweroff='\x71\x3f'
  18. * # halt='\x38\x76'
  19. * # suspend='\x1b\x1b'
  20. * # spidev_test -D /dev/spidev2.0 -p $suspend # or $reboot, $poweroff, $halt
  21. */
  22. #include <linux/completion.h>
  23. #include <linux/module.h>
  24. #include <linux/reboot.h>
  25. #include <linux/suspend.h>
  26. #include <linux/spi/spi.h>
  27. /*
  28. * The numbers are chosen to display something human-readable on two 7-segment
  29. * displays connected to two 74HC595 shift registers
  30. */
  31. #define CMD_REBOOT 0x7c50 /* rb */
  32. #define CMD_POWEROFF 0x713f /* OF */
  33. #define CMD_HALT 0x3876 /* HL */
  34. #define CMD_SUSPEND 0x1b1b /* ZZ */
  35. struct spi_slave_system_control_priv {
  36. struct spi_device *spi;
  37. struct completion finished;
  38. struct spi_transfer xfer;
  39. struct spi_message msg;
  40. __be16 cmd;
  41. };
  42. static
  43. int spi_slave_system_control_submit(struct spi_slave_system_control_priv *priv);
  44. static void spi_slave_system_control_complete(void *arg)
  45. {
  46. struct spi_slave_system_control_priv *priv = arg;
  47. u16 cmd;
  48. int ret;
  49. if (priv->msg.status)
  50. goto terminate;
  51. cmd = be16_to_cpu(priv->cmd);
  52. switch (cmd) {
  53. case CMD_REBOOT:
  54. dev_info(&priv->spi->dev, "Rebooting system...\n");
  55. kernel_restart(NULL);
  56. case CMD_POWEROFF:
  57. dev_info(&priv->spi->dev, "Powering off system...\n");
  58. kernel_power_off();
  59. break;
  60. case CMD_HALT:
  61. dev_info(&priv->spi->dev, "Halting system...\n");
  62. kernel_halt();
  63. break;
  64. case CMD_SUSPEND:
  65. dev_info(&priv->spi->dev, "Suspending system...\n");
  66. pm_suspend(PM_SUSPEND_MEM);
  67. break;
  68. default:
  69. dev_warn(&priv->spi->dev, "Unknown command 0x%x\n", cmd);
  70. break;
  71. }
  72. ret = spi_slave_system_control_submit(priv);
  73. if (ret)
  74. goto terminate;
  75. return;
  76. terminate:
  77. dev_info(&priv->spi->dev, "Terminating\n");
  78. complete(&priv->finished);
  79. }
  80. static
  81. int spi_slave_system_control_submit(struct spi_slave_system_control_priv *priv)
  82. {
  83. int ret;
  84. spi_message_init_with_transfers(&priv->msg, &priv->xfer, 1);
  85. priv->msg.complete = spi_slave_system_control_complete;
  86. priv->msg.context = priv;
  87. ret = spi_async(priv->spi, &priv->msg);
  88. if (ret)
  89. dev_err(&priv->spi->dev, "spi_async() failed %d\n", ret);
  90. return ret;
  91. }
  92. static int spi_slave_system_control_probe(struct spi_device *spi)
  93. {
  94. struct spi_slave_system_control_priv *priv;
  95. int ret;
  96. priv = devm_kzalloc(&spi->dev, sizeof(*priv), GFP_KERNEL);
  97. if (!priv)
  98. return -ENOMEM;
  99. priv->spi = spi;
  100. init_completion(&priv->finished);
  101. priv->xfer.rx_buf = &priv->cmd;
  102. priv->xfer.len = sizeof(priv->cmd);
  103. ret = spi_slave_system_control_submit(priv);
  104. if (ret)
  105. return ret;
  106. spi_set_drvdata(spi, priv);
  107. return 0;
  108. }
  109. static int spi_slave_system_control_remove(struct spi_device *spi)
  110. {
  111. struct spi_slave_system_control_priv *priv = spi_get_drvdata(spi);
  112. spi_slave_abort(spi);
  113. wait_for_completion(&priv->finished);
  114. return 0;
  115. }
  116. static struct spi_driver spi_slave_system_control_driver = {
  117. .driver = {
  118. .name = "spi-slave-system-control",
  119. },
  120. .probe = spi_slave_system_control_probe,
  121. .remove = spi_slave_system_control_remove,
  122. };
  123. module_spi_driver(spi_slave_system_control_driver);
  124. MODULE_AUTHOR("Geert Uytterhoeven <geert+renesas@glider.be>");
  125. MODULE_DESCRIPTION("SPI slave handler controlling system state");
  126. MODULE_LICENSE("GPL v2");