hid-lg4ff.c 18 KB

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