2 * Elantech Touchpad driver (v6)
4 * Copyright (C) 2007-2009 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); \
27 static bool force_elantech;
28 module_param_named(force_elantech, force_elantech, bool, 0644);
29 MODULE_PARM_DESC(force_elantech, "Force the Elantech PS/2 protocol extension to be used, 1 = enabled, 0 = disabled (default).");
32 * Send a Synaptics style sliced query command
34 static int synaptics_send_cmd(struct psmouse *psmouse, unsigned char c,
37 if (psmouse_sliced_command(psmouse, c) ||
38 ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_GETINFO)) {
39 pr_err("elantech.c: synaptics_send_cmd query 0x%02x failed.\n", c);
47 * A retrying version of ps2_command
49 static int elantech_ps2_command(struct psmouse *psmouse,
50 unsigned char *param, int command)
52 struct ps2dev *ps2dev = &psmouse->ps2dev;
53 struct elantech_data *etd = psmouse->private;
55 int tries = ETP_PS2_COMMAND_TRIES;
58 rc = ps2_command(ps2dev, param, command);
62 elantech_debug("elantech.c: retrying ps2 command 0x%02x (%d).\n",
64 msleep(ETP_PS2_COMMAND_DELAY);
68 pr_err("elantech.c: ps2 command 0x%02x failed.\n", command);
74 * Send an Elantech style special command to read a value from a register
76 static int elantech_read_reg(struct psmouse *psmouse, unsigned char reg,
79 struct elantech_data *etd = psmouse->private;
80 unsigned char param[3];
83 if (reg < 0x10 || reg > 0x26)
86 if (reg > 0x11 && reg < 0x20)
89 switch (etd->hw_version) {
91 if (psmouse_sliced_command(psmouse, ETP_REGISTER_READ) ||
92 psmouse_sliced_command(psmouse, reg) ||
93 ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_GETINFO)) {
99 if (elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
100 elantech_ps2_command(psmouse, NULL, ETP_REGISTER_READ) ||
101 elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
102 elantech_ps2_command(psmouse, NULL, reg) ||
103 elantech_ps2_command(psmouse, param, PSMOUSE_CMD_GETINFO)) {
110 pr_err("elantech.c: failed to read register 0x%02x.\n", reg);
118 * Send an Elantech style special command to write a register with a value
120 static int elantech_write_reg(struct psmouse *psmouse, unsigned char reg,
123 struct elantech_data *etd = psmouse->private;
126 if (reg < 0x10 || reg > 0x26)
129 if (reg > 0x11 && reg < 0x20)
132 switch (etd->hw_version) {
134 if (psmouse_sliced_command(psmouse, ETP_REGISTER_WRITE) ||
135 psmouse_sliced_command(psmouse, reg) ||
136 psmouse_sliced_command(psmouse, val) ||
137 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11)) {
143 if (elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
144 elantech_ps2_command(psmouse, NULL, ETP_REGISTER_WRITE) ||
145 elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
146 elantech_ps2_command(psmouse, NULL, reg) ||
147 elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
148 elantech_ps2_command(psmouse, NULL, val) ||
149 elantech_ps2_command(psmouse, NULL, PSMOUSE_CMD_SETSCALE11)) {
156 pr_err("elantech.c: failed to write register 0x%02x with value 0x%02x.\n",
163 * Dump a complete mouse movement packet to the syslog
165 static void elantech_packet_dump(unsigned char *packet, int size)
169 printk(KERN_DEBUG "elantech.c: PS/2 packet [");
170 for (i = 0; i < size; i++)
171 printk("%s0x%02x ", (i) ? ", " : " ", packet[i]);
176 * Interpret complete data packets and report absolute mode input events for
177 * hardware version 1. (4 byte packets)
179 static void elantech_report_absolute_v1(struct psmouse *psmouse)
181 struct input_dev *dev = psmouse->dev;
182 struct elantech_data *etd = psmouse->private;
183 unsigned char *packet = psmouse->packet;
185 static int old_fingers;
187 if (etd->fw_version_maj == 0x01) {
188 /* byte 0: D U p1 p2 1 p3 R L
189 byte 1: f 0 th tw x9 x8 y9 y8 */
190 fingers = ((packet[1] & 0x80) >> 7) +
191 ((packet[1] & 0x30) >> 4);
193 /* byte 0: n1 n0 p2 p1 1 p3 R L
194 byte 1: 0 0 0 0 x9 x8 y9 y8 */
195 fingers = (packet[0] & 0xc0) >> 6;
198 if (etd->jumpy_cursor) {
199 /* Discard packets that are likely to have bogus coordinates */
200 if (fingers > old_fingers) {
201 elantech_debug("elantech.c: discarding packet\n");
202 goto discard_packet_v1;
206 input_report_key(dev, BTN_TOUCH, fingers != 0);
208 /* byte 2: x7 x6 x5 x4 x3 x2 x1 x0
209 byte 3: y7 y6 y5 y4 y3 y2 y1 y0 */
211 input_report_abs(dev, ABS_X,
212 ((packet[1] & 0x0c) << 6) | packet[2]);
213 input_report_abs(dev, ABS_Y, ETP_YMAX_V1 -
214 (((packet[1] & 0x03) << 8) | packet[3]));
217 input_report_key(dev, BTN_TOOL_FINGER, fingers == 1);
218 input_report_key(dev, BTN_TOOL_DOUBLETAP, fingers == 2);
219 input_report_key(dev, BTN_TOOL_TRIPLETAP, fingers == 3);
220 input_report_key(dev, BTN_LEFT, packet[0] & 0x01);
221 input_report_key(dev, BTN_RIGHT, packet[0] & 0x02);
223 if ((etd->fw_version_maj == 0x01) &&
224 (etd->capabilities & ETP_CAP_HAS_ROCKER)) {
226 input_report_key(dev, BTN_FORWARD, packet[0] & 0x40);
228 input_report_key(dev, BTN_BACK, packet[0] & 0x80);
234 old_fingers = fingers;
238 * Interpret complete data packets and report absolute mode input events for
239 * hardware version 2. (6 byte packets)
241 static void elantech_report_absolute_v2(struct psmouse *psmouse)
243 struct input_dev *dev = psmouse->dev;
244 unsigned char *packet = psmouse->packet;
245 int fingers, x1, y1, x2, y2;
247 /* byte 0: n1 n0 . . . . R L */
248 fingers = (packet[0] & 0xc0) >> 6;
249 input_report_key(dev, BTN_TOUCH, fingers != 0);
253 /* byte 1: x15 x14 x13 x12 x11 x10 x9 x8
254 byte 2: x7 x6 x5 x4 x4 x2 x1 x0 */
255 input_report_abs(dev, ABS_X, (packet[1] << 8) | packet[2]);
256 /* byte 4: y15 y14 y13 y12 y11 y10 y8 y8
257 byte 5: y7 y6 y5 y4 y3 y2 y1 y0 */
258 input_report_abs(dev, ABS_Y, ETP_YMAX_V2 -
259 ((packet[4] << 8) | packet[5]));
263 /* The coordinate of each finger is reported separately with
264 a lower resolution for two finger touches */
265 /* byte 0: . . ay8 ax8 . . . .
266 byte 1: ax7 ax6 ax5 ax4 ax3 ax2 ax1 ax0 */
267 x1 = ((packet[0] & 0x10) << 4) | packet[1];
268 /* byte 2: ay7 ay6 ay5 ay4 ay3 ay2 ay1 ay0 */
269 y1 = ETP_2FT_YMAX - (((packet[0] & 0x20) << 3) | packet[2]);
270 /* byte 3: . . by8 bx8 . . . .
271 byte 4: bx7 bx6 bx5 bx4 bx3 bx2 bx1 bx0 */
272 x2 = ((packet[3] & 0x10) << 4) | packet[4];
273 /* byte 5: by7 by8 by5 by4 by3 by2 by1 by0 */
274 y2 = ETP_2FT_YMAX - (((packet[3] & 0x20) << 3) | packet[5]);
275 /* For compatibility with the X Synaptics driver scale up one
276 coordinate and report as ordinary mouse movent */
277 input_report_abs(dev, ABS_X, x1 << 2);
278 input_report_abs(dev, ABS_Y, y1 << 2);
279 /* For compatibility with the proprietary X Elantech driver
280 report both coordinates as hat coordinates */
281 input_report_abs(dev, ABS_HAT0X, x1);
282 input_report_abs(dev, ABS_HAT0Y, y1);
283 input_report_abs(dev, ABS_HAT1X, x2);
284 input_report_abs(dev, ABS_HAT1Y, y2);
288 input_report_key(dev, BTN_TOOL_FINGER, fingers == 1);
289 input_report_key(dev, BTN_TOOL_DOUBLETAP, fingers == 2);
290 input_report_key(dev, BTN_TOOL_TRIPLETAP, fingers == 3);
291 input_report_key(dev, BTN_LEFT, packet[0] & 0x01);
292 input_report_key(dev, BTN_RIGHT, packet[0] & 0x02);
297 static int elantech_check_parity_v1(struct psmouse *psmouse)
299 struct elantech_data *etd = psmouse->private;
300 unsigned char *packet = psmouse->packet;
301 unsigned char p1, p2, p3;
303 /* Parity bits are placed differently */
304 if (etd->fw_version_maj == 0x01) {
305 /* byte 0: D U p1 p2 1 p3 R L */
306 p1 = (packet[0] & 0x20) >> 5;
307 p2 = (packet[0] & 0x10) >> 4;
309 /* byte 0: n1 n0 p2 p1 1 p3 R L */
310 p1 = (packet[0] & 0x10) >> 4;
311 p2 = (packet[0] & 0x20) >> 5;
314 p3 = (packet[0] & 0x04) >> 2;
316 return etd->parity[packet[1]] == p1 &&
317 etd->parity[packet[2]] == p2 &&
318 etd->parity[packet[3]] == p3;
322 * Process byte stream from mouse and handle complete packets
324 static psmouse_ret_t elantech_process_byte(struct psmouse *psmouse)
326 struct elantech_data *etd = psmouse->private;
328 if (psmouse->pktcnt < psmouse->pktsize)
329 return PSMOUSE_GOOD_DATA;
332 elantech_packet_dump(psmouse->packet, psmouse->pktsize);
334 switch (etd->hw_version) {
336 if (etd->paritycheck && !elantech_check_parity_v1(psmouse))
337 return PSMOUSE_BAD_DATA;
339 elantech_report_absolute_v1(psmouse);
343 /* We don't know how to check parity in protocol v2 */
344 elantech_report_absolute_v2(psmouse);
348 return PSMOUSE_FULL_PACKET;
352 * Put the touchpad into absolute mode
354 static int elantech_set_absolute_mode(struct psmouse *psmouse)
356 struct elantech_data *etd = psmouse->private;
358 int tries = ETP_READ_BACK_TRIES;
361 switch (etd->hw_version) {
365 if (elantech_write_reg(psmouse, 0x10, etd->reg_10) ||
366 elantech_write_reg(psmouse, 0x11, etd->reg_11)) {
372 /* Windows driver values */
374 etd->reg_11 = 0x88; /* 0x8a */
375 etd->reg_21 = 0x60; /* 0x00 */
376 if (elantech_write_reg(psmouse, 0x10, etd->reg_10) ||
377 elantech_write_reg(psmouse, 0x11, etd->reg_11) ||
378 elantech_write_reg(psmouse, 0x21, etd->reg_21)) {
386 * Read back reg 0x10. For hardware version 1 we must make
387 * sure the absolute mode bit is set. For hardware version 2
388 * the touchpad is probably initalising and not ready until
389 * we read back the value we just wrote.
392 rc = elantech_read_reg(psmouse, 0x10, &val);
396 elantech_debug("elantech.c: retrying read (%d).\n",
398 msleep(ETP_READ_BACK_DELAY);
402 pr_err("elantech.c: failed to read back register 0x10.\n");
403 } else if (etd->hw_version == 1 &&
404 !(val & ETP_R10_ABSOLUTE_MODE)) {
405 pr_err("elantech.c: touchpad refuses "
406 "to switch to absolute mode.\n");
412 pr_err("elantech.c: failed to initialise registers.\n");
418 * Set the appropriate event bits for the input subsystem
420 static void elantech_set_input_params(struct psmouse *psmouse)
422 struct input_dev *dev = psmouse->dev;
423 struct elantech_data *etd = psmouse->private;
425 __set_bit(EV_KEY, dev->evbit);
426 __set_bit(EV_ABS, dev->evbit);
427 __clear_bit(EV_REL, dev->evbit);
429 __set_bit(BTN_LEFT, dev->keybit);
430 __set_bit(BTN_RIGHT, dev->keybit);
432 __set_bit(BTN_TOUCH, dev->keybit);
433 __set_bit(BTN_TOOL_FINGER, dev->keybit);
434 __set_bit(BTN_TOOL_DOUBLETAP, dev->keybit);
435 __set_bit(BTN_TOOL_TRIPLETAP, dev->keybit);
437 switch (etd->hw_version) {
440 if ((etd->fw_version_maj == 0x01) &&
441 (etd->capabilities & ETP_CAP_HAS_ROCKER)) {
442 __set_bit(BTN_FORWARD, dev->keybit);
443 __set_bit(BTN_BACK, dev->keybit);
445 input_set_abs_params(dev, ABS_X, ETP_XMIN_V1, ETP_XMAX_V1, 0, 0);
446 input_set_abs_params(dev, ABS_Y, ETP_YMIN_V1, ETP_YMAX_V1, 0, 0);
450 input_set_abs_params(dev, ABS_X, ETP_XMIN_V2, ETP_XMAX_V2, 0, 0);
451 input_set_abs_params(dev, ABS_Y, ETP_YMIN_V2, ETP_YMAX_V2, 0, 0);
452 input_set_abs_params(dev, ABS_HAT0X, ETP_2FT_XMIN, ETP_2FT_XMAX, 0, 0);
453 input_set_abs_params(dev, ABS_HAT0Y, ETP_2FT_YMIN, ETP_2FT_YMAX, 0, 0);
454 input_set_abs_params(dev, ABS_HAT1X, ETP_2FT_XMIN, ETP_2FT_XMAX, 0, 0);
455 input_set_abs_params(dev, ABS_HAT1Y, ETP_2FT_YMIN, ETP_2FT_YMAX, 0, 0);
460 struct elantech_attr_data {
466 * Display a register value by reading a sysfs entry
468 static ssize_t elantech_show_int_attr(struct psmouse *psmouse, void *data,
471 struct elantech_data *etd = psmouse->private;
472 struct elantech_attr_data *attr = data;
473 unsigned char *reg = (unsigned char *) etd + attr->field_offset;
477 rc = elantech_read_reg(psmouse, attr->reg, reg);
479 return sprintf(buf, "0x%02x\n", (attr->reg && rc) ? -1 : *reg);
483 * Write a register value by writing a sysfs entry
485 static ssize_t elantech_set_int_attr(struct psmouse *psmouse,
486 void *data, const char *buf, size_t count)
488 struct elantech_data *etd = psmouse->private;
489 struct elantech_attr_data *attr = data;
490 unsigned char *reg = (unsigned char *) etd + attr->field_offset;
494 err = strict_strtoul(buf, 16, &value);
501 /* Do we need to preserve some bits for version 2 hardware too? */
502 if (etd->hw_version == 1) {
503 if (attr->reg == 0x10)
504 /* Force absolute mode always on */
505 value |= ETP_R10_ABSOLUTE_MODE;
506 else if (attr->reg == 0x11)
507 /* Force 4 byte mode always on */
508 value |= ETP_R11_4_BYTE_MODE;
511 if (!attr->reg || elantech_write_reg(psmouse, attr->reg, value) == 0)
517 #define ELANTECH_INT_ATTR(_name, _register) \
518 static struct elantech_attr_data elantech_attr_##_name = { \
519 .field_offset = offsetof(struct elantech_data, _name), \
522 PSMOUSE_DEFINE_ATTR(_name, S_IWUSR | S_IRUGO, \
523 &elantech_attr_##_name, \
524 elantech_show_int_attr, \
525 elantech_set_int_attr)
527 ELANTECH_INT_ATTR(reg_10, 0x10);
528 ELANTECH_INT_ATTR(reg_11, 0x11);
529 ELANTECH_INT_ATTR(reg_20, 0x20);
530 ELANTECH_INT_ATTR(reg_21, 0x21);
531 ELANTECH_INT_ATTR(reg_22, 0x22);
532 ELANTECH_INT_ATTR(reg_23, 0x23);
533 ELANTECH_INT_ATTR(reg_24, 0x24);
534 ELANTECH_INT_ATTR(reg_25, 0x25);
535 ELANTECH_INT_ATTR(reg_26, 0x26);
536 ELANTECH_INT_ATTR(debug, 0);
537 ELANTECH_INT_ATTR(paritycheck, 0);
539 static struct attribute *elantech_attrs[] = {
540 &psmouse_attr_reg_10.dattr.attr,
541 &psmouse_attr_reg_11.dattr.attr,
542 &psmouse_attr_reg_20.dattr.attr,
543 &psmouse_attr_reg_21.dattr.attr,
544 &psmouse_attr_reg_22.dattr.attr,
545 &psmouse_attr_reg_23.dattr.attr,
546 &psmouse_attr_reg_24.dattr.attr,
547 &psmouse_attr_reg_25.dattr.attr,
548 &psmouse_attr_reg_26.dattr.attr,
549 &psmouse_attr_debug.dattr.attr,
550 &psmouse_attr_paritycheck.dattr.attr,
554 static struct attribute_group elantech_attr_group = {
555 .attrs = elantech_attrs,
559 * Use magic knock to detect Elantech touchpad
561 int elantech_detect(struct psmouse *psmouse, bool set_properties)
563 struct ps2dev *ps2dev = &psmouse->ps2dev;
564 unsigned char param[3];
566 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
568 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
569 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
570 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
571 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
572 ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO)) {
573 pr_debug("elantech.c: sending Elantech magic knock failed.\n");
578 * Report this in case there are Elantech models that use a different
579 * set of magic numbers
581 if (param[0] != 0x3c || param[1] != 0x03 || param[2] != 0xc8) {
582 pr_debug("elantech.c: "
583 "unexpected magic knock result 0x%02x, 0x%02x, 0x%02x.\n",
584 param[0], param[1], param[2]);
589 * Query touchpad's firmware version and see if it reports known
590 * value to avoid mis-detection. Logitech mice are known to respond
591 * to Elantech magic knock and there might be more.
593 if (synaptics_send_cmd(psmouse, ETP_FW_VERSION_QUERY, param)) {
594 pr_debug("elantech.c: failed to query firmware version.\n");
598 pr_debug("elantech.c: Elantech version query result 0x%02x, 0x%02x, 0x%02x.\n",
599 param[0], param[1], param[2]);
601 if (param[0] == 0 || param[1] != 0) {
602 if (!force_elantech) {
603 pr_debug("elantech.c: Probably not a real Elantech touchpad. Aborting.\n");
607 pr_debug("elantech.c: Probably not a real Elantech touchpad. Enabling anyway due to force_elantech.\n");
610 if (set_properties) {
611 psmouse->vendor = "Elantech";
612 psmouse->name = "Touchpad";
619 * Clean up sysfs entries when disconnecting
621 static void elantech_disconnect(struct psmouse *psmouse)
623 sysfs_remove_group(&psmouse->ps2dev.serio->dev.kobj,
624 &elantech_attr_group);
625 kfree(psmouse->private);
626 psmouse->private = NULL;
630 * Put the touchpad back into absolute mode when reconnecting
632 static int elantech_reconnect(struct psmouse *psmouse)
634 if (elantech_detect(psmouse, 0))
637 if (elantech_set_absolute_mode(psmouse)) {
638 pr_err("elantech.c: failed to put touchpad back into absolute mode.\n");
646 * Initialize the touchpad and create sysfs entries
648 int elantech_init(struct psmouse *psmouse)
650 struct elantech_data *etd;
652 unsigned char param[3];
654 psmouse->private = etd = kzalloc(sizeof(struct elantech_data), GFP_KERNEL);
659 for (i = 1; i < 256; i++)
660 etd->parity[i] = etd->parity[i & (i - 1)] ^ 1;
663 * Do the version query again so we can store the result
665 if (synaptics_send_cmd(psmouse, ETP_FW_VERSION_QUERY, param)) {
666 pr_err("elantech.c: failed to query firmware version.\n");
669 etd->fw_version_maj = param[0];
670 etd->fw_version_min = param[2];
673 * Assume every version greater than this is new EeePC style
674 * hardware with 6 byte packets
676 if ((etd->fw_version_maj == 0x02 && etd->fw_version_min >= 0x30) ||
677 etd->fw_version_maj > 0x02) {
679 /* For now show extra debug information */
681 /* Don't know how to do parity checking for version 2 */
682 etd->paritycheck = 0;
685 etd->paritycheck = 1;
687 pr_info("elantech.c: assuming hardware version %d, firmware version %d.%d\n",
688 etd->hw_version, etd->fw_version_maj, etd->fw_version_min);
690 if (synaptics_send_cmd(psmouse, ETP_CAPABILITIES_QUERY, param)) {
691 pr_err("elantech.c: failed to query capabilities.\n");
694 pr_info("elantech.c: Synaptics capabilities query result 0x%02x, 0x%02x, 0x%02x.\n",
695 param[0], param[1], param[2]);
696 etd->capabilities = param[0];
699 * This firmware seems to suffer from misreporting coordinates when
700 * a touch action starts causing the mouse cursor or scrolled page
701 * to jump. Enable a workaround.
703 if (etd->fw_version_maj == 0x02 && etd->fw_version_min == 0x22) {
704 pr_info("elantech.c: firmware version 2.34 detected, "
705 "enabling jumpy cursor workaround\n");
706 etd->jumpy_cursor = 1;
709 if (elantech_set_absolute_mode(psmouse)) {
710 pr_err("elantech.c: failed to put touchpad into absolute mode.\n");
714 elantech_set_input_params(psmouse);
716 error = sysfs_create_group(&psmouse->ps2dev.serio->dev.kobj,
717 &elantech_attr_group);
719 pr_err("elantech.c: failed to create sysfs attributes, error: %d.\n",
724 psmouse->protocol_handler = elantech_process_byte;
725 psmouse->disconnect = elantech_disconnect;
726 psmouse->reconnect = elantech_reconnect;
727 psmouse->pktsize = etd->hw_version == 2 ? 6 : 4;