xref: /openbmc/linux/drivers/input/mouse/alps.c (revision 92bac83d)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * ALPS touchpad PS/2 mouse driver
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Copyright (c) 2003 Neil Brown <neilb@cse.unsw.edu.au>
5963f626dSPeter Osterlund  * Copyright (c) 2003-2005 Peter Osterlund <petero2@telia.com>
61da177e4SLinus Torvalds  * Copyright (c) 2004 Dmitry Torokhov <dtor@mail.ru>
71da177e4SLinus Torvalds  * Copyright (c) 2005 Vojtech Pavlik <vojtech@suse.cz>
81d9f2626SSebastian Kapfer  * Copyright (c) 2009 Sebastian Kapfer <sebastian_kapfer@gmx.net>
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  * ALPS detection, tap switching and status querying info is taken from
111da177e4SLinus Torvalds  * tpconfig utility (by C. Scott Ananian and Bruce Kall).
121da177e4SLinus Torvalds  *
131da177e4SLinus Torvalds  * This program is free software; you can redistribute it and/or modify it
141da177e4SLinus Torvalds  * under the terms of the GNU General Public License version 2 as published by
151da177e4SLinus Torvalds  * the Free Software Foundation.
161da177e4SLinus Torvalds  */
171da177e4SLinus Torvalds 
185a0e3ad6STejun Heo #include <linux/slab.h>
191da177e4SLinus Torvalds #include <linux/input.h>
2001ce661fSSeth Forshee #include <linux/input/mt.h>
211da177e4SLinus Torvalds #include <linux/serio.h>
221da177e4SLinus Torvalds #include <linux/libps2.h>
231da177e4SLinus Torvalds 
241da177e4SLinus Torvalds #include "psmouse.h"
251da177e4SLinus Torvalds #include "alps.h"
261da177e4SLinus Torvalds 
2725bded7cSSeth Forshee /*
2825bded7cSSeth Forshee  * Definitions for ALPS version 3 and 4 command mode protocol
2925bded7cSSeth Forshee  */
3025bded7cSSeth Forshee #define ALPS_CMD_NIBBLE_10	0x01f2
3125bded7cSSeth Forshee 
32cd401204SKevin Cernekee #define ALPS_REG_BASE_RUSHMORE	0xc2c0
33cd401204SKevin Cernekee #define ALPS_REG_BASE_PINNACLE	0x0000
34cd401204SKevin Cernekee 
3525bded7cSSeth Forshee static const struct alps_nibble_commands alps_v3_nibble_commands[] = {
3625bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETPOLL,		0x00 }, /* 0 */
3725bded7cSSeth Forshee 	{ PSMOUSE_CMD_RESET_DIS,	0x00 }, /* 1 */
3825bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETSCALE21,	0x00 }, /* 2 */
3925bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0x0a }, /* 3 */
4025bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0x14 }, /* 4 */
4125bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0x28 }, /* 5 */
4225bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0x3c }, /* 6 */
4325bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0x50 }, /* 7 */
4425bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0x64 }, /* 8 */
4525bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0xc8 }, /* 9 */
4625bded7cSSeth Forshee 	{ ALPS_CMD_NIBBLE_10,		0x00 }, /* a */
4725bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRES,		0x00 }, /* b */
4825bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRES,		0x01 }, /* c */
4925bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRES,		0x02 }, /* d */
5025bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRES,		0x03 }, /* e */
5125bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETSCALE11,	0x00 }, /* f */
5225bded7cSSeth Forshee };
5325bded7cSSeth Forshee 
5425bded7cSSeth Forshee static const struct alps_nibble_commands alps_v4_nibble_commands[] = {
5525bded7cSSeth Forshee 	{ PSMOUSE_CMD_ENABLE,		0x00 }, /* 0 */
5625bded7cSSeth Forshee 	{ PSMOUSE_CMD_RESET_DIS,	0x00 }, /* 1 */
5725bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETSCALE21,	0x00 }, /* 2 */
5825bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0x0a }, /* 3 */
5925bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0x14 }, /* 4 */
6025bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0x28 }, /* 5 */
6125bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0x3c }, /* 6 */
6225bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0x50 }, /* 7 */
6325bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0x64 }, /* 8 */
6425bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0xc8 }, /* 9 */
6525bded7cSSeth Forshee 	{ ALPS_CMD_NIBBLE_10,		0x00 }, /* a */
6625bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRES,		0x00 }, /* b */
6725bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRES,		0x01 }, /* c */
6825bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRES,		0x02 }, /* d */
6925bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRES,		0x03 }, /* e */
7025bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETSCALE11,	0x00 }, /* f */
7125bded7cSSeth Forshee };
7225bded7cSSeth Forshee 
7395f75e91SYunkang Tang static const struct alps_nibble_commands alps_v6_nibble_commands[] = {
7495f75e91SYunkang Tang 	{ PSMOUSE_CMD_ENABLE,		0x00 }, /* 0 */
7595f75e91SYunkang Tang 	{ PSMOUSE_CMD_SETRATE,		0x0a }, /* 1 */
7695f75e91SYunkang Tang 	{ PSMOUSE_CMD_SETRATE,		0x14 }, /* 2 */
7795f75e91SYunkang Tang 	{ PSMOUSE_CMD_SETRATE,		0x28 }, /* 3 */
7895f75e91SYunkang Tang 	{ PSMOUSE_CMD_SETRATE,		0x3c }, /* 4 */
7995f75e91SYunkang Tang 	{ PSMOUSE_CMD_SETRATE,		0x50 }, /* 5 */
8095f75e91SYunkang Tang 	{ PSMOUSE_CMD_SETRATE,		0x64 }, /* 6 */
8195f75e91SYunkang Tang 	{ PSMOUSE_CMD_SETRATE,		0xc8 }, /* 7 */
8295f75e91SYunkang Tang 	{ PSMOUSE_CMD_GETID,		0x00 }, /* 8 */
8395f75e91SYunkang Tang 	{ PSMOUSE_CMD_GETINFO,		0x00 }, /* 9 */
8495f75e91SYunkang Tang 	{ PSMOUSE_CMD_SETRES,		0x00 }, /* a */
8595f75e91SYunkang Tang 	{ PSMOUSE_CMD_SETRES,		0x01 }, /* b */
8695f75e91SYunkang Tang 	{ PSMOUSE_CMD_SETRES,		0x02 }, /* c */
8795f75e91SYunkang Tang 	{ PSMOUSE_CMD_SETRES,		0x03 }, /* d */
8895f75e91SYunkang Tang 	{ PSMOUSE_CMD_SETSCALE21,	0x00 }, /* e */
8995f75e91SYunkang Tang 	{ PSMOUSE_CMD_SETSCALE11,	0x00 }, /* f */
9095f75e91SYunkang Tang };
9195f75e91SYunkang Tang 
9225bded7cSSeth Forshee 
9371bb21b6SMaxim Levitsky #define ALPS_DUALPOINT		0x02	/* touchpad has trackstick */
9471bb21b6SMaxim Levitsky #define ALPS_PASS		0x04	/* device has a pass-through port */
9571bb21b6SMaxim Levitsky 
9671bb21b6SMaxim Levitsky #define ALPS_WHEEL		0x08	/* hardware wheel present */
9771bb21b6SMaxim Levitsky #define ALPS_FW_BK_1		0x10	/* front & back buttons present */
9871bb21b6SMaxim Levitsky #define ALPS_FW_BK_2		0x20	/* front & back buttons present */
9971bb21b6SMaxim Levitsky #define ALPS_FOUR_BUTTONS	0x40	/* 4 direction button present */
1001d9f2626SSebastian Kapfer #define ALPS_PS2_INTERLEAVED	0x80	/* 3-byte PS/2 packet interleaved with
1011d9f2626SSebastian Kapfer 					   6-byte ALPS packet */
1023808843cSYunkang Tang #define ALPS_BUTTONPAD		0x200	/* device is a clickpad */
1031da177e4SLinus Torvalds 
104e38de678SHelge Deller static const struct alps_model_info alps_model_data[] = {
1058326bb57SDmitry Torokhov 	{ { 0x32, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } },	/* Toshiba Salellite Pro M10 */
1068326bb57SDmitry Torokhov 	{ { 0x33, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V1, 0x88, 0xf8, 0 } },				/* UMAX-530T */
1078326bb57SDmitry Torokhov 	{ { 0x53, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
1088326bb57SDmitry Torokhov 	{ { 0x53, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
1098326bb57SDmitry Torokhov 	{ { 0x60, 0x03, 0xc8 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },				/* HP ze1115 */
1108326bb57SDmitry Torokhov 	{ { 0x63, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
1118326bb57SDmitry Torokhov 	{ { 0x63, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
1128326bb57SDmitry Torokhov 	{ { 0x63, 0x02, 0x28 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 } },		/* Fujitsu Siemens S6010 */
1138326bb57SDmitry Torokhov 	{ { 0x63, 0x02, 0x3c }, 0x00, { ALPS_PROTO_V2, 0x8f, 0x8f, ALPS_WHEEL } },		/* Toshiba Satellite S2400-103 */
1148326bb57SDmitry Torokhov 	{ { 0x63, 0x02, 0x50 }, 0x00, { ALPS_PROTO_V2, 0xef, 0xef, ALPS_FW_BK_1 } },		/* NEC Versa L320 */
1158326bb57SDmitry Torokhov 	{ { 0x63, 0x02, 0x64 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
1168326bb57SDmitry Torokhov 	{ { 0x63, 0x03, 0xc8 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } },	/* Dell Latitude D800 */
1178326bb57SDmitry Torokhov 	{ { 0x73, 0x00, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_DUALPOINT } },		/* ThinkPad R61 8918-5QG */
1188326bb57SDmitry Torokhov 	{ { 0x73, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
1198326bb57SDmitry Torokhov 	{ { 0x73, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 } },		/* Ahtec Laptop */
120626b9da0SDmitry Torokhov 
121626b9da0SDmitry Torokhov 	/*
122626b9da0SDmitry Torokhov 	 * XXX This entry is suspicious. First byte has zero lower nibble,
123626b9da0SDmitry Torokhov 	 * which is what a normal mouse would report. Also, the value 0x0e
124626b9da0SDmitry Torokhov 	 * isn't valid per PS/2 spec.
125626b9da0SDmitry Torokhov 	 */
126626b9da0SDmitry Torokhov 	{ { 0x20, 0x02, 0x0e }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } },
127626b9da0SDmitry Torokhov 
1288326bb57SDmitry Torokhov 	{ { 0x22, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } },
1298326bb57SDmitry Torokhov 	{ { 0x22, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xff, 0xff, ALPS_PASS | ALPS_DUALPOINT } },	/* Dell Latitude D600 */
1301d9f2626SSebastian Kapfer 	/* Dell Latitude E5500, E6400, E6500, Precision M4400 */
1318326bb57SDmitry Torokhov 	{ { 0x62, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xcf, 0xcf,
1328326bb57SDmitry Torokhov 		ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED } },
1338326bb57SDmitry Torokhov 	{ { 0x73, 0x00, 0x14 }, 0x00, { ALPS_PROTO_V6, 0xff, 0xff, ALPS_DUALPOINT } },		/* Dell XT2 */
1348326bb57SDmitry Torokhov 	{ { 0x73, 0x02, 0x50 }, 0x00, { ALPS_PROTO_V2, 0xcf, 0xcf, ALPS_FOUR_BUTTONS } },	/* Dell Vostro 1400 */
1358326bb57SDmitry Torokhov 	{ { 0x52, 0x01, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xff, 0xff,
1368326bb57SDmitry Torokhov 		ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED } },				/* Toshiba Tecra A11-11L */
1378326bb57SDmitry Torokhov 	{ { 0x73, 0x02, 0x64 }, 0x8a, { ALPS_PROTO_V4, 0x8f, 0x8f, 0 } },
1381da177e4SLinus Torvalds };
1391da177e4SLinus Torvalds 
1403296f71cSDmitry Torokhov static const struct alps_protocol_info alps_v3_protocol_data = {
1413296f71cSDmitry Torokhov 	ALPS_PROTO_V3, 0x8f, 0x8f, ALPS_DUALPOINT
1423296f71cSDmitry Torokhov };
1433296f71cSDmitry Torokhov 
1443296f71cSDmitry Torokhov static const struct alps_protocol_info alps_v3_rushmore_data = {
1453296f71cSDmitry Torokhov 	ALPS_PROTO_V3_RUSHMORE, 0x8f, 0x8f, ALPS_DUALPOINT
1463296f71cSDmitry Torokhov };
1473296f71cSDmitry Torokhov 
1483296f71cSDmitry Torokhov static const struct alps_protocol_info alps_v5_protocol_data = {
1493296f71cSDmitry Torokhov 	ALPS_PROTO_V5, 0xc8, 0xd8, 0
1503296f71cSDmitry Torokhov };
1513296f71cSDmitry Torokhov 
1523296f71cSDmitry Torokhov static const struct alps_protocol_info alps_v7_protocol_data = {
1533296f71cSDmitry Torokhov 	ALPS_PROTO_V7, 0x48, 0x48, ALPS_DUALPOINT
1543296f71cSDmitry Torokhov };
1553296f71cSDmitry Torokhov 
15624af5cb9SKevin Cernekee static void alps_set_abs_params_st(struct alps_data *priv,
15724af5cb9SKevin Cernekee 				   struct input_dev *dev1);
15824af5cb9SKevin Cernekee static void alps_set_abs_params_mt(struct alps_data *priv,
15924af5cb9SKevin Cernekee 				   struct input_dev *dev1);
16024af5cb9SKevin Cernekee 
161d4b347b2SSeth Forshee /* Packet formats are described in Documentation/input/alps.txt */
1621da177e4SLinus Torvalds 
16399df65e7SKevin Cernekee static bool alps_is_valid_first_byte(struct alps_data *priv,
1641d9f2626SSebastian Kapfer 				     unsigned char data)
1651d9f2626SSebastian Kapfer {
16699df65e7SKevin Cernekee 	return (data & priv->mask0) == priv->byte0;
1671d9f2626SSebastian Kapfer }
1681d9f2626SSebastian Kapfer 
16904aae283SPali Rohár static void alps_report_buttons(struct input_dev *dev1, struct input_dev *dev2,
1701d9f2626SSebastian Kapfer 				int left, int right, int middle)
1711d9f2626SSebastian Kapfer {
1721d9f2626SSebastian Kapfer 	struct input_dev *dev;
1731d9f2626SSebastian Kapfer 
1741d9f2626SSebastian Kapfer 	/*
1751d9f2626SSebastian Kapfer 	 * If shared button has already been reported on the
1761d9f2626SSebastian Kapfer 	 * other device (dev2) then this event should be also
1771d9f2626SSebastian Kapfer 	 * sent through that device.
1781d9f2626SSebastian Kapfer 	 */
17904aae283SPali Rohár 	dev = (dev2 && test_bit(BTN_LEFT, dev2->key)) ? dev2 : dev1;
1801d9f2626SSebastian Kapfer 	input_report_key(dev, BTN_LEFT, left);
1811d9f2626SSebastian Kapfer 
18204aae283SPali Rohár 	dev = (dev2 && test_bit(BTN_RIGHT, dev2->key)) ? dev2 : dev1;
1831d9f2626SSebastian Kapfer 	input_report_key(dev, BTN_RIGHT, right);
1841d9f2626SSebastian Kapfer 
18504aae283SPali Rohár 	dev = (dev2 && test_bit(BTN_MIDDLE, dev2->key)) ? dev2 : dev1;
1861d9f2626SSebastian Kapfer 	input_report_key(dev, BTN_MIDDLE, middle);
1871d9f2626SSebastian Kapfer 
1881d9f2626SSebastian Kapfer 	/*
1891d9f2626SSebastian Kapfer 	 * Sync the _other_ device now, we'll do the first
1901d9f2626SSebastian Kapfer 	 * device later once we report the rest of the events.
1911d9f2626SSebastian Kapfer 	 */
19204aae283SPali Rohár 	if (dev2)
1931d9f2626SSebastian Kapfer 		input_sync(dev2);
1941d9f2626SSebastian Kapfer }
1951d9f2626SSebastian Kapfer 
19625bded7cSSeth Forshee static void alps_process_packet_v1_v2(struct psmouse *psmouse)
1971da177e4SLinus Torvalds {
1981da177e4SLinus Torvalds 	struct alps_data *priv = psmouse->private;
1991da177e4SLinus Torvalds 	unsigned char *packet = psmouse->packet;
2002e5b636bSDmitry Torokhov 	struct input_dev *dev = psmouse->dev;
2012e5b636bSDmitry Torokhov 	struct input_dev *dev2 = priv->dev2;
2021da177e4SLinus Torvalds 	int x, y, z, ges, fin, left, right, middle;
203c30b4c10SIvan Casado Ruiz 	int back = 0, forward = 0;
2041da177e4SLinus Torvalds 
20599df65e7SKevin Cernekee 	if (priv->proto_version == ALPS_PROTO_V1) {
206d2f4012fSYotam Medini 		left = packet[2] & 0x10;
207d2f4012fSYotam Medini 		right = packet[2] & 0x08;
2081da177e4SLinus Torvalds 		middle = 0;
2091da177e4SLinus Torvalds 		x = packet[1] | ((packet[0] & 0x07) << 7);
2101da177e4SLinus Torvalds 		y = packet[4] | ((packet[3] & 0x07) << 7);
2111da177e4SLinus Torvalds 		z = packet[5];
2121da177e4SLinus Torvalds 	} else {
2131da177e4SLinus Torvalds 		left = packet[3] & 1;
2141da177e4SLinus Torvalds 		right = packet[3] & 2;
2151da177e4SLinus Torvalds 		middle = packet[3] & 4;
2161da177e4SLinus Torvalds 		x = packet[1] | ((packet[2] & 0x78) << (7 - 3));
2171da177e4SLinus Torvalds 		y = packet[4] | ((packet[3] & 0x70) << (7 - 4));
2181da177e4SLinus Torvalds 		z = packet[5];
2191da177e4SLinus Torvalds 	}
2201da177e4SLinus Torvalds 
22199df65e7SKevin Cernekee 	if (priv->flags & ALPS_FW_BK_1) {
2223c00bb96SLaszlo Kajan 		back = packet[0] & 0x10;
2233c00bb96SLaszlo Kajan 		forward = packet[2] & 4;
224c30b4c10SIvan Casado Ruiz 	}
225c30b4c10SIvan Casado Ruiz 
22699df65e7SKevin Cernekee 	if (priv->flags & ALPS_FW_BK_2) {
227c30b4c10SIvan Casado Ruiz 		back = packet[3] & 4;
228c30b4c10SIvan Casado Ruiz 		forward = packet[2] & 4;
229c30b4c10SIvan Casado Ruiz 		if ((middle = forward && back))
230c30b4c10SIvan Casado Ruiz 			forward = back = 0;
231c30b4c10SIvan Casado Ruiz 	}
232c30b4c10SIvan Casado Ruiz 
2331da177e4SLinus Torvalds 	ges = packet[2] & 1;
2341da177e4SLinus Torvalds 	fin = packet[2] & 2;
2351da177e4SLinus Torvalds 
23699df65e7SKevin Cernekee 	if ((priv->flags & ALPS_DUALPOINT) && z == 127) {
2371da177e4SLinus Torvalds 		input_report_rel(dev2, REL_X,  (x > 383 ? (x - 768) : x));
2381da177e4SLinus Torvalds 		input_report_rel(dev2, REL_Y, -(y > 255 ? (y - 512) : y));
239d7ed5d88SUlrich Dangel 
24004aae283SPali Rohár 		alps_report_buttons(dev2, dev, left, right, middle);
241d7ed5d88SUlrich Dangel 
2421da177e4SLinus Torvalds 		input_sync(dev2);
2431da177e4SLinus Torvalds 		return;
2441da177e4SLinus Torvalds 	}
2451da177e4SLinus Torvalds 
24692bac83dSHans de Goede 	/* Non interleaved V2 dualpoint has separate stick button bits */
24792bac83dSHans de Goede 	if (priv->proto_version == ALPS_PROTO_V2 &&
24892bac83dSHans de Goede 	    priv->flags == (ALPS_PASS | ALPS_DUALPOINT)) {
24992bac83dSHans de Goede 		left |= packet[0] & 1;
25092bac83dSHans de Goede 		right |= packet[0] & 2;
25192bac83dSHans de Goede 		middle |= packet[0] & 4;
25292bac83dSHans de Goede 	}
25392bac83dSHans de Goede 
25404aae283SPali Rohár 	alps_report_buttons(dev, dev2, left, right, middle);
255d7ed5d88SUlrich Dangel 
2561da177e4SLinus Torvalds 	/* Convert hardware tap to a reasonable Z value */
25771bb21b6SMaxim Levitsky 	if (ges && !fin)
25871bb21b6SMaxim Levitsky 		z = 40;
2591da177e4SLinus Torvalds 
2601da177e4SLinus Torvalds 	/*
2611da177e4SLinus Torvalds 	 * A "tap and drag" operation is reported by the hardware as a transition
2621da177e4SLinus Torvalds 	 * from (!fin && ges) to (fin && ges). This should be translated to the
2631da177e4SLinus Torvalds 	 * sequence Z>0, Z==0, Z>0, so the Z==0 event has to be generated manually.
2641da177e4SLinus Torvalds 	 */
2651da177e4SLinus Torvalds 	if (ges && fin && !priv->prev_fin) {
2661da177e4SLinus Torvalds 		input_report_abs(dev, ABS_X, x);
2671da177e4SLinus Torvalds 		input_report_abs(dev, ABS_Y, y);
2681da177e4SLinus Torvalds 		input_report_abs(dev, ABS_PRESSURE, 0);
2691da177e4SLinus Torvalds 		input_report_key(dev, BTN_TOOL_FINGER, 0);
2701da177e4SLinus Torvalds 		input_sync(dev);
2711da177e4SLinus Torvalds 	}
2721da177e4SLinus Torvalds 	priv->prev_fin = fin;
2731da177e4SLinus Torvalds 
27471bb21b6SMaxim Levitsky 	if (z > 30)
27571bb21b6SMaxim Levitsky 		input_report_key(dev, BTN_TOUCH, 1);
27671bb21b6SMaxim Levitsky 	if (z < 25)
27771bb21b6SMaxim Levitsky 		input_report_key(dev, BTN_TOUCH, 0);
2781da177e4SLinus Torvalds 
2791da177e4SLinus Torvalds 	if (z > 0) {
2801da177e4SLinus Torvalds 		input_report_abs(dev, ABS_X, x);
2811da177e4SLinus Torvalds 		input_report_abs(dev, ABS_Y, y);
2821da177e4SLinus Torvalds 	}
2831da177e4SLinus Torvalds 
2841da177e4SLinus Torvalds 	input_report_abs(dev, ABS_PRESSURE, z);
2851da177e4SLinus Torvalds 	input_report_key(dev, BTN_TOOL_FINGER, z > 0);
2861da177e4SLinus Torvalds 
28799df65e7SKevin Cernekee 	if (priv->flags & ALPS_WHEEL)
288e6c047b9SVojtech Pavlik 		input_report_rel(dev, REL_WHEEL, ((packet[2] << 1) & 0x08) - ((packet[0] >> 4) & 0x07));
2891da177e4SLinus Torvalds 
29099df65e7SKevin Cernekee 	if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
291c30b4c10SIvan Casado Ruiz 		input_report_key(dev, BTN_FORWARD, forward);
292c30b4c10SIvan Casado Ruiz 		input_report_key(dev, BTN_BACK, back);
2931da177e4SLinus Torvalds 	}
2941da177e4SLinus Torvalds 
29599df65e7SKevin Cernekee 	if (priv->flags & ALPS_FOUR_BUTTONS) {
29671bb21b6SMaxim Levitsky 		input_report_key(dev, BTN_0, packet[2] & 4);
29771bb21b6SMaxim Levitsky 		input_report_key(dev, BTN_1, packet[0] & 0x10);
29871bb21b6SMaxim Levitsky 		input_report_key(dev, BTN_2, packet[3] & 4);
29971bb21b6SMaxim Levitsky 		input_report_key(dev, BTN_3, packet[0] & 0x20);
30071bb21b6SMaxim Levitsky 	}
30171bb21b6SMaxim Levitsky 
3021da177e4SLinus Torvalds 	input_sync(dev);
3031da177e4SLinus Torvalds }
3041da177e4SLinus Torvalds 
30501ce661fSSeth Forshee /*
306ee65d4b3SYunkang Tang  * Process bitmap data for V5 protocols. Return value is null.
307ee65d4b3SYunkang Tang  *
308ee65d4b3SYunkang Tang  * The bitmaps don't have enough data to track fingers, so this function
309ee65d4b3SYunkang Tang  * only generates points representing a bounding box of at most two contacts.
31002d04254SHans de Goede  * These two points are returned in fields->mt.
311ee65d4b3SYunkang Tang  */
312ee65d4b3SYunkang Tang static void alps_process_bitmap_dolphin(struct alps_data *priv,
31302d04254SHans de Goede 					struct alps_fields *fields)
314ee65d4b3SYunkang Tang {
315ee65d4b3SYunkang Tang 	int box_middle_x, box_middle_y;
316ee65d4b3SYunkang Tang 	unsigned int x_map, y_map;
317ee65d4b3SYunkang Tang 	unsigned char start_bit, end_bit;
318ee65d4b3SYunkang Tang 	unsigned char x_msb, x_lsb, y_msb, y_lsb;
319ee65d4b3SYunkang Tang 
320ee65d4b3SYunkang Tang 	x_map = fields->x_map;
321ee65d4b3SYunkang Tang 	y_map = fields->y_map;
322ee65d4b3SYunkang Tang 
323ee65d4b3SYunkang Tang 	if (!x_map || !y_map)
324ee65d4b3SYunkang Tang 		return;
325ee65d4b3SYunkang Tang 
326ee65d4b3SYunkang Tang 	/* Get Most-significant and Least-significant bit */
327ee65d4b3SYunkang Tang 	x_msb = fls(x_map);
328ee65d4b3SYunkang Tang 	x_lsb = ffs(x_map);
329ee65d4b3SYunkang Tang 	y_msb = fls(y_map);
330ee65d4b3SYunkang Tang 	y_lsb = ffs(y_map);
331ee65d4b3SYunkang Tang 
332ee65d4b3SYunkang Tang 	/* Most-significant bit should never exceed max sensor line number */
333ee65d4b3SYunkang Tang 	if (x_msb > priv->x_bits || y_msb > priv->y_bits)
334ee65d4b3SYunkang Tang 		return;
335ee65d4b3SYunkang Tang 
336ee65d4b3SYunkang Tang 	if (fields->fingers > 1) {
337ee65d4b3SYunkang Tang 		start_bit = priv->x_bits - x_msb;
338ee65d4b3SYunkang Tang 		end_bit = priv->x_bits - x_lsb;
339ee65d4b3SYunkang Tang 		box_middle_x = (priv->x_max * (start_bit + end_bit)) /
340ee65d4b3SYunkang Tang 				(2 * (priv->x_bits - 1));
341ee65d4b3SYunkang Tang 
342ee65d4b3SYunkang Tang 		start_bit = y_lsb - 1;
343ee65d4b3SYunkang Tang 		end_bit = y_msb - 1;
344ee65d4b3SYunkang Tang 		box_middle_y = (priv->y_max * (start_bit + end_bit)) /
345ee65d4b3SYunkang Tang 				(2 * (priv->y_bits - 1));
34602d04254SHans de Goede 		fields->mt[0] = fields->st;
34702d04254SHans de Goede 		fields->mt[1].x = 2 * box_middle_x - fields->mt[0].x;
34802d04254SHans de Goede 		fields->mt[1].y = 2 * box_middle_y - fields->mt[0].y;
349ee65d4b3SYunkang Tang 	}
350ee65d4b3SYunkang Tang }
351ee65d4b3SYunkang Tang 
352036e6c7bSHans de Goede static void alps_get_bitmap_points(unsigned int map,
353036e6c7bSHans de Goede 				   struct alps_bitmap_point *low,
354036e6c7bSHans de Goede 				   struct alps_bitmap_point *high,
355036e6c7bSHans de Goede 				   int *fingers)
356036e6c7bSHans de Goede {
357036e6c7bSHans de Goede 	struct alps_bitmap_point *point;
358036e6c7bSHans de Goede 	int i, bit, prev_bit = 0;
359036e6c7bSHans de Goede 
360036e6c7bSHans de Goede 	point = low;
361036e6c7bSHans de Goede 	for (i = 0; map != 0; i++, map >>= 1) {
362036e6c7bSHans de Goede 		bit = map & 1;
363036e6c7bSHans de Goede 		if (bit) {
364036e6c7bSHans de Goede 			if (!prev_bit) {
365036e6c7bSHans de Goede 				point->start_bit = i;
366105affbfSHans de Goede 				point->num_bits = 0;
367036e6c7bSHans de Goede 				(*fingers)++;
368036e6c7bSHans de Goede 			}
369036e6c7bSHans de Goede 			point->num_bits++;
370036e6c7bSHans de Goede 		} else {
371036e6c7bSHans de Goede 			if (prev_bit)
372036e6c7bSHans de Goede 				point = high;
373036e6c7bSHans de Goede 		}
374036e6c7bSHans de Goede 		prev_bit = bit;
375036e6c7bSHans de Goede 	}
376036e6c7bSHans de Goede }
377036e6c7bSHans de Goede 
378ee65d4b3SYunkang Tang /*
37901ce661fSSeth Forshee  * Process bitmap data from v3 and v4 protocols. Returns the number of
38001ce661fSSeth Forshee  * fingers detected. A return value of 0 means at least one of the
38101ce661fSSeth Forshee  * bitmaps was empty.
38201ce661fSSeth Forshee  *
38301ce661fSSeth Forshee  * The bitmaps don't have enough data to track fingers, so this function
38401ce661fSSeth Forshee  * only generates points representing a bounding box of all contacts.
38502d04254SHans de Goede  * These points are returned in fields->mt when the return value
38601ce661fSSeth Forshee  * is greater than 0.
38701ce661fSSeth Forshee  */
3887a9f73e7SKevin Cernekee static int alps_process_bitmap(struct alps_data *priv,
38902d04254SHans de Goede 			       struct alps_fields *fields)
39001ce661fSSeth Forshee {
391036e6c7bSHans de Goede 	int i, fingers_x = 0, fingers_y = 0, fingers;
39201ce661fSSeth Forshee 	struct alps_bitmap_point x_low = {0,}, x_high = {0,};
39301ce661fSSeth Forshee 	struct alps_bitmap_point y_low = {0,}, y_high = {0,};
39401ce661fSSeth Forshee 
39502d04254SHans de Goede 	if (!fields->x_map || !fields->y_map)
39601ce661fSSeth Forshee 		return 0;
39701ce661fSSeth Forshee 
39802d04254SHans de Goede 	alps_get_bitmap_points(fields->x_map, &x_low, &x_high, &fingers_x);
39902d04254SHans de Goede 	alps_get_bitmap_points(fields->y_map, &y_low, &y_high, &fingers_y);
40001ce661fSSeth Forshee 
40101ce661fSSeth Forshee 	/*
40201ce661fSSeth Forshee 	 * Fingers can overlap, so we use the maximum count of fingers
40301ce661fSSeth Forshee 	 * on either axis as the finger count.
40401ce661fSSeth Forshee 	 */
40501ce661fSSeth Forshee 	fingers = max(fingers_x, fingers_y);
40601ce661fSSeth Forshee 
40701ce661fSSeth Forshee 	/*
40820bea68bSHans de Goede 	 * If an axis reports only a single contact, we have overlapping or
40920bea68bSHans de Goede 	 * adjacent fingers. Divide the single contact between the two points.
41001ce661fSSeth Forshee 	 */
41101ce661fSSeth Forshee 	if (fingers_x == 1) {
41228835f45SHans de Goede 		i = (x_low.num_bits - 1) / 2;
41301ce661fSSeth Forshee 		x_low.num_bits = x_low.num_bits - i;
41401ce661fSSeth Forshee 		x_high.start_bit = x_low.start_bit + i;
41501ce661fSSeth Forshee 		x_high.num_bits = max(i, 1);
41620bea68bSHans de Goede 	}
41720bea68bSHans de Goede 	if (fingers_y == 1) {
41828835f45SHans de Goede 		i = (y_low.num_bits - 1) / 2;
41901ce661fSSeth Forshee 		y_low.num_bits = y_low.num_bits - i;
42001ce661fSSeth Forshee 		y_high.start_bit = y_low.start_bit + i;
42101ce661fSSeth Forshee 		y_high.num_bits = max(i, 1);
42201ce661fSSeth Forshee 	}
42301ce661fSSeth Forshee 
42402d04254SHans de Goede 	fields->mt[0].x =
42502d04254SHans de Goede 		(priv->x_max * (2 * x_low.start_bit + x_low.num_bits - 1)) /
4267a9f73e7SKevin Cernekee 		(2 * (priv->x_bits - 1));
42702d04254SHans de Goede 	fields->mt[0].y =
42802d04254SHans de Goede 		(priv->y_max * (2 * y_low.start_bit + y_low.num_bits - 1)) /
4297a9f73e7SKevin Cernekee 		(2 * (priv->y_bits - 1));
43001ce661fSSeth Forshee 
43102d04254SHans de Goede 	fields->mt[1].x =
43202d04254SHans de Goede 		(priv->x_max * (2 * x_high.start_bit + x_high.num_bits - 1)) /
4337a9f73e7SKevin Cernekee 		(2 * (priv->x_bits - 1));
43402d04254SHans de Goede 	fields->mt[1].y =
43502d04254SHans de Goede 		(priv->y_max * (2 * y_high.start_bit + y_high.num_bits - 1)) /
4367a9f73e7SKevin Cernekee 		(2 * (priv->y_bits - 1));
43701ce661fSSeth Forshee 
43840e8f53bSHans de Goede 	/* y-bitmap order is reversed, except on rushmore */
439fb2dd7a6SDmitry Torokhov 	if (priv->proto_version != ALPS_PROTO_V3_RUSHMORE) {
44002d04254SHans de Goede 		fields->mt[0].y = priv->y_max - fields->mt[0].y;
44102d04254SHans de Goede 		fields->mt[1].y = priv->y_max - fields->mt[1].y;
44240e8f53bSHans de Goede 	}
44340e8f53bSHans de Goede 
44401ce661fSSeth Forshee 	return fingers;
44501ce661fSSeth Forshee }
44601ce661fSSeth Forshee 
447cdf333efSHans de Goede static void alps_set_slot(struct input_dev *dev, int slot, int x, int y)
44801ce661fSSeth Forshee {
44901ce661fSSeth Forshee 	input_mt_slot(dev, slot);
450cdf333efSHans de Goede 	input_mt_report_slot_state(dev, MT_TOOL_FINGER, true);
45101ce661fSSeth Forshee 	input_report_abs(dev, ABS_MT_POSITION_X, x);
45201ce661fSSeth Forshee 	input_report_abs(dev, ABS_MT_POSITION_Y, y);
45301ce661fSSeth Forshee }
45401ce661fSSeth Forshee 
455cdf333efSHans de Goede static void alps_report_mt_data(struct psmouse *psmouse, int n)
45601ce661fSSeth Forshee {
45702d04254SHans de Goede 	struct alps_data *priv = psmouse->private;
45802d04254SHans de Goede 	struct input_dev *dev = psmouse->dev;
45902d04254SHans de Goede 	struct alps_fields *f = &priv->f;
460cdf333efSHans de Goede 	int i, slot[MAX_TOUCHES];
46102d04254SHans de Goede 
462448c7f38SHenrik Rydberg 	input_mt_assign_slots(dev, slot, f->mt, n, 0);
463cdf333efSHans de Goede 	for (i = 0; i < n; i++)
464cdf333efSHans de Goede 		alps_set_slot(dev, slot[i], f->mt[i].x, f->mt[i].y);
465cdf333efSHans de Goede 
466cdf333efSHans de Goede 	input_mt_sync_frame(dev);
46701ce661fSSeth Forshee }
46801ce661fSSeth Forshee 
46968c21870SHans de Goede static void alps_report_semi_mt_data(struct psmouse *psmouse, int fingers)
47068c21870SHans de Goede {
47168c21870SHans de Goede 	struct alps_data *priv = psmouse->private;
47268c21870SHans de Goede 	struct input_dev *dev = psmouse->dev;
47368c21870SHans de Goede 	struct alps_fields *f = &priv->f;
47468c21870SHans de Goede 
47568c21870SHans de Goede 	/* Use st data when we don't have mt data */
47668c21870SHans de Goede 	if (fingers < 2) {
47768c21870SHans de Goede 		f->mt[0].x = f->st.x;
47868c21870SHans de Goede 		f->mt[0].y = f->st.y;
47968c21870SHans de Goede 		fingers = f->pressure > 0 ? 1 : 0;
48068c21870SHans de Goede 	}
48168c21870SHans de Goede 
48299d9996cSHans de Goede 	alps_report_mt_data(psmouse, (fingers <= 2) ? fingers : 2);
48368c21870SHans de Goede 
48468c21870SHans de Goede 	input_mt_report_finger_count(dev, fingers);
48568c21870SHans de Goede 
48668c21870SHans de Goede 	input_report_key(dev, BTN_LEFT, f->left);
48768c21870SHans de Goede 	input_report_key(dev, BTN_RIGHT, f->right);
48868c21870SHans de Goede 	input_report_key(dev, BTN_MIDDLE, f->middle);
48968c21870SHans de Goede 
49068c21870SHans de Goede 	input_report_abs(dev, ABS_PRESSURE, f->pressure);
49168c21870SHans de Goede 
49268c21870SHans de Goede 	input_sync(dev);
49368c21870SHans de Goede }
49468c21870SHans de Goede 
49525bded7cSSeth Forshee static void alps_process_trackstick_packet_v3(struct psmouse *psmouse)
49625bded7cSSeth Forshee {
49725bded7cSSeth Forshee 	struct alps_data *priv = psmouse->private;
49825bded7cSSeth Forshee 	unsigned char *packet = psmouse->packet;
49925bded7cSSeth Forshee 	struct input_dev *dev = priv->dev2;
50025bded7cSSeth Forshee 	int x, y, z, left, right, middle;
50125bded7cSSeth Forshee 
50234412ba2SPali Rohár 	/* It should be a DualPoint when received trackstick packet */
50334412ba2SPali Rohár 	if (!(priv->flags & ALPS_DUALPOINT)) {
50434412ba2SPali Rohár 		psmouse_warn(psmouse,
50534412ba2SPali Rohár 			     "Rejected trackstick packet from non DualPoint device");
50634412ba2SPali Rohár 		return;
50734412ba2SPali Rohár 	}
50834412ba2SPali Rohár 
50925bded7cSSeth Forshee 	/* Sanity check packet */
51025bded7cSSeth Forshee 	if (!(packet[0] & 0x40)) {
51125bded7cSSeth Forshee 		psmouse_dbg(psmouse, "Bad trackstick packet, discarding\n");
51225bded7cSSeth Forshee 		return;
51325bded7cSSeth Forshee 	}
51425bded7cSSeth Forshee 
51525bded7cSSeth Forshee 	/*
51625bded7cSSeth Forshee 	 * There's a special packet that seems to indicate the end
51725bded7cSSeth Forshee 	 * of a stream of trackstick data. Filter these out.
51825bded7cSSeth Forshee 	 */
51925bded7cSSeth Forshee 	if (packet[1] == 0x7f && packet[2] == 0x7f && packet[4] == 0x7f)
52025bded7cSSeth Forshee 		return;
52125bded7cSSeth Forshee 
52225bded7cSSeth Forshee 	x = (s8)(((packet[0] & 0x20) << 2) | (packet[1] & 0x7f));
52325bded7cSSeth Forshee 	y = (s8)(((packet[0] & 0x10) << 3) | (packet[2] & 0x7f));
52425bded7cSSeth Forshee 	z = (packet[4] & 0x7c) >> 2;
52525bded7cSSeth Forshee 
52625bded7cSSeth Forshee 	/*
52725bded7cSSeth Forshee 	 * The x and y values tend to be quite large, and when used
52825bded7cSSeth Forshee 	 * alone the trackstick is difficult to use. Scale them down
52925bded7cSSeth Forshee 	 * to compensate.
53025bded7cSSeth Forshee 	 */
53125bded7cSSeth Forshee 	x /= 8;
53225bded7cSSeth Forshee 	y /= 8;
53325bded7cSSeth Forshee 
53425bded7cSSeth Forshee 	input_report_rel(dev, REL_X, x);
53525bded7cSSeth Forshee 	input_report_rel(dev, REL_Y, -y);
53625bded7cSSeth Forshee 
53725bded7cSSeth Forshee 	/*
53825bded7cSSeth Forshee 	 * Most ALPS models report the trackstick buttons in the touchpad
53925bded7cSSeth Forshee 	 * packets, but a few report them here. No reliable way has been
54025bded7cSSeth Forshee 	 * found to differentiate between the models upfront, so we enable
54125bded7cSSeth Forshee 	 * the quirk in response to seeing a button press in the trackstick
54225bded7cSSeth Forshee 	 * packet.
54325bded7cSSeth Forshee 	 */
54425bded7cSSeth Forshee 	left = packet[3] & 0x01;
54525bded7cSSeth Forshee 	right = packet[3] & 0x02;
54625bded7cSSeth Forshee 	middle = packet[3] & 0x04;
54725bded7cSSeth Forshee 
54825bded7cSSeth Forshee 	if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) &&
54925bded7cSSeth Forshee 	    (left || right || middle))
55025bded7cSSeth Forshee 		priv->quirks |= ALPS_QUIRK_TRACKSTICK_BUTTONS;
55125bded7cSSeth Forshee 
55225bded7cSSeth Forshee 	if (priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) {
55325bded7cSSeth Forshee 		input_report_key(dev, BTN_LEFT, left);
55425bded7cSSeth Forshee 		input_report_key(dev, BTN_RIGHT, right);
55525bded7cSSeth Forshee 		input_report_key(dev, BTN_MIDDLE, middle);
55625bded7cSSeth Forshee 	}
55725bded7cSSeth Forshee 
55825bded7cSSeth Forshee 	input_sync(dev);
55925bded7cSSeth Forshee 	return;
56025bded7cSSeth Forshee }
56125bded7cSSeth Forshee 
562f85e5001SKevin Cernekee static void alps_decode_buttons_v3(struct alps_fields *f, unsigned char *p)
563f85e5001SKevin Cernekee {
564f85e5001SKevin Cernekee 	f->left = !!(p[3] & 0x01);
565f85e5001SKevin Cernekee 	f->right = !!(p[3] & 0x02);
566f85e5001SKevin Cernekee 	f->middle = !!(p[3] & 0x04);
567f85e5001SKevin Cernekee 
568f85e5001SKevin Cernekee 	f->ts_left = !!(p[3] & 0x10);
569f85e5001SKevin Cernekee 	f->ts_right = !!(p[3] & 0x20);
570f85e5001SKevin Cernekee 	f->ts_middle = !!(p[3] & 0x40);
571f85e5001SKevin Cernekee }
572f85e5001SKevin Cernekee 
57338c11eaaSHans de Goede static int alps_decode_pinnacle(struct alps_fields *f, unsigned char *p,
574ee65d4b3SYunkang Tang 				 struct psmouse *psmouse)
575f85e5001SKevin Cernekee {
576f85e5001SKevin Cernekee 	f->first_mp = !!(p[4] & 0x40);
577f85e5001SKevin Cernekee 	f->is_mp = !!(p[0] & 0x40);
578f85e5001SKevin Cernekee 
579f85e5001SKevin Cernekee 	f->fingers = (p[5] & 0x3) + 1;
580f85e5001SKevin Cernekee 	f->x_map = ((p[4] & 0x7e) << 8) |
581f85e5001SKevin Cernekee 		   ((p[1] & 0x7f) << 2) |
582f85e5001SKevin Cernekee 		   ((p[0] & 0x30) >> 4);
583f85e5001SKevin Cernekee 	f->y_map = ((p[3] & 0x70) << 4) |
584f85e5001SKevin Cernekee 		   ((p[2] & 0x7f) << 1) |
585f85e5001SKevin Cernekee 		   (p[4] & 0x01);
586f85e5001SKevin Cernekee 
58702d04254SHans de Goede 	f->st.x = ((p[1] & 0x7f) << 4) | ((p[4] & 0x30) >> 2) |
588f85e5001SKevin Cernekee 	       ((p[0] & 0x30) >> 4);
58902d04254SHans de Goede 	f->st.y = ((p[2] & 0x7f) << 4) | (p[4] & 0x0f);
59002d04254SHans de Goede 	f->pressure = p[5] & 0x7f;
591f85e5001SKevin Cernekee 
592f85e5001SKevin Cernekee 	alps_decode_buttons_v3(f, p);
59338c11eaaSHans de Goede 
59438c11eaaSHans de Goede 	return 0;
595f85e5001SKevin Cernekee }
596f85e5001SKevin Cernekee 
59738c11eaaSHans de Goede static int alps_decode_rushmore(struct alps_fields *f, unsigned char *p,
598ee65d4b3SYunkang Tang 				 struct psmouse *psmouse)
5991302bac3SKevin Cernekee {
600ee65d4b3SYunkang Tang 	alps_decode_pinnacle(f, p, psmouse);
6011302bac3SKevin Cernekee 
602f105e34aSYunkang Tang 	/* Rushmore's packet decode has a bit difference with Pinnacle's */
603f105e34aSYunkang Tang 	f->is_mp = !!(p[5] & 0x40);
604f105e34aSYunkang Tang 	f->fingers = max((p[5] & 0x3), ((p[5] >> 2) & 0x3)) + 1;
6051302bac3SKevin Cernekee 	f->x_map |= (p[5] & 0x10) << 11;
6061302bac3SKevin Cernekee 	f->y_map |= (p[5] & 0x20) << 6;
60738c11eaaSHans de Goede 
60838c11eaaSHans de Goede 	return 0;
6091302bac3SKevin Cernekee }
6101302bac3SKevin Cernekee 
61138c11eaaSHans de Goede static int alps_decode_dolphin(struct alps_fields *f, unsigned char *p,
612ee65d4b3SYunkang Tang 				struct psmouse *psmouse)
61375af9e56SDave Turvene {
614ee65d4b3SYunkang Tang 	u64 palm_data = 0;
615ee65d4b3SYunkang Tang 	struct alps_data *priv = psmouse->private;
616ee65d4b3SYunkang Tang 
61775af9e56SDave Turvene 	f->first_mp = !!(p[0] & 0x02);
61875af9e56SDave Turvene 	f->is_mp = !!(p[0] & 0x20);
61975af9e56SDave Turvene 
620ee65d4b3SYunkang Tang 	if (!f->is_mp) {
62102d04254SHans de Goede 		f->st.x = ((p[1] & 0x7f) | ((p[4] & 0x0f) << 7));
62202d04254SHans de Goede 		f->st.y = ((p[2] & 0x7f) | ((p[4] & 0xf0) << 3));
62302d04254SHans de Goede 		f->pressure = (p[0] & 4) ? 0 : p[5] & 0x7f;
62475af9e56SDave Turvene 		alps_decode_buttons_v3(f, p);
625ee65d4b3SYunkang Tang 	} else {
626ee65d4b3SYunkang Tang 		f->fingers = ((p[0] & 0x6) >> 1 |
627ee65d4b3SYunkang Tang 		     (p[0] & 0x10) >> 2);
628ee65d4b3SYunkang Tang 
629ee65d4b3SYunkang Tang 		palm_data = (p[1] & 0x7f) |
630ee65d4b3SYunkang Tang 			    ((p[2] & 0x7f) << 7) |
631ee65d4b3SYunkang Tang 			    ((p[4] & 0x7f) << 14) |
632ee65d4b3SYunkang Tang 			    ((p[5] & 0x7f) << 21) |
633ee65d4b3SYunkang Tang 			    ((p[3] & 0x07) << 28) |
634ee65d4b3SYunkang Tang 			    (((u64)p[3] & 0x70) << 27) |
635ee65d4b3SYunkang Tang 			    (((u64)p[0] & 0x01) << 34);
636ee65d4b3SYunkang Tang 
637ee65d4b3SYunkang Tang 		/* Y-profile is stored in P(0) to p(n-1), n = y_bits; */
638ee65d4b3SYunkang Tang 		f->y_map = palm_data & (BIT(priv->y_bits) - 1);
639ee65d4b3SYunkang Tang 
640ee65d4b3SYunkang Tang 		/* X-profile is stored in p(n) to p(n+m-1), m = x_bits; */
641ee65d4b3SYunkang Tang 		f->x_map = (palm_data >> priv->y_bits) &
642ee65d4b3SYunkang Tang 			   (BIT(priv->x_bits) - 1);
643ee65d4b3SYunkang Tang 	}
64438c11eaaSHans de Goede 
64538c11eaaSHans de Goede 	return 0;
64675af9e56SDave Turvene }
64775af9e56SDave Turvene 
648ee65d4b3SYunkang Tang static void alps_process_touchpad_packet_v3_v5(struct psmouse *psmouse)
64925bded7cSSeth Forshee {
65025bded7cSSeth Forshee 	struct alps_data *priv = psmouse->private;
65125bded7cSSeth Forshee 	unsigned char *packet = psmouse->packet;
65225bded7cSSeth Forshee 	struct input_dev *dev2 = priv->dev2;
65302d04254SHans de Goede 	struct alps_fields *f = &priv->f;
65402d04254SHans de Goede 	int fingers = 0;
655f85e5001SKevin Cernekee 
65602d04254SHans de Goede 	memset(f, 0, sizeof(*f));
65702d04254SHans de Goede 
65802d04254SHans de Goede 	priv->decode_fields(f, packet, psmouse);
65925bded7cSSeth Forshee 
66025bded7cSSeth Forshee 	/*
66101ce661fSSeth Forshee 	 * There's no single feature of touchpad position and bitmap packets
66201ce661fSSeth Forshee 	 * that can be used to distinguish between them. We rely on the fact
66301ce661fSSeth Forshee 	 * that a bitmap packet should always follow a position packet with
66401ce661fSSeth Forshee 	 * bit 6 of packet[4] set.
66525bded7cSSeth Forshee 	 */
66625bded7cSSeth Forshee 	if (priv->multi_packet) {
66725bded7cSSeth Forshee 		/*
66825bded7cSSeth Forshee 		 * Sometimes a position packet will indicate a multi-packet
66925bded7cSSeth Forshee 		 * sequence, but then what follows is another position
67025bded7cSSeth Forshee 		 * packet. Check for this, and when it happens process the
67125bded7cSSeth Forshee 		 * position packet as usual.
67225bded7cSSeth Forshee 		 */
67302d04254SHans de Goede 		if (f->is_mp) {
67402d04254SHans de Goede 			fingers = f->fingers;
675fb2dd7a6SDmitry Torokhov 			if (priv->proto_version == ALPS_PROTO_V3 ||
676fb2dd7a6SDmitry Torokhov 			    priv->proto_version == ALPS_PROTO_V3_RUSHMORE) {
67702d04254SHans de Goede 				if (alps_process_bitmap(priv, f) == 0)
67820bea68bSHans de Goede 					fingers = 0; /* Use st data */
67901ce661fSSeth Forshee 
68001ce661fSSeth Forshee 				/* Now process position packet */
68102d04254SHans de Goede 				priv->decode_fields(f, priv->multi_data,
682ee65d4b3SYunkang Tang 						    psmouse);
683ee65d4b3SYunkang Tang 			} else {
684ee65d4b3SYunkang Tang 				/*
685ee65d4b3SYunkang Tang 				 * Because Dolphin uses position packet's
686ee65d4b3SYunkang Tang 				 * coordinate data as Pt1 and uses it to
687ee65d4b3SYunkang Tang 				 * calculate Pt2, so we need to do position
688ee65d4b3SYunkang Tang 				 * packet decode first.
689ee65d4b3SYunkang Tang 				 */
69002d04254SHans de Goede 				priv->decode_fields(f, priv->multi_data,
691ee65d4b3SYunkang Tang 						    psmouse);
692ee65d4b3SYunkang Tang 
693ee65d4b3SYunkang Tang 				/*
694ee65d4b3SYunkang Tang 				 * Since Dolphin's finger number is reliable,
695ee65d4b3SYunkang Tang 				 * there is no need to compare with bmap_fn.
696ee65d4b3SYunkang Tang 				 */
69702d04254SHans de Goede 				alps_process_bitmap_dolphin(priv, f);
698ee65d4b3SYunkang Tang 			}
69901ce661fSSeth Forshee 		} else {
70001ce661fSSeth Forshee 			priv->multi_packet = 0;
70125bded7cSSeth Forshee 		}
70225bded7cSSeth Forshee 	}
70325bded7cSSeth Forshee 
70401ce661fSSeth Forshee 	/*
70501ce661fSSeth Forshee 	 * Bit 6 of byte 0 is not usually set in position packets. The only
70601ce661fSSeth Forshee 	 * times it seems to be set is in situations where the data is
70701ce661fSSeth Forshee 	 * suspect anyway, e.g. a palm resting flat on the touchpad. Given
70801ce661fSSeth Forshee 	 * this combined with the fact that this bit is useful for filtering
70901ce661fSSeth Forshee 	 * out misidentified bitmap packets, we reject anything with this
71001ce661fSSeth Forshee 	 * bit set.
71101ce661fSSeth Forshee 	 */
71202d04254SHans de Goede 	if (f->is_mp)
71301ce661fSSeth Forshee 		return;
71401ce661fSSeth Forshee 
71502d04254SHans de Goede 	if (!priv->multi_packet && f->first_mp) {
71625bded7cSSeth Forshee 		priv->multi_packet = 1;
71701ce661fSSeth Forshee 		memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
71801ce661fSSeth Forshee 		return;
71901ce661fSSeth Forshee 	}
72001ce661fSSeth Forshee 
72125bded7cSSeth Forshee 	priv->multi_packet = 0;
72225bded7cSSeth Forshee 
72325bded7cSSeth Forshee 	/*
72425bded7cSSeth Forshee 	 * Sometimes the hardware sends a single packet with z = 0
72525bded7cSSeth Forshee 	 * in the middle of a stream. Real releases generate packets
72625bded7cSSeth Forshee 	 * with x, y, and z all zero, so these seem to be flukes.
72725bded7cSSeth Forshee 	 * Ignore them.
72825bded7cSSeth Forshee 	 */
72902d04254SHans de Goede 	if (f->st.x && f->st.y && !f->pressure)
73025bded7cSSeth Forshee 		return;
73125bded7cSSeth Forshee 
73268c21870SHans de Goede 	alps_report_semi_mt_data(psmouse, fingers);
73325bded7cSSeth Forshee 
73434412ba2SPali Rohár 	if ((priv->flags & ALPS_DUALPOINT) &&
73534412ba2SPali Rohár 	    !(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS)) {
73602d04254SHans de Goede 		input_report_key(dev2, BTN_LEFT, f->ts_left);
73702d04254SHans de Goede 		input_report_key(dev2, BTN_RIGHT, f->ts_right);
73802d04254SHans de Goede 		input_report_key(dev2, BTN_MIDDLE, f->ts_middle);
73925bded7cSSeth Forshee 		input_sync(dev2);
74025bded7cSSeth Forshee 	}
74125bded7cSSeth Forshee }
74225bded7cSSeth Forshee 
74325bded7cSSeth Forshee static void alps_process_packet_v3(struct psmouse *psmouse)
74425bded7cSSeth Forshee {
74525bded7cSSeth Forshee 	unsigned char *packet = psmouse->packet;
74625bded7cSSeth Forshee 
74725bded7cSSeth Forshee 	/*
74825bded7cSSeth Forshee 	 * v3 protocol packets come in three types, two representing
74925bded7cSSeth Forshee 	 * touchpad data and one representing trackstick data.
75025bded7cSSeth Forshee 	 * Trackstick packets seem to be distinguished by always
75125bded7cSSeth Forshee 	 * having 0x3f in the last byte. This value has never been
75225bded7cSSeth Forshee 	 * observed in the last byte of either of the other types
75325bded7cSSeth Forshee 	 * of packets.
75425bded7cSSeth Forshee 	 */
75525bded7cSSeth Forshee 	if (packet[5] == 0x3f) {
75625bded7cSSeth Forshee 		alps_process_trackstick_packet_v3(psmouse);
75725bded7cSSeth Forshee 		return;
75825bded7cSSeth Forshee 	}
75925bded7cSSeth Forshee 
760ee65d4b3SYunkang Tang 	alps_process_touchpad_packet_v3_v5(psmouse);
76125bded7cSSeth Forshee }
76225bded7cSSeth Forshee 
76395f75e91SYunkang Tang static void alps_process_packet_v6(struct psmouse *psmouse)
76495f75e91SYunkang Tang {
76595f75e91SYunkang Tang 	struct alps_data *priv = psmouse->private;
76695f75e91SYunkang Tang 	unsigned char *packet = psmouse->packet;
76795f75e91SYunkang Tang 	struct input_dev *dev = psmouse->dev;
76895f75e91SYunkang Tang 	struct input_dev *dev2 = priv->dev2;
76995f75e91SYunkang Tang 	int x, y, z, left, right, middle;
77095f75e91SYunkang Tang 
77195f75e91SYunkang Tang 	/*
77295f75e91SYunkang Tang 	 * We can use Byte5 to distinguish if the packet is from Touchpad
77395f75e91SYunkang Tang 	 * or Trackpoint.
77495f75e91SYunkang Tang 	 * Touchpad:	0 - 0x7E
77595f75e91SYunkang Tang 	 * Trackpoint:	0x7F
77695f75e91SYunkang Tang 	 */
77795f75e91SYunkang Tang 	if (packet[5] == 0x7F) {
77895f75e91SYunkang Tang 		/* It should be a DualPoint when received Trackpoint packet */
77934412ba2SPali Rohár 		if (!(priv->flags & ALPS_DUALPOINT)) {
78034412ba2SPali Rohár 			psmouse_warn(psmouse,
78134412ba2SPali Rohár 				     "Rejected trackstick packet from non DualPoint device");
78295f75e91SYunkang Tang 			return;
78334412ba2SPali Rohár 		}
78495f75e91SYunkang Tang 
78595f75e91SYunkang Tang 		/* Trackpoint packet */
78695f75e91SYunkang Tang 		x = packet[1] | ((packet[3] & 0x20) << 2);
78795f75e91SYunkang Tang 		y = packet[2] | ((packet[3] & 0x40) << 1);
78895f75e91SYunkang Tang 		z = packet[4];
78995f75e91SYunkang Tang 		left = packet[3] & 0x01;
79095f75e91SYunkang Tang 		right = packet[3] & 0x02;
79195f75e91SYunkang Tang 		middle = packet[3] & 0x04;
79295f75e91SYunkang Tang 
79395f75e91SYunkang Tang 		/* To prevent the cursor jump when finger lifted */
79495f75e91SYunkang Tang 		if (x == 0x7F && y == 0x7F && z == 0x7F)
79595f75e91SYunkang Tang 			x = y = z = 0;
79695f75e91SYunkang Tang 
79795f75e91SYunkang Tang 		/* Divide 4 since trackpoint's speed is too fast */
79895f75e91SYunkang Tang 		input_report_rel(dev2, REL_X, (char)x / 4);
79995f75e91SYunkang Tang 		input_report_rel(dev2, REL_Y, -((char)y / 4));
80095f75e91SYunkang Tang 
80195f75e91SYunkang Tang 		input_report_key(dev2, BTN_LEFT, left);
80295f75e91SYunkang Tang 		input_report_key(dev2, BTN_RIGHT, right);
80395f75e91SYunkang Tang 		input_report_key(dev2, BTN_MIDDLE, middle);
80495f75e91SYunkang Tang 
80595f75e91SYunkang Tang 		input_sync(dev2);
80695f75e91SYunkang Tang 		return;
80795f75e91SYunkang Tang 	}
80895f75e91SYunkang Tang 
80995f75e91SYunkang Tang 	/* Touchpad packet */
81095f75e91SYunkang Tang 	x = packet[1] | ((packet[3] & 0x78) << 4);
81195f75e91SYunkang Tang 	y = packet[2] | ((packet[4] & 0x78) << 4);
81295f75e91SYunkang Tang 	z = packet[5];
81395f75e91SYunkang Tang 	left = packet[3] & 0x01;
81495f75e91SYunkang Tang 	right = packet[3] & 0x02;
81595f75e91SYunkang Tang 
81695f75e91SYunkang Tang 	if (z > 30)
81795f75e91SYunkang Tang 		input_report_key(dev, BTN_TOUCH, 1);
81895f75e91SYunkang Tang 	if (z < 25)
81995f75e91SYunkang Tang 		input_report_key(dev, BTN_TOUCH, 0);
82095f75e91SYunkang Tang 
82195f75e91SYunkang Tang 	if (z > 0) {
82295f75e91SYunkang Tang 		input_report_abs(dev, ABS_X, x);
82395f75e91SYunkang Tang 		input_report_abs(dev, ABS_Y, y);
82495f75e91SYunkang Tang 	}
82595f75e91SYunkang Tang 
82695f75e91SYunkang Tang 	input_report_abs(dev, ABS_PRESSURE, z);
82795f75e91SYunkang Tang 	input_report_key(dev, BTN_TOOL_FINGER, z > 0);
82895f75e91SYunkang Tang 
82995f75e91SYunkang Tang 	/* v6 touchpad does not have middle button */
83095f75e91SYunkang Tang 	input_report_key(dev, BTN_LEFT, left);
83195f75e91SYunkang Tang 	input_report_key(dev, BTN_RIGHT, right);
83295f75e91SYunkang Tang 
83395f75e91SYunkang Tang 	input_sync(dev);
83495f75e91SYunkang Tang }
83595f75e91SYunkang Tang 
83625bded7cSSeth Forshee static void alps_process_packet_v4(struct psmouse *psmouse)
83725bded7cSSeth Forshee {
8383b7e09faSGeorge Pantalos 	struct alps_data *priv = psmouse->private;
83925bded7cSSeth Forshee 	unsigned char *packet = psmouse->packet;
84002d04254SHans de Goede 	struct alps_fields *f = &priv->f;
84168c21870SHans de Goede 	int offset;
8423b7e09faSGeorge Pantalos 
8433b7e09faSGeorge Pantalos 	/*
8443b7e09faSGeorge Pantalos 	 * v4 has a 6-byte encoding for bitmap data, but this data is
8453b7e09faSGeorge Pantalos 	 * broken up between 3 normal packets. Use priv->multi_packet to
8463b7e09faSGeorge Pantalos 	 * track our position in the bitmap packet.
8473b7e09faSGeorge Pantalos 	 */
8483b7e09faSGeorge Pantalos 	if (packet[6] & 0x40) {
8493b7e09faSGeorge Pantalos 		/* sync, reset position */
8503b7e09faSGeorge Pantalos 		priv->multi_packet = 0;
8513b7e09faSGeorge Pantalos 	}
8523b7e09faSGeorge Pantalos 
8533b7e09faSGeorge Pantalos 	if (WARN_ON_ONCE(priv->multi_packet > 2))
8543b7e09faSGeorge Pantalos 		return;
8553b7e09faSGeorge Pantalos 
8563b7e09faSGeorge Pantalos 	offset = 2 * priv->multi_packet;
8573b7e09faSGeorge Pantalos 	priv->multi_data[offset] = packet[6];
8583b7e09faSGeorge Pantalos 	priv->multi_data[offset + 1] = packet[7];
8593b7e09faSGeorge Pantalos 
8603b7e09faSGeorge Pantalos 	if (++priv->multi_packet > 2) {
8613b7e09faSGeorge Pantalos 		priv->multi_packet = 0;
8623b7e09faSGeorge Pantalos 
86302d04254SHans de Goede 		f->x_map = ((priv->multi_data[2] & 0x1f) << 10) |
8643b7e09faSGeorge Pantalos 			   ((priv->multi_data[3] & 0x60) << 3) |
8653b7e09faSGeorge Pantalos 			   ((priv->multi_data[0] & 0x3f) << 2) |
8663b7e09faSGeorge Pantalos 			   ((priv->multi_data[1] & 0x60) >> 5);
86702d04254SHans de Goede 		f->y_map = ((priv->multi_data[5] & 0x01) << 10) |
8683b7e09faSGeorge Pantalos 			   ((priv->multi_data[3] & 0x1f) << 5) |
8693b7e09faSGeorge Pantalos 			    (priv->multi_data[1] & 0x1f);
8703b7e09faSGeorge Pantalos 
87102d04254SHans de Goede 		f->fingers = alps_process_bitmap(priv, f);
8723b7e09faSGeorge Pantalos 	}
87325bded7cSSeth Forshee 
874b0cfb794SAndreas Bosch 	f->left = !!(packet[4] & 0x01);
875b0cfb794SAndreas Bosch 	f->right = !!(packet[4] & 0x02);
87625bded7cSSeth Forshee 
87702d04254SHans de Goede 	f->st.x = ((packet[1] & 0x7f) << 4) | ((packet[3] & 0x30) >> 2) |
87825bded7cSSeth Forshee 		  ((packet[0] & 0x30) >> 4);
87902d04254SHans de Goede 	f->st.y = ((packet[2] & 0x7f) << 4) | (packet[3] & 0x0f);
88002d04254SHans de Goede 	f->pressure = packet[5] & 0x7f;
88125bded7cSSeth Forshee 
88268c21870SHans de Goede 	alps_report_semi_mt_data(psmouse, f->fingers);
88325bded7cSSeth Forshee }
88425bded7cSSeth Forshee 
8853808843cSYunkang Tang static bool alps_is_valid_package_v7(struct psmouse *psmouse)
8863808843cSYunkang Tang {
8873808843cSYunkang Tang 	switch (psmouse->pktcnt) {
8883808843cSYunkang Tang 	case 3:
8893808843cSYunkang Tang 		return (psmouse->packet[2] & 0x40) == 0x40;
8903808843cSYunkang Tang 	case 4:
8913808843cSYunkang Tang 		return (psmouse->packet[3] & 0x48) == 0x48;
8923808843cSYunkang Tang 	case 6:
8933808843cSYunkang Tang 		return (psmouse->packet[5] & 0x40) == 0x00;
8943808843cSYunkang Tang 	}
8953808843cSYunkang Tang 	return true;
8963808843cSYunkang Tang }
8973808843cSYunkang Tang 
8983808843cSYunkang Tang static unsigned char alps_get_packet_id_v7(char *byte)
8993808843cSYunkang Tang {
9003808843cSYunkang Tang 	unsigned char packet_id;
9013808843cSYunkang Tang 
9023808843cSYunkang Tang 	if (byte[4] & 0x40)
9033808843cSYunkang Tang 		packet_id = V7_PACKET_ID_TWO;
9043808843cSYunkang Tang 	else if (byte[4] & 0x01)
9053808843cSYunkang Tang 		packet_id = V7_PACKET_ID_MULTI;
9063808843cSYunkang Tang 	else if ((byte[0] & 0x10) && !(byte[4] & 0x43))
9073808843cSYunkang Tang 		packet_id = V7_PACKET_ID_NEW;
9083808843cSYunkang Tang 	else if (byte[1] == 0x00 && byte[4] == 0x00)
9093808843cSYunkang Tang 		packet_id = V7_PACKET_ID_IDLE;
9103808843cSYunkang Tang 	else
9113808843cSYunkang Tang 		packet_id = V7_PACKET_ID_UNKNOWN;
9123808843cSYunkang Tang 
9133808843cSYunkang Tang 	return packet_id;
9143808843cSYunkang Tang }
9153808843cSYunkang Tang 
9163808843cSYunkang Tang static void alps_get_finger_coordinate_v7(struct input_mt_pos *mt,
9173808843cSYunkang Tang 					  unsigned char *pkt,
9183808843cSYunkang Tang 					  unsigned char pkt_id)
9193808843cSYunkang Tang {
9203808843cSYunkang Tang 	mt[0].x = ((pkt[2] & 0x80) << 4);
9213808843cSYunkang Tang 	mt[0].x |= ((pkt[2] & 0x3F) << 5);
9223808843cSYunkang Tang 	mt[0].x |= ((pkt[3] & 0x30) >> 1);
9233808843cSYunkang Tang 	mt[0].x |= (pkt[3] & 0x07);
9243808843cSYunkang Tang 	mt[0].y = (pkt[1] << 3) | (pkt[0] & 0x07);
9253808843cSYunkang Tang 
9263808843cSYunkang Tang 	mt[1].x = ((pkt[3] & 0x80) << 4);
9273808843cSYunkang Tang 	mt[1].x |= ((pkt[4] & 0x80) << 3);
9283808843cSYunkang Tang 	mt[1].x |= ((pkt[4] & 0x3F) << 4);
9293808843cSYunkang Tang 	mt[1].y = ((pkt[5] & 0x80) << 3);
9303808843cSYunkang Tang 	mt[1].y |= ((pkt[5] & 0x3F) << 4);
9313808843cSYunkang Tang 
9323808843cSYunkang Tang 	switch (pkt_id) {
9333808843cSYunkang Tang 	case V7_PACKET_ID_TWO:
9343808843cSYunkang Tang 		mt[1].x &= ~0x000F;
9353808843cSYunkang Tang 		mt[1].y |= 0x000F;
9363808843cSYunkang Tang 		break;
9373808843cSYunkang Tang 
9383808843cSYunkang Tang 	case V7_PACKET_ID_MULTI:
9393808843cSYunkang Tang 		mt[1].x &= ~0x003F;
9403808843cSYunkang Tang 		mt[1].y &= ~0x0020;
9413808843cSYunkang Tang 		mt[1].y |= ((pkt[4] & 0x02) << 4);
9423808843cSYunkang Tang 		mt[1].y |= 0x001F;
9433808843cSYunkang Tang 		break;
9443808843cSYunkang Tang 
9453808843cSYunkang Tang 	case V7_PACKET_ID_NEW:
9463808843cSYunkang Tang 		mt[1].x &= ~0x003F;
9473808843cSYunkang Tang 		mt[1].x |= (pkt[0] & 0x20);
9483808843cSYunkang Tang 		mt[1].y |= 0x000F;
9493808843cSYunkang Tang 		break;
9503808843cSYunkang Tang 	}
9513808843cSYunkang Tang 
9523808843cSYunkang Tang 	mt[0].y = 0x7FF - mt[0].y;
9533808843cSYunkang Tang 	mt[1].y = 0x7FF - mt[1].y;
9543808843cSYunkang Tang }
9553808843cSYunkang Tang 
9563808843cSYunkang Tang static int alps_get_mt_count(struct input_mt_pos *mt)
9573808843cSYunkang Tang {
9587091c443SHans de Goede 	int i, fingers = 0;
9593808843cSYunkang Tang 
9607091c443SHans de Goede 	for (i = 0; i < MAX_TOUCHES; i++) {
9617091c443SHans de Goede 		if (mt[i].x != 0 || mt[i].y != 0)
9627091c443SHans de Goede 			fingers++;
9637091c443SHans de Goede 	}
9643808843cSYunkang Tang 
9657091c443SHans de Goede 	return fingers;
9663808843cSYunkang Tang }
9673808843cSYunkang Tang 
9683808843cSYunkang Tang static int alps_decode_packet_v7(struct alps_fields *f,
9693808843cSYunkang Tang 				  unsigned char *p,
9703808843cSYunkang Tang 				  struct psmouse *psmouse)
9713808843cSYunkang Tang {
972d27eb793SHans de Goede 	struct alps_data *priv = psmouse->private;
9733808843cSYunkang Tang 	unsigned char pkt_id;
9743808843cSYunkang Tang 
9753808843cSYunkang Tang 	pkt_id = alps_get_packet_id_v7(p);
9763808843cSYunkang Tang 	if (pkt_id == V7_PACKET_ID_IDLE)
9773808843cSYunkang Tang 		return 0;
9783808843cSYunkang Tang 	if (pkt_id == V7_PACKET_ID_UNKNOWN)
9793808843cSYunkang Tang 		return -1;
9808b238115SHans de Goede 	/*
9818b238115SHans de Goede 	 * NEW packets are send to indicate a discontinuity in the finger
9828b238115SHans de Goede 	 * coordinate reporting. Specifically a finger may have moved from
9838b238115SHans de Goede 	 * slot 0 to 1 or vice versa. INPUT_MT_TRACK takes care of this for
9848b238115SHans de Goede 	 * us.
9858b238115SHans de Goede 	 *
9868b238115SHans de Goede 	 * NEW packets have 3 problems:
9878b238115SHans de Goede 	 * 1) They do not contain middle / right button info (on non clickpads)
9888b238115SHans de Goede 	 *    this can be worked around by preserving the old button state
9898b238115SHans de Goede 	 * 2) They do not contain an accurate fingercount, and they are
9908b238115SHans de Goede 	 *    typically send when the number of fingers changes. We cannot use
9918b238115SHans de Goede 	 *    the old finger count as that may mismatch with the amount of
9928b238115SHans de Goede 	 *    touch coordinates we've available in the NEW packet
9938b238115SHans de Goede 	 * 3) Their x data for the second touch is inaccurate leading to
9948b238115SHans de Goede 	 *    a possible jump of the x coordinate by 16 units when the first
9958b238115SHans de Goede 	 *    non NEW packet comes in
9968b238115SHans de Goede 	 * Since problems 2 & 3 cannot be worked around, just ignore them.
9978b238115SHans de Goede 	 */
9988b238115SHans de Goede 	if (pkt_id == V7_PACKET_ID_NEW)
9998b238115SHans de Goede 		return 1;
10003808843cSYunkang Tang 
10013808843cSYunkang Tang 	alps_get_finger_coordinate_v7(f->mt, p, pkt_id);
10023808843cSYunkang Tang 
10033808843cSYunkang Tang 	if (pkt_id == V7_PACKET_ID_TWO)
10043808843cSYunkang Tang 		f->fingers = alps_get_mt_count(f->mt);
10058b238115SHans de Goede 	else /* pkt_id == V7_PACKET_ID_MULTI */
10063808843cSYunkang Tang 		f->fingers = 3 + (p[5] & 0x03);
10073808843cSYunkang Tang 
1008d27eb793SHans de Goede 	f->left = (p[0] & 0x80) >> 7;
1009d27eb793SHans de Goede 	if (priv->flags & ALPS_BUTTONPAD) {
1010d27eb793SHans de Goede 		if (p[0] & 0x20)
1011d27eb793SHans de Goede 			f->fingers++;
1012d27eb793SHans de Goede 		if (p[0] & 0x10)
1013d27eb793SHans de Goede 			f->fingers++;
1014d27eb793SHans de Goede 	} else {
1015d27eb793SHans de Goede 		f->right = (p[0] & 0x20) >> 5;
1016d27eb793SHans de Goede 		f->middle = (p[0] & 0x10) >> 4;
1017d27eb793SHans de Goede 	}
1018d27eb793SHans de Goede 
10197091c443SHans de Goede 	/* Sometimes a single touch is reported in mt[1] rather then mt[0] */
10207091c443SHans de Goede 	if (f->fingers == 1 && f->mt[0].x == 0 && f->mt[0].y == 0) {
10217091c443SHans de Goede 		f->mt[0].x = f->mt[1].x;
10227091c443SHans de Goede 		f->mt[0].y = f->mt[1].y;
10237091c443SHans de Goede 		f->mt[1].x = 0;
10247091c443SHans de Goede 		f->mt[1].y = 0;
10257091c443SHans de Goede 	}
10267091c443SHans de Goede 
10273808843cSYunkang Tang 	return 0;
10283808843cSYunkang Tang }
10293808843cSYunkang Tang 
10303808843cSYunkang Tang static void alps_process_trackstick_packet_v7(struct psmouse *psmouse)
10313808843cSYunkang Tang {
10323808843cSYunkang Tang 	struct alps_data *priv = psmouse->private;
10333808843cSYunkang Tang 	unsigned char *packet = psmouse->packet;
10343808843cSYunkang Tang 	struct input_dev *dev2 = priv->dev2;
10353808843cSYunkang Tang 	int x, y, z, left, right, middle;
10363808843cSYunkang Tang 
103734412ba2SPali Rohár 	/* It should be a DualPoint when received trackstick packet */
103834412ba2SPali Rohár 	if (!(priv->flags & ALPS_DUALPOINT)) {
103934412ba2SPali Rohár 		psmouse_warn(psmouse,
104034412ba2SPali Rohár 			     "Rejected trackstick packet from non DualPoint device");
104134412ba2SPali Rohár 		return;
104234412ba2SPali Rohár 	}
104334412ba2SPali Rohár 
10443808843cSYunkang Tang 	x = ((packet[2] & 0xbf)) | ((packet[3] & 0x10) << 2);
10453808843cSYunkang Tang 	y = (packet[3] & 0x07) | (packet[4] & 0xb8) |
10463808843cSYunkang Tang 	    ((packet[3] & 0x20) << 1);
10473808843cSYunkang Tang 	z = (packet[5] & 0x3f) | ((packet[3] & 0x80) >> 1);
10483808843cSYunkang Tang 
10493808843cSYunkang Tang 	left = (packet[1] & 0x01);
10503808843cSYunkang Tang 	right = (packet[1] & 0x02) >> 1;
10513808843cSYunkang Tang 	middle = (packet[1] & 0x04) >> 2;
10523808843cSYunkang Tang 
10533808843cSYunkang Tang 	/* Divide 2 since trackpoint's speed is too fast */
10543808843cSYunkang Tang 	input_report_rel(dev2, REL_X, (char)x / 2);
10553808843cSYunkang Tang 	input_report_rel(dev2, REL_Y, -((char)y / 2));
10563808843cSYunkang Tang 
10573808843cSYunkang Tang 	input_report_key(dev2, BTN_LEFT, left);
10583808843cSYunkang Tang 	input_report_key(dev2, BTN_RIGHT, right);
10593808843cSYunkang Tang 	input_report_key(dev2, BTN_MIDDLE, middle);
10603808843cSYunkang Tang 
10613808843cSYunkang Tang 	input_sync(dev2);
10623808843cSYunkang Tang }
10633808843cSYunkang Tang 
10643808843cSYunkang Tang static void alps_process_touchpad_packet_v7(struct psmouse *psmouse)
10653808843cSYunkang Tang {
10663808843cSYunkang Tang 	struct alps_data *priv = psmouse->private;
10673808843cSYunkang Tang 	struct input_dev *dev = psmouse->dev;
10683808843cSYunkang Tang 	struct alps_fields *f = &priv->f;
10693808843cSYunkang Tang 
10703808843cSYunkang Tang 	memset(f, 0, sizeof(*f));
10713808843cSYunkang Tang 
10723808843cSYunkang Tang 	if (priv->decode_fields(f, psmouse->packet, psmouse))
10733808843cSYunkang Tang 		return;
10743808843cSYunkang Tang 
10753808843cSYunkang Tang 	alps_report_mt_data(psmouse, alps_get_mt_count(f->mt));
10763808843cSYunkang Tang 
10773808843cSYunkang Tang 	input_mt_report_finger_count(dev, f->fingers);
10783808843cSYunkang Tang 
10793808843cSYunkang Tang 	input_report_key(dev, BTN_LEFT, f->left);
10803808843cSYunkang Tang 	input_report_key(dev, BTN_RIGHT, f->right);
10813808843cSYunkang Tang 	input_report_key(dev, BTN_MIDDLE, f->middle);
10823808843cSYunkang Tang 
10833808843cSYunkang Tang 	input_sync(dev);
10843808843cSYunkang Tang }
10853808843cSYunkang Tang 
10863808843cSYunkang Tang static void alps_process_packet_v7(struct psmouse *psmouse)
10873808843cSYunkang Tang {
10883808843cSYunkang Tang 	unsigned char *packet = psmouse->packet;
10893808843cSYunkang Tang 
10903808843cSYunkang Tang 	if (packet[0] == 0x48 && (packet[4] & 0x47) == 0x06)
10913808843cSYunkang Tang 		alps_process_trackstick_packet_v7(psmouse);
10923808843cSYunkang Tang 	else
10933808843cSYunkang Tang 		alps_process_touchpad_packet_v7(psmouse);
10943808843cSYunkang Tang }
10953808843cSYunkang Tang 
109604aae283SPali Rohár static DEFINE_MUTEX(alps_mutex);
109704aae283SPali Rohár 
109804aae283SPali Rohár static void alps_register_bare_ps2_mouse(struct work_struct *work)
109904aae283SPali Rohár {
110004aae283SPali Rohár 	struct alps_data *priv =
110104aae283SPali Rohár 		container_of(work, struct alps_data, dev3_register_work.work);
110204aae283SPali Rohár 	struct psmouse *psmouse = priv->psmouse;
110304aae283SPali Rohár 	struct input_dev *dev3;
110404aae283SPali Rohár 	int error = 0;
110504aae283SPali Rohár 
110604aae283SPali Rohár 	mutex_lock(&alps_mutex);
110704aae283SPali Rohár 
110804aae283SPali Rohár 	if (priv->dev3)
110904aae283SPali Rohár 		goto out;
111004aae283SPali Rohár 
111104aae283SPali Rohár 	dev3 = input_allocate_device();
111204aae283SPali Rohár 	if (!dev3) {
111304aae283SPali Rohár 		psmouse_err(psmouse, "failed to allocate secondary device\n");
111404aae283SPali Rohár 		error = -ENOMEM;
111504aae283SPali Rohár 		goto out;
111604aae283SPali Rohár 	}
111704aae283SPali Rohár 
111804aae283SPali Rohár 	snprintf(priv->phys3, sizeof(priv->phys3), "%s/%s",
111904aae283SPali Rohár 		 psmouse->ps2dev.serio->phys,
112004aae283SPali Rohár 		 (priv->dev2 ? "input2" : "input1"));
112104aae283SPali Rohár 	dev3->phys = priv->phys3;
112204aae283SPali Rohár 
112304aae283SPali Rohár 	/*
112404aae283SPali Rohár 	 * format of input device name is: "protocol vendor name"
112504aae283SPali Rohár 	 * see function psmouse_switch_protocol() in psmouse-base.c
112604aae283SPali Rohár 	 */
112704aae283SPali Rohár 	dev3->name = "PS/2 ALPS Mouse";
112804aae283SPali Rohár 
112904aae283SPali Rohár 	dev3->id.bustype = BUS_I8042;
113004aae283SPali Rohár 	dev3->id.vendor  = 0x0002;
113104aae283SPali Rohár 	dev3->id.product = PSMOUSE_PS2;
113204aae283SPali Rohár 	dev3->id.version = 0x0000;
113304aae283SPali Rohár 	dev3->dev.parent = &psmouse->ps2dev.serio->dev;
113404aae283SPali Rohár 
113504aae283SPali Rohár 	input_set_capability(dev3, EV_REL, REL_X);
113604aae283SPali Rohár 	input_set_capability(dev3, EV_REL, REL_Y);
113704aae283SPali Rohár 	input_set_capability(dev3, EV_KEY, BTN_LEFT);
113804aae283SPali Rohár 	input_set_capability(dev3, EV_KEY, BTN_RIGHT);
113904aae283SPali Rohár 	input_set_capability(dev3, EV_KEY, BTN_MIDDLE);
114004aae283SPali Rohár 
114104aae283SPali Rohár 	__set_bit(INPUT_PROP_POINTER, dev3->propbit);
114204aae283SPali Rohár 
114304aae283SPali Rohár 	error = input_register_device(dev3);
114404aae283SPali Rohár 	if (error) {
114504aae283SPali Rohár 		psmouse_err(psmouse,
114604aae283SPali Rohár 			    "failed to register secondary device: %d\n",
114704aae283SPali Rohár 			    error);
114804aae283SPali Rohár 		input_free_device(dev3);
114904aae283SPali Rohár 		goto out;
115004aae283SPali Rohár 	}
115104aae283SPali Rohár 
115204aae283SPali Rohár 	priv->dev3 = dev3;
115304aae283SPali Rohár 
115404aae283SPali Rohár out:
115504aae283SPali Rohár 	/*
115604aae283SPali Rohár 	 * Save the error code so that we can detect that we
115704aae283SPali Rohár 	 * already tried to create the device.
115804aae283SPali Rohár 	 */
115904aae283SPali Rohár 	if (error)
116004aae283SPali Rohár 		priv->dev3 = ERR_PTR(error);
116104aae283SPali Rohár 
116204aae283SPali Rohár 	mutex_unlock(&alps_mutex);
116304aae283SPali Rohár }
116404aae283SPali Rohár 
116559c30afbSHans de Goede static void alps_report_bare_ps2_packet(struct psmouse *psmouse,
11661d9f2626SSebastian Kapfer 					unsigned char packet[],
11671d9f2626SSebastian Kapfer 					bool report_buttons)
11681d9f2626SSebastian Kapfer {
116959c30afbSHans de Goede 	struct alps_data *priv = psmouse->private;
11706bcca19fSHans de Goede 	struct input_dev *dev, *dev2 = NULL;
117159c30afbSHans de Goede 
1172e3a79212SHans de Goede 	/* Figure out which device to use to report the bare packet */
1173e3a79212SHans de Goede 	if (priv->proto_version == ALPS_PROTO_V2 &&
1174e3a79212SHans de Goede 	    (priv->flags & ALPS_DUALPOINT)) {
1175e3a79212SHans de Goede 		/* On V2 devices the DualPoint Stick reports bare packets */
1176e3a79212SHans de Goede 		dev = priv->dev2;
11776bcca19fSHans de Goede 		dev2 = psmouse->dev;
1178e3a79212SHans de Goede 	} else if (unlikely(IS_ERR_OR_NULL(priv->dev3))) {
117959c30afbSHans de Goede 		/* Register dev3 mouse if we received PS/2 packet first time */
118059c30afbSHans de Goede 		if (!IS_ERR(priv->dev3))
118159c30afbSHans de Goede 			psmouse_queue_work(psmouse, &priv->dev3_register_work,
118259c30afbSHans de Goede 					   0);
118359c30afbSHans de Goede 		return;
118459c30afbSHans de Goede 	} else {
118559c30afbSHans de Goede 		dev = priv->dev3;
118659c30afbSHans de Goede 	}
118759c30afbSHans de Goede 
11881d9f2626SSebastian Kapfer 	if (report_buttons)
11896bcca19fSHans de Goede 		alps_report_buttons(dev, dev2,
11901d9f2626SSebastian Kapfer 				packet[0] & 1, packet[0] & 2, packet[0] & 4);
11911d9f2626SSebastian Kapfer 
119204aae283SPali Rohár 	input_report_rel(dev, REL_X,
11931d9f2626SSebastian Kapfer 		packet[1] ? packet[1] - ((packet[0] << 4) & 0x100) : 0);
119404aae283SPali Rohár 	input_report_rel(dev, REL_Y,
11951d9f2626SSebastian Kapfer 		packet[2] ? ((packet[0] << 3) & 0x100) - packet[2] : 0);
11961d9f2626SSebastian Kapfer 
119704aae283SPali Rohár 	input_sync(dev);
11981d9f2626SSebastian Kapfer }
11991d9f2626SSebastian Kapfer 
12001d9f2626SSebastian Kapfer static psmouse_ret_t alps_handle_interleaved_ps2(struct psmouse *psmouse)
12011da177e4SLinus Torvalds {
12021da177e4SLinus Torvalds 	struct alps_data *priv = psmouse->private;
12031da177e4SLinus Torvalds 
12041d9f2626SSebastian Kapfer 	if (psmouse->pktcnt < 6)
12051d9f2626SSebastian Kapfer 		return PSMOUSE_GOOD_DATA;
12061d9f2626SSebastian Kapfer 
12071d9f2626SSebastian Kapfer 	if (psmouse->pktcnt == 6) {
12081d9f2626SSebastian Kapfer 		/*
12091d9f2626SSebastian Kapfer 		 * Start a timer to flush the packet if it ends up last
12101d9f2626SSebastian Kapfer 		 * 6-byte packet in the stream. Timer needs to fire
12111d9f2626SSebastian Kapfer 		 * psmouse core times out itself. 20 ms should be enough
12121d9f2626SSebastian Kapfer 		 * to decide if we are getting more data or not.
12131d9f2626SSebastian Kapfer 		 */
12141d9f2626SSebastian Kapfer 		mod_timer(&priv->timer, jiffies + msecs_to_jiffies(20));
12151d9f2626SSebastian Kapfer 		return PSMOUSE_GOOD_DATA;
12161d9f2626SSebastian Kapfer 	}
12171d9f2626SSebastian Kapfer 
12181d9f2626SSebastian Kapfer 	del_timer(&priv->timer);
12191d9f2626SSebastian Kapfer 
12201d9f2626SSebastian Kapfer 	if (psmouse->packet[6] & 0x80) {
12211d9f2626SSebastian Kapfer 
12221d9f2626SSebastian Kapfer 		/*
12231d9f2626SSebastian Kapfer 		 * Highest bit is set - that means we either had
12241d9f2626SSebastian Kapfer 		 * complete ALPS packet and this is start of the
12251d9f2626SSebastian Kapfer 		 * next packet or we got garbage.
12261d9f2626SSebastian Kapfer 		 */
12271d9f2626SSebastian Kapfer 
12281d9f2626SSebastian Kapfer 		if (((psmouse->packet[3] |
12291d9f2626SSebastian Kapfer 		      psmouse->packet[4] |
12301d9f2626SSebastian Kapfer 		      psmouse->packet[5]) & 0x80) ||
123199df65e7SKevin Cernekee 		    (!alps_is_valid_first_byte(priv, psmouse->packet[6]))) {
1232b5d21704SDmitry Torokhov 			psmouse_dbg(psmouse,
12333b112923SAndy Shevchenko 				    "refusing packet %4ph (suspected interleaved ps/2)\n",
12343b112923SAndy Shevchenko 				    psmouse->packet + 3);
12351d9f2626SSebastian Kapfer 			return PSMOUSE_BAD_DATA;
12361d9f2626SSebastian Kapfer 		}
12371d9f2626SSebastian Kapfer 
123824af5cb9SKevin Cernekee 		priv->process_packet(psmouse);
12391d9f2626SSebastian Kapfer 
12401d9f2626SSebastian Kapfer 		/* Continue with the next packet */
12411d9f2626SSebastian Kapfer 		psmouse->packet[0] = psmouse->packet[6];
12421d9f2626SSebastian Kapfer 		psmouse->pktcnt = 1;
12431d9f2626SSebastian Kapfer 
12441d9f2626SSebastian Kapfer 	} else {
12451d9f2626SSebastian Kapfer 
12461d9f2626SSebastian Kapfer 		/*
12471d9f2626SSebastian Kapfer 		 * High bit is 0 - that means that we indeed got a PS/2
12481d9f2626SSebastian Kapfer 		 * packet in the middle of ALPS packet.
12491d9f2626SSebastian Kapfer 		 *
12501d9f2626SSebastian Kapfer 		 * There is also possibility that we got 6-byte ALPS
12511d9f2626SSebastian Kapfer 		 * packet followed  by 3-byte packet from trackpoint. We
12521d9f2626SSebastian Kapfer 		 * can not distinguish between these 2 scenarios but
1253b5d21704SDmitry Torokhov 		 * because the latter is unlikely to happen in course of
12541d9f2626SSebastian Kapfer 		 * normal operation (user would need to press all
12551d9f2626SSebastian Kapfer 		 * buttons on the pad and start moving trackpoint
12561d9f2626SSebastian Kapfer 		 * without touching the pad surface) we assume former.
12571d9f2626SSebastian Kapfer 		 * Even if we are wrong the wost thing that would happen
12581d9f2626SSebastian Kapfer 		 * the cursor would jump but we should not get protocol
1259b5d21704SDmitry Torokhov 		 * de-synchronization.
12601d9f2626SSebastian Kapfer 		 */
12611d9f2626SSebastian Kapfer 
126259c30afbSHans de Goede 		alps_report_bare_ps2_packet(psmouse, &psmouse->packet[3],
126359c30afbSHans de Goede 					    false);
12641d9f2626SSebastian Kapfer 
12651d9f2626SSebastian Kapfer 		/*
12661d9f2626SSebastian Kapfer 		 * Continue with the standard ALPS protocol handling,
12671d9f2626SSebastian Kapfer 		 * but make sure we won't process it as an interleaved
12681d9f2626SSebastian Kapfer 		 * packet again, which may happen if all buttons are
12691d9f2626SSebastian Kapfer 		 * pressed. To avoid this let's reset the 4th bit which
12701d9f2626SSebastian Kapfer 		 * is normally 1.
12711d9f2626SSebastian Kapfer 		 */
12721d9f2626SSebastian Kapfer 		psmouse->packet[3] = psmouse->packet[6] & 0xf7;
12731d9f2626SSebastian Kapfer 		psmouse->pktcnt = 4;
12741d9f2626SSebastian Kapfer 	}
12751d9f2626SSebastian Kapfer 
12761d9f2626SSebastian Kapfer 	return PSMOUSE_GOOD_DATA;
12771d9f2626SSebastian Kapfer }
12781d9f2626SSebastian Kapfer 
12791d9f2626SSebastian Kapfer static void alps_flush_packet(unsigned long data)
12801d9f2626SSebastian Kapfer {
12811d9f2626SSebastian Kapfer 	struct psmouse *psmouse = (struct psmouse *)data;
128224af5cb9SKevin Cernekee 	struct alps_data *priv = psmouse->private;
12831d9f2626SSebastian Kapfer 
12841d9f2626SSebastian Kapfer 	serio_pause_rx(psmouse->ps2dev.serio);
12851d9f2626SSebastian Kapfer 
1286b46615feSSeth Forshee 	if (psmouse->pktcnt == psmouse->pktsize) {
12871d9f2626SSebastian Kapfer 
12881d9f2626SSebastian Kapfer 		/*
12891d9f2626SSebastian Kapfer 		 * We did not any more data in reasonable amount of time.
12901d9f2626SSebastian Kapfer 		 * Validate the last 3 bytes and process as a standard
12911d9f2626SSebastian Kapfer 		 * ALPS packet.
12921d9f2626SSebastian Kapfer 		 */
12931d9f2626SSebastian Kapfer 		if ((psmouse->packet[3] |
12941d9f2626SSebastian Kapfer 		     psmouse->packet[4] |
12951d9f2626SSebastian Kapfer 		     psmouse->packet[5]) & 0x80) {
1296b5d21704SDmitry Torokhov 			psmouse_dbg(psmouse,
12973b112923SAndy Shevchenko 				    "refusing packet %3ph (suspected interleaved ps/2)\n",
12983b112923SAndy Shevchenko 				    psmouse->packet + 3);
12991d9f2626SSebastian Kapfer 		} else {
130024af5cb9SKevin Cernekee 			priv->process_packet(psmouse);
13011d9f2626SSebastian Kapfer 		}
13021d9f2626SSebastian Kapfer 		psmouse->pktcnt = 0;
13031d9f2626SSebastian Kapfer 	}
13041d9f2626SSebastian Kapfer 
13051d9f2626SSebastian Kapfer 	serio_continue_rx(psmouse->ps2dev.serio);
13061d9f2626SSebastian Kapfer }
13071d9f2626SSebastian Kapfer 
13081d9f2626SSebastian Kapfer static psmouse_ret_t alps_process_byte(struct psmouse *psmouse)
13091d9f2626SSebastian Kapfer {
13101d9f2626SSebastian Kapfer 	struct alps_data *priv = psmouse->private;
13111d9f2626SSebastian Kapfer 
13124ab8f7f3SPali Rohár 	/*
13134ab8f7f3SPali Rohár 	 * Check if we are dealing with a bare PS/2 packet, presumably from
13144ab8f7f3SPali Rohár 	 * a device connected to the external PS/2 port. Because bare PS/2
13154ab8f7f3SPali Rohár 	 * protocol does not have enough constant bits to self-synchronize
13164ab8f7f3SPali Rohár 	 * properly we only do this if the device is fully synchronized.
13174ab8f7f3SPali Rohár 	 */
13184ab8f7f3SPali Rohár 	if (!psmouse->out_of_sync_cnt && (psmouse->packet[0] & 0xc8) == 0x08) {
13191da177e4SLinus Torvalds 		if (psmouse->pktcnt == 3) {
132059c30afbSHans de Goede 			alps_report_bare_ps2_packet(psmouse, psmouse->packet,
13211d9f2626SSebastian Kapfer 						    true);
13221da177e4SLinus Torvalds 			return PSMOUSE_FULL_PACKET;
13231da177e4SLinus Torvalds 		}
13241da177e4SLinus Torvalds 		return PSMOUSE_GOOD_DATA;
13251da177e4SLinus Torvalds 	}
13261da177e4SLinus Torvalds 
13271d9f2626SSebastian Kapfer 	/* Check for PS/2 packet stuffed in the middle of ALPS packet. */
13281d9f2626SSebastian Kapfer 
132999df65e7SKevin Cernekee 	if ((priv->flags & ALPS_PS2_INTERLEAVED) &&
13301d9f2626SSebastian Kapfer 	    psmouse->pktcnt >= 4 && (psmouse->packet[3] & 0x0f) == 0x0f) {
13311d9f2626SSebastian Kapfer 		return alps_handle_interleaved_ps2(psmouse);
13321d9f2626SSebastian Kapfer 	}
13331d9f2626SSebastian Kapfer 
133499df65e7SKevin Cernekee 	if (!alps_is_valid_first_byte(priv, psmouse->packet[0])) {
1335b5d21704SDmitry Torokhov 		psmouse_dbg(psmouse,
1336b5d21704SDmitry Torokhov 			    "refusing packet[0] = %x (mask0 = %x, byte0 = %x)\n",
133799df65e7SKevin Cernekee 			    psmouse->packet[0], priv->mask0, priv->byte0);
13381da177e4SLinus Torvalds 		return PSMOUSE_BAD_DATA;
13391d9f2626SSebastian Kapfer 	}
13401da177e4SLinus Torvalds 
1341b46615feSSeth Forshee 	/* Bytes 2 - pktsize should have 0 in the highest bit */
1342a7ef82aeSPali Rohár 	if (priv->proto_version < ALPS_PROTO_V5 &&
134375af9e56SDave Turvene 	    psmouse->pktcnt >= 2 && psmouse->pktcnt <= psmouse->pktsize &&
13441d9f2626SSebastian Kapfer 	    (psmouse->packet[psmouse->pktcnt - 1] & 0x80)) {
1345b5d21704SDmitry Torokhov 		psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
1346b5d21704SDmitry Torokhov 			    psmouse->pktcnt - 1,
1347b5d21704SDmitry Torokhov 			    psmouse->packet[psmouse->pktcnt - 1]);
1348a7ef82aeSPali Rohár 
1349fb2dd7a6SDmitry Torokhov 		if (priv->proto_version == ALPS_PROTO_V3_RUSHMORE &&
1350a7ef82aeSPali Rohár 		    psmouse->pktcnt == psmouse->pktsize) {
1351a7ef82aeSPali Rohár 			/*
1352a7ef82aeSPali Rohár 			 * Some Dell boxes, such as Latitude E6440 or E7440
1353a7ef82aeSPali Rohár 			 * with closed lid, quite often smash last byte of
1354a7ef82aeSPali Rohár 			 * otherwise valid packet with 0xff. Given that the
1355a7ef82aeSPali Rohár 			 * next packet is very likely to be valid let's
1356a7ef82aeSPali Rohár 			 * report PSMOUSE_FULL_PACKET but not process data,
1357a7ef82aeSPali Rohár 			 * rather than reporting PSMOUSE_BAD_DATA and
1358a7ef82aeSPali Rohár 			 * filling the logs.
1359a7ef82aeSPali Rohár 			 */
1360a7ef82aeSPali Rohár 			return PSMOUSE_FULL_PACKET;
1361a7ef82aeSPali Rohár 		}
1362a7ef82aeSPali Rohár 
13631da177e4SLinus Torvalds 		return PSMOUSE_BAD_DATA;
13641d9f2626SSebastian Kapfer 	}
13651da177e4SLinus Torvalds 
13663808843cSYunkang Tang 	if (priv->proto_version == ALPS_PROTO_V7 &&
13673808843cSYunkang Tang 	    !alps_is_valid_package_v7(psmouse)) {
13683808843cSYunkang Tang 		psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
13693808843cSYunkang Tang 			    psmouse->pktcnt - 1,
13703808843cSYunkang Tang 			    psmouse->packet[psmouse->pktcnt - 1]);
13713808843cSYunkang Tang 		return PSMOUSE_BAD_DATA;
13723808843cSYunkang Tang 	}
13733808843cSYunkang Tang 
1374b46615feSSeth Forshee 	if (psmouse->pktcnt == psmouse->pktsize) {
137524af5cb9SKevin Cernekee 		priv->process_packet(psmouse);
13761da177e4SLinus Torvalds 		return PSMOUSE_FULL_PACKET;
13771da177e4SLinus Torvalds 	}
13781da177e4SLinus Torvalds 
13791da177e4SLinus Torvalds 	return PSMOUSE_GOOD_DATA;
13801da177e4SLinus Torvalds }
13811da177e4SLinus Torvalds 
138225bded7cSSeth Forshee static int alps_command_mode_send_nibble(struct psmouse *psmouse, int nibble)
138325bded7cSSeth Forshee {
138425bded7cSSeth Forshee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
138525bded7cSSeth Forshee 	struct alps_data *priv = psmouse->private;
138625bded7cSSeth Forshee 	int command;
138725bded7cSSeth Forshee 	unsigned char *param;
138825bded7cSSeth Forshee 	unsigned char dummy[4];
138925bded7cSSeth Forshee 
139025bded7cSSeth Forshee 	BUG_ON(nibble > 0xf);
139125bded7cSSeth Forshee 
139225bded7cSSeth Forshee 	command = priv->nibble_commands[nibble].command;
139325bded7cSSeth Forshee 	param = (command & 0x0f00) ?
139425bded7cSSeth Forshee 		dummy : (unsigned char *)&priv->nibble_commands[nibble].data;
139525bded7cSSeth Forshee 
139625bded7cSSeth Forshee 	if (ps2_command(ps2dev, param, command))
139725bded7cSSeth Forshee 		return -1;
139825bded7cSSeth Forshee 
139925bded7cSSeth Forshee 	return 0;
140025bded7cSSeth Forshee }
140125bded7cSSeth Forshee 
140225bded7cSSeth Forshee static int alps_command_mode_set_addr(struct psmouse *psmouse, int addr)
140325bded7cSSeth Forshee {
140425bded7cSSeth Forshee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
140525bded7cSSeth Forshee 	struct alps_data *priv = psmouse->private;
140625bded7cSSeth Forshee 	int i, nibble;
140725bded7cSSeth Forshee 
140825bded7cSSeth Forshee 	if (ps2_command(ps2dev, NULL, priv->addr_command))
140925bded7cSSeth Forshee 		return -1;
141025bded7cSSeth Forshee 
141125bded7cSSeth Forshee 	for (i = 12; i >= 0; i -= 4) {
141225bded7cSSeth Forshee 		nibble = (addr >> i) & 0xf;
141325bded7cSSeth Forshee 		if (alps_command_mode_send_nibble(psmouse, nibble))
141425bded7cSSeth Forshee 			return -1;
141525bded7cSSeth Forshee 	}
141625bded7cSSeth Forshee 
141725bded7cSSeth Forshee 	return 0;
141825bded7cSSeth Forshee }
141925bded7cSSeth Forshee 
142025bded7cSSeth Forshee static int __alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
142125bded7cSSeth Forshee {
142225bded7cSSeth Forshee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
142325bded7cSSeth Forshee 	unsigned char param[4];
142425bded7cSSeth Forshee 
142525bded7cSSeth Forshee 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
142625bded7cSSeth Forshee 		return -1;
142725bded7cSSeth Forshee 
142825bded7cSSeth Forshee 	/*
142925bded7cSSeth Forshee 	 * The address being read is returned in the first two bytes
143025bded7cSSeth Forshee 	 * of the result. Check that this address matches the expected
143125bded7cSSeth Forshee 	 * address.
143225bded7cSSeth Forshee 	 */
143325bded7cSSeth Forshee 	if (addr != ((param[0] << 8) | param[1]))
143425bded7cSSeth Forshee 		return -1;
143525bded7cSSeth Forshee 
143625bded7cSSeth Forshee 	return param[2];
143725bded7cSSeth Forshee }
143825bded7cSSeth Forshee 
143925bded7cSSeth Forshee static int alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
144025bded7cSSeth Forshee {
144125bded7cSSeth Forshee 	if (alps_command_mode_set_addr(psmouse, addr))
144225bded7cSSeth Forshee 		return -1;
144325bded7cSSeth Forshee 	return __alps_command_mode_read_reg(psmouse, addr);
144425bded7cSSeth Forshee }
144525bded7cSSeth Forshee 
144625bded7cSSeth Forshee static int __alps_command_mode_write_reg(struct psmouse *psmouse, u8 value)
144725bded7cSSeth Forshee {
144825bded7cSSeth Forshee 	if (alps_command_mode_send_nibble(psmouse, (value >> 4) & 0xf))
144925bded7cSSeth Forshee 		return -1;
145025bded7cSSeth Forshee 	if (alps_command_mode_send_nibble(psmouse, value & 0xf))
145125bded7cSSeth Forshee 		return -1;
145225bded7cSSeth Forshee 	return 0;
145325bded7cSSeth Forshee }
145425bded7cSSeth Forshee 
145525bded7cSSeth Forshee static int alps_command_mode_write_reg(struct psmouse *psmouse, int addr,
145625bded7cSSeth Forshee 				       u8 value)
145725bded7cSSeth Forshee {
145825bded7cSSeth Forshee 	if (alps_command_mode_set_addr(psmouse, addr))
145925bded7cSSeth Forshee 		return -1;
146025bded7cSSeth Forshee 	return __alps_command_mode_write_reg(psmouse, value);
146125bded7cSSeth Forshee }
146225bded7cSSeth Forshee 
146324ba9707SKevin Cernekee static int alps_rpt_cmd(struct psmouse *psmouse, int init_command,
146424ba9707SKevin Cernekee 			int repeated_command, unsigned char *param)
146524ba9707SKevin Cernekee {
146624ba9707SKevin Cernekee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
146724ba9707SKevin Cernekee 
146824ba9707SKevin Cernekee 	param[0] = 0;
146924ba9707SKevin Cernekee 	if (init_command && ps2_command(ps2dev, param, init_command))
147024ba9707SKevin Cernekee 		return -EIO;
147124ba9707SKevin Cernekee 
147224ba9707SKevin Cernekee 	if (ps2_command(ps2dev,  NULL, repeated_command) ||
147324ba9707SKevin Cernekee 	    ps2_command(ps2dev,  NULL, repeated_command) ||
147424ba9707SKevin Cernekee 	    ps2_command(ps2dev,  NULL, repeated_command))
147524ba9707SKevin Cernekee 		return -EIO;
147624ba9707SKevin Cernekee 
147724ba9707SKevin Cernekee 	param[0] = param[1] = param[2] = 0xff;
147824ba9707SKevin Cernekee 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
147924ba9707SKevin Cernekee 		return -EIO;
148024ba9707SKevin Cernekee 
148139fbe585SDmitry Torokhov 	psmouse_dbg(psmouse, "%2.2X report: %3ph\n",
148239fbe585SDmitry Torokhov 		    repeated_command, param);
148324ba9707SKevin Cernekee 	return 0;
148424ba9707SKevin Cernekee }
148524ba9707SKevin Cernekee 
14863808843cSYunkang Tang static bool alps_check_valid_firmware_id(unsigned char id[])
14873808843cSYunkang Tang {
14883808843cSYunkang Tang 	if (id[0] == 0x73)
14893808843cSYunkang Tang 		return true;
14903808843cSYunkang Tang 
14913808843cSYunkang Tang 	if (id[0] == 0x88 &&
14923808843cSYunkang Tang 	    (id[1] == 0x07 ||
14933808843cSYunkang Tang 	     id[1] == 0x08 ||
14943808843cSYunkang Tang 	     (id[1] & 0xf0) == 0xb0 ||
14953808843cSYunkang Tang 	     (id[1] & 0xf0) == 0xc0)) {
14963808843cSYunkang Tang 		return true;
14973808843cSYunkang Tang 	}
14983808843cSYunkang Tang 
14993808843cSYunkang Tang 	return false;
15003808843cSYunkang Tang }
15013808843cSYunkang Tang 
1502d18e53fcSKevin Cernekee static int alps_enter_command_mode(struct psmouse *psmouse)
150325bded7cSSeth Forshee {
150425bded7cSSeth Forshee 	unsigned char param[4];
150525bded7cSSeth Forshee 
150624ba9707SKevin Cernekee 	if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_RESET_WRAP, param)) {
150725bded7cSSeth Forshee 		psmouse_err(psmouse, "failed to enter command mode\n");
150825bded7cSSeth Forshee 		return -1;
150925bded7cSSeth Forshee 	}
151025bded7cSSeth Forshee 
15113808843cSYunkang Tang 	if (!alps_check_valid_firmware_id(param)) {
151225bded7cSSeth Forshee 		psmouse_dbg(psmouse,
151324ba9707SKevin Cernekee 			    "unknown response while entering command mode\n");
151425bded7cSSeth Forshee 		return -1;
151525bded7cSSeth Forshee 	}
151625bded7cSSeth Forshee 	return 0;
151725bded7cSSeth Forshee }
151825bded7cSSeth Forshee 
151925bded7cSSeth Forshee static inline int alps_exit_command_mode(struct psmouse *psmouse)
152025bded7cSSeth Forshee {
152125bded7cSSeth Forshee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
152225bded7cSSeth Forshee 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM))
152325bded7cSSeth Forshee 		return -1;
152425bded7cSSeth Forshee 	return 0;
152525bded7cSSeth Forshee }
152625bded7cSSeth Forshee 
15271da177e4SLinus Torvalds /*
15281da177e4SLinus Torvalds  * For DualPoint devices select the device that should respond to
15291da177e4SLinus Torvalds  * subsequent commands. It looks like glidepad is behind stickpointer,
15301da177e4SLinus Torvalds  * I'd thought it would be other way around...
15311da177e4SLinus Torvalds  */
153225bded7cSSeth Forshee static int alps_passthrough_mode_v2(struct psmouse *psmouse, bool enable)
15331da177e4SLinus Torvalds {
15341da177e4SLinus Torvalds 	struct ps2dev *ps2dev = &psmouse->ps2dev;
15351da177e4SLinus Torvalds 	int cmd = enable ? PSMOUSE_CMD_SETSCALE21 : PSMOUSE_CMD_SETSCALE11;
15361da177e4SLinus Torvalds 
15371da177e4SLinus Torvalds 	if (ps2_command(ps2dev, NULL, cmd) ||
15381da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, cmd) ||
15391da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, cmd) ||
15401da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
15411da177e4SLinus Torvalds 		return -1;
15421da177e4SLinus Torvalds 
15431da177e4SLinus Torvalds 	/* we may get 3 more bytes, just ignore them */
1544c611763dSDmitry Torokhov 	ps2_drain(ps2dev, 3, 100);
15451da177e4SLinus Torvalds 
15461da177e4SLinus Torvalds 	return 0;
15471da177e4SLinus Torvalds }
15481da177e4SLinus Torvalds 
154925bded7cSSeth Forshee static int alps_absolute_mode_v1_v2(struct psmouse *psmouse)
15501da177e4SLinus Torvalds {
15511da177e4SLinus Torvalds 	struct ps2dev *ps2dev = &psmouse->ps2dev;
15521da177e4SLinus Torvalds 
15531da177e4SLinus Torvalds 	/* Try ALPS magic knock - 4 disable before enable */
15541da177e4SLinus Torvalds 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
15551da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
15561da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
15571da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
15581da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
15591da177e4SLinus Torvalds 		return -1;
15601da177e4SLinus Torvalds 
15611da177e4SLinus Torvalds 	/*
15621da177e4SLinus Torvalds 	 * Switch mouse to poll (remote) mode so motion data will not
15631da177e4SLinus Torvalds 	 * get in our way
15641da177e4SLinus Torvalds 	 */
15651da177e4SLinus Torvalds 	return ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETPOLL);
15661da177e4SLinus Torvalds }
15671da177e4SLinus Torvalds 
156895f75e91SYunkang Tang static int alps_monitor_mode_send_word(struct psmouse *psmouse, u16 word)
156995f75e91SYunkang Tang {
157095f75e91SYunkang Tang 	int i, nibble;
157195f75e91SYunkang Tang 
157295f75e91SYunkang Tang 	/*
157395f75e91SYunkang Tang 	 * b0-b11 are valid bits, send sequence is inverse.
157495f75e91SYunkang Tang 	 * e.g. when word = 0x0123, nibble send sequence is 3, 2, 1
157595f75e91SYunkang Tang 	 */
157695f75e91SYunkang Tang 	for (i = 0; i <= 8; i += 4) {
157795f75e91SYunkang Tang 		nibble = (word >> i) & 0xf;
157895f75e91SYunkang Tang 		if (alps_command_mode_send_nibble(psmouse, nibble))
157995f75e91SYunkang Tang 			return -1;
158095f75e91SYunkang Tang 	}
158195f75e91SYunkang Tang 
158295f75e91SYunkang Tang 	return 0;
158395f75e91SYunkang Tang }
158495f75e91SYunkang Tang 
158595f75e91SYunkang Tang static int alps_monitor_mode_write_reg(struct psmouse *psmouse,
158695f75e91SYunkang Tang 				       u16 addr, u16 value)
158795f75e91SYunkang Tang {
158895f75e91SYunkang Tang 	struct ps2dev *ps2dev = &psmouse->ps2dev;
158995f75e91SYunkang Tang 
159095f75e91SYunkang Tang 	/* 0x0A0 is the command to write the word */
159195f75e91SYunkang Tang 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE) ||
159295f75e91SYunkang Tang 	    alps_monitor_mode_send_word(psmouse, 0x0A0) ||
159395f75e91SYunkang Tang 	    alps_monitor_mode_send_word(psmouse, addr) ||
159495f75e91SYunkang Tang 	    alps_monitor_mode_send_word(psmouse, value) ||
159595f75e91SYunkang Tang 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
159695f75e91SYunkang Tang 		return -1;
159795f75e91SYunkang Tang 
159895f75e91SYunkang Tang 	return 0;
159995f75e91SYunkang Tang }
160095f75e91SYunkang Tang 
160195f75e91SYunkang Tang static int alps_monitor_mode(struct psmouse *psmouse, bool enable)
160295f75e91SYunkang Tang {
160395f75e91SYunkang Tang 	struct ps2dev *ps2dev = &psmouse->ps2dev;
160495f75e91SYunkang Tang 
160595f75e91SYunkang Tang 	if (enable) {
160695f75e91SYunkang Tang 		/* EC E9 F5 F5 E7 E6 E7 E9 to enter monitor mode */
160795f75e91SYunkang Tang 		if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
160895f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO) ||
160995f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
161095f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
161195f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
161295f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
161395f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
161495f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO))
161595f75e91SYunkang Tang 			return -1;
161695f75e91SYunkang Tang 	} else {
161795f75e91SYunkang Tang 		/* EC to exit monitor mode */
161895f75e91SYunkang Tang 		if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP))
161995f75e91SYunkang Tang 			return -1;
162095f75e91SYunkang Tang 	}
162195f75e91SYunkang Tang 
162295f75e91SYunkang Tang 	return 0;
162395f75e91SYunkang Tang }
162495f75e91SYunkang Tang 
162595f75e91SYunkang Tang static int alps_absolute_mode_v6(struct psmouse *psmouse)
162695f75e91SYunkang Tang {
162795f75e91SYunkang Tang 	u16 reg_val = 0x181;
162895f75e91SYunkang Tang 	int ret = -1;
162995f75e91SYunkang Tang 
163095f75e91SYunkang Tang 	/* enter monitor mode, to write the register */
163195f75e91SYunkang Tang 	if (alps_monitor_mode(psmouse, true))
163295f75e91SYunkang Tang 		return -1;
163395f75e91SYunkang Tang 
163495f75e91SYunkang Tang 	ret = alps_monitor_mode_write_reg(psmouse, 0x000, reg_val);
163595f75e91SYunkang Tang 
163695f75e91SYunkang Tang 	if (alps_monitor_mode(psmouse, false))
163795f75e91SYunkang Tang 		ret = -1;
163895f75e91SYunkang Tang 
163995f75e91SYunkang Tang 	return ret;
164095f75e91SYunkang Tang }
164195f75e91SYunkang Tang 
16421da177e4SLinus Torvalds static int alps_get_status(struct psmouse *psmouse, char *param)
16431da177e4SLinus Torvalds {
16441da177e4SLinus Torvalds 	/* Get status: 0xF5 0xF5 0xF5 0xE9 */
164524ba9707SKevin Cernekee 	if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_DISABLE, param))
16461da177e4SLinus Torvalds 		return -1;
16471da177e4SLinus Torvalds 
16481da177e4SLinus Torvalds 	return 0;
16491da177e4SLinus Torvalds }
16501da177e4SLinus Torvalds 
16511da177e4SLinus Torvalds /*
16521da177e4SLinus Torvalds  * Turn touchpad tapping on or off. The sequences are:
16531da177e4SLinus Torvalds  * 0xE9 0xF5 0xF5 0xF3 0x0A to enable,
16541da177e4SLinus Torvalds  * 0xE9 0xF5 0xF5 0xE8 0x00 to disable.
16551da177e4SLinus Torvalds  * My guess that 0xE9 (GetInfo) is here as a sync point.
16561da177e4SLinus Torvalds  * For models that also have stickpointer (DualPoints) its tapping
16571da177e4SLinus Torvalds  * is controlled separately (0xE6 0xE6 0xE6 0xF3 0x14|0x0A) but
16581da177e4SLinus Torvalds  * we don't fiddle with it.
16591da177e4SLinus Torvalds  */
16601da177e4SLinus Torvalds static int alps_tap_mode(struct psmouse *psmouse, int enable)
16611da177e4SLinus Torvalds {
16621da177e4SLinus Torvalds 	struct ps2dev *ps2dev = &psmouse->ps2dev;
16631da177e4SLinus Torvalds 	int cmd = enable ? PSMOUSE_CMD_SETRATE : PSMOUSE_CMD_SETRES;
16641da177e4SLinus Torvalds 	unsigned char tap_arg = enable ? 0x0A : 0x00;
16651da177e4SLinus Torvalds 	unsigned char param[4];
16661da177e4SLinus Torvalds 
16671da177e4SLinus Torvalds 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO) ||
16681da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
16691da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
16701da177e4SLinus Torvalds 	    ps2_command(ps2dev, &tap_arg, cmd))
16711da177e4SLinus Torvalds 		return -1;
16721da177e4SLinus Torvalds 
16731da177e4SLinus Torvalds 	if (alps_get_status(psmouse, param))
16741da177e4SLinus Torvalds 		return -1;
16751da177e4SLinus Torvalds 
16761da177e4SLinus Torvalds 	return 0;
16771da177e4SLinus Torvalds }
16781da177e4SLinus Torvalds 
1679f0d5c6f4SDmitry Torokhov /*
1680f0d5c6f4SDmitry Torokhov  * alps_poll() - poll the touchpad for current motion packet.
1681f0d5c6f4SDmitry Torokhov  * Used in resync.
1682f0d5c6f4SDmitry Torokhov  */
1683f0d5c6f4SDmitry Torokhov static int alps_poll(struct psmouse *psmouse)
1684f0d5c6f4SDmitry Torokhov {
1685f0d5c6f4SDmitry Torokhov 	struct alps_data *priv = psmouse->private;
1686b46615feSSeth Forshee 	unsigned char buf[sizeof(psmouse->packet)];
1687b7802c5cSDmitry Torokhov 	bool poll_failed;
1688f0d5c6f4SDmitry Torokhov 
168999df65e7SKevin Cernekee 	if (priv->flags & ALPS_PASS)
169025bded7cSSeth Forshee 		alps_passthrough_mode_v2(psmouse, true);
1691f0d5c6f4SDmitry Torokhov 
1692f0d5c6f4SDmitry Torokhov 	poll_failed = ps2_command(&psmouse->ps2dev, buf,
1693f0d5c6f4SDmitry Torokhov 				  PSMOUSE_CMD_POLL | (psmouse->pktsize << 8)) < 0;
1694f0d5c6f4SDmitry Torokhov 
169599df65e7SKevin Cernekee 	if (priv->flags & ALPS_PASS)
169625bded7cSSeth Forshee 		alps_passthrough_mode_v2(psmouse, false);
1697f0d5c6f4SDmitry Torokhov 
169899df65e7SKevin Cernekee 	if (poll_failed || (buf[0] & priv->mask0) != priv->byte0)
1699f0d5c6f4SDmitry Torokhov 		return -1;
1700f0d5c6f4SDmitry Torokhov 
1701f0d5c6f4SDmitry Torokhov 	if ((psmouse->badbyte & 0xc8) == 0x08) {
1702f0d5c6f4SDmitry Torokhov /*
1703f0d5c6f4SDmitry Torokhov  * Poll the track stick ...
1704f0d5c6f4SDmitry Torokhov  */
1705f0d5c6f4SDmitry Torokhov 		if (ps2_command(&psmouse->ps2dev, buf, PSMOUSE_CMD_POLL | (3 << 8)))
1706f0d5c6f4SDmitry Torokhov 			return -1;
1707f0d5c6f4SDmitry Torokhov 	}
1708f0d5c6f4SDmitry Torokhov 
1709f0d5c6f4SDmitry Torokhov 	memcpy(psmouse->packet, buf, sizeof(buf));
1710f0d5c6f4SDmitry Torokhov 	return 0;
1711f0d5c6f4SDmitry Torokhov }
1712f0d5c6f4SDmitry Torokhov 
171325bded7cSSeth Forshee static int alps_hw_init_v1_v2(struct psmouse *psmouse)
17141da177e4SLinus Torvalds {
17151da177e4SLinus Torvalds 	struct alps_data *priv = psmouse->private;
17161da177e4SLinus Torvalds 
171799df65e7SKevin Cernekee 	if ((priv->flags & ALPS_PASS) &&
171825bded7cSSeth Forshee 	    alps_passthrough_mode_v2(psmouse, true)) {
17191da177e4SLinus Torvalds 		return -1;
1720b7802c5cSDmitry Torokhov 	}
17211da177e4SLinus Torvalds 
1722b7802c5cSDmitry Torokhov 	if (alps_tap_mode(psmouse, true)) {
1723b5d21704SDmitry Torokhov 		psmouse_warn(psmouse, "Failed to enable hardware tapping\n");
17241da177e4SLinus Torvalds 		return -1;
1725963f626dSPeter Osterlund 	}
17261da177e4SLinus Torvalds 
172725bded7cSSeth Forshee 	if (alps_absolute_mode_v1_v2(psmouse)) {
1728b5d21704SDmitry Torokhov 		psmouse_err(psmouse, "Failed to enable absolute mode\n");
17291da177e4SLinus Torvalds 		return -1;
17301da177e4SLinus Torvalds 	}
17311da177e4SLinus Torvalds 
173299df65e7SKevin Cernekee 	if ((priv->flags & ALPS_PASS) &&
173325bded7cSSeth Forshee 	    alps_passthrough_mode_v2(psmouse, false)) {
17341da177e4SLinus Torvalds 		return -1;
1735b7802c5cSDmitry Torokhov 	}
17361da177e4SLinus Torvalds 
17371e0c5b12SDmitry Torokhov 	/* ALPS needs stream mode, otherwise it won't report any data */
17381e0c5b12SDmitry Torokhov 	if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)) {
1739b5d21704SDmitry Torokhov 		psmouse_err(psmouse, "Failed to enable stream mode\n");
17401e0c5b12SDmitry Torokhov 		return -1;
17411e0c5b12SDmitry Torokhov 	}
17421e0c5b12SDmitry Torokhov 
17431e0c5b12SDmitry Torokhov 	return 0;
17441e0c5b12SDmitry Torokhov }
17451e0c5b12SDmitry Torokhov 
174695f75e91SYunkang Tang static int alps_hw_init_v6(struct psmouse *psmouse)
174795f75e91SYunkang Tang {
174895f75e91SYunkang Tang 	unsigned char param[2] = {0xC8, 0x14};
174995f75e91SYunkang Tang 
175095f75e91SYunkang Tang 	/* Enter passthrough mode to let trackpoint enter 6byte raw mode */
175195f75e91SYunkang Tang 	if (alps_passthrough_mode_v2(psmouse, true))
175295f75e91SYunkang Tang 		return -1;
175395f75e91SYunkang Tang 
175495f75e91SYunkang Tang 	if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
175595f75e91SYunkang Tang 	    ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
175695f75e91SYunkang Tang 	    ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
175795f75e91SYunkang Tang 	    ps2_command(&psmouse->ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
175895f75e91SYunkang Tang 	    ps2_command(&psmouse->ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
175995f75e91SYunkang Tang 		return -1;
176095f75e91SYunkang Tang 
176195f75e91SYunkang Tang 	if (alps_passthrough_mode_v2(psmouse, false))
176295f75e91SYunkang Tang 		return -1;
176395f75e91SYunkang Tang 
176495f75e91SYunkang Tang 	if (alps_absolute_mode_v6(psmouse)) {
176595f75e91SYunkang Tang 		psmouse_err(psmouse, "Failed to enable absolute mode\n");
176695f75e91SYunkang Tang 		return -1;
176795f75e91SYunkang Tang 	}
176895f75e91SYunkang Tang 
176995f75e91SYunkang Tang 	return 0;
177095f75e91SYunkang Tang }
177195f75e91SYunkang Tang 
177225bded7cSSeth Forshee /*
1773cd401204SKevin Cernekee  * Enable or disable passthrough mode to the trackstick.
177425bded7cSSeth Forshee  */
1775cd401204SKevin Cernekee static int alps_passthrough_mode_v3(struct psmouse *psmouse,
1776cd401204SKevin Cernekee 				    int reg_base, bool enable)
177725bded7cSSeth Forshee {
1778cd401204SKevin Cernekee 	int reg_val, ret = -1;
177925bded7cSSeth Forshee 
1780d18e53fcSKevin Cernekee 	if (alps_enter_command_mode(psmouse))
178125bded7cSSeth Forshee 		return -1;
178225bded7cSSeth Forshee 
1783cd401204SKevin Cernekee 	reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x0008);
1784cd401204SKevin Cernekee 	if (reg_val == -1)
1785cd401204SKevin Cernekee 		goto error;
1786cd401204SKevin Cernekee 
178725bded7cSSeth Forshee 	if (enable)
178825bded7cSSeth Forshee 		reg_val |= 0x01;
178925bded7cSSeth Forshee 	else
179025bded7cSSeth Forshee 		reg_val &= ~0x01;
179125bded7cSSeth Forshee 
1792cd401204SKevin Cernekee 	ret = __alps_command_mode_write_reg(psmouse, reg_val);
179325bded7cSSeth Forshee 
1794cd401204SKevin Cernekee error:
1795cd401204SKevin Cernekee 	if (alps_exit_command_mode(psmouse))
1796cd401204SKevin Cernekee 		ret = -1;
1797cd401204SKevin Cernekee 	return ret;
179825bded7cSSeth Forshee }
179925bded7cSSeth Forshee 
180025bded7cSSeth Forshee /* Must be in command mode when calling this function */
180125bded7cSSeth Forshee static int alps_absolute_mode_v3(struct psmouse *psmouse)
180225bded7cSSeth Forshee {
180325bded7cSSeth Forshee 	int reg_val;
180425bded7cSSeth Forshee 
180525bded7cSSeth Forshee 	reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
180625bded7cSSeth Forshee 	if (reg_val == -1)
180725bded7cSSeth Forshee 		return -1;
180825bded7cSSeth Forshee 
180925bded7cSSeth Forshee 	reg_val |= 0x06;
181025bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, reg_val))
181125bded7cSSeth Forshee 		return -1;
181225bded7cSSeth Forshee 
181325bded7cSSeth Forshee 	return 0;
181425bded7cSSeth Forshee }
181525bded7cSSeth Forshee 
1816cd401204SKevin Cernekee static int alps_probe_trackstick_v3(struct psmouse *psmouse, int reg_base)
181725bded7cSSeth Forshee {
1818cd401204SKevin Cernekee 	int ret = -EIO, reg_val;
181925bded7cSSeth Forshee 
1820d18e53fcSKevin Cernekee 	if (alps_enter_command_mode(psmouse))
182125bded7cSSeth Forshee 		goto error;
182225bded7cSSeth Forshee 
1823cd401204SKevin Cernekee 	reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x08);
182425bded7cSSeth Forshee 	if (reg_val == -1)
182525bded7cSSeth Forshee 		goto error;
1826cd401204SKevin Cernekee 
1827cd401204SKevin Cernekee 	/* bit 7: trackstick is present */
1828cd401204SKevin Cernekee 	ret = reg_val & 0x80 ? 0 : -ENODEV;
1829cd401204SKevin Cernekee 
1830cd401204SKevin Cernekee error:
1831cd401204SKevin Cernekee 	alps_exit_command_mode(psmouse);
1832cd401204SKevin Cernekee 	return ret;
1833cd401204SKevin Cernekee }
1834cd401204SKevin Cernekee 
1835cd401204SKevin Cernekee static int alps_setup_trackstick_v3(struct psmouse *psmouse, int reg_base)
1836cd401204SKevin Cernekee {
1837cd401204SKevin Cernekee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
1838cd401204SKevin Cernekee 	int ret = 0;
1839cd401204SKevin Cernekee 	unsigned char param[4];
1840cd401204SKevin Cernekee 
1841cd401204SKevin Cernekee 	if (alps_passthrough_mode_v3(psmouse, reg_base, true))
1842cd401204SKevin Cernekee 		return -EIO;
184325bded7cSSeth Forshee 
184425bded7cSSeth Forshee 	/*
184525bded7cSSeth Forshee 	 * E7 report for the trackstick
184625bded7cSSeth Forshee 	 *
184725bded7cSSeth Forshee 	 * There have been reports of failures to seem to trace back
184825bded7cSSeth Forshee 	 * to the above trackstick check failing. When these occur
184925bded7cSSeth Forshee 	 * this E7 report fails, so when that happens we continue
185025bded7cSSeth Forshee 	 * with the assumption that there isn't a trackstick after
185125bded7cSSeth Forshee 	 * all.
185225bded7cSSeth Forshee 	 */
1853cd401204SKevin Cernekee 	if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_SETSCALE21, param)) {
1854a09221e8SDmitry Torokhov 		psmouse_warn(psmouse, "Failed to initialize trackstick (E7 report failed)\n");
1855cd401204SKevin Cernekee 		ret = -ENODEV;
185625bded7cSSeth Forshee 	} else {
185739fbe585SDmitry Torokhov 		psmouse_dbg(psmouse, "trackstick E7 report: %3ph\n", param);
185825bded7cSSeth Forshee 
185925bded7cSSeth Forshee 		/*
186025bded7cSSeth Forshee 		 * Not sure what this does, but it is absolutely
186125bded7cSSeth Forshee 		 * essential. Without it, the touchpad does not
186225bded7cSSeth Forshee 		 * work at all and the trackstick just emits normal
186325bded7cSSeth Forshee 		 * PS/2 packets.
186425bded7cSSeth Forshee 		 */
186525bded7cSSeth Forshee 		if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
186625bded7cSSeth Forshee 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
186725bded7cSSeth Forshee 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
186825bded7cSSeth Forshee 		    alps_command_mode_send_nibble(psmouse, 0x9) ||
186925bded7cSSeth Forshee 		    alps_command_mode_send_nibble(psmouse, 0x4)) {
187025bded7cSSeth Forshee 			psmouse_err(psmouse,
187125bded7cSSeth Forshee 				    "Error sending magic E6 sequence\n");
1872cd401204SKevin Cernekee 			ret = -EIO;
187325bded7cSSeth Forshee 			goto error;
187425bded7cSSeth Forshee 		}
187525bded7cSSeth Forshee 
1876cd401204SKevin Cernekee 		/*
1877cd401204SKevin Cernekee 		 * This ensures the trackstick packets are in the format
1878cd401204SKevin Cernekee 		 * supported by this driver. If bit 1 isn't set the packet
1879cd401204SKevin Cernekee 		 * format is different.
1880cd401204SKevin Cernekee 		 */
1881d18e53fcSKevin Cernekee 		if (alps_enter_command_mode(psmouse) ||
1882cd401204SKevin Cernekee 		    alps_command_mode_write_reg(psmouse,
1883cd401204SKevin Cernekee 						reg_base + 0x08, 0x82) ||
1884cd401204SKevin Cernekee 		    alps_exit_command_mode(psmouse))
1885cd401204SKevin Cernekee 			ret = -EIO;
1886cd401204SKevin Cernekee 	}
1887cd401204SKevin Cernekee 
1888cd401204SKevin Cernekee error:
1889cd401204SKevin Cernekee 	if (alps_passthrough_mode_v3(psmouse, reg_base, false))
1890cd401204SKevin Cernekee 		ret = -EIO;
1891cd401204SKevin Cernekee 
1892cd401204SKevin Cernekee 	return ret;
1893cd401204SKevin Cernekee }
1894cd401204SKevin Cernekee 
1895cd401204SKevin Cernekee static int alps_hw_init_v3(struct psmouse *psmouse)
1896cd401204SKevin Cernekee {
1897cd401204SKevin Cernekee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
1898cd401204SKevin Cernekee 	int reg_val;
1899cd401204SKevin Cernekee 	unsigned char param[4];
1900cd401204SKevin Cernekee 
1901cd401204SKevin Cernekee 	reg_val = alps_probe_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE);
1902cd401204SKevin Cernekee 	if (reg_val == -EIO)
1903cd401204SKevin Cernekee 		goto error;
190439fbe585SDmitry Torokhov 
1905cd401204SKevin Cernekee 	if (reg_val == 0 &&
1906cd401204SKevin Cernekee 	    alps_setup_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE) == -EIO)
1907cd401204SKevin Cernekee 		goto error;
1908cd401204SKevin Cernekee 
1909d18e53fcSKevin Cernekee 	if (alps_enter_command_mode(psmouse) ||
1910cd401204SKevin Cernekee 	    alps_absolute_mode_v3(psmouse)) {
191125bded7cSSeth Forshee 		psmouse_err(psmouse, "Failed to enter absolute mode\n");
191225bded7cSSeth Forshee 		goto error;
191325bded7cSSeth Forshee 	}
191425bded7cSSeth Forshee 
191525bded7cSSeth Forshee 	reg_val = alps_command_mode_read_reg(psmouse, 0x0006);
191625bded7cSSeth Forshee 	if (reg_val == -1)
191725bded7cSSeth Forshee 		goto error;
191825bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
191925bded7cSSeth Forshee 		goto error;
192025bded7cSSeth Forshee 
192125bded7cSSeth Forshee 	reg_val = alps_command_mode_read_reg(psmouse, 0x0007);
192225bded7cSSeth Forshee 	if (reg_val == -1)
192325bded7cSSeth Forshee 		goto error;
192425bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
192525bded7cSSeth Forshee 		goto error;
192625bded7cSSeth Forshee 
192725bded7cSSeth Forshee 	if (alps_command_mode_read_reg(psmouse, 0x0144) == -1)
192825bded7cSSeth Forshee 		goto error;
192925bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, 0x04))
193025bded7cSSeth Forshee 		goto error;
193125bded7cSSeth Forshee 
193225bded7cSSeth Forshee 	if (alps_command_mode_read_reg(psmouse, 0x0159) == -1)
193325bded7cSSeth Forshee 		goto error;
193425bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, 0x03))
193525bded7cSSeth Forshee 		goto error;
193625bded7cSSeth Forshee 
193725bded7cSSeth Forshee 	if (alps_command_mode_read_reg(psmouse, 0x0163) == -1)
193825bded7cSSeth Forshee 		goto error;
193925bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0163, 0x03))
194025bded7cSSeth Forshee 		goto error;
194125bded7cSSeth Forshee 
194225bded7cSSeth Forshee 	if (alps_command_mode_read_reg(psmouse, 0x0162) == -1)
194325bded7cSSeth Forshee 		goto error;
194425bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0162, 0x04))
194525bded7cSSeth Forshee 		goto error;
194625bded7cSSeth Forshee 
194725bded7cSSeth Forshee 	alps_exit_command_mode(psmouse);
194825bded7cSSeth Forshee 
194925bded7cSSeth Forshee 	/* Set rate and enable data reporting */
195025bded7cSSeth Forshee 	param[0] = 0x64;
195125bded7cSSeth Forshee 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
195225bded7cSSeth Forshee 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
195325bded7cSSeth Forshee 		psmouse_err(psmouse, "Failed to enable data reporting\n");
195425bded7cSSeth Forshee 		return -1;
195525bded7cSSeth Forshee 	}
195625bded7cSSeth Forshee 
195725bded7cSSeth Forshee 	return 0;
195825bded7cSSeth Forshee 
195925bded7cSSeth Forshee error:
196025bded7cSSeth Forshee 	/*
196125bded7cSSeth Forshee 	 * Leaving the touchpad in command mode will essentially render
196225bded7cSSeth Forshee 	 * it unusable until the machine reboots, so exit it here just
196325bded7cSSeth Forshee 	 * to be safe
196425bded7cSSeth Forshee 	 */
196525bded7cSSeth Forshee 	alps_exit_command_mode(psmouse);
196625bded7cSSeth Forshee 	return -1;
196725bded7cSSeth Forshee }
196825bded7cSSeth Forshee 
1969f3f33c67SHans de Goede static int alps_get_v3_v7_resolution(struct psmouse *psmouse, int reg_pitch)
1970f3f33c67SHans de Goede {
1971f3f33c67SHans de Goede 	int reg, x_pitch, y_pitch, x_electrode, y_electrode, x_phys, y_phys;
1972f3f33c67SHans de Goede 	struct alps_data *priv = psmouse->private;
1973f3f33c67SHans de Goede 
1974f3f33c67SHans de Goede 	reg = alps_command_mode_read_reg(psmouse, reg_pitch);
1975f3f33c67SHans de Goede 	if (reg < 0)
1976f3f33c67SHans de Goede 		return reg;
1977f3f33c67SHans de Goede 
1978f3f33c67SHans de Goede 	x_pitch = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
1979f3f33c67SHans de Goede 	x_pitch = 50 + 2 * x_pitch; /* In 0.1 mm units */
1980f3f33c67SHans de Goede 
1981f3f33c67SHans de Goede 	y_pitch = (char)reg >> 4; /* sign extend upper 4 bits */
1982f3f33c67SHans de Goede 	y_pitch = 36 + 2 * y_pitch; /* In 0.1 mm units */
1983f3f33c67SHans de Goede 
1984f3f33c67SHans de Goede 	reg = alps_command_mode_read_reg(psmouse, reg_pitch + 1);
1985f3f33c67SHans de Goede 	if (reg < 0)
1986f3f33c67SHans de Goede 		return reg;
1987f3f33c67SHans de Goede 
1988f3f33c67SHans de Goede 	x_electrode = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
1989f3f33c67SHans de Goede 	x_electrode = 17 + x_electrode;
1990f3f33c67SHans de Goede 
1991f3f33c67SHans de Goede 	y_electrode = (char)reg >> 4; /* sign extend upper 4 bits */
1992f3f33c67SHans de Goede 	y_electrode = 13 + y_electrode;
1993f3f33c67SHans de Goede 
1994f3f33c67SHans de Goede 	x_phys = x_pitch * (x_electrode - 1); /* In 0.1 mm units */
1995f3f33c67SHans de Goede 	y_phys = y_pitch * (y_electrode - 1); /* In 0.1 mm units */
1996f3f33c67SHans de Goede 
1997f3f33c67SHans de Goede 	priv->x_res = priv->x_max * 10 / x_phys; /* units / mm */
1998f3f33c67SHans de Goede 	priv->y_res = priv->y_max * 10 / y_phys; /* units / mm */
1999f3f33c67SHans de Goede 
2000f3f33c67SHans de Goede 	psmouse_dbg(psmouse,
2001f3f33c67SHans de Goede 		    "pitch %dx%d num-electrodes %dx%d physical size %dx%d mm res %dx%d\n",
2002f3f33c67SHans de Goede 		    x_pitch, y_pitch, x_electrode, y_electrode,
2003f3f33c67SHans de Goede 		    x_phys / 10, y_phys / 10, priv->x_res, priv->y_res);
2004f3f33c67SHans de Goede 
2005f3f33c67SHans de Goede 	return 0;
2006f3f33c67SHans de Goede }
2007f3f33c67SHans de Goede 
20081302bac3SKevin Cernekee static int alps_hw_init_rushmore_v3(struct psmouse *psmouse)
20091302bac3SKevin Cernekee {
2010cd401204SKevin Cernekee 	struct alps_data *priv = psmouse->private;
20111302bac3SKevin Cernekee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
20121302bac3SKevin Cernekee 	int reg_val, ret = -1;
20131302bac3SKevin Cernekee 
2014cd401204SKevin Cernekee 	if (priv->flags & ALPS_DUALPOINT) {
2015cd401204SKevin Cernekee 		reg_val = alps_setup_trackstick_v3(psmouse,
2016cd401204SKevin Cernekee 						   ALPS_REG_BASE_RUSHMORE);
2017cd401204SKevin Cernekee 		if (reg_val == -EIO)
2018cd401204SKevin Cernekee 			goto error;
2019cd401204SKevin Cernekee 	}
2020cd401204SKevin Cernekee 
2021d18e53fcSKevin Cernekee 	if (alps_enter_command_mode(psmouse) ||
20221302bac3SKevin Cernekee 	    alps_command_mode_read_reg(psmouse, 0xc2d9) == -1 ||
20231302bac3SKevin Cernekee 	    alps_command_mode_write_reg(psmouse, 0xc2cb, 0x00))
20241302bac3SKevin Cernekee 		goto error;
20251302bac3SKevin Cernekee 
2026f3f33c67SHans de Goede 	if (alps_get_v3_v7_resolution(psmouse, 0xc2da))
2027f3f33c67SHans de Goede 		goto error;
2028f3f33c67SHans de Goede 
20291302bac3SKevin Cernekee 	reg_val = alps_command_mode_read_reg(psmouse, 0xc2c6);
20301302bac3SKevin Cernekee 	if (reg_val == -1)
20311302bac3SKevin Cernekee 		goto error;
20321302bac3SKevin Cernekee 	if (__alps_command_mode_write_reg(psmouse, reg_val & 0xfd))
20331302bac3SKevin Cernekee 		goto error;
20341302bac3SKevin Cernekee 
20351302bac3SKevin Cernekee 	if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
20361302bac3SKevin Cernekee 		goto error;
20371302bac3SKevin Cernekee 
20381302bac3SKevin Cernekee 	/* enter absolute mode */
20391302bac3SKevin Cernekee 	reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
20401302bac3SKevin Cernekee 	if (reg_val == -1)
20411302bac3SKevin Cernekee 		goto error;
20421302bac3SKevin Cernekee 	if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
20431302bac3SKevin Cernekee 		goto error;
20441302bac3SKevin Cernekee 
20451302bac3SKevin Cernekee 	alps_exit_command_mode(psmouse);
20461302bac3SKevin Cernekee 	return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
20471302bac3SKevin Cernekee 
20481302bac3SKevin Cernekee error:
20491302bac3SKevin Cernekee 	alps_exit_command_mode(psmouse);
20501302bac3SKevin Cernekee 	return ret;
20511302bac3SKevin Cernekee }
20521302bac3SKevin Cernekee 
205325bded7cSSeth Forshee /* Must be in command mode when calling this function */
205425bded7cSSeth Forshee static int alps_absolute_mode_v4(struct psmouse *psmouse)
205525bded7cSSeth Forshee {
205625bded7cSSeth Forshee 	int reg_val;
205725bded7cSSeth Forshee 
205825bded7cSSeth Forshee 	reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
205925bded7cSSeth Forshee 	if (reg_val == -1)
206025bded7cSSeth Forshee 		return -1;
206125bded7cSSeth Forshee 
206225bded7cSSeth Forshee 	reg_val |= 0x02;
206325bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, reg_val))
206425bded7cSSeth Forshee 		return -1;
206525bded7cSSeth Forshee 
206625bded7cSSeth Forshee 	return 0;
206725bded7cSSeth Forshee }
206825bded7cSSeth Forshee 
206925bded7cSSeth Forshee static int alps_hw_init_v4(struct psmouse *psmouse)
207025bded7cSSeth Forshee {
207125bded7cSSeth Forshee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
207225bded7cSSeth Forshee 	unsigned char param[4];
207325bded7cSSeth Forshee 
2074d18e53fcSKevin Cernekee 	if (alps_enter_command_mode(psmouse))
207525bded7cSSeth Forshee 		goto error;
207625bded7cSSeth Forshee 
207725bded7cSSeth Forshee 	if (alps_absolute_mode_v4(psmouse)) {
207825bded7cSSeth Forshee 		psmouse_err(psmouse, "Failed to enter absolute mode\n");
207925bded7cSSeth Forshee 		goto error;
208025bded7cSSeth Forshee 	}
208125bded7cSSeth Forshee 
208225bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0007, 0x8c))
208325bded7cSSeth Forshee 		goto error;
208425bded7cSSeth Forshee 
208525bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0149, 0x03))
208625bded7cSSeth Forshee 		goto error;
208725bded7cSSeth Forshee 
208825bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0160, 0x03))
208925bded7cSSeth Forshee 		goto error;
209025bded7cSSeth Forshee 
209125bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x017f, 0x15))
209225bded7cSSeth Forshee 		goto error;
209325bded7cSSeth Forshee 
209425bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0151, 0x01))
209525bded7cSSeth Forshee 		goto error;
209625bded7cSSeth Forshee 
209725bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0168, 0x03))
209825bded7cSSeth Forshee 		goto error;
209925bded7cSSeth Forshee 
210025bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x014a, 0x03))
210125bded7cSSeth Forshee 		goto error;
210225bded7cSSeth Forshee 
210325bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0161, 0x03))
210425bded7cSSeth Forshee 		goto error;
210525bded7cSSeth Forshee 
210625bded7cSSeth Forshee 	alps_exit_command_mode(psmouse);
210725bded7cSSeth Forshee 
210825bded7cSSeth Forshee 	/*
210925bded7cSSeth Forshee 	 * This sequence changes the output from a 9-byte to an
211025bded7cSSeth Forshee 	 * 8-byte format. All the same data seems to be present,
211125bded7cSSeth Forshee 	 * just in a more compact format.
211225bded7cSSeth Forshee 	 */
211325bded7cSSeth Forshee 	param[0] = 0xc8;
211425bded7cSSeth Forshee 	param[1] = 0x64;
211525bded7cSSeth Forshee 	param[2] = 0x50;
211625bded7cSSeth Forshee 	if (ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
211725bded7cSSeth Forshee 	    ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE) ||
211825bded7cSSeth Forshee 	    ps2_command(ps2dev, &param[2], PSMOUSE_CMD_SETRATE) ||
211925bded7cSSeth Forshee 	    ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
212025bded7cSSeth Forshee 		return -1;
212125bded7cSSeth Forshee 
212225bded7cSSeth Forshee 	/* Set rate and enable data reporting */
212325bded7cSSeth Forshee 	param[0] = 0x64;
212425bded7cSSeth Forshee 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
212525bded7cSSeth Forshee 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
212625bded7cSSeth Forshee 		psmouse_err(psmouse, "Failed to enable data reporting\n");
212725bded7cSSeth Forshee 		return -1;
212825bded7cSSeth Forshee 	}
212925bded7cSSeth Forshee 
213025bded7cSSeth Forshee 	return 0;
213125bded7cSSeth Forshee 
213225bded7cSSeth Forshee error:
213325bded7cSSeth Forshee 	/*
213425bded7cSSeth Forshee 	 * Leaving the touchpad in command mode will essentially render
213525bded7cSSeth Forshee 	 * it unusable until the machine reboots, so exit it here just
213625bded7cSSeth Forshee 	 * to be safe
213725bded7cSSeth Forshee 	 */
213825bded7cSSeth Forshee 	alps_exit_command_mode(psmouse);
213925bded7cSSeth Forshee 	return -1;
214025bded7cSSeth Forshee }
214125bded7cSSeth Forshee 
2142ee65d4b3SYunkang Tang static int alps_dolphin_get_device_area(struct psmouse *psmouse,
2143ee65d4b3SYunkang Tang 					struct alps_data *priv)
2144ee65d4b3SYunkang Tang {
2145ee65d4b3SYunkang Tang 	struct ps2dev *ps2dev = &psmouse->ps2dev;
2146ee65d4b3SYunkang Tang 	unsigned char param[4] = {0};
2147ee65d4b3SYunkang Tang 	int num_x_electrode, num_y_electrode;
2148ee65d4b3SYunkang Tang 
2149ee65d4b3SYunkang Tang 	if (alps_enter_command_mode(psmouse))
2150ee65d4b3SYunkang Tang 		return -1;
2151ee65d4b3SYunkang Tang 
2152ee65d4b3SYunkang Tang 	param[0] = 0x0a;
2153ee65d4b3SYunkang Tang 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
2154ee65d4b3SYunkang Tang 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2155ee65d4b3SYunkang Tang 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2156ee65d4b3SYunkang Tang 	    ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2157ee65d4b3SYunkang Tang 	    ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE))
2158ee65d4b3SYunkang Tang 		return -1;
2159ee65d4b3SYunkang Tang 
2160ee65d4b3SYunkang Tang 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
2161ee65d4b3SYunkang Tang 		return -1;
2162ee65d4b3SYunkang Tang 
2163ee65d4b3SYunkang Tang 	/*
2164ee65d4b3SYunkang Tang 	 * Dolphin's sensor line number is not fixed. It can be calculated
2165ee65d4b3SYunkang Tang 	 * by adding the device's register value with DOLPHIN_PROFILE_X/YOFFSET.
2166ee65d4b3SYunkang Tang 	 * Further more, we can get device's x_max and y_max by multiplying
2167ee65d4b3SYunkang Tang 	 * sensor line number with DOLPHIN_COUNT_PER_ELECTRODE.
2168ee65d4b3SYunkang Tang 	 *
2169ee65d4b3SYunkang Tang 	 * e.g. When we get register's sensor_x = 11 & sensor_y = 8,
2170ee65d4b3SYunkang Tang 	 *	real sensor line number X = 11 + 8 = 19, and
2171ee65d4b3SYunkang Tang 	 *	real sensor line number Y = 8 + 1 = 9.
2172ee65d4b3SYunkang Tang 	 *	So, x_max = (19 - 1) * 64 = 1152, and
2173ee65d4b3SYunkang Tang 	 *	    y_max = (9 - 1) * 64 = 512.
2174ee65d4b3SYunkang Tang 	 */
2175ee65d4b3SYunkang Tang 	num_x_electrode = DOLPHIN_PROFILE_XOFFSET + (param[2] & 0x0F);
2176ee65d4b3SYunkang Tang 	num_y_electrode = DOLPHIN_PROFILE_YOFFSET + ((param[2] >> 4) & 0x0F);
2177ee65d4b3SYunkang Tang 	priv->x_bits = num_x_electrode;
2178ee65d4b3SYunkang Tang 	priv->y_bits = num_y_electrode;
2179ee65d4b3SYunkang Tang 	priv->x_max = (num_x_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
2180ee65d4b3SYunkang Tang 	priv->y_max = (num_y_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
2181ee65d4b3SYunkang Tang 
2182ee65d4b3SYunkang Tang 	if (alps_exit_command_mode(psmouse))
2183ee65d4b3SYunkang Tang 		return -1;
2184ee65d4b3SYunkang Tang 
2185ee65d4b3SYunkang Tang 	return 0;
2186ee65d4b3SYunkang Tang }
2187ee65d4b3SYunkang Tang 
218875af9e56SDave Turvene static int alps_hw_init_dolphin_v1(struct psmouse *psmouse)
218975af9e56SDave Turvene {
219075af9e56SDave Turvene 	struct ps2dev *ps2dev = &psmouse->ps2dev;
219175af9e56SDave Turvene 	unsigned char param[2];
219275af9e56SDave Turvene 
219375af9e56SDave Turvene 	/* This is dolphin "v1" as empirically defined by florin9doi */
219475af9e56SDave Turvene 	param[0] = 0x64;
219575af9e56SDave Turvene 	param[1] = 0x28;
219675af9e56SDave Turvene 
219775af9e56SDave Turvene 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
219875af9e56SDave Turvene 	    ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
219975af9e56SDave Turvene 	    ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
220075af9e56SDave Turvene 		return -1;
220175af9e56SDave Turvene 
220275af9e56SDave Turvene 	return 0;
220375af9e56SDave Turvene }
220475af9e56SDave Turvene 
22053808843cSYunkang Tang static int alps_hw_init_v7(struct psmouse *psmouse)
22063808843cSYunkang Tang {
22073808843cSYunkang Tang 	struct ps2dev *ps2dev = &psmouse->ps2dev;
22083808843cSYunkang Tang 	int reg_val, ret = -1;
22093808843cSYunkang Tang 
22103808843cSYunkang Tang 	if (alps_enter_command_mode(psmouse) ||
22113808843cSYunkang Tang 	    alps_command_mode_read_reg(psmouse, 0xc2d9) == -1)
22123808843cSYunkang Tang 		goto error;
22133808843cSYunkang Tang 
2214f3f33c67SHans de Goede 	if (alps_get_v3_v7_resolution(psmouse, 0xc397))
2215f3f33c67SHans de Goede 		goto error;
2216f3f33c67SHans de Goede 
22173808843cSYunkang Tang 	if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
22183808843cSYunkang Tang 		goto error;
22193808843cSYunkang Tang 
22203808843cSYunkang Tang 	reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
22213808843cSYunkang Tang 	if (reg_val == -1)
22223808843cSYunkang Tang 		goto error;
22233808843cSYunkang Tang 	if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
22243808843cSYunkang Tang 		goto error;
22253808843cSYunkang Tang 
22263808843cSYunkang Tang 	alps_exit_command_mode(psmouse);
22273808843cSYunkang Tang 	return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
22283808843cSYunkang Tang 
22293808843cSYunkang Tang error:
22303808843cSYunkang Tang 	alps_exit_command_mode(psmouse);
22313808843cSYunkang Tang 	return ret;
22323808843cSYunkang Tang }
22333808843cSYunkang Tang 
22343296f71cSDmitry Torokhov static int alps_set_protocol(struct psmouse *psmouse,
22353296f71cSDmitry Torokhov 			     struct alps_data *priv,
22363296f71cSDmitry Torokhov 			     const struct alps_protocol_info *protocol)
223725bded7cSSeth Forshee {
22383296f71cSDmitry Torokhov 	psmouse->private = priv;
22393296f71cSDmitry Torokhov 
22403296f71cSDmitry Torokhov 	setup_timer(&priv->timer, alps_flush_packet, (unsigned long)psmouse);
22413296f71cSDmitry Torokhov 
22423296f71cSDmitry Torokhov 	priv->proto_version = protocol->version;
22433296f71cSDmitry Torokhov 	priv->byte0 = protocol->byte0;
22443296f71cSDmitry Torokhov 	priv->mask0 = protocol->mask0;
22453296f71cSDmitry Torokhov 	priv->flags = protocol->flags;
2246f673ceb1SKevin Cernekee 
22477a9f73e7SKevin Cernekee 	priv->x_max = 2000;
22487a9f73e7SKevin Cernekee 	priv->y_max = 1400;
22497a9f73e7SKevin Cernekee 	priv->x_bits = 15;
22507a9f73e7SKevin Cernekee 	priv->y_bits = 11;
22517a9f73e7SKevin Cernekee 
225299df65e7SKevin Cernekee 	switch (priv->proto_version) {
225325bded7cSSeth Forshee 	case ALPS_PROTO_V1:
225425bded7cSSeth Forshee 	case ALPS_PROTO_V2:
225524af5cb9SKevin Cernekee 		priv->hw_init = alps_hw_init_v1_v2;
225624af5cb9SKevin Cernekee 		priv->process_packet = alps_process_packet_v1_v2;
225724af5cb9SKevin Cernekee 		priv->set_abs_params = alps_set_abs_params_st;
225895f75e91SYunkang Tang 		priv->x_max = 1023;
225995f75e91SYunkang Tang 		priv->y_max = 767;
226025bded7cSSeth Forshee 		break;
2261fb2dd7a6SDmitry Torokhov 
226225bded7cSSeth Forshee 	case ALPS_PROTO_V3:
226324af5cb9SKevin Cernekee 		priv->hw_init = alps_hw_init_v3;
226424af5cb9SKevin Cernekee 		priv->process_packet = alps_process_packet_v3;
226524af5cb9SKevin Cernekee 		priv->set_abs_params = alps_set_abs_params_mt;
2266f85e5001SKevin Cernekee 		priv->decode_fields = alps_decode_pinnacle;
226750e8b216SKevin Cernekee 		priv->nibble_commands = alps_v3_nibble_commands;
226850e8b216SKevin Cernekee 		priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
226925bded7cSSeth Forshee 		break;
2270fb2dd7a6SDmitry Torokhov 
2271fb2dd7a6SDmitry Torokhov 	case ALPS_PROTO_V3_RUSHMORE:
2272fb2dd7a6SDmitry Torokhov 		priv->hw_init = alps_hw_init_rushmore_v3;
2273fb2dd7a6SDmitry Torokhov 		priv->process_packet = alps_process_packet_v3;
2274fb2dd7a6SDmitry Torokhov 		priv->set_abs_params = alps_set_abs_params_mt;
2275fb2dd7a6SDmitry Torokhov 		priv->decode_fields = alps_decode_rushmore;
2276fb2dd7a6SDmitry Torokhov 		priv->nibble_commands = alps_v3_nibble_commands;
2277fb2dd7a6SDmitry Torokhov 		priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2278fb2dd7a6SDmitry Torokhov 		priv->x_bits = 16;
2279fb2dd7a6SDmitry Torokhov 		priv->y_bits = 12;
22803296f71cSDmitry Torokhov 
22813296f71cSDmitry Torokhov 		if (alps_probe_trackstick_v3(psmouse,
22823296f71cSDmitry Torokhov 					     ALPS_REG_BASE_RUSHMORE) < 0)
22833296f71cSDmitry Torokhov 			priv->flags &= ~ALPS_DUALPOINT;
22843296f71cSDmitry Torokhov 
2285fb2dd7a6SDmitry Torokhov 		break;
2286fb2dd7a6SDmitry Torokhov 
228725bded7cSSeth Forshee 	case ALPS_PROTO_V4:
228824af5cb9SKevin Cernekee 		priv->hw_init = alps_hw_init_v4;
228924af5cb9SKevin Cernekee 		priv->process_packet = alps_process_packet_v4;
229024af5cb9SKevin Cernekee 		priv->set_abs_params = alps_set_abs_params_mt;
229150e8b216SKevin Cernekee 		priv->nibble_commands = alps_v4_nibble_commands;
229250e8b216SKevin Cernekee 		priv->addr_command = PSMOUSE_CMD_DISABLE;
229325bded7cSSeth Forshee 		break;
22943296f71cSDmitry Torokhov 
229575af9e56SDave Turvene 	case ALPS_PROTO_V5:
229675af9e56SDave Turvene 		priv->hw_init = alps_hw_init_dolphin_v1;
2297ee65d4b3SYunkang Tang 		priv->process_packet = alps_process_touchpad_packet_v3_v5;
229875af9e56SDave Turvene 		priv->decode_fields = alps_decode_dolphin;
229975af9e56SDave Turvene 		priv->set_abs_params = alps_set_abs_params_mt;
230075af9e56SDave Turvene 		priv->nibble_commands = alps_v3_nibble_commands;
230175af9e56SDave Turvene 		priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
230275af9e56SDave Turvene 		priv->x_bits = 23;
230375af9e56SDave Turvene 		priv->y_bits = 12;
2304c164c147SDmitry Torokhov 
2305c164c147SDmitry Torokhov 		if (alps_dolphin_get_device_area(psmouse, priv))
2306c164c147SDmitry Torokhov 			return -EIO;
2307c164c147SDmitry Torokhov 
230875af9e56SDave Turvene 		break;
23093296f71cSDmitry Torokhov 
231095f75e91SYunkang Tang 	case ALPS_PROTO_V6:
231195f75e91SYunkang Tang 		priv->hw_init = alps_hw_init_v6;
231295f75e91SYunkang Tang 		priv->process_packet = alps_process_packet_v6;
231395f75e91SYunkang Tang 		priv->set_abs_params = alps_set_abs_params_st;
231495f75e91SYunkang Tang 		priv->nibble_commands = alps_v6_nibble_commands;
231595f75e91SYunkang Tang 		priv->x_max = 2047;
231695f75e91SYunkang Tang 		priv->y_max = 1535;
231795f75e91SYunkang Tang 		break;
23183296f71cSDmitry Torokhov 
23193808843cSYunkang Tang 	case ALPS_PROTO_V7:
23203808843cSYunkang Tang 		priv->hw_init = alps_hw_init_v7;
23213808843cSYunkang Tang 		priv->process_packet = alps_process_packet_v7;
23223808843cSYunkang Tang 		priv->decode_fields = alps_decode_packet_v7;
23233808843cSYunkang Tang 		priv->set_abs_params = alps_set_abs_params_mt;
23243808843cSYunkang Tang 		priv->nibble_commands = alps_v3_nibble_commands;
23253808843cSYunkang Tang 		priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2326c164c147SDmitry Torokhov 		priv->x_max = 0xfff;
2327c164c147SDmitry Torokhov 		priv->y_max = 0x7ff;
23283808843cSYunkang Tang 
23293808843cSYunkang Tang 		if (priv->fw_ver[1] != 0xba)
23303808843cSYunkang Tang 			priv->flags |= ALPS_BUTTONPAD;
23313296f71cSDmitry Torokhov 
23323808843cSYunkang Tang 		break;
233325bded7cSSeth Forshee 	}
23343296f71cSDmitry Torokhov 
23353296f71cSDmitry Torokhov 	return 0;
233625bded7cSSeth Forshee }
233725bded7cSSeth Forshee 
23383296f71cSDmitry Torokhov static const struct alps_protocol_info *alps_match_table(unsigned char *e7,
23393296f71cSDmitry Torokhov 							 unsigned char *ec)
23402e992cc0SKevin Cernekee {
2341b5d6b851SKevin Cernekee 	const struct alps_model_info *model;
23422e992cc0SKevin Cernekee 	int i;
23432e992cc0SKevin Cernekee 
2344b5d6b851SKevin Cernekee 	for (i = 0; i < ARRAY_SIZE(alps_model_data); i++) {
2345b5d6b851SKevin Cernekee 		model = &alps_model_data[i];
2346b5d6b851SKevin Cernekee 
2347b5d6b851SKevin Cernekee 		if (!memcmp(e7, model->signature, sizeof(model->signature)) &&
2348b5d6b851SKevin Cernekee 		    (!model->command_mode_resp ||
2349b5d6b851SKevin Cernekee 		     model->command_mode_resp == ec[2])) {
2350b5d6b851SKevin Cernekee 
23513296f71cSDmitry Torokhov 			return &model->protocol_info;
2352b5d6b851SKevin Cernekee 		}
2353b5d6b851SKevin Cernekee 	}
2354b5d6b851SKevin Cernekee 
23553296f71cSDmitry Torokhov 	return NULL;
2356b5d6b851SKevin Cernekee }
2357b5d6b851SKevin Cernekee 
2358b5d6b851SKevin Cernekee static int alps_identify(struct psmouse *psmouse, struct alps_data *priv)
2359b5d6b851SKevin Cernekee {
23603296f71cSDmitry Torokhov 	const struct alps_protocol_info *protocol;
2361b5d6b851SKevin Cernekee 	unsigned char e6[4], e7[4], ec[4];
2362a09221e8SDmitry Torokhov 	int error;
2363b5d6b851SKevin Cernekee 
23642e992cc0SKevin Cernekee 	/*
23652e992cc0SKevin Cernekee 	 * First try "E6 report".
23662e992cc0SKevin Cernekee 	 * ALPS should return 0,0,10 or 0,0,100 if no buttons are pressed.
23672e992cc0SKevin Cernekee 	 * The bits 0-2 of the first byte will be 1s if some buttons are
23682e992cc0SKevin Cernekee 	 * pressed.
23692e992cc0SKevin Cernekee 	 */
2370b5d6b851SKevin Cernekee 	if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2371b5d6b851SKevin Cernekee 			 PSMOUSE_CMD_SETSCALE11, e6))
2372b5d6b851SKevin Cernekee 		return -EIO;
23732e992cc0SKevin Cernekee 
2374b5d6b851SKevin Cernekee 	if ((e6[0] & 0xf8) != 0 || e6[1] != 0 || (e6[2] != 10 && e6[2] != 100))
2375b5d6b851SKevin Cernekee 		return -EINVAL;
23762e992cc0SKevin Cernekee 
23772e992cc0SKevin Cernekee 	/*
2378b5d6b851SKevin Cernekee 	 * Now get the "E7" and "EC" reports.  These will uniquely identify
2379b5d6b851SKevin Cernekee 	 * most ALPS touchpads.
23802e992cc0SKevin Cernekee 	 */
2381b5d6b851SKevin Cernekee 	if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2382b5d6b851SKevin Cernekee 			 PSMOUSE_CMD_SETSCALE21, e7) ||
2383b5d6b851SKevin Cernekee 	    alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2384b5d6b851SKevin Cernekee 			 PSMOUSE_CMD_RESET_WRAP, ec) ||
2385b5d6b851SKevin Cernekee 	    alps_exit_command_mode(psmouse))
2386b5d6b851SKevin Cernekee 		return -EIO;
23872e992cc0SKevin Cernekee 
23883296f71cSDmitry Torokhov 	protocol = alps_match_table(e7, ec);
23893296f71cSDmitry Torokhov 	if (!protocol) {
23903296f71cSDmitry Torokhov 		if (e7[0] == 0x73 && e7[1] == 0x03 && e7[2] == 0x50 &&
23913296f71cSDmitry Torokhov 			   ec[0] == 0x73 && (ec[1] == 0x01 || ec[1] == 0x02)) {
23923296f71cSDmitry Torokhov 			protocol = &alps_v5_protocol_data;
23933296f71cSDmitry Torokhov 		} else if (ec[0] == 0x88 &&
23943296f71cSDmitry Torokhov 			   ((ec[1] & 0xf0) == 0xb0 || (ec[1] & 0xf0) == 0xc0)) {
23953296f71cSDmitry Torokhov 			protocol = &alps_v7_protocol_data;
23963296f71cSDmitry Torokhov 		} else if (ec[0] == 0x88 && ec[1] == 0x08) {
23973296f71cSDmitry Torokhov 			protocol = &alps_v3_rushmore_data;
23983296f71cSDmitry Torokhov 		} else if (ec[0] == 0x88 && ec[1] == 0x07 &&
23993296f71cSDmitry Torokhov 			   ec[2] >= 0x90 && ec[2] <= 0x9d) {
24003296f71cSDmitry Torokhov 			protocol = &alps_v3_protocol_data;
24013296f71cSDmitry Torokhov 		} else {
24023296f71cSDmitry Torokhov 			psmouse_dbg(psmouse,
24033296f71cSDmitry Torokhov 				    "Likely not an ALPS touchpad: E7=%3ph, EC=%3ph\n", e7, ec);
24043296f71cSDmitry Torokhov 			return -EINVAL;
24053296f71cSDmitry Torokhov 		}
24063296f71cSDmitry Torokhov 	}
24073296f71cSDmitry Torokhov 
2408a09221e8SDmitry Torokhov 	if (priv) {
2409c0cd17f6SHans de Goede 		/* Save the Firmware version */
2410c0cd17f6SHans de Goede 		memcpy(priv->fw_ver, ec, 3);
2411a09221e8SDmitry Torokhov 		error = alps_set_protocol(psmouse, priv, protocol);
2412a09221e8SDmitry Torokhov 		if (error)
2413a09221e8SDmitry Torokhov 			return error;
2414a09221e8SDmitry Torokhov 	}
2415c0cd17f6SHans de Goede 
2416a09221e8SDmitry Torokhov 	return 0;
24172e992cc0SKevin Cernekee }
24182e992cc0SKevin Cernekee 
24191e0c5b12SDmitry Torokhov static int alps_reconnect(struct psmouse *psmouse)
24201e0c5b12SDmitry Torokhov {
2421b5d6b851SKevin Cernekee 	struct alps_data *priv = psmouse->private;
242271bb21b6SMaxim Levitsky 
24231e0c5b12SDmitry Torokhov 	psmouse_reset(psmouse);
24241e0c5b12SDmitry Torokhov 
2425b5d6b851SKevin Cernekee 	if (alps_identify(psmouse, priv) < 0)
24261e0c5b12SDmitry Torokhov 		return -1;
24271e0c5b12SDmitry Torokhov 
242824af5cb9SKevin Cernekee 	return priv->hw_init(psmouse);
24291da177e4SLinus Torvalds }
24301da177e4SLinus Torvalds 
24311da177e4SLinus Torvalds static void alps_disconnect(struct psmouse *psmouse)
24321da177e4SLinus Torvalds {
24331da177e4SLinus Torvalds 	struct alps_data *priv = psmouse->private;
24342e5b636bSDmitry Torokhov 
24351da177e4SLinus Torvalds 	psmouse_reset(psmouse);
24361d9f2626SSebastian Kapfer 	del_timer_sync(&priv->timer);
243704aae283SPali Rohár 	if (priv->dev2)
24382e5b636bSDmitry Torokhov 		input_unregister_device(priv->dev2);
243904aae283SPali Rohár 	if (!IS_ERR_OR_NULL(priv->dev3))
244004aae283SPali Rohár 		input_unregister_device(priv->dev3);
24411da177e4SLinus Torvalds 	kfree(priv);
24421da177e4SLinus Torvalds }
24431da177e4SLinus Torvalds 
244424af5cb9SKevin Cernekee static void alps_set_abs_params_st(struct alps_data *priv,
244524af5cb9SKevin Cernekee 				   struct input_dev *dev1)
244624af5cb9SKevin Cernekee {
244795f75e91SYunkang Tang 	input_set_abs_params(dev1, ABS_X, 0, priv->x_max, 0, 0);
244895f75e91SYunkang Tang 	input_set_abs_params(dev1, ABS_Y, 0, priv->y_max, 0, 0);
244924af5cb9SKevin Cernekee }
245024af5cb9SKevin Cernekee 
245124af5cb9SKevin Cernekee static void alps_set_abs_params_mt(struct alps_data *priv,
245224af5cb9SKevin Cernekee 				   struct input_dev *dev1)
245324af5cb9SKevin Cernekee {
24547a9f73e7SKevin Cernekee 	input_set_abs_params(dev1, ABS_MT_POSITION_X, 0, priv->x_max, 0, 0);
24557a9f73e7SKevin Cernekee 	input_set_abs_params(dev1, ABS_MT_POSITION_Y, 0, priv->y_max, 0, 0);
245624af5cb9SKevin Cernekee 
2457f3f33c67SHans de Goede 	input_abs_set_res(dev1, ABS_MT_POSITION_X, priv->x_res);
2458f3f33c67SHans de Goede 	input_abs_set_res(dev1, ABS_MT_POSITION_Y, priv->y_res);
2459f3f33c67SHans de Goede 
2460cdf333efSHans de Goede 	input_mt_init_slots(dev1, MAX_TOUCHES, INPUT_MT_POINTER |
2461cdf333efSHans de Goede 		INPUT_MT_DROP_UNUSED | INPUT_MT_TRACK | INPUT_MT_SEMI_MT);
2462cdf333efSHans de Goede 
246324af5cb9SKevin Cernekee 	set_bit(BTN_TOOL_TRIPLETAP, dev1->keybit);
246424af5cb9SKevin Cernekee 	set_bit(BTN_TOOL_QUADTAP, dev1->keybit);
24653808843cSYunkang Tang 
24663808843cSYunkang Tang 	/* V7 is real multi-touch */
24673808843cSYunkang Tang 	if (priv->proto_version == ALPS_PROTO_V7)
24683808843cSYunkang Tang 		clear_bit(INPUT_PROP_SEMI_MT, dev1->propbit);
246924af5cb9SKevin Cernekee }
247024af5cb9SKevin Cernekee 
24711da177e4SLinus Torvalds int alps_init(struct psmouse *psmouse)
24721da177e4SLinus Torvalds {
2473a09221e8SDmitry Torokhov 	struct alps_data *priv = psmouse->private;
247404aae283SPali Rohár 	struct input_dev *dev1 = psmouse->dev;
2475a09221e8SDmitry Torokhov 	int error;
24761da177e4SLinus Torvalds 
2477a09221e8SDmitry Torokhov 	error = priv->hw_init(psmouse);
2478a09221e8SDmitry Torokhov 	if (error)
24791da177e4SLinus Torvalds 		goto init_fail;
24801da177e4SLinus Torvalds 
24817105d2eaSDmitry Torokhov 	/*
24827105d2eaSDmitry Torokhov 	 * Undo part of setup done for us by psmouse core since touchpad
24837105d2eaSDmitry Torokhov 	 * is not a relative device.
24847105d2eaSDmitry Torokhov 	 */
24857105d2eaSDmitry Torokhov 	__clear_bit(EV_REL, dev1->evbit);
24867105d2eaSDmitry Torokhov 	__clear_bit(REL_X, dev1->relbit);
24877105d2eaSDmitry Torokhov 	__clear_bit(REL_Y, dev1->relbit);
24887105d2eaSDmitry Torokhov 
24897105d2eaSDmitry Torokhov 	/*
24907105d2eaSDmitry Torokhov 	 * Now set up our capabilities.
24917105d2eaSDmitry Torokhov 	 */
24927b19ada2SJiri Slaby 	dev1->evbit[BIT_WORD(EV_KEY)] |= BIT_MASK(EV_KEY);
24937b19ada2SJiri Slaby 	dev1->keybit[BIT_WORD(BTN_TOUCH)] |= BIT_MASK(BTN_TOUCH);
24947b19ada2SJiri Slaby 	dev1->keybit[BIT_WORD(BTN_TOOL_FINGER)] |= BIT_MASK(BTN_TOOL_FINGER);
249571bb21b6SMaxim Levitsky 	dev1->keybit[BIT_WORD(BTN_LEFT)] |=
249671bb21b6SMaxim Levitsky 		BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
24971da177e4SLinus Torvalds 
24987b19ada2SJiri Slaby 	dev1->evbit[BIT_WORD(EV_ABS)] |= BIT_MASK(EV_ABS);
249925bded7cSSeth Forshee 
250024af5cb9SKevin Cernekee 	priv->set_abs_params(priv, dev1);
25013808843cSYunkang Tang 	/* No pressure on V7 */
25023808843cSYunkang Tang 	if (priv->proto_version != ALPS_PROTO_V7)
25032e5b636bSDmitry Torokhov 		input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
25041da177e4SLinus Torvalds 
250599df65e7SKevin Cernekee 	if (priv->flags & ALPS_WHEEL) {
25067b19ada2SJiri Slaby 		dev1->evbit[BIT_WORD(EV_REL)] |= BIT_MASK(EV_REL);
25077b19ada2SJiri Slaby 		dev1->relbit[BIT_WORD(REL_WHEEL)] |= BIT_MASK(REL_WHEEL);
25081da177e4SLinus Torvalds 	}
25091da177e4SLinus Torvalds 
251099df65e7SKevin Cernekee 	if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
25117b19ada2SJiri Slaby 		dev1->keybit[BIT_WORD(BTN_FORWARD)] |= BIT_MASK(BTN_FORWARD);
25127b19ada2SJiri Slaby 		dev1->keybit[BIT_WORD(BTN_BACK)] |= BIT_MASK(BTN_BACK);
25131da177e4SLinus Torvalds 	}
25141da177e4SLinus Torvalds 
251599df65e7SKevin Cernekee 	if (priv->flags & ALPS_FOUR_BUTTONS) {
251671bb21b6SMaxim Levitsky 		dev1->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_0);
251771bb21b6SMaxim Levitsky 		dev1->keybit[BIT_WORD(BTN_1)] |= BIT_MASK(BTN_1);
251871bb21b6SMaxim Levitsky 		dev1->keybit[BIT_WORD(BTN_2)] |= BIT_MASK(BTN_2);
251971bb21b6SMaxim Levitsky 		dev1->keybit[BIT_WORD(BTN_3)] |= BIT_MASK(BTN_3);
25203808843cSYunkang Tang 	} else if (priv->flags & ALPS_BUTTONPAD) {
25213808843cSYunkang Tang 		set_bit(INPUT_PROP_BUTTONPAD, dev1->propbit);
25223808843cSYunkang Tang 		clear_bit(BTN_RIGHT, dev1->keybit);
252371bb21b6SMaxim Levitsky 	} else {
252471bb21b6SMaxim Levitsky 		dev1->keybit[BIT_WORD(BTN_MIDDLE)] |= BIT_MASK(BTN_MIDDLE);
252571bb21b6SMaxim Levitsky 	}
252671bb21b6SMaxim Levitsky 
2527dfba8600SPali Rohár 	if (priv->flags & ALPS_DUALPOINT) {
252804aae283SPali Rohár 		struct input_dev *dev2;
252904aae283SPali Rohár 
253004aae283SPali Rohár 		dev2 = input_allocate_device();
253104aae283SPali Rohár 		if (!dev2) {
253204aae283SPali Rohár 			psmouse_err(psmouse,
253304aae283SPali Rohár 				    "failed to allocate trackstick device\n");
253404aae283SPali Rohár 			error = -ENOMEM;
253504aae283SPali Rohár 			goto init_fail;
253604aae283SPali Rohár 		}
253704aae283SPali Rohár 
253804aae283SPali Rohár 		snprintf(priv->phys2, sizeof(priv->phys2), "%s/input1",
253904aae283SPali Rohár 			 psmouse->ps2dev.serio->phys);
254004aae283SPali Rohár 		dev2->phys = priv->phys2;
254104aae283SPali Rohár 
2542dfba8600SPali Rohár 		/*
2543dfba8600SPali Rohár 		 * format of input device name is: "protocol vendor name"
2544dfba8600SPali Rohár 		 * see function psmouse_switch_protocol() in psmouse-base.c
2545dfba8600SPali Rohár 		 */
2546dfba8600SPali Rohár 		dev2->name = "AlpsPS/2 ALPS DualPoint Stick";
2547dfba8600SPali Rohár 
25482e5b636bSDmitry Torokhov 		dev2->id.bustype = BUS_I8042;
25492e5b636bSDmitry Torokhov 		dev2->id.vendor  = 0x0002;
255004aae283SPali Rohár 		dev2->id.product = PSMOUSE_ALPS;
255104aae283SPali Rohár 		dev2->id.version = priv->proto_version;
25521db3a345SDmitry Torokhov 		dev2->dev.parent = &psmouse->ps2dev.serio->dev;
25531da177e4SLinus Torvalds 
255404aae283SPali Rohár 		input_set_capability(dev2, EV_REL, REL_X);
255504aae283SPali Rohár 		input_set_capability(dev2, EV_REL, REL_Y);
255604aae283SPali Rohár 		input_set_capability(dev2, EV_KEY, BTN_LEFT);
255704aae283SPali Rohár 		input_set_capability(dev2, EV_KEY, BTN_RIGHT);
255804aae283SPali Rohár 		input_set_capability(dev2, EV_KEY, BTN_MIDDLE);
25591da177e4SLinus Torvalds 
256001d4cd5cSHans de Goede 		__set_bit(INPUT_PROP_POINTER, dev2->propbit);
25617611392fSHans de Goede 		__set_bit(INPUT_PROP_POINTING_STICK, dev2->propbit);
25627611392fSHans de Goede 
256304aae283SPali Rohár 		error = input_register_device(dev2);
256404aae283SPali Rohár 		if (error) {
256504aae283SPali Rohár 			psmouse_err(psmouse,
256604aae283SPali Rohár 				    "failed to register trackstick device: %d\n",
256704aae283SPali Rohár 				    error);
256804aae283SPali Rohár 			input_free_device(dev2);
2569f42649e8SDmitry Torokhov 			goto init_fail;
257004aae283SPali Rohár 		}
257104aae283SPali Rohár 
257204aae283SPali Rohár 		priv->dev2 = dev2;
257304aae283SPali Rohár 	}
257404aae283SPali Rohár 
257504aae283SPali Rohár 	priv->psmouse = psmouse;
257604aae283SPali Rohár 
257704aae283SPali Rohár 	INIT_DELAYED_WORK(&priv->dev3_register_work,
257804aae283SPali Rohár 			  alps_register_bare_ps2_mouse);
25791da177e4SLinus Torvalds 
25801da177e4SLinus Torvalds 	psmouse->protocol_handler = alps_process_byte;
2581f0d5c6f4SDmitry Torokhov 	psmouse->poll = alps_poll;
25821da177e4SLinus Torvalds 	psmouse->disconnect = alps_disconnect;
25831da177e4SLinus Torvalds 	psmouse->reconnect = alps_reconnect;
258499df65e7SKevin Cernekee 	psmouse->pktsize = priv->proto_version == ALPS_PROTO_V4 ? 8 : 6;
25851da177e4SLinus Torvalds 
2586f0d5c6f4SDmitry Torokhov 	/* We are having trouble resyncing ALPS touchpads so disable it for now */
2587f0d5c6f4SDmitry Torokhov 	psmouse->resync_time = 0;
2588f0d5c6f4SDmitry Torokhov 
25899d720b34SPali Rohár 	/* Allow 2 invalid packets without resetting device */
25909d720b34SPali Rohár 	psmouse->resetafter = psmouse->pktsize * 2;
25919d720b34SPali Rohár 
25921da177e4SLinus Torvalds 	return 0;
25931da177e4SLinus Torvalds 
25941da177e4SLinus Torvalds init_fail:
2595f42649e8SDmitry Torokhov 	psmouse_reset(psmouse);
2596a09221e8SDmitry Torokhov 	/*
2597a09221e8SDmitry Torokhov 	 * Even though we did not allocate psmouse->private we do free
2598a09221e8SDmitry Torokhov 	 * it here.
2599a09221e8SDmitry Torokhov 	 */
2600a09221e8SDmitry Torokhov 	kfree(psmouse->private);
26011e0c5b12SDmitry Torokhov 	psmouse->private = NULL;
2602a09221e8SDmitry Torokhov 	return error;
26031da177e4SLinus Torvalds }
26041da177e4SLinus Torvalds 
2605b7802c5cSDmitry Torokhov int alps_detect(struct psmouse *psmouse, bool set_properties)
26061da177e4SLinus Torvalds {
2607a09221e8SDmitry Torokhov 	struct alps_data *priv;
2608a09221e8SDmitry Torokhov 	int error;
26091da177e4SLinus Torvalds 
2610a09221e8SDmitry Torokhov 	error = alps_identify(psmouse, NULL);
2611a09221e8SDmitry Torokhov 	if (error)
2612a09221e8SDmitry Torokhov 		return error;
2613a09221e8SDmitry Torokhov 
2614a09221e8SDmitry Torokhov 	/*
2615a09221e8SDmitry Torokhov 	 * Reset the device to make sure it is fully operational:
2616a09221e8SDmitry Torokhov 	 * on some laptops, like certain Dell Latitudes, we may
2617a09221e8SDmitry Torokhov 	 * fail to properly detect presence of trackstick if device
2618a09221e8SDmitry Torokhov 	 * has not been reset.
2619a09221e8SDmitry Torokhov 	 */
2620a09221e8SDmitry Torokhov 	psmouse_reset(psmouse);
2621a09221e8SDmitry Torokhov 
2622a09221e8SDmitry Torokhov 	priv = kzalloc(sizeof(struct alps_data), GFP_KERNEL);
2623a09221e8SDmitry Torokhov 	if (!priv)
2624a09221e8SDmitry Torokhov 		return -ENOMEM;
2625a09221e8SDmitry Torokhov 
2626a09221e8SDmitry Torokhov 	error = alps_identify(psmouse, priv);
262793050db2SDmitry Torokhov 	if (error) {
262893050db2SDmitry Torokhov 		kfree(priv);
2629a09221e8SDmitry Torokhov 		return error;
263093050db2SDmitry Torokhov 	}
26311da177e4SLinus Torvalds 
26321da177e4SLinus Torvalds 	if (set_properties) {
26331da177e4SLinus Torvalds 		psmouse->vendor = "ALPS";
2634a09221e8SDmitry Torokhov 		psmouse->name = priv->flags & ALPS_DUALPOINT ?
2635968ac842SDmitry Torokhov 				"DualPoint TouchPad" : "GlidePoint";
2636a09221e8SDmitry Torokhov 		psmouse->model = priv->proto_version;
2637a09221e8SDmitry Torokhov 	} else {
2638a09221e8SDmitry Torokhov 		/*
2639a09221e8SDmitry Torokhov 		 * Destroy alps_data structure we allocated earlier since
2640a09221e8SDmitry Torokhov 		 * this was just a "trial run". Otherwise we'll keep it
2641a09221e8SDmitry Torokhov 		 * to be used by alps_init() which has to be called if
2642a09221e8SDmitry Torokhov 		 * we succeed and set_properties is true.
2643a09221e8SDmitry Torokhov 		 */
2644a09221e8SDmitry Torokhov 		kfree(priv);
2645a09221e8SDmitry Torokhov 		psmouse->private = NULL;
26461da177e4SLinus Torvalds 	}
2647a09221e8SDmitry Torokhov 
26481da177e4SLinus Torvalds 	return 0;
26491da177e4SLinus Torvalds }
26501da177e4SLinus Torvalds 
2651