iforce-ff.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (c) 2000-2002 Vojtech Pavlik <vojtech@ucw.cz>
  4. * Copyright (c) 2001-2002, 2007 Johann Deneux <johann.deneux@gmail.com>
  5. *
  6. * USB/RS232 I-Force joysticks and wheels.
  7. */
  8. #include "iforce.h"
  9. /*
  10. * Set the magnitude of a constant force effect
  11. * Return error code
  12. *
  13. * Note: caller must ensure exclusive access to device
  14. */
  15. static int make_magnitude_modifier(struct iforce* iforce,
  16. struct resource* mod_chunk, int no_alloc, __s16 level)
  17. {
  18. unsigned char data[3];
  19. if (!no_alloc) {
  20. mutex_lock(&iforce->mem_mutex);
  21. if (allocate_resource(&(iforce->device_memory), mod_chunk, 2,
  22. iforce->device_memory.start, iforce->device_memory.end, 2L,
  23. NULL, NULL)) {
  24. mutex_unlock(&iforce->mem_mutex);
  25. return -ENOSPC;
  26. }
  27. mutex_unlock(&iforce->mem_mutex);
  28. }
  29. data[0] = LO(mod_chunk->start);
  30. data[1] = HI(mod_chunk->start);
  31. data[2] = HIFIX80(level);
  32. iforce_send_packet(iforce, FF_CMD_MAGNITUDE, data);
  33. iforce_dump_packet(iforce, "magnitude", FF_CMD_MAGNITUDE, data);
  34. return 0;
  35. }
  36. /*
  37. * Upload the component of an effect dealing with the period, phase and magnitude
  38. */
  39. static int make_period_modifier(struct iforce* iforce,
  40. struct resource* mod_chunk, int no_alloc,
  41. __s16 magnitude, __s16 offset, u16 period, u16 phase)
  42. {
  43. unsigned char data[7];
  44. period = TIME_SCALE(period);
  45. if (!no_alloc) {
  46. mutex_lock(&iforce->mem_mutex);
  47. if (allocate_resource(&(iforce->device_memory), mod_chunk, 0x0c,
  48. iforce->device_memory.start, iforce->device_memory.end, 2L,
  49. NULL, NULL)) {
  50. mutex_unlock(&iforce->mem_mutex);
  51. return -ENOSPC;
  52. }
  53. mutex_unlock(&iforce->mem_mutex);
  54. }
  55. data[0] = LO(mod_chunk->start);
  56. data[1] = HI(mod_chunk->start);
  57. data[2] = HIFIX80(magnitude);
  58. data[3] = HIFIX80(offset);
  59. data[4] = HI(phase);
  60. data[5] = LO(period);
  61. data[6] = HI(period);
  62. iforce_send_packet(iforce, FF_CMD_PERIOD, data);
  63. return 0;
  64. }
  65. /*
  66. * Uploads the part of an effect setting the envelope of the force
  67. */
  68. static int make_envelope_modifier(struct iforce* iforce,
  69. struct resource* mod_chunk, int no_alloc,
  70. u16 attack_duration, __s16 initial_level,
  71. u16 fade_duration, __s16 final_level)
  72. {
  73. unsigned char data[8];
  74. attack_duration = TIME_SCALE(attack_duration);
  75. fade_duration = TIME_SCALE(fade_duration);
  76. if (!no_alloc) {
  77. mutex_lock(&iforce->mem_mutex);
  78. if (allocate_resource(&(iforce->device_memory), mod_chunk, 0x0e,
  79. iforce->device_memory.start, iforce->device_memory.end, 2L,
  80. NULL, NULL)) {
  81. mutex_unlock(&iforce->mem_mutex);
  82. return -ENOSPC;
  83. }
  84. mutex_unlock(&iforce->mem_mutex);
  85. }
  86. data[0] = LO(mod_chunk->start);
  87. data[1] = HI(mod_chunk->start);
  88. data[2] = LO(attack_duration);
  89. data[3] = HI(attack_duration);
  90. data[4] = HI(initial_level);
  91. data[5] = LO(fade_duration);
  92. data[6] = HI(fade_duration);
  93. data[7] = HI(final_level);
  94. iforce_send_packet(iforce, FF_CMD_ENVELOPE, data);
  95. return 0;
  96. }
  97. /*
  98. * Component of spring, friction, inertia... effects
  99. */
  100. static int make_condition_modifier(struct iforce* iforce,
  101. struct resource* mod_chunk, int no_alloc,
  102. __u16 rsat, __u16 lsat, __s16 rk, __s16 lk, u16 db, __s16 center)
  103. {
  104. unsigned char data[10];
  105. if (!no_alloc) {
  106. mutex_lock(&iforce->mem_mutex);
  107. if (allocate_resource(&(iforce->device_memory), mod_chunk, 8,
  108. iforce->device_memory.start, iforce->device_memory.end, 2L,
  109. NULL, NULL)) {
  110. mutex_unlock(&iforce->mem_mutex);
  111. return -ENOSPC;
  112. }
  113. mutex_unlock(&iforce->mem_mutex);
  114. }
  115. data[0] = LO(mod_chunk->start);
  116. data[1] = HI(mod_chunk->start);
  117. data[2] = (100 * rk) >> 15; /* Dangerous: the sign is extended by gcc on plateforms providing an arith shift */
  118. data[3] = (100 * lk) >> 15; /* This code is incorrect on cpus lacking arith shift */
  119. center = (500 * center) >> 15;
  120. data[4] = LO(center);
  121. data[5] = HI(center);
  122. db = (1000 * db) >> 16;
  123. data[6] = LO(db);
  124. data[7] = HI(db);
  125. data[8] = (100 * rsat) >> 16;
  126. data[9] = (100 * lsat) >> 16;
  127. iforce_send_packet(iforce, FF_CMD_CONDITION, data);
  128. iforce_dump_packet(iforce, "condition", FF_CMD_CONDITION, data);
  129. return 0;
  130. }
  131. static unsigned char find_button(struct iforce *iforce, signed short button)
  132. {
  133. int i;
  134. for (i = 1; iforce->type->btn[i] >= 0; i++)
  135. if (iforce->type->btn[i] == button)
  136. return i + 1;
  137. return 0;
  138. }
  139. /*
  140. * Analyse the changes in an effect, and tell if we need to send an condition
  141. * parameter packet
  142. */
  143. static int need_condition_modifier(struct iforce *iforce,
  144. struct ff_effect *old,
  145. struct ff_effect *new)
  146. {
  147. int ret = 0;
  148. int i;
  149. if (new->type != FF_SPRING && new->type != FF_FRICTION) {
  150. dev_warn(&iforce->dev->dev, "bad effect type in %s\n",
  151. __func__);
  152. return 0;
  153. }
  154. for (i = 0; i < 2; i++) {
  155. ret |= old->u.condition[i].right_saturation != new->u.condition[i].right_saturation
  156. || old->u.condition[i].left_saturation != new->u.condition[i].left_saturation
  157. || old->u.condition[i].right_coeff != new->u.condition[i].right_coeff
  158. || old->u.condition[i].left_coeff != new->u.condition[i].left_coeff
  159. || old->u.condition[i].deadband != new->u.condition[i].deadband
  160. || old->u.condition[i].center != new->u.condition[i].center;
  161. }
  162. return ret;
  163. }
  164. /*
  165. * Analyse the changes in an effect, and tell if we need to send a magnitude
  166. * parameter packet
  167. */
  168. static int need_magnitude_modifier(struct iforce *iforce,
  169. struct ff_effect *old,
  170. struct ff_effect *effect)
  171. {
  172. if (effect->type != FF_CONSTANT) {
  173. dev_warn(&iforce->dev->dev, "bad effect type in %s\n",
  174. __func__);
  175. return 0;
  176. }
  177. return old->u.constant.level != effect->u.constant.level;
  178. }
  179. /*
  180. * Analyse the changes in an effect, and tell if we need to send an envelope
  181. * parameter packet
  182. */
  183. static int need_envelope_modifier(struct iforce *iforce, struct ff_effect *old,
  184. struct ff_effect *effect)
  185. {
  186. switch (effect->type) {
  187. case FF_CONSTANT:
  188. if (old->u.constant.envelope.attack_length != effect->u.constant.envelope.attack_length
  189. || old->u.constant.envelope.attack_level != effect->u.constant.envelope.attack_level
  190. || old->u.constant.envelope.fade_length != effect->u.constant.envelope.fade_length
  191. || old->u.constant.envelope.fade_level != effect->u.constant.envelope.fade_level)
  192. return 1;
  193. break;
  194. case FF_PERIODIC:
  195. if (old->u.periodic.envelope.attack_length != effect->u.periodic.envelope.attack_length
  196. || old->u.periodic.envelope.attack_level != effect->u.periodic.envelope.attack_level
  197. || old->u.periodic.envelope.fade_length != effect->u.periodic.envelope.fade_length
  198. || old->u.periodic.envelope.fade_level != effect->u.periodic.envelope.fade_level)
  199. return 1;
  200. break;
  201. default:
  202. dev_warn(&iforce->dev->dev, "bad effect type in %s\n",
  203. __func__);
  204. }
  205. return 0;
  206. }
  207. /*
  208. * Analyse the changes in an effect, and tell if we need to send a periodic
  209. * parameter effect
  210. */
  211. static int need_period_modifier(struct iforce *iforce, struct ff_effect *old,
  212. struct ff_effect *new)
  213. {
  214. if (new->type != FF_PERIODIC) {
  215. dev_warn(&iforce->dev->dev, "bad effect type in %s\n",
  216. __func__);
  217. return 0;
  218. }
  219. return (old->u.periodic.period != new->u.periodic.period
  220. || old->u.periodic.magnitude != new->u.periodic.magnitude
  221. || old->u.periodic.offset != new->u.periodic.offset
  222. || old->u.periodic.phase != new->u.periodic.phase);
  223. }
  224. /*
  225. * Analyse the changes in an effect, and tell if we need to send an effect
  226. * packet
  227. */
  228. static int need_core(struct ff_effect *old, struct ff_effect *new)
  229. {
  230. if (old->direction != new->direction
  231. || old->trigger.button != new->trigger.button
  232. || old->trigger.interval != new->trigger.interval
  233. || old->replay.length != new->replay.length
  234. || old->replay.delay != new->replay.delay)
  235. return 1;
  236. return 0;
  237. }
  238. /*
  239. * Send the part common to all effects to the device
  240. */
  241. static int make_core(struct iforce* iforce, u16 id, u16 mod_id1, u16 mod_id2,
  242. u8 effect_type, u8 axes, u16 duration, u16 delay, u16 button,
  243. u16 interval, u16 direction)
  244. {
  245. unsigned char data[14];
  246. duration = TIME_SCALE(duration);
  247. delay = TIME_SCALE(delay);
  248. interval = TIME_SCALE(interval);
  249. data[0] = LO(id);
  250. data[1] = effect_type;
  251. data[2] = LO(axes) | find_button(iforce, button);
  252. data[3] = LO(duration);
  253. data[4] = HI(duration);
  254. data[5] = HI(direction);
  255. data[6] = LO(interval);
  256. data[7] = HI(interval);
  257. data[8] = LO(mod_id1);
  258. data[9] = HI(mod_id1);
  259. data[10] = LO(mod_id2);
  260. data[11] = HI(mod_id2);
  261. data[12] = LO(delay);
  262. data[13] = HI(delay);
  263. /* Stop effect */
  264. /* iforce_control_playback(iforce, id, 0);*/
  265. iforce_send_packet(iforce, FF_CMD_EFFECT, data);
  266. /* If needed, restart effect */
  267. if (test_bit(FF_CORE_SHOULD_PLAY, iforce->core_effects[id].flags)) {
  268. /* BUG: perhaps we should replay n times, instead of 1. But we do not know n */
  269. iforce_control_playback(iforce, id, 1);
  270. }
  271. return 0;
  272. }
  273. /*
  274. * Upload a periodic effect to the device
  275. * See also iforce_upload_constant.
  276. */
  277. int iforce_upload_periodic(struct iforce *iforce, struct ff_effect *effect, struct ff_effect *old)
  278. {
  279. u8 wave_code;
  280. int core_id = effect->id;
  281. struct iforce_core_effect* core_effect = iforce->core_effects + core_id;
  282. struct resource* mod1_chunk = &(iforce->core_effects[core_id].mod1_chunk);
  283. struct resource* mod2_chunk = &(iforce->core_effects[core_id].mod2_chunk);
  284. int param1_err = 1;
  285. int param2_err = 1;
  286. int core_err = 0;
  287. if (!old || need_period_modifier(iforce, old, effect)) {
  288. param1_err = make_period_modifier(iforce, mod1_chunk,
  289. old != NULL,
  290. effect->u.periodic.magnitude, effect->u.periodic.offset,
  291. effect->u.periodic.period, effect->u.periodic.phase);
  292. if (param1_err)
  293. return param1_err;
  294. set_bit(FF_MOD1_IS_USED, core_effect->flags);
  295. }
  296. if (!old || need_envelope_modifier(iforce, old, effect)) {
  297. param2_err = make_envelope_modifier(iforce, mod2_chunk,
  298. old !=NULL,
  299. effect->u.periodic.envelope.attack_length,
  300. effect->u.periodic.envelope.attack_level,
  301. effect->u.periodic.envelope.fade_length,
  302. effect->u.periodic.envelope.fade_level);
  303. if (param2_err)
  304. return param2_err;
  305. set_bit(FF_MOD2_IS_USED, core_effect->flags);
  306. }
  307. switch (effect->u.periodic.waveform) {
  308. case FF_SQUARE: wave_code = 0x20; break;
  309. case FF_TRIANGLE: wave_code = 0x21; break;
  310. case FF_SINE: wave_code = 0x22; break;
  311. case FF_SAW_UP: wave_code = 0x23; break;
  312. case FF_SAW_DOWN: wave_code = 0x24; break;
  313. default: wave_code = 0x20; break;
  314. }
  315. if (!old || need_core(old, effect)) {
  316. core_err = make_core(iforce, effect->id,
  317. mod1_chunk->start,
  318. mod2_chunk->start,
  319. wave_code,
  320. 0x20,
  321. effect->replay.length,
  322. effect->replay.delay,
  323. effect->trigger.button,
  324. effect->trigger.interval,
  325. effect->direction);
  326. }
  327. /* If one of the parameter creation failed, we already returned an
  328. * error code.
  329. * If the core creation failed, we return its error code.
  330. * Else: if one parameter at least was created, we return 0
  331. * else we return 1;
  332. */
  333. return core_err < 0 ? core_err : (param1_err && param2_err);
  334. }
  335. /*
  336. * Upload a constant force effect
  337. * Return value:
  338. * <0 Error code
  339. * 0 Ok, effect created or updated
  340. * 1 effect did not change since last upload, and no packet was therefore sent
  341. */
  342. int iforce_upload_constant(struct iforce *iforce, struct ff_effect *effect, struct ff_effect *old)
  343. {
  344. int core_id = effect->id;
  345. struct iforce_core_effect* core_effect = iforce->core_effects + core_id;
  346. struct resource* mod1_chunk = &(iforce->core_effects[core_id].mod1_chunk);
  347. struct resource* mod2_chunk = &(iforce->core_effects[core_id].mod2_chunk);
  348. int param1_err = 1;
  349. int param2_err = 1;
  350. int core_err = 0;
  351. if (!old || need_magnitude_modifier(iforce, old, effect)) {
  352. param1_err = make_magnitude_modifier(iforce, mod1_chunk,
  353. old != NULL,
  354. effect->u.constant.level);
  355. if (param1_err)
  356. return param1_err;
  357. set_bit(FF_MOD1_IS_USED, core_effect->flags);
  358. }
  359. if (!old || need_envelope_modifier(iforce, old, effect)) {
  360. param2_err = make_envelope_modifier(iforce, mod2_chunk,
  361. old != NULL,
  362. effect->u.constant.envelope.attack_length,
  363. effect->u.constant.envelope.attack_level,
  364. effect->u.constant.envelope.fade_length,
  365. effect->u.constant.envelope.fade_level);
  366. if (param2_err)
  367. return param2_err;
  368. set_bit(FF_MOD2_IS_USED, core_effect->flags);
  369. }
  370. if (!old || need_core(old, effect)) {
  371. core_err = make_core(iforce, effect->id,
  372. mod1_chunk->start,
  373. mod2_chunk->start,
  374. 0x00,
  375. 0x20,
  376. effect->replay.length,
  377. effect->replay.delay,
  378. effect->trigger.button,
  379. effect->trigger.interval,
  380. effect->direction);
  381. }
  382. /* If one of the parameter creation failed, we already returned an
  383. * error code.
  384. * If the core creation failed, we return its error code.
  385. * Else: if one parameter at least was created, we return 0
  386. * else we return 1;
  387. */
  388. return core_err < 0 ? core_err : (param1_err && param2_err);
  389. }
  390. /*
  391. * Upload an condition effect. Those are for example friction, inertia, springs...
  392. */
  393. int iforce_upload_condition(struct iforce *iforce, struct ff_effect *effect, struct ff_effect *old)
  394. {
  395. int core_id = effect->id;
  396. struct iforce_core_effect* core_effect = iforce->core_effects + core_id;
  397. struct resource* mod1_chunk = &(core_effect->mod1_chunk);
  398. struct resource* mod2_chunk = &(core_effect->mod2_chunk);
  399. u8 type;
  400. int param_err = 1;
  401. int core_err = 0;
  402. switch (effect->type) {
  403. case FF_SPRING: type = 0x40; break;
  404. case FF_DAMPER: type = 0x41; break;
  405. default: return -1;
  406. }
  407. if (!old || need_condition_modifier(iforce, old, effect)) {
  408. param_err = make_condition_modifier(iforce, mod1_chunk,
  409. old != NULL,
  410. effect->u.condition[0].right_saturation,
  411. effect->u.condition[0].left_saturation,
  412. effect->u.condition[0].right_coeff,
  413. effect->u.condition[0].left_coeff,
  414. effect->u.condition[0].deadband,
  415. effect->u.condition[0].center);
  416. if (param_err)
  417. return param_err;
  418. set_bit(FF_MOD1_IS_USED, core_effect->flags);
  419. param_err = make_condition_modifier(iforce, mod2_chunk,
  420. old != NULL,
  421. effect->u.condition[1].right_saturation,
  422. effect->u.condition[1].left_saturation,
  423. effect->u.condition[1].right_coeff,
  424. effect->u.condition[1].left_coeff,
  425. effect->u.condition[1].deadband,
  426. effect->u.condition[1].center);
  427. if (param_err)
  428. return param_err;
  429. set_bit(FF_MOD2_IS_USED, core_effect->flags);
  430. }
  431. if (!old || need_core(old, effect)) {
  432. core_err = make_core(iforce, effect->id,
  433. mod1_chunk->start, mod2_chunk->start,
  434. type, 0xc0,
  435. effect->replay.length, effect->replay.delay,
  436. effect->trigger.button, effect->trigger.interval,
  437. effect->direction);
  438. }
  439. /* If the parameter creation failed, we already returned an
  440. * error code.
  441. * If the core creation failed, we return its error code.
  442. * Else: if a parameter was created, we return 0
  443. * else we return 1;
  444. */
  445. return core_err < 0 ? core_err : param_err;
  446. }