hid-lg4ff.c 19 KB

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