Anssi Hannula | 7d928a2 | 2006-07-19 01:40:30 -0400 | [diff] [blame] | 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 | /* |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 22 | */ |
| 23 | |
| 24 | /* #define DEBUG */ |
| 25 | |
Joe Perches | da0c490 | 2010-11-29 23:33:07 -0800 | [diff] [blame] | 26 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
Anssi Hannula | 7d928a2 | 2006-07-19 01:40:30 -0400 | [diff] [blame] | 27 | |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 28 | #include <linux/slab.h> |
Anssi Hannula | 7d928a2 | 2006-07-19 01:40:30 -0400 | [diff] [blame] | 29 | #include <linux/input.h> |
| 30 | #include <linux/module.h> |
| 31 | #include <linux/mutex.h> |
| 32 | #include <linux/spinlock.h> |
Tim Schmielau | cd354f1 | 2007-02-14 00:33:14 -0800 | [diff] [blame] | 33 | #include <linux/jiffies.h> |
Anssi Hannula | 7d928a2 | 2006-07-19 01:40:30 -0400 | [diff] [blame] | 34 | |
| 35 | #include "fixp-arith.h" |
| 36 | |
| 37 | MODULE_LICENSE("GPL"); |
| 38 | MODULE_AUTHOR("Anssi Hannula <anssi.hannula@gmail.com>"); |
| 39 | MODULE_DESCRIPTION("Force feedback support for memoryless devices"); |
| 40 | |
| 41 | /* Number of effects handled with memoryless devices */ |
| 42 | #define FF_MEMLESS_EFFECTS 16 |
| 43 | |
| 44 | /* Envelope update interval in ms */ |
| 45 | #define FF_ENVELOPE_INTERVAL 50 |
| 46 | |
| 47 | #define FF_EFFECT_STARTED 0 |
| 48 | #define FF_EFFECT_PLAYING 1 |
| 49 | #define FF_EFFECT_ABORTING 2 |
| 50 | |
| 51 | struct ml_effect_state { |
| 52 | struct ff_effect *effect; |
| 53 | unsigned long flags; /* effect state (STARTED, PLAYING, etc) */ |
| 54 | int count; /* loop count of the effect */ |
| 55 | unsigned long play_at; /* start time */ |
| 56 | unsigned long stop_at; /* stop time */ |
| 57 | unsigned long adj_at; /* last time the effect was sent */ |
| 58 | }; |
| 59 | |
| 60 | struct ml_device { |
| 61 | void *private; |
| 62 | struct ml_effect_state states[FF_MEMLESS_EFFECTS]; |
| 63 | int gain; |
| 64 | struct timer_list timer; |
Anssi Hannula | 7d928a2 | 2006-07-19 01:40:30 -0400 | [diff] [blame] | 65 | struct input_dev *dev; |
| 66 | |
| 67 | int (*play_effect)(struct input_dev *dev, void *data, |
| 68 | struct ff_effect *effect); |
| 69 | }; |
| 70 | |
| 71 | static const struct ff_envelope *get_envelope(const struct ff_effect *effect) |
| 72 | { |
| 73 | static const struct ff_envelope empty_envelope; |
| 74 | |
| 75 | switch (effect->type) { |
| 76 | case FF_PERIODIC: |
| 77 | return &effect->u.periodic.envelope; |
| 78 | case FF_CONSTANT: |
| 79 | return &effect->u.constant.envelope; |
| 80 | default: |
| 81 | return &empty_envelope; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | /* |
| 86 | * Check for the next time envelope requires an update on memoryless devices |
| 87 | */ |
| 88 | static unsigned long calculate_next_time(struct ml_effect_state *state) |
| 89 | { |
| 90 | const struct ff_envelope *envelope = get_envelope(state->effect); |
| 91 | unsigned long attack_stop, fade_start, next_fade; |
| 92 | |
| 93 | if (envelope->attack_length) { |
| 94 | attack_stop = state->play_at + |
| 95 | msecs_to_jiffies(envelope->attack_length); |
| 96 | if (time_before(state->adj_at, attack_stop)) |
| 97 | return state->adj_at + |
| 98 | msecs_to_jiffies(FF_ENVELOPE_INTERVAL); |
| 99 | } |
| 100 | |
| 101 | if (state->effect->replay.length) { |
| 102 | if (envelope->fade_length) { |
| 103 | /* check when fading should start */ |
| 104 | fade_start = state->stop_at - |
| 105 | msecs_to_jiffies(envelope->fade_length); |
| 106 | |
| 107 | if (time_before(state->adj_at, fade_start)) |
| 108 | return fade_start; |
| 109 | |
| 110 | /* already fading, advance to next checkpoint */ |
| 111 | next_fade = state->adj_at + |
| 112 | msecs_to_jiffies(FF_ENVELOPE_INTERVAL); |
| 113 | if (time_before(next_fade, state->stop_at)) |
| 114 | return next_fade; |
| 115 | } |
| 116 | |
| 117 | return state->stop_at; |
| 118 | } |
| 119 | |
| 120 | return state->play_at; |
| 121 | } |
| 122 | |
| 123 | static void ml_schedule_timer(struct ml_device *ml) |
| 124 | { |
| 125 | struct ml_effect_state *state; |
| 126 | unsigned long now = jiffies; |
| 127 | unsigned long earliest = 0; |
| 128 | unsigned long next_at; |
| 129 | int events = 0; |
| 130 | int i; |
| 131 | |
Joe Perches | da0c490 | 2010-11-29 23:33:07 -0800 | [diff] [blame] | 132 | pr_debug("calculating next timer\n"); |
Anssi Hannula | 7d928a2 | 2006-07-19 01:40:30 -0400 | [diff] [blame] | 133 | |
| 134 | for (i = 0; i < FF_MEMLESS_EFFECTS; i++) { |
| 135 | |
| 136 | state = &ml->states[i]; |
| 137 | |
| 138 | if (!test_bit(FF_EFFECT_STARTED, &state->flags)) |
| 139 | continue; |
| 140 | |
| 141 | if (test_bit(FF_EFFECT_PLAYING, &state->flags)) |
| 142 | next_at = calculate_next_time(state); |
| 143 | else |
| 144 | next_at = state->play_at; |
| 145 | |
| 146 | if (time_before_eq(now, next_at) && |
| 147 | (++events == 1 || time_before(next_at, earliest))) |
| 148 | earliest = next_at; |
| 149 | } |
| 150 | |
| 151 | if (!events) { |
Joe Perches | da0c490 | 2010-11-29 23:33:07 -0800 | [diff] [blame] | 152 | pr_debug("no actions\n"); |
Anssi Hannula | 7d928a2 | 2006-07-19 01:40:30 -0400 | [diff] [blame] | 153 | del_timer(&ml->timer); |
| 154 | } else { |
Joe Perches | da0c490 | 2010-11-29 23:33:07 -0800 | [diff] [blame] | 155 | pr_debug("timer set\n"); |
Anssi Hannula | 7d928a2 | 2006-07-19 01:40:30 -0400 | [diff] [blame] | 156 | mod_timer(&ml->timer, earliest); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | /* |
| 161 | * Apply an envelope to a value |
| 162 | */ |
| 163 | static int apply_envelope(struct ml_effect_state *state, int value, |
| 164 | struct ff_envelope *envelope) |
| 165 | { |
| 166 | struct ff_effect *effect = state->effect; |
| 167 | unsigned long now = jiffies; |
| 168 | int time_from_level; |
| 169 | int time_of_envelope; |
| 170 | int envelope_level; |
| 171 | int difference; |
| 172 | |
| 173 | if (envelope->attack_length && |
| 174 | time_before(now, |
| 175 | state->play_at + msecs_to_jiffies(envelope->attack_length))) { |
Joe Perches | da0c490 | 2010-11-29 23:33:07 -0800 | [diff] [blame] | 176 | pr_debug("value = 0x%x, attack_level = 0x%x\n", |
| 177 | value, envelope->attack_level); |
Anssi Hannula | 7d928a2 | 2006-07-19 01:40:30 -0400 | [diff] [blame] | 178 | time_from_level = jiffies_to_msecs(now - state->play_at); |
| 179 | time_of_envelope = envelope->attack_length; |
| 180 | envelope_level = min_t(__s16, envelope->attack_level, 0x7fff); |
| 181 | |
| 182 | } else if (envelope->fade_length && effect->replay.length && |
| 183 | time_after(now, |
| 184 | state->stop_at - msecs_to_jiffies(envelope->fade_length)) && |
| 185 | time_before(now, state->stop_at)) { |
| 186 | time_from_level = jiffies_to_msecs(state->stop_at - now); |
| 187 | time_of_envelope = envelope->fade_length; |
| 188 | envelope_level = min_t(__s16, envelope->fade_level, 0x7fff); |
| 189 | } else |
| 190 | return value; |
| 191 | |
| 192 | difference = abs(value) - envelope_level; |
| 193 | |
Joe Perches | da0c490 | 2010-11-29 23:33:07 -0800 | [diff] [blame] | 194 | pr_debug("difference = %d\n", difference); |
| 195 | pr_debug("time_from_level = 0x%x\n", time_from_level); |
| 196 | pr_debug("time_of_envelope = 0x%x\n", time_of_envelope); |
Anssi Hannula | 7d928a2 | 2006-07-19 01:40:30 -0400 | [diff] [blame] | 197 | |
| 198 | difference = difference * time_from_level / time_of_envelope; |
| 199 | |
Joe Perches | da0c490 | 2010-11-29 23:33:07 -0800 | [diff] [blame] | 200 | pr_debug("difference = %d\n", difference); |
Anssi Hannula | 7d928a2 | 2006-07-19 01:40:30 -0400 | [diff] [blame] | 201 | |
| 202 | return value < 0 ? |
| 203 | -(difference + envelope_level) : (difference + envelope_level); |
| 204 | } |
| 205 | |
| 206 | /* |
| 207 | * Return the type the effect has to be converted into (memless devices) |
| 208 | */ |
| 209 | static int get_compatible_type(struct ff_device *ff, int effect_type) |
| 210 | { |
| 211 | |
| 212 | if (test_bit(effect_type, ff->ffbit)) |
| 213 | return effect_type; |
| 214 | |
| 215 | if (effect_type == FF_PERIODIC && test_bit(FF_RUMBLE, ff->ffbit)) |
| 216 | return FF_RUMBLE; |
| 217 | |
Joe Perches | da0c490 | 2010-11-29 23:33:07 -0800 | [diff] [blame] | 218 | pr_err("invalid type in get_compatible_type()\n"); |
Anssi Hannula | 7d928a2 | 2006-07-19 01:40:30 -0400 | [diff] [blame] | 219 | |
| 220 | return 0; |
| 221 | } |
| 222 | |
| 223 | /* |
Jari Vanhala | 94ec26c | 2009-12-24 21:52:20 -0800 | [diff] [blame] | 224 | * Only left/right direction should be used (under/over 0x8000) for |
| 225 | * forward/reverse motor direction (to keep calculation fast & simple). |
| 226 | */ |
| 227 | static u16 ml_calculate_direction(u16 direction, u16 force, |
| 228 | u16 new_direction, u16 new_force) |
| 229 | { |
| 230 | if (!force) |
| 231 | return new_direction; |
| 232 | if (!new_force) |
| 233 | return direction; |
| 234 | return (((u32)(direction >> 1) * force + |
| 235 | (new_direction >> 1) * new_force) / |
| 236 | (force + new_force)) << 1; |
| 237 | } |
| 238 | |
| 239 | /* |
Anssi Hannula | 7d928a2 | 2006-07-19 01:40:30 -0400 | [diff] [blame] | 240 | * Combine two effects and apply gain. |
| 241 | */ |
| 242 | static void ml_combine_effects(struct ff_effect *effect, |
| 243 | struct ml_effect_state *state, |
Dmitry Torokhov | 1b11c88 | 2009-12-24 21:44:19 -0800 | [diff] [blame] | 244 | int gain) |
Anssi Hannula | 7d928a2 | 2006-07-19 01:40:30 -0400 | [diff] [blame] | 245 | { |
| 246 | struct ff_effect *new = state->effect; |
| 247 | unsigned int strong, weak, i; |
| 248 | int x, y; |
| 249 | fixp_t level; |
| 250 | |
| 251 | switch (new->type) { |
| 252 | case FF_CONSTANT: |
| 253 | i = new->direction * 360 / 0xffff; |
| 254 | level = fixp_new16(apply_envelope(state, |
| 255 | new->u.constant.level, |
| 256 | &new->u.constant.envelope)); |
| 257 | x = fixp_mult(fixp_sin(i), level) * gain / 0xffff; |
| 258 | y = fixp_mult(-fixp_cos(i), level) * gain / 0xffff; |
| 259 | /* |
| 260 | * here we abuse ff_ramp to hold x and y of constant force |
| 261 | * If in future any driver wants something else than x and y |
| 262 | * in s8, this should be changed to something more generic |
| 263 | */ |
| 264 | effect->u.ramp.start_level = |
Harvey Harrison | 9231047 | 2008-05-05 11:50:59 -0400 | [diff] [blame] | 265 | clamp_val(effect->u.ramp.start_level + x, -0x80, 0x7f); |
Anssi Hannula | 7d928a2 | 2006-07-19 01:40:30 -0400 | [diff] [blame] | 266 | effect->u.ramp.end_level = |
Harvey Harrison | 9231047 | 2008-05-05 11:50:59 -0400 | [diff] [blame] | 267 | clamp_val(effect->u.ramp.end_level + y, -0x80, 0x7f); |
Anssi Hannula | 7d928a2 | 2006-07-19 01:40:30 -0400 | [diff] [blame] | 268 | break; |
| 269 | |
| 270 | case FF_RUMBLE: |
Dmitry Torokhov | 1b11c88 | 2009-12-24 21:44:19 -0800 | [diff] [blame] | 271 | strong = (u32)new->u.rumble.strong_magnitude * gain / 0xffff; |
| 272 | weak = (u32)new->u.rumble.weak_magnitude * gain / 0xffff; |
Jari Vanhala | 94ec26c | 2009-12-24 21:52:20 -0800 | [diff] [blame] | 273 | |
| 274 | if (effect->u.rumble.strong_magnitude + strong) |
| 275 | effect->direction = ml_calculate_direction( |
| 276 | effect->direction, |
| 277 | effect->u.rumble.strong_magnitude, |
| 278 | new->direction, strong); |
| 279 | else if (effect->u.rumble.weak_magnitude + weak) |
| 280 | effect->direction = ml_calculate_direction( |
| 281 | effect->direction, |
| 282 | effect->u.rumble.weak_magnitude, |
| 283 | new->direction, weak); |
| 284 | else |
| 285 | effect->direction = 0; |
Anssi Hannula | 7d928a2 | 2006-07-19 01:40:30 -0400 | [diff] [blame] | 286 | effect->u.rumble.strong_magnitude = |
| 287 | min(strong + effect->u.rumble.strong_magnitude, |
| 288 | 0xffffU); |
| 289 | effect->u.rumble.weak_magnitude = |
| 290 | min(weak + effect->u.rumble.weak_magnitude, 0xffffU); |
| 291 | break; |
| 292 | |
| 293 | case FF_PERIODIC: |
| 294 | i = apply_envelope(state, abs(new->u.periodic.magnitude), |
| 295 | &new->u.periodic.envelope); |
| 296 | |
| 297 | /* here we also scale it 0x7fff => 0xffff */ |
| 298 | i = i * gain / 0x7fff; |
| 299 | |
Jari Vanhala | 94ec26c | 2009-12-24 21:52:20 -0800 | [diff] [blame] | 300 | if (effect->u.rumble.strong_magnitude + i) |
| 301 | effect->direction = ml_calculate_direction( |
| 302 | effect->direction, |
| 303 | effect->u.rumble.strong_magnitude, |
| 304 | new->direction, i); |
| 305 | else |
| 306 | effect->direction = 0; |
Anssi Hannula | 7d928a2 | 2006-07-19 01:40:30 -0400 | [diff] [blame] | 307 | effect->u.rumble.strong_magnitude = |
| 308 | min(i + effect->u.rumble.strong_magnitude, 0xffffU); |
| 309 | effect->u.rumble.weak_magnitude = |
| 310 | min(i + effect->u.rumble.weak_magnitude, 0xffffU); |
| 311 | break; |
| 312 | |
| 313 | default: |
Joe Perches | da0c490 | 2010-11-29 23:33:07 -0800 | [diff] [blame] | 314 | pr_err("invalid type in ml_combine_effects()\n"); |
Anssi Hannula | 7d928a2 | 2006-07-19 01:40:30 -0400 | [diff] [blame] | 315 | break; |
| 316 | } |
| 317 | |
| 318 | } |
| 319 | |
| 320 | |
| 321 | /* |
| 322 | * Because memoryless devices have only one effect per effect type active |
| 323 | * at one time we have to combine multiple effects into one |
| 324 | */ |
| 325 | static int ml_get_combo_effect(struct ml_device *ml, |
| 326 | unsigned long *effect_handled, |
| 327 | struct ff_effect *combo_effect) |
| 328 | { |
| 329 | struct ff_effect *effect; |
| 330 | struct ml_effect_state *state; |
| 331 | int effect_type; |
| 332 | int i; |
| 333 | |
| 334 | memset(combo_effect, 0, sizeof(struct ff_effect)); |
| 335 | |
| 336 | for (i = 0; i < FF_MEMLESS_EFFECTS; i++) { |
| 337 | if (__test_and_set_bit(i, effect_handled)) |
| 338 | continue; |
| 339 | |
| 340 | state = &ml->states[i]; |
| 341 | effect = state->effect; |
| 342 | |
| 343 | if (!test_bit(FF_EFFECT_STARTED, &state->flags)) |
| 344 | continue; |
| 345 | |
| 346 | if (time_before(jiffies, state->play_at)) |
| 347 | continue; |
| 348 | |
| 349 | /* |
| 350 | * here we have started effects that are either |
| 351 | * currently playing (and may need be aborted) |
| 352 | * or need to start playing. |
| 353 | */ |
| 354 | effect_type = get_compatible_type(ml->dev->ff, effect->type); |
| 355 | if (combo_effect->type != effect_type) { |
| 356 | if (combo_effect->type != 0) { |
| 357 | __clear_bit(i, effect_handled); |
| 358 | continue; |
| 359 | } |
| 360 | combo_effect->type = effect_type; |
| 361 | } |
| 362 | |
| 363 | if (__test_and_clear_bit(FF_EFFECT_ABORTING, &state->flags)) { |
| 364 | __clear_bit(FF_EFFECT_PLAYING, &state->flags); |
| 365 | __clear_bit(FF_EFFECT_STARTED, &state->flags); |
| 366 | } else if (effect->replay.length && |
| 367 | time_after_eq(jiffies, state->stop_at)) { |
| 368 | |
| 369 | __clear_bit(FF_EFFECT_PLAYING, &state->flags); |
| 370 | |
| 371 | if (--state->count <= 0) { |
| 372 | __clear_bit(FF_EFFECT_STARTED, &state->flags); |
| 373 | } else { |
| 374 | state->play_at = jiffies + |
| 375 | msecs_to_jiffies(effect->replay.delay); |
| 376 | state->stop_at = state->play_at + |
| 377 | msecs_to_jiffies(effect->replay.length); |
| 378 | } |
| 379 | } else { |
| 380 | __set_bit(FF_EFFECT_PLAYING, &state->flags); |
| 381 | state->adj_at = jiffies; |
| 382 | ml_combine_effects(combo_effect, state, ml->gain); |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | return combo_effect->type != 0; |
| 387 | } |
| 388 | |
| 389 | static void ml_play_effects(struct ml_device *ml) |
| 390 | { |
| 391 | struct ff_effect effect; |
| 392 | DECLARE_BITMAP(handled_bm, FF_MEMLESS_EFFECTS); |
| 393 | |
| 394 | memset(handled_bm, 0, sizeof(handled_bm)); |
| 395 | |
| 396 | while (ml_get_combo_effect(ml, handled_bm, &effect)) |
| 397 | ml->play_effect(ml->dev, ml->private, &effect); |
| 398 | |
| 399 | ml_schedule_timer(ml); |
| 400 | } |
| 401 | |
| 402 | static void ml_effect_timer(unsigned long timer_data) |
| 403 | { |
| 404 | struct input_dev *dev = (struct input_dev *)timer_data; |
| 405 | struct ml_device *ml = dev->ff->private; |
Dmitry Torokhov | bf3204c | 2009-11-06 21:39:07 -0800 | [diff] [blame] | 406 | unsigned long flags; |
Anssi Hannula | 7d928a2 | 2006-07-19 01:40:30 -0400 | [diff] [blame] | 407 | |
Joe Perches | da0c490 | 2010-11-29 23:33:07 -0800 | [diff] [blame] | 408 | pr_debug("timer: updating effects\n"); |
Anssi Hannula | 7d928a2 | 2006-07-19 01:40:30 -0400 | [diff] [blame] | 409 | |
Dmitry Torokhov | bf3204c | 2009-11-06 21:39:07 -0800 | [diff] [blame] | 410 | spin_lock_irqsave(&dev->event_lock, flags); |
Anssi Hannula | 7d928a2 | 2006-07-19 01:40:30 -0400 | [diff] [blame] | 411 | ml_play_effects(ml); |
Dmitry Torokhov | bf3204c | 2009-11-06 21:39:07 -0800 | [diff] [blame] | 412 | spin_unlock_irqrestore(&dev->event_lock, flags); |
Anssi Hannula | 7d928a2 | 2006-07-19 01:40:30 -0400 | [diff] [blame] | 413 | } |
| 414 | |
Dmitry Torokhov | bf3204c | 2009-11-06 21:39:07 -0800 | [diff] [blame] | 415 | /* |
| 416 | * Sets requested gain for FF effects. Called with dev->event_lock held. |
| 417 | */ |
Anssi Hannula | 7d928a2 | 2006-07-19 01:40:30 -0400 | [diff] [blame] | 418 | static void ml_ff_set_gain(struct input_dev *dev, u16 gain) |
| 419 | { |
| 420 | struct ml_device *ml = dev->ff->private; |
| 421 | int i; |
| 422 | |
Anssi Hannula | 7d928a2 | 2006-07-19 01:40:30 -0400 | [diff] [blame] | 423 | ml->gain = gain; |
| 424 | |
| 425 | for (i = 0; i < FF_MEMLESS_EFFECTS; i++) |
| 426 | __clear_bit(FF_EFFECT_PLAYING, &ml->states[i].flags); |
| 427 | |
| 428 | ml_play_effects(ml); |
Anssi Hannula | 7d928a2 | 2006-07-19 01:40:30 -0400 | [diff] [blame] | 429 | } |
| 430 | |
Dmitry Torokhov | bf3204c | 2009-11-06 21:39:07 -0800 | [diff] [blame] | 431 | /* |
| 432 | * Start/stop specified FF effect. Called with dev->event_lock held. |
| 433 | */ |
Anssi Hannula | 7d928a2 | 2006-07-19 01:40:30 -0400 | [diff] [blame] | 434 | static int ml_ff_playback(struct input_dev *dev, int effect_id, int value) |
| 435 | { |
| 436 | struct ml_device *ml = dev->ff->private; |
| 437 | struct ml_effect_state *state = &ml->states[effect_id]; |
Anssi Hannula | 7d928a2 | 2006-07-19 01:40:30 -0400 | [diff] [blame] | 438 | |
| 439 | if (value > 0) { |
Joe Perches | da0c490 | 2010-11-29 23:33:07 -0800 | [diff] [blame] | 440 | pr_debug("initiated play\n"); |
Anssi Hannula | 7d928a2 | 2006-07-19 01:40:30 -0400 | [diff] [blame] | 441 | |
| 442 | __set_bit(FF_EFFECT_STARTED, &state->flags); |
| 443 | state->count = value; |
| 444 | state->play_at = jiffies + |
| 445 | msecs_to_jiffies(state->effect->replay.delay); |
| 446 | state->stop_at = state->play_at + |
| 447 | msecs_to_jiffies(state->effect->replay.length); |
| 448 | state->adj_at = state->play_at; |
| 449 | |
Anssi Hannula | 7d928a2 | 2006-07-19 01:40:30 -0400 | [diff] [blame] | 450 | } else { |
Joe Perches | da0c490 | 2010-11-29 23:33:07 -0800 | [diff] [blame] | 451 | pr_debug("initiated stop\n"); |
Anssi Hannula | 7d928a2 | 2006-07-19 01:40:30 -0400 | [diff] [blame] | 452 | |
| 453 | if (test_bit(FF_EFFECT_PLAYING, &state->flags)) |
| 454 | __set_bit(FF_EFFECT_ABORTING, &state->flags); |
| 455 | else |
| 456 | __clear_bit(FF_EFFECT_STARTED, &state->flags); |
Anssi Hannula | 7d928a2 | 2006-07-19 01:40:30 -0400 | [diff] [blame] | 457 | } |
| 458 | |
Jari Vanhala | 25ae083 | 2009-12-24 21:52:19 -0800 | [diff] [blame] | 459 | ml_play_effects(ml); |
| 460 | |
Anssi Hannula | 7d928a2 | 2006-07-19 01:40:30 -0400 | [diff] [blame] | 461 | return 0; |
| 462 | } |
| 463 | |
| 464 | static int ml_ff_upload(struct input_dev *dev, |
| 465 | struct ff_effect *effect, struct ff_effect *old) |
| 466 | { |
| 467 | struct ml_device *ml = dev->ff->private; |
| 468 | struct ml_effect_state *state = &ml->states[effect->id]; |
| 469 | |
Dmitry Torokhov | bf3204c | 2009-11-06 21:39:07 -0800 | [diff] [blame] | 470 | spin_lock_irq(&dev->event_lock); |
Anssi Hannula | 7d928a2 | 2006-07-19 01:40:30 -0400 | [diff] [blame] | 471 | |
| 472 | if (test_bit(FF_EFFECT_STARTED, &state->flags)) { |
| 473 | __clear_bit(FF_EFFECT_PLAYING, &state->flags); |
| 474 | state->play_at = jiffies + |
| 475 | msecs_to_jiffies(state->effect->replay.delay); |
| 476 | state->stop_at = state->play_at + |
| 477 | msecs_to_jiffies(state->effect->replay.length); |
| 478 | state->adj_at = state->play_at; |
| 479 | ml_schedule_timer(ml); |
| 480 | } |
| 481 | |
Dmitry Torokhov | bf3204c | 2009-11-06 21:39:07 -0800 | [diff] [blame] | 482 | spin_unlock_irq(&dev->event_lock); |
Anssi Hannula | 7d928a2 | 2006-07-19 01:40:30 -0400 | [diff] [blame] | 483 | |
| 484 | return 0; |
| 485 | } |
| 486 | |
| 487 | static void ml_ff_destroy(struct ff_device *ff) |
| 488 | { |
| 489 | struct ml_device *ml = ff->private; |
| 490 | |
| 491 | kfree(ml->private); |
| 492 | } |
| 493 | |
| 494 | /** |
Randy Dunlap | e4477d2 | 2006-11-24 00:43:09 -0500 | [diff] [blame] | 495 | * input_ff_create_memless() - create memoryless force-feedback device |
Anssi Hannula | 7d928a2 | 2006-07-19 01:40:30 -0400 | [diff] [blame] | 496 | * @dev: input device supporting force-feedback |
| 497 | * @data: driver-specific data to be passed into @play_effect |
| 498 | * @play_effect: driver-specific method for playing FF effect |
| 499 | */ |
| 500 | int input_ff_create_memless(struct input_dev *dev, void *data, |
| 501 | int (*play_effect)(struct input_dev *, void *, struct ff_effect *)) |
| 502 | { |
| 503 | struct ml_device *ml; |
| 504 | struct ff_device *ff; |
| 505 | int error; |
| 506 | int i; |
| 507 | |
| 508 | ml = kzalloc(sizeof(struct ml_device), GFP_KERNEL); |
| 509 | if (!ml) |
| 510 | return -ENOMEM; |
| 511 | |
| 512 | ml->dev = dev; |
| 513 | ml->private = data; |
| 514 | ml->play_effect = play_effect; |
| 515 | ml->gain = 0xffff; |
Anssi Hannula | 7d928a2 | 2006-07-19 01:40:30 -0400 | [diff] [blame] | 516 | setup_timer(&ml->timer, ml_effect_timer, (unsigned long)dev); |
| 517 | |
| 518 | set_bit(FF_GAIN, dev->ffbit); |
| 519 | |
| 520 | error = input_ff_create(dev, FF_MEMLESS_EFFECTS); |
| 521 | if (error) { |
| 522 | kfree(ml); |
| 523 | return error; |
| 524 | } |
| 525 | |
| 526 | ff = dev->ff; |
| 527 | ff->private = ml; |
| 528 | ff->upload = ml_ff_upload; |
| 529 | ff->playback = ml_ff_playback; |
| 530 | ff->set_gain = ml_ff_set_gain; |
| 531 | ff->destroy = ml_ff_destroy; |
| 532 | |
| 533 | /* we can emulate periodic effects with RUMBLE */ |
| 534 | if (test_bit(FF_RUMBLE, ff->ffbit)) { |
| 535 | set_bit(FF_PERIODIC, dev->ffbit); |
| 536 | set_bit(FF_SINE, dev->ffbit); |
| 537 | set_bit(FF_TRIANGLE, dev->ffbit); |
| 538 | set_bit(FF_SQUARE, dev->ffbit); |
| 539 | } |
| 540 | |
| 541 | for (i = 0; i < FF_MEMLESS_EFFECTS; i++) |
| 542 | ml->states[i].effect = &ff->effects[i]; |
| 543 | |
| 544 | return 0; |
| 545 | } |
| 546 | EXPORT_SYMBOL_GPL(input_ff_create_memless); |