Input: elantech - enforce common prefix on messages
[GitHub/mt8127/android_kernel_alcatel_ttab.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>
19#include <linux/serio.h>
20#include <linux/libps2.h>
21#include "psmouse.h"
22#include "elantech.h"
23
d4ae84a8
DT
24#define elantech_debug(fmt, ...) \
25 do { \
26 if (etd->debug) \
27 printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__); \
2a0bd75e
AO
28 } while (0)
29
f81bc788
FR
30static bool force_elantech;
31module_param_named(force_elantech, force_elantech, bool, 0644);
32MODULE_PARM_DESC(force_elantech, "Force the Elantech PS/2 protocol extension to be used, 1 = enabled, 0 = disabled (default).");
33
2a0bd75e
AO
34/*
35 * Send a Synaptics style sliced query command
36 */
37static int synaptics_send_cmd(struct psmouse *psmouse, unsigned char c,
38 unsigned char *param)
39{
40 if (psmouse_sliced_command(psmouse, c) ||
41 ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_GETINFO)) {
d4ae84a8 42 pr_err("synaptics_send_cmd query 0x%02x failed.\n", c);
2a0bd75e
AO
43 return -1;
44 }
45
46 return 0;
47}
48
49/*
50 * A retrying version of ps2_command
51 */
52static int elantech_ps2_command(struct psmouse *psmouse,
53 unsigned char *param, int command)
54{
55 struct ps2dev *ps2dev = &psmouse->ps2dev;
56 struct elantech_data *etd = psmouse->private;
57 int rc;
58 int tries = ETP_PS2_COMMAND_TRIES;
59
60 do {
61 rc = ps2_command(ps2dev, param, command);
62 if (rc == 0)
63 break;
64 tries--;
d4ae84a8
DT
65 elantech_debug("retrying ps2 command 0x%02x (%d).\n",
66 command, tries);
2a0bd75e
AO
67 msleep(ETP_PS2_COMMAND_DELAY);
68 } while (tries > 0);
69
70 if (rc)
d4ae84a8 71 pr_err("ps2 command 0x%02x failed.\n", command);
2a0bd75e
AO
72
73 return rc;
74}
75
76/*
77 * Send an Elantech style special command to read a value from a register
78 */
79static int elantech_read_reg(struct psmouse *psmouse, unsigned char reg,
80 unsigned char *val)
81{
82 struct elantech_data *etd = psmouse->private;
83 unsigned char param[3];
84 int rc = 0;
85
86 if (reg < 0x10 || reg > 0x26)
87 return -1;
88
89 if (reg > 0x11 && reg < 0x20)
90 return -1;
91
92 switch (etd->hw_version) {
93 case 1:
94 if (psmouse_sliced_command(psmouse, ETP_REGISTER_READ) ||
95 psmouse_sliced_command(psmouse, reg) ||
96 ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_GETINFO)) {
97 rc = -1;
98 }
99 break;
100
101 case 2:
102 if (elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
103 elantech_ps2_command(psmouse, NULL, ETP_REGISTER_READ) ||
104 elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
105 elantech_ps2_command(psmouse, NULL, reg) ||
106 elantech_ps2_command(psmouse, param, PSMOUSE_CMD_GETINFO)) {
107 rc = -1;
108 }
109 break;
110 }
111
112 if (rc)
d4ae84a8 113 pr_err("failed to read register 0x%02x.\n", reg);
2a0bd75e
AO
114 else
115 *val = param[0];
116
117 return rc;
118}
119
120/*
121 * Send an Elantech style special command to write a register with a value
122 */
123static int elantech_write_reg(struct psmouse *psmouse, unsigned char reg,
124 unsigned char val)
125{
126 struct elantech_data *etd = psmouse->private;
127 int rc = 0;
128
129 if (reg < 0x10 || reg > 0x26)
130 return -1;
131
132 if (reg > 0x11 && reg < 0x20)
133 return -1;
134
135 switch (etd->hw_version) {
136 case 1:
137 if (psmouse_sliced_command(psmouse, ETP_REGISTER_WRITE) ||
138 psmouse_sliced_command(psmouse, reg) ||
139 psmouse_sliced_command(psmouse, val) ||
140 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11)) {
141 rc = -1;
142 }
143 break;
144
145 case 2:
146 if (elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
147 elantech_ps2_command(psmouse, NULL, ETP_REGISTER_WRITE) ||
148 elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
149 elantech_ps2_command(psmouse, NULL, reg) ||
150 elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
151 elantech_ps2_command(psmouse, NULL, val) ||
152 elantech_ps2_command(psmouse, NULL, PSMOUSE_CMD_SETSCALE11)) {
153 rc = -1;
154 }
155 break;
156 }
157
158 if (rc)
d4ae84a8 159 pr_err("failed to write register 0x%02x with value 0x%02x.\n",
2a0bd75e
AO
160 reg, val);
161
162 return rc;
163}
164
165/*
166 * Dump a complete mouse movement packet to the syslog
167 */
168static void elantech_packet_dump(unsigned char *packet, int size)
169{
170 int i;
171
d4ae84a8 172 printk(KERN_DEBUG pr_fmt("PS/2 packet ["));
2a0bd75e
AO
173 for (i = 0; i < size; i++)
174 printk("%s0x%02x ", (i) ? ", " : " ", packet[i]);
175 printk("]\n");
176}
177
178/*
179 * Interpret complete data packets and report absolute mode input events for
180 * hardware version 1. (4 byte packets)
181 */
182static void elantech_report_absolute_v1(struct psmouse *psmouse)
183{
184 struct input_dev *dev = psmouse->dev;
185 struct elantech_data *etd = psmouse->private;
186 unsigned char *packet = psmouse->packet;
187 int fingers;
3f8c0df4 188 static int old_fingers;
2a0bd75e 189
504e8bee 190 if (etd->fw_version < 0x020000) {
e938fbfd
FR
191 /*
192 * byte 0: D U p1 p2 1 p3 R L
193 * byte 1: f 0 th tw x9 x8 y9 y8
194 */
2a0bd75e
AO
195 fingers = ((packet[1] & 0x80) >> 7) +
196 ((packet[1] & 0x30) >> 4);
197 } else {
e938fbfd
FR
198 /*
199 * byte 0: n1 n0 p2 p1 1 p3 R L
200 * byte 1: 0 0 0 0 x9 x8 y9 y8
201 */
2a0bd75e
AO
202 fingers = (packet[0] & 0xc0) >> 6;
203 }
204
3f8c0df4
AO
205 if (etd->jumpy_cursor) {
206 /* Discard packets that are likely to have bogus coordinates */
207 if (fingers > old_fingers) {
d4ae84a8 208 elantech_debug("discarding packet\n");
3f8c0df4
AO
209 goto discard_packet_v1;
210 }
211 }
212
2a0bd75e
AO
213 input_report_key(dev, BTN_TOUCH, fingers != 0);
214
e938fbfd
FR
215 /*
216 * byte 2: x7 x6 x5 x4 x3 x2 x1 x0
217 * byte 3: y7 y6 y5 y4 y3 y2 y1 y0
218 */
2a0bd75e
AO
219 if (fingers) {
220 input_report_abs(dev, ABS_X,
221 ((packet[1] & 0x0c) << 6) | packet[2]);
e938fbfd
FR
222 input_report_abs(dev, ABS_Y,
223 ETP_YMAX_V1 - (((packet[1] & 0x03) << 8) | packet[3]));
2a0bd75e
AO
224 }
225
226 input_report_key(dev, BTN_TOOL_FINGER, fingers == 1);
227 input_report_key(dev, BTN_TOOL_DOUBLETAP, fingers == 2);
228 input_report_key(dev, BTN_TOOL_TRIPLETAP, fingers == 3);
229 input_report_key(dev, BTN_LEFT, packet[0] & 0x01);
230 input_report_key(dev, BTN_RIGHT, packet[0] & 0x02);
231
504e8bee 232 if (etd->fw_version < 0x020000 &&
2a0bd75e
AO
233 (etd->capabilities & ETP_CAP_HAS_ROCKER)) {
234 /* rocker up */
235 input_report_key(dev, BTN_FORWARD, packet[0] & 0x40);
236 /* rocker down */
237 input_report_key(dev, BTN_BACK, packet[0] & 0x80);
238 }
239
240 input_sync(dev);
3f8c0df4
AO
241
242 discard_packet_v1:
243 old_fingers = fingers;
2a0bd75e
AO
244}
245
246/*
247 * Interpret complete data packets and report absolute mode input events for
248 * hardware version 2. (6 byte packets)
249 */
250static void elantech_report_absolute_v2(struct psmouse *psmouse)
251{
252 struct input_dev *dev = psmouse->dev;
253 unsigned char *packet = psmouse->packet;
254 int fingers, x1, y1, x2, y2;
255
256 /* byte 0: n1 n0 . . . . R L */
257 fingers = (packet[0] & 0xc0) >> 6;
258 input_report_key(dev, BTN_TOUCH, fingers != 0);
259
260 switch (fingers) {
261 case 1:
e938fbfd
FR
262 /*
263 * byte 1: . . . . . x10 x9 x8
264 * byte 2: x7 x6 x5 x4 x4 x2 x1 x0
265 */
266 input_report_abs(dev, ABS_X,
267 ((packet[1] & 0x07) << 8) | packet[2]);
268 /*
269 * byte 4: . . . . . . y9 y8
270 * byte 5: y7 y6 y5 y4 y3 y2 y1 y0
271 */
272 input_report_abs(dev, ABS_Y,
273 ETP_YMAX_V2 - (((packet[4] & 0x03) << 8) | packet[5]));
2a0bd75e
AO
274 break;
275
276 case 2:
e938fbfd
FR
277 /*
278 * The coordinate of each finger is reported separately
279 * with a lower resolution for two finger touches:
280 * byte 0: . . ay8 ax8 . . . .
281 * byte 1: ax7 ax6 ax5 ax4 ax3 ax2 ax1 ax0
282 */
2a0bd75e
AO
283 x1 = ((packet[0] & 0x10) << 4) | packet[1];
284 /* byte 2: ay7 ay6 ay5 ay4 ay3 ay2 ay1 ay0 */
285 y1 = ETP_2FT_YMAX - (((packet[0] & 0x20) << 3) | packet[2]);
e938fbfd
FR
286 /*
287 * byte 3: . . by8 bx8 . . . .
288 * byte 4: bx7 bx6 bx5 bx4 bx3 bx2 bx1 bx0
289 */
2a0bd75e
AO
290 x2 = ((packet[3] & 0x10) << 4) | packet[4];
291 /* byte 5: by7 by8 by5 by4 by3 by2 by1 by0 */
292 y2 = ETP_2FT_YMAX - (((packet[3] & 0x20) << 3) | packet[5]);
e938fbfd
FR
293 /*
294 * For compatibility with the X Synaptics driver scale up
295 * one coordinate and report as ordinary mouse movent
296 */
2a0bd75e
AO
297 input_report_abs(dev, ABS_X, x1 << 2);
298 input_report_abs(dev, ABS_Y, y1 << 2);
e938fbfd
FR
299 /*
300 * For compatibility with the proprietary X Elantech driver
301 * report both coordinates as hat coordinates
302 */
2a0bd75e
AO
303 input_report_abs(dev, ABS_HAT0X, x1);
304 input_report_abs(dev, ABS_HAT0Y, y1);
305 input_report_abs(dev, ABS_HAT1X, x2);
306 input_report_abs(dev, ABS_HAT1Y, y2);
307 break;
308 }
309
310 input_report_key(dev, BTN_TOOL_FINGER, fingers == 1);
311 input_report_key(dev, BTN_TOOL_DOUBLETAP, fingers == 2);
312 input_report_key(dev, BTN_TOOL_TRIPLETAP, fingers == 3);
313 input_report_key(dev, BTN_LEFT, packet[0] & 0x01);
314 input_report_key(dev, BTN_RIGHT, packet[0] & 0x02);
315
316 input_sync(dev);
317}
318
319static int elantech_check_parity_v1(struct psmouse *psmouse)
320{
321 struct elantech_data *etd = psmouse->private;
322 unsigned char *packet = psmouse->packet;
323 unsigned char p1, p2, p3;
324
325 /* Parity bits are placed differently */
504e8bee 326 if (etd->fw_version < 0x020000) {
2a0bd75e
AO
327 /* byte 0: D U p1 p2 1 p3 R L */
328 p1 = (packet[0] & 0x20) >> 5;
329 p2 = (packet[0] & 0x10) >> 4;
330 } else {
331 /* byte 0: n1 n0 p2 p1 1 p3 R L */
332 p1 = (packet[0] & 0x10) >> 4;
333 p2 = (packet[0] & 0x20) >> 5;
334 }
335
336 p3 = (packet[0] & 0x04) >> 2;
337
338 return etd->parity[packet[1]] == p1 &&
339 etd->parity[packet[2]] == p2 &&
340 etd->parity[packet[3]] == p3;
341}
342
343/*
344 * Process byte stream from mouse and handle complete packets
345 */
346static psmouse_ret_t elantech_process_byte(struct psmouse *psmouse)
347{
348 struct elantech_data *etd = psmouse->private;
349
350 if (psmouse->pktcnt < psmouse->pktsize)
351 return PSMOUSE_GOOD_DATA;
352
353 if (etd->debug > 1)
354 elantech_packet_dump(psmouse->packet, psmouse->pktsize);
355
356 switch (etd->hw_version) {
357 case 1:
358 if (etd->paritycheck && !elantech_check_parity_v1(psmouse))
359 return PSMOUSE_BAD_DATA;
360
361 elantech_report_absolute_v1(psmouse);
362 break;
363
364 case 2:
365 /* We don't know how to check parity in protocol v2 */
366 elantech_report_absolute_v2(psmouse);
367 break;
368 }
369
370 return PSMOUSE_FULL_PACKET;
371}
372
373/*
374 * Put the touchpad into absolute mode
375 */
376static int elantech_set_absolute_mode(struct psmouse *psmouse)
377{
378 struct elantech_data *etd = psmouse->private;
379 unsigned char val;
380 int tries = ETP_READ_BACK_TRIES;
381 int rc = 0;
382
383 switch (etd->hw_version) {
384 case 1:
385 etd->reg_10 = 0x16;
386 etd->reg_11 = 0x8f;
387 if (elantech_write_reg(psmouse, 0x10, etd->reg_10) ||
388 elantech_write_reg(psmouse, 0x11, etd->reg_11)) {
389 rc = -1;
390 }
391 break;
392
393 case 2:
394 /* Windows driver values */
395 etd->reg_10 = 0x54;
396 etd->reg_11 = 0x88; /* 0x8a */
397 etd->reg_21 = 0x60; /* 0x00 */
398 if (elantech_write_reg(psmouse, 0x10, etd->reg_10) ||
399 elantech_write_reg(psmouse, 0x11, etd->reg_11) ||
400 elantech_write_reg(psmouse, 0x21, etd->reg_21)) {
401 rc = -1;
402 break;
403 }
b2546df6
AO
404 }
405
406 if (rc == 0) {
2a0bd75e 407 /*
b2546df6
AO
408 * Read back reg 0x10. For hardware version 1 we must make
409 * sure the absolute mode bit is set. For hardware version 2
410 * the touchpad is probably initalising and not ready until
411 * we read back the value we just wrote.
2a0bd75e
AO
412 */
413 do {
414 rc = elantech_read_reg(psmouse, 0x10, &val);
415 if (rc == 0)
416 break;
417 tries--;
d4ae84a8 418 elantech_debug("retrying read (%d).\n", tries);
2a0bd75e
AO
419 msleep(ETP_READ_BACK_DELAY);
420 } while (tries > 0);
b2546df6
AO
421
422 if (rc) {
d4ae84a8 423 pr_err("failed to read back register 0x10.\n");
b2546df6
AO
424 } else if (etd->hw_version == 1 &&
425 !(val & ETP_R10_ABSOLUTE_MODE)) {
d4ae84a8 426 pr_err("touchpad refuses to switch to absolute mode.\n");
b2546df6
AO
427 rc = -1;
428 }
2a0bd75e
AO
429 }
430
431 if (rc)
d4ae84a8 432 pr_err("failed to initialise registers.\n");
2a0bd75e
AO
433
434 return rc;
435}
436
437/*
438 * Set the appropriate event bits for the input subsystem
439 */
440static void elantech_set_input_params(struct psmouse *psmouse)
441{
442 struct input_dev *dev = psmouse->dev;
443 struct elantech_data *etd = psmouse->private;
444
445 __set_bit(EV_KEY, dev->evbit);
446 __set_bit(EV_ABS, dev->evbit);
c7a1f3cc 447 __clear_bit(EV_REL, dev->evbit);
2a0bd75e
AO
448
449 __set_bit(BTN_LEFT, dev->keybit);
450 __set_bit(BTN_RIGHT, dev->keybit);
451
452 __set_bit(BTN_TOUCH, dev->keybit);
453 __set_bit(BTN_TOOL_FINGER, dev->keybit);
454 __set_bit(BTN_TOOL_DOUBLETAP, dev->keybit);
455 __set_bit(BTN_TOOL_TRIPLETAP, dev->keybit);
456
457 switch (etd->hw_version) {
458 case 1:
459 /* Rocker button */
504e8bee 460 if (etd->fw_version < 0x020000 &&
2a0bd75e
AO
461 (etd->capabilities & ETP_CAP_HAS_ROCKER)) {
462 __set_bit(BTN_FORWARD, dev->keybit);
463 __set_bit(BTN_BACK, dev->keybit);
464 }
465 input_set_abs_params(dev, ABS_X, ETP_XMIN_V1, ETP_XMAX_V1, 0, 0);
466 input_set_abs_params(dev, ABS_Y, ETP_YMIN_V1, ETP_YMAX_V1, 0, 0);
467 break;
468
469 case 2:
470 input_set_abs_params(dev, ABS_X, ETP_XMIN_V2, ETP_XMAX_V2, 0, 0);
471 input_set_abs_params(dev, ABS_Y, ETP_YMIN_V2, ETP_YMAX_V2, 0, 0);
472 input_set_abs_params(dev, ABS_HAT0X, ETP_2FT_XMIN, ETP_2FT_XMAX, 0, 0);
473 input_set_abs_params(dev, ABS_HAT0Y, ETP_2FT_YMIN, ETP_2FT_YMAX, 0, 0);
474 input_set_abs_params(dev, ABS_HAT1X, ETP_2FT_XMIN, ETP_2FT_XMAX, 0, 0);
475 input_set_abs_params(dev, ABS_HAT1Y, ETP_2FT_YMIN, ETP_2FT_YMAX, 0, 0);
476 break;
477 }
478}
479
480struct elantech_attr_data {
481 size_t field_offset;
482 unsigned char reg;
483};
484
485/*
486 * Display a register value by reading a sysfs entry
487 */
488static ssize_t elantech_show_int_attr(struct psmouse *psmouse, void *data,
489 char *buf)
490{
491 struct elantech_data *etd = psmouse->private;
492 struct elantech_attr_data *attr = data;
493 unsigned char *reg = (unsigned char *) etd + attr->field_offset;
494 int rc = 0;
495
496 if (attr->reg)
497 rc = elantech_read_reg(psmouse, attr->reg, reg);
498
499 return sprintf(buf, "0x%02x\n", (attr->reg && rc) ? -1 : *reg);
500}
501
502/*
503 * Write a register value by writing a sysfs entry
504 */
505static ssize_t elantech_set_int_attr(struct psmouse *psmouse,
506 void *data, const char *buf, size_t count)
507{
508 struct elantech_data *etd = psmouse->private;
509 struct elantech_attr_data *attr = data;
510 unsigned char *reg = (unsigned char *) etd + attr->field_offset;
511 unsigned long value;
512 int err;
513
514 err = strict_strtoul(buf, 16, &value);
515 if (err)
516 return err;
517
518 if (value > 0xff)
519 return -EINVAL;
520
521 /* Do we need to preserve some bits for version 2 hardware too? */
522 if (etd->hw_version == 1) {
523 if (attr->reg == 0x10)
524 /* Force absolute mode always on */
525 value |= ETP_R10_ABSOLUTE_MODE;
526 else if (attr->reg == 0x11)
527 /* Force 4 byte mode always on */
528 value |= ETP_R11_4_BYTE_MODE;
529 }
530
531 if (!attr->reg || elantech_write_reg(psmouse, attr->reg, value) == 0)
532 *reg = value;
533
534 return count;
535}
536
537#define ELANTECH_INT_ATTR(_name, _register) \
538 static struct elantech_attr_data elantech_attr_##_name = { \
539 .field_offset = offsetof(struct elantech_data, _name), \
540 .reg = _register, \
541 }; \
542 PSMOUSE_DEFINE_ATTR(_name, S_IWUSR | S_IRUGO, \
543 &elantech_attr_##_name, \
544 elantech_show_int_attr, \
545 elantech_set_int_attr)
546
547ELANTECH_INT_ATTR(reg_10, 0x10);
548ELANTECH_INT_ATTR(reg_11, 0x11);
549ELANTECH_INT_ATTR(reg_20, 0x20);
550ELANTECH_INT_ATTR(reg_21, 0x21);
551ELANTECH_INT_ATTR(reg_22, 0x22);
552ELANTECH_INT_ATTR(reg_23, 0x23);
553ELANTECH_INT_ATTR(reg_24, 0x24);
554ELANTECH_INT_ATTR(reg_25, 0x25);
555ELANTECH_INT_ATTR(reg_26, 0x26);
556ELANTECH_INT_ATTR(debug, 0);
557ELANTECH_INT_ATTR(paritycheck, 0);
558
559static struct attribute *elantech_attrs[] = {
560 &psmouse_attr_reg_10.dattr.attr,
561 &psmouse_attr_reg_11.dattr.attr,
562 &psmouse_attr_reg_20.dattr.attr,
563 &psmouse_attr_reg_21.dattr.attr,
564 &psmouse_attr_reg_22.dattr.attr,
565 &psmouse_attr_reg_23.dattr.attr,
566 &psmouse_attr_reg_24.dattr.attr,
567 &psmouse_attr_reg_25.dattr.attr,
568 &psmouse_attr_reg_26.dattr.attr,
569 &psmouse_attr_debug.dattr.attr,
570 &psmouse_attr_paritycheck.dattr.attr,
571 NULL
572};
573
574static struct attribute_group elantech_attr_group = {
575 .attrs = elantech_attrs,
576};
577
578/*
579 * Use magic knock to detect Elantech touchpad
580 */
b7802c5c 581int elantech_detect(struct psmouse *psmouse, bool set_properties)
2a0bd75e
AO
582{
583 struct ps2dev *ps2dev = &psmouse->ps2dev;
584 unsigned char param[3];
585
586 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
587
588 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
589 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
590 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
591 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
592 ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO)) {
d4ae84a8 593 pr_debug("sending Elantech magic knock failed.\n");
2a0bd75e
AO
594 return -1;
595 }
596
597 /*
598 * Report this in case there are Elantech models that use a different
599 * set of magic numbers
600 */
601 if (param[0] != 0x3c || param[1] != 0x03 || param[2] != 0xc8) {
d4ae84a8 602 pr_debug("unexpected magic knock result 0x%02x, 0x%02x, 0x%02x.\n",
9ab7b25e
AO
603 param[0], param[1], param[2]);
604 return -1;
605 }
606
607 /*
608 * Query touchpad's firmware version and see if it reports known
609 * value to avoid mis-detection. Logitech mice are known to respond
610 * to Elantech magic knock and there might be more.
611 */
612 if (synaptics_send_cmd(psmouse, ETP_FW_VERSION_QUERY, param)) {
d4ae84a8 613 pr_debug("failed to query firmware version.\n");
9ab7b25e
AO
614 return -1;
615 }
616
d4ae84a8 617 pr_debug("Elantech version query result 0x%02x, 0x%02x, 0x%02x.\n",
9ab7b25e
AO
618 param[0], param[1], param[2]);
619
620 if (param[0] == 0 || param[1] != 0) {
f81bc788 621 if (!force_elantech) {
d4ae84a8 622 pr_debug("Probably not a real Elantech touchpad. Aborting.\n");
f81bc788
FR
623 return -1;
624 }
625
d4ae84a8 626 pr_debug("Probably not a real Elantech touchpad. Enabling anyway due to force_elantech.\n");
2a0bd75e
AO
627 }
628
629 if (set_properties) {
630 psmouse->vendor = "Elantech";
631 psmouse->name = "Touchpad";
632 }
633
634 return 0;
635}
636
637/*
638 * Clean up sysfs entries when disconnecting
639 */
640static void elantech_disconnect(struct psmouse *psmouse)
641{
642 sysfs_remove_group(&psmouse->ps2dev.serio->dev.kobj,
643 &elantech_attr_group);
644 kfree(psmouse->private);
645 psmouse->private = NULL;
646}
647
648/*
649 * Put the touchpad back into absolute mode when reconnecting
650 */
651static int elantech_reconnect(struct psmouse *psmouse)
652{
653 if (elantech_detect(psmouse, 0))
654 return -1;
655
656 if (elantech_set_absolute_mode(psmouse)) {
d4ae84a8 657 pr_err("failed to put touchpad back into absolute mode.\n");
2a0bd75e
AO
658 return -1;
659 }
660
661 return 0;
662}
663
664/*
665 * Initialize the touchpad and create sysfs entries
666 */
667int elantech_init(struct psmouse *psmouse)
668{
669 struct elantech_data *etd;
670 int i, error;
671 unsigned char param[3];
672
9ab7b25e 673 psmouse->private = etd = kzalloc(sizeof(struct elantech_data), GFP_KERNEL);
2a0bd75e
AO
674 if (!etd)
675 return -1;
676
677 etd->parity[0] = 1;
678 for (i = 1; i < 256; i++)
679 etd->parity[i] = etd->parity[i & (i - 1)] ^ 1;
680
681 /*
9ab7b25e 682 * Do the version query again so we can store the result
2a0bd75e
AO
683 */
684 if (synaptics_send_cmd(psmouse, ETP_FW_VERSION_QUERY, param)) {
d4ae84a8 685 pr_err("failed to query firmware version.\n");
2a0bd75e
AO
686 goto init_fail;
687 }
504e8bee
DT
688
689 etd->fw_version = (param[0] << 16) | (param[1] << 8) | param[2];
2a0bd75e
AO
690
691 /*
692 * Assume every version greater than this is new EeePC style
693 * hardware with 6 byte packets
694 */
504e8bee 695 if (etd->fw_version >= 0x020030) {
2a0bd75e
AO
696 etd->hw_version = 2;
697 /* For now show extra debug information */
698 etd->debug = 1;
699 /* Don't know how to do parity checking for version 2 */
700 etd->paritycheck = 0;
701 } else {
702 etd->hw_version = 1;
703 etd->paritycheck = 1;
704 }
504e8bee 705
d4ae84a8 706 pr_info("assuming hardware version %d, firmware version %d.%d.%d\n",
504e8bee 707 etd->hw_version, param[0], param[1], param[2]);
2a0bd75e
AO
708
709 if (synaptics_send_cmd(psmouse, ETP_CAPABILITIES_QUERY, param)) {
d4ae84a8 710 pr_err("failed to query capabilities.\n");
2a0bd75e
AO
711 goto init_fail;
712 }
d4ae84a8 713 pr_info("Synaptics capabilities query result 0x%02x, 0x%02x, 0x%02x.\n",
2a0bd75e
AO
714 param[0], param[1], param[2]);
715 etd->capabilities = param[0];
716
3f8c0df4
AO
717 /*
718 * This firmware seems to suffer from misreporting coordinates when
719 * a touch action starts causing the mouse cursor or scrolled page
720 * to jump. Enable a workaround.
721 */
504e8bee 722 if (etd->fw_version == 0x020022) {
d4ae84a8 723 pr_info("firmware version 2.0.34 detected, enabling jumpy cursor workaround\n");
3f8c0df4
AO
724 etd->jumpy_cursor = 1;
725 }
726
2a0bd75e 727 if (elantech_set_absolute_mode(psmouse)) {
d4ae84a8 728 pr_err("failed to put touchpad into absolute mode.\n");
2a0bd75e
AO
729 goto init_fail;
730 }
731
732 elantech_set_input_params(psmouse);
733
734 error = sysfs_create_group(&psmouse->ps2dev.serio->dev.kobj,
735 &elantech_attr_group);
736 if (error) {
d4ae84a8 737 pr_err("failed to create sysfs attributes, error: %d.\n", error);
2a0bd75e
AO
738 goto init_fail;
739 }
740
741 psmouse->protocol_handler = elantech_process_byte;
742 psmouse->disconnect = elantech_disconnect;
743 psmouse->reconnect = elantech_reconnect;
744 psmouse->pktsize = etd->hw_version == 2 ? 6 : 4;
745
746 return 0;
747
748 init_fail:
749 kfree(etd);
750 return -1;
751}