iforce-ff.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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. mutex_lock(&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. mutex_unlock(&iforce->mem_mutex);
  45. return -ENOSPC;
  46. }
  47. mutex_unlock(&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. mutex_lock(&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. mutex_unlock(&iforce->mem_mutex);
  71. return -ENOSPC;
  72. }
  73. mutex_unlock(&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. mutex_lock(&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. mutex_unlock(&iforce->mem_mutex);
  102. return -ENOSPC;
  103. }
  104. mutex_unlock(&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. mutex_lock(&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. mutex_unlock(&iforce->mem_mutex);
  131. return -ENOSPC;
  132. }
  133. mutex_unlock(&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 ff_effect *old, struct ff_effect *new)
  164. {
  165. int ret = 0;
  166. int i;
  167. if (new->type != FF_SPRING && new->type != FF_FRICTION) {
  168. printk(KERN_WARNING "iforce.c: bad effect type in need_condition_modifier\n");
  169. return 0;
  170. }
  171. for (i = 0; i < 2; i++) {
  172. ret |= old->u.condition[i].right_saturation != new->u.condition[i].right_saturation
  173. || old->u.condition[i].left_saturation != new->u.condition[i].left_saturation
  174. || old->u.condition[i].right_coeff != new->u.condition[i].right_coeff
  175. || old->u.condition[i].left_coeff != new->u.condition[i].left_coeff
  176. || old->u.condition[i].deadband != new->u.condition[i].deadband
  177. || old->u.condition[i].center != new->u.condition[i].center;
  178. }
  179. return ret;
  180. }
  181. /*
  182. * Analyse the changes in an effect, and tell if we need to send a magnitude
  183. * parameter packet
  184. */
  185. static int need_magnitude_modifier(struct ff_effect *old, struct ff_effect *effect)
  186. {
  187. if (effect->type != FF_CONSTANT) {
  188. printk(KERN_WARNING "iforce.c: bad effect type in need_envelope_modifier\n");
  189. return 0;
  190. }
  191. return old->u.constant.level != effect->u.constant.level;
  192. }
  193. /*
  194. * Analyse the changes in an effect, and tell if we need to send an envelope
  195. * parameter packet
  196. */
  197. static int need_envelope_modifier(struct ff_effect *old, struct ff_effect *effect)
  198. {
  199. switch (effect->type) {
  200. case FF_CONSTANT:
  201. if (old->u.constant.envelope.attack_length != effect->u.constant.envelope.attack_length
  202. || old->u.constant.envelope.attack_level != effect->u.constant.envelope.attack_level
  203. || old->u.constant.envelope.fade_length != effect->u.constant.envelope.fade_length
  204. || old->u.constant.envelope.fade_level != effect->u.constant.envelope.fade_level)
  205. return 1;
  206. break;
  207. case FF_PERIODIC:
  208. if (old->u.periodic.envelope.attack_length != effect->u.periodic.envelope.attack_length
  209. || old->u.periodic.envelope.attack_level != effect->u.periodic.envelope.attack_level
  210. || old->u.periodic.envelope.fade_length != effect->u.periodic.envelope.fade_length
  211. || old->u.periodic.envelope.fade_level != effect->u.periodic.envelope.fade_level)
  212. return 1;
  213. break;
  214. default:
  215. printk(KERN_WARNING "iforce.c: bad effect type in need_envelope_modifier\n");
  216. }
  217. return 0;
  218. }
  219. /*
  220. * Analyse the changes in an effect, and tell if we need to send a periodic
  221. * parameter effect
  222. */
  223. static int need_period_modifier(struct ff_effect *old, struct ff_effect *new)
  224. {
  225. if (new->type != FF_PERIODIC) {
  226. printk(KERN_WARNING "iforce.c: bad effect type in need_period_modifier\n");
  227. return 0;
  228. }
  229. return (old->u.periodic.period != new->u.periodic.period
  230. || old->u.periodic.magnitude != new->u.periodic.magnitude
  231. || old->u.periodic.offset != new->u.periodic.offset
  232. || old->u.periodic.phase != new->u.periodic.phase);
  233. }
  234. /*
  235. * Analyse the changes in an effect, and tell if we need to send an effect
  236. * packet
  237. */
  238. static int need_core(struct ff_effect *old, struct ff_effect *new)
  239. {
  240. if (old->direction != new->direction
  241. || old->trigger.button != new->trigger.button
  242. || old->trigger.interval != new->trigger.interval
  243. || old->replay.length != new->replay.length
  244. || old->replay.delay != new->replay.delay)
  245. return 1;
  246. return 0;
  247. }
  248. /*
  249. * Send the part common to all effects to the device
  250. */
  251. static int make_core(struct iforce* iforce, u16 id, u16 mod_id1, u16 mod_id2,
  252. u8 effect_type, u8 axes, u16 duration, u16 delay, u16 button,
  253. u16 interval, u16 direction)
  254. {
  255. unsigned char data[14];
  256. duration = TIME_SCALE(duration);
  257. delay = TIME_SCALE(delay);
  258. interval = TIME_SCALE(interval);
  259. data[0] = LO(id);
  260. data[1] = effect_type;
  261. data[2] = LO(axes) | find_button(iforce, button);
  262. data[3] = LO(duration);
  263. data[4] = HI(duration);
  264. data[5] = HI(direction);
  265. data[6] = LO(interval);
  266. data[7] = HI(interval);
  267. data[8] = LO(mod_id1);
  268. data[9] = HI(mod_id1);
  269. data[10] = LO(mod_id2);
  270. data[11] = HI(mod_id2);
  271. data[12] = LO(delay);
  272. data[13] = HI(delay);
  273. /* Stop effect */
  274. /* iforce_control_playback(iforce, id, 0);*/
  275. iforce_send_packet(iforce, FF_CMD_EFFECT, data);
  276. /* If needed, restart effect */
  277. if (test_bit(FF_CORE_SHOULD_PLAY, iforce->core_effects[id].flags)) {
  278. /* BUG: perhaps we should replay n times, instead of 1. But we do not know n */
  279. iforce_control_playback(iforce, id, 1);
  280. }
  281. return 0;
  282. }
  283. /*
  284. * Upload a periodic effect to the device
  285. * See also iforce_upload_constant.
  286. */
  287. int iforce_upload_periodic(struct iforce *iforce, struct ff_effect *effect, struct ff_effect *old)
  288. {
  289. u8 wave_code;
  290. int core_id = effect->id;
  291. struct iforce_core_effect* core_effect = iforce->core_effects + core_id;
  292. struct resource* mod1_chunk = &(iforce->core_effects[core_id].mod1_chunk);
  293. struct resource* mod2_chunk = &(iforce->core_effects[core_id].mod2_chunk);
  294. int param1_err = 1;
  295. int param2_err = 1;
  296. int core_err = 0;
  297. if (!old || need_period_modifier(old, effect)) {
  298. param1_err = make_period_modifier(iforce, mod1_chunk,
  299. old != NULL,
  300. effect->u.periodic.magnitude, effect->u.periodic.offset,
  301. effect->u.periodic.period, effect->u.periodic.phase);
  302. if (param1_err)
  303. return param1_err;
  304. set_bit(FF_MOD1_IS_USED, core_effect->flags);
  305. }
  306. if (!old || need_envelope_modifier(old, effect)) {
  307. param2_err = make_envelope_modifier(iforce, mod2_chunk,
  308. old !=NULL,
  309. effect->u.periodic.envelope.attack_length,
  310. effect->u.periodic.envelope.attack_level,
  311. effect->u.periodic.envelope.fade_length,
  312. effect->u.periodic.envelope.fade_level);
  313. if (param2_err)
  314. return param2_err;
  315. set_bit(FF_MOD2_IS_USED, core_effect->flags);
  316. }
  317. switch (effect->u.periodic.waveform) {
  318. case FF_SQUARE: wave_code = 0x20; break;
  319. case FF_TRIANGLE: wave_code = 0x21; break;
  320. case FF_SINE: wave_code = 0x22; break;
  321. case FF_SAW_UP: wave_code = 0x23; break;
  322. case FF_SAW_DOWN: wave_code = 0x24; break;
  323. default: wave_code = 0x20; break;
  324. }
  325. if (!old || need_core(old, effect)) {
  326. core_err = make_core(iforce, effect->id,
  327. mod1_chunk->start,
  328. mod2_chunk->start,
  329. wave_code,
  330. 0x20,
  331. effect->replay.length,
  332. effect->replay.delay,
  333. effect->trigger.button,
  334. effect->trigger.interval,
  335. effect->direction);
  336. }
  337. /* If one of the parameter creation failed, we already returned an
  338. * error code.
  339. * If the core creation failed, we return its error code.
  340. * Else: if one parameter at least was created, we return 0
  341. * else we return 1;
  342. */
  343. return core_err < 0 ? core_err : (param1_err && param2_err);
  344. }
  345. /*
  346. * Upload a constant force effect
  347. * Return value:
  348. * <0 Error code
  349. * 0 Ok, effect created or updated
  350. * 1 effect did not change since last upload, and no packet was therefore sent
  351. */
  352. int iforce_upload_constant(struct iforce *iforce, struct ff_effect *effect, struct ff_effect *old)
  353. {
  354. int core_id = effect->id;
  355. struct iforce_core_effect* core_effect = iforce->core_effects + core_id;
  356. struct resource* mod1_chunk = &(iforce->core_effects[core_id].mod1_chunk);
  357. struct resource* mod2_chunk = &(iforce->core_effects[core_id].mod2_chunk);
  358. int param1_err = 1;
  359. int param2_err = 1;
  360. int core_err = 0;
  361. if (!old || need_magnitude_modifier(old, effect)) {
  362. param1_err = make_magnitude_modifier(iforce, mod1_chunk,
  363. old != NULL,
  364. effect->u.constant.level);
  365. if (param1_err)
  366. return param1_err;
  367. set_bit(FF_MOD1_IS_USED, core_effect->flags);
  368. }
  369. if (!old || need_envelope_modifier(old, effect)) {
  370. param2_err = make_envelope_modifier(iforce, mod2_chunk,
  371. old != NULL,
  372. effect->u.constant.envelope.attack_length,
  373. effect->u.constant.envelope.attack_level,
  374. effect->u.constant.envelope.fade_length,
  375. effect->u.constant.envelope.fade_level);
  376. if (param2_err)
  377. return param2_err;
  378. set_bit(FF_MOD2_IS_USED, core_effect->flags);
  379. }
  380. if (!old || need_core(old, effect)) {
  381. core_err = make_core(iforce, effect->id,
  382. mod1_chunk->start,
  383. mod2_chunk->start,
  384. 0x00,
  385. 0x20,
  386. effect->replay.length,
  387. effect->replay.delay,
  388. effect->trigger.button,
  389. effect->trigger.interval,
  390. effect->direction);
  391. }
  392. /* If one of the parameter creation failed, we already returned an
  393. * error code.
  394. * If the core creation failed, we return its error code.
  395. * Else: if one parameter at least was created, we return 0
  396. * else we return 1;
  397. */
  398. return core_err < 0 ? core_err : (param1_err && param2_err);
  399. }
  400. /*
  401. * Upload an condition effect. Those are for example friction, inertia, springs...
  402. */
  403. int iforce_upload_condition(struct iforce *iforce, struct ff_effect *effect, struct ff_effect *old)
  404. {
  405. int core_id = effect->id;
  406. struct iforce_core_effect* core_effect = iforce->core_effects + core_id;
  407. struct resource* mod1_chunk = &(core_effect->mod1_chunk);
  408. struct resource* mod2_chunk = &(core_effect->mod2_chunk);
  409. u8 type;
  410. int param_err = 1;
  411. int core_err = 0;
  412. switch (effect->type) {
  413. case FF_SPRING: type = 0x40; break;
  414. case FF_DAMPER: type = 0x41; break;
  415. default: return -1;
  416. }
  417. if (!old || need_condition_modifier(old, effect)) {
  418. param_err = make_condition_modifier(iforce, mod1_chunk,
  419. old != NULL,
  420. effect->u.condition[0].right_saturation,
  421. effect->u.condition[0].left_saturation,
  422. effect->u.condition[0].right_coeff,
  423. effect->u.condition[0].left_coeff,
  424. effect->u.condition[0].deadband,
  425. effect->u.condition[0].center);
  426. if (param_err)
  427. return param_err;
  428. set_bit(FF_MOD1_IS_USED, core_effect->flags);
  429. param_err = make_condition_modifier(iforce, mod2_chunk,
  430. old != NULL,
  431. effect->u.condition[1].right_saturation,
  432. effect->u.condition[1].left_saturation,
  433. effect->u.condition[1].right_coeff,
  434. effect->u.condition[1].left_coeff,
  435. effect->u.condition[1].deadband,
  436. effect->u.condition[1].center);
  437. if (param_err)
  438. return param_err;
  439. set_bit(FF_MOD2_IS_USED, core_effect->flags);
  440. }
  441. if (!old || need_core(old, effect)) {
  442. core_err = make_core(iforce, effect->id,
  443. mod1_chunk->start, mod2_chunk->start,
  444. type, 0xc0,
  445. effect->replay.length, effect->replay.delay,
  446. effect->trigger.button, effect->trigger.interval,
  447. effect->direction);
  448. }
  449. /* If the parameter creation failed, we already returned an
  450. * error code.
  451. * If the core creation failed, we return its error code.
  452. * Else: if a parameter was created, we return 0
  453. * else we return 1;
  454. */
  455. return core_err < 0 ? core_err : param_err;
  456. }