2 * Force feedback support for various HID compliant devices by ThrustMaster:
3 * ThrustMaster FireStorm Dual Power 2
4 * and possibly others whose device ids haven't been added.
6 * Modified to support ThrustMaster devices by Zinx Verituse
7 * on 2003-01-25 from the Logitech force feedback driver,
8 * which is by Johann Deneux.
10 * Copyright (c) 2003 Zinx Verituse <zinx@epicsol.org>
11 * Copyright (c) 2002 Johann Deneux
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 #include <linux/input.h>
31 #include <linux/sched.h>
34 #include <linux/usb.h>
36 #include <linux/circ_buf.h>
39 #include "fixp-arith.h"
41 /* Usages for thrustmaster devices I know about */
42 #define THRUSTMASTER_USAGE_RUMBLE_LR (HID_UP_GENDESK | 0xbb)
43 #define DELAY_CALC(t,delay) ((t) + (delay)*HZ/1000)
46 #define EFFECT_STARTED 0 /* Effect is going to play after some time */
47 #define EFFECT_PLAYING 1 /* Effect is playing */
50 /* For tmff_device::flags */
51 #define DEVICE_CLOSING 0 /* The driver is being unitialised */
53 /* Check that the current process can access an effect */
54 #define CHECK_OWNERSHIP(effect) (current->pid == 0 \
55 || effect.owner == current->pid)
57 #define TMFF_CHECK_ID(id) ((id) >= 0 && (id) < TMFF_EFFECTS)
59 #define TMFF_CHECK_OWNERSHIP(i, l) \
60 (test_bit(EFFECT_USED, l->effects[i].flags) \
61 && CHECK_OWNERSHIP(l->effects[i]))
63 #define TMFF_EFFECTS 8
68 struct ff_effect effect;
70 unsigned long flags[1];
71 unsigned int count; /* Number of times left to play */
73 unsigned long play_at; /* When the effect starts to play */
74 unsigned long stop_at; /* When the effect ends */
78 struct hid_device *hid;
80 struct hid_report *report;
82 struct hid_field *rumble;
84 unsigned int effects_playing;
85 struct tmff_effect effects[TMFF_EFFECTS];
86 spinlock_t lock; /* device-level lock. Having locks on
87 a per-effect basis could be nice, but
88 isn't really necessary */
90 unsigned long flags[1]; /* Contains various information about the
91 state of the driver for this device */
93 struct timer_list timer;
97 static void hid_tmff_exit(struct hid_device *hid);
98 static int hid_tmff_event(struct hid_device *hid, struct input_dev *input,
99 unsigned int type, unsigned int code, int value);
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);
105 /* Local functions */
106 static void hid_tmff_recalculate_timer(struct tmff_device *tmff);
107 static void hid_tmff_timer(unsigned long timer_data);
109 int hid_tmff_init(struct hid_device *hid)
111 struct tmff_device *private;
112 struct list_head *pos;
113 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
115 private = kmalloc(sizeof(struct tmff_device), GFP_KERNEL);
119 memset(private, 0, sizeof(struct tmff_device));
120 hid->ff_private = private;
122 /* Find the report to use */
123 __list_for_each(pos, &hid->report_enum[HID_OUTPUT_REPORT].report_list) {
124 struct hid_report *report = (struct hid_report *)pos;
127 for (fieldnum = 0; fieldnum < report->maxfield; ++fieldnum) {
128 struct hid_field *field = report->field[fieldnum];
130 if (field->maxusage <= 0)
133 switch (field->usage[0].hid) {
134 case THRUSTMASTER_USAGE_RUMBLE_LR:
135 if (field->report_count < 2) {
136 warn("ignoring THRUSTMASTER_USAGE_RUMBLE_LR with report_count < 2");
140 if (field->logical_maximum == field->logical_minimum) {
141 warn("ignoring THRUSTMASTER_USAGE_RUMBLE_LR with logical_maximum == logical_minimum");
145 if (private->report && private->report != report) {
146 warn("ignoring THRUSTMASTER_USAGE_RUMBLE_LR in other report");
150 if (private->rumble && private->rumble != field) {
151 warn("ignoring duplicate THRUSTMASTER_USAGE_RUMBLE_LR");
155 private->report = report;
156 private->rumble = field;
158 set_bit(FF_RUMBLE, hidinput->input.ffbit);
162 warn("ignoring unknown output usage %08x", field->usage[0].hid);
166 /* Fallthrough to here only when a valid usage is found */
167 hidinput->input.upload_effect = hid_tmff_upload_effect;
168 hidinput->input.flush = hid_tmff_flush;
170 set_bit(EV_FF, hidinput->input.evbit);
171 hidinput->input.ff_effects_max = TMFF_EFFECTS;
177 spin_lock_init(&private->lock);
178 init_timer(&private->timer);
179 private->timer.data = (unsigned long)private;
180 private->timer.function = hid_tmff_timer;
182 /* Event and exit callbacks */
183 hid->ff_exit = hid_tmff_exit;
184 hid->ff_event = hid_tmff_event;
186 info("Force feedback for ThrustMaster rumble pad devices by Zinx Verituse <zinx@epicsol.org>");
191 static void hid_tmff_exit(struct hid_device *hid)
193 struct tmff_device *tmff = hid->ff_private;
196 spin_lock_irqsave(&tmff->lock, flags);
198 set_bit(DEVICE_CLOSING, tmff->flags);
199 del_timer_sync(&tmff->timer);
201 spin_unlock_irqrestore(&tmff->lock, flags);
206 static int hid_tmff_event(struct hid_device *hid, struct input_dev *input,
207 unsigned int type, unsigned int code, int value)
209 struct tmff_device *tmff = hid->ff_private;
210 struct tmff_effect *effect = &tmff->effects[code];
215 if (!TMFF_CHECK_ID(code))
217 if (!TMFF_CHECK_OWNERSHIP(code, tmff))
222 spin_lock_irqsave(&tmff->lock, flags);
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);
230 clear_bit(EFFECT_STARTED, effect->flags);
231 clear_bit(EFFECT_PLAYING, effect->flags);
234 hid_tmff_recalculate_timer(tmff);
236 spin_unlock_irqrestore(&tmff->lock, flags);
242 /* Erase all effects this process owns */
244 static int hid_tmff_flush(struct input_dev *dev, struct file *file)
246 struct hid_device *hid = dev->private;
247 struct tmff_device *tmff = hid->ff_private;
250 for (i=0; i<dev->ff_effects_max; ++i)
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 */
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);
266 static int hid_tmff_erase(struct input_dev *dev, int id)
268 struct hid_device *hid = dev->private;
269 struct tmff_device *tmff = hid->ff_private;
272 if (!TMFF_CHECK_ID(id))
274 if (!TMFF_CHECK_OWNERSHIP(id, tmff))
277 spin_lock_irqsave(&tmff->lock, flags);
279 tmff->effects[id].flags[0] = 0;
280 hid_tmff_recalculate_timer(tmff);
282 spin_unlock_irqrestore(&tmff->lock, flags);
287 static int hid_tmff_upload_effect(struct input_dev *input,
288 struct ff_effect *effect)
290 struct hid_device *hid = input->private;
291 struct tmff_device *tmff = hid->ff_private;
295 if (!test_bit(effect->type, input->ffbit))
297 if (effect->id != -1 && !TMFF_CHECK_ID(effect->id))
300 spin_lock_irqsave(&tmff->lock, flags);
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);
306 if (id >= TMFF_EFFECTS) {
307 spin_unlock_irqrestore(&tmff->lock, flags);
312 tmff->effects[id].owner = current->pid;
313 tmff->effects[id].flags[0] = 0;
314 set_bit(EFFECT_USED, tmff->effects[id].flags);
317 /* Re-uploading an owned effect, to change parameters */
319 clear_bit(EFFECT_PLAYING, tmff->effects[id].flags);
322 tmff->effects[id].effect = *effect;
324 hid_tmff_recalculate_timer(tmff);
326 spin_unlock_irqrestore(&tmff->lock, flags);
330 /* Start the timer for the next start/stop/delay */
331 /* Always call this while tmff->lock is locked */
333 static void hid_tmff_recalculate_timer(struct tmff_device *tmff)
337 unsigned long next_time;
339 next_time = 0; /* Shut up compiler's incorrect warning */
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;
346 if (!test_bit(EFFECT_STARTED, effect->flags))
349 effect->stop_at = DELAY_CALC(effect->play_at, effect->effect.replay.length);
351 if (!test_bit(EFFECT_PLAYING, effect->flags))
352 play_time = effect->play_at;
354 play_time = effect->stop_at;
358 if (time_after(jiffies, play_time))
362 next_time = play_time;
364 if (time_after(next_time, play_time))
365 next_time = play_time;
369 if (!events && tmff->effects_playing) {
370 /* Treat all effects turning off as an event */
376 /* No events, no time, no need for a timer. */
377 del_timer_sync(&tmff->timer);
381 mod_timer(&tmff->timer, next_time);
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)
389 ret = (in * (maximum - minimum) / 0xffff) + minimum;
397 static void hid_tmff_timer(unsigned long timer_data)
399 struct tmff_device *tmff = (struct tmff_device *) timer_data;
400 struct hid_device *hid = tmff->hid;
402 int left = 0, right = 0; /* Rumbling */
405 spin_lock_irqsave(&tmff->lock, flags);
407 tmff->effects_playing = 0;
409 for (i = 0; i < TMFF_EFFECTS; ++i) {
410 struct tmff_effect *effect = &tmff->effects[i];
412 if (!test_bit(EFFECT_STARTED, effect->flags))
415 if (!time_after(jiffies, effect->play_at))
418 if (time_after(jiffies, effect->stop_at)) {
420 dbg("Finished playing once %d", i);
421 clear_bit(EFFECT_PLAYING, effect->flags);
423 if (--effect->count <= 0) {
424 dbg("Stopped %d", i);
425 clear_bit(EFFECT_STARTED, effect->flags);
428 dbg("Start again %d", i);
429 effect->play_at = DELAY_CALC(jiffies, effect->effect.replay.delay);
434 ++tmff->effects_playing;
436 set_bit(EFFECT_PLAYING, effect->flags);
438 switch (effect->effect.type) {
440 right += effect->effect.u.rumble.strong_magnitude;
441 left += effect->effect.u.rumble.weak_magnitude;
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);
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);
459 if (!test_bit(DEVICE_CLOSING, tmff->flags))
460 hid_tmff_recalculate_timer(tmff);
462 spin_unlock_irqrestore(&tmff->lock, flags);