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.
15 * Set the magnitude of a constant force effect
18 * Note: caller must ensure exclusive access to device
21 static int make_magnitude_modifier(struct iforce* iforce,
22 struct resource* mod_chunk, int no_alloc, __s16 level)
24 unsigned char data[3];
27 mutex_lock(&iforce->mem_mutex);
28 if (allocate_resource(&(iforce->device_memory), mod_chunk, 2,
29 iforce->device_memory.start, iforce->device_memory.end, 2L,
31 mutex_unlock(&iforce->mem_mutex);
34 mutex_unlock(&iforce->mem_mutex);
37 data[0] = LO(mod_chunk->start);
38 data[1] = HI(mod_chunk->start);
39 data[2] = HIFIX80(level);
41 iforce_send_packet(iforce, FF_CMD_MAGNITUDE, data);
43 iforce_dump_packet(iforce, "magnitude", FF_CMD_MAGNITUDE, data);
48 * Upload the component of an effect dealing with the period, phase and magnitude
51 static int make_period_modifier(struct iforce* iforce,
52 struct resource* mod_chunk, int no_alloc,
53 __s16 magnitude, __s16 offset, u16 period, u16 phase)
55 unsigned char data[7];
57 period = TIME_SCALE(period);
60 mutex_lock(&iforce->mem_mutex);
61 if (allocate_resource(&(iforce->device_memory), mod_chunk, 0x0c,
62 iforce->device_memory.start, iforce->device_memory.end, 2L,
64 mutex_unlock(&iforce->mem_mutex);
67 mutex_unlock(&iforce->mem_mutex);
70 data[0] = LO(mod_chunk->start);
71 data[1] = HI(mod_chunk->start);
73 data[2] = HIFIX80(magnitude);
74 data[3] = HIFIX80(offset);
80 iforce_send_packet(iforce, FF_CMD_PERIOD, data);
86 * Uploads the part of an effect setting the envelope of the force
89 static int make_envelope_modifier(struct iforce* iforce,
90 struct resource* mod_chunk, int no_alloc,
91 u16 attack_duration, __s16 initial_level,
92 u16 fade_duration, __s16 final_level)
94 unsigned char data[8];
96 attack_duration = TIME_SCALE(attack_duration);
97 fade_duration = TIME_SCALE(fade_duration);
100 mutex_lock(&iforce->mem_mutex);
101 if (allocate_resource(&(iforce->device_memory), mod_chunk, 0x0e,
102 iforce->device_memory.start, iforce->device_memory.end, 2L,
104 mutex_unlock(&iforce->mem_mutex);
107 mutex_unlock(&iforce->mem_mutex);
110 data[0] = LO(mod_chunk->start);
111 data[1] = HI(mod_chunk->start);
113 data[2] = LO(attack_duration);
114 data[3] = HI(attack_duration);
115 data[4] = HI(initial_level);
117 data[5] = LO(fade_duration);
118 data[6] = HI(fade_duration);
119 data[7] = HI(final_level);
121 iforce_send_packet(iforce, FF_CMD_ENVELOPE, data);
127 * Component of spring, friction, inertia... effects
130 static int make_condition_modifier(struct iforce* iforce,
131 struct resource* mod_chunk, int no_alloc,
132 __u16 rsat, __u16 lsat, __s16 rk, __s16 lk, u16 db, __s16 center)
134 unsigned char data[10];
137 mutex_lock(&iforce->mem_mutex);
138 if (allocate_resource(&(iforce->device_memory), mod_chunk, 8,
139 iforce->device_memory.start, iforce->device_memory.end, 2L,
141 mutex_unlock(&iforce->mem_mutex);
144 mutex_unlock(&iforce->mem_mutex);
147 data[0] = LO(mod_chunk->start);
148 data[1] = HI(mod_chunk->start);
150 data[2] = (100 * rk) >> 15; /* Dangerous: the sign is extended by gcc on plateforms providing an arith shift */
151 data[3] = (100 * lk) >> 15; /* This code is incorrect on cpus lacking arith shift */
153 center = (500 * center) >> 15;
154 data[4] = LO(center);
155 data[5] = HI(center);
157 db = (1000 * db) >> 16;
161 data[8] = (100 * rsat) >> 16;
162 data[9] = (100 * lsat) >> 16;
164 iforce_send_packet(iforce, FF_CMD_CONDITION, data);
165 iforce_dump_packet(iforce, "condition", FF_CMD_CONDITION, data);
170 static unsigned char find_button(struct iforce *iforce, signed short button)
174 for (i = 1; iforce->type->btn[i] >= 0; i++)
175 if (iforce->type->btn[i] == button)
181 * Analyse the changes in an effect, and tell if we need to send an condition
184 static int need_condition_modifier(struct iforce *iforce,
185 struct ff_effect *old,
186 struct ff_effect *new)
191 if (new->type != FF_SPRING && new->type != FF_FRICTION) {
192 dev_warn(&iforce->dev->dev, "bad effect type in %s\n",
197 for (i = 0; i < 2; i++) {
198 ret |= old->u.condition[i].right_saturation != new->u.condition[i].right_saturation
199 || old->u.condition[i].left_saturation != new->u.condition[i].left_saturation
200 || old->u.condition[i].right_coeff != new->u.condition[i].right_coeff
201 || old->u.condition[i].left_coeff != new->u.condition[i].left_coeff
202 || old->u.condition[i].deadband != new->u.condition[i].deadband
203 || old->u.condition[i].center != new->u.condition[i].center;
209 * Analyse the changes in an effect, and tell if we need to send a magnitude
212 static int need_magnitude_modifier(struct iforce *iforce,
213 struct ff_effect *old,
214 struct ff_effect *effect)
216 if (effect->type != FF_CONSTANT) {
217 dev_warn(&iforce->dev->dev, "bad effect type in %s\n",
222 return old->u.constant.level != effect->u.constant.level;
226 * Analyse the changes in an effect, and tell if we need to send an envelope
229 static int need_envelope_modifier(struct iforce *iforce, struct ff_effect *old,
230 struct ff_effect *effect)
232 switch (effect->type) {
234 if (old->u.constant.envelope.attack_length != effect->u.constant.envelope.attack_length
235 || old->u.constant.envelope.attack_level != effect->u.constant.envelope.attack_level
236 || old->u.constant.envelope.fade_length != effect->u.constant.envelope.fade_length
237 || old->u.constant.envelope.fade_level != effect->u.constant.envelope.fade_level)
242 if (old->u.periodic.envelope.attack_length != effect->u.periodic.envelope.attack_length
243 || old->u.periodic.envelope.attack_level != effect->u.periodic.envelope.attack_level
244 || old->u.periodic.envelope.fade_length != effect->u.periodic.envelope.fade_length
245 || old->u.periodic.envelope.fade_level != effect->u.periodic.envelope.fade_level)
250 dev_warn(&iforce->dev->dev, "bad effect type in %s\n",
258 * Analyse the changes in an effect, and tell if we need to send a periodic
261 static int need_period_modifier(struct iforce *iforce, struct ff_effect *old,
262 struct ff_effect *new)
264 if (new->type != FF_PERIODIC) {
265 dev_warn(&iforce->dev->dev, "bad effect type in %s\n",
269 return (old->u.periodic.period != new->u.periodic.period
270 || old->u.periodic.magnitude != new->u.periodic.magnitude
271 || old->u.periodic.offset != new->u.periodic.offset
272 || old->u.periodic.phase != new->u.periodic.phase);
276 * Analyse the changes in an effect, and tell if we need to send an effect
279 static int need_core(struct ff_effect *old, struct ff_effect *new)
281 if (old->direction != new->direction
282 || old->trigger.button != new->trigger.button
283 || old->trigger.interval != new->trigger.interval
284 || old->replay.length != new->replay.length
285 || old->replay.delay != new->replay.delay)
291 * Send the part common to all effects to the device
293 static int make_core(struct iforce* iforce, u16 id, u16 mod_id1, u16 mod_id2,
294 u8 effect_type, u8 axes, u16 duration, u16 delay, u16 button,
295 u16 interval, u16 direction)
297 unsigned char data[14];
299 duration = TIME_SCALE(duration);
300 delay = TIME_SCALE(delay);
301 interval = TIME_SCALE(interval);
304 data[1] = effect_type;
305 data[2] = LO(axes) | find_button(iforce, button);
307 data[3] = LO(duration);
308 data[4] = HI(duration);
310 data[5] = HI(direction);
312 data[6] = LO(interval);
313 data[7] = HI(interval);
315 data[8] = LO(mod_id1);
316 data[9] = HI(mod_id1);
317 data[10] = LO(mod_id2);
318 data[11] = HI(mod_id2);
320 data[12] = LO(delay);
321 data[13] = HI(delay);
324 /* iforce_control_playback(iforce, id, 0);*/
326 iforce_send_packet(iforce, FF_CMD_EFFECT, data);
328 /* If needed, restart effect */
329 if (test_bit(FF_CORE_SHOULD_PLAY, iforce->core_effects[id].flags)) {
330 /* BUG: perhaps we should replay n times, instead of 1. But we do not know n */
331 iforce_control_playback(iforce, id, 1);
338 * Upload a periodic effect to the device
339 * See also iforce_upload_constant.
341 int iforce_upload_periodic(struct iforce *iforce, struct ff_effect *effect, struct ff_effect *old)
344 int core_id = effect->id;
345 struct iforce_core_effect* core_effect = iforce->core_effects + core_id;
346 struct resource* mod1_chunk = &(iforce->core_effects[core_id].mod1_chunk);
347 struct resource* mod2_chunk = &(iforce->core_effects[core_id].mod2_chunk);
352 if (!old || need_period_modifier(iforce, old, effect)) {
353 param1_err = make_period_modifier(iforce, mod1_chunk,
355 effect->u.periodic.magnitude, effect->u.periodic.offset,
356 effect->u.periodic.period, effect->u.periodic.phase);
359 set_bit(FF_MOD1_IS_USED, core_effect->flags);
362 if (!old || need_envelope_modifier(iforce, old, effect)) {
363 param2_err = make_envelope_modifier(iforce, mod2_chunk,
365 effect->u.periodic.envelope.attack_length,
366 effect->u.periodic.envelope.attack_level,
367 effect->u.periodic.envelope.fade_length,
368 effect->u.periodic.envelope.fade_level);
371 set_bit(FF_MOD2_IS_USED, core_effect->flags);
374 switch (effect->u.periodic.waveform) {
375 case FF_SQUARE: wave_code = 0x20; break;
376 case FF_TRIANGLE: wave_code = 0x21; break;
377 case FF_SINE: wave_code = 0x22; break;
378 case FF_SAW_UP: wave_code = 0x23; break;
379 case FF_SAW_DOWN: wave_code = 0x24; break;
380 default: wave_code = 0x20; break;
383 if (!old || need_core(old, effect)) {
384 core_err = make_core(iforce, effect->id,
389 effect->replay.length,
390 effect->replay.delay,
391 effect->trigger.button,
392 effect->trigger.interval,
396 /* If one of the parameter creation failed, we already returned an
398 * If the core creation failed, we return its error code.
399 * Else: if one parameter at least was created, we return 0
402 return core_err < 0 ? core_err : (param1_err && param2_err);
406 * Upload a constant force effect
409 * 0 Ok, effect created or updated
410 * 1 effect did not change since last upload, and no packet was therefore sent
412 int iforce_upload_constant(struct iforce *iforce, struct ff_effect *effect, struct ff_effect *old)
414 int core_id = effect->id;
415 struct iforce_core_effect* core_effect = iforce->core_effects + core_id;
416 struct resource* mod1_chunk = &(iforce->core_effects[core_id].mod1_chunk);
417 struct resource* mod2_chunk = &(iforce->core_effects[core_id].mod2_chunk);
422 if (!old || need_magnitude_modifier(iforce, old, effect)) {
423 param1_err = make_magnitude_modifier(iforce, mod1_chunk,
425 effect->u.constant.level);
428 set_bit(FF_MOD1_IS_USED, core_effect->flags);
431 if (!old || need_envelope_modifier(iforce, old, effect)) {
432 param2_err = make_envelope_modifier(iforce, mod2_chunk,
434 effect->u.constant.envelope.attack_length,
435 effect->u.constant.envelope.attack_level,
436 effect->u.constant.envelope.fade_length,
437 effect->u.constant.envelope.fade_level);
440 set_bit(FF_MOD2_IS_USED, core_effect->flags);
443 if (!old || need_core(old, effect)) {
444 core_err = make_core(iforce, effect->id,
449 effect->replay.length,
450 effect->replay.delay,
451 effect->trigger.button,
452 effect->trigger.interval,
456 /* If one of the parameter creation failed, we already returned an
458 * If the core creation failed, we return its error code.
459 * Else: if one parameter at least was created, we return 0
462 return core_err < 0 ? core_err : (param1_err && param2_err);
466 * Upload an condition effect. Those are for example friction, inertia, springs...
468 int iforce_upload_condition(struct iforce *iforce, struct ff_effect *effect, struct ff_effect *old)
470 int core_id = effect->id;
471 struct iforce_core_effect* core_effect = iforce->core_effects + core_id;
472 struct resource* mod1_chunk = &(core_effect->mod1_chunk);
473 struct resource* mod2_chunk = &(core_effect->mod2_chunk);
478 switch (effect->type) {
479 case FF_SPRING: type = 0x40; break;
480 case FF_DAMPER: type = 0x41; break;
484 if (!old || need_condition_modifier(iforce, old, effect)) {
485 param_err = make_condition_modifier(iforce, mod1_chunk,
487 effect->u.condition[0].right_saturation,
488 effect->u.condition[0].left_saturation,
489 effect->u.condition[0].right_coeff,
490 effect->u.condition[0].left_coeff,
491 effect->u.condition[0].deadband,
492 effect->u.condition[0].center);
495 set_bit(FF_MOD1_IS_USED, core_effect->flags);
497 param_err = make_condition_modifier(iforce, mod2_chunk,
499 effect->u.condition[1].right_saturation,
500 effect->u.condition[1].left_saturation,
501 effect->u.condition[1].right_coeff,
502 effect->u.condition[1].left_coeff,
503 effect->u.condition[1].deadband,
504 effect->u.condition[1].center);
507 set_bit(FF_MOD2_IS_USED, core_effect->flags);
511 if (!old || need_core(old, effect)) {
512 core_err = make_core(iforce, effect->id,
513 mod1_chunk->start, mod2_chunk->start,
515 effect->replay.length, effect->replay.delay,
516 effect->trigger.button, effect->trigger.interval,
520 /* If the parameter creation failed, we already returned an
522 * If the core creation failed, we return its error code.
523 * Else: if a parameter was created, we return 0
526 return core_err < 0 ? core_err : param_err;