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