xref: /openbmc/linux/drivers/input/mouse/elantech.c (revision 6a551c11)
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 <asm/unaligned.h>
22 #include "psmouse.h"
23 #include "elantech.h"
24 
25 #define elantech_debug(fmt, ...)					\
26 	do {								\
27 		if (etd->debug)						\
28 			psmouse_printk(KERN_DEBUG, psmouse,		\
29 					fmt, ##__VA_ARGS__);		\
30 	} while (0)
31 
32 /*
33  * Send a Synaptics style sliced query command
34  */
35 static int synaptics_send_cmd(struct psmouse *psmouse, unsigned char c,
36 				unsigned char *param)
37 {
38 	if (psmouse_sliced_command(psmouse, c) ||
39 	    ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_GETINFO)) {
40 		psmouse_err(psmouse, "%s query 0x%02x failed.\n", __func__, c);
41 		return -1;
42 	}
43 
44 	return 0;
45 }
46 
47 /*
48  * V3 and later support this fast command
49  */
50 static int elantech_send_cmd(struct psmouse *psmouse, unsigned char c,
51 				unsigned char *param)
52 {
53 	struct ps2dev *ps2dev = &psmouse->ps2dev;
54 
55 	if (ps2_command(ps2dev, NULL, ETP_PS2_CUSTOM_COMMAND) ||
56 	    ps2_command(ps2dev, NULL, c) ||
57 	    ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO)) {
58 		psmouse_err(psmouse, "%s query 0x%02x failed.\n", __func__, c);
59 		return -1;
60 	}
61 
62 	return 0;
63 }
64 
65 /*
66  * A retrying version of ps2_command
67  */
68 static int elantech_ps2_command(struct psmouse *psmouse,
69 				unsigned char *param, int command)
70 {
71 	struct ps2dev *ps2dev = &psmouse->ps2dev;
72 	struct elantech_data *etd = psmouse->private;
73 	int rc;
74 	int tries = ETP_PS2_COMMAND_TRIES;
75 
76 	do {
77 		rc = ps2_command(ps2dev, param, command);
78 		if (rc == 0)
79 			break;
80 		tries--;
81 		elantech_debug("retrying ps2 command 0x%02x (%d).\n",
82 				command, tries);
83 		msleep(ETP_PS2_COMMAND_DELAY);
84 	} while (tries > 0);
85 
86 	if (rc)
87 		psmouse_err(psmouse, "ps2 command 0x%02x failed.\n", command);
88 
89 	return rc;
90 }
91 
92 /*
93  * Send an Elantech style special command to read a value from a register
94  */
95 static int elantech_read_reg(struct psmouse *psmouse, unsigned char reg,
96 				unsigned char *val)
97 {
98 	struct elantech_data *etd = psmouse->private;
99 	unsigned char param[3];
100 	int rc = 0;
101 
102 	if (reg < 0x07 || reg > 0x26)
103 		return -1;
104 
105 	if (reg > 0x11 && reg < 0x20)
106 		return -1;
107 
108 	switch (etd->hw_version) {
109 	case 1:
110 		if (psmouse_sliced_command(psmouse, ETP_REGISTER_READ) ||
111 		    psmouse_sliced_command(psmouse, reg) ||
112 		    ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_GETINFO)) {
113 			rc = -1;
114 		}
115 		break;
116 
117 	case 2:
118 		if (elantech_ps2_command(psmouse,  NULL, ETP_PS2_CUSTOM_COMMAND) ||
119 		    elantech_ps2_command(psmouse,  NULL, ETP_REGISTER_READ) ||
120 		    elantech_ps2_command(psmouse,  NULL, ETP_PS2_CUSTOM_COMMAND) ||
121 		    elantech_ps2_command(psmouse,  NULL, reg) ||
122 		    elantech_ps2_command(psmouse, param, PSMOUSE_CMD_GETINFO)) {
123 			rc = -1;
124 		}
125 		break;
126 
127 	case 3 ... 4:
128 		if (elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
129 		    elantech_ps2_command(psmouse, NULL, ETP_REGISTER_READWRITE) ||
130 		    elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
131 		    elantech_ps2_command(psmouse, NULL, reg) ||
132 		    elantech_ps2_command(psmouse, param, PSMOUSE_CMD_GETINFO)) {
133 			rc = -1;
134 		}
135 		break;
136 	}
137 
138 	if (rc)
139 		psmouse_err(psmouse, "failed to read register 0x%02x.\n", reg);
140 	else if (etd->hw_version != 4)
141 		*val = param[0];
142 	else
143 		*val = param[1];
144 
145 	return rc;
146 }
147 
148 /*
149  * Send an Elantech style special command to write a register with a value
150  */
151 static int elantech_write_reg(struct psmouse *psmouse, unsigned char reg,
152 				unsigned char val)
153 {
154 	struct elantech_data *etd = psmouse->private;
155 	int rc = 0;
156 
157 	if (reg < 0x07 || reg > 0x26)
158 		return -1;
159 
160 	if (reg > 0x11 && reg < 0x20)
161 		return -1;
162 
163 	switch (etd->hw_version) {
164 	case 1:
165 		if (psmouse_sliced_command(psmouse, ETP_REGISTER_WRITE) ||
166 		    psmouse_sliced_command(psmouse, reg) ||
167 		    psmouse_sliced_command(psmouse, val) ||
168 		    ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11)) {
169 			rc = -1;
170 		}
171 		break;
172 
173 	case 2:
174 		if (elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
175 		    elantech_ps2_command(psmouse, NULL, ETP_REGISTER_WRITE) ||
176 		    elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
177 		    elantech_ps2_command(psmouse, NULL, reg) ||
178 		    elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
179 		    elantech_ps2_command(psmouse, NULL, val) ||
180 		    elantech_ps2_command(psmouse, NULL, PSMOUSE_CMD_SETSCALE11)) {
181 			rc = -1;
182 		}
183 		break;
184 
185 	case 3:
186 		if (elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
187 		    elantech_ps2_command(psmouse, NULL, ETP_REGISTER_READWRITE) ||
188 		    elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
189 		    elantech_ps2_command(psmouse, NULL, reg) ||
190 		    elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
191 		    elantech_ps2_command(psmouse, NULL, val) ||
192 		    elantech_ps2_command(psmouse, NULL, PSMOUSE_CMD_SETSCALE11)) {
193 			rc = -1;
194 		}
195 		break;
196 
197 	case 4:
198 		if (elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
199 		    elantech_ps2_command(psmouse, NULL, ETP_REGISTER_READWRITE) ||
200 		    elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
201 		    elantech_ps2_command(psmouse, NULL, reg) ||
202 		    elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
203 		    elantech_ps2_command(psmouse, NULL, ETP_REGISTER_READWRITE) ||
204 		    elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
205 		    elantech_ps2_command(psmouse, NULL, val) ||
206 		    elantech_ps2_command(psmouse, NULL, PSMOUSE_CMD_SETSCALE11)) {
207 			rc = -1;
208 		}
209 		break;
210 	}
211 
212 	if (rc)
213 		psmouse_err(psmouse,
214 			    "failed to write register 0x%02x with value 0x%02x.\n",
215 			    reg, val);
216 
217 	return rc;
218 }
219 
220 /*
221  * Dump a complete mouse movement packet to the syslog
222  */
223 static void elantech_packet_dump(struct psmouse *psmouse)
224 {
225 	int	i;
226 
227 	psmouse_printk(KERN_DEBUG, psmouse, "PS/2 packet [");
228 	for (i = 0; i < psmouse->pktsize; i++)
229 		printk("%s0x%02x ", i ? ", " : " ", psmouse->packet[i]);
230 	printk("]\n");
231 }
232 
233 /*
234  * Interpret complete data packets and report absolute mode input events for
235  * hardware version 1. (4 byte packets)
236  */
237 static void elantech_report_absolute_v1(struct psmouse *psmouse)
238 {
239 	struct input_dev *dev = psmouse->dev;
240 	struct elantech_data *etd = psmouse->private;
241 	unsigned char *packet = psmouse->packet;
242 	int fingers;
243 
244 	if (etd->fw_version < 0x020000) {
245 		/*
246 		 * byte 0:  D   U  p1  p2   1  p3   R   L
247 		 * byte 1:  f   0  th  tw  x9  x8  y9  y8
248 		 */
249 		fingers = ((packet[1] & 0x80) >> 7) +
250 				((packet[1] & 0x30) >> 4);
251 	} else {
252 		/*
253 		 * byte 0: n1  n0  p2  p1   1  p3   R   L
254 		 * byte 1:  0   0   0   0  x9  x8  y9  y8
255 		 */
256 		fingers = (packet[0] & 0xc0) >> 6;
257 	}
258 
259 	if (etd->jumpy_cursor) {
260 		if (fingers != 1) {
261 			etd->single_finger_reports = 0;
262 		} else if (etd->single_finger_reports < 2) {
263 			/* Discard first 2 reports of one finger, bogus */
264 			etd->single_finger_reports++;
265 			elantech_debug("discarding packet\n");
266 			return;
267 		}
268 	}
269 
270 	input_report_key(dev, BTN_TOUCH, fingers != 0);
271 
272 	/*
273 	 * byte 2: x7  x6  x5  x4  x3  x2  x1  x0
274 	 * byte 3: y7  y6  y5  y4  y3  y2  y1  y0
275 	 */
276 	if (fingers) {
277 		input_report_abs(dev, ABS_X,
278 			((packet[1] & 0x0c) << 6) | packet[2]);
279 		input_report_abs(dev, ABS_Y,
280 			etd->y_max - (((packet[1] & 0x03) << 8) | packet[3]));
281 	}
282 
283 	input_report_key(dev, BTN_TOOL_FINGER, fingers == 1);
284 	input_report_key(dev, BTN_TOOL_DOUBLETAP, fingers == 2);
285 	input_report_key(dev, BTN_TOOL_TRIPLETAP, fingers == 3);
286 	input_report_key(dev, BTN_LEFT, packet[0] & 0x01);
287 	input_report_key(dev, BTN_RIGHT, packet[0] & 0x02);
288 
289 	if (etd->fw_version < 0x020000 &&
290 	    (etd->capabilities[0] & ETP_CAP_HAS_ROCKER)) {
291 		/* rocker up */
292 		input_report_key(dev, BTN_FORWARD, packet[0] & 0x40);
293 		/* rocker down */
294 		input_report_key(dev, BTN_BACK, packet[0] & 0x80);
295 	}
296 
297 	input_sync(dev);
298 }
299 
300 static void elantech_set_slot(struct input_dev *dev, int slot, bool active,
301 			      unsigned int x, unsigned int y)
302 {
303 	input_mt_slot(dev, slot);
304 	input_mt_report_slot_state(dev, MT_TOOL_FINGER, active);
305 	if (active) {
306 		input_report_abs(dev, ABS_MT_POSITION_X, x);
307 		input_report_abs(dev, ABS_MT_POSITION_Y, y);
308 	}
309 }
310 
311 /* x1 < x2 and y1 < y2 when two fingers, x = y = 0 when not pressed */
312 static void elantech_report_semi_mt_data(struct input_dev *dev,
313 					 unsigned int num_fingers,
314 					 unsigned int x1, unsigned int y1,
315 					 unsigned int x2, unsigned int y2)
316 {
317 	elantech_set_slot(dev, 0, num_fingers != 0, x1, y1);
318 	elantech_set_slot(dev, 1, num_fingers >= 2, x2, y2);
319 }
320 
321 /*
322  * Interpret complete data packets and report absolute mode input events for
323  * hardware version 2. (6 byte packets)
324  */
325 static void elantech_report_absolute_v2(struct psmouse *psmouse)
326 {
327 	struct elantech_data *etd = psmouse->private;
328 	struct input_dev *dev = psmouse->dev;
329 	unsigned char *packet = psmouse->packet;
330 	unsigned int fingers, x1 = 0, y1 = 0, x2 = 0, y2 = 0;
331 	unsigned int width = 0, pres = 0;
332 
333 	/* byte 0: n1  n0   .   .   .   .   R   L */
334 	fingers = (packet[0] & 0xc0) >> 6;
335 
336 	switch (fingers) {
337 	case 3:
338 		/*
339 		 * Same as one finger, except report of more than 3 fingers:
340 		 * byte 3:  n4  .   w1  w0   .   .   .   .
341 		 */
342 		if (packet[3] & 0x80)
343 			fingers = 4;
344 		/* pass through... */
345 	case 1:
346 		/*
347 		 * byte 1:  .   .   .   .  x11 x10 x9  x8
348 		 * byte 2: x7  x6  x5  x4  x4  x2  x1  x0
349 		 */
350 		x1 = ((packet[1] & 0x0f) << 8) | packet[2];
351 		/*
352 		 * byte 4:  .   .   .   .  y11 y10 y9  y8
353 		 * byte 5: y7  y6  y5  y4  y3  y2  y1  y0
354 		 */
355 		y1 = etd->y_max - (((packet[4] & 0x0f) << 8) | packet[5]);
356 
357 		pres = (packet[1] & 0xf0) | ((packet[4] & 0xf0) >> 4);
358 		width = ((packet[0] & 0x30) >> 2) | ((packet[3] & 0x30) >> 4);
359 		break;
360 
361 	case 2:
362 		/*
363 		 * The coordinate of each finger is reported separately
364 		 * with a lower resolution for two finger touches:
365 		 * byte 0:  .   .  ay8 ax8  .   .   .   .
366 		 * byte 1: ax7 ax6 ax5 ax4 ax3 ax2 ax1 ax0
367 		 */
368 		x1 = (((packet[0] & 0x10) << 4) | packet[1]) << 2;
369 		/* byte 2: ay7 ay6 ay5 ay4 ay3 ay2 ay1 ay0 */
370 		y1 = etd->y_max -
371 			((((packet[0] & 0x20) << 3) | packet[2]) << 2);
372 		/*
373 		 * byte 3:  .   .  by8 bx8  .   .   .   .
374 		 * byte 4: bx7 bx6 bx5 bx4 bx3 bx2 bx1 bx0
375 		 */
376 		x2 = (((packet[3] & 0x10) << 4) | packet[4]) << 2;
377 		/* byte 5: by7 by8 by5 by4 by3 by2 by1 by0 */
378 		y2 = etd->y_max -
379 			((((packet[3] & 0x20) << 3) | packet[5]) << 2);
380 
381 		/* Unknown so just report sensible values */
382 		pres = 127;
383 		width = 7;
384 		break;
385 	}
386 
387 	input_report_key(dev, BTN_TOUCH, fingers != 0);
388 	if (fingers != 0) {
389 		input_report_abs(dev, ABS_X, x1);
390 		input_report_abs(dev, ABS_Y, y1);
391 	}
392 	elantech_report_semi_mt_data(dev, fingers, x1, y1, x2, y2);
393 	input_report_key(dev, BTN_TOOL_FINGER, fingers == 1);
394 	input_report_key(dev, BTN_TOOL_DOUBLETAP, fingers == 2);
395 	input_report_key(dev, BTN_TOOL_TRIPLETAP, fingers == 3);
396 	input_report_key(dev, BTN_TOOL_QUADTAP, fingers == 4);
397 	input_report_key(dev, BTN_LEFT, packet[0] & 0x01);
398 	input_report_key(dev, BTN_RIGHT, packet[0] & 0x02);
399 	if (etd->reports_pressure) {
400 		input_report_abs(dev, ABS_PRESSURE, pres);
401 		input_report_abs(dev, ABS_TOOL_WIDTH, width);
402 	}
403 
404 	input_sync(dev);
405 }
406 
407 static void elantech_report_trackpoint(struct psmouse *psmouse,
408 				       int packet_type)
409 {
410 	/*
411 	 * byte 0:  0   0  sx  sy   0   M   R   L
412 	 * byte 1:~sx   0   0   0   0   0   0   0
413 	 * byte 2:~sy   0   0   0   0   0   0   0
414 	 * byte 3:  0   0 ~sy ~sx   0   1   1   0
415 	 * byte 4: x7  x6  x5  x4  x3  x2  x1  x0
416 	 * byte 5: y7  y6  y5  y4  y3  y2  y1  y0
417 	 *
418 	 * x and y are written in two's complement spread
419 	 * over 9 bits with sx/sy the relative top bit and
420 	 * x7..x0 and y7..y0 the lower bits.
421 	 * The sign of y is opposite to what the input driver
422 	 * expects for a relative movement
423 	 */
424 
425 	struct elantech_data *etd = psmouse->private;
426 	struct input_dev *tp_dev = etd->tp_dev;
427 	unsigned char *packet = psmouse->packet;
428 	int x, y;
429 	u32 t;
430 
431 	t = get_unaligned_le32(&packet[0]);
432 
433 	switch (t & ~7U) {
434 	case 0x06000030U:
435 	case 0x16008020U:
436 	case 0x26800010U:
437 	case 0x36808000U:
438 		x = packet[4] - (int)((packet[1]^0x80) << 1);
439 		y = (int)((packet[2]^0x80) << 1) - packet[5];
440 
441 		input_report_key(tp_dev, BTN_LEFT, packet[0] & 0x01);
442 		input_report_key(tp_dev, BTN_RIGHT, packet[0] & 0x02);
443 		input_report_key(tp_dev, BTN_MIDDLE, packet[0] & 0x04);
444 
445 		input_report_rel(tp_dev, REL_X, x);
446 		input_report_rel(tp_dev, REL_Y, y);
447 
448 		input_sync(tp_dev);
449 
450 		break;
451 
452 	default:
453 		/* Dump unexpected packet sequences if debug=1 (default) */
454 		if (etd->debug == 1)
455 			elantech_packet_dump(psmouse);
456 
457 		break;
458 	}
459 }
460 
461 /*
462  * Interpret complete data packets and report absolute mode input events for
463  * hardware version 3. (12 byte packets for two fingers)
464  */
465 static void elantech_report_absolute_v3(struct psmouse *psmouse,
466 					int packet_type)
467 {
468 	struct input_dev *dev = psmouse->dev;
469 	struct elantech_data *etd = psmouse->private;
470 	unsigned char *packet = psmouse->packet;
471 	unsigned int fingers = 0, x1 = 0, y1 = 0, x2 = 0, y2 = 0;
472 	unsigned int width = 0, pres = 0;
473 
474 	/* byte 0: n1  n0   .   .   .   .   R   L */
475 	fingers = (packet[0] & 0xc0) >> 6;
476 
477 	switch (fingers) {
478 	case 3:
479 	case 1:
480 		/*
481 		 * byte 1:  .   .   .   .  x11 x10 x9  x8
482 		 * byte 2: x7  x6  x5  x4  x4  x2  x1  x0
483 		 */
484 		x1 = ((packet[1] & 0x0f) << 8) | packet[2];
485 		/*
486 		 * byte 4:  .   .   .   .  y11 y10 y9  y8
487 		 * byte 5: y7  y6  y5  y4  y3  y2  y1  y0
488 		 */
489 		y1 = etd->y_max - (((packet[4] & 0x0f) << 8) | packet[5]);
490 		break;
491 
492 	case 2:
493 		if (packet_type == PACKET_V3_HEAD) {
494 			/*
495 			 * byte 1:   .    .    .    .  ax11 ax10 ax9  ax8
496 			 * byte 2: ax7  ax6  ax5  ax4  ax3  ax2  ax1  ax0
497 			 */
498 			etd->mt[0].x = ((packet[1] & 0x0f) << 8) | packet[2];
499 			/*
500 			 * byte 4:   .    .    .    .  ay11 ay10 ay9  ay8
501 			 * byte 5: ay7  ay6  ay5  ay4  ay3  ay2  ay1  ay0
502 			 */
503 			etd->mt[0].y = etd->y_max -
504 				(((packet[4] & 0x0f) << 8) | packet[5]);
505 			/*
506 			 * wait for next packet
507 			 */
508 			return;
509 		}
510 
511 		/* packet_type == PACKET_V3_TAIL */
512 		x1 = etd->mt[0].x;
513 		y1 = etd->mt[0].y;
514 		x2 = ((packet[1] & 0x0f) << 8) | packet[2];
515 		y2 = etd->y_max - (((packet[4] & 0x0f) << 8) | packet[5]);
516 		break;
517 	}
518 
519 	pres = (packet[1] & 0xf0) | ((packet[4] & 0xf0) >> 4);
520 	width = ((packet[0] & 0x30) >> 2) | ((packet[3] & 0x30) >> 4);
521 
522 	input_report_key(dev, BTN_TOUCH, fingers != 0);
523 	if (fingers != 0) {
524 		input_report_abs(dev, ABS_X, x1);
525 		input_report_abs(dev, ABS_Y, y1);
526 	}
527 	elantech_report_semi_mt_data(dev, fingers, x1, y1, x2, y2);
528 	input_report_key(dev, BTN_TOOL_FINGER, fingers == 1);
529 	input_report_key(dev, BTN_TOOL_DOUBLETAP, fingers == 2);
530 	input_report_key(dev, BTN_TOOL_TRIPLETAP, fingers == 3);
531 
532 	/* For clickpads map both buttons to BTN_LEFT */
533 	if (etd->fw_version & 0x001000) {
534 		input_report_key(dev, BTN_LEFT, packet[0] & 0x03);
535 	} else {
536 		input_report_key(dev, BTN_LEFT, packet[0] & 0x01);
537 		input_report_key(dev, BTN_RIGHT, packet[0] & 0x02);
538 	}
539 
540 	input_report_abs(dev, ABS_PRESSURE, pres);
541 	input_report_abs(dev, ABS_TOOL_WIDTH, width);
542 
543 	input_sync(dev);
544 }
545 
546 static void elantech_input_sync_v4(struct psmouse *psmouse)
547 {
548 	struct input_dev *dev = psmouse->dev;
549 	struct elantech_data *etd = psmouse->private;
550 	unsigned char *packet = psmouse->packet;
551 
552 	/* For clickpads map both buttons to BTN_LEFT */
553 	if (etd->fw_version & 0x001000) {
554 		input_report_key(dev, BTN_LEFT, packet[0] & 0x03);
555 	} else {
556 		input_report_key(dev, BTN_LEFT, packet[0] & 0x01);
557 		input_report_key(dev, BTN_RIGHT, packet[0] & 0x02);
558 		input_report_key(dev, BTN_MIDDLE, packet[0] & 0x04);
559 	}
560 
561 	input_mt_report_pointer_emulation(dev, true);
562 	input_sync(dev);
563 }
564 
565 static void process_packet_status_v4(struct psmouse *psmouse)
566 {
567 	struct input_dev *dev = psmouse->dev;
568 	unsigned char *packet = psmouse->packet;
569 	unsigned fingers;
570 	int i;
571 
572 	/* notify finger state change */
573 	fingers = packet[1] & 0x1f;
574 	for (i = 0; i < ETP_MAX_FINGERS; i++) {
575 		if ((fingers & (1 << i)) == 0) {
576 			input_mt_slot(dev, i);
577 			input_mt_report_slot_state(dev, MT_TOOL_FINGER, false);
578 		}
579 	}
580 
581 	elantech_input_sync_v4(psmouse);
582 }
583 
584 static void process_packet_head_v4(struct psmouse *psmouse)
585 {
586 	struct input_dev *dev = psmouse->dev;
587 	struct elantech_data *etd = psmouse->private;
588 	unsigned char *packet = psmouse->packet;
589 	int id = ((packet[3] & 0xe0) >> 5) - 1;
590 	int pres, traces;
591 
592 	if (id < 0)
593 		return;
594 
595 	etd->mt[id].x = ((packet[1] & 0x0f) << 8) | packet[2];
596 	etd->mt[id].y = etd->y_max - (((packet[4] & 0x0f) << 8) | packet[5]);
597 	pres = (packet[1] & 0xf0) | ((packet[4] & 0xf0) >> 4);
598 	traces = (packet[0] & 0xf0) >> 4;
599 
600 	input_mt_slot(dev, id);
601 	input_mt_report_slot_state(dev, MT_TOOL_FINGER, true);
602 
603 	input_report_abs(dev, ABS_MT_POSITION_X, etd->mt[id].x);
604 	input_report_abs(dev, ABS_MT_POSITION_Y, etd->mt[id].y);
605 	input_report_abs(dev, ABS_MT_PRESSURE, pres);
606 	input_report_abs(dev, ABS_MT_TOUCH_MAJOR, traces * etd->width);
607 	/* report this for backwards compatibility */
608 	input_report_abs(dev, ABS_TOOL_WIDTH, traces);
609 
610 	elantech_input_sync_v4(psmouse);
611 }
612 
613 static void process_packet_motion_v4(struct psmouse *psmouse)
614 {
615 	struct input_dev *dev = psmouse->dev;
616 	struct elantech_data *etd = psmouse->private;
617 	unsigned char *packet = psmouse->packet;
618 	int weight, delta_x1 = 0, delta_y1 = 0, delta_x2 = 0, delta_y2 = 0;
619 	int id, sid;
620 
621 	id = ((packet[0] & 0xe0) >> 5) - 1;
622 	if (id < 0)
623 		return;
624 
625 	sid = ((packet[3] & 0xe0) >> 5) - 1;
626 	weight = (packet[0] & 0x10) ? ETP_WEIGHT_VALUE : 1;
627 	/*
628 	 * Motion packets give us the delta of x, y values of specific fingers,
629 	 * but in two's complement. Let the compiler do the conversion for us.
630 	 * Also _enlarge_ the numbers to int, in case of overflow.
631 	 */
632 	delta_x1 = (signed char)packet[1];
633 	delta_y1 = (signed char)packet[2];
634 	delta_x2 = (signed char)packet[4];
635 	delta_y2 = (signed char)packet[5];
636 
637 	etd->mt[id].x += delta_x1 * weight;
638 	etd->mt[id].y -= delta_y1 * weight;
639 	input_mt_slot(dev, id);
640 	input_report_abs(dev, ABS_MT_POSITION_X, etd->mt[id].x);
641 	input_report_abs(dev, ABS_MT_POSITION_Y, etd->mt[id].y);
642 
643 	if (sid >= 0) {
644 		etd->mt[sid].x += delta_x2 * weight;
645 		etd->mt[sid].y -= delta_y2 * weight;
646 		input_mt_slot(dev, sid);
647 		input_report_abs(dev, ABS_MT_POSITION_X, etd->mt[sid].x);
648 		input_report_abs(dev, ABS_MT_POSITION_Y, etd->mt[sid].y);
649 	}
650 
651 	elantech_input_sync_v4(psmouse);
652 }
653 
654 static void elantech_report_absolute_v4(struct psmouse *psmouse,
655 					int packet_type)
656 {
657 	switch (packet_type) {
658 	case PACKET_V4_STATUS:
659 		process_packet_status_v4(psmouse);
660 		break;
661 
662 	case PACKET_V4_HEAD:
663 		process_packet_head_v4(psmouse);
664 		break;
665 
666 	case PACKET_V4_MOTION:
667 		process_packet_motion_v4(psmouse);
668 		break;
669 
670 	case PACKET_UNKNOWN:
671 	default:
672 		/* impossible to get here */
673 		break;
674 	}
675 }
676 
677 static int elantech_packet_check_v1(struct psmouse *psmouse)
678 {
679 	struct elantech_data *etd = psmouse->private;
680 	unsigned char *packet = psmouse->packet;
681 	unsigned char p1, p2, p3;
682 
683 	/* Parity bits are placed differently */
684 	if (etd->fw_version < 0x020000) {
685 		/* byte 0:  D   U  p1  p2   1  p3   R   L */
686 		p1 = (packet[0] & 0x20) >> 5;
687 		p2 = (packet[0] & 0x10) >> 4;
688 	} else {
689 		/* byte 0: n1  n0  p2  p1   1  p3   R   L */
690 		p1 = (packet[0] & 0x10) >> 4;
691 		p2 = (packet[0] & 0x20) >> 5;
692 	}
693 
694 	p3 = (packet[0] & 0x04) >> 2;
695 
696 	return etd->parity[packet[1]] == p1 &&
697 	       etd->parity[packet[2]] == p2 &&
698 	       etd->parity[packet[3]] == p3;
699 }
700 
701 static int elantech_debounce_check_v2(struct psmouse *psmouse)
702 {
703         /*
704          * When we encounter packet that matches this exactly, it means the
705          * hardware is in debounce status. Just ignore the whole packet.
706          */
707         const u8 debounce_packet[] = { 0x84, 0xff, 0xff, 0x02, 0xff, 0xff };
708         unsigned char *packet = psmouse->packet;
709 
710         return !memcmp(packet, debounce_packet, sizeof(debounce_packet));
711 }
712 
713 static int elantech_packet_check_v2(struct psmouse *psmouse)
714 {
715 	struct elantech_data *etd = psmouse->private;
716 	unsigned char *packet = psmouse->packet;
717 
718 	/*
719 	 * V2 hardware has two flavors. Older ones that do not report pressure,
720 	 * and newer ones that reports pressure and width. With newer ones, all
721 	 * packets (1, 2, 3 finger touch) have the same constant bits. With
722 	 * older ones, 1/3 finger touch packets and 2 finger touch packets
723 	 * have different constant bits.
724 	 * With all three cases, if the constant bits are not exactly what I
725 	 * expected, I consider them invalid.
726 	 */
727 	if (etd->reports_pressure)
728 		return (packet[0] & 0x0c) == 0x04 &&
729 		       (packet[3] & 0x0f) == 0x02;
730 
731 	if ((packet[0] & 0xc0) == 0x80)
732 		return (packet[0] & 0x0c) == 0x0c &&
733 		       (packet[3] & 0x0e) == 0x08;
734 
735 	return (packet[0] & 0x3c) == 0x3c &&
736 	       (packet[1] & 0xf0) == 0x00 &&
737 	       (packet[3] & 0x3e) == 0x38 &&
738 	       (packet[4] & 0xf0) == 0x00;
739 }
740 
741 /*
742  * We check the constant bits to determine what packet type we get,
743  * so packet checking is mandatory for v3 and later hardware.
744  */
745 static int elantech_packet_check_v3(struct psmouse *psmouse)
746 {
747 	struct elantech_data *etd = psmouse->private;
748 	const u8 debounce_packet[] = { 0xc4, 0xff, 0xff, 0x02, 0xff, 0xff };
749 	unsigned char *packet = psmouse->packet;
750 
751 	/*
752 	 * check debounce first, it has the same signature in byte 0
753 	 * and byte 3 as PACKET_V3_HEAD.
754 	 */
755 	if (!memcmp(packet, debounce_packet, sizeof(debounce_packet)))
756 		return PACKET_DEBOUNCE;
757 
758 	/*
759 	 * If the hardware flag 'crc_enabled' is set the packets have
760 	 * different signatures.
761 	 */
762 	if (etd->crc_enabled) {
763 		if ((packet[3] & 0x09) == 0x08)
764 			return PACKET_V3_HEAD;
765 
766 		if ((packet[3] & 0x09) == 0x09)
767 			return PACKET_V3_TAIL;
768 	} else {
769 		if ((packet[0] & 0x0c) == 0x04 && (packet[3] & 0xcf) == 0x02)
770 			return PACKET_V3_HEAD;
771 
772 		if ((packet[0] & 0x0c) == 0x0c && (packet[3] & 0xce) == 0x0c)
773 			return PACKET_V3_TAIL;
774 		if ((packet[3] & 0x0f) == 0x06)
775 			return PACKET_TRACKPOINT;
776 	}
777 
778 	return PACKET_UNKNOWN;
779 }
780 
781 static int elantech_packet_check_v4(struct psmouse *psmouse)
782 {
783 	struct elantech_data *etd = psmouse->private;
784 	unsigned char *packet = psmouse->packet;
785 	unsigned char packet_type = packet[3] & 0x03;
786 	unsigned int ic_version;
787 	bool sanity_check;
788 
789 	if (etd->tp_dev && (packet[3] & 0x0f) == 0x06)
790 		return PACKET_TRACKPOINT;
791 
792 	/* This represents the version of IC body. */
793 	ic_version = (etd->fw_version & 0x0f0000) >> 16;
794 
795 	/*
796 	 * Sanity check based on the constant bits of a packet.
797 	 * The constant bits change depending on the value of
798 	 * the hardware flag 'crc_enabled' and the version of
799 	 * the IC body, but are the same for every packet,
800 	 * regardless of the type.
801 	 */
802 	if (etd->crc_enabled)
803 		sanity_check = ((packet[3] & 0x08) == 0x00);
804 	else if (ic_version == 7 && etd->samples[1] == 0x2A)
805 		sanity_check = ((packet[3] & 0x1c) == 0x10);
806 	else
807 		sanity_check = ((packet[0] & 0x0c) == 0x04 &&
808 				(packet[3] & 0x1c) == 0x10);
809 
810 	if (!sanity_check)
811 		return PACKET_UNKNOWN;
812 
813 	switch (packet_type) {
814 	case 0:
815 		return PACKET_V4_STATUS;
816 
817 	case 1:
818 		return PACKET_V4_HEAD;
819 
820 	case 2:
821 		return PACKET_V4_MOTION;
822 	}
823 
824 	return PACKET_UNKNOWN;
825 }
826 
827 /*
828  * Process byte stream from mouse and handle complete packets
829  */
830 static psmouse_ret_t elantech_process_byte(struct psmouse *psmouse)
831 {
832 	struct elantech_data *etd = psmouse->private;
833 	int packet_type;
834 
835 	if (psmouse->pktcnt < psmouse->pktsize)
836 		return PSMOUSE_GOOD_DATA;
837 
838 	if (etd->debug > 1)
839 		elantech_packet_dump(psmouse);
840 
841 	switch (etd->hw_version) {
842 	case 1:
843 		if (etd->paritycheck && !elantech_packet_check_v1(psmouse))
844 			return PSMOUSE_BAD_DATA;
845 
846 		elantech_report_absolute_v1(psmouse);
847 		break;
848 
849 	case 2:
850 		/* ignore debounce */
851 		if (elantech_debounce_check_v2(psmouse))
852 			return PSMOUSE_FULL_PACKET;
853 
854 		if (etd->paritycheck && !elantech_packet_check_v2(psmouse))
855 			return PSMOUSE_BAD_DATA;
856 
857 		elantech_report_absolute_v2(psmouse);
858 		break;
859 
860 	case 3:
861 		packet_type = elantech_packet_check_v3(psmouse);
862 		switch (packet_type) {
863 		case PACKET_UNKNOWN:
864 			return PSMOUSE_BAD_DATA;
865 
866 		case PACKET_DEBOUNCE:
867 			/* ignore debounce */
868 			break;
869 
870 		case PACKET_TRACKPOINT:
871 			elantech_report_trackpoint(psmouse, packet_type);
872 			break;
873 
874 		default:
875 			elantech_report_absolute_v3(psmouse, packet_type);
876 			break;
877 		}
878 
879 		break;
880 
881 	case 4:
882 		packet_type = elantech_packet_check_v4(psmouse);
883 		switch (packet_type) {
884 		case PACKET_UNKNOWN:
885 			return PSMOUSE_BAD_DATA;
886 
887 		case PACKET_TRACKPOINT:
888 			elantech_report_trackpoint(psmouse, packet_type);
889 			break;
890 
891 		default:
892 			elantech_report_absolute_v4(psmouse, packet_type);
893 			break;
894 		}
895 
896 		break;
897 	}
898 
899 	return PSMOUSE_FULL_PACKET;
900 }
901 
902 /*
903  * This writes the reg_07 value again to the hardware at the end of every
904  * set_rate call because the register loses its value. reg_07 allows setting
905  * absolute mode on v4 hardware
906  */
907 static void elantech_set_rate_restore_reg_07(struct psmouse *psmouse,
908 		unsigned int rate)
909 {
910 	struct elantech_data *etd = psmouse->private;
911 
912 	etd->original_set_rate(psmouse, rate);
913 	if (elantech_write_reg(psmouse, 0x07, etd->reg_07))
914 		psmouse_err(psmouse, "restoring reg_07 failed\n");
915 }
916 
917 /*
918  * Put the touchpad into absolute mode
919  */
920 static int elantech_set_absolute_mode(struct psmouse *psmouse)
921 {
922 	struct elantech_data *etd = psmouse->private;
923 	unsigned char val;
924 	int tries = ETP_READ_BACK_TRIES;
925 	int rc = 0;
926 
927 	switch (etd->hw_version) {
928 	case 1:
929 		etd->reg_10 = 0x16;
930 		etd->reg_11 = 0x8f;
931 		if (elantech_write_reg(psmouse, 0x10, etd->reg_10) ||
932 		    elantech_write_reg(psmouse, 0x11, etd->reg_11)) {
933 			rc = -1;
934 		}
935 		break;
936 
937 	case 2:
938 					/* Windows driver values */
939 		etd->reg_10 = 0x54;
940 		etd->reg_11 = 0x88;	/* 0x8a */
941 		etd->reg_21 = 0x60;	/* 0x00 */
942 		if (elantech_write_reg(psmouse, 0x10, etd->reg_10) ||
943 		    elantech_write_reg(psmouse, 0x11, etd->reg_11) ||
944 		    elantech_write_reg(psmouse, 0x21, etd->reg_21)) {
945 			rc = -1;
946 		}
947 		break;
948 
949 	case 3:
950 		if (etd->set_hw_resolution)
951 			etd->reg_10 = 0x0b;
952 		else
953 			etd->reg_10 = 0x01;
954 
955 		if (elantech_write_reg(psmouse, 0x10, etd->reg_10))
956 			rc = -1;
957 
958 		break;
959 
960 	case 4:
961 		etd->reg_07 = 0x01;
962 		if (elantech_write_reg(psmouse, 0x07, etd->reg_07))
963 			rc = -1;
964 
965 		goto skip_readback_reg_10; /* v4 has no reg 0x10 to read */
966 	}
967 
968 	if (rc == 0) {
969 		/*
970 		 * Read back reg 0x10. For hardware version 1 we must make
971 		 * sure the absolute mode bit is set. For hardware version 2
972 		 * the touchpad is probably initializing and not ready until
973 		 * we read back the value we just wrote.
974 		 */
975 		do {
976 			rc = elantech_read_reg(psmouse, 0x10, &val);
977 			if (rc == 0)
978 				break;
979 			tries--;
980 			elantech_debug("retrying read (%d).\n", tries);
981 			msleep(ETP_READ_BACK_DELAY);
982 		} while (tries > 0);
983 
984 		if (rc) {
985 			psmouse_err(psmouse,
986 				    "failed to read back register 0x10.\n");
987 		} else if (etd->hw_version == 1 &&
988 			   !(val & ETP_R10_ABSOLUTE_MODE)) {
989 			psmouse_err(psmouse,
990 				    "touchpad refuses to switch to absolute mode.\n");
991 			rc = -1;
992 		}
993 	}
994 
995  skip_readback_reg_10:
996 	if (rc)
997 		psmouse_err(psmouse, "failed to initialise registers.\n");
998 
999 	return rc;
1000 }
1001 
1002 static int elantech_set_range(struct psmouse *psmouse,
1003 			      unsigned int *x_min, unsigned int *y_min,
1004 			      unsigned int *x_max, unsigned int *y_max,
1005 			      unsigned int *width)
1006 {
1007 	struct elantech_data *etd = psmouse->private;
1008 	unsigned char param[3];
1009 	unsigned char traces;
1010 
1011 	switch (etd->hw_version) {
1012 	case 1:
1013 		*x_min = ETP_XMIN_V1;
1014 		*y_min = ETP_YMIN_V1;
1015 		*x_max = ETP_XMAX_V1;
1016 		*y_max = ETP_YMAX_V1;
1017 		break;
1018 
1019 	case 2:
1020 		if (etd->fw_version == 0x020800 ||
1021 		    etd->fw_version == 0x020b00 ||
1022 		    etd->fw_version == 0x020030) {
1023 			*x_min = ETP_XMIN_V2;
1024 			*y_min = ETP_YMIN_V2;
1025 			*x_max = ETP_XMAX_V2;
1026 			*y_max = ETP_YMAX_V2;
1027 		} else {
1028 			int i;
1029 			int fixed_dpi;
1030 
1031 			i = (etd->fw_version > 0x020800 &&
1032 			     etd->fw_version < 0x020900) ? 1 : 2;
1033 
1034 			if (etd->send_cmd(psmouse, ETP_FW_ID_QUERY, param))
1035 				return -1;
1036 
1037 			fixed_dpi = param[1] & 0x10;
1038 
1039 			if (((etd->fw_version >> 16) == 0x14) && fixed_dpi) {
1040 				if (etd->send_cmd(psmouse, ETP_SAMPLE_QUERY, param))
1041 					return -1;
1042 
1043 				*x_max = (etd->capabilities[1] - i) * param[1] / 2;
1044 				*y_max = (etd->capabilities[2] - i) * param[2] / 2;
1045 			} else if (etd->fw_version == 0x040216) {
1046 				*x_max = 819;
1047 				*y_max = 405;
1048 			} else if (etd->fw_version == 0x040219 || etd->fw_version == 0x040215) {
1049 				*x_max = 900;
1050 				*y_max = 500;
1051 			} else {
1052 				*x_max = (etd->capabilities[1] - i) * 64;
1053 				*y_max = (etd->capabilities[2] - i) * 64;
1054 			}
1055 		}
1056 		break;
1057 
1058 	case 3:
1059 		if (etd->send_cmd(psmouse, ETP_FW_ID_QUERY, param))
1060 			return -1;
1061 
1062 		*x_max = (0x0f & param[0]) << 8 | param[1];
1063 		*y_max = (0xf0 & param[0]) << 4 | param[2];
1064 		break;
1065 
1066 	case 4:
1067 		if (etd->send_cmd(psmouse, ETP_FW_ID_QUERY, param))
1068 			return -1;
1069 
1070 		*x_max = (0x0f & param[0]) << 8 | param[1];
1071 		*y_max = (0xf0 & param[0]) << 4 | param[2];
1072 		traces = etd->capabilities[1];
1073 		if ((traces < 2) || (traces > *x_max))
1074 			return -1;
1075 
1076 		*width = *x_max / (traces - 1);
1077 		break;
1078 	}
1079 
1080 	return 0;
1081 }
1082 
1083 /*
1084  * (value from firmware) * 10 + 790 = dpi
1085  * we also have to convert dpi to dots/mm (*10/254 to avoid floating point)
1086  */
1087 static unsigned int elantech_convert_res(unsigned int val)
1088 {
1089 	return (val * 10 + 790) * 10 / 254;
1090 }
1091 
1092 static int elantech_get_resolution_v4(struct psmouse *psmouse,
1093 				      unsigned int *x_res,
1094 				      unsigned int *y_res)
1095 {
1096 	unsigned char param[3];
1097 
1098 	if (elantech_send_cmd(psmouse, ETP_RESOLUTION_QUERY, param))
1099 		return -1;
1100 
1101 	*x_res = elantech_convert_res(param[1] & 0x0f);
1102 	*y_res = elantech_convert_res((param[1] & 0xf0) >> 4);
1103 
1104 	return 0;
1105 }
1106 
1107 /*
1108  * Advertise INPUT_PROP_BUTTONPAD for clickpads. The testing of bit 12 in
1109  * fw_version for this is based on the following fw_version & caps table:
1110  *
1111  * Laptop-model:           fw_version:     caps:           buttons:
1112  * Acer S3                 0x461f00        10, 13, 0e      clickpad
1113  * Acer S7-392             0x581f01        50, 17, 0d      clickpad
1114  * Acer V5-131             0x461f02        01, 16, 0c      clickpad
1115  * Acer V5-551             0x461f00        ?               clickpad
1116  * Asus K53SV              0x450f01        78, 15, 0c      2 hw buttons
1117  * Asus G46VW              0x460f02        00, 18, 0c      2 hw buttons
1118  * Asus G750JX             0x360f00        00, 16, 0c      2 hw buttons
1119  * Asus TP500LN            0x381f17        10, 14, 0e      clickpad
1120  * Asus X750JN             0x381f17        10, 14, 0e      clickpad
1121  * Asus UX31               0x361f00        20, 15, 0e      clickpad
1122  * Asus UX32VD             0x361f02        00, 15, 0e      clickpad
1123  * Avatar AVIU-145A2       0x361f00        ?               clickpad
1124  * Fujitsu LIFEBOOK E544   0x470f00        d0, 12, 09      2 hw buttons
1125  * Fujitsu LIFEBOOK E554   0x570f01        40, 14, 0c      2 hw buttons
1126  * Fujitsu T725            0x470f01        05, 12, 09      2 hw buttons
1127  * Fujitsu H730            0x570f00        c0, 14, 0c      3 hw buttons (**)
1128  * Gigabyte U2442          0x450f01        58, 17, 0c      2 hw buttons
1129  * Lenovo L430             0x350f02        b9, 15, 0c      2 hw buttons (*)
1130  * Lenovo L530             0x350f02        b9, 15, 0c      2 hw buttons (*)
1131  * Samsung NF210           0x150b00        78, 14, 0a      2 hw buttons
1132  * Samsung NP770Z5E        0x575f01        10, 15, 0f      clickpad
1133  * Samsung NP700Z5B        0x361f06        21, 15, 0f      clickpad
1134  * Samsung NP900X3E-A02    0x575f03        ?               clickpad
1135  * Samsung NP-QX410        0x851b00        19, 14, 0c      clickpad
1136  * Samsung RC512           0x450f00        08, 15, 0c      2 hw buttons
1137  * Samsung RF710           0x450f00        ?               2 hw buttons
1138  * System76 Pangolin       0x250f01        ?               2 hw buttons
1139  * (*) + 3 trackpoint buttons
1140  * (**) + 0 trackpoint buttons
1141  * Note: Lenovo L430 and Lenovo L430 have the same fw_version/caps
1142  */
1143 static void elantech_set_buttonpad_prop(struct psmouse *psmouse)
1144 {
1145 	struct input_dev *dev = psmouse->dev;
1146 	struct elantech_data *etd = psmouse->private;
1147 
1148 	if (etd->fw_version & 0x001000) {
1149 		__set_bit(INPUT_PROP_BUTTONPAD, dev->propbit);
1150 		__clear_bit(BTN_RIGHT, dev->keybit);
1151 	}
1152 }
1153 
1154 /*
1155  * Some hw_version 4 models do have a middle button
1156  */
1157 static const struct dmi_system_id elantech_dmi_has_middle_button[] = {
1158 #if defined(CONFIG_DMI) && defined(CONFIG_X86)
1159 	{
1160 		/* Fujitsu H730 has a middle button */
1161 		.matches = {
1162 			DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU"),
1163 			DMI_MATCH(DMI_PRODUCT_NAME, "CELSIUS H730"),
1164 		},
1165 	},
1166 #endif
1167 	{ }
1168 };
1169 
1170 /*
1171  * Set the appropriate event bits for the input subsystem
1172  */
1173 static int elantech_set_input_params(struct psmouse *psmouse)
1174 {
1175 	struct input_dev *dev = psmouse->dev;
1176 	struct elantech_data *etd = psmouse->private;
1177 	unsigned int x_min = 0, y_min = 0, x_max = 0, y_max = 0, width = 0;
1178 	unsigned int x_res = 31, y_res = 31;
1179 
1180 	if (elantech_set_range(psmouse, &x_min, &y_min, &x_max, &y_max, &width))
1181 		return -1;
1182 
1183 	__set_bit(INPUT_PROP_POINTER, dev->propbit);
1184 	__set_bit(EV_KEY, dev->evbit);
1185 	__set_bit(EV_ABS, dev->evbit);
1186 	__clear_bit(EV_REL, dev->evbit);
1187 
1188 	__set_bit(BTN_LEFT, dev->keybit);
1189 	if (dmi_check_system(elantech_dmi_has_middle_button))
1190 		__set_bit(BTN_MIDDLE, dev->keybit);
1191 	__set_bit(BTN_RIGHT, dev->keybit);
1192 
1193 	__set_bit(BTN_TOUCH, dev->keybit);
1194 	__set_bit(BTN_TOOL_FINGER, dev->keybit);
1195 	__set_bit(BTN_TOOL_DOUBLETAP, dev->keybit);
1196 	__set_bit(BTN_TOOL_TRIPLETAP, dev->keybit);
1197 
1198 	switch (etd->hw_version) {
1199 	case 1:
1200 		/* Rocker button */
1201 		if (etd->fw_version < 0x020000 &&
1202 		    (etd->capabilities[0] & ETP_CAP_HAS_ROCKER)) {
1203 			__set_bit(BTN_FORWARD, dev->keybit);
1204 			__set_bit(BTN_BACK, dev->keybit);
1205 		}
1206 		input_set_abs_params(dev, ABS_X, x_min, x_max, 0, 0);
1207 		input_set_abs_params(dev, ABS_Y, y_min, y_max, 0, 0);
1208 		break;
1209 
1210 	case 2:
1211 		__set_bit(BTN_TOOL_QUADTAP, dev->keybit);
1212 		__set_bit(INPUT_PROP_SEMI_MT, dev->propbit);
1213 		/* fall through */
1214 	case 3:
1215 		if (etd->hw_version == 3)
1216 			elantech_set_buttonpad_prop(psmouse);
1217 		input_set_abs_params(dev, ABS_X, x_min, x_max, 0, 0);
1218 		input_set_abs_params(dev, ABS_Y, y_min, y_max, 0, 0);
1219 		if (etd->reports_pressure) {
1220 			input_set_abs_params(dev, ABS_PRESSURE, ETP_PMIN_V2,
1221 					     ETP_PMAX_V2, 0, 0);
1222 			input_set_abs_params(dev, ABS_TOOL_WIDTH, ETP_WMIN_V2,
1223 					     ETP_WMAX_V2, 0, 0);
1224 		}
1225 		input_mt_init_slots(dev, 2, INPUT_MT_SEMI_MT);
1226 		input_set_abs_params(dev, ABS_MT_POSITION_X, x_min, x_max, 0, 0);
1227 		input_set_abs_params(dev, ABS_MT_POSITION_Y, y_min, y_max, 0, 0);
1228 		break;
1229 
1230 	case 4:
1231 		if (elantech_get_resolution_v4(psmouse, &x_res, &y_res)) {
1232 			/*
1233 			 * if query failed, print a warning and leave the values
1234 			 * zero to resemble synaptics.c behavior.
1235 			 */
1236 			psmouse_warn(psmouse, "couldn't query resolution data.\n");
1237 		}
1238 		elantech_set_buttonpad_prop(psmouse);
1239 		__set_bit(BTN_TOOL_QUADTAP, dev->keybit);
1240 		/* For X to recognize me as touchpad. */
1241 		input_set_abs_params(dev, ABS_X, x_min, x_max, 0, 0);
1242 		input_set_abs_params(dev, ABS_Y, y_min, y_max, 0, 0);
1243 		/*
1244 		 * range of pressure and width is the same as v2,
1245 		 * report ABS_PRESSURE, ABS_TOOL_WIDTH for compatibility.
1246 		 */
1247 		input_set_abs_params(dev, ABS_PRESSURE, ETP_PMIN_V2,
1248 				     ETP_PMAX_V2, 0, 0);
1249 		input_set_abs_params(dev, ABS_TOOL_WIDTH, ETP_WMIN_V2,
1250 				     ETP_WMAX_V2, 0, 0);
1251 		/* Multitouch capable pad, up to 5 fingers. */
1252 		input_mt_init_slots(dev, ETP_MAX_FINGERS, 0);
1253 		input_set_abs_params(dev, ABS_MT_POSITION_X, x_min, x_max, 0, 0);
1254 		input_set_abs_params(dev, ABS_MT_POSITION_Y, y_min, y_max, 0, 0);
1255 		input_set_abs_params(dev, ABS_MT_PRESSURE, ETP_PMIN_V2,
1256 				     ETP_PMAX_V2, 0, 0);
1257 		/*
1258 		 * The firmware reports how many trace lines the finger spans,
1259 		 * convert to surface unit as Protocol-B requires.
1260 		 */
1261 		input_set_abs_params(dev, ABS_MT_TOUCH_MAJOR, 0,
1262 				     ETP_WMAX_V2 * width, 0, 0);
1263 		break;
1264 	}
1265 
1266 	input_abs_set_res(dev, ABS_X, x_res);
1267 	input_abs_set_res(dev, ABS_Y, y_res);
1268 	if (etd->hw_version > 1) {
1269 		input_abs_set_res(dev, ABS_MT_POSITION_X, x_res);
1270 		input_abs_set_res(dev, ABS_MT_POSITION_Y, y_res);
1271 	}
1272 
1273 	etd->y_max = y_max;
1274 	etd->width = width;
1275 
1276 	return 0;
1277 }
1278 
1279 struct elantech_attr_data {
1280 	size_t		field_offset;
1281 	unsigned char	reg;
1282 };
1283 
1284 /*
1285  * Display a register value by reading a sysfs entry
1286  */
1287 static ssize_t elantech_show_int_attr(struct psmouse *psmouse, void *data,
1288 					char *buf)
1289 {
1290 	struct elantech_data *etd = psmouse->private;
1291 	struct elantech_attr_data *attr = data;
1292 	unsigned char *reg = (unsigned char *) etd + attr->field_offset;
1293 	int rc = 0;
1294 
1295 	if (attr->reg)
1296 		rc = elantech_read_reg(psmouse, attr->reg, reg);
1297 
1298 	return sprintf(buf, "0x%02x\n", (attr->reg && rc) ? -1 : *reg);
1299 }
1300 
1301 /*
1302  * Write a register value by writing a sysfs entry
1303  */
1304 static ssize_t elantech_set_int_attr(struct psmouse *psmouse,
1305 				     void *data, const char *buf, size_t count)
1306 {
1307 	struct elantech_data *etd = psmouse->private;
1308 	struct elantech_attr_data *attr = data;
1309 	unsigned char *reg = (unsigned char *) etd + attr->field_offset;
1310 	unsigned char value;
1311 	int err;
1312 
1313 	err = kstrtou8(buf, 16, &value);
1314 	if (err)
1315 		return err;
1316 
1317 	/* Do we need to preserve some bits for version 2 hardware too? */
1318 	if (etd->hw_version == 1) {
1319 		if (attr->reg == 0x10)
1320 			/* Force absolute mode always on */
1321 			value |= ETP_R10_ABSOLUTE_MODE;
1322 		else if (attr->reg == 0x11)
1323 			/* Force 4 byte mode always on */
1324 			value |= ETP_R11_4_BYTE_MODE;
1325 	}
1326 
1327 	if (!attr->reg || elantech_write_reg(psmouse, attr->reg, value) == 0)
1328 		*reg = value;
1329 
1330 	return count;
1331 }
1332 
1333 #define ELANTECH_INT_ATTR(_name, _register)				\
1334 	static struct elantech_attr_data elantech_attr_##_name = {	\
1335 		.field_offset = offsetof(struct elantech_data, _name),	\
1336 		.reg = _register,					\
1337 	};								\
1338 	PSMOUSE_DEFINE_ATTR(_name, S_IWUSR | S_IRUGO,			\
1339 			    &elantech_attr_##_name,			\
1340 			    elantech_show_int_attr,			\
1341 			    elantech_set_int_attr)
1342 
1343 ELANTECH_INT_ATTR(reg_07, 0x07);
1344 ELANTECH_INT_ATTR(reg_10, 0x10);
1345 ELANTECH_INT_ATTR(reg_11, 0x11);
1346 ELANTECH_INT_ATTR(reg_20, 0x20);
1347 ELANTECH_INT_ATTR(reg_21, 0x21);
1348 ELANTECH_INT_ATTR(reg_22, 0x22);
1349 ELANTECH_INT_ATTR(reg_23, 0x23);
1350 ELANTECH_INT_ATTR(reg_24, 0x24);
1351 ELANTECH_INT_ATTR(reg_25, 0x25);
1352 ELANTECH_INT_ATTR(reg_26, 0x26);
1353 ELANTECH_INT_ATTR(debug, 0);
1354 ELANTECH_INT_ATTR(paritycheck, 0);
1355 ELANTECH_INT_ATTR(crc_enabled, 0);
1356 
1357 static struct attribute *elantech_attrs[] = {
1358 	&psmouse_attr_reg_07.dattr.attr,
1359 	&psmouse_attr_reg_10.dattr.attr,
1360 	&psmouse_attr_reg_11.dattr.attr,
1361 	&psmouse_attr_reg_20.dattr.attr,
1362 	&psmouse_attr_reg_21.dattr.attr,
1363 	&psmouse_attr_reg_22.dattr.attr,
1364 	&psmouse_attr_reg_23.dattr.attr,
1365 	&psmouse_attr_reg_24.dattr.attr,
1366 	&psmouse_attr_reg_25.dattr.attr,
1367 	&psmouse_attr_reg_26.dattr.attr,
1368 	&psmouse_attr_debug.dattr.attr,
1369 	&psmouse_attr_paritycheck.dattr.attr,
1370 	&psmouse_attr_crc_enabled.dattr.attr,
1371 	NULL
1372 };
1373 
1374 static struct attribute_group elantech_attr_group = {
1375 	.attrs = elantech_attrs,
1376 };
1377 
1378 static bool elantech_is_signature_valid(const unsigned char *param)
1379 {
1380 	static const unsigned char rates[] = { 200, 100, 80, 60, 40, 20, 10 };
1381 	int i;
1382 
1383 	if (param[0] == 0)
1384 		return false;
1385 
1386 	if (param[1] == 0)
1387 		return true;
1388 
1389 	/*
1390 	 * Some hw_version >= 4 models have a revision higher then 20. Meaning
1391 	 * that param[2] may be 10 or 20, skip the rates check for these.
1392 	 */
1393 	if ((param[0] & 0x0f) >= 0x06 && (param[1] & 0xaf) == 0x0f &&
1394 	    param[2] < 40)
1395 		return true;
1396 
1397 	for (i = 0; i < ARRAY_SIZE(rates); i++)
1398 		if (param[2] == rates[i])
1399 			return false;
1400 
1401 	return true;
1402 }
1403 
1404 /*
1405  * Use magic knock to detect Elantech touchpad
1406  */
1407 int elantech_detect(struct psmouse *psmouse, bool set_properties)
1408 {
1409 	struct ps2dev *ps2dev = &psmouse->ps2dev;
1410 	unsigned char param[3];
1411 
1412 	ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
1413 
1414 	if (ps2_command(ps2dev,  NULL, PSMOUSE_CMD_DISABLE) ||
1415 	    ps2_command(ps2dev,  NULL, PSMOUSE_CMD_SETSCALE11) ||
1416 	    ps2_command(ps2dev,  NULL, PSMOUSE_CMD_SETSCALE11) ||
1417 	    ps2_command(ps2dev,  NULL, PSMOUSE_CMD_SETSCALE11) ||
1418 	    ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO)) {
1419 		psmouse_dbg(psmouse, "sending Elantech magic knock failed.\n");
1420 		return -1;
1421 	}
1422 
1423 	/*
1424 	 * Report this in case there are Elantech models that use a different
1425 	 * set of magic numbers
1426 	 */
1427 	if (param[0] != 0x3c || param[1] != 0x03 ||
1428 	    (param[2] != 0xc8 && param[2] != 0x00)) {
1429 		psmouse_dbg(psmouse,
1430 			    "unexpected magic knock result 0x%02x, 0x%02x, 0x%02x.\n",
1431 			    param[0], param[1], param[2]);
1432 		return -1;
1433 	}
1434 
1435 	/*
1436 	 * Query touchpad's firmware version and see if it reports known
1437 	 * value to avoid mis-detection. Logitech mice are known to respond
1438 	 * to Elantech magic knock and there might be more.
1439 	 */
1440 	if (synaptics_send_cmd(psmouse, ETP_FW_VERSION_QUERY, param)) {
1441 		psmouse_dbg(psmouse, "failed to query firmware version.\n");
1442 		return -1;
1443 	}
1444 
1445 	psmouse_dbg(psmouse,
1446 		    "Elantech version query result 0x%02x, 0x%02x, 0x%02x.\n",
1447 		    param[0], param[1], param[2]);
1448 
1449 	if (!elantech_is_signature_valid(param)) {
1450 		psmouse_dbg(psmouse,
1451 			    "Probably not a real Elantech touchpad. Aborting.\n");
1452 		return -1;
1453 	}
1454 
1455 	if (set_properties) {
1456 		psmouse->vendor = "Elantech";
1457 		psmouse->name = "Touchpad";
1458 	}
1459 
1460 	return 0;
1461 }
1462 
1463 /*
1464  * Clean up sysfs entries when disconnecting
1465  */
1466 static void elantech_disconnect(struct psmouse *psmouse)
1467 {
1468 	struct elantech_data *etd = psmouse->private;
1469 
1470 	if (etd->tp_dev)
1471 		input_unregister_device(etd->tp_dev);
1472 	sysfs_remove_group(&psmouse->ps2dev.serio->dev.kobj,
1473 			   &elantech_attr_group);
1474 	kfree(psmouse->private);
1475 	psmouse->private = NULL;
1476 }
1477 
1478 /*
1479  * Put the touchpad back into absolute mode when reconnecting
1480  */
1481 static int elantech_reconnect(struct psmouse *psmouse)
1482 {
1483 	psmouse_reset(psmouse);
1484 
1485 	if (elantech_detect(psmouse, 0))
1486 		return -1;
1487 
1488 	if (elantech_set_absolute_mode(psmouse)) {
1489 		psmouse_err(psmouse,
1490 			    "failed to put touchpad back into absolute mode.\n");
1491 		return -1;
1492 	}
1493 
1494 	return 0;
1495 }
1496 
1497 /*
1498  * Some hw_version 4 models do not work with crc_disabled
1499  */
1500 static const struct dmi_system_id elantech_dmi_force_crc_enabled[] = {
1501 #if defined(CONFIG_DMI) && defined(CONFIG_X86)
1502 	{
1503 		/* Fujitsu H730 does not work with crc_enabled == 0 */
1504 		.matches = {
1505 			DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU"),
1506 			DMI_MATCH(DMI_PRODUCT_NAME, "CELSIUS H730"),
1507 		},
1508 	},
1509 	{
1510 		/* Fujitsu LIFEBOOK E554  does not work with crc_enabled == 0 */
1511 		.matches = {
1512 			DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU"),
1513 			DMI_MATCH(DMI_PRODUCT_NAME, "LIFEBOOK E554"),
1514 		},
1515 	},
1516 	{
1517 		/* Fujitsu LIFEBOOK E544  does not work with crc_enabled == 0 */
1518 		.matches = {
1519 			DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU"),
1520 			DMI_MATCH(DMI_PRODUCT_NAME, "LIFEBOOK E544"),
1521 		},
1522 	},
1523 	{
1524 		/* Fujitsu LIFEBOOK U745 does not work with crc_enabled == 0 */
1525 		.matches = {
1526 			DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU"),
1527 			DMI_MATCH(DMI_PRODUCT_NAME, "LIFEBOOK U745"),
1528 		},
1529 	},
1530 #endif
1531 	{ }
1532 };
1533 
1534 /*
1535  * Some hw_version 3 models go into error state when we try to set
1536  * bit 3 and/or bit 1 of r10.
1537  */
1538 static const struct dmi_system_id no_hw_res_dmi_table[] = {
1539 #if defined(CONFIG_DMI) && defined(CONFIG_X86)
1540 	{
1541 		/* Gigabyte U2442 */
1542 		.matches = {
1543 			DMI_MATCH(DMI_SYS_VENDOR, "GIGABYTE"),
1544 			DMI_MATCH(DMI_PRODUCT_NAME, "U2442"),
1545 		},
1546 	},
1547 #endif
1548 	{ }
1549 };
1550 
1551 /*
1552  * determine hardware version and set some properties according to it.
1553  */
1554 static int elantech_set_properties(struct elantech_data *etd)
1555 {
1556 	/* This represents the version of IC body. */
1557 	int ver = (etd->fw_version & 0x0f0000) >> 16;
1558 
1559 	/* Early version of Elan touchpads doesn't obey the rule. */
1560 	if (etd->fw_version < 0x020030 || etd->fw_version == 0x020600)
1561 		etd->hw_version = 1;
1562 	else {
1563 		switch (ver) {
1564 		case 2:
1565 		case 4:
1566 			etd->hw_version = 2;
1567 			break;
1568 		case 5:
1569 			etd->hw_version = 3;
1570 			break;
1571 		case 6 ... 14:
1572 			etd->hw_version = 4;
1573 			break;
1574 		default:
1575 			return -1;
1576 		}
1577 	}
1578 
1579 	/* decide which send_cmd we're gonna use early */
1580 	etd->send_cmd = etd->hw_version >= 3 ? elantech_send_cmd :
1581 					       synaptics_send_cmd;
1582 
1583 	/* Turn on packet checking by default */
1584 	etd->paritycheck = 1;
1585 
1586 	/*
1587 	 * This firmware suffers from misreporting coordinates when
1588 	 * a touch action starts causing the mouse cursor or scrolled page
1589 	 * to jump. Enable a workaround.
1590 	 */
1591 	etd->jumpy_cursor =
1592 		(etd->fw_version == 0x020022 || etd->fw_version == 0x020600);
1593 
1594 	if (etd->hw_version > 1) {
1595 		/* For now show extra debug information */
1596 		etd->debug = 1;
1597 
1598 		if (etd->fw_version >= 0x020800)
1599 			etd->reports_pressure = true;
1600 	}
1601 
1602 	/*
1603 	 * The signatures of v3 and v4 packets change depending on the
1604 	 * value of this hardware flag.
1605 	 */
1606 	etd->crc_enabled = (etd->fw_version & 0x4000) == 0x4000 ||
1607 			   dmi_check_system(elantech_dmi_force_crc_enabled);
1608 
1609 	/* Enable real hardware resolution on hw_version 3 ? */
1610 	etd->set_hw_resolution = !dmi_check_system(no_hw_res_dmi_table);
1611 
1612 	return 0;
1613 }
1614 
1615 /*
1616  * Initialize the touchpad and create sysfs entries
1617  */
1618 int elantech_init(struct psmouse *psmouse)
1619 {
1620 	struct elantech_data *etd;
1621 	int i;
1622 	int error = -EINVAL;
1623 	unsigned char param[3];
1624 	struct input_dev *tp_dev;
1625 
1626 	psmouse->private = etd = kzalloc(sizeof(struct elantech_data), GFP_KERNEL);
1627 	if (!etd)
1628 		return -ENOMEM;
1629 
1630 	psmouse_reset(psmouse);
1631 
1632 	etd->parity[0] = 1;
1633 	for (i = 1; i < 256; i++)
1634 		etd->parity[i] = etd->parity[i & (i - 1)] ^ 1;
1635 
1636 	/*
1637 	 * Do the version query again so we can store the result
1638 	 */
1639 	if (synaptics_send_cmd(psmouse, ETP_FW_VERSION_QUERY, param)) {
1640 		psmouse_err(psmouse, "failed to query firmware version.\n");
1641 		goto init_fail;
1642 	}
1643 	etd->fw_version = (param[0] << 16) | (param[1] << 8) | param[2];
1644 
1645 	if (elantech_set_properties(etd)) {
1646 		psmouse_err(psmouse, "unknown hardware version, aborting...\n");
1647 		goto init_fail;
1648 	}
1649 	psmouse_info(psmouse,
1650 		     "assuming hardware version %d (with firmware version 0x%02x%02x%02x)\n",
1651 		     etd->hw_version, param[0], param[1], param[2]);
1652 
1653 	if (etd->send_cmd(psmouse, ETP_CAPABILITIES_QUERY,
1654 	    etd->capabilities)) {
1655 		psmouse_err(psmouse, "failed to query capabilities.\n");
1656 		goto init_fail;
1657 	}
1658 	psmouse_info(psmouse,
1659 		     "Synaptics capabilities query result 0x%02x, 0x%02x, 0x%02x.\n",
1660 		     etd->capabilities[0], etd->capabilities[1],
1661 		     etd->capabilities[2]);
1662 
1663 	if (etd->hw_version != 1) {
1664 		if (etd->send_cmd(psmouse, ETP_SAMPLE_QUERY, etd->samples)) {
1665 			psmouse_err(psmouse, "failed to query sample data\n");
1666 			goto init_fail;
1667 		}
1668 		psmouse_info(psmouse,
1669 			     "Elan sample query result %02x, %02x, %02x\n",
1670 			     etd->samples[0], etd->samples[1], etd->samples[2]);
1671 	}
1672 
1673 	if (elantech_set_absolute_mode(psmouse)) {
1674 		psmouse_err(psmouse,
1675 			    "failed to put touchpad into absolute mode.\n");
1676 		goto init_fail;
1677 	}
1678 
1679 	if (etd->fw_version == 0x381f17) {
1680 		etd->original_set_rate = psmouse->set_rate;
1681 		psmouse->set_rate = elantech_set_rate_restore_reg_07;
1682 	}
1683 
1684 	if (elantech_set_input_params(psmouse)) {
1685 		psmouse_err(psmouse, "failed to query touchpad range.\n");
1686 		goto init_fail;
1687 	}
1688 
1689 	error = sysfs_create_group(&psmouse->ps2dev.serio->dev.kobj,
1690 				   &elantech_attr_group);
1691 	if (error) {
1692 		psmouse_err(psmouse,
1693 			    "failed to create sysfs attributes, error: %d.\n",
1694 			    error);
1695 		goto init_fail;
1696 	}
1697 
1698 	/* The MSB indicates the presence of the trackpoint */
1699 	if ((etd->capabilities[0] & 0x80) == 0x80) {
1700 		tp_dev = input_allocate_device();
1701 
1702 		if (!tp_dev) {
1703 			error = -ENOMEM;
1704 			goto init_fail_tp_alloc;
1705 		}
1706 
1707 		etd->tp_dev = tp_dev;
1708 		snprintf(etd->tp_phys, sizeof(etd->tp_phys), "%s/input1",
1709 			psmouse->ps2dev.serio->phys);
1710 		tp_dev->phys = etd->tp_phys;
1711 		tp_dev->name = "Elantech PS/2 TrackPoint";
1712 		tp_dev->id.bustype = BUS_I8042;
1713 		tp_dev->id.vendor  = 0x0002;
1714 		tp_dev->id.product = PSMOUSE_ELANTECH;
1715 		tp_dev->id.version = 0x0000;
1716 		tp_dev->dev.parent = &psmouse->ps2dev.serio->dev;
1717 		tp_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
1718 		tp_dev->relbit[BIT_WORD(REL_X)] =
1719 			BIT_MASK(REL_X) | BIT_MASK(REL_Y);
1720 		tp_dev->keybit[BIT_WORD(BTN_LEFT)] =
1721 			BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_MIDDLE) |
1722 			BIT_MASK(BTN_RIGHT);
1723 
1724 		__set_bit(INPUT_PROP_POINTER, tp_dev->propbit);
1725 		__set_bit(INPUT_PROP_POINTING_STICK, tp_dev->propbit);
1726 
1727 		error = input_register_device(etd->tp_dev);
1728 		if (error < 0)
1729 			goto init_fail_tp_reg;
1730 	}
1731 
1732 	psmouse->protocol_handler = elantech_process_byte;
1733 	psmouse->disconnect = elantech_disconnect;
1734 	psmouse->reconnect = elantech_reconnect;
1735 	psmouse->pktsize = etd->hw_version > 1 ? 6 : 4;
1736 
1737 	return 0;
1738  init_fail_tp_reg:
1739 	input_free_device(tp_dev);
1740  init_fail_tp_alloc:
1741 	sysfs_remove_group(&psmouse->ps2dev.serio->dev.kobj,
1742 			   &elantech_attr_group);
1743  init_fail:
1744 	psmouse_reset(psmouse);
1745 	kfree(etd);
1746 	return error;
1747 }
1748