Input: elantech - export pressure and width when supported
[deliverable/linux.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;
188
504e8bee 189 if (etd->fw_version < 0x020000) {
e938fbfd
FR
190 /*
191 * byte 0: D U p1 p2 1 p3 R L
192 * byte 1: f 0 th tw x9 x8 y9 y8
193 */
2a0bd75e
AO
194 fingers = ((packet[1] & 0x80) >> 7) +
195 ((packet[1] & 0x30) >> 4);
196 } else {
e938fbfd
FR
197 /*
198 * byte 0: n1 n0 p2 p1 1 p3 R L
199 * byte 1: 0 0 0 0 x9 x8 y9 y8
200 */
2a0bd75e
AO
201 fingers = (packet[0] & 0xc0) >> 6;
202 }
203
3f8c0df4 204 if (etd->jumpy_cursor) {
7f29f17b
ÉP
205 if (fingers != 1) {
206 etd->single_finger_reports = 0;
207 } else if (etd->single_finger_reports < 2) {
208 /* Discard first 2 reports of one finger, bogus */
209 etd->single_finger_reports++;
d4ae84a8 210 elantech_debug("discarding packet\n");
7f29f17b 211 return;
3f8c0df4
AO
212 }
213 }
214
2a0bd75e
AO
215 input_report_key(dev, BTN_TOUCH, fingers != 0);
216
e938fbfd
FR
217 /*
218 * byte 2: x7 x6 x5 x4 x3 x2 x1 x0
219 * byte 3: y7 y6 y5 y4 y3 y2 y1 y0
220 */
2a0bd75e
AO
221 if (fingers) {
222 input_report_abs(dev, ABS_X,
223 ((packet[1] & 0x0c) << 6) | packet[2]);
e938fbfd
FR
224 input_report_abs(dev, ABS_Y,
225 ETP_YMAX_V1 - (((packet[1] & 0x03) << 8) | packet[3]));
2a0bd75e
AO
226 }
227
228 input_report_key(dev, BTN_TOOL_FINGER, fingers == 1);
229 input_report_key(dev, BTN_TOOL_DOUBLETAP, fingers == 2);
230 input_report_key(dev, BTN_TOOL_TRIPLETAP, fingers == 3);
231 input_report_key(dev, BTN_LEFT, packet[0] & 0x01);
232 input_report_key(dev, BTN_RIGHT, packet[0] & 0x02);
233
504e8bee 234 if (etd->fw_version < 0x020000 &&
2a0bd75e
AO
235 (etd->capabilities & ETP_CAP_HAS_ROCKER)) {
236 /* rocker up */
237 input_report_key(dev, BTN_FORWARD, packet[0] & 0x40);
238 /* rocker down */
239 input_report_key(dev, BTN_BACK, packet[0] & 0x80);
240 }
241
242 input_sync(dev);
243}
244
245/*
246 * Interpret complete data packets and report absolute mode input events for
247 * hardware version 2. (6 byte packets)
248 */
249static void elantech_report_absolute_v2(struct psmouse *psmouse)
250{
f941c705 251 struct elantech_data *etd = psmouse->private;
2a0bd75e
AO
252 struct input_dev *dev = psmouse->dev;
253 unsigned char *packet = psmouse->packet;
f941c705 254 int fingers, x1, y1, x2, y2, width = 0, pres = 0;
2a0bd75e
AO
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) {
22462d9f
ÉP
261 case 3:
262 /*
263 * Same as one finger, except report of more than 3 fingers:
264 * byte 3: n4 . w1 w0 . . . .
265 */
266 if (packet[3] & 0x80)
267 fingers = 4;
268 /* pass through... */
2a0bd75e 269 case 1:
e938fbfd
FR
270 /*
271 * byte 1: . . . . . x10 x9 x8
272 * byte 2: x7 x6 x5 x4 x4 x2 x1 x0
273 */
274 input_report_abs(dev, ABS_X,
275 ((packet[1] & 0x07) << 8) | packet[2]);
276 /*
277 * byte 4: . . . . . . y9 y8
278 * byte 5: y7 y6 y5 y4 y3 y2 y1 y0
279 */
280 input_report_abs(dev, ABS_Y,
281 ETP_YMAX_V2 - (((packet[4] & 0x03) << 8) | packet[5]));
f941c705
ÉP
282 pres = (packet[1] & 0xf0) | ((packet[4] & 0xf0) >> 4);
283 width = ((packet[0] & 0x30) >> 2) | ((packet[3] & 0x30) >> 4);
2a0bd75e
AO
284 break;
285
286 case 2:
e938fbfd
FR
287 /*
288 * The coordinate of each finger is reported separately
289 * with a lower resolution for two finger touches:
290 * byte 0: . . ay8 ax8 . . . .
291 * byte 1: ax7 ax6 ax5 ax4 ax3 ax2 ax1 ax0
292 */
2a0bd75e
AO
293 x1 = ((packet[0] & 0x10) << 4) | packet[1];
294 /* byte 2: ay7 ay6 ay5 ay4 ay3 ay2 ay1 ay0 */
295 y1 = ETP_2FT_YMAX - (((packet[0] & 0x20) << 3) | packet[2]);
e938fbfd
FR
296 /*
297 * byte 3: . . by8 bx8 . . . .
298 * byte 4: bx7 bx6 bx5 bx4 bx3 bx2 bx1 bx0
299 */
2a0bd75e
AO
300 x2 = ((packet[3] & 0x10) << 4) | packet[4];
301 /* byte 5: by7 by8 by5 by4 by3 by2 by1 by0 */
302 y2 = ETP_2FT_YMAX - (((packet[3] & 0x20) << 3) | packet[5]);
e938fbfd
FR
303 /*
304 * For compatibility with the X Synaptics driver scale up
305 * one coordinate and report as ordinary mouse movent
306 */
2a0bd75e
AO
307 input_report_abs(dev, ABS_X, x1 << 2);
308 input_report_abs(dev, ABS_Y, y1 << 2);
e938fbfd
FR
309 /*
310 * For compatibility with the proprietary X Elantech driver
311 * report both coordinates as hat coordinates
312 */
2a0bd75e
AO
313 input_report_abs(dev, ABS_HAT0X, x1);
314 input_report_abs(dev, ABS_HAT0Y, y1);
315 input_report_abs(dev, ABS_HAT1X, x2);
316 input_report_abs(dev, ABS_HAT1Y, y2);
f941c705
ÉP
317
318 /* Unknown so just report sensible values */
319 pres = 127;
320 width = 7;
2a0bd75e
AO
321 break;
322 }
323
324 input_report_key(dev, BTN_TOOL_FINGER, fingers == 1);
325 input_report_key(dev, BTN_TOOL_DOUBLETAP, fingers == 2);
326 input_report_key(dev, BTN_TOOL_TRIPLETAP, fingers == 3);
22462d9f 327 input_report_key(dev, BTN_TOOL_QUADTAP, fingers == 4);
2a0bd75e
AO
328 input_report_key(dev, BTN_LEFT, packet[0] & 0x01);
329 input_report_key(dev, BTN_RIGHT, packet[0] & 0x02);
f941c705
ÉP
330 if (etd->reports_pressure) {
331 input_report_abs(dev, ABS_PRESSURE, pres);
332 input_report_abs(dev, ABS_TOOL_WIDTH, width);
333 }
2a0bd75e
AO
334
335 input_sync(dev);
336}
337
338static int elantech_check_parity_v1(struct psmouse *psmouse)
339{
340 struct elantech_data *etd = psmouse->private;
341 unsigned char *packet = psmouse->packet;
342 unsigned char p1, p2, p3;
343
344 /* Parity bits are placed differently */
504e8bee 345 if (etd->fw_version < 0x020000) {
2a0bd75e
AO
346 /* byte 0: D U p1 p2 1 p3 R L */
347 p1 = (packet[0] & 0x20) >> 5;
348 p2 = (packet[0] & 0x10) >> 4;
349 } else {
350 /* byte 0: n1 n0 p2 p1 1 p3 R L */
351 p1 = (packet[0] & 0x10) >> 4;
352 p2 = (packet[0] & 0x20) >> 5;
353 }
354
355 p3 = (packet[0] & 0x04) >> 2;
356
357 return etd->parity[packet[1]] == p1 &&
358 etd->parity[packet[2]] == p2 &&
359 etd->parity[packet[3]] == p3;
360}
361
362/*
363 * Process byte stream from mouse and handle complete packets
364 */
365static psmouse_ret_t elantech_process_byte(struct psmouse *psmouse)
366{
367 struct elantech_data *etd = psmouse->private;
368
369 if (psmouse->pktcnt < psmouse->pktsize)
370 return PSMOUSE_GOOD_DATA;
371
372 if (etd->debug > 1)
373 elantech_packet_dump(psmouse->packet, psmouse->pktsize);
374
375 switch (etd->hw_version) {
376 case 1:
377 if (etd->paritycheck && !elantech_check_parity_v1(psmouse))
378 return PSMOUSE_BAD_DATA;
379
380 elantech_report_absolute_v1(psmouse);
381 break;
382
383 case 2:
384 /* We don't know how to check parity in protocol v2 */
385 elantech_report_absolute_v2(psmouse);
386 break;
387 }
388
389 return PSMOUSE_FULL_PACKET;
390}
391
392/*
393 * Put the touchpad into absolute mode
394 */
395static int elantech_set_absolute_mode(struct psmouse *psmouse)
396{
397 struct elantech_data *etd = psmouse->private;
398 unsigned char val;
399 int tries = ETP_READ_BACK_TRIES;
400 int rc = 0;
401
402 switch (etd->hw_version) {
403 case 1:
404 etd->reg_10 = 0x16;
405 etd->reg_11 = 0x8f;
406 if (elantech_write_reg(psmouse, 0x10, etd->reg_10) ||
407 elantech_write_reg(psmouse, 0x11, etd->reg_11)) {
408 rc = -1;
409 }
410 break;
411
412 case 2:
413 /* Windows driver values */
414 etd->reg_10 = 0x54;
415 etd->reg_11 = 0x88; /* 0x8a */
416 etd->reg_21 = 0x60; /* 0x00 */
417 if (elantech_write_reg(psmouse, 0x10, etd->reg_10) ||
418 elantech_write_reg(psmouse, 0x11, etd->reg_11) ||
419 elantech_write_reg(psmouse, 0x21, etd->reg_21)) {
420 rc = -1;
421 break;
422 }
b2546df6
AO
423 }
424
425 if (rc == 0) {
2a0bd75e 426 /*
b2546df6
AO
427 * Read back reg 0x10. For hardware version 1 we must make
428 * sure the absolute mode bit is set. For hardware version 2
429 * the touchpad is probably initalising and not ready until
430 * we read back the value we just wrote.
2a0bd75e
AO
431 */
432 do {
433 rc = elantech_read_reg(psmouse, 0x10, &val);
434 if (rc == 0)
435 break;
436 tries--;
d4ae84a8 437 elantech_debug("retrying read (%d).\n", tries);
2a0bd75e
AO
438 msleep(ETP_READ_BACK_DELAY);
439 } while (tries > 0);
b2546df6
AO
440
441 if (rc) {
d4ae84a8 442 pr_err("failed to read back register 0x10.\n");
b2546df6
AO
443 } else if (etd->hw_version == 1 &&
444 !(val & ETP_R10_ABSOLUTE_MODE)) {
d4ae84a8 445 pr_err("touchpad refuses to switch to absolute mode.\n");
b2546df6
AO
446 rc = -1;
447 }
2a0bd75e
AO
448 }
449
450 if (rc)
d4ae84a8 451 pr_err("failed to initialise registers.\n");
2a0bd75e
AO
452
453 return rc;
454}
455
456/*
457 * Set the appropriate event bits for the input subsystem
458 */
459static void elantech_set_input_params(struct psmouse *psmouse)
460{
461 struct input_dev *dev = psmouse->dev;
462 struct elantech_data *etd = psmouse->private;
463
464 __set_bit(EV_KEY, dev->evbit);
465 __set_bit(EV_ABS, dev->evbit);
c7a1f3cc 466 __clear_bit(EV_REL, dev->evbit);
2a0bd75e
AO
467
468 __set_bit(BTN_LEFT, dev->keybit);
469 __set_bit(BTN_RIGHT, dev->keybit);
470
471 __set_bit(BTN_TOUCH, dev->keybit);
472 __set_bit(BTN_TOOL_FINGER, dev->keybit);
473 __set_bit(BTN_TOOL_DOUBLETAP, dev->keybit);
474 __set_bit(BTN_TOOL_TRIPLETAP, dev->keybit);
475
476 switch (etd->hw_version) {
477 case 1:
478 /* Rocker button */
504e8bee 479 if (etd->fw_version < 0x020000 &&
2a0bd75e
AO
480 (etd->capabilities & ETP_CAP_HAS_ROCKER)) {
481 __set_bit(BTN_FORWARD, dev->keybit);
482 __set_bit(BTN_BACK, dev->keybit);
483 }
484 input_set_abs_params(dev, ABS_X, ETP_XMIN_V1, ETP_XMAX_V1, 0, 0);
485 input_set_abs_params(dev, ABS_Y, ETP_YMIN_V1, ETP_YMAX_V1, 0, 0);
486 break;
487
488 case 2:
22462d9f 489 __set_bit(BTN_TOOL_QUADTAP, dev->keybit);
2a0bd75e
AO
490 input_set_abs_params(dev, ABS_X, ETP_XMIN_V2, ETP_XMAX_V2, 0, 0);
491 input_set_abs_params(dev, ABS_Y, ETP_YMIN_V2, ETP_YMAX_V2, 0, 0);
f941c705
ÉP
492 if (etd->reports_pressure) {
493 input_set_abs_params(dev, ABS_PRESSURE, ETP_PMIN_V2,
494 ETP_PMAX_V2, 0, 0);
495 input_set_abs_params(dev, ABS_TOOL_WIDTH, ETP_WMIN_V2,
496 ETP_WMAX_V2, 0, 0);
497 }
2a0bd75e
AO
498 input_set_abs_params(dev, ABS_HAT0X, ETP_2FT_XMIN, ETP_2FT_XMAX, 0, 0);
499 input_set_abs_params(dev, ABS_HAT0Y, ETP_2FT_YMIN, ETP_2FT_YMAX, 0, 0);
500 input_set_abs_params(dev, ABS_HAT1X, ETP_2FT_XMIN, ETP_2FT_XMAX, 0, 0);
501 input_set_abs_params(dev, ABS_HAT1Y, ETP_2FT_YMIN, ETP_2FT_YMAX, 0, 0);
502 break;
503 }
504}
505
506struct elantech_attr_data {
507 size_t field_offset;
508 unsigned char reg;
509};
510
511/*
512 * Display a register value by reading a sysfs entry
513 */
514static ssize_t elantech_show_int_attr(struct psmouse *psmouse, void *data,
515 char *buf)
516{
517 struct elantech_data *etd = psmouse->private;
518 struct elantech_attr_data *attr = data;
519 unsigned char *reg = (unsigned char *) etd + attr->field_offset;
520 int rc = 0;
521
522 if (attr->reg)
523 rc = elantech_read_reg(psmouse, attr->reg, reg);
524
525 return sprintf(buf, "0x%02x\n", (attr->reg && rc) ? -1 : *reg);
526}
527
528/*
529 * Write a register value by writing a sysfs entry
530 */
531static ssize_t elantech_set_int_attr(struct psmouse *psmouse,
532 void *data, const char *buf, size_t count)
533{
534 struct elantech_data *etd = psmouse->private;
535 struct elantech_attr_data *attr = data;
536 unsigned char *reg = (unsigned char *) etd + attr->field_offset;
537 unsigned long value;
538 int err;
539
540 err = strict_strtoul(buf, 16, &value);
541 if (err)
542 return err;
543
544 if (value > 0xff)
545 return -EINVAL;
546
547 /* Do we need to preserve some bits for version 2 hardware too? */
548 if (etd->hw_version == 1) {
549 if (attr->reg == 0x10)
550 /* Force absolute mode always on */
551 value |= ETP_R10_ABSOLUTE_MODE;
552 else if (attr->reg == 0x11)
553 /* Force 4 byte mode always on */
554 value |= ETP_R11_4_BYTE_MODE;
555 }
556
557 if (!attr->reg || elantech_write_reg(psmouse, attr->reg, value) == 0)
558 *reg = value;
559
560 return count;
561}
562
563#define ELANTECH_INT_ATTR(_name, _register) \
564 static struct elantech_attr_data elantech_attr_##_name = { \
565 .field_offset = offsetof(struct elantech_data, _name), \
566 .reg = _register, \
567 }; \
568 PSMOUSE_DEFINE_ATTR(_name, S_IWUSR | S_IRUGO, \
569 &elantech_attr_##_name, \
570 elantech_show_int_attr, \
571 elantech_set_int_attr)
572
573ELANTECH_INT_ATTR(reg_10, 0x10);
574ELANTECH_INT_ATTR(reg_11, 0x11);
575ELANTECH_INT_ATTR(reg_20, 0x20);
576ELANTECH_INT_ATTR(reg_21, 0x21);
577ELANTECH_INT_ATTR(reg_22, 0x22);
578ELANTECH_INT_ATTR(reg_23, 0x23);
579ELANTECH_INT_ATTR(reg_24, 0x24);
580ELANTECH_INT_ATTR(reg_25, 0x25);
581ELANTECH_INT_ATTR(reg_26, 0x26);
582ELANTECH_INT_ATTR(debug, 0);
583ELANTECH_INT_ATTR(paritycheck, 0);
584
585static struct attribute *elantech_attrs[] = {
586 &psmouse_attr_reg_10.dattr.attr,
587 &psmouse_attr_reg_11.dattr.attr,
588 &psmouse_attr_reg_20.dattr.attr,
589 &psmouse_attr_reg_21.dattr.attr,
590 &psmouse_attr_reg_22.dattr.attr,
591 &psmouse_attr_reg_23.dattr.attr,
592 &psmouse_attr_reg_24.dattr.attr,
593 &psmouse_attr_reg_25.dattr.attr,
594 &psmouse_attr_reg_26.dattr.attr,
595 &psmouse_attr_debug.dattr.attr,
596 &psmouse_attr_paritycheck.dattr.attr,
597 NULL
598};
599
600static struct attribute_group elantech_attr_group = {
601 .attrs = elantech_attrs,
602};
603
a083632e
DT
604static bool elantech_is_signature_valid(const unsigned char *param)
605{
606 static const unsigned char rates[] = { 200, 100, 80, 60, 40, 20, 10 };
607 int i;
608
609 if (param[0] == 0)
610 return false;
611
612 if (param[1] == 0)
613 return true;
614
615 for (i = 0; i < ARRAY_SIZE(rates); i++)
616 if (param[2] == rates[i])
617 return false;
618
619 return true;
620}
621
2a0bd75e
AO
622/*
623 * Use magic knock to detect Elantech touchpad
624 */
b7802c5c 625int elantech_detect(struct psmouse *psmouse, bool set_properties)
2a0bd75e
AO
626{
627 struct ps2dev *ps2dev = &psmouse->ps2dev;
628 unsigned char param[3];
629
630 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
631
632 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
633 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
634 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
635 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
636 ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO)) {
d4ae84a8 637 pr_debug("sending Elantech magic knock failed.\n");
2a0bd75e
AO
638 return -1;
639 }
640
641 /*
642 * Report this in case there are Elantech models that use a different
643 * set of magic numbers
644 */
645 if (param[0] != 0x3c || param[1] != 0x03 || param[2] != 0xc8) {
d4ae84a8 646 pr_debug("unexpected magic knock result 0x%02x, 0x%02x, 0x%02x.\n",
9ab7b25e
AO
647 param[0], param[1], param[2]);
648 return -1;
649 }
650
651 /*
652 * Query touchpad's firmware version and see if it reports known
653 * value to avoid mis-detection. Logitech mice are known to respond
654 * to Elantech magic knock and there might be more.
655 */
656 if (synaptics_send_cmd(psmouse, ETP_FW_VERSION_QUERY, param)) {
d4ae84a8 657 pr_debug("failed to query firmware version.\n");
9ab7b25e
AO
658 return -1;
659 }
660
d4ae84a8 661 pr_debug("Elantech version query result 0x%02x, 0x%02x, 0x%02x.\n",
9ab7b25e
AO
662 param[0], param[1], param[2]);
663
a083632e 664 if (!elantech_is_signature_valid(param)) {
f81bc788 665 if (!force_elantech) {
d4ae84a8 666 pr_debug("Probably not a real Elantech touchpad. Aborting.\n");
f81bc788
FR
667 return -1;
668 }
669
d4ae84a8 670 pr_debug("Probably not a real Elantech touchpad. Enabling anyway due to force_elantech.\n");
2a0bd75e
AO
671 }
672
673 if (set_properties) {
674 psmouse->vendor = "Elantech";
675 psmouse->name = "Touchpad";
676 }
677
678 return 0;
679}
680
681/*
682 * Clean up sysfs entries when disconnecting
683 */
684static void elantech_disconnect(struct psmouse *psmouse)
685{
686 sysfs_remove_group(&psmouse->ps2dev.serio->dev.kobj,
687 &elantech_attr_group);
688 kfree(psmouse->private);
689 psmouse->private = NULL;
690}
691
692/*
693 * Put the touchpad back into absolute mode when reconnecting
694 */
695static int elantech_reconnect(struct psmouse *psmouse)
696{
697 if (elantech_detect(psmouse, 0))
698 return -1;
699
700 if (elantech_set_absolute_mode(psmouse)) {
d4ae84a8 701 pr_err("failed to put touchpad back into absolute mode.\n");
2a0bd75e
AO
702 return -1;
703 }
704
705 return 0;
706}
707
708/*
709 * Initialize the touchpad and create sysfs entries
710 */
711int elantech_init(struct psmouse *psmouse)
712{
713 struct elantech_data *etd;
714 int i, error;
715 unsigned char param[3];
716
9ab7b25e 717 psmouse->private = etd = kzalloc(sizeof(struct elantech_data), GFP_KERNEL);
2a0bd75e 718 if (!etd)
6792cbbb 719 return -ENOMEM;
2a0bd75e
AO
720
721 etd->parity[0] = 1;
722 for (i = 1; i < 256; i++)
723 etd->parity[i] = etd->parity[i & (i - 1)] ^ 1;
724
725 /*
9ab7b25e 726 * Do the version query again so we can store the result
2a0bd75e
AO
727 */
728 if (synaptics_send_cmd(psmouse, ETP_FW_VERSION_QUERY, param)) {
d4ae84a8 729 pr_err("failed to query firmware version.\n");
2a0bd75e
AO
730 goto init_fail;
731 }
504e8bee
DT
732
733 etd->fw_version = (param[0] << 16) | (param[1] << 8) | param[2];
2a0bd75e
AO
734
735 /*
736 * Assume every version greater than this is new EeePC style
737 * hardware with 6 byte packets
738 */
504e8bee 739 if (etd->fw_version >= 0x020030) {
2a0bd75e
AO
740 etd->hw_version = 2;
741 /* For now show extra debug information */
742 etd->debug = 1;
743 /* Don't know how to do parity checking for version 2 */
744 etd->paritycheck = 0;
f941c705
ÉP
745
746 if (etd->fw_version >= 0x020800)
747 etd->reports_pressure = true;
748
2a0bd75e
AO
749 } else {
750 etd->hw_version = 1;
751 etd->paritycheck = 1;
752 }
504e8bee 753
d4ae84a8 754 pr_info("assuming hardware version %d, firmware version %d.%d.%d\n",
504e8bee 755 etd->hw_version, param[0], param[1], param[2]);
2a0bd75e
AO
756
757 if (synaptics_send_cmd(psmouse, ETP_CAPABILITIES_QUERY, param)) {
d4ae84a8 758 pr_err("failed to query capabilities.\n");
2a0bd75e
AO
759 goto init_fail;
760 }
d4ae84a8 761 pr_info("Synaptics capabilities query result 0x%02x, 0x%02x, 0x%02x.\n",
2a0bd75e
AO
762 param[0], param[1], param[2]);
763 etd->capabilities = param[0];
764
3f8c0df4 765 /*
7f29f17b 766 * This firmware suffers from misreporting coordinates when
3f8c0df4
AO
767 * a touch action starts causing the mouse cursor or scrolled page
768 * to jump. Enable a workaround.
769 */
7f29f17b
ÉP
770 if (etd->fw_version == 0x020022 || etd->fw_version == 0x020600) {
771 pr_info("firmware version 2.0.34/2.6.0 detected, enabling jumpy cursor workaround\n");
772 etd->jumpy_cursor = true;
3f8c0df4
AO
773 }
774
2a0bd75e 775 if (elantech_set_absolute_mode(psmouse)) {
d4ae84a8 776 pr_err("failed to put touchpad into absolute mode.\n");
2a0bd75e
AO
777 goto init_fail;
778 }
779
780 elantech_set_input_params(psmouse);
781
782 error = sysfs_create_group(&psmouse->ps2dev.serio->dev.kobj,
783 &elantech_attr_group);
784 if (error) {
d4ae84a8 785 pr_err("failed to create sysfs attributes, error: %d.\n", error);
2a0bd75e
AO
786 goto init_fail;
787 }
788
789 psmouse->protocol_handler = elantech_process_byte;
790 psmouse->disconnect = elantech_disconnect;
791 psmouse->reconnect = elantech_reconnect;
792 psmouse->pktsize = etd->hw_version == 2 ? 6 : 4;
793
794 return 0;
795
796 init_fail:
797 kfree(etd);
798 return -1;
799}
This page took 0.16235 seconds and 5 git commands to generate.