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