hid-lg4ff.c 19 KB

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