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>
33 * Check that the effect_id is a valid effect and whether the user
36 static int check_effect_access(struct ff_device
*ff
, int effect_id
,
39 if (effect_id
< 0 || effect_id
>= ff
->max_effects
||
40 !ff
->effect_owners
[effect_id
])
43 if (file
&& ff
->effect_owners
[effect_id
] != file
)
50 * Checks whether 2 effects can be combined together
52 static inline int check_effects_compatible(struct ff_effect
*e1
,
55 return e1
->type
== e2
->type
&&
56 (e1
->type
!= FF_PERIODIC
||
57 e1
->u
.periodic
.waveform
== e2
->u
.periodic
.waveform
);
61 * Convert an effect into compatible one
63 static int compat_effect(struct ff_device
*ff
, struct ff_effect
*effect
)
67 switch (effect
->type
) {
69 if (!test_bit(FF_PERIODIC
, ff
->ffbit
))
73 * calculate manginude of sine wave as average of rumble's
74 * 2/3 of strong magnitude and 1/3 of weak magnitude
76 magnitude
= effect
->u
.rumble
.strong_magnitude
/ 3 +
77 effect
->u
.rumble
.weak_magnitude
/ 6;
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;
93 /* Let driver handle conversion */
99 * input_ff_upload() - upload effect into force-feedback device
101 * @effect: effect to be uploaded
102 * @file: owner of the effect
104 int input_ff_upload(struct input_dev
*dev
, struct ff_effect
*effect
,
107 struct ff_device
*ff
= dev
->ff
;
108 struct ff_effect
*old
;
112 if (!test_bit(EV_FF
, dev
->evbit
))
115 if (effect
->type
< FF_EFFECT_MIN
|| effect
->type
> FF_EFFECT_MAX
||
116 !test_bit(effect
->type
, dev
->ffbit
)) {
117 debug("invalid or not supported effect type in upload");
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 debug("invalid or not supported wave form in upload");
129 if (!test_bit(effect
->type
, ff
->ffbit
)) {
130 ret
= compat_effect(ff
, effect
);
135 mutex_lock(&ff
->mutex
);
137 if (effect
->id
== -1) {
138 for (id
= 0; id
< ff
->max_effects
; id
++)
139 if (!ff
->effect_owners
[id
])
142 if (id
>= ff
->max_effects
) {
153 ret
= check_effect_access(ff
, id
, file
);
157 old
= &ff
->effects
[id
];
159 if (!check_effects_compatible(effect
, old
)) {
165 ret
= ff
->upload(dev
, effect
, old
);
169 ff
->effects
[id
] = *effect
;
170 ff
->effect_owners
[id
] = file
;
173 mutex_unlock(&ff
->mutex
);
176 EXPORT_SYMBOL_GPL(input_ff_upload
);
179 * Erases the effect if the requester is also the effect owner. The mutex
180 * should already be locked before calling this function.
182 static int erase_effect(struct input_dev
*dev
, int effect_id
,
185 struct ff_device
*ff
= dev
->ff
;
188 error
= check_effect_access(ff
, effect_id
, file
);
192 ff
->playback(dev
, effect_id
, 0);
195 error
= ff
->erase(dev
, effect_id
);
200 ff
->effect_owners
[effect_id
] = NULL
;
206 * input_ff_erase - erase an effect from device
207 * @dev: input device to erase effect from
208 * @effect_id: id of the ffect to be erased
209 * @file: purported owner of the request
211 * This function erases a force-feedback effect from specified device.
212 * The effect will only be erased if it was uploaded through the same
213 * file handle that is requesting erase.
215 int input_ff_erase(struct input_dev
*dev
, int effect_id
, struct file
*file
)
217 struct ff_device
*ff
= dev
->ff
;
220 if (!test_bit(EV_FF
, dev
->evbit
))
223 mutex_lock(&ff
->mutex
);
224 ret
= erase_effect(dev
, effect_id
, file
);
225 mutex_unlock(&ff
->mutex
);
229 EXPORT_SYMBOL_GPL(input_ff_erase
);
232 * flush_effects - erase all effects owned by a file handle
234 static int flush_effects(struct input_dev
*dev
, struct file
*file
)
236 struct ff_device
*ff
= dev
->ff
;
239 debug("flushing now");
241 mutex_lock(&ff
->mutex
);
243 for (i
= 0; i
< ff
->max_effects
; i
++)
244 erase_effect(dev
, i
, file
);
246 mutex_unlock(&ff
->mutex
);
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
;
266 mutex_lock(&ff
->mutex
);
270 if (!test_bit(FF_GAIN
, dev
->ffbit
) || value
> 0xffff)
273 ff
->set_gain(dev
, value
);
277 if (!test_bit(FF_AUTOCENTER
, dev
->ffbit
) || value
> 0xffff)
280 ff
->set_autocenter(dev
, value
);
284 ff
->playback(dev
, code
, value
);
288 mutex_unlock(&ff
->mutex
);
291 EXPORT_SYMBOL_GPL(input_ff_event
);
294 * input_ff_create() - create force-feedback device
295 * @dev: input device supporting force-feedback
296 * @max_effects: maximum number of effects supported by the device
298 * This function allocates all necessary memory for a force feedback
299 * portion of an input device and installs all default handlers.
300 * @dev->ffbit should be already set up before calling this function.
301 * Once ff device is created you need to setup its upload, erase,
302 * playback and other handlers before registering input device
304 int input_ff_create(struct input_dev
*dev
, int max_effects
)
306 struct ff_device
*ff
;
311 "ff-core: cannot allocate device without any effects\n");
315 ff
= kzalloc(sizeof(struct ff_device
) +
316 max_effects
* sizeof(struct file
*), GFP_KERNEL
);
320 ff
->effects
= kcalloc(max_effects
, sizeof(struct ff_effect
),
327 ff
->max_effects
= max_effects
;
328 mutex_init(&ff
->mutex
);
331 dev
->flush
= flush_effects
;
332 dev
->event
= input_ff_event
;
333 set_bit(EV_FF
, dev
->evbit
);
335 /* Copy "true" bits into ff device bitmap */
336 for (i
= 0; i
<= FF_MAX
; i
++)
337 if (test_bit(i
, dev
->ffbit
))
338 set_bit(i
, ff
->ffbit
);
340 /* we can emulate RUMBLE with periodic effects */
341 if (test_bit(FF_PERIODIC
, ff
->ffbit
))
342 set_bit(FF_RUMBLE
, dev
->ffbit
);
346 EXPORT_SYMBOL_GPL(input_ff_create
);
349 * input_ff_free() - frees force feedback portion of input device
350 * @dev: input device supporintg force feedback
352 * This function is only needed in error path as input core will
353 * automatically free force feedback structures when device is
356 void input_ff_destroy(struct input_dev
*dev
)
358 clear_bit(EV_FF
, dev
->evbit
);
360 if (dev
->ff
->destroy
)
361 dev
->ff
->destroy(dev
->ff
);
362 kfree(dev
->ff
->private);
367 EXPORT_SYMBOL_GPL(input_ff_destroy
);