xref: /openbmc/linux/drivers/input/mouse/elantech.c (revision 31e67366)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Elantech Touchpad driver (v6)
4  *
5  * Copyright (C) 2007-2009 Arjan Opmeer <arjan@opmeer.net>
6  *
7  * Trademarks are the property of their respective owners.
8  */
9 
10 #include <linux/delay.h>
11 #include <linux/dmi.h>
12 #include <linux/slab.h>
13 #include <linux/module.h>
14 #include <linux/i2c.h>
15 #include <linux/input.h>
16 #include <linux/input/mt.h>
17 #include <linux/platform_device.h>
18 #include <linux/serio.h>
19 #include <linux/libps2.h>
20 #include <asm/unaligned.h>
21 #include "psmouse.h"
22 #include "elantech.h"
23 #include "elan_i2c.h"
24 
25 #define elantech_debug(fmt, ...)					\
26 	do {								\
27 		if (etd->info.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 (ps2_sliced_command(&psmouse->ps2dev, 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 3 bytes from a register
94  */
95 static int elantech_read_reg_params(struct psmouse *psmouse, u8 reg, u8 *param)
96 {
97 	if (elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
98 	    elantech_ps2_command(psmouse, NULL, ETP_REGISTER_READWRITE) ||
99 	    elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
100 	    elantech_ps2_command(psmouse, NULL, reg) ||
101 	    elantech_ps2_command(psmouse, param, PSMOUSE_CMD_GETINFO)) {
102 		psmouse_err(psmouse,
103 			    "failed to read register %#02x\n", reg);
104 		return -EIO;
105 	}
106 
107 	return 0;
108 }
109 
110 /*
111  * Send an Elantech style special command to write a register with a parameter
112  */
113 static int elantech_write_reg_params(struct psmouse *psmouse, u8 reg, u8 *param)
114 {
115 	if (elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
116 	    elantech_ps2_command(psmouse, NULL, ETP_REGISTER_READWRITE) ||
117 	    elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
118 	    elantech_ps2_command(psmouse, NULL, reg) ||
119 	    elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
120 	    elantech_ps2_command(psmouse, NULL, param[0]) ||
121 	    elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
122 	    elantech_ps2_command(psmouse, NULL, param[1]) ||
123 	    elantech_ps2_command(psmouse, NULL, PSMOUSE_CMD_SETSCALE11)) {
124 		psmouse_err(psmouse,
125 			    "failed to write register %#02x with value %#02x%#02x\n",
126 			    reg, param[0], param[1]);
127 		return -EIO;
128 	}
129 
130 	return 0;
131 }
132 
133 /*
134  * Send an Elantech style special command to read a value from a register
135  */
136 static int elantech_read_reg(struct psmouse *psmouse, unsigned char reg,
137 				unsigned char *val)
138 {
139 	struct elantech_data *etd = psmouse->private;
140 	unsigned char param[3];
141 	int rc = 0;
142 
143 	if (reg < 0x07 || reg > 0x26)
144 		return -1;
145 
146 	if (reg > 0x11 && reg < 0x20)
147 		return -1;
148 
149 	switch (etd->info.hw_version) {
150 	case 1:
151 		if (ps2_sliced_command(&psmouse->ps2dev, ETP_REGISTER_READ) ||
152 		    ps2_sliced_command(&psmouse->ps2dev, reg) ||
153 		    ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_GETINFO)) {
154 			rc = -1;
155 		}
156 		break;
157 
158 	case 2:
159 		if (elantech_ps2_command(psmouse,  NULL, ETP_PS2_CUSTOM_COMMAND) ||
160 		    elantech_ps2_command(psmouse,  NULL, ETP_REGISTER_READ) ||
161 		    elantech_ps2_command(psmouse,  NULL, ETP_PS2_CUSTOM_COMMAND) ||
162 		    elantech_ps2_command(psmouse,  NULL, reg) ||
163 		    elantech_ps2_command(psmouse, param, PSMOUSE_CMD_GETINFO)) {
164 			rc = -1;
165 		}
166 		break;
167 
168 	case 3 ... 4:
169 		if (elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
170 		    elantech_ps2_command(psmouse, NULL, ETP_REGISTER_READWRITE) ||
171 		    elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
172 		    elantech_ps2_command(psmouse, NULL, reg) ||
173 		    elantech_ps2_command(psmouse, param, PSMOUSE_CMD_GETINFO)) {
174 			rc = -1;
175 		}
176 		break;
177 	}
178 
179 	if (rc)
180 		psmouse_err(psmouse, "failed to read register 0x%02x.\n", reg);
181 	else if (etd->info.hw_version != 4)
182 		*val = param[0];
183 	else
184 		*val = param[1];
185 
186 	return rc;
187 }
188 
189 /*
190  * Send an Elantech style special command to write a register with a value
191  */
192 static int elantech_write_reg(struct psmouse *psmouse, unsigned char reg,
193 				unsigned char val)
194 {
195 	struct elantech_data *etd = psmouse->private;
196 	int rc = 0;
197 
198 	if (reg < 0x07 || reg > 0x26)
199 		return -1;
200 
201 	if (reg > 0x11 && reg < 0x20)
202 		return -1;
203 
204 	switch (etd->info.hw_version) {
205 	case 1:
206 		if (ps2_sliced_command(&psmouse->ps2dev, ETP_REGISTER_WRITE) ||
207 		    ps2_sliced_command(&psmouse->ps2dev, reg) ||
208 		    ps2_sliced_command(&psmouse->ps2dev, val) ||
209 		    ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11)) {
210 			rc = -1;
211 		}
212 		break;
213 
214 	case 2:
215 		if (elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
216 		    elantech_ps2_command(psmouse, NULL, ETP_REGISTER_WRITE) ||
217 		    elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
218 		    elantech_ps2_command(psmouse, NULL, reg) ||
219 		    elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
220 		    elantech_ps2_command(psmouse, NULL, val) ||
221 		    elantech_ps2_command(psmouse, NULL, PSMOUSE_CMD_SETSCALE11)) {
222 			rc = -1;
223 		}
224 		break;
225 
226 	case 3:
227 		if (elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
228 		    elantech_ps2_command(psmouse, NULL, ETP_REGISTER_READWRITE) ||
229 		    elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
230 		    elantech_ps2_command(psmouse, NULL, reg) ||
231 		    elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
232 		    elantech_ps2_command(psmouse, NULL, val) ||
233 		    elantech_ps2_command(psmouse, NULL, PSMOUSE_CMD_SETSCALE11)) {
234 			rc = -1;
235 		}
236 		break;
237 
238 	case 4:
239 		if (elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
240 		    elantech_ps2_command(psmouse, NULL, ETP_REGISTER_READWRITE) ||
241 		    elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
242 		    elantech_ps2_command(psmouse, NULL, reg) ||
243 		    elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
244 		    elantech_ps2_command(psmouse, NULL, ETP_REGISTER_READWRITE) ||
245 		    elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
246 		    elantech_ps2_command(psmouse, NULL, val) ||
247 		    elantech_ps2_command(psmouse, NULL, PSMOUSE_CMD_SETSCALE11)) {
248 			rc = -1;
249 		}
250 		break;
251 	}
252 
253 	if (rc)
254 		psmouse_err(psmouse,
255 			    "failed to write register 0x%02x with value 0x%02x.\n",
256 			    reg, val);
257 
258 	return rc;
259 }
260 
261 /*
262  * Dump a complete mouse movement packet to the syslog
263  */
264 static void elantech_packet_dump(struct psmouse *psmouse)
265 {
266 	psmouse_printk(KERN_DEBUG, psmouse, "PS/2 packet [%*ph]\n",
267 		       psmouse->pktsize, psmouse->packet);
268 }
269 
270 /*
271  * Advertise INPUT_PROP_BUTTONPAD for clickpads. The testing of bit 12 in
272  * fw_version for this is based on the following fw_version & caps table:
273  *
274  * Laptop-model:           fw_version:     caps:           buttons:
275  * Acer S3                 0x461f00        10, 13, 0e      clickpad
276  * Acer S7-392             0x581f01        50, 17, 0d      clickpad
277  * Acer V5-131             0x461f02        01, 16, 0c      clickpad
278  * Acer V5-551             0x461f00        ?               clickpad
279  * Asus K53SV              0x450f01        78, 15, 0c      2 hw buttons
280  * Asus G46VW              0x460f02        00, 18, 0c      2 hw buttons
281  * Asus G750JX             0x360f00        00, 16, 0c      2 hw buttons
282  * Asus TP500LN            0x381f17        10, 14, 0e      clickpad
283  * Asus X750JN             0x381f17        10, 14, 0e      clickpad
284  * Asus UX31               0x361f00        20, 15, 0e      clickpad
285  * Asus UX32VD             0x361f02        00, 15, 0e      clickpad
286  * Avatar AVIU-145A2       0x361f00        ?               clickpad
287  * Fujitsu CELSIUS H760    0x570f02        40, 14, 0c      3 hw buttons (**)
288  * Fujitsu CELSIUS H780    0x5d0f02        41, 16, 0d      3 hw buttons (**)
289  * Fujitsu LIFEBOOK E544   0x470f00        d0, 12, 09      2 hw buttons
290  * Fujitsu LIFEBOOK E546   0x470f00        50, 12, 09      2 hw buttons
291  * Fujitsu LIFEBOOK E547   0x470f00        50, 12, 09      2 hw buttons
292  * Fujitsu LIFEBOOK E554   0x570f01        40, 14, 0c      2 hw buttons
293  * Fujitsu LIFEBOOK E557   0x570f01        40, 14, 0c      2 hw buttons
294  * Fujitsu T725            0x470f01        05, 12, 09      2 hw buttons
295  * Fujitsu H730            0x570f00        c0, 14, 0c      3 hw buttons (**)
296  * Gigabyte U2442          0x450f01        58, 17, 0c      2 hw buttons
297  * Lenovo L430             0x350f02        b9, 15, 0c      2 hw buttons (*)
298  * Lenovo L530             0x350f02        b9, 15, 0c      2 hw buttons (*)
299  * Samsung NF210           0x150b00        78, 14, 0a      2 hw buttons
300  * Samsung NP770Z5E        0x575f01        10, 15, 0f      clickpad
301  * Samsung NP700Z5B        0x361f06        21, 15, 0f      clickpad
302  * Samsung NP900X3E-A02    0x575f03        ?               clickpad
303  * Samsung NP-QX410        0x851b00        19, 14, 0c      clickpad
304  * Samsung RC512           0x450f00        08, 15, 0c      2 hw buttons
305  * Samsung RF710           0x450f00        ?               2 hw buttons
306  * System76 Pangolin       0x250f01        ?               2 hw buttons
307  * (*) + 3 trackpoint buttons
308  * (**) + 0 trackpoint buttons
309  * Note: Lenovo L430 and Lenovo L530 have the same fw_version/caps
310  */
311 static inline int elantech_is_buttonpad(struct elantech_device_info *info)
312 {
313 	return info->fw_version & 0x001000;
314 }
315 
316 /*
317  * Interpret complete data packets and report absolute mode input events for
318  * hardware version 1. (4 byte packets)
319  */
320 static void elantech_report_absolute_v1(struct psmouse *psmouse)
321 {
322 	struct input_dev *dev = psmouse->dev;
323 	struct elantech_data *etd = psmouse->private;
324 	unsigned char *packet = psmouse->packet;
325 	int fingers;
326 
327 	if (etd->info.fw_version < 0x020000) {
328 		/*
329 		 * byte 0:  D   U  p1  p2   1  p3   R   L
330 		 * byte 1:  f   0  th  tw  x9  x8  y9  y8
331 		 */
332 		fingers = ((packet[1] & 0x80) >> 7) +
333 				((packet[1] & 0x30) >> 4);
334 	} else {
335 		/*
336 		 * byte 0: n1  n0  p2  p1   1  p3   R   L
337 		 * byte 1:  0   0   0   0  x9  x8  y9  y8
338 		 */
339 		fingers = (packet[0] & 0xc0) >> 6;
340 	}
341 
342 	if (etd->info.jumpy_cursor) {
343 		if (fingers != 1) {
344 			etd->single_finger_reports = 0;
345 		} else if (etd->single_finger_reports < 2) {
346 			/* Discard first 2 reports of one finger, bogus */
347 			etd->single_finger_reports++;
348 			elantech_debug("discarding packet\n");
349 			return;
350 		}
351 	}
352 
353 	input_report_key(dev, BTN_TOUCH, fingers != 0);
354 
355 	/*
356 	 * byte 2: x7  x6  x5  x4  x3  x2  x1  x0
357 	 * byte 3: y7  y6  y5  y4  y3  y2  y1  y0
358 	 */
359 	if (fingers) {
360 		input_report_abs(dev, ABS_X,
361 			((packet[1] & 0x0c) << 6) | packet[2]);
362 		input_report_abs(dev, ABS_Y,
363 			etd->y_max - (((packet[1] & 0x03) << 8) | packet[3]));
364 	}
365 
366 	input_report_key(dev, BTN_TOOL_FINGER, fingers == 1);
367 	input_report_key(dev, BTN_TOOL_DOUBLETAP, fingers == 2);
368 	input_report_key(dev, BTN_TOOL_TRIPLETAP, fingers == 3);
369 
370 	psmouse_report_standard_buttons(dev, packet[0]);
371 
372 	if (etd->info.fw_version < 0x020000 &&
373 	    (etd->info.capabilities[0] & ETP_CAP_HAS_ROCKER)) {
374 		/* rocker up */
375 		input_report_key(dev, BTN_FORWARD, packet[0] & 0x40);
376 		/* rocker down */
377 		input_report_key(dev, BTN_BACK, packet[0] & 0x80);
378 	}
379 
380 	input_sync(dev);
381 }
382 
383 static void elantech_set_slot(struct input_dev *dev, int slot, bool active,
384 			      unsigned int x, unsigned int y)
385 {
386 	input_mt_slot(dev, slot);
387 	input_mt_report_slot_state(dev, MT_TOOL_FINGER, active);
388 	if (active) {
389 		input_report_abs(dev, ABS_MT_POSITION_X, x);
390 		input_report_abs(dev, ABS_MT_POSITION_Y, y);
391 	}
392 }
393 
394 /* x1 < x2 and y1 < y2 when two fingers, x = y = 0 when not pressed */
395 static void elantech_report_semi_mt_data(struct input_dev *dev,
396 					 unsigned int num_fingers,
397 					 unsigned int x1, unsigned int y1,
398 					 unsigned int x2, unsigned int y2)
399 {
400 	elantech_set_slot(dev, 0, num_fingers != 0, x1, y1);
401 	elantech_set_slot(dev, 1, num_fingers >= 2, x2, y2);
402 }
403 
404 /*
405  * Interpret complete data packets and report absolute mode input events for
406  * hardware version 2. (6 byte packets)
407  */
408 static void elantech_report_absolute_v2(struct psmouse *psmouse)
409 {
410 	struct elantech_data *etd = psmouse->private;
411 	struct input_dev *dev = psmouse->dev;
412 	unsigned char *packet = psmouse->packet;
413 	unsigned int fingers, x1 = 0, y1 = 0, x2 = 0, y2 = 0;
414 	unsigned int width = 0, pres = 0;
415 
416 	/* byte 0: n1  n0   .   .   .   .   R   L */
417 	fingers = (packet[0] & 0xc0) >> 6;
418 
419 	switch (fingers) {
420 	case 3:
421 		/*
422 		 * Same as one finger, except report of more than 3 fingers:
423 		 * byte 3:  n4  .   w1  w0   .   .   .   .
424 		 */
425 		if (packet[3] & 0x80)
426 			fingers = 4;
427 		fallthrough;
428 	case 1:
429 		/*
430 		 * byte 1:  .   .   .   .  x11 x10 x9  x8
431 		 * byte 2: x7  x6  x5  x4  x4  x2  x1  x0
432 		 */
433 		x1 = ((packet[1] & 0x0f) << 8) | packet[2];
434 		/*
435 		 * byte 4:  .   .   .   .  y11 y10 y9  y8
436 		 * byte 5: y7  y6  y5  y4  y3  y2  y1  y0
437 		 */
438 		y1 = etd->y_max - (((packet[4] & 0x0f) << 8) | packet[5]);
439 
440 		pres = (packet[1] & 0xf0) | ((packet[4] & 0xf0) >> 4);
441 		width = ((packet[0] & 0x30) >> 2) | ((packet[3] & 0x30) >> 4);
442 		break;
443 
444 	case 2:
445 		/*
446 		 * The coordinate of each finger is reported separately
447 		 * with a lower resolution for two finger touches:
448 		 * byte 0:  .   .  ay8 ax8  .   .   .   .
449 		 * byte 1: ax7 ax6 ax5 ax4 ax3 ax2 ax1 ax0
450 		 */
451 		x1 = (((packet[0] & 0x10) << 4) | packet[1]) << 2;
452 		/* byte 2: ay7 ay6 ay5 ay4 ay3 ay2 ay1 ay0 */
453 		y1 = etd->y_max -
454 			((((packet[0] & 0x20) << 3) | packet[2]) << 2);
455 		/*
456 		 * byte 3:  .   .  by8 bx8  .   .   .   .
457 		 * byte 4: bx7 bx6 bx5 bx4 bx3 bx2 bx1 bx0
458 		 */
459 		x2 = (((packet[3] & 0x10) << 4) | packet[4]) << 2;
460 		/* byte 5: by7 by8 by5 by4 by3 by2 by1 by0 */
461 		y2 = etd->y_max -
462 			((((packet[3] & 0x20) << 3) | packet[5]) << 2);
463 
464 		/* Unknown so just report sensible values */
465 		pres = 127;
466 		width = 7;
467 		break;
468 	}
469 
470 	input_report_key(dev, BTN_TOUCH, fingers != 0);
471 	if (fingers != 0) {
472 		input_report_abs(dev, ABS_X, x1);
473 		input_report_abs(dev, ABS_Y, y1);
474 	}
475 	elantech_report_semi_mt_data(dev, fingers, x1, y1, x2, y2);
476 	input_report_key(dev, BTN_TOOL_FINGER, fingers == 1);
477 	input_report_key(dev, BTN_TOOL_DOUBLETAP, fingers == 2);
478 	input_report_key(dev, BTN_TOOL_TRIPLETAP, fingers == 3);
479 	input_report_key(dev, BTN_TOOL_QUADTAP, fingers == 4);
480 	psmouse_report_standard_buttons(dev, packet[0]);
481 	if (etd->info.reports_pressure) {
482 		input_report_abs(dev, ABS_PRESSURE, pres);
483 		input_report_abs(dev, ABS_TOOL_WIDTH, width);
484 	}
485 
486 	input_sync(dev);
487 }
488 
489 static void elantech_report_trackpoint(struct psmouse *psmouse,
490 				       int packet_type)
491 {
492 	/*
493 	 * byte 0:  0   0  sx  sy   0   M   R   L
494 	 * byte 1:~sx   0   0   0   0   0   0   0
495 	 * byte 2:~sy   0   0   0   0   0   0   0
496 	 * byte 3:  0   0 ~sy ~sx   0   1   1   0
497 	 * byte 4: x7  x6  x5  x4  x3  x2  x1  x0
498 	 * byte 5: y7  y6  y5  y4  y3  y2  y1  y0
499 	 *
500 	 * x and y are written in two's complement spread
501 	 * over 9 bits with sx/sy the relative top bit and
502 	 * x7..x0 and y7..y0 the lower bits.
503 	 * The sign of y is opposite to what the input driver
504 	 * expects for a relative movement
505 	 */
506 
507 	struct elantech_data *etd = psmouse->private;
508 	struct input_dev *tp_dev = etd->tp_dev;
509 	unsigned char *packet = psmouse->packet;
510 	int x, y;
511 	u32 t;
512 
513 	t = get_unaligned_le32(&packet[0]);
514 
515 	switch (t & ~7U) {
516 	case 0x06000030U:
517 	case 0x16008020U:
518 	case 0x26800010U:
519 	case 0x36808000U:
520 		x = packet[4] - (int)((packet[1]^0x80) << 1);
521 		y = (int)((packet[2]^0x80) << 1) - packet[5];
522 
523 		psmouse_report_standard_buttons(tp_dev, packet[0]);
524 
525 		input_report_rel(tp_dev, REL_X, x);
526 		input_report_rel(tp_dev, REL_Y, y);
527 
528 		input_sync(tp_dev);
529 
530 		break;
531 
532 	default:
533 		/* Dump unexpected packet sequences if debug=1 (default) */
534 		if (etd->info.debug == 1)
535 			elantech_packet_dump(psmouse);
536 
537 		break;
538 	}
539 }
540 
541 /*
542  * Interpret complete data packets and report absolute mode input events for
543  * hardware version 3. (12 byte packets for two fingers)
544  */
545 static void elantech_report_absolute_v3(struct psmouse *psmouse,
546 					int packet_type)
547 {
548 	struct input_dev *dev = psmouse->dev;
549 	struct elantech_data *etd = psmouse->private;
550 	unsigned char *packet = psmouse->packet;
551 	unsigned int fingers = 0, x1 = 0, y1 = 0, x2 = 0, y2 = 0;
552 	unsigned int width = 0, pres = 0;
553 
554 	/* byte 0: n1  n0   .   .   .   .   R   L */
555 	fingers = (packet[0] & 0xc0) >> 6;
556 
557 	switch (fingers) {
558 	case 3:
559 	case 1:
560 		/*
561 		 * byte 1:  .   .   .   .  x11 x10 x9  x8
562 		 * byte 2: x7  x6  x5  x4  x4  x2  x1  x0
563 		 */
564 		x1 = ((packet[1] & 0x0f) << 8) | packet[2];
565 		/*
566 		 * byte 4:  .   .   .   .  y11 y10 y9  y8
567 		 * byte 5: y7  y6  y5  y4  y3  y2  y1  y0
568 		 */
569 		y1 = etd->y_max - (((packet[4] & 0x0f) << 8) | packet[5]);
570 		break;
571 
572 	case 2:
573 		if (packet_type == PACKET_V3_HEAD) {
574 			/*
575 			 * byte 1:   .    .    .    .  ax11 ax10 ax9  ax8
576 			 * byte 2: ax7  ax6  ax5  ax4  ax3  ax2  ax1  ax0
577 			 */
578 			etd->mt[0].x = ((packet[1] & 0x0f) << 8) | packet[2];
579 			/*
580 			 * byte 4:   .    .    .    .  ay11 ay10 ay9  ay8
581 			 * byte 5: ay7  ay6  ay5  ay4  ay3  ay2  ay1  ay0
582 			 */
583 			etd->mt[0].y = etd->y_max -
584 				(((packet[4] & 0x0f) << 8) | packet[5]);
585 			/*
586 			 * wait for next packet
587 			 */
588 			return;
589 		}
590 
591 		/* packet_type == PACKET_V3_TAIL */
592 		x1 = etd->mt[0].x;
593 		y1 = etd->mt[0].y;
594 		x2 = ((packet[1] & 0x0f) << 8) | packet[2];
595 		y2 = etd->y_max - (((packet[4] & 0x0f) << 8) | packet[5]);
596 		break;
597 	}
598 
599 	pres = (packet[1] & 0xf0) | ((packet[4] & 0xf0) >> 4);
600 	width = ((packet[0] & 0x30) >> 2) | ((packet[3] & 0x30) >> 4);
601 
602 	input_report_key(dev, BTN_TOUCH, fingers != 0);
603 	if (fingers != 0) {
604 		input_report_abs(dev, ABS_X, x1);
605 		input_report_abs(dev, ABS_Y, y1);
606 	}
607 	elantech_report_semi_mt_data(dev, fingers, x1, y1, x2, y2);
608 	input_report_key(dev, BTN_TOOL_FINGER, fingers == 1);
609 	input_report_key(dev, BTN_TOOL_DOUBLETAP, fingers == 2);
610 	input_report_key(dev, BTN_TOOL_TRIPLETAP, fingers == 3);
611 
612 	/* For clickpads map both buttons to BTN_LEFT */
613 	if (elantech_is_buttonpad(&etd->info))
614 		input_report_key(dev, BTN_LEFT, packet[0] & 0x03);
615 	else
616 		psmouse_report_standard_buttons(dev, packet[0]);
617 
618 	input_report_abs(dev, ABS_PRESSURE, pres);
619 	input_report_abs(dev, ABS_TOOL_WIDTH, width);
620 
621 	input_sync(dev);
622 }
623 
624 static void elantech_input_sync_v4(struct psmouse *psmouse)
625 {
626 	struct input_dev *dev = psmouse->dev;
627 	struct elantech_data *etd = psmouse->private;
628 	unsigned char *packet = psmouse->packet;
629 
630 	/* For clickpads map both buttons to BTN_LEFT */
631 	if (elantech_is_buttonpad(&etd->info))
632 		input_report_key(dev, BTN_LEFT, packet[0] & 0x03);
633 	else
634 		psmouse_report_standard_buttons(dev, packet[0]);
635 
636 	input_mt_report_pointer_emulation(dev, true);
637 	input_sync(dev);
638 }
639 
640 static void process_packet_status_v4(struct psmouse *psmouse)
641 {
642 	struct input_dev *dev = psmouse->dev;
643 	unsigned char *packet = psmouse->packet;
644 	unsigned fingers;
645 	int i;
646 
647 	/* notify finger state change */
648 	fingers = packet[1] & 0x1f;
649 	for (i = 0; i < ETP_MAX_FINGERS; i++) {
650 		if ((fingers & (1 << i)) == 0) {
651 			input_mt_slot(dev, i);
652 			input_mt_report_slot_state(dev, MT_TOOL_FINGER, false);
653 		}
654 	}
655 
656 	elantech_input_sync_v4(psmouse);
657 }
658 
659 static void process_packet_head_v4(struct psmouse *psmouse)
660 {
661 	struct input_dev *dev = psmouse->dev;
662 	struct elantech_data *etd = psmouse->private;
663 	unsigned char *packet = psmouse->packet;
664 	int id = ((packet[3] & 0xe0) >> 5) - 1;
665 	int pres, traces;
666 
667 	if (id < 0)
668 		return;
669 
670 	etd->mt[id].x = ((packet[1] & 0x0f) << 8) | packet[2];
671 	etd->mt[id].y = etd->y_max - (((packet[4] & 0x0f) << 8) | packet[5]);
672 	pres = (packet[1] & 0xf0) | ((packet[4] & 0xf0) >> 4);
673 	traces = (packet[0] & 0xf0) >> 4;
674 
675 	input_mt_slot(dev, id);
676 	input_mt_report_slot_state(dev, MT_TOOL_FINGER, true);
677 
678 	input_report_abs(dev, ABS_MT_POSITION_X, etd->mt[id].x);
679 	input_report_abs(dev, ABS_MT_POSITION_Y, etd->mt[id].y);
680 	input_report_abs(dev, ABS_MT_PRESSURE, pres);
681 	input_report_abs(dev, ABS_MT_TOUCH_MAJOR, traces * etd->width);
682 	/* report this for backwards compatibility */
683 	input_report_abs(dev, ABS_TOOL_WIDTH, traces);
684 
685 	elantech_input_sync_v4(psmouse);
686 }
687 
688 static void process_packet_motion_v4(struct psmouse *psmouse)
689 {
690 	struct input_dev *dev = psmouse->dev;
691 	struct elantech_data *etd = psmouse->private;
692 	unsigned char *packet = psmouse->packet;
693 	int weight, delta_x1 = 0, delta_y1 = 0, delta_x2 = 0, delta_y2 = 0;
694 	int id, sid;
695 
696 	id = ((packet[0] & 0xe0) >> 5) - 1;
697 	if (id < 0)
698 		return;
699 
700 	sid = ((packet[3] & 0xe0) >> 5) - 1;
701 	weight = (packet[0] & 0x10) ? ETP_WEIGHT_VALUE : 1;
702 	/*
703 	 * Motion packets give us the delta of x, y values of specific fingers,
704 	 * but in two's complement. Let the compiler do the conversion for us.
705 	 * Also _enlarge_ the numbers to int, in case of overflow.
706 	 */
707 	delta_x1 = (signed char)packet[1];
708 	delta_y1 = (signed char)packet[2];
709 	delta_x2 = (signed char)packet[4];
710 	delta_y2 = (signed char)packet[5];
711 
712 	etd->mt[id].x += delta_x1 * weight;
713 	etd->mt[id].y -= delta_y1 * weight;
714 	input_mt_slot(dev, id);
715 	input_report_abs(dev, ABS_MT_POSITION_X, etd->mt[id].x);
716 	input_report_abs(dev, ABS_MT_POSITION_Y, etd->mt[id].y);
717 
718 	if (sid >= 0) {
719 		etd->mt[sid].x += delta_x2 * weight;
720 		etd->mt[sid].y -= delta_y2 * weight;
721 		input_mt_slot(dev, sid);
722 		input_report_abs(dev, ABS_MT_POSITION_X, etd->mt[sid].x);
723 		input_report_abs(dev, ABS_MT_POSITION_Y, etd->mt[sid].y);
724 	}
725 
726 	elantech_input_sync_v4(psmouse);
727 }
728 
729 static void elantech_report_absolute_v4(struct psmouse *psmouse,
730 					int packet_type)
731 {
732 	switch (packet_type) {
733 	case PACKET_V4_STATUS:
734 		process_packet_status_v4(psmouse);
735 		break;
736 
737 	case PACKET_V4_HEAD:
738 		process_packet_head_v4(psmouse);
739 		break;
740 
741 	case PACKET_V4_MOTION:
742 		process_packet_motion_v4(psmouse);
743 		break;
744 
745 	case PACKET_UNKNOWN:
746 	default:
747 		/* impossible to get here */
748 		break;
749 	}
750 }
751 
752 static int elantech_packet_check_v1(struct psmouse *psmouse)
753 {
754 	struct elantech_data *etd = psmouse->private;
755 	unsigned char *packet = psmouse->packet;
756 	unsigned char p1, p2, p3;
757 
758 	/* Parity bits are placed differently */
759 	if (etd->info.fw_version < 0x020000) {
760 		/* byte 0:  D   U  p1  p2   1  p3   R   L */
761 		p1 = (packet[0] & 0x20) >> 5;
762 		p2 = (packet[0] & 0x10) >> 4;
763 	} else {
764 		/* byte 0: n1  n0  p2  p1   1  p3   R   L */
765 		p1 = (packet[0] & 0x10) >> 4;
766 		p2 = (packet[0] & 0x20) >> 5;
767 	}
768 
769 	p3 = (packet[0] & 0x04) >> 2;
770 
771 	return etd->parity[packet[1]] == p1 &&
772 	       etd->parity[packet[2]] == p2 &&
773 	       etd->parity[packet[3]] == p3;
774 }
775 
776 static int elantech_debounce_check_v2(struct psmouse *psmouse)
777 {
778         /*
779          * When we encounter packet that matches this exactly, it means the
780          * hardware is in debounce status. Just ignore the whole packet.
781          */
782 	static const u8 debounce_packet[] = {
783 		0x84, 0xff, 0xff, 0x02, 0xff, 0xff
784 	};
785         unsigned char *packet = psmouse->packet;
786 
787         return !memcmp(packet, debounce_packet, sizeof(debounce_packet));
788 }
789 
790 static int elantech_packet_check_v2(struct psmouse *psmouse)
791 {
792 	struct elantech_data *etd = psmouse->private;
793 	unsigned char *packet = psmouse->packet;
794 
795 	/*
796 	 * V2 hardware has two flavors. Older ones that do not report pressure,
797 	 * and newer ones that reports pressure and width. With newer ones, all
798 	 * packets (1, 2, 3 finger touch) have the same constant bits. With
799 	 * older ones, 1/3 finger touch packets and 2 finger touch packets
800 	 * have different constant bits.
801 	 * With all three cases, if the constant bits are not exactly what I
802 	 * expected, I consider them invalid.
803 	 */
804 	if (etd->info.reports_pressure)
805 		return (packet[0] & 0x0c) == 0x04 &&
806 		       (packet[3] & 0x0f) == 0x02;
807 
808 	if ((packet[0] & 0xc0) == 0x80)
809 		return (packet[0] & 0x0c) == 0x0c &&
810 		       (packet[3] & 0x0e) == 0x08;
811 
812 	return (packet[0] & 0x3c) == 0x3c &&
813 	       (packet[1] & 0xf0) == 0x00 &&
814 	       (packet[3] & 0x3e) == 0x38 &&
815 	       (packet[4] & 0xf0) == 0x00;
816 }
817 
818 /*
819  * We check the constant bits to determine what packet type we get,
820  * so packet checking is mandatory for v3 and later hardware.
821  */
822 static int elantech_packet_check_v3(struct psmouse *psmouse)
823 {
824 	struct elantech_data *etd = psmouse->private;
825 	static const u8 debounce_packet[] = {
826 		0xc4, 0xff, 0xff, 0x02, 0xff, 0xff
827 	};
828 	unsigned char *packet = psmouse->packet;
829 
830 	/*
831 	 * check debounce first, it has the same signature in byte 0
832 	 * and byte 3 as PACKET_V3_HEAD.
833 	 */
834 	if (!memcmp(packet, debounce_packet, sizeof(debounce_packet)))
835 		return PACKET_DEBOUNCE;
836 
837 	/*
838 	 * If the hardware flag 'crc_enabled' is set the packets have
839 	 * different signatures.
840 	 */
841 	if (etd->info.crc_enabled) {
842 		if ((packet[3] & 0x09) == 0x08)
843 			return PACKET_V3_HEAD;
844 
845 		if ((packet[3] & 0x09) == 0x09)
846 			return PACKET_V3_TAIL;
847 	} else {
848 		if ((packet[0] & 0x0c) == 0x04 && (packet[3] & 0xcf) == 0x02)
849 			return PACKET_V3_HEAD;
850 
851 		if ((packet[0] & 0x0c) == 0x0c && (packet[3] & 0xce) == 0x0c)
852 			return PACKET_V3_TAIL;
853 		if ((packet[3] & 0x0f) == 0x06)
854 			return PACKET_TRACKPOINT;
855 	}
856 
857 	return PACKET_UNKNOWN;
858 }
859 
860 static int elantech_packet_check_v4(struct psmouse *psmouse)
861 {
862 	struct elantech_data *etd = psmouse->private;
863 	unsigned char *packet = psmouse->packet;
864 	unsigned char packet_type = packet[3] & 0x03;
865 	unsigned int ic_version;
866 	bool sanity_check;
867 
868 	if (etd->tp_dev && (packet[3] & 0x0f) == 0x06)
869 		return PACKET_TRACKPOINT;
870 
871 	/* This represents the version of IC body. */
872 	ic_version = (etd->info.fw_version & 0x0f0000) >> 16;
873 
874 	/*
875 	 * Sanity check based on the constant bits of a packet.
876 	 * The constant bits change depending on the value of
877 	 * the hardware flag 'crc_enabled' and the version of
878 	 * the IC body, but are the same for every packet,
879 	 * regardless of the type.
880 	 */
881 	if (etd->info.crc_enabled)
882 		sanity_check = ((packet[3] & 0x08) == 0x00);
883 	else if (ic_version == 7 && etd->info.samples[1] == 0x2A)
884 		sanity_check = ((packet[3] & 0x1c) == 0x10);
885 	else
886 		sanity_check = ((packet[0] & 0x08) == 0x00 &&
887 				(packet[3] & 0x1c) == 0x10);
888 
889 	if (!sanity_check)
890 		return PACKET_UNKNOWN;
891 
892 	switch (packet_type) {
893 	case 0:
894 		return PACKET_V4_STATUS;
895 
896 	case 1:
897 		return PACKET_V4_HEAD;
898 
899 	case 2:
900 		return PACKET_V4_MOTION;
901 	}
902 
903 	return PACKET_UNKNOWN;
904 }
905 
906 /*
907  * Process byte stream from mouse and handle complete packets
908  */
909 static psmouse_ret_t elantech_process_byte(struct psmouse *psmouse)
910 {
911 	struct elantech_data *etd = psmouse->private;
912 	int packet_type;
913 
914 	if (psmouse->pktcnt < psmouse->pktsize)
915 		return PSMOUSE_GOOD_DATA;
916 
917 	if (etd->info.debug > 1)
918 		elantech_packet_dump(psmouse);
919 
920 	switch (etd->info.hw_version) {
921 	case 1:
922 		if (etd->info.paritycheck && !elantech_packet_check_v1(psmouse))
923 			return PSMOUSE_BAD_DATA;
924 
925 		elantech_report_absolute_v1(psmouse);
926 		break;
927 
928 	case 2:
929 		/* ignore debounce */
930 		if (elantech_debounce_check_v2(psmouse))
931 			return PSMOUSE_FULL_PACKET;
932 
933 		if (etd->info.paritycheck && !elantech_packet_check_v2(psmouse))
934 			return PSMOUSE_BAD_DATA;
935 
936 		elantech_report_absolute_v2(psmouse);
937 		break;
938 
939 	case 3:
940 		packet_type = elantech_packet_check_v3(psmouse);
941 		switch (packet_type) {
942 		case PACKET_UNKNOWN:
943 			return PSMOUSE_BAD_DATA;
944 
945 		case PACKET_DEBOUNCE:
946 			/* ignore debounce */
947 			break;
948 
949 		case PACKET_TRACKPOINT:
950 			elantech_report_trackpoint(psmouse, packet_type);
951 			break;
952 
953 		default:
954 			elantech_report_absolute_v3(psmouse, packet_type);
955 			break;
956 		}
957 
958 		break;
959 
960 	case 4:
961 		packet_type = elantech_packet_check_v4(psmouse);
962 		switch (packet_type) {
963 		case PACKET_UNKNOWN:
964 			return PSMOUSE_BAD_DATA;
965 
966 		case PACKET_TRACKPOINT:
967 			elantech_report_trackpoint(psmouse, packet_type);
968 			break;
969 
970 		default:
971 			elantech_report_absolute_v4(psmouse, packet_type);
972 			break;
973 		}
974 
975 		break;
976 	}
977 
978 	return PSMOUSE_FULL_PACKET;
979 }
980 
981 /*
982  * This writes the reg_07 value again to the hardware at the end of every
983  * set_rate call because the register loses its value. reg_07 allows setting
984  * absolute mode on v4 hardware
985  */
986 static void elantech_set_rate_restore_reg_07(struct psmouse *psmouse,
987 		unsigned int rate)
988 {
989 	struct elantech_data *etd = psmouse->private;
990 
991 	etd->original_set_rate(psmouse, rate);
992 	if (elantech_write_reg(psmouse, 0x07, etd->reg_07))
993 		psmouse_err(psmouse, "restoring reg_07 failed\n");
994 }
995 
996 /*
997  * Put the touchpad into absolute mode
998  */
999 static int elantech_set_absolute_mode(struct psmouse *psmouse)
1000 {
1001 	struct elantech_data *etd = psmouse->private;
1002 	unsigned char val;
1003 	int tries = ETP_READ_BACK_TRIES;
1004 	int rc = 0;
1005 
1006 	switch (etd->info.hw_version) {
1007 	case 1:
1008 		etd->reg_10 = 0x16;
1009 		etd->reg_11 = 0x8f;
1010 		if (elantech_write_reg(psmouse, 0x10, etd->reg_10) ||
1011 		    elantech_write_reg(psmouse, 0x11, etd->reg_11)) {
1012 			rc = -1;
1013 		}
1014 		break;
1015 
1016 	case 2:
1017 					/* Windows driver values */
1018 		etd->reg_10 = 0x54;
1019 		etd->reg_11 = 0x88;	/* 0x8a */
1020 		etd->reg_21 = 0x60;	/* 0x00 */
1021 		if (elantech_write_reg(psmouse, 0x10, etd->reg_10) ||
1022 		    elantech_write_reg(psmouse, 0x11, etd->reg_11) ||
1023 		    elantech_write_reg(psmouse, 0x21, etd->reg_21)) {
1024 			rc = -1;
1025 		}
1026 		break;
1027 
1028 	case 3:
1029 		if (etd->info.set_hw_resolution)
1030 			etd->reg_10 = 0x0b;
1031 		else
1032 			etd->reg_10 = 0x01;
1033 
1034 		if (elantech_write_reg(psmouse, 0x10, etd->reg_10))
1035 			rc = -1;
1036 
1037 		break;
1038 
1039 	case 4:
1040 		etd->reg_07 = 0x01;
1041 		if (elantech_write_reg(psmouse, 0x07, etd->reg_07))
1042 			rc = -1;
1043 
1044 		goto skip_readback_reg_10; /* v4 has no reg 0x10 to read */
1045 	}
1046 
1047 	if (rc == 0) {
1048 		/*
1049 		 * Read back reg 0x10. For hardware version 1 we must make
1050 		 * sure the absolute mode bit is set. For hardware version 2
1051 		 * the touchpad is probably initializing and not ready until
1052 		 * we read back the value we just wrote.
1053 		 */
1054 		do {
1055 			rc = elantech_read_reg(psmouse, 0x10, &val);
1056 			if (rc == 0)
1057 				break;
1058 			tries--;
1059 			elantech_debug("retrying read (%d).\n", tries);
1060 			msleep(ETP_READ_BACK_DELAY);
1061 		} while (tries > 0);
1062 
1063 		if (rc) {
1064 			psmouse_err(psmouse,
1065 				    "failed to read back register 0x10.\n");
1066 		} else if (etd->info.hw_version == 1 &&
1067 			   !(val & ETP_R10_ABSOLUTE_MODE)) {
1068 			psmouse_err(psmouse,
1069 				    "touchpad refuses to switch to absolute mode.\n");
1070 			rc = -1;
1071 		}
1072 	}
1073 
1074  skip_readback_reg_10:
1075 	if (rc)
1076 		psmouse_err(psmouse, "failed to initialise registers.\n");
1077 
1078 	return rc;
1079 }
1080 
1081 /*
1082  * (value from firmware) * 10 + 790 = dpi
1083  * we also have to convert dpi to dots/mm (*10/254 to avoid floating point)
1084  */
1085 static unsigned int elantech_convert_res(unsigned int val)
1086 {
1087 	return (val * 10 + 790) * 10 / 254;
1088 }
1089 
1090 static int elantech_get_resolution_v4(struct psmouse *psmouse,
1091 				      unsigned int *x_res,
1092 				      unsigned int *y_res,
1093 				      unsigned int *bus)
1094 {
1095 	unsigned char param[3];
1096 
1097 	if (elantech_send_cmd(psmouse, ETP_RESOLUTION_QUERY, param))
1098 		return -1;
1099 
1100 	*x_res = elantech_convert_res(param[1] & 0x0f);
1101 	*y_res = elantech_convert_res((param[1] & 0xf0) >> 4);
1102 	*bus = param[2];
1103 
1104 	return 0;
1105 }
1106 
1107 static void elantech_set_buttonpad_prop(struct psmouse *psmouse)
1108 {
1109 	struct input_dev *dev = psmouse->dev;
1110 	struct elantech_data *etd = psmouse->private;
1111 
1112 	if (elantech_is_buttonpad(&etd->info)) {
1113 		__set_bit(INPUT_PROP_BUTTONPAD, dev->propbit);
1114 		__clear_bit(BTN_RIGHT, dev->keybit);
1115 	}
1116 }
1117 
1118 /*
1119  * Some hw_version 4 models do have a middle button
1120  */
1121 static const struct dmi_system_id elantech_dmi_has_middle_button[] = {
1122 #if defined(CONFIG_DMI) && defined(CONFIG_X86)
1123 	{
1124 		/* Fujitsu H730 has a middle button */
1125 		.matches = {
1126 			DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU"),
1127 			DMI_MATCH(DMI_PRODUCT_NAME, "CELSIUS H730"),
1128 		},
1129 	},
1130 	{
1131 		/* Fujitsu H760 also has a middle button */
1132 		.matches = {
1133 			DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU"),
1134 			DMI_MATCH(DMI_PRODUCT_NAME, "CELSIUS H760"),
1135 		},
1136 	},
1137 	{
1138 		/* Fujitsu H780 also has a middle button */
1139 		.matches = {
1140 			DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU"),
1141 			DMI_MATCH(DMI_PRODUCT_NAME, "CELSIUS H780"),
1142 		},
1143 	},
1144 #endif
1145 	{ }
1146 };
1147 
1148 /*
1149  * Set the appropriate event bits for the input subsystem
1150  */
1151 static int elantech_set_input_params(struct psmouse *psmouse)
1152 {
1153 	struct input_dev *dev = psmouse->dev;
1154 	struct elantech_data *etd = psmouse->private;
1155 	struct elantech_device_info *info = &etd->info;
1156 	unsigned int x_min = info->x_min, y_min = info->y_min,
1157 		     x_max = info->x_max, y_max = info->y_max,
1158 		     width = info->width;
1159 
1160 	__set_bit(INPUT_PROP_POINTER, dev->propbit);
1161 	__set_bit(EV_KEY, dev->evbit);
1162 	__set_bit(EV_ABS, dev->evbit);
1163 	__clear_bit(EV_REL, dev->evbit);
1164 
1165 	__set_bit(BTN_LEFT, dev->keybit);
1166 	if (info->has_middle_button)
1167 		__set_bit(BTN_MIDDLE, dev->keybit);
1168 	__set_bit(BTN_RIGHT, dev->keybit);
1169 
1170 	__set_bit(BTN_TOUCH, dev->keybit);
1171 	__set_bit(BTN_TOOL_FINGER, dev->keybit);
1172 	__set_bit(BTN_TOOL_DOUBLETAP, dev->keybit);
1173 	__set_bit(BTN_TOOL_TRIPLETAP, dev->keybit);
1174 
1175 	switch (info->hw_version) {
1176 	case 1:
1177 		/* Rocker button */
1178 		if (info->fw_version < 0x020000 &&
1179 		    (info->capabilities[0] & ETP_CAP_HAS_ROCKER)) {
1180 			__set_bit(BTN_FORWARD, dev->keybit);
1181 			__set_bit(BTN_BACK, dev->keybit);
1182 		}
1183 		input_set_abs_params(dev, ABS_X, x_min, x_max, 0, 0);
1184 		input_set_abs_params(dev, ABS_Y, y_min, y_max, 0, 0);
1185 		break;
1186 
1187 	case 2:
1188 		__set_bit(BTN_TOOL_QUADTAP, dev->keybit);
1189 		__set_bit(INPUT_PROP_SEMI_MT, dev->propbit);
1190 		fallthrough;
1191 	case 3:
1192 		if (info->hw_version == 3)
1193 			elantech_set_buttonpad_prop(psmouse);
1194 		input_set_abs_params(dev, ABS_X, x_min, x_max, 0, 0);
1195 		input_set_abs_params(dev, ABS_Y, y_min, y_max, 0, 0);
1196 		if (info->reports_pressure) {
1197 			input_set_abs_params(dev, ABS_PRESSURE, ETP_PMIN_V2,
1198 					     ETP_PMAX_V2, 0, 0);
1199 			input_set_abs_params(dev, ABS_TOOL_WIDTH, ETP_WMIN_V2,
1200 					     ETP_WMAX_V2, 0, 0);
1201 		}
1202 		input_mt_init_slots(dev, 2, INPUT_MT_SEMI_MT);
1203 		input_set_abs_params(dev, ABS_MT_POSITION_X, x_min, x_max, 0, 0);
1204 		input_set_abs_params(dev, ABS_MT_POSITION_Y, y_min, y_max, 0, 0);
1205 		break;
1206 
1207 	case 4:
1208 		elantech_set_buttonpad_prop(psmouse);
1209 		__set_bit(BTN_TOOL_QUADTAP, dev->keybit);
1210 		/* For X to recognize me as touchpad. */
1211 		input_set_abs_params(dev, ABS_X, x_min, x_max, 0, 0);
1212 		input_set_abs_params(dev, ABS_Y, y_min, y_max, 0, 0);
1213 		/*
1214 		 * range of pressure and width is the same as v2,
1215 		 * report ABS_PRESSURE, ABS_TOOL_WIDTH for compatibility.
1216 		 */
1217 		input_set_abs_params(dev, ABS_PRESSURE, ETP_PMIN_V2,
1218 				     ETP_PMAX_V2, 0, 0);
1219 		input_set_abs_params(dev, ABS_TOOL_WIDTH, ETP_WMIN_V2,
1220 				     ETP_WMAX_V2, 0, 0);
1221 		/* Multitouch capable pad, up to 5 fingers. */
1222 		input_mt_init_slots(dev, ETP_MAX_FINGERS, 0);
1223 		input_set_abs_params(dev, ABS_MT_POSITION_X, x_min, x_max, 0, 0);
1224 		input_set_abs_params(dev, ABS_MT_POSITION_Y, y_min, y_max, 0, 0);
1225 		input_set_abs_params(dev, ABS_MT_PRESSURE, ETP_PMIN_V2,
1226 				     ETP_PMAX_V2, 0, 0);
1227 		/*
1228 		 * The firmware reports how many trace lines the finger spans,
1229 		 * convert to surface unit as Protocol-B requires.
1230 		 */
1231 		input_set_abs_params(dev, ABS_MT_TOUCH_MAJOR, 0,
1232 				     ETP_WMAX_V2 * width, 0, 0);
1233 		break;
1234 	}
1235 
1236 	input_abs_set_res(dev, ABS_X, info->x_res);
1237 	input_abs_set_res(dev, ABS_Y, info->y_res);
1238 	if (info->hw_version > 1) {
1239 		input_abs_set_res(dev, ABS_MT_POSITION_X, info->x_res);
1240 		input_abs_set_res(dev, ABS_MT_POSITION_Y, info->y_res);
1241 	}
1242 
1243 	etd->y_max = y_max;
1244 	etd->width = width;
1245 
1246 	return 0;
1247 }
1248 
1249 struct elantech_attr_data {
1250 	size_t		field_offset;
1251 	unsigned char	reg;
1252 };
1253 
1254 /*
1255  * Display a register value by reading a sysfs entry
1256  */
1257 static ssize_t elantech_show_int_attr(struct psmouse *psmouse, void *data,
1258 					char *buf)
1259 {
1260 	struct elantech_data *etd = psmouse->private;
1261 	struct elantech_attr_data *attr = data;
1262 	unsigned char *reg = (unsigned char *) etd + attr->field_offset;
1263 	int rc = 0;
1264 
1265 	if (attr->reg)
1266 		rc = elantech_read_reg(psmouse, attr->reg, reg);
1267 
1268 	return sprintf(buf, "0x%02x\n", (attr->reg && rc) ? -1 : *reg);
1269 }
1270 
1271 /*
1272  * Write a register value by writing a sysfs entry
1273  */
1274 static ssize_t elantech_set_int_attr(struct psmouse *psmouse,
1275 				     void *data, const char *buf, size_t count)
1276 {
1277 	struct elantech_data *etd = psmouse->private;
1278 	struct elantech_attr_data *attr = data;
1279 	unsigned char *reg = (unsigned char *) etd + attr->field_offset;
1280 	unsigned char value;
1281 	int err;
1282 
1283 	err = kstrtou8(buf, 16, &value);
1284 	if (err)
1285 		return err;
1286 
1287 	/* Do we need to preserve some bits for version 2 hardware too? */
1288 	if (etd->info.hw_version == 1) {
1289 		if (attr->reg == 0x10)
1290 			/* Force absolute mode always on */
1291 			value |= ETP_R10_ABSOLUTE_MODE;
1292 		else if (attr->reg == 0x11)
1293 			/* Force 4 byte mode always on */
1294 			value |= ETP_R11_4_BYTE_MODE;
1295 	}
1296 
1297 	if (!attr->reg || elantech_write_reg(psmouse, attr->reg, value) == 0)
1298 		*reg = value;
1299 
1300 	return count;
1301 }
1302 
1303 #define ELANTECH_INT_ATTR(_name, _register)				\
1304 	static struct elantech_attr_data elantech_attr_##_name = {	\
1305 		.field_offset = offsetof(struct elantech_data, _name),	\
1306 		.reg = _register,					\
1307 	};								\
1308 	PSMOUSE_DEFINE_ATTR(_name, 0644,				\
1309 			    &elantech_attr_##_name,			\
1310 			    elantech_show_int_attr,			\
1311 			    elantech_set_int_attr)
1312 
1313 #define ELANTECH_INFO_ATTR(_name)					       \
1314 	static struct elantech_attr_data elantech_attr_##_name = {	       \
1315 		.field_offset = offsetof(struct elantech_data, info) +	       \
1316 				offsetof(struct elantech_device_info, _name),  \
1317 		.reg = 0,						       \
1318 	};								       \
1319 	PSMOUSE_DEFINE_ATTR(_name, 0644,				       \
1320 			    &elantech_attr_##_name,			       \
1321 			    elantech_show_int_attr,			       \
1322 			    elantech_set_int_attr)
1323 
1324 ELANTECH_INT_ATTR(reg_07, 0x07);
1325 ELANTECH_INT_ATTR(reg_10, 0x10);
1326 ELANTECH_INT_ATTR(reg_11, 0x11);
1327 ELANTECH_INT_ATTR(reg_20, 0x20);
1328 ELANTECH_INT_ATTR(reg_21, 0x21);
1329 ELANTECH_INT_ATTR(reg_22, 0x22);
1330 ELANTECH_INT_ATTR(reg_23, 0x23);
1331 ELANTECH_INT_ATTR(reg_24, 0x24);
1332 ELANTECH_INT_ATTR(reg_25, 0x25);
1333 ELANTECH_INT_ATTR(reg_26, 0x26);
1334 ELANTECH_INFO_ATTR(debug);
1335 ELANTECH_INFO_ATTR(paritycheck);
1336 ELANTECH_INFO_ATTR(crc_enabled);
1337 
1338 static struct attribute *elantech_attrs[] = {
1339 	&psmouse_attr_reg_07.dattr.attr,
1340 	&psmouse_attr_reg_10.dattr.attr,
1341 	&psmouse_attr_reg_11.dattr.attr,
1342 	&psmouse_attr_reg_20.dattr.attr,
1343 	&psmouse_attr_reg_21.dattr.attr,
1344 	&psmouse_attr_reg_22.dattr.attr,
1345 	&psmouse_attr_reg_23.dattr.attr,
1346 	&psmouse_attr_reg_24.dattr.attr,
1347 	&psmouse_attr_reg_25.dattr.attr,
1348 	&psmouse_attr_reg_26.dattr.attr,
1349 	&psmouse_attr_debug.dattr.attr,
1350 	&psmouse_attr_paritycheck.dattr.attr,
1351 	&psmouse_attr_crc_enabled.dattr.attr,
1352 	NULL
1353 };
1354 
1355 static const struct attribute_group elantech_attr_group = {
1356 	.attrs = elantech_attrs,
1357 };
1358 
1359 static bool elantech_is_signature_valid(const unsigned char *param)
1360 {
1361 	static const unsigned char rates[] = { 200, 100, 80, 60, 40, 20, 10 };
1362 	int i;
1363 
1364 	if (param[0] == 0)
1365 		return false;
1366 
1367 	if (param[1] == 0)
1368 		return true;
1369 
1370 	/*
1371 	 * Some hw_version >= 4 models have a revision higher then 20. Meaning
1372 	 * that param[2] may be 10 or 20, skip the rates check for these.
1373 	 */
1374 	if ((param[0] & 0x0f) >= 0x06 && (param[1] & 0xaf) == 0x0f &&
1375 	    param[2] < 40)
1376 		return true;
1377 
1378 	for (i = 0; i < ARRAY_SIZE(rates); i++)
1379 		if (param[2] == rates[i])
1380 			return false;
1381 
1382 	return true;
1383 }
1384 
1385 /*
1386  * Use magic knock to detect Elantech touchpad
1387  */
1388 int elantech_detect(struct psmouse *psmouse, bool set_properties)
1389 {
1390 	struct ps2dev *ps2dev = &psmouse->ps2dev;
1391 	unsigned char param[3];
1392 
1393 	ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
1394 
1395 	if (ps2_command(ps2dev,  NULL, PSMOUSE_CMD_DISABLE) ||
1396 	    ps2_command(ps2dev,  NULL, PSMOUSE_CMD_SETSCALE11) ||
1397 	    ps2_command(ps2dev,  NULL, PSMOUSE_CMD_SETSCALE11) ||
1398 	    ps2_command(ps2dev,  NULL, PSMOUSE_CMD_SETSCALE11) ||
1399 	    ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO)) {
1400 		psmouse_dbg(psmouse, "sending Elantech magic knock failed.\n");
1401 		return -1;
1402 	}
1403 
1404 	/*
1405 	 * Report this in case there are Elantech models that use a different
1406 	 * set of magic numbers
1407 	 */
1408 	if (param[0] != 0x3c || param[1] != 0x03 ||
1409 	    (param[2] != 0xc8 && param[2] != 0x00)) {
1410 		psmouse_dbg(psmouse,
1411 			    "unexpected magic knock result 0x%02x, 0x%02x, 0x%02x.\n",
1412 			    param[0], param[1], param[2]);
1413 		return -1;
1414 	}
1415 
1416 	/*
1417 	 * Query touchpad's firmware version and see if it reports known
1418 	 * value to avoid mis-detection. Logitech mice are known to respond
1419 	 * to Elantech magic knock and there might be more.
1420 	 */
1421 	if (synaptics_send_cmd(psmouse, ETP_FW_VERSION_QUERY, param)) {
1422 		psmouse_dbg(psmouse, "failed to query firmware version.\n");
1423 		return -1;
1424 	}
1425 
1426 	psmouse_dbg(psmouse,
1427 		    "Elantech version query result 0x%02x, 0x%02x, 0x%02x.\n",
1428 		    param[0], param[1], param[2]);
1429 
1430 	if (!elantech_is_signature_valid(param)) {
1431 		psmouse_dbg(psmouse,
1432 			    "Probably not a real Elantech touchpad. Aborting.\n");
1433 		return -1;
1434 	}
1435 
1436 	if (set_properties) {
1437 		psmouse->vendor = "Elantech";
1438 		psmouse->name = "Touchpad";
1439 	}
1440 
1441 	return 0;
1442 }
1443 
1444 /*
1445  * Clean up sysfs entries when disconnecting
1446  */
1447 static void elantech_disconnect(struct psmouse *psmouse)
1448 {
1449 	struct elantech_data *etd = psmouse->private;
1450 
1451 	/*
1452 	 * We might have left a breadcrumb when trying to
1453 	 * set up SMbus companion.
1454 	 */
1455 	psmouse_smbus_cleanup(psmouse);
1456 
1457 	if (etd->tp_dev)
1458 		input_unregister_device(etd->tp_dev);
1459 	sysfs_remove_group(&psmouse->ps2dev.serio->dev.kobj,
1460 			   &elantech_attr_group);
1461 	kfree(psmouse->private);
1462 	psmouse->private = NULL;
1463 }
1464 
1465 /*
1466  * Put the touchpad back into absolute mode when reconnecting
1467  */
1468 static int elantech_reconnect(struct psmouse *psmouse)
1469 {
1470 	psmouse_reset(psmouse);
1471 
1472 	if (elantech_detect(psmouse, 0))
1473 		return -1;
1474 
1475 	if (elantech_set_absolute_mode(psmouse)) {
1476 		psmouse_err(psmouse,
1477 			    "failed to put touchpad back into absolute mode.\n");
1478 		return -1;
1479 	}
1480 
1481 	return 0;
1482 }
1483 
1484 /*
1485  * Some hw_version 4 models do not work with crc_disabled
1486  */
1487 static const struct dmi_system_id elantech_dmi_force_crc_enabled[] = {
1488 #if defined(CONFIG_DMI) && defined(CONFIG_X86)
1489 	{
1490 		/* Fujitsu H730 does not work with crc_enabled == 0 */
1491 		.matches = {
1492 			DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU"),
1493 			DMI_MATCH(DMI_PRODUCT_NAME, "CELSIUS H730"),
1494 		},
1495 	},
1496 	{
1497 		/* Fujitsu H760 does not work with crc_enabled == 0 */
1498 		.matches = {
1499 			DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU"),
1500 			DMI_MATCH(DMI_PRODUCT_NAME, "CELSIUS H760"),
1501 		},
1502 	},
1503 	{
1504 		/* Fujitsu LIFEBOOK E544  does not work with crc_enabled == 0 */
1505 		.matches = {
1506 			DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU"),
1507 			DMI_MATCH(DMI_PRODUCT_NAME, "LIFEBOOK E544"),
1508 		},
1509 	},
1510 	{
1511 		/* Fujitsu LIFEBOOK E546  does not work with crc_enabled == 0 */
1512 		.matches = {
1513 			DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU"),
1514 			DMI_MATCH(DMI_PRODUCT_NAME, "LIFEBOOK E546"),
1515 		},
1516 	},
1517 	{
1518 		/* Fujitsu LIFEBOOK E547 does not work with crc_enabled == 0 */
1519 		.matches = {
1520 			DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU"),
1521 			DMI_MATCH(DMI_PRODUCT_NAME, "LIFEBOOK E547"),
1522 		},
1523 	},
1524 	{
1525 		/* Fujitsu LIFEBOOK E554  does not work with crc_enabled == 0 */
1526 		.matches = {
1527 			DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU"),
1528 			DMI_MATCH(DMI_PRODUCT_NAME, "LIFEBOOK E554"),
1529 		},
1530 	},
1531 	{
1532 		/* Fujitsu LIFEBOOK E556 does not work with crc_enabled == 0 */
1533 		.matches = {
1534 			DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU"),
1535 			DMI_MATCH(DMI_PRODUCT_NAME, "LIFEBOOK E556"),
1536 		},
1537 	},
1538 	{
1539 		/* Fujitsu LIFEBOOK E557 does not work with crc_enabled == 0 */
1540 		.matches = {
1541 			DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU"),
1542 			DMI_MATCH(DMI_PRODUCT_NAME, "LIFEBOOK E557"),
1543 		},
1544 	},
1545 	{
1546 		/* Fujitsu LIFEBOOK U745 does not work with crc_enabled == 0 */
1547 		.matches = {
1548 			DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU"),
1549 			DMI_MATCH(DMI_PRODUCT_NAME, "LIFEBOOK U745"),
1550 		},
1551 	},
1552 #endif
1553 	{ }
1554 };
1555 
1556 /*
1557  * Some hw_version 3 models go into error state when we try to set
1558  * bit 3 and/or bit 1 of r10.
1559  */
1560 static const struct dmi_system_id no_hw_res_dmi_table[] = {
1561 #if defined(CONFIG_DMI) && defined(CONFIG_X86)
1562 	{
1563 		/* Gigabyte U2442 */
1564 		.matches = {
1565 			DMI_MATCH(DMI_SYS_VENDOR, "GIGABYTE"),
1566 			DMI_MATCH(DMI_PRODUCT_NAME, "U2442"),
1567 		},
1568 	},
1569 #endif
1570 	{ }
1571 };
1572 
1573 /*
1574  * Change Report id 0x5E to 0x5F.
1575  */
1576 static int elantech_change_report_id(struct psmouse *psmouse)
1577 {
1578 	unsigned char param[2] = { 0x10, 0x03 };
1579 
1580 	if (elantech_write_reg_params(psmouse, 0x7, param) ||
1581 	    elantech_read_reg_params(psmouse, 0x7, param) ||
1582 	    param[0] != 0x10 || param[1] != 0x03) {
1583 		psmouse_err(psmouse, "Unable to change report ID to 0x5f.\n");
1584 		return -EIO;
1585 	}
1586 
1587 	return 0;
1588 }
1589 /*
1590  * determine hardware version and set some properties according to it.
1591  */
1592 static int elantech_set_properties(struct elantech_device_info *info)
1593 {
1594 	/* This represents the version of IC body. */
1595 	info->ic_version = (info->fw_version & 0x0f0000) >> 16;
1596 
1597 	/* Early version of Elan touchpads doesn't obey the rule. */
1598 	if (info->fw_version < 0x020030 || info->fw_version == 0x020600)
1599 		info->hw_version = 1;
1600 	else {
1601 		switch (info->ic_version) {
1602 		case 2:
1603 		case 4:
1604 			info->hw_version = 2;
1605 			break;
1606 		case 5:
1607 			info->hw_version = 3;
1608 			break;
1609 		case 6 ... 15:
1610 			info->hw_version = 4;
1611 			break;
1612 		default:
1613 			return -1;
1614 		}
1615 	}
1616 
1617 	/* Get information pattern for hw_version 4 */
1618 	info->pattern = 0x00;
1619 	if (info->ic_version == 0x0f && (info->fw_version & 0xff) <= 0x02)
1620 		info->pattern = info->fw_version & 0xff;
1621 
1622 	/* decide which send_cmd we're gonna use early */
1623 	info->send_cmd = info->hw_version >= 3 ? elantech_send_cmd :
1624 						 synaptics_send_cmd;
1625 
1626 	/* Turn on packet checking by default */
1627 	info->paritycheck = 1;
1628 
1629 	/*
1630 	 * This firmware suffers from misreporting coordinates when
1631 	 * a touch action starts causing the mouse cursor or scrolled page
1632 	 * to jump. Enable a workaround.
1633 	 */
1634 	info->jumpy_cursor =
1635 		(info->fw_version == 0x020022 || info->fw_version == 0x020600);
1636 
1637 	if (info->hw_version > 1) {
1638 		/* For now show extra debug information */
1639 		info->debug = 1;
1640 
1641 		if (info->fw_version >= 0x020800)
1642 			info->reports_pressure = true;
1643 	}
1644 
1645 	/*
1646 	 * The signatures of v3 and v4 packets change depending on the
1647 	 * value of this hardware flag.
1648 	 */
1649 	info->crc_enabled = (info->fw_version & 0x4000) == 0x4000 ||
1650 			     dmi_check_system(elantech_dmi_force_crc_enabled);
1651 
1652 	/* Enable real hardware resolution on hw_version 3 ? */
1653 	info->set_hw_resolution = !dmi_check_system(no_hw_res_dmi_table);
1654 
1655 	return 0;
1656 }
1657 
1658 static int elantech_query_info(struct psmouse *psmouse,
1659 			       struct elantech_device_info *info)
1660 {
1661 	unsigned char param[3];
1662 	unsigned char traces;
1663 	unsigned char ic_body[3];
1664 
1665 	memset(info, 0, sizeof(*info));
1666 
1667 	/*
1668 	 * Do the version query again so we can store the result
1669 	 */
1670 	if (synaptics_send_cmd(psmouse, ETP_FW_VERSION_QUERY, param)) {
1671 		psmouse_err(psmouse, "failed to query firmware version.\n");
1672 		return -EINVAL;
1673 	}
1674 	info->fw_version = (param[0] << 16) | (param[1] << 8) | param[2];
1675 
1676 	if (elantech_set_properties(info)) {
1677 		psmouse_err(psmouse, "unknown hardware version, aborting...\n");
1678 		return -EINVAL;
1679 	}
1680 	psmouse_info(psmouse,
1681 		     "assuming hardware version %d (with firmware version 0x%02x%02x%02x)\n",
1682 		     info->hw_version, param[0], param[1], param[2]);
1683 
1684 	if (info->send_cmd(psmouse, ETP_CAPABILITIES_QUERY,
1685 	    info->capabilities)) {
1686 		psmouse_err(psmouse, "failed to query capabilities.\n");
1687 		return -EINVAL;
1688 	}
1689 	psmouse_info(psmouse,
1690 		     "Synaptics capabilities query result 0x%02x, 0x%02x, 0x%02x.\n",
1691 		     info->capabilities[0], info->capabilities[1],
1692 		     info->capabilities[2]);
1693 
1694 	if (info->hw_version != 1) {
1695 		if (info->send_cmd(psmouse, ETP_SAMPLE_QUERY, info->samples)) {
1696 			psmouse_err(psmouse, "failed to query sample data\n");
1697 			return -EINVAL;
1698 		}
1699 		psmouse_info(psmouse,
1700 			     "Elan sample query result %02x, %02x, %02x\n",
1701 			     info->samples[0],
1702 			     info->samples[1],
1703 			     info->samples[2]);
1704 	}
1705 
1706 	if (info->pattern > 0x00 && info->ic_version == 0xf) {
1707 		if (info->send_cmd(psmouse, ETP_ICBODY_QUERY, ic_body)) {
1708 			psmouse_err(psmouse, "failed to query ic body\n");
1709 			return -EINVAL;
1710 		}
1711 		info->ic_version = be16_to_cpup((__be16 *)ic_body);
1712 		psmouse_info(psmouse,
1713 			     "Elan ic body: %#04x, current fw version: %#02x\n",
1714 			     info->ic_version, ic_body[2]);
1715 	}
1716 
1717 	info->product_id = be16_to_cpup((__be16 *)info->samples);
1718 	if (info->pattern == 0x00)
1719 		info->product_id &= 0xff;
1720 
1721 	if (info->samples[1] == 0x74 && info->hw_version == 0x03) {
1722 		/*
1723 		 * This module has a bug which makes absolute mode
1724 		 * unusable, so let's abort so we'll be using standard
1725 		 * PS/2 protocol.
1726 		 */
1727 		psmouse_info(psmouse,
1728 			     "absolute mode broken, forcing standard PS/2 protocol\n");
1729 		return -ENODEV;
1730 	}
1731 
1732 	/* The MSB indicates the presence of the trackpoint */
1733 	info->has_trackpoint = (info->capabilities[0] & 0x80) == 0x80;
1734 
1735 	if (info->has_trackpoint && info->ic_version == 0x0011 &&
1736 	    (info->product_id == 0x08 || info->product_id == 0x09 ||
1737 	     info->product_id == 0x0d || info->product_id == 0x0e)) {
1738 		/*
1739 		 * This module has a bug which makes trackpoint in SMBus
1740 		 * mode return invalid data unless trackpoint is switched
1741 		 * from using 0x5e reports to 0x5f. If we are not able to
1742 		 * make the switch, let's abort initialization so we'll be
1743 		 * using standard PS/2 protocol.
1744 		 */
1745 		if (elantech_change_report_id(psmouse)) {
1746 			psmouse_info(psmouse,
1747 				     "Trackpoint report is broken, forcing standard PS/2 protocol\n");
1748 			return -ENODEV;
1749 		}
1750 	}
1751 
1752 	info->x_res = 31;
1753 	info->y_res = 31;
1754 	if (info->hw_version == 4) {
1755 		if (elantech_get_resolution_v4(psmouse,
1756 					       &info->x_res,
1757 					       &info->y_res,
1758 					       &info->bus)) {
1759 			psmouse_warn(psmouse,
1760 				     "failed to query resolution data.\n");
1761 		}
1762 	}
1763 
1764 	/* query range information */
1765 	switch (info->hw_version) {
1766 	case 1:
1767 		info->x_min = ETP_XMIN_V1;
1768 		info->y_min = ETP_YMIN_V1;
1769 		info->x_max = ETP_XMAX_V1;
1770 		info->y_max = ETP_YMAX_V1;
1771 		break;
1772 
1773 	case 2:
1774 		if (info->fw_version == 0x020800 ||
1775 		    info->fw_version == 0x020b00 ||
1776 		    info->fw_version == 0x020030) {
1777 			info->x_min = ETP_XMIN_V2;
1778 			info->y_min = ETP_YMIN_V2;
1779 			info->x_max = ETP_XMAX_V2;
1780 			info->y_max = ETP_YMAX_V2;
1781 		} else {
1782 			int i;
1783 			int fixed_dpi;
1784 
1785 			i = (info->fw_version > 0x020800 &&
1786 			     info->fw_version < 0x020900) ? 1 : 2;
1787 
1788 			if (info->send_cmd(psmouse, ETP_FW_ID_QUERY, param))
1789 				return -EINVAL;
1790 
1791 			fixed_dpi = param[1] & 0x10;
1792 
1793 			if (((info->fw_version >> 16) == 0x14) && fixed_dpi) {
1794 				if (info->send_cmd(psmouse, ETP_SAMPLE_QUERY, param))
1795 					return -EINVAL;
1796 
1797 				info->x_max = (info->capabilities[1] - i) * param[1] / 2;
1798 				info->y_max = (info->capabilities[2] - i) * param[2] / 2;
1799 			} else if (info->fw_version == 0x040216) {
1800 				info->x_max = 819;
1801 				info->y_max = 405;
1802 			} else if (info->fw_version == 0x040219 || info->fw_version == 0x040215) {
1803 				info->x_max = 900;
1804 				info->y_max = 500;
1805 			} else {
1806 				info->x_max = (info->capabilities[1] - i) * 64;
1807 				info->y_max = (info->capabilities[2] - i) * 64;
1808 			}
1809 		}
1810 		break;
1811 
1812 	case 3:
1813 		if (info->send_cmd(psmouse, ETP_FW_ID_QUERY, param))
1814 			return -EINVAL;
1815 
1816 		info->x_max = (0x0f & param[0]) << 8 | param[1];
1817 		info->y_max = (0xf0 & param[0]) << 4 | param[2];
1818 		break;
1819 
1820 	case 4:
1821 		if (info->send_cmd(psmouse, ETP_FW_ID_QUERY, param))
1822 			return -EINVAL;
1823 
1824 		info->x_max = (0x0f & param[0]) << 8 | param[1];
1825 		info->y_max = (0xf0 & param[0]) << 4 | param[2];
1826 		traces = info->capabilities[1];
1827 		if ((traces < 2) || (traces > info->x_max))
1828 			return -EINVAL;
1829 
1830 		info->width = info->x_max / (traces - 1);
1831 
1832 		/* column number of traces */
1833 		info->x_traces = traces;
1834 
1835 		/* row number of traces */
1836 		traces = info->capabilities[2];
1837 		if ((traces >= 2) && (traces <= info->y_max))
1838 			info->y_traces = traces;
1839 
1840 		break;
1841 	}
1842 
1843 	/* check for the middle button: DMI matching or new v4 firmwares */
1844 	info->has_middle_button = dmi_check_system(elantech_dmi_has_middle_button) ||
1845 				  (ETP_NEW_IC_SMBUS_HOST_NOTIFY(info->fw_version) &&
1846 				   !elantech_is_buttonpad(info));
1847 
1848 	return 0;
1849 }
1850 
1851 #if defined(CONFIG_MOUSE_PS2_ELANTECH_SMBUS)
1852 
1853 /*
1854  * The newest Elantech device can use a secondary bus (over SMBus) which
1855  * provides a better bandwidth and allow a better control of the touchpads.
1856  * This is used to decide if we need to use this bus or not.
1857  */
1858 enum {
1859 	ELANTECH_SMBUS_NOT_SET = -1,
1860 	ELANTECH_SMBUS_OFF,
1861 	ELANTECH_SMBUS_ON,
1862 };
1863 
1864 static int elantech_smbus = IS_ENABLED(CONFIG_MOUSE_ELAN_I2C_SMBUS) ?
1865 		ELANTECH_SMBUS_NOT_SET : ELANTECH_SMBUS_OFF;
1866 module_param_named(elantech_smbus, elantech_smbus, int, 0644);
1867 MODULE_PARM_DESC(elantech_smbus, "Use a secondary bus for the Elantech device.");
1868 
1869 static const char * const i2c_blacklist_pnp_ids[] = {
1870 	/*
1871 	 * These are known to not be working properly as bits are missing
1872 	 * in elan_i2c.
1873 	 */
1874 	NULL
1875 };
1876 
1877 static int elantech_create_smbus(struct psmouse *psmouse,
1878 				 struct elantech_device_info *info,
1879 				 bool leave_breadcrumbs)
1880 {
1881 	struct property_entry i2c_props[11] = {};
1882 	struct i2c_board_info smbus_board = {
1883 		I2C_BOARD_INFO("elan_i2c", 0x15),
1884 		.flags = I2C_CLIENT_HOST_NOTIFY,
1885 	};
1886 	unsigned int idx = 0;
1887 
1888 	smbus_board.properties = i2c_props;
1889 
1890 	i2c_props[idx++] = PROPERTY_ENTRY_U32("touchscreen-size-x",
1891 						   info->x_max + 1);
1892 	i2c_props[idx++] = PROPERTY_ENTRY_U32("touchscreen-size-y",
1893 						   info->y_max + 1);
1894 	i2c_props[idx++] = PROPERTY_ENTRY_U32("touchscreen-min-x",
1895 						   info->x_min);
1896 	i2c_props[idx++] = PROPERTY_ENTRY_U32("touchscreen-min-y",
1897 						   info->y_min);
1898 	if (info->x_res)
1899 		i2c_props[idx++] = PROPERTY_ENTRY_U32("touchscreen-x-mm",
1900 						      (info->x_max + 1) / info->x_res);
1901 	if (info->y_res)
1902 		i2c_props[idx++] = PROPERTY_ENTRY_U32("touchscreen-y-mm",
1903 						      (info->y_max + 1) / info->y_res);
1904 
1905 	if (info->has_trackpoint)
1906 		i2c_props[idx++] = PROPERTY_ENTRY_BOOL("elan,trackpoint");
1907 
1908 	if (info->has_middle_button)
1909 		i2c_props[idx++] = PROPERTY_ENTRY_BOOL("elan,middle-button");
1910 
1911 	if (info->x_traces)
1912 		i2c_props[idx++] = PROPERTY_ENTRY_U32("elan,x_traces",
1913 						      info->x_traces);
1914 	if (info->y_traces)
1915 		i2c_props[idx++] = PROPERTY_ENTRY_U32("elan,y_traces",
1916 						      info->y_traces);
1917 
1918 	if (elantech_is_buttonpad(info))
1919 		i2c_props[idx++] = PROPERTY_ENTRY_BOOL("elan,clickpad");
1920 
1921 	return psmouse_smbus_init(psmouse, &smbus_board, NULL, 0, false,
1922 				  leave_breadcrumbs);
1923 }
1924 
1925 /*
1926  * elantech_setup_smbus - called once the PS/2 devices are enumerated
1927  * and decides to instantiate a SMBus InterTouch device.
1928  */
1929 static int elantech_setup_smbus(struct psmouse *psmouse,
1930 				struct elantech_device_info *info,
1931 				bool leave_breadcrumbs)
1932 {
1933 	int error;
1934 
1935 	if (elantech_smbus == ELANTECH_SMBUS_OFF)
1936 		return -ENXIO;
1937 
1938 	if (elantech_smbus == ELANTECH_SMBUS_NOT_SET) {
1939 		/*
1940 		 * New ICs are enabled by default, unless mentioned in
1941 		 * i2c_blacklist_pnp_ids.
1942 		 * Old ICs are up to the user to decide.
1943 		 */
1944 		if (!ETP_NEW_IC_SMBUS_HOST_NOTIFY(info->fw_version) ||
1945 		    psmouse_matches_pnp_id(psmouse, i2c_blacklist_pnp_ids))
1946 			return -ENXIO;
1947 	}
1948 
1949 	psmouse_info(psmouse, "Trying to set up SMBus access\n");
1950 
1951 	error = elantech_create_smbus(psmouse, info, leave_breadcrumbs);
1952 	if (error) {
1953 		if (error == -EAGAIN)
1954 			psmouse_info(psmouse, "SMbus companion is not ready yet\n");
1955 		else
1956 			psmouse_err(psmouse, "unable to create intertouch device\n");
1957 
1958 		return error;
1959 	}
1960 
1961 	return 0;
1962 }
1963 
1964 static bool elantech_use_host_notify(struct psmouse *psmouse,
1965 				     struct elantech_device_info *info)
1966 {
1967 	if (ETP_NEW_IC_SMBUS_HOST_NOTIFY(info->fw_version))
1968 		return true;
1969 
1970 	switch (info->bus) {
1971 	case ETP_BUS_PS2_ONLY:
1972 		/* expected case */
1973 		break;
1974 	case ETP_BUS_SMB_ALERT_ONLY:
1975 	case ETP_BUS_PS2_SMB_ALERT:
1976 		psmouse_dbg(psmouse, "Ignoring SMBus provider through alert protocol.\n");
1977 		break;
1978 	case ETP_BUS_SMB_HST_NTFY_ONLY:
1979 	case ETP_BUS_PS2_SMB_HST_NTFY:
1980 		return true;
1981 	default:
1982 		psmouse_dbg(psmouse,
1983 			    "Ignoring SMBus bus provider %d.\n",
1984 			    info->bus);
1985 	}
1986 
1987 	return false;
1988 }
1989 
1990 int elantech_init_smbus(struct psmouse *psmouse)
1991 {
1992 	struct elantech_device_info info;
1993 	int error;
1994 
1995 	psmouse_reset(psmouse);
1996 
1997 	error = elantech_query_info(psmouse, &info);
1998 	if (error)
1999 		goto init_fail;
2000 
2001 	if (info.hw_version < 4) {
2002 		error = -ENXIO;
2003 		goto init_fail;
2004 	}
2005 
2006 	return elantech_create_smbus(psmouse, &info, false);
2007  init_fail:
2008 	psmouse_reset(psmouse);
2009 	return error;
2010 }
2011 #endif /* CONFIG_MOUSE_PS2_ELANTECH_SMBUS */
2012 
2013 /*
2014  * Initialize the touchpad and create sysfs entries
2015  */
2016 static int elantech_setup_ps2(struct psmouse *psmouse,
2017 			      struct elantech_device_info *info)
2018 {
2019 	struct elantech_data *etd;
2020 	int i;
2021 	int error = -EINVAL;
2022 	struct input_dev *tp_dev;
2023 
2024 	psmouse->private = etd = kzalloc(sizeof(*etd), GFP_KERNEL);
2025 	if (!etd)
2026 		return -ENOMEM;
2027 
2028 	etd->info = *info;
2029 
2030 	etd->parity[0] = 1;
2031 	for (i = 1; i < 256; i++)
2032 		etd->parity[i] = etd->parity[i & (i - 1)] ^ 1;
2033 
2034 	if (elantech_set_absolute_mode(psmouse)) {
2035 		psmouse_err(psmouse,
2036 			    "failed to put touchpad into absolute mode.\n");
2037 		goto init_fail;
2038 	}
2039 
2040 	if (info->fw_version == 0x381f17) {
2041 		etd->original_set_rate = psmouse->set_rate;
2042 		psmouse->set_rate = elantech_set_rate_restore_reg_07;
2043 	}
2044 
2045 	if (elantech_set_input_params(psmouse)) {
2046 		psmouse_err(psmouse, "failed to query touchpad range.\n");
2047 		goto init_fail;
2048 	}
2049 
2050 	error = sysfs_create_group(&psmouse->ps2dev.serio->dev.kobj,
2051 				   &elantech_attr_group);
2052 	if (error) {
2053 		psmouse_err(psmouse,
2054 			    "failed to create sysfs attributes, error: %d.\n",
2055 			    error);
2056 		goto init_fail;
2057 	}
2058 
2059 	if (info->has_trackpoint) {
2060 		tp_dev = input_allocate_device();
2061 
2062 		if (!tp_dev) {
2063 			error = -ENOMEM;
2064 			goto init_fail_tp_alloc;
2065 		}
2066 
2067 		etd->tp_dev = tp_dev;
2068 		snprintf(etd->tp_phys, sizeof(etd->tp_phys), "%s/input1",
2069 			psmouse->ps2dev.serio->phys);
2070 		tp_dev->phys = etd->tp_phys;
2071 		tp_dev->name = "ETPS/2 Elantech TrackPoint";
2072 		tp_dev->id.bustype = BUS_I8042;
2073 		tp_dev->id.vendor  = 0x0002;
2074 		tp_dev->id.product = PSMOUSE_ELANTECH;
2075 		tp_dev->id.version = 0x0000;
2076 		tp_dev->dev.parent = &psmouse->ps2dev.serio->dev;
2077 		tp_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
2078 		tp_dev->relbit[BIT_WORD(REL_X)] =
2079 			BIT_MASK(REL_X) | BIT_MASK(REL_Y);
2080 		tp_dev->keybit[BIT_WORD(BTN_LEFT)] =
2081 			BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_MIDDLE) |
2082 			BIT_MASK(BTN_RIGHT);
2083 
2084 		__set_bit(INPUT_PROP_POINTER, tp_dev->propbit);
2085 		__set_bit(INPUT_PROP_POINTING_STICK, tp_dev->propbit);
2086 
2087 		error = input_register_device(etd->tp_dev);
2088 		if (error < 0)
2089 			goto init_fail_tp_reg;
2090 	}
2091 
2092 	psmouse->protocol_handler = elantech_process_byte;
2093 	psmouse->disconnect = elantech_disconnect;
2094 	psmouse->reconnect = elantech_reconnect;
2095 	psmouse->pktsize = info->hw_version > 1 ? 6 : 4;
2096 
2097 	return 0;
2098  init_fail_tp_reg:
2099 	input_free_device(tp_dev);
2100  init_fail_tp_alloc:
2101 	sysfs_remove_group(&psmouse->ps2dev.serio->dev.kobj,
2102 			   &elantech_attr_group);
2103  init_fail:
2104 	kfree(etd);
2105 	return error;
2106 }
2107 
2108 int elantech_init_ps2(struct psmouse *psmouse)
2109 {
2110 	struct elantech_device_info info;
2111 	int error;
2112 
2113 	psmouse_reset(psmouse);
2114 
2115 	error = elantech_query_info(psmouse, &info);
2116 	if (error)
2117 		goto init_fail;
2118 
2119 	error = elantech_setup_ps2(psmouse, &info);
2120 	if (error)
2121 		goto init_fail;
2122 
2123 	return 0;
2124  init_fail:
2125 	psmouse_reset(psmouse);
2126 	return error;
2127 }
2128 
2129 int elantech_init(struct psmouse *psmouse)
2130 {
2131 	struct elantech_device_info info;
2132 	int error;
2133 
2134 	psmouse_reset(psmouse);
2135 
2136 	error = elantech_query_info(psmouse, &info);
2137 	if (error)
2138 		goto init_fail;
2139 
2140 #if defined(CONFIG_MOUSE_PS2_ELANTECH_SMBUS)
2141 
2142 	if (elantech_use_host_notify(psmouse, &info)) {
2143 		if (!IS_ENABLED(CONFIG_MOUSE_ELAN_I2C_SMBUS) ||
2144 		    !IS_ENABLED(CONFIG_MOUSE_PS2_ELANTECH_SMBUS)) {
2145 			psmouse_warn(psmouse,
2146 				     "The touchpad can support a better bus than the too old PS/2 protocol. "
2147 				     "Make sure MOUSE_PS2_ELANTECH_SMBUS and MOUSE_ELAN_I2C_SMBUS are enabled to get a better touchpad experience.\n");
2148 		}
2149 		error = elantech_setup_smbus(psmouse, &info, true);
2150 		if (!error)
2151 			return PSMOUSE_ELANTECH_SMBUS;
2152 	}
2153 
2154 #endif /* CONFIG_MOUSE_PS2_ELANTECH_SMBUS */
2155 
2156 	error = elantech_setup_ps2(psmouse, &info);
2157 	if (error < 0) {
2158 		/*
2159 		 * Not using any flavor of Elantech support, so clean up
2160 		 * SMbus breadcrumbs, if any.
2161 		 */
2162 		psmouse_smbus_cleanup(psmouse);
2163 		goto init_fail;
2164 	}
2165 
2166 	return PSMOUSE_ELANTECH;
2167  init_fail:
2168 	psmouse_reset(psmouse);
2169 	return error;
2170 }
2171