LCOV - code coverage report
Current view: top level - drivers/input/mouse - cypress_ps2.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 258 0.0 %
Date: 2014-02-18 Functions: 0 21 0.0 %
Branches: 0 174 0.0 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * Cypress Trackpad PS/2 mouse driver
       3                 :            :  *
       4                 :            :  * Copyright (c) 2012 Cypress Semiconductor Corporation.
       5                 :            :  *
       6                 :            :  * Author:
       7                 :            :  *   Dudley Du <dudl@cypress.com>
       8                 :            :  *
       9                 :            :  * Additional contributors include:
      10                 :            :  *   Kamal Mostafa <kamal@canonical.com>
      11                 :            :  *   Kyle Fazzari <git@status.e4ward.com>
      12                 :            :  *
      13                 :            :  * This program is free software; you can redistribute it and/or modify it
      14                 :            :  * under the terms of the GNU General Public License version 2 as published by
      15                 :            :  * the Free Software Foundation.
      16                 :            :  */
      17                 :            : 
      18                 :            : #include <linux/init.h>
      19                 :            : #include <linux/module.h>
      20                 :            : #include <linux/kernel.h>
      21                 :            : #include <linux/slab.h>
      22                 :            : #include <linux/serio.h>
      23                 :            : #include <linux/libps2.h>
      24                 :            : #include <linux/input.h>
      25                 :            : #include <linux/input/mt.h>
      26                 :            : #include <linux/sched.h>
      27                 :            : #include <linux/wait.h>
      28                 :            : 
      29                 :            : #include "cypress_ps2.h"
      30                 :            : 
      31                 :            : #undef CYTP_DEBUG_VERBOSE  /* define this and DEBUG for more verbose dump */
      32                 :            : 
      33                 :            : static void cypress_set_packet_size(struct psmouse *psmouse, unsigned int n)
      34                 :            : {
      35                 :            :         struct cytp_data *cytp = psmouse->private;
      36                 :          0 :         cytp->pkt_size = n;
      37                 :            : }
      38                 :            : 
      39                 :            : static const unsigned char cytp_rate[] = {10, 20, 40, 60, 100, 200};
      40                 :            : static const unsigned char cytp_resolution[] = {0x00, 0x01, 0x02, 0x03};
      41                 :            : 
      42                 :          0 : static int cypress_ps2_sendbyte(struct psmouse *psmouse, int value)
      43                 :            : {
      44                 :          0 :         struct ps2dev *ps2dev = &psmouse->ps2dev;
      45                 :            : 
      46         [ #  # ]:          0 :         if (ps2_sendbyte(ps2dev, value & 0xff, CYTP_CMD_TIMEOUT) < 0) {
      47                 :            :                 psmouse_dbg(psmouse,
      48                 :            :                                 "sending command 0x%02x failed, resp 0x%02x\n",
      49                 :            :                                 value & 0xff, ps2dev->nak);
      50         [ #  # ]:          0 :                 if (ps2dev->nak == CYTP_PS2_RETRY)
      51                 :            :                         return CYTP_PS2_RETRY;
      52                 :            :                 else
      53                 :          0 :                         return CYTP_PS2_ERROR;
      54                 :            :         }
      55                 :            : 
      56                 :            : #ifdef CYTP_DEBUG_VERBOSE
      57                 :            :         psmouse_dbg(psmouse, "sending command 0x%02x succeeded, resp 0xfa\n",
      58                 :            :                         value & 0xff);
      59                 :            : #endif
      60                 :            : 
      61                 :            :         return 0;
      62                 :            : }
      63                 :            : 
      64                 :          0 : static int cypress_ps2_ext_cmd(struct psmouse *psmouse, unsigned short cmd,
      65                 :            :                                unsigned char data)
      66                 :            : {
      67                 :          0 :         struct ps2dev *ps2dev = &psmouse->ps2dev;
      68                 :            :         int tries = CYTP_PS2_CMD_TRIES;
      69                 :            :         int rc;
      70                 :            : 
      71                 :          0 :         ps2_begin_command(ps2dev);
      72                 :            : 
      73                 :            :         do {
      74                 :            :                 /*
      75                 :            :                  * Send extension command byte (0xE8 or 0xF3).
      76                 :            :                  * If sending the command fails, send recovery command
      77                 :            :                  * to make the device return to the ready state.
      78                 :            :                  */
      79                 :          0 :                 rc = cypress_ps2_sendbyte(psmouse, cmd & 0xff);
      80         [ #  # ]:          0 :                 if (rc == CYTP_PS2_RETRY) {
      81                 :          0 :                         rc = cypress_ps2_sendbyte(psmouse, 0x00);
      82         [ #  # ]:          0 :                         if (rc == CYTP_PS2_RETRY)
      83                 :          0 :                                 rc = cypress_ps2_sendbyte(psmouse, 0x0a);
      84                 :            :                 }
      85         [ #  # ]:          0 :                 if (rc == CYTP_PS2_ERROR)
      86                 :          0 :                         continue;
      87                 :            : 
      88                 :          0 :                 rc = cypress_ps2_sendbyte(psmouse, data);
      89         [ #  # ]:          0 :                 if (rc == CYTP_PS2_RETRY)
      90                 :          0 :                         rc = cypress_ps2_sendbyte(psmouse, data);
      91         [ #  # ]:          0 :                 if (rc == CYTP_PS2_ERROR)
      92                 :          0 :                         continue;
      93                 :            :                 else
      94                 :            :                         break;
      95         [ #  # ]:          0 :         } while (--tries > 0);
      96                 :            : 
      97                 :          0 :         ps2_end_command(ps2dev);
      98                 :            : 
      99                 :          0 :         return rc;
     100                 :            : }
     101                 :            : 
     102                 :          0 : static int cypress_ps2_read_cmd_status(struct psmouse *psmouse,
     103                 :            :                                        unsigned char cmd,
     104                 :            :                                        unsigned char *param)
     105                 :            : {
     106                 :            :         int rc;
     107                 :            :         struct ps2dev *ps2dev = &psmouse->ps2dev;
     108                 :            :         enum psmouse_state old_state;
     109                 :            :         int pktsize;
     110                 :            : 
     111                 :          0 :         ps2_begin_command(&psmouse->ps2dev);
     112                 :            : 
     113                 :          0 :         old_state = psmouse->state;
     114                 :          0 :         psmouse->state = PSMOUSE_CMD_MODE;
     115                 :          0 :         psmouse->pktcnt = 0;
     116                 :            : 
     117         [ #  # ]:          0 :         pktsize = (cmd == CYTP_CMD_READ_TP_METRICS) ? 8 : 3;
     118         [ #  # ]:          0 :         memset(param, 0, pktsize);
     119                 :            : 
     120                 :          0 :         rc = cypress_ps2_sendbyte(psmouse, 0xe9);
     121         [ #  # ]:          0 :         if (rc < 0)
     122                 :            :                 goto out;
     123                 :            : 
     124 [ #  # ][ #  # ]:          0 :         wait_event_timeout(ps2dev->wait,
         [ #  # ][ #  # ]
     125                 :            :                         (psmouse->pktcnt >= pktsize),
     126                 :            :                         msecs_to_jiffies(CYTP_CMD_TIMEOUT));
     127                 :            : 
     128                 :          0 :         memcpy(param, psmouse->packet, pktsize);
     129                 :            : 
     130                 :            :         psmouse_dbg(psmouse, "Command 0x%02x response data (0x): %*ph\n",
     131                 :            :                         cmd, pktsize, param);
     132                 :            : 
     133                 :            : out:
     134                 :          0 :         psmouse->state = old_state;
     135                 :          0 :         psmouse->pktcnt = 0;
     136                 :            : 
     137                 :          0 :         ps2_end_command(&psmouse->ps2dev);
     138                 :            : 
     139                 :          0 :         return rc;
     140                 :            : }
     141                 :            : 
     142                 :          0 : static bool cypress_verify_cmd_state(struct psmouse *psmouse,
     143                 :            :                                      unsigned char cmd, unsigned char *param)
     144                 :            : {
     145                 :            :         bool rate_match = false;
     146                 :            :         bool resolution_match = false;
     147                 :            :         int i;
     148                 :            : 
     149                 :            :         /* callers will do further checking. */
     150         [ #  # ]:          0 :         if (cmd == CYTP_CMD_READ_CYPRESS_ID ||
     151         [ #  # ]:          0 :             cmd == CYTP_CMD_STANDARD_MODE ||
     152                 :            :             cmd == CYTP_CMD_READ_TP_METRICS)
     153                 :            :                 return true;
     154                 :            : 
     155 [ #  # ][ #  # ]:          0 :         if ((~param[0] & DFLT_RESP_BITS_VALID) == DFLT_RESP_BITS_VALID &&
     156                 :          0 :             (param[0] & DFLT_RESP_BIT_MODE) == DFLT_RESP_STREAM_MODE) {
     157         [ #  # ]:          0 :                 for (i = 0; i < sizeof(cytp_resolution); i++)
     158         [ #  # ]:          0 :                         if (cytp_resolution[i] == param[1])
     159                 :            :                                 resolution_match = true;
     160                 :            : 
     161         [ #  # ]:          0 :                 for (i = 0; i < sizeof(cytp_rate); i++)
     162         [ #  # ]:          0 :                         if (cytp_rate[i] == param[2])
     163                 :            :                                 rate_match = true;
     164                 :            : 
     165         [ #  # ]:          0 :                 if (resolution_match && rate_match)
     166                 :            :                         return true;
     167                 :            :         }
     168                 :            : 
     169                 :            :         psmouse_dbg(psmouse, "verify cmd state failed.\n");
     170                 :            :         return false;
     171                 :            : }
     172                 :            : 
     173                 :          0 : static int cypress_send_ext_cmd(struct psmouse *psmouse, unsigned char cmd,
     174                 :            :                                 unsigned char *param)
     175                 :            : {
     176                 :            :         int tries = CYTP_PS2_CMD_TRIES;
     177                 :            :         int rc;
     178                 :            : 
     179                 :            :         psmouse_dbg(psmouse, "send extension cmd 0x%02x, [%d %d %d %d]\n",
     180                 :            :                  cmd, DECODE_CMD_AA(cmd), DECODE_CMD_BB(cmd),
     181                 :            :                  DECODE_CMD_CC(cmd), DECODE_CMD_DD(cmd));
     182                 :            : 
     183                 :            :         do {
     184                 :          0 :                 cypress_ps2_ext_cmd(psmouse,
     185                 :            :                                     PSMOUSE_CMD_SETRES, DECODE_CMD_DD(cmd));
     186                 :          0 :                 cypress_ps2_ext_cmd(psmouse,
     187                 :            :                                     PSMOUSE_CMD_SETRES, DECODE_CMD_CC(cmd));
     188                 :          0 :                 cypress_ps2_ext_cmd(psmouse,
     189                 :            :                                     PSMOUSE_CMD_SETRES, DECODE_CMD_BB(cmd));
     190                 :          0 :                 cypress_ps2_ext_cmd(psmouse,
     191                 :            :                                     PSMOUSE_CMD_SETRES, DECODE_CMD_AA(cmd));
     192                 :            : 
     193                 :          0 :                 rc = cypress_ps2_read_cmd_status(psmouse, cmd, param);
     194         [ #  # ]:          0 :                 if (rc)
     195                 :          0 :                         continue;
     196                 :            : 
     197         [ #  # ]:          0 :                 if (cypress_verify_cmd_state(psmouse, cmd, param))
     198                 :            :                         return 0;
     199                 :            : 
     200         [ #  # ]:          0 :         } while (--tries > 0);
     201                 :            : 
     202                 :            :         return -EIO;
     203                 :            : }
     204                 :            : 
     205                 :          0 : int cypress_detect(struct psmouse *psmouse, bool set_properties)
     206                 :            : {
     207                 :            :         unsigned char param[3];
     208                 :            : 
     209         [ #  # ]:          0 :         if (cypress_send_ext_cmd(psmouse, CYTP_CMD_READ_CYPRESS_ID, param))
     210                 :            :                 return -ENODEV;
     211                 :            : 
     212                 :            :         /* Check for Cypress Trackpad signature bytes: 0x33 0xCC */
     213 [ #  # ][ #  # ]:          0 :         if (param[0] != 0x33 || param[1] != 0xCC)
     214                 :            :                 return -ENODEV;
     215                 :            : 
     216         [ #  # ]:          0 :         if (set_properties) {
     217                 :          0 :                 psmouse->vendor = "Cypress";
     218                 :          0 :                 psmouse->name = "Trackpad";
     219                 :            :         }
     220                 :            : 
     221                 :            :         return 0;
     222                 :            : }
     223                 :            : 
     224                 :          0 : static int cypress_read_fw_version(struct psmouse *psmouse)
     225                 :            : {
     226                 :          0 :         struct cytp_data *cytp = psmouse->private;
     227                 :            :         unsigned char param[3];
     228                 :            : 
     229         [ #  # ]:          0 :         if (cypress_send_ext_cmd(psmouse, CYTP_CMD_READ_CYPRESS_ID, param))
     230                 :            :                 return -ENODEV;
     231                 :            : 
     232                 :            :         /* Check for Cypress Trackpad signature bytes: 0x33 0xCC */
     233 [ #  # ][ #  # ]:          0 :         if (param[0] != 0x33 || param[1] != 0xCC)
     234                 :            :                 return -ENODEV;
     235                 :            : 
     236                 :          0 :         cytp->fw_version = param[2] & FW_VERSION_MASX;
     237                 :          0 :         cytp->tp_metrics_supported = (param[2] & TP_METRICS_MASK) ? 1 : 0;
     238                 :            : 
     239                 :            :         /*
     240                 :            :          * Trackpad fw_version 11 (in Dell XPS12) yields a bogus response to
     241                 :            :          * CYTP_CMD_READ_TP_METRICS so do not try to use it. LP: #1103594.
     242                 :            :          */
     243         [ #  # ]:          0 :         if (cytp->fw_version >= 11)
     244                 :          0 :                 cytp->tp_metrics_supported = 0;
     245                 :            : 
     246                 :            :         psmouse_dbg(psmouse, "cytp->fw_version = %d\n", cytp->fw_version);
     247                 :            :         psmouse_dbg(psmouse, "cytp->tp_metrics_supported = %d\n",
     248                 :            :                  cytp->tp_metrics_supported);
     249                 :            : 
     250                 :            :         return 0;
     251                 :            : }
     252                 :            : 
     253                 :          0 : static int cypress_read_tp_metrics(struct psmouse *psmouse)
     254                 :            : {
     255                 :          0 :         struct cytp_data *cytp = psmouse->private;
     256                 :            :         unsigned char param[8];
     257                 :            : 
     258                 :            :         /* set default values for tp metrics. */
     259                 :          0 :         cytp->tp_width = CYTP_DEFAULT_WIDTH;
     260                 :          0 :         cytp->tp_high = CYTP_DEFAULT_HIGH;
     261                 :          0 :         cytp->tp_max_abs_x = CYTP_ABS_MAX_X;
     262                 :          0 :         cytp->tp_max_abs_y = CYTP_ABS_MAX_Y;
     263                 :          0 :         cytp->tp_min_pressure = CYTP_MIN_PRESSURE;
     264                 :          0 :         cytp->tp_max_pressure = CYTP_MAX_PRESSURE;
     265                 :          0 :         cytp->tp_res_x = cytp->tp_max_abs_x / cytp->tp_width;
     266                 :          0 :         cytp->tp_res_y = cytp->tp_max_abs_y / cytp->tp_high;
     267                 :            : 
     268         [ #  # ]:          0 :         if (!cytp->tp_metrics_supported)
     269                 :            :                 return 0;
     270                 :            : 
     271                 :          0 :         memset(param, 0, sizeof(param));
     272         [ #  # ]:          0 :         if (cypress_send_ext_cmd(psmouse, CYTP_CMD_READ_TP_METRICS, param) == 0) {
     273                 :            :                 /* Update trackpad parameters. */
     274                 :          0 :                 cytp->tp_max_abs_x = (param[1] << 8) | param[0];
     275                 :          0 :                 cytp->tp_max_abs_y = (param[3] << 8) | param[2];
     276                 :          0 :                 cytp->tp_min_pressure = param[4];
     277                 :          0 :                 cytp->tp_max_pressure = param[5];
     278                 :            :         }
     279                 :            : 
     280 [ #  # ][ #  # ]:          0 :         if (!cytp->tp_max_pressure ||
     281         [ #  # ]:          0 :             cytp->tp_max_pressure < cytp->tp_min_pressure ||
     282 [ #  # ][ #  # ]:          0 :             !cytp->tp_width || !cytp->tp_high ||
     283         [ #  # ]:          0 :             !cytp->tp_max_abs_x ||
     284         [ #  # ]:          0 :             cytp->tp_max_abs_x < cytp->tp_width ||
     285         [ #  # ]:          0 :             !cytp->tp_max_abs_y ||
     286                 :            :             cytp->tp_max_abs_y < cytp->tp_high)
     287                 :            :                 return -EINVAL;
     288                 :            : 
     289                 :          0 :         cytp->tp_res_x = cytp->tp_max_abs_x / cytp->tp_width;
     290                 :          0 :         cytp->tp_res_y = cytp->tp_max_abs_y / cytp->tp_high;
     291                 :            : 
     292                 :            : #ifdef CYTP_DEBUG_VERBOSE
     293                 :            :         psmouse_dbg(psmouse, "Dump trackpad hardware configuration as below:\n");
     294                 :            :         psmouse_dbg(psmouse, "cytp->tp_width = %d\n", cytp->tp_width);
     295                 :            :         psmouse_dbg(psmouse, "cytp->tp_high = %d\n", cytp->tp_high);
     296                 :            :         psmouse_dbg(psmouse, "cytp->tp_max_abs_x = %d\n", cytp->tp_max_abs_x);
     297                 :            :         psmouse_dbg(psmouse, "cytp->tp_max_abs_y = %d\n", cytp->tp_max_abs_y);
     298                 :            :         psmouse_dbg(psmouse, "cytp->tp_min_pressure = %d\n", cytp->tp_min_pressure);
     299                 :            :         psmouse_dbg(psmouse, "cytp->tp_max_pressure = %d\n", cytp->tp_max_pressure);
     300                 :            :         psmouse_dbg(psmouse, "cytp->tp_res_x = %d\n", cytp->tp_res_x);
     301                 :            :         psmouse_dbg(psmouse, "cytp->tp_res_y = %d\n", cytp->tp_res_y);
     302                 :            : 
     303                 :            :         psmouse_dbg(psmouse, "tp_type_APA = %d\n",
     304                 :            :                         (param[6] & TP_METRICS_BIT_APA) ? 1 : 0);
     305                 :            :         psmouse_dbg(psmouse, "tp_type_MTG = %d\n",
     306                 :            :                         (param[6] & TP_METRICS_BIT_MTG) ? 1 : 0);
     307                 :            :         psmouse_dbg(psmouse, "tp_palm = %d\n",
     308                 :            :                         (param[6] & TP_METRICS_BIT_PALM) ? 1 : 0);
     309                 :            :         psmouse_dbg(psmouse, "tp_stubborn = %d\n",
     310                 :            :                         (param[6] & TP_METRICS_BIT_STUBBORN) ? 1 : 0);
     311                 :            :         psmouse_dbg(psmouse, "tp_1f_jitter = %d\n",
     312                 :            :                         (param[6] & TP_METRICS_BIT_1F_JITTER) >> 2);
     313                 :            :         psmouse_dbg(psmouse, "tp_2f_jitter = %d\n",
     314                 :            :                         (param[6] & TP_METRICS_BIT_2F_JITTER) >> 4);
     315                 :            :         psmouse_dbg(psmouse, "tp_1f_spike = %d\n",
     316                 :            :                         param[7] & TP_METRICS_BIT_1F_SPIKE);
     317                 :            :         psmouse_dbg(psmouse, "tp_2f_spike = %d\n",
     318                 :            :                         (param[7] & TP_METRICS_BIT_2F_SPIKE) >> 2);
     319                 :            :         psmouse_dbg(psmouse, "tp_abs_packet_format_set = %d\n",
     320                 :            :                         (param[7] & TP_METRICS_BIT_ABS_PKT_FORMAT_SET) >> 4);
     321                 :            : #endif
     322                 :            : 
     323                 :          0 :         return 0;
     324                 :            : }
     325                 :            : 
     326                 :          0 : static int cypress_query_hardware(struct psmouse *psmouse)
     327                 :            : {
     328                 :            :         int ret;
     329                 :            : 
     330                 :          0 :         ret = cypress_read_fw_version(psmouse);
     331         [ #  # ]:          0 :         if (ret)
     332                 :            :                 return ret;
     333                 :            : 
     334                 :          0 :         ret = cypress_read_tp_metrics(psmouse);
     335         [ #  # ]:          0 :         if (ret)
     336                 :          0 :                 return ret;
     337                 :            : 
     338                 :            :         return 0;
     339                 :            : }
     340                 :            : 
     341                 :          0 : static int cypress_set_absolute_mode(struct psmouse *psmouse)
     342                 :            : {
     343                 :          0 :         struct cytp_data *cytp = psmouse->private;
     344                 :            :         unsigned char param[3];
     345                 :            : 
     346         [ #  # ]:          0 :         if (cypress_send_ext_cmd(psmouse, CYTP_CMD_ABS_WITH_PRESSURE_MODE, param) < 0)
     347                 :            :                 return -1;
     348                 :            : 
     349                 :          0 :         cytp->mode = (cytp->mode & ~CYTP_BIT_ABS_REL_MASK)
     350                 :          0 :                         | CYTP_BIT_ABS_PRESSURE;
     351                 :            :         cypress_set_packet_size(psmouse, 5);
     352                 :            : 
     353                 :          0 :         return 0;
     354                 :            : }
     355                 :            : 
     356                 :            : /*
     357                 :            :  * Reset trackpad device.
     358                 :            :  * This is also the default mode when trackpad powered on.
     359                 :            :  */
     360                 :          0 : static void cypress_reset(struct psmouse *psmouse)
     361                 :            : {
     362                 :          0 :         struct cytp_data *cytp = psmouse->private;
     363                 :            : 
     364                 :          0 :         cytp->mode = 0;
     365                 :            : 
     366                 :          0 :         psmouse_reset(psmouse);
     367                 :          0 : }
     368                 :            : 
     369                 :          0 : static int cypress_set_input_params(struct input_dev *input,
     370                 :            :                                     struct cytp_data *cytp)
     371                 :            : {
     372                 :            :         int ret;
     373                 :            : 
     374 [ #  # ][ #  # ]:          0 :         if (!cytp->tp_res_x || !cytp->tp_res_y)
     375                 :            :                 return -EINVAL;
     376                 :            : 
     377                 :            :         __set_bit(EV_ABS, input->evbit);
     378                 :          0 :         input_set_abs_params(input, ABS_X, 0, cytp->tp_max_abs_x, 0, 0);
     379                 :          0 :         input_set_abs_params(input, ABS_Y, 0, cytp->tp_max_abs_y, 0, 0);
     380                 :          0 :         input_set_abs_params(input, ABS_PRESSURE,
     381                 :            :                              cytp->tp_min_pressure, cytp->tp_max_pressure, 0, 0);
     382                 :          0 :         input_set_abs_params(input, ABS_TOOL_WIDTH, 0, 255, 0, 0);
     383                 :            : 
     384                 :            :         /* finger position */
     385                 :          0 :         input_set_abs_params(input, ABS_MT_POSITION_X, 0, cytp->tp_max_abs_x, 0, 0);
     386                 :          0 :         input_set_abs_params(input, ABS_MT_POSITION_Y, 0, cytp->tp_max_abs_y, 0, 0);
     387                 :          0 :         input_set_abs_params(input, ABS_MT_PRESSURE, 0, 255, 0, 0);
     388                 :            : 
     389                 :          0 :         ret = input_mt_init_slots(input, CYTP_MAX_MT_SLOTS,
     390                 :            :                         INPUT_MT_DROP_UNUSED|INPUT_MT_TRACK);
     391         [ #  # ]:          0 :         if (ret < 0)
     392                 :            :                 return ret;
     393                 :            : 
     394                 :            :         __set_bit(INPUT_PROP_SEMI_MT, input->propbit);
     395                 :            : 
     396                 :          0 :         input_abs_set_res(input, ABS_X, cytp->tp_res_x);
     397                 :          0 :         input_abs_set_res(input, ABS_Y, cytp->tp_res_y);
     398                 :            : 
     399                 :          0 :         input_abs_set_res(input, ABS_MT_POSITION_X, cytp->tp_res_x);
     400                 :          0 :         input_abs_set_res(input, ABS_MT_POSITION_Y, cytp->tp_res_y);
     401                 :            : 
     402                 :            :         __set_bit(BTN_TOUCH, input->keybit);
     403                 :            :         __set_bit(BTN_TOOL_FINGER, input->keybit);
     404                 :            :         __set_bit(BTN_TOOL_DOUBLETAP, input->keybit);
     405                 :            :         __set_bit(BTN_TOOL_TRIPLETAP, input->keybit);
     406                 :            :         __set_bit(BTN_TOOL_QUADTAP, input->keybit);
     407                 :            :         __set_bit(BTN_TOOL_QUINTTAP, input->keybit);
     408                 :            : 
     409                 :            :         __clear_bit(EV_REL, input->evbit);
     410                 :            :         __clear_bit(REL_X, input->relbit);
     411                 :            :         __clear_bit(REL_Y, input->relbit);
     412                 :            : 
     413                 :            :         __set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
     414                 :            :         __set_bit(EV_KEY, input->evbit);
     415                 :            :         __set_bit(BTN_LEFT, input->keybit);
     416                 :            :         __set_bit(BTN_RIGHT, input->keybit);
     417                 :            :         __set_bit(BTN_MIDDLE, input->keybit);
     418                 :            : 
     419                 :            :         input_set_drvdata(input, cytp);
     420                 :            : 
     421                 :          0 :         return 0;
     422                 :            : }
     423                 :            : 
     424                 :            : static int cypress_get_finger_count(unsigned char header_byte)
     425                 :            : {
     426                 :            :         unsigned char bits6_7;
     427                 :            :         int finger_count;
     428                 :            : 
     429                 :          0 :         bits6_7 = header_byte >> 6;
     430                 :          0 :         finger_count = bits6_7 & 0x03;
     431                 :            : 
     432 [ #  # ][ #  # ]:          0 :         if (finger_count == 1)
     433                 :            :                 return 1;
     434                 :            : 
     435 [ #  # ][ #  # ]:          0 :         if (header_byte & ABS_HSCROLL_BIT) {
     436                 :            :                 /* HSCROLL gets added on to 0 finger count. */
     437      [ #  #  # ]:          0 :                 switch (finger_count) {
              [ #  #  # ]
     438                 :            :                         case 0: return 4;
     439                 :            :                         case 2: return 5;
     440                 :            :                         default:
     441                 :            :                                 /* Invalid contact (e.g. palm). Ignore it. */
     442                 :            :                                 return 0;
     443                 :            :                 }
     444                 :            :         }
     445                 :            : 
     446                 :            :         return finger_count;
     447                 :            : }
     448                 :            : 
     449                 :            : 
     450                 :          0 : static int cypress_parse_packet(struct psmouse *psmouse,
     451                 :            :                                 struct cytp_data *cytp, struct cytp_report_data *report_data)
     452                 :            : {
     453                 :            :         unsigned char *packet = psmouse->packet;
     454                 :          0 :         unsigned char header_byte = packet[0];
     455                 :            : 
     456                 :          0 :         memset(report_data, 0, sizeof(struct cytp_report_data));
     457                 :            : 
     458                 :          0 :         report_data->contact_cnt = cypress_get_finger_count(header_byte);
     459                 :          0 :         report_data->tap = (header_byte & ABS_MULTIFINGER_TAP) ? 1 : 0;
     460                 :            : 
     461         [ #  # ]:          0 :         if (report_data->contact_cnt == 1) {
     462                 :          0 :                 report_data->contacts[0].x =
     463                 :          0 :                         ((packet[1] & 0x70) << 4) | packet[2];
     464                 :          0 :                 report_data->contacts[0].y =
     465                 :          0 :                         ((packet[1] & 0x07) << 8) | packet[3];
     466         [ #  # ]:          0 :                 if (cytp->mode & CYTP_BIT_ABS_PRESSURE)
     467                 :          0 :                         report_data->contacts[0].z = packet[4];
     468                 :            : 
     469         [ #  # ]:          0 :         } else if (report_data->contact_cnt >= 2) {
     470                 :          0 :                 report_data->contacts[0].x =
     471                 :          0 :                         ((packet[1] & 0x70) << 4) | packet[2];
     472                 :          0 :                 report_data->contacts[0].y =
     473                 :          0 :                         ((packet[1] & 0x07) << 8) | packet[3];
     474         [ #  # ]:          0 :                 if (cytp->mode & CYTP_BIT_ABS_PRESSURE)
     475                 :          0 :                         report_data->contacts[0].z = packet[4];
     476                 :            : 
     477                 :          0 :                 report_data->contacts[1].x =
     478                 :          0 :                         ((packet[5] & 0xf0) << 4) | packet[6];
     479                 :          0 :                 report_data->contacts[1].y =
     480                 :          0 :                         ((packet[5] & 0x0f) << 8) | packet[7];
     481         [ #  # ]:          0 :                 if (cytp->mode & CYTP_BIT_ABS_PRESSURE)
     482                 :          0 :                         report_data->contacts[1].z = report_data->contacts[0].z;
     483                 :            :         }
     484                 :            : 
     485                 :          0 :         report_data->left = (header_byte & BTN_LEFT_BIT) ? 1 : 0;
     486                 :          0 :         report_data->right = (header_byte & BTN_RIGHT_BIT) ? 1 : 0;
     487                 :            : 
     488                 :            :         /*
     489                 :            :          * This is only true if one of the mouse buttons were tapped.  Make
     490                 :            :          * sure it doesn't turn into a click. The regular tap-to-click
     491                 :            :          * functionality will handle that on its own. If we don't do this,
     492                 :            :          * disabling tap-to-click won't affect the mouse button zones.
     493                 :            :          */
     494         [ #  # ]:          0 :         if (report_data->tap)
     495                 :          0 :                 report_data->left = 0;
     496                 :            : 
     497                 :            : #ifdef CYTP_DEBUG_VERBOSE
     498                 :            :         {
     499                 :            :                 int i;
     500                 :            :                 int n = report_data->contact_cnt;
     501                 :            :                 psmouse_dbg(psmouse, "Dump parsed report data as below:\n");
     502                 :            :                 psmouse_dbg(psmouse, "contact_cnt = %d\n",
     503                 :            :                         report_data->contact_cnt);
     504                 :            :                 if (n > CYTP_MAX_MT_SLOTS)
     505                 :            :                     n = CYTP_MAX_MT_SLOTS;
     506                 :            :                 for (i = 0; i < n; i++)
     507                 :            :                         psmouse_dbg(psmouse, "contacts[%d] = {%d, %d, %d}\n", i,
     508                 :            :                                         report_data->contacts[i].x,
     509                 :            :                                         report_data->contacts[i].y,
     510                 :            :                                         report_data->contacts[i].z);
     511                 :            :                 psmouse_dbg(psmouse, "left = %d\n", report_data->left);
     512                 :            :                 psmouse_dbg(psmouse, "right = %d\n", report_data->right);
     513                 :            :                 psmouse_dbg(psmouse, "middle = %d\n", report_data->middle);
     514                 :            :         }
     515                 :            : #endif
     516                 :            : 
     517                 :          0 :         return 0;
     518                 :            : }
     519                 :            : 
     520                 :          0 : static void cypress_process_packet(struct psmouse *psmouse, bool zero_pkt)
     521                 :            : {
     522                 :            :         int i;
     523                 :          0 :         struct input_dev *input = psmouse->dev;
     524                 :          0 :         struct cytp_data *cytp = psmouse->private;
     525                 :            :         struct cytp_report_data report_data;
     526                 :            :         struct cytp_contact *contact;
     527                 :            :         struct input_mt_pos pos[CYTP_MAX_MT_SLOTS];
     528                 :            :         int slots[CYTP_MAX_MT_SLOTS];
     529                 :            :         int n;
     530                 :            : 
     531                 :          0 :         cypress_parse_packet(psmouse, cytp, &report_data);
     532                 :            : 
     533                 :          0 :         n = report_data.contact_cnt;
     534         [ #  # ]:          0 :         if (n > CYTP_MAX_MT_SLOTS)
     535                 :            :                 n = CYTP_MAX_MT_SLOTS;
     536                 :            : 
     537         [ #  # ]:          0 :         for (i = 0; i < n; i++) {
     538                 :          0 :                 contact = &report_data.contacts[i];
     539                 :          0 :                 pos[i].x = contact->x;
     540                 :          0 :                 pos[i].y = contact->y;
     541                 :            :         }
     542                 :            : 
     543                 :          0 :         input_mt_assign_slots(input, slots, pos, n);
     544                 :            : 
     545         [ #  # ]:          0 :         for (i = 0; i < n; i++) {
     546                 :          0 :                 contact = &report_data.contacts[i];
     547                 :          0 :                 input_mt_slot(input, slots[i]);
     548                 :          0 :                 input_mt_report_slot_state(input, MT_TOOL_FINGER, true);
     549                 :          0 :                 input_report_abs(input, ABS_MT_POSITION_X, contact->x);
     550                 :          0 :                 input_report_abs(input, ABS_MT_POSITION_Y, contact->y);
     551                 :          0 :                 input_report_abs(input, ABS_MT_PRESSURE, contact->z);
     552                 :            :         }
     553                 :            : 
     554                 :          0 :         input_mt_sync_frame(input);
     555                 :            : 
     556                 :          0 :         input_mt_report_finger_count(input, report_data.contact_cnt);
     557                 :            : 
     558                 :          0 :         input_report_key(input, BTN_LEFT, report_data.left);
     559                 :          0 :         input_report_key(input, BTN_RIGHT, report_data.right);
     560                 :          0 :         input_report_key(input, BTN_MIDDLE, report_data.middle);
     561                 :            : 
     562                 :            :         input_sync(input);
     563                 :          0 : }
     564                 :            : 
     565                 :          0 : static psmouse_ret_t cypress_validate_byte(struct psmouse *psmouse)
     566                 :            : {
     567                 :            :         int contact_cnt;
     568                 :          0 :         int index = psmouse->pktcnt - 1;
     569                 :            :         unsigned char *packet = psmouse->packet;
     570                 :          0 :         struct cytp_data *cytp = psmouse->private;
     571                 :            : 
     572 [ #  # ][ #  # ]:          0 :         if (index < 0 || index > cytp->pkt_size)
     573                 :            :                 return PSMOUSE_BAD_DATA;
     574                 :            : 
     575 [ #  # ][ #  # ]:          0 :         if (index == 0 && (packet[0] & 0xfc) == 0) {
     576                 :            :                 /* call packet process for reporting finger leave. */
     577                 :          0 :                 cypress_process_packet(psmouse, 1);
     578                 :          0 :                 return PSMOUSE_FULL_PACKET;
     579                 :            :         }
     580                 :            : 
     581                 :            :         /*
     582                 :            :          * Perform validation (and adjust packet size) based only on the
     583                 :            :          * first byte; allow all further bytes through.
     584                 :            :          */
     585         [ #  # ]:          0 :         if (index != 0)
     586                 :            :                 return PSMOUSE_GOOD_DATA;
     587                 :            : 
     588                 :            :         /*
     589                 :            :          * If absolute/relative mode bit has not been set yet, just pass
     590                 :            :          * the byte through.
     591                 :            :          */
     592         [ #  # ]:          0 :         if ((cytp->mode & CYTP_BIT_ABS_REL_MASK) == 0)
     593                 :            :                 return PSMOUSE_GOOD_DATA;
     594                 :            : 
     595         [ #  # ]:          0 :         if ((packet[0] & 0x08) == 0x08)
     596                 :            :                 return PSMOUSE_BAD_DATA;
     597                 :            : 
     598                 :            :         contact_cnt = cypress_get_finger_count(packet[0]);
     599         [ #  # ]:          0 :         if (cytp->mode & CYTP_BIT_ABS_NO_PRESSURE)
     600         [ #  # ]:          0 :                 cypress_set_packet_size(psmouse, contact_cnt == 2 ? 7 : 4);
     601                 :            :         else
     602         [ #  # ]:          0 :                 cypress_set_packet_size(psmouse, contact_cnt == 2 ? 8 : 5);
     603                 :            : 
     604                 :            :         return PSMOUSE_GOOD_DATA;
     605                 :            : }
     606                 :            : 
     607                 :          0 : static psmouse_ret_t cypress_protocol_handler(struct psmouse *psmouse)
     608                 :            : {
     609                 :          0 :         struct cytp_data *cytp = psmouse->private;
     610                 :            : 
     611         [ #  # ]:          0 :         if (psmouse->pktcnt >= cytp->pkt_size) {
     612                 :          0 :                 cypress_process_packet(psmouse, 0);
     613                 :          0 :                 return PSMOUSE_FULL_PACKET;
     614                 :            :         }
     615                 :            : 
     616                 :          0 :         return cypress_validate_byte(psmouse);
     617                 :            : }
     618                 :            : 
     619                 :          0 : static void cypress_set_rate(struct psmouse *psmouse, unsigned int rate)
     620                 :            : {
     621                 :          0 :         struct cytp_data *cytp = psmouse->private;
     622                 :            : 
     623         [ #  # ]:          0 :         if (rate >= 80) {
     624                 :          0 :                 psmouse->rate = 80;
     625                 :          0 :                 cytp->mode |= CYTP_BIT_HIGH_RATE;
     626                 :            :         } else {
     627                 :          0 :                 psmouse->rate = 40;
     628                 :          0 :                 cytp->mode &= ~CYTP_BIT_HIGH_RATE;
     629                 :            :         }
     630                 :            : 
     631                 :          0 :         ps2_command(&psmouse->ps2dev, (unsigned char *)&psmouse->rate,
     632                 :            :                     PSMOUSE_CMD_SETRATE);
     633                 :          0 : }
     634                 :            : 
     635                 :          0 : static void cypress_disconnect(struct psmouse *psmouse)
     636                 :            : {
     637                 :            :         cypress_reset(psmouse);
     638                 :          0 :         kfree(psmouse->private);
     639                 :          0 :         psmouse->private = NULL;
     640                 :          0 : }
     641                 :            : 
     642                 :          0 : static int cypress_reconnect(struct psmouse *psmouse)
     643                 :            : {
     644                 :            :         int tries = CYTP_PS2_CMD_TRIES;
     645                 :            :         int rc;
     646                 :            : 
     647                 :            :         do {
     648                 :            :                 cypress_reset(psmouse);
     649                 :          0 :                 rc = cypress_detect(psmouse, false);
     650 [ #  # ][ #  # ]:          0 :         } while (rc && (--tries > 0));
     651                 :            : 
     652         [ #  # ]:          0 :         if (rc) {
     653                 :          0 :                 psmouse_err(psmouse, "Reconnect: unable to detect trackpad.\n");
     654                 :          0 :                 return -1;
     655                 :            :         }
     656                 :            : 
     657         [ #  # ]:          0 :         if (cypress_set_absolute_mode(psmouse)) {
     658                 :          0 :                 psmouse_err(psmouse, "Reconnect: Unable to initialize Cypress absolute mode.\n");
     659                 :          0 :                 return -1;
     660                 :            :         }
     661                 :            : 
     662                 :            :         return 0;
     663                 :            : }
     664                 :            : 
     665                 :          0 : int cypress_init(struct psmouse *psmouse)
     666                 :            : {
     667                 :            :         struct cytp_data *cytp;
     668                 :            : 
     669                 :            :         cytp = kzalloc(sizeof(struct cytp_data), GFP_KERNEL);
     670         [ #  # ]:          0 :         if (!cytp)
     671                 :            :                 return -ENOMEM;
     672                 :            : 
     673                 :          0 :         psmouse->private = cytp;
     674                 :          0 :         psmouse->pktsize = 8;
     675                 :            : 
     676                 :            :         cypress_reset(psmouse);
     677                 :            : 
     678         [ #  # ]:          0 :         if (cypress_query_hardware(psmouse)) {
     679                 :          0 :                 psmouse_err(psmouse, "Unable to query Trackpad hardware.\n");
     680                 :          0 :                 goto err_exit;
     681                 :            :         }
     682                 :            : 
     683         [ #  # ]:          0 :         if (cypress_set_absolute_mode(psmouse)) {
     684                 :          0 :                 psmouse_err(psmouse, "init: Unable to initialize Cypress absolute mode.\n");
     685                 :          0 :                 goto err_exit;
     686                 :            :         }
     687                 :            : 
     688         [ #  # ]:          0 :         if (cypress_set_input_params(psmouse->dev, cytp) < 0) {
     689                 :          0 :                 psmouse_err(psmouse, "init: Unable to set input params.\n");
     690                 :          0 :                 goto err_exit;
     691                 :            :         }
     692                 :            : 
     693                 :          0 :         psmouse->model = 1;
     694                 :          0 :         psmouse->protocol_handler = cypress_protocol_handler;
     695                 :          0 :         psmouse->set_rate = cypress_set_rate;
     696                 :          0 :         psmouse->disconnect = cypress_disconnect;
     697                 :          0 :         psmouse->reconnect = cypress_reconnect;
     698                 :          0 :         psmouse->cleanup = cypress_reset;
     699                 :          0 :         psmouse->resync_time = 0;
     700                 :            : 
     701                 :          0 :         return 0;
     702                 :            : 
     703                 :            : err_exit:
     704                 :            :         /*
     705                 :            :          * Reset Cypress Trackpad as a standard mouse. Then
     706                 :            :          * let psmouse driver commmunicating with it as default PS2 mouse.
     707                 :            :          */
     708                 :            :         cypress_reset(psmouse);
     709                 :            : 
     710                 :          0 :         psmouse->private = NULL;
     711                 :          0 :         kfree(cytp);
     712                 :            : 
     713                 :          0 :         return -1;
     714                 :            : }
     715                 :            : 
     716                 :          0 : bool cypress_supported(void)
     717                 :            : {
     718                 :          0 :         return true;
     719                 :            : }

Generated by: LCOV version 1.9