iforce-ff.c 15 KB

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