1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26#define debug(format, arg...) pr_debug("ff-core: " format "\n", ## arg)
27
28#include <linux/input.h>
29#include <linux/module.h>
30#include <linux/mutex.h>
31#include <linux/sched.h>
32
33
34
35
36
37static int check_effect_access(struct ff_device *ff, int effect_id,
38 struct file *file)
39{
40 if (effect_id < 0 || effect_id >= ff->max_effects ||
41 !ff->effect_owners[effect_id])
42 return -EINVAL;
43
44 if (file && ff->effect_owners[effect_id] != file)
45 return -EACCES;
46
47 return 0;
48}
49
50
51
52
53static inline int check_effects_compatible(struct ff_effect *e1,
54 struct ff_effect *e2)
55{
56 return e1->type == e2->type &&
57 (e1->type != FF_PERIODIC ||
58 e1->u.periodic.waveform == e2->u.periodic.waveform);
59}
60
61
62
63
64static int compat_effect(struct ff_device *ff, struct ff_effect *effect)
65{
66 int magnitude;
67
68 switch (effect->type) {
69 case FF_RUMBLE:
70 if (!test_bit(FF_PERIODIC, ff->ffbit))
71 return -EINVAL;
72
73
74
75
76
77 magnitude = effect->u.rumble.strong_magnitude / 3 +
78 effect->u.rumble.weak_magnitude / 6;
79
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;
90
91 return 0;
92
93 default:
94
95 return 0;
96 }
97}
98
99
100
101
102
103
104
105int input_ff_upload(struct input_dev *dev, struct ff_effect *effect,
106 struct file *file)
107{
108 struct ff_device *ff = dev->ff;
109 struct ff_effect *old;
110 int ret = 0;
111 int id;
112
113 if (!test_bit(EV_FF, dev->evbit))
114 return -ENOSYS;
115
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;
120 }
121
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;
128 }
129
130 if (!test_bit(effect->type, ff->ffbit)) {
131 ret = compat_effect(ff, effect);
132 if (ret)
133 return ret;
134 }
135
136 mutex_lock(&ff->mutex);
137
138 if (effect->id == -1) {
139 for (id = 0; id < ff->max_effects; id++)
140 if (!ff->effect_owners[id])
141 break;
142
143 if (id >= ff->max_effects) {
144 ret = -ENOSPC;
145 goto out;
146 }
147
148 effect->id = id;
149 old = NULL;
150
151 } else {
152 id = effect->id;
153
154 ret = check_effect_access(ff, id, file);
155 if (ret)
156 goto out;
157
158 old = &ff->effects[id];
159
160 if (!check_effects_compatible(effect, old)) {
161 ret = -EINVAL;
162 goto out;
163 }
164 }
165
166 ret = ff->upload(dev, effect, old);
167 if (ret)
168 goto out;
169
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);
174
175 out:
176 mutex_unlock(&ff->mutex);
177 return ret;
178}
179EXPORT_SYMBOL_GPL(input_ff_upload);
180
181
182
183
184
185static int erase_effect(struct input_dev *dev, int effect_id,
186 struct file *file)
187{
188 struct ff_device *ff = dev->ff;
189 int error;
190
191 error = check_effect_access(ff, effect_id, file);
192 if (error)
193 return error;
194
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);
199
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);
206
207 return error;
208 }
209 }
210
211 return 0;
212}
213
214
215
216
217
218
219
220
221
222
223
224int input_ff_erase(struct input_dev *dev, int effect_id, struct file *file)
225{
226 struct ff_device *ff = dev->ff;
227 int ret;
228
229 if (!test_bit(EV_FF, dev->evbit))
230 return -ENOSYS;
231
232 mutex_lock(&ff->mutex);
233 ret = erase_effect(dev, effect_id, file);
234 mutex_unlock(&ff->mutex);
235
236 return ret;
237}
238EXPORT_SYMBOL_GPL(input_ff_erase);
239
240
241
242
243static int flush_effects(struct input_dev *dev, struct file *file)
244{
245 struct ff_device *ff = dev->ff;
246 int i;
247
248 debug("flushing now");
249
250 mutex_lock(&ff->mutex);
251
252 for (i = 0; i < ff->max_effects; i++)
253 erase_effect(dev, i, file);
254
255 mutex_unlock(&ff->mutex);
256
257 return 0;
258}
259
260
261
262
263
264
265
266
267int input_ff_event(struct input_dev *dev, unsigned int type,
268 unsigned int code, int value)
269{
270 struct ff_device *ff = dev->ff;
271
272 if (type != EV_FF)
273 return 0;
274
275 switch (code) {
276 case FF_GAIN:
277 if (!test_bit(FF_GAIN, dev->ffbit) || value > 0xffff)
278 break;
279
280 ff->set_gain(dev, value);
281 break;
282
283 case FF_AUTOCENTER:
284 if (!test_bit(FF_AUTOCENTER, dev->ffbit) || value > 0xffff)
285 break;
286
287 ff->set_autocenter(dev, value);
288 break;
289
290 default:
291 if (check_effect_access(ff, code, NULL) == 0)
292 ff->playback(dev, code, value);
293 break;
294 }
295
296 return 0;
297}
298EXPORT_SYMBOL_GPL(input_ff_event);
299
300
301
302
303
304
305
306
307
308
309
310
311int input_ff_create(struct input_dev *dev, int max_effects)
312{
313 struct ff_device *ff;
314 int i;
315
316 if (!max_effects) {
317 printk(KERN_ERR
318 "ff-core: cannot allocate device without any effects\n");
319 return -EINVAL;
320 }
321
322 ff = kzalloc(sizeof(struct ff_device) +
323 max_effects * sizeof(struct file *), GFP_KERNEL);
324 if (!ff)
325 return -ENOMEM;
326
327 ff->effects = kcalloc(max_effects, sizeof(struct ff_effect),
328 GFP_KERNEL);
329 if (!ff->effects) {
330 kfree(ff);
331 return -ENOMEM;
332 }
333
334 ff->max_effects = max_effects;
335 mutex_init(&ff->mutex);
336
337 dev->ff = ff;
338 dev->flush = flush_effects;
339 dev->event = input_ff_event;
340 set_bit(EV_FF, dev->evbit);
341
342
343 for (i = 0; i <= FF_MAX; i++)
344 if (test_bit(i, dev->ffbit))
345 set_bit(i, ff->ffbit);
346
347
348 if (test_bit(FF_PERIODIC, ff->ffbit))
349 set_bit(FF_RUMBLE, dev->ffbit);
350
351 return 0;
352}
353EXPORT_SYMBOL_GPL(input_ff_create);
354
355
356
357
358
359
360
361
362
363void input_ff_destroy(struct input_dev *dev)
364{
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;
372 }
373}
374EXPORT_SYMBOL_GPL(input_ff_destroy);
375