hid-gaff.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * Force feedback support for GreenAsia (Product ID 0x12) based devices
  3. *
  4. * The devices are distributed under various names and the same USB device ID
  5. * can be used in many game controllers.
  6. *
  7. *
  8. * 0e8f:0012 "GreenAsia Inc. USB Joystick "
  9. * - tested with MANTA Warior MM816 and SpeedLink Strike2 SL-6635.
  10. *
  11. * Copyright (c) 2008 Lukasz Lubojanski <lukasz@lubojanski.info>
  12. */
  13. /*
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  27. */
  28. #include <linux/input.h>
  29. #include <linux/slab.h>
  30. #include <linux/usb.h>
  31. #include <linux/hid.h>
  32. #include <linux/module.h>
  33. #include "hid-ids.h"
  34. #ifdef CONFIG_GREENASIA_FF
  35. #include "usbhid/usbhid.h"
  36. struct gaff_device {
  37. struct hid_report *report;
  38. };
  39. static int hid_gaff_play(struct input_dev *dev, void *data,
  40. struct ff_effect *effect)
  41. {
  42. struct hid_device *hid = input_get_drvdata(dev);
  43. struct gaff_device *gaff = data;
  44. int left, right;
  45. left = effect->u.rumble.strong_magnitude;
  46. right = effect->u.rumble.weak_magnitude;
  47. dbg_hid("called with 0x%04x 0x%04x", left, right);
  48. left = left * 0xfe / 0xffff;
  49. right = right * 0xfe / 0xffff;
  50. gaff->report->field[0]->value[0] = 0x51;
  51. gaff->report->field[0]->value[1] = 0x0;
  52. gaff->report->field[0]->value[2] = right;
  53. gaff->report->field[0]->value[3] = 0;
  54. gaff->report->field[0]->value[4] = left;
  55. gaff->report->field[0]->value[5] = 0;
  56. dbg_hid("running with 0x%02x 0x%02x", left, right);
  57. usbhid_submit_report(hid, gaff->report, USB_DIR_OUT);
  58. gaff->report->field[0]->value[0] = 0xfa;
  59. gaff->report->field[0]->value[1] = 0xfe;
  60. gaff->report->field[0]->value[2] = 0x0;
  61. gaff->report->field[0]->value[4] = 0x0;
  62. usbhid_submit_report(hid, gaff->report, USB_DIR_OUT);
  63. return 0;
  64. }
  65. static int gaff_init(struct hid_device *hid)
  66. {
  67. struct gaff_device *gaff;
  68. struct hid_report *report;
  69. struct hid_input *hidinput;
  70. struct list_head *report_list =
  71. &hid->report_enum[HID_OUTPUT_REPORT].report_list;
  72. struct list_head *report_ptr = report_list;
  73. struct input_dev *dev;
  74. int error;
  75. if (list_empty(&hid->inputs)) {
  76. hid_err(hid, "no inputs found\n");
  77. return -ENODEV;
  78. }
  79. hidinput = list_entry(hid->inputs.next, struct hid_input, list);
  80. dev = hidinput->input;
  81. if (list_empty(report_list)) {
  82. hid_err(hid, "no output reports found\n");
  83. return -ENODEV;
  84. }
  85. report_ptr = report_ptr->next;
  86. report = list_entry(report_ptr, struct hid_report, list);
  87. if (report->maxfield < 1) {
  88. hid_err(hid, "no fields in the report\n");
  89. return -ENODEV;
  90. }
  91. if (report->field[0]->report_count < 6) {
  92. hid_err(hid, "not enough values in the field\n");
  93. return -ENODEV;
  94. }
  95. gaff = kzalloc(sizeof(struct gaff_device), GFP_KERNEL);
  96. if (!gaff)
  97. return -ENOMEM;
  98. set_bit(FF_RUMBLE, dev->ffbit);
  99. error = input_ff_create_memless(dev, gaff, hid_gaff_play);
  100. if (error) {
  101. kfree(gaff);
  102. return error;
  103. }
  104. gaff->report = report;
  105. gaff->report->field[0]->value[0] = 0x51;
  106. gaff->report->field[0]->value[1] = 0x00;
  107. gaff->report->field[0]->value[2] = 0x00;
  108. gaff->report->field[0]->value[3] = 0x00;
  109. usbhid_submit_report(hid, gaff->report, USB_DIR_OUT);
  110. gaff->report->field[0]->value[0] = 0xfa;
  111. gaff->report->field[0]->value[1] = 0xfe;
  112. usbhid_submit_report(hid, gaff->report, USB_DIR_OUT);
  113. hid_info(hid, "Force Feedback for GreenAsia 0x12 devices by Lukasz Lubojanski <lukasz@lubojanski.info>\n");
  114. return 0;
  115. }
  116. #else
  117. static inline int gaff_init(struct hid_device *hdev)
  118. {
  119. return 0;
  120. }
  121. #endif
  122. static int ga_probe(struct hid_device *hdev, const struct hid_device_id *id)
  123. {
  124. int ret;
  125. dev_dbg(&hdev->dev, "Greenasia HID hardware probe...");
  126. ret = hid_parse(hdev);
  127. if (ret) {
  128. hid_err(hdev, "parse failed\n");
  129. goto err;
  130. }
  131. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
  132. if (ret) {
  133. hid_err(hdev, "hw start failed\n");
  134. goto err;
  135. }
  136. gaff_init(hdev);
  137. return 0;
  138. err:
  139. return ret;
  140. }
  141. static const struct hid_device_id ga_devices[] = {
  142. { HID_USB_DEVICE(USB_VENDOR_ID_GREENASIA, 0x0012), },
  143. { }
  144. };
  145. MODULE_DEVICE_TABLE(hid, ga_devices);
  146. static struct hid_driver ga_driver = {
  147. .name = "greenasia",
  148. .id_table = ga_devices,
  149. .probe = ga_probe,
  150. };
  151. static int __init ga_init(void)
  152. {
  153. return hid_register_driver(&ga_driver);
  154. }
  155. static void __exit ga_exit(void)
  156. {
  157. hid_unregister_driver(&ga_driver);
  158. }
  159. module_init(ga_init);
  160. module_exit(ga_exit);
  161. MODULE_LICENSE("GPL");