hid-lg4ff.c 19 KB

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