elantech.c 54.1 KB
Newer Older
1
/*
2
 * Elantech Touchpad driver (v6)
3
 *
4
 * Copyright (C) 2007-2009 Arjan Opmeer <arjan@opmeer.net>
5 6 7 8 9 10 11 12 13
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as published
 * by the Free Software Foundation.
 *
 * Trademarks are the property of their respective owners.
 */

#include <linux/delay.h>
14
#include <linux/dmi.h>
15
#include <linux/slab.h>
16
#include <linux/module.h>
17
#include <linux/i2c.h>
18
#include <linux/input.h>
19
#include <linux/input/mt.h>
20
#include <linux/platform_device.h>
21 22
#include <linux/serio.h>
#include <linux/libps2.h>
23
#include <asm/unaligned.h>
24 25
#include "psmouse.h"
#include "elantech.h"
26
#include "elan_i2c.h"
27

28 29
#define elantech_debug(fmt, ...)					\
	do {								\
30
		if (etd->info.debug)					\
31 32
			psmouse_printk(KERN_DEBUG, psmouse,		\
					fmt, ##__VA_ARGS__);		\
33 34 35 36 37 38 39 40
	} while (0)

/*
 * Send a Synaptics style sliced query command
 */
static int synaptics_send_cmd(struct psmouse *psmouse, unsigned char c,
				unsigned char *param)
{
41
	if (ps2_sliced_command(&psmouse->ps2dev, c) ||
42
	    ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_GETINFO)) {
43
		psmouse_err(psmouse, "%s query 0x%02x failed.\n", __func__, c);
44 45 46 47 48 49
		return -1;
	}

	return 0;
}

50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
/*
 * V3 and later support this fast command
 */
static int elantech_send_cmd(struct psmouse *psmouse, unsigned char c,
				unsigned char *param)
{
	struct ps2dev *ps2dev = &psmouse->ps2dev;

	if (ps2_command(ps2dev, NULL, ETP_PS2_CUSTOM_COMMAND) ||
	    ps2_command(ps2dev, NULL, c) ||
	    ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO)) {
		psmouse_err(psmouse, "%s query 0x%02x failed.\n", __func__, c);
		return -1;
	}

	return 0;
}

68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
/*
 * A retrying version of ps2_command
 */
static int elantech_ps2_command(struct psmouse *psmouse,
				unsigned char *param, int command)
{
	struct ps2dev *ps2dev = &psmouse->ps2dev;
	struct elantech_data *etd = psmouse->private;
	int rc;
	int tries = ETP_PS2_COMMAND_TRIES;

	do {
		rc = ps2_command(ps2dev, param, command);
		if (rc == 0)
			break;
		tries--;
84 85
		elantech_debug("retrying ps2 command 0x%02x (%d).\n",
				command, tries);
86 87 88 89
		msleep(ETP_PS2_COMMAND_DELAY);
	} while (tries > 0);

	if (rc)
90
		psmouse_err(psmouse, "ps2 command 0x%02x failed.\n", command);
91 92 93 94 95 96 97 98 99 100 101 102 103 104

	return rc;
}

/*
 * Send an Elantech style special command to read a value from a register
 */
static int elantech_read_reg(struct psmouse *psmouse, unsigned char reg,
				unsigned char *val)
{
	struct elantech_data *etd = psmouse->private;
	unsigned char param[3];
	int rc = 0;

105
	if (reg < 0x07 || reg > 0x26)
106 107 108 109 110
		return -1;

	if (reg > 0x11 && reg < 0x20)
		return -1;

111
	switch (etd->info.hw_version) {
112
	case 1:
113 114
		if (ps2_sliced_command(&psmouse->ps2dev, ETP_REGISTER_READ) ||
		    ps2_sliced_command(&psmouse->ps2dev, reg) ||
115 116 117 118 119 120 121 122 123 124 125 126 127 128
		    ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_GETINFO)) {
			rc = -1;
		}
		break;

	case 2:
		if (elantech_ps2_command(psmouse,  NULL, ETP_PS2_CUSTOM_COMMAND) ||
		    elantech_ps2_command(psmouse,  NULL, ETP_REGISTER_READ) ||
		    elantech_ps2_command(psmouse,  NULL, ETP_PS2_CUSTOM_COMMAND) ||
		    elantech_ps2_command(psmouse,  NULL, reg) ||
		    elantech_ps2_command(psmouse, param, PSMOUSE_CMD_GETINFO)) {
			rc = -1;
		}
		break;
129

130
	case 3 ... 4:
131 132 133 134 135 136 137 138
		if (elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
		    elantech_ps2_command(psmouse, NULL, ETP_REGISTER_READWRITE) ||
		    elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
		    elantech_ps2_command(psmouse, NULL, reg) ||
		    elantech_ps2_command(psmouse, param, PSMOUSE_CMD_GETINFO)) {
			rc = -1;
		}
		break;
139 140 141
	}

	if (rc)
142
		psmouse_err(psmouse, "failed to read register 0x%02x.\n", reg);
143
	else if (etd->info.hw_version != 4)
144
		*val = param[0];
145 146
	else
		*val = param[1];
147 148 149 150 151 152 153 154 155 156 157 158 159

	return rc;
}

/*
 * Send an Elantech style special command to write a register with a value
 */
static int elantech_write_reg(struct psmouse *psmouse, unsigned char reg,
				unsigned char val)
{
	struct elantech_data *etd = psmouse->private;
	int rc = 0;

160
	if (reg < 0x07 || reg > 0x26)
161 162 163 164 165
		return -1;

	if (reg > 0x11 && reg < 0x20)
		return -1;

166
	switch (etd->info.hw_version) {
167
	case 1:
168 169 170
		if (ps2_sliced_command(&psmouse->ps2dev, ETP_REGISTER_WRITE) ||
		    ps2_sliced_command(&psmouse->ps2dev, reg) ||
		    ps2_sliced_command(&psmouse->ps2dev, val) ||
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
		    ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11)) {
			rc = -1;
		}
		break;

	case 2:
		if (elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
		    elantech_ps2_command(psmouse, NULL, ETP_REGISTER_WRITE) ||
		    elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
		    elantech_ps2_command(psmouse, NULL, reg) ||
		    elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
		    elantech_ps2_command(psmouse, NULL, val) ||
		    elantech_ps2_command(psmouse, NULL, PSMOUSE_CMD_SETSCALE11)) {
			rc = -1;
		}
		break;
187 188 189 190 191 192 193 194 195 196 197 198

	case 3:
		if (elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
		    elantech_ps2_command(psmouse, NULL, ETP_REGISTER_READWRITE) ||
		    elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
		    elantech_ps2_command(psmouse, NULL, reg) ||
		    elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
		    elantech_ps2_command(psmouse, NULL, val) ||
		    elantech_ps2_command(psmouse, NULL, PSMOUSE_CMD_SETSCALE11)) {
			rc = -1;
		}
		break;
199 200 201 202 203 204 205 206 207 208 209 210 211 212

	case 4:
		if (elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
		    elantech_ps2_command(psmouse, NULL, ETP_REGISTER_READWRITE) ||
		    elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
		    elantech_ps2_command(psmouse, NULL, reg) ||
		    elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
		    elantech_ps2_command(psmouse, NULL, ETP_REGISTER_READWRITE) ||
		    elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
		    elantech_ps2_command(psmouse, NULL, val) ||
		    elantech_ps2_command(psmouse, NULL, PSMOUSE_CMD_SETSCALE11)) {
			rc = -1;
		}
		break;
213 214 215
	}

	if (rc)
216 217 218
		psmouse_err(psmouse,
			    "failed to write register 0x%02x with value 0x%02x.\n",
			    reg, val);
219 220 221 222 223 224 225

	return rc;
}

/*
 * Dump a complete mouse movement packet to the syslog
 */
226
static void elantech_packet_dump(struct psmouse *psmouse)
227
{
228 229
	psmouse_printk(KERN_DEBUG, psmouse, "PS/2 packet [%*ph]\n",
		       psmouse->pktsize, psmouse->packet);
230 231 232 233 234 235 236 237 238 239 240 241 242
}

/*
 * Interpret complete data packets and report absolute mode input events for
 * hardware version 1. (4 byte packets)
 */
static void elantech_report_absolute_v1(struct psmouse *psmouse)
{
	struct input_dev *dev = psmouse->dev;
	struct elantech_data *etd = psmouse->private;
	unsigned char *packet = psmouse->packet;
	int fingers;

243
	if (etd->info.fw_version < 0x020000) {
244 245 246 247
		/*
		 * byte 0:  D   U  p1  p2   1  p3   R   L
		 * byte 1:  f   0  th  tw  x9  x8  y9  y8
		 */
248 249 250
		fingers = ((packet[1] & 0x80) >> 7) +
				((packet[1] & 0x30) >> 4);
	} else {
251 252 253 254
		/*
		 * byte 0: n1  n0  p2  p1   1  p3   R   L
		 * byte 1:  0   0   0   0  x9  x8  y9  y8
		 */
255 256 257
		fingers = (packet[0] & 0xc0) >> 6;
	}

258
	if (etd->info.jumpy_cursor) {
259 260 261 262 263
		if (fingers != 1) {
			etd->single_finger_reports = 0;
		} else if (etd->single_finger_reports < 2) {
			/* Discard first 2 reports of one finger, bogus */
			etd->single_finger_reports++;
264
			elantech_debug("discarding packet\n");
265
			return;
266 267 268
		}
	}

269 270
	input_report_key(dev, BTN_TOUCH, fingers != 0);

271 272 273 274
	/*
	 * byte 2: x7  x6  x5  x4  x3  x2  x1  x0
	 * byte 3: y7  y6  y5  y4  y3  y2  y1  y0
	 */
275 276 277
	if (fingers) {
		input_report_abs(dev, ABS_X,
			((packet[1] & 0x0c) << 6) | packet[2]);
278
		input_report_abs(dev, ABS_Y,
279
			etd->y_max - (((packet[1] & 0x03) << 8) | packet[3]));
280 281 282 283 284
	}

	input_report_key(dev, BTN_TOOL_FINGER, fingers == 1);
	input_report_key(dev, BTN_TOOL_DOUBLETAP, fingers == 2);
	input_report_key(dev, BTN_TOOL_TRIPLETAP, fingers == 3);
285 286

	psmouse_report_standard_buttons(dev, packet[0]);
287

288 289
	if (etd->info.fw_version < 0x020000 &&
	    (etd->info.capabilities[0] & ETP_CAP_HAS_ROCKER)) {
290 291 292 293 294 295 296 297 298
		/* rocker up */
		input_report_key(dev, BTN_FORWARD, packet[0] & 0x40);
		/* rocker down */
		input_report_key(dev, BTN_BACK, packet[0] & 0x80);
	}

	input_sync(dev);
}

299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
static void elantech_set_slot(struct input_dev *dev, int slot, bool active,
			      unsigned int x, unsigned int y)
{
	input_mt_slot(dev, slot);
	input_mt_report_slot_state(dev, MT_TOOL_FINGER, active);
	if (active) {
		input_report_abs(dev, ABS_MT_POSITION_X, x);
		input_report_abs(dev, ABS_MT_POSITION_Y, y);
	}
}

/* x1 < x2 and y1 < y2 when two fingers, x = y = 0 when not pressed */
static void elantech_report_semi_mt_data(struct input_dev *dev,
					 unsigned int num_fingers,
					 unsigned int x1, unsigned int y1,
					 unsigned int x2, unsigned int y2)
{
	elantech_set_slot(dev, 0, num_fingers != 0, x1, y1);
317
	elantech_set_slot(dev, 1, num_fingers >= 2, x2, y2);
318 319
}

320 321 322 323 324 325
/*
 * Interpret complete data packets and report absolute mode input events for
 * hardware version 2. (6 byte packets)
 */
static void elantech_report_absolute_v2(struct psmouse *psmouse)
{
326
	struct elantech_data *etd = psmouse->private;
327 328
	struct input_dev *dev = psmouse->dev;
	unsigned char *packet = psmouse->packet;
329 330
	unsigned int fingers, x1 = 0, y1 = 0, x2 = 0, y2 = 0;
	unsigned int width = 0, pres = 0;
331 332 333 334 335

	/* byte 0: n1  n0   .   .   .   .   R   L */
	fingers = (packet[0] & 0xc0) >> 6;

	switch (fingers) {
336 337 338 339 340 341 342 343
	case 3:
		/*
		 * Same as one finger, except report of more than 3 fingers:
		 * byte 3:  n4  .   w1  w0   .   .   .   .
		 */
		if (packet[3] & 0x80)
			fingers = 4;
		/* pass through... */
344
	case 1:
345
		/*
346
		 * byte 1:  .   .   .   .  x11 x10 x9  x8
347 348
		 * byte 2: x7  x6  x5  x4  x4  x2  x1  x0
		 */
349
		x1 = ((packet[1] & 0x0f) << 8) | packet[2];
350
		/*
351
		 * byte 4:  .   .   .   .  y11 y10 y9  y8
352 353
		 * byte 5: y7  y6  y5  y4  y3  y2  y1  y0
		 */
354
		y1 = etd->y_max - (((packet[4] & 0x0f) << 8) | packet[5]);
355

356 357
		pres = (packet[1] & 0xf0) | ((packet[4] & 0xf0) >> 4);
		width = ((packet[0] & 0x30) >> 2) | ((packet[3] & 0x30) >> 4);
358 359 360
		break;

	case 2:
361 362 363 364 365 366
		/*
		 * The coordinate of each finger is reported separately
		 * with a lower resolution for two finger touches:
		 * byte 0:  .   .  ay8 ax8  .   .   .   .
		 * byte 1: ax7 ax6 ax5 ax4 ax3 ax2 ax1 ax0
		 */
367
		x1 = (((packet[0] & 0x10) << 4) | packet[1]) << 2;
368
		/* byte 2: ay7 ay6 ay5 ay4 ay3 ay2 ay1 ay0 */
369
		y1 = etd->y_max -
370
			((((packet[0] & 0x20) << 3) | packet[2]) << 2);
371 372 373 374
		/*
		 * byte 3:  .   .  by8 bx8  .   .   .   .
		 * byte 4: bx7 bx6 bx5 bx4 bx3 bx2 bx1 bx0
		 */
375
		x2 = (((packet[3] & 0x10) << 4) | packet[4]) << 2;
376
		/* byte 5: by7 by8 by5 by4 by3 by2 by1 by0 */
377
		y2 = etd->y_max -
378
			((((packet[3] & 0x20) << 3) | packet[5]) << 2);
379 380 381 382

		/* Unknown so just report sensible values */
		pres = 127;
		width = 7;
383 384 385
		break;
	}

386 387 388 389 390
	input_report_key(dev, BTN_TOUCH, fingers != 0);
	if (fingers != 0) {
		input_report_abs(dev, ABS_X, x1);
		input_report_abs(dev, ABS_Y, y1);
	}
391
	elantech_report_semi_mt_data(dev, fingers, x1, y1, x2, y2);
392 393 394
	input_report_key(dev, BTN_TOOL_FINGER, fingers == 1);
	input_report_key(dev, BTN_TOOL_DOUBLETAP, fingers == 2);
	input_report_key(dev, BTN_TOOL_TRIPLETAP, fingers == 3);
395
	input_report_key(dev, BTN_TOOL_QUADTAP, fingers == 4);
396
	psmouse_report_standard_buttons(dev, packet[0]);
397
	if (etd->info.reports_pressure) {
398 399 400
		input_report_abs(dev, ABS_PRESSURE, pres);
		input_report_abs(dev, ABS_TOOL_WIDTH, width);
	}
401 402 403 404

	input_sync(dev);
}

405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
static void elantech_report_trackpoint(struct psmouse *psmouse,
				       int packet_type)
{
	/*
	 * byte 0:  0   0  sx  sy   0   M   R   L
	 * byte 1:~sx   0   0   0   0   0   0   0
	 * byte 2:~sy   0   0   0   0   0   0   0
	 * byte 3:  0   0 ~sy ~sx   0   1   1   0
	 * byte 4: x7  x6  x5  x4  x3  x2  x1  x0
	 * byte 5: y7  y6  y5  y4  y3  y2  y1  y0
	 *
	 * x and y are written in two's complement spread
	 * over 9 bits with sx/sy the relative top bit and
	 * x7..x0 and y7..y0 the lower bits.
	 * The sign of y is opposite to what the input driver
	 * expects for a relative movement
	 */

	struct elantech_data *etd = psmouse->private;
	struct input_dev *tp_dev = etd->tp_dev;
	unsigned char *packet = psmouse->packet;
	int x, y;
	u32 t;

	t = get_unaligned_le32(&packet[0]);

	switch (t & ~7U) {
	case 0x06000030U:
	case 0x16008020U:
	case 0x26800010U:
	case 0x36808000U:
		x = packet[4] - (int)((packet[1]^0x80) << 1);
		y = (int)((packet[2]^0x80) << 1) - packet[5];

439
		psmouse_report_standard_buttons(tp_dev, packet[0]);
440 441 442 443 444 445 446 447 448 449

		input_report_rel(tp_dev, REL_X, x);
		input_report_rel(tp_dev, REL_Y, y);

		input_sync(tp_dev);

		break;

	default:
		/* Dump unexpected packet sequences if debug=1 (default) */
450
		if (etd->info.debug == 1)
451 452 453 454 455 456
			elantech_packet_dump(psmouse);

		break;
	}
}

457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
/*
 * Interpret complete data packets and report absolute mode input events for
 * hardware version 3. (12 byte packets for two fingers)
 */
static void elantech_report_absolute_v3(struct psmouse *psmouse,
					int packet_type)
{
	struct input_dev *dev = psmouse->dev;
	struct elantech_data *etd = psmouse->private;
	unsigned char *packet = psmouse->packet;
	unsigned int fingers = 0, x1 = 0, y1 = 0, x2 = 0, y2 = 0;
	unsigned int width = 0, pres = 0;

	/* byte 0: n1  n0   .   .   .   .   R   L */
	fingers = (packet[0] & 0xc0) >> 6;

	switch (fingers) {
	case 3:
	case 1:
		/*
		 * byte 1:  .   .   .   .  x11 x10 x9  x8
		 * byte 2: x7  x6  x5  x4  x4  x2  x1  x0
		 */
		x1 = ((packet[1] & 0x0f) << 8) | packet[2];
		/*
		 * byte 4:  .   .   .   .  y11 y10 y9  y8
		 * byte 5: y7  y6  y5  y4  y3  y2  y1  y0
		 */
		y1 = etd->y_max - (((packet[4] & 0x0f) << 8) | packet[5]);
		break;

	case 2:
		if (packet_type == PACKET_V3_HEAD) {
			/*
			 * byte 1:   .    .    .    .  ax11 ax10 ax9  ax8
			 * byte 2: ax7  ax6  ax5  ax4  ax3  ax2  ax1  ax0
			 */
494
			etd->mt[0].x = ((packet[1] & 0x0f) << 8) | packet[2];
495 496 497 498
			/*
			 * byte 4:   .    .    .    .  ay11 ay10 ay9  ay8
			 * byte 5: ay7  ay6  ay5  ay4  ay3  ay2  ay1  ay0
			 */
499
			etd->mt[0].y = etd->y_max -
500 501 502 503 504 505 506 507
				(((packet[4] & 0x0f) << 8) | packet[5]);
			/*
			 * wait for next packet
			 */
			return;
		}

		/* packet_type == PACKET_V3_TAIL */
508 509
		x1 = etd->mt[0].x;
		y1 = etd->mt[0].y;
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
		x2 = ((packet[1] & 0x0f) << 8) | packet[2];
		y2 = etd->y_max - (((packet[4] & 0x0f) << 8) | packet[5]);
		break;
	}

	pres = (packet[1] & 0xf0) | ((packet[4] & 0xf0) >> 4);
	width = ((packet[0] & 0x30) >> 2) | ((packet[3] & 0x30) >> 4);

	input_report_key(dev, BTN_TOUCH, fingers != 0);
	if (fingers != 0) {
		input_report_abs(dev, ABS_X, x1);
		input_report_abs(dev, ABS_Y, y1);
	}
	elantech_report_semi_mt_data(dev, fingers, x1, y1, x2, y2);
	input_report_key(dev, BTN_TOOL_FINGER, fingers == 1);
	input_report_key(dev, BTN_TOOL_DOUBLETAP, fingers == 2);
	input_report_key(dev, BTN_TOOL_TRIPLETAP, fingers == 3);
527 528

	/* For clickpads map both buttons to BTN_LEFT */
529
	if (etd->info.fw_version & 0x001000)
530
		input_report_key(dev, BTN_LEFT, packet[0] & 0x03);
531 532
	else
		psmouse_report_standard_buttons(dev, packet[0]);
533

534 535 536 537 538 539
	input_report_abs(dev, ABS_PRESSURE, pres);
	input_report_abs(dev, ABS_TOOL_WIDTH, width);

	input_sync(dev);
}

540 541 542
static void elantech_input_sync_v4(struct psmouse *psmouse)
{
	struct input_dev *dev = psmouse->dev;
543
	struct elantech_data *etd = psmouse->private;
544 545
	unsigned char *packet = psmouse->packet;

546
	/* For clickpads map both buttons to BTN_LEFT */
547
	if (etd->info.fw_version & 0x001000)
548
		input_report_key(dev, BTN_LEFT, packet[0] & 0x03);
549 550
	else
		psmouse_report_standard_buttons(dev, packet[0]);
551

552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667
	input_mt_report_pointer_emulation(dev, true);
	input_sync(dev);
}

static void process_packet_status_v4(struct psmouse *psmouse)
{
	struct input_dev *dev = psmouse->dev;
	unsigned char *packet = psmouse->packet;
	unsigned fingers;
	int i;

	/* notify finger state change */
	fingers = packet[1] & 0x1f;
	for (i = 0; i < ETP_MAX_FINGERS; i++) {
		if ((fingers & (1 << i)) == 0) {
			input_mt_slot(dev, i);
			input_mt_report_slot_state(dev, MT_TOOL_FINGER, false);
		}
	}

	elantech_input_sync_v4(psmouse);
}

static void process_packet_head_v4(struct psmouse *psmouse)
{
	struct input_dev *dev = psmouse->dev;
	struct elantech_data *etd = psmouse->private;
	unsigned char *packet = psmouse->packet;
	int id = ((packet[3] & 0xe0) >> 5) - 1;
	int pres, traces;

	if (id < 0)
		return;

	etd->mt[id].x = ((packet[1] & 0x0f) << 8) | packet[2];
	etd->mt[id].y = etd->y_max - (((packet[4] & 0x0f) << 8) | packet[5]);
	pres = (packet[1] & 0xf0) | ((packet[4] & 0xf0) >> 4);
	traces = (packet[0] & 0xf0) >> 4;

	input_mt_slot(dev, id);
	input_mt_report_slot_state(dev, MT_TOOL_FINGER, true);

	input_report_abs(dev, ABS_MT_POSITION_X, etd->mt[id].x);
	input_report_abs(dev, ABS_MT_POSITION_Y, etd->mt[id].y);
	input_report_abs(dev, ABS_MT_PRESSURE, pres);
	input_report_abs(dev, ABS_MT_TOUCH_MAJOR, traces * etd->width);
	/* report this for backwards compatibility */
	input_report_abs(dev, ABS_TOOL_WIDTH, traces);

	elantech_input_sync_v4(psmouse);
}

static void process_packet_motion_v4(struct psmouse *psmouse)
{
	struct input_dev *dev = psmouse->dev;
	struct elantech_data *etd = psmouse->private;
	unsigned char *packet = psmouse->packet;
	int weight, delta_x1 = 0, delta_y1 = 0, delta_x2 = 0, delta_y2 = 0;
	int id, sid;

	id = ((packet[0] & 0xe0) >> 5) - 1;
	if (id < 0)
		return;

	sid = ((packet[3] & 0xe0) >> 5) - 1;
	weight = (packet[0] & 0x10) ? ETP_WEIGHT_VALUE : 1;
	/*
	 * Motion packets give us the delta of x, y values of specific fingers,
	 * but in two's complement. Let the compiler do the conversion for us.
	 * Also _enlarge_ the numbers to int, in case of overflow.
	 */
	delta_x1 = (signed char)packet[1];
	delta_y1 = (signed char)packet[2];
	delta_x2 = (signed char)packet[4];
	delta_y2 = (signed char)packet[5];

	etd->mt[id].x += delta_x1 * weight;
	etd->mt[id].y -= delta_y1 * weight;
	input_mt_slot(dev, id);
	input_report_abs(dev, ABS_MT_POSITION_X, etd->mt[id].x);
	input_report_abs(dev, ABS_MT_POSITION_Y, etd->mt[id].y);

	if (sid >= 0) {
		etd->mt[sid].x += delta_x2 * weight;
		etd->mt[sid].y -= delta_y2 * weight;
		input_mt_slot(dev, sid);
		input_report_abs(dev, ABS_MT_POSITION_X, etd->mt[sid].x);
		input_report_abs(dev, ABS_MT_POSITION_Y, etd->mt[sid].y);
	}

	elantech_input_sync_v4(psmouse);
}

static void elantech_report_absolute_v4(struct psmouse *psmouse,
					int packet_type)
{
	switch (packet_type) {
	case PACKET_V4_STATUS:
		process_packet_status_v4(psmouse);
		break;

	case PACKET_V4_HEAD:
		process_packet_head_v4(psmouse);
		break;

	case PACKET_V4_MOTION:
		process_packet_motion_v4(psmouse);
		break;

	case PACKET_UNKNOWN:
	default:
		/* impossible to get here */
		break;
	}
}

668
static int elantech_packet_check_v1(struct psmouse *psmouse)
669 670 671 672 673 674
{
	struct elantech_data *etd = psmouse->private;
	unsigned char *packet = psmouse->packet;
	unsigned char p1, p2, p3;

	/* Parity bits are placed differently */
675
	if (etd->info.fw_version < 0x020000) {
676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
		/* byte 0:  D   U  p1  p2   1  p3   R   L */
		p1 = (packet[0] & 0x20) >> 5;
		p2 = (packet[0] & 0x10) >> 4;
	} else {
		/* byte 0: n1  n0  p2  p1   1  p3   R   L */
		p1 = (packet[0] & 0x10) >> 4;
		p2 = (packet[0] & 0x20) >> 5;
	}

	p3 = (packet[0] & 0x04) >> 2;

	return etd->parity[packet[1]] == p1 &&
	       etd->parity[packet[2]] == p2 &&
	       etd->parity[packet[3]] == p3;
}

692 693 694 695 696 697
static int elantech_debounce_check_v2(struct psmouse *psmouse)
{
        /*
         * When we encounter packet that matches this exactly, it means the
         * hardware is in debounce status. Just ignore the whole packet.
         */
698 699 700
	static const u8 debounce_packet[] = {
		0x84, 0xff, 0xff, 0x02, 0xff, 0xff
	};
701 702 703 704 705
        unsigned char *packet = psmouse->packet;

        return !memcmp(packet, debounce_packet, sizeof(debounce_packet));
}

706 707 708 709 710 711 712 713 714 715 716 717 718 719
static int elantech_packet_check_v2(struct psmouse *psmouse)
{
	struct elantech_data *etd = psmouse->private;
	unsigned char *packet = psmouse->packet;

	/*
	 * V2 hardware has two flavors. Older ones that do not report pressure,
	 * and newer ones that reports pressure and width. With newer ones, all
	 * packets (1, 2, 3 finger touch) have the same constant bits. With
	 * older ones, 1/3 finger touch packets and 2 finger touch packets
	 * have different constant bits.
	 * With all three cases, if the constant bits are not exactly what I
	 * expected, I consider them invalid.
	 */
720
	if (etd->info.reports_pressure)
721 722 723 724 725 726 727 728 729 730 731 732 733
		return (packet[0] & 0x0c) == 0x04 &&
		       (packet[3] & 0x0f) == 0x02;

	if ((packet[0] & 0xc0) == 0x80)
		return (packet[0] & 0x0c) == 0x0c &&
		       (packet[3] & 0x0e) == 0x08;

	return (packet[0] & 0x3c) == 0x3c &&
	       (packet[1] & 0xf0) == 0x00 &&
	       (packet[3] & 0x3e) == 0x38 &&
	       (packet[4] & 0xf0) == 0x00;
}

734 735
/*
 * We check the constant bits to determine what packet type we get,
736
 * so packet checking is mandatory for v3 and later hardware.
737 738 739
 */
static int elantech_packet_check_v3(struct psmouse *psmouse)
{
740
	struct elantech_data *etd = psmouse->private;
741 742 743
	static const u8 debounce_packet[] = {
		0xc4, 0xff, 0xff, 0x02, 0xff, 0xff
	};
744 745 746 747 748 749 750 751 752
	unsigned char *packet = psmouse->packet;

	/*
	 * check debounce first, it has the same signature in byte 0
	 * and byte 3 as PACKET_V3_HEAD.
	 */
	if (!memcmp(packet, debounce_packet, sizeof(debounce_packet)))
		return PACKET_DEBOUNCE;

753 754 755 756
	/*
	 * If the hardware flag 'crc_enabled' is set the packets have
	 * different signatures.
	 */
757
	if (etd->info.crc_enabled) {
758 759 760 761 762 763 764 765
		if ((packet[3] & 0x09) == 0x08)
			return PACKET_V3_HEAD;

		if ((packet[3] & 0x09) == 0x09)
			return PACKET_V3_TAIL;
	} else {
		if ((packet[0] & 0x0c) == 0x04 && (packet[3] & 0xcf) == 0x02)
			return PACKET_V3_HEAD;
766

767 768
		if ((packet[0] & 0x0c) == 0x0c && (packet[3] & 0xce) == 0x0c)
			return PACKET_V3_TAIL;
769 770
		if ((packet[3] & 0x0f) == 0x06)
			return PACKET_TRACKPOINT;
771
	}
772 773 774 775

	return PACKET_UNKNOWN;
}

776 777
static int elantech_packet_check_v4(struct psmouse *psmouse)
{
778
	struct elantech_data *etd = psmouse->private;
779
	unsigned char *packet = psmouse->packet;
780
	unsigned char packet_type = packet[3] & 0x03;
781
	unsigned int ic_version;
782 783
	bool sanity_check;

784
	if (etd->tp_dev && (packet[3] & 0x0f) == 0x06)
785 786
		return PACKET_TRACKPOINT;

787
	/* This represents the version of IC body. */
788
	ic_version = (etd->info.fw_version & 0x0f0000) >> 16;
789

790 791 792
	/*
	 * Sanity check based on the constant bits of a packet.
	 * The constant bits change depending on the value of
793 794 795
	 * the hardware flag 'crc_enabled' and the version of
	 * the IC body, but are the same for every packet,
	 * regardless of the type.
796
	 */
797
	if (etd->info.crc_enabled)
798
		sanity_check = ((packet[3] & 0x08) == 0x00);
799
	else if (ic_version == 7 && etd->info.samples[1] == 0x2A)
800
		sanity_check = ((packet[3] & 0x1c) == 0x10);
801
	else
802
		sanity_check = ((packet[0] & 0x08) == 0x00 &&
803 804 805 806
				(packet[3] & 0x1c) == 0x10);

	if (!sanity_check)
		return PACKET_UNKNOWN;
807

808 809 810 811 812
	switch (packet_type) {
	case 0:
		return PACKET_V4_STATUS;

	case 1:
813 814
		return PACKET_V4_HEAD;

815
	case 2:
816
		return PACKET_V4_MOTION;
817
	}
818 819 820 821

	return PACKET_UNKNOWN;
}

822 823 824 825 826 827
/*
 * Process byte stream from mouse and handle complete packets
 */
static psmouse_ret_t elantech_process_byte(struct psmouse *psmouse)
{
	struct elantech_data *etd = psmouse->private;
828
	int packet_type;
829 830 831 832

	if (psmouse->pktcnt < psmouse->pktsize)
		return PSMOUSE_GOOD_DATA;

833
	if (etd->info.debug > 1)
834
		elantech_packet_dump(psmouse);
835

836
	switch (etd->info.hw_version) {
837
	case 1:
838
		if (etd->info.paritycheck && !elantech_packet_check_v1(psmouse))
839 840 841 842 843 844
			return PSMOUSE_BAD_DATA;

		elantech_report_absolute_v1(psmouse);
		break;

	case 2:
845 846 847 848
		/* ignore debounce */
		if (elantech_debounce_check_v2(psmouse))
			return PSMOUSE_FULL_PACKET;

849
		if (etd->info.paritycheck && !elantech_packet_check_v2(psmouse))
850 851
			return PSMOUSE_BAD_DATA;

852 853
		elantech_report_absolute_v2(psmouse);
		break;
854 855 856

	case 3:
		packet_type = elantech_packet_check_v3(psmouse);
857 858
		switch (packet_type) {
		case PACKET_UNKNOWN:
859 860
			return PSMOUSE_BAD_DATA;

861 862 863 864 865 866 867 868 869 870 871 872 873
		case PACKET_DEBOUNCE:
			/* ignore debounce */
			break;

		case PACKET_TRACKPOINT:
			elantech_report_trackpoint(psmouse, packet_type);
			break;

		default:
			elantech_report_absolute_v3(psmouse, packet_type);
			break;
		}

874
		break;
875 876 877

	case 4:
		packet_type = elantech_packet_check_v4(psmouse);
878 879
		switch (packet_type) {
		case PACKET_UNKNOWN:
880 881
			return PSMOUSE_BAD_DATA;

882 883 884 885 886 887 888 889 890
		case PACKET_TRACKPOINT:
			elantech_report_trackpoint(psmouse, packet_type);
			break;

		default:
			elantech_report_absolute_v4(psmouse, packet_type);
			break;
		}

891
		break;
892 893 894 895 896
	}

	return PSMOUSE_FULL_PACKET;
}

897 898 899 900 901 902 903 904 905 906 907 908 909 910 911
/*
 * This writes the reg_07 value again to the hardware at the end of every
 * set_rate call because the register loses its value. reg_07 allows setting
 * absolute mode on v4 hardware
 */
static void elantech_set_rate_restore_reg_07(struct psmouse *psmouse,
		unsigned int rate)
{
	struct elantech_data *etd = psmouse->private;

	etd->original_set_rate(psmouse, rate);
	if (elantech_write_reg(psmouse, 0x07, etd->reg_07))
		psmouse_err(psmouse, "restoring reg_07 failed\n");
}

912 913 914 915 916 917 918 919 920 921
/*
 * Put the touchpad into absolute mode
 */
static int elantech_set_absolute_mode(struct psmouse *psmouse)
{
	struct elantech_data *etd = psmouse->private;
	unsigned char val;
	int tries = ETP_READ_BACK_TRIES;
	int rc = 0;

922
	switch (etd->info.hw_version) {
923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941
	case 1:
		etd->reg_10 = 0x16;
		etd->reg_11 = 0x8f;
		if (elantech_write_reg(psmouse, 0x10, etd->reg_10) ||
		    elantech_write_reg(psmouse, 0x11, etd->reg_11)) {
			rc = -1;
		}
		break;

	case 2:
					/* Windows driver values */
		etd->reg_10 = 0x54;
		etd->reg_11 = 0x88;	/* 0x8a */
		etd->reg_21 = 0x60;	/* 0x00 */
		if (elantech_write_reg(psmouse, 0x10, etd->reg_10) ||
		    elantech_write_reg(psmouse, 0x11, etd->reg_11) ||
		    elantech_write_reg(psmouse, 0x21, etd->reg_21)) {
			rc = -1;
		}
942 943 944
		break;

	case 3:
945
		if (etd->info.set_hw_resolution)
946 947
			etd->reg_10 = 0x0b;
		else
948
			etd->reg_10 = 0x01;
949

950 951 952 953
		if (elantech_write_reg(psmouse, 0x10, etd->reg_10))
			rc = -1;

		break;
954 955 956 957 958 959 960

	case 4:
		etd->reg_07 = 0x01;
		if (elantech_write_reg(psmouse, 0x07, etd->reg_07))
			rc = -1;

		goto skip_readback_reg_10; /* v4 has no reg 0x10 to read */
961 962 963
	}

	if (rc == 0) {
964
		/*
965 966
		 * Read back reg 0x10. For hardware version 1 we must make
		 * sure the absolute mode bit is set. For hardware version 2
967
		 * the touchpad is probably initializing and not ready until
968
		 * we read back the value we just wrote.
969 970 971 972 973 974
		 */
		do {
			rc = elantech_read_reg(psmouse, 0x10, &val);
			if (rc == 0)
				break;
			tries--;
975
			elantech_debug("retrying read (%d).\n", tries);
976 977
			msleep(ETP_READ_BACK_DELAY);
		} while (tries > 0);
978 979

		if (rc) {
980 981
			psmouse_err(psmouse,
				    "failed to read back register 0x10.\n");
982
		} else if (etd->info.hw_version == 1 &&
983
			   !(val & ETP_R10_ABSOLUTE_MODE)) {
984 985
			psmouse_err(psmouse,
				    "touchpad refuses to switch to absolute mode.\n");
986 987
			rc = -1;
		}
988 989
	}

990
 skip_readback_reg_10:
991
	if (rc)
992
		psmouse_err(psmouse, "failed to initialise registers.\n");
993 994 995 996

	return rc;
}

997 998
static int elantech_set_range(struct psmouse *psmouse,
			      unsigned int *x_min, unsigned int *y_min,
999 1000
			      unsigned int *x_max, unsigned int *y_max,
			      unsigned int *width)
1001 1002
{
	struct elantech_data *etd = psmouse->private;
1003
	struct elantech_device_info *info = &etd->info;
1004
	unsigned char param[3];
1005
	unsigned char traces;
1006

1007
	switch (info->hw_version) {
1008 1009 1010 1011 1012 1013 1014 1015
	case 1:
		*x_min = ETP_XMIN_V1;
		*y_min = ETP_YMIN_V1;
		*x_max = ETP_XMAX_V1;
		*y_max = ETP_YMAX_V1;
		break;

	case 2:
1016 1017 1018
		if (info->fw_version == 0x020800 ||
		    info->fw_version == 0x020b00 ||
		    info->fw_version == 0x020030) {
1019 1020 1021 1022 1023
			*x_min = ETP_XMIN_V2;
			*y_min = ETP_YMIN_V2;
			*x_max = ETP_XMAX_V2;
			*y_max = ETP_YMAX_V2;
		} else {
1024 1025 1026
			int i;
			int fixed_dpi;

1027 1028
			i = (info->fw_version > 0x020800 &&
			     info->fw_version < 0x020900) ? 1 : 2;
1029

1030
			if (info->send_cmd(psmouse, ETP_FW_ID_QUERY, param))
1031 1032 1033 1034
				return -1;

			fixed_dpi = param[1] & 0x10;

1035 1036
			if (((info->fw_version >> 16) == 0x14) && fixed_dpi) {
				if (info->send_cmd(psmouse, ETP_SAMPLE_QUERY, param))
1037 1038
					return -1;

1039 1040 1041
				*x_max = (info->capabilities[1] - i) * param[1] / 2;
				*y_max = (info->capabilities[2] - i) * param[2] / 2;
			} else if (info->fw_version == 0x040216) {
1042 1043
				*x_max = 819;
				*y_max = 405;
1044
			} else if (info->fw_version == 0x040219 || info->fw_version == 0x040215) {
1045 1046 1047
				*x_max = 900;
				*y_max = 500;
			} else {
1048 1049
				*x_max = (info->capabilities[1] - i) * 64;
				*y_max = (info->capabilities[2] - i) * 64;
1050
			}
1051 1052
		}
		break;
1053 1054

	case 3:
1055
		if (info->send_cmd(psmouse, ETP_FW_ID_QUERY, param))
1056 1057 1058 1059 1060
			return -1;

		*x_max = (0x0f & param[0]) << 8 | param[1];
		*y_max = (0xf0 & param[0]) << 4 | param[2];
		break;
1061 1062

	case 4:
1063
		if (info->send_cmd(psmouse, ETP_FW_ID_QUERY, param))
1064 1065 1066 1067
			return -1;

		*x_max = (0x0f & param[0]) << 8 | param[1];
		*y_max = (0xf0 & param[0]) << 4 | param[2];
1068
		traces = info->capabilities[1];
1069 1070 1071 1072 1073
		if ((traces < 2) || (traces > *x_max))
			return -1;

		*width = *x_max / (traces - 1);
		break;
1074
	}
1075 1076

	return 0;
1077 1078
}

1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089
/*
 * (value from firmware) * 10 + 790 = dpi
 * we also have to convert dpi to dots/mm (*10/254 to avoid floating point)
 */
static unsigned int elantech_convert_res(unsigned int val)
{
	return (val * 10 + 790) * 10 / 254;
}

static int elantech_get_resolution_v4(struct psmouse *psmouse,
				      unsigned int *x_res,
1090 1091
				      unsigned int *y_res,
				      unsigned int *bus)
1092 1093 1094 1095 1096 1097 1098 1099
{
	unsigned char param[3];

	if (elantech_send_cmd(psmouse, ETP_RESOLUTION_QUERY, param))
		return -1;

	*x_res = elantech_convert_res(param[1] & 0x0f);
	*y_res = elantech_convert_res((param[1] & 0xf0) >> 4);
1100
	*bus = param[2];
1101 1102 1103 1104

	return 0;
}

1105 1106 1107