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/dmi.h>
15 #include <linux/slab.h>
16 #include <linux/module.h>
17 #include <linux/input.h>
18 #include <linux/input/mt.h>
19 #include <linux/serio.h>
20 #include <linux/libps2.h>
24 #define elantech_debug(fmt, ...) \
27 psmouse_printk(KERN_DEBUG, psmouse, \
28 fmt, ##__VA_ARGS__); \
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 psmouse_err(psmouse, "%s query 0x%02x failed.\n", __func__, 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("retrying ps2 command 0x%02x (%d).\n",
64 msleep(ETP_PS2_COMMAND_DELAY);
68 psmouse_err(psmouse, "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 < 0x07 || 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)) {
109 if (elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
110 elantech_ps2_command(psmouse, NULL, ETP_REGISTER_READWRITE) ||
111 elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
112 elantech_ps2_command(psmouse, NULL, reg) ||
113 elantech_ps2_command(psmouse, param, PSMOUSE_CMD_GETINFO)) {
120 psmouse_err(psmouse, "failed to read register 0x%02x.\n", reg);
121 else if (etd->hw_version != 4)
130 * Send an Elantech style special command to write a register with a value
132 static int elantech_write_reg(struct psmouse *psmouse, unsigned char reg,
135 struct elantech_data *etd = psmouse->private;
138 if (reg < 0x07 || reg > 0x26)
141 if (reg > 0x11 && reg < 0x20)
144 switch (etd->hw_version) {
146 if (psmouse_sliced_command(psmouse, ETP_REGISTER_WRITE) ||
147 psmouse_sliced_command(psmouse, reg) ||
148 psmouse_sliced_command(psmouse, val) ||
149 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11)) {
155 if (elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
156 elantech_ps2_command(psmouse, NULL, ETP_REGISTER_WRITE) ||
157 elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
158 elantech_ps2_command(psmouse, NULL, reg) ||
159 elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
160 elantech_ps2_command(psmouse, NULL, val) ||
161 elantech_ps2_command(psmouse, NULL, PSMOUSE_CMD_SETSCALE11)) {
167 if (elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
168 elantech_ps2_command(psmouse, NULL, ETP_REGISTER_READWRITE) ||
169 elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
170 elantech_ps2_command(psmouse, NULL, reg) ||
171 elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
172 elantech_ps2_command(psmouse, NULL, val) ||
173 elantech_ps2_command(psmouse, NULL, PSMOUSE_CMD_SETSCALE11)) {
179 if (elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
180 elantech_ps2_command(psmouse, NULL, ETP_REGISTER_READWRITE) ||
181 elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
182 elantech_ps2_command(psmouse, NULL, reg) ||
183 elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
184 elantech_ps2_command(psmouse, NULL, ETP_REGISTER_READWRITE) ||
185 elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
186 elantech_ps2_command(psmouse, NULL, val) ||
187 elantech_ps2_command(psmouse, NULL, PSMOUSE_CMD_SETSCALE11)) {
195 "failed to write register 0x%02x with value 0x%02x.\n",
202 * Dump a complete mouse movement packet to the syslog
204 static void elantech_packet_dump(struct psmouse *psmouse)
208 psmouse_printk(KERN_DEBUG, psmouse, "PS/2 packet [");
209 for (i = 0; i < psmouse->pktsize; i++)
210 printk("%s0x%02x ", i ? ", " : " ", psmouse->packet[i]);
215 * Interpret complete data packets and report absolute mode input events for
216 * hardware version 1. (4 byte packets)
218 static void elantech_report_absolute_v1(struct psmouse *psmouse)
220 struct input_dev *dev = psmouse->dev;
221 struct elantech_data *etd = psmouse->private;
222 unsigned char *packet = psmouse->packet;
225 if (etd->fw_version < 0x020000) {
227 * byte 0: D U p1 p2 1 p3 R L
228 * byte 1: f 0 th tw x9 x8 y9 y8
230 fingers = ((packet[1] & 0x80) >> 7) +
231 ((packet[1] & 0x30) >> 4);
234 * byte 0: n1 n0 p2 p1 1 p3 R L
235 * byte 1: 0 0 0 0 x9 x8 y9 y8
237 fingers = (packet[0] & 0xc0) >> 6;
240 if (etd->jumpy_cursor) {
242 etd->single_finger_reports = 0;
243 } else if (etd->single_finger_reports < 2) {
244 /* Discard first 2 reports of one finger, bogus */
245 etd->single_finger_reports++;
246 elantech_debug("discarding packet\n");
251 input_report_key(dev, BTN_TOUCH, fingers != 0);
254 * byte 2: x7 x6 x5 x4 x3 x2 x1 x0
255 * byte 3: y7 y6 y5 y4 y3 y2 y1 y0
258 input_report_abs(dev, ABS_X,
259 ((packet[1] & 0x0c) << 6) | packet[2]);
260 input_report_abs(dev, ABS_Y,
261 etd->y_max - (((packet[1] & 0x03) << 8) | packet[3]));
264 input_report_key(dev, BTN_TOOL_FINGER, fingers == 1);
265 input_report_key(dev, BTN_TOOL_DOUBLETAP, fingers == 2);
266 input_report_key(dev, BTN_TOOL_TRIPLETAP, fingers == 3);
267 input_report_key(dev, BTN_LEFT, packet[0] & 0x01);
268 input_report_key(dev, BTN_RIGHT, packet[0] & 0x02);
270 if (etd->fw_version < 0x020000 &&
271 (etd->capabilities[0] & ETP_CAP_HAS_ROCKER)) {
273 input_report_key(dev, BTN_FORWARD, packet[0] & 0x40);
275 input_report_key(dev, BTN_BACK, packet[0] & 0x80);
281 static void elantech_set_slot(struct input_dev *dev, int slot, bool active,
282 unsigned int x, unsigned int y)
284 input_mt_slot(dev, slot);
285 input_mt_report_slot_state(dev, MT_TOOL_FINGER, active);
287 input_report_abs(dev, ABS_MT_POSITION_X, x);
288 input_report_abs(dev, ABS_MT_POSITION_Y, y);
292 /* x1 < x2 and y1 < y2 when two fingers, x = y = 0 when not pressed */
293 static void elantech_report_semi_mt_data(struct input_dev *dev,
294 unsigned int num_fingers,
295 unsigned int x1, unsigned int y1,
296 unsigned int x2, unsigned int y2)
298 elantech_set_slot(dev, 0, num_fingers != 0, x1, y1);
299 elantech_set_slot(dev, 1, num_fingers >= 2, x2, y2);
303 * Interpret complete data packets and report absolute mode input events for
304 * hardware version 2. (6 byte packets)
306 static void elantech_report_absolute_v2(struct psmouse *psmouse)
308 struct elantech_data *etd = psmouse->private;
309 struct input_dev *dev = psmouse->dev;
310 unsigned char *packet = psmouse->packet;
311 unsigned int fingers, x1 = 0, y1 = 0, x2 = 0, y2 = 0;
312 unsigned int width = 0, pres = 0;
314 /* byte 0: n1 n0 . . . . R L */
315 fingers = (packet[0] & 0xc0) >> 6;
320 * Same as one finger, except report of more than 3 fingers:
321 * byte 3: n4 . w1 w0 . . . .
323 if (packet[3] & 0x80)
325 /* pass through... */
328 * byte 1: . . . . x11 x10 x9 x8
329 * byte 2: x7 x6 x5 x4 x4 x2 x1 x0
331 x1 = ((packet[1] & 0x0f) << 8) | packet[2];
333 * byte 4: . . . . y11 y10 y9 y8
334 * byte 5: y7 y6 y5 y4 y3 y2 y1 y0
336 y1 = etd->y_max - (((packet[4] & 0x0f) << 8) | packet[5]);
338 pres = (packet[1] & 0xf0) | ((packet[4] & 0xf0) >> 4);
339 width = ((packet[0] & 0x30) >> 2) | ((packet[3] & 0x30) >> 4);
344 * The coordinate of each finger is reported separately
345 * with a lower resolution for two finger touches:
346 * byte 0: . . ay8 ax8 . . . .
347 * byte 1: ax7 ax6 ax5 ax4 ax3 ax2 ax1 ax0
349 x1 = (((packet[0] & 0x10) << 4) | packet[1]) << 2;
350 /* byte 2: ay7 ay6 ay5 ay4 ay3 ay2 ay1 ay0 */
352 ((((packet[0] & 0x20) << 3) | packet[2]) << 2);
354 * byte 3: . . by8 bx8 . . . .
355 * byte 4: bx7 bx6 bx5 bx4 bx3 bx2 bx1 bx0
357 x2 = (((packet[3] & 0x10) << 4) | packet[4]) << 2;
358 /* byte 5: by7 by8 by5 by4 by3 by2 by1 by0 */
360 ((((packet[3] & 0x20) << 3) | packet[5]) << 2);
362 /* Unknown so just report sensible values */
368 input_report_key(dev, BTN_TOUCH, fingers != 0);
370 input_report_abs(dev, ABS_X, x1);
371 input_report_abs(dev, ABS_Y, y1);
373 elantech_report_semi_mt_data(dev, fingers, x1, y1, x2, y2);
374 input_report_key(dev, BTN_TOOL_FINGER, fingers == 1);
375 input_report_key(dev, BTN_TOOL_DOUBLETAP, fingers == 2);
376 input_report_key(dev, BTN_TOOL_TRIPLETAP, fingers == 3);
377 input_report_key(dev, BTN_TOOL_QUADTAP, fingers == 4);
378 input_report_key(dev, BTN_LEFT, packet[0] & 0x01);
379 input_report_key(dev, BTN_RIGHT, packet[0] & 0x02);
380 if (etd->reports_pressure) {
381 input_report_abs(dev, ABS_PRESSURE, pres);
382 input_report_abs(dev, ABS_TOOL_WIDTH, width);
389 * Interpret complete data packets and report absolute mode input events for
390 * hardware version 3. (12 byte packets for two fingers)
392 static void elantech_report_absolute_v3(struct psmouse *psmouse,
395 struct input_dev *dev = psmouse->dev;
396 struct elantech_data *etd = psmouse->private;
397 unsigned char *packet = psmouse->packet;
398 unsigned int fingers = 0, x1 = 0, y1 = 0, x2 = 0, y2 = 0;
399 unsigned int width = 0, pres = 0;
401 /* byte 0: n1 n0 . . . . R L */
402 fingers = (packet[0] & 0xc0) >> 6;
408 * byte 1: . . . . x11 x10 x9 x8
409 * byte 2: x7 x6 x5 x4 x4 x2 x1 x0
411 x1 = ((packet[1] & 0x0f) << 8) | packet[2];
413 * byte 4: . . . . y11 y10 y9 y8
414 * byte 5: y7 y6 y5 y4 y3 y2 y1 y0
416 y1 = etd->y_max - (((packet[4] & 0x0f) << 8) | packet[5]);
420 if (packet_type == PACKET_V3_HEAD) {
422 * byte 1: . . . . ax11 ax10 ax9 ax8
423 * byte 2: ax7 ax6 ax5 ax4 ax3 ax2 ax1 ax0
425 etd->mt[0].x = ((packet[1] & 0x0f) << 8) | packet[2];
427 * byte 4: . . . . ay11 ay10 ay9 ay8
428 * byte 5: ay7 ay6 ay5 ay4 ay3 ay2 ay1 ay0
430 etd->mt[0].y = etd->y_max -
431 (((packet[4] & 0x0f) << 8) | packet[5]);
433 * wait for next packet
438 /* packet_type == PACKET_V3_TAIL */
441 x2 = ((packet[1] & 0x0f) << 8) | packet[2];
442 y2 = etd->y_max - (((packet[4] & 0x0f) << 8) | packet[5]);
446 pres = (packet[1] & 0xf0) | ((packet[4] & 0xf0) >> 4);
447 width = ((packet[0] & 0x30) >> 2) | ((packet[3] & 0x30) >> 4);
449 input_report_key(dev, BTN_TOUCH, fingers != 0);
451 input_report_abs(dev, ABS_X, x1);
452 input_report_abs(dev, ABS_Y, y1);
454 elantech_report_semi_mt_data(dev, fingers, x1, y1, x2, y2);
455 input_report_key(dev, BTN_TOOL_FINGER, fingers == 1);
456 input_report_key(dev, BTN_TOOL_DOUBLETAP, fingers == 2);
457 input_report_key(dev, BTN_TOOL_TRIPLETAP, fingers == 3);
459 /* For clickpads map both buttons to BTN_LEFT */
460 if (etd->fw_version & 0x001000) {
461 input_report_key(dev, BTN_LEFT, packet[0] & 0x03);
463 input_report_key(dev, BTN_LEFT, packet[0] & 0x01);
464 input_report_key(dev, BTN_RIGHT, packet[0] & 0x02);
467 input_report_abs(dev, ABS_PRESSURE, pres);
468 input_report_abs(dev, ABS_TOOL_WIDTH, width);
473 static void elantech_input_sync_v4(struct psmouse *psmouse)
475 struct input_dev *dev = psmouse->dev;
476 struct elantech_data *etd = psmouse->private;
477 unsigned char *packet = psmouse->packet;
479 /* For clickpads map both buttons to BTN_LEFT */
480 if (etd->fw_version & 0x001000) {
481 input_report_key(dev, BTN_LEFT, packet[0] & 0x03);
483 input_report_key(dev, BTN_LEFT, packet[0] & 0x01);
484 input_report_key(dev, BTN_RIGHT, packet[0] & 0x02);
487 input_mt_report_pointer_emulation(dev, true);
491 static void process_packet_status_v4(struct psmouse *psmouse)
493 struct input_dev *dev = psmouse->dev;
494 unsigned char *packet = psmouse->packet;
498 /* notify finger state change */
499 fingers = packet[1] & 0x1f;
500 for (i = 0; i < ETP_MAX_FINGERS; i++) {
501 if ((fingers & (1 << i)) == 0) {
502 input_mt_slot(dev, i);
503 input_mt_report_slot_state(dev, MT_TOOL_FINGER, false);
507 elantech_input_sync_v4(psmouse);
510 static void process_packet_head_v4(struct psmouse *psmouse)
512 struct input_dev *dev = psmouse->dev;
513 struct elantech_data *etd = psmouse->private;
514 unsigned char *packet = psmouse->packet;
515 int id = ((packet[3] & 0xe0) >> 5) - 1;
521 etd->mt[id].x = ((packet[1] & 0x0f) << 8) | packet[2];
522 etd->mt[id].y = etd->y_max - (((packet[4] & 0x0f) << 8) | packet[5]);
523 pres = (packet[1] & 0xf0) | ((packet[4] & 0xf0) >> 4);
524 traces = (packet[0] & 0xf0) >> 4;
526 input_mt_slot(dev, id);
527 input_mt_report_slot_state(dev, MT_TOOL_FINGER, true);
529 input_report_abs(dev, ABS_MT_POSITION_X, etd->mt[id].x);
530 input_report_abs(dev, ABS_MT_POSITION_Y, etd->mt[id].y);
531 input_report_abs(dev, ABS_MT_PRESSURE, pres);
532 input_report_abs(dev, ABS_MT_TOUCH_MAJOR, traces * etd->width);
533 /* report this for backwards compatibility */
534 input_report_abs(dev, ABS_TOOL_WIDTH, traces);
536 elantech_input_sync_v4(psmouse);
539 static void process_packet_motion_v4(struct psmouse *psmouse)
541 struct input_dev *dev = psmouse->dev;
542 struct elantech_data *etd = psmouse->private;
543 unsigned char *packet = psmouse->packet;
544 int weight, delta_x1 = 0, delta_y1 = 0, delta_x2 = 0, delta_y2 = 0;
547 id = ((packet[0] & 0xe0) >> 5) - 1;
551 sid = ((packet[3] & 0xe0) >> 5) - 1;
552 weight = (packet[0] & 0x10) ? ETP_WEIGHT_VALUE : 1;
554 * Motion packets give us the delta of x, y values of specific fingers,
555 * but in two's complement. Let the compiler do the conversion for us.
556 * Also _enlarge_ the numbers to int, in case of overflow.
558 delta_x1 = (signed char)packet[1];
559 delta_y1 = (signed char)packet[2];
560 delta_x2 = (signed char)packet[4];
561 delta_y2 = (signed char)packet[5];
563 etd->mt[id].x += delta_x1 * weight;
564 etd->mt[id].y -= delta_y1 * weight;
565 input_mt_slot(dev, id);
566 input_report_abs(dev, ABS_MT_POSITION_X, etd->mt[id].x);
567 input_report_abs(dev, ABS_MT_POSITION_Y, etd->mt[id].y);
570 etd->mt[sid].x += delta_x2 * weight;
571 etd->mt[sid].y -= delta_y2 * weight;
572 input_mt_slot(dev, sid);
573 input_report_abs(dev, ABS_MT_POSITION_X, etd->mt[sid].x);
574 input_report_abs(dev, ABS_MT_POSITION_Y, etd->mt[sid].y);
577 elantech_input_sync_v4(psmouse);
580 static void elantech_report_absolute_v4(struct psmouse *psmouse,
583 switch (packet_type) {
584 case PACKET_V4_STATUS:
585 process_packet_status_v4(psmouse);
589 process_packet_head_v4(psmouse);
592 case PACKET_V4_MOTION:
593 process_packet_motion_v4(psmouse);
598 /* impossible to get here */
603 static int elantech_packet_check_v1(struct psmouse *psmouse)
605 struct elantech_data *etd = psmouse->private;
606 unsigned char *packet = psmouse->packet;
607 unsigned char p1, p2, p3;
609 /* Parity bits are placed differently */
610 if (etd->fw_version < 0x020000) {
611 /* byte 0: D U p1 p2 1 p3 R L */
612 p1 = (packet[0] & 0x20) >> 5;
613 p2 = (packet[0] & 0x10) >> 4;
615 /* byte 0: n1 n0 p2 p1 1 p3 R L */
616 p1 = (packet[0] & 0x10) >> 4;
617 p2 = (packet[0] & 0x20) >> 5;
620 p3 = (packet[0] & 0x04) >> 2;
622 return etd->parity[packet[1]] == p1 &&
623 etd->parity[packet[2]] == p2 &&
624 etd->parity[packet[3]] == p3;
627 static int elantech_debounce_check_v2(struct psmouse *psmouse)
630 * When we encounter packet that matches this exactly, it means the
631 * hardware is in debounce status. Just ignore the whole packet.
633 const u8 debounce_packet[] = { 0x84, 0xff, 0xff, 0x02, 0xff, 0xff };
634 unsigned char *packet = psmouse->packet;
636 return !memcmp(packet, debounce_packet, sizeof(debounce_packet));
639 static int elantech_packet_check_v2(struct psmouse *psmouse)
641 struct elantech_data *etd = psmouse->private;
642 unsigned char *packet = psmouse->packet;
645 * V2 hardware has two flavors. Older ones that do not report pressure,
646 * and newer ones that reports pressure and width. With newer ones, all
647 * packets (1, 2, 3 finger touch) have the same constant bits. With
648 * older ones, 1/3 finger touch packets and 2 finger touch packets
649 * have different constant bits.
650 * With all three cases, if the constant bits are not exactly what I
651 * expected, I consider them invalid.
653 if (etd->reports_pressure)
654 return (packet[0] & 0x0c) == 0x04 &&
655 (packet[3] & 0x0f) == 0x02;
657 if ((packet[0] & 0xc0) == 0x80)
658 return (packet[0] & 0x0c) == 0x0c &&
659 (packet[3] & 0x0e) == 0x08;
661 return (packet[0] & 0x3c) == 0x3c &&
662 (packet[1] & 0xf0) == 0x00 &&
663 (packet[3] & 0x3e) == 0x38 &&
664 (packet[4] & 0xf0) == 0x00;
668 * We check the constant bits to determine what packet type we get,
669 * so packet checking is mandatory for v3 and later hardware.
671 static int elantech_packet_check_v3(struct psmouse *psmouse)
673 const u8 debounce_packet[] = { 0xc4, 0xff, 0xff, 0x02, 0xff, 0xff };
674 unsigned char *packet = psmouse->packet;
677 * check debounce first, it has the same signature in byte 0
678 * and byte 3 as PACKET_V3_HEAD.
680 if (!memcmp(packet, debounce_packet, sizeof(debounce_packet)))
681 return PACKET_DEBOUNCE;
683 if ((packet[0] & 0x0c) == 0x04 && (packet[3] & 0xcf) == 0x02)
684 return PACKET_V3_HEAD;
686 if ((packet[0] & 0x0c) == 0x0c && (packet[3] & 0xce) == 0x0c)
687 return PACKET_V3_TAIL;
689 return PACKET_UNKNOWN;
692 static int elantech_packet_check_v4(struct psmouse *psmouse)
694 unsigned char *packet = psmouse->packet;
695 unsigned char packet_type = packet[3] & 0x03;
697 switch (packet_type) {
699 return PACKET_V4_STATUS;
702 return PACKET_V4_HEAD;
705 return PACKET_V4_MOTION;
708 return PACKET_UNKNOWN;
712 * Process byte stream from mouse and handle complete packets
714 static psmouse_ret_t elantech_process_byte(struct psmouse *psmouse)
716 struct elantech_data *etd = psmouse->private;
719 if (psmouse->pktcnt < psmouse->pktsize)
720 return PSMOUSE_GOOD_DATA;
723 elantech_packet_dump(psmouse);
725 switch (etd->hw_version) {
727 if (etd->paritycheck && !elantech_packet_check_v1(psmouse))
728 return PSMOUSE_BAD_DATA;
730 elantech_report_absolute_v1(psmouse);
734 /* ignore debounce */
735 if (elantech_debounce_check_v2(psmouse))
736 return PSMOUSE_FULL_PACKET;
738 if (etd->paritycheck && !elantech_packet_check_v2(psmouse))
739 return PSMOUSE_BAD_DATA;
741 elantech_report_absolute_v2(psmouse);
745 packet_type = elantech_packet_check_v3(psmouse);
746 /* ignore debounce */
747 if (packet_type == PACKET_DEBOUNCE)
748 return PSMOUSE_FULL_PACKET;
750 if (packet_type == PACKET_UNKNOWN)
751 return PSMOUSE_BAD_DATA;
753 elantech_report_absolute_v3(psmouse, packet_type);
757 packet_type = elantech_packet_check_v4(psmouse);
758 if (packet_type == PACKET_UNKNOWN)
759 return PSMOUSE_BAD_DATA;
761 elantech_report_absolute_v4(psmouse, packet_type);
765 return PSMOUSE_FULL_PACKET;
769 * This writes the reg_07 value again to the hardware at the end of every
770 * set_rate call because the register loses its value. reg_07 allows setting
771 * absolute mode on v4 hardware
773 static void elantech_set_rate_restore_reg_07(struct psmouse *psmouse,
776 struct elantech_data *etd = psmouse->private;
778 etd->original_set_rate(psmouse, rate);
779 if (elantech_write_reg(psmouse, 0x07, etd->reg_07))
780 psmouse_err(psmouse, "restoring reg_07 failed\n");
784 * Put the touchpad into absolute mode
786 static int elantech_set_absolute_mode(struct psmouse *psmouse)
788 struct elantech_data *etd = psmouse->private;
790 int tries = ETP_READ_BACK_TRIES;
793 switch (etd->hw_version) {
797 if (elantech_write_reg(psmouse, 0x10, etd->reg_10) ||
798 elantech_write_reg(psmouse, 0x11, etd->reg_11)) {
804 /* Windows driver values */
806 etd->reg_11 = 0x88; /* 0x8a */
807 etd->reg_21 = 0x60; /* 0x00 */
808 if (elantech_write_reg(psmouse, 0x10, etd->reg_10) ||
809 elantech_write_reg(psmouse, 0x11, etd->reg_11) ||
810 elantech_write_reg(psmouse, 0x21, etd->reg_21)) {
816 if (etd->set_hw_resolution)
821 if (elantech_write_reg(psmouse, 0x10, etd->reg_10))
828 if (elantech_write_reg(psmouse, 0x07, etd->reg_07))
831 goto skip_readback_reg_10; /* v4 has no reg 0x10 to read */
836 * Read back reg 0x10. For hardware version 1 we must make
837 * sure the absolute mode bit is set. For hardware version 2
838 * the touchpad is probably initializing and not ready until
839 * we read back the value we just wrote.
842 rc = elantech_read_reg(psmouse, 0x10, &val);
846 elantech_debug("retrying read (%d).\n", tries);
847 msleep(ETP_READ_BACK_DELAY);
852 "failed to read back register 0x10.\n");
853 } else if (etd->hw_version == 1 &&
854 !(val & ETP_R10_ABSOLUTE_MODE)) {
856 "touchpad refuses to switch to absolute mode.\n");
861 skip_readback_reg_10:
863 psmouse_err(psmouse, "failed to initialise registers.\n");
868 static int elantech_set_range(struct psmouse *psmouse,
869 unsigned int *x_min, unsigned int *y_min,
870 unsigned int *x_max, unsigned int *y_max,
873 struct elantech_data *etd = psmouse->private;
874 unsigned char param[3];
875 unsigned char traces;
877 switch (etd->hw_version) {
879 *x_min = ETP_XMIN_V1;
880 *y_min = ETP_YMIN_V1;
881 *x_max = ETP_XMAX_V1;
882 *y_max = ETP_YMAX_V1;
886 if (etd->fw_version == 0x020800 ||
887 etd->fw_version == 0x020b00 ||
888 etd->fw_version == 0x020030) {
889 *x_min = ETP_XMIN_V2;
890 *y_min = ETP_YMIN_V2;
891 *x_max = ETP_XMAX_V2;
892 *y_max = ETP_YMAX_V2;
897 i = (etd->fw_version > 0x020800 &&
898 etd->fw_version < 0x020900) ? 1 : 2;
900 if (synaptics_send_cmd(psmouse, ETP_FW_ID_QUERY, param))
903 fixed_dpi = param[1] & 0x10;
905 if (((etd->fw_version >> 16) == 0x14) && fixed_dpi) {
906 if (synaptics_send_cmd(psmouse, ETP_SAMPLE_QUERY, param))
909 *x_max = (etd->capabilities[1] - i) * param[1] / 2;
910 *y_max = (etd->capabilities[2] - i) * param[2] / 2;
911 } else if (etd->fw_version == 0x040216) {
914 } else if (etd->fw_version == 0x040219 || etd->fw_version == 0x040215) {
918 *x_max = (etd->capabilities[1] - i) * 64;
919 *y_max = (etd->capabilities[2] - i) * 64;
925 if (synaptics_send_cmd(psmouse, ETP_FW_ID_QUERY, param))
928 *x_max = (0x0f & param[0]) << 8 | param[1];
929 *y_max = (0xf0 & param[0]) << 4 | param[2];
933 if (synaptics_send_cmd(psmouse, ETP_FW_ID_QUERY, param))
936 *x_max = (0x0f & param[0]) << 8 | param[1];
937 *y_max = (0xf0 & param[0]) << 4 | param[2];
938 traces = etd->capabilities[1];
939 if ((traces < 2) || (traces > *x_max))
942 *width = *x_max / (traces - 1);
950 * Set the appropriate event bits for the input subsystem
952 static int elantech_set_input_params(struct psmouse *psmouse)
954 struct input_dev *dev = psmouse->dev;
955 struct elantech_data *etd = psmouse->private;
956 unsigned int x_min = 0, y_min = 0, x_max = 0, y_max = 0, width = 0;
958 if (elantech_set_range(psmouse, &x_min, &y_min, &x_max, &y_max, &width))
961 __set_bit(EV_KEY, dev->evbit);
962 __set_bit(EV_ABS, dev->evbit);
963 __clear_bit(EV_REL, dev->evbit);
965 __set_bit(BTN_LEFT, dev->keybit);
966 __set_bit(BTN_RIGHT, dev->keybit);
968 __set_bit(BTN_TOUCH, dev->keybit);
969 __set_bit(BTN_TOOL_FINGER, dev->keybit);
970 __set_bit(BTN_TOOL_DOUBLETAP, dev->keybit);
971 __set_bit(BTN_TOOL_TRIPLETAP, dev->keybit);
973 switch (etd->hw_version) {
976 if (etd->fw_version < 0x020000 &&
977 (etd->capabilities[0] & ETP_CAP_HAS_ROCKER)) {
978 __set_bit(BTN_FORWARD, dev->keybit);
979 __set_bit(BTN_BACK, dev->keybit);
981 input_set_abs_params(dev, ABS_X, x_min, x_max, 0, 0);
982 input_set_abs_params(dev, ABS_Y, y_min, y_max, 0, 0);
986 __set_bit(BTN_TOOL_QUADTAP, dev->keybit);
987 __set_bit(INPUT_PROP_SEMI_MT, dev->propbit);
990 input_set_abs_params(dev, ABS_X, x_min, x_max, 0, 0);
991 input_set_abs_params(dev, ABS_Y, y_min, y_max, 0, 0);
992 if (etd->reports_pressure) {
993 input_set_abs_params(dev, ABS_PRESSURE, ETP_PMIN_V2,
995 input_set_abs_params(dev, ABS_TOOL_WIDTH, ETP_WMIN_V2,
998 input_mt_init_slots(dev, 2);
999 input_set_abs_params(dev, ABS_MT_POSITION_X, x_min, x_max, 0, 0);
1000 input_set_abs_params(dev, ABS_MT_POSITION_Y, y_min, y_max, 0, 0);
1004 __set_bit(BTN_TOOL_QUADTAP, dev->keybit);
1005 /* For X to recognize me as touchpad. */
1006 input_set_abs_params(dev, ABS_X, x_min, x_max, 0, 0);
1007 input_set_abs_params(dev, ABS_Y, y_min, y_max, 0, 0);
1009 * range of pressure and width is the same as v2,
1010 * report ABS_PRESSURE, ABS_TOOL_WIDTH for compatibility.
1012 input_set_abs_params(dev, ABS_PRESSURE, ETP_PMIN_V2,
1014 input_set_abs_params(dev, ABS_TOOL_WIDTH, ETP_WMIN_V2,
1016 /* Multitouch capable pad, up to 5 fingers. */
1017 input_mt_init_slots(dev, ETP_MAX_FINGERS);
1018 input_set_abs_params(dev, ABS_MT_POSITION_X, x_min, x_max, 0, 0);
1019 input_set_abs_params(dev, ABS_MT_POSITION_Y, y_min, y_max, 0, 0);
1020 input_set_abs_params(dev, ABS_MT_PRESSURE, ETP_PMIN_V2,
1023 * The firmware reports how many trace lines the finger spans,
1024 * convert to surface unit as Protocol-B requires.
1026 input_set_abs_params(dev, ABS_MT_TOUCH_MAJOR, 0,
1027 ETP_WMAX_V2 * width, 0, 0);
1037 struct elantech_attr_data {
1038 size_t field_offset;
1043 * Display a register value by reading a sysfs entry
1045 static ssize_t elantech_show_int_attr(struct psmouse *psmouse, void *data,
1048 struct elantech_data *etd = psmouse->private;
1049 struct elantech_attr_data *attr = data;
1050 unsigned char *reg = (unsigned char *) etd + attr->field_offset;
1054 rc = elantech_read_reg(psmouse, attr->reg, reg);
1056 return sprintf(buf, "0x%02x\n", (attr->reg && rc) ? -1 : *reg);
1060 * Write a register value by writing a sysfs entry
1062 static ssize_t elantech_set_int_attr(struct psmouse *psmouse,
1063 void *data, const char *buf, size_t count)
1065 struct elantech_data *etd = psmouse->private;
1066 struct elantech_attr_data *attr = data;
1067 unsigned char *reg = (unsigned char *) etd + attr->field_offset;
1068 unsigned long value;
1071 err = strict_strtoul(buf, 16, &value);
1078 /* Do we need to preserve some bits for version 2 hardware too? */
1079 if (etd->hw_version == 1) {
1080 if (attr->reg == 0x10)
1081 /* Force absolute mode always on */
1082 value |= ETP_R10_ABSOLUTE_MODE;
1083 else if (attr->reg == 0x11)
1084 /* Force 4 byte mode always on */
1085 value |= ETP_R11_4_BYTE_MODE;
1088 if (!attr->reg || elantech_write_reg(psmouse, attr->reg, value) == 0)
1094 #define ELANTECH_INT_ATTR(_name, _register) \
1095 static struct elantech_attr_data elantech_attr_##_name = { \
1096 .field_offset = offsetof(struct elantech_data, _name), \
1099 PSMOUSE_DEFINE_ATTR(_name, S_IWUSR | S_IRUGO, \
1100 &elantech_attr_##_name, \
1101 elantech_show_int_attr, \
1102 elantech_set_int_attr)
1104 ELANTECH_INT_ATTR(reg_07, 0x07);
1105 ELANTECH_INT_ATTR(reg_10, 0x10);
1106 ELANTECH_INT_ATTR(reg_11, 0x11);
1107 ELANTECH_INT_ATTR(reg_20, 0x20);
1108 ELANTECH_INT_ATTR(reg_21, 0x21);
1109 ELANTECH_INT_ATTR(reg_22, 0x22);
1110 ELANTECH_INT_ATTR(reg_23, 0x23);
1111 ELANTECH_INT_ATTR(reg_24, 0x24);
1112 ELANTECH_INT_ATTR(reg_25, 0x25);
1113 ELANTECH_INT_ATTR(reg_26, 0x26);
1114 ELANTECH_INT_ATTR(debug, 0);
1115 ELANTECH_INT_ATTR(paritycheck, 0);
1117 static struct attribute *elantech_attrs[] = {
1118 &psmouse_attr_reg_07.dattr.attr,
1119 &psmouse_attr_reg_10.dattr.attr,
1120 &psmouse_attr_reg_11.dattr.attr,
1121 &psmouse_attr_reg_20.dattr.attr,
1122 &psmouse_attr_reg_21.dattr.attr,
1123 &psmouse_attr_reg_22.dattr.attr,
1124 &psmouse_attr_reg_23.dattr.attr,
1125 &psmouse_attr_reg_24.dattr.attr,
1126 &psmouse_attr_reg_25.dattr.attr,
1127 &psmouse_attr_reg_26.dattr.attr,
1128 &psmouse_attr_debug.dattr.attr,
1129 &psmouse_attr_paritycheck.dattr.attr,
1133 static struct attribute_group elantech_attr_group = {
1134 .attrs = elantech_attrs,
1137 static bool elantech_is_signature_valid(const unsigned char *param)
1139 static const unsigned char rates[] = { 200, 100, 80, 60, 40, 20, 10 };
1149 * Some hw_version >= 4 models have a revision higher then 20. Meaning
1150 * that param[2] may be 10 or 20, skip the rates check for these.
1152 if ((param[0] & 0x0f) >= 0x06 && (param[1] & 0xaf) == 0x0f &&
1156 for (i = 0; i < ARRAY_SIZE(rates); i++)
1157 if (param[2] == rates[i])
1164 * Use magic knock to detect Elantech touchpad
1166 int elantech_detect(struct psmouse *psmouse, bool set_properties)
1168 struct ps2dev *ps2dev = &psmouse->ps2dev;
1169 unsigned char param[3];
1171 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
1173 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1174 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1175 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1176 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1177 ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO)) {
1178 psmouse_dbg(psmouse, "sending Elantech magic knock failed.\n");
1183 * Report this in case there are Elantech models that use a different
1184 * set of magic numbers
1186 if (param[0] != 0x3c || param[1] != 0x03 ||
1187 (param[2] != 0xc8 && param[2] != 0x00)) {
1188 psmouse_dbg(psmouse,
1189 "unexpected magic knock result 0x%02x, 0x%02x, 0x%02x.\n",
1190 param[0], param[1], param[2]);
1195 * Query touchpad's firmware version and see if it reports known
1196 * value to avoid mis-detection. Logitech mice are known to respond
1197 * to Elantech magic knock and there might be more.
1199 if (synaptics_send_cmd(psmouse, ETP_FW_VERSION_QUERY, param)) {
1200 psmouse_dbg(psmouse, "failed to query firmware version.\n");
1204 psmouse_dbg(psmouse,
1205 "Elantech version query result 0x%02x, 0x%02x, 0x%02x.\n",
1206 param[0], param[1], param[2]);
1208 if (!elantech_is_signature_valid(param)) {
1209 psmouse_dbg(psmouse,
1210 "Probably not a real Elantech touchpad. Aborting.\n");
1214 if (set_properties) {
1215 psmouse->vendor = "Elantech";
1216 psmouse->name = "Touchpad";
1223 * Clean up sysfs entries when disconnecting
1225 static void elantech_disconnect(struct psmouse *psmouse)
1227 sysfs_remove_group(&psmouse->ps2dev.serio->dev.kobj,
1228 &elantech_attr_group);
1229 kfree(psmouse->private);
1230 psmouse->private = NULL;
1234 * Put the touchpad back into absolute mode when reconnecting
1236 static int elantech_reconnect(struct psmouse *psmouse)
1238 if (elantech_detect(psmouse, 0))
1241 if (elantech_set_absolute_mode(psmouse)) {
1242 psmouse_err(psmouse,
1243 "failed to put touchpad back into absolute mode.\n");
1251 * Some hw_version 3 models go into error state when we try to set
1252 * bit 3 and/or bit 1 of r10.
1254 static const struct dmi_system_id no_hw_res_dmi_table[] = {
1255 #if defined(CONFIG_DMI) && defined(CONFIG_X86)
1257 /* Gigabyte U2442 */
1259 DMI_MATCH(DMI_SYS_VENDOR, "GIGABYTE"),
1260 DMI_MATCH(DMI_PRODUCT_NAME, "U2442"),
1268 * determine hardware version and set some properties according to it.
1270 static int elantech_set_properties(struct elantech_data *etd)
1272 /* This represents the version of IC body. */
1273 int ver = (etd->fw_version & 0x0f0000) >> 16;
1275 /* Early version of Elan touchpads doesn't obey the rule. */
1276 if (etd->fw_version < 0x020030 || etd->fw_version == 0x020600)
1277 etd->hw_version = 1;
1282 etd->hw_version = 2;
1285 etd->hw_version = 3;
1288 etd->hw_version = 4;
1296 * Turn on packet checking by default.
1298 etd->paritycheck = 1;
1301 * This firmware suffers from misreporting coordinates when
1302 * a touch action starts causing the mouse cursor or scrolled page
1303 * to jump. Enable a workaround.
1306 (etd->fw_version == 0x020022 || etd->fw_version == 0x020600);
1308 if (etd->hw_version > 1) {
1309 /* For now show extra debug information */
1312 if (etd->fw_version >= 0x020800)
1313 etd->reports_pressure = true;
1316 /* Enable real hardware resolution on hw_version 3 ? */
1317 etd->set_hw_resolution = !dmi_check_system(no_hw_res_dmi_table);
1323 * Initialize the touchpad and create sysfs entries
1325 int elantech_init(struct psmouse *psmouse)
1327 struct elantech_data *etd;
1329 unsigned char param[3];
1331 psmouse->private = etd = kzalloc(sizeof(struct elantech_data), GFP_KERNEL);
1336 for (i = 1; i < 256; i++)
1337 etd->parity[i] = etd->parity[i & (i - 1)] ^ 1;
1340 * Do the version query again so we can store the result
1342 if (synaptics_send_cmd(psmouse, ETP_FW_VERSION_QUERY, param)) {
1343 psmouse_err(psmouse, "failed to query firmware version.\n");
1346 etd->fw_version = (param[0] << 16) | (param[1] << 8) | param[2];
1348 if (elantech_set_properties(etd)) {
1349 psmouse_err(psmouse, "unknown hardware version, aborting...\n");
1352 psmouse_info(psmouse,
1353 "assuming hardware version %d (with firmware version 0x%02x%02x%02x)\n",
1354 etd->hw_version, param[0], param[1], param[2]);
1356 if (synaptics_send_cmd(psmouse, ETP_CAPABILITIES_QUERY,
1357 etd->capabilities)) {
1358 psmouse_err(psmouse, "failed to query capabilities.\n");
1361 psmouse_info(psmouse,
1362 "Synaptics capabilities query result 0x%02x, 0x%02x, 0x%02x.\n",
1363 etd->capabilities[0], etd->capabilities[1],
1364 etd->capabilities[2]);
1366 if (elantech_set_absolute_mode(psmouse)) {
1367 psmouse_err(psmouse,
1368 "failed to put touchpad into absolute mode.\n");
1372 if (etd->fw_version == 0x381f17) {
1373 etd->original_set_rate = psmouse->set_rate;
1374 psmouse->set_rate = elantech_set_rate_restore_reg_07;
1377 if (elantech_set_input_params(psmouse)) {
1378 psmouse_err(psmouse, "failed to query touchpad range.\n");
1382 error = sysfs_create_group(&psmouse->ps2dev.serio->dev.kobj,
1383 &elantech_attr_group);
1385 psmouse_err(psmouse,
1386 "failed to create sysfs attributes, error: %d.\n",
1391 psmouse->protocol_handler = elantech_process_byte;
1392 psmouse->disconnect = elantech_disconnect;
1393 psmouse->reconnect = elantech_reconnect;
1394 psmouse->pktsize = etd->hw_version > 1 ? 6 : 4;