ff-memless.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. /*
  2. * Force feedback support for memoryless devices
  3. *
  4. * Copyright (c) 2006 Anssi Hannula <anssi.hannula@gmail.com>
  5. * Copyright (c) 2006 Dmitry Torokhov <dtor@mail.ru>
  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. /* #define DEBUG */
  23. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  24. #include <linux/slab.h>
  25. #include <linux/input.h>
  26. #include <linux/module.h>
  27. #include <linux/mutex.h>
  28. #include <linux/spinlock.h>
  29. #include <linux/jiffies.h>
  30. #include "fixp-arith.h"
  31. MODULE_LICENSE("GPL");
  32. MODULE_AUTHOR("Anssi Hannula <anssi.hannula@gmail.com>");
  33. MODULE_DESCRIPTION("Force feedback support for memoryless devices");
  34. /* Number of effects handled with memoryless devices */
  35. #define FF_MEMLESS_EFFECTS 16
  36. /* Envelope update interval in ms */
  37. #define FF_ENVELOPE_INTERVAL 50
  38. #define FF_EFFECT_STARTED 0
  39. #define FF_EFFECT_PLAYING 1
  40. #define FF_EFFECT_ABORTING 2
  41. struct ml_effect_state {
  42. struct ff_effect *effect;
  43. unsigned long flags; /* effect state (STARTED, PLAYING, etc) */
  44. int count; /* loop count of the effect */
  45. unsigned long play_at; /* start time */
  46. unsigned long stop_at; /* stop time */
  47. unsigned long adj_at; /* last time the effect was sent */
  48. };
  49. struct ml_device {
  50. void *private;
  51. struct ml_effect_state states[FF_MEMLESS_EFFECTS];
  52. int gain;
  53. struct timer_list timer;
  54. struct input_dev *dev;
  55. int (*play_effect)(struct input_dev *dev, void *data,
  56. struct ff_effect *effect);
  57. };
  58. static const struct ff_envelope *get_envelope(const struct ff_effect *effect)
  59. {
  60. static const struct ff_envelope empty_envelope;
  61. switch (effect->type) {
  62. case FF_PERIODIC:
  63. return &effect->u.periodic.envelope;
  64. case FF_CONSTANT:
  65. return &effect->u.constant.envelope;
  66. default:
  67. return &empty_envelope;
  68. }
  69. }
  70. /*
  71. * Check for the next time envelope requires an update on memoryless devices
  72. */
  73. static unsigned long calculate_next_time(struct ml_effect_state *state)
  74. {
  75. const struct ff_envelope *envelope = get_envelope(state->effect);
  76. unsigned long attack_stop, fade_start, next_fade;
  77. if (envelope->attack_length) {
  78. attack_stop = state->play_at +
  79. msecs_to_jiffies(envelope->attack_length);
  80. if (time_before(state->adj_at, attack_stop))
  81. return state->adj_at +
  82. msecs_to_jiffies(FF_ENVELOPE_INTERVAL);
  83. }
  84. if (state->effect->replay.length) {
  85. if (envelope->fade_length) {
  86. /* check when fading should start */
  87. fade_start = state->stop_at -
  88. msecs_to_jiffies(envelope->fade_length);
  89. if (time_before(state->adj_at, fade_start))
  90. return fade_start;
  91. /* already fading, advance to next checkpoint */
  92. next_fade = state->adj_at +
  93. msecs_to_jiffies(FF_ENVELOPE_INTERVAL);
  94. if (time_before(next_fade, state->stop_at))
  95. return next_fade;
  96. }
  97. return state->stop_at;
  98. }
  99. return state->play_at;
  100. }
  101. static void ml_schedule_timer(struct ml_device *ml)
  102. {
  103. struct ml_effect_state *state;
  104. unsigned long now = jiffies;
  105. unsigned long earliest = 0;
  106. unsigned long next_at;
  107. int events = 0;
  108. int i;
  109. pr_debug("calculating next timer\n");
  110. for (i = 0; i < FF_MEMLESS_EFFECTS; i++) {
  111. state = &ml->states[i];
  112. if (!test_bit(FF_EFFECT_STARTED, &state->flags))
  113. continue;
  114. if (test_bit(FF_EFFECT_PLAYING, &state->flags))
  115. next_at = calculate_next_time(state);
  116. else
  117. next_at = state->play_at;
  118. if (time_before_eq(now, next_at) &&
  119. (++events == 1 || time_before(next_at, earliest)))
  120. earliest = next_at;
  121. }
  122. if (!events) {
  123. pr_debug("no actions\n");
  124. del_timer(&ml->timer);
  125. } else {
  126. pr_debug("timer set\n");
  127. mod_timer(&ml->timer, earliest);
  128. }
  129. }
  130. /*
  131. * Apply an envelope to a value
  132. */
  133. static int apply_envelope(struct ml_effect_state *state, int value,
  134. struct ff_envelope *envelope)
  135. {
  136. struct ff_effect *effect = state->effect;
  137. unsigned long now = jiffies;
  138. int time_from_level;
  139. int time_of_envelope;
  140. int envelope_level;
  141. int difference;
  142. if (envelope->attack_length &&
  143. time_before(now,
  144. state->play_at + msecs_to_jiffies(envelope->attack_length))) {
  145. pr_debug("value = 0x%x, attack_level = 0x%x\n",
  146. value, envelope->attack_level);
  147. time_from_level = jiffies_to_msecs(now - state->play_at);
  148. time_of_envelope = envelope->attack_length;
  149. envelope_level = min_t(__s16, envelope->attack_level, 0x7fff);
  150. } else if (envelope->fade_length && effect->replay.length &&
  151. time_after(now,
  152. state->stop_at - msecs_to_jiffies(envelope->fade_length)) &&
  153. time_before(now, state->stop_at)) {
  154. time_from_level = jiffies_to_msecs(state->stop_at - now);
  155. time_of_envelope = envelope->fade_length;
  156. envelope_level = min_t(__s16, envelope->fade_level, 0x7fff);
  157. } else
  158. return value;
  159. difference = abs(value) - envelope_level;
  160. pr_debug("difference = %d\n", difference);
  161. pr_debug("time_from_level = 0x%x\n", time_from_level);
  162. pr_debug("time_of_envelope = 0x%x\n", time_of_envelope);
  163. difference = difference * time_from_level / time_of_envelope;
  164. pr_debug("difference = %d\n", difference);
  165. return value < 0 ?
  166. -(difference + envelope_level) : (difference + envelope_level);
  167. }
  168. /*
  169. * Return the type the effect has to be converted into (memless devices)
  170. */
  171. static int get_compatible_type(struct ff_device *ff, int effect_type)
  172. {
  173. if (test_bit(effect_type, ff->ffbit))
  174. return effect_type;
  175. if (effect_type == FF_PERIODIC && test_bit(FF_RUMBLE, ff->ffbit))
  176. return FF_RUMBLE;
  177. pr_err("invalid type in get_compatible_type()\n");
  178. return 0;
  179. }
  180. /*
  181. * Only left/right direction should be used (under/over 0x8000) for
  182. * forward/reverse motor direction (to keep calculation fast & simple).
  183. */
  184. static u16 ml_calculate_direction(u16 direction, u16 force,
  185. u16 new_direction, u16 new_force)
  186. {
  187. if (!force)
  188. return new_direction;
  189. if (!new_force)
  190. return direction;
  191. return (((u32)(direction >> 1) * force +
  192. (new_direction >> 1) * new_force) /
  193. (force + new_force)) << 1;
  194. }
  195. /*
  196. * Combine two effects and apply gain.
  197. */
  198. static void ml_combine_effects(struct ff_effect *effect,
  199. struct ml_effect_state *state,
  200. int gain)
  201. {
  202. struct ff_effect *new = state->effect;
  203. unsigned int strong, weak, i;
  204. int x, y;
  205. fixp_t level;
  206. switch (new->type) {
  207. case FF_CONSTANT:
  208. i = new->direction * 360 / 0xffff;
  209. level = fixp_new16(apply_envelope(state,
  210. new->u.constant.level,
  211. &new->u.constant.envelope));
  212. x = fixp_mult(fixp_sin(i), level) * gain / 0xffff;
  213. y = fixp_mult(-fixp_cos(i), level) * gain / 0xffff;
  214. /*
  215. * here we abuse ff_ramp to hold x and y of constant force
  216. * If in future any driver wants something else than x and y
  217. * in s8, this should be changed to something more generic
  218. */
  219. effect->u.ramp.start_level =
  220. clamp_val(effect->u.ramp.start_level + x, -0x80, 0x7f);
  221. effect->u.ramp.end_level =
  222. clamp_val(effect->u.ramp.end_level + y, -0x80, 0x7f);
  223. break;
  224. case FF_RUMBLE:
  225. strong = (u32)new->u.rumble.strong_magnitude * gain / 0xffff;
  226. weak = (u32)new->u.rumble.weak_magnitude * gain / 0xffff;
  227. if (effect->u.rumble.strong_magnitude + strong)
  228. effect->direction = ml_calculate_direction(
  229. effect->direction,
  230. effect->u.rumble.strong_magnitude,
  231. new->direction, strong);
  232. else if (effect->u.rumble.weak_magnitude + weak)
  233. effect->direction = ml_calculate_direction(
  234. effect->direction,
  235. effect->u.rumble.weak_magnitude,
  236. new->direction, weak);
  237. else
  238. effect->direction = 0;
  239. effect->u.rumble.strong_magnitude =
  240. min(strong + effect->u.rumble.strong_magnitude,
  241. 0xffffU);
  242. effect->u.rumble.weak_magnitude =
  243. min(weak + effect->u.rumble.weak_magnitude, 0xffffU);
  244. break;
  245. case FF_PERIODIC:
  246. i = apply_envelope(state, abs(new->u.periodic.magnitude),
  247. &new->u.periodic.envelope);
  248. /* here we also scale it 0x7fff => 0xffff */
  249. i = i * gain / 0x7fff;
  250. if (effect->u.rumble.strong_magnitude + i)
  251. effect->direction = ml_calculate_direction(
  252. effect->direction,
  253. effect->u.rumble.strong_magnitude,
  254. new->direction, i);
  255. else
  256. effect->direction = 0;
  257. effect->u.rumble.strong_magnitude =
  258. min(i + effect->u.rumble.strong_magnitude, 0xffffU);
  259. effect->u.rumble.weak_magnitude =
  260. min(i + effect->u.rumble.weak_magnitude, 0xffffU);
  261. break;
  262. default:
  263. pr_err("invalid type in ml_combine_effects()\n");
  264. break;
  265. }
  266. }
  267. /*
  268. * Because memoryless devices have only one effect per effect type active
  269. * at one time we have to combine multiple effects into one
  270. */
  271. static int ml_get_combo_effect(struct ml_device *ml,
  272. unsigned long *effect_handled,
  273. struct ff_effect *combo_effect)
  274. {
  275. struct ff_effect *effect;
  276. struct ml_effect_state *state;
  277. int effect_type;
  278. int i;
  279. memset(combo_effect, 0, sizeof(struct ff_effect));
  280. for (i = 0; i < FF_MEMLESS_EFFECTS; i++) {
  281. if (__test_and_set_bit(i, effect_handled))
  282. continue;
  283. state = &ml->states[i];
  284. effect = state->effect;
  285. if (!test_bit(FF_EFFECT_STARTED, &state->flags))
  286. continue;
  287. if (time_before(jiffies, state->play_at))
  288. continue;
  289. /*
  290. * here we have started effects that are either
  291. * currently playing (and may need be aborted)
  292. * or need to start playing.
  293. */
  294. effect_type = get_compatible_type(ml->dev->ff, effect->type);
  295. if (combo_effect->type != effect_type) {
  296. if (combo_effect->type != 0) {
  297. __clear_bit(i, effect_handled);
  298. continue;
  299. }
  300. combo_effect->type = effect_type;
  301. }
  302. if (__test_and_clear_bit(FF_EFFECT_ABORTING, &state->flags)) {
  303. __clear_bit(FF_EFFECT_PLAYING, &state->flags);
  304. __clear_bit(FF_EFFECT_STARTED, &state->flags);
  305. } else if (effect->replay.length &&
  306. time_after_eq(jiffies, state->stop_at)) {
  307. __clear_bit(FF_EFFECT_PLAYING, &state->flags);
  308. if (--state->count <= 0) {
  309. __clear_bit(FF_EFFECT_STARTED, &state->flags);
  310. } else {
  311. state->play_at = jiffies +
  312. msecs_to_jiffies(effect->replay.delay);
  313. state->stop_at = state->play_at +
  314. msecs_to_jiffies(effect->replay.length);
  315. }
  316. } else {
  317. __set_bit(FF_EFFECT_PLAYING, &state->flags);
  318. state->adj_at = jiffies;
  319. ml_combine_effects(combo_effect, state, ml->gain);
  320. }
  321. }
  322. return combo_effect->type != 0;
  323. }
  324. static void ml_play_effects(struct ml_device *ml)
  325. {
  326. struct ff_effect effect;
  327. DECLARE_BITMAP(handled_bm, FF_MEMLESS_EFFECTS);
  328. memset(handled_bm, 0, sizeof(handled_bm));
  329. while (ml_get_combo_effect(ml, handled_bm, &effect))
  330. ml->play_effect(ml->dev, ml->private, &effect);
  331. ml_schedule_timer(ml);
  332. }
  333. static void ml_effect_timer(unsigned long timer_data)
  334. {
  335. struct input_dev *dev = (struct input_dev *)timer_data;
  336. struct ml_device *ml = dev->ff->private;
  337. unsigned long flags;
  338. pr_debug("timer: updating effects\n");
  339. spin_lock_irqsave(&dev->event_lock, flags);
  340. ml_play_effects(ml);
  341. spin_unlock_irqrestore(&dev->event_lock, flags);
  342. }
  343. /*
  344. * Sets requested gain for FF effects. Called with dev->event_lock held.
  345. */
  346. static void ml_ff_set_gain(struct input_dev *dev, u16 gain)
  347. {
  348. struct ml_device *ml = dev->ff->private;
  349. int i;
  350. ml->gain = gain;
  351. for (i = 0; i < FF_MEMLESS_EFFECTS; i++)
  352. __clear_bit(FF_EFFECT_PLAYING, &ml->states[i].flags);
  353. ml_play_effects(ml);
  354. }
  355. /*
  356. * Start/stop specified FF effect. Called with dev->event_lock held.
  357. */
  358. static int ml_ff_playback(struct input_dev *dev, int effect_id, int value)
  359. {
  360. struct ml_device *ml = dev->ff->private;
  361. struct ml_effect_state *state = &ml->states[effect_id];
  362. if (value > 0) {
  363. pr_debug("initiated play\n");
  364. __set_bit(FF_EFFECT_STARTED, &state->flags);
  365. state->count = value;
  366. state->play_at = jiffies +
  367. msecs_to_jiffies(state->effect->replay.delay);
  368. state->stop_at = state->play_at +
  369. msecs_to_jiffies(state->effect->replay.length);
  370. state->adj_at = state->play_at;
  371. } else {
  372. pr_debug("initiated stop\n");
  373. if (test_bit(FF_EFFECT_PLAYING, &state->flags))
  374. __set_bit(FF_EFFECT_ABORTING, &state->flags);
  375. else
  376. __clear_bit(FF_EFFECT_STARTED, &state->flags);
  377. }
  378. ml_play_effects(ml);
  379. return 0;
  380. }
  381. static int ml_ff_upload(struct input_dev *dev,
  382. struct ff_effect *effect, struct ff_effect *old)
  383. {
  384. struct ml_device *ml = dev->ff->private;
  385. struct ml_effect_state *state = &ml->states[effect->id];
  386. spin_lock_irq(&dev->event_lock);
  387. if (test_bit(FF_EFFECT_STARTED, &state->flags)) {
  388. __clear_bit(FF_EFFECT_PLAYING, &state->flags);
  389. state->play_at = jiffies +
  390. msecs_to_jiffies(state->effect->replay.delay);
  391. state->stop_at = state->play_at +
  392. msecs_to_jiffies(state->effect->replay.length);
  393. state->adj_at = state->play_at;
  394. ml_schedule_timer(ml);
  395. }
  396. spin_unlock_irq(&dev->event_lock);
  397. return 0;
  398. }
  399. static void ml_ff_destroy(struct ff_device *ff)
  400. {
  401. struct ml_device *ml = ff->private;
  402. /*
  403. * Even though we stop all playing effects when tearing down
  404. * an input device (via input_device_flush() that calls into
  405. * input_ff_flush() that stops and erases all effects), we
  406. * do not actually stop the timer, and therefore we should
  407. * do it here.
  408. */
  409. del_timer_sync(&ml->timer);
  410. kfree(ml->private);
  411. }
  412. /**
  413. * input_ff_create_memless() - create memoryless force-feedback device
  414. * @dev: input device supporting force-feedback
  415. * @data: driver-specific data to be passed into @play_effect
  416. * @play_effect: driver-specific method for playing FF effect
  417. */
  418. int input_ff_create_memless(struct input_dev *dev, void *data,
  419. int (*play_effect)(struct input_dev *, void *, struct ff_effect *))
  420. {
  421. struct ml_device *ml;
  422. struct ff_device *ff;
  423. int error;
  424. int i;
  425. ml = kzalloc(sizeof(struct ml_device), GFP_KERNEL);
  426. if (!ml)
  427. return -ENOMEM;
  428. ml->dev = dev;
  429. ml->private = data;
  430. ml->play_effect = play_effect;
  431. ml->gain = 0xffff;
  432. setup_timer(&ml->timer, ml_effect_timer, (unsigned long)dev);
  433. set_bit(FF_GAIN, dev->ffbit);
  434. error = input_ff_create(dev, FF_MEMLESS_EFFECTS);
  435. if (error) {
  436. kfree(ml);
  437. return error;
  438. }
  439. ff = dev->ff;
  440. ff->private = ml;
  441. ff->upload = ml_ff_upload;
  442. ff->playback = ml_ff_playback;
  443. ff->set_gain = ml_ff_set_gain;
  444. ff->destroy = ml_ff_destroy;
  445. /* we can emulate periodic effects with RUMBLE */
  446. if (test_bit(FF_RUMBLE, ff->ffbit)) {
  447. set_bit(FF_PERIODIC, dev->ffbit);
  448. set_bit(FF_SINE, dev->ffbit);
  449. set_bit(FF_TRIANGLE, dev->ffbit);
  450. set_bit(FF_SQUARE, dev->ffbit);
  451. }
  452. for (i = 0; i < FF_MEMLESS_EFFECTS; i++)
  453. ml->states[i].effect = &ff->effects[i];
  454. return 0;
  455. }
  456. EXPORT_SYMBOL_GPL(input_ff_create_memless);