linux/drivers/hid/usbhid/hid-pidff.c
<<
>>
Prefs
   1/*
   2 *  Force feedback driver for USB HID PID compliant devices
   3 *
   4 *  Copyright (c) 2005, 2006 Anssi Hannula <anssi.hannula@gmail.com>
   5 */
   6
   7/*
   8 * This program is free software; you can redistribute it and/or modify
   9 * it under the terms of the GNU General Public License as published by
  10 * the Free Software Foundation; either version 2 of the License, or
  11 * (at your option) any later version.
  12 *
  13 * This program is distributed in the hope that it will be useful,
  14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16 * GNU General Public License for more details.
  17 *
  18 * You should have received a copy of the GNU General Public License
  19 * along with this program; if not, write to the Free Software
  20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21 */
  22
  23/* #define DEBUG */
  24
  25#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  26
  27#include <linux/input.h>
  28#include <linux/slab.h>
  29#include <linux/usb.h>
  30
  31#include <linux/hid.h>
  32
  33#include "usbhid.h"
  34
  35#define PID_EFFECTS_MAX         64
  36
  37/* Report usage table used to put reports into an array */
  38
  39#define PID_SET_EFFECT          0
  40#define PID_EFFECT_OPERATION    1
  41#define PID_DEVICE_GAIN         2
  42#define PID_POOL                3
  43#define PID_BLOCK_LOAD          4
  44#define PID_BLOCK_FREE          5
  45#define PID_DEVICE_CONTROL      6
  46#define PID_CREATE_NEW_EFFECT   7
  47
  48#define PID_REQUIRED_REPORTS    7
  49
  50#define PID_SET_ENVELOPE        8
  51#define PID_SET_CONDITION       9
  52#define PID_SET_PERIODIC        10
  53#define PID_SET_CONSTANT        11
  54#define PID_SET_RAMP            12
  55static const u8 pidff_reports[] = {
  56        0x21, 0x77, 0x7d, 0x7f, 0x89, 0x90, 0x96, 0xab,
  57        0x5a, 0x5f, 0x6e, 0x73, 0x74
  58};
  59
  60/* device_control is really 0x95, but 0x96 specified as it is the usage of
  61the only field in that report */
  62
  63/* Value usage tables used to put fields and values into arrays */
  64
  65#define PID_EFFECT_BLOCK_INDEX  0
  66
  67#define PID_DURATION            1
  68#define PID_GAIN                2
  69#define PID_TRIGGER_BUTTON      3
  70#define PID_TRIGGER_REPEAT_INT  4
  71#define PID_DIRECTION_ENABLE    5
  72#define PID_START_DELAY         6
  73static const u8 pidff_set_effect[] = {
  74        0x22, 0x50, 0x52, 0x53, 0x54, 0x56, 0xa7
  75};
  76
  77#define PID_ATTACK_LEVEL        1
  78#define PID_ATTACK_TIME         2
  79#define PID_FADE_LEVEL          3
  80#define PID_FADE_TIME           4
  81static const u8 pidff_set_envelope[] = { 0x22, 0x5b, 0x5c, 0x5d, 0x5e };
  82
  83#define PID_PARAM_BLOCK_OFFSET  1
  84#define PID_CP_OFFSET           2
  85#define PID_POS_COEFFICIENT     3
  86#define PID_NEG_COEFFICIENT     4
  87#define PID_POS_SATURATION      5
  88#define PID_NEG_SATURATION      6
  89#define PID_DEAD_BAND           7
  90static const u8 pidff_set_condition[] = {
  91        0x22, 0x23, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65
  92};
  93
  94#define PID_MAGNITUDE           1
  95#define PID_OFFSET              2
  96#define PID_PHASE               3
  97#define PID_PERIOD              4
  98static const u8 pidff_set_periodic[] = { 0x22, 0x70, 0x6f, 0x71, 0x72 };
  99static const u8 pidff_set_constant[] = { 0x22, 0x70 };
 100
 101#define PID_RAMP_START          1
 102#define PID_RAMP_END            2
 103static const u8 pidff_set_ramp[] = { 0x22, 0x75, 0x76 };
 104
 105#define PID_RAM_POOL_AVAILABLE  1
 106static const u8 pidff_block_load[] = { 0x22, 0xac };
 107
 108#define PID_LOOP_COUNT          1
 109static const u8 pidff_effect_operation[] = { 0x22, 0x7c };
 110
 111static const u8 pidff_block_free[] = { 0x22 };
 112
 113#define PID_DEVICE_GAIN_FIELD   0
 114static const u8 pidff_device_gain[] = { 0x7e };
 115
 116#define PID_RAM_POOL_SIZE       0
 117#define PID_SIMULTANEOUS_MAX    1
 118#define PID_DEVICE_MANAGED_POOL 2
 119static const u8 pidff_pool[] = { 0x80, 0x83, 0xa9 };
 120
 121/* Special field key tables used to put special field keys into arrays */
 122
 123#define PID_ENABLE_ACTUATORS    0
 124#define PID_RESET               1
 125static const u8 pidff_device_control[] = { 0x97, 0x9a };
 126
 127#define PID_CONSTANT    0
 128#define PID_RAMP        1
 129#define PID_SQUARE      2
 130#define PID_SINE        3
 131#define PID_TRIANGLE    4
 132#define PID_SAW_UP      5
 133#define PID_SAW_DOWN    6
 134#define PID_SPRING      7
 135#define PID_DAMPER      8
 136#define PID_INERTIA     9
 137#define PID_FRICTION    10
 138static const u8 pidff_effect_types[] = {
 139        0x26, 0x27, 0x30, 0x31, 0x32, 0x33, 0x34,
 140        0x40, 0x41, 0x42, 0x43
 141};
 142
 143#define PID_BLOCK_LOAD_SUCCESS  0
 144#define PID_BLOCK_LOAD_FULL     1
 145static const u8 pidff_block_load_status[] = { 0x8c, 0x8d };
 146
 147#define PID_EFFECT_START        0
 148#define PID_EFFECT_STOP         1
 149static const u8 pidff_effect_operation_status[] = { 0x79, 0x7b };
 150
 151struct pidff_usage {
 152        struct hid_field *field;
 153        s32 *value;
 154};
 155
 156struct 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        /* Special field is a field that is not composed of
 175           usage<->value pairs that pidff_usage values are */
 176
 177        /* Special field in create_new_effect */
 178        struct hid_field *create_new_effect_type;
 179
 180        /* Special fields in set_effect */
 181        struct hid_field *set_effect_type;
 182        struct hid_field *effect_direction;
 183
 184        /* Special field in device_control */
 185        struct hid_field *device_control;
 186
 187        /* Special field in block_load */
 188        struct hid_field *block_load_status;
 189
 190        /* Special field in effect_operation */
 191        struct hid_field *effect_operation_status;
 192
 193        int control_id[sizeof(pidff_device_control)];
 194        int type_id[sizeof(pidff_effect_types)];
 195        int status_id[sizeof(pidff_block_load_status)];
 196        int operation_id[sizeof(pidff_effect_operation_status)];
 197
 198        int pid_id[PID_EFFECTS_MAX];
 199};
 200
 201/*
 202 * Scale an unsigned value with range 0..max for the given field
 203 */
 204static int pidff_rescale(int i, int max, struct hid_field *field)
 205{
 206        return i * (field->logical_maximum - field->logical_minimum) / max +
 207            field->logical_minimum;
 208}
 209
 210/*
 211 * Scale a signed value in range -0x8000..0x7fff for the given field
 212 */
 213static int pidff_rescale_signed(int i, struct hid_field *field)
 214{
 215        return i == 0 ? 0 : i >
 216            0 ? i * field->logical_maximum / 0x7fff : i *
 217            field->logical_minimum / -0x8000;
 218}
 219
 220static void pidff_set(struct pidff_usage *usage, u16 value)
 221{
 222        usage->value[0] = pidff_rescale(value, 0xffff, usage->field);
 223        pr_debug("calculated from %d to %d\n", value, usage->value[0]);
 224}
 225
 226static void pidff_set_signed(struct pidff_usage *usage, s16 value)
 227{
 228        if (usage->field->logical_minimum < 0)
 229                usage->value[0] = pidff_rescale_signed(value, usage->field);
 230        else {
 231                if (value < 0)
 232                        usage->value[0] =
 233                            pidff_rescale(-value, 0x8000, usage->field);
 234                else
 235                        usage->value[0] =
 236                            pidff_rescale(value, 0x7fff, usage->field);
 237        }
 238        pr_debug("calculated from %d to %d\n", value, usage->value[0]);
 239}
 240
 241/*
 242 * Send envelope report to the device
 243 */
 244static void pidff_set_envelope_report(struct pidff_device *pidff,
 245                                      struct ff_envelope *envelope)
 246{
 247        pidff->set_envelope[PID_EFFECT_BLOCK_INDEX].value[0] =
 248            pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0];
 249
 250        pidff->set_envelope[PID_ATTACK_LEVEL].value[0] =
 251            pidff_rescale(envelope->attack_level >
 252                          0x7fff ? 0x7fff : envelope->attack_level, 0x7fff,
 253                          pidff->set_envelope[PID_ATTACK_LEVEL].field);
 254        pidff->set_envelope[PID_FADE_LEVEL].value[0] =
 255            pidff_rescale(envelope->fade_level >
 256                          0x7fff ? 0x7fff : envelope->fade_level, 0x7fff,
 257                          pidff->set_envelope[PID_FADE_LEVEL].field);
 258
 259        pidff->set_envelope[PID_ATTACK_TIME].value[0] = envelope->attack_length;
 260        pidff->set_envelope[PID_FADE_TIME].value[0] = envelope->fade_length;
 261
 262        hid_dbg(pidff->hid, "attack %u => %d\n",
 263                envelope->attack_level,
 264                pidff->set_envelope[PID_ATTACK_LEVEL].value[0]);
 265
 266        hid_hw_request(pidff->hid, pidff->reports[PID_SET_ENVELOPE],
 267                        HID_REQ_SET_REPORT);
 268}
 269
 270/*
 271 * Test if the new envelope differs from old one
 272 */
 273static int pidff_needs_set_envelope(struct ff_envelope *envelope,
 274                                    struct ff_envelope *old)
 275{
 276        return envelope->attack_level != old->attack_level ||
 277               envelope->fade_level != old->fade_level ||
 278               envelope->attack_length != old->attack_length ||
 279               envelope->fade_length != old->fade_length;
 280}
 281
 282/*
 283 * Send constant force report to the device
 284 */
 285static void pidff_set_constant_force_report(struct pidff_device *pidff,
 286                                            struct ff_effect *effect)
 287{
 288        pidff->set_constant[PID_EFFECT_BLOCK_INDEX].value[0] =
 289                pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0];
 290        pidff_set_signed(&pidff->set_constant[PID_MAGNITUDE],
 291                         effect->u.constant.level);
 292
 293        hid_hw_request(pidff->hid, pidff->reports[PID_SET_CONSTANT],
 294                        HID_REQ_SET_REPORT);
 295}
 296
 297/*
 298 * Test if the constant parameters have changed between effects
 299 */
 300static int pidff_needs_set_constant(struct ff_effect *effect,
 301                                    struct ff_effect *old)
 302{
 303        return effect->u.constant.level != old->u.constant.level;
 304}
 305
 306/*
 307 * Send set effect report to the device
 308 */
 309static void pidff_set_effect_report(struct pidff_device *pidff,
 310                                    struct ff_effect *effect)
 311{
 312        pidff->set_effect[PID_EFFECT_BLOCK_INDEX].value[0] =
 313                pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0];
 314        pidff->set_effect_type->value[0] =
 315                pidff->create_new_effect_type->value[0];
 316        pidff->set_effect[PID_DURATION].value[0] = effect->replay.length;
 317        pidff->set_effect[PID_TRIGGER_BUTTON].value[0] = effect->trigger.button;
 318        pidff->set_effect[PID_TRIGGER_REPEAT_INT].value[0] =
 319                effect->trigger.interval;
 320        pidff->set_effect[PID_GAIN].value[0] =
 321                pidff->set_effect[PID_GAIN].field->logical_maximum;
 322        pidff->set_effect[PID_DIRECTION_ENABLE].value[0] = 1;
 323        pidff->effect_direction->value[0] =
 324                pidff_rescale(effect->direction, 0xffff,
 325                                pidff->effect_direction);
 326        pidff->set_effect[PID_START_DELAY].value[0] = effect->replay.delay;
 327
 328        hid_hw_request(pidff->hid, pidff->reports[PID_SET_EFFECT],
 329                        HID_REQ_SET_REPORT);
 330}
 331
 332/*
 333 * Test if the values used in set_effect have changed
 334 */
 335static int pidff_needs_set_effect(struct ff_effect *effect,
 336                                  struct ff_effect *old)
 337{
 338        return effect->replay.length != old->replay.length ||
 339               effect->trigger.interval != old->trigger.interval ||
 340               effect->trigger.button != old->trigger.button ||
 341               effect->direction != old->direction ||
 342               effect->replay.delay != old->replay.delay;
 343}
 344
 345/*
 346 * Send periodic effect report to the device
 347 */
 348static void pidff_set_periodic_report(struct pidff_device *pidff,
 349                                      struct ff_effect *effect)
 350{
 351        pidff->set_periodic[PID_EFFECT_BLOCK_INDEX].value[0] =
 352                pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0];
 353        pidff_set_signed(&pidff->set_periodic[PID_MAGNITUDE],
 354                         effect->u.periodic.magnitude);
 355        pidff_set_signed(&pidff->set_periodic[PID_OFFSET],
 356                         effect->u.periodic.offset);
 357        pidff_set(&pidff->set_periodic[PID_PHASE], effect->u.periodic.phase);
 358        pidff->set_periodic[PID_PERIOD].value[0] = effect->u.periodic.period;
 359
 360        hid_hw_request(pidff->hid, pidff->reports[PID_SET_PERIODIC],
 361                        HID_REQ_SET_REPORT);
 362
 363}
 364
 365/*
 366 * Test if periodic effect parameters have changed
 367 */
 368static int pidff_needs_set_periodic(struct ff_effect *effect,
 369                                    struct ff_effect *old)
 370{
 371        return effect->u.periodic.magnitude != old->u.periodic.magnitude ||
 372               effect->u.periodic.offset != old->u.periodic.offset ||
 373               effect->u.periodic.phase != old->u.periodic.phase ||
 374               effect->u.periodic.period != old->u.periodic.period;
 375}
 376
 377/*
 378 * Send condition effect reports to the device
 379 */
 380static void pidff_set_condition_report(struct pidff_device *pidff,
 381                                       struct ff_effect *effect)
 382{
 383        int i;
 384
 385        pidff->set_condition[PID_EFFECT_BLOCK_INDEX].value[0] =
 386                pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0];
 387
 388        for (i = 0; i < 2; i++) {
 389                pidff->set_condition[PID_PARAM_BLOCK_OFFSET].value[0] = i;
 390                pidff_set_signed(&pidff->set_condition[PID_CP_OFFSET],
 391                                 effect->u.condition[i].center);
 392                pidff_set_signed(&pidff->set_condition[PID_POS_COEFFICIENT],
 393                                 effect->u.condition[i].right_coeff);
 394                pidff_set_signed(&pidff->set_condition[PID_NEG_COEFFICIENT],
 395                                 effect->u.condition[i].left_coeff);
 396                pidff_set(&pidff->set_condition[PID_POS_SATURATION],
 397                          effect->u.condition[i].right_saturation);
 398                pidff_set(&pidff->set_condition[PID_NEG_SATURATION],
 399                          effect->u.condition[i].left_saturation);
 400                pidff_set(&pidff->set_condition[PID_DEAD_BAND],
 401                          effect->u.condition[i].deadband);
 402                hid_hw_request(pidff->hid, pidff->reports[PID_SET_CONDITION],
 403                                HID_REQ_SET_REPORT);
 404        }
 405}
 406
 407/*
 408 * Test if condition effect parameters have changed
 409 */
 410static int pidff_needs_set_condition(struct ff_effect *effect,
 411                                     struct ff_effect *old)
 412{
 413        int i;
 414        int ret = 0;
 415
 416        for (i = 0; i < 2; i++) {
 417                struct ff_condition_effect *cond = &effect->u.condition[i];
 418                struct ff_condition_effect *old_cond = &old->u.condition[i];
 419
 420                ret |= cond->center != old_cond->center ||
 421                       cond->right_coeff != old_cond->right_coeff ||
 422                       cond->left_coeff != old_cond->left_coeff ||
 423                       cond->right_saturation != old_cond->right_saturation ||
 424                       cond->left_saturation != old_cond->left_saturation ||
 425                       cond->deadband != old_cond->deadband;
 426        }
 427
 428        return ret;
 429}
 430
 431/*
 432 * Send ramp force report to the device
 433 */
 434static void pidff_set_ramp_force_report(struct pidff_device *pidff,
 435                                        struct ff_effect *effect)
 436{
 437        pidff->set_ramp[PID_EFFECT_BLOCK_INDEX].value[0] =
 438                pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0];
 439        pidff_set_signed(&pidff->set_ramp[PID_RAMP_START],
 440                         effect->u.ramp.start_level);
 441        pidff_set_signed(&pidff->set_ramp[PID_RAMP_END],
 442                         effect->u.ramp.end_level);
 443        hid_hw_request(pidff->hid, pidff->reports[PID_SET_RAMP],
 444                        HID_REQ_SET_REPORT);
 445}
 446
 447/*
 448 * Test if ramp force parameters have changed
 449 */
 450static int pidff_needs_set_ramp(struct ff_effect *effect, struct ff_effect *old)
 451{
 452        return effect->u.ramp.start_level != old->u.ramp.start_level ||
 453               effect->u.ramp.end_level != old->u.ramp.end_level;
 454}
 455
 456/*
 457 * Send a request for effect upload to the device
 458 *
 459 * Returns 0 if device reported success, -ENOSPC if the device reported memory
 460 * is full. Upon unknown response the function will retry for 60 times, if
 461 * still unsuccessful -EIO is returned.
 462 */
 463static int pidff_request_effect_upload(struct pidff_device *pidff, int efnum)
 464{
 465        int j;
 466
 467        pidff->create_new_effect_type->value[0] = efnum;
 468        hid_hw_request(pidff->hid, pidff->reports[PID_CREATE_NEW_EFFECT],
 469                        HID_REQ_SET_REPORT);
 470        hid_dbg(pidff->hid, "create_new_effect sent, type: %d\n", efnum);
 471
 472        pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0] = 0;
 473        pidff->block_load_status->value[0] = 0;
 474        hid_hw_wait(pidff->hid);
 475
 476        for (j = 0; j < 60; j++) {
 477                hid_dbg(pidff->hid, "pid_block_load requested\n");
 478                hid_hw_request(pidff->hid, pidff->reports[PID_BLOCK_LOAD],
 479                                HID_REQ_GET_REPORT);
 480                hid_hw_wait(pidff->hid);
 481                if (pidff->block_load_status->value[0] ==
 482                    pidff->status_id[PID_BLOCK_LOAD_SUCCESS]) {
 483                        hid_dbg(pidff->hid, "device reported free memory: %d bytes\n",
 484                                 pidff->block_load[PID_RAM_POOL_AVAILABLE].value ?
 485                                 pidff->block_load[PID_RAM_POOL_AVAILABLE].value[0] : -1);
 486                        return 0;
 487                }
 488                if (pidff->block_load_status->value[0] ==
 489                    pidff->status_id[PID_BLOCK_LOAD_FULL]) {
 490                        hid_dbg(pidff->hid, "not enough memory free: %d bytes\n",
 491                                pidff->block_load[PID_RAM_POOL_AVAILABLE].value ?
 492                                pidff->block_load[PID_RAM_POOL_AVAILABLE].value[0] : -1);
 493                        return -ENOSPC;
 494                }
 495        }
 496        hid_err(pidff->hid, "pid_block_load failed 60 times\n");
 497        return -EIO;
 498}
 499
 500/*
 501 * Play the effect with PID id n times
 502 */
 503static void pidff_playback_pid(struct pidff_device *pidff, int pid_id, int n)
 504{
 505        pidff->effect_operation[PID_EFFECT_BLOCK_INDEX].value[0] = pid_id;
 506
 507        if (n == 0) {
 508                pidff->effect_operation_status->value[0] =
 509                        pidff->operation_id[PID_EFFECT_STOP];
 510        } else {
 511                pidff->effect_operation_status->value[0] =
 512                        pidff->operation_id[PID_EFFECT_START];
 513                pidff->effect_operation[PID_LOOP_COUNT].value[0] = n;
 514        }
 515
 516        hid_hw_request(pidff->hid, pidff->reports[PID_EFFECT_OPERATION],
 517                        HID_REQ_SET_REPORT);
 518}
 519
 520/**
 521 * Play the effect with effect id @effect_id for @value times
 522 */
 523static int pidff_playback(struct input_dev *dev, int effect_id, int value)
 524{
 525        struct pidff_device *pidff = dev->ff->private;
 526
 527        pidff_playback_pid(pidff, pidff->pid_id[effect_id], value);
 528
 529        return 0;
 530}
 531
 532/*
 533 * Erase effect with PID id
 534 */
 535static void pidff_erase_pid(struct pidff_device *pidff, int pid_id)
 536{
 537        pidff->block_free[PID_EFFECT_BLOCK_INDEX].value[0] = pid_id;
 538        hid_hw_request(pidff->hid, pidff->reports[PID_BLOCK_FREE],
 539                        HID_REQ_SET_REPORT);
 540}
 541
 542/*
 543 * Stop and erase effect with effect_id
 544 */
 545static int pidff_erase_effect(struct input_dev *dev, int effect_id)
 546{
 547        struct pidff_device *pidff = dev->ff->private;
 548        int pid_id = pidff->pid_id[effect_id];
 549
 550        hid_dbg(pidff->hid, "starting to erase %d/%d\n",
 551                effect_id, pidff->pid_id[effect_id]);
 552        /* Wait for the queue to clear. We do not want a full fifo to
 553           prevent the effect removal. */
 554        hid_hw_wait(pidff->hid);
 555        pidff_playback_pid(pidff, pid_id, 0);
 556        pidff_erase_pid(pidff, pid_id);
 557
 558        return 0;
 559}
 560
 561/*
 562 * Effect upload handler
 563 */
 564static int pidff_upload_effect(struct input_dev *dev, struct ff_effect *effect,
 565                               struct ff_effect *old)
 566{
 567        struct pidff_device *pidff = dev->ff->private;
 568        int type_id;
 569        int error;
 570
 571        switch (effect->type) {
 572        case FF_CONSTANT:
 573                if (!old) {
 574                        error = pidff_request_effect_upload(pidff,
 575                                        pidff->type_id[PID_CONSTANT]);
 576                        if (error)
 577                                return error;
 578                }
 579                if (!old || pidff_needs_set_effect(effect, old))
 580                        pidff_set_effect_report(pidff, effect);
 581                if (!old || pidff_needs_set_constant(effect, old))
 582                        pidff_set_constant_force_report(pidff, effect);
 583                if (!old ||
 584                    pidff_needs_set_envelope(&effect->u.constant.envelope,
 585                                        &old->u.constant.envelope))
 586                        pidff_set_envelope_report(pidff,
 587                                        &effect->u.constant.envelope);
 588                break;
 589
 590        case FF_PERIODIC:
 591                if (!old) {
 592                        switch (effect->u.periodic.waveform) {
 593                        case FF_SQUARE:
 594                                type_id = PID_SQUARE;
 595                                break;
 596                        case FF_TRIANGLE:
 597                                type_id = PID_TRIANGLE;
 598                                break;
 599                        case FF_SINE:
 600                                type_id = PID_SINE;
 601                                break;
 602                        case FF_SAW_UP:
 603                                type_id = PID_SAW_UP;
 604                                break;
 605                        case FF_SAW_DOWN:
 606                                type_id = PID_SAW_DOWN;
 607                                break;
 608                        default:
 609                                hid_err(pidff->hid, "invalid waveform\n");
 610                                return -EINVAL;
 611                        }
 612
 613                        error = pidff_request_effect_upload(pidff,
 614                                        pidff->type_id[type_id]);
 615                        if (error)
 616                                return error;
 617                }
 618                if (!old || pidff_needs_set_effect(effect, old))
 619                        pidff_set_effect_report(pidff, effect);
 620                if (!old || pidff_needs_set_periodic(effect, old))
 621                        pidff_set_periodic_report(pidff, effect);
 622                if (!old ||
 623                    pidff_needs_set_envelope(&effect->u.periodic.envelope,
 624                                        &old->u.periodic.envelope))
 625                        pidff_set_envelope_report(pidff,
 626                                        &effect->u.periodic.envelope);
 627                break;
 628
 629        case FF_RAMP:
 630                if (!old) {
 631                        error = pidff_request_effect_upload(pidff,
 632                                        pidff->type_id[PID_RAMP]);
 633                        if (error)
 634                                return error;
 635                }
 636                if (!old || pidff_needs_set_effect(effect, old))
 637                        pidff_set_effect_report(pidff, effect);
 638                if (!old || pidff_needs_set_ramp(effect, old))
 639                        pidff_set_ramp_force_report(pidff, effect);
 640                if (!old ||
 641                    pidff_needs_set_envelope(&effect->u.ramp.envelope,
 642                                        &old->u.ramp.envelope))
 643                        pidff_set_envelope_report(pidff,
 644                                        &effect->u.ramp.envelope);
 645                break;
 646
 647        case FF_SPRING:
 648                if (!old) {
 649                        error = pidff_request_effect_upload(pidff,
 650                                        pidff->type_id[PID_SPRING]);
 651                        if (error)
 652                                return error;
 653                }
 654                if (!old || pidff_needs_set_effect(effect, old))
 655                        pidff_set_effect_report(pidff, effect);
 656                if (!old || pidff_needs_set_condition(effect, old))
 657                        pidff_set_condition_report(pidff, effect);
 658                break;
 659
 660        case FF_FRICTION:
 661                if (!old) {
 662                        error = pidff_request_effect_upload(pidff,
 663                                        pidff->type_id[PID_FRICTION]);
 664                        if (error)
 665                                return error;
 666                }
 667                if (!old || pidff_needs_set_effect(effect, old))
 668                        pidff_set_effect_report(pidff, effect);
 669                if (!old || pidff_needs_set_condition(effect, old))
 670                        pidff_set_condition_report(pidff, effect);
 671                break;
 672
 673        case FF_DAMPER:
 674                if (!old) {
 675                        error = pidff_request_effect_upload(pidff,
 676                                        pidff->type_id[PID_DAMPER]);
 677                        if (error)
 678                                return error;
 679                }
 680                if (!old || pidff_needs_set_effect(effect, old))
 681                        pidff_set_effect_report(pidff, effect);
 682                if (!old || pidff_needs_set_condition(effect, old))
 683                        pidff_set_condition_report(pidff, effect);
 684                break;
 685
 686        case FF_INERTIA:
 687                if (!old) {
 688                        error = pidff_request_effect_upload(pidff,
 689                                        pidff->type_id[PID_INERTIA]);
 690                        if (error)
 691                                return error;
 692                }
 693                if (!old || pidff_needs_set_effect(effect, old))
 694                        pidff_set_effect_report(pidff, effect);
 695                if (!old || pidff_needs_set_condition(effect, old))
 696                        pidff_set_condition_report(pidff, effect);
 697                break;
 698
 699        default:
 700                hid_err(pidff->hid, "invalid type\n");
 701                return -EINVAL;
 702        }
 703
 704        if (!old)
 705                pidff->pid_id[effect->id] =
 706                    pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0];
 707
 708        hid_dbg(pidff->hid, "uploaded\n");
 709
 710        return 0;
 711}
 712
 713/*
 714 * set_gain() handler
 715 */
 716static void pidff_set_gain(struct input_dev *dev, u16 gain)
 717{
 718        struct pidff_device *pidff = dev->ff->private;
 719
 720        pidff_set(&pidff->device_gain[PID_DEVICE_GAIN_FIELD], gain);
 721        hid_hw_request(pidff->hid, pidff->reports[PID_DEVICE_GAIN],
 722                        HID_REQ_SET_REPORT);
 723}
 724
 725static void pidff_autocenter(struct pidff_device *pidff, u16 magnitude)
 726{
 727        struct hid_field *field =
 728                pidff->block_load[PID_EFFECT_BLOCK_INDEX].field;
 729
 730        if (!magnitude) {
 731                pidff_playback_pid(pidff, field->logical_minimum, 0);
 732                return;
 733        }
 734
 735        pidff_playback_pid(pidff, field->logical_minimum, 1);
 736
 737        pidff->set_effect[PID_EFFECT_BLOCK_INDEX].value[0] =
 738                pidff->block_load[PID_EFFECT_BLOCK_INDEX].field->logical_minimum;
 739        pidff->set_effect_type->value[0] = pidff->type_id[PID_SPRING];
 740        pidff->set_effect[PID_DURATION].value[0] = 0;
 741        pidff->set_effect[PID_TRIGGER_BUTTON].value[0] = 0;
 742        pidff->set_effect[PID_TRIGGER_REPEAT_INT].value[0] = 0;
 743        pidff_set(&pidff->set_effect[PID_GAIN], magnitude);
 744        pidff->set_effect[PID_DIRECTION_ENABLE].value[0] = 1;
 745        pidff->set_effect[PID_START_DELAY].value[0] = 0;
 746
 747        hid_hw_request(pidff->hid, pidff->reports[PID_SET_EFFECT],
 748                        HID_REQ_SET_REPORT);
 749}
 750
 751/*
 752 * pidff_set_autocenter() handler
 753 */
 754static void pidff_set_autocenter(struct input_dev *dev, u16 magnitude)
 755{
 756        struct pidff_device *pidff = dev->ff->private;
 757
 758        pidff_autocenter(pidff, magnitude);
 759}
 760
 761/*
 762 * Find fields from a report and fill a pidff_usage
 763 */
 764static int pidff_find_fields(struct pidff_usage *usage, const u8 *table,
 765                             struct hid_report *report, int count, int strict)
 766{
 767        int i, j, k, found;
 768
 769        for (k = 0; k < count; k++) {
 770                found = 0;
 771                for (i = 0; i < report->maxfield; i++) {
 772                        if (report->field[i]->maxusage !=
 773                            report->field[i]->report_count) {
 774                                pr_debug("maxusage and report_count do not match, skipping\n");
 775                                continue;
 776                        }
 777                        for (j = 0; j < report->field[i]->maxusage; j++) {
 778                                if (report->field[i]->usage[j].hid ==
 779                                    (HID_UP_PID | table[k])) {
 780                                        pr_debug("found %d at %d->%d\n",
 781                                                 k, i, j);
 782                                        usage[k].field = report->field[i];
 783                                        usage[k].value =
 784                                                &report->field[i]->value[j];
 785                                        found = 1;
 786                                        break;
 787                                }
 788                        }
 789                        if (found)
 790                                break;
 791                }
 792                if (!found && strict) {
 793                        pr_debug("failed to locate %d\n", k);
 794                        return -1;
 795                }
 796        }
 797        return 0;
 798}
 799
 800/*
 801 * Return index into pidff_reports for the given usage
 802 */
 803static int pidff_check_usage(int usage)
 804{
 805        int i;
 806
 807        for (i = 0; i < sizeof(pidff_reports); i++)
 808                if (usage == (HID_UP_PID | pidff_reports[i]))
 809                        return i;
 810
 811        return -1;
 812}
 813
 814/*
 815 * Find the reports and fill pidff->reports[]
 816 * report_type specifies either OUTPUT or FEATURE reports
 817 */
 818static void pidff_find_reports(struct hid_device *hid, int report_type,
 819                               struct pidff_device *pidff)
 820{
 821        struct hid_report *report;
 822        int i, ret;
 823
 824        list_for_each_entry(report,
 825                            &hid->report_enum[report_type].report_list, list) {
 826                if (report->maxfield < 1)
 827                        continue;
 828                ret = pidff_check_usage(report->field[0]->logical);
 829                if (ret != -1) {
 830                        hid_dbg(hid, "found usage 0x%02x from field->logical\n",
 831                                pidff_reports[ret]);
 832                        pidff->reports[ret] = report;
 833                        continue;
 834                }
 835
 836                /*
 837                 * Sometimes logical collections are stacked to indicate
 838                 * different usages for the report and the field, in which
 839                 * case we want the usage of the parent. However, Linux HID
 840                 * implementation hides this fact, so we have to dig it up
 841                 * ourselves
 842                 */
 843                i = report->field[0]->usage[0].collection_index;
 844                if (i <= 0 ||
 845                    hid->collection[i - 1].type != HID_COLLECTION_LOGICAL)
 846                        continue;
 847                ret = pidff_check_usage(hid->collection[i - 1].usage);
 848                if (ret != -1 && !pidff->reports[ret]) {
 849                        hid_dbg(hid,
 850                                "found usage 0x%02x from collection array\n",
 851                                pidff_reports[ret]);
 852                        pidff->reports[ret] = report;
 853                }
 854        }
 855}
 856
 857/*
 858 * Test if the required reports have been found
 859 */
 860static int pidff_reports_ok(struct pidff_device *pidff)
 861{
 862        int i;
 863
 864        for (i = 0; i <= PID_REQUIRED_REPORTS; i++) {
 865                if (!pidff->reports[i]) {
 866                        hid_dbg(pidff->hid, "%d missing\n", i);
 867                        return 0;
 868                }
 869        }
 870
 871        return 1;
 872}
 873
 874/*
 875 * Find a field with a specific usage within a report
 876 */
 877static struct hid_field *pidff_find_special_field(struct hid_report *report,
 878                                                  int usage, int enforce_min)
 879{
 880        int i;
 881
 882        for (i = 0; i < report->maxfield; i++) {
 883                if (report->field[i]->logical == (HID_UP_PID | usage) &&
 884                    report->field[i]->report_count > 0) {
 885                        if (!enforce_min ||
 886                            report->field[i]->logical_minimum == 1)
 887                                return report->field[i];
 888                        else {
 889                                pr_err("logical_minimum is not 1 as it should be\n");
 890                                return NULL;
 891                        }
 892                }
 893        }
 894        return NULL;
 895}
 896
 897/*
 898 * Fill a pidff->*_id struct table
 899 */
 900static int pidff_find_special_keys(int *keys, struct hid_field *fld,
 901                                   const u8 *usagetable, int count)
 902{
 903
 904        int i, j;
 905        int found = 0;
 906
 907        for (i = 0; i < count; i++) {
 908                for (j = 0; j < fld->maxusage; j++) {
 909                        if (fld->usage[j].hid == (HID_UP_PID | usagetable[i])) {
 910                                keys[i] = j + 1;
 911                                found++;
 912                                break;
 913                        }
 914                }
 915        }
 916        return found;
 917}
 918
 919#define PIDFF_FIND_SPECIAL_KEYS(keys, field, name) \
 920        pidff_find_special_keys(pidff->keys, pidff->field, pidff_ ## name, \
 921                sizeof(pidff_ ## name))
 922
 923/*
 924 * Find and check the special fields
 925 */
 926static int pidff_find_special_fields(struct pidff_device *pidff)
 927{
 928        hid_dbg(pidff->hid, "finding special fields\n");
 929
 930        pidff->create_new_effect_type =
 931                pidff_find_special_field(pidff->reports[PID_CREATE_NEW_EFFECT],
 932                                         0x25, 1);
 933        pidff->set_effect_type =
 934                pidff_find_special_field(pidff->reports[PID_SET_EFFECT],
 935                                         0x25, 1);
 936        pidff->effect_direction =
 937                pidff_find_special_field(pidff->reports[PID_SET_EFFECT],
 938                                         0x57, 0);
 939        pidff->device_control =
 940                pidff_find_special_field(pidff->reports[PID_DEVICE_CONTROL],
 941                                         0x96, 1);
 942        pidff->block_load_status =
 943                pidff_find_special_field(pidff->reports[PID_BLOCK_LOAD],
 944                                         0x8b, 1);
 945        pidff->effect_operation_status =
 946                pidff_find_special_field(pidff->reports[PID_EFFECT_OPERATION],
 947                                         0x78, 1);
 948
 949        hid_dbg(pidff->hid, "search done\n");
 950
 951        if (!pidff->create_new_effect_type || !pidff->set_effect_type) {
 952                hid_err(pidff->hid, "effect lists not found\n");
 953                return -1;
 954        }
 955
 956        if (!pidff->effect_direction) {
 957                hid_err(pidff->hid, "direction field not found\n");
 958                return -1;
 959        }
 960
 961        if (!pidff->device_control) {
 962                hid_err(pidff->hid, "device control field not found\n");
 963                return -1;
 964        }
 965
 966        if (!pidff->block_load_status) {
 967                hid_err(pidff->hid, "block load status field not found\n");
 968                return -1;
 969        }
 970
 971        if (!pidff->effect_operation_status) {
 972                hid_err(pidff->hid, "effect operation field not found\n");
 973                return -1;
 974        }
 975
 976        pidff_find_special_keys(pidff->control_id, pidff->device_control,
 977                                pidff_device_control,
 978                                sizeof(pidff_device_control));
 979
 980        PIDFF_FIND_SPECIAL_KEYS(control_id, device_control, device_control);
 981
 982        if (!PIDFF_FIND_SPECIAL_KEYS(type_id, create_new_effect_type,
 983                                     effect_types)) {
 984                hid_err(pidff->hid, "no effect types found\n");
 985                return -1;
 986        }
 987
 988        if (PIDFF_FIND_SPECIAL_KEYS(status_id, block_load_status,
 989                                    block_load_status) !=
 990                        sizeof(pidff_block_load_status)) {
 991                hid_err(pidff->hid,
 992                        "block load status identifiers not found\n");
 993                return -1;
 994        }
 995
 996        if (PIDFF_FIND_SPECIAL_KEYS(operation_id, effect_operation_status,
 997                                    effect_operation_status) !=
 998                        sizeof(pidff_effect_operation_status)) {
 999                hid_err(pidff->hid, "effect operation identifiers not found\n");
1000                return -1;
1001        }
1002
1003        return 0;
1004}
1005
1006/**
1007 * Find the implemented effect types
1008 */
1009static int pidff_find_effects(struct pidff_device *pidff,
1010                              struct input_dev *dev)
1011{
1012        int i;
1013
1014        for (i = 0; i < sizeof(pidff_effect_types); i++) {
1015                int pidff_type = pidff->type_id[i];
1016                if (pidff->set_effect_type->usage[pidff_type].hid !=
1017                    pidff->create_new_effect_type->usage[pidff_type].hid) {
1018                        hid_err(pidff->hid,
1019                                "effect type number %d is invalid\n", i);
1020                        return -1;
1021                }
1022        }
1023
1024        if (pidff->type_id[PID_CONSTANT])
1025                set_bit(FF_CONSTANT, dev->ffbit);
1026        if (pidff->type_id[PID_RAMP])
1027                set_bit(FF_RAMP, dev->ffbit);
1028        if (pidff->type_id[PID_SQUARE]) {
1029                set_bit(FF_SQUARE, dev->ffbit);
1030                set_bit(FF_PERIODIC, dev->ffbit);
1031        }
1032        if (pidff->type_id[PID_SINE]) {
1033                set_bit(FF_SINE, dev->ffbit);
1034                set_bit(FF_PERIODIC, dev->ffbit);
1035        }
1036        if (pidff->type_id[PID_TRIANGLE]) {
1037                set_bit(FF_TRIANGLE, dev->ffbit);
1038                set_bit(FF_PERIODIC, dev->ffbit);
1039        }
1040        if (pidff->type_id[PID_SAW_UP]) {
1041                set_bit(FF_SAW_UP, dev->ffbit);
1042                set_bit(FF_PERIODIC, dev->ffbit);
1043        }
1044        if (pidff->type_id[PID_SAW_DOWN]) {
1045                set_bit(FF_SAW_DOWN, dev->ffbit);
1046                set_bit(FF_PERIODIC, dev->ffbit);
1047        }
1048        if (pidff->type_id[PID_SPRING])
1049                set_bit(FF_SPRING, dev->ffbit);
1050        if (pidff->type_id[PID_DAMPER])
1051                set_bit(FF_DAMPER, dev->ffbit);
1052        if (pidff->type_id[PID_INERTIA])
1053                set_bit(FF_INERTIA, dev->ffbit);
1054        if (pidff->type_id[PID_FRICTION])
1055                set_bit(FF_FRICTION, dev->ffbit);
1056
1057        return 0;
1058
1059}
1060
1061#define PIDFF_FIND_FIELDS(name, report, strict) \
1062        pidff_find_fields(pidff->name, pidff_ ## name, \
1063                pidff->reports[report], \
1064                sizeof(pidff_ ## name), strict)
1065
1066/*
1067 * Fill and check the pidff_usages
1068 */
1069static int pidff_init_fields(struct pidff_device *pidff, struct input_dev *dev)
1070{
1071        int envelope_ok = 0;
1072
1073        if (PIDFF_FIND_FIELDS(set_effect, PID_SET_EFFECT, 1)) {
1074                hid_err(pidff->hid, "unknown set_effect report layout\n");
1075                return -ENODEV;
1076        }
1077
1078        PIDFF_FIND_FIELDS(block_load, PID_BLOCK_LOAD, 0);
1079        if (!pidff->block_load[PID_EFFECT_BLOCK_INDEX].value) {
1080                hid_err(pidff->hid, "unknown pid_block_load report layout\n");
1081                return -ENODEV;
1082        }
1083
1084        if (PIDFF_FIND_FIELDS(effect_operation, PID_EFFECT_OPERATION, 1)) {
1085                hid_err(pidff->hid, "unknown effect_operation report layout\n");
1086                return -ENODEV;
1087        }
1088
1089        if (PIDFF_FIND_FIELDS(block_free, PID_BLOCK_FREE, 1)) {
1090                hid_err(pidff->hid, "unknown pid_block_free report layout\n");
1091                return -ENODEV;
1092        }
1093
1094        if (!PIDFF_FIND_FIELDS(set_envelope, PID_SET_ENVELOPE, 1))
1095                envelope_ok = 1;
1096
1097        if (pidff_find_special_fields(pidff) || pidff_find_effects(pidff, dev))
1098                return -ENODEV;
1099
1100        if (!envelope_ok) {
1101                if (test_and_clear_bit(FF_CONSTANT, dev->ffbit))
1102                        hid_warn(pidff->hid,
1103                                 "has constant effect but no envelope\n");
1104                if (test_and_clear_bit(FF_RAMP, dev->ffbit))
1105                        hid_warn(pidff->hid,
1106                                 "has ramp effect but no envelope\n");
1107
1108                if (test_and_clear_bit(FF_PERIODIC, dev->ffbit))
1109                        hid_warn(pidff->hid,
1110                                 "has periodic effect but no envelope\n");
1111        }
1112
1113        if (test_bit(FF_CONSTANT, dev->ffbit) &&
1114            PIDFF_FIND_FIELDS(set_constant, PID_SET_CONSTANT, 1)) {
1115                hid_warn(pidff->hid, "unknown constant effect layout\n");
1116                clear_bit(FF_CONSTANT, dev->ffbit);
1117        }
1118
1119        if (test_bit(FF_RAMP, dev->ffbit) &&
1120            PIDFF_FIND_FIELDS(set_ramp, PID_SET_RAMP, 1)) {
1121                hid_warn(pidff->hid, "unknown ramp effect layout\n");
1122                clear_bit(FF_RAMP, dev->ffbit);
1123        }
1124
1125        if ((test_bit(FF_SPRING, dev->ffbit) ||
1126             test_bit(FF_DAMPER, dev->ffbit) ||
1127             test_bit(FF_FRICTION, dev->ffbit) ||
1128             test_bit(FF_INERTIA, dev->ffbit)) &&
1129            PIDFF_FIND_FIELDS(set_condition, PID_SET_CONDITION, 1)) {
1130                hid_warn(pidff->hid, "unknown condition effect layout\n");
1131                clear_bit(FF_SPRING, dev->ffbit);
1132                clear_bit(FF_DAMPER, dev->ffbit);
1133                clear_bit(FF_FRICTION, dev->ffbit);
1134                clear_bit(FF_INERTIA, dev->ffbit);
1135        }
1136
1137        if (test_bit(FF_PERIODIC, dev->ffbit) &&
1138            PIDFF_FIND_FIELDS(set_periodic, PID_SET_PERIODIC, 1)) {
1139                hid_warn(pidff->hid, "unknown periodic effect layout\n");
1140                clear_bit(FF_PERIODIC, dev->ffbit);
1141        }
1142
1143        PIDFF_FIND_FIELDS(pool, PID_POOL, 0);
1144
1145        if (!PIDFF_FIND_FIELDS(device_gain, PID_DEVICE_GAIN, 1))
1146                set_bit(FF_GAIN, dev->ffbit);
1147
1148        return 0;
1149}
1150
1151/*
1152 * Reset the device
1153 */
1154static void pidff_reset(struct pidff_device *pidff)
1155{
1156        struct hid_device *hid = pidff->hid;
1157        int i = 0;
1158
1159        pidff->device_control->value[0] = pidff->control_id[PID_RESET];
1160        /* We reset twice as sometimes hid_wait_io isn't waiting long enough */
1161        hid_hw_request(hid, pidff->reports[PID_DEVICE_CONTROL], HID_REQ_SET_REPORT);
1162        hid_hw_wait(hid);
1163        hid_hw_request(hid, pidff->reports[PID_DEVICE_CONTROL], HID_REQ_SET_REPORT);
1164        hid_hw_wait(hid);
1165
1166        pidff->device_control->value[0] =
1167                pidff->control_id[PID_ENABLE_ACTUATORS];
1168        hid_hw_request(hid, pidff->reports[PID_DEVICE_CONTROL], HID_REQ_SET_REPORT);
1169        hid_hw_wait(hid);
1170
1171        /* pool report is sometimes messed up, refetch it */
1172        hid_hw_request(hid, pidff->reports[PID_POOL], HID_REQ_GET_REPORT);
1173        hid_hw_wait(hid);
1174
1175        if (pidff->pool[PID_SIMULTANEOUS_MAX].value) {
1176                while (pidff->pool[PID_SIMULTANEOUS_MAX].value[0] < 2) {
1177                        if (i++ > 20) {
1178                                hid_warn(pidff->hid,
1179                                         "device reports %d simultaneous effects\n",
1180                                         pidff->pool[PID_SIMULTANEOUS_MAX].value[0]);
1181                                break;
1182                        }
1183                        hid_dbg(pidff->hid, "pid_pool requested again\n");
1184                        hid_hw_request(hid, pidff->reports[PID_POOL],
1185                                          HID_REQ_GET_REPORT);
1186                        hid_hw_wait(hid);
1187                }
1188        }
1189}
1190
1191/*
1192 * Test if autocenter modification is using the supported method
1193 */
1194static int pidff_check_autocenter(struct pidff_device *pidff,
1195                                  struct input_dev *dev)
1196{
1197        int error;
1198
1199        /*
1200         * Let's find out if autocenter modification is supported
1201         * Specification doesn't specify anything, so we request an
1202         * effect upload and cancel it immediately. If the approved
1203         * effect id was one above the minimum, then we assume the first
1204         * effect id is a built-in spring type effect used for autocenter
1205         */
1206
1207        error = pidff_request_effect_upload(pidff, 1);
1208        if (error) {
1209                hid_err(pidff->hid, "upload request failed\n");
1210                return error;
1211        }
1212
1213        if (pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0] ==
1214            pidff->block_load[PID_EFFECT_BLOCK_INDEX].field->logical_minimum + 1) {
1215                pidff_autocenter(pidff, 0xffff);
1216                set_bit(FF_AUTOCENTER, dev->ffbit);
1217        } else {
1218                hid_notice(pidff->hid,
1219                           "device has unknown autocenter control method\n");
1220        }
1221
1222        pidff_erase_pid(pidff,
1223                        pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0]);
1224
1225        return 0;
1226
1227}
1228
1229/*
1230 * Check if the device is PID and initialize it
1231 */
1232int hid_pidff_init(struct hid_device *hid)
1233{
1234        struct pidff_device *pidff;
1235        struct hid_input *hidinput = list_entry(hid->inputs.next,
1236                                                struct hid_input, list);
1237        struct input_dev *dev = hidinput->input;
1238        struct ff_device *ff;
1239        int max_effects;
1240        int error;
1241
1242        hid_dbg(hid, "starting pid init\n");
1243
1244        if (list_empty(&hid->report_enum[HID_OUTPUT_REPORT].report_list)) {
1245                hid_dbg(hid, "not a PID device, no output report\n");
1246                return -ENODEV;
1247        }
1248
1249        pidff = kzalloc(sizeof(*pidff), GFP_KERNEL);
1250        if (!pidff)
1251                return -ENOMEM;
1252
1253        pidff->hid = hid;
1254
1255        pidff_find_reports(hid, HID_OUTPUT_REPORT, pidff);
1256        pidff_find_reports(hid, HID_FEATURE_REPORT, pidff);
1257
1258        if (!pidff_reports_ok(pidff)) {
1259                hid_dbg(hid, "reports not ok, aborting\n");
1260                error = -ENODEV;
1261                goto fail;
1262        }
1263
1264        error = pidff_init_fields(pidff, dev);
1265        if (error)
1266                goto fail;
1267
1268        pidff_reset(pidff);
1269
1270        if (test_bit(FF_GAIN, dev->ffbit)) {
1271                pidff_set(&pidff->device_gain[PID_DEVICE_GAIN_FIELD], 0xffff);
1272                hid_hw_request(hid, pidff->reports[PID_DEVICE_GAIN],
1273                                     HID_REQ_SET_REPORT);
1274        }
1275
1276        error = pidff_check_autocenter(pidff, dev);
1277        if (error)
1278                goto fail;
1279
1280        max_effects =
1281            pidff->block_load[PID_EFFECT_BLOCK_INDEX].field->logical_maximum -
1282            pidff->block_load[PID_EFFECT_BLOCK_INDEX].field->logical_minimum +
1283            1;
1284        hid_dbg(hid, "max effects is %d\n", max_effects);
1285
1286        if (max_effects > PID_EFFECTS_MAX)
1287                max_effects = PID_EFFECTS_MAX;
1288
1289        if (pidff->pool[PID_SIMULTANEOUS_MAX].value)
1290                hid_dbg(hid, "max simultaneous effects is %d\n",
1291                        pidff->pool[PID_SIMULTANEOUS_MAX].value[0]);
1292
1293        if (pidff->pool[PID_RAM_POOL_SIZE].value)
1294                hid_dbg(hid, "device memory size is %d bytes\n",
1295                        pidff->pool[PID_RAM_POOL_SIZE].value[0]);
1296
1297        if (pidff->pool[PID_DEVICE_MANAGED_POOL].value &&
1298            pidff->pool[PID_DEVICE_MANAGED_POOL].value[0] == 0) {
1299                hid_notice(hid,
1300                           "device does not support device managed pool\n");
1301                goto fail;
1302        }
1303
1304        error = input_ff_create(dev, max_effects);
1305        if (error)
1306                goto fail;
1307
1308        ff = dev->ff;
1309        ff->private = pidff;
1310        ff->upload = pidff_upload_effect;
1311        ff->erase = pidff_erase_effect;
1312        ff->set_gain = pidff_set_gain;
1313        ff->set_autocenter = pidff_set_autocenter;
1314        ff->playback = pidff_playback;
1315
1316        hid_info(dev, "Force feedback for USB HID PID devices by Anssi Hannula <anssi.hannula@gmail.com>\n");
1317
1318        return 0;
1319
1320 fail:
1321        kfree(pidff);
1322        return error;
1323}
1324