hid-dr.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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/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_DRAGONRISE_FF
  35. #include "usbhid/usbhid.h"
  36. struct drff_device {
  37. struct hid_report *report;
  38. };
  39. static int drff_play(struct input_dev *dev, void *data,
  40. struct ff_effect *effect)
  41. {
  42. struct hid_device *hid = input_get_drvdata(dev);
  43. struct drff_device *drff = data;
  44. int strong, weak;
  45. strong = effect->u.rumble.strong_magnitude;
  46. weak = effect->u.rumble.weak_magnitude;
  47. dbg_hid("called with 0x%04x 0x%04x", strong, weak);
  48. if (strong || weak) {
  49. strong = strong * 0xff / 0xffff;
  50. weak = weak * 0xff / 0xffff;
  51. /* While reverse engineering this device, I found that when
  52. this value is set, it causes the strong rumble to function
  53. at a near maximum speed, so we'll bypass it. */
  54. if (weak == 0x0a)
  55. weak = 0x0b;
  56. drff->report->field[0]->value[0] = 0x51;
  57. drff->report->field[0]->value[1] = 0x00;
  58. drff->report->field[0]->value[2] = weak;
  59. drff->report->field[0]->value[4] = strong;
  60. usbhid_submit_report(hid, drff->report, USB_DIR_OUT);
  61. drff->report->field[0]->value[0] = 0xfa;
  62. drff->report->field[0]->value[1] = 0xfe;
  63. } else {
  64. drff->report->field[0]->value[0] = 0xf3;
  65. drff->report->field[0]->value[1] = 0x00;
  66. }
  67. drff->report->field[0]->value[2] = 0x00;
  68. drff->report->field[0]->value[4] = 0x00;
  69. dbg_hid("running with 0x%02x 0x%02x", strong, weak);
  70. usbhid_submit_report(hid, drff->report, USB_DIR_OUT);
  71. return 0;
  72. }
  73. static int drff_init(struct hid_device *hid)
  74. {
  75. struct drff_device *drff;
  76. struct hid_report *report;
  77. struct hid_input *hidinput;
  78. struct list_head *report_list =
  79. &hid->report_enum[HID_OUTPUT_REPORT].report_list;
  80. struct input_dev *dev;
  81. int error;
  82. if (list_empty(&hid->inputs)) {
  83. hid_err(hid, "no inputs found\n");
  84. return -ENODEV;
  85. }
  86. hidinput = list_first_entry(&hid->inputs, struct hid_input, list);
  87. dev = hidinput->input;
  88. if (list_empty(report_list)) {
  89. hid_err(hid, "no output reports found\n");
  90. return -ENODEV;
  91. }
  92. report = list_first_entry(report_list, struct hid_report, list);
  93. if (report->maxfield < 1) {
  94. hid_err(hid, "no fields in the report\n");
  95. return -ENODEV;
  96. }
  97. if (report->field[0]->report_count < 7) {
  98. hid_err(hid, "not enough values in the field\n");
  99. return -ENODEV;
  100. }
  101. drff = kzalloc(sizeof(struct drff_device), GFP_KERNEL);
  102. if (!drff)
  103. return -ENOMEM;
  104. set_bit(FF_RUMBLE, dev->ffbit);
  105. error = input_ff_create_memless(dev, drff, drff_play);
  106. if (error) {
  107. kfree(drff);
  108. return error;
  109. }
  110. drff->report = report;
  111. drff->report->field[0]->value[0] = 0xf3;
  112. drff->report->field[0]->value[1] = 0x00;
  113. drff->report->field[0]->value[2] = 0x00;
  114. drff->report->field[0]->value[3] = 0x00;
  115. drff->report->field[0]->value[4] = 0x00;
  116. drff->report->field[0]->value[5] = 0x00;
  117. drff->report->field[0]->value[6] = 0x00;
  118. usbhid_submit_report(hid, drff->report, USB_DIR_OUT);
  119. hid_info(hid, "Force Feedback for DragonRise Inc. "
  120. "game controllers by Richard Walmsley <richwalm@gmail.com>\n");
  121. return 0;
  122. }
  123. #else
  124. static inline int drff_init(struct hid_device *hid)
  125. {
  126. return 0;
  127. }
  128. #endif
  129. /*
  130. * The original descriptor of joystick with PID 0x0011, represented by DVTech PC
  131. * JS19. It seems both copied from another device and a result of confusion
  132. * either about the specification or about the program used to create the
  133. * descriptor. In any case, it's a wonder it works on Windows.
  134. *
  135. * Usage Page (Desktop), ; Generic desktop controls (01h)
  136. * Usage (Joystik), ; Joystik (04h, application collection)
  137. * Collection (Application),
  138. * Collection (Logical),
  139. * Report Size (8),
  140. * Report Count (5),
  141. * Logical Minimum (0),
  142. * Logical Maximum (255),
  143. * Physical Minimum (0),
  144. * Physical Maximum (255),
  145. * Usage (X), ; X (30h, dynamic value)
  146. * Usage (X), ; X (30h, dynamic value)
  147. * Usage (X), ; X (30h, dynamic value)
  148. * Usage (X), ; X (30h, dynamic value)
  149. * Usage (Y), ; Y (31h, dynamic value)
  150. * Input (Variable),
  151. * Report Size (4),
  152. * Report Count (1),
  153. * Logical Maximum (7),
  154. * Physical Maximum (315),
  155. * Unit (Degrees),
  156. * Usage (00h),
  157. * Input (Variable, Null State),
  158. * Unit,
  159. * Report Size (1),
  160. * Report Count (10),
  161. * Logical Maximum (1),
  162. * Physical Maximum (1),
  163. * Usage Page (Button), ; Button (09h)
  164. * Usage Minimum (01h),
  165. * Usage Maximum (0Ah),
  166. * Input (Variable),
  167. * Usage Page (FF00h), ; FF00h, vendor-defined
  168. * Report Size (1),
  169. * Report Count (10),
  170. * Logical Maximum (1),
  171. * Physical Maximum (1),
  172. * Usage (01h),
  173. * Input (Variable),
  174. * End Collection,
  175. * Collection (Logical),
  176. * Report Size (8),
  177. * Report Count (4),
  178. * Physical Maximum (255),
  179. * Logical Maximum (255),
  180. * Usage (02h),
  181. * Output (Variable),
  182. * End Collection,
  183. * End Collection
  184. */
  185. /* Size of the original descriptor of the PID 0x0011 joystick */
  186. #define PID0011_RDESC_ORIG_SIZE 101
  187. /* Fixed report descriptor for PID 0x011 joystick */
  188. static __u8 pid0011_rdesc_fixed[] = {
  189. 0x05, 0x01, /* Usage Page (Desktop), */
  190. 0x09, 0x04, /* Usage (Joystik), */
  191. 0xA1, 0x01, /* Collection (Application), */
  192. 0xA1, 0x02, /* Collection (Logical), */
  193. 0x14, /* Logical Minimum (0), */
  194. 0x75, 0x08, /* Report Size (8), */
  195. 0x95, 0x03, /* Report Count (3), */
  196. 0x81, 0x01, /* Input (Constant), */
  197. 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
  198. 0x95, 0x02, /* Report Count (2), */
  199. 0x09, 0x30, /* Usage (X), */
  200. 0x09, 0x31, /* Usage (Y), */
  201. 0x81, 0x02, /* Input (Variable), */
  202. 0x75, 0x01, /* Report Size (1), */
  203. 0x95, 0x04, /* Report Count (4), */
  204. 0x81, 0x01, /* Input (Constant), */
  205. 0x25, 0x01, /* Logical Maximum (1), */
  206. 0x95, 0x0A, /* Report Count (10), */
  207. 0x05, 0x09, /* Usage Page (Button), */
  208. 0x19, 0x01, /* Usage Minimum (01h), */
  209. 0x29, 0x0A, /* Usage Maximum (0Ah), */
  210. 0x81, 0x02, /* Input (Variable), */
  211. 0x95, 0x0A, /* Report Count (10), */
  212. 0x81, 0x01, /* Input (Constant), */
  213. 0xC0, /* End Collection, */
  214. 0xC0 /* End Collection */
  215. };
  216. static __u8 *dr_report_fixup(struct hid_device *hdev, __u8 *rdesc,
  217. unsigned int *rsize)
  218. {
  219. switch (hdev->product) {
  220. case 0x0011:
  221. if (*rsize == PID0011_RDESC_ORIG_SIZE) {
  222. rdesc = pid0011_rdesc_fixed;
  223. *rsize = sizeof(pid0011_rdesc_fixed);
  224. }
  225. break;
  226. }
  227. return rdesc;
  228. }
  229. static int dr_probe(struct hid_device *hdev, const struct hid_device_id *id)
  230. {
  231. int ret;
  232. dev_dbg(&hdev->dev, "DragonRise Inc. HID hardware probe...");
  233. ret = hid_parse(hdev);
  234. if (ret) {
  235. hid_err(hdev, "parse failed\n");
  236. goto err;
  237. }
  238. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
  239. if (ret) {
  240. hid_err(hdev, "hw start failed\n");
  241. goto err;
  242. }
  243. switch (hdev->product) {
  244. case 0x0006:
  245. ret = drff_init(hdev);
  246. if (ret) {
  247. dev_err(&hdev->dev, "force feedback init failed\n");
  248. hid_hw_stop(hdev);
  249. goto err;
  250. }
  251. break;
  252. }
  253. return 0;
  254. err:
  255. return ret;
  256. }
  257. static const struct hid_device_id dr_devices[] = {
  258. { HID_USB_DEVICE(USB_VENDOR_ID_DRAGONRISE, 0x0006), },
  259. { HID_USB_DEVICE(USB_VENDOR_ID_DRAGONRISE, 0x0011), },
  260. { }
  261. };
  262. MODULE_DEVICE_TABLE(hid, dr_devices);
  263. static struct hid_driver dr_driver = {
  264. .name = "dragonrise",
  265. .id_table = dr_devices,
  266. .report_fixup = dr_report_fixup,
  267. .probe = dr_probe,
  268. };
  269. static int __init dr_init(void)
  270. {
  271. return hid_register_driver(&dr_driver);
  272. }
  273. static void __exit dr_exit(void)
  274. {
  275. hid_unregister_driver(&dr_driver);
  276. }
  277. module_init(dr_init);
  278. module_exit(dr_exit);
  279. MODULE_LICENSE("GPL");