blob: cd8e2e5ced862774a2850267c9b9959bd07ae5be [file] [log] [blame]
Arjan Opmeer2a0bd752008-10-16 22:10:19 -04001/*
Arjan Opmeer3f8c0df2009-04-18 19:05:40 -07002 * Elantech Touchpad driver (v6)
Arjan Opmeer2a0bd752008-10-16 22:10:19 -04003 *
Arjan Opmeer3f8c0df2009-04-18 19:05:40 -07004 * Copyright (C) 2007-2009 Arjan Opmeer <arjan@opmeer.net>
Arjan Opmeer2a0bd752008-10-16 22:10:19 -04005 *
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 Torokhovd4ae84a82010-05-13 00:41:15 -070013#define pr_fmt(fmt) KBUILD_BASENAME ": " fmt
14
Arjan Opmeer2a0bd752008-10-16 22:10:19 -040015#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Arjan Opmeer2a0bd752008-10-16 22:10:19 -040017#include <linux/module.h>
18#include <linux/input.h>
Éric Piel89eec4d2011-05-16 22:45:54 -070019#include <linux/input/mt.h>
Arjan Opmeer2a0bd752008-10-16 22:10:19 -040020#include <linux/serio.h>
21#include <linux/libps2.h>
22#include "psmouse.h"
23#include "elantech.h"
24
Dmitry Torokhovd4ae84a82010-05-13 00:41:15 -070025#define elantech_debug(fmt, ...) \
26 do { \
27 if (etd->debug) \
28 printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__); \
Arjan Opmeer2a0bd752008-10-16 22:10:19 -040029 } while (0)
30
Florian Ragwitzf81bc782010-04-27 00:47:04 -070031static bool force_elantech;
32module_param_named(force_elantech, force_elantech, bool, 0644);
33MODULE_PARM_DESC(force_elantech, "Force the Elantech PS/2 protocol extension to be used, 1 = enabled, 0 = disabled (default).");
34
Arjan Opmeer2a0bd752008-10-16 22:10:19 -040035/*
36 * Send a Synaptics style sliced query command
37 */
38static 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 Torokhovd4ae84a82010-05-13 00:41:15 -070043 pr_err("synaptics_send_cmd query 0x%02x failed.\n", c);
Arjan Opmeer2a0bd752008-10-16 22:10:19 -040044 return -1;
45 }
46
47 return 0;
48}
49
50/*
51 * A retrying version of ps2_command
52 */
53static 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 Torokhovd4ae84a82010-05-13 00:41:15 -070066 elantech_debug("retrying ps2 command 0x%02x (%d).\n",
67 command, tries);
Arjan Opmeer2a0bd752008-10-16 22:10:19 -040068 msleep(ETP_PS2_COMMAND_DELAY);
69 } while (tries > 0);
70
71 if (rc)
Dmitry Torokhovd4ae84a82010-05-13 00:41:15 -070072 pr_err("ps2 command 0x%02x failed.\n", command);
Arjan Opmeer2a0bd752008-10-16 22:10:19 -040073
74 return rc;
75}
76
77/*
78 * Send an Elantech style special command to read a value from a register
79 */
80static int elantech_read_reg(struct psmouse *psmouse, unsigned char reg,
81 unsigned char *val)
82{
83 struct elantech_data *etd = psmouse->private;
84 unsigned char param[3];
85 int rc = 0;
86
87 if (reg < 0x10 || reg > 0x26)
88 return -1;
89
90 if (reg > 0x11 && reg < 0x20)
91 return -1;
92
93 switch (etd->hw_version) {
94 case 1:
95 if (psmouse_sliced_command(psmouse, ETP_REGISTER_READ) ||
96 psmouse_sliced_command(psmouse, reg) ||
97 ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_GETINFO)) {
98 rc = -1;
99 }
100 break;
101
102 case 2:
103 if (elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
104 elantech_ps2_command(psmouse, NULL, ETP_REGISTER_READ) ||
105 elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
106 elantech_ps2_command(psmouse, NULL, reg) ||
107 elantech_ps2_command(psmouse, param, PSMOUSE_CMD_GETINFO)) {
108 rc = -1;
109 }
110 break;
111 }
112
113 if (rc)
Dmitry Torokhovd4ae84a82010-05-13 00:41:15 -0700114 pr_err("failed to read register 0x%02x.\n", reg);
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400115 else
116 *val = param[0];
117
118 return rc;
119}
120
121/*
122 * Send an Elantech style special command to write a register with a value
123 */
124static int elantech_write_reg(struct psmouse *psmouse, unsigned char reg,
125 unsigned char val)
126{
127 struct elantech_data *etd = psmouse->private;
128 int rc = 0;
129
130 if (reg < 0x10 || reg > 0x26)
131 return -1;
132
133 if (reg > 0x11 && reg < 0x20)
134 return -1;
135
136 switch (etd->hw_version) {
137 case 1:
138 if (psmouse_sliced_command(psmouse, ETP_REGISTER_WRITE) ||
139 psmouse_sliced_command(psmouse, reg) ||
140 psmouse_sliced_command(psmouse, val) ||
141 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11)) {
142 rc = -1;
143 }
144 break;
145
146 case 2:
147 if (elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
148 elantech_ps2_command(psmouse, NULL, ETP_REGISTER_WRITE) ||
149 elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
150 elantech_ps2_command(psmouse, NULL, reg) ||
151 elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
152 elantech_ps2_command(psmouse, NULL, val) ||
153 elantech_ps2_command(psmouse, NULL, PSMOUSE_CMD_SETSCALE11)) {
154 rc = -1;
155 }
156 break;
157 }
158
159 if (rc)
Dmitry Torokhovd4ae84a82010-05-13 00:41:15 -0700160 pr_err("failed to write register 0x%02x with value 0x%02x.\n",
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400161 reg, val);
162
163 return rc;
164}
165
166/*
167 * Dump a complete mouse movement packet to the syslog
168 */
169static void elantech_packet_dump(unsigned char *packet, int size)
170{
171 int i;
172
Dmitry Torokhovd4ae84a82010-05-13 00:41:15 -0700173 printk(KERN_DEBUG pr_fmt("PS/2 packet ["));
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400174 for (i = 0; i < size; i++)
175 printk("%s0x%02x ", (i) ? ", " : " ", packet[i]);
176 printk("]\n");
177}
178
179/*
180 * Interpret complete data packets and report absolute mode input events for
181 * hardware version 1. (4 byte packets)
182 */
183static void elantech_report_absolute_v1(struct psmouse *psmouse)
184{
185 struct input_dev *dev = psmouse->dev;
186 struct elantech_data *etd = psmouse->private;
187 unsigned char *packet = psmouse->packet;
188 int fingers;
189
Dmitry Torokhov504e8be2010-05-13 00:41:15 -0700190 if (etd->fw_version < 0x020000) {
Florian Ragwitze938fbf2010-05-03 23:29:37 -0700191 /*
192 * byte 0: D U p1 p2 1 p3 R L
193 * byte 1: f 0 th tw x9 x8 y9 y8
194 */
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400195 fingers = ((packet[1] & 0x80) >> 7) +
196 ((packet[1] & 0x30) >> 4);
197 } else {
Florian Ragwitze938fbf2010-05-03 23:29:37 -0700198 /*
199 * byte 0: n1 n0 p2 p1 1 p3 R L
200 * byte 1: 0 0 0 0 x9 x8 y9 y8
201 */
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400202 fingers = (packet[0] & 0xc0) >> 6;
203 }
204
Arjan Opmeer3f8c0df2009-04-18 19:05:40 -0700205 if (etd->jumpy_cursor) {
Éric Piel7f29f172010-08-05 23:51:49 -0700206 if (fingers != 1) {
207 etd->single_finger_reports = 0;
208 } else if (etd->single_finger_reports < 2) {
209 /* Discard first 2 reports of one finger, bogus */
210 etd->single_finger_reports++;
Dmitry Torokhovd4ae84a82010-05-13 00:41:15 -0700211 elantech_debug("discarding packet\n");
Éric Piel7f29f172010-08-05 23:51:49 -0700212 return;
Arjan Opmeer3f8c0df2009-04-18 19:05:40 -0700213 }
214 }
215
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400216 input_report_key(dev, BTN_TOUCH, fingers != 0);
217
Florian Ragwitze938fbf2010-05-03 23:29:37 -0700218 /*
219 * byte 2: x7 x6 x5 x4 x3 x2 x1 x0
220 * byte 3: y7 y6 y5 y4 y3 y2 y1 y0
221 */
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400222 if (fingers) {
223 input_report_abs(dev, ABS_X,
224 ((packet[1] & 0x0c) << 6) | packet[2]);
Florian Ragwitze938fbf2010-05-03 23:29:37 -0700225 input_report_abs(dev, ABS_Y,
226 ETP_YMAX_V1 - (((packet[1] & 0x03) << 8) | packet[3]));
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400227 }
228
229 input_report_key(dev, BTN_TOOL_FINGER, fingers == 1);
230 input_report_key(dev, BTN_TOOL_DOUBLETAP, fingers == 2);
231 input_report_key(dev, BTN_TOOL_TRIPLETAP, fingers == 3);
232 input_report_key(dev, BTN_LEFT, packet[0] & 0x01);
233 input_report_key(dev, BTN_RIGHT, packet[0] & 0x02);
234
Dmitry Torokhov504e8be2010-05-13 00:41:15 -0700235 if (etd->fw_version < 0x020000 &&
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400236 (etd->capabilities & ETP_CAP_HAS_ROCKER)) {
237 /* rocker up */
238 input_report_key(dev, BTN_FORWARD, packet[0] & 0x40);
239 /* rocker down */
240 input_report_key(dev, BTN_BACK, packet[0] & 0x80);
241 }
242
243 input_sync(dev);
244}
245
Éric Piel89eec4d2011-05-16 22:45:54 -0700246static void elantech_set_slot(struct input_dev *dev, int slot, bool active,
247 unsigned int x, unsigned int y)
248{
249 input_mt_slot(dev, slot);
250 input_mt_report_slot_state(dev, MT_TOOL_FINGER, active);
251 if (active) {
252 input_report_abs(dev, ABS_MT_POSITION_X, x);
253 input_report_abs(dev, ABS_MT_POSITION_Y, y);
254 }
255}
256
257/* x1 < x2 and y1 < y2 when two fingers, x = y = 0 when not pressed */
258static void elantech_report_semi_mt_data(struct input_dev *dev,
259 unsigned int num_fingers,
260 unsigned int x1, unsigned int y1,
261 unsigned int x2, unsigned int y2)
262{
263 elantech_set_slot(dev, 0, num_fingers != 0, x1, y1);
264 elantech_set_slot(dev, 1, num_fingers == 2, x2, y2);
265}
266
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400267/*
268 * Interpret complete data packets and report absolute mode input events for
269 * hardware version 2. (6 byte packets)
270 */
271static void elantech_report_absolute_v2(struct psmouse *psmouse)
272{
Éric Pielf941c702011-05-16 22:45:54 -0700273 struct elantech_data *etd = psmouse->private;
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400274 struct input_dev *dev = psmouse->dev;
275 unsigned char *packet = psmouse->packet;
JJ Ding461a7912011-09-09 10:22:58 -0700276 unsigned int fingers, x1 = 0, y1 = 0, x2 = 0, y2 = 0;
277 unsigned int width = 0, pres = 0;
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400278
279 /* byte 0: n1 n0 . . . . R L */
280 fingers = (packet[0] & 0xc0) >> 6;
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400281
282 switch (fingers) {
Éric Piel22462d9f2010-08-05 23:51:49 -0700283 case 3:
284 /*
285 * Same as one finger, except report of more than 3 fingers:
286 * byte 3: n4 . w1 w0 . . . .
287 */
288 if (packet[3] & 0x80)
289 fingers = 4;
290 /* pass through... */
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400291 case 1:
Florian Ragwitze938fbf2010-05-03 23:29:37 -0700292 /*
JJ Ding11559612011-09-09 10:22:19 -0700293 * byte 1: . . . . x11 x10 x9 x8
Florian Ragwitze938fbf2010-05-03 23:29:37 -0700294 * byte 2: x7 x6 x5 x4 x4 x2 x1 x0
295 */
JJ Ding11559612011-09-09 10:22:19 -0700296 x1 = ((packet[1] & 0x0f) << 8) | packet[2];
Florian Ragwitze938fbf2010-05-03 23:29:37 -0700297 /*
JJ Ding11559612011-09-09 10:22:19 -0700298 * byte 4: . . . . y11 y10 y9 y8
Florian Ragwitze938fbf2010-05-03 23:29:37 -0700299 * byte 5: y7 y6 y5 y4 y3 y2 y1 y0
300 */
JJ Ding11559612011-09-09 10:22:19 -0700301 y1 = ETP_YMAX_V2 - (((packet[4] & 0x0f) << 8) | packet[5]);
Éric Piel89eec4d2011-05-16 22:45:54 -0700302
Éric Pielf941c702011-05-16 22:45:54 -0700303 pres = (packet[1] & 0xf0) | ((packet[4] & 0xf0) >> 4);
304 width = ((packet[0] & 0x30) >> 2) | ((packet[3] & 0x30) >> 4);
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400305 break;
306
307 case 2:
Florian Ragwitze938fbf2010-05-03 23:29:37 -0700308 /*
309 * The coordinate of each finger is reported separately
310 * with a lower resolution for two finger touches:
311 * byte 0: . . ay8 ax8 . . . .
312 * byte 1: ax7 ax6 ax5 ax4 ax3 ax2 ax1 ax0
313 */
JJ Ding461a7912011-09-09 10:22:58 -0700314 x1 = (((packet[0] & 0x10) << 4) | packet[1]) << 2;
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400315 /* byte 2: ay7 ay6 ay5 ay4 ay3 ay2 ay1 ay0 */
JJ Ding461a7912011-09-09 10:22:58 -0700316 y1 = ETP_YMAX_V2 -
317 ((((packet[0] & 0x20) << 3) | packet[2]) << 2);
Florian Ragwitze938fbf2010-05-03 23:29:37 -0700318 /*
319 * byte 3: . . by8 bx8 . . . .
320 * byte 4: bx7 bx6 bx5 bx4 bx3 bx2 bx1 bx0
321 */
JJ Ding461a7912011-09-09 10:22:58 -0700322 x2 = (((packet[3] & 0x10) << 4) | packet[4]) << 2;
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400323 /* byte 5: by7 by8 by5 by4 by3 by2 by1 by0 */
JJ Ding461a7912011-09-09 10:22:58 -0700324 y2 = ETP_YMAX_V2 -
325 ((((packet[3] & 0x20) << 3) | packet[5]) << 2);
Éric Pielf941c702011-05-16 22:45:54 -0700326
327 /* Unknown so just report sensible values */
328 pres = 127;
329 width = 7;
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400330 break;
331 }
332
JJ Ding461a7912011-09-09 10:22:58 -0700333 input_report_key(dev, BTN_TOUCH, fingers != 0);
334 if (fingers != 0) {
335 input_report_abs(dev, ABS_X, x1);
336 input_report_abs(dev, ABS_Y, y1);
337 }
Éric Piel89eec4d2011-05-16 22:45:54 -0700338 elantech_report_semi_mt_data(dev, fingers, x1, y1, x2, y2);
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400339 input_report_key(dev, BTN_TOOL_FINGER, fingers == 1);
340 input_report_key(dev, BTN_TOOL_DOUBLETAP, fingers == 2);
341 input_report_key(dev, BTN_TOOL_TRIPLETAP, fingers == 3);
Éric Piel22462d9f2010-08-05 23:51:49 -0700342 input_report_key(dev, BTN_TOOL_QUADTAP, fingers == 4);
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400343 input_report_key(dev, BTN_LEFT, packet[0] & 0x01);
344 input_report_key(dev, BTN_RIGHT, packet[0] & 0x02);
Éric Pielf941c702011-05-16 22:45:54 -0700345 if (etd->reports_pressure) {
346 input_report_abs(dev, ABS_PRESSURE, pres);
347 input_report_abs(dev, ABS_TOOL_WIDTH, width);
348 }
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400349
350 input_sync(dev);
351}
352
353static int elantech_check_parity_v1(struct psmouse *psmouse)
354{
355 struct elantech_data *etd = psmouse->private;
356 unsigned char *packet = psmouse->packet;
357 unsigned char p1, p2, p3;
358
359 /* Parity bits are placed differently */
Dmitry Torokhov504e8be2010-05-13 00:41:15 -0700360 if (etd->fw_version < 0x020000) {
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400361 /* byte 0: D U p1 p2 1 p3 R L */
362 p1 = (packet[0] & 0x20) >> 5;
363 p2 = (packet[0] & 0x10) >> 4;
364 } else {
365 /* byte 0: n1 n0 p2 p1 1 p3 R L */
366 p1 = (packet[0] & 0x10) >> 4;
367 p2 = (packet[0] & 0x20) >> 5;
368 }
369
370 p3 = (packet[0] & 0x04) >> 2;
371
372 return etd->parity[packet[1]] == p1 &&
373 etd->parity[packet[2]] == p2 &&
374 etd->parity[packet[3]] == p3;
375}
376
377/*
378 * Process byte stream from mouse and handle complete packets
379 */
380static psmouse_ret_t elantech_process_byte(struct psmouse *psmouse)
381{
382 struct elantech_data *etd = psmouse->private;
383
384 if (psmouse->pktcnt < psmouse->pktsize)
385 return PSMOUSE_GOOD_DATA;
386
387 if (etd->debug > 1)
388 elantech_packet_dump(psmouse->packet, psmouse->pktsize);
389
390 switch (etd->hw_version) {
391 case 1:
392 if (etd->paritycheck && !elantech_check_parity_v1(psmouse))
393 return PSMOUSE_BAD_DATA;
394
395 elantech_report_absolute_v1(psmouse);
396 break;
397
398 case 2:
399 /* We don't know how to check parity in protocol v2 */
400 elantech_report_absolute_v2(psmouse);
401 break;
402 }
403
404 return PSMOUSE_FULL_PACKET;
405}
406
407/*
408 * Put the touchpad into absolute mode
409 */
410static int elantech_set_absolute_mode(struct psmouse *psmouse)
411{
412 struct elantech_data *etd = psmouse->private;
413 unsigned char val;
414 int tries = ETP_READ_BACK_TRIES;
415 int rc = 0;
416
417 switch (etd->hw_version) {
418 case 1:
419 etd->reg_10 = 0x16;
420 etd->reg_11 = 0x8f;
421 if (elantech_write_reg(psmouse, 0x10, etd->reg_10) ||
422 elantech_write_reg(psmouse, 0x11, etd->reg_11)) {
423 rc = -1;
424 }
425 break;
426
427 case 2:
428 /* Windows driver values */
429 etd->reg_10 = 0x54;
430 etd->reg_11 = 0x88; /* 0x8a */
431 etd->reg_21 = 0x60; /* 0x00 */
432 if (elantech_write_reg(psmouse, 0x10, etd->reg_10) ||
433 elantech_write_reg(psmouse, 0x11, etd->reg_11) ||
434 elantech_write_reg(psmouse, 0x21, etd->reg_21)) {
435 rc = -1;
436 break;
437 }
Arjan Opmeerb2546df2009-04-18 19:10:17 -0700438 }
439
440 if (rc == 0) {
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400441 /*
Arjan Opmeerb2546df2009-04-18 19:10:17 -0700442 * Read back reg 0x10. For hardware version 1 we must make
443 * sure the absolute mode bit is set. For hardware version 2
444 * the touchpad is probably initalising and not ready until
445 * we read back the value we just wrote.
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400446 */
447 do {
448 rc = elantech_read_reg(psmouse, 0x10, &val);
449 if (rc == 0)
450 break;
451 tries--;
Dmitry Torokhovd4ae84a82010-05-13 00:41:15 -0700452 elantech_debug("retrying read (%d).\n", tries);
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400453 msleep(ETP_READ_BACK_DELAY);
454 } while (tries > 0);
Arjan Opmeerb2546df2009-04-18 19:10:17 -0700455
456 if (rc) {
Dmitry Torokhovd4ae84a82010-05-13 00:41:15 -0700457 pr_err("failed to read back register 0x10.\n");
Arjan Opmeerb2546df2009-04-18 19:10:17 -0700458 } else if (etd->hw_version == 1 &&
459 !(val & ETP_R10_ABSOLUTE_MODE)) {
Dmitry Torokhovd4ae84a82010-05-13 00:41:15 -0700460 pr_err("touchpad refuses to switch to absolute mode.\n");
Arjan Opmeerb2546df2009-04-18 19:10:17 -0700461 rc = -1;
462 }
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400463 }
464
465 if (rc)
Dmitry Torokhovd4ae84a82010-05-13 00:41:15 -0700466 pr_err("failed to initialise registers.\n");
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400467
468 return rc;
469}
470
471/*
472 * Set the appropriate event bits for the input subsystem
473 */
474static void elantech_set_input_params(struct psmouse *psmouse)
475{
476 struct input_dev *dev = psmouse->dev;
477 struct elantech_data *etd = psmouse->private;
478
479 __set_bit(EV_KEY, dev->evbit);
480 __set_bit(EV_ABS, dev->evbit);
Dmitry Torokhovc7a1f3c2009-11-16 22:12:21 -0800481 __clear_bit(EV_REL, dev->evbit);
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400482
483 __set_bit(BTN_LEFT, dev->keybit);
484 __set_bit(BTN_RIGHT, dev->keybit);
485
486 __set_bit(BTN_TOUCH, dev->keybit);
487 __set_bit(BTN_TOOL_FINGER, dev->keybit);
488 __set_bit(BTN_TOOL_DOUBLETAP, dev->keybit);
489 __set_bit(BTN_TOOL_TRIPLETAP, dev->keybit);
490
491 switch (etd->hw_version) {
492 case 1:
493 /* Rocker button */
Dmitry Torokhov504e8be2010-05-13 00:41:15 -0700494 if (etd->fw_version < 0x020000 &&
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400495 (etd->capabilities & ETP_CAP_HAS_ROCKER)) {
496 __set_bit(BTN_FORWARD, dev->keybit);
497 __set_bit(BTN_BACK, dev->keybit);
498 }
499 input_set_abs_params(dev, ABS_X, ETP_XMIN_V1, ETP_XMAX_V1, 0, 0);
500 input_set_abs_params(dev, ABS_Y, ETP_YMIN_V1, ETP_YMAX_V1, 0, 0);
501 break;
502
503 case 2:
Éric Piel22462d9f2010-08-05 23:51:49 -0700504 __set_bit(BTN_TOOL_QUADTAP, dev->keybit);
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400505 input_set_abs_params(dev, ABS_X, ETP_XMIN_V2, ETP_XMAX_V2, 0, 0);
506 input_set_abs_params(dev, ABS_Y, ETP_YMIN_V2, ETP_YMAX_V2, 0, 0);
Éric Pielf941c702011-05-16 22:45:54 -0700507 if (etd->reports_pressure) {
508 input_set_abs_params(dev, ABS_PRESSURE, ETP_PMIN_V2,
509 ETP_PMAX_V2, 0, 0);
510 input_set_abs_params(dev, ABS_TOOL_WIDTH, ETP_WMIN_V2,
511 ETP_WMAX_V2, 0, 0);
512 }
Éric Piel89eec4d2011-05-16 22:45:54 -0700513 __set_bit(INPUT_PROP_SEMI_MT, dev->propbit);
514 input_mt_init_slots(dev, 2);
515 input_set_abs_params(dev, ABS_MT_POSITION_X, ETP_XMIN_V2, ETP_XMAX_V2, 0, 0);
516 input_set_abs_params(dev, ABS_MT_POSITION_Y, ETP_YMIN_V2, ETP_YMAX_V2, 0, 0);
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400517 break;
518 }
519}
520
521struct elantech_attr_data {
522 size_t field_offset;
523 unsigned char reg;
524};
525
526/*
527 * Display a register value by reading a sysfs entry
528 */
529static ssize_t elantech_show_int_attr(struct psmouse *psmouse, void *data,
530 char *buf)
531{
532 struct elantech_data *etd = psmouse->private;
533 struct elantech_attr_data *attr = data;
534 unsigned char *reg = (unsigned char *) etd + attr->field_offset;
535 int rc = 0;
536
537 if (attr->reg)
538 rc = elantech_read_reg(psmouse, attr->reg, reg);
539
540 return sprintf(buf, "0x%02x\n", (attr->reg && rc) ? -1 : *reg);
541}
542
543/*
544 * Write a register value by writing a sysfs entry
545 */
546static ssize_t elantech_set_int_attr(struct psmouse *psmouse,
547 void *data, const char *buf, size_t count)
548{
549 struct elantech_data *etd = psmouse->private;
550 struct elantech_attr_data *attr = data;
551 unsigned char *reg = (unsigned char *) etd + attr->field_offset;
552 unsigned long value;
553 int err;
554
555 err = strict_strtoul(buf, 16, &value);
556 if (err)
557 return err;
558
559 if (value > 0xff)
560 return -EINVAL;
561
562 /* Do we need to preserve some bits for version 2 hardware too? */
563 if (etd->hw_version == 1) {
564 if (attr->reg == 0x10)
565 /* Force absolute mode always on */
566 value |= ETP_R10_ABSOLUTE_MODE;
567 else if (attr->reg == 0x11)
568 /* Force 4 byte mode always on */
569 value |= ETP_R11_4_BYTE_MODE;
570 }
571
572 if (!attr->reg || elantech_write_reg(psmouse, attr->reg, value) == 0)
573 *reg = value;
574
575 return count;
576}
577
578#define ELANTECH_INT_ATTR(_name, _register) \
579 static struct elantech_attr_data elantech_attr_##_name = { \
580 .field_offset = offsetof(struct elantech_data, _name), \
581 .reg = _register, \
582 }; \
583 PSMOUSE_DEFINE_ATTR(_name, S_IWUSR | S_IRUGO, \
584 &elantech_attr_##_name, \
585 elantech_show_int_attr, \
586 elantech_set_int_attr)
587
588ELANTECH_INT_ATTR(reg_10, 0x10);
589ELANTECH_INT_ATTR(reg_11, 0x11);
590ELANTECH_INT_ATTR(reg_20, 0x20);
591ELANTECH_INT_ATTR(reg_21, 0x21);
592ELANTECH_INT_ATTR(reg_22, 0x22);
593ELANTECH_INT_ATTR(reg_23, 0x23);
594ELANTECH_INT_ATTR(reg_24, 0x24);
595ELANTECH_INT_ATTR(reg_25, 0x25);
596ELANTECH_INT_ATTR(reg_26, 0x26);
597ELANTECH_INT_ATTR(debug, 0);
598ELANTECH_INT_ATTR(paritycheck, 0);
599
600static struct attribute *elantech_attrs[] = {
601 &psmouse_attr_reg_10.dattr.attr,
602 &psmouse_attr_reg_11.dattr.attr,
603 &psmouse_attr_reg_20.dattr.attr,
604 &psmouse_attr_reg_21.dattr.attr,
605 &psmouse_attr_reg_22.dattr.attr,
606 &psmouse_attr_reg_23.dattr.attr,
607 &psmouse_attr_reg_24.dattr.attr,
608 &psmouse_attr_reg_25.dattr.attr,
609 &psmouse_attr_reg_26.dattr.attr,
610 &psmouse_attr_debug.dattr.attr,
611 &psmouse_attr_paritycheck.dattr.attr,
612 NULL
613};
614
615static struct attribute_group elantech_attr_group = {
616 .attrs = elantech_attrs,
617};
618
Dmitry Torokhova0836322010-05-19 10:11:13 -0700619static bool elantech_is_signature_valid(const unsigned char *param)
620{
621 static const unsigned char rates[] = { 200, 100, 80, 60, 40, 20, 10 };
622 int i;
623
624 if (param[0] == 0)
625 return false;
626
627 if (param[1] == 0)
628 return true;
629
630 for (i = 0; i < ARRAY_SIZE(rates); i++)
631 if (param[2] == rates[i])
632 return false;
633
634 return true;
635}
636
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400637/*
638 * Use magic knock to detect Elantech touchpad
639 */
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700640int elantech_detect(struct psmouse *psmouse, bool set_properties)
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400641{
642 struct ps2dev *ps2dev = &psmouse->ps2dev;
643 unsigned char param[3];
644
645 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
646
647 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
648 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
649 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
650 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
651 ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO)) {
Dmitry Torokhovd4ae84a82010-05-13 00:41:15 -0700652 pr_debug("sending Elantech magic knock failed.\n");
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400653 return -1;
654 }
655
656 /*
657 * Report this in case there are Elantech models that use a different
658 * set of magic numbers
659 */
660 if (param[0] != 0x3c || param[1] != 0x03 || param[2] != 0xc8) {
Dmitry Torokhovd4ae84a82010-05-13 00:41:15 -0700661 pr_debug("unexpected magic knock result 0x%02x, 0x%02x, 0x%02x.\n",
Arjan Opmeer9ab7b252009-02-28 13:52:40 -0800662 param[0], param[1], param[2]);
663 return -1;
664 }
665
666 /*
667 * Query touchpad's firmware version and see if it reports known
668 * value to avoid mis-detection. Logitech mice are known to respond
669 * to Elantech magic knock and there might be more.
670 */
671 if (synaptics_send_cmd(psmouse, ETP_FW_VERSION_QUERY, param)) {
Dmitry Torokhovd4ae84a82010-05-13 00:41:15 -0700672 pr_debug("failed to query firmware version.\n");
Arjan Opmeer9ab7b252009-02-28 13:52:40 -0800673 return -1;
674 }
675
Dmitry Torokhovd4ae84a82010-05-13 00:41:15 -0700676 pr_debug("Elantech version query result 0x%02x, 0x%02x, 0x%02x.\n",
Arjan Opmeer9ab7b252009-02-28 13:52:40 -0800677 param[0], param[1], param[2]);
678
Dmitry Torokhova0836322010-05-19 10:11:13 -0700679 if (!elantech_is_signature_valid(param)) {
Florian Ragwitzf81bc782010-04-27 00:47:04 -0700680 if (!force_elantech) {
Dmitry Torokhovd4ae84a82010-05-13 00:41:15 -0700681 pr_debug("Probably not a real Elantech touchpad. Aborting.\n");
Florian Ragwitzf81bc782010-04-27 00:47:04 -0700682 return -1;
683 }
684
Dmitry Torokhovd4ae84a82010-05-13 00:41:15 -0700685 pr_debug("Probably not a real Elantech touchpad. Enabling anyway due to force_elantech.\n");
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400686 }
687
688 if (set_properties) {
689 psmouse->vendor = "Elantech";
690 psmouse->name = "Touchpad";
691 }
692
693 return 0;
694}
695
696/*
697 * Clean up sysfs entries when disconnecting
698 */
699static void elantech_disconnect(struct psmouse *psmouse)
700{
701 sysfs_remove_group(&psmouse->ps2dev.serio->dev.kobj,
702 &elantech_attr_group);
703 kfree(psmouse->private);
704 psmouse->private = NULL;
705}
706
707/*
708 * Put the touchpad back into absolute mode when reconnecting
709 */
710static int elantech_reconnect(struct psmouse *psmouse)
711{
712 if (elantech_detect(psmouse, 0))
713 return -1;
714
715 if (elantech_set_absolute_mode(psmouse)) {
Dmitry Torokhovd4ae84a82010-05-13 00:41:15 -0700716 pr_err("failed to put touchpad back into absolute mode.\n");
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400717 return -1;
718 }
719
720 return 0;
721}
722
723/*
724 * Initialize the touchpad and create sysfs entries
725 */
726int elantech_init(struct psmouse *psmouse)
727{
728 struct elantech_data *etd;
729 int i, error;
730 unsigned char param[3];
731
Arjan Opmeer9ab7b252009-02-28 13:52:40 -0800732 psmouse->private = etd = kzalloc(sizeof(struct elantech_data), GFP_KERNEL);
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400733 if (!etd)
Davidlohr Bueso6792cbb2010-09-29 18:53:35 -0700734 return -ENOMEM;
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400735
736 etd->parity[0] = 1;
737 for (i = 1; i < 256; i++)
738 etd->parity[i] = etd->parity[i & (i - 1)] ^ 1;
739
740 /*
Arjan Opmeer9ab7b252009-02-28 13:52:40 -0800741 * Do the version query again so we can store the result
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400742 */
743 if (synaptics_send_cmd(psmouse, ETP_FW_VERSION_QUERY, param)) {
Dmitry Torokhovd4ae84a82010-05-13 00:41:15 -0700744 pr_err("failed to query firmware version.\n");
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400745 goto init_fail;
746 }
Dmitry Torokhov504e8be2010-05-13 00:41:15 -0700747
748 etd->fw_version = (param[0] << 16) | (param[1] << 8) | param[2];
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400749
750 /*
751 * Assume every version greater than this is new EeePC style
752 * hardware with 6 byte packets
753 */
Dmitry Torokhov504e8be2010-05-13 00:41:15 -0700754 if (etd->fw_version >= 0x020030) {
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400755 etd->hw_version = 2;
756 /* For now show extra debug information */
757 etd->debug = 1;
758 /* Don't know how to do parity checking for version 2 */
759 etd->paritycheck = 0;
Éric Pielf941c702011-05-16 22:45:54 -0700760
761 if (etd->fw_version >= 0x020800)
762 etd->reports_pressure = true;
763
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400764 } else {
765 etd->hw_version = 1;
766 etd->paritycheck = 1;
767 }
Dmitry Torokhov504e8be2010-05-13 00:41:15 -0700768
Dmitry Torokhovd4ae84a82010-05-13 00:41:15 -0700769 pr_info("assuming hardware version %d, firmware version %d.%d.%d\n",
Dmitry Torokhov504e8be2010-05-13 00:41:15 -0700770 etd->hw_version, param[0], param[1], param[2]);
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400771
772 if (synaptics_send_cmd(psmouse, ETP_CAPABILITIES_QUERY, param)) {
Dmitry Torokhovd4ae84a82010-05-13 00:41:15 -0700773 pr_err("failed to query capabilities.\n");
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400774 goto init_fail;
775 }
Dmitry Torokhovd4ae84a82010-05-13 00:41:15 -0700776 pr_info("Synaptics capabilities query result 0x%02x, 0x%02x, 0x%02x.\n",
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400777 param[0], param[1], param[2]);
778 etd->capabilities = param[0];
779
Arjan Opmeer3f8c0df2009-04-18 19:05:40 -0700780 /*
Éric Piel7f29f172010-08-05 23:51:49 -0700781 * This firmware suffers from misreporting coordinates when
Arjan Opmeer3f8c0df2009-04-18 19:05:40 -0700782 * a touch action starts causing the mouse cursor or scrolled page
783 * to jump. Enable a workaround.
784 */
Éric Piel7f29f172010-08-05 23:51:49 -0700785 if (etd->fw_version == 0x020022 || etd->fw_version == 0x020600) {
786 pr_info("firmware version 2.0.34/2.6.0 detected, enabling jumpy cursor workaround\n");
787 etd->jumpy_cursor = true;
Arjan Opmeer3f8c0df2009-04-18 19:05:40 -0700788 }
789
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400790 if (elantech_set_absolute_mode(psmouse)) {
Dmitry Torokhovd4ae84a82010-05-13 00:41:15 -0700791 pr_err("failed to put touchpad into absolute mode.\n");
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400792 goto init_fail;
793 }
794
795 elantech_set_input_params(psmouse);
796
797 error = sysfs_create_group(&psmouse->ps2dev.serio->dev.kobj,
798 &elantech_attr_group);
799 if (error) {
Dmitry Torokhovd4ae84a82010-05-13 00:41:15 -0700800 pr_err("failed to create sysfs attributes, error: %d.\n", error);
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400801 goto init_fail;
802 }
803
804 psmouse->protocol_handler = elantech_process_byte;
805 psmouse->disconnect = elantech_disconnect;
806 psmouse->reconnect = elantech_reconnect;
807 psmouse->pktsize = etd->hw_version == 2 ? 6 : 4;
808
809 return 0;
810
811 init_fail:
812 kfree(etd);
813 return -1;
814}