hid-lg4ff.c 41 KB

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