iforce-ff.c 15 KB

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