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