LCOV - code coverage report
Current view: top level - drivers/hid/usbhid - hid-pidff.c (source / functions) Hit Total Coverage
Test: Real Lines: 26 477 5.5 %
Date: 2020-10-17 15:46:16 Functions: 0 26 0.0 %
Legend: Neither, QEMU, Real, Both Branches: 0 0 -

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

Generated by: LCOV version 1.14