2 * Copyright (c) 2000-2002 Vojtech Pavlik <vojtech@ucw.cz>
3 * Copyright (c) 2001-2002, 2007 Johann Deneux <johann.deneux@gmail.com>
5 * USB/RS232 I-Force joysticks and wheels.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 * Set the magnitude of a constant force effect
30 * Note: caller must ensure exclusive access to device
33 static int make_magnitude_modifier(struct iforce
* iforce
,
34 struct resource
* mod_chunk
, int no_alloc
, __s16 level
)
36 unsigned char data
[3];
39 mutex_lock(&iforce
->mem_mutex
);
40 if (allocate_resource(&(iforce
->device_memory
), mod_chunk
, 2,
41 iforce
->device_memory
.start
, iforce
->device_memory
.end
, 2L,
43 mutex_unlock(&iforce
->mem_mutex
);
46 mutex_unlock(&iforce
->mem_mutex
);
49 data
[0] = LO(mod_chunk
->start
);
50 data
[1] = HI(mod_chunk
->start
);
51 data
[2] = HIFIX80(level
);
53 iforce_send_packet(iforce
, FF_CMD_MAGNITUDE
, data
);
55 iforce_dump_packet(iforce
, "magnitude", FF_CMD_MAGNITUDE
, data
);
60 * Upload the component of an effect dealing with the period, phase and magnitude
63 static int make_period_modifier(struct iforce
* iforce
,
64 struct resource
* mod_chunk
, int no_alloc
,
65 __s16 magnitude
, __s16 offset
, u16 period
, u16 phase
)
67 unsigned char data
[7];
69 period
= TIME_SCALE(period
);
72 mutex_lock(&iforce
->mem_mutex
);
73 if (allocate_resource(&(iforce
->device_memory
), mod_chunk
, 0x0c,
74 iforce
->device_memory
.start
, iforce
->device_memory
.end
, 2L,
76 mutex_unlock(&iforce
->mem_mutex
);
79 mutex_unlock(&iforce
->mem_mutex
);
82 data
[0] = LO(mod_chunk
->start
);
83 data
[1] = HI(mod_chunk
->start
);
85 data
[2] = HIFIX80(magnitude
);
86 data
[3] = HIFIX80(offset
);
92 iforce_send_packet(iforce
, FF_CMD_PERIOD
, data
);
98 * Uploads the part of an effect setting the envelope of the force
101 static int make_envelope_modifier(struct iforce
* iforce
,
102 struct resource
* mod_chunk
, int no_alloc
,
103 u16 attack_duration
, __s16 initial_level
,
104 u16 fade_duration
, __s16 final_level
)
106 unsigned char data
[8];
108 attack_duration
= TIME_SCALE(attack_duration
);
109 fade_duration
= TIME_SCALE(fade_duration
);
112 mutex_lock(&iforce
->mem_mutex
);
113 if (allocate_resource(&(iforce
->device_memory
), mod_chunk
, 0x0e,
114 iforce
->device_memory
.start
, iforce
->device_memory
.end
, 2L,
116 mutex_unlock(&iforce
->mem_mutex
);
119 mutex_unlock(&iforce
->mem_mutex
);
122 data
[0] = LO(mod_chunk
->start
);
123 data
[1] = HI(mod_chunk
->start
);
125 data
[2] = LO(attack_duration
);
126 data
[3] = HI(attack_duration
);
127 data
[4] = HI(initial_level
);
129 data
[5] = LO(fade_duration
);
130 data
[6] = HI(fade_duration
);
131 data
[7] = HI(final_level
);
133 iforce_send_packet(iforce
, FF_CMD_ENVELOPE
, data
);
139 * Component of spring, friction, inertia... effects
142 static int make_condition_modifier(struct iforce
* iforce
,
143 struct resource
* mod_chunk
, int no_alloc
,
144 __u16 rsat
, __u16 lsat
, __s16 rk
, __s16 lk
, u16 db
, __s16 center
)
146 unsigned char data
[10];
149 mutex_lock(&iforce
->mem_mutex
);
150 if (allocate_resource(&(iforce
->device_memory
), mod_chunk
, 8,
151 iforce
->device_memory
.start
, iforce
->device_memory
.end
, 2L,
153 mutex_unlock(&iforce
->mem_mutex
);
156 mutex_unlock(&iforce
->mem_mutex
);
159 data
[0] = LO(mod_chunk
->start
);
160 data
[1] = HI(mod_chunk
->start
);
162 data
[2] = (100 * rk
) >> 15; /* Dangerous: the sign is extended by gcc on plateforms providing an arith shift */
163 data
[3] = (100 * lk
) >> 15; /* This code is incorrect on cpus lacking arith shift */
165 center
= (500 * center
) >> 15;
166 data
[4] = LO(center
);
167 data
[5] = HI(center
);
169 db
= (1000 * db
) >> 16;
173 data
[8] = (100 * rsat
) >> 16;
174 data
[9] = (100 * lsat
) >> 16;
176 iforce_send_packet(iforce
, FF_CMD_CONDITION
, data
);
177 iforce_dump_packet(iforce
, "condition", FF_CMD_CONDITION
, data
);
182 static unsigned char find_button(struct iforce
*iforce
, signed short button
)
186 for (i
= 1; iforce
->type
->btn
[i
] >= 0; i
++)
187 if (iforce
->type
->btn
[i
] == button
)
193 * Analyse the changes in an effect, and tell if we need to send an condition
196 static int need_condition_modifier(struct iforce
*iforce
,
197 struct ff_effect
*old
,
198 struct ff_effect
*new)
203 if (new->type
!= FF_SPRING
&& new->type
!= FF_FRICTION
) {
204 dev_warn(&iforce
->dev
->dev
, "bad effect type in %s\n",
209 for (i
= 0; i
< 2; i
++) {
210 ret
|= old
->u
.condition
[i
].right_saturation
!= new->u
.condition
[i
].right_saturation
211 || old
->u
.condition
[i
].left_saturation
!= new->u
.condition
[i
].left_saturation
212 || old
->u
.condition
[i
].right_coeff
!= new->u
.condition
[i
].right_coeff
213 || old
->u
.condition
[i
].left_coeff
!= new->u
.condition
[i
].left_coeff
214 || old
->u
.condition
[i
].deadband
!= new->u
.condition
[i
].deadband
215 || old
->u
.condition
[i
].center
!= new->u
.condition
[i
].center
;
221 * Analyse the changes in an effect, and tell if we need to send a magnitude
224 static int need_magnitude_modifier(struct iforce
*iforce
,
225 struct ff_effect
*old
,
226 struct ff_effect
*effect
)
228 if (effect
->type
!= FF_CONSTANT
) {
229 dev_warn(&iforce
->dev
->dev
, "bad effect type in %s\n",
234 return old
->u
.constant
.level
!= effect
->u
.constant
.level
;
238 * Analyse the changes in an effect, and tell if we need to send an envelope
241 static int need_envelope_modifier(struct iforce
*iforce
, struct ff_effect
*old
,
242 struct ff_effect
*effect
)
244 switch (effect
->type
) {
246 if (old
->u
.constant
.envelope
.attack_length
!= effect
->u
.constant
.envelope
.attack_length
247 || old
->u
.constant
.envelope
.attack_level
!= effect
->u
.constant
.envelope
.attack_level
248 || old
->u
.constant
.envelope
.fade_length
!= effect
->u
.constant
.envelope
.fade_length
249 || old
->u
.constant
.envelope
.fade_level
!= effect
->u
.constant
.envelope
.fade_level
)
254 if (old
->u
.periodic
.envelope
.attack_length
!= effect
->u
.periodic
.envelope
.attack_length
255 || old
->u
.periodic
.envelope
.attack_level
!= effect
->u
.periodic
.envelope
.attack_level
256 || old
->u
.periodic
.envelope
.fade_length
!= effect
->u
.periodic
.envelope
.fade_length
257 || old
->u
.periodic
.envelope
.fade_level
!= effect
->u
.periodic
.envelope
.fade_level
)
262 dev_warn(&iforce
->dev
->dev
, "bad effect type in %s\n",
270 * Analyse the changes in an effect, and tell if we need to send a periodic
273 static int need_period_modifier(struct iforce
*iforce
, struct ff_effect
*old
,
274 struct ff_effect
*new)
276 if (new->type
!= FF_PERIODIC
) {
277 dev_warn(&iforce
->dev
->dev
, "bad effect type in %s\n",
281 return (old
->u
.periodic
.period
!= new->u
.periodic
.period
282 || old
->u
.periodic
.magnitude
!= new->u
.periodic
.magnitude
283 || old
->u
.periodic
.offset
!= new->u
.periodic
.offset
284 || old
->u
.periodic
.phase
!= new->u
.periodic
.phase
);
288 * Analyse the changes in an effect, and tell if we need to send an effect
291 static int need_core(struct ff_effect
*old
, struct ff_effect
*new)
293 if (old
->direction
!= new->direction
294 || old
->trigger
.button
!= new->trigger
.button
295 || old
->trigger
.interval
!= new->trigger
.interval
296 || old
->replay
.length
!= new->replay
.length
297 || old
->replay
.delay
!= new->replay
.delay
)
303 * Send the part common to all effects to the device
305 static int make_core(struct iforce
* iforce
, u16 id
, u16 mod_id1
, u16 mod_id2
,
306 u8 effect_type
, u8 axes
, u16 duration
, u16 delay
, u16 button
,
307 u16 interval
, u16 direction
)
309 unsigned char data
[14];
311 duration
= TIME_SCALE(duration
);
312 delay
= TIME_SCALE(delay
);
313 interval
= TIME_SCALE(interval
);
316 data
[1] = effect_type
;
317 data
[2] = LO(axes
) | find_button(iforce
, button
);
319 data
[3] = LO(duration
);
320 data
[4] = HI(duration
);
322 data
[5] = HI(direction
);
324 data
[6] = LO(interval
);
325 data
[7] = HI(interval
);
327 data
[8] = LO(mod_id1
);
328 data
[9] = HI(mod_id1
);
329 data
[10] = LO(mod_id2
);
330 data
[11] = HI(mod_id2
);
332 data
[12] = LO(delay
);
333 data
[13] = HI(delay
);
336 /* iforce_control_playback(iforce, id, 0);*/
338 iforce_send_packet(iforce
, FF_CMD_EFFECT
, data
);
340 /* If needed, restart effect */
341 if (test_bit(FF_CORE_SHOULD_PLAY
, iforce
->core_effects
[id
].flags
)) {
342 /* BUG: perhaps we should replay n times, instead of 1. But we do not know n */
343 iforce_control_playback(iforce
, id
, 1);
350 * Upload a periodic effect to the device
351 * See also iforce_upload_constant.
353 int iforce_upload_periodic(struct iforce
*iforce
, struct ff_effect
*effect
, struct ff_effect
*old
)
356 int core_id
= effect
->id
;
357 struct iforce_core_effect
* core_effect
= iforce
->core_effects
+ core_id
;
358 struct resource
* mod1_chunk
= &(iforce
->core_effects
[core_id
].mod1_chunk
);
359 struct resource
* mod2_chunk
= &(iforce
->core_effects
[core_id
].mod2_chunk
);
364 if (!old
|| need_period_modifier(iforce
, old
, effect
)) {
365 param1_err
= make_period_modifier(iforce
, mod1_chunk
,
367 effect
->u
.periodic
.magnitude
, effect
->u
.periodic
.offset
,
368 effect
->u
.periodic
.period
, effect
->u
.periodic
.phase
);
371 set_bit(FF_MOD1_IS_USED
, core_effect
->flags
);
374 if (!old
|| need_envelope_modifier(iforce
, old
, effect
)) {
375 param2_err
= make_envelope_modifier(iforce
, mod2_chunk
,
377 effect
->u
.periodic
.envelope
.attack_length
,
378 effect
->u
.periodic
.envelope
.attack_level
,
379 effect
->u
.periodic
.envelope
.fade_length
,
380 effect
->u
.periodic
.envelope
.fade_level
);
383 set_bit(FF_MOD2_IS_USED
, core_effect
->flags
);
386 switch (effect
->u
.periodic
.waveform
) {
387 case FF_SQUARE
: wave_code
= 0x20; break;
388 case FF_TRIANGLE
: wave_code
= 0x21; break;
389 case FF_SINE
: wave_code
= 0x22; break;
390 case FF_SAW_UP
: wave_code
= 0x23; break;
391 case FF_SAW_DOWN
: wave_code
= 0x24; break;
392 default: wave_code
= 0x20; break;
395 if (!old
|| need_core(old
, effect
)) {
396 core_err
= make_core(iforce
, effect
->id
,
401 effect
->replay
.length
,
402 effect
->replay
.delay
,
403 effect
->trigger
.button
,
404 effect
->trigger
.interval
,
408 /* If one of the parameter creation failed, we already returned an
410 * If the core creation failed, we return its error code.
411 * Else: if one parameter at least was created, we return 0
414 return core_err
< 0 ? core_err
: (param1_err
&& param2_err
);
418 * Upload a constant force effect
421 * 0 Ok, effect created or updated
422 * 1 effect did not change since last upload, and no packet was therefore sent
424 int iforce_upload_constant(struct iforce
*iforce
, struct ff_effect
*effect
, struct ff_effect
*old
)
426 int core_id
= effect
->id
;
427 struct iforce_core_effect
* core_effect
= iforce
->core_effects
+ core_id
;
428 struct resource
* mod1_chunk
= &(iforce
->core_effects
[core_id
].mod1_chunk
);
429 struct resource
* mod2_chunk
= &(iforce
->core_effects
[core_id
].mod2_chunk
);
434 if (!old
|| need_magnitude_modifier(iforce
, old
, effect
)) {
435 param1_err
= make_magnitude_modifier(iforce
, mod1_chunk
,
437 effect
->u
.constant
.level
);
440 set_bit(FF_MOD1_IS_USED
, core_effect
->flags
);
443 if (!old
|| need_envelope_modifier(iforce
, old
, effect
)) {
444 param2_err
= make_envelope_modifier(iforce
, mod2_chunk
,
446 effect
->u
.constant
.envelope
.attack_length
,
447 effect
->u
.constant
.envelope
.attack_level
,
448 effect
->u
.constant
.envelope
.fade_length
,
449 effect
->u
.constant
.envelope
.fade_level
);
452 set_bit(FF_MOD2_IS_USED
, core_effect
->flags
);
455 if (!old
|| need_core(old
, effect
)) {
456 core_err
= make_core(iforce
, effect
->id
,
461 effect
->replay
.length
,
462 effect
->replay
.delay
,
463 effect
->trigger
.button
,
464 effect
->trigger
.interval
,
468 /* If one of the parameter creation failed, we already returned an
470 * If the core creation failed, we return its error code.
471 * Else: if one parameter at least was created, we return 0
474 return core_err
< 0 ? core_err
: (param1_err
&& param2_err
);
478 * Upload an condition effect. Those are for example friction, inertia, springs...
480 int iforce_upload_condition(struct iforce
*iforce
, struct ff_effect
*effect
, struct ff_effect
*old
)
482 int core_id
= effect
->id
;
483 struct iforce_core_effect
* core_effect
= iforce
->core_effects
+ core_id
;
484 struct resource
* mod1_chunk
= &(core_effect
->mod1_chunk
);
485 struct resource
* mod2_chunk
= &(core_effect
->mod2_chunk
);
490 switch (effect
->type
) {
491 case FF_SPRING
: type
= 0x40; break;
492 case FF_DAMPER
: type
= 0x41; break;
496 if (!old
|| need_condition_modifier(iforce
, old
, effect
)) {
497 param_err
= make_condition_modifier(iforce
, mod1_chunk
,
499 effect
->u
.condition
[0].right_saturation
,
500 effect
->u
.condition
[0].left_saturation
,
501 effect
->u
.condition
[0].right_coeff
,
502 effect
->u
.condition
[0].left_coeff
,
503 effect
->u
.condition
[0].deadband
,
504 effect
->u
.condition
[0].center
);
507 set_bit(FF_MOD1_IS_USED
, core_effect
->flags
);
509 param_err
= make_condition_modifier(iforce
, mod2_chunk
,
511 effect
->u
.condition
[1].right_saturation
,
512 effect
->u
.condition
[1].left_saturation
,
513 effect
->u
.condition
[1].right_coeff
,
514 effect
->u
.condition
[1].left_coeff
,
515 effect
->u
.condition
[1].deadband
,
516 effect
->u
.condition
[1].center
);
519 set_bit(FF_MOD2_IS_USED
, core_effect
->flags
);
523 if (!old
|| need_core(old
, effect
)) {
524 core_err
= make_core(iforce
, effect
->id
,
525 mod1_chunk
->start
, mod2_chunk
->start
,
527 effect
->replay
.length
, effect
->replay
.delay
,
528 effect
->trigger
.button
, effect
->trigger
.interval
,
532 /* If the parameter creation failed, we already returned an
534 * If the core creation failed, we return its error code.
535 * Else: if a parameter was created, we return 0
538 return core_err
< 0 ? core_err
: param_err
;