blob: 9667a5fd6bd782d4a6748635ca4d2ecceb88e3ae [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
28#include <linux/input.h>
29#include <linux/module.h>
30#include <linux/mutex.h>
31#include <linux/spinlock.h>
Tim Schmielaucd354f12007-02-14 00:33:14 -080032#include <linux/jiffies.h>
Anssi Hannula7d928a22006-07-19 01:40:30 -040033
34#include "fixp-arith.h"
35
36MODULE_LICENSE("GPL");
37MODULE_AUTHOR("Anssi Hannula <anssi.hannula@gmail.com>");
38MODULE_DESCRIPTION("Force feedback support for memoryless devices");
39
40/* Number of effects handled with memoryless devices */
41#define FF_MEMLESS_EFFECTS 16
42
43/* Envelope update interval in ms */
44#define FF_ENVELOPE_INTERVAL 50
45
46#define FF_EFFECT_STARTED 0
47#define FF_EFFECT_PLAYING 1
48#define FF_EFFECT_ABORTING 2
49
50struct 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 */
57};
58
59struct ml_device {
60 void *private;
61 struct ml_effect_state states[FF_MEMLESS_EFFECTS];
62 int gain;
63 struct timer_list timer;
Anssi Hannula7d928a22006-07-19 01:40:30 -040064 struct input_dev *dev;
65
66 int (*play_effect)(struct input_dev *dev, void *data,
67 struct ff_effect *effect);
68};
69
70static const struct ff_envelope *get_envelope(const struct ff_effect *effect)
71{
72 static const struct ff_envelope empty_envelope;
73
74 switch (effect->type) {
75 case FF_PERIODIC:
76 return &effect->u.periodic.envelope;
77 case FF_CONSTANT:
78 return &effect->u.constant.envelope;
79 default:
80 return &empty_envelope;
81 }
82}
83
84/*
85 * Check for the next time envelope requires an update on memoryless devices
86 */
87static unsigned long calculate_next_time(struct ml_effect_state *state)
88{
89 const struct ff_envelope *envelope = get_envelope(state->effect);
90 unsigned long attack_stop, fade_start, next_fade;
91
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);
98 }
99
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);
105
106 if (time_before(state->adj_at, fade_start))
107 return fade_start;
108
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))
113 return next_fade;
114 }
115
116 return state->stop_at;
117 }
118
119 return state->play_at;
120}
121
122static void ml_schedule_timer(struct ml_device *ml)
123{
124 struct ml_effect_state *state;
125 unsigned long now = jiffies;
126 unsigned long earliest = 0;
127 unsigned long next_at;
128 int events = 0;
129 int i;
130
131 debug("calculating next timer");
132
133 for (i = 0; i < FF_MEMLESS_EFFECTS; i++) {
134
135 state = &ml->states[i];
136
137 if (!test_bit(FF_EFFECT_STARTED, &state->flags))
138 continue;
139
140 if (test_bit(FF_EFFECT_PLAYING, &state->flags))
141 next_at = calculate_next_time(state);
142 else
143 next_at = state->play_at;
144
145 if (time_before_eq(now, next_at) &&
146 (++events == 1 || time_before(next_at, earliest)))
147 earliest = next_at;
148 }
149
150 if (!events) {
151 debug("no actions");
152 del_timer(&ml->timer);
153 } else {
154 debug("timer set");
155 mod_timer(&ml->timer, earliest);
156 }
157}
158
159/*
160 * Apply an envelope to a value
161 */
162static int apply_envelope(struct ml_effect_state *state, int value,
163 struct ff_envelope *envelope)
164{
165 struct ff_effect *effect = state->effect;
166 unsigned long now = jiffies;
167 int time_from_level;
168 int time_of_envelope;
169 int envelope_level;
170 int difference;
171
172 if (envelope->attack_length &&
173 time_before(now,
174 state->play_at + msecs_to_jiffies(envelope->attack_length))) {
175 debug("value = 0x%x, attack_level = 0x%x", value,
176 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(__s16, envelope->attack_level, 0x7fff);
180
181 } else if (envelope->fade_length && effect->replay.length &&
182 time_after(now,
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(__s16, envelope->fade_level, 0x7fff);
188 } else
189 return value;
190
191 difference = abs(value) - envelope_level;
192
193 debug("difference = %d", difference);
194 debug("time_from_level = 0x%x", time_from_level);
195 debug("time_of_envelope = 0x%x", time_of_envelope);
196
197 difference = difference * time_from_level / time_of_envelope;
198
199 debug("difference = %d", difference);
200
201 return value < 0 ?
202 -(difference + envelope_level) : (difference + envelope_level);
203}
204
205/*
206 * Return the type the effect has to be converted into (memless devices)
207 */
208static int get_compatible_type(struct ff_device *ff, int effect_type)
209{
210
211 if (test_bit(effect_type, ff->ffbit))
212 return effect_type;
213
214 if (effect_type == FF_PERIODIC && test_bit(FF_RUMBLE, ff->ffbit))
215 return FF_RUMBLE;
216
217 printk(KERN_ERR
218 "ff-memless: invalid type in get_compatible_type()\n");
219
220 return 0;
221}
222
223/*
224 * Combine two effects and apply gain.
225 */
226static void ml_combine_effects(struct ff_effect *effect,
227 struct ml_effect_state *state,
Dmitry Torokhov1b11c882009-12-24 21:44:19 -0800228 int gain)
Anssi Hannula7d928a22006-07-19 01:40:30 -0400229{
230 struct ff_effect *new = state->effect;
231 unsigned int strong, weak, i;
232 int x, y;
233 fixp_t level;
234
235 switch (new->type) {
236 case FF_CONSTANT:
237 i = new->direction * 360 / 0xffff;
238 level = fixp_new16(apply_envelope(state,
239 new->u.constant.level,
240 &new->u.constant.envelope));
241 x = fixp_mult(fixp_sin(i), level) * gain / 0xffff;
242 y = fixp_mult(-fixp_cos(i), level) * gain / 0xffff;
243 /*
244 * here we abuse ff_ramp to hold x and y of constant force
245 * If in future any driver wants something else than x and y
246 * in s8, this should be changed to something more generic
247 */
248 effect->u.ramp.start_level =
Harvey Harrison92310472008-05-05 11:50:59 -0400249 clamp_val(effect->u.ramp.start_level + x, -0x80, 0x7f);
Anssi Hannula7d928a22006-07-19 01:40:30 -0400250 effect->u.ramp.end_level =
Harvey Harrison92310472008-05-05 11:50:59 -0400251 clamp_val(effect->u.ramp.end_level + y, -0x80, 0x7f);
Anssi Hannula7d928a22006-07-19 01:40:30 -0400252 break;
253
254 case FF_RUMBLE:
Dmitry Torokhov1b11c882009-12-24 21:44:19 -0800255 strong = (u32)new->u.rumble.strong_magnitude * gain / 0xffff;
256 weak = (u32)new->u.rumble.weak_magnitude * gain / 0xffff;
Anssi Hannula7d928a22006-07-19 01:40:30 -0400257 effect->u.rumble.strong_magnitude =
258 min(strong + effect->u.rumble.strong_magnitude,
259 0xffffU);
260 effect->u.rumble.weak_magnitude =
261 min(weak + effect->u.rumble.weak_magnitude, 0xffffU);
262 break;
263
264 case FF_PERIODIC:
265 i = apply_envelope(state, abs(new->u.periodic.magnitude),
266 &new->u.periodic.envelope);
267
268 /* here we also scale it 0x7fff => 0xffff */
269 i = i * gain / 0x7fff;
270
271 effect->u.rumble.strong_magnitude =
272 min(i + effect->u.rumble.strong_magnitude, 0xffffU);
273 effect->u.rumble.weak_magnitude =
274 min(i + effect->u.rumble.weak_magnitude, 0xffffU);
275 break;
276
277 default:
278 printk(KERN_ERR "ff-memless: invalid type in ml_combine_effects()\n");
279 break;
280 }
281
282}
283
284
285/*
286 * Because memoryless devices have only one effect per effect type active
287 * at one time we have to combine multiple effects into one
288 */
289static int ml_get_combo_effect(struct ml_device *ml,
290 unsigned long *effect_handled,
291 struct ff_effect *combo_effect)
292{
293 struct ff_effect *effect;
294 struct ml_effect_state *state;
295 int effect_type;
296 int i;
297
298 memset(combo_effect, 0, sizeof(struct ff_effect));
299
300 for (i = 0; i < FF_MEMLESS_EFFECTS; i++) {
301 if (__test_and_set_bit(i, effect_handled))
302 continue;
303
304 state = &ml->states[i];
305 effect = state->effect;
306
307 if (!test_bit(FF_EFFECT_STARTED, &state->flags))
308 continue;
309
310 if (time_before(jiffies, state->play_at))
311 continue;
312
313 /*
314 * here we have started effects that are either
315 * currently playing (and may need be aborted)
316 * or need to start playing.
317 */
318 effect_type = get_compatible_type(ml->dev->ff, effect->type);
319 if (combo_effect->type != effect_type) {
320 if (combo_effect->type != 0) {
321 __clear_bit(i, effect_handled);
322 continue;
323 }
324 combo_effect->type = effect_type;
325 }
326
327 if (__test_and_clear_bit(FF_EFFECT_ABORTING, &state->flags)) {
328 __clear_bit(FF_EFFECT_PLAYING, &state->flags);
329 __clear_bit(FF_EFFECT_STARTED, &state->flags);
330 } else if (effect->replay.length &&
331 time_after_eq(jiffies, state->stop_at)) {
332
333 __clear_bit(FF_EFFECT_PLAYING, &state->flags);
334
335 if (--state->count <= 0) {
336 __clear_bit(FF_EFFECT_STARTED, &state->flags);
337 } else {
338 state->play_at = jiffies +
339 msecs_to_jiffies(effect->replay.delay);
340 state->stop_at = state->play_at +
341 msecs_to_jiffies(effect->replay.length);
342 }
343 } else {
344 __set_bit(FF_EFFECT_PLAYING, &state->flags);
345 state->adj_at = jiffies;
346 ml_combine_effects(combo_effect, state, ml->gain);
347 }
348 }
349
350 return combo_effect->type != 0;
351}
352
353static void ml_play_effects(struct ml_device *ml)
354{
355 struct ff_effect effect;
356 DECLARE_BITMAP(handled_bm, FF_MEMLESS_EFFECTS);
357
358 memset(handled_bm, 0, sizeof(handled_bm));
359
360 while (ml_get_combo_effect(ml, handled_bm, &effect))
361 ml->play_effect(ml->dev, ml->private, &effect);
362
363 ml_schedule_timer(ml);
364}
365
366static void ml_effect_timer(unsigned long timer_data)
367{
368 struct input_dev *dev = (struct input_dev *)timer_data;
369 struct ml_device *ml = dev->ff->private;
Dmitry Torokhovbf3204c2009-11-06 21:39:07 -0800370 unsigned long flags;
Anssi Hannula7d928a22006-07-19 01:40:30 -0400371
372 debug("timer: updating effects");
373
Dmitry Torokhovbf3204c2009-11-06 21:39:07 -0800374 spin_lock_irqsave(&dev->event_lock, flags);
Anssi Hannula7d928a22006-07-19 01:40:30 -0400375 ml_play_effects(ml);
Dmitry Torokhovbf3204c2009-11-06 21:39:07 -0800376 spin_unlock_irqrestore(&dev->event_lock, flags);
Anssi Hannula7d928a22006-07-19 01:40:30 -0400377}
378
Dmitry Torokhovbf3204c2009-11-06 21:39:07 -0800379/*
380 * Sets requested gain for FF effects. Called with dev->event_lock held.
381 */
Anssi Hannula7d928a22006-07-19 01:40:30 -0400382static void ml_ff_set_gain(struct input_dev *dev, u16 gain)
383{
384 struct ml_device *ml = dev->ff->private;
385 int i;
386
Anssi Hannula7d928a22006-07-19 01:40:30 -0400387 ml->gain = gain;
388
389 for (i = 0; i < FF_MEMLESS_EFFECTS; i++)
390 __clear_bit(FF_EFFECT_PLAYING, &ml->states[i].flags);
391
392 ml_play_effects(ml);
Anssi Hannula7d928a22006-07-19 01:40:30 -0400393}
394
Dmitry Torokhovbf3204c2009-11-06 21:39:07 -0800395/*
396 * Start/stop specified FF effect. Called with dev->event_lock held.
397 */
Anssi Hannula7d928a22006-07-19 01:40:30 -0400398static int ml_ff_playback(struct input_dev *dev, int effect_id, int value)
399{
400 struct ml_device *ml = dev->ff->private;
401 struct ml_effect_state *state = &ml->states[effect_id];
Anssi Hannula7d928a22006-07-19 01:40:30 -0400402
403 if (value > 0) {
404 debug("initiated play");
405
406 __set_bit(FF_EFFECT_STARTED, &state->flags);
407 state->count = value;
408 state->play_at = jiffies +
409 msecs_to_jiffies(state->effect->replay.delay);
410 state->stop_at = state->play_at +
411 msecs_to_jiffies(state->effect->replay.length);
412 state->adj_at = state->play_at;
413
Anssi Hannula7d928a22006-07-19 01:40:30 -0400414 } else {
415 debug("initiated stop");
416
417 if (test_bit(FF_EFFECT_PLAYING, &state->flags))
418 __set_bit(FF_EFFECT_ABORTING, &state->flags);
419 else
420 __clear_bit(FF_EFFECT_STARTED, &state->flags);
Anssi Hannula7d928a22006-07-19 01:40:30 -0400421 }
422
Jari Vanhala25ae0832009-12-24 21:52:19 -0800423 ml_play_effects(ml);
424
Anssi Hannula7d928a22006-07-19 01:40:30 -0400425 return 0;
426}
427
428static int ml_ff_upload(struct input_dev *dev,
429 struct ff_effect *effect, struct ff_effect *old)
430{
431 struct ml_device *ml = dev->ff->private;
432 struct ml_effect_state *state = &ml->states[effect->id];
433
Dmitry Torokhovbf3204c2009-11-06 21:39:07 -0800434 spin_lock_irq(&dev->event_lock);
Anssi Hannula7d928a22006-07-19 01:40:30 -0400435
436 if (test_bit(FF_EFFECT_STARTED, &state->flags)) {
437 __clear_bit(FF_EFFECT_PLAYING, &state->flags);
438 state->play_at = jiffies +
439 msecs_to_jiffies(state->effect->replay.delay);
440 state->stop_at = state->play_at +
441 msecs_to_jiffies(state->effect->replay.length);
442 state->adj_at = state->play_at;
443 ml_schedule_timer(ml);
444 }
445
Dmitry Torokhovbf3204c2009-11-06 21:39:07 -0800446 spin_unlock_irq(&dev->event_lock);
Anssi Hannula7d928a22006-07-19 01:40:30 -0400447
448 return 0;
449}
450
451static void ml_ff_destroy(struct ff_device *ff)
452{
453 struct ml_device *ml = ff->private;
454
455 kfree(ml->private);
456}
457
458/**
Randy Dunlape4477d22006-11-24 00:43:09 -0500459 * input_ff_create_memless() - create memoryless force-feedback device
Anssi Hannula7d928a22006-07-19 01:40:30 -0400460 * @dev: input device supporting force-feedback
461 * @data: driver-specific data to be passed into @play_effect
462 * @play_effect: driver-specific method for playing FF effect
463 */
464int input_ff_create_memless(struct input_dev *dev, void *data,
465 int (*play_effect)(struct input_dev *, void *, struct ff_effect *))
466{
467 struct ml_device *ml;
468 struct ff_device *ff;
469 int error;
470 int i;
471
472 ml = kzalloc(sizeof(struct ml_device), GFP_KERNEL);
473 if (!ml)
474 return -ENOMEM;
475
476 ml->dev = dev;
477 ml->private = data;
478 ml->play_effect = play_effect;
479 ml->gain = 0xffff;
Anssi Hannula7d928a22006-07-19 01:40:30 -0400480 setup_timer(&ml->timer, ml_effect_timer, (unsigned long)dev);
481
482 set_bit(FF_GAIN, dev->ffbit);
483
484 error = input_ff_create(dev, FF_MEMLESS_EFFECTS);
485 if (error) {
486 kfree(ml);
487 return error;
488 }
489
490 ff = dev->ff;
491 ff->private = ml;
492 ff->upload = ml_ff_upload;
493 ff->playback = ml_ff_playback;
494 ff->set_gain = ml_ff_set_gain;
495 ff->destroy = ml_ff_destroy;
496
497 /* we can emulate periodic effects with RUMBLE */
498 if (test_bit(FF_RUMBLE, ff->ffbit)) {
499 set_bit(FF_PERIODIC, dev->ffbit);
500 set_bit(FF_SINE, dev->ffbit);
501 set_bit(FF_TRIANGLE, dev->ffbit);
502 set_bit(FF_SQUARE, dev->ffbit);
503 }
504
505 for (i = 0; i < FF_MEMLESS_EFFECTS; i++)
506 ml->states[i].effect = &ff->effects[i];
507
508 return 0;
509}
510EXPORT_SYMBOL_GPL(input_ff_create_memless);