hid-lg4ff.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /*
  2. * Force feedback support for Logitech Speed Force Wireless
  3. *
  4. * http://wiibrew.org/wiki/Logitech_USB_steering_wheel
  5. *
  6. * Copyright (c) 2010 Simon Wood <simon@mungewell.org>
  7. */
  8. /*
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. */
  23. #include <linux/input.h>
  24. #include <linux/usb.h>
  25. #include <linux/hid.h>
  26. #include "usbhid/usbhid.h"
  27. #include "hid-lg.h"
  28. #include "hid-ids.h"
  29. #define DFGT_REV_MAJ 0x13
  30. #define DFGT_REV_MIN 0x22
  31. #define DFP_REV_MAJ 0x11
  32. #define DFP_REV_MIN 0x06
  33. #define FFEX_REV_MAJ 0x21
  34. #define FFEX_REV_MIN 0x00
  35. #define G25_REV_MAJ 0x12
  36. #define G25_REV_MIN 0x22
  37. #define G27_REV_MAJ 0x12
  38. #define G27_REV_MIN 0x38
  39. #define to_hid_device(pdev) container_of(pdev, struct hid_device, dev)
  40. static void hid_lg4ff_set_range_dfp(struct hid_device *hid, u16 range);
  41. static void hid_lg4ff_set_range_g25(struct hid_device *hid, u16 range);
  42. static ssize_t lg4ff_range_show(struct device *dev, struct device_attribute *attr, char *buf);
  43. static ssize_t lg4ff_range_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count);
  44. static DEVICE_ATTR(range, S_IRWXU | S_IRWXG | S_IRWXO, lg4ff_range_show, lg4ff_range_store);
  45. static bool list_inited;
  46. struct lg4ff_device_entry {
  47. char *device_id; /* Use name in respective kobject structure's address as the ID */
  48. __u16 range;
  49. __u16 min_range;
  50. __u16 max_range;
  51. __u8 leds;
  52. struct list_head list;
  53. void (*set_range)(struct hid_device *hid, u16 range);
  54. };
  55. static struct lg4ff_device_entry device_list;
  56. static const signed short lg4ff_wheel_effects[] = {
  57. FF_CONSTANT,
  58. FF_AUTOCENTER,
  59. -1
  60. };
  61. struct lg4ff_wheel {
  62. const __u32 product_id;
  63. const signed short *ff_effects;
  64. const __u16 min_range;
  65. const __u16 max_range;
  66. void (*set_range)(struct hid_device *hid, u16 range);
  67. };
  68. static const struct lg4ff_wheel lg4ff_devices[] = {
  69. {USB_DEVICE_ID_LOGITECH_WHEEL, lg4ff_wheel_effects, 40, 270, NULL},
  70. {USB_DEVICE_ID_LOGITECH_MOMO_WHEEL, lg4ff_wheel_effects, 40, 270, NULL},
  71. {USB_DEVICE_ID_LOGITECH_DFP_WHEEL, lg4ff_wheel_effects, 40, 900, hid_lg4ff_set_range_dfp},
  72. {USB_DEVICE_ID_LOGITECH_G25_WHEEL, lg4ff_wheel_effects, 40, 900, hid_lg4ff_set_range_g25},
  73. {USB_DEVICE_ID_LOGITECH_DFGT_WHEEL, lg4ff_wheel_effects, 40, 900, hid_lg4ff_set_range_g25},
  74. {USB_DEVICE_ID_LOGITECH_G27_WHEEL, lg4ff_wheel_effects, 40, 900, hid_lg4ff_set_range_g25},
  75. {USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2, lg4ff_wheel_effects, 40, 270, NULL},
  76. {USB_DEVICE_ID_LOGITECH_WII_WHEEL, lg4ff_wheel_effects, 40, 270, NULL}
  77. };
  78. struct lg4ff_native_cmd {
  79. const __u8 cmd_num; /* Number of commands to send */
  80. const __u8 cmd[];
  81. };
  82. struct lg4ff_usb_revision {
  83. const __u16 rev_maj;
  84. const __u16 rev_min;
  85. const struct lg4ff_native_cmd *command;
  86. };
  87. static const struct lg4ff_native_cmd native_dfp = {
  88. 1,
  89. {0xf8, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00}
  90. };
  91. static const struct lg4ff_native_cmd native_dfgt = {
  92. 2,
  93. {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* 1st command */
  94. 0xf8, 0x09, 0x03, 0x01, 0x00, 0x00, 0x00} /* 2nd command */
  95. };
  96. static const struct lg4ff_native_cmd native_g25 = {
  97. 1,
  98. {0xf8, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00}
  99. };
  100. static const struct lg4ff_native_cmd native_g27 = {
  101. 2,
  102. {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* 1st command */
  103. 0xf8, 0x09, 0x04, 0x01, 0x00, 0x00, 0x00} /* 2nd command */
  104. };
  105. static const struct lg4ff_usb_revision lg4ff_revs[] = {
  106. {DFGT_REV_MAJ, DFGT_REV_MIN, &native_dfgt}, /* Driving Force GT */
  107. {DFP_REV_MAJ, DFP_REV_MIN, &native_dfp}, /* Driving Force Pro */
  108. {G25_REV_MAJ, G25_REV_MIN, &native_g25}, /* G25 */
  109. {G27_REV_MAJ, G27_REV_MIN, &native_g27}, /* G27 */
  110. };
  111. static int hid_lg4ff_play(struct input_dev *dev, void *data, struct ff_effect *effect)
  112. {
  113. struct hid_device *hid = input_get_drvdata(dev);
  114. struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
  115. struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
  116. int x;
  117. #define CLAMP(x) if (x < 0) x = 0; if (x > 0xff) x = 0xff
  118. switch (effect->type) {
  119. case FF_CONSTANT:
  120. x = effect->u.ramp.start_level + 0x80; /* 0x80 is no force */
  121. CLAMP(x);
  122. report->field[0]->value[0] = 0x11; /* Slot 1 */
  123. report->field[0]->value[1] = 0x08;
  124. report->field[0]->value[2] = x;
  125. report->field[0]->value[3] = 0x80;
  126. report->field[0]->value[4] = 0x00;
  127. report->field[0]->value[5] = 0x00;
  128. report->field[0]->value[6] = 0x00;
  129. usbhid_submit_report(hid, report, USB_DIR_OUT);
  130. break;
  131. }
  132. return 0;
  133. }
  134. static void hid_lg4ff_set_autocenter(struct input_dev *dev, u16 magnitude)
  135. {
  136. struct hid_device *hid = input_get_drvdata(dev);
  137. struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
  138. struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
  139. report->field[0]->value[0] = 0xfe;
  140. report->field[0]->value[1] = 0x0d;
  141. report->field[0]->value[2] = magnitude >> 13;
  142. report->field[0]->value[3] = magnitude >> 13;
  143. report->field[0]->value[4] = magnitude >> 8;
  144. report->field[0]->value[5] = 0x00;
  145. report->field[0]->value[6] = 0x00;
  146. usbhid_submit_report(hid, report, USB_DIR_OUT);
  147. }
  148. /* Sends command to set range compatible with G25/G27/Driving Force GT */
  149. static void hid_lg4ff_set_range_g25(struct hid_device *hid, u16 range)
  150. {
  151. struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
  152. struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
  153. dbg_hid("G25/G27/DFGT: setting range to %u\n", range);
  154. report->field[0]->value[0] = 0xf8;
  155. report->field[0]->value[1] = 0x81;
  156. report->field[0]->value[2] = range & 0x00ff;
  157. report->field[0]->value[3] = (range & 0xff00) >> 8;
  158. report->field[0]->value[4] = 0x00;
  159. report->field[0]->value[5] = 0x00;
  160. report->field[0]->value[6] = 0x00;
  161. usbhid_submit_report(hid, report, USB_DIR_OUT);
  162. }
  163. /* Sends commands to set range compatible with Driving Force Pro wheel */
  164. static void hid_lg4ff_set_range_dfp(struct hid_device *hid, __u16 range)
  165. {
  166. struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
  167. struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
  168. int start_left, start_right, full_range;
  169. dbg_hid("Driving Force Pro: setting range to %u\n", range);
  170. /* Prepare "coarse" limit command */
  171. report->field[0]->value[0] = 0xf8;
  172. report->field[0]->value[1] = 0x00; /* Set later */
  173. report->field[0]->value[2] = 0x00;
  174. report->field[0]->value[3] = 0x00;
  175. report->field[0]->value[4] = 0x00;
  176. report->field[0]->value[5] = 0x00;
  177. report->field[0]->value[6] = 0x00;
  178. if (range > 200) {
  179. report->field[0]->value[1] = 0x03;
  180. full_range = 900;
  181. } else {
  182. report->field[0]->value[1] = 0x02;
  183. full_range = 200;
  184. }
  185. usbhid_submit_report(hid, report, USB_DIR_OUT);
  186. /* Prepare "fine" limit command */
  187. report->field[0]->value[0] = 0x81;
  188. report->field[0]->value[1] = 0x0b;
  189. report->field[0]->value[2] = 0x00;
  190. report->field[0]->value[3] = 0x00;
  191. report->field[0]->value[4] = 0x00;
  192. report->field[0]->value[5] = 0x00;
  193. report->field[0]->value[6] = 0x00;
  194. if (range == 200 || range == 900) { /* Do not apply any fine limit */
  195. usbhid_submit_report(hid, report, USB_DIR_OUT);
  196. return;
  197. }
  198. /* Construct fine limit command */
  199. start_left = (((full_range - range + 1) * 2047) / full_range);
  200. start_right = 0xfff - start_left;
  201. report->field[0]->value[2] = start_left >> 4;
  202. report->field[0]->value[3] = start_right >> 4;
  203. report->field[0]->value[4] = 0xff;
  204. report->field[0]->value[5] = (start_right & 0xe) << 4 | (start_left & 0xe);
  205. report->field[0]->value[6] = 0xff;
  206. usbhid_submit_report(hid, report, USB_DIR_OUT);
  207. }
  208. static void hid_lg4ff_switch_native(struct hid_device *hid, const struct lg4ff_native_cmd *cmd)
  209. {
  210. struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
  211. struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
  212. __u8 i, j;
  213. j = 0;
  214. while (j < 7*cmd->cmd_num) {
  215. for (i = 0; i < 7; i++)
  216. report->field[0]->value[i] = cmd->cmd[j++];
  217. usbhid_submit_report(hid, report, USB_DIR_OUT);
  218. }
  219. }
  220. /* Read current range and display it in terminal */
  221. static ssize_t lg4ff_range_show(struct device *dev, struct device_attribute *attr, char *buf)
  222. {
  223. struct lg4ff_device_entry *entry = 0;
  224. struct list_head *h;
  225. struct hid_device *hid = to_hid_device(dev);
  226. size_t count;
  227. list_for_each(h, &device_list.list) {
  228. entry = list_entry(h, struct lg4ff_device_entry, list);
  229. if (strcmp(entry->device_id, (&hid->dev)->kobj.name) == 0)
  230. break;
  231. }
  232. if (h == &device_list.list) {
  233. dbg_hid("Device not found!");
  234. return 0;
  235. }
  236. count = scnprintf(buf, PAGE_SIZE, "%u\n", entry->range);
  237. return count;
  238. }
  239. /* Set range to user specified value, call appropriate function
  240. * according to the type of the wheel */
  241. static ssize_t lg4ff_range_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  242. {
  243. struct lg4ff_device_entry *entry = 0;
  244. struct list_head *h;
  245. struct hid_device *hid = to_hid_device(dev);
  246. __u16 range = simple_strtoul(buf, NULL, 10);
  247. list_for_each(h, &device_list.list) {
  248. entry = list_entry(h, struct lg4ff_device_entry, list);
  249. if (strcmp(entry->device_id, (&hid->dev)->kobj.name) == 0)
  250. break;
  251. }
  252. if (h == &device_list.list) {
  253. dbg_hid("Device not found!");
  254. return count;
  255. }
  256. if (range == 0)
  257. range = entry->max_range;
  258. /* Check if the wheel supports range setting
  259. * and that the range is within limits for the wheel */
  260. if (entry->set_range != NULL && range >= entry->min_range && range <= entry->max_range) {
  261. entry->set_range(hid, range);
  262. entry->range = range;
  263. }
  264. return count;
  265. }
  266. int lg4ff_init(struct hid_device *hid)
  267. {
  268. struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
  269. struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
  270. struct input_dev *dev = hidinput->input;
  271. struct hid_report *report;
  272. struct hid_field *field;
  273. struct lg4ff_device_entry *entry;
  274. struct usb_device_descriptor *udesc;
  275. int error, i, j;
  276. __u16 bcdDevice, rev_maj, rev_min;
  277. /* Find the report to use */
  278. if (list_empty(report_list)) {
  279. hid_err(hid, "No output report found\n");
  280. return -1;
  281. }
  282. /* Check that the report looks ok */
  283. report = list_entry(report_list->next, struct hid_report, list);
  284. if (!report) {
  285. hid_err(hid, "NULL output report\n");
  286. return -1;
  287. }
  288. field = report->field[0];
  289. if (!field) {
  290. hid_err(hid, "NULL field\n");
  291. return -1;
  292. }
  293. /* Check what wheel has been connected */
  294. for (i = 0; i < ARRAY_SIZE(lg4ff_devices); i++) {
  295. if (hid->product == lg4ff_devices[i].product_id) {
  296. dbg_hid("Found compatible device, product ID %04X\n", lg4ff_devices[i].product_id);
  297. break;
  298. }
  299. }
  300. if (i == ARRAY_SIZE(lg4ff_devices)) {
  301. hid_err(hid, "Device is not supported by lg4ff driver. If you think it should be, consider reporting a bug to"
  302. "LKML, Simon Wood <simon@mungewell.org> or Michal Maly <madcatxster@gmail.com>\n");
  303. return -1;
  304. }
  305. /* Attempt to switch wheel to native mode when applicable */
  306. udesc = &(hid_to_usb_dev(hid)->descriptor);
  307. if (!udesc) {
  308. hid_err(hid, "NULL USB device descriptor\n");
  309. return -1;
  310. }
  311. bcdDevice = le16_to_cpu(udesc->bcdDevice);
  312. rev_maj = bcdDevice >> 8;
  313. rev_min = bcdDevice & 0xff;
  314. if (lg4ff_devices[i].product_id == USB_DEVICE_ID_LOGITECH_WHEEL) {
  315. dbg_hid("Generic wheel detected, can it do native?\n");
  316. dbg_hid("USB revision: %2x.%02x\n", rev_maj, rev_min);
  317. for (j = 0; j < ARRAY_SIZE(lg4ff_revs); j++) {
  318. if (lg4ff_revs[j].rev_maj == rev_maj && lg4ff_revs[j].rev_min == rev_min) {
  319. hid_lg4ff_switch_native(hid, lg4ff_revs[j].command);
  320. hid_info(hid, "Switched to native mode\n");
  321. }
  322. }
  323. }
  324. /* Set supported force feedback capabilities */
  325. for (j = 0; lg4ff_devices[i].ff_effects[j] >= 0; j++)
  326. set_bit(lg4ff_devices[i].ff_effects[j], dev->ffbit);
  327. error = input_ff_create_memless(dev, NULL, hid_lg4ff_play);
  328. if (error)
  329. return error;
  330. if (test_bit(FF_AUTOCENTER, dev->ffbit))
  331. dev->ff->set_autocenter = hid_lg4ff_set_autocenter;
  332. /* Initialize device_list if this is the first device to handle by lg4ff */
  333. if (!list_inited) {
  334. INIT_LIST_HEAD(&device_list.list);
  335. list_inited = 1;
  336. }
  337. /* Add the device to device_list */
  338. entry = (struct lg4ff_device_entry *)kzalloc(sizeof(struct lg4ff_device_entry), GFP_KERNEL);
  339. if (!entry) {
  340. hid_err(hid, "Cannot add device, insufficient memory.\n");
  341. return -ENOMEM;
  342. }
  343. entry->device_id = (char *)kzalloc(strlen((&hid->dev)->kobj.name) + 1, GFP_KERNEL);
  344. if (!entry->device_id) {
  345. hid_err(hid, "Cannot set device_id, insufficient memory.\n");
  346. return -ENOMEM;
  347. }
  348. strcpy(entry->device_id, (&hid->dev)->kobj.name);
  349. entry->min_range = lg4ff_devices[i].min_range;
  350. entry->max_range = lg4ff_devices[i].max_range;
  351. entry->set_range = lg4ff_devices[i].set_range;
  352. list_add(&entry->list, &device_list.list);
  353. /* Create sysfs interface */
  354. error = device_create_file(&hid->dev, &dev_attr_range);
  355. if (error)
  356. return error;
  357. dbg_hid("sysfs interface created\n");
  358. /* Set the maximum range to start with */
  359. entry->range = entry->max_range;
  360. if (entry->set_range != NULL)
  361. entry->set_range(hid, entry->range);
  362. hid_info(hid, "Force feedback for Logitech Speed Force Wireless by Simon Wood <simon@mungewell.org>\n");
  363. return 0;
  364. }
  365. int lg4ff_deinit(struct hid_device *hid)
  366. {
  367. bool found = 0;
  368. struct lg4ff_device_entry *entry;
  369. struct list_head *h, *g;
  370. list_for_each_safe(h, g, &device_list.list) {
  371. entry = list_entry(h, struct lg4ff_device_entry, list);
  372. if (strcmp(entry->device_id, (&hid->dev)->kobj.name) == 0) {
  373. list_del(h);
  374. kfree(entry->device_id);
  375. kfree(entry);
  376. found = 1;
  377. break;
  378. }
  379. }
  380. if (!found) {
  381. dbg_hid("Device entry not found!\n");
  382. return -1;
  383. }
  384. device_remove_file(&hid->dev, &dev_attr_range);
  385. dbg_hid("Device successfully unregistered\n");
  386. return 0;
  387. }