1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Force feedback support for Linux input subsystem
5 * Copyright (c) 2006 Anssi Hannula <anssi.hannula@gmail.com>
6 * Copyright (c) 2006 Dmitry Torokhov <dtor@mail.ru>
11 #include <linux/input.h>
12 #include <linux/module.h>
13 #include <linux/mutex.h>
14 #include <linux/sched.h>
15 #include <linux/slab.h>
18 * Check that the effect_id is a valid effect and whether the user
21 static int check_effect_access(struct ff_device *ff, int effect_id,
24 if (effect_id < 0 || effect_id >= ff->max_effects ||
25 !ff->effect_owners[effect_id])
28 if (file && ff->effect_owners[effect_id] != file)
35 * Checks whether 2 effects can be combined together
37 static inline int check_effects_compatible(struct ff_effect *e1,
40 return e1->type == e2->type &&
41 (e1->type != FF_PERIODIC ||
42 e1->u.periodic.waveform == e2->u.periodic.waveform);
46 * Convert an effect into compatible one
48 static int compat_effect(struct ff_device *ff, struct ff_effect *effect)
52 switch (effect->type) {
54 if (!test_bit(FF_PERIODIC, ff->ffbit))
58 * calculate magnitude of sine wave as average of rumble's
59 * 2/3 of strong magnitude and 1/3 of weak magnitude
61 magnitude = effect->u.rumble.strong_magnitude / 3 +
62 effect->u.rumble.weak_magnitude / 6;
64 effect->type = FF_PERIODIC;
65 effect->u.periodic.waveform = FF_SINE;
66 effect->u.periodic.period = 50;
67 effect->u.periodic.magnitude = magnitude;
68 effect->u.periodic.offset = 0;
69 effect->u.periodic.phase = 0;
70 effect->u.periodic.envelope.attack_length = 0;
71 effect->u.periodic.envelope.attack_level = 0;
72 effect->u.periodic.envelope.fade_length = 0;
73 effect->u.periodic.envelope.fade_level = 0;
78 /* Let driver handle conversion */
84 * input_ff_upload() - upload effect into force-feedback device
86 * @effect: effect to be uploaded
87 * @file: owner of the effect
89 int input_ff_upload(struct input_dev *dev, struct ff_effect *effect,
92 struct ff_device *ff = dev->ff;
93 struct ff_effect *old;
97 if (!test_bit(EV_FF, dev->evbit))
100 if (effect->type < FF_EFFECT_MIN || effect->type > FF_EFFECT_MAX ||
101 !test_bit(effect->type, dev->ffbit)) {
102 dev_dbg(&dev->dev, "invalid or not supported effect type in upload\n");
106 if (effect->type == FF_PERIODIC &&
107 (effect->u.periodic.waveform < FF_WAVEFORM_MIN ||
108 effect->u.periodic.waveform > FF_WAVEFORM_MAX ||
109 !test_bit(effect->u.periodic.waveform, dev->ffbit))) {
110 dev_dbg(&dev->dev, "invalid or not supported wave form in upload\n");
114 if (!test_bit(effect->type, ff->ffbit)) {
115 ret = compat_effect(ff, effect);
120 mutex_lock(&ff->mutex);
122 if (effect->id == -1) {
123 for (id = 0; id < ff->max_effects; id++)
124 if (!ff->effect_owners[id])
127 if (id >= ff->max_effects) {
138 ret = check_effect_access(ff, id, file);
142 old = &ff->effects[id];
144 if (!check_effects_compatible(effect, old)) {
150 ret = ff->upload(dev, effect, old);
154 spin_lock_irq(&dev->event_lock);
155 ff->effects[id] = *effect;
156 ff->effect_owners[id] = file;
157 spin_unlock_irq(&dev->event_lock);
160 mutex_unlock(&ff->mutex);
163 EXPORT_SYMBOL_GPL(input_ff_upload);
166 * Erases the effect if the requester is also the effect owner. The mutex
167 * should already be locked before calling this function.
169 static int erase_effect(struct input_dev *dev, int effect_id,
172 struct ff_device *ff = dev->ff;
175 error = check_effect_access(ff, effect_id, file);
179 spin_lock_irq(&dev->event_lock);
180 ff->playback(dev, effect_id, 0);
181 ff->effect_owners[effect_id] = NULL;
182 spin_unlock_irq(&dev->event_lock);
185 error = ff->erase(dev, effect_id);
187 spin_lock_irq(&dev->event_lock);
188 ff->effect_owners[effect_id] = file;
189 spin_unlock_irq(&dev->event_lock);
199 * input_ff_erase - erase a force-feedback effect from device
200 * @dev: input device to erase effect from
201 * @effect_id: id of the effect to be erased
202 * @file: purported owner of the request
204 * This function erases a force-feedback effect from specified device.
205 * The effect will only be erased if it was uploaded through the same
206 * file handle that is requesting erase.
208 int input_ff_erase(struct input_dev *dev, int effect_id, struct file *file)
210 struct ff_device *ff = dev->ff;
213 if (!test_bit(EV_FF, dev->evbit))
216 mutex_lock(&ff->mutex);
217 ret = erase_effect(dev, effect_id, file);
218 mutex_unlock(&ff->mutex);
222 EXPORT_SYMBOL_GPL(input_ff_erase);
225 * input_ff_flush - erase all effects owned by a file handle
226 * @dev: input device to erase effect from
227 * @file: purported owner of the effects
229 * This function erases all force-feedback effects associated with
230 * the given owner from specified device. Note that @file may be %NULL,
231 * in which case all effects will be erased.
233 int input_ff_flush(struct input_dev *dev, struct file *file)
235 struct ff_device *ff = dev->ff;
238 dev_dbg(&dev->dev, "flushing now\n");
240 mutex_lock(&ff->mutex);
242 for (i = 0; i < ff->max_effects; i++)
243 erase_effect(dev, i, file);
245 mutex_unlock(&ff->mutex);
249 EXPORT_SYMBOL_GPL(input_ff_flush);
252 * input_ff_event() - generic handler for force-feedback events
253 * @dev: input device to send the effect to
254 * @type: event type (anything but EV_FF is ignored)
256 * @value: event value
258 int input_ff_event(struct input_dev *dev, unsigned int type,
259 unsigned int code, int value)
261 struct ff_device *ff = dev->ff;
268 if (!test_bit(FF_GAIN, dev->ffbit) || value > 0xffffU)
271 ff->set_gain(dev, value);
275 if (!test_bit(FF_AUTOCENTER, dev->ffbit) || value > 0xffffU)
278 ff->set_autocenter(dev, value);
282 if (check_effect_access(ff, code, NULL) == 0)
283 ff->playback(dev, code, value);
289 EXPORT_SYMBOL_GPL(input_ff_event);
292 * input_ff_create() - create force-feedback device
293 * @dev: input device supporting force-feedback
294 * @max_effects: maximum number of effects supported by the device
296 * This function allocates all necessary memory for a force feedback
297 * portion of an input device and installs all default handlers.
298 * @dev->ffbit should be already set up before calling this function.
299 * Once ff device is created you need to setup its upload, erase,
300 * playback and other handlers before registering input device
302 int input_ff_create(struct input_dev *dev, unsigned int max_effects)
304 struct ff_device *ff;
309 dev_err(&dev->dev, "cannot allocate device without any effects\n");
313 if (max_effects > FF_MAX_EFFECTS) {
314 dev_err(&dev->dev, "cannot allocate more than FF_MAX_EFFECTS effects\n");
318 ff_dev_size = sizeof(struct ff_device) +
319 max_effects * sizeof(struct file *);
320 if (ff_dev_size < max_effects) /* overflow */
323 ff = kzalloc(ff_dev_size, 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 = input_ff_flush;
339 dev->event = input_ff_event;
340 __set_bit(EV_FF, dev->evbit);
342 /* Copy "true" bits into ff device bitmap */
343 for_each_set_bit(i, dev->ffbit, FF_CNT)
344 __set_bit(i, ff->ffbit);
346 /* we can emulate RUMBLE with periodic effects */
347 if (test_bit(FF_PERIODIC, ff->ffbit))
348 __set_bit(FF_RUMBLE, dev->ffbit);
352 EXPORT_SYMBOL_GPL(input_ff_create);
355 * input_ff_destroy() - frees force feedback portion of input device
356 * @dev: input device supporting force feedback
358 * This function is only needed in error path as input core will
359 * automatically free force feedback structures when device is
362 void input_ff_destroy(struct input_dev *dev)
364 struct ff_device *ff = dev->ff;
366 __clear_bit(EV_FF, dev->evbit);
376 EXPORT_SYMBOL_GPL(input_ff_destroy);