1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Force feedback support for memoryless devices
5 * Copyright (c) 2006 Anssi Hannula <anssi.hannula@gmail.com>
6 * Copyright (c) 2006 Dmitry Torokhov <dtor@mail.ru>
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <linux/slab.h>
14 #include <linux/input.h>
15 #include <linux/module.h>
16 #include <linux/mutex.h>
17 #include <linux/spinlock.h>
18 #include <linux/jiffies.h>
19 #include <linux/fixp-arith.h>
21 MODULE_LICENSE("GPL");
22 MODULE_AUTHOR("Anssi Hannula <anssi.hannula@gmail.com>");
23 MODULE_DESCRIPTION("Force feedback support for memoryless devices");
25 /* Number of effects handled with memoryless devices */
26 #define FF_MEMLESS_EFFECTS 16
28 /* Envelope update interval in ms */
29 #define FF_ENVELOPE_INTERVAL 50
31 #define FF_EFFECT_STARTED 0
32 #define FF_EFFECT_PLAYING 1
33 #define FF_EFFECT_ABORTING 2
35 struct ml_effect_state
{
36 struct ff_effect
*effect
;
37 unsigned long flags
; /* effect state (STARTED, PLAYING, etc) */
38 int count
; /* loop count of the effect */
39 unsigned long play_at
; /* start time */
40 unsigned long stop_at
; /* stop time */
41 unsigned long adj_at
; /* last time the effect was sent */
46 struct ml_effect_state states
[FF_MEMLESS_EFFECTS
];
48 struct timer_list timer
;
49 struct input_dev
*dev
;
51 int (*play_effect
)(struct input_dev
*dev
, void *data
,
52 struct ff_effect
*effect
);
55 static const struct ff_envelope
*get_envelope(const struct ff_effect
*effect
)
57 static const struct ff_envelope empty_envelope
;
59 switch (effect
->type
) {
61 return &effect
->u
.periodic
.envelope
;
64 return &effect
->u
.constant
.envelope
;
67 return &empty_envelope
;
72 * Check for the next time envelope requires an update on memoryless devices
74 static unsigned long calculate_next_time(struct ml_effect_state
*state
)
76 const struct ff_envelope
*envelope
= get_envelope(state
->effect
);
77 unsigned long attack_stop
, fade_start
, next_fade
;
79 if (envelope
->attack_length
) {
80 attack_stop
= state
->play_at
+
81 msecs_to_jiffies(envelope
->attack_length
);
82 if (time_before(state
->adj_at
, attack_stop
))
83 return state
->adj_at
+
84 msecs_to_jiffies(FF_ENVELOPE_INTERVAL
);
87 if (state
->effect
->replay
.length
) {
88 if (envelope
->fade_length
) {
89 /* check when fading should start */
90 fade_start
= state
->stop_at
-
91 msecs_to_jiffies(envelope
->fade_length
);
93 if (time_before(state
->adj_at
, fade_start
))
96 /* already fading, advance to next checkpoint */
97 next_fade
= state
->adj_at
+
98 msecs_to_jiffies(FF_ENVELOPE_INTERVAL
);
99 if (time_before(next_fade
, state
->stop_at
))
103 return state
->stop_at
;
106 return state
->play_at
;
109 static void ml_schedule_timer(struct ml_device
*ml
)
111 struct ml_effect_state
*state
;
112 unsigned long now
= jiffies
;
113 unsigned long earliest
= 0;
114 unsigned long next_at
;
118 pr_debug("calculating next timer\n");
120 for (i
= 0; i
< FF_MEMLESS_EFFECTS
; i
++) {
122 state
= &ml
->states
[i
];
124 if (!test_bit(FF_EFFECT_STARTED
, &state
->flags
))
127 if (test_bit(FF_EFFECT_PLAYING
, &state
->flags
))
128 next_at
= calculate_next_time(state
);
130 next_at
= state
->play_at
;
132 if (time_before_eq(now
, next_at
) &&
133 (++events
== 1 || time_before(next_at
, earliest
)))
138 pr_debug("no actions\n");
139 del_timer(&ml
->timer
);
141 pr_debug("timer set\n");
142 mod_timer(&ml
->timer
, earliest
);
147 * Apply an envelope to a value
149 static int apply_envelope(struct ml_effect_state
*state
, int value
,
150 struct ff_envelope
*envelope
)
152 struct ff_effect
*effect
= state
->effect
;
153 unsigned long now
= jiffies
;
155 int time_of_envelope
;
159 if (envelope
->attack_length
&&
161 state
->play_at
+ msecs_to_jiffies(envelope
->attack_length
))) {
162 pr_debug("value = 0x%x, attack_level = 0x%x\n",
163 value
, envelope
->attack_level
);
164 time_from_level
= jiffies_to_msecs(now
- state
->play_at
);
165 time_of_envelope
= envelope
->attack_length
;
166 envelope_level
= min_t(u16
, envelope
->attack_level
, 0x7fff);
168 } else if (envelope
->fade_length
&& effect
->replay
.length
&&
170 state
->stop_at
- msecs_to_jiffies(envelope
->fade_length
)) &&
171 time_before(now
, state
->stop_at
)) {
172 time_from_level
= jiffies_to_msecs(state
->stop_at
- now
);
173 time_of_envelope
= envelope
->fade_length
;
174 envelope_level
= min_t(u16
, envelope
->fade_level
, 0x7fff);
178 difference
= abs(value
) - envelope_level
;
180 pr_debug("difference = %d\n", difference
);
181 pr_debug("time_from_level = 0x%x\n", time_from_level
);
182 pr_debug("time_of_envelope = 0x%x\n", time_of_envelope
);
184 difference
= difference
* time_from_level
/ time_of_envelope
;
186 pr_debug("difference = %d\n", difference
);
189 -(difference
+ envelope_level
) : (difference
+ envelope_level
);
193 * Return the type the effect has to be converted into (memless devices)
195 static int get_compatible_type(struct ff_device
*ff
, int effect_type
)
198 if (test_bit(effect_type
, ff
->ffbit
))
201 if (effect_type
== FF_PERIODIC
&& test_bit(FF_RUMBLE
, ff
->ffbit
))
204 pr_err("invalid type in get_compatible_type()\n");
210 * Only left/right direction should be used (under/over 0x8000) for
211 * forward/reverse motor direction (to keep calculation fast & simple).
213 static u16
ml_calculate_direction(u16 direction
, u16 force
,
214 u16 new_direction
, u16 new_force
)
217 return new_direction
;
220 return (((u32
)(direction
>> 1) * force
+
221 (new_direction
>> 1) * new_force
) /
222 (force
+ new_force
)) << 1;
226 static inline s16
fixp_new16(s16 a
)
228 return ((s32
)a
) >> (16 - FRAC_N
);
231 static inline s16
fixp_mult(s16 a
, s16 b
)
233 a
= ((s32
)a
* 0x100) / 0x7fff;
234 return ((s32
)(a
* b
)) >> FRAC_N
;
238 * Combine two effects and apply gain.
240 static void ml_combine_effects(struct ff_effect
*effect
,
241 struct ml_effect_state
*state
,
244 struct ff_effect
*new = state
->effect
;
245 unsigned int strong
, weak
, i
;
251 i
= new->direction
* 360 / 0xffff;
252 level
= fixp_new16(apply_envelope(state
,
253 new->u
.constant
.level
,
254 &new->u
.constant
.envelope
));
255 x
= fixp_mult(fixp_sin16(i
), level
) * gain
/ 0xffff;
256 y
= fixp_mult(-fixp_cos16(i
), level
) * gain
/ 0xffff;
258 * here we abuse ff_ramp to hold x and y of constant force
259 * If in future any driver wants something else than x and y
260 * in s8, this should be changed to something more generic
262 effect
->u
.ramp
.start_level
=
263 clamp_val(effect
->u
.ramp
.start_level
+ x
, -0x80, 0x7f);
264 effect
->u
.ramp
.end_level
=
265 clamp_val(effect
->u
.ramp
.end_level
+ y
, -0x80, 0x7f);
269 strong
= (u32
)new->u
.rumble
.strong_magnitude
* gain
/ 0xffff;
270 weak
= (u32
)new->u
.rumble
.weak_magnitude
* gain
/ 0xffff;
272 if (effect
->u
.rumble
.strong_magnitude
+ strong
)
273 effect
->direction
= ml_calculate_direction(
275 effect
->u
.rumble
.strong_magnitude
,
276 new->direction
, strong
);
277 else if (effect
->u
.rumble
.weak_magnitude
+ weak
)
278 effect
->direction
= ml_calculate_direction(
280 effect
->u
.rumble
.weak_magnitude
,
281 new->direction
, weak
);
283 effect
->direction
= 0;
284 effect
->u
.rumble
.strong_magnitude
=
285 min(strong
+ effect
->u
.rumble
.strong_magnitude
,
287 effect
->u
.rumble
.weak_magnitude
=
288 min(weak
+ effect
->u
.rumble
.weak_magnitude
, 0xffffU
);
292 i
= apply_envelope(state
, abs(new->u
.periodic
.magnitude
),
293 &new->u
.periodic
.envelope
);
295 /* here we also scale it 0x7fff => 0xffff */
296 i
= i
* gain
/ 0x7fff;
298 if (effect
->u
.rumble
.strong_magnitude
+ i
)
299 effect
->direction
= ml_calculate_direction(
301 effect
->u
.rumble
.strong_magnitude
,
304 effect
->direction
= 0;
305 effect
->u
.rumble
.strong_magnitude
=
306 min(i
+ effect
->u
.rumble
.strong_magnitude
, 0xffffU
);
307 effect
->u
.rumble
.weak_magnitude
=
308 min(i
+ effect
->u
.rumble
.weak_magnitude
, 0xffffU
);
312 pr_err("invalid type in ml_combine_effects()\n");
320 * Because memoryless devices have only one effect per effect type active
321 * at one time we have to combine multiple effects into one
323 static int ml_get_combo_effect(struct ml_device
*ml
,
324 unsigned long *effect_handled
,
325 struct ff_effect
*combo_effect
)
327 struct ff_effect
*effect
;
328 struct ml_effect_state
*state
;
332 memset(combo_effect
, 0, sizeof(struct ff_effect
));
334 for (i
= 0; i
< FF_MEMLESS_EFFECTS
; i
++) {
335 if (__test_and_set_bit(i
, effect_handled
))
338 state
= &ml
->states
[i
];
339 effect
= state
->effect
;
341 if (!test_bit(FF_EFFECT_STARTED
, &state
->flags
))
344 if (time_before(jiffies
, state
->play_at
))
348 * here we have started effects that are either
349 * currently playing (and may need be aborted)
350 * or need to start playing.
352 effect_type
= get_compatible_type(ml
->dev
->ff
, effect
->type
);
353 if (combo_effect
->type
!= effect_type
) {
354 if (combo_effect
->type
!= 0) {
355 __clear_bit(i
, effect_handled
);
358 combo_effect
->type
= effect_type
;
361 if (__test_and_clear_bit(FF_EFFECT_ABORTING
, &state
->flags
)) {
362 __clear_bit(FF_EFFECT_PLAYING
, &state
->flags
);
363 __clear_bit(FF_EFFECT_STARTED
, &state
->flags
);
364 } else if (effect
->replay
.length
&&
365 time_after_eq(jiffies
, state
->stop_at
)) {
367 __clear_bit(FF_EFFECT_PLAYING
, &state
->flags
);
369 if (--state
->count
<= 0) {
370 __clear_bit(FF_EFFECT_STARTED
, &state
->flags
);
372 state
->play_at
= jiffies
+
373 msecs_to_jiffies(effect
->replay
.delay
);
374 state
->stop_at
= state
->play_at
+
375 msecs_to_jiffies(effect
->replay
.length
);
378 __set_bit(FF_EFFECT_PLAYING
, &state
->flags
);
379 state
->adj_at
= jiffies
;
380 ml_combine_effects(combo_effect
, state
, ml
->gain
);
384 return combo_effect
->type
!= 0;
387 static void ml_play_effects(struct ml_device
*ml
)
389 struct ff_effect effect
;
390 DECLARE_BITMAP(handled_bm
, FF_MEMLESS_EFFECTS
);
392 memset(handled_bm
, 0, sizeof(handled_bm
));
394 while (ml_get_combo_effect(ml
, handled_bm
, &effect
))
395 ml
->play_effect(ml
->dev
, ml
->private, &effect
);
397 ml_schedule_timer(ml
);
400 static void ml_effect_timer(struct timer_list
*t
)
402 struct ml_device
*ml
= from_timer(ml
, t
, timer
);
403 struct input_dev
*dev
= ml
->dev
;
406 pr_debug("timer: updating effects\n");
408 spin_lock_irqsave(&dev
->event_lock
, flags
);
410 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
414 * Sets requested gain for FF effects. Called with dev->event_lock held.
416 static void ml_ff_set_gain(struct input_dev
*dev
, u16 gain
)
418 struct ml_device
*ml
= dev
->ff
->private;
423 for (i
= 0; i
< FF_MEMLESS_EFFECTS
; i
++)
424 __clear_bit(FF_EFFECT_PLAYING
, &ml
->states
[i
].flags
);
430 * Start/stop specified FF effect. Called with dev->event_lock held.
432 static int ml_ff_playback(struct input_dev
*dev
, int effect_id
, int value
)
434 struct ml_device
*ml
= dev
->ff
->private;
435 struct ml_effect_state
*state
= &ml
->states
[effect_id
];
438 pr_debug("initiated play\n");
440 __set_bit(FF_EFFECT_STARTED
, &state
->flags
);
441 state
->count
= value
;
442 state
->play_at
= jiffies
+
443 msecs_to_jiffies(state
->effect
->replay
.delay
);
444 state
->stop_at
= state
->play_at
+
445 msecs_to_jiffies(state
->effect
->replay
.length
);
446 state
->adj_at
= state
->play_at
;
449 pr_debug("initiated stop\n");
451 if (test_bit(FF_EFFECT_PLAYING
, &state
->flags
))
452 __set_bit(FF_EFFECT_ABORTING
, &state
->flags
);
454 __clear_bit(FF_EFFECT_STARTED
, &state
->flags
);
462 static int ml_ff_upload(struct input_dev
*dev
,
463 struct ff_effect
*effect
, struct ff_effect
*old
)
465 struct ml_device
*ml
= dev
->ff
->private;
466 struct ml_effect_state
*state
= &ml
->states
[effect
->id
];
468 spin_lock_irq(&dev
->event_lock
);
470 if (test_bit(FF_EFFECT_STARTED
, &state
->flags
)) {
471 __clear_bit(FF_EFFECT_PLAYING
, &state
->flags
);
472 state
->play_at
= jiffies
+
473 msecs_to_jiffies(state
->effect
->replay
.delay
);
474 state
->stop_at
= state
->play_at
+
475 msecs_to_jiffies(state
->effect
->replay
.length
);
476 state
->adj_at
= state
->play_at
;
477 ml_schedule_timer(ml
);
480 spin_unlock_irq(&dev
->event_lock
);
485 static void ml_ff_destroy(struct ff_device
*ff
)
487 struct ml_device
*ml
= ff
->private;
490 * Even though we stop all playing effects when tearing down
491 * an input device (via input_device_flush() that calls into
492 * input_ff_flush() that stops and erases all effects), we
493 * do not actually stop the timer, and therefore we should
496 del_timer_sync(&ml
->timer
);
502 * input_ff_create_memless() - create memoryless force-feedback device
503 * @dev: input device supporting force-feedback
504 * @data: driver-specific data to be passed into @play_effect
505 * @play_effect: driver-specific method for playing FF effect
507 int input_ff_create_memless(struct input_dev
*dev
, void *data
,
508 int (*play_effect
)(struct input_dev
*, void *, struct ff_effect
*))
510 struct ml_device
*ml
;
511 struct ff_device
*ff
;
515 ml
= kzalloc(sizeof(struct ml_device
), GFP_KERNEL
);
521 ml
->play_effect
= play_effect
;
523 timer_setup(&ml
->timer
, ml_effect_timer
, 0);
525 set_bit(FF_GAIN
, dev
->ffbit
);
527 error
= input_ff_create(dev
, FF_MEMLESS_EFFECTS
);
535 ff
->upload
= ml_ff_upload
;
536 ff
->playback
= ml_ff_playback
;
537 ff
->set_gain
= ml_ff_set_gain
;
538 ff
->destroy
= ml_ff_destroy
;
540 /* we can emulate periodic effects with RUMBLE */
541 if (test_bit(FF_RUMBLE
, ff
->ffbit
)) {
542 set_bit(FF_PERIODIC
, dev
->ffbit
);
543 set_bit(FF_SINE
, dev
->ffbit
);
544 set_bit(FF_TRIANGLE
, dev
->ffbit
);
545 set_bit(FF_SQUARE
, dev
->ffbit
);
548 for (i
= 0; i
< FF_MEMLESS_EFFECTS
; i
++)
549 ml
->states
[i
].effect
= &ff
->effects
[i
];
553 EXPORT_SYMBOL_GPL(input_ff_create_memless
);