2 * Elantech Touchpad driver (v5)
4 * Copyright (C) 2007-2008 Arjan Opmeer <arjan@opmeer.net>
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.
10 * Trademarks are the property of their respective owners.
13 #include <linux/delay.h>
14 #include <linux/module.h>
15 #include <linux/input.h>
16 #include <linux/serio.h>
17 #include <linux/libps2.h>
21 #define elantech_debug(format, arg...) \
24 printk(KERN_DEBUG format, ##arg); \
28 * Send a Synaptics style sliced query command
30 static int synaptics_send_cmd(struct psmouse
*psmouse
, unsigned char c
,
33 if (psmouse_sliced_command(psmouse
, c
) ||
34 ps2_command(&psmouse
->ps2dev
, param
, PSMOUSE_CMD_GETINFO
)) {
35 pr_err("elantech.c: synaptics_send_cmd query 0x%02x failed.\n", c
);
43 * A retrying version of ps2_command
45 static int elantech_ps2_command(struct psmouse
*psmouse
,
46 unsigned char *param
, int command
)
48 struct ps2dev
*ps2dev
= &psmouse
->ps2dev
;
49 struct elantech_data
*etd
= psmouse
->private;
51 int tries
= ETP_PS2_COMMAND_TRIES
;
54 rc
= ps2_command(ps2dev
, param
, command
);
58 elantech_debug("elantech.c: retrying ps2 command 0x%02x (%d).\n",
60 msleep(ETP_PS2_COMMAND_DELAY
);
64 pr_err("elantech.c: ps2 command 0x%02x failed.\n", command
);
70 * Send an Elantech style special command to read a value from a register
72 static int elantech_read_reg(struct psmouse
*psmouse
, unsigned char reg
,
75 struct elantech_data
*etd
= psmouse
->private;
76 unsigned char param
[3];
79 if (reg
< 0x10 || reg
> 0x26)
82 if (reg
> 0x11 && reg
< 0x20)
85 switch (etd
->hw_version
) {
87 if (psmouse_sliced_command(psmouse
, ETP_REGISTER_READ
) ||
88 psmouse_sliced_command(psmouse
, reg
) ||
89 ps2_command(&psmouse
->ps2dev
, param
, PSMOUSE_CMD_GETINFO
)) {
95 if (elantech_ps2_command(psmouse
, NULL
, ETP_PS2_CUSTOM_COMMAND
) ||
96 elantech_ps2_command(psmouse
, NULL
, ETP_REGISTER_READ
) ||
97 elantech_ps2_command(psmouse
, NULL
, ETP_PS2_CUSTOM_COMMAND
) ||
98 elantech_ps2_command(psmouse
, NULL
, reg
) ||
99 elantech_ps2_command(psmouse
, param
, PSMOUSE_CMD_GETINFO
)) {
106 pr_err("elantech.c: failed to read register 0x%02x.\n", reg
);
114 * Send an Elantech style special command to write a register with a value
116 static int elantech_write_reg(struct psmouse
*psmouse
, unsigned char reg
,
119 struct elantech_data
*etd
= psmouse
->private;
122 if (reg
< 0x10 || reg
> 0x26)
125 if (reg
> 0x11 && reg
< 0x20)
128 switch (etd
->hw_version
) {
130 if (psmouse_sliced_command(psmouse
, ETP_REGISTER_WRITE
) ||
131 psmouse_sliced_command(psmouse
, reg
) ||
132 psmouse_sliced_command(psmouse
, val
) ||
133 ps2_command(&psmouse
->ps2dev
, NULL
, PSMOUSE_CMD_SETSCALE11
)) {
139 if (elantech_ps2_command(psmouse
, NULL
, ETP_PS2_CUSTOM_COMMAND
) ||
140 elantech_ps2_command(psmouse
, NULL
, ETP_REGISTER_WRITE
) ||
141 elantech_ps2_command(psmouse
, NULL
, ETP_PS2_CUSTOM_COMMAND
) ||
142 elantech_ps2_command(psmouse
, NULL
, reg
) ||
143 elantech_ps2_command(psmouse
, NULL
, ETP_PS2_CUSTOM_COMMAND
) ||
144 elantech_ps2_command(psmouse
, NULL
, val
) ||
145 elantech_ps2_command(psmouse
, NULL
, PSMOUSE_CMD_SETSCALE11
)) {
152 pr_err("elantech.c: failed to write register 0x%02x with value 0x%02x.\n",
159 * Dump a complete mouse movement packet to the syslog
161 static void elantech_packet_dump(unsigned char *packet
, int size
)
165 printk(KERN_DEBUG
"elantech.c: PS/2 packet [");
166 for (i
= 0; i
< size
; i
++)
167 printk("%s0x%02x ", (i
) ? ", " : " ", packet
[i
]);
172 * Interpret complete data packets and report absolute mode input events for
173 * hardware version 1. (4 byte packets)
175 static void elantech_report_absolute_v1(struct psmouse
*psmouse
)
177 struct input_dev
*dev
= psmouse
->dev
;
178 struct elantech_data
*etd
= psmouse
->private;
179 unsigned char *packet
= psmouse
->packet
;
182 if (etd
->fw_version_maj
== 0x01) {
183 /* byte 0: D U p1 p2 1 p3 R L
184 byte 1: f 0 th tw x9 x8 y9 y8 */
185 fingers
= ((packet
[1] & 0x80) >> 7) +
186 ((packet
[1] & 0x30) >> 4);
188 /* byte 0: n1 n0 p2 p1 1 p3 R L
189 byte 1: 0 0 0 0 x9 x8 y9 y8 */
190 fingers
= (packet
[0] & 0xc0) >> 6;
193 input_report_key(dev
, BTN_TOUCH
, fingers
!= 0);
195 /* byte 2: x7 x6 x5 x4 x3 x2 x1 x0
196 byte 3: y7 y6 y5 y4 y3 y2 y1 y0 */
198 input_report_abs(dev
, ABS_X
,
199 ((packet
[1] & 0x0c) << 6) | packet
[2]);
200 input_report_abs(dev
, ABS_Y
, ETP_YMAX_V1
-
201 (((packet
[1] & 0x03) << 8) | packet
[3]));
204 input_report_key(dev
, BTN_TOOL_FINGER
, fingers
== 1);
205 input_report_key(dev
, BTN_TOOL_DOUBLETAP
, fingers
== 2);
206 input_report_key(dev
, BTN_TOOL_TRIPLETAP
, fingers
== 3);
207 input_report_key(dev
, BTN_LEFT
, packet
[0] & 0x01);
208 input_report_key(dev
, BTN_RIGHT
, packet
[0] & 0x02);
210 if ((etd
->fw_version_maj
== 0x01) &&
211 (etd
->capabilities
& ETP_CAP_HAS_ROCKER
)) {
213 input_report_key(dev
, BTN_FORWARD
, packet
[0] & 0x40);
215 input_report_key(dev
, BTN_BACK
, packet
[0] & 0x80);
222 * Interpret complete data packets and report absolute mode input events for
223 * hardware version 2. (6 byte packets)
225 static void elantech_report_absolute_v2(struct psmouse
*psmouse
)
227 struct input_dev
*dev
= psmouse
->dev
;
228 unsigned char *packet
= psmouse
->packet
;
229 int fingers
, x1
, y1
, x2
, y2
;
231 /* byte 0: n1 n0 . . . . R L */
232 fingers
= (packet
[0] & 0xc0) >> 6;
233 input_report_key(dev
, BTN_TOUCH
, fingers
!= 0);
237 /* byte 1: x15 x14 x13 x12 x11 x10 x9 x8
238 byte 2: x7 x6 x5 x4 x4 x2 x1 x0 */
239 input_report_abs(dev
, ABS_X
, (packet
[1] << 8) | packet
[2]);
240 /* byte 4: y15 y14 y13 y12 y11 y10 y8 y8
241 byte 5: y7 y6 y5 y4 y3 y2 y1 y0 */
242 input_report_abs(dev
, ABS_Y
, ETP_YMAX_V2
-
243 ((packet
[4] << 8) | packet
[5]));
247 /* The coordinate of each finger is reported separately with
248 a lower resolution for two finger touches */
249 /* byte 0: . . ay8 ax8 . . . .
250 byte 1: ax7 ax6 ax5 ax4 ax3 ax2 ax1 ax0 */
251 x1
= ((packet
[0] & 0x10) << 4) | packet
[1];
252 /* byte 2: ay7 ay6 ay5 ay4 ay3 ay2 ay1 ay0 */
253 y1
= ETP_2FT_YMAX
- (((packet
[0] & 0x20) << 3) | packet
[2]);
254 /* byte 3: . . by8 bx8 . . . .
255 byte 4: bx7 bx6 bx5 bx4 bx3 bx2 bx1 bx0 */
256 x2
= ((packet
[3] & 0x10) << 4) | packet
[4];
257 /* byte 5: by7 by8 by5 by4 by3 by2 by1 by0 */
258 y2
= ETP_2FT_YMAX
- (((packet
[3] & 0x20) << 3) | packet
[5]);
259 /* For compatibility with the X Synaptics driver scale up one
260 coordinate and report as ordinary mouse movent */
261 input_report_abs(dev
, ABS_X
, x1
<< 2);
262 input_report_abs(dev
, ABS_Y
, y1
<< 2);
263 /* For compatibility with the proprietary X Elantech driver
264 report both coordinates as hat coordinates */
265 input_report_abs(dev
, ABS_HAT0X
, x1
);
266 input_report_abs(dev
, ABS_HAT0Y
, y1
);
267 input_report_abs(dev
, ABS_HAT1X
, x2
);
268 input_report_abs(dev
, ABS_HAT1Y
, y2
);
272 input_report_key(dev
, BTN_TOOL_FINGER
, fingers
== 1);
273 input_report_key(dev
, BTN_TOOL_DOUBLETAP
, fingers
== 2);
274 input_report_key(dev
, BTN_TOOL_TRIPLETAP
, fingers
== 3);
275 input_report_key(dev
, BTN_LEFT
, packet
[0] & 0x01);
276 input_report_key(dev
, BTN_RIGHT
, packet
[0] & 0x02);
281 static int elantech_check_parity_v1(struct psmouse
*psmouse
)
283 struct elantech_data
*etd
= psmouse
->private;
284 unsigned char *packet
= psmouse
->packet
;
285 unsigned char p1
, p2
, p3
;
287 /* Parity bits are placed differently */
288 if (etd
->fw_version_maj
== 0x01) {
289 /* byte 0: D U p1 p2 1 p3 R L */
290 p1
= (packet
[0] & 0x20) >> 5;
291 p2
= (packet
[0] & 0x10) >> 4;
293 /* byte 0: n1 n0 p2 p1 1 p3 R L */
294 p1
= (packet
[0] & 0x10) >> 4;
295 p2
= (packet
[0] & 0x20) >> 5;
298 p3
= (packet
[0] & 0x04) >> 2;
300 return etd
->parity
[packet
[1]] == p1
&&
301 etd
->parity
[packet
[2]] == p2
&&
302 etd
->parity
[packet
[3]] == p3
;
306 * Process byte stream from mouse and handle complete packets
308 static psmouse_ret_t
elantech_process_byte(struct psmouse
*psmouse
)
310 struct elantech_data
*etd
= psmouse
->private;
312 if (psmouse
->pktcnt
< psmouse
->pktsize
)
313 return PSMOUSE_GOOD_DATA
;
316 elantech_packet_dump(psmouse
->packet
, psmouse
->pktsize
);
318 switch (etd
->hw_version
) {
320 if (etd
->paritycheck
&& !elantech_check_parity_v1(psmouse
))
321 return PSMOUSE_BAD_DATA
;
323 elantech_report_absolute_v1(psmouse
);
327 /* We don't know how to check parity in protocol v2 */
328 elantech_report_absolute_v2(psmouse
);
332 return PSMOUSE_FULL_PACKET
;
336 * Put the touchpad into absolute mode
338 static int elantech_set_absolute_mode(struct psmouse
*psmouse
)
340 struct elantech_data
*etd
= psmouse
->private;
342 int tries
= ETP_READ_BACK_TRIES
;
345 switch (etd
->hw_version
) {
349 if (elantech_write_reg(psmouse
, 0x10, etd
->reg_10
) ||
350 elantech_write_reg(psmouse
, 0x11, etd
->reg_11
)) {
356 /* Windows driver values */
358 etd
->reg_11
= 0x88; /* 0x8a */
359 etd
->reg_21
= 0x60; /* 0x00 */
360 if (elantech_write_reg(psmouse
, 0x10, etd
->reg_10
) ||
361 elantech_write_reg(psmouse
, 0x11, etd
->reg_11
) ||
362 elantech_write_reg(psmouse
, 0x21, etd
->reg_21
)) {
367 * Read back reg 0x10. The touchpad is probably initalising
368 * and not ready until we read back the value we just wrote.
371 rc
= elantech_read_reg(psmouse
, 0x10, &val
);
375 elantech_debug("elantech.c: retrying read (%d).\n",
377 msleep(ETP_READ_BACK_DELAY
);
380 pr_err("elantech.c: failed to read back register 0x10.\n");
385 pr_err("elantech.c: failed to initialise registers.\n");
391 * Set the appropriate event bits for the input subsystem
393 static void elantech_set_input_params(struct psmouse
*psmouse
)
395 struct input_dev
*dev
= psmouse
->dev
;
396 struct elantech_data
*etd
= psmouse
->private;
398 __set_bit(EV_KEY
, dev
->evbit
);
399 __set_bit(EV_ABS
, dev
->evbit
);
401 __set_bit(BTN_LEFT
, dev
->keybit
);
402 __set_bit(BTN_RIGHT
, dev
->keybit
);
404 __set_bit(BTN_TOUCH
, dev
->keybit
);
405 __set_bit(BTN_TOOL_FINGER
, dev
->keybit
);
406 __set_bit(BTN_TOOL_DOUBLETAP
, dev
->keybit
);
407 __set_bit(BTN_TOOL_TRIPLETAP
, dev
->keybit
);
409 switch (etd
->hw_version
) {
412 if ((etd
->fw_version_maj
== 0x01) &&
413 (etd
->capabilities
& ETP_CAP_HAS_ROCKER
)) {
414 __set_bit(BTN_FORWARD
, dev
->keybit
);
415 __set_bit(BTN_BACK
, dev
->keybit
);
417 input_set_abs_params(dev
, ABS_X
, ETP_XMIN_V1
, ETP_XMAX_V1
, 0, 0);
418 input_set_abs_params(dev
, ABS_Y
, ETP_YMIN_V1
, ETP_YMAX_V1
, 0, 0);
422 input_set_abs_params(dev
, ABS_X
, ETP_XMIN_V2
, ETP_XMAX_V2
, 0, 0);
423 input_set_abs_params(dev
, ABS_Y
, ETP_YMIN_V2
, ETP_YMAX_V2
, 0, 0);
424 input_set_abs_params(dev
, ABS_HAT0X
, ETP_2FT_XMIN
, ETP_2FT_XMAX
, 0, 0);
425 input_set_abs_params(dev
, ABS_HAT0Y
, ETP_2FT_YMIN
, ETP_2FT_YMAX
, 0, 0);
426 input_set_abs_params(dev
, ABS_HAT1X
, ETP_2FT_XMIN
, ETP_2FT_XMAX
, 0, 0);
427 input_set_abs_params(dev
, ABS_HAT1Y
, ETP_2FT_YMIN
, ETP_2FT_YMAX
, 0, 0);
432 struct elantech_attr_data
{
438 * Display a register value by reading a sysfs entry
440 static ssize_t
elantech_show_int_attr(struct psmouse
*psmouse
, void *data
,
443 struct elantech_data
*etd
= psmouse
->private;
444 struct elantech_attr_data
*attr
= data
;
445 unsigned char *reg
= (unsigned char *) etd
+ attr
->field_offset
;
449 rc
= elantech_read_reg(psmouse
, attr
->reg
, reg
);
451 return sprintf(buf
, "0x%02x\n", (attr
->reg
&& rc
) ? -1 : *reg
);
455 * Write a register value by writing a sysfs entry
457 static ssize_t
elantech_set_int_attr(struct psmouse
*psmouse
,
458 void *data
, const char *buf
, size_t count
)
460 struct elantech_data
*etd
= psmouse
->private;
461 struct elantech_attr_data
*attr
= data
;
462 unsigned char *reg
= (unsigned char *) etd
+ attr
->field_offset
;
466 err
= strict_strtoul(buf
, 16, &value
);
473 /* Do we need to preserve some bits for version 2 hardware too? */
474 if (etd
->hw_version
== 1) {
475 if (attr
->reg
== 0x10)
476 /* Force absolute mode always on */
477 value
|= ETP_R10_ABSOLUTE_MODE
;
478 else if (attr
->reg
== 0x11)
479 /* Force 4 byte mode always on */
480 value
|= ETP_R11_4_BYTE_MODE
;
483 if (!attr
->reg
|| elantech_write_reg(psmouse
, attr
->reg
, value
) == 0)
489 #define ELANTECH_INT_ATTR(_name, _register) \
490 static struct elantech_attr_data elantech_attr_##_name = { \
491 .field_offset = offsetof(struct elantech_data, _name), \
494 PSMOUSE_DEFINE_ATTR(_name, S_IWUSR | S_IRUGO, \
495 &elantech_attr_##_name, \
496 elantech_show_int_attr, \
497 elantech_set_int_attr)
499 ELANTECH_INT_ATTR(reg_10
, 0x10);
500 ELANTECH_INT_ATTR(reg_11
, 0x11);
501 ELANTECH_INT_ATTR(reg_20
, 0x20);
502 ELANTECH_INT_ATTR(reg_21
, 0x21);
503 ELANTECH_INT_ATTR(reg_22
, 0x22);
504 ELANTECH_INT_ATTR(reg_23
, 0x23);
505 ELANTECH_INT_ATTR(reg_24
, 0x24);
506 ELANTECH_INT_ATTR(reg_25
, 0x25);
507 ELANTECH_INT_ATTR(reg_26
, 0x26);
508 ELANTECH_INT_ATTR(debug
, 0);
509 ELANTECH_INT_ATTR(paritycheck
, 0);
511 static struct attribute
*elantech_attrs
[] = {
512 &psmouse_attr_reg_10
.dattr
.attr
,
513 &psmouse_attr_reg_11
.dattr
.attr
,
514 &psmouse_attr_reg_20
.dattr
.attr
,
515 &psmouse_attr_reg_21
.dattr
.attr
,
516 &psmouse_attr_reg_22
.dattr
.attr
,
517 &psmouse_attr_reg_23
.dattr
.attr
,
518 &psmouse_attr_reg_24
.dattr
.attr
,
519 &psmouse_attr_reg_25
.dattr
.attr
,
520 &psmouse_attr_reg_26
.dattr
.attr
,
521 &psmouse_attr_debug
.dattr
.attr
,
522 &psmouse_attr_paritycheck
.dattr
.attr
,
526 static struct attribute_group elantech_attr_group
= {
527 .attrs
= elantech_attrs
,
531 * Use magic knock to detect Elantech touchpad
533 int elantech_detect(struct psmouse
*psmouse
, int set_properties
)
535 struct ps2dev
*ps2dev
= &psmouse
->ps2dev
;
536 unsigned char param
[3];
538 ps2_command(&psmouse
->ps2dev
, NULL
, PSMOUSE_CMD_RESET_DIS
);
540 if (ps2_command(ps2dev
, NULL
, PSMOUSE_CMD_DISABLE
) ||
541 ps2_command(ps2dev
, NULL
, PSMOUSE_CMD_SETSCALE11
) ||
542 ps2_command(ps2dev
, NULL
, PSMOUSE_CMD_SETSCALE11
) ||
543 ps2_command(ps2dev
, NULL
, PSMOUSE_CMD_SETSCALE11
) ||
544 ps2_command(ps2dev
, param
, PSMOUSE_CMD_GETINFO
)) {
545 pr_debug("elantech.c: sending Elantech magic knock failed.\n");
550 * Report this in case there are Elantech models that use a different
551 * set of magic numbers
553 if (param
[0] != 0x3c || param
[1] != 0x03 || param
[2] != 0xc8) {
554 pr_debug("elantech.c: "
555 "unexpected magic knock result 0x%02x, 0x%02x, 0x%02x.\n",
556 param
[0], param
[1], param
[2]);
561 * Query touchpad's firmware version and see if it reports known
562 * value to avoid mis-detection. Logitech mice are known to respond
563 * to Elantech magic knock and there might be more.
565 if (synaptics_send_cmd(psmouse
, ETP_FW_VERSION_QUERY
, param
)) {
566 pr_debug("elantech.c: failed to query firmware version.\n");
570 pr_debug("elantech.c: Elantech version query result 0x%02x, 0x%02x, 0x%02x.\n",
571 param
[0], param
[1], param
[2]);
573 if (param
[0] == 0 || param
[1] != 0) {
574 pr_debug("elantech.c: Probably not a real Elantech touchpad. Aborting.\n");
578 if (set_properties
) {
579 psmouse
->vendor
= "Elantech";
580 psmouse
->name
= "Touchpad";
587 * Clean up sysfs entries when disconnecting
589 static void elantech_disconnect(struct psmouse
*psmouse
)
591 sysfs_remove_group(&psmouse
->ps2dev
.serio
->dev
.kobj
,
592 &elantech_attr_group
);
593 kfree(psmouse
->private);
594 psmouse
->private = NULL
;
598 * Put the touchpad back into absolute mode when reconnecting
600 static int elantech_reconnect(struct psmouse
*psmouse
)
602 if (elantech_detect(psmouse
, 0))
605 if (elantech_set_absolute_mode(psmouse
)) {
606 pr_err("elantech.c: failed to put touchpad back into absolute mode.\n");
614 * Initialize the touchpad and create sysfs entries
616 int elantech_init(struct psmouse
*psmouse
)
618 struct elantech_data
*etd
;
620 unsigned char param
[3];
622 psmouse
->private = etd
= kzalloc(sizeof(struct elantech_data
), GFP_KERNEL
);
627 for (i
= 1; i
< 256; i
++)
628 etd
->parity
[i
] = etd
->parity
[i
& (i
- 1)] ^ 1;
631 * Do the version query again so we can store the result
633 if (synaptics_send_cmd(psmouse
, ETP_FW_VERSION_QUERY
, param
)) {
634 pr_err("elantech.c: failed to query firmware version.\n");
637 etd
->fw_version_maj
= param
[0];
638 etd
->fw_version_min
= param
[2];
641 * Assume every version greater than this is new EeePC style
642 * hardware with 6 byte packets
644 if (etd
->fw_version_maj
>= 0x02 && etd
->fw_version_min
>= 0x30) {
646 /* For now show extra debug information */
648 /* Don't know how to do parity checking for version 2 */
649 etd
->paritycheck
= 0;
652 etd
->paritycheck
= 1;
654 pr_info("elantech.c: assuming hardware version %d, firmware version %d.%d\n",
655 etd
->hw_version
, etd
->fw_version_maj
, etd
->fw_version_min
);
657 if (synaptics_send_cmd(psmouse
, ETP_CAPABILITIES_QUERY
, param
)) {
658 pr_err("elantech.c: failed to query capabilities.\n");
661 pr_info("elantech.c: Synaptics capabilities query result 0x%02x, 0x%02x, 0x%02x.\n",
662 param
[0], param
[1], param
[2]);
663 etd
->capabilities
= param
[0];
665 if (elantech_set_absolute_mode(psmouse
)) {
666 pr_err("elantech.c: failed to put touchpad into absolute mode.\n");
670 elantech_set_input_params(psmouse
);
672 error
= sysfs_create_group(&psmouse
->ps2dev
.serio
->dev
.kobj
,
673 &elantech_attr_group
);
675 pr_err("elantech.c: failed to create sysfs attributes, error: %d.\n",
680 psmouse
->protocol_handler
= elantech_process_byte
;
681 psmouse
->disconnect
= elantech_disconnect
;
682 psmouse
->reconnect
= elantech_reconnect
;
683 psmouse
->pktsize
= etd
->hw_version
== 2 ? 6 : 4;