Input: elantech - add v3 hardware support
[linux-2.6-block.git] / drivers / input / mouse / elantech.c
CommitLineData
2a0bd75e 1/*
3f8c0df4 2 * Elantech Touchpad driver (v6)
2a0bd75e 3 *
3f8c0df4 4 * Copyright (C) 2007-2009 Arjan Opmeer <arjan@opmeer.net>
2a0bd75e
AO
5 *
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.
9 *
10 * Trademarks are the property of their respective owners.
11 */
12
d4ae84a8
DT
13#define pr_fmt(fmt) KBUILD_BASENAME ": " fmt
14
2a0bd75e 15#include <linux/delay.h>
5a0e3ad6 16#include <linux/slab.h>
2a0bd75e
AO
17#include <linux/module.h>
18#include <linux/input.h>
89eec4d7 19#include <linux/input/mt.h>
2a0bd75e
AO
20#include <linux/serio.h>
21#include <linux/libps2.h>
22#include "psmouse.h"
23#include "elantech.h"
24
d4ae84a8
DT
25#define elantech_debug(fmt, ...) \
26 do { \
27 if (etd->debug) \
28 printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__); \
2a0bd75e
AO
29 } while (0)
30
f81bc788
FR
31static bool force_elantech;
32module_param_named(force_elantech, force_elantech, bool, 0644);
33MODULE_PARM_DESC(force_elantech, "Force the Elantech PS/2 protocol extension to be used, 1 = enabled, 0 = disabled (default).");
34
2a0bd75e
AO
35/*
36 * Send a Synaptics style sliced query command
37 */
38static int synaptics_send_cmd(struct psmouse *psmouse, unsigned char c,
39 unsigned char *param)
40{
41 if (psmouse_sliced_command(psmouse, c) ||
42 ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_GETINFO)) {
d4ae84a8 43 pr_err("synaptics_send_cmd query 0x%02x failed.\n", c);
2a0bd75e
AO
44 return -1;
45 }
46
47 return 0;
48}
49
50/*
51 * A retrying version of ps2_command
52 */
53static int elantech_ps2_command(struct psmouse *psmouse,
54 unsigned char *param, int command)
55{
56 struct ps2dev *ps2dev = &psmouse->ps2dev;
57 struct elantech_data *etd = psmouse->private;
58 int rc;
59 int tries = ETP_PS2_COMMAND_TRIES;
60
61 do {
62 rc = ps2_command(ps2dev, param, command);
63 if (rc == 0)
64 break;
65 tries--;
d4ae84a8
DT
66 elantech_debug("retrying ps2 command 0x%02x (%d).\n",
67 command, tries);
2a0bd75e
AO
68 msleep(ETP_PS2_COMMAND_DELAY);
69 } while (tries > 0);
70
71 if (rc)
d4ae84a8 72 pr_err("ps2 command 0x%02x failed.\n", command);
2a0bd75e
AO
73
74 return rc;
75}
76
77/*
78 * Send an Elantech style special command to read a value from a register
79 */
80static int elantech_read_reg(struct psmouse *psmouse, unsigned char reg,
81 unsigned char *val)
82{
83 struct elantech_data *etd = psmouse->private;
84 unsigned char param[3];
85 int rc = 0;
86
87 if (reg < 0x10 || reg > 0x26)
88 return -1;
89
90 if (reg > 0x11 && reg < 0x20)
91 return -1;
92
93 switch (etd->hw_version) {
94 case 1:
95 if (psmouse_sliced_command(psmouse, ETP_REGISTER_READ) ||
96 psmouse_sliced_command(psmouse, reg) ||
97 ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_GETINFO)) {
98 rc = -1;
99 }
100 break;
101
102 case 2:
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)) {
108 rc = -1;
109 }
110 break;
28f49616
JD
111
112 case 3:
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)) {
118 rc = -1;
119 }
120 break;
2a0bd75e
AO
121 }
122
123 if (rc)
d4ae84a8 124 pr_err("failed to read register 0x%02x.\n", reg);
2a0bd75e
AO
125 else
126 *val = param[0];
127
128 return rc;
129}
130
131/*
132 * Send an Elantech style special command to write a register with a value
133 */
134static int elantech_write_reg(struct psmouse *psmouse, unsigned char reg,
135 unsigned char val)
136{
137 struct elantech_data *etd = psmouse->private;
138 int rc = 0;
139
140 if (reg < 0x10 || reg > 0x26)
141 return -1;
142
143 if (reg > 0x11 && reg < 0x20)
144 return -1;
145
146 switch (etd->hw_version) {
147 case 1:
148 if (psmouse_sliced_command(psmouse, ETP_REGISTER_WRITE) ||
149 psmouse_sliced_command(psmouse, reg) ||
150 psmouse_sliced_command(psmouse, val) ||
151 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11)) {
152 rc = -1;
153 }
154 break;
155
156 case 2:
157 if (elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
158 elantech_ps2_command(psmouse, NULL, ETP_REGISTER_WRITE) ||
159 elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
160 elantech_ps2_command(psmouse, NULL, reg) ||
161 elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
162 elantech_ps2_command(psmouse, NULL, val) ||
163 elantech_ps2_command(psmouse, NULL, PSMOUSE_CMD_SETSCALE11)) {
164 rc = -1;
165 }
166 break;
28f49616
JD
167
168 case 3:
169 if (elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
170 elantech_ps2_command(psmouse, NULL, ETP_REGISTER_READWRITE) ||
171 elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
172 elantech_ps2_command(psmouse, NULL, reg) ||
173 elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
174 elantech_ps2_command(psmouse, NULL, val) ||
175 elantech_ps2_command(psmouse, NULL, PSMOUSE_CMD_SETSCALE11)) {
176 rc = -1;
177 }
178 break;
2a0bd75e
AO
179 }
180
181 if (rc)
d4ae84a8 182 pr_err("failed to write register 0x%02x with value 0x%02x.\n",
2a0bd75e
AO
183 reg, val);
184
185 return rc;
186}
187
188/*
189 * Dump a complete mouse movement packet to the syslog
190 */
191static void elantech_packet_dump(unsigned char *packet, int size)
192{
193 int i;
194
d4ae84a8 195 printk(KERN_DEBUG pr_fmt("PS/2 packet ["));
2a0bd75e
AO
196 for (i = 0; i < size; i++)
197 printk("%s0x%02x ", (i) ? ", " : " ", packet[i]);
198 printk("]\n");
199}
200
201/*
202 * Interpret complete data packets and report absolute mode input events for
203 * hardware version 1. (4 byte packets)
204 */
205static void elantech_report_absolute_v1(struct psmouse *psmouse)
206{
207 struct input_dev *dev = psmouse->dev;
208 struct elantech_data *etd = psmouse->private;
209 unsigned char *packet = psmouse->packet;
210 int fingers;
211
504e8bee 212 if (etd->fw_version < 0x020000) {
e938fbfd
FR
213 /*
214 * byte 0: D U p1 p2 1 p3 R L
215 * byte 1: f 0 th tw x9 x8 y9 y8
216 */
2a0bd75e
AO
217 fingers = ((packet[1] & 0x80) >> 7) +
218 ((packet[1] & 0x30) >> 4);
219 } else {
e938fbfd
FR
220 /*
221 * byte 0: n1 n0 p2 p1 1 p3 R L
222 * byte 1: 0 0 0 0 x9 x8 y9 y8
223 */
2a0bd75e
AO
224 fingers = (packet[0] & 0xc0) >> 6;
225 }
226
3f8c0df4 227 if (etd->jumpy_cursor) {
7f29f17b
ÉP
228 if (fingers != 1) {
229 etd->single_finger_reports = 0;
230 } else if (etd->single_finger_reports < 2) {
231 /* Discard first 2 reports of one finger, bogus */
232 etd->single_finger_reports++;
d4ae84a8 233 elantech_debug("discarding packet\n");
7f29f17b 234 return;
3f8c0df4
AO
235 }
236 }
237
2a0bd75e
AO
238 input_report_key(dev, BTN_TOUCH, fingers != 0);
239
e938fbfd
FR
240 /*
241 * byte 2: x7 x6 x5 x4 x3 x2 x1 x0
242 * byte 3: y7 y6 y5 y4 y3 y2 y1 y0
243 */
2a0bd75e
AO
244 if (fingers) {
245 input_report_abs(dev, ABS_X,
246 ((packet[1] & 0x0c) << 6) | packet[2]);
e938fbfd 247 input_report_abs(dev, ABS_Y,
230282a7 248 etd->y_max - (((packet[1] & 0x03) << 8) | packet[3]));
2a0bd75e
AO
249 }
250
251 input_report_key(dev, BTN_TOOL_FINGER, fingers == 1);
252 input_report_key(dev, BTN_TOOL_DOUBLETAP, fingers == 2);
253 input_report_key(dev, BTN_TOOL_TRIPLETAP, fingers == 3);
254 input_report_key(dev, BTN_LEFT, packet[0] & 0x01);
255 input_report_key(dev, BTN_RIGHT, packet[0] & 0x02);
256
504e8bee 257 if (etd->fw_version < 0x020000 &&
230282a7 258 (etd->capabilities[0] & ETP_CAP_HAS_ROCKER)) {
2a0bd75e
AO
259 /* rocker up */
260 input_report_key(dev, BTN_FORWARD, packet[0] & 0x40);
261 /* rocker down */
262 input_report_key(dev, BTN_BACK, packet[0] & 0x80);
263 }
264
265 input_sync(dev);
266}
267
89eec4d7
ÉP
268static void elantech_set_slot(struct input_dev *dev, int slot, bool active,
269 unsigned int x, unsigned int y)
270{
271 input_mt_slot(dev, slot);
272 input_mt_report_slot_state(dev, MT_TOOL_FINGER, active);
273 if (active) {
274 input_report_abs(dev, ABS_MT_POSITION_X, x);
275 input_report_abs(dev, ABS_MT_POSITION_Y, y);
276 }
277}
278
279/* x1 < x2 and y1 < y2 when two fingers, x = y = 0 when not pressed */
280static void elantech_report_semi_mt_data(struct input_dev *dev,
281 unsigned int num_fingers,
282 unsigned int x1, unsigned int y1,
283 unsigned int x2, unsigned int y2)
284{
285 elantech_set_slot(dev, 0, num_fingers != 0, x1, y1);
286 elantech_set_slot(dev, 1, num_fingers == 2, x2, y2);
287}
288
2a0bd75e
AO
289/*
290 * Interpret complete data packets and report absolute mode input events for
291 * hardware version 2. (6 byte packets)
292 */
293static void elantech_report_absolute_v2(struct psmouse *psmouse)
294{
f941c705 295 struct elantech_data *etd = psmouse->private;
2a0bd75e
AO
296 struct input_dev *dev = psmouse->dev;
297 unsigned char *packet = psmouse->packet;
461a7917
JD
298 unsigned int fingers, x1 = 0, y1 = 0, x2 = 0, y2 = 0;
299 unsigned int width = 0, pres = 0;
2a0bd75e
AO
300
301 /* byte 0: n1 n0 . . . . R L */
302 fingers = (packet[0] & 0xc0) >> 6;
2a0bd75e
AO
303
304 switch (fingers) {
22462d9f
ÉP
305 case 3:
306 /*
307 * Same as one finger, except report of more than 3 fingers:
308 * byte 3: n4 . w1 w0 . . . .
309 */
310 if (packet[3] & 0x80)
311 fingers = 4;
312 /* pass through... */
2a0bd75e 313 case 1:
e938fbfd 314 /*
11559619 315 * byte 1: . . . . x11 x10 x9 x8
e938fbfd
FR
316 * byte 2: x7 x6 x5 x4 x4 x2 x1 x0
317 */
11559619 318 x1 = ((packet[1] & 0x0f) << 8) | packet[2];
e938fbfd 319 /*
11559619 320 * byte 4: . . . . y11 y10 y9 y8
e938fbfd
FR
321 * byte 5: y7 y6 y5 y4 y3 y2 y1 y0
322 */
230282a7 323 y1 = etd->y_max - (((packet[4] & 0x0f) << 8) | packet[5]);
89eec4d7 324
f941c705
ÉP
325 pres = (packet[1] & 0xf0) | ((packet[4] & 0xf0) >> 4);
326 width = ((packet[0] & 0x30) >> 2) | ((packet[3] & 0x30) >> 4);
2a0bd75e
AO
327 break;
328
329 case 2:
e938fbfd
FR
330 /*
331 * The coordinate of each finger is reported separately
332 * with a lower resolution for two finger touches:
333 * byte 0: . . ay8 ax8 . . . .
334 * byte 1: ax7 ax6 ax5 ax4 ax3 ax2 ax1 ax0
335 */
461a7917 336 x1 = (((packet[0] & 0x10) << 4) | packet[1]) << 2;
2a0bd75e 337 /* byte 2: ay7 ay6 ay5 ay4 ay3 ay2 ay1 ay0 */
230282a7 338 y1 = etd->y_max -
461a7917 339 ((((packet[0] & 0x20) << 3) | packet[2]) << 2);
e938fbfd
FR
340 /*
341 * byte 3: . . by8 bx8 . . . .
342 * byte 4: bx7 bx6 bx5 bx4 bx3 bx2 bx1 bx0
343 */
461a7917 344 x2 = (((packet[3] & 0x10) << 4) | packet[4]) << 2;
2a0bd75e 345 /* byte 5: by7 by8 by5 by4 by3 by2 by1 by0 */
230282a7 346 y2 = etd->y_max -
461a7917 347 ((((packet[3] & 0x20) << 3) | packet[5]) << 2);
f941c705
ÉP
348
349 /* Unknown so just report sensible values */
350 pres = 127;
351 width = 7;
2a0bd75e
AO
352 break;
353 }
354
461a7917
JD
355 input_report_key(dev, BTN_TOUCH, fingers != 0);
356 if (fingers != 0) {
357 input_report_abs(dev, ABS_X, x1);
358 input_report_abs(dev, ABS_Y, y1);
359 }
89eec4d7 360 elantech_report_semi_mt_data(dev, fingers, x1, y1, x2, y2);
2a0bd75e
AO
361 input_report_key(dev, BTN_TOOL_FINGER, fingers == 1);
362 input_report_key(dev, BTN_TOOL_DOUBLETAP, fingers == 2);
363 input_report_key(dev, BTN_TOOL_TRIPLETAP, fingers == 3);
22462d9f 364 input_report_key(dev, BTN_TOOL_QUADTAP, fingers == 4);
2a0bd75e
AO
365 input_report_key(dev, BTN_LEFT, packet[0] & 0x01);
366 input_report_key(dev, BTN_RIGHT, packet[0] & 0x02);
f941c705
ÉP
367 if (etd->reports_pressure) {
368 input_report_abs(dev, ABS_PRESSURE, pres);
369 input_report_abs(dev, ABS_TOOL_WIDTH, width);
370 }
2a0bd75e
AO
371
372 input_sync(dev);
373}
374
28f49616
JD
375/*
376 * Interpret complete data packets and report absolute mode input events for
377 * hardware version 3. (12 byte packets for two fingers)
378 */
379static void elantech_report_absolute_v3(struct psmouse *psmouse,
380 int packet_type)
381{
382 struct input_dev *dev = psmouse->dev;
383 struct elantech_data *etd = psmouse->private;
384 unsigned char *packet = psmouse->packet;
385 unsigned int fingers = 0, x1 = 0, y1 = 0, x2 = 0, y2 = 0;
386 unsigned int width = 0, pres = 0;
387
388 /* byte 0: n1 n0 . . . . R L */
389 fingers = (packet[0] & 0xc0) >> 6;
390
391 switch (fingers) {
392 case 3:
393 case 1:
394 /*
395 * byte 1: . . . . x11 x10 x9 x8
396 * byte 2: x7 x6 x5 x4 x4 x2 x1 x0
397 */
398 x1 = ((packet[1] & 0x0f) << 8) | packet[2];
399 /*
400 * byte 4: . . . . y11 y10 y9 y8
401 * byte 5: y7 y6 y5 y4 y3 y2 y1 y0
402 */
403 y1 = etd->y_max - (((packet[4] & 0x0f) << 8) | packet[5]);
404 break;
405
406 case 2:
407 if (packet_type == PACKET_V3_HEAD) {
408 /*
409 * byte 1: . . . . ax11 ax10 ax9 ax8
410 * byte 2: ax7 ax6 ax5 ax4 ax3 ax2 ax1 ax0
411 */
412 etd->prev_x = ((packet[1] & 0x0f) << 8) | packet[2];
413 /*
414 * byte 4: . . . . ay11 ay10 ay9 ay8
415 * byte 5: ay7 ay6 ay5 ay4 ay3 ay2 ay1 ay0
416 */
417 etd->prev_y = etd->y_max -
418 (((packet[4] & 0x0f) << 8) | packet[5]);
419 /*
420 * wait for next packet
421 */
422 return;
423 }
424
425 /* packet_type == PACKET_V3_TAIL */
426 x1 = etd->prev_x;
427 y1 = etd->prev_y;
428 x2 = ((packet[1] & 0x0f) << 8) | packet[2];
429 y2 = etd->y_max - (((packet[4] & 0x0f) << 8) | packet[5]);
430 break;
431 }
432
433 pres = (packet[1] & 0xf0) | ((packet[4] & 0xf0) >> 4);
434 width = ((packet[0] & 0x30) >> 2) | ((packet[3] & 0x30) >> 4);
435
436 input_report_key(dev, BTN_TOUCH, fingers != 0);
437 if (fingers != 0) {
438 input_report_abs(dev, ABS_X, x1);
439 input_report_abs(dev, ABS_Y, y1);
440 }
441 elantech_report_semi_mt_data(dev, fingers, x1, y1, x2, y2);
442 input_report_key(dev, BTN_TOOL_FINGER, fingers == 1);
443 input_report_key(dev, BTN_TOOL_DOUBLETAP, fingers == 2);
444 input_report_key(dev, BTN_TOOL_TRIPLETAP, fingers == 3);
445 input_report_key(dev, BTN_LEFT, packet[0] & 0x01);
446 input_report_key(dev, BTN_RIGHT, packet[0] & 0x02);
447 input_report_abs(dev, ABS_PRESSURE, pres);
448 input_report_abs(dev, ABS_TOOL_WIDTH, width);
449
450 input_sync(dev);
451}
452
7894f21b 453static int elantech_packet_check_v1(struct psmouse *psmouse)
2a0bd75e
AO
454{
455 struct elantech_data *etd = psmouse->private;
456 unsigned char *packet = psmouse->packet;
457 unsigned char p1, p2, p3;
458
459 /* Parity bits are placed differently */
504e8bee 460 if (etd->fw_version < 0x020000) {
2a0bd75e
AO
461 /* byte 0: D U p1 p2 1 p3 R L */
462 p1 = (packet[0] & 0x20) >> 5;
463 p2 = (packet[0] & 0x10) >> 4;
464 } else {
465 /* byte 0: n1 n0 p2 p1 1 p3 R L */
466 p1 = (packet[0] & 0x10) >> 4;
467 p2 = (packet[0] & 0x20) >> 5;
468 }
469
470 p3 = (packet[0] & 0x04) >> 2;
471
472 return etd->parity[packet[1]] == p1 &&
473 etd->parity[packet[2]] == p2 &&
474 etd->parity[packet[3]] == p3;
475}
476
7894f21b
JD
477static int elantech_packet_check_v2(struct psmouse *psmouse)
478{
479 struct elantech_data *etd = psmouse->private;
480 unsigned char *packet = psmouse->packet;
481
482 /*
483 * V2 hardware has two flavors. Older ones that do not report pressure,
484 * and newer ones that reports pressure and width. With newer ones, all
485 * packets (1, 2, 3 finger touch) have the same constant bits. With
486 * older ones, 1/3 finger touch packets and 2 finger touch packets
487 * have different constant bits.
488 * With all three cases, if the constant bits are not exactly what I
489 * expected, I consider them invalid.
490 */
491 if (etd->reports_pressure)
492 return (packet[0] & 0x0c) == 0x04 &&
493 (packet[3] & 0x0f) == 0x02;
494
495 if ((packet[0] & 0xc0) == 0x80)
496 return (packet[0] & 0x0c) == 0x0c &&
497 (packet[3] & 0x0e) == 0x08;
498
499 return (packet[0] & 0x3c) == 0x3c &&
500 (packet[1] & 0xf0) == 0x00 &&
501 (packet[3] & 0x3e) == 0x38 &&
502 (packet[4] & 0xf0) == 0x00;
503}
504
28f49616
JD
505/*
506 * We check the constant bits to determine what packet type we get,
507 * so packet checking is mandatory for v3 hardware.
508 */
509static int elantech_packet_check_v3(struct psmouse *psmouse)
510{
511 const u8 debounce_packet[] = { 0xc4, 0xff, 0xff, 0x02, 0xff, 0xff };
512 unsigned char *packet = psmouse->packet;
513
514 /*
515 * check debounce first, it has the same signature in byte 0
516 * and byte 3 as PACKET_V3_HEAD.
517 */
518 if (!memcmp(packet, debounce_packet, sizeof(debounce_packet)))
519 return PACKET_DEBOUNCE;
520
521 if ((packet[0] & 0x0c) == 0x04 && (packet[3] & 0xcf) == 0x02)
522 return PACKET_V3_HEAD;
523
524 if ((packet[0] & 0x0c) == 0x0c && (packet[3] & 0xce) == 0x0c)
525 return PACKET_V3_TAIL;
526
527 return PACKET_UNKNOWN;
528}
529
2a0bd75e
AO
530/*
531 * Process byte stream from mouse and handle complete packets
532 */
533static psmouse_ret_t elantech_process_byte(struct psmouse *psmouse)
534{
535 struct elantech_data *etd = psmouse->private;
28f49616 536 int packet_type;
2a0bd75e
AO
537
538 if (psmouse->pktcnt < psmouse->pktsize)
539 return PSMOUSE_GOOD_DATA;
540
541 if (etd->debug > 1)
542 elantech_packet_dump(psmouse->packet, psmouse->pktsize);
543
544 switch (etd->hw_version) {
545 case 1:
7894f21b 546 if (etd->paritycheck && !elantech_packet_check_v1(psmouse))
2a0bd75e
AO
547 return PSMOUSE_BAD_DATA;
548
549 elantech_report_absolute_v1(psmouse);
550 break;
551
552 case 2:
7894f21b
JD
553 if (etd->paritycheck && !elantech_packet_check_v2(psmouse))
554 return PSMOUSE_BAD_DATA;
555
2a0bd75e
AO
556 elantech_report_absolute_v2(psmouse);
557 break;
28f49616
JD
558
559 case 3:
560 packet_type = elantech_packet_check_v3(psmouse);
561 /* ignore debounce */
562 if (packet_type == PACKET_DEBOUNCE)
563 return PSMOUSE_FULL_PACKET;
564
565 if (packet_type == PACKET_UNKNOWN)
566 return PSMOUSE_BAD_DATA;
567
568 elantech_report_absolute_v3(psmouse, packet_type);
569 break;
2a0bd75e
AO
570 }
571
572 return PSMOUSE_FULL_PACKET;
573}
574
575/*
576 * Put the touchpad into absolute mode
577 */
578static int elantech_set_absolute_mode(struct psmouse *psmouse)
579{
580 struct elantech_data *etd = psmouse->private;
581 unsigned char val;
582 int tries = ETP_READ_BACK_TRIES;
583 int rc = 0;
584
585 switch (etd->hw_version) {
586 case 1:
587 etd->reg_10 = 0x16;
588 etd->reg_11 = 0x8f;
589 if (elantech_write_reg(psmouse, 0x10, etd->reg_10) ||
590 elantech_write_reg(psmouse, 0x11, etd->reg_11)) {
591 rc = -1;
592 }
593 break;
594
595 case 2:
596 /* Windows driver values */
597 etd->reg_10 = 0x54;
598 etd->reg_11 = 0x88; /* 0x8a */
599 etd->reg_21 = 0x60; /* 0x00 */
600 if (elantech_write_reg(psmouse, 0x10, etd->reg_10) ||
601 elantech_write_reg(psmouse, 0x11, etd->reg_11) ||
602 elantech_write_reg(psmouse, 0x21, etd->reg_21)) {
603 rc = -1;
2a0bd75e 604 }
28f49616
JD
605 break;
606
607 case 3:
608 etd->reg_10 = 0x0b;
609 if (elantech_write_reg(psmouse, 0x10, etd->reg_10))
610 rc = -1;
611
612 break;
b2546df6
AO
613 }
614
615 if (rc == 0) {
2a0bd75e 616 /*
b2546df6
AO
617 * Read back reg 0x10. For hardware version 1 we must make
618 * sure the absolute mode bit is set. For hardware version 2
619 * the touchpad is probably initalising and not ready until
620 * we read back the value we just wrote.
2a0bd75e
AO
621 */
622 do {
623 rc = elantech_read_reg(psmouse, 0x10, &val);
624 if (rc == 0)
625 break;
626 tries--;
d4ae84a8 627 elantech_debug("retrying read (%d).\n", tries);
2a0bd75e
AO
628 msleep(ETP_READ_BACK_DELAY);
629 } while (tries > 0);
b2546df6
AO
630
631 if (rc) {
d4ae84a8 632 pr_err("failed to read back register 0x10.\n");
b2546df6
AO
633 } else if (etd->hw_version == 1 &&
634 !(val & ETP_R10_ABSOLUTE_MODE)) {
d4ae84a8 635 pr_err("touchpad refuses to switch to absolute mode.\n");
b2546df6
AO
636 rc = -1;
637 }
2a0bd75e
AO
638 }
639
640 if (rc)
d4ae84a8 641 pr_err("failed to initialise registers.\n");
2a0bd75e
AO
642
643 return rc;
644}
645
28f49616
JD
646static int elantech_set_range(struct psmouse *psmouse,
647 unsigned int *x_min, unsigned int *y_min,
648 unsigned int *x_max, unsigned int *y_max)
230282a7
JD
649{
650 struct elantech_data *etd = psmouse->private;
28f49616 651 unsigned char param[3];
230282a7
JD
652 int i;
653
654 switch (etd->hw_version) {
655 case 1:
656 *x_min = ETP_XMIN_V1;
657 *y_min = ETP_YMIN_V1;
658 *x_max = ETP_XMAX_V1;
659 *y_max = ETP_YMAX_V1;
660 break;
661
662 case 2:
663 if (etd->fw_version == 0x020800 ||
664 etd->fw_version == 0x020b00 ||
665 etd->fw_version == 0x020030) {
666 *x_min = ETP_XMIN_V2;
667 *y_min = ETP_YMIN_V2;
668 *x_max = ETP_XMAX_V2;
669 *y_max = ETP_YMAX_V2;
670 } else {
671 i = (etd->fw_version > 0x020800 &&
672 etd->fw_version < 0x020900) ? 1 : 2;
673 *x_min = 0;
674 *y_min = 0;
675 *x_max = (etd->capabilities[1] - i) * 64;
676 *y_max = (etd->capabilities[2] - i) * 64;
677 }
678 break;
28f49616
JD
679
680 case 3:
681 if (synaptics_send_cmd(psmouse, ETP_FW_ID_QUERY, param))
682 return -1;
683
684 *x_max = (0x0f & param[0]) << 8 | param[1];
685 *y_max = (0xf0 & param[0]) << 4 | param[2];
686 break;
230282a7 687 }
28f49616
JD
688
689 return 0;
230282a7
JD
690}
691
2a0bd75e
AO
692/*
693 * Set the appropriate event bits for the input subsystem
694 */
28f49616 695static int elantech_set_input_params(struct psmouse *psmouse)
2a0bd75e
AO
696{
697 struct input_dev *dev = psmouse->dev;
698 struct elantech_data *etd = psmouse->private;
230282a7
JD
699 unsigned int x_min = 0, y_min = 0, x_max = 0, y_max = 0;
700
28f49616
JD
701 if (elantech_set_range(psmouse, &x_min, &y_min, &x_max, &y_max))
702 return -1;
2a0bd75e
AO
703
704 __set_bit(EV_KEY, dev->evbit);
705 __set_bit(EV_ABS, dev->evbit);
c7a1f3cc 706 __clear_bit(EV_REL, dev->evbit);
2a0bd75e
AO
707
708 __set_bit(BTN_LEFT, dev->keybit);
709 __set_bit(BTN_RIGHT, dev->keybit);
710
711 __set_bit(BTN_TOUCH, dev->keybit);
712 __set_bit(BTN_TOOL_FINGER, dev->keybit);
713 __set_bit(BTN_TOOL_DOUBLETAP, dev->keybit);
714 __set_bit(BTN_TOOL_TRIPLETAP, dev->keybit);
715
716 switch (etd->hw_version) {
717 case 1:
718 /* Rocker button */
504e8bee 719 if (etd->fw_version < 0x020000 &&
230282a7 720 (etd->capabilities[0] & ETP_CAP_HAS_ROCKER)) {
2a0bd75e
AO
721 __set_bit(BTN_FORWARD, dev->keybit);
722 __set_bit(BTN_BACK, dev->keybit);
723 }
230282a7
JD
724 input_set_abs_params(dev, ABS_X, x_min, x_max, 0, 0);
725 input_set_abs_params(dev, ABS_Y, y_min, y_max, 0, 0);
2a0bd75e
AO
726 break;
727
728 case 2:
22462d9f 729 __set_bit(BTN_TOOL_QUADTAP, dev->keybit);
28f49616
JD
730 __set_bit(INPUT_PROP_SEMI_MT, dev->propbit);
731 /* fall through */
732 case 3:
230282a7
JD
733 input_set_abs_params(dev, ABS_X, x_min, x_max, 0, 0);
734 input_set_abs_params(dev, ABS_Y, y_min, y_max, 0, 0);
f941c705
ÉP
735 if (etd->reports_pressure) {
736 input_set_abs_params(dev, ABS_PRESSURE, ETP_PMIN_V2,
737 ETP_PMAX_V2, 0, 0);
738 input_set_abs_params(dev, ABS_TOOL_WIDTH, ETP_WMIN_V2,
739 ETP_WMAX_V2, 0, 0);
740 }
89eec4d7 741 input_mt_init_slots(dev, 2);
230282a7
JD
742 input_set_abs_params(dev, ABS_MT_POSITION_X, x_min, x_max, 0, 0);
743 input_set_abs_params(dev, ABS_MT_POSITION_Y, y_min, y_max, 0, 0);
2a0bd75e
AO
744 break;
745 }
230282a7
JD
746
747 etd->y_max = y_max;
28f49616
JD
748
749 return 0;
2a0bd75e
AO
750}
751
752struct elantech_attr_data {
753 size_t field_offset;
754 unsigned char reg;
755};
756
757/*
758 * Display a register value by reading a sysfs entry
759 */
760static ssize_t elantech_show_int_attr(struct psmouse *psmouse, void *data,
761 char *buf)
762{
763 struct elantech_data *etd = psmouse->private;
764 struct elantech_attr_data *attr = data;
765 unsigned char *reg = (unsigned char *) etd + attr->field_offset;
766 int rc = 0;
767
768 if (attr->reg)
769 rc = elantech_read_reg(psmouse, attr->reg, reg);
770
771 return sprintf(buf, "0x%02x\n", (attr->reg && rc) ? -1 : *reg);
772}
773
774/*
775 * Write a register value by writing a sysfs entry
776 */
777static ssize_t elantech_set_int_attr(struct psmouse *psmouse,
778 void *data, const char *buf, size_t count)
779{
780 struct elantech_data *etd = psmouse->private;
781 struct elantech_attr_data *attr = data;
782 unsigned char *reg = (unsigned char *) etd + attr->field_offset;
783 unsigned long value;
784 int err;
785
786 err = strict_strtoul(buf, 16, &value);
787 if (err)
788 return err;
789
790 if (value > 0xff)
791 return -EINVAL;
792
793 /* Do we need to preserve some bits for version 2 hardware too? */
794 if (etd->hw_version == 1) {
795 if (attr->reg == 0x10)
796 /* Force absolute mode always on */
797 value |= ETP_R10_ABSOLUTE_MODE;
798 else if (attr->reg == 0x11)
799 /* Force 4 byte mode always on */
800 value |= ETP_R11_4_BYTE_MODE;
801 }
802
803 if (!attr->reg || elantech_write_reg(psmouse, attr->reg, value) == 0)
804 *reg = value;
805
806 return count;
807}
808
809#define ELANTECH_INT_ATTR(_name, _register) \
810 static struct elantech_attr_data elantech_attr_##_name = { \
811 .field_offset = offsetof(struct elantech_data, _name), \
812 .reg = _register, \
813 }; \
814 PSMOUSE_DEFINE_ATTR(_name, S_IWUSR | S_IRUGO, \
815 &elantech_attr_##_name, \
816 elantech_show_int_attr, \
817 elantech_set_int_attr)
818
819ELANTECH_INT_ATTR(reg_10, 0x10);
820ELANTECH_INT_ATTR(reg_11, 0x11);
821ELANTECH_INT_ATTR(reg_20, 0x20);
822ELANTECH_INT_ATTR(reg_21, 0x21);
823ELANTECH_INT_ATTR(reg_22, 0x22);
824ELANTECH_INT_ATTR(reg_23, 0x23);
825ELANTECH_INT_ATTR(reg_24, 0x24);
826ELANTECH_INT_ATTR(reg_25, 0x25);
827ELANTECH_INT_ATTR(reg_26, 0x26);
828ELANTECH_INT_ATTR(debug, 0);
829ELANTECH_INT_ATTR(paritycheck, 0);
830
831static struct attribute *elantech_attrs[] = {
832 &psmouse_attr_reg_10.dattr.attr,
833 &psmouse_attr_reg_11.dattr.attr,
834 &psmouse_attr_reg_20.dattr.attr,
835 &psmouse_attr_reg_21.dattr.attr,
836 &psmouse_attr_reg_22.dattr.attr,
837 &psmouse_attr_reg_23.dattr.attr,
838 &psmouse_attr_reg_24.dattr.attr,
839 &psmouse_attr_reg_25.dattr.attr,
840 &psmouse_attr_reg_26.dattr.attr,
841 &psmouse_attr_debug.dattr.attr,
842 &psmouse_attr_paritycheck.dattr.attr,
843 NULL
844};
845
846static struct attribute_group elantech_attr_group = {
847 .attrs = elantech_attrs,
848};
849
a083632e
DT
850static bool elantech_is_signature_valid(const unsigned char *param)
851{
852 static const unsigned char rates[] = { 200, 100, 80, 60, 40, 20, 10 };
853 int i;
854
855 if (param[0] == 0)
856 return false;
857
858 if (param[1] == 0)
859 return true;
860
861 for (i = 0; i < ARRAY_SIZE(rates); i++)
862 if (param[2] == rates[i])
863 return false;
864
865 return true;
866}
867
2a0bd75e
AO
868/*
869 * Use magic knock to detect Elantech touchpad
870 */
b7802c5c 871int elantech_detect(struct psmouse *psmouse, bool set_properties)
2a0bd75e
AO
872{
873 struct ps2dev *ps2dev = &psmouse->ps2dev;
874 unsigned char param[3];
875
876 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
877
878 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
879 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
880 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
881 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
882 ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO)) {
d4ae84a8 883 pr_debug("sending Elantech magic knock failed.\n");
2a0bd75e
AO
884 return -1;
885 }
886
887 /*
888 * Report this in case there are Elantech models that use a different
889 * set of magic numbers
890 */
28f49616
JD
891 if (param[0] != 0x3c || param[1] != 0x03 ||
892 (param[2] != 0xc8 && param[2] != 0x00)) {
d4ae84a8 893 pr_debug("unexpected magic knock result 0x%02x, 0x%02x, 0x%02x.\n",
9ab7b25e
AO
894 param[0], param[1], param[2]);
895 return -1;
896 }
897
898 /*
899 * Query touchpad's firmware version and see if it reports known
900 * value to avoid mis-detection. Logitech mice are known to respond
901 * to Elantech magic knock and there might be more.
902 */
903 if (synaptics_send_cmd(psmouse, ETP_FW_VERSION_QUERY, param)) {
d4ae84a8 904 pr_debug("failed to query firmware version.\n");
9ab7b25e
AO
905 return -1;
906 }
907
d4ae84a8 908 pr_debug("Elantech version query result 0x%02x, 0x%02x, 0x%02x.\n",
9ab7b25e
AO
909 param[0], param[1], param[2]);
910
a083632e 911 if (!elantech_is_signature_valid(param)) {
f81bc788 912 if (!force_elantech) {
d4ae84a8 913 pr_debug("Probably not a real Elantech touchpad. Aborting.\n");
f81bc788
FR
914 return -1;
915 }
916
d4ae84a8 917 pr_debug("Probably not a real Elantech touchpad. Enabling anyway due to force_elantech.\n");
2a0bd75e
AO
918 }
919
920 if (set_properties) {
921 psmouse->vendor = "Elantech";
922 psmouse->name = "Touchpad";
923 }
924
925 return 0;
926}
927
928/*
929 * Clean up sysfs entries when disconnecting
930 */
931static void elantech_disconnect(struct psmouse *psmouse)
932{
933 sysfs_remove_group(&psmouse->ps2dev.serio->dev.kobj,
934 &elantech_attr_group);
935 kfree(psmouse->private);
936 psmouse->private = NULL;
937}
938
939/*
940 * Put the touchpad back into absolute mode when reconnecting
941 */
942static int elantech_reconnect(struct psmouse *psmouse)
943{
944 if (elantech_detect(psmouse, 0))
945 return -1;
946
947 if (elantech_set_absolute_mode(psmouse)) {
d4ae84a8 948 pr_err("failed to put touchpad back into absolute mode.\n");
2a0bd75e
AO
949 return -1;
950 }
951
952 return 0;
953}
954
3c8bbb95
JD
955/*
956 * determine hardware version and set some properties according to it.
957 */
28f49616 958static int elantech_set_properties(struct elantech_data *etd)
3c8bbb95 959{
3c8bbb95
JD
960 if (etd->fw_version < 0x020030 || etd->fw_version == 0x020600)
961 etd->hw_version = 1;
28f49616 962 else if (etd->fw_version < 0x150600)
3c8bbb95 963 etd->hw_version = 2;
28f49616
JD
964 else if ((etd->fw_version & 0x0f0000) >> 16 == 5)
965 etd->hw_version = 3;
966 else
967 return -1;
3c8bbb95
JD
968
969 /*
970 * Turn on packet checking by default.
971 */
972 etd->paritycheck = 1;
973
974 /*
975 * This firmware suffers from misreporting coordinates when
976 * a touch action starts causing the mouse cursor or scrolled page
977 * to jump. Enable a workaround.
978 */
979 etd->jumpy_cursor =
980 (etd->fw_version == 0x020022 || etd->fw_version == 0x020600);
981
28f49616 982 if (etd->hw_version > 1) {
3c8bbb95
JD
983 /* For now show extra debug information */
984 etd->debug = 1;
985
986 if (etd->fw_version >= 0x020800)
987 etd->reports_pressure = true;
988 }
28f49616
JD
989
990 return 0;
3c8bbb95
JD
991}
992
2a0bd75e
AO
993/*
994 * Initialize the touchpad and create sysfs entries
995 */
996int elantech_init(struct psmouse *psmouse)
997{
998 struct elantech_data *etd;
999 int i, error;
1000 unsigned char param[3];
1001
9ab7b25e 1002 psmouse->private = etd = kzalloc(sizeof(struct elantech_data), GFP_KERNEL);
2a0bd75e 1003 if (!etd)
6792cbbb 1004 return -ENOMEM;
2a0bd75e
AO
1005
1006 etd->parity[0] = 1;
1007 for (i = 1; i < 256; i++)
1008 etd->parity[i] = etd->parity[i & (i - 1)] ^ 1;
1009
1010 /*
9ab7b25e 1011 * Do the version query again so we can store the result
2a0bd75e
AO
1012 */
1013 if (synaptics_send_cmd(psmouse, ETP_FW_VERSION_QUERY, param)) {
d4ae84a8 1014 pr_err("failed to query firmware version.\n");
2a0bd75e
AO
1015 goto init_fail;
1016 }
504e8bee 1017 etd->fw_version = (param[0] << 16) | (param[1] << 8) | param[2];
28f49616
JD
1018
1019 if (elantech_set_properties(etd)) {
1020 pr_err("unknown hardware version, aborting...\n");
1021 goto init_fail;
1022 }
3c8bbb95
JD
1023 pr_info("assuming hardware version %d "
1024 "(with firmware version 0x%02x%02x%02x)\n",
504e8bee 1025 etd->hw_version, param[0], param[1], param[2]);
2a0bd75e 1026
230282a7
JD
1027 if (synaptics_send_cmd(psmouse, ETP_CAPABILITIES_QUERY,
1028 etd->capabilities)) {
d4ae84a8 1029 pr_err("failed to query capabilities.\n");
2a0bd75e
AO
1030 goto init_fail;
1031 }
d4ae84a8 1032 pr_info("Synaptics capabilities query result 0x%02x, 0x%02x, 0x%02x.\n",
230282a7
JD
1033 etd->capabilities[0], etd->capabilities[1],
1034 etd->capabilities[2]);
2a0bd75e
AO
1035
1036 if (elantech_set_absolute_mode(psmouse)) {
d4ae84a8 1037 pr_err("failed to put touchpad into absolute mode.\n");
2a0bd75e
AO
1038 goto init_fail;
1039 }
1040
28f49616
JD
1041 if (elantech_set_input_params(psmouse)) {
1042 pr_err("failed to query touchpad range.\n");
1043 goto init_fail;
1044 }
2a0bd75e
AO
1045
1046 error = sysfs_create_group(&psmouse->ps2dev.serio->dev.kobj,
1047 &elantech_attr_group);
1048 if (error) {
d4ae84a8 1049 pr_err("failed to create sysfs attributes, error: %d.\n", error);
2a0bd75e
AO
1050 goto init_fail;
1051 }
1052
1053 psmouse->protocol_handler = elantech_process_byte;
1054 psmouse->disconnect = elantech_disconnect;
1055 psmouse->reconnect = elantech_reconnect;
28f49616 1056 psmouse->pktsize = etd->hw_version > 1 ? 6 : 4;
2a0bd75e
AO
1057
1058 return 0;
1059
1060 init_fail:
1061 kfree(etd);
1062 return -1;
1063}