ams-input.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Apple Motion Sensor driver (joystick emulation)
  3. *
  4. * Copyright (C) 2005 Stelian Pop (stelian@popies.net)
  5. * Copyright (C) 2006 Michael Hanselmann (linux-kernel@hansmi.ch)
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/types.h>
  14. #include <linux/errno.h>
  15. #include <linux/init.h>
  16. #include <linux/delay.h>
  17. #include "ams.h"
  18. static bool joystick;
  19. module_param(joystick, bool, S_IRUGO);
  20. MODULE_PARM_DESC(joystick, "Enable the input class device on module load");
  21. static bool invert;
  22. module_param(invert, bool, S_IWUSR | S_IRUGO);
  23. MODULE_PARM_DESC(invert, "Invert input data on X and Y axis");
  24. static DEFINE_MUTEX(ams_input_mutex);
  25. static void ams_idev_poll(struct input_polled_dev *dev)
  26. {
  27. struct input_dev *idev = dev->input;
  28. s8 x, y, z;
  29. mutex_lock(&ams_info.lock);
  30. ams_sensors(&x, &y, &z);
  31. x -= ams_info.xcalib;
  32. y -= ams_info.ycalib;
  33. z -= ams_info.zcalib;
  34. input_report_abs(idev, ABS_X, invert ? -x : x);
  35. input_report_abs(idev, ABS_Y, invert ? -y : y);
  36. input_report_abs(idev, ABS_Z, z);
  37. input_sync(idev);
  38. mutex_unlock(&ams_info.lock);
  39. }
  40. /* Call with ams_info.lock held! */
  41. static int ams_input_enable(void)
  42. {
  43. struct input_dev *input;
  44. s8 x, y, z;
  45. int error;
  46. ams_sensors(&x, &y, &z);
  47. ams_info.xcalib = x;
  48. ams_info.ycalib = y;
  49. ams_info.zcalib = z;
  50. ams_info.idev = input_allocate_polled_device();
  51. if (!ams_info.idev)
  52. return -ENOMEM;
  53. ams_info.idev->poll = ams_idev_poll;
  54. ams_info.idev->poll_interval = 25;
  55. input = ams_info.idev->input;
  56. input->name = "Apple Motion Sensor";
  57. input->id.bustype = ams_info.bustype;
  58. input->id.vendor = 0;
  59. input->dev.parent = &ams_info.of_dev->dev;
  60. input_set_abs_params(input, ABS_X, -50, 50, 3, 0);
  61. input_set_abs_params(input, ABS_Y, -50, 50, 3, 0);
  62. input_set_abs_params(input, ABS_Z, -50, 50, 3, 0);
  63. set_bit(EV_ABS, input->evbit);
  64. set_bit(EV_KEY, input->evbit);
  65. set_bit(BTN_TOUCH, input->keybit);
  66. error = input_register_polled_device(ams_info.idev);
  67. if (error) {
  68. input_free_polled_device(ams_info.idev);
  69. ams_info.idev = NULL;
  70. return error;
  71. }
  72. joystick = 1;
  73. return 0;
  74. }
  75. static void ams_input_disable(void)
  76. {
  77. if (ams_info.idev) {
  78. input_unregister_polled_device(ams_info.idev);
  79. input_free_polled_device(ams_info.idev);
  80. ams_info.idev = NULL;
  81. }
  82. joystick = 0;
  83. }
  84. static ssize_t ams_input_show_joystick(struct device *dev,
  85. struct device_attribute *attr, char *buf)
  86. {
  87. return sprintf(buf, "%d\n", joystick);
  88. }
  89. static ssize_t ams_input_store_joystick(struct device *dev,
  90. struct device_attribute *attr, const char *buf, size_t count)
  91. {
  92. unsigned long enable;
  93. int error = 0;
  94. int ret;
  95. ret = kstrtoul(buf, 0, &enable);
  96. if (ret)
  97. return ret;
  98. if (enable > 1)
  99. return -EINVAL;
  100. mutex_lock(&ams_input_mutex);
  101. if (enable != joystick) {
  102. if (enable)
  103. error = ams_input_enable();
  104. else
  105. ams_input_disable();
  106. }
  107. mutex_unlock(&ams_input_mutex);
  108. return error ? error : count;
  109. }
  110. static DEVICE_ATTR(joystick, S_IRUGO | S_IWUSR,
  111. ams_input_show_joystick, ams_input_store_joystick);
  112. int ams_input_init(void)
  113. {
  114. if (joystick)
  115. ams_input_enable();
  116. return device_create_file(&ams_info.of_dev->dev, &dev_attr_joystick);
  117. }
  118. void ams_input_exit(void)
  119. {
  120. device_remove_file(&ams_info.of_dev->dev, &dev_attr_joystick);
  121. mutex_lock(&ams_input_mutex);
  122. ams_input_disable();
  123. mutex_unlock(&ams_input_mutex);
  124. }