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