diff options
Diffstat (limited to 'drivers/usb/input/hid-tmff.c')
-rw-r--r-- | drivers/usb/input/hid-tmff.c | 399 |
1 files changed, 41 insertions, 358 deletions
diff --git a/drivers/usb/input/hid-tmff.c b/drivers/usb/input/hid-tmff.c index bc6c54c4c7d3..2d5be4c318ac 100644 --- a/drivers/usb/input/hid-tmff.c +++ b/drivers/usb/input/hid-tmff.c | |||
@@ -28,97 +28,65 @@ | |||
28 | */ | 28 | */ |
29 | 29 | ||
30 | #include <linux/input.h> | 30 | #include <linux/input.h> |
31 | #include <linux/sched.h> | ||
32 | 31 | ||
33 | #undef DEBUG | 32 | #undef DEBUG |
34 | #include <linux/usb.h> | 33 | #include <linux/usb.h> |
35 | 34 | ||
36 | #include <linux/circ_buf.h> | ||
37 | |||
38 | #include "hid.h" | 35 | #include "hid.h" |
39 | #include "../../input/fixp-arith.h" | ||
40 | 36 | ||
41 | /* Usages for thrustmaster devices I know about */ | 37 | /* Usages for thrustmaster devices I know about */ |
42 | #define THRUSTMASTER_USAGE_RUMBLE_LR (HID_UP_GENDESK | 0xbb) | 38 | #define THRUSTMASTER_USAGE_RUMBLE_LR (HID_UP_GENDESK | 0xbb) |
43 | #define DELAY_CALC(t,delay) ((t) + (delay)*HZ/1000) | ||
44 | |||
45 | /* Effect status */ | ||
46 | #define EFFECT_STARTED 0 /* Effect is going to play after some time */ | ||
47 | #define EFFECT_PLAYING 1 /* Effect is playing */ | ||
48 | #define EFFECT_USED 2 | ||
49 | |||
50 | /* For tmff_device::flags */ | ||
51 | #define DEVICE_CLOSING 0 /* The driver is being unitialised */ | ||
52 | |||
53 | /* Check that the current process can access an effect */ | ||
54 | #define CHECK_OWNERSHIP(effect) (current->pid == 0 \ | ||
55 | || effect.owner == current->pid) | ||
56 | |||
57 | #define TMFF_CHECK_ID(id) ((id) >= 0 && (id) < TMFF_EFFECTS) | ||
58 | 39 | ||
59 | #define TMFF_CHECK_OWNERSHIP(i, l) \ | ||
60 | (test_bit(EFFECT_USED, l->effects[i].flags) \ | ||
61 | && CHECK_OWNERSHIP(l->effects[i])) | ||
62 | |||
63 | #define TMFF_EFFECTS 8 | ||
64 | |||
65 | struct tmff_effect { | ||
66 | pid_t owner; | ||
67 | |||
68 | struct ff_effect effect; | ||
69 | |||
70 | unsigned long flags[1]; | ||
71 | unsigned int count; /* Number of times left to play */ | ||
72 | |||
73 | unsigned long play_at; /* When the effect starts to play */ | ||
74 | unsigned long stop_at; /* When the effect ends */ | ||
75 | }; | ||
76 | 40 | ||
77 | struct tmff_device { | 41 | struct tmff_device { |
78 | struct hid_device *hid; | ||
79 | |||
80 | struct hid_report *report; | 42 | struct hid_report *report; |
81 | |||
82 | struct hid_field *rumble; | 43 | struct hid_field *rumble; |
44 | }; | ||
83 | 45 | ||
84 | unsigned int effects_playing; | 46 | /* Changes values from 0 to 0xffff into values from minimum to maximum */ |
85 | struct tmff_effect effects[TMFF_EFFECTS]; | 47 | static inline int hid_tmff_scale(unsigned int in, int minimum, int maximum) |
86 | spinlock_t lock; /* device-level lock. Having locks on | 48 | { |
87 | a per-effect basis could be nice, but | 49 | int ret; |
88 | isn't really necessary */ | ||
89 | 50 | ||
90 | unsigned long flags[1]; /* Contains various information about the | 51 | ret = (in * (maximum - minimum) / 0xffff) + minimum; |
91 | state of the driver for this device */ | 52 | if (ret < minimum) |
53 | return minimum; | ||
54 | if (ret > maximum) | ||
55 | return maximum; | ||
56 | return ret; | ||
57 | } | ||
92 | 58 | ||
93 | struct timer_list timer; | 59 | static int hid_tmff_play(struct input_dev *dev, void *data, struct ff_effect *effect) |
94 | }; | 60 | { |
61 | struct hid_device *hid = dev->private; | ||
62 | struct tmff_device *tmff = data; | ||
63 | int left, right; /* Rumbling */ | ||
95 | 64 | ||
96 | /* Callbacks */ | 65 | left = hid_tmff_scale(effect->u.rumble.weak_magnitude, |
97 | static void hid_tmff_exit(struct hid_device *hid); | 66 | tmff->rumble->logical_minimum, tmff->rumble->logical_maximum); |
98 | static int hid_tmff_event(struct hid_device *hid, struct input_dev *input, | 67 | right = hid_tmff_scale(effect->u.rumble.strong_magnitude, |
99 | unsigned int type, unsigned int code, int value); | 68 | tmff->rumble->logical_minimum, tmff->rumble->logical_maximum); |
100 | static int hid_tmff_flush(struct input_dev *input, struct file *file); | ||
101 | static int hid_tmff_upload_effect(struct input_dev *input, | ||
102 | struct ff_effect *effect); | ||
103 | static int hid_tmff_erase(struct input_dev *input, int id); | ||
104 | 69 | ||
105 | /* Local functions */ | 70 | tmff->rumble->value[0] = left; |
106 | static void hid_tmff_recalculate_timer(struct tmff_device *tmff); | 71 | tmff->rumble->value[1] = right; |
107 | static void hid_tmff_timer(unsigned long timer_data); | 72 | dbg("(left,right)=(%08x, %08x)", left, right); |
73 | hid_submit_report(hid, tmff->report, USB_DIR_OUT); | ||
74 | |||
75 | return 0; | ||
76 | } | ||
108 | 77 | ||
109 | int hid_tmff_init(struct hid_device *hid) | 78 | int hid_tmff_init(struct hid_device *hid) |
110 | { | 79 | { |
111 | struct tmff_device *private; | 80 | struct tmff_device *tmff; |
112 | struct list_head *pos; | 81 | struct list_head *pos; |
113 | struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list); | 82 | struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list); |
114 | struct input_dev *input_dev = hidinput->input; | 83 | struct input_dev *input_dev = hidinput->input; |
84 | int error; | ||
115 | 85 | ||
116 | private = kzalloc(sizeof(struct tmff_device), GFP_KERNEL); | 86 | tmff = kzalloc(sizeof(struct tmff_device), GFP_KERNEL); |
117 | if (!private) | 87 | if (!tmff) |
118 | return -ENOMEM; | 88 | return -ENOMEM; |
119 | 89 | ||
120 | hid->ff_private = private; | ||
121 | |||
122 | /* Find the report to use */ | 90 | /* Find the report to use */ |
123 | __list_for_each(pos, &hid->report_enum[HID_OUTPUT_REPORT].report_list) { | 91 | __list_for_each(pos, &hid->report_enum[HID_OUTPUT_REPORT].report_list) { |
124 | struct hid_report *report = (struct hid_report *)pos; | 92 | struct hid_report *report = (struct hid_report *)pos; |
@@ -142,18 +110,18 @@ int hid_tmff_init(struct hid_device *hid) | |||
142 | continue; | 110 | continue; |
143 | } | 111 | } |
144 | 112 | ||
145 | if (private->report && private->report != report) { | 113 | if (tmff->report && tmff->report != report) { |
146 | warn("ignoring THRUSTMASTER_USAGE_RUMBLE_LR in other report"); | 114 | warn("ignoring THRUSTMASTER_USAGE_RUMBLE_LR in other report"); |
147 | continue; | 115 | continue; |
148 | } | 116 | } |
149 | 117 | ||
150 | if (private->rumble && private->rumble != field) { | 118 | if (tmff->rumble && tmff->rumble != field) { |
151 | warn("ignoring duplicate THRUSTMASTER_USAGE_RUMBLE_LR"); | 119 | warn("ignoring duplicate THRUSTMASTER_USAGE_RUMBLE_LR"); |
152 | continue; | 120 | continue; |
153 | } | 121 | } |
154 | 122 | ||
155 | private->report = report; | 123 | tmff->report = report; |
156 | private->rumble = field; | 124 | tmff->rumble = field; |
157 | 125 | ||
158 | set_bit(FF_RUMBLE, input_dev->ffbit); | 126 | set_bit(FF_RUMBLE, input_dev->ffbit); |
159 | break; | 127 | break; |
@@ -162,302 +130,17 @@ int hid_tmff_init(struct hid_device *hid) | |||
162 | warn("ignoring unknown output usage %08x", field->usage[0].hid); | 130 | warn("ignoring unknown output usage %08x", field->usage[0].hid); |
163 | continue; | 131 | continue; |
164 | } | 132 | } |
165 | |||
166 | /* Fallthrough to here only when a valid usage is found */ | ||
167 | input_dev->upload_effect = hid_tmff_upload_effect; | ||
168 | input_dev->flush = hid_tmff_flush; | ||
169 | |||
170 | set_bit(EV_FF, input_dev->evbit); | ||
171 | input_dev->ff_effects_max = TMFF_EFFECTS; | ||
172 | } | 133 | } |
173 | } | 134 | } |
174 | 135 | ||
175 | private->hid = hid; | 136 | error = input_ff_create_memless(input_dev, tmff, hid_tmff_play); |
176 | 137 | if (error) { | |
177 | spin_lock_init(&private->lock); | 138 | kfree(tmff); |
178 | init_timer(&private->timer); | 139 | return error; |
179 | private->timer.data = (unsigned long)private; | ||
180 | private->timer.function = hid_tmff_timer; | ||
181 | |||
182 | /* Event and exit callbacks */ | ||
183 | hid->ff_exit = hid_tmff_exit; | ||
184 | hid->ff_event = hid_tmff_event; | ||
185 | |||
186 | info("Force feedback for ThrustMaster rumble pad devices by Zinx Verituse <zinx@epicsol.org>"); | ||
187 | |||
188 | return 0; | ||
189 | } | ||
190 | |||
191 | static void hid_tmff_exit(struct hid_device *hid) | ||
192 | { | ||
193 | struct tmff_device *tmff = hid->ff_private; | ||
194 | unsigned long flags; | ||
195 | |||
196 | spin_lock_irqsave(&tmff->lock, flags); | ||
197 | |||
198 | set_bit(DEVICE_CLOSING, tmff->flags); | ||
199 | del_timer_sync(&tmff->timer); | ||
200 | |||
201 | spin_unlock_irqrestore(&tmff->lock, flags); | ||
202 | |||
203 | kfree(tmff); | ||
204 | } | ||
205 | |||
206 | static int hid_tmff_event(struct hid_device *hid, struct input_dev *input, | ||
207 | unsigned int type, unsigned int code, int value) | ||
208 | { | ||
209 | struct tmff_device *tmff = hid->ff_private; | ||
210 | struct tmff_effect *effect = &tmff->effects[code]; | ||
211 | unsigned long flags; | ||
212 | |||
213 | if (type != EV_FF) | ||
214 | return -EINVAL; | ||
215 | if (!TMFF_CHECK_ID(code)) | ||
216 | return -EINVAL; | ||
217 | if (!TMFF_CHECK_OWNERSHIP(code, tmff)) | ||
218 | return -EACCES; | ||
219 | if (value < 0) | ||
220 | return -EINVAL; | ||
221 | |||
222 | spin_lock_irqsave(&tmff->lock, flags); | ||
223 | |||
224 | if (value > 0) { | ||
225 | set_bit(EFFECT_STARTED, effect->flags); | ||
226 | clear_bit(EFFECT_PLAYING, effect->flags); | ||
227 | effect->count = value; | ||
228 | effect->play_at = DELAY_CALC(jiffies, effect->effect.replay.delay); | ||
229 | } else { | ||
230 | clear_bit(EFFECT_STARTED, effect->flags); | ||
231 | clear_bit(EFFECT_PLAYING, effect->flags); | ||
232 | } | ||
233 | |||
234 | hid_tmff_recalculate_timer(tmff); | ||
235 | |||
236 | spin_unlock_irqrestore(&tmff->lock, flags); | ||
237 | |||
238 | return 0; | ||
239 | |||
240 | } | ||
241 | |||
242 | /* Erase all effects this process owns */ | ||
243 | |||
244 | static int hid_tmff_flush(struct input_dev *dev, struct file *file) | ||
245 | { | ||
246 | struct hid_device *hid = dev->private; | ||
247 | struct tmff_device *tmff = hid->ff_private; | ||
248 | int i; | ||
249 | |||
250 | for (i=0; i<dev->ff_effects_max; ++i) | ||
251 | |||
252 | /* NOTE: no need to lock here. The only times EFFECT_USED is | ||
253 | modified is when effects are uploaded or when an effect is | ||
254 | erased. But a process cannot close its dev/input/eventX fd | ||
255 | and perform ioctls on the same fd all at the same time */ | ||
256 | |||
257 | if (current->pid == tmff->effects[i].owner | ||
258 | && test_bit(EFFECT_USED, tmff->effects[i].flags)) | ||
259 | if (hid_tmff_erase(dev, i)) | ||
260 | warn("erase effect %d failed", i); | ||
261 | |||
262 | |||
263 | return 0; | ||
264 | } | ||
265 | |||
266 | static int hid_tmff_erase(struct input_dev *dev, int id) | ||
267 | { | ||
268 | struct hid_device *hid = dev->private; | ||
269 | struct tmff_device *tmff = hid->ff_private; | ||
270 | unsigned long flags; | ||
271 | |||
272 | if (!TMFF_CHECK_ID(id)) | ||
273 | return -EINVAL; | ||
274 | if (!TMFF_CHECK_OWNERSHIP(id, tmff)) | ||
275 | return -EACCES; | ||
276 | |||
277 | spin_lock_irqsave(&tmff->lock, flags); | ||
278 | |||
279 | tmff->effects[id].flags[0] = 0; | ||
280 | hid_tmff_recalculate_timer(tmff); | ||
281 | |||
282 | spin_unlock_irqrestore(&tmff->lock, flags); | ||
283 | |||
284 | return 0; | ||
285 | } | ||
286 | |||
287 | static int hid_tmff_upload_effect(struct input_dev *input, | ||
288 | struct ff_effect *effect) | ||
289 | { | ||
290 | struct hid_device *hid = input->private; | ||
291 | struct tmff_device *tmff = hid->ff_private; | ||
292 | int id; | ||
293 | unsigned long flags; | ||
294 | |||
295 | if (!test_bit(effect->type, input->ffbit)) | ||
296 | return -EINVAL; | ||
297 | if (effect->id != -1 && !TMFF_CHECK_ID(effect->id)) | ||
298 | return -EINVAL; | ||
299 | |||
300 | spin_lock_irqsave(&tmff->lock, flags); | ||
301 | |||
302 | if (effect->id == -1) { | ||
303 | /* Find a free effect */ | ||
304 | for (id = 0; id < TMFF_EFFECTS && test_bit(EFFECT_USED, tmff->effects[id].flags); ++id); | ||
305 | |||
306 | if (id >= TMFF_EFFECTS) { | ||
307 | spin_unlock_irqrestore(&tmff->lock, flags); | ||
308 | return -ENOSPC; | ||
309 | } | ||
310 | |||
311 | effect->id = id; | ||
312 | tmff->effects[id].owner = current->pid; | ||
313 | tmff->effects[id].flags[0] = 0; | ||
314 | set_bit(EFFECT_USED, tmff->effects[id].flags); | ||
315 | |||
316 | } else { | ||
317 | /* Re-uploading an owned effect, to change parameters */ | ||
318 | id = effect->id; | ||
319 | clear_bit(EFFECT_PLAYING, tmff->effects[id].flags); | ||
320 | } | 140 | } |
321 | 141 | ||
322 | tmff->effects[id].effect = *effect; | 142 | info("Force feedback for ThrustMaster rumble pad devices by Zinx Verituse <zinx@epicsol.org>"); |
323 | |||
324 | hid_tmff_recalculate_timer(tmff); | ||
325 | 143 | ||
326 | spin_unlock_irqrestore(&tmff->lock, flags); | ||
327 | return 0; | 144 | return 0; |
328 | } | 145 | } |
329 | 146 | ||
330 | /* Start the timer for the next start/stop/delay */ | ||
331 | /* Always call this while tmff->lock is locked */ | ||
332 | |||
333 | static void hid_tmff_recalculate_timer(struct tmff_device *tmff) | ||
334 | { | ||
335 | int i; | ||
336 | int events = 0; | ||
337 | unsigned long next_time; | ||
338 | |||
339 | next_time = 0; /* Shut up compiler's incorrect warning */ | ||
340 | |||
341 | /* Find the next change in an effect's status */ | ||
342 | for (i = 0; i < TMFF_EFFECTS; ++i) { | ||
343 | struct tmff_effect *effect = &tmff->effects[i]; | ||
344 | unsigned long play_time; | ||
345 | |||
346 | if (!test_bit(EFFECT_STARTED, effect->flags)) | ||
347 | continue; | ||
348 | |||
349 | effect->stop_at = DELAY_CALC(effect->play_at, effect->effect.replay.length); | ||
350 | |||
351 | if (!test_bit(EFFECT_PLAYING, effect->flags)) | ||
352 | play_time = effect->play_at; | ||
353 | else | ||
354 | play_time = effect->stop_at; | ||
355 | |||
356 | events++; | ||
357 | |||
358 | if (time_after(jiffies, play_time)) | ||
359 | play_time = jiffies; | ||
360 | |||
361 | if (events == 1) | ||
362 | next_time = play_time; | ||
363 | else { | ||
364 | if (time_after(next_time, play_time)) | ||
365 | next_time = play_time; | ||
366 | } | ||
367 | } | ||
368 | |||
369 | if (!events && tmff->effects_playing) { | ||
370 | /* Treat all effects turning off as an event */ | ||
371 | events = 1; | ||
372 | next_time = jiffies; | ||
373 | } | ||
374 | |||
375 | if (!events) { | ||
376 | /* No events, no time, no need for a timer. */ | ||
377 | del_timer_sync(&tmff->timer); | ||
378 | return; | ||
379 | } | ||
380 | |||
381 | mod_timer(&tmff->timer, next_time); | ||
382 | } | ||
383 | |||
384 | /* Changes values from 0 to 0xffff into values from minimum to maximum */ | ||
385 | static inline int hid_tmff_scale(unsigned int in, int minimum, int maximum) | ||
386 | { | ||
387 | int ret; | ||
388 | |||
389 | ret = (in * (maximum - minimum) / 0xffff) + minimum; | ||
390 | if (ret < minimum) | ||
391 | return minimum; | ||
392 | if (ret > maximum) | ||
393 | return maximum; | ||
394 | return ret; | ||
395 | } | ||
396 | |||
397 | static void hid_tmff_timer(unsigned long timer_data) | ||
398 | { | ||
399 | struct tmff_device *tmff = (struct tmff_device *) timer_data; | ||
400 | struct hid_device *hid = tmff->hid; | ||
401 | unsigned long flags; | ||
402 | int left = 0, right = 0; /* Rumbling */ | ||
403 | int i; | ||
404 | |||
405 | spin_lock_irqsave(&tmff->lock, flags); | ||
406 | |||
407 | tmff->effects_playing = 0; | ||
408 | |||
409 | for (i = 0; i < TMFF_EFFECTS; ++i) { | ||
410 | struct tmff_effect *effect = &tmff->effects[i]; | ||
411 | |||
412 | if (!test_bit(EFFECT_STARTED, effect->flags)) | ||
413 | continue; | ||
414 | |||
415 | if (!time_after(jiffies, effect->play_at)) | ||
416 | continue; | ||
417 | |||
418 | if (time_after(jiffies, effect->stop_at)) { | ||
419 | |||
420 | dbg("Finished playing once %d", i); | ||
421 | clear_bit(EFFECT_PLAYING, effect->flags); | ||
422 | |||
423 | if (--effect->count <= 0) { | ||
424 | dbg("Stopped %d", i); | ||
425 | clear_bit(EFFECT_STARTED, effect->flags); | ||
426 | continue; | ||
427 | } else { | ||
428 | dbg("Start again %d", i); | ||
429 | effect->play_at = DELAY_CALC(jiffies, effect->effect.replay.delay); | ||
430 | continue; | ||
431 | } | ||
432 | } | ||
433 | |||
434 | ++tmff->effects_playing; | ||
435 | |||
436 | set_bit(EFFECT_PLAYING, effect->flags); | ||
437 | |||
438 | switch (effect->effect.type) { | ||
439 | case FF_RUMBLE: | ||
440 | right += effect->effect.u.rumble.strong_magnitude; | ||
441 | left += effect->effect.u.rumble.weak_magnitude; | ||
442 | break; | ||
443 | default: | ||
444 | BUG(); | ||
445 | break; | ||
446 | } | ||
447 | } | ||
448 | |||
449 | left = hid_tmff_scale(left, tmff->rumble->logical_minimum, tmff->rumble->logical_maximum); | ||
450 | right = hid_tmff_scale(right, tmff->rumble->logical_minimum, tmff->rumble->logical_maximum); | ||
451 | |||
452 | if (left != tmff->rumble->value[0] || right != tmff->rumble->value[1]) { | ||
453 | tmff->rumble->value[0] = left; | ||
454 | tmff->rumble->value[1] = right; | ||
455 | dbg("(left,right)=(%08x, %08x)", left, right); | ||
456 | hid_submit_report(hid, tmff->report, USB_DIR_OUT); | ||
457 | } | ||
458 | |||
459 | if (!test_bit(DEVICE_CLOSING, tmff->flags)) | ||
460 | hid_tmff_recalculate_timer(tmff); | ||
461 | |||
462 | spin_unlock_irqrestore(&tmff->lock, flags); | ||
463 | } | ||