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