hid-pidff.c 36 KB

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