ams-input.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 unsigned int joystick;
  19. module_param(joystick, bool, 0644);
  20. MODULE_PARM_DESC(joystick, "Enable the input class device on module load");
  21. static unsigned int invert;
  22. module_param(invert, bool, 0644);
  23. MODULE_PARM_DESC(invert, "Invert input data on X and Y axis");
  24. static void ams_idev_poll(struct input_polled_dev *dev)
  25. {
  26. struct input_dev *idev = dev->input;
  27. s8 x, y, z;
  28. mutex_lock(&ams_info.lock);
  29. ams_sensors(&x, &y, &z);
  30. x -= ams_info.xcalib;
  31. y -= ams_info.ycalib;
  32. z -= ams_info.zcalib;
  33. input_report_abs(idev, ABS_X, invert ? -x : x);
  34. input_report_abs(idev, ABS_Y, invert ? -y : y);
  35. input_report_abs(idev, ABS_Z, z);
  36. input_sync(idev);
  37. mutex_unlock(&ams_info.lock);
  38. }
  39. /* Call with ams_info.lock held! */
  40. static void ams_input_enable(void)
  41. {
  42. struct input_dev *input;
  43. s8 x, y, z;
  44. if (ams_info.idev)
  45. return;
  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;
  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. if (input_register_polled_device(ams_info.idev)) {
  67. input_free_polled_device(ams_info.idev);
  68. ams_info.idev = NULL;
  69. return;
  70. }
  71. }
  72. /* Call with ams_info.lock held! */
  73. static void ams_input_disable(void)
  74. {
  75. if (ams_info.idev) {
  76. input_unregister_polled_device(ams_info.idev);
  77. input_free_polled_device(ams_info.idev);
  78. ams_info.idev = NULL;
  79. }
  80. }
  81. static ssize_t ams_input_show_joystick(struct device *dev,
  82. struct device_attribute *attr, char *buf)
  83. {
  84. return sprintf(buf, "%d\n", joystick);
  85. }
  86. static ssize_t ams_input_store_joystick(struct device *dev,
  87. struct device_attribute *attr, const char *buf, size_t count)
  88. {
  89. if (sscanf(buf, "%d\n", &joystick) != 1)
  90. return -EINVAL;
  91. mutex_lock(&ams_info.lock);
  92. if (joystick)
  93. ams_input_enable();
  94. else
  95. ams_input_disable();
  96. mutex_unlock(&ams_info.lock);
  97. return count;
  98. }
  99. static DEVICE_ATTR(joystick, S_IRUGO | S_IWUSR,
  100. ams_input_show_joystick, ams_input_store_joystick);
  101. /* Call with ams_info.lock held! */
  102. int ams_input_init(void)
  103. {
  104. int result;
  105. result = device_create_file(&ams_info.of_dev->dev, &dev_attr_joystick);
  106. if (!result && joystick)
  107. ams_input_enable();
  108. return result;
  109. }
  110. /* Call with ams_info.lock held! */
  111. void ams_input_exit(void)
  112. {
  113. ams_input_disable();
  114. device_remove_file(&ams_info.of_dev->dev, &dev_attr_joystick);
  115. }