ams-input.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Apple Motion Sensor driver (joystick emulation)
  4. *
  5. * Copyright (C) 2005 Stelian Pop ([email protected])
  6. * Copyright (C) 2006 Michael Hanselmann ([email protected])
  7. */
  8. #include <linux/module.h>
  9. #include <linux/types.h>
  10. #include <linux/errno.h>
  11. #include <linux/init.h>
  12. #include <linux/delay.h>
  13. #include "ams.h"
  14. static bool joystick;
  15. module_param(joystick, bool, S_IRUGO);
  16. MODULE_PARM_DESC(joystick, "Enable the input class device on module load");
  17. static bool invert;
  18. module_param(invert, bool, S_IWUSR | S_IRUGO);
  19. MODULE_PARM_DESC(invert, "Invert input data on X and Y axis");
  20. static DEFINE_MUTEX(ams_input_mutex);
  21. static void ams_idev_poll(struct input_dev *idev)
  22. {
  23. s8 x, y, z;
  24. mutex_lock(&ams_info.lock);
  25. ams_sensors(&x, &y, &z);
  26. x -= ams_info.xcalib;
  27. y -= ams_info.ycalib;
  28. z -= ams_info.zcalib;
  29. input_report_abs(idev, ABS_X, invert ? -x : x);
  30. input_report_abs(idev, ABS_Y, invert ? -y : y);
  31. input_report_abs(idev, ABS_Z, z);
  32. input_sync(idev);
  33. mutex_unlock(&ams_info.lock);
  34. }
  35. /* Call with ams_info.lock held! */
  36. static int ams_input_enable(void)
  37. {
  38. struct input_dev *input;
  39. s8 x, y, z;
  40. int error;
  41. ams_sensors(&x, &y, &z);
  42. ams_info.xcalib = x;
  43. ams_info.ycalib = y;
  44. ams_info.zcalib = z;
  45. input = input_allocate_device();
  46. if (!input)
  47. return -ENOMEM;
  48. input->name = "Apple Motion Sensor";
  49. input->id.bustype = ams_info.bustype;
  50. input->id.vendor = 0;
  51. input->dev.parent = &ams_info.of_dev->dev;
  52. input_set_abs_params(input, ABS_X, -50, 50, 3, 0);
  53. input_set_abs_params(input, ABS_Y, -50, 50, 3, 0);
  54. input_set_abs_params(input, ABS_Z, -50, 50, 3, 0);
  55. input_set_capability(input, EV_KEY, BTN_TOUCH);
  56. error = input_setup_polling(input, ams_idev_poll);
  57. if (error)
  58. goto err_free_input;
  59. input_set_poll_interval(input, 25);
  60. error = input_register_device(input);
  61. if (error)
  62. goto err_free_input;
  63. ams_info.idev = input;
  64. joystick = true;
  65. return 0;
  66. err_free_input:
  67. input_free_device(input);
  68. return error;
  69. }
  70. static void ams_input_disable(void)
  71. {
  72. if (ams_info.idev) {
  73. input_unregister_device(ams_info.idev);
  74. ams_info.idev = NULL;
  75. }
  76. joystick = false;
  77. }
  78. static ssize_t ams_input_show_joystick(struct device *dev,
  79. struct device_attribute *attr, char *buf)
  80. {
  81. return sprintf(buf, "%d\n", joystick);
  82. }
  83. static ssize_t ams_input_store_joystick(struct device *dev,
  84. struct device_attribute *attr, const char *buf, size_t count)
  85. {
  86. unsigned long enable;
  87. int error = 0;
  88. int ret;
  89. ret = kstrtoul(buf, 0, &enable);
  90. if (ret)
  91. return ret;
  92. if (enable > 1)
  93. return -EINVAL;
  94. mutex_lock(&ams_input_mutex);
  95. if (enable != joystick) {
  96. if (enable)
  97. error = ams_input_enable();
  98. else
  99. ams_input_disable();
  100. }
  101. mutex_unlock(&ams_input_mutex);
  102. return error ? error : count;
  103. }
  104. static DEVICE_ATTR(joystick, S_IRUGO | S_IWUSR,
  105. ams_input_show_joystick, ams_input_store_joystick);
  106. int ams_input_init(void)
  107. {
  108. if (joystick)
  109. ams_input_enable();
  110. return device_create_file(&ams_info.of_dev->dev, &dev_attr_joystick);
  111. }
  112. void ams_input_exit(void)
  113. {
  114. device_remove_file(&ams_info.of_dev->dev, &dev_attr_joystick);
  115. mutex_lock(&ams_input_mutex);
  116. ams_input_disable();
  117. mutex_unlock(&ams_input_mutex);
  118. }