iforce-ff.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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 iforce *iforce,
  162. struct ff_effect *old,
  163. struct ff_effect *new)
  164. {
  165. int ret = 0;
  166. int i;
  167. if (new->type != FF_SPRING && new->type != FF_FRICTION) {
  168. dev_warn(&iforce->dev->dev, "bad effect type in %s\n",
  169. __func__);
  170. return 0;
  171. }
  172. for (i = 0; i < 2; i++) {
  173. ret |= old->u.condition[i].right_saturation != new->u.condition[i].right_saturation
  174. || old->u.condition[i].left_saturation != new->u.condition[i].left_saturation
  175. || old->u.condition[i].right_coeff != new->u.condition[i].right_coeff
  176. || old->u.condition[i].left_coeff != new->u.condition[i].left_coeff
  177. || old->u.condition[i].deadband != new->u.condition[i].deadband
  178. || old->u.condition[i].center != new->u.condition[i].center;
  179. }
  180. return ret;
  181. }
  182. /*
  183. * Analyse the changes in an effect, and tell if we need to send a magnitude
  184. * parameter packet
  185. */
  186. static int need_magnitude_modifier(struct iforce *iforce,
  187. struct ff_effect *old,
  188. struct ff_effect *effect)
  189. {
  190. if (effect->type != FF_CONSTANT) {
  191. dev_warn(&iforce->dev->dev, "bad effect type in %s\n",
  192. __func__);
  193. return 0;
  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 *old,
  202. struct ff_effect *effect)
  203. {
  204. switch (effect->type) {
  205. case FF_CONSTANT:
  206. if (old->u.constant.envelope.attack_length != effect->u.constant.envelope.attack_length
  207. || old->u.constant.envelope.attack_level != effect->u.constant.envelope.attack_level
  208. || old->u.constant.envelope.fade_length != effect->u.constant.envelope.fade_length
  209. || old->u.constant.envelope.fade_level != effect->u.constant.envelope.fade_level)
  210. return 1;
  211. break;
  212. case FF_PERIODIC:
  213. if (old->u.periodic.envelope.attack_length != effect->u.periodic.envelope.attack_length
  214. || old->u.periodic.envelope.attack_level != effect->u.periodic.envelope.attack_level
  215. || old->u.periodic.envelope.fade_length != effect->u.periodic.envelope.fade_length
  216. || old->u.periodic.envelope.fade_level != effect->u.periodic.envelope.fade_level)
  217. return 1;
  218. break;
  219. default:
  220. dev_warn(&iforce->dev->dev, "bad effect type in %s\n",
  221. __func__);
  222. }
  223. return 0;
  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 *old,
  230. struct ff_effect *new)
  231. {
  232. if (new->type != FF_PERIODIC) {
  233. dev_warn(&iforce->dev->dev, "bad effect type in %s\n",
  234. __func__);
  235. return 0;
  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 ff_effect *old, struct ff_effect *new)
  247. {
  248. if (old->direction != new->direction
  249. || old->trigger.button != new->trigger.button
  250. || old->trigger.interval != new->trigger.interval
  251. || old->replay.length != new->replay.length
  252. || old->replay.delay != new->replay.delay)
  253. return 1;
  254. return 0;
  255. }
  256. /*
  257. * Send the part common to all effects to the device
  258. */
  259. static int make_core(struct iforce* iforce, u16 id, u16 mod_id1, u16 mod_id2,
  260. u8 effect_type, u8 axes, u16 duration, u16 delay, u16 button,
  261. u16 interval, u16 direction)
  262. {
  263. unsigned char data[14];
  264. duration = TIME_SCALE(duration);
  265. delay = TIME_SCALE(delay);
  266. interval = TIME_SCALE(interval);
  267. data[0] = LO(id);
  268. data[1] = effect_type;
  269. data[2] = LO(axes) | find_button(iforce, button);
  270. data[3] = LO(duration);
  271. data[4] = HI(duration);
  272. data[5] = HI(direction);
  273. data[6] = LO(interval);
  274. data[7] = HI(interval);
  275. data[8] = LO(mod_id1);
  276. data[9] = HI(mod_id1);
  277. data[10] = LO(mod_id2);
  278. data[11] = HI(mod_id2);
  279. data[12] = LO(delay);
  280. data[13] = HI(delay);
  281. /* Stop effect */
  282. /* iforce_control_playback(iforce, id, 0);*/
  283. iforce_send_packet(iforce, FF_CMD_EFFECT, data);
  284. /* If needed, restart effect */
  285. if (test_bit(FF_CORE_SHOULD_PLAY, iforce->core_effects[id].flags)) {
  286. /* BUG: perhaps we should replay n times, instead of 1. But we do not know n */
  287. iforce_control_playback(iforce, id, 1);
  288. }
  289. return 0;
  290. }
  291. /*
  292. * Upload a periodic effect to the device
  293. * See also iforce_upload_constant.
  294. */
  295. int iforce_upload_periodic(struct iforce *iforce, struct ff_effect *effect, struct ff_effect *old)
  296. {
  297. u8 wave_code;
  298. int core_id = effect->id;
  299. struct iforce_core_effect* core_effect = iforce->core_effects + core_id;
  300. struct resource* mod1_chunk = &(iforce->core_effects[core_id].mod1_chunk);
  301. struct resource* mod2_chunk = &(iforce->core_effects[core_id].mod2_chunk);
  302. int param1_err = 1;
  303. int param2_err = 1;
  304. int core_err = 0;
  305. if (!old || need_period_modifier(iforce, old, effect)) {
  306. param1_err = make_period_modifier(iforce, mod1_chunk,
  307. old != NULL,
  308. effect->u.periodic.magnitude, effect->u.periodic.offset,
  309. effect->u.periodic.period, effect->u.periodic.phase);
  310. if (param1_err)
  311. return param1_err;
  312. set_bit(FF_MOD1_IS_USED, core_effect->flags);
  313. }
  314. if (!old || need_envelope_modifier(iforce, old, effect)) {
  315. param2_err = make_envelope_modifier(iforce, mod2_chunk,
  316. old !=NULL,
  317. effect->u.periodic.envelope.attack_length,
  318. effect->u.periodic.envelope.attack_level,
  319. effect->u.periodic.envelope.fade_length,
  320. effect->u.periodic.envelope.fade_level);
  321. if (param2_err)
  322. 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 (!old || need_core(old, 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, struct ff_effect *old)
  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 (!old || need_magnitude_modifier(iforce, old, effect)) {
  370. param1_err = make_magnitude_modifier(iforce, mod1_chunk,
  371. old != NULL,
  372. effect->u.constant.level);
  373. if (param1_err)
  374. return param1_err;
  375. set_bit(FF_MOD1_IS_USED, core_effect->flags);
  376. }
  377. if (!old || need_envelope_modifier(iforce, old, effect)) {
  378. param2_err = make_envelope_modifier(iforce, mod2_chunk,
  379. old != NULL,
  380. effect->u.constant.envelope.attack_length,
  381. effect->u.constant.envelope.attack_level,
  382. effect->u.constant.envelope.fade_length,
  383. effect->u.constant.envelope.fade_level);
  384. if (param2_err)
  385. return param2_err;
  386. set_bit(FF_MOD2_IS_USED, core_effect->flags);
  387. }
  388. if (!old || need_core(old, effect)) {
  389. core_err = make_core(iforce, effect->id,
  390. mod1_chunk->start,
  391. mod2_chunk->start,
  392. 0x00,
  393. 0x20,
  394. effect->replay.length,
  395. effect->replay.delay,
  396. effect->trigger.button,
  397. effect->trigger.interval,
  398. effect->direction);
  399. }
  400. /* If one of the parameter creation failed, we already returned an
  401. * error code.
  402. * If the core creation failed, we return its error code.
  403. * Else: if one parameter at least was created, we return 0
  404. * else we return 1;
  405. */
  406. return core_err < 0 ? core_err : (param1_err && param2_err);
  407. }
  408. /*
  409. * Upload an condition effect. Those are for example friction, inertia, springs...
  410. */
  411. int iforce_upload_condition(struct iforce *iforce, struct ff_effect *effect, struct ff_effect *old)
  412. {
  413. int core_id = effect->id;
  414. struct iforce_core_effect* core_effect = iforce->core_effects + core_id;
  415. struct resource* mod1_chunk = &(core_effect->mod1_chunk);
  416. struct resource* mod2_chunk = &(core_effect->mod2_chunk);
  417. u8 type;
  418. int param_err = 1;
  419. int core_err = 0;
  420. switch (effect->type) {
  421. case FF_SPRING: type = 0x40; break;
  422. case FF_DAMPER: type = 0x41; break;
  423. default: return -1;
  424. }
  425. if (!old || need_condition_modifier(iforce, old, effect)) {
  426. param_err = make_condition_modifier(iforce, mod1_chunk,
  427. old != NULL,
  428. effect->u.condition[0].right_saturation,
  429. effect->u.condition[0].left_saturation,
  430. effect->u.condition[0].right_coeff,
  431. effect->u.condition[0].left_coeff,
  432. effect->u.condition[0].deadband,
  433. effect->u.condition[0].center);
  434. if (param_err)
  435. return param_err;
  436. set_bit(FF_MOD1_IS_USED, core_effect->flags);
  437. param_err = make_condition_modifier(iforce, mod2_chunk,
  438. old != NULL,
  439. effect->u.condition[1].right_saturation,
  440. effect->u.condition[1].left_saturation,
  441. effect->u.condition[1].right_coeff,
  442. effect->u.condition[1].left_coeff,
  443. effect->u.condition[1].deadband,
  444. effect->u.condition[1].center);
  445. if (param_err)
  446. return param_err;
  447. set_bit(FF_MOD2_IS_USED, core_effect->flags);
  448. }
  449. if (!old || need_core(old, effect)) {
  450. core_err = make_core(iforce, effect->id,
  451. mod1_chunk->start, mod2_chunk->start,
  452. type, 0xc0,
  453. effect->replay.length, effect->replay.delay,
  454. effect->trigger.button, effect->trigger.interval,
  455. effect->direction);
  456. }
  457. /* If the parameter creation failed, we already returned an
  458. * error code.
  459. * If the core creation failed, we return its error code.
  460. * Else: if a parameter was created, we return 0
  461. * else we return 1;
  462. */
  463. return core_err < 0 ? core_err : param_err;
  464. }