hid-pidff.c 36 KB

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