linux/drivers/input/mouse/elantech.c
<<
>>
Prefs
   1/*
   2 * Elantech Touchpad driver (v6)
   3 *
   4 * Copyright (C) 2007-2009 Arjan Opmeer <arjan@opmeer.net>
   5 *
   6 * This program is free software; you can redistribute it and/or modify it
   7 * under the terms of the GNU General Public License version 2 as published
   8 * by the Free Software Foundation.
   9 *
  10 * Trademarks are the property of their respective owners.
  11 */
  12
  13#define pr_fmt(fmt) KBUILD_BASENAME ": " fmt
  14
  15#include <linux/delay.h>
  16#include <linux/slab.h>
  17#include <linux/module.h>
  18#include <linux/input.h>
  19#include <linux/serio.h>
  20#include <linux/libps2.h>
  21#include "psmouse.h"
  22#include "elantech.h"
  23
  24#define elantech_debug(fmt, ...)                                        \
  25        do {                                                            \
  26                if (etd->debug)                                         \
  27                        printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__);  \
  28        } while (0)
  29
  30static bool force_elantech;
  31module_param_named(force_elantech, force_elantech, bool, 0644);
  32MODULE_PARM_DESC(force_elantech, "Force the Elantech PS/2 protocol extension to be used, 1 = enabled, 0 = disabled (default).");
  33
  34/*
  35 * Send a Synaptics style sliced query command
  36 */
  37static int synaptics_send_cmd(struct psmouse *psmouse, unsigned char c,
  38                                unsigned char *param)
  39{
  40        if (psmouse_sliced_command(psmouse, c) ||
  41            ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_GETINFO)) {
  42                pr_err("synaptics_send_cmd query 0x%02x failed.\n", c);
  43                return -1;
  44        }
  45
  46        return 0;
  47}
  48
  49/*
  50 * A retrying version of ps2_command
  51 */
  52static int elantech_ps2_command(struct psmouse *psmouse,
  53                                unsigned char *param, int command)
  54{
  55        struct ps2dev *ps2dev = &psmouse->ps2dev;
  56        struct elantech_data *etd = psmouse->private;
  57        int rc;
  58        int tries = ETP_PS2_COMMAND_TRIES;
  59
  60        do {
  61                rc = ps2_command(ps2dev, param, command);
  62                if (rc == 0)
  63                        break;
  64                tries--;
  65                elantech_debug("retrying ps2 command 0x%02x (%d).\n",
  66                                command, tries);
  67                msleep(ETP_PS2_COMMAND_DELAY);
  68        } while (tries > 0);
  69
  70        if (rc)
  71                pr_err("ps2 command 0x%02x failed.\n", command);
  72
  73        return rc;
  74}
  75
  76/*
  77 * Send an Elantech style special command to read a value from a register
  78 */
  79static int elantech_read_reg(struct psmouse *psmouse, unsigned char reg,
  80                                unsigned char *val)
  81{
  82        struct elantech_data *etd = psmouse->private;
  83        unsigned char param[3];
  84        int rc = 0;
  85
  86        if (reg < 0x10 || reg > 0x26)
  87                return -1;
  88
  89        if (reg > 0x11 && reg < 0x20)
  90                return -1;
  91
  92        switch (etd->hw_version) {
  93        case 1:
  94                if (psmouse_sliced_command(psmouse, ETP_REGISTER_READ) ||
  95                    psmouse_sliced_command(psmouse, reg) ||
  96                    ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_GETINFO)) {
  97                        rc = -1;
  98                }
  99                break;
 100
 101        case 2:
 102                if (elantech_ps2_command(psmouse,  NULL, ETP_PS2_CUSTOM_COMMAND) ||
 103                    elantech_ps2_command(psmouse,  NULL, ETP_REGISTER_READ) ||
 104                    elantech_ps2_command(psmouse,  NULL, ETP_PS2_CUSTOM_COMMAND) ||
 105                    elantech_ps2_command(psmouse,  NULL, reg) ||
 106                    elantech_ps2_command(psmouse, param, PSMOUSE_CMD_GETINFO)) {
 107                        rc = -1;
 108                }
 109                break;
 110        }
 111
 112        if (rc)
 113                pr_err("failed to read register 0x%02x.\n", reg);
 114        else
 115                *val = param[0];
 116
 117        return rc;
 118}
 119
 120/*
 121 * Send an Elantech style special command to write a register with a value
 122 */
 123static int elantech_write_reg(struct psmouse *psmouse, unsigned char reg,
 124                                unsigned char val)
 125{
 126        struct elantech_data *etd = psmouse->private;
 127        int rc = 0;
 128
 129        if (reg < 0x10 || reg > 0x26)
 130                return -1;
 131
 132        if (reg > 0x11 && reg < 0x20)
 133                return -1;
 134
 135        switch (etd->hw_version) {
 136        case 1:
 137                if (psmouse_sliced_command(psmouse, ETP_REGISTER_WRITE) ||
 138                    psmouse_sliced_command(psmouse, reg) ||
 139                    psmouse_sliced_command(psmouse, val) ||
 140                    ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11)) {
 141                        rc = -1;
 142                }
 143                break;
 144
 145        case 2:
 146                if (elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
 147                    elantech_ps2_command(psmouse, NULL, ETP_REGISTER_WRITE) ||
 148                    elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
 149                    elantech_ps2_command(psmouse, NULL, reg) ||
 150                    elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
 151                    elantech_ps2_command(psmouse, NULL, val) ||
 152                    elantech_ps2_command(psmouse, NULL, PSMOUSE_CMD_SETSCALE11)) {
 153                        rc = -1;
 154                }
 155                break;
 156        }
 157
 158        if (rc)
 159                pr_err("failed to write register 0x%02x with value 0x%02x.\n",
 160                        reg, val);
 161
 162        return rc;
 163}
 164
 165/*
 166 * Dump a complete mouse movement packet to the syslog
 167 */
 168static void elantech_packet_dump(unsigned char *packet, int size)
 169{
 170        int     i;
 171
 172        printk(KERN_DEBUG pr_fmt("PS/2 packet ["));
 173        for (i = 0; i < size; i++)
 174                printk("%s0x%02x ", (i) ? ", " : " ", packet[i]);
 175        printk("]\n");
 176}
 177
 178/*
 179 * Interpret complete data packets and report absolute mode input events for
 180 * hardware version 1. (4 byte packets)
 181 */
 182static void elantech_report_absolute_v1(struct psmouse *psmouse)
 183{
 184        struct input_dev *dev = psmouse->dev;
 185        struct elantech_data *etd = psmouse->private;
 186        unsigned char *packet = psmouse->packet;
 187        int fingers;
 188
 189        if (etd->fw_version < 0x020000) {
 190                /*
 191                 * byte 0:  D   U  p1  p2   1  p3   R   L
 192                 * byte 1:  f   0  th  tw  x9  x8  y9  y8
 193                 */
 194                fingers = ((packet[1] & 0x80) >> 7) +
 195                                ((packet[1] & 0x30) >> 4);
 196        } else {
 197                /*
 198                 * byte 0: n1  n0  p2  p1   1  p3   R   L
 199                 * byte 1:  0   0   0   0  x9  x8  y9  y8
 200                 */
 201                fingers = (packet[0] & 0xc0) >> 6;
 202        }
 203
 204        if (etd->jumpy_cursor) {
 205                if (fingers != 1) {
 206                        etd->single_finger_reports = 0;
 207                } else if (etd->single_finger_reports < 2) {
 208                        /* Discard first 2 reports of one finger, bogus */
 209                        etd->single_finger_reports++;
 210                        elantech_debug("discarding packet\n");
 211                        return;
 212                }
 213        }
 214
 215        input_report_key(dev, BTN_TOUCH, fingers != 0);
 216
 217        /*
 218         * byte 2: x7  x6  x5  x4  x3  x2  x1  x0
 219         * byte 3: y7  y6  y5  y4  y3  y2  y1  y0
 220         */
 221        if (fingers) {
 222                input_report_abs(dev, ABS_X,
 223                        ((packet[1] & 0x0c) << 6) | packet[2]);
 224                input_report_abs(dev, ABS_Y,
 225                        ETP_YMAX_V1 - (((packet[1] & 0x03) << 8) | packet[3]));
 226        }
 227
 228        input_report_key(dev, BTN_TOOL_FINGER, fingers == 1);
 229        input_report_key(dev, BTN_TOOL_DOUBLETAP, fingers == 2);
 230        input_report_key(dev, BTN_TOOL_TRIPLETAP, fingers == 3);
 231        input_report_key(dev, BTN_LEFT, packet[0] & 0x01);
 232        input_report_key(dev, BTN_RIGHT, packet[0] & 0x02);
 233
 234        if (etd->fw_version < 0x020000 &&
 235            (etd->capabilities & ETP_CAP_HAS_ROCKER)) {
 236                /* rocker up */
 237                input_report_key(dev, BTN_FORWARD, packet[0] & 0x40);
 238                /* rocker down */
 239                input_report_key(dev, BTN_BACK, packet[0] & 0x80);
 240        }
 241
 242        input_sync(dev);
 243}
 244
 245/*
 246 * Interpret complete data packets and report absolute mode input events for
 247 * hardware version 2. (6 byte packets)
 248 */
 249static void elantech_report_absolute_v2(struct psmouse *psmouse)
 250{
 251        struct input_dev *dev = psmouse->dev;
 252        unsigned char *packet = psmouse->packet;
 253        int fingers, x1, y1, x2, y2;
 254
 255        /* byte 0: n1  n0   .   .   .   .   R   L */
 256        fingers = (packet[0] & 0xc0) >> 6;
 257        input_report_key(dev, BTN_TOUCH, fingers != 0);
 258
 259        switch (fingers) {
 260        case 3:
 261                /*
 262                 * Same as one finger, except report of more than 3 fingers:
 263                 * byte 3:  n4  .   w1  w0   .   .   .   .
 264                 */
 265                if (packet[3] & 0x80)
 266                        fingers = 4;
 267                /* pass through... */
 268        case 1:
 269                /*
 270                 * byte 1:  .   .   .   .   .  x10 x9  x8
 271                 * byte 2: x7  x6  x5  x4  x4  x2  x1  x0
 272                 */
 273                input_report_abs(dev, ABS_X,
 274                        ((packet[1] & 0x07) << 8) | packet[2]);
 275                /*
 276                 * byte 4:  .   .   .   .   .   .  y9  y8
 277                 * byte 5: y7  y6  y5  y4  y3  y2  y1  y0
 278                 */
 279                input_report_abs(dev, ABS_Y,
 280                        ETP_YMAX_V2 - (((packet[4] & 0x03) << 8) | packet[5]));
 281                break;
 282
 283        case 2:
 284                /*
 285                 * The coordinate of each finger is reported separately
 286                 * with a lower resolution for two finger touches:
 287                 * byte 0:  .   .  ay8 ax8  .   .   .   .
 288                 * byte 1: ax7 ax6 ax5 ax4 ax3 ax2 ax1 ax0
 289                 */
 290                x1 = ((packet[0] & 0x10) << 4) | packet[1];
 291                /* byte 2: ay7 ay6 ay5 ay4 ay3 ay2 ay1 ay0 */
 292                y1 = ETP_2FT_YMAX - (((packet[0] & 0x20) << 3) | packet[2]);
 293                /*
 294                 * byte 3:  .   .  by8 bx8  .   .   .   .
 295                 * byte 4: bx7 bx6 bx5 bx4 bx3 bx2 bx1 bx0
 296                 */
 297                x2 = ((packet[3] & 0x10) << 4) | packet[4];
 298                /* byte 5: by7 by8 by5 by4 by3 by2 by1 by0 */
 299                y2 = ETP_2FT_YMAX - (((packet[3] & 0x20) << 3) | packet[5]);
 300                /*
 301                 * For compatibility with the X Synaptics driver scale up
 302                 * one coordinate and report as ordinary mouse movent
 303                 */
 304                input_report_abs(dev, ABS_X, x1 << 2);
 305                input_report_abs(dev, ABS_Y, y1 << 2);
 306                /*
 307                 * For compatibility with the proprietary X Elantech driver
 308                 * report both coordinates as hat coordinates
 309                 */
 310                input_report_abs(dev, ABS_HAT0X, x1);
 311                input_report_abs(dev, ABS_HAT0Y, y1);
 312                input_report_abs(dev, ABS_HAT1X, x2);
 313                input_report_abs(dev, ABS_HAT1Y, y2);
 314                break;
 315        }
 316
 317        input_report_key(dev, BTN_TOOL_FINGER, fingers == 1);
 318        input_report_key(dev, BTN_TOOL_DOUBLETAP, fingers == 2);
 319        input_report_key(dev, BTN_TOOL_TRIPLETAP, fingers == 3);
 320        input_report_key(dev, BTN_TOOL_QUADTAP, fingers == 4);
 321        input_report_key(dev, BTN_LEFT, packet[0] & 0x01);
 322        input_report_key(dev, BTN_RIGHT, packet[0] & 0x02);
 323
 324        input_sync(dev);
 325}
 326
 327static int elantech_check_parity_v1(struct psmouse *psmouse)
 328{
 329        struct elantech_data *etd = psmouse->private;
 330        unsigned char *packet = psmouse->packet;
 331        unsigned char p1, p2, p3;
 332
 333        /* Parity bits are placed differently */
 334        if (etd->fw_version < 0x020000) {
 335                /* byte 0:  D   U  p1  p2   1  p3   R   L */
 336                p1 = (packet[0] & 0x20) >> 5;
 337                p2 = (packet[0] & 0x10) >> 4;
 338        } else {
 339                /* byte 0: n1  n0  p2  p1   1  p3   R   L */
 340                p1 = (packet[0] & 0x10) >> 4;
 341                p2 = (packet[0] & 0x20) >> 5;
 342        }
 343
 344        p3 = (packet[0] & 0x04) >> 2;
 345
 346        return etd->parity[packet[1]] == p1 &&
 347               etd->parity[packet[2]] == p2 &&
 348               etd->parity[packet[3]] == p3;
 349}
 350
 351/*
 352 * Process byte stream from mouse and handle complete packets
 353 */
 354static psmouse_ret_t elantech_process_byte(struct psmouse *psmouse)
 355{
 356        struct elantech_data *etd = psmouse->private;
 357
 358        if (psmouse->pktcnt < psmouse->pktsize)
 359                return PSMOUSE_GOOD_DATA;
 360
 361        if (etd->debug > 1)
 362                elantech_packet_dump(psmouse->packet, psmouse->pktsize);
 363
 364        switch (etd->hw_version) {
 365        case 1:
 366                if (etd->paritycheck && !elantech_check_parity_v1(psmouse))
 367                        return PSMOUSE_BAD_DATA;
 368
 369                elantech_report_absolute_v1(psmouse);
 370                break;
 371
 372        case 2:
 373                /* We don't know how to check parity in protocol v2 */
 374                elantech_report_absolute_v2(psmouse);
 375                break;
 376        }
 377
 378        return PSMOUSE_FULL_PACKET;
 379}
 380
 381/*
 382 * Put the touchpad into absolute mode
 383 */
 384static int elantech_set_absolute_mode(struct psmouse *psmouse)
 385{
 386        struct elantech_data *etd = psmouse->private;
 387        unsigned char val;
 388        int tries = ETP_READ_BACK_TRIES;
 389        int rc = 0;
 390
 391        switch (etd->hw_version) {
 392        case 1:
 393                etd->reg_10 = 0x16;
 394                etd->reg_11 = 0x8f;
 395                if (elantech_write_reg(psmouse, 0x10, etd->reg_10) ||
 396                    elantech_write_reg(psmouse, 0x11, etd->reg_11)) {
 397                        rc = -1;
 398                }
 399                break;
 400
 401        case 2:
 402                                        /* Windows driver values */
 403                etd->reg_10 = 0x54;
 404                etd->reg_11 = 0x88;     /* 0x8a */
 405                etd->reg_21 = 0x60;     /* 0x00 */
 406                if (elantech_write_reg(psmouse, 0x10, etd->reg_10) ||
 407                    elantech_write_reg(psmouse, 0x11, etd->reg_11) ||
 408                    elantech_write_reg(psmouse, 0x21, etd->reg_21)) {
 409                        rc = -1;
 410                        break;
 411                }
 412        }
 413
 414        if (rc == 0) {
 415                /*
 416                 * Read back reg 0x10. For hardware version 1 we must make
 417                 * sure the absolute mode bit is set. For hardware version 2
 418                 * the touchpad is probably initalising and not ready until
 419                 * we read back the value we just wrote.
 420                 */
 421                do {
 422                        rc = elantech_read_reg(psmouse, 0x10, &val);
 423                        if (rc == 0)
 424                                break;
 425                        tries--;
 426                        elantech_debug("retrying read (%d).\n", tries);
 427                        msleep(ETP_READ_BACK_DELAY);
 428                } while (tries > 0);
 429
 430                if (rc) {
 431                        pr_err("failed to read back register 0x10.\n");
 432                } else if (etd->hw_version == 1 &&
 433                           !(val & ETP_R10_ABSOLUTE_MODE)) {
 434                        pr_err("touchpad refuses to switch to absolute mode.\n");
 435                        rc = -1;
 436                }
 437        }
 438
 439        if (rc)
 440                pr_err("failed to initialise registers.\n");
 441
 442        return rc;
 443}
 444
 445/*
 446 * Set the appropriate event bits for the input subsystem
 447 */
 448static void elantech_set_input_params(struct psmouse *psmouse)
 449{
 450        struct input_dev *dev = psmouse->dev;
 451        struct elantech_data *etd = psmouse->private;
 452
 453        __set_bit(EV_KEY, dev->evbit);
 454        __set_bit(EV_ABS, dev->evbit);
 455        __clear_bit(EV_REL, dev->evbit);
 456
 457        __set_bit(BTN_LEFT, dev->keybit);
 458        __set_bit(BTN_RIGHT, dev->keybit);
 459
 460        __set_bit(BTN_TOUCH, dev->keybit);
 461        __set_bit(BTN_TOOL_FINGER, dev->keybit);
 462        __set_bit(BTN_TOOL_DOUBLETAP, dev->keybit);
 463        __set_bit(BTN_TOOL_TRIPLETAP, dev->keybit);
 464
 465        switch (etd->hw_version) {
 466        case 1:
 467                /* Rocker button */
 468                if (etd->fw_version < 0x020000 &&
 469                    (etd->capabilities & ETP_CAP_HAS_ROCKER)) {
 470                        __set_bit(BTN_FORWARD, dev->keybit);
 471                        __set_bit(BTN_BACK, dev->keybit);
 472                }
 473                input_set_abs_params(dev, ABS_X, ETP_XMIN_V1, ETP_XMAX_V1, 0, 0);
 474                input_set_abs_params(dev, ABS_Y, ETP_YMIN_V1, ETP_YMAX_V1, 0, 0);
 475                break;
 476
 477        case 2:
 478                __set_bit(BTN_TOOL_QUADTAP, dev->keybit);
 479                input_set_abs_params(dev, ABS_X, ETP_XMIN_V2, ETP_XMAX_V2, 0, 0);
 480                input_set_abs_params(dev, ABS_Y, ETP_YMIN_V2, ETP_YMAX_V2, 0, 0);
 481                input_set_abs_params(dev, ABS_HAT0X, ETP_2FT_XMIN, ETP_2FT_XMAX, 0, 0);
 482                input_set_abs_params(dev, ABS_HAT0Y, ETP_2FT_YMIN, ETP_2FT_YMAX, 0, 0);
 483                input_set_abs_params(dev, ABS_HAT1X, ETP_2FT_XMIN, ETP_2FT_XMAX, 0, 0);
 484                input_set_abs_params(dev, ABS_HAT1Y, ETP_2FT_YMIN, ETP_2FT_YMAX, 0, 0);
 485                break;
 486        }
 487}
 488
 489struct elantech_attr_data {
 490        size_t          field_offset;
 491        unsigned char   reg;
 492};
 493
 494/*
 495 * Display a register value by reading a sysfs entry
 496 */
 497static ssize_t elantech_show_int_attr(struct psmouse *psmouse, void *data,
 498                                        char *buf)
 499{
 500        struct elantech_data *etd = psmouse->private;
 501        struct elantech_attr_data *attr = data;
 502        unsigned char *reg = (unsigned char *) etd + attr->field_offset;
 503        int rc = 0;
 504
 505        if (attr->reg)
 506                rc = elantech_read_reg(psmouse, attr->reg, reg);
 507
 508        return sprintf(buf, "0x%02x\n", (attr->reg && rc) ? -1 : *reg);
 509}
 510
 511/*
 512 * Write a register value by writing a sysfs entry
 513 */
 514static ssize_t elantech_set_int_attr(struct psmouse *psmouse,
 515                                     void *data, const char *buf, size_t count)
 516{
 517        struct elantech_data *etd = psmouse->private;
 518        struct elantech_attr_data *attr = data;
 519        unsigned char *reg = (unsigned char *) etd + attr->field_offset;
 520        unsigned long value;
 521        int err;
 522
 523        err = strict_strtoul(buf, 16, &value);
 524        if (err)
 525                return err;
 526
 527        if (value > 0xff)
 528                return -EINVAL;
 529
 530        /* Do we need to preserve some bits for version 2 hardware too? */
 531        if (etd->hw_version == 1) {
 532                if (attr->reg == 0x10)
 533                        /* Force absolute mode always on */
 534                        value |= ETP_R10_ABSOLUTE_MODE;
 535                else if (attr->reg == 0x11)
 536                        /* Force 4 byte mode always on */
 537                        value |= ETP_R11_4_BYTE_MODE;
 538        }
 539
 540        if (!attr->reg || elantech_write_reg(psmouse, attr->reg, value) == 0)
 541                *reg = value;
 542
 543        return count;
 544}
 545
 546#define ELANTECH_INT_ATTR(_name, _register)                             \
 547        static struct elantech_attr_data elantech_attr_##_name = {      \
 548                .field_offset = offsetof(struct elantech_data, _name),  \
 549                .reg = _register,                                       \
 550        };                                                              \
 551        PSMOUSE_DEFINE_ATTR(_name, S_IWUSR | S_IRUGO,                   \
 552                            &elantech_attr_##_name,                     \
 553                            elantech_show_int_attr,                     \
 554                            elantech_set_int_attr)
 555
 556ELANTECH_INT_ATTR(reg_10, 0x10);
 557ELANTECH_INT_ATTR(reg_11, 0x11);
 558ELANTECH_INT_ATTR(reg_20, 0x20);
 559ELANTECH_INT_ATTR(reg_21, 0x21);
 560ELANTECH_INT_ATTR(reg_22, 0x22);
 561ELANTECH_INT_ATTR(reg_23, 0x23);
 562ELANTECH_INT_ATTR(reg_24, 0x24);
 563ELANTECH_INT_ATTR(reg_25, 0x25);
 564ELANTECH_INT_ATTR(reg_26, 0x26);
 565ELANTECH_INT_ATTR(debug, 0);
 566ELANTECH_INT_ATTR(paritycheck, 0);
 567
 568static struct attribute *elantech_attrs[] = {
 569        &psmouse_attr_reg_10.dattr.attr,
 570        &psmouse_attr_reg_11.dattr.attr,
 571        &psmouse_attr_reg_20.dattr.attr,
 572        &psmouse_attr_reg_21.dattr.attr,
 573        &psmouse_attr_reg_22.dattr.attr,
 574        &psmouse_attr_reg_23.dattr.attr,
 575        &psmouse_attr_reg_24.dattr.attr,
 576        &psmouse_attr_reg_25.dattr.attr,
 577        &psmouse_attr_reg_26.dattr.attr,
 578        &psmouse_attr_debug.dattr.attr,
 579        &psmouse_attr_paritycheck.dattr.attr,
 580        NULL
 581};
 582
 583static struct attribute_group elantech_attr_group = {
 584        .attrs = elantech_attrs,
 585};
 586
 587static bool elantech_is_signature_valid(const unsigned char *param)
 588{
 589        static const unsigned char rates[] = { 200, 100, 80, 60, 40, 20, 10 };
 590        int i;
 591
 592        if (param[0] == 0)
 593                return false;
 594
 595        if (param[1] == 0)
 596                return true;
 597
 598        for (i = 0; i < ARRAY_SIZE(rates); i++)
 599                if (param[2] == rates[i])
 600                        return false;
 601
 602        return true;
 603}
 604
 605/*
 606 * Use magic knock to detect Elantech touchpad
 607 */
 608int elantech_detect(struct psmouse *psmouse, bool set_properties)
 609{
 610        struct ps2dev *ps2dev = &psmouse->ps2dev;
 611        unsigned char param[3];
 612
 613        ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
 614
 615        if (ps2_command(ps2dev,  NULL, PSMOUSE_CMD_DISABLE) ||
 616            ps2_command(ps2dev,  NULL, PSMOUSE_CMD_SETSCALE11) ||
 617            ps2_command(ps2dev,  NULL, PSMOUSE_CMD_SETSCALE11) ||
 618            ps2_command(ps2dev,  NULL, PSMOUSE_CMD_SETSCALE11) ||
 619            ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO)) {
 620                pr_debug("sending Elantech magic knock failed.\n");
 621                return -1;
 622        }
 623
 624        /*
 625         * Report this in case there are Elantech models that use a different
 626         * set of magic numbers
 627         */
 628        if (param[0] != 0x3c || param[1] != 0x03 || param[2] != 0xc8) {
 629                pr_debug("unexpected magic knock result 0x%02x, 0x%02x, 0x%02x.\n",
 630                         param[0], param[1], param[2]);
 631                return -1;
 632        }
 633
 634        /*
 635         * Query touchpad's firmware version and see if it reports known
 636         * value to avoid mis-detection. Logitech mice are known to respond
 637         * to Elantech magic knock and there might be more.
 638         */
 639        if (synaptics_send_cmd(psmouse, ETP_FW_VERSION_QUERY, param)) {
 640                pr_debug("failed to query firmware version.\n");
 641                return -1;
 642        }
 643
 644        pr_debug("Elantech version query result 0x%02x, 0x%02x, 0x%02x.\n",
 645                 param[0], param[1], param[2]);
 646
 647        if (!elantech_is_signature_valid(param)) {
 648                if (!force_elantech) {
 649                        pr_debug("Probably not a real Elantech touchpad. Aborting.\n");
 650                        return -1;
 651                }
 652
 653                pr_debug("Probably not a real Elantech touchpad. Enabling anyway due to force_elantech.\n");
 654        }
 655
 656        if (set_properties) {
 657                psmouse->vendor = "Elantech";
 658                psmouse->name = "Touchpad";
 659        }
 660
 661        return 0;
 662}
 663
 664/*
 665 * Clean up sysfs entries when disconnecting
 666 */
 667static void elantech_disconnect(struct psmouse *psmouse)
 668{
 669        sysfs_remove_group(&psmouse->ps2dev.serio->dev.kobj,
 670                           &elantech_attr_group);
 671        kfree(psmouse->private);
 672        psmouse->private = NULL;
 673}
 674
 675/*
 676 * Put the touchpad back into absolute mode when reconnecting
 677 */
 678static int elantech_reconnect(struct psmouse *psmouse)
 679{
 680        if (elantech_detect(psmouse, 0))
 681                return -1;
 682
 683        if (elantech_set_absolute_mode(psmouse)) {
 684                pr_err("failed to put touchpad back into absolute mode.\n");
 685                return -1;
 686        }
 687
 688        return 0;
 689}
 690
 691/*
 692 * Initialize the touchpad and create sysfs entries
 693 */
 694int elantech_init(struct psmouse *psmouse)
 695{
 696        struct elantech_data *etd;
 697        int i, error;
 698        unsigned char param[3];
 699
 700        psmouse->private = etd = kzalloc(sizeof(struct elantech_data), GFP_KERNEL);
 701        if (!etd)
 702                return -ENOMEM;
 703
 704        etd->parity[0] = 1;
 705        for (i = 1; i < 256; i++)
 706                etd->parity[i] = etd->parity[i & (i - 1)] ^ 1;
 707
 708        /*
 709         * Do the version query again so we can store the result
 710         */
 711        if (synaptics_send_cmd(psmouse, ETP_FW_VERSION_QUERY, param)) {
 712                pr_err("failed to query firmware version.\n");
 713                goto init_fail;
 714        }
 715
 716        etd->fw_version = (param[0] << 16) | (param[1] << 8) | param[2];
 717
 718        /*
 719         * Assume every version greater than this is new EeePC style
 720         * hardware with 6 byte packets
 721         */
 722        if (etd->fw_version >= 0x020030) {
 723                etd->hw_version = 2;
 724                /* For now show extra debug information */
 725                etd->debug = 1;
 726                /* Don't know how to do parity checking for version 2 */
 727                etd->paritycheck = 0;
 728        } else {
 729                etd->hw_version = 1;
 730                etd->paritycheck = 1;
 731        }
 732
 733        pr_info("assuming hardware version %d, firmware version %d.%d.%d\n",
 734                etd->hw_version, param[0], param[1], param[2]);
 735
 736        if (synaptics_send_cmd(psmouse, ETP_CAPABILITIES_QUERY, param)) {
 737                pr_err("failed to query capabilities.\n");
 738                goto init_fail;
 739        }
 740        pr_info("Synaptics capabilities query result 0x%02x, 0x%02x, 0x%02x.\n",
 741                param[0], param[1], param[2]);
 742        etd->capabilities = param[0];
 743
 744        /*
 745         * This firmware suffers from misreporting coordinates when
 746         * a touch action starts causing the mouse cursor or scrolled page
 747         * to jump. Enable a workaround.
 748         */
 749        if (etd->fw_version == 0x020022 || etd->fw_version == 0x020600) {
 750                pr_info("firmware version 2.0.34/2.6.0 detected, enabling jumpy cursor workaround\n");
 751                etd->jumpy_cursor = true;
 752        }
 753
 754        if (elantech_set_absolute_mode(psmouse)) {
 755                pr_err("failed to put touchpad into absolute mode.\n");
 756                goto init_fail;
 757        }
 758
 759        elantech_set_input_params(psmouse);
 760
 761        error = sysfs_create_group(&psmouse->ps2dev.serio->dev.kobj,
 762                                   &elantech_attr_group);
 763        if (error) {
 764                pr_err("failed to create sysfs attributes, error: %d.\n", error);
 765                goto init_fail;
 766        }
 767
 768        psmouse->protocol_handler = elantech_process_byte;
 769        psmouse->disconnect = elantech_disconnect;
 770        psmouse->reconnect = elantech_reconnect;
 771        psmouse->pktsize = etd->hw_version == 2 ? 6 : 4;
 772
 773        return 0;
 774
 775 init_fail:
 776        kfree(etd);
 777        return -1;
 778}
 779