hid-dr.c 9.1 KB

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