hid-lg4ff.c 19 KB

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