LCOV - code coverage report
Current view: top level - drivers/input/mouse - trackpoint.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 156 0.0 %
Date: 2023-03-27 20:00:47 Functions: 0 12 0.0 %

          Line data    Source code
       1             : // SPDX-License-Identifier: GPL-2.0-only
       2             : /*
       3             :  * Stephen Evanchik <evanchsa@gmail.com>
       4             :  *
       5             :  * Trademarks are the property of their respective owners.
       6             :  */
       7             : 
       8             : #include <linux/slab.h>
       9             : #include <linux/delay.h>
      10             : #include <linux/serio.h>
      11             : #include <linux/module.h>
      12             : #include <linux/input.h>
      13             : #include <linux/libps2.h>
      14             : #include <linux/proc_fs.h>
      15             : #include <linux/uaccess.h>
      16             : #include "psmouse.h"
      17             : #include "trackpoint.h"
      18             : 
      19             : static const char * const trackpoint_variants[] = {
      20             :         [TP_VARIANT_IBM]                = "IBM",
      21             :         [TP_VARIANT_ALPS]               = "ALPS",
      22             :         [TP_VARIANT_ELAN]               = "Elan",
      23             :         [TP_VARIANT_NXP]                = "NXP",
      24             :         [TP_VARIANT_JYT_SYNAPTICS]      = "JYT_Synaptics",
      25             :         [TP_VARIANT_SYNAPTICS]          = "Synaptics",
      26             : };
      27             : 
      28             : /*
      29             :  * Power-on Reset: Resets all trackpoint parameters, including RAM values,
      30             :  * to defaults.
      31             :  * Returns zero on success, non-zero on failure.
      32             :  */
      33           0 : static int trackpoint_power_on_reset(struct ps2dev *ps2dev)
      34             : {
      35           0 :         u8 param[2] = { TP_POR };
      36             :         int err;
      37             : 
      38           0 :         err = ps2_command(ps2dev, param, MAKE_PS2_CMD(1, 2, TP_COMMAND));
      39           0 :         if (err)
      40             :                 return err;
      41             : 
      42             :         /* Check for success response -- 0xAA00 */
      43           0 :         if (param[0] != 0xAA || param[1] != 0x00)
      44             :                 return -ENODEV;
      45             : 
      46           0 :         return 0;
      47             : }
      48             : 
      49             : /*
      50             :  * Device IO: read, write and toggle bit
      51             :  */
      52             : static int trackpoint_read(struct ps2dev *ps2dev, u8 loc, u8 *results)
      53             : {
      54           0 :         results[0] = loc;
      55             : 
      56           0 :         return ps2_command(ps2dev, results, MAKE_PS2_CMD(1, 1, TP_COMMAND));
      57             : }
      58             : 
      59             : static int trackpoint_write(struct ps2dev *ps2dev, u8 loc, u8 val)
      60             : {
      61           0 :         u8 param[3] = { TP_WRITE_MEM, loc, val };
      62             : 
      63           0 :         return ps2_command(ps2dev, param, MAKE_PS2_CMD(3, 0, TP_COMMAND));
      64             : }
      65             : 
      66             : static int trackpoint_toggle_bit(struct ps2dev *ps2dev, u8 loc, u8 mask)
      67             : {
      68           0 :         u8 param[3] = { TP_TOGGLE, loc, mask };
      69             : 
      70             :         /* Bad things will happen if the loc param isn't in this range */
      71           0 :         if (loc < 0x20 || loc >= 0x2F)
      72             :                 return -EINVAL;
      73             : 
      74           0 :         return ps2_command(ps2dev, param, MAKE_PS2_CMD(3, 0, TP_COMMAND));
      75             : }
      76             : 
      77           0 : static int trackpoint_update_bit(struct ps2dev *ps2dev,
      78             :                                  u8 loc, u8 mask, u8 value)
      79             : {
      80             :         int retval;
      81             :         u8 data;
      82             : 
      83           0 :         retval = trackpoint_read(ps2dev, loc, &data);
      84           0 :         if (retval)
      85             :                 return retval;
      86             : 
      87           0 :         if (((data & mask) == mask) != !!value)
      88           0 :                 retval = trackpoint_toggle_bit(ps2dev, loc, mask);
      89             : 
      90             :         return retval;
      91             : }
      92             : 
      93             : /*
      94             :  * Trackpoint-specific attributes
      95             :  */
      96             : struct trackpoint_attr_data {
      97             :         size_t field_offset;
      98             :         u8 command;
      99             :         u8 mask;
     100             :         bool inverted;
     101             :         u8 power_on_default;
     102             : };
     103             : 
     104           0 : static ssize_t trackpoint_show_int_attr(struct psmouse *psmouse,
     105             :                                         void *data, char *buf)
     106             : {
     107           0 :         struct trackpoint_data *tp = psmouse->private;
     108           0 :         struct trackpoint_attr_data *attr = data;
     109           0 :         u8 value = *(u8 *)((void *)tp + attr->field_offset);
     110             : 
     111           0 :         if (attr->inverted)
     112           0 :                 value = !value;
     113             : 
     114           0 :         return sprintf(buf, "%u\n", value);
     115             : }
     116             : 
     117           0 : static ssize_t trackpoint_set_int_attr(struct psmouse *psmouse, void *data,
     118             :                                         const char *buf, size_t count)
     119             : {
     120           0 :         struct trackpoint_data *tp = psmouse->private;
     121           0 :         struct trackpoint_attr_data *attr = data;
     122           0 :         u8 *field = (void *)tp + attr->field_offset;
     123             :         u8 value;
     124             :         int err;
     125             : 
     126           0 :         err = kstrtou8(buf, 10, &value);
     127           0 :         if (err)
     128           0 :                 return err;
     129             : 
     130           0 :         *field = value;
     131           0 :         err = trackpoint_write(&psmouse->ps2dev, attr->command, value);
     132             : 
     133           0 :         return err ?: count;
     134             : }
     135             : 
     136             : #define TRACKPOINT_INT_ATTR(_name, _command, _default)                          \
     137             :         static struct trackpoint_attr_data trackpoint_attr_##_name = {          \
     138             :                 .field_offset = offsetof(struct trackpoint_data, _name),        \
     139             :                 .command = _command,                                            \
     140             :                 .power_on_default = _default,                                   \
     141             :         };                                                                      \
     142             :         PSMOUSE_DEFINE_ATTR(_name, S_IWUSR | S_IRUGO,                           \
     143             :                             &trackpoint_attr_##_name,                               \
     144             :                             trackpoint_show_int_attr, trackpoint_set_int_attr)
     145             : 
     146           0 : static ssize_t trackpoint_set_bit_attr(struct psmouse *psmouse, void *data,
     147             :                                         const char *buf, size_t count)
     148             : {
     149           0 :         struct trackpoint_data *tp = psmouse->private;
     150           0 :         struct trackpoint_attr_data *attr = data;
     151           0 :         bool *field = (void *)tp + attr->field_offset;
     152             :         bool value;
     153             :         int err;
     154             : 
     155           0 :         err = kstrtobool(buf, &value);
     156           0 :         if (err)
     157           0 :                 return err;
     158             : 
     159           0 :         if (attr->inverted)
     160           0 :                 value = !value;
     161             : 
     162           0 :         if (*field != value) {
     163           0 :                 *field = value;
     164           0 :                 err = trackpoint_toggle_bit(&psmouse->ps2dev,
     165           0 :                                             attr->command, attr->mask);
     166             :         }
     167             : 
     168           0 :         return err ?: count;
     169             : }
     170             : 
     171             : 
     172             : #define TRACKPOINT_BIT_ATTR(_name, _command, _mask, _inv, _default)     \
     173             : static struct trackpoint_attr_data trackpoint_attr_##_name = {          \
     174             :         .field_offset           = offsetof(struct trackpoint_data,      \
     175             :                                            _name),                      \
     176             :         .command                = _command,                             \
     177             :         .mask                   = _mask,                                \
     178             :         .inverted               = _inv,                                 \
     179             :         .power_on_default       = _default,                             \
     180             :         };                                                              \
     181             : PSMOUSE_DEFINE_ATTR(_name, S_IWUSR | S_IRUGO,                           \
     182             :                     &trackpoint_attr_##_name,                               \
     183             :                     trackpoint_show_int_attr, trackpoint_set_bit_attr)
     184             : 
     185             : TRACKPOINT_INT_ATTR(sensitivity, TP_SENS, TP_DEF_SENS);
     186             : TRACKPOINT_INT_ATTR(speed, TP_SPEED, TP_DEF_SPEED);
     187             : TRACKPOINT_INT_ATTR(inertia, TP_INERTIA, TP_DEF_INERTIA);
     188             : TRACKPOINT_INT_ATTR(reach, TP_REACH, TP_DEF_REACH);
     189             : TRACKPOINT_INT_ATTR(draghys, TP_DRAGHYS, TP_DEF_DRAGHYS);
     190             : TRACKPOINT_INT_ATTR(mindrag, TP_MINDRAG, TP_DEF_MINDRAG);
     191             : TRACKPOINT_INT_ATTR(thresh, TP_THRESH, TP_DEF_THRESH);
     192             : TRACKPOINT_INT_ATTR(upthresh, TP_UP_THRESH, TP_DEF_UP_THRESH);
     193             : TRACKPOINT_INT_ATTR(ztime, TP_Z_TIME, TP_DEF_Z_TIME);
     194             : TRACKPOINT_INT_ATTR(jenks, TP_JENKS_CURV, TP_DEF_JENKS_CURV);
     195             : TRACKPOINT_INT_ATTR(drift_time, TP_DRIFT_TIME, TP_DEF_DRIFT_TIME);
     196             : 
     197             : TRACKPOINT_BIT_ATTR(press_to_select, TP_TOGGLE_PTSON, TP_MASK_PTSON, false,
     198             :                     TP_DEF_PTSON);
     199             : TRACKPOINT_BIT_ATTR(skipback, TP_TOGGLE_SKIPBACK, TP_MASK_SKIPBACK, false,
     200             :                     TP_DEF_SKIPBACK);
     201             : TRACKPOINT_BIT_ATTR(ext_dev, TP_TOGGLE_EXT_DEV, TP_MASK_EXT_DEV, true,
     202             :                     TP_DEF_EXT_DEV);
     203             : 
     204             : static bool trackpoint_is_attr_available(struct psmouse *psmouse,
     205             :                                          struct attribute *attr)
     206             : {
     207           0 :         struct trackpoint_data *tp = psmouse->private;
     208             : 
     209           0 :         return tp->variant_id == TP_VARIANT_IBM ||
     210           0 :                 attr == &psmouse_attr_sensitivity.dattr.attr ||
     211             :                 attr == &psmouse_attr_press_to_select.dattr.attr;
     212             : }
     213             : 
     214           0 : static umode_t trackpoint_is_attr_visible(struct kobject *kobj,
     215             :                                           struct attribute *attr, int n)
     216             : {
     217           0 :         struct device *dev = kobj_to_dev(kobj);
     218           0 :         struct serio *serio = to_serio_port(dev);
     219           0 :         struct psmouse *psmouse = serio_get_drvdata(serio);
     220             : 
     221           0 :         return trackpoint_is_attr_available(psmouse, attr) ? attr->mode : 0;
     222             : }
     223             : 
     224             : static struct attribute *trackpoint_attrs[] = {
     225             :         &psmouse_attr_sensitivity.dattr.attr,
     226             :         &psmouse_attr_speed.dattr.attr,
     227             :         &psmouse_attr_inertia.dattr.attr,
     228             :         &psmouse_attr_reach.dattr.attr,
     229             :         &psmouse_attr_draghys.dattr.attr,
     230             :         &psmouse_attr_mindrag.dattr.attr,
     231             :         &psmouse_attr_thresh.dattr.attr,
     232             :         &psmouse_attr_upthresh.dattr.attr,
     233             :         &psmouse_attr_ztime.dattr.attr,
     234             :         &psmouse_attr_jenks.dattr.attr,
     235             :         &psmouse_attr_drift_time.dattr.attr,
     236             :         &psmouse_attr_press_to_select.dattr.attr,
     237             :         &psmouse_attr_skipback.dattr.attr,
     238             :         &psmouse_attr_ext_dev.dattr.attr,
     239             :         NULL
     240             : };
     241             : 
     242             : static struct attribute_group trackpoint_attr_group = {
     243             :         .is_visible     = trackpoint_is_attr_visible,
     244             :         .attrs          = trackpoint_attrs,
     245             : };
     246             : 
     247             : #define TRACKPOINT_UPDATE(_power_on, _psmouse, _tp, _name)              \
     248             : do {                                                                    \
     249             :         struct trackpoint_attr_data *_attr = &trackpoint_attr_##_name;      \
     250             :                                                                         \
     251             :         if ((!_power_on || _tp->_name != _attr->power_on_default) &&      \
     252             :             trackpoint_is_attr_available(_psmouse,                      \
     253             :                                 &psmouse_attr_##_name.dattr.attr)) {        \
     254             :                 if (!_attr->mask)                                    \
     255             :                         trackpoint_write(&_psmouse->ps2dev,              \
     256             :                                          _attr->command, _tp->_name);     \
     257             :                 else                                                    \
     258             :                         trackpoint_update_bit(&_psmouse->ps2dev, \
     259             :                                         _attr->command, _attr->mask,      \
     260             :                                         _tp->_name);                 \
     261             :         }                                                               \
     262             : } while (0)
     263             : 
     264             : #define TRACKPOINT_SET_POWER_ON_DEFAULT(_tp, _name)                     \
     265             : do {                                                                    \
     266             :         _tp->_name = trackpoint_attr_##_name.power_on_default;               \
     267             : } while (0)
     268             : 
     269           0 : static int trackpoint_start_protocol(struct psmouse *psmouse,
     270             :                                      u8 *variant_id, u8 *firmware_id)
     271             : {
     272           0 :         u8 param[2] = { 0 };
     273             :         int error;
     274             : 
     275           0 :         error = ps2_command(&psmouse->ps2dev,
     276             :                             param, MAKE_PS2_CMD(0, 2, TP_READ_ID));
     277           0 :         if (error)
     278             :                 return error;
     279             : 
     280           0 :         switch (param[0]) {
     281             :         case TP_VARIANT_IBM:
     282             :         case TP_VARIANT_ALPS:
     283             :         case TP_VARIANT_ELAN:
     284             :         case TP_VARIANT_NXP:
     285             :         case TP_VARIANT_JYT_SYNAPTICS:
     286             :         case TP_VARIANT_SYNAPTICS:
     287           0 :                 if (variant_id)
     288           0 :                         *variant_id = param[0];
     289           0 :                 if (firmware_id)
     290           0 :                         *firmware_id = param[1];
     291             :                 return 0;
     292             :         }
     293             : 
     294             :         return -ENODEV;
     295             : }
     296             : 
     297             : /*
     298             :  * Write parameters to trackpad.
     299             :  * in_power_on_state: Set to true if TP is in default / power-on state (ex. if
     300             :  *                    power-on reset was run). If so, values will only be
     301             :  *                    written to TP if they differ from power-on default.
     302             :  */
     303           0 : static int trackpoint_sync(struct psmouse *psmouse, bool in_power_on_state)
     304             : {
     305           0 :         struct trackpoint_data *tp = psmouse->private;
     306             : 
     307           0 :         if (!in_power_on_state && tp->variant_id == TP_VARIANT_IBM) {
     308             :                 /*
     309             :                  * Disable features that may make device unusable
     310             :                  * with this driver.
     311             :                  */
     312           0 :                 trackpoint_update_bit(&psmouse->ps2dev, TP_TOGGLE_TWOHAND,
     313             :                                       TP_MASK_TWOHAND, TP_DEF_TWOHAND);
     314             : 
     315           0 :                 trackpoint_update_bit(&psmouse->ps2dev, TP_TOGGLE_SOURCE_TAG,
     316             :                                       TP_MASK_SOURCE_TAG, TP_DEF_SOURCE_TAG);
     317             : 
     318           0 :                 trackpoint_update_bit(&psmouse->ps2dev, TP_TOGGLE_MB,
     319             :                                       TP_MASK_MB, TP_DEF_MB);
     320             :         }
     321             : 
     322             :         /*
     323             :          * These properties can be changed in this driver. Only
     324             :          * configure them if the values are non-default or if the TP is in
     325             :          * an unknown state.
     326             :          */
     327           0 :         TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, sensitivity);
     328           0 :         TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, inertia);
     329           0 :         TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, speed);
     330           0 :         TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, reach);
     331           0 :         TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, draghys);
     332           0 :         TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, mindrag);
     333           0 :         TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, thresh);
     334           0 :         TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, upthresh);
     335           0 :         TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, ztime);
     336           0 :         TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, jenks);
     337           0 :         TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, drift_time);
     338             : 
     339             :         /* toggles */
     340           0 :         TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, press_to_select);
     341           0 :         TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, skipback);
     342           0 :         TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, ext_dev);
     343             : 
     344           0 :         return 0;
     345             : }
     346             : 
     347           0 : static void trackpoint_defaults(struct trackpoint_data *tp)
     348             : {
     349           0 :         TRACKPOINT_SET_POWER_ON_DEFAULT(tp, sensitivity);
     350           0 :         TRACKPOINT_SET_POWER_ON_DEFAULT(tp, speed);
     351           0 :         TRACKPOINT_SET_POWER_ON_DEFAULT(tp, reach);
     352           0 :         TRACKPOINT_SET_POWER_ON_DEFAULT(tp, draghys);
     353           0 :         TRACKPOINT_SET_POWER_ON_DEFAULT(tp, mindrag);
     354           0 :         TRACKPOINT_SET_POWER_ON_DEFAULT(tp, thresh);
     355           0 :         TRACKPOINT_SET_POWER_ON_DEFAULT(tp, upthresh);
     356           0 :         TRACKPOINT_SET_POWER_ON_DEFAULT(tp, ztime);
     357           0 :         TRACKPOINT_SET_POWER_ON_DEFAULT(tp, jenks);
     358           0 :         TRACKPOINT_SET_POWER_ON_DEFAULT(tp, drift_time);
     359           0 :         TRACKPOINT_SET_POWER_ON_DEFAULT(tp, inertia);
     360             : 
     361             :         /* toggles */
     362           0 :         TRACKPOINT_SET_POWER_ON_DEFAULT(tp, press_to_select);
     363           0 :         TRACKPOINT_SET_POWER_ON_DEFAULT(tp, skipback);
     364           0 :         TRACKPOINT_SET_POWER_ON_DEFAULT(tp, ext_dev);
     365           0 : }
     366             : 
     367           0 : static void trackpoint_disconnect(struct psmouse *psmouse)
     368             : {
     369           0 :         device_remove_group(&psmouse->ps2dev.serio->dev,
     370             :                             &trackpoint_attr_group);
     371             : 
     372           0 :         kfree(psmouse->private);
     373           0 :         psmouse->private = NULL;
     374           0 : }
     375             : 
     376           0 : static int trackpoint_reconnect(struct psmouse *psmouse)
     377             : {
     378           0 :         struct trackpoint_data *tp = psmouse->private;
     379             :         int error;
     380             :         bool was_reset;
     381             : 
     382           0 :         error = trackpoint_start_protocol(psmouse, NULL, NULL);
     383           0 :         if (error)
     384             :                 return error;
     385             : 
     386           0 :         was_reset = tp->variant_id == TP_VARIANT_IBM &&
     387           0 :                     trackpoint_power_on_reset(&psmouse->ps2dev) == 0;
     388             : 
     389           0 :         error = trackpoint_sync(psmouse, was_reset);
     390           0 :         if (error)
     391             :                 return error;
     392             : 
     393           0 :         return 0;
     394             : }
     395             : 
     396           0 : int trackpoint_detect(struct psmouse *psmouse, bool set_properties)
     397             : {
     398           0 :         struct ps2dev *ps2dev = &psmouse->ps2dev;
     399             :         struct trackpoint_data *tp;
     400             :         u8 variant_id;
     401             :         u8 firmware_id;
     402             :         u8 button_info;
     403             :         int error;
     404             : 
     405           0 :         error = trackpoint_start_protocol(psmouse, &variant_id, &firmware_id);
     406           0 :         if (error)
     407             :                 return error;
     408             : 
     409           0 :         if (!set_properties)
     410             :                 return 0;
     411             : 
     412           0 :         tp = kzalloc(sizeof(*tp), GFP_KERNEL);
     413           0 :         if (!tp)
     414             :                 return -ENOMEM;
     415             : 
     416           0 :         trackpoint_defaults(tp);
     417           0 :         tp->variant_id = variant_id;
     418           0 :         tp->firmware_id = firmware_id;
     419             : 
     420           0 :         psmouse->private = tp;
     421             : 
     422           0 :         psmouse->vendor = trackpoint_variants[variant_id];
     423           0 :         psmouse->name = "TrackPoint";
     424             : 
     425           0 :         psmouse->reconnect = trackpoint_reconnect;
     426           0 :         psmouse->disconnect = trackpoint_disconnect;
     427             : 
     428           0 :         if (variant_id != TP_VARIANT_IBM) {
     429             :                 /* Newer variants do not support extended button query. */
     430           0 :                 button_info = 0x33;
     431             :         } else {
     432           0 :                 error = trackpoint_read(ps2dev, TP_EXT_BTN, &button_info);
     433           0 :                 if (error) {
     434           0 :                         psmouse_warn(psmouse,
     435             :                                      "failed to get extended button data, assuming 3 buttons\n");
     436           0 :                         button_info = 0x33;
     437           0 :                 } else if (!button_info) {
     438           0 :                         psmouse_warn(psmouse,
     439             :                                      "got 0 in extended button data, assuming 3 buttons\n");
     440           0 :                         button_info = 0x33;
     441             :                 }
     442             :         }
     443             : 
     444           0 :         if ((button_info & 0x0f) >= 3)
     445           0 :                 input_set_capability(psmouse->dev, EV_KEY, BTN_MIDDLE);
     446             : 
     447           0 :         __set_bit(INPUT_PROP_POINTER, psmouse->dev->propbit);
     448           0 :         __set_bit(INPUT_PROP_POINTING_STICK, psmouse->dev->propbit);
     449             : 
     450           0 :         if (variant_id != TP_VARIANT_IBM ||
     451           0 :             trackpoint_power_on_reset(ps2dev) != 0) {
     452             :                 /*
     453             :                  * Write defaults to TP if we did not reset the trackpoint.
     454             :                  */
     455           0 :                 trackpoint_sync(psmouse, false);
     456             :         }
     457             : 
     458           0 :         error = device_add_group(&ps2dev->serio->dev, &trackpoint_attr_group);
     459           0 :         if (error) {
     460           0 :                 psmouse_err(psmouse,
     461             :                             "failed to create sysfs attributes, error: %d\n",
     462             :                             error);
     463           0 :                 kfree(psmouse->private);
     464           0 :                 psmouse->private = NULL;
     465           0 :                 return -1;
     466             :         }
     467             : 
     468           0 :         psmouse_info(psmouse,
     469             :                      "%s TrackPoint firmware: 0x%02x, buttons: %d/%d\n",
     470             :                      psmouse->vendor, firmware_id,
     471             :                      (button_info & 0xf0) >> 4, button_info & 0x0f);
     472             : 
     473           0 :         return 0;
     474             : }
     475             : 

Generated by: LCOV version 1.14