]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/dde-libinput/src/contrib/mouse/elantech.c
Inital import
[l4.git] / l4 / pkg / dde-libinput / src / contrib / mouse / elantech.c
1 /*
2  * Elantech Touchpad driver (v5)
3  *
4  * Copyright (C) 2007-2008 Arjan Opmeer <arjan@opmeer.net>
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
13 #include <linux/delay.h>
14 #include <linux/module.h>
15 #include <linux/input.h>
16 #include <linux/serio.h>
17 #include <linux/libps2.h>
18 #include "psmouse.h"
19 #include "elantech.h"
20
21 #define elantech_debug(format, arg...)                          \
22         do {                                                    \
23                 if (etd->debug)                                 \
24                         printk(KERN_DEBUG format, ##arg);       \
25         } while (0)
26
27 /*
28  * Send a Synaptics style sliced query command
29  */
30 static int synaptics_send_cmd(struct psmouse *psmouse, unsigned char c,
31                                 unsigned char *param)
32 {
33         if (psmouse_sliced_command(psmouse, c) ||
34             ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_GETINFO)) {
35                 pr_err("elantech.c: synaptics_send_cmd query 0x%02x failed.\n", c);
36                 return -1;
37         }
38
39         return 0;
40 }
41
42 /*
43  * A retrying version of ps2_command
44  */
45 static int elantech_ps2_command(struct psmouse *psmouse,
46                                 unsigned char *param, int command)
47 {
48         struct ps2dev *ps2dev = &psmouse->ps2dev;
49         struct elantech_data *etd = psmouse->private;
50         int rc;
51         int tries = ETP_PS2_COMMAND_TRIES;
52
53         do {
54                 rc = ps2_command(ps2dev, param, command);
55                 if (rc == 0)
56                         break;
57                 tries--;
58                 elantech_debug("elantech.c: retrying ps2 command 0x%02x (%d).\n",
59                         command, tries);
60                 msleep(ETP_PS2_COMMAND_DELAY);
61         } while (tries > 0);
62
63         if (rc)
64                 pr_err("elantech.c: ps2 command 0x%02x failed.\n", command);
65
66         return rc;
67 }
68
69 /*
70  * Send an Elantech style special command to read a value from a register
71  */
72 static int elantech_read_reg(struct psmouse *psmouse, unsigned char reg,
73                                 unsigned char *val)
74 {
75         struct elantech_data *etd = psmouse->private;
76         unsigned char param[3];
77         int rc = 0;
78
79         if (reg < 0x10 || reg > 0x26)
80                 return -1;
81
82         if (reg > 0x11 && reg < 0x20)
83                 return -1;
84
85         switch (etd->hw_version) {
86         case 1:
87                 if (psmouse_sliced_command(psmouse, ETP_REGISTER_READ) ||
88                     psmouse_sliced_command(psmouse, reg) ||
89                     ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_GETINFO)) {
90                         rc = -1;
91                 }
92                 break;
93
94         case 2:
95                 if (elantech_ps2_command(psmouse,  NULL, ETP_PS2_CUSTOM_COMMAND) ||
96                     elantech_ps2_command(psmouse,  NULL, ETP_REGISTER_READ) ||
97                     elantech_ps2_command(psmouse,  NULL, ETP_PS2_CUSTOM_COMMAND) ||
98                     elantech_ps2_command(psmouse,  NULL, reg) ||
99                     elantech_ps2_command(psmouse, param, PSMOUSE_CMD_GETINFO)) {
100                         rc = -1;
101                 }
102                 break;
103         }
104
105         if (rc)
106                 pr_err("elantech.c: failed to read register 0x%02x.\n", reg);
107         else
108                 *val = param[0];
109
110         return rc;
111 }
112
113 /*
114  * Send an Elantech style special command to write a register with a value
115  */
116 static int elantech_write_reg(struct psmouse *psmouse, unsigned char reg,
117                                 unsigned char val)
118 {
119         struct elantech_data *etd = psmouse->private;
120         int rc = 0;
121
122         if (reg < 0x10 || reg > 0x26)
123                 return -1;
124
125         if (reg > 0x11 && reg < 0x20)
126                 return -1;
127
128         switch (etd->hw_version) {
129         case 1:
130                 if (psmouse_sliced_command(psmouse, ETP_REGISTER_WRITE) ||
131                     psmouse_sliced_command(psmouse, reg) ||
132                     psmouse_sliced_command(psmouse, val) ||
133                     ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11)) {
134                         rc = -1;
135                 }
136                 break;
137
138         case 2:
139                 if (elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
140                     elantech_ps2_command(psmouse, NULL, ETP_REGISTER_WRITE) ||
141                     elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
142                     elantech_ps2_command(psmouse, NULL, reg) ||
143                     elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
144                     elantech_ps2_command(psmouse, NULL, val) ||
145                     elantech_ps2_command(psmouse, NULL, PSMOUSE_CMD_SETSCALE11)) {
146                         rc = -1;
147                 }
148                 break;
149         }
150
151         if (rc)
152                 pr_err("elantech.c: failed to write register 0x%02x with value 0x%02x.\n",
153                         reg, val);
154
155         return rc;
156 }
157
158 /*
159  * Dump a complete mouse movement packet to the syslog
160  */
161 static void elantech_packet_dump(unsigned char *packet, int size)
162 {
163         int     i;
164
165         printk(KERN_DEBUG "elantech.c: PS/2 packet [");
166         for (i = 0; i < size; i++)
167                 printk("%s0x%02x ", (i) ? ", " : " ", packet[i]);
168         printk("]\n");
169 }
170
171 /*
172  * Interpret complete data packets and report absolute mode input events for
173  * hardware version 1. (4 byte packets)
174  */
175 static void elantech_report_absolute_v1(struct psmouse *psmouse)
176 {
177         struct input_dev *dev = psmouse->dev;
178         struct elantech_data *etd = psmouse->private;
179         unsigned char *packet = psmouse->packet;
180         int fingers;
181
182         if (etd->fw_version_maj == 0x01) {
183                 /* byte 0:  D   U  p1  p2   1  p3   R   L
184                    byte 1:  f   0  th  tw  x9  x8  y9  y8 */
185                 fingers = ((packet[1] & 0x80) >> 7) +
186                                 ((packet[1] & 0x30) >> 4);
187         } else {
188                 /* byte 0: n1  n0  p2  p1   1  p3   R   L
189                    byte 1:  0   0   0   0  x9  x8  y9  y8 */
190                 fingers = (packet[0] & 0xc0) >> 6;
191         }
192
193         input_report_key(dev, BTN_TOUCH, fingers != 0);
194
195         /* byte 2: x7  x6  x5  x4  x3  x2  x1  x0
196            byte 3: y7  y6  y5  y4  y3  y2  y1  y0 */
197         if (fingers) {
198                 input_report_abs(dev, ABS_X,
199                         ((packet[1] & 0x0c) << 6) | packet[2]);
200                 input_report_abs(dev, ABS_Y, ETP_YMAX_V1 -
201                         (((packet[1] & 0x03) << 8) | packet[3]));
202         }
203
204         input_report_key(dev, BTN_TOOL_FINGER, fingers == 1);
205         input_report_key(dev, BTN_TOOL_DOUBLETAP, fingers == 2);
206         input_report_key(dev, BTN_TOOL_TRIPLETAP, fingers == 3);
207         input_report_key(dev, BTN_LEFT, packet[0] & 0x01);
208         input_report_key(dev, BTN_RIGHT, packet[0] & 0x02);
209
210         if ((etd->fw_version_maj == 0x01) &&
211             (etd->capabilities & ETP_CAP_HAS_ROCKER)) {
212                 /* rocker up */
213                 input_report_key(dev, BTN_FORWARD, packet[0] & 0x40);
214                 /* rocker down */
215                 input_report_key(dev, BTN_BACK, packet[0] & 0x80);
216         }
217
218         input_sync(dev);
219 }
220
221 /*
222  * Interpret complete data packets and report absolute mode input events for
223  * hardware version 2. (6 byte packets)
224  */
225 static void elantech_report_absolute_v2(struct psmouse *psmouse)
226 {
227         struct input_dev *dev = psmouse->dev;
228         unsigned char *packet = psmouse->packet;
229         int fingers, x1, y1, x2, y2;
230
231         /* byte 0: n1  n0   .   .   .   .   R   L */
232         fingers = (packet[0] & 0xc0) >> 6;
233         input_report_key(dev, BTN_TOUCH, fingers != 0);
234
235         switch (fingers) {
236         case 1:
237                 /* byte 1: x15 x14 x13 x12 x11 x10 x9  x8
238                    byte 2: x7  x6  x5  x4  x4  x2  x1  x0 */
239                 input_report_abs(dev, ABS_X, (packet[1] << 8) | packet[2]);
240                 /* byte 4: y15 y14 y13 y12 y11 y10 y8  y8
241                    byte 5: y7  y6  y5  y4  y3  y2  y1  y0 */
242                 input_report_abs(dev, ABS_Y, ETP_YMAX_V2 -
243                         ((packet[4] << 8) | packet[5]));
244                 break;
245
246         case 2:
247                 /* The coordinate of each finger is reported separately with
248                    a lower resolution for two finger touches */
249                 /* byte 0:  .   .  ay8 ax8  .   .   .   .
250                    byte 1: ax7 ax6 ax5 ax4 ax3 ax2 ax1 ax0 */
251                 x1 = ((packet[0] & 0x10) << 4) | packet[1];
252                 /* byte 2: ay7 ay6 ay5 ay4 ay3 ay2 ay1 ay0 */
253                 y1 = ETP_2FT_YMAX - (((packet[0] & 0x20) << 3) | packet[2]);
254                 /* byte 3:  .   .  by8 bx8  .   .   .   .
255                    byte 4: bx7 bx6 bx5 bx4 bx3 bx2 bx1 bx0 */
256                 x2 = ((packet[3] & 0x10) << 4) | packet[4];
257                 /* byte 5: by7 by8 by5 by4 by3 by2 by1 by0 */
258                 y2 = ETP_2FT_YMAX - (((packet[3] & 0x20) << 3) | packet[5]);
259                 /* For compatibility with the X Synaptics driver scale up one
260                    coordinate and report as ordinary mouse movent */
261                 input_report_abs(dev, ABS_X, x1 << 2);
262                 input_report_abs(dev, ABS_Y, y1 << 2);
263                 /* For compatibility with the proprietary X Elantech driver
264                    report both coordinates as hat coordinates */
265                 input_report_abs(dev, ABS_HAT0X, x1);
266                 input_report_abs(dev, ABS_HAT0Y, y1);
267                 input_report_abs(dev, ABS_HAT1X, x2);
268                 input_report_abs(dev, ABS_HAT1Y, y2);
269                 break;
270         }
271
272         input_report_key(dev, BTN_TOOL_FINGER, fingers == 1);
273         input_report_key(dev, BTN_TOOL_DOUBLETAP, fingers == 2);
274         input_report_key(dev, BTN_TOOL_TRIPLETAP, fingers == 3);
275         input_report_key(dev, BTN_LEFT, packet[0] & 0x01);
276         input_report_key(dev, BTN_RIGHT, packet[0] & 0x02);
277
278         input_sync(dev);
279 }
280
281 static int elantech_check_parity_v1(struct psmouse *psmouse)
282 {
283         struct elantech_data *etd = psmouse->private;
284         unsigned char *packet = psmouse->packet;
285         unsigned char p1, p2, p3;
286
287         /* Parity bits are placed differently */
288         if (etd->fw_version_maj == 0x01) {
289                 /* byte 0:  D   U  p1  p2   1  p3   R   L */
290                 p1 = (packet[0] & 0x20) >> 5;
291                 p2 = (packet[0] & 0x10) >> 4;
292         } else {
293                 /* byte 0: n1  n0  p2  p1   1  p3   R   L */
294                 p1 = (packet[0] & 0x10) >> 4;
295                 p2 = (packet[0] & 0x20) >> 5;
296         }
297
298         p3 = (packet[0] & 0x04) >> 2;
299
300         return etd->parity[packet[1]] == p1 &&
301                etd->parity[packet[2]] == p2 &&
302                etd->parity[packet[3]] == p3;
303 }
304
305 /*
306  * Process byte stream from mouse and handle complete packets
307  */
308 static psmouse_ret_t elantech_process_byte(struct psmouse *psmouse)
309 {
310         struct elantech_data *etd = psmouse->private;
311
312         if (psmouse->pktcnt < psmouse->pktsize)
313                 return PSMOUSE_GOOD_DATA;
314
315         if (etd->debug > 1)
316                 elantech_packet_dump(psmouse->packet, psmouse->pktsize);
317
318         switch (etd->hw_version) {
319         case 1:
320                 if (etd->paritycheck && !elantech_check_parity_v1(psmouse))
321                         return PSMOUSE_BAD_DATA;
322
323                 elantech_report_absolute_v1(psmouse);
324                 break;
325
326         case 2:
327                 /* We don't know how to check parity in protocol v2 */
328                 elantech_report_absolute_v2(psmouse);
329                 break;
330         }
331
332         return PSMOUSE_FULL_PACKET;
333 }
334
335 /*
336  * Put the touchpad into absolute mode
337  */
338 static int elantech_set_absolute_mode(struct psmouse *psmouse)
339 {
340         struct elantech_data *etd = psmouse->private;
341         unsigned char val;
342         int tries = ETP_READ_BACK_TRIES;
343         int rc = 0;
344
345         switch (etd->hw_version) {
346         case 1:
347                 etd->reg_10 = 0x16;
348                 etd->reg_11 = 0x8f;
349                 if (elantech_write_reg(psmouse, 0x10, etd->reg_10) ||
350                     elantech_write_reg(psmouse, 0x11, etd->reg_11)) {
351                         rc = -1;
352                 }
353                 break;
354
355         case 2:
356                                         /* Windows driver values */
357                 etd->reg_10 = 0x54;
358                 etd->reg_11 = 0x88;     /* 0x8a */
359                 etd->reg_21 = 0x60;     /* 0x00 */
360                 if (elantech_write_reg(psmouse, 0x10, etd->reg_10) ||
361                     elantech_write_reg(psmouse, 0x11, etd->reg_11) ||
362                     elantech_write_reg(psmouse, 0x21, etd->reg_21)) {
363                         rc = -1;
364                         break;
365                 }
366                 /*
367                  * Read back reg 0x10. The touchpad is probably initalising
368                  * and not ready until we read back the value we just wrote.
369                  */
370                 do {
371                         rc = elantech_read_reg(psmouse, 0x10, &val);
372                         if (rc == 0)
373                                 break;
374                         tries--;
375                         elantech_debug("elantech.c: retrying read (%d).\n",
376                                 tries);
377                         msleep(ETP_READ_BACK_DELAY);
378                 } while (tries > 0);
379                 if (rc)
380                         pr_err("elantech.c: failed to read back register 0x10.\n");
381                 break;
382         }
383
384         if (rc)
385                 pr_err("elantech.c: failed to initialise registers.\n");
386
387         return rc;
388 }
389
390 /*
391  * Set the appropriate event bits for the input subsystem
392  */
393 static void elantech_set_input_params(struct psmouse *psmouse)
394 {
395         struct input_dev *dev = psmouse->dev;
396         struct elantech_data *etd = psmouse->private;
397
398         __set_bit(EV_KEY, dev->evbit);
399         __set_bit(EV_ABS, dev->evbit);
400
401         __set_bit(BTN_LEFT, dev->keybit);
402         __set_bit(BTN_RIGHT, dev->keybit);
403
404         __set_bit(BTN_TOUCH, dev->keybit);
405         __set_bit(BTN_TOOL_FINGER, dev->keybit);
406         __set_bit(BTN_TOOL_DOUBLETAP, dev->keybit);
407         __set_bit(BTN_TOOL_TRIPLETAP, dev->keybit);
408
409         switch (etd->hw_version) {
410         case 1:
411                 /* Rocker button */
412                 if ((etd->fw_version_maj == 0x01) &&
413                     (etd->capabilities & ETP_CAP_HAS_ROCKER)) {
414                         __set_bit(BTN_FORWARD, dev->keybit);
415                         __set_bit(BTN_BACK, dev->keybit);
416                 }
417                 input_set_abs_params(dev, ABS_X, ETP_XMIN_V1, ETP_XMAX_V1, 0, 0);
418                 input_set_abs_params(dev, ABS_Y, ETP_YMIN_V1, ETP_YMAX_V1, 0, 0);
419                 break;
420
421         case 2:
422                 input_set_abs_params(dev, ABS_X, ETP_XMIN_V2, ETP_XMAX_V2, 0, 0);
423                 input_set_abs_params(dev, ABS_Y, ETP_YMIN_V2, ETP_YMAX_V2, 0, 0);
424                 input_set_abs_params(dev, ABS_HAT0X, ETP_2FT_XMIN, ETP_2FT_XMAX, 0, 0);
425                 input_set_abs_params(dev, ABS_HAT0Y, ETP_2FT_YMIN, ETP_2FT_YMAX, 0, 0);
426                 input_set_abs_params(dev, ABS_HAT1X, ETP_2FT_XMIN, ETP_2FT_XMAX, 0, 0);
427                 input_set_abs_params(dev, ABS_HAT1Y, ETP_2FT_YMIN, ETP_2FT_YMAX, 0, 0);
428                 break;
429         }
430 }
431
432 struct elantech_attr_data {
433         size_t          field_offset;
434         unsigned char   reg;
435 };
436
437 /*
438  * Display a register value by reading a sysfs entry
439  */
440 static ssize_t elantech_show_int_attr(struct psmouse *psmouse, void *data,
441                                         char *buf)
442 {
443         struct elantech_data *etd = psmouse->private;
444         struct elantech_attr_data *attr = data;
445         unsigned char *reg = (unsigned char *) etd + attr->field_offset;
446         int rc = 0;
447
448         if (attr->reg)
449                 rc = elantech_read_reg(psmouse, attr->reg, reg);
450
451         return sprintf(buf, "0x%02x\n", (attr->reg && rc) ? -1 : *reg);
452 }
453
454 /*
455  * Write a register value by writing a sysfs entry
456  */
457 static ssize_t elantech_set_int_attr(struct psmouse *psmouse,
458                                      void *data, const char *buf, size_t count)
459 {
460         struct elantech_data *etd = psmouse->private;
461         struct elantech_attr_data *attr = data;
462         unsigned char *reg = (unsigned char *) etd + attr->field_offset;
463         unsigned long value;
464         int err;
465
466         err = strict_strtoul(buf, 16, &value);
467         if (err)
468                 return err;
469
470         if (value > 0xff)
471                 return -EINVAL;
472
473         /* Do we need to preserve some bits for version 2 hardware too? */
474         if (etd->hw_version == 1) {
475                 if (attr->reg == 0x10)
476                         /* Force absolute mode always on */
477                         value |= ETP_R10_ABSOLUTE_MODE;
478                 else if (attr->reg == 0x11)
479                         /* Force 4 byte mode always on */
480                         value |= ETP_R11_4_BYTE_MODE;
481         }
482
483         if (!attr->reg || elantech_write_reg(psmouse, attr->reg, value) == 0)
484                 *reg = value;
485
486         return count;
487 }
488
489 #define ELANTECH_INT_ATTR(_name, _register)                             \
490         static struct elantech_attr_data elantech_attr_##_name = {      \
491                 .field_offset = offsetof(struct elantech_data, _name),  \
492                 .reg = _register,                                       \
493         };                                                              \
494         PSMOUSE_DEFINE_ATTR(_name, S_IWUSR | S_IRUGO,                   \
495                             &elantech_attr_##_name,                     \
496                             elantech_show_int_attr,                     \
497                             elantech_set_int_attr)
498
499 ELANTECH_INT_ATTR(reg_10, 0x10);
500 ELANTECH_INT_ATTR(reg_11, 0x11);
501 ELANTECH_INT_ATTR(reg_20, 0x20);
502 ELANTECH_INT_ATTR(reg_21, 0x21);
503 ELANTECH_INT_ATTR(reg_22, 0x22);
504 ELANTECH_INT_ATTR(reg_23, 0x23);
505 ELANTECH_INT_ATTR(reg_24, 0x24);
506 ELANTECH_INT_ATTR(reg_25, 0x25);
507 ELANTECH_INT_ATTR(reg_26, 0x26);
508 ELANTECH_INT_ATTR(debug, 0);
509 ELANTECH_INT_ATTR(paritycheck, 0);
510
511 static struct attribute *elantech_attrs[] = {
512         &psmouse_attr_reg_10.dattr.attr,
513         &psmouse_attr_reg_11.dattr.attr,
514         &psmouse_attr_reg_20.dattr.attr,
515         &psmouse_attr_reg_21.dattr.attr,
516         &psmouse_attr_reg_22.dattr.attr,
517         &psmouse_attr_reg_23.dattr.attr,
518         &psmouse_attr_reg_24.dattr.attr,
519         &psmouse_attr_reg_25.dattr.attr,
520         &psmouse_attr_reg_26.dattr.attr,
521         &psmouse_attr_debug.dattr.attr,
522         &psmouse_attr_paritycheck.dattr.attr,
523         NULL
524 };
525
526 static struct attribute_group elantech_attr_group = {
527         .attrs = elantech_attrs,
528 };
529
530 /*
531  * Use magic knock to detect Elantech touchpad
532  */
533 int elantech_detect(struct psmouse *psmouse, int set_properties)
534 {
535         struct ps2dev *ps2dev = &psmouse->ps2dev;
536         unsigned char param[3];
537
538         ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
539
540         if (ps2_command(ps2dev,  NULL, PSMOUSE_CMD_DISABLE) ||
541             ps2_command(ps2dev,  NULL, PSMOUSE_CMD_SETSCALE11) ||
542             ps2_command(ps2dev,  NULL, PSMOUSE_CMD_SETSCALE11) ||
543             ps2_command(ps2dev,  NULL, PSMOUSE_CMD_SETSCALE11) ||
544             ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO)) {
545                 pr_debug("elantech.c: sending Elantech magic knock failed.\n");
546                 return -1;
547         }
548
549         /*
550          * Report this in case there are Elantech models that use a different
551          * set of magic numbers
552          */
553         if (param[0] != 0x3c || param[1] != 0x03 || param[2] != 0xc8) {
554                 pr_debug("elantech.c: "
555                          "unexpected magic knock result 0x%02x, 0x%02x, 0x%02x.\n",
556                          param[0], param[1], param[2]);
557                 return -1;
558         }
559
560         /*
561          * Query touchpad's firmware version and see if it reports known
562          * value to avoid mis-detection. Logitech mice are known to respond
563          * to Elantech magic knock and there might be more.
564          */
565         if (synaptics_send_cmd(psmouse, ETP_FW_VERSION_QUERY, param)) {
566                 pr_debug("elantech.c: failed to query firmware version.\n");
567                 return -1;
568         }
569
570         pr_debug("elantech.c: Elantech version query result 0x%02x, 0x%02x, 0x%02x.\n",
571                  param[0], param[1], param[2]);
572
573         if (param[0] == 0 || param[1] != 0) {
574                 pr_debug("elantech.c: Probably not a real Elantech touchpad. Aborting.\n");
575                 return -1;
576         }
577
578         if (set_properties) {
579                 psmouse->vendor = "Elantech";
580                 psmouse->name = "Touchpad";
581         }
582
583         return 0;
584 }
585
586 /*
587  * Clean up sysfs entries when disconnecting
588  */
589 static void elantech_disconnect(struct psmouse *psmouse)
590 {
591         sysfs_remove_group(&psmouse->ps2dev.serio->dev.kobj,
592                            &elantech_attr_group);
593         kfree(psmouse->private);
594         psmouse->private = NULL;
595 }
596
597 /*
598  * Put the touchpad back into absolute mode when reconnecting
599  */
600 static int elantech_reconnect(struct psmouse *psmouse)
601 {
602         if (elantech_detect(psmouse, 0))
603                 return -1;
604
605         if (elantech_set_absolute_mode(psmouse)) {
606                 pr_err("elantech.c: failed to put touchpad back into absolute mode.\n");
607                 return -1;
608         }
609
610         return 0;
611 }
612
613 /*
614  * Initialize the touchpad and create sysfs entries
615  */
616 int elantech_init(struct psmouse *psmouse)
617 {
618         struct elantech_data *etd;
619         int i, error;
620         unsigned char param[3];
621
622         psmouse->private = etd = kzalloc(sizeof(struct elantech_data), GFP_KERNEL);
623         if (!etd)
624                 return -1;
625
626         etd->parity[0] = 1;
627         for (i = 1; i < 256; i++)
628                 etd->parity[i] = etd->parity[i & (i - 1)] ^ 1;
629
630         /*
631          * Do the version query again so we can store the result
632          */
633         if (synaptics_send_cmd(psmouse, ETP_FW_VERSION_QUERY, param)) {
634                 pr_err("elantech.c: failed to query firmware version.\n");
635                 goto init_fail;
636         }
637         etd->fw_version_maj = param[0];
638         etd->fw_version_min = param[2];
639
640         /*
641          * Assume every version greater than this is new EeePC style
642          * hardware with 6 byte packets
643          */
644         if (etd->fw_version_maj >= 0x02 && etd->fw_version_min >= 0x30) {
645                 etd->hw_version = 2;
646                 /* For now show extra debug information */
647                 etd->debug = 1;
648                 /* Don't know how to do parity checking for version 2 */
649                 etd->paritycheck = 0;
650         } else {
651                 etd->hw_version = 1;
652                 etd->paritycheck = 1;
653         }
654         pr_info("elantech.c: assuming hardware version %d, firmware version %d.%d\n",
655                 etd->hw_version, etd->fw_version_maj, etd->fw_version_min);
656
657         if (synaptics_send_cmd(psmouse, ETP_CAPABILITIES_QUERY, param)) {
658                 pr_err("elantech.c: failed to query capabilities.\n");
659                 goto init_fail;
660         }
661         pr_info("elantech.c: Synaptics capabilities query result 0x%02x, 0x%02x, 0x%02x.\n",
662                 param[0], param[1], param[2]);
663         etd->capabilities = param[0];
664
665         if (elantech_set_absolute_mode(psmouse)) {
666                 pr_err("elantech.c: failed to put touchpad into absolute mode.\n");
667                 goto init_fail;
668         }
669
670         elantech_set_input_params(psmouse);
671
672         error = sysfs_create_group(&psmouse->ps2dev.serio->dev.kobj,
673                                    &elantech_attr_group);
674         if (error) {
675                 pr_err("elantech.c: failed to create sysfs attributes, error: %d.\n",
676                         error);
677                 goto init_fail;
678         }
679
680         psmouse->protocol_handler = elantech_process_byte;
681         psmouse->disconnect = elantech_disconnect;
682         psmouse->reconnect = elantech_reconnect;
683         psmouse->pktsize = etd->hw_version == 2 ? 6 : 4;
684
685         return 0;
686
687  init_fail:
688         kfree(etd);
689         return -1;
690 }
691 /*
692  * Elantech Touchpad driver (v5)
693  *
694  * Copyright (C) 2007-2008 Arjan Opmeer <arjan@opmeer.net>
695  *
696  * This program is free software; you can redistribute it and/or modify it
697  * under the terms of the GNU General Public License version 2 as published
698  * by the Free Software Foundation.
699  *
700  * Trademarks are the property of their respective owners.
701  */
702
703 #include <linux/delay.h>
704 #include <linux/module.h>
705 #include <linux/input.h>
706 #include <linux/serio.h>
707 #include <linux/libps2.h>
708 #include "psmouse.h"
709 #include "elantech.h"
710
711 #define elantech_debug(format, arg...)                          \
712         do {                                                    \
713                 if (etd->debug)                                 \
714                         printk(KERN_DEBUG format, ##arg);       \
715         } while (0)
716
717 /*
718  * Send a Synaptics style sliced query command
719  */
720 static int synaptics_send_cmd(struct psmouse *psmouse, unsigned char c,
721                                 unsigned char *param)
722 {
723         if (psmouse_sliced_command(psmouse, c) ||
724             ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_GETINFO)) {
725                 pr_err("elantech.c: synaptics_send_cmd query 0x%02x failed.\n", c);
726                 return -1;
727         }
728
729         return 0;
730 }
731
732 /*
733  * A retrying version of ps2_command
734  */
735 static int elantech_ps2_command(struct psmouse *psmouse,
736                                 unsigned char *param, int command)
737 {
738         struct ps2dev *ps2dev = &psmouse->ps2dev;
739         struct elantech_data *etd = psmouse->private;
740         int rc;
741         int tries = ETP_PS2_COMMAND_TRIES;
742
743         do {
744                 rc = ps2_command(ps2dev, param, command);
745                 if (rc == 0)
746                         break;
747                 tries--;
748                 elantech_debug("elantech.c: retrying ps2 command 0x%02x (%d).\n",
749                         command, tries);
750                 msleep(ETP_PS2_COMMAND_DELAY);
751         } while (tries > 0);
752
753         if (rc)
754                 pr_err("elantech.c: ps2 command 0x%02x failed.\n", command);
755
756         return rc;
757 }
758
759 /*
760  * Send an Elantech style special command to read a value from a register
761  */
762 static int elantech_read_reg(struct psmouse *psmouse, unsigned char reg,
763                                 unsigned char *val)
764 {
765         struct elantech_data *etd = psmouse->private;
766         unsigned char param[3];
767         int rc = 0;
768
769         if (reg < 0x10 || reg > 0x26)
770                 return -1;
771
772         if (reg > 0x11 && reg < 0x20)
773                 return -1;
774
775         switch (etd->hw_version) {
776         case 1:
777                 if (psmouse_sliced_command(psmouse, ETP_REGISTER_READ) ||
778                     psmouse_sliced_command(psmouse, reg) ||
779                     ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_GETINFO)) {
780                         rc = -1;
781                 }
782                 break;
783
784         case 2:
785                 if (elantech_ps2_command(psmouse,  NULL, ETP_PS2_CUSTOM_COMMAND) ||
786                     elantech_ps2_command(psmouse,  NULL, ETP_REGISTER_READ) ||
787                     elantech_ps2_command(psmouse,  NULL, ETP_PS2_CUSTOM_COMMAND) ||
788                     elantech_ps2_command(psmouse,  NULL, reg) ||
789                     elantech_ps2_command(psmouse, param, PSMOUSE_CMD_GETINFO)) {
790                         rc = -1;
791                 }
792                 break;
793         }
794
795         if (rc)
796                 pr_err("elantech.c: failed to read register 0x%02x.\n", reg);
797         else
798                 *val = param[0];
799
800         return rc;
801 }
802
803 /*
804  * Send an Elantech style special command to write a register with a value
805  */
806 static int elantech_write_reg(struct psmouse *psmouse, unsigned char reg,
807                                 unsigned char val)
808 {
809         struct elantech_data *etd = psmouse->private;
810         int rc = 0;
811
812         if (reg < 0x10 || reg > 0x26)
813                 return -1;
814
815         if (reg > 0x11 && reg < 0x20)
816                 return -1;
817
818         switch (etd->hw_version) {
819         case 1:
820                 if (psmouse_sliced_command(psmouse, ETP_REGISTER_WRITE) ||
821                     psmouse_sliced_command(psmouse, reg) ||
822                     psmouse_sliced_command(psmouse, val) ||
823                     ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11)) {
824                         rc = -1;
825                 }
826                 break;
827
828         case 2:
829                 if (elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
830                     elantech_ps2_command(psmouse, NULL, ETP_REGISTER_WRITE) ||
831                     elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
832                     elantech_ps2_command(psmouse, NULL, reg) ||
833                     elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
834                     elantech_ps2_command(psmouse, NULL, val) ||
835                     elantech_ps2_command(psmouse, NULL, PSMOUSE_CMD_SETSCALE11)) {
836                         rc = -1;
837                 }
838                 break;
839         }
840
841         if (rc)
842                 pr_err("elantech.c: failed to write register 0x%02x with value 0x%02x.\n",
843                         reg, val);
844
845         return rc;
846 }
847
848 /*
849  * Dump a complete mouse movement packet to the syslog
850  */
851 static void elantech_packet_dump(unsigned char *packet, int size)
852 {
853         int     i;
854
855         printk(KERN_DEBUG "elantech.c: PS/2 packet [");
856         for (i = 0; i < size; i++)
857                 printk("%s0x%02x ", (i) ? ", " : " ", packet[i]);
858         printk("]\n");
859 }
860
861 /*
862  * Interpret complete data packets and report absolute mode input events for
863  * hardware version 1. (4 byte packets)
864  */
865 static void elantech_report_absolute_v1(struct psmouse *psmouse)
866 {
867         struct input_dev *dev = psmouse->dev;
868         struct elantech_data *etd = psmouse->private;
869         unsigned char *packet = psmouse->packet;
870         int fingers;
871
872         if (etd->fw_version_maj == 0x01) {
873                 /* byte 0:  D   U  p1  p2   1  p3   R   L
874                    byte 1:  f   0  th  tw  x9  x8  y9  y8 */
875                 fingers = ((packet[1] & 0x80) >> 7) +
876                                 ((packet[1] & 0x30) >> 4);
877         } else {
878                 /* byte 0: n1  n0  p2  p1   1  p3   R   L
879                    byte 1:  0   0   0   0  x9  x8  y9  y8 */
880                 fingers = (packet[0] & 0xc0) >> 6;
881         }
882
883         input_report_key(dev, BTN_TOUCH, fingers != 0);
884
885         /* byte 2: x7  x6  x5  x4  x3  x2  x1  x0
886            byte 3: y7  y6  y5  y4  y3  y2  y1  y0 */
887         if (fingers) {
888                 input_report_abs(dev, ABS_X,
889                         ((packet[1] & 0x0c) << 6) | packet[2]);
890                 input_report_abs(dev, ABS_Y, ETP_YMAX_V1 -
891                         (((packet[1] & 0x03) << 8) | packet[3]));
892         }
893
894         input_report_key(dev, BTN_TOOL_FINGER, fingers == 1);
895         input_report_key(dev, BTN_TOOL_DOUBLETAP, fingers == 2);
896         input_report_key(dev, BTN_TOOL_TRIPLETAP, fingers == 3);
897         input_report_key(dev, BTN_LEFT, packet[0] & 0x01);
898         input_report_key(dev, BTN_RIGHT, packet[0] & 0x02);
899
900         if ((etd->fw_version_maj == 0x01) &&
901             (etd->capabilities & ETP_CAP_HAS_ROCKER)) {
902                 /* rocker up */
903                 input_report_key(dev, BTN_FORWARD, packet[0] & 0x40);
904                 /* rocker down */
905                 input_report_key(dev, BTN_BACK, packet[0] & 0x80);
906         }
907
908         input_sync(dev);
909 }
910
911 /*
912  * Interpret complete data packets and report absolute mode input events for
913  * hardware version 2. (6 byte packets)
914  */
915 static void elantech_report_absolute_v2(struct psmouse *psmouse)
916 {
917         struct input_dev *dev = psmouse->dev;
918         unsigned char *packet = psmouse->packet;
919         int fingers, x1, y1, x2, y2;
920
921         /* byte 0: n1  n0   .   .   .   .   R   L */
922         fingers = (packet[0] & 0xc0) >> 6;
923         input_report_key(dev, BTN_TOUCH, fingers != 0);
924
925         switch (fingers) {
926         case 1:
927                 /* byte 1: x15 x14 x13 x12 x11 x10 x9  x8
928                    byte 2: x7  x6  x5  x4  x4  x2  x1  x0 */
929                 input_report_abs(dev, ABS_X, (packet[1] << 8) | packet[2]);
930                 /* byte 4: y15 y14 y13 y12 y11 y10 y8  y8
931                    byte 5: y7  y6  y5  y4  y3  y2  y1  y0 */
932                 input_report_abs(dev, ABS_Y, ETP_YMAX_V2 -
933                         ((packet[4] << 8) | packet[5]));
934                 break;
935
936         case 2:
937                 /* The coordinate of each finger is reported separately with
938                    a lower resolution for two finger touches */
939                 /* byte 0:  .   .  ay8 ax8  .   .   .   .
940                    byte 1: ax7 ax6 ax5 ax4 ax3 ax2 ax1 ax0 */
941                 x1 = ((packet[0] & 0x10) << 4) | packet[1];
942                 /* byte 2: ay7 ay6 ay5 ay4 ay3 ay2 ay1 ay0 */
943                 y1 = ETP_2FT_YMAX - (((packet[0] & 0x20) << 3) | packet[2]);
944                 /* byte 3:  .   .  by8 bx8  .   .   .   .
945                    byte 4: bx7 bx6 bx5 bx4 bx3 bx2 bx1 bx0 */
946                 x2 = ((packet[3] & 0x10) << 4) | packet[4];
947                 /* byte 5: by7 by8 by5 by4 by3 by2 by1 by0 */
948                 y2 = ETP_2FT_YMAX - (((packet[3] & 0x20) << 3) | packet[5]);
949                 /* For compatibility with the X Synaptics driver scale up one
950                    coordinate and report as ordinary mouse movent */
951                 input_report_abs(dev, ABS_X, x1 << 2);
952                 input_report_abs(dev, ABS_Y, y1 << 2);
953                 /* For compatibility with the proprietary X Elantech driver
954                    report both coordinates as hat coordinates */
955                 input_report_abs(dev, ABS_HAT0X, x1);
956                 input_report_abs(dev, ABS_HAT0Y, y1);
957                 input_report_abs(dev, ABS_HAT1X, x2);
958                 input_report_abs(dev, ABS_HAT1Y, y2);
959                 break;
960         }
961
962         input_report_key(dev, BTN_TOOL_FINGER, fingers == 1);
963         input_report_key(dev, BTN_TOOL_DOUBLETAP, fingers == 2);
964         input_report_key(dev, BTN_TOOL_TRIPLETAP, fingers == 3);
965         input_report_key(dev, BTN_LEFT, packet[0] & 0x01);
966         input_report_key(dev, BTN_RIGHT, packet[0] & 0x02);
967
968         input_sync(dev);
969 }
970
971 static int elantech_check_parity_v1(struct psmouse *psmouse)
972 {
973         struct elantech_data *etd = psmouse->private;
974         unsigned char *packet = psmouse->packet;
975         unsigned char p1, p2, p3;
976
977         /* Parity bits are placed differently */
978         if (etd->fw_version_maj == 0x01) {
979                 /* byte 0:  D   U  p1  p2   1  p3   R   L */
980                 p1 = (packet[0] & 0x20) >> 5;
981                 p2 = (packet[0] & 0x10) >> 4;
982         } else {
983                 /* byte 0: n1  n0  p2  p1   1  p3   R   L */
984                 p1 = (packet[0] & 0x10) >> 4;
985                 p2 = (packet[0] & 0x20) >> 5;
986         }
987
988         p3 = (packet[0] & 0x04) >> 2;
989
990         return etd->parity[packet[1]] == p1 &&
991                etd->parity[packet[2]] == p2 &&
992                etd->parity[packet[3]] == p3;
993 }
994
995 /*
996  * Process byte stream from mouse and handle complete packets
997  */
998 static psmouse_ret_t elantech_process_byte(struct psmouse *psmouse)
999 {
1000         struct elantech_data *etd = psmouse->private;
1001
1002         if (psmouse->pktcnt < psmouse->pktsize)
1003                 return PSMOUSE_GOOD_DATA;
1004
1005         if (etd->debug > 1)
1006                 elantech_packet_dump(psmouse->packet, psmouse->pktsize);
1007
1008         switch (etd->hw_version) {
1009         case 1:
1010                 if (etd->paritycheck && !elantech_check_parity_v1(psmouse))
1011                         return PSMOUSE_BAD_DATA;
1012
1013                 elantech_report_absolute_v1(psmouse);
1014                 break;
1015
1016         case 2:
1017                 /* We don't know how to check parity in protocol v2 */
1018                 elantech_report_absolute_v2(psmouse);
1019                 break;
1020         }
1021
1022         return PSMOUSE_FULL_PACKET;
1023 }
1024
1025 /*
1026  * Put the touchpad into absolute mode
1027  */
1028 static int elantech_set_absolute_mode(struct psmouse *psmouse)
1029 {
1030         struct elantech_data *etd = psmouse->private;
1031         unsigned char val;
1032         int tries = ETP_READ_BACK_TRIES;
1033         int rc = 0;
1034
1035         switch (etd->hw_version) {
1036         case 1:
1037                 etd->reg_10 = 0x16;
1038                 etd->reg_11 = 0x8f;
1039                 if (elantech_write_reg(psmouse, 0x10, etd->reg_10) ||
1040                     elantech_write_reg(psmouse, 0x11, etd->reg_11)) {
1041                         rc = -1;
1042                 }
1043                 break;
1044
1045         case 2:
1046                                         /* Windows driver values */
1047                 etd->reg_10 = 0x54;
1048                 etd->reg_11 = 0x88;     /* 0x8a */
1049                 etd->reg_21 = 0x60;     /* 0x00 */
1050                 if (elantech_write_reg(psmouse, 0x10, etd->reg_10) ||
1051                     elantech_write_reg(psmouse, 0x11, etd->reg_11) ||
1052                     elantech_write_reg(psmouse, 0x21, etd->reg_21)) {
1053                         rc = -1;
1054                         break;
1055                 }
1056                 /*
1057                  * Read back reg 0x10. The touchpad is probably initalising
1058                  * and not ready until we read back the value we just wrote.
1059                  */
1060                 do {
1061                         rc = elantech_read_reg(psmouse, 0x10, &val);
1062                         if (rc == 0)
1063                                 break;
1064                         tries--;
1065                         elantech_debug("elantech.c: retrying read (%d).\n",
1066                                 tries);
1067                         msleep(ETP_READ_BACK_DELAY);
1068                 } while (tries > 0);
1069                 if (rc)
1070                         pr_err("elantech.c: failed to read back register 0x10.\n");
1071                 break;
1072         }
1073
1074         if (rc)
1075                 pr_err("elantech.c: failed to initialise registers.\n");
1076
1077         return rc;
1078 }
1079
1080 /*
1081  * Set the appropriate event bits for the input subsystem
1082  */
1083 static void elantech_set_input_params(struct psmouse *psmouse)
1084 {
1085         struct input_dev *dev = psmouse->dev;
1086         struct elantech_data *etd = psmouse->private;
1087
1088         __set_bit(EV_KEY, dev->evbit);
1089         __set_bit(EV_ABS, dev->evbit);
1090
1091         __set_bit(BTN_LEFT, dev->keybit);
1092         __set_bit(BTN_RIGHT, dev->keybit);
1093
1094         __set_bit(BTN_TOUCH, dev->keybit);
1095         __set_bit(BTN_TOOL_FINGER, dev->keybit);
1096         __set_bit(BTN_TOOL_DOUBLETAP, dev->keybit);
1097         __set_bit(BTN_TOOL_TRIPLETAP, dev->keybit);
1098
1099         switch (etd->hw_version) {
1100         case 1:
1101                 /* Rocker button */
1102                 if ((etd->fw_version_maj == 0x01) &&
1103                     (etd->capabilities & ETP_CAP_HAS_ROCKER)) {
1104                         __set_bit(BTN_FORWARD, dev->keybit);
1105                         __set_bit(BTN_BACK, dev->keybit);
1106                 }
1107                 input_set_abs_params(dev, ABS_X, ETP_XMIN_V1, ETP_XMAX_V1, 0, 0);
1108                 input_set_abs_params(dev, ABS_Y, ETP_YMIN_V1, ETP_YMAX_V1, 0, 0);
1109                 break;
1110
1111         case 2:
1112                 input_set_abs_params(dev, ABS_X, ETP_XMIN_V2, ETP_XMAX_V2, 0, 0);
1113                 input_set_abs_params(dev, ABS_Y, ETP_YMIN_V2, ETP_YMAX_V2, 0, 0);
1114                 input_set_abs_params(dev, ABS_HAT0X, ETP_2FT_XMIN, ETP_2FT_XMAX, 0, 0);
1115                 input_set_abs_params(dev, ABS_HAT0Y, ETP_2FT_YMIN, ETP_2FT_YMAX, 0, 0);
1116                 input_set_abs_params(dev, ABS_HAT1X, ETP_2FT_XMIN, ETP_2FT_XMAX, 0, 0);
1117                 input_set_abs_params(dev, ABS_HAT1Y, ETP_2FT_YMIN, ETP_2FT_YMAX, 0, 0);
1118                 break;
1119         }
1120 }
1121
1122 struct elantech_attr_data {
1123         size_t          field_offset;
1124         unsigned char   reg;
1125 };
1126
1127 /*
1128  * Display a register value by reading a sysfs entry
1129  */
1130 static ssize_t elantech_show_int_attr(struct psmouse *psmouse, void *data,
1131                                         char *buf)
1132 {
1133         struct elantech_data *etd = psmouse->private;
1134         struct elantech_attr_data *attr = data;
1135         unsigned char *reg = (unsigned char *) etd + attr->field_offset;
1136         int rc = 0;
1137
1138         if (attr->reg)
1139                 rc = elantech_read_reg(psmouse, attr->reg, reg);
1140
1141         return sprintf(buf, "0x%02x\n", (attr->reg && rc) ? -1 : *reg);
1142 }
1143
1144 /*
1145  * Write a register value by writing a sysfs entry
1146  */
1147 static ssize_t elantech_set_int_attr(struct psmouse *psmouse,
1148                                      void *data, const char *buf, size_t count)
1149 {
1150         struct elantech_data *etd = psmouse->private;
1151         struct elantech_attr_data *attr = data;
1152         unsigned char *reg = (unsigned char *) etd + attr->field_offset;
1153         unsigned long value;
1154         int err;
1155
1156         err = strict_strtoul(buf, 16, &value);
1157         if (err)
1158                 return err;
1159
1160         if (value > 0xff)
1161                 return -EINVAL;
1162
1163         /* Do we need to preserve some bits for version 2 hardware too? */
1164         if (etd->hw_version == 1) {
1165                 if (attr->reg == 0x10)
1166                         /* Force absolute mode always on */
1167                         value |= ETP_R10_ABSOLUTE_MODE;
1168                 else if (attr->reg == 0x11)
1169                         /* Force 4 byte mode always on */
1170                         value |= ETP_R11_4_BYTE_MODE;
1171         }
1172
1173         if (!attr->reg || elantech_write_reg(psmouse, attr->reg, value) == 0)
1174                 *reg = value;
1175
1176         return count;
1177 }
1178
1179 #define ELANTECH_INT_ATTR(_name, _register)                             \
1180         static struct elantech_attr_data elantech_attr_##_name = {      \
1181                 .field_offset = offsetof(struct elantech_data, _name),  \
1182                 .reg = _register,                                       \
1183         };                                                              \
1184         PSMOUSE_DEFINE_ATTR(_name, S_IWUSR | S_IRUGO,                   \
1185                             &elantech_attr_##_name,                     \
1186                             elantech_show_int_attr,                     \
1187                             elantech_set_int_attr)
1188
1189 ELANTECH_INT_ATTR(reg_10, 0x10);
1190 ELANTECH_INT_ATTR(reg_11, 0x11);
1191 ELANTECH_INT_ATTR(reg_20, 0x20);
1192 ELANTECH_INT_ATTR(reg_21, 0x21);
1193 ELANTECH_INT_ATTR(reg_22, 0x22);
1194 ELANTECH_INT_ATTR(reg_23, 0x23);
1195 ELANTECH_INT_ATTR(reg_24, 0x24);
1196 ELANTECH_INT_ATTR(reg_25, 0x25);
1197 ELANTECH_INT_ATTR(reg_26, 0x26);
1198 ELANTECH_INT_ATTR(debug, 0);
1199 ELANTECH_INT_ATTR(paritycheck, 0);
1200
1201 static struct attribute *elantech_attrs[] = {
1202         &psmouse_attr_reg_10.dattr.attr,
1203         &psmouse_attr_reg_11.dattr.attr,
1204         &psmouse_attr_reg_20.dattr.attr,
1205         &psmouse_attr_reg_21.dattr.attr,
1206         &psmouse_attr_reg_22.dattr.attr,
1207         &psmouse_attr_reg_23.dattr.attr,
1208         &psmouse_attr_reg_24.dattr.attr,
1209         &psmouse_attr_reg_25.dattr.attr,
1210         &psmouse_attr_reg_26.dattr.attr,
1211         &psmouse_attr_debug.dattr.attr,
1212         &psmouse_attr_paritycheck.dattr.attr,
1213         NULL
1214 };
1215
1216 static struct attribute_group elantech_attr_group = {
1217         .attrs = elantech_attrs,
1218 };
1219
1220 /*
1221  * Use magic knock to detect Elantech touchpad
1222  */
1223 int elantech_detect(struct psmouse *psmouse, int set_properties)
1224 {
1225         struct ps2dev *ps2dev = &psmouse->ps2dev;
1226         unsigned char param[3];
1227
1228         ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
1229
1230         if (ps2_command(ps2dev,  NULL, PSMOUSE_CMD_DISABLE) ||
1231             ps2_command(ps2dev,  NULL, PSMOUSE_CMD_SETSCALE11) ||
1232             ps2_command(ps2dev,  NULL, PSMOUSE_CMD_SETSCALE11) ||
1233             ps2_command(ps2dev,  NULL, PSMOUSE_CMD_SETSCALE11) ||
1234             ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO)) {
1235                 pr_debug("elantech.c: sending Elantech magic knock failed.\n");
1236                 return -1;
1237         }
1238
1239         /*
1240          * Report this in case there are Elantech models that use a different
1241          * set of magic numbers
1242          */
1243         if (param[0] != 0x3c || param[1] != 0x03 || param[2] != 0xc8) {
1244                 pr_debug("elantech.c: "
1245                          "unexpected magic knock result 0x%02x, 0x%02x, 0x%02x.\n",
1246                          param[0], param[1], param[2]);
1247                 return -1;
1248         }
1249
1250         /*
1251          * Query touchpad's firmware version and see if it reports known
1252          * value to avoid mis-detection. Logitech mice are known to respond
1253          * to Elantech magic knock and there might be more.
1254          */
1255         if (synaptics_send_cmd(psmouse, ETP_FW_VERSION_QUERY, param)) {
1256                 pr_debug("elantech.c: failed to query firmware version.\n");
1257                 return -1;
1258         }
1259
1260         pr_debug("elantech.c: Elantech version query result 0x%02x, 0x%02x, 0x%02x.\n",
1261                  param[0], param[1], param[2]);
1262
1263         if (param[0] == 0 || param[1] != 0) {
1264                 pr_debug("elantech.c: Probably not a real Elantech touchpad. Aborting.\n");
1265                 return -1;
1266         }
1267
1268         if (set_properties) {
1269                 psmouse->vendor = "Elantech";
1270                 psmouse->name = "Touchpad";
1271         }
1272
1273         return 0;
1274 }
1275
1276 /*
1277  * Clean up sysfs entries when disconnecting
1278  */
1279 static void elantech_disconnect(struct psmouse *psmouse)
1280 {
1281         sysfs_remove_group(&psmouse->ps2dev.serio->dev.kobj,
1282                            &elantech_attr_group);
1283         kfree(psmouse->private);
1284         psmouse->private = NULL;
1285 }
1286
1287 /*
1288  * Put the touchpad back into absolute mode when reconnecting
1289  */
1290 static int elantech_reconnect(struct psmouse *psmouse)
1291 {
1292         if (elantech_detect(psmouse, 0))
1293                 return -1;
1294
1295         if (elantech_set_absolute_mode(psmouse)) {
1296                 pr_err("elantech.c: failed to put touchpad back into absolute mode.\n");
1297                 return -1;
1298         }
1299
1300         return 0;
1301 }
1302
1303 /*
1304  * Initialize the touchpad and create sysfs entries
1305  */
1306 int elantech_init(struct psmouse *psmouse)
1307 {
1308         struct elantech_data *etd;
1309         int i, error;
1310         unsigned char param[3];
1311
1312         psmouse->private = etd = kzalloc(sizeof(struct elantech_data), GFP_KERNEL);
1313         if (!etd)
1314                 return -1;
1315
1316         etd->parity[0] = 1;
1317         for (i = 1; i < 256; i++)
1318                 etd->parity[i] = etd->parity[i & (i - 1)] ^ 1;
1319
1320         /*
1321          * Do the version query again so we can store the result
1322          */
1323         if (synaptics_send_cmd(psmouse, ETP_FW_VERSION_QUERY, param)) {
1324                 pr_err("elantech.c: failed to query firmware version.\n");
1325                 goto init_fail;
1326         }
1327         etd->fw_version_maj = param[0];
1328         etd->fw_version_min = param[2];
1329
1330         /*
1331          * Assume every version greater than this is new EeePC style
1332          * hardware with 6 byte packets
1333          */
1334         if (etd->fw_version_maj >= 0x02 && etd->fw_version_min >= 0x30) {
1335                 etd->hw_version = 2;
1336                 /* For now show extra debug information */
1337                 etd->debug = 1;
1338                 /* Don't know how to do parity checking for version 2 */
1339                 etd->paritycheck = 0;
1340         } else {
1341                 etd->hw_version = 1;
1342                 etd->paritycheck = 1;
1343         }
1344         pr_info("elantech.c: assuming hardware version %d, firmware version %d.%d\n",
1345                 etd->hw_version, etd->fw_version_maj, etd->fw_version_min);
1346
1347         if (synaptics_send_cmd(psmouse, ETP_CAPABILITIES_QUERY, param)) {
1348                 pr_err("elantech.c: failed to query capabilities.\n");
1349                 goto init_fail;
1350         }
1351         pr_info("elantech.c: Synaptics capabilities query result 0x%02x, 0x%02x, 0x%02x.\n",
1352                 param[0], param[1], param[2]);
1353         etd->capabilities = param[0];
1354
1355         if (elantech_set_absolute_mode(psmouse)) {
1356                 pr_err("elantech.c: failed to put touchpad into absolute mode.\n");
1357                 goto init_fail;
1358         }
1359
1360         elantech_set_input_params(psmouse);
1361
1362         error = sysfs_create_group(&psmouse->ps2dev.serio->dev.kobj,
1363                                    &elantech_attr_group);
1364         if (error) {
1365                 pr_err("elantech.c: failed to create sysfs attributes, error: %d.\n",
1366                         error);
1367                 goto init_fail;
1368         }
1369
1370         psmouse->protocol_handler = elantech_process_byte;
1371         psmouse->disconnect = elantech_disconnect;
1372         psmouse->reconnect = elantech_reconnect;
1373         psmouse->pktsize = etd->hw_version == 2 ? 6 : 4;
1374
1375         return 0;
1376
1377  init_fail:
1378         kfree(etd);
1379         return -1;
1380 }