Input: elantech - support new ICs types for version 4
[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/dmi.h>
15 #include <linux/slab.h>
16 #include <linux/module.h>
17 #include <linux/input.h>
18 #include <linux/input/mt.h>
19 #include <linux/serio.h>
20 #include <linux/libps2.h>
21 #include "psmouse.h"
22 #include "elantech.h"
23
24 #define elantech_debug(fmt, ...)                                        \
25         do {                                                            \
26                 if (etd->debug)                                         \
27                         psmouse_printk(KERN_DEBUG, psmouse,             \
28                                         fmt, ##__VA_ARGS__);            \
29         } while (0)
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                 psmouse_err(psmouse, "%s query 0x%02x failed.\n", __func__, 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("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                 psmouse_err(psmouse, "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 < 0x07 || 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         case 3 ... 4:
109                 if (elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
110                     elantech_ps2_command(psmouse, NULL, ETP_REGISTER_READWRITE) ||
111                     elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
112                     elantech_ps2_command(psmouse, NULL, reg) ||
113                     elantech_ps2_command(psmouse, param, PSMOUSE_CMD_GETINFO)) {
114                         rc = -1;
115                 }
116                 break;
117         }
118
119         if (rc)
120                 psmouse_err(psmouse, "failed to read register 0x%02x.\n", reg);
121         else if (etd->hw_version != 4)
122                 *val = param[0];
123         else
124                 *val = param[1];
125
126         return rc;
127 }
128
129 /*
130  * Send an Elantech style special command to write a register with a value
131  */
132 static int elantech_write_reg(struct psmouse *psmouse, unsigned char reg,
133                                 unsigned char val)
134 {
135         struct elantech_data *etd = psmouse->private;
136         int rc = 0;
137
138         if (reg < 0x07 || reg > 0x26)
139                 return -1;
140
141         if (reg > 0x11 && reg < 0x20)
142                 return -1;
143
144         switch (etd->hw_version) {
145         case 1:
146                 if (psmouse_sliced_command(psmouse, ETP_REGISTER_WRITE) ||
147                     psmouse_sliced_command(psmouse, reg) ||
148                     psmouse_sliced_command(psmouse, val) ||
149                     ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11)) {
150                         rc = -1;
151                 }
152                 break;
153
154         case 2:
155                 if (elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
156                     elantech_ps2_command(psmouse, NULL, ETP_REGISTER_WRITE) ||
157                     elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
158                     elantech_ps2_command(psmouse, NULL, reg) ||
159                     elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
160                     elantech_ps2_command(psmouse, NULL, val) ||
161                     elantech_ps2_command(psmouse, NULL, PSMOUSE_CMD_SETSCALE11)) {
162                         rc = -1;
163                 }
164                 break;
165
166         case 3:
167                 if (elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
168                     elantech_ps2_command(psmouse, NULL, ETP_REGISTER_READWRITE) ||
169                     elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
170                     elantech_ps2_command(psmouse, NULL, reg) ||
171                     elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
172                     elantech_ps2_command(psmouse, NULL, val) ||
173                     elantech_ps2_command(psmouse, NULL, PSMOUSE_CMD_SETSCALE11)) {
174                         rc = -1;
175                 }
176                 break;
177
178         case 4:
179                 if (elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
180                     elantech_ps2_command(psmouse, NULL, ETP_REGISTER_READWRITE) ||
181                     elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
182                     elantech_ps2_command(psmouse, NULL, reg) ||
183                     elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
184                     elantech_ps2_command(psmouse, NULL, ETP_REGISTER_READWRITE) ||
185                     elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
186                     elantech_ps2_command(psmouse, NULL, val) ||
187                     elantech_ps2_command(psmouse, NULL, PSMOUSE_CMD_SETSCALE11)) {
188                         rc = -1;
189                 }
190                 break;
191         }
192
193         if (rc)
194                 psmouse_err(psmouse,
195                             "failed to write register 0x%02x with value 0x%02x.\n",
196                             reg, val);
197
198         return rc;
199 }
200
201 /*
202  * Dump a complete mouse movement packet to the syslog
203  */
204 static void elantech_packet_dump(struct psmouse *psmouse)
205 {
206         int     i;
207
208         psmouse_printk(KERN_DEBUG, psmouse, "PS/2 packet [");
209         for (i = 0; i < psmouse->pktsize; i++)
210                 printk("%s0x%02x ", i ? ", " : " ", psmouse->packet[i]);
211         printk("]\n");
212 }
213
214 /*
215  * Interpret complete data packets and report absolute mode input events for
216  * hardware version 1. (4 byte packets)
217  */
218 static void elantech_report_absolute_v1(struct psmouse *psmouse)
219 {
220         struct input_dev *dev = psmouse->dev;
221         struct elantech_data *etd = psmouse->private;
222         unsigned char *packet = psmouse->packet;
223         int fingers;
224
225         if (etd->fw_version < 0x020000) {
226                 /*
227                  * byte 0:  D   U  p1  p2   1  p3   R   L
228                  * byte 1:  f   0  th  tw  x9  x8  y9  y8
229                  */
230                 fingers = ((packet[1] & 0x80) >> 7) +
231                                 ((packet[1] & 0x30) >> 4);
232         } else {
233                 /*
234                  * byte 0: n1  n0  p2  p1   1  p3   R   L
235                  * byte 1:  0   0   0   0  x9  x8  y9  y8
236                  */
237                 fingers = (packet[0] & 0xc0) >> 6;
238         }
239
240         if (etd->jumpy_cursor) {
241                 if (fingers != 1) {
242                         etd->single_finger_reports = 0;
243                 } else if (etd->single_finger_reports < 2) {
244                         /* Discard first 2 reports of one finger, bogus */
245                         etd->single_finger_reports++;
246                         elantech_debug("discarding packet\n");
247                         return;
248                 }
249         }
250
251         input_report_key(dev, BTN_TOUCH, fingers != 0);
252
253         /*
254          * byte 2: x7  x6  x5  x4  x3  x2  x1  x0
255          * byte 3: y7  y6  y5  y4  y3  y2  y1  y0
256          */
257         if (fingers) {
258                 input_report_abs(dev, ABS_X,
259                         ((packet[1] & 0x0c) << 6) | packet[2]);
260                 input_report_abs(dev, ABS_Y,
261                         etd->y_max - (((packet[1] & 0x03) << 8) | packet[3]));
262         }
263
264         input_report_key(dev, BTN_TOOL_FINGER, fingers == 1);
265         input_report_key(dev, BTN_TOOL_DOUBLETAP, fingers == 2);
266         input_report_key(dev, BTN_TOOL_TRIPLETAP, fingers == 3);
267         input_report_key(dev, BTN_LEFT, packet[0] & 0x01);
268         input_report_key(dev, BTN_RIGHT, packet[0] & 0x02);
269
270         if (etd->fw_version < 0x020000 &&
271             (etd->capabilities[0] & ETP_CAP_HAS_ROCKER)) {
272                 /* rocker up */
273                 input_report_key(dev, BTN_FORWARD, packet[0] & 0x40);
274                 /* rocker down */
275                 input_report_key(dev, BTN_BACK, packet[0] & 0x80);
276         }
277
278         input_sync(dev);
279 }
280
281 static void elantech_set_slot(struct input_dev *dev, int slot, bool active,
282                               unsigned int x, unsigned int y)
283 {
284         input_mt_slot(dev, slot);
285         input_mt_report_slot_state(dev, MT_TOOL_FINGER, active);
286         if (active) {
287                 input_report_abs(dev, ABS_MT_POSITION_X, x);
288                 input_report_abs(dev, ABS_MT_POSITION_Y, y);
289         }
290 }
291
292 /* x1 < x2 and y1 < y2 when two fingers, x = y = 0 when not pressed */
293 static void elantech_report_semi_mt_data(struct input_dev *dev,
294                                          unsigned int num_fingers,
295                                          unsigned int x1, unsigned int y1,
296                                          unsigned int x2, unsigned int y2)
297 {
298         elantech_set_slot(dev, 0, num_fingers != 0, x1, y1);
299         elantech_set_slot(dev, 1, num_fingers >= 2, x2, y2);
300 }
301
302 /*
303  * Interpret complete data packets and report absolute mode input events for
304  * hardware version 2. (6 byte packets)
305  */
306 static void elantech_report_absolute_v2(struct psmouse *psmouse)
307 {
308         struct elantech_data *etd = psmouse->private;
309         struct input_dev *dev = psmouse->dev;
310         unsigned char *packet = psmouse->packet;
311         unsigned int fingers, x1 = 0, y1 = 0, x2 = 0, y2 = 0;
312         unsigned int width = 0, pres = 0;
313
314         /* byte 0: n1  n0   .   .   .   .   R   L */
315         fingers = (packet[0] & 0xc0) >> 6;
316
317         switch (fingers) {
318         case 3:
319                 /*
320                  * Same as one finger, except report of more than 3 fingers:
321                  * byte 3:  n4  .   w1  w0   .   .   .   .
322                  */
323                 if (packet[3] & 0x80)
324                         fingers = 4;
325                 /* pass through... */
326         case 1:
327                 /*
328                  * byte 1:  .   .   .   .  x11 x10 x9  x8
329                  * byte 2: x7  x6  x5  x4  x4  x2  x1  x0
330                  */
331                 x1 = ((packet[1] & 0x0f) << 8) | packet[2];
332                 /*
333                  * byte 4:  .   .   .   .  y11 y10 y9  y8
334                  * byte 5: y7  y6  y5  y4  y3  y2  y1  y0
335                  */
336                 y1 = etd->y_max - (((packet[4] & 0x0f) << 8) | packet[5]);
337
338                 pres = (packet[1] & 0xf0) | ((packet[4] & 0xf0) >> 4);
339                 width = ((packet[0] & 0x30) >> 2) | ((packet[3] & 0x30) >> 4);
340                 break;
341
342         case 2:
343                 /*
344                  * The coordinate of each finger is reported separately
345                  * with a lower resolution for two finger touches:
346                  * byte 0:  .   .  ay8 ax8  .   .   .   .
347                  * byte 1: ax7 ax6 ax5 ax4 ax3 ax2 ax1 ax0
348                  */
349                 x1 = (((packet[0] & 0x10) << 4) | packet[1]) << 2;
350                 /* byte 2: ay7 ay6 ay5 ay4 ay3 ay2 ay1 ay0 */
351                 y1 = etd->y_max -
352                         ((((packet[0] & 0x20) << 3) | packet[2]) << 2);
353                 /*
354                  * byte 3:  .   .  by8 bx8  .   .   .   .
355                  * byte 4: bx7 bx6 bx5 bx4 bx3 bx2 bx1 bx0
356                  */
357                 x2 = (((packet[3] & 0x10) << 4) | packet[4]) << 2;
358                 /* byte 5: by7 by8 by5 by4 by3 by2 by1 by0 */
359                 y2 = etd->y_max -
360                         ((((packet[3] & 0x20) << 3) | packet[5]) << 2);
361
362                 /* Unknown so just report sensible values */
363                 pres = 127;
364                 width = 7;
365                 break;
366         }
367
368         input_report_key(dev, BTN_TOUCH, fingers != 0);
369         if (fingers != 0) {
370                 input_report_abs(dev, ABS_X, x1);
371                 input_report_abs(dev, ABS_Y, y1);
372         }
373         elantech_report_semi_mt_data(dev, fingers, x1, y1, x2, y2);
374         input_report_key(dev, BTN_TOOL_FINGER, fingers == 1);
375         input_report_key(dev, BTN_TOOL_DOUBLETAP, fingers == 2);
376         input_report_key(dev, BTN_TOOL_TRIPLETAP, fingers == 3);
377         input_report_key(dev, BTN_TOOL_QUADTAP, fingers == 4);
378         input_report_key(dev, BTN_LEFT, packet[0] & 0x01);
379         input_report_key(dev, BTN_RIGHT, packet[0] & 0x02);
380         if (etd->reports_pressure) {
381                 input_report_abs(dev, ABS_PRESSURE, pres);
382                 input_report_abs(dev, ABS_TOOL_WIDTH, width);
383         }
384
385         input_sync(dev);
386 }
387
388 /*
389  * Interpret complete data packets and report absolute mode input events for
390  * hardware version 3. (12 byte packets for two fingers)
391  */
392 static void elantech_report_absolute_v3(struct psmouse *psmouse,
393                                         int packet_type)
394 {
395         struct input_dev *dev = psmouse->dev;
396         struct elantech_data *etd = psmouse->private;
397         unsigned char *packet = psmouse->packet;
398         unsigned int fingers = 0, x1 = 0, y1 = 0, x2 = 0, y2 = 0;
399         unsigned int width = 0, pres = 0;
400
401         /* byte 0: n1  n0   .   .   .   .   R   L */
402         fingers = (packet[0] & 0xc0) >> 6;
403
404         switch (fingers) {
405         case 3:
406         case 1:
407                 /*
408                  * byte 1:  .   .   .   .  x11 x10 x9  x8
409                  * byte 2: x7  x6  x5  x4  x4  x2  x1  x0
410                  */
411                 x1 = ((packet[1] & 0x0f) << 8) | packet[2];
412                 /*
413                  * byte 4:  .   .   .   .  y11 y10 y9  y8
414                  * byte 5: y7  y6  y5  y4  y3  y2  y1  y0
415                  */
416                 y1 = etd->y_max - (((packet[4] & 0x0f) << 8) | packet[5]);
417                 break;
418
419         case 2:
420                 if (packet_type == PACKET_V3_HEAD) {
421                         /*
422                          * byte 1:   .    .    .    .  ax11 ax10 ax9  ax8
423                          * byte 2: ax7  ax6  ax5  ax4  ax3  ax2  ax1  ax0
424                          */
425                         etd->mt[0].x = ((packet[1] & 0x0f) << 8) | packet[2];
426                         /*
427                          * byte 4:   .    .    .    .  ay11 ay10 ay9  ay8
428                          * byte 5: ay7  ay6  ay5  ay4  ay3  ay2  ay1  ay0
429                          */
430                         etd->mt[0].y = etd->y_max -
431                                 (((packet[4] & 0x0f) << 8) | packet[5]);
432                         /*
433                          * wait for next packet
434                          */
435                         return;
436                 }
437
438                 /* packet_type == PACKET_V3_TAIL */
439                 x1 = etd->mt[0].x;
440                 y1 = etd->mt[0].y;
441                 x2 = ((packet[1] & 0x0f) << 8) | packet[2];
442                 y2 = etd->y_max - (((packet[4] & 0x0f) << 8) | packet[5]);
443                 break;
444         }
445
446         pres = (packet[1] & 0xf0) | ((packet[4] & 0xf0) >> 4);
447         width = ((packet[0] & 0x30) >> 2) | ((packet[3] & 0x30) >> 4);
448
449         input_report_key(dev, BTN_TOUCH, fingers != 0);
450         if (fingers != 0) {
451                 input_report_abs(dev, ABS_X, x1);
452                 input_report_abs(dev, ABS_Y, y1);
453         }
454         elantech_report_semi_mt_data(dev, fingers, x1, y1, x2, y2);
455         input_report_key(dev, BTN_TOOL_FINGER, fingers == 1);
456         input_report_key(dev, BTN_TOOL_DOUBLETAP, fingers == 2);
457         input_report_key(dev, BTN_TOOL_TRIPLETAP, fingers == 3);
458
459         /* For clickpads map both buttons to BTN_LEFT */
460         if (etd->fw_version & 0x001000) {
461                 input_report_key(dev, BTN_LEFT, packet[0] & 0x03);
462         } else {
463                 input_report_key(dev, BTN_LEFT, packet[0] & 0x01);
464                 input_report_key(dev, BTN_RIGHT, packet[0] & 0x02);
465         }
466
467         input_report_abs(dev, ABS_PRESSURE, pres);
468         input_report_abs(dev, ABS_TOOL_WIDTH, width);
469
470         input_sync(dev);
471 }
472
473 static void elantech_input_sync_v4(struct psmouse *psmouse)
474 {
475         struct input_dev *dev = psmouse->dev;
476         struct elantech_data *etd = psmouse->private;
477         unsigned char *packet = psmouse->packet;
478
479         /* For clickpads map both buttons to BTN_LEFT */
480         if (etd->fw_version & 0x001000) {
481                 input_report_key(dev, BTN_LEFT, packet[0] & 0x03);
482         } else {
483                 input_report_key(dev, BTN_LEFT, packet[0] & 0x01);
484                 input_report_key(dev, BTN_RIGHT, packet[0] & 0x02);
485         }
486
487         input_mt_report_pointer_emulation(dev, true);
488         input_sync(dev);
489 }
490
491 static void process_packet_status_v4(struct psmouse *psmouse)
492 {
493         struct input_dev *dev = psmouse->dev;
494         unsigned char *packet = psmouse->packet;
495         unsigned fingers;
496         int i;
497
498         /* notify finger state change */
499         fingers = packet[1] & 0x1f;
500         for (i = 0; i < ETP_MAX_FINGERS; i++) {
501                 if ((fingers & (1 << i)) == 0) {
502                         input_mt_slot(dev, i);
503                         input_mt_report_slot_state(dev, MT_TOOL_FINGER, false);
504                 }
505         }
506
507         elantech_input_sync_v4(psmouse);
508 }
509
510 static void process_packet_head_v4(struct psmouse *psmouse)
511 {
512         struct input_dev *dev = psmouse->dev;
513         struct elantech_data *etd = psmouse->private;
514         unsigned char *packet = psmouse->packet;
515         int id = ((packet[3] & 0xe0) >> 5) - 1;
516         int pres, traces;
517
518         if (id < 0)
519                 return;
520
521         etd->mt[id].x = ((packet[1] & 0x0f) << 8) | packet[2];
522         etd->mt[id].y = etd->y_max - (((packet[4] & 0x0f) << 8) | packet[5]);
523         pres = (packet[1] & 0xf0) | ((packet[4] & 0xf0) >> 4);
524         traces = (packet[0] & 0xf0) >> 4;
525
526         input_mt_slot(dev, id);
527         input_mt_report_slot_state(dev, MT_TOOL_FINGER, true);
528
529         input_report_abs(dev, ABS_MT_POSITION_X, etd->mt[id].x);
530         input_report_abs(dev, ABS_MT_POSITION_Y, etd->mt[id].y);
531         input_report_abs(dev, ABS_MT_PRESSURE, pres);
532         input_report_abs(dev, ABS_MT_TOUCH_MAJOR, traces * etd->width);
533         /* report this for backwards compatibility */
534         input_report_abs(dev, ABS_TOOL_WIDTH, traces);
535
536         elantech_input_sync_v4(psmouse);
537 }
538
539 static void process_packet_motion_v4(struct psmouse *psmouse)
540 {
541         struct input_dev *dev = psmouse->dev;
542         struct elantech_data *etd = psmouse->private;
543         unsigned char *packet = psmouse->packet;
544         int weight, delta_x1 = 0, delta_y1 = 0, delta_x2 = 0, delta_y2 = 0;
545         int id, sid;
546
547         id = ((packet[0] & 0xe0) >> 5) - 1;
548         if (id < 0)
549                 return;
550
551         sid = ((packet[3] & 0xe0) >> 5) - 1;
552         weight = (packet[0] & 0x10) ? ETP_WEIGHT_VALUE : 1;
553         /*
554          * Motion packets give us the delta of x, y values of specific fingers,
555          * but in two's complement. Let the compiler do the conversion for us.
556          * Also _enlarge_ the numbers to int, in case of overflow.
557          */
558         delta_x1 = (signed char)packet[1];
559         delta_y1 = (signed char)packet[2];
560         delta_x2 = (signed char)packet[4];
561         delta_y2 = (signed char)packet[5];
562
563         etd->mt[id].x += delta_x1 * weight;
564         etd->mt[id].y -= delta_y1 * weight;
565         input_mt_slot(dev, id);
566         input_report_abs(dev, ABS_MT_POSITION_X, etd->mt[id].x);
567         input_report_abs(dev, ABS_MT_POSITION_Y, etd->mt[id].y);
568
569         if (sid >= 0) {
570                 etd->mt[sid].x += delta_x2 * weight;
571                 etd->mt[sid].y -= delta_y2 * weight;
572                 input_mt_slot(dev, sid);
573                 input_report_abs(dev, ABS_MT_POSITION_X, etd->mt[sid].x);
574                 input_report_abs(dev, ABS_MT_POSITION_Y, etd->mt[sid].y);
575         }
576
577         elantech_input_sync_v4(psmouse);
578 }
579
580 static void elantech_report_absolute_v4(struct psmouse *psmouse,
581                                         int packet_type)
582 {
583         switch (packet_type) {
584         case PACKET_V4_STATUS:
585                 process_packet_status_v4(psmouse);
586                 break;
587
588         case PACKET_V4_HEAD:
589                 process_packet_head_v4(psmouse);
590                 break;
591
592         case PACKET_V4_MOTION:
593                 process_packet_motion_v4(psmouse);
594                 break;
595
596         case PACKET_UNKNOWN:
597         default:
598                 /* impossible to get here */
599                 break;
600         }
601 }
602
603 static int elantech_packet_check_v1(struct psmouse *psmouse)
604 {
605         struct elantech_data *etd = psmouse->private;
606         unsigned char *packet = psmouse->packet;
607         unsigned char p1, p2, p3;
608
609         /* Parity bits are placed differently */
610         if (etd->fw_version < 0x020000) {
611                 /* byte 0:  D   U  p1  p2   1  p3   R   L */
612                 p1 = (packet[0] & 0x20) >> 5;
613                 p2 = (packet[0] & 0x10) >> 4;
614         } else {
615                 /* byte 0: n1  n0  p2  p1   1  p3   R   L */
616                 p1 = (packet[0] & 0x10) >> 4;
617                 p2 = (packet[0] & 0x20) >> 5;
618         }
619
620         p3 = (packet[0] & 0x04) >> 2;
621
622         return etd->parity[packet[1]] == p1 &&
623                etd->parity[packet[2]] == p2 &&
624                etd->parity[packet[3]] == p3;
625 }
626
627 static int elantech_debounce_check_v2(struct psmouse *psmouse)
628 {
629         /*
630          * When we encounter packet that matches this exactly, it means the
631          * hardware is in debounce status. Just ignore the whole packet.
632          */
633         const u8 debounce_packet[] = { 0x84, 0xff, 0xff, 0x02, 0xff, 0xff };
634         unsigned char *packet = psmouse->packet;
635
636         return !memcmp(packet, debounce_packet, sizeof(debounce_packet));
637 }
638
639 static int elantech_packet_check_v2(struct psmouse *psmouse)
640 {
641         struct elantech_data *etd = psmouse->private;
642         unsigned char *packet = psmouse->packet;
643
644         /*
645          * V2 hardware has two flavors. Older ones that do not report pressure,
646          * and newer ones that reports pressure and width. With newer ones, all
647          * packets (1, 2, 3 finger touch) have the same constant bits. With
648          * older ones, 1/3 finger touch packets and 2 finger touch packets
649          * have different constant bits.
650          * With all three cases, if the constant bits are not exactly what I
651          * expected, I consider them invalid.
652          */
653         if (etd->reports_pressure)
654                 return (packet[0] & 0x0c) == 0x04 &&
655                        (packet[3] & 0x0f) == 0x02;
656
657         if ((packet[0] & 0xc0) == 0x80)
658                 return (packet[0] & 0x0c) == 0x0c &&
659                        (packet[3] & 0x0e) == 0x08;
660
661         return (packet[0] & 0x3c) == 0x3c &&
662                (packet[1] & 0xf0) == 0x00 &&
663                (packet[3] & 0x3e) == 0x38 &&
664                (packet[4] & 0xf0) == 0x00;
665 }
666
667 /*
668  * We check the constant bits to determine what packet type we get,
669  * so packet checking is mandatory for v3 and later hardware.
670  */
671 static int elantech_packet_check_v3(struct psmouse *psmouse)
672 {
673         const u8 debounce_packet[] = { 0xc4, 0xff, 0xff, 0x02, 0xff, 0xff };
674         unsigned char *packet = psmouse->packet;
675
676         /*
677          * check debounce first, it has the same signature in byte 0
678          * and byte 3 as PACKET_V3_HEAD.
679          */
680         if (!memcmp(packet, debounce_packet, sizeof(debounce_packet)))
681                 return PACKET_DEBOUNCE;
682
683         if ((packet[0] & 0x0c) == 0x04 && (packet[3] & 0xcf) == 0x02)
684                 return PACKET_V3_HEAD;
685
686         if ((packet[0] & 0x0c) == 0x0c && (packet[3] & 0xce) == 0x0c)
687                 return PACKET_V3_TAIL;
688
689         return PACKET_UNKNOWN;
690 }
691
692 static int elantech_packet_check_v4(struct psmouse *psmouse)
693 {
694         unsigned char *packet = psmouse->packet;
695         unsigned char packet_type = packet[3] & 0x03;
696
697         switch (packet_type) {
698         case 0:
699                 return PACKET_V4_STATUS;
700
701         case 1:
702                 return PACKET_V4_HEAD;
703
704         case 2:
705                 return PACKET_V4_MOTION;
706         }
707
708         return PACKET_UNKNOWN;
709 }
710
711 /*
712  * Process byte stream from mouse and handle complete packets
713  */
714 static psmouse_ret_t elantech_process_byte(struct psmouse *psmouse)
715 {
716         struct elantech_data *etd = psmouse->private;
717         int packet_type;
718
719         if (psmouse->pktcnt < psmouse->pktsize)
720                 return PSMOUSE_GOOD_DATA;
721
722         if (etd->debug > 1)
723                 elantech_packet_dump(psmouse);
724
725         switch (etd->hw_version) {
726         case 1:
727                 if (etd->paritycheck && !elantech_packet_check_v1(psmouse))
728                         return PSMOUSE_BAD_DATA;
729
730                 elantech_report_absolute_v1(psmouse);
731                 break;
732
733         case 2:
734                 /* ignore debounce */
735                 if (elantech_debounce_check_v2(psmouse))
736                         return PSMOUSE_FULL_PACKET;
737
738                 if (etd->paritycheck && !elantech_packet_check_v2(psmouse))
739                         return PSMOUSE_BAD_DATA;
740
741                 elantech_report_absolute_v2(psmouse);
742                 break;
743
744         case 3:
745                 packet_type = elantech_packet_check_v3(psmouse);
746                 /* ignore debounce */
747                 if (packet_type == PACKET_DEBOUNCE)
748                         return PSMOUSE_FULL_PACKET;
749
750                 if (packet_type == PACKET_UNKNOWN)
751                         return PSMOUSE_BAD_DATA;
752
753                 elantech_report_absolute_v3(psmouse, packet_type);
754                 break;
755
756         case 4:
757                 packet_type = elantech_packet_check_v4(psmouse);
758                 if (packet_type == PACKET_UNKNOWN)
759                         return PSMOUSE_BAD_DATA;
760
761                 elantech_report_absolute_v4(psmouse, packet_type);
762                 break;
763         }
764
765         return PSMOUSE_FULL_PACKET;
766 }
767
768 /*
769  * This writes the reg_07 value again to the hardware at the end of every
770  * set_rate call because the register loses its value. reg_07 allows setting
771  * absolute mode on v4 hardware
772  */
773 static void elantech_set_rate_restore_reg_07(struct psmouse *psmouse,
774                 unsigned int rate)
775 {
776         struct elantech_data *etd = psmouse->private;
777
778         etd->original_set_rate(psmouse, rate);
779         if (elantech_write_reg(psmouse, 0x07, etd->reg_07))
780                 psmouse_err(psmouse, "restoring reg_07 failed\n");
781 }
782
783 /*
784  * Put the touchpad into absolute mode
785  */
786 static int elantech_set_absolute_mode(struct psmouse *psmouse)
787 {
788         struct elantech_data *etd = psmouse->private;
789         unsigned char val;
790         int tries = ETP_READ_BACK_TRIES;
791         int rc = 0;
792
793         switch (etd->hw_version) {
794         case 1:
795                 etd->reg_10 = 0x16;
796                 etd->reg_11 = 0x8f;
797                 if (elantech_write_reg(psmouse, 0x10, etd->reg_10) ||
798                     elantech_write_reg(psmouse, 0x11, etd->reg_11)) {
799                         rc = -1;
800                 }
801                 break;
802
803         case 2:
804                                         /* Windows driver values */
805                 etd->reg_10 = 0x54;
806                 etd->reg_11 = 0x88;     /* 0x8a */
807                 etd->reg_21 = 0x60;     /* 0x00 */
808                 if (elantech_write_reg(psmouse, 0x10, etd->reg_10) ||
809                     elantech_write_reg(psmouse, 0x11, etd->reg_11) ||
810                     elantech_write_reg(psmouse, 0x21, etd->reg_21)) {
811                         rc = -1;
812                 }
813                 break;
814
815         case 3:
816                 if (etd->set_hw_resolution)
817                         etd->reg_10 = 0x0b;
818                 else
819                         etd->reg_10 = 0x01;
820
821                 if (elantech_write_reg(psmouse, 0x10, etd->reg_10))
822                         rc = -1;
823
824                 break;
825
826         case 4:
827                 etd->reg_07 = 0x01;
828                 if (elantech_write_reg(psmouse, 0x07, etd->reg_07))
829                         rc = -1;
830
831                 goto skip_readback_reg_10; /* v4 has no reg 0x10 to read */
832         }
833
834         if (rc == 0) {
835                 /*
836                  * Read back reg 0x10. For hardware version 1 we must make
837                  * sure the absolute mode bit is set. For hardware version 2
838                  * the touchpad is probably initializing and not ready until
839                  * we read back the value we just wrote.
840                  */
841                 do {
842                         rc = elantech_read_reg(psmouse, 0x10, &val);
843                         if (rc == 0)
844                                 break;
845                         tries--;
846                         elantech_debug("retrying read (%d).\n", tries);
847                         msleep(ETP_READ_BACK_DELAY);
848                 } while (tries > 0);
849
850                 if (rc) {
851                         psmouse_err(psmouse,
852                                     "failed to read back register 0x10.\n");
853                 } else if (etd->hw_version == 1 &&
854                            !(val & ETP_R10_ABSOLUTE_MODE)) {
855                         psmouse_err(psmouse,
856                                     "touchpad refuses to switch to absolute mode.\n");
857                         rc = -1;
858                 }
859         }
860
861  skip_readback_reg_10:
862         if (rc)
863                 psmouse_err(psmouse, "failed to initialise registers.\n");
864
865         return rc;
866 }
867
868 static int elantech_set_range(struct psmouse *psmouse,
869                               unsigned int *x_min, unsigned int *y_min,
870                               unsigned int *x_max, unsigned int *y_max,
871                               unsigned int *width)
872 {
873         struct elantech_data *etd = psmouse->private;
874         unsigned char param[3];
875         unsigned char traces;
876
877         switch (etd->hw_version) {
878         case 1:
879                 *x_min = ETP_XMIN_V1;
880                 *y_min = ETP_YMIN_V1;
881                 *x_max = ETP_XMAX_V1;
882                 *y_max = ETP_YMAX_V1;
883                 break;
884
885         case 2:
886                 if (etd->fw_version == 0x020800 ||
887                     etd->fw_version == 0x020b00 ||
888                     etd->fw_version == 0x020030) {
889                         *x_min = ETP_XMIN_V2;
890                         *y_min = ETP_YMIN_V2;
891                         *x_max = ETP_XMAX_V2;
892                         *y_max = ETP_YMAX_V2;
893                 } else {
894                         int i;
895                         int fixed_dpi;
896
897                         i = (etd->fw_version > 0x020800 &&
898                              etd->fw_version < 0x020900) ? 1 : 2;
899
900                         if (synaptics_send_cmd(psmouse, ETP_FW_ID_QUERY, param))
901                                 return -1;
902
903                         fixed_dpi = param[1] & 0x10;
904
905                         if (((etd->fw_version >> 16) == 0x14) && fixed_dpi) {
906                                 if (synaptics_send_cmd(psmouse, ETP_SAMPLE_QUERY, param))
907                                         return -1;
908
909                                 *x_max = (etd->capabilities[1] - i) * param[1] / 2;
910                                 *y_max = (etd->capabilities[2] - i) * param[2] / 2;
911                         } else if (etd->fw_version == 0x040216) {
912                                 *x_max = 819;
913                                 *y_max = 405;
914                         } else if (etd->fw_version == 0x040219 || etd->fw_version == 0x040215) {
915                                 *x_max = 900;
916                                 *y_max = 500;
917                         } else {
918                                 *x_max = (etd->capabilities[1] - i) * 64;
919                                 *y_max = (etd->capabilities[2] - i) * 64;
920                         }
921                 }
922                 break;
923
924         case 3:
925                 if (synaptics_send_cmd(psmouse, ETP_FW_ID_QUERY, param))
926                         return -1;
927
928                 *x_max = (0x0f & param[0]) << 8 | param[1];
929                 *y_max = (0xf0 & param[0]) << 4 | param[2];
930                 break;
931
932         case 4:
933                 if (synaptics_send_cmd(psmouse, ETP_FW_ID_QUERY, param))
934                         return -1;
935
936                 *x_max = (0x0f & param[0]) << 8 | param[1];
937                 *y_max = (0xf0 & param[0]) << 4 | param[2];
938                 traces = etd->capabilities[1];
939                 if ((traces < 2) || (traces > *x_max))
940                         return -1;
941
942                 *width = *x_max / (traces - 1);
943                 break;
944         }
945
946         return 0;
947 }
948
949 /*
950  * Set the appropriate event bits for the input subsystem
951  */
952 static int elantech_set_input_params(struct psmouse *psmouse)
953 {
954         struct input_dev *dev = psmouse->dev;
955         struct elantech_data *etd = psmouse->private;
956         unsigned int x_min = 0, y_min = 0, x_max = 0, y_max = 0, width = 0;
957
958         if (elantech_set_range(psmouse, &x_min, &y_min, &x_max, &y_max, &width))
959                 return -1;
960
961         __set_bit(EV_KEY, dev->evbit);
962         __set_bit(EV_ABS, dev->evbit);
963         __clear_bit(EV_REL, dev->evbit);
964
965         __set_bit(BTN_LEFT, dev->keybit);
966         __set_bit(BTN_RIGHT, dev->keybit);
967
968         __set_bit(BTN_TOUCH, dev->keybit);
969         __set_bit(BTN_TOOL_FINGER, dev->keybit);
970         __set_bit(BTN_TOOL_DOUBLETAP, dev->keybit);
971         __set_bit(BTN_TOOL_TRIPLETAP, dev->keybit);
972
973         switch (etd->hw_version) {
974         case 1:
975                 /* Rocker button */
976                 if (etd->fw_version < 0x020000 &&
977                     (etd->capabilities[0] & ETP_CAP_HAS_ROCKER)) {
978                         __set_bit(BTN_FORWARD, dev->keybit);
979                         __set_bit(BTN_BACK, dev->keybit);
980                 }
981                 input_set_abs_params(dev, ABS_X, x_min, x_max, 0, 0);
982                 input_set_abs_params(dev, ABS_Y, y_min, y_max, 0, 0);
983                 break;
984
985         case 2:
986                 __set_bit(BTN_TOOL_QUADTAP, dev->keybit);
987                 __set_bit(INPUT_PROP_SEMI_MT, dev->propbit);
988                 /* fall through */
989         case 3:
990                 input_set_abs_params(dev, ABS_X, x_min, x_max, 0, 0);
991                 input_set_abs_params(dev, ABS_Y, y_min, y_max, 0, 0);
992                 if (etd->reports_pressure) {
993                         input_set_abs_params(dev, ABS_PRESSURE, ETP_PMIN_V2,
994                                              ETP_PMAX_V2, 0, 0);
995                         input_set_abs_params(dev, ABS_TOOL_WIDTH, ETP_WMIN_V2,
996                                              ETP_WMAX_V2, 0, 0);
997                 }
998                 input_mt_init_slots(dev, 2);
999                 input_set_abs_params(dev, ABS_MT_POSITION_X, x_min, x_max, 0, 0);
1000                 input_set_abs_params(dev, ABS_MT_POSITION_Y, y_min, y_max, 0, 0);
1001                 break;
1002
1003         case 4:
1004                 __set_bit(BTN_TOOL_QUADTAP, dev->keybit);
1005                 /* For X to recognize me as touchpad. */
1006                 input_set_abs_params(dev, ABS_X, x_min, x_max, 0, 0);
1007                 input_set_abs_params(dev, ABS_Y, y_min, y_max, 0, 0);
1008                 /*
1009                  * range of pressure and width is the same as v2,
1010                  * report ABS_PRESSURE, ABS_TOOL_WIDTH for compatibility.
1011                  */
1012                 input_set_abs_params(dev, ABS_PRESSURE, ETP_PMIN_V2,
1013                                      ETP_PMAX_V2, 0, 0);
1014                 input_set_abs_params(dev, ABS_TOOL_WIDTH, ETP_WMIN_V2,
1015                                      ETP_WMAX_V2, 0, 0);
1016                 /* Multitouch capable pad, up to 5 fingers. */
1017                 input_mt_init_slots(dev, ETP_MAX_FINGERS);
1018                 input_set_abs_params(dev, ABS_MT_POSITION_X, x_min, x_max, 0, 0);
1019                 input_set_abs_params(dev, ABS_MT_POSITION_Y, y_min, y_max, 0, 0);
1020                 input_set_abs_params(dev, ABS_MT_PRESSURE, ETP_PMIN_V2,
1021                                      ETP_PMAX_V2, 0, 0);
1022                 /*
1023                  * The firmware reports how many trace lines the finger spans,
1024                  * convert to surface unit as Protocol-B requires.
1025                  */
1026                 input_set_abs_params(dev, ABS_MT_TOUCH_MAJOR, 0,
1027                                      ETP_WMAX_V2 * width, 0, 0);
1028                 break;
1029         }
1030
1031         etd->y_max = y_max;
1032         etd->width = width;
1033
1034         return 0;
1035 }
1036
1037 struct elantech_attr_data {
1038         size_t          field_offset;
1039         unsigned char   reg;
1040 };
1041
1042 /*
1043  * Display a register value by reading a sysfs entry
1044  */
1045 static ssize_t elantech_show_int_attr(struct psmouse *psmouse, void *data,
1046                                         char *buf)
1047 {
1048         struct elantech_data *etd = psmouse->private;
1049         struct elantech_attr_data *attr = data;
1050         unsigned char *reg = (unsigned char *) etd + attr->field_offset;
1051         int rc = 0;
1052
1053         if (attr->reg)
1054                 rc = elantech_read_reg(psmouse, attr->reg, reg);
1055
1056         return sprintf(buf, "0x%02x\n", (attr->reg && rc) ? -1 : *reg);
1057 }
1058
1059 /*
1060  * Write a register value by writing a sysfs entry
1061  */
1062 static ssize_t elantech_set_int_attr(struct psmouse *psmouse,
1063                                      void *data, const char *buf, size_t count)
1064 {
1065         struct elantech_data *etd = psmouse->private;
1066         struct elantech_attr_data *attr = data;
1067         unsigned char *reg = (unsigned char *) etd + attr->field_offset;
1068         unsigned long value;
1069         int err;
1070
1071         err = strict_strtoul(buf, 16, &value);
1072         if (err)
1073                 return err;
1074
1075         if (value > 0xff)
1076                 return -EINVAL;
1077
1078         /* Do we need to preserve some bits for version 2 hardware too? */
1079         if (etd->hw_version == 1) {
1080                 if (attr->reg == 0x10)
1081                         /* Force absolute mode always on */
1082                         value |= ETP_R10_ABSOLUTE_MODE;
1083                 else if (attr->reg == 0x11)
1084                         /* Force 4 byte mode always on */
1085                         value |= ETP_R11_4_BYTE_MODE;
1086         }
1087
1088         if (!attr->reg || elantech_write_reg(psmouse, attr->reg, value) == 0)
1089                 *reg = value;
1090
1091         return count;
1092 }
1093
1094 #define ELANTECH_INT_ATTR(_name, _register)                             \
1095         static struct elantech_attr_data elantech_attr_##_name = {      \
1096                 .field_offset = offsetof(struct elantech_data, _name),  \
1097                 .reg = _register,                                       \
1098         };                                                              \
1099         PSMOUSE_DEFINE_ATTR(_name, S_IWUSR | S_IRUGO,                   \
1100                             &elantech_attr_##_name,                     \
1101                             elantech_show_int_attr,                     \
1102                             elantech_set_int_attr)
1103
1104 ELANTECH_INT_ATTR(reg_07, 0x07);
1105 ELANTECH_INT_ATTR(reg_10, 0x10);
1106 ELANTECH_INT_ATTR(reg_11, 0x11);
1107 ELANTECH_INT_ATTR(reg_20, 0x20);
1108 ELANTECH_INT_ATTR(reg_21, 0x21);
1109 ELANTECH_INT_ATTR(reg_22, 0x22);
1110 ELANTECH_INT_ATTR(reg_23, 0x23);
1111 ELANTECH_INT_ATTR(reg_24, 0x24);
1112 ELANTECH_INT_ATTR(reg_25, 0x25);
1113 ELANTECH_INT_ATTR(reg_26, 0x26);
1114 ELANTECH_INT_ATTR(debug, 0);
1115 ELANTECH_INT_ATTR(paritycheck, 0);
1116
1117 static struct attribute *elantech_attrs[] = {
1118         &psmouse_attr_reg_07.dattr.attr,
1119         &psmouse_attr_reg_10.dattr.attr,
1120         &psmouse_attr_reg_11.dattr.attr,
1121         &psmouse_attr_reg_20.dattr.attr,
1122         &psmouse_attr_reg_21.dattr.attr,
1123         &psmouse_attr_reg_22.dattr.attr,
1124         &psmouse_attr_reg_23.dattr.attr,
1125         &psmouse_attr_reg_24.dattr.attr,
1126         &psmouse_attr_reg_25.dattr.attr,
1127         &psmouse_attr_reg_26.dattr.attr,
1128         &psmouse_attr_debug.dattr.attr,
1129         &psmouse_attr_paritycheck.dattr.attr,
1130         NULL
1131 };
1132
1133 static struct attribute_group elantech_attr_group = {
1134         .attrs = elantech_attrs,
1135 };
1136
1137 static bool elantech_is_signature_valid(const unsigned char *param)
1138 {
1139         static const unsigned char rates[] = { 200, 100, 80, 60, 40, 20, 10 };
1140         int i;
1141
1142         if (param[0] == 0)
1143                 return false;
1144
1145         if (param[1] == 0)
1146                 return true;
1147
1148         /*
1149          * Some hw_version >= 4 models have a revision higher then 20. Meaning
1150          * that param[2] may be 10 or 20, skip the rates check for these.
1151          */
1152         if ((param[0] & 0x0f) >= 0x06 && (param[1] & 0xaf) == 0x0f &&
1153             param[2] < 40)
1154                 return true;
1155
1156         for (i = 0; i < ARRAY_SIZE(rates); i++)
1157                 if (param[2] == rates[i])
1158                         return false;
1159
1160         return true;
1161 }
1162
1163 /*
1164  * Use magic knock to detect Elantech touchpad
1165  */
1166 int elantech_detect(struct psmouse *psmouse, bool set_properties)
1167 {
1168         struct ps2dev *ps2dev = &psmouse->ps2dev;
1169         unsigned char param[3];
1170
1171         ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
1172
1173         if (ps2_command(ps2dev,  NULL, PSMOUSE_CMD_DISABLE) ||
1174             ps2_command(ps2dev,  NULL, PSMOUSE_CMD_SETSCALE11) ||
1175             ps2_command(ps2dev,  NULL, PSMOUSE_CMD_SETSCALE11) ||
1176             ps2_command(ps2dev,  NULL, PSMOUSE_CMD_SETSCALE11) ||
1177             ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO)) {
1178                 psmouse_dbg(psmouse, "sending Elantech magic knock failed.\n");
1179                 return -1;
1180         }
1181
1182         /*
1183          * Report this in case there are Elantech models that use a different
1184          * set of magic numbers
1185          */
1186         if (param[0] != 0x3c || param[1] != 0x03 ||
1187             (param[2] != 0xc8 && param[2] != 0x00)) {
1188                 psmouse_dbg(psmouse,
1189                             "unexpected magic knock result 0x%02x, 0x%02x, 0x%02x.\n",
1190                             param[0], param[1], param[2]);
1191                 return -1;
1192         }
1193
1194         /*
1195          * Query touchpad's firmware version and see if it reports known
1196          * value to avoid mis-detection. Logitech mice are known to respond
1197          * to Elantech magic knock and there might be more.
1198          */
1199         if (synaptics_send_cmd(psmouse, ETP_FW_VERSION_QUERY, param)) {
1200                 psmouse_dbg(psmouse, "failed to query firmware version.\n");
1201                 return -1;
1202         }
1203
1204         psmouse_dbg(psmouse,
1205                     "Elantech version query result 0x%02x, 0x%02x, 0x%02x.\n",
1206                     param[0], param[1], param[2]);
1207
1208         if (!elantech_is_signature_valid(param)) {
1209                 psmouse_dbg(psmouse,
1210                             "Probably not a real Elantech touchpad. Aborting.\n");
1211                 return -1;
1212         }
1213
1214         if (set_properties) {
1215                 psmouse->vendor = "Elantech";
1216                 psmouse->name = "Touchpad";
1217         }
1218
1219         return 0;
1220 }
1221
1222 /*
1223  * Clean up sysfs entries when disconnecting
1224  */
1225 static void elantech_disconnect(struct psmouse *psmouse)
1226 {
1227         sysfs_remove_group(&psmouse->ps2dev.serio->dev.kobj,
1228                            &elantech_attr_group);
1229         kfree(psmouse->private);
1230         psmouse->private = NULL;
1231 }
1232
1233 /*
1234  * Put the touchpad back into absolute mode when reconnecting
1235  */
1236 static int elantech_reconnect(struct psmouse *psmouse)
1237 {
1238         if (elantech_detect(psmouse, 0))
1239                 return -1;
1240
1241         if (elantech_set_absolute_mode(psmouse)) {
1242                 psmouse_err(psmouse,
1243                             "failed to put touchpad back into absolute mode.\n");
1244                 return -1;
1245         }
1246
1247         return 0;
1248 }
1249
1250 /*
1251  * Some hw_version 3 models go into error state when we try to set
1252  * bit 3 and/or bit 1 of r10.
1253  */
1254 static const struct dmi_system_id no_hw_res_dmi_table[] = {
1255 #if defined(CONFIG_DMI) && defined(CONFIG_X86)
1256         {
1257                 /* Gigabyte U2442 */
1258                 .matches = {
1259                         DMI_MATCH(DMI_SYS_VENDOR, "GIGABYTE"),
1260                         DMI_MATCH(DMI_PRODUCT_NAME, "U2442"),
1261                 },
1262         },
1263 #endif
1264         { }
1265 };
1266
1267 /*
1268  * determine hardware version and set some properties according to it.
1269  */
1270 static int elantech_set_properties(struct elantech_data *etd)
1271 {
1272         /* This represents the version of IC body. */
1273         int ver = (etd->fw_version & 0x0f0000) >> 16;
1274
1275         /* Early version of Elan touchpads doesn't obey the rule. */
1276         if (etd->fw_version < 0x020030 || etd->fw_version == 0x020600)
1277                 etd->hw_version = 1;
1278         else {
1279                 switch (ver) {
1280                 case 2:
1281                 case 4:
1282                         etd->hw_version = 2;
1283                         break;
1284                 case 5:
1285                         etd->hw_version = 3;
1286                         break;
1287                 case 6:
1288                 case 7:
1289                 case 8:
1290                 case 9:
1291                 case 10:
1292                 case 13:
1293                         etd->hw_version = 4;
1294                         break;
1295                 default:
1296                         return -1;
1297                 }
1298         }
1299
1300         /*
1301          * Turn on packet checking by default.
1302          */
1303         etd->paritycheck = 1;
1304
1305         /*
1306          * This firmware suffers from misreporting coordinates when
1307          * a touch action starts causing the mouse cursor or scrolled page
1308          * to jump. Enable a workaround.
1309          */
1310         etd->jumpy_cursor =
1311                 (etd->fw_version == 0x020022 || etd->fw_version == 0x020600);
1312
1313         if (etd->hw_version > 1) {
1314                 /* For now show extra debug information */
1315                 etd->debug = 1;
1316
1317                 if (etd->fw_version >= 0x020800)
1318                         etd->reports_pressure = true;
1319         }
1320
1321         /* Enable real hardware resolution on hw_version 3 ? */
1322         etd->set_hw_resolution = !dmi_check_system(no_hw_res_dmi_table);
1323
1324         return 0;
1325 }
1326
1327 /*
1328  * Initialize the touchpad and create sysfs entries
1329  */
1330 int elantech_init(struct psmouse *psmouse)
1331 {
1332         struct elantech_data *etd;
1333         int i, error;
1334         unsigned char param[3];
1335
1336         psmouse->private = etd = kzalloc(sizeof(struct elantech_data), GFP_KERNEL);
1337         if (!etd)
1338                 return -ENOMEM;
1339
1340         etd->parity[0] = 1;
1341         for (i = 1; i < 256; i++)
1342                 etd->parity[i] = etd->parity[i & (i - 1)] ^ 1;
1343
1344         /*
1345          * Do the version query again so we can store the result
1346          */
1347         if (synaptics_send_cmd(psmouse, ETP_FW_VERSION_QUERY, param)) {
1348                 psmouse_err(psmouse, "failed to query firmware version.\n");
1349                 goto init_fail;
1350         }
1351         etd->fw_version = (param[0] << 16) | (param[1] << 8) | param[2];
1352
1353         if (elantech_set_properties(etd)) {
1354                 psmouse_err(psmouse, "unknown hardware version, aborting...\n");
1355                 goto init_fail;
1356         }
1357         psmouse_info(psmouse,
1358                      "assuming hardware version %d (with firmware version 0x%02x%02x%02x)\n",
1359                      etd->hw_version, param[0], param[1], param[2]);
1360
1361         if (synaptics_send_cmd(psmouse, ETP_CAPABILITIES_QUERY,
1362             etd->capabilities)) {
1363                 psmouse_err(psmouse, "failed to query capabilities.\n");
1364                 goto init_fail;
1365         }
1366         psmouse_info(psmouse,
1367                      "Synaptics capabilities query result 0x%02x, 0x%02x, 0x%02x.\n",
1368                      etd->capabilities[0], etd->capabilities[1],
1369                      etd->capabilities[2]);
1370
1371         if (elantech_set_absolute_mode(psmouse)) {
1372                 psmouse_err(psmouse,
1373                             "failed to put touchpad into absolute mode.\n");
1374                 goto init_fail;
1375         }
1376
1377         if (etd->fw_version == 0x381f17) {
1378                 etd->original_set_rate = psmouse->set_rate;
1379                 psmouse->set_rate = elantech_set_rate_restore_reg_07;
1380         }
1381
1382         if (elantech_set_input_params(psmouse)) {
1383                 psmouse_err(psmouse, "failed to query touchpad range.\n");
1384                 goto init_fail;
1385         }
1386
1387         error = sysfs_create_group(&psmouse->ps2dev.serio->dev.kobj,
1388                                    &elantech_attr_group);
1389         if (error) {
1390                 psmouse_err(psmouse,
1391                             "failed to create sysfs attributes, error: %d.\n",
1392                             error);
1393                 goto init_fail;
1394         }
1395
1396         psmouse->protocol_handler = elantech_process_byte;
1397         psmouse->disconnect = elantech_disconnect;
1398         psmouse->reconnect = elantech_reconnect;
1399         psmouse->pktsize = etd->hw_version > 1 ? 6 : 4;
1400
1401         return 0;
1402
1403  init_fail:
1404         kfree(etd);
1405         return -1;
1406 }