blob: bd5b91da169e941305e423ba267775d54fc0c6bc [file] [log] [blame]
Arjan Opmeer2a0bd752008-10-16 22:10:19 -04001/*
Arjan Opmeer3f8c0df2009-04-18 19:05:40 -07002 * Elantech Touchpad driver (v6)
Arjan Opmeer2a0bd752008-10-16 22:10:19 -04003 *
Arjan Opmeer3f8c0df2009-04-18 19:05:40 -07004 * Copyright (C) 2007-2009 Arjan Opmeer <arjan@opmeer.net>
Arjan Opmeer2a0bd752008-10-16 22:10:19 -04005 *
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
Dmitry Torokhovd4ae84a82010-05-13 00:41:15 -070013#define pr_fmt(fmt) KBUILD_BASENAME ": " fmt
14
Arjan Opmeer2a0bd752008-10-16 22:10:19 -040015#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Arjan Opmeer2a0bd752008-10-16 22:10:19 -040017#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
Dmitry Torokhovd4ae84a82010-05-13 00:41:15 -070024#define elantech_debug(fmt, ...) \
25 do { \
26 if (etd->debug) \
27 printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__); \
Arjan Opmeer2a0bd752008-10-16 22:10:19 -040028 } while (0)
29
Florian Ragwitzf81bc782010-04-27 00:47:04 -070030static 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
Arjan Opmeer2a0bd752008-10-16 22:10:19 -040034/*
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)) {
Dmitry Torokhovd4ae84a82010-05-13 00:41:15 -070042 pr_err("synaptics_send_cmd query 0x%02x failed.\n", c);
Arjan Opmeer2a0bd752008-10-16 22:10:19 -040043 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--;
Dmitry Torokhovd4ae84a82010-05-13 00:41:15 -070065 elantech_debug("retrying ps2 command 0x%02x (%d).\n",
66 command, tries);
Arjan Opmeer2a0bd752008-10-16 22:10:19 -040067 msleep(ETP_PS2_COMMAND_DELAY);
68 } while (tries > 0);
69
70 if (rc)
Dmitry Torokhovd4ae84a82010-05-13 00:41:15 -070071 pr_err("ps2 command 0x%02x failed.\n", command);
Arjan Opmeer2a0bd752008-10-16 22:10:19 -040072
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)
Dmitry Torokhovd4ae84a82010-05-13 00:41:15 -0700113 pr_err("failed to read register 0x%02x.\n", reg);
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400114 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)
Dmitry Torokhovd4ae84a82010-05-13 00:41:15 -0700159 pr_err("failed to write register 0x%02x with value 0x%02x.\n",
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400160 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
Dmitry Torokhovd4ae84a82010-05-13 00:41:15 -0700172 printk(KERN_DEBUG pr_fmt("PS/2 packet ["));
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400173 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
Dmitry Torokhov504e8be2010-05-13 00:41:15 -0700189 if (etd->fw_version < 0x020000) {
Florian Ragwitze938fbf2010-05-03 23:29:37 -0700190 /*
191 * byte 0: D U p1 p2 1 p3 R L
192 * byte 1: f 0 th tw x9 x8 y9 y8
193 */
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400194 fingers = ((packet[1] & 0x80) >> 7) +
195 ((packet[1] & 0x30) >> 4);
196 } else {
Florian Ragwitze938fbf2010-05-03 23:29:37 -0700197 /*
198 * byte 0: n1 n0 p2 p1 1 p3 R L
199 * byte 1: 0 0 0 0 x9 x8 y9 y8
200 */
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400201 fingers = (packet[0] & 0xc0) >> 6;
202 }
203
Arjan Opmeer3f8c0df2009-04-18 19:05:40 -0700204 if (etd->jumpy_cursor) {
Éric Piel7f29f172010-08-05 23:51:49 -0700205 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++;
Dmitry Torokhovd4ae84a82010-05-13 00:41:15 -0700210 elantech_debug("discarding packet\n");
Éric Piel7f29f172010-08-05 23:51:49 -0700211 return;
Arjan Opmeer3f8c0df2009-04-18 19:05:40 -0700212 }
213 }
214
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400215 input_report_key(dev, BTN_TOUCH, fingers != 0);
216
Florian Ragwitze938fbf2010-05-03 23:29:37 -0700217 /*
218 * byte 2: x7 x6 x5 x4 x3 x2 x1 x0
219 * byte 3: y7 y6 y5 y4 y3 y2 y1 y0
220 */
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400221 if (fingers) {
222 input_report_abs(dev, ABS_X,
223 ((packet[1] & 0x0c) << 6) | packet[2]);
Florian Ragwitze938fbf2010-05-03 23:29:37 -0700224 input_report_abs(dev, ABS_Y,
225 ETP_YMAX_V1 - (((packet[1] & 0x03) << 8) | packet[3]));
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400226 }
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
Dmitry Torokhov504e8be2010-05-13 00:41:15 -0700234 if (etd->fw_version < 0x020000 &&
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400235 (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{
251 struct input_dev *dev = psmouse->dev;
252 unsigned char *packet = psmouse->packet;
253 int fingers, x1, y1, x2, y2;
254
255 /* byte 0: n1 n0 . . . . R L */
256 fingers = (packet[0] & 0xc0) >> 6;
257 input_report_key(dev, BTN_TOUCH, fingers != 0);
258
259 switch (fingers) {
260 case 1:
Florian Ragwitze938fbf2010-05-03 23:29:37 -0700261 /*
262 * byte 1: . . . . . x10 x9 x8
263 * byte 2: x7 x6 x5 x4 x4 x2 x1 x0
264 */
265 input_report_abs(dev, ABS_X,
266 ((packet[1] & 0x07) << 8) | packet[2]);
267 /*
268 * byte 4: . . . . . . y9 y8
269 * byte 5: y7 y6 y5 y4 y3 y2 y1 y0
270 */
271 input_report_abs(dev, ABS_Y,
272 ETP_YMAX_V2 - (((packet[4] & 0x03) << 8) | packet[5]));
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400273 break;
274
275 case 2:
Florian Ragwitze938fbf2010-05-03 23:29:37 -0700276 /*
277 * The coordinate of each finger is reported separately
278 * with a lower resolution for two finger touches:
279 * byte 0: . . ay8 ax8 . . . .
280 * byte 1: ax7 ax6 ax5 ax4 ax3 ax2 ax1 ax0
281 */
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400282 x1 = ((packet[0] & 0x10) << 4) | packet[1];
283 /* byte 2: ay7 ay6 ay5 ay4 ay3 ay2 ay1 ay0 */
284 y1 = ETP_2FT_YMAX - (((packet[0] & 0x20) << 3) | packet[2]);
Florian Ragwitze938fbf2010-05-03 23:29:37 -0700285 /*
286 * byte 3: . . by8 bx8 . . . .
287 * byte 4: bx7 bx6 bx5 bx4 bx3 bx2 bx1 bx0
288 */
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400289 x2 = ((packet[3] & 0x10) << 4) | packet[4];
290 /* byte 5: by7 by8 by5 by4 by3 by2 by1 by0 */
291 y2 = ETP_2FT_YMAX - (((packet[3] & 0x20) << 3) | packet[5]);
Florian Ragwitze938fbf2010-05-03 23:29:37 -0700292 /*
293 * For compatibility with the X Synaptics driver scale up
294 * one coordinate and report as ordinary mouse movent
295 */
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400296 input_report_abs(dev, ABS_X, x1 << 2);
297 input_report_abs(dev, ABS_Y, y1 << 2);
Florian Ragwitze938fbf2010-05-03 23:29:37 -0700298 /*
299 * For compatibility with the proprietary X Elantech driver
300 * report both coordinates as hat coordinates
301 */
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400302 input_report_abs(dev, ABS_HAT0X, x1);
303 input_report_abs(dev, ABS_HAT0Y, y1);
304 input_report_abs(dev, ABS_HAT1X, x2);
305 input_report_abs(dev, ABS_HAT1Y, y2);
306 break;
307 }
308
309 input_report_key(dev, BTN_TOOL_FINGER, fingers == 1);
310 input_report_key(dev, BTN_TOOL_DOUBLETAP, fingers == 2);
311 input_report_key(dev, BTN_TOOL_TRIPLETAP, fingers == 3);
312 input_report_key(dev, BTN_LEFT, packet[0] & 0x01);
313 input_report_key(dev, BTN_RIGHT, packet[0] & 0x02);
314
315 input_sync(dev);
316}
317
318static int elantech_check_parity_v1(struct psmouse *psmouse)
319{
320 struct elantech_data *etd = psmouse->private;
321 unsigned char *packet = psmouse->packet;
322 unsigned char p1, p2, p3;
323
324 /* Parity bits are placed differently */
Dmitry Torokhov504e8be2010-05-13 00:41:15 -0700325 if (etd->fw_version < 0x020000) {
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400326 /* byte 0: D U p1 p2 1 p3 R L */
327 p1 = (packet[0] & 0x20) >> 5;
328 p2 = (packet[0] & 0x10) >> 4;
329 } else {
330 /* byte 0: n1 n0 p2 p1 1 p3 R L */
331 p1 = (packet[0] & 0x10) >> 4;
332 p2 = (packet[0] & 0x20) >> 5;
333 }
334
335 p3 = (packet[0] & 0x04) >> 2;
336
337 return etd->parity[packet[1]] == p1 &&
338 etd->parity[packet[2]] == p2 &&
339 etd->parity[packet[3]] == p3;
340}
341
342/*
343 * Process byte stream from mouse and handle complete packets
344 */
345static psmouse_ret_t elantech_process_byte(struct psmouse *psmouse)
346{
347 struct elantech_data *etd = psmouse->private;
348
349 if (psmouse->pktcnt < psmouse->pktsize)
350 return PSMOUSE_GOOD_DATA;
351
352 if (etd->debug > 1)
353 elantech_packet_dump(psmouse->packet, psmouse->pktsize);
354
355 switch (etd->hw_version) {
356 case 1:
357 if (etd->paritycheck && !elantech_check_parity_v1(psmouse))
358 return PSMOUSE_BAD_DATA;
359
360 elantech_report_absolute_v1(psmouse);
361 break;
362
363 case 2:
364 /* We don't know how to check parity in protocol v2 */
365 elantech_report_absolute_v2(psmouse);
366 break;
367 }
368
369 return PSMOUSE_FULL_PACKET;
370}
371
372/*
373 * Put the touchpad into absolute mode
374 */
375static int elantech_set_absolute_mode(struct psmouse *psmouse)
376{
377 struct elantech_data *etd = psmouse->private;
378 unsigned char val;
379 int tries = ETP_READ_BACK_TRIES;
380 int rc = 0;
381
382 switch (etd->hw_version) {
383 case 1:
384 etd->reg_10 = 0x16;
385 etd->reg_11 = 0x8f;
386 if (elantech_write_reg(psmouse, 0x10, etd->reg_10) ||
387 elantech_write_reg(psmouse, 0x11, etd->reg_11)) {
388 rc = -1;
389 }
390 break;
391
392 case 2:
393 /* Windows driver values */
394 etd->reg_10 = 0x54;
395 etd->reg_11 = 0x88; /* 0x8a */
396 etd->reg_21 = 0x60; /* 0x00 */
397 if (elantech_write_reg(psmouse, 0x10, etd->reg_10) ||
398 elantech_write_reg(psmouse, 0x11, etd->reg_11) ||
399 elantech_write_reg(psmouse, 0x21, etd->reg_21)) {
400 rc = -1;
401 break;
402 }
Arjan Opmeerb2546df2009-04-18 19:10:17 -0700403 }
404
405 if (rc == 0) {
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400406 /*
Arjan Opmeerb2546df2009-04-18 19:10:17 -0700407 * Read back reg 0x10. For hardware version 1 we must make
408 * sure the absolute mode bit is set. For hardware version 2
409 * the touchpad is probably initalising and not ready until
410 * we read back the value we just wrote.
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400411 */
412 do {
413 rc = elantech_read_reg(psmouse, 0x10, &val);
414 if (rc == 0)
415 break;
416 tries--;
Dmitry Torokhovd4ae84a82010-05-13 00:41:15 -0700417 elantech_debug("retrying read (%d).\n", tries);
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400418 msleep(ETP_READ_BACK_DELAY);
419 } while (tries > 0);
Arjan Opmeerb2546df2009-04-18 19:10:17 -0700420
421 if (rc) {
Dmitry Torokhovd4ae84a82010-05-13 00:41:15 -0700422 pr_err("failed to read back register 0x10.\n");
Arjan Opmeerb2546df2009-04-18 19:10:17 -0700423 } else if (etd->hw_version == 1 &&
424 !(val & ETP_R10_ABSOLUTE_MODE)) {
Dmitry Torokhovd4ae84a82010-05-13 00:41:15 -0700425 pr_err("touchpad refuses to switch to absolute mode.\n");
Arjan Opmeerb2546df2009-04-18 19:10:17 -0700426 rc = -1;
427 }
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400428 }
429
430 if (rc)
Dmitry Torokhovd4ae84a82010-05-13 00:41:15 -0700431 pr_err("failed to initialise registers.\n");
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400432
433 return rc;
434}
435
436/*
437 * Set the appropriate event bits for the input subsystem
438 */
439static void elantech_set_input_params(struct psmouse *psmouse)
440{
441 struct input_dev *dev = psmouse->dev;
442 struct elantech_data *etd = psmouse->private;
443
444 __set_bit(EV_KEY, dev->evbit);
445 __set_bit(EV_ABS, dev->evbit);
Dmitry Torokhovc7a1f3c2009-11-16 22:12:21 -0800446 __clear_bit(EV_REL, dev->evbit);
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400447
448 __set_bit(BTN_LEFT, dev->keybit);
449 __set_bit(BTN_RIGHT, dev->keybit);
450
451 __set_bit(BTN_TOUCH, dev->keybit);
452 __set_bit(BTN_TOOL_FINGER, dev->keybit);
453 __set_bit(BTN_TOOL_DOUBLETAP, dev->keybit);
454 __set_bit(BTN_TOOL_TRIPLETAP, dev->keybit);
455
456 switch (etd->hw_version) {
457 case 1:
458 /* Rocker button */
Dmitry Torokhov504e8be2010-05-13 00:41:15 -0700459 if (etd->fw_version < 0x020000 &&
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400460 (etd->capabilities & ETP_CAP_HAS_ROCKER)) {
461 __set_bit(BTN_FORWARD, dev->keybit);
462 __set_bit(BTN_BACK, dev->keybit);
463 }
464 input_set_abs_params(dev, ABS_X, ETP_XMIN_V1, ETP_XMAX_V1, 0, 0);
465 input_set_abs_params(dev, ABS_Y, ETP_YMIN_V1, ETP_YMAX_V1, 0, 0);
466 break;
467
468 case 2:
469 input_set_abs_params(dev, ABS_X, ETP_XMIN_V2, ETP_XMAX_V2, 0, 0);
470 input_set_abs_params(dev, ABS_Y, ETP_YMIN_V2, ETP_YMAX_V2, 0, 0);
471 input_set_abs_params(dev, ABS_HAT0X, ETP_2FT_XMIN, ETP_2FT_XMAX, 0, 0);
472 input_set_abs_params(dev, ABS_HAT0Y, ETP_2FT_YMIN, ETP_2FT_YMAX, 0, 0);
473 input_set_abs_params(dev, ABS_HAT1X, ETP_2FT_XMIN, ETP_2FT_XMAX, 0, 0);
474 input_set_abs_params(dev, ABS_HAT1Y, ETP_2FT_YMIN, ETP_2FT_YMAX, 0, 0);
475 break;
476 }
477}
478
479struct elantech_attr_data {
480 size_t field_offset;
481 unsigned char reg;
482};
483
484/*
485 * Display a register value by reading a sysfs entry
486 */
487static ssize_t elantech_show_int_attr(struct psmouse *psmouse, void *data,
488 char *buf)
489{
490 struct elantech_data *etd = psmouse->private;
491 struct elantech_attr_data *attr = data;
492 unsigned char *reg = (unsigned char *) etd + attr->field_offset;
493 int rc = 0;
494
495 if (attr->reg)
496 rc = elantech_read_reg(psmouse, attr->reg, reg);
497
498 return sprintf(buf, "0x%02x\n", (attr->reg && rc) ? -1 : *reg);
499}
500
501/*
502 * Write a register value by writing a sysfs entry
503 */
504static ssize_t elantech_set_int_attr(struct psmouse *psmouse,
505 void *data, const char *buf, size_t count)
506{
507 struct elantech_data *etd = psmouse->private;
508 struct elantech_attr_data *attr = data;
509 unsigned char *reg = (unsigned char *) etd + attr->field_offset;
510 unsigned long value;
511 int err;
512
513 err = strict_strtoul(buf, 16, &value);
514 if (err)
515 return err;
516
517 if (value > 0xff)
518 return -EINVAL;
519
520 /* Do we need to preserve some bits for version 2 hardware too? */
521 if (etd->hw_version == 1) {
522 if (attr->reg == 0x10)
523 /* Force absolute mode always on */
524 value |= ETP_R10_ABSOLUTE_MODE;
525 else if (attr->reg == 0x11)
526 /* Force 4 byte mode always on */
527 value |= ETP_R11_4_BYTE_MODE;
528 }
529
530 if (!attr->reg || elantech_write_reg(psmouse, attr->reg, value) == 0)
531 *reg = value;
532
533 return count;
534}
535
536#define ELANTECH_INT_ATTR(_name, _register) \
537 static struct elantech_attr_data elantech_attr_##_name = { \
538 .field_offset = offsetof(struct elantech_data, _name), \
539 .reg = _register, \
540 }; \
541 PSMOUSE_DEFINE_ATTR(_name, S_IWUSR | S_IRUGO, \
542 &elantech_attr_##_name, \
543 elantech_show_int_attr, \
544 elantech_set_int_attr)
545
546ELANTECH_INT_ATTR(reg_10, 0x10);
547ELANTECH_INT_ATTR(reg_11, 0x11);
548ELANTECH_INT_ATTR(reg_20, 0x20);
549ELANTECH_INT_ATTR(reg_21, 0x21);
550ELANTECH_INT_ATTR(reg_22, 0x22);
551ELANTECH_INT_ATTR(reg_23, 0x23);
552ELANTECH_INT_ATTR(reg_24, 0x24);
553ELANTECH_INT_ATTR(reg_25, 0x25);
554ELANTECH_INT_ATTR(reg_26, 0x26);
555ELANTECH_INT_ATTR(debug, 0);
556ELANTECH_INT_ATTR(paritycheck, 0);
557
558static struct attribute *elantech_attrs[] = {
559 &psmouse_attr_reg_10.dattr.attr,
560 &psmouse_attr_reg_11.dattr.attr,
561 &psmouse_attr_reg_20.dattr.attr,
562 &psmouse_attr_reg_21.dattr.attr,
563 &psmouse_attr_reg_22.dattr.attr,
564 &psmouse_attr_reg_23.dattr.attr,
565 &psmouse_attr_reg_24.dattr.attr,
566 &psmouse_attr_reg_25.dattr.attr,
567 &psmouse_attr_reg_26.dattr.attr,
568 &psmouse_attr_debug.dattr.attr,
569 &psmouse_attr_paritycheck.dattr.attr,
570 NULL
571};
572
573static struct attribute_group elantech_attr_group = {
574 .attrs = elantech_attrs,
575};
576
Dmitry Torokhova0836322010-05-19 10:11:13 -0700577static bool elantech_is_signature_valid(const unsigned char *param)
578{
579 static const unsigned char rates[] = { 200, 100, 80, 60, 40, 20, 10 };
580 int i;
581
582 if (param[0] == 0)
583 return false;
584
585 if (param[1] == 0)
586 return true;
587
588 for (i = 0; i < ARRAY_SIZE(rates); i++)
589 if (param[2] == rates[i])
590 return false;
591
592 return true;
593}
594
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400595/*
596 * Use magic knock to detect Elantech touchpad
597 */
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700598int elantech_detect(struct psmouse *psmouse, bool set_properties)
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400599{
600 struct ps2dev *ps2dev = &psmouse->ps2dev;
601 unsigned char param[3];
602
603 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
604
605 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
606 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
607 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
608 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
609 ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO)) {
Dmitry Torokhovd4ae84a82010-05-13 00:41:15 -0700610 pr_debug("sending Elantech magic knock failed.\n");
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400611 return -1;
612 }
613
614 /*
615 * Report this in case there are Elantech models that use a different
616 * set of magic numbers
617 */
618 if (param[0] != 0x3c || param[1] != 0x03 || param[2] != 0xc8) {
Dmitry Torokhovd4ae84a82010-05-13 00:41:15 -0700619 pr_debug("unexpected magic knock result 0x%02x, 0x%02x, 0x%02x.\n",
Arjan Opmeer9ab7b252009-02-28 13:52:40 -0800620 param[0], param[1], param[2]);
621 return -1;
622 }
623
624 /*
625 * Query touchpad's firmware version and see if it reports known
626 * value to avoid mis-detection. Logitech mice are known to respond
627 * to Elantech magic knock and there might be more.
628 */
629 if (synaptics_send_cmd(psmouse, ETP_FW_VERSION_QUERY, param)) {
Dmitry Torokhovd4ae84a82010-05-13 00:41:15 -0700630 pr_debug("failed to query firmware version.\n");
Arjan Opmeer9ab7b252009-02-28 13:52:40 -0800631 return -1;
632 }
633
Dmitry Torokhovd4ae84a82010-05-13 00:41:15 -0700634 pr_debug("Elantech version query result 0x%02x, 0x%02x, 0x%02x.\n",
Arjan Opmeer9ab7b252009-02-28 13:52:40 -0800635 param[0], param[1], param[2]);
636
Dmitry Torokhova0836322010-05-19 10:11:13 -0700637 if (!elantech_is_signature_valid(param)) {
Florian Ragwitzf81bc782010-04-27 00:47:04 -0700638 if (!force_elantech) {
Dmitry Torokhovd4ae84a82010-05-13 00:41:15 -0700639 pr_debug("Probably not a real Elantech touchpad. Aborting.\n");
Florian Ragwitzf81bc782010-04-27 00:47:04 -0700640 return -1;
641 }
642
Dmitry Torokhovd4ae84a82010-05-13 00:41:15 -0700643 pr_debug("Probably not a real Elantech touchpad. Enabling anyway due to force_elantech.\n");
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400644 }
645
646 if (set_properties) {
647 psmouse->vendor = "Elantech";
648 psmouse->name = "Touchpad";
649 }
650
651 return 0;
652}
653
654/*
655 * Clean up sysfs entries when disconnecting
656 */
657static void elantech_disconnect(struct psmouse *psmouse)
658{
659 sysfs_remove_group(&psmouse->ps2dev.serio->dev.kobj,
660 &elantech_attr_group);
661 kfree(psmouse->private);
662 psmouse->private = NULL;
663}
664
665/*
666 * Put the touchpad back into absolute mode when reconnecting
667 */
668static int elantech_reconnect(struct psmouse *psmouse)
669{
670 if (elantech_detect(psmouse, 0))
671 return -1;
672
673 if (elantech_set_absolute_mode(psmouse)) {
Dmitry Torokhovd4ae84a82010-05-13 00:41:15 -0700674 pr_err("failed to put touchpad back into absolute mode.\n");
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400675 return -1;
676 }
677
678 return 0;
679}
680
681/*
682 * Initialize the touchpad and create sysfs entries
683 */
684int elantech_init(struct psmouse *psmouse)
685{
686 struct elantech_data *etd;
687 int i, error;
688 unsigned char param[3];
689
Arjan Opmeer9ab7b252009-02-28 13:52:40 -0800690 psmouse->private = etd = kzalloc(sizeof(struct elantech_data), GFP_KERNEL);
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400691 if (!etd)
692 return -1;
693
694 etd->parity[0] = 1;
695 for (i = 1; i < 256; i++)
696 etd->parity[i] = etd->parity[i & (i - 1)] ^ 1;
697
698 /*
Arjan Opmeer9ab7b252009-02-28 13:52:40 -0800699 * Do the version query again so we can store the result
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400700 */
701 if (synaptics_send_cmd(psmouse, ETP_FW_VERSION_QUERY, param)) {
Dmitry Torokhovd4ae84a82010-05-13 00:41:15 -0700702 pr_err("failed to query firmware version.\n");
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400703 goto init_fail;
704 }
Dmitry Torokhov504e8be2010-05-13 00:41:15 -0700705
706 etd->fw_version = (param[0] << 16) | (param[1] << 8) | param[2];
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400707
708 /*
709 * Assume every version greater than this is new EeePC style
710 * hardware with 6 byte packets
711 */
Dmitry Torokhov504e8be2010-05-13 00:41:15 -0700712 if (etd->fw_version >= 0x020030) {
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400713 etd->hw_version = 2;
714 /* For now show extra debug information */
715 etd->debug = 1;
716 /* Don't know how to do parity checking for version 2 */
717 etd->paritycheck = 0;
718 } else {
719 etd->hw_version = 1;
720 etd->paritycheck = 1;
721 }
Dmitry Torokhov504e8be2010-05-13 00:41:15 -0700722
Dmitry Torokhovd4ae84a82010-05-13 00:41:15 -0700723 pr_info("assuming hardware version %d, firmware version %d.%d.%d\n",
Dmitry Torokhov504e8be2010-05-13 00:41:15 -0700724 etd->hw_version, param[0], param[1], param[2]);
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400725
726 if (synaptics_send_cmd(psmouse, ETP_CAPABILITIES_QUERY, param)) {
Dmitry Torokhovd4ae84a82010-05-13 00:41:15 -0700727 pr_err("failed to query capabilities.\n");
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400728 goto init_fail;
729 }
Dmitry Torokhovd4ae84a82010-05-13 00:41:15 -0700730 pr_info("Synaptics capabilities query result 0x%02x, 0x%02x, 0x%02x.\n",
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400731 param[0], param[1], param[2]);
732 etd->capabilities = param[0];
733
Arjan Opmeer3f8c0df2009-04-18 19:05:40 -0700734 /*
Éric Piel7f29f172010-08-05 23:51:49 -0700735 * This firmware suffers from misreporting coordinates when
Arjan Opmeer3f8c0df2009-04-18 19:05:40 -0700736 * a touch action starts causing the mouse cursor or scrolled page
737 * to jump. Enable a workaround.
738 */
Éric Piel7f29f172010-08-05 23:51:49 -0700739 if (etd->fw_version == 0x020022 || etd->fw_version == 0x020600) {
740 pr_info("firmware version 2.0.34/2.6.0 detected, enabling jumpy cursor workaround\n");
741 etd->jumpy_cursor = true;
Arjan Opmeer3f8c0df2009-04-18 19:05:40 -0700742 }
743
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400744 if (elantech_set_absolute_mode(psmouse)) {
Dmitry Torokhovd4ae84a82010-05-13 00:41:15 -0700745 pr_err("failed to put touchpad into absolute mode.\n");
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400746 goto init_fail;
747 }
748
749 elantech_set_input_params(psmouse);
750
751 error = sysfs_create_group(&psmouse->ps2dev.serio->dev.kobj,
752 &elantech_attr_group);
753 if (error) {
Dmitry Torokhovd4ae84a82010-05-13 00:41:15 -0700754 pr_err("failed to create sysfs attributes, error: %d.\n", error);
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400755 goto init_fail;
756 }
757
758 psmouse->protocol_handler = elantech_process_byte;
759 psmouse->disconnect = elantech_disconnect;
760 psmouse->reconnect = elantech_reconnect;
761 psmouse->pktsize = etd->hw_version == 2 ? 6 : 4;
762
763 return 0;
764
765 init_fail:
766 kfree(etd);
767 return -1;
768}