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
32
33
34
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
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
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
74
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
94 return 0;
95 }
96}
97
98
99
100
101
102
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 debug("invalid or not supported effect type in upload");
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 debug("invalid or not supported wave form in upload");
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 ff->effects[id] = *effect;
170 ff->effect_owners[id] = file;
171
172 out:
173 mutex_unlock(&ff->mutex);
174 return ret;
175}
176EXPORT_SYMBOL_GPL(input_ff_upload);
177
178
179
180
181
182static int erase_effect(struct input_dev *dev, int effect_id,
183 struct file *file)
184{
185 struct ff_device *ff = dev->ff;
186 int error;
187
188 error = check_effect_access(ff, effect_id, file);
189 if (error)
190 return error;
191
192 ff->playback(dev, effect_id, 0);
193
194 if (ff->erase) {
195 error = ff->erase(dev, effect_id);
196 if (error)
197 return error;
198 }
199
200 ff->effect_owners[effect_id] = NULL;
201
202 return 0;
203}
204
205
206
207
208
209
210
211
212
213
214
215int input_ff_erase(struct input_dev *dev, int effect_id, struct file *file)
216{
217 struct ff_device *ff = dev->ff;
218 int ret;
219
220 if (!test_bit(EV_FF, dev->evbit))
221 return -ENOSYS;
222
223 mutex_lock(&ff->mutex);
224 ret = erase_effect(dev, effect_id, file);
225 mutex_unlock(&ff->mutex);
226
227 return ret;
228}
229EXPORT_SYMBOL_GPL(input_ff_erase);
230
231
232
233
234static int flush_effects(struct input_dev *dev, struct file *file)
235{
236 struct ff_device *ff = dev->ff;
237 int i;
238
239 debug("flushing now");
240
241 mutex_lock(&ff->mutex);
242
243 for (i = 0; i < ff->max_effects; i++)
244 erase_effect(dev, i, file);
245
246 mutex_unlock(&ff->mutex);
247
248 return 0;
249}
250
251
252
253
254
255
256
257
258int input_ff_event(struct input_dev *dev, unsigned int type,
259 unsigned int code, int value)
260{
261 struct ff_device *ff = dev->ff;
262
263 if (type != EV_FF)
264 return 0;
265
266 mutex_lock(&ff->mutex);
267
268 switch (code) {
269 case FF_GAIN:
270 if (!test_bit(FF_GAIN, dev->ffbit) || value > 0xffff)
271 break;
272
273 ff->set_gain(dev, value);
274 break;
275
276 case FF_AUTOCENTER:
277 if (!test_bit(FF_AUTOCENTER, dev->ffbit) || value > 0xffff)
278 break;
279
280 ff->set_autocenter(dev, value);
281 break;
282
283 default:
284 if (check_effect_access(ff, code, NULL) == 0)
285 ff->playback(dev, code, value);
286 break;
287 }
288
289 mutex_unlock(&ff->mutex);
290 return 0;
291}
292EXPORT_SYMBOL_GPL(input_ff_event);
293
294
295
296
297
298
299
300
301
302
303
304
305int input_ff_create(struct input_dev *dev, int max_effects)
306{
307 struct ff_device *ff;
308 int i;
309
310 if (!max_effects) {
311 printk(KERN_ERR
312 "ff-core: cannot allocate device without any effects\n");
313 return -EINVAL;
314 }
315
316 ff = kzalloc(sizeof(struct ff_device) +
317 max_effects * sizeof(struct file *), GFP_KERNEL);
318 if (!ff)
319 return -ENOMEM;
320
321 ff->effects = kcalloc(max_effects, sizeof(struct ff_effect),
322 GFP_KERNEL);
323 if (!ff->effects) {
324 kfree(ff);
325 return -ENOMEM;
326 }
327
328 ff->max_effects = max_effects;
329 mutex_init(&ff->mutex);
330
331 dev->ff = ff;
332 dev->flush = flush_effects;
333 dev->event = input_ff_event;
334 set_bit(EV_FF, dev->evbit);
335
336
337 for (i = 0; i <= FF_MAX; i++)
338 if (test_bit(i, dev->ffbit))
339 set_bit(i, ff->ffbit);
340
341
342 if (test_bit(FF_PERIODIC, ff->ffbit))
343 set_bit(FF_RUMBLE, dev->ffbit);
344
345 return 0;
346}
347EXPORT_SYMBOL_GPL(input_ff_create);
348
349
350
351
352
353
354
355
356
357void input_ff_destroy(struct input_dev *dev)
358{
359 clear_bit(EV_FF, dev->evbit);
360 if (dev->ff) {
361 if (dev->ff->destroy)
362 dev->ff->destroy(dev->ff);
363 kfree(dev->ff->private);
364 kfree(dev->ff);
365 dev->ff = NULL;
366 }
367}
368EXPORT_SYMBOL_GPL(input_ff_destroy);
369