hid-lg4ff.c 19 KB

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