2 * Force feedback support for Linux input subsystem
4 * Copyright (c) 2006 Anssi Hannula <anssi.hannula@gmail.com>
5 * Copyright (c) 2006 Dmitry Torokhov <dtor@mail.ru>
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 debug(format, arg...) pr_debug("ff-core: " format "\n", ## arg)
28 #include <linux/input.h>
29 #include <linux/module.h>
30 #include <linux/mutex.h>
31 #include <linux/sched.h>
34 * Check that the effect_id is a valid effect and whether the user
37 static int check_effect_access(struct ff_device
*ff
, int effect_id
,
40 if (effect_id
< 0 || effect_id
>= ff
->max_effects
||
41 !ff
->effect_owners
[effect_id
])
44 if (file
&& ff
->effect_owners
[effect_id
] != file
)
51 * Checks whether 2 effects can be combined together
53 static inline int check_effects_compatible(struct ff_effect
*e1
,
56 return e1
->type
== e2
->type
&&
57 (e1
->type
!= FF_PERIODIC
||
58 e1
->u
.periodic
.waveform
== e2
->u
.periodic
.waveform
);
62 * Convert an effect into compatible one
64 static int compat_effect(struct ff_device
*ff
, struct ff_effect
*effect
)
68 switch (effect
->type
) {
70 if (!test_bit(FF_PERIODIC
, ff
->ffbit
))
74 * calculate manginude of sine wave as average of rumble's
75 * 2/3 of strong magnitude and 1/3 of weak magnitude
77 magnitude
= effect
->u
.rumble
.strong_magnitude
/ 3 +
78 effect
->u
.rumble
.weak_magnitude
/ 6;
80 effect
->type
= FF_PERIODIC
;
81 effect
->u
.periodic
.waveform
= FF_SINE
;
82 effect
->u
.periodic
.period
= 50;
83 effect
->u
.periodic
.magnitude
= max(magnitude
, 0x7fff);
84 effect
->u
.periodic
.offset
= 0;
85 effect
->u
.periodic
.phase
= 0;
86 effect
->u
.periodic
.envelope
.attack_length
= 0;
87 effect
->u
.periodic
.envelope
.attack_level
= 0;
88 effect
->u
.periodic
.envelope
.fade_length
= 0;
89 effect
->u
.periodic
.envelope
.fade_level
= 0;
94 /* Let driver handle conversion */
100 * input_ff_upload() - upload effect into force-feedback device
102 * @effect: effect to be uploaded
103 * @file: owner of the effect
105 int input_ff_upload(struct input_dev
*dev
, struct ff_effect
*effect
,
108 struct ff_device
*ff
= dev
->ff
;
109 struct ff_effect
*old
;
113 if (!test_bit(EV_FF
, dev
->evbit
))
116 if (effect
->type
< FF_EFFECT_MIN
|| effect
->type
> FF_EFFECT_MAX
||
117 !test_bit(effect
->type
, dev
->ffbit
)) {
118 debug("invalid or not supported effect type in upload");
122 if (effect
->type
== FF_PERIODIC
&&
123 (effect
->u
.periodic
.waveform
< FF_WAVEFORM_MIN
||
124 effect
->u
.periodic
.waveform
> FF_WAVEFORM_MAX
||
125 !test_bit(effect
->u
.periodic
.waveform
, dev
->ffbit
))) {
126 debug("invalid or not supported wave form in upload");
130 if (!test_bit(effect
->type
, ff
->ffbit
)) {
131 ret
= compat_effect(ff
, effect
);
136 mutex_lock(&ff
->mutex
);
138 if (effect
->id
== -1) {
139 for (id
= 0; id
< ff
->max_effects
; id
++)
140 if (!ff
->effect_owners
[id
])
143 if (id
>= ff
->max_effects
) {
154 ret
= check_effect_access(ff
, id
, file
);
158 old
= &ff
->effects
[id
];
160 if (!check_effects_compatible(effect
, old
)) {
166 ret
= ff
->upload(dev
, effect
, old
);
170 spin_lock_irq(&dev
->event_lock
);
171 ff
->effects
[id
] = *effect
;
172 ff
->effect_owners
[id
] = file
;
173 spin_unlock_irq(&dev
->event_lock
);
176 mutex_unlock(&ff
->mutex
);
179 EXPORT_SYMBOL_GPL(input_ff_upload
);
182 * Erases the effect if the requester is also the effect owner. The mutex
183 * should already be locked before calling this function.
185 static int erase_effect(struct input_dev
*dev
, int effect_id
,
188 struct ff_device
*ff
= dev
->ff
;
191 error
= check_effect_access(ff
, effect_id
, file
);
195 spin_lock_irq(&dev
->event_lock
);
196 ff
->playback(dev
, effect_id
, 0);
197 ff
->effect_owners
[effect_id
] = NULL
;
198 spin_unlock_irq(&dev
->event_lock
);
201 error
= ff
->erase(dev
, effect_id
);
203 spin_lock_irq(&dev
->event_lock
);
204 ff
->effect_owners
[effect_id
] = file
;
205 spin_unlock_irq(&dev
->event_lock
);
215 * input_ff_erase - erase a force-feedback effect from device
216 * @dev: input device to erase effect from
217 * @effect_id: id of the ffect to be erased
218 * @file: purported owner of the request
220 * This function erases a force-feedback effect from specified device.
221 * The effect will only be erased if it was uploaded through the same
222 * file handle that is requesting erase.
224 int input_ff_erase(struct input_dev
*dev
, int effect_id
, struct file
*file
)
226 struct ff_device
*ff
= dev
->ff
;
229 if (!test_bit(EV_FF
, dev
->evbit
))
232 mutex_lock(&ff
->mutex
);
233 ret
= erase_effect(dev
, effect_id
, file
);
234 mutex_unlock(&ff
->mutex
);
238 EXPORT_SYMBOL_GPL(input_ff_erase
);
241 * flush_effects - erase all effects owned by a file handle
243 static int flush_effects(struct input_dev
*dev
, struct file
*file
)
245 struct ff_device
*ff
= dev
->ff
;
248 debug("flushing now");
250 mutex_lock(&ff
->mutex
);
252 for (i
= 0; i
< ff
->max_effects
; i
++)
253 erase_effect(dev
, i
, file
);
255 mutex_unlock(&ff
->mutex
);
261 * input_ff_event() - generic handler for force-feedback events
262 * @dev: input device to send the effect to
263 * @type: event type (anything but EV_FF is ignored)
265 * @value: event value
267 int input_ff_event(struct input_dev
*dev
, unsigned int type
,
268 unsigned int code
, int value
)
270 struct ff_device
*ff
= dev
->ff
;
277 if (!test_bit(FF_GAIN
, dev
->ffbit
) || value
> 0xffff)
280 ff
->set_gain(dev
, value
);
284 if (!test_bit(FF_AUTOCENTER
, dev
->ffbit
) || value
> 0xffff)
287 ff
->set_autocenter(dev
, value
);
291 if (check_effect_access(ff
, code
, NULL
) == 0)
292 ff
->playback(dev
, code
, value
);
298 EXPORT_SYMBOL_GPL(input_ff_event
);
301 * input_ff_create() - create force-feedback device
302 * @dev: input device supporting force-feedback
303 * @max_effects: maximum number of effects supported by the device
305 * This function allocates all necessary memory for a force feedback
306 * portion of an input device and installs all default handlers.
307 * @dev->ffbit should be already set up before calling this function.
308 * Once ff device is created you need to setup its upload, erase,
309 * playback and other handlers before registering input device
311 int input_ff_create(struct input_dev
*dev
, int max_effects
)
313 struct ff_device
*ff
;
318 "ff-core: 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
);