hid-lg4ff.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493
  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 <simon@mungewell.org>
  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 (lbuf[i-1] == '\n') {
  763. if (i == 1) {
  764. kfree(lbuf);
  765. return -EINVAL;
  766. }
  767. lbuf[i-1] = '\0';
  768. }
  769. for (i = 0; i < LG4FF_MODE_MAX_IDX; i++) {
  770. const u16 mode_product_id = lg4ff_alternate_modes[i].product_id;
  771. const char *tag = lg4ff_alternate_modes[i].tag;
  772. if (entry->wdata.alternate_modes & BIT(i)) {
  773. if (!strcmp(tag, lbuf)) {
  774. if (!mode_product_id)
  775. target_product_id = entry->wdata.real_product_id;
  776. else
  777. target_product_id = mode_product_id;
  778. break;
  779. }
  780. }
  781. }
  782. if (i == LG4FF_MODE_MAX_IDX) {
  783. hid_info(hid, "Requested mode \"%s\" is not supported by the device\n", lbuf);
  784. kfree(lbuf);
  785. return -EINVAL;
  786. }
  787. kfree(lbuf); /* Not needed anymore */
  788. if (target_product_id == entry->wdata.product_id) /* Nothing to do */
  789. return count;
  790. /* Automatic switching has to be disabled for the switch to DF-EX mode to work correctly */
  791. if (target_product_id == USB_DEVICE_ID_LOGITECH_WHEEL && !lg4ff_no_autoswitch) {
  792. 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",
  793. entry->wdata.real_name);
  794. return -EINVAL;
  795. }
  796. /* Take care of hardware limitations */
  797. if ((entry->wdata.real_product_id == USB_DEVICE_ID_LOGITECH_DFP_WHEEL || entry->wdata.real_product_id == USB_DEVICE_ID_LOGITECH_G25_WHEEL) &&
  798. entry->wdata.product_id > target_product_id) {
  799. hid_info(hid, "\"%s\" cannot be switched back into \"%s\" mode\n", entry->wdata.real_name, lg4ff_alternate_modes[i].name);
  800. return -EINVAL;
  801. }
  802. s = lg4ff_get_mode_switch_command(entry->wdata.real_product_id, target_product_id);
  803. if (!s) {
  804. hid_err(hid, "Invalid target product ID %X\n", target_product_id);
  805. return -EINVAL;
  806. }
  807. ret = lg4ff_switch_compatibility_mode(hid, s);
  808. return (ret == 0 ? count : ret);
  809. }
  810. static DEVICE_ATTR(alternate_modes, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, lg4ff_alternate_modes_show, lg4ff_alternate_modes_store);
  811. static ssize_t lg4ff_combine_show(struct device *dev, struct device_attribute *attr,
  812. char *buf)
  813. {
  814. struct hid_device *hid = to_hid_device(dev);
  815. struct lg4ff_device_entry *entry;
  816. struct lg_drv_data *drv_data;
  817. size_t count;
  818. drv_data = hid_get_drvdata(hid);
  819. if (!drv_data) {
  820. hid_err(hid, "Private driver data not found!\n");
  821. return 0;
  822. }
  823. entry = drv_data->device_props;
  824. if (!entry) {
  825. hid_err(hid, "Device properties not found!\n");
  826. return 0;
  827. }
  828. count = scnprintf(buf, PAGE_SIZE, "%u\n", entry->wdata.combine);
  829. return count;
  830. }
  831. static ssize_t lg4ff_combine_store(struct device *dev, struct device_attribute *attr,
  832. const char *buf, size_t count)
  833. {
  834. struct hid_device *hid = to_hid_device(dev);
  835. struct lg4ff_device_entry *entry;
  836. struct lg_drv_data *drv_data;
  837. u16 combine = simple_strtoul(buf, NULL, 10);
  838. drv_data = hid_get_drvdata(hid);
  839. if (!drv_data) {
  840. hid_err(hid, "Private driver data not found!\n");
  841. return -EINVAL;
  842. }
  843. entry = drv_data->device_props;
  844. if (!entry) {
  845. hid_err(hid, "Device properties not found!\n");
  846. return -EINVAL;
  847. }
  848. if (combine > 1)
  849. combine = 1;
  850. entry->wdata.combine = combine;
  851. return count;
  852. }
  853. static DEVICE_ATTR(combine_pedals, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, lg4ff_combine_show, lg4ff_combine_store);
  854. /* Export the currently set range of the wheel */
  855. static ssize_t lg4ff_range_show(struct device *dev, struct device_attribute *attr,
  856. char *buf)
  857. {
  858. struct hid_device *hid = to_hid_device(dev);
  859. struct lg4ff_device_entry *entry;
  860. struct lg_drv_data *drv_data;
  861. size_t count;
  862. drv_data = hid_get_drvdata(hid);
  863. if (!drv_data) {
  864. hid_err(hid, "Private driver data not found!\n");
  865. return 0;
  866. }
  867. entry = drv_data->device_props;
  868. if (!entry) {
  869. hid_err(hid, "Device properties not found!\n");
  870. return 0;
  871. }
  872. count = scnprintf(buf, PAGE_SIZE, "%u\n", entry->wdata.range);
  873. return count;
  874. }
  875. /* Set range to user specified value, call appropriate function
  876. * according to the type of the wheel */
  877. static ssize_t lg4ff_range_store(struct device *dev, struct device_attribute *attr,
  878. const char *buf, size_t count)
  879. {
  880. struct hid_device *hid = to_hid_device(dev);
  881. struct lg4ff_device_entry *entry;
  882. struct lg_drv_data *drv_data;
  883. u16 range = simple_strtoul(buf, NULL, 10);
  884. drv_data = hid_get_drvdata(hid);
  885. if (!drv_data) {
  886. hid_err(hid, "Private driver data not found!\n");
  887. return -EINVAL;
  888. }
  889. entry = drv_data->device_props;
  890. if (!entry) {
  891. hid_err(hid, "Device properties not found!\n");
  892. return -EINVAL;
  893. }
  894. if (range == 0)
  895. range = entry->wdata.max_range;
  896. /* Check if the wheel supports range setting
  897. * and that the range is within limits for the wheel */
  898. if (entry->wdata.set_range && range >= entry->wdata.min_range && range <= entry->wdata.max_range) {
  899. entry->wdata.set_range(hid, range);
  900. entry->wdata.range = range;
  901. }
  902. return count;
  903. }
  904. static DEVICE_ATTR(range, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, lg4ff_range_show, lg4ff_range_store);
  905. static ssize_t lg4ff_real_id_show(struct device *dev, struct device_attribute *attr, char *buf)
  906. {
  907. struct hid_device *hid = to_hid_device(dev);
  908. struct lg4ff_device_entry *entry;
  909. struct lg_drv_data *drv_data;
  910. size_t count;
  911. drv_data = hid_get_drvdata(hid);
  912. if (!drv_data) {
  913. hid_err(hid, "Private driver data not found!\n");
  914. return 0;
  915. }
  916. entry = drv_data->device_props;
  917. if (!entry) {
  918. hid_err(hid, "Device properties not found!\n");
  919. return 0;
  920. }
  921. if (!entry->wdata.real_tag || !entry->wdata.real_name) {
  922. hid_err(hid, "NULL pointer to string\n");
  923. return 0;
  924. }
  925. count = scnprintf(buf, PAGE_SIZE, "%s: %s\n", entry->wdata.real_tag, entry->wdata.real_name);
  926. return count;
  927. }
  928. static ssize_t lg4ff_real_id_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  929. {
  930. /* Real ID is a read-only value */
  931. return -EPERM;
  932. }
  933. static DEVICE_ATTR(real_id, S_IRUGO, lg4ff_real_id_show, lg4ff_real_id_store);
  934. #ifdef CONFIG_LEDS_CLASS
  935. static void lg4ff_set_leds(struct hid_device *hid, u8 leds)
  936. {
  937. struct lg_drv_data *drv_data;
  938. struct lg4ff_device_entry *entry;
  939. unsigned long flags;
  940. s32 *value;
  941. drv_data = hid_get_drvdata(hid);
  942. if (!drv_data) {
  943. hid_err(hid, "Private driver data not found!\n");
  944. return;
  945. }
  946. entry = drv_data->device_props;
  947. if (!entry) {
  948. hid_err(hid, "Device properties not found!\n");
  949. return;
  950. }
  951. value = entry->report->field[0]->value;
  952. spin_lock_irqsave(&entry->report_lock, flags);
  953. value[0] = 0xf8;
  954. value[1] = 0x12;
  955. value[2] = leds;
  956. value[3] = 0x00;
  957. value[4] = 0x00;
  958. value[5] = 0x00;
  959. value[6] = 0x00;
  960. hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
  961. spin_unlock_irqrestore(&entry->report_lock, flags);
  962. }
  963. static void lg4ff_led_set_brightness(struct led_classdev *led_cdev,
  964. enum led_brightness value)
  965. {
  966. struct device *dev = led_cdev->dev->parent;
  967. struct hid_device *hid = to_hid_device(dev);
  968. struct lg_drv_data *drv_data = hid_get_drvdata(hid);
  969. struct lg4ff_device_entry *entry;
  970. int i, state = 0;
  971. if (!drv_data) {
  972. hid_err(hid, "Device data not found.");
  973. return;
  974. }
  975. entry = drv_data->device_props;
  976. if (!entry) {
  977. hid_err(hid, "Device properties not found.");
  978. return;
  979. }
  980. for (i = 0; i < 5; i++) {
  981. if (led_cdev != entry->wdata.led[i])
  982. continue;
  983. state = (entry->wdata.led_state >> i) & 1;
  984. if (value == LED_OFF && state) {
  985. entry->wdata.led_state &= ~(1 << i);
  986. lg4ff_set_leds(hid, entry->wdata.led_state);
  987. } else if (value != LED_OFF && !state) {
  988. entry->wdata.led_state |= 1 << i;
  989. lg4ff_set_leds(hid, entry->wdata.led_state);
  990. }
  991. break;
  992. }
  993. }
  994. static enum led_brightness lg4ff_led_get_brightness(struct led_classdev *led_cdev)
  995. {
  996. struct device *dev = led_cdev->dev->parent;
  997. struct hid_device *hid = to_hid_device(dev);
  998. struct lg_drv_data *drv_data = hid_get_drvdata(hid);
  999. struct lg4ff_device_entry *entry;
  1000. int i, value = 0;
  1001. if (!drv_data) {
  1002. hid_err(hid, "Device data not found.");
  1003. return LED_OFF;
  1004. }
  1005. entry = drv_data->device_props;
  1006. if (!entry) {
  1007. hid_err(hid, "Device properties not found.");
  1008. return LED_OFF;
  1009. }
  1010. for (i = 0; i < 5; i++)
  1011. if (led_cdev == entry->wdata.led[i]) {
  1012. value = (entry->wdata.led_state >> i) & 1;
  1013. break;
  1014. }
  1015. return value ? LED_FULL : LED_OFF;
  1016. }
  1017. #endif
  1018. static u16 lg4ff_identify_multimode_wheel(struct hid_device *hid, const u16 reported_product_id, const u16 bcdDevice)
  1019. {
  1020. u32 current_mode;
  1021. int i;
  1022. /* identify current mode from USB PID */
  1023. for (i = 1; i < ARRAY_SIZE(lg4ff_alternate_modes); i++) {
  1024. dbg_hid("Testing whether PID is %X\n", lg4ff_alternate_modes[i].product_id);
  1025. if (reported_product_id == lg4ff_alternate_modes[i].product_id)
  1026. break;
  1027. }
  1028. if (i == ARRAY_SIZE(lg4ff_alternate_modes))
  1029. return 0;
  1030. current_mode = BIT(i);
  1031. for (i = 0; i < ARRAY_SIZE(lg4ff_main_checklist); i++) {
  1032. const u16 mask = lg4ff_main_checklist[i]->mask;
  1033. const u16 result = lg4ff_main_checklist[i]->result;
  1034. const u16 real_product_id = lg4ff_main_checklist[i]->real_product_id;
  1035. if ((current_mode & lg4ff_main_checklist[i]->modes) && \
  1036. (bcdDevice & mask) == result) {
  1037. dbg_hid("Found wheel with real PID %X whose reported PID is %X\n", real_product_id, reported_product_id);
  1038. return real_product_id;
  1039. }
  1040. }
  1041. /* No match found. This is either Driving Force or an unknown
  1042. * wheel model, do not touch it */
  1043. dbg_hid("Wheel with bcdDevice %X was not recognized as multimode wheel, leaving in its current mode\n", bcdDevice);
  1044. return 0;
  1045. }
  1046. static int lg4ff_handle_multimode_wheel(struct hid_device *hid, u16 *real_product_id, const u16 bcdDevice)
  1047. {
  1048. const u16 reported_product_id = hid->product;
  1049. int ret;
  1050. *real_product_id = lg4ff_identify_multimode_wheel(hid, reported_product_id, bcdDevice);
  1051. /* Probed wheel is not a multimode wheel */
  1052. if (!*real_product_id) {
  1053. *real_product_id = reported_product_id;
  1054. dbg_hid("Wheel is not a multimode wheel\n");
  1055. return LG4FF_MMODE_NOT_MULTIMODE;
  1056. }
  1057. /* Switch from "Driving Force" mode to native mode automatically.
  1058. * Otherwise keep the wheel in its current mode */
  1059. if (reported_product_id == USB_DEVICE_ID_LOGITECH_WHEEL &&
  1060. reported_product_id != *real_product_id &&
  1061. !lg4ff_no_autoswitch) {
  1062. const struct lg4ff_compat_mode_switch *s = lg4ff_get_mode_switch_command(*real_product_id, *real_product_id);
  1063. if (!s) {
  1064. hid_err(hid, "Invalid product id %X\n", *real_product_id);
  1065. return LG4FF_MMODE_NOT_MULTIMODE;
  1066. }
  1067. ret = lg4ff_switch_compatibility_mode(hid, s);
  1068. if (ret) {
  1069. /* Wheel could not have been switched to native mode,
  1070. * leave it in "Driving Force" mode and continue */
  1071. hid_err(hid, "Unable to switch wheel mode, errno %d\n", ret);
  1072. return LG4FF_MMODE_IS_MULTIMODE;
  1073. }
  1074. return LG4FF_MMODE_SWITCHED;
  1075. }
  1076. return LG4FF_MMODE_IS_MULTIMODE;
  1077. }
  1078. int lg4ff_init(struct hid_device *hid)
  1079. {
  1080. struct hid_input *hidinput;
  1081. struct input_dev *dev;
  1082. struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
  1083. struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
  1084. const struct usb_device_descriptor *udesc = &(hid_to_usb_dev(hid)->descriptor);
  1085. const u16 bcdDevice = le16_to_cpu(udesc->bcdDevice);
  1086. const struct lg4ff_multimode_wheel *mmode_wheel = NULL;
  1087. struct lg4ff_device_entry *entry;
  1088. struct lg_drv_data *drv_data;
  1089. int error, i, j;
  1090. int mmode_ret, mmode_idx = -1;
  1091. u16 real_product_id;
  1092. if (list_empty(&hid->inputs)) {
  1093. hid_err(hid, "no inputs found\n");
  1094. return -ENODEV;
  1095. }
  1096. hidinput = list_entry(hid->inputs.next, struct hid_input, list);
  1097. dev = hidinput->input;
  1098. /* Check that the report looks ok */
  1099. if (!hid_validate_values(hid, HID_OUTPUT_REPORT, 0, 0, 7))
  1100. return -1;
  1101. drv_data = hid_get_drvdata(hid);
  1102. if (!drv_data) {
  1103. hid_err(hid, "Cannot add device, private driver data not allocated\n");
  1104. return -1;
  1105. }
  1106. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  1107. if (!entry)
  1108. return -ENOMEM;
  1109. spin_lock_init(&entry->report_lock);
  1110. entry->report = report;
  1111. drv_data->device_props = entry;
  1112. /* Check if a multimode wheel has been connected and
  1113. * handle it appropriately */
  1114. mmode_ret = lg4ff_handle_multimode_wheel(hid, &real_product_id, bcdDevice);
  1115. /* Wheel has been told to switch to native mode. There is no point in going on
  1116. * with the initialization as the wheel will do a USB reset when it switches mode
  1117. */
  1118. if (mmode_ret == LG4FF_MMODE_SWITCHED)
  1119. return 0;
  1120. else if (mmode_ret < 0) {
  1121. hid_err(hid, "Unable to switch device mode during initialization, errno %d\n", mmode_ret);
  1122. error = mmode_ret;
  1123. goto err_init;
  1124. }
  1125. /* Check what wheel has been connected */
  1126. for (i = 0; i < ARRAY_SIZE(lg4ff_devices); i++) {
  1127. if (hid->product == lg4ff_devices[i].product_id) {
  1128. dbg_hid("Found compatible device, product ID %04X\n", lg4ff_devices[i].product_id);
  1129. break;
  1130. }
  1131. }
  1132. if (i == ARRAY_SIZE(lg4ff_devices)) {
  1133. hid_err(hid, "This device is flagged to be handled by the lg4ff module but this module does not know how to handle it. "
  1134. "Please report this as a bug to LKML, Simon Wood <simon@mungewell.org> or "
  1135. "Michal Maly <madcatxster@devoid-pointer.net>\n");
  1136. error = -1;
  1137. goto err_init;
  1138. }
  1139. if (mmode_ret == LG4FF_MMODE_IS_MULTIMODE) {
  1140. for (mmode_idx = 0; mmode_idx < ARRAY_SIZE(lg4ff_multimode_wheels); mmode_idx++) {
  1141. if (real_product_id == lg4ff_multimode_wheels[mmode_idx].product_id)
  1142. break;
  1143. }
  1144. if (mmode_idx == ARRAY_SIZE(lg4ff_multimode_wheels)) {
  1145. hid_err(hid, "Device product ID %X is not listed as a multimode wheel", real_product_id);
  1146. error = -1;
  1147. goto err_init;
  1148. }
  1149. }
  1150. /* Set supported force feedback capabilities */
  1151. for (j = 0; lg4ff_devices[i].ff_effects[j] >= 0; j++)
  1152. set_bit(lg4ff_devices[i].ff_effects[j], dev->ffbit);
  1153. error = input_ff_create_memless(dev, NULL, lg4ff_play);
  1154. if (error)
  1155. goto err_init;
  1156. /* Initialize device properties */
  1157. if (mmode_ret == LG4FF_MMODE_IS_MULTIMODE) {
  1158. BUG_ON(mmode_idx == -1);
  1159. mmode_wheel = &lg4ff_multimode_wheels[mmode_idx];
  1160. }
  1161. lg4ff_init_wheel_data(&entry->wdata, &lg4ff_devices[i], mmode_wheel, real_product_id);
  1162. /* Check if autocentering is available and
  1163. * set the centering force to zero by default */
  1164. if (test_bit(FF_AUTOCENTER, dev->ffbit)) {
  1165. /* Formula Force EX expects different autocentering command */
  1166. if ((bcdDevice >> 8) == LG4FF_FFEX_REV_MAJ &&
  1167. (bcdDevice & 0xff) == LG4FF_FFEX_REV_MIN)
  1168. dev->ff->set_autocenter = lg4ff_set_autocenter_ffex;
  1169. else
  1170. dev->ff->set_autocenter = lg4ff_set_autocenter_default;
  1171. dev->ff->set_autocenter(dev, 0);
  1172. }
  1173. /* Create sysfs interface */
  1174. error = device_create_file(&hid->dev, &dev_attr_combine_pedals);
  1175. if (error)
  1176. hid_warn(hid, "Unable to create sysfs interface for \"combine\", errno %d\n", error);
  1177. error = device_create_file(&hid->dev, &dev_attr_range);
  1178. if (error)
  1179. hid_warn(hid, "Unable to create sysfs interface for \"range\", errno %d\n", error);
  1180. if (mmode_ret == LG4FF_MMODE_IS_MULTIMODE) {
  1181. error = device_create_file(&hid->dev, &dev_attr_real_id);
  1182. if (error)
  1183. hid_warn(hid, "Unable to create sysfs interface for \"real_id\", errno %d\n", error);
  1184. error = device_create_file(&hid->dev, &dev_attr_alternate_modes);
  1185. if (error)
  1186. hid_warn(hid, "Unable to create sysfs interface for \"alternate_modes\", errno %d\n", error);
  1187. }
  1188. dbg_hid("sysfs interface created\n");
  1189. /* Set the maximum range to start with */
  1190. entry->wdata.range = entry->wdata.max_range;
  1191. if (entry->wdata.set_range)
  1192. entry->wdata.set_range(hid, entry->wdata.range);
  1193. #ifdef CONFIG_LEDS_CLASS
  1194. /* register led subsystem - G27/G29 only */
  1195. entry->wdata.led_state = 0;
  1196. for (j = 0; j < 5; j++)
  1197. entry->wdata.led[j] = NULL;
  1198. if (lg4ff_devices[i].product_id == USB_DEVICE_ID_LOGITECH_G27_WHEEL ||
  1199. lg4ff_devices[i].product_id == USB_DEVICE_ID_LOGITECH_G29_WHEEL) {
  1200. struct led_classdev *led;
  1201. size_t name_sz;
  1202. char *name;
  1203. lg4ff_set_leds(hid, 0);
  1204. name_sz = strlen(dev_name(&hid->dev)) + 8;
  1205. for (j = 0; j < 5; j++) {
  1206. led = kzalloc(sizeof(struct led_classdev)+name_sz, GFP_KERNEL);
  1207. if (!led) {
  1208. hid_err(hid, "can't allocate memory for LED %d\n", j);
  1209. goto err_leds;
  1210. }
  1211. name = (void *)(&led[1]);
  1212. snprintf(name, name_sz, "%s::RPM%d", dev_name(&hid->dev), j+1);
  1213. led->name = name;
  1214. led->brightness = 0;
  1215. led->max_brightness = 1;
  1216. led->brightness_get = lg4ff_led_get_brightness;
  1217. led->brightness_set = lg4ff_led_set_brightness;
  1218. entry->wdata.led[j] = led;
  1219. error = led_classdev_register(&hid->dev, led);
  1220. if (error) {
  1221. hid_err(hid, "failed to register LED %d. Aborting.\n", j);
  1222. err_leds:
  1223. /* Deregister LEDs (if any) */
  1224. for (j = 0; j < 5; j++) {
  1225. led = entry->wdata.led[j];
  1226. entry->wdata.led[j] = NULL;
  1227. if (!led)
  1228. continue;
  1229. led_classdev_unregister(led);
  1230. kfree(led);
  1231. }
  1232. goto out; /* Let the driver continue without LEDs */
  1233. }
  1234. }
  1235. }
  1236. out:
  1237. #endif
  1238. hid_info(hid, "Force feedback support for Logitech Gaming Wheels\n");
  1239. return 0;
  1240. err_init:
  1241. drv_data->device_props = NULL;
  1242. kfree(entry);
  1243. return error;
  1244. }
  1245. int lg4ff_deinit(struct hid_device *hid)
  1246. {
  1247. struct lg4ff_device_entry *entry;
  1248. struct lg_drv_data *drv_data;
  1249. drv_data = hid_get_drvdata(hid);
  1250. if (!drv_data) {
  1251. hid_err(hid, "Error while deinitializing device, no private driver data.\n");
  1252. return -1;
  1253. }
  1254. entry = drv_data->device_props;
  1255. if (!entry)
  1256. goto out; /* Nothing more to do */
  1257. /* Multimode devices will have at least the "MODE_NATIVE" bit set */
  1258. if (entry->wdata.alternate_modes) {
  1259. device_remove_file(&hid->dev, &dev_attr_real_id);
  1260. device_remove_file(&hid->dev, &dev_attr_alternate_modes);
  1261. }
  1262. device_remove_file(&hid->dev, &dev_attr_combine_pedals);
  1263. device_remove_file(&hid->dev, &dev_attr_range);
  1264. #ifdef CONFIG_LEDS_CLASS
  1265. {
  1266. int j;
  1267. struct led_classdev *led;
  1268. /* Deregister LEDs (if any) */
  1269. for (j = 0; j < 5; j++) {
  1270. led = entry->wdata.led[j];
  1271. entry->wdata.led[j] = NULL;
  1272. if (!led)
  1273. continue;
  1274. led_classdev_unregister(led);
  1275. kfree(led);
  1276. }
  1277. }
  1278. #endif
  1279. drv_data->device_props = NULL;
  1280. kfree(entry);
  1281. out:
  1282. dbg_hid("Device successfully unregistered\n");
  1283. return 0;
  1284. }