Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
at v2.6.12 463 lines 12 kB view raw
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 65struct 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 77struct 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 */ 97static void hid_tmff_exit(struct hid_device *hid); 98static int hid_tmff_event(struct hid_device *hid, struct input_dev *input, 99 unsigned int type, unsigned int code, int value); 100static int hid_tmff_flush(struct input_dev *input, struct file *file); 101static int hid_tmff_upload_effect(struct input_dev *input, 102 struct ff_effect *effect); 103static int hid_tmff_erase(struct input_dev *input, int id); 104 105/* Local functions */ 106static void hid_tmff_recalculate_timer(struct tmff_device *tmff); 107static void hid_tmff_timer(unsigned long timer_data); 108 109int 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); 114 115 private = kmalloc(sizeof(struct tmff_device), GFP_KERNEL); 116 if (!private) 117 return -ENOMEM; 118 119 memset(private, 0, sizeof(struct tmff_device)); 120 hid->ff_private = private; 121 122 /* Find the report to use */ 123 __list_for_each(pos, &hid->report_enum[HID_OUTPUT_REPORT].report_list) { 124 struct hid_report *report = (struct hid_report *)pos; 125 int fieldnum; 126 127 for (fieldnum = 0; fieldnum < report->maxfield; ++fieldnum) { 128 struct hid_field *field = report->field[fieldnum]; 129 130 if (field->maxusage <= 0) 131 continue; 132 133 switch (field->usage[0].hid) { 134 case THRUSTMASTER_USAGE_RUMBLE_LR: 135 if (field->report_count < 2) { 136 warn("ignoring THRUSTMASTER_USAGE_RUMBLE_LR with report_count < 2"); 137 continue; 138 } 139 140 if (field->logical_maximum == field->logical_minimum) { 141 warn("ignoring THRUSTMASTER_USAGE_RUMBLE_LR with logical_maximum == logical_minimum"); 142 continue; 143 } 144 145 if (private->report && private->report != report) { 146 warn("ignoring THRUSTMASTER_USAGE_RUMBLE_LR in other report"); 147 continue; 148 } 149 150 if (private->rumble && private->rumble != field) { 151 warn("ignoring duplicate THRUSTMASTER_USAGE_RUMBLE_LR"); 152 continue; 153 } 154 155 private->report = report; 156 private->rumble = field; 157 158 set_bit(FF_RUMBLE, hidinput->input.ffbit); 159 break; 160 161 default: 162 warn("ignoring unknown output usage %08x", field->usage[0].hid); 163 continue; 164 } 165 166 /* Fallthrough to here only when a valid usage is found */ 167 hidinput->input.upload_effect = hid_tmff_upload_effect; 168 hidinput->input.flush = hid_tmff_flush; 169 170 set_bit(EV_FF, hidinput->input.evbit); 171 hidinput->input.ff_effects_max = TMFF_EFFECTS; 172 } 173 } 174 175 private->hid = hid; 176 177 spin_lock_init(&private->lock); 178 init_timer(&private->timer); 179 private->timer.data = (unsigned long)private; 180 private->timer.function = hid_tmff_timer; 181 182 /* Event and exit callbacks */ 183 hid->ff_exit = hid_tmff_exit; 184 hid->ff_event = hid_tmff_event; 185 186 info("Force feedback for ThrustMaster rumble pad devices by Zinx Verituse <zinx@epicsol.org>"); 187 188 return 0; 189} 190 191static void hid_tmff_exit(struct hid_device *hid) 192{ 193 struct tmff_device *tmff = hid->ff_private; 194 unsigned long flags; 195 196 spin_lock_irqsave(&tmff->lock, flags); 197 198 set_bit(DEVICE_CLOSING, tmff->flags); 199 del_timer_sync(&tmff->timer); 200 201 spin_unlock_irqrestore(&tmff->lock, flags); 202 203 kfree(tmff); 204} 205 206static int hid_tmff_event(struct hid_device *hid, struct input_dev *input, 207 unsigned int type, unsigned int code, int value) 208{ 209 struct tmff_device *tmff = hid->ff_private; 210 struct tmff_effect *effect = &tmff->effects[code]; 211 unsigned long flags; 212 213 if (type != EV_FF) 214 return -EINVAL; 215 if (!TMFF_CHECK_ID(code)) 216 return -EINVAL; 217 if (!TMFF_CHECK_OWNERSHIP(code, tmff)) 218 return -EACCES; 219 if (value < 0) 220 return -EINVAL; 221 222 spin_lock_irqsave(&tmff->lock, flags); 223 224 if (value > 0) { 225 set_bit(EFFECT_STARTED, effect->flags); 226 clear_bit(EFFECT_PLAYING, effect->flags); 227 effect->count = value; 228 effect->play_at = DELAY_CALC(jiffies, effect->effect.replay.delay); 229 } else { 230 clear_bit(EFFECT_STARTED, effect->flags); 231 clear_bit(EFFECT_PLAYING, effect->flags); 232 } 233 234 hid_tmff_recalculate_timer(tmff); 235 236 spin_unlock_irqrestore(&tmff->lock, flags); 237 238 return 0; 239 240} 241 242/* Erase all effects this process owns */ 243 244static int hid_tmff_flush(struct input_dev *dev, struct file *file) 245{ 246 struct hid_device *hid = dev->private; 247 struct tmff_device *tmff = hid->ff_private; 248 int i; 249 250 for (i=0; i<dev->ff_effects_max; ++i) 251 252 /* NOTE: no need to lock here. The only times EFFECT_USED is 253 modified is when effects are uploaded or when an effect is 254 erased. But a process cannot close its dev/input/eventX fd 255 and perform ioctls on the same fd all at the same time */ 256 257 if (current->pid == tmff->effects[i].owner 258 && test_bit(EFFECT_USED, tmff->effects[i].flags)) 259 if (hid_tmff_erase(dev, i)) 260 warn("erase effect %d failed", i); 261 262 263 return 0; 264} 265 266static int hid_tmff_erase(struct input_dev *dev, int id) 267{ 268 struct hid_device *hid = dev->private; 269 struct tmff_device *tmff = hid->ff_private; 270 unsigned long flags; 271 272 if (!TMFF_CHECK_ID(id)) 273 return -EINVAL; 274 if (!TMFF_CHECK_OWNERSHIP(id, tmff)) 275 return -EACCES; 276 277 spin_lock_irqsave(&tmff->lock, flags); 278 279 tmff->effects[id].flags[0] = 0; 280 hid_tmff_recalculate_timer(tmff); 281 282 spin_unlock_irqrestore(&tmff->lock, flags); 283 284 return 0; 285} 286 287static int hid_tmff_upload_effect(struct input_dev *input, 288 struct ff_effect *effect) 289{ 290 struct hid_device *hid = input->private; 291 struct tmff_device *tmff = hid->ff_private; 292 int id; 293 unsigned long flags; 294 295 if (!test_bit(effect->type, input->ffbit)) 296 return -EINVAL; 297 if (effect->id != -1 && !TMFF_CHECK_ID(effect->id)) 298 return -EINVAL; 299 300 spin_lock_irqsave(&tmff->lock, flags); 301 302 if (effect->id == -1) { 303 /* Find a free effect */ 304 for (id = 0; id < TMFF_EFFECTS && test_bit(EFFECT_USED, tmff->effects[id].flags); ++id); 305 306 if (id >= TMFF_EFFECTS) { 307 spin_unlock_irqrestore(&tmff->lock, flags); 308 return -ENOSPC; 309 } 310 311 effect->id = id; 312 tmff->effects[id].owner = current->pid; 313 tmff->effects[id].flags[0] = 0; 314 set_bit(EFFECT_USED, tmff->effects[id].flags); 315 316 } else { 317 /* Re-uploading an owned effect, to change parameters */ 318 id = effect->id; 319 clear_bit(EFFECT_PLAYING, tmff->effects[id].flags); 320 } 321 322 tmff->effects[id].effect = *effect; 323 324 hid_tmff_recalculate_timer(tmff); 325 326 spin_unlock_irqrestore(&tmff->lock, flags); 327 return 0; 328} 329 330/* Start the timer for the next start/stop/delay */ 331/* Always call this while tmff->lock is locked */ 332 333static void hid_tmff_recalculate_timer(struct tmff_device *tmff) 334{ 335 int i; 336 int events = 0; 337 unsigned long next_time; 338 339 next_time = 0; /* Shut up compiler's incorrect warning */ 340 341 /* Find the next change in an effect's status */ 342 for (i = 0; i < TMFF_EFFECTS; ++i) { 343 struct tmff_effect *effect = &tmff->effects[i]; 344 unsigned long play_time; 345 346 if (!test_bit(EFFECT_STARTED, effect->flags)) 347 continue; 348 349 effect->stop_at = DELAY_CALC(effect->play_at, effect->effect.replay.length); 350 351 if (!test_bit(EFFECT_PLAYING, effect->flags)) 352 play_time = effect->play_at; 353 else 354 play_time = effect->stop_at; 355 356 events++; 357 358 if (time_after(jiffies, play_time)) 359 play_time = jiffies; 360 361 if (events == 1) 362 next_time = play_time; 363 else { 364 if (time_after(next_time, play_time)) 365 next_time = play_time; 366 } 367 } 368 369 if (!events && tmff->effects_playing) { 370 /* Treat all effects turning off as an event */ 371 events = 1; 372 next_time = jiffies; 373 } 374 375 if (!events) { 376 /* No events, no time, no need for a timer. */ 377 del_timer_sync(&tmff->timer); 378 return; 379 } 380 381 mod_timer(&tmff->timer, next_time); 382} 383 384/* Changes values from 0 to 0xffff into values from minimum to maximum */ 385static inline int hid_tmff_scale(unsigned int in, int minimum, int maximum) 386{ 387 int ret; 388 389 ret = (in * (maximum - minimum) / 0xffff) + minimum; 390 if (ret < minimum) 391 return minimum; 392 if (ret > maximum) 393 return maximum; 394 return ret; 395} 396 397static void hid_tmff_timer(unsigned long timer_data) 398{ 399 struct tmff_device *tmff = (struct tmff_device *) timer_data; 400 struct hid_device *hid = tmff->hid; 401 unsigned long flags; 402 int left = 0, right = 0; /* Rumbling */ 403 int i; 404 405 spin_lock_irqsave(&tmff->lock, flags); 406 407 tmff->effects_playing = 0; 408 409 for (i = 0; i < TMFF_EFFECTS; ++i) { 410 struct tmff_effect *effect = &tmff->effects[i]; 411 412 if (!test_bit(EFFECT_STARTED, effect->flags)) 413 continue; 414 415 if (!time_after(jiffies, effect->play_at)) 416 continue; 417 418 if (time_after(jiffies, effect->stop_at)) { 419 420 dbg("Finished playing once %d", i); 421 clear_bit(EFFECT_PLAYING, effect->flags); 422 423 if (--effect->count <= 0) { 424 dbg("Stopped %d", i); 425 clear_bit(EFFECT_STARTED, effect->flags); 426 continue; 427 } else { 428 dbg("Start again %d", i); 429 effect->play_at = DELAY_CALC(jiffies, effect->effect.replay.delay); 430 continue; 431 } 432 } 433 434 ++tmff->effects_playing; 435 436 set_bit(EFFECT_PLAYING, effect->flags); 437 438 switch (effect->effect.type) { 439 case FF_RUMBLE: 440 right += effect->effect.u.rumble.strong_magnitude; 441 left += effect->effect.u.rumble.weak_magnitude; 442 break; 443 default: 444 BUG(); 445 break; 446 } 447 } 448 449 left = hid_tmff_scale(left, tmff->rumble->logical_minimum, tmff->rumble->logical_maximum); 450 right = hid_tmff_scale(right, tmff->rumble->logical_minimum, tmff->rumble->logical_maximum); 451 452 if (left != tmff->rumble->value[0] || right != tmff->rumble->value[1]) { 453 tmff->rumble->value[0] = left; 454 tmff->rumble->value[1] = right; 455 dbg("(left,right)=(%08x, %08x)", left, right); 456 hid_submit_report(hid, tmff->report, USB_DIR_OUT); 457 } 458 459 if (!test_bit(DEVICE_CLOSING, tmff->flags)) 460 hid_tmff_recalculate_timer(tmff); 461 462 spin_unlock_irqrestore(&tmff->lock, flags); 463}