hid-lg4ff.c 42 KB

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