hid-lg4ff.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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. /* Sends default autocentering command compatible with
  135. * all wheels except Formula Force EX */
  136. static void hid_lg4ff_set_autocenter_default(struct input_dev *dev, u16 magnitude)
  137. {
  138. struct hid_device *hid = input_get_drvdata(dev);
  139. struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
  140. struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
  141. report->field[0]->value[0] = 0xfe;
  142. report->field[0]->value[1] = 0x0d;
  143. report->field[0]->value[2] = magnitude >> 13;
  144. report->field[0]->value[3] = magnitude >> 13;
  145. report->field[0]->value[4] = magnitude >> 8;
  146. report->field[0]->value[5] = 0x00;
  147. report->field[0]->value[6] = 0x00;
  148. usbhid_submit_report(hid, report, USB_DIR_OUT);
  149. }
  150. /* Sends autocentering command compatible with Formula Force EX */
  151. static void hid_lg4ff_set_autocenter_ffex(struct input_dev *dev, u16 magnitude)
  152. {
  153. struct hid_device *hid = input_get_drvdata(dev);
  154. struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
  155. struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
  156. magnitude = magnitude * 90 / 65535;
  157. report->field[0]->value[0] = 0xfe;
  158. report->field[0]->value[1] = 0x03;
  159. report->field[0]->value[2] = magnitude >> 14;
  160. report->field[0]->value[3] = magnitude >> 14;
  161. report->field[0]->value[4] = magnitude;
  162. report->field[0]->value[5] = 0x00;
  163. report->field[0]->value[6] = 0x00;
  164. usbhid_submit_report(hid, report, USB_DIR_OUT);
  165. }
  166. /* Sends command to set range compatible with G25/G27/Driving Force GT */
  167. static void hid_lg4ff_set_range_g25(struct hid_device *hid, u16 range)
  168. {
  169. struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
  170. struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
  171. dbg_hid("G25/G27/DFGT: setting range to %u\n", range);
  172. report->field[0]->value[0] = 0xf8;
  173. report->field[0]->value[1] = 0x81;
  174. report->field[0]->value[2] = range & 0x00ff;
  175. report->field[0]->value[3] = (range & 0xff00) >> 8;
  176. report->field[0]->value[4] = 0x00;
  177. report->field[0]->value[5] = 0x00;
  178. report->field[0]->value[6] = 0x00;
  179. usbhid_submit_report(hid, report, USB_DIR_OUT);
  180. }
  181. /* Sends commands to set range compatible with Driving Force Pro wheel */
  182. static void hid_lg4ff_set_range_dfp(struct hid_device *hid, __u16 range)
  183. {
  184. struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
  185. struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
  186. int start_left, start_right, full_range;
  187. dbg_hid("Driving Force Pro: setting range to %u\n", range);
  188. /* Prepare "coarse" limit command */
  189. report->field[0]->value[0] = 0xf8;
  190. report->field[0]->value[1] = 0x00; /* Set later */
  191. report->field[0]->value[2] = 0x00;
  192. report->field[0]->value[3] = 0x00;
  193. report->field[0]->value[4] = 0x00;
  194. report->field[0]->value[5] = 0x00;
  195. report->field[0]->value[6] = 0x00;
  196. if (range > 200) {
  197. report->field[0]->value[1] = 0x03;
  198. full_range = 900;
  199. } else {
  200. report->field[0]->value[1] = 0x02;
  201. full_range = 200;
  202. }
  203. usbhid_submit_report(hid, report, USB_DIR_OUT);
  204. /* Prepare "fine" limit command */
  205. report->field[0]->value[0] = 0x81;
  206. report->field[0]->value[1] = 0x0b;
  207. report->field[0]->value[2] = 0x00;
  208. report->field[0]->value[3] = 0x00;
  209. report->field[0]->value[4] = 0x00;
  210. report->field[0]->value[5] = 0x00;
  211. report->field[0]->value[6] = 0x00;
  212. if (range == 200 || range == 900) { /* Do not apply any fine limit */
  213. usbhid_submit_report(hid, report, USB_DIR_OUT);
  214. return;
  215. }
  216. /* Construct fine limit command */
  217. start_left = (((full_range - range + 1) * 2047) / full_range);
  218. start_right = 0xfff - start_left;
  219. report->field[0]->value[2] = start_left >> 4;
  220. report->field[0]->value[3] = start_right >> 4;
  221. report->field[0]->value[4] = 0xff;
  222. report->field[0]->value[5] = (start_right & 0xe) << 4 | (start_left & 0xe);
  223. report->field[0]->value[6] = 0xff;
  224. usbhid_submit_report(hid, report, USB_DIR_OUT);
  225. }
  226. static void hid_lg4ff_switch_native(struct hid_device *hid, const struct lg4ff_native_cmd *cmd)
  227. {
  228. struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
  229. struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
  230. __u8 i, j;
  231. j = 0;
  232. while (j < 7*cmd->cmd_num) {
  233. for (i = 0; i < 7; i++)
  234. report->field[0]->value[i] = cmd->cmd[j++];
  235. usbhid_submit_report(hid, report, USB_DIR_OUT);
  236. }
  237. }
  238. /* Read current range and display it in terminal */
  239. static ssize_t lg4ff_range_show(struct device *dev, struct device_attribute *attr, char *buf)
  240. {
  241. struct lg4ff_device_entry *uninitialized_var(entry);
  242. struct list_head *h;
  243. struct hid_device *hid = to_hid_device(dev);
  244. size_t count;
  245. list_for_each(h, &device_list.list) {
  246. entry = list_entry(h, struct lg4ff_device_entry, list);
  247. if (strcmp(entry->device_id, (&hid->dev)->kobj.name) == 0)
  248. break;
  249. }
  250. if (h == &device_list.list) {
  251. dbg_hid("Device not found!");
  252. return 0;
  253. }
  254. count = scnprintf(buf, PAGE_SIZE, "%u\n", entry->range);
  255. return count;
  256. }
  257. /* Set range to user specified value, call appropriate function
  258. * according to the type of the wheel */
  259. static ssize_t lg4ff_range_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  260. {
  261. struct lg4ff_device_entry *uninitialized_var(entry);
  262. struct list_head *h;
  263. struct hid_device *hid = to_hid_device(dev);
  264. __u16 range = simple_strtoul(buf, NULL, 10);
  265. list_for_each(h, &device_list.list) {
  266. entry = list_entry(h, struct lg4ff_device_entry, list);
  267. if (strcmp(entry->device_id, (&hid->dev)->kobj.name) == 0)
  268. break;
  269. }
  270. if (h == &device_list.list) {
  271. dbg_hid("Device not found!");
  272. return count;
  273. }
  274. if (range == 0)
  275. range = entry->max_range;
  276. /* Check if the wheel supports range setting
  277. * and that the range is within limits for the wheel */
  278. if (entry->set_range != NULL && range >= entry->min_range && range <= entry->max_range) {
  279. entry->set_range(hid, range);
  280. entry->range = range;
  281. }
  282. return count;
  283. }
  284. int lg4ff_init(struct hid_device *hid)
  285. {
  286. struct hid_input *hidinput;
  287. struct input_dev *dev;
  288. struct lg4ff_device_entry *entry;
  289. struct usb_device_descriptor *udesc;
  290. int error, i, j;
  291. __u16 bcdDevice, rev_maj, rev_min;
  292. if (list_empty(&hid->inputs)) {
  293. hid_err(hid, "no inputs found\n");
  294. return -ENODEV;
  295. }
  296. hidinput = list_entry(hid->inputs.next, struct hid_input, list);
  297. dev = hidinput->input;
  298. /* Check that the report looks ok */
  299. if (!hid_validate_values(hid, HID_OUTPUT_REPORT, 0, 0, 7))
  300. return -1;
  301. /* Check what wheel has been connected */
  302. for (i = 0; i < ARRAY_SIZE(lg4ff_devices); i++) {
  303. if (hid->product == lg4ff_devices[i].product_id) {
  304. dbg_hid("Found compatible device, product ID %04X\n", lg4ff_devices[i].product_id);
  305. break;
  306. }
  307. }
  308. if (i == ARRAY_SIZE(lg4ff_devices)) {
  309. hid_err(hid, "Device is not supported by lg4ff driver. If you think it should be, consider reporting a bug to"
  310. "LKML, Simon Wood <simon@mungewell.org> or Michal Maly <madcatxster@gmail.com>\n");
  311. return -1;
  312. }
  313. /* Attempt to switch wheel to native mode when applicable */
  314. udesc = &(hid_to_usb_dev(hid)->descriptor);
  315. if (!udesc) {
  316. hid_err(hid, "NULL USB device descriptor\n");
  317. return -1;
  318. }
  319. bcdDevice = le16_to_cpu(udesc->bcdDevice);
  320. rev_maj = bcdDevice >> 8;
  321. rev_min = bcdDevice & 0xff;
  322. if (lg4ff_devices[i].product_id == USB_DEVICE_ID_LOGITECH_WHEEL) {
  323. dbg_hid("Generic wheel detected, can it do native?\n");
  324. dbg_hid("USB revision: %2x.%02x\n", rev_maj, rev_min);
  325. for (j = 0; j < ARRAY_SIZE(lg4ff_revs); j++) {
  326. if (lg4ff_revs[j].rev_maj == rev_maj && lg4ff_revs[j].rev_min == rev_min) {
  327. hid_lg4ff_switch_native(hid, lg4ff_revs[j].command);
  328. hid_info(hid, "Switched to native mode\n");
  329. }
  330. }
  331. }
  332. /* Set supported force feedback capabilities */
  333. for (j = 0; lg4ff_devices[i].ff_effects[j] >= 0; j++)
  334. set_bit(lg4ff_devices[i].ff_effects[j], dev->ffbit);
  335. error = input_ff_create_memless(dev, NULL, hid_lg4ff_play);
  336. if (error)
  337. return error;
  338. /* Check if autocentering is available and
  339. * set the centering force to zero by default */
  340. if (test_bit(FF_AUTOCENTER, dev->ffbit)) {
  341. if(rev_maj == FFEX_REV_MAJ && rev_min == FFEX_REV_MIN) /* Formula Force EX expects different autocentering command */
  342. dev->ff->set_autocenter = hid_lg4ff_set_autocenter_ffex;
  343. else
  344. dev->ff->set_autocenter = hid_lg4ff_set_autocenter_default;
  345. dev->ff->set_autocenter(dev, 0);
  346. }
  347. /* Initialize device_list if this is the first device to handle by lg4ff */
  348. if (!list_inited) {
  349. INIT_LIST_HEAD(&device_list.list);
  350. list_inited = 1;
  351. }
  352. /* Add the device to device_list */
  353. entry = kzalloc(sizeof(struct lg4ff_device_entry), GFP_KERNEL);
  354. if (!entry) {
  355. hid_err(hid, "Cannot add device, insufficient memory.\n");
  356. return -ENOMEM;
  357. }
  358. entry->device_id = kstrdup((&hid->dev)->kobj.name, GFP_KERNEL);
  359. if (!entry->device_id) {
  360. hid_err(hid, "Cannot set device_id, insufficient memory.\n");
  361. kfree(entry);
  362. return -ENOMEM;
  363. }
  364. entry->min_range = lg4ff_devices[i].min_range;
  365. entry->max_range = lg4ff_devices[i].max_range;
  366. entry->set_range = lg4ff_devices[i].set_range;
  367. list_add(&entry->list, &device_list.list);
  368. /* Create sysfs interface */
  369. error = device_create_file(&hid->dev, &dev_attr_range);
  370. if (error)
  371. return error;
  372. dbg_hid("sysfs interface created\n");
  373. /* Set the maximum range to start with */
  374. entry->range = entry->max_range;
  375. if (entry->set_range != NULL)
  376. entry->set_range(hid, entry->range);
  377. hid_info(hid, "Force feedback for Logitech Speed Force Wireless by Simon Wood <simon@mungewell.org>\n");
  378. return 0;
  379. }
  380. int lg4ff_deinit(struct hid_device *hid)
  381. {
  382. bool found = 0;
  383. struct lg4ff_device_entry *entry;
  384. struct list_head *h, *g;
  385. list_for_each_safe(h, g, &device_list.list) {
  386. entry = list_entry(h, struct lg4ff_device_entry, list);
  387. if (strcmp(entry->device_id, (&hid->dev)->kobj.name) == 0) {
  388. list_del(h);
  389. kfree(entry->device_id);
  390. kfree(entry);
  391. found = 1;
  392. break;
  393. }
  394. }
  395. if (!found) {
  396. dbg_hid("Device entry not found!\n");
  397. return -1;
  398. }
  399. device_remove_file(&hid->dev, &dev_attr_range);
  400. dbg_hid("Device successfully unregistered\n");
  401. return 0;
  402. }