26 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
28 #include <linux/slab.h>
29 #include <linux/input.h>
30 #include <linux/module.h>
41 #define FF_MEMLESS_EFFECTS 16
44 #define FF_ENVELOPE_INTERVAL 50
46 #define FF_EFFECT_STARTED 0
47 #define FF_EFFECT_PLAYING 1
48 #define FF_EFFECT_ABORTING 2
64 struct input_dev *
dev;
74 switch (effect->
type) {
82 return &empty_envelope;
92 unsigned long attack_stop, fade_start, next_fade;
102 if (state->
effect->replay.length) {
112 next_fade = state->
adj_at +
124 static void ml_schedule_timer(
struct ml_device *ml)
128 unsigned long earliest = 0;
129 unsigned long next_at;
133 pr_debug(
"calculating next timer\n");
143 next_at = calculate_next_time(state);
170 int time_of_envelope;
177 pr_debug(
"value = 0x%x, attack_level = 0x%x\n",
193 difference =
abs(value) - envelope_level;
195 pr_debug(
"difference = %d\n", difference);
196 pr_debug(
"time_from_level = 0x%x\n", time_from_level);
197 pr_debug(
"time_of_envelope = 0x%x\n", time_of_envelope);
199 difference = difference * time_from_level / time_of_envelope;
201 pr_debug(
"difference = %d\n", difference);
204 -(difference + envelope_level) : (difference + envelope_level);
210 static int get_compatible_type(
struct ff_device *ff,
int effect_type)
213 if (
test_bit(effect_type, ff->ffbit))
219 pr_err(
"invalid type in get_compatible_type()\n");
229 u16 new_direction,
u16 new_force)
232 return new_direction;
235 return (((
u32)(direction >> 1) * force +
236 (new_direction >> 1) * new_force) /
237 (force + new_force)) << 1;
243 static void ml_combine_effects(
struct ff_effect *effect,
248 unsigned int strong, weak,
i;
254 i =
new->direction * 360 / 0xffff;
255 level = fixp_new16(apply_envelope(state,
256 new->u.constant.level,
257 &new->u.constant.envelope));
258 x = fixp_mult(fixp_sin(i), level) * gain / 0xffff;
259 y = fixp_mult(-fixp_cos(i), level) * gain / 0xffff;
265 effect->
u.
ramp.start_level =
267 effect->
u.
ramp.end_level =
272 strong = (
u32)new->u.rumble.strong_magnitude * gain / 0xffff;
273 weak = (
u32)
new->u.rumble.weak_magnitude * gain / 0xffff;
275 if (effect->
u.
rumble.strong_magnitude + strong)
276 effect->
direction = ml_calculate_direction(
278 effect->
u.
rumble.strong_magnitude,
279 new->direction, strong);
280 else if (effect->
u.
rumble.weak_magnitude + weak)
281 effect->
direction = ml_calculate_direction(
283 effect->
u.
rumble.weak_magnitude,
284 new->direction, weak);
287 effect->
u.
rumble.strong_magnitude =
288 min(strong + effect->
u.
rumble.strong_magnitude,
290 effect->
u.
rumble.weak_magnitude =
291 min(weak + effect->
u.
rumble.weak_magnitude, 0xffffU);
295 i = apply_envelope(state,
abs(new->u.periodic.magnitude),
296 &new->u.periodic.envelope);
299 i = i * gain / 0x7fff;
301 if (effect->
u.
rumble.strong_magnitude + i)
302 effect->
direction = ml_calculate_direction(
304 effect->
u.
rumble.strong_magnitude,
308 effect->
u.
rumble.strong_magnitude =
309 min(i + effect->
u.
rumble.strong_magnitude, 0xffffU);
310 effect->
u.
rumble.weak_magnitude =
311 min(i + effect->
u.
rumble.weak_magnitude, 0xffffU);
315 pr_err(
"invalid type in ml_combine_effects()\n");
326 static int ml_get_combo_effect(
struct ml_device *ml,
327 unsigned long *effect_handled,
355 effect_type = get_compatible_type(ml->
dev->ff, effect->
type);
356 if (combo_effect->
type != effect_type) {
357 if (combo_effect->
type != 0) {
361 combo_effect->
type = effect_type;
367 }
else if (effect->
replay.length &&
372 if (--state->
count <= 0) {
383 ml_combine_effects(combo_effect, state, ml->
gain);
387 return combo_effect->
type != 0;
390 static void ml_play_effects(
struct ml_device *ml)
395 memset(handled_bm, 0,
sizeof(handled_bm));
397 while (ml_get_combo_effect(ml, handled_bm, &effect))
400 ml_schedule_timer(ml);
403 static void ml_effect_timer(
unsigned long timer_data)
405 struct input_dev *
dev = (
struct input_dev *)timer_data;
409 pr_debug(
"timer: updating effects\n");
413 spin_unlock_irqrestore(&dev->event_lock, flags);
419 static void ml_ff_set_gain(
struct input_dev *
dev,
u16 gain)
435 static int ml_ff_playback(
struct input_dev *dev,
int effect_id,
int value)
465 static int ml_ff_upload(
struct input_dev *dev,
471 spin_lock_irq(&dev->event_lock);
480 ml_schedule_timer(ml);
483 spin_unlock_irq(&dev->event_lock);
488 static void ml_ff_destroy(
struct ff_device *ff)
505 struct ff_device *ff;
521 error = input_ff_create(dev, FF_MEMLESS_EFFECTS);
529 ff->upload = ml_ff_upload;
530 ff->playback = ml_ff_playback;
531 ff->set_gain = ml_ff_set_gain;
532 ff->destroy = ml_ff_destroy;
543 ml->
states[i].effect = &ff->effects[i];