hid-lg4ff.c 39 KB

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