ff-memless.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  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 <linux/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(u16, 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(u16, 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. #define FRAC_N 8
  196. static inline s16 fixp_new16(s16 a)
  197. {
  198. return ((s32)a) >> (16 - FRAC_N);
  199. }
  200. static inline s16 fixp_mult(s16 a, s16 b)
  201. {
  202. a = ((s32)a * 0x100) / 0x7fff;
  203. return ((s32)(a * b)) >> FRAC_N;
  204. }
  205. /*
  206. * Combine two effects and apply gain.
  207. */
  208. static void ml_combine_effects(struct ff_effect *effect,
  209. struct ml_effect_state *state,
  210. int gain)
  211. {
  212. struct ff_effect *new = state->effect;
  213. unsigned int strong, weak, i;
  214. int x, y;
  215. s16 level;
  216. switch (new->type) {
  217. case FF_CONSTANT:
  218. i = new->direction * 360 / 0xffff;
  219. level = fixp_new16(apply_envelope(state,
  220. new->u.constant.level,
  221. &new->u.constant.envelope));
  222. x = fixp_mult(fixp_sin16(i), level) * gain / 0xffff;
  223. y = fixp_mult(-fixp_cos16(i), level) * gain / 0xffff;
  224. /*
  225. * here we abuse ff_ramp to hold x and y of constant force
  226. * If in future any driver wants something else than x and y
  227. * in s8, this should be changed to something more generic
  228. */
  229. effect->u.ramp.start_level =
  230. clamp_val(effect->u.ramp.start_level + x, -0x80, 0x7f);
  231. effect->u.ramp.end_level =
  232. clamp_val(effect->u.ramp.end_level + y, -0x80, 0x7f);
  233. break;
  234. case FF_RUMBLE:
  235. strong = (u32)new->u.rumble.strong_magnitude * gain / 0xffff;
  236. weak = (u32)new->u.rumble.weak_magnitude * gain / 0xffff;
  237. if (effect->u.rumble.strong_magnitude + strong)
  238. effect->direction = ml_calculate_direction(
  239. effect->direction,
  240. effect->u.rumble.strong_magnitude,
  241. new->direction, strong);
  242. else if (effect->u.rumble.weak_magnitude + weak)
  243. effect->direction = ml_calculate_direction(
  244. effect->direction,
  245. effect->u.rumble.weak_magnitude,
  246. new->direction, weak);
  247. else
  248. effect->direction = 0;
  249. effect->u.rumble.strong_magnitude =
  250. min(strong + effect->u.rumble.strong_magnitude,
  251. 0xffffU);
  252. effect->u.rumble.weak_magnitude =
  253. min(weak + effect->u.rumble.weak_magnitude, 0xffffU);
  254. break;
  255. case FF_PERIODIC:
  256. i = apply_envelope(state, abs(new->u.periodic.magnitude),
  257. &new->u.periodic.envelope);
  258. /* here we also scale it 0x7fff => 0xffff */
  259. i = i * gain / 0x7fff;
  260. if (effect->u.rumble.strong_magnitude + i)
  261. effect->direction = ml_calculate_direction(
  262. effect->direction,
  263. effect->u.rumble.strong_magnitude,
  264. new->direction, i);
  265. else
  266. effect->direction = 0;
  267. effect->u.rumble.strong_magnitude =
  268. min(i + effect->u.rumble.strong_magnitude, 0xffffU);
  269. effect->u.rumble.weak_magnitude =
  270. min(i + effect->u.rumble.weak_magnitude, 0xffffU);
  271. break;
  272. default:
  273. pr_err("invalid type in ml_combine_effects()\n");
  274. break;
  275. }
  276. }
  277. /*
  278. * Because memoryless devices have only one effect per effect type active
  279. * at one time we have to combine multiple effects into one
  280. */
  281. static int ml_get_combo_effect(struct ml_device *ml,
  282. unsigned long *effect_handled,
  283. struct ff_effect *combo_effect)
  284. {
  285. struct ff_effect *effect;
  286. struct ml_effect_state *state;
  287. int effect_type;
  288. int i;
  289. memset(combo_effect, 0, sizeof(struct ff_effect));
  290. for (i = 0; i < FF_MEMLESS_EFFECTS; i++) {
  291. if (__test_and_set_bit(i, effect_handled))
  292. continue;
  293. state = &ml->states[i];
  294. effect = state->effect;
  295. if (!test_bit(FF_EFFECT_STARTED, &state->flags))
  296. continue;
  297. if (time_before(jiffies, state->play_at))
  298. continue;
  299. /*
  300. * here we have started effects that are either
  301. * currently playing (and may need be aborted)
  302. * or need to start playing.
  303. */
  304. effect_type = get_compatible_type(ml->dev->ff, effect->type);
  305. if (combo_effect->type != effect_type) {
  306. if (combo_effect->type != 0) {
  307. __clear_bit(i, effect_handled);
  308. continue;
  309. }
  310. combo_effect->type = effect_type;
  311. }
  312. if (__test_and_clear_bit(FF_EFFECT_ABORTING, &state->flags)) {
  313. __clear_bit(FF_EFFECT_PLAYING, &state->flags);
  314. __clear_bit(FF_EFFECT_STARTED, &state->flags);
  315. } else if (effect->replay.length &&
  316. time_after_eq(jiffies, state->stop_at)) {
  317. __clear_bit(FF_EFFECT_PLAYING, &state->flags);
  318. if (--state->count <= 0) {
  319. __clear_bit(FF_EFFECT_STARTED, &state->flags);
  320. } else {
  321. state->play_at = jiffies +
  322. msecs_to_jiffies(effect->replay.delay);
  323. state->stop_at = state->play_at +
  324. msecs_to_jiffies(effect->replay.length);
  325. }
  326. } else {
  327. __set_bit(FF_EFFECT_PLAYING, &state->flags);
  328. state->adj_at = jiffies;
  329. ml_combine_effects(combo_effect, state, ml->gain);
  330. }
  331. }
  332. return combo_effect->type != 0;
  333. }
  334. static void ml_play_effects(struct ml_device *ml)
  335. {
  336. struct ff_effect effect;
  337. DECLARE_BITMAP(handled_bm, FF_MEMLESS_EFFECTS);
  338. memset(handled_bm, 0, sizeof(handled_bm));
  339. while (ml_get_combo_effect(ml, handled_bm, &effect))
  340. ml->play_effect(ml->dev, ml->private, &effect);
  341. ml_schedule_timer(ml);
  342. }
  343. static void ml_effect_timer(unsigned long timer_data)
  344. {
  345. struct input_dev *dev = (struct input_dev *)timer_data;
  346. struct ml_device *ml = dev->ff->private;
  347. unsigned long flags;
  348. pr_debug("timer: updating effects\n");
  349. spin_lock_irqsave(&dev->event_lock, flags);
  350. ml_play_effects(ml);
  351. spin_unlock_irqrestore(&dev->event_lock, flags);
  352. }
  353. /*
  354. * Sets requested gain for FF effects. Called with dev->event_lock held.
  355. */
  356. static void ml_ff_set_gain(struct input_dev *dev, u16 gain)
  357. {
  358. struct ml_device *ml = dev->ff->private;
  359. int i;
  360. ml->gain = gain;
  361. for (i = 0; i < FF_MEMLESS_EFFECTS; i++)
  362. __clear_bit(FF_EFFECT_PLAYING, &ml->states[i].flags);
  363. ml_play_effects(ml);
  364. }
  365. /*
  366. * Start/stop specified FF effect. Called with dev->event_lock held.
  367. */
  368. static int ml_ff_playback(struct input_dev *dev, int effect_id, int value)
  369. {
  370. struct ml_device *ml = dev->ff->private;
  371. struct ml_effect_state *state = &ml->states[effect_id];
  372. if (value > 0) {
  373. pr_debug("initiated play\n");
  374. __set_bit(FF_EFFECT_STARTED, &state->flags);
  375. state->count = value;
  376. state->play_at = jiffies +
  377. msecs_to_jiffies(state->effect->replay.delay);
  378. state->stop_at = state->play_at +
  379. msecs_to_jiffies(state->effect->replay.length);
  380. state->adj_at = state->play_at;
  381. } else {
  382. pr_debug("initiated stop\n");
  383. if (test_bit(FF_EFFECT_PLAYING, &state->flags))
  384. __set_bit(FF_EFFECT_ABORTING, &state->flags);
  385. else
  386. __clear_bit(FF_EFFECT_STARTED, &state->flags);
  387. }
  388. ml_play_effects(ml);
  389. return 0;
  390. }
  391. static int ml_ff_upload(struct input_dev *dev,
  392. struct ff_effect *effect, struct ff_effect *old)
  393. {
  394. struct ml_device *ml = dev->ff->private;
  395. struct ml_effect_state *state = &ml->states[effect->id];
  396. spin_lock_irq(&dev->event_lock);
  397. if (test_bit(FF_EFFECT_STARTED, &state->flags)) {
  398. __clear_bit(FF_EFFECT_PLAYING, &state->flags);
  399. state->play_at = jiffies +
  400. msecs_to_jiffies(state->effect->replay.delay);
  401. state->stop_at = state->play_at +
  402. msecs_to_jiffies(state->effect->replay.length);
  403. state->adj_at = state->play_at;
  404. ml_schedule_timer(ml);
  405. }
  406. spin_unlock_irq(&dev->event_lock);
  407. return 0;
  408. }
  409. static void ml_ff_destroy(struct ff_device *ff)
  410. {
  411. struct ml_device *ml = ff->private;
  412. kfree(ml->private);
  413. }
  414. /**
  415. * input_ff_create_memless() - create memoryless force-feedback device
  416. * @dev: input device supporting force-feedback
  417. * @data: driver-specific data to be passed into @play_effect
  418. * @play_effect: driver-specific method for playing FF effect
  419. */
  420. int input_ff_create_memless(struct input_dev *dev, void *data,
  421. int (*play_effect)(struct input_dev *, void *, struct ff_effect *))
  422. {
  423. struct ml_device *ml;
  424. struct ff_device *ff;
  425. int error;
  426. int i;
  427. ml = kzalloc(sizeof(struct ml_device), GFP_KERNEL);
  428. if (!ml)
  429. return -ENOMEM;
  430. ml->dev = dev;
  431. ml->private = data;
  432. ml->play_effect = play_effect;
  433. ml->gain = 0xffff;
  434. setup_timer(&ml->timer, ml_effect_timer, (unsigned long)dev);
  435. set_bit(FF_GAIN, dev->ffbit);
  436. error = input_ff_create(dev, FF_MEMLESS_EFFECTS);
  437. if (error) {
  438. kfree(ml);
  439. return error;
  440. }
  441. ff = dev->ff;
  442. ff->private = ml;
  443. ff->upload = ml_ff_upload;
  444. ff->playback = ml_ff_playback;
  445. ff->set_gain = ml_ff_set_gain;
  446. ff->destroy = ml_ff_destroy;
  447. /* we can emulate periodic effects with RUMBLE */
  448. if (test_bit(FF_RUMBLE, ff->ffbit)) {
  449. set_bit(FF_PERIODIC, dev->ffbit);
  450. set_bit(FF_SINE, dev->ffbit);
  451. set_bit(FF_TRIANGLE, dev->ffbit);
  452. set_bit(FF_SQUARE, dev->ffbit);
  453. }
  454. for (i = 0; i < FF_MEMLESS_EFFECTS; i++)
  455. ml->states[i].effect = &ff->effects[i];
  456. return 0;
  457. }
  458. EXPORT_SYMBOL_GPL(input_ff_create_memless);