2 * Force feedback support for Linux input subsystem
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_BASENAME ": " fmt
28 #include <linux/input.h>
29 #include <linux/module.h>
30 #include <linux/mutex.h>
31 #include <linux/sched.h>
32 #include <linux/slab.h>
35 * Check that the effect_id is a valid effect and whether the user
38 static int check_effect_access(struct ff_device *ff, int effect_id,
41 if (effect_id < 0 || effect_id >= ff->max_effects ||
42 !ff->effect_owners[effect_id])
45 if (file && ff->effect_owners[effect_id] != file)
52 * Checks whether 2 effects can be combined together
54 static inline int check_effects_compatible(struct ff_effect *e1,
57 return e1->type == e2->type &&
58 (e1->type != FF_PERIODIC ||
59 e1->u.periodic.waveform == e2->u.periodic.waveform);
63 * Convert an effect into compatible one
65 static int compat_effect(struct ff_device *ff, struct ff_effect *effect)
69 switch (effect->type) {
71 if (!test_bit(FF_PERIODIC, ff->ffbit))
75 * calculate manginude of sine wave as average of rumble's
76 * 2/3 of strong magnitude and 1/3 of weak magnitude
78 magnitude = effect->u.rumble.strong_magnitude / 3 +
79 effect->u.rumble.weak_magnitude / 6;
81 effect->type = FF_PERIODIC;
82 effect->u.periodic.waveform = FF_SINE;
83 effect->u.periodic.period = 50;
84 effect->u.periodic.magnitude = max(magnitude, 0x7fff);
85 effect->u.periodic.offset = 0;
86 effect->u.periodic.phase = 0;
87 effect->u.periodic.envelope.attack_length = 0;
88 effect->u.periodic.envelope.attack_level = 0;
89 effect->u.periodic.envelope.fade_length = 0;
90 effect->u.periodic.envelope.fade_level = 0;
95 /* Let driver handle conversion */
101 * input_ff_upload() - upload effect into force-feedback device
103 * @effect: effect to be uploaded
104 * @file: owner of the effect
106 int input_ff_upload(struct input_dev *dev, struct ff_effect *effect,
109 struct ff_device *ff = dev->ff;
110 struct ff_effect *old;
114 if (!test_bit(EV_FF, dev->evbit))
117 if (effect->type < FF_EFFECT_MIN || effect->type > FF_EFFECT_MAX ||
118 !test_bit(effect->type, dev->ffbit)) {
119 pr_debug("invalid or not supported effect type in upload\n");
123 if (effect->type == FF_PERIODIC &&
124 (effect->u.periodic.waveform < FF_WAVEFORM_MIN ||
125 effect->u.periodic.waveform > FF_WAVEFORM_MAX ||
126 !test_bit(effect->u.periodic.waveform, dev->ffbit))) {
127 pr_debug("invalid or not supported wave form in upload\n");
131 if (!test_bit(effect->type, ff->ffbit)) {
132 ret = compat_effect(ff, effect);
137 mutex_lock(&ff->mutex);
139 if (effect->id == -1) {
140 for (id = 0; id < ff->max_effects; id++)
141 if (!ff->effect_owners[id])
144 if (id >= ff->max_effects) {
155 ret = check_effect_access(ff, id, file);
159 old = &ff->effects[id];
161 if (!check_effects_compatible(effect, old)) {
167 ret = ff->upload(dev, effect, old);
171 spin_lock_irq(&dev->event_lock);
172 ff->effects[id] = *effect;
173 ff->effect_owners[id] = file;
174 spin_unlock_irq(&dev->event_lock);
177 mutex_unlock(&ff->mutex);
180 EXPORT_SYMBOL_GPL(input_ff_upload);
183 * Erases the effect if the requester is also the effect owner. The mutex
184 * should already be locked before calling this function.
186 static int erase_effect(struct input_dev *dev, int effect_id,
189 struct ff_device *ff = dev->ff;
192 error = check_effect_access(ff, effect_id, file);
196 spin_lock_irq(&dev->event_lock);
197 ff->playback(dev, effect_id, 0);
198 ff->effect_owners[effect_id] = NULL;
199 spin_unlock_irq(&dev->event_lock);
202 error = ff->erase(dev, effect_id);
204 spin_lock_irq(&dev->event_lock);
205 ff->effect_owners[effect_id] = file;
206 spin_unlock_irq(&dev->event_lock);
216 * input_ff_erase - erase a force-feedback effect from device
217 * @dev: input device to erase effect from
218 * @effect_id: id of the ffect to be erased
219 * @file: purported owner of the request
221 * This function erases a force-feedback effect from specified device.
222 * The effect will only be erased if it was uploaded through the same
223 * file handle that is requesting erase.
225 int input_ff_erase(struct input_dev *dev, int effect_id, struct file *file)
227 struct ff_device *ff = dev->ff;
230 if (!test_bit(EV_FF, dev->evbit))
233 mutex_lock(&ff->mutex);
234 ret = erase_effect(dev, effect_id, file);
235 mutex_unlock(&ff->mutex);
239 EXPORT_SYMBOL_GPL(input_ff_erase);
242 * flush_effects - erase all effects owned by a file handle
244 static int flush_effects(struct input_dev *dev, struct file *file)
246 struct ff_device *ff = dev->ff;
249 pr_debug("flushing now\n");
251 mutex_lock(&ff->mutex);
253 for (i = 0; i < ff->max_effects; i++)
254 erase_effect(dev, i, file);
256 mutex_unlock(&ff->mutex);
262 * input_ff_event() - generic handler for force-feedback events
263 * @dev: input device to send the effect to
264 * @type: event type (anything but EV_FF is ignored)
266 * @value: event value
268 int input_ff_event(struct input_dev *dev, unsigned int type,
269 unsigned int code, int value)
271 struct ff_device *ff = dev->ff;
278 if (!test_bit(FF_GAIN, dev->ffbit) || value > 0xffff)
281 ff->set_gain(dev, value);
285 if (!test_bit(FF_AUTOCENTER, dev->ffbit) || value > 0xffff)
288 ff->set_autocenter(dev, value);
292 if (check_effect_access(ff, code, NULL) == 0)
293 ff->playback(dev, code, value);
299 EXPORT_SYMBOL_GPL(input_ff_event);
302 * input_ff_create() - create force-feedback device
303 * @dev: input device supporting force-feedback
304 * @max_effects: maximum number of effects supported by the device
306 * This function allocates all necessary memory for a force feedback
307 * portion of an input device and installs all default handlers.
308 * @dev->ffbit should be already set up before calling this function.
309 * Once ff device is created you need to setup its upload, erase,
310 * playback and other handlers before registering input device
312 int input_ff_create(struct input_dev *dev, int max_effects)
314 struct ff_device *ff;
318 pr_err("cannot allocate device without any effects\n");
322 ff = kzalloc(sizeof(struct ff_device) +
323 max_effects * sizeof(struct file *), GFP_KERNEL);
327 ff->effects = kcalloc(max_effects, sizeof(struct ff_effect),
334 ff->max_effects = max_effects;
335 mutex_init(&ff->mutex);
338 dev->flush = flush_effects;
339 dev->event = input_ff_event;
340 __set_bit(EV_FF, dev->evbit);
342 /* Copy "true" bits into ff device bitmap */
343 for (i = 0; i <= FF_MAX; i++)
344 if (test_bit(i, dev->ffbit))
345 __set_bit(i, ff->ffbit);
347 /* we can emulate RUMBLE with periodic effects */
348 if (test_bit(FF_PERIODIC, ff->ffbit))
349 __set_bit(FF_RUMBLE, dev->ffbit);
353 EXPORT_SYMBOL_GPL(input_ff_create);
356 * input_ff_destroy() - frees force feedback portion of input device
357 * @dev: input device supporting force feedback
359 * This function is only needed in error path as input core will
360 * automatically free force feedback structures when device is
363 void input_ff_destroy(struct input_dev *dev)
365 struct ff_device *ff = dev->ff;
367 __clear_bit(EV_FF, dev->evbit);
377 EXPORT_SYMBOL_GPL(input_ff_destroy);