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