hid-lg4ff.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354
  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-lg4ff.h"
  30. #include "hid-ids.h"
  31. #define to_hid_device(pdev) container_of(pdev, struct hid_device, dev)
  32. #define LG4FF_MMODE_IS_MULTIMODE 0
  33. #define LG4FF_MMODE_SWITCHED 1
  34. #define LG4FF_MMODE_NOT_MULTIMODE 2
  35. #define LG4FF_MODE_NATIVE_IDX 0
  36. #define LG4FF_MODE_DFEX_IDX 1
  37. #define LG4FF_MODE_DFP_IDX 2
  38. #define LG4FF_MODE_G25_IDX 3
  39. #define LG4FF_MODE_DFGT_IDX 4
  40. #define LG4FF_MODE_G27_IDX 5
  41. #define LG4FF_MODE_MAX_IDX 6
  42. #define LG4FF_MODE_NATIVE BIT(LG4FF_MODE_NATIVE_IDX)
  43. #define LG4FF_MODE_DFEX BIT(LG4FF_MODE_DFEX_IDX)
  44. #define LG4FF_MODE_DFP BIT(LG4FF_MODE_DFP_IDX)
  45. #define LG4FF_MODE_G25 BIT(LG4FF_MODE_G25_IDX)
  46. #define LG4FF_MODE_DFGT BIT(LG4FF_MODE_DFGT_IDX)
  47. #define LG4FF_MODE_G27 BIT(LG4FF_MODE_G27_IDX)
  48. #define LG4FF_DFEX_TAG "DF-EX"
  49. #define LG4FF_DFEX_NAME "Driving Force / Formula EX"
  50. #define LG4FF_DFP_TAG "DFP"
  51. #define LG4FF_DFP_NAME "Driving Force Pro"
  52. #define LG4FF_G25_TAG "G25"
  53. #define LG4FF_G25_NAME "G25 Racing Wheel"
  54. #define LG4FF_G27_TAG "G27"
  55. #define LG4FF_G27_NAME "G27 Racing Wheel"
  56. #define LG4FF_DFGT_TAG "DFGT"
  57. #define LG4FF_DFGT_NAME "Driving Force GT"
  58. #define LG4FF_FFEX_REV_MAJ 0x21
  59. #define LG4FF_FFEX_REV_MIN 0x00
  60. static void lg4ff_set_range_dfp(struct hid_device *hid, u16 range);
  61. static void lg4ff_set_range_g25(struct hid_device *hid, u16 range);
  62. struct lg4ff_wheel_data {
  63. const u32 product_id;
  64. u16 range;
  65. const u16 min_range;
  66. const u16 max_range;
  67. #ifdef CONFIG_LEDS_CLASS
  68. u8 led_state;
  69. struct led_classdev *led[5];
  70. #endif
  71. const u32 alternate_modes;
  72. const char * const real_tag;
  73. const char * const real_name;
  74. const u16 real_product_id;
  75. void (*set_range)(struct hid_device *hid, u16 range);
  76. };
  77. struct lg4ff_device_entry {
  78. spinlock_t report_lock; /* Protect output HID report */
  79. struct hid_report *report;
  80. struct lg4ff_wheel_data wdata;
  81. };
  82. static const signed short lg4ff_wheel_effects[] = {
  83. FF_CONSTANT,
  84. FF_AUTOCENTER,
  85. -1
  86. };
  87. struct lg4ff_wheel {
  88. const u32 product_id;
  89. const signed short *ff_effects;
  90. const u16 min_range;
  91. const u16 max_range;
  92. void (*set_range)(struct hid_device *hid, u16 range);
  93. };
  94. struct lg4ff_compat_mode_switch {
  95. const u8 cmd_count; /* Number of commands to send */
  96. const u8 cmd[];
  97. };
  98. struct lg4ff_wheel_ident_info {
  99. const u16 mask;
  100. const u16 result;
  101. const u16 real_product_id;
  102. };
  103. struct lg4ff_wheel_ident_checklist {
  104. const u32 count;
  105. const struct lg4ff_wheel_ident_info *models[];
  106. };
  107. struct lg4ff_multimode_wheel {
  108. const u16 product_id;
  109. const u32 alternate_modes;
  110. const char *real_tag;
  111. const char *real_name;
  112. };
  113. struct lg4ff_alternate_mode {
  114. const u16 product_id;
  115. const char *tag;
  116. const char *name;
  117. };
  118. static const struct lg4ff_wheel lg4ff_devices[] = {
  119. {USB_DEVICE_ID_LOGITECH_WHEEL, lg4ff_wheel_effects, 40, 270, NULL},
  120. {USB_DEVICE_ID_LOGITECH_MOMO_WHEEL, lg4ff_wheel_effects, 40, 270, NULL},
  121. {USB_DEVICE_ID_LOGITECH_DFP_WHEEL, lg4ff_wheel_effects, 40, 900, lg4ff_set_range_dfp},
  122. {USB_DEVICE_ID_LOGITECH_G25_WHEEL, lg4ff_wheel_effects, 40, 900, lg4ff_set_range_g25},
  123. {USB_DEVICE_ID_LOGITECH_DFGT_WHEEL, lg4ff_wheel_effects, 40, 900, lg4ff_set_range_g25},
  124. {USB_DEVICE_ID_LOGITECH_G27_WHEEL, lg4ff_wheel_effects, 40, 900, lg4ff_set_range_g25},
  125. {USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2, lg4ff_wheel_effects, 40, 270, NULL},
  126. {USB_DEVICE_ID_LOGITECH_WII_WHEEL, lg4ff_wheel_effects, 40, 270, NULL}
  127. };
  128. static const struct lg4ff_multimode_wheel lg4ff_multimode_wheels[] = {
  129. {USB_DEVICE_ID_LOGITECH_DFP_WHEEL,
  130. LG4FF_MODE_NATIVE | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
  131. LG4FF_DFP_TAG, LG4FF_DFP_NAME},
  132. {USB_DEVICE_ID_LOGITECH_G25_WHEEL,
  133. LG4FF_MODE_NATIVE | LG4FF_MODE_G25 | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
  134. LG4FF_G25_TAG, LG4FF_G25_NAME},
  135. {USB_DEVICE_ID_LOGITECH_DFGT_WHEEL,
  136. LG4FF_MODE_NATIVE | LG4FF_MODE_DFGT | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
  137. LG4FF_DFGT_TAG, LG4FF_DFGT_NAME},
  138. {USB_DEVICE_ID_LOGITECH_G27_WHEEL,
  139. LG4FF_MODE_NATIVE | LG4FF_MODE_G27 | LG4FF_MODE_G25 | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
  140. LG4FF_G27_TAG, LG4FF_G27_NAME},
  141. };
  142. static const struct lg4ff_alternate_mode lg4ff_alternate_modes[] = {
  143. [LG4FF_MODE_NATIVE_IDX] = {0, "native", ""},
  144. [LG4FF_MODE_DFEX_IDX] = {USB_DEVICE_ID_LOGITECH_WHEEL, LG4FF_DFEX_TAG, LG4FF_DFEX_NAME},
  145. [LG4FF_MODE_DFP_IDX] = {USB_DEVICE_ID_LOGITECH_DFP_WHEEL, LG4FF_DFP_TAG, LG4FF_DFP_NAME},
  146. [LG4FF_MODE_G25_IDX] = {USB_DEVICE_ID_LOGITECH_G25_WHEEL, LG4FF_G25_TAG, LG4FF_G25_NAME},
  147. [LG4FF_MODE_DFGT_IDX] = {USB_DEVICE_ID_LOGITECH_DFGT_WHEEL, LG4FF_DFGT_TAG, LG4FF_DFGT_NAME},
  148. [LG4FF_MODE_G27_IDX] = {USB_DEVICE_ID_LOGITECH_G27_WHEEL, LG4FF_G27_TAG, LG4FF_G27_NAME}
  149. };
  150. /* Multimode wheel identificators */
  151. static const struct lg4ff_wheel_ident_info lg4ff_dfp_ident_info = {
  152. 0xf000,
  153. 0x1000,
  154. USB_DEVICE_ID_LOGITECH_DFP_WHEEL
  155. };
  156. static const struct lg4ff_wheel_ident_info lg4ff_g25_ident_info = {
  157. 0xff00,
  158. 0x1200,
  159. USB_DEVICE_ID_LOGITECH_G25_WHEEL
  160. };
  161. static const struct lg4ff_wheel_ident_info lg4ff_g27_ident_info = {
  162. 0xfff0,
  163. 0x1230,
  164. USB_DEVICE_ID_LOGITECH_G27_WHEEL
  165. };
  166. static const struct lg4ff_wheel_ident_info lg4ff_dfgt_ident_info = {
  167. 0xff00,
  168. 0x1300,
  169. USB_DEVICE_ID_LOGITECH_DFGT_WHEEL
  170. };
  171. /* Multimode wheel identification checklists */
  172. static const struct lg4ff_wheel_ident_checklist lg4ff_main_checklist = {
  173. 4,
  174. {&lg4ff_dfgt_ident_info,
  175. &lg4ff_g27_ident_info,
  176. &lg4ff_g25_ident_info,
  177. &lg4ff_dfp_ident_info}
  178. };
  179. /* Compatibility mode switching commands */
  180. /* EXT_CMD9 - Understood by G27 and DFGT */
  181. static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_dfex = {
  182. 2,
  183. {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* Revert mode upon USB reset */
  184. 0xf8, 0x09, 0x00, 0x01, 0x00, 0x00, 0x00} /* Switch mode to DF-EX with detach */
  185. };
  186. static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_dfp = {
  187. 2,
  188. {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* Revert mode upon USB reset */
  189. 0xf8, 0x09, 0x01, 0x01, 0x00, 0x00, 0x00} /* Switch mode to DFP with detach */
  190. };
  191. static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_g25 = {
  192. 2,
  193. {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* Revert mode upon USB reset */
  194. 0xf8, 0x09, 0x02, 0x01, 0x00, 0x00, 0x00} /* Switch mode to G25 with detach */
  195. };
  196. static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_dfgt = {
  197. 2,
  198. {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* Revert mode upon USB reset */
  199. 0xf8, 0x09, 0x03, 0x01, 0x00, 0x00, 0x00} /* Switch mode to DFGT with detach */
  200. };
  201. static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_g27 = {
  202. 2,
  203. {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* Revert mode upon USB reset */
  204. 0xf8, 0x09, 0x04, 0x01, 0x00, 0x00, 0x00} /* Switch mode to G27 with detach */
  205. };
  206. /* EXT_CMD1 - Understood by DFP, G25, G27 and DFGT */
  207. static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext01_dfp = {
  208. 1,
  209. {0xf8, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00}
  210. };
  211. /* EXT_CMD16 - Understood by G25 and G27 */
  212. static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext16_g25 = {
  213. 1,
  214. {0xf8, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00}
  215. };
  216. /* Recalculates X axis value accordingly to currently selected range */
  217. static s32 lg4ff_adjust_dfp_x_axis(s32 value, u16 range)
  218. {
  219. u16 max_range;
  220. s32 new_value;
  221. if (range == 900)
  222. return value;
  223. else if (range == 200)
  224. return value;
  225. else if (range < 200)
  226. max_range = 200;
  227. else
  228. max_range = 900;
  229. new_value = 8192 + mult_frac(value - 8192, max_range, range);
  230. if (new_value < 0)
  231. return 0;
  232. else if (new_value > 16383)
  233. return 16383;
  234. else
  235. return new_value;
  236. }
  237. int lg4ff_adjust_input_event(struct hid_device *hid, struct hid_field *field,
  238. struct hid_usage *usage, s32 value, struct lg_drv_data *drv_data)
  239. {
  240. struct lg4ff_device_entry *entry = drv_data->device_props;
  241. s32 new_value = 0;
  242. if (!entry) {
  243. hid_err(hid, "Device properties not found");
  244. return 0;
  245. }
  246. switch (entry->wdata.product_id) {
  247. case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
  248. switch (usage->code) {
  249. case ABS_X:
  250. new_value = lg4ff_adjust_dfp_x_axis(value, entry->wdata.range);
  251. input_event(field->hidinput->input, usage->type, usage->code, new_value);
  252. return 1;
  253. default:
  254. return 0;
  255. }
  256. default:
  257. return 0;
  258. }
  259. }
  260. static void lg4ff_init_wheel_data(struct lg4ff_wheel_data * const wdata, const struct lg4ff_wheel *wheel,
  261. const struct lg4ff_multimode_wheel *mmode_wheel,
  262. const u16 real_product_id)
  263. {
  264. u32 alternate_modes = 0;
  265. const char *real_tag = NULL;
  266. const char *real_name = NULL;
  267. if (mmode_wheel) {
  268. alternate_modes = mmode_wheel->alternate_modes;
  269. real_tag = mmode_wheel->real_tag;
  270. real_name = mmode_wheel->real_name;
  271. }
  272. {
  273. struct lg4ff_wheel_data t_wdata = { .product_id = wheel->product_id,
  274. .real_product_id = real_product_id,
  275. .min_range = wheel->min_range,
  276. .max_range = wheel->max_range,
  277. .set_range = wheel->set_range,
  278. .alternate_modes = alternate_modes,
  279. .real_tag = real_tag,
  280. .real_name = real_name };
  281. memcpy(wdata, &t_wdata, sizeof(t_wdata));
  282. }
  283. }
  284. static int lg4ff_play(struct input_dev *dev, void *data, struct ff_effect *effect)
  285. {
  286. struct hid_device *hid = input_get_drvdata(dev);
  287. struct lg4ff_device_entry *entry;
  288. struct lg_drv_data *drv_data;
  289. unsigned long flags;
  290. s32 *value;
  291. int x;
  292. drv_data = hid_get_drvdata(hid);
  293. if (!drv_data) {
  294. hid_err(hid, "Private driver data not found!\n");
  295. return -EINVAL;
  296. }
  297. entry = drv_data->device_props;
  298. if (!entry) {
  299. hid_err(hid, "Device properties not found!\n");
  300. return -EINVAL;
  301. }
  302. value = entry->report->field[0]->value;
  303. #define CLAMP(x) do { if (x < 0) x = 0; else if (x > 0xff) x = 0xff; } while (0)
  304. switch (effect->type) {
  305. case FF_CONSTANT:
  306. x = effect->u.ramp.start_level + 0x80; /* 0x80 is no force */
  307. CLAMP(x);
  308. spin_lock_irqsave(&entry->report_lock, flags);
  309. if (x == 0x80) {
  310. /* De-activate force in slot-1*/
  311. value[0] = 0x13;
  312. value[1] = 0x00;
  313. value[2] = 0x00;
  314. value[3] = 0x00;
  315. value[4] = 0x00;
  316. value[5] = 0x00;
  317. value[6] = 0x00;
  318. hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
  319. spin_unlock_irqrestore(&entry->report_lock, flags);
  320. return 0;
  321. }
  322. value[0] = 0x11; /* Slot 1 */
  323. value[1] = 0x08;
  324. value[2] = x;
  325. value[3] = 0x80;
  326. value[4] = 0x00;
  327. value[5] = 0x00;
  328. value[6] = 0x00;
  329. hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
  330. spin_unlock_irqrestore(&entry->report_lock, flags);
  331. break;
  332. }
  333. return 0;
  334. }
  335. /* Sends default autocentering command compatible with
  336. * all wheels except Formula Force EX */
  337. static void lg4ff_set_autocenter_default(struct input_dev *dev, u16 magnitude)
  338. {
  339. struct hid_device *hid = input_get_drvdata(dev);
  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. u32 expand_a, expand_b;
  344. struct lg4ff_device_entry *entry;
  345. struct lg_drv_data *drv_data;
  346. unsigned long flags;
  347. drv_data = hid_get_drvdata(hid);
  348. if (!drv_data) {
  349. hid_err(hid, "Private driver data not found!\n");
  350. return;
  351. }
  352. entry = drv_data->device_props;
  353. if (!entry) {
  354. hid_err(hid, "Device properties not found!\n");
  355. return;
  356. }
  357. value = entry->report->field[0]->value;
  358. /* De-activate Auto-Center */
  359. spin_lock_irqsave(&entry->report_lock, flags);
  360. if (magnitude == 0) {
  361. value[0] = 0xf5;
  362. value[1] = 0x00;
  363. value[2] = 0x00;
  364. value[3] = 0x00;
  365. value[4] = 0x00;
  366. value[5] = 0x00;
  367. value[6] = 0x00;
  368. hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
  369. spin_unlock_irqrestore(&entry->report_lock, flags);
  370. return;
  371. }
  372. if (magnitude <= 0xaaaa) {
  373. expand_a = 0x0c * magnitude;
  374. expand_b = 0x80 * magnitude;
  375. } else {
  376. expand_a = (0x0c * 0xaaaa) + 0x06 * (magnitude - 0xaaaa);
  377. expand_b = (0x80 * 0xaaaa) + 0xff * (magnitude - 0xaaaa);
  378. }
  379. /* Adjust for non-MOMO wheels */
  380. switch (entry->wdata.product_id) {
  381. case USB_DEVICE_ID_LOGITECH_MOMO_WHEEL:
  382. case USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2:
  383. break;
  384. default:
  385. expand_a = expand_a >> 1;
  386. break;
  387. }
  388. value[0] = 0xfe;
  389. value[1] = 0x0d;
  390. value[2] = expand_a / 0xaaaa;
  391. value[3] = expand_a / 0xaaaa;
  392. value[4] = expand_b / 0xaaaa;
  393. value[5] = 0x00;
  394. value[6] = 0x00;
  395. hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
  396. /* Activate Auto-Center */
  397. value[0] = 0x14;
  398. value[1] = 0x00;
  399. value[2] = 0x00;
  400. value[3] = 0x00;
  401. value[4] = 0x00;
  402. value[5] = 0x00;
  403. value[6] = 0x00;
  404. hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
  405. spin_unlock_irqrestore(&entry->report_lock, flags);
  406. }
  407. /* Sends autocentering command compatible with Formula Force EX */
  408. static void lg4ff_set_autocenter_ffex(struct input_dev *dev, u16 magnitude)
  409. {
  410. struct hid_device *hid = input_get_drvdata(dev);
  411. struct lg4ff_device_entry *entry;
  412. struct lg_drv_data *drv_data;
  413. unsigned long flags;
  414. s32 *value;
  415. magnitude = magnitude * 90 / 65535;
  416. drv_data = hid_get_drvdata(hid);
  417. if (!drv_data) {
  418. hid_err(hid, "Private driver data not found!\n");
  419. return;
  420. }
  421. entry = drv_data->device_props;
  422. if (!entry) {
  423. hid_err(hid, "Device properties not found!\n");
  424. return;
  425. }
  426. value = entry->report->field[0]->value;
  427. spin_lock_irqsave(&entry->report_lock, flags);
  428. value[0] = 0xfe;
  429. value[1] = 0x03;
  430. value[2] = magnitude >> 14;
  431. value[3] = magnitude >> 14;
  432. value[4] = magnitude;
  433. value[5] = 0x00;
  434. value[6] = 0x00;
  435. hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
  436. spin_unlock_irqrestore(&entry->report_lock, flags);
  437. }
  438. /* Sends command to set range compatible with G25/G27/Driving Force GT */
  439. static void lg4ff_set_range_g25(struct hid_device *hid, u16 range)
  440. {
  441. struct lg4ff_device_entry *entry;
  442. struct lg_drv_data *drv_data;
  443. unsigned long flags;
  444. s32 *value;
  445. drv_data = hid_get_drvdata(hid);
  446. if (!drv_data) {
  447. hid_err(hid, "Private driver data not found!\n");
  448. return;
  449. }
  450. entry = drv_data->device_props;
  451. if (!entry) {
  452. hid_err(hid, "Device properties not found!\n");
  453. return;
  454. }
  455. value = entry->report->field[0]->value;
  456. dbg_hid("G25/G27/DFGT: setting range to %u\n", range);
  457. spin_lock_irqsave(&entry->report_lock, flags);
  458. value[0] = 0xf8;
  459. value[1] = 0x81;
  460. value[2] = range & 0x00ff;
  461. value[3] = (range & 0xff00) >> 8;
  462. value[4] = 0x00;
  463. value[5] = 0x00;
  464. value[6] = 0x00;
  465. hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
  466. spin_unlock_irqrestore(&entry->report_lock, flags);
  467. }
  468. /* Sends commands to set range compatible with Driving Force Pro wheel */
  469. static void lg4ff_set_range_dfp(struct hid_device *hid, u16 range)
  470. {
  471. struct lg4ff_device_entry *entry;
  472. struct lg_drv_data *drv_data;
  473. unsigned long flags;
  474. int start_left, start_right, full_range;
  475. s32 *value;
  476. drv_data = hid_get_drvdata(hid);
  477. if (!drv_data) {
  478. hid_err(hid, "Private driver data not found!\n");
  479. return;
  480. }
  481. entry = drv_data->device_props;
  482. if (!entry) {
  483. hid_err(hid, "Device properties not found!\n");
  484. return;
  485. }
  486. value = entry->report->field[0]->value;
  487. dbg_hid("Driving Force Pro: setting range to %u\n", range);
  488. /* Prepare "coarse" limit command */
  489. spin_lock_irqsave(&entry->report_lock, flags);
  490. value[0] = 0xf8;
  491. value[1] = 0x00; /* Set later */
  492. value[2] = 0x00;
  493. value[3] = 0x00;
  494. value[4] = 0x00;
  495. value[5] = 0x00;
  496. value[6] = 0x00;
  497. if (range > 200) {
  498. value[1] = 0x03;
  499. full_range = 900;
  500. } else {
  501. value[1] = 0x02;
  502. full_range = 200;
  503. }
  504. hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
  505. /* Prepare "fine" limit command */
  506. value[0] = 0x81;
  507. value[1] = 0x0b;
  508. value[2] = 0x00;
  509. value[3] = 0x00;
  510. value[4] = 0x00;
  511. value[5] = 0x00;
  512. value[6] = 0x00;
  513. if (range == 200 || range == 900) { /* Do not apply any fine limit */
  514. hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
  515. spin_unlock_irqrestore(&entry->report_lock, flags);
  516. return;
  517. }
  518. /* Construct fine limit command */
  519. start_left = (((full_range - range + 1) * 2047) / full_range);
  520. start_right = 0xfff - start_left;
  521. value[2] = start_left >> 4;
  522. value[3] = start_right >> 4;
  523. value[4] = 0xff;
  524. value[5] = (start_right & 0xe) << 4 | (start_left & 0xe);
  525. value[6] = 0xff;
  526. hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
  527. spin_unlock_irqrestore(&entry->report_lock, flags);
  528. }
  529. static const struct lg4ff_compat_mode_switch *lg4ff_get_mode_switch_command(const u16 real_product_id, const u16 target_product_id)
  530. {
  531. switch (real_product_id) {
  532. case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
  533. switch (target_product_id) {
  534. case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
  535. return &lg4ff_mode_switch_ext01_dfp;
  536. /* DFP can only be switched to its native mode */
  537. default:
  538. return NULL;
  539. }
  540. break;
  541. case USB_DEVICE_ID_LOGITECH_G25_WHEEL:
  542. switch (target_product_id) {
  543. case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
  544. return &lg4ff_mode_switch_ext01_dfp;
  545. case USB_DEVICE_ID_LOGITECH_G25_WHEEL:
  546. return &lg4ff_mode_switch_ext16_g25;
  547. /* G25 can only be switched to DFP mode or its native mode */
  548. default:
  549. return NULL;
  550. }
  551. break;
  552. case USB_DEVICE_ID_LOGITECH_G27_WHEEL:
  553. switch (target_product_id) {
  554. case USB_DEVICE_ID_LOGITECH_WHEEL:
  555. return &lg4ff_mode_switch_ext09_dfex;
  556. case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
  557. return &lg4ff_mode_switch_ext09_dfp;
  558. case USB_DEVICE_ID_LOGITECH_G25_WHEEL:
  559. return &lg4ff_mode_switch_ext09_g25;
  560. case USB_DEVICE_ID_LOGITECH_G27_WHEEL:
  561. return &lg4ff_mode_switch_ext09_g27;
  562. /* G27 can only be switched to DF-EX, DFP, G25 or its native mode */
  563. default:
  564. return NULL;
  565. }
  566. break;
  567. case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL:
  568. switch (target_product_id) {
  569. case USB_DEVICE_ID_LOGITECH_WHEEL:
  570. return &lg4ff_mode_switch_ext09_dfex;
  571. case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
  572. return &lg4ff_mode_switch_ext09_dfp;
  573. case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL:
  574. return &lg4ff_mode_switch_ext09_dfgt;
  575. /* DFGT can only be switched to DF-EX, DFP or its native mode */
  576. default:
  577. return NULL;
  578. }
  579. break;
  580. /* No other wheels have multiple modes */
  581. default:
  582. return NULL;
  583. }
  584. }
  585. static int lg4ff_switch_compatibility_mode(struct hid_device *hid, const struct lg4ff_compat_mode_switch *s)
  586. {
  587. struct lg4ff_device_entry *entry;
  588. struct lg_drv_data *drv_data;
  589. unsigned long flags;
  590. s32 *value;
  591. u8 i;
  592. drv_data = hid_get_drvdata(hid);
  593. if (!drv_data) {
  594. hid_err(hid, "Private driver data not found!\n");
  595. return -EINVAL;
  596. }
  597. entry = drv_data->device_props;
  598. if (!entry) {
  599. hid_err(hid, "Device properties not found!\n");
  600. return -EINVAL;
  601. }
  602. value = entry->report->field[0]->value;
  603. spin_lock_irqsave(&entry->report_lock, flags);
  604. for (i = 0; i < s->cmd_count; i++) {
  605. u8 j;
  606. for (j = 0; j < 7; j++)
  607. value[j] = s->cmd[j + (7*i)];
  608. hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
  609. }
  610. spin_unlock_irqrestore(&entry->report_lock, flags);
  611. hid_hw_wait(hid);
  612. return 0;
  613. }
  614. static ssize_t lg4ff_alternate_modes_show(struct device *dev, struct device_attribute *attr, char *buf)
  615. {
  616. struct hid_device *hid = to_hid_device(dev);
  617. struct lg4ff_device_entry *entry;
  618. struct lg_drv_data *drv_data;
  619. ssize_t count = 0;
  620. int i;
  621. drv_data = hid_get_drvdata(hid);
  622. if (!drv_data) {
  623. hid_err(hid, "Private driver data not found!\n");
  624. return 0;
  625. }
  626. entry = drv_data->device_props;
  627. if (!entry) {
  628. hid_err(hid, "Device properties not found!\n");
  629. return 0;
  630. }
  631. if (!entry->wdata.real_name) {
  632. hid_err(hid, "NULL pointer to string\n");
  633. return 0;
  634. }
  635. for (i = 0; i < LG4FF_MODE_MAX_IDX; i++) {
  636. if (entry->wdata.alternate_modes & BIT(i)) {
  637. /* Print tag and full name */
  638. count += scnprintf(buf + count, PAGE_SIZE - count, "%s: %s",
  639. lg4ff_alternate_modes[i].tag,
  640. !lg4ff_alternate_modes[i].product_id ? entry->wdata.real_name : lg4ff_alternate_modes[i].name);
  641. if (count >= PAGE_SIZE - 1)
  642. return count;
  643. /* Mark the currently active mode with an asterisk */
  644. if (lg4ff_alternate_modes[i].product_id == entry->wdata.product_id ||
  645. (lg4ff_alternate_modes[i].product_id == 0 && entry->wdata.product_id == entry->wdata.real_product_id))
  646. count += scnprintf(buf + count, PAGE_SIZE - count, " *\n");
  647. else
  648. count += scnprintf(buf + count, PAGE_SIZE - count, "\n");
  649. if (count >= PAGE_SIZE - 1)
  650. return count;
  651. }
  652. }
  653. return count;
  654. }
  655. static ssize_t lg4ff_alternate_modes_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  656. {
  657. struct hid_device *hid = to_hid_device(dev);
  658. struct lg4ff_device_entry *entry;
  659. struct lg_drv_data *drv_data;
  660. const struct lg4ff_compat_mode_switch *s;
  661. u16 target_product_id = 0;
  662. int i, ret;
  663. char *lbuf;
  664. drv_data = hid_get_drvdata(hid);
  665. if (!drv_data) {
  666. hid_err(hid, "Private driver data not found!\n");
  667. return -EINVAL;
  668. }
  669. entry = drv_data->device_props;
  670. if (!entry) {
  671. hid_err(hid, "Device properties not found!\n");
  672. return -EINVAL;
  673. }
  674. /* Allow \n at the end of the input parameter */
  675. lbuf = kasprintf(GFP_KERNEL, "%s", buf);
  676. if (!lbuf)
  677. return -ENOMEM;
  678. i = strlen(lbuf);
  679. if (lbuf[i-1] == '\n') {
  680. if (i == 1) {
  681. kfree(lbuf);
  682. return -EINVAL;
  683. }
  684. lbuf[i-1] = '\0';
  685. }
  686. for (i = 0; i < LG4FF_MODE_MAX_IDX; i++) {
  687. const u16 mode_product_id = lg4ff_alternate_modes[i].product_id;
  688. const char *tag = lg4ff_alternate_modes[i].tag;
  689. if (entry->wdata.alternate_modes & BIT(i)) {
  690. if (!strcmp(tag, lbuf)) {
  691. if (!mode_product_id)
  692. target_product_id = entry->wdata.real_product_id;
  693. else
  694. target_product_id = mode_product_id;
  695. break;
  696. }
  697. }
  698. }
  699. if (i == LG4FF_MODE_MAX_IDX) {
  700. hid_info(hid, "Requested mode \"%s\" is not supported by the device\n", lbuf);
  701. kfree(lbuf);
  702. return -EINVAL;
  703. }
  704. kfree(lbuf); /* Not needed anymore */
  705. if (target_product_id == entry->wdata.product_id) /* Nothing to do */
  706. return count;
  707. /* Automatic switching has to be disabled for the switch to DF-EX mode to work correctly */
  708. if (target_product_id == USB_DEVICE_ID_LOGITECH_WHEEL && !lg4ff_no_autoswitch) {
  709. hid_info(hid, "\"%s\" cannot be switched to \"DF-EX\" mode. Load the \"hid_logitech\" module with \"lg4ff_no_autoswitch=1\" parameter set and try again\n",
  710. entry->wdata.real_name);
  711. return -EINVAL;
  712. }
  713. /* Take care of hardware limitations */
  714. if ((entry->wdata.real_product_id == USB_DEVICE_ID_LOGITECH_DFP_WHEEL || entry->wdata.real_product_id == USB_DEVICE_ID_LOGITECH_G25_WHEEL) &&
  715. entry->wdata.product_id > target_product_id) {
  716. hid_info(hid, "\"%s\" cannot be switched back into \"%s\" mode\n", entry->wdata.real_name, lg4ff_alternate_modes[i].name);
  717. return -EINVAL;
  718. }
  719. s = lg4ff_get_mode_switch_command(entry->wdata.real_product_id, target_product_id);
  720. if (!s) {
  721. hid_err(hid, "Invalid target product ID %X\n", target_product_id);
  722. return -EINVAL;
  723. }
  724. ret = lg4ff_switch_compatibility_mode(hid, s);
  725. return (ret == 0 ? count : ret);
  726. }
  727. static DEVICE_ATTR(alternate_modes, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, lg4ff_alternate_modes_show, lg4ff_alternate_modes_store);
  728. /* Export the currently set range of the wheel */
  729. static ssize_t lg4ff_range_show(struct device *dev, struct device_attribute *attr,
  730. char *buf)
  731. {
  732. struct hid_device *hid = to_hid_device(dev);
  733. struct lg4ff_device_entry *entry;
  734. struct lg_drv_data *drv_data;
  735. size_t count;
  736. drv_data = hid_get_drvdata(hid);
  737. if (!drv_data) {
  738. hid_err(hid, "Private driver data not found!\n");
  739. return 0;
  740. }
  741. entry = drv_data->device_props;
  742. if (!entry) {
  743. hid_err(hid, "Device properties not found!\n");
  744. return 0;
  745. }
  746. count = scnprintf(buf, PAGE_SIZE, "%u\n", entry->wdata.range);
  747. return count;
  748. }
  749. /* Set range to user specified value, call appropriate function
  750. * according to the type of the wheel */
  751. static ssize_t lg4ff_range_store(struct device *dev, struct device_attribute *attr,
  752. const char *buf, size_t count)
  753. {
  754. struct hid_device *hid = to_hid_device(dev);
  755. struct lg4ff_device_entry *entry;
  756. struct lg_drv_data *drv_data;
  757. u16 range = simple_strtoul(buf, NULL, 10);
  758. drv_data = hid_get_drvdata(hid);
  759. if (!drv_data) {
  760. hid_err(hid, "Private driver data not found!\n");
  761. return -EINVAL;
  762. }
  763. entry = drv_data->device_props;
  764. if (!entry) {
  765. hid_err(hid, "Device properties not found!\n");
  766. return -EINVAL;
  767. }
  768. if (range == 0)
  769. range = entry->wdata.max_range;
  770. /* Check if the wheel supports range setting
  771. * and that the range is within limits for the wheel */
  772. if (entry->wdata.set_range && range >= entry->wdata.min_range && range <= entry->wdata.max_range) {
  773. entry->wdata.set_range(hid, range);
  774. entry->wdata.range = range;
  775. }
  776. return count;
  777. }
  778. static DEVICE_ATTR(range, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, lg4ff_range_show, lg4ff_range_store);
  779. static ssize_t lg4ff_real_id_show(struct device *dev, struct device_attribute *attr, char *buf)
  780. {
  781. struct hid_device *hid = to_hid_device(dev);
  782. struct lg4ff_device_entry *entry;
  783. struct lg_drv_data *drv_data;
  784. size_t count;
  785. drv_data = hid_get_drvdata(hid);
  786. if (!drv_data) {
  787. hid_err(hid, "Private driver data not found!\n");
  788. return 0;
  789. }
  790. entry = drv_data->device_props;
  791. if (!entry) {
  792. hid_err(hid, "Device properties not found!\n");
  793. return 0;
  794. }
  795. if (!entry->wdata.real_tag || !entry->wdata.real_name) {
  796. hid_err(hid, "NULL pointer to string\n");
  797. return 0;
  798. }
  799. count = scnprintf(buf, PAGE_SIZE, "%s: %s\n", entry->wdata.real_tag, entry->wdata.real_name);
  800. return count;
  801. }
  802. static ssize_t lg4ff_real_id_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  803. {
  804. /* Real ID is a read-only value */
  805. return -EPERM;
  806. }
  807. static DEVICE_ATTR(real_id, S_IRUGO, lg4ff_real_id_show, lg4ff_real_id_store);
  808. #ifdef CONFIG_LEDS_CLASS
  809. static void lg4ff_set_leds(struct hid_device *hid, u8 leds)
  810. {
  811. struct lg_drv_data *drv_data;
  812. struct lg4ff_device_entry *entry;
  813. unsigned long flags;
  814. s32 *value;
  815. drv_data = hid_get_drvdata(hid);
  816. if (!drv_data) {
  817. hid_err(hid, "Private driver data not found!\n");
  818. return;
  819. }
  820. entry = drv_data->device_props;
  821. if (!entry) {
  822. hid_err(hid, "Device properties not found!\n");
  823. return;
  824. }
  825. value = entry->report->field[0]->value;
  826. spin_lock_irqsave(&entry->report_lock, flags);
  827. value[0] = 0xf8;
  828. value[1] = 0x12;
  829. value[2] = leds;
  830. value[3] = 0x00;
  831. value[4] = 0x00;
  832. value[5] = 0x00;
  833. value[6] = 0x00;
  834. hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
  835. spin_unlock_irqrestore(&entry->report_lock, flags);
  836. }
  837. static void lg4ff_led_set_brightness(struct led_classdev *led_cdev,
  838. enum led_brightness value)
  839. {
  840. struct device *dev = led_cdev->dev->parent;
  841. struct hid_device *hid = container_of(dev, struct hid_device, dev);
  842. struct lg_drv_data *drv_data = hid_get_drvdata(hid);
  843. struct lg4ff_device_entry *entry;
  844. int i, state = 0;
  845. if (!drv_data) {
  846. hid_err(hid, "Device data not found.");
  847. return;
  848. }
  849. entry = drv_data->device_props;
  850. if (!entry) {
  851. hid_err(hid, "Device properties not found.");
  852. return;
  853. }
  854. for (i = 0; i < 5; i++) {
  855. if (led_cdev != entry->wdata.led[i])
  856. continue;
  857. state = (entry->wdata.led_state >> i) & 1;
  858. if (value == LED_OFF && state) {
  859. entry->wdata.led_state &= ~(1 << i);
  860. lg4ff_set_leds(hid, entry->wdata.led_state);
  861. } else if (value != LED_OFF && !state) {
  862. entry->wdata.led_state |= 1 << i;
  863. lg4ff_set_leds(hid, entry->wdata.led_state);
  864. }
  865. break;
  866. }
  867. }
  868. static enum led_brightness lg4ff_led_get_brightness(struct led_classdev *led_cdev)
  869. {
  870. struct device *dev = led_cdev->dev->parent;
  871. struct hid_device *hid = container_of(dev, struct hid_device, dev);
  872. struct lg_drv_data *drv_data = hid_get_drvdata(hid);
  873. struct lg4ff_device_entry *entry;
  874. int i, value = 0;
  875. if (!drv_data) {
  876. hid_err(hid, "Device data not found.");
  877. return LED_OFF;
  878. }
  879. entry = drv_data->device_props;
  880. if (!entry) {
  881. hid_err(hid, "Device properties not found.");
  882. return LED_OFF;
  883. }
  884. for (i = 0; i < 5; i++)
  885. if (led_cdev == entry->wdata.led[i]) {
  886. value = (entry->wdata.led_state >> i) & 1;
  887. break;
  888. }
  889. return value ? LED_FULL : LED_OFF;
  890. }
  891. #endif
  892. static u16 lg4ff_identify_multimode_wheel(struct hid_device *hid, const u16 reported_product_id, const u16 bcdDevice)
  893. {
  894. const struct lg4ff_wheel_ident_checklist *checklist;
  895. int i, from_idx, to_idx;
  896. switch (reported_product_id) {
  897. case USB_DEVICE_ID_LOGITECH_WHEEL:
  898. case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
  899. checklist = &lg4ff_main_checklist;
  900. from_idx = 0;
  901. to_idx = checklist->count - 1;
  902. break;
  903. case USB_DEVICE_ID_LOGITECH_G25_WHEEL:
  904. checklist = &lg4ff_main_checklist;
  905. from_idx = 0;
  906. to_idx = checklist->count - 2; /* End identity check at G25 */
  907. break;
  908. case USB_DEVICE_ID_LOGITECH_G27_WHEEL:
  909. checklist = &lg4ff_main_checklist;
  910. from_idx = 1; /* Start identity check at G27 */
  911. to_idx = checklist->count - 3; /* End identity check at G27 */
  912. break;
  913. case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL:
  914. checklist = &lg4ff_main_checklist;
  915. from_idx = 0;
  916. to_idx = checklist->count - 4; /* End identity check at DFGT */
  917. break;
  918. default:
  919. return 0;
  920. }
  921. for (i = from_idx; i <= to_idx; i++) {
  922. const u16 mask = checklist->models[i]->mask;
  923. const u16 result = checklist->models[i]->result;
  924. const u16 real_product_id = checklist->models[i]->real_product_id;
  925. if ((bcdDevice & mask) == result) {
  926. dbg_hid("Found wheel with real PID %X whose reported PID is %X\n", real_product_id, reported_product_id);
  927. return real_product_id;
  928. }
  929. }
  930. /* No match found. This is either Driving Force or an unknown
  931. * wheel model, do not touch it */
  932. dbg_hid("Wheel with bcdDevice %X was not recognized as multimode wheel, leaving in its current mode\n", bcdDevice);
  933. return 0;
  934. }
  935. static int lg4ff_handle_multimode_wheel(struct hid_device *hid, u16 *real_product_id, const u16 bcdDevice)
  936. {
  937. const u16 reported_product_id = hid->product;
  938. int ret;
  939. *real_product_id = lg4ff_identify_multimode_wheel(hid, reported_product_id, bcdDevice);
  940. /* Probed wheel is not a multimode wheel */
  941. if (!*real_product_id) {
  942. *real_product_id = reported_product_id;
  943. dbg_hid("Wheel is not a multimode wheel\n");
  944. return LG4FF_MMODE_NOT_MULTIMODE;
  945. }
  946. /* Switch from "Driving Force" mode to native mode automatically.
  947. * Otherwise keep the wheel in its current mode */
  948. if (reported_product_id == USB_DEVICE_ID_LOGITECH_WHEEL &&
  949. reported_product_id != *real_product_id &&
  950. !lg4ff_no_autoswitch) {
  951. const struct lg4ff_compat_mode_switch *s = lg4ff_get_mode_switch_command(*real_product_id, *real_product_id);
  952. if (!s) {
  953. hid_err(hid, "Invalid product id %X\n", *real_product_id);
  954. return LG4FF_MMODE_NOT_MULTIMODE;
  955. }
  956. ret = lg4ff_switch_compatibility_mode(hid, s);
  957. if (ret) {
  958. /* Wheel could not have been switched to native mode,
  959. * leave it in "Driving Force" mode and continue */
  960. hid_err(hid, "Unable to switch wheel mode, errno %d\n", ret);
  961. return LG4FF_MMODE_IS_MULTIMODE;
  962. }
  963. return LG4FF_MMODE_SWITCHED;
  964. }
  965. return LG4FF_MMODE_IS_MULTIMODE;
  966. }
  967. int lg4ff_init(struct hid_device *hid)
  968. {
  969. struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
  970. struct input_dev *dev = hidinput->input;
  971. struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
  972. struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
  973. const struct usb_device_descriptor *udesc = &(hid_to_usb_dev(hid)->descriptor);
  974. const u16 bcdDevice = le16_to_cpu(udesc->bcdDevice);
  975. const struct lg4ff_multimode_wheel *mmode_wheel = NULL;
  976. struct lg4ff_device_entry *entry;
  977. struct lg_drv_data *drv_data;
  978. int error, i, j;
  979. int mmode_ret, mmode_idx = -1;
  980. u16 real_product_id;
  981. /* Check that the report looks ok */
  982. if (!hid_validate_values(hid, HID_OUTPUT_REPORT, 0, 0, 7))
  983. return -1;
  984. drv_data = hid_get_drvdata(hid);
  985. if (!drv_data) {
  986. hid_err(hid, "Cannot add device, private driver data not allocated\n");
  987. return -1;
  988. }
  989. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  990. if (!entry)
  991. return -ENOMEM;
  992. spin_lock_init(&entry->report_lock);
  993. entry->report = report;
  994. drv_data->device_props = entry;
  995. /* Check if a multimode wheel has been connected and
  996. * handle it appropriately */
  997. mmode_ret = lg4ff_handle_multimode_wheel(hid, &real_product_id, bcdDevice);
  998. /* Wheel has been told to switch to native mode. There is no point in going on
  999. * with the initialization as the wheel will do a USB reset when it switches mode
  1000. */
  1001. if (mmode_ret == LG4FF_MMODE_SWITCHED)
  1002. return 0;
  1003. else if (mmode_ret < 0) {
  1004. hid_err(hid, "Unable to switch device mode during initialization, errno %d\n", mmode_ret);
  1005. error = mmode_ret;
  1006. goto err_init;
  1007. }
  1008. /* Check what wheel has been connected */
  1009. for (i = 0; i < ARRAY_SIZE(lg4ff_devices); i++) {
  1010. if (hid->product == lg4ff_devices[i].product_id) {
  1011. dbg_hid("Found compatible device, product ID %04X\n", lg4ff_devices[i].product_id);
  1012. break;
  1013. }
  1014. }
  1015. if (i == ARRAY_SIZE(lg4ff_devices)) {
  1016. hid_err(hid, "This device is flagged to be handled by the lg4ff module but this module does not know how to handle it. "
  1017. "Please report this as a bug to LKML, Simon Wood <simon@mungewell.org> or "
  1018. "Michal Maly <madcatxster@devoid-pointer.net>\n");
  1019. error = -1;
  1020. goto err_init;
  1021. }
  1022. if (mmode_ret == LG4FF_MMODE_IS_MULTIMODE) {
  1023. for (mmode_idx = 0; mmode_idx < ARRAY_SIZE(lg4ff_multimode_wheels); mmode_idx++) {
  1024. if (real_product_id == lg4ff_multimode_wheels[mmode_idx].product_id)
  1025. break;
  1026. }
  1027. if (mmode_idx == ARRAY_SIZE(lg4ff_multimode_wheels)) {
  1028. hid_err(hid, "Device product ID %X is not listed as a multimode wheel", real_product_id);
  1029. error = -1;
  1030. goto err_init;
  1031. }
  1032. }
  1033. /* Set supported force feedback capabilities */
  1034. for (j = 0; lg4ff_devices[i].ff_effects[j] >= 0; j++)
  1035. set_bit(lg4ff_devices[i].ff_effects[j], dev->ffbit);
  1036. error = input_ff_create_memless(dev, NULL, lg4ff_play);
  1037. if (error)
  1038. goto err_init;
  1039. /* Initialize device properties */
  1040. if (mmode_ret == LG4FF_MMODE_IS_MULTIMODE) {
  1041. BUG_ON(mmode_idx == -1);
  1042. mmode_wheel = &lg4ff_multimode_wheels[mmode_idx];
  1043. }
  1044. lg4ff_init_wheel_data(&entry->wdata, &lg4ff_devices[i], mmode_wheel, real_product_id);
  1045. /* Check if autocentering is available and
  1046. * set the centering force to zero by default */
  1047. if (test_bit(FF_AUTOCENTER, dev->ffbit)) {
  1048. /* Formula Force EX expects different autocentering command */
  1049. if ((bcdDevice >> 8) == LG4FF_FFEX_REV_MAJ &&
  1050. (bcdDevice & 0xff) == LG4FF_FFEX_REV_MIN)
  1051. dev->ff->set_autocenter = lg4ff_set_autocenter_ffex;
  1052. else
  1053. dev->ff->set_autocenter = lg4ff_set_autocenter_default;
  1054. dev->ff->set_autocenter(dev, 0);
  1055. }
  1056. /* Create sysfs interface */
  1057. error = device_create_file(&hid->dev, &dev_attr_range);
  1058. if (error)
  1059. hid_warn(hid, "Unable to create sysfs interface for \"range\", errno %d\n", error);
  1060. if (mmode_ret == LG4FF_MMODE_IS_MULTIMODE) {
  1061. error = device_create_file(&hid->dev, &dev_attr_real_id);
  1062. if (error)
  1063. hid_warn(hid, "Unable to create sysfs interface for \"real_id\", errno %d\n", error);
  1064. error = device_create_file(&hid->dev, &dev_attr_alternate_modes);
  1065. if (error)
  1066. hid_warn(hid, "Unable to create sysfs interface for \"alternate_modes\", errno %d\n", error);
  1067. }
  1068. dbg_hid("sysfs interface created\n");
  1069. /* Set the maximum range to start with */
  1070. entry->wdata.range = entry->wdata.max_range;
  1071. if (entry->wdata.set_range)
  1072. entry->wdata.set_range(hid, entry->wdata.range);
  1073. #ifdef CONFIG_LEDS_CLASS
  1074. /* register led subsystem - G27 only */
  1075. entry->wdata.led_state = 0;
  1076. for (j = 0; j < 5; j++)
  1077. entry->wdata.led[j] = NULL;
  1078. if (lg4ff_devices[i].product_id == USB_DEVICE_ID_LOGITECH_G27_WHEEL) {
  1079. struct led_classdev *led;
  1080. size_t name_sz;
  1081. char *name;
  1082. lg4ff_set_leds(hid, 0);
  1083. name_sz = strlen(dev_name(&hid->dev)) + 8;
  1084. for (j = 0; j < 5; j++) {
  1085. led = kzalloc(sizeof(struct led_classdev)+name_sz, GFP_KERNEL);
  1086. if (!led) {
  1087. hid_err(hid, "can't allocate memory for LED %d\n", j);
  1088. goto err_leds;
  1089. }
  1090. name = (void *)(&led[1]);
  1091. snprintf(name, name_sz, "%s::RPM%d", dev_name(&hid->dev), j+1);
  1092. led->name = name;
  1093. led->brightness = 0;
  1094. led->max_brightness = 1;
  1095. led->brightness_get = lg4ff_led_get_brightness;
  1096. led->brightness_set = lg4ff_led_set_brightness;
  1097. entry->wdata.led[j] = led;
  1098. error = led_classdev_register(&hid->dev, led);
  1099. if (error) {
  1100. hid_err(hid, "failed to register LED %d. Aborting.\n", j);
  1101. err_leds:
  1102. /* Deregister LEDs (if any) */
  1103. for (j = 0; j < 5; j++) {
  1104. led = entry->wdata.led[j];
  1105. entry->wdata.led[j] = NULL;
  1106. if (!led)
  1107. continue;
  1108. led_classdev_unregister(led);
  1109. kfree(led);
  1110. }
  1111. goto out; /* Let the driver continue without LEDs */
  1112. }
  1113. }
  1114. }
  1115. out:
  1116. #endif
  1117. hid_info(hid, "Force feedback support for Logitech Gaming Wheels\n");
  1118. return 0;
  1119. err_init:
  1120. drv_data->device_props = NULL;
  1121. kfree(entry);
  1122. return error;
  1123. }
  1124. int lg4ff_deinit(struct hid_device *hid)
  1125. {
  1126. struct lg4ff_device_entry *entry;
  1127. struct lg_drv_data *drv_data;
  1128. drv_data = hid_get_drvdata(hid);
  1129. if (!drv_data) {
  1130. hid_err(hid, "Error while deinitializing device, no private driver data.\n");
  1131. return -1;
  1132. }
  1133. entry = drv_data->device_props;
  1134. if (!entry)
  1135. goto out; /* Nothing more to do */
  1136. /* Multimode devices will have at least the "MODE_NATIVE" bit set */
  1137. if (entry->wdata.alternate_modes) {
  1138. device_remove_file(&hid->dev, &dev_attr_real_id);
  1139. device_remove_file(&hid->dev, &dev_attr_alternate_modes);
  1140. }
  1141. device_remove_file(&hid->dev, &dev_attr_range);
  1142. #ifdef CONFIG_LEDS_CLASS
  1143. {
  1144. int j;
  1145. struct led_classdev *led;
  1146. /* Deregister LEDs (if any) */
  1147. for (j = 0; j < 5; j++) {
  1148. led = entry->wdata.led[j];
  1149. entry->wdata.led[j] = NULL;
  1150. if (!led)
  1151. continue;
  1152. led_classdev_unregister(led);
  1153. kfree(led);
  1154. }
  1155. }
  1156. #endif
  1157. hid_hw_stop(hid);
  1158. drv_data->device_props = NULL;
  1159. kfree(entry);
  1160. out:
  1161. dbg_hid("Device successfully unregistered\n");
  1162. return 0;
  1163. }