xref: /openbmc/linux/drivers/input/mouse/alps.c (revision dccf1dd8)
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 
1563db5b9f7SMasaki Ota static const struct alps_protocol_info alps_v8_protocol_data = {
1573db5b9f7SMasaki Ota 	ALPS_PROTO_V8, 0x18, 0x18, 0
1583db5b9f7SMasaki Ota };
1593db5b9f7SMasaki Ota 
16024af5cb9SKevin Cernekee static void alps_set_abs_params_st(struct alps_data *priv,
16124af5cb9SKevin Cernekee 				   struct input_dev *dev1);
162688ea364SHans de Goede static void alps_set_abs_params_semi_mt(struct alps_data *priv,
16324af5cb9SKevin Cernekee 					struct input_dev *dev1);
1648eccd393SMasaki Ota static void alps_set_abs_params_v7(struct alps_data *priv,
1658eccd393SMasaki Ota 				   struct input_dev *dev1);
1663db5b9f7SMasaki Ota static void alps_set_abs_params_ss4_v2(struct alps_data *priv,
1673db5b9f7SMasaki Ota 				       struct input_dev *dev1);
16824af5cb9SKevin Cernekee 
169d4b347b2SSeth Forshee /* Packet formats are described in Documentation/input/alps.txt */
1701da177e4SLinus Torvalds 
17199df65e7SKevin Cernekee static bool alps_is_valid_first_byte(struct alps_data *priv,
1721d9f2626SSebastian Kapfer 				     unsigned char data)
1731d9f2626SSebastian Kapfer {
17499df65e7SKevin Cernekee 	return (data & priv->mask0) == priv->byte0;
1751d9f2626SSebastian Kapfer }
1761d9f2626SSebastian Kapfer 
17704aae283SPali Rohár static void alps_report_buttons(struct input_dev *dev1, struct input_dev *dev2,
1781d9f2626SSebastian Kapfer 				int left, int right, int middle)
1791d9f2626SSebastian Kapfer {
1801d9f2626SSebastian Kapfer 	struct input_dev *dev;
1811d9f2626SSebastian Kapfer 
1821d9f2626SSebastian Kapfer 	/*
1831d9f2626SSebastian Kapfer 	 * If shared button has already been reported on the
1841d9f2626SSebastian Kapfer 	 * other device (dev2) then this event should be also
1851d9f2626SSebastian Kapfer 	 * sent through that device.
1861d9f2626SSebastian Kapfer 	 */
18704aae283SPali Rohár 	dev = (dev2 && test_bit(BTN_LEFT, dev2->key)) ? dev2 : dev1;
1881d9f2626SSebastian Kapfer 	input_report_key(dev, BTN_LEFT, left);
1891d9f2626SSebastian Kapfer 
19004aae283SPali Rohár 	dev = (dev2 && test_bit(BTN_RIGHT, dev2->key)) ? dev2 : dev1;
1911d9f2626SSebastian Kapfer 	input_report_key(dev, BTN_RIGHT, right);
1921d9f2626SSebastian Kapfer 
19304aae283SPali Rohár 	dev = (dev2 && test_bit(BTN_MIDDLE, dev2->key)) ? dev2 : dev1;
1941d9f2626SSebastian Kapfer 	input_report_key(dev, BTN_MIDDLE, middle);
1951d9f2626SSebastian Kapfer 
1961d9f2626SSebastian Kapfer 	/*
1971d9f2626SSebastian Kapfer 	 * Sync the _other_ device now, we'll do the first
1981d9f2626SSebastian Kapfer 	 * device later once we report the rest of the events.
1991d9f2626SSebastian Kapfer 	 */
20004aae283SPali Rohár 	if (dev2)
2011d9f2626SSebastian Kapfer 		input_sync(dev2);
2021d9f2626SSebastian Kapfer }
2031d9f2626SSebastian Kapfer 
20425bded7cSSeth Forshee static void alps_process_packet_v1_v2(struct psmouse *psmouse)
2051da177e4SLinus Torvalds {
2061da177e4SLinus Torvalds 	struct alps_data *priv = psmouse->private;
2071da177e4SLinus Torvalds 	unsigned char *packet = psmouse->packet;
2082e5b636bSDmitry Torokhov 	struct input_dev *dev = psmouse->dev;
2092e5b636bSDmitry Torokhov 	struct input_dev *dev2 = priv->dev2;
2101da177e4SLinus Torvalds 	int x, y, z, ges, fin, left, right, middle;
211c30b4c10SIvan Casado Ruiz 	int back = 0, forward = 0;
2121da177e4SLinus Torvalds 
21399df65e7SKevin Cernekee 	if (priv->proto_version == ALPS_PROTO_V1) {
214d2f4012fSYotam Medini 		left = packet[2] & 0x10;
215d2f4012fSYotam Medini 		right = packet[2] & 0x08;
2161da177e4SLinus Torvalds 		middle = 0;
2171da177e4SLinus Torvalds 		x = packet[1] | ((packet[0] & 0x07) << 7);
2181da177e4SLinus Torvalds 		y = packet[4] | ((packet[3] & 0x07) << 7);
2191da177e4SLinus Torvalds 		z = packet[5];
2201da177e4SLinus Torvalds 	} else {
2211da177e4SLinus Torvalds 		left = packet[3] & 1;
2221da177e4SLinus Torvalds 		right = packet[3] & 2;
2231da177e4SLinus Torvalds 		middle = packet[3] & 4;
2241da177e4SLinus Torvalds 		x = packet[1] | ((packet[2] & 0x78) << (7 - 3));
2251da177e4SLinus Torvalds 		y = packet[4] | ((packet[3] & 0x70) << (7 - 4));
2261da177e4SLinus Torvalds 		z = packet[5];
2271da177e4SLinus Torvalds 	}
2281da177e4SLinus Torvalds 
22999df65e7SKevin Cernekee 	if (priv->flags & ALPS_FW_BK_1) {
2303c00bb96SLaszlo Kajan 		back = packet[0] & 0x10;
2313c00bb96SLaszlo Kajan 		forward = packet[2] & 4;
232c30b4c10SIvan Casado Ruiz 	}
233c30b4c10SIvan Casado Ruiz 
23499df65e7SKevin Cernekee 	if (priv->flags & ALPS_FW_BK_2) {
235c30b4c10SIvan Casado Ruiz 		back = packet[3] & 4;
236c30b4c10SIvan Casado Ruiz 		forward = packet[2] & 4;
237c30b4c10SIvan Casado Ruiz 		if ((middle = forward && back))
238c30b4c10SIvan Casado Ruiz 			forward = back = 0;
239c30b4c10SIvan Casado Ruiz 	}
240c30b4c10SIvan Casado Ruiz 
2411da177e4SLinus Torvalds 	ges = packet[2] & 1;
2421da177e4SLinus Torvalds 	fin = packet[2] & 2;
2431da177e4SLinus Torvalds 
24499df65e7SKevin Cernekee 	if ((priv->flags & ALPS_DUALPOINT) && z == 127) {
2451da177e4SLinus Torvalds 		input_report_rel(dev2, REL_X,  (x > 383 ? (x - 768) : x));
2461da177e4SLinus Torvalds 		input_report_rel(dev2, REL_Y, -(y > 255 ? (y - 512) : y));
247d7ed5d88SUlrich Dangel 
24804aae283SPali Rohár 		alps_report_buttons(dev2, dev, left, right, middle);
249d7ed5d88SUlrich Dangel 
2501da177e4SLinus Torvalds 		input_sync(dev2);
2511da177e4SLinus Torvalds 		return;
2521da177e4SLinus Torvalds 	}
2531da177e4SLinus Torvalds 
25404aae283SPali Rohár 	alps_report_buttons(dev, dev2, left, right, middle);
255d7ed5d88SUlrich Dangel 
2561da177e4SLinus Torvalds 	/* Convert hardware tap to a reasonable Z value */
25771bb21b6SMaxim Levitsky 	if (ges && !fin)
25871bb21b6SMaxim Levitsky 		z = 40;
2591da177e4SLinus Torvalds 
2601da177e4SLinus Torvalds 	/*
2611da177e4SLinus Torvalds 	 * A "tap and drag" operation is reported by the hardware as a transition
2621da177e4SLinus Torvalds 	 * from (!fin && ges) to (fin && ges). This should be translated to the
2631da177e4SLinus Torvalds 	 * sequence Z>0, Z==0, Z>0, so the Z==0 event has to be generated manually.
2641da177e4SLinus Torvalds 	 */
2651da177e4SLinus Torvalds 	if (ges && fin && !priv->prev_fin) {
2661da177e4SLinus Torvalds 		input_report_abs(dev, ABS_X, x);
2671da177e4SLinus Torvalds 		input_report_abs(dev, ABS_Y, y);
2681da177e4SLinus Torvalds 		input_report_abs(dev, ABS_PRESSURE, 0);
2691da177e4SLinus Torvalds 		input_report_key(dev, BTN_TOOL_FINGER, 0);
2701da177e4SLinus Torvalds 		input_sync(dev);
2711da177e4SLinus Torvalds 	}
2721da177e4SLinus Torvalds 	priv->prev_fin = fin;
2731da177e4SLinus Torvalds 
27471bb21b6SMaxim Levitsky 	if (z > 30)
27571bb21b6SMaxim Levitsky 		input_report_key(dev, BTN_TOUCH, 1);
27671bb21b6SMaxim Levitsky 	if (z < 25)
27771bb21b6SMaxim Levitsky 		input_report_key(dev, BTN_TOUCH, 0);
2781da177e4SLinus Torvalds 
2791da177e4SLinus Torvalds 	if (z > 0) {
2801da177e4SLinus Torvalds 		input_report_abs(dev, ABS_X, x);
2811da177e4SLinus Torvalds 		input_report_abs(dev, ABS_Y, y);
2821da177e4SLinus Torvalds 	}
2831da177e4SLinus Torvalds 
2841da177e4SLinus Torvalds 	input_report_abs(dev, ABS_PRESSURE, z);
2851da177e4SLinus Torvalds 	input_report_key(dev, BTN_TOOL_FINGER, z > 0);
2861da177e4SLinus Torvalds 
28799df65e7SKevin Cernekee 	if (priv->flags & ALPS_WHEEL)
288e6c047b9SVojtech Pavlik 		input_report_rel(dev, REL_WHEEL, ((packet[2] << 1) & 0x08) - ((packet[0] >> 4) & 0x07));
2891da177e4SLinus Torvalds 
29099df65e7SKevin Cernekee 	if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
291c30b4c10SIvan Casado Ruiz 		input_report_key(dev, BTN_FORWARD, forward);
292c30b4c10SIvan Casado Ruiz 		input_report_key(dev, BTN_BACK, back);
2931da177e4SLinus Torvalds 	}
2941da177e4SLinus Torvalds 
29599df65e7SKevin Cernekee 	if (priv->flags & ALPS_FOUR_BUTTONS) {
29671bb21b6SMaxim Levitsky 		input_report_key(dev, BTN_0, packet[2] & 4);
29771bb21b6SMaxim Levitsky 		input_report_key(dev, BTN_1, packet[0] & 0x10);
29871bb21b6SMaxim Levitsky 		input_report_key(dev, BTN_2, packet[3] & 4);
29971bb21b6SMaxim Levitsky 		input_report_key(dev, BTN_3, packet[0] & 0x20);
30071bb21b6SMaxim Levitsky 	}
30171bb21b6SMaxim Levitsky 
3021da177e4SLinus Torvalds 	input_sync(dev);
3031da177e4SLinus Torvalds }
3041da177e4SLinus Torvalds 
305036e6c7bSHans de Goede static void alps_get_bitmap_points(unsigned int map,
306036e6c7bSHans de Goede 				   struct alps_bitmap_point *low,
307036e6c7bSHans de Goede 				   struct alps_bitmap_point *high,
308036e6c7bSHans de Goede 				   int *fingers)
309036e6c7bSHans de Goede {
310036e6c7bSHans de Goede 	struct alps_bitmap_point *point;
311036e6c7bSHans de Goede 	int i, bit, prev_bit = 0;
312036e6c7bSHans de Goede 
313036e6c7bSHans de Goede 	point = low;
314036e6c7bSHans de Goede 	for (i = 0; map != 0; i++, map >>= 1) {
315036e6c7bSHans de Goede 		bit = map & 1;
316036e6c7bSHans de Goede 		if (bit) {
317036e6c7bSHans de Goede 			if (!prev_bit) {
318036e6c7bSHans de Goede 				point->start_bit = i;
319105affbfSHans de Goede 				point->num_bits = 0;
320036e6c7bSHans de Goede 				(*fingers)++;
321036e6c7bSHans de Goede 			}
322036e6c7bSHans de Goede 			point->num_bits++;
323036e6c7bSHans de Goede 		} else {
324036e6c7bSHans de Goede 			if (prev_bit)
325036e6c7bSHans de Goede 				point = high;
326036e6c7bSHans de Goede 		}
327036e6c7bSHans de Goede 		prev_bit = bit;
328036e6c7bSHans de Goede 	}
329036e6c7bSHans de Goede }
330036e6c7bSHans de Goede 
331ee65d4b3SYunkang Tang /*
332dccf1dd8SHans de Goede  * Process bitmap data from semi-mt protocols. Returns the number of
33301ce661fSSeth Forshee  * fingers detected. A return value of 0 means at least one of the
33401ce661fSSeth Forshee  * bitmaps was empty.
33501ce661fSSeth Forshee  *
33601ce661fSSeth Forshee  * The bitmaps don't have enough data to track fingers, so this function
33701ce661fSSeth Forshee  * only generates points representing a bounding box of all contacts.
33802d04254SHans de Goede  * These points are returned in fields->mt when the return value
33901ce661fSSeth Forshee  * is greater than 0.
34001ce661fSSeth Forshee  */
3417a9f73e7SKevin Cernekee static int alps_process_bitmap(struct alps_data *priv,
34202d04254SHans de Goede 			       struct alps_fields *fields)
34301ce661fSSeth Forshee {
3444dd26573SHans de Goede 	int i, fingers_x = 0, fingers_y = 0, fingers, closest;
34501ce661fSSeth Forshee 	struct alps_bitmap_point x_low = {0,}, x_high = {0,};
34601ce661fSSeth Forshee 	struct alps_bitmap_point y_low = {0,}, y_high = {0,};
3474dd26573SHans de Goede 	struct input_mt_pos corner[4];
34801ce661fSSeth Forshee 
34902d04254SHans de Goede 	if (!fields->x_map || !fields->y_map)
35001ce661fSSeth Forshee 		return 0;
35101ce661fSSeth Forshee 
35202d04254SHans de Goede 	alps_get_bitmap_points(fields->x_map, &x_low, &x_high, &fingers_x);
35302d04254SHans de Goede 	alps_get_bitmap_points(fields->y_map, &y_low, &y_high, &fingers_y);
35401ce661fSSeth Forshee 
35501ce661fSSeth Forshee 	/*
35601ce661fSSeth Forshee 	 * Fingers can overlap, so we use the maximum count of fingers
35701ce661fSSeth Forshee 	 * on either axis as the finger count.
35801ce661fSSeth Forshee 	 */
35901ce661fSSeth Forshee 	fingers = max(fingers_x, fingers_y);
36001ce661fSSeth Forshee 
36101ce661fSSeth Forshee 	/*
36220bea68bSHans de Goede 	 * If an axis reports only a single contact, we have overlapping or
36320bea68bSHans de Goede 	 * adjacent fingers. Divide the single contact between the two points.
36401ce661fSSeth Forshee 	 */
36501ce661fSSeth Forshee 	if (fingers_x == 1) {
36628835f45SHans de Goede 		i = (x_low.num_bits - 1) / 2;
36701ce661fSSeth Forshee 		x_low.num_bits = x_low.num_bits - i;
36801ce661fSSeth Forshee 		x_high.start_bit = x_low.start_bit + i;
36901ce661fSSeth Forshee 		x_high.num_bits = max(i, 1);
37020bea68bSHans de Goede 	}
37120bea68bSHans de Goede 	if (fingers_y == 1) {
37228835f45SHans de Goede 		i = (y_low.num_bits - 1) / 2;
37301ce661fSSeth Forshee 		y_low.num_bits = y_low.num_bits - i;
37401ce661fSSeth Forshee 		y_high.start_bit = y_low.start_bit + i;
37501ce661fSSeth Forshee 		y_high.num_bits = max(i, 1);
37601ce661fSSeth Forshee 	}
37701ce661fSSeth Forshee 
3784dd26573SHans de Goede 	/* top-left corner */
3794dd26573SHans de Goede 	corner[0].x =
38002d04254SHans de Goede 		(priv->x_max * (2 * x_low.start_bit + x_low.num_bits - 1)) /
3817a9f73e7SKevin Cernekee 		(2 * (priv->x_bits - 1));
3824dd26573SHans de Goede 	corner[0].y =
38302d04254SHans de Goede 		(priv->y_max * (2 * y_low.start_bit + y_low.num_bits - 1)) /
3847a9f73e7SKevin Cernekee 		(2 * (priv->y_bits - 1));
38501ce661fSSeth Forshee 
3864dd26573SHans de Goede 	/* top-right corner */
3874dd26573SHans de Goede 	corner[1].x =
38802d04254SHans de Goede 		(priv->x_max * (2 * x_high.start_bit + x_high.num_bits - 1)) /
3897a9f73e7SKevin Cernekee 		(2 * (priv->x_bits - 1));
3904dd26573SHans de Goede 	corner[1].y =
3914dd26573SHans de Goede 		(priv->y_max * (2 * y_low.start_bit + y_low.num_bits - 1)) /
3924dd26573SHans de Goede 		(2 * (priv->y_bits - 1));
3934dd26573SHans de Goede 
3944dd26573SHans de Goede 	/* bottom-right corner */
3954dd26573SHans de Goede 	corner[2].x =
3964dd26573SHans de Goede 		(priv->x_max * (2 * x_high.start_bit + x_high.num_bits - 1)) /
3974dd26573SHans de Goede 		(2 * (priv->x_bits - 1));
3984dd26573SHans de Goede 	corner[2].y =
3994dd26573SHans de Goede 		(priv->y_max * (2 * y_high.start_bit + y_high.num_bits - 1)) /
4004dd26573SHans de Goede 		(2 * (priv->y_bits - 1));
4014dd26573SHans de Goede 
4024dd26573SHans de Goede 	/* bottom-left corner */
4034dd26573SHans de Goede 	corner[3].x =
4044dd26573SHans de Goede 		(priv->x_max * (2 * x_low.start_bit + x_low.num_bits - 1)) /
4054dd26573SHans de Goede 		(2 * (priv->x_bits - 1));
4064dd26573SHans de Goede 	corner[3].y =
40702d04254SHans de Goede 		(priv->y_max * (2 * y_high.start_bit + y_high.num_bits - 1)) /
4087a9f73e7SKevin Cernekee 		(2 * (priv->y_bits - 1));
40901ce661fSSeth Forshee 
410dccf1dd8SHans de Goede 	/* x-bitmap order is reversed on v5 touchpads  */
411dccf1dd8SHans de Goede 	if (priv->proto_version == ALPS_PROTO_V5) {
412dccf1dd8SHans de Goede 		for (i = 0; i < 4; i++)
413dccf1dd8SHans de Goede 			corner[i].x = priv->x_max - corner[i].x;
414dccf1dd8SHans de Goede 	}
415dccf1dd8SHans de Goede 
416dccf1dd8SHans de Goede 	/* y-bitmap order is reversed on v3 and v4 touchpads  */
417dccf1dd8SHans de Goede 	if (priv->proto_version == ALPS_PROTO_V3 ||
418dccf1dd8SHans de Goede 	    priv->proto_version == ALPS_PROTO_V4) {
4194dd26573SHans de Goede 		for (i = 0; i < 4; i++)
4204dd26573SHans de Goede 			corner[i].y = priv->y_max - corner[i].y;
42140e8f53bSHans de Goede 	}
42240e8f53bSHans de Goede 
4234dd26573SHans de Goede 	/*
4244dd26573SHans de Goede 	 * We only select a corner for the second touch once per 2 finger
4254dd26573SHans de Goede 	 * touch sequence to avoid the chosen corner (and thus the coordinates)
4264dd26573SHans de Goede 	 * jumping around when the first touch is in the middle.
4274dd26573SHans de Goede 	 */
4284dd26573SHans de Goede 	if (priv->second_touch == -1) {
4294dd26573SHans de Goede 		/* Find corner closest to our st coordinates */
4304dd26573SHans de Goede 		closest = 0x7fffffff;
4314dd26573SHans de Goede 		for (i = 0; i < 4; i++) {
4324dd26573SHans de Goede 			int dx = fields->st.x - corner[i].x;
4334dd26573SHans de Goede 			int dy = fields->st.y - corner[i].y;
4344dd26573SHans de Goede 			int distance = dx * dx + dy * dy;
4354dd26573SHans de Goede 
4364dd26573SHans de Goede 			if (distance < closest) {
4374dd26573SHans de Goede 				priv->second_touch = i;
4384dd26573SHans de Goede 				closest = distance;
4394dd26573SHans de Goede 			}
4404dd26573SHans de Goede 		}
4414dd26573SHans de Goede 		/* And select the opposite corner to use for the 2nd touch */
4424dd26573SHans de Goede 		priv->second_touch = (priv->second_touch + 2) % 4;
4434dd26573SHans de Goede 	}
4444dd26573SHans de Goede 
4454dd26573SHans de Goede 	fields->mt[0] = fields->st;
4464dd26573SHans de Goede 	fields->mt[1] = corner[priv->second_touch];
4474dd26573SHans de Goede 
44801ce661fSSeth Forshee 	return fingers;
44901ce661fSSeth Forshee }
45001ce661fSSeth Forshee 
451cdf333efSHans de Goede static void alps_set_slot(struct input_dev *dev, int slot, int x, int y)
45201ce661fSSeth Forshee {
45301ce661fSSeth Forshee 	input_mt_slot(dev, slot);
454cdf333efSHans de Goede 	input_mt_report_slot_state(dev, MT_TOOL_FINGER, true);
45501ce661fSSeth Forshee 	input_report_abs(dev, ABS_MT_POSITION_X, x);
45601ce661fSSeth Forshee 	input_report_abs(dev, ABS_MT_POSITION_Y, y);
45701ce661fSSeth Forshee }
45801ce661fSSeth Forshee 
459cdf333efSHans de Goede static void alps_report_mt_data(struct psmouse *psmouse, int n)
46001ce661fSSeth Forshee {
46102d04254SHans de Goede 	struct alps_data *priv = psmouse->private;
46202d04254SHans de Goede 	struct input_dev *dev = psmouse->dev;
46302d04254SHans de Goede 	struct alps_fields *f = &priv->f;
464cdf333efSHans de Goede 	int i, slot[MAX_TOUCHES];
46502d04254SHans de Goede 
466448c7f38SHenrik Rydberg 	input_mt_assign_slots(dev, slot, f->mt, n, 0);
467cdf333efSHans de Goede 	for (i = 0; i < n; i++)
468cdf333efSHans de Goede 		alps_set_slot(dev, slot[i], f->mt[i].x, f->mt[i].y);
469cdf333efSHans de Goede 
470cdf333efSHans de Goede 	input_mt_sync_frame(dev);
47101ce661fSSeth Forshee }
47201ce661fSSeth Forshee 
47368c21870SHans de Goede static void alps_report_semi_mt_data(struct psmouse *psmouse, int fingers)
47468c21870SHans de Goede {
47568c21870SHans de Goede 	struct alps_data *priv = psmouse->private;
47668c21870SHans de Goede 	struct input_dev *dev = psmouse->dev;
47768c21870SHans de Goede 	struct alps_fields *f = &priv->f;
47868c21870SHans de Goede 
47968c21870SHans de Goede 	/* Use st data when we don't have mt data */
48068c21870SHans de Goede 	if (fingers < 2) {
48168c21870SHans de Goede 		f->mt[0].x = f->st.x;
48268c21870SHans de Goede 		f->mt[0].y = f->st.y;
48368c21870SHans de Goede 		fingers = f->pressure > 0 ? 1 : 0;
4844dd26573SHans de Goede 		priv->second_touch = -1;
48568c21870SHans de Goede 	}
48668c21870SHans de Goede 
4871662c033SHans de Goede 	if (fingers >= 1)
4881662c033SHans de Goede 		alps_set_slot(dev, 0, f->mt[0].x, f->mt[0].y);
4891662c033SHans de Goede 	if (fingers >= 2)
4901662c033SHans de Goede 		alps_set_slot(dev, 1, f->mt[1].x, f->mt[1].y);
4911662c033SHans de Goede 	input_mt_sync_frame(dev);
49268c21870SHans de Goede 
49368c21870SHans de Goede 	input_mt_report_finger_count(dev, fingers);
49468c21870SHans de Goede 
49568c21870SHans de Goede 	input_report_key(dev, BTN_LEFT, f->left);
49668c21870SHans de Goede 	input_report_key(dev, BTN_RIGHT, f->right);
49768c21870SHans de Goede 	input_report_key(dev, BTN_MIDDLE, f->middle);
49868c21870SHans de Goede 
49968c21870SHans de Goede 	input_report_abs(dev, ABS_PRESSURE, f->pressure);
50068c21870SHans de Goede 
50168c21870SHans de Goede 	input_sync(dev);
50268c21870SHans de Goede }
50368c21870SHans de Goede 
50425bded7cSSeth Forshee static void alps_process_trackstick_packet_v3(struct psmouse *psmouse)
50525bded7cSSeth Forshee {
50625bded7cSSeth Forshee 	struct alps_data *priv = psmouse->private;
50725bded7cSSeth Forshee 	unsigned char *packet = psmouse->packet;
50825bded7cSSeth Forshee 	struct input_dev *dev = priv->dev2;
50925bded7cSSeth Forshee 	int x, y, z, left, right, middle;
51025bded7cSSeth Forshee 
51134412ba2SPali Rohár 	/* It should be a DualPoint when received trackstick packet */
51234412ba2SPali Rohár 	if (!(priv->flags & ALPS_DUALPOINT)) {
51334412ba2SPali Rohár 		psmouse_warn(psmouse,
51434412ba2SPali Rohár 			     "Rejected trackstick packet from non DualPoint device");
51534412ba2SPali Rohár 		return;
51634412ba2SPali Rohár 	}
51734412ba2SPali Rohár 
51825bded7cSSeth Forshee 	/* Sanity check packet */
51925bded7cSSeth Forshee 	if (!(packet[0] & 0x40)) {
52025bded7cSSeth Forshee 		psmouse_dbg(psmouse, "Bad trackstick packet, discarding\n");
52125bded7cSSeth Forshee 		return;
52225bded7cSSeth Forshee 	}
52325bded7cSSeth Forshee 
52425bded7cSSeth Forshee 	/*
52525bded7cSSeth Forshee 	 * There's a special packet that seems to indicate the end
52625bded7cSSeth Forshee 	 * of a stream of trackstick data. Filter these out.
52725bded7cSSeth Forshee 	 */
52825bded7cSSeth Forshee 	if (packet[1] == 0x7f && packet[2] == 0x7f && packet[4] == 0x7f)
52925bded7cSSeth Forshee 		return;
53025bded7cSSeth Forshee 
53125bded7cSSeth Forshee 	x = (s8)(((packet[0] & 0x20) << 2) | (packet[1] & 0x7f));
53225bded7cSSeth Forshee 	y = (s8)(((packet[0] & 0x10) << 3) | (packet[2] & 0x7f));
53325bded7cSSeth Forshee 	z = (packet[4] & 0x7c) >> 2;
53425bded7cSSeth Forshee 
53525bded7cSSeth Forshee 	/*
53625bded7cSSeth Forshee 	 * The x and y values tend to be quite large, and when used
53725bded7cSSeth Forshee 	 * alone the trackstick is difficult to use. Scale them down
53825bded7cSSeth Forshee 	 * to compensate.
53925bded7cSSeth Forshee 	 */
54025bded7cSSeth Forshee 	x /= 8;
54125bded7cSSeth Forshee 	y /= 8;
54225bded7cSSeth Forshee 
54325bded7cSSeth Forshee 	input_report_rel(dev, REL_X, x);
54425bded7cSSeth Forshee 	input_report_rel(dev, REL_Y, -y);
54525bded7cSSeth Forshee 
54625bded7cSSeth Forshee 	/*
54725bded7cSSeth Forshee 	 * Most ALPS models report the trackstick buttons in the touchpad
54825bded7cSSeth Forshee 	 * packets, but a few report them here. No reliable way has been
54925bded7cSSeth Forshee 	 * found to differentiate between the models upfront, so we enable
55025bded7cSSeth Forshee 	 * the quirk in response to seeing a button press in the trackstick
55125bded7cSSeth Forshee 	 * packet.
55225bded7cSSeth Forshee 	 */
55325bded7cSSeth Forshee 	left = packet[3] & 0x01;
55425bded7cSSeth Forshee 	right = packet[3] & 0x02;
55525bded7cSSeth Forshee 	middle = packet[3] & 0x04;
55625bded7cSSeth Forshee 
55725bded7cSSeth Forshee 	if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) &&
55825bded7cSSeth Forshee 	    (left || right || middle))
55925bded7cSSeth Forshee 		priv->quirks |= ALPS_QUIRK_TRACKSTICK_BUTTONS;
56025bded7cSSeth Forshee 
56125bded7cSSeth Forshee 	if (priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) {
56225bded7cSSeth Forshee 		input_report_key(dev, BTN_LEFT, left);
56325bded7cSSeth Forshee 		input_report_key(dev, BTN_RIGHT, right);
56425bded7cSSeth Forshee 		input_report_key(dev, BTN_MIDDLE, middle);
56525bded7cSSeth Forshee 	}
56625bded7cSSeth Forshee 
56725bded7cSSeth Forshee 	input_sync(dev);
56825bded7cSSeth Forshee 	return;
56925bded7cSSeth Forshee }
57025bded7cSSeth Forshee 
571f85e5001SKevin Cernekee static void alps_decode_buttons_v3(struct alps_fields *f, unsigned char *p)
572f85e5001SKevin Cernekee {
573f85e5001SKevin Cernekee 	f->left = !!(p[3] & 0x01);
574f85e5001SKevin Cernekee 	f->right = !!(p[3] & 0x02);
575f85e5001SKevin Cernekee 	f->middle = !!(p[3] & 0x04);
576f85e5001SKevin Cernekee 
577f85e5001SKevin Cernekee 	f->ts_left = !!(p[3] & 0x10);
578f85e5001SKevin Cernekee 	f->ts_right = !!(p[3] & 0x20);
579f85e5001SKevin Cernekee 	f->ts_middle = !!(p[3] & 0x40);
580f85e5001SKevin Cernekee }
581f85e5001SKevin Cernekee 
58238c11eaaSHans de Goede static int alps_decode_pinnacle(struct alps_fields *f, unsigned char *p,
583ee65d4b3SYunkang Tang 				 struct psmouse *psmouse)
584f85e5001SKevin Cernekee {
585f85e5001SKevin Cernekee 	f->first_mp = !!(p[4] & 0x40);
586f85e5001SKevin Cernekee 	f->is_mp = !!(p[0] & 0x40);
587f85e5001SKevin Cernekee 
588a839cd57SHans de Goede 	if (f->is_mp) {
589f85e5001SKevin Cernekee 		f->fingers = (p[5] & 0x3) + 1;
590f85e5001SKevin Cernekee 		f->x_map = ((p[4] & 0x7e) << 8) |
591f85e5001SKevin Cernekee 			   ((p[1] & 0x7f) << 2) |
592f85e5001SKevin Cernekee 			   ((p[0] & 0x30) >> 4);
593f85e5001SKevin Cernekee 		f->y_map = ((p[3] & 0x70) << 4) |
594f85e5001SKevin Cernekee 			   ((p[2] & 0x7f) << 1) |
595f85e5001SKevin Cernekee 			   (p[4] & 0x01);
596a839cd57SHans de Goede 	} else {
59702d04254SHans de Goede 		f->st.x = ((p[1] & 0x7f) << 4) | ((p[4] & 0x30) >> 2) |
598f85e5001SKevin Cernekee 		       ((p[0] & 0x30) >> 4);
59902d04254SHans de Goede 		f->st.y = ((p[2] & 0x7f) << 4) | (p[4] & 0x0f);
60002d04254SHans de Goede 		f->pressure = p[5] & 0x7f;
601f85e5001SKevin Cernekee 
602f85e5001SKevin Cernekee 		alps_decode_buttons_v3(f, p);
603a839cd57SHans de Goede 	}
60438c11eaaSHans de Goede 
60538c11eaaSHans de Goede 	return 0;
606f85e5001SKevin Cernekee }
607f85e5001SKevin Cernekee 
60838c11eaaSHans de Goede static int alps_decode_rushmore(struct alps_fields *f, unsigned char *p,
609ee65d4b3SYunkang Tang 				 struct psmouse *psmouse)
6101302bac3SKevin Cernekee {
611aab9cf7bSHans de Goede 	f->first_mp = !!(p[4] & 0x40);
612f105e34aSYunkang Tang 	f->is_mp = !!(p[5] & 0x40);
613aab9cf7bSHans de Goede 
614a839cd57SHans de Goede 	if (f->is_mp) {
615f105e34aSYunkang Tang 		f->fingers = max((p[5] & 0x3), ((p[5] >> 2) & 0x3)) + 1;
616aab9cf7bSHans de Goede 		f->x_map = ((p[5] & 0x10) << 11) |
617aab9cf7bSHans de Goede 			   ((p[4] & 0x7e) << 8) |
618aab9cf7bSHans de Goede 			   ((p[1] & 0x7f) << 2) |
619aab9cf7bSHans de Goede 			   ((p[0] & 0x30) >> 4);
620aab9cf7bSHans de Goede 		f->y_map = ((p[5] & 0x20) << 6) |
621aab9cf7bSHans de Goede 			   ((p[3] & 0x70) << 4) |
622aab9cf7bSHans de Goede 			   ((p[2] & 0x7f) << 1) |
623aab9cf7bSHans de Goede 			   (p[4] & 0x01);
624a839cd57SHans de Goede 	} else {
625aab9cf7bSHans de Goede 		f->st.x = ((p[1] & 0x7f) << 4) | ((p[4] & 0x30) >> 2) |
626aab9cf7bSHans de Goede 		       ((p[0] & 0x30) >> 4);
627aab9cf7bSHans de Goede 		f->st.y = ((p[2] & 0x7f) << 4) | (p[4] & 0x0f);
628aab9cf7bSHans de Goede 		f->pressure = p[5] & 0x7f;
629aab9cf7bSHans de Goede 
630aab9cf7bSHans de Goede 		alps_decode_buttons_v3(f, p);
631a839cd57SHans de Goede 	}
63238c11eaaSHans de Goede 
63338c11eaaSHans de Goede 	return 0;
6341302bac3SKevin Cernekee }
6351302bac3SKevin Cernekee 
63638c11eaaSHans de Goede static int alps_decode_dolphin(struct alps_fields *f, unsigned char *p,
637ee65d4b3SYunkang Tang 				struct psmouse *psmouse)
63875af9e56SDave Turvene {
639ee65d4b3SYunkang Tang 	u64 palm_data = 0;
640ee65d4b3SYunkang Tang 	struct alps_data *priv = psmouse->private;
641ee65d4b3SYunkang Tang 
64275af9e56SDave Turvene 	f->first_mp = !!(p[0] & 0x02);
64375af9e56SDave Turvene 	f->is_mp = !!(p[0] & 0x20);
64475af9e56SDave Turvene 
645ee65d4b3SYunkang Tang 	if (!f->is_mp) {
64602d04254SHans de Goede 		f->st.x = ((p[1] & 0x7f) | ((p[4] & 0x0f) << 7));
64702d04254SHans de Goede 		f->st.y = ((p[2] & 0x7f) | ((p[4] & 0xf0) << 3));
64802d04254SHans de Goede 		f->pressure = (p[0] & 4) ? 0 : p[5] & 0x7f;
64975af9e56SDave Turvene 		alps_decode_buttons_v3(f, p);
650ee65d4b3SYunkang Tang 	} else {
651ee65d4b3SYunkang Tang 		f->fingers = ((p[0] & 0x6) >> 1 |
652ee65d4b3SYunkang Tang 		     (p[0] & 0x10) >> 2);
653ee65d4b3SYunkang Tang 
654ee65d4b3SYunkang Tang 		palm_data = (p[1] & 0x7f) |
655ee65d4b3SYunkang Tang 			    ((p[2] & 0x7f) << 7) |
656ee65d4b3SYunkang Tang 			    ((p[4] & 0x7f) << 14) |
657ee65d4b3SYunkang Tang 			    ((p[5] & 0x7f) << 21) |
658ee65d4b3SYunkang Tang 			    ((p[3] & 0x07) << 28) |
659ee65d4b3SYunkang Tang 			    (((u64)p[3] & 0x70) << 27) |
660ee65d4b3SYunkang Tang 			    (((u64)p[0] & 0x01) << 34);
661ee65d4b3SYunkang Tang 
662ee65d4b3SYunkang Tang 		/* Y-profile is stored in P(0) to p(n-1), n = y_bits; */
663ee65d4b3SYunkang Tang 		f->y_map = palm_data & (BIT(priv->y_bits) - 1);
664ee65d4b3SYunkang Tang 
665ee65d4b3SYunkang Tang 		/* X-profile is stored in p(n) to p(n+m-1), m = x_bits; */
666ee65d4b3SYunkang Tang 		f->x_map = (palm_data >> priv->y_bits) &
667ee65d4b3SYunkang Tang 			   (BIT(priv->x_bits) - 1);
668ee65d4b3SYunkang Tang 	}
66938c11eaaSHans de Goede 
67038c11eaaSHans de Goede 	return 0;
67175af9e56SDave Turvene }
67275af9e56SDave Turvene 
673ee65d4b3SYunkang Tang static void alps_process_touchpad_packet_v3_v5(struct psmouse *psmouse)
67425bded7cSSeth Forshee {
67525bded7cSSeth Forshee 	struct alps_data *priv = psmouse->private;
67625bded7cSSeth Forshee 	unsigned char *packet = psmouse->packet;
67725bded7cSSeth Forshee 	struct input_dev *dev2 = priv->dev2;
67802d04254SHans de Goede 	struct alps_fields *f = &priv->f;
67902d04254SHans de Goede 	int fingers = 0;
680f85e5001SKevin Cernekee 
68102d04254SHans de Goede 	memset(f, 0, sizeof(*f));
68202d04254SHans de Goede 
68302d04254SHans de Goede 	priv->decode_fields(f, packet, psmouse);
68425bded7cSSeth Forshee 
68525bded7cSSeth Forshee 	/*
68601ce661fSSeth Forshee 	 * There's no single feature of touchpad position and bitmap packets
68701ce661fSSeth Forshee 	 * that can be used to distinguish between them. We rely on the fact
68801ce661fSSeth Forshee 	 * that a bitmap packet should always follow a position packet with
68901ce661fSSeth Forshee 	 * bit 6 of packet[4] set.
69025bded7cSSeth Forshee 	 */
69125bded7cSSeth Forshee 	if (priv->multi_packet) {
69225bded7cSSeth Forshee 		/*
69325bded7cSSeth Forshee 		 * Sometimes a position packet will indicate a multi-packet
69425bded7cSSeth Forshee 		 * sequence, but then what follows is another position
69525bded7cSSeth Forshee 		 * packet. Check for this, and when it happens process the
69625bded7cSSeth Forshee 		 * position packet as usual.
69725bded7cSSeth Forshee 		 */
69802d04254SHans de Goede 		if (f->is_mp) {
69902d04254SHans de Goede 			fingers = f->fingers;
70044b77f38SHans de Goede 			/*
70144b77f38SHans de Goede 			 * Bitmap processing uses position packet's coordinate
70244b77f38SHans de Goede 			 * data, so we need to do decode it first.
70344b77f38SHans de Goede 			 */
70444b77f38SHans de Goede 			priv->decode_fields(f, priv->multi_data, psmouse);
70502d04254SHans de Goede 			if (alps_process_bitmap(priv, f) == 0)
70620bea68bSHans de Goede 				fingers = 0; /* Use st data */
707ee65d4b3SYunkang Tang 		} else {
70801ce661fSSeth Forshee 			priv->multi_packet = 0;
70925bded7cSSeth Forshee 		}
71025bded7cSSeth Forshee 	}
71125bded7cSSeth Forshee 
71201ce661fSSeth Forshee 	/*
71301ce661fSSeth Forshee 	 * Bit 6 of byte 0 is not usually set in position packets. The only
71401ce661fSSeth Forshee 	 * times it seems to be set is in situations where the data is
71501ce661fSSeth Forshee 	 * suspect anyway, e.g. a palm resting flat on the touchpad. Given
71601ce661fSSeth Forshee 	 * this combined with the fact that this bit is useful for filtering
71701ce661fSSeth Forshee 	 * out misidentified bitmap packets, we reject anything with this
71801ce661fSSeth Forshee 	 * bit set.
71901ce661fSSeth Forshee 	 */
72002d04254SHans de Goede 	if (f->is_mp)
72101ce661fSSeth Forshee 		return;
72201ce661fSSeth Forshee 
72302d04254SHans de Goede 	if (!priv->multi_packet && f->first_mp) {
72425bded7cSSeth Forshee 		priv->multi_packet = 1;
72501ce661fSSeth Forshee 		memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
72601ce661fSSeth Forshee 		return;
72701ce661fSSeth Forshee 	}
72801ce661fSSeth Forshee 
72925bded7cSSeth Forshee 	priv->multi_packet = 0;
73025bded7cSSeth Forshee 
73125bded7cSSeth Forshee 	/*
73225bded7cSSeth Forshee 	 * Sometimes the hardware sends a single packet with z = 0
73325bded7cSSeth Forshee 	 * in the middle of a stream. Real releases generate packets
73425bded7cSSeth Forshee 	 * with x, y, and z all zero, so these seem to be flukes.
73525bded7cSSeth Forshee 	 * Ignore them.
73625bded7cSSeth Forshee 	 */
73702d04254SHans de Goede 	if (f->st.x && f->st.y && !f->pressure)
73825bded7cSSeth Forshee 		return;
73925bded7cSSeth Forshee 
74068c21870SHans de Goede 	alps_report_semi_mt_data(psmouse, fingers);
74125bded7cSSeth Forshee 
74234412ba2SPali Rohár 	if ((priv->flags & ALPS_DUALPOINT) &&
74334412ba2SPali Rohár 	    !(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS)) {
74402d04254SHans de Goede 		input_report_key(dev2, BTN_LEFT, f->ts_left);
74502d04254SHans de Goede 		input_report_key(dev2, BTN_RIGHT, f->ts_right);
74602d04254SHans de Goede 		input_report_key(dev2, BTN_MIDDLE, f->ts_middle);
74725bded7cSSeth Forshee 		input_sync(dev2);
74825bded7cSSeth Forshee 	}
74925bded7cSSeth Forshee }
75025bded7cSSeth Forshee 
75125bded7cSSeth Forshee static void alps_process_packet_v3(struct psmouse *psmouse)
75225bded7cSSeth Forshee {
75325bded7cSSeth Forshee 	unsigned char *packet = psmouse->packet;
75425bded7cSSeth Forshee 
75525bded7cSSeth Forshee 	/*
75625bded7cSSeth Forshee 	 * v3 protocol packets come in three types, two representing
75725bded7cSSeth Forshee 	 * touchpad data and one representing trackstick data.
75825bded7cSSeth Forshee 	 * Trackstick packets seem to be distinguished by always
75925bded7cSSeth Forshee 	 * having 0x3f in the last byte. This value has never been
76025bded7cSSeth Forshee 	 * observed in the last byte of either of the other types
76125bded7cSSeth Forshee 	 * of packets.
76225bded7cSSeth Forshee 	 */
76325bded7cSSeth Forshee 	if (packet[5] == 0x3f) {
76425bded7cSSeth Forshee 		alps_process_trackstick_packet_v3(psmouse);
76525bded7cSSeth Forshee 		return;
76625bded7cSSeth Forshee 	}
76725bded7cSSeth Forshee 
768ee65d4b3SYunkang Tang 	alps_process_touchpad_packet_v3_v5(psmouse);
76925bded7cSSeth Forshee }
77025bded7cSSeth Forshee 
77195f75e91SYunkang Tang static void alps_process_packet_v6(struct psmouse *psmouse)
77295f75e91SYunkang Tang {
77395f75e91SYunkang Tang 	struct alps_data *priv = psmouse->private;
77495f75e91SYunkang Tang 	unsigned char *packet = psmouse->packet;
77595f75e91SYunkang Tang 	struct input_dev *dev = psmouse->dev;
77695f75e91SYunkang Tang 	struct input_dev *dev2 = priv->dev2;
77795f75e91SYunkang Tang 	int x, y, z, left, right, middle;
77895f75e91SYunkang Tang 
77995f75e91SYunkang Tang 	/*
78095f75e91SYunkang Tang 	 * We can use Byte5 to distinguish if the packet is from Touchpad
78195f75e91SYunkang Tang 	 * or Trackpoint.
78295f75e91SYunkang Tang 	 * Touchpad:	0 - 0x7E
78395f75e91SYunkang Tang 	 * Trackpoint:	0x7F
78495f75e91SYunkang Tang 	 */
78595f75e91SYunkang Tang 	if (packet[5] == 0x7F) {
78695f75e91SYunkang Tang 		/* It should be a DualPoint when received Trackpoint packet */
78734412ba2SPali Rohár 		if (!(priv->flags & ALPS_DUALPOINT)) {
78834412ba2SPali Rohár 			psmouse_warn(psmouse,
78934412ba2SPali Rohár 				     "Rejected trackstick packet from non DualPoint device");
79095f75e91SYunkang Tang 			return;
79134412ba2SPali Rohár 		}
79295f75e91SYunkang Tang 
79395f75e91SYunkang Tang 		/* Trackpoint packet */
79495f75e91SYunkang Tang 		x = packet[1] | ((packet[3] & 0x20) << 2);
79595f75e91SYunkang Tang 		y = packet[2] | ((packet[3] & 0x40) << 1);
79695f75e91SYunkang Tang 		z = packet[4];
79795f75e91SYunkang Tang 		left = packet[3] & 0x01;
79895f75e91SYunkang Tang 		right = packet[3] & 0x02;
79995f75e91SYunkang Tang 		middle = packet[3] & 0x04;
80095f75e91SYunkang Tang 
80195f75e91SYunkang Tang 		/* To prevent the cursor jump when finger lifted */
80295f75e91SYunkang Tang 		if (x == 0x7F && y == 0x7F && z == 0x7F)
80395f75e91SYunkang Tang 			x = y = z = 0;
80495f75e91SYunkang Tang 
80595f75e91SYunkang Tang 		/* Divide 4 since trackpoint's speed is too fast */
80695f75e91SYunkang Tang 		input_report_rel(dev2, REL_X, (char)x / 4);
80795f75e91SYunkang Tang 		input_report_rel(dev2, REL_Y, -((char)y / 4));
80895f75e91SYunkang Tang 
80995f75e91SYunkang Tang 		input_report_key(dev2, BTN_LEFT, left);
81095f75e91SYunkang Tang 		input_report_key(dev2, BTN_RIGHT, right);
81195f75e91SYunkang Tang 		input_report_key(dev2, BTN_MIDDLE, middle);
81295f75e91SYunkang Tang 
81395f75e91SYunkang Tang 		input_sync(dev2);
81495f75e91SYunkang Tang 		return;
81595f75e91SYunkang Tang 	}
81695f75e91SYunkang Tang 
81795f75e91SYunkang Tang 	/* Touchpad packet */
81895f75e91SYunkang Tang 	x = packet[1] | ((packet[3] & 0x78) << 4);
81995f75e91SYunkang Tang 	y = packet[2] | ((packet[4] & 0x78) << 4);
82095f75e91SYunkang Tang 	z = packet[5];
82195f75e91SYunkang Tang 	left = packet[3] & 0x01;
82295f75e91SYunkang Tang 	right = packet[3] & 0x02;
82395f75e91SYunkang Tang 
82495f75e91SYunkang Tang 	if (z > 30)
82595f75e91SYunkang Tang 		input_report_key(dev, BTN_TOUCH, 1);
82695f75e91SYunkang Tang 	if (z < 25)
82795f75e91SYunkang Tang 		input_report_key(dev, BTN_TOUCH, 0);
82895f75e91SYunkang Tang 
82995f75e91SYunkang Tang 	if (z > 0) {
83095f75e91SYunkang Tang 		input_report_abs(dev, ABS_X, x);
83195f75e91SYunkang Tang 		input_report_abs(dev, ABS_Y, y);
83295f75e91SYunkang Tang 	}
83395f75e91SYunkang Tang 
83495f75e91SYunkang Tang 	input_report_abs(dev, ABS_PRESSURE, z);
83595f75e91SYunkang Tang 	input_report_key(dev, BTN_TOOL_FINGER, z > 0);
83695f75e91SYunkang Tang 
83795f75e91SYunkang Tang 	/* v6 touchpad does not have middle button */
83895f75e91SYunkang Tang 	input_report_key(dev, BTN_LEFT, left);
83995f75e91SYunkang Tang 	input_report_key(dev, BTN_RIGHT, right);
84095f75e91SYunkang Tang 
84195f75e91SYunkang Tang 	input_sync(dev);
84295f75e91SYunkang Tang }
84395f75e91SYunkang Tang 
84425bded7cSSeth Forshee static void alps_process_packet_v4(struct psmouse *psmouse)
84525bded7cSSeth Forshee {
8463b7e09faSGeorge Pantalos 	struct alps_data *priv = psmouse->private;
84725bded7cSSeth Forshee 	unsigned char *packet = psmouse->packet;
84802d04254SHans de Goede 	struct alps_fields *f = &priv->f;
84968c21870SHans de Goede 	int offset;
8503b7e09faSGeorge Pantalos 
8513b7e09faSGeorge Pantalos 	/*
8523b7e09faSGeorge Pantalos 	 * v4 has a 6-byte encoding for bitmap data, but this data is
8533b7e09faSGeorge Pantalos 	 * broken up between 3 normal packets. Use priv->multi_packet to
8543b7e09faSGeorge Pantalos 	 * track our position in the bitmap packet.
8553b7e09faSGeorge Pantalos 	 */
8563b7e09faSGeorge Pantalos 	if (packet[6] & 0x40) {
8573b7e09faSGeorge Pantalos 		/* sync, reset position */
8583b7e09faSGeorge Pantalos 		priv->multi_packet = 0;
8593b7e09faSGeorge Pantalos 	}
8603b7e09faSGeorge Pantalos 
8613b7e09faSGeorge Pantalos 	if (WARN_ON_ONCE(priv->multi_packet > 2))
8623b7e09faSGeorge Pantalos 		return;
8633b7e09faSGeorge Pantalos 
8643b7e09faSGeorge Pantalos 	offset = 2 * priv->multi_packet;
8653b7e09faSGeorge Pantalos 	priv->multi_data[offset] = packet[6];
8663b7e09faSGeorge Pantalos 	priv->multi_data[offset + 1] = packet[7];
8673b7e09faSGeorge Pantalos 
86844b77f38SHans de Goede 	f->left = !!(packet[4] & 0x01);
86944b77f38SHans de Goede 	f->right = !!(packet[4] & 0x02);
87044b77f38SHans de Goede 
87144b77f38SHans de Goede 	f->st.x = ((packet[1] & 0x7f) << 4) | ((packet[3] & 0x30) >> 2) |
87244b77f38SHans de Goede 		  ((packet[0] & 0x30) >> 4);
87344b77f38SHans de Goede 	f->st.y = ((packet[2] & 0x7f) << 4) | (packet[3] & 0x0f);
87444b77f38SHans de Goede 	f->pressure = packet[5] & 0x7f;
87544b77f38SHans de Goede 
8763b7e09faSGeorge Pantalos 	if (++priv->multi_packet > 2) {
8773b7e09faSGeorge Pantalos 		priv->multi_packet = 0;
8783b7e09faSGeorge Pantalos 
87902d04254SHans de Goede 		f->x_map = ((priv->multi_data[2] & 0x1f) << 10) |
8803b7e09faSGeorge Pantalos 			   ((priv->multi_data[3] & 0x60) << 3) |
8813b7e09faSGeorge Pantalos 			   ((priv->multi_data[0] & 0x3f) << 2) |
8823b7e09faSGeorge Pantalos 			   ((priv->multi_data[1] & 0x60) >> 5);
88302d04254SHans de Goede 		f->y_map = ((priv->multi_data[5] & 0x01) << 10) |
8843b7e09faSGeorge Pantalos 			   ((priv->multi_data[3] & 0x1f) << 5) |
8853b7e09faSGeorge Pantalos 			    (priv->multi_data[1] & 0x1f);
8863b7e09faSGeorge Pantalos 
88702d04254SHans de Goede 		f->fingers = alps_process_bitmap(priv, f);
8883b7e09faSGeorge Pantalos 	}
88925bded7cSSeth Forshee 
89068c21870SHans de Goede 	alps_report_semi_mt_data(psmouse, f->fingers);
89125bded7cSSeth Forshee }
89225bded7cSSeth Forshee 
8933808843cSYunkang Tang static bool alps_is_valid_package_v7(struct psmouse *psmouse)
8943808843cSYunkang Tang {
8953808843cSYunkang Tang 	switch (psmouse->pktcnt) {
8963808843cSYunkang Tang 	case 3:
8973808843cSYunkang Tang 		return (psmouse->packet[2] & 0x40) == 0x40;
8983808843cSYunkang Tang 	case 4:
8993808843cSYunkang Tang 		return (psmouse->packet[3] & 0x48) == 0x48;
9003808843cSYunkang Tang 	case 6:
9013808843cSYunkang Tang 		return (psmouse->packet[5] & 0x40) == 0x00;
9023808843cSYunkang Tang 	}
9033808843cSYunkang Tang 	return true;
9043808843cSYunkang Tang }
9053808843cSYunkang Tang 
9063808843cSYunkang Tang static unsigned char alps_get_packet_id_v7(char *byte)
9073808843cSYunkang Tang {
9083808843cSYunkang Tang 	unsigned char packet_id;
9093808843cSYunkang Tang 
9103808843cSYunkang Tang 	if (byte[4] & 0x40)
9113808843cSYunkang Tang 		packet_id = V7_PACKET_ID_TWO;
9123808843cSYunkang Tang 	else if (byte[4] & 0x01)
9133808843cSYunkang Tang 		packet_id = V7_PACKET_ID_MULTI;
9143808843cSYunkang Tang 	else if ((byte[0] & 0x10) && !(byte[4] & 0x43))
9153808843cSYunkang Tang 		packet_id = V7_PACKET_ID_NEW;
9163808843cSYunkang Tang 	else if (byte[1] == 0x00 && byte[4] == 0x00)
9173808843cSYunkang Tang 		packet_id = V7_PACKET_ID_IDLE;
9183808843cSYunkang Tang 	else
9193808843cSYunkang Tang 		packet_id = V7_PACKET_ID_UNKNOWN;
9203808843cSYunkang Tang 
9213808843cSYunkang Tang 	return packet_id;
9223808843cSYunkang Tang }
9233808843cSYunkang Tang 
9243808843cSYunkang Tang static void alps_get_finger_coordinate_v7(struct input_mt_pos *mt,
9253808843cSYunkang Tang 					  unsigned char *pkt,
9263808843cSYunkang Tang 					  unsigned char pkt_id)
9273808843cSYunkang Tang {
9283808843cSYunkang Tang 	mt[0].x = ((pkt[2] & 0x80) << 4);
9293808843cSYunkang Tang 	mt[0].x |= ((pkt[2] & 0x3F) << 5);
9303808843cSYunkang Tang 	mt[0].x |= ((pkt[3] & 0x30) >> 1);
9313808843cSYunkang Tang 	mt[0].x |= (pkt[3] & 0x07);
9323808843cSYunkang Tang 	mt[0].y = (pkt[1] << 3) | (pkt[0] & 0x07);
9333808843cSYunkang Tang 
9343808843cSYunkang Tang 	mt[1].x = ((pkt[3] & 0x80) << 4);
9353808843cSYunkang Tang 	mt[1].x |= ((pkt[4] & 0x80) << 3);
9363808843cSYunkang Tang 	mt[1].x |= ((pkt[4] & 0x3F) << 4);
9373808843cSYunkang Tang 	mt[1].y = ((pkt[5] & 0x80) << 3);
9383808843cSYunkang Tang 	mt[1].y |= ((pkt[5] & 0x3F) << 4);
9393808843cSYunkang Tang 
9403808843cSYunkang Tang 	switch (pkt_id) {
9413808843cSYunkang Tang 	case V7_PACKET_ID_TWO:
9423808843cSYunkang Tang 		mt[1].x &= ~0x000F;
9433808843cSYunkang Tang 		mt[1].y |= 0x000F;
9443808843cSYunkang Tang 		break;
9453808843cSYunkang Tang 
9463808843cSYunkang Tang 	case V7_PACKET_ID_MULTI:
9473808843cSYunkang Tang 		mt[1].x &= ~0x003F;
9483808843cSYunkang Tang 		mt[1].y &= ~0x0020;
9493808843cSYunkang Tang 		mt[1].y |= ((pkt[4] & 0x02) << 4);
9503808843cSYunkang Tang 		mt[1].y |= 0x001F;
9513808843cSYunkang Tang 		break;
9523808843cSYunkang Tang 
9533808843cSYunkang Tang 	case V7_PACKET_ID_NEW:
9543808843cSYunkang Tang 		mt[1].x &= ~0x003F;
9553808843cSYunkang Tang 		mt[1].x |= (pkt[0] & 0x20);
9563808843cSYunkang Tang 		mt[1].y |= 0x000F;
9573808843cSYunkang Tang 		break;
9583808843cSYunkang Tang 	}
9593808843cSYunkang Tang 
9603808843cSYunkang Tang 	mt[0].y = 0x7FF - mt[0].y;
9613808843cSYunkang Tang 	mt[1].y = 0x7FF - mt[1].y;
9623808843cSYunkang Tang }
9633808843cSYunkang Tang 
9643808843cSYunkang Tang static int alps_get_mt_count(struct input_mt_pos *mt)
9653808843cSYunkang Tang {
9667091c443SHans de Goede 	int i, fingers = 0;
9673808843cSYunkang Tang 
9687091c443SHans de Goede 	for (i = 0; i < MAX_TOUCHES; i++) {
9697091c443SHans de Goede 		if (mt[i].x != 0 || mt[i].y != 0)
9707091c443SHans de Goede 			fingers++;
9717091c443SHans de Goede 	}
9723808843cSYunkang Tang 
9737091c443SHans de Goede 	return fingers;
9743808843cSYunkang Tang }
9753808843cSYunkang Tang 
9763808843cSYunkang Tang static int alps_decode_packet_v7(struct alps_fields *f,
9773808843cSYunkang Tang 				  unsigned char *p,
9783808843cSYunkang Tang 				  struct psmouse *psmouse)
9793808843cSYunkang Tang {
980d27eb793SHans de Goede 	struct alps_data *priv = psmouse->private;
9813808843cSYunkang Tang 	unsigned char pkt_id;
9823808843cSYunkang Tang 
9833808843cSYunkang Tang 	pkt_id = alps_get_packet_id_v7(p);
9843808843cSYunkang Tang 	if (pkt_id == V7_PACKET_ID_IDLE)
9853808843cSYunkang Tang 		return 0;
9863808843cSYunkang Tang 	if (pkt_id == V7_PACKET_ID_UNKNOWN)
9873808843cSYunkang Tang 		return -1;
9888b238115SHans de Goede 	/*
9898b238115SHans de Goede 	 * NEW packets are send to indicate a discontinuity in the finger
9908b238115SHans de Goede 	 * coordinate reporting. Specifically a finger may have moved from
9918b238115SHans de Goede 	 * slot 0 to 1 or vice versa. INPUT_MT_TRACK takes care of this for
9928b238115SHans de Goede 	 * us.
9938b238115SHans de Goede 	 *
9948b238115SHans de Goede 	 * NEW packets have 3 problems:
9958b238115SHans de Goede 	 * 1) They do not contain middle / right button info (on non clickpads)
9968b238115SHans de Goede 	 *    this can be worked around by preserving the old button state
9978b238115SHans de Goede 	 * 2) They do not contain an accurate fingercount, and they are
9988b238115SHans de Goede 	 *    typically send when the number of fingers changes. We cannot use
9998b238115SHans de Goede 	 *    the old finger count as that may mismatch with the amount of
10008b238115SHans de Goede 	 *    touch coordinates we've available in the NEW packet
10018b238115SHans de Goede 	 * 3) Their x data for the second touch is inaccurate leading to
10028b238115SHans de Goede 	 *    a possible jump of the x coordinate by 16 units when the first
10038b238115SHans de Goede 	 *    non NEW packet comes in
10048b238115SHans de Goede 	 * Since problems 2 & 3 cannot be worked around, just ignore them.
10058b238115SHans de Goede 	 */
10068b238115SHans de Goede 	if (pkt_id == V7_PACKET_ID_NEW)
10078b238115SHans de Goede 		return 1;
10083808843cSYunkang Tang 
10093808843cSYunkang Tang 	alps_get_finger_coordinate_v7(f->mt, p, pkt_id);
10103808843cSYunkang Tang 
10113808843cSYunkang Tang 	if (pkt_id == V7_PACKET_ID_TWO)
10123808843cSYunkang Tang 		f->fingers = alps_get_mt_count(f->mt);
10138b238115SHans de Goede 	else /* pkt_id == V7_PACKET_ID_MULTI */
10143808843cSYunkang Tang 		f->fingers = 3 + (p[5] & 0x03);
10153808843cSYunkang Tang 
1016d27eb793SHans de Goede 	f->left = (p[0] & 0x80) >> 7;
1017d27eb793SHans de Goede 	if (priv->flags & ALPS_BUTTONPAD) {
1018d27eb793SHans de Goede 		if (p[0] & 0x20)
1019d27eb793SHans de Goede 			f->fingers++;
1020d27eb793SHans de Goede 		if (p[0] & 0x10)
1021d27eb793SHans de Goede 			f->fingers++;
1022d27eb793SHans de Goede 	} else {
1023d27eb793SHans de Goede 		f->right = (p[0] & 0x20) >> 5;
1024d27eb793SHans de Goede 		f->middle = (p[0] & 0x10) >> 4;
1025d27eb793SHans de Goede 	}
1026d27eb793SHans de Goede 
10277091c443SHans de Goede 	/* Sometimes a single touch is reported in mt[1] rather then mt[0] */
10287091c443SHans de Goede 	if (f->fingers == 1 && f->mt[0].x == 0 && f->mt[0].y == 0) {
10297091c443SHans de Goede 		f->mt[0].x = f->mt[1].x;
10307091c443SHans de Goede 		f->mt[0].y = f->mt[1].y;
10317091c443SHans de Goede 		f->mt[1].x = 0;
10327091c443SHans de Goede 		f->mt[1].y = 0;
10337091c443SHans de Goede 	}
10347091c443SHans de Goede 
10353808843cSYunkang Tang 	return 0;
10363808843cSYunkang Tang }
10373808843cSYunkang Tang 
10383808843cSYunkang Tang static void alps_process_trackstick_packet_v7(struct psmouse *psmouse)
10393808843cSYunkang Tang {
10403808843cSYunkang Tang 	struct alps_data *priv = psmouse->private;
10413808843cSYunkang Tang 	unsigned char *packet = psmouse->packet;
10423808843cSYunkang Tang 	struct input_dev *dev2 = priv->dev2;
10433808843cSYunkang Tang 	int x, y, z, left, right, middle;
10443808843cSYunkang Tang 
104534412ba2SPali Rohár 	/* It should be a DualPoint when received trackstick packet */
104634412ba2SPali Rohár 	if (!(priv->flags & ALPS_DUALPOINT)) {
104734412ba2SPali Rohár 		psmouse_warn(psmouse,
104834412ba2SPali Rohár 			     "Rejected trackstick packet from non DualPoint device");
104934412ba2SPali Rohár 		return;
105034412ba2SPali Rohár 	}
105134412ba2SPali Rohár 
10523808843cSYunkang Tang 	x = ((packet[2] & 0xbf)) | ((packet[3] & 0x10) << 2);
10533808843cSYunkang Tang 	y = (packet[3] & 0x07) | (packet[4] & 0xb8) |
10543808843cSYunkang Tang 	    ((packet[3] & 0x20) << 1);
10553808843cSYunkang Tang 	z = (packet[5] & 0x3f) | ((packet[3] & 0x80) >> 1);
10563808843cSYunkang Tang 
10573808843cSYunkang Tang 	left = (packet[1] & 0x01);
10583808843cSYunkang Tang 	right = (packet[1] & 0x02) >> 1;
10593808843cSYunkang Tang 	middle = (packet[1] & 0x04) >> 2;
10603808843cSYunkang Tang 
10613808843cSYunkang Tang 	/* Divide 2 since trackpoint's speed is too fast */
10623808843cSYunkang Tang 	input_report_rel(dev2, REL_X, (char)x / 2);
10633808843cSYunkang Tang 	input_report_rel(dev2, REL_Y, -((char)y / 2));
10643808843cSYunkang Tang 
10653808843cSYunkang Tang 	input_report_key(dev2, BTN_LEFT, left);
10663808843cSYunkang Tang 	input_report_key(dev2, BTN_RIGHT, right);
10673808843cSYunkang Tang 	input_report_key(dev2, BTN_MIDDLE, middle);
10683808843cSYunkang Tang 
10693808843cSYunkang Tang 	input_sync(dev2);
10703808843cSYunkang Tang }
10713808843cSYunkang Tang 
10723808843cSYunkang Tang static void alps_process_touchpad_packet_v7(struct psmouse *psmouse)
10733808843cSYunkang Tang {
10743808843cSYunkang Tang 	struct alps_data *priv = psmouse->private;
10753808843cSYunkang Tang 	struct input_dev *dev = psmouse->dev;
10763808843cSYunkang Tang 	struct alps_fields *f = &priv->f;
10773808843cSYunkang Tang 
10783808843cSYunkang Tang 	memset(f, 0, sizeof(*f));
10793808843cSYunkang Tang 
10803808843cSYunkang Tang 	if (priv->decode_fields(f, psmouse->packet, psmouse))
10813808843cSYunkang Tang 		return;
10823808843cSYunkang Tang 
10833808843cSYunkang Tang 	alps_report_mt_data(psmouse, alps_get_mt_count(f->mt));
10843808843cSYunkang Tang 
10853808843cSYunkang Tang 	input_mt_report_finger_count(dev, f->fingers);
10863808843cSYunkang Tang 
10873808843cSYunkang Tang 	input_report_key(dev, BTN_LEFT, f->left);
10883808843cSYunkang Tang 	input_report_key(dev, BTN_RIGHT, f->right);
10893808843cSYunkang Tang 	input_report_key(dev, BTN_MIDDLE, f->middle);
10903808843cSYunkang Tang 
10913808843cSYunkang Tang 	input_sync(dev);
10923808843cSYunkang Tang }
10933808843cSYunkang Tang 
10943808843cSYunkang Tang static void alps_process_packet_v7(struct psmouse *psmouse)
10953808843cSYunkang Tang {
10963808843cSYunkang Tang 	unsigned char *packet = psmouse->packet;
10973808843cSYunkang Tang 
10983808843cSYunkang Tang 	if (packet[0] == 0x48 && (packet[4] & 0x47) == 0x06)
10993808843cSYunkang Tang 		alps_process_trackstick_packet_v7(psmouse);
11003808843cSYunkang Tang 	else
11013808843cSYunkang Tang 		alps_process_touchpad_packet_v7(psmouse);
11023808843cSYunkang Tang }
11033808843cSYunkang Tang 
110407f19e3dSFengguang Wu static unsigned char alps_get_pkt_id_ss4_v2(unsigned char *byte)
11053db5b9f7SMasaki Ota {
11063db5b9f7SMasaki Ota 	unsigned char pkt_id = SS4_PACKET_ID_IDLE;
11073db5b9f7SMasaki Ota 
11083db5b9f7SMasaki Ota 	if (byte[0] == 0x18 && byte[1] == 0x10 && byte[2] == 0x00 &&
11093db5b9f7SMasaki Ota 	    (byte[3] & 0x88) == 0x08 && byte[4] == 0x10 && byte[5] == 0x00) {
11103db5b9f7SMasaki Ota 		pkt_id = SS4_PACKET_ID_IDLE;
11113db5b9f7SMasaki Ota 	} else if (!(byte[3] & 0x10)) {
11123db5b9f7SMasaki Ota 		pkt_id = SS4_PACKET_ID_ONE;
11133db5b9f7SMasaki Ota 	} else if (!(byte[3] & 0x20)) {
11143db5b9f7SMasaki Ota 		pkt_id = SS4_PACKET_ID_TWO;
11153db5b9f7SMasaki Ota 	} else {
11163db5b9f7SMasaki Ota 		pkt_id = SS4_PACKET_ID_MULTI;
11173db5b9f7SMasaki Ota 	}
11183db5b9f7SMasaki Ota 
11193db5b9f7SMasaki Ota 	return pkt_id;
11203db5b9f7SMasaki Ota }
11213db5b9f7SMasaki Ota 
11223db5b9f7SMasaki Ota static int alps_decode_ss4_v2(struct alps_fields *f,
11233db5b9f7SMasaki Ota 			      unsigned char *p, struct psmouse *psmouse)
11243db5b9f7SMasaki Ota {
11253db5b9f7SMasaki Ota 	struct alps_data *priv = psmouse->private;
11263db5b9f7SMasaki Ota 	unsigned char pkt_id;
11273db5b9f7SMasaki Ota 	unsigned int no_data_x, no_data_y;
11283db5b9f7SMasaki Ota 
11293db5b9f7SMasaki Ota 	pkt_id = alps_get_pkt_id_ss4_v2(p);
11303db5b9f7SMasaki Ota 
11313db5b9f7SMasaki Ota 	/* Current packet is 1Finger coordinate packet */
11323db5b9f7SMasaki Ota 	switch (pkt_id) {
11333db5b9f7SMasaki Ota 	case SS4_PACKET_ID_ONE:
11343db5b9f7SMasaki Ota 		f->mt[0].x = SS4_1F_X_V2(p);
11353db5b9f7SMasaki Ota 		f->mt[0].y = SS4_1F_Y_V2(p);
11363db5b9f7SMasaki Ota 		f->pressure = ((SS4_1F_Z_V2(p)) * 2) & 0x7f;
11373db5b9f7SMasaki Ota 		f->fingers = 1;
11383db5b9f7SMasaki Ota 		f->first_mp = 0;
11393db5b9f7SMasaki Ota 		f->is_mp = 0;
11403db5b9f7SMasaki Ota 		break;
11413db5b9f7SMasaki Ota 
11423db5b9f7SMasaki Ota 	case SS4_PACKET_ID_TWO:
11433db5b9f7SMasaki Ota 		if (priv->flags & ALPS_BUTTONPAD) {
11443db5b9f7SMasaki Ota 			f->mt[0].x = SS4_BTL_MF_X_V2(p, 0);
11453db5b9f7SMasaki Ota 			f->mt[0].y = SS4_BTL_MF_Y_V2(p, 0);
11463db5b9f7SMasaki Ota 			f->mt[1].x = SS4_BTL_MF_X_V2(p, 1);
11473db5b9f7SMasaki Ota 			f->mt[1].y = SS4_BTL_MF_Y_V2(p, 1);
11483db5b9f7SMasaki Ota 		} else {
11493db5b9f7SMasaki Ota 			f->mt[0].x = SS4_STD_MF_X_V2(p, 0);
11503db5b9f7SMasaki Ota 			f->mt[0].y = SS4_STD_MF_Y_V2(p, 0);
11513db5b9f7SMasaki Ota 			f->mt[1].x = SS4_STD_MF_X_V2(p, 1);
11523db5b9f7SMasaki Ota 			f->mt[1].y = SS4_STD_MF_Y_V2(p, 1);
11533db5b9f7SMasaki Ota 		}
11543db5b9f7SMasaki Ota 		f->pressure = SS4_MF_Z_V2(p, 0) ? 0x30 : 0;
11553db5b9f7SMasaki Ota 
11563db5b9f7SMasaki Ota 		if (SS4_IS_MF_CONTINUE(p)) {
11573db5b9f7SMasaki Ota 			f->first_mp = 1;
11583db5b9f7SMasaki Ota 		} else {
11593db5b9f7SMasaki Ota 			f->fingers = 2;
11603db5b9f7SMasaki Ota 			f->first_mp = 0;
11613db5b9f7SMasaki Ota 		}
11623db5b9f7SMasaki Ota 		f->is_mp = 0;
11633db5b9f7SMasaki Ota 
11643db5b9f7SMasaki Ota 		break;
11653db5b9f7SMasaki Ota 
11663db5b9f7SMasaki Ota 	case SS4_PACKET_ID_MULTI:
11673db5b9f7SMasaki Ota 		if (priv->flags & ALPS_BUTTONPAD) {
11683db5b9f7SMasaki Ota 			f->mt[2].x = SS4_BTL_MF_X_V2(p, 0);
11693db5b9f7SMasaki Ota 			f->mt[2].y = SS4_BTL_MF_Y_V2(p, 0);
11703db5b9f7SMasaki Ota 			f->mt[3].x = SS4_BTL_MF_X_V2(p, 1);
11713db5b9f7SMasaki Ota 			f->mt[3].y = SS4_BTL_MF_Y_V2(p, 1);
11723db5b9f7SMasaki Ota 			no_data_x = SS4_MFPACKET_NO_AX_BL;
11733db5b9f7SMasaki Ota 			no_data_y = SS4_MFPACKET_NO_AY_BL;
11743db5b9f7SMasaki Ota 		} else {
11753db5b9f7SMasaki Ota 			f->mt[2].x = SS4_STD_MF_X_V2(p, 0);
11763db5b9f7SMasaki Ota 			f->mt[2].y = SS4_STD_MF_Y_V2(p, 0);
11773db5b9f7SMasaki Ota 			f->mt[3].x = SS4_STD_MF_X_V2(p, 1);
11783db5b9f7SMasaki Ota 			f->mt[3].y = SS4_STD_MF_Y_V2(p, 1);
11793db5b9f7SMasaki Ota 			no_data_x = SS4_MFPACKET_NO_AX;
11803db5b9f7SMasaki Ota 			no_data_y = SS4_MFPACKET_NO_AY;
11813db5b9f7SMasaki Ota 		}
11823db5b9f7SMasaki Ota 
11833db5b9f7SMasaki Ota 		f->first_mp = 0;
11843db5b9f7SMasaki Ota 		f->is_mp = 1;
11853db5b9f7SMasaki Ota 
11863db5b9f7SMasaki Ota 		if (SS4_IS_5F_DETECTED(p)) {
11873db5b9f7SMasaki Ota 			f->fingers = 5;
11883db5b9f7SMasaki Ota 		} else if (f->mt[3].x == no_data_x &&
11893db5b9f7SMasaki Ota 			     f->mt[3].y == no_data_y) {
11903db5b9f7SMasaki Ota 			f->mt[3].x = 0;
11913db5b9f7SMasaki Ota 			f->mt[3].y = 0;
11923db5b9f7SMasaki Ota 			f->fingers = 3;
11933db5b9f7SMasaki Ota 		} else {
11943db5b9f7SMasaki Ota 			f->fingers = 4;
11953db5b9f7SMasaki Ota 		}
11963db5b9f7SMasaki Ota 		break;
11973db5b9f7SMasaki Ota 
11983db5b9f7SMasaki Ota 	case SS4_PACKET_ID_IDLE:
11993db5b9f7SMasaki Ota 	default:
12003db5b9f7SMasaki Ota 		memset(f, 0, sizeof(struct alps_fields));
12013db5b9f7SMasaki Ota 		break;
12023db5b9f7SMasaki Ota 	}
12033db5b9f7SMasaki Ota 
12043db5b9f7SMasaki Ota 	f->left = !!(SS4_BTN_V2(p) & 0x01);
12053db5b9f7SMasaki Ota 	if (!(priv->flags & ALPS_BUTTONPAD)) {
12063db5b9f7SMasaki Ota 		f->right = !!(SS4_BTN_V2(p) & 0x02);
12073db5b9f7SMasaki Ota 		f->middle = !!(SS4_BTN_V2(p) & 0x04);
12083db5b9f7SMasaki Ota 	}
12093db5b9f7SMasaki Ota 
12103db5b9f7SMasaki Ota 	return 0;
12113db5b9f7SMasaki Ota }
12123db5b9f7SMasaki Ota 
12133db5b9f7SMasaki Ota static void alps_process_packet_ss4_v2(struct psmouse *psmouse)
12143db5b9f7SMasaki Ota {
12153db5b9f7SMasaki Ota 	struct alps_data *priv = psmouse->private;
12163db5b9f7SMasaki Ota 	unsigned char *packet = psmouse->packet;
12173db5b9f7SMasaki Ota 	struct input_dev *dev = psmouse->dev;
12183db5b9f7SMasaki Ota 	struct alps_fields *f = &priv->f;
12193db5b9f7SMasaki Ota 
12203db5b9f7SMasaki Ota 	memset(f, 0, sizeof(struct alps_fields));
12213db5b9f7SMasaki Ota 	priv->decode_fields(f, packet, psmouse);
12223db5b9f7SMasaki Ota 	if (priv->multi_packet) {
12233db5b9f7SMasaki Ota 		/*
12243db5b9f7SMasaki Ota 		 * Sometimes the first packet will indicate a multi-packet
12253db5b9f7SMasaki Ota 		 * sequence, but sometimes the next multi-packet would not
12263db5b9f7SMasaki Ota 		 * come. Check for this, and when it happens process the
12273db5b9f7SMasaki Ota 		 * position packet as usual.
12283db5b9f7SMasaki Ota 		 */
12293db5b9f7SMasaki Ota 		if (f->is_mp) {
12303db5b9f7SMasaki Ota 			/* Now process the 1st packet */
12313db5b9f7SMasaki Ota 			priv->decode_fields(f, priv->multi_data, psmouse);
12323db5b9f7SMasaki Ota 		} else {
12333db5b9f7SMasaki Ota 			priv->multi_packet = 0;
12343db5b9f7SMasaki Ota 		}
12353db5b9f7SMasaki Ota 	}
12363db5b9f7SMasaki Ota 
12373db5b9f7SMasaki Ota 	/*
12383db5b9f7SMasaki Ota 	 * "f.is_mp" would always be '0' after merging the 1st and 2nd packet.
12393db5b9f7SMasaki Ota 	 * When it is set, it means 2nd packet comes without 1st packet come.
12403db5b9f7SMasaki Ota 	 */
12413db5b9f7SMasaki Ota 	if (f->is_mp)
12423db5b9f7SMasaki Ota 		return;
12433db5b9f7SMasaki Ota 
12443db5b9f7SMasaki Ota 	/* Save the first packet */
12453db5b9f7SMasaki Ota 	if (!priv->multi_packet && f->first_mp) {
12463db5b9f7SMasaki Ota 		priv->multi_packet = 1;
12473db5b9f7SMasaki Ota 		memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
12483db5b9f7SMasaki Ota 		return;
12493db5b9f7SMasaki Ota 	}
12503db5b9f7SMasaki Ota 
12513db5b9f7SMasaki Ota 	priv->multi_packet = 0;
12523db5b9f7SMasaki Ota 
12533db5b9f7SMasaki Ota 	alps_report_mt_data(psmouse, (f->fingers <= 4) ? f->fingers : 4);
12543db5b9f7SMasaki Ota 
12553db5b9f7SMasaki Ota 	input_mt_report_finger_count(dev, f->fingers);
12563db5b9f7SMasaki Ota 
12573db5b9f7SMasaki Ota 	input_report_key(dev, BTN_LEFT, f->left);
12583db5b9f7SMasaki Ota 	input_report_key(dev, BTN_RIGHT, f->right);
12593db5b9f7SMasaki Ota 	input_report_key(dev, BTN_MIDDLE, f->middle);
12603db5b9f7SMasaki Ota 
12613db5b9f7SMasaki Ota 	input_report_abs(dev, ABS_PRESSURE, f->pressure);
12623db5b9f7SMasaki Ota 	input_sync(dev);
12633db5b9f7SMasaki Ota }
12643db5b9f7SMasaki Ota 
12653db5b9f7SMasaki Ota static bool alps_is_valid_package_ss4_v2(struct psmouse *psmouse)
12663db5b9f7SMasaki Ota {
12673db5b9f7SMasaki Ota 	if (psmouse->pktcnt == 4 && ((psmouse->packet[3] & 0x08) != 0x08))
12683db5b9f7SMasaki Ota 		return false;
12693db5b9f7SMasaki Ota 	if (psmouse->pktcnt == 6 && ((psmouse->packet[5] & 0x10) != 0x0))
12703db5b9f7SMasaki Ota 		return false;
12713db5b9f7SMasaki Ota 	return true;
12723db5b9f7SMasaki Ota }
12733db5b9f7SMasaki Ota 
127404aae283SPali Rohár static DEFINE_MUTEX(alps_mutex);
127504aae283SPali Rohár 
127604aae283SPali Rohár static void alps_register_bare_ps2_mouse(struct work_struct *work)
127704aae283SPali Rohár {
127804aae283SPali Rohár 	struct alps_data *priv =
127904aae283SPali Rohár 		container_of(work, struct alps_data, dev3_register_work.work);
128004aae283SPali Rohár 	struct psmouse *psmouse = priv->psmouse;
128104aae283SPali Rohár 	struct input_dev *dev3;
128204aae283SPali Rohár 	int error = 0;
128304aae283SPali Rohár 
128404aae283SPali Rohár 	mutex_lock(&alps_mutex);
128504aae283SPali Rohár 
128604aae283SPali Rohár 	if (priv->dev3)
128704aae283SPali Rohár 		goto out;
128804aae283SPali Rohár 
128904aae283SPali Rohár 	dev3 = input_allocate_device();
129004aae283SPali Rohár 	if (!dev3) {
129104aae283SPali Rohár 		psmouse_err(psmouse, "failed to allocate secondary device\n");
129204aae283SPali Rohár 		error = -ENOMEM;
129304aae283SPali Rohár 		goto out;
129404aae283SPali Rohár 	}
129504aae283SPali Rohár 
129604aae283SPali Rohár 	snprintf(priv->phys3, sizeof(priv->phys3), "%s/%s",
129704aae283SPali Rohár 		 psmouse->ps2dev.serio->phys,
129804aae283SPali Rohár 		 (priv->dev2 ? "input2" : "input1"));
129904aae283SPali Rohár 	dev3->phys = priv->phys3;
130004aae283SPali Rohár 
130104aae283SPali Rohár 	/*
130204aae283SPali Rohár 	 * format of input device name is: "protocol vendor name"
130304aae283SPali Rohár 	 * see function psmouse_switch_protocol() in psmouse-base.c
130404aae283SPali Rohár 	 */
130504aae283SPali Rohár 	dev3->name = "PS/2 ALPS Mouse";
130604aae283SPali Rohár 
130704aae283SPali Rohár 	dev3->id.bustype = BUS_I8042;
130804aae283SPali Rohár 	dev3->id.vendor  = 0x0002;
130904aae283SPali Rohár 	dev3->id.product = PSMOUSE_PS2;
131004aae283SPali Rohár 	dev3->id.version = 0x0000;
131104aae283SPali Rohár 	dev3->dev.parent = &psmouse->ps2dev.serio->dev;
131204aae283SPali Rohár 
131304aae283SPali Rohár 	input_set_capability(dev3, EV_REL, REL_X);
131404aae283SPali Rohár 	input_set_capability(dev3, EV_REL, REL_Y);
131504aae283SPali Rohár 	input_set_capability(dev3, EV_KEY, BTN_LEFT);
131604aae283SPali Rohár 	input_set_capability(dev3, EV_KEY, BTN_RIGHT);
131704aae283SPali Rohár 	input_set_capability(dev3, EV_KEY, BTN_MIDDLE);
131804aae283SPali Rohár 
131904aae283SPali Rohár 	__set_bit(INPUT_PROP_POINTER, dev3->propbit);
132004aae283SPali Rohár 
132104aae283SPali Rohár 	error = input_register_device(dev3);
132204aae283SPali Rohár 	if (error) {
132304aae283SPali Rohár 		psmouse_err(psmouse,
132404aae283SPali Rohár 			    "failed to register secondary device: %d\n",
132504aae283SPali Rohár 			    error);
132604aae283SPali Rohár 		input_free_device(dev3);
132704aae283SPali Rohár 		goto out;
132804aae283SPali Rohár 	}
132904aae283SPali Rohár 
133004aae283SPali Rohár 	priv->dev3 = dev3;
133104aae283SPali Rohár 
133204aae283SPali Rohár out:
133304aae283SPali Rohár 	/*
133404aae283SPali Rohár 	 * Save the error code so that we can detect that we
133504aae283SPali Rohár 	 * already tried to create the device.
133604aae283SPali Rohár 	 */
133704aae283SPali Rohár 	if (error)
133804aae283SPali Rohár 		priv->dev3 = ERR_PTR(error);
133904aae283SPali Rohár 
134004aae283SPali Rohár 	mutex_unlock(&alps_mutex);
134104aae283SPali Rohár }
134204aae283SPali Rohár 
134359c30afbSHans de Goede static void alps_report_bare_ps2_packet(struct psmouse *psmouse,
13441d9f2626SSebastian Kapfer 					unsigned char packet[],
13451d9f2626SSebastian Kapfer 					bool report_buttons)
13461d9f2626SSebastian Kapfer {
134759c30afbSHans de Goede 	struct alps_data *priv = psmouse->private;
134859c30afbSHans de Goede 	struct input_dev *dev;
134959c30afbSHans de Goede 
1350e3a79212SHans de Goede 	/* Figure out which device to use to report the bare packet */
1351e3a79212SHans de Goede 	if (priv->proto_version == ALPS_PROTO_V2 &&
1352e3a79212SHans de Goede 	    (priv->flags & ALPS_DUALPOINT)) {
1353e3a79212SHans de Goede 		/* On V2 devices the DualPoint Stick reports bare packets */
1354e3a79212SHans de Goede 		dev = priv->dev2;
1355e3a79212SHans de Goede 	} else if (unlikely(IS_ERR_OR_NULL(priv->dev3))) {
135659c30afbSHans de Goede 		/* Register dev3 mouse if we received PS/2 packet first time */
135759c30afbSHans de Goede 		if (!IS_ERR(priv->dev3))
135859c30afbSHans de Goede 			psmouse_queue_work(psmouse, &priv->dev3_register_work,
135959c30afbSHans de Goede 					   0);
136059c30afbSHans de Goede 		return;
136159c30afbSHans de Goede 	} else {
136259c30afbSHans de Goede 		dev = priv->dev3;
136359c30afbSHans de Goede 	}
136459c30afbSHans de Goede 
13651d9f2626SSebastian Kapfer 	if (report_buttons)
136604aae283SPali Rohár 		alps_report_buttons(dev, NULL,
13671d9f2626SSebastian Kapfer 				packet[0] & 1, packet[0] & 2, packet[0] & 4);
13681d9f2626SSebastian Kapfer 
136904aae283SPali Rohár 	input_report_rel(dev, REL_X,
13701d9f2626SSebastian Kapfer 		packet[1] ? packet[1] - ((packet[0] << 4) & 0x100) : 0);
137104aae283SPali Rohár 	input_report_rel(dev, REL_Y,
13721d9f2626SSebastian Kapfer 		packet[2] ? ((packet[0] << 3) & 0x100) - packet[2] : 0);
13731d9f2626SSebastian Kapfer 
137404aae283SPali Rohár 	input_sync(dev);
13751d9f2626SSebastian Kapfer }
13761d9f2626SSebastian Kapfer 
13771d9f2626SSebastian Kapfer static psmouse_ret_t alps_handle_interleaved_ps2(struct psmouse *psmouse)
13781da177e4SLinus Torvalds {
13791da177e4SLinus Torvalds 	struct alps_data *priv = psmouse->private;
13801da177e4SLinus Torvalds 
13811d9f2626SSebastian Kapfer 	if (psmouse->pktcnt < 6)
13821d9f2626SSebastian Kapfer 		return PSMOUSE_GOOD_DATA;
13831d9f2626SSebastian Kapfer 
13841d9f2626SSebastian Kapfer 	if (psmouse->pktcnt == 6) {
13851d9f2626SSebastian Kapfer 		/*
13861d9f2626SSebastian Kapfer 		 * Start a timer to flush the packet if it ends up last
13871d9f2626SSebastian Kapfer 		 * 6-byte packet in the stream. Timer needs to fire
13881d9f2626SSebastian Kapfer 		 * psmouse core times out itself. 20 ms should be enough
13891d9f2626SSebastian Kapfer 		 * to decide if we are getting more data or not.
13901d9f2626SSebastian Kapfer 		 */
13911d9f2626SSebastian Kapfer 		mod_timer(&priv->timer, jiffies + msecs_to_jiffies(20));
13921d9f2626SSebastian Kapfer 		return PSMOUSE_GOOD_DATA;
13931d9f2626SSebastian Kapfer 	}
13941d9f2626SSebastian Kapfer 
13951d9f2626SSebastian Kapfer 	del_timer(&priv->timer);
13961d9f2626SSebastian Kapfer 
13971d9f2626SSebastian Kapfer 	if (psmouse->packet[6] & 0x80) {
13981d9f2626SSebastian Kapfer 
13991d9f2626SSebastian Kapfer 		/*
14001d9f2626SSebastian Kapfer 		 * Highest bit is set - that means we either had
14011d9f2626SSebastian Kapfer 		 * complete ALPS packet and this is start of the
14021d9f2626SSebastian Kapfer 		 * next packet or we got garbage.
14031d9f2626SSebastian Kapfer 		 */
14041d9f2626SSebastian Kapfer 
14051d9f2626SSebastian Kapfer 		if (((psmouse->packet[3] |
14061d9f2626SSebastian Kapfer 		      psmouse->packet[4] |
14071d9f2626SSebastian Kapfer 		      psmouse->packet[5]) & 0x80) ||
140899df65e7SKevin Cernekee 		    (!alps_is_valid_first_byte(priv, psmouse->packet[6]))) {
1409b5d21704SDmitry Torokhov 			psmouse_dbg(psmouse,
14103b112923SAndy Shevchenko 				    "refusing packet %4ph (suspected interleaved ps/2)\n",
14113b112923SAndy Shevchenko 				    psmouse->packet + 3);
14121d9f2626SSebastian Kapfer 			return PSMOUSE_BAD_DATA;
14131d9f2626SSebastian Kapfer 		}
14141d9f2626SSebastian Kapfer 
141524af5cb9SKevin Cernekee 		priv->process_packet(psmouse);
14161d9f2626SSebastian Kapfer 
14171d9f2626SSebastian Kapfer 		/* Continue with the next packet */
14181d9f2626SSebastian Kapfer 		psmouse->packet[0] = psmouse->packet[6];
14191d9f2626SSebastian Kapfer 		psmouse->pktcnt = 1;
14201d9f2626SSebastian Kapfer 
14211d9f2626SSebastian Kapfer 	} else {
14221d9f2626SSebastian Kapfer 
14231d9f2626SSebastian Kapfer 		/*
14241d9f2626SSebastian Kapfer 		 * High bit is 0 - that means that we indeed got a PS/2
14251d9f2626SSebastian Kapfer 		 * packet in the middle of ALPS packet.
14261d9f2626SSebastian Kapfer 		 *
14271d9f2626SSebastian Kapfer 		 * There is also possibility that we got 6-byte ALPS
14281d9f2626SSebastian Kapfer 		 * packet followed  by 3-byte packet from trackpoint. We
14291d9f2626SSebastian Kapfer 		 * can not distinguish between these 2 scenarios but
1430b5d21704SDmitry Torokhov 		 * because the latter is unlikely to happen in course of
14311d9f2626SSebastian Kapfer 		 * normal operation (user would need to press all
14321d9f2626SSebastian Kapfer 		 * buttons on the pad and start moving trackpoint
14331d9f2626SSebastian Kapfer 		 * without touching the pad surface) we assume former.
14341d9f2626SSebastian Kapfer 		 * Even if we are wrong the wost thing that would happen
14351d9f2626SSebastian Kapfer 		 * the cursor would jump but we should not get protocol
1436b5d21704SDmitry Torokhov 		 * de-synchronization.
14371d9f2626SSebastian Kapfer 		 */
14381d9f2626SSebastian Kapfer 
143959c30afbSHans de Goede 		alps_report_bare_ps2_packet(psmouse, &psmouse->packet[3],
144059c30afbSHans de Goede 					    false);
14411d9f2626SSebastian Kapfer 
14421d9f2626SSebastian Kapfer 		/*
14431d9f2626SSebastian Kapfer 		 * Continue with the standard ALPS protocol handling,
14441d9f2626SSebastian Kapfer 		 * but make sure we won't process it as an interleaved
14451d9f2626SSebastian Kapfer 		 * packet again, which may happen if all buttons are
14461d9f2626SSebastian Kapfer 		 * pressed. To avoid this let's reset the 4th bit which
14471d9f2626SSebastian Kapfer 		 * is normally 1.
14481d9f2626SSebastian Kapfer 		 */
14491d9f2626SSebastian Kapfer 		psmouse->packet[3] = psmouse->packet[6] & 0xf7;
14501d9f2626SSebastian Kapfer 		psmouse->pktcnt = 4;
14511d9f2626SSebastian Kapfer 	}
14521d9f2626SSebastian Kapfer 
14531d9f2626SSebastian Kapfer 	return PSMOUSE_GOOD_DATA;
14541d9f2626SSebastian Kapfer }
14551d9f2626SSebastian Kapfer 
14561d9f2626SSebastian Kapfer static void alps_flush_packet(unsigned long data)
14571d9f2626SSebastian Kapfer {
14581d9f2626SSebastian Kapfer 	struct psmouse *psmouse = (struct psmouse *)data;
145924af5cb9SKevin Cernekee 	struct alps_data *priv = psmouse->private;
14601d9f2626SSebastian Kapfer 
14611d9f2626SSebastian Kapfer 	serio_pause_rx(psmouse->ps2dev.serio);
14621d9f2626SSebastian Kapfer 
1463b46615feSSeth Forshee 	if (psmouse->pktcnt == psmouse->pktsize) {
14641d9f2626SSebastian Kapfer 
14651d9f2626SSebastian Kapfer 		/*
14661d9f2626SSebastian Kapfer 		 * We did not any more data in reasonable amount of time.
14671d9f2626SSebastian Kapfer 		 * Validate the last 3 bytes and process as a standard
14681d9f2626SSebastian Kapfer 		 * ALPS packet.
14691d9f2626SSebastian Kapfer 		 */
14701d9f2626SSebastian Kapfer 		if ((psmouse->packet[3] |
14711d9f2626SSebastian Kapfer 		     psmouse->packet[4] |
14721d9f2626SSebastian Kapfer 		     psmouse->packet[5]) & 0x80) {
1473b5d21704SDmitry Torokhov 			psmouse_dbg(psmouse,
14743b112923SAndy Shevchenko 				    "refusing packet %3ph (suspected interleaved ps/2)\n",
14753b112923SAndy Shevchenko 				    psmouse->packet + 3);
14761d9f2626SSebastian Kapfer 		} else {
147724af5cb9SKevin Cernekee 			priv->process_packet(psmouse);
14781d9f2626SSebastian Kapfer 		}
14791d9f2626SSebastian Kapfer 		psmouse->pktcnt = 0;
14801d9f2626SSebastian Kapfer 	}
14811d9f2626SSebastian Kapfer 
14821d9f2626SSebastian Kapfer 	serio_continue_rx(psmouse->ps2dev.serio);
14831d9f2626SSebastian Kapfer }
14841d9f2626SSebastian Kapfer 
14851d9f2626SSebastian Kapfer static psmouse_ret_t alps_process_byte(struct psmouse *psmouse)
14861d9f2626SSebastian Kapfer {
14871d9f2626SSebastian Kapfer 	struct alps_data *priv = psmouse->private;
14881d9f2626SSebastian Kapfer 
14894ab8f7f3SPali Rohár 	/*
14904ab8f7f3SPali Rohár 	 * Check if we are dealing with a bare PS/2 packet, presumably from
14914ab8f7f3SPali Rohár 	 * a device connected to the external PS/2 port. Because bare PS/2
14924ab8f7f3SPali Rohár 	 * protocol does not have enough constant bits to self-synchronize
14934ab8f7f3SPali Rohár 	 * properly we only do this if the device is fully synchronized.
14943db5b9f7SMasaki Ota 	 * Can not distinguish V8's first byte from PS/2 packet's
14954ab8f7f3SPali Rohár 	 */
14963db5b9f7SMasaki Ota 	if (priv->proto_version != ALPS_PROTO_V8 &&
14973db5b9f7SMasaki Ota 	    !psmouse->out_of_sync_cnt &&
14983db5b9f7SMasaki Ota 	    (psmouse->packet[0] & 0xc8) == 0x08) {
14993db5b9f7SMasaki Ota 
15001da177e4SLinus Torvalds 		if (psmouse->pktcnt == 3) {
150159c30afbSHans de Goede 			alps_report_bare_ps2_packet(psmouse, psmouse->packet,
15021d9f2626SSebastian Kapfer 						    true);
15031da177e4SLinus Torvalds 			return PSMOUSE_FULL_PACKET;
15041da177e4SLinus Torvalds 		}
15051da177e4SLinus Torvalds 		return PSMOUSE_GOOD_DATA;
15061da177e4SLinus Torvalds 	}
15071da177e4SLinus Torvalds 
15081d9f2626SSebastian Kapfer 	/* Check for PS/2 packet stuffed in the middle of ALPS packet. */
15091d9f2626SSebastian Kapfer 
151099df65e7SKevin Cernekee 	if ((priv->flags & ALPS_PS2_INTERLEAVED) &&
15111d9f2626SSebastian Kapfer 	    psmouse->pktcnt >= 4 && (psmouse->packet[3] & 0x0f) == 0x0f) {
15121d9f2626SSebastian Kapfer 		return alps_handle_interleaved_ps2(psmouse);
15131d9f2626SSebastian Kapfer 	}
15141d9f2626SSebastian Kapfer 
151599df65e7SKevin Cernekee 	if (!alps_is_valid_first_byte(priv, psmouse->packet[0])) {
1516b5d21704SDmitry Torokhov 		psmouse_dbg(psmouse,
1517b5d21704SDmitry Torokhov 			    "refusing packet[0] = %x (mask0 = %x, byte0 = %x)\n",
151899df65e7SKevin Cernekee 			    psmouse->packet[0], priv->mask0, priv->byte0);
15191da177e4SLinus Torvalds 		return PSMOUSE_BAD_DATA;
15201d9f2626SSebastian Kapfer 	}
15211da177e4SLinus Torvalds 
1522b46615feSSeth Forshee 	/* Bytes 2 - pktsize should have 0 in the highest bit */
1523a7ef82aeSPali Rohár 	if (priv->proto_version < ALPS_PROTO_V5 &&
152475af9e56SDave Turvene 	    psmouse->pktcnt >= 2 && psmouse->pktcnt <= psmouse->pktsize &&
15251d9f2626SSebastian Kapfer 	    (psmouse->packet[psmouse->pktcnt - 1] & 0x80)) {
1526b5d21704SDmitry Torokhov 		psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
1527b5d21704SDmitry Torokhov 			    psmouse->pktcnt - 1,
1528b5d21704SDmitry Torokhov 			    psmouse->packet[psmouse->pktcnt - 1]);
1529a7ef82aeSPali Rohár 
1530fb2dd7a6SDmitry Torokhov 		if (priv->proto_version == ALPS_PROTO_V3_RUSHMORE &&
1531a7ef82aeSPali Rohár 		    psmouse->pktcnt == psmouse->pktsize) {
1532a7ef82aeSPali Rohár 			/*
1533a7ef82aeSPali Rohár 			 * Some Dell boxes, such as Latitude E6440 or E7440
1534a7ef82aeSPali Rohár 			 * with closed lid, quite often smash last byte of
1535a7ef82aeSPali Rohár 			 * otherwise valid packet with 0xff. Given that the
1536a7ef82aeSPali Rohár 			 * next packet is very likely to be valid let's
1537a7ef82aeSPali Rohár 			 * report PSMOUSE_FULL_PACKET but not process data,
1538a7ef82aeSPali Rohár 			 * rather than reporting PSMOUSE_BAD_DATA and
1539a7ef82aeSPali Rohár 			 * filling the logs.
1540a7ef82aeSPali Rohár 			 */
1541a7ef82aeSPali Rohár 			return PSMOUSE_FULL_PACKET;
1542a7ef82aeSPali Rohár 		}
1543a7ef82aeSPali Rohár 
15441da177e4SLinus Torvalds 		return PSMOUSE_BAD_DATA;
15451d9f2626SSebastian Kapfer 	}
15461da177e4SLinus Torvalds 
15473db5b9f7SMasaki Ota 	if ((priv->proto_version == ALPS_PROTO_V7 &&
15483db5b9f7SMasaki Ota 			!alps_is_valid_package_v7(psmouse)) ||
15493db5b9f7SMasaki Ota 	    (priv->proto_version == ALPS_PROTO_V8 &&
15503db5b9f7SMasaki Ota 			!alps_is_valid_package_ss4_v2(psmouse))) {
15513808843cSYunkang Tang 		psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
15523808843cSYunkang Tang 			    psmouse->pktcnt - 1,
15533808843cSYunkang Tang 			    psmouse->packet[psmouse->pktcnt - 1]);
15543808843cSYunkang Tang 		return PSMOUSE_BAD_DATA;
15553808843cSYunkang Tang 	}
15563808843cSYunkang Tang 
1557b46615feSSeth Forshee 	if (psmouse->pktcnt == psmouse->pktsize) {
155824af5cb9SKevin Cernekee 		priv->process_packet(psmouse);
15591da177e4SLinus Torvalds 		return PSMOUSE_FULL_PACKET;
15601da177e4SLinus Torvalds 	}
15611da177e4SLinus Torvalds 
15621da177e4SLinus Torvalds 	return PSMOUSE_GOOD_DATA;
15631da177e4SLinus Torvalds }
15641da177e4SLinus Torvalds 
156525bded7cSSeth Forshee static int alps_command_mode_send_nibble(struct psmouse *psmouse, int nibble)
156625bded7cSSeth Forshee {
156725bded7cSSeth Forshee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
156825bded7cSSeth Forshee 	struct alps_data *priv = psmouse->private;
156925bded7cSSeth Forshee 	int command;
157025bded7cSSeth Forshee 	unsigned char *param;
157125bded7cSSeth Forshee 	unsigned char dummy[4];
157225bded7cSSeth Forshee 
157325bded7cSSeth Forshee 	BUG_ON(nibble > 0xf);
157425bded7cSSeth Forshee 
157525bded7cSSeth Forshee 	command = priv->nibble_commands[nibble].command;
157625bded7cSSeth Forshee 	param = (command & 0x0f00) ?
157725bded7cSSeth Forshee 		dummy : (unsigned char *)&priv->nibble_commands[nibble].data;
157825bded7cSSeth Forshee 
157925bded7cSSeth Forshee 	if (ps2_command(ps2dev, param, command))
158025bded7cSSeth Forshee 		return -1;
158125bded7cSSeth Forshee 
158225bded7cSSeth Forshee 	return 0;
158325bded7cSSeth Forshee }
158425bded7cSSeth Forshee 
158525bded7cSSeth Forshee static int alps_command_mode_set_addr(struct psmouse *psmouse, int addr)
158625bded7cSSeth Forshee {
158725bded7cSSeth Forshee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
158825bded7cSSeth Forshee 	struct alps_data *priv = psmouse->private;
158925bded7cSSeth Forshee 	int i, nibble;
159025bded7cSSeth Forshee 
159125bded7cSSeth Forshee 	if (ps2_command(ps2dev, NULL, priv->addr_command))
159225bded7cSSeth Forshee 		return -1;
159325bded7cSSeth Forshee 
159425bded7cSSeth Forshee 	for (i = 12; i >= 0; i -= 4) {
159525bded7cSSeth Forshee 		nibble = (addr >> i) & 0xf;
159625bded7cSSeth Forshee 		if (alps_command_mode_send_nibble(psmouse, nibble))
159725bded7cSSeth Forshee 			return -1;
159825bded7cSSeth Forshee 	}
159925bded7cSSeth Forshee 
160025bded7cSSeth Forshee 	return 0;
160125bded7cSSeth Forshee }
160225bded7cSSeth Forshee 
160325bded7cSSeth Forshee static int __alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
160425bded7cSSeth Forshee {
160525bded7cSSeth Forshee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
160625bded7cSSeth Forshee 	unsigned char param[4];
160725bded7cSSeth Forshee 
160825bded7cSSeth Forshee 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
160925bded7cSSeth Forshee 		return -1;
161025bded7cSSeth Forshee 
161125bded7cSSeth Forshee 	/*
161225bded7cSSeth Forshee 	 * The address being read is returned in the first two bytes
161325bded7cSSeth Forshee 	 * of the result. Check that this address matches the expected
161425bded7cSSeth Forshee 	 * address.
161525bded7cSSeth Forshee 	 */
161625bded7cSSeth Forshee 	if (addr != ((param[0] << 8) | param[1]))
161725bded7cSSeth Forshee 		return -1;
161825bded7cSSeth Forshee 
161925bded7cSSeth Forshee 	return param[2];
162025bded7cSSeth Forshee }
162125bded7cSSeth Forshee 
162225bded7cSSeth Forshee static int alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
162325bded7cSSeth Forshee {
162425bded7cSSeth Forshee 	if (alps_command_mode_set_addr(psmouse, addr))
162525bded7cSSeth Forshee 		return -1;
162625bded7cSSeth Forshee 	return __alps_command_mode_read_reg(psmouse, addr);
162725bded7cSSeth Forshee }
162825bded7cSSeth Forshee 
162925bded7cSSeth Forshee static int __alps_command_mode_write_reg(struct psmouse *psmouse, u8 value)
163025bded7cSSeth Forshee {
163125bded7cSSeth Forshee 	if (alps_command_mode_send_nibble(psmouse, (value >> 4) & 0xf))
163225bded7cSSeth Forshee 		return -1;
163325bded7cSSeth Forshee 	if (alps_command_mode_send_nibble(psmouse, value & 0xf))
163425bded7cSSeth Forshee 		return -1;
163525bded7cSSeth Forshee 	return 0;
163625bded7cSSeth Forshee }
163725bded7cSSeth Forshee 
163825bded7cSSeth Forshee static int alps_command_mode_write_reg(struct psmouse *psmouse, int addr,
163925bded7cSSeth Forshee 				       u8 value)
164025bded7cSSeth Forshee {
164125bded7cSSeth Forshee 	if (alps_command_mode_set_addr(psmouse, addr))
164225bded7cSSeth Forshee 		return -1;
164325bded7cSSeth Forshee 	return __alps_command_mode_write_reg(psmouse, value);
164425bded7cSSeth Forshee }
164525bded7cSSeth Forshee 
164624ba9707SKevin Cernekee static int alps_rpt_cmd(struct psmouse *psmouse, int init_command,
164724ba9707SKevin Cernekee 			int repeated_command, unsigned char *param)
164824ba9707SKevin Cernekee {
164924ba9707SKevin Cernekee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
165024ba9707SKevin Cernekee 
165124ba9707SKevin Cernekee 	param[0] = 0;
165224ba9707SKevin Cernekee 	if (init_command && ps2_command(ps2dev, param, init_command))
165324ba9707SKevin Cernekee 		return -EIO;
165424ba9707SKevin Cernekee 
165524ba9707SKevin Cernekee 	if (ps2_command(ps2dev,  NULL, repeated_command) ||
165624ba9707SKevin Cernekee 	    ps2_command(ps2dev,  NULL, repeated_command) ||
165724ba9707SKevin Cernekee 	    ps2_command(ps2dev,  NULL, repeated_command))
165824ba9707SKevin Cernekee 		return -EIO;
165924ba9707SKevin Cernekee 
166024ba9707SKevin Cernekee 	param[0] = param[1] = param[2] = 0xff;
166124ba9707SKevin Cernekee 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
166224ba9707SKevin Cernekee 		return -EIO;
166324ba9707SKevin Cernekee 
166439fbe585SDmitry Torokhov 	psmouse_dbg(psmouse, "%2.2X report: %3ph\n",
166539fbe585SDmitry Torokhov 		    repeated_command, param);
166624ba9707SKevin Cernekee 	return 0;
166724ba9707SKevin Cernekee }
166824ba9707SKevin Cernekee 
16693808843cSYunkang Tang static bool alps_check_valid_firmware_id(unsigned char id[])
16703808843cSYunkang Tang {
16713808843cSYunkang Tang 	if (id[0] == 0x73)
16723808843cSYunkang Tang 		return true;
16733808843cSYunkang Tang 
16743808843cSYunkang Tang 	if (id[0] == 0x88 &&
16753808843cSYunkang Tang 	    (id[1] == 0x07 ||
16763808843cSYunkang Tang 	     id[1] == 0x08 ||
16773808843cSYunkang Tang 	     (id[1] & 0xf0) == 0xb0 ||
16783808843cSYunkang Tang 	     (id[1] & 0xf0) == 0xc0)) {
16793808843cSYunkang Tang 		return true;
16803808843cSYunkang Tang 	}
16813808843cSYunkang Tang 
16823808843cSYunkang Tang 	return false;
16833808843cSYunkang Tang }
16843808843cSYunkang Tang 
1685d18e53fcSKevin Cernekee static int alps_enter_command_mode(struct psmouse *psmouse)
168625bded7cSSeth Forshee {
168725bded7cSSeth Forshee 	unsigned char param[4];
168825bded7cSSeth Forshee 
168924ba9707SKevin Cernekee 	if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_RESET_WRAP, param)) {
169025bded7cSSeth Forshee 		psmouse_err(psmouse, "failed to enter command mode\n");
169125bded7cSSeth Forshee 		return -1;
169225bded7cSSeth Forshee 	}
169325bded7cSSeth Forshee 
16943808843cSYunkang Tang 	if (!alps_check_valid_firmware_id(param)) {
169525bded7cSSeth Forshee 		psmouse_dbg(psmouse,
169624ba9707SKevin Cernekee 			    "unknown response while entering command mode\n");
169725bded7cSSeth Forshee 		return -1;
169825bded7cSSeth Forshee 	}
169925bded7cSSeth Forshee 	return 0;
170025bded7cSSeth Forshee }
170125bded7cSSeth Forshee 
170225bded7cSSeth Forshee static inline int alps_exit_command_mode(struct psmouse *psmouse)
170325bded7cSSeth Forshee {
170425bded7cSSeth Forshee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
170525bded7cSSeth Forshee 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM))
170625bded7cSSeth Forshee 		return -1;
170725bded7cSSeth Forshee 	return 0;
170825bded7cSSeth Forshee }
170925bded7cSSeth Forshee 
17101da177e4SLinus Torvalds /*
17111da177e4SLinus Torvalds  * For DualPoint devices select the device that should respond to
17121da177e4SLinus Torvalds  * subsequent commands. It looks like glidepad is behind stickpointer,
17131da177e4SLinus Torvalds  * I'd thought it would be other way around...
17141da177e4SLinus Torvalds  */
171525bded7cSSeth Forshee static int alps_passthrough_mode_v2(struct psmouse *psmouse, bool enable)
17161da177e4SLinus Torvalds {
17171da177e4SLinus Torvalds 	struct ps2dev *ps2dev = &psmouse->ps2dev;
17181da177e4SLinus Torvalds 	int cmd = enable ? PSMOUSE_CMD_SETSCALE21 : PSMOUSE_CMD_SETSCALE11;
17191da177e4SLinus Torvalds 
17201da177e4SLinus Torvalds 	if (ps2_command(ps2dev, NULL, cmd) ||
17211da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, cmd) ||
17221da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, cmd) ||
17231da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
17241da177e4SLinus Torvalds 		return -1;
17251da177e4SLinus Torvalds 
17261da177e4SLinus Torvalds 	/* we may get 3 more bytes, just ignore them */
1727c611763dSDmitry Torokhov 	ps2_drain(ps2dev, 3, 100);
17281da177e4SLinus Torvalds 
17291da177e4SLinus Torvalds 	return 0;
17301da177e4SLinus Torvalds }
17311da177e4SLinus Torvalds 
173225bded7cSSeth Forshee static int alps_absolute_mode_v1_v2(struct psmouse *psmouse)
17331da177e4SLinus Torvalds {
17341da177e4SLinus Torvalds 	struct ps2dev *ps2dev = &psmouse->ps2dev;
17351da177e4SLinus Torvalds 
17361da177e4SLinus Torvalds 	/* Try ALPS magic knock - 4 disable before enable */
17371da177e4SLinus Torvalds 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
17381da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
17391da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
17401da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
17411da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
17421da177e4SLinus Torvalds 		return -1;
17431da177e4SLinus Torvalds 
17441da177e4SLinus Torvalds 	/*
17451da177e4SLinus Torvalds 	 * Switch mouse to poll (remote) mode so motion data will not
17461da177e4SLinus Torvalds 	 * get in our way
17471da177e4SLinus Torvalds 	 */
17481da177e4SLinus Torvalds 	return ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETPOLL);
17491da177e4SLinus Torvalds }
17501da177e4SLinus Torvalds 
175195f75e91SYunkang Tang static int alps_monitor_mode_send_word(struct psmouse *psmouse, u16 word)
175295f75e91SYunkang Tang {
175395f75e91SYunkang Tang 	int i, nibble;
175495f75e91SYunkang Tang 
175595f75e91SYunkang Tang 	/*
175695f75e91SYunkang Tang 	 * b0-b11 are valid bits, send sequence is inverse.
175795f75e91SYunkang Tang 	 * e.g. when word = 0x0123, nibble send sequence is 3, 2, 1
175895f75e91SYunkang Tang 	 */
175995f75e91SYunkang Tang 	for (i = 0; i <= 8; i += 4) {
176095f75e91SYunkang Tang 		nibble = (word >> i) & 0xf;
176195f75e91SYunkang Tang 		if (alps_command_mode_send_nibble(psmouse, nibble))
176295f75e91SYunkang Tang 			return -1;
176395f75e91SYunkang Tang 	}
176495f75e91SYunkang Tang 
176595f75e91SYunkang Tang 	return 0;
176695f75e91SYunkang Tang }
176795f75e91SYunkang Tang 
176895f75e91SYunkang Tang static int alps_monitor_mode_write_reg(struct psmouse *psmouse,
176995f75e91SYunkang Tang 				       u16 addr, u16 value)
177095f75e91SYunkang Tang {
177195f75e91SYunkang Tang 	struct ps2dev *ps2dev = &psmouse->ps2dev;
177295f75e91SYunkang Tang 
177395f75e91SYunkang Tang 	/* 0x0A0 is the command to write the word */
177495f75e91SYunkang Tang 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE) ||
177595f75e91SYunkang Tang 	    alps_monitor_mode_send_word(psmouse, 0x0A0) ||
177695f75e91SYunkang Tang 	    alps_monitor_mode_send_word(psmouse, addr) ||
177795f75e91SYunkang Tang 	    alps_monitor_mode_send_word(psmouse, value) ||
177895f75e91SYunkang Tang 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
177995f75e91SYunkang Tang 		return -1;
178095f75e91SYunkang Tang 
178195f75e91SYunkang Tang 	return 0;
178295f75e91SYunkang Tang }
178395f75e91SYunkang Tang 
178495f75e91SYunkang Tang static int alps_monitor_mode(struct psmouse *psmouse, bool enable)
178595f75e91SYunkang Tang {
178695f75e91SYunkang Tang 	struct ps2dev *ps2dev = &psmouse->ps2dev;
178795f75e91SYunkang Tang 
178895f75e91SYunkang Tang 	if (enable) {
178995f75e91SYunkang Tang 		/* EC E9 F5 F5 E7 E6 E7 E9 to enter monitor mode */
179095f75e91SYunkang Tang 		if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
179195f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO) ||
179295f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
179395f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
179495f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
179595f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
179695f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
179795f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO))
179895f75e91SYunkang Tang 			return -1;
179995f75e91SYunkang Tang 	} else {
180095f75e91SYunkang Tang 		/* EC to exit monitor mode */
180195f75e91SYunkang Tang 		if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP))
180295f75e91SYunkang Tang 			return -1;
180395f75e91SYunkang Tang 	}
180495f75e91SYunkang Tang 
180595f75e91SYunkang Tang 	return 0;
180695f75e91SYunkang Tang }
180795f75e91SYunkang Tang 
180895f75e91SYunkang Tang static int alps_absolute_mode_v6(struct psmouse *psmouse)
180995f75e91SYunkang Tang {
181095f75e91SYunkang Tang 	u16 reg_val = 0x181;
181195f75e91SYunkang Tang 	int ret = -1;
181295f75e91SYunkang Tang 
181395f75e91SYunkang Tang 	/* enter monitor mode, to write the register */
181495f75e91SYunkang Tang 	if (alps_monitor_mode(psmouse, true))
181595f75e91SYunkang Tang 		return -1;
181695f75e91SYunkang Tang 
181795f75e91SYunkang Tang 	ret = alps_monitor_mode_write_reg(psmouse, 0x000, reg_val);
181895f75e91SYunkang Tang 
181995f75e91SYunkang Tang 	if (alps_monitor_mode(psmouse, false))
182095f75e91SYunkang Tang 		ret = -1;
182195f75e91SYunkang Tang 
182295f75e91SYunkang Tang 	return ret;
182395f75e91SYunkang Tang }
182495f75e91SYunkang Tang 
18251da177e4SLinus Torvalds static int alps_get_status(struct psmouse *psmouse, char *param)
18261da177e4SLinus Torvalds {
18271da177e4SLinus Torvalds 	/* Get status: 0xF5 0xF5 0xF5 0xE9 */
182824ba9707SKevin Cernekee 	if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_DISABLE, param))
18291da177e4SLinus Torvalds 		return -1;
18301da177e4SLinus Torvalds 
18311da177e4SLinus Torvalds 	return 0;
18321da177e4SLinus Torvalds }
18331da177e4SLinus Torvalds 
18341da177e4SLinus Torvalds /*
18351da177e4SLinus Torvalds  * Turn touchpad tapping on or off. The sequences are:
18361da177e4SLinus Torvalds  * 0xE9 0xF5 0xF5 0xF3 0x0A to enable,
18371da177e4SLinus Torvalds  * 0xE9 0xF5 0xF5 0xE8 0x00 to disable.
18381da177e4SLinus Torvalds  * My guess that 0xE9 (GetInfo) is here as a sync point.
18391da177e4SLinus Torvalds  * For models that also have stickpointer (DualPoints) its tapping
18401da177e4SLinus Torvalds  * is controlled separately (0xE6 0xE6 0xE6 0xF3 0x14|0x0A) but
18411da177e4SLinus Torvalds  * we don't fiddle with it.
18421da177e4SLinus Torvalds  */
18431da177e4SLinus Torvalds static int alps_tap_mode(struct psmouse *psmouse, int enable)
18441da177e4SLinus Torvalds {
18451da177e4SLinus Torvalds 	struct ps2dev *ps2dev = &psmouse->ps2dev;
18461da177e4SLinus Torvalds 	int cmd = enable ? PSMOUSE_CMD_SETRATE : PSMOUSE_CMD_SETRES;
18471da177e4SLinus Torvalds 	unsigned char tap_arg = enable ? 0x0A : 0x00;
18481da177e4SLinus Torvalds 	unsigned char param[4];
18491da177e4SLinus Torvalds 
18501da177e4SLinus Torvalds 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO) ||
18511da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
18521da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
18531da177e4SLinus Torvalds 	    ps2_command(ps2dev, &tap_arg, cmd))
18541da177e4SLinus Torvalds 		return -1;
18551da177e4SLinus Torvalds 
18561da177e4SLinus Torvalds 	if (alps_get_status(psmouse, param))
18571da177e4SLinus Torvalds 		return -1;
18581da177e4SLinus Torvalds 
18591da177e4SLinus Torvalds 	return 0;
18601da177e4SLinus Torvalds }
18611da177e4SLinus Torvalds 
1862f0d5c6f4SDmitry Torokhov /*
1863f0d5c6f4SDmitry Torokhov  * alps_poll() - poll the touchpad for current motion packet.
1864f0d5c6f4SDmitry Torokhov  * Used in resync.
1865f0d5c6f4SDmitry Torokhov  */
1866f0d5c6f4SDmitry Torokhov static int alps_poll(struct psmouse *psmouse)
1867f0d5c6f4SDmitry Torokhov {
1868f0d5c6f4SDmitry Torokhov 	struct alps_data *priv = psmouse->private;
1869b46615feSSeth Forshee 	unsigned char buf[sizeof(psmouse->packet)];
1870b7802c5cSDmitry Torokhov 	bool poll_failed;
1871f0d5c6f4SDmitry Torokhov 
187299df65e7SKevin Cernekee 	if (priv->flags & ALPS_PASS)
187325bded7cSSeth Forshee 		alps_passthrough_mode_v2(psmouse, true);
1874f0d5c6f4SDmitry Torokhov 
1875f0d5c6f4SDmitry Torokhov 	poll_failed = ps2_command(&psmouse->ps2dev, buf,
1876f0d5c6f4SDmitry Torokhov 				  PSMOUSE_CMD_POLL | (psmouse->pktsize << 8)) < 0;
1877f0d5c6f4SDmitry Torokhov 
187899df65e7SKevin Cernekee 	if (priv->flags & ALPS_PASS)
187925bded7cSSeth Forshee 		alps_passthrough_mode_v2(psmouse, false);
1880f0d5c6f4SDmitry Torokhov 
188199df65e7SKevin Cernekee 	if (poll_failed || (buf[0] & priv->mask0) != priv->byte0)
1882f0d5c6f4SDmitry Torokhov 		return -1;
1883f0d5c6f4SDmitry Torokhov 
1884f0d5c6f4SDmitry Torokhov 	if ((psmouse->badbyte & 0xc8) == 0x08) {
1885f0d5c6f4SDmitry Torokhov /*
1886f0d5c6f4SDmitry Torokhov  * Poll the track stick ...
1887f0d5c6f4SDmitry Torokhov  */
1888f0d5c6f4SDmitry Torokhov 		if (ps2_command(&psmouse->ps2dev, buf, PSMOUSE_CMD_POLL | (3 << 8)))
1889f0d5c6f4SDmitry Torokhov 			return -1;
1890f0d5c6f4SDmitry Torokhov 	}
1891f0d5c6f4SDmitry Torokhov 
1892f0d5c6f4SDmitry Torokhov 	memcpy(psmouse->packet, buf, sizeof(buf));
1893f0d5c6f4SDmitry Torokhov 	return 0;
1894f0d5c6f4SDmitry Torokhov }
1895f0d5c6f4SDmitry Torokhov 
189625bded7cSSeth Forshee static int alps_hw_init_v1_v2(struct psmouse *psmouse)
18971da177e4SLinus Torvalds {
18981da177e4SLinus Torvalds 	struct alps_data *priv = psmouse->private;
18991da177e4SLinus Torvalds 
190099df65e7SKevin Cernekee 	if ((priv->flags & ALPS_PASS) &&
190125bded7cSSeth Forshee 	    alps_passthrough_mode_v2(psmouse, true)) {
19021da177e4SLinus Torvalds 		return -1;
1903b7802c5cSDmitry Torokhov 	}
19041da177e4SLinus Torvalds 
1905b7802c5cSDmitry Torokhov 	if (alps_tap_mode(psmouse, true)) {
1906b5d21704SDmitry Torokhov 		psmouse_warn(psmouse, "Failed to enable hardware tapping\n");
19071da177e4SLinus Torvalds 		return -1;
1908963f626dSPeter Osterlund 	}
19091da177e4SLinus Torvalds 
191025bded7cSSeth Forshee 	if (alps_absolute_mode_v1_v2(psmouse)) {
1911b5d21704SDmitry Torokhov 		psmouse_err(psmouse, "Failed to enable absolute mode\n");
19121da177e4SLinus Torvalds 		return -1;
19131da177e4SLinus Torvalds 	}
19141da177e4SLinus Torvalds 
191599df65e7SKevin Cernekee 	if ((priv->flags & ALPS_PASS) &&
191625bded7cSSeth Forshee 	    alps_passthrough_mode_v2(psmouse, false)) {
19171da177e4SLinus Torvalds 		return -1;
1918b7802c5cSDmitry Torokhov 	}
19191da177e4SLinus Torvalds 
19201e0c5b12SDmitry Torokhov 	/* ALPS needs stream mode, otherwise it won't report any data */
19211e0c5b12SDmitry Torokhov 	if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)) {
1922b5d21704SDmitry Torokhov 		psmouse_err(psmouse, "Failed to enable stream mode\n");
19231e0c5b12SDmitry Torokhov 		return -1;
19241e0c5b12SDmitry Torokhov 	}
19251e0c5b12SDmitry Torokhov 
19261e0c5b12SDmitry Torokhov 	return 0;
19271e0c5b12SDmitry Torokhov }
19281e0c5b12SDmitry Torokhov 
192995f75e91SYunkang Tang static int alps_hw_init_v6(struct psmouse *psmouse)
193095f75e91SYunkang Tang {
193195f75e91SYunkang Tang 	unsigned char param[2] = {0xC8, 0x14};
193295f75e91SYunkang Tang 
193395f75e91SYunkang Tang 	/* Enter passthrough mode to let trackpoint enter 6byte raw mode */
193495f75e91SYunkang Tang 	if (alps_passthrough_mode_v2(psmouse, true))
193595f75e91SYunkang Tang 		return -1;
193695f75e91SYunkang Tang 
193795f75e91SYunkang Tang 	if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
193895f75e91SYunkang Tang 	    ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
193995f75e91SYunkang Tang 	    ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
194095f75e91SYunkang Tang 	    ps2_command(&psmouse->ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
194195f75e91SYunkang Tang 	    ps2_command(&psmouse->ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
194295f75e91SYunkang Tang 		return -1;
194395f75e91SYunkang Tang 
194495f75e91SYunkang Tang 	if (alps_passthrough_mode_v2(psmouse, false))
194595f75e91SYunkang Tang 		return -1;
194695f75e91SYunkang Tang 
194795f75e91SYunkang Tang 	if (alps_absolute_mode_v6(psmouse)) {
194895f75e91SYunkang Tang 		psmouse_err(psmouse, "Failed to enable absolute mode\n");
194995f75e91SYunkang Tang 		return -1;
195095f75e91SYunkang Tang 	}
195195f75e91SYunkang Tang 
195295f75e91SYunkang Tang 	return 0;
195395f75e91SYunkang Tang }
195495f75e91SYunkang Tang 
195525bded7cSSeth Forshee /*
1956cd401204SKevin Cernekee  * Enable or disable passthrough mode to the trackstick.
195725bded7cSSeth Forshee  */
1958cd401204SKevin Cernekee static int alps_passthrough_mode_v3(struct psmouse *psmouse,
1959cd401204SKevin Cernekee 				    int reg_base, bool enable)
196025bded7cSSeth Forshee {
1961cd401204SKevin Cernekee 	int reg_val, ret = -1;
196225bded7cSSeth Forshee 
1963d18e53fcSKevin Cernekee 	if (alps_enter_command_mode(psmouse))
196425bded7cSSeth Forshee 		return -1;
196525bded7cSSeth Forshee 
1966cd401204SKevin Cernekee 	reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x0008);
1967cd401204SKevin Cernekee 	if (reg_val == -1)
1968cd401204SKevin Cernekee 		goto error;
1969cd401204SKevin Cernekee 
197025bded7cSSeth Forshee 	if (enable)
197125bded7cSSeth Forshee 		reg_val |= 0x01;
197225bded7cSSeth Forshee 	else
197325bded7cSSeth Forshee 		reg_val &= ~0x01;
197425bded7cSSeth Forshee 
1975cd401204SKevin Cernekee 	ret = __alps_command_mode_write_reg(psmouse, reg_val);
197625bded7cSSeth Forshee 
1977cd401204SKevin Cernekee error:
1978cd401204SKevin Cernekee 	if (alps_exit_command_mode(psmouse))
1979cd401204SKevin Cernekee 		ret = -1;
1980cd401204SKevin Cernekee 	return ret;
198125bded7cSSeth Forshee }
198225bded7cSSeth Forshee 
198325bded7cSSeth Forshee /* Must be in command mode when calling this function */
198425bded7cSSeth Forshee static int alps_absolute_mode_v3(struct psmouse *psmouse)
198525bded7cSSeth Forshee {
198625bded7cSSeth Forshee 	int reg_val;
198725bded7cSSeth Forshee 
198825bded7cSSeth Forshee 	reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
198925bded7cSSeth Forshee 	if (reg_val == -1)
199025bded7cSSeth Forshee 		return -1;
199125bded7cSSeth Forshee 
199225bded7cSSeth Forshee 	reg_val |= 0x06;
199325bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, reg_val))
199425bded7cSSeth Forshee 		return -1;
199525bded7cSSeth Forshee 
199625bded7cSSeth Forshee 	return 0;
199725bded7cSSeth Forshee }
199825bded7cSSeth Forshee 
1999cd401204SKevin Cernekee static int alps_probe_trackstick_v3(struct psmouse *psmouse, int reg_base)
200025bded7cSSeth Forshee {
2001cd401204SKevin Cernekee 	int ret = -EIO, reg_val;
200225bded7cSSeth Forshee 
2003d18e53fcSKevin Cernekee 	if (alps_enter_command_mode(psmouse))
200425bded7cSSeth Forshee 		goto error;
200525bded7cSSeth Forshee 
2006cd401204SKevin Cernekee 	reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x08);
200725bded7cSSeth Forshee 	if (reg_val == -1)
200825bded7cSSeth Forshee 		goto error;
2009cd401204SKevin Cernekee 
2010cd401204SKevin Cernekee 	/* bit 7: trackstick is present */
2011cd401204SKevin Cernekee 	ret = reg_val & 0x80 ? 0 : -ENODEV;
2012cd401204SKevin Cernekee 
2013cd401204SKevin Cernekee error:
2014cd401204SKevin Cernekee 	alps_exit_command_mode(psmouse);
2015cd401204SKevin Cernekee 	return ret;
2016cd401204SKevin Cernekee }
2017cd401204SKevin Cernekee 
2018cd401204SKevin Cernekee static int alps_setup_trackstick_v3(struct psmouse *psmouse, int reg_base)
2019cd401204SKevin Cernekee {
2020cd401204SKevin Cernekee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
2021cd401204SKevin Cernekee 	int ret = 0;
2022cd401204SKevin Cernekee 	unsigned char param[4];
2023cd401204SKevin Cernekee 
2024cd401204SKevin Cernekee 	if (alps_passthrough_mode_v3(psmouse, reg_base, true))
2025cd401204SKevin Cernekee 		return -EIO;
202625bded7cSSeth Forshee 
202725bded7cSSeth Forshee 	/*
202825bded7cSSeth Forshee 	 * E7 report for the trackstick
202925bded7cSSeth Forshee 	 *
203025bded7cSSeth Forshee 	 * There have been reports of failures to seem to trace back
203125bded7cSSeth Forshee 	 * to the above trackstick check failing. When these occur
203225bded7cSSeth Forshee 	 * this E7 report fails, so when that happens we continue
203325bded7cSSeth Forshee 	 * with the assumption that there isn't a trackstick after
203425bded7cSSeth Forshee 	 * all.
203525bded7cSSeth Forshee 	 */
2036cd401204SKevin Cernekee 	if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_SETSCALE21, param)) {
2037a09221e8SDmitry Torokhov 		psmouse_warn(psmouse, "Failed to initialize trackstick (E7 report failed)\n");
2038cd401204SKevin Cernekee 		ret = -ENODEV;
203925bded7cSSeth Forshee 	} else {
204039fbe585SDmitry Torokhov 		psmouse_dbg(psmouse, "trackstick E7 report: %3ph\n", param);
204125bded7cSSeth Forshee 
204225bded7cSSeth Forshee 		/*
204325bded7cSSeth Forshee 		 * Not sure what this does, but it is absolutely
204425bded7cSSeth Forshee 		 * essential. Without it, the touchpad does not
204525bded7cSSeth Forshee 		 * work at all and the trackstick just emits normal
204625bded7cSSeth Forshee 		 * PS/2 packets.
204725bded7cSSeth Forshee 		 */
204825bded7cSSeth Forshee 		if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
204925bded7cSSeth Forshee 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
205025bded7cSSeth Forshee 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
205125bded7cSSeth Forshee 		    alps_command_mode_send_nibble(psmouse, 0x9) ||
205225bded7cSSeth Forshee 		    alps_command_mode_send_nibble(psmouse, 0x4)) {
205325bded7cSSeth Forshee 			psmouse_err(psmouse,
205425bded7cSSeth Forshee 				    "Error sending magic E6 sequence\n");
2055cd401204SKevin Cernekee 			ret = -EIO;
205625bded7cSSeth Forshee 			goto error;
205725bded7cSSeth Forshee 		}
205825bded7cSSeth Forshee 
2059cd401204SKevin Cernekee 		/*
2060cd401204SKevin Cernekee 		 * This ensures the trackstick packets are in the format
2061cd401204SKevin Cernekee 		 * supported by this driver. If bit 1 isn't set the packet
2062cd401204SKevin Cernekee 		 * format is different.
2063cd401204SKevin Cernekee 		 */
2064d18e53fcSKevin Cernekee 		if (alps_enter_command_mode(psmouse) ||
2065cd401204SKevin Cernekee 		    alps_command_mode_write_reg(psmouse,
2066cd401204SKevin Cernekee 						reg_base + 0x08, 0x82) ||
2067cd401204SKevin Cernekee 		    alps_exit_command_mode(psmouse))
2068cd401204SKevin Cernekee 			ret = -EIO;
2069cd401204SKevin Cernekee 	}
2070cd401204SKevin Cernekee 
2071cd401204SKevin Cernekee error:
2072cd401204SKevin Cernekee 	if (alps_passthrough_mode_v3(psmouse, reg_base, false))
2073cd401204SKevin Cernekee 		ret = -EIO;
2074cd401204SKevin Cernekee 
2075cd401204SKevin Cernekee 	return ret;
2076cd401204SKevin Cernekee }
2077cd401204SKevin Cernekee 
2078cd401204SKevin Cernekee static int alps_hw_init_v3(struct psmouse *psmouse)
2079cd401204SKevin Cernekee {
2080cd401204SKevin Cernekee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
2081cd401204SKevin Cernekee 	int reg_val;
2082cd401204SKevin Cernekee 	unsigned char param[4];
2083cd401204SKevin Cernekee 
2084cd401204SKevin Cernekee 	reg_val = alps_probe_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE);
2085cd401204SKevin Cernekee 	if (reg_val == -EIO)
2086cd401204SKevin Cernekee 		goto error;
208739fbe585SDmitry Torokhov 
2088cd401204SKevin Cernekee 	if (reg_val == 0 &&
2089cd401204SKevin Cernekee 	    alps_setup_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE) == -EIO)
2090cd401204SKevin Cernekee 		goto error;
2091cd401204SKevin Cernekee 
2092d18e53fcSKevin Cernekee 	if (alps_enter_command_mode(psmouse) ||
2093cd401204SKevin Cernekee 	    alps_absolute_mode_v3(psmouse)) {
209425bded7cSSeth Forshee 		psmouse_err(psmouse, "Failed to enter absolute mode\n");
209525bded7cSSeth Forshee 		goto error;
209625bded7cSSeth Forshee 	}
209725bded7cSSeth Forshee 
209825bded7cSSeth Forshee 	reg_val = alps_command_mode_read_reg(psmouse, 0x0006);
209925bded7cSSeth Forshee 	if (reg_val == -1)
210025bded7cSSeth Forshee 		goto error;
210125bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
210225bded7cSSeth Forshee 		goto error;
210325bded7cSSeth Forshee 
210425bded7cSSeth Forshee 	reg_val = alps_command_mode_read_reg(psmouse, 0x0007);
210525bded7cSSeth Forshee 	if (reg_val == -1)
210625bded7cSSeth Forshee 		goto error;
210725bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
210825bded7cSSeth Forshee 		goto error;
210925bded7cSSeth Forshee 
211025bded7cSSeth Forshee 	if (alps_command_mode_read_reg(psmouse, 0x0144) == -1)
211125bded7cSSeth Forshee 		goto error;
211225bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, 0x04))
211325bded7cSSeth Forshee 		goto error;
211425bded7cSSeth Forshee 
211525bded7cSSeth Forshee 	if (alps_command_mode_read_reg(psmouse, 0x0159) == -1)
211625bded7cSSeth Forshee 		goto error;
211725bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, 0x03))
211825bded7cSSeth Forshee 		goto error;
211925bded7cSSeth Forshee 
212025bded7cSSeth Forshee 	if (alps_command_mode_read_reg(psmouse, 0x0163) == -1)
212125bded7cSSeth Forshee 		goto error;
212225bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0163, 0x03))
212325bded7cSSeth Forshee 		goto error;
212425bded7cSSeth Forshee 
212525bded7cSSeth Forshee 	if (alps_command_mode_read_reg(psmouse, 0x0162) == -1)
212625bded7cSSeth Forshee 		goto error;
212725bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0162, 0x04))
212825bded7cSSeth Forshee 		goto error;
212925bded7cSSeth Forshee 
213025bded7cSSeth Forshee 	alps_exit_command_mode(psmouse);
213125bded7cSSeth Forshee 
213225bded7cSSeth Forshee 	/* Set rate and enable data reporting */
213325bded7cSSeth Forshee 	param[0] = 0x64;
213425bded7cSSeth Forshee 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
213525bded7cSSeth Forshee 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
213625bded7cSSeth Forshee 		psmouse_err(psmouse, "Failed to enable data reporting\n");
213725bded7cSSeth Forshee 		return -1;
213825bded7cSSeth Forshee 	}
213925bded7cSSeth Forshee 
214025bded7cSSeth Forshee 	return 0;
214125bded7cSSeth Forshee 
214225bded7cSSeth Forshee error:
214325bded7cSSeth Forshee 	/*
214425bded7cSSeth Forshee 	 * Leaving the touchpad in command mode will essentially render
214525bded7cSSeth Forshee 	 * it unusable until the machine reboots, so exit it here just
214625bded7cSSeth Forshee 	 * to be safe
214725bded7cSSeth Forshee 	 */
214825bded7cSSeth Forshee 	alps_exit_command_mode(psmouse);
214925bded7cSSeth Forshee 	return -1;
215025bded7cSSeth Forshee }
215125bded7cSSeth Forshee 
2152f3f33c67SHans de Goede static int alps_get_v3_v7_resolution(struct psmouse *psmouse, int reg_pitch)
2153f3f33c67SHans de Goede {
2154f3f33c67SHans de Goede 	int reg, x_pitch, y_pitch, x_electrode, y_electrode, x_phys, y_phys;
2155f3f33c67SHans de Goede 	struct alps_data *priv = psmouse->private;
2156f3f33c67SHans de Goede 
2157f3f33c67SHans de Goede 	reg = alps_command_mode_read_reg(psmouse, reg_pitch);
2158f3f33c67SHans de Goede 	if (reg < 0)
2159f3f33c67SHans de Goede 		return reg;
2160f3f33c67SHans de Goede 
2161f3f33c67SHans de Goede 	x_pitch = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
2162f3f33c67SHans de Goede 	x_pitch = 50 + 2 * x_pitch; /* In 0.1 mm units */
2163f3f33c67SHans de Goede 
2164f3f33c67SHans de Goede 	y_pitch = (char)reg >> 4; /* sign extend upper 4 bits */
2165f3f33c67SHans de Goede 	y_pitch = 36 + 2 * y_pitch; /* In 0.1 mm units */
2166f3f33c67SHans de Goede 
2167f3f33c67SHans de Goede 	reg = alps_command_mode_read_reg(psmouse, reg_pitch + 1);
2168f3f33c67SHans de Goede 	if (reg < 0)
2169f3f33c67SHans de Goede 		return reg;
2170f3f33c67SHans de Goede 
2171f3f33c67SHans de Goede 	x_electrode = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
2172f3f33c67SHans de Goede 	x_electrode = 17 + x_electrode;
2173f3f33c67SHans de Goede 
2174f3f33c67SHans de Goede 	y_electrode = (char)reg >> 4; /* sign extend upper 4 bits */
2175f3f33c67SHans de Goede 	y_electrode = 13 + y_electrode;
2176f3f33c67SHans de Goede 
2177f3f33c67SHans de Goede 	x_phys = x_pitch * (x_electrode - 1); /* In 0.1 mm units */
2178f3f33c67SHans de Goede 	y_phys = y_pitch * (y_electrode - 1); /* In 0.1 mm units */
2179f3f33c67SHans de Goede 
2180f3f33c67SHans de Goede 	priv->x_res = priv->x_max * 10 / x_phys; /* units / mm */
2181f3f33c67SHans de Goede 	priv->y_res = priv->y_max * 10 / y_phys; /* units / mm */
2182f3f33c67SHans de Goede 
2183f3f33c67SHans de Goede 	psmouse_dbg(psmouse,
2184f3f33c67SHans de Goede 		    "pitch %dx%d num-electrodes %dx%d physical size %dx%d mm res %dx%d\n",
2185f3f33c67SHans de Goede 		    x_pitch, y_pitch, x_electrode, y_electrode,
2186f3f33c67SHans de Goede 		    x_phys / 10, y_phys / 10, priv->x_res, priv->y_res);
2187f3f33c67SHans de Goede 
2188f3f33c67SHans de Goede 	return 0;
2189f3f33c67SHans de Goede }
2190f3f33c67SHans de Goede 
21911302bac3SKevin Cernekee static int alps_hw_init_rushmore_v3(struct psmouse *psmouse)
21921302bac3SKevin Cernekee {
2193cd401204SKevin Cernekee 	struct alps_data *priv = psmouse->private;
21941302bac3SKevin Cernekee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
21951302bac3SKevin Cernekee 	int reg_val, ret = -1;
21961302bac3SKevin Cernekee 
2197cd401204SKevin Cernekee 	if (priv->flags & ALPS_DUALPOINT) {
2198cd401204SKevin Cernekee 		reg_val = alps_setup_trackstick_v3(psmouse,
2199cd401204SKevin Cernekee 						   ALPS_REG_BASE_RUSHMORE);
2200cd401204SKevin Cernekee 		if (reg_val == -EIO)
2201cd401204SKevin Cernekee 			goto error;
2202cd401204SKevin Cernekee 	}
2203cd401204SKevin Cernekee 
2204d18e53fcSKevin Cernekee 	if (alps_enter_command_mode(psmouse) ||
22051302bac3SKevin Cernekee 	    alps_command_mode_read_reg(psmouse, 0xc2d9) == -1 ||
22061302bac3SKevin Cernekee 	    alps_command_mode_write_reg(psmouse, 0xc2cb, 0x00))
22071302bac3SKevin Cernekee 		goto error;
22081302bac3SKevin Cernekee 
2209f3f33c67SHans de Goede 	if (alps_get_v3_v7_resolution(psmouse, 0xc2da))
2210f3f33c67SHans de Goede 		goto error;
2211f3f33c67SHans de Goede 
22121302bac3SKevin Cernekee 	reg_val = alps_command_mode_read_reg(psmouse, 0xc2c6);
22131302bac3SKevin Cernekee 	if (reg_val == -1)
22141302bac3SKevin Cernekee 		goto error;
22151302bac3SKevin Cernekee 	if (__alps_command_mode_write_reg(psmouse, reg_val & 0xfd))
22161302bac3SKevin Cernekee 		goto error;
22171302bac3SKevin Cernekee 
22181302bac3SKevin Cernekee 	if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
22191302bac3SKevin Cernekee 		goto error;
22201302bac3SKevin Cernekee 
22211302bac3SKevin Cernekee 	/* enter absolute mode */
22221302bac3SKevin Cernekee 	reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
22231302bac3SKevin Cernekee 	if (reg_val == -1)
22241302bac3SKevin Cernekee 		goto error;
22251302bac3SKevin Cernekee 	if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
22261302bac3SKevin Cernekee 		goto error;
22271302bac3SKevin Cernekee 
22281302bac3SKevin Cernekee 	alps_exit_command_mode(psmouse);
22291302bac3SKevin Cernekee 	return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
22301302bac3SKevin Cernekee 
22311302bac3SKevin Cernekee error:
22321302bac3SKevin Cernekee 	alps_exit_command_mode(psmouse);
22331302bac3SKevin Cernekee 	return ret;
22341302bac3SKevin Cernekee }
22351302bac3SKevin Cernekee 
223625bded7cSSeth Forshee /* Must be in command mode when calling this function */
223725bded7cSSeth Forshee static int alps_absolute_mode_v4(struct psmouse *psmouse)
223825bded7cSSeth Forshee {
223925bded7cSSeth Forshee 	int reg_val;
224025bded7cSSeth Forshee 
224125bded7cSSeth Forshee 	reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
224225bded7cSSeth Forshee 	if (reg_val == -1)
224325bded7cSSeth Forshee 		return -1;
224425bded7cSSeth Forshee 
224525bded7cSSeth Forshee 	reg_val |= 0x02;
224625bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, reg_val))
224725bded7cSSeth Forshee 		return -1;
224825bded7cSSeth Forshee 
224925bded7cSSeth Forshee 	return 0;
225025bded7cSSeth Forshee }
225125bded7cSSeth Forshee 
225225bded7cSSeth Forshee static int alps_hw_init_v4(struct psmouse *psmouse)
225325bded7cSSeth Forshee {
225425bded7cSSeth Forshee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
225525bded7cSSeth Forshee 	unsigned char param[4];
225625bded7cSSeth Forshee 
2257d18e53fcSKevin Cernekee 	if (alps_enter_command_mode(psmouse))
225825bded7cSSeth Forshee 		goto error;
225925bded7cSSeth Forshee 
226025bded7cSSeth Forshee 	if (alps_absolute_mode_v4(psmouse)) {
226125bded7cSSeth Forshee 		psmouse_err(psmouse, "Failed to enter absolute mode\n");
226225bded7cSSeth Forshee 		goto error;
226325bded7cSSeth Forshee 	}
226425bded7cSSeth Forshee 
226525bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0007, 0x8c))
226625bded7cSSeth Forshee 		goto error;
226725bded7cSSeth Forshee 
226825bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0149, 0x03))
226925bded7cSSeth Forshee 		goto error;
227025bded7cSSeth Forshee 
227125bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0160, 0x03))
227225bded7cSSeth Forshee 		goto error;
227325bded7cSSeth Forshee 
227425bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x017f, 0x15))
227525bded7cSSeth Forshee 		goto error;
227625bded7cSSeth Forshee 
227725bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0151, 0x01))
227825bded7cSSeth Forshee 		goto error;
227925bded7cSSeth Forshee 
228025bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0168, 0x03))
228125bded7cSSeth Forshee 		goto error;
228225bded7cSSeth Forshee 
228325bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x014a, 0x03))
228425bded7cSSeth Forshee 		goto error;
228525bded7cSSeth Forshee 
228625bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0161, 0x03))
228725bded7cSSeth Forshee 		goto error;
228825bded7cSSeth Forshee 
228925bded7cSSeth Forshee 	alps_exit_command_mode(psmouse);
229025bded7cSSeth Forshee 
229125bded7cSSeth Forshee 	/*
229225bded7cSSeth Forshee 	 * This sequence changes the output from a 9-byte to an
229325bded7cSSeth Forshee 	 * 8-byte format. All the same data seems to be present,
229425bded7cSSeth Forshee 	 * just in a more compact format.
229525bded7cSSeth Forshee 	 */
229625bded7cSSeth Forshee 	param[0] = 0xc8;
229725bded7cSSeth Forshee 	param[1] = 0x64;
229825bded7cSSeth Forshee 	param[2] = 0x50;
229925bded7cSSeth Forshee 	if (ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
230025bded7cSSeth Forshee 	    ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE) ||
230125bded7cSSeth Forshee 	    ps2_command(ps2dev, &param[2], PSMOUSE_CMD_SETRATE) ||
230225bded7cSSeth Forshee 	    ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
230325bded7cSSeth Forshee 		return -1;
230425bded7cSSeth Forshee 
230525bded7cSSeth Forshee 	/* Set rate and enable data reporting */
230625bded7cSSeth Forshee 	param[0] = 0x64;
230725bded7cSSeth Forshee 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
230825bded7cSSeth Forshee 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
230925bded7cSSeth Forshee 		psmouse_err(psmouse, "Failed to enable data reporting\n");
231025bded7cSSeth Forshee 		return -1;
231125bded7cSSeth Forshee 	}
231225bded7cSSeth Forshee 
231325bded7cSSeth Forshee 	return 0;
231425bded7cSSeth Forshee 
231525bded7cSSeth Forshee error:
231625bded7cSSeth Forshee 	/*
231725bded7cSSeth Forshee 	 * Leaving the touchpad in command mode will essentially render
231825bded7cSSeth Forshee 	 * it unusable until the machine reboots, so exit it here just
231925bded7cSSeth Forshee 	 * to be safe
232025bded7cSSeth Forshee 	 */
232125bded7cSSeth Forshee 	alps_exit_command_mode(psmouse);
232225bded7cSSeth Forshee 	return -1;
232325bded7cSSeth Forshee }
232425bded7cSSeth Forshee 
23253db5b9f7SMasaki Ota static int alps_get_otp_values_ss4_v2(struct psmouse *psmouse,
23263db5b9f7SMasaki Ota 				      unsigned char index, unsigned char otp[])
23273db5b9f7SMasaki Ota {
23283db5b9f7SMasaki Ota 	struct ps2dev *ps2dev = &psmouse->ps2dev;
23293db5b9f7SMasaki Ota 
23303db5b9f7SMasaki Ota 	switch (index) {
23313db5b9f7SMasaki Ota 	case 0:
23323db5b9f7SMasaki Ota 		if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)  ||
23333db5b9f7SMasaki Ota 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)  ||
23343db5b9f7SMasaki Ota 		    ps2_command(ps2dev, otp, PSMOUSE_CMD_GETINFO))
23353db5b9f7SMasaki Ota 			return -1;
23363db5b9f7SMasaki Ota 
23373db5b9f7SMasaki Ota 		break;
23383db5b9f7SMasaki Ota 
23393db5b9f7SMasaki Ota 	case 1:
23403db5b9f7SMasaki Ota 		if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL)  ||
23413db5b9f7SMasaki Ota 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL)  ||
23423db5b9f7SMasaki Ota 		    ps2_command(ps2dev, otp, PSMOUSE_CMD_GETINFO))
23433db5b9f7SMasaki Ota 			return -1;
23443db5b9f7SMasaki Ota 
23453db5b9f7SMasaki Ota 		break;
23463db5b9f7SMasaki Ota 	}
23473db5b9f7SMasaki Ota 
23483db5b9f7SMasaki Ota 	return 0;
23493db5b9f7SMasaki Ota }
23503db5b9f7SMasaki Ota 
235107f19e3dSFengguang Wu static int alps_update_device_area_ss4_v2(unsigned char otp[][4],
23523db5b9f7SMasaki Ota 					  struct alps_data *priv)
23533db5b9f7SMasaki Ota {
23543db5b9f7SMasaki Ota 	int num_x_electrode;
23553db5b9f7SMasaki Ota 	int num_y_electrode;
23563db5b9f7SMasaki Ota 	int x_pitch, y_pitch, x_phys, y_phys;
23573db5b9f7SMasaki Ota 
23583db5b9f7SMasaki Ota 	num_x_electrode = SS4_NUMSENSOR_XOFFSET + (otp[1][0] & 0x0F);
23593db5b9f7SMasaki Ota 	num_y_electrode = SS4_NUMSENSOR_YOFFSET + ((otp[1][0] >> 4) & 0x0F);
23603db5b9f7SMasaki Ota 
23613db5b9f7SMasaki Ota 	priv->x_max = (num_x_electrode - 1) * SS4_COUNT_PER_ELECTRODE;
23623db5b9f7SMasaki Ota 	priv->y_max = (num_y_electrode - 1) * SS4_COUNT_PER_ELECTRODE;
23633db5b9f7SMasaki Ota 
23643db5b9f7SMasaki Ota 	x_pitch = ((otp[1][2] >> 2) & 0x07) + SS4_MIN_PITCH_MM;
23653db5b9f7SMasaki Ota 	y_pitch = ((otp[1][2] >> 5) & 0x07) + SS4_MIN_PITCH_MM;
23663db5b9f7SMasaki Ota 
23673db5b9f7SMasaki Ota 	x_phys = x_pitch * (num_x_electrode - 1); /* In 0.1 mm units */
23683db5b9f7SMasaki Ota 	y_phys = y_pitch * (num_y_electrode - 1); /* In 0.1 mm units */
23693db5b9f7SMasaki Ota 
23703db5b9f7SMasaki Ota 	priv->x_res = priv->x_max * 10 / x_phys; /* units / mm */
23713db5b9f7SMasaki Ota 	priv->y_res = priv->y_max * 10 / y_phys; /* units / mm */
23723db5b9f7SMasaki Ota 
23733db5b9f7SMasaki Ota 	return 0;
23743db5b9f7SMasaki Ota }
23753db5b9f7SMasaki Ota 
237607f19e3dSFengguang Wu static int alps_update_btn_info_ss4_v2(unsigned char otp[][4],
237707f19e3dSFengguang Wu 				       struct alps_data *priv)
23783db5b9f7SMasaki Ota {
23793db5b9f7SMasaki Ota 	unsigned char is_btnless;
23803db5b9f7SMasaki Ota 
23813db5b9f7SMasaki Ota 	is_btnless = (otp[1][1] >> 3) & 0x01;
23823db5b9f7SMasaki Ota 
23833db5b9f7SMasaki Ota 	if (is_btnless)
23843db5b9f7SMasaki Ota 		priv->flags |= ALPS_BUTTONPAD;
23853db5b9f7SMasaki Ota 
23863db5b9f7SMasaki Ota 	return 0;
23873db5b9f7SMasaki Ota }
23883db5b9f7SMasaki Ota 
23893db5b9f7SMasaki Ota static int alps_set_defaults_ss4_v2(struct psmouse *psmouse,
23903db5b9f7SMasaki Ota 				    struct alps_data *priv)
23913db5b9f7SMasaki Ota {
23923db5b9f7SMasaki Ota 	unsigned char otp[2][4];
23933db5b9f7SMasaki Ota 
23943db5b9f7SMasaki Ota 	memset(otp, 0, sizeof(otp));
23953db5b9f7SMasaki Ota 
23963db5b9f7SMasaki Ota 	if (alps_get_otp_values_ss4_v2(psmouse, 0, &otp[0][0]) ||
23973db5b9f7SMasaki Ota 	    alps_get_otp_values_ss4_v2(psmouse, 1, &otp[1][0]))
23983db5b9f7SMasaki Ota 		return -1;
23993db5b9f7SMasaki Ota 
24003db5b9f7SMasaki Ota 	alps_update_device_area_ss4_v2(otp, priv);
24013db5b9f7SMasaki Ota 
24023db5b9f7SMasaki Ota 	alps_update_btn_info_ss4_v2(otp, priv);
24033db5b9f7SMasaki Ota 
24043db5b9f7SMasaki Ota 	return 0;
24053db5b9f7SMasaki Ota }
24063db5b9f7SMasaki Ota 
2407ee65d4b3SYunkang Tang static int alps_dolphin_get_device_area(struct psmouse *psmouse,
2408ee65d4b3SYunkang Tang 					struct alps_data *priv)
2409ee65d4b3SYunkang Tang {
2410ee65d4b3SYunkang Tang 	struct ps2dev *ps2dev = &psmouse->ps2dev;
2411ee65d4b3SYunkang Tang 	unsigned char param[4] = {0};
2412ee65d4b3SYunkang Tang 	int num_x_electrode, num_y_electrode;
2413ee65d4b3SYunkang Tang 
2414ee65d4b3SYunkang Tang 	if (alps_enter_command_mode(psmouse))
2415ee65d4b3SYunkang Tang 		return -1;
2416ee65d4b3SYunkang Tang 
2417ee65d4b3SYunkang Tang 	param[0] = 0x0a;
2418ee65d4b3SYunkang Tang 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
2419ee65d4b3SYunkang Tang 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2420ee65d4b3SYunkang Tang 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2421ee65d4b3SYunkang Tang 	    ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2422ee65d4b3SYunkang Tang 	    ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE))
2423ee65d4b3SYunkang Tang 		return -1;
2424ee65d4b3SYunkang Tang 
2425ee65d4b3SYunkang Tang 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
2426ee65d4b3SYunkang Tang 		return -1;
2427ee65d4b3SYunkang Tang 
2428ee65d4b3SYunkang Tang 	/*
2429ee65d4b3SYunkang Tang 	 * Dolphin's sensor line number is not fixed. It can be calculated
2430ee65d4b3SYunkang Tang 	 * by adding the device's register value with DOLPHIN_PROFILE_X/YOFFSET.
2431ee65d4b3SYunkang Tang 	 * Further more, we can get device's x_max and y_max by multiplying
2432ee65d4b3SYunkang Tang 	 * sensor line number with DOLPHIN_COUNT_PER_ELECTRODE.
2433ee65d4b3SYunkang Tang 	 *
2434ee65d4b3SYunkang Tang 	 * e.g. When we get register's sensor_x = 11 & sensor_y = 8,
2435ee65d4b3SYunkang Tang 	 *	real sensor line number X = 11 + 8 = 19, and
2436ee65d4b3SYunkang Tang 	 *	real sensor line number Y = 8 + 1 = 9.
2437ee65d4b3SYunkang Tang 	 *	So, x_max = (19 - 1) * 64 = 1152, and
2438ee65d4b3SYunkang Tang 	 *	    y_max = (9 - 1) * 64 = 512.
2439ee65d4b3SYunkang Tang 	 */
2440ee65d4b3SYunkang Tang 	num_x_electrode = DOLPHIN_PROFILE_XOFFSET + (param[2] & 0x0F);
2441ee65d4b3SYunkang Tang 	num_y_electrode = DOLPHIN_PROFILE_YOFFSET + ((param[2] >> 4) & 0x0F);
2442ee65d4b3SYunkang Tang 	priv->x_bits = num_x_electrode;
2443ee65d4b3SYunkang Tang 	priv->y_bits = num_y_electrode;
2444ee65d4b3SYunkang Tang 	priv->x_max = (num_x_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
2445ee65d4b3SYunkang Tang 	priv->y_max = (num_y_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
2446ee65d4b3SYunkang Tang 
2447ee65d4b3SYunkang Tang 	if (alps_exit_command_mode(psmouse))
2448ee65d4b3SYunkang Tang 		return -1;
2449ee65d4b3SYunkang Tang 
2450ee65d4b3SYunkang Tang 	return 0;
2451ee65d4b3SYunkang Tang }
2452ee65d4b3SYunkang Tang 
245375af9e56SDave Turvene static int alps_hw_init_dolphin_v1(struct psmouse *psmouse)
245475af9e56SDave Turvene {
245575af9e56SDave Turvene 	struct ps2dev *ps2dev = &psmouse->ps2dev;
245675af9e56SDave Turvene 	unsigned char param[2];
245775af9e56SDave Turvene 
245875af9e56SDave Turvene 	/* This is dolphin "v1" as empirically defined by florin9doi */
245975af9e56SDave Turvene 	param[0] = 0x64;
246075af9e56SDave Turvene 	param[1] = 0x28;
246175af9e56SDave Turvene 
246275af9e56SDave Turvene 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
246375af9e56SDave Turvene 	    ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
246475af9e56SDave Turvene 	    ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
246575af9e56SDave Turvene 		return -1;
246675af9e56SDave Turvene 
246775af9e56SDave Turvene 	return 0;
246875af9e56SDave Turvene }
246975af9e56SDave Turvene 
24703808843cSYunkang Tang static int alps_hw_init_v7(struct psmouse *psmouse)
24713808843cSYunkang Tang {
24723808843cSYunkang Tang 	struct ps2dev *ps2dev = &psmouse->ps2dev;
24733808843cSYunkang Tang 	int reg_val, ret = -1;
24743808843cSYunkang Tang 
24753808843cSYunkang Tang 	if (alps_enter_command_mode(psmouse) ||
24763808843cSYunkang Tang 	    alps_command_mode_read_reg(psmouse, 0xc2d9) == -1)
24773808843cSYunkang Tang 		goto error;
24783808843cSYunkang Tang 
2479f3f33c67SHans de Goede 	if (alps_get_v3_v7_resolution(psmouse, 0xc397))
2480f3f33c67SHans de Goede 		goto error;
2481f3f33c67SHans de Goede 
24823808843cSYunkang Tang 	if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
24833808843cSYunkang Tang 		goto error;
24843808843cSYunkang Tang 
24853808843cSYunkang Tang 	reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
24863808843cSYunkang Tang 	if (reg_val == -1)
24873808843cSYunkang Tang 		goto error;
24883808843cSYunkang Tang 	if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
24893808843cSYunkang Tang 		goto error;
24903808843cSYunkang Tang 
24913808843cSYunkang Tang 	alps_exit_command_mode(psmouse);
24923808843cSYunkang Tang 	return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
24933808843cSYunkang Tang 
24943808843cSYunkang Tang error:
24953808843cSYunkang Tang 	alps_exit_command_mode(psmouse);
24963808843cSYunkang Tang 	return ret;
24973808843cSYunkang Tang }
24983808843cSYunkang Tang 
24993db5b9f7SMasaki Ota static int alps_hw_init_ss4_v2(struct psmouse *psmouse)
25003db5b9f7SMasaki Ota {
25013db5b9f7SMasaki Ota 	struct ps2dev *ps2dev = &psmouse->ps2dev;
25023db5b9f7SMasaki Ota 	char param[2] = {0x64, 0x28};
25033db5b9f7SMasaki Ota 	int ret = -1;
25043db5b9f7SMasaki Ota 
25053db5b9f7SMasaki Ota 	/* enter absolute mode */
25063db5b9f7SMasaki Ota 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
25073db5b9f7SMasaki Ota 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
25083db5b9f7SMasaki Ota 	    ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
25093db5b9f7SMasaki Ota 	    ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE)) {
25103db5b9f7SMasaki Ota 		goto error;
25113db5b9f7SMasaki Ota 	}
25123db5b9f7SMasaki Ota 
25133db5b9f7SMasaki Ota 	/* T.B.D. Decread noise packet number, delete in the future */
25143db5b9f7SMasaki Ota 	if (alps_exit_command_mode(psmouse) ||
25153db5b9f7SMasaki Ota 	    alps_enter_command_mode(psmouse) ||
25163db5b9f7SMasaki Ota 	    alps_command_mode_write_reg(psmouse, 0x001D, 0x20)) {
25173db5b9f7SMasaki Ota 		goto error;
25183db5b9f7SMasaki Ota 	}
25193db5b9f7SMasaki Ota 	alps_exit_command_mode(psmouse);
25203db5b9f7SMasaki Ota 
25213db5b9f7SMasaki Ota 	return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
25223db5b9f7SMasaki Ota 
25233db5b9f7SMasaki Ota error:
25243db5b9f7SMasaki Ota 	alps_exit_command_mode(psmouse);
25253db5b9f7SMasaki Ota 	return ret;
25263db5b9f7SMasaki Ota }
25273db5b9f7SMasaki Ota 
25283296f71cSDmitry Torokhov static int alps_set_protocol(struct psmouse *psmouse,
25293296f71cSDmitry Torokhov 			     struct alps_data *priv,
25303296f71cSDmitry Torokhov 			     const struct alps_protocol_info *protocol)
253125bded7cSSeth Forshee {
25323296f71cSDmitry Torokhov 	psmouse->private = priv;
25333296f71cSDmitry Torokhov 
25343296f71cSDmitry Torokhov 	setup_timer(&priv->timer, alps_flush_packet, (unsigned long)psmouse);
25353296f71cSDmitry Torokhov 
25363296f71cSDmitry Torokhov 	priv->proto_version = protocol->version;
25373296f71cSDmitry Torokhov 	priv->byte0 = protocol->byte0;
25383296f71cSDmitry Torokhov 	priv->mask0 = protocol->mask0;
25393296f71cSDmitry Torokhov 	priv->flags = protocol->flags;
2540f673ceb1SKevin Cernekee 
25417a9f73e7SKevin Cernekee 	priv->x_max = 2000;
25427a9f73e7SKevin Cernekee 	priv->y_max = 1400;
25437a9f73e7SKevin Cernekee 	priv->x_bits = 15;
25447a9f73e7SKevin Cernekee 	priv->y_bits = 11;
25457a9f73e7SKevin Cernekee 
254699df65e7SKevin Cernekee 	switch (priv->proto_version) {
254725bded7cSSeth Forshee 	case ALPS_PROTO_V1:
254825bded7cSSeth Forshee 	case ALPS_PROTO_V2:
254924af5cb9SKevin Cernekee 		priv->hw_init = alps_hw_init_v1_v2;
255024af5cb9SKevin Cernekee 		priv->process_packet = alps_process_packet_v1_v2;
255124af5cb9SKevin Cernekee 		priv->set_abs_params = alps_set_abs_params_st;
255295f75e91SYunkang Tang 		priv->x_max = 1023;
255395f75e91SYunkang Tang 		priv->y_max = 767;
255425bded7cSSeth Forshee 		break;
2555fb2dd7a6SDmitry Torokhov 
255625bded7cSSeth Forshee 	case ALPS_PROTO_V3:
255724af5cb9SKevin Cernekee 		priv->hw_init = alps_hw_init_v3;
255824af5cb9SKevin Cernekee 		priv->process_packet = alps_process_packet_v3;
2559688ea364SHans de Goede 		priv->set_abs_params = alps_set_abs_params_semi_mt;
2560f85e5001SKevin Cernekee 		priv->decode_fields = alps_decode_pinnacle;
256150e8b216SKevin Cernekee 		priv->nibble_commands = alps_v3_nibble_commands;
256250e8b216SKevin Cernekee 		priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
256325bded7cSSeth Forshee 		break;
2564fb2dd7a6SDmitry Torokhov 
2565fb2dd7a6SDmitry Torokhov 	case ALPS_PROTO_V3_RUSHMORE:
2566fb2dd7a6SDmitry Torokhov 		priv->hw_init = alps_hw_init_rushmore_v3;
2567fb2dd7a6SDmitry Torokhov 		priv->process_packet = alps_process_packet_v3;
2568688ea364SHans de Goede 		priv->set_abs_params = alps_set_abs_params_semi_mt;
2569fb2dd7a6SDmitry Torokhov 		priv->decode_fields = alps_decode_rushmore;
2570fb2dd7a6SDmitry Torokhov 		priv->nibble_commands = alps_v3_nibble_commands;
2571fb2dd7a6SDmitry Torokhov 		priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2572fb2dd7a6SDmitry Torokhov 		priv->x_bits = 16;
2573fb2dd7a6SDmitry Torokhov 		priv->y_bits = 12;
25743296f71cSDmitry Torokhov 
25753296f71cSDmitry Torokhov 		if (alps_probe_trackstick_v3(psmouse,
25763296f71cSDmitry Torokhov 					     ALPS_REG_BASE_RUSHMORE) < 0)
25773296f71cSDmitry Torokhov 			priv->flags &= ~ALPS_DUALPOINT;
25783296f71cSDmitry Torokhov 
2579fb2dd7a6SDmitry Torokhov 		break;
2580fb2dd7a6SDmitry Torokhov 
258125bded7cSSeth Forshee 	case ALPS_PROTO_V4:
258224af5cb9SKevin Cernekee 		priv->hw_init = alps_hw_init_v4;
258324af5cb9SKevin Cernekee 		priv->process_packet = alps_process_packet_v4;
2584688ea364SHans de Goede 		priv->set_abs_params = alps_set_abs_params_semi_mt;
258550e8b216SKevin Cernekee 		priv->nibble_commands = alps_v4_nibble_commands;
258650e8b216SKevin Cernekee 		priv->addr_command = PSMOUSE_CMD_DISABLE;
258725bded7cSSeth Forshee 		break;
25883296f71cSDmitry Torokhov 
258975af9e56SDave Turvene 	case ALPS_PROTO_V5:
259075af9e56SDave Turvene 		priv->hw_init = alps_hw_init_dolphin_v1;
2591ee65d4b3SYunkang Tang 		priv->process_packet = alps_process_touchpad_packet_v3_v5;
259275af9e56SDave Turvene 		priv->decode_fields = alps_decode_dolphin;
2593688ea364SHans de Goede 		priv->set_abs_params = alps_set_abs_params_semi_mt;
259475af9e56SDave Turvene 		priv->nibble_commands = alps_v3_nibble_commands;
259575af9e56SDave Turvene 		priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
259675af9e56SDave Turvene 		priv->x_bits = 23;
259775af9e56SDave Turvene 		priv->y_bits = 12;
2598c164c147SDmitry Torokhov 
2599c164c147SDmitry Torokhov 		if (alps_dolphin_get_device_area(psmouse, priv))
2600c164c147SDmitry Torokhov 			return -EIO;
2601c164c147SDmitry Torokhov 
260275af9e56SDave Turvene 		break;
26033296f71cSDmitry Torokhov 
260495f75e91SYunkang Tang 	case ALPS_PROTO_V6:
260595f75e91SYunkang Tang 		priv->hw_init = alps_hw_init_v6;
260695f75e91SYunkang Tang 		priv->process_packet = alps_process_packet_v6;
260795f75e91SYunkang Tang 		priv->set_abs_params = alps_set_abs_params_st;
260895f75e91SYunkang Tang 		priv->nibble_commands = alps_v6_nibble_commands;
260995f75e91SYunkang Tang 		priv->x_max = 2047;
261095f75e91SYunkang Tang 		priv->y_max = 1535;
261195f75e91SYunkang Tang 		break;
26123296f71cSDmitry Torokhov 
26133808843cSYunkang Tang 	case ALPS_PROTO_V7:
26143808843cSYunkang Tang 		priv->hw_init = alps_hw_init_v7;
26153808843cSYunkang Tang 		priv->process_packet = alps_process_packet_v7;
26163808843cSYunkang Tang 		priv->decode_fields = alps_decode_packet_v7;
26178eccd393SMasaki Ota 		priv->set_abs_params = alps_set_abs_params_v7;
26183808843cSYunkang Tang 		priv->nibble_commands = alps_v3_nibble_commands;
26193808843cSYunkang Tang 		priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2620c164c147SDmitry Torokhov 		priv->x_max = 0xfff;
2621c164c147SDmitry Torokhov 		priv->y_max = 0x7ff;
26223808843cSYunkang Tang 
26233808843cSYunkang Tang 		if (priv->fw_ver[1] != 0xba)
26243808843cSYunkang Tang 			priv->flags |= ALPS_BUTTONPAD;
26253296f71cSDmitry Torokhov 
26263808843cSYunkang Tang 		break;
26273db5b9f7SMasaki Ota 
26283db5b9f7SMasaki Ota 	case ALPS_PROTO_V8:
26293db5b9f7SMasaki Ota 		priv->hw_init = alps_hw_init_ss4_v2;
26303db5b9f7SMasaki Ota 		priv->process_packet = alps_process_packet_ss4_v2;
26313db5b9f7SMasaki Ota 		priv->decode_fields = alps_decode_ss4_v2;
26323db5b9f7SMasaki Ota 		priv->set_abs_params = alps_set_abs_params_ss4_v2;
26333db5b9f7SMasaki Ota 		priv->nibble_commands = alps_v3_nibble_commands;
26343db5b9f7SMasaki Ota 		priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
26353db5b9f7SMasaki Ota 
26363db5b9f7SMasaki Ota 		if (alps_set_defaults_ss4_v2(psmouse, priv))
26373db5b9f7SMasaki Ota 			return -EIO;
26383db5b9f7SMasaki Ota 
26393db5b9f7SMasaki Ota 		break;
264025bded7cSSeth Forshee 	}
26413296f71cSDmitry Torokhov 
26423296f71cSDmitry Torokhov 	return 0;
264325bded7cSSeth Forshee }
264425bded7cSSeth Forshee 
26453296f71cSDmitry Torokhov static const struct alps_protocol_info *alps_match_table(unsigned char *e7,
26463296f71cSDmitry Torokhov 							 unsigned char *ec)
26472e992cc0SKevin Cernekee {
2648b5d6b851SKevin Cernekee 	const struct alps_model_info *model;
26492e992cc0SKevin Cernekee 	int i;
26502e992cc0SKevin Cernekee 
2651b5d6b851SKevin Cernekee 	for (i = 0; i < ARRAY_SIZE(alps_model_data); i++) {
2652b5d6b851SKevin Cernekee 		model = &alps_model_data[i];
2653b5d6b851SKevin Cernekee 
2654b5d6b851SKevin Cernekee 		if (!memcmp(e7, model->signature, sizeof(model->signature)) &&
2655b5d6b851SKevin Cernekee 		    (!model->command_mode_resp ||
2656b5d6b851SKevin Cernekee 		     model->command_mode_resp == ec[2])) {
2657b5d6b851SKevin Cernekee 
26583296f71cSDmitry Torokhov 			return &model->protocol_info;
2659b5d6b851SKevin Cernekee 		}
2660b5d6b851SKevin Cernekee 	}
2661b5d6b851SKevin Cernekee 
26623296f71cSDmitry Torokhov 	return NULL;
2663b5d6b851SKevin Cernekee }
2664b5d6b851SKevin Cernekee 
2665b5d6b851SKevin Cernekee static int alps_identify(struct psmouse *psmouse, struct alps_data *priv)
2666b5d6b851SKevin Cernekee {
26673296f71cSDmitry Torokhov 	const struct alps_protocol_info *protocol;
2668b5d6b851SKevin Cernekee 	unsigned char e6[4], e7[4], ec[4];
2669a09221e8SDmitry Torokhov 	int error;
2670b5d6b851SKevin Cernekee 
26712e992cc0SKevin Cernekee 	/*
26722e992cc0SKevin Cernekee 	 * First try "E6 report".
26732e992cc0SKevin Cernekee 	 * ALPS should return 0,0,10 or 0,0,100 if no buttons are pressed.
26742e992cc0SKevin Cernekee 	 * The bits 0-2 of the first byte will be 1s if some buttons are
26752e992cc0SKevin Cernekee 	 * pressed.
26762e992cc0SKevin Cernekee 	 */
2677b5d6b851SKevin Cernekee 	if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2678b5d6b851SKevin Cernekee 			 PSMOUSE_CMD_SETSCALE11, e6))
2679b5d6b851SKevin Cernekee 		return -EIO;
26802e992cc0SKevin Cernekee 
2681b5d6b851SKevin Cernekee 	if ((e6[0] & 0xf8) != 0 || e6[1] != 0 || (e6[2] != 10 && e6[2] != 100))
2682b5d6b851SKevin Cernekee 		return -EINVAL;
26832e992cc0SKevin Cernekee 
26842e992cc0SKevin Cernekee 	/*
2685b5d6b851SKevin Cernekee 	 * Now get the "E7" and "EC" reports.  These will uniquely identify
2686b5d6b851SKevin Cernekee 	 * most ALPS touchpads.
26872e992cc0SKevin Cernekee 	 */
2688b5d6b851SKevin Cernekee 	if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2689b5d6b851SKevin Cernekee 			 PSMOUSE_CMD_SETSCALE21, e7) ||
2690b5d6b851SKevin Cernekee 	    alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2691b5d6b851SKevin Cernekee 			 PSMOUSE_CMD_RESET_WRAP, ec) ||
2692b5d6b851SKevin Cernekee 	    alps_exit_command_mode(psmouse))
2693b5d6b851SKevin Cernekee 		return -EIO;
26942e992cc0SKevin Cernekee 
26953296f71cSDmitry Torokhov 	protocol = alps_match_table(e7, ec);
26963296f71cSDmitry Torokhov 	if (!protocol) {
26973296f71cSDmitry Torokhov 		if (e7[0] == 0x73 && e7[1] == 0x03 && e7[2] == 0x50 &&
26983296f71cSDmitry Torokhov 			   ec[0] == 0x73 && (ec[1] == 0x01 || ec[1] == 0x02)) {
26993296f71cSDmitry Torokhov 			protocol = &alps_v5_protocol_data;
27003296f71cSDmitry Torokhov 		} else if (ec[0] == 0x88 &&
27013296f71cSDmitry Torokhov 			   ((ec[1] & 0xf0) == 0xb0 || (ec[1] & 0xf0) == 0xc0)) {
27023296f71cSDmitry Torokhov 			protocol = &alps_v7_protocol_data;
27033296f71cSDmitry Torokhov 		} else if (ec[0] == 0x88 && ec[1] == 0x08) {
27043296f71cSDmitry Torokhov 			protocol = &alps_v3_rushmore_data;
27053296f71cSDmitry Torokhov 		} else if (ec[0] == 0x88 && ec[1] == 0x07 &&
27063296f71cSDmitry Torokhov 			   ec[2] >= 0x90 && ec[2] <= 0x9d) {
27073296f71cSDmitry Torokhov 			protocol = &alps_v3_protocol_data;
27083db5b9f7SMasaki Ota 		} else if (e7[0] == 0x73 && e7[1] == 0x03 &&
27093db5b9f7SMasaki Ota 			   e7[2] == 0x14 && ec[1] == 0x02) {
27103db5b9f7SMasaki Ota 			protocol = &alps_v8_protocol_data;
27113296f71cSDmitry Torokhov 		} else {
27123296f71cSDmitry Torokhov 			psmouse_dbg(psmouse,
27133296f71cSDmitry Torokhov 				    "Likely not an ALPS touchpad: E7=%3ph, EC=%3ph\n", e7, ec);
27143296f71cSDmitry Torokhov 			return -EINVAL;
27153296f71cSDmitry Torokhov 		}
27163296f71cSDmitry Torokhov 	}
27173296f71cSDmitry Torokhov 
2718a09221e8SDmitry Torokhov 	if (priv) {
2719c0cd17f6SHans de Goede 		/* Save the Firmware version */
2720c0cd17f6SHans de Goede 		memcpy(priv->fw_ver, ec, 3);
2721a09221e8SDmitry Torokhov 		error = alps_set_protocol(psmouse, priv, protocol);
2722a09221e8SDmitry Torokhov 		if (error)
2723a09221e8SDmitry Torokhov 			return error;
2724a09221e8SDmitry Torokhov 	}
2725c0cd17f6SHans de Goede 
2726a09221e8SDmitry Torokhov 	return 0;
27272e992cc0SKevin Cernekee }
27282e992cc0SKevin Cernekee 
27291e0c5b12SDmitry Torokhov static int alps_reconnect(struct psmouse *psmouse)
27301e0c5b12SDmitry Torokhov {
2731b5d6b851SKevin Cernekee 	struct alps_data *priv = psmouse->private;
273271bb21b6SMaxim Levitsky 
27331e0c5b12SDmitry Torokhov 	psmouse_reset(psmouse);
27341e0c5b12SDmitry Torokhov 
2735b5d6b851SKevin Cernekee 	if (alps_identify(psmouse, priv) < 0)
27361e0c5b12SDmitry Torokhov 		return -1;
27371e0c5b12SDmitry Torokhov 
273824af5cb9SKevin Cernekee 	return priv->hw_init(psmouse);
27391da177e4SLinus Torvalds }
27401da177e4SLinus Torvalds 
27411da177e4SLinus Torvalds static void alps_disconnect(struct psmouse *psmouse)
27421da177e4SLinus Torvalds {
27431da177e4SLinus Torvalds 	struct alps_data *priv = psmouse->private;
27442e5b636bSDmitry Torokhov 
27451da177e4SLinus Torvalds 	psmouse_reset(psmouse);
27461d9f2626SSebastian Kapfer 	del_timer_sync(&priv->timer);
274704aae283SPali Rohár 	if (priv->dev2)
27482e5b636bSDmitry Torokhov 		input_unregister_device(priv->dev2);
274904aae283SPali Rohár 	if (!IS_ERR_OR_NULL(priv->dev3))
275004aae283SPali Rohár 		input_unregister_device(priv->dev3);
27511da177e4SLinus Torvalds 	kfree(priv);
27521da177e4SLinus Torvalds }
27531da177e4SLinus Torvalds 
275424af5cb9SKevin Cernekee static void alps_set_abs_params_st(struct alps_data *priv,
275524af5cb9SKevin Cernekee 				   struct input_dev *dev1)
275624af5cb9SKevin Cernekee {
275795f75e91SYunkang Tang 	input_set_abs_params(dev1, ABS_X, 0, priv->x_max, 0, 0);
275895f75e91SYunkang Tang 	input_set_abs_params(dev1, ABS_Y, 0, priv->y_max, 0, 0);
27598eccd393SMasaki Ota 	input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
276024af5cb9SKevin Cernekee }
276124af5cb9SKevin Cernekee 
27628eccd393SMasaki Ota static void alps_set_abs_params_mt_common(struct alps_data *priv,
276324af5cb9SKevin Cernekee 					  struct input_dev *dev1)
276424af5cb9SKevin Cernekee {
27657a9f73e7SKevin Cernekee 	input_set_abs_params(dev1, ABS_MT_POSITION_X, 0, priv->x_max, 0, 0);
27667a9f73e7SKevin Cernekee 	input_set_abs_params(dev1, ABS_MT_POSITION_Y, 0, priv->y_max, 0, 0);
276724af5cb9SKevin Cernekee 
2768f3f33c67SHans de Goede 	input_abs_set_res(dev1, ABS_MT_POSITION_X, priv->x_res);
2769f3f33c67SHans de Goede 	input_abs_set_res(dev1, ABS_MT_POSITION_Y, priv->y_res);
2770f3f33c67SHans de Goede 
277124af5cb9SKevin Cernekee 	set_bit(BTN_TOOL_TRIPLETAP, dev1->keybit);
277224af5cb9SKevin Cernekee 	set_bit(BTN_TOOL_QUADTAP, dev1->keybit);
27738eccd393SMasaki Ota }
27743808843cSYunkang Tang 
2775688ea364SHans de Goede static void alps_set_abs_params_semi_mt(struct alps_data *priv,
27768eccd393SMasaki Ota 					struct input_dev *dev1)
27778eccd393SMasaki Ota {
27788eccd393SMasaki Ota 	alps_set_abs_params_mt_common(priv, dev1);
27798eccd393SMasaki Ota 	input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
27808eccd393SMasaki Ota 
27818eccd393SMasaki Ota 	input_mt_init_slots(dev1, MAX_TOUCHES,
27828eccd393SMasaki Ota 			    INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
27831662c033SHans de Goede 				INPUT_MT_SEMI_MT);
27848eccd393SMasaki Ota }
27858eccd393SMasaki Ota 
27868eccd393SMasaki Ota static void alps_set_abs_params_v7(struct alps_data *priv,
27878eccd393SMasaki Ota 				   struct input_dev *dev1)
27888eccd393SMasaki Ota {
27898eccd393SMasaki Ota 	alps_set_abs_params_mt_common(priv, dev1);
27908d289842SMasaki Ota 	set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
27918eccd393SMasaki Ota 
27928eccd393SMasaki Ota 	input_mt_init_slots(dev1, MAX_TOUCHES,
27938eccd393SMasaki Ota 			    INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
27948eccd393SMasaki Ota 				INPUT_MT_TRACK);
27953db5b9f7SMasaki Ota 
27963db5b9f7SMasaki Ota 	set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
27973db5b9f7SMasaki Ota }
27983db5b9f7SMasaki Ota 
27993db5b9f7SMasaki Ota static void alps_set_abs_params_ss4_v2(struct alps_data *priv,
28003db5b9f7SMasaki Ota 				       struct input_dev *dev1)
28013db5b9f7SMasaki Ota {
28023db5b9f7SMasaki Ota 	alps_set_abs_params_mt_common(priv, dev1);
28033db5b9f7SMasaki Ota 	input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
28043db5b9f7SMasaki Ota 	set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
28053db5b9f7SMasaki Ota 
28063db5b9f7SMasaki Ota 	input_mt_init_slots(dev1, MAX_TOUCHES,
28073db5b9f7SMasaki Ota 			    INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
28083db5b9f7SMasaki Ota 				INPUT_MT_TRACK);
280924af5cb9SKevin Cernekee }
281024af5cb9SKevin Cernekee 
28111da177e4SLinus Torvalds int alps_init(struct psmouse *psmouse)
28121da177e4SLinus Torvalds {
2813a09221e8SDmitry Torokhov 	struct alps_data *priv = psmouse->private;
281404aae283SPali Rohár 	struct input_dev *dev1 = psmouse->dev;
2815a09221e8SDmitry Torokhov 	int error;
28161da177e4SLinus Torvalds 
2817a09221e8SDmitry Torokhov 	error = priv->hw_init(psmouse);
2818a09221e8SDmitry Torokhov 	if (error)
28191da177e4SLinus Torvalds 		goto init_fail;
28201da177e4SLinus Torvalds 
28217105d2eaSDmitry Torokhov 	/*
28227105d2eaSDmitry Torokhov 	 * Undo part of setup done for us by psmouse core since touchpad
28237105d2eaSDmitry Torokhov 	 * is not a relative device.
28247105d2eaSDmitry Torokhov 	 */
28257105d2eaSDmitry Torokhov 	__clear_bit(EV_REL, dev1->evbit);
28267105d2eaSDmitry Torokhov 	__clear_bit(REL_X, dev1->relbit);
28277105d2eaSDmitry Torokhov 	__clear_bit(REL_Y, dev1->relbit);
28287105d2eaSDmitry Torokhov 
28297105d2eaSDmitry Torokhov 	/*
28307105d2eaSDmitry Torokhov 	 * Now set up our capabilities.
28317105d2eaSDmitry Torokhov 	 */
28327b19ada2SJiri Slaby 	dev1->evbit[BIT_WORD(EV_KEY)] |= BIT_MASK(EV_KEY);
28337b19ada2SJiri Slaby 	dev1->keybit[BIT_WORD(BTN_TOUCH)] |= BIT_MASK(BTN_TOUCH);
28347b19ada2SJiri Slaby 	dev1->keybit[BIT_WORD(BTN_TOOL_FINGER)] |= BIT_MASK(BTN_TOOL_FINGER);
283571bb21b6SMaxim Levitsky 	dev1->keybit[BIT_WORD(BTN_LEFT)] |=
283671bb21b6SMaxim Levitsky 		BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
28371da177e4SLinus Torvalds 
28387b19ada2SJiri Slaby 	dev1->evbit[BIT_WORD(EV_ABS)] |= BIT_MASK(EV_ABS);
283925bded7cSSeth Forshee 
284024af5cb9SKevin Cernekee 	priv->set_abs_params(priv, dev1);
28411da177e4SLinus Torvalds 
284299df65e7SKevin Cernekee 	if (priv->flags & ALPS_WHEEL) {
28437b19ada2SJiri Slaby 		dev1->evbit[BIT_WORD(EV_REL)] |= BIT_MASK(EV_REL);
28447b19ada2SJiri Slaby 		dev1->relbit[BIT_WORD(REL_WHEEL)] |= BIT_MASK(REL_WHEEL);
28451da177e4SLinus Torvalds 	}
28461da177e4SLinus Torvalds 
284799df65e7SKevin Cernekee 	if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
28487b19ada2SJiri Slaby 		dev1->keybit[BIT_WORD(BTN_FORWARD)] |= BIT_MASK(BTN_FORWARD);
28497b19ada2SJiri Slaby 		dev1->keybit[BIT_WORD(BTN_BACK)] |= BIT_MASK(BTN_BACK);
28501da177e4SLinus Torvalds 	}
28511da177e4SLinus Torvalds 
285299df65e7SKevin Cernekee 	if (priv->flags & ALPS_FOUR_BUTTONS) {
285371bb21b6SMaxim Levitsky 		dev1->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_0);
285471bb21b6SMaxim Levitsky 		dev1->keybit[BIT_WORD(BTN_1)] |= BIT_MASK(BTN_1);
285571bb21b6SMaxim Levitsky 		dev1->keybit[BIT_WORD(BTN_2)] |= BIT_MASK(BTN_2);
285671bb21b6SMaxim Levitsky 		dev1->keybit[BIT_WORD(BTN_3)] |= BIT_MASK(BTN_3);
28573808843cSYunkang Tang 	} else if (priv->flags & ALPS_BUTTONPAD) {
28583808843cSYunkang Tang 		set_bit(INPUT_PROP_BUTTONPAD, dev1->propbit);
28593808843cSYunkang Tang 		clear_bit(BTN_RIGHT, dev1->keybit);
286071bb21b6SMaxim Levitsky 	} else {
286171bb21b6SMaxim Levitsky 		dev1->keybit[BIT_WORD(BTN_MIDDLE)] |= BIT_MASK(BTN_MIDDLE);
286271bb21b6SMaxim Levitsky 	}
286371bb21b6SMaxim Levitsky 
2864dfba8600SPali Rohár 	if (priv->flags & ALPS_DUALPOINT) {
286504aae283SPali Rohár 		struct input_dev *dev2;
286604aae283SPali Rohár 
286704aae283SPali Rohár 		dev2 = input_allocate_device();
286804aae283SPali Rohár 		if (!dev2) {
286904aae283SPali Rohár 			psmouse_err(psmouse,
287004aae283SPali Rohár 				    "failed to allocate trackstick device\n");
287104aae283SPali Rohár 			error = -ENOMEM;
287204aae283SPali Rohár 			goto init_fail;
287304aae283SPali Rohár 		}
287404aae283SPali Rohár 
287504aae283SPali Rohár 		snprintf(priv->phys2, sizeof(priv->phys2), "%s/input1",
287604aae283SPali Rohár 			 psmouse->ps2dev.serio->phys);
287704aae283SPali Rohár 		dev2->phys = priv->phys2;
287804aae283SPali Rohár 
2879dfba8600SPali Rohár 		/*
2880dfba8600SPali Rohár 		 * format of input device name is: "protocol vendor name"
2881dfba8600SPali Rohár 		 * see function psmouse_switch_protocol() in psmouse-base.c
2882dfba8600SPali Rohár 		 */
2883dfba8600SPali Rohár 		dev2->name = "AlpsPS/2 ALPS DualPoint Stick";
2884dfba8600SPali Rohár 
28852e5b636bSDmitry Torokhov 		dev2->id.bustype = BUS_I8042;
28862e5b636bSDmitry Torokhov 		dev2->id.vendor  = 0x0002;
288704aae283SPali Rohár 		dev2->id.product = PSMOUSE_ALPS;
288804aae283SPali Rohár 		dev2->id.version = priv->proto_version;
28891db3a345SDmitry Torokhov 		dev2->dev.parent = &psmouse->ps2dev.serio->dev;
28901da177e4SLinus Torvalds 
289104aae283SPali Rohár 		input_set_capability(dev2, EV_REL, REL_X);
289204aae283SPali Rohár 		input_set_capability(dev2, EV_REL, REL_Y);
289304aae283SPali Rohár 		input_set_capability(dev2, EV_KEY, BTN_LEFT);
289404aae283SPali Rohár 		input_set_capability(dev2, EV_KEY, BTN_RIGHT);
289504aae283SPali Rohár 		input_set_capability(dev2, EV_KEY, BTN_MIDDLE);
28961da177e4SLinus Torvalds 
289701d4cd5cSHans de Goede 		__set_bit(INPUT_PROP_POINTER, dev2->propbit);
28987611392fSHans de Goede 		__set_bit(INPUT_PROP_POINTING_STICK, dev2->propbit);
28997611392fSHans de Goede 
290004aae283SPali Rohár 		error = input_register_device(dev2);
290104aae283SPali Rohár 		if (error) {
290204aae283SPali Rohár 			psmouse_err(psmouse,
290304aae283SPali Rohár 				    "failed to register trackstick device: %d\n",
290404aae283SPali Rohár 				    error);
290504aae283SPali Rohár 			input_free_device(dev2);
2906f42649e8SDmitry Torokhov 			goto init_fail;
290704aae283SPali Rohár 		}
290804aae283SPali Rohár 
290904aae283SPali Rohár 		priv->dev2 = dev2;
291004aae283SPali Rohár 	}
291104aae283SPali Rohár 
291204aae283SPali Rohár 	priv->psmouse = psmouse;
291304aae283SPali Rohár 
291404aae283SPali Rohár 	INIT_DELAYED_WORK(&priv->dev3_register_work,
291504aae283SPali Rohár 			  alps_register_bare_ps2_mouse);
29161da177e4SLinus Torvalds 
29171da177e4SLinus Torvalds 	psmouse->protocol_handler = alps_process_byte;
2918f0d5c6f4SDmitry Torokhov 	psmouse->poll = alps_poll;
29191da177e4SLinus Torvalds 	psmouse->disconnect = alps_disconnect;
29201da177e4SLinus Torvalds 	psmouse->reconnect = alps_reconnect;
292199df65e7SKevin Cernekee 	psmouse->pktsize = priv->proto_version == ALPS_PROTO_V4 ? 8 : 6;
29221da177e4SLinus Torvalds 
2923f0d5c6f4SDmitry Torokhov 	/* We are having trouble resyncing ALPS touchpads so disable it for now */
2924f0d5c6f4SDmitry Torokhov 	psmouse->resync_time = 0;
2925f0d5c6f4SDmitry Torokhov 
29269d720b34SPali Rohár 	/* Allow 2 invalid packets without resetting device */
29279d720b34SPali Rohár 	psmouse->resetafter = psmouse->pktsize * 2;
29289d720b34SPali Rohár 
29291da177e4SLinus Torvalds 	return 0;
29301da177e4SLinus Torvalds 
29311da177e4SLinus Torvalds init_fail:
2932f42649e8SDmitry Torokhov 	psmouse_reset(psmouse);
2933a09221e8SDmitry Torokhov 	/*
2934a09221e8SDmitry Torokhov 	 * Even though we did not allocate psmouse->private we do free
2935a09221e8SDmitry Torokhov 	 * it here.
2936a09221e8SDmitry Torokhov 	 */
2937a09221e8SDmitry Torokhov 	kfree(psmouse->private);
29381e0c5b12SDmitry Torokhov 	psmouse->private = NULL;
2939a09221e8SDmitry Torokhov 	return error;
29401da177e4SLinus Torvalds }
29411da177e4SLinus Torvalds 
2942b7802c5cSDmitry Torokhov int alps_detect(struct psmouse *psmouse, bool set_properties)
29431da177e4SLinus Torvalds {
2944a09221e8SDmitry Torokhov 	struct alps_data *priv;
2945a09221e8SDmitry Torokhov 	int error;
29461da177e4SLinus Torvalds 
2947a09221e8SDmitry Torokhov 	error = alps_identify(psmouse, NULL);
2948a09221e8SDmitry Torokhov 	if (error)
2949a09221e8SDmitry Torokhov 		return error;
2950a09221e8SDmitry Torokhov 
2951a09221e8SDmitry Torokhov 	/*
2952a09221e8SDmitry Torokhov 	 * Reset the device to make sure it is fully operational:
2953a09221e8SDmitry Torokhov 	 * on some laptops, like certain Dell Latitudes, we may
2954a09221e8SDmitry Torokhov 	 * fail to properly detect presence of trackstick if device
2955a09221e8SDmitry Torokhov 	 * has not been reset.
2956a09221e8SDmitry Torokhov 	 */
2957a09221e8SDmitry Torokhov 	psmouse_reset(psmouse);
2958a09221e8SDmitry Torokhov 
2959a09221e8SDmitry Torokhov 	priv = kzalloc(sizeof(struct alps_data), GFP_KERNEL);
2960a09221e8SDmitry Torokhov 	if (!priv)
2961a09221e8SDmitry Torokhov 		return -ENOMEM;
2962a09221e8SDmitry Torokhov 
2963a09221e8SDmitry Torokhov 	error = alps_identify(psmouse, priv);
296493050db2SDmitry Torokhov 	if (error) {
296593050db2SDmitry Torokhov 		kfree(priv);
2966a09221e8SDmitry Torokhov 		return error;
296793050db2SDmitry Torokhov 	}
29681da177e4SLinus Torvalds 
29691da177e4SLinus Torvalds 	if (set_properties) {
29701da177e4SLinus Torvalds 		psmouse->vendor = "ALPS";
2971a09221e8SDmitry Torokhov 		psmouse->name = priv->flags & ALPS_DUALPOINT ?
2972968ac842SDmitry Torokhov 				"DualPoint TouchPad" : "GlidePoint";
2973a09221e8SDmitry Torokhov 		psmouse->model = priv->proto_version;
2974a09221e8SDmitry Torokhov 	} else {
2975a09221e8SDmitry Torokhov 		/*
2976a09221e8SDmitry Torokhov 		 * Destroy alps_data structure we allocated earlier since
2977a09221e8SDmitry Torokhov 		 * this was just a "trial run". Otherwise we'll keep it
2978a09221e8SDmitry Torokhov 		 * to be used by alps_init() which has to be called if
2979a09221e8SDmitry Torokhov 		 * we succeed and set_properties is true.
2980a09221e8SDmitry Torokhov 		 */
2981a09221e8SDmitry Torokhov 		kfree(priv);
2982a09221e8SDmitry Torokhov 		psmouse->private = NULL;
29831da177e4SLinus Torvalds 	}
2984a09221e8SDmitry Torokhov 
29851da177e4SLinus Torvalds 	return 0;
29861da177e4SLinus Torvalds }
29871da177e4SLinus Torvalds 
2988