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 #define pr_fmt(fmt) KBUILD_BASENAME ": " fmt
15 #include <linux/delay.h>
16 #include <linux/slab.h>
17 #include <linux/module.h>
18 #include <linux/input.h>
19 #include <linux/input/mt.h>
20 #include <linux/serio.h>
21 #include <linux/libps2.h>
25 #define elantech_debug(fmt, ...) \
28 printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__); \
31 static bool force_elantech;
32 module_param_named(force_elantech, force_elantech, bool, 0644);
33 MODULE_PARM_DESC(force_elantech, "Force the Elantech PS/2 protocol extension to be used, 1 = enabled, 0 = disabled (default).");
36 * Send a Synaptics style sliced query command
38 static int synaptics_send_cmd(struct psmouse *psmouse, unsigned char c,
41 if (psmouse_sliced_command(psmouse, c) ||
42 ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_GETINFO)) {
43 pr_err("synaptics_send_cmd query 0x%02x failed.\n", c);
51 * A retrying version of ps2_command
53 static int elantech_ps2_command(struct psmouse *psmouse,
54 unsigned char *param, int command)
56 struct ps2dev *ps2dev = &psmouse->ps2dev;
57 struct elantech_data *etd = psmouse->private;
59 int tries = ETP_PS2_COMMAND_TRIES;
62 rc = ps2_command(ps2dev, param, command);
66 elantech_debug("retrying ps2 command 0x%02x (%d).\n",
68 msleep(ETP_PS2_COMMAND_DELAY);
72 pr_err("ps2 command 0x%02x failed.\n", command);
78 * Send an Elantech style special command to read a value from a register
80 static int elantech_read_reg(struct psmouse *psmouse, unsigned char reg,
83 struct elantech_data *etd = psmouse->private;
84 unsigned char param[3];
87 if (reg < 0x07 || reg > 0x26)
90 if (reg > 0x11 && reg < 0x20)
93 switch (etd->hw_version) {
95 if (psmouse_sliced_command(psmouse, ETP_REGISTER_READ) ||
96 psmouse_sliced_command(psmouse, reg) ||
97 ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_GETINFO)) {
103 if (elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
104 elantech_ps2_command(psmouse, NULL, ETP_REGISTER_READ) ||
105 elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
106 elantech_ps2_command(psmouse, NULL, reg) ||
107 elantech_ps2_command(psmouse, param, PSMOUSE_CMD_GETINFO)) {
113 if (elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
114 elantech_ps2_command(psmouse, NULL, ETP_REGISTER_READWRITE) ||
115 elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
116 elantech_ps2_command(psmouse, NULL, reg) ||
117 elantech_ps2_command(psmouse, param, PSMOUSE_CMD_GETINFO)) {
124 pr_err("failed to read register 0x%02x.\n", reg);
125 else if (etd->hw_version != 4)
134 * Send an Elantech style special command to write a register with a value
136 static int elantech_write_reg(struct psmouse *psmouse, unsigned char reg,
139 struct elantech_data *etd = psmouse->private;
142 if (reg < 0x07 || reg > 0x26)
145 if (reg > 0x11 && reg < 0x20)
148 switch (etd->hw_version) {
150 if (psmouse_sliced_command(psmouse, ETP_REGISTER_WRITE) ||
151 psmouse_sliced_command(psmouse, reg) ||
152 psmouse_sliced_command(psmouse, val) ||
153 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11)) {
159 if (elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
160 elantech_ps2_command(psmouse, NULL, ETP_REGISTER_WRITE) ||
161 elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
162 elantech_ps2_command(psmouse, NULL, reg) ||
163 elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
164 elantech_ps2_command(psmouse, NULL, val) ||
165 elantech_ps2_command(psmouse, NULL, PSMOUSE_CMD_SETSCALE11)) {
171 if (elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
172 elantech_ps2_command(psmouse, NULL, ETP_REGISTER_READWRITE) ||
173 elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
174 elantech_ps2_command(psmouse, NULL, reg) ||
175 elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
176 elantech_ps2_command(psmouse, NULL, val) ||
177 elantech_ps2_command(psmouse, NULL, PSMOUSE_CMD_SETSCALE11)) {
183 if (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, reg) ||
187 elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
188 elantech_ps2_command(psmouse, NULL, ETP_REGISTER_READWRITE) ||
189 elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
190 elantech_ps2_command(psmouse, NULL, val) ||
191 elantech_ps2_command(psmouse, NULL, PSMOUSE_CMD_SETSCALE11)) {
198 pr_err("failed to write register 0x%02x with value 0x%02x.\n",
205 * Dump a complete mouse movement packet to the syslog
207 static void elantech_packet_dump(unsigned char *packet, int size)
211 printk(KERN_DEBUG pr_fmt("PS/2 packet ["));
212 for (i = 0; i < size; i++)
213 printk("%s0x%02x ", (i) ? ", " : " ", packet[i]);
218 * Interpret complete data packets and report absolute mode input events for
219 * hardware version 1. (4 byte packets)
221 static void elantech_report_absolute_v1(struct psmouse *psmouse)
223 struct input_dev *dev = psmouse->dev;
224 struct elantech_data *etd = psmouse->private;
225 unsigned char *packet = psmouse->packet;
228 if (etd->fw_version < 0x020000) {
230 * byte 0: D U p1 p2 1 p3 R L
231 * byte 1: f 0 th tw x9 x8 y9 y8
233 fingers = ((packet[1] & 0x80) >> 7) +
234 ((packet[1] & 0x30) >> 4);
237 * byte 0: n1 n0 p2 p1 1 p3 R L
238 * byte 1: 0 0 0 0 x9 x8 y9 y8
240 fingers = (packet[0] & 0xc0) >> 6;
243 if (etd->jumpy_cursor) {
245 etd->single_finger_reports = 0;
246 } else if (etd->single_finger_reports < 2) {
247 /* Discard first 2 reports of one finger, bogus */
248 etd->single_finger_reports++;
249 elantech_debug("discarding packet\n");
254 input_report_key(dev, BTN_TOUCH, fingers != 0);
257 * byte 2: x7 x6 x5 x4 x3 x2 x1 x0
258 * byte 3: y7 y6 y5 y4 y3 y2 y1 y0
261 input_report_abs(dev, ABS_X,
262 ((packet[1] & 0x0c) << 6) | packet[2]);
263 input_report_abs(dev, ABS_Y,
264 etd->y_max - (((packet[1] & 0x03) << 8) | packet[3]));
267 input_report_key(dev, BTN_TOOL_FINGER, fingers == 1);
268 input_report_key(dev, BTN_TOOL_DOUBLETAP, fingers == 2);
269 input_report_key(dev, BTN_TOOL_TRIPLETAP, fingers == 3);
270 input_report_key(dev, BTN_LEFT, packet[0] & 0x01);
271 input_report_key(dev, BTN_RIGHT, packet[0] & 0x02);
273 if (etd->fw_version < 0x020000 &&
274 (etd->capabilities[0] & ETP_CAP_HAS_ROCKER)) {
276 input_report_key(dev, BTN_FORWARD, packet[0] & 0x40);
278 input_report_key(dev, BTN_BACK, packet[0] & 0x80);
284 static void elantech_set_slot(struct input_dev *dev, int slot, bool active,
285 unsigned int x, unsigned int y)
287 input_mt_slot(dev, slot);
288 input_mt_report_slot_state(dev, MT_TOOL_FINGER, active);
290 input_report_abs(dev, ABS_MT_POSITION_X, x);
291 input_report_abs(dev, ABS_MT_POSITION_Y, y);
295 /* x1 < x2 and y1 < y2 when two fingers, x = y = 0 when not pressed */
296 static void elantech_report_semi_mt_data(struct input_dev *dev,
297 unsigned int num_fingers,
298 unsigned int x1, unsigned int y1,
299 unsigned int x2, unsigned int y2)
301 elantech_set_slot(dev, 0, num_fingers != 0, x1, y1);
302 elantech_set_slot(dev, 1, num_fingers == 2, x2, y2);
306 * Interpret complete data packets and report absolute mode input events for
307 * hardware version 2. (6 byte packets)
309 static void elantech_report_absolute_v2(struct psmouse *psmouse)
311 struct elantech_data *etd = psmouse->private;
312 struct input_dev *dev = psmouse->dev;
313 unsigned char *packet = psmouse->packet;
314 unsigned int fingers, x1 = 0, y1 = 0, x2 = 0, y2 = 0;
315 unsigned int width = 0, pres = 0;
317 /* byte 0: n1 n0 . . . . R L */
318 fingers = (packet[0] & 0xc0) >> 6;
323 * Same as one finger, except report of more than 3 fingers:
324 * byte 3: n4 . w1 w0 . . . .
326 if (packet[3] & 0x80)
328 /* pass through... */
331 * byte 1: . . . . x11 x10 x9 x8
332 * byte 2: x7 x6 x5 x4 x4 x2 x1 x0
334 x1 = ((packet[1] & 0x0f) << 8) | packet[2];
336 * byte 4: . . . . y11 y10 y9 y8
337 * byte 5: y7 y6 y5 y4 y3 y2 y1 y0
339 y1 = etd->y_max - (((packet[4] & 0x0f) << 8) | packet[5]);
341 pres = (packet[1] & 0xf0) | ((packet[4] & 0xf0) >> 4);
342 width = ((packet[0] & 0x30) >> 2) | ((packet[3] & 0x30) >> 4);
347 * The coordinate of each finger is reported separately
348 * with a lower resolution for two finger touches:
349 * byte 0: . . ay8 ax8 . . . .
350 * byte 1: ax7 ax6 ax5 ax4 ax3 ax2 ax1 ax0
352 x1 = (((packet[0] & 0x10) << 4) | packet[1]) << 2;
353 /* byte 2: ay7 ay6 ay5 ay4 ay3 ay2 ay1 ay0 */
355 ((((packet[0] & 0x20) << 3) | packet[2]) << 2);
357 * byte 3: . . by8 bx8 . . . .
358 * byte 4: bx7 bx6 bx5 bx4 bx3 bx2 bx1 bx0
360 x2 = (((packet[3] & 0x10) << 4) | packet[4]) << 2;
361 /* byte 5: by7 by8 by5 by4 by3 by2 by1 by0 */
363 ((((packet[3] & 0x20) << 3) | packet[5]) << 2);
365 /* Unknown so just report sensible values */
371 input_report_key(dev, BTN_TOUCH, fingers != 0);
373 input_report_abs(dev, ABS_X, x1);
374 input_report_abs(dev, ABS_Y, y1);
376 elantech_report_semi_mt_data(dev, fingers, x1, y1, x2, y2);
377 input_report_key(dev, BTN_TOOL_FINGER, fingers == 1);
378 input_report_key(dev, BTN_TOOL_DOUBLETAP, fingers == 2);
379 input_report_key(dev, BTN_TOOL_TRIPLETAP, fingers == 3);
380 input_report_key(dev, BTN_TOOL_QUADTAP, fingers == 4);
381 input_report_key(dev, BTN_LEFT, packet[0] & 0x01);
382 input_report_key(dev, BTN_RIGHT, packet[0] & 0x02);
383 if (etd->reports_pressure) {
384 input_report_abs(dev, ABS_PRESSURE, pres);
385 input_report_abs(dev, ABS_TOOL_WIDTH, width);
392 * Interpret complete data packets and report absolute mode input events for
393 * hardware version 3. (12 byte packets for two fingers)
395 static void elantech_report_absolute_v3(struct psmouse *psmouse,
398 struct input_dev *dev = psmouse->dev;
399 struct elantech_data *etd = psmouse->private;
400 unsigned char *packet = psmouse->packet;
401 unsigned int fingers = 0, x1 = 0, y1 = 0, x2 = 0, y2 = 0;
402 unsigned int width = 0, pres = 0;
404 /* byte 0: n1 n0 . . . . R L */
405 fingers = (packet[0] & 0xc0) >> 6;
411 * byte 1: . . . . x11 x10 x9 x8
412 * byte 2: x7 x6 x5 x4 x4 x2 x1 x0
414 x1 = ((packet[1] & 0x0f) << 8) | packet[2];
416 * byte 4: . . . . y11 y10 y9 y8
417 * byte 5: y7 y6 y5 y4 y3 y2 y1 y0
419 y1 = etd->y_max - (((packet[4] & 0x0f) << 8) | packet[5]);
423 if (packet_type == PACKET_V3_HEAD) {
425 * byte 1: . . . . ax11 ax10 ax9 ax8
426 * byte 2: ax7 ax6 ax5 ax4 ax3 ax2 ax1 ax0
428 etd->mt[0].x = ((packet[1] & 0x0f) << 8) | packet[2];
430 * byte 4: . . . . ay11 ay10 ay9 ay8
431 * byte 5: ay7 ay6 ay5 ay4 ay3 ay2 ay1 ay0
433 etd->mt[0].y = etd->y_max -
434 (((packet[4] & 0x0f) << 8) | packet[5]);
436 * wait for next packet
441 /* packet_type == PACKET_V3_TAIL */
444 x2 = ((packet[1] & 0x0f) << 8) | packet[2];
445 y2 = etd->y_max - (((packet[4] & 0x0f) << 8) | packet[5]);
449 pres = (packet[1] & 0xf0) | ((packet[4] & 0xf0) >> 4);
450 width = ((packet[0] & 0x30) >> 2) | ((packet[3] & 0x30) >> 4);
452 input_report_key(dev, BTN_TOUCH, fingers != 0);
454 input_report_abs(dev, ABS_X, x1);
455 input_report_abs(dev, ABS_Y, y1);
457 elantech_report_semi_mt_data(dev, fingers, x1, y1, x2, y2);
458 input_report_key(dev, BTN_TOOL_FINGER, fingers == 1);
459 input_report_key(dev, BTN_TOOL_DOUBLETAP, fingers == 2);
460 input_report_key(dev, BTN_TOOL_TRIPLETAP, fingers == 3);
461 input_report_key(dev, BTN_LEFT, packet[0] & 0x01);
462 input_report_key(dev, BTN_RIGHT, packet[0] & 0x02);
463 input_report_abs(dev, ABS_PRESSURE, pres);
464 input_report_abs(dev, ABS_TOOL_WIDTH, width);
469 static void elantech_input_sync_v4(struct psmouse *psmouse)
471 struct input_dev *dev = psmouse->dev;
472 unsigned char *packet = psmouse->packet;
474 input_report_key(dev, BTN_LEFT, packet[0] & 0x01);
475 input_report_key(dev, BTN_RIGHT, packet[0] & 0x02);
476 input_mt_report_pointer_emulation(dev, true);
480 static void process_packet_status_v4(struct psmouse *psmouse)
482 struct input_dev *dev = psmouse->dev;
483 unsigned char *packet = psmouse->packet;
487 /* notify finger state change */
488 fingers = packet[1] & 0x1f;
489 for (i = 0; i < ETP_MAX_FINGERS; i++) {
490 if ((fingers & (1 << i)) == 0) {
491 input_mt_slot(dev, i);
492 input_mt_report_slot_state(dev, MT_TOOL_FINGER, false);
496 elantech_input_sync_v4(psmouse);
499 static void process_packet_head_v4(struct psmouse *psmouse)
501 struct input_dev *dev = psmouse->dev;
502 struct elantech_data *etd = psmouse->private;
503 unsigned char *packet = psmouse->packet;
504 int id = ((packet[3] & 0xe0) >> 5) - 1;
510 etd->mt[id].x = ((packet[1] & 0x0f) << 8) | packet[2];
511 etd->mt[id].y = etd->y_max - (((packet[4] & 0x0f) << 8) | packet[5]);
512 pres = (packet[1] & 0xf0) | ((packet[4] & 0xf0) >> 4);
513 traces = (packet[0] & 0xf0) >> 4;
515 input_mt_slot(dev, id);
516 input_mt_report_slot_state(dev, MT_TOOL_FINGER, true);
518 input_report_abs(dev, ABS_MT_POSITION_X, etd->mt[id].x);
519 input_report_abs(dev, ABS_MT_POSITION_Y, etd->mt[id].y);
520 input_report_abs(dev, ABS_MT_PRESSURE, pres);
521 input_report_abs(dev, ABS_MT_TOUCH_MAJOR, traces * etd->width);
522 /* report this for backwards compatibility */
523 input_report_abs(dev, ABS_TOOL_WIDTH, traces);
525 elantech_input_sync_v4(psmouse);
528 static void process_packet_motion_v4(struct psmouse *psmouse)
530 struct input_dev *dev = psmouse->dev;
531 struct elantech_data *etd = psmouse->private;
532 unsigned char *packet = psmouse->packet;
533 int weight, delta_x1 = 0, delta_y1 = 0, delta_x2 = 0, delta_y2 = 0;
536 id = ((packet[0] & 0xe0) >> 5) - 1;
540 sid = ((packet[3] & 0xe0) >> 5) - 1;
541 weight = (packet[0] & 0x10) ? ETP_WEIGHT_VALUE : 1;
543 * Motion packets give us the delta of x, y values of specific fingers,
544 * but in two's complement. Let the compiler do the conversion for us.
545 * Also _enlarge_ the numbers to int, in case of overflow.
547 delta_x1 = (signed char)packet[1];
548 delta_y1 = (signed char)packet[2];
549 delta_x2 = (signed char)packet[4];
550 delta_y2 = (signed char)packet[5];
552 etd->mt[id].x += delta_x1 * weight;
553 etd->mt[id].y -= delta_y1 * weight;
554 input_mt_slot(dev, id);
555 input_report_abs(dev, ABS_MT_POSITION_X, etd->mt[id].x);
556 input_report_abs(dev, ABS_MT_POSITION_Y, etd->mt[id].y);
559 etd->mt[sid].x += delta_x2 * weight;
560 etd->mt[sid].y -= delta_y2 * weight;
561 input_mt_slot(dev, sid);
562 input_report_abs(dev, ABS_MT_POSITION_X, etd->mt[sid].x);
563 input_report_abs(dev, ABS_MT_POSITION_Y, etd->mt[sid].y);
566 elantech_input_sync_v4(psmouse);
569 static void elantech_report_absolute_v4(struct psmouse *psmouse,
572 switch (packet_type) {
573 case PACKET_V4_STATUS:
574 process_packet_status_v4(psmouse);
578 process_packet_head_v4(psmouse);
581 case PACKET_V4_MOTION:
582 process_packet_motion_v4(psmouse);
587 /* impossible to get here */
592 static int elantech_packet_check_v1(struct psmouse *psmouse)
594 struct elantech_data *etd = psmouse->private;
595 unsigned char *packet = psmouse->packet;
596 unsigned char p1, p2, p3;
598 /* Parity bits are placed differently */
599 if (etd->fw_version < 0x020000) {
600 /* byte 0: D U p1 p2 1 p3 R L */
601 p1 = (packet[0] & 0x20) >> 5;
602 p2 = (packet[0] & 0x10) >> 4;
604 /* byte 0: n1 n0 p2 p1 1 p3 R L */
605 p1 = (packet[0] & 0x10) >> 4;
606 p2 = (packet[0] & 0x20) >> 5;
609 p3 = (packet[0] & 0x04) >> 2;
611 return etd->parity[packet[1]] == p1 &&
612 etd->parity[packet[2]] == p2 &&
613 etd->parity[packet[3]] == p3;
616 static int elantech_packet_check_v2(struct psmouse *psmouse)
618 struct elantech_data *etd = psmouse->private;
619 unsigned char *packet = psmouse->packet;
622 * V2 hardware has two flavors. Older ones that do not report pressure,
623 * and newer ones that reports pressure and width. With newer ones, all
624 * packets (1, 2, 3 finger touch) have the same constant bits. With
625 * older ones, 1/3 finger touch packets and 2 finger touch packets
626 * have different constant bits.
627 * With all three cases, if the constant bits are not exactly what I
628 * expected, I consider them invalid.
630 if (etd->reports_pressure)
631 return (packet[0] & 0x0c) == 0x04 &&
632 (packet[3] & 0x0f) == 0x02;
634 if ((packet[0] & 0xc0) == 0x80)
635 return (packet[0] & 0x0c) == 0x0c &&
636 (packet[3] & 0x0e) == 0x08;
638 return (packet[0] & 0x3c) == 0x3c &&
639 (packet[1] & 0xf0) == 0x00 &&
640 (packet[3] & 0x3e) == 0x38 &&
641 (packet[4] & 0xf0) == 0x00;
645 * We check the constant bits to determine what packet type we get,
646 * so packet checking is mandatory for v3 and later hardware.
648 static int elantech_packet_check_v3(struct psmouse *psmouse)
650 const u8 debounce_packet[] = { 0xc4, 0xff, 0xff, 0x02, 0xff, 0xff };
651 unsigned char *packet = psmouse->packet;
654 * check debounce first, it has the same signature in byte 0
655 * and byte 3 as PACKET_V3_HEAD.
657 if (!memcmp(packet, debounce_packet, sizeof(debounce_packet)))
658 return PACKET_DEBOUNCE;
660 if ((packet[0] & 0x0c) == 0x04 && (packet[3] & 0xcf) == 0x02)
661 return PACKET_V3_HEAD;
663 if ((packet[0] & 0x0c) == 0x0c && (packet[3] & 0xce) == 0x0c)
664 return PACKET_V3_TAIL;
666 return PACKET_UNKNOWN;
669 static int elantech_packet_check_v4(struct psmouse *psmouse)
671 unsigned char *packet = psmouse->packet;
673 if ((packet[0] & 0x0c) == 0x04 &&
674 (packet[3] & 0x1f) == 0x11)
675 return PACKET_V4_HEAD;
677 if ((packet[0] & 0x0c) == 0x04 &&
678 (packet[3] & 0x1f) == 0x12)
679 return PACKET_V4_MOTION;
681 if ((packet[0] & 0x0c) == 0x04 &&
682 (packet[3] & 0x1f) == 0x10)
683 return PACKET_V4_STATUS;
685 return PACKET_UNKNOWN;
689 * Process byte stream from mouse and handle complete packets
691 static psmouse_ret_t elantech_process_byte(struct psmouse *psmouse)
693 struct elantech_data *etd = psmouse->private;
696 if (psmouse->pktcnt < psmouse->pktsize)
697 return PSMOUSE_GOOD_DATA;
700 elantech_packet_dump(psmouse->packet, psmouse->pktsize);
702 switch (etd->hw_version) {
704 if (etd->paritycheck && !elantech_packet_check_v1(psmouse))
705 return PSMOUSE_BAD_DATA;
707 elantech_report_absolute_v1(psmouse);
711 if (etd->paritycheck && !elantech_packet_check_v2(psmouse))
712 return PSMOUSE_BAD_DATA;
714 elantech_report_absolute_v2(psmouse);
718 packet_type = elantech_packet_check_v3(psmouse);
719 /* ignore debounce */
720 if (packet_type == PACKET_DEBOUNCE)
721 return PSMOUSE_FULL_PACKET;
723 if (packet_type == PACKET_UNKNOWN)
724 return PSMOUSE_BAD_DATA;
726 elantech_report_absolute_v3(psmouse, packet_type);
730 packet_type = elantech_packet_check_v4(psmouse);
731 if (packet_type == PACKET_UNKNOWN)
732 return PSMOUSE_BAD_DATA;
734 elantech_report_absolute_v4(psmouse, packet_type);
738 return PSMOUSE_FULL_PACKET;
742 * Put the touchpad into absolute mode
744 static int elantech_set_absolute_mode(struct psmouse *psmouse)
746 struct elantech_data *etd = psmouse->private;
748 int tries = ETP_READ_BACK_TRIES;
751 switch (etd->hw_version) {
755 if (elantech_write_reg(psmouse, 0x10, etd->reg_10) ||
756 elantech_write_reg(psmouse, 0x11, etd->reg_11)) {
762 /* Windows driver values */
764 etd->reg_11 = 0x88; /* 0x8a */
765 etd->reg_21 = 0x60; /* 0x00 */
766 if (elantech_write_reg(psmouse, 0x10, etd->reg_10) ||
767 elantech_write_reg(psmouse, 0x11, etd->reg_11) ||
768 elantech_write_reg(psmouse, 0x21, etd->reg_21)) {
775 if (elantech_write_reg(psmouse, 0x10, etd->reg_10))
782 if (elantech_write_reg(psmouse, 0x07, etd->reg_07))
785 goto skip_readback_reg_10; /* v4 has no reg 0x10 to read */
790 * Read back reg 0x10. For hardware version 1 we must make
791 * sure the absolute mode bit is set. For hardware version 2
792 * the touchpad is probably initalising and not ready until
793 * we read back the value we just wrote.
796 rc = elantech_read_reg(psmouse, 0x10, &val);
800 elantech_debug("retrying read (%d).\n", tries);
801 msleep(ETP_READ_BACK_DELAY);
805 pr_err("failed to read back register 0x10.\n");
806 } else if (etd->hw_version == 1 &&
807 !(val & ETP_R10_ABSOLUTE_MODE)) {
808 pr_err("touchpad refuses to switch to absolute mode.\n");
813 skip_readback_reg_10:
815 pr_err("failed to initialise registers.\n");
820 static int elantech_set_range(struct psmouse *psmouse,
821 unsigned int *x_min, unsigned int *y_min,
822 unsigned int *x_max, unsigned int *y_max,
825 struct elantech_data *etd = psmouse->private;
826 unsigned char param[3];
827 unsigned char traces;
830 switch (etd->hw_version) {
832 *x_min = ETP_XMIN_V1;
833 *y_min = ETP_YMIN_V1;
834 *x_max = ETP_XMAX_V1;
835 *y_max = ETP_YMAX_V1;
839 if (etd->fw_version == 0x020800 ||
840 etd->fw_version == 0x020b00 ||
841 etd->fw_version == 0x020030) {
842 *x_min = ETP_XMIN_V2;
843 *y_min = ETP_YMIN_V2;
844 *x_max = ETP_XMAX_V2;
845 *y_max = ETP_YMAX_V2;
847 i = (etd->fw_version > 0x020800 &&
848 etd->fw_version < 0x020900) ? 1 : 2;
851 *x_max = (etd->capabilities[1] - i) * 64;
852 *y_max = (etd->capabilities[2] - i) * 64;
857 if (synaptics_send_cmd(psmouse, ETP_FW_ID_QUERY, param))
860 *x_max = (0x0f & param[0]) << 8 | param[1];
861 *y_max = (0xf0 & param[0]) << 4 | param[2];
865 if (synaptics_send_cmd(psmouse, ETP_FW_ID_QUERY, param))
868 *x_max = (0x0f & param[0]) << 8 | param[1];
869 *y_max = (0xf0 & param[0]) << 4 | param[2];
870 traces = etd->capabilities[1];
871 if ((traces < 2) || (traces > *x_max))
874 *width = *x_max / (traces - 1);
882 * Set the appropriate event bits for the input subsystem
884 static int elantech_set_input_params(struct psmouse *psmouse)
886 struct input_dev *dev = psmouse->dev;
887 struct elantech_data *etd = psmouse->private;
888 unsigned int x_min = 0, y_min = 0, x_max = 0, y_max = 0, width = 0;
890 if (elantech_set_range(psmouse, &x_min, &y_min, &x_max, &y_max, &width))
893 __set_bit(EV_KEY, dev->evbit);
894 __set_bit(EV_ABS, dev->evbit);
895 __clear_bit(EV_REL, dev->evbit);
897 __set_bit(BTN_LEFT, dev->keybit);
898 __set_bit(BTN_RIGHT, dev->keybit);
900 __set_bit(BTN_TOUCH, dev->keybit);
901 __set_bit(BTN_TOOL_FINGER, dev->keybit);
902 __set_bit(BTN_TOOL_DOUBLETAP, dev->keybit);
903 __set_bit(BTN_TOOL_TRIPLETAP, dev->keybit);
905 switch (etd->hw_version) {
908 if (etd->fw_version < 0x020000 &&
909 (etd->capabilities[0] & ETP_CAP_HAS_ROCKER)) {
910 __set_bit(BTN_FORWARD, dev->keybit);
911 __set_bit(BTN_BACK, dev->keybit);
913 input_set_abs_params(dev, ABS_X, x_min, x_max, 0, 0);
914 input_set_abs_params(dev, ABS_Y, y_min, y_max, 0, 0);
918 __set_bit(BTN_TOOL_QUADTAP, dev->keybit);
919 __set_bit(INPUT_PROP_SEMI_MT, dev->propbit);
922 input_set_abs_params(dev, ABS_X, x_min, x_max, 0, 0);
923 input_set_abs_params(dev, ABS_Y, y_min, y_max, 0, 0);
924 if (etd->reports_pressure) {
925 input_set_abs_params(dev, ABS_PRESSURE, ETP_PMIN_V2,
927 input_set_abs_params(dev, ABS_TOOL_WIDTH, ETP_WMIN_V2,
930 input_mt_init_slots(dev, 2);
931 input_set_abs_params(dev, ABS_MT_POSITION_X, x_min, x_max, 0, 0);
932 input_set_abs_params(dev, ABS_MT_POSITION_Y, y_min, y_max, 0, 0);
936 __set_bit(BTN_TOOL_QUADTAP, dev->keybit);
937 /* For X to recognize me as touchpad. */
938 input_set_abs_params(dev, ABS_X, x_min, x_max, 0, 0);
939 input_set_abs_params(dev, ABS_Y, y_min, y_max, 0, 0);
941 * range of pressure and width is the same as v2,
942 * report ABS_PRESSURE, ABS_TOOL_WIDTH for compatibility.
944 input_set_abs_params(dev, ABS_PRESSURE, ETP_PMIN_V2,
946 input_set_abs_params(dev, ABS_TOOL_WIDTH, ETP_WMIN_V2,
948 /* Multitouch capable pad, up to 5 fingers. */
949 input_mt_init_slots(dev, ETP_MAX_FINGERS);
950 input_set_abs_params(dev, ABS_MT_POSITION_X, x_min, x_max, 0, 0);
951 input_set_abs_params(dev, ABS_MT_POSITION_Y, y_min, y_max, 0, 0);
952 input_set_abs_params(dev, ABS_MT_PRESSURE, ETP_PMIN_V2,
955 * The firmware reports how many trace lines the finger spans,
956 * convert to surface unit as Protocol-B requires.
958 input_set_abs_params(dev, ABS_MT_TOUCH_MAJOR, 0,
959 ETP_WMAX_V2 * width, 0, 0);
969 struct elantech_attr_data {
975 * Display a register value by reading a sysfs entry
977 static ssize_t elantech_show_int_attr(struct psmouse *psmouse, void *data,
980 struct elantech_data *etd = psmouse->private;
981 struct elantech_attr_data *attr = data;
982 unsigned char *reg = (unsigned char *) etd + attr->field_offset;
986 rc = elantech_read_reg(psmouse, attr->reg, reg);
988 return sprintf(buf, "0x%02x\n", (attr->reg && rc) ? -1 : *reg);
992 * Write a register value by writing a sysfs entry
994 static ssize_t elantech_set_int_attr(struct psmouse *psmouse,
995 void *data, const char *buf, size_t count)
997 struct elantech_data *etd = psmouse->private;
998 struct elantech_attr_data *attr = data;
999 unsigned char *reg = (unsigned char *) etd + attr->field_offset;
1000 unsigned long value;
1003 err = strict_strtoul(buf, 16, &value);
1010 /* Do we need to preserve some bits for version 2 hardware too? */
1011 if (etd->hw_version == 1) {
1012 if (attr->reg == 0x10)
1013 /* Force absolute mode always on */
1014 value |= ETP_R10_ABSOLUTE_MODE;
1015 else if (attr->reg == 0x11)
1016 /* Force 4 byte mode always on */
1017 value |= ETP_R11_4_BYTE_MODE;
1020 if (!attr->reg || elantech_write_reg(psmouse, attr->reg, value) == 0)
1026 #define ELANTECH_INT_ATTR(_name, _register) \
1027 static struct elantech_attr_data elantech_attr_##_name = { \
1028 .field_offset = offsetof(struct elantech_data, _name), \
1031 PSMOUSE_DEFINE_ATTR(_name, S_IWUSR | S_IRUGO, \
1032 &elantech_attr_##_name, \
1033 elantech_show_int_attr, \
1034 elantech_set_int_attr)
1036 ELANTECH_INT_ATTR(reg_07, 0x07);
1037 ELANTECH_INT_ATTR(reg_10, 0x10);
1038 ELANTECH_INT_ATTR(reg_11, 0x11);
1039 ELANTECH_INT_ATTR(reg_20, 0x20);
1040 ELANTECH_INT_ATTR(reg_21, 0x21);
1041 ELANTECH_INT_ATTR(reg_22, 0x22);
1042 ELANTECH_INT_ATTR(reg_23, 0x23);
1043 ELANTECH_INT_ATTR(reg_24, 0x24);
1044 ELANTECH_INT_ATTR(reg_25, 0x25);
1045 ELANTECH_INT_ATTR(reg_26, 0x26);
1046 ELANTECH_INT_ATTR(debug, 0);
1047 ELANTECH_INT_ATTR(paritycheck, 0);
1049 static struct attribute *elantech_attrs[] = {
1050 &psmouse_attr_reg_07.dattr.attr,
1051 &psmouse_attr_reg_10.dattr.attr,
1052 &psmouse_attr_reg_11.dattr.attr,
1053 &psmouse_attr_reg_20.dattr.attr,
1054 &psmouse_attr_reg_21.dattr.attr,
1055 &psmouse_attr_reg_22.dattr.attr,
1056 &psmouse_attr_reg_23.dattr.attr,
1057 &psmouse_attr_reg_24.dattr.attr,
1058 &psmouse_attr_reg_25.dattr.attr,
1059 &psmouse_attr_reg_26.dattr.attr,
1060 &psmouse_attr_debug.dattr.attr,
1061 &psmouse_attr_paritycheck.dattr.attr,
1065 static struct attribute_group elantech_attr_group = {
1066 .attrs = elantech_attrs,
1069 static bool elantech_is_signature_valid(const unsigned char *param)
1071 static const unsigned char rates[] = { 200, 100, 80, 60, 40, 20, 10 };
1080 for (i = 0; i < ARRAY_SIZE(rates); i++)
1081 if (param[2] == rates[i])
1088 * Use magic knock to detect Elantech touchpad
1090 int elantech_detect(struct psmouse *psmouse, bool set_properties)
1092 struct ps2dev *ps2dev = &psmouse->ps2dev;
1093 unsigned char param[3];
1095 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
1097 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1098 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1099 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1100 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1101 ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO)) {
1102 pr_debug("sending Elantech magic knock failed.\n");
1107 * Report this in case there are Elantech models that use a different
1108 * set of magic numbers
1110 if (param[0] != 0x3c || param[1] != 0x03 ||
1111 (param[2] != 0xc8 && param[2] != 0x00)) {
1112 pr_debug("unexpected magic knock result 0x%02x, 0x%02x, 0x%02x.\n",
1113 param[0], param[1], param[2]);
1118 * Query touchpad's firmware version and see if it reports known
1119 * value to avoid mis-detection. Logitech mice are known to respond
1120 * to Elantech magic knock and there might be more.
1122 if (synaptics_send_cmd(psmouse, ETP_FW_VERSION_QUERY, param)) {
1123 pr_debug("failed to query firmware version.\n");
1127 pr_debug("Elantech version query result 0x%02x, 0x%02x, 0x%02x.\n",
1128 param[0], param[1], param[2]);
1130 if (!elantech_is_signature_valid(param)) {
1131 if (!force_elantech) {
1132 pr_debug("Probably not a real Elantech touchpad. Aborting.\n");
1136 pr_debug("Probably not a real Elantech touchpad. Enabling anyway due to force_elantech.\n");
1139 if (set_properties) {
1140 psmouse->vendor = "Elantech";
1141 psmouse->name = "Touchpad";
1148 * Clean up sysfs entries when disconnecting
1150 static void elantech_disconnect(struct psmouse *psmouse)
1152 sysfs_remove_group(&psmouse->ps2dev.serio->dev.kobj,
1153 &elantech_attr_group);
1154 kfree(psmouse->private);
1155 psmouse->private = NULL;
1159 * Put the touchpad back into absolute mode when reconnecting
1161 static int elantech_reconnect(struct psmouse *psmouse)
1163 if (elantech_detect(psmouse, 0))
1166 if (elantech_set_absolute_mode(psmouse)) {
1167 pr_err("failed to put touchpad back into absolute mode.\n");
1175 * determine hardware version and set some properties according to it.
1177 static int elantech_set_properties(struct elantech_data *etd)
1179 int ver = (etd->fw_version & 0x0f0000) >> 16;
1181 if (etd->fw_version < 0x020030 || etd->fw_version == 0x020600)
1182 etd->hw_version = 1;
1183 else if (etd->fw_version < 0x150600)
1184 etd->hw_version = 2;
1186 etd->hw_version = 3;
1188 etd->hw_version = 4;
1193 * Turn on packet checking by default.
1195 etd->paritycheck = 1;
1198 * This firmware suffers from misreporting coordinates when
1199 * a touch action starts causing the mouse cursor or scrolled page
1200 * to jump. Enable a workaround.
1203 (etd->fw_version == 0x020022 || etd->fw_version == 0x020600);
1205 if (etd->hw_version > 1) {
1206 /* For now show extra debug information */
1209 if (etd->fw_version >= 0x020800)
1210 etd->reports_pressure = true;
1217 * Initialize the touchpad and create sysfs entries
1219 int elantech_init(struct psmouse *psmouse)
1221 struct elantech_data *etd;
1223 unsigned char param[3];
1225 psmouse->private = etd = kzalloc(sizeof(struct elantech_data), GFP_KERNEL);
1230 for (i = 1; i < 256; i++)
1231 etd->parity[i] = etd->parity[i & (i - 1)] ^ 1;
1234 * Do the version query again so we can store the result
1236 if (synaptics_send_cmd(psmouse, ETP_FW_VERSION_QUERY, param)) {
1237 pr_err("failed to query firmware version.\n");
1240 etd->fw_version = (param[0] << 16) | (param[1] << 8) | param[2];
1242 if (elantech_set_properties(etd)) {
1243 pr_err("unknown hardware version, aborting...\n");
1246 pr_info("assuming hardware version %d "
1247 "(with firmware version 0x%02x%02x%02x)\n",
1248 etd->hw_version, param[0], param[1], param[2]);
1250 if (synaptics_send_cmd(psmouse, ETP_CAPABILITIES_QUERY,
1251 etd->capabilities)) {
1252 pr_err("failed to query capabilities.\n");
1255 pr_info("Synaptics capabilities query result 0x%02x, 0x%02x, 0x%02x.\n",
1256 etd->capabilities[0], etd->capabilities[1],
1257 etd->capabilities[2]);
1259 if (elantech_set_absolute_mode(psmouse)) {
1260 pr_err("failed to put touchpad into absolute mode.\n");
1264 if (elantech_set_input_params(psmouse)) {
1265 pr_err("failed to query touchpad range.\n");
1269 error = sysfs_create_group(&psmouse->ps2dev.serio->dev.kobj,
1270 &elantech_attr_group);
1272 pr_err("failed to create sysfs attributes, error: %d.\n", error);
1276 psmouse->protocol_handler = elantech_process_byte;
1277 psmouse->disconnect = elantech_disconnect;
1278 psmouse->reconnect = elantech_reconnect;
1279 psmouse->pktsize = etd->hw_version > 1 ? 6 : 4;