hid-dr.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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/hid.h>
  31. #include <linux/module.h>
  32. #include "hid-ids.h"
  33. #ifdef CONFIG_DRAGONRISE_FF
  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. hid_hw_request(hid, drff->report, HID_REQ_SET_REPORT);
  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. hid_hw_request(hid, drff->report, HID_REQ_SET_REPORT);
  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. hid_err(hid, "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. hid_err(hid, "no fields in the report\n");
  88. return -ENODEV;
  89. }
  90. if (report->field[0]->report_count < 7) {
  91. hid_err(hid, "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. hid_hw_request(hid, drff->report, HID_REQ_SET_REPORT);
  112. hid_info(hid, "Force Feedback for DragonRise Inc. "
  113. "game 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. /*
  123. * The original descriptor of joystick with PID 0x0011, represented by DVTech PC
  124. * JS19. It seems both copied from another device and a result of confusion
  125. * either about the specification or about the program used to create the
  126. * descriptor. In any case, it's a wonder it works on Windows.
  127. *
  128. * Usage Page (Desktop), ; Generic desktop controls (01h)
  129. * Usage (Joystik), ; Joystik (04h, application collection)
  130. * Collection (Application),
  131. * Collection (Logical),
  132. * Report Size (8),
  133. * Report Count (5),
  134. * Logical Minimum (0),
  135. * Logical Maximum (255),
  136. * Physical Minimum (0),
  137. * Physical Maximum (255),
  138. * Usage (X), ; X (30h, dynamic value)
  139. * Usage (X), ; X (30h, dynamic value)
  140. * Usage (X), ; X (30h, dynamic value)
  141. * Usage (X), ; X (30h, dynamic value)
  142. * Usage (Y), ; Y (31h, dynamic value)
  143. * Input (Variable),
  144. * Report Size (4),
  145. * Report Count (1),
  146. * Logical Maximum (7),
  147. * Physical Maximum (315),
  148. * Unit (Degrees),
  149. * Usage (00h),
  150. * Input (Variable, Null State),
  151. * Unit,
  152. * Report Size (1),
  153. * Report Count (10),
  154. * Logical Maximum (1),
  155. * Physical Maximum (1),
  156. * Usage Page (Button), ; Button (09h)
  157. * Usage Minimum (01h),
  158. * Usage Maximum (0Ah),
  159. * Input (Variable),
  160. * Usage Page (FF00h), ; FF00h, vendor-defined
  161. * Report Size (1),
  162. * Report Count (10),
  163. * Logical Maximum (1),
  164. * Physical Maximum (1),
  165. * Usage (01h),
  166. * Input (Variable),
  167. * End Collection,
  168. * Collection (Logical),
  169. * Report Size (8),
  170. * Report Count (4),
  171. * Physical Maximum (255),
  172. * Logical Maximum (255),
  173. * Usage (02h),
  174. * Output (Variable),
  175. * End Collection,
  176. * End Collection
  177. */
  178. /* Size of the original descriptor of the PID 0x0011 joystick */
  179. #define PID0011_RDESC_ORIG_SIZE 101
  180. /* Fixed report descriptor for PID 0x011 joystick */
  181. static __u8 pid0011_rdesc_fixed[] = {
  182. 0x05, 0x01, /* Usage Page (Desktop), */
  183. 0x09, 0x04, /* Usage (Joystik), */
  184. 0xA1, 0x01, /* Collection (Application), */
  185. 0xA1, 0x02, /* Collection (Logical), */
  186. 0x14, /* Logical Minimum (0), */
  187. 0x75, 0x08, /* Report Size (8), */
  188. 0x95, 0x03, /* Report Count (3), */
  189. 0x81, 0x01, /* Input (Constant), */
  190. 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
  191. 0x95, 0x02, /* Report Count (2), */
  192. 0x09, 0x30, /* Usage (X), */
  193. 0x09, 0x31, /* Usage (Y), */
  194. 0x81, 0x02, /* Input (Variable), */
  195. 0x75, 0x01, /* Report Size (1), */
  196. 0x95, 0x04, /* Report Count (4), */
  197. 0x81, 0x01, /* Input (Constant), */
  198. 0x25, 0x01, /* Logical Maximum (1), */
  199. 0x95, 0x0A, /* Report Count (10), */
  200. 0x05, 0x09, /* Usage Page (Button), */
  201. 0x19, 0x01, /* Usage Minimum (01h), */
  202. 0x29, 0x0A, /* Usage Maximum (0Ah), */
  203. 0x81, 0x02, /* Input (Variable), */
  204. 0x95, 0x0A, /* Report Count (10), */
  205. 0x81, 0x01, /* Input (Constant), */
  206. 0xC0, /* End Collection, */
  207. 0xC0 /* End Collection */
  208. };
  209. static __u8 pid0006_rdesc_fixed[] = {
  210. 0x05, 0x01, /* Usage Page (Generic Desktop) */
  211. 0x09, 0x04, /* Usage (Joystick) */
  212. 0xA1, 0x01, /* Collection (Application) */
  213. 0xA1, 0x02, /* Collection (Logical) */
  214. 0x75, 0x08, /* Report Size (8) */
  215. 0x95, 0x05, /* Report Count (5) */
  216. 0x15, 0x00, /* Logical Minimum (0) */
  217. 0x26, 0xFF, 0x00, /* Logical Maximum (255) */
  218. 0x35, 0x00, /* Physical Minimum (0) */
  219. 0x46, 0xFF, 0x00, /* Physical Maximum (255) */
  220. 0x09, 0x30, /* Usage (X) */
  221. 0x09, 0x33, /* Usage (Ry) */
  222. 0x09, 0x32, /* Usage (Z) */
  223. 0x09, 0x31, /* Usage (Y) */
  224. 0x09, 0x34, /* Usage (Ry) */
  225. 0x81, 0x02, /* Input (Variable) */
  226. 0x75, 0x04, /* Report Size (4) */
  227. 0x95, 0x01, /* Report Count (1) */
  228. 0x25, 0x07, /* Logical Maximum (7) */
  229. 0x46, 0x3B, 0x01, /* Physical Maximum (315) */
  230. 0x65, 0x14, /* Unit (Centimeter) */
  231. 0x09, 0x39, /* Usage (Hat switch) */
  232. 0x81, 0x42, /* Input (Variable) */
  233. 0x65, 0x00, /* Unit (None) */
  234. 0x75, 0x01, /* Report Size (1) */
  235. 0x95, 0x0C, /* Report Count (12) */
  236. 0x25, 0x01, /* Logical Maximum (1) */
  237. 0x45, 0x01, /* Physical Maximum (1) */
  238. 0x05, 0x09, /* Usage Page (Button) */
  239. 0x19, 0x01, /* Usage Minimum (0x01) */
  240. 0x29, 0x0C, /* Usage Maximum (0x0C) */
  241. 0x81, 0x02, /* Input (Variable) */
  242. 0x06, 0x00, 0xFF, /* Usage Page (Vendor Defined) */
  243. 0x75, 0x01, /* Report Size (1) */
  244. 0x95, 0x08, /* Report Count (8) */
  245. 0x25, 0x01, /* Logical Maximum (1) */
  246. 0x45, 0x01, /* Physical Maximum (1) */
  247. 0x09, 0x01, /* Usage (0x01) */
  248. 0x81, 0x02, /* Input (Variable) */
  249. 0xC0, /* End Collection */
  250. 0xA1, 0x02, /* Collection (Logical) */
  251. 0x75, 0x08, /* Report Size (8) */
  252. 0x95, 0x07, /* Report Count (7) */
  253. 0x46, 0xFF, 0x00, /* Physical Maximum (255) */
  254. 0x26, 0xFF, 0x00, /* Logical Maximum (255) */
  255. 0x09, 0x02, /* Usage (0x02) */
  256. 0x91, 0x02, /* Output (Variable) */
  257. 0xC0, /* End Collection */
  258. 0xC0 /* End Collection */
  259. };
  260. static __u8 *dr_report_fixup(struct hid_device *hdev, __u8 *rdesc,
  261. unsigned int *rsize)
  262. {
  263. switch (hdev->product) {
  264. case 0x0011:
  265. if (*rsize == PID0011_RDESC_ORIG_SIZE) {
  266. rdesc = pid0011_rdesc_fixed;
  267. *rsize = sizeof(pid0011_rdesc_fixed);
  268. }
  269. break;
  270. case 0x0006:
  271. if (*rsize == sizeof(pid0006_rdesc_fixed)) {
  272. rdesc = pid0006_rdesc_fixed;
  273. *rsize = sizeof(pid0006_rdesc_fixed);
  274. }
  275. break;
  276. }
  277. return rdesc;
  278. }
  279. static int dr_probe(struct hid_device *hdev, const struct hid_device_id *id)
  280. {
  281. int ret;
  282. dev_dbg(&hdev->dev, "DragonRise Inc. HID hardware probe...");
  283. ret = hid_parse(hdev);
  284. if (ret) {
  285. hid_err(hdev, "parse failed\n");
  286. goto err;
  287. }
  288. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
  289. if (ret) {
  290. hid_err(hdev, "hw start failed\n");
  291. goto err;
  292. }
  293. switch (hdev->product) {
  294. case 0x0006:
  295. ret = drff_init(hdev);
  296. if (ret) {
  297. dev_err(&hdev->dev, "force feedback init failed\n");
  298. hid_hw_stop(hdev);
  299. goto err;
  300. }
  301. break;
  302. }
  303. return 0;
  304. err:
  305. return ret;
  306. }
  307. static const struct hid_device_id dr_devices[] = {
  308. { HID_USB_DEVICE(USB_VENDOR_ID_DRAGONRISE, 0x0006), },
  309. { HID_USB_DEVICE(USB_VENDOR_ID_DRAGONRISE, 0x0011), },
  310. { }
  311. };
  312. MODULE_DEVICE_TABLE(hid, dr_devices);
  313. static struct hid_driver dr_driver = {
  314. .name = "dragonrise",
  315. .id_table = dr_devices,
  316. .report_fixup = dr_report_fixup,
  317. .probe = dr_probe,
  318. };
  319. module_hid_driver(dr_driver);
  320. MODULE_LICENSE("GPL");