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