xref: /openbmc/linux/drivers/input/mouse/alps.c (revision a09221e8)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * ALPS touchpad PS/2 mouse driver
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Copyright (c) 2003 Neil Brown <neilb@cse.unsw.edu.au>
5963f626dSPeter Osterlund  * Copyright (c) 2003-2005 Peter Osterlund <petero2@telia.com>
61da177e4SLinus Torvalds  * Copyright (c) 2004 Dmitry Torokhov <dtor@mail.ru>
71da177e4SLinus Torvalds  * Copyright (c) 2005 Vojtech Pavlik <vojtech@suse.cz>
81d9f2626SSebastian Kapfer  * Copyright (c) 2009 Sebastian Kapfer <sebastian_kapfer@gmx.net>
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  * ALPS detection, tap switching and status querying info is taken from
111da177e4SLinus Torvalds  * tpconfig utility (by C. Scott Ananian and Bruce Kall).
121da177e4SLinus Torvalds  *
131da177e4SLinus Torvalds  * This program is free software; you can redistribute it and/or modify it
141da177e4SLinus Torvalds  * under the terms of the GNU General Public License version 2 as published by
151da177e4SLinus Torvalds  * the Free Software Foundation.
161da177e4SLinus Torvalds  */
171da177e4SLinus Torvalds 
185a0e3ad6STejun Heo #include <linux/slab.h>
191da177e4SLinus Torvalds #include <linux/input.h>
2001ce661fSSeth Forshee #include <linux/input/mt.h>
211da177e4SLinus Torvalds #include <linux/serio.h>
221da177e4SLinus Torvalds #include <linux/libps2.h>
231da177e4SLinus Torvalds 
241da177e4SLinus Torvalds #include "psmouse.h"
251da177e4SLinus Torvalds #include "alps.h"
261da177e4SLinus Torvalds 
2725bded7cSSeth Forshee /*
2825bded7cSSeth Forshee  * Definitions for ALPS version 3 and 4 command mode protocol
2925bded7cSSeth Forshee  */
3025bded7cSSeth Forshee #define ALPS_CMD_NIBBLE_10	0x01f2
3125bded7cSSeth Forshee 
32cd401204SKevin Cernekee #define ALPS_REG_BASE_RUSHMORE	0xc2c0
33cd401204SKevin Cernekee #define ALPS_REG_BASE_PINNACLE	0x0000
34cd401204SKevin Cernekee 
3525bded7cSSeth Forshee static const struct alps_nibble_commands alps_v3_nibble_commands[] = {
3625bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETPOLL,		0x00 }, /* 0 */
3725bded7cSSeth Forshee 	{ PSMOUSE_CMD_RESET_DIS,	0x00 }, /* 1 */
3825bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETSCALE21,	0x00 }, /* 2 */
3925bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0x0a }, /* 3 */
4025bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0x14 }, /* 4 */
4125bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0x28 }, /* 5 */
4225bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0x3c }, /* 6 */
4325bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0x50 }, /* 7 */
4425bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0x64 }, /* 8 */
4525bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0xc8 }, /* 9 */
4625bded7cSSeth Forshee 	{ ALPS_CMD_NIBBLE_10,		0x00 }, /* a */
4725bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRES,		0x00 }, /* b */
4825bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRES,		0x01 }, /* c */
4925bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRES,		0x02 }, /* d */
5025bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRES,		0x03 }, /* e */
5125bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETSCALE11,	0x00 }, /* f */
5225bded7cSSeth Forshee };
5325bded7cSSeth Forshee 
5425bded7cSSeth Forshee static const struct alps_nibble_commands alps_v4_nibble_commands[] = {
5525bded7cSSeth Forshee 	{ PSMOUSE_CMD_ENABLE,		0x00 }, /* 0 */
5625bded7cSSeth Forshee 	{ PSMOUSE_CMD_RESET_DIS,	0x00 }, /* 1 */
5725bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETSCALE21,	0x00 }, /* 2 */
5825bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0x0a }, /* 3 */
5925bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0x14 }, /* 4 */
6025bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0x28 }, /* 5 */
6125bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0x3c }, /* 6 */
6225bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0x50 }, /* 7 */
6325bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0x64 }, /* 8 */
6425bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0xc8 }, /* 9 */
6525bded7cSSeth Forshee 	{ ALPS_CMD_NIBBLE_10,		0x00 }, /* a */
6625bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRES,		0x00 }, /* b */
6725bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRES,		0x01 }, /* c */
6825bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRES,		0x02 }, /* d */
6925bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRES,		0x03 }, /* e */
7025bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETSCALE11,	0x00 }, /* f */
7125bded7cSSeth Forshee };
7225bded7cSSeth Forshee 
7395f75e91SYunkang Tang static const struct alps_nibble_commands alps_v6_nibble_commands[] = {
7495f75e91SYunkang Tang 	{ PSMOUSE_CMD_ENABLE,		0x00 }, /* 0 */
7595f75e91SYunkang Tang 	{ PSMOUSE_CMD_SETRATE,		0x0a }, /* 1 */
7695f75e91SYunkang Tang 	{ PSMOUSE_CMD_SETRATE,		0x14 }, /* 2 */
7795f75e91SYunkang Tang 	{ PSMOUSE_CMD_SETRATE,		0x28 }, /* 3 */
7895f75e91SYunkang Tang 	{ PSMOUSE_CMD_SETRATE,		0x3c }, /* 4 */
7995f75e91SYunkang Tang 	{ PSMOUSE_CMD_SETRATE,		0x50 }, /* 5 */
8095f75e91SYunkang Tang 	{ PSMOUSE_CMD_SETRATE,		0x64 }, /* 6 */
8195f75e91SYunkang Tang 	{ PSMOUSE_CMD_SETRATE,		0xc8 }, /* 7 */
8295f75e91SYunkang Tang 	{ PSMOUSE_CMD_GETID,		0x00 }, /* 8 */
8395f75e91SYunkang Tang 	{ PSMOUSE_CMD_GETINFO,		0x00 }, /* 9 */
8495f75e91SYunkang Tang 	{ PSMOUSE_CMD_SETRES,		0x00 }, /* a */
8595f75e91SYunkang Tang 	{ PSMOUSE_CMD_SETRES,		0x01 }, /* b */
8695f75e91SYunkang Tang 	{ PSMOUSE_CMD_SETRES,		0x02 }, /* c */
8795f75e91SYunkang Tang 	{ PSMOUSE_CMD_SETRES,		0x03 }, /* d */
8895f75e91SYunkang Tang 	{ PSMOUSE_CMD_SETSCALE21,	0x00 }, /* e */
8995f75e91SYunkang Tang 	{ PSMOUSE_CMD_SETSCALE11,	0x00 }, /* f */
9095f75e91SYunkang Tang };
9195f75e91SYunkang Tang 
9225bded7cSSeth Forshee 
9371bb21b6SMaxim Levitsky #define ALPS_DUALPOINT		0x02	/* touchpad has trackstick */
9471bb21b6SMaxim Levitsky #define ALPS_PASS		0x04	/* device has a pass-through port */
9571bb21b6SMaxim Levitsky 
9671bb21b6SMaxim Levitsky #define ALPS_WHEEL		0x08	/* hardware wheel present */
9771bb21b6SMaxim Levitsky #define ALPS_FW_BK_1		0x10	/* front & back buttons present */
9871bb21b6SMaxim Levitsky #define ALPS_FW_BK_2		0x20	/* front & back buttons present */
9971bb21b6SMaxim Levitsky #define ALPS_FOUR_BUTTONS	0x40	/* 4 direction button present */
1001d9f2626SSebastian Kapfer #define ALPS_PS2_INTERLEAVED	0x80	/* 3-byte PS/2 packet interleaved with
1011d9f2626SSebastian Kapfer 					   6-byte ALPS packet */
1023808843cSYunkang Tang #define ALPS_BUTTONPAD		0x200	/* device is a clickpad */
1031da177e4SLinus Torvalds 
104e38de678SHelge Deller static const struct alps_model_info alps_model_data[] = {
1058326bb57SDmitry Torokhov 	{ { 0x32, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } },	/* Toshiba Salellite Pro M10 */
1068326bb57SDmitry Torokhov 	{ { 0x33, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V1, 0x88, 0xf8, 0 } },				/* UMAX-530T */
1078326bb57SDmitry Torokhov 	{ { 0x53, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
1088326bb57SDmitry Torokhov 	{ { 0x53, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
1098326bb57SDmitry Torokhov 	{ { 0x60, 0x03, 0xc8 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },				/* HP ze1115 */
1108326bb57SDmitry Torokhov 	{ { 0x63, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
1118326bb57SDmitry Torokhov 	{ { 0x63, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
1128326bb57SDmitry Torokhov 	{ { 0x63, 0x02, 0x28 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 } },		/* Fujitsu Siemens S6010 */
1138326bb57SDmitry Torokhov 	{ { 0x63, 0x02, 0x3c }, 0x00, { ALPS_PROTO_V2, 0x8f, 0x8f, ALPS_WHEEL } },		/* Toshiba Satellite S2400-103 */
1148326bb57SDmitry Torokhov 	{ { 0x63, 0x02, 0x50 }, 0x00, { ALPS_PROTO_V2, 0xef, 0xef, ALPS_FW_BK_1 } },		/* NEC Versa L320 */
1158326bb57SDmitry Torokhov 	{ { 0x63, 0x02, 0x64 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
1168326bb57SDmitry Torokhov 	{ { 0x63, 0x03, 0xc8 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } },	/* Dell Latitude D800 */
1178326bb57SDmitry Torokhov 	{ { 0x73, 0x00, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_DUALPOINT } },		/* ThinkPad R61 8918-5QG */
1188326bb57SDmitry Torokhov 	{ { 0x73, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
1198326bb57SDmitry Torokhov 	{ { 0x73, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 } },		/* Ahtec Laptop */
1208326bb57SDmitry Torokhov 	{ { 0x20, 0x02, 0x0e }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } },	/* XXX */
1218326bb57SDmitry Torokhov 	{ { 0x22, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } },
1228326bb57SDmitry Torokhov 	{ { 0x22, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xff, 0xff, ALPS_PASS | ALPS_DUALPOINT } },	/* Dell Latitude D600 */
1231d9f2626SSebastian Kapfer 	/* Dell Latitude E5500, E6400, E6500, Precision M4400 */
1248326bb57SDmitry Torokhov 	{ { 0x62, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xcf, 0xcf,
1258326bb57SDmitry Torokhov 		ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED } },
1268326bb57SDmitry Torokhov 	{ { 0x73, 0x00, 0x14 }, 0x00, { ALPS_PROTO_V6, 0xff, 0xff, ALPS_DUALPOINT } },		/* Dell XT2 */
1278326bb57SDmitry Torokhov 	{ { 0x73, 0x02, 0x50 }, 0x00, { ALPS_PROTO_V2, 0xcf, 0xcf, ALPS_FOUR_BUTTONS } },	/* Dell Vostro 1400 */
1288326bb57SDmitry Torokhov 	{ { 0x52, 0x01, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xff, 0xff,
1298326bb57SDmitry Torokhov 		ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED } },				/* Toshiba Tecra A11-11L */
1308326bb57SDmitry Torokhov 	{ { 0x73, 0x02, 0x64 }, 0x8a, { ALPS_PROTO_V4, 0x8f, 0x8f, 0 } },
1311da177e4SLinus Torvalds };
1321da177e4SLinus Torvalds 
1333296f71cSDmitry Torokhov static const struct alps_protocol_info alps_v3_protocol_data = {
1343296f71cSDmitry Torokhov 	ALPS_PROTO_V3, 0x8f, 0x8f, ALPS_DUALPOINT
1353296f71cSDmitry Torokhov };
1363296f71cSDmitry Torokhov 
1373296f71cSDmitry Torokhov static const struct alps_protocol_info alps_v3_rushmore_data = {
1383296f71cSDmitry Torokhov 	ALPS_PROTO_V3_RUSHMORE, 0x8f, 0x8f, ALPS_DUALPOINT
1393296f71cSDmitry Torokhov };
1403296f71cSDmitry Torokhov 
1413296f71cSDmitry Torokhov static const struct alps_protocol_info alps_v5_protocol_data = {
1423296f71cSDmitry Torokhov 	ALPS_PROTO_V5, 0xc8, 0xd8, 0
1433296f71cSDmitry Torokhov };
1443296f71cSDmitry Torokhov 
1453296f71cSDmitry Torokhov static const struct alps_protocol_info alps_v7_protocol_data = {
1463296f71cSDmitry Torokhov 	ALPS_PROTO_V7, 0x48, 0x48, ALPS_DUALPOINT
1473296f71cSDmitry Torokhov };
1483296f71cSDmitry Torokhov 
14924af5cb9SKevin Cernekee static void alps_set_abs_params_st(struct alps_data *priv,
15024af5cb9SKevin Cernekee 				   struct input_dev *dev1);
15124af5cb9SKevin Cernekee static void alps_set_abs_params_mt(struct alps_data *priv,
15224af5cb9SKevin Cernekee 				   struct input_dev *dev1);
15324af5cb9SKevin Cernekee 
1541da177e4SLinus Torvalds /*
1551da177e4SLinus Torvalds  * XXX - this entry is suspicious. First byte has zero lower nibble,
1561da177e4SLinus Torvalds  * which is what a normal mouse would report. Also, the value 0x0e
1571da177e4SLinus Torvalds  * isn't valid per PS/2 spec.
1581da177e4SLinus Torvalds  */
1591da177e4SLinus Torvalds 
160d4b347b2SSeth Forshee /* Packet formats are described in Documentation/input/alps.txt */
1611da177e4SLinus Torvalds 
16299df65e7SKevin Cernekee static bool alps_is_valid_first_byte(struct alps_data *priv,
1631d9f2626SSebastian Kapfer 				     unsigned char data)
1641d9f2626SSebastian Kapfer {
16599df65e7SKevin Cernekee 	return (data & priv->mask0) == priv->byte0;
1661d9f2626SSebastian Kapfer }
1671d9f2626SSebastian Kapfer 
1681d9f2626SSebastian Kapfer static void alps_report_buttons(struct psmouse *psmouse,
1691d9f2626SSebastian Kapfer 				struct input_dev *dev1, struct input_dev *dev2,
1701d9f2626SSebastian Kapfer 				int left, int right, int middle)
1711d9f2626SSebastian Kapfer {
1721d9f2626SSebastian Kapfer 	struct input_dev *dev;
1731d9f2626SSebastian Kapfer 
1741d9f2626SSebastian Kapfer 	/*
1751d9f2626SSebastian Kapfer 	 * If shared button has already been reported on the
1761d9f2626SSebastian Kapfer 	 * other device (dev2) then this event should be also
1771d9f2626SSebastian Kapfer 	 * sent through that device.
1781d9f2626SSebastian Kapfer 	 */
1791d9f2626SSebastian Kapfer 	dev = test_bit(BTN_LEFT, dev2->key) ? dev2 : dev1;
1801d9f2626SSebastian Kapfer 	input_report_key(dev, BTN_LEFT, left);
1811d9f2626SSebastian Kapfer 
1821d9f2626SSebastian Kapfer 	dev = test_bit(BTN_RIGHT, dev2->key) ? dev2 : dev1;
1831d9f2626SSebastian Kapfer 	input_report_key(dev, BTN_RIGHT, right);
1841d9f2626SSebastian Kapfer 
1851d9f2626SSebastian Kapfer 	dev = test_bit(BTN_MIDDLE, dev2->key) ? dev2 : dev1;
1861d9f2626SSebastian Kapfer 	input_report_key(dev, BTN_MIDDLE, middle);
1871d9f2626SSebastian Kapfer 
1881d9f2626SSebastian Kapfer 	/*
1891d9f2626SSebastian Kapfer 	 * Sync the _other_ device now, we'll do the first
1901d9f2626SSebastian Kapfer 	 * device later once we report the rest of the events.
1911d9f2626SSebastian Kapfer 	 */
1921d9f2626SSebastian Kapfer 	input_sync(dev2);
1931d9f2626SSebastian Kapfer }
1941d9f2626SSebastian Kapfer 
19525bded7cSSeth Forshee static void alps_process_packet_v1_v2(struct psmouse *psmouse)
1961da177e4SLinus Torvalds {
1971da177e4SLinus Torvalds 	struct alps_data *priv = psmouse->private;
1981da177e4SLinus Torvalds 	unsigned char *packet = psmouse->packet;
1992e5b636bSDmitry Torokhov 	struct input_dev *dev = psmouse->dev;
2002e5b636bSDmitry Torokhov 	struct input_dev *dev2 = priv->dev2;
2011da177e4SLinus Torvalds 	int x, y, z, ges, fin, left, right, middle;
202c30b4c10SIvan Casado Ruiz 	int back = 0, forward = 0;
2031da177e4SLinus Torvalds 
20499df65e7SKevin Cernekee 	if (priv->proto_version == ALPS_PROTO_V1) {
205d2f4012fSYotam Medini 		left = packet[2] & 0x10;
206d2f4012fSYotam Medini 		right = packet[2] & 0x08;
2071da177e4SLinus Torvalds 		middle = 0;
2081da177e4SLinus Torvalds 		x = packet[1] | ((packet[0] & 0x07) << 7);
2091da177e4SLinus Torvalds 		y = packet[4] | ((packet[3] & 0x07) << 7);
2101da177e4SLinus Torvalds 		z = packet[5];
2111da177e4SLinus Torvalds 	} else {
2121da177e4SLinus Torvalds 		left = packet[3] & 1;
2131da177e4SLinus Torvalds 		right = packet[3] & 2;
2141da177e4SLinus Torvalds 		middle = packet[3] & 4;
2151da177e4SLinus Torvalds 		x = packet[1] | ((packet[2] & 0x78) << (7 - 3));
2161da177e4SLinus Torvalds 		y = packet[4] | ((packet[3] & 0x70) << (7 - 4));
2171da177e4SLinus Torvalds 		z = packet[5];
2181da177e4SLinus Torvalds 	}
2191da177e4SLinus Torvalds 
22099df65e7SKevin Cernekee 	if (priv->flags & ALPS_FW_BK_1) {
2213c00bb96SLaszlo Kajan 		back = packet[0] & 0x10;
2223c00bb96SLaszlo Kajan 		forward = packet[2] & 4;
223c30b4c10SIvan Casado Ruiz 	}
224c30b4c10SIvan Casado Ruiz 
22599df65e7SKevin Cernekee 	if (priv->flags & ALPS_FW_BK_2) {
226c30b4c10SIvan Casado Ruiz 		back = packet[3] & 4;
227c30b4c10SIvan Casado Ruiz 		forward = packet[2] & 4;
228c30b4c10SIvan Casado Ruiz 		if ((middle = forward && back))
229c30b4c10SIvan Casado Ruiz 			forward = back = 0;
230c30b4c10SIvan Casado Ruiz 	}
231c30b4c10SIvan Casado Ruiz 
2321da177e4SLinus Torvalds 	ges = packet[2] & 1;
2331da177e4SLinus Torvalds 	fin = packet[2] & 2;
2341da177e4SLinus Torvalds 
23599df65e7SKevin Cernekee 	if ((priv->flags & ALPS_DUALPOINT) && z == 127) {
2361da177e4SLinus Torvalds 		input_report_rel(dev2, REL_X,  (x > 383 ? (x - 768) : x));
2371da177e4SLinus Torvalds 		input_report_rel(dev2, REL_Y, -(y > 255 ? (y - 512) : y));
238d7ed5d88SUlrich Dangel 
2391d9f2626SSebastian Kapfer 		alps_report_buttons(psmouse, dev2, dev, left, right, middle);
240d7ed5d88SUlrich Dangel 
2411da177e4SLinus Torvalds 		input_sync(dev2);
2421da177e4SLinus Torvalds 		return;
2431da177e4SLinus Torvalds 	}
2441da177e4SLinus Torvalds 
2451d9f2626SSebastian Kapfer 	alps_report_buttons(psmouse, dev, dev2, left, right, middle);
246d7ed5d88SUlrich Dangel 
2471da177e4SLinus Torvalds 	/* Convert hardware tap to a reasonable Z value */
24871bb21b6SMaxim Levitsky 	if (ges && !fin)
24971bb21b6SMaxim Levitsky 		z = 40;
2501da177e4SLinus Torvalds 
2511da177e4SLinus Torvalds 	/*
2521da177e4SLinus Torvalds 	 * A "tap and drag" operation is reported by the hardware as a transition
2531da177e4SLinus Torvalds 	 * from (!fin && ges) to (fin && ges). This should be translated to the
2541da177e4SLinus Torvalds 	 * sequence Z>0, Z==0, Z>0, so the Z==0 event has to be generated manually.
2551da177e4SLinus Torvalds 	 */
2561da177e4SLinus Torvalds 	if (ges && fin && !priv->prev_fin) {
2571da177e4SLinus Torvalds 		input_report_abs(dev, ABS_X, x);
2581da177e4SLinus Torvalds 		input_report_abs(dev, ABS_Y, y);
2591da177e4SLinus Torvalds 		input_report_abs(dev, ABS_PRESSURE, 0);
2601da177e4SLinus Torvalds 		input_report_key(dev, BTN_TOOL_FINGER, 0);
2611da177e4SLinus Torvalds 		input_sync(dev);
2621da177e4SLinus Torvalds 	}
2631da177e4SLinus Torvalds 	priv->prev_fin = fin;
2641da177e4SLinus Torvalds 
26571bb21b6SMaxim Levitsky 	if (z > 30)
26671bb21b6SMaxim Levitsky 		input_report_key(dev, BTN_TOUCH, 1);
26771bb21b6SMaxim Levitsky 	if (z < 25)
26871bb21b6SMaxim Levitsky 		input_report_key(dev, BTN_TOUCH, 0);
2691da177e4SLinus Torvalds 
2701da177e4SLinus Torvalds 	if (z > 0) {
2711da177e4SLinus Torvalds 		input_report_abs(dev, ABS_X, x);
2721da177e4SLinus Torvalds 		input_report_abs(dev, ABS_Y, y);
2731da177e4SLinus Torvalds 	}
2741da177e4SLinus Torvalds 
2751da177e4SLinus Torvalds 	input_report_abs(dev, ABS_PRESSURE, z);
2761da177e4SLinus Torvalds 	input_report_key(dev, BTN_TOOL_FINGER, z > 0);
2771da177e4SLinus Torvalds 
27899df65e7SKevin Cernekee 	if (priv->flags & ALPS_WHEEL)
279e6c047b9SVojtech Pavlik 		input_report_rel(dev, REL_WHEEL, ((packet[2] << 1) & 0x08) - ((packet[0] >> 4) & 0x07));
2801da177e4SLinus Torvalds 
28199df65e7SKevin Cernekee 	if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
282c30b4c10SIvan Casado Ruiz 		input_report_key(dev, BTN_FORWARD, forward);
283c30b4c10SIvan Casado Ruiz 		input_report_key(dev, BTN_BACK, back);
2841da177e4SLinus Torvalds 	}
2851da177e4SLinus Torvalds 
28699df65e7SKevin Cernekee 	if (priv->flags & ALPS_FOUR_BUTTONS) {
28771bb21b6SMaxim Levitsky 		input_report_key(dev, BTN_0, packet[2] & 4);
28871bb21b6SMaxim Levitsky 		input_report_key(dev, BTN_1, packet[0] & 0x10);
28971bb21b6SMaxim Levitsky 		input_report_key(dev, BTN_2, packet[3] & 4);
29071bb21b6SMaxim Levitsky 		input_report_key(dev, BTN_3, packet[0] & 0x20);
29171bb21b6SMaxim Levitsky 	}
29271bb21b6SMaxim Levitsky 
2931da177e4SLinus Torvalds 	input_sync(dev);
2941da177e4SLinus Torvalds }
2951da177e4SLinus Torvalds 
29601ce661fSSeth Forshee /*
297ee65d4b3SYunkang Tang  * Process bitmap data for V5 protocols. Return value is null.
298ee65d4b3SYunkang Tang  *
299ee65d4b3SYunkang Tang  * The bitmaps don't have enough data to track fingers, so this function
300ee65d4b3SYunkang Tang  * only generates points representing a bounding box of at most two contacts.
30102d04254SHans de Goede  * These two points are returned in fields->mt.
302ee65d4b3SYunkang Tang  */
303ee65d4b3SYunkang Tang static void alps_process_bitmap_dolphin(struct alps_data *priv,
30402d04254SHans de Goede 					struct alps_fields *fields)
305ee65d4b3SYunkang Tang {
306ee65d4b3SYunkang Tang 	int box_middle_x, box_middle_y;
307ee65d4b3SYunkang Tang 	unsigned int x_map, y_map;
308ee65d4b3SYunkang Tang 	unsigned char start_bit, end_bit;
309ee65d4b3SYunkang Tang 	unsigned char x_msb, x_lsb, y_msb, y_lsb;
310ee65d4b3SYunkang Tang 
311ee65d4b3SYunkang Tang 	x_map = fields->x_map;
312ee65d4b3SYunkang Tang 	y_map = fields->y_map;
313ee65d4b3SYunkang Tang 
314ee65d4b3SYunkang Tang 	if (!x_map || !y_map)
315ee65d4b3SYunkang Tang 		return;
316ee65d4b3SYunkang Tang 
317ee65d4b3SYunkang Tang 	/* Get Most-significant and Least-significant bit */
318ee65d4b3SYunkang Tang 	x_msb = fls(x_map);
319ee65d4b3SYunkang Tang 	x_lsb = ffs(x_map);
320ee65d4b3SYunkang Tang 	y_msb = fls(y_map);
321ee65d4b3SYunkang Tang 	y_lsb = ffs(y_map);
322ee65d4b3SYunkang Tang 
323ee65d4b3SYunkang Tang 	/* Most-significant bit should never exceed max sensor line number */
324ee65d4b3SYunkang Tang 	if (x_msb > priv->x_bits || y_msb > priv->y_bits)
325ee65d4b3SYunkang Tang 		return;
326ee65d4b3SYunkang Tang 
327ee65d4b3SYunkang Tang 	if (fields->fingers > 1) {
328ee65d4b3SYunkang Tang 		start_bit = priv->x_bits - x_msb;
329ee65d4b3SYunkang Tang 		end_bit = priv->x_bits - x_lsb;
330ee65d4b3SYunkang Tang 		box_middle_x = (priv->x_max * (start_bit + end_bit)) /
331ee65d4b3SYunkang Tang 				(2 * (priv->x_bits - 1));
332ee65d4b3SYunkang Tang 
333ee65d4b3SYunkang Tang 		start_bit = y_lsb - 1;
334ee65d4b3SYunkang Tang 		end_bit = y_msb - 1;
335ee65d4b3SYunkang Tang 		box_middle_y = (priv->y_max * (start_bit + end_bit)) /
336ee65d4b3SYunkang Tang 				(2 * (priv->y_bits - 1));
33702d04254SHans de Goede 		fields->mt[0] = fields->st;
33802d04254SHans de Goede 		fields->mt[1].x = 2 * box_middle_x - fields->mt[0].x;
33902d04254SHans de Goede 		fields->mt[1].y = 2 * box_middle_y - fields->mt[0].y;
340ee65d4b3SYunkang Tang 	}
341ee65d4b3SYunkang Tang }
342ee65d4b3SYunkang Tang 
343036e6c7bSHans de Goede static void alps_get_bitmap_points(unsigned int map,
344036e6c7bSHans de Goede 				   struct alps_bitmap_point *low,
345036e6c7bSHans de Goede 				   struct alps_bitmap_point *high,
346036e6c7bSHans de Goede 				   int *fingers)
347036e6c7bSHans de Goede {
348036e6c7bSHans de Goede 	struct alps_bitmap_point *point;
349036e6c7bSHans de Goede 	int i, bit, prev_bit = 0;
350036e6c7bSHans de Goede 
351036e6c7bSHans de Goede 	point = low;
352036e6c7bSHans de Goede 	for (i = 0; map != 0; i++, map >>= 1) {
353036e6c7bSHans de Goede 		bit = map & 1;
354036e6c7bSHans de Goede 		if (bit) {
355036e6c7bSHans de Goede 			if (!prev_bit) {
356036e6c7bSHans de Goede 				point->start_bit = i;
357105affbfSHans de Goede 				point->num_bits = 0;
358036e6c7bSHans de Goede 				(*fingers)++;
359036e6c7bSHans de Goede 			}
360036e6c7bSHans de Goede 			point->num_bits++;
361036e6c7bSHans de Goede 		} else {
362036e6c7bSHans de Goede 			if (prev_bit)
363036e6c7bSHans de Goede 				point = high;
364036e6c7bSHans de Goede 		}
365036e6c7bSHans de Goede 		prev_bit = bit;
366036e6c7bSHans de Goede 	}
367036e6c7bSHans de Goede }
368036e6c7bSHans de Goede 
369ee65d4b3SYunkang Tang /*
37001ce661fSSeth Forshee  * Process bitmap data from v3 and v4 protocols. Returns the number of
37101ce661fSSeth Forshee  * fingers detected. A return value of 0 means at least one of the
37201ce661fSSeth Forshee  * bitmaps was empty.
37301ce661fSSeth Forshee  *
37401ce661fSSeth Forshee  * The bitmaps don't have enough data to track fingers, so this function
37501ce661fSSeth Forshee  * only generates points representing a bounding box of all contacts.
37602d04254SHans de Goede  * These points are returned in fields->mt when the return value
37701ce661fSSeth Forshee  * is greater than 0.
37801ce661fSSeth Forshee  */
3797a9f73e7SKevin Cernekee static int alps_process_bitmap(struct alps_data *priv,
38002d04254SHans de Goede 			       struct alps_fields *fields)
38101ce661fSSeth Forshee {
382036e6c7bSHans de Goede 	int i, fingers_x = 0, fingers_y = 0, fingers;
38301ce661fSSeth Forshee 	struct alps_bitmap_point x_low = {0,}, x_high = {0,};
38401ce661fSSeth Forshee 	struct alps_bitmap_point y_low = {0,}, y_high = {0,};
38501ce661fSSeth Forshee 
38602d04254SHans de Goede 	if (!fields->x_map || !fields->y_map)
38701ce661fSSeth Forshee 		return 0;
38801ce661fSSeth Forshee 
38902d04254SHans de Goede 	alps_get_bitmap_points(fields->x_map, &x_low, &x_high, &fingers_x);
39002d04254SHans de Goede 	alps_get_bitmap_points(fields->y_map, &y_low, &y_high, &fingers_y);
39101ce661fSSeth Forshee 
39201ce661fSSeth Forshee 	/*
39301ce661fSSeth Forshee 	 * Fingers can overlap, so we use the maximum count of fingers
39401ce661fSSeth Forshee 	 * on either axis as the finger count.
39501ce661fSSeth Forshee 	 */
39601ce661fSSeth Forshee 	fingers = max(fingers_x, fingers_y);
39701ce661fSSeth Forshee 
39801ce661fSSeth Forshee 	/*
39920bea68bSHans de Goede 	 * If an axis reports only a single contact, we have overlapping or
40020bea68bSHans de Goede 	 * adjacent fingers. Divide the single contact between the two points.
40101ce661fSSeth Forshee 	 */
40201ce661fSSeth Forshee 	if (fingers_x == 1) {
40328835f45SHans de Goede 		i = (x_low.num_bits - 1) / 2;
40401ce661fSSeth Forshee 		x_low.num_bits = x_low.num_bits - i;
40501ce661fSSeth Forshee 		x_high.start_bit = x_low.start_bit + i;
40601ce661fSSeth Forshee 		x_high.num_bits = max(i, 1);
40720bea68bSHans de Goede 	}
40820bea68bSHans de Goede 	if (fingers_y == 1) {
40928835f45SHans de Goede 		i = (y_low.num_bits - 1) / 2;
41001ce661fSSeth Forshee 		y_low.num_bits = y_low.num_bits - i;
41101ce661fSSeth Forshee 		y_high.start_bit = y_low.start_bit + i;
41201ce661fSSeth Forshee 		y_high.num_bits = max(i, 1);
41301ce661fSSeth Forshee 	}
41401ce661fSSeth Forshee 
41502d04254SHans de Goede 	fields->mt[0].x =
41602d04254SHans de Goede 		(priv->x_max * (2 * x_low.start_bit + x_low.num_bits - 1)) /
4177a9f73e7SKevin Cernekee 		(2 * (priv->x_bits - 1));
41802d04254SHans de Goede 	fields->mt[0].y =
41902d04254SHans de Goede 		(priv->y_max * (2 * y_low.start_bit + y_low.num_bits - 1)) /
4207a9f73e7SKevin Cernekee 		(2 * (priv->y_bits - 1));
42101ce661fSSeth Forshee 
42202d04254SHans de Goede 	fields->mt[1].x =
42302d04254SHans de Goede 		(priv->x_max * (2 * x_high.start_bit + x_high.num_bits - 1)) /
4247a9f73e7SKevin Cernekee 		(2 * (priv->x_bits - 1));
42502d04254SHans de Goede 	fields->mt[1].y =
42602d04254SHans de Goede 		(priv->y_max * (2 * y_high.start_bit + y_high.num_bits - 1)) /
4277a9f73e7SKevin Cernekee 		(2 * (priv->y_bits - 1));
42801ce661fSSeth Forshee 
42940e8f53bSHans de Goede 	/* y-bitmap order is reversed, except on rushmore */
430fb2dd7a6SDmitry Torokhov 	if (priv->proto_version != ALPS_PROTO_V3_RUSHMORE) {
43102d04254SHans de Goede 		fields->mt[0].y = priv->y_max - fields->mt[0].y;
43202d04254SHans de Goede 		fields->mt[1].y = priv->y_max - fields->mt[1].y;
43340e8f53bSHans de Goede 	}
43440e8f53bSHans de Goede 
43501ce661fSSeth Forshee 	return fingers;
43601ce661fSSeth Forshee }
43701ce661fSSeth Forshee 
438cdf333efSHans de Goede static void alps_set_slot(struct input_dev *dev, int slot, int x, int y)
43901ce661fSSeth Forshee {
44001ce661fSSeth Forshee 	input_mt_slot(dev, slot);
441cdf333efSHans de Goede 	input_mt_report_slot_state(dev, MT_TOOL_FINGER, true);
44201ce661fSSeth Forshee 	input_report_abs(dev, ABS_MT_POSITION_X, x);
44301ce661fSSeth Forshee 	input_report_abs(dev, ABS_MT_POSITION_Y, y);
44401ce661fSSeth Forshee }
44501ce661fSSeth Forshee 
446cdf333efSHans de Goede static void alps_report_mt_data(struct psmouse *psmouse, int n)
44701ce661fSSeth Forshee {
44802d04254SHans de Goede 	struct alps_data *priv = psmouse->private;
44902d04254SHans de Goede 	struct input_dev *dev = psmouse->dev;
45002d04254SHans de Goede 	struct alps_fields *f = &priv->f;
451cdf333efSHans de Goede 	int i, slot[MAX_TOUCHES];
45202d04254SHans de Goede 
453448c7f38SHenrik Rydberg 	input_mt_assign_slots(dev, slot, f->mt, n, 0);
454cdf333efSHans de Goede 	for (i = 0; i < n; i++)
455cdf333efSHans de Goede 		alps_set_slot(dev, slot[i], f->mt[i].x, f->mt[i].y);
456cdf333efSHans de Goede 
457cdf333efSHans de Goede 	input_mt_sync_frame(dev);
45801ce661fSSeth Forshee }
45901ce661fSSeth Forshee 
46068c21870SHans de Goede static void alps_report_semi_mt_data(struct psmouse *psmouse, int fingers)
46168c21870SHans de Goede {
46268c21870SHans de Goede 	struct alps_data *priv = psmouse->private;
46368c21870SHans de Goede 	struct input_dev *dev = psmouse->dev;
46468c21870SHans de Goede 	struct alps_fields *f = &priv->f;
46568c21870SHans de Goede 
46668c21870SHans de Goede 	/* Use st data when we don't have mt data */
46768c21870SHans de Goede 	if (fingers < 2) {
46868c21870SHans de Goede 		f->mt[0].x = f->st.x;
46968c21870SHans de Goede 		f->mt[0].y = f->st.y;
47068c21870SHans de Goede 		fingers = f->pressure > 0 ? 1 : 0;
47168c21870SHans de Goede 	}
47268c21870SHans de Goede 
47399d9996cSHans de Goede 	alps_report_mt_data(psmouse, (fingers <= 2) ? fingers : 2);
47468c21870SHans de Goede 
47568c21870SHans de Goede 	input_mt_report_finger_count(dev, fingers);
47668c21870SHans de Goede 
47768c21870SHans de Goede 	input_report_key(dev, BTN_LEFT, f->left);
47868c21870SHans de Goede 	input_report_key(dev, BTN_RIGHT, f->right);
47968c21870SHans de Goede 	input_report_key(dev, BTN_MIDDLE, f->middle);
48068c21870SHans de Goede 
48168c21870SHans de Goede 	input_report_abs(dev, ABS_PRESSURE, f->pressure);
48268c21870SHans de Goede 
48368c21870SHans de Goede 	input_sync(dev);
48468c21870SHans de Goede }
48568c21870SHans de Goede 
48625bded7cSSeth Forshee static void alps_process_trackstick_packet_v3(struct psmouse *psmouse)
48725bded7cSSeth Forshee {
48825bded7cSSeth Forshee 	struct alps_data *priv = psmouse->private;
48925bded7cSSeth Forshee 	unsigned char *packet = psmouse->packet;
49025bded7cSSeth Forshee 	struct input_dev *dev = priv->dev2;
49125bded7cSSeth Forshee 	int x, y, z, left, right, middle;
49225bded7cSSeth Forshee 
49334412ba2SPali Rohár 	/* It should be a DualPoint when received trackstick packet */
49434412ba2SPali Rohár 	if (!(priv->flags & ALPS_DUALPOINT)) {
49534412ba2SPali Rohár 		psmouse_warn(psmouse,
49634412ba2SPali Rohár 			     "Rejected trackstick packet from non DualPoint device");
49734412ba2SPali Rohár 		return;
49834412ba2SPali Rohár 	}
49934412ba2SPali Rohár 
50025bded7cSSeth Forshee 	/* Sanity check packet */
50125bded7cSSeth Forshee 	if (!(packet[0] & 0x40)) {
50225bded7cSSeth Forshee 		psmouse_dbg(psmouse, "Bad trackstick packet, discarding\n");
50325bded7cSSeth Forshee 		return;
50425bded7cSSeth Forshee 	}
50525bded7cSSeth Forshee 
50625bded7cSSeth Forshee 	/*
50725bded7cSSeth Forshee 	 * There's a special packet that seems to indicate the end
50825bded7cSSeth Forshee 	 * of a stream of trackstick data. Filter these out.
50925bded7cSSeth Forshee 	 */
51025bded7cSSeth Forshee 	if (packet[1] == 0x7f && packet[2] == 0x7f && packet[4] == 0x7f)
51125bded7cSSeth Forshee 		return;
51225bded7cSSeth Forshee 
51325bded7cSSeth Forshee 	x = (s8)(((packet[0] & 0x20) << 2) | (packet[1] & 0x7f));
51425bded7cSSeth Forshee 	y = (s8)(((packet[0] & 0x10) << 3) | (packet[2] & 0x7f));
51525bded7cSSeth Forshee 	z = (packet[4] & 0x7c) >> 2;
51625bded7cSSeth Forshee 
51725bded7cSSeth Forshee 	/*
51825bded7cSSeth Forshee 	 * The x and y values tend to be quite large, and when used
51925bded7cSSeth Forshee 	 * alone the trackstick is difficult to use. Scale them down
52025bded7cSSeth Forshee 	 * to compensate.
52125bded7cSSeth Forshee 	 */
52225bded7cSSeth Forshee 	x /= 8;
52325bded7cSSeth Forshee 	y /= 8;
52425bded7cSSeth Forshee 
52525bded7cSSeth Forshee 	input_report_rel(dev, REL_X, x);
52625bded7cSSeth Forshee 	input_report_rel(dev, REL_Y, -y);
52725bded7cSSeth Forshee 
52825bded7cSSeth Forshee 	/*
52925bded7cSSeth Forshee 	 * Most ALPS models report the trackstick buttons in the touchpad
53025bded7cSSeth Forshee 	 * packets, but a few report them here. No reliable way has been
53125bded7cSSeth Forshee 	 * found to differentiate between the models upfront, so we enable
53225bded7cSSeth Forshee 	 * the quirk in response to seeing a button press in the trackstick
53325bded7cSSeth Forshee 	 * packet.
53425bded7cSSeth Forshee 	 */
53525bded7cSSeth Forshee 	left = packet[3] & 0x01;
53625bded7cSSeth Forshee 	right = packet[3] & 0x02;
53725bded7cSSeth Forshee 	middle = packet[3] & 0x04;
53825bded7cSSeth Forshee 
53925bded7cSSeth Forshee 	if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) &&
54025bded7cSSeth Forshee 	    (left || right || middle))
54125bded7cSSeth Forshee 		priv->quirks |= ALPS_QUIRK_TRACKSTICK_BUTTONS;
54225bded7cSSeth Forshee 
54325bded7cSSeth Forshee 	if (priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) {
54425bded7cSSeth Forshee 		input_report_key(dev, BTN_LEFT, left);
54525bded7cSSeth Forshee 		input_report_key(dev, BTN_RIGHT, right);
54625bded7cSSeth Forshee 		input_report_key(dev, BTN_MIDDLE, middle);
54725bded7cSSeth Forshee 	}
54825bded7cSSeth Forshee 
54925bded7cSSeth Forshee 	input_sync(dev);
55025bded7cSSeth Forshee 	return;
55125bded7cSSeth Forshee }
55225bded7cSSeth Forshee 
553f85e5001SKevin Cernekee static void alps_decode_buttons_v3(struct alps_fields *f, unsigned char *p)
554f85e5001SKevin Cernekee {
555f85e5001SKevin Cernekee 	f->left = !!(p[3] & 0x01);
556f85e5001SKevin Cernekee 	f->right = !!(p[3] & 0x02);
557f85e5001SKevin Cernekee 	f->middle = !!(p[3] & 0x04);
558f85e5001SKevin Cernekee 
559f85e5001SKevin Cernekee 	f->ts_left = !!(p[3] & 0x10);
560f85e5001SKevin Cernekee 	f->ts_right = !!(p[3] & 0x20);
561f85e5001SKevin Cernekee 	f->ts_middle = !!(p[3] & 0x40);
562f85e5001SKevin Cernekee }
563f85e5001SKevin Cernekee 
56438c11eaaSHans de Goede static int alps_decode_pinnacle(struct alps_fields *f, unsigned char *p,
565ee65d4b3SYunkang Tang 				 struct psmouse *psmouse)
566f85e5001SKevin Cernekee {
567f85e5001SKevin Cernekee 	f->first_mp = !!(p[4] & 0x40);
568f85e5001SKevin Cernekee 	f->is_mp = !!(p[0] & 0x40);
569f85e5001SKevin Cernekee 
570f85e5001SKevin Cernekee 	f->fingers = (p[5] & 0x3) + 1;
571f85e5001SKevin Cernekee 	f->x_map = ((p[4] & 0x7e) << 8) |
572f85e5001SKevin Cernekee 		   ((p[1] & 0x7f) << 2) |
573f85e5001SKevin Cernekee 		   ((p[0] & 0x30) >> 4);
574f85e5001SKevin Cernekee 	f->y_map = ((p[3] & 0x70) << 4) |
575f85e5001SKevin Cernekee 		   ((p[2] & 0x7f) << 1) |
576f85e5001SKevin Cernekee 		   (p[4] & 0x01);
577f85e5001SKevin Cernekee 
57802d04254SHans de Goede 	f->st.x = ((p[1] & 0x7f) << 4) | ((p[4] & 0x30) >> 2) |
579f85e5001SKevin Cernekee 	       ((p[0] & 0x30) >> 4);
58002d04254SHans de Goede 	f->st.y = ((p[2] & 0x7f) << 4) | (p[4] & 0x0f);
58102d04254SHans de Goede 	f->pressure = p[5] & 0x7f;
582f85e5001SKevin Cernekee 
583f85e5001SKevin Cernekee 	alps_decode_buttons_v3(f, p);
58438c11eaaSHans de Goede 
58538c11eaaSHans de Goede 	return 0;
586f85e5001SKevin Cernekee }
587f85e5001SKevin Cernekee 
58838c11eaaSHans de Goede static int alps_decode_rushmore(struct alps_fields *f, unsigned char *p,
589ee65d4b3SYunkang Tang 				 struct psmouse *psmouse)
5901302bac3SKevin Cernekee {
591ee65d4b3SYunkang Tang 	alps_decode_pinnacle(f, p, psmouse);
5921302bac3SKevin Cernekee 
593f105e34aSYunkang Tang 	/* Rushmore's packet decode has a bit difference with Pinnacle's */
594f105e34aSYunkang Tang 	f->is_mp = !!(p[5] & 0x40);
595f105e34aSYunkang Tang 	f->fingers = max((p[5] & 0x3), ((p[5] >> 2) & 0x3)) + 1;
5961302bac3SKevin Cernekee 	f->x_map |= (p[5] & 0x10) << 11;
5971302bac3SKevin Cernekee 	f->y_map |= (p[5] & 0x20) << 6;
59838c11eaaSHans de Goede 
59938c11eaaSHans de Goede 	return 0;
6001302bac3SKevin Cernekee }
6011302bac3SKevin Cernekee 
60238c11eaaSHans de Goede static int alps_decode_dolphin(struct alps_fields *f, unsigned char *p,
603ee65d4b3SYunkang Tang 				struct psmouse *psmouse)
60475af9e56SDave Turvene {
605ee65d4b3SYunkang Tang 	u64 palm_data = 0;
606ee65d4b3SYunkang Tang 	struct alps_data *priv = psmouse->private;
607ee65d4b3SYunkang Tang 
60875af9e56SDave Turvene 	f->first_mp = !!(p[0] & 0x02);
60975af9e56SDave Turvene 	f->is_mp = !!(p[0] & 0x20);
61075af9e56SDave Turvene 
611ee65d4b3SYunkang Tang 	if (!f->is_mp) {
61202d04254SHans de Goede 		f->st.x = ((p[1] & 0x7f) | ((p[4] & 0x0f) << 7));
61302d04254SHans de Goede 		f->st.y = ((p[2] & 0x7f) | ((p[4] & 0xf0) << 3));
61402d04254SHans de Goede 		f->pressure = (p[0] & 4) ? 0 : p[5] & 0x7f;
61575af9e56SDave Turvene 		alps_decode_buttons_v3(f, p);
616ee65d4b3SYunkang Tang 	} else {
617ee65d4b3SYunkang Tang 		f->fingers = ((p[0] & 0x6) >> 1 |
618ee65d4b3SYunkang Tang 		     (p[0] & 0x10) >> 2);
619ee65d4b3SYunkang Tang 
620ee65d4b3SYunkang Tang 		palm_data = (p[1] & 0x7f) |
621ee65d4b3SYunkang Tang 			    ((p[2] & 0x7f) << 7) |
622ee65d4b3SYunkang Tang 			    ((p[4] & 0x7f) << 14) |
623ee65d4b3SYunkang Tang 			    ((p[5] & 0x7f) << 21) |
624ee65d4b3SYunkang Tang 			    ((p[3] & 0x07) << 28) |
625ee65d4b3SYunkang Tang 			    (((u64)p[3] & 0x70) << 27) |
626ee65d4b3SYunkang Tang 			    (((u64)p[0] & 0x01) << 34);
627ee65d4b3SYunkang Tang 
628ee65d4b3SYunkang Tang 		/* Y-profile is stored in P(0) to p(n-1), n = y_bits; */
629ee65d4b3SYunkang Tang 		f->y_map = palm_data & (BIT(priv->y_bits) - 1);
630ee65d4b3SYunkang Tang 
631ee65d4b3SYunkang Tang 		/* X-profile is stored in p(n) to p(n+m-1), m = x_bits; */
632ee65d4b3SYunkang Tang 		f->x_map = (palm_data >> priv->y_bits) &
633ee65d4b3SYunkang Tang 			   (BIT(priv->x_bits) - 1);
634ee65d4b3SYunkang Tang 	}
63538c11eaaSHans de Goede 
63638c11eaaSHans de Goede 	return 0;
63775af9e56SDave Turvene }
63875af9e56SDave Turvene 
639ee65d4b3SYunkang Tang static void alps_process_touchpad_packet_v3_v5(struct psmouse *psmouse)
64025bded7cSSeth Forshee {
64125bded7cSSeth Forshee 	struct alps_data *priv = psmouse->private;
64225bded7cSSeth Forshee 	unsigned char *packet = psmouse->packet;
64325bded7cSSeth Forshee 	struct input_dev *dev2 = priv->dev2;
64402d04254SHans de Goede 	struct alps_fields *f = &priv->f;
64502d04254SHans de Goede 	int fingers = 0;
646f85e5001SKevin Cernekee 
64702d04254SHans de Goede 	memset(f, 0, sizeof(*f));
64802d04254SHans de Goede 
64902d04254SHans de Goede 	priv->decode_fields(f, packet, psmouse);
65025bded7cSSeth Forshee 
65125bded7cSSeth Forshee 	/*
65201ce661fSSeth Forshee 	 * There's no single feature of touchpad position and bitmap packets
65301ce661fSSeth Forshee 	 * that can be used to distinguish between them. We rely on the fact
65401ce661fSSeth Forshee 	 * that a bitmap packet should always follow a position packet with
65501ce661fSSeth Forshee 	 * bit 6 of packet[4] set.
65625bded7cSSeth Forshee 	 */
65725bded7cSSeth Forshee 	if (priv->multi_packet) {
65825bded7cSSeth Forshee 		/*
65925bded7cSSeth Forshee 		 * Sometimes a position packet will indicate a multi-packet
66025bded7cSSeth Forshee 		 * sequence, but then what follows is another position
66125bded7cSSeth Forshee 		 * packet. Check for this, and when it happens process the
66225bded7cSSeth Forshee 		 * position packet as usual.
66325bded7cSSeth Forshee 		 */
66402d04254SHans de Goede 		if (f->is_mp) {
66502d04254SHans de Goede 			fingers = f->fingers;
666fb2dd7a6SDmitry Torokhov 			if (priv->proto_version == ALPS_PROTO_V3 ||
667fb2dd7a6SDmitry Torokhov 			    priv->proto_version == ALPS_PROTO_V3_RUSHMORE) {
66802d04254SHans de Goede 				if (alps_process_bitmap(priv, f) == 0)
66920bea68bSHans de Goede 					fingers = 0; /* Use st data */
67001ce661fSSeth Forshee 
67101ce661fSSeth Forshee 				/* Now process position packet */
67202d04254SHans de Goede 				priv->decode_fields(f, priv->multi_data,
673ee65d4b3SYunkang Tang 						    psmouse);
674ee65d4b3SYunkang Tang 			} else {
675ee65d4b3SYunkang Tang 				/*
676ee65d4b3SYunkang Tang 				 * Because Dolphin uses position packet's
677ee65d4b3SYunkang Tang 				 * coordinate data as Pt1 and uses it to
678ee65d4b3SYunkang Tang 				 * calculate Pt2, so we need to do position
679ee65d4b3SYunkang Tang 				 * packet decode first.
680ee65d4b3SYunkang Tang 				 */
68102d04254SHans de Goede 				priv->decode_fields(f, priv->multi_data,
682ee65d4b3SYunkang Tang 						    psmouse);
683ee65d4b3SYunkang Tang 
684ee65d4b3SYunkang Tang 				/*
685ee65d4b3SYunkang Tang 				 * Since Dolphin's finger number is reliable,
686ee65d4b3SYunkang Tang 				 * there is no need to compare with bmap_fn.
687ee65d4b3SYunkang Tang 				 */
68802d04254SHans de Goede 				alps_process_bitmap_dolphin(priv, f);
689ee65d4b3SYunkang Tang 			}
69001ce661fSSeth Forshee 		} else {
69101ce661fSSeth Forshee 			priv->multi_packet = 0;
69225bded7cSSeth Forshee 		}
69325bded7cSSeth Forshee 	}
69425bded7cSSeth Forshee 
69501ce661fSSeth Forshee 	/*
69601ce661fSSeth Forshee 	 * Bit 6 of byte 0 is not usually set in position packets. The only
69701ce661fSSeth Forshee 	 * times it seems to be set is in situations where the data is
69801ce661fSSeth Forshee 	 * suspect anyway, e.g. a palm resting flat on the touchpad. Given
69901ce661fSSeth Forshee 	 * this combined with the fact that this bit is useful for filtering
70001ce661fSSeth Forshee 	 * out misidentified bitmap packets, we reject anything with this
70101ce661fSSeth Forshee 	 * bit set.
70201ce661fSSeth Forshee 	 */
70302d04254SHans de Goede 	if (f->is_mp)
70401ce661fSSeth Forshee 		return;
70501ce661fSSeth Forshee 
70602d04254SHans de Goede 	if (!priv->multi_packet && f->first_mp) {
70725bded7cSSeth Forshee 		priv->multi_packet = 1;
70801ce661fSSeth Forshee 		memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
70901ce661fSSeth Forshee 		return;
71001ce661fSSeth Forshee 	}
71101ce661fSSeth Forshee 
71225bded7cSSeth Forshee 	priv->multi_packet = 0;
71325bded7cSSeth Forshee 
71425bded7cSSeth Forshee 	/*
71525bded7cSSeth Forshee 	 * Sometimes the hardware sends a single packet with z = 0
71625bded7cSSeth Forshee 	 * in the middle of a stream. Real releases generate packets
71725bded7cSSeth Forshee 	 * with x, y, and z all zero, so these seem to be flukes.
71825bded7cSSeth Forshee 	 * Ignore them.
71925bded7cSSeth Forshee 	 */
72002d04254SHans de Goede 	if (f->st.x && f->st.y && !f->pressure)
72125bded7cSSeth Forshee 		return;
72225bded7cSSeth Forshee 
72368c21870SHans de Goede 	alps_report_semi_mt_data(psmouse, fingers);
72425bded7cSSeth Forshee 
72534412ba2SPali Rohár 	if ((priv->flags & ALPS_DUALPOINT) &&
72634412ba2SPali Rohár 	    !(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS)) {
72702d04254SHans de Goede 		input_report_key(dev2, BTN_LEFT, f->ts_left);
72802d04254SHans de Goede 		input_report_key(dev2, BTN_RIGHT, f->ts_right);
72902d04254SHans de Goede 		input_report_key(dev2, BTN_MIDDLE, f->ts_middle);
73025bded7cSSeth Forshee 		input_sync(dev2);
73125bded7cSSeth Forshee 	}
73225bded7cSSeth Forshee }
73325bded7cSSeth Forshee 
73425bded7cSSeth Forshee static void alps_process_packet_v3(struct psmouse *psmouse)
73525bded7cSSeth Forshee {
73625bded7cSSeth Forshee 	unsigned char *packet = psmouse->packet;
73725bded7cSSeth Forshee 
73825bded7cSSeth Forshee 	/*
73925bded7cSSeth Forshee 	 * v3 protocol packets come in three types, two representing
74025bded7cSSeth Forshee 	 * touchpad data and one representing trackstick data.
74125bded7cSSeth Forshee 	 * Trackstick packets seem to be distinguished by always
74225bded7cSSeth Forshee 	 * having 0x3f in the last byte. This value has never been
74325bded7cSSeth Forshee 	 * observed in the last byte of either of the other types
74425bded7cSSeth Forshee 	 * of packets.
74525bded7cSSeth Forshee 	 */
74625bded7cSSeth Forshee 	if (packet[5] == 0x3f) {
74725bded7cSSeth Forshee 		alps_process_trackstick_packet_v3(psmouse);
74825bded7cSSeth Forshee 		return;
74925bded7cSSeth Forshee 	}
75025bded7cSSeth Forshee 
751ee65d4b3SYunkang Tang 	alps_process_touchpad_packet_v3_v5(psmouse);
75225bded7cSSeth Forshee }
75325bded7cSSeth Forshee 
75495f75e91SYunkang Tang static void alps_process_packet_v6(struct psmouse *psmouse)
75595f75e91SYunkang Tang {
75695f75e91SYunkang Tang 	struct alps_data *priv = psmouse->private;
75795f75e91SYunkang Tang 	unsigned char *packet = psmouse->packet;
75895f75e91SYunkang Tang 	struct input_dev *dev = psmouse->dev;
75995f75e91SYunkang Tang 	struct input_dev *dev2 = priv->dev2;
76095f75e91SYunkang Tang 	int x, y, z, left, right, middle;
76195f75e91SYunkang Tang 
76295f75e91SYunkang Tang 	/*
76395f75e91SYunkang Tang 	 * We can use Byte5 to distinguish if the packet is from Touchpad
76495f75e91SYunkang Tang 	 * or Trackpoint.
76595f75e91SYunkang Tang 	 * Touchpad:	0 - 0x7E
76695f75e91SYunkang Tang 	 * Trackpoint:	0x7F
76795f75e91SYunkang Tang 	 */
76895f75e91SYunkang Tang 	if (packet[5] == 0x7F) {
76995f75e91SYunkang Tang 		/* It should be a DualPoint when received Trackpoint packet */
77034412ba2SPali Rohár 		if (!(priv->flags & ALPS_DUALPOINT)) {
77134412ba2SPali Rohár 			psmouse_warn(psmouse,
77234412ba2SPali Rohár 				     "Rejected trackstick packet from non DualPoint device");
77395f75e91SYunkang Tang 			return;
77434412ba2SPali Rohár 		}
77595f75e91SYunkang Tang 
77695f75e91SYunkang Tang 		/* Trackpoint packet */
77795f75e91SYunkang Tang 		x = packet[1] | ((packet[3] & 0x20) << 2);
77895f75e91SYunkang Tang 		y = packet[2] | ((packet[3] & 0x40) << 1);
77995f75e91SYunkang Tang 		z = packet[4];
78095f75e91SYunkang Tang 		left = packet[3] & 0x01;
78195f75e91SYunkang Tang 		right = packet[3] & 0x02;
78295f75e91SYunkang Tang 		middle = packet[3] & 0x04;
78395f75e91SYunkang Tang 
78495f75e91SYunkang Tang 		/* To prevent the cursor jump when finger lifted */
78595f75e91SYunkang Tang 		if (x == 0x7F && y == 0x7F && z == 0x7F)
78695f75e91SYunkang Tang 			x = y = z = 0;
78795f75e91SYunkang Tang 
78895f75e91SYunkang Tang 		/* Divide 4 since trackpoint's speed is too fast */
78995f75e91SYunkang Tang 		input_report_rel(dev2, REL_X, (char)x / 4);
79095f75e91SYunkang Tang 		input_report_rel(dev2, REL_Y, -((char)y / 4));
79195f75e91SYunkang Tang 
79295f75e91SYunkang Tang 		input_report_key(dev2, BTN_LEFT, left);
79395f75e91SYunkang Tang 		input_report_key(dev2, BTN_RIGHT, right);
79495f75e91SYunkang Tang 		input_report_key(dev2, BTN_MIDDLE, middle);
79595f75e91SYunkang Tang 
79695f75e91SYunkang Tang 		input_sync(dev2);
79795f75e91SYunkang Tang 		return;
79895f75e91SYunkang Tang 	}
79995f75e91SYunkang Tang 
80095f75e91SYunkang Tang 	/* Touchpad packet */
80195f75e91SYunkang Tang 	x = packet[1] | ((packet[3] & 0x78) << 4);
80295f75e91SYunkang Tang 	y = packet[2] | ((packet[4] & 0x78) << 4);
80395f75e91SYunkang Tang 	z = packet[5];
80495f75e91SYunkang Tang 	left = packet[3] & 0x01;
80595f75e91SYunkang Tang 	right = packet[3] & 0x02;
80695f75e91SYunkang Tang 
80795f75e91SYunkang Tang 	if (z > 30)
80895f75e91SYunkang Tang 		input_report_key(dev, BTN_TOUCH, 1);
80995f75e91SYunkang Tang 	if (z < 25)
81095f75e91SYunkang Tang 		input_report_key(dev, BTN_TOUCH, 0);
81195f75e91SYunkang Tang 
81295f75e91SYunkang Tang 	if (z > 0) {
81395f75e91SYunkang Tang 		input_report_abs(dev, ABS_X, x);
81495f75e91SYunkang Tang 		input_report_abs(dev, ABS_Y, y);
81595f75e91SYunkang Tang 	}
81695f75e91SYunkang Tang 
81795f75e91SYunkang Tang 	input_report_abs(dev, ABS_PRESSURE, z);
81895f75e91SYunkang Tang 	input_report_key(dev, BTN_TOOL_FINGER, z > 0);
81995f75e91SYunkang Tang 
82095f75e91SYunkang Tang 	/* v6 touchpad does not have middle button */
82195f75e91SYunkang Tang 	input_report_key(dev, BTN_LEFT, left);
82295f75e91SYunkang Tang 	input_report_key(dev, BTN_RIGHT, right);
82395f75e91SYunkang Tang 
82495f75e91SYunkang Tang 	input_sync(dev);
82595f75e91SYunkang Tang }
82695f75e91SYunkang Tang 
82725bded7cSSeth Forshee static void alps_process_packet_v4(struct psmouse *psmouse)
82825bded7cSSeth Forshee {
8293b7e09faSGeorge Pantalos 	struct alps_data *priv = psmouse->private;
83025bded7cSSeth Forshee 	unsigned char *packet = psmouse->packet;
83102d04254SHans de Goede 	struct alps_fields *f = &priv->f;
83268c21870SHans de Goede 	int offset;
8333b7e09faSGeorge Pantalos 
8343b7e09faSGeorge Pantalos 	/*
8353b7e09faSGeorge Pantalos 	 * v4 has a 6-byte encoding for bitmap data, but this data is
8363b7e09faSGeorge Pantalos 	 * broken up between 3 normal packets. Use priv->multi_packet to
8373b7e09faSGeorge Pantalos 	 * track our position in the bitmap packet.
8383b7e09faSGeorge Pantalos 	 */
8393b7e09faSGeorge Pantalos 	if (packet[6] & 0x40) {
8403b7e09faSGeorge Pantalos 		/* sync, reset position */
8413b7e09faSGeorge Pantalos 		priv->multi_packet = 0;
8423b7e09faSGeorge Pantalos 	}
8433b7e09faSGeorge Pantalos 
8443b7e09faSGeorge Pantalos 	if (WARN_ON_ONCE(priv->multi_packet > 2))
8453b7e09faSGeorge Pantalos 		return;
8463b7e09faSGeorge Pantalos 
8473b7e09faSGeorge Pantalos 	offset = 2 * priv->multi_packet;
8483b7e09faSGeorge Pantalos 	priv->multi_data[offset] = packet[6];
8493b7e09faSGeorge Pantalos 	priv->multi_data[offset + 1] = packet[7];
8503b7e09faSGeorge Pantalos 
8513b7e09faSGeorge Pantalos 	if (++priv->multi_packet > 2) {
8523b7e09faSGeorge Pantalos 		priv->multi_packet = 0;
8533b7e09faSGeorge Pantalos 
85402d04254SHans de Goede 		f->x_map = ((priv->multi_data[2] & 0x1f) << 10) |
8553b7e09faSGeorge Pantalos 			   ((priv->multi_data[3] & 0x60) << 3) |
8563b7e09faSGeorge Pantalos 			   ((priv->multi_data[0] & 0x3f) << 2) |
8573b7e09faSGeorge Pantalos 			   ((priv->multi_data[1] & 0x60) >> 5);
85802d04254SHans de Goede 		f->y_map = ((priv->multi_data[5] & 0x01) << 10) |
8593b7e09faSGeorge Pantalos 			   ((priv->multi_data[3] & 0x1f) << 5) |
8603b7e09faSGeorge Pantalos 			    (priv->multi_data[1] & 0x1f);
8613b7e09faSGeorge Pantalos 
86202d04254SHans de Goede 		f->fingers = alps_process_bitmap(priv, f);
8633b7e09faSGeorge Pantalos 	}
86425bded7cSSeth Forshee 
865b0cfb794SAndreas Bosch 	f->left = !!(packet[4] & 0x01);
866b0cfb794SAndreas Bosch 	f->right = !!(packet[4] & 0x02);
86725bded7cSSeth Forshee 
86802d04254SHans de Goede 	f->st.x = ((packet[1] & 0x7f) << 4) | ((packet[3] & 0x30) >> 2) |
86925bded7cSSeth Forshee 		  ((packet[0] & 0x30) >> 4);
87002d04254SHans de Goede 	f->st.y = ((packet[2] & 0x7f) << 4) | (packet[3] & 0x0f);
87102d04254SHans de Goede 	f->pressure = packet[5] & 0x7f;
87225bded7cSSeth Forshee 
87368c21870SHans de Goede 	alps_report_semi_mt_data(psmouse, f->fingers);
87425bded7cSSeth Forshee }
87525bded7cSSeth Forshee 
8763808843cSYunkang Tang static bool alps_is_valid_package_v7(struct psmouse *psmouse)
8773808843cSYunkang Tang {
8783808843cSYunkang Tang 	switch (psmouse->pktcnt) {
8793808843cSYunkang Tang 	case 3:
8803808843cSYunkang Tang 		return (psmouse->packet[2] & 0x40) == 0x40;
8813808843cSYunkang Tang 	case 4:
8823808843cSYunkang Tang 		return (psmouse->packet[3] & 0x48) == 0x48;
8833808843cSYunkang Tang 	case 6:
8843808843cSYunkang Tang 		return (psmouse->packet[5] & 0x40) == 0x00;
8853808843cSYunkang Tang 	}
8863808843cSYunkang Tang 	return true;
8873808843cSYunkang Tang }
8883808843cSYunkang Tang 
8893808843cSYunkang Tang static unsigned char alps_get_packet_id_v7(char *byte)
8903808843cSYunkang Tang {
8913808843cSYunkang Tang 	unsigned char packet_id;
8923808843cSYunkang Tang 
8933808843cSYunkang Tang 	if (byte[4] & 0x40)
8943808843cSYunkang Tang 		packet_id = V7_PACKET_ID_TWO;
8953808843cSYunkang Tang 	else if (byte[4] & 0x01)
8963808843cSYunkang Tang 		packet_id = V7_PACKET_ID_MULTI;
8973808843cSYunkang Tang 	else if ((byte[0] & 0x10) && !(byte[4] & 0x43))
8983808843cSYunkang Tang 		packet_id = V7_PACKET_ID_NEW;
8993808843cSYunkang Tang 	else if (byte[1] == 0x00 && byte[4] == 0x00)
9003808843cSYunkang Tang 		packet_id = V7_PACKET_ID_IDLE;
9013808843cSYunkang Tang 	else
9023808843cSYunkang Tang 		packet_id = V7_PACKET_ID_UNKNOWN;
9033808843cSYunkang Tang 
9043808843cSYunkang Tang 	return packet_id;
9053808843cSYunkang Tang }
9063808843cSYunkang Tang 
9073808843cSYunkang Tang static void alps_get_finger_coordinate_v7(struct input_mt_pos *mt,
9083808843cSYunkang Tang 					  unsigned char *pkt,
9093808843cSYunkang Tang 					  unsigned char pkt_id)
9103808843cSYunkang Tang {
91127a560baSHans de Goede 	/*
91227a560baSHans de Goede 	 *       packet-fmt    b7   b6    b5   b4   b3   b2   b1   b0
91327a560baSHans de Goede 	 * Byte0 TWO & MULTI    L    1     R    M    1 Y0-2 Y0-1 Y0-0
91427a560baSHans de Goede 	 * Byte0 NEW            L    1  X1-5    1    1 Y0-2 Y0-1 Y0-0
91527a560baSHans de Goede 	 * Byte1            Y0-10 Y0-9  Y0-8 Y0-7 Y0-6 Y0-5 Y0-4 Y0-3
91627a560baSHans de Goede 	 * Byte2            X0-11    1 X0-10 X0-9 X0-8 X0-7 X0-6 X0-5
91727a560baSHans de Goede 	 * Byte3            X1-11    1  X0-4 X0-3    1 X0-2 X0-1 X0-0
91827a560baSHans de Goede 	 * Byte4 TWO        X1-10  TWO  X1-9 X1-8 X1-7 X1-6 X1-5 X1-4
91927a560baSHans de Goede 	 * Byte4 MULTI      X1-10  TWO  X1-9 X1-8 X1-7 X1-6 Y1-5    1
92027a560baSHans de Goede 	 * Byte4 NEW        X1-10  TWO  X1-9 X1-8 X1-7 X1-6    0    0
92127a560baSHans de Goede 	 * Byte5 TWO & NEW  Y1-10    0  Y1-9 Y1-8 Y1-7 Y1-6 Y1-5 Y1-4
92227a560baSHans de Goede 	 * Byte5 MULTI      Y1-10    0  Y1-9 Y1-8 Y1-7 Y1-6  F-1  F-0
92327a560baSHans de Goede 	 * L:         Left button
92427a560baSHans de Goede 	 * R / M:     Non-clickpads: Right / Middle button
92527a560baSHans de Goede 	 *            Clickpads: When > 2 fingers are down, and some fingers
92627a560baSHans de Goede 	 *            are in the button area, then the 2 coordinates reported
92727a560baSHans de Goede 	 *            are for fingers outside the button area and these report
92827a560baSHans de Goede 	 *            extra fingers being present in the right / left button
92927a560baSHans de Goede 	 *            area. Note these fingers are not added to the F field!
93027a560baSHans de Goede 	 *            so if a TWO packet is received and R = 1 then there are
93127a560baSHans de Goede 	 *            3 fingers down, etc.
93227a560baSHans de Goede 	 * TWO:       1: Two touches present, byte 0/4/5 are in TWO fmt
93327a560baSHans de Goede 	 *            0: If byte 4 bit 0 is 1, then byte 0/4/5 are in MULTI fmt
93427a560baSHans de Goede 	 *               otherwise byte 0 bit 4 must be set and byte 0/4/5 are
93527a560baSHans de Goede 	 *               in NEW fmt
93627a560baSHans de Goede 	 * F:         Number of fingers - 3, 0 means 3 fingers, 1 means 4 ...
93727a560baSHans de Goede 	 */
93827a560baSHans de Goede 
9393808843cSYunkang Tang 	mt[0].x = ((pkt[2] & 0x80) << 4);
9403808843cSYunkang Tang 	mt[0].x |= ((pkt[2] & 0x3F) << 5);
9413808843cSYunkang Tang 	mt[0].x |= ((pkt[3] & 0x30) >> 1);
9423808843cSYunkang Tang 	mt[0].x |= (pkt[3] & 0x07);
9433808843cSYunkang Tang 	mt[0].y = (pkt[1] << 3) | (pkt[0] & 0x07);
9443808843cSYunkang Tang 
9453808843cSYunkang Tang 	mt[1].x = ((pkt[3] & 0x80) << 4);
9463808843cSYunkang Tang 	mt[1].x |= ((pkt[4] & 0x80) << 3);
9473808843cSYunkang Tang 	mt[1].x |= ((pkt[4] & 0x3F) << 4);
9483808843cSYunkang Tang 	mt[1].y = ((pkt[5] & 0x80) << 3);
9493808843cSYunkang Tang 	mt[1].y |= ((pkt[5] & 0x3F) << 4);
9503808843cSYunkang Tang 
9513808843cSYunkang Tang 	switch (pkt_id) {
9523808843cSYunkang Tang 	case V7_PACKET_ID_TWO:
9533808843cSYunkang Tang 		mt[1].x &= ~0x000F;
9543808843cSYunkang Tang 		mt[1].y |= 0x000F;
9553808843cSYunkang Tang 		break;
9563808843cSYunkang Tang 
9573808843cSYunkang Tang 	case V7_PACKET_ID_MULTI:
9583808843cSYunkang Tang 		mt[1].x &= ~0x003F;
9593808843cSYunkang Tang 		mt[1].y &= ~0x0020;
9603808843cSYunkang Tang 		mt[1].y |= ((pkt[4] & 0x02) << 4);
9613808843cSYunkang Tang 		mt[1].y |= 0x001F;
9623808843cSYunkang Tang 		break;
9633808843cSYunkang Tang 
9643808843cSYunkang Tang 	case V7_PACKET_ID_NEW:
9653808843cSYunkang Tang 		mt[1].x &= ~0x003F;
9663808843cSYunkang Tang 		mt[1].x |= (pkt[0] & 0x20);
9673808843cSYunkang Tang 		mt[1].y |= 0x000F;
9683808843cSYunkang Tang 		break;
9693808843cSYunkang Tang 	}
9703808843cSYunkang Tang 
9713808843cSYunkang Tang 	mt[0].y = 0x7FF - mt[0].y;
9723808843cSYunkang Tang 	mt[1].y = 0x7FF - mt[1].y;
9733808843cSYunkang Tang }
9743808843cSYunkang Tang 
9753808843cSYunkang Tang static int alps_get_mt_count(struct input_mt_pos *mt)
9763808843cSYunkang Tang {
9777091c443SHans de Goede 	int i, fingers = 0;
9783808843cSYunkang Tang 
9797091c443SHans de Goede 	for (i = 0; i < MAX_TOUCHES; i++) {
9807091c443SHans de Goede 		if (mt[i].x != 0 || mt[i].y != 0)
9817091c443SHans de Goede 			fingers++;
9827091c443SHans de Goede 	}
9833808843cSYunkang Tang 
9847091c443SHans de Goede 	return fingers;
9853808843cSYunkang Tang }
9863808843cSYunkang Tang 
9873808843cSYunkang Tang static int alps_decode_packet_v7(struct alps_fields *f,
9883808843cSYunkang Tang 				  unsigned char *p,
9893808843cSYunkang Tang 				  struct psmouse *psmouse)
9903808843cSYunkang Tang {
991d27eb793SHans de Goede 	struct alps_data *priv = psmouse->private;
9923808843cSYunkang Tang 	unsigned char pkt_id;
9933808843cSYunkang Tang 
9943808843cSYunkang Tang 	pkt_id = alps_get_packet_id_v7(p);
9953808843cSYunkang Tang 	if (pkt_id == V7_PACKET_ID_IDLE)
9963808843cSYunkang Tang 		return 0;
9973808843cSYunkang Tang 	if (pkt_id == V7_PACKET_ID_UNKNOWN)
9983808843cSYunkang Tang 		return -1;
9998b238115SHans de Goede 	/*
10008b238115SHans de Goede 	 * NEW packets are send to indicate a discontinuity in the finger
10018b238115SHans de Goede 	 * coordinate reporting. Specifically a finger may have moved from
10028b238115SHans de Goede 	 * slot 0 to 1 or vice versa. INPUT_MT_TRACK takes care of this for
10038b238115SHans de Goede 	 * us.
10048b238115SHans de Goede 	 *
10058b238115SHans de Goede 	 * NEW packets have 3 problems:
10068b238115SHans de Goede 	 * 1) They do not contain middle / right button info (on non clickpads)
10078b238115SHans de Goede 	 *    this can be worked around by preserving the old button state
10088b238115SHans de Goede 	 * 2) They do not contain an accurate fingercount, and they are
10098b238115SHans de Goede 	 *    typically send when the number of fingers changes. We cannot use
10108b238115SHans de Goede 	 *    the old finger count as that may mismatch with the amount of
10118b238115SHans de Goede 	 *    touch coordinates we've available in the NEW packet
10128b238115SHans de Goede 	 * 3) Their x data for the second touch is inaccurate leading to
10138b238115SHans de Goede 	 *    a possible jump of the x coordinate by 16 units when the first
10148b238115SHans de Goede 	 *    non NEW packet comes in
10158b238115SHans de Goede 	 * Since problems 2 & 3 cannot be worked around, just ignore them.
10168b238115SHans de Goede 	 */
10178b238115SHans de Goede 	if (pkt_id == V7_PACKET_ID_NEW)
10188b238115SHans de Goede 		return 1;
10193808843cSYunkang Tang 
10203808843cSYunkang Tang 	alps_get_finger_coordinate_v7(f->mt, p, pkt_id);
10213808843cSYunkang Tang 
10223808843cSYunkang Tang 	if (pkt_id == V7_PACKET_ID_TWO)
10233808843cSYunkang Tang 		f->fingers = alps_get_mt_count(f->mt);
10248b238115SHans de Goede 	else /* pkt_id == V7_PACKET_ID_MULTI */
10253808843cSYunkang Tang 		f->fingers = 3 + (p[5] & 0x03);
10263808843cSYunkang Tang 
1027d27eb793SHans de Goede 	f->left = (p[0] & 0x80) >> 7;
1028d27eb793SHans de Goede 	if (priv->flags & ALPS_BUTTONPAD) {
1029d27eb793SHans de Goede 		if (p[0] & 0x20)
1030d27eb793SHans de Goede 			f->fingers++;
1031d27eb793SHans de Goede 		if (p[0] & 0x10)
1032d27eb793SHans de Goede 			f->fingers++;
1033d27eb793SHans de Goede 	} else {
1034d27eb793SHans de Goede 		f->right = (p[0] & 0x20) >> 5;
1035d27eb793SHans de Goede 		f->middle = (p[0] & 0x10) >> 4;
1036d27eb793SHans de Goede 	}
1037d27eb793SHans de Goede 
10387091c443SHans de Goede 	/* Sometimes a single touch is reported in mt[1] rather then mt[0] */
10397091c443SHans de Goede 	if (f->fingers == 1 && f->mt[0].x == 0 && f->mt[0].y == 0) {
10407091c443SHans de Goede 		f->mt[0].x = f->mt[1].x;
10417091c443SHans de Goede 		f->mt[0].y = f->mt[1].y;
10427091c443SHans de Goede 		f->mt[1].x = 0;
10437091c443SHans de Goede 		f->mt[1].y = 0;
10447091c443SHans de Goede 	}
10457091c443SHans de Goede 
10463808843cSYunkang Tang 	return 0;
10473808843cSYunkang Tang }
10483808843cSYunkang Tang 
10493808843cSYunkang Tang static void alps_process_trackstick_packet_v7(struct psmouse *psmouse)
10503808843cSYunkang Tang {
10513808843cSYunkang Tang 	struct alps_data *priv = psmouse->private;
10523808843cSYunkang Tang 	unsigned char *packet = psmouse->packet;
10533808843cSYunkang Tang 	struct input_dev *dev2 = priv->dev2;
10543808843cSYunkang Tang 	int x, y, z, left, right, middle;
10553808843cSYunkang Tang 
105634412ba2SPali Rohár 	/* It should be a DualPoint when received trackstick packet */
105734412ba2SPali Rohár 	if (!(priv->flags & ALPS_DUALPOINT)) {
105834412ba2SPali Rohár 		psmouse_warn(psmouse,
105934412ba2SPali Rohár 			     "Rejected trackstick packet from non DualPoint device");
106034412ba2SPali Rohár 		return;
106134412ba2SPali Rohár 	}
106234412ba2SPali Rohár 
10633808843cSYunkang Tang 	/*
10643808843cSYunkang Tang 	 *        b7 b6 b5 b4 b3 b2 b1 b0
10653808843cSYunkang Tang 	 * Byte0   0  1  0  0  1  0  0  0
10663808843cSYunkang Tang 	 * Byte1   1  1  *  *  1  M  R  L
10673808843cSYunkang Tang 	 * Byte2  X7  1 X5 X4 X3 X2 X1 X0
10683808843cSYunkang Tang 	 * Byte3  Z6  1 Y6 X6  1 Y2 Y1 Y0
10693808843cSYunkang Tang 	 * Byte4  Y7  0 Y5 Y4 Y3  1  1  0
10703808843cSYunkang Tang 	 * Byte5 T&P  0 Z5 Z4 Z3 Z2 Z1 Z0
10713808843cSYunkang Tang 	 * M / R / L: Middle / Right / Left button
10723808843cSYunkang Tang 	 */
10733808843cSYunkang Tang 
10743808843cSYunkang Tang 	x = ((packet[2] & 0xbf)) | ((packet[3] & 0x10) << 2);
10753808843cSYunkang Tang 	y = (packet[3] & 0x07) | (packet[4] & 0xb8) |
10763808843cSYunkang Tang 	    ((packet[3] & 0x20) << 1);
10773808843cSYunkang Tang 	z = (packet[5] & 0x3f) | ((packet[3] & 0x80) >> 1);
10783808843cSYunkang Tang 
10793808843cSYunkang Tang 	left = (packet[1] & 0x01);
10803808843cSYunkang Tang 	right = (packet[1] & 0x02) >> 1;
10813808843cSYunkang Tang 	middle = (packet[1] & 0x04) >> 2;
10823808843cSYunkang Tang 
10833808843cSYunkang Tang 	/* Divide 2 since trackpoint's speed is too fast */
10843808843cSYunkang Tang 	input_report_rel(dev2, REL_X, (char)x / 2);
10853808843cSYunkang Tang 	input_report_rel(dev2, REL_Y, -((char)y / 2));
10863808843cSYunkang Tang 
10873808843cSYunkang Tang 	input_report_key(dev2, BTN_LEFT, left);
10883808843cSYunkang Tang 	input_report_key(dev2, BTN_RIGHT, right);
10893808843cSYunkang Tang 	input_report_key(dev2, BTN_MIDDLE, middle);
10903808843cSYunkang Tang 
10913808843cSYunkang Tang 	input_sync(dev2);
10923808843cSYunkang Tang }
10933808843cSYunkang Tang 
10943808843cSYunkang Tang static void alps_process_touchpad_packet_v7(struct psmouse *psmouse)
10953808843cSYunkang Tang {
10963808843cSYunkang Tang 	struct alps_data *priv = psmouse->private;
10973808843cSYunkang Tang 	struct input_dev *dev = psmouse->dev;
10983808843cSYunkang Tang 	struct alps_fields *f = &priv->f;
10993808843cSYunkang Tang 
11003808843cSYunkang Tang 	memset(f, 0, sizeof(*f));
11013808843cSYunkang Tang 
11023808843cSYunkang Tang 	if (priv->decode_fields(f, psmouse->packet, psmouse))
11033808843cSYunkang Tang 		return;
11043808843cSYunkang Tang 
11053808843cSYunkang Tang 	alps_report_mt_data(psmouse, alps_get_mt_count(f->mt));
11063808843cSYunkang Tang 
11073808843cSYunkang Tang 	input_mt_report_finger_count(dev, f->fingers);
11083808843cSYunkang Tang 
11093808843cSYunkang Tang 	input_report_key(dev, BTN_LEFT, f->left);
11103808843cSYunkang Tang 	input_report_key(dev, BTN_RIGHT, f->right);
11113808843cSYunkang Tang 	input_report_key(dev, BTN_MIDDLE, f->middle);
11123808843cSYunkang Tang 
11133808843cSYunkang Tang 	input_sync(dev);
11143808843cSYunkang Tang }
11153808843cSYunkang Tang 
11163808843cSYunkang Tang static void alps_process_packet_v7(struct psmouse *psmouse)
11173808843cSYunkang Tang {
11183808843cSYunkang Tang 	unsigned char *packet = psmouse->packet;
11193808843cSYunkang Tang 
11203808843cSYunkang Tang 	if (packet[0] == 0x48 && (packet[4] & 0x47) == 0x06)
11213808843cSYunkang Tang 		alps_process_trackstick_packet_v7(psmouse);
11223808843cSYunkang Tang 	else
11233808843cSYunkang Tang 		alps_process_touchpad_packet_v7(psmouse);
11243808843cSYunkang Tang }
11253808843cSYunkang Tang 
11261d9f2626SSebastian Kapfer static void alps_report_bare_ps2_packet(struct psmouse *psmouse,
11271d9f2626SSebastian Kapfer 					unsigned char packet[],
11281d9f2626SSebastian Kapfer 					bool report_buttons)
11291d9f2626SSebastian Kapfer {
11301d9f2626SSebastian Kapfer 	struct alps_data *priv = psmouse->private;
11311d9f2626SSebastian Kapfer 	struct input_dev *dev2 = priv->dev2;
11321d9f2626SSebastian Kapfer 
11331d9f2626SSebastian Kapfer 	if (report_buttons)
11341d9f2626SSebastian Kapfer 		alps_report_buttons(psmouse, dev2, psmouse->dev,
11351d9f2626SSebastian Kapfer 				packet[0] & 1, packet[0] & 2, packet[0] & 4);
11361d9f2626SSebastian Kapfer 
11371d9f2626SSebastian Kapfer 	input_report_rel(dev2, REL_X,
11381d9f2626SSebastian Kapfer 		packet[1] ? packet[1] - ((packet[0] << 4) & 0x100) : 0);
11391d9f2626SSebastian Kapfer 	input_report_rel(dev2, REL_Y,
11401d9f2626SSebastian Kapfer 		packet[2] ? ((packet[0] << 3) & 0x100) - packet[2] : 0);
11411d9f2626SSebastian Kapfer 
11421d9f2626SSebastian Kapfer 	input_sync(dev2);
11431d9f2626SSebastian Kapfer }
11441d9f2626SSebastian Kapfer 
11451d9f2626SSebastian Kapfer static psmouse_ret_t alps_handle_interleaved_ps2(struct psmouse *psmouse)
11461da177e4SLinus Torvalds {
11471da177e4SLinus Torvalds 	struct alps_data *priv = psmouse->private;
11481da177e4SLinus Torvalds 
11491d9f2626SSebastian Kapfer 	if (psmouse->pktcnt < 6)
11501d9f2626SSebastian Kapfer 		return PSMOUSE_GOOD_DATA;
11511d9f2626SSebastian Kapfer 
11521d9f2626SSebastian Kapfer 	if (psmouse->pktcnt == 6) {
11531d9f2626SSebastian Kapfer 		/*
11541d9f2626SSebastian Kapfer 		 * Start a timer to flush the packet if it ends up last
11551d9f2626SSebastian Kapfer 		 * 6-byte packet in the stream. Timer needs to fire
11561d9f2626SSebastian Kapfer 		 * psmouse core times out itself. 20 ms should be enough
11571d9f2626SSebastian Kapfer 		 * to decide if we are getting more data or not.
11581d9f2626SSebastian Kapfer 		 */
11591d9f2626SSebastian Kapfer 		mod_timer(&priv->timer, jiffies + msecs_to_jiffies(20));
11601d9f2626SSebastian Kapfer 		return PSMOUSE_GOOD_DATA;
11611d9f2626SSebastian Kapfer 	}
11621d9f2626SSebastian Kapfer 
11631d9f2626SSebastian Kapfer 	del_timer(&priv->timer);
11641d9f2626SSebastian Kapfer 
11651d9f2626SSebastian Kapfer 	if (psmouse->packet[6] & 0x80) {
11661d9f2626SSebastian Kapfer 
11671d9f2626SSebastian Kapfer 		/*
11681d9f2626SSebastian Kapfer 		 * Highest bit is set - that means we either had
11691d9f2626SSebastian Kapfer 		 * complete ALPS packet and this is start of the
11701d9f2626SSebastian Kapfer 		 * next packet or we got garbage.
11711d9f2626SSebastian Kapfer 		 */
11721d9f2626SSebastian Kapfer 
11731d9f2626SSebastian Kapfer 		if (((psmouse->packet[3] |
11741d9f2626SSebastian Kapfer 		      psmouse->packet[4] |
11751d9f2626SSebastian Kapfer 		      psmouse->packet[5]) & 0x80) ||
117699df65e7SKevin Cernekee 		    (!alps_is_valid_first_byte(priv, psmouse->packet[6]))) {
1177b5d21704SDmitry Torokhov 			psmouse_dbg(psmouse,
11783b112923SAndy Shevchenko 				    "refusing packet %4ph (suspected interleaved ps/2)\n",
11793b112923SAndy Shevchenko 				    psmouse->packet + 3);
11801d9f2626SSebastian Kapfer 			return PSMOUSE_BAD_DATA;
11811d9f2626SSebastian Kapfer 		}
11821d9f2626SSebastian Kapfer 
118324af5cb9SKevin Cernekee 		priv->process_packet(psmouse);
11841d9f2626SSebastian Kapfer 
11851d9f2626SSebastian Kapfer 		/* Continue with the next packet */
11861d9f2626SSebastian Kapfer 		psmouse->packet[0] = psmouse->packet[6];
11871d9f2626SSebastian Kapfer 		psmouse->pktcnt = 1;
11881d9f2626SSebastian Kapfer 
11891d9f2626SSebastian Kapfer 	} else {
11901d9f2626SSebastian Kapfer 
11911d9f2626SSebastian Kapfer 		/*
11921d9f2626SSebastian Kapfer 		 * High bit is 0 - that means that we indeed got a PS/2
11931d9f2626SSebastian Kapfer 		 * packet in the middle of ALPS packet.
11941d9f2626SSebastian Kapfer 		 *
11951d9f2626SSebastian Kapfer 		 * There is also possibility that we got 6-byte ALPS
11961d9f2626SSebastian Kapfer 		 * packet followed  by 3-byte packet from trackpoint. We
11971d9f2626SSebastian Kapfer 		 * can not distinguish between these 2 scenarios but
1198b5d21704SDmitry Torokhov 		 * because the latter is unlikely to happen in course of
11991d9f2626SSebastian Kapfer 		 * normal operation (user would need to press all
12001d9f2626SSebastian Kapfer 		 * buttons on the pad and start moving trackpoint
12011d9f2626SSebastian Kapfer 		 * without touching the pad surface) we assume former.
12021d9f2626SSebastian Kapfer 		 * Even if we are wrong the wost thing that would happen
12031d9f2626SSebastian Kapfer 		 * the cursor would jump but we should not get protocol
1204b5d21704SDmitry Torokhov 		 * de-synchronization.
12051d9f2626SSebastian Kapfer 		 */
12061d9f2626SSebastian Kapfer 
12071d9f2626SSebastian Kapfer 		alps_report_bare_ps2_packet(psmouse, &psmouse->packet[3],
12081d9f2626SSebastian Kapfer 					    false);
12091d9f2626SSebastian Kapfer 
12101d9f2626SSebastian Kapfer 		/*
12111d9f2626SSebastian Kapfer 		 * Continue with the standard ALPS protocol handling,
12121d9f2626SSebastian Kapfer 		 * but make sure we won't process it as an interleaved
12131d9f2626SSebastian Kapfer 		 * packet again, which may happen if all buttons are
12141d9f2626SSebastian Kapfer 		 * pressed. To avoid this let's reset the 4th bit which
12151d9f2626SSebastian Kapfer 		 * is normally 1.
12161d9f2626SSebastian Kapfer 		 */
12171d9f2626SSebastian Kapfer 		psmouse->packet[3] = psmouse->packet[6] & 0xf7;
12181d9f2626SSebastian Kapfer 		psmouse->pktcnt = 4;
12191d9f2626SSebastian Kapfer 	}
12201d9f2626SSebastian Kapfer 
12211d9f2626SSebastian Kapfer 	return PSMOUSE_GOOD_DATA;
12221d9f2626SSebastian Kapfer }
12231d9f2626SSebastian Kapfer 
12241d9f2626SSebastian Kapfer static void alps_flush_packet(unsigned long data)
12251d9f2626SSebastian Kapfer {
12261d9f2626SSebastian Kapfer 	struct psmouse *psmouse = (struct psmouse *)data;
122724af5cb9SKevin Cernekee 	struct alps_data *priv = psmouse->private;
12281d9f2626SSebastian Kapfer 
12291d9f2626SSebastian Kapfer 	serio_pause_rx(psmouse->ps2dev.serio);
12301d9f2626SSebastian Kapfer 
1231b46615feSSeth Forshee 	if (psmouse->pktcnt == psmouse->pktsize) {
12321d9f2626SSebastian Kapfer 
12331d9f2626SSebastian Kapfer 		/*
12341d9f2626SSebastian Kapfer 		 * We did not any more data in reasonable amount of time.
12351d9f2626SSebastian Kapfer 		 * Validate the last 3 bytes and process as a standard
12361d9f2626SSebastian Kapfer 		 * ALPS packet.
12371d9f2626SSebastian Kapfer 		 */
12381d9f2626SSebastian Kapfer 		if ((psmouse->packet[3] |
12391d9f2626SSebastian Kapfer 		     psmouse->packet[4] |
12401d9f2626SSebastian Kapfer 		     psmouse->packet[5]) & 0x80) {
1241b5d21704SDmitry Torokhov 			psmouse_dbg(psmouse,
12423b112923SAndy Shevchenko 				    "refusing packet %3ph (suspected interleaved ps/2)\n",
12433b112923SAndy Shevchenko 				    psmouse->packet + 3);
12441d9f2626SSebastian Kapfer 		} else {
124524af5cb9SKevin Cernekee 			priv->process_packet(psmouse);
12461d9f2626SSebastian Kapfer 		}
12471d9f2626SSebastian Kapfer 		psmouse->pktcnt = 0;
12481d9f2626SSebastian Kapfer 	}
12491d9f2626SSebastian Kapfer 
12501d9f2626SSebastian Kapfer 	serio_continue_rx(psmouse->ps2dev.serio);
12511d9f2626SSebastian Kapfer }
12521d9f2626SSebastian Kapfer 
12531d9f2626SSebastian Kapfer static psmouse_ret_t alps_process_byte(struct psmouse *psmouse)
12541d9f2626SSebastian Kapfer {
12551d9f2626SSebastian Kapfer 	struct alps_data *priv = psmouse->private;
12561d9f2626SSebastian Kapfer 
12574ab8f7f3SPali Rohár 	/*
12584ab8f7f3SPali Rohár 	 * Check if we are dealing with a bare PS/2 packet, presumably from
12594ab8f7f3SPali Rohár 	 * a device connected to the external PS/2 port. Because bare PS/2
12604ab8f7f3SPali Rohár 	 * protocol does not have enough constant bits to self-synchronize
12614ab8f7f3SPali Rohár 	 * properly we only do this if the device is fully synchronized.
12624ab8f7f3SPali Rohár 	 */
12634ab8f7f3SPali Rohár 	if (!psmouse->out_of_sync_cnt && (psmouse->packet[0] & 0xc8) == 0x08) {
12641da177e4SLinus Torvalds 		if (psmouse->pktcnt == 3) {
12651d9f2626SSebastian Kapfer 			alps_report_bare_ps2_packet(psmouse, psmouse->packet,
12661d9f2626SSebastian Kapfer 						    true);
12671da177e4SLinus Torvalds 			return PSMOUSE_FULL_PACKET;
12681da177e4SLinus Torvalds 		}
12691da177e4SLinus Torvalds 		return PSMOUSE_GOOD_DATA;
12701da177e4SLinus Torvalds 	}
12711da177e4SLinus Torvalds 
12721d9f2626SSebastian Kapfer 	/* Check for PS/2 packet stuffed in the middle of ALPS packet. */
12731d9f2626SSebastian Kapfer 
127499df65e7SKevin Cernekee 	if ((priv->flags & ALPS_PS2_INTERLEAVED) &&
12751d9f2626SSebastian Kapfer 	    psmouse->pktcnt >= 4 && (psmouse->packet[3] & 0x0f) == 0x0f) {
12761d9f2626SSebastian Kapfer 		return alps_handle_interleaved_ps2(psmouse);
12771d9f2626SSebastian Kapfer 	}
12781d9f2626SSebastian Kapfer 
127999df65e7SKevin Cernekee 	if (!alps_is_valid_first_byte(priv, psmouse->packet[0])) {
1280b5d21704SDmitry Torokhov 		psmouse_dbg(psmouse,
1281b5d21704SDmitry Torokhov 			    "refusing packet[0] = %x (mask0 = %x, byte0 = %x)\n",
128299df65e7SKevin Cernekee 			    psmouse->packet[0], priv->mask0, priv->byte0);
12831da177e4SLinus Torvalds 		return PSMOUSE_BAD_DATA;
12841d9f2626SSebastian Kapfer 	}
12851da177e4SLinus Torvalds 
1286b46615feSSeth Forshee 	/* Bytes 2 - pktsize should have 0 in the highest bit */
1287a7ef82aeSPali Rohár 	if (priv->proto_version < ALPS_PROTO_V5 &&
128875af9e56SDave Turvene 	    psmouse->pktcnt >= 2 && psmouse->pktcnt <= psmouse->pktsize &&
12891d9f2626SSebastian Kapfer 	    (psmouse->packet[psmouse->pktcnt - 1] & 0x80)) {
1290b5d21704SDmitry Torokhov 		psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
1291b5d21704SDmitry Torokhov 			    psmouse->pktcnt - 1,
1292b5d21704SDmitry Torokhov 			    psmouse->packet[psmouse->pktcnt - 1]);
1293a7ef82aeSPali Rohár 
1294fb2dd7a6SDmitry Torokhov 		if (priv->proto_version == ALPS_PROTO_V3_RUSHMORE &&
1295a7ef82aeSPali Rohár 		    psmouse->pktcnt == psmouse->pktsize) {
1296a7ef82aeSPali Rohár 			/*
1297a7ef82aeSPali Rohár 			 * Some Dell boxes, such as Latitude E6440 or E7440
1298a7ef82aeSPali Rohár 			 * with closed lid, quite often smash last byte of
1299a7ef82aeSPali Rohár 			 * otherwise valid packet with 0xff. Given that the
1300a7ef82aeSPali Rohár 			 * next packet is very likely to be valid let's
1301a7ef82aeSPali Rohár 			 * report PSMOUSE_FULL_PACKET but not process data,
1302a7ef82aeSPali Rohár 			 * rather than reporting PSMOUSE_BAD_DATA and
1303a7ef82aeSPali Rohár 			 * filling the logs.
1304a7ef82aeSPali Rohár 			 */
1305a7ef82aeSPali Rohár 			return PSMOUSE_FULL_PACKET;
1306a7ef82aeSPali Rohár 		}
1307a7ef82aeSPali Rohár 
13081da177e4SLinus Torvalds 		return PSMOUSE_BAD_DATA;
13091d9f2626SSebastian Kapfer 	}
13101da177e4SLinus Torvalds 
13113808843cSYunkang Tang 	if (priv->proto_version == ALPS_PROTO_V7 &&
13123808843cSYunkang Tang 	    !alps_is_valid_package_v7(psmouse)) {
13133808843cSYunkang Tang 		psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
13143808843cSYunkang Tang 			    psmouse->pktcnt - 1,
13153808843cSYunkang Tang 			    psmouse->packet[psmouse->pktcnt - 1]);
13163808843cSYunkang Tang 		return PSMOUSE_BAD_DATA;
13173808843cSYunkang Tang 	}
13183808843cSYunkang Tang 
1319b46615feSSeth Forshee 	if (psmouse->pktcnt == psmouse->pktsize) {
132024af5cb9SKevin Cernekee 		priv->process_packet(psmouse);
13211da177e4SLinus Torvalds 		return PSMOUSE_FULL_PACKET;
13221da177e4SLinus Torvalds 	}
13231da177e4SLinus Torvalds 
13241da177e4SLinus Torvalds 	return PSMOUSE_GOOD_DATA;
13251da177e4SLinus Torvalds }
13261da177e4SLinus Torvalds 
132725bded7cSSeth Forshee static int alps_command_mode_send_nibble(struct psmouse *psmouse, int nibble)
132825bded7cSSeth Forshee {
132925bded7cSSeth Forshee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
133025bded7cSSeth Forshee 	struct alps_data *priv = psmouse->private;
133125bded7cSSeth Forshee 	int command;
133225bded7cSSeth Forshee 	unsigned char *param;
133325bded7cSSeth Forshee 	unsigned char dummy[4];
133425bded7cSSeth Forshee 
133525bded7cSSeth Forshee 	BUG_ON(nibble > 0xf);
133625bded7cSSeth Forshee 
133725bded7cSSeth Forshee 	command = priv->nibble_commands[nibble].command;
133825bded7cSSeth Forshee 	param = (command & 0x0f00) ?
133925bded7cSSeth Forshee 		dummy : (unsigned char *)&priv->nibble_commands[nibble].data;
134025bded7cSSeth Forshee 
134125bded7cSSeth Forshee 	if (ps2_command(ps2dev, param, command))
134225bded7cSSeth Forshee 		return -1;
134325bded7cSSeth Forshee 
134425bded7cSSeth Forshee 	return 0;
134525bded7cSSeth Forshee }
134625bded7cSSeth Forshee 
134725bded7cSSeth Forshee static int alps_command_mode_set_addr(struct psmouse *psmouse, int addr)
134825bded7cSSeth Forshee {
134925bded7cSSeth Forshee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
135025bded7cSSeth Forshee 	struct alps_data *priv = psmouse->private;
135125bded7cSSeth Forshee 	int i, nibble;
135225bded7cSSeth Forshee 
135325bded7cSSeth Forshee 	if (ps2_command(ps2dev, NULL, priv->addr_command))
135425bded7cSSeth Forshee 		return -1;
135525bded7cSSeth Forshee 
135625bded7cSSeth Forshee 	for (i = 12; i >= 0; i -= 4) {
135725bded7cSSeth Forshee 		nibble = (addr >> i) & 0xf;
135825bded7cSSeth Forshee 		if (alps_command_mode_send_nibble(psmouse, nibble))
135925bded7cSSeth Forshee 			return -1;
136025bded7cSSeth Forshee 	}
136125bded7cSSeth Forshee 
136225bded7cSSeth Forshee 	return 0;
136325bded7cSSeth Forshee }
136425bded7cSSeth Forshee 
136525bded7cSSeth Forshee static int __alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
136625bded7cSSeth Forshee {
136725bded7cSSeth Forshee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
136825bded7cSSeth Forshee 	unsigned char param[4];
136925bded7cSSeth Forshee 
137025bded7cSSeth Forshee 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
137125bded7cSSeth Forshee 		return -1;
137225bded7cSSeth Forshee 
137325bded7cSSeth Forshee 	/*
137425bded7cSSeth Forshee 	 * The address being read is returned in the first two bytes
137525bded7cSSeth Forshee 	 * of the result. Check that this address matches the expected
137625bded7cSSeth Forshee 	 * address.
137725bded7cSSeth Forshee 	 */
137825bded7cSSeth Forshee 	if (addr != ((param[0] << 8) | param[1]))
137925bded7cSSeth Forshee 		return -1;
138025bded7cSSeth Forshee 
138125bded7cSSeth Forshee 	return param[2];
138225bded7cSSeth Forshee }
138325bded7cSSeth Forshee 
138425bded7cSSeth Forshee static int alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
138525bded7cSSeth Forshee {
138625bded7cSSeth Forshee 	if (alps_command_mode_set_addr(psmouse, addr))
138725bded7cSSeth Forshee 		return -1;
138825bded7cSSeth Forshee 	return __alps_command_mode_read_reg(psmouse, addr);
138925bded7cSSeth Forshee }
139025bded7cSSeth Forshee 
139125bded7cSSeth Forshee static int __alps_command_mode_write_reg(struct psmouse *psmouse, u8 value)
139225bded7cSSeth Forshee {
139325bded7cSSeth Forshee 	if (alps_command_mode_send_nibble(psmouse, (value >> 4) & 0xf))
139425bded7cSSeth Forshee 		return -1;
139525bded7cSSeth Forshee 	if (alps_command_mode_send_nibble(psmouse, value & 0xf))
139625bded7cSSeth Forshee 		return -1;
139725bded7cSSeth Forshee 	return 0;
139825bded7cSSeth Forshee }
139925bded7cSSeth Forshee 
140025bded7cSSeth Forshee static int alps_command_mode_write_reg(struct psmouse *psmouse, int addr,
140125bded7cSSeth Forshee 				       u8 value)
140225bded7cSSeth Forshee {
140325bded7cSSeth Forshee 	if (alps_command_mode_set_addr(psmouse, addr))
140425bded7cSSeth Forshee 		return -1;
140525bded7cSSeth Forshee 	return __alps_command_mode_write_reg(psmouse, value);
140625bded7cSSeth Forshee }
140725bded7cSSeth Forshee 
140824ba9707SKevin Cernekee static int alps_rpt_cmd(struct psmouse *psmouse, int init_command,
140924ba9707SKevin Cernekee 			int repeated_command, unsigned char *param)
141024ba9707SKevin Cernekee {
141124ba9707SKevin Cernekee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
141224ba9707SKevin Cernekee 
141324ba9707SKevin Cernekee 	param[0] = 0;
141424ba9707SKevin Cernekee 	if (init_command && ps2_command(ps2dev, param, init_command))
141524ba9707SKevin Cernekee 		return -EIO;
141624ba9707SKevin Cernekee 
141724ba9707SKevin Cernekee 	if (ps2_command(ps2dev,  NULL, repeated_command) ||
141824ba9707SKevin Cernekee 	    ps2_command(ps2dev,  NULL, repeated_command) ||
141924ba9707SKevin Cernekee 	    ps2_command(ps2dev,  NULL, repeated_command))
142024ba9707SKevin Cernekee 		return -EIO;
142124ba9707SKevin Cernekee 
142224ba9707SKevin Cernekee 	param[0] = param[1] = param[2] = 0xff;
142324ba9707SKevin Cernekee 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
142424ba9707SKevin Cernekee 		return -EIO;
142524ba9707SKevin Cernekee 
142639fbe585SDmitry Torokhov 	psmouse_dbg(psmouse, "%2.2X report: %3ph\n",
142739fbe585SDmitry Torokhov 		    repeated_command, param);
142824ba9707SKevin Cernekee 	return 0;
142924ba9707SKevin Cernekee }
143024ba9707SKevin Cernekee 
14313808843cSYunkang Tang static bool alps_check_valid_firmware_id(unsigned char id[])
14323808843cSYunkang Tang {
14333808843cSYunkang Tang 	if (id[0] == 0x73)
14343808843cSYunkang Tang 		return true;
14353808843cSYunkang Tang 
14363808843cSYunkang Tang 	if (id[0] == 0x88 &&
14373808843cSYunkang Tang 	    (id[1] == 0x07 ||
14383808843cSYunkang Tang 	     id[1] == 0x08 ||
14393808843cSYunkang Tang 	     (id[1] & 0xf0) == 0xb0 ||
14403808843cSYunkang Tang 	     (id[1] & 0xf0) == 0xc0)) {
14413808843cSYunkang Tang 		return true;
14423808843cSYunkang Tang 	}
14433808843cSYunkang Tang 
14443808843cSYunkang Tang 	return false;
14453808843cSYunkang Tang }
14463808843cSYunkang Tang 
1447d18e53fcSKevin Cernekee static int alps_enter_command_mode(struct psmouse *psmouse)
144825bded7cSSeth Forshee {
144925bded7cSSeth Forshee 	unsigned char param[4];
145025bded7cSSeth Forshee 
145124ba9707SKevin Cernekee 	if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_RESET_WRAP, param)) {
145225bded7cSSeth Forshee 		psmouse_err(psmouse, "failed to enter command mode\n");
145325bded7cSSeth Forshee 		return -1;
145425bded7cSSeth Forshee 	}
145525bded7cSSeth Forshee 
14563808843cSYunkang Tang 	if (!alps_check_valid_firmware_id(param)) {
145725bded7cSSeth Forshee 		psmouse_dbg(psmouse,
145824ba9707SKevin Cernekee 			    "unknown response while entering command mode\n");
145925bded7cSSeth Forshee 		return -1;
146025bded7cSSeth Forshee 	}
146125bded7cSSeth Forshee 	return 0;
146225bded7cSSeth Forshee }
146325bded7cSSeth Forshee 
146425bded7cSSeth Forshee static inline int alps_exit_command_mode(struct psmouse *psmouse)
146525bded7cSSeth Forshee {
146625bded7cSSeth Forshee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
146725bded7cSSeth Forshee 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM))
146825bded7cSSeth Forshee 		return -1;
146925bded7cSSeth Forshee 	return 0;
147025bded7cSSeth Forshee }
147125bded7cSSeth Forshee 
14721da177e4SLinus Torvalds /*
14731da177e4SLinus Torvalds  * For DualPoint devices select the device that should respond to
14741da177e4SLinus Torvalds  * subsequent commands. It looks like glidepad is behind stickpointer,
14751da177e4SLinus Torvalds  * I'd thought it would be other way around...
14761da177e4SLinus Torvalds  */
147725bded7cSSeth Forshee static int alps_passthrough_mode_v2(struct psmouse *psmouse, bool enable)
14781da177e4SLinus Torvalds {
14791da177e4SLinus Torvalds 	struct ps2dev *ps2dev = &psmouse->ps2dev;
14801da177e4SLinus Torvalds 	int cmd = enable ? PSMOUSE_CMD_SETSCALE21 : PSMOUSE_CMD_SETSCALE11;
14811da177e4SLinus Torvalds 
14821da177e4SLinus Torvalds 	if (ps2_command(ps2dev, NULL, cmd) ||
14831da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, cmd) ||
14841da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, cmd) ||
14851da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
14861da177e4SLinus Torvalds 		return -1;
14871da177e4SLinus Torvalds 
14881da177e4SLinus Torvalds 	/* we may get 3 more bytes, just ignore them */
1489c611763dSDmitry Torokhov 	ps2_drain(ps2dev, 3, 100);
14901da177e4SLinus Torvalds 
14911da177e4SLinus Torvalds 	return 0;
14921da177e4SLinus Torvalds }
14931da177e4SLinus Torvalds 
149425bded7cSSeth Forshee static int alps_absolute_mode_v1_v2(struct psmouse *psmouse)
14951da177e4SLinus Torvalds {
14961da177e4SLinus Torvalds 	struct ps2dev *ps2dev = &psmouse->ps2dev;
14971da177e4SLinus Torvalds 
14981da177e4SLinus Torvalds 	/* Try ALPS magic knock - 4 disable before enable */
14991da177e4SLinus Torvalds 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
15001da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
15011da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
15021da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
15031da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
15041da177e4SLinus Torvalds 		return -1;
15051da177e4SLinus Torvalds 
15061da177e4SLinus Torvalds 	/*
15071da177e4SLinus Torvalds 	 * Switch mouse to poll (remote) mode so motion data will not
15081da177e4SLinus Torvalds 	 * get in our way
15091da177e4SLinus Torvalds 	 */
15101da177e4SLinus Torvalds 	return ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETPOLL);
15111da177e4SLinus Torvalds }
15121da177e4SLinus Torvalds 
151395f75e91SYunkang Tang static int alps_monitor_mode_send_word(struct psmouse *psmouse, u16 word)
151495f75e91SYunkang Tang {
151595f75e91SYunkang Tang 	int i, nibble;
151695f75e91SYunkang Tang 
151795f75e91SYunkang Tang 	/*
151895f75e91SYunkang Tang 	 * b0-b11 are valid bits, send sequence is inverse.
151995f75e91SYunkang Tang 	 * e.g. when word = 0x0123, nibble send sequence is 3, 2, 1
152095f75e91SYunkang Tang 	 */
152195f75e91SYunkang Tang 	for (i = 0; i <= 8; i += 4) {
152295f75e91SYunkang Tang 		nibble = (word >> i) & 0xf;
152395f75e91SYunkang Tang 		if (alps_command_mode_send_nibble(psmouse, nibble))
152495f75e91SYunkang Tang 			return -1;
152595f75e91SYunkang Tang 	}
152695f75e91SYunkang Tang 
152795f75e91SYunkang Tang 	return 0;
152895f75e91SYunkang Tang }
152995f75e91SYunkang Tang 
153095f75e91SYunkang Tang static int alps_monitor_mode_write_reg(struct psmouse *psmouse,
153195f75e91SYunkang Tang 				       u16 addr, u16 value)
153295f75e91SYunkang Tang {
153395f75e91SYunkang Tang 	struct ps2dev *ps2dev = &psmouse->ps2dev;
153495f75e91SYunkang Tang 
153595f75e91SYunkang Tang 	/* 0x0A0 is the command to write the word */
153695f75e91SYunkang Tang 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE) ||
153795f75e91SYunkang Tang 	    alps_monitor_mode_send_word(psmouse, 0x0A0) ||
153895f75e91SYunkang Tang 	    alps_monitor_mode_send_word(psmouse, addr) ||
153995f75e91SYunkang Tang 	    alps_monitor_mode_send_word(psmouse, value) ||
154095f75e91SYunkang Tang 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
154195f75e91SYunkang Tang 		return -1;
154295f75e91SYunkang Tang 
154395f75e91SYunkang Tang 	return 0;
154495f75e91SYunkang Tang }
154595f75e91SYunkang Tang 
154695f75e91SYunkang Tang static int alps_monitor_mode(struct psmouse *psmouse, bool enable)
154795f75e91SYunkang Tang {
154895f75e91SYunkang Tang 	struct ps2dev *ps2dev = &psmouse->ps2dev;
154995f75e91SYunkang Tang 
155095f75e91SYunkang Tang 	if (enable) {
155195f75e91SYunkang Tang 		/* EC E9 F5 F5 E7 E6 E7 E9 to enter monitor mode */
155295f75e91SYunkang Tang 		if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
155395f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO) ||
155495f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
155595f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
155695f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
155795f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
155895f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
155995f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO))
156095f75e91SYunkang Tang 			return -1;
156195f75e91SYunkang Tang 	} else {
156295f75e91SYunkang Tang 		/* EC to exit monitor mode */
156395f75e91SYunkang Tang 		if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP))
156495f75e91SYunkang Tang 			return -1;
156595f75e91SYunkang Tang 	}
156695f75e91SYunkang Tang 
156795f75e91SYunkang Tang 	return 0;
156895f75e91SYunkang Tang }
156995f75e91SYunkang Tang 
157095f75e91SYunkang Tang static int alps_absolute_mode_v6(struct psmouse *psmouse)
157195f75e91SYunkang Tang {
157295f75e91SYunkang Tang 	u16 reg_val = 0x181;
157395f75e91SYunkang Tang 	int ret = -1;
157495f75e91SYunkang Tang 
157595f75e91SYunkang Tang 	/* enter monitor mode, to write the register */
157695f75e91SYunkang Tang 	if (alps_monitor_mode(psmouse, true))
157795f75e91SYunkang Tang 		return -1;
157895f75e91SYunkang Tang 
157995f75e91SYunkang Tang 	ret = alps_monitor_mode_write_reg(psmouse, 0x000, reg_val);
158095f75e91SYunkang Tang 
158195f75e91SYunkang Tang 	if (alps_monitor_mode(psmouse, false))
158295f75e91SYunkang Tang 		ret = -1;
158395f75e91SYunkang Tang 
158495f75e91SYunkang Tang 	return ret;
158595f75e91SYunkang Tang }
158695f75e91SYunkang Tang 
15871da177e4SLinus Torvalds static int alps_get_status(struct psmouse *psmouse, char *param)
15881da177e4SLinus Torvalds {
15891da177e4SLinus Torvalds 	/* Get status: 0xF5 0xF5 0xF5 0xE9 */
159024ba9707SKevin Cernekee 	if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_DISABLE, param))
15911da177e4SLinus Torvalds 		return -1;
15921da177e4SLinus Torvalds 
15931da177e4SLinus Torvalds 	return 0;
15941da177e4SLinus Torvalds }
15951da177e4SLinus Torvalds 
15961da177e4SLinus Torvalds /*
15971da177e4SLinus Torvalds  * Turn touchpad tapping on or off. The sequences are:
15981da177e4SLinus Torvalds  * 0xE9 0xF5 0xF5 0xF3 0x0A to enable,
15991da177e4SLinus Torvalds  * 0xE9 0xF5 0xF5 0xE8 0x00 to disable.
16001da177e4SLinus Torvalds  * My guess that 0xE9 (GetInfo) is here as a sync point.
16011da177e4SLinus Torvalds  * For models that also have stickpointer (DualPoints) its tapping
16021da177e4SLinus Torvalds  * is controlled separately (0xE6 0xE6 0xE6 0xF3 0x14|0x0A) but
16031da177e4SLinus Torvalds  * we don't fiddle with it.
16041da177e4SLinus Torvalds  */
16051da177e4SLinus Torvalds static int alps_tap_mode(struct psmouse *psmouse, int enable)
16061da177e4SLinus Torvalds {
16071da177e4SLinus Torvalds 	struct ps2dev *ps2dev = &psmouse->ps2dev;
16081da177e4SLinus Torvalds 	int cmd = enable ? PSMOUSE_CMD_SETRATE : PSMOUSE_CMD_SETRES;
16091da177e4SLinus Torvalds 	unsigned char tap_arg = enable ? 0x0A : 0x00;
16101da177e4SLinus Torvalds 	unsigned char param[4];
16111da177e4SLinus Torvalds 
16121da177e4SLinus Torvalds 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO) ||
16131da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
16141da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
16151da177e4SLinus Torvalds 	    ps2_command(ps2dev, &tap_arg, cmd))
16161da177e4SLinus Torvalds 		return -1;
16171da177e4SLinus Torvalds 
16181da177e4SLinus Torvalds 	if (alps_get_status(psmouse, param))
16191da177e4SLinus Torvalds 		return -1;
16201da177e4SLinus Torvalds 
16211da177e4SLinus Torvalds 	return 0;
16221da177e4SLinus Torvalds }
16231da177e4SLinus Torvalds 
1624f0d5c6f4SDmitry Torokhov /*
1625f0d5c6f4SDmitry Torokhov  * alps_poll() - poll the touchpad for current motion packet.
1626f0d5c6f4SDmitry Torokhov  * Used in resync.
1627f0d5c6f4SDmitry Torokhov  */
1628f0d5c6f4SDmitry Torokhov static int alps_poll(struct psmouse *psmouse)
1629f0d5c6f4SDmitry Torokhov {
1630f0d5c6f4SDmitry Torokhov 	struct alps_data *priv = psmouse->private;
1631b46615feSSeth Forshee 	unsigned char buf[sizeof(psmouse->packet)];
1632b7802c5cSDmitry Torokhov 	bool poll_failed;
1633f0d5c6f4SDmitry Torokhov 
163499df65e7SKevin Cernekee 	if (priv->flags & ALPS_PASS)
163525bded7cSSeth Forshee 		alps_passthrough_mode_v2(psmouse, true);
1636f0d5c6f4SDmitry Torokhov 
1637f0d5c6f4SDmitry Torokhov 	poll_failed = ps2_command(&psmouse->ps2dev, buf,
1638f0d5c6f4SDmitry Torokhov 				  PSMOUSE_CMD_POLL | (psmouse->pktsize << 8)) < 0;
1639f0d5c6f4SDmitry Torokhov 
164099df65e7SKevin Cernekee 	if (priv->flags & ALPS_PASS)
164125bded7cSSeth Forshee 		alps_passthrough_mode_v2(psmouse, false);
1642f0d5c6f4SDmitry Torokhov 
164399df65e7SKevin Cernekee 	if (poll_failed || (buf[0] & priv->mask0) != priv->byte0)
1644f0d5c6f4SDmitry Torokhov 		return -1;
1645f0d5c6f4SDmitry Torokhov 
1646f0d5c6f4SDmitry Torokhov 	if ((psmouse->badbyte & 0xc8) == 0x08) {
1647f0d5c6f4SDmitry Torokhov /*
1648f0d5c6f4SDmitry Torokhov  * Poll the track stick ...
1649f0d5c6f4SDmitry Torokhov  */
1650f0d5c6f4SDmitry Torokhov 		if (ps2_command(&psmouse->ps2dev, buf, PSMOUSE_CMD_POLL | (3 << 8)))
1651f0d5c6f4SDmitry Torokhov 			return -1;
1652f0d5c6f4SDmitry Torokhov 	}
1653f0d5c6f4SDmitry Torokhov 
1654f0d5c6f4SDmitry Torokhov 	memcpy(psmouse->packet, buf, sizeof(buf));
1655f0d5c6f4SDmitry Torokhov 	return 0;
1656f0d5c6f4SDmitry Torokhov }
1657f0d5c6f4SDmitry Torokhov 
165825bded7cSSeth Forshee static int alps_hw_init_v1_v2(struct psmouse *psmouse)
16591da177e4SLinus Torvalds {
16601da177e4SLinus Torvalds 	struct alps_data *priv = psmouse->private;
16611da177e4SLinus Torvalds 
166299df65e7SKevin Cernekee 	if ((priv->flags & ALPS_PASS) &&
166325bded7cSSeth Forshee 	    alps_passthrough_mode_v2(psmouse, true)) {
16641da177e4SLinus Torvalds 		return -1;
1665b7802c5cSDmitry Torokhov 	}
16661da177e4SLinus Torvalds 
1667b7802c5cSDmitry Torokhov 	if (alps_tap_mode(psmouse, true)) {
1668b5d21704SDmitry Torokhov 		psmouse_warn(psmouse, "Failed to enable hardware tapping\n");
16691da177e4SLinus Torvalds 		return -1;
1670963f626dSPeter Osterlund 	}
16711da177e4SLinus Torvalds 
167225bded7cSSeth Forshee 	if (alps_absolute_mode_v1_v2(psmouse)) {
1673b5d21704SDmitry Torokhov 		psmouse_err(psmouse, "Failed to enable absolute mode\n");
16741da177e4SLinus Torvalds 		return -1;
16751da177e4SLinus Torvalds 	}
16761da177e4SLinus Torvalds 
167799df65e7SKevin Cernekee 	if ((priv->flags & ALPS_PASS) &&
167825bded7cSSeth Forshee 	    alps_passthrough_mode_v2(psmouse, false)) {
16791da177e4SLinus Torvalds 		return -1;
1680b7802c5cSDmitry Torokhov 	}
16811da177e4SLinus Torvalds 
16821e0c5b12SDmitry Torokhov 	/* ALPS needs stream mode, otherwise it won't report any data */
16831e0c5b12SDmitry Torokhov 	if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)) {
1684b5d21704SDmitry Torokhov 		psmouse_err(psmouse, "Failed to enable stream mode\n");
16851e0c5b12SDmitry Torokhov 		return -1;
16861e0c5b12SDmitry Torokhov 	}
16871e0c5b12SDmitry Torokhov 
16881e0c5b12SDmitry Torokhov 	return 0;
16891e0c5b12SDmitry Torokhov }
16901e0c5b12SDmitry Torokhov 
169195f75e91SYunkang Tang static int alps_hw_init_v6(struct psmouse *psmouse)
169295f75e91SYunkang Tang {
169395f75e91SYunkang Tang 	unsigned char param[2] = {0xC8, 0x14};
169495f75e91SYunkang Tang 
169595f75e91SYunkang Tang 	/* Enter passthrough mode to let trackpoint enter 6byte raw mode */
169695f75e91SYunkang Tang 	if (alps_passthrough_mode_v2(psmouse, true))
169795f75e91SYunkang Tang 		return -1;
169895f75e91SYunkang Tang 
169995f75e91SYunkang Tang 	if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
170095f75e91SYunkang Tang 	    ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
170195f75e91SYunkang Tang 	    ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
170295f75e91SYunkang Tang 	    ps2_command(&psmouse->ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
170395f75e91SYunkang Tang 	    ps2_command(&psmouse->ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
170495f75e91SYunkang Tang 		return -1;
170595f75e91SYunkang Tang 
170695f75e91SYunkang Tang 	if (alps_passthrough_mode_v2(psmouse, false))
170795f75e91SYunkang Tang 		return -1;
170895f75e91SYunkang Tang 
170995f75e91SYunkang Tang 	if (alps_absolute_mode_v6(psmouse)) {
171095f75e91SYunkang Tang 		psmouse_err(psmouse, "Failed to enable absolute mode\n");
171195f75e91SYunkang Tang 		return -1;
171295f75e91SYunkang Tang 	}
171395f75e91SYunkang Tang 
171495f75e91SYunkang Tang 	return 0;
171595f75e91SYunkang Tang }
171695f75e91SYunkang Tang 
171725bded7cSSeth Forshee /*
1718cd401204SKevin Cernekee  * Enable or disable passthrough mode to the trackstick.
171925bded7cSSeth Forshee  */
1720cd401204SKevin Cernekee static int alps_passthrough_mode_v3(struct psmouse *psmouse,
1721cd401204SKevin Cernekee 				    int reg_base, bool enable)
172225bded7cSSeth Forshee {
1723cd401204SKevin Cernekee 	int reg_val, ret = -1;
172425bded7cSSeth Forshee 
1725d18e53fcSKevin Cernekee 	if (alps_enter_command_mode(psmouse))
172625bded7cSSeth Forshee 		return -1;
172725bded7cSSeth Forshee 
1728cd401204SKevin Cernekee 	reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x0008);
1729cd401204SKevin Cernekee 	if (reg_val == -1)
1730cd401204SKevin Cernekee 		goto error;
1731cd401204SKevin Cernekee 
173225bded7cSSeth Forshee 	if (enable)
173325bded7cSSeth Forshee 		reg_val |= 0x01;
173425bded7cSSeth Forshee 	else
173525bded7cSSeth Forshee 		reg_val &= ~0x01;
173625bded7cSSeth Forshee 
1737cd401204SKevin Cernekee 	ret = __alps_command_mode_write_reg(psmouse, reg_val);
173825bded7cSSeth Forshee 
1739cd401204SKevin Cernekee error:
1740cd401204SKevin Cernekee 	if (alps_exit_command_mode(psmouse))
1741cd401204SKevin Cernekee 		ret = -1;
1742cd401204SKevin Cernekee 	return ret;
174325bded7cSSeth Forshee }
174425bded7cSSeth Forshee 
174525bded7cSSeth Forshee /* Must be in command mode when calling this function */
174625bded7cSSeth Forshee static int alps_absolute_mode_v3(struct psmouse *psmouse)
174725bded7cSSeth Forshee {
174825bded7cSSeth Forshee 	int reg_val;
174925bded7cSSeth Forshee 
175025bded7cSSeth Forshee 	reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
175125bded7cSSeth Forshee 	if (reg_val == -1)
175225bded7cSSeth Forshee 		return -1;
175325bded7cSSeth Forshee 
175425bded7cSSeth Forshee 	reg_val |= 0x06;
175525bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, reg_val))
175625bded7cSSeth Forshee 		return -1;
175725bded7cSSeth Forshee 
175825bded7cSSeth Forshee 	return 0;
175925bded7cSSeth Forshee }
176025bded7cSSeth Forshee 
1761cd401204SKevin Cernekee static int alps_probe_trackstick_v3(struct psmouse *psmouse, int reg_base)
176225bded7cSSeth Forshee {
1763cd401204SKevin Cernekee 	int ret = -EIO, reg_val;
176425bded7cSSeth Forshee 
1765d18e53fcSKevin Cernekee 	if (alps_enter_command_mode(psmouse))
176625bded7cSSeth Forshee 		goto error;
176725bded7cSSeth Forshee 
1768cd401204SKevin Cernekee 	reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x08);
176925bded7cSSeth Forshee 	if (reg_val == -1)
177025bded7cSSeth Forshee 		goto error;
1771cd401204SKevin Cernekee 
1772cd401204SKevin Cernekee 	/* bit 7: trackstick is present */
1773cd401204SKevin Cernekee 	ret = reg_val & 0x80 ? 0 : -ENODEV;
1774cd401204SKevin Cernekee 
1775cd401204SKevin Cernekee error:
1776cd401204SKevin Cernekee 	alps_exit_command_mode(psmouse);
1777cd401204SKevin Cernekee 	return ret;
1778cd401204SKevin Cernekee }
1779cd401204SKevin Cernekee 
1780cd401204SKevin Cernekee static int alps_setup_trackstick_v3(struct psmouse *psmouse, int reg_base)
1781cd401204SKevin Cernekee {
1782cd401204SKevin Cernekee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
1783cd401204SKevin Cernekee 	int ret = 0;
1784cd401204SKevin Cernekee 	unsigned char param[4];
1785cd401204SKevin Cernekee 
1786cd401204SKevin Cernekee 	if (alps_passthrough_mode_v3(psmouse, reg_base, true))
1787cd401204SKevin Cernekee 		return -EIO;
178825bded7cSSeth Forshee 
178925bded7cSSeth Forshee 	/*
179025bded7cSSeth Forshee 	 * E7 report for the trackstick
179125bded7cSSeth Forshee 	 *
179225bded7cSSeth Forshee 	 * There have been reports of failures to seem to trace back
179325bded7cSSeth Forshee 	 * to the above trackstick check failing. When these occur
179425bded7cSSeth Forshee 	 * this E7 report fails, so when that happens we continue
179525bded7cSSeth Forshee 	 * with the assumption that there isn't a trackstick after
179625bded7cSSeth Forshee 	 * all.
179725bded7cSSeth Forshee 	 */
1798cd401204SKevin Cernekee 	if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_SETSCALE21, param)) {
1799a09221e8SDmitry Torokhov 		psmouse_warn(psmouse, "Failed to initialize trackstick (E7 report failed)\n");
1800cd401204SKevin Cernekee 		ret = -ENODEV;
180125bded7cSSeth Forshee 	} else {
180239fbe585SDmitry Torokhov 		psmouse_dbg(psmouse, "trackstick E7 report: %3ph\n", param);
180325bded7cSSeth Forshee 
180425bded7cSSeth Forshee 		/*
180525bded7cSSeth Forshee 		 * Not sure what this does, but it is absolutely
180625bded7cSSeth Forshee 		 * essential. Without it, the touchpad does not
180725bded7cSSeth Forshee 		 * work at all and the trackstick just emits normal
180825bded7cSSeth Forshee 		 * PS/2 packets.
180925bded7cSSeth Forshee 		 */
181025bded7cSSeth Forshee 		if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
181125bded7cSSeth Forshee 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
181225bded7cSSeth Forshee 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
181325bded7cSSeth Forshee 		    alps_command_mode_send_nibble(psmouse, 0x9) ||
181425bded7cSSeth Forshee 		    alps_command_mode_send_nibble(psmouse, 0x4)) {
181525bded7cSSeth Forshee 			psmouse_err(psmouse,
181625bded7cSSeth Forshee 				    "Error sending magic E6 sequence\n");
1817cd401204SKevin Cernekee 			ret = -EIO;
181825bded7cSSeth Forshee 			goto error;
181925bded7cSSeth Forshee 		}
182025bded7cSSeth Forshee 
1821cd401204SKevin Cernekee 		/*
1822cd401204SKevin Cernekee 		 * This ensures the trackstick packets are in the format
1823cd401204SKevin Cernekee 		 * supported by this driver. If bit 1 isn't set the packet
1824cd401204SKevin Cernekee 		 * format is different.
1825cd401204SKevin Cernekee 		 */
1826d18e53fcSKevin Cernekee 		if (alps_enter_command_mode(psmouse) ||
1827cd401204SKevin Cernekee 		    alps_command_mode_write_reg(psmouse,
1828cd401204SKevin Cernekee 						reg_base + 0x08, 0x82) ||
1829cd401204SKevin Cernekee 		    alps_exit_command_mode(psmouse))
1830cd401204SKevin Cernekee 			ret = -EIO;
1831cd401204SKevin Cernekee 	}
1832cd401204SKevin Cernekee 
1833cd401204SKevin Cernekee error:
1834cd401204SKevin Cernekee 	if (alps_passthrough_mode_v3(psmouse, reg_base, false))
1835cd401204SKevin Cernekee 		ret = -EIO;
1836cd401204SKevin Cernekee 
1837cd401204SKevin Cernekee 	return ret;
1838cd401204SKevin Cernekee }
1839cd401204SKevin Cernekee 
1840cd401204SKevin Cernekee static int alps_hw_init_v3(struct psmouse *psmouse)
1841cd401204SKevin Cernekee {
1842cd401204SKevin Cernekee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
1843cd401204SKevin Cernekee 	int reg_val;
1844cd401204SKevin Cernekee 	unsigned char param[4];
1845cd401204SKevin Cernekee 
1846cd401204SKevin Cernekee 	reg_val = alps_probe_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE);
1847cd401204SKevin Cernekee 	if (reg_val == -EIO)
1848cd401204SKevin Cernekee 		goto error;
184939fbe585SDmitry Torokhov 
1850cd401204SKevin Cernekee 	if (reg_val == 0 &&
1851cd401204SKevin Cernekee 	    alps_setup_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE) == -EIO)
1852cd401204SKevin Cernekee 		goto error;
1853cd401204SKevin Cernekee 
1854d18e53fcSKevin Cernekee 	if (alps_enter_command_mode(psmouse) ||
1855cd401204SKevin Cernekee 	    alps_absolute_mode_v3(psmouse)) {
185625bded7cSSeth Forshee 		psmouse_err(psmouse, "Failed to enter absolute mode\n");
185725bded7cSSeth Forshee 		goto error;
185825bded7cSSeth Forshee 	}
185925bded7cSSeth Forshee 
186025bded7cSSeth Forshee 	reg_val = alps_command_mode_read_reg(psmouse, 0x0006);
186125bded7cSSeth Forshee 	if (reg_val == -1)
186225bded7cSSeth Forshee 		goto error;
186325bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
186425bded7cSSeth Forshee 		goto error;
186525bded7cSSeth Forshee 
186625bded7cSSeth Forshee 	reg_val = alps_command_mode_read_reg(psmouse, 0x0007);
186725bded7cSSeth Forshee 	if (reg_val == -1)
186825bded7cSSeth Forshee 		goto error;
186925bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
187025bded7cSSeth Forshee 		goto error;
187125bded7cSSeth Forshee 
187225bded7cSSeth Forshee 	if (alps_command_mode_read_reg(psmouse, 0x0144) == -1)
187325bded7cSSeth Forshee 		goto error;
187425bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, 0x04))
187525bded7cSSeth Forshee 		goto error;
187625bded7cSSeth Forshee 
187725bded7cSSeth Forshee 	if (alps_command_mode_read_reg(psmouse, 0x0159) == -1)
187825bded7cSSeth Forshee 		goto error;
187925bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, 0x03))
188025bded7cSSeth Forshee 		goto error;
188125bded7cSSeth Forshee 
188225bded7cSSeth Forshee 	if (alps_command_mode_read_reg(psmouse, 0x0163) == -1)
188325bded7cSSeth Forshee 		goto error;
188425bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0163, 0x03))
188525bded7cSSeth Forshee 		goto error;
188625bded7cSSeth Forshee 
188725bded7cSSeth Forshee 	if (alps_command_mode_read_reg(psmouse, 0x0162) == -1)
188825bded7cSSeth Forshee 		goto error;
188925bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0162, 0x04))
189025bded7cSSeth Forshee 		goto error;
189125bded7cSSeth Forshee 
189225bded7cSSeth Forshee 	alps_exit_command_mode(psmouse);
189325bded7cSSeth Forshee 
189425bded7cSSeth Forshee 	/* Set rate and enable data reporting */
189525bded7cSSeth Forshee 	param[0] = 0x64;
189625bded7cSSeth Forshee 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
189725bded7cSSeth Forshee 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
189825bded7cSSeth Forshee 		psmouse_err(psmouse, "Failed to enable data reporting\n");
189925bded7cSSeth Forshee 		return -1;
190025bded7cSSeth Forshee 	}
190125bded7cSSeth Forshee 
190225bded7cSSeth Forshee 	return 0;
190325bded7cSSeth Forshee 
190425bded7cSSeth Forshee error:
190525bded7cSSeth Forshee 	/*
190625bded7cSSeth Forshee 	 * Leaving the touchpad in command mode will essentially render
190725bded7cSSeth Forshee 	 * it unusable until the machine reboots, so exit it here just
190825bded7cSSeth Forshee 	 * to be safe
190925bded7cSSeth Forshee 	 */
191025bded7cSSeth Forshee 	alps_exit_command_mode(psmouse);
191125bded7cSSeth Forshee 	return -1;
191225bded7cSSeth Forshee }
191325bded7cSSeth Forshee 
1914f3f33c67SHans de Goede static int alps_get_v3_v7_resolution(struct psmouse *psmouse, int reg_pitch)
1915f3f33c67SHans de Goede {
1916f3f33c67SHans de Goede 	int reg, x_pitch, y_pitch, x_electrode, y_electrode, x_phys, y_phys;
1917f3f33c67SHans de Goede 	struct alps_data *priv = psmouse->private;
1918f3f33c67SHans de Goede 
1919f3f33c67SHans de Goede 	reg = alps_command_mode_read_reg(psmouse, reg_pitch);
1920f3f33c67SHans de Goede 	if (reg < 0)
1921f3f33c67SHans de Goede 		return reg;
1922f3f33c67SHans de Goede 
1923f3f33c67SHans de Goede 	x_pitch = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
1924f3f33c67SHans de Goede 	x_pitch = 50 + 2 * x_pitch; /* In 0.1 mm units */
1925f3f33c67SHans de Goede 
1926f3f33c67SHans de Goede 	y_pitch = (char)reg >> 4; /* sign extend upper 4 bits */
1927f3f33c67SHans de Goede 	y_pitch = 36 + 2 * y_pitch; /* In 0.1 mm units */
1928f3f33c67SHans de Goede 
1929f3f33c67SHans de Goede 	reg = alps_command_mode_read_reg(psmouse, reg_pitch + 1);
1930f3f33c67SHans de Goede 	if (reg < 0)
1931f3f33c67SHans de Goede 		return reg;
1932f3f33c67SHans de Goede 
1933f3f33c67SHans de Goede 	x_electrode = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
1934f3f33c67SHans de Goede 	x_electrode = 17 + x_electrode;
1935f3f33c67SHans de Goede 
1936f3f33c67SHans de Goede 	y_electrode = (char)reg >> 4; /* sign extend upper 4 bits */
1937f3f33c67SHans de Goede 	y_electrode = 13 + y_electrode;
1938f3f33c67SHans de Goede 
1939f3f33c67SHans de Goede 	x_phys = x_pitch * (x_electrode - 1); /* In 0.1 mm units */
1940f3f33c67SHans de Goede 	y_phys = y_pitch * (y_electrode - 1); /* In 0.1 mm units */
1941f3f33c67SHans de Goede 
1942f3f33c67SHans de Goede 	priv->x_res = priv->x_max * 10 / x_phys; /* units / mm */
1943f3f33c67SHans de Goede 	priv->y_res = priv->y_max * 10 / y_phys; /* units / mm */
1944f3f33c67SHans de Goede 
1945f3f33c67SHans de Goede 	psmouse_dbg(psmouse,
1946f3f33c67SHans de Goede 		    "pitch %dx%d num-electrodes %dx%d physical size %dx%d mm res %dx%d\n",
1947f3f33c67SHans de Goede 		    x_pitch, y_pitch, x_electrode, y_electrode,
1948f3f33c67SHans de Goede 		    x_phys / 10, y_phys / 10, priv->x_res, priv->y_res);
1949f3f33c67SHans de Goede 
1950f3f33c67SHans de Goede 	return 0;
1951f3f33c67SHans de Goede }
1952f3f33c67SHans de Goede 
19531302bac3SKevin Cernekee static int alps_hw_init_rushmore_v3(struct psmouse *psmouse)
19541302bac3SKevin Cernekee {
1955cd401204SKevin Cernekee 	struct alps_data *priv = psmouse->private;
19561302bac3SKevin Cernekee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
19571302bac3SKevin Cernekee 	int reg_val, ret = -1;
19581302bac3SKevin Cernekee 
1959cd401204SKevin Cernekee 	if (priv->flags & ALPS_DUALPOINT) {
1960cd401204SKevin Cernekee 		reg_val = alps_setup_trackstick_v3(psmouse,
1961cd401204SKevin Cernekee 						   ALPS_REG_BASE_RUSHMORE);
1962cd401204SKevin Cernekee 		if (reg_val == -EIO)
1963cd401204SKevin Cernekee 			goto error;
1964cd401204SKevin Cernekee 	}
1965cd401204SKevin Cernekee 
1966d18e53fcSKevin Cernekee 	if (alps_enter_command_mode(psmouse) ||
19671302bac3SKevin Cernekee 	    alps_command_mode_read_reg(psmouse, 0xc2d9) == -1 ||
19681302bac3SKevin Cernekee 	    alps_command_mode_write_reg(psmouse, 0xc2cb, 0x00))
19691302bac3SKevin Cernekee 		goto error;
19701302bac3SKevin Cernekee 
1971f3f33c67SHans de Goede 	if (alps_get_v3_v7_resolution(psmouse, 0xc2da))
1972f3f33c67SHans de Goede 		goto error;
1973f3f33c67SHans de Goede 
19741302bac3SKevin Cernekee 	reg_val = alps_command_mode_read_reg(psmouse, 0xc2c6);
19751302bac3SKevin Cernekee 	if (reg_val == -1)
19761302bac3SKevin Cernekee 		goto error;
19771302bac3SKevin Cernekee 	if (__alps_command_mode_write_reg(psmouse, reg_val & 0xfd))
19781302bac3SKevin Cernekee 		goto error;
19791302bac3SKevin Cernekee 
19801302bac3SKevin Cernekee 	if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
19811302bac3SKevin Cernekee 		goto error;
19821302bac3SKevin Cernekee 
19831302bac3SKevin Cernekee 	/* enter absolute mode */
19841302bac3SKevin Cernekee 	reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
19851302bac3SKevin Cernekee 	if (reg_val == -1)
19861302bac3SKevin Cernekee 		goto error;
19871302bac3SKevin Cernekee 	if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
19881302bac3SKevin Cernekee 		goto error;
19891302bac3SKevin Cernekee 
19901302bac3SKevin Cernekee 	alps_exit_command_mode(psmouse);
19911302bac3SKevin Cernekee 	return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
19921302bac3SKevin Cernekee 
19931302bac3SKevin Cernekee error:
19941302bac3SKevin Cernekee 	alps_exit_command_mode(psmouse);
19951302bac3SKevin Cernekee 	return ret;
19961302bac3SKevin Cernekee }
19971302bac3SKevin Cernekee 
199825bded7cSSeth Forshee /* Must be in command mode when calling this function */
199925bded7cSSeth Forshee static int alps_absolute_mode_v4(struct psmouse *psmouse)
200025bded7cSSeth Forshee {
200125bded7cSSeth Forshee 	int reg_val;
200225bded7cSSeth Forshee 
200325bded7cSSeth Forshee 	reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
200425bded7cSSeth Forshee 	if (reg_val == -1)
200525bded7cSSeth Forshee 		return -1;
200625bded7cSSeth Forshee 
200725bded7cSSeth Forshee 	reg_val |= 0x02;
200825bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, reg_val))
200925bded7cSSeth Forshee 		return -1;
201025bded7cSSeth Forshee 
201125bded7cSSeth Forshee 	return 0;
201225bded7cSSeth Forshee }
201325bded7cSSeth Forshee 
201425bded7cSSeth Forshee static int alps_hw_init_v4(struct psmouse *psmouse)
201525bded7cSSeth Forshee {
201625bded7cSSeth Forshee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
201725bded7cSSeth Forshee 	unsigned char param[4];
201825bded7cSSeth Forshee 
2019d18e53fcSKevin Cernekee 	if (alps_enter_command_mode(psmouse))
202025bded7cSSeth Forshee 		goto error;
202125bded7cSSeth Forshee 
202225bded7cSSeth Forshee 	if (alps_absolute_mode_v4(psmouse)) {
202325bded7cSSeth Forshee 		psmouse_err(psmouse, "Failed to enter absolute mode\n");
202425bded7cSSeth Forshee 		goto error;
202525bded7cSSeth Forshee 	}
202625bded7cSSeth Forshee 
202725bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0007, 0x8c))
202825bded7cSSeth Forshee 		goto error;
202925bded7cSSeth Forshee 
203025bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0149, 0x03))
203125bded7cSSeth Forshee 		goto error;
203225bded7cSSeth Forshee 
203325bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0160, 0x03))
203425bded7cSSeth Forshee 		goto error;
203525bded7cSSeth Forshee 
203625bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x017f, 0x15))
203725bded7cSSeth Forshee 		goto error;
203825bded7cSSeth Forshee 
203925bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0151, 0x01))
204025bded7cSSeth Forshee 		goto error;
204125bded7cSSeth Forshee 
204225bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0168, 0x03))
204325bded7cSSeth Forshee 		goto error;
204425bded7cSSeth Forshee 
204525bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x014a, 0x03))
204625bded7cSSeth Forshee 		goto error;
204725bded7cSSeth Forshee 
204825bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0161, 0x03))
204925bded7cSSeth Forshee 		goto error;
205025bded7cSSeth Forshee 
205125bded7cSSeth Forshee 	alps_exit_command_mode(psmouse);
205225bded7cSSeth Forshee 
205325bded7cSSeth Forshee 	/*
205425bded7cSSeth Forshee 	 * This sequence changes the output from a 9-byte to an
205525bded7cSSeth Forshee 	 * 8-byte format. All the same data seems to be present,
205625bded7cSSeth Forshee 	 * just in a more compact format.
205725bded7cSSeth Forshee 	 */
205825bded7cSSeth Forshee 	param[0] = 0xc8;
205925bded7cSSeth Forshee 	param[1] = 0x64;
206025bded7cSSeth Forshee 	param[2] = 0x50;
206125bded7cSSeth Forshee 	if (ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
206225bded7cSSeth Forshee 	    ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE) ||
206325bded7cSSeth Forshee 	    ps2_command(ps2dev, &param[2], PSMOUSE_CMD_SETRATE) ||
206425bded7cSSeth Forshee 	    ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
206525bded7cSSeth Forshee 		return -1;
206625bded7cSSeth Forshee 
206725bded7cSSeth Forshee 	/* Set rate and enable data reporting */
206825bded7cSSeth Forshee 	param[0] = 0x64;
206925bded7cSSeth Forshee 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
207025bded7cSSeth Forshee 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
207125bded7cSSeth Forshee 		psmouse_err(psmouse, "Failed to enable data reporting\n");
207225bded7cSSeth Forshee 		return -1;
207325bded7cSSeth Forshee 	}
207425bded7cSSeth Forshee 
207525bded7cSSeth Forshee 	return 0;
207625bded7cSSeth Forshee 
207725bded7cSSeth Forshee error:
207825bded7cSSeth Forshee 	/*
207925bded7cSSeth Forshee 	 * Leaving the touchpad in command mode will essentially render
208025bded7cSSeth Forshee 	 * it unusable until the machine reboots, so exit it here just
208125bded7cSSeth Forshee 	 * to be safe
208225bded7cSSeth Forshee 	 */
208325bded7cSSeth Forshee 	alps_exit_command_mode(psmouse);
208425bded7cSSeth Forshee 	return -1;
208525bded7cSSeth Forshee }
208625bded7cSSeth Forshee 
2087ee65d4b3SYunkang Tang static int alps_dolphin_get_device_area(struct psmouse *psmouse,
2088ee65d4b3SYunkang Tang 					struct alps_data *priv)
2089ee65d4b3SYunkang Tang {
2090ee65d4b3SYunkang Tang 	struct ps2dev *ps2dev = &psmouse->ps2dev;
2091ee65d4b3SYunkang Tang 	unsigned char param[4] = {0};
2092ee65d4b3SYunkang Tang 	int num_x_electrode, num_y_electrode;
2093ee65d4b3SYunkang Tang 
2094ee65d4b3SYunkang Tang 	if (alps_enter_command_mode(psmouse))
2095ee65d4b3SYunkang Tang 		return -1;
2096ee65d4b3SYunkang Tang 
2097ee65d4b3SYunkang Tang 	param[0] = 0x0a;
2098ee65d4b3SYunkang Tang 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
2099ee65d4b3SYunkang Tang 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2100ee65d4b3SYunkang Tang 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2101ee65d4b3SYunkang Tang 	    ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2102ee65d4b3SYunkang Tang 	    ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE))
2103ee65d4b3SYunkang Tang 		return -1;
2104ee65d4b3SYunkang Tang 
2105ee65d4b3SYunkang Tang 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
2106ee65d4b3SYunkang Tang 		return -1;
2107ee65d4b3SYunkang Tang 
2108ee65d4b3SYunkang Tang 	/*
2109ee65d4b3SYunkang Tang 	 * Dolphin's sensor line number is not fixed. It can be calculated
2110ee65d4b3SYunkang Tang 	 * by adding the device's register value with DOLPHIN_PROFILE_X/YOFFSET.
2111ee65d4b3SYunkang Tang 	 * Further more, we can get device's x_max and y_max by multiplying
2112ee65d4b3SYunkang Tang 	 * sensor line number with DOLPHIN_COUNT_PER_ELECTRODE.
2113ee65d4b3SYunkang Tang 	 *
2114ee65d4b3SYunkang Tang 	 * e.g. When we get register's sensor_x = 11 & sensor_y = 8,
2115ee65d4b3SYunkang Tang 	 *	real sensor line number X = 11 + 8 = 19, and
2116ee65d4b3SYunkang Tang 	 *	real sensor line number Y = 8 + 1 = 9.
2117ee65d4b3SYunkang Tang 	 *	So, x_max = (19 - 1) * 64 = 1152, and
2118ee65d4b3SYunkang Tang 	 *	    y_max = (9 - 1) * 64 = 512.
2119ee65d4b3SYunkang Tang 	 */
2120ee65d4b3SYunkang Tang 	num_x_electrode = DOLPHIN_PROFILE_XOFFSET + (param[2] & 0x0F);
2121ee65d4b3SYunkang Tang 	num_y_electrode = DOLPHIN_PROFILE_YOFFSET + ((param[2] >> 4) & 0x0F);
2122ee65d4b3SYunkang Tang 	priv->x_bits = num_x_electrode;
2123ee65d4b3SYunkang Tang 	priv->y_bits = num_y_electrode;
2124ee65d4b3SYunkang Tang 	priv->x_max = (num_x_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
2125ee65d4b3SYunkang Tang 	priv->y_max = (num_y_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
2126ee65d4b3SYunkang Tang 
2127ee65d4b3SYunkang Tang 	if (alps_exit_command_mode(psmouse))
2128ee65d4b3SYunkang Tang 		return -1;
2129ee65d4b3SYunkang Tang 
2130ee65d4b3SYunkang Tang 	return 0;
2131ee65d4b3SYunkang Tang }
2132ee65d4b3SYunkang Tang 
213375af9e56SDave Turvene static int alps_hw_init_dolphin_v1(struct psmouse *psmouse)
213475af9e56SDave Turvene {
213575af9e56SDave Turvene 	struct ps2dev *ps2dev = &psmouse->ps2dev;
213675af9e56SDave Turvene 	unsigned char param[2];
213775af9e56SDave Turvene 
213875af9e56SDave Turvene 	/* This is dolphin "v1" as empirically defined by florin9doi */
213975af9e56SDave Turvene 	param[0] = 0x64;
214075af9e56SDave Turvene 	param[1] = 0x28;
214175af9e56SDave Turvene 
214275af9e56SDave Turvene 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
214375af9e56SDave Turvene 	    ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
214475af9e56SDave Turvene 	    ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
214575af9e56SDave Turvene 		return -1;
214675af9e56SDave Turvene 
214775af9e56SDave Turvene 	return 0;
214875af9e56SDave Turvene }
214975af9e56SDave Turvene 
21503808843cSYunkang Tang static int alps_hw_init_v7(struct psmouse *psmouse)
21513808843cSYunkang Tang {
21523808843cSYunkang Tang 	struct ps2dev *ps2dev = &psmouse->ps2dev;
21533808843cSYunkang Tang 	int reg_val, ret = -1;
21543808843cSYunkang Tang 
21553808843cSYunkang Tang 	if (alps_enter_command_mode(psmouse) ||
21563808843cSYunkang Tang 	    alps_command_mode_read_reg(psmouse, 0xc2d9) == -1)
21573808843cSYunkang Tang 		goto error;
21583808843cSYunkang Tang 
2159f3f33c67SHans de Goede 	if (alps_get_v3_v7_resolution(psmouse, 0xc397))
2160f3f33c67SHans de Goede 		goto error;
2161f3f33c67SHans de Goede 
21623808843cSYunkang Tang 	if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
21633808843cSYunkang Tang 		goto error;
21643808843cSYunkang Tang 
21653808843cSYunkang Tang 	reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
21663808843cSYunkang Tang 	if (reg_val == -1)
21673808843cSYunkang Tang 		goto error;
21683808843cSYunkang Tang 	if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
21693808843cSYunkang Tang 		goto error;
21703808843cSYunkang Tang 
21713808843cSYunkang Tang 	alps_exit_command_mode(psmouse);
21723808843cSYunkang Tang 	return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
21733808843cSYunkang Tang 
21743808843cSYunkang Tang error:
21753808843cSYunkang Tang 	alps_exit_command_mode(psmouse);
21763808843cSYunkang Tang 	return ret;
21773808843cSYunkang Tang }
21783808843cSYunkang Tang 
21793296f71cSDmitry Torokhov static int alps_set_protocol(struct psmouse *psmouse,
21803296f71cSDmitry Torokhov 			     struct alps_data *priv,
21813296f71cSDmitry Torokhov 			     const struct alps_protocol_info *protocol)
218225bded7cSSeth Forshee {
21833296f71cSDmitry Torokhov 	psmouse->private = priv;
21843296f71cSDmitry Torokhov 
21853296f71cSDmitry Torokhov 	setup_timer(&priv->timer, alps_flush_packet, (unsigned long)psmouse);
21863296f71cSDmitry Torokhov 
21873296f71cSDmitry Torokhov 	priv->proto_version = protocol->version;
21883296f71cSDmitry Torokhov 	priv->byte0 = protocol->byte0;
21893296f71cSDmitry Torokhov 	priv->mask0 = protocol->mask0;
21903296f71cSDmitry Torokhov 	priv->flags = protocol->flags;
2191f673ceb1SKevin Cernekee 
21927a9f73e7SKevin Cernekee 	priv->x_max = 2000;
21937a9f73e7SKevin Cernekee 	priv->y_max = 1400;
21947a9f73e7SKevin Cernekee 	priv->x_bits = 15;
21957a9f73e7SKevin Cernekee 	priv->y_bits = 11;
21967a9f73e7SKevin Cernekee 
219799df65e7SKevin Cernekee 	switch (priv->proto_version) {
219825bded7cSSeth Forshee 	case ALPS_PROTO_V1:
219925bded7cSSeth Forshee 	case ALPS_PROTO_V2:
220024af5cb9SKevin Cernekee 		priv->hw_init = alps_hw_init_v1_v2;
220124af5cb9SKevin Cernekee 		priv->process_packet = alps_process_packet_v1_v2;
220224af5cb9SKevin Cernekee 		priv->set_abs_params = alps_set_abs_params_st;
220395f75e91SYunkang Tang 		priv->x_max = 1023;
220495f75e91SYunkang Tang 		priv->y_max = 767;
220525bded7cSSeth Forshee 		break;
2206fb2dd7a6SDmitry Torokhov 
220725bded7cSSeth Forshee 	case ALPS_PROTO_V3:
220824af5cb9SKevin Cernekee 		priv->hw_init = alps_hw_init_v3;
220924af5cb9SKevin Cernekee 		priv->process_packet = alps_process_packet_v3;
221024af5cb9SKevin Cernekee 		priv->set_abs_params = alps_set_abs_params_mt;
2211f85e5001SKevin Cernekee 		priv->decode_fields = alps_decode_pinnacle;
221250e8b216SKevin Cernekee 		priv->nibble_commands = alps_v3_nibble_commands;
221350e8b216SKevin Cernekee 		priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
221425bded7cSSeth Forshee 		break;
2215fb2dd7a6SDmitry Torokhov 
2216fb2dd7a6SDmitry Torokhov 	case ALPS_PROTO_V3_RUSHMORE:
2217fb2dd7a6SDmitry Torokhov 		priv->hw_init = alps_hw_init_rushmore_v3;
2218fb2dd7a6SDmitry Torokhov 		priv->process_packet = alps_process_packet_v3;
2219fb2dd7a6SDmitry Torokhov 		priv->set_abs_params = alps_set_abs_params_mt;
2220fb2dd7a6SDmitry Torokhov 		priv->decode_fields = alps_decode_rushmore;
2221fb2dd7a6SDmitry Torokhov 		priv->nibble_commands = alps_v3_nibble_commands;
2222fb2dd7a6SDmitry Torokhov 		priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2223fb2dd7a6SDmitry Torokhov 		priv->x_bits = 16;
2224fb2dd7a6SDmitry Torokhov 		priv->y_bits = 12;
22253296f71cSDmitry Torokhov 
22263296f71cSDmitry Torokhov 		if (alps_probe_trackstick_v3(psmouse,
22273296f71cSDmitry Torokhov 					     ALPS_REG_BASE_RUSHMORE) < 0)
22283296f71cSDmitry Torokhov 			priv->flags &= ~ALPS_DUALPOINT;
22293296f71cSDmitry Torokhov 
2230fb2dd7a6SDmitry Torokhov 		break;
2231fb2dd7a6SDmitry Torokhov 
223225bded7cSSeth Forshee 	case ALPS_PROTO_V4:
223324af5cb9SKevin Cernekee 		priv->hw_init = alps_hw_init_v4;
223424af5cb9SKevin Cernekee 		priv->process_packet = alps_process_packet_v4;
223524af5cb9SKevin Cernekee 		priv->set_abs_params = alps_set_abs_params_mt;
223650e8b216SKevin Cernekee 		priv->nibble_commands = alps_v4_nibble_commands;
223750e8b216SKevin Cernekee 		priv->addr_command = PSMOUSE_CMD_DISABLE;
223825bded7cSSeth Forshee 		break;
22393296f71cSDmitry Torokhov 
224075af9e56SDave Turvene 	case ALPS_PROTO_V5:
224175af9e56SDave Turvene 		priv->hw_init = alps_hw_init_dolphin_v1;
2242ee65d4b3SYunkang Tang 		priv->process_packet = alps_process_touchpad_packet_v3_v5;
224375af9e56SDave Turvene 		priv->decode_fields = alps_decode_dolphin;
224475af9e56SDave Turvene 		priv->set_abs_params = alps_set_abs_params_mt;
224575af9e56SDave Turvene 		priv->nibble_commands = alps_v3_nibble_commands;
224675af9e56SDave Turvene 		priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
224775af9e56SDave Turvene 		priv->x_max = 1360;
224875af9e56SDave Turvene 		priv->y_max = 660;
224975af9e56SDave Turvene 		priv->x_bits = 23;
225075af9e56SDave Turvene 		priv->y_bits = 12;
225175af9e56SDave Turvene 		break;
22523296f71cSDmitry Torokhov 
225395f75e91SYunkang Tang 	case ALPS_PROTO_V6:
225495f75e91SYunkang Tang 		priv->hw_init = alps_hw_init_v6;
225595f75e91SYunkang Tang 		priv->process_packet = alps_process_packet_v6;
225695f75e91SYunkang Tang 		priv->set_abs_params = alps_set_abs_params_st;
225795f75e91SYunkang Tang 		priv->nibble_commands = alps_v6_nibble_commands;
225895f75e91SYunkang Tang 		priv->x_max = 2047;
225995f75e91SYunkang Tang 		priv->y_max = 1535;
226095f75e91SYunkang Tang 		break;
22613296f71cSDmitry Torokhov 
22623808843cSYunkang Tang 	case ALPS_PROTO_V7:
22633808843cSYunkang Tang 		priv->hw_init = alps_hw_init_v7;
22643808843cSYunkang Tang 		priv->process_packet = alps_process_packet_v7;
22653808843cSYunkang Tang 		priv->decode_fields = alps_decode_packet_v7;
22663808843cSYunkang Tang 		priv->set_abs_params = alps_set_abs_params_mt;
22673808843cSYunkang Tang 		priv->nibble_commands = alps_v3_nibble_commands;
22683808843cSYunkang Tang 		priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
22693296f71cSDmitry Torokhov 
22703296f71cSDmitry Torokhov 		if (alps_dolphin_get_device_area(psmouse, priv))
22713296f71cSDmitry Torokhov 			return -EIO;
22723808843cSYunkang Tang 
22733808843cSYunkang Tang 		if (priv->fw_ver[1] != 0xba)
22743808843cSYunkang Tang 			priv->flags |= ALPS_BUTTONPAD;
22753296f71cSDmitry Torokhov 
22763808843cSYunkang Tang 		break;
227725bded7cSSeth Forshee 	}
22783296f71cSDmitry Torokhov 
22793296f71cSDmitry Torokhov 	return 0;
228025bded7cSSeth Forshee }
228125bded7cSSeth Forshee 
22823296f71cSDmitry Torokhov static const struct alps_protocol_info *alps_match_table(unsigned char *e7,
22833296f71cSDmitry Torokhov 							 unsigned char *ec)
22842e992cc0SKevin Cernekee {
2285b5d6b851SKevin Cernekee 	const struct alps_model_info *model;
22862e992cc0SKevin Cernekee 	int i;
22872e992cc0SKevin Cernekee 
2288b5d6b851SKevin Cernekee 	for (i = 0; i < ARRAY_SIZE(alps_model_data); i++) {
2289b5d6b851SKevin Cernekee 		model = &alps_model_data[i];
2290b5d6b851SKevin Cernekee 
2291b5d6b851SKevin Cernekee 		if (!memcmp(e7, model->signature, sizeof(model->signature)) &&
2292b5d6b851SKevin Cernekee 		    (!model->command_mode_resp ||
2293b5d6b851SKevin Cernekee 		     model->command_mode_resp == ec[2])) {
2294b5d6b851SKevin Cernekee 
22953296f71cSDmitry Torokhov 			return &model->protocol_info;
2296b5d6b851SKevin Cernekee 		}
2297b5d6b851SKevin Cernekee 	}
2298b5d6b851SKevin Cernekee 
22993296f71cSDmitry Torokhov 	return NULL;
2300b5d6b851SKevin Cernekee }
2301b5d6b851SKevin Cernekee 
2302b5d6b851SKevin Cernekee static int alps_identify(struct psmouse *psmouse, struct alps_data *priv)
2303b5d6b851SKevin Cernekee {
23043296f71cSDmitry Torokhov 	const struct alps_protocol_info *protocol;
2305b5d6b851SKevin Cernekee 	unsigned char e6[4], e7[4], ec[4];
2306a09221e8SDmitry Torokhov 	int error;
2307b5d6b851SKevin Cernekee 
23082e992cc0SKevin Cernekee 	/*
23092e992cc0SKevin Cernekee 	 * First try "E6 report".
23102e992cc0SKevin Cernekee 	 * ALPS should return 0,0,10 or 0,0,100 if no buttons are pressed.
23112e992cc0SKevin Cernekee 	 * The bits 0-2 of the first byte will be 1s if some buttons are
23122e992cc0SKevin Cernekee 	 * pressed.
23132e992cc0SKevin Cernekee 	 */
2314b5d6b851SKevin Cernekee 	if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2315b5d6b851SKevin Cernekee 			 PSMOUSE_CMD_SETSCALE11, e6))
2316b5d6b851SKevin Cernekee 		return -EIO;
23172e992cc0SKevin Cernekee 
2318b5d6b851SKevin Cernekee 	if ((e6[0] & 0xf8) != 0 || e6[1] != 0 || (e6[2] != 10 && e6[2] != 100))
2319b5d6b851SKevin Cernekee 		return -EINVAL;
23202e992cc0SKevin Cernekee 
23212e992cc0SKevin Cernekee 	/*
2322b5d6b851SKevin Cernekee 	 * Now get the "E7" and "EC" reports.  These will uniquely identify
2323b5d6b851SKevin Cernekee 	 * most ALPS touchpads.
23242e992cc0SKevin Cernekee 	 */
2325b5d6b851SKevin Cernekee 	if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2326b5d6b851SKevin Cernekee 			 PSMOUSE_CMD_SETSCALE21, e7) ||
2327b5d6b851SKevin Cernekee 	    alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2328b5d6b851SKevin Cernekee 			 PSMOUSE_CMD_RESET_WRAP, ec) ||
2329b5d6b851SKevin Cernekee 	    alps_exit_command_mode(psmouse))
2330b5d6b851SKevin Cernekee 		return -EIO;
23312e992cc0SKevin Cernekee 
23323296f71cSDmitry Torokhov 	protocol = alps_match_table(e7, ec);
23333296f71cSDmitry Torokhov 	if (!protocol) {
23343296f71cSDmitry Torokhov 		if (e7[0] == 0x73 && e7[1] == 0x03 && e7[2] == 0x50 &&
23353296f71cSDmitry Torokhov 			   ec[0] == 0x73 && (ec[1] == 0x01 || ec[1] == 0x02)) {
23363296f71cSDmitry Torokhov 			protocol = &alps_v5_protocol_data;
23373296f71cSDmitry Torokhov 		} else if (ec[0] == 0x88 &&
23383296f71cSDmitry Torokhov 			   ((ec[1] & 0xf0) == 0xb0 || (ec[1] & 0xf0) == 0xc0)) {
23393296f71cSDmitry Torokhov 			protocol = &alps_v7_protocol_data;
23403296f71cSDmitry Torokhov 		} else if (ec[0] == 0x88 && ec[1] == 0x08) {
23413296f71cSDmitry Torokhov 			protocol = &alps_v3_rushmore_data;
23423296f71cSDmitry Torokhov 		} else if (ec[0] == 0x88 && ec[1] == 0x07 &&
23433296f71cSDmitry Torokhov 			   ec[2] >= 0x90 && ec[2] <= 0x9d) {
23443296f71cSDmitry Torokhov 			protocol = &alps_v3_protocol_data;
23453296f71cSDmitry Torokhov 		} else {
23463296f71cSDmitry Torokhov 			psmouse_dbg(psmouse,
23473296f71cSDmitry Torokhov 				    "Likely not an ALPS touchpad: E7=%3ph, EC=%3ph\n", e7, ec);
23483296f71cSDmitry Torokhov 			return -EINVAL;
23493296f71cSDmitry Torokhov 		}
23503296f71cSDmitry Torokhov 	}
23513296f71cSDmitry Torokhov 
2352a09221e8SDmitry Torokhov 	if (priv) {
2353c0cd17f6SHans de Goede 		/* Save the Firmware version */
2354c0cd17f6SHans de Goede 		memcpy(priv->fw_ver, ec, 3);
2355a09221e8SDmitry Torokhov 		error = alps_set_protocol(psmouse, priv, protocol);
2356a09221e8SDmitry Torokhov 		if (error)
2357a09221e8SDmitry Torokhov 			return error;
2358a09221e8SDmitry Torokhov 	}
2359c0cd17f6SHans de Goede 
2360a09221e8SDmitry Torokhov 	return 0;
23612e992cc0SKevin Cernekee }
23622e992cc0SKevin Cernekee 
23631e0c5b12SDmitry Torokhov static int alps_reconnect(struct psmouse *psmouse)
23641e0c5b12SDmitry Torokhov {
2365b5d6b851SKevin Cernekee 	struct alps_data *priv = psmouse->private;
236671bb21b6SMaxim Levitsky 
23671e0c5b12SDmitry Torokhov 	psmouse_reset(psmouse);
23681e0c5b12SDmitry Torokhov 
2369b5d6b851SKevin Cernekee 	if (alps_identify(psmouse, priv) < 0)
23701e0c5b12SDmitry Torokhov 		return -1;
23711e0c5b12SDmitry Torokhov 
237224af5cb9SKevin Cernekee 	return priv->hw_init(psmouse);
23731da177e4SLinus Torvalds }
23741da177e4SLinus Torvalds 
23751da177e4SLinus Torvalds static void alps_disconnect(struct psmouse *psmouse)
23761da177e4SLinus Torvalds {
23771da177e4SLinus Torvalds 	struct alps_data *priv = psmouse->private;
23782e5b636bSDmitry Torokhov 
23791da177e4SLinus Torvalds 	psmouse_reset(psmouse);
23801d9f2626SSebastian Kapfer 	del_timer_sync(&priv->timer);
23812e5b636bSDmitry Torokhov 	input_unregister_device(priv->dev2);
23821da177e4SLinus Torvalds 	kfree(priv);
23831da177e4SLinus Torvalds }
23841da177e4SLinus Torvalds 
238524af5cb9SKevin Cernekee static void alps_set_abs_params_st(struct alps_data *priv,
238624af5cb9SKevin Cernekee 				   struct input_dev *dev1)
238724af5cb9SKevin Cernekee {
238895f75e91SYunkang Tang 	input_set_abs_params(dev1, ABS_X, 0, priv->x_max, 0, 0);
238995f75e91SYunkang Tang 	input_set_abs_params(dev1, ABS_Y, 0, priv->y_max, 0, 0);
239024af5cb9SKevin Cernekee }
239124af5cb9SKevin Cernekee 
239224af5cb9SKevin Cernekee static void alps_set_abs_params_mt(struct alps_data *priv,
239324af5cb9SKevin Cernekee 				   struct input_dev *dev1)
239424af5cb9SKevin Cernekee {
23957a9f73e7SKevin Cernekee 	input_set_abs_params(dev1, ABS_MT_POSITION_X, 0, priv->x_max, 0, 0);
23967a9f73e7SKevin Cernekee 	input_set_abs_params(dev1, ABS_MT_POSITION_Y, 0, priv->y_max, 0, 0);
239724af5cb9SKevin Cernekee 
2398f3f33c67SHans de Goede 	input_abs_set_res(dev1, ABS_MT_POSITION_X, priv->x_res);
2399f3f33c67SHans de Goede 	input_abs_set_res(dev1, ABS_MT_POSITION_Y, priv->y_res);
2400f3f33c67SHans de Goede 
2401cdf333efSHans de Goede 	input_mt_init_slots(dev1, MAX_TOUCHES, INPUT_MT_POINTER |
2402cdf333efSHans de Goede 		INPUT_MT_DROP_UNUSED | INPUT_MT_TRACK | INPUT_MT_SEMI_MT);
2403cdf333efSHans de Goede 
240424af5cb9SKevin Cernekee 	set_bit(BTN_TOOL_TRIPLETAP, dev1->keybit);
240524af5cb9SKevin Cernekee 	set_bit(BTN_TOOL_QUADTAP, dev1->keybit);
24063808843cSYunkang Tang 
24073808843cSYunkang Tang 	/* V7 is real multi-touch */
24083808843cSYunkang Tang 	if (priv->proto_version == ALPS_PROTO_V7)
24093808843cSYunkang Tang 		clear_bit(INPUT_PROP_SEMI_MT, dev1->propbit);
241024af5cb9SKevin Cernekee }
241124af5cb9SKevin Cernekee 
24121da177e4SLinus Torvalds int alps_init(struct psmouse *psmouse)
24131da177e4SLinus Torvalds {
2414a09221e8SDmitry Torokhov 	struct alps_data *priv = psmouse->private;
24152e5b636bSDmitry Torokhov 	struct input_dev *dev1 = psmouse->dev, *dev2;
2416a09221e8SDmitry Torokhov 	int error;
24171da177e4SLinus Torvalds 
24182e5b636bSDmitry Torokhov 	dev2 = input_allocate_device();
2419a09221e8SDmitry Torokhov 	if (!dev2) {
2420a09221e8SDmitry Torokhov 		error = -ENOMEM;
24211da177e4SLinus Torvalds 		goto init_fail;
2422a09221e8SDmitry Torokhov 	}
24232e5b636bSDmitry Torokhov 
24242e5b636bSDmitry Torokhov 	priv->dev2 = dev2;
24251da177e4SLinus Torvalds 
2426a09221e8SDmitry Torokhov 	error = priv->hw_init(psmouse);
2427a09221e8SDmitry Torokhov 	if (error)
24281da177e4SLinus Torvalds 		goto init_fail;
24291da177e4SLinus Torvalds 
24307105d2eaSDmitry Torokhov 	/*
24317105d2eaSDmitry Torokhov 	 * Undo part of setup done for us by psmouse core since touchpad
24327105d2eaSDmitry Torokhov 	 * is not a relative device.
24337105d2eaSDmitry Torokhov 	 */
24347105d2eaSDmitry Torokhov 	__clear_bit(EV_REL, dev1->evbit);
24357105d2eaSDmitry Torokhov 	__clear_bit(REL_X, dev1->relbit);
24367105d2eaSDmitry Torokhov 	__clear_bit(REL_Y, dev1->relbit);
24377105d2eaSDmitry Torokhov 
24387105d2eaSDmitry Torokhov 	/*
24397105d2eaSDmitry Torokhov 	 * Now set up our capabilities.
24407105d2eaSDmitry Torokhov 	 */
24417b19ada2SJiri Slaby 	dev1->evbit[BIT_WORD(EV_KEY)] |= BIT_MASK(EV_KEY);
24427b19ada2SJiri Slaby 	dev1->keybit[BIT_WORD(BTN_TOUCH)] |= BIT_MASK(BTN_TOUCH);
24437b19ada2SJiri Slaby 	dev1->keybit[BIT_WORD(BTN_TOOL_FINGER)] |= BIT_MASK(BTN_TOOL_FINGER);
244471bb21b6SMaxim Levitsky 	dev1->keybit[BIT_WORD(BTN_LEFT)] |=
244571bb21b6SMaxim Levitsky 		BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
24461da177e4SLinus Torvalds 
24477b19ada2SJiri Slaby 	dev1->evbit[BIT_WORD(EV_ABS)] |= BIT_MASK(EV_ABS);
244825bded7cSSeth Forshee 
244924af5cb9SKevin Cernekee 	priv->set_abs_params(priv, dev1);
24503808843cSYunkang Tang 	/* No pressure on V7 */
24513808843cSYunkang Tang 	if (priv->proto_version != ALPS_PROTO_V7)
24522e5b636bSDmitry Torokhov 		input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
24531da177e4SLinus Torvalds 
245499df65e7SKevin Cernekee 	if (priv->flags & ALPS_WHEEL) {
24557b19ada2SJiri Slaby 		dev1->evbit[BIT_WORD(EV_REL)] |= BIT_MASK(EV_REL);
24567b19ada2SJiri Slaby 		dev1->relbit[BIT_WORD(REL_WHEEL)] |= BIT_MASK(REL_WHEEL);
24571da177e4SLinus Torvalds 	}
24581da177e4SLinus Torvalds 
245999df65e7SKevin Cernekee 	if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
24607b19ada2SJiri Slaby 		dev1->keybit[BIT_WORD(BTN_FORWARD)] |= BIT_MASK(BTN_FORWARD);
24617b19ada2SJiri Slaby 		dev1->keybit[BIT_WORD(BTN_BACK)] |= BIT_MASK(BTN_BACK);
24621da177e4SLinus Torvalds 	}
24631da177e4SLinus Torvalds 
246499df65e7SKevin Cernekee 	if (priv->flags & ALPS_FOUR_BUTTONS) {
246571bb21b6SMaxim Levitsky 		dev1->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_0);
246671bb21b6SMaxim Levitsky 		dev1->keybit[BIT_WORD(BTN_1)] |= BIT_MASK(BTN_1);
246771bb21b6SMaxim Levitsky 		dev1->keybit[BIT_WORD(BTN_2)] |= BIT_MASK(BTN_2);
246871bb21b6SMaxim Levitsky 		dev1->keybit[BIT_WORD(BTN_3)] |= BIT_MASK(BTN_3);
24693808843cSYunkang Tang 	} else if (priv->flags & ALPS_BUTTONPAD) {
24703808843cSYunkang Tang 		set_bit(INPUT_PROP_BUTTONPAD, dev1->propbit);
24713808843cSYunkang Tang 		clear_bit(BTN_RIGHT, dev1->keybit);
247271bb21b6SMaxim Levitsky 	} else {
247371bb21b6SMaxim Levitsky 		dev1->keybit[BIT_WORD(BTN_MIDDLE)] |= BIT_MASK(BTN_MIDDLE);
247471bb21b6SMaxim Levitsky 	}
247571bb21b6SMaxim Levitsky 
2476dfba8600SPali Rohár 	if (priv->flags & ALPS_DUALPOINT) {
2477dfba8600SPali Rohár 		/*
2478dfba8600SPali Rohár 		 * format of input device name is: "protocol vendor name"
2479dfba8600SPali Rohár 		 * see function psmouse_switch_protocol() in psmouse-base.c
2480dfba8600SPali Rohár 		 */
2481dfba8600SPali Rohár 		dev2->name = "AlpsPS/2 ALPS DualPoint Stick";
2482dfba8600SPali Rohár 		dev2->id.product = PSMOUSE_ALPS;
2483dfba8600SPali Rohár 		dev2->id.version = priv->proto_version;
2484dfba8600SPali Rohár 	} else {
2485dfba8600SPali Rohár 		dev2->name = "PS/2 ALPS Mouse";
2486dfba8600SPali Rohár 		dev2->id.product = PSMOUSE_PS2;
2487dfba8600SPali Rohár 		dev2->id.version = 0x0000;
2488dfba8600SPali Rohár 	}
2489dfba8600SPali Rohár 
249008ffce45SDmitry Torokhov 	snprintf(priv->phys, sizeof(priv->phys), "%s/input1", psmouse->ps2dev.serio->phys);
24912e5b636bSDmitry Torokhov 	dev2->phys = priv->phys;
24922e5b636bSDmitry Torokhov 	dev2->id.bustype = BUS_I8042;
24932e5b636bSDmitry Torokhov 	dev2->id.vendor  = 0x0002;
24941db3a345SDmitry Torokhov 	dev2->dev.parent = &psmouse->ps2dev.serio->dev;
24951da177e4SLinus Torvalds 
24967b19ada2SJiri Slaby 	dev2->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
249771bb21b6SMaxim Levitsky 	dev2->relbit[BIT_WORD(REL_X)] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
249871bb21b6SMaxim Levitsky 	dev2->keybit[BIT_WORD(BTN_LEFT)] =
249971bb21b6SMaxim Levitsky 		BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_MIDDLE) | BIT_MASK(BTN_RIGHT);
25001da177e4SLinus Torvalds 
250101d4cd5cSHans de Goede 	__set_bit(INPUT_PROP_POINTER, dev2->propbit);
25027611392fSHans de Goede 	if (priv->flags & ALPS_DUALPOINT)
25037611392fSHans de Goede 		__set_bit(INPUT_PROP_POINTING_STICK, dev2->propbit);
25047611392fSHans de Goede 
2505f42649e8SDmitry Torokhov 	if (input_register_device(priv->dev2))
2506f42649e8SDmitry Torokhov 		goto init_fail;
25071da177e4SLinus Torvalds 
25081da177e4SLinus Torvalds 	psmouse->protocol_handler = alps_process_byte;
2509f0d5c6f4SDmitry Torokhov 	psmouse->poll = alps_poll;
25101da177e4SLinus Torvalds 	psmouse->disconnect = alps_disconnect;
25111da177e4SLinus Torvalds 	psmouse->reconnect = alps_reconnect;
251299df65e7SKevin Cernekee 	psmouse->pktsize = priv->proto_version == ALPS_PROTO_V4 ? 8 : 6;
25131da177e4SLinus Torvalds 
2514f0d5c6f4SDmitry Torokhov 	/* We are having trouble resyncing ALPS touchpads so disable it for now */
2515f0d5c6f4SDmitry Torokhov 	psmouse->resync_time = 0;
2516f0d5c6f4SDmitry Torokhov 
25179d720b34SPali Rohár 	/* Allow 2 invalid packets without resetting device */
25189d720b34SPali Rohár 	psmouse->resetafter = psmouse->pktsize * 2;
25199d720b34SPali Rohár 
25201da177e4SLinus Torvalds 	return 0;
25211da177e4SLinus Torvalds 
25221da177e4SLinus Torvalds init_fail:
2523f42649e8SDmitry Torokhov 	psmouse_reset(psmouse);
25242e5b636bSDmitry Torokhov 	input_free_device(dev2);
2525a09221e8SDmitry Torokhov 	/*
2526a09221e8SDmitry Torokhov 	 * Even though we did not allocate psmouse->private we do free
2527a09221e8SDmitry Torokhov 	 * it here.
2528a09221e8SDmitry Torokhov 	 */
2529a09221e8SDmitry Torokhov 	kfree(psmouse->private);
25301e0c5b12SDmitry Torokhov 	psmouse->private = NULL;
2531a09221e8SDmitry Torokhov 	return error;
25321da177e4SLinus Torvalds }
25331da177e4SLinus Torvalds 
2534b7802c5cSDmitry Torokhov int alps_detect(struct psmouse *psmouse, bool set_properties)
25351da177e4SLinus Torvalds {
2536a09221e8SDmitry Torokhov 	struct alps_data *priv;
2537a09221e8SDmitry Torokhov 	int error;
25381da177e4SLinus Torvalds 
2539a09221e8SDmitry Torokhov 	error = alps_identify(psmouse, NULL);
2540a09221e8SDmitry Torokhov 	if (error)
2541a09221e8SDmitry Torokhov 		return error;
2542a09221e8SDmitry Torokhov 
2543a09221e8SDmitry Torokhov 	/*
2544a09221e8SDmitry Torokhov 	 * Reset the device to make sure it is fully operational:
2545a09221e8SDmitry Torokhov 	 * on some laptops, like certain Dell Latitudes, we may
2546a09221e8SDmitry Torokhov 	 * fail to properly detect presence of trackstick if device
2547a09221e8SDmitry Torokhov 	 * has not been reset.
2548a09221e8SDmitry Torokhov 	 */
2549a09221e8SDmitry Torokhov 	psmouse_reset(psmouse);
2550a09221e8SDmitry Torokhov 
2551a09221e8SDmitry Torokhov 	priv = kzalloc(sizeof(struct alps_data), GFP_KERNEL);
2552a09221e8SDmitry Torokhov 	if (!priv)
2553a09221e8SDmitry Torokhov 		return -ENOMEM;
2554a09221e8SDmitry Torokhov 
2555a09221e8SDmitry Torokhov 	error = alps_identify(psmouse, priv);
2556a09221e8SDmitry Torokhov 	if (error)
2557a09221e8SDmitry Torokhov 		return error;
25581da177e4SLinus Torvalds 
25591da177e4SLinus Torvalds 	if (set_properties) {
25601da177e4SLinus Torvalds 		psmouse->vendor = "ALPS";
2561a09221e8SDmitry Torokhov 		psmouse->name = priv->flags & ALPS_DUALPOINT ?
2562968ac842SDmitry Torokhov 				"DualPoint TouchPad" : "GlidePoint";
2563a09221e8SDmitry Torokhov 		psmouse->model = priv->proto_version;
2564a09221e8SDmitry Torokhov 	} else {
2565a09221e8SDmitry Torokhov 		/*
2566a09221e8SDmitry Torokhov 		 * Destroy alps_data structure we allocated earlier since
2567a09221e8SDmitry Torokhov 		 * this was just a "trial run". Otherwise we'll keep it
2568a09221e8SDmitry Torokhov 		 * to be used by alps_init() which has to be called if
2569a09221e8SDmitry Torokhov 		 * we succeed and set_properties is true.
2570a09221e8SDmitry Torokhov 		 */
2571a09221e8SDmitry Torokhov 		kfree(priv);
2572a09221e8SDmitry Torokhov 		psmouse->private = NULL;
25731da177e4SLinus Torvalds 	}
2574a09221e8SDmitry Torokhov 
25751da177e4SLinus Torvalds 	return 0;
25761da177e4SLinus Torvalds }
25771da177e4SLinus Torvalds 
2578