drm/panfrost: Move gpu_{write, read}() macros to panfrost_regs.h
[linux/fpc-iii.git] / drivers / input / joystick / iforce / iforce-ff.c
blob3536d5f5ad18210a8f20a102912e16a2bf71f8ce
1 /*
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.
6 */
8 /*
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
24 #include "iforce.h"
27 * Set the magnitude of a constant force effect
28 * Return error code
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];
38 if (!no_alloc) {
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,
42 NULL, NULL)) {
43 mutex_unlock(&iforce->mem_mutex);
44 return -ENOSPC;
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);
56 return 0;
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);
71 if (!no_alloc) {
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,
75 NULL, NULL)) {
76 mutex_unlock(&iforce->mem_mutex);
77 return -ENOSPC;
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);
87 data[4] = HI(phase);
89 data[5] = LO(period);
90 data[6] = HI(period);
92 iforce_send_packet(iforce, FF_CMD_PERIOD, data);
94 return 0;
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);
111 if (!no_alloc) {
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,
115 NULL, NULL)) {
116 mutex_unlock(&iforce->mem_mutex);
117 return -ENOSPC;
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);
135 return 0;
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];
148 if (!no_alloc) {
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,
152 NULL, NULL)) {
153 mutex_unlock(&iforce->mem_mutex);
154 return -ENOSPC;
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;
170 data[6] = LO(db);
171 data[7] = HI(db);
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);
179 return 0;
182 static unsigned char find_button(struct iforce *iforce, signed short button)
184 int i;
186 for (i = 1; iforce->type->btn[i] >= 0; i++)
187 if (iforce->type->btn[i] == button)
188 return i + 1;
189 return 0;
193 * Analyse the changes in an effect, and tell if we need to send an condition
194 * parameter packet
196 static int need_condition_modifier(struct iforce *iforce,
197 struct ff_effect *old,
198 struct ff_effect *new)
200 int ret = 0;
201 int i;
203 if (new->type != FF_SPRING && new->type != FF_FRICTION) {
204 dev_warn(&iforce->dev->dev, "bad effect type in %s\n",
205 __func__);
206 return 0;
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;
217 return ret;
221 * Analyse the changes in an effect, and tell if we need to send a magnitude
222 * parameter packet
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",
230 __func__);
231 return 0;
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
239 * parameter packet
241 static int need_envelope_modifier(struct iforce *iforce, struct ff_effect *old,
242 struct ff_effect *effect)
244 switch (effect->type) {
245 case FF_CONSTANT:
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)
250 return 1;
251 break;
253 case FF_PERIODIC:
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)
258 return 1;
259 break;
261 default:
262 dev_warn(&iforce->dev->dev, "bad effect type in %s\n",
263 __func__);
266 return 0;
270 * Analyse the changes in an effect, and tell if we need to send a periodic
271 * parameter effect
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",
278 __func__);
279 return 0;
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
289 * packet
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)
298 return 1;
300 return 0;
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);
315 data[0] = LO(id);
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);
335 /* Stop effect */
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);
346 return 0;
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)
355 u8 wave_code;
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);
360 int param1_err = 1;
361 int param2_err = 1;
362 int core_err = 0;
364 if (!old || need_period_modifier(iforce, old, effect)) {
365 param1_err = make_period_modifier(iforce, mod1_chunk,
366 old != NULL,
367 effect->u.periodic.magnitude, effect->u.periodic.offset,
368 effect->u.periodic.period, effect->u.periodic.phase);
369 if (param1_err)
370 return param1_err;
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,
376 old !=NULL,
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);
381 if (param2_err)
382 return param2_err;
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,
397 mod1_chunk->start,
398 mod2_chunk->start,
399 wave_code,
400 0x20,
401 effect->replay.length,
402 effect->replay.delay,
403 effect->trigger.button,
404 effect->trigger.interval,
405 effect->direction);
408 /* If one of the parameter creation failed, we already returned an
409 * error code.
410 * If the core creation failed, we return its error code.
411 * Else: if one parameter at least was created, we return 0
412 * else we return 1;
414 return core_err < 0 ? core_err : (param1_err && param2_err);
418 * Upload a constant force effect
419 * Return value:
420 * <0 Error code
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);
430 int param1_err = 1;
431 int param2_err = 1;
432 int core_err = 0;
434 if (!old || need_magnitude_modifier(iforce, old, effect)) {
435 param1_err = make_magnitude_modifier(iforce, mod1_chunk,
436 old != NULL,
437 effect->u.constant.level);
438 if (param1_err)
439 return param1_err;
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,
445 old != NULL,
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);
450 if (param2_err)
451 return param2_err;
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,
457 mod1_chunk->start,
458 mod2_chunk->start,
459 0x00,
460 0x20,
461 effect->replay.length,
462 effect->replay.delay,
463 effect->trigger.button,
464 effect->trigger.interval,
465 effect->direction);
468 /* If one of the parameter creation failed, we already returned an
469 * error code.
470 * If the core creation failed, we return its error code.
471 * Else: if one parameter at least was created, we return 0
472 * else we return 1;
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);
486 u8 type;
487 int param_err = 1;
488 int core_err = 0;
490 switch (effect->type) {
491 case FF_SPRING: type = 0x40; break;
492 case FF_DAMPER: type = 0x41; break;
493 default: return -1;
496 if (!old || need_condition_modifier(iforce, old, effect)) {
497 param_err = make_condition_modifier(iforce, mod1_chunk,
498 old != NULL,
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);
505 if (param_err)
506 return param_err;
507 set_bit(FF_MOD1_IS_USED, core_effect->flags);
509 param_err = make_condition_modifier(iforce, mod2_chunk,
510 old != NULL,
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);
517 if (param_err)
518 return param_err;
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,
526 type, 0xc0,
527 effect->replay.length, effect->replay.delay,
528 effect->trigger.button, effect->trigger.interval,
529 effect->direction);
532 /* If the parameter creation failed, we already returned an
533 * error code.
534 * If the core creation failed, we return its error code.
535 * Else: if a parameter was created, we return 0
536 * else we return 1;
538 return core_err < 0 ? core_err : param_err;