xref: /openbmc/linux/drivers/input/mouse/alps.c (revision e3a79212)
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 
24604aae283SPali Rohár 	alps_report_buttons(dev, dev2, left, right, middle);
247d7ed5d88SUlrich Dangel 
2481da177e4SLinus Torvalds 	/* Convert hardware tap to a reasonable Z value */
24971bb21b6SMaxim Levitsky 	if (ges && !fin)
25071bb21b6SMaxim Levitsky 		z = 40;
2511da177e4SLinus Torvalds 
2521da177e4SLinus Torvalds 	/*
2531da177e4SLinus Torvalds 	 * A "tap and drag" operation is reported by the hardware as a transition
2541da177e4SLinus Torvalds 	 * from (!fin && ges) to (fin && ges). This should be translated to the
2551da177e4SLinus Torvalds 	 * sequence Z>0, Z==0, Z>0, so the Z==0 event has to be generated manually.
2561da177e4SLinus Torvalds 	 */
2571da177e4SLinus Torvalds 	if (ges && fin && !priv->prev_fin) {
2581da177e4SLinus Torvalds 		input_report_abs(dev, ABS_X, x);
2591da177e4SLinus Torvalds 		input_report_abs(dev, ABS_Y, y);
2601da177e4SLinus Torvalds 		input_report_abs(dev, ABS_PRESSURE, 0);
2611da177e4SLinus Torvalds 		input_report_key(dev, BTN_TOOL_FINGER, 0);
2621da177e4SLinus Torvalds 		input_sync(dev);
2631da177e4SLinus Torvalds 	}
2641da177e4SLinus Torvalds 	priv->prev_fin = fin;
2651da177e4SLinus Torvalds 
26671bb21b6SMaxim Levitsky 	if (z > 30)
26771bb21b6SMaxim Levitsky 		input_report_key(dev, BTN_TOUCH, 1);
26871bb21b6SMaxim Levitsky 	if (z < 25)
26971bb21b6SMaxim Levitsky 		input_report_key(dev, BTN_TOUCH, 0);
2701da177e4SLinus Torvalds 
2711da177e4SLinus Torvalds 	if (z > 0) {
2721da177e4SLinus Torvalds 		input_report_abs(dev, ABS_X, x);
2731da177e4SLinus Torvalds 		input_report_abs(dev, ABS_Y, y);
2741da177e4SLinus Torvalds 	}
2751da177e4SLinus Torvalds 
2761da177e4SLinus Torvalds 	input_report_abs(dev, ABS_PRESSURE, z);
2771da177e4SLinus Torvalds 	input_report_key(dev, BTN_TOOL_FINGER, z > 0);
2781da177e4SLinus Torvalds 
27999df65e7SKevin Cernekee 	if (priv->flags & ALPS_WHEEL)
280e6c047b9SVojtech Pavlik 		input_report_rel(dev, REL_WHEEL, ((packet[2] << 1) & 0x08) - ((packet[0] >> 4) & 0x07));
2811da177e4SLinus Torvalds 
28299df65e7SKevin Cernekee 	if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
283c30b4c10SIvan Casado Ruiz 		input_report_key(dev, BTN_FORWARD, forward);
284c30b4c10SIvan Casado Ruiz 		input_report_key(dev, BTN_BACK, back);
2851da177e4SLinus Torvalds 	}
2861da177e4SLinus Torvalds 
28799df65e7SKevin Cernekee 	if (priv->flags & ALPS_FOUR_BUTTONS) {
28871bb21b6SMaxim Levitsky 		input_report_key(dev, BTN_0, packet[2] & 4);
28971bb21b6SMaxim Levitsky 		input_report_key(dev, BTN_1, packet[0] & 0x10);
29071bb21b6SMaxim Levitsky 		input_report_key(dev, BTN_2, packet[3] & 4);
29171bb21b6SMaxim Levitsky 		input_report_key(dev, BTN_3, packet[0] & 0x20);
29271bb21b6SMaxim Levitsky 	}
29371bb21b6SMaxim Levitsky 
2941da177e4SLinus Torvalds 	input_sync(dev);
2951da177e4SLinus Torvalds }
2961da177e4SLinus Torvalds 
29701ce661fSSeth Forshee /*
298ee65d4b3SYunkang Tang  * Process bitmap data for V5 protocols. Return value is null.
299ee65d4b3SYunkang Tang  *
300ee65d4b3SYunkang Tang  * The bitmaps don't have enough data to track fingers, so this function
301ee65d4b3SYunkang Tang  * only generates points representing a bounding box of at most two contacts.
30202d04254SHans de Goede  * These two points are returned in fields->mt.
303ee65d4b3SYunkang Tang  */
304ee65d4b3SYunkang Tang static void alps_process_bitmap_dolphin(struct alps_data *priv,
30502d04254SHans de Goede 					struct alps_fields *fields)
306ee65d4b3SYunkang Tang {
307ee65d4b3SYunkang Tang 	int box_middle_x, box_middle_y;
308ee65d4b3SYunkang Tang 	unsigned int x_map, y_map;
309ee65d4b3SYunkang Tang 	unsigned char start_bit, end_bit;
310ee65d4b3SYunkang Tang 	unsigned char x_msb, x_lsb, y_msb, y_lsb;
311ee65d4b3SYunkang Tang 
312ee65d4b3SYunkang Tang 	x_map = fields->x_map;
313ee65d4b3SYunkang Tang 	y_map = fields->y_map;
314ee65d4b3SYunkang Tang 
315ee65d4b3SYunkang Tang 	if (!x_map || !y_map)
316ee65d4b3SYunkang Tang 		return;
317ee65d4b3SYunkang Tang 
318ee65d4b3SYunkang Tang 	/* Get Most-significant and Least-significant bit */
319ee65d4b3SYunkang Tang 	x_msb = fls(x_map);
320ee65d4b3SYunkang Tang 	x_lsb = ffs(x_map);
321ee65d4b3SYunkang Tang 	y_msb = fls(y_map);
322ee65d4b3SYunkang Tang 	y_lsb = ffs(y_map);
323ee65d4b3SYunkang Tang 
324ee65d4b3SYunkang Tang 	/* Most-significant bit should never exceed max sensor line number */
325ee65d4b3SYunkang Tang 	if (x_msb > priv->x_bits || y_msb > priv->y_bits)
326ee65d4b3SYunkang Tang 		return;
327ee65d4b3SYunkang Tang 
328ee65d4b3SYunkang Tang 	if (fields->fingers > 1) {
329ee65d4b3SYunkang Tang 		start_bit = priv->x_bits - x_msb;
330ee65d4b3SYunkang Tang 		end_bit = priv->x_bits - x_lsb;
331ee65d4b3SYunkang Tang 		box_middle_x = (priv->x_max * (start_bit + end_bit)) /
332ee65d4b3SYunkang Tang 				(2 * (priv->x_bits - 1));
333ee65d4b3SYunkang Tang 
334ee65d4b3SYunkang Tang 		start_bit = y_lsb - 1;
335ee65d4b3SYunkang Tang 		end_bit = y_msb - 1;
336ee65d4b3SYunkang Tang 		box_middle_y = (priv->y_max * (start_bit + end_bit)) /
337ee65d4b3SYunkang Tang 				(2 * (priv->y_bits - 1));
33802d04254SHans de Goede 		fields->mt[0] = fields->st;
33902d04254SHans de Goede 		fields->mt[1].x = 2 * box_middle_x - fields->mt[0].x;
34002d04254SHans de Goede 		fields->mt[1].y = 2 * box_middle_y - fields->mt[0].y;
341ee65d4b3SYunkang Tang 	}
342ee65d4b3SYunkang Tang }
343ee65d4b3SYunkang Tang 
344036e6c7bSHans de Goede static void alps_get_bitmap_points(unsigned int map,
345036e6c7bSHans de Goede 				   struct alps_bitmap_point *low,
346036e6c7bSHans de Goede 				   struct alps_bitmap_point *high,
347036e6c7bSHans de Goede 				   int *fingers)
348036e6c7bSHans de Goede {
349036e6c7bSHans de Goede 	struct alps_bitmap_point *point;
350036e6c7bSHans de Goede 	int i, bit, prev_bit = 0;
351036e6c7bSHans de Goede 
352036e6c7bSHans de Goede 	point = low;
353036e6c7bSHans de Goede 	for (i = 0; map != 0; i++, map >>= 1) {
354036e6c7bSHans de Goede 		bit = map & 1;
355036e6c7bSHans de Goede 		if (bit) {
356036e6c7bSHans de Goede 			if (!prev_bit) {
357036e6c7bSHans de Goede 				point->start_bit = i;
358105affbfSHans de Goede 				point->num_bits = 0;
359036e6c7bSHans de Goede 				(*fingers)++;
360036e6c7bSHans de Goede 			}
361036e6c7bSHans de Goede 			point->num_bits++;
362036e6c7bSHans de Goede 		} else {
363036e6c7bSHans de Goede 			if (prev_bit)
364036e6c7bSHans de Goede 				point = high;
365036e6c7bSHans de Goede 		}
366036e6c7bSHans de Goede 		prev_bit = bit;
367036e6c7bSHans de Goede 	}
368036e6c7bSHans de Goede }
369036e6c7bSHans de Goede 
370ee65d4b3SYunkang Tang /*
37101ce661fSSeth Forshee  * Process bitmap data from v3 and v4 protocols. Returns the number of
37201ce661fSSeth Forshee  * fingers detected. A return value of 0 means at least one of the
37301ce661fSSeth Forshee  * bitmaps was empty.
37401ce661fSSeth Forshee  *
37501ce661fSSeth Forshee  * The bitmaps don't have enough data to track fingers, so this function
37601ce661fSSeth Forshee  * only generates points representing a bounding box of all contacts.
37702d04254SHans de Goede  * These points are returned in fields->mt when the return value
37801ce661fSSeth Forshee  * is greater than 0.
37901ce661fSSeth Forshee  */
3807a9f73e7SKevin Cernekee static int alps_process_bitmap(struct alps_data *priv,
38102d04254SHans de Goede 			       struct alps_fields *fields)
38201ce661fSSeth Forshee {
383036e6c7bSHans de Goede 	int i, fingers_x = 0, fingers_y = 0, fingers;
38401ce661fSSeth Forshee 	struct alps_bitmap_point x_low = {0,}, x_high = {0,};
38501ce661fSSeth Forshee 	struct alps_bitmap_point y_low = {0,}, y_high = {0,};
38601ce661fSSeth Forshee 
38702d04254SHans de Goede 	if (!fields->x_map || !fields->y_map)
38801ce661fSSeth Forshee 		return 0;
38901ce661fSSeth Forshee 
39002d04254SHans de Goede 	alps_get_bitmap_points(fields->x_map, &x_low, &x_high, &fingers_x);
39102d04254SHans de Goede 	alps_get_bitmap_points(fields->y_map, &y_low, &y_high, &fingers_y);
39201ce661fSSeth Forshee 
39301ce661fSSeth Forshee 	/*
39401ce661fSSeth Forshee 	 * Fingers can overlap, so we use the maximum count of fingers
39501ce661fSSeth Forshee 	 * on either axis as the finger count.
39601ce661fSSeth Forshee 	 */
39701ce661fSSeth Forshee 	fingers = max(fingers_x, fingers_y);
39801ce661fSSeth Forshee 
39901ce661fSSeth Forshee 	/*
40020bea68bSHans de Goede 	 * If an axis reports only a single contact, we have overlapping or
40120bea68bSHans de Goede 	 * adjacent fingers. Divide the single contact between the two points.
40201ce661fSSeth Forshee 	 */
40301ce661fSSeth Forshee 	if (fingers_x == 1) {
40428835f45SHans de Goede 		i = (x_low.num_bits - 1) / 2;
40501ce661fSSeth Forshee 		x_low.num_bits = x_low.num_bits - i;
40601ce661fSSeth Forshee 		x_high.start_bit = x_low.start_bit + i;
40701ce661fSSeth Forshee 		x_high.num_bits = max(i, 1);
40820bea68bSHans de Goede 	}
40920bea68bSHans de Goede 	if (fingers_y == 1) {
41028835f45SHans de Goede 		i = (y_low.num_bits - 1) / 2;
41101ce661fSSeth Forshee 		y_low.num_bits = y_low.num_bits - i;
41201ce661fSSeth Forshee 		y_high.start_bit = y_low.start_bit + i;
41301ce661fSSeth Forshee 		y_high.num_bits = max(i, 1);
41401ce661fSSeth Forshee 	}
41501ce661fSSeth Forshee 
41602d04254SHans de Goede 	fields->mt[0].x =
41702d04254SHans de Goede 		(priv->x_max * (2 * x_low.start_bit + x_low.num_bits - 1)) /
4187a9f73e7SKevin Cernekee 		(2 * (priv->x_bits - 1));
41902d04254SHans de Goede 	fields->mt[0].y =
42002d04254SHans de Goede 		(priv->y_max * (2 * y_low.start_bit + y_low.num_bits - 1)) /
4217a9f73e7SKevin Cernekee 		(2 * (priv->y_bits - 1));
42201ce661fSSeth Forshee 
42302d04254SHans de Goede 	fields->mt[1].x =
42402d04254SHans de Goede 		(priv->x_max * (2 * x_high.start_bit + x_high.num_bits - 1)) /
4257a9f73e7SKevin Cernekee 		(2 * (priv->x_bits - 1));
42602d04254SHans de Goede 	fields->mt[1].y =
42702d04254SHans de Goede 		(priv->y_max * (2 * y_high.start_bit + y_high.num_bits - 1)) /
4287a9f73e7SKevin Cernekee 		(2 * (priv->y_bits - 1));
42901ce661fSSeth Forshee 
43040e8f53bSHans de Goede 	/* y-bitmap order is reversed, except on rushmore */
431fb2dd7a6SDmitry Torokhov 	if (priv->proto_version != ALPS_PROTO_V3_RUSHMORE) {
43202d04254SHans de Goede 		fields->mt[0].y = priv->y_max - fields->mt[0].y;
43302d04254SHans de Goede 		fields->mt[1].y = priv->y_max - fields->mt[1].y;
43440e8f53bSHans de Goede 	}
43540e8f53bSHans de Goede 
43601ce661fSSeth Forshee 	return fingers;
43701ce661fSSeth Forshee }
43801ce661fSSeth Forshee 
439cdf333efSHans de Goede static void alps_set_slot(struct input_dev *dev, int slot, int x, int y)
44001ce661fSSeth Forshee {
44101ce661fSSeth Forshee 	input_mt_slot(dev, slot);
442cdf333efSHans de Goede 	input_mt_report_slot_state(dev, MT_TOOL_FINGER, true);
44301ce661fSSeth Forshee 	input_report_abs(dev, ABS_MT_POSITION_X, x);
44401ce661fSSeth Forshee 	input_report_abs(dev, ABS_MT_POSITION_Y, y);
44501ce661fSSeth Forshee }
44601ce661fSSeth Forshee 
447cdf333efSHans de Goede static void alps_report_mt_data(struct psmouse *psmouse, int n)
44801ce661fSSeth Forshee {
44902d04254SHans de Goede 	struct alps_data *priv = psmouse->private;
45002d04254SHans de Goede 	struct input_dev *dev = psmouse->dev;
45102d04254SHans de Goede 	struct alps_fields *f = &priv->f;
452cdf333efSHans de Goede 	int i, slot[MAX_TOUCHES];
45302d04254SHans de Goede 
454448c7f38SHenrik Rydberg 	input_mt_assign_slots(dev, slot, f->mt, n, 0);
455cdf333efSHans de Goede 	for (i = 0; i < n; i++)
456cdf333efSHans de Goede 		alps_set_slot(dev, slot[i], f->mt[i].x, f->mt[i].y);
457cdf333efSHans de Goede 
458cdf333efSHans de Goede 	input_mt_sync_frame(dev);
45901ce661fSSeth Forshee }
46001ce661fSSeth Forshee 
46168c21870SHans de Goede static void alps_report_semi_mt_data(struct psmouse *psmouse, int fingers)
46268c21870SHans de Goede {
46368c21870SHans de Goede 	struct alps_data *priv = psmouse->private;
46468c21870SHans de Goede 	struct input_dev *dev = psmouse->dev;
46568c21870SHans de Goede 	struct alps_fields *f = &priv->f;
46668c21870SHans de Goede 
46768c21870SHans de Goede 	/* Use st data when we don't have mt data */
46868c21870SHans de Goede 	if (fingers < 2) {
46968c21870SHans de Goede 		f->mt[0].x = f->st.x;
47068c21870SHans de Goede 		f->mt[0].y = f->st.y;
47168c21870SHans de Goede 		fingers = f->pressure > 0 ? 1 : 0;
47268c21870SHans de Goede 	}
47368c21870SHans de Goede 
47499d9996cSHans de Goede 	alps_report_mt_data(psmouse, (fingers <= 2) ? fingers : 2);
47568c21870SHans de Goede 
47668c21870SHans de Goede 	input_mt_report_finger_count(dev, fingers);
47768c21870SHans de Goede 
47868c21870SHans de Goede 	input_report_key(dev, BTN_LEFT, f->left);
47968c21870SHans de Goede 	input_report_key(dev, BTN_RIGHT, f->right);
48068c21870SHans de Goede 	input_report_key(dev, BTN_MIDDLE, f->middle);
48168c21870SHans de Goede 
48268c21870SHans de Goede 	input_report_abs(dev, ABS_PRESSURE, f->pressure);
48368c21870SHans de Goede 
48468c21870SHans de Goede 	input_sync(dev);
48568c21870SHans de Goede }
48668c21870SHans de Goede 
48725bded7cSSeth Forshee static void alps_process_trackstick_packet_v3(struct psmouse *psmouse)
48825bded7cSSeth Forshee {
48925bded7cSSeth Forshee 	struct alps_data *priv = psmouse->private;
49025bded7cSSeth Forshee 	unsigned char *packet = psmouse->packet;
49125bded7cSSeth Forshee 	struct input_dev *dev = priv->dev2;
49225bded7cSSeth Forshee 	int x, y, z, left, right, middle;
49325bded7cSSeth Forshee 
49434412ba2SPali Rohár 	/* It should be a DualPoint when received trackstick packet */
49534412ba2SPali Rohár 	if (!(priv->flags & ALPS_DUALPOINT)) {
49634412ba2SPali Rohár 		psmouse_warn(psmouse,
49734412ba2SPali Rohár 			     "Rejected trackstick packet from non DualPoint device");
49834412ba2SPali Rohár 		return;
49934412ba2SPali Rohár 	}
50034412ba2SPali Rohár 
50125bded7cSSeth Forshee 	/* Sanity check packet */
50225bded7cSSeth Forshee 	if (!(packet[0] & 0x40)) {
50325bded7cSSeth Forshee 		psmouse_dbg(psmouse, "Bad trackstick packet, discarding\n");
50425bded7cSSeth Forshee 		return;
50525bded7cSSeth Forshee 	}
50625bded7cSSeth Forshee 
50725bded7cSSeth Forshee 	/*
50825bded7cSSeth Forshee 	 * There's a special packet that seems to indicate the end
50925bded7cSSeth Forshee 	 * of a stream of trackstick data. Filter these out.
51025bded7cSSeth Forshee 	 */
51125bded7cSSeth Forshee 	if (packet[1] == 0x7f && packet[2] == 0x7f && packet[4] == 0x7f)
51225bded7cSSeth Forshee 		return;
51325bded7cSSeth Forshee 
51425bded7cSSeth Forshee 	x = (s8)(((packet[0] & 0x20) << 2) | (packet[1] & 0x7f));
51525bded7cSSeth Forshee 	y = (s8)(((packet[0] & 0x10) << 3) | (packet[2] & 0x7f));
51625bded7cSSeth Forshee 	z = (packet[4] & 0x7c) >> 2;
51725bded7cSSeth Forshee 
51825bded7cSSeth Forshee 	/*
51925bded7cSSeth Forshee 	 * The x and y values tend to be quite large, and when used
52025bded7cSSeth Forshee 	 * alone the trackstick is difficult to use. Scale them down
52125bded7cSSeth Forshee 	 * to compensate.
52225bded7cSSeth Forshee 	 */
52325bded7cSSeth Forshee 	x /= 8;
52425bded7cSSeth Forshee 	y /= 8;
52525bded7cSSeth Forshee 
52625bded7cSSeth Forshee 	input_report_rel(dev, REL_X, x);
52725bded7cSSeth Forshee 	input_report_rel(dev, REL_Y, -y);
52825bded7cSSeth Forshee 
52925bded7cSSeth Forshee 	/*
53025bded7cSSeth Forshee 	 * Most ALPS models report the trackstick buttons in the touchpad
53125bded7cSSeth Forshee 	 * packets, but a few report them here. No reliable way has been
53225bded7cSSeth Forshee 	 * found to differentiate between the models upfront, so we enable
53325bded7cSSeth Forshee 	 * the quirk in response to seeing a button press in the trackstick
53425bded7cSSeth Forshee 	 * packet.
53525bded7cSSeth Forshee 	 */
53625bded7cSSeth Forshee 	left = packet[3] & 0x01;
53725bded7cSSeth Forshee 	right = packet[3] & 0x02;
53825bded7cSSeth Forshee 	middle = packet[3] & 0x04;
53925bded7cSSeth Forshee 
54025bded7cSSeth Forshee 	if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) &&
54125bded7cSSeth Forshee 	    (left || right || middle))
54225bded7cSSeth Forshee 		priv->quirks |= ALPS_QUIRK_TRACKSTICK_BUTTONS;
54325bded7cSSeth Forshee 
54425bded7cSSeth Forshee 	if (priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) {
54525bded7cSSeth Forshee 		input_report_key(dev, BTN_LEFT, left);
54625bded7cSSeth Forshee 		input_report_key(dev, BTN_RIGHT, right);
54725bded7cSSeth Forshee 		input_report_key(dev, BTN_MIDDLE, middle);
54825bded7cSSeth Forshee 	}
54925bded7cSSeth Forshee 
55025bded7cSSeth Forshee 	input_sync(dev);
55125bded7cSSeth Forshee 	return;
55225bded7cSSeth Forshee }
55325bded7cSSeth Forshee 
554f85e5001SKevin Cernekee static void alps_decode_buttons_v3(struct alps_fields *f, unsigned char *p)
555f85e5001SKevin Cernekee {
556f85e5001SKevin Cernekee 	f->left = !!(p[3] & 0x01);
557f85e5001SKevin Cernekee 	f->right = !!(p[3] & 0x02);
558f85e5001SKevin Cernekee 	f->middle = !!(p[3] & 0x04);
559f85e5001SKevin Cernekee 
560f85e5001SKevin Cernekee 	f->ts_left = !!(p[3] & 0x10);
561f85e5001SKevin Cernekee 	f->ts_right = !!(p[3] & 0x20);
562f85e5001SKevin Cernekee 	f->ts_middle = !!(p[3] & 0x40);
563f85e5001SKevin Cernekee }
564f85e5001SKevin Cernekee 
56538c11eaaSHans de Goede static int alps_decode_pinnacle(struct alps_fields *f, unsigned char *p,
566ee65d4b3SYunkang Tang 				 struct psmouse *psmouse)
567f85e5001SKevin Cernekee {
568f85e5001SKevin Cernekee 	f->first_mp = !!(p[4] & 0x40);
569f85e5001SKevin Cernekee 	f->is_mp = !!(p[0] & 0x40);
570f85e5001SKevin Cernekee 
571f85e5001SKevin Cernekee 	f->fingers = (p[5] & 0x3) + 1;
572f85e5001SKevin Cernekee 	f->x_map = ((p[4] & 0x7e) << 8) |
573f85e5001SKevin Cernekee 		   ((p[1] & 0x7f) << 2) |
574f85e5001SKevin Cernekee 		   ((p[0] & 0x30) >> 4);
575f85e5001SKevin Cernekee 	f->y_map = ((p[3] & 0x70) << 4) |
576f85e5001SKevin Cernekee 		   ((p[2] & 0x7f) << 1) |
577f85e5001SKevin Cernekee 		   (p[4] & 0x01);
578f85e5001SKevin Cernekee 
57902d04254SHans de Goede 	f->st.x = ((p[1] & 0x7f) << 4) | ((p[4] & 0x30) >> 2) |
580f85e5001SKevin Cernekee 	       ((p[0] & 0x30) >> 4);
58102d04254SHans de Goede 	f->st.y = ((p[2] & 0x7f) << 4) | (p[4] & 0x0f);
58202d04254SHans de Goede 	f->pressure = p[5] & 0x7f;
583f85e5001SKevin Cernekee 
584f85e5001SKevin Cernekee 	alps_decode_buttons_v3(f, p);
58538c11eaaSHans de Goede 
58638c11eaaSHans de Goede 	return 0;
587f85e5001SKevin Cernekee }
588f85e5001SKevin Cernekee 
58938c11eaaSHans de Goede static int alps_decode_rushmore(struct alps_fields *f, unsigned char *p,
590ee65d4b3SYunkang Tang 				 struct psmouse *psmouse)
5911302bac3SKevin Cernekee {
592ee65d4b3SYunkang Tang 	alps_decode_pinnacle(f, p, psmouse);
5931302bac3SKevin Cernekee 
594f105e34aSYunkang Tang 	/* Rushmore's packet decode has a bit difference with Pinnacle's */
595f105e34aSYunkang Tang 	f->is_mp = !!(p[5] & 0x40);
596f105e34aSYunkang Tang 	f->fingers = max((p[5] & 0x3), ((p[5] >> 2) & 0x3)) + 1;
5971302bac3SKevin Cernekee 	f->x_map |= (p[5] & 0x10) << 11;
5981302bac3SKevin Cernekee 	f->y_map |= (p[5] & 0x20) << 6;
59938c11eaaSHans de Goede 
60038c11eaaSHans de Goede 	return 0;
6011302bac3SKevin Cernekee }
6021302bac3SKevin Cernekee 
60338c11eaaSHans de Goede static int alps_decode_dolphin(struct alps_fields *f, unsigned char *p,
604ee65d4b3SYunkang Tang 				struct psmouse *psmouse)
60575af9e56SDave Turvene {
606ee65d4b3SYunkang Tang 	u64 palm_data = 0;
607ee65d4b3SYunkang Tang 	struct alps_data *priv = psmouse->private;
608ee65d4b3SYunkang Tang 
60975af9e56SDave Turvene 	f->first_mp = !!(p[0] & 0x02);
61075af9e56SDave Turvene 	f->is_mp = !!(p[0] & 0x20);
61175af9e56SDave Turvene 
612ee65d4b3SYunkang Tang 	if (!f->is_mp) {
61302d04254SHans de Goede 		f->st.x = ((p[1] & 0x7f) | ((p[4] & 0x0f) << 7));
61402d04254SHans de Goede 		f->st.y = ((p[2] & 0x7f) | ((p[4] & 0xf0) << 3));
61502d04254SHans de Goede 		f->pressure = (p[0] & 4) ? 0 : p[5] & 0x7f;
61675af9e56SDave Turvene 		alps_decode_buttons_v3(f, p);
617ee65d4b3SYunkang Tang 	} else {
618ee65d4b3SYunkang Tang 		f->fingers = ((p[0] & 0x6) >> 1 |
619ee65d4b3SYunkang Tang 		     (p[0] & 0x10) >> 2);
620ee65d4b3SYunkang Tang 
621ee65d4b3SYunkang Tang 		palm_data = (p[1] & 0x7f) |
622ee65d4b3SYunkang Tang 			    ((p[2] & 0x7f) << 7) |
623ee65d4b3SYunkang Tang 			    ((p[4] & 0x7f) << 14) |
624ee65d4b3SYunkang Tang 			    ((p[5] & 0x7f) << 21) |
625ee65d4b3SYunkang Tang 			    ((p[3] & 0x07) << 28) |
626ee65d4b3SYunkang Tang 			    (((u64)p[3] & 0x70) << 27) |
627ee65d4b3SYunkang Tang 			    (((u64)p[0] & 0x01) << 34);
628ee65d4b3SYunkang Tang 
629ee65d4b3SYunkang Tang 		/* Y-profile is stored in P(0) to p(n-1), n = y_bits; */
630ee65d4b3SYunkang Tang 		f->y_map = palm_data & (BIT(priv->y_bits) - 1);
631ee65d4b3SYunkang Tang 
632ee65d4b3SYunkang Tang 		/* X-profile is stored in p(n) to p(n+m-1), m = x_bits; */
633ee65d4b3SYunkang Tang 		f->x_map = (palm_data >> priv->y_bits) &
634ee65d4b3SYunkang Tang 			   (BIT(priv->x_bits) - 1);
635ee65d4b3SYunkang Tang 	}
63638c11eaaSHans de Goede 
63738c11eaaSHans de Goede 	return 0;
63875af9e56SDave Turvene }
63975af9e56SDave Turvene 
640ee65d4b3SYunkang Tang static void alps_process_touchpad_packet_v3_v5(struct psmouse *psmouse)
64125bded7cSSeth Forshee {
64225bded7cSSeth Forshee 	struct alps_data *priv = psmouse->private;
64325bded7cSSeth Forshee 	unsigned char *packet = psmouse->packet;
64425bded7cSSeth Forshee 	struct input_dev *dev2 = priv->dev2;
64502d04254SHans de Goede 	struct alps_fields *f = &priv->f;
64602d04254SHans de Goede 	int fingers = 0;
647f85e5001SKevin Cernekee 
64802d04254SHans de Goede 	memset(f, 0, sizeof(*f));
64902d04254SHans de Goede 
65002d04254SHans de Goede 	priv->decode_fields(f, packet, psmouse);
65125bded7cSSeth Forshee 
65225bded7cSSeth Forshee 	/*
65301ce661fSSeth Forshee 	 * There's no single feature of touchpad position and bitmap packets
65401ce661fSSeth Forshee 	 * that can be used to distinguish between them. We rely on the fact
65501ce661fSSeth Forshee 	 * that a bitmap packet should always follow a position packet with
65601ce661fSSeth Forshee 	 * bit 6 of packet[4] set.
65725bded7cSSeth Forshee 	 */
65825bded7cSSeth Forshee 	if (priv->multi_packet) {
65925bded7cSSeth Forshee 		/*
66025bded7cSSeth Forshee 		 * Sometimes a position packet will indicate a multi-packet
66125bded7cSSeth Forshee 		 * sequence, but then what follows is another position
66225bded7cSSeth Forshee 		 * packet. Check for this, and when it happens process the
66325bded7cSSeth Forshee 		 * position packet as usual.
66425bded7cSSeth Forshee 		 */
66502d04254SHans de Goede 		if (f->is_mp) {
66602d04254SHans de Goede 			fingers = f->fingers;
667fb2dd7a6SDmitry Torokhov 			if (priv->proto_version == ALPS_PROTO_V3 ||
668fb2dd7a6SDmitry Torokhov 			    priv->proto_version == ALPS_PROTO_V3_RUSHMORE) {
66902d04254SHans de Goede 				if (alps_process_bitmap(priv, f) == 0)
67020bea68bSHans de Goede 					fingers = 0; /* Use st data */
67101ce661fSSeth Forshee 
67201ce661fSSeth Forshee 				/* Now process position packet */
67302d04254SHans de Goede 				priv->decode_fields(f, priv->multi_data,
674ee65d4b3SYunkang Tang 						    psmouse);
675ee65d4b3SYunkang Tang 			} else {
676ee65d4b3SYunkang Tang 				/*
677ee65d4b3SYunkang Tang 				 * Because Dolphin uses position packet's
678ee65d4b3SYunkang Tang 				 * coordinate data as Pt1 and uses it to
679ee65d4b3SYunkang Tang 				 * calculate Pt2, so we need to do position
680ee65d4b3SYunkang Tang 				 * packet decode first.
681ee65d4b3SYunkang Tang 				 */
68202d04254SHans de Goede 				priv->decode_fields(f, priv->multi_data,
683ee65d4b3SYunkang Tang 						    psmouse);
684ee65d4b3SYunkang Tang 
685ee65d4b3SYunkang Tang 				/*
686ee65d4b3SYunkang Tang 				 * Since Dolphin's finger number is reliable,
687ee65d4b3SYunkang Tang 				 * there is no need to compare with bmap_fn.
688ee65d4b3SYunkang Tang 				 */
68902d04254SHans de Goede 				alps_process_bitmap_dolphin(priv, f);
690ee65d4b3SYunkang Tang 			}
69101ce661fSSeth Forshee 		} else {
69201ce661fSSeth Forshee 			priv->multi_packet = 0;
69325bded7cSSeth Forshee 		}
69425bded7cSSeth Forshee 	}
69525bded7cSSeth Forshee 
69601ce661fSSeth Forshee 	/*
69701ce661fSSeth Forshee 	 * Bit 6 of byte 0 is not usually set in position packets. The only
69801ce661fSSeth Forshee 	 * times it seems to be set is in situations where the data is
69901ce661fSSeth Forshee 	 * suspect anyway, e.g. a palm resting flat on the touchpad. Given
70001ce661fSSeth Forshee 	 * this combined with the fact that this bit is useful for filtering
70101ce661fSSeth Forshee 	 * out misidentified bitmap packets, we reject anything with this
70201ce661fSSeth Forshee 	 * bit set.
70301ce661fSSeth Forshee 	 */
70402d04254SHans de Goede 	if (f->is_mp)
70501ce661fSSeth Forshee 		return;
70601ce661fSSeth Forshee 
70702d04254SHans de Goede 	if (!priv->multi_packet && f->first_mp) {
70825bded7cSSeth Forshee 		priv->multi_packet = 1;
70901ce661fSSeth Forshee 		memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
71001ce661fSSeth Forshee 		return;
71101ce661fSSeth Forshee 	}
71201ce661fSSeth Forshee 
71325bded7cSSeth Forshee 	priv->multi_packet = 0;
71425bded7cSSeth Forshee 
71525bded7cSSeth Forshee 	/*
71625bded7cSSeth Forshee 	 * Sometimes the hardware sends a single packet with z = 0
71725bded7cSSeth Forshee 	 * in the middle of a stream. Real releases generate packets
71825bded7cSSeth Forshee 	 * with x, y, and z all zero, so these seem to be flukes.
71925bded7cSSeth Forshee 	 * Ignore them.
72025bded7cSSeth Forshee 	 */
72102d04254SHans de Goede 	if (f->st.x && f->st.y && !f->pressure)
72225bded7cSSeth Forshee 		return;
72325bded7cSSeth Forshee 
72468c21870SHans de Goede 	alps_report_semi_mt_data(psmouse, fingers);
72525bded7cSSeth Forshee 
72634412ba2SPali Rohár 	if ((priv->flags & ALPS_DUALPOINT) &&
72734412ba2SPali Rohár 	    !(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS)) {
72802d04254SHans de Goede 		input_report_key(dev2, BTN_LEFT, f->ts_left);
72902d04254SHans de Goede 		input_report_key(dev2, BTN_RIGHT, f->ts_right);
73002d04254SHans de Goede 		input_report_key(dev2, BTN_MIDDLE, f->ts_middle);
73125bded7cSSeth Forshee 		input_sync(dev2);
73225bded7cSSeth Forshee 	}
73325bded7cSSeth Forshee }
73425bded7cSSeth Forshee 
73525bded7cSSeth Forshee static void alps_process_packet_v3(struct psmouse *psmouse)
73625bded7cSSeth Forshee {
73725bded7cSSeth Forshee 	unsigned char *packet = psmouse->packet;
73825bded7cSSeth Forshee 
73925bded7cSSeth Forshee 	/*
74025bded7cSSeth Forshee 	 * v3 protocol packets come in three types, two representing
74125bded7cSSeth Forshee 	 * touchpad data and one representing trackstick data.
74225bded7cSSeth Forshee 	 * Trackstick packets seem to be distinguished by always
74325bded7cSSeth Forshee 	 * having 0x3f in the last byte. This value has never been
74425bded7cSSeth Forshee 	 * observed in the last byte of either of the other types
74525bded7cSSeth Forshee 	 * of packets.
74625bded7cSSeth Forshee 	 */
74725bded7cSSeth Forshee 	if (packet[5] == 0x3f) {
74825bded7cSSeth Forshee 		alps_process_trackstick_packet_v3(psmouse);
74925bded7cSSeth Forshee 		return;
75025bded7cSSeth Forshee 	}
75125bded7cSSeth Forshee 
752ee65d4b3SYunkang Tang 	alps_process_touchpad_packet_v3_v5(psmouse);
75325bded7cSSeth Forshee }
75425bded7cSSeth Forshee 
75595f75e91SYunkang Tang static void alps_process_packet_v6(struct psmouse *psmouse)
75695f75e91SYunkang Tang {
75795f75e91SYunkang Tang 	struct alps_data *priv = psmouse->private;
75895f75e91SYunkang Tang 	unsigned char *packet = psmouse->packet;
75995f75e91SYunkang Tang 	struct input_dev *dev = psmouse->dev;
76095f75e91SYunkang Tang 	struct input_dev *dev2 = priv->dev2;
76195f75e91SYunkang Tang 	int x, y, z, left, right, middle;
76295f75e91SYunkang Tang 
76395f75e91SYunkang Tang 	/*
76495f75e91SYunkang Tang 	 * We can use Byte5 to distinguish if the packet is from Touchpad
76595f75e91SYunkang Tang 	 * or Trackpoint.
76695f75e91SYunkang Tang 	 * Touchpad:	0 - 0x7E
76795f75e91SYunkang Tang 	 * Trackpoint:	0x7F
76895f75e91SYunkang Tang 	 */
76995f75e91SYunkang Tang 	if (packet[5] == 0x7F) {
77095f75e91SYunkang Tang 		/* It should be a DualPoint when received Trackpoint packet */
77134412ba2SPali Rohár 		if (!(priv->flags & ALPS_DUALPOINT)) {
77234412ba2SPali Rohár 			psmouse_warn(psmouse,
77334412ba2SPali Rohár 				     "Rejected trackstick packet from non DualPoint device");
77495f75e91SYunkang Tang 			return;
77534412ba2SPali Rohár 		}
77695f75e91SYunkang Tang 
77795f75e91SYunkang Tang 		/* Trackpoint packet */
77895f75e91SYunkang Tang 		x = packet[1] | ((packet[3] & 0x20) << 2);
77995f75e91SYunkang Tang 		y = packet[2] | ((packet[3] & 0x40) << 1);
78095f75e91SYunkang Tang 		z = packet[4];
78195f75e91SYunkang Tang 		left = packet[3] & 0x01;
78295f75e91SYunkang Tang 		right = packet[3] & 0x02;
78395f75e91SYunkang Tang 		middle = packet[3] & 0x04;
78495f75e91SYunkang Tang 
78595f75e91SYunkang Tang 		/* To prevent the cursor jump when finger lifted */
78695f75e91SYunkang Tang 		if (x == 0x7F && y == 0x7F && z == 0x7F)
78795f75e91SYunkang Tang 			x = y = z = 0;
78895f75e91SYunkang Tang 
78995f75e91SYunkang Tang 		/* Divide 4 since trackpoint's speed is too fast */
79095f75e91SYunkang Tang 		input_report_rel(dev2, REL_X, (char)x / 4);
79195f75e91SYunkang Tang 		input_report_rel(dev2, REL_Y, -((char)y / 4));
79295f75e91SYunkang Tang 
79395f75e91SYunkang Tang 		input_report_key(dev2, BTN_LEFT, left);
79495f75e91SYunkang Tang 		input_report_key(dev2, BTN_RIGHT, right);
79595f75e91SYunkang Tang 		input_report_key(dev2, BTN_MIDDLE, middle);
79695f75e91SYunkang Tang 
79795f75e91SYunkang Tang 		input_sync(dev2);
79895f75e91SYunkang Tang 		return;
79995f75e91SYunkang Tang 	}
80095f75e91SYunkang Tang 
80195f75e91SYunkang Tang 	/* Touchpad packet */
80295f75e91SYunkang Tang 	x = packet[1] | ((packet[3] & 0x78) << 4);
80395f75e91SYunkang Tang 	y = packet[2] | ((packet[4] & 0x78) << 4);
80495f75e91SYunkang Tang 	z = packet[5];
80595f75e91SYunkang Tang 	left = packet[3] & 0x01;
80695f75e91SYunkang Tang 	right = packet[3] & 0x02;
80795f75e91SYunkang Tang 
80895f75e91SYunkang Tang 	if (z > 30)
80995f75e91SYunkang Tang 		input_report_key(dev, BTN_TOUCH, 1);
81095f75e91SYunkang Tang 	if (z < 25)
81195f75e91SYunkang Tang 		input_report_key(dev, BTN_TOUCH, 0);
81295f75e91SYunkang Tang 
81395f75e91SYunkang Tang 	if (z > 0) {
81495f75e91SYunkang Tang 		input_report_abs(dev, ABS_X, x);
81595f75e91SYunkang Tang 		input_report_abs(dev, ABS_Y, y);
81695f75e91SYunkang Tang 	}
81795f75e91SYunkang Tang 
81895f75e91SYunkang Tang 	input_report_abs(dev, ABS_PRESSURE, z);
81995f75e91SYunkang Tang 	input_report_key(dev, BTN_TOOL_FINGER, z > 0);
82095f75e91SYunkang Tang 
82195f75e91SYunkang Tang 	/* v6 touchpad does not have middle button */
82295f75e91SYunkang Tang 	input_report_key(dev, BTN_LEFT, left);
82395f75e91SYunkang Tang 	input_report_key(dev, BTN_RIGHT, right);
82495f75e91SYunkang Tang 
82595f75e91SYunkang Tang 	input_sync(dev);
82695f75e91SYunkang Tang }
82795f75e91SYunkang Tang 
82825bded7cSSeth Forshee static void alps_process_packet_v4(struct psmouse *psmouse)
82925bded7cSSeth Forshee {
8303b7e09faSGeorge Pantalos 	struct alps_data *priv = psmouse->private;
83125bded7cSSeth Forshee 	unsigned char *packet = psmouse->packet;
83202d04254SHans de Goede 	struct alps_fields *f = &priv->f;
83368c21870SHans de Goede 	int offset;
8343b7e09faSGeorge Pantalos 
8353b7e09faSGeorge Pantalos 	/*
8363b7e09faSGeorge Pantalos 	 * v4 has a 6-byte encoding for bitmap data, but this data is
8373b7e09faSGeorge Pantalos 	 * broken up between 3 normal packets. Use priv->multi_packet to
8383b7e09faSGeorge Pantalos 	 * track our position in the bitmap packet.
8393b7e09faSGeorge Pantalos 	 */
8403b7e09faSGeorge Pantalos 	if (packet[6] & 0x40) {
8413b7e09faSGeorge Pantalos 		/* sync, reset position */
8423b7e09faSGeorge Pantalos 		priv->multi_packet = 0;
8433b7e09faSGeorge Pantalos 	}
8443b7e09faSGeorge Pantalos 
8453b7e09faSGeorge Pantalos 	if (WARN_ON_ONCE(priv->multi_packet > 2))
8463b7e09faSGeorge Pantalos 		return;
8473b7e09faSGeorge Pantalos 
8483b7e09faSGeorge Pantalos 	offset = 2 * priv->multi_packet;
8493b7e09faSGeorge Pantalos 	priv->multi_data[offset] = packet[6];
8503b7e09faSGeorge Pantalos 	priv->multi_data[offset + 1] = packet[7];
8513b7e09faSGeorge Pantalos 
8523b7e09faSGeorge Pantalos 	if (++priv->multi_packet > 2) {
8533b7e09faSGeorge Pantalos 		priv->multi_packet = 0;
8543b7e09faSGeorge Pantalos 
85502d04254SHans de Goede 		f->x_map = ((priv->multi_data[2] & 0x1f) << 10) |
8563b7e09faSGeorge Pantalos 			   ((priv->multi_data[3] & 0x60) << 3) |
8573b7e09faSGeorge Pantalos 			   ((priv->multi_data[0] & 0x3f) << 2) |
8583b7e09faSGeorge Pantalos 			   ((priv->multi_data[1] & 0x60) >> 5);
85902d04254SHans de Goede 		f->y_map = ((priv->multi_data[5] & 0x01) << 10) |
8603b7e09faSGeorge Pantalos 			   ((priv->multi_data[3] & 0x1f) << 5) |
8613b7e09faSGeorge Pantalos 			    (priv->multi_data[1] & 0x1f);
8623b7e09faSGeorge Pantalos 
86302d04254SHans de Goede 		f->fingers = alps_process_bitmap(priv, f);
8643b7e09faSGeorge Pantalos 	}
86525bded7cSSeth Forshee 
866b0cfb794SAndreas Bosch 	f->left = !!(packet[4] & 0x01);
867b0cfb794SAndreas Bosch 	f->right = !!(packet[4] & 0x02);
86825bded7cSSeth Forshee 
86902d04254SHans de Goede 	f->st.x = ((packet[1] & 0x7f) << 4) | ((packet[3] & 0x30) >> 2) |
87025bded7cSSeth Forshee 		  ((packet[0] & 0x30) >> 4);
87102d04254SHans de Goede 	f->st.y = ((packet[2] & 0x7f) << 4) | (packet[3] & 0x0f);
87202d04254SHans de Goede 	f->pressure = packet[5] & 0x7f;
87325bded7cSSeth Forshee 
87468c21870SHans de Goede 	alps_report_semi_mt_data(psmouse, f->fingers);
87525bded7cSSeth Forshee }
87625bded7cSSeth Forshee 
8773808843cSYunkang Tang static bool alps_is_valid_package_v7(struct psmouse *psmouse)
8783808843cSYunkang Tang {
8793808843cSYunkang Tang 	switch (psmouse->pktcnt) {
8803808843cSYunkang Tang 	case 3:
8813808843cSYunkang Tang 		return (psmouse->packet[2] & 0x40) == 0x40;
8823808843cSYunkang Tang 	case 4:
8833808843cSYunkang Tang 		return (psmouse->packet[3] & 0x48) == 0x48;
8843808843cSYunkang Tang 	case 6:
8853808843cSYunkang Tang 		return (psmouse->packet[5] & 0x40) == 0x00;
8863808843cSYunkang Tang 	}
8873808843cSYunkang Tang 	return true;
8883808843cSYunkang Tang }
8893808843cSYunkang Tang 
8903808843cSYunkang Tang static unsigned char alps_get_packet_id_v7(char *byte)
8913808843cSYunkang Tang {
8923808843cSYunkang Tang 	unsigned char packet_id;
8933808843cSYunkang Tang 
8943808843cSYunkang Tang 	if (byte[4] & 0x40)
8953808843cSYunkang Tang 		packet_id = V7_PACKET_ID_TWO;
8963808843cSYunkang Tang 	else if (byte[4] & 0x01)
8973808843cSYunkang Tang 		packet_id = V7_PACKET_ID_MULTI;
8983808843cSYunkang Tang 	else if ((byte[0] & 0x10) && !(byte[4] & 0x43))
8993808843cSYunkang Tang 		packet_id = V7_PACKET_ID_NEW;
9003808843cSYunkang Tang 	else if (byte[1] == 0x00 && byte[4] == 0x00)
9013808843cSYunkang Tang 		packet_id = V7_PACKET_ID_IDLE;
9023808843cSYunkang Tang 	else
9033808843cSYunkang Tang 		packet_id = V7_PACKET_ID_UNKNOWN;
9043808843cSYunkang Tang 
9053808843cSYunkang Tang 	return packet_id;
9063808843cSYunkang Tang }
9073808843cSYunkang Tang 
9083808843cSYunkang Tang static void alps_get_finger_coordinate_v7(struct input_mt_pos *mt,
9093808843cSYunkang Tang 					  unsigned char *pkt,
9103808843cSYunkang Tang 					  unsigned char pkt_id)
9113808843cSYunkang Tang {
9123808843cSYunkang Tang 	mt[0].x = ((pkt[2] & 0x80) << 4);
9133808843cSYunkang Tang 	mt[0].x |= ((pkt[2] & 0x3F) << 5);
9143808843cSYunkang Tang 	mt[0].x |= ((pkt[3] & 0x30) >> 1);
9153808843cSYunkang Tang 	mt[0].x |= (pkt[3] & 0x07);
9163808843cSYunkang Tang 	mt[0].y = (pkt[1] << 3) | (pkt[0] & 0x07);
9173808843cSYunkang Tang 
9183808843cSYunkang Tang 	mt[1].x = ((pkt[3] & 0x80) << 4);
9193808843cSYunkang Tang 	mt[1].x |= ((pkt[4] & 0x80) << 3);
9203808843cSYunkang Tang 	mt[1].x |= ((pkt[4] & 0x3F) << 4);
9213808843cSYunkang Tang 	mt[1].y = ((pkt[5] & 0x80) << 3);
9223808843cSYunkang Tang 	mt[1].y |= ((pkt[5] & 0x3F) << 4);
9233808843cSYunkang Tang 
9243808843cSYunkang Tang 	switch (pkt_id) {
9253808843cSYunkang Tang 	case V7_PACKET_ID_TWO:
9263808843cSYunkang Tang 		mt[1].x &= ~0x000F;
9273808843cSYunkang Tang 		mt[1].y |= 0x000F;
9283808843cSYunkang Tang 		break;
9293808843cSYunkang Tang 
9303808843cSYunkang Tang 	case V7_PACKET_ID_MULTI:
9313808843cSYunkang Tang 		mt[1].x &= ~0x003F;
9323808843cSYunkang Tang 		mt[1].y &= ~0x0020;
9333808843cSYunkang Tang 		mt[1].y |= ((pkt[4] & 0x02) << 4);
9343808843cSYunkang Tang 		mt[1].y |= 0x001F;
9353808843cSYunkang Tang 		break;
9363808843cSYunkang Tang 
9373808843cSYunkang Tang 	case V7_PACKET_ID_NEW:
9383808843cSYunkang Tang 		mt[1].x &= ~0x003F;
9393808843cSYunkang Tang 		mt[1].x |= (pkt[0] & 0x20);
9403808843cSYunkang Tang 		mt[1].y |= 0x000F;
9413808843cSYunkang Tang 		break;
9423808843cSYunkang Tang 	}
9433808843cSYunkang Tang 
9443808843cSYunkang Tang 	mt[0].y = 0x7FF - mt[0].y;
9453808843cSYunkang Tang 	mt[1].y = 0x7FF - mt[1].y;
9463808843cSYunkang Tang }
9473808843cSYunkang Tang 
9483808843cSYunkang Tang static int alps_get_mt_count(struct input_mt_pos *mt)
9493808843cSYunkang Tang {
9507091c443SHans de Goede 	int i, fingers = 0;
9513808843cSYunkang Tang 
9527091c443SHans de Goede 	for (i = 0; i < MAX_TOUCHES; i++) {
9537091c443SHans de Goede 		if (mt[i].x != 0 || mt[i].y != 0)
9547091c443SHans de Goede 			fingers++;
9557091c443SHans de Goede 	}
9563808843cSYunkang Tang 
9577091c443SHans de Goede 	return fingers;
9583808843cSYunkang Tang }
9593808843cSYunkang Tang 
9603808843cSYunkang Tang static int alps_decode_packet_v7(struct alps_fields *f,
9613808843cSYunkang Tang 				  unsigned char *p,
9623808843cSYunkang Tang 				  struct psmouse *psmouse)
9633808843cSYunkang Tang {
964d27eb793SHans de Goede 	struct alps_data *priv = psmouse->private;
9653808843cSYunkang Tang 	unsigned char pkt_id;
9663808843cSYunkang Tang 
9673808843cSYunkang Tang 	pkt_id = alps_get_packet_id_v7(p);
9683808843cSYunkang Tang 	if (pkt_id == V7_PACKET_ID_IDLE)
9693808843cSYunkang Tang 		return 0;
9703808843cSYunkang Tang 	if (pkt_id == V7_PACKET_ID_UNKNOWN)
9713808843cSYunkang Tang 		return -1;
9728b238115SHans de Goede 	/*
9738b238115SHans de Goede 	 * NEW packets are send to indicate a discontinuity in the finger
9748b238115SHans de Goede 	 * coordinate reporting. Specifically a finger may have moved from
9758b238115SHans de Goede 	 * slot 0 to 1 or vice versa. INPUT_MT_TRACK takes care of this for
9768b238115SHans de Goede 	 * us.
9778b238115SHans de Goede 	 *
9788b238115SHans de Goede 	 * NEW packets have 3 problems:
9798b238115SHans de Goede 	 * 1) They do not contain middle / right button info (on non clickpads)
9808b238115SHans de Goede 	 *    this can be worked around by preserving the old button state
9818b238115SHans de Goede 	 * 2) They do not contain an accurate fingercount, and they are
9828b238115SHans de Goede 	 *    typically send when the number of fingers changes. We cannot use
9838b238115SHans de Goede 	 *    the old finger count as that may mismatch with the amount of
9848b238115SHans de Goede 	 *    touch coordinates we've available in the NEW packet
9858b238115SHans de Goede 	 * 3) Their x data for the second touch is inaccurate leading to
9868b238115SHans de Goede 	 *    a possible jump of the x coordinate by 16 units when the first
9878b238115SHans de Goede 	 *    non NEW packet comes in
9888b238115SHans de Goede 	 * Since problems 2 & 3 cannot be worked around, just ignore them.
9898b238115SHans de Goede 	 */
9908b238115SHans de Goede 	if (pkt_id == V7_PACKET_ID_NEW)
9918b238115SHans de Goede 		return 1;
9923808843cSYunkang Tang 
9933808843cSYunkang Tang 	alps_get_finger_coordinate_v7(f->mt, p, pkt_id);
9943808843cSYunkang Tang 
9953808843cSYunkang Tang 	if (pkt_id == V7_PACKET_ID_TWO)
9963808843cSYunkang Tang 		f->fingers = alps_get_mt_count(f->mt);
9978b238115SHans de Goede 	else /* pkt_id == V7_PACKET_ID_MULTI */
9983808843cSYunkang Tang 		f->fingers = 3 + (p[5] & 0x03);
9993808843cSYunkang Tang 
1000d27eb793SHans de Goede 	f->left = (p[0] & 0x80) >> 7;
1001d27eb793SHans de Goede 	if (priv->flags & ALPS_BUTTONPAD) {
1002d27eb793SHans de Goede 		if (p[0] & 0x20)
1003d27eb793SHans de Goede 			f->fingers++;
1004d27eb793SHans de Goede 		if (p[0] & 0x10)
1005d27eb793SHans de Goede 			f->fingers++;
1006d27eb793SHans de Goede 	} else {
1007d27eb793SHans de Goede 		f->right = (p[0] & 0x20) >> 5;
1008d27eb793SHans de Goede 		f->middle = (p[0] & 0x10) >> 4;
1009d27eb793SHans de Goede 	}
1010d27eb793SHans de Goede 
10117091c443SHans de Goede 	/* Sometimes a single touch is reported in mt[1] rather then mt[0] */
10127091c443SHans de Goede 	if (f->fingers == 1 && f->mt[0].x == 0 && f->mt[0].y == 0) {
10137091c443SHans de Goede 		f->mt[0].x = f->mt[1].x;
10147091c443SHans de Goede 		f->mt[0].y = f->mt[1].y;
10157091c443SHans de Goede 		f->mt[1].x = 0;
10167091c443SHans de Goede 		f->mt[1].y = 0;
10177091c443SHans de Goede 	}
10187091c443SHans de Goede 
10193808843cSYunkang Tang 	return 0;
10203808843cSYunkang Tang }
10213808843cSYunkang Tang 
10223808843cSYunkang Tang static void alps_process_trackstick_packet_v7(struct psmouse *psmouse)
10233808843cSYunkang Tang {
10243808843cSYunkang Tang 	struct alps_data *priv = psmouse->private;
10253808843cSYunkang Tang 	unsigned char *packet = psmouse->packet;
10263808843cSYunkang Tang 	struct input_dev *dev2 = priv->dev2;
10273808843cSYunkang Tang 	int x, y, z, left, right, middle;
10283808843cSYunkang Tang 
102934412ba2SPali Rohár 	/* It should be a DualPoint when received trackstick packet */
103034412ba2SPali Rohár 	if (!(priv->flags & ALPS_DUALPOINT)) {
103134412ba2SPali Rohár 		psmouse_warn(psmouse,
103234412ba2SPali Rohár 			     "Rejected trackstick packet from non DualPoint device");
103334412ba2SPali Rohár 		return;
103434412ba2SPali Rohár 	}
103534412ba2SPali Rohár 
10363808843cSYunkang Tang 	x = ((packet[2] & 0xbf)) | ((packet[3] & 0x10) << 2);
10373808843cSYunkang Tang 	y = (packet[3] & 0x07) | (packet[4] & 0xb8) |
10383808843cSYunkang Tang 	    ((packet[3] & 0x20) << 1);
10393808843cSYunkang Tang 	z = (packet[5] & 0x3f) | ((packet[3] & 0x80) >> 1);
10403808843cSYunkang Tang 
10413808843cSYunkang Tang 	left = (packet[1] & 0x01);
10423808843cSYunkang Tang 	right = (packet[1] & 0x02) >> 1;
10433808843cSYunkang Tang 	middle = (packet[1] & 0x04) >> 2;
10443808843cSYunkang Tang 
10453808843cSYunkang Tang 	/* Divide 2 since trackpoint's speed is too fast */
10463808843cSYunkang Tang 	input_report_rel(dev2, REL_X, (char)x / 2);
10473808843cSYunkang Tang 	input_report_rel(dev2, REL_Y, -((char)y / 2));
10483808843cSYunkang Tang 
10493808843cSYunkang Tang 	input_report_key(dev2, BTN_LEFT, left);
10503808843cSYunkang Tang 	input_report_key(dev2, BTN_RIGHT, right);
10513808843cSYunkang Tang 	input_report_key(dev2, BTN_MIDDLE, middle);
10523808843cSYunkang Tang 
10533808843cSYunkang Tang 	input_sync(dev2);
10543808843cSYunkang Tang }
10553808843cSYunkang Tang 
10563808843cSYunkang Tang static void alps_process_touchpad_packet_v7(struct psmouse *psmouse)
10573808843cSYunkang Tang {
10583808843cSYunkang Tang 	struct alps_data *priv = psmouse->private;
10593808843cSYunkang Tang 	struct input_dev *dev = psmouse->dev;
10603808843cSYunkang Tang 	struct alps_fields *f = &priv->f;
10613808843cSYunkang Tang 
10623808843cSYunkang Tang 	memset(f, 0, sizeof(*f));
10633808843cSYunkang Tang 
10643808843cSYunkang Tang 	if (priv->decode_fields(f, psmouse->packet, psmouse))
10653808843cSYunkang Tang 		return;
10663808843cSYunkang Tang 
10673808843cSYunkang Tang 	alps_report_mt_data(psmouse, alps_get_mt_count(f->mt));
10683808843cSYunkang Tang 
10693808843cSYunkang Tang 	input_mt_report_finger_count(dev, f->fingers);
10703808843cSYunkang Tang 
10713808843cSYunkang Tang 	input_report_key(dev, BTN_LEFT, f->left);
10723808843cSYunkang Tang 	input_report_key(dev, BTN_RIGHT, f->right);
10733808843cSYunkang Tang 	input_report_key(dev, BTN_MIDDLE, f->middle);
10743808843cSYunkang Tang 
10753808843cSYunkang Tang 	input_sync(dev);
10763808843cSYunkang Tang }
10773808843cSYunkang Tang 
10783808843cSYunkang Tang static void alps_process_packet_v7(struct psmouse *psmouse)
10793808843cSYunkang Tang {
10803808843cSYunkang Tang 	unsigned char *packet = psmouse->packet;
10813808843cSYunkang Tang 
10823808843cSYunkang Tang 	if (packet[0] == 0x48 && (packet[4] & 0x47) == 0x06)
10833808843cSYunkang Tang 		alps_process_trackstick_packet_v7(psmouse);
10843808843cSYunkang Tang 	else
10853808843cSYunkang Tang 		alps_process_touchpad_packet_v7(psmouse);
10863808843cSYunkang Tang }
10873808843cSYunkang Tang 
108804aae283SPali Rohár static DEFINE_MUTEX(alps_mutex);
108904aae283SPali Rohár 
109004aae283SPali Rohár static void alps_register_bare_ps2_mouse(struct work_struct *work)
109104aae283SPali Rohár {
109204aae283SPali Rohár 	struct alps_data *priv =
109304aae283SPali Rohár 		container_of(work, struct alps_data, dev3_register_work.work);
109404aae283SPali Rohár 	struct psmouse *psmouse = priv->psmouse;
109504aae283SPali Rohár 	struct input_dev *dev3;
109604aae283SPali Rohár 	int error = 0;
109704aae283SPali Rohár 
109804aae283SPali Rohár 	mutex_lock(&alps_mutex);
109904aae283SPali Rohár 
110004aae283SPali Rohár 	if (priv->dev3)
110104aae283SPali Rohár 		goto out;
110204aae283SPali Rohár 
110304aae283SPali Rohár 	dev3 = input_allocate_device();
110404aae283SPali Rohár 	if (!dev3) {
110504aae283SPali Rohár 		psmouse_err(psmouse, "failed to allocate secondary device\n");
110604aae283SPali Rohár 		error = -ENOMEM;
110704aae283SPali Rohár 		goto out;
110804aae283SPali Rohár 	}
110904aae283SPali Rohár 
111004aae283SPali Rohár 	snprintf(priv->phys3, sizeof(priv->phys3), "%s/%s",
111104aae283SPali Rohár 		 psmouse->ps2dev.serio->phys,
111204aae283SPali Rohár 		 (priv->dev2 ? "input2" : "input1"));
111304aae283SPali Rohár 	dev3->phys = priv->phys3;
111404aae283SPali Rohár 
111504aae283SPali Rohár 	/*
111604aae283SPali Rohár 	 * format of input device name is: "protocol vendor name"
111704aae283SPali Rohár 	 * see function psmouse_switch_protocol() in psmouse-base.c
111804aae283SPali Rohár 	 */
111904aae283SPali Rohár 	dev3->name = "PS/2 ALPS Mouse";
112004aae283SPali Rohár 
112104aae283SPali Rohár 	dev3->id.bustype = BUS_I8042;
112204aae283SPali Rohár 	dev3->id.vendor  = 0x0002;
112304aae283SPali Rohár 	dev3->id.product = PSMOUSE_PS2;
112404aae283SPali Rohár 	dev3->id.version = 0x0000;
112504aae283SPali Rohár 	dev3->dev.parent = &psmouse->ps2dev.serio->dev;
112604aae283SPali Rohár 
112704aae283SPali Rohár 	input_set_capability(dev3, EV_REL, REL_X);
112804aae283SPali Rohár 	input_set_capability(dev3, EV_REL, REL_Y);
112904aae283SPali Rohár 	input_set_capability(dev3, EV_KEY, BTN_LEFT);
113004aae283SPali Rohár 	input_set_capability(dev3, EV_KEY, BTN_RIGHT);
113104aae283SPali Rohár 	input_set_capability(dev3, EV_KEY, BTN_MIDDLE);
113204aae283SPali Rohár 
113304aae283SPali Rohár 	__set_bit(INPUT_PROP_POINTER, dev3->propbit);
113404aae283SPali Rohár 
113504aae283SPali Rohár 	error = input_register_device(dev3);
113604aae283SPali Rohár 	if (error) {
113704aae283SPali Rohár 		psmouse_err(psmouse,
113804aae283SPali Rohár 			    "failed to register secondary device: %d\n",
113904aae283SPali Rohár 			    error);
114004aae283SPali Rohár 		input_free_device(dev3);
114104aae283SPali Rohár 		goto out;
114204aae283SPali Rohár 	}
114304aae283SPali Rohár 
114404aae283SPali Rohár 	priv->dev3 = dev3;
114504aae283SPali Rohár 
114604aae283SPali Rohár out:
114704aae283SPali Rohár 	/*
114804aae283SPali Rohár 	 * Save the error code so that we can detect that we
114904aae283SPali Rohár 	 * already tried to create the device.
115004aae283SPali Rohár 	 */
115104aae283SPali Rohár 	if (error)
115204aae283SPali Rohár 		priv->dev3 = ERR_PTR(error);
115304aae283SPali Rohár 
115404aae283SPali Rohár 	mutex_unlock(&alps_mutex);
115504aae283SPali Rohár }
115604aae283SPali Rohár 
115759c30afbSHans de Goede static void alps_report_bare_ps2_packet(struct psmouse *psmouse,
11581d9f2626SSebastian Kapfer 					unsigned char packet[],
11591d9f2626SSebastian Kapfer 					bool report_buttons)
11601d9f2626SSebastian Kapfer {
116159c30afbSHans de Goede 	struct alps_data *priv = psmouse->private;
116259c30afbSHans de Goede 	struct input_dev *dev;
116359c30afbSHans de Goede 
1164e3a79212SHans de Goede 	/* Figure out which device to use to report the bare packet */
1165e3a79212SHans de Goede 	if (priv->proto_version == ALPS_PROTO_V2 &&
1166e3a79212SHans de Goede 	    (priv->flags & ALPS_DUALPOINT)) {
1167e3a79212SHans de Goede 		/* On V2 devices the DualPoint Stick reports bare packets */
1168e3a79212SHans de Goede 		dev = priv->dev2;
1169e3a79212SHans de Goede 	} else if (unlikely(IS_ERR_OR_NULL(priv->dev3))) {
117059c30afbSHans de Goede 		/* Register dev3 mouse if we received PS/2 packet first time */
117159c30afbSHans de Goede 		if (!IS_ERR(priv->dev3))
117259c30afbSHans de Goede 			psmouse_queue_work(psmouse, &priv->dev3_register_work,
117359c30afbSHans de Goede 					   0);
117459c30afbSHans de Goede 		return;
117559c30afbSHans de Goede 	} else {
117659c30afbSHans de Goede 		dev = priv->dev3;
117759c30afbSHans de Goede 	}
117859c30afbSHans de Goede 
11791d9f2626SSebastian Kapfer 	if (report_buttons)
118004aae283SPali Rohár 		alps_report_buttons(dev, NULL,
11811d9f2626SSebastian Kapfer 				packet[0] & 1, packet[0] & 2, packet[0] & 4);
11821d9f2626SSebastian Kapfer 
118304aae283SPali Rohár 	input_report_rel(dev, REL_X,
11841d9f2626SSebastian Kapfer 		packet[1] ? packet[1] - ((packet[0] << 4) & 0x100) : 0);
118504aae283SPali Rohár 	input_report_rel(dev, REL_Y,
11861d9f2626SSebastian Kapfer 		packet[2] ? ((packet[0] << 3) & 0x100) - packet[2] : 0);
11871d9f2626SSebastian Kapfer 
118804aae283SPali Rohár 	input_sync(dev);
11891d9f2626SSebastian Kapfer }
11901d9f2626SSebastian Kapfer 
11911d9f2626SSebastian Kapfer static psmouse_ret_t alps_handle_interleaved_ps2(struct psmouse *psmouse)
11921da177e4SLinus Torvalds {
11931da177e4SLinus Torvalds 	struct alps_data *priv = psmouse->private;
11941da177e4SLinus Torvalds 
11951d9f2626SSebastian Kapfer 	if (psmouse->pktcnt < 6)
11961d9f2626SSebastian Kapfer 		return PSMOUSE_GOOD_DATA;
11971d9f2626SSebastian Kapfer 
11981d9f2626SSebastian Kapfer 	if (psmouse->pktcnt == 6) {
11991d9f2626SSebastian Kapfer 		/*
12001d9f2626SSebastian Kapfer 		 * Start a timer to flush the packet if it ends up last
12011d9f2626SSebastian Kapfer 		 * 6-byte packet in the stream. Timer needs to fire
12021d9f2626SSebastian Kapfer 		 * psmouse core times out itself. 20 ms should be enough
12031d9f2626SSebastian Kapfer 		 * to decide if we are getting more data or not.
12041d9f2626SSebastian Kapfer 		 */
12051d9f2626SSebastian Kapfer 		mod_timer(&priv->timer, jiffies + msecs_to_jiffies(20));
12061d9f2626SSebastian Kapfer 		return PSMOUSE_GOOD_DATA;
12071d9f2626SSebastian Kapfer 	}
12081d9f2626SSebastian Kapfer 
12091d9f2626SSebastian Kapfer 	del_timer(&priv->timer);
12101d9f2626SSebastian Kapfer 
12111d9f2626SSebastian Kapfer 	if (psmouse->packet[6] & 0x80) {
12121d9f2626SSebastian Kapfer 
12131d9f2626SSebastian Kapfer 		/*
12141d9f2626SSebastian Kapfer 		 * Highest bit is set - that means we either had
12151d9f2626SSebastian Kapfer 		 * complete ALPS packet and this is start of the
12161d9f2626SSebastian Kapfer 		 * next packet or we got garbage.
12171d9f2626SSebastian Kapfer 		 */
12181d9f2626SSebastian Kapfer 
12191d9f2626SSebastian Kapfer 		if (((psmouse->packet[3] |
12201d9f2626SSebastian Kapfer 		      psmouse->packet[4] |
12211d9f2626SSebastian Kapfer 		      psmouse->packet[5]) & 0x80) ||
122299df65e7SKevin Cernekee 		    (!alps_is_valid_first_byte(priv, psmouse->packet[6]))) {
1223b5d21704SDmitry Torokhov 			psmouse_dbg(psmouse,
12243b112923SAndy Shevchenko 				    "refusing packet %4ph (suspected interleaved ps/2)\n",
12253b112923SAndy Shevchenko 				    psmouse->packet + 3);
12261d9f2626SSebastian Kapfer 			return PSMOUSE_BAD_DATA;
12271d9f2626SSebastian Kapfer 		}
12281d9f2626SSebastian Kapfer 
122924af5cb9SKevin Cernekee 		priv->process_packet(psmouse);
12301d9f2626SSebastian Kapfer 
12311d9f2626SSebastian Kapfer 		/* Continue with the next packet */
12321d9f2626SSebastian Kapfer 		psmouse->packet[0] = psmouse->packet[6];
12331d9f2626SSebastian Kapfer 		psmouse->pktcnt = 1;
12341d9f2626SSebastian Kapfer 
12351d9f2626SSebastian Kapfer 	} else {
12361d9f2626SSebastian Kapfer 
12371d9f2626SSebastian Kapfer 		/*
12381d9f2626SSebastian Kapfer 		 * High bit is 0 - that means that we indeed got a PS/2
12391d9f2626SSebastian Kapfer 		 * packet in the middle of ALPS packet.
12401d9f2626SSebastian Kapfer 		 *
12411d9f2626SSebastian Kapfer 		 * There is also possibility that we got 6-byte ALPS
12421d9f2626SSebastian Kapfer 		 * packet followed  by 3-byte packet from trackpoint. We
12431d9f2626SSebastian Kapfer 		 * can not distinguish between these 2 scenarios but
1244b5d21704SDmitry Torokhov 		 * because the latter is unlikely to happen in course of
12451d9f2626SSebastian Kapfer 		 * normal operation (user would need to press all
12461d9f2626SSebastian Kapfer 		 * buttons on the pad and start moving trackpoint
12471d9f2626SSebastian Kapfer 		 * without touching the pad surface) we assume former.
12481d9f2626SSebastian Kapfer 		 * Even if we are wrong the wost thing that would happen
12491d9f2626SSebastian Kapfer 		 * the cursor would jump but we should not get protocol
1250b5d21704SDmitry Torokhov 		 * de-synchronization.
12511d9f2626SSebastian Kapfer 		 */
12521d9f2626SSebastian Kapfer 
125359c30afbSHans de Goede 		alps_report_bare_ps2_packet(psmouse, &psmouse->packet[3],
125459c30afbSHans de Goede 					    false);
12551d9f2626SSebastian Kapfer 
12561d9f2626SSebastian Kapfer 		/*
12571d9f2626SSebastian Kapfer 		 * Continue with the standard ALPS protocol handling,
12581d9f2626SSebastian Kapfer 		 * but make sure we won't process it as an interleaved
12591d9f2626SSebastian Kapfer 		 * packet again, which may happen if all buttons are
12601d9f2626SSebastian Kapfer 		 * pressed. To avoid this let's reset the 4th bit which
12611d9f2626SSebastian Kapfer 		 * is normally 1.
12621d9f2626SSebastian Kapfer 		 */
12631d9f2626SSebastian Kapfer 		psmouse->packet[3] = psmouse->packet[6] & 0xf7;
12641d9f2626SSebastian Kapfer 		psmouse->pktcnt = 4;
12651d9f2626SSebastian Kapfer 	}
12661d9f2626SSebastian Kapfer 
12671d9f2626SSebastian Kapfer 	return PSMOUSE_GOOD_DATA;
12681d9f2626SSebastian Kapfer }
12691d9f2626SSebastian Kapfer 
12701d9f2626SSebastian Kapfer static void alps_flush_packet(unsigned long data)
12711d9f2626SSebastian Kapfer {
12721d9f2626SSebastian Kapfer 	struct psmouse *psmouse = (struct psmouse *)data;
127324af5cb9SKevin Cernekee 	struct alps_data *priv = psmouse->private;
12741d9f2626SSebastian Kapfer 
12751d9f2626SSebastian Kapfer 	serio_pause_rx(psmouse->ps2dev.serio);
12761d9f2626SSebastian Kapfer 
1277b46615feSSeth Forshee 	if (psmouse->pktcnt == psmouse->pktsize) {
12781d9f2626SSebastian Kapfer 
12791d9f2626SSebastian Kapfer 		/*
12801d9f2626SSebastian Kapfer 		 * We did not any more data in reasonable amount of time.
12811d9f2626SSebastian Kapfer 		 * Validate the last 3 bytes and process as a standard
12821d9f2626SSebastian Kapfer 		 * ALPS packet.
12831d9f2626SSebastian Kapfer 		 */
12841d9f2626SSebastian Kapfer 		if ((psmouse->packet[3] |
12851d9f2626SSebastian Kapfer 		     psmouse->packet[4] |
12861d9f2626SSebastian Kapfer 		     psmouse->packet[5]) & 0x80) {
1287b5d21704SDmitry Torokhov 			psmouse_dbg(psmouse,
12883b112923SAndy Shevchenko 				    "refusing packet %3ph (suspected interleaved ps/2)\n",
12893b112923SAndy Shevchenko 				    psmouse->packet + 3);
12901d9f2626SSebastian Kapfer 		} else {
129124af5cb9SKevin Cernekee 			priv->process_packet(psmouse);
12921d9f2626SSebastian Kapfer 		}
12931d9f2626SSebastian Kapfer 		psmouse->pktcnt = 0;
12941d9f2626SSebastian Kapfer 	}
12951d9f2626SSebastian Kapfer 
12961d9f2626SSebastian Kapfer 	serio_continue_rx(psmouse->ps2dev.serio);
12971d9f2626SSebastian Kapfer }
12981d9f2626SSebastian Kapfer 
12991d9f2626SSebastian Kapfer static psmouse_ret_t alps_process_byte(struct psmouse *psmouse)
13001d9f2626SSebastian Kapfer {
13011d9f2626SSebastian Kapfer 	struct alps_data *priv = psmouse->private;
13021d9f2626SSebastian Kapfer 
13034ab8f7f3SPali Rohár 	/*
13044ab8f7f3SPali Rohár 	 * Check if we are dealing with a bare PS/2 packet, presumably from
13054ab8f7f3SPali Rohár 	 * a device connected to the external PS/2 port. Because bare PS/2
13064ab8f7f3SPali Rohár 	 * protocol does not have enough constant bits to self-synchronize
13074ab8f7f3SPali Rohár 	 * properly we only do this if the device is fully synchronized.
13084ab8f7f3SPali Rohár 	 */
13094ab8f7f3SPali Rohár 	if (!psmouse->out_of_sync_cnt && (psmouse->packet[0] & 0xc8) == 0x08) {
13101da177e4SLinus Torvalds 		if (psmouse->pktcnt == 3) {
131159c30afbSHans de Goede 			alps_report_bare_ps2_packet(psmouse, psmouse->packet,
13121d9f2626SSebastian Kapfer 						    true);
13131da177e4SLinus Torvalds 			return PSMOUSE_FULL_PACKET;
13141da177e4SLinus Torvalds 		}
13151da177e4SLinus Torvalds 		return PSMOUSE_GOOD_DATA;
13161da177e4SLinus Torvalds 	}
13171da177e4SLinus Torvalds 
13181d9f2626SSebastian Kapfer 	/* Check for PS/2 packet stuffed in the middle of ALPS packet. */
13191d9f2626SSebastian Kapfer 
132099df65e7SKevin Cernekee 	if ((priv->flags & ALPS_PS2_INTERLEAVED) &&
13211d9f2626SSebastian Kapfer 	    psmouse->pktcnt >= 4 && (psmouse->packet[3] & 0x0f) == 0x0f) {
13221d9f2626SSebastian Kapfer 		return alps_handle_interleaved_ps2(psmouse);
13231d9f2626SSebastian Kapfer 	}
13241d9f2626SSebastian Kapfer 
132599df65e7SKevin Cernekee 	if (!alps_is_valid_first_byte(priv, psmouse->packet[0])) {
1326b5d21704SDmitry Torokhov 		psmouse_dbg(psmouse,
1327b5d21704SDmitry Torokhov 			    "refusing packet[0] = %x (mask0 = %x, byte0 = %x)\n",
132899df65e7SKevin Cernekee 			    psmouse->packet[0], priv->mask0, priv->byte0);
13291da177e4SLinus Torvalds 		return PSMOUSE_BAD_DATA;
13301d9f2626SSebastian Kapfer 	}
13311da177e4SLinus Torvalds 
1332b46615feSSeth Forshee 	/* Bytes 2 - pktsize should have 0 in the highest bit */
1333a7ef82aeSPali Rohár 	if (priv->proto_version < ALPS_PROTO_V5 &&
133475af9e56SDave Turvene 	    psmouse->pktcnt >= 2 && psmouse->pktcnt <= psmouse->pktsize &&
13351d9f2626SSebastian Kapfer 	    (psmouse->packet[psmouse->pktcnt - 1] & 0x80)) {
1336b5d21704SDmitry Torokhov 		psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
1337b5d21704SDmitry Torokhov 			    psmouse->pktcnt - 1,
1338b5d21704SDmitry Torokhov 			    psmouse->packet[psmouse->pktcnt - 1]);
1339a7ef82aeSPali Rohár 
1340fb2dd7a6SDmitry Torokhov 		if (priv->proto_version == ALPS_PROTO_V3_RUSHMORE &&
1341a7ef82aeSPali Rohár 		    psmouse->pktcnt == psmouse->pktsize) {
1342a7ef82aeSPali Rohár 			/*
1343a7ef82aeSPali Rohár 			 * Some Dell boxes, such as Latitude E6440 or E7440
1344a7ef82aeSPali Rohár 			 * with closed lid, quite often smash last byte of
1345a7ef82aeSPali Rohár 			 * otherwise valid packet with 0xff. Given that the
1346a7ef82aeSPali Rohár 			 * next packet is very likely to be valid let's
1347a7ef82aeSPali Rohár 			 * report PSMOUSE_FULL_PACKET but not process data,
1348a7ef82aeSPali Rohár 			 * rather than reporting PSMOUSE_BAD_DATA and
1349a7ef82aeSPali Rohár 			 * filling the logs.
1350a7ef82aeSPali Rohár 			 */
1351a7ef82aeSPali Rohár 			return PSMOUSE_FULL_PACKET;
1352a7ef82aeSPali Rohár 		}
1353a7ef82aeSPali Rohár 
13541da177e4SLinus Torvalds 		return PSMOUSE_BAD_DATA;
13551d9f2626SSebastian Kapfer 	}
13561da177e4SLinus Torvalds 
13573808843cSYunkang Tang 	if (priv->proto_version == ALPS_PROTO_V7 &&
13583808843cSYunkang Tang 	    !alps_is_valid_package_v7(psmouse)) {
13593808843cSYunkang Tang 		psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
13603808843cSYunkang Tang 			    psmouse->pktcnt - 1,
13613808843cSYunkang Tang 			    psmouse->packet[psmouse->pktcnt - 1]);
13623808843cSYunkang Tang 		return PSMOUSE_BAD_DATA;
13633808843cSYunkang Tang 	}
13643808843cSYunkang Tang 
1365b46615feSSeth Forshee 	if (psmouse->pktcnt == psmouse->pktsize) {
136624af5cb9SKevin Cernekee 		priv->process_packet(psmouse);
13671da177e4SLinus Torvalds 		return PSMOUSE_FULL_PACKET;
13681da177e4SLinus Torvalds 	}
13691da177e4SLinus Torvalds 
13701da177e4SLinus Torvalds 	return PSMOUSE_GOOD_DATA;
13711da177e4SLinus Torvalds }
13721da177e4SLinus Torvalds 
137325bded7cSSeth Forshee static int alps_command_mode_send_nibble(struct psmouse *psmouse, int nibble)
137425bded7cSSeth Forshee {
137525bded7cSSeth Forshee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
137625bded7cSSeth Forshee 	struct alps_data *priv = psmouse->private;
137725bded7cSSeth Forshee 	int command;
137825bded7cSSeth Forshee 	unsigned char *param;
137925bded7cSSeth Forshee 	unsigned char dummy[4];
138025bded7cSSeth Forshee 
138125bded7cSSeth Forshee 	BUG_ON(nibble > 0xf);
138225bded7cSSeth Forshee 
138325bded7cSSeth Forshee 	command = priv->nibble_commands[nibble].command;
138425bded7cSSeth Forshee 	param = (command & 0x0f00) ?
138525bded7cSSeth Forshee 		dummy : (unsigned char *)&priv->nibble_commands[nibble].data;
138625bded7cSSeth Forshee 
138725bded7cSSeth Forshee 	if (ps2_command(ps2dev, param, command))
138825bded7cSSeth Forshee 		return -1;
138925bded7cSSeth Forshee 
139025bded7cSSeth Forshee 	return 0;
139125bded7cSSeth Forshee }
139225bded7cSSeth Forshee 
139325bded7cSSeth Forshee static int alps_command_mode_set_addr(struct psmouse *psmouse, int addr)
139425bded7cSSeth Forshee {
139525bded7cSSeth Forshee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
139625bded7cSSeth Forshee 	struct alps_data *priv = psmouse->private;
139725bded7cSSeth Forshee 	int i, nibble;
139825bded7cSSeth Forshee 
139925bded7cSSeth Forshee 	if (ps2_command(ps2dev, NULL, priv->addr_command))
140025bded7cSSeth Forshee 		return -1;
140125bded7cSSeth Forshee 
140225bded7cSSeth Forshee 	for (i = 12; i >= 0; i -= 4) {
140325bded7cSSeth Forshee 		nibble = (addr >> i) & 0xf;
140425bded7cSSeth Forshee 		if (alps_command_mode_send_nibble(psmouse, nibble))
140525bded7cSSeth Forshee 			return -1;
140625bded7cSSeth Forshee 	}
140725bded7cSSeth Forshee 
140825bded7cSSeth Forshee 	return 0;
140925bded7cSSeth Forshee }
141025bded7cSSeth Forshee 
141125bded7cSSeth Forshee static int __alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
141225bded7cSSeth Forshee {
141325bded7cSSeth Forshee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
141425bded7cSSeth Forshee 	unsigned char param[4];
141525bded7cSSeth Forshee 
141625bded7cSSeth Forshee 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
141725bded7cSSeth Forshee 		return -1;
141825bded7cSSeth Forshee 
141925bded7cSSeth Forshee 	/*
142025bded7cSSeth Forshee 	 * The address being read is returned in the first two bytes
142125bded7cSSeth Forshee 	 * of the result. Check that this address matches the expected
142225bded7cSSeth Forshee 	 * address.
142325bded7cSSeth Forshee 	 */
142425bded7cSSeth Forshee 	if (addr != ((param[0] << 8) | param[1]))
142525bded7cSSeth Forshee 		return -1;
142625bded7cSSeth Forshee 
142725bded7cSSeth Forshee 	return param[2];
142825bded7cSSeth Forshee }
142925bded7cSSeth Forshee 
143025bded7cSSeth Forshee static int alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
143125bded7cSSeth Forshee {
143225bded7cSSeth Forshee 	if (alps_command_mode_set_addr(psmouse, addr))
143325bded7cSSeth Forshee 		return -1;
143425bded7cSSeth Forshee 	return __alps_command_mode_read_reg(psmouse, addr);
143525bded7cSSeth Forshee }
143625bded7cSSeth Forshee 
143725bded7cSSeth Forshee static int __alps_command_mode_write_reg(struct psmouse *psmouse, u8 value)
143825bded7cSSeth Forshee {
143925bded7cSSeth Forshee 	if (alps_command_mode_send_nibble(psmouse, (value >> 4) & 0xf))
144025bded7cSSeth Forshee 		return -1;
144125bded7cSSeth Forshee 	if (alps_command_mode_send_nibble(psmouse, value & 0xf))
144225bded7cSSeth Forshee 		return -1;
144325bded7cSSeth Forshee 	return 0;
144425bded7cSSeth Forshee }
144525bded7cSSeth Forshee 
144625bded7cSSeth Forshee static int alps_command_mode_write_reg(struct psmouse *psmouse, int addr,
144725bded7cSSeth Forshee 				       u8 value)
144825bded7cSSeth Forshee {
144925bded7cSSeth Forshee 	if (alps_command_mode_set_addr(psmouse, addr))
145025bded7cSSeth Forshee 		return -1;
145125bded7cSSeth Forshee 	return __alps_command_mode_write_reg(psmouse, value);
145225bded7cSSeth Forshee }
145325bded7cSSeth Forshee 
145424ba9707SKevin Cernekee static int alps_rpt_cmd(struct psmouse *psmouse, int init_command,
145524ba9707SKevin Cernekee 			int repeated_command, unsigned char *param)
145624ba9707SKevin Cernekee {
145724ba9707SKevin Cernekee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
145824ba9707SKevin Cernekee 
145924ba9707SKevin Cernekee 	param[0] = 0;
146024ba9707SKevin Cernekee 	if (init_command && ps2_command(ps2dev, param, init_command))
146124ba9707SKevin Cernekee 		return -EIO;
146224ba9707SKevin Cernekee 
146324ba9707SKevin Cernekee 	if (ps2_command(ps2dev,  NULL, repeated_command) ||
146424ba9707SKevin Cernekee 	    ps2_command(ps2dev,  NULL, repeated_command) ||
146524ba9707SKevin Cernekee 	    ps2_command(ps2dev,  NULL, repeated_command))
146624ba9707SKevin Cernekee 		return -EIO;
146724ba9707SKevin Cernekee 
146824ba9707SKevin Cernekee 	param[0] = param[1] = param[2] = 0xff;
146924ba9707SKevin Cernekee 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
147024ba9707SKevin Cernekee 		return -EIO;
147124ba9707SKevin Cernekee 
147239fbe585SDmitry Torokhov 	psmouse_dbg(psmouse, "%2.2X report: %3ph\n",
147339fbe585SDmitry Torokhov 		    repeated_command, param);
147424ba9707SKevin Cernekee 	return 0;
147524ba9707SKevin Cernekee }
147624ba9707SKevin Cernekee 
14773808843cSYunkang Tang static bool alps_check_valid_firmware_id(unsigned char id[])
14783808843cSYunkang Tang {
14793808843cSYunkang Tang 	if (id[0] == 0x73)
14803808843cSYunkang Tang 		return true;
14813808843cSYunkang Tang 
14823808843cSYunkang Tang 	if (id[0] == 0x88 &&
14833808843cSYunkang Tang 	    (id[1] == 0x07 ||
14843808843cSYunkang Tang 	     id[1] == 0x08 ||
14853808843cSYunkang Tang 	     (id[1] & 0xf0) == 0xb0 ||
14863808843cSYunkang Tang 	     (id[1] & 0xf0) == 0xc0)) {
14873808843cSYunkang Tang 		return true;
14883808843cSYunkang Tang 	}
14893808843cSYunkang Tang 
14903808843cSYunkang Tang 	return false;
14913808843cSYunkang Tang }
14923808843cSYunkang Tang 
1493d18e53fcSKevin Cernekee static int alps_enter_command_mode(struct psmouse *psmouse)
149425bded7cSSeth Forshee {
149525bded7cSSeth Forshee 	unsigned char param[4];
149625bded7cSSeth Forshee 
149724ba9707SKevin Cernekee 	if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_RESET_WRAP, param)) {
149825bded7cSSeth Forshee 		psmouse_err(psmouse, "failed to enter command mode\n");
149925bded7cSSeth Forshee 		return -1;
150025bded7cSSeth Forshee 	}
150125bded7cSSeth Forshee 
15023808843cSYunkang Tang 	if (!alps_check_valid_firmware_id(param)) {
150325bded7cSSeth Forshee 		psmouse_dbg(psmouse,
150424ba9707SKevin Cernekee 			    "unknown response while entering command mode\n");
150525bded7cSSeth Forshee 		return -1;
150625bded7cSSeth Forshee 	}
150725bded7cSSeth Forshee 	return 0;
150825bded7cSSeth Forshee }
150925bded7cSSeth Forshee 
151025bded7cSSeth Forshee static inline int alps_exit_command_mode(struct psmouse *psmouse)
151125bded7cSSeth Forshee {
151225bded7cSSeth Forshee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
151325bded7cSSeth Forshee 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM))
151425bded7cSSeth Forshee 		return -1;
151525bded7cSSeth Forshee 	return 0;
151625bded7cSSeth Forshee }
151725bded7cSSeth Forshee 
15181da177e4SLinus Torvalds /*
15191da177e4SLinus Torvalds  * For DualPoint devices select the device that should respond to
15201da177e4SLinus Torvalds  * subsequent commands. It looks like glidepad is behind stickpointer,
15211da177e4SLinus Torvalds  * I'd thought it would be other way around...
15221da177e4SLinus Torvalds  */
152325bded7cSSeth Forshee static int alps_passthrough_mode_v2(struct psmouse *psmouse, bool enable)
15241da177e4SLinus Torvalds {
15251da177e4SLinus Torvalds 	struct ps2dev *ps2dev = &psmouse->ps2dev;
15261da177e4SLinus Torvalds 	int cmd = enable ? PSMOUSE_CMD_SETSCALE21 : PSMOUSE_CMD_SETSCALE11;
15271da177e4SLinus Torvalds 
15281da177e4SLinus Torvalds 	if (ps2_command(ps2dev, NULL, cmd) ||
15291da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, cmd) ||
15301da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, cmd) ||
15311da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
15321da177e4SLinus Torvalds 		return -1;
15331da177e4SLinus Torvalds 
15341da177e4SLinus Torvalds 	/* we may get 3 more bytes, just ignore them */
1535c611763dSDmitry Torokhov 	ps2_drain(ps2dev, 3, 100);
15361da177e4SLinus Torvalds 
15371da177e4SLinus Torvalds 	return 0;
15381da177e4SLinus Torvalds }
15391da177e4SLinus Torvalds 
154025bded7cSSeth Forshee static int alps_absolute_mode_v1_v2(struct psmouse *psmouse)
15411da177e4SLinus Torvalds {
15421da177e4SLinus Torvalds 	struct ps2dev *ps2dev = &psmouse->ps2dev;
15431da177e4SLinus Torvalds 
15441da177e4SLinus Torvalds 	/* Try ALPS magic knock - 4 disable before enable */
15451da177e4SLinus Torvalds 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
15461da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
15471da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
15481da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
15491da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
15501da177e4SLinus Torvalds 		return -1;
15511da177e4SLinus Torvalds 
15521da177e4SLinus Torvalds 	/*
15531da177e4SLinus Torvalds 	 * Switch mouse to poll (remote) mode so motion data will not
15541da177e4SLinus Torvalds 	 * get in our way
15551da177e4SLinus Torvalds 	 */
15561da177e4SLinus Torvalds 	return ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETPOLL);
15571da177e4SLinus Torvalds }
15581da177e4SLinus Torvalds 
155995f75e91SYunkang Tang static int alps_monitor_mode_send_word(struct psmouse *psmouse, u16 word)
156095f75e91SYunkang Tang {
156195f75e91SYunkang Tang 	int i, nibble;
156295f75e91SYunkang Tang 
156395f75e91SYunkang Tang 	/*
156495f75e91SYunkang Tang 	 * b0-b11 are valid bits, send sequence is inverse.
156595f75e91SYunkang Tang 	 * e.g. when word = 0x0123, nibble send sequence is 3, 2, 1
156695f75e91SYunkang Tang 	 */
156795f75e91SYunkang Tang 	for (i = 0; i <= 8; i += 4) {
156895f75e91SYunkang Tang 		nibble = (word >> i) & 0xf;
156995f75e91SYunkang Tang 		if (alps_command_mode_send_nibble(psmouse, nibble))
157095f75e91SYunkang Tang 			return -1;
157195f75e91SYunkang Tang 	}
157295f75e91SYunkang Tang 
157395f75e91SYunkang Tang 	return 0;
157495f75e91SYunkang Tang }
157595f75e91SYunkang Tang 
157695f75e91SYunkang Tang static int alps_monitor_mode_write_reg(struct psmouse *psmouse,
157795f75e91SYunkang Tang 				       u16 addr, u16 value)
157895f75e91SYunkang Tang {
157995f75e91SYunkang Tang 	struct ps2dev *ps2dev = &psmouse->ps2dev;
158095f75e91SYunkang Tang 
158195f75e91SYunkang Tang 	/* 0x0A0 is the command to write the word */
158295f75e91SYunkang Tang 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE) ||
158395f75e91SYunkang Tang 	    alps_monitor_mode_send_word(psmouse, 0x0A0) ||
158495f75e91SYunkang Tang 	    alps_monitor_mode_send_word(psmouse, addr) ||
158595f75e91SYunkang Tang 	    alps_monitor_mode_send_word(psmouse, value) ||
158695f75e91SYunkang Tang 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
158795f75e91SYunkang Tang 		return -1;
158895f75e91SYunkang Tang 
158995f75e91SYunkang Tang 	return 0;
159095f75e91SYunkang Tang }
159195f75e91SYunkang Tang 
159295f75e91SYunkang Tang static int alps_monitor_mode(struct psmouse *psmouse, bool enable)
159395f75e91SYunkang Tang {
159495f75e91SYunkang Tang 	struct ps2dev *ps2dev = &psmouse->ps2dev;
159595f75e91SYunkang Tang 
159695f75e91SYunkang Tang 	if (enable) {
159795f75e91SYunkang Tang 		/* EC E9 F5 F5 E7 E6 E7 E9 to enter monitor mode */
159895f75e91SYunkang Tang 		if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
159995f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO) ||
160095f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
160195f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
160295f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
160395f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
160495f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
160595f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO))
160695f75e91SYunkang Tang 			return -1;
160795f75e91SYunkang Tang 	} else {
160895f75e91SYunkang Tang 		/* EC to exit monitor mode */
160995f75e91SYunkang Tang 		if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP))
161095f75e91SYunkang Tang 			return -1;
161195f75e91SYunkang Tang 	}
161295f75e91SYunkang Tang 
161395f75e91SYunkang Tang 	return 0;
161495f75e91SYunkang Tang }
161595f75e91SYunkang Tang 
161695f75e91SYunkang Tang static int alps_absolute_mode_v6(struct psmouse *psmouse)
161795f75e91SYunkang Tang {
161895f75e91SYunkang Tang 	u16 reg_val = 0x181;
161995f75e91SYunkang Tang 	int ret = -1;
162095f75e91SYunkang Tang 
162195f75e91SYunkang Tang 	/* enter monitor mode, to write the register */
162295f75e91SYunkang Tang 	if (alps_monitor_mode(psmouse, true))
162395f75e91SYunkang Tang 		return -1;
162495f75e91SYunkang Tang 
162595f75e91SYunkang Tang 	ret = alps_monitor_mode_write_reg(psmouse, 0x000, reg_val);
162695f75e91SYunkang Tang 
162795f75e91SYunkang Tang 	if (alps_monitor_mode(psmouse, false))
162895f75e91SYunkang Tang 		ret = -1;
162995f75e91SYunkang Tang 
163095f75e91SYunkang Tang 	return ret;
163195f75e91SYunkang Tang }
163295f75e91SYunkang Tang 
16331da177e4SLinus Torvalds static int alps_get_status(struct psmouse *psmouse, char *param)
16341da177e4SLinus Torvalds {
16351da177e4SLinus Torvalds 	/* Get status: 0xF5 0xF5 0xF5 0xE9 */
163624ba9707SKevin Cernekee 	if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_DISABLE, param))
16371da177e4SLinus Torvalds 		return -1;
16381da177e4SLinus Torvalds 
16391da177e4SLinus Torvalds 	return 0;
16401da177e4SLinus Torvalds }
16411da177e4SLinus Torvalds 
16421da177e4SLinus Torvalds /*
16431da177e4SLinus Torvalds  * Turn touchpad tapping on or off. The sequences are:
16441da177e4SLinus Torvalds  * 0xE9 0xF5 0xF5 0xF3 0x0A to enable,
16451da177e4SLinus Torvalds  * 0xE9 0xF5 0xF5 0xE8 0x00 to disable.
16461da177e4SLinus Torvalds  * My guess that 0xE9 (GetInfo) is here as a sync point.
16471da177e4SLinus Torvalds  * For models that also have stickpointer (DualPoints) its tapping
16481da177e4SLinus Torvalds  * is controlled separately (0xE6 0xE6 0xE6 0xF3 0x14|0x0A) but
16491da177e4SLinus Torvalds  * we don't fiddle with it.
16501da177e4SLinus Torvalds  */
16511da177e4SLinus Torvalds static int alps_tap_mode(struct psmouse *psmouse, int enable)
16521da177e4SLinus Torvalds {
16531da177e4SLinus Torvalds 	struct ps2dev *ps2dev = &psmouse->ps2dev;
16541da177e4SLinus Torvalds 	int cmd = enable ? PSMOUSE_CMD_SETRATE : PSMOUSE_CMD_SETRES;
16551da177e4SLinus Torvalds 	unsigned char tap_arg = enable ? 0x0A : 0x00;
16561da177e4SLinus Torvalds 	unsigned char param[4];
16571da177e4SLinus Torvalds 
16581da177e4SLinus Torvalds 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO) ||
16591da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
16601da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
16611da177e4SLinus Torvalds 	    ps2_command(ps2dev, &tap_arg, cmd))
16621da177e4SLinus Torvalds 		return -1;
16631da177e4SLinus Torvalds 
16641da177e4SLinus Torvalds 	if (alps_get_status(psmouse, param))
16651da177e4SLinus Torvalds 		return -1;
16661da177e4SLinus Torvalds 
16671da177e4SLinus Torvalds 	return 0;
16681da177e4SLinus Torvalds }
16691da177e4SLinus Torvalds 
1670f0d5c6f4SDmitry Torokhov /*
1671f0d5c6f4SDmitry Torokhov  * alps_poll() - poll the touchpad for current motion packet.
1672f0d5c6f4SDmitry Torokhov  * Used in resync.
1673f0d5c6f4SDmitry Torokhov  */
1674f0d5c6f4SDmitry Torokhov static int alps_poll(struct psmouse *psmouse)
1675f0d5c6f4SDmitry Torokhov {
1676f0d5c6f4SDmitry Torokhov 	struct alps_data *priv = psmouse->private;
1677b46615feSSeth Forshee 	unsigned char buf[sizeof(psmouse->packet)];
1678b7802c5cSDmitry Torokhov 	bool poll_failed;
1679f0d5c6f4SDmitry Torokhov 
168099df65e7SKevin Cernekee 	if (priv->flags & ALPS_PASS)
168125bded7cSSeth Forshee 		alps_passthrough_mode_v2(psmouse, true);
1682f0d5c6f4SDmitry Torokhov 
1683f0d5c6f4SDmitry Torokhov 	poll_failed = ps2_command(&psmouse->ps2dev, buf,
1684f0d5c6f4SDmitry Torokhov 				  PSMOUSE_CMD_POLL | (psmouse->pktsize << 8)) < 0;
1685f0d5c6f4SDmitry Torokhov 
168699df65e7SKevin Cernekee 	if (priv->flags & ALPS_PASS)
168725bded7cSSeth Forshee 		alps_passthrough_mode_v2(psmouse, false);
1688f0d5c6f4SDmitry Torokhov 
168999df65e7SKevin Cernekee 	if (poll_failed || (buf[0] & priv->mask0) != priv->byte0)
1690f0d5c6f4SDmitry Torokhov 		return -1;
1691f0d5c6f4SDmitry Torokhov 
1692f0d5c6f4SDmitry Torokhov 	if ((psmouse->badbyte & 0xc8) == 0x08) {
1693f0d5c6f4SDmitry Torokhov /*
1694f0d5c6f4SDmitry Torokhov  * Poll the track stick ...
1695f0d5c6f4SDmitry Torokhov  */
1696f0d5c6f4SDmitry Torokhov 		if (ps2_command(&psmouse->ps2dev, buf, PSMOUSE_CMD_POLL | (3 << 8)))
1697f0d5c6f4SDmitry Torokhov 			return -1;
1698f0d5c6f4SDmitry Torokhov 	}
1699f0d5c6f4SDmitry Torokhov 
1700f0d5c6f4SDmitry Torokhov 	memcpy(psmouse->packet, buf, sizeof(buf));
1701f0d5c6f4SDmitry Torokhov 	return 0;
1702f0d5c6f4SDmitry Torokhov }
1703f0d5c6f4SDmitry Torokhov 
170425bded7cSSeth Forshee static int alps_hw_init_v1_v2(struct psmouse *psmouse)
17051da177e4SLinus Torvalds {
17061da177e4SLinus Torvalds 	struct alps_data *priv = psmouse->private;
17071da177e4SLinus Torvalds 
170899df65e7SKevin Cernekee 	if ((priv->flags & ALPS_PASS) &&
170925bded7cSSeth Forshee 	    alps_passthrough_mode_v2(psmouse, true)) {
17101da177e4SLinus Torvalds 		return -1;
1711b7802c5cSDmitry Torokhov 	}
17121da177e4SLinus Torvalds 
1713b7802c5cSDmitry Torokhov 	if (alps_tap_mode(psmouse, true)) {
1714b5d21704SDmitry Torokhov 		psmouse_warn(psmouse, "Failed to enable hardware tapping\n");
17151da177e4SLinus Torvalds 		return -1;
1716963f626dSPeter Osterlund 	}
17171da177e4SLinus Torvalds 
171825bded7cSSeth Forshee 	if (alps_absolute_mode_v1_v2(psmouse)) {
1719b5d21704SDmitry Torokhov 		psmouse_err(psmouse, "Failed to enable absolute mode\n");
17201da177e4SLinus Torvalds 		return -1;
17211da177e4SLinus Torvalds 	}
17221da177e4SLinus Torvalds 
172399df65e7SKevin Cernekee 	if ((priv->flags & ALPS_PASS) &&
172425bded7cSSeth Forshee 	    alps_passthrough_mode_v2(psmouse, false)) {
17251da177e4SLinus Torvalds 		return -1;
1726b7802c5cSDmitry Torokhov 	}
17271da177e4SLinus Torvalds 
17281e0c5b12SDmitry Torokhov 	/* ALPS needs stream mode, otherwise it won't report any data */
17291e0c5b12SDmitry Torokhov 	if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)) {
1730b5d21704SDmitry Torokhov 		psmouse_err(psmouse, "Failed to enable stream mode\n");
17311e0c5b12SDmitry Torokhov 		return -1;
17321e0c5b12SDmitry Torokhov 	}
17331e0c5b12SDmitry Torokhov 
17341e0c5b12SDmitry Torokhov 	return 0;
17351e0c5b12SDmitry Torokhov }
17361e0c5b12SDmitry Torokhov 
173795f75e91SYunkang Tang static int alps_hw_init_v6(struct psmouse *psmouse)
173895f75e91SYunkang Tang {
173995f75e91SYunkang Tang 	unsigned char param[2] = {0xC8, 0x14};
174095f75e91SYunkang Tang 
174195f75e91SYunkang Tang 	/* Enter passthrough mode to let trackpoint enter 6byte raw mode */
174295f75e91SYunkang Tang 	if (alps_passthrough_mode_v2(psmouse, true))
174395f75e91SYunkang Tang 		return -1;
174495f75e91SYunkang Tang 
174595f75e91SYunkang Tang 	if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
174695f75e91SYunkang Tang 	    ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
174795f75e91SYunkang Tang 	    ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
174895f75e91SYunkang Tang 	    ps2_command(&psmouse->ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
174995f75e91SYunkang Tang 	    ps2_command(&psmouse->ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
175095f75e91SYunkang Tang 		return -1;
175195f75e91SYunkang Tang 
175295f75e91SYunkang Tang 	if (alps_passthrough_mode_v2(psmouse, false))
175395f75e91SYunkang Tang 		return -1;
175495f75e91SYunkang Tang 
175595f75e91SYunkang Tang 	if (alps_absolute_mode_v6(psmouse)) {
175695f75e91SYunkang Tang 		psmouse_err(psmouse, "Failed to enable absolute mode\n");
175795f75e91SYunkang Tang 		return -1;
175895f75e91SYunkang Tang 	}
175995f75e91SYunkang Tang 
176095f75e91SYunkang Tang 	return 0;
176195f75e91SYunkang Tang }
176295f75e91SYunkang Tang 
176325bded7cSSeth Forshee /*
1764cd401204SKevin Cernekee  * Enable or disable passthrough mode to the trackstick.
176525bded7cSSeth Forshee  */
1766cd401204SKevin Cernekee static int alps_passthrough_mode_v3(struct psmouse *psmouse,
1767cd401204SKevin Cernekee 				    int reg_base, bool enable)
176825bded7cSSeth Forshee {
1769cd401204SKevin Cernekee 	int reg_val, ret = -1;
177025bded7cSSeth Forshee 
1771d18e53fcSKevin Cernekee 	if (alps_enter_command_mode(psmouse))
177225bded7cSSeth Forshee 		return -1;
177325bded7cSSeth Forshee 
1774cd401204SKevin Cernekee 	reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x0008);
1775cd401204SKevin Cernekee 	if (reg_val == -1)
1776cd401204SKevin Cernekee 		goto error;
1777cd401204SKevin Cernekee 
177825bded7cSSeth Forshee 	if (enable)
177925bded7cSSeth Forshee 		reg_val |= 0x01;
178025bded7cSSeth Forshee 	else
178125bded7cSSeth Forshee 		reg_val &= ~0x01;
178225bded7cSSeth Forshee 
1783cd401204SKevin Cernekee 	ret = __alps_command_mode_write_reg(psmouse, reg_val);
178425bded7cSSeth Forshee 
1785cd401204SKevin Cernekee error:
1786cd401204SKevin Cernekee 	if (alps_exit_command_mode(psmouse))
1787cd401204SKevin Cernekee 		ret = -1;
1788cd401204SKevin Cernekee 	return ret;
178925bded7cSSeth Forshee }
179025bded7cSSeth Forshee 
179125bded7cSSeth Forshee /* Must be in command mode when calling this function */
179225bded7cSSeth Forshee static int alps_absolute_mode_v3(struct psmouse *psmouse)
179325bded7cSSeth Forshee {
179425bded7cSSeth Forshee 	int reg_val;
179525bded7cSSeth Forshee 
179625bded7cSSeth Forshee 	reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
179725bded7cSSeth Forshee 	if (reg_val == -1)
179825bded7cSSeth Forshee 		return -1;
179925bded7cSSeth Forshee 
180025bded7cSSeth Forshee 	reg_val |= 0x06;
180125bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, reg_val))
180225bded7cSSeth Forshee 		return -1;
180325bded7cSSeth Forshee 
180425bded7cSSeth Forshee 	return 0;
180525bded7cSSeth Forshee }
180625bded7cSSeth Forshee 
1807cd401204SKevin Cernekee static int alps_probe_trackstick_v3(struct psmouse *psmouse, int reg_base)
180825bded7cSSeth Forshee {
1809cd401204SKevin Cernekee 	int ret = -EIO, reg_val;
181025bded7cSSeth Forshee 
1811d18e53fcSKevin Cernekee 	if (alps_enter_command_mode(psmouse))
181225bded7cSSeth Forshee 		goto error;
181325bded7cSSeth Forshee 
1814cd401204SKevin Cernekee 	reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x08);
181525bded7cSSeth Forshee 	if (reg_val == -1)
181625bded7cSSeth Forshee 		goto error;
1817cd401204SKevin Cernekee 
1818cd401204SKevin Cernekee 	/* bit 7: trackstick is present */
1819cd401204SKevin Cernekee 	ret = reg_val & 0x80 ? 0 : -ENODEV;
1820cd401204SKevin Cernekee 
1821cd401204SKevin Cernekee error:
1822cd401204SKevin Cernekee 	alps_exit_command_mode(psmouse);
1823cd401204SKevin Cernekee 	return ret;
1824cd401204SKevin Cernekee }
1825cd401204SKevin Cernekee 
1826cd401204SKevin Cernekee static int alps_setup_trackstick_v3(struct psmouse *psmouse, int reg_base)
1827cd401204SKevin Cernekee {
1828cd401204SKevin Cernekee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
1829cd401204SKevin Cernekee 	int ret = 0;
1830cd401204SKevin Cernekee 	unsigned char param[4];
1831cd401204SKevin Cernekee 
1832cd401204SKevin Cernekee 	if (alps_passthrough_mode_v3(psmouse, reg_base, true))
1833cd401204SKevin Cernekee 		return -EIO;
183425bded7cSSeth Forshee 
183525bded7cSSeth Forshee 	/*
183625bded7cSSeth Forshee 	 * E7 report for the trackstick
183725bded7cSSeth Forshee 	 *
183825bded7cSSeth Forshee 	 * There have been reports of failures to seem to trace back
183925bded7cSSeth Forshee 	 * to the above trackstick check failing. When these occur
184025bded7cSSeth Forshee 	 * this E7 report fails, so when that happens we continue
184125bded7cSSeth Forshee 	 * with the assumption that there isn't a trackstick after
184225bded7cSSeth Forshee 	 * all.
184325bded7cSSeth Forshee 	 */
1844cd401204SKevin Cernekee 	if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_SETSCALE21, param)) {
1845a09221e8SDmitry Torokhov 		psmouse_warn(psmouse, "Failed to initialize trackstick (E7 report failed)\n");
1846cd401204SKevin Cernekee 		ret = -ENODEV;
184725bded7cSSeth Forshee 	} else {
184839fbe585SDmitry Torokhov 		psmouse_dbg(psmouse, "trackstick E7 report: %3ph\n", param);
184925bded7cSSeth Forshee 
185025bded7cSSeth Forshee 		/*
185125bded7cSSeth Forshee 		 * Not sure what this does, but it is absolutely
185225bded7cSSeth Forshee 		 * essential. Without it, the touchpad does not
185325bded7cSSeth Forshee 		 * work at all and the trackstick just emits normal
185425bded7cSSeth Forshee 		 * PS/2 packets.
185525bded7cSSeth Forshee 		 */
185625bded7cSSeth Forshee 		if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
185725bded7cSSeth Forshee 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
185825bded7cSSeth Forshee 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
185925bded7cSSeth Forshee 		    alps_command_mode_send_nibble(psmouse, 0x9) ||
186025bded7cSSeth Forshee 		    alps_command_mode_send_nibble(psmouse, 0x4)) {
186125bded7cSSeth Forshee 			psmouse_err(psmouse,
186225bded7cSSeth Forshee 				    "Error sending magic E6 sequence\n");
1863cd401204SKevin Cernekee 			ret = -EIO;
186425bded7cSSeth Forshee 			goto error;
186525bded7cSSeth Forshee 		}
186625bded7cSSeth Forshee 
1867cd401204SKevin Cernekee 		/*
1868cd401204SKevin Cernekee 		 * This ensures the trackstick packets are in the format
1869cd401204SKevin Cernekee 		 * supported by this driver. If bit 1 isn't set the packet
1870cd401204SKevin Cernekee 		 * format is different.
1871cd401204SKevin Cernekee 		 */
1872d18e53fcSKevin Cernekee 		if (alps_enter_command_mode(psmouse) ||
1873cd401204SKevin Cernekee 		    alps_command_mode_write_reg(psmouse,
1874cd401204SKevin Cernekee 						reg_base + 0x08, 0x82) ||
1875cd401204SKevin Cernekee 		    alps_exit_command_mode(psmouse))
1876cd401204SKevin Cernekee 			ret = -EIO;
1877cd401204SKevin Cernekee 	}
1878cd401204SKevin Cernekee 
1879cd401204SKevin Cernekee error:
1880cd401204SKevin Cernekee 	if (alps_passthrough_mode_v3(psmouse, reg_base, false))
1881cd401204SKevin Cernekee 		ret = -EIO;
1882cd401204SKevin Cernekee 
1883cd401204SKevin Cernekee 	return ret;
1884cd401204SKevin Cernekee }
1885cd401204SKevin Cernekee 
1886cd401204SKevin Cernekee static int alps_hw_init_v3(struct psmouse *psmouse)
1887cd401204SKevin Cernekee {
1888cd401204SKevin Cernekee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
1889cd401204SKevin Cernekee 	int reg_val;
1890cd401204SKevin Cernekee 	unsigned char param[4];
1891cd401204SKevin Cernekee 
1892cd401204SKevin Cernekee 	reg_val = alps_probe_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE);
1893cd401204SKevin Cernekee 	if (reg_val == -EIO)
1894cd401204SKevin Cernekee 		goto error;
189539fbe585SDmitry Torokhov 
1896cd401204SKevin Cernekee 	if (reg_val == 0 &&
1897cd401204SKevin Cernekee 	    alps_setup_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE) == -EIO)
1898cd401204SKevin Cernekee 		goto error;
1899cd401204SKevin Cernekee 
1900d18e53fcSKevin Cernekee 	if (alps_enter_command_mode(psmouse) ||
1901cd401204SKevin Cernekee 	    alps_absolute_mode_v3(psmouse)) {
190225bded7cSSeth Forshee 		psmouse_err(psmouse, "Failed to enter absolute mode\n");
190325bded7cSSeth Forshee 		goto error;
190425bded7cSSeth Forshee 	}
190525bded7cSSeth Forshee 
190625bded7cSSeth Forshee 	reg_val = alps_command_mode_read_reg(psmouse, 0x0006);
190725bded7cSSeth Forshee 	if (reg_val == -1)
190825bded7cSSeth Forshee 		goto error;
190925bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
191025bded7cSSeth Forshee 		goto error;
191125bded7cSSeth Forshee 
191225bded7cSSeth Forshee 	reg_val = alps_command_mode_read_reg(psmouse, 0x0007);
191325bded7cSSeth Forshee 	if (reg_val == -1)
191425bded7cSSeth Forshee 		goto error;
191525bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
191625bded7cSSeth Forshee 		goto error;
191725bded7cSSeth Forshee 
191825bded7cSSeth Forshee 	if (alps_command_mode_read_reg(psmouse, 0x0144) == -1)
191925bded7cSSeth Forshee 		goto error;
192025bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, 0x04))
192125bded7cSSeth Forshee 		goto error;
192225bded7cSSeth Forshee 
192325bded7cSSeth Forshee 	if (alps_command_mode_read_reg(psmouse, 0x0159) == -1)
192425bded7cSSeth Forshee 		goto error;
192525bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, 0x03))
192625bded7cSSeth Forshee 		goto error;
192725bded7cSSeth Forshee 
192825bded7cSSeth Forshee 	if (alps_command_mode_read_reg(psmouse, 0x0163) == -1)
192925bded7cSSeth Forshee 		goto error;
193025bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0163, 0x03))
193125bded7cSSeth Forshee 		goto error;
193225bded7cSSeth Forshee 
193325bded7cSSeth Forshee 	if (alps_command_mode_read_reg(psmouse, 0x0162) == -1)
193425bded7cSSeth Forshee 		goto error;
193525bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0162, 0x04))
193625bded7cSSeth Forshee 		goto error;
193725bded7cSSeth Forshee 
193825bded7cSSeth Forshee 	alps_exit_command_mode(psmouse);
193925bded7cSSeth Forshee 
194025bded7cSSeth Forshee 	/* Set rate and enable data reporting */
194125bded7cSSeth Forshee 	param[0] = 0x64;
194225bded7cSSeth Forshee 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
194325bded7cSSeth Forshee 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
194425bded7cSSeth Forshee 		psmouse_err(psmouse, "Failed to enable data reporting\n");
194525bded7cSSeth Forshee 		return -1;
194625bded7cSSeth Forshee 	}
194725bded7cSSeth Forshee 
194825bded7cSSeth Forshee 	return 0;
194925bded7cSSeth Forshee 
195025bded7cSSeth Forshee error:
195125bded7cSSeth Forshee 	/*
195225bded7cSSeth Forshee 	 * Leaving the touchpad in command mode will essentially render
195325bded7cSSeth Forshee 	 * it unusable until the machine reboots, so exit it here just
195425bded7cSSeth Forshee 	 * to be safe
195525bded7cSSeth Forshee 	 */
195625bded7cSSeth Forshee 	alps_exit_command_mode(psmouse);
195725bded7cSSeth Forshee 	return -1;
195825bded7cSSeth Forshee }
195925bded7cSSeth Forshee 
1960f3f33c67SHans de Goede static int alps_get_v3_v7_resolution(struct psmouse *psmouse, int reg_pitch)
1961f3f33c67SHans de Goede {
1962f3f33c67SHans de Goede 	int reg, x_pitch, y_pitch, x_electrode, y_electrode, x_phys, y_phys;
1963f3f33c67SHans de Goede 	struct alps_data *priv = psmouse->private;
1964f3f33c67SHans de Goede 
1965f3f33c67SHans de Goede 	reg = alps_command_mode_read_reg(psmouse, reg_pitch);
1966f3f33c67SHans de Goede 	if (reg < 0)
1967f3f33c67SHans de Goede 		return reg;
1968f3f33c67SHans de Goede 
1969f3f33c67SHans de Goede 	x_pitch = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
1970f3f33c67SHans de Goede 	x_pitch = 50 + 2 * x_pitch; /* In 0.1 mm units */
1971f3f33c67SHans de Goede 
1972f3f33c67SHans de Goede 	y_pitch = (char)reg >> 4; /* sign extend upper 4 bits */
1973f3f33c67SHans de Goede 	y_pitch = 36 + 2 * y_pitch; /* In 0.1 mm units */
1974f3f33c67SHans de Goede 
1975f3f33c67SHans de Goede 	reg = alps_command_mode_read_reg(psmouse, reg_pitch + 1);
1976f3f33c67SHans de Goede 	if (reg < 0)
1977f3f33c67SHans de Goede 		return reg;
1978f3f33c67SHans de Goede 
1979f3f33c67SHans de Goede 	x_electrode = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
1980f3f33c67SHans de Goede 	x_electrode = 17 + x_electrode;
1981f3f33c67SHans de Goede 
1982f3f33c67SHans de Goede 	y_electrode = (char)reg >> 4; /* sign extend upper 4 bits */
1983f3f33c67SHans de Goede 	y_electrode = 13 + y_electrode;
1984f3f33c67SHans de Goede 
1985f3f33c67SHans de Goede 	x_phys = x_pitch * (x_electrode - 1); /* In 0.1 mm units */
1986f3f33c67SHans de Goede 	y_phys = y_pitch * (y_electrode - 1); /* In 0.1 mm units */
1987f3f33c67SHans de Goede 
1988f3f33c67SHans de Goede 	priv->x_res = priv->x_max * 10 / x_phys; /* units / mm */
1989f3f33c67SHans de Goede 	priv->y_res = priv->y_max * 10 / y_phys; /* units / mm */
1990f3f33c67SHans de Goede 
1991f3f33c67SHans de Goede 	psmouse_dbg(psmouse,
1992f3f33c67SHans de Goede 		    "pitch %dx%d num-electrodes %dx%d physical size %dx%d mm res %dx%d\n",
1993f3f33c67SHans de Goede 		    x_pitch, y_pitch, x_electrode, y_electrode,
1994f3f33c67SHans de Goede 		    x_phys / 10, y_phys / 10, priv->x_res, priv->y_res);
1995f3f33c67SHans de Goede 
1996f3f33c67SHans de Goede 	return 0;
1997f3f33c67SHans de Goede }
1998f3f33c67SHans de Goede 
19991302bac3SKevin Cernekee static int alps_hw_init_rushmore_v3(struct psmouse *psmouse)
20001302bac3SKevin Cernekee {
2001cd401204SKevin Cernekee 	struct alps_data *priv = psmouse->private;
20021302bac3SKevin Cernekee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
20031302bac3SKevin Cernekee 	int reg_val, ret = -1;
20041302bac3SKevin Cernekee 
2005cd401204SKevin Cernekee 	if (priv->flags & ALPS_DUALPOINT) {
2006cd401204SKevin Cernekee 		reg_val = alps_setup_trackstick_v3(psmouse,
2007cd401204SKevin Cernekee 						   ALPS_REG_BASE_RUSHMORE);
2008cd401204SKevin Cernekee 		if (reg_val == -EIO)
2009cd401204SKevin Cernekee 			goto error;
2010cd401204SKevin Cernekee 	}
2011cd401204SKevin Cernekee 
2012d18e53fcSKevin Cernekee 	if (alps_enter_command_mode(psmouse) ||
20131302bac3SKevin Cernekee 	    alps_command_mode_read_reg(psmouse, 0xc2d9) == -1 ||
20141302bac3SKevin Cernekee 	    alps_command_mode_write_reg(psmouse, 0xc2cb, 0x00))
20151302bac3SKevin Cernekee 		goto error;
20161302bac3SKevin Cernekee 
2017f3f33c67SHans de Goede 	if (alps_get_v3_v7_resolution(psmouse, 0xc2da))
2018f3f33c67SHans de Goede 		goto error;
2019f3f33c67SHans de Goede 
20201302bac3SKevin Cernekee 	reg_val = alps_command_mode_read_reg(psmouse, 0xc2c6);
20211302bac3SKevin Cernekee 	if (reg_val == -1)
20221302bac3SKevin Cernekee 		goto error;
20231302bac3SKevin Cernekee 	if (__alps_command_mode_write_reg(psmouse, reg_val & 0xfd))
20241302bac3SKevin Cernekee 		goto error;
20251302bac3SKevin Cernekee 
20261302bac3SKevin Cernekee 	if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
20271302bac3SKevin Cernekee 		goto error;
20281302bac3SKevin Cernekee 
20291302bac3SKevin Cernekee 	/* enter absolute mode */
20301302bac3SKevin Cernekee 	reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
20311302bac3SKevin Cernekee 	if (reg_val == -1)
20321302bac3SKevin Cernekee 		goto error;
20331302bac3SKevin Cernekee 	if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
20341302bac3SKevin Cernekee 		goto error;
20351302bac3SKevin Cernekee 
20361302bac3SKevin Cernekee 	alps_exit_command_mode(psmouse);
20371302bac3SKevin Cernekee 	return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
20381302bac3SKevin Cernekee 
20391302bac3SKevin Cernekee error:
20401302bac3SKevin Cernekee 	alps_exit_command_mode(psmouse);
20411302bac3SKevin Cernekee 	return ret;
20421302bac3SKevin Cernekee }
20431302bac3SKevin Cernekee 
204425bded7cSSeth Forshee /* Must be in command mode when calling this function */
204525bded7cSSeth Forshee static int alps_absolute_mode_v4(struct psmouse *psmouse)
204625bded7cSSeth Forshee {
204725bded7cSSeth Forshee 	int reg_val;
204825bded7cSSeth Forshee 
204925bded7cSSeth Forshee 	reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
205025bded7cSSeth Forshee 	if (reg_val == -1)
205125bded7cSSeth Forshee 		return -1;
205225bded7cSSeth Forshee 
205325bded7cSSeth Forshee 	reg_val |= 0x02;
205425bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, reg_val))
205525bded7cSSeth Forshee 		return -1;
205625bded7cSSeth Forshee 
205725bded7cSSeth Forshee 	return 0;
205825bded7cSSeth Forshee }
205925bded7cSSeth Forshee 
206025bded7cSSeth Forshee static int alps_hw_init_v4(struct psmouse *psmouse)
206125bded7cSSeth Forshee {
206225bded7cSSeth Forshee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
206325bded7cSSeth Forshee 	unsigned char param[4];
206425bded7cSSeth Forshee 
2065d18e53fcSKevin Cernekee 	if (alps_enter_command_mode(psmouse))
206625bded7cSSeth Forshee 		goto error;
206725bded7cSSeth Forshee 
206825bded7cSSeth Forshee 	if (alps_absolute_mode_v4(psmouse)) {
206925bded7cSSeth Forshee 		psmouse_err(psmouse, "Failed to enter absolute mode\n");
207025bded7cSSeth Forshee 		goto error;
207125bded7cSSeth Forshee 	}
207225bded7cSSeth Forshee 
207325bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0007, 0x8c))
207425bded7cSSeth Forshee 		goto error;
207525bded7cSSeth Forshee 
207625bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0149, 0x03))
207725bded7cSSeth Forshee 		goto error;
207825bded7cSSeth Forshee 
207925bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0160, 0x03))
208025bded7cSSeth Forshee 		goto error;
208125bded7cSSeth Forshee 
208225bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x017f, 0x15))
208325bded7cSSeth Forshee 		goto error;
208425bded7cSSeth Forshee 
208525bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0151, 0x01))
208625bded7cSSeth Forshee 		goto error;
208725bded7cSSeth Forshee 
208825bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0168, 0x03))
208925bded7cSSeth Forshee 		goto error;
209025bded7cSSeth Forshee 
209125bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x014a, 0x03))
209225bded7cSSeth Forshee 		goto error;
209325bded7cSSeth Forshee 
209425bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0161, 0x03))
209525bded7cSSeth Forshee 		goto error;
209625bded7cSSeth Forshee 
209725bded7cSSeth Forshee 	alps_exit_command_mode(psmouse);
209825bded7cSSeth Forshee 
209925bded7cSSeth Forshee 	/*
210025bded7cSSeth Forshee 	 * This sequence changes the output from a 9-byte to an
210125bded7cSSeth Forshee 	 * 8-byte format. All the same data seems to be present,
210225bded7cSSeth Forshee 	 * just in a more compact format.
210325bded7cSSeth Forshee 	 */
210425bded7cSSeth Forshee 	param[0] = 0xc8;
210525bded7cSSeth Forshee 	param[1] = 0x64;
210625bded7cSSeth Forshee 	param[2] = 0x50;
210725bded7cSSeth Forshee 	if (ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
210825bded7cSSeth Forshee 	    ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE) ||
210925bded7cSSeth Forshee 	    ps2_command(ps2dev, &param[2], PSMOUSE_CMD_SETRATE) ||
211025bded7cSSeth Forshee 	    ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
211125bded7cSSeth Forshee 		return -1;
211225bded7cSSeth Forshee 
211325bded7cSSeth Forshee 	/* Set rate and enable data reporting */
211425bded7cSSeth Forshee 	param[0] = 0x64;
211525bded7cSSeth Forshee 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
211625bded7cSSeth Forshee 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
211725bded7cSSeth Forshee 		psmouse_err(psmouse, "Failed to enable data reporting\n");
211825bded7cSSeth Forshee 		return -1;
211925bded7cSSeth Forshee 	}
212025bded7cSSeth Forshee 
212125bded7cSSeth Forshee 	return 0;
212225bded7cSSeth Forshee 
212325bded7cSSeth Forshee error:
212425bded7cSSeth Forshee 	/*
212525bded7cSSeth Forshee 	 * Leaving the touchpad in command mode will essentially render
212625bded7cSSeth Forshee 	 * it unusable until the machine reboots, so exit it here just
212725bded7cSSeth Forshee 	 * to be safe
212825bded7cSSeth Forshee 	 */
212925bded7cSSeth Forshee 	alps_exit_command_mode(psmouse);
213025bded7cSSeth Forshee 	return -1;
213125bded7cSSeth Forshee }
213225bded7cSSeth Forshee 
2133ee65d4b3SYunkang Tang static int alps_dolphin_get_device_area(struct psmouse *psmouse,
2134ee65d4b3SYunkang Tang 					struct alps_data *priv)
2135ee65d4b3SYunkang Tang {
2136ee65d4b3SYunkang Tang 	struct ps2dev *ps2dev = &psmouse->ps2dev;
2137ee65d4b3SYunkang Tang 	unsigned char param[4] = {0};
2138ee65d4b3SYunkang Tang 	int num_x_electrode, num_y_electrode;
2139ee65d4b3SYunkang Tang 
2140ee65d4b3SYunkang Tang 	if (alps_enter_command_mode(psmouse))
2141ee65d4b3SYunkang Tang 		return -1;
2142ee65d4b3SYunkang Tang 
2143ee65d4b3SYunkang Tang 	param[0] = 0x0a;
2144ee65d4b3SYunkang Tang 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
2145ee65d4b3SYunkang Tang 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2146ee65d4b3SYunkang Tang 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2147ee65d4b3SYunkang Tang 	    ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2148ee65d4b3SYunkang Tang 	    ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE))
2149ee65d4b3SYunkang Tang 		return -1;
2150ee65d4b3SYunkang Tang 
2151ee65d4b3SYunkang Tang 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
2152ee65d4b3SYunkang Tang 		return -1;
2153ee65d4b3SYunkang Tang 
2154ee65d4b3SYunkang Tang 	/*
2155ee65d4b3SYunkang Tang 	 * Dolphin's sensor line number is not fixed. It can be calculated
2156ee65d4b3SYunkang Tang 	 * by adding the device's register value with DOLPHIN_PROFILE_X/YOFFSET.
2157ee65d4b3SYunkang Tang 	 * Further more, we can get device's x_max and y_max by multiplying
2158ee65d4b3SYunkang Tang 	 * sensor line number with DOLPHIN_COUNT_PER_ELECTRODE.
2159ee65d4b3SYunkang Tang 	 *
2160ee65d4b3SYunkang Tang 	 * e.g. When we get register's sensor_x = 11 & sensor_y = 8,
2161ee65d4b3SYunkang Tang 	 *	real sensor line number X = 11 + 8 = 19, and
2162ee65d4b3SYunkang Tang 	 *	real sensor line number Y = 8 + 1 = 9.
2163ee65d4b3SYunkang Tang 	 *	So, x_max = (19 - 1) * 64 = 1152, and
2164ee65d4b3SYunkang Tang 	 *	    y_max = (9 - 1) * 64 = 512.
2165ee65d4b3SYunkang Tang 	 */
2166ee65d4b3SYunkang Tang 	num_x_electrode = DOLPHIN_PROFILE_XOFFSET + (param[2] & 0x0F);
2167ee65d4b3SYunkang Tang 	num_y_electrode = DOLPHIN_PROFILE_YOFFSET + ((param[2] >> 4) & 0x0F);
2168ee65d4b3SYunkang Tang 	priv->x_bits = num_x_electrode;
2169ee65d4b3SYunkang Tang 	priv->y_bits = num_y_electrode;
2170ee65d4b3SYunkang Tang 	priv->x_max = (num_x_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
2171ee65d4b3SYunkang Tang 	priv->y_max = (num_y_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
2172ee65d4b3SYunkang Tang 
2173ee65d4b3SYunkang Tang 	if (alps_exit_command_mode(psmouse))
2174ee65d4b3SYunkang Tang 		return -1;
2175ee65d4b3SYunkang Tang 
2176ee65d4b3SYunkang Tang 	return 0;
2177ee65d4b3SYunkang Tang }
2178ee65d4b3SYunkang Tang 
217975af9e56SDave Turvene static int alps_hw_init_dolphin_v1(struct psmouse *psmouse)
218075af9e56SDave Turvene {
218175af9e56SDave Turvene 	struct ps2dev *ps2dev = &psmouse->ps2dev;
218275af9e56SDave Turvene 	unsigned char param[2];
218375af9e56SDave Turvene 
218475af9e56SDave Turvene 	/* This is dolphin "v1" as empirically defined by florin9doi */
218575af9e56SDave Turvene 	param[0] = 0x64;
218675af9e56SDave Turvene 	param[1] = 0x28;
218775af9e56SDave Turvene 
218875af9e56SDave Turvene 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
218975af9e56SDave Turvene 	    ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
219075af9e56SDave Turvene 	    ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
219175af9e56SDave Turvene 		return -1;
219275af9e56SDave Turvene 
219375af9e56SDave Turvene 	return 0;
219475af9e56SDave Turvene }
219575af9e56SDave Turvene 
21963808843cSYunkang Tang static int alps_hw_init_v7(struct psmouse *psmouse)
21973808843cSYunkang Tang {
21983808843cSYunkang Tang 	struct ps2dev *ps2dev = &psmouse->ps2dev;
21993808843cSYunkang Tang 	int reg_val, ret = -1;
22003808843cSYunkang Tang 
22013808843cSYunkang Tang 	if (alps_enter_command_mode(psmouse) ||
22023808843cSYunkang Tang 	    alps_command_mode_read_reg(psmouse, 0xc2d9) == -1)
22033808843cSYunkang Tang 		goto error;
22043808843cSYunkang Tang 
2205f3f33c67SHans de Goede 	if (alps_get_v3_v7_resolution(psmouse, 0xc397))
2206f3f33c67SHans de Goede 		goto error;
2207f3f33c67SHans de Goede 
22083808843cSYunkang Tang 	if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
22093808843cSYunkang Tang 		goto error;
22103808843cSYunkang Tang 
22113808843cSYunkang Tang 	reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
22123808843cSYunkang Tang 	if (reg_val == -1)
22133808843cSYunkang Tang 		goto error;
22143808843cSYunkang Tang 	if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
22153808843cSYunkang Tang 		goto error;
22163808843cSYunkang Tang 
22173808843cSYunkang Tang 	alps_exit_command_mode(psmouse);
22183808843cSYunkang Tang 	return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
22193808843cSYunkang Tang 
22203808843cSYunkang Tang error:
22213808843cSYunkang Tang 	alps_exit_command_mode(psmouse);
22223808843cSYunkang Tang 	return ret;
22233808843cSYunkang Tang }
22243808843cSYunkang Tang 
22253296f71cSDmitry Torokhov static int alps_set_protocol(struct psmouse *psmouse,
22263296f71cSDmitry Torokhov 			     struct alps_data *priv,
22273296f71cSDmitry Torokhov 			     const struct alps_protocol_info *protocol)
222825bded7cSSeth Forshee {
22293296f71cSDmitry Torokhov 	psmouse->private = priv;
22303296f71cSDmitry Torokhov 
22313296f71cSDmitry Torokhov 	setup_timer(&priv->timer, alps_flush_packet, (unsigned long)psmouse);
22323296f71cSDmitry Torokhov 
22333296f71cSDmitry Torokhov 	priv->proto_version = protocol->version;
22343296f71cSDmitry Torokhov 	priv->byte0 = protocol->byte0;
22353296f71cSDmitry Torokhov 	priv->mask0 = protocol->mask0;
22363296f71cSDmitry Torokhov 	priv->flags = protocol->flags;
2237f673ceb1SKevin Cernekee 
22387a9f73e7SKevin Cernekee 	priv->x_max = 2000;
22397a9f73e7SKevin Cernekee 	priv->y_max = 1400;
22407a9f73e7SKevin Cernekee 	priv->x_bits = 15;
22417a9f73e7SKevin Cernekee 	priv->y_bits = 11;
22427a9f73e7SKevin Cernekee 
224399df65e7SKevin Cernekee 	switch (priv->proto_version) {
224425bded7cSSeth Forshee 	case ALPS_PROTO_V1:
224525bded7cSSeth Forshee 	case ALPS_PROTO_V2:
224624af5cb9SKevin Cernekee 		priv->hw_init = alps_hw_init_v1_v2;
224724af5cb9SKevin Cernekee 		priv->process_packet = alps_process_packet_v1_v2;
224824af5cb9SKevin Cernekee 		priv->set_abs_params = alps_set_abs_params_st;
224995f75e91SYunkang Tang 		priv->x_max = 1023;
225095f75e91SYunkang Tang 		priv->y_max = 767;
225125bded7cSSeth Forshee 		break;
2252fb2dd7a6SDmitry Torokhov 
225325bded7cSSeth Forshee 	case ALPS_PROTO_V3:
225424af5cb9SKevin Cernekee 		priv->hw_init = alps_hw_init_v3;
225524af5cb9SKevin Cernekee 		priv->process_packet = alps_process_packet_v3;
225624af5cb9SKevin Cernekee 		priv->set_abs_params = alps_set_abs_params_mt;
2257f85e5001SKevin Cernekee 		priv->decode_fields = alps_decode_pinnacle;
225850e8b216SKevin Cernekee 		priv->nibble_commands = alps_v3_nibble_commands;
225950e8b216SKevin Cernekee 		priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
226025bded7cSSeth Forshee 		break;
2261fb2dd7a6SDmitry Torokhov 
2262fb2dd7a6SDmitry Torokhov 	case ALPS_PROTO_V3_RUSHMORE:
2263fb2dd7a6SDmitry Torokhov 		priv->hw_init = alps_hw_init_rushmore_v3;
2264fb2dd7a6SDmitry Torokhov 		priv->process_packet = alps_process_packet_v3;
2265fb2dd7a6SDmitry Torokhov 		priv->set_abs_params = alps_set_abs_params_mt;
2266fb2dd7a6SDmitry Torokhov 		priv->decode_fields = alps_decode_rushmore;
2267fb2dd7a6SDmitry Torokhov 		priv->nibble_commands = alps_v3_nibble_commands;
2268fb2dd7a6SDmitry Torokhov 		priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2269fb2dd7a6SDmitry Torokhov 		priv->x_bits = 16;
2270fb2dd7a6SDmitry Torokhov 		priv->y_bits = 12;
22713296f71cSDmitry Torokhov 
22723296f71cSDmitry Torokhov 		if (alps_probe_trackstick_v3(psmouse,
22733296f71cSDmitry Torokhov 					     ALPS_REG_BASE_RUSHMORE) < 0)
22743296f71cSDmitry Torokhov 			priv->flags &= ~ALPS_DUALPOINT;
22753296f71cSDmitry Torokhov 
2276fb2dd7a6SDmitry Torokhov 		break;
2277fb2dd7a6SDmitry Torokhov 
227825bded7cSSeth Forshee 	case ALPS_PROTO_V4:
227924af5cb9SKevin Cernekee 		priv->hw_init = alps_hw_init_v4;
228024af5cb9SKevin Cernekee 		priv->process_packet = alps_process_packet_v4;
228124af5cb9SKevin Cernekee 		priv->set_abs_params = alps_set_abs_params_mt;
228250e8b216SKevin Cernekee 		priv->nibble_commands = alps_v4_nibble_commands;
228350e8b216SKevin Cernekee 		priv->addr_command = PSMOUSE_CMD_DISABLE;
228425bded7cSSeth Forshee 		break;
22853296f71cSDmitry Torokhov 
228675af9e56SDave Turvene 	case ALPS_PROTO_V5:
228775af9e56SDave Turvene 		priv->hw_init = alps_hw_init_dolphin_v1;
2288ee65d4b3SYunkang Tang 		priv->process_packet = alps_process_touchpad_packet_v3_v5;
228975af9e56SDave Turvene 		priv->decode_fields = alps_decode_dolphin;
229075af9e56SDave Turvene 		priv->set_abs_params = alps_set_abs_params_mt;
229175af9e56SDave Turvene 		priv->nibble_commands = alps_v3_nibble_commands;
229275af9e56SDave Turvene 		priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
229375af9e56SDave Turvene 		priv->x_bits = 23;
229475af9e56SDave Turvene 		priv->y_bits = 12;
2295c164c147SDmitry Torokhov 
2296c164c147SDmitry Torokhov 		if (alps_dolphin_get_device_area(psmouse, priv))
2297c164c147SDmitry Torokhov 			return -EIO;
2298c164c147SDmitry Torokhov 
229975af9e56SDave Turvene 		break;
23003296f71cSDmitry Torokhov 
230195f75e91SYunkang Tang 	case ALPS_PROTO_V6:
230295f75e91SYunkang Tang 		priv->hw_init = alps_hw_init_v6;
230395f75e91SYunkang Tang 		priv->process_packet = alps_process_packet_v6;
230495f75e91SYunkang Tang 		priv->set_abs_params = alps_set_abs_params_st;
230595f75e91SYunkang Tang 		priv->nibble_commands = alps_v6_nibble_commands;
230695f75e91SYunkang Tang 		priv->x_max = 2047;
230795f75e91SYunkang Tang 		priv->y_max = 1535;
230895f75e91SYunkang Tang 		break;
23093296f71cSDmitry Torokhov 
23103808843cSYunkang Tang 	case ALPS_PROTO_V7:
23113808843cSYunkang Tang 		priv->hw_init = alps_hw_init_v7;
23123808843cSYunkang Tang 		priv->process_packet = alps_process_packet_v7;
23133808843cSYunkang Tang 		priv->decode_fields = alps_decode_packet_v7;
23143808843cSYunkang Tang 		priv->set_abs_params = alps_set_abs_params_mt;
23153808843cSYunkang Tang 		priv->nibble_commands = alps_v3_nibble_commands;
23163808843cSYunkang Tang 		priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2317c164c147SDmitry Torokhov 		priv->x_max = 0xfff;
2318c164c147SDmitry Torokhov 		priv->y_max = 0x7ff;
23193808843cSYunkang Tang 
23203808843cSYunkang Tang 		if (priv->fw_ver[1] != 0xba)
23213808843cSYunkang Tang 			priv->flags |= ALPS_BUTTONPAD;
23223296f71cSDmitry Torokhov 
23233808843cSYunkang Tang 		break;
232425bded7cSSeth Forshee 	}
23253296f71cSDmitry Torokhov 
23263296f71cSDmitry Torokhov 	return 0;
232725bded7cSSeth Forshee }
232825bded7cSSeth Forshee 
23293296f71cSDmitry Torokhov static const struct alps_protocol_info *alps_match_table(unsigned char *e7,
23303296f71cSDmitry Torokhov 							 unsigned char *ec)
23312e992cc0SKevin Cernekee {
2332b5d6b851SKevin Cernekee 	const struct alps_model_info *model;
23332e992cc0SKevin Cernekee 	int i;
23342e992cc0SKevin Cernekee 
2335b5d6b851SKevin Cernekee 	for (i = 0; i < ARRAY_SIZE(alps_model_data); i++) {
2336b5d6b851SKevin Cernekee 		model = &alps_model_data[i];
2337b5d6b851SKevin Cernekee 
2338b5d6b851SKevin Cernekee 		if (!memcmp(e7, model->signature, sizeof(model->signature)) &&
2339b5d6b851SKevin Cernekee 		    (!model->command_mode_resp ||
2340b5d6b851SKevin Cernekee 		     model->command_mode_resp == ec[2])) {
2341b5d6b851SKevin Cernekee 
23423296f71cSDmitry Torokhov 			return &model->protocol_info;
2343b5d6b851SKevin Cernekee 		}
2344b5d6b851SKevin Cernekee 	}
2345b5d6b851SKevin Cernekee 
23463296f71cSDmitry Torokhov 	return NULL;
2347b5d6b851SKevin Cernekee }
2348b5d6b851SKevin Cernekee 
2349b5d6b851SKevin Cernekee static int alps_identify(struct psmouse *psmouse, struct alps_data *priv)
2350b5d6b851SKevin Cernekee {
23513296f71cSDmitry Torokhov 	const struct alps_protocol_info *protocol;
2352b5d6b851SKevin Cernekee 	unsigned char e6[4], e7[4], ec[4];
2353a09221e8SDmitry Torokhov 	int error;
2354b5d6b851SKevin Cernekee 
23552e992cc0SKevin Cernekee 	/*
23562e992cc0SKevin Cernekee 	 * First try "E6 report".
23572e992cc0SKevin Cernekee 	 * ALPS should return 0,0,10 or 0,0,100 if no buttons are pressed.
23582e992cc0SKevin Cernekee 	 * The bits 0-2 of the first byte will be 1s if some buttons are
23592e992cc0SKevin Cernekee 	 * pressed.
23602e992cc0SKevin Cernekee 	 */
2361b5d6b851SKevin Cernekee 	if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2362b5d6b851SKevin Cernekee 			 PSMOUSE_CMD_SETSCALE11, e6))
2363b5d6b851SKevin Cernekee 		return -EIO;
23642e992cc0SKevin Cernekee 
2365b5d6b851SKevin Cernekee 	if ((e6[0] & 0xf8) != 0 || e6[1] != 0 || (e6[2] != 10 && e6[2] != 100))
2366b5d6b851SKevin Cernekee 		return -EINVAL;
23672e992cc0SKevin Cernekee 
23682e992cc0SKevin Cernekee 	/*
2369b5d6b851SKevin Cernekee 	 * Now get the "E7" and "EC" reports.  These will uniquely identify
2370b5d6b851SKevin Cernekee 	 * most ALPS touchpads.
23712e992cc0SKevin Cernekee 	 */
2372b5d6b851SKevin Cernekee 	if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2373b5d6b851SKevin Cernekee 			 PSMOUSE_CMD_SETSCALE21, e7) ||
2374b5d6b851SKevin Cernekee 	    alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2375b5d6b851SKevin Cernekee 			 PSMOUSE_CMD_RESET_WRAP, ec) ||
2376b5d6b851SKevin Cernekee 	    alps_exit_command_mode(psmouse))
2377b5d6b851SKevin Cernekee 		return -EIO;
23782e992cc0SKevin Cernekee 
23793296f71cSDmitry Torokhov 	protocol = alps_match_table(e7, ec);
23803296f71cSDmitry Torokhov 	if (!protocol) {
23813296f71cSDmitry Torokhov 		if (e7[0] == 0x73 && e7[1] == 0x03 && e7[2] == 0x50 &&
23823296f71cSDmitry Torokhov 			   ec[0] == 0x73 && (ec[1] == 0x01 || ec[1] == 0x02)) {
23833296f71cSDmitry Torokhov 			protocol = &alps_v5_protocol_data;
23843296f71cSDmitry Torokhov 		} else if (ec[0] == 0x88 &&
23853296f71cSDmitry Torokhov 			   ((ec[1] & 0xf0) == 0xb0 || (ec[1] & 0xf0) == 0xc0)) {
23863296f71cSDmitry Torokhov 			protocol = &alps_v7_protocol_data;
23873296f71cSDmitry Torokhov 		} else if (ec[0] == 0x88 && ec[1] == 0x08) {
23883296f71cSDmitry Torokhov 			protocol = &alps_v3_rushmore_data;
23893296f71cSDmitry Torokhov 		} else if (ec[0] == 0x88 && ec[1] == 0x07 &&
23903296f71cSDmitry Torokhov 			   ec[2] >= 0x90 && ec[2] <= 0x9d) {
23913296f71cSDmitry Torokhov 			protocol = &alps_v3_protocol_data;
23923296f71cSDmitry Torokhov 		} else {
23933296f71cSDmitry Torokhov 			psmouse_dbg(psmouse,
23943296f71cSDmitry Torokhov 				    "Likely not an ALPS touchpad: E7=%3ph, EC=%3ph\n", e7, ec);
23953296f71cSDmitry Torokhov 			return -EINVAL;
23963296f71cSDmitry Torokhov 		}
23973296f71cSDmitry Torokhov 	}
23983296f71cSDmitry Torokhov 
2399a09221e8SDmitry Torokhov 	if (priv) {
2400c0cd17f6SHans de Goede 		/* Save the Firmware version */
2401c0cd17f6SHans de Goede 		memcpy(priv->fw_ver, ec, 3);
2402a09221e8SDmitry Torokhov 		error = alps_set_protocol(psmouse, priv, protocol);
2403a09221e8SDmitry Torokhov 		if (error)
2404a09221e8SDmitry Torokhov 			return error;
2405a09221e8SDmitry Torokhov 	}
2406c0cd17f6SHans de Goede 
2407a09221e8SDmitry Torokhov 	return 0;
24082e992cc0SKevin Cernekee }
24092e992cc0SKevin Cernekee 
24101e0c5b12SDmitry Torokhov static int alps_reconnect(struct psmouse *psmouse)
24111e0c5b12SDmitry Torokhov {
2412b5d6b851SKevin Cernekee 	struct alps_data *priv = psmouse->private;
241371bb21b6SMaxim Levitsky 
24141e0c5b12SDmitry Torokhov 	psmouse_reset(psmouse);
24151e0c5b12SDmitry Torokhov 
2416b5d6b851SKevin Cernekee 	if (alps_identify(psmouse, priv) < 0)
24171e0c5b12SDmitry Torokhov 		return -1;
24181e0c5b12SDmitry Torokhov 
241924af5cb9SKevin Cernekee 	return priv->hw_init(psmouse);
24201da177e4SLinus Torvalds }
24211da177e4SLinus Torvalds 
24221da177e4SLinus Torvalds static void alps_disconnect(struct psmouse *psmouse)
24231da177e4SLinus Torvalds {
24241da177e4SLinus Torvalds 	struct alps_data *priv = psmouse->private;
24252e5b636bSDmitry Torokhov 
24261da177e4SLinus Torvalds 	psmouse_reset(psmouse);
24271d9f2626SSebastian Kapfer 	del_timer_sync(&priv->timer);
242804aae283SPali Rohár 	if (priv->dev2)
24292e5b636bSDmitry Torokhov 		input_unregister_device(priv->dev2);
243004aae283SPali Rohár 	if (!IS_ERR_OR_NULL(priv->dev3))
243104aae283SPali Rohár 		input_unregister_device(priv->dev3);
24321da177e4SLinus Torvalds 	kfree(priv);
24331da177e4SLinus Torvalds }
24341da177e4SLinus Torvalds 
243524af5cb9SKevin Cernekee static void alps_set_abs_params_st(struct alps_data *priv,
243624af5cb9SKevin Cernekee 				   struct input_dev *dev1)
243724af5cb9SKevin Cernekee {
243895f75e91SYunkang Tang 	input_set_abs_params(dev1, ABS_X, 0, priv->x_max, 0, 0);
243995f75e91SYunkang Tang 	input_set_abs_params(dev1, ABS_Y, 0, priv->y_max, 0, 0);
244024af5cb9SKevin Cernekee }
244124af5cb9SKevin Cernekee 
244224af5cb9SKevin Cernekee static void alps_set_abs_params_mt(struct alps_data *priv,
244324af5cb9SKevin Cernekee 				   struct input_dev *dev1)
244424af5cb9SKevin Cernekee {
24457a9f73e7SKevin Cernekee 	input_set_abs_params(dev1, ABS_MT_POSITION_X, 0, priv->x_max, 0, 0);
24467a9f73e7SKevin Cernekee 	input_set_abs_params(dev1, ABS_MT_POSITION_Y, 0, priv->y_max, 0, 0);
244724af5cb9SKevin Cernekee 
2448f3f33c67SHans de Goede 	input_abs_set_res(dev1, ABS_MT_POSITION_X, priv->x_res);
2449f3f33c67SHans de Goede 	input_abs_set_res(dev1, ABS_MT_POSITION_Y, priv->y_res);
2450f3f33c67SHans de Goede 
2451cdf333efSHans de Goede 	input_mt_init_slots(dev1, MAX_TOUCHES, INPUT_MT_POINTER |
2452cdf333efSHans de Goede 		INPUT_MT_DROP_UNUSED | INPUT_MT_TRACK | INPUT_MT_SEMI_MT);
2453cdf333efSHans de Goede 
245424af5cb9SKevin Cernekee 	set_bit(BTN_TOOL_TRIPLETAP, dev1->keybit);
245524af5cb9SKevin Cernekee 	set_bit(BTN_TOOL_QUADTAP, dev1->keybit);
24563808843cSYunkang Tang 
24573808843cSYunkang Tang 	/* V7 is real multi-touch */
24583808843cSYunkang Tang 	if (priv->proto_version == ALPS_PROTO_V7)
24593808843cSYunkang Tang 		clear_bit(INPUT_PROP_SEMI_MT, dev1->propbit);
246024af5cb9SKevin Cernekee }
246124af5cb9SKevin Cernekee 
24621da177e4SLinus Torvalds int alps_init(struct psmouse *psmouse)
24631da177e4SLinus Torvalds {
2464a09221e8SDmitry Torokhov 	struct alps_data *priv = psmouse->private;
246504aae283SPali Rohár 	struct input_dev *dev1 = psmouse->dev;
2466a09221e8SDmitry Torokhov 	int error;
24671da177e4SLinus Torvalds 
2468a09221e8SDmitry Torokhov 	error = priv->hw_init(psmouse);
2469a09221e8SDmitry Torokhov 	if (error)
24701da177e4SLinus Torvalds 		goto init_fail;
24711da177e4SLinus Torvalds 
24727105d2eaSDmitry Torokhov 	/*
24737105d2eaSDmitry Torokhov 	 * Undo part of setup done for us by psmouse core since touchpad
24747105d2eaSDmitry Torokhov 	 * is not a relative device.
24757105d2eaSDmitry Torokhov 	 */
24767105d2eaSDmitry Torokhov 	__clear_bit(EV_REL, dev1->evbit);
24777105d2eaSDmitry Torokhov 	__clear_bit(REL_X, dev1->relbit);
24787105d2eaSDmitry Torokhov 	__clear_bit(REL_Y, dev1->relbit);
24797105d2eaSDmitry Torokhov 
24807105d2eaSDmitry Torokhov 	/*
24817105d2eaSDmitry Torokhov 	 * Now set up our capabilities.
24827105d2eaSDmitry Torokhov 	 */
24837b19ada2SJiri Slaby 	dev1->evbit[BIT_WORD(EV_KEY)] |= BIT_MASK(EV_KEY);
24847b19ada2SJiri Slaby 	dev1->keybit[BIT_WORD(BTN_TOUCH)] |= BIT_MASK(BTN_TOUCH);
24857b19ada2SJiri Slaby 	dev1->keybit[BIT_WORD(BTN_TOOL_FINGER)] |= BIT_MASK(BTN_TOOL_FINGER);
248671bb21b6SMaxim Levitsky 	dev1->keybit[BIT_WORD(BTN_LEFT)] |=
248771bb21b6SMaxim Levitsky 		BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
24881da177e4SLinus Torvalds 
24897b19ada2SJiri Slaby 	dev1->evbit[BIT_WORD(EV_ABS)] |= BIT_MASK(EV_ABS);
249025bded7cSSeth Forshee 
249124af5cb9SKevin Cernekee 	priv->set_abs_params(priv, dev1);
24923808843cSYunkang Tang 	/* No pressure on V7 */
24933808843cSYunkang Tang 	if (priv->proto_version != ALPS_PROTO_V7)
24942e5b636bSDmitry Torokhov 		input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
24951da177e4SLinus Torvalds 
249699df65e7SKevin Cernekee 	if (priv->flags & ALPS_WHEEL) {
24977b19ada2SJiri Slaby 		dev1->evbit[BIT_WORD(EV_REL)] |= BIT_MASK(EV_REL);
24987b19ada2SJiri Slaby 		dev1->relbit[BIT_WORD(REL_WHEEL)] |= BIT_MASK(REL_WHEEL);
24991da177e4SLinus Torvalds 	}
25001da177e4SLinus Torvalds 
250199df65e7SKevin Cernekee 	if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
25027b19ada2SJiri Slaby 		dev1->keybit[BIT_WORD(BTN_FORWARD)] |= BIT_MASK(BTN_FORWARD);
25037b19ada2SJiri Slaby 		dev1->keybit[BIT_WORD(BTN_BACK)] |= BIT_MASK(BTN_BACK);
25041da177e4SLinus Torvalds 	}
25051da177e4SLinus Torvalds 
250699df65e7SKevin Cernekee 	if (priv->flags & ALPS_FOUR_BUTTONS) {
250771bb21b6SMaxim Levitsky 		dev1->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_0);
250871bb21b6SMaxim Levitsky 		dev1->keybit[BIT_WORD(BTN_1)] |= BIT_MASK(BTN_1);
250971bb21b6SMaxim Levitsky 		dev1->keybit[BIT_WORD(BTN_2)] |= BIT_MASK(BTN_2);
251071bb21b6SMaxim Levitsky 		dev1->keybit[BIT_WORD(BTN_3)] |= BIT_MASK(BTN_3);
25113808843cSYunkang Tang 	} else if (priv->flags & ALPS_BUTTONPAD) {
25123808843cSYunkang Tang 		set_bit(INPUT_PROP_BUTTONPAD, dev1->propbit);
25133808843cSYunkang Tang 		clear_bit(BTN_RIGHT, dev1->keybit);
251471bb21b6SMaxim Levitsky 	} else {
251571bb21b6SMaxim Levitsky 		dev1->keybit[BIT_WORD(BTN_MIDDLE)] |= BIT_MASK(BTN_MIDDLE);
251671bb21b6SMaxim Levitsky 	}
251771bb21b6SMaxim Levitsky 
2518dfba8600SPali Rohár 	if (priv->flags & ALPS_DUALPOINT) {
251904aae283SPali Rohár 		struct input_dev *dev2;
252004aae283SPali Rohár 
252104aae283SPali Rohár 		dev2 = input_allocate_device();
252204aae283SPali Rohár 		if (!dev2) {
252304aae283SPali Rohár 			psmouse_err(psmouse,
252404aae283SPali Rohár 				    "failed to allocate trackstick device\n");
252504aae283SPali Rohár 			error = -ENOMEM;
252604aae283SPali Rohár 			goto init_fail;
252704aae283SPali Rohár 		}
252804aae283SPali Rohár 
252904aae283SPali Rohár 		snprintf(priv->phys2, sizeof(priv->phys2), "%s/input1",
253004aae283SPali Rohár 			 psmouse->ps2dev.serio->phys);
253104aae283SPali Rohár 		dev2->phys = priv->phys2;
253204aae283SPali Rohár 
2533dfba8600SPali Rohár 		/*
2534dfba8600SPali Rohár 		 * format of input device name is: "protocol vendor name"
2535dfba8600SPali Rohár 		 * see function psmouse_switch_protocol() in psmouse-base.c
2536dfba8600SPali Rohár 		 */
2537dfba8600SPali Rohár 		dev2->name = "AlpsPS/2 ALPS DualPoint Stick";
2538dfba8600SPali Rohár 
25392e5b636bSDmitry Torokhov 		dev2->id.bustype = BUS_I8042;
25402e5b636bSDmitry Torokhov 		dev2->id.vendor  = 0x0002;
254104aae283SPali Rohár 		dev2->id.product = PSMOUSE_ALPS;
254204aae283SPali Rohár 		dev2->id.version = priv->proto_version;
25431db3a345SDmitry Torokhov 		dev2->dev.parent = &psmouse->ps2dev.serio->dev;
25441da177e4SLinus Torvalds 
254504aae283SPali Rohár 		input_set_capability(dev2, EV_REL, REL_X);
254604aae283SPali Rohár 		input_set_capability(dev2, EV_REL, REL_Y);
254704aae283SPali Rohár 		input_set_capability(dev2, EV_KEY, BTN_LEFT);
254804aae283SPali Rohár 		input_set_capability(dev2, EV_KEY, BTN_RIGHT);
254904aae283SPali Rohár 		input_set_capability(dev2, EV_KEY, BTN_MIDDLE);
25501da177e4SLinus Torvalds 
255101d4cd5cSHans de Goede 		__set_bit(INPUT_PROP_POINTER, dev2->propbit);
25527611392fSHans de Goede 		__set_bit(INPUT_PROP_POINTING_STICK, dev2->propbit);
25537611392fSHans de Goede 
255404aae283SPali Rohár 		error = input_register_device(dev2);
255504aae283SPali Rohár 		if (error) {
255604aae283SPali Rohár 			psmouse_err(psmouse,
255704aae283SPali Rohár 				    "failed to register trackstick device: %d\n",
255804aae283SPali Rohár 				    error);
255904aae283SPali Rohár 			input_free_device(dev2);
2560f42649e8SDmitry Torokhov 			goto init_fail;
256104aae283SPali Rohár 		}
256204aae283SPali Rohár 
256304aae283SPali Rohár 		priv->dev2 = dev2;
256404aae283SPali Rohár 	}
256504aae283SPali Rohár 
256604aae283SPali Rohár 	priv->psmouse = psmouse;
256704aae283SPali Rohár 
256804aae283SPali Rohár 	INIT_DELAYED_WORK(&priv->dev3_register_work,
256904aae283SPali Rohár 			  alps_register_bare_ps2_mouse);
25701da177e4SLinus Torvalds 
25711da177e4SLinus Torvalds 	psmouse->protocol_handler = alps_process_byte;
2572f0d5c6f4SDmitry Torokhov 	psmouse->poll = alps_poll;
25731da177e4SLinus Torvalds 	psmouse->disconnect = alps_disconnect;
25741da177e4SLinus Torvalds 	psmouse->reconnect = alps_reconnect;
257599df65e7SKevin Cernekee 	psmouse->pktsize = priv->proto_version == ALPS_PROTO_V4 ? 8 : 6;
25761da177e4SLinus Torvalds 
2577f0d5c6f4SDmitry Torokhov 	/* We are having trouble resyncing ALPS touchpads so disable it for now */
2578f0d5c6f4SDmitry Torokhov 	psmouse->resync_time = 0;
2579f0d5c6f4SDmitry Torokhov 
25809d720b34SPali Rohár 	/* Allow 2 invalid packets without resetting device */
25819d720b34SPali Rohár 	psmouse->resetafter = psmouse->pktsize * 2;
25829d720b34SPali Rohár 
25831da177e4SLinus Torvalds 	return 0;
25841da177e4SLinus Torvalds 
25851da177e4SLinus Torvalds init_fail:
2586f42649e8SDmitry Torokhov 	psmouse_reset(psmouse);
2587a09221e8SDmitry Torokhov 	/*
2588a09221e8SDmitry Torokhov 	 * Even though we did not allocate psmouse->private we do free
2589a09221e8SDmitry Torokhov 	 * it here.
2590a09221e8SDmitry Torokhov 	 */
2591a09221e8SDmitry Torokhov 	kfree(psmouse->private);
25921e0c5b12SDmitry Torokhov 	psmouse->private = NULL;
2593a09221e8SDmitry Torokhov 	return error;
25941da177e4SLinus Torvalds }
25951da177e4SLinus Torvalds 
2596b7802c5cSDmitry Torokhov int alps_detect(struct psmouse *psmouse, bool set_properties)
25971da177e4SLinus Torvalds {
2598a09221e8SDmitry Torokhov 	struct alps_data *priv;
2599a09221e8SDmitry Torokhov 	int error;
26001da177e4SLinus Torvalds 
2601a09221e8SDmitry Torokhov 	error = alps_identify(psmouse, NULL);
2602a09221e8SDmitry Torokhov 	if (error)
2603a09221e8SDmitry Torokhov 		return error;
2604a09221e8SDmitry Torokhov 
2605a09221e8SDmitry Torokhov 	/*
2606a09221e8SDmitry Torokhov 	 * Reset the device to make sure it is fully operational:
2607a09221e8SDmitry Torokhov 	 * on some laptops, like certain Dell Latitudes, we may
2608a09221e8SDmitry Torokhov 	 * fail to properly detect presence of trackstick if device
2609a09221e8SDmitry Torokhov 	 * has not been reset.
2610a09221e8SDmitry Torokhov 	 */
2611a09221e8SDmitry Torokhov 	psmouse_reset(psmouse);
2612a09221e8SDmitry Torokhov 
2613a09221e8SDmitry Torokhov 	priv = kzalloc(sizeof(struct alps_data), GFP_KERNEL);
2614a09221e8SDmitry Torokhov 	if (!priv)
2615a09221e8SDmitry Torokhov 		return -ENOMEM;
2616a09221e8SDmitry Torokhov 
2617a09221e8SDmitry Torokhov 	error = alps_identify(psmouse, priv);
261893050db2SDmitry Torokhov 	if (error) {
261993050db2SDmitry Torokhov 		kfree(priv);
2620a09221e8SDmitry Torokhov 		return error;
262193050db2SDmitry Torokhov 	}
26221da177e4SLinus Torvalds 
26231da177e4SLinus Torvalds 	if (set_properties) {
26241da177e4SLinus Torvalds 		psmouse->vendor = "ALPS";
2625a09221e8SDmitry Torokhov 		psmouse->name = priv->flags & ALPS_DUALPOINT ?
2626968ac842SDmitry Torokhov 				"DualPoint TouchPad" : "GlidePoint";
2627a09221e8SDmitry Torokhov 		psmouse->model = priv->proto_version;
2628a09221e8SDmitry Torokhov 	} else {
2629a09221e8SDmitry Torokhov 		/*
2630a09221e8SDmitry Torokhov 		 * Destroy alps_data structure we allocated earlier since
2631a09221e8SDmitry Torokhov 		 * this was just a "trial run". Otherwise we'll keep it
2632a09221e8SDmitry Torokhov 		 * to be used by alps_init() which has to be called if
2633a09221e8SDmitry Torokhov 		 * we succeed and set_properties is true.
2634a09221e8SDmitry Torokhov 		 */
2635a09221e8SDmitry Torokhov 		kfree(priv);
2636a09221e8SDmitry Torokhov 		psmouse->private = NULL;
26371da177e4SLinus Torvalds 	}
2638a09221e8SDmitry Torokhov 
26391da177e4SLinus Torvalds 	return 0;
26401da177e4SLinus Torvalds }
26411da177e4SLinus Torvalds 
2642