hid-pidff.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Force feedback driver for USB HID PID compliant devices
  4. *
  5. * Copyright (c) 2005, 2006 Anssi Hannula <anssi.hannula@gmail.com>
  6. * Upgraded 2025 by Oleg Makarenko and Tomasz Pakuła
  7. */
  8. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  9. #include "hid-pidff.h"
  10. #include <linux/input.h>
  11. #include <linux/slab.h>
  12. #include <linux/usb.h>
  13. #include <linux/hid.h>
  14. #include <linux/minmax.h>
  15. #define PID_EFFECTS_MAX 64
  16. #define PID_INFINITE U16_MAX
  17. /* Linux Force Feedback API uses miliseconds as time unit */
  18. #define FF_TIME_EXPONENT -3
  19. #define FF_INFINITE 0
  20. /* Report usage table used to put reports into an array */
  21. #define PID_SET_EFFECT 0
  22. #define PID_EFFECT_OPERATION 1
  23. #define PID_DEVICE_GAIN 2
  24. #define PID_POOL 3
  25. #define PID_BLOCK_LOAD 4
  26. #define PID_BLOCK_FREE 5
  27. #define PID_DEVICE_CONTROL 6
  28. #define PID_CREATE_NEW_EFFECT 7
  29. #define PID_REQUIRED_REPORTS 7
  30. #define PID_SET_ENVELOPE 8
  31. #define PID_SET_CONDITION 9
  32. #define PID_SET_PERIODIC 10
  33. #define PID_SET_CONSTANT 11
  34. #define PID_SET_RAMP 12
  35. static const u8 pidff_reports[] = {
  36. 0x21, 0x77, 0x7d, 0x7f, 0x89, 0x90, 0x96, 0xab,
  37. 0x5a, 0x5f, 0x6e, 0x73, 0x74
  38. };
  39. /*
  40. * device_control is really 0x95, but 0x96 specified
  41. * as it is the usage of the only field in that report.
  42. */
  43. /* PID special fields */
  44. #define PID_EFFECT_TYPE 0x25
  45. #define PID_DIRECTION 0x57
  46. #define PID_EFFECT_OPERATION_ARRAY 0x78
  47. #define PID_BLOCK_LOAD_STATUS 0x8b
  48. #define PID_DEVICE_CONTROL_ARRAY 0x96
  49. /* Value usage tables used to put fields and values into arrays */
  50. #define PID_EFFECT_BLOCK_INDEX 0
  51. #define PID_DURATION 1
  52. #define PID_GAIN 2
  53. #define PID_TRIGGER_BUTTON 3
  54. #define PID_TRIGGER_REPEAT_INT 4
  55. #define PID_DIRECTION_ENABLE 5
  56. #define PID_START_DELAY 6
  57. static const u8 pidff_set_effect[] = {
  58. 0x22, 0x50, 0x52, 0x53, 0x54, 0x56, 0xa7
  59. };
  60. #define PID_ATTACK_LEVEL 1
  61. #define PID_ATTACK_TIME 2
  62. #define PID_FADE_LEVEL 3
  63. #define PID_FADE_TIME 4
  64. static const u8 pidff_set_envelope[] = { 0x22, 0x5b, 0x5c, 0x5d, 0x5e };
  65. #define PID_PARAM_BLOCK_OFFSET 1
  66. #define PID_CP_OFFSET 2
  67. #define PID_POS_COEFFICIENT 3
  68. #define PID_NEG_COEFFICIENT 4
  69. #define PID_POS_SATURATION 5
  70. #define PID_NEG_SATURATION 6
  71. #define PID_DEAD_BAND 7
  72. static const u8 pidff_set_condition[] = {
  73. 0x22, 0x23, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65
  74. };
  75. #define PID_MAGNITUDE 1
  76. #define PID_OFFSET 2
  77. #define PID_PHASE 3
  78. #define PID_PERIOD 4
  79. static const u8 pidff_set_periodic[] = { 0x22, 0x70, 0x6f, 0x71, 0x72 };
  80. static const u8 pidff_set_constant[] = { 0x22, 0x70 };
  81. #define PID_RAMP_START 1
  82. #define PID_RAMP_END 2
  83. static const u8 pidff_set_ramp[] = { 0x22, 0x75, 0x76 };
  84. #define PID_RAM_POOL_AVAILABLE 1
  85. static const u8 pidff_block_load[] = { 0x22, 0xac };
  86. #define PID_LOOP_COUNT 1
  87. static const u8 pidff_effect_operation[] = { 0x22, 0x7c };
  88. static const u8 pidff_block_free[] = { 0x22 };
  89. #define PID_DEVICE_GAIN_FIELD 0
  90. static const u8 pidff_device_gain[] = { 0x7e };
  91. #define PID_RAM_POOL_SIZE 0
  92. #define PID_SIMULTANEOUS_MAX 1
  93. #define PID_DEVICE_MANAGED_POOL 2
  94. static const u8 pidff_pool[] = { 0x80, 0x83, 0xa9 };
  95. /* Special field key tables used to put special field keys into arrays */
  96. #define PID_ENABLE_ACTUATORS 0
  97. #define PID_DISABLE_ACTUATORS 1
  98. #define PID_STOP_ALL_EFFECTS 2
  99. #define PID_RESET 3
  100. #define PID_PAUSE 4
  101. #define PID_CONTINUE 5
  102. static const u8 pidff_device_control[] = { 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c };
  103. #define PID_CONSTANT 0
  104. #define PID_RAMP 1
  105. #define PID_SQUARE 2
  106. #define PID_SINE 3
  107. #define PID_TRIANGLE 4
  108. #define PID_SAW_UP 5
  109. #define PID_SAW_DOWN 6
  110. #define PID_SPRING 7
  111. #define PID_DAMPER 8
  112. #define PID_INERTIA 9
  113. #define PID_FRICTION 10
  114. static const u8 pidff_effect_types[] = {
  115. 0x26, 0x27, 0x30, 0x31, 0x32, 0x33, 0x34,
  116. 0x40, 0x41, 0x42, 0x43
  117. };
  118. #define PID_BLOCK_LOAD_SUCCESS 0
  119. #define PID_BLOCK_LOAD_FULL 1
  120. #define PID_BLOCK_LOAD_ERROR 2
  121. static const u8 pidff_block_load_status[] = { 0x8c, 0x8d, 0x8e};
  122. #define PID_EFFECT_START 0
  123. #define PID_EFFECT_STOP 1
  124. static const u8 pidff_effect_operation_status[] = { 0x79, 0x7b };
  125. /* Polar direction 90 degrees (East) */
  126. #define PIDFF_FIXED_WHEEL_DIRECTION 0x4000
  127. struct pidff_usage {
  128. struct hid_field *field;
  129. s32 *value;
  130. };
  131. struct pidff_device {
  132. struct hid_device *hid;
  133. struct hid_report *reports[sizeof(pidff_reports)];
  134. struct pidff_usage set_effect[sizeof(pidff_set_effect)];
  135. struct pidff_usage set_envelope[sizeof(pidff_set_envelope)];
  136. struct pidff_usage set_condition[sizeof(pidff_set_condition)];
  137. struct pidff_usage set_periodic[sizeof(pidff_set_periodic)];
  138. struct pidff_usage set_constant[sizeof(pidff_set_constant)];
  139. struct pidff_usage set_ramp[sizeof(pidff_set_ramp)];
  140. struct pidff_usage device_gain[sizeof(pidff_device_gain)];
  141. struct pidff_usage block_load[sizeof(pidff_block_load)];
  142. struct pidff_usage pool[sizeof(pidff_pool)];
  143. struct pidff_usage effect_operation[sizeof(pidff_effect_operation)];
  144. struct pidff_usage block_free[sizeof(pidff_block_free)];
  145. /*
  146. * Special field is a field that is not composed of
  147. * usage<->value pairs that pidff_usage values are
  148. */
  149. /* Special field in create_new_effect */
  150. struct hid_field *create_new_effect_type;
  151. /* Special fields in set_effect */
  152. struct hid_field *set_effect_type;
  153. struct hid_field *effect_direction;
  154. /* Special field in device_control */
  155. struct hid_field *device_control;
  156. /* Special field in block_load */
  157. struct hid_field *block_load_status;
  158. /* Special field in effect_operation */
  159. struct hid_field *effect_operation_status;
  160. int control_id[sizeof(pidff_device_control)];
  161. int type_id[sizeof(pidff_effect_types)];
  162. int status_id[sizeof(pidff_block_load_status)];
  163. int operation_id[sizeof(pidff_effect_operation_status)];
  164. int pid_id[PID_EFFECTS_MAX];
  165. u32 quirks;
  166. u8 effect_count;
  167. };
  168. /*
  169. * Clamp value for a given field
  170. */
  171. static s32 pidff_clamp(s32 i, struct hid_field *field)
  172. {
  173. s32 clamped = clamp(i, field->logical_minimum, field->logical_maximum);
  174. pr_debug("clamped from %d to %d", i, clamped);
  175. return clamped;
  176. }
  177. /*
  178. * Scale an unsigned value with range 0..max for the given field
  179. */
  180. static int pidff_rescale(int i, int max, struct hid_field *field)
  181. {
  182. return i * (field->logical_maximum - field->logical_minimum) / max +
  183. field->logical_minimum;
  184. }
  185. /*
  186. * Scale a signed value in range S16_MIN..S16_MAX for the given field
  187. */
  188. static int pidff_rescale_signed(int i, struct hid_field *field)
  189. {
  190. if (i > 0) return i * field->logical_maximum / S16_MAX;
  191. if (i < 0) return i * field->logical_minimum / S16_MIN;
  192. return 0;
  193. }
  194. /*
  195. * Scale time value from Linux default (ms) to field units
  196. */
  197. static u32 pidff_rescale_time(u16 time, struct hid_field *field)
  198. {
  199. u32 scaled_time = time;
  200. int exponent = field->unit_exponent;
  201. pr_debug("time field exponent: %d\n", exponent);
  202. for (;exponent < FF_TIME_EXPONENT; exponent++)
  203. scaled_time *= 10;
  204. for (;exponent > FF_TIME_EXPONENT; exponent--)
  205. scaled_time /= 10;
  206. pr_debug("time calculated from %d to %d\n", time, scaled_time);
  207. return scaled_time;
  208. }
  209. static void pidff_set(struct pidff_usage *usage, u16 value)
  210. {
  211. usage->value[0] = pidff_rescale(value, U16_MAX, usage->field);
  212. pr_debug("calculated from %d to %d\n", value, usage->value[0]);
  213. }
  214. static void pidff_set_signed(struct pidff_usage *usage, s16 value)
  215. {
  216. if (usage->field->logical_minimum < 0)
  217. usage->value[0] = pidff_rescale_signed(value, usage->field);
  218. else {
  219. if (value < 0)
  220. usage->value[0] =
  221. pidff_rescale(-value, -S16_MIN, usage->field);
  222. else
  223. usage->value[0] =
  224. pidff_rescale(value, S16_MAX, usage->field);
  225. }
  226. pr_debug("calculated from %d to %d\n", value, usage->value[0]);
  227. }
  228. static void pidff_set_time(struct pidff_usage *usage, u16 time)
  229. {
  230. u32 modified_time = pidff_rescale_time(time, usage->field);
  231. usage->value[0] = pidff_clamp(modified_time, usage->field);
  232. }
  233. static void pidff_set_duration(struct pidff_usage *usage, u16 duration)
  234. {
  235. /* Infinite value conversion from Linux API -> PID */
  236. if (duration == FF_INFINITE)
  237. duration = PID_INFINITE;
  238. /* PID defines INFINITE as the max possible value for duration field */
  239. if (duration == PID_INFINITE) {
  240. usage->value[0] = (1U << usage->field->report_size) - 1;
  241. return;
  242. }
  243. pidff_set_time(usage, duration);
  244. }
  245. /*
  246. * Send envelope report to the device
  247. */
  248. static void pidff_set_envelope_report(struct pidff_device *pidff,
  249. struct ff_envelope *envelope)
  250. {
  251. pidff->set_envelope[PID_EFFECT_BLOCK_INDEX].value[0] =
  252. pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0];
  253. pidff->set_envelope[PID_ATTACK_LEVEL].value[0] =
  254. pidff_rescale(envelope->attack_level >
  255. S16_MAX ? S16_MAX : envelope->attack_level, S16_MAX,
  256. pidff->set_envelope[PID_ATTACK_LEVEL].field);
  257. pidff->set_envelope[PID_FADE_LEVEL].value[0] =
  258. pidff_rescale(envelope->fade_level >
  259. S16_MAX ? S16_MAX : envelope->fade_level, S16_MAX,
  260. pidff->set_envelope[PID_FADE_LEVEL].field);
  261. pidff_set_time(&pidff->set_envelope[PID_ATTACK_TIME],
  262. envelope->attack_length);
  263. pidff_set_time(&pidff->set_envelope[PID_FADE_TIME],
  264. envelope->fade_length);
  265. hid_dbg(pidff->hid, "attack %u => %d\n",
  266. envelope->attack_level,
  267. pidff->set_envelope[PID_ATTACK_LEVEL].value[0]);
  268. hid_hw_request(pidff->hid, pidff->reports[PID_SET_ENVELOPE],
  269. HID_REQ_SET_REPORT);
  270. }
  271. /*
  272. * Test if the new envelope differs from old one
  273. */
  274. static int pidff_needs_set_envelope(struct ff_envelope *envelope,
  275. struct ff_envelope *old)
  276. {
  277. bool needs_new_envelope;
  278. needs_new_envelope = envelope->attack_level != 0 ||
  279. envelope->fade_level != 0 ||
  280. envelope->attack_length != 0 ||
  281. envelope->fade_length != 0;
  282. if (!needs_new_envelope)
  283. return false;
  284. if (!old)
  285. return needs_new_envelope;
  286. return envelope->attack_level != old->attack_level ||
  287. envelope->fade_level != old->fade_level ||
  288. envelope->attack_length != old->attack_length ||
  289. envelope->fade_length != old->fade_length;
  290. }
  291. /*
  292. * Send constant force report to the device
  293. */
  294. static void pidff_set_constant_force_report(struct pidff_device *pidff,
  295. struct ff_effect *effect)
  296. {
  297. pidff->set_constant[PID_EFFECT_BLOCK_INDEX].value[0] =
  298. pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0];
  299. pidff_set_signed(&pidff->set_constant[PID_MAGNITUDE],
  300. effect->u.constant.level);
  301. hid_hw_request(pidff->hid, pidff->reports[PID_SET_CONSTANT],
  302. HID_REQ_SET_REPORT);
  303. }
  304. /*
  305. * Test if the constant parameters have changed between effects
  306. */
  307. static int pidff_needs_set_constant(struct ff_effect *effect,
  308. struct ff_effect *old)
  309. {
  310. return effect->u.constant.level != old->u.constant.level;
  311. }
  312. /*
  313. * Send set effect report to the device
  314. */
  315. static void pidff_set_effect_report(struct pidff_device *pidff,
  316. struct ff_effect *effect)
  317. {
  318. pidff->set_effect[PID_EFFECT_BLOCK_INDEX].value[0] =
  319. pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0];
  320. pidff->set_effect_type->value[0] =
  321. pidff->create_new_effect_type->value[0];
  322. pidff_set_duration(&pidff->set_effect[PID_DURATION],
  323. effect->replay.length);
  324. pidff->set_effect[PID_TRIGGER_BUTTON].value[0] = effect->trigger.button;
  325. pidff_set_time(&pidff->set_effect[PID_TRIGGER_REPEAT_INT],
  326. effect->trigger.interval);
  327. pidff->set_effect[PID_GAIN].value[0] =
  328. pidff->set_effect[PID_GAIN].field->logical_maximum;
  329. pidff->set_effect[PID_DIRECTION_ENABLE].value[0] = 1;
  330. /* Use fixed direction if needed */
  331. pidff->effect_direction->value[0] = pidff_rescale(
  332. pidff->quirks & HID_PIDFF_QUIRK_FIX_WHEEL_DIRECTION ?
  333. PIDFF_FIXED_WHEEL_DIRECTION : effect->direction,
  334. U16_MAX, pidff->effect_direction);
  335. /* Omit setting delay field if it's missing */
  336. if (!(pidff->quirks & HID_PIDFF_QUIRK_MISSING_DELAY))
  337. pidff_set_time(&pidff->set_effect[PID_START_DELAY],
  338. effect->replay.delay);
  339. hid_hw_request(pidff->hid, pidff->reports[PID_SET_EFFECT],
  340. HID_REQ_SET_REPORT);
  341. }
  342. /*
  343. * Test if the values used in set_effect have changed
  344. */
  345. static int pidff_needs_set_effect(struct ff_effect *effect,
  346. struct ff_effect *old)
  347. {
  348. return effect->replay.length != old->replay.length ||
  349. effect->trigger.interval != old->trigger.interval ||
  350. effect->trigger.button != old->trigger.button ||
  351. effect->direction != old->direction ||
  352. effect->replay.delay != old->replay.delay;
  353. }
  354. /*
  355. * Send periodic effect report to the device
  356. */
  357. static void pidff_set_periodic_report(struct pidff_device *pidff,
  358. struct ff_effect *effect)
  359. {
  360. pidff->set_periodic[PID_EFFECT_BLOCK_INDEX].value[0] =
  361. pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0];
  362. pidff_set_signed(&pidff->set_periodic[PID_MAGNITUDE],
  363. effect->u.periodic.magnitude);
  364. pidff_set_signed(&pidff->set_periodic[PID_OFFSET],
  365. effect->u.periodic.offset);
  366. pidff_set(&pidff->set_periodic[PID_PHASE], effect->u.periodic.phase);
  367. pidff_set_time(&pidff->set_periodic[PID_PERIOD],
  368. effect->u.periodic.period);
  369. hid_hw_request(pidff->hid, pidff->reports[PID_SET_PERIODIC],
  370. HID_REQ_SET_REPORT);
  371. }
  372. /*
  373. * Test if periodic effect parameters have changed
  374. */
  375. static int pidff_needs_set_periodic(struct ff_effect *effect,
  376. struct ff_effect *old)
  377. {
  378. return effect->u.periodic.magnitude != old->u.periodic.magnitude ||
  379. effect->u.periodic.offset != old->u.periodic.offset ||
  380. effect->u.periodic.phase != old->u.periodic.phase ||
  381. effect->u.periodic.period != old->u.periodic.period;
  382. }
  383. /*
  384. * Send condition effect reports to the device
  385. */
  386. static void pidff_set_condition_report(struct pidff_device *pidff,
  387. struct ff_effect *effect)
  388. {
  389. int i, max_axis;
  390. /* Devices missing Parameter Block Offset can only have one axis */
  391. max_axis = pidff->quirks & HID_PIDFF_QUIRK_MISSING_PBO ? 1 : 2;
  392. pidff->set_condition[PID_EFFECT_BLOCK_INDEX].value[0] =
  393. pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0];
  394. for (i = 0; i < max_axis; i++) {
  395. /* Omit Parameter Block Offset if missing */
  396. if (!(pidff->quirks & HID_PIDFF_QUIRK_MISSING_PBO))
  397. pidff->set_condition[PID_PARAM_BLOCK_OFFSET].value[0] = i;
  398. pidff_set_signed(&pidff->set_condition[PID_CP_OFFSET],
  399. effect->u.condition[i].center);
  400. pidff_set_signed(&pidff->set_condition[PID_POS_COEFFICIENT],
  401. effect->u.condition[i].right_coeff);
  402. pidff_set_signed(&pidff->set_condition[PID_NEG_COEFFICIENT],
  403. effect->u.condition[i].left_coeff);
  404. pidff_set(&pidff->set_condition[PID_POS_SATURATION],
  405. effect->u.condition[i].right_saturation);
  406. pidff_set(&pidff->set_condition[PID_NEG_SATURATION],
  407. effect->u.condition[i].left_saturation);
  408. pidff_set(&pidff->set_condition[PID_DEAD_BAND],
  409. effect->u.condition[i].deadband);
  410. hid_hw_request(pidff->hid, pidff->reports[PID_SET_CONDITION],
  411. HID_REQ_SET_REPORT);
  412. }
  413. }
  414. /*
  415. * Test if condition effect parameters have changed
  416. */
  417. static int pidff_needs_set_condition(struct ff_effect *effect,
  418. struct ff_effect *old)
  419. {
  420. int i;
  421. int ret = 0;
  422. for (i = 0; i < 2; i++) {
  423. struct ff_condition_effect *cond = &effect->u.condition[i];
  424. struct ff_condition_effect *old_cond = &old->u.condition[i];
  425. ret |= cond->center != old_cond->center ||
  426. cond->right_coeff != old_cond->right_coeff ||
  427. cond->left_coeff != old_cond->left_coeff ||
  428. cond->right_saturation != old_cond->right_saturation ||
  429. cond->left_saturation != old_cond->left_saturation ||
  430. cond->deadband != old_cond->deadband;
  431. }
  432. return ret;
  433. }
  434. /*
  435. * Send ramp force report to the device
  436. */
  437. static void pidff_set_ramp_force_report(struct pidff_device *pidff,
  438. struct ff_effect *effect)
  439. {
  440. pidff->set_ramp[PID_EFFECT_BLOCK_INDEX].value[0] =
  441. pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0];
  442. pidff_set_signed(&pidff->set_ramp[PID_RAMP_START],
  443. effect->u.ramp.start_level);
  444. pidff_set_signed(&pidff->set_ramp[PID_RAMP_END],
  445. effect->u.ramp.end_level);
  446. hid_hw_request(pidff->hid, pidff->reports[PID_SET_RAMP],
  447. HID_REQ_SET_REPORT);
  448. }
  449. /*
  450. * Test if ramp force parameters have changed
  451. */
  452. static int pidff_needs_set_ramp(struct ff_effect *effect, struct ff_effect *old)
  453. {
  454. return effect->u.ramp.start_level != old->u.ramp.start_level ||
  455. effect->u.ramp.end_level != old->u.ramp.end_level;
  456. }
  457. /*
  458. * Set device gain
  459. */
  460. static void pidff_set_gain_report(struct pidff_device *pidff, u16 gain)
  461. {
  462. if (!pidff->device_gain[PID_DEVICE_GAIN_FIELD].field)
  463. return;
  464. pidff_set(&pidff->device_gain[PID_DEVICE_GAIN_FIELD], gain);
  465. hid_hw_request(pidff->hid, pidff->reports[PID_DEVICE_GAIN],
  466. HID_REQ_SET_REPORT);
  467. }
  468. /*
  469. * Send device control report to the device
  470. */
  471. static void pidff_set_device_control(struct pidff_device *pidff, int field)
  472. {
  473. int i, index;
  474. int field_index = pidff->control_id[field];
  475. if (field_index < 1)
  476. return;
  477. /* Detect if the field is a bitmask variable or an array */
  478. if (pidff->device_control->flags & HID_MAIN_ITEM_VARIABLE) {
  479. hid_dbg(pidff->hid, "DEVICE_CONTROL is a bitmask\n");
  480. /* Clear current bitmask */
  481. for(i = 0; i < sizeof(pidff_device_control); i++) {
  482. index = pidff->control_id[i];
  483. if (index < 1)
  484. continue;
  485. pidff->device_control->value[index - 1] = 0;
  486. }
  487. pidff->device_control->value[field_index - 1] = 1;
  488. } else {
  489. hid_dbg(pidff->hid, "DEVICE_CONTROL is an array\n");
  490. pidff->device_control->value[0] = field_index;
  491. }
  492. hid_hw_request(pidff->hid, pidff->reports[PID_DEVICE_CONTROL], HID_REQ_SET_REPORT);
  493. hid_hw_wait(pidff->hid);
  494. }
  495. /*
  496. * Modify actuators state
  497. */
  498. static void pidff_set_actuators(struct pidff_device *pidff, bool enable)
  499. {
  500. hid_dbg(pidff->hid, "%s actuators\n", enable ? "Enable" : "Disable");
  501. pidff_set_device_control(pidff,
  502. enable ? PID_ENABLE_ACTUATORS : PID_DISABLE_ACTUATORS);
  503. }
  504. /*
  505. * Reset the device, stop all effects, enable actuators
  506. */
  507. static void pidff_reset(struct pidff_device *pidff)
  508. {
  509. /* We reset twice as sometimes hid_wait_io isn't waiting long enough */
  510. pidff_set_device_control(pidff, PID_RESET);
  511. pidff_set_device_control(pidff, PID_RESET);
  512. pidff->effect_count = 0;
  513. pidff_set_device_control(pidff, PID_STOP_ALL_EFFECTS);
  514. pidff_set_actuators(pidff, 1);
  515. }
  516. /*
  517. * Fetch pool report
  518. */
  519. static void pidff_fetch_pool(struct pidff_device *pidff)
  520. {
  521. int i;
  522. struct hid_device *hid = pidff->hid;
  523. /* Repeat if PID_SIMULTANEOUS_MAX < 2 to make sure it's correct */
  524. for(i = 0; i < 20; i++) {
  525. hid_hw_request(hid, pidff->reports[PID_POOL], HID_REQ_GET_REPORT);
  526. hid_hw_wait(hid);
  527. if (!pidff->pool[PID_SIMULTANEOUS_MAX].value)
  528. return;
  529. if (pidff->pool[PID_SIMULTANEOUS_MAX].value[0] >= 2)
  530. return;
  531. }
  532. hid_warn(hid, "device reports %d simultaneous effects\n",
  533. pidff->pool[PID_SIMULTANEOUS_MAX].value[0]);
  534. }
  535. /*
  536. * Send a request for effect upload to the device
  537. *
  538. * Reset and enable actuators if no effects were present on the device
  539. *
  540. * Returns 0 if device reported success, -ENOSPC if the device reported memory
  541. * is full. Upon unknown response the function will retry for 60 times, if
  542. * still unsuccessful -EIO is returned.
  543. */
  544. static int pidff_request_effect_upload(struct pidff_device *pidff, int efnum)
  545. {
  546. int j;
  547. if (!pidff->effect_count)
  548. pidff_reset(pidff);
  549. pidff->create_new_effect_type->value[0] = efnum;
  550. hid_hw_request(pidff->hid, pidff->reports[PID_CREATE_NEW_EFFECT],
  551. HID_REQ_SET_REPORT);
  552. hid_dbg(pidff->hid, "create_new_effect sent, type: %d\n", efnum);
  553. pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0] = 0;
  554. pidff->block_load_status->value[0] = 0;
  555. hid_hw_wait(pidff->hid);
  556. for (j = 0; j < 60; j++) {
  557. hid_dbg(pidff->hid, "pid_block_load requested\n");
  558. hid_hw_request(pidff->hid, pidff->reports[PID_BLOCK_LOAD],
  559. HID_REQ_GET_REPORT);
  560. hid_hw_wait(pidff->hid);
  561. if (pidff->block_load_status->value[0] ==
  562. pidff->status_id[PID_BLOCK_LOAD_SUCCESS]) {
  563. hid_dbg(pidff->hid, "device reported free memory: %d bytes\n",
  564. pidff->block_load[PID_RAM_POOL_AVAILABLE].value ?
  565. pidff->block_load[PID_RAM_POOL_AVAILABLE].value[0] : -1);
  566. pidff->effect_count++;
  567. return 0;
  568. }
  569. if (pidff->block_load_status->value[0] ==
  570. pidff->status_id[PID_BLOCK_LOAD_FULL]) {
  571. hid_dbg(pidff->hid, "not enough memory free: %d bytes\n",
  572. pidff->block_load[PID_RAM_POOL_AVAILABLE].value ?
  573. pidff->block_load[PID_RAM_POOL_AVAILABLE].value[0] : -1);
  574. return -ENOSPC;
  575. }
  576. if (pidff->block_load_status->value[0] ==
  577. pidff->status_id[PID_BLOCK_LOAD_ERROR]) {
  578. hid_dbg(pidff->hid, "device error during effect creation\n");
  579. return -EREMOTEIO;
  580. }
  581. }
  582. hid_err(pidff->hid, "pid_block_load failed 60 times\n");
  583. return -EIO;
  584. }
  585. /*
  586. * Play the effect with PID id n times
  587. */
  588. static void pidff_playback_pid(struct pidff_device *pidff, int pid_id, int n)
  589. {
  590. pidff->effect_operation[PID_EFFECT_BLOCK_INDEX].value[0] = pid_id;
  591. if (n == 0) {
  592. pidff->effect_operation_status->value[0] =
  593. pidff->operation_id[PID_EFFECT_STOP];
  594. } else {
  595. pidff->effect_operation_status->value[0] =
  596. pidff->operation_id[PID_EFFECT_START];
  597. pidff->effect_operation[PID_LOOP_COUNT].value[0] =
  598. pidff_clamp(n, pidff->effect_operation[PID_LOOP_COUNT].field);
  599. }
  600. hid_hw_request(pidff->hid, pidff->reports[PID_EFFECT_OPERATION],
  601. HID_REQ_SET_REPORT);
  602. }
  603. /*
  604. * Play the effect with effect id @effect_id for @value times
  605. */
  606. static int pidff_playback(struct input_dev *dev, int effect_id, int value)
  607. {
  608. struct pidff_device *pidff = dev->ff->private;
  609. pidff_playback_pid(pidff, pidff->pid_id[effect_id], value);
  610. return 0;
  611. }
  612. /*
  613. * Erase effect with PID id
  614. * Decrease the device effect counter
  615. */
  616. static void pidff_erase_pid(struct pidff_device *pidff, int pid_id)
  617. {
  618. pidff->block_free[PID_EFFECT_BLOCK_INDEX].value[0] = pid_id;
  619. hid_hw_request(pidff->hid, pidff->reports[PID_BLOCK_FREE],
  620. HID_REQ_SET_REPORT);
  621. if (pidff->effect_count > 0)
  622. pidff->effect_count--;
  623. }
  624. /*
  625. * Stop and erase effect with effect_id
  626. */
  627. static int pidff_erase_effect(struct input_dev *dev, int effect_id)
  628. {
  629. struct pidff_device *pidff = dev->ff->private;
  630. int pid_id = pidff->pid_id[effect_id];
  631. hid_dbg(pidff->hid, "starting to erase %d/%d\n",
  632. effect_id, pidff->pid_id[effect_id]);
  633. /*
  634. * Wait for the queue to clear. We do not want
  635. * a full fifo to prevent the effect removal.
  636. */
  637. hid_hw_wait(pidff->hid);
  638. pidff_playback_pid(pidff, pid_id, 0);
  639. pidff_erase_pid(pidff, pid_id);
  640. return 0;
  641. }
  642. /*
  643. * Effect upload handler
  644. */
  645. static int pidff_upload_effect(struct input_dev *dev, struct ff_effect *effect,
  646. struct ff_effect *old)
  647. {
  648. struct pidff_device *pidff = dev->ff->private;
  649. int type_id;
  650. int error;
  651. pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0] = 0;
  652. if (old) {
  653. pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0] =
  654. pidff->pid_id[effect->id];
  655. }
  656. switch (effect->type) {
  657. case FF_CONSTANT:
  658. if (!old) {
  659. error = pidff_request_effect_upload(pidff,
  660. pidff->type_id[PID_CONSTANT]);
  661. if (error)
  662. return error;
  663. }
  664. if (!old || pidff_needs_set_effect(effect, old))
  665. pidff_set_effect_report(pidff, effect);
  666. if (!old || pidff_needs_set_constant(effect, old))
  667. pidff_set_constant_force_report(pidff, effect);
  668. if (pidff_needs_set_envelope(&effect->u.constant.envelope,
  669. old ? &old->u.constant.envelope : NULL))
  670. pidff_set_envelope_report(pidff, &effect->u.constant.envelope);
  671. break;
  672. case FF_PERIODIC:
  673. if (!old) {
  674. switch (effect->u.periodic.waveform) {
  675. case FF_SQUARE:
  676. type_id = PID_SQUARE;
  677. break;
  678. case FF_TRIANGLE:
  679. type_id = PID_TRIANGLE;
  680. break;
  681. case FF_SINE:
  682. type_id = PID_SINE;
  683. break;
  684. case FF_SAW_UP:
  685. type_id = PID_SAW_UP;
  686. break;
  687. case FF_SAW_DOWN:
  688. type_id = PID_SAW_DOWN;
  689. break;
  690. default:
  691. hid_err(pidff->hid, "invalid waveform\n");
  692. return -EINVAL;
  693. }
  694. if (pidff->quirks & HID_PIDFF_QUIRK_PERIODIC_SINE_ONLY)
  695. type_id = PID_SINE;
  696. error = pidff_request_effect_upload(pidff,
  697. pidff->type_id[type_id]);
  698. if (error)
  699. return error;
  700. }
  701. if (!old || pidff_needs_set_effect(effect, old))
  702. pidff_set_effect_report(pidff, effect);
  703. if (!old || pidff_needs_set_periodic(effect, old))
  704. pidff_set_periodic_report(pidff, effect);
  705. if (pidff_needs_set_envelope(&effect->u.periodic.envelope,
  706. old ? &old->u.periodic.envelope : NULL))
  707. pidff_set_envelope_report(pidff, &effect->u.periodic.envelope);
  708. break;
  709. case FF_RAMP:
  710. if (!old) {
  711. error = pidff_request_effect_upload(pidff,
  712. pidff->type_id[PID_RAMP]);
  713. if (error)
  714. return error;
  715. }
  716. if (!old || pidff_needs_set_effect(effect, old))
  717. pidff_set_effect_report(pidff, effect);
  718. if (!old || pidff_needs_set_ramp(effect, old))
  719. pidff_set_ramp_force_report(pidff, effect);
  720. if (pidff_needs_set_envelope(&effect->u.ramp.envelope,
  721. old ? &old->u.ramp.envelope : NULL))
  722. pidff_set_envelope_report(pidff, &effect->u.ramp.envelope);
  723. break;
  724. case FF_SPRING:
  725. case FF_DAMPER:
  726. case FF_INERTIA:
  727. case FF_FRICTION:
  728. if (!old) {
  729. switch(effect->type) {
  730. case FF_SPRING:
  731. type_id = PID_SPRING;
  732. break;
  733. case FF_DAMPER:
  734. type_id = PID_DAMPER;
  735. break;
  736. case FF_INERTIA:
  737. type_id = PID_INERTIA;
  738. break;
  739. case FF_FRICTION:
  740. type_id = PID_FRICTION;
  741. break;
  742. }
  743. error = pidff_request_effect_upload(pidff,
  744. pidff->type_id[type_id]);
  745. if (error)
  746. return error;
  747. }
  748. if (!old || pidff_needs_set_effect(effect, old))
  749. pidff_set_effect_report(pidff, effect);
  750. if (!old || pidff_needs_set_condition(effect, old))
  751. pidff_set_condition_report(pidff, effect);
  752. break;
  753. default:
  754. hid_err(pidff->hid, "invalid type\n");
  755. return -EINVAL;
  756. }
  757. if (!old)
  758. pidff->pid_id[effect->id] =
  759. pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0];
  760. hid_dbg(pidff->hid, "uploaded\n");
  761. return 0;
  762. }
  763. /*
  764. * set_gain() handler
  765. */
  766. static void pidff_set_gain(struct input_dev *dev, u16 gain)
  767. {
  768. pidff_set_gain_report(dev->ff->private, gain);
  769. }
  770. static void pidff_autocenter(struct pidff_device *pidff, u16 magnitude)
  771. {
  772. struct hid_field *field =
  773. pidff->block_load[PID_EFFECT_BLOCK_INDEX].field;
  774. if (!magnitude) {
  775. pidff_playback_pid(pidff, field->logical_minimum, 0);
  776. return;
  777. }
  778. pidff_playback_pid(pidff, field->logical_minimum, 1);
  779. pidff->set_effect[PID_EFFECT_BLOCK_INDEX].value[0] =
  780. pidff->block_load[PID_EFFECT_BLOCK_INDEX].field->logical_minimum;
  781. pidff->set_effect_type->value[0] = pidff->type_id[PID_SPRING];
  782. pidff->set_effect[PID_DURATION].value[0] = 0;
  783. pidff->set_effect[PID_TRIGGER_BUTTON].value[0] = 0;
  784. pidff->set_effect[PID_TRIGGER_REPEAT_INT].value[0] = 0;
  785. pidff_set(&pidff->set_effect[PID_GAIN], magnitude);
  786. pidff->set_effect[PID_DIRECTION_ENABLE].value[0] = 1;
  787. /* Omit setting delay field if it's missing */
  788. if (!(pidff->quirks & HID_PIDFF_QUIRK_MISSING_DELAY))
  789. pidff->set_effect[PID_START_DELAY].value[0] = 0;
  790. hid_hw_request(pidff->hid, pidff->reports[PID_SET_EFFECT],
  791. HID_REQ_SET_REPORT);
  792. }
  793. /*
  794. * pidff_set_autocenter() handler
  795. */
  796. static void pidff_set_autocenter(struct input_dev *dev, u16 magnitude)
  797. {
  798. pidff_autocenter(dev->ff->private, magnitude);
  799. }
  800. /*
  801. * Find fields from a report and fill a pidff_usage
  802. */
  803. static int pidff_find_fields(struct pidff_usage *usage, const u8 *table,
  804. struct hid_report *report, int count, int strict)
  805. {
  806. if (!report) {
  807. pr_debug("pidff_find_fields, null report\n");
  808. return -1;
  809. }
  810. int i, j, k, found;
  811. int return_value = 0;
  812. for (k = 0; k < count; k++) {
  813. found = 0;
  814. for (i = 0; i < report->maxfield; i++) {
  815. if (report->field[i]->maxusage !=
  816. report->field[i]->report_count) {
  817. pr_debug("maxusage and report_count do not match, skipping\n");
  818. continue;
  819. }
  820. for (j = 0; j < report->field[i]->maxusage; j++) {
  821. if (report->field[i]->usage[j].hid ==
  822. (HID_UP_PID | table[k])) {
  823. pr_debug("found %d at %d->%d\n",
  824. k, i, j);
  825. usage[k].field = report->field[i];
  826. usage[k].value =
  827. &report->field[i]->value[j];
  828. found = 1;
  829. break;
  830. }
  831. }
  832. if (found)
  833. break;
  834. }
  835. if (!found && table[k] == pidff_set_effect[PID_START_DELAY]) {
  836. pr_debug("Delay field not found, but that's OK\n");
  837. pr_debug("Setting MISSING_DELAY quirk\n");
  838. return_value |= HID_PIDFF_QUIRK_MISSING_DELAY;
  839. }
  840. else if (!found && table[k] == pidff_set_condition[PID_PARAM_BLOCK_OFFSET]) {
  841. pr_debug("PBO field not found, but that's OK\n");
  842. pr_debug("Setting MISSING_PBO quirk\n");
  843. return_value |= HID_PIDFF_QUIRK_MISSING_PBO;
  844. }
  845. else if (!found && strict) {
  846. pr_debug("failed to locate %d\n", k);
  847. return -1;
  848. }
  849. }
  850. return return_value;
  851. }
  852. /*
  853. * Return index into pidff_reports for the given usage
  854. */
  855. static int pidff_check_usage(int usage)
  856. {
  857. int i;
  858. for (i = 0; i < sizeof(pidff_reports); i++)
  859. if (usage == (HID_UP_PID | pidff_reports[i]))
  860. return i;
  861. return -1;
  862. }
  863. /*
  864. * Find the reports and fill pidff->reports[]
  865. * report_type specifies either OUTPUT or FEATURE reports
  866. */
  867. static void pidff_find_reports(struct hid_device *hid, int report_type,
  868. struct pidff_device *pidff)
  869. {
  870. struct hid_report *report;
  871. int i, ret;
  872. list_for_each_entry(report,
  873. &hid->report_enum[report_type].report_list, list) {
  874. if (report->maxfield < 1)
  875. continue;
  876. ret = pidff_check_usage(report->field[0]->logical);
  877. if (ret != -1) {
  878. hid_dbg(hid, "found usage 0x%02x from field->logical\n",
  879. pidff_reports[ret]);
  880. pidff->reports[ret] = report;
  881. continue;
  882. }
  883. /*
  884. * Sometimes logical collections are stacked to indicate
  885. * different usages for the report and the field, in which
  886. * case we want the usage of the parent. However, Linux HID
  887. * implementation hides this fact, so we have to dig it up
  888. * ourselves
  889. */
  890. i = report->field[0]->usage[0].collection_index;
  891. if (i <= 0 ||
  892. hid->collection[i - 1].type != HID_COLLECTION_LOGICAL)
  893. continue;
  894. ret = pidff_check_usage(hid->collection[i - 1].usage);
  895. if (ret != -1 && !pidff->reports[ret]) {
  896. hid_dbg(hid,
  897. "found usage 0x%02x from collection array\n",
  898. pidff_reports[ret]);
  899. pidff->reports[ret] = report;
  900. }
  901. }
  902. }
  903. /*
  904. * Test if the required reports have been found
  905. */
  906. static int pidff_reports_ok(struct pidff_device *pidff)
  907. {
  908. int i;
  909. for (i = 0; i <= PID_REQUIRED_REPORTS; i++) {
  910. if (!pidff->reports[i]) {
  911. hid_dbg(pidff->hid, "%d missing\n", i);
  912. return 0;
  913. }
  914. }
  915. return 1;
  916. }
  917. /*
  918. * Find a field with a specific usage within a report
  919. */
  920. static struct hid_field *pidff_find_special_field(struct hid_report *report,
  921. int usage, int enforce_min)
  922. {
  923. if (!report) {
  924. pr_debug("pidff_find_special_field, null report\n");
  925. return NULL;
  926. }
  927. int i;
  928. for (i = 0; i < report->maxfield; i++) {
  929. if (report->field[i]->logical == (HID_UP_PID | usage) &&
  930. report->field[i]->report_count > 0) {
  931. if (!enforce_min ||
  932. report->field[i]->logical_minimum == 1)
  933. return report->field[i];
  934. else {
  935. pr_err("logical_minimum is not 1 as it should be\n");
  936. return NULL;
  937. }
  938. }
  939. }
  940. return NULL;
  941. }
  942. /*
  943. * Fill a pidff->*_id struct table
  944. */
  945. static int pidff_find_special_keys(int *keys, struct hid_field *fld,
  946. const u8 *usagetable, int count)
  947. {
  948. int i, j;
  949. int found = 0;
  950. for (i = 0; i < count; i++) {
  951. for (j = 0; j < fld->maxusage; j++) {
  952. if (fld->usage[j].hid == (HID_UP_PID | usagetable[i])) {
  953. keys[i] = j + 1;
  954. found++;
  955. break;
  956. }
  957. }
  958. }
  959. return found;
  960. }
  961. #define PIDFF_FIND_SPECIAL_KEYS(keys, field, name) \
  962. pidff_find_special_keys(pidff->keys, pidff->field, pidff_ ## name, \
  963. sizeof(pidff_ ## name))
  964. /*
  965. * Find and check the special fields
  966. */
  967. static int pidff_find_special_fields(struct pidff_device *pidff)
  968. {
  969. hid_dbg(pidff->hid, "finding special fields\n");
  970. pidff->create_new_effect_type =
  971. pidff_find_special_field(pidff->reports[PID_CREATE_NEW_EFFECT],
  972. PID_EFFECT_TYPE, 1);
  973. pidff->set_effect_type =
  974. pidff_find_special_field(pidff->reports[PID_SET_EFFECT],
  975. PID_EFFECT_TYPE, 1);
  976. pidff->effect_direction =
  977. pidff_find_special_field(pidff->reports[PID_SET_EFFECT],
  978. PID_DIRECTION, 0);
  979. pidff->device_control =
  980. pidff_find_special_field(pidff->reports[PID_DEVICE_CONTROL],
  981. PID_DEVICE_CONTROL_ARRAY,
  982. !(pidff->quirks & HID_PIDFF_QUIRK_PERMISSIVE_CONTROL));
  983. pidff->block_load_status =
  984. pidff_find_special_field(pidff->reports[PID_BLOCK_LOAD],
  985. PID_BLOCK_LOAD_STATUS, 1);
  986. pidff->effect_operation_status =
  987. pidff_find_special_field(pidff->reports[PID_EFFECT_OPERATION],
  988. PID_EFFECT_OPERATION_ARRAY, 1);
  989. hid_dbg(pidff->hid, "search done\n");
  990. if (!pidff->create_new_effect_type || !pidff->set_effect_type) {
  991. hid_err(pidff->hid, "effect lists not found\n");
  992. return -1;
  993. }
  994. if (!pidff->effect_direction) {
  995. hid_err(pidff->hid, "direction field not found\n");
  996. return -1;
  997. }
  998. if (!pidff->device_control) {
  999. hid_err(pidff->hid, "device control field not found\n");
  1000. return -1;
  1001. }
  1002. if (!pidff->block_load_status) {
  1003. hid_err(pidff->hid, "block load status field not found\n");
  1004. return -1;
  1005. }
  1006. if (!pidff->effect_operation_status) {
  1007. hid_err(pidff->hid, "effect operation field not found\n");
  1008. return -1;
  1009. }
  1010. PIDFF_FIND_SPECIAL_KEYS(control_id, device_control, device_control);
  1011. if (!PIDFF_FIND_SPECIAL_KEYS(type_id, create_new_effect_type,
  1012. effect_types)) {
  1013. hid_err(pidff->hid, "no effect types found\n");
  1014. return -1;
  1015. }
  1016. if (PIDFF_FIND_SPECIAL_KEYS(status_id, block_load_status,
  1017. block_load_status) !=
  1018. sizeof(pidff_block_load_status)) {
  1019. hid_err(pidff->hid,
  1020. "block load status identifiers not found\n");
  1021. return -1;
  1022. }
  1023. if (PIDFF_FIND_SPECIAL_KEYS(operation_id, effect_operation_status,
  1024. effect_operation_status) !=
  1025. sizeof(pidff_effect_operation_status)) {
  1026. hid_err(pidff->hid, "effect operation identifiers not found\n");
  1027. return -1;
  1028. }
  1029. return 0;
  1030. }
  1031. /*
  1032. * Find the implemented effect types
  1033. */
  1034. static int pidff_find_effects(struct pidff_device *pidff,
  1035. struct input_dev *dev)
  1036. {
  1037. int i;
  1038. for (i = 0; i < sizeof(pidff_effect_types); i++) {
  1039. int pidff_type = pidff->type_id[i];
  1040. if (pidff->set_effect_type->usage[pidff_type].hid !=
  1041. pidff->create_new_effect_type->usage[pidff_type].hid) {
  1042. hid_err(pidff->hid,
  1043. "effect type number %d is invalid\n", i);
  1044. return -1;
  1045. }
  1046. }
  1047. if (pidff->type_id[PID_CONSTANT])
  1048. set_bit(FF_CONSTANT, dev->ffbit);
  1049. if (pidff->type_id[PID_RAMP])
  1050. set_bit(FF_RAMP, dev->ffbit);
  1051. if (pidff->type_id[PID_SQUARE]) {
  1052. set_bit(FF_SQUARE, dev->ffbit);
  1053. set_bit(FF_PERIODIC, dev->ffbit);
  1054. }
  1055. if (pidff->type_id[PID_SINE]) {
  1056. set_bit(FF_SINE, dev->ffbit);
  1057. set_bit(FF_PERIODIC, dev->ffbit);
  1058. }
  1059. if (pidff->type_id[PID_TRIANGLE]) {
  1060. set_bit(FF_TRIANGLE, dev->ffbit);
  1061. set_bit(FF_PERIODIC, dev->ffbit);
  1062. }
  1063. if (pidff->type_id[PID_SAW_UP]) {
  1064. set_bit(FF_SAW_UP, dev->ffbit);
  1065. set_bit(FF_PERIODIC, dev->ffbit);
  1066. }
  1067. if (pidff->type_id[PID_SAW_DOWN]) {
  1068. set_bit(FF_SAW_DOWN, dev->ffbit);
  1069. set_bit(FF_PERIODIC, dev->ffbit);
  1070. }
  1071. if (pidff->type_id[PID_SPRING])
  1072. set_bit(FF_SPRING, dev->ffbit);
  1073. if (pidff->type_id[PID_DAMPER])
  1074. set_bit(FF_DAMPER, dev->ffbit);
  1075. if (pidff->type_id[PID_INERTIA])
  1076. set_bit(FF_INERTIA, dev->ffbit);
  1077. if (pidff->type_id[PID_FRICTION])
  1078. set_bit(FF_FRICTION, dev->ffbit);
  1079. return 0;
  1080. }
  1081. #define PIDFF_FIND_FIELDS(name, report, strict) \
  1082. pidff_find_fields(pidff->name, pidff_ ## name, \
  1083. pidff->reports[report], \
  1084. sizeof(pidff_ ## name), strict)
  1085. /*
  1086. * Fill and check the pidff_usages
  1087. */
  1088. static int pidff_init_fields(struct pidff_device *pidff, struct input_dev *dev)
  1089. {
  1090. int status = 0;
  1091. /* Save info about the device not having the DELAY ffb field. */
  1092. status = PIDFF_FIND_FIELDS(set_effect, PID_SET_EFFECT, 1);
  1093. if (status == -1) {
  1094. hid_err(pidff->hid, "unknown set_effect report layout\n");
  1095. return -ENODEV;
  1096. }
  1097. pidff->quirks |= status;
  1098. if (status & HID_PIDFF_QUIRK_MISSING_DELAY)
  1099. hid_dbg(pidff->hid, "Adding MISSING_DELAY quirk\n");
  1100. PIDFF_FIND_FIELDS(block_load, PID_BLOCK_LOAD, 0);
  1101. if (!pidff->block_load[PID_EFFECT_BLOCK_INDEX].value) {
  1102. hid_err(pidff->hid, "unknown pid_block_load report layout\n");
  1103. return -ENODEV;
  1104. }
  1105. if (PIDFF_FIND_FIELDS(effect_operation, PID_EFFECT_OPERATION, 1)) {
  1106. hid_err(pidff->hid, "unknown effect_operation report layout\n");
  1107. return -ENODEV;
  1108. }
  1109. if (PIDFF_FIND_FIELDS(block_free, PID_BLOCK_FREE, 1)) {
  1110. hid_err(pidff->hid, "unknown pid_block_free report layout\n");
  1111. return -ENODEV;
  1112. }
  1113. if (pidff_find_special_fields(pidff) || pidff_find_effects(pidff, dev))
  1114. return -ENODEV;
  1115. if (PIDFF_FIND_FIELDS(set_envelope, PID_SET_ENVELOPE, 1)) {
  1116. if (test_and_clear_bit(FF_CONSTANT, dev->ffbit))
  1117. hid_warn(pidff->hid,
  1118. "has constant effect but no envelope\n");
  1119. if (test_and_clear_bit(FF_RAMP, dev->ffbit))
  1120. hid_warn(pidff->hid,
  1121. "has ramp effect but no envelope\n");
  1122. if (test_and_clear_bit(FF_PERIODIC, dev->ffbit))
  1123. hid_warn(pidff->hid,
  1124. "has periodic effect but no envelope\n");
  1125. }
  1126. if (test_bit(FF_CONSTANT, dev->ffbit) &&
  1127. PIDFF_FIND_FIELDS(set_constant, PID_SET_CONSTANT, 1)) {
  1128. hid_warn(pidff->hid, "unknown constant effect layout\n");
  1129. clear_bit(FF_CONSTANT, dev->ffbit);
  1130. }
  1131. if (test_bit(FF_RAMP, dev->ffbit) &&
  1132. PIDFF_FIND_FIELDS(set_ramp, PID_SET_RAMP, 1)) {
  1133. hid_warn(pidff->hid, "unknown ramp effect layout\n");
  1134. clear_bit(FF_RAMP, dev->ffbit);
  1135. }
  1136. if (test_bit(FF_SPRING, dev->ffbit) ||
  1137. test_bit(FF_DAMPER, dev->ffbit) ||
  1138. test_bit(FF_FRICTION, dev->ffbit) ||
  1139. test_bit(FF_INERTIA, dev->ffbit)) {
  1140. status = PIDFF_FIND_FIELDS(set_condition, PID_SET_CONDITION, 1);
  1141. if (status < 0) {
  1142. hid_warn(pidff->hid, "unknown condition effect layout\n");
  1143. clear_bit(FF_SPRING, dev->ffbit);
  1144. clear_bit(FF_DAMPER, dev->ffbit);
  1145. clear_bit(FF_FRICTION, dev->ffbit);
  1146. clear_bit(FF_INERTIA, dev->ffbit);
  1147. }
  1148. pidff->quirks |= status;
  1149. }
  1150. if (test_bit(FF_PERIODIC, dev->ffbit) &&
  1151. PIDFF_FIND_FIELDS(set_periodic, PID_SET_PERIODIC, 1)) {
  1152. hid_warn(pidff->hid, "unknown periodic effect layout\n");
  1153. clear_bit(FF_PERIODIC, dev->ffbit);
  1154. }
  1155. PIDFF_FIND_FIELDS(pool, PID_POOL, 0);
  1156. if (!PIDFF_FIND_FIELDS(device_gain, PID_DEVICE_GAIN, 1))
  1157. set_bit(FF_GAIN, dev->ffbit);
  1158. return 0;
  1159. }
  1160. /*
  1161. * Test if autocenter modification is using the supported method
  1162. */
  1163. static int pidff_check_autocenter(struct pidff_device *pidff,
  1164. struct input_dev *dev)
  1165. {
  1166. int error;
  1167. /*
  1168. * Let's find out if autocenter modification is supported
  1169. * Specification doesn't specify anything, so we request an
  1170. * effect upload and cancel it immediately. If the approved
  1171. * effect id was one above the minimum, then we assume the first
  1172. * effect id is a built-in spring type effect used for autocenter
  1173. */
  1174. error = pidff_request_effect_upload(pidff, 1);
  1175. if (error) {
  1176. hid_err(pidff->hid, "upload request failed\n");
  1177. return error;
  1178. }
  1179. if (pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0] ==
  1180. pidff->block_load[PID_EFFECT_BLOCK_INDEX].field->logical_minimum + 1) {
  1181. pidff_autocenter(pidff, U16_MAX);
  1182. set_bit(FF_AUTOCENTER, dev->ffbit);
  1183. } else {
  1184. hid_notice(pidff->hid,
  1185. "device has unknown autocenter control method\n");
  1186. }
  1187. pidff_erase_pid(pidff,
  1188. pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0]);
  1189. return 0;
  1190. }
  1191. /*
  1192. * Check if the device is PID and initialize it
  1193. * Set initial quirks
  1194. */
  1195. int hid_pidff_init_with_quirks(struct hid_device *hid, u32 initial_quirks)
  1196. {
  1197. struct pidff_device *pidff;
  1198. struct hid_input *hidinput = list_entry(hid->inputs.next,
  1199. struct hid_input, list);
  1200. struct input_dev *dev = hidinput->input;
  1201. struct ff_device *ff;
  1202. int max_effects;
  1203. int error;
  1204. hid_dbg(hid, "starting pid init\n");
  1205. if (list_empty(&hid->report_enum[HID_OUTPUT_REPORT].report_list)) {
  1206. hid_dbg(hid, "not a PID device, no output report\n");
  1207. return -ENODEV;
  1208. }
  1209. pidff = kzalloc(sizeof(*pidff), GFP_KERNEL);
  1210. if (!pidff)
  1211. return -ENOMEM;
  1212. pidff->hid = hid;
  1213. pidff->quirks = initial_quirks;
  1214. pidff->effect_count = 0;
  1215. hid_device_io_start(hid);
  1216. pidff_find_reports(hid, HID_OUTPUT_REPORT, pidff);
  1217. pidff_find_reports(hid, HID_FEATURE_REPORT, pidff);
  1218. if (!pidff_reports_ok(pidff)) {
  1219. hid_dbg(hid, "reports not ok, aborting\n");
  1220. error = -ENODEV;
  1221. goto fail;
  1222. }
  1223. error = pidff_init_fields(pidff, dev);
  1224. if (error)
  1225. goto fail;
  1226. /* pool report is sometimes messed up, refetch it */
  1227. pidff_fetch_pool(pidff);
  1228. pidff_set_gain_report(pidff, U16_MAX);
  1229. error = pidff_check_autocenter(pidff, dev);
  1230. if (error)
  1231. goto fail;
  1232. max_effects =
  1233. pidff->block_load[PID_EFFECT_BLOCK_INDEX].field->logical_maximum -
  1234. pidff->block_load[PID_EFFECT_BLOCK_INDEX].field->logical_minimum +
  1235. 1;
  1236. hid_dbg(hid, "max effects is %d\n", max_effects);
  1237. if (max_effects > PID_EFFECTS_MAX)
  1238. max_effects = PID_EFFECTS_MAX;
  1239. if (pidff->pool[PID_SIMULTANEOUS_MAX].value)
  1240. hid_dbg(hid, "max simultaneous effects is %d\n",
  1241. pidff->pool[PID_SIMULTANEOUS_MAX].value[0]);
  1242. if (pidff->pool[PID_RAM_POOL_SIZE].value)
  1243. hid_dbg(hid, "device memory size is %d bytes\n",
  1244. pidff->pool[PID_RAM_POOL_SIZE].value[0]);
  1245. if (pidff->pool[PID_DEVICE_MANAGED_POOL].value &&
  1246. pidff->pool[PID_DEVICE_MANAGED_POOL].value[0] == 0) {
  1247. error = -EPERM;
  1248. hid_notice(hid,
  1249. "device does not support device managed pool\n");
  1250. goto fail;
  1251. }
  1252. error = input_ff_create(dev, max_effects);
  1253. if (error)
  1254. goto fail;
  1255. ff = dev->ff;
  1256. ff->private = pidff;
  1257. ff->upload = pidff_upload_effect;
  1258. ff->erase = pidff_erase_effect;
  1259. ff->set_gain = pidff_set_gain;
  1260. ff->set_autocenter = pidff_set_autocenter;
  1261. ff->playback = pidff_playback;
  1262. hid_info(dev, "Force feedback for USB HID PID devices by Anssi Hannula <anssi.hannula@gmail.com>\n");
  1263. hid_dbg(dev, "Active quirks mask: 0x%x\n", pidff->quirks);
  1264. hid_device_io_stop(hid);
  1265. return 0;
  1266. fail:
  1267. hid_device_io_stop(hid);
  1268. kfree(pidff);
  1269. return error;
  1270. }
  1271. EXPORT_SYMBOL_GPL(hid_pidff_init_with_quirks);
  1272. /*
  1273. * Check if the device is PID and initialize it
  1274. * Wrapper made to keep the compatibility with old
  1275. * init function
  1276. */
  1277. int hid_pidff_init(struct hid_device *hid)
  1278. {
  1279. return hid_pidff_init_with_quirks(hid, 0);
  1280. }