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