linux/drivers/input/mouse/synaptics.c
<<
>>
Prefs
   1/*
   2 * Synaptics TouchPad PS/2 mouse driver
   3 *
   4 *   2003 Dmitry Torokhov <dtor@mail.ru>
   5 *     Added support for pass-through port. Special thanks to Peter Berg Larsen
   6 *     for explaining various Synaptics quirks.
   7 *
   8 *   2003 Peter Osterlund <petero2@telia.com>
   9 *     Ported to 2.5 input device infrastructure.
  10 *
  11 *   Copyright (C) 2001 Stefan Gmeiner <riddlebox@freesurf.ch>
  12 *     start merging tpconfig and gpm code to a xfree-input module
  13 *     adding some changes and extensions (ex. 3rd and 4th button)
  14 *
  15 *   Copyright (c) 1997 C. Scott Ananian <cananian@alumni.priceton.edu>
  16 *   Copyright (c) 1998-2000 Bruce Kalk <kall@compass.com>
  17 *     code for the special synaptics commands (from the tpconfig-source)
  18 *
  19 * This program is free software; you can redistribute it and/or modify it
  20 * under the terms of the GNU General Public License version 2 as published by
  21 * the Free Software Foundation.
  22 *
  23 * Trademarks are the property of their respective owners.
  24 */
  25
  26#include <linux/module.h>
  27#include <linux/delay.h>
  28#include <linux/dmi.h>
  29#include <linux/input/mt.h>
  30#include <linux/serio.h>
  31#include <linux/libps2.h>
  32#include <linux/slab.h>
  33#include "psmouse.h"
  34#include "synaptics.h"
  35
  36/*
  37 * The x/y limits are taken from the Synaptics TouchPad interfacing Guide,
  38 * section 2.3.2, which says that they should be valid regardless of the
  39 * actual size of the sensor.
  40 * Note that newer firmware allows querying device for maximum useable
  41 * coordinates.
  42 */
  43#define XMIN 0
  44#define XMAX 6143
  45#define YMIN 0
  46#define YMAX 6143
  47#define XMIN_NOMINAL 1472
  48#define XMAX_NOMINAL 5472
  49#define YMIN_NOMINAL 1408
  50#define YMAX_NOMINAL 4448
  51
  52/* Size in bits of absolute position values reported by the hardware */
  53#define ABS_POS_BITS 13
  54
  55/*
  56 * These values should represent the absolute maximum value that will
  57 * be reported for a positive position value. Some Synaptics firmware
  58 * uses this value to indicate a finger near the edge of the touchpad
  59 * whose precise position cannot be determined.
  60 *
  61 * At least one touchpad is known to report positions in excess of this
  62 * value which are actually negative values truncated to the 13-bit
  63 * reporting range. These values have never been observed to be lower
  64 * than 8184 (i.e. -8), so we treat all values greater than 8176 as
  65 * negative and any other value as positive.
  66 */
  67#define X_MAX_POSITIVE 8176
  68#define Y_MAX_POSITIVE 8176
  69
  70/*****************************************************************************
  71 *      Stuff we need even when we do not want native Synaptics support
  72 ****************************************************************************/
  73
  74/*
  75 * Set the synaptics touchpad mode byte by special commands
  76 */
  77static int synaptics_mode_cmd(struct psmouse *psmouse, unsigned char mode)
  78{
  79        unsigned char param[1];
  80
  81        if (psmouse_sliced_command(psmouse, mode))
  82                return -1;
  83        param[0] = SYN_PS_SET_MODE2;
  84        if (ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_SETRATE))
  85                return -1;
  86        return 0;
  87}
  88
  89int synaptics_detect(struct psmouse *psmouse, bool set_properties)
  90{
  91        struct ps2dev *ps2dev = &psmouse->ps2dev;
  92        unsigned char param[4];
  93
  94        param[0] = 0;
  95
  96        ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
  97        ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
  98        ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
  99        ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
 100        ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO);
 101
 102        if (param[1] != 0x47)
 103                return -ENODEV;
 104
 105        if (set_properties) {
 106                psmouse->vendor = "Synaptics";
 107                psmouse->name = "TouchPad";
 108        }
 109
 110        return 0;
 111}
 112
 113void synaptics_reset(struct psmouse *psmouse)
 114{
 115        /* reset touchpad back to relative mode, gestures enabled */
 116        synaptics_mode_cmd(psmouse, 0);
 117}
 118
 119#ifdef CONFIG_MOUSE_PS2_SYNAPTICS
 120
 121#define ANY_BOARD_ID 0
 122struct min_max_quirk {
 123        const char * const *pnp_ids;
 124        struct {
 125                unsigned long int min, max;
 126        } board_id;
 127        int x_min, x_max, y_min, y_max;
 128};
 129
 130static const struct min_max_quirk min_max_pnpid_table[] = {
 131        {
 132                (const char * const []){"LEN0033", NULL},
 133                {ANY_BOARD_ID, ANY_BOARD_ID},
 134                1024, 5052, 2258, 4832
 135        },
 136        {
 137                (const char * const []){"LEN0042", NULL},
 138                {ANY_BOARD_ID, ANY_BOARD_ID},
 139                1232, 5710, 1156, 4696
 140        },
 141        {
 142                (const char * const []){"LEN0034", "LEN0036", "LEN0037",
 143                                        "LEN0039", "LEN2002", "LEN2004",
 144                                        NULL},
 145                {ANY_BOARD_ID, 2961},
 146                1024, 5112, 2024, 4832
 147        },
 148        {
 149                (const char * const []){"LEN2001", NULL},
 150                {ANY_BOARD_ID, ANY_BOARD_ID},
 151                1024, 5022, 2508, 4832
 152        },
 153        {
 154                (const char * const []){"LEN2006", NULL},
 155                {ANY_BOARD_ID, ANY_BOARD_ID},
 156                1264, 5675, 1171, 4688
 157        },
 158        { }
 159};
 160
 161/* This list has been kindly provided by Synaptics. */
 162static const char * const topbuttonpad_pnp_ids[] = {
 163        "LEN0017",
 164        "LEN0018",
 165        "LEN0019",
 166        "LEN0023",
 167        "LEN002A",
 168        "LEN002B",
 169        "LEN002C",
 170        "LEN002D",
 171        "LEN002E",
 172        "LEN0033", /* Helix */
 173        "LEN0034", /* T431s, L440, L540, T540, W540, X1 Carbon 2nd */
 174        "LEN0035", /* X240 */
 175        "LEN0036", /* T440 */
 176        "LEN0037", /* X1 Carbon 2nd */
 177        "LEN0038",
 178        "LEN0039", /* T440s */
 179        "LEN0041",
 180        "LEN0042", /* Yoga */
 181        "LEN0045",
 182        "LEN0047",
 183        "LEN0049",
 184        "LEN2000",
 185        "LEN2001", /* Edge E431 */
 186        "LEN2002", /* Edge E531 */
 187        "LEN2003",
 188        "LEN2004", /* L440 */
 189        "LEN2005",
 190        "LEN2006",
 191        "LEN2007",
 192        "LEN2008",
 193        "LEN2009",
 194        "LEN200A",
 195        "LEN200B",
 196        NULL
 197};
 198
 199/*****************************************************************************
 200 *      Synaptics communications functions
 201 ****************************************************************************/
 202
 203/*
 204 * Synaptics touchpads report the y coordinate from bottom to top, which is
 205 * opposite from what userspace expects.
 206 * This function is used to invert y before reporting.
 207 */
 208static int synaptics_invert_y(int y)
 209{
 210        return YMAX_NOMINAL + YMIN_NOMINAL - y;
 211}
 212
 213/*
 214 * Send a command to the synpatics touchpad by special commands
 215 */
 216static int synaptics_send_cmd(struct psmouse *psmouse, unsigned char c, unsigned char *param)
 217{
 218        if (psmouse_sliced_command(psmouse, c))
 219                return -1;
 220        if (ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_GETINFO))
 221                return -1;
 222        return 0;
 223}
 224
 225/*
 226 * Read the model-id bytes from the touchpad
 227 * see also SYN_MODEL_* macros
 228 */
 229static int synaptics_model_id(struct psmouse *psmouse)
 230{
 231        struct synaptics_data *priv = psmouse->private;
 232        unsigned char mi[3];
 233
 234        if (synaptics_send_cmd(psmouse, SYN_QUE_MODEL, mi))
 235                return -1;
 236        priv->model_id = (mi[0]<<16) | (mi[1]<<8) | mi[2];
 237        return 0;
 238}
 239
 240static int synaptics_more_extended_queries(struct psmouse *psmouse)
 241{
 242        struct synaptics_data *priv = psmouse->private;
 243        unsigned char buf[3];
 244
 245        if (synaptics_send_cmd(psmouse, SYN_QUE_MEXT_CAPAB_10, buf))
 246                return -1;
 247
 248        priv->ext_cap_10 = (buf[0]<<16) | (buf[1]<<8) | buf[2];
 249
 250        return 0;
 251}
 252
 253/*
 254 * Read the board id and the "More Extended Queries" from the touchpad
 255 * The board id is encoded in the "QUERY MODES" response
 256 */
 257static int synaptics_query_modes(struct psmouse *psmouse)
 258{
 259        struct synaptics_data *priv = psmouse->private;
 260        unsigned char bid[3];
 261
 262        /* firmwares prior 7.5 have no board_id encoded */
 263        if (SYN_ID_FULL(priv->identity) < 0x705)
 264                return 0;
 265
 266        if (synaptics_send_cmd(psmouse, SYN_QUE_MODES, bid))
 267                return -1;
 268        priv->board_id = ((bid[0] & 0xfc) << 6) | bid[1];
 269
 270        if (SYN_MEXT_CAP_BIT(bid[0]))
 271                return synaptics_more_extended_queries(psmouse);
 272
 273        return 0;
 274}
 275
 276/*
 277 * Read the firmware id from the touchpad
 278 */
 279static int synaptics_firmware_id(struct psmouse *psmouse)
 280{
 281        struct synaptics_data *priv = psmouse->private;
 282        unsigned char fwid[3];
 283
 284        if (synaptics_send_cmd(psmouse, SYN_QUE_FIRMWARE_ID, fwid))
 285                return -1;
 286        priv->firmware_id = (fwid[0] << 16) | (fwid[1] << 8) | fwid[2];
 287        return 0;
 288}
 289
 290/*
 291 * Read the capability-bits from the touchpad
 292 * see also the SYN_CAP_* macros
 293 */
 294static int synaptics_capability(struct psmouse *psmouse)
 295{
 296        struct synaptics_data *priv = psmouse->private;
 297        unsigned char cap[3];
 298
 299        if (synaptics_send_cmd(psmouse, SYN_QUE_CAPABILITIES, cap))
 300                return -1;
 301        priv->capabilities = (cap[0] << 16) | (cap[1] << 8) | cap[2];
 302        priv->ext_cap = priv->ext_cap_0c = 0;
 303
 304        /*
 305         * Older firmwares had submodel ID fixed to 0x47
 306         */
 307        if (SYN_ID_FULL(priv->identity) < 0x705 &&
 308            SYN_CAP_SUBMODEL_ID(priv->capabilities) != 0x47) {
 309                return -1;
 310        }
 311
 312        /*
 313         * Unless capExtended is set the rest of the flags should be ignored
 314         */
 315        if (!SYN_CAP_EXTENDED(priv->capabilities))
 316                priv->capabilities = 0;
 317
 318        if (SYN_EXT_CAP_REQUESTS(priv->capabilities) >= 1) {
 319                if (synaptics_send_cmd(psmouse, SYN_QUE_EXT_CAPAB, cap)) {
 320                        psmouse_warn(psmouse,
 321                                     "device claims to have extended capabilities, but I'm not able to read them.\n");
 322                } else {
 323                        priv->ext_cap = (cap[0] << 16) | (cap[1] << 8) | cap[2];
 324
 325                        /*
 326                         * if nExtBtn is greater than 8 it should be considered
 327                         * invalid and treated as 0
 328                         */
 329                        if (SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap) > 8)
 330                                priv->ext_cap &= 0xff0fff;
 331                }
 332        }
 333
 334        if (SYN_EXT_CAP_REQUESTS(priv->capabilities) >= 4) {
 335                if (synaptics_send_cmd(psmouse, SYN_QUE_EXT_CAPAB_0C, cap)) {
 336                        psmouse_warn(psmouse,
 337                                     "device claims to have extended capability 0x0c, but I'm not able to read it.\n");
 338                } else {
 339                        priv->ext_cap_0c = (cap[0] << 16) | (cap[1] << 8) | cap[2];
 340                }
 341        }
 342
 343        return 0;
 344}
 345
 346/*
 347 * Identify Touchpad
 348 * See also the SYN_ID_* macros
 349 */
 350static int synaptics_identify(struct psmouse *psmouse)
 351{
 352        struct synaptics_data *priv = psmouse->private;
 353        unsigned char id[3];
 354
 355        if (synaptics_send_cmd(psmouse, SYN_QUE_IDENTIFY, id))
 356                return -1;
 357        priv->identity = (id[0]<<16) | (id[1]<<8) | id[2];
 358        if (SYN_ID_IS_SYNAPTICS(priv->identity))
 359                return 0;
 360        return -1;
 361}
 362
 363/*
 364 * Read touchpad resolution and maximum reported coordinates
 365 * Resolution is left zero if touchpad does not support the query
 366 */
 367
 368static int synaptics_resolution(struct psmouse *psmouse)
 369{
 370        struct synaptics_data *priv = psmouse->private;
 371        unsigned char resp[3];
 372
 373        if (SYN_ID_MAJOR(priv->identity) < 4)
 374                return 0;
 375
 376        if (synaptics_send_cmd(psmouse, SYN_QUE_RESOLUTION, resp) == 0) {
 377                if (resp[0] != 0 && (resp[1] & 0x80) && resp[2] != 0) {
 378                        priv->x_res = resp[0]; /* x resolution in units/mm */
 379                        priv->y_res = resp[2]; /* y resolution in units/mm */
 380                }
 381        }
 382
 383        if (SYN_EXT_CAP_REQUESTS(priv->capabilities) >= 5 &&
 384            SYN_CAP_MAX_DIMENSIONS(priv->ext_cap_0c)) {
 385                if (synaptics_send_cmd(psmouse, SYN_QUE_EXT_MAX_COORDS, resp)) {
 386                        psmouse_warn(psmouse,
 387                                     "device claims to have max coordinates query, but I'm not able to read it.\n");
 388                } else {
 389                        priv->x_max = (resp[0] << 5) | ((resp[1] & 0x0f) << 1);
 390                        priv->y_max = (resp[2] << 5) | ((resp[1] & 0xf0) >> 3);
 391                        psmouse_info(psmouse,
 392                                     "queried max coordinates: x [..%d], y [..%d]\n",
 393                                     priv->x_max, priv->y_max);
 394                }
 395        }
 396
 397        if (SYN_CAP_MIN_DIMENSIONS(priv->ext_cap_0c) &&
 398            (SYN_EXT_CAP_REQUESTS(priv->capabilities) >= 7 ||
 399             /*
 400              * Firmware v8.1 does not report proper number of extended
 401              * capabilities, but has been proven to report correct min
 402              * coordinates.
 403              */
 404             SYN_ID_FULL(priv->identity) == 0x801)) {
 405                if (synaptics_send_cmd(psmouse, SYN_QUE_EXT_MIN_COORDS, resp)) {
 406                        psmouse_warn(psmouse,
 407                                     "device claims to have min coordinates query, but I'm not able to read it.\n");
 408                } else {
 409                        priv->x_min = (resp[0] << 5) | ((resp[1] & 0x0f) << 1);
 410                        priv->y_min = (resp[2] << 5) | ((resp[1] & 0xf0) >> 3);
 411                        psmouse_info(psmouse,
 412                                     "queried min coordinates: x [%d..], y [%d..]\n",
 413                                     priv->x_min, priv->y_min);
 414                }
 415        }
 416
 417        return 0;
 418}
 419
 420/*
 421 * Apply quirk(s) if the hardware matches
 422 */
 423
 424static void synaptics_apply_quirks(struct psmouse *psmouse)
 425{
 426        struct synaptics_data *priv = psmouse->private;
 427        int i;
 428
 429        for (i = 0; min_max_pnpid_table[i].pnp_ids; i++) {
 430                if (!psmouse_matches_pnp_id(psmouse,
 431                                            min_max_pnpid_table[i].pnp_ids))
 432                        continue;
 433
 434                if (min_max_pnpid_table[i].board_id.min != ANY_BOARD_ID &&
 435                    priv->board_id < min_max_pnpid_table[i].board_id.min)
 436                        continue;
 437
 438                if (min_max_pnpid_table[i].board_id.max != ANY_BOARD_ID &&
 439                    priv->board_id > min_max_pnpid_table[i].board_id.max)
 440                        continue;
 441
 442                priv->x_min = min_max_pnpid_table[i].x_min;
 443                priv->x_max = min_max_pnpid_table[i].x_max;
 444                priv->y_min = min_max_pnpid_table[i].y_min;
 445                priv->y_max = min_max_pnpid_table[i].y_max;
 446                psmouse_info(psmouse,
 447                             "quirked min/max coordinates: x [%d..%d], y [%d..%d]\n",
 448                             priv->x_min, priv->x_max,
 449                             priv->y_min, priv->y_max);
 450                break;
 451        }
 452}
 453
 454static int synaptics_query_hardware(struct psmouse *psmouse)
 455{
 456        if (synaptics_identify(psmouse))
 457                return -1;
 458        if (synaptics_model_id(psmouse))
 459                return -1;
 460        if (synaptics_firmware_id(psmouse))
 461                return -1;
 462        if (synaptics_query_modes(psmouse))
 463                return -1;
 464        if (synaptics_capability(psmouse))
 465                return -1;
 466        if (synaptics_resolution(psmouse))
 467                return -1;
 468
 469        synaptics_apply_quirks(psmouse);
 470
 471        return 0;
 472}
 473
 474static int synaptics_set_advanced_gesture_mode(struct psmouse *psmouse)
 475{
 476        static unsigned char param = 0xc8;
 477        struct synaptics_data *priv = psmouse->private;
 478
 479        if (!(SYN_CAP_ADV_GESTURE(priv->ext_cap_0c) ||
 480              SYN_CAP_IMAGE_SENSOR(priv->ext_cap_0c)))
 481                return 0;
 482
 483        if (psmouse_sliced_command(psmouse, SYN_QUE_MODEL))
 484                return -1;
 485
 486        if (ps2_command(&psmouse->ps2dev, &param, PSMOUSE_CMD_SETRATE))
 487                return -1;
 488
 489        /* Advanced gesture mode also sends multi finger data */
 490        priv->capabilities |= BIT(1);
 491
 492        return 0;
 493}
 494
 495static int synaptics_set_mode(struct psmouse *psmouse)
 496{
 497        struct synaptics_data *priv = psmouse->private;
 498
 499        priv->mode = 0;
 500        if (priv->absolute_mode)
 501                priv->mode |= SYN_BIT_ABSOLUTE_MODE;
 502        if (priv->disable_gesture)
 503                priv->mode |= SYN_BIT_DISABLE_GESTURE;
 504        if (psmouse->rate >= 80)
 505                priv->mode |= SYN_BIT_HIGH_RATE;
 506        if (SYN_CAP_EXTENDED(priv->capabilities))
 507                priv->mode |= SYN_BIT_W_MODE;
 508
 509        if (synaptics_mode_cmd(psmouse, priv->mode))
 510                return -1;
 511
 512        if (priv->absolute_mode &&
 513            synaptics_set_advanced_gesture_mode(psmouse)) {
 514                psmouse_err(psmouse, "Advanced gesture mode init failed.\n");
 515                return -1;
 516        }
 517
 518        return 0;
 519}
 520
 521static void synaptics_set_rate(struct psmouse *psmouse, unsigned int rate)
 522{
 523        struct synaptics_data *priv = psmouse->private;
 524
 525        if (rate >= 80) {
 526                priv->mode |= SYN_BIT_HIGH_RATE;
 527                psmouse->rate = 80;
 528        } else {
 529                priv->mode &= ~SYN_BIT_HIGH_RATE;
 530                psmouse->rate = 40;
 531        }
 532
 533        synaptics_mode_cmd(psmouse, priv->mode);
 534}
 535
 536/*****************************************************************************
 537 *      Synaptics pass-through PS/2 port support
 538 ****************************************************************************/
 539static int synaptics_pt_write(struct serio *serio, unsigned char c)
 540{
 541        struct psmouse *parent = serio_get_drvdata(serio->parent);
 542        char rate_param = SYN_PS_CLIENT_CMD; /* indicates that we want pass-through port */
 543
 544        if (psmouse_sliced_command(parent, c))
 545                return -1;
 546        if (ps2_command(&parent->ps2dev, &rate_param, PSMOUSE_CMD_SETRATE))
 547                return -1;
 548        return 0;
 549}
 550
 551static int synaptics_pt_start(struct serio *serio)
 552{
 553        struct psmouse *parent = serio_get_drvdata(serio->parent);
 554        struct synaptics_data *priv = parent->private;
 555
 556        serio_pause_rx(parent->ps2dev.serio);
 557        priv->pt_port = serio;
 558        serio_continue_rx(parent->ps2dev.serio);
 559
 560        return 0;
 561}
 562
 563static void synaptics_pt_stop(struct serio *serio)
 564{
 565        struct psmouse *parent = serio_get_drvdata(serio->parent);
 566        struct synaptics_data *priv = parent->private;
 567
 568        serio_pause_rx(parent->ps2dev.serio);
 569        priv->pt_port = NULL;
 570        serio_continue_rx(parent->ps2dev.serio);
 571}
 572
 573static int synaptics_is_pt_packet(unsigned char *buf)
 574{
 575        return (buf[0] & 0xFC) == 0x84 && (buf[3] & 0xCC) == 0xC4;
 576}
 577
 578static void synaptics_pass_pt_packet(struct psmouse *psmouse,
 579                                     struct serio *ptport,
 580                                     unsigned char *packet)
 581{
 582        struct synaptics_data *priv = psmouse->private;
 583        struct psmouse *child = serio_get_drvdata(ptport);
 584
 585        if (child && child->state == PSMOUSE_ACTIVATED) {
 586                serio_interrupt(ptport, packet[1] | priv->pt_buttons, 0);
 587                serio_interrupt(ptport, packet[4], 0);
 588                serio_interrupt(ptport, packet[5], 0);
 589                if (child->pktsize == 4)
 590                        serio_interrupt(ptport, packet[2], 0);
 591        } else {
 592                serio_interrupt(ptport, packet[1], 0);
 593        }
 594}
 595
 596static void synaptics_pt_activate(struct psmouse *psmouse)
 597{
 598        struct synaptics_data *priv = psmouse->private;
 599        struct psmouse *child = serio_get_drvdata(priv->pt_port);
 600
 601        /* adjust the touchpad to child's choice of protocol */
 602        if (child) {
 603                if (child->pktsize == 4)
 604                        priv->mode |= SYN_BIT_FOUR_BYTE_CLIENT;
 605                else
 606                        priv->mode &= ~SYN_BIT_FOUR_BYTE_CLIENT;
 607
 608                if (synaptics_mode_cmd(psmouse, priv->mode))
 609                        psmouse_warn(psmouse,
 610                                     "failed to switch guest protocol\n");
 611        }
 612}
 613
 614static void synaptics_pt_create(struct psmouse *psmouse)
 615{
 616        struct serio *serio;
 617
 618        serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
 619        if (!serio) {
 620                psmouse_err(psmouse,
 621                            "not enough memory for pass-through port\n");
 622                return;
 623        }
 624
 625        serio->id.type = SERIO_PS_PSTHRU;
 626        strlcpy(serio->name, "Synaptics pass-through", sizeof(serio->name));
 627        strlcpy(serio->phys, "synaptics-pt/serio0", sizeof(serio->name));
 628        serio->write = synaptics_pt_write;
 629        serio->start = synaptics_pt_start;
 630        serio->stop = synaptics_pt_stop;
 631        serio->parent = psmouse->ps2dev.serio;
 632
 633        psmouse->pt_activate = synaptics_pt_activate;
 634
 635        psmouse_info(psmouse, "serio: %s port at %s\n",
 636                     serio->name, psmouse->phys);
 637        serio_register_port(serio);
 638}
 639
 640/*****************************************************************************
 641 *      Functions to interpret the absolute mode packets
 642 ****************************************************************************/
 643
 644static void synaptics_mt_state_set(struct synaptics_mt_state *state, int count,
 645                                   int sgm, int agm)
 646{
 647        state->count = count;
 648        state->sgm = sgm;
 649        state->agm = agm;
 650}
 651
 652static void synaptics_parse_agm(const unsigned char buf[],
 653                                struct synaptics_data *priv,
 654                                struct synaptics_hw_state *hw)
 655{
 656        struct synaptics_hw_state *agm = &priv->agm;
 657        int agm_packet_type;
 658
 659        agm_packet_type = (buf[5] & 0x30) >> 4;
 660        switch (agm_packet_type) {
 661        case 1:
 662                /* Gesture packet: (x, y, z) half resolution */
 663                agm->w = hw->w;
 664                agm->x = (((buf[4] & 0x0f) << 8) | buf[1]) << 1;
 665                agm->y = (((buf[4] & 0xf0) << 4) | buf[2]) << 1;
 666                agm->z = ((buf[3] & 0x30) | (buf[5] & 0x0f)) << 1;
 667                break;
 668
 669        case 2:
 670                /* AGM-CONTACT packet: (count, sgm, agm) */
 671                synaptics_mt_state_set(&agm->mt_state, buf[1], buf[2], buf[4]);
 672                break;
 673
 674        default:
 675                break;
 676        }
 677
 678        /* Record that at least one AGM has been received since last SGM */
 679        priv->agm_pending = true;
 680}
 681
 682static void synaptics_parse_ext_buttons(const unsigned char buf[],
 683                                        struct synaptics_data *priv,
 684                                        struct synaptics_hw_state *hw)
 685{
 686        unsigned int ext_bits =
 687                (SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap) + 1) >> 1;
 688        unsigned int ext_mask = GENMASK(ext_bits - 1, 0);
 689
 690        hw->ext_buttons = buf[4] & ext_mask;
 691        hw->ext_buttons |= (buf[5] & ext_mask) << ext_bits;
 692}
 693
 694static int synaptics_parse_hw_state(const unsigned char buf[],
 695                                    struct synaptics_data *priv,
 696                                    struct synaptics_hw_state *hw)
 697{
 698        memset(hw, 0, sizeof(struct synaptics_hw_state));
 699
 700        if (SYN_MODEL_NEWABS(priv->model_id)) {
 701                hw->w = (((buf[0] & 0x30) >> 2) |
 702                         ((buf[0] & 0x04) >> 1) |
 703                         ((buf[3] & 0x04) >> 2));
 704
 705                hw->left  = (buf[0] & 0x01) ? 1 : 0;
 706                hw->right = (buf[0] & 0x02) ? 1 : 0;
 707
 708                if (SYN_CAP_CLICKPAD(priv->ext_cap_0c)) {
 709                        /*
 710                         * Clickpad's button is transmitted as middle button,
 711                         * however, since it is primary button, we will report
 712                         * it as BTN_LEFT.
 713                         */
 714                        hw->left = ((buf[0] ^ buf[3]) & 0x01) ? 1 : 0;
 715
 716                } else if (SYN_CAP_MIDDLE_BUTTON(priv->capabilities)) {
 717                        hw->middle = ((buf[0] ^ buf[3]) & 0x01) ? 1 : 0;
 718                        if (hw->w == 2)
 719                                hw->scroll = (signed char)(buf[1]);
 720                }
 721
 722                if (SYN_CAP_FOUR_BUTTON(priv->capabilities)) {
 723                        hw->up   = ((buf[0] ^ buf[3]) & 0x01) ? 1 : 0;
 724                        hw->down = ((buf[0] ^ buf[3]) & 0x02) ? 1 : 0;
 725                }
 726
 727                if ((SYN_CAP_ADV_GESTURE(priv->ext_cap_0c) ||
 728                        SYN_CAP_IMAGE_SENSOR(priv->ext_cap_0c)) &&
 729                    hw->w == 2) {
 730                        synaptics_parse_agm(buf, priv, hw);
 731                        return 1;
 732                }
 733
 734                hw->x = (((buf[3] & 0x10) << 8) |
 735                         ((buf[1] & 0x0f) << 8) |
 736                         buf[4]);
 737                hw->y = (((buf[3] & 0x20) << 7) |
 738                         ((buf[1] & 0xf0) << 4) |
 739                         buf[5]);
 740                hw->z = buf[2];
 741
 742                if (SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap) > 0 &&
 743                    ((buf[0] ^ buf[3]) & 0x02)) {
 744                        synaptics_parse_ext_buttons(buf, priv, hw);
 745                }
 746        } else {
 747                hw->x = (((buf[1] & 0x1f) << 8) | buf[2]);
 748                hw->y = (((buf[4] & 0x1f) << 8) | buf[5]);
 749
 750                hw->z = (((buf[0] & 0x30) << 2) | (buf[3] & 0x3F));
 751                hw->w = (((buf[1] & 0x80) >> 4) | ((buf[0] & 0x04) >> 1));
 752
 753                hw->left  = (buf[0] & 0x01) ? 1 : 0;
 754                hw->right = (buf[0] & 0x02) ? 1 : 0;
 755        }
 756
 757        /*
 758         * Convert wrap-around values to negative. (X|Y)_MAX_POSITIVE
 759         * is used by some firmware to indicate a finger at the edge of
 760         * the touchpad whose precise position cannot be determined, so
 761         * convert these values to the maximum axis value.
 762         */
 763        if (hw->x > X_MAX_POSITIVE)
 764                hw->x -= 1 << ABS_POS_BITS;
 765        else if (hw->x == X_MAX_POSITIVE)
 766                hw->x = XMAX;
 767
 768        if (hw->y > Y_MAX_POSITIVE)
 769                hw->y -= 1 << ABS_POS_BITS;
 770        else if (hw->y == Y_MAX_POSITIVE)
 771                hw->y = YMAX;
 772
 773        return 0;
 774}
 775
 776static void synaptics_report_semi_mt_slot(struct input_dev *dev, int slot,
 777                                          bool active, int x, int y)
 778{
 779        input_mt_slot(dev, slot);
 780        input_mt_report_slot_state(dev, MT_TOOL_FINGER, active);
 781        if (active) {
 782                input_report_abs(dev, ABS_MT_POSITION_X, x);
 783                input_report_abs(dev, ABS_MT_POSITION_Y, synaptics_invert_y(y));
 784        }
 785}
 786
 787static void synaptics_report_semi_mt_data(struct input_dev *dev,
 788                                          const struct synaptics_hw_state *a,
 789                                          const struct synaptics_hw_state *b,
 790                                          int num_fingers)
 791{
 792        if (num_fingers >= 2) {
 793                synaptics_report_semi_mt_slot(dev, 0, true, min(a->x, b->x),
 794                                              min(a->y, b->y));
 795                synaptics_report_semi_mt_slot(dev, 1, true, max(a->x, b->x),
 796                                              max(a->y, b->y));
 797        } else if (num_fingers == 1) {
 798                synaptics_report_semi_mt_slot(dev, 0, true, a->x, a->y);
 799                synaptics_report_semi_mt_slot(dev, 1, false, 0, 0);
 800        } else {
 801                synaptics_report_semi_mt_slot(dev, 0, false, 0, 0);
 802                synaptics_report_semi_mt_slot(dev, 1, false, 0, 0);
 803        }
 804}
 805
 806static void synaptics_report_ext_buttons(struct psmouse *psmouse,
 807                                         const struct synaptics_hw_state *hw)
 808{
 809        struct input_dev *dev = psmouse->dev;
 810        struct synaptics_data *priv = psmouse->private;
 811        int ext_bits = (SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap) + 1) >> 1;
 812        char buf[6] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
 813        int i;
 814
 815        if (!SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap))
 816                return;
 817
 818        /* Bug in FW 8.1 & 8.2, buttons are reported only when ExtBit is 1 */
 819        if ((SYN_ID_FULL(priv->identity) == 0x801 ||
 820             SYN_ID_FULL(priv->identity) == 0x802) &&
 821            !((psmouse->packet[0] ^ psmouse->packet[3]) & 0x02))
 822                return;
 823
 824        if (!SYN_CAP_EXT_BUTTONS_STICK(priv->ext_cap_10)) {
 825                for (i = 0; i < ext_bits; i++) {
 826                        input_report_key(dev, BTN_0 + 2 * i,
 827                                hw->ext_buttons & (1 << i));
 828                        input_report_key(dev, BTN_1 + 2 * i,
 829                                hw->ext_buttons & (1 << (i + ext_bits)));
 830                }
 831                return;
 832        }
 833
 834        /*
 835         * This generation of touchpads has the trackstick buttons
 836         * physically wired to the touchpad. Re-route them through
 837         * the pass-through interface.
 838         */
 839        if (!priv->pt_port)
 840                return;
 841
 842        /* The trackstick expects at most 3 buttons */
 843        priv->pt_buttons = SYN_CAP_EXT_BUTTON_STICK_L(hw->ext_buttons)      |
 844                           SYN_CAP_EXT_BUTTON_STICK_R(hw->ext_buttons) << 1 |
 845                           SYN_CAP_EXT_BUTTON_STICK_M(hw->ext_buttons) << 2;
 846
 847        synaptics_pass_pt_packet(psmouse, priv->pt_port, buf);
 848}
 849
 850static void synaptics_report_buttons(struct psmouse *psmouse,
 851                                     const struct synaptics_hw_state *hw)
 852{
 853        struct input_dev *dev = psmouse->dev;
 854        struct synaptics_data *priv = psmouse->private;
 855
 856        input_report_key(dev, BTN_LEFT, hw->left);
 857        input_report_key(dev, BTN_RIGHT, hw->right);
 858
 859        if (SYN_CAP_MIDDLE_BUTTON(priv->capabilities))
 860                input_report_key(dev, BTN_MIDDLE, hw->middle);
 861
 862        if (SYN_CAP_FOUR_BUTTON(priv->capabilities)) {
 863                input_report_key(dev, BTN_FORWARD, hw->up);
 864                input_report_key(dev, BTN_BACK, hw->down);
 865        }
 866
 867        synaptics_report_ext_buttons(psmouse, hw);
 868}
 869
 870static void synaptics_report_slot(struct input_dev *dev, int slot,
 871                                  const struct synaptics_hw_state *hw)
 872{
 873        input_mt_slot(dev, slot);
 874        input_mt_report_slot_state(dev, MT_TOOL_FINGER, (hw != NULL));
 875        if (!hw)
 876                return;
 877
 878        input_report_abs(dev, ABS_MT_POSITION_X, hw->x);
 879        input_report_abs(dev, ABS_MT_POSITION_Y, synaptics_invert_y(hw->y));
 880        input_report_abs(dev, ABS_MT_PRESSURE, hw->z);
 881}
 882
 883static void synaptics_report_mt_data(struct psmouse *psmouse,
 884                                     struct synaptics_mt_state *mt_state,
 885                                     const struct synaptics_hw_state *sgm)
 886{
 887        struct input_dev *dev = psmouse->dev;
 888        struct synaptics_data *priv = psmouse->private;
 889        struct synaptics_hw_state *agm = &priv->agm;
 890        struct synaptics_mt_state *old = &priv->mt_state;
 891
 892        switch (mt_state->count) {
 893        case 0:
 894                synaptics_report_slot(dev, 0, NULL);
 895                synaptics_report_slot(dev, 1, NULL);
 896                break;
 897        case 1:
 898                if (mt_state->sgm == -1) {
 899                        synaptics_report_slot(dev, 0, NULL);
 900                        synaptics_report_slot(dev, 1, NULL);
 901                } else if (mt_state->sgm == 0) {
 902                        synaptics_report_slot(dev, 0, sgm);
 903                        synaptics_report_slot(dev, 1, NULL);
 904                } else {
 905                        synaptics_report_slot(dev, 0, NULL);
 906                        synaptics_report_slot(dev, 1, sgm);
 907                }
 908                break;
 909        default:
 910                /*
 911                 * If the finger slot contained in SGM is valid, and either
 912                 * hasn't changed, or is new, or the old SGM has now moved to
 913                 * AGM, then report SGM in MTB slot 0.
 914                 * Otherwise, empty MTB slot 0.
 915                 */
 916                if (mt_state->sgm != -1 &&
 917                    (mt_state->sgm == old->sgm ||
 918                     old->sgm == -1 || mt_state->agm == old->sgm))
 919                        synaptics_report_slot(dev, 0, sgm);
 920                else
 921                        synaptics_report_slot(dev, 0, NULL);
 922
 923                /*
 924                 * If the finger slot contained in AGM is valid, and either
 925                 * hasn't changed, or is new, then report AGM in MTB slot 1.
 926                 * Otherwise, empty MTB slot 1.
 927                 *
 928                 * However, in the case where the AGM is new, make sure that
 929                 * that it is either the same as the old SGM, or there was no
 930                 * SGM.
 931                 *
 932                 * Otherwise, if the SGM was just 1, and the new AGM is 2, then
 933                 * the new AGM will keep the old SGM's tracking ID, which can
 934                 * cause apparent drumroll.  This happens if in the following
 935                 * valid finger sequence:
 936                 *
 937                 *  Action                 SGM  AGM (MTB slot:Contact)
 938                 *  1. Touch contact 0    (0:0)
 939                 *  2. Touch contact 1    (0:0, 1:1)
 940                 *  3. Lift  contact 0    (1:1)
 941                 *  4. Touch contacts 2,3 (0:2, 1:3)
 942                 *
 943                 * In step 4, contact 3, in AGM must not be given the same
 944                 * tracking ID as contact 1 had in step 3.  To avoid this,
 945                 * the first agm with contact 3 is dropped and slot 1 is
 946                 * invalidated (tracking ID = -1).
 947                 */
 948                if (mt_state->agm != -1 &&
 949                    (mt_state->agm == old->agm ||
 950                     (old->agm == -1 &&
 951                      (old->sgm == -1 || mt_state->agm == old->sgm))))
 952                        synaptics_report_slot(dev, 1, agm);
 953                else
 954                        synaptics_report_slot(dev, 1, NULL);
 955                break;
 956        }
 957
 958        /* Don't use active slot count to generate BTN_TOOL events. */
 959        input_mt_report_pointer_emulation(dev, false);
 960
 961        /* Send the number of fingers reported by touchpad itself. */
 962        input_mt_report_finger_count(dev, mt_state->count);
 963
 964        synaptics_report_buttons(psmouse, sgm);
 965
 966        input_sync(dev);
 967}
 968
 969/* Handle case where mt_state->count = 0 */
 970static void synaptics_image_sensor_0f(struct synaptics_data *priv,
 971                                      struct synaptics_mt_state *mt_state)
 972{
 973        synaptics_mt_state_set(mt_state, 0, -1, -1);
 974        priv->mt_state_lost = false;
 975}
 976
 977/* Handle case where mt_state->count = 1 */
 978static void synaptics_image_sensor_1f(struct synaptics_data *priv,
 979                                      struct synaptics_mt_state *mt_state)
 980{
 981        struct synaptics_hw_state *agm = &priv->agm;
 982        struct synaptics_mt_state *old = &priv->mt_state;
 983
 984        /*
 985         * If the last AGM was (0,0,0), and there is only one finger left,
 986         * then we absolutely know that SGM contains slot 0, and all other
 987         * fingers have been removed.
 988         */
 989        if (priv->agm_pending && agm->z == 0) {
 990                synaptics_mt_state_set(mt_state, 1, 0, -1);
 991                priv->mt_state_lost = false;
 992                return;
 993        }
 994
 995        switch (old->count) {
 996        case 0:
 997                synaptics_mt_state_set(mt_state, 1, 0, -1);
 998                break;
 999        case 1:
1000                /*
1001                 * If mt_state_lost, then the previous transition was 3->1,
1002                 * and SGM now contains either slot 0 or 1, but we don't know
1003                 * which.  So, we just assume that the SGM now contains slot 1.
1004                 *
1005                 * If pending AGM and either:
1006                 *   (a) the previous SGM slot contains slot 0, or
1007                 *   (b) there was no SGM slot
1008                 * then, the SGM now contains slot 1
1009                 *
1010                 * Case (a) happens with very rapid "drum roll" gestures, where
1011                 * slot 0 finger is lifted and a new slot 1 finger touches
1012                 * within one reporting interval.
1013                 *
1014                 * Case (b) happens if initially two or more fingers tap
1015                 * briefly, and all but one lift before the end of the first
1016                 * reporting interval.
1017                 *
1018                 * (In both these cases, slot 0 will becomes empty, so SGM
1019                 * contains slot 1 with the new finger)
1020                 *
1021                 * Else, if there was no previous SGM, it now contains slot 0.
1022                 *
1023                 * Otherwise, SGM still contains the same slot.
1024                 */
1025                if (priv->mt_state_lost ||
1026                    (priv->agm_pending && old->sgm <= 0))
1027                        synaptics_mt_state_set(mt_state, 1, 1, -1);
1028                else if (old->sgm == -1)
1029                        synaptics_mt_state_set(mt_state, 1, 0, -1);
1030                break;
1031        case 2:
1032                /*
1033                 * If mt_state_lost, we don't know which finger SGM contains.
1034                 *
1035                 * So, report 1 finger, but with both slots empty.
1036                 * We will use slot 1 on subsequent 1->1
1037                 */
1038                if (priv->mt_state_lost) {
1039                        synaptics_mt_state_set(mt_state, 1, -1, -1);
1040                        break;
1041                }
1042                /*
1043                 * Since the last AGM was NOT (0,0,0), it was the finger in
1044                 * slot 0 that has been removed.
1045                 * So, SGM now contains previous AGM's slot, and AGM is now
1046                 * empty.
1047                 */
1048                synaptics_mt_state_set(mt_state, 1, old->agm, -1);
1049                break;
1050        case 3:
1051                /*
1052                 * Since last AGM was not (0,0,0), we don't know which finger
1053                 * is left.
1054                 *
1055                 * So, report 1 finger, but with both slots empty.
1056                 * We will use slot 1 on subsequent 1->1
1057                 */
1058                synaptics_mt_state_set(mt_state, 1, -1, -1);
1059                priv->mt_state_lost = true;
1060                break;
1061        case 4:
1062        case 5:
1063                /* mt_state was updated by AGM-CONTACT packet */
1064                break;
1065        }
1066}
1067
1068/* Handle case where mt_state->count = 2 */
1069static void synaptics_image_sensor_2f(struct synaptics_data *priv,
1070                                      struct synaptics_mt_state *mt_state)
1071{
1072        struct synaptics_mt_state *old = &priv->mt_state;
1073
1074        switch (old->count) {
1075        case 0:
1076                synaptics_mt_state_set(mt_state, 2, 0, 1);
1077                break;
1078        case 1:
1079                /*
1080                 * If previous SGM contained slot 1 or higher, SGM now contains
1081                 * slot 0 (the newly touching finger) and AGM contains SGM's
1082                 * previous slot.
1083                 *
1084                 * Otherwise, SGM still contains slot 0 and AGM now contains
1085                 * slot 1.
1086                 */
1087                if (old->sgm >= 1)
1088                        synaptics_mt_state_set(mt_state, 2, 0, old->sgm);
1089                else
1090                        synaptics_mt_state_set(mt_state, 2, 0, 1);
1091                break;
1092        case 2:
1093                /*
1094                 * If mt_state_lost, SGM now contains either finger 1 or 2, but
1095                 * we don't know which.
1096                 * So, we just assume that the SGM contains slot 0 and AGM 1.
1097                 */
1098                if (priv->mt_state_lost)
1099                        synaptics_mt_state_set(mt_state, 2, 0, 1);
1100                /*
1101                 * Otherwise, use the same mt_state, since it either hasn't
1102                 * changed, or was updated by a recently received AGM-CONTACT
1103                 * packet.
1104                 */
1105                break;
1106        case 3:
1107                /*
1108                 * 3->2 transitions have two unsolvable problems:
1109                 *  1) no indication is given which finger was removed
1110                 *  2) no way to tell if agm packet was for finger 3
1111                 *     before 3->2, or finger 2 after 3->2.
1112                 *
1113                 * So, report 2 fingers, but empty all slots.
1114                 * We will guess slots [0,1] on subsequent 2->2.
1115                 */
1116                synaptics_mt_state_set(mt_state, 2, -1, -1);
1117                priv->mt_state_lost = true;
1118                break;
1119        case 4:
1120        case 5:
1121                /* mt_state was updated by AGM-CONTACT packet */
1122                break;
1123        }
1124}
1125
1126/* Handle case where mt_state->count = 3 */
1127static void synaptics_image_sensor_3f(struct synaptics_data *priv,
1128                                      struct synaptics_mt_state *mt_state)
1129{
1130        struct synaptics_mt_state *old = &priv->mt_state;
1131
1132        switch (old->count) {
1133        case 0:
1134                synaptics_mt_state_set(mt_state, 3, 0, 2);
1135                break;
1136        case 1:
1137                /*
1138                 * If previous SGM contained slot 2 or higher, SGM now contains
1139                 * slot 0 (one of the newly touching fingers) and AGM contains
1140                 * SGM's previous slot.
1141                 *
1142                 * Otherwise, SGM now contains slot 0 and AGM contains slot 2.
1143                 */
1144                if (old->sgm >= 2)
1145                        synaptics_mt_state_set(mt_state, 3, 0, old->sgm);
1146                else
1147                        synaptics_mt_state_set(mt_state, 3, 0, 2);
1148                break;
1149        case 2:
1150                /*
1151                 * If the AGM previously contained slot 3 or higher, then the
1152                 * newly touching finger is in the lowest available slot.
1153                 *
1154                 * If SGM was previously 1 or higher, then the new SGM is
1155                 * now slot 0 (with a new finger), otherwise, the new finger
1156                 * is now in a hidden slot between 0 and AGM's slot.
1157                 *
1158                 * In all such cases, the SGM now contains slot 0, and the AGM
1159                 * continues to contain the same slot as before.
1160                 */
1161                if (old->agm >= 3) {
1162                        synaptics_mt_state_set(mt_state, 3, 0, old->agm);
1163                        break;
1164                }
1165
1166                /*
1167                 * After some 3->1 and all 3->2 transitions, we lose track
1168                 * of which slot is reported by SGM and AGM.
1169                 *
1170                 * For 2->3 in this state, report 3 fingers, but empty all
1171                 * slots, and we will guess (0,2) on a subsequent 0->3.
1172                 *
1173                 * To userspace, the resulting transition will look like:
1174                 *    2:[0,1] -> 3:[-1,-1] -> 3:[0,2]
1175                 */
1176                if (priv->mt_state_lost) {
1177                        synaptics_mt_state_set(mt_state, 3, -1, -1);
1178                        break;
1179                }
1180
1181                /*
1182                 * If the (SGM,AGM) really previously contained slots (0, 1),
1183                 * then we cannot know what slot was just reported by the AGM,
1184                 * because the 2->3 transition can occur either before or after
1185                 * the AGM packet. Thus, this most recent AGM could contain
1186                 * either the same old slot 1 or the new slot 2.
1187                 * Subsequent AGMs will be reporting slot 2.
1188                 *
1189                 * To userspace, the resulting transition will look like:
1190                 *    2:[0,1] -> 3:[0,-1] -> 3:[0,2]
1191                 */
1192                synaptics_mt_state_set(mt_state, 3, 0, -1);
1193                break;
1194        case 3:
1195                /*
1196                 * If, for whatever reason, the previous agm was invalid,
1197                 * Assume SGM now contains slot 0, AGM now contains slot 2.
1198                 */
1199                if (old->agm <= 2)
1200                        synaptics_mt_state_set(mt_state, 3, 0, 2);
1201                /*
1202                 * mt_state either hasn't changed, or was updated by a recently
1203                 * received AGM-CONTACT packet.
1204                 */
1205                break;
1206
1207        case 4:
1208        case 5:
1209                /* mt_state was updated by AGM-CONTACT packet */
1210                break;
1211        }
1212}
1213
1214/* Handle case where mt_state->count = 4, or = 5 */
1215static void synaptics_image_sensor_45f(struct synaptics_data *priv,
1216                                       struct synaptics_mt_state *mt_state)
1217{
1218        /* mt_state was updated correctly by AGM-CONTACT packet */
1219        priv->mt_state_lost = false;
1220}
1221
1222static void synaptics_image_sensor_process(struct psmouse *psmouse,
1223                                           struct synaptics_hw_state *sgm)
1224{
1225        struct synaptics_data *priv = psmouse->private;
1226        struct synaptics_hw_state *agm = &priv->agm;
1227        struct synaptics_mt_state mt_state;
1228
1229        /* Initialize using current mt_state (as updated by last agm) */
1230        mt_state = agm->mt_state;
1231
1232        /*
1233         * Update mt_state using the new finger count and current mt_state.
1234         */
1235        if (sgm->z == 0)
1236                synaptics_image_sensor_0f(priv, &mt_state);
1237        else if (sgm->w >= 4)
1238                synaptics_image_sensor_1f(priv, &mt_state);
1239        else if (sgm->w == 0)
1240                synaptics_image_sensor_2f(priv, &mt_state);
1241        else if (sgm->w == 1 && mt_state.count <= 3)
1242                synaptics_image_sensor_3f(priv, &mt_state);
1243        else
1244                synaptics_image_sensor_45f(priv, &mt_state);
1245
1246        /* Send resulting input events to user space */
1247        synaptics_report_mt_data(psmouse, &mt_state, sgm);
1248
1249        /* Store updated mt_state */
1250        priv->mt_state = agm->mt_state = mt_state;
1251        priv->agm_pending = false;
1252}
1253
1254/*
1255 *  called for each full received packet from the touchpad
1256 */
1257static void synaptics_process_packet(struct psmouse *psmouse)
1258{
1259        struct input_dev *dev = psmouse->dev;
1260        struct synaptics_data *priv = psmouse->private;
1261        struct synaptics_hw_state hw;
1262        int num_fingers;
1263        int finger_width;
1264
1265        if (synaptics_parse_hw_state(psmouse->packet, priv, &hw))
1266                return;
1267
1268        if (SYN_CAP_IMAGE_SENSOR(priv->ext_cap_0c)) {
1269                synaptics_image_sensor_process(psmouse, &hw);
1270                return;
1271        }
1272
1273        if (hw.scroll) {
1274                priv->scroll += hw.scroll;
1275
1276                while (priv->scroll >= 4) {
1277                        input_report_key(dev, BTN_BACK, !hw.down);
1278                        input_sync(dev);
1279                        input_report_key(dev, BTN_BACK, hw.down);
1280                        input_sync(dev);
1281                        priv->scroll -= 4;
1282                }
1283                while (priv->scroll <= -4) {
1284                        input_report_key(dev, BTN_FORWARD, !hw.up);
1285                        input_sync(dev);
1286                        input_report_key(dev, BTN_FORWARD, hw.up);
1287                        input_sync(dev);
1288                        priv->scroll += 4;
1289                }
1290                return;
1291        }
1292
1293        if (hw.z > 0 && hw.x > 1) {
1294                num_fingers = 1;
1295                finger_width = 5;
1296                if (SYN_CAP_EXTENDED(priv->capabilities)) {
1297                        switch (hw.w) {
1298                        case 0 ... 1:
1299                                if (SYN_CAP_MULTIFINGER(priv->capabilities))
1300                                        num_fingers = hw.w + 2;
1301                                break;
1302                        case 2:
1303                                if (SYN_MODEL_PEN(priv->model_id))
1304                                        ;   /* Nothing, treat a pen as a single finger */
1305                                break;
1306                        case 4 ... 15:
1307                                if (SYN_CAP_PALMDETECT(priv->capabilities))
1308                                        finger_width = hw.w;
1309                                break;
1310                        }
1311                }
1312        } else {
1313                num_fingers = 0;
1314                finger_width = 0;
1315        }
1316
1317        if (SYN_CAP_ADV_GESTURE(priv->ext_cap_0c))
1318                synaptics_report_semi_mt_data(dev, &hw, &priv->agm,
1319                                              num_fingers);
1320
1321        /* Post events
1322         * BTN_TOUCH has to be first as mousedev relies on it when doing
1323         * absolute -> relative conversion
1324         */
1325        if (hw.z > 30) input_report_key(dev, BTN_TOUCH, 1);
1326        if (hw.z < 25) input_report_key(dev, BTN_TOUCH, 0);
1327
1328        if (num_fingers > 0) {
1329                input_report_abs(dev, ABS_X, hw.x);
1330                input_report_abs(dev, ABS_Y, synaptics_invert_y(hw.y));
1331        }
1332        input_report_abs(dev, ABS_PRESSURE, hw.z);
1333
1334        if (SYN_CAP_PALMDETECT(priv->capabilities))
1335                input_report_abs(dev, ABS_TOOL_WIDTH, finger_width);
1336
1337        input_report_key(dev, BTN_TOOL_FINGER, num_fingers == 1);
1338        if (SYN_CAP_MULTIFINGER(priv->capabilities)) {
1339                input_report_key(dev, BTN_TOOL_DOUBLETAP, num_fingers == 2);
1340                input_report_key(dev, BTN_TOOL_TRIPLETAP, num_fingers == 3);
1341        }
1342
1343        synaptics_report_buttons(psmouse, &hw);
1344
1345        input_sync(dev);
1346}
1347
1348static int synaptics_validate_byte(struct psmouse *psmouse,
1349                                   int idx, unsigned char pkt_type)
1350{
1351        static const unsigned char newabs_mask[]        = { 0xC8, 0x00, 0x00, 0xC8, 0x00 };
1352        static const unsigned char newabs_rel_mask[]    = { 0xC0, 0x00, 0x00, 0xC0, 0x00 };
1353        static const unsigned char newabs_rslt[]        = { 0x80, 0x00, 0x00, 0xC0, 0x00 };
1354        static const unsigned char oldabs_mask[]        = { 0xC0, 0x60, 0x00, 0xC0, 0x60 };
1355        static const unsigned char oldabs_rslt[]        = { 0xC0, 0x00, 0x00, 0x80, 0x00 };
1356        const char *packet = psmouse->packet;
1357
1358        if (idx < 0 || idx > 4)
1359                return 0;
1360
1361        switch (pkt_type) {
1362
1363        case SYN_NEWABS:
1364        case SYN_NEWABS_RELAXED:
1365                return (packet[idx] & newabs_rel_mask[idx]) == newabs_rslt[idx];
1366
1367        case SYN_NEWABS_STRICT:
1368                return (packet[idx] & newabs_mask[idx]) == newabs_rslt[idx];
1369
1370        case SYN_OLDABS:
1371                return (packet[idx] & oldabs_mask[idx]) == oldabs_rslt[idx];
1372
1373        default:
1374                psmouse_err(psmouse, "unknown packet type %d\n", pkt_type);
1375                return 0;
1376        }
1377}
1378
1379static unsigned char synaptics_detect_pkt_type(struct psmouse *psmouse)
1380{
1381        int i;
1382
1383        for (i = 0; i < 5; i++)
1384                if (!synaptics_validate_byte(psmouse, i, SYN_NEWABS_STRICT)) {
1385                        psmouse_info(psmouse, "using relaxed packet validation\n");
1386                        return SYN_NEWABS_RELAXED;
1387                }
1388
1389        return SYN_NEWABS_STRICT;
1390}
1391
1392static psmouse_ret_t synaptics_process_byte(struct psmouse *psmouse)
1393{
1394        struct synaptics_data *priv = psmouse->private;
1395
1396        if (psmouse->pktcnt >= 6) { /* Full packet received */
1397                if (unlikely(priv->pkt_type == SYN_NEWABS))
1398                        priv->pkt_type = synaptics_detect_pkt_type(psmouse);
1399
1400                if (SYN_CAP_PASS_THROUGH(priv->capabilities) &&
1401                    synaptics_is_pt_packet(psmouse->packet)) {
1402                        if (priv->pt_port)
1403                                synaptics_pass_pt_packet(psmouse, priv->pt_port,
1404                                                         psmouse->packet);
1405                } else
1406                        synaptics_process_packet(psmouse);
1407
1408                return PSMOUSE_FULL_PACKET;
1409        }
1410
1411        return synaptics_validate_byte(psmouse, psmouse->pktcnt - 1, priv->pkt_type) ?
1412                PSMOUSE_GOOD_DATA : PSMOUSE_BAD_DATA;
1413}
1414
1415/*****************************************************************************
1416 *      Driver initialization/cleanup functions
1417 ****************************************************************************/
1418static void set_abs_position_params(struct input_dev *dev,
1419                                    struct synaptics_data *priv, int x_code,
1420                                    int y_code)
1421{
1422        int x_min = priv->x_min ?: XMIN_NOMINAL;
1423        int x_max = priv->x_max ?: XMAX_NOMINAL;
1424        int y_min = priv->y_min ?: YMIN_NOMINAL;
1425        int y_max = priv->y_max ?: YMAX_NOMINAL;
1426        int fuzz = SYN_CAP_REDUCED_FILTERING(priv->ext_cap_0c) ?
1427                        SYN_REDUCED_FILTER_FUZZ : 0;
1428
1429        input_set_abs_params(dev, x_code, x_min, x_max, fuzz, 0);
1430        input_set_abs_params(dev, y_code, y_min, y_max, fuzz, 0);
1431        input_abs_set_res(dev, x_code, priv->x_res);
1432        input_abs_set_res(dev, y_code, priv->y_res);
1433}
1434
1435static void set_input_params(struct psmouse *psmouse,
1436                             struct synaptics_data *priv)
1437{
1438        struct input_dev *dev = psmouse->dev;
1439        int i;
1440
1441        /* Things that apply to both modes */
1442        __set_bit(INPUT_PROP_POINTER, dev->propbit);
1443        __set_bit(EV_KEY, dev->evbit);
1444        __set_bit(BTN_LEFT, dev->keybit);
1445        __set_bit(BTN_RIGHT, dev->keybit);
1446
1447        if (SYN_CAP_MIDDLE_BUTTON(priv->capabilities))
1448                __set_bit(BTN_MIDDLE, dev->keybit);
1449
1450        if (!priv->absolute_mode) {
1451                /* Relative mode */
1452                __set_bit(EV_REL, dev->evbit);
1453                __set_bit(REL_X, dev->relbit);
1454                __set_bit(REL_Y, dev->relbit);
1455                return;
1456        }
1457
1458        /* Absolute mode */
1459        __set_bit(EV_ABS, dev->evbit);
1460        set_abs_position_params(dev, priv, ABS_X, ABS_Y);
1461        input_set_abs_params(dev, ABS_PRESSURE, 0, 255, 0, 0);
1462
1463        if (SYN_CAP_IMAGE_SENSOR(priv->ext_cap_0c)) {
1464                set_abs_position_params(dev, priv, ABS_MT_POSITION_X,
1465                                        ABS_MT_POSITION_Y);
1466                /* Image sensors can report per-contact pressure */
1467                input_set_abs_params(dev, ABS_MT_PRESSURE, 0, 255, 0, 0);
1468                input_mt_init_slots(dev, 2, INPUT_MT_POINTER);
1469
1470                /* Image sensors can signal 4 and 5 finger clicks */
1471                __set_bit(BTN_TOOL_QUADTAP, dev->keybit);
1472                __set_bit(BTN_TOOL_QUINTTAP, dev->keybit);
1473        } else if (SYN_CAP_ADV_GESTURE(priv->ext_cap_0c)) {
1474                /* Non-image sensors with AGM use semi-mt */
1475                __set_bit(INPUT_PROP_SEMI_MT, dev->propbit);
1476                input_mt_init_slots(dev, 2, 0);
1477                set_abs_position_params(dev, priv, ABS_MT_POSITION_X,
1478                                        ABS_MT_POSITION_Y);
1479        }
1480
1481        if (SYN_CAP_PALMDETECT(priv->capabilities))
1482                input_set_abs_params(dev, ABS_TOOL_WIDTH, 0, 15, 0, 0);
1483
1484        __set_bit(BTN_TOUCH, dev->keybit);
1485        __set_bit(BTN_TOOL_FINGER, dev->keybit);
1486
1487        if (SYN_CAP_MULTIFINGER(priv->capabilities)) {
1488                __set_bit(BTN_TOOL_DOUBLETAP, dev->keybit);
1489                __set_bit(BTN_TOOL_TRIPLETAP, dev->keybit);
1490        }
1491
1492        if (SYN_CAP_FOUR_BUTTON(priv->capabilities) ||
1493            SYN_CAP_MIDDLE_BUTTON(priv->capabilities)) {
1494                __set_bit(BTN_FORWARD, dev->keybit);
1495                __set_bit(BTN_BACK, dev->keybit);
1496        }
1497
1498        if (!SYN_CAP_EXT_BUTTONS_STICK(priv->ext_cap_10))
1499                for (i = 0; i < SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap); i++)
1500                        __set_bit(BTN_0 + i, dev->keybit);
1501
1502        __clear_bit(EV_REL, dev->evbit);
1503        __clear_bit(REL_X, dev->relbit);
1504        __clear_bit(REL_Y, dev->relbit);
1505
1506        if (SYN_CAP_CLICKPAD(priv->ext_cap_0c)) {
1507                __set_bit(INPUT_PROP_BUTTONPAD, dev->propbit);
1508                if (psmouse_matches_pnp_id(psmouse, topbuttonpad_pnp_ids) &&
1509                    !SYN_CAP_EXT_BUTTONS_STICK(priv->ext_cap_10))
1510                        __set_bit(INPUT_PROP_TOPBUTTONPAD, dev->propbit);
1511                /* Clickpads report only left button */
1512                __clear_bit(BTN_RIGHT, dev->keybit);
1513                __clear_bit(BTN_MIDDLE, dev->keybit);
1514        }
1515}
1516
1517static ssize_t synaptics_show_disable_gesture(struct psmouse *psmouse,
1518                                              void *data, char *buf)
1519{
1520        struct synaptics_data *priv = psmouse->private;
1521
1522        return sprintf(buf, "%c\n", priv->disable_gesture ? '1' : '0');
1523}
1524
1525static ssize_t synaptics_set_disable_gesture(struct psmouse *psmouse,
1526                                             void *data, const char *buf,
1527                                             size_t len)
1528{
1529        struct synaptics_data *priv = psmouse->private;
1530        unsigned int value;
1531        int err;
1532
1533        err = kstrtouint(buf, 10, &value);
1534        if (err)
1535                return err;
1536
1537        if (value > 1)
1538                return -EINVAL;
1539
1540        if (value == priv->disable_gesture)
1541                return len;
1542
1543        priv->disable_gesture = value;
1544        if (value)
1545                priv->mode |= SYN_BIT_DISABLE_GESTURE;
1546        else
1547                priv->mode &= ~SYN_BIT_DISABLE_GESTURE;
1548
1549        if (synaptics_mode_cmd(psmouse, priv->mode))
1550                return -EIO;
1551
1552        return len;
1553}
1554
1555PSMOUSE_DEFINE_ATTR(disable_gesture, S_IWUSR | S_IRUGO, NULL,
1556                    synaptics_show_disable_gesture,
1557                    synaptics_set_disable_gesture);
1558
1559static void synaptics_disconnect(struct psmouse *psmouse)
1560{
1561        struct synaptics_data *priv = psmouse->private;
1562
1563        if (!priv->absolute_mode && SYN_ID_DISGEST_SUPPORTED(priv->identity))
1564                device_remove_file(&psmouse->ps2dev.serio->dev,
1565                                   &psmouse_attr_disable_gesture.dattr);
1566
1567        synaptics_reset(psmouse);
1568        kfree(priv);
1569        psmouse->private = NULL;
1570}
1571
1572static int synaptics_reconnect(struct psmouse *psmouse)
1573{
1574        struct synaptics_data *priv = psmouse->private;
1575        struct synaptics_data old_priv = *priv;
1576        unsigned char param[2];
1577        int retry = 0;
1578        int error;
1579
1580        do {
1581                psmouse_reset(psmouse);
1582                if (retry) {
1583                        /*
1584                         * On some boxes, right after resuming, the touchpad
1585                         * needs some time to finish initializing (I assume
1586                         * it needs time to calibrate) and start responding
1587                         * to Synaptics-specific queries, so let's wait a
1588                         * bit.
1589                         */
1590                        ssleep(1);
1591                }
1592                ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_GETID);
1593                error = synaptics_detect(psmouse, 0);
1594        } while (error && ++retry < 3);
1595
1596        if (error)
1597                return -1;
1598
1599        if (retry > 1)
1600                psmouse_dbg(psmouse, "reconnected after %d tries\n", retry);
1601
1602        if (synaptics_query_hardware(psmouse)) {
1603                psmouse_err(psmouse, "Unable to query device.\n");
1604                return -1;
1605        }
1606
1607        if (synaptics_set_mode(psmouse)) {
1608                psmouse_err(psmouse, "Unable to initialize device.\n");
1609                return -1;
1610        }
1611
1612        if (old_priv.identity != priv->identity ||
1613            old_priv.model_id != priv->model_id ||
1614            old_priv.capabilities != priv->capabilities ||
1615            old_priv.ext_cap != priv->ext_cap) {
1616                psmouse_err(psmouse,
1617                            "hardware appears to be different: id(%ld-%ld), model(%ld-%ld), caps(%lx-%lx), ext(%lx-%lx).\n",
1618                            old_priv.identity, priv->identity,
1619                            old_priv.model_id, priv->model_id,
1620                            old_priv.capabilities, priv->capabilities,
1621                            old_priv.ext_cap, priv->ext_cap);
1622                return -1;
1623        }
1624
1625        return 0;
1626}
1627
1628static bool impaired_toshiba_kbc;
1629
1630static const struct dmi_system_id toshiba_dmi_table[] __initconst = {
1631#if defined(CONFIG_DMI) && defined(CONFIG_X86)
1632        {
1633                /* Toshiba Satellite */
1634                .matches = {
1635                        DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
1636                        DMI_MATCH(DMI_PRODUCT_NAME, "Satellite"),
1637                },
1638        },
1639        {
1640                /* Toshiba Dynabook */
1641                .matches = {
1642                        DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
1643                        DMI_MATCH(DMI_PRODUCT_NAME, "dynabook"),
1644                },
1645        },
1646        {
1647                /* Toshiba Portege M300 */
1648                .matches = {
1649                        DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
1650                        DMI_MATCH(DMI_PRODUCT_NAME, "PORTEGE M300"),
1651                },
1652
1653        },
1654        {
1655                /* Toshiba Portege M300 */
1656                .matches = {
1657                        DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
1658                        DMI_MATCH(DMI_PRODUCT_NAME, "Portable PC"),
1659                        DMI_MATCH(DMI_PRODUCT_VERSION, "Version 1.0"),
1660                },
1661
1662        },
1663#endif
1664        { }
1665};
1666
1667static bool broken_olpc_ec;
1668
1669static const struct dmi_system_id olpc_dmi_table[] __initconst = {
1670#if defined(CONFIG_DMI) && defined(CONFIG_OLPC)
1671        {
1672                /* OLPC XO-1 or XO-1.5 */
1673                .matches = {
1674                        DMI_MATCH(DMI_SYS_VENDOR, "OLPC"),
1675                        DMI_MATCH(DMI_PRODUCT_NAME, "XO"),
1676                },
1677        },
1678#endif
1679        { }
1680};
1681
1682void __init synaptics_module_init(void)
1683{
1684        impaired_toshiba_kbc = dmi_check_system(toshiba_dmi_table);
1685        broken_olpc_ec = dmi_check_system(olpc_dmi_table);
1686}
1687
1688static int __synaptics_init(struct psmouse *psmouse, bool absolute_mode)
1689{
1690        struct synaptics_data *priv;
1691        int err = -1;
1692
1693        /*
1694         * The OLPC XO has issues with Synaptics' absolute mode; the constant
1695         * packet spew overloads the EC such that key presses on the keyboard
1696         * are missed.  Given that, don't even attempt to use Absolute mode.
1697         * Relative mode seems to work just fine.
1698         */
1699        if (absolute_mode && broken_olpc_ec) {
1700                psmouse_info(psmouse,
1701                             "OLPC XO detected, not enabling Synaptics protocol.\n");
1702                return -ENODEV;
1703        }
1704
1705        psmouse->private = priv = kzalloc(sizeof(struct synaptics_data), GFP_KERNEL);
1706        if (!priv)
1707                return -ENOMEM;
1708
1709        psmouse_reset(psmouse);
1710
1711        if (synaptics_query_hardware(psmouse)) {
1712                psmouse_err(psmouse, "Unable to query device.\n");
1713                goto init_fail;
1714        }
1715
1716        priv->absolute_mode = absolute_mode;
1717        if (SYN_ID_DISGEST_SUPPORTED(priv->identity))
1718                priv->disable_gesture = true;
1719
1720        if (synaptics_set_mode(psmouse)) {
1721                psmouse_err(psmouse, "Unable to initialize device.\n");
1722                goto init_fail;
1723        }
1724
1725        priv->pkt_type = SYN_MODEL_NEWABS(priv->model_id) ? SYN_NEWABS : SYN_OLDABS;
1726
1727        psmouse_info(psmouse,
1728                     "Touchpad model: %ld, fw: %ld.%ld, id: %#lx, caps: %#lx/%#lx/%#lx, board id: %lu, fw id: %lu\n",
1729                     SYN_ID_MODEL(priv->identity),
1730                     SYN_ID_MAJOR(priv->identity), SYN_ID_MINOR(priv->identity),
1731                     priv->model_id,
1732                     priv->capabilities, priv->ext_cap, priv->ext_cap_0c,
1733                     priv->board_id, priv->firmware_id);
1734
1735        set_input_params(psmouse, priv);
1736
1737        /*
1738         * Encode touchpad model so that it can be used to set
1739         * input device->id.version and be visible to userspace.
1740         * Because version is __u16 we have to drop something.
1741         * Hardware info bits seem to be good candidates as they
1742         * are documented to be for Synaptics corp. internal use.
1743         */
1744        psmouse->model = ((priv->model_id & 0x00ff0000) >> 8) |
1745                          (priv->model_id & 0x000000ff);
1746
1747        if (absolute_mode) {
1748                psmouse->protocol_handler = synaptics_process_byte;
1749                psmouse->pktsize = 6;
1750        } else {
1751                /* Relative mode follows standard PS/2 mouse protocol */
1752                psmouse->protocol_handler = psmouse_process_byte;
1753                psmouse->pktsize = 3;
1754        }
1755
1756        psmouse->set_rate = synaptics_set_rate;
1757        psmouse->disconnect = synaptics_disconnect;
1758        psmouse->reconnect = synaptics_reconnect;
1759        psmouse->cleanup = synaptics_reset;
1760        /* Synaptics can usually stay in sync without extra help */
1761        psmouse->resync_time = 0;
1762
1763        if (SYN_CAP_PASS_THROUGH(priv->capabilities))
1764                synaptics_pt_create(psmouse);
1765
1766        /*
1767         * Toshiba's KBC seems to have trouble handling data from
1768         * Synaptics at full rate.  Switch to a lower rate (roughly
1769         * the same rate as a standard PS/2 mouse).
1770         */
1771        if (psmouse->rate >= 80 && impaired_toshiba_kbc) {
1772                psmouse_info(psmouse,
1773                             "Toshiba %s detected, limiting rate to 40pps.\n",
1774                             dmi_get_system_info(DMI_PRODUCT_NAME));
1775                psmouse->rate = 40;
1776        }
1777
1778        if (!priv->absolute_mode && SYN_ID_DISGEST_SUPPORTED(priv->identity)) {
1779                err = device_create_file(&psmouse->ps2dev.serio->dev,
1780                                         &psmouse_attr_disable_gesture.dattr);
1781                if (err) {
1782                        psmouse_err(psmouse,
1783                                    "Failed to create disable_gesture attribute (%d)",
1784                                    err);
1785                        goto init_fail;
1786                }
1787        }
1788
1789        return 0;
1790
1791 init_fail:
1792        kfree(priv);
1793        return err;
1794}
1795
1796int synaptics_init(struct psmouse *psmouse)
1797{
1798        return __synaptics_init(psmouse, true);
1799}
1800
1801int synaptics_init_relative(struct psmouse *psmouse)
1802{
1803        return __synaptics_init(psmouse, false);
1804}
1805
1806bool synaptics_supported(void)
1807{
1808        return true;
1809}
1810
1811#else /* CONFIG_MOUSE_PS2_SYNAPTICS */
1812
1813void __init synaptics_module_init(void)
1814{
1815}
1816
1817int synaptics_init(struct psmouse *psmouse)
1818{
1819        return -ENOSYS;
1820}
1821
1822bool synaptics_supported(void)
1823{
1824        return false;
1825}
1826
1827#endif /* CONFIG_MOUSE_PS2_SYNAPTICS */
1828