iforce-ff.c 15 KB

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