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 debug(format, arg...) pr_debug("ff-memless: " format "\n", ## arg)
28 #include <linux/input.h>
29 #include <linux/module.h>
30 #include <linux/mutex.h>
31 #include <linux/interrupt.h>
32 #include <linux/spinlock.h>
33 #include <linux/jiffies.h>
35 #include "fixp-arith.h"
37 MODULE_LICENSE("GPL");
38 MODULE_AUTHOR("Anssi Hannula <anssi.hannula@gmail.com>");
39 MODULE_DESCRIPTION("Force feedback support for memoryless devices");
41 /* Number of effects handled with memoryless devices */
42 #define FF_MEMLESS_EFFECTS 16
44 /* Envelope update interval in ms */
45 #define FF_ENVELOPE_INTERVAL 50
47 #define FF_EFFECT_STARTED 0
48 #define FF_EFFECT_PLAYING 1
49 #define FF_EFFECT_ABORTING 2
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 */
62 struct ml_effect_state states
[FF_MEMLESS_EFFECTS
];
64 struct timer_list timer
;
65 spinlock_t timer_lock
;
66 struct input_dev
*dev
;
68 int (*play_effect
)(struct input_dev
*dev
, void *data
,
69 struct ff_effect
*effect
);
72 static const struct ff_envelope
*get_envelope(const struct ff_effect
*effect
)
74 static const struct ff_envelope empty_envelope
;
76 switch (effect
->type
) {
78 return &effect
->u
.periodic
.envelope
;
80 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 debug("calculating next timer");
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
)))
154 del_timer(&ml
->timer
);
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 debug("value = 0x%x, attack_level = 0x%x", value
,
178 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(__s16
, 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(__s16
, envelope
->fade_level
, 0x7fff);
193 difference
= abs(value
) - envelope_level
;
195 debug("difference = %d", difference
);
196 debug("time_from_level = 0x%x", time_from_level
);
197 debug("time_of_envelope = 0x%x", time_of_envelope
);
199 difference
= difference
* time_from_level
/ time_of_envelope
;
201 debug("difference = %d", 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
))
220 "ff-memless: invalid type in get_compatible_type()\n");
226 * Combine two effects and apply gain.
228 static void ml_combine_effects(struct ff_effect
*effect
,
229 struct ml_effect_state
*state
,
232 struct ff_effect
*new = state
->effect
;
233 unsigned int strong
, weak
, i
;
239 i
= new->direction
* 360 / 0xffff;
240 level
= fixp_new16(apply_envelope(state
,
241 new->u
.constant
.level
,
242 &new->u
.constant
.envelope
));
243 x
= fixp_mult(fixp_sin(i
), level
) * gain
/ 0xffff;
244 y
= fixp_mult(-fixp_cos(i
), level
) * gain
/ 0xffff;
246 * here we abuse ff_ramp to hold x and y of constant force
247 * If in future any driver wants something else than x and y
248 * in s8, this should be changed to something more generic
250 effect
->u
.ramp
.start_level
=
251 clamp_val(effect
->u
.ramp
.start_level
+ x
, -0x80, 0x7f);
252 effect
->u
.ramp
.end_level
=
253 clamp_val(effect
->u
.ramp
.end_level
+ y
, -0x80, 0x7f);
257 strong
= new->u
.rumble
.strong_magnitude
* gain
/ 0xffff;
258 weak
= new->u
.rumble
.weak_magnitude
* gain
/ 0xffff;
259 effect
->u
.rumble
.strong_magnitude
=
260 min(strong
+ effect
->u
.rumble
.strong_magnitude
,
262 effect
->u
.rumble
.weak_magnitude
=
263 min(weak
+ effect
->u
.rumble
.weak_magnitude
, 0xffffU
);
267 i
= apply_envelope(state
, abs(new->u
.periodic
.magnitude
),
268 &new->u
.periodic
.envelope
);
270 /* here we also scale it 0x7fff => 0xffff */
271 i
= i
* gain
/ 0x7fff;
273 effect
->u
.rumble
.strong_magnitude
=
274 min(i
+ effect
->u
.rumble
.strong_magnitude
, 0xffffU
);
275 effect
->u
.rumble
.weak_magnitude
=
276 min(i
+ effect
->u
.rumble
.weak_magnitude
, 0xffffU
);
280 printk(KERN_ERR
"ff-memless: invalid type in ml_combine_effects()\n");
288 * Because memoryless devices have only one effect per effect type active
289 * at one time we have to combine multiple effects into one
291 static int ml_get_combo_effect(struct ml_device
*ml
,
292 unsigned long *effect_handled
,
293 struct ff_effect
*combo_effect
)
295 struct ff_effect
*effect
;
296 struct ml_effect_state
*state
;
300 memset(combo_effect
, 0, sizeof(struct ff_effect
));
302 for (i
= 0; i
< FF_MEMLESS_EFFECTS
; i
++) {
303 if (__test_and_set_bit(i
, effect_handled
))
306 state
= &ml
->states
[i
];
307 effect
= state
->effect
;
309 if (!test_bit(FF_EFFECT_STARTED
, &state
->flags
))
312 if (time_before(jiffies
, state
->play_at
))
316 * here we have started effects that are either
317 * currently playing (and may need be aborted)
318 * or need to start playing.
320 effect_type
= get_compatible_type(ml
->dev
->ff
, effect
->type
);
321 if (combo_effect
->type
!= effect_type
) {
322 if (combo_effect
->type
!= 0) {
323 __clear_bit(i
, effect_handled
);
326 combo_effect
->type
= effect_type
;
329 if (__test_and_clear_bit(FF_EFFECT_ABORTING
, &state
->flags
)) {
330 __clear_bit(FF_EFFECT_PLAYING
, &state
->flags
);
331 __clear_bit(FF_EFFECT_STARTED
, &state
->flags
);
332 } else if (effect
->replay
.length
&&
333 time_after_eq(jiffies
, state
->stop_at
)) {
335 __clear_bit(FF_EFFECT_PLAYING
, &state
->flags
);
337 if (--state
->count
<= 0) {
338 __clear_bit(FF_EFFECT_STARTED
, &state
->flags
);
340 state
->play_at
= jiffies
+
341 msecs_to_jiffies(effect
->replay
.delay
);
342 state
->stop_at
= state
->play_at
+
343 msecs_to_jiffies(effect
->replay
.length
);
346 __set_bit(FF_EFFECT_PLAYING
, &state
->flags
);
347 state
->adj_at
= jiffies
;
348 ml_combine_effects(combo_effect
, state
, ml
->gain
);
352 return combo_effect
->type
!= 0;
355 static void ml_play_effects(struct ml_device
*ml
)
357 struct ff_effect effect
;
358 DECLARE_BITMAP(handled_bm
, FF_MEMLESS_EFFECTS
);
360 memset(handled_bm
, 0, sizeof(handled_bm
));
362 while (ml_get_combo_effect(ml
, handled_bm
, &effect
))
363 ml
->play_effect(ml
->dev
, ml
->private, &effect
);
365 ml_schedule_timer(ml
);
368 static void ml_effect_timer(unsigned long timer_data
)
370 struct input_dev
*dev
= (struct input_dev
*)timer_data
;
371 struct ml_device
*ml
= dev
->ff
->private;
373 debug("timer: updating effects");
375 spin_lock(&ml
->timer_lock
);
377 spin_unlock(&ml
->timer_lock
);
380 static void ml_ff_set_gain(struct input_dev
*dev
, u16 gain
)
382 struct ml_device
*ml
= dev
->ff
->private;
385 spin_lock_bh(&ml
->timer_lock
);
389 for (i
= 0; i
< FF_MEMLESS_EFFECTS
; i
++)
390 __clear_bit(FF_EFFECT_PLAYING
, &ml
->states
[i
].flags
);
394 spin_unlock_bh(&ml
->timer_lock
);
397 static int ml_ff_playback(struct input_dev
*dev
, int effect_id
, int value
)
399 struct ml_device
*ml
= dev
->ff
->private;
400 struct ml_effect_state
*state
= &ml
->states
[effect_id
];
403 spin_lock_irqsave(&ml
->timer_lock
, flags
);
406 debug("initiated play");
408 __set_bit(FF_EFFECT_STARTED
, &state
->flags
);
409 state
->count
= value
;
410 state
->play_at
= jiffies
+
411 msecs_to_jiffies(state
->effect
->replay
.delay
);
412 state
->stop_at
= state
->play_at
+
413 msecs_to_jiffies(state
->effect
->replay
.length
);
414 state
->adj_at
= state
->play_at
;
416 ml_schedule_timer(ml
);
419 debug("initiated stop");
421 if (test_bit(FF_EFFECT_PLAYING
, &state
->flags
))
422 __set_bit(FF_EFFECT_ABORTING
, &state
->flags
);
424 __clear_bit(FF_EFFECT_STARTED
, &state
->flags
);
429 spin_unlock_irqrestore(&ml
->timer_lock
, flags
);
434 static int ml_ff_upload(struct input_dev
*dev
,
435 struct ff_effect
*effect
, struct ff_effect
*old
)
437 struct ml_device
*ml
= dev
->ff
->private;
438 struct ml_effect_state
*state
= &ml
->states
[effect
->id
];
440 spin_lock_bh(&ml
->timer_lock
);
442 if (test_bit(FF_EFFECT_STARTED
, &state
->flags
)) {
443 __clear_bit(FF_EFFECT_PLAYING
, &state
->flags
);
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 ml_schedule_timer(ml
);
452 spin_unlock_bh(&ml
->timer_lock
);
457 static void ml_ff_destroy(struct ff_device
*ff
)
459 struct ml_device
*ml
= ff
->private;
465 * input_ff_create_memless() - create memoryless force-feedback device
466 * @dev: input device supporting force-feedback
467 * @data: driver-specific data to be passed into @play_effect
468 * @play_effect: driver-specific method for playing FF effect
470 int input_ff_create_memless(struct input_dev
*dev
, void *data
,
471 int (*play_effect
)(struct input_dev
*, void *, struct ff_effect
*))
473 struct ml_device
*ml
;
474 struct ff_device
*ff
;
478 ml
= kzalloc(sizeof(struct ml_device
), GFP_KERNEL
);
484 ml
->play_effect
= play_effect
;
486 spin_lock_init(&ml
->timer_lock
);
487 setup_timer(&ml
->timer
, ml_effect_timer
, (unsigned long)dev
);
489 set_bit(FF_GAIN
, dev
->ffbit
);
491 error
= input_ff_create(dev
, FF_MEMLESS_EFFECTS
);
499 ff
->upload
= ml_ff_upload
;
500 ff
->playback
= ml_ff_playback
;
501 ff
->set_gain
= ml_ff_set_gain
;
502 ff
->destroy
= ml_ff_destroy
;
504 /* we can emulate periodic effects with RUMBLE */
505 if (test_bit(FF_RUMBLE
, ff
->ffbit
)) {
506 set_bit(FF_PERIODIC
, dev
->ffbit
);
507 set_bit(FF_SINE
, dev
->ffbit
);
508 set_bit(FF_TRIANGLE
, dev
->ffbit
);
509 set_bit(FF_SQUARE
, dev
->ffbit
);
512 for (i
= 0; i
< FF_MEMLESS_EFFECTS
; i
++)
513 ml
->states
[i
].effect
= &ff
->effects
[i
];
517 EXPORT_SYMBOL_GPL(input_ff_create_memless
);