xref: /openbmc/linux/drivers/input/mouse/alps.c (revision 34412ba2)
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 */
10240e8f53bSHans de Goede #define ALPS_IS_RUSHMORE	0x100	/* device is a rushmore */
1033808843cSYunkang Tang #define ALPS_BUTTONPAD		0x200	/* device is a clickpad */
1041da177e4SLinus Torvalds 
105e38de678SHelge Deller static const struct alps_model_info alps_model_data[] = {
10625bded7cSSeth Forshee 	{ { 0x32, 0x02, 0x14 },	0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT },	/* Toshiba Salellite Pro M10 */
10725bded7cSSeth Forshee 	{ { 0x33, 0x02, 0x0a },	0x00, ALPS_PROTO_V1, 0x88, 0xf8, 0 },				/* UMAX-530T */
10825bded7cSSeth Forshee 	{ { 0x53, 0x02, 0x0a },	0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
10925bded7cSSeth Forshee 	{ { 0x53, 0x02, 0x14 },	0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
11025bded7cSSeth Forshee 	{ { 0x60, 0x03, 0xc8 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },				/* HP ze1115 */
11125bded7cSSeth Forshee 	{ { 0x63, 0x02, 0x0a },	0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
11225bded7cSSeth Forshee 	{ { 0x63, 0x02, 0x14 },	0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
11325bded7cSSeth Forshee 	{ { 0x63, 0x02, 0x28 },	0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 },		/* Fujitsu Siemens S6010 */
11425bded7cSSeth Forshee 	{ { 0x63, 0x02, 0x3c },	0x00, ALPS_PROTO_V2, 0x8f, 0x8f, ALPS_WHEEL },			/* Toshiba Satellite S2400-103 */
11525bded7cSSeth Forshee 	{ { 0x63, 0x02, 0x50 },	0x00, ALPS_PROTO_V2, 0xef, 0xef, ALPS_FW_BK_1 },		/* NEC Versa L320 */
11625bded7cSSeth Forshee 	{ { 0x63, 0x02, 0x64 },	0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
11725bded7cSSeth Forshee 	{ { 0x63, 0x03, 0xc8 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT },	/* Dell Latitude D800 */
11825bded7cSSeth Forshee 	{ { 0x73, 0x00, 0x0a },	0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_DUALPOINT },		/* ThinkPad R61 8918-5QG */
11925bded7cSSeth Forshee 	{ { 0x73, 0x02, 0x0a },	0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
12025bded7cSSeth Forshee 	{ { 0x73, 0x02, 0x14 },	0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 },		/* Ahtec Laptop */
12125bded7cSSeth Forshee 	{ { 0x20, 0x02, 0x0e },	0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT },	/* XXX */
12225bded7cSSeth Forshee 	{ { 0x22, 0x02, 0x0a },	0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT },
12325bded7cSSeth Forshee 	{ { 0x22, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xff, 0xff, ALPS_PASS | ALPS_DUALPOINT },	/* Dell Latitude D600 */
1241d9f2626SSebastian Kapfer 	/* Dell Latitude E5500, E6400, E6500, Precision M4400 */
12525bded7cSSeth Forshee 	{ { 0x62, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xcf, 0xcf,
1261d9f2626SSebastian Kapfer 		ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED },
12795f75e91SYunkang Tang 	{ { 0x73, 0x00, 0x14 }, 0x00, ALPS_PROTO_V6, 0xff, 0xff, ALPS_DUALPOINT },		/* Dell XT2 */
12825bded7cSSeth Forshee 	{ { 0x73, 0x02, 0x50 }, 0x00, ALPS_PROTO_V2, 0xcf, 0xcf, ALPS_FOUR_BUTTONS },		/* Dell Vostro 1400 */
12925bded7cSSeth Forshee 	{ { 0x52, 0x01, 0x14 }, 0x00, ALPS_PROTO_V2, 0xff, 0xff,
130eb8bff85SThomas Bächler 		ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED },				/* Toshiba Tecra A11-11L */
13125bded7cSSeth Forshee 	{ { 0x73, 0x02, 0x64 },	0x8a, ALPS_PROTO_V4, 0x8f, 0x8f, 0 },
1321da177e4SLinus Torvalds };
1331da177e4SLinus Torvalds 
13424af5cb9SKevin Cernekee static void alps_set_abs_params_st(struct alps_data *priv,
13524af5cb9SKevin Cernekee 				   struct input_dev *dev1);
13624af5cb9SKevin Cernekee static void alps_set_abs_params_mt(struct alps_data *priv,
13724af5cb9SKevin Cernekee 				   struct input_dev *dev1);
13824af5cb9SKevin Cernekee 
1391da177e4SLinus Torvalds /*
1401da177e4SLinus Torvalds  * XXX - this entry is suspicious. First byte has zero lower nibble,
1411da177e4SLinus Torvalds  * which is what a normal mouse would report. Also, the value 0x0e
1421da177e4SLinus Torvalds  * isn't valid per PS/2 spec.
1431da177e4SLinus Torvalds  */
1441da177e4SLinus Torvalds 
145d4b347b2SSeth Forshee /* Packet formats are described in Documentation/input/alps.txt */
1461da177e4SLinus Torvalds 
14799df65e7SKevin Cernekee static bool alps_is_valid_first_byte(struct alps_data *priv,
1481d9f2626SSebastian Kapfer 				     unsigned char data)
1491d9f2626SSebastian Kapfer {
15099df65e7SKevin Cernekee 	return (data & priv->mask0) == priv->byte0;
1511d9f2626SSebastian Kapfer }
1521d9f2626SSebastian Kapfer 
1531d9f2626SSebastian Kapfer static void alps_report_buttons(struct psmouse *psmouse,
1541d9f2626SSebastian Kapfer 				struct input_dev *dev1, struct input_dev *dev2,
1551d9f2626SSebastian Kapfer 				int left, int right, int middle)
1561d9f2626SSebastian Kapfer {
1571d9f2626SSebastian Kapfer 	struct input_dev *dev;
1581d9f2626SSebastian Kapfer 
1591d9f2626SSebastian Kapfer 	/*
1601d9f2626SSebastian Kapfer 	 * If shared button has already been reported on the
1611d9f2626SSebastian Kapfer 	 * other device (dev2) then this event should be also
1621d9f2626SSebastian Kapfer 	 * sent through that device.
1631d9f2626SSebastian Kapfer 	 */
1641d9f2626SSebastian Kapfer 	dev = test_bit(BTN_LEFT, dev2->key) ? dev2 : dev1;
1651d9f2626SSebastian Kapfer 	input_report_key(dev, BTN_LEFT, left);
1661d9f2626SSebastian Kapfer 
1671d9f2626SSebastian Kapfer 	dev = test_bit(BTN_RIGHT, dev2->key) ? dev2 : dev1;
1681d9f2626SSebastian Kapfer 	input_report_key(dev, BTN_RIGHT, right);
1691d9f2626SSebastian Kapfer 
1701d9f2626SSebastian Kapfer 	dev = test_bit(BTN_MIDDLE, dev2->key) ? dev2 : dev1;
1711d9f2626SSebastian Kapfer 	input_report_key(dev, BTN_MIDDLE, middle);
1721d9f2626SSebastian Kapfer 
1731d9f2626SSebastian Kapfer 	/*
1741d9f2626SSebastian Kapfer 	 * Sync the _other_ device now, we'll do the first
1751d9f2626SSebastian Kapfer 	 * device later once we report the rest of the events.
1761d9f2626SSebastian Kapfer 	 */
1771d9f2626SSebastian Kapfer 	input_sync(dev2);
1781d9f2626SSebastian Kapfer }
1791d9f2626SSebastian Kapfer 
18025bded7cSSeth Forshee static void alps_process_packet_v1_v2(struct psmouse *psmouse)
1811da177e4SLinus Torvalds {
1821da177e4SLinus Torvalds 	struct alps_data *priv = psmouse->private;
1831da177e4SLinus Torvalds 	unsigned char *packet = psmouse->packet;
1842e5b636bSDmitry Torokhov 	struct input_dev *dev = psmouse->dev;
1852e5b636bSDmitry Torokhov 	struct input_dev *dev2 = priv->dev2;
1861da177e4SLinus Torvalds 	int x, y, z, ges, fin, left, right, middle;
187c30b4c10SIvan Casado Ruiz 	int back = 0, forward = 0;
1881da177e4SLinus Torvalds 
18999df65e7SKevin Cernekee 	if (priv->proto_version == ALPS_PROTO_V1) {
190d2f4012fSYotam Medini 		left = packet[2] & 0x10;
191d2f4012fSYotam Medini 		right = packet[2] & 0x08;
1921da177e4SLinus Torvalds 		middle = 0;
1931da177e4SLinus Torvalds 		x = packet[1] | ((packet[0] & 0x07) << 7);
1941da177e4SLinus Torvalds 		y = packet[4] | ((packet[3] & 0x07) << 7);
1951da177e4SLinus Torvalds 		z = packet[5];
1961da177e4SLinus Torvalds 	} else {
1971da177e4SLinus Torvalds 		left = packet[3] & 1;
1981da177e4SLinus Torvalds 		right = packet[3] & 2;
1991da177e4SLinus Torvalds 		middle = packet[3] & 4;
2001da177e4SLinus Torvalds 		x = packet[1] | ((packet[2] & 0x78) << (7 - 3));
2011da177e4SLinus Torvalds 		y = packet[4] | ((packet[3] & 0x70) << (7 - 4));
2021da177e4SLinus Torvalds 		z = packet[5];
2031da177e4SLinus Torvalds 	}
2041da177e4SLinus Torvalds 
20599df65e7SKevin Cernekee 	if (priv->flags & ALPS_FW_BK_1) {
2063c00bb96SLaszlo Kajan 		back = packet[0] & 0x10;
2073c00bb96SLaszlo Kajan 		forward = packet[2] & 4;
208c30b4c10SIvan Casado Ruiz 	}
209c30b4c10SIvan Casado Ruiz 
21099df65e7SKevin Cernekee 	if (priv->flags & ALPS_FW_BK_2) {
211c30b4c10SIvan Casado Ruiz 		back = packet[3] & 4;
212c30b4c10SIvan Casado Ruiz 		forward = packet[2] & 4;
213c30b4c10SIvan Casado Ruiz 		if ((middle = forward && back))
214c30b4c10SIvan Casado Ruiz 			forward = back = 0;
215c30b4c10SIvan Casado Ruiz 	}
216c30b4c10SIvan Casado Ruiz 
2171da177e4SLinus Torvalds 	ges = packet[2] & 1;
2181da177e4SLinus Torvalds 	fin = packet[2] & 2;
2191da177e4SLinus Torvalds 
22099df65e7SKevin Cernekee 	if ((priv->flags & ALPS_DUALPOINT) && z == 127) {
2211da177e4SLinus Torvalds 		input_report_rel(dev2, REL_X,  (x > 383 ? (x - 768) : x));
2221da177e4SLinus Torvalds 		input_report_rel(dev2, REL_Y, -(y > 255 ? (y - 512) : y));
223d7ed5d88SUlrich Dangel 
2241d9f2626SSebastian Kapfer 		alps_report_buttons(psmouse, dev2, dev, left, right, middle);
225d7ed5d88SUlrich Dangel 
2261da177e4SLinus Torvalds 		input_sync(dev2);
2271da177e4SLinus Torvalds 		return;
2281da177e4SLinus Torvalds 	}
2291da177e4SLinus Torvalds 
2301d9f2626SSebastian Kapfer 	alps_report_buttons(psmouse, dev, dev2, left, right, middle);
231d7ed5d88SUlrich Dangel 
2321da177e4SLinus Torvalds 	/* Convert hardware tap to a reasonable Z value */
23371bb21b6SMaxim Levitsky 	if (ges && !fin)
23471bb21b6SMaxim Levitsky 		z = 40;
2351da177e4SLinus Torvalds 
2361da177e4SLinus Torvalds 	/*
2371da177e4SLinus Torvalds 	 * A "tap and drag" operation is reported by the hardware as a transition
2381da177e4SLinus Torvalds 	 * from (!fin && ges) to (fin && ges). This should be translated to the
2391da177e4SLinus Torvalds 	 * sequence Z>0, Z==0, Z>0, so the Z==0 event has to be generated manually.
2401da177e4SLinus Torvalds 	 */
2411da177e4SLinus Torvalds 	if (ges && fin && !priv->prev_fin) {
2421da177e4SLinus Torvalds 		input_report_abs(dev, ABS_X, x);
2431da177e4SLinus Torvalds 		input_report_abs(dev, ABS_Y, y);
2441da177e4SLinus Torvalds 		input_report_abs(dev, ABS_PRESSURE, 0);
2451da177e4SLinus Torvalds 		input_report_key(dev, BTN_TOOL_FINGER, 0);
2461da177e4SLinus Torvalds 		input_sync(dev);
2471da177e4SLinus Torvalds 	}
2481da177e4SLinus Torvalds 	priv->prev_fin = fin;
2491da177e4SLinus Torvalds 
25071bb21b6SMaxim Levitsky 	if (z > 30)
25171bb21b6SMaxim Levitsky 		input_report_key(dev, BTN_TOUCH, 1);
25271bb21b6SMaxim Levitsky 	if (z < 25)
25371bb21b6SMaxim Levitsky 		input_report_key(dev, BTN_TOUCH, 0);
2541da177e4SLinus Torvalds 
2551da177e4SLinus Torvalds 	if (z > 0) {
2561da177e4SLinus Torvalds 		input_report_abs(dev, ABS_X, x);
2571da177e4SLinus Torvalds 		input_report_abs(dev, ABS_Y, y);
2581da177e4SLinus Torvalds 	}
2591da177e4SLinus Torvalds 
2601da177e4SLinus Torvalds 	input_report_abs(dev, ABS_PRESSURE, z);
2611da177e4SLinus Torvalds 	input_report_key(dev, BTN_TOOL_FINGER, z > 0);
2621da177e4SLinus Torvalds 
26399df65e7SKevin Cernekee 	if (priv->flags & ALPS_WHEEL)
264e6c047b9SVojtech Pavlik 		input_report_rel(dev, REL_WHEEL, ((packet[2] << 1) & 0x08) - ((packet[0] >> 4) & 0x07));
2651da177e4SLinus Torvalds 
26699df65e7SKevin Cernekee 	if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
267c30b4c10SIvan Casado Ruiz 		input_report_key(dev, BTN_FORWARD, forward);
268c30b4c10SIvan Casado Ruiz 		input_report_key(dev, BTN_BACK, back);
2691da177e4SLinus Torvalds 	}
2701da177e4SLinus Torvalds 
27199df65e7SKevin Cernekee 	if (priv->flags & ALPS_FOUR_BUTTONS) {
27271bb21b6SMaxim Levitsky 		input_report_key(dev, BTN_0, packet[2] & 4);
27371bb21b6SMaxim Levitsky 		input_report_key(dev, BTN_1, packet[0] & 0x10);
27471bb21b6SMaxim Levitsky 		input_report_key(dev, BTN_2, packet[3] & 4);
27571bb21b6SMaxim Levitsky 		input_report_key(dev, BTN_3, packet[0] & 0x20);
27671bb21b6SMaxim Levitsky 	}
27771bb21b6SMaxim Levitsky 
2781da177e4SLinus Torvalds 	input_sync(dev);
2791da177e4SLinus Torvalds }
2801da177e4SLinus Torvalds 
28101ce661fSSeth Forshee /*
282ee65d4b3SYunkang Tang  * Process bitmap data for V5 protocols. Return value is null.
283ee65d4b3SYunkang Tang  *
284ee65d4b3SYunkang Tang  * The bitmaps don't have enough data to track fingers, so this function
285ee65d4b3SYunkang Tang  * only generates points representing a bounding box of at most two contacts.
28602d04254SHans de Goede  * These two points are returned in fields->mt.
287ee65d4b3SYunkang Tang  */
288ee65d4b3SYunkang Tang static void alps_process_bitmap_dolphin(struct alps_data *priv,
28902d04254SHans de Goede 					struct alps_fields *fields)
290ee65d4b3SYunkang Tang {
291ee65d4b3SYunkang Tang 	int box_middle_x, box_middle_y;
292ee65d4b3SYunkang Tang 	unsigned int x_map, y_map;
293ee65d4b3SYunkang Tang 	unsigned char start_bit, end_bit;
294ee65d4b3SYunkang Tang 	unsigned char x_msb, x_lsb, y_msb, y_lsb;
295ee65d4b3SYunkang Tang 
296ee65d4b3SYunkang Tang 	x_map = fields->x_map;
297ee65d4b3SYunkang Tang 	y_map = fields->y_map;
298ee65d4b3SYunkang Tang 
299ee65d4b3SYunkang Tang 	if (!x_map || !y_map)
300ee65d4b3SYunkang Tang 		return;
301ee65d4b3SYunkang Tang 
302ee65d4b3SYunkang Tang 	/* Get Most-significant and Least-significant bit */
303ee65d4b3SYunkang Tang 	x_msb = fls(x_map);
304ee65d4b3SYunkang Tang 	x_lsb = ffs(x_map);
305ee65d4b3SYunkang Tang 	y_msb = fls(y_map);
306ee65d4b3SYunkang Tang 	y_lsb = ffs(y_map);
307ee65d4b3SYunkang Tang 
308ee65d4b3SYunkang Tang 	/* Most-significant bit should never exceed max sensor line number */
309ee65d4b3SYunkang Tang 	if (x_msb > priv->x_bits || y_msb > priv->y_bits)
310ee65d4b3SYunkang Tang 		return;
311ee65d4b3SYunkang Tang 
312ee65d4b3SYunkang Tang 	if (fields->fingers > 1) {
313ee65d4b3SYunkang Tang 		start_bit = priv->x_bits - x_msb;
314ee65d4b3SYunkang Tang 		end_bit = priv->x_bits - x_lsb;
315ee65d4b3SYunkang Tang 		box_middle_x = (priv->x_max * (start_bit + end_bit)) /
316ee65d4b3SYunkang Tang 				(2 * (priv->x_bits - 1));
317ee65d4b3SYunkang Tang 
318ee65d4b3SYunkang Tang 		start_bit = y_lsb - 1;
319ee65d4b3SYunkang Tang 		end_bit = y_msb - 1;
320ee65d4b3SYunkang Tang 		box_middle_y = (priv->y_max * (start_bit + end_bit)) /
321ee65d4b3SYunkang Tang 				(2 * (priv->y_bits - 1));
32202d04254SHans de Goede 		fields->mt[0] = fields->st;
32302d04254SHans de Goede 		fields->mt[1].x = 2 * box_middle_x - fields->mt[0].x;
32402d04254SHans de Goede 		fields->mt[1].y = 2 * box_middle_y - fields->mt[0].y;
325ee65d4b3SYunkang Tang 	}
326ee65d4b3SYunkang Tang }
327ee65d4b3SYunkang Tang 
328036e6c7bSHans de Goede static void alps_get_bitmap_points(unsigned int map,
329036e6c7bSHans de Goede 				   struct alps_bitmap_point *low,
330036e6c7bSHans de Goede 				   struct alps_bitmap_point *high,
331036e6c7bSHans de Goede 				   int *fingers)
332036e6c7bSHans de Goede {
333036e6c7bSHans de Goede 	struct alps_bitmap_point *point;
334036e6c7bSHans de Goede 	int i, bit, prev_bit = 0;
335036e6c7bSHans de Goede 
336036e6c7bSHans de Goede 	point = low;
337036e6c7bSHans de Goede 	for (i = 0; map != 0; i++, map >>= 1) {
338036e6c7bSHans de Goede 		bit = map & 1;
339036e6c7bSHans de Goede 		if (bit) {
340036e6c7bSHans de Goede 			if (!prev_bit) {
341036e6c7bSHans de Goede 				point->start_bit = i;
342105affbfSHans de Goede 				point->num_bits = 0;
343036e6c7bSHans de Goede 				(*fingers)++;
344036e6c7bSHans de Goede 			}
345036e6c7bSHans de Goede 			point->num_bits++;
346036e6c7bSHans de Goede 		} else {
347036e6c7bSHans de Goede 			if (prev_bit)
348036e6c7bSHans de Goede 				point = high;
349036e6c7bSHans de Goede 		}
350036e6c7bSHans de Goede 		prev_bit = bit;
351036e6c7bSHans de Goede 	}
352036e6c7bSHans de Goede }
353036e6c7bSHans de Goede 
354ee65d4b3SYunkang Tang /*
35501ce661fSSeth Forshee  * Process bitmap data from v3 and v4 protocols. Returns the number of
35601ce661fSSeth Forshee  * fingers detected. A return value of 0 means at least one of the
35701ce661fSSeth Forshee  * bitmaps was empty.
35801ce661fSSeth Forshee  *
35901ce661fSSeth Forshee  * The bitmaps don't have enough data to track fingers, so this function
36001ce661fSSeth Forshee  * only generates points representing a bounding box of all contacts.
36102d04254SHans de Goede  * These points are returned in fields->mt when the return value
36201ce661fSSeth Forshee  * is greater than 0.
36301ce661fSSeth Forshee  */
3647a9f73e7SKevin Cernekee static int alps_process_bitmap(struct alps_data *priv,
36502d04254SHans de Goede 			       struct alps_fields *fields)
36601ce661fSSeth Forshee {
367036e6c7bSHans de Goede 	int i, fingers_x = 0, fingers_y = 0, fingers;
36801ce661fSSeth Forshee 	struct alps_bitmap_point x_low = {0,}, x_high = {0,};
36901ce661fSSeth Forshee 	struct alps_bitmap_point y_low = {0,}, y_high = {0,};
37001ce661fSSeth Forshee 
37102d04254SHans de Goede 	if (!fields->x_map || !fields->y_map)
37201ce661fSSeth Forshee 		return 0;
37301ce661fSSeth Forshee 
37402d04254SHans de Goede 	alps_get_bitmap_points(fields->x_map, &x_low, &x_high, &fingers_x);
37502d04254SHans de Goede 	alps_get_bitmap_points(fields->y_map, &y_low, &y_high, &fingers_y);
37601ce661fSSeth Forshee 
37701ce661fSSeth Forshee 	/*
37801ce661fSSeth Forshee 	 * Fingers can overlap, so we use the maximum count of fingers
37901ce661fSSeth Forshee 	 * on either axis as the finger count.
38001ce661fSSeth Forshee 	 */
38101ce661fSSeth Forshee 	fingers = max(fingers_x, fingers_y);
38201ce661fSSeth Forshee 
38301ce661fSSeth Forshee 	/*
38420bea68bSHans de Goede 	 * If an axis reports only a single contact, we have overlapping or
38520bea68bSHans de Goede 	 * adjacent fingers. Divide the single contact between the two points.
38601ce661fSSeth Forshee 	 */
38701ce661fSSeth Forshee 	if (fingers_x == 1) {
38828835f45SHans de Goede 		i = (x_low.num_bits - 1) / 2;
38901ce661fSSeth Forshee 		x_low.num_bits = x_low.num_bits - i;
39001ce661fSSeth Forshee 		x_high.start_bit = x_low.start_bit + i;
39101ce661fSSeth Forshee 		x_high.num_bits = max(i, 1);
39220bea68bSHans de Goede 	}
39320bea68bSHans de Goede 	if (fingers_y == 1) {
39428835f45SHans de Goede 		i = (y_low.num_bits - 1) / 2;
39501ce661fSSeth Forshee 		y_low.num_bits = y_low.num_bits - i;
39601ce661fSSeth Forshee 		y_high.start_bit = y_low.start_bit + i;
39701ce661fSSeth Forshee 		y_high.num_bits = max(i, 1);
39801ce661fSSeth Forshee 	}
39901ce661fSSeth Forshee 
40002d04254SHans de Goede 	fields->mt[0].x =
40102d04254SHans de Goede 		(priv->x_max * (2 * x_low.start_bit + x_low.num_bits - 1)) /
4027a9f73e7SKevin Cernekee 		(2 * (priv->x_bits - 1));
40302d04254SHans de Goede 	fields->mt[0].y =
40402d04254SHans de Goede 		(priv->y_max * (2 * y_low.start_bit + y_low.num_bits - 1)) /
4057a9f73e7SKevin Cernekee 		(2 * (priv->y_bits - 1));
40601ce661fSSeth Forshee 
40702d04254SHans de Goede 	fields->mt[1].x =
40802d04254SHans de Goede 		(priv->x_max * (2 * x_high.start_bit + x_high.num_bits - 1)) /
4097a9f73e7SKevin Cernekee 		(2 * (priv->x_bits - 1));
41002d04254SHans de Goede 	fields->mt[1].y =
41102d04254SHans de Goede 		(priv->y_max * (2 * y_high.start_bit + y_high.num_bits - 1)) /
4127a9f73e7SKevin Cernekee 		(2 * (priv->y_bits - 1));
41301ce661fSSeth Forshee 
41440e8f53bSHans de Goede 	/* y-bitmap order is reversed, except on rushmore */
41540e8f53bSHans de Goede 	if (!(priv->flags & ALPS_IS_RUSHMORE)) {
41602d04254SHans de Goede 		fields->mt[0].y = priv->y_max - fields->mt[0].y;
41702d04254SHans de Goede 		fields->mt[1].y = priv->y_max - fields->mt[1].y;
41840e8f53bSHans de Goede 	}
41940e8f53bSHans de Goede 
42001ce661fSSeth Forshee 	return fingers;
42101ce661fSSeth Forshee }
42201ce661fSSeth Forshee 
423cdf333efSHans de Goede static void alps_set_slot(struct input_dev *dev, int slot, int x, int y)
42401ce661fSSeth Forshee {
42501ce661fSSeth Forshee 	input_mt_slot(dev, slot);
426cdf333efSHans de Goede 	input_mt_report_slot_state(dev, MT_TOOL_FINGER, true);
42701ce661fSSeth Forshee 	input_report_abs(dev, ABS_MT_POSITION_X, x);
42801ce661fSSeth Forshee 	input_report_abs(dev, ABS_MT_POSITION_Y, y);
42901ce661fSSeth Forshee }
43001ce661fSSeth Forshee 
431cdf333efSHans de Goede static void alps_report_mt_data(struct psmouse *psmouse, int n)
43201ce661fSSeth Forshee {
43302d04254SHans de Goede 	struct alps_data *priv = psmouse->private;
43402d04254SHans de Goede 	struct input_dev *dev = psmouse->dev;
43502d04254SHans de Goede 	struct alps_fields *f = &priv->f;
436cdf333efSHans de Goede 	int i, slot[MAX_TOUCHES];
43702d04254SHans de Goede 
438cdf333efSHans de Goede 	input_mt_assign_slots(dev, slot, f->mt, n);
439cdf333efSHans de Goede 	for (i = 0; i < n; i++)
440cdf333efSHans de Goede 		alps_set_slot(dev, slot[i], f->mt[i].x, f->mt[i].y);
441cdf333efSHans de Goede 
442cdf333efSHans de Goede 	input_mt_sync_frame(dev);
44301ce661fSSeth Forshee }
44401ce661fSSeth Forshee 
44568c21870SHans de Goede static void alps_report_semi_mt_data(struct psmouse *psmouse, int fingers)
44668c21870SHans de Goede {
44768c21870SHans de Goede 	struct alps_data *priv = psmouse->private;
44868c21870SHans de Goede 	struct input_dev *dev = psmouse->dev;
44968c21870SHans de Goede 	struct alps_fields *f = &priv->f;
45068c21870SHans de Goede 
45168c21870SHans de Goede 	/* Use st data when we don't have mt data */
45268c21870SHans de Goede 	if (fingers < 2) {
45368c21870SHans de Goede 		f->mt[0].x = f->st.x;
45468c21870SHans de Goede 		f->mt[0].y = f->st.y;
45568c21870SHans de Goede 		fingers = f->pressure > 0 ? 1 : 0;
45668c21870SHans de Goede 	}
45768c21870SHans de Goede 
45899d9996cSHans de Goede 	alps_report_mt_data(psmouse, (fingers <= 2) ? fingers : 2);
45968c21870SHans de Goede 
46068c21870SHans de Goede 	input_mt_report_finger_count(dev, fingers);
46168c21870SHans de Goede 
46268c21870SHans de Goede 	input_report_key(dev, BTN_LEFT, f->left);
46368c21870SHans de Goede 	input_report_key(dev, BTN_RIGHT, f->right);
46468c21870SHans de Goede 	input_report_key(dev, BTN_MIDDLE, f->middle);
46568c21870SHans de Goede 
46668c21870SHans de Goede 	input_report_abs(dev, ABS_PRESSURE, f->pressure);
46768c21870SHans de Goede 
46868c21870SHans de Goede 	input_sync(dev);
46968c21870SHans de Goede }
47068c21870SHans de Goede 
47125bded7cSSeth Forshee static void alps_process_trackstick_packet_v3(struct psmouse *psmouse)
47225bded7cSSeth Forshee {
47325bded7cSSeth Forshee 	struct alps_data *priv = psmouse->private;
47425bded7cSSeth Forshee 	unsigned char *packet = psmouse->packet;
47525bded7cSSeth Forshee 	struct input_dev *dev = priv->dev2;
47625bded7cSSeth Forshee 	int x, y, z, left, right, middle;
47725bded7cSSeth Forshee 
47834412ba2SPali Rohár 	/* It should be a DualPoint when received trackstick packet */
47934412ba2SPali Rohár 	if (!(priv->flags & ALPS_DUALPOINT)) {
48034412ba2SPali Rohár 		psmouse_warn(psmouse,
48134412ba2SPali Rohár 			     "Rejected trackstick packet from non DualPoint device");
48234412ba2SPali Rohár 		return;
48334412ba2SPali Rohár 	}
48434412ba2SPali Rohár 
48525bded7cSSeth Forshee 	/* Sanity check packet */
48625bded7cSSeth Forshee 	if (!(packet[0] & 0x40)) {
48725bded7cSSeth Forshee 		psmouse_dbg(psmouse, "Bad trackstick packet, discarding\n");
48825bded7cSSeth Forshee 		return;
48925bded7cSSeth Forshee 	}
49025bded7cSSeth Forshee 
49125bded7cSSeth Forshee 	/*
49225bded7cSSeth Forshee 	 * There's a special packet that seems to indicate the end
49325bded7cSSeth Forshee 	 * of a stream of trackstick data. Filter these out.
49425bded7cSSeth Forshee 	 */
49525bded7cSSeth Forshee 	if (packet[1] == 0x7f && packet[2] == 0x7f && packet[4] == 0x7f)
49625bded7cSSeth Forshee 		return;
49725bded7cSSeth Forshee 
49825bded7cSSeth Forshee 	x = (s8)(((packet[0] & 0x20) << 2) | (packet[1] & 0x7f));
49925bded7cSSeth Forshee 	y = (s8)(((packet[0] & 0x10) << 3) | (packet[2] & 0x7f));
50025bded7cSSeth Forshee 	z = (packet[4] & 0x7c) >> 2;
50125bded7cSSeth Forshee 
50225bded7cSSeth Forshee 	/*
50325bded7cSSeth Forshee 	 * The x and y values tend to be quite large, and when used
50425bded7cSSeth Forshee 	 * alone the trackstick is difficult to use. Scale them down
50525bded7cSSeth Forshee 	 * to compensate.
50625bded7cSSeth Forshee 	 */
50725bded7cSSeth Forshee 	x /= 8;
50825bded7cSSeth Forshee 	y /= 8;
50925bded7cSSeth Forshee 
51025bded7cSSeth Forshee 	input_report_rel(dev, REL_X, x);
51125bded7cSSeth Forshee 	input_report_rel(dev, REL_Y, -y);
51225bded7cSSeth Forshee 
51325bded7cSSeth Forshee 	/*
51425bded7cSSeth Forshee 	 * Most ALPS models report the trackstick buttons in the touchpad
51525bded7cSSeth Forshee 	 * packets, but a few report them here. No reliable way has been
51625bded7cSSeth Forshee 	 * found to differentiate between the models upfront, so we enable
51725bded7cSSeth Forshee 	 * the quirk in response to seeing a button press in the trackstick
51825bded7cSSeth Forshee 	 * packet.
51925bded7cSSeth Forshee 	 */
52025bded7cSSeth Forshee 	left = packet[3] & 0x01;
52125bded7cSSeth Forshee 	right = packet[3] & 0x02;
52225bded7cSSeth Forshee 	middle = packet[3] & 0x04;
52325bded7cSSeth Forshee 
52425bded7cSSeth Forshee 	if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) &&
52525bded7cSSeth Forshee 	    (left || right || middle))
52625bded7cSSeth Forshee 		priv->quirks |= ALPS_QUIRK_TRACKSTICK_BUTTONS;
52725bded7cSSeth Forshee 
52825bded7cSSeth Forshee 	if (priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) {
52925bded7cSSeth Forshee 		input_report_key(dev, BTN_LEFT, left);
53025bded7cSSeth Forshee 		input_report_key(dev, BTN_RIGHT, right);
53125bded7cSSeth Forshee 		input_report_key(dev, BTN_MIDDLE, middle);
53225bded7cSSeth Forshee 	}
53325bded7cSSeth Forshee 
53425bded7cSSeth Forshee 	input_sync(dev);
53525bded7cSSeth Forshee 	return;
53625bded7cSSeth Forshee }
53725bded7cSSeth Forshee 
538f85e5001SKevin Cernekee static void alps_decode_buttons_v3(struct alps_fields *f, unsigned char *p)
539f85e5001SKevin Cernekee {
540f85e5001SKevin Cernekee 	f->left = !!(p[3] & 0x01);
541f85e5001SKevin Cernekee 	f->right = !!(p[3] & 0x02);
542f85e5001SKevin Cernekee 	f->middle = !!(p[3] & 0x04);
543f85e5001SKevin Cernekee 
544f85e5001SKevin Cernekee 	f->ts_left = !!(p[3] & 0x10);
545f85e5001SKevin Cernekee 	f->ts_right = !!(p[3] & 0x20);
546f85e5001SKevin Cernekee 	f->ts_middle = !!(p[3] & 0x40);
547f85e5001SKevin Cernekee }
548f85e5001SKevin Cernekee 
54938c11eaaSHans de Goede static int alps_decode_pinnacle(struct alps_fields *f, unsigned char *p,
550ee65d4b3SYunkang Tang 				 struct psmouse *psmouse)
551f85e5001SKevin Cernekee {
552f85e5001SKevin Cernekee 	f->first_mp = !!(p[4] & 0x40);
553f85e5001SKevin Cernekee 	f->is_mp = !!(p[0] & 0x40);
554f85e5001SKevin Cernekee 
555f85e5001SKevin Cernekee 	f->fingers = (p[5] & 0x3) + 1;
556f85e5001SKevin Cernekee 	f->x_map = ((p[4] & 0x7e) << 8) |
557f85e5001SKevin Cernekee 		   ((p[1] & 0x7f) << 2) |
558f85e5001SKevin Cernekee 		   ((p[0] & 0x30) >> 4);
559f85e5001SKevin Cernekee 	f->y_map = ((p[3] & 0x70) << 4) |
560f85e5001SKevin Cernekee 		   ((p[2] & 0x7f) << 1) |
561f85e5001SKevin Cernekee 		   (p[4] & 0x01);
562f85e5001SKevin Cernekee 
56302d04254SHans de Goede 	f->st.x = ((p[1] & 0x7f) << 4) | ((p[4] & 0x30) >> 2) |
564f85e5001SKevin Cernekee 	       ((p[0] & 0x30) >> 4);
56502d04254SHans de Goede 	f->st.y = ((p[2] & 0x7f) << 4) | (p[4] & 0x0f);
56602d04254SHans de Goede 	f->pressure = p[5] & 0x7f;
567f85e5001SKevin Cernekee 
568f85e5001SKevin Cernekee 	alps_decode_buttons_v3(f, p);
56938c11eaaSHans de Goede 
57038c11eaaSHans de Goede 	return 0;
571f85e5001SKevin Cernekee }
572f85e5001SKevin Cernekee 
57338c11eaaSHans de Goede static int alps_decode_rushmore(struct alps_fields *f, unsigned char *p,
574ee65d4b3SYunkang Tang 				 struct psmouse *psmouse)
5751302bac3SKevin Cernekee {
576ee65d4b3SYunkang Tang 	alps_decode_pinnacle(f, p, psmouse);
5771302bac3SKevin Cernekee 
578f105e34aSYunkang Tang 	/* Rushmore's packet decode has a bit difference with Pinnacle's */
579f105e34aSYunkang Tang 	f->is_mp = !!(p[5] & 0x40);
580f105e34aSYunkang Tang 	f->fingers = max((p[5] & 0x3), ((p[5] >> 2) & 0x3)) + 1;
5811302bac3SKevin Cernekee 	f->x_map |= (p[5] & 0x10) << 11;
5821302bac3SKevin Cernekee 	f->y_map |= (p[5] & 0x20) << 6;
58338c11eaaSHans de Goede 
58438c11eaaSHans de Goede 	return 0;
5851302bac3SKevin Cernekee }
5861302bac3SKevin Cernekee 
58738c11eaaSHans de Goede static int alps_decode_dolphin(struct alps_fields *f, unsigned char *p,
588ee65d4b3SYunkang Tang 				struct psmouse *psmouse)
58975af9e56SDave Turvene {
590ee65d4b3SYunkang Tang 	u64 palm_data = 0;
591ee65d4b3SYunkang Tang 	struct alps_data *priv = psmouse->private;
592ee65d4b3SYunkang Tang 
59375af9e56SDave Turvene 	f->first_mp = !!(p[0] & 0x02);
59475af9e56SDave Turvene 	f->is_mp = !!(p[0] & 0x20);
59575af9e56SDave Turvene 
596ee65d4b3SYunkang Tang 	if (!f->is_mp) {
59702d04254SHans de Goede 		f->st.x = ((p[1] & 0x7f) | ((p[4] & 0x0f) << 7));
59802d04254SHans de Goede 		f->st.y = ((p[2] & 0x7f) | ((p[4] & 0xf0) << 3));
59902d04254SHans de Goede 		f->pressure = (p[0] & 4) ? 0 : p[5] & 0x7f;
60075af9e56SDave Turvene 		alps_decode_buttons_v3(f, p);
601ee65d4b3SYunkang Tang 	} else {
602ee65d4b3SYunkang Tang 		f->fingers = ((p[0] & 0x6) >> 1 |
603ee65d4b3SYunkang Tang 		     (p[0] & 0x10) >> 2);
604ee65d4b3SYunkang Tang 
605ee65d4b3SYunkang Tang 		palm_data = (p[1] & 0x7f) |
606ee65d4b3SYunkang Tang 			    ((p[2] & 0x7f) << 7) |
607ee65d4b3SYunkang Tang 			    ((p[4] & 0x7f) << 14) |
608ee65d4b3SYunkang Tang 			    ((p[5] & 0x7f) << 21) |
609ee65d4b3SYunkang Tang 			    ((p[3] & 0x07) << 28) |
610ee65d4b3SYunkang Tang 			    (((u64)p[3] & 0x70) << 27) |
611ee65d4b3SYunkang Tang 			    (((u64)p[0] & 0x01) << 34);
612ee65d4b3SYunkang Tang 
613ee65d4b3SYunkang Tang 		/* Y-profile is stored in P(0) to p(n-1), n = y_bits; */
614ee65d4b3SYunkang Tang 		f->y_map = palm_data & (BIT(priv->y_bits) - 1);
615ee65d4b3SYunkang Tang 
616ee65d4b3SYunkang Tang 		/* X-profile is stored in p(n) to p(n+m-1), m = x_bits; */
617ee65d4b3SYunkang Tang 		f->x_map = (palm_data >> priv->y_bits) &
618ee65d4b3SYunkang Tang 			   (BIT(priv->x_bits) - 1);
619ee65d4b3SYunkang Tang 	}
62038c11eaaSHans de Goede 
62138c11eaaSHans de Goede 	return 0;
62275af9e56SDave Turvene }
62375af9e56SDave Turvene 
624ee65d4b3SYunkang Tang static void alps_process_touchpad_packet_v3_v5(struct psmouse *psmouse)
62525bded7cSSeth Forshee {
62625bded7cSSeth Forshee 	struct alps_data *priv = psmouse->private;
62725bded7cSSeth Forshee 	unsigned char *packet = psmouse->packet;
62825bded7cSSeth Forshee 	struct input_dev *dev2 = priv->dev2;
62902d04254SHans de Goede 	struct alps_fields *f = &priv->f;
63002d04254SHans de Goede 	int fingers = 0;
631f85e5001SKevin Cernekee 
63202d04254SHans de Goede 	memset(f, 0, sizeof(*f));
63302d04254SHans de Goede 
63402d04254SHans de Goede 	priv->decode_fields(f, packet, psmouse);
63525bded7cSSeth Forshee 
63625bded7cSSeth Forshee 	/*
63701ce661fSSeth Forshee 	 * There's no single feature of touchpad position and bitmap packets
63801ce661fSSeth Forshee 	 * that can be used to distinguish between them. We rely on the fact
63901ce661fSSeth Forshee 	 * that a bitmap packet should always follow a position packet with
64001ce661fSSeth Forshee 	 * bit 6 of packet[4] set.
64125bded7cSSeth Forshee 	 */
64225bded7cSSeth Forshee 	if (priv->multi_packet) {
64325bded7cSSeth Forshee 		/*
64425bded7cSSeth Forshee 		 * Sometimes a position packet will indicate a multi-packet
64525bded7cSSeth Forshee 		 * sequence, but then what follows is another position
64625bded7cSSeth Forshee 		 * packet. Check for this, and when it happens process the
64725bded7cSSeth Forshee 		 * position packet as usual.
64825bded7cSSeth Forshee 		 */
64902d04254SHans de Goede 		if (f->is_mp) {
65002d04254SHans de Goede 			fingers = f->fingers;
651ee65d4b3SYunkang Tang 			if (priv->proto_version == ALPS_PROTO_V3) {
65202d04254SHans de Goede 				if (alps_process_bitmap(priv, f) == 0)
65320bea68bSHans de Goede 					fingers = 0; /* Use st data */
65401ce661fSSeth Forshee 
65501ce661fSSeth Forshee 				/* Now process position packet */
65602d04254SHans de Goede 				priv->decode_fields(f, priv->multi_data,
657ee65d4b3SYunkang Tang 						    psmouse);
658ee65d4b3SYunkang Tang 			} else {
659ee65d4b3SYunkang Tang 				/*
660ee65d4b3SYunkang Tang 				 * Because Dolphin uses position packet's
661ee65d4b3SYunkang Tang 				 * coordinate data as Pt1 and uses it to
662ee65d4b3SYunkang Tang 				 * calculate Pt2, so we need to do position
663ee65d4b3SYunkang Tang 				 * packet decode first.
664ee65d4b3SYunkang Tang 				 */
66502d04254SHans de Goede 				priv->decode_fields(f, priv->multi_data,
666ee65d4b3SYunkang Tang 						    psmouse);
667ee65d4b3SYunkang Tang 
668ee65d4b3SYunkang Tang 				/*
669ee65d4b3SYunkang Tang 				 * Since Dolphin's finger number is reliable,
670ee65d4b3SYunkang Tang 				 * there is no need to compare with bmap_fn.
671ee65d4b3SYunkang Tang 				 */
67202d04254SHans de Goede 				alps_process_bitmap_dolphin(priv, f);
673ee65d4b3SYunkang Tang 			}
67401ce661fSSeth Forshee 		} else {
67501ce661fSSeth Forshee 			priv->multi_packet = 0;
67625bded7cSSeth Forshee 		}
67725bded7cSSeth Forshee 	}
67825bded7cSSeth Forshee 
67901ce661fSSeth Forshee 	/*
68001ce661fSSeth Forshee 	 * Bit 6 of byte 0 is not usually set in position packets. The only
68101ce661fSSeth Forshee 	 * times it seems to be set is in situations where the data is
68201ce661fSSeth Forshee 	 * suspect anyway, e.g. a palm resting flat on the touchpad. Given
68301ce661fSSeth Forshee 	 * this combined with the fact that this bit is useful for filtering
68401ce661fSSeth Forshee 	 * out misidentified bitmap packets, we reject anything with this
68501ce661fSSeth Forshee 	 * bit set.
68601ce661fSSeth Forshee 	 */
68702d04254SHans de Goede 	if (f->is_mp)
68801ce661fSSeth Forshee 		return;
68901ce661fSSeth Forshee 
69002d04254SHans de Goede 	if (!priv->multi_packet && f->first_mp) {
69125bded7cSSeth Forshee 		priv->multi_packet = 1;
69201ce661fSSeth Forshee 		memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
69301ce661fSSeth Forshee 		return;
69401ce661fSSeth Forshee 	}
69501ce661fSSeth Forshee 
69625bded7cSSeth Forshee 	priv->multi_packet = 0;
69725bded7cSSeth Forshee 
69825bded7cSSeth Forshee 	/*
69925bded7cSSeth Forshee 	 * Sometimes the hardware sends a single packet with z = 0
70025bded7cSSeth Forshee 	 * in the middle of a stream. Real releases generate packets
70125bded7cSSeth Forshee 	 * with x, y, and z all zero, so these seem to be flukes.
70225bded7cSSeth Forshee 	 * Ignore them.
70325bded7cSSeth Forshee 	 */
70402d04254SHans de Goede 	if (f->st.x && f->st.y && !f->pressure)
70525bded7cSSeth Forshee 		return;
70625bded7cSSeth Forshee 
70768c21870SHans de Goede 	alps_report_semi_mt_data(psmouse, fingers);
70825bded7cSSeth Forshee 
70934412ba2SPali Rohár 	if ((priv->flags & ALPS_DUALPOINT) &&
71034412ba2SPali Rohár 	    !(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS)) {
71102d04254SHans de Goede 		input_report_key(dev2, BTN_LEFT, f->ts_left);
71202d04254SHans de Goede 		input_report_key(dev2, BTN_RIGHT, f->ts_right);
71302d04254SHans de Goede 		input_report_key(dev2, BTN_MIDDLE, f->ts_middle);
71425bded7cSSeth Forshee 		input_sync(dev2);
71525bded7cSSeth Forshee 	}
71625bded7cSSeth Forshee }
71725bded7cSSeth Forshee 
71825bded7cSSeth Forshee static void alps_process_packet_v3(struct psmouse *psmouse)
71925bded7cSSeth Forshee {
72025bded7cSSeth Forshee 	unsigned char *packet = psmouse->packet;
72125bded7cSSeth Forshee 
72225bded7cSSeth Forshee 	/*
72325bded7cSSeth Forshee 	 * v3 protocol packets come in three types, two representing
72425bded7cSSeth Forshee 	 * touchpad data and one representing trackstick data.
72525bded7cSSeth Forshee 	 * Trackstick packets seem to be distinguished by always
72625bded7cSSeth Forshee 	 * having 0x3f in the last byte. This value has never been
72725bded7cSSeth Forshee 	 * observed in the last byte of either of the other types
72825bded7cSSeth Forshee 	 * of packets.
72925bded7cSSeth Forshee 	 */
73025bded7cSSeth Forshee 	if (packet[5] == 0x3f) {
73125bded7cSSeth Forshee 		alps_process_trackstick_packet_v3(psmouse);
73225bded7cSSeth Forshee 		return;
73325bded7cSSeth Forshee 	}
73425bded7cSSeth Forshee 
735ee65d4b3SYunkang Tang 	alps_process_touchpad_packet_v3_v5(psmouse);
73625bded7cSSeth Forshee }
73725bded7cSSeth Forshee 
73895f75e91SYunkang Tang static void alps_process_packet_v6(struct psmouse *psmouse)
73995f75e91SYunkang Tang {
74095f75e91SYunkang Tang 	struct alps_data *priv = psmouse->private;
74195f75e91SYunkang Tang 	unsigned char *packet = psmouse->packet;
74295f75e91SYunkang Tang 	struct input_dev *dev = psmouse->dev;
74395f75e91SYunkang Tang 	struct input_dev *dev2 = priv->dev2;
74495f75e91SYunkang Tang 	int x, y, z, left, right, middle;
74595f75e91SYunkang Tang 
74695f75e91SYunkang Tang 	/*
74795f75e91SYunkang Tang 	 * We can use Byte5 to distinguish if the packet is from Touchpad
74895f75e91SYunkang Tang 	 * or Trackpoint.
74995f75e91SYunkang Tang 	 * Touchpad:	0 - 0x7E
75095f75e91SYunkang Tang 	 * Trackpoint:	0x7F
75195f75e91SYunkang Tang 	 */
75295f75e91SYunkang Tang 	if (packet[5] == 0x7F) {
75395f75e91SYunkang Tang 		/* It should be a DualPoint when received Trackpoint packet */
75434412ba2SPali Rohár 		if (!(priv->flags & ALPS_DUALPOINT)) {
75534412ba2SPali Rohár 			psmouse_warn(psmouse,
75634412ba2SPali Rohár 				     "Rejected trackstick packet from non DualPoint device");
75795f75e91SYunkang Tang 			return;
75834412ba2SPali Rohár 		}
75995f75e91SYunkang Tang 
76095f75e91SYunkang Tang 		/* Trackpoint packet */
76195f75e91SYunkang Tang 		x = packet[1] | ((packet[3] & 0x20) << 2);
76295f75e91SYunkang Tang 		y = packet[2] | ((packet[3] & 0x40) << 1);
76395f75e91SYunkang Tang 		z = packet[4];
76495f75e91SYunkang Tang 		left = packet[3] & 0x01;
76595f75e91SYunkang Tang 		right = packet[3] & 0x02;
76695f75e91SYunkang Tang 		middle = packet[3] & 0x04;
76795f75e91SYunkang Tang 
76895f75e91SYunkang Tang 		/* To prevent the cursor jump when finger lifted */
76995f75e91SYunkang Tang 		if (x == 0x7F && y == 0x7F && z == 0x7F)
77095f75e91SYunkang Tang 			x = y = z = 0;
77195f75e91SYunkang Tang 
77295f75e91SYunkang Tang 		/* Divide 4 since trackpoint's speed is too fast */
77395f75e91SYunkang Tang 		input_report_rel(dev2, REL_X, (char)x / 4);
77495f75e91SYunkang Tang 		input_report_rel(dev2, REL_Y, -((char)y / 4));
77595f75e91SYunkang Tang 
77695f75e91SYunkang Tang 		input_report_key(dev2, BTN_LEFT, left);
77795f75e91SYunkang Tang 		input_report_key(dev2, BTN_RIGHT, right);
77895f75e91SYunkang Tang 		input_report_key(dev2, BTN_MIDDLE, middle);
77995f75e91SYunkang Tang 
78095f75e91SYunkang Tang 		input_sync(dev2);
78195f75e91SYunkang Tang 		return;
78295f75e91SYunkang Tang 	}
78395f75e91SYunkang Tang 
78495f75e91SYunkang Tang 	/* Touchpad packet */
78595f75e91SYunkang Tang 	x = packet[1] | ((packet[3] & 0x78) << 4);
78695f75e91SYunkang Tang 	y = packet[2] | ((packet[4] & 0x78) << 4);
78795f75e91SYunkang Tang 	z = packet[5];
78895f75e91SYunkang Tang 	left = packet[3] & 0x01;
78995f75e91SYunkang Tang 	right = packet[3] & 0x02;
79095f75e91SYunkang Tang 
79195f75e91SYunkang Tang 	if (z > 30)
79295f75e91SYunkang Tang 		input_report_key(dev, BTN_TOUCH, 1);
79395f75e91SYunkang Tang 	if (z < 25)
79495f75e91SYunkang Tang 		input_report_key(dev, BTN_TOUCH, 0);
79595f75e91SYunkang Tang 
79695f75e91SYunkang Tang 	if (z > 0) {
79795f75e91SYunkang Tang 		input_report_abs(dev, ABS_X, x);
79895f75e91SYunkang Tang 		input_report_abs(dev, ABS_Y, y);
79995f75e91SYunkang Tang 	}
80095f75e91SYunkang Tang 
80195f75e91SYunkang Tang 	input_report_abs(dev, ABS_PRESSURE, z);
80295f75e91SYunkang Tang 	input_report_key(dev, BTN_TOOL_FINGER, z > 0);
80395f75e91SYunkang Tang 
80495f75e91SYunkang Tang 	/* v6 touchpad does not have middle button */
80595f75e91SYunkang Tang 	input_report_key(dev, BTN_LEFT, left);
80695f75e91SYunkang Tang 	input_report_key(dev, BTN_RIGHT, right);
80795f75e91SYunkang Tang 
80895f75e91SYunkang Tang 	input_sync(dev);
80995f75e91SYunkang Tang }
81095f75e91SYunkang Tang 
81125bded7cSSeth Forshee static void alps_process_packet_v4(struct psmouse *psmouse)
81225bded7cSSeth Forshee {
8133b7e09faSGeorge Pantalos 	struct alps_data *priv = psmouse->private;
81425bded7cSSeth Forshee 	unsigned char *packet = psmouse->packet;
81502d04254SHans de Goede 	struct alps_fields *f = &priv->f;
81668c21870SHans de Goede 	int offset;
8173b7e09faSGeorge Pantalos 
8183b7e09faSGeorge Pantalos 	/*
8193b7e09faSGeorge Pantalos 	 * v4 has a 6-byte encoding for bitmap data, but this data is
8203b7e09faSGeorge Pantalos 	 * broken up between 3 normal packets. Use priv->multi_packet to
8213b7e09faSGeorge Pantalos 	 * track our position in the bitmap packet.
8223b7e09faSGeorge Pantalos 	 */
8233b7e09faSGeorge Pantalos 	if (packet[6] & 0x40) {
8243b7e09faSGeorge Pantalos 		/* sync, reset position */
8253b7e09faSGeorge Pantalos 		priv->multi_packet = 0;
8263b7e09faSGeorge Pantalos 	}
8273b7e09faSGeorge Pantalos 
8283b7e09faSGeorge Pantalos 	if (WARN_ON_ONCE(priv->multi_packet > 2))
8293b7e09faSGeorge Pantalos 		return;
8303b7e09faSGeorge Pantalos 
8313b7e09faSGeorge Pantalos 	offset = 2 * priv->multi_packet;
8323b7e09faSGeorge Pantalos 	priv->multi_data[offset] = packet[6];
8333b7e09faSGeorge Pantalos 	priv->multi_data[offset + 1] = packet[7];
8343b7e09faSGeorge Pantalos 
8353b7e09faSGeorge Pantalos 	if (++priv->multi_packet > 2) {
8363b7e09faSGeorge Pantalos 		priv->multi_packet = 0;
8373b7e09faSGeorge Pantalos 
83802d04254SHans de Goede 		f->x_map = ((priv->multi_data[2] & 0x1f) << 10) |
8393b7e09faSGeorge Pantalos 			   ((priv->multi_data[3] & 0x60) << 3) |
8403b7e09faSGeorge Pantalos 			   ((priv->multi_data[0] & 0x3f) << 2) |
8413b7e09faSGeorge Pantalos 			   ((priv->multi_data[1] & 0x60) >> 5);
84202d04254SHans de Goede 		f->y_map = ((priv->multi_data[5] & 0x01) << 10) |
8433b7e09faSGeorge Pantalos 			   ((priv->multi_data[3] & 0x1f) << 5) |
8443b7e09faSGeorge Pantalos 			    (priv->multi_data[1] & 0x1f);
8453b7e09faSGeorge Pantalos 
84602d04254SHans de Goede 		f->fingers = alps_process_bitmap(priv, f);
8473b7e09faSGeorge Pantalos 	}
84825bded7cSSeth Forshee 
84902d04254SHans de Goede 	f->left = packet[4] & 0x01;
85002d04254SHans de Goede 	f->right = packet[4] & 0x02;
85125bded7cSSeth Forshee 
85202d04254SHans de Goede 	f->st.x = ((packet[1] & 0x7f) << 4) | ((packet[3] & 0x30) >> 2) |
85325bded7cSSeth Forshee 		  ((packet[0] & 0x30) >> 4);
85402d04254SHans de Goede 	f->st.y = ((packet[2] & 0x7f) << 4) | (packet[3] & 0x0f);
85502d04254SHans de Goede 	f->pressure = packet[5] & 0x7f;
85625bded7cSSeth Forshee 
85768c21870SHans de Goede 	alps_report_semi_mt_data(psmouse, f->fingers);
85825bded7cSSeth Forshee }
85925bded7cSSeth Forshee 
8603808843cSYunkang Tang static bool alps_is_valid_package_v7(struct psmouse *psmouse)
8613808843cSYunkang Tang {
8623808843cSYunkang Tang 	switch (psmouse->pktcnt) {
8633808843cSYunkang Tang 	case 3:
8643808843cSYunkang Tang 		return (psmouse->packet[2] & 0x40) == 0x40;
8653808843cSYunkang Tang 	case 4:
8663808843cSYunkang Tang 		return (psmouse->packet[3] & 0x48) == 0x48;
8673808843cSYunkang Tang 	case 6:
8683808843cSYunkang Tang 		return (psmouse->packet[5] & 0x40) == 0x00;
8693808843cSYunkang Tang 	}
8703808843cSYunkang Tang 	return true;
8713808843cSYunkang Tang }
8723808843cSYunkang Tang 
8733808843cSYunkang Tang static unsigned char alps_get_packet_id_v7(char *byte)
8743808843cSYunkang Tang {
8753808843cSYunkang Tang 	unsigned char packet_id;
8763808843cSYunkang Tang 
8773808843cSYunkang Tang 	if (byte[4] & 0x40)
8783808843cSYunkang Tang 		packet_id = V7_PACKET_ID_TWO;
8793808843cSYunkang Tang 	else if (byte[4] & 0x01)
8803808843cSYunkang Tang 		packet_id = V7_PACKET_ID_MULTI;
8813808843cSYunkang Tang 	else if ((byte[0] & 0x10) && !(byte[4] & 0x43))
8823808843cSYunkang Tang 		packet_id = V7_PACKET_ID_NEW;
8833808843cSYunkang Tang 	else if (byte[1] == 0x00 && byte[4] == 0x00)
8843808843cSYunkang Tang 		packet_id = V7_PACKET_ID_IDLE;
8853808843cSYunkang Tang 	else
8863808843cSYunkang Tang 		packet_id = V7_PACKET_ID_UNKNOWN;
8873808843cSYunkang Tang 
8883808843cSYunkang Tang 	return packet_id;
8893808843cSYunkang Tang }
8903808843cSYunkang Tang 
8913808843cSYunkang Tang static void alps_get_finger_coordinate_v7(struct input_mt_pos *mt,
8923808843cSYunkang Tang 					  unsigned char *pkt,
8933808843cSYunkang Tang 					  unsigned char pkt_id)
8943808843cSYunkang Tang {
89527a560baSHans de Goede 	/*
89627a560baSHans de Goede 	 *       packet-fmt    b7   b6    b5   b4   b3   b2   b1   b0
89727a560baSHans de Goede 	 * Byte0 TWO & MULTI    L    1     R    M    1 Y0-2 Y0-1 Y0-0
89827a560baSHans de Goede 	 * Byte0 NEW            L    1  X1-5    1    1 Y0-2 Y0-1 Y0-0
89927a560baSHans de Goede 	 * Byte1            Y0-10 Y0-9  Y0-8 Y0-7 Y0-6 Y0-5 Y0-4 Y0-3
90027a560baSHans de Goede 	 * Byte2            X0-11    1 X0-10 X0-9 X0-8 X0-7 X0-6 X0-5
90127a560baSHans de Goede 	 * Byte3            X1-11    1  X0-4 X0-3    1 X0-2 X0-1 X0-0
90227a560baSHans de Goede 	 * Byte4 TWO        X1-10  TWO  X1-9 X1-8 X1-7 X1-6 X1-5 X1-4
90327a560baSHans de Goede 	 * Byte4 MULTI      X1-10  TWO  X1-9 X1-8 X1-7 X1-6 Y1-5    1
90427a560baSHans de Goede 	 * Byte4 NEW        X1-10  TWO  X1-9 X1-8 X1-7 X1-6    0    0
90527a560baSHans de Goede 	 * Byte5 TWO & NEW  Y1-10    0  Y1-9 Y1-8 Y1-7 Y1-6 Y1-5 Y1-4
90627a560baSHans de Goede 	 * Byte5 MULTI      Y1-10    0  Y1-9 Y1-8 Y1-7 Y1-6  F-1  F-0
90727a560baSHans de Goede 	 * L:         Left button
90827a560baSHans de Goede 	 * R / M:     Non-clickpads: Right / Middle button
90927a560baSHans de Goede 	 *            Clickpads: When > 2 fingers are down, and some fingers
91027a560baSHans de Goede 	 *            are in the button area, then the 2 coordinates reported
91127a560baSHans de Goede 	 *            are for fingers outside the button area and these report
91227a560baSHans de Goede 	 *            extra fingers being present in the right / left button
91327a560baSHans de Goede 	 *            area. Note these fingers are not added to the F field!
91427a560baSHans de Goede 	 *            so if a TWO packet is received and R = 1 then there are
91527a560baSHans de Goede 	 *            3 fingers down, etc.
91627a560baSHans de Goede 	 * TWO:       1: Two touches present, byte 0/4/5 are in TWO fmt
91727a560baSHans de Goede 	 *            0: If byte 4 bit 0 is 1, then byte 0/4/5 are in MULTI fmt
91827a560baSHans de Goede 	 *               otherwise byte 0 bit 4 must be set and byte 0/4/5 are
91927a560baSHans de Goede 	 *               in NEW fmt
92027a560baSHans de Goede 	 * F:         Number of fingers - 3, 0 means 3 fingers, 1 means 4 ...
92127a560baSHans de Goede 	 */
92227a560baSHans de Goede 
9233808843cSYunkang Tang 	mt[0].x = ((pkt[2] & 0x80) << 4);
9243808843cSYunkang Tang 	mt[0].x |= ((pkt[2] & 0x3F) << 5);
9253808843cSYunkang Tang 	mt[0].x |= ((pkt[3] & 0x30) >> 1);
9263808843cSYunkang Tang 	mt[0].x |= (pkt[3] & 0x07);
9273808843cSYunkang Tang 	mt[0].y = (pkt[1] << 3) | (pkt[0] & 0x07);
9283808843cSYunkang Tang 
9293808843cSYunkang Tang 	mt[1].x = ((pkt[3] & 0x80) << 4);
9303808843cSYunkang Tang 	mt[1].x |= ((pkt[4] & 0x80) << 3);
9313808843cSYunkang Tang 	mt[1].x |= ((pkt[4] & 0x3F) << 4);
9323808843cSYunkang Tang 	mt[1].y = ((pkt[5] & 0x80) << 3);
9333808843cSYunkang Tang 	mt[1].y |= ((pkt[5] & 0x3F) << 4);
9343808843cSYunkang Tang 
9353808843cSYunkang Tang 	switch (pkt_id) {
9363808843cSYunkang Tang 	case V7_PACKET_ID_TWO:
9373808843cSYunkang Tang 		mt[1].x &= ~0x000F;
9383808843cSYunkang Tang 		mt[1].y |= 0x000F;
9393808843cSYunkang Tang 		break;
9403808843cSYunkang Tang 
9413808843cSYunkang Tang 	case V7_PACKET_ID_MULTI:
9423808843cSYunkang Tang 		mt[1].x &= ~0x003F;
9433808843cSYunkang Tang 		mt[1].y &= ~0x0020;
9443808843cSYunkang Tang 		mt[1].y |= ((pkt[4] & 0x02) << 4);
9453808843cSYunkang Tang 		mt[1].y |= 0x001F;
9463808843cSYunkang Tang 		break;
9473808843cSYunkang Tang 
9483808843cSYunkang Tang 	case V7_PACKET_ID_NEW:
9493808843cSYunkang Tang 		mt[1].x &= ~0x003F;
9503808843cSYunkang Tang 		mt[1].x |= (pkt[0] & 0x20);
9513808843cSYunkang Tang 		mt[1].y |= 0x000F;
9523808843cSYunkang Tang 		break;
9533808843cSYunkang Tang 	}
9543808843cSYunkang Tang 
9553808843cSYunkang Tang 	mt[0].y = 0x7FF - mt[0].y;
9563808843cSYunkang Tang 	mt[1].y = 0x7FF - mt[1].y;
9573808843cSYunkang Tang }
9583808843cSYunkang Tang 
9593808843cSYunkang Tang static int alps_get_mt_count(struct input_mt_pos *mt)
9603808843cSYunkang Tang {
9617091c443SHans de Goede 	int i, fingers = 0;
9623808843cSYunkang Tang 
9637091c443SHans de Goede 	for (i = 0; i < MAX_TOUCHES; i++) {
9647091c443SHans de Goede 		if (mt[i].x != 0 || mt[i].y != 0)
9657091c443SHans de Goede 			fingers++;
9667091c443SHans de Goede 	}
9673808843cSYunkang Tang 
9687091c443SHans de Goede 	return fingers;
9693808843cSYunkang Tang }
9703808843cSYunkang Tang 
9713808843cSYunkang Tang static int alps_decode_packet_v7(struct alps_fields *f,
9723808843cSYunkang Tang 				  unsigned char *p,
9733808843cSYunkang Tang 				  struct psmouse *psmouse)
9743808843cSYunkang Tang {
975d27eb793SHans de Goede 	struct alps_data *priv = psmouse->private;
9763808843cSYunkang Tang 	unsigned char pkt_id;
9773808843cSYunkang Tang 
9783808843cSYunkang Tang 	pkt_id = alps_get_packet_id_v7(p);
9793808843cSYunkang Tang 	if (pkt_id == V7_PACKET_ID_IDLE)
9803808843cSYunkang Tang 		return 0;
9813808843cSYunkang Tang 	if (pkt_id == V7_PACKET_ID_UNKNOWN)
9823808843cSYunkang Tang 		return -1;
9838b238115SHans de Goede 	/*
9848b238115SHans de Goede 	 * NEW packets are send to indicate a discontinuity in the finger
9858b238115SHans de Goede 	 * coordinate reporting. Specifically a finger may have moved from
9868b238115SHans de Goede 	 * slot 0 to 1 or vice versa. INPUT_MT_TRACK takes care of this for
9878b238115SHans de Goede 	 * us.
9888b238115SHans de Goede 	 *
9898b238115SHans de Goede 	 * NEW packets have 3 problems:
9908b238115SHans de Goede 	 * 1) They do not contain middle / right button info (on non clickpads)
9918b238115SHans de Goede 	 *    this can be worked around by preserving the old button state
9928b238115SHans de Goede 	 * 2) They do not contain an accurate fingercount, and they are
9938b238115SHans de Goede 	 *    typically send when the number of fingers changes. We cannot use
9948b238115SHans de Goede 	 *    the old finger count as that may mismatch with the amount of
9958b238115SHans de Goede 	 *    touch coordinates we've available in the NEW packet
9968b238115SHans de Goede 	 * 3) Their x data for the second touch is inaccurate leading to
9978b238115SHans de Goede 	 *    a possible jump of the x coordinate by 16 units when the first
9988b238115SHans de Goede 	 *    non NEW packet comes in
9998b238115SHans de Goede 	 * Since problems 2 & 3 cannot be worked around, just ignore them.
10008b238115SHans de Goede 	 */
10018b238115SHans de Goede 	if (pkt_id == V7_PACKET_ID_NEW)
10028b238115SHans de Goede 		return 1;
10033808843cSYunkang Tang 
10043808843cSYunkang Tang 	alps_get_finger_coordinate_v7(f->mt, p, pkt_id);
10053808843cSYunkang Tang 
10063808843cSYunkang Tang 	if (pkt_id == V7_PACKET_ID_TWO)
10073808843cSYunkang Tang 		f->fingers = alps_get_mt_count(f->mt);
10088b238115SHans de Goede 	else /* pkt_id == V7_PACKET_ID_MULTI */
10093808843cSYunkang Tang 		f->fingers = 3 + (p[5] & 0x03);
10103808843cSYunkang Tang 
1011d27eb793SHans de Goede 	f->left = (p[0] & 0x80) >> 7;
1012d27eb793SHans de Goede 	if (priv->flags & ALPS_BUTTONPAD) {
1013d27eb793SHans de Goede 		if (p[0] & 0x20)
1014d27eb793SHans de Goede 			f->fingers++;
1015d27eb793SHans de Goede 		if (p[0] & 0x10)
1016d27eb793SHans de Goede 			f->fingers++;
1017d27eb793SHans de Goede 	} else {
1018d27eb793SHans de Goede 		f->right = (p[0] & 0x20) >> 5;
1019d27eb793SHans de Goede 		f->middle = (p[0] & 0x10) >> 4;
1020d27eb793SHans de Goede 	}
1021d27eb793SHans de Goede 
10227091c443SHans de Goede 	/* Sometimes a single touch is reported in mt[1] rather then mt[0] */
10237091c443SHans de Goede 	if (f->fingers == 1 && f->mt[0].x == 0 && f->mt[0].y == 0) {
10247091c443SHans de Goede 		f->mt[0].x = f->mt[1].x;
10257091c443SHans de Goede 		f->mt[0].y = f->mt[1].y;
10267091c443SHans de Goede 		f->mt[1].x = 0;
10277091c443SHans de Goede 		f->mt[1].y = 0;
10287091c443SHans de Goede 	}
10297091c443SHans de Goede 
10303808843cSYunkang Tang 	return 0;
10313808843cSYunkang Tang }
10323808843cSYunkang Tang 
10333808843cSYunkang Tang static void alps_process_trackstick_packet_v7(struct psmouse *psmouse)
10343808843cSYunkang Tang {
10353808843cSYunkang Tang 	struct alps_data *priv = psmouse->private;
10363808843cSYunkang Tang 	unsigned char *packet = psmouse->packet;
10373808843cSYunkang Tang 	struct input_dev *dev2 = priv->dev2;
10383808843cSYunkang Tang 	int x, y, z, left, right, middle;
10393808843cSYunkang Tang 
104034412ba2SPali Rohár 	/* It should be a DualPoint when received trackstick packet */
104134412ba2SPali Rohár 	if (!(priv->flags & ALPS_DUALPOINT)) {
104234412ba2SPali Rohár 		psmouse_warn(psmouse,
104334412ba2SPali Rohár 			     "Rejected trackstick packet from non DualPoint device");
104434412ba2SPali Rohár 		return;
104534412ba2SPali Rohár 	}
104634412ba2SPali Rohár 
10473808843cSYunkang Tang 	/*
10483808843cSYunkang Tang 	 *        b7 b6 b5 b4 b3 b2 b1 b0
10493808843cSYunkang Tang 	 * Byte0   0  1  0  0  1  0  0  0
10503808843cSYunkang Tang 	 * Byte1   1  1  *  *  1  M  R  L
10513808843cSYunkang Tang 	 * Byte2  X7  1 X5 X4 X3 X2 X1 X0
10523808843cSYunkang Tang 	 * Byte3  Z6  1 Y6 X6  1 Y2 Y1 Y0
10533808843cSYunkang Tang 	 * Byte4  Y7  0 Y5 Y4 Y3  1  1  0
10543808843cSYunkang Tang 	 * Byte5 T&P  0 Z5 Z4 Z3 Z2 Z1 Z0
10553808843cSYunkang Tang 	 * M / R / L: Middle / Right / Left button
10563808843cSYunkang Tang 	 */
10573808843cSYunkang Tang 
10583808843cSYunkang Tang 	x = ((packet[2] & 0xbf)) | ((packet[3] & 0x10) << 2);
10593808843cSYunkang Tang 	y = (packet[3] & 0x07) | (packet[4] & 0xb8) |
10603808843cSYunkang Tang 	    ((packet[3] & 0x20) << 1);
10613808843cSYunkang Tang 	z = (packet[5] & 0x3f) | ((packet[3] & 0x80) >> 1);
10623808843cSYunkang Tang 
10633808843cSYunkang Tang 	left = (packet[1] & 0x01);
10643808843cSYunkang Tang 	right = (packet[1] & 0x02) >> 1;
10653808843cSYunkang Tang 	middle = (packet[1] & 0x04) >> 2;
10663808843cSYunkang Tang 
10673808843cSYunkang Tang 	/* Divide 2 since trackpoint's speed is too fast */
10683808843cSYunkang Tang 	input_report_rel(dev2, REL_X, (char)x / 2);
10693808843cSYunkang Tang 	input_report_rel(dev2, REL_Y, -((char)y / 2));
10703808843cSYunkang Tang 
10713808843cSYunkang Tang 	input_report_key(dev2, BTN_LEFT, left);
10723808843cSYunkang Tang 	input_report_key(dev2, BTN_RIGHT, right);
10733808843cSYunkang Tang 	input_report_key(dev2, BTN_MIDDLE, middle);
10743808843cSYunkang Tang 
10753808843cSYunkang Tang 	input_sync(dev2);
10763808843cSYunkang Tang }
10773808843cSYunkang Tang 
10783808843cSYunkang Tang static void alps_process_touchpad_packet_v7(struct psmouse *psmouse)
10793808843cSYunkang Tang {
10803808843cSYunkang Tang 	struct alps_data *priv = psmouse->private;
10813808843cSYunkang Tang 	struct input_dev *dev = psmouse->dev;
10823808843cSYunkang Tang 	struct alps_fields *f = &priv->f;
10833808843cSYunkang Tang 
10843808843cSYunkang Tang 	memset(f, 0, sizeof(*f));
10853808843cSYunkang Tang 
10863808843cSYunkang Tang 	if (priv->decode_fields(f, psmouse->packet, psmouse))
10873808843cSYunkang Tang 		return;
10883808843cSYunkang Tang 
10893808843cSYunkang Tang 	alps_report_mt_data(psmouse, alps_get_mt_count(f->mt));
10903808843cSYunkang Tang 
10913808843cSYunkang Tang 	input_mt_report_finger_count(dev, f->fingers);
10923808843cSYunkang Tang 
10933808843cSYunkang Tang 	input_report_key(dev, BTN_LEFT, f->left);
10943808843cSYunkang Tang 	input_report_key(dev, BTN_RIGHT, f->right);
10953808843cSYunkang Tang 	input_report_key(dev, BTN_MIDDLE, f->middle);
10963808843cSYunkang Tang 
10973808843cSYunkang Tang 	input_sync(dev);
10983808843cSYunkang Tang }
10993808843cSYunkang Tang 
11003808843cSYunkang Tang static void alps_process_packet_v7(struct psmouse *psmouse)
11013808843cSYunkang Tang {
11023808843cSYunkang Tang 	unsigned char *packet = psmouse->packet;
11033808843cSYunkang Tang 
11043808843cSYunkang Tang 	if (packet[0] == 0x48 && (packet[4] & 0x47) == 0x06)
11053808843cSYunkang Tang 		alps_process_trackstick_packet_v7(psmouse);
11063808843cSYunkang Tang 	else
11073808843cSYunkang Tang 		alps_process_touchpad_packet_v7(psmouse);
11083808843cSYunkang Tang }
11093808843cSYunkang Tang 
11101d9f2626SSebastian Kapfer static void alps_report_bare_ps2_packet(struct psmouse *psmouse,
11111d9f2626SSebastian Kapfer 					unsigned char packet[],
11121d9f2626SSebastian Kapfer 					bool report_buttons)
11131d9f2626SSebastian Kapfer {
11141d9f2626SSebastian Kapfer 	struct alps_data *priv = psmouse->private;
11151d9f2626SSebastian Kapfer 	struct input_dev *dev2 = priv->dev2;
11161d9f2626SSebastian Kapfer 
11171d9f2626SSebastian Kapfer 	if (report_buttons)
11181d9f2626SSebastian Kapfer 		alps_report_buttons(psmouse, dev2, psmouse->dev,
11191d9f2626SSebastian Kapfer 				packet[0] & 1, packet[0] & 2, packet[0] & 4);
11201d9f2626SSebastian Kapfer 
11211d9f2626SSebastian Kapfer 	input_report_rel(dev2, REL_X,
11221d9f2626SSebastian Kapfer 		packet[1] ? packet[1] - ((packet[0] << 4) & 0x100) : 0);
11231d9f2626SSebastian Kapfer 	input_report_rel(dev2, REL_Y,
11241d9f2626SSebastian Kapfer 		packet[2] ? ((packet[0] << 3) & 0x100) - packet[2] : 0);
11251d9f2626SSebastian Kapfer 
11261d9f2626SSebastian Kapfer 	input_sync(dev2);
11271d9f2626SSebastian Kapfer }
11281d9f2626SSebastian Kapfer 
11291d9f2626SSebastian Kapfer static psmouse_ret_t alps_handle_interleaved_ps2(struct psmouse *psmouse)
11301da177e4SLinus Torvalds {
11311da177e4SLinus Torvalds 	struct alps_data *priv = psmouse->private;
11321da177e4SLinus Torvalds 
11331d9f2626SSebastian Kapfer 	if (psmouse->pktcnt < 6)
11341d9f2626SSebastian Kapfer 		return PSMOUSE_GOOD_DATA;
11351d9f2626SSebastian Kapfer 
11361d9f2626SSebastian Kapfer 	if (psmouse->pktcnt == 6) {
11371d9f2626SSebastian Kapfer 		/*
11381d9f2626SSebastian Kapfer 		 * Start a timer to flush the packet if it ends up last
11391d9f2626SSebastian Kapfer 		 * 6-byte packet in the stream. Timer needs to fire
11401d9f2626SSebastian Kapfer 		 * psmouse core times out itself. 20 ms should be enough
11411d9f2626SSebastian Kapfer 		 * to decide if we are getting more data or not.
11421d9f2626SSebastian Kapfer 		 */
11431d9f2626SSebastian Kapfer 		mod_timer(&priv->timer, jiffies + msecs_to_jiffies(20));
11441d9f2626SSebastian Kapfer 		return PSMOUSE_GOOD_DATA;
11451d9f2626SSebastian Kapfer 	}
11461d9f2626SSebastian Kapfer 
11471d9f2626SSebastian Kapfer 	del_timer(&priv->timer);
11481d9f2626SSebastian Kapfer 
11491d9f2626SSebastian Kapfer 	if (psmouse->packet[6] & 0x80) {
11501d9f2626SSebastian Kapfer 
11511d9f2626SSebastian Kapfer 		/*
11521d9f2626SSebastian Kapfer 		 * Highest bit is set - that means we either had
11531d9f2626SSebastian Kapfer 		 * complete ALPS packet and this is start of the
11541d9f2626SSebastian Kapfer 		 * next packet or we got garbage.
11551d9f2626SSebastian Kapfer 		 */
11561d9f2626SSebastian Kapfer 
11571d9f2626SSebastian Kapfer 		if (((psmouse->packet[3] |
11581d9f2626SSebastian Kapfer 		      psmouse->packet[4] |
11591d9f2626SSebastian Kapfer 		      psmouse->packet[5]) & 0x80) ||
116099df65e7SKevin Cernekee 		    (!alps_is_valid_first_byte(priv, psmouse->packet[6]))) {
1161b5d21704SDmitry Torokhov 			psmouse_dbg(psmouse,
11623b112923SAndy Shevchenko 				    "refusing packet %4ph (suspected interleaved ps/2)\n",
11633b112923SAndy Shevchenko 				    psmouse->packet + 3);
11641d9f2626SSebastian Kapfer 			return PSMOUSE_BAD_DATA;
11651d9f2626SSebastian Kapfer 		}
11661d9f2626SSebastian Kapfer 
116724af5cb9SKevin Cernekee 		priv->process_packet(psmouse);
11681d9f2626SSebastian Kapfer 
11691d9f2626SSebastian Kapfer 		/* Continue with the next packet */
11701d9f2626SSebastian Kapfer 		psmouse->packet[0] = psmouse->packet[6];
11711d9f2626SSebastian Kapfer 		psmouse->pktcnt = 1;
11721d9f2626SSebastian Kapfer 
11731d9f2626SSebastian Kapfer 	} else {
11741d9f2626SSebastian Kapfer 
11751d9f2626SSebastian Kapfer 		/*
11761d9f2626SSebastian Kapfer 		 * High bit is 0 - that means that we indeed got a PS/2
11771d9f2626SSebastian Kapfer 		 * packet in the middle of ALPS packet.
11781d9f2626SSebastian Kapfer 		 *
11791d9f2626SSebastian Kapfer 		 * There is also possibility that we got 6-byte ALPS
11801d9f2626SSebastian Kapfer 		 * packet followed  by 3-byte packet from trackpoint. We
11811d9f2626SSebastian Kapfer 		 * can not distinguish between these 2 scenarios but
1182b5d21704SDmitry Torokhov 		 * because the latter is unlikely to happen in course of
11831d9f2626SSebastian Kapfer 		 * normal operation (user would need to press all
11841d9f2626SSebastian Kapfer 		 * buttons on the pad and start moving trackpoint
11851d9f2626SSebastian Kapfer 		 * without touching the pad surface) we assume former.
11861d9f2626SSebastian Kapfer 		 * Even if we are wrong the wost thing that would happen
11871d9f2626SSebastian Kapfer 		 * the cursor would jump but we should not get protocol
1188b5d21704SDmitry Torokhov 		 * de-synchronization.
11891d9f2626SSebastian Kapfer 		 */
11901d9f2626SSebastian Kapfer 
11911d9f2626SSebastian Kapfer 		alps_report_bare_ps2_packet(psmouse, &psmouse->packet[3],
11921d9f2626SSebastian Kapfer 					    false);
11931d9f2626SSebastian Kapfer 
11941d9f2626SSebastian Kapfer 		/*
11951d9f2626SSebastian Kapfer 		 * Continue with the standard ALPS protocol handling,
11961d9f2626SSebastian Kapfer 		 * but make sure we won't process it as an interleaved
11971d9f2626SSebastian Kapfer 		 * packet again, which may happen if all buttons are
11981d9f2626SSebastian Kapfer 		 * pressed. To avoid this let's reset the 4th bit which
11991d9f2626SSebastian Kapfer 		 * is normally 1.
12001d9f2626SSebastian Kapfer 		 */
12011d9f2626SSebastian Kapfer 		psmouse->packet[3] = psmouse->packet[6] & 0xf7;
12021d9f2626SSebastian Kapfer 		psmouse->pktcnt = 4;
12031d9f2626SSebastian Kapfer 	}
12041d9f2626SSebastian Kapfer 
12051d9f2626SSebastian Kapfer 	return PSMOUSE_GOOD_DATA;
12061d9f2626SSebastian Kapfer }
12071d9f2626SSebastian Kapfer 
12081d9f2626SSebastian Kapfer static void alps_flush_packet(unsigned long data)
12091d9f2626SSebastian Kapfer {
12101d9f2626SSebastian Kapfer 	struct psmouse *psmouse = (struct psmouse *)data;
121124af5cb9SKevin Cernekee 	struct alps_data *priv = psmouse->private;
12121d9f2626SSebastian Kapfer 
12131d9f2626SSebastian Kapfer 	serio_pause_rx(psmouse->ps2dev.serio);
12141d9f2626SSebastian Kapfer 
1215b46615feSSeth Forshee 	if (psmouse->pktcnt == psmouse->pktsize) {
12161d9f2626SSebastian Kapfer 
12171d9f2626SSebastian Kapfer 		/*
12181d9f2626SSebastian Kapfer 		 * We did not any more data in reasonable amount of time.
12191d9f2626SSebastian Kapfer 		 * Validate the last 3 bytes and process as a standard
12201d9f2626SSebastian Kapfer 		 * ALPS packet.
12211d9f2626SSebastian Kapfer 		 */
12221d9f2626SSebastian Kapfer 		if ((psmouse->packet[3] |
12231d9f2626SSebastian Kapfer 		     psmouse->packet[4] |
12241d9f2626SSebastian Kapfer 		     psmouse->packet[5]) & 0x80) {
1225b5d21704SDmitry Torokhov 			psmouse_dbg(psmouse,
12263b112923SAndy Shevchenko 				    "refusing packet %3ph (suspected interleaved ps/2)\n",
12273b112923SAndy Shevchenko 				    psmouse->packet + 3);
12281d9f2626SSebastian Kapfer 		} else {
122924af5cb9SKevin Cernekee 			priv->process_packet(psmouse);
12301d9f2626SSebastian Kapfer 		}
12311d9f2626SSebastian Kapfer 		psmouse->pktcnt = 0;
12321d9f2626SSebastian Kapfer 	}
12331d9f2626SSebastian Kapfer 
12341d9f2626SSebastian Kapfer 	serio_continue_rx(psmouse->ps2dev.serio);
12351d9f2626SSebastian Kapfer }
12361d9f2626SSebastian Kapfer 
12371d9f2626SSebastian Kapfer static psmouse_ret_t alps_process_byte(struct psmouse *psmouse)
12381d9f2626SSebastian Kapfer {
12391d9f2626SSebastian Kapfer 	struct alps_data *priv = psmouse->private;
12401d9f2626SSebastian Kapfer 
12411da177e4SLinus Torvalds 	if ((psmouse->packet[0] & 0xc8) == 0x08) { /* PS/2 packet */
12421da177e4SLinus Torvalds 		if (psmouse->pktcnt == 3) {
12431d9f2626SSebastian Kapfer 			alps_report_bare_ps2_packet(psmouse, psmouse->packet,
12441d9f2626SSebastian Kapfer 						    true);
12451da177e4SLinus Torvalds 			return PSMOUSE_FULL_PACKET;
12461da177e4SLinus Torvalds 		}
12471da177e4SLinus Torvalds 		return PSMOUSE_GOOD_DATA;
12481da177e4SLinus Torvalds 	}
12491da177e4SLinus Torvalds 
12501d9f2626SSebastian Kapfer 	/* Check for PS/2 packet stuffed in the middle of ALPS packet. */
12511d9f2626SSebastian Kapfer 
125299df65e7SKevin Cernekee 	if ((priv->flags & ALPS_PS2_INTERLEAVED) &&
12531d9f2626SSebastian Kapfer 	    psmouse->pktcnt >= 4 && (psmouse->packet[3] & 0x0f) == 0x0f) {
12541d9f2626SSebastian Kapfer 		return alps_handle_interleaved_ps2(psmouse);
12551d9f2626SSebastian Kapfer 	}
12561d9f2626SSebastian Kapfer 
125799df65e7SKevin Cernekee 	if (!alps_is_valid_first_byte(priv, psmouse->packet[0])) {
1258b5d21704SDmitry Torokhov 		psmouse_dbg(psmouse,
1259b5d21704SDmitry Torokhov 			    "refusing packet[0] = %x (mask0 = %x, byte0 = %x)\n",
126099df65e7SKevin Cernekee 			    psmouse->packet[0], priv->mask0, priv->byte0);
12611da177e4SLinus Torvalds 		return PSMOUSE_BAD_DATA;
12621d9f2626SSebastian Kapfer 	}
12631da177e4SLinus Torvalds 
1264b46615feSSeth Forshee 	/* Bytes 2 - pktsize should have 0 in the highest bit */
126595f75e91SYunkang Tang 	if ((priv->proto_version < ALPS_PROTO_V5) &&
126675af9e56SDave Turvene 	    psmouse->pktcnt >= 2 && psmouse->pktcnt <= psmouse->pktsize &&
12671d9f2626SSebastian Kapfer 	    (psmouse->packet[psmouse->pktcnt - 1] & 0x80)) {
1268b5d21704SDmitry Torokhov 		psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
1269b5d21704SDmitry Torokhov 			    psmouse->pktcnt - 1,
1270b5d21704SDmitry Torokhov 			    psmouse->packet[psmouse->pktcnt - 1]);
12711da177e4SLinus Torvalds 		return PSMOUSE_BAD_DATA;
12721d9f2626SSebastian Kapfer 	}
12731da177e4SLinus Torvalds 
12743808843cSYunkang Tang 	if (priv->proto_version == ALPS_PROTO_V7 &&
12753808843cSYunkang Tang 	    !alps_is_valid_package_v7(psmouse)) {
12763808843cSYunkang Tang 		psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
12773808843cSYunkang Tang 			    psmouse->pktcnt - 1,
12783808843cSYunkang Tang 			    psmouse->packet[psmouse->pktcnt - 1]);
12793808843cSYunkang Tang 		return PSMOUSE_BAD_DATA;
12803808843cSYunkang Tang 	}
12813808843cSYunkang Tang 
1282b46615feSSeth Forshee 	if (psmouse->pktcnt == psmouse->pktsize) {
128324af5cb9SKevin Cernekee 		priv->process_packet(psmouse);
12841da177e4SLinus Torvalds 		return PSMOUSE_FULL_PACKET;
12851da177e4SLinus Torvalds 	}
12861da177e4SLinus Torvalds 
12871da177e4SLinus Torvalds 	return PSMOUSE_GOOD_DATA;
12881da177e4SLinus Torvalds }
12891da177e4SLinus Torvalds 
129025bded7cSSeth Forshee static int alps_command_mode_send_nibble(struct psmouse *psmouse, int nibble)
129125bded7cSSeth Forshee {
129225bded7cSSeth Forshee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
129325bded7cSSeth Forshee 	struct alps_data *priv = psmouse->private;
129425bded7cSSeth Forshee 	int command;
129525bded7cSSeth Forshee 	unsigned char *param;
129625bded7cSSeth Forshee 	unsigned char dummy[4];
129725bded7cSSeth Forshee 
129825bded7cSSeth Forshee 	BUG_ON(nibble > 0xf);
129925bded7cSSeth Forshee 
130025bded7cSSeth Forshee 	command = priv->nibble_commands[nibble].command;
130125bded7cSSeth Forshee 	param = (command & 0x0f00) ?
130225bded7cSSeth Forshee 		dummy : (unsigned char *)&priv->nibble_commands[nibble].data;
130325bded7cSSeth Forshee 
130425bded7cSSeth Forshee 	if (ps2_command(ps2dev, param, command))
130525bded7cSSeth Forshee 		return -1;
130625bded7cSSeth Forshee 
130725bded7cSSeth Forshee 	return 0;
130825bded7cSSeth Forshee }
130925bded7cSSeth Forshee 
131025bded7cSSeth Forshee static int alps_command_mode_set_addr(struct psmouse *psmouse, int addr)
131125bded7cSSeth Forshee {
131225bded7cSSeth Forshee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
131325bded7cSSeth Forshee 	struct alps_data *priv = psmouse->private;
131425bded7cSSeth Forshee 	int i, nibble;
131525bded7cSSeth Forshee 
131625bded7cSSeth Forshee 	if (ps2_command(ps2dev, NULL, priv->addr_command))
131725bded7cSSeth Forshee 		return -1;
131825bded7cSSeth Forshee 
131925bded7cSSeth Forshee 	for (i = 12; i >= 0; i -= 4) {
132025bded7cSSeth Forshee 		nibble = (addr >> i) & 0xf;
132125bded7cSSeth Forshee 		if (alps_command_mode_send_nibble(psmouse, nibble))
132225bded7cSSeth Forshee 			return -1;
132325bded7cSSeth Forshee 	}
132425bded7cSSeth Forshee 
132525bded7cSSeth Forshee 	return 0;
132625bded7cSSeth Forshee }
132725bded7cSSeth Forshee 
132825bded7cSSeth Forshee static int __alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
132925bded7cSSeth Forshee {
133025bded7cSSeth Forshee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
133125bded7cSSeth Forshee 	unsigned char param[4];
133225bded7cSSeth Forshee 
133325bded7cSSeth Forshee 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
133425bded7cSSeth Forshee 		return -1;
133525bded7cSSeth Forshee 
133625bded7cSSeth Forshee 	/*
133725bded7cSSeth Forshee 	 * The address being read is returned in the first two bytes
133825bded7cSSeth Forshee 	 * of the result. Check that this address matches the expected
133925bded7cSSeth Forshee 	 * address.
134025bded7cSSeth Forshee 	 */
134125bded7cSSeth Forshee 	if (addr != ((param[0] << 8) | param[1]))
134225bded7cSSeth Forshee 		return -1;
134325bded7cSSeth Forshee 
134425bded7cSSeth Forshee 	return param[2];
134525bded7cSSeth Forshee }
134625bded7cSSeth Forshee 
134725bded7cSSeth Forshee static int alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
134825bded7cSSeth Forshee {
134925bded7cSSeth Forshee 	if (alps_command_mode_set_addr(psmouse, addr))
135025bded7cSSeth Forshee 		return -1;
135125bded7cSSeth Forshee 	return __alps_command_mode_read_reg(psmouse, addr);
135225bded7cSSeth Forshee }
135325bded7cSSeth Forshee 
135425bded7cSSeth Forshee static int __alps_command_mode_write_reg(struct psmouse *psmouse, u8 value)
135525bded7cSSeth Forshee {
135625bded7cSSeth Forshee 	if (alps_command_mode_send_nibble(psmouse, (value >> 4) & 0xf))
135725bded7cSSeth Forshee 		return -1;
135825bded7cSSeth Forshee 	if (alps_command_mode_send_nibble(psmouse, value & 0xf))
135925bded7cSSeth Forshee 		return -1;
136025bded7cSSeth Forshee 	return 0;
136125bded7cSSeth Forshee }
136225bded7cSSeth Forshee 
136325bded7cSSeth Forshee static int alps_command_mode_write_reg(struct psmouse *psmouse, int addr,
136425bded7cSSeth Forshee 				       u8 value)
136525bded7cSSeth Forshee {
136625bded7cSSeth Forshee 	if (alps_command_mode_set_addr(psmouse, addr))
136725bded7cSSeth Forshee 		return -1;
136825bded7cSSeth Forshee 	return __alps_command_mode_write_reg(psmouse, value);
136925bded7cSSeth Forshee }
137025bded7cSSeth Forshee 
137124ba9707SKevin Cernekee static int alps_rpt_cmd(struct psmouse *psmouse, int init_command,
137224ba9707SKevin Cernekee 			int repeated_command, unsigned char *param)
137324ba9707SKevin Cernekee {
137424ba9707SKevin Cernekee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
137524ba9707SKevin Cernekee 
137624ba9707SKevin Cernekee 	param[0] = 0;
137724ba9707SKevin Cernekee 	if (init_command && ps2_command(ps2dev, param, init_command))
137824ba9707SKevin Cernekee 		return -EIO;
137924ba9707SKevin Cernekee 
138024ba9707SKevin Cernekee 	if (ps2_command(ps2dev,  NULL, repeated_command) ||
138124ba9707SKevin Cernekee 	    ps2_command(ps2dev,  NULL, repeated_command) ||
138224ba9707SKevin Cernekee 	    ps2_command(ps2dev,  NULL, repeated_command))
138324ba9707SKevin Cernekee 		return -EIO;
138424ba9707SKevin Cernekee 
138524ba9707SKevin Cernekee 	param[0] = param[1] = param[2] = 0xff;
138624ba9707SKevin Cernekee 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
138724ba9707SKevin Cernekee 		return -EIO;
138824ba9707SKevin Cernekee 
138939fbe585SDmitry Torokhov 	psmouse_dbg(psmouse, "%2.2X report: %3ph\n",
139039fbe585SDmitry Torokhov 		    repeated_command, param);
139124ba9707SKevin Cernekee 	return 0;
139224ba9707SKevin Cernekee }
139324ba9707SKevin Cernekee 
13943808843cSYunkang Tang static bool alps_check_valid_firmware_id(unsigned char id[])
13953808843cSYunkang Tang {
13963808843cSYunkang Tang 	if (id[0] == 0x73)
13973808843cSYunkang Tang 		return true;
13983808843cSYunkang Tang 
13993808843cSYunkang Tang 	if (id[0] == 0x88 &&
14003808843cSYunkang Tang 	    (id[1] == 0x07 ||
14013808843cSYunkang Tang 	     id[1] == 0x08 ||
14023808843cSYunkang Tang 	     (id[1] & 0xf0) == 0xb0 ||
14033808843cSYunkang Tang 	     (id[1] & 0xf0) == 0xc0)) {
14043808843cSYunkang Tang 		return true;
14053808843cSYunkang Tang 	}
14063808843cSYunkang Tang 
14073808843cSYunkang Tang 	return false;
14083808843cSYunkang Tang }
14093808843cSYunkang Tang 
1410d18e53fcSKevin Cernekee static int alps_enter_command_mode(struct psmouse *psmouse)
141125bded7cSSeth Forshee {
141225bded7cSSeth Forshee 	unsigned char param[4];
141325bded7cSSeth Forshee 
141424ba9707SKevin Cernekee 	if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_RESET_WRAP, param)) {
141525bded7cSSeth Forshee 		psmouse_err(psmouse, "failed to enter command mode\n");
141625bded7cSSeth Forshee 		return -1;
141725bded7cSSeth Forshee 	}
141825bded7cSSeth Forshee 
14193808843cSYunkang Tang 	if (!alps_check_valid_firmware_id(param)) {
142025bded7cSSeth Forshee 		psmouse_dbg(psmouse,
142124ba9707SKevin Cernekee 			    "unknown response while entering command mode\n");
142225bded7cSSeth Forshee 		return -1;
142325bded7cSSeth Forshee 	}
142425bded7cSSeth Forshee 	return 0;
142525bded7cSSeth Forshee }
142625bded7cSSeth Forshee 
142725bded7cSSeth Forshee static inline int alps_exit_command_mode(struct psmouse *psmouse)
142825bded7cSSeth Forshee {
142925bded7cSSeth Forshee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
143025bded7cSSeth Forshee 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM))
143125bded7cSSeth Forshee 		return -1;
143225bded7cSSeth Forshee 	return 0;
143325bded7cSSeth Forshee }
143425bded7cSSeth Forshee 
14351da177e4SLinus Torvalds /*
14361da177e4SLinus Torvalds  * For DualPoint devices select the device that should respond to
14371da177e4SLinus Torvalds  * subsequent commands. It looks like glidepad is behind stickpointer,
14381da177e4SLinus Torvalds  * I'd thought it would be other way around...
14391da177e4SLinus Torvalds  */
144025bded7cSSeth Forshee static int alps_passthrough_mode_v2(struct psmouse *psmouse, bool enable)
14411da177e4SLinus Torvalds {
14421da177e4SLinus Torvalds 	struct ps2dev *ps2dev = &psmouse->ps2dev;
14431da177e4SLinus Torvalds 	int cmd = enable ? PSMOUSE_CMD_SETSCALE21 : PSMOUSE_CMD_SETSCALE11;
14441da177e4SLinus Torvalds 
14451da177e4SLinus Torvalds 	if (ps2_command(ps2dev, NULL, cmd) ||
14461da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, cmd) ||
14471da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, cmd) ||
14481da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
14491da177e4SLinus Torvalds 		return -1;
14501da177e4SLinus Torvalds 
14511da177e4SLinus Torvalds 	/* we may get 3 more bytes, just ignore them */
1452c611763dSDmitry Torokhov 	ps2_drain(ps2dev, 3, 100);
14531da177e4SLinus Torvalds 
14541da177e4SLinus Torvalds 	return 0;
14551da177e4SLinus Torvalds }
14561da177e4SLinus Torvalds 
145725bded7cSSeth Forshee static int alps_absolute_mode_v1_v2(struct psmouse *psmouse)
14581da177e4SLinus Torvalds {
14591da177e4SLinus Torvalds 	struct ps2dev *ps2dev = &psmouse->ps2dev;
14601da177e4SLinus Torvalds 
14611da177e4SLinus Torvalds 	/* Try ALPS magic knock - 4 disable before enable */
14621da177e4SLinus Torvalds 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
14631da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
14641da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
14651da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
14661da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
14671da177e4SLinus Torvalds 		return -1;
14681da177e4SLinus Torvalds 
14691da177e4SLinus Torvalds 	/*
14701da177e4SLinus Torvalds 	 * Switch mouse to poll (remote) mode so motion data will not
14711da177e4SLinus Torvalds 	 * get in our way
14721da177e4SLinus Torvalds 	 */
14731da177e4SLinus Torvalds 	return ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETPOLL);
14741da177e4SLinus Torvalds }
14751da177e4SLinus Torvalds 
147695f75e91SYunkang Tang static int alps_monitor_mode_send_word(struct psmouse *psmouse, u16 word)
147795f75e91SYunkang Tang {
147895f75e91SYunkang Tang 	int i, nibble;
147995f75e91SYunkang Tang 
148095f75e91SYunkang Tang 	/*
148195f75e91SYunkang Tang 	 * b0-b11 are valid bits, send sequence is inverse.
148295f75e91SYunkang Tang 	 * e.g. when word = 0x0123, nibble send sequence is 3, 2, 1
148395f75e91SYunkang Tang 	 */
148495f75e91SYunkang Tang 	for (i = 0; i <= 8; i += 4) {
148595f75e91SYunkang Tang 		nibble = (word >> i) & 0xf;
148695f75e91SYunkang Tang 		if (alps_command_mode_send_nibble(psmouse, nibble))
148795f75e91SYunkang Tang 			return -1;
148895f75e91SYunkang Tang 	}
148995f75e91SYunkang Tang 
149095f75e91SYunkang Tang 	return 0;
149195f75e91SYunkang Tang }
149295f75e91SYunkang Tang 
149395f75e91SYunkang Tang static int alps_monitor_mode_write_reg(struct psmouse *psmouse,
149495f75e91SYunkang Tang 				       u16 addr, u16 value)
149595f75e91SYunkang Tang {
149695f75e91SYunkang Tang 	struct ps2dev *ps2dev = &psmouse->ps2dev;
149795f75e91SYunkang Tang 
149895f75e91SYunkang Tang 	/* 0x0A0 is the command to write the word */
149995f75e91SYunkang Tang 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE) ||
150095f75e91SYunkang Tang 	    alps_monitor_mode_send_word(psmouse, 0x0A0) ||
150195f75e91SYunkang Tang 	    alps_monitor_mode_send_word(psmouse, addr) ||
150295f75e91SYunkang Tang 	    alps_monitor_mode_send_word(psmouse, value) ||
150395f75e91SYunkang Tang 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
150495f75e91SYunkang Tang 		return -1;
150595f75e91SYunkang Tang 
150695f75e91SYunkang Tang 	return 0;
150795f75e91SYunkang Tang }
150895f75e91SYunkang Tang 
150995f75e91SYunkang Tang static int alps_monitor_mode(struct psmouse *psmouse, bool enable)
151095f75e91SYunkang Tang {
151195f75e91SYunkang Tang 	struct ps2dev *ps2dev = &psmouse->ps2dev;
151295f75e91SYunkang Tang 
151395f75e91SYunkang Tang 	if (enable) {
151495f75e91SYunkang Tang 		/* EC E9 F5 F5 E7 E6 E7 E9 to enter monitor mode */
151595f75e91SYunkang Tang 		if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
151695f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO) ||
151795f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
151895f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
151995f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
152095f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
152195f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
152295f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO))
152395f75e91SYunkang Tang 			return -1;
152495f75e91SYunkang Tang 	} else {
152595f75e91SYunkang Tang 		/* EC to exit monitor mode */
152695f75e91SYunkang Tang 		if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP))
152795f75e91SYunkang Tang 			return -1;
152895f75e91SYunkang Tang 	}
152995f75e91SYunkang Tang 
153095f75e91SYunkang Tang 	return 0;
153195f75e91SYunkang Tang }
153295f75e91SYunkang Tang 
153395f75e91SYunkang Tang static int alps_absolute_mode_v6(struct psmouse *psmouse)
153495f75e91SYunkang Tang {
153595f75e91SYunkang Tang 	u16 reg_val = 0x181;
153695f75e91SYunkang Tang 	int ret = -1;
153795f75e91SYunkang Tang 
153895f75e91SYunkang Tang 	/* enter monitor mode, to write the register */
153995f75e91SYunkang Tang 	if (alps_monitor_mode(psmouse, true))
154095f75e91SYunkang Tang 		return -1;
154195f75e91SYunkang Tang 
154295f75e91SYunkang Tang 	ret = alps_monitor_mode_write_reg(psmouse, 0x000, reg_val);
154395f75e91SYunkang Tang 
154495f75e91SYunkang Tang 	if (alps_monitor_mode(psmouse, false))
154595f75e91SYunkang Tang 		ret = -1;
154695f75e91SYunkang Tang 
154795f75e91SYunkang Tang 	return ret;
154895f75e91SYunkang Tang }
154995f75e91SYunkang Tang 
15501da177e4SLinus Torvalds static int alps_get_status(struct psmouse *psmouse, char *param)
15511da177e4SLinus Torvalds {
15521da177e4SLinus Torvalds 	/* Get status: 0xF5 0xF5 0xF5 0xE9 */
155324ba9707SKevin Cernekee 	if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_DISABLE, param))
15541da177e4SLinus Torvalds 		return -1;
15551da177e4SLinus Torvalds 
15561da177e4SLinus Torvalds 	return 0;
15571da177e4SLinus Torvalds }
15581da177e4SLinus Torvalds 
15591da177e4SLinus Torvalds /*
15601da177e4SLinus Torvalds  * Turn touchpad tapping on or off. The sequences are:
15611da177e4SLinus Torvalds  * 0xE9 0xF5 0xF5 0xF3 0x0A to enable,
15621da177e4SLinus Torvalds  * 0xE9 0xF5 0xF5 0xE8 0x00 to disable.
15631da177e4SLinus Torvalds  * My guess that 0xE9 (GetInfo) is here as a sync point.
15641da177e4SLinus Torvalds  * For models that also have stickpointer (DualPoints) its tapping
15651da177e4SLinus Torvalds  * is controlled separately (0xE6 0xE6 0xE6 0xF3 0x14|0x0A) but
15661da177e4SLinus Torvalds  * we don't fiddle with it.
15671da177e4SLinus Torvalds  */
15681da177e4SLinus Torvalds static int alps_tap_mode(struct psmouse *psmouse, int enable)
15691da177e4SLinus Torvalds {
15701da177e4SLinus Torvalds 	struct ps2dev *ps2dev = &psmouse->ps2dev;
15711da177e4SLinus Torvalds 	int cmd = enable ? PSMOUSE_CMD_SETRATE : PSMOUSE_CMD_SETRES;
15721da177e4SLinus Torvalds 	unsigned char tap_arg = enable ? 0x0A : 0x00;
15731da177e4SLinus Torvalds 	unsigned char param[4];
15741da177e4SLinus Torvalds 
15751da177e4SLinus Torvalds 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO) ||
15761da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
15771da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
15781da177e4SLinus Torvalds 	    ps2_command(ps2dev, &tap_arg, cmd))
15791da177e4SLinus Torvalds 		return -1;
15801da177e4SLinus Torvalds 
15811da177e4SLinus Torvalds 	if (alps_get_status(psmouse, param))
15821da177e4SLinus Torvalds 		return -1;
15831da177e4SLinus Torvalds 
15841da177e4SLinus Torvalds 	return 0;
15851da177e4SLinus Torvalds }
15861da177e4SLinus Torvalds 
1587f0d5c6f4SDmitry Torokhov /*
1588f0d5c6f4SDmitry Torokhov  * alps_poll() - poll the touchpad for current motion packet.
1589f0d5c6f4SDmitry Torokhov  * Used in resync.
1590f0d5c6f4SDmitry Torokhov  */
1591f0d5c6f4SDmitry Torokhov static int alps_poll(struct psmouse *psmouse)
1592f0d5c6f4SDmitry Torokhov {
1593f0d5c6f4SDmitry Torokhov 	struct alps_data *priv = psmouse->private;
1594b46615feSSeth Forshee 	unsigned char buf[sizeof(psmouse->packet)];
1595b7802c5cSDmitry Torokhov 	bool poll_failed;
1596f0d5c6f4SDmitry Torokhov 
159799df65e7SKevin Cernekee 	if (priv->flags & ALPS_PASS)
159825bded7cSSeth Forshee 		alps_passthrough_mode_v2(psmouse, true);
1599f0d5c6f4SDmitry Torokhov 
1600f0d5c6f4SDmitry Torokhov 	poll_failed = ps2_command(&psmouse->ps2dev, buf,
1601f0d5c6f4SDmitry Torokhov 				  PSMOUSE_CMD_POLL | (psmouse->pktsize << 8)) < 0;
1602f0d5c6f4SDmitry Torokhov 
160399df65e7SKevin Cernekee 	if (priv->flags & ALPS_PASS)
160425bded7cSSeth Forshee 		alps_passthrough_mode_v2(psmouse, false);
1605f0d5c6f4SDmitry Torokhov 
160699df65e7SKevin Cernekee 	if (poll_failed || (buf[0] & priv->mask0) != priv->byte0)
1607f0d5c6f4SDmitry Torokhov 		return -1;
1608f0d5c6f4SDmitry Torokhov 
1609f0d5c6f4SDmitry Torokhov 	if ((psmouse->badbyte & 0xc8) == 0x08) {
1610f0d5c6f4SDmitry Torokhov /*
1611f0d5c6f4SDmitry Torokhov  * Poll the track stick ...
1612f0d5c6f4SDmitry Torokhov  */
1613f0d5c6f4SDmitry Torokhov 		if (ps2_command(&psmouse->ps2dev, buf, PSMOUSE_CMD_POLL | (3 << 8)))
1614f0d5c6f4SDmitry Torokhov 			return -1;
1615f0d5c6f4SDmitry Torokhov 	}
1616f0d5c6f4SDmitry Torokhov 
1617f0d5c6f4SDmitry Torokhov 	memcpy(psmouse->packet, buf, sizeof(buf));
1618f0d5c6f4SDmitry Torokhov 	return 0;
1619f0d5c6f4SDmitry Torokhov }
1620f0d5c6f4SDmitry Torokhov 
162125bded7cSSeth Forshee static int alps_hw_init_v1_v2(struct psmouse *psmouse)
16221da177e4SLinus Torvalds {
16231da177e4SLinus Torvalds 	struct alps_data *priv = psmouse->private;
16241da177e4SLinus Torvalds 
162599df65e7SKevin Cernekee 	if ((priv->flags & ALPS_PASS) &&
162625bded7cSSeth Forshee 	    alps_passthrough_mode_v2(psmouse, true)) {
16271da177e4SLinus Torvalds 		return -1;
1628b7802c5cSDmitry Torokhov 	}
16291da177e4SLinus Torvalds 
1630b7802c5cSDmitry Torokhov 	if (alps_tap_mode(psmouse, true)) {
1631b5d21704SDmitry Torokhov 		psmouse_warn(psmouse, "Failed to enable hardware tapping\n");
16321da177e4SLinus Torvalds 		return -1;
1633963f626dSPeter Osterlund 	}
16341da177e4SLinus Torvalds 
163525bded7cSSeth Forshee 	if (alps_absolute_mode_v1_v2(psmouse)) {
1636b5d21704SDmitry Torokhov 		psmouse_err(psmouse, "Failed to enable absolute mode\n");
16371da177e4SLinus Torvalds 		return -1;
16381da177e4SLinus Torvalds 	}
16391da177e4SLinus Torvalds 
164099df65e7SKevin Cernekee 	if ((priv->flags & ALPS_PASS) &&
164125bded7cSSeth Forshee 	    alps_passthrough_mode_v2(psmouse, false)) {
16421da177e4SLinus Torvalds 		return -1;
1643b7802c5cSDmitry Torokhov 	}
16441da177e4SLinus Torvalds 
16451e0c5b12SDmitry Torokhov 	/* ALPS needs stream mode, otherwise it won't report any data */
16461e0c5b12SDmitry Torokhov 	if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)) {
1647b5d21704SDmitry Torokhov 		psmouse_err(psmouse, "Failed to enable stream mode\n");
16481e0c5b12SDmitry Torokhov 		return -1;
16491e0c5b12SDmitry Torokhov 	}
16501e0c5b12SDmitry Torokhov 
16511e0c5b12SDmitry Torokhov 	return 0;
16521e0c5b12SDmitry Torokhov }
16531e0c5b12SDmitry Torokhov 
165495f75e91SYunkang Tang static int alps_hw_init_v6(struct psmouse *psmouse)
165595f75e91SYunkang Tang {
165695f75e91SYunkang Tang 	unsigned char param[2] = {0xC8, 0x14};
165795f75e91SYunkang Tang 
165895f75e91SYunkang Tang 	/* Enter passthrough mode to let trackpoint enter 6byte raw mode */
165995f75e91SYunkang Tang 	if (alps_passthrough_mode_v2(psmouse, true))
166095f75e91SYunkang Tang 		return -1;
166195f75e91SYunkang Tang 
166295f75e91SYunkang Tang 	if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
166395f75e91SYunkang Tang 	    ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
166495f75e91SYunkang Tang 	    ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
166595f75e91SYunkang Tang 	    ps2_command(&psmouse->ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
166695f75e91SYunkang Tang 	    ps2_command(&psmouse->ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
166795f75e91SYunkang Tang 		return -1;
166895f75e91SYunkang Tang 
166995f75e91SYunkang Tang 	if (alps_passthrough_mode_v2(psmouse, false))
167095f75e91SYunkang Tang 		return -1;
167195f75e91SYunkang Tang 
167295f75e91SYunkang Tang 	if (alps_absolute_mode_v6(psmouse)) {
167395f75e91SYunkang Tang 		psmouse_err(psmouse, "Failed to enable absolute mode\n");
167495f75e91SYunkang Tang 		return -1;
167595f75e91SYunkang Tang 	}
167695f75e91SYunkang Tang 
167795f75e91SYunkang Tang 	return 0;
167895f75e91SYunkang Tang }
167995f75e91SYunkang Tang 
168025bded7cSSeth Forshee /*
1681cd401204SKevin Cernekee  * Enable or disable passthrough mode to the trackstick.
168225bded7cSSeth Forshee  */
1683cd401204SKevin Cernekee static int alps_passthrough_mode_v3(struct psmouse *psmouse,
1684cd401204SKevin Cernekee 				    int reg_base, bool enable)
168525bded7cSSeth Forshee {
1686cd401204SKevin Cernekee 	int reg_val, ret = -1;
168725bded7cSSeth Forshee 
1688d18e53fcSKevin Cernekee 	if (alps_enter_command_mode(psmouse))
168925bded7cSSeth Forshee 		return -1;
169025bded7cSSeth Forshee 
1691cd401204SKevin Cernekee 	reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x0008);
1692cd401204SKevin Cernekee 	if (reg_val == -1)
1693cd401204SKevin Cernekee 		goto error;
1694cd401204SKevin Cernekee 
169525bded7cSSeth Forshee 	if (enable)
169625bded7cSSeth Forshee 		reg_val |= 0x01;
169725bded7cSSeth Forshee 	else
169825bded7cSSeth Forshee 		reg_val &= ~0x01;
169925bded7cSSeth Forshee 
1700cd401204SKevin Cernekee 	ret = __alps_command_mode_write_reg(psmouse, reg_val);
170125bded7cSSeth Forshee 
1702cd401204SKevin Cernekee error:
1703cd401204SKevin Cernekee 	if (alps_exit_command_mode(psmouse))
1704cd401204SKevin Cernekee 		ret = -1;
1705cd401204SKevin Cernekee 	return ret;
170625bded7cSSeth Forshee }
170725bded7cSSeth Forshee 
170825bded7cSSeth Forshee /* Must be in command mode when calling this function */
170925bded7cSSeth Forshee static int alps_absolute_mode_v3(struct psmouse *psmouse)
171025bded7cSSeth Forshee {
171125bded7cSSeth Forshee 	int reg_val;
171225bded7cSSeth Forshee 
171325bded7cSSeth Forshee 	reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
171425bded7cSSeth Forshee 	if (reg_val == -1)
171525bded7cSSeth Forshee 		return -1;
171625bded7cSSeth Forshee 
171725bded7cSSeth Forshee 	reg_val |= 0x06;
171825bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, reg_val))
171925bded7cSSeth Forshee 		return -1;
172025bded7cSSeth Forshee 
172125bded7cSSeth Forshee 	return 0;
172225bded7cSSeth Forshee }
172325bded7cSSeth Forshee 
1724cd401204SKevin Cernekee static int alps_probe_trackstick_v3(struct psmouse *psmouse, int reg_base)
172525bded7cSSeth Forshee {
1726cd401204SKevin Cernekee 	int ret = -EIO, reg_val;
172725bded7cSSeth Forshee 
1728d18e53fcSKevin Cernekee 	if (alps_enter_command_mode(psmouse))
172925bded7cSSeth Forshee 		goto error;
173025bded7cSSeth Forshee 
1731cd401204SKevin Cernekee 	reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x08);
173225bded7cSSeth Forshee 	if (reg_val == -1)
173325bded7cSSeth Forshee 		goto error;
1734cd401204SKevin Cernekee 
1735cd401204SKevin Cernekee 	/* bit 7: trackstick is present */
1736cd401204SKevin Cernekee 	ret = reg_val & 0x80 ? 0 : -ENODEV;
1737cd401204SKevin Cernekee 
1738cd401204SKevin Cernekee error:
1739cd401204SKevin Cernekee 	alps_exit_command_mode(psmouse);
1740cd401204SKevin Cernekee 	return ret;
1741cd401204SKevin Cernekee }
1742cd401204SKevin Cernekee 
1743cd401204SKevin Cernekee static int alps_setup_trackstick_v3(struct psmouse *psmouse, int reg_base)
1744cd401204SKevin Cernekee {
1745cd401204SKevin Cernekee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
1746cd401204SKevin Cernekee 	int ret = 0;
1747cd401204SKevin Cernekee 	unsigned char param[4];
1748cd401204SKevin Cernekee 
1749cd401204SKevin Cernekee 	if (alps_passthrough_mode_v3(psmouse, reg_base, true))
1750cd401204SKevin Cernekee 		return -EIO;
175125bded7cSSeth Forshee 
175225bded7cSSeth Forshee 	/*
175325bded7cSSeth Forshee 	 * E7 report for the trackstick
175425bded7cSSeth Forshee 	 *
175525bded7cSSeth Forshee 	 * There have been reports of failures to seem to trace back
175625bded7cSSeth Forshee 	 * to the above trackstick check failing. When these occur
175725bded7cSSeth Forshee 	 * this E7 report fails, so when that happens we continue
175825bded7cSSeth Forshee 	 * with the assumption that there isn't a trackstick after
175925bded7cSSeth Forshee 	 * all.
176025bded7cSSeth Forshee 	 */
1761cd401204SKevin Cernekee 	if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_SETSCALE21, param)) {
176225bded7cSSeth Forshee 		psmouse_warn(psmouse, "trackstick E7 report failed\n");
1763cd401204SKevin Cernekee 		ret = -ENODEV;
176425bded7cSSeth Forshee 	} else {
176539fbe585SDmitry Torokhov 		psmouse_dbg(psmouse, "trackstick E7 report: %3ph\n", param);
176625bded7cSSeth Forshee 
176725bded7cSSeth Forshee 		/*
176825bded7cSSeth Forshee 		 * Not sure what this does, but it is absolutely
176925bded7cSSeth Forshee 		 * essential. Without it, the touchpad does not
177025bded7cSSeth Forshee 		 * work at all and the trackstick just emits normal
177125bded7cSSeth Forshee 		 * PS/2 packets.
177225bded7cSSeth Forshee 		 */
177325bded7cSSeth Forshee 		if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
177425bded7cSSeth Forshee 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
177525bded7cSSeth Forshee 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
177625bded7cSSeth Forshee 		    alps_command_mode_send_nibble(psmouse, 0x9) ||
177725bded7cSSeth Forshee 		    alps_command_mode_send_nibble(psmouse, 0x4)) {
177825bded7cSSeth Forshee 			psmouse_err(psmouse,
177925bded7cSSeth Forshee 				    "Error sending magic E6 sequence\n");
1780cd401204SKevin Cernekee 			ret = -EIO;
178125bded7cSSeth Forshee 			goto error;
178225bded7cSSeth Forshee 		}
178325bded7cSSeth Forshee 
1784cd401204SKevin Cernekee 		/*
1785cd401204SKevin Cernekee 		 * This ensures the trackstick packets are in the format
1786cd401204SKevin Cernekee 		 * supported by this driver. If bit 1 isn't set the packet
1787cd401204SKevin Cernekee 		 * format is different.
1788cd401204SKevin Cernekee 		 */
1789d18e53fcSKevin Cernekee 		if (alps_enter_command_mode(psmouse) ||
1790cd401204SKevin Cernekee 		    alps_command_mode_write_reg(psmouse,
1791cd401204SKevin Cernekee 						reg_base + 0x08, 0x82) ||
1792cd401204SKevin Cernekee 		    alps_exit_command_mode(psmouse))
1793cd401204SKevin Cernekee 			ret = -EIO;
1794cd401204SKevin Cernekee 	}
1795cd401204SKevin Cernekee 
1796cd401204SKevin Cernekee error:
1797cd401204SKevin Cernekee 	if (alps_passthrough_mode_v3(psmouse, reg_base, false))
1798cd401204SKevin Cernekee 		ret = -EIO;
1799cd401204SKevin Cernekee 
1800cd401204SKevin Cernekee 	return ret;
1801cd401204SKevin Cernekee }
1802cd401204SKevin Cernekee 
1803cd401204SKevin Cernekee static int alps_hw_init_v3(struct psmouse *psmouse)
1804cd401204SKevin Cernekee {
1805cd401204SKevin Cernekee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
1806cd401204SKevin Cernekee 	int reg_val;
1807cd401204SKevin Cernekee 	unsigned char param[4];
1808cd401204SKevin Cernekee 
1809cd401204SKevin Cernekee 	reg_val = alps_probe_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE);
1810cd401204SKevin Cernekee 	if (reg_val == -EIO)
1811cd401204SKevin Cernekee 		goto error;
181239fbe585SDmitry Torokhov 
1813cd401204SKevin Cernekee 	if (reg_val == 0 &&
1814cd401204SKevin Cernekee 	    alps_setup_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE) == -EIO)
1815cd401204SKevin Cernekee 		goto error;
1816cd401204SKevin Cernekee 
1817d18e53fcSKevin Cernekee 	if (alps_enter_command_mode(psmouse) ||
1818cd401204SKevin Cernekee 	    alps_absolute_mode_v3(psmouse)) {
181925bded7cSSeth Forshee 		psmouse_err(psmouse, "Failed to enter absolute mode\n");
182025bded7cSSeth Forshee 		goto error;
182125bded7cSSeth Forshee 	}
182225bded7cSSeth Forshee 
182325bded7cSSeth Forshee 	reg_val = alps_command_mode_read_reg(psmouse, 0x0006);
182425bded7cSSeth Forshee 	if (reg_val == -1)
182525bded7cSSeth Forshee 		goto error;
182625bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
182725bded7cSSeth Forshee 		goto error;
182825bded7cSSeth Forshee 
182925bded7cSSeth Forshee 	reg_val = alps_command_mode_read_reg(psmouse, 0x0007);
183025bded7cSSeth Forshee 	if (reg_val == -1)
183125bded7cSSeth Forshee 		goto error;
183225bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
183325bded7cSSeth Forshee 		goto error;
183425bded7cSSeth Forshee 
183525bded7cSSeth Forshee 	if (alps_command_mode_read_reg(psmouse, 0x0144) == -1)
183625bded7cSSeth Forshee 		goto error;
183725bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, 0x04))
183825bded7cSSeth Forshee 		goto error;
183925bded7cSSeth Forshee 
184025bded7cSSeth Forshee 	if (alps_command_mode_read_reg(psmouse, 0x0159) == -1)
184125bded7cSSeth Forshee 		goto error;
184225bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, 0x03))
184325bded7cSSeth Forshee 		goto error;
184425bded7cSSeth Forshee 
184525bded7cSSeth Forshee 	if (alps_command_mode_read_reg(psmouse, 0x0163) == -1)
184625bded7cSSeth Forshee 		goto error;
184725bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0163, 0x03))
184825bded7cSSeth Forshee 		goto error;
184925bded7cSSeth Forshee 
185025bded7cSSeth Forshee 	if (alps_command_mode_read_reg(psmouse, 0x0162) == -1)
185125bded7cSSeth Forshee 		goto error;
185225bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0162, 0x04))
185325bded7cSSeth Forshee 		goto error;
185425bded7cSSeth Forshee 
185525bded7cSSeth Forshee 	alps_exit_command_mode(psmouse);
185625bded7cSSeth Forshee 
185725bded7cSSeth Forshee 	/* Set rate and enable data reporting */
185825bded7cSSeth Forshee 	param[0] = 0x64;
185925bded7cSSeth Forshee 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
186025bded7cSSeth Forshee 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
186125bded7cSSeth Forshee 		psmouse_err(psmouse, "Failed to enable data reporting\n");
186225bded7cSSeth Forshee 		return -1;
186325bded7cSSeth Forshee 	}
186425bded7cSSeth Forshee 
186525bded7cSSeth Forshee 	return 0;
186625bded7cSSeth Forshee 
186725bded7cSSeth Forshee error:
186825bded7cSSeth Forshee 	/*
186925bded7cSSeth Forshee 	 * Leaving the touchpad in command mode will essentially render
187025bded7cSSeth Forshee 	 * it unusable until the machine reboots, so exit it here just
187125bded7cSSeth Forshee 	 * to be safe
187225bded7cSSeth Forshee 	 */
187325bded7cSSeth Forshee 	alps_exit_command_mode(psmouse);
187425bded7cSSeth Forshee 	return -1;
187525bded7cSSeth Forshee }
187625bded7cSSeth Forshee 
1877f3f33c67SHans de Goede static int alps_get_v3_v7_resolution(struct psmouse *psmouse, int reg_pitch)
1878f3f33c67SHans de Goede {
1879f3f33c67SHans de Goede 	int reg, x_pitch, y_pitch, x_electrode, y_electrode, x_phys, y_phys;
1880f3f33c67SHans de Goede 	struct alps_data *priv = psmouse->private;
1881f3f33c67SHans de Goede 
1882f3f33c67SHans de Goede 	reg = alps_command_mode_read_reg(psmouse, reg_pitch);
1883f3f33c67SHans de Goede 	if (reg < 0)
1884f3f33c67SHans de Goede 		return reg;
1885f3f33c67SHans de Goede 
1886f3f33c67SHans de Goede 	x_pitch = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
1887f3f33c67SHans de Goede 	x_pitch = 50 + 2 * x_pitch; /* In 0.1 mm units */
1888f3f33c67SHans de Goede 
1889f3f33c67SHans de Goede 	y_pitch = (char)reg >> 4; /* sign extend upper 4 bits */
1890f3f33c67SHans de Goede 	y_pitch = 36 + 2 * y_pitch; /* In 0.1 mm units */
1891f3f33c67SHans de Goede 
1892f3f33c67SHans de Goede 	reg = alps_command_mode_read_reg(psmouse, reg_pitch + 1);
1893f3f33c67SHans de Goede 	if (reg < 0)
1894f3f33c67SHans de Goede 		return reg;
1895f3f33c67SHans de Goede 
1896f3f33c67SHans de Goede 	x_electrode = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
1897f3f33c67SHans de Goede 	x_electrode = 17 + x_electrode;
1898f3f33c67SHans de Goede 
1899f3f33c67SHans de Goede 	y_electrode = (char)reg >> 4; /* sign extend upper 4 bits */
1900f3f33c67SHans de Goede 	y_electrode = 13 + y_electrode;
1901f3f33c67SHans de Goede 
1902f3f33c67SHans de Goede 	x_phys = x_pitch * (x_electrode - 1); /* In 0.1 mm units */
1903f3f33c67SHans de Goede 	y_phys = y_pitch * (y_electrode - 1); /* In 0.1 mm units */
1904f3f33c67SHans de Goede 
1905f3f33c67SHans de Goede 	priv->x_res = priv->x_max * 10 / x_phys; /* units / mm */
1906f3f33c67SHans de Goede 	priv->y_res = priv->y_max * 10 / y_phys; /* units / mm */
1907f3f33c67SHans de Goede 
1908f3f33c67SHans de Goede 	psmouse_dbg(psmouse,
1909f3f33c67SHans de Goede 		    "pitch %dx%d num-electrodes %dx%d physical size %dx%d mm res %dx%d\n",
1910f3f33c67SHans de Goede 		    x_pitch, y_pitch, x_electrode, y_electrode,
1911f3f33c67SHans de Goede 		    x_phys / 10, y_phys / 10, priv->x_res, priv->y_res);
1912f3f33c67SHans de Goede 
1913f3f33c67SHans de Goede 	return 0;
1914f3f33c67SHans de Goede }
1915f3f33c67SHans de Goede 
19161302bac3SKevin Cernekee static int alps_hw_init_rushmore_v3(struct psmouse *psmouse)
19171302bac3SKevin Cernekee {
1918cd401204SKevin Cernekee 	struct alps_data *priv = psmouse->private;
19191302bac3SKevin Cernekee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
19201302bac3SKevin Cernekee 	int reg_val, ret = -1;
19211302bac3SKevin Cernekee 
1922cd401204SKevin Cernekee 	if (priv->flags & ALPS_DUALPOINT) {
1923cd401204SKevin Cernekee 		reg_val = alps_setup_trackstick_v3(psmouse,
1924cd401204SKevin Cernekee 						   ALPS_REG_BASE_RUSHMORE);
1925cd401204SKevin Cernekee 		if (reg_val == -EIO)
1926cd401204SKevin Cernekee 			goto error;
1927cd401204SKevin Cernekee 		if (reg_val == -ENODEV)
1928cd401204SKevin Cernekee 			priv->flags &= ~ALPS_DUALPOINT;
1929cd401204SKevin Cernekee 	}
1930cd401204SKevin Cernekee 
1931d18e53fcSKevin Cernekee 	if (alps_enter_command_mode(psmouse) ||
19321302bac3SKevin Cernekee 	    alps_command_mode_read_reg(psmouse, 0xc2d9) == -1 ||
19331302bac3SKevin Cernekee 	    alps_command_mode_write_reg(psmouse, 0xc2cb, 0x00))
19341302bac3SKevin Cernekee 		goto error;
19351302bac3SKevin Cernekee 
1936f3f33c67SHans de Goede 	if (alps_get_v3_v7_resolution(psmouse, 0xc2da))
1937f3f33c67SHans de Goede 		goto error;
1938f3f33c67SHans de Goede 
19391302bac3SKevin Cernekee 	reg_val = alps_command_mode_read_reg(psmouse, 0xc2c6);
19401302bac3SKevin Cernekee 	if (reg_val == -1)
19411302bac3SKevin Cernekee 		goto error;
19421302bac3SKevin Cernekee 	if (__alps_command_mode_write_reg(psmouse, reg_val & 0xfd))
19431302bac3SKevin Cernekee 		goto error;
19441302bac3SKevin Cernekee 
19451302bac3SKevin Cernekee 	if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
19461302bac3SKevin Cernekee 		goto error;
19471302bac3SKevin Cernekee 
19481302bac3SKevin Cernekee 	/* enter absolute mode */
19491302bac3SKevin Cernekee 	reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
19501302bac3SKevin Cernekee 	if (reg_val == -1)
19511302bac3SKevin Cernekee 		goto error;
19521302bac3SKevin Cernekee 	if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
19531302bac3SKevin Cernekee 		goto error;
19541302bac3SKevin Cernekee 
19551302bac3SKevin Cernekee 	alps_exit_command_mode(psmouse);
19561302bac3SKevin Cernekee 	return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
19571302bac3SKevin Cernekee 
19581302bac3SKevin Cernekee error:
19591302bac3SKevin Cernekee 	alps_exit_command_mode(psmouse);
19601302bac3SKevin Cernekee 	return ret;
19611302bac3SKevin Cernekee }
19621302bac3SKevin Cernekee 
196325bded7cSSeth Forshee /* Must be in command mode when calling this function */
196425bded7cSSeth Forshee static int alps_absolute_mode_v4(struct psmouse *psmouse)
196525bded7cSSeth Forshee {
196625bded7cSSeth Forshee 	int reg_val;
196725bded7cSSeth Forshee 
196825bded7cSSeth Forshee 	reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
196925bded7cSSeth Forshee 	if (reg_val == -1)
197025bded7cSSeth Forshee 		return -1;
197125bded7cSSeth Forshee 
197225bded7cSSeth Forshee 	reg_val |= 0x02;
197325bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, reg_val))
197425bded7cSSeth Forshee 		return -1;
197525bded7cSSeth Forshee 
197625bded7cSSeth Forshee 	return 0;
197725bded7cSSeth Forshee }
197825bded7cSSeth Forshee 
197925bded7cSSeth Forshee static int alps_hw_init_v4(struct psmouse *psmouse)
198025bded7cSSeth Forshee {
198125bded7cSSeth Forshee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
198225bded7cSSeth Forshee 	unsigned char param[4];
198325bded7cSSeth Forshee 
1984d18e53fcSKevin Cernekee 	if (alps_enter_command_mode(psmouse))
198525bded7cSSeth Forshee 		goto error;
198625bded7cSSeth Forshee 
198725bded7cSSeth Forshee 	if (alps_absolute_mode_v4(psmouse)) {
198825bded7cSSeth Forshee 		psmouse_err(psmouse, "Failed to enter absolute mode\n");
198925bded7cSSeth Forshee 		goto error;
199025bded7cSSeth Forshee 	}
199125bded7cSSeth Forshee 
199225bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0007, 0x8c))
199325bded7cSSeth Forshee 		goto error;
199425bded7cSSeth Forshee 
199525bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0149, 0x03))
199625bded7cSSeth Forshee 		goto error;
199725bded7cSSeth Forshee 
199825bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0160, 0x03))
199925bded7cSSeth Forshee 		goto error;
200025bded7cSSeth Forshee 
200125bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x017f, 0x15))
200225bded7cSSeth Forshee 		goto error;
200325bded7cSSeth Forshee 
200425bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0151, 0x01))
200525bded7cSSeth Forshee 		goto error;
200625bded7cSSeth Forshee 
200725bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0168, 0x03))
200825bded7cSSeth Forshee 		goto error;
200925bded7cSSeth Forshee 
201025bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x014a, 0x03))
201125bded7cSSeth Forshee 		goto error;
201225bded7cSSeth Forshee 
201325bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0161, 0x03))
201425bded7cSSeth Forshee 		goto error;
201525bded7cSSeth Forshee 
201625bded7cSSeth Forshee 	alps_exit_command_mode(psmouse);
201725bded7cSSeth Forshee 
201825bded7cSSeth Forshee 	/*
201925bded7cSSeth Forshee 	 * This sequence changes the output from a 9-byte to an
202025bded7cSSeth Forshee 	 * 8-byte format. All the same data seems to be present,
202125bded7cSSeth Forshee 	 * just in a more compact format.
202225bded7cSSeth Forshee 	 */
202325bded7cSSeth Forshee 	param[0] = 0xc8;
202425bded7cSSeth Forshee 	param[1] = 0x64;
202525bded7cSSeth Forshee 	param[2] = 0x50;
202625bded7cSSeth Forshee 	if (ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
202725bded7cSSeth Forshee 	    ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE) ||
202825bded7cSSeth Forshee 	    ps2_command(ps2dev, &param[2], PSMOUSE_CMD_SETRATE) ||
202925bded7cSSeth Forshee 	    ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
203025bded7cSSeth Forshee 		return -1;
203125bded7cSSeth Forshee 
203225bded7cSSeth Forshee 	/* Set rate and enable data reporting */
203325bded7cSSeth Forshee 	param[0] = 0x64;
203425bded7cSSeth Forshee 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
203525bded7cSSeth Forshee 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
203625bded7cSSeth Forshee 		psmouse_err(psmouse, "Failed to enable data reporting\n");
203725bded7cSSeth Forshee 		return -1;
203825bded7cSSeth Forshee 	}
203925bded7cSSeth Forshee 
204025bded7cSSeth Forshee 	return 0;
204125bded7cSSeth Forshee 
204225bded7cSSeth Forshee error:
204325bded7cSSeth Forshee 	/*
204425bded7cSSeth Forshee 	 * Leaving the touchpad in command mode will essentially render
204525bded7cSSeth Forshee 	 * it unusable until the machine reboots, so exit it here just
204625bded7cSSeth Forshee 	 * to be safe
204725bded7cSSeth Forshee 	 */
204825bded7cSSeth Forshee 	alps_exit_command_mode(psmouse);
204925bded7cSSeth Forshee 	return -1;
205025bded7cSSeth Forshee }
205125bded7cSSeth Forshee 
2052ee65d4b3SYunkang Tang static int alps_dolphin_get_device_area(struct psmouse *psmouse,
2053ee65d4b3SYunkang Tang 					struct alps_data *priv)
2054ee65d4b3SYunkang Tang {
2055ee65d4b3SYunkang Tang 	struct ps2dev *ps2dev = &psmouse->ps2dev;
2056ee65d4b3SYunkang Tang 	unsigned char param[4] = {0};
2057ee65d4b3SYunkang Tang 	int num_x_electrode, num_y_electrode;
2058ee65d4b3SYunkang Tang 
2059ee65d4b3SYunkang Tang 	if (alps_enter_command_mode(psmouse))
2060ee65d4b3SYunkang Tang 		return -1;
2061ee65d4b3SYunkang Tang 
2062ee65d4b3SYunkang Tang 	param[0] = 0x0a;
2063ee65d4b3SYunkang Tang 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
2064ee65d4b3SYunkang Tang 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2065ee65d4b3SYunkang Tang 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2066ee65d4b3SYunkang Tang 	    ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2067ee65d4b3SYunkang Tang 	    ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE))
2068ee65d4b3SYunkang Tang 		return -1;
2069ee65d4b3SYunkang Tang 
2070ee65d4b3SYunkang Tang 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
2071ee65d4b3SYunkang Tang 		return -1;
2072ee65d4b3SYunkang Tang 
2073ee65d4b3SYunkang Tang 	/*
2074ee65d4b3SYunkang Tang 	 * Dolphin's sensor line number is not fixed. It can be calculated
2075ee65d4b3SYunkang Tang 	 * by adding the device's register value with DOLPHIN_PROFILE_X/YOFFSET.
2076ee65d4b3SYunkang Tang 	 * Further more, we can get device's x_max and y_max by multiplying
2077ee65d4b3SYunkang Tang 	 * sensor line number with DOLPHIN_COUNT_PER_ELECTRODE.
2078ee65d4b3SYunkang Tang 	 *
2079ee65d4b3SYunkang Tang 	 * e.g. When we get register's sensor_x = 11 & sensor_y = 8,
2080ee65d4b3SYunkang Tang 	 *	real sensor line number X = 11 + 8 = 19, and
2081ee65d4b3SYunkang Tang 	 *	real sensor line number Y = 8 + 1 = 9.
2082ee65d4b3SYunkang Tang 	 *	So, x_max = (19 - 1) * 64 = 1152, and
2083ee65d4b3SYunkang Tang 	 *	    y_max = (9 - 1) * 64 = 512.
2084ee65d4b3SYunkang Tang 	 */
2085ee65d4b3SYunkang Tang 	num_x_electrode = DOLPHIN_PROFILE_XOFFSET + (param[2] & 0x0F);
2086ee65d4b3SYunkang Tang 	num_y_electrode = DOLPHIN_PROFILE_YOFFSET + ((param[2] >> 4) & 0x0F);
2087ee65d4b3SYunkang Tang 	priv->x_bits = num_x_electrode;
2088ee65d4b3SYunkang Tang 	priv->y_bits = num_y_electrode;
2089ee65d4b3SYunkang Tang 	priv->x_max = (num_x_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
2090ee65d4b3SYunkang Tang 	priv->y_max = (num_y_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
2091ee65d4b3SYunkang Tang 
2092ee65d4b3SYunkang Tang 	if (alps_exit_command_mode(psmouse))
2093ee65d4b3SYunkang Tang 		return -1;
2094ee65d4b3SYunkang Tang 
2095ee65d4b3SYunkang Tang 	return 0;
2096ee65d4b3SYunkang Tang }
2097ee65d4b3SYunkang Tang 
209875af9e56SDave Turvene static int alps_hw_init_dolphin_v1(struct psmouse *psmouse)
209975af9e56SDave Turvene {
210075af9e56SDave Turvene 	struct ps2dev *ps2dev = &psmouse->ps2dev;
210175af9e56SDave Turvene 	unsigned char param[2];
210275af9e56SDave Turvene 
210375af9e56SDave Turvene 	/* This is dolphin "v1" as empirically defined by florin9doi */
210475af9e56SDave Turvene 	param[0] = 0x64;
210575af9e56SDave Turvene 	param[1] = 0x28;
210675af9e56SDave Turvene 
210775af9e56SDave Turvene 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
210875af9e56SDave Turvene 	    ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
210975af9e56SDave Turvene 	    ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
211075af9e56SDave Turvene 		return -1;
211175af9e56SDave Turvene 
211275af9e56SDave Turvene 	return 0;
211375af9e56SDave Turvene }
211475af9e56SDave Turvene 
21153808843cSYunkang Tang static int alps_hw_init_v7(struct psmouse *psmouse)
21163808843cSYunkang Tang {
21173808843cSYunkang Tang 	struct ps2dev *ps2dev = &psmouse->ps2dev;
21183808843cSYunkang Tang 	int reg_val, ret = -1;
21193808843cSYunkang Tang 
21203808843cSYunkang Tang 	if (alps_enter_command_mode(psmouse) ||
21213808843cSYunkang Tang 	    alps_command_mode_read_reg(psmouse, 0xc2d9) == -1)
21223808843cSYunkang Tang 		goto error;
21233808843cSYunkang Tang 
2124f3f33c67SHans de Goede 	if (alps_get_v3_v7_resolution(psmouse, 0xc397))
2125f3f33c67SHans de Goede 		goto error;
2126f3f33c67SHans de Goede 
21273808843cSYunkang Tang 	if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
21283808843cSYunkang Tang 		goto error;
21293808843cSYunkang Tang 
21303808843cSYunkang Tang 	reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
21313808843cSYunkang Tang 	if (reg_val == -1)
21323808843cSYunkang Tang 		goto error;
21333808843cSYunkang Tang 	if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
21343808843cSYunkang Tang 		goto error;
21353808843cSYunkang Tang 
21363808843cSYunkang Tang 	alps_exit_command_mode(psmouse);
21373808843cSYunkang Tang 	return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
21383808843cSYunkang Tang 
21393808843cSYunkang Tang error:
21403808843cSYunkang Tang 	alps_exit_command_mode(psmouse);
21413808843cSYunkang Tang 	return ret;
21423808843cSYunkang Tang }
21433808843cSYunkang Tang 
214424af5cb9SKevin Cernekee static void alps_set_defaults(struct alps_data *priv)
214525bded7cSSeth Forshee {
2146f673ceb1SKevin Cernekee 	priv->byte0 = 0x8f;
2147f673ceb1SKevin Cernekee 	priv->mask0 = 0x8f;
2148f673ceb1SKevin Cernekee 	priv->flags = ALPS_DUALPOINT;
2149f673ceb1SKevin Cernekee 
21507a9f73e7SKevin Cernekee 	priv->x_max = 2000;
21517a9f73e7SKevin Cernekee 	priv->y_max = 1400;
21527a9f73e7SKevin Cernekee 	priv->x_bits = 15;
21537a9f73e7SKevin Cernekee 	priv->y_bits = 11;
21547a9f73e7SKevin Cernekee 
215599df65e7SKevin Cernekee 	switch (priv->proto_version) {
215625bded7cSSeth Forshee 	case ALPS_PROTO_V1:
215725bded7cSSeth Forshee 	case ALPS_PROTO_V2:
215824af5cb9SKevin Cernekee 		priv->hw_init = alps_hw_init_v1_v2;
215924af5cb9SKevin Cernekee 		priv->process_packet = alps_process_packet_v1_v2;
216024af5cb9SKevin Cernekee 		priv->set_abs_params = alps_set_abs_params_st;
216195f75e91SYunkang Tang 		priv->x_max = 1023;
216295f75e91SYunkang Tang 		priv->y_max = 767;
216325bded7cSSeth Forshee 		break;
216425bded7cSSeth Forshee 	case ALPS_PROTO_V3:
216524af5cb9SKevin Cernekee 		priv->hw_init = alps_hw_init_v3;
216624af5cb9SKevin Cernekee 		priv->process_packet = alps_process_packet_v3;
216724af5cb9SKevin Cernekee 		priv->set_abs_params = alps_set_abs_params_mt;
2168f85e5001SKevin Cernekee 		priv->decode_fields = alps_decode_pinnacle;
216950e8b216SKevin Cernekee 		priv->nibble_commands = alps_v3_nibble_commands;
217050e8b216SKevin Cernekee 		priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
217125bded7cSSeth Forshee 		break;
217225bded7cSSeth Forshee 	case ALPS_PROTO_V4:
217324af5cb9SKevin Cernekee 		priv->hw_init = alps_hw_init_v4;
217424af5cb9SKevin Cernekee 		priv->process_packet = alps_process_packet_v4;
217524af5cb9SKevin Cernekee 		priv->set_abs_params = alps_set_abs_params_mt;
217650e8b216SKevin Cernekee 		priv->nibble_commands = alps_v4_nibble_commands;
217750e8b216SKevin Cernekee 		priv->addr_command = PSMOUSE_CMD_DISABLE;
217825bded7cSSeth Forshee 		break;
217975af9e56SDave Turvene 	case ALPS_PROTO_V5:
218075af9e56SDave Turvene 		priv->hw_init = alps_hw_init_dolphin_v1;
2181ee65d4b3SYunkang Tang 		priv->process_packet = alps_process_touchpad_packet_v3_v5;
218275af9e56SDave Turvene 		priv->decode_fields = alps_decode_dolphin;
218375af9e56SDave Turvene 		priv->set_abs_params = alps_set_abs_params_mt;
218475af9e56SDave Turvene 		priv->nibble_commands = alps_v3_nibble_commands;
218575af9e56SDave Turvene 		priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
218675af9e56SDave Turvene 		priv->byte0 = 0xc8;
2187ee65d4b3SYunkang Tang 		priv->mask0 = 0xd8;
218875af9e56SDave Turvene 		priv->flags = 0;
218975af9e56SDave Turvene 		priv->x_max = 1360;
219075af9e56SDave Turvene 		priv->y_max = 660;
219175af9e56SDave Turvene 		priv->x_bits = 23;
219275af9e56SDave Turvene 		priv->y_bits = 12;
219375af9e56SDave Turvene 		break;
219495f75e91SYunkang Tang 	case ALPS_PROTO_V6:
219595f75e91SYunkang Tang 		priv->hw_init = alps_hw_init_v6;
219695f75e91SYunkang Tang 		priv->process_packet = alps_process_packet_v6;
219795f75e91SYunkang Tang 		priv->set_abs_params = alps_set_abs_params_st;
219895f75e91SYunkang Tang 		priv->nibble_commands = alps_v6_nibble_commands;
219995f75e91SYunkang Tang 		priv->x_max = 2047;
220095f75e91SYunkang Tang 		priv->y_max = 1535;
220195f75e91SYunkang Tang 		break;
22023808843cSYunkang Tang 	case ALPS_PROTO_V7:
22033808843cSYunkang Tang 		priv->hw_init = alps_hw_init_v7;
22043808843cSYunkang Tang 		priv->process_packet = alps_process_packet_v7;
22053808843cSYunkang Tang 		priv->decode_fields = alps_decode_packet_v7;
22063808843cSYunkang Tang 		priv->set_abs_params = alps_set_abs_params_mt;
22073808843cSYunkang Tang 		priv->nibble_commands = alps_v3_nibble_commands;
22083808843cSYunkang Tang 		priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
22093808843cSYunkang Tang 		priv->x_max = 0xfff;
22103808843cSYunkang Tang 		priv->y_max = 0x7ff;
22113808843cSYunkang Tang 		priv->byte0 = 0x48;
22123808843cSYunkang Tang 		priv->mask0 = 0x48;
22133808843cSYunkang Tang 
22143808843cSYunkang Tang 		if (priv->fw_ver[1] != 0xba)
22153808843cSYunkang Tang 			priv->flags |= ALPS_BUTTONPAD;
22163808843cSYunkang Tang 		break;
221725bded7cSSeth Forshee 	}
221825bded7cSSeth Forshee }
221925bded7cSSeth Forshee 
2220b5d6b851SKevin Cernekee static int alps_match_table(struct psmouse *psmouse, struct alps_data *priv,
2221b5d6b851SKevin Cernekee 			    unsigned char *e7, unsigned char *ec)
22222e992cc0SKevin Cernekee {
2223b5d6b851SKevin Cernekee 	const struct alps_model_info *model;
22242e992cc0SKevin Cernekee 	int i;
22252e992cc0SKevin Cernekee 
2226b5d6b851SKevin Cernekee 	for (i = 0; i < ARRAY_SIZE(alps_model_data); i++) {
2227b5d6b851SKevin Cernekee 		model = &alps_model_data[i];
2228b5d6b851SKevin Cernekee 
2229b5d6b851SKevin Cernekee 		if (!memcmp(e7, model->signature, sizeof(model->signature)) &&
2230b5d6b851SKevin Cernekee 		    (!model->command_mode_resp ||
2231b5d6b851SKevin Cernekee 		     model->command_mode_resp == ec[2])) {
2232b5d6b851SKevin Cernekee 
2233b5d6b851SKevin Cernekee 			priv->proto_version = model->proto_version;
223424af5cb9SKevin Cernekee 			alps_set_defaults(priv);
223524af5cb9SKevin Cernekee 
2236b5d6b851SKevin Cernekee 			priv->flags = model->flags;
2237b5d6b851SKevin Cernekee 			priv->byte0 = model->byte0;
2238b5d6b851SKevin Cernekee 			priv->mask0 = model->mask0;
2239b5d6b851SKevin Cernekee 
2240b5d6b851SKevin Cernekee 			return 0;
2241b5d6b851SKevin Cernekee 		}
2242b5d6b851SKevin Cernekee 	}
2243b5d6b851SKevin Cernekee 
2244b5d6b851SKevin Cernekee 	return -EINVAL;
2245b5d6b851SKevin Cernekee }
2246b5d6b851SKevin Cernekee 
2247b5d6b851SKevin Cernekee static int alps_identify(struct psmouse *psmouse, struct alps_data *priv)
2248b5d6b851SKevin Cernekee {
2249b5d6b851SKevin Cernekee 	unsigned char e6[4], e7[4], ec[4];
2250b5d6b851SKevin Cernekee 
22512e992cc0SKevin Cernekee 	/*
22522e992cc0SKevin Cernekee 	 * First try "E6 report".
22532e992cc0SKevin Cernekee 	 * ALPS should return 0,0,10 or 0,0,100 if no buttons are pressed.
22542e992cc0SKevin Cernekee 	 * The bits 0-2 of the first byte will be 1s if some buttons are
22552e992cc0SKevin Cernekee 	 * pressed.
22562e992cc0SKevin Cernekee 	 */
2257b5d6b851SKevin Cernekee 	if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2258b5d6b851SKevin Cernekee 			 PSMOUSE_CMD_SETSCALE11, e6))
2259b5d6b851SKevin Cernekee 		return -EIO;
22602e992cc0SKevin Cernekee 
2261b5d6b851SKevin Cernekee 	if ((e6[0] & 0xf8) != 0 || e6[1] != 0 || (e6[2] != 10 && e6[2] != 100))
2262b5d6b851SKevin Cernekee 		return -EINVAL;
22632e992cc0SKevin Cernekee 
22642e992cc0SKevin Cernekee 	/*
2265b5d6b851SKevin Cernekee 	 * Now get the "E7" and "EC" reports.  These will uniquely identify
2266b5d6b851SKevin Cernekee 	 * most ALPS touchpads.
22672e992cc0SKevin Cernekee 	 */
2268b5d6b851SKevin Cernekee 	if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2269b5d6b851SKevin Cernekee 			 PSMOUSE_CMD_SETSCALE21, e7) ||
2270b5d6b851SKevin Cernekee 	    alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2271b5d6b851SKevin Cernekee 			 PSMOUSE_CMD_RESET_WRAP, ec) ||
2272b5d6b851SKevin Cernekee 	    alps_exit_command_mode(psmouse))
2273b5d6b851SKevin Cernekee 		return -EIO;
22742e992cc0SKevin Cernekee 
2275c0cd17f6SHans de Goede 	/* Save the Firmware version */
2276c0cd17f6SHans de Goede 	memcpy(priv->fw_ver, ec, 3);
2277c0cd17f6SHans de Goede 
2278f673ceb1SKevin Cernekee 	if (alps_match_table(psmouse, priv, e7, ec) == 0) {
2279b5d6b851SKevin Cernekee 		return 0;
228075af9e56SDave Turvene 	} else if (e7[0] == 0x73 && e7[1] == 0x03 && e7[2] == 0x50 &&
2281ee65d4b3SYunkang Tang 		   ec[0] == 0x73 && (ec[1] == 0x01 || ec[1] == 0x02)) {
228275af9e56SDave Turvene 		priv->proto_version = ALPS_PROTO_V5;
228375af9e56SDave Turvene 		alps_set_defaults(priv);
2284ee65d4b3SYunkang Tang 		if (alps_dolphin_get_device_area(psmouse, priv))
2285ee65d4b3SYunkang Tang 			return -EIO;
2286ee65d4b3SYunkang Tang 		else
228775af9e56SDave Turvene 			return 0;
22883808843cSYunkang Tang 	} else if (ec[0] == 0x88 &&
22893808843cSYunkang Tang 		   ((ec[1] & 0xf0) == 0xb0 || (ec[1] & 0xf0) == 0xc0)) {
22903808843cSYunkang Tang 		priv->proto_version = ALPS_PROTO_V7;
22913808843cSYunkang Tang 		alps_set_defaults(priv);
22923808843cSYunkang Tang 
22933808843cSYunkang Tang 		return 0;
22941302bac3SKevin Cernekee 	} else if (ec[0] == 0x88 && ec[1] == 0x08) {
22951302bac3SKevin Cernekee 		priv->proto_version = ALPS_PROTO_V3;
22961302bac3SKevin Cernekee 		alps_set_defaults(priv);
22971302bac3SKevin Cernekee 
22981302bac3SKevin Cernekee 		priv->hw_init = alps_hw_init_rushmore_v3;
22991302bac3SKevin Cernekee 		priv->decode_fields = alps_decode_rushmore;
23001302bac3SKevin Cernekee 		priv->x_bits = 16;
23011302bac3SKevin Cernekee 		priv->y_bits = 12;
230240e8f53bSHans de Goede 		priv->flags |= ALPS_IS_RUSHMORE;
23031302bac3SKevin Cernekee 
2304cd401204SKevin Cernekee 		/* hack to make addr_command, nibble_command available */
2305cd401204SKevin Cernekee 		psmouse->private = priv;
2306cd401204SKevin Cernekee 
2307cd401204SKevin Cernekee 		if (alps_probe_trackstick_v3(psmouse, ALPS_REG_BASE_RUSHMORE))
2308cd401204SKevin Cernekee 			priv->flags &= ~ALPS_DUALPOINT;
2309cd401204SKevin Cernekee 
23101302bac3SKevin Cernekee 		return 0;
2311f673ceb1SKevin Cernekee 	} else if (ec[0] == 0x88 && ec[1] == 0x07 &&
2312f673ceb1SKevin Cernekee 		   ec[2] >= 0x90 && ec[2] <= 0x9d) {
2313f673ceb1SKevin Cernekee 		priv->proto_version = ALPS_PROTO_V3;
2314f673ceb1SKevin Cernekee 		alps_set_defaults(priv);
2315f673ceb1SKevin Cernekee 
2316f673ceb1SKevin Cernekee 		return 0;
2317f673ceb1SKevin Cernekee 	}
23182e992cc0SKevin Cernekee 
2319a9e06219SDmitry Torokhov 	psmouse_dbg(psmouse,
2320a9e06219SDmitry Torokhov 		    "Likely not an ALPS touchpad: E7=%3ph, EC=%3ph\n", e7, ec);
23212e992cc0SKevin Cernekee 
2322b5d6b851SKevin Cernekee 	return -EINVAL;
23232e992cc0SKevin Cernekee }
23242e992cc0SKevin Cernekee 
23251e0c5b12SDmitry Torokhov static int alps_reconnect(struct psmouse *psmouse)
23261e0c5b12SDmitry Torokhov {
2327b5d6b851SKevin Cernekee 	struct alps_data *priv = psmouse->private;
232871bb21b6SMaxim Levitsky 
23291e0c5b12SDmitry Torokhov 	psmouse_reset(psmouse);
23301e0c5b12SDmitry Torokhov 
2331b5d6b851SKevin Cernekee 	if (alps_identify(psmouse, priv) < 0)
23321e0c5b12SDmitry Torokhov 		return -1;
23331e0c5b12SDmitry Torokhov 
233424af5cb9SKevin Cernekee 	return priv->hw_init(psmouse);
23351da177e4SLinus Torvalds }
23361da177e4SLinus Torvalds 
23371da177e4SLinus Torvalds static void alps_disconnect(struct psmouse *psmouse)
23381da177e4SLinus Torvalds {
23391da177e4SLinus Torvalds 	struct alps_data *priv = psmouse->private;
23402e5b636bSDmitry Torokhov 
23411da177e4SLinus Torvalds 	psmouse_reset(psmouse);
23421d9f2626SSebastian Kapfer 	del_timer_sync(&priv->timer);
23432e5b636bSDmitry Torokhov 	input_unregister_device(priv->dev2);
23441da177e4SLinus Torvalds 	kfree(priv);
23451da177e4SLinus Torvalds }
23461da177e4SLinus Torvalds 
234724af5cb9SKevin Cernekee static void alps_set_abs_params_st(struct alps_data *priv,
234824af5cb9SKevin Cernekee 				   struct input_dev *dev1)
234924af5cb9SKevin Cernekee {
235095f75e91SYunkang Tang 	input_set_abs_params(dev1, ABS_X, 0, priv->x_max, 0, 0);
235195f75e91SYunkang Tang 	input_set_abs_params(dev1, ABS_Y, 0, priv->y_max, 0, 0);
235224af5cb9SKevin Cernekee }
235324af5cb9SKevin Cernekee 
235424af5cb9SKevin Cernekee static void alps_set_abs_params_mt(struct alps_data *priv,
235524af5cb9SKevin Cernekee 				   struct input_dev *dev1)
235624af5cb9SKevin Cernekee {
23577a9f73e7SKevin Cernekee 	input_set_abs_params(dev1, ABS_MT_POSITION_X, 0, priv->x_max, 0, 0);
23587a9f73e7SKevin Cernekee 	input_set_abs_params(dev1, ABS_MT_POSITION_Y, 0, priv->y_max, 0, 0);
235924af5cb9SKevin Cernekee 
2360f3f33c67SHans de Goede 	input_abs_set_res(dev1, ABS_MT_POSITION_X, priv->x_res);
2361f3f33c67SHans de Goede 	input_abs_set_res(dev1, ABS_MT_POSITION_Y, priv->y_res);
2362f3f33c67SHans de Goede 
2363cdf333efSHans de Goede 	input_mt_init_slots(dev1, MAX_TOUCHES, INPUT_MT_POINTER |
2364cdf333efSHans de Goede 		INPUT_MT_DROP_UNUSED | INPUT_MT_TRACK | INPUT_MT_SEMI_MT);
2365cdf333efSHans de Goede 
236624af5cb9SKevin Cernekee 	set_bit(BTN_TOOL_TRIPLETAP, dev1->keybit);
236724af5cb9SKevin Cernekee 	set_bit(BTN_TOOL_QUADTAP, dev1->keybit);
23683808843cSYunkang Tang 
23693808843cSYunkang Tang 	/* V7 is real multi-touch */
23703808843cSYunkang Tang 	if (priv->proto_version == ALPS_PROTO_V7)
23713808843cSYunkang Tang 		clear_bit(INPUT_PROP_SEMI_MT, dev1->propbit);
237224af5cb9SKevin Cernekee }
237324af5cb9SKevin Cernekee 
23741da177e4SLinus Torvalds int alps_init(struct psmouse *psmouse)
23751da177e4SLinus Torvalds {
23761da177e4SLinus Torvalds 	struct alps_data *priv;
23772e5b636bSDmitry Torokhov 	struct input_dev *dev1 = psmouse->dev, *dev2;
23781da177e4SLinus Torvalds 
2379f42649e8SDmitry Torokhov 	priv = kzalloc(sizeof(struct alps_data), GFP_KERNEL);
23802e5b636bSDmitry Torokhov 	dev2 = input_allocate_device();
23812e5b636bSDmitry Torokhov 	if (!priv || !dev2)
23821da177e4SLinus Torvalds 		goto init_fail;
23832e5b636bSDmitry Torokhov 
23842e5b636bSDmitry Torokhov 	priv->dev2 = dev2;
23851d9f2626SSebastian Kapfer 	setup_timer(&priv->timer, alps_flush_packet, (unsigned long)psmouse);
23861d9f2626SSebastian Kapfer 
23871e0c5b12SDmitry Torokhov 	psmouse->private = priv;
23881da177e4SLinus Torvalds 
238925bded7cSSeth Forshee 	psmouse_reset(psmouse);
239025bded7cSSeth Forshee 
2391b5d6b851SKevin Cernekee 	if (alps_identify(psmouse, priv) < 0)
239271bb21b6SMaxim Levitsky 		goto init_fail;
239371bb21b6SMaxim Levitsky 
239424af5cb9SKevin Cernekee 	if (priv->hw_init(psmouse))
23951da177e4SLinus Torvalds 		goto init_fail;
23961da177e4SLinus Torvalds 
23977105d2eaSDmitry Torokhov 	/*
23987105d2eaSDmitry Torokhov 	 * Undo part of setup done for us by psmouse core since touchpad
23997105d2eaSDmitry Torokhov 	 * is not a relative device.
24007105d2eaSDmitry Torokhov 	 */
24017105d2eaSDmitry Torokhov 	__clear_bit(EV_REL, dev1->evbit);
24027105d2eaSDmitry Torokhov 	__clear_bit(REL_X, dev1->relbit);
24037105d2eaSDmitry Torokhov 	__clear_bit(REL_Y, dev1->relbit);
24047105d2eaSDmitry Torokhov 
24057105d2eaSDmitry Torokhov 	/*
24067105d2eaSDmitry Torokhov 	 * Now set up our capabilities.
24077105d2eaSDmitry Torokhov 	 */
24087b19ada2SJiri Slaby 	dev1->evbit[BIT_WORD(EV_KEY)] |= BIT_MASK(EV_KEY);
24097b19ada2SJiri Slaby 	dev1->keybit[BIT_WORD(BTN_TOUCH)] |= BIT_MASK(BTN_TOUCH);
24107b19ada2SJiri Slaby 	dev1->keybit[BIT_WORD(BTN_TOOL_FINGER)] |= BIT_MASK(BTN_TOOL_FINGER);
241171bb21b6SMaxim Levitsky 	dev1->keybit[BIT_WORD(BTN_LEFT)] |=
241271bb21b6SMaxim Levitsky 		BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
24131da177e4SLinus Torvalds 
24147b19ada2SJiri Slaby 	dev1->evbit[BIT_WORD(EV_ABS)] |= BIT_MASK(EV_ABS);
241525bded7cSSeth Forshee 
241624af5cb9SKevin Cernekee 	priv->set_abs_params(priv, dev1);
24173808843cSYunkang Tang 	/* No pressure on V7 */
24183808843cSYunkang Tang 	if (priv->proto_version != ALPS_PROTO_V7)
24192e5b636bSDmitry Torokhov 		input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
24201da177e4SLinus Torvalds 
242199df65e7SKevin Cernekee 	if (priv->flags & ALPS_WHEEL) {
24227b19ada2SJiri Slaby 		dev1->evbit[BIT_WORD(EV_REL)] |= BIT_MASK(EV_REL);
24237b19ada2SJiri Slaby 		dev1->relbit[BIT_WORD(REL_WHEEL)] |= BIT_MASK(REL_WHEEL);
24241da177e4SLinus Torvalds 	}
24251da177e4SLinus Torvalds 
242699df65e7SKevin Cernekee 	if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
24277b19ada2SJiri Slaby 		dev1->keybit[BIT_WORD(BTN_FORWARD)] |= BIT_MASK(BTN_FORWARD);
24287b19ada2SJiri Slaby 		dev1->keybit[BIT_WORD(BTN_BACK)] |= BIT_MASK(BTN_BACK);
24291da177e4SLinus Torvalds 	}
24301da177e4SLinus Torvalds 
243199df65e7SKevin Cernekee 	if (priv->flags & ALPS_FOUR_BUTTONS) {
243271bb21b6SMaxim Levitsky 		dev1->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_0);
243371bb21b6SMaxim Levitsky 		dev1->keybit[BIT_WORD(BTN_1)] |= BIT_MASK(BTN_1);
243471bb21b6SMaxim Levitsky 		dev1->keybit[BIT_WORD(BTN_2)] |= BIT_MASK(BTN_2);
243571bb21b6SMaxim Levitsky 		dev1->keybit[BIT_WORD(BTN_3)] |= BIT_MASK(BTN_3);
24363808843cSYunkang Tang 	} else if (priv->flags & ALPS_BUTTONPAD) {
24373808843cSYunkang Tang 		set_bit(INPUT_PROP_BUTTONPAD, dev1->propbit);
24383808843cSYunkang Tang 		clear_bit(BTN_RIGHT, dev1->keybit);
243971bb21b6SMaxim Levitsky 	} else {
244071bb21b6SMaxim Levitsky 		dev1->keybit[BIT_WORD(BTN_MIDDLE)] |= BIT_MASK(BTN_MIDDLE);
244171bb21b6SMaxim Levitsky 	}
244271bb21b6SMaxim Levitsky 
2443dfba8600SPali Rohár 	if (priv->flags & ALPS_DUALPOINT) {
2444dfba8600SPali Rohár 		/*
2445dfba8600SPali Rohár 		 * format of input device name is: "protocol vendor name"
2446dfba8600SPali Rohár 		 * see function psmouse_switch_protocol() in psmouse-base.c
2447dfba8600SPali Rohár 		 */
2448dfba8600SPali Rohár 		dev2->name = "AlpsPS/2 ALPS DualPoint Stick";
2449dfba8600SPali Rohár 		dev2->id.product = PSMOUSE_ALPS;
2450dfba8600SPali Rohár 		dev2->id.version = priv->proto_version;
2451dfba8600SPali Rohár 	} else {
2452dfba8600SPali Rohár 		dev2->name = "PS/2 ALPS Mouse";
2453dfba8600SPali Rohár 		dev2->id.product = PSMOUSE_PS2;
2454dfba8600SPali Rohár 		dev2->id.version = 0x0000;
2455dfba8600SPali Rohár 	}
2456dfba8600SPali Rohár 
245708ffce45SDmitry Torokhov 	snprintf(priv->phys, sizeof(priv->phys), "%s/input1", psmouse->ps2dev.serio->phys);
24582e5b636bSDmitry Torokhov 	dev2->phys = priv->phys;
24592e5b636bSDmitry Torokhov 	dev2->id.bustype = BUS_I8042;
24602e5b636bSDmitry Torokhov 	dev2->id.vendor  = 0x0002;
24611db3a345SDmitry Torokhov 	dev2->dev.parent = &psmouse->ps2dev.serio->dev;
24621da177e4SLinus Torvalds 
24637b19ada2SJiri Slaby 	dev2->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
246471bb21b6SMaxim Levitsky 	dev2->relbit[BIT_WORD(REL_X)] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
246571bb21b6SMaxim Levitsky 	dev2->keybit[BIT_WORD(BTN_LEFT)] =
246671bb21b6SMaxim Levitsky 		BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_MIDDLE) | BIT_MASK(BTN_RIGHT);
24671da177e4SLinus Torvalds 
246801d4cd5cSHans de Goede 	__set_bit(INPUT_PROP_POINTER, dev2->propbit);
24697611392fSHans de Goede 	if (priv->flags & ALPS_DUALPOINT)
24707611392fSHans de Goede 		__set_bit(INPUT_PROP_POINTING_STICK, dev2->propbit);
24717611392fSHans de Goede 
2472f42649e8SDmitry Torokhov 	if (input_register_device(priv->dev2))
2473f42649e8SDmitry Torokhov 		goto init_fail;
24741da177e4SLinus Torvalds 
24751da177e4SLinus Torvalds 	psmouse->protocol_handler = alps_process_byte;
2476f0d5c6f4SDmitry Torokhov 	psmouse->poll = alps_poll;
24771da177e4SLinus Torvalds 	psmouse->disconnect = alps_disconnect;
24781da177e4SLinus Torvalds 	psmouse->reconnect = alps_reconnect;
247999df65e7SKevin Cernekee 	psmouse->pktsize = priv->proto_version == ALPS_PROTO_V4 ? 8 : 6;
24801da177e4SLinus Torvalds 
2481f0d5c6f4SDmitry Torokhov 	/* We are having trouble resyncing ALPS touchpads so disable it for now */
2482f0d5c6f4SDmitry Torokhov 	psmouse->resync_time = 0;
2483f0d5c6f4SDmitry Torokhov 
24841da177e4SLinus Torvalds 	return 0;
24851da177e4SLinus Torvalds 
24861da177e4SLinus Torvalds init_fail:
2487f42649e8SDmitry Torokhov 	psmouse_reset(psmouse);
24882e5b636bSDmitry Torokhov 	input_free_device(dev2);
24891da177e4SLinus Torvalds 	kfree(priv);
24901e0c5b12SDmitry Torokhov 	psmouse->private = NULL;
24911da177e4SLinus Torvalds 	return -1;
24921da177e4SLinus Torvalds }
24931da177e4SLinus Torvalds 
2494b7802c5cSDmitry Torokhov int alps_detect(struct psmouse *psmouse, bool set_properties)
24951da177e4SLinus Torvalds {
2496b5d6b851SKevin Cernekee 	struct alps_data dummy;
24971da177e4SLinus Torvalds 
2498b5d6b851SKevin Cernekee 	if (alps_identify(psmouse, &dummy) < 0)
24991da177e4SLinus Torvalds 		return -1;
25001da177e4SLinus Torvalds 
25011da177e4SLinus Torvalds 	if (set_properties) {
25021da177e4SLinus Torvalds 		psmouse->vendor = "ALPS";
2503b5d6b851SKevin Cernekee 		psmouse->name = dummy.flags & ALPS_DUALPOINT ?
2504968ac842SDmitry Torokhov 				"DualPoint TouchPad" : "GlidePoint";
2505b5d6b851SKevin Cernekee 		psmouse->model = dummy.proto_version << 8;
25061da177e4SLinus Torvalds 	}
25071da177e4SLinus Torvalds 	return 0;
25081da177e4SLinus Torvalds }
25091da177e4SLinus Torvalds 
2510