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