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);
458 input_report_key(dev, BTN_LEFT, packet[0] & 0x01);
459 input_report_key(dev, BTN_RIGHT, packet[0] & 0x02);
460 input_report_abs(dev, ABS_PRESSURE, pres);
461 input_report_abs(dev, ABS_TOOL_WIDTH, width);
466 static void elantech_input_sync_v4(struct psmouse *psmouse)
468 struct input_dev *dev = psmouse->dev;
469 unsigned char *packet = psmouse->packet;
471 input_report_key(dev, BTN_LEFT, packet[0] & 0x01);
472 input_report_key(dev, BTN_RIGHT, packet[0] & 0x02);
473 input_mt_report_pointer_emulation(dev, true);
477 static void process_packet_status_v4(struct psmouse *psmouse)
479 struct input_dev *dev = psmouse->dev;
480 unsigned char *packet = psmouse->packet;
484 /* notify finger state change */
485 fingers = packet[1] & 0x1f;
486 for (i = 0; i < ETP_MAX_FINGERS; i++) {
487 if ((fingers & (1 << i)) == 0) {
488 input_mt_slot(dev, i);
489 input_mt_report_slot_state(dev, MT_TOOL_FINGER, false);
493 elantech_input_sync_v4(psmouse);
496 static void process_packet_head_v4(struct psmouse *psmouse)
498 struct input_dev *dev = psmouse->dev;
499 struct elantech_data *etd = psmouse->private;
500 unsigned char *packet = psmouse->packet;
501 int id = ((packet[3] & 0xe0) >> 5) - 1;
507 etd->mt[id].x = ((packet[1] & 0x0f) << 8) | packet[2];
508 etd->mt[id].y = etd->y_max - (((packet[4] & 0x0f) << 8) | packet[5]);
509 pres = (packet[1] & 0xf0) | ((packet[4] & 0xf0) >> 4);
510 traces = (packet[0] & 0xf0) >> 4;
512 input_mt_slot(dev, id);
513 input_mt_report_slot_state(dev, MT_TOOL_FINGER, true);
515 input_report_abs(dev, ABS_MT_POSITION_X, etd->mt[id].x);
516 input_report_abs(dev, ABS_MT_POSITION_Y, etd->mt[id].y);
517 input_report_abs(dev, ABS_MT_PRESSURE, pres);
518 input_report_abs(dev, ABS_MT_TOUCH_MAJOR, traces * etd->width);
519 /* report this for backwards compatibility */
520 input_report_abs(dev, ABS_TOOL_WIDTH, traces);
522 elantech_input_sync_v4(psmouse);
525 static void process_packet_motion_v4(struct psmouse *psmouse)
527 struct input_dev *dev = psmouse->dev;
528 struct elantech_data *etd = psmouse->private;
529 unsigned char *packet = psmouse->packet;
530 int weight, delta_x1 = 0, delta_y1 = 0, delta_x2 = 0, delta_y2 = 0;
533 id = ((packet[0] & 0xe0) >> 5) - 1;
537 sid = ((packet[3] & 0xe0) >> 5) - 1;
538 weight = (packet[0] & 0x10) ? ETP_WEIGHT_VALUE : 1;
540 * Motion packets give us the delta of x, y values of specific fingers,
541 * but in two's complement. Let the compiler do the conversion for us.
542 * Also _enlarge_ the numbers to int, in case of overflow.
544 delta_x1 = (signed char)packet[1];
545 delta_y1 = (signed char)packet[2];
546 delta_x2 = (signed char)packet[4];
547 delta_y2 = (signed char)packet[5];
549 etd->mt[id].x += delta_x1 * weight;
550 etd->mt[id].y -= delta_y1 * weight;
551 input_mt_slot(dev, id);
552 input_report_abs(dev, ABS_MT_POSITION_X, etd->mt[id].x);
553 input_report_abs(dev, ABS_MT_POSITION_Y, etd->mt[id].y);
556 etd->mt[sid].x += delta_x2 * weight;
557 etd->mt[sid].y -= delta_y2 * weight;
558 input_mt_slot(dev, sid);
559 input_report_abs(dev, ABS_MT_POSITION_X, etd->mt[sid].x);
560 input_report_abs(dev, ABS_MT_POSITION_Y, etd->mt[sid].y);
563 elantech_input_sync_v4(psmouse);
566 static void elantech_report_absolute_v4(struct psmouse *psmouse,
569 switch (packet_type) {
570 case PACKET_V4_STATUS:
571 process_packet_status_v4(psmouse);
575 process_packet_head_v4(psmouse);
578 case PACKET_V4_MOTION:
579 process_packet_motion_v4(psmouse);
584 /* impossible to get here */
589 static int elantech_packet_check_v1(struct psmouse *psmouse)
591 struct elantech_data *etd = psmouse->private;
592 unsigned char *packet = psmouse->packet;
593 unsigned char p1, p2, p3;
595 /* Parity bits are placed differently */
596 if (etd->fw_version < 0x020000) {
597 /* byte 0: D U p1 p2 1 p3 R L */
598 p1 = (packet[0] & 0x20) >> 5;
599 p2 = (packet[0] & 0x10) >> 4;
601 /* byte 0: n1 n0 p2 p1 1 p3 R L */
602 p1 = (packet[0] & 0x10) >> 4;
603 p2 = (packet[0] & 0x20) >> 5;
606 p3 = (packet[0] & 0x04) >> 2;
608 return etd->parity[packet[1]] == p1 &&
609 etd->parity[packet[2]] == p2 &&
610 etd->parity[packet[3]] == p3;
613 static int elantech_debounce_check_v2(struct psmouse *psmouse)
616 * When we encounter packet that matches this exactly, it means the
617 * hardware is in debounce status. Just ignore the whole packet.
619 const u8 debounce_packet[] = { 0x84, 0xff, 0xff, 0x02, 0xff, 0xff };
620 unsigned char *packet = psmouse->packet;
622 return !memcmp(packet, debounce_packet, sizeof(debounce_packet));
625 static int elantech_packet_check_v2(struct psmouse *psmouse)
627 struct elantech_data *etd = psmouse->private;
628 unsigned char *packet = psmouse->packet;
631 * V2 hardware has two flavors. Older ones that do not report pressure,
632 * and newer ones that reports pressure and width. With newer ones, all
633 * packets (1, 2, 3 finger touch) have the same constant bits. With
634 * older ones, 1/3 finger touch packets and 2 finger touch packets
635 * have different constant bits.
636 * With all three cases, if the constant bits are not exactly what I
637 * expected, I consider them invalid.
639 if (etd->reports_pressure)
640 return (packet[0] & 0x0c) == 0x04 &&
641 (packet[3] & 0x0f) == 0x02;
643 if ((packet[0] & 0xc0) == 0x80)
644 return (packet[0] & 0x0c) == 0x0c &&
645 (packet[3] & 0x0e) == 0x08;
647 return (packet[0] & 0x3c) == 0x3c &&
648 (packet[1] & 0xf0) == 0x00 &&
649 (packet[3] & 0x3e) == 0x38 &&
650 (packet[4] & 0xf0) == 0x00;
654 * We check the constant bits to determine what packet type we get,
655 * so packet checking is mandatory for v3 and later hardware.
657 static int elantech_packet_check_v3(struct psmouse *psmouse)
659 const u8 debounce_packet[] = { 0xc4, 0xff, 0xff, 0x02, 0xff, 0xff };
660 unsigned char *packet = psmouse->packet;
663 * check debounce first, it has the same signature in byte 0
664 * and byte 3 as PACKET_V3_HEAD.
666 if (!memcmp(packet, debounce_packet, sizeof(debounce_packet)))
667 return PACKET_DEBOUNCE;
669 if ((packet[0] & 0x0c) == 0x04 && (packet[3] & 0xcf) == 0x02)
670 return PACKET_V3_HEAD;
672 if ((packet[0] & 0x0c) == 0x0c && (packet[3] & 0xce) == 0x0c)
673 return PACKET_V3_TAIL;
675 return PACKET_UNKNOWN;
678 static int elantech_packet_check_v4(struct psmouse *psmouse)
680 unsigned char *packet = psmouse->packet;
682 if ((packet[0] & 0x0c) == 0x04 &&
683 (packet[3] & 0x1f) == 0x11)
684 return PACKET_V4_HEAD;
686 if ((packet[0] & 0x0c) == 0x04 &&
687 (packet[3] & 0x1f) == 0x12)
688 return PACKET_V4_MOTION;
690 if ((packet[0] & 0x0c) == 0x04 &&
691 (packet[3] & 0x1f) == 0x10)
692 return PACKET_V4_STATUS;
694 return PACKET_UNKNOWN;
698 * Process byte stream from mouse and handle complete packets
700 static psmouse_ret_t elantech_process_byte(struct psmouse *psmouse)
702 struct elantech_data *etd = psmouse->private;
705 if (psmouse->pktcnt < psmouse->pktsize)
706 return PSMOUSE_GOOD_DATA;
709 elantech_packet_dump(psmouse);
711 switch (etd->hw_version) {
713 if (etd->paritycheck && !elantech_packet_check_v1(psmouse))
714 return PSMOUSE_BAD_DATA;
716 elantech_report_absolute_v1(psmouse);
720 /* ignore debounce */
721 if (elantech_debounce_check_v2(psmouse))
722 return PSMOUSE_FULL_PACKET;
724 if (etd->paritycheck && !elantech_packet_check_v2(psmouse))
725 return PSMOUSE_BAD_DATA;
727 elantech_report_absolute_v2(psmouse);
731 packet_type = elantech_packet_check_v3(psmouse);
732 /* ignore debounce */
733 if (packet_type == PACKET_DEBOUNCE)
734 return PSMOUSE_FULL_PACKET;
736 if (packet_type == PACKET_UNKNOWN)
737 return PSMOUSE_BAD_DATA;
739 elantech_report_absolute_v3(psmouse, packet_type);
743 packet_type = elantech_packet_check_v4(psmouse);
744 if (packet_type == PACKET_UNKNOWN)
745 return PSMOUSE_BAD_DATA;
747 elantech_report_absolute_v4(psmouse, packet_type);
751 return PSMOUSE_FULL_PACKET;
755 * Put the touchpad into absolute mode
757 static int elantech_set_absolute_mode(struct psmouse *psmouse)
759 struct elantech_data *etd = psmouse->private;
761 int tries = ETP_READ_BACK_TRIES;
764 switch (etd->hw_version) {
768 if (elantech_write_reg(psmouse, 0x10, etd->reg_10) ||
769 elantech_write_reg(psmouse, 0x11, etd->reg_11)) {
775 /* Windows driver values */
777 etd->reg_11 = 0x88; /* 0x8a */
778 etd->reg_21 = 0x60; /* 0x00 */
779 if (elantech_write_reg(psmouse, 0x10, etd->reg_10) ||
780 elantech_write_reg(psmouse, 0x11, etd->reg_11) ||
781 elantech_write_reg(psmouse, 0x21, etd->reg_21)) {
787 if (etd->set_hw_resolution)
792 if (elantech_write_reg(psmouse, 0x10, etd->reg_10))
799 if (elantech_write_reg(psmouse, 0x07, etd->reg_07))
802 goto skip_readback_reg_10; /* v4 has no reg 0x10 to read */
807 * Read back reg 0x10. For hardware version 1 we must make
808 * sure the absolute mode bit is set. For hardware version 2
809 * the touchpad is probably initializing and not ready until
810 * we read back the value we just wrote.
813 rc = elantech_read_reg(psmouse, 0x10, &val);
817 elantech_debug("retrying read (%d).\n", tries);
818 msleep(ETP_READ_BACK_DELAY);
823 "failed to read back register 0x10.\n");
824 } else if (etd->hw_version == 1 &&
825 !(val & ETP_R10_ABSOLUTE_MODE)) {
827 "touchpad refuses to switch to absolute mode.\n");
832 skip_readback_reg_10:
834 psmouse_err(psmouse, "failed to initialise registers.\n");
839 static int elantech_set_range(struct psmouse *psmouse,
840 unsigned int *x_min, unsigned int *y_min,
841 unsigned int *x_max, unsigned int *y_max,
844 struct elantech_data *etd = psmouse->private;
845 unsigned char param[3];
846 unsigned char traces;
848 switch (etd->hw_version) {
850 *x_min = ETP_XMIN_V1;
851 *y_min = ETP_YMIN_V1;
852 *x_max = ETP_XMAX_V1;
853 *y_max = ETP_YMAX_V1;
857 if (etd->fw_version == 0x020800 ||
858 etd->fw_version == 0x020b00 ||
859 etd->fw_version == 0x020030) {
860 *x_min = ETP_XMIN_V2;
861 *y_min = ETP_YMIN_V2;
862 *x_max = ETP_XMAX_V2;
863 *y_max = ETP_YMAX_V2;
868 i = (etd->fw_version > 0x020800 &&
869 etd->fw_version < 0x020900) ? 1 : 2;
871 if (synaptics_send_cmd(psmouse, ETP_FW_ID_QUERY, param))
874 fixed_dpi = param[1] & 0x10;
876 if (((etd->fw_version >> 16) == 0x14) && fixed_dpi) {
877 if (synaptics_send_cmd(psmouse, ETP_SAMPLE_QUERY, param))
880 *x_max = (etd->capabilities[1] - i) * param[1] / 2;
881 *y_max = (etd->capabilities[2] - i) * param[2] / 2;
882 } else if (etd->fw_version == 0x040216) {
885 } else if (etd->fw_version == 0x040219 || etd->fw_version == 0x040215) {
889 *x_max = (etd->capabilities[1] - i) * 64;
890 *y_max = (etd->capabilities[2] - i) * 64;
896 if (synaptics_send_cmd(psmouse, ETP_FW_ID_QUERY, param))
899 *x_max = (0x0f & param[0]) << 8 | param[1];
900 *y_max = (0xf0 & param[0]) << 4 | param[2];
904 if (synaptics_send_cmd(psmouse, ETP_FW_ID_QUERY, param))
907 *x_max = (0x0f & param[0]) << 8 | param[1];
908 *y_max = (0xf0 & param[0]) << 4 | param[2];
909 traces = etd->capabilities[1];
910 if ((traces < 2) || (traces > *x_max))
913 *width = *x_max / (traces - 1);
921 * Set the appropriate event bits for the input subsystem
923 static int elantech_set_input_params(struct psmouse *psmouse)
925 struct input_dev *dev = psmouse->dev;
926 struct elantech_data *etd = psmouse->private;
927 unsigned int x_min = 0, y_min = 0, x_max = 0, y_max = 0, width = 0;
929 if (elantech_set_range(psmouse, &x_min, &y_min, &x_max, &y_max, &width))
932 __set_bit(EV_KEY, dev->evbit);
933 __set_bit(EV_ABS, dev->evbit);
934 __clear_bit(EV_REL, dev->evbit);
936 __set_bit(BTN_LEFT, dev->keybit);
937 __set_bit(BTN_RIGHT, dev->keybit);
939 __set_bit(BTN_TOUCH, dev->keybit);
940 __set_bit(BTN_TOOL_FINGER, dev->keybit);
941 __set_bit(BTN_TOOL_DOUBLETAP, dev->keybit);
942 __set_bit(BTN_TOOL_TRIPLETAP, dev->keybit);
944 switch (etd->hw_version) {
947 if (etd->fw_version < 0x020000 &&
948 (etd->capabilities[0] & ETP_CAP_HAS_ROCKER)) {
949 __set_bit(BTN_FORWARD, dev->keybit);
950 __set_bit(BTN_BACK, dev->keybit);
952 input_set_abs_params(dev, ABS_X, x_min, x_max, 0, 0);
953 input_set_abs_params(dev, ABS_Y, y_min, y_max, 0, 0);
957 __set_bit(BTN_TOOL_QUADTAP, dev->keybit);
958 __set_bit(INPUT_PROP_SEMI_MT, dev->propbit);
961 input_set_abs_params(dev, ABS_X, x_min, x_max, 0, 0);
962 input_set_abs_params(dev, ABS_Y, y_min, y_max, 0, 0);
963 if (etd->reports_pressure) {
964 input_set_abs_params(dev, ABS_PRESSURE, ETP_PMIN_V2,
966 input_set_abs_params(dev, ABS_TOOL_WIDTH, ETP_WMIN_V2,
969 input_mt_init_slots(dev, 2);
970 input_set_abs_params(dev, ABS_MT_POSITION_X, x_min, x_max, 0, 0);
971 input_set_abs_params(dev, ABS_MT_POSITION_Y, y_min, y_max, 0, 0);
975 __set_bit(BTN_TOOL_QUADTAP, dev->keybit);
976 /* For X to recognize me as touchpad. */
977 input_set_abs_params(dev, ABS_X, x_min, x_max, 0, 0);
978 input_set_abs_params(dev, ABS_Y, y_min, y_max, 0, 0);
980 * range of pressure and width is the same as v2,
981 * report ABS_PRESSURE, ABS_TOOL_WIDTH for compatibility.
983 input_set_abs_params(dev, ABS_PRESSURE, ETP_PMIN_V2,
985 input_set_abs_params(dev, ABS_TOOL_WIDTH, ETP_WMIN_V2,
987 /* Multitouch capable pad, up to 5 fingers. */
988 input_mt_init_slots(dev, ETP_MAX_FINGERS);
989 input_set_abs_params(dev, ABS_MT_POSITION_X, x_min, x_max, 0, 0);
990 input_set_abs_params(dev, ABS_MT_POSITION_Y, y_min, y_max, 0, 0);
991 input_set_abs_params(dev, ABS_MT_PRESSURE, ETP_PMIN_V2,
994 * The firmware reports how many trace lines the finger spans,
995 * convert to surface unit as Protocol-B requires.
997 input_set_abs_params(dev, ABS_MT_TOUCH_MAJOR, 0,
998 ETP_WMAX_V2 * width, 0, 0);
1008 struct elantech_attr_data {
1009 size_t field_offset;
1014 * Display a register value by reading a sysfs entry
1016 static ssize_t elantech_show_int_attr(struct psmouse *psmouse, void *data,
1019 struct elantech_data *etd = psmouse->private;
1020 struct elantech_attr_data *attr = data;
1021 unsigned char *reg = (unsigned char *) etd + attr->field_offset;
1025 rc = elantech_read_reg(psmouse, attr->reg, reg);
1027 return sprintf(buf, "0x%02x\n", (attr->reg && rc) ? -1 : *reg);
1031 * Write a register value by writing a sysfs entry
1033 static ssize_t elantech_set_int_attr(struct psmouse *psmouse,
1034 void *data, const char *buf, size_t count)
1036 struct elantech_data *etd = psmouse->private;
1037 struct elantech_attr_data *attr = data;
1038 unsigned char *reg = (unsigned char *) etd + attr->field_offset;
1039 unsigned long value;
1042 err = strict_strtoul(buf, 16, &value);
1049 /* Do we need to preserve some bits for version 2 hardware too? */
1050 if (etd->hw_version == 1) {
1051 if (attr->reg == 0x10)
1052 /* Force absolute mode always on */
1053 value |= ETP_R10_ABSOLUTE_MODE;
1054 else if (attr->reg == 0x11)
1055 /* Force 4 byte mode always on */
1056 value |= ETP_R11_4_BYTE_MODE;
1059 if (!attr->reg || elantech_write_reg(psmouse, attr->reg, value) == 0)
1065 #define ELANTECH_INT_ATTR(_name, _register) \
1066 static struct elantech_attr_data elantech_attr_##_name = { \
1067 .field_offset = offsetof(struct elantech_data, _name), \
1070 PSMOUSE_DEFINE_ATTR(_name, S_IWUSR | S_IRUGO, \
1071 &elantech_attr_##_name, \
1072 elantech_show_int_attr, \
1073 elantech_set_int_attr)
1075 ELANTECH_INT_ATTR(reg_07, 0x07);
1076 ELANTECH_INT_ATTR(reg_10, 0x10);
1077 ELANTECH_INT_ATTR(reg_11, 0x11);
1078 ELANTECH_INT_ATTR(reg_20, 0x20);
1079 ELANTECH_INT_ATTR(reg_21, 0x21);
1080 ELANTECH_INT_ATTR(reg_22, 0x22);
1081 ELANTECH_INT_ATTR(reg_23, 0x23);
1082 ELANTECH_INT_ATTR(reg_24, 0x24);
1083 ELANTECH_INT_ATTR(reg_25, 0x25);
1084 ELANTECH_INT_ATTR(reg_26, 0x26);
1085 ELANTECH_INT_ATTR(debug, 0);
1086 ELANTECH_INT_ATTR(paritycheck, 0);
1088 static struct attribute *elantech_attrs[] = {
1089 &psmouse_attr_reg_07.dattr.attr,
1090 &psmouse_attr_reg_10.dattr.attr,
1091 &psmouse_attr_reg_11.dattr.attr,
1092 &psmouse_attr_reg_20.dattr.attr,
1093 &psmouse_attr_reg_21.dattr.attr,
1094 &psmouse_attr_reg_22.dattr.attr,
1095 &psmouse_attr_reg_23.dattr.attr,
1096 &psmouse_attr_reg_24.dattr.attr,
1097 &psmouse_attr_reg_25.dattr.attr,
1098 &psmouse_attr_reg_26.dattr.attr,
1099 &psmouse_attr_debug.dattr.attr,
1100 &psmouse_attr_paritycheck.dattr.attr,
1104 static struct attribute_group elantech_attr_group = {
1105 .attrs = elantech_attrs,
1108 static bool elantech_is_signature_valid(const unsigned char *param)
1110 static const unsigned char rates[] = { 200, 100, 80, 60, 40, 20, 10 };
1119 for (i = 0; i < ARRAY_SIZE(rates); i++)
1120 if (param[2] == rates[i])
1127 * Use magic knock to detect Elantech touchpad
1129 int elantech_detect(struct psmouse *psmouse, bool set_properties)
1131 struct ps2dev *ps2dev = &psmouse->ps2dev;
1132 unsigned char param[3];
1134 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
1136 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1137 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1138 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1139 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1140 ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO)) {
1141 psmouse_dbg(psmouse, "sending Elantech magic knock failed.\n");
1146 * Report this in case there are Elantech models that use a different
1147 * set of magic numbers
1149 if (param[0] != 0x3c || param[1] != 0x03 ||
1150 (param[2] != 0xc8 && param[2] != 0x00)) {
1151 psmouse_dbg(psmouse,
1152 "unexpected magic knock result 0x%02x, 0x%02x, 0x%02x.\n",
1153 param[0], param[1], param[2]);
1158 * Query touchpad's firmware version and see if it reports known
1159 * value to avoid mis-detection. Logitech mice are known to respond
1160 * to Elantech magic knock and there might be more.
1162 if (synaptics_send_cmd(psmouse, ETP_FW_VERSION_QUERY, param)) {
1163 psmouse_dbg(psmouse, "failed to query firmware version.\n");
1167 psmouse_dbg(psmouse,
1168 "Elantech version query result 0x%02x, 0x%02x, 0x%02x.\n",
1169 param[0], param[1], param[2]);
1171 if (!elantech_is_signature_valid(param)) {
1172 psmouse_dbg(psmouse,
1173 "Probably not a real Elantech touchpad. Aborting.\n");
1177 if (set_properties) {
1178 psmouse->vendor = "Elantech";
1179 psmouse->name = "Touchpad";
1186 * Clean up sysfs entries when disconnecting
1188 static void elantech_disconnect(struct psmouse *psmouse)
1190 sysfs_remove_group(&psmouse->ps2dev.serio->dev.kobj,
1191 &elantech_attr_group);
1192 kfree(psmouse->private);
1193 psmouse->private = NULL;
1197 * Put the touchpad back into absolute mode when reconnecting
1199 static int elantech_reconnect(struct psmouse *psmouse)
1201 if (elantech_detect(psmouse, 0))
1204 if (elantech_set_absolute_mode(psmouse)) {
1205 psmouse_err(psmouse,
1206 "failed to put touchpad back into absolute mode.\n");
1214 * Some hw_version 3 models go into error state when we try to set bit 3 of r10
1216 static const struct dmi_system_id no_hw_res_dmi_table[] = {
1217 #if defined(CONFIG_DMI) && defined(CONFIG_X86)
1219 /* Gigabyte U2442 */
1221 DMI_MATCH(DMI_SYS_VENDOR, "GIGABYTE"),
1222 DMI_MATCH(DMI_PRODUCT_NAME, "U2442"),
1230 * determine hardware version and set some properties according to it.
1232 static int elantech_set_properties(struct elantech_data *etd)
1234 /* This represents the version of IC body. */
1235 int ver = (etd->fw_version & 0x0f0000) >> 16;
1237 /* Early version of Elan touchpads doesn't obey the rule. */
1238 if (etd->fw_version < 0x020030 || etd->fw_version == 0x020600)
1239 etd->hw_version = 1;
1244 etd->hw_version = 2;
1247 etd->hw_version = 3;
1250 etd->hw_version = 4;
1258 * Turn on packet checking by default.
1260 etd->paritycheck = 1;
1263 * This firmware suffers from misreporting coordinates when
1264 * a touch action starts causing the mouse cursor or scrolled page
1265 * to jump. Enable a workaround.
1268 (etd->fw_version == 0x020022 || etd->fw_version == 0x020600);
1270 if (etd->hw_version > 1) {
1271 /* For now show extra debug information */
1274 if (etd->fw_version >= 0x020800)
1275 etd->reports_pressure = true;
1278 /* Enable real hardware resolution on hw_version 3 ? */
1279 etd->set_hw_resolution = !dmi_check_system(no_hw_res_dmi_table);
1285 * Initialize the touchpad and create sysfs entries
1287 int elantech_init(struct psmouse *psmouse)
1289 struct elantech_data *etd;
1291 unsigned char param[3];
1293 psmouse->private = etd = kzalloc(sizeof(struct elantech_data), GFP_KERNEL);
1298 for (i = 1; i < 256; i++)
1299 etd->parity[i] = etd->parity[i & (i - 1)] ^ 1;
1302 * Do the version query again so we can store the result
1304 if (synaptics_send_cmd(psmouse, ETP_FW_VERSION_QUERY, param)) {
1305 psmouse_err(psmouse, "failed to query firmware version.\n");
1308 etd->fw_version = (param[0] << 16) | (param[1] << 8) | param[2];
1310 if (elantech_set_properties(etd)) {
1311 psmouse_err(psmouse, "unknown hardware version, aborting...\n");
1314 psmouse_info(psmouse,
1315 "assuming hardware version %d (with firmware version 0x%02x%02x%02x)\n",
1316 etd->hw_version, param[0], param[1], param[2]);
1318 if (synaptics_send_cmd(psmouse, ETP_CAPABILITIES_QUERY,
1319 etd->capabilities)) {
1320 psmouse_err(psmouse, "failed to query capabilities.\n");
1323 psmouse_info(psmouse,
1324 "Synaptics capabilities query result 0x%02x, 0x%02x, 0x%02x.\n",
1325 etd->capabilities[0], etd->capabilities[1],
1326 etd->capabilities[2]);
1328 if (elantech_set_absolute_mode(psmouse)) {
1329 psmouse_err(psmouse,
1330 "failed to put touchpad into absolute mode.\n");
1334 if (elantech_set_input_params(psmouse)) {
1335 psmouse_err(psmouse, "failed to query touchpad range.\n");
1339 error = sysfs_create_group(&psmouse->ps2dev.serio->dev.kobj,
1340 &elantech_attr_group);
1342 psmouse_err(psmouse,
1343 "failed to create sysfs attributes, error: %d.\n",
1348 psmouse->protocol_handler = elantech_process_byte;
1349 psmouse->disconnect = elantech_disconnect;
1350 psmouse->reconnect = elantech_reconnect;
1351 psmouse->pktsize = etd->hw_version > 1 ? 6 : 4;