ams-input.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 int ams_input_kthread(void *data)
  25. {
  26. s8 x, y, z;
  27. while (!kthread_should_stop()) {
  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(ams_info.idev, ABS_X, invert ? -x : x);
  34. input_report_abs(ams_info.idev, ABS_Y, invert ? -y : y);
  35. input_report_abs(ams_info.idev, ABS_Z, z);
  36. input_sync(ams_info.idev);
  37. mutex_unlock(&ams_info.lock);
  38. msleep(25);
  39. }
  40. return 0;
  41. }
  42. static int ams_input_open(struct input_dev *dev)
  43. {
  44. ams_info.kthread = kthread_run(ams_input_kthread, NULL, "kams");
  45. return IS_ERR(ams_info.kthread) ? PTR_ERR(ams_info.kthread) : 0;
  46. }
  47. static void ams_input_close(struct input_dev *dev)
  48. {
  49. kthread_stop(ams_info.kthread);
  50. }
  51. /* Call with ams_info.lock held! */
  52. static void ams_input_enable(void)
  53. {
  54. s8 x, y, z;
  55. if (ams_info.idev)
  56. return;
  57. ams_sensors(&x, &y, &z);
  58. ams_info.xcalib = x;
  59. ams_info.ycalib = y;
  60. ams_info.zcalib = z;
  61. ams_info.idev = input_allocate_device();
  62. if (!ams_info.idev)
  63. return;
  64. ams_info.idev->name = "Apple Motion Sensor";
  65. ams_info.idev->id.bustype = ams_info.bustype;
  66. ams_info.idev->id.vendor = 0;
  67. ams_info.idev->open = ams_input_open;
  68. ams_info.idev->close = ams_input_close;
  69. ams_info.idev->dev.parent = &ams_info.of_dev->dev;
  70. input_set_abs_params(ams_info.idev, ABS_X, -50, 50, 3, 0);
  71. input_set_abs_params(ams_info.idev, ABS_Y, -50, 50, 3, 0);
  72. input_set_abs_params(ams_info.idev, ABS_Z, -50, 50, 3, 0);
  73. set_bit(EV_ABS, ams_info.idev->evbit);
  74. set_bit(EV_KEY, ams_info.idev->evbit);
  75. set_bit(BTN_TOUCH, ams_info.idev->keybit);
  76. if (input_register_device(ams_info.idev)) {
  77. input_free_device(ams_info.idev);
  78. ams_info.idev = NULL;
  79. return;
  80. }
  81. }
  82. /* Call with ams_info.lock held! */
  83. static void ams_input_disable(void)
  84. {
  85. if (ams_info.idev) {
  86. input_unregister_device(ams_info.idev);
  87. ams_info.idev = NULL;
  88. }
  89. }
  90. static ssize_t ams_input_show_joystick(struct device *dev,
  91. struct device_attribute *attr, char *buf)
  92. {
  93. return sprintf(buf, "%d\n", joystick);
  94. }
  95. static ssize_t ams_input_store_joystick(struct device *dev,
  96. struct device_attribute *attr, const char *buf, size_t count)
  97. {
  98. if (sscanf(buf, "%d\n", &joystick) != 1)
  99. return -EINVAL;
  100. mutex_lock(&ams_info.lock);
  101. if (joystick)
  102. ams_input_enable();
  103. else
  104. ams_input_disable();
  105. mutex_unlock(&ams_info.lock);
  106. return count;
  107. }
  108. static DEVICE_ATTR(joystick, S_IRUGO | S_IWUSR,
  109. ams_input_show_joystick, ams_input_store_joystick);
  110. /* Call with ams_info.lock held! */
  111. int ams_input_init(void)
  112. {
  113. int result;
  114. result = device_create_file(&ams_info.of_dev->dev, &dev_attr_joystick);
  115. if (!result && joystick)
  116. ams_input_enable();
  117. return result;
  118. }
  119. /* Call with ams_info.lock held! */
  120. void ams_input_exit(void)
  121. {
  122. ams_input_disable();
  123. device_remove_file(&ams_info.of_dev->dev, &dev_attr_joystick);
  124. }