1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) 2000-2002 Vojtech Pavlik <vojtech@ucw.cz>
4 * Copyright (c) 2001-2002, 2007 Johann Deneux <johann.deneux@gmail.com>
6 * USB/RS232 I-Force joysticks and wheels.
12 * Set the magnitude of a constant force effect
15 * Note: caller must ensure exclusive access to device
18 static int make_magnitude_modifier(struct iforce
* iforce
,
19 struct resource
* mod_chunk
, int no_alloc
, __s16 level
)
21 unsigned char data
[3];
24 guard(mutex
)(&iforce
->mem_mutex
);
26 if (allocate_resource(&iforce
->device_memory
, mod_chunk
, 2,
27 iforce
->device_memory
.start
,
28 iforce
->device_memory
.end
,
33 data
[0] = LO(mod_chunk
->start
);
34 data
[1] = HI(mod_chunk
->start
);
35 data
[2] = HIFIX80(level
);
37 iforce_send_packet(iforce
, FF_CMD_MAGNITUDE
, data
);
39 iforce_dump_packet(iforce
, "magnitude", FF_CMD_MAGNITUDE
, data
);
44 * Upload the component of an effect dealing with the period, phase and magnitude
47 static int make_period_modifier(struct iforce
* iforce
,
48 struct resource
* mod_chunk
, int no_alloc
,
49 __s16 magnitude
, __s16 offset
, u16 period
, u16 phase
)
51 unsigned char data
[7];
53 period
= TIME_SCALE(period
);
56 guard(mutex
)(&iforce
->mem_mutex
);
58 if (allocate_resource(&iforce
->device_memory
, mod_chunk
, 0x0c,
59 iforce
->device_memory
.start
,
60 iforce
->device_memory
.end
,
65 data
[0] = LO(mod_chunk
->start
);
66 data
[1] = HI(mod_chunk
->start
);
68 data
[2] = HIFIX80(magnitude
);
69 data
[3] = HIFIX80(offset
);
75 iforce_send_packet(iforce
, FF_CMD_PERIOD
, data
);
81 * Uploads the part of an effect setting the envelope of the force
84 static int make_envelope_modifier(struct iforce
* iforce
,
85 struct resource
* mod_chunk
, int no_alloc
,
86 u16 attack_duration
, __s16 initial_level
,
87 u16 fade_duration
, __s16 final_level
)
89 unsigned char data
[8];
91 attack_duration
= TIME_SCALE(attack_duration
);
92 fade_duration
= TIME_SCALE(fade_duration
);
95 guard(mutex
)(&iforce
->mem_mutex
);
97 if (allocate_resource(&(iforce
->device_memory
), mod_chunk
, 0x0e,
98 iforce
->device_memory
.start
,
99 iforce
->device_memory
.end
,
104 data
[0] = LO(mod_chunk
->start
);
105 data
[1] = HI(mod_chunk
->start
);
107 data
[2] = LO(attack_duration
);
108 data
[3] = HI(attack_duration
);
109 data
[4] = HI(initial_level
);
111 data
[5] = LO(fade_duration
);
112 data
[6] = HI(fade_duration
);
113 data
[7] = HI(final_level
);
115 iforce_send_packet(iforce
, FF_CMD_ENVELOPE
, data
);
121 * Component of spring, friction, inertia... effects
124 static int make_condition_modifier(struct iforce
* iforce
,
125 struct resource
* mod_chunk
, int no_alloc
,
126 __u16 rsat
, __u16 lsat
, __s16 rk
, __s16 lk
, u16 db
, __s16 center
)
128 unsigned char data
[10];
131 guard(mutex
)(&iforce
->mem_mutex
);
133 if (allocate_resource(&(iforce
->device_memory
), mod_chunk
, 8,
134 iforce
->device_memory
.start
,
135 iforce
->device_memory
.end
,
140 data
[0] = LO(mod_chunk
->start
);
141 data
[1] = HI(mod_chunk
->start
);
143 data
[2] = (100 * rk
) >> 15; /* Dangerous: the sign is extended by gcc on plateforms providing an arith shift */
144 data
[3] = (100 * lk
) >> 15; /* This code is incorrect on cpus lacking arith shift */
146 center
= (500 * center
) >> 15;
147 data
[4] = LO(center
);
148 data
[5] = HI(center
);
150 db
= (1000 * db
) >> 16;
154 data
[8] = (100 * rsat
) >> 16;
155 data
[9] = (100 * lsat
) >> 16;
157 iforce_send_packet(iforce
, FF_CMD_CONDITION
, data
);
158 iforce_dump_packet(iforce
, "condition", FF_CMD_CONDITION
, data
);
163 static unsigned char find_button(struct iforce
*iforce
, signed short button
)
167 for (i
= 1; iforce
->type
->btn
[i
] >= 0; i
++)
168 if (iforce
->type
->btn
[i
] == button
)
174 * Analyse the changes in an effect, and tell if we need to send an condition
177 static int need_condition_modifier(struct iforce
*iforce
,
178 struct ff_effect
*old
,
179 struct ff_effect
*new)
184 if (new->type
!= FF_SPRING
&& new->type
!= FF_FRICTION
) {
185 dev_warn(&iforce
->dev
->dev
, "bad effect type in %s\n",
190 for (i
= 0; i
< 2; i
++) {
191 ret
|= old
->u
.condition
[i
].right_saturation
!= new->u
.condition
[i
].right_saturation
192 || old
->u
.condition
[i
].left_saturation
!= new->u
.condition
[i
].left_saturation
193 || old
->u
.condition
[i
].right_coeff
!= new->u
.condition
[i
].right_coeff
194 || old
->u
.condition
[i
].left_coeff
!= new->u
.condition
[i
].left_coeff
195 || old
->u
.condition
[i
].deadband
!= new->u
.condition
[i
].deadband
196 || old
->u
.condition
[i
].center
!= new->u
.condition
[i
].center
;
202 * Analyse the changes in an effect, and tell if we need to send a magnitude
205 static int need_magnitude_modifier(struct iforce
*iforce
,
206 struct ff_effect
*old
,
207 struct ff_effect
*effect
)
209 if (effect
->type
!= FF_CONSTANT
) {
210 dev_warn(&iforce
->dev
->dev
, "bad effect type in %s\n",
215 return old
->u
.constant
.level
!= effect
->u
.constant
.level
;
219 * Analyse the changes in an effect, and tell if we need to send an envelope
222 static int need_envelope_modifier(struct iforce
*iforce
, struct ff_effect
*old
,
223 struct ff_effect
*effect
)
225 switch (effect
->type
) {
227 if (old
->u
.constant
.envelope
.attack_length
!= effect
->u
.constant
.envelope
.attack_length
228 || old
->u
.constant
.envelope
.attack_level
!= effect
->u
.constant
.envelope
.attack_level
229 || old
->u
.constant
.envelope
.fade_length
!= effect
->u
.constant
.envelope
.fade_length
230 || old
->u
.constant
.envelope
.fade_level
!= effect
->u
.constant
.envelope
.fade_level
)
235 if (old
->u
.periodic
.envelope
.attack_length
!= effect
->u
.periodic
.envelope
.attack_length
236 || old
->u
.periodic
.envelope
.attack_level
!= effect
->u
.periodic
.envelope
.attack_level
237 || old
->u
.periodic
.envelope
.fade_length
!= effect
->u
.periodic
.envelope
.fade_length
238 || old
->u
.periodic
.envelope
.fade_level
!= effect
->u
.periodic
.envelope
.fade_level
)
243 dev_warn(&iforce
->dev
->dev
, "bad effect type in %s\n",
251 * Analyse the changes in an effect, and tell if we need to send a periodic
254 static int need_period_modifier(struct iforce
*iforce
, struct ff_effect
*old
,
255 struct ff_effect
*new)
257 if (new->type
!= FF_PERIODIC
) {
258 dev_warn(&iforce
->dev
->dev
, "bad effect type in %s\n",
262 return (old
->u
.periodic
.period
!= new->u
.periodic
.period
263 || old
->u
.periodic
.magnitude
!= new->u
.periodic
.magnitude
264 || old
->u
.periodic
.offset
!= new->u
.periodic
.offset
265 || old
->u
.periodic
.phase
!= new->u
.periodic
.phase
);
269 * Analyse the changes in an effect, and tell if we need to send an effect
272 static int need_core(struct ff_effect
*old
, struct ff_effect
*new)
274 if (old
->direction
!= new->direction
275 || old
->trigger
.button
!= new->trigger
.button
276 || old
->trigger
.interval
!= new->trigger
.interval
277 || old
->replay
.length
!= new->replay
.length
278 || old
->replay
.delay
!= new->replay
.delay
)
284 * Send the part common to all effects to the device
286 static int make_core(struct iforce
* iforce
, u16 id
, u16 mod_id1
, u16 mod_id2
,
287 u8 effect_type
, u8 axes
, u16 duration
, u16 delay
, u16 button
,
288 u16 interval
, u16 direction
)
290 unsigned char data
[14];
292 duration
= TIME_SCALE(duration
);
293 delay
= TIME_SCALE(delay
);
294 interval
= TIME_SCALE(interval
);
297 data
[1] = effect_type
;
298 data
[2] = LO(axes
) | find_button(iforce
, button
);
300 data
[3] = LO(duration
);
301 data
[4] = HI(duration
);
303 data
[5] = HI(direction
);
305 data
[6] = LO(interval
);
306 data
[7] = HI(interval
);
308 data
[8] = LO(mod_id1
);
309 data
[9] = HI(mod_id1
);
310 data
[10] = LO(mod_id2
);
311 data
[11] = HI(mod_id2
);
313 data
[12] = LO(delay
);
314 data
[13] = HI(delay
);
317 /* iforce_control_playback(iforce, id, 0);*/
319 iforce_send_packet(iforce
, FF_CMD_EFFECT
, data
);
321 /* If needed, restart effect */
322 if (test_bit(FF_CORE_SHOULD_PLAY
, iforce
->core_effects
[id
].flags
)) {
323 /* BUG: perhaps we should replay n times, instead of 1. But we do not know n */
324 iforce_control_playback(iforce
, id
, 1);
331 * Upload a periodic effect to the device
332 * See also iforce_upload_constant.
334 int iforce_upload_periodic(struct iforce
*iforce
, struct ff_effect
*effect
, struct ff_effect
*old
)
337 int core_id
= effect
->id
;
338 struct iforce_core_effect
* core_effect
= iforce
->core_effects
+ core_id
;
339 struct resource
* mod1_chunk
= &(iforce
->core_effects
[core_id
].mod1_chunk
);
340 struct resource
* mod2_chunk
= &(iforce
->core_effects
[core_id
].mod2_chunk
);
345 if (!old
|| need_period_modifier(iforce
, old
, effect
)) {
346 param1_err
= make_period_modifier(iforce
, mod1_chunk
,
348 effect
->u
.periodic
.magnitude
, effect
->u
.periodic
.offset
,
349 effect
->u
.periodic
.period
, effect
->u
.periodic
.phase
);
352 set_bit(FF_MOD1_IS_USED
, core_effect
->flags
);
355 if (!old
|| need_envelope_modifier(iforce
, old
, effect
)) {
356 param2_err
= make_envelope_modifier(iforce
, mod2_chunk
,
358 effect
->u
.periodic
.envelope
.attack_length
,
359 effect
->u
.periodic
.envelope
.attack_level
,
360 effect
->u
.periodic
.envelope
.fade_length
,
361 effect
->u
.periodic
.envelope
.fade_level
);
364 set_bit(FF_MOD2_IS_USED
, core_effect
->flags
);
367 switch (effect
->u
.periodic
.waveform
) {
368 case FF_SQUARE
: wave_code
= 0x20; break;
369 case FF_TRIANGLE
: wave_code
= 0x21; break;
370 case FF_SINE
: wave_code
= 0x22; break;
371 case FF_SAW_UP
: wave_code
= 0x23; break;
372 case FF_SAW_DOWN
: wave_code
= 0x24; break;
373 default: wave_code
= 0x20; break;
376 if (!old
|| need_core(old
, effect
)) {
377 core_err
= make_core(iforce
, effect
->id
,
382 effect
->replay
.length
,
383 effect
->replay
.delay
,
384 effect
->trigger
.button
,
385 effect
->trigger
.interval
,
389 /* If one of the parameter creation failed, we already returned an
391 * If the core creation failed, we return its error code.
392 * Else: if one parameter at least was created, we return 0
395 return core_err
< 0 ? core_err
: (param1_err
&& param2_err
);
399 * Upload a constant force effect
402 * 0 Ok, effect created or updated
403 * 1 effect did not change since last upload, and no packet was therefore sent
405 int iforce_upload_constant(struct iforce
*iforce
, struct ff_effect
*effect
, struct ff_effect
*old
)
407 int core_id
= effect
->id
;
408 struct iforce_core_effect
* core_effect
= iforce
->core_effects
+ core_id
;
409 struct resource
* mod1_chunk
= &(iforce
->core_effects
[core_id
].mod1_chunk
);
410 struct resource
* mod2_chunk
= &(iforce
->core_effects
[core_id
].mod2_chunk
);
415 if (!old
|| need_magnitude_modifier(iforce
, old
, effect
)) {
416 param1_err
= make_magnitude_modifier(iforce
, mod1_chunk
,
418 effect
->u
.constant
.level
);
421 set_bit(FF_MOD1_IS_USED
, core_effect
->flags
);
424 if (!old
|| need_envelope_modifier(iforce
, old
, effect
)) {
425 param2_err
= make_envelope_modifier(iforce
, mod2_chunk
,
427 effect
->u
.constant
.envelope
.attack_length
,
428 effect
->u
.constant
.envelope
.attack_level
,
429 effect
->u
.constant
.envelope
.fade_length
,
430 effect
->u
.constant
.envelope
.fade_level
);
433 set_bit(FF_MOD2_IS_USED
, core_effect
->flags
);
436 if (!old
|| need_core(old
, effect
)) {
437 core_err
= make_core(iforce
, effect
->id
,
442 effect
->replay
.length
,
443 effect
->replay
.delay
,
444 effect
->trigger
.button
,
445 effect
->trigger
.interval
,
449 /* If one of the parameter creation failed, we already returned an
451 * If the core creation failed, we return its error code.
452 * Else: if one parameter at least was created, we return 0
455 return core_err
< 0 ? core_err
: (param1_err
&& param2_err
);
459 * Upload an condition effect. Those are for example friction, inertia, springs...
461 int iforce_upload_condition(struct iforce
*iforce
, struct ff_effect
*effect
, struct ff_effect
*old
)
463 int core_id
= effect
->id
;
464 struct iforce_core_effect
* core_effect
= iforce
->core_effects
+ core_id
;
465 struct resource
* mod1_chunk
= &(core_effect
->mod1_chunk
);
466 struct resource
* mod2_chunk
= &(core_effect
->mod2_chunk
);
471 switch (effect
->type
) {
472 case FF_SPRING
: type
= 0x40; break;
473 case FF_DAMPER
: type
= 0x41; break;
477 if (!old
|| need_condition_modifier(iforce
, old
, effect
)) {
478 param_err
= make_condition_modifier(iforce
, mod1_chunk
,
480 effect
->u
.condition
[0].right_saturation
,
481 effect
->u
.condition
[0].left_saturation
,
482 effect
->u
.condition
[0].right_coeff
,
483 effect
->u
.condition
[0].left_coeff
,
484 effect
->u
.condition
[0].deadband
,
485 effect
->u
.condition
[0].center
);
488 set_bit(FF_MOD1_IS_USED
, core_effect
->flags
);
490 param_err
= make_condition_modifier(iforce
, mod2_chunk
,
492 effect
->u
.condition
[1].right_saturation
,
493 effect
->u
.condition
[1].left_saturation
,
494 effect
->u
.condition
[1].right_coeff
,
495 effect
->u
.condition
[1].left_coeff
,
496 effect
->u
.condition
[1].deadband
,
497 effect
->u
.condition
[1].center
);
500 set_bit(FF_MOD2_IS_USED
, core_effect
->flags
);
504 if (!old
|| need_core(old
, effect
)) {
505 core_err
= make_core(iforce
, effect
->id
,
506 mod1_chunk
->start
, mod2_chunk
->start
,
508 effect
->replay
.length
, effect
->replay
.delay
,
509 effect
->trigger
.button
, effect
->trigger
.interval
,
513 /* If the parameter creation failed, we already returned an
515 * If the core creation failed, we return its error code.
516 * Else: if a parameter was created, we return 0
519 return core_err
< 0 ? core_err
: param_err
;