qnap-mcu-input.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Driver for input events on QNAP-MCUs
  4. *
  5. * Copyright (C) 2024 Heiko Stuebner <heiko@sntech.de>
  6. */
  7. #include <linux/input.h>
  8. #include <linux/mfd/qnap-mcu.h>
  9. #include <linux/module.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/slab.h>
  12. #include <uapi/linux/input-event-codes.h>
  13. /*
  14. * The power-key needs to be pressed for a while to create an event,
  15. * so there is no use for overly frequent polling.
  16. */
  17. #define POLL_INTERVAL 500
  18. struct qnap_mcu_input_dev {
  19. struct input_dev *input;
  20. struct qnap_mcu *mcu;
  21. struct device *dev;
  22. struct work_struct beep_work;
  23. int beep_type;
  24. };
  25. static void qnap_mcu_input_poll(struct input_dev *input)
  26. {
  27. struct qnap_mcu_input_dev *idev = input_get_drvdata(input);
  28. static const u8 cmd[] = { '@', 'C', 'V' };
  29. u8 reply[4];
  30. int state, ret;
  31. /* poll the power button */
  32. ret = qnap_mcu_exec(idev->mcu, cmd, sizeof(cmd), reply, sizeof(reply));
  33. if (ret)
  34. return;
  35. /* First bytes must mirror the sent command */
  36. if (memcmp(cmd, reply, sizeof(cmd))) {
  37. dev_err(idev->dev, "malformed data received\n");
  38. return;
  39. }
  40. state = reply[3] - 0x30;
  41. input_event(input, EV_KEY, KEY_POWER, state);
  42. input_sync(input);
  43. }
  44. static void qnap_mcu_input_beeper_work(struct work_struct *work)
  45. {
  46. struct qnap_mcu_input_dev *idev =
  47. container_of(work, struct qnap_mcu_input_dev, beep_work);
  48. const u8 cmd[] = { '@', 'C', (idev->beep_type == SND_TONE) ? '3' : '2' };
  49. qnap_mcu_exec_with_ack(idev->mcu, cmd, sizeof(cmd));
  50. }
  51. static int qnap_mcu_input_event(struct input_dev *input, unsigned int type,
  52. unsigned int code, int value)
  53. {
  54. struct qnap_mcu_input_dev *idev = input_get_drvdata(input);
  55. if (type != EV_SND || (code != SND_BELL && code != SND_TONE))
  56. return -EOPNOTSUPP;
  57. if (value < 0)
  58. return -EINVAL;
  59. /* beep runtime is determined by the MCU */
  60. if (value == 0)
  61. return 0;
  62. /* Schedule work to actually turn the beeper on */
  63. idev->beep_type = code;
  64. schedule_work(&idev->beep_work);
  65. return 0;
  66. }
  67. static void qnap_mcu_input_close(struct input_dev *input)
  68. {
  69. struct qnap_mcu_input_dev *idev = input_get_drvdata(input);
  70. cancel_work_sync(&idev->beep_work);
  71. }
  72. static int qnap_mcu_input_probe(struct platform_device *pdev)
  73. {
  74. struct qnap_mcu *mcu = dev_get_drvdata(pdev->dev.parent);
  75. struct qnap_mcu_input_dev *idev;
  76. struct device *dev = &pdev->dev;
  77. struct input_dev *input;
  78. int ret;
  79. idev = devm_kzalloc(dev, sizeof(*idev), GFP_KERNEL);
  80. if (!idev)
  81. return -ENOMEM;
  82. input = devm_input_allocate_device(dev);
  83. if (!input)
  84. return -ENOMEM;
  85. idev->input = input;
  86. idev->dev = dev;
  87. idev->mcu = mcu;
  88. input_set_drvdata(input, idev);
  89. input->name = "qnap-mcu";
  90. input->phys = "qnap-mcu-input/input0";
  91. input->id.bustype = BUS_HOST;
  92. input->id.vendor = 0x0001;
  93. input->id.product = 0x0001;
  94. input->id.version = 0x0100;
  95. input->event = qnap_mcu_input_event;
  96. input->close = qnap_mcu_input_close;
  97. input_set_capability(input, EV_KEY, KEY_POWER);
  98. input_set_capability(input, EV_SND, SND_BELL);
  99. input_set_capability(input, EV_SND, SND_TONE);
  100. INIT_WORK(&idev->beep_work, qnap_mcu_input_beeper_work);
  101. ret = input_setup_polling(input, qnap_mcu_input_poll);
  102. if (ret)
  103. return dev_err_probe(dev, ret, "unable to set up polling\n");
  104. input_set_poll_interval(input, POLL_INTERVAL);
  105. ret = input_register_device(input);
  106. if (ret)
  107. return dev_err_probe(dev, ret, "unable to register input device\n");
  108. return 0;
  109. }
  110. static struct platform_driver qnap_mcu_input_driver = {
  111. .probe = qnap_mcu_input_probe,
  112. .driver = {
  113. .name = "qnap-mcu-input",
  114. },
  115. };
  116. module_platform_driver(qnap_mcu_input_driver);
  117. MODULE_ALIAS("platform:qnap-mcu-input");
  118. MODULE_AUTHOR("Heiko Stuebner <heiko@sntech.de>");
  119. MODULE_DESCRIPTION("QNAP MCU input driver");
  120. MODULE_LICENSE("GPL");