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/slab.h>
15 #include <linux/module.h>
16 #include <linux/input.h>
17 #include <linux/serio.h>
18 #include <linux/libps2.h>
22 #define elantech_debug(format, arg...) \
25 printk(KERN_DEBUG format, ##arg); \
29 * Send a Synaptics style sliced query command
31 static int synaptics_send_cmd(struct psmouse *psmouse, unsigned char c,
34 if (psmouse_sliced_command(psmouse, c) ||
35 ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_GETINFO)) {
36 pr_err("elantech.c: synaptics_send_cmd query 0x%02x failed.\n", c);
44 * A retrying version of ps2_command
46 static int elantech_ps2_command(struct psmouse *psmouse,
47 unsigned char *param, int command)
49 struct ps2dev *ps2dev = &psmouse->ps2dev;
50 struct elantech_data *etd = psmouse->private;
52 int tries = ETP_PS2_COMMAND_TRIES;
55 rc = ps2_command(ps2dev, param, command);
59 elantech_debug("elantech.c: retrying ps2 command 0x%02x (%d).\n",
61 msleep(ETP_PS2_COMMAND_DELAY);
65 pr_err("elantech.c: ps2 command 0x%02x failed.\n", command);
71 * Send an Elantech style special command to read a value from a register
73 static int elantech_read_reg(struct psmouse *psmouse, unsigned char reg,
76 struct elantech_data *etd = psmouse->private;
77 unsigned char param[3];
80 if (reg < 0x10 || reg > 0x26)
83 if (reg > 0x11 && reg < 0x20)
86 switch (etd->hw_version) {
88 if (psmouse_sliced_command(psmouse, ETP_REGISTER_READ) ||
89 psmouse_sliced_command(psmouse, reg) ||
90 ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_GETINFO)) {
96 if (elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
97 elantech_ps2_command(psmouse, NULL, ETP_REGISTER_READ) ||
98 elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
99 elantech_ps2_command(psmouse, NULL, reg) ||
100 elantech_ps2_command(psmouse, param, PSMOUSE_CMD_GETINFO)) {
107 pr_err("elantech.c: failed to read register 0x%02x.\n", reg);
115 * Send an Elantech style special command to write a register with a value
117 static int elantech_write_reg(struct psmouse *psmouse, unsigned char reg,
120 struct elantech_data *etd = psmouse->private;
123 if (reg < 0x10 || reg > 0x26)
126 if (reg > 0x11 && reg < 0x20)
129 switch (etd->hw_version) {
131 if (psmouse_sliced_command(psmouse, ETP_REGISTER_WRITE) ||
132 psmouse_sliced_command(psmouse, reg) ||
133 psmouse_sliced_command(psmouse, val) ||
134 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11)) {
140 if (elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
141 elantech_ps2_command(psmouse, NULL, ETP_REGISTER_WRITE) ||
142 elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
143 elantech_ps2_command(psmouse, NULL, reg) ||
144 elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
145 elantech_ps2_command(psmouse, NULL, val) ||
146 elantech_ps2_command(psmouse, NULL, PSMOUSE_CMD_SETSCALE11)) {
153 pr_err("elantech.c: failed to write register 0x%02x with value 0x%02x.\n",
160 * Dump a complete mouse movement packet to the syslog
162 static void elantech_packet_dump(unsigned char *packet, int size)
166 printk(KERN_DEBUG "elantech.c: PS/2 packet [");
167 for (i = 0; i < size; i++)
168 printk("%s0x%02x ", (i) ? ", " : " ", packet[i]);
173 * Interpret complete data packets and report absolute mode input events for
174 * hardware version 1. (4 byte packets)
176 static void elantech_report_absolute_v1(struct psmouse *psmouse)
178 struct input_dev *dev = psmouse->dev;
179 struct elantech_data *etd = psmouse->private;
180 unsigned char *packet = psmouse->packet;
182 static int old_fingers;
184 if (etd->fw_version_maj == 0x01) {
185 /* byte 0: D U p1 p2 1 p3 R L
186 byte 1: f 0 th tw x9 x8 y9 y8 */
187 fingers = ((packet[1] & 0x80) >> 7) +
188 ((packet[1] & 0x30) >> 4);
190 /* byte 0: n1 n0 p2 p1 1 p3 R L
191 byte 1: 0 0 0 0 x9 x8 y9 y8 */
192 fingers = (packet[0] & 0xc0) >> 6;
195 if (etd->jumpy_cursor) {
196 /* Discard packets that are likely to have bogus coordinates */
197 if (fingers > old_fingers) {
198 elantech_debug("elantech.c: discarding packet\n");
199 goto discard_packet_v1;
203 input_report_key(dev, BTN_TOUCH, fingers != 0);
205 /* byte 2: x7 x6 x5 x4 x3 x2 x1 x0
206 byte 3: y7 y6 y5 y4 y3 y2 y1 y0 */
208 input_report_abs(dev, ABS_X,
209 ((packet[1] & 0x0c) << 6) | packet[2]);
210 input_report_abs(dev, ABS_Y, ETP_YMAX_V1 -
211 (((packet[1] & 0x03) << 8) | packet[3]));
214 input_report_key(dev, BTN_TOOL_FINGER, fingers == 1);
215 input_report_key(dev, BTN_TOOL_DOUBLETAP, fingers == 2);
216 input_report_key(dev, BTN_TOOL_TRIPLETAP, fingers == 3);
217 input_report_key(dev, BTN_LEFT, packet[0] & 0x01);
218 input_report_key(dev, BTN_RIGHT, packet[0] & 0x02);
220 if ((etd->fw_version_maj == 0x01) &&
221 (etd->capabilities & ETP_CAP_HAS_ROCKER)) {
223 input_report_key(dev, BTN_FORWARD, packet[0] & 0x40);
225 input_report_key(dev, BTN_BACK, packet[0] & 0x80);
231 old_fingers = fingers;
235 * Interpret complete data packets and report absolute mode input events for
236 * hardware version 2. (6 byte packets)
238 static void elantech_report_absolute_v2(struct psmouse *psmouse)
240 struct input_dev *dev = psmouse->dev;
241 unsigned char *packet = psmouse->packet;
242 int fingers, x1, y1, x2, y2;
244 /* byte 0: n1 n0 . . . . R L */
245 fingers = (packet[0] & 0xc0) >> 6;
246 input_report_key(dev, BTN_TOUCH, fingers != 0);
250 /* byte 1: x15 x14 x13 x12 x11 x10 x9 x8
251 byte 2: x7 x6 x5 x4 x4 x2 x1 x0 */
252 input_report_abs(dev, ABS_X, (packet[1] << 8) | packet[2]);
253 /* byte 4: y15 y14 y13 y12 y11 y10 y8 y8
254 byte 5: y7 y6 y5 y4 y3 y2 y1 y0 */
255 input_report_abs(dev, ABS_Y, ETP_YMAX_V2 -
256 ((packet[4] << 8) | packet[5]));
260 /* The coordinate of each finger is reported separately with
261 a lower resolution for two finger touches */
262 /* byte 0: . . ay8 ax8 . . . .
263 byte 1: ax7 ax6 ax5 ax4 ax3 ax2 ax1 ax0 */
264 x1 = ((packet[0] & 0x10) << 4) | packet[1];
265 /* byte 2: ay7 ay6 ay5 ay4 ay3 ay2 ay1 ay0 */
266 y1 = ETP_2FT_YMAX - (((packet[0] & 0x20) << 3) | packet[2]);
267 /* byte 3: . . by8 bx8 . . . .
268 byte 4: bx7 bx6 bx5 bx4 bx3 bx2 bx1 bx0 */
269 x2 = ((packet[3] & 0x10) << 4) | packet[4];
270 /* byte 5: by7 by8 by5 by4 by3 by2 by1 by0 */
271 y2 = ETP_2FT_YMAX - (((packet[3] & 0x20) << 3) | packet[5]);
272 /* For compatibility with the X Synaptics driver scale up one
273 coordinate and report as ordinary mouse movent */
274 input_report_abs(dev, ABS_X, x1 << 2);
275 input_report_abs(dev, ABS_Y, y1 << 2);
276 /* For compatibility with the proprietary X Elantech driver
277 report both coordinates as hat coordinates */
278 input_report_abs(dev, ABS_HAT0X, x1);
279 input_report_abs(dev, ABS_HAT0Y, y1);
280 input_report_abs(dev, ABS_HAT1X, x2);
281 input_report_abs(dev, ABS_HAT1Y, y2);
285 input_report_key(dev, BTN_TOOL_FINGER, fingers == 1);
286 input_report_key(dev, BTN_TOOL_DOUBLETAP, fingers == 2);
287 input_report_key(dev, BTN_TOOL_TRIPLETAP, fingers == 3);
288 input_report_key(dev, BTN_LEFT, packet[0] & 0x01);
289 input_report_key(dev, BTN_RIGHT, packet[0] & 0x02);
294 static int elantech_check_parity_v1(struct psmouse *psmouse)
296 struct elantech_data *etd = psmouse->private;
297 unsigned char *packet = psmouse->packet;
298 unsigned char p1, p2, p3;
300 /* Parity bits are placed differently */
301 if (etd->fw_version_maj == 0x01) {
302 /* byte 0: D U p1 p2 1 p3 R L */
303 p1 = (packet[0] & 0x20) >> 5;
304 p2 = (packet[0] & 0x10) >> 4;
306 /* byte 0: n1 n0 p2 p1 1 p3 R L */
307 p1 = (packet[0] & 0x10) >> 4;
308 p2 = (packet[0] & 0x20) >> 5;
311 p3 = (packet[0] & 0x04) >> 2;
313 return etd->parity[packet[1]] == p1 &&
314 etd->parity[packet[2]] == p2 &&
315 etd->parity[packet[3]] == p3;
319 * Process byte stream from mouse and handle complete packets
321 static psmouse_ret_t elantech_process_byte(struct psmouse *psmouse)
323 struct elantech_data *etd = psmouse->private;
325 if (psmouse->pktcnt < psmouse->pktsize)
326 return PSMOUSE_GOOD_DATA;
329 elantech_packet_dump(psmouse->packet, psmouse->pktsize);
331 switch (etd->hw_version) {
333 if (etd->paritycheck && !elantech_check_parity_v1(psmouse))
334 return PSMOUSE_BAD_DATA;
336 elantech_report_absolute_v1(psmouse);
340 /* We don't know how to check parity in protocol v2 */
341 elantech_report_absolute_v2(psmouse);
345 return PSMOUSE_FULL_PACKET;
349 * Put the touchpad into absolute mode
351 static int elantech_set_absolute_mode(struct psmouse *psmouse)
353 struct elantech_data *etd = psmouse->private;
355 int tries = ETP_READ_BACK_TRIES;
358 switch (etd->hw_version) {
362 if (elantech_write_reg(psmouse, 0x10, etd->reg_10) ||
363 elantech_write_reg(psmouse, 0x11, etd->reg_11)) {
369 /* Windows driver values */
371 etd->reg_11 = 0x88; /* 0x8a */
372 etd->reg_21 = 0x60; /* 0x00 */
373 if (elantech_write_reg(psmouse, 0x10, etd->reg_10) ||
374 elantech_write_reg(psmouse, 0x11, etd->reg_11) ||
375 elantech_write_reg(psmouse, 0x21, etd->reg_21)) {
383 * Read back reg 0x10. For hardware version 1 we must make
384 * sure the absolute mode bit is set. For hardware version 2
385 * the touchpad is probably initalising and not ready until
386 * we read back the value we just wrote.
389 rc = elantech_read_reg(psmouse, 0x10, &val);
393 elantech_debug("elantech.c: retrying read (%d).\n",
395 msleep(ETP_READ_BACK_DELAY);
399 pr_err("elantech.c: failed to read back register 0x10.\n");
400 } else if (etd->hw_version == 1 &&
401 !(val & ETP_R10_ABSOLUTE_MODE)) {
402 pr_err("elantech.c: touchpad refuses "
403 "to switch to absolute mode.\n");
409 pr_err("elantech.c: failed to initialise registers.\n");
415 * Set the appropriate event bits for the input subsystem
417 static void elantech_set_input_params(struct psmouse *psmouse)
419 struct input_dev *dev = psmouse->dev;
420 struct elantech_data *etd = psmouse->private;
422 __set_bit(EV_KEY, dev->evbit);
423 __set_bit(EV_ABS, dev->evbit);
424 __clear_bit(EV_REL, dev->evbit);
426 __set_bit(BTN_LEFT, dev->keybit);
427 __set_bit(BTN_RIGHT, dev->keybit);
429 __set_bit(BTN_TOUCH, dev->keybit);
430 __set_bit(BTN_TOOL_FINGER, dev->keybit);
431 __set_bit(BTN_TOOL_DOUBLETAP, dev->keybit);
432 __set_bit(BTN_TOOL_TRIPLETAP, dev->keybit);
434 switch (etd->hw_version) {
437 if ((etd->fw_version_maj == 0x01) &&
438 (etd->capabilities & ETP_CAP_HAS_ROCKER)) {
439 __set_bit(BTN_FORWARD, dev->keybit);
440 __set_bit(BTN_BACK, dev->keybit);
442 input_set_abs_params(dev, ABS_X, ETP_XMIN_V1, ETP_XMAX_V1, 0, 0);
443 input_set_abs_params(dev, ABS_Y, ETP_YMIN_V1, ETP_YMAX_V1, 0, 0);
447 input_set_abs_params(dev, ABS_X, ETP_XMIN_V2, ETP_XMAX_V2, 0, 0);
448 input_set_abs_params(dev, ABS_Y, ETP_YMIN_V2, ETP_YMAX_V2, 0, 0);
449 input_set_abs_params(dev, ABS_HAT0X, ETP_2FT_XMIN, ETP_2FT_XMAX, 0, 0);
450 input_set_abs_params(dev, ABS_HAT0Y, ETP_2FT_YMIN, ETP_2FT_YMAX, 0, 0);
451 input_set_abs_params(dev, ABS_HAT1X, ETP_2FT_XMIN, ETP_2FT_XMAX, 0, 0);
452 input_set_abs_params(dev, ABS_HAT1Y, ETP_2FT_YMIN, ETP_2FT_YMAX, 0, 0);
457 struct elantech_attr_data {
463 * Display a register value by reading a sysfs entry
465 static ssize_t elantech_show_int_attr(struct psmouse *psmouse, void *data,
468 struct elantech_data *etd = psmouse->private;
469 struct elantech_attr_data *attr = data;
470 unsigned char *reg = (unsigned char *) etd + attr->field_offset;
474 rc = elantech_read_reg(psmouse, attr->reg, reg);
476 return sprintf(buf, "0x%02x\n", (attr->reg && rc) ? -1 : *reg);
480 * Write a register value by writing a sysfs entry
482 static ssize_t elantech_set_int_attr(struct psmouse *psmouse,
483 void *data, const char *buf, size_t count)
485 struct elantech_data *etd = psmouse->private;
486 struct elantech_attr_data *attr = data;
487 unsigned char *reg = (unsigned char *) etd + attr->field_offset;
491 err = strict_strtoul(buf, 16, &value);
498 /* Do we need to preserve some bits for version 2 hardware too? */
499 if (etd->hw_version == 1) {
500 if (attr->reg == 0x10)
501 /* Force absolute mode always on */
502 value |= ETP_R10_ABSOLUTE_MODE;
503 else if (attr->reg == 0x11)
504 /* Force 4 byte mode always on */
505 value |= ETP_R11_4_BYTE_MODE;
508 if (!attr->reg || elantech_write_reg(psmouse, attr->reg, value) == 0)
514 #define ELANTECH_INT_ATTR(_name, _register) \
515 static struct elantech_attr_data elantech_attr_##_name = { \
516 .field_offset = offsetof(struct elantech_data, _name), \
519 PSMOUSE_DEFINE_ATTR(_name, S_IWUSR | S_IRUGO, \
520 &elantech_attr_##_name, \
521 elantech_show_int_attr, \
522 elantech_set_int_attr)
524 ELANTECH_INT_ATTR(reg_10, 0x10);
525 ELANTECH_INT_ATTR(reg_11, 0x11);
526 ELANTECH_INT_ATTR(reg_20, 0x20);
527 ELANTECH_INT_ATTR(reg_21, 0x21);
528 ELANTECH_INT_ATTR(reg_22, 0x22);
529 ELANTECH_INT_ATTR(reg_23, 0x23);
530 ELANTECH_INT_ATTR(reg_24, 0x24);
531 ELANTECH_INT_ATTR(reg_25, 0x25);
532 ELANTECH_INT_ATTR(reg_26, 0x26);
533 ELANTECH_INT_ATTR(debug, 0);
534 ELANTECH_INT_ATTR(paritycheck, 0);
536 static struct attribute *elantech_attrs[] = {
537 &psmouse_attr_reg_10.dattr.attr,
538 &psmouse_attr_reg_11.dattr.attr,
539 &psmouse_attr_reg_20.dattr.attr,
540 &psmouse_attr_reg_21.dattr.attr,
541 &psmouse_attr_reg_22.dattr.attr,
542 &psmouse_attr_reg_23.dattr.attr,
543 &psmouse_attr_reg_24.dattr.attr,
544 &psmouse_attr_reg_25.dattr.attr,
545 &psmouse_attr_reg_26.dattr.attr,
546 &psmouse_attr_debug.dattr.attr,
547 &psmouse_attr_paritycheck.dattr.attr,
551 static struct attribute_group elantech_attr_group = {
552 .attrs = elantech_attrs,
556 * Use magic knock to detect Elantech touchpad
558 int elantech_detect(struct psmouse *psmouse, bool set_properties)
560 struct ps2dev *ps2dev = &psmouse->ps2dev;
561 unsigned char param[3];
563 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
565 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
566 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
567 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
568 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
569 ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO)) {
570 pr_debug("elantech.c: sending Elantech magic knock failed.\n");
575 * Report this in case there are Elantech models that use a different
576 * set of magic numbers
578 if (param[0] != 0x3c || param[1] != 0x03 || param[2] != 0xc8) {
579 pr_debug("elantech.c: "
580 "unexpected magic knock result 0x%02x, 0x%02x, 0x%02x.\n",
581 param[0], param[1], param[2]);
586 * Query touchpad's firmware version and see if it reports known
587 * value to avoid mis-detection. Logitech mice are known to respond
588 * to Elantech magic knock and there might be more.
590 if (synaptics_send_cmd(psmouse, ETP_FW_VERSION_QUERY, param)) {
591 pr_debug("elantech.c: failed to query firmware version.\n");
595 pr_debug("elantech.c: Elantech version query result 0x%02x, 0x%02x, 0x%02x.\n",
596 param[0], param[1], param[2]);
598 if (param[0] == 0 || param[1] != 0) {
599 pr_debug("elantech.c: Probably not a real Elantech touchpad. Aborting.\n");
603 if (set_properties) {
604 psmouse->vendor = "Elantech";
605 psmouse->name = "Touchpad";
612 * Clean up sysfs entries when disconnecting
614 static void elantech_disconnect(struct psmouse *psmouse)
616 sysfs_remove_group(&psmouse->ps2dev.serio->dev.kobj,
617 &elantech_attr_group);
618 kfree(psmouse->private);
619 psmouse->private = NULL;
623 * Put the touchpad back into absolute mode when reconnecting
625 static int elantech_reconnect(struct psmouse *psmouse)
627 if (elantech_detect(psmouse, 0))
630 if (elantech_set_absolute_mode(psmouse)) {
631 pr_err("elantech.c: failed to put touchpad back into absolute mode.\n");
639 * Initialize the touchpad and create sysfs entries
641 int elantech_init(struct psmouse *psmouse)
643 struct elantech_data *etd;
645 unsigned char param[3];
647 psmouse->private = etd = kzalloc(sizeof(struct elantech_data), GFP_KERNEL);
652 for (i = 1; i < 256; i++)
653 etd->parity[i] = etd->parity[i & (i - 1)] ^ 1;
656 * Do the version query again so we can store the result
658 if (synaptics_send_cmd(psmouse, ETP_FW_VERSION_QUERY, param)) {
659 pr_err("elantech.c: failed to query firmware version.\n");
662 etd->fw_version_maj = param[0];
663 etd->fw_version_min = param[2];
666 * Assume every version greater than this is new EeePC style
667 * hardware with 6 byte packets
669 if (etd->fw_version_maj >= 0x02 && etd->fw_version_min >= 0x30) {
671 /* For now show extra debug information */
673 /* Don't know how to do parity checking for version 2 */
674 etd->paritycheck = 0;
677 etd->paritycheck = 1;
679 pr_info("elantech.c: assuming hardware version %d, firmware version %d.%d\n",
680 etd->hw_version, etd->fw_version_maj, etd->fw_version_min);
682 if (synaptics_send_cmd(psmouse, ETP_CAPABILITIES_QUERY, param)) {
683 pr_err("elantech.c: failed to query capabilities.\n");
686 pr_info("elantech.c: Synaptics capabilities query result 0x%02x, 0x%02x, 0x%02x.\n",
687 param[0], param[1], param[2]);
688 etd->capabilities = param[0];
691 * This firmware seems to suffer from misreporting coordinates when
692 * a touch action starts causing the mouse cursor or scrolled page
693 * to jump. Enable a workaround.
695 if (etd->fw_version_maj == 0x02 && etd->fw_version_min == 0x22) {
696 pr_info("elantech.c: firmware version 2.34 detected, "
697 "enabling jumpy cursor workaround\n");
698 etd->jumpy_cursor = 1;
701 if (elantech_set_absolute_mode(psmouse)) {
702 pr_err("elantech.c: failed to put touchpad into absolute mode.\n");
706 elantech_set_input_params(psmouse);
708 error = sysfs_create_group(&psmouse->ps2dev.serio->dev.kobj,
709 &elantech_attr_group);
711 pr_err("elantech.c: failed to create sysfs attributes, error: %d.\n",
716 psmouse->protocol_handler = elantech_process_byte;
717 psmouse->disconnect = elantech_disconnect;
718 psmouse->reconnect = elantech_reconnect;
719 psmouse->pktsize = etd->hw_version == 2 ? 6 : 4;