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