Loading...
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Force feedback support for Linux input subsystem
4 *
5 * Copyright (c) 2006 Anssi Hannula <anssi.hannula@gmail.com>
6 * Copyright (c) 2006 Dmitry Torokhov <dtor@mail.ru>
7 */
8
9/*
10 */
11
12/* #define DEBUG */
13
14#include <linux/input.h>
15#include <linux/module.h>
16#include <linux/mutex.h>
17#include <linux/sched.h>
18#include <linux/slab.h>
19
20/*
21 * Check that the effect_id is a valid effect and whether the user
22 * is the owner
23 */
24static int check_effect_access(struct ff_device *ff, int effect_id,
25 struct file *file)
26{
27 if (effect_id < 0 || effect_id >= ff->max_effects ||
28 !ff->effect_owners[effect_id])
29 return -EINVAL;
30
31 if (file && ff->effect_owners[effect_id] != file)
32 return -EACCES;
33
34 return 0;
35}
36
37/*
38 * Checks whether 2 effects can be combined together
39 */
40static inline int check_effects_compatible(struct ff_effect *e1,
41 struct ff_effect *e2)
42{
43 return e1->type == e2->type &&
44 (e1->type != FF_PERIODIC ||
45 e1->u.periodic.waveform == e2->u.periodic.waveform);
46}
47
48/*
49 * Convert an effect into compatible one
50 */
51static int compat_effect(struct ff_device *ff, struct ff_effect *effect)
52{
53 int magnitude;
54
55 switch (effect->type) {
56 case FF_RUMBLE:
57 if (!test_bit(FF_PERIODIC, ff->ffbit))
58 return -EINVAL;
59
60 /*
61 * calculate magnitude of sine wave as average of rumble's
62 * 2/3 of strong magnitude and 1/3 of weak magnitude
63 */
64 magnitude = effect->u.rumble.strong_magnitude / 3 +
65 effect->u.rumble.weak_magnitude / 6;
66
67 effect->type = FF_PERIODIC;
68 effect->u.periodic.waveform = FF_SINE;
69 effect->u.periodic.period = 50;
70 effect->u.periodic.magnitude = max(magnitude, 0x7fff);
71 effect->u.periodic.offset = 0;
72 effect->u.periodic.phase = 0;
73 effect->u.periodic.envelope.attack_length = 0;
74 effect->u.periodic.envelope.attack_level = 0;
75 effect->u.periodic.envelope.fade_length = 0;
76 effect->u.periodic.envelope.fade_level = 0;
77
78 return 0;
79
80 default:
81 /* Let driver handle conversion */
82 return 0;
83 }
84}
85
86/**
87 * input_ff_upload() - upload effect into force-feedback device
88 * @dev: input device
89 * @effect: effect to be uploaded
90 * @file: owner of the effect
91 */
92int input_ff_upload(struct input_dev *dev, struct ff_effect *effect,
93 struct file *file)
94{
95 struct ff_device *ff = dev->ff;
96 struct ff_effect *old;
97 int ret = 0;
98 int id;
99
100 if (!test_bit(EV_FF, dev->evbit))
101 return -ENOSYS;
102
103 if (effect->type < FF_EFFECT_MIN || effect->type > FF_EFFECT_MAX ||
104 !test_bit(effect->type, dev->ffbit)) {
105 dev_dbg(&dev->dev, "invalid or not supported effect type in upload\n");
106 return -EINVAL;
107 }
108
109 if (effect->type == FF_PERIODIC &&
110 (effect->u.periodic.waveform < FF_WAVEFORM_MIN ||
111 effect->u.periodic.waveform > FF_WAVEFORM_MAX ||
112 !test_bit(effect->u.periodic.waveform, dev->ffbit))) {
113 dev_dbg(&dev->dev, "invalid or not supported wave form in upload\n");
114 return -EINVAL;
115 }
116
117 if (!test_bit(effect->type, ff->ffbit)) {
118 ret = compat_effect(ff, effect);
119 if (ret)
120 return ret;
121 }
122
123 mutex_lock(&ff->mutex);
124
125 if (effect->id == -1) {
126 for (id = 0; id < ff->max_effects; id++)
127 if (!ff->effect_owners[id])
128 break;
129
130 if (id >= ff->max_effects) {
131 ret = -ENOSPC;
132 goto out;
133 }
134
135 effect->id = id;
136 old = NULL;
137
138 } else {
139 id = effect->id;
140
141 ret = check_effect_access(ff, id, file);
142 if (ret)
143 goto out;
144
145 old = &ff->effects[id];
146
147 if (!check_effects_compatible(effect, old)) {
148 ret = -EINVAL;
149 goto out;
150 }
151 }
152
153 ret = ff->upload(dev, effect, old);
154 if (ret)
155 goto out;
156
157 spin_lock_irq(&dev->event_lock);
158 ff->effects[id] = *effect;
159 ff->effect_owners[id] = file;
160 spin_unlock_irq(&dev->event_lock);
161
162 out:
163 mutex_unlock(&ff->mutex);
164 return ret;
165}
166EXPORT_SYMBOL_GPL(input_ff_upload);
167
168/*
169 * Erases the effect if the requester is also the effect owner. The mutex
170 * should already be locked before calling this function.
171 */
172static int erase_effect(struct input_dev *dev, int effect_id,
173 struct file *file)
174{
175 struct ff_device *ff = dev->ff;
176 int error;
177
178 error = check_effect_access(ff, effect_id, file);
179 if (error)
180 return error;
181
182 spin_lock_irq(&dev->event_lock);
183 ff->playback(dev, effect_id, 0);
184 ff->effect_owners[effect_id] = NULL;
185 spin_unlock_irq(&dev->event_lock);
186
187 if (ff->erase) {
188 error = ff->erase(dev, effect_id);
189 if (error) {
190 spin_lock_irq(&dev->event_lock);
191 ff->effect_owners[effect_id] = file;
192 spin_unlock_irq(&dev->event_lock);
193
194 return error;
195 }
196 }
197
198 return 0;
199}
200
201/**
202 * input_ff_erase - erase a force-feedback effect from device
203 * @dev: input device to erase effect from
204 * @effect_id: id of the effect to be erased
205 * @file: purported owner of the request
206 *
207 * This function erases a force-feedback effect from specified device.
208 * The effect will only be erased if it was uploaded through the same
209 * file handle that is requesting erase.
210 */
211int input_ff_erase(struct input_dev *dev, int effect_id, struct file *file)
212{
213 struct ff_device *ff = dev->ff;
214 int ret;
215
216 if (!test_bit(EV_FF, dev->evbit))
217 return -ENOSYS;
218
219 mutex_lock(&ff->mutex);
220 ret = erase_effect(dev, effect_id, file);
221 mutex_unlock(&ff->mutex);
222
223 return ret;
224}
225EXPORT_SYMBOL_GPL(input_ff_erase);
226
227/*
228 * input_ff_flush - erase all effects owned by a file handle
229 * @dev: input device to erase effect from
230 * @file: purported owner of the effects
231 *
232 * This function erases all force-feedback effects associated with
233 * the given owner from specified device. Note that @file may be %NULL,
234 * in which case all effects will be erased.
235 */
236int input_ff_flush(struct input_dev *dev, struct file *file)
237{
238 struct ff_device *ff = dev->ff;
239 int i;
240
241 dev_dbg(&dev->dev, "flushing now\n");
242
243 mutex_lock(&ff->mutex);
244
245 for (i = 0; i < ff->max_effects; i++)
246 erase_effect(dev, i, file);
247
248 mutex_unlock(&ff->mutex);
249
250 return 0;
251}
252EXPORT_SYMBOL_GPL(input_ff_flush);
253
254/**
255 * input_ff_event() - generic handler for force-feedback events
256 * @dev: input device to send the effect to
257 * @type: event type (anything but EV_FF is ignored)
258 * @code: event code
259 * @value: event value
260 */
261int input_ff_event(struct input_dev *dev, unsigned int type,
262 unsigned int code, int value)
263{
264 struct ff_device *ff = dev->ff;
265
266 if (type != EV_FF)
267 return 0;
268
269 switch (code) {
270 case FF_GAIN:
271 if (!test_bit(FF_GAIN, dev->ffbit) || value > 0xffffU)
272 break;
273
274 ff->set_gain(dev, value);
275 break;
276
277 case FF_AUTOCENTER:
278 if (!test_bit(FF_AUTOCENTER, dev->ffbit) || value > 0xffffU)
279 break;
280
281 ff->set_autocenter(dev, value);
282 break;
283
284 default:
285 if (check_effect_access(ff, code, NULL) == 0)
286 ff->playback(dev, code, value);
287 break;
288 }
289
290 return 0;
291}
292EXPORT_SYMBOL_GPL(input_ff_event);
293
294/**
295 * input_ff_create() - create force-feedback device
296 * @dev: input device supporting force-feedback
297 * @max_effects: maximum number of effects supported by the device
298 *
299 * This function allocates all necessary memory for a force feedback
300 * portion of an input device and installs all default handlers.
301 * @dev->ffbit should be already set up before calling this function.
302 * Once ff device is created you need to setup its upload, erase,
303 * playback and other handlers before registering input device
304 */
305int input_ff_create(struct input_dev *dev, unsigned int max_effects)
306{
307 struct ff_device *ff;
308 size_t ff_dev_size;
309 int i;
310
311 if (!max_effects) {
312 dev_err(&dev->dev, "cannot allocate device without any effects\n");
313 return -EINVAL;
314 }
315
316 if (max_effects > FF_MAX_EFFECTS) {
317 dev_err(&dev->dev, "cannot allocate more than FF_MAX_EFFECTS effects\n");
318 return -EINVAL;
319 }
320
321 ff_dev_size = sizeof(struct ff_device) +
322 max_effects * sizeof(struct file *);
323 if (ff_dev_size < max_effects) /* overflow */
324 return -EINVAL;
325
326 ff = kzalloc(ff_dev_size, GFP_KERNEL);
327 if (!ff)
328 return -ENOMEM;
329
330 ff->effects = kcalloc(max_effects, sizeof(struct ff_effect),
331 GFP_KERNEL);
332 if (!ff->effects) {
333 kfree(ff);
334 return -ENOMEM;
335 }
336
337 ff->max_effects = max_effects;
338 mutex_init(&ff->mutex);
339
340 dev->ff = ff;
341 dev->flush = input_ff_flush;
342 dev->event = input_ff_event;
343 __set_bit(EV_FF, dev->evbit);
344
345 /* Copy "true" bits into ff device bitmap */
346 for_each_set_bit(i, dev->ffbit, FF_CNT)
347 __set_bit(i, ff->ffbit);
348
349 /* we can emulate RUMBLE with periodic effects */
350 if (test_bit(FF_PERIODIC, ff->ffbit))
351 __set_bit(FF_RUMBLE, dev->ffbit);
352
353 return 0;
354}
355EXPORT_SYMBOL_GPL(input_ff_create);
356
357/**
358 * input_ff_destroy() - frees force feedback portion of input device
359 * @dev: input device supporting force feedback
360 *
361 * This function is only needed in error path as input core will
362 * automatically free force feedback structures when device is
363 * destroyed.
364 */
365void input_ff_destroy(struct input_dev *dev)
366{
367 struct ff_device *ff = dev->ff;
368
369 __clear_bit(EV_FF, dev->evbit);
370 if (ff) {
371 if (ff->destroy)
372 ff->destroy(ff);
373 kfree(ff->private);
374 kfree(ff->effects);
375 kfree(ff);
376 dev->ff = NULL;
377 }
378}
379EXPORT_SYMBOL_GPL(input_ff_destroy);
1/*
2 * Force feedback support for Linux input subsystem
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#include <linux/input.h>
27#include <linux/module.h>
28#include <linux/mutex.h>
29#include <linux/sched.h>
30#include <linux/slab.h>
31
32/*
33 * Check that the effect_id is a valid effect and whether the user
34 * is the owner
35 */
36static int check_effect_access(struct ff_device *ff, int effect_id,
37 struct file *file)
38{
39 if (effect_id < 0 || effect_id >= ff->max_effects ||
40 !ff->effect_owners[effect_id])
41 return -EINVAL;
42
43 if (file && ff->effect_owners[effect_id] != file)
44 return -EACCES;
45
46 return 0;
47}
48
49/*
50 * Checks whether 2 effects can be combined together
51 */
52static inline int check_effects_compatible(struct ff_effect *e1,
53 struct ff_effect *e2)
54{
55 return e1->type == e2->type &&
56 (e1->type != FF_PERIODIC ||
57 e1->u.periodic.waveform == e2->u.periodic.waveform);
58}
59
60/*
61 * Convert an effect into compatible one
62 */
63static int compat_effect(struct ff_device *ff, struct ff_effect *effect)
64{
65 int magnitude;
66
67 switch (effect->type) {
68 case FF_RUMBLE:
69 if (!test_bit(FF_PERIODIC, ff->ffbit))
70 return -EINVAL;
71
72 /*
73 * calculate magnitude of sine wave as average of rumble's
74 * 2/3 of strong magnitude and 1/3 of weak magnitude
75 */
76 magnitude = effect->u.rumble.strong_magnitude / 3 +
77 effect->u.rumble.weak_magnitude / 6;
78
79 effect->type = FF_PERIODIC;
80 effect->u.periodic.waveform = FF_SINE;
81 effect->u.periodic.period = 50;
82 effect->u.periodic.magnitude = max(magnitude, 0x7fff);
83 effect->u.periodic.offset = 0;
84 effect->u.periodic.phase = 0;
85 effect->u.periodic.envelope.attack_length = 0;
86 effect->u.periodic.envelope.attack_level = 0;
87 effect->u.periodic.envelope.fade_length = 0;
88 effect->u.periodic.envelope.fade_level = 0;
89
90 return 0;
91
92 default:
93 /* Let driver handle conversion */
94 return 0;
95 }
96}
97
98/**
99 * input_ff_upload() - upload effect into force-feedback device
100 * @dev: input device
101 * @effect: effect to be uploaded
102 * @file: owner of the effect
103 */
104int input_ff_upload(struct input_dev *dev, struct ff_effect *effect,
105 struct file *file)
106{
107 struct ff_device *ff = dev->ff;
108 struct ff_effect *old;
109 int ret = 0;
110 int id;
111
112 if (!test_bit(EV_FF, dev->evbit))
113 return -ENOSYS;
114
115 if (effect->type < FF_EFFECT_MIN || effect->type > FF_EFFECT_MAX ||
116 !test_bit(effect->type, dev->ffbit)) {
117 dev_dbg(&dev->dev, "invalid or not supported effect type in upload\n");
118 return -EINVAL;
119 }
120
121 if (effect->type == FF_PERIODIC &&
122 (effect->u.periodic.waveform < FF_WAVEFORM_MIN ||
123 effect->u.periodic.waveform > FF_WAVEFORM_MAX ||
124 !test_bit(effect->u.periodic.waveform, dev->ffbit))) {
125 dev_dbg(&dev->dev, "invalid or not supported wave form in upload\n");
126 return -EINVAL;
127 }
128
129 if (!test_bit(effect->type, ff->ffbit)) {
130 ret = compat_effect(ff, effect);
131 if (ret)
132 return ret;
133 }
134
135 mutex_lock(&ff->mutex);
136
137 if (effect->id == -1) {
138 for (id = 0; id < ff->max_effects; id++)
139 if (!ff->effect_owners[id])
140 break;
141
142 if (id >= ff->max_effects) {
143 ret = -ENOSPC;
144 goto out;
145 }
146
147 effect->id = id;
148 old = NULL;
149
150 } else {
151 id = effect->id;
152
153 ret = check_effect_access(ff, id, file);
154 if (ret)
155 goto out;
156
157 old = &ff->effects[id];
158
159 if (!check_effects_compatible(effect, old)) {
160 ret = -EINVAL;
161 goto out;
162 }
163 }
164
165 ret = ff->upload(dev, effect, old);
166 if (ret)
167 goto out;
168
169 spin_lock_irq(&dev->event_lock);
170 ff->effects[id] = *effect;
171 ff->effect_owners[id] = file;
172 spin_unlock_irq(&dev->event_lock);
173
174 out:
175 mutex_unlock(&ff->mutex);
176 return ret;
177}
178EXPORT_SYMBOL_GPL(input_ff_upload);
179
180/*
181 * Erases the effect if the requester is also the effect owner. The mutex
182 * should already be locked before calling this function.
183 */
184static int erase_effect(struct input_dev *dev, int effect_id,
185 struct file *file)
186{
187 struct ff_device *ff = dev->ff;
188 int error;
189
190 error = check_effect_access(ff, effect_id, file);
191 if (error)
192 return error;
193
194 spin_lock_irq(&dev->event_lock);
195 ff->playback(dev, effect_id, 0);
196 ff->effect_owners[effect_id] = NULL;
197 spin_unlock_irq(&dev->event_lock);
198
199 if (ff->erase) {
200 error = ff->erase(dev, effect_id);
201 if (error) {
202 spin_lock_irq(&dev->event_lock);
203 ff->effect_owners[effect_id] = file;
204 spin_unlock_irq(&dev->event_lock);
205
206 return error;
207 }
208 }
209
210 return 0;
211}
212
213/**
214 * input_ff_erase - erase a force-feedback effect from device
215 * @dev: input device to erase effect from
216 * @effect_id: id of the effect to be erased
217 * @file: purported owner of the request
218 *
219 * This function erases a force-feedback effect from specified device.
220 * The effect will only be erased if it was uploaded through the same
221 * file handle that is requesting erase.
222 */
223int input_ff_erase(struct input_dev *dev, int effect_id, struct file *file)
224{
225 struct ff_device *ff = dev->ff;
226 int ret;
227
228 if (!test_bit(EV_FF, dev->evbit))
229 return -ENOSYS;
230
231 mutex_lock(&ff->mutex);
232 ret = erase_effect(dev, effect_id, file);
233 mutex_unlock(&ff->mutex);
234
235 return ret;
236}
237EXPORT_SYMBOL_GPL(input_ff_erase);
238
239/*
240 * input_ff_flush - erase all effects owned by a file handle
241 * @dev: input device to erase effect from
242 * @file: purported owner of the effects
243 *
244 * This function erases all force-feedback effects associated with
245 * the given owner from specified device. Note that @file may be %NULL,
246 * in which case all effects will be erased.
247 */
248int input_ff_flush(struct input_dev *dev, struct file *file)
249{
250 struct ff_device *ff = dev->ff;
251 int i;
252
253 dev_dbg(&dev->dev, "flushing now\n");
254
255 mutex_lock(&ff->mutex);
256
257 for (i = 0; i < ff->max_effects; i++)
258 erase_effect(dev, i, file);
259
260 mutex_unlock(&ff->mutex);
261
262 return 0;
263}
264EXPORT_SYMBOL_GPL(input_ff_flush);
265
266/**
267 * input_ff_event() - generic handler for force-feedback events
268 * @dev: input device to send the effect to
269 * @type: event type (anything but EV_FF is ignored)
270 * @code: event code
271 * @value: event value
272 */
273int input_ff_event(struct input_dev *dev, unsigned int type,
274 unsigned int code, int value)
275{
276 struct ff_device *ff = dev->ff;
277
278 if (type != EV_FF)
279 return 0;
280
281 switch (code) {
282 case FF_GAIN:
283 if (!test_bit(FF_GAIN, dev->ffbit) || value > 0xffffU)
284 break;
285
286 ff->set_gain(dev, value);
287 break;
288
289 case FF_AUTOCENTER:
290 if (!test_bit(FF_AUTOCENTER, dev->ffbit) || value > 0xffffU)
291 break;
292
293 ff->set_autocenter(dev, value);
294 break;
295
296 default:
297 if (check_effect_access(ff, code, NULL) == 0)
298 ff->playback(dev, code, value);
299 break;
300 }
301
302 return 0;
303}
304EXPORT_SYMBOL_GPL(input_ff_event);
305
306/**
307 * input_ff_create() - create force-feedback device
308 * @dev: input device supporting force-feedback
309 * @max_effects: maximum number of effects supported by the device
310 *
311 * This function allocates all necessary memory for a force feedback
312 * portion of an input device and installs all default handlers.
313 * @dev->ffbit should be already set up before calling this function.
314 * Once ff device is created you need to setup its upload, erase,
315 * playback and other handlers before registering input device
316 */
317int input_ff_create(struct input_dev *dev, unsigned int max_effects)
318{
319 struct ff_device *ff;
320 size_t ff_dev_size;
321 int i;
322
323 if (!max_effects) {
324 dev_err(&dev->dev, "cannot allocate device without any effects\n");
325 return -EINVAL;
326 }
327
328 if (max_effects > FF_MAX_EFFECTS) {
329 dev_err(&dev->dev, "cannot allocate more than FF_MAX_EFFECTS effects\n");
330 return -EINVAL;
331 }
332
333 ff_dev_size = sizeof(struct ff_device) +
334 max_effects * sizeof(struct file *);
335 if (ff_dev_size < max_effects) /* overflow */
336 return -EINVAL;
337
338 ff = kzalloc(ff_dev_size, GFP_KERNEL);
339 if (!ff)
340 return -ENOMEM;
341
342 ff->effects = kcalloc(max_effects, sizeof(struct ff_effect),
343 GFP_KERNEL);
344 if (!ff->effects) {
345 kfree(ff);
346 return -ENOMEM;
347 }
348
349 ff->max_effects = max_effects;
350 mutex_init(&ff->mutex);
351
352 dev->ff = ff;
353 dev->flush = input_ff_flush;
354 dev->event = input_ff_event;
355 __set_bit(EV_FF, dev->evbit);
356
357 /* Copy "true" bits into ff device bitmap */
358 for_each_set_bit(i, dev->ffbit, FF_CNT)
359 __set_bit(i, ff->ffbit);
360
361 /* we can emulate RUMBLE with periodic effects */
362 if (test_bit(FF_PERIODIC, ff->ffbit))
363 __set_bit(FF_RUMBLE, dev->ffbit);
364
365 return 0;
366}
367EXPORT_SYMBOL_GPL(input_ff_create);
368
369/**
370 * input_ff_destroy() - frees force feedback portion of input device
371 * @dev: input device supporting force feedback
372 *
373 * This function is only needed in error path as input core will
374 * automatically free force feedback structures when device is
375 * destroyed.
376 */
377void input_ff_destroy(struct input_dev *dev)
378{
379 struct ff_device *ff = dev->ff;
380
381 __clear_bit(EV_FF, dev->evbit);
382 if (ff) {
383 if (ff->destroy)
384 ff->destroy(ff);
385 kfree(ff->private);
386 kfree(ff->effects);
387 kfree(ff);
388 dev->ff = NULL;
389 }
390}
391EXPORT_SYMBOL_GPL(input_ff_destroy);