xref: /openbmc/linux/drivers/input/mouse/alps.c (revision fb2dd7a6)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * ALPS touchpad PS/2 mouse driver
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Copyright (c) 2003 Neil Brown <neilb@cse.unsw.edu.au>
5963f626dSPeter Osterlund  * Copyright (c) 2003-2005 Peter Osterlund <petero2@telia.com>
61da177e4SLinus Torvalds  * Copyright (c) 2004 Dmitry Torokhov <dtor@mail.ru>
71da177e4SLinus Torvalds  * Copyright (c) 2005 Vojtech Pavlik <vojtech@suse.cz>
81d9f2626SSebastian Kapfer  * Copyright (c) 2009 Sebastian Kapfer <sebastian_kapfer@gmx.net>
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  * ALPS detection, tap switching and status querying info is taken from
111da177e4SLinus Torvalds  * tpconfig utility (by C. Scott Ananian and Bruce Kall).
121da177e4SLinus Torvalds  *
131da177e4SLinus Torvalds  * This program is free software; you can redistribute it and/or modify it
141da177e4SLinus Torvalds  * under the terms of the GNU General Public License version 2 as published by
151da177e4SLinus Torvalds  * the Free Software Foundation.
161da177e4SLinus Torvalds  */
171da177e4SLinus Torvalds 
185a0e3ad6STejun Heo #include <linux/slab.h>
191da177e4SLinus Torvalds #include <linux/input.h>
2001ce661fSSeth Forshee #include <linux/input/mt.h>
211da177e4SLinus Torvalds #include <linux/serio.h>
221da177e4SLinus Torvalds #include <linux/libps2.h>
231da177e4SLinus Torvalds 
241da177e4SLinus Torvalds #include "psmouse.h"
251da177e4SLinus Torvalds #include "alps.h"
261da177e4SLinus Torvalds 
2725bded7cSSeth Forshee /*
2825bded7cSSeth Forshee  * Definitions for ALPS version 3 and 4 command mode protocol
2925bded7cSSeth Forshee  */
3025bded7cSSeth Forshee #define ALPS_CMD_NIBBLE_10	0x01f2
3125bded7cSSeth Forshee 
32cd401204SKevin Cernekee #define ALPS_REG_BASE_RUSHMORE	0xc2c0
33cd401204SKevin Cernekee #define ALPS_REG_BASE_PINNACLE	0x0000
34cd401204SKevin Cernekee 
3525bded7cSSeth Forshee static const struct alps_nibble_commands alps_v3_nibble_commands[] = {
3625bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETPOLL,		0x00 }, /* 0 */
3725bded7cSSeth Forshee 	{ PSMOUSE_CMD_RESET_DIS,	0x00 }, /* 1 */
3825bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETSCALE21,	0x00 }, /* 2 */
3925bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0x0a }, /* 3 */
4025bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0x14 }, /* 4 */
4125bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0x28 }, /* 5 */
4225bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0x3c }, /* 6 */
4325bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0x50 }, /* 7 */
4425bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0x64 }, /* 8 */
4525bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0xc8 }, /* 9 */
4625bded7cSSeth Forshee 	{ ALPS_CMD_NIBBLE_10,		0x00 }, /* a */
4725bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRES,		0x00 }, /* b */
4825bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRES,		0x01 }, /* c */
4925bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRES,		0x02 }, /* d */
5025bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRES,		0x03 }, /* e */
5125bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETSCALE11,	0x00 }, /* f */
5225bded7cSSeth Forshee };
5325bded7cSSeth Forshee 
5425bded7cSSeth Forshee static const struct alps_nibble_commands alps_v4_nibble_commands[] = {
5525bded7cSSeth Forshee 	{ PSMOUSE_CMD_ENABLE,		0x00 }, /* 0 */
5625bded7cSSeth Forshee 	{ PSMOUSE_CMD_RESET_DIS,	0x00 }, /* 1 */
5725bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETSCALE21,	0x00 }, /* 2 */
5825bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0x0a }, /* 3 */
5925bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0x14 }, /* 4 */
6025bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0x28 }, /* 5 */
6125bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0x3c }, /* 6 */
6225bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0x50 }, /* 7 */
6325bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0x64 }, /* 8 */
6425bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0xc8 }, /* 9 */
6525bded7cSSeth Forshee 	{ ALPS_CMD_NIBBLE_10,		0x00 }, /* a */
6625bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRES,		0x00 }, /* b */
6725bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRES,		0x01 }, /* c */
6825bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRES,		0x02 }, /* d */
6925bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRES,		0x03 }, /* e */
7025bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETSCALE11,	0x00 }, /* f */
7125bded7cSSeth Forshee };
7225bded7cSSeth Forshee 
7395f75e91SYunkang Tang static const struct alps_nibble_commands alps_v6_nibble_commands[] = {
7495f75e91SYunkang Tang 	{ PSMOUSE_CMD_ENABLE,		0x00 }, /* 0 */
7595f75e91SYunkang Tang 	{ PSMOUSE_CMD_SETRATE,		0x0a }, /* 1 */
7695f75e91SYunkang Tang 	{ PSMOUSE_CMD_SETRATE,		0x14 }, /* 2 */
7795f75e91SYunkang Tang 	{ PSMOUSE_CMD_SETRATE,		0x28 }, /* 3 */
7895f75e91SYunkang Tang 	{ PSMOUSE_CMD_SETRATE,		0x3c }, /* 4 */
7995f75e91SYunkang Tang 	{ PSMOUSE_CMD_SETRATE,		0x50 }, /* 5 */
8095f75e91SYunkang Tang 	{ PSMOUSE_CMD_SETRATE,		0x64 }, /* 6 */
8195f75e91SYunkang Tang 	{ PSMOUSE_CMD_SETRATE,		0xc8 }, /* 7 */
8295f75e91SYunkang Tang 	{ PSMOUSE_CMD_GETID,		0x00 }, /* 8 */
8395f75e91SYunkang Tang 	{ PSMOUSE_CMD_GETINFO,		0x00 }, /* 9 */
8495f75e91SYunkang Tang 	{ PSMOUSE_CMD_SETRES,		0x00 }, /* a */
8595f75e91SYunkang Tang 	{ PSMOUSE_CMD_SETRES,		0x01 }, /* b */
8695f75e91SYunkang Tang 	{ PSMOUSE_CMD_SETRES,		0x02 }, /* c */
8795f75e91SYunkang Tang 	{ PSMOUSE_CMD_SETRES,		0x03 }, /* d */
8895f75e91SYunkang Tang 	{ PSMOUSE_CMD_SETSCALE21,	0x00 }, /* e */
8995f75e91SYunkang Tang 	{ PSMOUSE_CMD_SETSCALE11,	0x00 }, /* f */
9095f75e91SYunkang Tang };
9195f75e91SYunkang Tang 
9225bded7cSSeth Forshee 
9371bb21b6SMaxim Levitsky #define ALPS_DUALPOINT		0x02	/* touchpad has trackstick */
9471bb21b6SMaxim Levitsky #define ALPS_PASS		0x04	/* device has a pass-through port */
9571bb21b6SMaxim Levitsky 
9671bb21b6SMaxim Levitsky #define ALPS_WHEEL		0x08	/* hardware wheel present */
9771bb21b6SMaxim Levitsky #define ALPS_FW_BK_1		0x10	/* front & back buttons present */
9871bb21b6SMaxim Levitsky #define ALPS_FW_BK_2		0x20	/* front & back buttons present */
9971bb21b6SMaxim Levitsky #define ALPS_FOUR_BUTTONS	0x40	/* 4 direction button present */
1001d9f2626SSebastian Kapfer #define ALPS_PS2_INTERLEAVED	0x80	/* 3-byte PS/2 packet interleaved with
1011d9f2626SSebastian Kapfer 					   6-byte ALPS packet */
1023808843cSYunkang Tang #define ALPS_BUTTONPAD		0x200	/* device is a clickpad */
1031da177e4SLinus Torvalds 
104e38de678SHelge Deller static const struct alps_model_info alps_model_data[] = {
10525bded7cSSeth Forshee 	{ { 0x32, 0x02, 0x14 },	0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT },	/* Toshiba Salellite Pro M10 */
10625bded7cSSeth Forshee 	{ { 0x33, 0x02, 0x0a },	0x00, ALPS_PROTO_V1, 0x88, 0xf8, 0 },				/* UMAX-530T */
10725bded7cSSeth Forshee 	{ { 0x53, 0x02, 0x0a },	0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
10825bded7cSSeth Forshee 	{ { 0x53, 0x02, 0x14 },	0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
10925bded7cSSeth Forshee 	{ { 0x60, 0x03, 0xc8 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },				/* HP ze1115 */
11025bded7cSSeth Forshee 	{ { 0x63, 0x02, 0x0a },	0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
11125bded7cSSeth Forshee 	{ { 0x63, 0x02, 0x14 },	0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
11225bded7cSSeth Forshee 	{ { 0x63, 0x02, 0x28 },	0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 },		/* Fujitsu Siemens S6010 */
11325bded7cSSeth Forshee 	{ { 0x63, 0x02, 0x3c },	0x00, ALPS_PROTO_V2, 0x8f, 0x8f, ALPS_WHEEL },			/* Toshiba Satellite S2400-103 */
11425bded7cSSeth Forshee 	{ { 0x63, 0x02, 0x50 },	0x00, ALPS_PROTO_V2, 0xef, 0xef, ALPS_FW_BK_1 },		/* NEC Versa L320 */
11525bded7cSSeth Forshee 	{ { 0x63, 0x02, 0x64 },	0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
11625bded7cSSeth Forshee 	{ { 0x63, 0x03, 0xc8 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT },	/* Dell Latitude D800 */
11725bded7cSSeth Forshee 	{ { 0x73, 0x00, 0x0a },	0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_DUALPOINT },		/* ThinkPad R61 8918-5QG */
11825bded7cSSeth Forshee 	{ { 0x73, 0x02, 0x0a },	0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
11925bded7cSSeth Forshee 	{ { 0x73, 0x02, 0x14 },	0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 },		/* Ahtec Laptop */
12025bded7cSSeth Forshee 	{ { 0x20, 0x02, 0x0e },	0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT },	/* XXX */
12125bded7cSSeth Forshee 	{ { 0x22, 0x02, 0x0a },	0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT },
12225bded7cSSeth Forshee 	{ { 0x22, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xff, 0xff, ALPS_PASS | ALPS_DUALPOINT },	/* Dell Latitude D600 */
1231d9f2626SSebastian Kapfer 	/* Dell Latitude E5500, E6400, E6500, Precision M4400 */
12425bded7cSSeth Forshee 	{ { 0x62, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xcf, 0xcf,
1251d9f2626SSebastian Kapfer 		ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED },
12695f75e91SYunkang Tang 	{ { 0x73, 0x00, 0x14 }, 0x00, ALPS_PROTO_V6, 0xff, 0xff, ALPS_DUALPOINT },		/* Dell XT2 */
12725bded7cSSeth Forshee 	{ { 0x73, 0x02, 0x50 }, 0x00, ALPS_PROTO_V2, 0xcf, 0xcf, ALPS_FOUR_BUTTONS },		/* Dell Vostro 1400 */
12825bded7cSSeth Forshee 	{ { 0x52, 0x01, 0x14 }, 0x00, ALPS_PROTO_V2, 0xff, 0xff,
129eb8bff85SThomas Bächler 		ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED },				/* Toshiba Tecra A11-11L */
13025bded7cSSeth Forshee 	{ { 0x73, 0x02, 0x64 },	0x8a, ALPS_PROTO_V4, 0x8f, 0x8f, 0 },
1311da177e4SLinus Torvalds };
1321da177e4SLinus Torvalds 
13324af5cb9SKevin Cernekee static void alps_set_abs_params_st(struct alps_data *priv,
13424af5cb9SKevin Cernekee 				   struct input_dev *dev1);
13524af5cb9SKevin Cernekee static void alps_set_abs_params_mt(struct alps_data *priv,
13624af5cb9SKevin Cernekee 				   struct input_dev *dev1);
13724af5cb9SKevin Cernekee 
1381da177e4SLinus Torvalds /*
1391da177e4SLinus Torvalds  * XXX - this entry is suspicious. First byte has zero lower nibble,
1401da177e4SLinus Torvalds  * which is what a normal mouse would report. Also, the value 0x0e
1411da177e4SLinus Torvalds  * isn't valid per PS/2 spec.
1421da177e4SLinus Torvalds  */
1431da177e4SLinus Torvalds 
144d4b347b2SSeth Forshee /* Packet formats are described in Documentation/input/alps.txt */
1451da177e4SLinus Torvalds 
14699df65e7SKevin Cernekee static bool alps_is_valid_first_byte(struct alps_data *priv,
1471d9f2626SSebastian Kapfer 				     unsigned char data)
1481d9f2626SSebastian Kapfer {
14999df65e7SKevin Cernekee 	return (data & priv->mask0) == priv->byte0;
1501d9f2626SSebastian Kapfer }
1511d9f2626SSebastian Kapfer 
1521d9f2626SSebastian Kapfer static void alps_report_buttons(struct psmouse *psmouse,
1531d9f2626SSebastian Kapfer 				struct input_dev *dev1, struct input_dev *dev2,
1541d9f2626SSebastian Kapfer 				int left, int right, int middle)
1551d9f2626SSebastian Kapfer {
1561d9f2626SSebastian Kapfer 	struct input_dev *dev;
1571d9f2626SSebastian Kapfer 
1581d9f2626SSebastian Kapfer 	/*
1591d9f2626SSebastian Kapfer 	 * If shared button has already been reported on the
1601d9f2626SSebastian Kapfer 	 * other device (dev2) then this event should be also
1611d9f2626SSebastian Kapfer 	 * sent through that device.
1621d9f2626SSebastian Kapfer 	 */
1631d9f2626SSebastian Kapfer 	dev = test_bit(BTN_LEFT, dev2->key) ? dev2 : dev1;
1641d9f2626SSebastian Kapfer 	input_report_key(dev, BTN_LEFT, left);
1651d9f2626SSebastian Kapfer 
1661d9f2626SSebastian Kapfer 	dev = test_bit(BTN_RIGHT, dev2->key) ? dev2 : dev1;
1671d9f2626SSebastian Kapfer 	input_report_key(dev, BTN_RIGHT, right);
1681d9f2626SSebastian Kapfer 
1691d9f2626SSebastian Kapfer 	dev = test_bit(BTN_MIDDLE, dev2->key) ? dev2 : dev1;
1701d9f2626SSebastian Kapfer 	input_report_key(dev, BTN_MIDDLE, middle);
1711d9f2626SSebastian Kapfer 
1721d9f2626SSebastian Kapfer 	/*
1731d9f2626SSebastian Kapfer 	 * Sync the _other_ device now, we'll do the first
1741d9f2626SSebastian Kapfer 	 * device later once we report the rest of the events.
1751d9f2626SSebastian Kapfer 	 */
1761d9f2626SSebastian Kapfer 	input_sync(dev2);
1771d9f2626SSebastian Kapfer }
1781d9f2626SSebastian Kapfer 
17925bded7cSSeth Forshee static void alps_process_packet_v1_v2(struct psmouse *psmouse)
1801da177e4SLinus Torvalds {
1811da177e4SLinus Torvalds 	struct alps_data *priv = psmouse->private;
1821da177e4SLinus Torvalds 	unsigned char *packet = psmouse->packet;
1832e5b636bSDmitry Torokhov 	struct input_dev *dev = psmouse->dev;
1842e5b636bSDmitry Torokhov 	struct input_dev *dev2 = priv->dev2;
1851da177e4SLinus Torvalds 	int x, y, z, ges, fin, left, right, middle;
186c30b4c10SIvan Casado Ruiz 	int back = 0, forward = 0;
1871da177e4SLinus Torvalds 
18899df65e7SKevin Cernekee 	if (priv->proto_version == ALPS_PROTO_V1) {
189d2f4012fSYotam Medini 		left = packet[2] & 0x10;
190d2f4012fSYotam Medini 		right = packet[2] & 0x08;
1911da177e4SLinus Torvalds 		middle = 0;
1921da177e4SLinus Torvalds 		x = packet[1] | ((packet[0] & 0x07) << 7);
1931da177e4SLinus Torvalds 		y = packet[4] | ((packet[3] & 0x07) << 7);
1941da177e4SLinus Torvalds 		z = packet[5];
1951da177e4SLinus Torvalds 	} else {
1961da177e4SLinus Torvalds 		left = packet[3] & 1;
1971da177e4SLinus Torvalds 		right = packet[3] & 2;
1981da177e4SLinus Torvalds 		middle = packet[3] & 4;
1991da177e4SLinus Torvalds 		x = packet[1] | ((packet[2] & 0x78) << (7 - 3));
2001da177e4SLinus Torvalds 		y = packet[4] | ((packet[3] & 0x70) << (7 - 4));
2011da177e4SLinus Torvalds 		z = packet[5];
2021da177e4SLinus Torvalds 	}
2031da177e4SLinus Torvalds 
20499df65e7SKevin Cernekee 	if (priv->flags & ALPS_FW_BK_1) {
2053c00bb96SLaszlo Kajan 		back = packet[0] & 0x10;
2063c00bb96SLaszlo Kajan 		forward = packet[2] & 4;
207c30b4c10SIvan Casado Ruiz 	}
208c30b4c10SIvan Casado Ruiz 
20999df65e7SKevin Cernekee 	if (priv->flags & ALPS_FW_BK_2) {
210c30b4c10SIvan Casado Ruiz 		back = packet[3] & 4;
211c30b4c10SIvan Casado Ruiz 		forward = packet[2] & 4;
212c30b4c10SIvan Casado Ruiz 		if ((middle = forward && back))
213c30b4c10SIvan Casado Ruiz 			forward = back = 0;
214c30b4c10SIvan Casado Ruiz 	}
215c30b4c10SIvan Casado Ruiz 
2161da177e4SLinus Torvalds 	ges = packet[2] & 1;
2171da177e4SLinus Torvalds 	fin = packet[2] & 2;
2181da177e4SLinus Torvalds 
21999df65e7SKevin Cernekee 	if ((priv->flags & ALPS_DUALPOINT) && z == 127) {
2201da177e4SLinus Torvalds 		input_report_rel(dev2, REL_X,  (x > 383 ? (x - 768) : x));
2211da177e4SLinus Torvalds 		input_report_rel(dev2, REL_Y, -(y > 255 ? (y - 512) : y));
222d7ed5d88SUlrich Dangel 
2231d9f2626SSebastian Kapfer 		alps_report_buttons(psmouse, dev2, dev, left, right, middle);
224d7ed5d88SUlrich Dangel 
2251da177e4SLinus Torvalds 		input_sync(dev2);
2261da177e4SLinus Torvalds 		return;
2271da177e4SLinus Torvalds 	}
2281da177e4SLinus Torvalds 
2291d9f2626SSebastian Kapfer 	alps_report_buttons(psmouse, dev, dev2, left, right, middle);
230d7ed5d88SUlrich Dangel 
2311da177e4SLinus Torvalds 	/* Convert hardware tap to a reasonable Z value */
23271bb21b6SMaxim Levitsky 	if (ges && !fin)
23371bb21b6SMaxim Levitsky 		z = 40;
2341da177e4SLinus Torvalds 
2351da177e4SLinus Torvalds 	/*
2361da177e4SLinus Torvalds 	 * A "tap and drag" operation is reported by the hardware as a transition
2371da177e4SLinus Torvalds 	 * from (!fin && ges) to (fin && ges). This should be translated to the
2381da177e4SLinus Torvalds 	 * sequence Z>0, Z==0, Z>0, so the Z==0 event has to be generated manually.
2391da177e4SLinus Torvalds 	 */
2401da177e4SLinus Torvalds 	if (ges && fin && !priv->prev_fin) {
2411da177e4SLinus Torvalds 		input_report_abs(dev, ABS_X, x);
2421da177e4SLinus Torvalds 		input_report_abs(dev, ABS_Y, y);
2431da177e4SLinus Torvalds 		input_report_abs(dev, ABS_PRESSURE, 0);
2441da177e4SLinus Torvalds 		input_report_key(dev, BTN_TOOL_FINGER, 0);
2451da177e4SLinus Torvalds 		input_sync(dev);
2461da177e4SLinus Torvalds 	}
2471da177e4SLinus Torvalds 	priv->prev_fin = fin;
2481da177e4SLinus Torvalds 
24971bb21b6SMaxim Levitsky 	if (z > 30)
25071bb21b6SMaxim Levitsky 		input_report_key(dev, BTN_TOUCH, 1);
25171bb21b6SMaxim Levitsky 	if (z < 25)
25271bb21b6SMaxim Levitsky 		input_report_key(dev, BTN_TOUCH, 0);
2531da177e4SLinus Torvalds 
2541da177e4SLinus Torvalds 	if (z > 0) {
2551da177e4SLinus Torvalds 		input_report_abs(dev, ABS_X, x);
2561da177e4SLinus Torvalds 		input_report_abs(dev, ABS_Y, y);
2571da177e4SLinus Torvalds 	}
2581da177e4SLinus Torvalds 
2591da177e4SLinus Torvalds 	input_report_abs(dev, ABS_PRESSURE, z);
2601da177e4SLinus Torvalds 	input_report_key(dev, BTN_TOOL_FINGER, z > 0);
2611da177e4SLinus Torvalds 
26299df65e7SKevin Cernekee 	if (priv->flags & ALPS_WHEEL)
263e6c047b9SVojtech Pavlik 		input_report_rel(dev, REL_WHEEL, ((packet[2] << 1) & 0x08) - ((packet[0] >> 4) & 0x07));
2641da177e4SLinus Torvalds 
26599df65e7SKevin Cernekee 	if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
266c30b4c10SIvan Casado Ruiz 		input_report_key(dev, BTN_FORWARD, forward);
267c30b4c10SIvan Casado Ruiz 		input_report_key(dev, BTN_BACK, back);
2681da177e4SLinus Torvalds 	}
2691da177e4SLinus Torvalds 
27099df65e7SKevin Cernekee 	if (priv->flags & ALPS_FOUR_BUTTONS) {
27171bb21b6SMaxim Levitsky 		input_report_key(dev, BTN_0, packet[2] & 4);
27271bb21b6SMaxim Levitsky 		input_report_key(dev, BTN_1, packet[0] & 0x10);
27371bb21b6SMaxim Levitsky 		input_report_key(dev, BTN_2, packet[3] & 4);
27471bb21b6SMaxim Levitsky 		input_report_key(dev, BTN_3, packet[0] & 0x20);
27571bb21b6SMaxim Levitsky 	}
27671bb21b6SMaxim Levitsky 
2771da177e4SLinus Torvalds 	input_sync(dev);
2781da177e4SLinus Torvalds }
2791da177e4SLinus Torvalds 
28001ce661fSSeth Forshee /*
281ee65d4b3SYunkang Tang  * Process bitmap data for V5 protocols. Return value is null.
282ee65d4b3SYunkang Tang  *
283ee65d4b3SYunkang Tang  * The bitmaps don't have enough data to track fingers, so this function
284ee65d4b3SYunkang Tang  * only generates points representing a bounding box of at most two contacts.
28502d04254SHans de Goede  * These two points are returned in fields->mt.
286ee65d4b3SYunkang Tang  */
287ee65d4b3SYunkang Tang static void alps_process_bitmap_dolphin(struct alps_data *priv,
28802d04254SHans de Goede 					struct alps_fields *fields)
289ee65d4b3SYunkang Tang {
290ee65d4b3SYunkang Tang 	int box_middle_x, box_middle_y;
291ee65d4b3SYunkang Tang 	unsigned int x_map, y_map;
292ee65d4b3SYunkang Tang 	unsigned char start_bit, end_bit;
293ee65d4b3SYunkang Tang 	unsigned char x_msb, x_lsb, y_msb, y_lsb;
294ee65d4b3SYunkang Tang 
295ee65d4b3SYunkang Tang 	x_map = fields->x_map;
296ee65d4b3SYunkang Tang 	y_map = fields->y_map;
297ee65d4b3SYunkang Tang 
298ee65d4b3SYunkang Tang 	if (!x_map || !y_map)
299ee65d4b3SYunkang Tang 		return;
300ee65d4b3SYunkang Tang 
301ee65d4b3SYunkang Tang 	/* Get Most-significant and Least-significant bit */
302ee65d4b3SYunkang Tang 	x_msb = fls(x_map);
303ee65d4b3SYunkang Tang 	x_lsb = ffs(x_map);
304ee65d4b3SYunkang Tang 	y_msb = fls(y_map);
305ee65d4b3SYunkang Tang 	y_lsb = ffs(y_map);
306ee65d4b3SYunkang Tang 
307ee65d4b3SYunkang Tang 	/* Most-significant bit should never exceed max sensor line number */
308ee65d4b3SYunkang Tang 	if (x_msb > priv->x_bits || y_msb > priv->y_bits)
309ee65d4b3SYunkang Tang 		return;
310ee65d4b3SYunkang Tang 
311ee65d4b3SYunkang Tang 	if (fields->fingers > 1) {
312ee65d4b3SYunkang Tang 		start_bit = priv->x_bits - x_msb;
313ee65d4b3SYunkang Tang 		end_bit = priv->x_bits - x_lsb;
314ee65d4b3SYunkang Tang 		box_middle_x = (priv->x_max * (start_bit + end_bit)) /
315ee65d4b3SYunkang Tang 				(2 * (priv->x_bits - 1));
316ee65d4b3SYunkang Tang 
317ee65d4b3SYunkang Tang 		start_bit = y_lsb - 1;
318ee65d4b3SYunkang Tang 		end_bit = y_msb - 1;
319ee65d4b3SYunkang Tang 		box_middle_y = (priv->y_max * (start_bit + end_bit)) /
320ee65d4b3SYunkang Tang 				(2 * (priv->y_bits - 1));
32102d04254SHans de Goede 		fields->mt[0] = fields->st;
32202d04254SHans de Goede 		fields->mt[1].x = 2 * box_middle_x - fields->mt[0].x;
32302d04254SHans de Goede 		fields->mt[1].y = 2 * box_middle_y - fields->mt[0].y;
324ee65d4b3SYunkang Tang 	}
325ee65d4b3SYunkang Tang }
326ee65d4b3SYunkang Tang 
327036e6c7bSHans de Goede static void alps_get_bitmap_points(unsigned int map,
328036e6c7bSHans de Goede 				   struct alps_bitmap_point *low,
329036e6c7bSHans de Goede 				   struct alps_bitmap_point *high,
330036e6c7bSHans de Goede 				   int *fingers)
331036e6c7bSHans de Goede {
332036e6c7bSHans de Goede 	struct alps_bitmap_point *point;
333036e6c7bSHans de Goede 	int i, bit, prev_bit = 0;
334036e6c7bSHans de Goede 
335036e6c7bSHans de Goede 	point = low;
336036e6c7bSHans de Goede 	for (i = 0; map != 0; i++, map >>= 1) {
337036e6c7bSHans de Goede 		bit = map & 1;
338036e6c7bSHans de Goede 		if (bit) {
339036e6c7bSHans de Goede 			if (!prev_bit) {
340036e6c7bSHans de Goede 				point->start_bit = i;
341105affbfSHans de Goede 				point->num_bits = 0;
342036e6c7bSHans de Goede 				(*fingers)++;
343036e6c7bSHans de Goede 			}
344036e6c7bSHans de Goede 			point->num_bits++;
345036e6c7bSHans de Goede 		} else {
346036e6c7bSHans de Goede 			if (prev_bit)
347036e6c7bSHans de Goede 				point = high;
348036e6c7bSHans de Goede 		}
349036e6c7bSHans de Goede 		prev_bit = bit;
350036e6c7bSHans de Goede 	}
351036e6c7bSHans de Goede }
352036e6c7bSHans de Goede 
353ee65d4b3SYunkang Tang /*
35401ce661fSSeth Forshee  * Process bitmap data from v3 and v4 protocols. Returns the number of
35501ce661fSSeth Forshee  * fingers detected. A return value of 0 means at least one of the
35601ce661fSSeth Forshee  * bitmaps was empty.
35701ce661fSSeth Forshee  *
35801ce661fSSeth Forshee  * The bitmaps don't have enough data to track fingers, so this function
35901ce661fSSeth Forshee  * only generates points representing a bounding box of all contacts.
36002d04254SHans de Goede  * These points are returned in fields->mt when the return value
36101ce661fSSeth Forshee  * is greater than 0.
36201ce661fSSeth Forshee  */
3637a9f73e7SKevin Cernekee static int alps_process_bitmap(struct alps_data *priv,
36402d04254SHans de Goede 			       struct alps_fields *fields)
36501ce661fSSeth Forshee {
366036e6c7bSHans de Goede 	int i, fingers_x = 0, fingers_y = 0, fingers;
36701ce661fSSeth Forshee 	struct alps_bitmap_point x_low = {0,}, x_high = {0,};
36801ce661fSSeth Forshee 	struct alps_bitmap_point y_low = {0,}, y_high = {0,};
36901ce661fSSeth Forshee 
37002d04254SHans de Goede 	if (!fields->x_map || !fields->y_map)
37101ce661fSSeth Forshee 		return 0;
37201ce661fSSeth Forshee 
37302d04254SHans de Goede 	alps_get_bitmap_points(fields->x_map, &x_low, &x_high, &fingers_x);
37402d04254SHans de Goede 	alps_get_bitmap_points(fields->y_map, &y_low, &y_high, &fingers_y);
37501ce661fSSeth Forshee 
37601ce661fSSeth Forshee 	/*
37701ce661fSSeth Forshee 	 * Fingers can overlap, so we use the maximum count of fingers
37801ce661fSSeth Forshee 	 * on either axis as the finger count.
37901ce661fSSeth Forshee 	 */
38001ce661fSSeth Forshee 	fingers = max(fingers_x, fingers_y);
38101ce661fSSeth Forshee 
38201ce661fSSeth Forshee 	/*
38320bea68bSHans de Goede 	 * If an axis reports only a single contact, we have overlapping or
38420bea68bSHans de Goede 	 * adjacent fingers. Divide the single contact between the two points.
38501ce661fSSeth Forshee 	 */
38601ce661fSSeth Forshee 	if (fingers_x == 1) {
38728835f45SHans de Goede 		i = (x_low.num_bits - 1) / 2;
38801ce661fSSeth Forshee 		x_low.num_bits = x_low.num_bits - i;
38901ce661fSSeth Forshee 		x_high.start_bit = x_low.start_bit + i;
39001ce661fSSeth Forshee 		x_high.num_bits = max(i, 1);
39120bea68bSHans de Goede 	}
39220bea68bSHans de Goede 	if (fingers_y == 1) {
39328835f45SHans de Goede 		i = (y_low.num_bits - 1) / 2;
39401ce661fSSeth Forshee 		y_low.num_bits = y_low.num_bits - i;
39501ce661fSSeth Forshee 		y_high.start_bit = y_low.start_bit + i;
39601ce661fSSeth Forshee 		y_high.num_bits = max(i, 1);
39701ce661fSSeth Forshee 	}
39801ce661fSSeth Forshee 
39902d04254SHans de Goede 	fields->mt[0].x =
40002d04254SHans de Goede 		(priv->x_max * (2 * x_low.start_bit + x_low.num_bits - 1)) /
4017a9f73e7SKevin Cernekee 		(2 * (priv->x_bits - 1));
40202d04254SHans de Goede 	fields->mt[0].y =
40302d04254SHans de Goede 		(priv->y_max * (2 * y_low.start_bit + y_low.num_bits - 1)) /
4047a9f73e7SKevin Cernekee 		(2 * (priv->y_bits - 1));
40501ce661fSSeth Forshee 
40602d04254SHans de Goede 	fields->mt[1].x =
40702d04254SHans de Goede 		(priv->x_max * (2 * x_high.start_bit + x_high.num_bits - 1)) /
4087a9f73e7SKevin Cernekee 		(2 * (priv->x_bits - 1));
40902d04254SHans de Goede 	fields->mt[1].y =
41002d04254SHans de Goede 		(priv->y_max * (2 * y_high.start_bit + y_high.num_bits - 1)) /
4117a9f73e7SKevin Cernekee 		(2 * (priv->y_bits - 1));
41201ce661fSSeth Forshee 
41340e8f53bSHans de Goede 	/* y-bitmap order is reversed, except on rushmore */
414fb2dd7a6SDmitry Torokhov 	if (priv->proto_version != ALPS_PROTO_V3_RUSHMORE) {
41502d04254SHans de Goede 		fields->mt[0].y = priv->y_max - fields->mt[0].y;
41602d04254SHans de Goede 		fields->mt[1].y = priv->y_max - fields->mt[1].y;
41740e8f53bSHans de Goede 	}
41840e8f53bSHans de Goede 
41901ce661fSSeth Forshee 	return fingers;
42001ce661fSSeth Forshee }
42101ce661fSSeth Forshee 
422cdf333efSHans de Goede static void alps_set_slot(struct input_dev *dev, int slot, int x, int y)
42301ce661fSSeth Forshee {
42401ce661fSSeth Forshee 	input_mt_slot(dev, slot);
425cdf333efSHans de Goede 	input_mt_report_slot_state(dev, MT_TOOL_FINGER, true);
42601ce661fSSeth Forshee 	input_report_abs(dev, ABS_MT_POSITION_X, x);
42701ce661fSSeth Forshee 	input_report_abs(dev, ABS_MT_POSITION_Y, y);
42801ce661fSSeth Forshee }
42901ce661fSSeth Forshee 
430cdf333efSHans de Goede static void alps_report_mt_data(struct psmouse *psmouse, int n)
43101ce661fSSeth Forshee {
43202d04254SHans de Goede 	struct alps_data *priv = psmouse->private;
43302d04254SHans de Goede 	struct input_dev *dev = psmouse->dev;
43402d04254SHans de Goede 	struct alps_fields *f = &priv->f;
435cdf333efSHans de Goede 	int i, slot[MAX_TOUCHES];
43602d04254SHans de Goede 
437448c7f38SHenrik Rydberg 	input_mt_assign_slots(dev, slot, f->mt, n, 0);
438cdf333efSHans de Goede 	for (i = 0; i < n; i++)
439cdf333efSHans de Goede 		alps_set_slot(dev, slot[i], f->mt[i].x, f->mt[i].y);
440cdf333efSHans de Goede 
441cdf333efSHans de Goede 	input_mt_sync_frame(dev);
44201ce661fSSeth Forshee }
44301ce661fSSeth Forshee 
44468c21870SHans de Goede static void alps_report_semi_mt_data(struct psmouse *psmouse, int fingers)
44568c21870SHans de Goede {
44668c21870SHans de Goede 	struct alps_data *priv = psmouse->private;
44768c21870SHans de Goede 	struct input_dev *dev = psmouse->dev;
44868c21870SHans de Goede 	struct alps_fields *f = &priv->f;
44968c21870SHans de Goede 
45068c21870SHans de Goede 	/* Use st data when we don't have mt data */
45168c21870SHans de Goede 	if (fingers < 2) {
45268c21870SHans de Goede 		f->mt[0].x = f->st.x;
45368c21870SHans de Goede 		f->mt[0].y = f->st.y;
45468c21870SHans de Goede 		fingers = f->pressure > 0 ? 1 : 0;
45568c21870SHans de Goede 	}
45668c21870SHans de Goede 
45799d9996cSHans de Goede 	alps_report_mt_data(psmouse, (fingers <= 2) ? fingers : 2);
45868c21870SHans de Goede 
45968c21870SHans de Goede 	input_mt_report_finger_count(dev, fingers);
46068c21870SHans de Goede 
46168c21870SHans de Goede 	input_report_key(dev, BTN_LEFT, f->left);
46268c21870SHans de Goede 	input_report_key(dev, BTN_RIGHT, f->right);
46368c21870SHans de Goede 	input_report_key(dev, BTN_MIDDLE, f->middle);
46468c21870SHans de Goede 
46568c21870SHans de Goede 	input_report_abs(dev, ABS_PRESSURE, f->pressure);
46668c21870SHans de Goede 
46768c21870SHans de Goede 	input_sync(dev);
46868c21870SHans de Goede }
46968c21870SHans de Goede 
47025bded7cSSeth Forshee static void alps_process_trackstick_packet_v3(struct psmouse *psmouse)
47125bded7cSSeth Forshee {
47225bded7cSSeth Forshee 	struct alps_data *priv = psmouse->private;
47325bded7cSSeth Forshee 	unsigned char *packet = psmouse->packet;
47425bded7cSSeth Forshee 	struct input_dev *dev = priv->dev2;
47525bded7cSSeth Forshee 	int x, y, z, left, right, middle;
47625bded7cSSeth Forshee 
47734412ba2SPali Rohár 	/* It should be a DualPoint when received trackstick packet */
47834412ba2SPali Rohár 	if (!(priv->flags & ALPS_DUALPOINT)) {
47934412ba2SPali Rohár 		psmouse_warn(psmouse,
48034412ba2SPali Rohár 			     "Rejected trackstick packet from non DualPoint device");
48134412ba2SPali Rohár 		return;
48234412ba2SPali Rohár 	}
48334412ba2SPali Rohár 
48425bded7cSSeth Forshee 	/* Sanity check packet */
48525bded7cSSeth Forshee 	if (!(packet[0] & 0x40)) {
48625bded7cSSeth Forshee 		psmouse_dbg(psmouse, "Bad trackstick packet, discarding\n");
48725bded7cSSeth Forshee 		return;
48825bded7cSSeth Forshee 	}
48925bded7cSSeth Forshee 
49025bded7cSSeth Forshee 	/*
49125bded7cSSeth Forshee 	 * There's a special packet that seems to indicate the end
49225bded7cSSeth Forshee 	 * of a stream of trackstick data. Filter these out.
49325bded7cSSeth Forshee 	 */
49425bded7cSSeth Forshee 	if (packet[1] == 0x7f && packet[2] == 0x7f && packet[4] == 0x7f)
49525bded7cSSeth Forshee 		return;
49625bded7cSSeth Forshee 
49725bded7cSSeth Forshee 	x = (s8)(((packet[0] & 0x20) << 2) | (packet[1] & 0x7f));
49825bded7cSSeth Forshee 	y = (s8)(((packet[0] & 0x10) << 3) | (packet[2] & 0x7f));
49925bded7cSSeth Forshee 	z = (packet[4] & 0x7c) >> 2;
50025bded7cSSeth Forshee 
50125bded7cSSeth Forshee 	/*
50225bded7cSSeth Forshee 	 * The x and y values tend to be quite large, and when used
50325bded7cSSeth Forshee 	 * alone the trackstick is difficult to use. Scale them down
50425bded7cSSeth Forshee 	 * to compensate.
50525bded7cSSeth Forshee 	 */
50625bded7cSSeth Forshee 	x /= 8;
50725bded7cSSeth Forshee 	y /= 8;
50825bded7cSSeth Forshee 
50925bded7cSSeth Forshee 	input_report_rel(dev, REL_X, x);
51025bded7cSSeth Forshee 	input_report_rel(dev, REL_Y, -y);
51125bded7cSSeth Forshee 
51225bded7cSSeth Forshee 	/*
51325bded7cSSeth Forshee 	 * Most ALPS models report the trackstick buttons in the touchpad
51425bded7cSSeth Forshee 	 * packets, but a few report them here. No reliable way has been
51525bded7cSSeth Forshee 	 * found to differentiate between the models upfront, so we enable
51625bded7cSSeth Forshee 	 * the quirk in response to seeing a button press in the trackstick
51725bded7cSSeth Forshee 	 * packet.
51825bded7cSSeth Forshee 	 */
51925bded7cSSeth Forshee 	left = packet[3] & 0x01;
52025bded7cSSeth Forshee 	right = packet[3] & 0x02;
52125bded7cSSeth Forshee 	middle = packet[3] & 0x04;
52225bded7cSSeth Forshee 
52325bded7cSSeth Forshee 	if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) &&
52425bded7cSSeth Forshee 	    (left || right || middle))
52525bded7cSSeth Forshee 		priv->quirks |= ALPS_QUIRK_TRACKSTICK_BUTTONS;
52625bded7cSSeth Forshee 
52725bded7cSSeth Forshee 	if (priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) {
52825bded7cSSeth Forshee 		input_report_key(dev, BTN_LEFT, left);
52925bded7cSSeth Forshee 		input_report_key(dev, BTN_RIGHT, right);
53025bded7cSSeth Forshee 		input_report_key(dev, BTN_MIDDLE, middle);
53125bded7cSSeth Forshee 	}
53225bded7cSSeth Forshee 
53325bded7cSSeth Forshee 	input_sync(dev);
53425bded7cSSeth Forshee 	return;
53525bded7cSSeth Forshee }
53625bded7cSSeth Forshee 
537f85e5001SKevin Cernekee static void alps_decode_buttons_v3(struct alps_fields *f, unsigned char *p)
538f85e5001SKevin Cernekee {
539f85e5001SKevin Cernekee 	f->left = !!(p[3] & 0x01);
540f85e5001SKevin Cernekee 	f->right = !!(p[3] & 0x02);
541f85e5001SKevin Cernekee 	f->middle = !!(p[3] & 0x04);
542f85e5001SKevin Cernekee 
543f85e5001SKevin Cernekee 	f->ts_left = !!(p[3] & 0x10);
544f85e5001SKevin Cernekee 	f->ts_right = !!(p[3] & 0x20);
545f85e5001SKevin Cernekee 	f->ts_middle = !!(p[3] & 0x40);
546f85e5001SKevin Cernekee }
547f85e5001SKevin Cernekee 
54838c11eaaSHans de Goede static int alps_decode_pinnacle(struct alps_fields *f, unsigned char *p,
549ee65d4b3SYunkang Tang 				 struct psmouse *psmouse)
550f85e5001SKevin Cernekee {
551f85e5001SKevin Cernekee 	f->first_mp = !!(p[4] & 0x40);
552f85e5001SKevin Cernekee 	f->is_mp = !!(p[0] & 0x40);
553f85e5001SKevin Cernekee 
554f85e5001SKevin Cernekee 	f->fingers = (p[5] & 0x3) + 1;
555f85e5001SKevin Cernekee 	f->x_map = ((p[4] & 0x7e) << 8) |
556f85e5001SKevin Cernekee 		   ((p[1] & 0x7f) << 2) |
557f85e5001SKevin Cernekee 		   ((p[0] & 0x30) >> 4);
558f85e5001SKevin Cernekee 	f->y_map = ((p[3] & 0x70) << 4) |
559f85e5001SKevin Cernekee 		   ((p[2] & 0x7f) << 1) |
560f85e5001SKevin Cernekee 		   (p[4] & 0x01);
561f85e5001SKevin Cernekee 
56202d04254SHans de Goede 	f->st.x = ((p[1] & 0x7f) << 4) | ((p[4] & 0x30) >> 2) |
563f85e5001SKevin Cernekee 	       ((p[0] & 0x30) >> 4);
56402d04254SHans de Goede 	f->st.y = ((p[2] & 0x7f) << 4) | (p[4] & 0x0f);
56502d04254SHans de Goede 	f->pressure = p[5] & 0x7f;
566f85e5001SKevin Cernekee 
567f85e5001SKevin Cernekee 	alps_decode_buttons_v3(f, p);
56838c11eaaSHans de Goede 
56938c11eaaSHans de Goede 	return 0;
570f85e5001SKevin Cernekee }
571f85e5001SKevin Cernekee 
57238c11eaaSHans de Goede static int alps_decode_rushmore(struct alps_fields *f, unsigned char *p,
573ee65d4b3SYunkang Tang 				 struct psmouse *psmouse)
5741302bac3SKevin Cernekee {
575ee65d4b3SYunkang Tang 	alps_decode_pinnacle(f, p, psmouse);
5761302bac3SKevin Cernekee 
577f105e34aSYunkang Tang 	/* Rushmore's packet decode has a bit difference with Pinnacle's */
578f105e34aSYunkang Tang 	f->is_mp = !!(p[5] & 0x40);
579f105e34aSYunkang Tang 	f->fingers = max((p[5] & 0x3), ((p[5] >> 2) & 0x3)) + 1;
5801302bac3SKevin Cernekee 	f->x_map |= (p[5] & 0x10) << 11;
5811302bac3SKevin Cernekee 	f->y_map |= (p[5] & 0x20) << 6;
58238c11eaaSHans de Goede 
58338c11eaaSHans de Goede 	return 0;
5841302bac3SKevin Cernekee }
5851302bac3SKevin Cernekee 
58638c11eaaSHans de Goede static int alps_decode_dolphin(struct alps_fields *f, unsigned char *p,
587ee65d4b3SYunkang Tang 				struct psmouse *psmouse)
58875af9e56SDave Turvene {
589ee65d4b3SYunkang Tang 	u64 palm_data = 0;
590ee65d4b3SYunkang Tang 	struct alps_data *priv = psmouse->private;
591ee65d4b3SYunkang Tang 
59275af9e56SDave Turvene 	f->first_mp = !!(p[0] & 0x02);
59375af9e56SDave Turvene 	f->is_mp = !!(p[0] & 0x20);
59475af9e56SDave Turvene 
595ee65d4b3SYunkang Tang 	if (!f->is_mp) {
59602d04254SHans de Goede 		f->st.x = ((p[1] & 0x7f) | ((p[4] & 0x0f) << 7));
59702d04254SHans de Goede 		f->st.y = ((p[2] & 0x7f) | ((p[4] & 0xf0) << 3));
59802d04254SHans de Goede 		f->pressure = (p[0] & 4) ? 0 : p[5] & 0x7f;
59975af9e56SDave Turvene 		alps_decode_buttons_v3(f, p);
600ee65d4b3SYunkang Tang 	} else {
601ee65d4b3SYunkang Tang 		f->fingers = ((p[0] & 0x6) >> 1 |
602ee65d4b3SYunkang Tang 		     (p[0] & 0x10) >> 2);
603ee65d4b3SYunkang Tang 
604ee65d4b3SYunkang Tang 		palm_data = (p[1] & 0x7f) |
605ee65d4b3SYunkang Tang 			    ((p[2] & 0x7f) << 7) |
606ee65d4b3SYunkang Tang 			    ((p[4] & 0x7f) << 14) |
607ee65d4b3SYunkang Tang 			    ((p[5] & 0x7f) << 21) |
608ee65d4b3SYunkang Tang 			    ((p[3] & 0x07) << 28) |
609ee65d4b3SYunkang Tang 			    (((u64)p[3] & 0x70) << 27) |
610ee65d4b3SYunkang Tang 			    (((u64)p[0] & 0x01) << 34);
611ee65d4b3SYunkang Tang 
612ee65d4b3SYunkang Tang 		/* Y-profile is stored in P(0) to p(n-1), n = y_bits; */
613ee65d4b3SYunkang Tang 		f->y_map = palm_data & (BIT(priv->y_bits) - 1);
614ee65d4b3SYunkang Tang 
615ee65d4b3SYunkang Tang 		/* X-profile is stored in p(n) to p(n+m-1), m = x_bits; */
616ee65d4b3SYunkang Tang 		f->x_map = (palm_data >> priv->y_bits) &
617ee65d4b3SYunkang Tang 			   (BIT(priv->x_bits) - 1);
618ee65d4b3SYunkang Tang 	}
61938c11eaaSHans de Goede 
62038c11eaaSHans de Goede 	return 0;
62175af9e56SDave Turvene }
62275af9e56SDave Turvene 
623ee65d4b3SYunkang Tang static void alps_process_touchpad_packet_v3_v5(struct psmouse *psmouse)
62425bded7cSSeth Forshee {
62525bded7cSSeth Forshee 	struct alps_data *priv = psmouse->private;
62625bded7cSSeth Forshee 	unsigned char *packet = psmouse->packet;
62725bded7cSSeth Forshee 	struct input_dev *dev2 = priv->dev2;
62802d04254SHans de Goede 	struct alps_fields *f = &priv->f;
62902d04254SHans de Goede 	int fingers = 0;
630f85e5001SKevin Cernekee 
63102d04254SHans de Goede 	memset(f, 0, sizeof(*f));
63202d04254SHans de Goede 
63302d04254SHans de Goede 	priv->decode_fields(f, packet, psmouse);
63425bded7cSSeth Forshee 
63525bded7cSSeth Forshee 	/*
63601ce661fSSeth Forshee 	 * There's no single feature of touchpad position and bitmap packets
63701ce661fSSeth Forshee 	 * that can be used to distinguish between them. We rely on the fact
63801ce661fSSeth Forshee 	 * that a bitmap packet should always follow a position packet with
63901ce661fSSeth Forshee 	 * bit 6 of packet[4] set.
64025bded7cSSeth Forshee 	 */
64125bded7cSSeth Forshee 	if (priv->multi_packet) {
64225bded7cSSeth Forshee 		/*
64325bded7cSSeth Forshee 		 * Sometimes a position packet will indicate a multi-packet
64425bded7cSSeth Forshee 		 * sequence, but then what follows is another position
64525bded7cSSeth Forshee 		 * packet. Check for this, and when it happens process the
64625bded7cSSeth Forshee 		 * position packet as usual.
64725bded7cSSeth Forshee 		 */
64802d04254SHans de Goede 		if (f->is_mp) {
64902d04254SHans de Goede 			fingers = f->fingers;
650fb2dd7a6SDmitry Torokhov 			if (priv->proto_version == ALPS_PROTO_V3 ||
651fb2dd7a6SDmitry Torokhov 			    priv->proto_version == ALPS_PROTO_V3_RUSHMORE) {
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 
849b0cfb794SAndreas Bosch 	f->left = !!(packet[4] & 0x01);
850b0cfb794SAndreas Bosch 	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 
12414ab8f7f3SPali Rohár 	/*
12424ab8f7f3SPali Rohár 	 * Check if we are dealing with a bare PS/2 packet, presumably from
12434ab8f7f3SPali Rohár 	 * a device connected to the external PS/2 port. Because bare PS/2
12444ab8f7f3SPali Rohár 	 * protocol does not have enough constant bits to self-synchronize
12454ab8f7f3SPali Rohár 	 * properly we only do this if the device is fully synchronized.
12464ab8f7f3SPali Rohár 	 */
12474ab8f7f3SPali Rohár 	if (!psmouse->out_of_sync_cnt && (psmouse->packet[0] & 0xc8) == 0x08) {
12481da177e4SLinus Torvalds 		if (psmouse->pktcnt == 3) {
12491d9f2626SSebastian Kapfer 			alps_report_bare_ps2_packet(psmouse, psmouse->packet,
12501d9f2626SSebastian Kapfer 						    true);
12511da177e4SLinus Torvalds 			return PSMOUSE_FULL_PACKET;
12521da177e4SLinus Torvalds 		}
12531da177e4SLinus Torvalds 		return PSMOUSE_GOOD_DATA;
12541da177e4SLinus Torvalds 	}
12551da177e4SLinus Torvalds 
12561d9f2626SSebastian Kapfer 	/* Check for PS/2 packet stuffed in the middle of ALPS packet. */
12571d9f2626SSebastian Kapfer 
125899df65e7SKevin Cernekee 	if ((priv->flags & ALPS_PS2_INTERLEAVED) &&
12591d9f2626SSebastian Kapfer 	    psmouse->pktcnt >= 4 && (psmouse->packet[3] & 0x0f) == 0x0f) {
12601d9f2626SSebastian Kapfer 		return alps_handle_interleaved_ps2(psmouse);
12611d9f2626SSebastian Kapfer 	}
12621d9f2626SSebastian Kapfer 
126399df65e7SKevin Cernekee 	if (!alps_is_valid_first_byte(priv, psmouse->packet[0])) {
1264b5d21704SDmitry Torokhov 		psmouse_dbg(psmouse,
1265b5d21704SDmitry Torokhov 			    "refusing packet[0] = %x (mask0 = %x, byte0 = %x)\n",
126699df65e7SKevin Cernekee 			    psmouse->packet[0], priv->mask0, priv->byte0);
12671da177e4SLinus Torvalds 		return PSMOUSE_BAD_DATA;
12681d9f2626SSebastian Kapfer 	}
12691da177e4SLinus Torvalds 
1270b46615feSSeth Forshee 	/* Bytes 2 - pktsize should have 0 in the highest bit */
1271a7ef82aeSPali Rohár 	if (priv->proto_version < ALPS_PROTO_V5 &&
127275af9e56SDave Turvene 	    psmouse->pktcnt >= 2 && psmouse->pktcnt <= psmouse->pktsize &&
12731d9f2626SSebastian Kapfer 	    (psmouse->packet[psmouse->pktcnt - 1] & 0x80)) {
1274b5d21704SDmitry Torokhov 		psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
1275b5d21704SDmitry Torokhov 			    psmouse->pktcnt - 1,
1276b5d21704SDmitry Torokhov 			    psmouse->packet[psmouse->pktcnt - 1]);
1277a7ef82aeSPali Rohár 
1278fb2dd7a6SDmitry Torokhov 		if (priv->proto_version == ALPS_PROTO_V3_RUSHMORE &&
1279a7ef82aeSPali Rohár 		    psmouse->pktcnt == psmouse->pktsize) {
1280a7ef82aeSPali Rohár 			/*
1281a7ef82aeSPali Rohár 			 * Some Dell boxes, such as Latitude E6440 or E7440
1282a7ef82aeSPali Rohár 			 * with closed lid, quite often smash last byte of
1283a7ef82aeSPali Rohár 			 * otherwise valid packet with 0xff. Given that the
1284a7ef82aeSPali Rohár 			 * next packet is very likely to be valid let's
1285a7ef82aeSPali Rohár 			 * report PSMOUSE_FULL_PACKET but not process data,
1286a7ef82aeSPali Rohár 			 * rather than reporting PSMOUSE_BAD_DATA and
1287a7ef82aeSPali Rohár 			 * filling the logs.
1288a7ef82aeSPali Rohár 			 */
1289a7ef82aeSPali Rohár 			return PSMOUSE_FULL_PACKET;
1290a7ef82aeSPali Rohár 		}
1291a7ef82aeSPali Rohár 
12921da177e4SLinus Torvalds 		return PSMOUSE_BAD_DATA;
12931d9f2626SSebastian Kapfer 	}
12941da177e4SLinus Torvalds 
12953808843cSYunkang Tang 	if (priv->proto_version == ALPS_PROTO_V7 &&
12963808843cSYunkang Tang 	    !alps_is_valid_package_v7(psmouse)) {
12973808843cSYunkang Tang 		psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
12983808843cSYunkang Tang 			    psmouse->pktcnt - 1,
12993808843cSYunkang Tang 			    psmouse->packet[psmouse->pktcnt - 1]);
13003808843cSYunkang Tang 		return PSMOUSE_BAD_DATA;
13013808843cSYunkang Tang 	}
13023808843cSYunkang Tang 
1303b46615feSSeth Forshee 	if (psmouse->pktcnt == psmouse->pktsize) {
130424af5cb9SKevin Cernekee 		priv->process_packet(psmouse);
13051da177e4SLinus Torvalds 		return PSMOUSE_FULL_PACKET;
13061da177e4SLinus Torvalds 	}
13071da177e4SLinus Torvalds 
13081da177e4SLinus Torvalds 	return PSMOUSE_GOOD_DATA;
13091da177e4SLinus Torvalds }
13101da177e4SLinus Torvalds 
131125bded7cSSeth Forshee static int alps_command_mode_send_nibble(struct psmouse *psmouse, int nibble)
131225bded7cSSeth Forshee {
131325bded7cSSeth Forshee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
131425bded7cSSeth Forshee 	struct alps_data *priv = psmouse->private;
131525bded7cSSeth Forshee 	int command;
131625bded7cSSeth Forshee 	unsigned char *param;
131725bded7cSSeth Forshee 	unsigned char dummy[4];
131825bded7cSSeth Forshee 
131925bded7cSSeth Forshee 	BUG_ON(nibble > 0xf);
132025bded7cSSeth Forshee 
132125bded7cSSeth Forshee 	command = priv->nibble_commands[nibble].command;
132225bded7cSSeth Forshee 	param = (command & 0x0f00) ?
132325bded7cSSeth Forshee 		dummy : (unsigned char *)&priv->nibble_commands[nibble].data;
132425bded7cSSeth Forshee 
132525bded7cSSeth Forshee 	if (ps2_command(ps2dev, param, command))
132625bded7cSSeth Forshee 		return -1;
132725bded7cSSeth Forshee 
132825bded7cSSeth Forshee 	return 0;
132925bded7cSSeth Forshee }
133025bded7cSSeth Forshee 
133125bded7cSSeth Forshee static int alps_command_mode_set_addr(struct psmouse *psmouse, int addr)
133225bded7cSSeth Forshee {
133325bded7cSSeth Forshee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
133425bded7cSSeth Forshee 	struct alps_data *priv = psmouse->private;
133525bded7cSSeth Forshee 	int i, nibble;
133625bded7cSSeth Forshee 
133725bded7cSSeth Forshee 	if (ps2_command(ps2dev, NULL, priv->addr_command))
133825bded7cSSeth Forshee 		return -1;
133925bded7cSSeth Forshee 
134025bded7cSSeth Forshee 	for (i = 12; i >= 0; i -= 4) {
134125bded7cSSeth Forshee 		nibble = (addr >> i) & 0xf;
134225bded7cSSeth Forshee 		if (alps_command_mode_send_nibble(psmouse, nibble))
134325bded7cSSeth Forshee 			return -1;
134425bded7cSSeth Forshee 	}
134525bded7cSSeth Forshee 
134625bded7cSSeth Forshee 	return 0;
134725bded7cSSeth Forshee }
134825bded7cSSeth Forshee 
134925bded7cSSeth Forshee static int __alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
135025bded7cSSeth Forshee {
135125bded7cSSeth Forshee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
135225bded7cSSeth Forshee 	unsigned char param[4];
135325bded7cSSeth Forshee 
135425bded7cSSeth Forshee 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
135525bded7cSSeth Forshee 		return -1;
135625bded7cSSeth Forshee 
135725bded7cSSeth Forshee 	/*
135825bded7cSSeth Forshee 	 * The address being read is returned in the first two bytes
135925bded7cSSeth Forshee 	 * of the result. Check that this address matches the expected
136025bded7cSSeth Forshee 	 * address.
136125bded7cSSeth Forshee 	 */
136225bded7cSSeth Forshee 	if (addr != ((param[0] << 8) | param[1]))
136325bded7cSSeth Forshee 		return -1;
136425bded7cSSeth Forshee 
136525bded7cSSeth Forshee 	return param[2];
136625bded7cSSeth Forshee }
136725bded7cSSeth Forshee 
136825bded7cSSeth Forshee static int alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
136925bded7cSSeth Forshee {
137025bded7cSSeth Forshee 	if (alps_command_mode_set_addr(psmouse, addr))
137125bded7cSSeth Forshee 		return -1;
137225bded7cSSeth Forshee 	return __alps_command_mode_read_reg(psmouse, addr);
137325bded7cSSeth Forshee }
137425bded7cSSeth Forshee 
137525bded7cSSeth Forshee static int __alps_command_mode_write_reg(struct psmouse *psmouse, u8 value)
137625bded7cSSeth Forshee {
137725bded7cSSeth Forshee 	if (alps_command_mode_send_nibble(psmouse, (value >> 4) & 0xf))
137825bded7cSSeth Forshee 		return -1;
137925bded7cSSeth Forshee 	if (alps_command_mode_send_nibble(psmouse, value & 0xf))
138025bded7cSSeth Forshee 		return -1;
138125bded7cSSeth Forshee 	return 0;
138225bded7cSSeth Forshee }
138325bded7cSSeth Forshee 
138425bded7cSSeth Forshee static int alps_command_mode_write_reg(struct psmouse *psmouse, int addr,
138525bded7cSSeth Forshee 				       u8 value)
138625bded7cSSeth Forshee {
138725bded7cSSeth Forshee 	if (alps_command_mode_set_addr(psmouse, addr))
138825bded7cSSeth Forshee 		return -1;
138925bded7cSSeth Forshee 	return __alps_command_mode_write_reg(psmouse, value);
139025bded7cSSeth Forshee }
139125bded7cSSeth Forshee 
139224ba9707SKevin Cernekee static int alps_rpt_cmd(struct psmouse *psmouse, int init_command,
139324ba9707SKevin Cernekee 			int repeated_command, unsigned char *param)
139424ba9707SKevin Cernekee {
139524ba9707SKevin Cernekee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
139624ba9707SKevin Cernekee 
139724ba9707SKevin Cernekee 	param[0] = 0;
139824ba9707SKevin Cernekee 	if (init_command && ps2_command(ps2dev, param, init_command))
139924ba9707SKevin Cernekee 		return -EIO;
140024ba9707SKevin Cernekee 
140124ba9707SKevin Cernekee 	if (ps2_command(ps2dev,  NULL, repeated_command) ||
140224ba9707SKevin Cernekee 	    ps2_command(ps2dev,  NULL, repeated_command) ||
140324ba9707SKevin Cernekee 	    ps2_command(ps2dev,  NULL, repeated_command))
140424ba9707SKevin Cernekee 		return -EIO;
140524ba9707SKevin Cernekee 
140624ba9707SKevin Cernekee 	param[0] = param[1] = param[2] = 0xff;
140724ba9707SKevin Cernekee 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
140824ba9707SKevin Cernekee 		return -EIO;
140924ba9707SKevin Cernekee 
141039fbe585SDmitry Torokhov 	psmouse_dbg(psmouse, "%2.2X report: %3ph\n",
141139fbe585SDmitry Torokhov 		    repeated_command, param);
141224ba9707SKevin Cernekee 	return 0;
141324ba9707SKevin Cernekee }
141424ba9707SKevin Cernekee 
14153808843cSYunkang Tang static bool alps_check_valid_firmware_id(unsigned char id[])
14163808843cSYunkang Tang {
14173808843cSYunkang Tang 	if (id[0] == 0x73)
14183808843cSYunkang Tang 		return true;
14193808843cSYunkang Tang 
14203808843cSYunkang Tang 	if (id[0] == 0x88 &&
14213808843cSYunkang Tang 	    (id[1] == 0x07 ||
14223808843cSYunkang Tang 	     id[1] == 0x08 ||
14233808843cSYunkang Tang 	     (id[1] & 0xf0) == 0xb0 ||
14243808843cSYunkang Tang 	     (id[1] & 0xf0) == 0xc0)) {
14253808843cSYunkang Tang 		return true;
14263808843cSYunkang Tang 	}
14273808843cSYunkang Tang 
14283808843cSYunkang Tang 	return false;
14293808843cSYunkang Tang }
14303808843cSYunkang Tang 
1431d18e53fcSKevin Cernekee static int alps_enter_command_mode(struct psmouse *psmouse)
143225bded7cSSeth Forshee {
143325bded7cSSeth Forshee 	unsigned char param[4];
143425bded7cSSeth Forshee 
143524ba9707SKevin Cernekee 	if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_RESET_WRAP, param)) {
143625bded7cSSeth Forshee 		psmouse_err(psmouse, "failed to enter command mode\n");
143725bded7cSSeth Forshee 		return -1;
143825bded7cSSeth Forshee 	}
143925bded7cSSeth Forshee 
14403808843cSYunkang Tang 	if (!alps_check_valid_firmware_id(param)) {
144125bded7cSSeth Forshee 		psmouse_dbg(psmouse,
144224ba9707SKevin Cernekee 			    "unknown response while entering command mode\n");
144325bded7cSSeth Forshee 		return -1;
144425bded7cSSeth Forshee 	}
144525bded7cSSeth Forshee 	return 0;
144625bded7cSSeth Forshee }
144725bded7cSSeth Forshee 
144825bded7cSSeth Forshee static inline int alps_exit_command_mode(struct psmouse *psmouse)
144925bded7cSSeth Forshee {
145025bded7cSSeth Forshee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
145125bded7cSSeth Forshee 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM))
145225bded7cSSeth Forshee 		return -1;
145325bded7cSSeth Forshee 	return 0;
145425bded7cSSeth Forshee }
145525bded7cSSeth Forshee 
14561da177e4SLinus Torvalds /*
14571da177e4SLinus Torvalds  * For DualPoint devices select the device that should respond to
14581da177e4SLinus Torvalds  * subsequent commands. It looks like glidepad is behind stickpointer,
14591da177e4SLinus Torvalds  * I'd thought it would be other way around...
14601da177e4SLinus Torvalds  */
146125bded7cSSeth Forshee static int alps_passthrough_mode_v2(struct psmouse *psmouse, bool enable)
14621da177e4SLinus Torvalds {
14631da177e4SLinus Torvalds 	struct ps2dev *ps2dev = &psmouse->ps2dev;
14641da177e4SLinus Torvalds 	int cmd = enable ? PSMOUSE_CMD_SETSCALE21 : PSMOUSE_CMD_SETSCALE11;
14651da177e4SLinus Torvalds 
14661da177e4SLinus Torvalds 	if (ps2_command(ps2dev, NULL, cmd) ||
14671da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, cmd) ||
14681da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, cmd) ||
14691da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
14701da177e4SLinus Torvalds 		return -1;
14711da177e4SLinus Torvalds 
14721da177e4SLinus Torvalds 	/* we may get 3 more bytes, just ignore them */
1473c611763dSDmitry Torokhov 	ps2_drain(ps2dev, 3, 100);
14741da177e4SLinus Torvalds 
14751da177e4SLinus Torvalds 	return 0;
14761da177e4SLinus Torvalds }
14771da177e4SLinus Torvalds 
147825bded7cSSeth Forshee static int alps_absolute_mode_v1_v2(struct psmouse *psmouse)
14791da177e4SLinus Torvalds {
14801da177e4SLinus Torvalds 	struct ps2dev *ps2dev = &psmouse->ps2dev;
14811da177e4SLinus Torvalds 
14821da177e4SLinus Torvalds 	/* Try ALPS magic knock - 4 disable before enable */
14831da177e4SLinus Torvalds 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
14841da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
14851da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
14861da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
14871da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
14881da177e4SLinus Torvalds 		return -1;
14891da177e4SLinus Torvalds 
14901da177e4SLinus Torvalds 	/*
14911da177e4SLinus Torvalds 	 * Switch mouse to poll (remote) mode so motion data will not
14921da177e4SLinus Torvalds 	 * get in our way
14931da177e4SLinus Torvalds 	 */
14941da177e4SLinus Torvalds 	return ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETPOLL);
14951da177e4SLinus Torvalds }
14961da177e4SLinus Torvalds 
149795f75e91SYunkang Tang static int alps_monitor_mode_send_word(struct psmouse *psmouse, u16 word)
149895f75e91SYunkang Tang {
149995f75e91SYunkang Tang 	int i, nibble;
150095f75e91SYunkang Tang 
150195f75e91SYunkang Tang 	/*
150295f75e91SYunkang Tang 	 * b0-b11 are valid bits, send sequence is inverse.
150395f75e91SYunkang Tang 	 * e.g. when word = 0x0123, nibble send sequence is 3, 2, 1
150495f75e91SYunkang Tang 	 */
150595f75e91SYunkang Tang 	for (i = 0; i <= 8; i += 4) {
150695f75e91SYunkang Tang 		nibble = (word >> i) & 0xf;
150795f75e91SYunkang Tang 		if (alps_command_mode_send_nibble(psmouse, nibble))
150895f75e91SYunkang Tang 			return -1;
150995f75e91SYunkang Tang 	}
151095f75e91SYunkang Tang 
151195f75e91SYunkang Tang 	return 0;
151295f75e91SYunkang Tang }
151395f75e91SYunkang Tang 
151495f75e91SYunkang Tang static int alps_monitor_mode_write_reg(struct psmouse *psmouse,
151595f75e91SYunkang Tang 				       u16 addr, u16 value)
151695f75e91SYunkang Tang {
151795f75e91SYunkang Tang 	struct ps2dev *ps2dev = &psmouse->ps2dev;
151895f75e91SYunkang Tang 
151995f75e91SYunkang Tang 	/* 0x0A0 is the command to write the word */
152095f75e91SYunkang Tang 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE) ||
152195f75e91SYunkang Tang 	    alps_monitor_mode_send_word(psmouse, 0x0A0) ||
152295f75e91SYunkang Tang 	    alps_monitor_mode_send_word(psmouse, addr) ||
152395f75e91SYunkang Tang 	    alps_monitor_mode_send_word(psmouse, value) ||
152495f75e91SYunkang Tang 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
152595f75e91SYunkang Tang 		return -1;
152695f75e91SYunkang Tang 
152795f75e91SYunkang Tang 	return 0;
152895f75e91SYunkang Tang }
152995f75e91SYunkang Tang 
153095f75e91SYunkang Tang static int alps_monitor_mode(struct psmouse *psmouse, bool enable)
153195f75e91SYunkang Tang {
153295f75e91SYunkang Tang 	struct ps2dev *ps2dev = &psmouse->ps2dev;
153395f75e91SYunkang Tang 
153495f75e91SYunkang Tang 	if (enable) {
153595f75e91SYunkang Tang 		/* EC E9 F5 F5 E7 E6 E7 E9 to enter monitor mode */
153695f75e91SYunkang Tang 		if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
153795f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO) ||
153895f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
153995f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
154095f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
154195f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
154295f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
154395f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO))
154495f75e91SYunkang Tang 			return -1;
154595f75e91SYunkang Tang 	} else {
154695f75e91SYunkang Tang 		/* EC to exit monitor mode */
154795f75e91SYunkang Tang 		if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP))
154895f75e91SYunkang Tang 			return -1;
154995f75e91SYunkang Tang 	}
155095f75e91SYunkang Tang 
155195f75e91SYunkang Tang 	return 0;
155295f75e91SYunkang Tang }
155395f75e91SYunkang Tang 
155495f75e91SYunkang Tang static int alps_absolute_mode_v6(struct psmouse *psmouse)
155595f75e91SYunkang Tang {
155695f75e91SYunkang Tang 	u16 reg_val = 0x181;
155795f75e91SYunkang Tang 	int ret = -1;
155895f75e91SYunkang Tang 
155995f75e91SYunkang Tang 	/* enter monitor mode, to write the register */
156095f75e91SYunkang Tang 	if (alps_monitor_mode(psmouse, true))
156195f75e91SYunkang Tang 		return -1;
156295f75e91SYunkang Tang 
156395f75e91SYunkang Tang 	ret = alps_monitor_mode_write_reg(psmouse, 0x000, reg_val);
156495f75e91SYunkang Tang 
156595f75e91SYunkang Tang 	if (alps_monitor_mode(psmouse, false))
156695f75e91SYunkang Tang 		ret = -1;
156795f75e91SYunkang Tang 
156895f75e91SYunkang Tang 	return ret;
156995f75e91SYunkang Tang }
157095f75e91SYunkang Tang 
15711da177e4SLinus Torvalds static int alps_get_status(struct psmouse *psmouse, char *param)
15721da177e4SLinus Torvalds {
15731da177e4SLinus Torvalds 	/* Get status: 0xF5 0xF5 0xF5 0xE9 */
157424ba9707SKevin Cernekee 	if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_DISABLE, param))
15751da177e4SLinus Torvalds 		return -1;
15761da177e4SLinus Torvalds 
15771da177e4SLinus Torvalds 	return 0;
15781da177e4SLinus Torvalds }
15791da177e4SLinus Torvalds 
15801da177e4SLinus Torvalds /*
15811da177e4SLinus Torvalds  * Turn touchpad tapping on or off. The sequences are:
15821da177e4SLinus Torvalds  * 0xE9 0xF5 0xF5 0xF3 0x0A to enable,
15831da177e4SLinus Torvalds  * 0xE9 0xF5 0xF5 0xE8 0x00 to disable.
15841da177e4SLinus Torvalds  * My guess that 0xE9 (GetInfo) is here as a sync point.
15851da177e4SLinus Torvalds  * For models that also have stickpointer (DualPoints) its tapping
15861da177e4SLinus Torvalds  * is controlled separately (0xE6 0xE6 0xE6 0xF3 0x14|0x0A) but
15871da177e4SLinus Torvalds  * we don't fiddle with it.
15881da177e4SLinus Torvalds  */
15891da177e4SLinus Torvalds static int alps_tap_mode(struct psmouse *psmouse, int enable)
15901da177e4SLinus Torvalds {
15911da177e4SLinus Torvalds 	struct ps2dev *ps2dev = &psmouse->ps2dev;
15921da177e4SLinus Torvalds 	int cmd = enable ? PSMOUSE_CMD_SETRATE : PSMOUSE_CMD_SETRES;
15931da177e4SLinus Torvalds 	unsigned char tap_arg = enable ? 0x0A : 0x00;
15941da177e4SLinus Torvalds 	unsigned char param[4];
15951da177e4SLinus Torvalds 
15961da177e4SLinus Torvalds 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO) ||
15971da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
15981da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
15991da177e4SLinus Torvalds 	    ps2_command(ps2dev, &tap_arg, cmd))
16001da177e4SLinus Torvalds 		return -1;
16011da177e4SLinus Torvalds 
16021da177e4SLinus Torvalds 	if (alps_get_status(psmouse, param))
16031da177e4SLinus Torvalds 		return -1;
16041da177e4SLinus Torvalds 
16051da177e4SLinus Torvalds 	return 0;
16061da177e4SLinus Torvalds }
16071da177e4SLinus Torvalds 
1608f0d5c6f4SDmitry Torokhov /*
1609f0d5c6f4SDmitry Torokhov  * alps_poll() - poll the touchpad for current motion packet.
1610f0d5c6f4SDmitry Torokhov  * Used in resync.
1611f0d5c6f4SDmitry Torokhov  */
1612f0d5c6f4SDmitry Torokhov static int alps_poll(struct psmouse *psmouse)
1613f0d5c6f4SDmitry Torokhov {
1614f0d5c6f4SDmitry Torokhov 	struct alps_data *priv = psmouse->private;
1615b46615feSSeth Forshee 	unsigned char buf[sizeof(psmouse->packet)];
1616b7802c5cSDmitry Torokhov 	bool poll_failed;
1617f0d5c6f4SDmitry Torokhov 
161899df65e7SKevin Cernekee 	if (priv->flags & ALPS_PASS)
161925bded7cSSeth Forshee 		alps_passthrough_mode_v2(psmouse, true);
1620f0d5c6f4SDmitry Torokhov 
1621f0d5c6f4SDmitry Torokhov 	poll_failed = ps2_command(&psmouse->ps2dev, buf,
1622f0d5c6f4SDmitry Torokhov 				  PSMOUSE_CMD_POLL | (psmouse->pktsize << 8)) < 0;
1623f0d5c6f4SDmitry Torokhov 
162499df65e7SKevin Cernekee 	if (priv->flags & ALPS_PASS)
162525bded7cSSeth Forshee 		alps_passthrough_mode_v2(psmouse, false);
1626f0d5c6f4SDmitry Torokhov 
162799df65e7SKevin Cernekee 	if (poll_failed || (buf[0] & priv->mask0) != priv->byte0)
1628f0d5c6f4SDmitry Torokhov 		return -1;
1629f0d5c6f4SDmitry Torokhov 
1630f0d5c6f4SDmitry Torokhov 	if ((psmouse->badbyte & 0xc8) == 0x08) {
1631f0d5c6f4SDmitry Torokhov /*
1632f0d5c6f4SDmitry Torokhov  * Poll the track stick ...
1633f0d5c6f4SDmitry Torokhov  */
1634f0d5c6f4SDmitry Torokhov 		if (ps2_command(&psmouse->ps2dev, buf, PSMOUSE_CMD_POLL | (3 << 8)))
1635f0d5c6f4SDmitry Torokhov 			return -1;
1636f0d5c6f4SDmitry Torokhov 	}
1637f0d5c6f4SDmitry Torokhov 
1638f0d5c6f4SDmitry Torokhov 	memcpy(psmouse->packet, buf, sizeof(buf));
1639f0d5c6f4SDmitry Torokhov 	return 0;
1640f0d5c6f4SDmitry Torokhov }
1641f0d5c6f4SDmitry Torokhov 
164225bded7cSSeth Forshee static int alps_hw_init_v1_v2(struct psmouse *psmouse)
16431da177e4SLinus Torvalds {
16441da177e4SLinus Torvalds 	struct alps_data *priv = psmouse->private;
16451da177e4SLinus Torvalds 
164699df65e7SKevin Cernekee 	if ((priv->flags & ALPS_PASS) &&
164725bded7cSSeth Forshee 	    alps_passthrough_mode_v2(psmouse, true)) {
16481da177e4SLinus Torvalds 		return -1;
1649b7802c5cSDmitry Torokhov 	}
16501da177e4SLinus Torvalds 
1651b7802c5cSDmitry Torokhov 	if (alps_tap_mode(psmouse, true)) {
1652b5d21704SDmitry Torokhov 		psmouse_warn(psmouse, "Failed to enable hardware tapping\n");
16531da177e4SLinus Torvalds 		return -1;
1654963f626dSPeter Osterlund 	}
16551da177e4SLinus Torvalds 
165625bded7cSSeth Forshee 	if (alps_absolute_mode_v1_v2(psmouse)) {
1657b5d21704SDmitry Torokhov 		psmouse_err(psmouse, "Failed to enable absolute mode\n");
16581da177e4SLinus Torvalds 		return -1;
16591da177e4SLinus Torvalds 	}
16601da177e4SLinus Torvalds 
166199df65e7SKevin Cernekee 	if ((priv->flags & ALPS_PASS) &&
166225bded7cSSeth Forshee 	    alps_passthrough_mode_v2(psmouse, false)) {
16631da177e4SLinus Torvalds 		return -1;
1664b7802c5cSDmitry Torokhov 	}
16651da177e4SLinus Torvalds 
16661e0c5b12SDmitry Torokhov 	/* ALPS needs stream mode, otherwise it won't report any data */
16671e0c5b12SDmitry Torokhov 	if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)) {
1668b5d21704SDmitry Torokhov 		psmouse_err(psmouse, "Failed to enable stream mode\n");
16691e0c5b12SDmitry Torokhov 		return -1;
16701e0c5b12SDmitry Torokhov 	}
16711e0c5b12SDmitry Torokhov 
16721e0c5b12SDmitry Torokhov 	return 0;
16731e0c5b12SDmitry Torokhov }
16741e0c5b12SDmitry Torokhov 
167595f75e91SYunkang Tang static int alps_hw_init_v6(struct psmouse *psmouse)
167695f75e91SYunkang Tang {
167795f75e91SYunkang Tang 	unsigned char param[2] = {0xC8, 0x14};
167895f75e91SYunkang Tang 
167995f75e91SYunkang Tang 	/* Enter passthrough mode to let trackpoint enter 6byte raw mode */
168095f75e91SYunkang Tang 	if (alps_passthrough_mode_v2(psmouse, true))
168195f75e91SYunkang Tang 		return -1;
168295f75e91SYunkang Tang 
168395f75e91SYunkang Tang 	if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
168495f75e91SYunkang Tang 	    ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
168595f75e91SYunkang Tang 	    ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
168695f75e91SYunkang Tang 	    ps2_command(&psmouse->ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
168795f75e91SYunkang Tang 	    ps2_command(&psmouse->ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
168895f75e91SYunkang Tang 		return -1;
168995f75e91SYunkang Tang 
169095f75e91SYunkang Tang 	if (alps_passthrough_mode_v2(psmouse, false))
169195f75e91SYunkang Tang 		return -1;
169295f75e91SYunkang Tang 
169395f75e91SYunkang Tang 	if (alps_absolute_mode_v6(psmouse)) {
169495f75e91SYunkang Tang 		psmouse_err(psmouse, "Failed to enable absolute mode\n");
169595f75e91SYunkang Tang 		return -1;
169695f75e91SYunkang Tang 	}
169795f75e91SYunkang Tang 
169895f75e91SYunkang Tang 	return 0;
169995f75e91SYunkang Tang }
170095f75e91SYunkang Tang 
170125bded7cSSeth Forshee /*
1702cd401204SKevin Cernekee  * Enable or disable passthrough mode to the trackstick.
170325bded7cSSeth Forshee  */
1704cd401204SKevin Cernekee static int alps_passthrough_mode_v3(struct psmouse *psmouse,
1705cd401204SKevin Cernekee 				    int reg_base, bool enable)
170625bded7cSSeth Forshee {
1707cd401204SKevin Cernekee 	int reg_val, ret = -1;
170825bded7cSSeth Forshee 
1709d18e53fcSKevin Cernekee 	if (alps_enter_command_mode(psmouse))
171025bded7cSSeth Forshee 		return -1;
171125bded7cSSeth Forshee 
1712cd401204SKevin Cernekee 	reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x0008);
1713cd401204SKevin Cernekee 	if (reg_val == -1)
1714cd401204SKevin Cernekee 		goto error;
1715cd401204SKevin Cernekee 
171625bded7cSSeth Forshee 	if (enable)
171725bded7cSSeth Forshee 		reg_val |= 0x01;
171825bded7cSSeth Forshee 	else
171925bded7cSSeth Forshee 		reg_val &= ~0x01;
172025bded7cSSeth Forshee 
1721cd401204SKevin Cernekee 	ret = __alps_command_mode_write_reg(psmouse, reg_val);
172225bded7cSSeth Forshee 
1723cd401204SKevin Cernekee error:
1724cd401204SKevin Cernekee 	if (alps_exit_command_mode(psmouse))
1725cd401204SKevin Cernekee 		ret = -1;
1726cd401204SKevin Cernekee 	return ret;
172725bded7cSSeth Forshee }
172825bded7cSSeth Forshee 
172925bded7cSSeth Forshee /* Must be in command mode when calling this function */
173025bded7cSSeth Forshee static int alps_absolute_mode_v3(struct psmouse *psmouse)
173125bded7cSSeth Forshee {
173225bded7cSSeth Forshee 	int reg_val;
173325bded7cSSeth Forshee 
173425bded7cSSeth Forshee 	reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
173525bded7cSSeth Forshee 	if (reg_val == -1)
173625bded7cSSeth Forshee 		return -1;
173725bded7cSSeth Forshee 
173825bded7cSSeth Forshee 	reg_val |= 0x06;
173925bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, reg_val))
174025bded7cSSeth Forshee 		return -1;
174125bded7cSSeth Forshee 
174225bded7cSSeth Forshee 	return 0;
174325bded7cSSeth Forshee }
174425bded7cSSeth Forshee 
1745cd401204SKevin Cernekee static int alps_probe_trackstick_v3(struct psmouse *psmouse, int reg_base)
174625bded7cSSeth Forshee {
1747cd401204SKevin Cernekee 	int ret = -EIO, reg_val;
174825bded7cSSeth Forshee 
1749d18e53fcSKevin Cernekee 	if (alps_enter_command_mode(psmouse))
175025bded7cSSeth Forshee 		goto error;
175125bded7cSSeth Forshee 
1752cd401204SKevin Cernekee 	reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x08);
175325bded7cSSeth Forshee 	if (reg_val == -1)
175425bded7cSSeth Forshee 		goto error;
1755cd401204SKevin Cernekee 
1756cd401204SKevin Cernekee 	/* bit 7: trackstick is present */
1757cd401204SKevin Cernekee 	ret = reg_val & 0x80 ? 0 : -ENODEV;
1758cd401204SKevin Cernekee 
1759cd401204SKevin Cernekee error:
1760cd401204SKevin Cernekee 	alps_exit_command_mode(psmouse);
1761cd401204SKevin Cernekee 	return ret;
1762cd401204SKevin Cernekee }
1763cd401204SKevin Cernekee 
1764cd401204SKevin Cernekee static int alps_setup_trackstick_v3(struct psmouse *psmouse, int reg_base)
1765cd401204SKevin Cernekee {
1766cd401204SKevin Cernekee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
1767cd401204SKevin Cernekee 	int ret = 0;
1768cd401204SKevin Cernekee 	unsigned char param[4];
1769cd401204SKevin Cernekee 
1770cd401204SKevin Cernekee 	if (alps_passthrough_mode_v3(psmouse, reg_base, true))
1771cd401204SKevin Cernekee 		return -EIO;
177225bded7cSSeth Forshee 
177325bded7cSSeth Forshee 	/*
177425bded7cSSeth Forshee 	 * E7 report for the trackstick
177525bded7cSSeth Forshee 	 *
177625bded7cSSeth Forshee 	 * There have been reports of failures to seem to trace back
177725bded7cSSeth Forshee 	 * to the above trackstick check failing. When these occur
177825bded7cSSeth Forshee 	 * this E7 report fails, so when that happens we continue
177925bded7cSSeth Forshee 	 * with the assumption that there isn't a trackstick after
178025bded7cSSeth Forshee 	 * all.
178125bded7cSSeth Forshee 	 */
1782cd401204SKevin Cernekee 	if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_SETSCALE21, param)) {
178325bded7cSSeth Forshee 		psmouse_warn(psmouse, "trackstick E7 report failed\n");
1784cd401204SKevin Cernekee 		ret = -ENODEV;
178525bded7cSSeth Forshee 	} else {
178639fbe585SDmitry Torokhov 		psmouse_dbg(psmouse, "trackstick E7 report: %3ph\n", param);
178725bded7cSSeth Forshee 
178825bded7cSSeth Forshee 		/*
178925bded7cSSeth Forshee 		 * Not sure what this does, but it is absolutely
179025bded7cSSeth Forshee 		 * essential. Without it, the touchpad does not
179125bded7cSSeth Forshee 		 * work at all and the trackstick just emits normal
179225bded7cSSeth Forshee 		 * PS/2 packets.
179325bded7cSSeth Forshee 		 */
179425bded7cSSeth Forshee 		if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
179525bded7cSSeth Forshee 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
179625bded7cSSeth Forshee 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
179725bded7cSSeth Forshee 		    alps_command_mode_send_nibble(psmouse, 0x9) ||
179825bded7cSSeth Forshee 		    alps_command_mode_send_nibble(psmouse, 0x4)) {
179925bded7cSSeth Forshee 			psmouse_err(psmouse,
180025bded7cSSeth Forshee 				    "Error sending magic E6 sequence\n");
1801cd401204SKevin Cernekee 			ret = -EIO;
180225bded7cSSeth Forshee 			goto error;
180325bded7cSSeth Forshee 		}
180425bded7cSSeth Forshee 
1805cd401204SKevin Cernekee 		/*
1806cd401204SKevin Cernekee 		 * This ensures the trackstick packets are in the format
1807cd401204SKevin Cernekee 		 * supported by this driver. If bit 1 isn't set the packet
1808cd401204SKevin Cernekee 		 * format is different.
1809cd401204SKevin Cernekee 		 */
1810d18e53fcSKevin Cernekee 		if (alps_enter_command_mode(psmouse) ||
1811cd401204SKevin Cernekee 		    alps_command_mode_write_reg(psmouse,
1812cd401204SKevin Cernekee 						reg_base + 0x08, 0x82) ||
1813cd401204SKevin Cernekee 		    alps_exit_command_mode(psmouse))
1814cd401204SKevin Cernekee 			ret = -EIO;
1815cd401204SKevin Cernekee 	}
1816cd401204SKevin Cernekee 
1817cd401204SKevin Cernekee error:
1818cd401204SKevin Cernekee 	if (alps_passthrough_mode_v3(psmouse, reg_base, false))
1819cd401204SKevin Cernekee 		ret = -EIO;
1820cd401204SKevin Cernekee 
1821cd401204SKevin Cernekee 	return ret;
1822cd401204SKevin Cernekee }
1823cd401204SKevin Cernekee 
1824cd401204SKevin Cernekee static int alps_hw_init_v3(struct psmouse *psmouse)
1825cd401204SKevin Cernekee {
1826cd401204SKevin Cernekee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
1827cd401204SKevin Cernekee 	int reg_val;
1828cd401204SKevin Cernekee 	unsigned char param[4];
1829cd401204SKevin Cernekee 
1830cd401204SKevin Cernekee 	reg_val = alps_probe_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE);
1831cd401204SKevin Cernekee 	if (reg_val == -EIO)
1832cd401204SKevin Cernekee 		goto error;
183339fbe585SDmitry Torokhov 
1834cd401204SKevin Cernekee 	if (reg_val == 0 &&
1835cd401204SKevin Cernekee 	    alps_setup_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE) == -EIO)
1836cd401204SKevin Cernekee 		goto error;
1837cd401204SKevin Cernekee 
1838d18e53fcSKevin Cernekee 	if (alps_enter_command_mode(psmouse) ||
1839cd401204SKevin Cernekee 	    alps_absolute_mode_v3(psmouse)) {
184025bded7cSSeth Forshee 		psmouse_err(psmouse, "Failed to enter absolute mode\n");
184125bded7cSSeth Forshee 		goto error;
184225bded7cSSeth Forshee 	}
184325bded7cSSeth Forshee 
184425bded7cSSeth Forshee 	reg_val = alps_command_mode_read_reg(psmouse, 0x0006);
184525bded7cSSeth Forshee 	if (reg_val == -1)
184625bded7cSSeth Forshee 		goto error;
184725bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
184825bded7cSSeth Forshee 		goto error;
184925bded7cSSeth Forshee 
185025bded7cSSeth Forshee 	reg_val = alps_command_mode_read_reg(psmouse, 0x0007);
185125bded7cSSeth Forshee 	if (reg_val == -1)
185225bded7cSSeth Forshee 		goto error;
185325bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
185425bded7cSSeth Forshee 		goto error;
185525bded7cSSeth Forshee 
185625bded7cSSeth Forshee 	if (alps_command_mode_read_reg(psmouse, 0x0144) == -1)
185725bded7cSSeth Forshee 		goto error;
185825bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, 0x04))
185925bded7cSSeth Forshee 		goto error;
186025bded7cSSeth Forshee 
186125bded7cSSeth Forshee 	if (alps_command_mode_read_reg(psmouse, 0x0159) == -1)
186225bded7cSSeth Forshee 		goto error;
186325bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, 0x03))
186425bded7cSSeth Forshee 		goto error;
186525bded7cSSeth Forshee 
186625bded7cSSeth Forshee 	if (alps_command_mode_read_reg(psmouse, 0x0163) == -1)
186725bded7cSSeth Forshee 		goto error;
186825bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0163, 0x03))
186925bded7cSSeth Forshee 		goto error;
187025bded7cSSeth Forshee 
187125bded7cSSeth Forshee 	if (alps_command_mode_read_reg(psmouse, 0x0162) == -1)
187225bded7cSSeth Forshee 		goto error;
187325bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0162, 0x04))
187425bded7cSSeth Forshee 		goto error;
187525bded7cSSeth Forshee 
187625bded7cSSeth Forshee 	alps_exit_command_mode(psmouse);
187725bded7cSSeth Forshee 
187825bded7cSSeth Forshee 	/* Set rate and enable data reporting */
187925bded7cSSeth Forshee 	param[0] = 0x64;
188025bded7cSSeth Forshee 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
188125bded7cSSeth Forshee 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
188225bded7cSSeth Forshee 		psmouse_err(psmouse, "Failed to enable data reporting\n");
188325bded7cSSeth Forshee 		return -1;
188425bded7cSSeth Forshee 	}
188525bded7cSSeth Forshee 
188625bded7cSSeth Forshee 	return 0;
188725bded7cSSeth Forshee 
188825bded7cSSeth Forshee error:
188925bded7cSSeth Forshee 	/*
189025bded7cSSeth Forshee 	 * Leaving the touchpad in command mode will essentially render
189125bded7cSSeth Forshee 	 * it unusable until the machine reboots, so exit it here just
189225bded7cSSeth Forshee 	 * to be safe
189325bded7cSSeth Forshee 	 */
189425bded7cSSeth Forshee 	alps_exit_command_mode(psmouse);
189525bded7cSSeth Forshee 	return -1;
189625bded7cSSeth Forshee }
189725bded7cSSeth Forshee 
1898f3f33c67SHans de Goede static int alps_get_v3_v7_resolution(struct psmouse *psmouse, int reg_pitch)
1899f3f33c67SHans de Goede {
1900f3f33c67SHans de Goede 	int reg, x_pitch, y_pitch, x_electrode, y_electrode, x_phys, y_phys;
1901f3f33c67SHans de Goede 	struct alps_data *priv = psmouse->private;
1902f3f33c67SHans de Goede 
1903f3f33c67SHans de Goede 	reg = alps_command_mode_read_reg(psmouse, reg_pitch);
1904f3f33c67SHans de Goede 	if (reg < 0)
1905f3f33c67SHans de Goede 		return reg;
1906f3f33c67SHans de Goede 
1907f3f33c67SHans de Goede 	x_pitch = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
1908f3f33c67SHans de Goede 	x_pitch = 50 + 2 * x_pitch; /* In 0.1 mm units */
1909f3f33c67SHans de Goede 
1910f3f33c67SHans de Goede 	y_pitch = (char)reg >> 4; /* sign extend upper 4 bits */
1911f3f33c67SHans de Goede 	y_pitch = 36 + 2 * y_pitch; /* In 0.1 mm units */
1912f3f33c67SHans de Goede 
1913f3f33c67SHans de Goede 	reg = alps_command_mode_read_reg(psmouse, reg_pitch + 1);
1914f3f33c67SHans de Goede 	if (reg < 0)
1915f3f33c67SHans de Goede 		return reg;
1916f3f33c67SHans de Goede 
1917f3f33c67SHans de Goede 	x_electrode = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
1918f3f33c67SHans de Goede 	x_electrode = 17 + x_electrode;
1919f3f33c67SHans de Goede 
1920f3f33c67SHans de Goede 	y_electrode = (char)reg >> 4; /* sign extend upper 4 bits */
1921f3f33c67SHans de Goede 	y_electrode = 13 + y_electrode;
1922f3f33c67SHans de Goede 
1923f3f33c67SHans de Goede 	x_phys = x_pitch * (x_electrode - 1); /* In 0.1 mm units */
1924f3f33c67SHans de Goede 	y_phys = y_pitch * (y_electrode - 1); /* In 0.1 mm units */
1925f3f33c67SHans de Goede 
1926f3f33c67SHans de Goede 	priv->x_res = priv->x_max * 10 / x_phys; /* units / mm */
1927f3f33c67SHans de Goede 	priv->y_res = priv->y_max * 10 / y_phys; /* units / mm */
1928f3f33c67SHans de Goede 
1929f3f33c67SHans de Goede 	psmouse_dbg(psmouse,
1930f3f33c67SHans de Goede 		    "pitch %dx%d num-electrodes %dx%d physical size %dx%d mm res %dx%d\n",
1931f3f33c67SHans de Goede 		    x_pitch, y_pitch, x_electrode, y_electrode,
1932f3f33c67SHans de Goede 		    x_phys / 10, y_phys / 10, priv->x_res, priv->y_res);
1933f3f33c67SHans de Goede 
1934f3f33c67SHans de Goede 	return 0;
1935f3f33c67SHans de Goede }
1936f3f33c67SHans de Goede 
19371302bac3SKevin Cernekee static int alps_hw_init_rushmore_v3(struct psmouse *psmouse)
19381302bac3SKevin Cernekee {
1939cd401204SKevin Cernekee 	struct alps_data *priv = psmouse->private;
19401302bac3SKevin Cernekee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
19411302bac3SKevin Cernekee 	int reg_val, ret = -1;
19421302bac3SKevin Cernekee 
1943cd401204SKevin Cernekee 	if (priv->flags & ALPS_DUALPOINT) {
1944cd401204SKevin Cernekee 		reg_val = alps_setup_trackstick_v3(psmouse,
1945cd401204SKevin Cernekee 						   ALPS_REG_BASE_RUSHMORE);
1946cd401204SKevin Cernekee 		if (reg_val == -EIO)
1947cd401204SKevin Cernekee 			goto error;
1948cd401204SKevin Cernekee 		if (reg_val == -ENODEV)
1949cd401204SKevin Cernekee 			priv->flags &= ~ALPS_DUALPOINT;
1950cd401204SKevin Cernekee 	}
1951cd401204SKevin Cernekee 
1952d18e53fcSKevin Cernekee 	if (alps_enter_command_mode(psmouse) ||
19531302bac3SKevin Cernekee 	    alps_command_mode_read_reg(psmouse, 0xc2d9) == -1 ||
19541302bac3SKevin Cernekee 	    alps_command_mode_write_reg(psmouse, 0xc2cb, 0x00))
19551302bac3SKevin Cernekee 		goto error;
19561302bac3SKevin Cernekee 
1957f3f33c67SHans de Goede 	if (alps_get_v3_v7_resolution(psmouse, 0xc2da))
1958f3f33c67SHans de Goede 		goto error;
1959f3f33c67SHans de Goede 
19601302bac3SKevin Cernekee 	reg_val = alps_command_mode_read_reg(psmouse, 0xc2c6);
19611302bac3SKevin Cernekee 	if (reg_val == -1)
19621302bac3SKevin Cernekee 		goto error;
19631302bac3SKevin Cernekee 	if (__alps_command_mode_write_reg(psmouse, reg_val & 0xfd))
19641302bac3SKevin Cernekee 		goto error;
19651302bac3SKevin Cernekee 
19661302bac3SKevin Cernekee 	if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
19671302bac3SKevin Cernekee 		goto error;
19681302bac3SKevin Cernekee 
19691302bac3SKevin Cernekee 	/* enter absolute mode */
19701302bac3SKevin Cernekee 	reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
19711302bac3SKevin Cernekee 	if (reg_val == -1)
19721302bac3SKevin Cernekee 		goto error;
19731302bac3SKevin Cernekee 	if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
19741302bac3SKevin Cernekee 		goto error;
19751302bac3SKevin Cernekee 
19761302bac3SKevin Cernekee 	alps_exit_command_mode(psmouse);
19771302bac3SKevin Cernekee 	return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
19781302bac3SKevin Cernekee 
19791302bac3SKevin Cernekee error:
19801302bac3SKevin Cernekee 	alps_exit_command_mode(psmouse);
19811302bac3SKevin Cernekee 	return ret;
19821302bac3SKevin Cernekee }
19831302bac3SKevin Cernekee 
198425bded7cSSeth Forshee /* Must be in command mode when calling this function */
198525bded7cSSeth Forshee static int alps_absolute_mode_v4(struct psmouse *psmouse)
198625bded7cSSeth Forshee {
198725bded7cSSeth Forshee 	int reg_val;
198825bded7cSSeth Forshee 
198925bded7cSSeth Forshee 	reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
199025bded7cSSeth Forshee 	if (reg_val == -1)
199125bded7cSSeth Forshee 		return -1;
199225bded7cSSeth Forshee 
199325bded7cSSeth Forshee 	reg_val |= 0x02;
199425bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, reg_val))
199525bded7cSSeth Forshee 		return -1;
199625bded7cSSeth Forshee 
199725bded7cSSeth Forshee 	return 0;
199825bded7cSSeth Forshee }
199925bded7cSSeth Forshee 
200025bded7cSSeth Forshee static int alps_hw_init_v4(struct psmouse *psmouse)
200125bded7cSSeth Forshee {
200225bded7cSSeth Forshee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
200325bded7cSSeth Forshee 	unsigned char param[4];
200425bded7cSSeth Forshee 
2005d18e53fcSKevin Cernekee 	if (alps_enter_command_mode(psmouse))
200625bded7cSSeth Forshee 		goto error;
200725bded7cSSeth Forshee 
200825bded7cSSeth Forshee 	if (alps_absolute_mode_v4(psmouse)) {
200925bded7cSSeth Forshee 		psmouse_err(psmouse, "Failed to enter absolute mode\n");
201025bded7cSSeth Forshee 		goto error;
201125bded7cSSeth Forshee 	}
201225bded7cSSeth Forshee 
201325bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0007, 0x8c))
201425bded7cSSeth Forshee 		goto error;
201525bded7cSSeth Forshee 
201625bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0149, 0x03))
201725bded7cSSeth Forshee 		goto error;
201825bded7cSSeth Forshee 
201925bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0160, 0x03))
202025bded7cSSeth Forshee 		goto error;
202125bded7cSSeth Forshee 
202225bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x017f, 0x15))
202325bded7cSSeth Forshee 		goto error;
202425bded7cSSeth Forshee 
202525bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0151, 0x01))
202625bded7cSSeth Forshee 		goto error;
202725bded7cSSeth Forshee 
202825bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0168, 0x03))
202925bded7cSSeth Forshee 		goto error;
203025bded7cSSeth Forshee 
203125bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x014a, 0x03))
203225bded7cSSeth Forshee 		goto error;
203325bded7cSSeth Forshee 
203425bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0161, 0x03))
203525bded7cSSeth Forshee 		goto error;
203625bded7cSSeth Forshee 
203725bded7cSSeth Forshee 	alps_exit_command_mode(psmouse);
203825bded7cSSeth Forshee 
203925bded7cSSeth Forshee 	/*
204025bded7cSSeth Forshee 	 * This sequence changes the output from a 9-byte to an
204125bded7cSSeth Forshee 	 * 8-byte format. All the same data seems to be present,
204225bded7cSSeth Forshee 	 * just in a more compact format.
204325bded7cSSeth Forshee 	 */
204425bded7cSSeth Forshee 	param[0] = 0xc8;
204525bded7cSSeth Forshee 	param[1] = 0x64;
204625bded7cSSeth Forshee 	param[2] = 0x50;
204725bded7cSSeth Forshee 	if (ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
204825bded7cSSeth Forshee 	    ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE) ||
204925bded7cSSeth Forshee 	    ps2_command(ps2dev, &param[2], PSMOUSE_CMD_SETRATE) ||
205025bded7cSSeth Forshee 	    ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
205125bded7cSSeth Forshee 		return -1;
205225bded7cSSeth Forshee 
205325bded7cSSeth Forshee 	/* Set rate and enable data reporting */
205425bded7cSSeth Forshee 	param[0] = 0x64;
205525bded7cSSeth Forshee 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
205625bded7cSSeth Forshee 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
205725bded7cSSeth Forshee 		psmouse_err(psmouse, "Failed to enable data reporting\n");
205825bded7cSSeth Forshee 		return -1;
205925bded7cSSeth Forshee 	}
206025bded7cSSeth Forshee 
206125bded7cSSeth Forshee 	return 0;
206225bded7cSSeth Forshee 
206325bded7cSSeth Forshee error:
206425bded7cSSeth Forshee 	/*
206525bded7cSSeth Forshee 	 * Leaving the touchpad in command mode will essentially render
206625bded7cSSeth Forshee 	 * it unusable until the machine reboots, so exit it here just
206725bded7cSSeth Forshee 	 * to be safe
206825bded7cSSeth Forshee 	 */
206925bded7cSSeth Forshee 	alps_exit_command_mode(psmouse);
207025bded7cSSeth Forshee 	return -1;
207125bded7cSSeth Forshee }
207225bded7cSSeth Forshee 
2073ee65d4b3SYunkang Tang static int alps_dolphin_get_device_area(struct psmouse *psmouse,
2074ee65d4b3SYunkang Tang 					struct alps_data *priv)
2075ee65d4b3SYunkang Tang {
2076ee65d4b3SYunkang Tang 	struct ps2dev *ps2dev = &psmouse->ps2dev;
2077ee65d4b3SYunkang Tang 	unsigned char param[4] = {0};
2078ee65d4b3SYunkang Tang 	int num_x_electrode, num_y_electrode;
2079ee65d4b3SYunkang Tang 
2080ee65d4b3SYunkang Tang 	if (alps_enter_command_mode(psmouse))
2081ee65d4b3SYunkang Tang 		return -1;
2082ee65d4b3SYunkang Tang 
2083ee65d4b3SYunkang Tang 	param[0] = 0x0a;
2084ee65d4b3SYunkang Tang 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
2085ee65d4b3SYunkang Tang 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2086ee65d4b3SYunkang Tang 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2087ee65d4b3SYunkang Tang 	    ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2088ee65d4b3SYunkang Tang 	    ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE))
2089ee65d4b3SYunkang Tang 		return -1;
2090ee65d4b3SYunkang Tang 
2091ee65d4b3SYunkang Tang 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
2092ee65d4b3SYunkang Tang 		return -1;
2093ee65d4b3SYunkang Tang 
2094ee65d4b3SYunkang Tang 	/*
2095ee65d4b3SYunkang Tang 	 * Dolphin's sensor line number is not fixed. It can be calculated
2096ee65d4b3SYunkang Tang 	 * by adding the device's register value with DOLPHIN_PROFILE_X/YOFFSET.
2097ee65d4b3SYunkang Tang 	 * Further more, we can get device's x_max and y_max by multiplying
2098ee65d4b3SYunkang Tang 	 * sensor line number with DOLPHIN_COUNT_PER_ELECTRODE.
2099ee65d4b3SYunkang Tang 	 *
2100ee65d4b3SYunkang Tang 	 * e.g. When we get register's sensor_x = 11 & sensor_y = 8,
2101ee65d4b3SYunkang Tang 	 *	real sensor line number X = 11 + 8 = 19, and
2102ee65d4b3SYunkang Tang 	 *	real sensor line number Y = 8 + 1 = 9.
2103ee65d4b3SYunkang Tang 	 *	So, x_max = (19 - 1) * 64 = 1152, and
2104ee65d4b3SYunkang Tang 	 *	    y_max = (9 - 1) * 64 = 512.
2105ee65d4b3SYunkang Tang 	 */
2106ee65d4b3SYunkang Tang 	num_x_electrode = DOLPHIN_PROFILE_XOFFSET + (param[2] & 0x0F);
2107ee65d4b3SYunkang Tang 	num_y_electrode = DOLPHIN_PROFILE_YOFFSET + ((param[2] >> 4) & 0x0F);
2108ee65d4b3SYunkang Tang 	priv->x_bits = num_x_electrode;
2109ee65d4b3SYunkang Tang 	priv->y_bits = num_y_electrode;
2110ee65d4b3SYunkang Tang 	priv->x_max = (num_x_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
2111ee65d4b3SYunkang Tang 	priv->y_max = (num_y_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
2112ee65d4b3SYunkang Tang 
2113ee65d4b3SYunkang Tang 	if (alps_exit_command_mode(psmouse))
2114ee65d4b3SYunkang Tang 		return -1;
2115ee65d4b3SYunkang Tang 
2116ee65d4b3SYunkang Tang 	return 0;
2117ee65d4b3SYunkang Tang }
2118ee65d4b3SYunkang Tang 
211975af9e56SDave Turvene static int alps_hw_init_dolphin_v1(struct psmouse *psmouse)
212075af9e56SDave Turvene {
212175af9e56SDave Turvene 	struct ps2dev *ps2dev = &psmouse->ps2dev;
212275af9e56SDave Turvene 	unsigned char param[2];
212375af9e56SDave Turvene 
212475af9e56SDave Turvene 	/* This is dolphin "v1" as empirically defined by florin9doi */
212575af9e56SDave Turvene 	param[0] = 0x64;
212675af9e56SDave Turvene 	param[1] = 0x28;
212775af9e56SDave Turvene 
212875af9e56SDave Turvene 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
212975af9e56SDave Turvene 	    ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
213075af9e56SDave Turvene 	    ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
213175af9e56SDave Turvene 		return -1;
213275af9e56SDave Turvene 
213375af9e56SDave Turvene 	return 0;
213475af9e56SDave Turvene }
213575af9e56SDave Turvene 
21363808843cSYunkang Tang static int alps_hw_init_v7(struct psmouse *psmouse)
21373808843cSYunkang Tang {
21383808843cSYunkang Tang 	struct ps2dev *ps2dev = &psmouse->ps2dev;
21393808843cSYunkang Tang 	int reg_val, ret = -1;
21403808843cSYunkang Tang 
21413808843cSYunkang Tang 	if (alps_enter_command_mode(psmouse) ||
21423808843cSYunkang Tang 	    alps_command_mode_read_reg(psmouse, 0xc2d9) == -1)
21433808843cSYunkang Tang 		goto error;
21443808843cSYunkang Tang 
2145f3f33c67SHans de Goede 	if (alps_get_v3_v7_resolution(psmouse, 0xc397))
2146f3f33c67SHans de Goede 		goto error;
2147f3f33c67SHans de Goede 
21483808843cSYunkang Tang 	if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
21493808843cSYunkang Tang 		goto error;
21503808843cSYunkang Tang 
21513808843cSYunkang Tang 	reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
21523808843cSYunkang Tang 	if (reg_val == -1)
21533808843cSYunkang Tang 		goto error;
21543808843cSYunkang Tang 	if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
21553808843cSYunkang Tang 		goto error;
21563808843cSYunkang Tang 
21573808843cSYunkang Tang 	alps_exit_command_mode(psmouse);
21583808843cSYunkang Tang 	return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
21593808843cSYunkang Tang 
21603808843cSYunkang Tang error:
21613808843cSYunkang Tang 	alps_exit_command_mode(psmouse);
21623808843cSYunkang Tang 	return ret;
21633808843cSYunkang Tang }
21643808843cSYunkang Tang 
216524af5cb9SKevin Cernekee static void alps_set_defaults(struct alps_data *priv)
216625bded7cSSeth Forshee {
2167f673ceb1SKevin Cernekee 	priv->byte0 = 0x8f;
2168f673ceb1SKevin Cernekee 	priv->mask0 = 0x8f;
2169f673ceb1SKevin Cernekee 	priv->flags = ALPS_DUALPOINT;
2170f673ceb1SKevin Cernekee 
21717a9f73e7SKevin Cernekee 	priv->x_max = 2000;
21727a9f73e7SKevin Cernekee 	priv->y_max = 1400;
21737a9f73e7SKevin Cernekee 	priv->x_bits = 15;
21747a9f73e7SKevin Cernekee 	priv->y_bits = 11;
21757a9f73e7SKevin Cernekee 
217699df65e7SKevin Cernekee 	switch (priv->proto_version) {
217725bded7cSSeth Forshee 	case ALPS_PROTO_V1:
217825bded7cSSeth Forshee 	case ALPS_PROTO_V2:
217924af5cb9SKevin Cernekee 		priv->hw_init = alps_hw_init_v1_v2;
218024af5cb9SKevin Cernekee 		priv->process_packet = alps_process_packet_v1_v2;
218124af5cb9SKevin Cernekee 		priv->set_abs_params = alps_set_abs_params_st;
218295f75e91SYunkang Tang 		priv->x_max = 1023;
218395f75e91SYunkang Tang 		priv->y_max = 767;
218425bded7cSSeth Forshee 		break;
2185fb2dd7a6SDmitry Torokhov 
218625bded7cSSeth Forshee 	case ALPS_PROTO_V3:
218724af5cb9SKevin Cernekee 		priv->hw_init = alps_hw_init_v3;
218824af5cb9SKevin Cernekee 		priv->process_packet = alps_process_packet_v3;
218924af5cb9SKevin Cernekee 		priv->set_abs_params = alps_set_abs_params_mt;
2190f85e5001SKevin Cernekee 		priv->decode_fields = alps_decode_pinnacle;
219150e8b216SKevin Cernekee 		priv->nibble_commands = alps_v3_nibble_commands;
219250e8b216SKevin Cernekee 		priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
219325bded7cSSeth Forshee 		break;
2194fb2dd7a6SDmitry Torokhov 
2195fb2dd7a6SDmitry Torokhov 	case ALPS_PROTO_V3_RUSHMORE:
2196fb2dd7a6SDmitry Torokhov 		priv->hw_init = alps_hw_init_rushmore_v3;
2197fb2dd7a6SDmitry Torokhov 		priv->process_packet = alps_process_packet_v3;
2198fb2dd7a6SDmitry Torokhov 		priv->set_abs_params = alps_set_abs_params_mt;
2199fb2dd7a6SDmitry Torokhov 		priv->decode_fields = alps_decode_rushmore;
2200fb2dd7a6SDmitry Torokhov 		priv->nibble_commands = alps_v3_nibble_commands;
2201fb2dd7a6SDmitry Torokhov 		priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2202fb2dd7a6SDmitry Torokhov 		priv->x_bits = 16;
2203fb2dd7a6SDmitry Torokhov 		priv->y_bits = 12;
2204fb2dd7a6SDmitry Torokhov 		break;
2205fb2dd7a6SDmitry Torokhov 
220625bded7cSSeth Forshee 	case ALPS_PROTO_V4:
220724af5cb9SKevin Cernekee 		priv->hw_init = alps_hw_init_v4;
220824af5cb9SKevin Cernekee 		priv->process_packet = alps_process_packet_v4;
220924af5cb9SKevin Cernekee 		priv->set_abs_params = alps_set_abs_params_mt;
221050e8b216SKevin Cernekee 		priv->nibble_commands = alps_v4_nibble_commands;
221150e8b216SKevin Cernekee 		priv->addr_command = PSMOUSE_CMD_DISABLE;
221225bded7cSSeth Forshee 		break;
221375af9e56SDave Turvene 	case ALPS_PROTO_V5:
221475af9e56SDave Turvene 		priv->hw_init = alps_hw_init_dolphin_v1;
2215ee65d4b3SYunkang Tang 		priv->process_packet = alps_process_touchpad_packet_v3_v5;
221675af9e56SDave Turvene 		priv->decode_fields = alps_decode_dolphin;
221775af9e56SDave Turvene 		priv->set_abs_params = alps_set_abs_params_mt;
221875af9e56SDave Turvene 		priv->nibble_commands = alps_v3_nibble_commands;
221975af9e56SDave Turvene 		priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
222075af9e56SDave Turvene 		priv->byte0 = 0xc8;
2221ee65d4b3SYunkang Tang 		priv->mask0 = 0xd8;
222275af9e56SDave Turvene 		priv->flags = 0;
222375af9e56SDave Turvene 		priv->x_max = 1360;
222475af9e56SDave Turvene 		priv->y_max = 660;
222575af9e56SDave Turvene 		priv->x_bits = 23;
222675af9e56SDave Turvene 		priv->y_bits = 12;
222775af9e56SDave Turvene 		break;
222895f75e91SYunkang Tang 	case ALPS_PROTO_V6:
222995f75e91SYunkang Tang 		priv->hw_init = alps_hw_init_v6;
223095f75e91SYunkang Tang 		priv->process_packet = alps_process_packet_v6;
223195f75e91SYunkang Tang 		priv->set_abs_params = alps_set_abs_params_st;
223295f75e91SYunkang Tang 		priv->nibble_commands = alps_v6_nibble_commands;
223395f75e91SYunkang Tang 		priv->x_max = 2047;
223495f75e91SYunkang Tang 		priv->y_max = 1535;
223595f75e91SYunkang Tang 		break;
22363808843cSYunkang Tang 	case ALPS_PROTO_V7:
22373808843cSYunkang Tang 		priv->hw_init = alps_hw_init_v7;
22383808843cSYunkang Tang 		priv->process_packet = alps_process_packet_v7;
22393808843cSYunkang Tang 		priv->decode_fields = alps_decode_packet_v7;
22403808843cSYunkang Tang 		priv->set_abs_params = alps_set_abs_params_mt;
22413808843cSYunkang Tang 		priv->nibble_commands = alps_v3_nibble_commands;
22423808843cSYunkang Tang 		priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
22433808843cSYunkang Tang 		priv->x_max = 0xfff;
22443808843cSYunkang Tang 		priv->y_max = 0x7ff;
22453808843cSYunkang Tang 		priv->byte0 = 0x48;
22463808843cSYunkang Tang 		priv->mask0 = 0x48;
22473808843cSYunkang Tang 
22483808843cSYunkang Tang 		if (priv->fw_ver[1] != 0xba)
22493808843cSYunkang Tang 			priv->flags |= ALPS_BUTTONPAD;
22503808843cSYunkang Tang 		break;
225125bded7cSSeth Forshee 	}
225225bded7cSSeth Forshee }
225325bded7cSSeth Forshee 
2254b5d6b851SKevin Cernekee static int alps_match_table(struct psmouse *psmouse, struct alps_data *priv,
2255b5d6b851SKevin Cernekee 			    unsigned char *e7, unsigned char *ec)
22562e992cc0SKevin Cernekee {
2257b5d6b851SKevin Cernekee 	const struct alps_model_info *model;
22582e992cc0SKevin Cernekee 	int i;
22592e992cc0SKevin Cernekee 
2260b5d6b851SKevin Cernekee 	for (i = 0; i < ARRAY_SIZE(alps_model_data); i++) {
2261b5d6b851SKevin Cernekee 		model = &alps_model_data[i];
2262b5d6b851SKevin Cernekee 
2263b5d6b851SKevin Cernekee 		if (!memcmp(e7, model->signature, sizeof(model->signature)) &&
2264b5d6b851SKevin Cernekee 		    (!model->command_mode_resp ||
2265b5d6b851SKevin Cernekee 		     model->command_mode_resp == ec[2])) {
2266b5d6b851SKevin Cernekee 
2267b5d6b851SKevin Cernekee 			priv->proto_version = model->proto_version;
226824af5cb9SKevin Cernekee 			alps_set_defaults(priv);
226924af5cb9SKevin Cernekee 
2270b5d6b851SKevin Cernekee 			priv->flags = model->flags;
2271b5d6b851SKevin Cernekee 			priv->byte0 = model->byte0;
2272b5d6b851SKevin Cernekee 			priv->mask0 = model->mask0;
2273b5d6b851SKevin Cernekee 
2274b5d6b851SKevin Cernekee 			return 0;
2275b5d6b851SKevin Cernekee 		}
2276b5d6b851SKevin Cernekee 	}
2277b5d6b851SKevin Cernekee 
2278b5d6b851SKevin Cernekee 	return -EINVAL;
2279b5d6b851SKevin Cernekee }
2280b5d6b851SKevin Cernekee 
2281b5d6b851SKevin Cernekee static int alps_identify(struct psmouse *psmouse, struct alps_data *priv)
2282b5d6b851SKevin Cernekee {
2283b5d6b851SKevin Cernekee 	unsigned char e6[4], e7[4], ec[4];
2284b5d6b851SKevin Cernekee 
22852e992cc0SKevin Cernekee 	/*
22862e992cc0SKevin Cernekee 	 * First try "E6 report".
22872e992cc0SKevin Cernekee 	 * ALPS should return 0,0,10 or 0,0,100 if no buttons are pressed.
22882e992cc0SKevin Cernekee 	 * The bits 0-2 of the first byte will be 1s if some buttons are
22892e992cc0SKevin Cernekee 	 * pressed.
22902e992cc0SKevin Cernekee 	 */
2291b5d6b851SKevin Cernekee 	if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2292b5d6b851SKevin Cernekee 			 PSMOUSE_CMD_SETSCALE11, e6))
2293b5d6b851SKevin Cernekee 		return -EIO;
22942e992cc0SKevin Cernekee 
2295b5d6b851SKevin Cernekee 	if ((e6[0] & 0xf8) != 0 || e6[1] != 0 || (e6[2] != 10 && e6[2] != 100))
2296b5d6b851SKevin Cernekee 		return -EINVAL;
22972e992cc0SKevin Cernekee 
22982e992cc0SKevin Cernekee 	/*
2299b5d6b851SKevin Cernekee 	 * Now get the "E7" and "EC" reports.  These will uniquely identify
2300b5d6b851SKevin Cernekee 	 * most ALPS touchpads.
23012e992cc0SKevin Cernekee 	 */
2302b5d6b851SKevin Cernekee 	if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2303b5d6b851SKevin Cernekee 			 PSMOUSE_CMD_SETSCALE21, e7) ||
2304b5d6b851SKevin Cernekee 	    alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2305b5d6b851SKevin Cernekee 			 PSMOUSE_CMD_RESET_WRAP, ec) ||
2306b5d6b851SKevin Cernekee 	    alps_exit_command_mode(psmouse))
2307b5d6b851SKevin Cernekee 		return -EIO;
23082e992cc0SKevin Cernekee 
2309c0cd17f6SHans de Goede 	/* Save the Firmware version */
2310c0cd17f6SHans de Goede 	memcpy(priv->fw_ver, ec, 3);
2311c0cd17f6SHans de Goede 
2312f673ceb1SKevin Cernekee 	if (alps_match_table(psmouse, priv, e7, ec) == 0) {
2313b5d6b851SKevin Cernekee 		return 0;
231475af9e56SDave Turvene 	} else if (e7[0] == 0x73 && e7[1] == 0x03 && e7[2] == 0x50 &&
2315ee65d4b3SYunkang Tang 		   ec[0] == 0x73 && (ec[1] == 0x01 || ec[1] == 0x02)) {
231675af9e56SDave Turvene 		priv->proto_version = ALPS_PROTO_V5;
231775af9e56SDave Turvene 		alps_set_defaults(priv);
2318ee65d4b3SYunkang Tang 		if (alps_dolphin_get_device_area(psmouse, priv))
2319ee65d4b3SYunkang Tang 			return -EIO;
2320ee65d4b3SYunkang Tang 		else
232175af9e56SDave Turvene 			return 0;
23223808843cSYunkang Tang 	} else if (ec[0] == 0x88 &&
23233808843cSYunkang Tang 		   ((ec[1] & 0xf0) == 0xb0 || (ec[1] & 0xf0) == 0xc0)) {
23243808843cSYunkang Tang 		priv->proto_version = ALPS_PROTO_V7;
23253808843cSYunkang Tang 		alps_set_defaults(priv);
23263808843cSYunkang Tang 
23273808843cSYunkang Tang 		return 0;
23281302bac3SKevin Cernekee 	} else if (ec[0] == 0x88 && ec[1] == 0x08) {
2329fb2dd7a6SDmitry Torokhov 		priv->proto_version = ALPS_PROTO_V3_RUSHMORE;
23301302bac3SKevin Cernekee 		alps_set_defaults(priv);
23311302bac3SKevin Cernekee 
2332cd401204SKevin Cernekee 		/* hack to make addr_command, nibble_command available */
2333cd401204SKevin Cernekee 		psmouse->private = priv;
2334cd401204SKevin Cernekee 
2335cd401204SKevin Cernekee 		if (alps_probe_trackstick_v3(psmouse, ALPS_REG_BASE_RUSHMORE))
2336cd401204SKevin Cernekee 			priv->flags &= ~ALPS_DUALPOINT;
2337cd401204SKevin Cernekee 
23381302bac3SKevin Cernekee 		return 0;
2339f673ceb1SKevin Cernekee 	} else if (ec[0] == 0x88 && ec[1] == 0x07 &&
2340f673ceb1SKevin Cernekee 		   ec[2] >= 0x90 && ec[2] <= 0x9d) {
2341f673ceb1SKevin Cernekee 		priv->proto_version = ALPS_PROTO_V3;
2342f673ceb1SKevin Cernekee 		alps_set_defaults(priv);
2343f673ceb1SKevin Cernekee 
2344f673ceb1SKevin Cernekee 		return 0;
2345f673ceb1SKevin Cernekee 	}
23462e992cc0SKevin Cernekee 
2347a9e06219SDmitry Torokhov 	psmouse_dbg(psmouse,
2348a9e06219SDmitry Torokhov 		    "Likely not an ALPS touchpad: E7=%3ph, EC=%3ph\n", e7, ec);
23492e992cc0SKevin Cernekee 
2350b5d6b851SKevin Cernekee 	return -EINVAL;
23512e992cc0SKevin Cernekee }
23522e992cc0SKevin Cernekee 
23531e0c5b12SDmitry Torokhov static int alps_reconnect(struct psmouse *psmouse)
23541e0c5b12SDmitry Torokhov {
2355b5d6b851SKevin Cernekee 	struct alps_data *priv = psmouse->private;
235671bb21b6SMaxim Levitsky 
23571e0c5b12SDmitry Torokhov 	psmouse_reset(psmouse);
23581e0c5b12SDmitry Torokhov 
2359b5d6b851SKevin Cernekee 	if (alps_identify(psmouse, priv) < 0)
23601e0c5b12SDmitry Torokhov 		return -1;
23611e0c5b12SDmitry Torokhov 
236224af5cb9SKevin Cernekee 	return priv->hw_init(psmouse);
23631da177e4SLinus Torvalds }
23641da177e4SLinus Torvalds 
23651da177e4SLinus Torvalds static void alps_disconnect(struct psmouse *psmouse)
23661da177e4SLinus Torvalds {
23671da177e4SLinus Torvalds 	struct alps_data *priv = psmouse->private;
23682e5b636bSDmitry Torokhov 
23691da177e4SLinus Torvalds 	psmouse_reset(psmouse);
23701d9f2626SSebastian Kapfer 	del_timer_sync(&priv->timer);
23712e5b636bSDmitry Torokhov 	input_unregister_device(priv->dev2);
23721da177e4SLinus Torvalds 	kfree(priv);
23731da177e4SLinus Torvalds }
23741da177e4SLinus Torvalds 
237524af5cb9SKevin Cernekee static void alps_set_abs_params_st(struct alps_data *priv,
237624af5cb9SKevin Cernekee 				   struct input_dev *dev1)
237724af5cb9SKevin Cernekee {
237895f75e91SYunkang Tang 	input_set_abs_params(dev1, ABS_X, 0, priv->x_max, 0, 0);
237995f75e91SYunkang Tang 	input_set_abs_params(dev1, ABS_Y, 0, priv->y_max, 0, 0);
238024af5cb9SKevin Cernekee }
238124af5cb9SKevin Cernekee 
238224af5cb9SKevin Cernekee static void alps_set_abs_params_mt(struct alps_data *priv,
238324af5cb9SKevin Cernekee 				   struct input_dev *dev1)
238424af5cb9SKevin Cernekee {
23857a9f73e7SKevin Cernekee 	input_set_abs_params(dev1, ABS_MT_POSITION_X, 0, priv->x_max, 0, 0);
23867a9f73e7SKevin Cernekee 	input_set_abs_params(dev1, ABS_MT_POSITION_Y, 0, priv->y_max, 0, 0);
238724af5cb9SKevin Cernekee 
2388f3f33c67SHans de Goede 	input_abs_set_res(dev1, ABS_MT_POSITION_X, priv->x_res);
2389f3f33c67SHans de Goede 	input_abs_set_res(dev1, ABS_MT_POSITION_Y, priv->y_res);
2390f3f33c67SHans de Goede 
2391cdf333efSHans de Goede 	input_mt_init_slots(dev1, MAX_TOUCHES, INPUT_MT_POINTER |
2392cdf333efSHans de Goede 		INPUT_MT_DROP_UNUSED | INPUT_MT_TRACK | INPUT_MT_SEMI_MT);
2393cdf333efSHans de Goede 
239424af5cb9SKevin Cernekee 	set_bit(BTN_TOOL_TRIPLETAP, dev1->keybit);
239524af5cb9SKevin Cernekee 	set_bit(BTN_TOOL_QUADTAP, dev1->keybit);
23963808843cSYunkang Tang 
23973808843cSYunkang Tang 	/* V7 is real multi-touch */
23983808843cSYunkang Tang 	if (priv->proto_version == ALPS_PROTO_V7)
23993808843cSYunkang Tang 		clear_bit(INPUT_PROP_SEMI_MT, dev1->propbit);
240024af5cb9SKevin Cernekee }
240124af5cb9SKevin Cernekee 
24021da177e4SLinus Torvalds int alps_init(struct psmouse *psmouse)
24031da177e4SLinus Torvalds {
24041da177e4SLinus Torvalds 	struct alps_data *priv;
24052e5b636bSDmitry Torokhov 	struct input_dev *dev1 = psmouse->dev, *dev2;
24061da177e4SLinus Torvalds 
2407f42649e8SDmitry Torokhov 	priv = kzalloc(sizeof(struct alps_data), GFP_KERNEL);
24082e5b636bSDmitry Torokhov 	dev2 = input_allocate_device();
24092e5b636bSDmitry Torokhov 	if (!priv || !dev2)
24101da177e4SLinus Torvalds 		goto init_fail;
24112e5b636bSDmitry Torokhov 
24122e5b636bSDmitry Torokhov 	priv->dev2 = dev2;
24131d9f2626SSebastian Kapfer 	setup_timer(&priv->timer, alps_flush_packet, (unsigned long)psmouse);
24141d9f2626SSebastian Kapfer 
24151e0c5b12SDmitry Torokhov 	psmouse->private = priv;
24161da177e4SLinus Torvalds 
241725bded7cSSeth Forshee 	psmouse_reset(psmouse);
241825bded7cSSeth Forshee 
2419b5d6b851SKevin Cernekee 	if (alps_identify(psmouse, priv) < 0)
242071bb21b6SMaxim Levitsky 		goto init_fail;
242171bb21b6SMaxim Levitsky 
242224af5cb9SKevin Cernekee 	if (priv->hw_init(psmouse))
24231da177e4SLinus Torvalds 		goto init_fail;
24241da177e4SLinus Torvalds 
24257105d2eaSDmitry Torokhov 	/*
24267105d2eaSDmitry Torokhov 	 * Undo part of setup done for us by psmouse core since touchpad
24277105d2eaSDmitry Torokhov 	 * is not a relative device.
24287105d2eaSDmitry Torokhov 	 */
24297105d2eaSDmitry Torokhov 	__clear_bit(EV_REL, dev1->evbit);
24307105d2eaSDmitry Torokhov 	__clear_bit(REL_X, dev1->relbit);
24317105d2eaSDmitry Torokhov 	__clear_bit(REL_Y, dev1->relbit);
24327105d2eaSDmitry Torokhov 
24337105d2eaSDmitry Torokhov 	/*
24347105d2eaSDmitry Torokhov 	 * Now set up our capabilities.
24357105d2eaSDmitry Torokhov 	 */
24367b19ada2SJiri Slaby 	dev1->evbit[BIT_WORD(EV_KEY)] |= BIT_MASK(EV_KEY);
24377b19ada2SJiri Slaby 	dev1->keybit[BIT_WORD(BTN_TOUCH)] |= BIT_MASK(BTN_TOUCH);
24387b19ada2SJiri Slaby 	dev1->keybit[BIT_WORD(BTN_TOOL_FINGER)] |= BIT_MASK(BTN_TOOL_FINGER);
243971bb21b6SMaxim Levitsky 	dev1->keybit[BIT_WORD(BTN_LEFT)] |=
244071bb21b6SMaxim Levitsky 		BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
24411da177e4SLinus Torvalds 
24427b19ada2SJiri Slaby 	dev1->evbit[BIT_WORD(EV_ABS)] |= BIT_MASK(EV_ABS);
244325bded7cSSeth Forshee 
244424af5cb9SKevin Cernekee 	priv->set_abs_params(priv, dev1);
24453808843cSYunkang Tang 	/* No pressure on V7 */
24463808843cSYunkang Tang 	if (priv->proto_version != ALPS_PROTO_V7)
24472e5b636bSDmitry Torokhov 		input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
24481da177e4SLinus Torvalds 
244999df65e7SKevin Cernekee 	if (priv->flags & ALPS_WHEEL) {
24507b19ada2SJiri Slaby 		dev1->evbit[BIT_WORD(EV_REL)] |= BIT_MASK(EV_REL);
24517b19ada2SJiri Slaby 		dev1->relbit[BIT_WORD(REL_WHEEL)] |= BIT_MASK(REL_WHEEL);
24521da177e4SLinus Torvalds 	}
24531da177e4SLinus Torvalds 
245499df65e7SKevin Cernekee 	if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
24557b19ada2SJiri Slaby 		dev1->keybit[BIT_WORD(BTN_FORWARD)] |= BIT_MASK(BTN_FORWARD);
24567b19ada2SJiri Slaby 		dev1->keybit[BIT_WORD(BTN_BACK)] |= BIT_MASK(BTN_BACK);
24571da177e4SLinus Torvalds 	}
24581da177e4SLinus Torvalds 
245999df65e7SKevin Cernekee 	if (priv->flags & ALPS_FOUR_BUTTONS) {
246071bb21b6SMaxim Levitsky 		dev1->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_0);
246171bb21b6SMaxim Levitsky 		dev1->keybit[BIT_WORD(BTN_1)] |= BIT_MASK(BTN_1);
246271bb21b6SMaxim Levitsky 		dev1->keybit[BIT_WORD(BTN_2)] |= BIT_MASK(BTN_2);
246371bb21b6SMaxim Levitsky 		dev1->keybit[BIT_WORD(BTN_3)] |= BIT_MASK(BTN_3);
24643808843cSYunkang Tang 	} else if (priv->flags & ALPS_BUTTONPAD) {
24653808843cSYunkang Tang 		set_bit(INPUT_PROP_BUTTONPAD, dev1->propbit);
24663808843cSYunkang Tang 		clear_bit(BTN_RIGHT, dev1->keybit);
246771bb21b6SMaxim Levitsky 	} else {
246871bb21b6SMaxim Levitsky 		dev1->keybit[BIT_WORD(BTN_MIDDLE)] |= BIT_MASK(BTN_MIDDLE);
246971bb21b6SMaxim Levitsky 	}
247071bb21b6SMaxim Levitsky 
2471dfba8600SPali Rohár 	if (priv->flags & ALPS_DUALPOINT) {
2472dfba8600SPali Rohár 		/*
2473dfba8600SPali Rohár 		 * format of input device name is: "protocol vendor name"
2474dfba8600SPali Rohár 		 * see function psmouse_switch_protocol() in psmouse-base.c
2475dfba8600SPali Rohár 		 */
2476dfba8600SPali Rohár 		dev2->name = "AlpsPS/2 ALPS DualPoint Stick";
2477dfba8600SPali Rohár 		dev2->id.product = PSMOUSE_ALPS;
2478dfba8600SPali Rohár 		dev2->id.version = priv->proto_version;
2479dfba8600SPali Rohár 	} else {
2480dfba8600SPali Rohár 		dev2->name = "PS/2 ALPS Mouse";
2481dfba8600SPali Rohár 		dev2->id.product = PSMOUSE_PS2;
2482dfba8600SPali Rohár 		dev2->id.version = 0x0000;
2483dfba8600SPali Rohár 	}
2484dfba8600SPali Rohár 
248508ffce45SDmitry Torokhov 	snprintf(priv->phys, sizeof(priv->phys), "%s/input1", psmouse->ps2dev.serio->phys);
24862e5b636bSDmitry Torokhov 	dev2->phys = priv->phys;
24872e5b636bSDmitry Torokhov 	dev2->id.bustype = BUS_I8042;
24882e5b636bSDmitry Torokhov 	dev2->id.vendor  = 0x0002;
24891db3a345SDmitry Torokhov 	dev2->dev.parent = &psmouse->ps2dev.serio->dev;
24901da177e4SLinus Torvalds 
24917b19ada2SJiri Slaby 	dev2->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
249271bb21b6SMaxim Levitsky 	dev2->relbit[BIT_WORD(REL_X)] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
249371bb21b6SMaxim Levitsky 	dev2->keybit[BIT_WORD(BTN_LEFT)] =
249471bb21b6SMaxim Levitsky 		BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_MIDDLE) | BIT_MASK(BTN_RIGHT);
24951da177e4SLinus Torvalds 
249601d4cd5cSHans de Goede 	__set_bit(INPUT_PROP_POINTER, dev2->propbit);
24977611392fSHans de Goede 	if (priv->flags & ALPS_DUALPOINT)
24987611392fSHans de Goede 		__set_bit(INPUT_PROP_POINTING_STICK, dev2->propbit);
24997611392fSHans de Goede 
2500f42649e8SDmitry Torokhov 	if (input_register_device(priv->dev2))
2501f42649e8SDmitry Torokhov 		goto init_fail;
25021da177e4SLinus Torvalds 
25031da177e4SLinus Torvalds 	psmouse->protocol_handler = alps_process_byte;
2504f0d5c6f4SDmitry Torokhov 	psmouse->poll = alps_poll;
25051da177e4SLinus Torvalds 	psmouse->disconnect = alps_disconnect;
25061da177e4SLinus Torvalds 	psmouse->reconnect = alps_reconnect;
250799df65e7SKevin Cernekee 	psmouse->pktsize = priv->proto_version == ALPS_PROTO_V4 ? 8 : 6;
25081da177e4SLinus Torvalds 
2509f0d5c6f4SDmitry Torokhov 	/* We are having trouble resyncing ALPS touchpads so disable it for now */
2510f0d5c6f4SDmitry Torokhov 	psmouse->resync_time = 0;
2511f0d5c6f4SDmitry Torokhov 
25129d720b34SPali Rohár 	/* Allow 2 invalid packets without resetting device */
25139d720b34SPali Rohár 	psmouse->resetafter = psmouse->pktsize * 2;
25149d720b34SPali Rohár 
25151da177e4SLinus Torvalds 	return 0;
25161da177e4SLinus Torvalds 
25171da177e4SLinus Torvalds init_fail:
2518f42649e8SDmitry Torokhov 	psmouse_reset(psmouse);
25192e5b636bSDmitry Torokhov 	input_free_device(dev2);
25201da177e4SLinus Torvalds 	kfree(priv);
25211e0c5b12SDmitry Torokhov 	psmouse->private = NULL;
25221da177e4SLinus Torvalds 	return -1;
25231da177e4SLinus Torvalds }
25241da177e4SLinus Torvalds 
2525b7802c5cSDmitry Torokhov int alps_detect(struct psmouse *psmouse, bool set_properties)
25261da177e4SLinus Torvalds {
2527b5d6b851SKevin Cernekee 	struct alps_data dummy;
25281da177e4SLinus Torvalds 
2529b5d6b851SKevin Cernekee 	if (alps_identify(psmouse, &dummy) < 0)
25301da177e4SLinus Torvalds 		return -1;
25311da177e4SLinus Torvalds 
25321da177e4SLinus Torvalds 	if (set_properties) {
25331da177e4SLinus Torvalds 		psmouse->vendor = "ALPS";
2534b5d6b851SKevin Cernekee 		psmouse->name = dummy.flags & ALPS_DUALPOINT ?
2535968ac842SDmitry Torokhov 				"DualPoint TouchPad" : "GlidePoint";
2536d7c13d34SDmitry Torokhov 		psmouse->model = dummy.proto_version;
25371da177e4SLinus Torvalds 	}
25381da177e4SLinus Torvalds 	return 0;
25391da177e4SLinus Torvalds }
25401da177e4SLinus Torvalds 
2541