/kern_oII/drivers/input/ff-memless.c

http://omnia2droid.googlecode.com/ · C · 516 lines · 355 code · 101 blank · 60 comment · 49 complexity · afca54b042dc8e35e810b2e363eda60e MD5 · raw file

  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 debug(format, arg...) pr_debug("ff-memless: " format "\n", ## arg)
  24. #include <linux/input.h>
  25. #include <linux/module.h>
  26. #include <linux/mutex.h>
  27. #include <linux/spinlock.h>
  28. #include <linux/jiffies.h>
  29. #include "fixp-arith.h"
  30. MODULE_LICENSE("GPL");
  31. MODULE_AUTHOR("Anssi Hannula <anssi.hannula@gmail.com>");
  32. MODULE_DESCRIPTION("Force feedback support for memoryless devices");
  33. /* Number of effects handled with memoryless devices */
  34. #define FF_MEMLESS_EFFECTS 16
  35. /* Envelope update interval in ms */
  36. #define FF_ENVELOPE_INTERVAL 50
  37. #define FF_EFFECT_STARTED 0
  38. #define FF_EFFECT_PLAYING 1
  39. #define FF_EFFECT_ABORTING 2
  40. struct ml_effect_state {
  41. struct ff_effect *effect;
  42. unsigned long flags; /* effect state (STARTED, PLAYING, etc) */
  43. int count; /* loop count of the effect */
  44. unsigned long play_at; /* start time */
  45. unsigned long stop_at; /* stop time */
  46. unsigned long adj_at; /* last time the effect was sent */
  47. };
  48. struct ml_device {
  49. void *private;
  50. struct ml_effect_state states[FF_MEMLESS_EFFECTS];
  51. int gain;
  52. struct timer_list timer;
  53. spinlock_t timer_lock;
  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. debug("calculating next timer");
  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. debug("no actions");
  124. del_timer(&ml->timer);
  125. } else {
  126. debug("timer set");
  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. debug("value = 0x%x, attack_level = 0x%x", value,
  146. 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. debug("difference = %d", difference);
  161. debug("time_from_level = 0x%x", time_from_level);
  162. debug("time_of_envelope = 0x%x", time_of_envelope);
  163. difference = difference * time_from_level / time_of_envelope;
  164. debug("difference = %d", 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. printk(KERN_ERR
  178. "ff-memless: invalid type in get_compatible_type()\n");
  179. return 0;
  180. }
  181. /*
  182. * Combine two effects and apply gain.
  183. */
  184. static void ml_combine_effects(struct ff_effect *effect,
  185. struct ml_effect_state *state,
  186. unsigned int gain)
  187. {
  188. struct ff_effect *new = state->effect;
  189. unsigned int strong, weak, i;
  190. int x, y;
  191. fixp_t level;
  192. switch (new->type) {
  193. case FF_CONSTANT:
  194. i = new->direction * 360 / 0xffff;
  195. level = fixp_new16(apply_envelope(state,
  196. new->u.constant.level,
  197. &new->u.constant.envelope));
  198. x = fixp_mult(fixp_sin(i), level) * gain / 0xffff;
  199. y = fixp_mult(-fixp_cos(i), level) * gain / 0xffff;
  200. /*
  201. * here we abuse ff_ramp to hold x and y of constant force
  202. * If in future any driver wants something else than x and y
  203. * in s8, this should be changed to something more generic
  204. */
  205. effect->u.ramp.start_level =
  206. clamp_val(effect->u.ramp.start_level + x, -0x80, 0x7f);
  207. effect->u.ramp.end_level =
  208. clamp_val(effect->u.ramp.end_level + y, -0x80, 0x7f);
  209. break;
  210. case FF_RUMBLE:
  211. strong = new->u.rumble.strong_magnitude * gain / 0xffff;
  212. weak = new->u.rumble.weak_magnitude * gain / 0xffff;
  213. effect->u.rumble.strong_magnitude =
  214. min(strong + effect->u.rumble.strong_magnitude,
  215. 0xffffU);
  216. effect->u.rumble.weak_magnitude =
  217. min(weak + effect->u.rumble.weak_magnitude, 0xffffU);
  218. break;
  219. case FF_PERIODIC:
  220. i = apply_envelope(state, abs(new->u.periodic.magnitude),
  221. &new->u.periodic.envelope);
  222. /* here we also scale it 0x7fff => 0xffff */
  223. i = i * gain / 0x7fff;
  224. effect->u.rumble.strong_magnitude =
  225. min(i + effect->u.rumble.strong_magnitude, 0xffffU);
  226. effect->u.rumble.weak_magnitude =
  227. min(i + effect->u.rumble.weak_magnitude, 0xffffU);
  228. break;
  229. default:
  230. printk(KERN_ERR "ff-memless: invalid type in ml_combine_effects()\n");
  231. break;
  232. }
  233. }
  234. /*
  235. * Because memoryless devices have only one effect per effect type active
  236. * at one time we have to combine multiple effects into one
  237. */
  238. static int ml_get_combo_effect(struct ml_device *ml,
  239. unsigned long *effect_handled,
  240. struct ff_effect *combo_effect)
  241. {
  242. struct ff_effect *effect;
  243. struct ml_effect_state *state;
  244. int effect_type;
  245. int i;
  246. memset(combo_effect, 0, sizeof(struct ff_effect));
  247. for (i = 0; i < FF_MEMLESS_EFFECTS; i++) {
  248. if (__test_and_set_bit(i, effect_handled))
  249. continue;
  250. state = &ml->states[i];
  251. effect = state->effect;
  252. if (!test_bit(FF_EFFECT_STARTED, &state->flags))
  253. continue;
  254. if (time_before(jiffies, state->play_at))
  255. continue;
  256. /*
  257. * here we have started effects that are either
  258. * currently playing (and may need be aborted)
  259. * or need to start playing.
  260. */
  261. effect_type = get_compatible_type(ml->dev->ff, effect->type);
  262. if (combo_effect->type != effect_type) {
  263. if (combo_effect->type != 0) {
  264. __clear_bit(i, effect_handled);
  265. continue;
  266. }
  267. combo_effect->type = effect_type;
  268. }
  269. if (__test_and_clear_bit(FF_EFFECT_ABORTING, &state->flags)) {
  270. __clear_bit(FF_EFFECT_PLAYING, &state->flags);
  271. __clear_bit(FF_EFFECT_STARTED, &state->flags);
  272. } else if (effect->replay.length &&
  273. time_after_eq(jiffies, state->stop_at)) {
  274. __clear_bit(FF_EFFECT_PLAYING, &state->flags);
  275. if (--state->count <= 0) {
  276. __clear_bit(FF_EFFECT_STARTED, &state->flags);
  277. } else {
  278. state->play_at = jiffies +
  279. msecs_to_jiffies(effect->replay.delay);
  280. state->stop_at = state->play_at +
  281. msecs_to_jiffies(effect->replay.length);
  282. }
  283. } else {
  284. __set_bit(FF_EFFECT_PLAYING, &state->flags);
  285. state->adj_at = jiffies;
  286. ml_combine_effects(combo_effect, state, ml->gain);
  287. }
  288. }
  289. return combo_effect->type != 0;
  290. }
  291. static void ml_play_effects(struct ml_device *ml)
  292. {
  293. struct ff_effect effect;
  294. DECLARE_BITMAP(handled_bm, FF_MEMLESS_EFFECTS);
  295. memset(handled_bm, 0, sizeof(handled_bm));
  296. while (ml_get_combo_effect(ml, handled_bm, &effect))
  297. ml->play_effect(ml->dev, ml->private, &effect);
  298. ml_schedule_timer(ml);
  299. }
  300. static void ml_effect_timer(unsigned long timer_data)
  301. {
  302. struct input_dev *dev = (struct input_dev *)timer_data;
  303. struct ml_device *ml = dev->ff->private;
  304. debug("timer: updating effects");
  305. spin_lock(&ml->timer_lock);
  306. ml_play_effects(ml);
  307. spin_unlock(&ml->timer_lock);
  308. }
  309. static void ml_ff_set_gain(struct input_dev *dev, u16 gain)
  310. {
  311. struct ml_device *ml = dev->ff->private;
  312. int i;
  313. spin_lock_bh(&ml->timer_lock);
  314. ml->gain = gain;
  315. for (i = 0; i < FF_MEMLESS_EFFECTS; i++)
  316. __clear_bit(FF_EFFECT_PLAYING, &ml->states[i].flags);
  317. ml_play_effects(ml);
  318. spin_unlock_bh(&ml->timer_lock);
  319. }
  320. static int ml_ff_playback(struct input_dev *dev, int effect_id, int value)
  321. {
  322. struct ml_device *ml = dev->ff->private;
  323. struct ml_effect_state *state = &ml->states[effect_id];
  324. unsigned long flags;
  325. spin_lock_irqsave(&ml->timer_lock, flags);
  326. if (value > 0) {
  327. debug("initiated play");
  328. __set_bit(FF_EFFECT_STARTED, &state->flags);
  329. state->count = value;
  330. state->play_at = jiffies +
  331. msecs_to_jiffies(state->effect->replay.delay);
  332. state->stop_at = state->play_at +
  333. msecs_to_jiffies(state->effect->replay.length);
  334. state->adj_at = state->play_at;
  335. ml_schedule_timer(ml);
  336. } else {
  337. debug("initiated stop");
  338. if (test_bit(FF_EFFECT_PLAYING, &state->flags))
  339. __set_bit(FF_EFFECT_ABORTING, &state->flags);
  340. else
  341. __clear_bit(FF_EFFECT_STARTED, &state->flags);
  342. ml_play_effects(ml);
  343. }
  344. spin_unlock_irqrestore(&ml->timer_lock, flags);
  345. return 0;
  346. }
  347. static int ml_ff_upload(struct input_dev *dev,
  348. struct ff_effect *effect, struct ff_effect *old)
  349. {
  350. struct ml_device *ml = dev->ff->private;
  351. struct ml_effect_state *state = &ml->states[effect->id];
  352. spin_lock_bh(&ml->timer_lock);
  353. if (test_bit(FF_EFFECT_STARTED, &state->flags)) {
  354. __clear_bit(FF_EFFECT_PLAYING, &state->flags);
  355. state->play_at = jiffies +
  356. msecs_to_jiffies(state->effect->replay.delay);
  357. state->stop_at = state->play_at +
  358. msecs_to_jiffies(state->effect->replay.length);
  359. state->adj_at = state->play_at;
  360. ml_schedule_timer(ml);
  361. }
  362. spin_unlock_bh(&ml->timer_lock);
  363. return 0;
  364. }
  365. static void ml_ff_destroy(struct ff_device *ff)
  366. {
  367. struct ml_device *ml = ff->private;
  368. kfree(ml->private);
  369. }
  370. /**
  371. * input_ff_create_memless() - create memoryless force-feedback device
  372. * @dev: input device supporting force-feedback
  373. * @data: driver-specific data to be passed into @play_effect
  374. * @play_effect: driver-specific method for playing FF effect
  375. */
  376. int input_ff_create_memless(struct input_dev *dev, void *data,
  377. int (*play_effect)(struct input_dev *, void *, struct ff_effect *))
  378. {
  379. struct ml_device *ml;
  380. struct ff_device *ff;
  381. int error;
  382. int i;
  383. ml = kzalloc(sizeof(struct ml_device), GFP_KERNEL);
  384. if (!ml)
  385. return -ENOMEM;
  386. ml->dev = dev;
  387. ml->private = data;
  388. ml->play_effect = play_effect;
  389. ml->gain = 0xffff;
  390. spin_lock_init(&ml->timer_lock);
  391. setup_timer(&ml->timer, ml_effect_timer, (unsigned long)dev);
  392. set_bit(FF_GAIN, dev->ffbit);
  393. error = input_ff_create(dev, FF_MEMLESS_EFFECTS);
  394. if (error) {
  395. kfree(ml);
  396. return error;
  397. }
  398. ff = dev->ff;
  399. ff->private = ml;
  400. ff->upload = ml_ff_upload;
  401. ff->playback = ml_ff_playback;
  402. ff->set_gain = ml_ff_set_gain;
  403. ff->destroy = ml_ff_destroy;
  404. /* we can emulate periodic effects with RUMBLE */
  405. if (test_bit(FF_RUMBLE, ff->ffbit)) {
  406. set_bit(FF_PERIODIC, dev->ffbit);
  407. set_bit(FF_SINE, dev->ffbit);
  408. set_bit(FF_TRIANGLE, dev->ffbit);
  409. set_bit(FF_SQUARE, dev->ffbit);
  410. }
  411. for (i = 0; i < FF_MEMLESS_EFFECTS; i++)
  412. ml->states[i].effect = &ff->effects[i];
  413. return 0;
  414. }
  415. EXPORT_SYMBOL_GPL(input_ff_create_memless);