Input: elantech - fix touchpad initialization on Gigabyte U2442
[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         input_report_key(dev, BTN_LEFT, packet[0] & 0x01);
459         input_report_key(dev, BTN_RIGHT, packet[0] & 0x02);
460         input_report_abs(dev, ABS_PRESSURE, pres);
461         input_report_abs(dev, ABS_TOOL_WIDTH, width);
462
463         input_sync(dev);
464 }
465
466 static void elantech_input_sync_v4(struct psmouse *psmouse)
467 {
468         struct input_dev *dev = psmouse->dev;
469         unsigned char *packet = psmouse->packet;
470
471         input_report_key(dev, BTN_LEFT, packet[0] & 0x01);
472         input_report_key(dev, BTN_RIGHT, packet[0] & 0x02);
473         input_mt_report_pointer_emulation(dev, true);
474         input_sync(dev);
475 }
476
477 static void process_packet_status_v4(struct psmouse *psmouse)
478 {
479         struct input_dev *dev = psmouse->dev;
480         unsigned char *packet = psmouse->packet;
481         unsigned fingers;
482         int i;
483
484         /* notify finger state change */
485         fingers = packet[1] & 0x1f;
486         for (i = 0; i < ETP_MAX_FINGERS; i++) {
487                 if ((fingers & (1 << i)) == 0) {
488                         input_mt_slot(dev, i);
489                         input_mt_report_slot_state(dev, MT_TOOL_FINGER, false);
490                 }
491         }
492
493         elantech_input_sync_v4(psmouse);
494 }
495
496 static void process_packet_head_v4(struct psmouse *psmouse)
497 {
498         struct input_dev *dev = psmouse->dev;
499         struct elantech_data *etd = psmouse->private;
500         unsigned char *packet = psmouse->packet;
501         int id = ((packet[3] & 0xe0) >> 5) - 1;
502         int pres, traces;
503
504         if (id < 0)
505                 return;
506
507         etd->mt[id].x = ((packet[1] & 0x0f) << 8) | packet[2];
508         etd->mt[id].y = etd->y_max - (((packet[4] & 0x0f) << 8) | packet[5]);
509         pres = (packet[1] & 0xf0) | ((packet[4] & 0xf0) >> 4);
510         traces = (packet[0] & 0xf0) >> 4;
511
512         input_mt_slot(dev, id);
513         input_mt_report_slot_state(dev, MT_TOOL_FINGER, true);
514
515         input_report_abs(dev, ABS_MT_POSITION_X, etd->mt[id].x);
516         input_report_abs(dev, ABS_MT_POSITION_Y, etd->mt[id].y);
517         input_report_abs(dev, ABS_MT_PRESSURE, pres);
518         input_report_abs(dev, ABS_MT_TOUCH_MAJOR, traces * etd->width);
519         /* report this for backwards compatibility */
520         input_report_abs(dev, ABS_TOOL_WIDTH, traces);
521
522         elantech_input_sync_v4(psmouse);
523 }
524
525 static void process_packet_motion_v4(struct psmouse *psmouse)
526 {
527         struct input_dev *dev = psmouse->dev;
528         struct elantech_data *etd = psmouse->private;
529         unsigned char *packet = psmouse->packet;
530         int weight, delta_x1 = 0, delta_y1 = 0, delta_x2 = 0, delta_y2 = 0;
531         int id, sid;
532
533         id = ((packet[0] & 0xe0) >> 5) - 1;
534         if (id < 0)
535                 return;
536
537         sid = ((packet[3] & 0xe0) >> 5) - 1;
538         weight = (packet[0] & 0x10) ? ETP_WEIGHT_VALUE : 1;
539         /*
540          * Motion packets give us the delta of x, y values of specific fingers,
541          * but in two's complement. Let the compiler do the conversion for us.
542          * Also _enlarge_ the numbers to int, in case of overflow.
543          */
544         delta_x1 = (signed char)packet[1];
545         delta_y1 = (signed char)packet[2];
546         delta_x2 = (signed char)packet[4];
547         delta_y2 = (signed char)packet[5];
548
549         etd->mt[id].x += delta_x1 * weight;
550         etd->mt[id].y -= delta_y1 * weight;
551         input_mt_slot(dev, id);
552         input_report_abs(dev, ABS_MT_POSITION_X, etd->mt[id].x);
553         input_report_abs(dev, ABS_MT_POSITION_Y, etd->mt[id].y);
554
555         if (sid >= 0) {
556                 etd->mt[sid].x += delta_x2 * weight;
557                 etd->mt[sid].y -= delta_y2 * weight;
558                 input_mt_slot(dev, sid);
559                 input_report_abs(dev, ABS_MT_POSITION_X, etd->mt[sid].x);
560                 input_report_abs(dev, ABS_MT_POSITION_Y, etd->mt[sid].y);
561         }
562
563         elantech_input_sync_v4(psmouse);
564 }
565
566 static void elantech_report_absolute_v4(struct psmouse *psmouse,
567                                         int packet_type)
568 {
569         switch (packet_type) {
570         case PACKET_V4_STATUS:
571                 process_packet_status_v4(psmouse);
572                 break;
573
574         case PACKET_V4_HEAD:
575                 process_packet_head_v4(psmouse);
576                 break;
577
578         case PACKET_V4_MOTION:
579                 process_packet_motion_v4(psmouse);
580                 break;
581
582         case PACKET_UNKNOWN:
583         default:
584                 /* impossible to get here */
585                 break;
586         }
587 }
588
589 static int elantech_packet_check_v1(struct psmouse *psmouse)
590 {
591         struct elantech_data *etd = psmouse->private;
592         unsigned char *packet = psmouse->packet;
593         unsigned char p1, p2, p3;
594
595         /* Parity bits are placed differently */
596         if (etd->fw_version < 0x020000) {
597                 /* byte 0:  D   U  p1  p2   1  p3   R   L */
598                 p1 = (packet[0] & 0x20) >> 5;
599                 p2 = (packet[0] & 0x10) >> 4;
600         } else {
601                 /* byte 0: n1  n0  p2  p1   1  p3   R   L */
602                 p1 = (packet[0] & 0x10) >> 4;
603                 p2 = (packet[0] & 0x20) >> 5;
604         }
605
606         p3 = (packet[0] & 0x04) >> 2;
607
608         return etd->parity[packet[1]] == p1 &&
609                etd->parity[packet[2]] == p2 &&
610                etd->parity[packet[3]] == p3;
611 }
612
613 static int elantech_debounce_check_v2(struct psmouse *psmouse)
614 {
615         /*
616          * When we encounter packet that matches this exactly, it means the
617          * hardware is in debounce status. Just ignore the whole packet.
618          */
619         const u8 debounce_packet[] = { 0x84, 0xff, 0xff, 0x02, 0xff, 0xff };
620         unsigned char *packet = psmouse->packet;
621
622         return !memcmp(packet, debounce_packet, sizeof(debounce_packet));
623 }
624
625 static int elantech_packet_check_v2(struct psmouse *psmouse)
626 {
627         struct elantech_data *etd = psmouse->private;
628         unsigned char *packet = psmouse->packet;
629
630         /*
631          * V2 hardware has two flavors. Older ones that do not report pressure,
632          * and newer ones that reports pressure and width. With newer ones, all
633          * packets (1, 2, 3 finger touch) have the same constant bits. With
634          * older ones, 1/3 finger touch packets and 2 finger touch packets
635          * have different constant bits.
636          * With all three cases, if the constant bits are not exactly what I
637          * expected, I consider them invalid.
638          */
639         if (etd->reports_pressure)
640                 return (packet[0] & 0x0c) == 0x04 &&
641                        (packet[3] & 0x0f) == 0x02;
642
643         if ((packet[0] & 0xc0) == 0x80)
644                 return (packet[0] & 0x0c) == 0x0c &&
645                        (packet[3] & 0x0e) == 0x08;
646
647         return (packet[0] & 0x3c) == 0x3c &&
648                (packet[1] & 0xf0) == 0x00 &&
649                (packet[3] & 0x3e) == 0x38 &&
650                (packet[4] & 0xf0) == 0x00;
651 }
652
653 /*
654  * We check the constant bits to determine what packet type we get,
655  * so packet checking is mandatory for v3 and later hardware.
656  */
657 static int elantech_packet_check_v3(struct psmouse *psmouse)
658 {
659         const u8 debounce_packet[] = { 0xc4, 0xff, 0xff, 0x02, 0xff, 0xff };
660         unsigned char *packet = psmouse->packet;
661
662         /*
663          * check debounce first, it has the same signature in byte 0
664          * and byte 3 as PACKET_V3_HEAD.
665          */
666         if (!memcmp(packet, debounce_packet, sizeof(debounce_packet)))
667                 return PACKET_DEBOUNCE;
668
669         if ((packet[0] & 0x0c) == 0x04 && (packet[3] & 0xcf) == 0x02)
670                 return PACKET_V3_HEAD;
671
672         if ((packet[0] & 0x0c) == 0x0c && (packet[3] & 0xce) == 0x0c)
673                 return PACKET_V3_TAIL;
674
675         return PACKET_UNKNOWN;
676 }
677
678 static int elantech_packet_check_v4(struct psmouse *psmouse)
679 {
680         unsigned char *packet = psmouse->packet;
681
682         if ((packet[0] & 0x0c) == 0x04 &&
683             (packet[3] & 0x1f) == 0x11)
684                 return PACKET_V4_HEAD;
685
686         if ((packet[0] & 0x0c) == 0x04 &&
687             (packet[3] & 0x1f) == 0x12)
688                 return PACKET_V4_MOTION;
689
690         if ((packet[0] & 0x0c) == 0x04 &&
691             (packet[3] & 0x1f) == 0x10)
692                 return PACKET_V4_STATUS;
693
694         return PACKET_UNKNOWN;
695 }
696
697 /*
698  * Process byte stream from mouse and handle complete packets
699  */
700 static psmouse_ret_t elantech_process_byte(struct psmouse *psmouse)
701 {
702         struct elantech_data *etd = psmouse->private;
703         int packet_type;
704
705         if (psmouse->pktcnt < psmouse->pktsize)
706                 return PSMOUSE_GOOD_DATA;
707
708         if (etd->debug > 1)
709                 elantech_packet_dump(psmouse);
710
711         switch (etd->hw_version) {
712         case 1:
713                 if (etd->paritycheck && !elantech_packet_check_v1(psmouse))
714                         return PSMOUSE_BAD_DATA;
715
716                 elantech_report_absolute_v1(psmouse);
717                 break;
718
719         case 2:
720                 /* ignore debounce */
721                 if (elantech_debounce_check_v2(psmouse))
722                         return PSMOUSE_FULL_PACKET;
723
724                 if (etd->paritycheck && !elantech_packet_check_v2(psmouse))
725                         return PSMOUSE_BAD_DATA;
726
727                 elantech_report_absolute_v2(psmouse);
728                 break;
729
730         case 3:
731                 packet_type = elantech_packet_check_v3(psmouse);
732                 /* ignore debounce */
733                 if (packet_type == PACKET_DEBOUNCE)
734                         return PSMOUSE_FULL_PACKET;
735
736                 if (packet_type == PACKET_UNKNOWN)
737                         return PSMOUSE_BAD_DATA;
738
739                 elantech_report_absolute_v3(psmouse, packet_type);
740                 break;
741
742         case 4:
743                 packet_type = elantech_packet_check_v4(psmouse);
744                 if (packet_type == PACKET_UNKNOWN)
745                         return PSMOUSE_BAD_DATA;
746
747                 elantech_report_absolute_v4(psmouse, packet_type);
748                 break;
749         }
750
751         return PSMOUSE_FULL_PACKET;
752 }
753
754 /*
755  * Put the touchpad into absolute mode
756  */
757 static int elantech_set_absolute_mode(struct psmouse *psmouse)
758 {
759         struct elantech_data *etd = psmouse->private;
760         unsigned char val;
761         int tries = ETP_READ_BACK_TRIES;
762         int rc = 0;
763
764         switch (etd->hw_version) {
765         case 1:
766                 etd->reg_10 = 0x16;
767                 etd->reg_11 = 0x8f;
768                 if (elantech_write_reg(psmouse, 0x10, etd->reg_10) ||
769                     elantech_write_reg(psmouse, 0x11, etd->reg_11)) {
770                         rc = -1;
771                 }
772                 break;
773
774         case 2:
775                                         /* Windows driver values */
776                 etd->reg_10 = 0x54;
777                 etd->reg_11 = 0x88;     /* 0x8a */
778                 etd->reg_21 = 0x60;     /* 0x00 */
779                 if (elantech_write_reg(psmouse, 0x10, etd->reg_10) ||
780                     elantech_write_reg(psmouse, 0x11, etd->reg_11) ||
781                     elantech_write_reg(psmouse, 0x21, etd->reg_21)) {
782                         rc = -1;
783                 }
784                 break;
785
786         case 3:
787                 if (etd->set_hw_resolution)
788                         etd->reg_10 = 0x0b;
789                 else
790                         etd->reg_10 = 0x03;
791
792                 if (elantech_write_reg(psmouse, 0x10, etd->reg_10))
793                         rc = -1;
794
795                 break;
796
797         case 4:
798                 etd->reg_07 = 0x01;
799                 if (elantech_write_reg(psmouse, 0x07, etd->reg_07))
800                         rc = -1;
801
802                 goto skip_readback_reg_10; /* v4 has no reg 0x10 to read */
803         }
804
805         if (rc == 0) {
806                 /*
807                  * Read back reg 0x10. For hardware version 1 we must make
808                  * sure the absolute mode bit is set. For hardware version 2
809                  * the touchpad is probably initializing and not ready until
810                  * we read back the value we just wrote.
811                  */
812                 do {
813                         rc = elantech_read_reg(psmouse, 0x10, &val);
814                         if (rc == 0)
815                                 break;
816                         tries--;
817                         elantech_debug("retrying read (%d).\n", tries);
818                         msleep(ETP_READ_BACK_DELAY);
819                 } while (tries > 0);
820
821                 if (rc) {
822                         psmouse_err(psmouse,
823                                     "failed to read back register 0x10.\n");
824                 } else if (etd->hw_version == 1 &&
825                            !(val & ETP_R10_ABSOLUTE_MODE)) {
826                         psmouse_err(psmouse,
827                                     "touchpad refuses to switch to absolute mode.\n");
828                         rc = -1;
829                 }
830         }
831
832  skip_readback_reg_10:
833         if (rc)
834                 psmouse_err(psmouse, "failed to initialise registers.\n");
835
836         return rc;
837 }
838
839 static int elantech_set_range(struct psmouse *psmouse,
840                               unsigned int *x_min, unsigned int *y_min,
841                               unsigned int *x_max, unsigned int *y_max,
842                               unsigned int *width)
843 {
844         struct elantech_data *etd = psmouse->private;
845         unsigned char param[3];
846         unsigned char traces;
847
848         switch (etd->hw_version) {
849         case 1:
850                 *x_min = ETP_XMIN_V1;
851                 *y_min = ETP_YMIN_V1;
852                 *x_max = ETP_XMAX_V1;
853                 *y_max = ETP_YMAX_V1;
854                 break;
855
856         case 2:
857                 if (etd->fw_version == 0x020800 ||
858                     etd->fw_version == 0x020b00 ||
859                     etd->fw_version == 0x020030) {
860                         *x_min = ETP_XMIN_V2;
861                         *y_min = ETP_YMIN_V2;
862                         *x_max = ETP_XMAX_V2;
863                         *y_max = ETP_YMAX_V2;
864                 } else {
865                         int i;
866                         int fixed_dpi;
867
868                         i = (etd->fw_version > 0x020800 &&
869                              etd->fw_version < 0x020900) ? 1 : 2;
870
871                         if (synaptics_send_cmd(psmouse, ETP_FW_ID_QUERY, param))
872                                 return -1;
873
874                         fixed_dpi = param[1] & 0x10;
875
876                         if (((etd->fw_version >> 16) == 0x14) && fixed_dpi) {
877                                 if (synaptics_send_cmd(psmouse, ETP_SAMPLE_QUERY, param))
878                                         return -1;
879
880                                 *x_max = (etd->capabilities[1] - i) * param[1] / 2;
881                                 *y_max = (etd->capabilities[2] - i) * param[2] / 2;
882                         } else if (etd->fw_version == 0x040216) {
883                                 *x_max = 819;
884                                 *y_max = 405;
885                         } else if (etd->fw_version == 0x040219 || etd->fw_version == 0x040215) {
886                                 *x_max = 900;
887                                 *y_max = 500;
888                         } else {
889                                 *x_max = (etd->capabilities[1] - i) * 64;
890                                 *y_max = (etd->capabilities[2] - i) * 64;
891                         }
892                 }
893                 break;
894
895         case 3:
896                 if (synaptics_send_cmd(psmouse, ETP_FW_ID_QUERY, param))
897                         return -1;
898
899                 *x_max = (0x0f & param[0]) << 8 | param[1];
900                 *y_max = (0xf0 & param[0]) << 4 | param[2];
901                 break;
902
903         case 4:
904                 if (synaptics_send_cmd(psmouse, ETP_FW_ID_QUERY, param))
905                         return -1;
906
907                 *x_max = (0x0f & param[0]) << 8 | param[1];
908                 *y_max = (0xf0 & param[0]) << 4 | param[2];
909                 traces = etd->capabilities[1];
910                 if ((traces < 2) || (traces > *x_max))
911                         return -1;
912
913                 *width = *x_max / (traces - 1);
914                 break;
915         }
916
917         return 0;
918 }
919
920 /*
921  * Set the appropriate event bits for the input subsystem
922  */
923 static int elantech_set_input_params(struct psmouse *psmouse)
924 {
925         struct input_dev *dev = psmouse->dev;
926         struct elantech_data *etd = psmouse->private;
927         unsigned int x_min = 0, y_min = 0, x_max = 0, y_max = 0, width = 0;
928
929         if (elantech_set_range(psmouse, &x_min, &y_min, &x_max, &y_max, &width))
930                 return -1;
931
932         __set_bit(EV_KEY, dev->evbit);
933         __set_bit(EV_ABS, dev->evbit);
934         __clear_bit(EV_REL, dev->evbit);
935
936         __set_bit(BTN_LEFT, dev->keybit);
937         __set_bit(BTN_RIGHT, dev->keybit);
938
939         __set_bit(BTN_TOUCH, dev->keybit);
940         __set_bit(BTN_TOOL_FINGER, dev->keybit);
941         __set_bit(BTN_TOOL_DOUBLETAP, dev->keybit);
942         __set_bit(BTN_TOOL_TRIPLETAP, dev->keybit);
943
944         switch (etd->hw_version) {
945         case 1:
946                 /* Rocker button */
947                 if (etd->fw_version < 0x020000 &&
948                     (etd->capabilities[0] & ETP_CAP_HAS_ROCKER)) {
949                         __set_bit(BTN_FORWARD, dev->keybit);
950                         __set_bit(BTN_BACK, dev->keybit);
951                 }
952                 input_set_abs_params(dev, ABS_X, x_min, x_max, 0, 0);
953                 input_set_abs_params(dev, ABS_Y, y_min, y_max, 0, 0);
954                 break;
955
956         case 2:
957                 __set_bit(BTN_TOOL_QUADTAP, dev->keybit);
958                 __set_bit(INPUT_PROP_SEMI_MT, dev->propbit);
959                 /* fall through */
960         case 3:
961                 input_set_abs_params(dev, ABS_X, x_min, x_max, 0, 0);
962                 input_set_abs_params(dev, ABS_Y, y_min, y_max, 0, 0);
963                 if (etd->reports_pressure) {
964                         input_set_abs_params(dev, ABS_PRESSURE, ETP_PMIN_V2,
965                                              ETP_PMAX_V2, 0, 0);
966                         input_set_abs_params(dev, ABS_TOOL_WIDTH, ETP_WMIN_V2,
967                                              ETP_WMAX_V2, 0, 0);
968                 }
969                 input_mt_init_slots(dev, 2);
970                 input_set_abs_params(dev, ABS_MT_POSITION_X, x_min, x_max, 0, 0);
971                 input_set_abs_params(dev, ABS_MT_POSITION_Y, y_min, y_max, 0, 0);
972                 break;
973
974         case 4:
975                 __set_bit(BTN_TOOL_QUADTAP, dev->keybit);
976                 /* For X to recognize me as touchpad. */
977                 input_set_abs_params(dev, ABS_X, x_min, x_max, 0, 0);
978                 input_set_abs_params(dev, ABS_Y, y_min, y_max, 0, 0);
979                 /*
980                  * range of pressure and width is the same as v2,
981                  * report ABS_PRESSURE, ABS_TOOL_WIDTH for compatibility.
982                  */
983                 input_set_abs_params(dev, ABS_PRESSURE, ETP_PMIN_V2,
984                                      ETP_PMAX_V2, 0, 0);
985                 input_set_abs_params(dev, ABS_TOOL_WIDTH, ETP_WMIN_V2,
986                                      ETP_WMAX_V2, 0, 0);
987                 /* Multitouch capable pad, up to 5 fingers. */
988                 input_mt_init_slots(dev, ETP_MAX_FINGERS);
989                 input_set_abs_params(dev, ABS_MT_POSITION_X, x_min, x_max, 0, 0);
990                 input_set_abs_params(dev, ABS_MT_POSITION_Y, y_min, y_max, 0, 0);
991                 input_set_abs_params(dev, ABS_MT_PRESSURE, ETP_PMIN_V2,
992                                      ETP_PMAX_V2, 0, 0);
993                 /*
994                  * The firmware reports how many trace lines the finger spans,
995                  * convert to surface unit as Protocol-B requires.
996                  */
997                 input_set_abs_params(dev, ABS_MT_TOUCH_MAJOR, 0,
998                                      ETP_WMAX_V2 * width, 0, 0);
999                 break;
1000         }
1001
1002         etd->y_max = y_max;
1003         etd->width = width;
1004
1005         return 0;
1006 }
1007
1008 struct elantech_attr_data {
1009         size_t          field_offset;
1010         unsigned char   reg;
1011 };
1012
1013 /*
1014  * Display a register value by reading a sysfs entry
1015  */
1016 static ssize_t elantech_show_int_attr(struct psmouse *psmouse, void *data,
1017                                         char *buf)
1018 {
1019         struct elantech_data *etd = psmouse->private;
1020         struct elantech_attr_data *attr = data;
1021         unsigned char *reg = (unsigned char *) etd + attr->field_offset;
1022         int rc = 0;
1023
1024         if (attr->reg)
1025                 rc = elantech_read_reg(psmouse, attr->reg, reg);
1026
1027         return sprintf(buf, "0x%02x\n", (attr->reg && rc) ? -1 : *reg);
1028 }
1029
1030 /*
1031  * Write a register value by writing a sysfs entry
1032  */
1033 static ssize_t elantech_set_int_attr(struct psmouse *psmouse,
1034                                      void *data, const char *buf, size_t count)
1035 {
1036         struct elantech_data *etd = psmouse->private;
1037         struct elantech_attr_data *attr = data;
1038         unsigned char *reg = (unsigned char *) etd + attr->field_offset;
1039         unsigned long value;
1040         int err;
1041
1042         err = strict_strtoul(buf, 16, &value);
1043         if (err)
1044                 return err;
1045
1046         if (value > 0xff)
1047                 return -EINVAL;
1048
1049         /* Do we need to preserve some bits for version 2 hardware too? */
1050         if (etd->hw_version == 1) {
1051                 if (attr->reg == 0x10)
1052                         /* Force absolute mode always on */
1053                         value |= ETP_R10_ABSOLUTE_MODE;
1054                 else if (attr->reg == 0x11)
1055                         /* Force 4 byte mode always on */
1056                         value |= ETP_R11_4_BYTE_MODE;
1057         }
1058
1059         if (!attr->reg || elantech_write_reg(psmouse, attr->reg, value) == 0)
1060                 *reg = value;
1061
1062         return count;
1063 }
1064
1065 #define ELANTECH_INT_ATTR(_name, _register)                             \
1066         static struct elantech_attr_data elantech_attr_##_name = {      \
1067                 .field_offset = offsetof(struct elantech_data, _name),  \
1068                 .reg = _register,                                       \
1069         };                                                              \
1070         PSMOUSE_DEFINE_ATTR(_name, S_IWUSR | S_IRUGO,                   \
1071                             &elantech_attr_##_name,                     \
1072                             elantech_show_int_attr,                     \
1073                             elantech_set_int_attr)
1074
1075 ELANTECH_INT_ATTR(reg_07, 0x07);
1076 ELANTECH_INT_ATTR(reg_10, 0x10);
1077 ELANTECH_INT_ATTR(reg_11, 0x11);
1078 ELANTECH_INT_ATTR(reg_20, 0x20);
1079 ELANTECH_INT_ATTR(reg_21, 0x21);
1080 ELANTECH_INT_ATTR(reg_22, 0x22);
1081 ELANTECH_INT_ATTR(reg_23, 0x23);
1082 ELANTECH_INT_ATTR(reg_24, 0x24);
1083 ELANTECH_INT_ATTR(reg_25, 0x25);
1084 ELANTECH_INT_ATTR(reg_26, 0x26);
1085 ELANTECH_INT_ATTR(debug, 0);
1086 ELANTECH_INT_ATTR(paritycheck, 0);
1087
1088 static struct attribute *elantech_attrs[] = {
1089         &psmouse_attr_reg_07.dattr.attr,
1090         &psmouse_attr_reg_10.dattr.attr,
1091         &psmouse_attr_reg_11.dattr.attr,
1092         &psmouse_attr_reg_20.dattr.attr,
1093         &psmouse_attr_reg_21.dattr.attr,
1094         &psmouse_attr_reg_22.dattr.attr,
1095         &psmouse_attr_reg_23.dattr.attr,
1096         &psmouse_attr_reg_24.dattr.attr,
1097         &psmouse_attr_reg_25.dattr.attr,
1098         &psmouse_attr_reg_26.dattr.attr,
1099         &psmouse_attr_debug.dattr.attr,
1100         &psmouse_attr_paritycheck.dattr.attr,
1101         NULL
1102 };
1103
1104 static struct attribute_group elantech_attr_group = {
1105         .attrs = elantech_attrs,
1106 };
1107
1108 static bool elantech_is_signature_valid(const unsigned char *param)
1109 {
1110         static const unsigned char rates[] = { 200, 100, 80, 60, 40, 20, 10 };
1111         int i;
1112
1113         if (param[0] == 0)
1114                 return false;
1115
1116         if (param[1] == 0)
1117                 return true;
1118
1119         for (i = 0; i < ARRAY_SIZE(rates); i++)
1120                 if (param[2] == rates[i])
1121                         return false;
1122
1123         return true;
1124 }
1125
1126 /*
1127  * Use magic knock to detect Elantech touchpad
1128  */
1129 int elantech_detect(struct psmouse *psmouse, bool set_properties)
1130 {
1131         struct ps2dev *ps2dev = &psmouse->ps2dev;
1132         unsigned char param[3];
1133
1134         ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
1135
1136         if (ps2_command(ps2dev,  NULL, PSMOUSE_CMD_DISABLE) ||
1137             ps2_command(ps2dev,  NULL, PSMOUSE_CMD_SETSCALE11) ||
1138             ps2_command(ps2dev,  NULL, PSMOUSE_CMD_SETSCALE11) ||
1139             ps2_command(ps2dev,  NULL, PSMOUSE_CMD_SETSCALE11) ||
1140             ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO)) {
1141                 psmouse_dbg(psmouse, "sending Elantech magic knock failed.\n");
1142                 return -1;
1143         }
1144
1145         /*
1146          * Report this in case there are Elantech models that use a different
1147          * set of magic numbers
1148          */
1149         if (param[0] != 0x3c || param[1] != 0x03 ||
1150             (param[2] != 0xc8 && param[2] != 0x00)) {
1151                 psmouse_dbg(psmouse,
1152                             "unexpected magic knock result 0x%02x, 0x%02x, 0x%02x.\n",
1153                             param[0], param[1], param[2]);
1154                 return -1;
1155         }
1156
1157         /*
1158          * Query touchpad's firmware version and see if it reports known
1159          * value to avoid mis-detection. Logitech mice are known to respond
1160          * to Elantech magic knock and there might be more.
1161          */
1162         if (synaptics_send_cmd(psmouse, ETP_FW_VERSION_QUERY, param)) {
1163                 psmouse_dbg(psmouse, "failed to query firmware version.\n");
1164                 return -1;
1165         }
1166
1167         psmouse_dbg(psmouse,
1168                     "Elantech version query result 0x%02x, 0x%02x, 0x%02x.\n",
1169                     param[0], param[1], param[2]);
1170
1171         if (!elantech_is_signature_valid(param)) {
1172                 psmouse_dbg(psmouse,
1173                             "Probably not a real Elantech touchpad. Aborting.\n");
1174                 return -1;
1175         }
1176
1177         if (set_properties) {
1178                 psmouse->vendor = "Elantech";
1179                 psmouse->name = "Touchpad";
1180         }
1181
1182         return 0;
1183 }
1184
1185 /*
1186  * Clean up sysfs entries when disconnecting
1187  */
1188 static void elantech_disconnect(struct psmouse *psmouse)
1189 {
1190         sysfs_remove_group(&psmouse->ps2dev.serio->dev.kobj,
1191                            &elantech_attr_group);
1192         kfree(psmouse->private);
1193         psmouse->private = NULL;
1194 }
1195
1196 /*
1197  * Put the touchpad back into absolute mode when reconnecting
1198  */
1199 static int elantech_reconnect(struct psmouse *psmouse)
1200 {
1201         if (elantech_detect(psmouse, 0))
1202                 return -1;
1203
1204         if (elantech_set_absolute_mode(psmouse)) {
1205                 psmouse_err(psmouse,
1206                             "failed to put touchpad back into absolute mode.\n");
1207                 return -1;
1208         }
1209
1210         return 0;
1211 }
1212
1213 /*
1214  * Some hw_version 3 models go into error state when we try to set bit 3 of r10
1215  */
1216 static const struct dmi_system_id no_hw_res_dmi_table[] = {
1217 #if defined(CONFIG_DMI) && defined(CONFIG_X86)
1218         {
1219                 /* Gigabyte U2442 */
1220                 .matches = {
1221                         DMI_MATCH(DMI_SYS_VENDOR, "GIGABYTE"),
1222                         DMI_MATCH(DMI_PRODUCT_NAME, "U2442"),
1223                 },
1224         },
1225 #endif
1226         { }
1227 };
1228
1229 /*
1230  * determine hardware version and set some properties according to it.
1231  */
1232 static int elantech_set_properties(struct elantech_data *etd)
1233 {
1234         /* This represents the version of IC body. */
1235         int ver = (etd->fw_version & 0x0f0000) >> 16;
1236
1237         /* Early version of Elan touchpads doesn't obey the rule. */
1238         if (etd->fw_version < 0x020030 || etd->fw_version == 0x020600)
1239                 etd->hw_version = 1;
1240         else {
1241                 switch (ver) {
1242                 case 2:
1243                 case 4:
1244                         etd->hw_version = 2;
1245                         break;
1246                 case 5:
1247                         etd->hw_version = 3;
1248                         break;
1249                 case 6:
1250                         etd->hw_version = 4;
1251                         break;
1252                 default:
1253                         return -1;
1254                 }
1255         }
1256
1257         /*
1258          * Turn on packet checking by default.
1259          */
1260         etd->paritycheck = 1;
1261
1262         /*
1263          * This firmware suffers from misreporting coordinates when
1264          * a touch action starts causing the mouse cursor or scrolled page
1265          * to jump. Enable a workaround.
1266          */
1267         etd->jumpy_cursor =
1268                 (etd->fw_version == 0x020022 || etd->fw_version == 0x020600);
1269
1270         if (etd->hw_version > 1) {
1271                 /* For now show extra debug information */
1272                 etd->debug = 1;
1273
1274                 if (etd->fw_version >= 0x020800)
1275                         etd->reports_pressure = true;
1276         }
1277
1278         /* Enable real hardware resolution on hw_version 3 ? */
1279         etd->set_hw_resolution = !dmi_check_system(no_hw_res_dmi_table);
1280
1281         return 0;
1282 }
1283
1284 /*
1285  * Initialize the touchpad and create sysfs entries
1286  */
1287 int elantech_init(struct psmouse *psmouse)
1288 {
1289         struct elantech_data *etd;
1290         int i, error;
1291         unsigned char param[3];
1292
1293         psmouse->private = etd = kzalloc(sizeof(struct elantech_data), GFP_KERNEL);
1294         if (!etd)
1295                 return -ENOMEM;
1296
1297         etd->parity[0] = 1;
1298         for (i = 1; i < 256; i++)
1299                 etd->parity[i] = etd->parity[i & (i - 1)] ^ 1;
1300
1301         /*
1302          * Do the version query again so we can store the result
1303          */
1304         if (synaptics_send_cmd(psmouse, ETP_FW_VERSION_QUERY, param)) {
1305                 psmouse_err(psmouse, "failed to query firmware version.\n");
1306                 goto init_fail;
1307         }
1308         etd->fw_version = (param[0] << 16) | (param[1] << 8) | param[2];
1309
1310         if (elantech_set_properties(etd)) {
1311                 psmouse_err(psmouse, "unknown hardware version, aborting...\n");
1312                 goto init_fail;
1313         }
1314         psmouse_info(psmouse,
1315                      "assuming hardware version %d (with firmware version 0x%02x%02x%02x)\n",
1316                      etd->hw_version, param[0], param[1], param[2]);
1317
1318         if (synaptics_send_cmd(psmouse, ETP_CAPABILITIES_QUERY,
1319             etd->capabilities)) {
1320                 psmouse_err(psmouse, "failed to query capabilities.\n");
1321                 goto init_fail;
1322         }
1323         psmouse_info(psmouse,
1324                      "Synaptics capabilities query result 0x%02x, 0x%02x, 0x%02x.\n",
1325                      etd->capabilities[0], etd->capabilities[1],
1326                      etd->capabilities[2]);
1327
1328         if (elantech_set_absolute_mode(psmouse)) {
1329                 psmouse_err(psmouse,
1330                             "failed to put touchpad into absolute mode.\n");
1331                 goto init_fail;
1332         }
1333
1334         if (elantech_set_input_params(psmouse)) {
1335                 psmouse_err(psmouse, "failed to query touchpad range.\n");
1336                 goto init_fail;
1337         }
1338
1339         error = sysfs_create_group(&psmouse->ps2dev.serio->dev.kobj,
1340                                    &elantech_attr_group);
1341         if (error) {
1342                 psmouse_err(psmouse,
1343                             "failed to create sysfs attributes, error: %d.\n",
1344                             error);
1345                 goto init_fail;
1346         }
1347
1348         psmouse->protocol_handler = elantech_process_byte;
1349         psmouse->disconnect = elantech_disconnect;
1350         psmouse->reconnect = elantech_reconnect;
1351         psmouse->pktsize = etd->hw_version > 1 ? 6 : 4;
1352
1353         return 0;
1354
1355  init_fail:
1356         kfree(etd);
1357         return -1;
1358 }