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