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