1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Force feedback driver for USB HID PID compliant devices 4 * 5 * Copyright (c) 2005, 2006 Anssi Hannula <anssi.hannula@gmail.com> 6 * Upgraded 2025 by Oleg Makarenko and Tomasz Pakuła 7 */ 8 9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 10 11 #include "hid-pidff.h" 12 #include <linux/hid.h> 13 #include <linux/input.h> 14 #include <linux/minmax.h> 15 #include <linux/slab.h> 16 #include <linux/usb.h> 17 18 #define PID_EFFECTS_MAX 64 19 #define PID_INFINITE U16_MAX 20 21 /* Linux Force Feedback API uses miliseconds as time unit */ 22 #define FF_TIME_EXPONENT -3 23 #define FF_INFINITE 0 24 25 /* Report usage table used to put reports into an array */ 26 #define PID_SET_EFFECT 0 27 #define PID_EFFECT_OPERATION 1 28 #define PID_DEVICE_GAIN 2 29 #define PID_POOL 3 30 #define PID_BLOCK_LOAD 4 31 #define PID_BLOCK_FREE 5 32 #define PID_DEVICE_CONTROL 6 33 #define PID_CREATE_NEW_EFFECT 7 34 35 #define PID_REQUIRED_REPORTS 8 36 37 #define PID_SET_ENVELOPE 8 38 #define PID_SET_CONDITION 9 39 #define PID_SET_PERIODIC 10 40 #define PID_SET_CONSTANT 11 41 #define PID_SET_RAMP 12 42 static const u8 pidff_reports[] = { 43 0x21, 0x77, 0x7d, 0x7f, 0x89, 0x90, 0x96, 0xab, 44 0x5a, 0x5f, 0x6e, 0x73, 0x74 45 }; 46 /* 47 * device_control is really 0x95, but 0x96 specified 48 * as it is the usage of the only field in that report. 49 */ 50 51 /* PID special fields */ 52 #define PID_EFFECT_TYPE 0x25 53 #define PID_AXES_ENABLE 0x55 54 #define PID_DIRECTION 0x57 55 #define PID_EFFECT_OPERATION_ARRAY 0x78 56 #define PID_BLOCK_LOAD_STATUS 0x8b 57 #define PID_DEVICE_CONTROL_ARRAY 0x96 58 59 /* Value usage tables used to put fields and values into arrays */ 60 #define PID_EFFECT_BLOCK_INDEX 0 61 62 #define PID_DURATION 1 63 #define PID_GAIN 2 64 #define PID_TRIGGER_BUTTON 3 65 #define PID_TRIGGER_REPEAT_INT 4 66 #define PID_DIRECTION_ENABLE 5 67 #define PID_START_DELAY 6 68 static const u8 pidff_set_effect[] = { 69 0x22, 0x50, 0x52, 0x53, 0x54, 0x56, 0xa7 70 }; 71 72 #define PID_ATTACK_LEVEL 1 73 #define PID_ATTACK_TIME 2 74 #define PID_FADE_LEVEL 3 75 #define PID_FADE_TIME 4 76 static const u8 pidff_set_envelope[] = { 0x22, 0x5b, 0x5c, 0x5d, 0x5e }; 77 78 #define PID_PARAM_BLOCK_OFFSET 1 79 #define PID_CP_OFFSET 2 80 #define PID_POS_COEFFICIENT 3 81 #define PID_NEG_COEFFICIENT 4 82 #define PID_POS_SATURATION 5 83 #define PID_NEG_SATURATION 6 84 #define PID_DEAD_BAND 7 85 static const u8 pidff_set_condition[] = { 86 0x22, 0x23, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65 87 }; 88 89 #define PID_MAGNITUDE 1 90 #define PID_OFFSET 2 91 #define PID_PHASE 3 92 #define PID_PERIOD 4 93 static const u8 pidff_set_periodic[] = { 0x22, 0x70, 0x6f, 0x71, 0x72 }; 94 static const u8 pidff_set_constant[] = { 0x22, 0x70 }; 95 96 #define PID_RAMP_START 1 97 #define PID_RAMP_END 2 98 static const u8 pidff_set_ramp[] = { 0x22, 0x75, 0x76 }; 99 100 #define PID_RAM_POOL_AVAILABLE 1 101 static const u8 pidff_block_load[] = { 0x22, 0xac }; 102 103 #define PID_LOOP_COUNT 1 104 static const u8 pidff_effect_operation[] = { 0x22, 0x7c }; 105 106 static const u8 pidff_block_free[] = { 0x22 }; 107 108 #define PID_DEVICE_GAIN_FIELD 0 109 static const u8 pidff_device_gain[] = { 0x7e }; 110 111 #define PID_RAM_POOL_SIZE 0 112 #define PID_SIMULTANEOUS_MAX 1 113 #define PID_DEVICE_MANAGED_POOL 2 114 static const u8 pidff_pool[] = { 0x80, 0x83, 0xa9 }; 115 116 /* Special field key tables used to put special field keys into arrays */ 117 #define PID_ENABLE_ACTUATORS 0 118 #define PID_DISABLE_ACTUATORS 1 119 #define PID_STOP_ALL_EFFECTS 2 120 #define PID_RESET 3 121 #define PID_PAUSE 4 122 #define PID_CONTINUE 5 123 static const u8 pidff_device_control[] = { 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c }; 124 125 #define PID_CONSTANT 0 126 #define PID_RAMP 1 127 #define PID_SQUARE 2 128 #define PID_SINE 3 129 #define PID_TRIANGLE 4 130 #define PID_SAW_UP 5 131 #define PID_SAW_DOWN 6 132 #define PID_SPRING 7 133 #define PID_DAMPER 8 134 #define PID_INERTIA 9 135 #define PID_FRICTION 10 136 static const u8 pidff_effect_types[] = { 137 0x26, 0x27, 0x30, 0x31, 0x32, 0x33, 0x34, 138 0x40, 0x41, 0x42, 0x43 139 }; 140 141 #define PID_BLOCK_LOAD_SUCCESS 0 142 #define PID_BLOCK_LOAD_FULL 1 143 #define PID_BLOCK_LOAD_ERROR 2 144 static const u8 pidff_block_load_status[] = { 0x8c, 0x8d, 0x8e }; 145 146 #define PID_EFFECT_START 0 147 #define PID_EFFECT_STOP 1 148 static const u8 pidff_effect_operation_status[] = { 0x79, 0x7b }; 149 150 #define PID_DIRECTION_NORTH 0x0000 151 #define PID_DIRECTION_EAST 0x4000 152 #define PID_DIRECTION_SOUTH 0x8000 153 #define PID_DIRECTION_WEST 0xc000 154 155 #define PIDFF_FIXED_WHEEL_DIRECTION PID_DIRECTION_EAST 156 157 /* AXES_ENABLE and DIRECTION axes */ 158 enum pid_axes { 159 PID_AXIS_X, 160 PID_AXIS_Y, 161 PID_AXIS_Z, 162 PID_AXIS_RX, 163 PID_AXIS_RY, 164 PID_AXIS_RZ, 165 PID_AXIS_SLIDER, 166 PID_AXIS_DIAL, 167 PID_AXIS_WHEEL, 168 PID_AXES_COUNT, 169 }; 170 static const u8 pidff_direction_axis[] = { 171 HID_USAGE & HID_GD_X, 172 HID_USAGE & HID_GD_Y, 173 HID_USAGE & HID_GD_Z, 174 HID_USAGE & HID_GD_RX, 175 HID_USAGE & HID_GD_RY, 176 HID_USAGE & HID_GD_RZ, 177 HID_USAGE & HID_GD_SLIDER, 178 HID_USAGE & HID_GD_DIAL, 179 HID_USAGE & HID_GD_WHEEL, 180 }; 181 182 struct pidff_usage { 183 struct hid_field *field; 184 s32 *value; 185 }; 186 187 struct pidff_effect { 188 int pid_id; 189 int is_infinite; 190 unsigned int loop_count; 191 }; 192 193 struct pidff_device { 194 struct hid_device *hid; 195 196 struct hid_report *reports[ARRAY_SIZE(pidff_reports)]; 197 198 struct pidff_usage set_effect[ARRAY_SIZE(pidff_set_effect)]; 199 struct pidff_usage set_envelope[ARRAY_SIZE(pidff_set_envelope)]; 200 struct pidff_usage set_condition[ARRAY_SIZE(pidff_set_condition)]; 201 struct pidff_usage set_periodic[ARRAY_SIZE(pidff_set_periodic)]; 202 struct pidff_usage set_constant[ARRAY_SIZE(pidff_set_constant)]; 203 struct pidff_usage set_ramp[ARRAY_SIZE(pidff_set_ramp)]; 204 205 struct pidff_usage device_gain[ARRAY_SIZE(pidff_device_gain)]; 206 struct pidff_usage block_load[ARRAY_SIZE(pidff_block_load)]; 207 struct pidff_usage pool[ARRAY_SIZE(pidff_pool)]; 208 struct pidff_usage effect_operation[ARRAY_SIZE(pidff_effect_operation)]; 209 struct pidff_usage block_free[ARRAY_SIZE(pidff_block_free)]; 210 211 struct pidff_effect effect[PID_EFFECTS_MAX]; 212 213 /* 214 * Special field is a field that is not composed of 215 * usage<->value pairs that pidff_usage values are 216 */ 217 218 /* Special field in create_new_effect */ 219 struct hid_field *create_new_effect_type; 220 221 /* Special fields in set_effect */ 222 struct hid_field *set_effect_type; 223 struct hid_field *effect_direction; 224 struct hid_field *axes_enable; 225 226 /* Special field in device_control */ 227 struct hid_field *device_control; 228 229 /* Special field in block_load */ 230 struct hid_field *block_load_status; 231 232 /* Special field in effect_operation */ 233 struct hid_field *effect_operation_status; 234 235 int control_id[ARRAY_SIZE(pidff_device_control)]; 236 int type_id[ARRAY_SIZE(pidff_effect_types)]; 237 int status_id[ARRAY_SIZE(pidff_block_load_status)]; 238 int operation_id[ARRAY_SIZE(pidff_effect_operation_status)]; 239 int direction_axis_id[ARRAY_SIZE(pidff_direction_axis)]; 240 241 u32 quirks; 242 u8 effect_count; 243 u8 axis_count; 244 }; 245 246 static int pidff_is_effect_conditional(struct ff_effect *effect) 247 { 248 return effect->type == FF_SPRING || 249 effect->type == FF_DAMPER || 250 effect->type == FF_INERTIA || 251 effect->type == FF_FRICTION; 252 } 253 254 static int pidff_is_duration_infinite(u16 duration) 255 { 256 return duration == FF_INFINITE || duration == PID_INFINITE; 257 } 258 259 /* 260 * Get PID effect index from FF effect type. 261 * Return 0 if invalid. 262 */ 263 static int pidff_effect_ff_to_pid(struct ff_effect *effect) 264 { 265 switch (effect->type) { 266 case FF_CONSTANT: 267 return PID_CONSTANT; 268 case FF_RAMP: 269 return PID_RAMP; 270 case FF_SPRING: 271 return PID_SPRING; 272 case FF_DAMPER: 273 return PID_DAMPER; 274 case FF_INERTIA: 275 return PID_INERTIA; 276 case FF_FRICTION: 277 return PID_FRICTION; 278 case FF_PERIODIC: 279 switch (effect->u.periodic.waveform) { 280 case FF_SQUARE: 281 return PID_SQUARE; 282 case FF_TRIANGLE: 283 return PID_TRIANGLE; 284 case FF_SINE: 285 return PID_SINE; 286 case FF_SAW_UP: 287 return PID_SAW_UP; 288 case FF_SAW_DOWN: 289 return PID_SAW_DOWN; 290 } 291 } 292 pr_err("invalid effect type\n"); 293 return -EINVAL; 294 } 295 296 /* 297 * Get effect id in the device descriptor. 298 * Return 0 if invalid. 299 */ 300 static int pidff_get_effect_type_id(struct pidff_device *pidff, 301 struct ff_effect *effect) 302 { 303 int id = pidff_effect_ff_to_pid(effect); 304 305 if (id < 0) 306 return 0; 307 308 if (effect->type == FF_PERIODIC && 309 pidff->quirks & HID_PIDFF_QUIRK_PERIODIC_SINE_ONLY) 310 id = PID_SINE; 311 312 return pidff->type_id[id]; 313 } 314 315 /* 316 * Clamp value for a given field 317 */ 318 static s32 pidff_clamp(s32 i, struct hid_field *field) 319 { 320 return (s32)clamp(i, field->logical_minimum, field->logical_maximum); 321 } 322 323 /* 324 * Scale an unsigned value with range 0..max for the given field 325 */ 326 static int pidff_rescale(int i, int max, struct hid_field *field) 327 { 328 return i * (field->logical_maximum - field->logical_minimum) / max + 329 field->logical_minimum; 330 } 331 332 /* 333 * Scale a signed value in range S16_MIN..S16_MAX for the given field 334 */ 335 static int pidff_rescale_signed(int i, struct hid_field *field) 336 { 337 if (i > 0) 338 return i * field->logical_maximum / S16_MAX; 339 if (i < 0) 340 return i * field->logical_minimum / S16_MIN; 341 return 0; 342 } 343 344 /* 345 * Scale time value from Linux default (ms) to field units 346 */ 347 static u32 pidff_rescale_time(u16 time, struct hid_field *field) 348 { 349 u32 scaled_time = time; 350 int exponent = field->unit_exponent; 351 352 pr_debug("time field exponent: %d\n", exponent); 353 for (; exponent < FF_TIME_EXPONENT; exponent++) 354 scaled_time *= 10; 355 for (; exponent > FF_TIME_EXPONENT; exponent--) 356 scaled_time /= 10; 357 358 pr_debug("time calculated from %d to %d\n", time, scaled_time); 359 return scaled_time; 360 } 361 362 static void pidff_set(struct pidff_usage *usage, u16 value) 363 { 364 usage->value[0] = pidff_rescale(value, U16_MAX, usage->field); 365 pr_debug("calculated from %d to %d\n", value, usage->value[0]); 366 } 367 368 static void pidff_set_signed(struct pidff_usage *usage, s16 value) 369 { 370 if (usage->field->logical_minimum < 0) 371 usage->value[0] = pidff_rescale_signed(value, usage->field); 372 else { 373 if (value < 0) 374 usage->value[0] = 375 pidff_rescale(-value, -S16_MIN, usage->field); 376 else 377 usage->value[0] = 378 pidff_rescale(value, S16_MAX, usage->field); 379 } 380 pr_debug("calculated from %d to %d\n", value, usage->value[0]); 381 } 382 383 static void pidff_set_time(struct pidff_usage *usage, u16 time) 384 { 385 usage->value[0] = pidff_clamp(pidff_rescale_time(time, usage->field), 386 usage->field); 387 } 388 389 static void pidff_set_duration(struct pidff_usage *usage, u16 duration) 390 { 391 /* PID defines INFINITE as the max possible value for duration field */ 392 if (pidff_is_duration_infinite(duration)) { 393 usage->value[0] = (1U << usage->field->report_size) - 1; 394 return; 395 } 396 397 pidff_set_time(usage, duration); 398 } 399 400 static void pidff_set_effect_direction(struct pidff_device *pidff, 401 struct ff_effect *effect) 402 { 403 u16 direction = effect->direction; 404 int direction_enable = 1; 405 406 /* Use fixed direction if needed */ 407 if (pidff->quirks & HID_PIDFF_QUIRK_FIX_CONDITIONAL_DIRECTION && 408 pidff_is_effect_conditional(effect)) 409 direction = PIDFF_FIXED_WHEEL_DIRECTION; 410 411 pidff->set_effect[PID_DIRECTION_ENABLE].value[0] = direction_enable; 412 pidff->effect_direction->value[0] = 413 pidff_rescale(direction, U16_MAX, pidff->effect_direction); 414 415 if (direction_enable) 416 return; 417 418 /* 419 * For use with improved FFB API 420 * We want to read the selected axes and their direction from the effect 421 * struct and only enable those. For now, enable all axes. 422 * 423 */ 424 for (int i = 0; i < PID_AXES_COUNT; i++) { 425 /* HID index starts with 1 */ 426 int index = pidff->direction_axis_id[i] - 1; 427 428 if (index < 0) 429 continue; 430 431 pidff->axes_enable->value[index] = 1; 432 pidff->effect_direction->value[index] = pidff_rescale( 433 direction, U16_MAX, pidff->effect_direction); 434 } 435 } 436 437 /* 438 * Send envelope report to the device 439 */ 440 static void pidff_set_envelope_report(struct pidff_device *pidff, 441 struct ff_envelope *envelope) 442 { 443 pidff->set_envelope[PID_EFFECT_BLOCK_INDEX].value[0] = 444 pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0]; 445 446 pidff->set_envelope[PID_ATTACK_LEVEL].value[0] = 447 pidff_rescale(envelope->attack_level > 448 S16_MAX ? S16_MAX : envelope->attack_level, S16_MAX, 449 pidff->set_envelope[PID_ATTACK_LEVEL].field); 450 pidff->set_envelope[PID_FADE_LEVEL].value[0] = 451 pidff_rescale(envelope->fade_level > 452 S16_MAX ? S16_MAX : envelope->fade_level, S16_MAX, 453 pidff->set_envelope[PID_FADE_LEVEL].field); 454 455 pidff_set_time(&pidff->set_envelope[PID_ATTACK_TIME], 456 envelope->attack_length); 457 pidff_set_time(&pidff->set_envelope[PID_FADE_TIME], 458 envelope->fade_length); 459 460 hid_hw_request(pidff->hid, pidff->reports[PID_SET_ENVELOPE], 461 HID_REQ_SET_REPORT); 462 } 463 464 /* 465 * Test if the new envelope differs from old one 466 */ 467 static int pidff_needs_set_envelope(struct ff_envelope *envelope, 468 struct ff_envelope *old) 469 { 470 int needs_new_envelope; 471 472 needs_new_envelope = envelope->attack_level != 0 || 473 envelope->fade_level != 0 || 474 envelope->attack_length != 0 || 475 envelope->fade_length != 0; 476 477 if (!needs_new_envelope) 478 return 0; 479 if (!old) 480 return needs_new_envelope; 481 482 return envelope->attack_level != old->attack_level || 483 envelope->fade_level != old->fade_level || 484 envelope->attack_length != old->attack_length || 485 envelope->fade_length != old->fade_length; 486 } 487 488 /* 489 * Send constant force report to the device 490 */ 491 static void pidff_set_constant_report(struct pidff_device *pidff, 492 struct ff_effect *effect) 493 { 494 pidff->set_constant[PID_EFFECT_BLOCK_INDEX].value[0] = 495 pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0]; 496 pidff_set_signed(&pidff->set_constant[PID_MAGNITUDE], 497 effect->u.constant.level); 498 499 hid_hw_request(pidff->hid, pidff->reports[PID_SET_CONSTANT], 500 HID_REQ_SET_REPORT); 501 } 502 503 /* 504 * Test if the constant parameters have changed between effects 505 */ 506 static int pidff_needs_set_constant(struct ff_effect *effect, 507 struct ff_effect *old) 508 { 509 return effect->u.constant.level != old->u.constant.level; 510 } 511 512 /* 513 * Send set effect report to the device 514 */ 515 static void pidff_set_effect_report(struct pidff_device *pidff, 516 struct ff_effect *effect) 517 { 518 pidff->set_effect[PID_EFFECT_BLOCK_INDEX].value[0] = 519 pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0]; 520 pidff->set_effect_type->value[0] = 521 pidff->create_new_effect_type->value[0]; 522 523 pidff_set_duration(&pidff->set_effect[PID_DURATION], 524 effect->replay.length); 525 526 /* Some games set this to random values that can be out of range */ 527 s32 trigger_button_max = 528 pidff->set_effect[PID_TRIGGER_BUTTON].field->logical_maximum; 529 if (effect->trigger.button <= trigger_button_max) { 530 pidff->set_effect[PID_TRIGGER_BUTTON].value[0] = 531 effect->trigger.button; 532 pidff_set_time(&pidff->set_effect[PID_TRIGGER_REPEAT_INT], 533 effect->trigger.interval); 534 } else { 535 pidff->set_effect[PID_TRIGGER_BUTTON].value[0] = 0; 536 pidff->set_effect[PID_TRIGGER_REPEAT_INT].value[0] = 0; 537 } 538 539 pidff->set_effect[PID_GAIN].value[0] = 540 pidff->set_effect[PID_GAIN].field->logical_maximum; 541 542 pidff_set_effect_direction(pidff, effect); 543 544 /* Omit setting delay field if it's missing */ 545 if (!(pidff->quirks & HID_PIDFF_QUIRK_MISSING_DELAY)) 546 pidff_set_time(&pidff->set_effect[PID_START_DELAY], 547 effect->replay.delay); 548 549 hid_hw_request(pidff->hid, pidff->reports[PID_SET_EFFECT], 550 HID_REQ_SET_REPORT); 551 } 552 553 /* 554 * Test if the values used in set_effect have changed 555 */ 556 static int pidff_needs_set_effect(struct ff_effect *effect, 557 struct ff_effect *old) 558 { 559 return effect->replay.length != old->replay.length || 560 effect->trigger.interval != old->trigger.interval || 561 effect->trigger.button != old->trigger.button || 562 effect->direction != old->direction || 563 effect->replay.delay != old->replay.delay; 564 } 565 566 /* 567 * Send periodic effect report to the device 568 */ 569 static void pidff_set_periodic_report(struct pidff_device *pidff, 570 struct ff_effect *effect) 571 { 572 pidff->set_periodic[PID_EFFECT_BLOCK_INDEX].value[0] = 573 pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0]; 574 pidff_set_signed(&pidff->set_periodic[PID_MAGNITUDE], 575 effect->u.periodic.magnitude); 576 pidff_set_signed(&pidff->set_periodic[PID_OFFSET], 577 effect->u.periodic.offset); 578 pidff_set(&pidff->set_periodic[PID_PHASE], effect->u.periodic.phase); 579 pidff_set_time(&pidff->set_periodic[PID_PERIOD], 580 effect->u.periodic.period); 581 582 hid_hw_request(pidff->hid, pidff->reports[PID_SET_PERIODIC], 583 HID_REQ_SET_REPORT); 584 } 585 586 /* 587 * Test if periodic effect parameters have changed 588 */ 589 static int pidff_needs_set_periodic(struct ff_effect *effect, 590 struct ff_effect *old) 591 { 592 return effect->u.periodic.magnitude != old->u.periodic.magnitude || 593 effect->u.periodic.offset != old->u.periodic.offset || 594 effect->u.periodic.phase != old->u.periodic.phase || 595 effect->u.periodic.period != old->u.periodic.period; 596 } 597 598 /* 599 * Send condition effect reports to the device 600 */ 601 static void pidff_set_condition_report(struct pidff_device *pidff, 602 struct ff_effect *effect) 603 { 604 int i, max_axis; 605 606 /* Devices missing Parameter Block Offset can only have one axis */ 607 max_axis = pidff->quirks & HID_PIDFF_QUIRK_MISSING_PBO ? 1 : 2; 608 609 pidff->set_condition[PID_EFFECT_BLOCK_INDEX].value[0] = 610 pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0]; 611 612 for (i = 0; i < max_axis; i++) { 613 /* Omit Parameter Block Offset if missing */ 614 if (!(pidff->quirks & HID_PIDFF_QUIRK_MISSING_PBO)) 615 pidff->set_condition[PID_PARAM_BLOCK_OFFSET].value[0] = i; 616 617 pidff_set_signed(&pidff->set_condition[PID_CP_OFFSET], 618 effect->u.condition[i].center); 619 pidff_set_signed(&pidff->set_condition[PID_POS_COEFFICIENT], 620 effect->u.condition[i].right_coeff); 621 pidff_set_signed(&pidff->set_condition[PID_NEG_COEFFICIENT], 622 effect->u.condition[i].left_coeff); 623 pidff_set(&pidff->set_condition[PID_POS_SATURATION], 624 effect->u.condition[i].right_saturation); 625 pidff_set(&pidff->set_condition[PID_NEG_SATURATION], 626 effect->u.condition[i].left_saturation); 627 pidff_set(&pidff->set_condition[PID_DEAD_BAND], 628 effect->u.condition[i].deadband); 629 hid_hw_request(pidff->hid, pidff->reports[PID_SET_CONDITION], 630 HID_REQ_SET_REPORT); 631 } 632 } 633 634 /* 635 * Test if condition effect parameters have changed 636 */ 637 static int pidff_needs_set_condition(struct ff_effect *effect, 638 struct ff_effect *old) 639 { 640 int i; 641 int ret = 0; 642 643 for (i = 0; i < 2; i++) { 644 struct ff_condition_effect *cond = &effect->u.condition[i]; 645 struct ff_condition_effect *old_cond = &old->u.condition[i]; 646 647 ret |= cond->center != old_cond->center || 648 cond->right_coeff != old_cond->right_coeff || 649 cond->left_coeff != old_cond->left_coeff || 650 cond->right_saturation != old_cond->right_saturation || 651 cond->left_saturation != old_cond->left_saturation || 652 cond->deadband != old_cond->deadband; 653 } 654 655 return ret; 656 } 657 658 /* 659 * Send ramp force report to the device 660 */ 661 static void pidff_set_ramp_report(struct pidff_device *pidff, 662 struct ff_effect *effect) 663 { 664 pidff->set_ramp[PID_EFFECT_BLOCK_INDEX].value[0] = 665 pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0]; 666 pidff_set_signed(&pidff->set_ramp[PID_RAMP_START], 667 effect->u.ramp.start_level); 668 pidff_set_signed(&pidff->set_ramp[PID_RAMP_END], 669 effect->u.ramp.end_level); 670 hid_hw_request(pidff->hid, pidff->reports[PID_SET_RAMP], 671 HID_REQ_SET_REPORT); 672 } 673 674 /* 675 * Test if ramp force parameters have changed 676 */ 677 static int pidff_needs_set_ramp(struct ff_effect *effect, struct ff_effect *old) 678 { 679 return effect->u.ramp.start_level != old->u.ramp.start_level || 680 effect->u.ramp.end_level != old->u.ramp.end_level; 681 } 682 683 /* 684 * Set device gain 685 */ 686 static void pidff_set_gain_report(struct pidff_device *pidff, u16 gain) 687 { 688 if (!pidff->device_gain[PID_DEVICE_GAIN_FIELD].field) 689 return; 690 691 pidff_set(&pidff->device_gain[PID_DEVICE_GAIN_FIELD], gain); 692 hid_hw_request(pidff->hid, pidff->reports[PID_DEVICE_GAIN], 693 HID_REQ_SET_REPORT); 694 } 695 696 /* 697 * Send device control report to the device 698 */ 699 static void pidff_set_device_control(struct pidff_device *pidff, int field) 700 { 701 const int field_index = pidff->control_id[field]; 702 703 if (field_index < 1) 704 return; 705 706 /* Detect if the field is a bitmask variable or an array */ 707 if (pidff->device_control->flags & HID_MAIN_ITEM_VARIABLE) { 708 hid_dbg(pidff->hid, "DEVICE_CONTROL is a bitmask\n"); 709 710 /* Clear current bitmask */ 711 for (int i = 0; i < ARRAY_SIZE(pidff_device_control); i++) { 712 int index = pidff->control_id[i]; 713 714 if (index < 1) 715 continue; 716 717 pidff->device_control->value[index - 1] = 0; 718 } 719 720 pidff->device_control->value[field_index - 1] = 1; 721 } else { 722 hid_dbg(pidff->hid, "DEVICE_CONTROL is an array\n"); 723 pidff->device_control->value[0] = field_index; 724 } 725 726 hid_hw_request(pidff->hid, pidff->reports[PID_DEVICE_CONTROL], HID_REQ_SET_REPORT); 727 hid_hw_wait(pidff->hid); 728 hid_dbg(pidff->hid, "Device control command 0x%02x sent", 729 pidff_device_control[field]); 730 } 731 732 /* 733 * Reset the device, stop all effects, enable actuators 734 */ 735 static void pidff_reset(struct pidff_device *pidff) 736 { 737 /* We reset twice as sometimes hid_wait_io isn't waiting long enough */ 738 pidff_set_device_control(pidff, PID_RESET); 739 pidff_set_device_control(pidff, PID_RESET); 740 pidff->effect_count = 0; 741 742 pidff_set_device_control(pidff, PID_STOP_ALL_EFFECTS); 743 pidff_set_device_control(pidff, PID_ENABLE_ACTUATORS); 744 } 745 746 /* 747 * Fetch pool report 748 */ 749 static void pidff_fetch_pool(struct pidff_device *pidff) 750 { 751 int i; 752 struct hid_device *hid = pidff->hid; 753 754 /* Repeat if PID_SIMULTANEOUS_MAX < 2 to make sure it's correct */ 755 for (i = 0; i < 20; i++) { 756 hid_hw_request(hid, pidff->reports[PID_POOL], HID_REQ_GET_REPORT); 757 hid_hw_wait(hid); 758 759 if (!pidff->pool[PID_SIMULTANEOUS_MAX].value) 760 return; 761 if (pidff->pool[PID_SIMULTANEOUS_MAX].value[0] >= 2) 762 return; 763 } 764 hid_warn(hid, "device reports %d simultaneous effects\n", 765 pidff->pool[PID_SIMULTANEOUS_MAX].value[0]); 766 } 767 768 /* 769 * Send a request for effect upload to the device 770 * 771 * Reset and enable actuators if no effects were present on the device 772 * 773 * Returns 0 if device reported success, -ENOSPC if the device reported memory 774 * is full. Upon unknown response the function will retry for 60 times, if 775 * still unsuccessful -EIO is returned. 776 */ 777 static int pidff_request_effect_upload(struct pidff_device *pidff, int efnum) 778 { 779 pidff->create_new_effect_type->value[0] = efnum; 780 hid_hw_request(pidff->hid, pidff->reports[PID_CREATE_NEW_EFFECT], 781 HID_REQ_SET_REPORT); 782 hid_dbg(pidff->hid, "create_new_effect sent, type: %d\n", efnum); 783 784 pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0] = 0; 785 pidff->block_load_status->value[0] = 0; 786 hid_hw_wait(pidff->hid); 787 788 for (int i = 0; i < 60; i++) { 789 hid_dbg(pidff->hid, "pid_block_load requested\n"); 790 hid_hw_request(pidff->hid, pidff->reports[PID_BLOCK_LOAD], 791 HID_REQ_GET_REPORT); 792 hid_hw_wait(pidff->hid); 793 if (pidff->block_load_status->value[0] == 794 pidff->status_id[PID_BLOCK_LOAD_SUCCESS]) { 795 hid_dbg(pidff->hid, "device reported free memory: %d bytes\n", 796 pidff->block_load[PID_RAM_POOL_AVAILABLE].value ? 797 pidff->block_load[PID_RAM_POOL_AVAILABLE].value[0] : -1); 798 return 0; 799 } 800 if (pidff->block_load_status->value[0] == 801 pidff->status_id[PID_BLOCK_LOAD_FULL]) { 802 hid_dbg(pidff->hid, "not enough memory free: %d bytes\n", 803 pidff->block_load[PID_RAM_POOL_AVAILABLE].value ? 804 pidff->block_load[PID_RAM_POOL_AVAILABLE].value[0] : -1); 805 return -ENOSPC; 806 } 807 if (pidff->block_load_status->value[0] == 808 pidff->status_id[PID_BLOCK_LOAD_ERROR]) { 809 hid_dbg(pidff->hid, "device error during effect creation\n"); 810 return -EREMOTEIO; 811 } 812 } 813 hid_err(pidff->hid, "pid_block_load failed 60 times\n"); 814 return -EIO; 815 } 816 817 static int pidff_needs_playback(struct pidff_device *pidff, int effect_id, int n) 818 { 819 return !pidff->effect[effect_id].is_infinite || 820 pidff->effect[effect_id].loop_count != n; 821 } 822 823 /* 824 * Play the effect with PID id n times 825 */ 826 static void pidff_playback_pid(struct pidff_device *pidff, int pid_id, int n) 827 { 828 pidff->effect_operation[PID_EFFECT_BLOCK_INDEX].value[0] = pid_id; 829 830 hid_dbg(pidff->hid, "%s PID effect %d", n == 0 ? "stopping" : "playing", 831 pid_id); 832 833 if (n == 0) { 834 pidff->effect_operation_status->value[0] = 835 pidff->operation_id[PID_EFFECT_STOP]; 836 } else { 837 pidff->effect_operation_status->value[0] = 838 pidff->operation_id[PID_EFFECT_START]; 839 pidff->effect_operation[PID_LOOP_COUNT].value[0] = 840 pidff_clamp(n, pidff->effect_operation[PID_LOOP_COUNT].field); 841 } 842 843 hid_hw_request(pidff->hid, pidff->reports[PID_EFFECT_OPERATION], 844 HID_REQ_SET_REPORT); 845 } 846 847 /* 848 * Play the effect with effect id @effect_id for @value times 849 */ 850 static int pidff_playback(struct input_dev *dev, int effect_id, int value) 851 { 852 struct pidff_device *pidff = dev->ff->private; 853 854 if (!pidff_needs_playback(pidff, effect_id, value)) 855 return 0; 856 857 hid_dbg(pidff->hid, "requesting %s on FF effect %d", 858 value == 0 ? "stop" : "playback", effect_id); 859 860 pidff->effect[effect_id].loop_count = value; 861 pidff_playback_pid(pidff, pidff->effect[effect_id].pid_id, value); 862 return 0; 863 } 864 865 /* 866 * Erase effect with PID id 867 * Decrease the device effect counter 868 */ 869 static void pidff_erase_pid(struct pidff_device *pidff, int pid_id) 870 { 871 pidff->block_free[PID_EFFECT_BLOCK_INDEX].value[0] = pid_id; 872 hid_hw_request(pidff->hid, pidff->reports[PID_BLOCK_FREE], 873 HID_REQ_SET_REPORT); 874 } 875 876 /* 877 * Stop and erase effect with effect_id 878 */ 879 static int pidff_erase_effect(struct input_dev *dev, int effect_id) 880 { 881 struct pidff_device *pidff = dev->ff->private; 882 int pid_id = pidff->effect[effect_id].pid_id; 883 884 hid_dbg(pidff->hid, "starting to erase %d/%d\n", effect_id, pid_id); 885 886 /* 887 * Wait for the queue to clear. We do not want 888 * a full fifo to prevent the effect removal. 889 */ 890 hid_hw_wait(pidff->hid); 891 pidff_playback_pid(pidff, pid_id, 0); 892 pidff_erase_pid(pidff, pid_id); 893 894 if (pidff->effect_count > 0) 895 pidff->effect_count--; 896 897 hid_dbg(pidff->hid, "current effect count: %d", pidff->effect_count); 898 return 0; 899 } 900 901 #define PIDFF_SET_REPORT_IF_NEEDED(type, effect, old) \ 902 ({ if (!old || pidff_needs_set_## type(effect, old)) \ 903 pidff_set_ ##type## _report(pidff, effect); }) 904 905 #define PIDFF_SET_ENVELOPE_IF_NEEDED(type, effect, old) \ 906 ({ if (pidff_needs_set_envelope(&effect->u.type.envelope, \ 907 old ? &old->u.type.envelope : NULL)) \ 908 pidff_set_envelope_report(pidff, &effect->u.type.envelope); }) 909 910 /* 911 * Effect upload handler 912 */ 913 static int pidff_upload_effect(struct input_dev *dev, struct ff_effect *new, 914 struct ff_effect *old) 915 { 916 struct pidff_device *pidff = dev->ff->private; 917 const int type_id = pidff_get_effect_type_id(pidff, new); 918 919 if (!type_id) { 920 hid_err(pidff->hid, "effect type not supported\n"); 921 return -EINVAL; 922 } 923 924 if (!pidff->effect_count) 925 pidff_reset(pidff); 926 927 if (!old) { 928 int error = pidff_request_effect_upload(pidff, type_id); 929 930 if (error) 931 return error; 932 933 pidff->effect_count++; 934 hid_dbg(pidff->hid, "current effect count: %d", pidff->effect_count); 935 pidff->effect[new->id].loop_count = 0; 936 pidff->effect[new->id].pid_id = 937 pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0]; 938 } 939 940 pidff->effect[new->id].is_infinite = 941 pidff_is_duration_infinite(new->replay.length); 942 943 pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0] = 944 pidff->effect[new->id].pid_id; 945 946 PIDFF_SET_REPORT_IF_NEEDED(effect, new, old); 947 switch (new->type) { 948 case FF_CONSTANT: 949 PIDFF_SET_REPORT_IF_NEEDED(constant, new, old); 950 PIDFF_SET_ENVELOPE_IF_NEEDED(constant, new, old); 951 break; 952 953 case FF_PERIODIC: 954 PIDFF_SET_REPORT_IF_NEEDED(periodic, new, old); 955 PIDFF_SET_ENVELOPE_IF_NEEDED(periodic, new, old); 956 break; 957 958 case FF_RAMP: 959 PIDFF_SET_REPORT_IF_NEEDED(ramp, new, old); 960 PIDFF_SET_ENVELOPE_IF_NEEDED(ramp, new, old); 961 break; 962 963 case FF_SPRING: 964 case FF_DAMPER: 965 case FF_INERTIA: 966 case FF_FRICTION: 967 PIDFF_SET_REPORT_IF_NEEDED(condition, new, old); 968 break; 969 } 970 hid_dbg(pidff->hid, "uploaded\n"); 971 return 0; 972 } 973 974 /* 975 * set_gain() handler 976 */ 977 static void pidff_set_gain(struct input_dev *dev, u16 gain) 978 { 979 pidff_set_gain_report(dev->ff->private, gain); 980 } 981 982 static void pidff_autocenter(struct pidff_device *pidff, u16 magnitude) 983 { 984 struct hid_field *field = 985 pidff->block_load[PID_EFFECT_BLOCK_INDEX].field; 986 987 if (!magnitude) { 988 pidff_playback_pid(pidff, field->logical_minimum, 0); 989 return; 990 } 991 992 pidff_playback_pid(pidff, field->logical_minimum, 1); 993 994 pidff->set_effect[PID_EFFECT_BLOCK_INDEX].value[0] = 995 pidff->block_load[PID_EFFECT_BLOCK_INDEX].field->logical_minimum; 996 pidff->set_effect_type->value[0] = pidff->type_id[PID_SPRING]; 997 pidff->set_effect[PID_DURATION].value[0] = 0; 998 pidff->set_effect[PID_TRIGGER_BUTTON].value[0] = 0; 999 pidff->set_effect[PID_TRIGGER_REPEAT_INT].value[0] = 0; 1000 pidff_set(&pidff->set_effect[PID_GAIN], magnitude); 1001 pidff->set_effect[PID_DIRECTION_ENABLE].value[0] = 1; 1002 1003 /* Omit setting delay field if it's missing */ 1004 if (!(pidff->quirks & HID_PIDFF_QUIRK_MISSING_DELAY)) 1005 pidff->set_effect[PID_START_DELAY].value[0] = 0; 1006 1007 hid_hw_request(pidff->hid, pidff->reports[PID_SET_EFFECT], 1008 HID_REQ_SET_REPORT); 1009 } 1010 1011 /* 1012 * pidff_set_autocenter() handler 1013 */ 1014 static void pidff_set_autocenter(struct input_dev *dev, u16 magnitude) 1015 { 1016 pidff_autocenter(dev->ff->private, magnitude); 1017 } 1018 1019 /* 1020 * Find specific usage in a given hid_field 1021 */ 1022 static int pidff_find_usage(struct hid_field *fld, unsigned int usage_code) 1023 { 1024 for (int i = 0; i < fld->maxusage; i++) { 1025 if (fld->usage[i].hid == usage_code) 1026 return i; 1027 } 1028 return -1; 1029 } 1030 1031 /* 1032 * Find hid_field with a specific usage. Return the usage index as well 1033 */ 1034 static int pidff_find_field_with_usage(int *usage_index, 1035 struct hid_report *report, 1036 unsigned int usage_code) 1037 { 1038 for (int i = 0; i < report->maxfield; i++) { 1039 struct hid_field *fld = report->field[i]; 1040 1041 if (fld->maxusage != fld->report_count) { 1042 pr_debug("maxusage and report_count do not match, skipping\n"); 1043 continue; 1044 } 1045 1046 int index = pidff_find_usage(fld, usage_code); 1047 1048 if (index >= 0) { 1049 *usage_index = index; 1050 return i; 1051 } 1052 } 1053 return -1; 1054 } 1055 1056 /* 1057 * Find fields from a report and fill a pidff_usage 1058 */ 1059 static int pidff_find_fields(struct pidff_usage *usage, const u8 *table, 1060 struct hid_report *report, int count, int strict, 1061 u32 *quirks) 1062 { 1063 const u8 block_offset = pidff_set_condition[PID_PARAM_BLOCK_OFFSET]; 1064 const u8 delay = pidff_set_effect[PID_START_DELAY]; 1065 1066 if (!report) { 1067 pr_debug("%s, null report\n", __func__); 1068 return -1; 1069 } 1070 1071 for (int i = 0; i < count; i++) { 1072 int index; 1073 int found = pidff_find_field_with_usage(&index, report, 1074 HID_UP_PID | table[i]); 1075 1076 if (found >= 0) { 1077 pr_debug("found %d at %d->%d\n", i, found, index); 1078 usage[i].field = report->field[found]; 1079 usage[i].value = &report->field[found]->value[index]; 1080 continue; 1081 } 1082 1083 if (table[i] == delay) { 1084 pr_debug("Delay field not found, but that's OK\n"); 1085 pr_debug("Setting MISSING_DELAY quirk\n"); 1086 *quirks |= HID_PIDFF_QUIRK_MISSING_DELAY; 1087 1088 } else if (table[i] == block_offset) { 1089 pr_debug("PBO field not found, but that's OK\n"); 1090 pr_debug("Setting MISSING_PBO quirk\n"); 1091 *quirks |= HID_PIDFF_QUIRK_MISSING_PBO; 1092 1093 } else if (strict) { 1094 pr_debug("failed to locate %d\n", i); 1095 return -1; 1096 } 1097 } 1098 return 0; 1099 } 1100 1101 /* 1102 * Return index into pidff_reports for the given usage 1103 */ 1104 static int pidff_check_usage(int usage) 1105 { 1106 int i; 1107 1108 for (i = 0; i < ARRAY_SIZE(pidff_reports); i++) 1109 if (usage == (HID_UP_PID | pidff_reports[i])) 1110 return i; 1111 1112 return -1; 1113 } 1114 1115 /* 1116 * Find the reports and fill pidff->reports[] 1117 * report_type specifies either OUTPUT or FEATURE reports 1118 */ 1119 static void pidff_find_reports(struct hid_device *hid, int report_type, 1120 struct pidff_device *pidff) 1121 { 1122 struct hid_report *report; 1123 int i, ret; 1124 1125 list_for_each_entry(report, 1126 &hid->report_enum[report_type].report_list, list) { 1127 if (report->maxfield < 1) 1128 continue; 1129 ret = pidff_check_usage(report->field[0]->logical); 1130 if (ret != -1) { 1131 hid_dbg(hid, "found usage 0x%02x from field->logical\n", 1132 pidff_reports[ret]); 1133 pidff->reports[ret] = report; 1134 continue; 1135 } 1136 1137 /* 1138 * Sometimes logical collections are stacked to indicate 1139 * different usages for the report and the field, in which 1140 * case we want the usage of the parent. However, Linux HID 1141 * implementation hides this fact, so we have to dig it up 1142 * ourselves 1143 */ 1144 i = report->field[0]->usage[0].collection_index; 1145 if (i <= 0 || 1146 hid->collection[i - 1].type != HID_COLLECTION_LOGICAL) 1147 continue; 1148 ret = pidff_check_usage(hid->collection[i - 1].usage); 1149 if (ret != -1 && !pidff->reports[ret]) { 1150 hid_dbg(hid, 1151 "found usage 0x%02x from collection array\n", 1152 pidff_reports[ret]); 1153 pidff->reports[ret] = report; 1154 } 1155 } 1156 } 1157 1158 /* 1159 * Test if the required reports have been found 1160 */ 1161 static int pidff_reports_ok(struct pidff_device *pidff) 1162 { 1163 for (int i = 0; i < PID_REQUIRED_REPORTS; i++) { 1164 if (!pidff->reports[i]) { 1165 hid_dbg(pidff->hid, "%d missing\n", i); 1166 return 0; 1167 } 1168 } 1169 1170 return 1; 1171 } 1172 1173 /* 1174 * Find a field with a specific usage within a report 1175 */ 1176 static struct hid_field *pidff_find_special_field(struct hid_report *report, 1177 int usage, int enforce_min) 1178 { 1179 if (!report) { 1180 pr_debug("%s, null report\n", __func__); 1181 return NULL; 1182 } 1183 1184 for (int i = 0; i < report->maxfield; i++) { 1185 if (report->field[i]->logical == (HID_UP_PID | usage) && 1186 report->field[i]->report_count > 0) { 1187 if (!enforce_min || 1188 report->field[i]->logical_minimum == 1) 1189 return report->field[i]; 1190 1191 pr_err("logical_minimum is not 1 as it should be\n"); 1192 return NULL; 1193 } 1194 } 1195 return NULL; 1196 } 1197 1198 /* 1199 * Fill a pidff->*_id struct table 1200 */ 1201 static int pidff_find_special_keys(int *keys, struct hid_field *fld, 1202 const u8 *usagetable, int count, 1203 unsigned int usage_page) 1204 { 1205 int found = 0; 1206 1207 if (!fld) 1208 return 0; 1209 1210 for (int i = 0; i < count; i++) { 1211 keys[i] = pidff_find_usage(fld, usage_page | usagetable[i]) + 1; 1212 if (keys[i]) 1213 found++; 1214 } 1215 return found; 1216 } 1217 1218 #define PIDFF_FIND_SPECIAL_KEYS(keys, field, name) \ 1219 pidff_find_special_keys(pidff->keys, pidff->field, pidff_ ## name, \ 1220 ARRAY_SIZE(pidff_ ## name), HID_UP_PID) 1221 1222 #define PIDFF_FIND_GENERAL_DESKTOP(keys, field, name) \ 1223 pidff_find_special_keys(pidff->keys, pidff->field, pidff_ ## name, \ 1224 ARRAY_SIZE(pidff_ ## name), HID_UP_GENDESK) 1225 1226 /* 1227 * Find and check the special fields 1228 */ 1229 static int pidff_find_special_fields(struct pidff_device *pidff) 1230 { 1231 hid_dbg(pidff->hid, "finding special fields\n"); 1232 1233 pidff->create_new_effect_type = 1234 pidff_find_special_field(pidff->reports[PID_CREATE_NEW_EFFECT], 1235 PID_EFFECT_TYPE, 1); 1236 pidff->set_effect_type = 1237 pidff_find_special_field(pidff->reports[PID_SET_EFFECT], 1238 PID_EFFECT_TYPE, 1); 1239 pidff->axes_enable = 1240 pidff_find_special_field(pidff->reports[PID_SET_EFFECT], 1241 PID_AXES_ENABLE, 0); 1242 pidff->effect_direction = 1243 pidff_find_special_field(pidff->reports[PID_SET_EFFECT], 1244 PID_DIRECTION, 0); 1245 pidff->device_control = 1246 pidff_find_special_field(pidff->reports[PID_DEVICE_CONTROL], 1247 PID_DEVICE_CONTROL_ARRAY, 1); 1248 1249 /* Detect and set permissive control quirk */ 1250 if (!pidff->device_control) { 1251 pr_debug("Setting PERMISSIVE_CONTROL quirk\n"); 1252 pidff->quirks |= HID_PIDFF_QUIRK_PERMISSIVE_CONTROL; 1253 pidff->device_control = pidff_find_special_field( 1254 pidff->reports[PID_DEVICE_CONTROL], 1255 PID_DEVICE_CONTROL_ARRAY, 0); 1256 } 1257 1258 pidff->block_load_status = 1259 pidff_find_special_field(pidff->reports[PID_BLOCK_LOAD], 1260 PID_BLOCK_LOAD_STATUS, 1); 1261 pidff->effect_operation_status = 1262 pidff_find_special_field(pidff->reports[PID_EFFECT_OPERATION], 1263 PID_EFFECT_OPERATION_ARRAY, 1); 1264 1265 hid_dbg(pidff->hid, "search done\n"); 1266 1267 if (!pidff->create_new_effect_type || !pidff->set_effect_type) { 1268 hid_err(pidff->hid, "effect lists not found\n"); 1269 return -1; 1270 } 1271 1272 if (!pidff->effect_direction) { 1273 hid_err(pidff->hid, "direction field not found\n"); 1274 return -1; 1275 } 1276 1277 if (!pidff->device_control) { 1278 hid_err(pidff->hid, "device control field not found\n"); 1279 return -1; 1280 } 1281 1282 if (!pidff->block_load_status) { 1283 hid_err(pidff->hid, "block load status field not found\n"); 1284 return -1; 1285 } 1286 1287 if (!pidff->effect_operation_status) { 1288 hid_err(pidff->hid, "effect operation field not found\n"); 1289 return -1; 1290 } 1291 1292 PIDFF_FIND_SPECIAL_KEYS(control_id, device_control, device_control); 1293 1294 if (!PIDFF_FIND_SPECIAL_KEYS(type_id, create_new_effect_type, 1295 effect_types)) { 1296 hid_err(pidff->hid, "no effect types found\n"); 1297 return -1; 1298 } 1299 1300 if (PIDFF_FIND_SPECIAL_KEYS(status_id, block_load_status, 1301 block_load_status) != 1302 ARRAY_SIZE(pidff_block_load_status)) { 1303 hid_err(pidff->hid, 1304 "block load status identifiers not found\n"); 1305 return -1; 1306 } 1307 1308 if (PIDFF_FIND_SPECIAL_KEYS(operation_id, effect_operation_status, 1309 effect_operation_status) != 1310 ARRAY_SIZE(pidff_effect_operation_status)) { 1311 hid_err(pidff->hid, "effect operation identifiers not found\n"); 1312 return -1; 1313 } 1314 1315 if (!pidff->axes_enable) { 1316 hid_info(pidff->hid, "axes enable field not found!\n"); 1317 return 0; 1318 } 1319 1320 hid_dbg(pidff->hid, "axes enable report count: %u\n", 1321 pidff->axes_enable->report_count); 1322 1323 uint found = PIDFF_FIND_GENERAL_DESKTOP(direction_axis_id, axes_enable, 1324 direction_axis); 1325 1326 pidff->axis_count = found; 1327 hid_dbg(pidff->hid, "found direction axes: %u", found); 1328 1329 for (int i = 0; i < ARRAY_SIZE(pidff_direction_axis); i++) { 1330 if (!pidff->direction_axis_id[i]) 1331 continue; 1332 1333 hid_dbg(pidff->hid, "axis %d, usage: 0x%04x, index: %d", i + 1, 1334 pidff_direction_axis[i], pidff->direction_axis_id[i]); 1335 } 1336 1337 if (pidff->axes_enable && found != pidff->axes_enable->report_count) 1338 hid_warn(pidff->hid, "axes_enable: %u != direction axes: %u", 1339 pidff->axes_enable->report_count, found); 1340 1341 return 0; 1342 } 1343 1344 /* 1345 * Find the implemented effect types 1346 */ 1347 static int pidff_find_effects(struct pidff_device *pidff, 1348 struct input_dev *dev) 1349 { 1350 int i; 1351 1352 for (i = 0; i < ARRAY_SIZE(pidff_effect_types); i++) { 1353 int pidff_type = pidff->type_id[i]; 1354 1355 if (pidff->set_effect_type->usage[pidff_type].hid != 1356 pidff->create_new_effect_type->usage[pidff_type].hid) { 1357 hid_err(pidff->hid, 1358 "effect type number %d is invalid\n", i); 1359 return -1; 1360 } 1361 } 1362 1363 if (pidff->type_id[PID_CONSTANT]) 1364 set_bit(FF_CONSTANT, dev->ffbit); 1365 if (pidff->type_id[PID_RAMP]) 1366 set_bit(FF_RAMP, dev->ffbit); 1367 if (pidff->type_id[PID_SQUARE]) { 1368 set_bit(FF_SQUARE, dev->ffbit); 1369 set_bit(FF_PERIODIC, dev->ffbit); 1370 } 1371 if (pidff->type_id[PID_SINE]) { 1372 set_bit(FF_SINE, dev->ffbit); 1373 set_bit(FF_PERIODIC, dev->ffbit); 1374 } 1375 if (pidff->type_id[PID_TRIANGLE]) { 1376 set_bit(FF_TRIANGLE, dev->ffbit); 1377 set_bit(FF_PERIODIC, dev->ffbit); 1378 } 1379 if (pidff->type_id[PID_SAW_UP]) { 1380 set_bit(FF_SAW_UP, dev->ffbit); 1381 set_bit(FF_PERIODIC, dev->ffbit); 1382 } 1383 if (pidff->type_id[PID_SAW_DOWN]) { 1384 set_bit(FF_SAW_DOWN, dev->ffbit); 1385 set_bit(FF_PERIODIC, dev->ffbit); 1386 } 1387 if (pidff->type_id[PID_SPRING]) 1388 set_bit(FF_SPRING, dev->ffbit); 1389 if (pidff->type_id[PID_DAMPER]) 1390 set_bit(FF_DAMPER, dev->ffbit); 1391 if (pidff->type_id[PID_INERTIA]) 1392 set_bit(FF_INERTIA, dev->ffbit); 1393 if (pidff->type_id[PID_FRICTION]) 1394 set_bit(FF_FRICTION, dev->ffbit); 1395 1396 return 0; 1397 } 1398 1399 #define PIDFF_FIND_FIELDS(name, report, strict) \ 1400 pidff_find_fields(pidff->name, pidff_ ## name, \ 1401 pidff->reports[report], \ 1402 ARRAY_SIZE(pidff_ ## name), strict, &pidff->quirks) 1403 1404 /* 1405 * Fill and check the pidff_usages 1406 */ 1407 static int pidff_init_fields(struct pidff_device *pidff, struct input_dev *dev) 1408 { 1409 if (PIDFF_FIND_FIELDS(set_effect, PID_SET_EFFECT, 1)) { 1410 hid_err(pidff->hid, "unknown set_effect report layout\n"); 1411 return -ENODEV; 1412 } 1413 1414 PIDFF_FIND_FIELDS(block_load, PID_BLOCK_LOAD, 0); 1415 if (!pidff->block_load[PID_EFFECT_BLOCK_INDEX].value) { 1416 hid_err(pidff->hid, "unknown pid_block_load report layout\n"); 1417 return -ENODEV; 1418 } 1419 1420 if (PIDFF_FIND_FIELDS(effect_operation, PID_EFFECT_OPERATION, 1)) { 1421 hid_err(pidff->hid, "unknown effect_operation report layout\n"); 1422 return -ENODEV; 1423 } 1424 1425 if (PIDFF_FIND_FIELDS(block_free, PID_BLOCK_FREE, 1)) { 1426 hid_err(pidff->hid, "unknown pid_block_free report layout\n"); 1427 return -ENODEV; 1428 } 1429 1430 if (pidff_find_special_fields(pidff) || pidff_find_effects(pidff, dev)) 1431 return -ENODEV; 1432 1433 if (PIDFF_FIND_FIELDS(set_envelope, PID_SET_ENVELOPE, 1)) { 1434 if (test_and_clear_bit(FF_CONSTANT, dev->ffbit)) 1435 hid_warn(pidff->hid, 1436 "has constant effect but no envelope\n"); 1437 if (test_and_clear_bit(FF_RAMP, dev->ffbit)) 1438 hid_warn(pidff->hid, 1439 "has ramp effect but no envelope\n"); 1440 1441 if (test_and_clear_bit(FF_PERIODIC, dev->ffbit)) 1442 hid_warn(pidff->hid, 1443 "has periodic effect but no envelope\n"); 1444 } 1445 1446 if (PIDFF_FIND_FIELDS(set_constant, PID_SET_CONSTANT, 1) && 1447 test_and_clear_bit(FF_CONSTANT, dev->ffbit)) 1448 hid_warn(pidff->hid, "unknown constant effect layout\n"); 1449 1450 if (PIDFF_FIND_FIELDS(set_ramp, PID_SET_RAMP, 1) && 1451 test_and_clear_bit(FF_RAMP, dev->ffbit)) 1452 hid_warn(pidff->hid, "unknown ramp effect layout\n"); 1453 1454 if (PIDFF_FIND_FIELDS(set_condition, PID_SET_CONDITION, 1)) { 1455 if (test_and_clear_bit(FF_SPRING, dev->ffbit) || 1456 test_and_clear_bit(FF_DAMPER, dev->ffbit) || 1457 test_and_clear_bit(FF_FRICTION, dev->ffbit) || 1458 test_and_clear_bit(FF_INERTIA, dev->ffbit)) 1459 hid_warn(pidff->hid, "unknown condition effect layout\n"); 1460 } 1461 1462 if (PIDFF_FIND_FIELDS(set_periodic, PID_SET_PERIODIC, 1) && 1463 test_and_clear_bit(FF_PERIODIC, dev->ffbit)) 1464 hid_warn(pidff->hid, "unknown periodic effect layout\n"); 1465 1466 PIDFF_FIND_FIELDS(pool, PID_POOL, 0); 1467 1468 if (!PIDFF_FIND_FIELDS(device_gain, PID_DEVICE_GAIN, 1)) 1469 set_bit(FF_GAIN, dev->ffbit); 1470 1471 return 0; 1472 } 1473 1474 /* 1475 * Test if autocenter modification is using the supported method 1476 */ 1477 static int pidff_check_autocenter(struct pidff_device *pidff, 1478 struct input_dev *dev) 1479 { 1480 int error; 1481 1482 /* 1483 * Let's find out if autocenter modification is supported 1484 * Specification doesn't specify anything, so we request an 1485 * effect upload and cancel it immediately. If the approved 1486 * effect id was one above the minimum, then we assume the first 1487 * effect id is a built-in spring type effect used for autocenter 1488 */ 1489 1490 error = pidff_request_effect_upload(pidff, 1); 1491 if (error) { 1492 hid_err(pidff->hid, "upload request failed\n"); 1493 return error; 1494 } 1495 1496 if (pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0] == 1497 pidff->block_load[PID_EFFECT_BLOCK_INDEX].field->logical_minimum + 1) { 1498 pidff_autocenter(pidff, U16_MAX); 1499 set_bit(FF_AUTOCENTER, dev->ffbit); 1500 } else { 1501 hid_notice(pidff->hid, 1502 "device has unknown autocenter control method\n"); 1503 } 1504 pidff_erase_pid(pidff, 1505 pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0]); 1506 1507 return 0; 1508 } 1509 1510 /* 1511 * Check if the device is PID and initialize it 1512 * Set initial quirks 1513 */ 1514 int hid_pidff_init_with_quirks(struct hid_device *hid, u32 initial_quirks) 1515 { 1516 struct pidff_device *pidff; 1517 struct hid_input *hidinput = 1518 list_entry(hid->inputs.next, struct hid_input, list); 1519 struct input_dev *dev = hidinput->input; 1520 struct ff_device *ff; 1521 int max_effects; 1522 int error; 1523 1524 hid_dbg(hid, "starting pid init\n"); 1525 1526 if (list_empty(&hid->report_enum[HID_OUTPUT_REPORT].report_list)) { 1527 hid_dbg(hid, "not a PID device, no output report\n"); 1528 return -ENODEV; 1529 } 1530 1531 pidff = kzalloc(sizeof(*pidff), GFP_KERNEL); 1532 if (!pidff) 1533 return -ENOMEM; 1534 1535 pidff->hid = hid; 1536 pidff->quirks = initial_quirks; 1537 pidff->effect_count = 0; 1538 1539 hid_device_io_start(hid); 1540 1541 pidff_find_reports(hid, HID_OUTPUT_REPORT, pidff); 1542 pidff_find_reports(hid, HID_FEATURE_REPORT, pidff); 1543 1544 if (!pidff_reports_ok(pidff)) { 1545 hid_dbg(hid, "reports not ok, aborting\n"); 1546 error = -ENODEV; 1547 goto fail; 1548 } 1549 1550 error = pidff_init_fields(pidff, dev); 1551 if (error) 1552 goto fail; 1553 1554 /* pool report is sometimes messed up, refetch it */ 1555 pidff_fetch_pool(pidff); 1556 pidff_set_gain_report(pidff, U16_MAX); 1557 error = pidff_check_autocenter(pidff, dev); 1558 if (error) 1559 goto fail; 1560 1561 max_effects = 1562 pidff->block_load[PID_EFFECT_BLOCK_INDEX].field->logical_maximum - 1563 pidff->block_load[PID_EFFECT_BLOCK_INDEX].field->logical_minimum + 1564 1; 1565 hid_dbg(hid, "max effects is %d\n", max_effects); 1566 1567 if (max_effects > PID_EFFECTS_MAX) 1568 max_effects = PID_EFFECTS_MAX; 1569 1570 if (pidff->pool[PID_SIMULTANEOUS_MAX].value) 1571 hid_dbg(hid, "max simultaneous effects is %d\n", 1572 pidff->pool[PID_SIMULTANEOUS_MAX].value[0]); 1573 1574 if (pidff->pool[PID_RAM_POOL_SIZE].value) 1575 hid_dbg(hid, "device memory size is %d bytes\n", 1576 pidff->pool[PID_RAM_POOL_SIZE].value[0]); 1577 1578 if (pidff->pool[PID_DEVICE_MANAGED_POOL].value && 1579 pidff->pool[PID_DEVICE_MANAGED_POOL].value[0] == 0) { 1580 error = -EPERM; 1581 hid_notice(hid, 1582 "device does not support device managed pool\n"); 1583 goto fail; 1584 } 1585 1586 error = input_ff_create(dev, max_effects); 1587 if (error) 1588 goto fail; 1589 1590 ff = dev->ff; 1591 ff->private = pidff; 1592 ff->upload = pidff_upload_effect; 1593 ff->erase = pidff_erase_effect; 1594 ff->set_gain = pidff_set_gain; 1595 ff->set_autocenter = pidff_set_autocenter; 1596 ff->playback = pidff_playback; 1597 1598 hid_info(dev, "Force feedback for USB HID PID devices by Anssi Hannula\n"); 1599 hid_dbg(dev, "Active quirks mask: 0x%08x\n", pidff->quirks); 1600 1601 hid_device_io_stop(hid); 1602 1603 return 0; 1604 1605 fail: 1606 hid_device_io_stop(hid); 1607 1608 kfree(pidff); 1609 return error; 1610 } 1611 EXPORT_SYMBOL_GPL(hid_pidff_init_with_quirks); 1612 1613 /* 1614 * Check if the device is PID and initialize it 1615 * Wrapper made to keep the compatibility with old 1616 * init function 1617 */ 1618 int hid_pidff_init(struct hid_device *hid) 1619 { 1620 return hid_pidff_init_with_quirks(hid, 0); 1621 } 1622