Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Input: use new FF interface in the HID force feedback drivers

Signed-off-by: Anssi Hannula <anssi.hannula@gmail.com>
Signed-off-by: Dmitry Torokhov <dtor@mail.ru>

authored by

Anssi Hannula and committed by
Dmitry Torokhov
dc76c912 224ee88f

+118 -833
+2
drivers/usb/input/Kconfig
··· 70 70 config LOGITECH_FF 71 71 bool "Logitech WingMan *3D support" 72 72 depends on HID_FF 73 + select INPUT_FF_MEMLESS if USB_HID 73 74 help 74 75 Say Y here if you have one of these devices: 75 76 - Logitech WingMan Cordless RumblePad ··· 82 81 config THRUSTMASTER_FF 83 82 bool "ThrustMaster FireStorm Dual Power 2 support (EXPERIMENTAL)" 84 83 depends on HID_FF && EXPERIMENTAL 84 + select INPUT_FF_MEMLESS if USB_HID 85 85 help 86 86 Say Y here if you have a THRUSTMASTER FireStore Dual Power 2, 87 87 and want to enable force feedback support for it.
-2
drivers/usb/input/hid-core.c
··· 543 543 { 544 544 unsigned i,j; 545 545 546 - hid_ff_exit(device); 547 - 548 546 for (i = 0; i < HID_REPORT_TYPES; i++) { 549 547 struct hid_report_enum *report_enum = device->report_enum + i; 550 548
+1 -1
drivers/usb/input/hid-input.c
··· 729 729 int offset; 730 730 731 731 if (type == EV_FF) 732 - return hid_ff_event(hid, dev, type, code, value); 732 + return input_ff_event(dev, type, code, value); 733 733 734 734 if (type != EV_LED) 735 735 return -1;
+72 -451
drivers/usb/input/hid-lgff.c
··· 1 1 /* 2 - * $$ 3 - * 4 2 * Force feedback support for hid-compliant for some of the devices from 5 3 * Logitech, namely: 6 4 * - WingMan Cordless RumblePad 7 5 * - WingMan Force 3D 8 6 * 9 7 * Copyright (c) 2002-2004 Johann Deneux 8 + * Copyright (c) 2006 Anssi Hannula <anssi.hannula@gmail.com> 10 9 */ 11 10 12 11 /* ··· 28 29 */ 29 30 30 31 #include <linux/input.h> 31 - #include <linux/sched.h> 32 - 33 - //#define DEBUG 34 32 #include <linux/usb.h> 35 - 36 - #include <linux/circ_buf.h> 37 - 38 33 #include "hid.h" 39 - #include "../../input/fixp-arith.h" 40 - 41 - 42 - /* Periodicity of the update */ 43 - #define PERIOD (HZ/10) 44 - 45 - #define RUN_AT(t) (jiffies + (t)) 46 - 47 - /* Effect status */ 48 - #define EFFECT_STARTED 0 /* Effect is going to play after some time 49 - (ff_replay.delay) */ 50 - #define EFFECT_PLAYING 1 /* Effect is being played */ 51 - #define EFFECT_USED 2 52 - 53 - // For lgff_device::flags 54 - #define DEVICE_CLOSING 0 /* The driver is being unitialised */ 55 - 56 - /* Check that the current process can access an effect */ 57 - #define CHECK_OWNERSHIP(effect) (current->pid == 0 \ 58 - || effect.owner == current->pid) 59 - 60 - #define LGFF_CHECK_OWNERSHIP(i, l) \ 61 - (i>=0 && i<LGFF_EFFECTS \ 62 - && test_bit(EFFECT_USED, l->effects[i].flags) \ 63 - && CHECK_OWNERSHIP(l->effects[i])) 64 - 65 - #define LGFF_EFFECTS 8 66 34 67 35 struct device_type { 68 36 u16 idVendor; 69 37 u16 idProduct; 70 - signed short *ff; 38 + const signed short *ff; 71 39 }; 72 40 73 - struct lgff_effect { 74 - pid_t owner; 75 - 76 - struct ff_effect effect; 77 - 78 - unsigned long flags[1]; 79 - unsigned int count; /* Number of times left to play */ 80 - unsigned long started_at; /* When the effect started to play */ 81 - }; 82 - 83 - struct lgff_device { 84 - struct hid_device* hid; 85 - 86 - struct hid_report* constant; 87 - struct hid_report* rumble; 88 - struct hid_report* condition; 89 - 90 - struct lgff_effect effects[LGFF_EFFECTS]; 91 - spinlock_t lock; /* device-level lock. Having locks on 92 - a per-effect basis could be nice, but 93 - isn't really necessary */ 94 - 95 - unsigned long flags[1]; /* Contains various information about the 96 - state of the driver for this device */ 97 - 98 - struct timer_list timer; 99 - }; 100 - 101 - /* Callbacks */ 102 - static void hid_lgff_exit(struct hid_device* hid); 103 - static int hid_lgff_event(struct hid_device *hid, struct input_dev *input, 104 - unsigned int type, unsigned int code, int value); 105 - static int hid_lgff_flush(struct input_dev *input, struct file *file); 106 - static int hid_lgff_upload_effect(struct input_dev *input, 107 - struct ff_effect *effect); 108 - static int hid_lgff_erase(struct input_dev *input, int id); 109 - 110 - /* Local functions */ 111 - static void hid_lgff_input_init(struct hid_device* hid); 112 - static void hid_lgff_timer(unsigned long timer_data); 113 - static struct hid_report* hid_lgff_duplicate_report(struct hid_report*); 114 - static void hid_lgff_delete_report(struct hid_report*); 115 - 116 - static signed short ff_rumble[] = { 41 + static const signed short ff_rumble[] = { 117 42 FF_RUMBLE, 118 43 -1 119 44 }; 120 45 121 - static signed short ff_joystick[] = { 46 + static const signed short ff_joystick[] = { 122 47 FF_CONSTANT, 123 48 -1 124 49 }; 125 50 126 - static struct device_type devices[] = { 127 - {0x046d, 0xc211, ff_rumble}, 128 - {0x046d, 0xc219, ff_rumble}, 129 - {0x046d, 0xc283, ff_joystick}, 130 - {0x0000, 0x0000, ff_joystick} 51 + static const struct device_type devices[] = { 52 + { 0x046d, 0xc211, ff_rumble }, 53 + { 0x046d, 0xc219, ff_rumble }, 54 + { 0x046d, 0xc283, ff_joystick }, 55 + { 0x0000, 0x0000, ff_joystick } 131 56 }; 57 + 58 + static int hid_lgff_play(struct input_dev *dev, void *data, struct ff_effect *effect) 59 + { 60 + struct hid_device *hid = dev->private; 61 + struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list; 62 + struct hid_report *report = list_entry(report_list->next, struct hid_report, list); 63 + int x, y; 64 + unsigned int left, right; 65 + 66 + #define CLAMP(x) if (x < 0) x = 0; if (x > 0xff) x = 0xff 67 + 68 + switch (effect->type) { 69 + case FF_CONSTANT: 70 + x = effect->u.ramp.start_level + 0x7f; /* 0x7f is center */ 71 + y = effect->u.ramp.end_level + 0x7f; 72 + CLAMP(x); 73 + CLAMP(y); 74 + report->field[0]->value[0] = 0x51; 75 + report->field[0]->value[1] = 0x08; 76 + report->field[0]->value[2] = x; 77 + report->field[0]->value[3] = y; 78 + dbg("(x, y)=(%04x, %04x)", x, y); 79 + hid_submit_report(hid, report, USB_DIR_OUT); 80 + break; 81 + 82 + case FF_RUMBLE: 83 + right = effect->u.rumble.strong_magnitude; 84 + left = effect->u.rumble.weak_magnitude; 85 + right = right * 0xff / 0xffff; 86 + left = left * 0xff / 0xffff; 87 + CLAMP(left); 88 + CLAMP(right); 89 + report->field[0]->value[0] = 0x42; 90 + report->field[0]->value[1] = 0x00; 91 + report->field[0]->value[2] = left; 92 + report->field[0]->value[3] = right; 93 + dbg("(left, right)=(%04x, %04x)", left, right); 94 + hid_submit_report(hid, report, USB_DIR_OUT); 95 + break; 96 + } 97 + return 0; 98 + } 132 99 133 100 int hid_lgff_init(struct hid_device* hid) 134 101 { 135 - struct lgff_device *private; 136 - struct hid_report* report; 137 - struct hid_field* field; 102 + struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list); 103 + struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list; 104 + struct input_dev *dev = hidinput->input; 105 + struct hid_report *report; 106 + struct hid_field *field; 107 + int error; 108 + int i, j; 138 109 139 110 /* Find the report to use */ 140 - if (list_empty(&hid->report_enum[HID_OUTPUT_REPORT].report_list)) { 111 + if (list_empty(report_list)) { 141 112 err("No output report found"); 142 113 return -1; 143 114 } 115 + 144 116 /* Check that the report looks ok */ 145 - report = (struct hid_report*)hid->report_enum[HID_OUTPUT_REPORT].report_list.next; 117 + report = list_entry(report_list->next, struct hid_report, list); 146 118 if (!report) { 147 119 err("NULL output report"); 148 120 return -1; 149 121 } 122 + 150 123 field = report->field[0]; 151 124 if (!field) { 152 125 err("NULL field"); 153 126 return -1; 154 127 } 155 128 156 - private = kzalloc(sizeof(struct lgff_device), GFP_KERNEL); 157 - if (!private) 158 - return -1; 159 - hid->ff_private = private; 160 - 161 - /* Input init */ 162 - hid_lgff_input_init(hid); 163 - 164 - 165 - private->constant = hid_lgff_duplicate_report(report); 166 - if (!private->constant) { 167 - kfree(private); 168 - return -1; 169 - } 170 - private->constant->field[0]->value[0] = 0x51; 171 - private->constant->field[0]->value[1] = 0x08; 172 - private->constant->field[0]->value[2] = 0x7f; 173 - private->constant->field[0]->value[3] = 0x7f; 174 - 175 - private->rumble = hid_lgff_duplicate_report(report); 176 - if (!private->rumble) { 177 - hid_lgff_delete_report(private->constant); 178 - kfree(private); 179 - return -1; 180 - } 181 - private->rumble->field[0]->value[0] = 0x42; 182 - 183 - 184 - private->condition = hid_lgff_duplicate_report(report); 185 - if (!private->condition) { 186 - hid_lgff_delete_report(private->rumble); 187 - hid_lgff_delete_report(private->constant); 188 - kfree(private); 189 - return -1; 129 + for (i = 0; i < ARRAY_SIZE(devices); i++) { 130 + if (dev->id.vendor == devices[i].idVendor && 131 + dev->id.product == devices[i].idProduct) { 132 + for (j = 0; devices[i].ff[j] >= 0; j++) 133 + set_bit(devices[i].ff[j], dev->ffbit); 134 + break; 135 + } 190 136 } 191 137 192 - private->hid = hid; 193 - 194 - spin_lock_init(&private->lock); 195 - init_timer(&private->timer); 196 - private->timer.data = (unsigned long)private; 197 - private->timer.function = hid_lgff_timer; 198 - 199 - /* Event and exit callbacks */ 200 - hid->ff_exit = hid_lgff_exit; 201 - hid->ff_event = hid_lgff_event; 202 - 203 - /* Start the update task */ 204 - private->timer.expires = RUN_AT(PERIOD); 205 - add_timer(&private->timer); /*TODO: only run the timer when at least 206 - one effect is playing */ 138 + error = input_ff_create_memless(dev, NULL, hid_lgff_play); 139 + if (error) 140 + return error; 207 141 208 142 printk(KERN_INFO "Force feedback for Logitech force feedback devices by Johann Deneux <johann.deneux@it.uu.se>\n"); 209 143 210 144 return 0; 211 - } 212 - 213 - static struct hid_report* hid_lgff_duplicate_report(struct hid_report* report) 214 - { 215 - struct hid_report* ret; 216 - 217 - ret = kmalloc(sizeof(struct lgff_device), GFP_KERNEL); 218 - if (!ret) 219 - return NULL; 220 - *ret = *report; 221 - 222 - ret->field[0] = kmalloc(sizeof(struct hid_field), GFP_KERNEL); 223 - if (!ret->field[0]) { 224 - kfree(ret); 225 - return NULL; 226 - } 227 - *ret->field[0] = *report->field[0]; 228 - 229 - ret->field[0]->value = kzalloc(sizeof(s32[8]), GFP_KERNEL); 230 - if (!ret->field[0]->value) { 231 - kfree(ret->field[0]); 232 - kfree(ret); 233 - return NULL; 234 - } 235 - 236 - return ret; 237 - } 238 - 239 - static void hid_lgff_delete_report(struct hid_report* report) 240 - { 241 - if (report) { 242 - kfree(report->field[0]->value); 243 - kfree(report->field[0]); 244 - kfree(report); 245 - } 246 - } 247 - 248 - static void hid_lgff_input_init(struct hid_device* hid) 249 - { 250 - struct device_type* dev = devices; 251 - signed short* ff; 252 - u16 idVendor = le16_to_cpu(hid->dev->descriptor.idVendor); 253 - u16 idProduct = le16_to_cpu(hid->dev->descriptor.idProduct); 254 - struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list); 255 - struct input_dev *input_dev = hidinput->input; 256 - 257 - while (dev->idVendor && (idVendor != dev->idVendor || idProduct != dev->idProduct)) 258 - dev++; 259 - 260 - for (ff = dev->ff; *ff >= 0; ff++) 261 - set_bit(*ff, input_dev->ffbit); 262 - 263 - input_dev->upload_effect = hid_lgff_upload_effect; 264 - input_dev->flush = hid_lgff_flush; 265 - 266 - set_bit(EV_FF, input_dev->evbit); 267 - input_dev->ff_effects_max = LGFF_EFFECTS; 268 - } 269 - 270 - static void hid_lgff_exit(struct hid_device* hid) 271 - { 272 - struct lgff_device *lgff = hid->ff_private; 273 - 274 - set_bit(DEVICE_CLOSING, lgff->flags); 275 - del_timer_sync(&lgff->timer); 276 - 277 - hid_lgff_delete_report(lgff->condition); 278 - hid_lgff_delete_report(lgff->rumble); 279 - hid_lgff_delete_report(lgff->constant); 280 - 281 - kfree(lgff); 282 - } 283 - 284 - static int hid_lgff_event(struct hid_device *hid, struct input_dev* input, 285 - unsigned int type, unsigned int code, int value) 286 - { 287 - struct lgff_device *lgff = hid->ff_private; 288 - struct lgff_effect *effect = lgff->effects + code; 289 - unsigned long flags; 290 - 291 - if (type != EV_FF) return -EINVAL; 292 - if (!LGFF_CHECK_OWNERSHIP(code, lgff)) return -EACCES; 293 - if (value < 0) return -EINVAL; 294 - 295 - spin_lock_irqsave(&lgff->lock, flags); 296 - 297 - if (value > 0) { 298 - if (test_bit(EFFECT_STARTED, effect->flags)) { 299 - spin_unlock_irqrestore(&lgff->lock, flags); 300 - return -EBUSY; 301 - } 302 - if (test_bit(EFFECT_PLAYING, effect->flags)) { 303 - spin_unlock_irqrestore(&lgff->lock, flags); 304 - return -EBUSY; 305 - } 306 - 307 - effect->count = value; 308 - 309 - if (effect->effect.replay.delay) { 310 - set_bit(EFFECT_STARTED, effect->flags); 311 - } else { 312 - set_bit(EFFECT_PLAYING, effect->flags); 313 - } 314 - effect->started_at = jiffies; 315 - } 316 - else { /* value == 0 */ 317 - clear_bit(EFFECT_STARTED, effect->flags); 318 - clear_bit(EFFECT_PLAYING, effect->flags); 319 - } 320 - 321 - spin_unlock_irqrestore(&lgff->lock, flags); 322 - 323 - return 0; 324 - 325 - } 326 - 327 - /* Erase all effects this process owns */ 328 - static int hid_lgff_flush(struct input_dev *dev, struct file *file) 329 - { 330 - struct hid_device *hid = dev->private; 331 - struct lgff_device *lgff = hid->ff_private; 332 - int i; 333 - 334 - for (i=0; i<dev->ff_effects_max; ++i) { 335 - 336 - /*NOTE: no need to lock here. The only times EFFECT_USED is 337 - modified is when effects are uploaded or when an effect is 338 - erased. But a process cannot close its dev/input/eventX fd 339 - and perform ioctls on the same fd all at the same time */ 340 - if ( current->pid == lgff->effects[i].owner 341 - && test_bit(EFFECT_USED, lgff->effects[i].flags)) { 342 - 343 - if (hid_lgff_erase(dev, i)) 344 - warn("erase effect %d failed", i); 345 - } 346 - 347 - } 348 - 349 - return 0; 350 - } 351 - 352 - static int hid_lgff_erase(struct input_dev *dev, int id) 353 - { 354 - struct hid_device *hid = dev->private; 355 - struct lgff_device *lgff = hid->ff_private; 356 - unsigned long flags; 357 - 358 - if (!LGFF_CHECK_OWNERSHIP(id, lgff)) return -EACCES; 359 - 360 - spin_lock_irqsave(&lgff->lock, flags); 361 - lgff->effects[id].flags[0] = 0; 362 - spin_unlock_irqrestore(&lgff->lock, flags); 363 - 364 - return 0; 365 - } 366 - 367 - static int hid_lgff_upload_effect(struct input_dev* input, 368 - struct ff_effect* effect) 369 - { 370 - struct hid_device *hid = input->private; 371 - struct lgff_device *lgff = hid->ff_private; 372 - struct lgff_effect new; 373 - int id; 374 - unsigned long flags; 375 - 376 - dbg("ioctl rumble"); 377 - 378 - if (!test_bit(effect->type, input->ffbit)) return -EINVAL; 379 - 380 - spin_lock_irqsave(&lgff->lock, flags); 381 - 382 - if (effect->id == -1) { 383 - int i; 384 - 385 - for (i=0; i<LGFF_EFFECTS && test_bit(EFFECT_USED, lgff->effects[i].flags); ++i); 386 - if (i >= LGFF_EFFECTS) { 387 - spin_unlock_irqrestore(&lgff->lock, flags); 388 - return -ENOSPC; 389 - } 390 - 391 - effect->id = i; 392 - lgff->effects[i].owner = current->pid; 393 - lgff->effects[i].flags[0] = 0; 394 - set_bit(EFFECT_USED, lgff->effects[i].flags); 395 - } 396 - else if (!LGFF_CHECK_OWNERSHIP(effect->id, lgff)) { 397 - spin_unlock_irqrestore(&lgff->lock, flags); 398 - return -EACCES; 399 - } 400 - 401 - id = effect->id; 402 - new = lgff->effects[id]; 403 - 404 - new.effect = *effect; 405 - 406 - if (test_bit(EFFECT_STARTED, lgff->effects[id].flags) 407 - || test_bit(EFFECT_STARTED, lgff->effects[id].flags)) { 408 - 409 - /* Changing replay parameters is not allowed (for the time 410 - being) */ 411 - if (new.effect.replay.delay != lgff->effects[id].effect.replay.delay 412 - || new.effect.replay.length != lgff->effects[id].effect.replay.length) { 413 - spin_unlock_irqrestore(&lgff->lock, flags); 414 - return -ENOSYS; 415 - } 416 - 417 - lgff->effects[id] = new; 418 - 419 - } else { 420 - lgff->effects[id] = new; 421 - } 422 - 423 - spin_unlock_irqrestore(&lgff->lock, flags); 424 - return 0; 425 - } 426 - 427 - static void hid_lgff_timer(unsigned long timer_data) 428 - { 429 - struct lgff_device *lgff = (struct lgff_device*)timer_data; 430 - struct hid_device *hid = lgff->hid; 431 - unsigned long flags; 432 - int x = 0x7f, y = 0x7f; // Coordinates of constant effects 433 - unsigned int left = 0, right = 0; // Rumbling 434 - int i; 435 - 436 - spin_lock_irqsave(&lgff->lock, flags); 437 - 438 - for (i=0; i<LGFF_EFFECTS; ++i) { 439 - struct lgff_effect* effect = lgff->effects +i; 440 - 441 - if (test_bit(EFFECT_PLAYING, effect->flags)) { 442 - 443 - switch (effect->effect.type) { 444 - case FF_CONSTANT: { 445 - //TODO: handle envelopes 446 - int degrees = effect->effect.direction * 360 >> 16; 447 - x += fixp_mult(fixp_sin(degrees), 448 - fixp_new16(effect->effect.u.constant.level)); 449 - y += fixp_mult(-fixp_cos(degrees), 450 - fixp_new16(effect->effect.u.constant.level)); 451 - } break; 452 - case FF_RUMBLE: 453 - right += effect->effect.u.rumble.strong_magnitude; 454 - left += effect->effect.u.rumble.weak_magnitude; 455 - break; 456 - }; 457 - 458 - /* One run of the effect is finished playing */ 459 - if (time_after(jiffies, 460 - effect->started_at 461 - + effect->effect.replay.delay*HZ/1000 462 - + effect->effect.replay.length*HZ/1000)) { 463 - dbg("Finished playing once %d", i); 464 - if (--effect->count <= 0) { 465 - dbg("Stopped %d", i); 466 - clear_bit(EFFECT_PLAYING, effect->flags); 467 - } 468 - else { 469 - dbg("Start again %d", i); 470 - if (effect->effect.replay.length != 0) { 471 - clear_bit(EFFECT_PLAYING, effect->flags); 472 - set_bit(EFFECT_STARTED, effect->flags); 473 - } 474 - effect->started_at = jiffies; 475 - } 476 - } 477 - 478 - } else if (test_bit(EFFECT_STARTED, lgff->effects[i].flags)) { 479 - /* Check if we should start playing the effect */ 480 - if (time_after(jiffies, 481 - lgff->effects[i].started_at 482 - + lgff->effects[i].effect.replay.delay*HZ/1000)) { 483 - dbg("Now playing %d", i); 484 - clear_bit(EFFECT_STARTED, lgff->effects[i].flags); 485 - set_bit(EFFECT_PLAYING, lgff->effects[i].flags); 486 - } 487 - } 488 - } 489 - 490 - #define CLAMP(x) if (x < 0) x = 0; if (x > 0xff) x = 0xff 491 - 492 - // Clamp values 493 - CLAMP(x); 494 - CLAMP(y); 495 - CLAMP(left); 496 - CLAMP(right); 497 - 498 - #undef CLAMP 499 - 500 - if (x != lgff->constant->field[0]->value[2] 501 - || y != lgff->constant->field[0]->value[3]) { 502 - lgff->constant->field[0]->value[2] = x; 503 - lgff->constant->field[0]->value[3] = y; 504 - dbg("(x,y)=(%04x, %04x)", x, y); 505 - hid_submit_report(hid, lgff->constant, USB_DIR_OUT); 506 - } 507 - 508 - if (left != lgff->rumble->field[0]->value[2] 509 - || right != lgff->rumble->field[0]->value[3]) { 510 - lgff->rumble->field[0]->value[2] = left; 511 - lgff->rumble->field[0]->value[3] = right; 512 - dbg("(left,right)=(%04x, %04x)", left, right); 513 - hid_submit_report(hid, lgff->rumble, USB_DIR_OUT); 514 - } 515 - 516 - if (!test_bit(DEVICE_CLOSING, lgff->flags)) { 517 - lgff->timer.expires = RUN_AT(PERIOD); 518 - add_timer(&lgff->timer); 519 - } 520 - 521 - spin_unlock_irqrestore(&lgff->lock, flags); 522 145 }
+43 -360
drivers/usb/input/hid-tmff.c
··· 28 28 */ 29 29 30 30 #include <linux/input.h> 31 - #include <linux/sched.h> 32 31 33 32 #undef DEBUG 34 33 #include <linux/usb.h> 35 34 36 - #include <linux/circ_buf.h> 37 - 38 35 #include "hid.h" 39 - #include "../../input/fixp-arith.h" 40 36 41 37 /* Usages for thrustmaster devices I know about */ 42 38 #define THRUSTMASTER_USAGE_RUMBLE_LR (HID_UP_GENDESK | 0xbb) 43 - #define DELAY_CALC(t,delay) ((t) + (delay)*HZ/1000) 44 39 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 40 77 41 struct tmff_device { 78 - struct hid_device *hid; 79 - 80 42 struct hid_report *report; 81 - 82 43 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 44 }; 95 45 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); 46 + /* Changes values from 0 to 0xffff into values from minimum to maximum */ 47 + static inline int hid_tmff_scale(unsigned int in, int minimum, int maximum) 48 + { 49 + int ret; 104 50 105 - /* Local functions */ 106 - static void hid_tmff_recalculate_timer(struct tmff_device *tmff); 107 - static void hid_tmff_timer(unsigned long timer_data); 51 + ret = (in * (maximum - minimum) / 0xffff) + minimum; 52 + if (ret < minimum) 53 + return minimum; 54 + if (ret > maximum) 55 + return maximum; 56 + return ret; 57 + } 58 + 59 + static int hid_tmff_play(struct input_dev *dev, void *data, struct ff_effect *effect) 60 + { 61 + struct hid_device *hid = dev->private; 62 + struct tmff_device *tmff = data; 63 + int left, right; /* Rumbling */ 64 + 65 + left = hid_tmff_scale(effect->u.rumble.weak_magnitude, 66 + tmff->rumble->logical_minimum, tmff->rumble->logical_maximum); 67 + right = hid_tmff_scale(effect->u.rumble.strong_magnitude, 68 + tmff->rumble->logical_minimum, tmff->rumble->logical_maximum); 69 + 70 + tmff->rumble->value[0] = left; 71 + tmff->rumble->value[1] = right; 72 + dbg("(left,right)=(%08x, %08x)", left, right); 73 + hid_submit_report(hid, tmff->report, USB_DIR_OUT); 74 + 75 + return 0; 76 + } 108 77 109 78 int hid_tmff_init(struct hid_device *hid) 110 79 { 111 - struct tmff_device *private; 80 + struct tmff_device *tmff; 112 81 struct list_head *pos; 113 82 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list); 114 83 struct input_dev *input_dev = hidinput->input; 84 + int error; 115 85 116 - private = kzalloc(sizeof(struct tmff_device), GFP_KERNEL); 117 - if (!private) 86 + tmff = kzalloc(sizeof(struct tmff_device), GFP_KERNEL); 87 + if (!tmff) 118 88 return -ENOMEM; 119 - 120 - hid->ff_private = private; 121 89 122 90 /* Find the report to use */ 123 91 __list_for_each(pos, &hid->report_enum[HID_OUTPUT_REPORT].report_list) { ··· 110 142 continue; 111 143 } 112 144 113 - if (private->report && private->report != report) { 145 + if (tmff->report && tmff->report != report) { 114 146 warn("ignoring THRUSTMASTER_USAGE_RUMBLE_LR in other report"); 115 147 continue; 116 148 } 117 149 118 - if (private->rumble && private->rumble != field) { 150 + if (tmff->rumble && tmff->rumble != field) { 119 151 warn("ignoring duplicate THRUSTMASTER_USAGE_RUMBLE_LR"); 120 152 continue; 121 153 } 122 154 123 - private->report = report; 124 - private->rumble = field; 155 + tmff->report = report; 156 + tmff->rumble = field; 125 157 126 158 set_bit(FF_RUMBLE, input_dev->ffbit); 127 159 break; ··· 130 162 warn("ignoring unknown output usage %08x", field->usage[0].hid); 131 163 continue; 132 164 } 133 - 134 - /* Fallthrough to here only when a valid usage is found */ 135 - input_dev->upload_effect = hid_tmff_upload_effect; 136 - input_dev->flush = hid_tmff_flush; 137 - 138 - set_bit(EV_FF, input_dev->evbit); 139 - input_dev->ff_effects_max = TMFF_EFFECTS; 140 165 } 141 166 } 142 167 143 - private->hid = hid; 144 - 145 - spin_lock_init(&private->lock); 146 - init_timer(&private->timer); 147 - private->timer.data = (unsigned long)private; 148 - private->timer.function = hid_tmff_timer; 149 - 150 - /* Event and exit callbacks */ 151 - hid->ff_exit = hid_tmff_exit; 152 - hid->ff_event = hid_tmff_event; 168 + error = input_ff_create_memless(input_dev, tmff, hid_tmff_play); 169 + if (error) { 170 + kfree(tmff); 171 + return error; 172 + } 153 173 154 174 info("Force feedback for ThrustMaster rumble pad devices by Zinx Verituse <zinx@epicsol.org>"); 155 175 156 176 return 0; 157 177 } 158 178 159 - static void hid_tmff_exit(struct hid_device *hid) 160 - { 161 - struct tmff_device *tmff = hid->ff_private; 162 - unsigned long flags; 163 - 164 - spin_lock_irqsave(&tmff->lock, flags); 165 - 166 - set_bit(DEVICE_CLOSING, tmff->flags); 167 - del_timer_sync(&tmff->timer); 168 - 169 - spin_unlock_irqrestore(&tmff->lock, flags); 170 - 171 - kfree(tmff); 172 - } 173 - 174 - static int hid_tmff_event(struct hid_device *hid, struct input_dev *input, 175 - unsigned int type, unsigned int code, int value) 176 - { 177 - struct tmff_device *tmff = hid->ff_private; 178 - struct tmff_effect *effect = &tmff->effects[code]; 179 - unsigned long flags; 180 - 181 - if (type != EV_FF) 182 - return -EINVAL; 183 - if (!TMFF_CHECK_ID(code)) 184 - return -EINVAL; 185 - if (!TMFF_CHECK_OWNERSHIP(code, tmff)) 186 - return -EACCES; 187 - if (value < 0) 188 - return -EINVAL; 189 - 190 - spin_lock_irqsave(&tmff->lock, flags); 191 - 192 - if (value > 0) { 193 - set_bit(EFFECT_STARTED, effect->flags); 194 - clear_bit(EFFECT_PLAYING, effect->flags); 195 - effect->count = value; 196 - effect->play_at = DELAY_CALC(jiffies, effect->effect.replay.delay); 197 - } else { 198 - clear_bit(EFFECT_STARTED, effect->flags); 199 - clear_bit(EFFECT_PLAYING, effect->flags); 200 - } 201 - 202 - hid_tmff_recalculate_timer(tmff); 203 - 204 - spin_unlock_irqrestore(&tmff->lock, flags); 205 - 206 - return 0; 207 - 208 - } 209 - 210 - /* Erase all effects this process owns */ 211 - 212 - static int hid_tmff_flush(struct input_dev *dev, struct file *file) 213 - { 214 - struct hid_device *hid = dev->private; 215 - struct tmff_device *tmff = hid->ff_private; 216 - int i; 217 - 218 - for (i=0; i<dev->ff_effects_max; ++i) 219 - 220 - /* NOTE: no need to lock here. The only times EFFECT_USED is 221 - modified is when effects are uploaded or when an effect is 222 - erased. But a process cannot close its dev/input/eventX fd 223 - and perform ioctls on the same fd all at the same time */ 224 - 225 - if (current->pid == tmff->effects[i].owner 226 - && test_bit(EFFECT_USED, tmff->effects[i].flags)) 227 - if (hid_tmff_erase(dev, i)) 228 - warn("erase effect %d failed", i); 229 - 230 - 231 - return 0; 232 - } 233 - 234 - static int hid_tmff_erase(struct input_dev *dev, int id) 235 - { 236 - struct hid_device *hid = dev->private; 237 - struct tmff_device *tmff = hid->ff_private; 238 - unsigned long flags; 239 - 240 - if (!TMFF_CHECK_ID(id)) 241 - return -EINVAL; 242 - if (!TMFF_CHECK_OWNERSHIP(id, tmff)) 243 - return -EACCES; 244 - 245 - spin_lock_irqsave(&tmff->lock, flags); 246 - 247 - tmff->effects[id].flags[0] = 0; 248 - hid_tmff_recalculate_timer(tmff); 249 - 250 - spin_unlock_irqrestore(&tmff->lock, flags); 251 - 252 - return 0; 253 - } 254 - 255 - static int hid_tmff_upload_effect(struct input_dev *input, 256 - struct ff_effect *effect) 257 - { 258 - struct hid_device *hid = input->private; 259 - struct tmff_device *tmff = hid->ff_private; 260 - int id; 261 - unsigned long flags; 262 - 263 - if (!test_bit(effect->type, input->ffbit)) 264 - return -EINVAL; 265 - if (effect->id != -1 && !TMFF_CHECK_ID(effect->id)) 266 - return -EINVAL; 267 - 268 - spin_lock_irqsave(&tmff->lock, flags); 269 - 270 - if (effect->id == -1) { 271 - /* Find a free effect */ 272 - for (id = 0; id < TMFF_EFFECTS && test_bit(EFFECT_USED, tmff->effects[id].flags); ++id); 273 - 274 - if (id >= TMFF_EFFECTS) { 275 - spin_unlock_irqrestore(&tmff->lock, flags); 276 - return -ENOSPC; 277 - } 278 - 279 - effect->id = id; 280 - tmff->effects[id].owner = current->pid; 281 - tmff->effects[id].flags[0] = 0; 282 - set_bit(EFFECT_USED, tmff->effects[id].flags); 283 - 284 - } else { 285 - /* Re-uploading an owned effect, to change parameters */ 286 - id = effect->id; 287 - clear_bit(EFFECT_PLAYING, tmff->effects[id].flags); 288 - } 289 - 290 - tmff->effects[id].effect = *effect; 291 - 292 - hid_tmff_recalculate_timer(tmff); 293 - 294 - spin_unlock_irqrestore(&tmff->lock, flags); 295 - return 0; 296 - } 297 - 298 - /* Start the timer for the next start/stop/delay */ 299 - /* Always call this while tmff->lock is locked */ 300 - 301 - static void hid_tmff_recalculate_timer(struct tmff_device *tmff) 302 - { 303 - int i; 304 - int events = 0; 305 - unsigned long next_time; 306 - 307 - next_time = 0; /* Shut up compiler's incorrect warning */ 308 - 309 - /* Find the next change in an effect's status */ 310 - for (i = 0; i < TMFF_EFFECTS; ++i) { 311 - struct tmff_effect *effect = &tmff->effects[i]; 312 - unsigned long play_time; 313 - 314 - if (!test_bit(EFFECT_STARTED, effect->flags)) 315 - continue; 316 - 317 - effect->stop_at = DELAY_CALC(effect->play_at, effect->effect.replay.length); 318 - 319 - if (!test_bit(EFFECT_PLAYING, effect->flags)) 320 - play_time = effect->play_at; 321 - else 322 - play_time = effect->stop_at; 323 - 324 - events++; 325 - 326 - if (time_after(jiffies, play_time)) 327 - play_time = jiffies; 328 - 329 - if (events == 1) 330 - next_time = play_time; 331 - else { 332 - if (time_after(next_time, play_time)) 333 - next_time = play_time; 334 - } 335 - } 336 - 337 - if (!events && tmff->effects_playing) { 338 - /* Treat all effects turning off as an event */ 339 - events = 1; 340 - next_time = jiffies; 341 - } 342 - 343 - if (!events) { 344 - /* No events, no time, no need for a timer. */ 345 - del_timer_sync(&tmff->timer); 346 - return; 347 - } 348 - 349 - mod_timer(&tmff->timer, next_time); 350 - } 351 - 352 - /* Changes values from 0 to 0xffff into values from minimum to maximum */ 353 - static inline int hid_tmff_scale(unsigned int in, int minimum, int maximum) 354 - { 355 - int ret; 356 - 357 - ret = (in * (maximum - minimum) / 0xffff) + minimum; 358 - if (ret < minimum) 359 - return minimum; 360 - if (ret > maximum) 361 - return maximum; 362 - return ret; 363 - } 364 - 365 - static void hid_tmff_timer(unsigned long timer_data) 366 - { 367 - struct tmff_device *tmff = (struct tmff_device *) timer_data; 368 - struct hid_device *hid = tmff->hid; 369 - unsigned long flags; 370 - int left = 0, right = 0; /* Rumbling */ 371 - int i; 372 - 373 - spin_lock_irqsave(&tmff->lock, flags); 374 - 375 - tmff->effects_playing = 0; 376 - 377 - for (i = 0; i < TMFF_EFFECTS; ++i) { 378 - struct tmff_effect *effect = &tmff->effects[i]; 379 - 380 - if (!test_bit(EFFECT_STARTED, effect->flags)) 381 - continue; 382 - 383 - if (!time_after(jiffies, effect->play_at)) 384 - continue; 385 - 386 - if (time_after(jiffies, effect->stop_at)) { 387 - 388 - dbg("Finished playing once %d", i); 389 - clear_bit(EFFECT_PLAYING, effect->flags); 390 - 391 - if (--effect->count <= 0) { 392 - dbg("Stopped %d", i); 393 - clear_bit(EFFECT_STARTED, effect->flags); 394 - continue; 395 - } else { 396 - dbg("Start again %d", i); 397 - effect->play_at = DELAY_CALC(jiffies, effect->effect.replay.delay); 398 - continue; 399 - } 400 - } 401 - 402 - ++tmff->effects_playing; 403 - 404 - set_bit(EFFECT_PLAYING, effect->flags); 405 - 406 - switch (effect->effect.type) { 407 - case FF_RUMBLE: 408 - right += effect->effect.u.rumble.strong_magnitude; 409 - left += effect->effect.u.rumble.weak_magnitude; 410 - break; 411 - default: 412 - BUG(); 413 - break; 414 - } 415 - } 416 - 417 - left = hid_tmff_scale(left, tmff->rumble->logical_minimum, tmff->rumble->logical_maximum); 418 - right = hid_tmff_scale(right, tmff->rumble->logical_minimum, tmff->rumble->logical_maximum); 419 - 420 - if (left != tmff->rumble->value[0] || right != tmff->rumble->value[1]) { 421 - tmff->rumble->value[0] = left; 422 - tmff->rumble->value[1] = right; 423 - dbg("(left,right)=(%08x, %08x)", left, right); 424 - hid_submit_report(hid, tmff->report, USB_DIR_OUT); 425 - } 426 - 427 - if (!test_bit(DEVICE_CLOSING, tmff->flags)) 428 - hid_tmff_recalculate_timer(tmff); 429 - 430 - spin_unlock_irqrestore(&tmff->lock, flags); 431 - }
-19
drivers/usb/input/hid.h
··· 449 449 char phys[64]; /* Device physical location */ 450 450 char uniq[64]; /* Device unique identifier (serial #) */ 451 451 452 - void *ff_private; /* Private data for the force-feedback driver */ 453 - void (*ff_exit)(struct hid_device*); /* Called by hid_exit_ff(hid) */ 454 - int (*ff_event)(struct hid_device *hid, struct input_dev *input, 455 - unsigned int type, unsigned int code, int value); 456 - 457 452 #ifdef CONFIG_USB_HIDINPUT_POWERBOOK 458 453 unsigned long pb_pressed_fn[NBITS(KEY_MAX)]; 459 454 unsigned long pb_pressed_numlock[NBITS(KEY_MAX)]; ··· 534 539 #else 535 540 static inline int hid_ff_init(struct hid_device *hid) { return -1; } 536 541 #endif 537 - 538 - static inline void hid_ff_exit(struct hid_device *hid) 539 - { 540 - if (hid->ff_exit) 541 - hid->ff_exit(hid); 542 - } 543 - 544 - static inline int hid_ff_event(struct hid_device *hid, struct input_dev *input, 545 - unsigned int type, unsigned int code, int value) 546 - { 547 - if (hid->ff_event) 548 - return hid->ff_event(hid, input, type, code, value); 549 - return -ENOSYS; 550 - } 551 542