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
;
78 return &effect
->u
.constant
.envelope
;
80 return &empty_envelope
;
85 * Check for the next time envelope requires an update on memoryless devices
87 static unsigned long calculate_next_time(struct ml_effect_state
*state
)
89 const struct ff_envelope
*envelope
= get_envelope(state
->effect
);
90 unsigned long attack_stop
, fade_start
, next_fade
;
92 if (envelope
->attack_length
) {
93 attack_stop
= state
->play_at
+
94 msecs_to_jiffies(envelope
->attack_length
);
95 if (time_before(state
->adj_at
, attack_stop
))
96 return state
->adj_at
+
97 msecs_to_jiffies(FF_ENVELOPE_INTERVAL
);
100 if (state
->effect
->replay
.length
) {
101 if (envelope
->fade_length
) {
102 /* check when fading should start */
103 fade_start
= state
->stop_at
-
104 msecs_to_jiffies(envelope
->fade_length
);
106 if (time_before(state
->adj_at
, fade_start
))
109 /* already fading, advance to next checkpoint */
110 next_fade
= state
->adj_at
+
111 msecs_to_jiffies(FF_ENVELOPE_INTERVAL
);
112 if (time_before(next_fade
, state
->stop_at
))
116 return state
->stop_at
;
119 return state
->play_at
;
122 static void ml_schedule_timer(struct ml_device
*ml
)
124 struct ml_effect_state
*state
;
125 unsigned long now
= jiffies
;
126 unsigned long earliest
= 0;
127 unsigned long next_at
;
131 pr_debug("calculating next timer\n");
133 for (i
= 0; i
< FF_MEMLESS_EFFECTS
; i
++) {
135 state
= &ml
->states
[i
];
137 if (!test_bit(FF_EFFECT_STARTED
, &state
->flags
))
140 if (test_bit(FF_EFFECT_PLAYING
, &state
->flags
))
141 next_at
= calculate_next_time(state
);
143 next_at
= state
->play_at
;
145 if (time_before_eq(now
, next_at
) &&
146 (++events
== 1 || time_before(next_at
, earliest
)))
151 pr_debug("no actions\n");
152 del_timer(&ml
->timer
);
154 pr_debug("timer set\n");
155 mod_timer(&ml
->timer
, earliest
);
160 * Apply an envelope to a value
162 static int apply_envelope(struct ml_effect_state
*state
, int value
,
163 struct ff_envelope
*envelope
)
165 struct ff_effect
*effect
= state
->effect
;
166 unsigned long now
= jiffies
;
168 int time_of_envelope
;
172 if (envelope
->attack_length
&&
174 state
->play_at
+ msecs_to_jiffies(envelope
->attack_length
))) {
175 pr_debug("value = 0x%x, attack_level = 0x%x\n",
176 value
, envelope
->attack_level
);
177 time_from_level
= jiffies_to_msecs(now
- state
->play_at
);
178 time_of_envelope
= envelope
->attack_length
;
179 envelope_level
= min_t(u16
, envelope
->attack_level
, 0x7fff);
181 } else if (envelope
->fade_length
&& effect
->replay
.length
&&
183 state
->stop_at
- msecs_to_jiffies(envelope
->fade_length
)) &&
184 time_before(now
, state
->stop_at
)) {
185 time_from_level
= jiffies_to_msecs(state
->stop_at
- now
);
186 time_of_envelope
= envelope
->fade_length
;
187 envelope_level
= min_t(u16
, envelope
->fade_level
, 0x7fff);
191 difference
= abs(value
) - envelope_level
;
193 pr_debug("difference = %d\n", difference
);
194 pr_debug("time_from_level = 0x%x\n", time_from_level
);
195 pr_debug("time_of_envelope = 0x%x\n", time_of_envelope
);
197 difference
= difference
* time_from_level
/ time_of_envelope
;
199 pr_debug("difference = %d\n", difference
);
202 -(difference
+ envelope_level
) : (difference
+ envelope_level
);
206 * Return the type the effect has to be converted into (memless devices)
208 static int get_compatible_type(struct ff_device
*ff
, int effect_type
)
211 if (test_bit(effect_type
, ff
->ffbit
))
214 if (effect_type
== FF_PERIODIC
&& test_bit(FF_RUMBLE
, ff
->ffbit
))
217 pr_err("invalid type in get_compatible_type()\n");
223 * Only left/right direction should be used (under/over 0x8000) for
224 * forward/reverse motor direction (to keep calculation fast & simple).
226 static u16
ml_calculate_direction(u16 direction
, u16 force
,
227 u16 new_direction
, u16 new_force
)
230 return new_direction
;
233 return (((u32
)(direction
>> 1) * force
+
234 (new_direction
>> 1) * new_force
) /
235 (force
+ new_force
)) << 1;
239 * Combine two effects and apply gain.
241 static void ml_combine_effects(struct ff_effect
*effect
,
242 struct ml_effect_state
*state
,
245 struct ff_effect
*new = state
->effect
;
246 unsigned int strong
, weak
, i
;
252 i
= new->direction
* 360 / 0xffff;
253 level
= fixp_new16(apply_envelope(state
,
254 new->u
.constant
.level
,
255 &new->u
.constant
.envelope
));
256 x
= fixp_mult(fixp_sin(i
), level
) * gain
/ 0xffff;
257 y
= fixp_mult(-fixp_cos(i
), level
) * gain
/ 0xffff;
259 * here we abuse ff_ramp to hold x and y of constant force
260 * If in future any driver wants something else than x and y
261 * in s8, this should be changed to something more generic
263 effect
->u
.ramp
.start_level
=
264 clamp_val(effect
->u
.ramp
.start_level
+ x
, -0x80, 0x7f);
265 effect
->u
.ramp
.end_level
=
266 clamp_val(effect
->u
.ramp
.end_level
+ y
, -0x80, 0x7f);
270 strong
= (u32
)new->u
.rumble
.strong_magnitude
* gain
/ 0xffff;
271 weak
= (u32
)new->u
.rumble
.weak_magnitude
* gain
/ 0xffff;
273 if (effect
->u
.rumble
.strong_magnitude
+ strong
)
274 effect
->direction
= ml_calculate_direction(
276 effect
->u
.rumble
.strong_magnitude
,
277 new->direction
, strong
);
278 else if (effect
->u
.rumble
.weak_magnitude
+ weak
)
279 effect
->direction
= ml_calculate_direction(
281 effect
->u
.rumble
.weak_magnitude
,
282 new->direction
, weak
);
284 effect
->direction
= 0;
285 effect
->u
.rumble
.strong_magnitude
=
286 min(strong
+ effect
->u
.rumble
.strong_magnitude
,
288 effect
->u
.rumble
.weak_magnitude
=
289 min(weak
+ effect
->u
.rumble
.weak_magnitude
, 0xffffU
);
293 i
= apply_envelope(state
, abs(new->u
.periodic
.magnitude
),
294 &new->u
.periodic
.envelope
);
296 /* here we also scale it 0x7fff => 0xffff */
297 i
= i
* gain
/ 0x7fff;
299 if (effect
->u
.rumble
.strong_magnitude
+ i
)
300 effect
->direction
= ml_calculate_direction(
302 effect
->u
.rumble
.strong_magnitude
,
305 effect
->direction
= 0;
306 effect
->u
.rumble
.strong_magnitude
=
307 min(i
+ effect
->u
.rumble
.strong_magnitude
, 0xffffU
);
308 effect
->u
.rumble
.weak_magnitude
=
309 min(i
+ effect
->u
.rumble
.weak_magnitude
, 0xffffU
);
313 pr_err("invalid type in ml_combine_effects()\n");
321 * Because memoryless devices have only one effect per effect type active
322 * at one time we have to combine multiple effects into one
324 static int ml_get_combo_effect(struct ml_device
*ml
,
325 unsigned long *effect_handled
,
326 struct ff_effect
*combo_effect
)
328 struct ff_effect
*effect
;
329 struct ml_effect_state
*state
;
333 memset(combo_effect
, 0, sizeof(struct ff_effect
));
335 for (i
= 0; i
< FF_MEMLESS_EFFECTS
; i
++) {
336 if (__test_and_set_bit(i
, effect_handled
))
339 state
= &ml
->states
[i
];
340 effect
= state
->effect
;
342 if (!test_bit(FF_EFFECT_STARTED
, &state
->flags
))
345 if (time_before(jiffies
, state
->play_at
))
349 * here we have started effects that are either
350 * currently playing (and may need be aborted)
351 * or need to start playing.
353 effect_type
= get_compatible_type(ml
->dev
->ff
, effect
->type
);
354 if (combo_effect
->type
!= effect_type
) {
355 if (combo_effect
->type
!= 0) {
356 __clear_bit(i
, effect_handled
);
359 combo_effect
->type
= effect_type
;
362 if (__test_and_clear_bit(FF_EFFECT_ABORTING
, &state
->flags
)) {
363 __clear_bit(FF_EFFECT_PLAYING
, &state
->flags
);
364 __clear_bit(FF_EFFECT_STARTED
, &state
->flags
);
365 } else if (effect
->replay
.length
&&
366 time_after_eq(jiffies
, state
->stop_at
)) {
368 __clear_bit(FF_EFFECT_PLAYING
, &state
->flags
);
370 if (--state
->count
<= 0) {
371 __clear_bit(FF_EFFECT_STARTED
, &state
->flags
);
373 state
->play_at
= jiffies
+
374 msecs_to_jiffies(effect
->replay
.delay
);
375 state
->stop_at
= state
->play_at
+
376 msecs_to_jiffies(effect
->replay
.length
);
379 __set_bit(FF_EFFECT_PLAYING
, &state
->flags
);
380 state
->adj_at
= jiffies
;
381 ml_combine_effects(combo_effect
, state
, ml
->gain
);
385 return combo_effect
->type
!= 0;
388 static void ml_play_effects(struct ml_device
*ml
)
390 struct ff_effect effect
;
391 DECLARE_BITMAP(handled_bm
, FF_MEMLESS_EFFECTS
);
393 memset(handled_bm
, 0, sizeof(handled_bm
));
395 while (ml_get_combo_effect(ml
, handled_bm
, &effect
))
396 ml
->play_effect(ml
->dev
, ml
->private, &effect
);
398 ml_schedule_timer(ml
);
401 static void ml_effect_timer(unsigned long timer_data
)
403 struct input_dev
*dev
= (struct input_dev
*)timer_data
;
404 struct ml_device
*ml
= dev
->ff
->private;
407 pr_debug("timer: updating effects\n");
409 spin_lock_irqsave(&dev
->event_lock
, flags
);
411 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
415 * Sets requested gain for FF effects. Called with dev->event_lock held.
417 static void ml_ff_set_gain(struct input_dev
*dev
, u16 gain
)
419 struct ml_device
*ml
= dev
->ff
->private;
424 for (i
= 0; i
< FF_MEMLESS_EFFECTS
; i
++)
425 __clear_bit(FF_EFFECT_PLAYING
, &ml
->states
[i
].flags
);
431 * Start/stop specified FF effect. Called with dev->event_lock held.
433 static int ml_ff_playback(struct input_dev
*dev
, int effect_id
, int value
)
435 struct ml_device
*ml
= dev
->ff
->private;
436 struct ml_effect_state
*state
= &ml
->states
[effect_id
];
439 pr_debug("initiated play\n");
441 __set_bit(FF_EFFECT_STARTED
, &state
->flags
);
442 state
->count
= value
;
443 state
->play_at
= jiffies
+
444 msecs_to_jiffies(state
->effect
->replay
.delay
);
445 state
->stop_at
= state
->play_at
+
446 msecs_to_jiffies(state
->effect
->replay
.length
);
447 state
->adj_at
= state
->play_at
;
450 pr_debug("initiated stop\n");
452 if (test_bit(FF_EFFECT_PLAYING
, &state
->flags
))
453 __set_bit(FF_EFFECT_ABORTING
, &state
->flags
);
455 __clear_bit(FF_EFFECT_STARTED
, &state
->flags
);
463 static int ml_ff_upload(struct input_dev
*dev
,
464 struct ff_effect
*effect
, struct ff_effect
*old
)
466 struct ml_device
*ml
= dev
->ff
->private;
467 struct ml_effect_state
*state
= &ml
->states
[effect
->id
];
469 spin_lock_irq(&dev
->event_lock
);
471 if (test_bit(FF_EFFECT_STARTED
, &state
->flags
)) {
472 __clear_bit(FF_EFFECT_PLAYING
, &state
->flags
);
473 state
->play_at
= jiffies
+
474 msecs_to_jiffies(state
->effect
->replay
.delay
);
475 state
->stop_at
= state
->play_at
+
476 msecs_to_jiffies(state
->effect
->replay
.length
);
477 state
->adj_at
= state
->play_at
;
478 ml_schedule_timer(ml
);
481 spin_unlock_irq(&dev
->event_lock
);
486 static void ml_ff_destroy(struct ff_device
*ff
)
488 struct ml_device
*ml
= ff
->private;
494 * input_ff_create_memless() - create memoryless force-feedback device
495 * @dev: input device supporting force-feedback
496 * @data: driver-specific data to be passed into @play_effect
497 * @play_effect: driver-specific method for playing FF effect
499 int input_ff_create_memless(struct input_dev
*dev
, void *data
,
500 int (*play_effect
)(struct input_dev
*, void *, struct ff_effect
*))
502 struct ml_device
*ml
;
503 struct ff_device
*ff
;
507 ml
= kzalloc(sizeof(struct ml_device
), GFP_KERNEL
);
513 ml
->play_effect
= play_effect
;
515 setup_timer(&ml
->timer
, ml_effect_timer
, (unsigned long)dev
);
517 set_bit(FF_GAIN
, dev
->ffbit
);
519 error
= input_ff_create(dev
, FF_MEMLESS_EFFECTS
);
527 ff
->upload
= ml_ff_upload
;
528 ff
->playback
= ml_ff_playback
;
529 ff
->set_gain
= ml_ff_set_gain
;
530 ff
->destroy
= ml_ff_destroy
;
532 /* we can emulate periodic effects with RUMBLE */
533 if (test_bit(FF_RUMBLE
, ff
->ffbit
)) {
534 set_bit(FF_PERIODIC
, dev
->ffbit
);
535 set_bit(FF_SINE
, dev
->ffbit
);
536 set_bit(FF_TRIANGLE
, dev
->ffbit
);
537 set_bit(FF_SQUARE
, dev
->ffbit
);
540 for (i
= 0; i
< FF_MEMLESS_EFFECTS
; i
++)
541 ml
->states
[i
].effect
= &ff
->effects
[i
];
545 EXPORT_SYMBOL_GPL(input_ff_create_memless
);