hid-pidff.c 36 KB

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