OMAP3: SR: Remove redundant defines
[linux-ginger.git] / drivers / input / ff-core.c
blob72c63e5dd630709ddffccf8dc945147857bf41ba
1 /*
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>
6 */
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.
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
24 /* #define DEBUG */
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
35 * is the owner
37 static int check_effect_access(struct ff_device *ff, int effect_id,
38 struct file *file)
40 if (effect_id < 0 || effect_id >= ff->max_effects ||
41 !ff->effect_owners[effect_id])
42 return -EINVAL;
44 if (file && ff->effect_owners[effect_id] != file)
45 return -EACCES;
47 return 0;
51 * Checks whether 2 effects can be combined together
53 static inline int check_effects_compatible(struct ff_effect *e1,
54 struct ff_effect *e2)
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)
66 int magnitude;
68 switch (effect->type) {
69 case FF_RUMBLE:
70 if (!test_bit(FF_PERIODIC, ff->ffbit))
71 return -EINVAL;
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;
91 return 0;
93 default:
94 /* Let driver handle conversion */
95 return 0;
99 /**
100 * input_ff_upload() - upload effect into force-feedback device
101 * @dev: input 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,
106 struct file *file)
108 struct ff_device *ff = dev->ff;
109 struct ff_effect *old;
110 int ret = 0;
111 int id;
113 if (!test_bit(EV_FF, dev->evbit))
114 return -ENOSYS;
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");
119 return -EINVAL;
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");
127 return -EINVAL;
130 if (!test_bit(effect->type, ff->ffbit)) {
131 ret = compat_effect(ff, effect);
132 if (ret)
133 return ret;
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])
141 break;
143 if (id >= ff->max_effects) {
144 ret = -ENOSPC;
145 goto out;
148 effect->id = id;
149 old = NULL;
151 } else {
152 id = effect->id;
154 ret = check_effect_access(ff, id, file);
155 if (ret)
156 goto out;
158 old = &ff->effects[id];
160 if (!check_effects_compatible(effect, old)) {
161 ret = -EINVAL;
162 goto out;
166 ret = ff->upload(dev, effect, old);
167 if (ret)
168 goto out;
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);
175 out:
176 mutex_unlock(&ff->mutex);
177 return ret;
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,
186 struct file *file)
188 struct ff_device *ff = dev->ff;
189 int error;
191 error = check_effect_access(ff, effect_id, file);
192 if (error)
193 return error;
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);
200 if (ff->erase) {
201 error = ff->erase(dev, effect_id);
202 if (error) {
203 spin_lock_irq(&dev->event_lock);
204 ff->effect_owners[effect_id] = file;
205 spin_unlock_irq(&dev->event_lock);
207 return error;
211 return 0;
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;
227 int ret;
229 if (!test_bit(EV_FF, dev->evbit))
230 return -ENOSYS;
232 mutex_lock(&ff->mutex);
233 ret = erase_effect(dev, effect_id, file);
234 mutex_unlock(&ff->mutex);
236 return ret;
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;
246 int i;
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);
257 return 0;
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)
264 * @code: event code
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;
272 if (type != EV_FF)
273 return 0;
275 switch (code) {
276 case FF_GAIN:
277 if (!test_bit(FF_GAIN, dev->ffbit) || value > 0xffff)
278 break;
280 ff->set_gain(dev, value);
281 break;
283 case FF_AUTOCENTER:
284 if (!test_bit(FF_AUTOCENTER, dev->ffbit) || value > 0xffff)
285 break;
287 ff->set_autocenter(dev, value);
288 break;
290 default:
291 if (check_effect_access(ff, code, NULL) == 0)
292 ff->playback(dev, code, value);
293 break;
296 return 0;
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;
314 int i;
316 if (!max_effects) {
317 printk(KERN_ERR
318 "ff-core: cannot allocate device without any effects\n");
319 return -EINVAL;
322 ff = kzalloc(sizeof(struct ff_device) +
323 max_effects * sizeof(struct file *), GFP_KERNEL);
324 if (!ff)
325 return -ENOMEM;
327 ff->effects = kcalloc(max_effects, sizeof(struct ff_effect),
328 GFP_KERNEL);
329 if (!ff->effects) {
330 kfree(ff);
331 return -ENOMEM;
334 ff->max_effects = max_effects;
335 mutex_init(&ff->mutex);
337 dev->ff = ff;
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);
351 return 0;
353 EXPORT_SYMBOL_GPL(input_ff_create);
356 * input_ff_free() - 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
361 * destroyed.
363 void input_ff_destroy(struct input_dev *dev)
365 clear_bit(EV_FF, dev->evbit);
366 if (dev->ff) {
367 if (dev->ff->destroy)
368 dev->ff->destroy(dev->ff);
369 kfree(dev->ff->private);
370 kfree(dev->ff);
371 dev->ff = NULL;
374 EXPORT_SYMBOL_GPL(input_ff_destroy);