2 * Force feedback support for memoryless devices
4 * Copyright (c) 2006 Anssi Hannula <anssi.hannula@gmail.com>
5 * Copyright (c) 2006 Dmitry Torokhov <dtor@mail.ru>
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.
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.
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
26 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
28 #include <linux/slab.h>
29 #include <linux/input.h>
30 #include <linux/module.h>
31 #include <linux/mutex.h>
32 #include <linux/spinlock.h>
33 #include <linux/jiffies.h>
34 #include <linux/fixp-arith.h>
36 MODULE_LICENSE("GPL");
37 MODULE_AUTHOR("Anssi Hannula <anssi.hannula@gmail.com>");
38 MODULE_DESCRIPTION("Force feedback support for memoryless devices");
40 /* Number of effects handled with memoryless devices */
41 #define FF_MEMLESS_EFFECTS 16
43 /* Envelope update interval in ms */
44 #define FF_ENVELOPE_INTERVAL 50
46 #define FF_EFFECT_STARTED 0
47 #define FF_EFFECT_PLAYING 1
48 #define FF_EFFECT_ABORTING 2
50 struct ml_effect_state
{
51 struct ff_effect
*effect
;
52 unsigned long flags
; /* effect state (STARTED, PLAYING, etc) */
53 int count
; /* loop count of the effect */
54 unsigned long play_at
; /* start time */
55 unsigned long stop_at
; /* stop time */
56 unsigned long adj_at
; /* last time the effect was sent */
61 struct ml_effect_state states
[FF_MEMLESS_EFFECTS
];
63 struct timer_list timer
;
64 struct input_dev
*dev
;
66 int (*play_effect
)(struct input_dev
*dev
, void *data
,
67 struct ff_effect
*effect
);
70 static const struct ff_envelope
*get_envelope(const struct ff_effect
*effect
)
72 static const struct ff_envelope empty_envelope
;
74 switch (effect
->type
) {
76 return &effect
->u
.periodic
.envelope
;
79 return &effect
->u
.constant
.envelope
;
82 return &empty_envelope
;
87 * Check for the next time envelope requires an update on memoryless devices
89 static unsigned long calculate_next_time(struct ml_effect_state
*state
)
91 const struct ff_envelope
*envelope
= get_envelope(state
->effect
);
92 unsigned long attack_stop
, fade_start
, next_fade
;
94 if (envelope
->attack_length
) {
95 attack_stop
= state
->play_at
+
96 msecs_to_jiffies(envelope
->attack_length
);
97 if (time_before(state
->adj_at
, attack_stop
))
98 return state
->adj_at
+
99 msecs_to_jiffies(FF_ENVELOPE_INTERVAL
);
102 if (state
->effect
->replay
.length
) {
103 if (envelope
->fade_length
) {
104 /* check when fading should start */
105 fade_start
= state
->stop_at
-
106 msecs_to_jiffies(envelope
->fade_length
);
108 if (time_before(state
->adj_at
, fade_start
))
111 /* already fading, advance to next checkpoint */
112 next_fade
= state
->adj_at
+
113 msecs_to_jiffies(FF_ENVELOPE_INTERVAL
);
114 if (time_before(next_fade
, state
->stop_at
))
118 return state
->stop_at
;
121 return state
->play_at
;
124 static void ml_schedule_timer(struct ml_device
*ml
)
126 struct ml_effect_state
*state
;
127 unsigned long now
= jiffies
;
128 unsigned long earliest
= 0;
129 unsigned long next_at
;
133 pr_debug("calculating next timer\n");
135 for (i
= 0; i
< FF_MEMLESS_EFFECTS
; i
++) {
137 state
= &ml
->states
[i
];
139 if (!test_bit(FF_EFFECT_STARTED
, &state
->flags
))
142 if (test_bit(FF_EFFECT_PLAYING
, &state
->flags
))
143 next_at
= calculate_next_time(state
);
145 next_at
= state
->play_at
;
147 if (time_before_eq(now
, next_at
) &&
148 (++events
== 1 || time_before(next_at
, earliest
)))
153 pr_debug("no actions\n");
154 del_timer(&ml
->timer
);
156 pr_debug("timer set\n");
157 mod_timer(&ml
->timer
, earliest
);
162 * Apply an envelope to a value
164 static int apply_envelope(struct ml_effect_state
*state
, int value
,
165 struct ff_envelope
*envelope
)
167 struct ff_effect
*effect
= state
->effect
;
168 unsigned long now
= jiffies
;
170 int time_of_envelope
;
174 if (envelope
->attack_length
&&
176 state
->play_at
+ msecs_to_jiffies(envelope
->attack_length
))) {
177 pr_debug("value = 0x%x, attack_level = 0x%x\n",
178 value
, envelope
->attack_level
);
179 time_from_level
= jiffies_to_msecs(now
- state
->play_at
);
180 time_of_envelope
= envelope
->attack_length
;
181 envelope_level
= min_t(u16
, envelope
->attack_level
, 0x7fff);
183 } else if (envelope
->fade_length
&& effect
->replay
.length
&&
185 state
->stop_at
- msecs_to_jiffies(envelope
->fade_length
)) &&
186 time_before(now
, state
->stop_at
)) {
187 time_from_level
= jiffies_to_msecs(state
->stop_at
- now
);
188 time_of_envelope
= envelope
->fade_length
;
189 envelope_level
= min_t(u16
, envelope
->fade_level
, 0x7fff);
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
);
208 * Return the type the effect has to be converted into (memless devices)
210 static int get_compatible_type(struct ff_device
*ff
, int effect_type
)
213 if (test_bit(effect_type
, ff
->ffbit
))
216 if (effect_type
== FF_PERIODIC
&& test_bit(FF_RUMBLE
, ff
->ffbit
))
219 pr_err("invalid type in get_compatible_type()\n");
225 * Only left/right direction should be used (under/over 0x8000) for
226 * forward/reverse motor direction (to keep calculation fast & simple).
228 static u16
ml_calculate_direction(u16 direction
, u16 force
,
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;
241 static inline s16
fixp_new16(s16 a
)
243 return ((s32
)a
) >> (16 - FRAC_N
);
246 static inline s16
fixp_mult(s16 a
, s16 b
)
248 a
= ((s32
)a
* 0x100) / 0x7fff;
249 return ((s32
)(a
* b
)) >> FRAC_N
;
253 * Combine two effects and apply gain.
255 static void ml_combine_effects(struct ff_effect
*effect
,
256 struct ml_effect_state
*state
,
259 struct ff_effect
*new = state
->effect
;
260 unsigned int strong
, weak
, i
;
266 i
= new->direction
* 360 / 0xffff;
267 level
= fixp_new16(apply_envelope(state
,
268 new->u
.constant
.level
,
269 &new->u
.constant
.envelope
));
270 x
= fixp_mult(fixp_sin16(i
), level
) * gain
/ 0xffff;
271 y
= fixp_mult(-fixp_cos16(i
), level
) * gain
/ 0xffff;
273 * here we abuse ff_ramp to hold x and y of constant force
274 * If in future any driver wants something else than x and y
275 * in s8, this should be changed to something more generic
277 effect
->u
.ramp
.start_level
=
278 clamp_val(effect
->u
.ramp
.start_level
+ x
, -0x80, 0x7f);
279 effect
->u
.ramp
.end_level
=
280 clamp_val(effect
->u
.ramp
.end_level
+ y
, -0x80, 0x7f);
284 strong
= (u32
)new->u
.rumble
.strong_magnitude
* gain
/ 0xffff;
285 weak
= (u32
)new->u
.rumble
.weak_magnitude
* gain
/ 0xffff;
287 if (effect
->u
.rumble
.strong_magnitude
+ strong
)
288 effect
->direction
= ml_calculate_direction(
290 effect
->u
.rumble
.strong_magnitude
,
291 new->direction
, strong
);
292 else if (effect
->u
.rumble
.weak_magnitude
+ weak
)
293 effect
->direction
= ml_calculate_direction(
295 effect
->u
.rumble
.weak_magnitude
,
296 new->direction
, weak
);
298 effect
->direction
= 0;
299 effect
->u
.rumble
.strong_magnitude
=
300 min(strong
+ effect
->u
.rumble
.strong_magnitude
,
302 effect
->u
.rumble
.weak_magnitude
=
303 min(weak
+ effect
->u
.rumble
.weak_magnitude
, 0xffffU
);
307 i
= apply_envelope(state
, abs(new->u
.periodic
.magnitude
),
308 &new->u
.periodic
.envelope
);
310 /* here we also scale it 0x7fff => 0xffff */
311 i
= i
* gain
/ 0x7fff;
313 if (effect
->u
.rumble
.strong_magnitude
+ i
)
314 effect
->direction
= ml_calculate_direction(
316 effect
->u
.rumble
.strong_magnitude
,
319 effect
->direction
= 0;
320 effect
->u
.rumble
.strong_magnitude
=
321 min(i
+ effect
->u
.rumble
.strong_magnitude
, 0xffffU
);
322 effect
->u
.rumble
.weak_magnitude
=
323 min(i
+ effect
->u
.rumble
.weak_magnitude
, 0xffffU
);
327 pr_err("invalid type in ml_combine_effects()\n");
335 * Because memoryless devices have only one effect per effect type active
336 * at one time we have to combine multiple effects into one
338 static int ml_get_combo_effect(struct ml_device
*ml
,
339 unsigned long *effect_handled
,
340 struct ff_effect
*combo_effect
)
342 struct ff_effect
*effect
;
343 struct ml_effect_state
*state
;
347 memset(combo_effect
, 0, sizeof(struct ff_effect
));
349 for (i
= 0; i
< FF_MEMLESS_EFFECTS
; i
++) {
350 if (__test_and_set_bit(i
, effect_handled
))
353 state
= &ml
->states
[i
];
354 effect
= state
->effect
;
356 if (!test_bit(FF_EFFECT_STARTED
, &state
->flags
))
359 if (time_before(jiffies
, state
->play_at
))
363 * here we have started effects that are either
364 * currently playing (and may need be aborted)
365 * or need to start playing.
367 effect_type
= get_compatible_type(ml
->dev
->ff
, effect
->type
);
368 if (combo_effect
->type
!= effect_type
) {
369 if (combo_effect
->type
!= 0) {
370 __clear_bit(i
, effect_handled
);
373 combo_effect
->type
= effect_type
;
376 if (__test_and_clear_bit(FF_EFFECT_ABORTING
, &state
->flags
)) {
377 __clear_bit(FF_EFFECT_PLAYING
, &state
->flags
);
378 __clear_bit(FF_EFFECT_STARTED
, &state
->flags
);
379 } else if (effect
->replay
.length
&&
380 time_after_eq(jiffies
, state
->stop_at
)) {
382 __clear_bit(FF_EFFECT_PLAYING
, &state
->flags
);
384 if (--state
->count
<= 0) {
385 __clear_bit(FF_EFFECT_STARTED
, &state
->flags
);
387 state
->play_at
= jiffies
+
388 msecs_to_jiffies(effect
->replay
.delay
);
389 state
->stop_at
= state
->play_at
+
390 msecs_to_jiffies(effect
->replay
.length
);
393 __set_bit(FF_EFFECT_PLAYING
, &state
->flags
);
394 state
->adj_at
= jiffies
;
395 ml_combine_effects(combo_effect
, state
, ml
->gain
);
399 return combo_effect
->type
!= 0;
402 static void ml_play_effects(struct ml_device
*ml
)
404 struct ff_effect effect
;
405 DECLARE_BITMAP(handled_bm
, FF_MEMLESS_EFFECTS
);
407 memset(handled_bm
, 0, sizeof(handled_bm
));
409 while (ml_get_combo_effect(ml
, handled_bm
, &effect
))
410 ml
->play_effect(ml
->dev
, ml
->private, &effect
);
412 ml_schedule_timer(ml
);
415 static void ml_effect_timer(struct timer_list
*t
)
417 struct ml_device
*ml
= from_timer(ml
, t
, timer
);
418 struct input_dev
*dev
= ml
->dev
;
421 pr_debug("timer: updating effects\n");
423 spin_lock_irqsave(&dev
->event_lock
, flags
);
425 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
429 * Sets requested gain for FF effects. Called with dev->event_lock held.
431 static void ml_ff_set_gain(struct input_dev
*dev
, u16 gain
)
433 struct ml_device
*ml
= dev
->ff
->private;
438 for (i
= 0; i
< FF_MEMLESS_EFFECTS
; i
++)
439 __clear_bit(FF_EFFECT_PLAYING
, &ml
->states
[i
].flags
);
445 * Start/stop specified FF effect. Called with dev->event_lock held.
447 static int ml_ff_playback(struct input_dev
*dev
, int effect_id
, int value
)
449 struct ml_device
*ml
= dev
->ff
->private;
450 struct ml_effect_state
*state
= &ml
->states
[effect_id
];
453 pr_debug("initiated play\n");
455 __set_bit(FF_EFFECT_STARTED
, &state
->flags
);
456 state
->count
= value
;
457 state
->play_at
= jiffies
+
458 msecs_to_jiffies(state
->effect
->replay
.delay
);
459 state
->stop_at
= state
->play_at
+
460 msecs_to_jiffies(state
->effect
->replay
.length
);
461 state
->adj_at
= state
->play_at
;
464 pr_debug("initiated stop\n");
466 if (test_bit(FF_EFFECT_PLAYING
, &state
->flags
))
467 __set_bit(FF_EFFECT_ABORTING
, &state
->flags
);
469 __clear_bit(FF_EFFECT_STARTED
, &state
->flags
);
477 static int ml_ff_upload(struct input_dev
*dev
,
478 struct ff_effect
*effect
, struct ff_effect
*old
)
480 struct ml_device
*ml
= dev
->ff
->private;
481 struct ml_effect_state
*state
= &ml
->states
[effect
->id
];
483 spin_lock_irq(&dev
->event_lock
);
485 if (test_bit(FF_EFFECT_STARTED
, &state
->flags
)) {
486 __clear_bit(FF_EFFECT_PLAYING
, &state
->flags
);
487 state
->play_at
= jiffies
+
488 msecs_to_jiffies(state
->effect
->replay
.delay
);
489 state
->stop_at
= state
->play_at
+
490 msecs_to_jiffies(state
->effect
->replay
.length
);
491 state
->adj_at
= state
->play_at
;
492 ml_schedule_timer(ml
);
495 spin_unlock_irq(&dev
->event_lock
);
500 static void ml_ff_destroy(struct ff_device
*ff
)
502 struct ml_device
*ml
= ff
->private;
508 * input_ff_create_memless() - create memoryless force-feedback device
509 * @dev: input device supporting force-feedback
510 * @data: driver-specific data to be passed into @play_effect
511 * @play_effect: driver-specific method for playing FF effect
513 int input_ff_create_memless(struct input_dev
*dev
, void *data
,
514 int (*play_effect
)(struct input_dev
*, void *, struct ff_effect
*))
516 struct ml_device
*ml
;
517 struct ff_device
*ff
;
521 ml
= kzalloc(sizeof(struct ml_device
), GFP_KERNEL
);
527 ml
->play_effect
= play_effect
;
529 timer_setup(&ml
->timer
, ml_effect_timer
, 0);
531 set_bit(FF_GAIN
, dev
->ffbit
);
533 error
= input_ff_create(dev
, FF_MEMLESS_EFFECTS
);
541 ff
->upload
= ml_ff_upload
;
542 ff
->playback
= ml_ff_playback
;
543 ff
->set_gain
= ml_ff_set_gain
;
544 ff
->destroy
= ml_ff_destroy
;
546 /* we can emulate periodic effects with RUMBLE */
547 if (test_bit(FF_RUMBLE
, ff
->ffbit
)) {
548 set_bit(FF_PERIODIC
, dev
->ffbit
);
549 set_bit(FF_SINE
, dev
->ffbit
);
550 set_bit(FF_TRIANGLE
, dev
->ffbit
);
551 set_bit(FF_SQUARE
, dev
->ffbit
);
554 for (i
= 0; i
< FF_MEMLESS_EFFECTS
; i
++)
555 ml
->states
[i
].effect
= &ff
->effects
[i
];
559 EXPORT_SYMBOL_GPL(input_ff_create_memless
);