blob: 1d881c96ba8fc2c0db1c5ce289af448288ff42a5 [file] [log] [blame]
Anssi Hannula7d928a22006-07-19 01:40:30 -04001/*
2 * Force feedback support for memoryless devices
3 *
4 * Copyright (c) 2006 Anssi Hannula <anssi.hannula@gmail.com>
5 * Copyright (c) 2006 Dmitry Torokhov <dtor@mail.ru>
6 */
7
8/*
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.
13 *
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.
18 *
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
22 */
23
24/* #define DEBUG */
25
26#define debug(format, arg...) pr_debug("ff-memless: " format "\n", ## arg)
27
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Anssi Hannula7d928a22006-07-19 01:40:30 -040029#include <linux/input.h>
30#include <linux/module.h>
31#include <linux/mutex.h>
32#include <linux/spinlock.h>
Tim Schmielaucd354f12007-02-14 00:33:14 -080033#include <linux/jiffies.h>
Anssi Hannula7d928a22006-07-19 01:40:30 -040034
35#include "fixp-arith.h"
36
37MODULE_LICENSE("GPL");
38MODULE_AUTHOR("Anssi Hannula <anssi.hannula@gmail.com>");
39MODULE_DESCRIPTION("Force feedback support for memoryless devices");
40
41/* Number of effects handled with memoryless devices */
42#define FF_MEMLESS_EFFECTS 16
43
44/* Envelope update interval in ms */
45#define FF_ENVELOPE_INTERVAL 50
46
47#define FF_EFFECT_STARTED 0
48#define FF_EFFECT_PLAYING 1
49#define FF_EFFECT_ABORTING 2
50
51struct 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 */
58};
59
60struct ml_device {
61 void *private;
62 struct ml_effect_state states[FF_MEMLESS_EFFECTS];
63 int gain;
64 struct timer_list timer;
Anssi Hannula7d928a22006-07-19 01:40:30 -040065 struct input_dev *dev;
66
67 int (*play_effect)(struct input_dev *dev, void *data,
68 struct ff_effect *effect);
69};
70
71static const struct ff_envelope *get_envelope(const struct ff_effect *effect)
72{
73 static const struct ff_envelope empty_envelope;
74
75 switch (effect->type) {
76 case FF_PERIODIC:
77 return &effect->u.periodic.envelope;
78 case FF_CONSTANT:
79 return &effect->u.constant.envelope;
80 default:
81 return &empty_envelope;
82 }
83}
84
85/*
86 * Check for the next time envelope requires an update on memoryless devices
87 */
88static unsigned long calculate_next_time(struct ml_effect_state *state)
89{
90 const struct ff_envelope *envelope = get_envelope(state->effect);
91 unsigned long attack_stop, fade_start, next_fade;
92
93 if (envelope->attack_length) {
94 attack_stop = state->play_at +
95 msecs_to_jiffies(envelope->attack_length);
96 if (time_before(state->adj_at, attack_stop))
97 return state->adj_at +
98 msecs_to_jiffies(FF_ENVELOPE_INTERVAL);
99 }
100
101 if (state->effect->replay.length) {
102 if (envelope->fade_length) {
103 /* check when fading should start */
104 fade_start = state->stop_at -
105 msecs_to_jiffies(envelope->fade_length);
106
107 if (time_before(state->adj_at, fade_start))
108 return fade_start;
109
110 /* already fading, advance to next checkpoint */
111 next_fade = state->adj_at +
112 msecs_to_jiffies(FF_ENVELOPE_INTERVAL);
113 if (time_before(next_fade, state->stop_at))
114 return next_fade;
115 }
116
117 return state->stop_at;
118 }
119
120 return state->play_at;
121}
122
123static void ml_schedule_timer(struct ml_device *ml)
124{
125 struct ml_effect_state *state;
126 unsigned long now = jiffies;
127 unsigned long earliest = 0;
128 unsigned long next_at;
129 int events = 0;
130 int i;
131
132 debug("calculating next timer");
133
134 for (i = 0; i < FF_MEMLESS_EFFECTS; i++) {
135
136 state = &ml->states[i];
137
138 if (!test_bit(FF_EFFECT_STARTED, &state->flags))
139 continue;
140
141 if (test_bit(FF_EFFECT_PLAYING, &state->flags))
142 next_at = calculate_next_time(state);
143 else
144 next_at = state->play_at;
145
146 if (time_before_eq(now, next_at) &&
147 (++events == 1 || time_before(next_at, earliest)))
148 earliest = next_at;
149 }
150
151 if (!events) {
152 debug("no actions");
153 del_timer(&ml->timer);
154 } else {
155 debug("timer set");
156 mod_timer(&ml->timer, earliest);
157 }
158}
159
160/*
161 * Apply an envelope to a value
162 */
163static int apply_envelope(struct ml_effect_state *state, int value,
164 struct ff_envelope *envelope)
165{
166 struct ff_effect *effect = state->effect;
167 unsigned long now = jiffies;
168 int time_from_level;
169 int time_of_envelope;
170 int envelope_level;
171 int difference;
172
173 if (envelope->attack_length &&
174 time_before(now,
175 state->play_at + msecs_to_jiffies(envelope->attack_length))) {
176 debug("value = 0x%x, attack_level = 0x%x", value,
177 envelope->attack_level);
178 time_from_level = jiffies_to_msecs(now - state->play_at);
179 time_of_envelope = envelope->attack_length;
180 envelope_level = min_t(__s16, envelope->attack_level, 0x7fff);
181
182 } else if (envelope->fade_length && effect->replay.length &&
183 time_after(now,
184 state->stop_at - msecs_to_jiffies(envelope->fade_length)) &&
185 time_before(now, state->stop_at)) {
186 time_from_level = jiffies_to_msecs(state->stop_at - now);
187 time_of_envelope = envelope->fade_length;
188 envelope_level = min_t(__s16, envelope->fade_level, 0x7fff);
189 } else
190 return value;
191
192 difference = abs(value) - envelope_level;
193
194 debug("difference = %d", difference);
195 debug("time_from_level = 0x%x", time_from_level);
196 debug("time_of_envelope = 0x%x", time_of_envelope);
197
198 difference = difference * time_from_level / time_of_envelope;
199
200 debug("difference = %d", difference);
201
202 return value < 0 ?
203 -(difference + envelope_level) : (difference + envelope_level);
204}
205
206/*
207 * Return the type the effect has to be converted into (memless devices)
208 */
209static int get_compatible_type(struct ff_device *ff, int effect_type)
210{
211
212 if (test_bit(effect_type, ff->ffbit))
213 return effect_type;
214
215 if (effect_type == FF_PERIODIC && test_bit(FF_RUMBLE, ff->ffbit))
216 return FF_RUMBLE;
217
218 printk(KERN_ERR
219 "ff-memless: invalid type in get_compatible_type()\n");
220
221 return 0;
222}
223
224/*
Jari Vanhala94ec26c2009-12-24 21:52:20 -0800225 * Only left/right direction should be used (under/over 0x8000) for
226 * forward/reverse motor direction (to keep calculation fast & simple).
227 */
228static u16 ml_calculate_direction(u16 direction, u16 force,
229 u16 new_direction, u16 new_force)
230{
231 if (!force)
232 return new_direction;
233 if (!new_force)
234 return direction;
235 return (((u32)(direction >> 1) * force +
236 (new_direction >> 1) * new_force) /
237 (force + new_force)) << 1;
238}
239
240/*
Anssi Hannula7d928a22006-07-19 01:40:30 -0400241 * Combine two effects and apply gain.
242 */
243static void ml_combine_effects(struct ff_effect *effect,
244 struct ml_effect_state *state,
Dmitry Torokhov1b11c882009-12-24 21:44:19 -0800245 int gain)
Anssi Hannula7d928a22006-07-19 01:40:30 -0400246{
247 struct ff_effect *new = state->effect;
248 unsigned int strong, weak, i;
249 int x, y;
250 fixp_t level;
251
252 switch (new->type) {
253 case FF_CONSTANT:
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;
260 /*
261 * here we abuse ff_ramp to hold x and y of constant force
262 * If in future any driver wants something else than x and y
263 * in s8, this should be changed to something more generic
264 */
265 effect->u.ramp.start_level =
Harvey Harrison92310472008-05-05 11:50:59 -0400266 clamp_val(effect->u.ramp.start_level + x, -0x80, 0x7f);
Anssi Hannula7d928a22006-07-19 01:40:30 -0400267 effect->u.ramp.end_level =
Harvey Harrison92310472008-05-05 11:50:59 -0400268 clamp_val(effect->u.ramp.end_level + y, -0x80, 0x7f);
Anssi Hannula7d928a22006-07-19 01:40:30 -0400269 break;
270
271 case FF_RUMBLE:
Dmitry Torokhov1b11c882009-12-24 21:44:19 -0800272 strong = (u32)new->u.rumble.strong_magnitude * gain / 0xffff;
273 weak = (u32)new->u.rumble.weak_magnitude * gain / 0xffff;
Jari Vanhala94ec26c2009-12-24 21:52:20 -0800274
275 if (effect->u.rumble.strong_magnitude + strong)
276 effect->direction = ml_calculate_direction(
277 effect->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(
282 effect->direction,
283 effect->u.rumble.weak_magnitude,
284 new->direction, weak);
285 else
286 effect->direction = 0;
Anssi Hannula7d928a22006-07-19 01:40:30 -0400287 effect->u.rumble.strong_magnitude =
288 min(strong + effect->u.rumble.strong_magnitude,
289 0xffffU);
290 effect->u.rumble.weak_magnitude =
291 min(weak + effect->u.rumble.weak_magnitude, 0xffffU);
292 break;
293
294 case FF_PERIODIC:
295 i = apply_envelope(state, abs(new->u.periodic.magnitude),
296 &new->u.periodic.envelope);
297
298 /* here we also scale it 0x7fff => 0xffff */
299 i = i * gain / 0x7fff;
300
Jari Vanhala94ec26c2009-12-24 21:52:20 -0800301 if (effect->u.rumble.strong_magnitude + i)
302 effect->direction = ml_calculate_direction(
303 effect->direction,
304 effect->u.rumble.strong_magnitude,
305 new->direction, i);
306 else
307 effect->direction = 0;
Anssi Hannula7d928a22006-07-19 01:40:30 -0400308 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);
312 break;
313
314 default:
315 printk(KERN_ERR "ff-memless: invalid type in ml_combine_effects()\n");
316 break;
317 }
318
319}
320
321
322/*
323 * Because memoryless devices have only one effect per effect type active
324 * at one time we have to combine multiple effects into one
325 */
326static int ml_get_combo_effect(struct ml_device *ml,
327 unsigned long *effect_handled,
328 struct ff_effect *combo_effect)
329{
330 struct ff_effect *effect;
331 struct ml_effect_state *state;
332 int effect_type;
333 int i;
334
335 memset(combo_effect, 0, sizeof(struct ff_effect));
336
337 for (i = 0; i < FF_MEMLESS_EFFECTS; i++) {
338 if (__test_and_set_bit(i, effect_handled))
339 continue;
340
341 state = &ml->states[i];
342 effect = state->effect;
343
344 if (!test_bit(FF_EFFECT_STARTED, &state->flags))
345 continue;
346
347 if (time_before(jiffies, state->play_at))
348 continue;
349
350 /*
351 * here we have started effects that are either
352 * currently playing (and may need be aborted)
353 * or need to start playing.
354 */
355 effect_type = get_compatible_type(ml->dev->ff, effect->type);
356 if (combo_effect->type != effect_type) {
357 if (combo_effect->type != 0) {
358 __clear_bit(i, effect_handled);
359 continue;
360 }
361 combo_effect->type = effect_type;
362 }
363
364 if (__test_and_clear_bit(FF_EFFECT_ABORTING, &state->flags)) {
365 __clear_bit(FF_EFFECT_PLAYING, &state->flags);
366 __clear_bit(FF_EFFECT_STARTED, &state->flags);
367 } else if (effect->replay.length &&
368 time_after_eq(jiffies, state->stop_at)) {
369
370 __clear_bit(FF_EFFECT_PLAYING, &state->flags);
371
372 if (--state->count <= 0) {
373 __clear_bit(FF_EFFECT_STARTED, &state->flags);
374 } else {
375 state->play_at = jiffies +
376 msecs_to_jiffies(effect->replay.delay);
377 state->stop_at = state->play_at +
378 msecs_to_jiffies(effect->replay.length);
379 }
380 } else {
381 __set_bit(FF_EFFECT_PLAYING, &state->flags);
382 state->adj_at = jiffies;
383 ml_combine_effects(combo_effect, state, ml->gain);
384 }
385 }
386
387 return combo_effect->type != 0;
388}
389
390static void ml_play_effects(struct ml_device *ml)
391{
392 struct ff_effect effect;
393 DECLARE_BITMAP(handled_bm, FF_MEMLESS_EFFECTS);
394
395 memset(handled_bm, 0, sizeof(handled_bm));
396
397 while (ml_get_combo_effect(ml, handled_bm, &effect))
398 ml->play_effect(ml->dev, ml->private, &effect);
399
400 ml_schedule_timer(ml);
401}
402
403static void ml_effect_timer(unsigned long timer_data)
404{
405 struct input_dev *dev = (struct input_dev *)timer_data;
406 struct ml_device *ml = dev->ff->private;
Dmitry Torokhovbf3204c2009-11-06 21:39:07 -0800407 unsigned long flags;
Anssi Hannula7d928a22006-07-19 01:40:30 -0400408
409 debug("timer: updating effects");
410
Dmitry Torokhovbf3204c2009-11-06 21:39:07 -0800411 spin_lock_irqsave(&dev->event_lock, flags);
Anssi Hannula7d928a22006-07-19 01:40:30 -0400412 ml_play_effects(ml);
Dmitry Torokhovbf3204c2009-11-06 21:39:07 -0800413 spin_unlock_irqrestore(&dev->event_lock, flags);
Anssi Hannula7d928a22006-07-19 01:40:30 -0400414}
415
Dmitry Torokhovbf3204c2009-11-06 21:39:07 -0800416/*
417 * Sets requested gain for FF effects. Called with dev->event_lock held.
418 */
Anssi Hannula7d928a22006-07-19 01:40:30 -0400419static void ml_ff_set_gain(struct input_dev *dev, u16 gain)
420{
421 struct ml_device *ml = dev->ff->private;
422 int i;
423
Anssi Hannula7d928a22006-07-19 01:40:30 -0400424 ml->gain = gain;
425
426 for (i = 0; i < FF_MEMLESS_EFFECTS; i++)
427 __clear_bit(FF_EFFECT_PLAYING, &ml->states[i].flags);
428
429 ml_play_effects(ml);
Anssi Hannula7d928a22006-07-19 01:40:30 -0400430}
431
Dmitry Torokhovbf3204c2009-11-06 21:39:07 -0800432/*
433 * Start/stop specified FF effect. Called with dev->event_lock held.
434 */
Anssi Hannula7d928a22006-07-19 01:40:30 -0400435static int ml_ff_playback(struct input_dev *dev, int effect_id, int value)
436{
437 struct ml_device *ml = dev->ff->private;
438 struct ml_effect_state *state = &ml->states[effect_id];
Anssi Hannula7d928a22006-07-19 01:40:30 -0400439
440 if (value > 0) {
441 debug("initiated play");
442
443 __set_bit(FF_EFFECT_STARTED, &state->flags);
444 state->count = value;
445 state->play_at = jiffies +
446 msecs_to_jiffies(state->effect->replay.delay);
447 state->stop_at = state->play_at +
448 msecs_to_jiffies(state->effect->replay.length);
449 state->adj_at = state->play_at;
450
Anssi Hannula7d928a22006-07-19 01:40:30 -0400451 } else {
452 debug("initiated stop");
453
454 if (test_bit(FF_EFFECT_PLAYING, &state->flags))
455 __set_bit(FF_EFFECT_ABORTING, &state->flags);
456 else
457 __clear_bit(FF_EFFECT_STARTED, &state->flags);
Anssi Hannula7d928a22006-07-19 01:40:30 -0400458 }
459
Jari Vanhala25ae0832009-12-24 21:52:19 -0800460 ml_play_effects(ml);
461
Anssi Hannula7d928a22006-07-19 01:40:30 -0400462 return 0;
463}
464
465static int ml_ff_upload(struct input_dev *dev,
466 struct ff_effect *effect, struct ff_effect *old)
467{
468 struct ml_device *ml = dev->ff->private;
469 struct ml_effect_state *state = &ml->states[effect->id];
470
Dmitry Torokhovbf3204c2009-11-06 21:39:07 -0800471 spin_lock_irq(&dev->event_lock);
Anssi Hannula7d928a22006-07-19 01:40:30 -0400472
473 if (test_bit(FF_EFFECT_STARTED, &state->flags)) {
474 __clear_bit(FF_EFFECT_PLAYING, &state->flags);
475 state->play_at = jiffies +
476 msecs_to_jiffies(state->effect->replay.delay);
477 state->stop_at = state->play_at +
478 msecs_to_jiffies(state->effect->replay.length);
479 state->adj_at = state->play_at;
480 ml_schedule_timer(ml);
481 }
482
Dmitry Torokhovbf3204c2009-11-06 21:39:07 -0800483 spin_unlock_irq(&dev->event_lock);
Anssi Hannula7d928a22006-07-19 01:40:30 -0400484
485 return 0;
486}
487
488static void ml_ff_destroy(struct ff_device *ff)
489{
490 struct ml_device *ml = ff->private;
491
492 kfree(ml->private);
493}
494
495/**
Randy Dunlape4477d22006-11-24 00:43:09 -0500496 * input_ff_create_memless() - create memoryless force-feedback device
Anssi Hannula7d928a22006-07-19 01:40:30 -0400497 * @dev: input device supporting force-feedback
498 * @data: driver-specific data to be passed into @play_effect
499 * @play_effect: driver-specific method for playing FF effect
500 */
501int input_ff_create_memless(struct input_dev *dev, void *data,
502 int (*play_effect)(struct input_dev *, void *, struct ff_effect *))
503{
504 struct ml_device *ml;
505 struct ff_device *ff;
506 int error;
507 int i;
508
509 ml = kzalloc(sizeof(struct ml_device), GFP_KERNEL);
510 if (!ml)
511 return -ENOMEM;
512
513 ml->dev = dev;
514 ml->private = data;
515 ml->play_effect = play_effect;
516 ml->gain = 0xffff;
Anssi Hannula7d928a22006-07-19 01:40:30 -0400517 setup_timer(&ml->timer, ml_effect_timer, (unsigned long)dev);
518
519 set_bit(FF_GAIN, dev->ffbit);
520
521 error = input_ff_create(dev, FF_MEMLESS_EFFECTS);
522 if (error) {
523 kfree(ml);
524 return error;
525 }
526
527 ff = dev->ff;
528 ff->private = ml;
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;
533
534 /* we can emulate periodic effects with RUMBLE */
535 if (test_bit(FF_RUMBLE, ff->ffbit)) {
536 set_bit(FF_PERIODIC, dev->ffbit);
537 set_bit(FF_SINE, dev->ffbit);
538 set_bit(FF_TRIANGLE, dev->ffbit);
539 set_bit(FF_SQUARE, dev->ffbit);
540 }
541
542 for (i = 0; i < FF_MEMLESS_EFFECTS; i++)
543 ml->states[i].effect = &ff->effects[i];
544
545 return 0;
546}
547EXPORT_SYMBOL_GPL(input_ff_create_memless);