hid-lg4ff.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  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. struct lg4ff_device_entry *entry;
  205. struct lg_drv_data *drv_data;
  206. drv_data = hid_get_drvdata(hid);
  207. if (!drv_data) {
  208. hid_err(hid, "Private driver data not found!\n");
  209. return;
  210. }
  211. entry = drv_data->device_props;
  212. if (!entry) {
  213. hid_err(hid, "Device properties not found!\n");
  214. return;
  215. }
  216. /* De-activate Auto-Center */
  217. if (magnitude == 0) {
  218. value[0] = 0xf5;
  219. value[1] = 0x00;
  220. value[2] = 0x00;
  221. value[3] = 0x00;
  222. value[4] = 0x00;
  223. value[5] = 0x00;
  224. value[6] = 0x00;
  225. hid_hw_request(hid, report, HID_REQ_SET_REPORT);
  226. return;
  227. }
  228. if (magnitude <= 0xaaaa) {
  229. expand_a = 0x0c * magnitude;
  230. expand_b = 0x80 * magnitude;
  231. } else {
  232. expand_a = (0x0c * 0xaaaa) + 0x06 * (magnitude - 0xaaaa);
  233. expand_b = (0x80 * 0xaaaa) + 0xff * (magnitude - 0xaaaa);
  234. }
  235. /* Adjust for non-MOMO wheels */
  236. switch (entry->product_id) {
  237. case USB_DEVICE_ID_LOGITECH_MOMO_WHEEL:
  238. case USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2:
  239. break;
  240. default:
  241. expand_a = expand_a >> 1;
  242. break;
  243. }
  244. value[0] = 0xfe;
  245. value[1] = 0x0d;
  246. value[2] = expand_a / 0xaaaa;
  247. value[3] = expand_a / 0xaaaa;
  248. value[4] = expand_b / 0xaaaa;
  249. value[5] = 0x00;
  250. value[6] = 0x00;
  251. hid_hw_request(hid, report, HID_REQ_SET_REPORT);
  252. /* Activate Auto-Center */
  253. value[0] = 0x14;
  254. value[1] = 0x00;
  255. value[2] = 0x00;
  256. value[3] = 0x00;
  257. value[4] = 0x00;
  258. value[5] = 0x00;
  259. value[6] = 0x00;
  260. hid_hw_request(hid, report, HID_REQ_SET_REPORT);
  261. }
  262. /* Sends autocentering command compatible with Formula Force EX */
  263. static void hid_lg4ff_set_autocenter_ffex(struct input_dev *dev, u16 magnitude)
  264. {
  265. struct hid_device *hid = input_get_drvdata(dev);
  266. struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
  267. struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
  268. __s32 *value = report->field[0]->value;
  269. magnitude = magnitude * 90 / 65535;
  270. value[0] = 0xfe;
  271. value[1] = 0x03;
  272. value[2] = magnitude >> 14;
  273. value[3] = magnitude >> 14;
  274. value[4] = magnitude;
  275. value[5] = 0x00;
  276. value[6] = 0x00;
  277. hid_hw_request(hid, report, HID_REQ_SET_REPORT);
  278. }
  279. /* Sends command to set range compatible with G25/G27/Driving Force GT */
  280. static void hid_lg4ff_set_range_g25(struct hid_device *hid, u16 range)
  281. {
  282. struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
  283. struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
  284. __s32 *value = report->field[0]->value;
  285. dbg_hid("G25/G27/DFGT: setting range to %u\n", range);
  286. value[0] = 0xf8;
  287. value[1] = 0x81;
  288. value[2] = range & 0x00ff;
  289. value[3] = (range & 0xff00) >> 8;
  290. value[4] = 0x00;
  291. value[5] = 0x00;
  292. value[6] = 0x00;
  293. hid_hw_request(hid, report, HID_REQ_SET_REPORT);
  294. }
  295. /* Sends commands to set range compatible with Driving Force Pro wheel */
  296. static void hid_lg4ff_set_range_dfp(struct hid_device *hid, __u16 range)
  297. {
  298. struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
  299. struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
  300. int start_left, start_right, full_range;
  301. __s32 *value = report->field[0]->value;
  302. dbg_hid("Driving Force Pro: setting range to %u\n", range);
  303. /* Prepare "coarse" limit command */
  304. value[0] = 0xf8;
  305. value[1] = 0x00; /* Set later */
  306. value[2] = 0x00;
  307. value[3] = 0x00;
  308. value[4] = 0x00;
  309. value[5] = 0x00;
  310. value[6] = 0x00;
  311. if (range > 200) {
  312. report->field[0]->value[1] = 0x03;
  313. full_range = 900;
  314. } else {
  315. report->field[0]->value[1] = 0x02;
  316. full_range = 200;
  317. }
  318. hid_hw_request(hid, report, HID_REQ_SET_REPORT);
  319. /* Prepare "fine" limit command */
  320. value[0] = 0x81;
  321. value[1] = 0x0b;
  322. value[2] = 0x00;
  323. value[3] = 0x00;
  324. value[4] = 0x00;
  325. value[5] = 0x00;
  326. value[6] = 0x00;
  327. if (range == 200 || range == 900) { /* Do not apply any fine limit */
  328. hid_hw_request(hid, report, HID_REQ_SET_REPORT);
  329. return;
  330. }
  331. /* Construct fine limit command */
  332. start_left = (((full_range - range + 1) * 2047) / full_range);
  333. start_right = 0xfff - start_left;
  334. value[2] = start_left >> 4;
  335. value[3] = start_right >> 4;
  336. value[4] = 0xff;
  337. value[5] = (start_right & 0xe) << 4 | (start_left & 0xe);
  338. value[6] = 0xff;
  339. hid_hw_request(hid, report, HID_REQ_SET_REPORT);
  340. }
  341. static void hid_lg4ff_switch_native(struct hid_device *hid, const struct lg4ff_native_cmd *cmd)
  342. {
  343. struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
  344. struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
  345. __u8 i, j;
  346. j = 0;
  347. while (j < 7*cmd->cmd_num) {
  348. for (i = 0; i < 7; i++)
  349. report->field[0]->value[i] = cmd->cmd[j++];
  350. hid_hw_request(hid, report, HID_REQ_SET_REPORT);
  351. }
  352. }
  353. /* Read current range and display it in terminal */
  354. static ssize_t lg4ff_range_show(struct device *dev, struct device_attribute *attr, char *buf)
  355. {
  356. struct hid_device *hid = to_hid_device(dev);
  357. struct lg4ff_device_entry *entry;
  358. struct lg_drv_data *drv_data;
  359. size_t count;
  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. count = scnprintf(buf, PAGE_SIZE, "%u\n", entry->range);
  371. return count;
  372. }
  373. /* Set range to user specified value, call appropriate function
  374. * according to the type of the wheel */
  375. static ssize_t lg4ff_range_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  376. {
  377. struct hid_device *hid = to_hid_device(dev);
  378. struct lg4ff_device_entry *entry;
  379. struct lg_drv_data *drv_data;
  380. __u16 range = simple_strtoul(buf, NULL, 10);
  381. drv_data = hid_get_drvdata(hid);
  382. if (!drv_data) {
  383. hid_err(hid, "Private driver data not found!\n");
  384. return 0;
  385. }
  386. entry = drv_data->device_props;
  387. if (!entry) {
  388. hid_err(hid, "Device properties not found!\n");
  389. return 0;
  390. }
  391. if (range == 0)
  392. range = entry->max_range;
  393. /* Check if the wheel supports range setting
  394. * and that the range is within limits for the wheel */
  395. if (entry->set_range != NULL && range >= entry->min_range && range <= entry->max_range) {
  396. entry->set_range(hid, range);
  397. entry->range = range;
  398. }
  399. return count;
  400. }
  401. #ifdef CONFIG_LEDS_CLASS
  402. static void lg4ff_set_leds(struct hid_device *hid, __u8 leds)
  403. {
  404. struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
  405. struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
  406. __s32 *value = report->field[0]->value;
  407. value[0] = 0xf8;
  408. value[1] = 0x12;
  409. value[2] = leds;
  410. value[3] = 0x00;
  411. value[4] = 0x00;
  412. value[5] = 0x00;
  413. value[6] = 0x00;
  414. hid_hw_request(hid, report, HID_REQ_SET_REPORT);
  415. }
  416. static void lg4ff_led_set_brightness(struct led_classdev *led_cdev,
  417. enum led_brightness value)
  418. {
  419. struct device *dev = led_cdev->dev->parent;
  420. struct hid_device *hid = container_of(dev, struct hid_device, dev);
  421. struct lg_drv_data *drv_data = hid_get_drvdata(hid);
  422. struct lg4ff_device_entry *entry;
  423. int i, state = 0;
  424. if (!drv_data) {
  425. hid_err(hid, "Device data not found.");
  426. return;
  427. }
  428. entry = (struct lg4ff_device_entry *)drv_data->device_props;
  429. if (!entry) {
  430. hid_err(hid, "Device properties not found.");
  431. return;
  432. }
  433. for (i = 0; i < 5; i++) {
  434. if (led_cdev != entry->led[i])
  435. continue;
  436. state = (entry->led_state >> i) & 1;
  437. if (value == LED_OFF && state) {
  438. entry->led_state &= ~(1 << i);
  439. lg4ff_set_leds(hid, entry->led_state);
  440. } else if (value != LED_OFF && !state) {
  441. entry->led_state |= 1 << i;
  442. lg4ff_set_leds(hid, entry->led_state);
  443. }
  444. break;
  445. }
  446. }
  447. static enum led_brightness lg4ff_led_get_brightness(struct led_classdev *led_cdev)
  448. {
  449. struct device *dev = led_cdev->dev->parent;
  450. struct hid_device *hid = container_of(dev, struct hid_device, dev);
  451. struct lg_drv_data *drv_data = hid_get_drvdata(hid);
  452. struct lg4ff_device_entry *entry;
  453. int i, value = 0;
  454. if (!drv_data) {
  455. hid_err(hid, "Device data not found.");
  456. return LED_OFF;
  457. }
  458. entry = (struct lg4ff_device_entry *)drv_data->device_props;
  459. if (!entry) {
  460. hid_err(hid, "Device properties not found.");
  461. return LED_OFF;
  462. }
  463. for (i = 0; i < 5; i++)
  464. if (led_cdev == entry->led[i]) {
  465. value = (entry->led_state >> i) & 1;
  466. break;
  467. }
  468. return value ? LED_FULL : LED_OFF;
  469. }
  470. #endif
  471. int lg4ff_init(struct hid_device *hid)
  472. {
  473. struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
  474. struct input_dev *dev = hidinput->input;
  475. struct lg4ff_device_entry *entry;
  476. struct lg_drv_data *drv_data;
  477. struct usb_device_descriptor *udesc;
  478. int error, i, j;
  479. __u16 bcdDevice, rev_maj, rev_min;
  480. /* Check that the report looks ok */
  481. if (!hid_validate_values(hid, HID_OUTPUT_REPORT, 0, 0, 7))
  482. return -1;
  483. /* Check what wheel has been connected */
  484. for (i = 0; i < ARRAY_SIZE(lg4ff_devices); i++) {
  485. if (hid->product == lg4ff_devices[i].product_id) {
  486. dbg_hid("Found compatible device, product ID %04X\n", lg4ff_devices[i].product_id);
  487. break;
  488. }
  489. }
  490. if (i == ARRAY_SIZE(lg4ff_devices)) {
  491. hid_err(hid, "Device is not supported by lg4ff driver. If you think it should be, consider reporting a bug to"
  492. "LKML, Simon Wood <simon@mungewell.org> or Michal Maly <madcatxster@gmail.com>\n");
  493. return -1;
  494. }
  495. /* Attempt to switch wheel to native mode when applicable */
  496. udesc = &(hid_to_usb_dev(hid)->descriptor);
  497. if (!udesc) {
  498. hid_err(hid, "NULL USB device descriptor\n");
  499. return -1;
  500. }
  501. bcdDevice = le16_to_cpu(udesc->bcdDevice);
  502. rev_maj = bcdDevice >> 8;
  503. rev_min = bcdDevice & 0xff;
  504. if (lg4ff_devices[i].product_id == USB_DEVICE_ID_LOGITECH_WHEEL) {
  505. dbg_hid("Generic wheel detected, can it do native?\n");
  506. dbg_hid("USB revision: %2x.%02x\n", rev_maj, rev_min);
  507. for (j = 0; j < ARRAY_SIZE(lg4ff_revs); j++) {
  508. if (lg4ff_revs[j].rev_maj == rev_maj && lg4ff_revs[j].rev_min == rev_min) {
  509. hid_lg4ff_switch_native(hid, lg4ff_revs[j].command);
  510. hid_info(hid, "Switched to native mode\n");
  511. }
  512. }
  513. }
  514. /* Set supported force feedback capabilities */
  515. for (j = 0; lg4ff_devices[i].ff_effects[j] >= 0; j++)
  516. set_bit(lg4ff_devices[i].ff_effects[j], dev->ffbit);
  517. error = input_ff_create_memless(dev, NULL, hid_lg4ff_play);
  518. if (error)
  519. return error;
  520. /* Get private driver data */
  521. drv_data = hid_get_drvdata(hid);
  522. if (!drv_data) {
  523. hid_err(hid, "Cannot add device, private driver data not allocated\n");
  524. return -1;
  525. }
  526. /* Initialize device properties */
  527. entry = kzalloc(sizeof(struct lg4ff_device_entry), GFP_KERNEL);
  528. if (!entry) {
  529. hid_err(hid, "Cannot add device, insufficient memory to allocate device properties.\n");
  530. return -ENOMEM;
  531. }
  532. drv_data->device_props = entry;
  533. entry->product_id = lg4ff_devices[i].product_id;
  534. entry->min_range = lg4ff_devices[i].min_range;
  535. entry->max_range = lg4ff_devices[i].max_range;
  536. entry->set_range = lg4ff_devices[i].set_range;
  537. /* Check if autocentering is available and
  538. * set the centering force to zero by default */
  539. if (test_bit(FF_AUTOCENTER, dev->ffbit)) {
  540. if (rev_maj == FFEX_REV_MAJ && rev_min == FFEX_REV_MIN) /* Formula Force EX expects different autocentering command */
  541. dev->ff->set_autocenter = hid_lg4ff_set_autocenter_ffex;
  542. else
  543. dev->ff->set_autocenter = hid_lg4ff_set_autocenter_default;
  544. dev->ff->set_autocenter(dev, 0);
  545. }
  546. /* Create sysfs interface */
  547. error = device_create_file(&hid->dev, &dev_attr_range);
  548. if (error)
  549. return error;
  550. dbg_hid("sysfs interface created\n");
  551. /* Set the maximum range to start with */
  552. entry->range = entry->max_range;
  553. if (entry->set_range != NULL)
  554. entry->set_range(hid, entry->range);
  555. #ifdef CONFIG_LEDS_CLASS
  556. /* register led subsystem - G27 only */
  557. entry->led_state = 0;
  558. for (j = 0; j < 5; j++)
  559. entry->led[j] = NULL;
  560. if (lg4ff_devices[i].product_id == USB_DEVICE_ID_LOGITECH_G27_WHEEL) {
  561. struct led_classdev *led;
  562. size_t name_sz;
  563. char *name;
  564. lg4ff_set_leds(hid, 0);
  565. name_sz = strlen(dev_name(&hid->dev)) + 8;
  566. for (j = 0; j < 5; j++) {
  567. led = kzalloc(sizeof(struct led_classdev)+name_sz, GFP_KERNEL);
  568. if (!led) {
  569. hid_err(hid, "can't allocate memory for LED %d\n", j);
  570. goto err;
  571. }
  572. name = (void *)(&led[1]);
  573. snprintf(name, name_sz, "%s::RPM%d", dev_name(&hid->dev), j+1);
  574. led->name = name;
  575. led->brightness = 0;
  576. led->max_brightness = 1;
  577. led->brightness_get = lg4ff_led_get_brightness;
  578. led->brightness_set = lg4ff_led_set_brightness;
  579. entry->led[j] = led;
  580. error = led_classdev_register(&hid->dev, led);
  581. if (error) {
  582. hid_err(hid, "failed to register LED %d. Aborting.\n", j);
  583. err:
  584. /* Deregister LEDs (if any) */
  585. for (j = 0; j < 5; j++) {
  586. led = entry->led[j];
  587. entry->led[j] = NULL;
  588. if (!led)
  589. continue;
  590. led_classdev_unregister(led);
  591. kfree(led);
  592. }
  593. goto out; /* Let the driver continue without LEDs */
  594. }
  595. }
  596. }
  597. out:
  598. #endif
  599. hid_info(hid, "Force feedback support for Logitech Gaming Wheels\n");
  600. return 0;
  601. }
  602. int lg4ff_deinit(struct hid_device *hid)
  603. {
  604. struct lg4ff_device_entry *entry;
  605. struct lg_drv_data *drv_data;
  606. device_remove_file(&hid->dev, &dev_attr_range);
  607. drv_data = hid_get_drvdata(hid);
  608. if (!drv_data) {
  609. hid_err(hid, "Error while deinitializing device, no private driver data.\n");
  610. return -1;
  611. }
  612. entry = drv_data->device_props;
  613. if (!entry) {
  614. hid_err(hid, "Error while deinitializing device, no device properties data.\n");
  615. return -1;
  616. }
  617. #ifdef CONFIG_LEDS_CLASS
  618. {
  619. int j;
  620. struct led_classdev *led;
  621. /* Deregister LEDs (if any) */
  622. for (j = 0; j < 5; j++) {
  623. led = entry->led[j];
  624. entry->led[j] = NULL;
  625. if (!led)
  626. continue;
  627. led_classdev_unregister(led);
  628. kfree(led);
  629. }
  630. }
  631. #endif
  632. /* Deallocate memory */
  633. kfree(entry);
  634. dbg_hid("Device successfully unregistered\n");
  635. return 0;
  636. }