Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Force feedback support for various HID compliant devices by ThrustMaster: |
| 3 | * ThrustMaster FireStorm Dual Power 2 |
| 4 | * and possibly others whose device ids haven't been added. |
| 5 | * |
| 6 | * Modified to support ThrustMaster devices by Zinx Verituse |
| 7 | * on 2003-01-25 from the Logitech force feedback driver, |
| 8 | * which is by Johann Deneux. |
| 9 | * |
| 10 | * Copyright (c) 2003 Zinx Verituse <zinx@epicsol.org> |
| 11 | * Copyright (c) 2002 Johann Deneux |
| 12 | */ |
| 13 | |
| 14 | /* |
| 15 | * This program is free software; you can redistribute it and/or modify |
| 16 | * it under the terms of the GNU General Public License as published by |
| 17 | * the Free Software Foundation; either version 2 of the License, or |
| 18 | * (at your option) any later version. |
| 19 | * |
| 20 | * This program is distributed in the hope that it will be useful, |
| 21 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 23 | * GNU General Public License for more details. |
| 24 | * |
| 25 | * You should have received a copy of the GNU General Public License |
| 26 | * along with this program; if not, write to the Free Software |
| 27 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 28 | */ |
| 29 | |
| 30 | #include <linux/input.h> |
| 31 | #include <linux/sched.h> |
| 32 | |
| 33 | #undef DEBUG |
| 34 | #include <linux/usb.h> |
| 35 | |
| 36 | #include <linux/circ_buf.h> |
| 37 | |
| 38 | #include "hid.h" |
| 39 | #include "fixp-arith.h" |
| 40 | |
| 41 | /* Usages for thrustmaster devices I know about */ |
| 42 | #define THRUSTMASTER_USAGE_RUMBLE_LR (HID_UP_GENDESK | 0xbb) |
| 43 | #define DELAY_CALC(t,delay) ((t) + (delay)*HZ/1000) |
| 44 | |
| 45 | /* Effect status */ |
| 46 | #define EFFECT_STARTED 0 /* Effect is going to play after some time */ |
| 47 | #define EFFECT_PLAYING 1 /* Effect is playing */ |
| 48 | #define EFFECT_USED 2 |
| 49 | |
| 50 | /* For tmff_device::flags */ |
| 51 | #define DEVICE_CLOSING 0 /* The driver is being unitialised */ |
| 52 | |
| 53 | /* Check that the current process can access an effect */ |
| 54 | #define CHECK_OWNERSHIP(effect) (current->pid == 0 \ |
| 55 | || effect.owner == current->pid) |
| 56 | |
| 57 | #define TMFF_CHECK_ID(id) ((id) >= 0 && (id) < TMFF_EFFECTS) |
| 58 | |
| 59 | #define TMFF_CHECK_OWNERSHIP(i, l) \ |
| 60 | (test_bit(EFFECT_USED, l->effects[i].flags) \ |
| 61 | && CHECK_OWNERSHIP(l->effects[i])) |
| 62 | |
| 63 | #define TMFF_EFFECTS 8 |
| 64 | |
| 65 | struct tmff_effect { |
| 66 | pid_t owner; |
| 67 | |
| 68 | struct ff_effect effect; |
| 69 | |
| 70 | unsigned long flags[1]; |
| 71 | unsigned int count; /* Number of times left to play */ |
| 72 | |
| 73 | unsigned long play_at; /* When the effect starts to play */ |
| 74 | unsigned long stop_at; /* When the effect ends */ |
| 75 | }; |
| 76 | |
| 77 | struct tmff_device { |
| 78 | struct hid_device *hid; |
| 79 | |
| 80 | struct hid_report *report; |
| 81 | |
| 82 | struct hid_field *rumble; |
| 83 | |
| 84 | unsigned int effects_playing; |
| 85 | struct tmff_effect effects[TMFF_EFFECTS]; |
| 86 | spinlock_t lock; /* device-level lock. Having locks on |
| 87 | a per-effect basis could be nice, but |
| 88 | isn't really necessary */ |
| 89 | |
| 90 | unsigned long flags[1]; /* Contains various information about the |
| 91 | state of the driver for this device */ |
| 92 | |
| 93 | struct timer_list timer; |
| 94 | }; |
| 95 | |
| 96 | /* Callbacks */ |
| 97 | static void hid_tmff_exit(struct hid_device *hid); |
| 98 | static int hid_tmff_event(struct hid_device *hid, struct input_dev *input, |
| 99 | unsigned int type, unsigned int code, int value); |
| 100 | static int hid_tmff_flush(struct input_dev *input, struct file *file); |
| 101 | static int hid_tmff_upload_effect(struct input_dev *input, |
| 102 | struct ff_effect *effect); |
| 103 | static int hid_tmff_erase(struct input_dev *input, int id); |
| 104 | |
| 105 | /* Local functions */ |
| 106 | static void hid_tmff_recalculate_timer(struct tmff_device *tmff); |
| 107 | static void hid_tmff_timer(unsigned long timer_data); |
| 108 | |
| 109 | int hid_tmff_init(struct hid_device *hid) |
| 110 | { |
| 111 | struct tmff_device *private; |
| 112 | struct list_head *pos; |
| 113 | struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list); |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame^] | 114 | struct input_dev *input_dev = hidinput->input; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | |
| 116 | private = kmalloc(sizeof(struct tmff_device), GFP_KERNEL); |
| 117 | if (!private) |
| 118 | return -ENOMEM; |
| 119 | |
| 120 | memset(private, 0, sizeof(struct tmff_device)); |
| 121 | hid->ff_private = private; |
| 122 | |
| 123 | /* Find the report to use */ |
| 124 | __list_for_each(pos, &hid->report_enum[HID_OUTPUT_REPORT].report_list) { |
| 125 | struct hid_report *report = (struct hid_report *)pos; |
| 126 | int fieldnum; |
| 127 | |
| 128 | for (fieldnum = 0; fieldnum < report->maxfield; ++fieldnum) { |
| 129 | struct hid_field *field = report->field[fieldnum]; |
| 130 | |
| 131 | if (field->maxusage <= 0) |
| 132 | continue; |
| 133 | |
| 134 | switch (field->usage[0].hid) { |
| 135 | case THRUSTMASTER_USAGE_RUMBLE_LR: |
| 136 | if (field->report_count < 2) { |
| 137 | warn("ignoring THRUSTMASTER_USAGE_RUMBLE_LR with report_count < 2"); |
| 138 | continue; |
| 139 | } |
| 140 | |
| 141 | if (field->logical_maximum == field->logical_minimum) { |
| 142 | warn("ignoring THRUSTMASTER_USAGE_RUMBLE_LR with logical_maximum == logical_minimum"); |
| 143 | continue; |
| 144 | } |
| 145 | |
| 146 | if (private->report && private->report != report) { |
| 147 | warn("ignoring THRUSTMASTER_USAGE_RUMBLE_LR in other report"); |
| 148 | continue; |
| 149 | } |
| 150 | |
| 151 | if (private->rumble && private->rumble != field) { |
| 152 | warn("ignoring duplicate THRUSTMASTER_USAGE_RUMBLE_LR"); |
| 153 | continue; |
| 154 | } |
| 155 | |
| 156 | private->report = report; |
| 157 | private->rumble = field; |
| 158 | |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame^] | 159 | set_bit(FF_RUMBLE, input_dev->ffbit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | break; |
| 161 | |
| 162 | default: |
| 163 | warn("ignoring unknown output usage %08x", field->usage[0].hid); |
| 164 | continue; |
| 165 | } |
| 166 | |
| 167 | /* Fallthrough to here only when a valid usage is found */ |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame^] | 168 | input_dev->upload_effect = hid_tmff_upload_effect; |
| 169 | input_dev->flush = hid_tmff_flush; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame^] | 171 | set_bit(EV_FF, input_dev->evbit); |
| 172 | input_dev->ff_effects_max = TMFF_EFFECTS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | |
| 176 | private->hid = hid; |
| 177 | |
| 178 | spin_lock_init(&private->lock); |
| 179 | init_timer(&private->timer); |
| 180 | private->timer.data = (unsigned long)private; |
| 181 | private->timer.function = hid_tmff_timer; |
| 182 | |
| 183 | /* Event and exit callbacks */ |
| 184 | hid->ff_exit = hid_tmff_exit; |
| 185 | hid->ff_event = hid_tmff_event; |
| 186 | |
| 187 | info("Force feedback for ThrustMaster rumble pad devices by Zinx Verituse <zinx@epicsol.org>"); |
| 188 | |
| 189 | return 0; |
| 190 | } |
| 191 | |
| 192 | static void hid_tmff_exit(struct hid_device *hid) |
| 193 | { |
| 194 | struct tmff_device *tmff = hid->ff_private; |
| 195 | unsigned long flags; |
| 196 | |
| 197 | spin_lock_irqsave(&tmff->lock, flags); |
| 198 | |
| 199 | set_bit(DEVICE_CLOSING, tmff->flags); |
| 200 | del_timer_sync(&tmff->timer); |
| 201 | |
| 202 | spin_unlock_irqrestore(&tmff->lock, flags); |
| 203 | |
| 204 | kfree(tmff); |
| 205 | } |
| 206 | |
| 207 | static int hid_tmff_event(struct hid_device *hid, struct input_dev *input, |
| 208 | unsigned int type, unsigned int code, int value) |
| 209 | { |
| 210 | struct tmff_device *tmff = hid->ff_private; |
| 211 | struct tmff_effect *effect = &tmff->effects[code]; |
| 212 | unsigned long flags; |
| 213 | |
| 214 | if (type != EV_FF) |
| 215 | return -EINVAL; |
| 216 | if (!TMFF_CHECK_ID(code)) |
| 217 | return -EINVAL; |
| 218 | if (!TMFF_CHECK_OWNERSHIP(code, tmff)) |
| 219 | return -EACCES; |
| 220 | if (value < 0) |
| 221 | return -EINVAL; |
| 222 | |
| 223 | spin_lock_irqsave(&tmff->lock, flags); |
| 224 | |
| 225 | if (value > 0) { |
| 226 | set_bit(EFFECT_STARTED, effect->flags); |
| 227 | clear_bit(EFFECT_PLAYING, effect->flags); |
| 228 | effect->count = value; |
| 229 | effect->play_at = DELAY_CALC(jiffies, effect->effect.replay.delay); |
| 230 | } else { |
| 231 | clear_bit(EFFECT_STARTED, effect->flags); |
| 232 | clear_bit(EFFECT_PLAYING, effect->flags); |
| 233 | } |
| 234 | |
| 235 | hid_tmff_recalculate_timer(tmff); |
| 236 | |
| 237 | spin_unlock_irqrestore(&tmff->lock, flags); |
| 238 | |
| 239 | return 0; |
| 240 | |
| 241 | } |
| 242 | |
| 243 | /* Erase all effects this process owns */ |
| 244 | |
| 245 | static int hid_tmff_flush(struct input_dev *dev, struct file *file) |
| 246 | { |
| 247 | struct hid_device *hid = dev->private; |
| 248 | struct tmff_device *tmff = hid->ff_private; |
| 249 | int i; |
| 250 | |
| 251 | for (i=0; i<dev->ff_effects_max; ++i) |
| 252 | |
| 253 | /* NOTE: no need to lock here. The only times EFFECT_USED is |
| 254 | modified is when effects are uploaded or when an effect is |
| 255 | erased. But a process cannot close its dev/input/eventX fd |
| 256 | and perform ioctls on the same fd all at the same time */ |
| 257 | |
| 258 | if (current->pid == tmff->effects[i].owner |
| 259 | && test_bit(EFFECT_USED, tmff->effects[i].flags)) |
| 260 | if (hid_tmff_erase(dev, i)) |
| 261 | warn("erase effect %d failed", i); |
| 262 | |
| 263 | |
| 264 | return 0; |
| 265 | } |
| 266 | |
| 267 | static int hid_tmff_erase(struct input_dev *dev, int id) |
| 268 | { |
| 269 | struct hid_device *hid = dev->private; |
| 270 | struct tmff_device *tmff = hid->ff_private; |
| 271 | unsigned long flags; |
| 272 | |
| 273 | if (!TMFF_CHECK_ID(id)) |
| 274 | return -EINVAL; |
| 275 | if (!TMFF_CHECK_OWNERSHIP(id, tmff)) |
| 276 | return -EACCES; |
| 277 | |
| 278 | spin_lock_irqsave(&tmff->lock, flags); |
| 279 | |
| 280 | tmff->effects[id].flags[0] = 0; |
| 281 | hid_tmff_recalculate_timer(tmff); |
| 282 | |
| 283 | spin_unlock_irqrestore(&tmff->lock, flags); |
| 284 | |
| 285 | return 0; |
| 286 | } |
| 287 | |
| 288 | static int hid_tmff_upload_effect(struct input_dev *input, |
| 289 | struct ff_effect *effect) |
| 290 | { |
| 291 | struct hid_device *hid = input->private; |
| 292 | struct tmff_device *tmff = hid->ff_private; |
| 293 | int id; |
| 294 | unsigned long flags; |
| 295 | |
| 296 | if (!test_bit(effect->type, input->ffbit)) |
| 297 | return -EINVAL; |
| 298 | if (effect->id != -1 && !TMFF_CHECK_ID(effect->id)) |
| 299 | return -EINVAL; |
| 300 | |
| 301 | spin_lock_irqsave(&tmff->lock, flags); |
| 302 | |
| 303 | if (effect->id == -1) { |
| 304 | /* Find a free effect */ |
| 305 | for (id = 0; id < TMFF_EFFECTS && test_bit(EFFECT_USED, tmff->effects[id].flags); ++id); |
| 306 | |
| 307 | if (id >= TMFF_EFFECTS) { |
| 308 | spin_unlock_irqrestore(&tmff->lock, flags); |
| 309 | return -ENOSPC; |
| 310 | } |
| 311 | |
| 312 | effect->id = id; |
| 313 | tmff->effects[id].owner = current->pid; |
| 314 | tmff->effects[id].flags[0] = 0; |
| 315 | set_bit(EFFECT_USED, tmff->effects[id].flags); |
| 316 | |
| 317 | } else { |
| 318 | /* Re-uploading an owned effect, to change parameters */ |
| 319 | id = effect->id; |
| 320 | clear_bit(EFFECT_PLAYING, tmff->effects[id].flags); |
| 321 | } |
| 322 | |
| 323 | tmff->effects[id].effect = *effect; |
| 324 | |
| 325 | hid_tmff_recalculate_timer(tmff); |
| 326 | |
| 327 | spin_unlock_irqrestore(&tmff->lock, flags); |
| 328 | return 0; |
| 329 | } |
| 330 | |
| 331 | /* Start the timer for the next start/stop/delay */ |
| 332 | /* Always call this while tmff->lock is locked */ |
| 333 | |
| 334 | static void hid_tmff_recalculate_timer(struct tmff_device *tmff) |
| 335 | { |
| 336 | int i; |
| 337 | int events = 0; |
| 338 | unsigned long next_time; |
| 339 | |
| 340 | next_time = 0; /* Shut up compiler's incorrect warning */ |
| 341 | |
| 342 | /* Find the next change in an effect's status */ |
| 343 | for (i = 0; i < TMFF_EFFECTS; ++i) { |
| 344 | struct tmff_effect *effect = &tmff->effects[i]; |
| 345 | unsigned long play_time; |
| 346 | |
| 347 | if (!test_bit(EFFECT_STARTED, effect->flags)) |
| 348 | continue; |
| 349 | |
| 350 | effect->stop_at = DELAY_CALC(effect->play_at, effect->effect.replay.length); |
| 351 | |
| 352 | if (!test_bit(EFFECT_PLAYING, effect->flags)) |
| 353 | play_time = effect->play_at; |
| 354 | else |
| 355 | play_time = effect->stop_at; |
| 356 | |
| 357 | events++; |
| 358 | |
| 359 | if (time_after(jiffies, play_time)) |
| 360 | play_time = jiffies; |
| 361 | |
| 362 | if (events == 1) |
| 363 | next_time = play_time; |
| 364 | else { |
| 365 | if (time_after(next_time, play_time)) |
| 366 | next_time = play_time; |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | if (!events && tmff->effects_playing) { |
| 371 | /* Treat all effects turning off as an event */ |
| 372 | events = 1; |
| 373 | next_time = jiffies; |
| 374 | } |
| 375 | |
| 376 | if (!events) { |
| 377 | /* No events, no time, no need for a timer. */ |
| 378 | del_timer_sync(&tmff->timer); |
| 379 | return; |
| 380 | } |
| 381 | |
| 382 | mod_timer(&tmff->timer, next_time); |
| 383 | } |
| 384 | |
| 385 | /* Changes values from 0 to 0xffff into values from minimum to maximum */ |
| 386 | static inline int hid_tmff_scale(unsigned int in, int minimum, int maximum) |
| 387 | { |
| 388 | int ret; |
| 389 | |
| 390 | ret = (in * (maximum - minimum) / 0xffff) + minimum; |
| 391 | if (ret < minimum) |
| 392 | return minimum; |
| 393 | if (ret > maximum) |
| 394 | return maximum; |
| 395 | return ret; |
| 396 | } |
| 397 | |
| 398 | static void hid_tmff_timer(unsigned long timer_data) |
| 399 | { |
| 400 | struct tmff_device *tmff = (struct tmff_device *) timer_data; |
| 401 | struct hid_device *hid = tmff->hid; |
| 402 | unsigned long flags; |
| 403 | int left = 0, right = 0; /* Rumbling */ |
| 404 | int i; |
| 405 | |
| 406 | spin_lock_irqsave(&tmff->lock, flags); |
| 407 | |
| 408 | tmff->effects_playing = 0; |
| 409 | |
| 410 | for (i = 0; i < TMFF_EFFECTS; ++i) { |
| 411 | struct tmff_effect *effect = &tmff->effects[i]; |
| 412 | |
| 413 | if (!test_bit(EFFECT_STARTED, effect->flags)) |
| 414 | continue; |
| 415 | |
| 416 | if (!time_after(jiffies, effect->play_at)) |
| 417 | continue; |
| 418 | |
| 419 | if (time_after(jiffies, effect->stop_at)) { |
| 420 | |
| 421 | dbg("Finished playing once %d", i); |
| 422 | clear_bit(EFFECT_PLAYING, effect->flags); |
| 423 | |
| 424 | if (--effect->count <= 0) { |
| 425 | dbg("Stopped %d", i); |
| 426 | clear_bit(EFFECT_STARTED, effect->flags); |
| 427 | continue; |
| 428 | } else { |
| 429 | dbg("Start again %d", i); |
| 430 | effect->play_at = DELAY_CALC(jiffies, effect->effect.replay.delay); |
| 431 | continue; |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | ++tmff->effects_playing; |
| 436 | |
| 437 | set_bit(EFFECT_PLAYING, effect->flags); |
| 438 | |
| 439 | switch (effect->effect.type) { |
| 440 | case FF_RUMBLE: |
| 441 | right += effect->effect.u.rumble.strong_magnitude; |
| 442 | left += effect->effect.u.rumble.weak_magnitude; |
| 443 | break; |
| 444 | default: |
| 445 | BUG(); |
| 446 | break; |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | left = hid_tmff_scale(left, tmff->rumble->logical_minimum, tmff->rumble->logical_maximum); |
| 451 | right = hid_tmff_scale(right, tmff->rumble->logical_minimum, tmff->rumble->logical_maximum); |
| 452 | |
| 453 | if (left != tmff->rumble->value[0] || right != tmff->rumble->value[1]) { |
| 454 | tmff->rumble->value[0] = left; |
| 455 | tmff->rumble->value[1] = right; |
| 456 | dbg("(left,right)=(%08x, %08x)", left, right); |
| 457 | hid_submit_report(hid, tmff->report, USB_DIR_OUT); |
| 458 | } |
| 459 | |
| 460 | if (!test_bit(DEVICE_CLOSING, tmff->flags)) |
| 461 | hid_tmff_recalculate_timer(tmff); |
| 462 | |
| 463 | spin_unlock_irqrestore(&tmff->lock, flags); |
| 464 | } |