hid-drff.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * Force feedback support for DragonRise Inc. game controllers
  3. *
  4. * From what I have gathered, these devices are mass produced in China and are
  5. * distributed under several vendors. They often share the same design as
  6. * the original PlayStation DualShock controller.
  7. *
  8. * 0079:0006 "DragonRise Inc. Generic USB Joystick "
  9. * - tested with a Tesun USB-703 game controller.
  10. *
  11. * Copyright (c) 2009 Richard Walmsley <richwalm@gmail.com>
  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/usb.h>
  30. #include <linux/hid.h>
  31. #include "hid-ids.h"
  32. #ifdef CONFIG_DRAGONRISE_FF
  33. #include "usbhid/usbhid.h"
  34. struct drff_device {
  35. struct hid_report *report;
  36. };
  37. static int drff_play(struct input_dev *dev, void *data,
  38. struct ff_effect *effect)
  39. {
  40. struct hid_device *hid = input_get_drvdata(dev);
  41. struct drff_device *drff = data;
  42. int strong, weak;
  43. strong = effect->u.rumble.strong_magnitude;
  44. weak = effect->u.rumble.weak_magnitude;
  45. dbg_hid("called with 0x%04x 0x%04x", strong, weak);
  46. if (strong || weak) {
  47. strong = strong * 0xff / 0xffff;
  48. weak = weak * 0xff / 0xffff;
  49. /* While reverse engineering this device, I found that when
  50. this value is set, it causes the strong rumble to function
  51. at a near maximum speed, so we'll bypass it. */
  52. if (weak == 0x0a)
  53. weak = 0x0b;
  54. drff->report->field[0]->value[0] = 0x51;
  55. drff->report->field[0]->value[1] = 0x00;
  56. drff->report->field[0]->value[2] = weak;
  57. drff->report->field[0]->value[4] = strong;
  58. usbhid_submit_report(hid, drff->report, USB_DIR_OUT);
  59. drff->report->field[0]->value[0] = 0xfa;
  60. drff->report->field[0]->value[1] = 0xfe;
  61. } else {
  62. drff->report->field[0]->value[0] = 0xf3;
  63. drff->report->field[0]->value[1] = 0x00;
  64. }
  65. drff->report->field[0]->value[2] = 0x00;
  66. drff->report->field[0]->value[4] = 0x00;
  67. dbg_hid("running with 0x%02x 0x%02x", strong, weak);
  68. usbhid_submit_report(hid, drff->report, USB_DIR_OUT);
  69. return 0;
  70. }
  71. static int drff_init(struct hid_device *hid)
  72. {
  73. struct drff_device *drff;
  74. struct hid_report *report;
  75. struct hid_input *hidinput = list_first_entry(&hid->inputs,
  76. struct hid_input, list);
  77. struct list_head *report_list =
  78. &hid->report_enum[HID_OUTPUT_REPORT].report_list;
  79. struct input_dev *dev = hidinput->input;
  80. int error;
  81. if (list_empty(report_list)) {
  82. dev_err(&hid->dev, "no output reports found\n");
  83. return -ENODEV;
  84. }
  85. report = list_first_entry(report_list, struct hid_report, list);
  86. if (report->maxfield < 1) {
  87. dev_err(&hid->dev, "no fields in the report\n");
  88. return -ENODEV;
  89. }
  90. if (report->field[0]->report_count < 7) {
  91. dev_err(&hid->dev, "not enough values in the field\n");
  92. return -ENODEV;
  93. }
  94. drff = kzalloc(sizeof(struct drff_device), GFP_KERNEL);
  95. if (!drff)
  96. return -ENOMEM;
  97. set_bit(FF_RUMBLE, dev->ffbit);
  98. error = input_ff_create_memless(dev, drff, drff_play);
  99. if (error) {
  100. kfree(drff);
  101. return error;
  102. }
  103. drff->report = report;
  104. drff->report->field[0]->value[0] = 0xf3;
  105. drff->report->field[0]->value[1] = 0x00;
  106. drff->report->field[0]->value[2] = 0x00;
  107. drff->report->field[0]->value[3] = 0x00;
  108. drff->report->field[0]->value[4] = 0x00;
  109. drff->report->field[0]->value[5] = 0x00;
  110. drff->report->field[0]->value[6] = 0x00;
  111. usbhid_submit_report(hid, drff->report, USB_DIR_OUT);
  112. dev_info(&hid->dev, "Force Feedback for DragonRise Inc. game "
  113. "controllers by Richard Walmsley <richwalm@gmail.com>\n");
  114. return 0;
  115. }
  116. #else
  117. static inline int drff_init(struct hid_device *hid)
  118. {
  119. return 0;
  120. }
  121. #endif
  122. static int dr_probe(struct hid_device *hdev, const struct hid_device_id *id)
  123. {
  124. int ret;
  125. dev_dbg(&hdev->dev, "DragonRise Inc. HID hardware probe...");
  126. ret = hid_parse(hdev);
  127. if (ret) {
  128. dev_err(&hdev->dev, "parse failed\n");
  129. goto err;
  130. }
  131. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
  132. if (ret) {
  133. dev_err(&hdev->dev, "hw start failed\n");
  134. goto err;
  135. }
  136. drff_init(hdev);
  137. return 0;
  138. err:
  139. return ret;
  140. }
  141. static const struct hid_device_id dr_devices[] = {
  142. { HID_USB_DEVICE(USB_VENDOR_ID_DRAGONRISE, 0x0006), },
  143. { }
  144. };
  145. MODULE_DEVICE_TABLE(hid, dr_devices);
  146. static struct hid_driver dr_driver = {
  147. .name = "dragonrise",
  148. .id_table = dr_devices,
  149. .probe = dr_probe,
  150. };
  151. static int __init dr_init(void)
  152. {
  153. return hid_register_driver(&dr_driver);
  154. }
  155. static void __exit dr_exit(void)
  156. {
  157. hid_unregister_driver(&dr_driver);
  158. }
  159. module_init(dr_init);
  160. module_exit(dr_exit);
  161. MODULE_LICENSE("GPL");