hid-lg4ff.c 42 KB

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