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