hid-lg4ff.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  1. /*
  2. * Force feedback support for Logitech Gaming Wheels
  3. *
  4. * Including G27, G25, DFP, DFGT, FFEX, Momo, Momo2 &
  5. * Speed Force Wireless (WiiWheel)
  6. *
  7. * Copyright (c) 2010 Simon Wood <simon@mungewell.org>
  8. */
  9. /*
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. */
  24. #include <linux/input.h>
  25. #include <linux/usb.h>
  26. #include <linux/hid.h>
  27. #include "usbhid/usbhid.h"
  28. #include "hid-lg.h"
  29. #include "hid-ids.h"
  30. #define DFGT_REV_MAJ 0x13
  31. #define DFGT_REV_MIN 0x22
  32. #define DFGT2_REV_MIN 0x26
  33. #define DFP_REV_MAJ 0x11
  34. #define DFP_REV_MIN 0x06
  35. #define FFEX_REV_MAJ 0x21
  36. #define FFEX_REV_MIN 0x00
  37. #define G25_REV_MAJ 0x12
  38. #define G25_REV_MIN 0x22
  39. #define G27_REV_MAJ 0x12
  40. #define G27_REV_MIN 0x38
  41. #define to_hid_device(pdev) container_of(pdev, struct hid_device, dev)
  42. static void hid_lg4ff_set_range_dfp(struct hid_device *hid, u16 range);
  43. static void hid_lg4ff_set_range_g25(struct hid_device *hid, u16 range);
  44. static ssize_t lg4ff_range_show(struct device *dev, struct device_attribute *attr, char *buf);
  45. static ssize_t lg4ff_range_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count);
  46. static DEVICE_ATTR(range, S_IRWXU | S_IRWXG | S_IRWXO, lg4ff_range_show, lg4ff_range_store);
  47. struct lg4ff_device_entry {
  48. __u32 product_id;
  49. __u16 range;
  50. __u16 min_range;
  51. __u16 max_range;
  52. #ifdef CONFIG_LEDS_CLASS
  53. __u8 led_state;
  54. struct led_classdev *led[5];
  55. #endif
  56. struct list_head list;
  57. void (*set_range)(struct hid_device *hid, u16 range);
  58. };
  59. static const signed short lg4ff_wheel_effects[] = {
  60. FF_CONSTANT,
  61. FF_AUTOCENTER,
  62. -1
  63. };
  64. struct lg4ff_wheel {
  65. const __u32 product_id;
  66. const signed short *ff_effects;
  67. const __u16 min_range;
  68. const __u16 max_range;
  69. void (*set_range)(struct hid_device *hid, u16 range);
  70. };
  71. static const struct lg4ff_wheel lg4ff_devices[] = {
  72. {USB_DEVICE_ID_LOGITECH_WHEEL, lg4ff_wheel_effects, 40, 270, NULL},
  73. {USB_DEVICE_ID_LOGITECH_MOMO_WHEEL, lg4ff_wheel_effects, 40, 270, NULL},
  74. {USB_DEVICE_ID_LOGITECH_DFP_WHEEL, lg4ff_wheel_effects, 40, 900, hid_lg4ff_set_range_dfp},
  75. {USB_DEVICE_ID_LOGITECH_G25_WHEEL, lg4ff_wheel_effects, 40, 900, hid_lg4ff_set_range_g25},
  76. {USB_DEVICE_ID_LOGITECH_DFGT_WHEEL, lg4ff_wheel_effects, 40, 900, hid_lg4ff_set_range_g25},
  77. {USB_DEVICE_ID_LOGITECH_G27_WHEEL, lg4ff_wheel_effects, 40, 900, hid_lg4ff_set_range_g25},
  78. {USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2, lg4ff_wheel_effects, 40, 270, NULL},
  79. {USB_DEVICE_ID_LOGITECH_WII_WHEEL, lg4ff_wheel_effects, 40, 270, NULL}
  80. };
  81. struct lg4ff_native_cmd {
  82. const __u8 cmd_num; /* Number of commands to send */
  83. const __u8 cmd[];
  84. };
  85. struct lg4ff_usb_revision {
  86. const __u16 rev_maj;
  87. const __u16 rev_min;
  88. const struct lg4ff_native_cmd *command;
  89. };
  90. static const struct lg4ff_native_cmd native_dfp = {
  91. 1,
  92. {0xf8, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00}
  93. };
  94. static const struct lg4ff_native_cmd native_dfgt = {
  95. 2,
  96. {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* 1st command */
  97. 0xf8, 0x09, 0x03, 0x01, 0x00, 0x00, 0x00} /* 2nd command */
  98. };
  99. static const struct lg4ff_native_cmd native_g25 = {
  100. 1,
  101. {0xf8, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00}
  102. };
  103. static const struct lg4ff_native_cmd native_g27 = {
  104. 2,
  105. {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* 1st command */
  106. 0xf8, 0x09, 0x04, 0x01, 0x00, 0x00, 0x00} /* 2nd command */
  107. };
  108. static const struct lg4ff_usb_revision lg4ff_revs[] = {
  109. {DFGT_REV_MAJ, DFGT_REV_MIN, &native_dfgt}, /* Driving Force GT */
  110. {DFGT_REV_MAJ, DFGT2_REV_MIN, &native_dfgt}, /* Driving Force GT v2 */
  111. {DFP_REV_MAJ, DFP_REV_MIN, &native_dfp}, /* Driving Force Pro */
  112. {G25_REV_MAJ, G25_REV_MIN, &native_g25}, /* G25 */
  113. {G27_REV_MAJ, G27_REV_MIN, &native_g27}, /* G27 */
  114. };
  115. /* Recalculates X axis value accordingly to currently selected range */
  116. static __s32 lg4ff_adjust_dfp_x_axis(__s32 value, __u16 range)
  117. {
  118. __u16 max_range;
  119. __s32 new_value;
  120. if (range == 900)
  121. return value;
  122. else if (range == 200)
  123. return value;
  124. else if (range < 200)
  125. max_range = 200;
  126. else
  127. max_range = 900;
  128. new_value = 8192 + mult_frac(value - 8192, max_range, range);
  129. if (new_value < 0)
  130. return 0;
  131. else if (new_value > 16383)
  132. return 16383;
  133. else
  134. return new_value;
  135. }
  136. int lg4ff_adjust_input_event(struct hid_device *hid, struct hid_field *field,
  137. struct hid_usage *usage, __s32 value, struct lg_drv_data *drv_data)
  138. {
  139. struct lg4ff_device_entry *entry = drv_data->device_props;
  140. __s32 new_value = 0;
  141. if (!entry) {
  142. hid_err(hid, "Device properties not found");
  143. return 0;
  144. }
  145. switch (entry->product_id) {
  146. case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
  147. switch (usage->code) {
  148. case ABS_X:
  149. new_value = lg4ff_adjust_dfp_x_axis(value, entry->range);
  150. input_event(field->hidinput->input, usage->type, usage->code, new_value);
  151. return 1;
  152. default:
  153. return 0;
  154. }
  155. default:
  156. return 0;
  157. }
  158. }
  159. static int hid_lg4ff_play(struct input_dev *dev, void *data, struct ff_effect *effect)
  160. {
  161. struct hid_device *hid = input_get_drvdata(dev);
  162. struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
  163. struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
  164. __s32 *value = report->field[0]->value;
  165. int x;
  166. #define CLAMP(x) do { if (x < 0) x = 0; else if (x > 0xff) x = 0xff; } while (0)
  167. switch (effect->type) {
  168. case FF_CONSTANT:
  169. x = effect->u.ramp.start_level + 0x80; /* 0x80 is no force */
  170. CLAMP(x);
  171. if (x == 0x80) {
  172. /* De-activate force in slot-1*/
  173. value[0] = 0x13;
  174. value[1] = 0x00;
  175. value[2] = 0x00;
  176. value[3] = 0x00;
  177. value[4] = 0x00;
  178. value[5] = 0x00;
  179. value[6] = 0x00;
  180. hid_hw_request(hid, report, HID_REQ_SET_REPORT);
  181. return 0;
  182. }
  183. value[0] = 0x11; /* Slot 1 */
  184. value[1] = 0x08;
  185. value[2] = x;
  186. value[3] = 0x80;
  187. value[4] = 0x00;
  188. value[5] = 0x00;
  189. value[6] = 0x00;
  190. hid_hw_request(hid, report, HID_REQ_SET_REPORT);
  191. break;
  192. }
  193. return 0;
  194. }
  195. /* Sends default autocentering command compatible with
  196. * all wheels except Formula Force EX */
  197. static void hid_lg4ff_set_autocenter_default(struct input_dev *dev, u16 magnitude)
  198. {
  199. struct hid_device *hid = input_get_drvdata(dev);
  200. struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
  201. struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
  202. __s32 *value = report->field[0]->value;
  203. __u32 expand_a, expand_b;
  204. /* De-activate Auto-Center */
  205. if (magnitude == 0) {
  206. value[0] = 0xf5;
  207. value[1] = 0x00;
  208. value[2] = 0x00;
  209. value[3] = 0x00;
  210. value[4] = 0x00;
  211. value[5] = 0x00;
  212. value[6] = 0x00;
  213. hid_hw_request(hid, report, HID_REQ_SET_REPORT);
  214. return;
  215. }
  216. if (magnitude <= 0xaaaa) {
  217. expand_a = 0x0c * magnitude;
  218. expand_b = 0x80 * magnitude;
  219. } else {
  220. expand_a = (0x0c * 0xaaaa) + 0x06 * (magnitude - 0xaaaa);
  221. expand_b = (0x80 * 0xaaaa) + 0xff * (magnitude - 0xaaaa);
  222. }
  223. value[0] = 0xfe;
  224. value[1] = 0x0d;
  225. value[2] = expand_a / 0xaaaa;
  226. value[3] = expand_a / 0xaaaa;
  227. value[4] = expand_b / 0xaaaa;
  228. value[5] = 0x00;
  229. value[6] = 0x00;
  230. hid_hw_request(hid, report, HID_REQ_SET_REPORT);
  231. /* Activate Auto-Center */
  232. value[0] = 0x14;
  233. value[1] = 0x00;
  234. value[2] = 0x00;
  235. value[3] = 0x00;
  236. value[4] = 0x00;
  237. value[5] = 0x00;
  238. value[6] = 0x00;
  239. hid_hw_request(hid, report, HID_REQ_SET_REPORT);
  240. }
  241. /* Sends autocentering command compatible with Formula Force EX */
  242. static void hid_lg4ff_set_autocenter_ffex(struct input_dev *dev, u16 magnitude)
  243. {
  244. struct hid_device *hid = input_get_drvdata(dev);
  245. struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
  246. struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
  247. __s32 *value = report->field[0]->value;
  248. magnitude = magnitude * 90 / 65535;
  249. value[0] = 0xfe;
  250. value[1] = 0x03;
  251. value[2] = magnitude >> 14;
  252. value[3] = magnitude >> 14;
  253. value[4] = magnitude;
  254. value[5] = 0x00;
  255. value[6] = 0x00;
  256. hid_hw_request(hid, report, HID_REQ_SET_REPORT);
  257. }
  258. /* Sends command to set range compatible with G25/G27/Driving Force GT */
  259. static void hid_lg4ff_set_range_g25(struct hid_device *hid, u16 range)
  260. {
  261. struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
  262. struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
  263. __s32 *value = report->field[0]->value;
  264. dbg_hid("G25/G27/DFGT: setting range to %u\n", range);
  265. value[0] = 0xf8;
  266. value[1] = 0x81;
  267. value[2] = range & 0x00ff;
  268. value[3] = (range & 0xff00) >> 8;
  269. value[4] = 0x00;
  270. value[5] = 0x00;
  271. value[6] = 0x00;
  272. hid_hw_request(hid, report, HID_REQ_SET_REPORT);
  273. }
  274. /* Sends commands to set range compatible with Driving Force Pro wheel */
  275. static void hid_lg4ff_set_range_dfp(struct hid_device *hid, __u16 range)
  276. {
  277. struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
  278. struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
  279. int start_left, start_right, full_range;
  280. __s32 *value = report->field[0]->value;
  281. dbg_hid("Driving Force Pro: setting range to %u\n", range);
  282. /* Prepare "coarse" limit command */
  283. value[0] = 0xf8;
  284. value[1] = 0x00; /* Set later */
  285. value[2] = 0x00;
  286. value[3] = 0x00;
  287. value[4] = 0x00;
  288. value[5] = 0x00;
  289. value[6] = 0x00;
  290. if (range > 200) {
  291. report->field[0]->value[1] = 0x03;
  292. full_range = 900;
  293. } else {
  294. report->field[0]->value[1] = 0x02;
  295. full_range = 200;
  296. }
  297. hid_hw_request(hid, report, HID_REQ_SET_REPORT);
  298. /* Prepare "fine" limit command */
  299. value[0] = 0x81;
  300. value[1] = 0x0b;
  301. value[2] = 0x00;
  302. value[3] = 0x00;
  303. value[4] = 0x00;
  304. value[5] = 0x00;
  305. value[6] = 0x00;
  306. if (range == 200 || range == 900) { /* Do not apply any fine limit */
  307. hid_hw_request(hid, report, HID_REQ_SET_REPORT);
  308. return;
  309. }
  310. /* Construct fine limit command */
  311. start_left = (((full_range - range + 1) * 2047) / full_range);
  312. start_right = 0xfff - start_left;
  313. value[2] = start_left >> 4;
  314. value[3] = start_right >> 4;
  315. value[4] = 0xff;
  316. value[5] = (start_right & 0xe) << 4 | (start_left & 0xe);
  317. value[6] = 0xff;
  318. hid_hw_request(hid, report, HID_REQ_SET_REPORT);
  319. }
  320. static void hid_lg4ff_switch_native(struct hid_device *hid, const struct lg4ff_native_cmd *cmd)
  321. {
  322. struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
  323. struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
  324. __u8 i, j;
  325. j = 0;
  326. while (j < 7*cmd->cmd_num) {
  327. for (i = 0; i < 7; i++)
  328. report->field[0]->value[i] = cmd->cmd[j++];
  329. hid_hw_request(hid, report, HID_REQ_SET_REPORT);
  330. }
  331. }
  332. /* Read current range and display it in terminal */
  333. static ssize_t lg4ff_range_show(struct device *dev, struct device_attribute *attr, char *buf)
  334. {
  335. struct hid_device *hid = to_hid_device(dev);
  336. struct lg4ff_device_entry *entry;
  337. struct lg_drv_data *drv_data;
  338. size_t count;
  339. drv_data = hid_get_drvdata(hid);
  340. if (!drv_data) {
  341. hid_err(hid, "Private driver data not found!\n");
  342. return 0;
  343. }
  344. entry = drv_data->device_props;
  345. if (!entry) {
  346. hid_err(hid, "Device properties not found!\n");
  347. return 0;
  348. }
  349. count = scnprintf(buf, PAGE_SIZE, "%u\n", entry->range);
  350. return count;
  351. }
  352. /* Set range to user specified value, call appropriate function
  353. * according to the type of the wheel */
  354. static ssize_t lg4ff_range_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  355. {
  356. struct hid_device *hid = to_hid_device(dev);
  357. struct lg4ff_device_entry *entry;
  358. struct lg_drv_data *drv_data;
  359. __u16 range = simple_strtoul(buf, NULL, 10);
  360. drv_data = hid_get_drvdata(hid);
  361. if (!drv_data) {
  362. hid_err(hid, "Private driver data not found!\n");
  363. return 0;
  364. }
  365. entry = drv_data->device_props;
  366. if (!entry) {
  367. hid_err(hid, "Device properties not found!\n");
  368. return 0;
  369. }
  370. if (range == 0)
  371. range = entry->max_range;
  372. /* Check if the wheel supports range setting
  373. * and that the range is within limits for the wheel */
  374. if (entry->set_range != NULL && range >= entry->min_range && range <= entry->max_range) {
  375. entry->set_range(hid, range);
  376. entry->range = range;
  377. }
  378. return count;
  379. }
  380. #ifdef CONFIG_LEDS_CLASS
  381. static void lg4ff_set_leds(struct hid_device *hid, __u8 leds)
  382. {
  383. struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
  384. struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
  385. __s32 *value = report->field[0]->value;
  386. value[0] = 0xf8;
  387. value[1] = 0x12;
  388. value[2] = leds;
  389. value[3] = 0x00;
  390. value[4] = 0x00;
  391. value[5] = 0x00;
  392. value[6] = 0x00;
  393. hid_hw_request(hid, report, HID_REQ_SET_REPORT);
  394. }
  395. static void lg4ff_led_set_brightness(struct led_classdev *led_cdev,
  396. enum led_brightness value)
  397. {
  398. struct device *dev = led_cdev->dev->parent;
  399. struct hid_device *hid = container_of(dev, struct hid_device, dev);
  400. struct lg_drv_data *drv_data = hid_get_drvdata(hid);
  401. struct lg4ff_device_entry *entry;
  402. int i, state = 0;
  403. if (!drv_data) {
  404. hid_err(hid, "Device data not found.");
  405. return;
  406. }
  407. entry = (struct lg4ff_device_entry *)drv_data->device_props;
  408. if (!entry) {
  409. hid_err(hid, "Device properties not found.");
  410. return;
  411. }
  412. for (i = 0; i < 5; i++) {
  413. if (led_cdev != entry->led[i])
  414. continue;
  415. state = (entry->led_state >> i) & 1;
  416. if (value == LED_OFF && state) {
  417. entry->led_state &= ~(1 << i);
  418. lg4ff_set_leds(hid, entry->led_state);
  419. } else if (value != LED_OFF && !state) {
  420. entry->led_state |= 1 << i;
  421. lg4ff_set_leds(hid, entry->led_state);
  422. }
  423. break;
  424. }
  425. }
  426. static enum led_brightness lg4ff_led_get_brightness(struct led_classdev *led_cdev)
  427. {
  428. struct device *dev = led_cdev->dev->parent;
  429. struct hid_device *hid = container_of(dev, struct hid_device, dev);
  430. struct lg_drv_data *drv_data = hid_get_drvdata(hid);
  431. struct lg4ff_device_entry *entry;
  432. int i, value = 0;
  433. if (!drv_data) {
  434. hid_err(hid, "Device data not found.");
  435. return LED_OFF;
  436. }
  437. entry = (struct lg4ff_device_entry *)drv_data->device_props;
  438. if (!entry) {
  439. hid_err(hid, "Device properties not found.");
  440. return LED_OFF;
  441. }
  442. for (i = 0; i < 5; i++)
  443. if (led_cdev == entry->led[i]) {
  444. value = (entry->led_state >> i) & 1;
  445. break;
  446. }
  447. return value ? LED_FULL : LED_OFF;
  448. }
  449. #endif
  450. int lg4ff_init(struct hid_device *hid)
  451. {
  452. struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
  453. struct input_dev *dev = hidinput->input;
  454. struct lg4ff_device_entry *entry;
  455. struct lg_drv_data *drv_data;
  456. struct usb_device_descriptor *udesc;
  457. int error, i, j;
  458. __u16 bcdDevice, rev_maj, rev_min;
  459. /* Check that the report looks ok */
  460. if (!hid_validate_values(hid, HID_OUTPUT_REPORT, 0, 0, 7))
  461. return -1;
  462. /* Check what wheel has been connected */
  463. for (i = 0; i < ARRAY_SIZE(lg4ff_devices); i++) {
  464. if (hid->product == lg4ff_devices[i].product_id) {
  465. dbg_hid("Found compatible device, product ID %04X\n", lg4ff_devices[i].product_id);
  466. break;
  467. }
  468. }
  469. if (i == ARRAY_SIZE(lg4ff_devices)) {
  470. hid_err(hid, "Device is not supported by lg4ff driver. If you think it should be, consider reporting a bug to"
  471. "LKML, Simon Wood <simon@mungewell.org> or Michal Maly <madcatxster@gmail.com>\n");
  472. return -1;
  473. }
  474. /* Attempt to switch wheel to native mode when applicable */
  475. udesc = &(hid_to_usb_dev(hid)->descriptor);
  476. if (!udesc) {
  477. hid_err(hid, "NULL USB device descriptor\n");
  478. return -1;
  479. }
  480. bcdDevice = le16_to_cpu(udesc->bcdDevice);
  481. rev_maj = bcdDevice >> 8;
  482. rev_min = bcdDevice & 0xff;
  483. if (lg4ff_devices[i].product_id == USB_DEVICE_ID_LOGITECH_WHEEL) {
  484. dbg_hid("Generic wheel detected, can it do native?\n");
  485. dbg_hid("USB revision: %2x.%02x\n", rev_maj, rev_min);
  486. for (j = 0; j < ARRAY_SIZE(lg4ff_revs); j++) {
  487. if (lg4ff_revs[j].rev_maj == rev_maj && lg4ff_revs[j].rev_min == rev_min) {
  488. hid_lg4ff_switch_native(hid, lg4ff_revs[j].command);
  489. hid_info(hid, "Switched to native mode\n");
  490. }
  491. }
  492. }
  493. /* Set supported force feedback capabilities */
  494. for (j = 0; lg4ff_devices[i].ff_effects[j] >= 0; j++)
  495. set_bit(lg4ff_devices[i].ff_effects[j], dev->ffbit);
  496. error = input_ff_create_memless(dev, NULL, hid_lg4ff_play);
  497. if (error)
  498. return error;
  499. /* Check if autocentering is available and
  500. * set the centering force to zero by default */
  501. if (test_bit(FF_AUTOCENTER, dev->ffbit)) {
  502. if (rev_maj == FFEX_REV_MAJ && rev_min == FFEX_REV_MIN) /* Formula Force EX expects different autocentering command */
  503. dev->ff->set_autocenter = hid_lg4ff_set_autocenter_ffex;
  504. else
  505. dev->ff->set_autocenter = hid_lg4ff_set_autocenter_default;
  506. dev->ff->set_autocenter(dev, 0);
  507. }
  508. /* Get private driver data */
  509. drv_data = hid_get_drvdata(hid);
  510. if (!drv_data) {
  511. hid_err(hid, "Cannot add device, private driver data not allocated\n");
  512. return -1;
  513. }
  514. /* Initialize device properties */
  515. entry = kzalloc(sizeof(struct lg4ff_device_entry), GFP_KERNEL);
  516. if (!entry) {
  517. hid_err(hid, "Cannot add device, insufficient memory to allocate device properties.\n");
  518. return -ENOMEM;
  519. }
  520. drv_data->device_props = entry;
  521. entry->product_id = lg4ff_devices[i].product_id;
  522. entry->min_range = lg4ff_devices[i].min_range;
  523. entry->max_range = lg4ff_devices[i].max_range;
  524. entry->set_range = lg4ff_devices[i].set_range;
  525. /* Create sysfs interface */
  526. error = device_create_file(&hid->dev, &dev_attr_range);
  527. if (error)
  528. return error;
  529. dbg_hid("sysfs interface created\n");
  530. /* Set the maximum range to start with */
  531. entry->range = entry->max_range;
  532. if (entry->set_range != NULL)
  533. entry->set_range(hid, entry->range);
  534. #ifdef CONFIG_LEDS_CLASS
  535. /* register led subsystem - G27 only */
  536. entry->led_state = 0;
  537. for (j = 0; j < 5; j++)
  538. entry->led[j] = NULL;
  539. if (lg4ff_devices[i].product_id == USB_DEVICE_ID_LOGITECH_G27_WHEEL) {
  540. struct led_classdev *led;
  541. size_t name_sz;
  542. char *name;
  543. lg4ff_set_leds(hid, 0);
  544. name_sz = strlen(dev_name(&hid->dev)) + 8;
  545. for (j = 0; j < 5; j++) {
  546. led = kzalloc(sizeof(struct led_classdev)+name_sz, GFP_KERNEL);
  547. if (!led) {
  548. hid_err(hid, "can't allocate memory for LED %d\n", j);
  549. goto err;
  550. }
  551. name = (void *)(&led[1]);
  552. snprintf(name, name_sz, "%s::RPM%d", dev_name(&hid->dev), j+1);
  553. led->name = name;
  554. led->brightness = 0;
  555. led->max_brightness = 1;
  556. led->brightness_get = lg4ff_led_get_brightness;
  557. led->brightness_set = lg4ff_led_set_brightness;
  558. entry->led[j] = led;
  559. error = led_classdev_register(&hid->dev, led);
  560. if (error) {
  561. hid_err(hid, "failed to register LED %d. Aborting.\n", j);
  562. err:
  563. /* Deregister LEDs (if any) */
  564. for (j = 0; j < 5; j++) {
  565. led = entry->led[j];
  566. entry->led[j] = NULL;
  567. if (!led)
  568. continue;
  569. led_classdev_unregister(led);
  570. kfree(led);
  571. }
  572. goto out; /* Let the driver continue without LEDs */
  573. }
  574. }
  575. }
  576. out:
  577. #endif
  578. hid_info(hid, "Force feedback support for Logitech Gaming Wheels\n");
  579. return 0;
  580. }
  581. int lg4ff_deinit(struct hid_device *hid)
  582. {
  583. struct lg4ff_device_entry *entry;
  584. struct lg_drv_data *drv_data;
  585. device_remove_file(&hid->dev, &dev_attr_range);
  586. drv_data = hid_get_drvdata(hid);
  587. if (!drv_data) {
  588. hid_err(hid, "Error while deinitializing device, no private driver data.\n");
  589. return -1;
  590. }
  591. entry = drv_data->device_props;
  592. if (!entry) {
  593. hid_err(hid, "Error while deinitializing device, no device properties data.\n");
  594. return -1;
  595. }
  596. #ifdef CONFIG_LEDS_CLASS
  597. {
  598. int j;
  599. struct led_classdev *led;
  600. /* Deregister LEDs (if any) */
  601. for (j = 0; j < 5; j++) {
  602. led = entry->led[j];
  603. entry->led[j] = NULL;
  604. if (!led)
  605. continue;
  606. led_classdev_unregister(led);
  607. kfree(led);
  608. }
  609. }
  610. #endif
  611. /* Deallocate memory */
  612. kfree(entry);
  613. dbg_hid("Device successfully unregistered\n");
  614. return 0;
  615. }