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
);
114 struct input_dev
*input_dev
= hidinput
->input
;
116 private = kmalloc(sizeof(struct tmff_device
), GFP_KERNEL
);
120 memset(private, 0, sizeof(struct tmff_device
));
121 hid
->ff_private
= private;
123 /* Find the report to use */
124 __list_for_each(pos
, &hid
->report_enum
[HID_OUTPUT_REPORT
].report_list
) {
125 struct hid_report
*report
= (struct hid_report
*)pos
;
128 for (fieldnum
= 0; fieldnum
< report
->maxfield
; ++fieldnum
) {
129 struct hid_field
*field
= report
->field
[fieldnum
];
131 if (field
->maxusage
<= 0)
134 switch (field
->usage
[0].hid
) {
135 case THRUSTMASTER_USAGE_RUMBLE_LR
:
136 if (field
->report_count
< 2) {
137 warn("ignoring THRUSTMASTER_USAGE_RUMBLE_LR with report_count < 2");
141 if (field
->logical_maximum
== field
->logical_minimum
) {
142 warn("ignoring THRUSTMASTER_USAGE_RUMBLE_LR with logical_maximum == logical_minimum");
146 if (private->report
&& private->report
!= report
) {
147 warn("ignoring THRUSTMASTER_USAGE_RUMBLE_LR in other report");
151 if (private->rumble
&& private->rumble
!= field
) {
152 warn("ignoring duplicate THRUSTMASTER_USAGE_RUMBLE_LR");
156 private->report
= report
;
157 private->rumble
= field
;
159 set_bit(FF_RUMBLE
, input_dev
->ffbit
);
163 warn("ignoring unknown output usage %08x", field
->usage
[0].hid
);
167 /* Fallthrough to here only when a valid usage is found */
168 input_dev
->upload_effect
= hid_tmff_upload_effect
;
169 input_dev
->flush
= hid_tmff_flush
;
171 set_bit(EV_FF
, input_dev
->evbit
);
172 input_dev
->ff_effects_max
= TMFF_EFFECTS
;
178 spin_lock_init(&private->lock
);
179 init_timer(&private->timer
);
180 private->timer
.data
= (unsigned long)private;
181 private->timer
.function
= hid_tmff_timer
;
183 /* Event and exit callbacks */
184 hid
->ff_exit
= hid_tmff_exit
;
185 hid
->ff_event
= hid_tmff_event
;
187 info("Force feedback for ThrustMaster rumble pad devices by Zinx Verituse <zinx@epicsol.org>");
192 static void hid_tmff_exit(struct hid_device
*hid
)
194 struct tmff_device
*tmff
= hid
->ff_private
;
197 spin_lock_irqsave(&tmff
->lock
, flags
);
199 set_bit(DEVICE_CLOSING
, tmff
->flags
);
200 del_timer_sync(&tmff
->timer
);
202 spin_unlock_irqrestore(&tmff
->lock
, flags
);
207 static int hid_tmff_event(struct hid_device
*hid
, struct input_dev
*input
,
208 unsigned int type
, unsigned int code
, int value
)
210 struct tmff_device
*tmff
= hid
->ff_private
;
211 struct tmff_effect
*effect
= &tmff
->effects
[code
];
216 if (!TMFF_CHECK_ID(code
))
218 if (!TMFF_CHECK_OWNERSHIP(code
, tmff
))
223 spin_lock_irqsave(&tmff
->lock
, flags
);
226 set_bit(EFFECT_STARTED
, effect
->flags
);
227 clear_bit(EFFECT_PLAYING
, effect
->flags
);
228 effect
->count
= value
;
229 effect
->play_at
= DELAY_CALC(jiffies
, effect
->effect
.replay
.delay
);
231 clear_bit(EFFECT_STARTED
, effect
->flags
);
232 clear_bit(EFFECT_PLAYING
, effect
->flags
);
235 hid_tmff_recalculate_timer(tmff
);
237 spin_unlock_irqrestore(&tmff
->lock
, flags
);
243 /* Erase all effects this process owns */
245 static int hid_tmff_flush(struct input_dev
*dev
, struct file
*file
)
247 struct hid_device
*hid
= dev
->private;
248 struct tmff_device
*tmff
= hid
->ff_private
;
251 for (i
=0; i
<dev
->ff_effects_max
; ++i
)
253 /* NOTE: no need to lock here. The only times EFFECT_USED is
254 modified is when effects are uploaded or when an effect is
255 erased. But a process cannot close its dev/input/eventX fd
256 and perform ioctls on the same fd all at the same time */
258 if (current
->pid
== tmff
->effects
[i
].owner
259 && test_bit(EFFECT_USED
, tmff
->effects
[i
].flags
))
260 if (hid_tmff_erase(dev
, i
))
261 warn("erase effect %d failed", i
);
267 static int hid_tmff_erase(struct input_dev
*dev
, int id
)
269 struct hid_device
*hid
= dev
->private;
270 struct tmff_device
*tmff
= hid
->ff_private
;
273 if (!TMFF_CHECK_ID(id
))
275 if (!TMFF_CHECK_OWNERSHIP(id
, tmff
))
278 spin_lock_irqsave(&tmff
->lock
, flags
);
280 tmff
->effects
[id
].flags
[0] = 0;
281 hid_tmff_recalculate_timer(tmff
);
283 spin_unlock_irqrestore(&tmff
->lock
, flags
);
288 static int hid_tmff_upload_effect(struct input_dev
*input
,
289 struct ff_effect
*effect
)
291 struct hid_device
*hid
= input
->private;
292 struct tmff_device
*tmff
= hid
->ff_private
;
296 if (!test_bit(effect
->type
, input
->ffbit
))
298 if (effect
->id
!= -1 && !TMFF_CHECK_ID(effect
->id
))
301 spin_lock_irqsave(&tmff
->lock
, flags
);
303 if (effect
->id
== -1) {
304 /* Find a free effect */
305 for (id
= 0; id
< TMFF_EFFECTS
&& test_bit(EFFECT_USED
, tmff
->effects
[id
].flags
); ++id
);
307 if (id
>= TMFF_EFFECTS
) {
308 spin_unlock_irqrestore(&tmff
->lock
, flags
);
313 tmff
->effects
[id
].owner
= current
->pid
;
314 tmff
->effects
[id
].flags
[0] = 0;
315 set_bit(EFFECT_USED
, tmff
->effects
[id
].flags
);
318 /* Re-uploading an owned effect, to change parameters */
320 clear_bit(EFFECT_PLAYING
, tmff
->effects
[id
].flags
);
323 tmff
->effects
[id
].effect
= *effect
;
325 hid_tmff_recalculate_timer(tmff
);
327 spin_unlock_irqrestore(&tmff
->lock
, flags
);
331 /* Start the timer for the next start/stop/delay */
332 /* Always call this while tmff->lock is locked */
334 static void hid_tmff_recalculate_timer(struct tmff_device
*tmff
)
338 unsigned long next_time
;
340 next_time
= 0; /* Shut up compiler's incorrect warning */
342 /* Find the next change in an effect's status */
343 for (i
= 0; i
< TMFF_EFFECTS
; ++i
) {
344 struct tmff_effect
*effect
= &tmff
->effects
[i
];
345 unsigned long play_time
;
347 if (!test_bit(EFFECT_STARTED
, effect
->flags
))
350 effect
->stop_at
= DELAY_CALC(effect
->play_at
, effect
->effect
.replay
.length
);
352 if (!test_bit(EFFECT_PLAYING
, effect
->flags
))
353 play_time
= effect
->play_at
;
355 play_time
= effect
->stop_at
;
359 if (time_after(jiffies
, play_time
))
363 next_time
= play_time
;
365 if (time_after(next_time
, play_time
))
366 next_time
= play_time
;
370 if (!events
&& tmff
->effects_playing
) {
371 /* Treat all effects turning off as an event */
377 /* No events, no time, no need for a timer. */
378 del_timer_sync(&tmff
->timer
);
382 mod_timer(&tmff
->timer
, next_time
);
385 /* Changes values from 0 to 0xffff into values from minimum to maximum */
386 static inline int hid_tmff_scale(unsigned int in
, int minimum
, int maximum
)
390 ret
= (in
* (maximum
- minimum
) / 0xffff) + minimum
;
398 static void hid_tmff_timer(unsigned long timer_data
)
400 struct tmff_device
*tmff
= (struct tmff_device
*) timer_data
;
401 struct hid_device
*hid
= tmff
->hid
;
403 int left
= 0, right
= 0; /* Rumbling */
406 spin_lock_irqsave(&tmff
->lock
, flags
);
408 tmff
->effects_playing
= 0;
410 for (i
= 0; i
< TMFF_EFFECTS
; ++i
) {
411 struct tmff_effect
*effect
= &tmff
->effects
[i
];
413 if (!test_bit(EFFECT_STARTED
, effect
->flags
))
416 if (!time_after(jiffies
, effect
->play_at
))
419 if (time_after(jiffies
, effect
->stop_at
)) {
421 dbg("Finished playing once %d", i
);
422 clear_bit(EFFECT_PLAYING
, effect
->flags
);
424 if (--effect
->count
<= 0) {
425 dbg("Stopped %d", i
);
426 clear_bit(EFFECT_STARTED
, effect
->flags
);
429 dbg("Start again %d", i
);
430 effect
->play_at
= DELAY_CALC(jiffies
, effect
->effect
.replay
.delay
);
435 ++tmff
->effects_playing
;
437 set_bit(EFFECT_PLAYING
, effect
->flags
);
439 switch (effect
->effect
.type
) {
441 right
+= effect
->effect
.u
.rumble
.strong_magnitude
;
442 left
+= effect
->effect
.u
.rumble
.weak_magnitude
;
450 left
= hid_tmff_scale(left
, tmff
->rumble
->logical_minimum
, tmff
->rumble
->logical_maximum
);
451 right
= hid_tmff_scale(right
, tmff
->rumble
->logical_minimum
, tmff
->rumble
->logical_maximum
);
453 if (left
!= tmff
->rumble
->value
[0] || right
!= tmff
->rumble
->value
[1]) {
454 tmff
->rumble
->value
[0] = left
;
455 tmff
->rumble
->value
[1] = right
;
456 dbg("(left,right)=(%08x, %08x)", left
, right
);
457 hid_submit_report(hid
, tmff
->report
, USB_DIR_OUT
);
460 if (!test_bit(DEVICE_CLOSING
, tmff
->flags
))
461 hid_tmff_recalculate_timer(tmff
);
463 spin_unlock_irqrestore(&tmff
->lock
, flags
);