Merge branch 'x86-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[pandora-kernel.git] / drivers / input / joystick / iforce / iforce-ff.c
1 /*
2  *  Copyright (c) 2000-2002 Vojtech Pavlik <vojtech@ucw.cz>
3  *  Copyright (c) 2001-2002, 2007 Johann Deneux <johann.deneux@gmail.com>
4  *
5  *  USB/RS232 I-Force joysticks and wheels.
6  */
7
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.
13  *
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.
18  *
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
22  *
23  * Should you need to contact me, the author, you can do so either by
24  * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
25  * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
26  */
27
28 #include "iforce.h"
29
30 /*
31  * Set the magnitude of a constant force effect
32  * Return error code
33  *
34  * Note: caller must ensure exclusive access to device
35  */
36
37 static int make_magnitude_modifier(struct iforce* iforce,
38         struct resource* mod_chunk, int no_alloc, __s16 level)
39 {
40         unsigned char data[3];
41
42         if (!no_alloc) {
43                 mutex_lock(&iforce->mem_mutex);
44                 if (allocate_resource(&(iforce->device_memory), mod_chunk, 2,
45                         iforce->device_memory.start, iforce->device_memory.end, 2L,
46                         NULL, NULL)) {
47                         mutex_unlock(&iforce->mem_mutex);
48                         return -ENOSPC;
49                 }
50                 mutex_unlock(&iforce->mem_mutex);
51         }
52
53         data[0] = LO(mod_chunk->start);
54         data[1] = HI(mod_chunk->start);
55         data[2] = HIFIX80(level);
56
57         iforce_send_packet(iforce, FF_CMD_MAGNITUDE, data);
58
59         iforce_dump_packet("magnitude: ", FF_CMD_MAGNITUDE, data);
60         return 0;
61 }
62
63 /*
64  * Upload the component of an effect dealing with the period, phase and magnitude
65  */
66
67 static int make_period_modifier(struct iforce* iforce,
68         struct resource* mod_chunk, int no_alloc,
69         __s16 magnitude, __s16 offset, u16 period, u16 phase)
70 {
71         unsigned char data[7];
72
73         period = TIME_SCALE(period);
74
75         if (!no_alloc) {
76                 mutex_lock(&iforce->mem_mutex);
77                 if (allocate_resource(&(iforce->device_memory), mod_chunk, 0x0c,
78                         iforce->device_memory.start, iforce->device_memory.end, 2L,
79                         NULL, NULL)) {
80                         mutex_unlock(&iforce->mem_mutex);
81                         return -ENOSPC;
82                 }
83                 mutex_unlock(&iforce->mem_mutex);
84         }
85
86         data[0] = LO(mod_chunk->start);
87         data[1] = HI(mod_chunk->start);
88
89         data[2] = HIFIX80(magnitude);
90         data[3] = HIFIX80(offset);
91         data[4] = HI(phase);
92
93         data[5] = LO(period);
94         data[6] = HI(period);
95
96         iforce_send_packet(iforce, FF_CMD_PERIOD, data);
97
98         return 0;
99 }
100
101 /*
102  * Uploads the part of an effect setting the envelope of the force
103  */
104
105 static int make_envelope_modifier(struct iforce* iforce,
106         struct resource* mod_chunk, int no_alloc,
107         u16 attack_duration, __s16 initial_level,
108         u16 fade_duration, __s16 final_level)
109 {
110         unsigned char data[8];
111
112         attack_duration = TIME_SCALE(attack_duration);
113         fade_duration = TIME_SCALE(fade_duration);
114
115         if (!no_alloc) {
116                 mutex_lock(&iforce->mem_mutex);
117                 if (allocate_resource(&(iforce->device_memory), mod_chunk, 0x0e,
118                         iforce->device_memory.start, iforce->device_memory.end, 2L,
119                         NULL, NULL)) {
120                         mutex_unlock(&iforce->mem_mutex);
121                         return -ENOSPC;
122                 }
123                 mutex_unlock(&iforce->mem_mutex);
124         }
125
126         data[0] = LO(mod_chunk->start);
127         data[1] = HI(mod_chunk->start);
128
129         data[2] = LO(attack_duration);
130         data[3] = HI(attack_duration);
131         data[4] = HI(initial_level);
132
133         data[5] = LO(fade_duration);
134         data[6] = HI(fade_duration);
135         data[7] = HI(final_level);
136
137         iforce_send_packet(iforce, FF_CMD_ENVELOPE, data);
138
139         return 0;
140 }
141
142 /*
143  * Component of spring, friction, inertia... effects
144  */
145
146 static int make_condition_modifier(struct iforce* iforce,
147         struct resource* mod_chunk, int no_alloc,
148         __u16 rsat, __u16 lsat, __s16 rk, __s16 lk, u16 db, __s16 center)
149 {
150         unsigned char data[10];
151
152         if (!no_alloc) {
153                 mutex_lock(&iforce->mem_mutex);
154                 if (allocate_resource(&(iforce->device_memory), mod_chunk, 8,
155                         iforce->device_memory.start, iforce->device_memory.end, 2L,
156                         NULL, NULL)) {
157                         mutex_unlock(&iforce->mem_mutex);
158                         return -ENOSPC;
159                 }
160                 mutex_unlock(&iforce->mem_mutex);
161         }
162
163         data[0] = LO(mod_chunk->start);
164         data[1] = HI(mod_chunk->start);
165
166         data[2] = (100 * rk) >> 15;     /* Dangerous: the sign is extended by gcc on plateforms providing an arith shift */
167         data[3] = (100 * lk) >> 15; /* This code is incorrect on cpus lacking arith shift */
168
169         center = (500 * center) >> 15;
170         data[4] = LO(center);
171         data[5] = HI(center);
172
173         db = (1000 * db) >> 16;
174         data[6] = LO(db);
175         data[7] = HI(db);
176
177         data[8] = (100 * rsat) >> 16;
178         data[9] = (100 * lsat) >> 16;
179
180         iforce_send_packet(iforce, FF_CMD_CONDITION, data);
181         iforce_dump_packet("condition", FF_CMD_CONDITION, data);
182
183         return 0;
184 }
185
186 static unsigned char find_button(struct iforce *iforce, signed short button)
187 {
188         int i;
189
190         for (i = 1; iforce->type->btn[i] >= 0; i++)
191                 if (iforce->type->btn[i] == button)
192                         return i + 1;
193         return 0;
194 }
195
196 /*
197  * Analyse the changes in an effect, and tell if we need to send an condition
198  * parameter packet
199  */
200 static int need_condition_modifier(struct ff_effect *old, struct ff_effect *new)
201 {
202         int ret = 0;
203         int i;
204
205         if (new->type != FF_SPRING && new->type != FF_FRICTION) {
206                 warn("bad effect type in need_condition_modifier");
207                 return 0;
208         }
209
210         for (i = 0; i < 2; i++) {
211                 ret |= old->u.condition[i].right_saturation != new->u.condition[i].right_saturation
212                         || old->u.condition[i].left_saturation != new->u.condition[i].left_saturation
213                         || old->u.condition[i].right_coeff != new->u.condition[i].right_coeff
214                         || old->u.condition[i].left_coeff != new->u.condition[i].left_coeff
215                         || old->u.condition[i].deadband != new->u.condition[i].deadband
216                         || old->u.condition[i].center != new->u.condition[i].center;
217         }
218         return ret;
219 }
220
221 /*
222  * Analyse the changes in an effect, and tell if we need to send a magnitude
223  * parameter packet
224  */
225 static int need_magnitude_modifier(struct ff_effect *old, struct ff_effect *effect)
226 {
227         if (effect->type != FF_CONSTANT) {
228                 warn("bad effect type in need_envelope_modifier");
229                 return 0;
230         }
231
232         return old->u.constant.level != effect->u.constant.level;
233 }
234
235 /*
236  * Analyse the changes in an effect, and tell if we need to send an envelope
237  * parameter packet
238  */
239 static int need_envelope_modifier(struct ff_effect *old, struct ff_effect *effect)
240 {
241         switch (effect->type) {
242         case FF_CONSTANT:
243                 if (old->u.constant.envelope.attack_length != effect->u.constant.envelope.attack_length
244                 || old->u.constant.envelope.attack_level != effect->u.constant.envelope.attack_level
245                 || old->u.constant.envelope.fade_length != effect->u.constant.envelope.fade_length
246                 || old->u.constant.envelope.fade_level != effect->u.constant.envelope.fade_level)
247                         return 1;
248                 break;
249
250         case FF_PERIODIC:
251                 if (old->u.periodic.envelope.attack_length != effect->u.periodic.envelope.attack_length
252                 || old->u.periodic.envelope.attack_level != effect->u.periodic.envelope.attack_level
253                 || old->u.periodic.envelope.fade_length != effect->u.periodic.envelope.fade_length
254                 || old->u.periodic.envelope.fade_level != effect->u.periodic.envelope.fade_level)
255                         return 1;
256                 break;
257
258         default:
259                 warn("bad effect type in need_envelope_modifier");
260         }
261
262         return 0;
263 }
264
265 /*
266  * Analyse the changes in an effect, and tell if we need to send a periodic
267  * parameter effect
268  */
269 static int need_period_modifier(struct ff_effect *old, struct ff_effect *new)
270 {
271         if (new->type != FF_PERIODIC) {
272                 warn("bad effect type in need_period_modifier");
273                 return 0;
274         }
275         return (old->u.periodic.period != new->u.periodic.period
276                 || old->u.periodic.magnitude != new->u.periodic.magnitude
277                 || old->u.periodic.offset != new->u.periodic.offset
278                 || old->u.periodic.phase != new->u.periodic.phase);
279 }
280
281 /*
282  * Analyse the changes in an effect, and tell if we need to send an effect
283  * packet
284  */
285 static int need_core(struct ff_effect *old, struct ff_effect *new)
286 {
287         if (old->direction != new->direction
288                 || old->trigger.button != new->trigger.button
289                 || old->trigger.interval != new->trigger.interval
290                 || old->replay.length != new->replay.length
291                 || old->replay.delay != new->replay.delay)
292                 return 1;
293
294         return 0;
295 }
296 /*
297  * Send the part common to all effects to the device
298  */
299 static int make_core(struct iforce* iforce, u16 id, u16 mod_id1, u16 mod_id2,
300         u8 effect_type, u8 axes, u16 duration, u16 delay, u16 button,
301         u16 interval, u16 direction)
302 {
303         unsigned char data[14];
304
305         duration = TIME_SCALE(duration);
306         delay    = TIME_SCALE(delay);
307         interval = TIME_SCALE(interval);
308
309         data[0]  = LO(id);
310         data[1]  = effect_type;
311         data[2]  = LO(axes) | find_button(iforce, button);
312
313         data[3]  = LO(duration);
314         data[4]  = HI(duration);
315
316         data[5]  = HI(direction);
317
318         data[6]  = LO(interval);
319         data[7]  = HI(interval);
320
321         data[8]  = LO(mod_id1);
322         data[9]  = HI(mod_id1);
323         data[10] = LO(mod_id2);
324         data[11] = HI(mod_id2);
325
326         data[12] = LO(delay);
327         data[13] = HI(delay);
328
329         /* Stop effect */
330 /*      iforce_control_playback(iforce, id, 0);*/
331
332         iforce_send_packet(iforce, FF_CMD_EFFECT, data);
333
334         /* If needed, restart effect */
335         if (test_bit(FF_CORE_SHOULD_PLAY, iforce->core_effects[id].flags)) {
336                 /* BUG: perhaps we should replay n times, instead of 1. But we do not know n */
337                 iforce_control_playback(iforce, id, 1);
338         }
339
340         return 0;
341 }
342
343 /*
344  * Upload a periodic effect to the device
345  * See also iforce_upload_constant.
346  */
347 int iforce_upload_periodic(struct iforce *iforce, struct ff_effect *effect, struct ff_effect *old)
348 {
349         u8 wave_code;
350         int core_id = effect->id;
351         struct iforce_core_effect* core_effect = iforce->core_effects + core_id;
352         struct resource* mod1_chunk = &(iforce->core_effects[core_id].mod1_chunk);
353         struct resource* mod2_chunk = &(iforce->core_effects[core_id].mod2_chunk);
354         int param1_err = 1;
355         int param2_err = 1;
356         int core_err = 0;
357
358         if (!old || need_period_modifier(old, effect)) {
359                 param1_err = make_period_modifier(iforce, mod1_chunk,
360                         old != NULL,
361                         effect->u.periodic.magnitude, effect->u.periodic.offset,
362                         effect->u.periodic.period, effect->u.periodic.phase);
363                 if (param1_err)
364                         return param1_err;
365                 set_bit(FF_MOD1_IS_USED, core_effect->flags);
366         }
367
368         if (!old || need_envelope_modifier(old, effect)) {
369                 param2_err = make_envelope_modifier(iforce, mod2_chunk,
370                         old !=NULL,
371                         effect->u.periodic.envelope.attack_length,
372                         effect->u.periodic.envelope.attack_level,
373                         effect->u.periodic.envelope.fade_length,
374                         effect->u.periodic.envelope.fade_level);
375                 if (param2_err)
376                         return param2_err;
377                 set_bit(FF_MOD2_IS_USED, core_effect->flags);
378         }
379
380         switch (effect->u.periodic.waveform) {
381                 case FF_SQUARE:         wave_code = 0x20; break;
382                 case FF_TRIANGLE:       wave_code = 0x21; break;
383                 case FF_SINE:           wave_code = 0x22; break;
384                 case FF_SAW_UP:         wave_code = 0x23; break;
385                 case FF_SAW_DOWN:       wave_code = 0x24; break;
386                 default:                wave_code = 0x20; break;
387         }
388
389         if (!old || need_core(old, effect)) {
390                 core_err = make_core(iforce, effect->id,
391                         mod1_chunk->start,
392                         mod2_chunk->start,
393                         wave_code,
394                         0x20,
395                         effect->replay.length,
396                         effect->replay.delay,
397                         effect->trigger.button,
398                         effect->trigger.interval,
399                         effect->direction);
400         }
401
402         /* If one of the parameter creation failed, we already returned an
403          * error code.
404          * If the core creation failed, we return its error code.
405          * Else: if one parameter at least was created, we return 0
406          *       else we return 1;
407          */
408         return core_err < 0 ? core_err : (param1_err && param2_err);
409 }
410
411 /*
412  * Upload a constant force effect
413  * Return value:
414  *  <0 Error code
415  *  0 Ok, effect created or updated
416  *  1 effect did not change since last upload, and no packet was therefore sent
417  */
418 int iforce_upload_constant(struct iforce *iforce, struct ff_effect *effect, struct ff_effect *old)
419 {
420         int core_id = effect->id;
421         struct iforce_core_effect* core_effect = iforce->core_effects + core_id;
422         struct resource* mod1_chunk = &(iforce->core_effects[core_id].mod1_chunk);
423         struct resource* mod2_chunk = &(iforce->core_effects[core_id].mod2_chunk);
424         int param1_err = 1;
425         int param2_err = 1;
426         int core_err = 0;
427
428         if (!old || need_magnitude_modifier(old, effect)) {
429                 param1_err = make_magnitude_modifier(iforce, mod1_chunk,
430                         old != NULL,
431                         effect->u.constant.level);
432                 if (param1_err)
433                         return param1_err;
434                 set_bit(FF_MOD1_IS_USED, core_effect->flags);
435         }
436
437         if (!old || need_envelope_modifier(old, effect)) {
438                 param2_err = make_envelope_modifier(iforce, mod2_chunk,
439                         old != NULL,
440                         effect->u.constant.envelope.attack_length,
441                         effect->u.constant.envelope.attack_level,
442                         effect->u.constant.envelope.fade_length,
443                         effect->u.constant.envelope.fade_level);
444                 if (param2_err)
445                         return param2_err;
446                 set_bit(FF_MOD2_IS_USED, core_effect->flags);
447         }
448
449         if (!old || need_core(old, effect)) {
450                 core_err = make_core(iforce, effect->id,
451                         mod1_chunk->start,
452                         mod2_chunk->start,
453                         0x00,
454                         0x20,
455                         effect->replay.length,
456                         effect->replay.delay,
457                         effect->trigger.button,
458                         effect->trigger.interval,
459                         effect->direction);
460         }
461
462         /* If one of the parameter creation failed, we already returned an
463          * error code.
464          * If the core creation failed, we return its error code.
465          * Else: if one parameter at least was created, we return 0
466          *       else we return 1;
467          */
468         return core_err < 0 ? core_err : (param1_err && param2_err);
469 }
470
471 /*
472  * Upload an condition effect. Those are for example friction, inertia, springs...
473  */
474 int iforce_upload_condition(struct iforce *iforce, struct ff_effect *effect, struct ff_effect *old)
475 {
476         int core_id = effect->id;
477         struct iforce_core_effect* core_effect = iforce->core_effects + core_id;
478         struct resource* mod1_chunk = &(core_effect->mod1_chunk);
479         struct resource* mod2_chunk = &(core_effect->mod2_chunk);
480         u8 type;
481         int param_err = 1;
482         int core_err = 0;
483
484         switch (effect->type) {
485                 case FF_SPRING: type = 0x40; break;
486                 case FF_DAMPER: type = 0x41; break;
487                 default: return -1;
488         }
489
490         if (!old || need_condition_modifier(old, effect)) {
491                 param_err = make_condition_modifier(iforce, mod1_chunk,
492                         old != NULL,
493                         effect->u.condition[0].right_saturation,
494                         effect->u.condition[0].left_saturation,
495                         effect->u.condition[0].right_coeff,
496                         effect->u.condition[0].left_coeff,
497                         effect->u.condition[0].deadband,
498                         effect->u.condition[0].center);
499                 if (param_err)
500                         return param_err;
501                 set_bit(FF_MOD1_IS_USED, core_effect->flags);
502
503                 param_err = make_condition_modifier(iforce, mod2_chunk,
504                         old != NULL,
505                         effect->u.condition[1].right_saturation,
506                         effect->u.condition[1].left_saturation,
507                         effect->u.condition[1].right_coeff,
508                         effect->u.condition[1].left_coeff,
509                         effect->u.condition[1].deadband,
510                         effect->u.condition[1].center);
511                 if (param_err)
512                         return param_err;
513                 set_bit(FF_MOD2_IS_USED, core_effect->flags);
514
515         }
516
517         if (!old || need_core(old, effect)) {
518                 core_err = make_core(iforce, effect->id,
519                         mod1_chunk->start, mod2_chunk->start,
520                         type, 0xc0,
521                         effect->replay.length, effect->replay.delay,
522                         effect->trigger.button, effect->trigger.interval,
523                         effect->direction);
524         }
525
526         /* If the parameter creation failed, we already returned an
527          * error code.
528          * If the core creation failed, we return its error code.
529          * Else: if a parameter  was created, we return 0
530          *       else we return 1;
531          */
532         return core_err < 0 ? core_err : param_err;
533 }