xref: /openbmc/linux/drivers/input/mouse/alps.c (revision 07f19e3d)
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);
16224af5cb9SKevin Cernekee static void alps_set_abs_params_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 
30501ce661fSSeth Forshee /*
306ee65d4b3SYunkang Tang  * Process bitmap data for V5 protocols. Return value is null.
307ee65d4b3SYunkang Tang  *
308ee65d4b3SYunkang Tang  * The bitmaps don't have enough data to track fingers, so this function
309ee65d4b3SYunkang Tang  * only generates points representing a bounding box of at most two contacts.
31002d04254SHans de Goede  * These two points are returned in fields->mt.
311ee65d4b3SYunkang Tang  */
312ee65d4b3SYunkang Tang static void alps_process_bitmap_dolphin(struct alps_data *priv,
31302d04254SHans de Goede 					struct alps_fields *fields)
314ee65d4b3SYunkang Tang {
315ee65d4b3SYunkang Tang 	int box_middle_x, box_middle_y;
316ee65d4b3SYunkang Tang 	unsigned int x_map, y_map;
317ee65d4b3SYunkang Tang 	unsigned char start_bit, end_bit;
318ee65d4b3SYunkang Tang 	unsigned char x_msb, x_lsb, y_msb, y_lsb;
319ee65d4b3SYunkang Tang 
320ee65d4b3SYunkang Tang 	x_map = fields->x_map;
321ee65d4b3SYunkang Tang 	y_map = fields->y_map;
322ee65d4b3SYunkang Tang 
323ee65d4b3SYunkang Tang 	if (!x_map || !y_map)
324ee65d4b3SYunkang Tang 		return;
325ee65d4b3SYunkang Tang 
326ee65d4b3SYunkang Tang 	/* Get Most-significant and Least-significant bit */
327ee65d4b3SYunkang Tang 	x_msb = fls(x_map);
328ee65d4b3SYunkang Tang 	x_lsb = ffs(x_map);
329ee65d4b3SYunkang Tang 	y_msb = fls(y_map);
330ee65d4b3SYunkang Tang 	y_lsb = ffs(y_map);
331ee65d4b3SYunkang Tang 
332ee65d4b3SYunkang Tang 	/* Most-significant bit should never exceed max sensor line number */
333ee65d4b3SYunkang Tang 	if (x_msb > priv->x_bits || y_msb > priv->y_bits)
334ee65d4b3SYunkang Tang 		return;
335ee65d4b3SYunkang Tang 
336ee65d4b3SYunkang Tang 	if (fields->fingers > 1) {
337ee65d4b3SYunkang Tang 		start_bit = priv->x_bits - x_msb;
338ee65d4b3SYunkang Tang 		end_bit = priv->x_bits - x_lsb;
339ee65d4b3SYunkang Tang 		box_middle_x = (priv->x_max * (start_bit + end_bit)) /
340ee65d4b3SYunkang Tang 				(2 * (priv->x_bits - 1));
341ee65d4b3SYunkang Tang 
342ee65d4b3SYunkang Tang 		start_bit = y_lsb - 1;
343ee65d4b3SYunkang Tang 		end_bit = y_msb - 1;
344ee65d4b3SYunkang Tang 		box_middle_y = (priv->y_max * (start_bit + end_bit)) /
345ee65d4b3SYunkang Tang 				(2 * (priv->y_bits - 1));
34602d04254SHans de Goede 		fields->mt[0] = fields->st;
34702d04254SHans de Goede 		fields->mt[1].x = 2 * box_middle_x - fields->mt[0].x;
34802d04254SHans de Goede 		fields->mt[1].y = 2 * box_middle_y - fields->mt[0].y;
349ee65d4b3SYunkang Tang 	}
350ee65d4b3SYunkang Tang }
351ee65d4b3SYunkang Tang 
352036e6c7bSHans de Goede static void alps_get_bitmap_points(unsigned int map,
353036e6c7bSHans de Goede 				   struct alps_bitmap_point *low,
354036e6c7bSHans de Goede 				   struct alps_bitmap_point *high,
355036e6c7bSHans de Goede 				   int *fingers)
356036e6c7bSHans de Goede {
357036e6c7bSHans de Goede 	struct alps_bitmap_point *point;
358036e6c7bSHans de Goede 	int i, bit, prev_bit = 0;
359036e6c7bSHans de Goede 
360036e6c7bSHans de Goede 	point = low;
361036e6c7bSHans de Goede 	for (i = 0; map != 0; i++, map >>= 1) {
362036e6c7bSHans de Goede 		bit = map & 1;
363036e6c7bSHans de Goede 		if (bit) {
364036e6c7bSHans de Goede 			if (!prev_bit) {
365036e6c7bSHans de Goede 				point->start_bit = i;
366105affbfSHans de Goede 				point->num_bits = 0;
367036e6c7bSHans de Goede 				(*fingers)++;
368036e6c7bSHans de Goede 			}
369036e6c7bSHans de Goede 			point->num_bits++;
370036e6c7bSHans de Goede 		} else {
371036e6c7bSHans de Goede 			if (prev_bit)
372036e6c7bSHans de Goede 				point = high;
373036e6c7bSHans de Goede 		}
374036e6c7bSHans de Goede 		prev_bit = bit;
375036e6c7bSHans de Goede 	}
376036e6c7bSHans de Goede }
377036e6c7bSHans de Goede 
378ee65d4b3SYunkang Tang /*
37901ce661fSSeth Forshee  * Process bitmap data from v3 and v4 protocols. Returns the number of
38001ce661fSSeth Forshee  * fingers detected. A return value of 0 means at least one of the
38101ce661fSSeth Forshee  * bitmaps was empty.
38201ce661fSSeth Forshee  *
38301ce661fSSeth Forshee  * The bitmaps don't have enough data to track fingers, so this function
38401ce661fSSeth Forshee  * only generates points representing a bounding box of all contacts.
38502d04254SHans de Goede  * These points are returned in fields->mt when the return value
38601ce661fSSeth Forshee  * is greater than 0.
38701ce661fSSeth Forshee  */
3887a9f73e7SKevin Cernekee static int alps_process_bitmap(struct alps_data *priv,
38902d04254SHans de Goede 			       struct alps_fields *fields)
39001ce661fSSeth Forshee {
391036e6c7bSHans de Goede 	int i, fingers_x = 0, fingers_y = 0, fingers;
39201ce661fSSeth Forshee 	struct alps_bitmap_point x_low = {0,}, x_high = {0,};
39301ce661fSSeth Forshee 	struct alps_bitmap_point y_low = {0,}, y_high = {0,};
39401ce661fSSeth Forshee 
39502d04254SHans de Goede 	if (!fields->x_map || !fields->y_map)
39601ce661fSSeth Forshee 		return 0;
39701ce661fSSeth Forshee 
39802d04254SHans de Goede 	alps_get_bitmap_points(fields->x_map, &x_low, &x_high, &fingers_x);
39902d04254SHans de Goede 	alps_get_bitmap_points(fields->y_map, &y_low, &y_high, &fingers_y);
40001ce661fSSeth Forshee 
40101ce661fSSeth Forshee 	/*
40201ce661fSSeth Forshee 	 * Fingers can overlap, so we use the maximum count of fingers
40301ce661fSSeth Forshee 	 * on either axis as the finger count.
40401ce661fSSeth Forshee 	 */
40501ce661fSSeth Forshee 	fingers = max(fingers_x, fingers_y);
40601ce661fSSeth Forshee 
40701ce661fSSeth Forshee 	/*
40820bea68bSHans de Goede 	 * If an axis reports only a single contact, we have overlapping or
40920bea68bSHans de Goede 	 * adjacent fingers. Divide the single contact between the two points.
41001ce661fSSeth Forshee 	 */
41101ce661fSSeth Forshee 	if (fingers_x == 1) {
41228835f45SHans de Goede 		i = (x_low.num_bits - 1) / 2;
41301ce661fSSeth Forshee 		x_low.num_bits = x_low.num_bits - i;
41401ce661fSSeth Forshee 		x_high.start_bit = x_low.start_bit + i;
41501ce661fSSeth Forshee 		x_high.num_bits = max(i, 1);
41620bea68bSHans de Goede 	}
41720bea68bSHans de Goede 	if (fingers_y == 1) {
41828835f45SHans de Goede 		i = (y_low.num_bits - 1) / 2;
41901ce661fSSeth Forshee 		y_low.num_bits = y_low.num_bits - i;
42001ce661fSSeth Forshee 		y_high.start_bit = y_low.start_bit + i;
42101ce661fSSeth Forshee 		y_high.num_bits = max(i, 1);
42201ce661fSSeth Forshee 	}
42301ce661fSSeth Forshee 
42402d04254SHans de Goede 	fields->mt[0].x =
42502d04254SHans de Goede 		(priv->x_max * (2 * x_low.start_bit + x_low.num_bits - 1)) /
4267a9f73e7SKevin Cernekee 		(2 * (priv->x_bits - 1));
42702d04254SHans de Goede 	fields->mt[0].y =
42802d04254SHans de Goede 		(priv->y_max * (2 * y_low.start_bit + y_low.num_bits - 1)) /
4297a9f73e7SKevin Cernekee 		(2 * (priv->y_bits - 1));
43001ce661fSSeth Forshee 
43102d04254SHans de Goede 	fields->mt[1].x =
43202d04254SHans de Goede 		(priv->x_max * (2 * x_high.start_bit + x_high.num_bits - 1)) /
4337a9f73e7SKevin Cernekee 		(2 * (priv->x_bits - 1));
43402d04254SHans de Goede 	fields->mt[1].y =
43502d04254SHans de Goede 		(priv->y_max * (2 * y_high.start_bit + y_high.num_bits - 1)) /
4367a9f73e7SKevin Cernekee 		(2 * (priv->y_bits - 1));
43701ce661fSSeth Forshee 
43840e8f53bSHans de Goede 	/* y-bitmap order is reversed, except on rushmore */
439fb2dd7a6SDmitry Torokhov 	if (priv->proto_version != ALPS_PROTO_V3_RUSHMORE) {
44002d04254SHans de Goede 		fields->mt[0].y = priv->y_max - fields->mt[0].y;
44102d04254SHans de Goede 		fields->mt[1].y = priv->y_max - fields->mt[1].y;
44240e8f53bSHans de Goede 	}
44340e8f53bSHans de Goede 
44401ce661fSSeth Forshee 	return fingers;
44501ce661fSSeth Forshee }
44601ce661fSSeth Forshee 
447cdf333efSHans de Goede static void alps_set_slot(struct input_dev *dev, int slot, int x, int y)
44801ce661fSSeth Forshee {
44901ce661fSSeth Forshee 	input_mt_slot(dev, slot);
450cdf333efSHans de Goede 	input_mt_report_slot_state(dev, MT_TOOL_FINGER, true);
45101ce661fSSeth Forshee 	input_report_abs(dev, ABS_MT_POSITION_X, x);
45201ce661fSSeth Forshee 	input_report_abs(dev, ABS_MT_POSITION_Y, y);
45301ce661fSSeth Forshee }
45401ce661fSSeth Forshee 
455cdf333efSHans de Goede static void alps_report_mt_data(struct psmouse *psmouse, int n)
45601ce661fSSeth Forshee {
45702d04254SHans de Goede 	struct alps_data *priv = psmouse->private;
45802d04254SHans de Goede 	struct input_dev *dev = psmouse->dev;
45902d04254SHans de Goede 	struct alps_fields *f = &priv->f;
460cdf333efSHans de Goede 	int i, slot[MAX_TOUCHES];
46102d04254SHans de Goede 
462448c7f38SHenrik Rydberg 	input_mt_assign_slots(dev, slot, f->mt, n, 0);
463cdf333efSHans de Goede 	for (i = 0; i < n; i++)
464cdf333efSHans de Goede 		alps_set_slot(dev, slot[i], f->mt[i].x, f->mt[i].y);
465cdf333efSHans de Goede 
466cdf333efSHans de Goede 	input_mt_sync_frame(dev);
46701ce661fSSeth Forshee }
46801ce661fSSeth Forshee 
46968c21870SHans de Goede static void alps_report_semi_mt_data(struct psmouse *psmouse, int fingers)
47068c21870SHans de Goede {
47168c21870SHans de Goede 	struct alps_data *priv = psmouse->private;
47268c21870SHans de Goede 	struct input_dev *dev = psmouse->dev;
47368c21870SHans de Goede 	struct alps_fields *f = &priv->f;
47468c21870SHans de Goede 
47568c21870SHans de Goede 	/* Use st data when we don't have mt data */
47668c21870SHans de Goede 	if (fingers < 2) {
47768c21870SHans de Goede 		f->mt[0].x = f->st.x;
47868c21870SHans de Goede 		f->mt[0].y = f->st.y;
47968c21870SHans de Goede 		fingers = f->pressure > 0 ? 1 : 0;
48068c21870SHans de Goede 	}
48168c21870SHans de Goede 
48299d9996cSHans de Goede 	alps_report_mt_data(psmouse, (fingers <= 2) ? fingers : 2);
48368c21870SHans de Goede 
48468c21870SHans de Goede 	input_mt_report_finger_count(dev, fingers);
48568c21870SHans de Goede 
48668c21870SHans de Goede 	input_report_key(dev, BTN_LEFT, f->left);
48768c21870SHans de Goede 	input_report_key(dev, BTN_RIGHT, f->right);
48868c21870SHans de Goede 	input_report_key(dev, BTN_MIDDLE, f->middle);
48968c21870SHans de Goede 
49068c21870SHans de Goede 	input_report_abs(dev, ABS_PRESSURE, f->pressure);
49168c21870SHans de Goede 
49268c21870SHans de Goede 	input_sync(dev);
49368c21870SHans de Goede }
49468c21870SHans de Goede 
49525bded7cSSeth Forshee static void alps_process_trackstick_packet_v3(struct psmouse *psmouse)
49625bded7cSSeth Forshee {
49725bded7cSSeth Forshee 	struct alps_data *priv = psmouse->private;
49825bded7cSSeth Forshee 	unsigned char *packet = psmouse->packet;
49925bded7cSSeth Forshee 	struct input_dev *dev = priv->dev2;
50025bded7cSSeth Forshee 	int x, y, z, left, right, middle;
50125bded7cSSeth Forshee 
50234412ba2SPali Rohár 	/* It should be a DualPoint when received trackstick packet */
50334412ba2SPali Rohár 	if (!(priv->flags & ALPS_DUALPOINT)) {
50434412ba2SPali Rohár 		psmouse_warn(psmouse,
50534412ba2SPali Rohár 			     "Rejected trackstick packet from non DualPoint device");
50634412ba2SPali Rohár 		return;
50734412ba2SPali Rohár 	}
50834412ba2SPali Rohár 
50925bded7cSSeth Forshee 	/* Sanity check packet */
51025bded7cSSeth Forshee 	if (!(packet[0] & 0x40)) {
51125bded7cSSeth Forshee 		psmouse_dbg(psmouse, "Bad trackstick packet, discarding\n");
51225bded7cSSeth Forshee 		return;
51325bded7cSSeth Forshee 	}
51425bded7cSSeth Forshee 
51525bded7cSSeth Forshee 	/*
51625bded7cSSeth Forshee 	 * There's a special packet that seems to indicate the end
51725bded7cSSeth Forshee 	 * of a stream of trackstick data. Filter these out.
51825bded7cSSeth Forshee 	 */
51925bded7cSSeth Forshee 	if (packet[1] == 0x7f && packet[2] == 0x7f && packet[4] == 0x7f)
52025bded7cSSeth Forshee 		return;
52125bded7cSSeth Forshee 
52225bded7cSSeth Forshee 	x = (s8)(((packet[0] & 0x20) << 2) | (packet[1] & 0x7f));
52325bded7cSSeth Forshee 	y = (s8)(((packet[0] & 0x10) << 3) | (packet[2] & 0x7f));
52425bded7cSSeth Forshee 	z = (packet[4] & 0x7c) >> 2;
52525bded7cSSeth Forshee 
52625bded7cSSeth Forshee 	/*
52725bded7cSSeth Forshee 	 * The x and y values tend to be quite large, and when used
52825bded7cSSeth Forshee 	 * alone the trackstick is difficult to use. Scale them down
52925bded7cSSeth Forshee 	 * to compensate.
53025bded7cSSeth Forshee 	 */
53125bded7cSSeth Forshee 	x /= 8;
53225bded7cSSeth Forshee 	y /= 8;
53325bded7cSSeth Forshee 
53425bded7cSSeth Forshee 	input_report_rel(dev, REL_X, x);
53525bded7cSSeth Forshee 	input_report_rel(dev, REL_Y, -y);
53625bded7cSSeth Forshee 
53725bded7cSSeth Forshee 	/*
53825bded7cSSeth Forshee 	 * Most ALPS models report the trackstick buttons in the touchpad
53925bded7cSSeth Forshee 	 * packets, but a few report them here. No reliable way has been
54025bded7cSSeth Forshee 	 * found to differentiate between the models upfront, so we enable
54125bded7cSSeth Forshee 	 * the quirk in response to seeing a button press in the trackstick
54225bded7cSSeth Forshee 	 * packet.
54325bded7cSSeth Forshee 	 */
54425bded7cSSeth Forshee 	left = packet[3] & 0x01;
54525bded7cSSeth Forshee 	right = packet[3] & 0x02;
54625bded7cSSeth Forshee 	middle = packet[3] & 0x04;
54725bded7cSSeth Forshee 
54825bded7cSSeth Forshee 	if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) &&
54925bded7cSSeth Forshee 	    (left || right || middle))
55025bded7cSSeth Forshee 		priv->quirks |= ALPS_QUIRK_TRACKSTICK_BUTTONS;
55125bded7cSSeth Forshee 
55225bded7cSSeth Forshee 	if (priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) {
55325bded7cSSeth Forshee 		input_report_key(dev, BTN_LEFT, left);
55425bded7cSSeth Forshee 		input_report_key(dev, BTN_RIGHT, right);
55525bded7cSSeth Forshee 		input_report_key(dev, BTN_MIDDLE, middle);
55625bded7cSSeth Forshee 	}
55725bded7cSSeth Forshee 
55825bded7cSSeth Forshee 	input_sync(dev);
55925bded7cSSeth Forshee 	return;
56025bded7cSSeth Forshee }
56125bded7cSSeth Forshee 
562f85e5001SKevin Cernekee static void alps_decode_buttons_v3(struct alps_fields *f, unsigned char *p)
563f85e5001SKevin Cernekee {
564f85e5001SKevin Cernekee 	f->left = !!(p[3] & 0x01);
565f85e5001SKevin Cernekee 	f->right = !!(p[3] & 0x02);
566f85e5001SKevin Cernekee 	f->middle = !!(p[3] & 0x04);
567f85e5001SKevin Cernekee 
568f85e5001SKevin Cernekee 	f->ts_left = !!(p[3] & 0x10);
569f85e5001SKevin Cernekee 	f->ts_right = !!(p[3] & 0x20);
570f85e5001SKevin Cernekee 	f->ts_middle = !!(p[3] & 0x40);
571f85e5001SKevin Cernekee }
572f85e5001SKevin Cernekee 
57338c11eaaSHans de Goede static int alps_decode_pinnacle(struct alps_fields *f, unsigned char *p,
574ee65d4b3SYunkang Tang 				 struct psmouse *psmouse)
575f85e5001SKevin Cernekee {
576f85e5001SKevin Cernekee 	f->first_mp = !!(p[4] & 0x40);
577f85e5001SKevin Cernekee 	f->is_mp = !!(p[0] & 0x40);
578f85e5001SKevin Cernekee 
579f85e5001SKevin Cernekee 	f->fingers = (p[5] & 0x3) + 1;
580f85e5001SKevin Cernekee 	f->x_map = ((p[4] & 0x7e) << 8) |
581f85e5001SKevin Cernekee 		   ((p[1] & 0x7f) << 2) |
582f85e5001SKevin Cernekee 		   ((p[0] & 0x30) >> 4);
583f85e5001SKevin Cernekee 	f->y_map = ((p[3] & 0x70) << 4) |
584f85e5001SKevin Cernekee 		   ((p[2] & 0x7f) << 1) |
585f85e5001SKevin Cernekee 		   (p[4] & 0x01);
586f85e5001SKevin Cernekee 
58702d04254SHans de Goede 	f->st.x = ((p[1] & 0x7f) << 4) | ((p[4] & 0x30) >> 2) |
588f85e5001SKevin Cernekee 	       ((p[0] & 0x30) >> 4);
58902d04254SHans de Goede 	f->st.y = ((p[2] & 0x7f) << 4) | (p[4] & 0x0f);
59002d04254SHans de Goede 	f->pressure = p[5] & 0x7f;
591f85e5001SKevin Cernekee 
592f85e5001SKevin Cernekee 	alps_decode_buttons_v3(f, p);
59338c11eaaSHans de Goede 
59438c11eaaSHans de Goede 	return 0;
595f85e5001SKevin Cernekee }
596f85e5001SKevin Cernekee 
59738c11eaaSHans de Goede static int alps_decode_rushmore(struct alps_fields *f, unsigned char *p,
598ee65d4b3SYunkang Tang 				 struct psmouse *psmouse)
5991302bac3SKevin Cernekee {
600ee65d4b3SYunkang Tang 	alps_decode_pinnacle(f, p, psmouse);
6011302bac3SKevin Cernekee 
602f105e34aSYunkang Tang 	/* Rushmore's packet decode has a bit difference with Pinnacle's */
603f105e34aSYunkang Tang 	f->is_mp = !!(p[5] & 0x40);
604f105e34aSYunkang Tang 	f->fingers = max((p[5] & 0x3), ((p[5] >> 2) & 0x3)) + 1;
6051302bac3SKevin Cernekee 	f->x_map |= (p[5] & 0x10) << 11;
6061302bac3SKevin Cernekee 	f->y_map |= (p[5] & 0x20) << 6;
60738c11eaaSHans de Goede 
60838c11eaaSHans de Goede 	return 0;
6091302bac3SKevin Cernekee }
6101302bac3SKevin Cernekee 
61138c11eaaSHans de Goede static int alps_decode_dolphin(struct alps_fields *f, unsigned char *p,
612ee65d4b3SYunkang Tang 				struct psmouse *psmouse)
61375af9e56SDave Turvene {
614ee65d4b3SYunkang Tang 	u64 palm_data = 0;
615ee65d4b3SYunkang Tang 	struct alps_data *priv = psmouse->private;
616ee65d4b3SYunkang Tang 
61775af9e56SDave Turvene 	f->first_mp = !!(p[0] & 0x02);
61875af9e56SDave Turvene 	f->is_mp = !!(p[0] & 0x20);
61975af9e56SDave Turvene 
620ee65d4b3SYunkang Tang 	if (!f->is_mp) {
62102d04254SHans de Goede 		f->st.x = ((p[1] & 0x7f) | ((p[4] & 0x0f) << 7));
62202d04254SHans de Goede 		f->st.y = ((p[2] & 0x7f) | ((p[4] & 0xf0) << 3));
62302d04254SHans de Goede 		f->pressure = (p[0] & 4) ? 0 : p[5] & 0x7f;
62475af9e56SDave Turvene 		alps_decode_buttons_v3(f, p);
625ee65d4b3SYunkang Tang 	} else {
626ee65d4b3SYunkang Tang 		f->fingers = ((p[0] & 0x6) >> 1 |
627ee65d4b3SYunkang Tang 		     (p[0] & 0x10) >> 2);
628ee65d4b3SYunkang Tang 
629ee65d4b3SYunkang Tang 		palm_data = (p[1] & 0x7f) |
630ee65d4b3SYunkang Tang 			    ((p[2] & 0x7f) << 7) |
631ee65d4b3SYunkang Tang 			    ((p[4] & 0x7f) << 14) |
632ee65d4b3SYunkang Tang 			    ((p[5] & 0x7f) << 21) |
633ee65d4b3SYunkang Tang 			    ((p[3] & 0x07) << 28) |
634ee65d4b3SYunkang Tang 			    (((u64)p[3] & 0x70) << 27) |
635ee65d4b3SYunkang Tang 			    (((u64)p[0] & 0x01) << 34);
636ee65d4b3SYunkang Tang 
637ee65d4b3SYunkang Tang 		/* Y-profile is stored in P(0) to p(n-1), n = y_bits; */
638ee65d4b3SYunkang Tang 		f->y_map = palm_data & (BIT(priv->y_bits) - 1);
639ee65d4b3SYunkang Tang 
640ee65d4b3SYunkang Tang 		/* X-profile is stored in p(n) to p(n+m-1), m = x_bits; */
641ee65d4b3SYunkang Tang 		f->x_map = (palm_data >> priv->y_bits) &
642ee65d4b3SYunkang Tang 			   (BIT(priv->x_bits) - 1);
643ee65d4b3SYunkang Tang 	}
64438c11eaaSHans de Goede 
64538c11eaaSHans de Goede 	return 0;
64675af9e56SDave Turvene }
64775af9e56SDave Turvene 
648ee65d4b3SYunkang Tang static void alps_process_touchpad_packet_v3_v5(struct psmouse *psmouse)
64925bded7cSSeth Forshee {
65025bded7cSSeth Forshee 	struct alps_data *priv = psmouse->private;
65125bded7cSSeth Forshee 	unsigned char *packet = psmouse->packet;
65225bded7cSSeth Forshee 	struct input_dev *dev2 = priv->dev2;
65302d04254SHans de Goede 	struct alps_fields *f = &priv->f;
65402d04254SHans de Goede 	int fingers = 0;
655f85e5001SKevin Cernekee 
65602d04254SHans de Goede 	memset(f, 0, sizeof(*f));
65702d04254SHans de Goede 
65802d04254SHans de Goede 	priv->decode_fields(f, packet, psmouse);
65925bded7cSSeth Forshee 
66025bded7cSSeth Forshee 	/*
66101ce661fSSeth Forshee 	 * There's no single feature of touchpad position and bitmap packets
66201ce661fSSeth Forshee 	 * that can be used to distinguish between them. We rely on the fact
66301ce661fSSeth Forshee 	 * that a bitmap packet should always follow a position packet with
66401ce661fSSeth Forshee 	 * bit 6 of packet[4] set.
66525bded7cSSeth Forshee 	 */
66625bded7cSSeth Forshee 	if (priv->multi_packet) {
66725bded7cSSeth Forshee 		/*
66825bded7cSSeth Forshee 		 * Sometimes a position packet will indicate a multi-packet
66925bded7cSSeth Forshee 		 * sequence, but then what follows is another position
67025bded7cSSeth Forshee 		 * packet. Check for this, and when it happens process the
67125bded7cSSeth Forshee 		 * position packet as usual.
67225bded7cSSeth Forshee 		 */
67302d04254SHans de Goede 		if (f->is_mp) {
67402d04254SHans de Goede 			fingers = f->fingers;
675fb2dd7a6SDmitry Torokhov 			if (priv->proto_version == ALPS_PROTO_V3 ||
676fb2dd7a6SDmitry Torokhov 			    priv->proto_version == ALPS_PROTO_V3_RUSHMORE) {
67702d04254SHans de Goede 				if (alps_process_bitmap(priv, f) == 0)
67820bea68bSHans de Goede 					fingers = 0; /* Use st data */
67901ce661fSSeth Forshee 
68001ce661fSSeth Forshee 				/* Now process position packet */
68102d04254SHans de Goede 				priv->decode_fields(f, priv->multi_data,
682ee65d4b3SYunkang Tang 						    psmouse);
683ee65d4b3SYunkang Tang 			} else {
684ee65d4b3SYunkang Tang 				/*
685ee65d4b3SYunkang Tang 				 * Because Dolphin uses position packet's
686ee65d4b3SYunkang Tang 				 * coordinate data as Pt1 and uses it to
687ee65d4b3SYunkang Tang 				 * calculate Pt2, so we need to do position
688ee65d4b3SYunkang Tang 				 * packet decode first.
689ee65d4b3SYunkang Tang 				 */
69002d04254SHans de Goede 				priv->decode_fields(f, priv->multi_data,
691ee65d4b3SYunkang Tang 						    psmouse);
692ee65d4b3SYunkang Tang 
693ee65d4b3SYunkang Tang 				/*
694ee65d4b3SYunkang Tang 				 * Since Dolphin's finger number is reliable,
695ee65d4b3SYunkang Tang 				 * there is no need to compare with bmap_fn.
696ee65d4b3SYunkang Tang 				 */
69702d04254SHans de Goede 				alps_process_bitmap_dolphin(priv, f);
698ee65d4b3SYunkang Tang 			}
69901ce661fSSeth Forshee 		} else {
70001ce661fSSeth Forshee 			priv->multi_packet = 0;
70125bded7cSSeth Forshee 		}
70225bded7cSSeth Forshee 	}
70325bded7cSSeth Forshee 
70401ce661fSSeth Forshee 	/*
70501ce661fSSeth Forshee 	 * Bit 6 of byte 0 is not usually set in position packets. The only
70601ce661fSSeth Forshee 	 * times it seems to be set is in situations where the data is
70701ce661fSSeth Forshee 	 * suspect anyway, e.g. a palm resting flat on the touchpad. Given
70801ce661fSSeth Forshee 	 * this combined with the fact that this bit is useful for filtering
70901ce661fSSeth Forshee 	 * out misidentified bitmap packets, we reject anything with this
71001ce661fSSeth Forshee 	 * bit set.
71101ce661fSSeth Forshee 	 */
71202d04254SHans de Goede 	if (f->is_mp)
71301ce661fSSeth Forshee 		return;
71401ce661fSSeth Forshee 
71502d04254SHans de Goede 	if (!priv->multi_packet && f->first_mp) {
71625bded7cSSeth Forshee 		priv->multi_packet = 1;
71701ce661fSSeth Forshee 		memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
71801ce661fSSeth Forshee 		return;
71901ce661fSSeth Forshee 	}
72001ce661fSSeth Forshee 
72125bded7cSSeth Forshee 	priv->multi_packet = 0;
72225bded7cSSeth Forshee 
72325bded7cSSeth Forshee 	/*
72425bded7cSSeth Forshee 	 * Sometimes the hardware sends a single packet with z = 0
72525bded7cSSeth Forshee 	 * in the middle of a stream. Real releases generate packets
72625bded7cSSeth Forshee 	 * with x, y, and z all zero, so these seem to be flukes.
72725bded7cSSeth Forshee 	 * Ignore them.
72825bded7cSSeth Forshee 	 */
72902d04254SHans de Goede 	if (f->st.x && f->st.y && !f->pressure)
73025bded7cSSeth Forshee 		return;
73125bded7cSSeth Forshee 
73268c21870SHans de Goede 	alps_report_semi_mt_data(psmouse, fingers);
73325bded7cSSeth Forshee 
73434412ba2SPali Rohár 	if ((priv->flags & ALPS_DUALPOINT) &&
73534412ba2SPali Rohár 	    !(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS)) {
73602d04254SHans de Goede 		input_report_key(dev2, BTN_LEFT, f->ts_left);
73702d04254SHans de Goede 		input_report_key(dev2, BTN_RIGHT, f->ts_right);
73802d04254SHans de Goede 		input_report_key(dev2, BTN_MIDDLE, f->ts_middle);
73925bded7cSSeth Forshee 		input_sync(dev2);
74025bded7cSSeth Forshee 	}
74125bded7cSSeth Forshee }
74225bded7cSSeth Forshee 
74325bded7cSSeth Forshee static void alps_process_packet_v3(struct psmouse *psmouse)
74425bded7cSSeth Forshee {
74525bded7cSSeth Forshee 	unsigned char *packet = psmouse->packet;
74625bded7cSSeth Forshee 
74725bded7cSSeth Forshee 	/*
74825bded7cSSeth Forshee 	 * v3 protocol packets come in three types, two representing
74925bded7cSSeth Forshee 	 * touchpad data and one representing trackstick data.
75025bded7cSSeth Forshee 	 * Trackstick packets seem to be distinguished by always
75125bded7cSSeth Forshee 	 * having 0x3f in the last byte. This value has never been
75225bded7cSSeth Forshee 	 * observed in the last byte of either of the other types
75325bded7cSSeth Forshee 	 * of packets.
75425bded7cSSeth Forshee 	 */
75525bded7cSSeth Forshee 	if (packet[5] == 0x3f) {
75625bded7cSSeth Forshee 		alps_process_trackstick_packet_v3(psmouse);
75725bded7cSSeth Forshee 		return;
75825bded7cSSeth Forshee 	}
75925bded7cSSeth Forshee 
760ee65d4b3SYunkang Tang 	alps_process_touchpad_packet_v3_v5(psmouse);
76125bded7cSSeth Forshee }
76225bded7cSSeth Forshee 
76395f75e91SYunkang Tang static void alps_process_packet_v6(struct psmouse *psmouse)
76495f75e91SYunkang Tang {
76595f75e91SYunkang Tang 	struct alps_data *priv = psmouse->private;
76695f75e91SYunkang Tang 	unsigned char *packet = psmouse->packet;
76795f75e91SYunkang Tang 	struct input_dev *dev = psmouse->dev;
76895f75e91SYunkang Tang 	struct input_dev *dev2 = priv->dev2;
76995f75e91SYunkang Tang 	int x, y, z, left, right, middle;
77095f75e91SYunkang Tang 
77195f75e91SYunkang Tang 	/*
77295f75e91SYunkang Tang 	 * We can use Byte5 to distinguish if the packet is from Touchpad
77395f75e91SYunkang Tang 	 * or Trackpoint.
77495f75e91SYunkang Tang 	 * Touchpad:	0 - 0x7E
77595f75e91SYunkang Tang 	 * Trackpoint:	0x7F
77695f75e91SYunkang Tang 	 */
77795f75e91SYunkang Tang 	if (packet[5] == 0x7F) {
77895f75e91SYunkang Tang 		/* It should be a DualPoint when received Trackpoint packet */
77934412ba2SPali Rohár 		if (!(priv->flags & ALPS_DUALPOINT)) {
78034412ba2SPali Rohár 			psmouse_warn(psmouse,
78134412ba2SPali Rohár 				     "Rejected trackstick packet from non DualPoint device");
78295f75e91SYunkang Tang 			return;
78334412ba2SPali Rohár 		}
78495f75e91SYunkang Tang 
78595f75e91SYunkang Tang 		/* Trackpoint packet */
78695f75e91SYunkang Tang 		x = packet[1] | ((packet[3] & 0x20) << 2);
78795f75e91SYunkang Tang 		y = packet[2] | ((packet[3] & 0x40) << 1);
78895f75e91SYunkang Tang 		z = packet[4];
78995f75e91SYunkang Tang 		left = packet[3] & 0x01;
79095f75e91SYunkang Tang 		right = packet[3] & 0x02;
79195f75e91SYunkang Tang 		middle = packet[3] & 0x04;
79295f75e91SYunkang Tang 
79395f75e91SYunkang Tang 		/* To prevent the cursor jump when finger lifted */
79495f75e91SYunkang Tang 		if (x == 0x7F && y == 0x7F && z == 0x7F)
79595f75e91SYunkang Tang 			x = y = z = 0;
79695f75e91SYunkang Tang 
79795f75e91SYunkang Tang 		/* Divide 4 since trackpoint's speed is too fast */
79895f75e91SYunkang Tang 		input_report_rel(dev2, REL_X, (char)x / 4);
79995f75e91SYunkang Tang 		input_report_rel(dev2, REL_Y, -((char)y / 4));
80095f75e91SYunkang Tang 
80195f75e91SYunkang Tang 		input_report_key(dev2, BTN_LEFT, left);
80295f75e91SYunkang Tang 		input_report_key(dev2, BTN_RIGHT, right);
80395f75e91SYunkang Tang 		input_report_key(dev2, BTN_MIDDLE, middle);
80495f75e91SYunkang Tang 
80595f75e91SYunkang Tang 		input_sync(dev2);
80695f75e91SYunkang Tang 		return;
80795f75e91SYunkang Tang 	}
80895f75e91SYunkang Tang 
80995f75e91SYunkang Tang 	/* Touchpad packet */
81095f75e91SYunkang Tang 	x = packet[1] | ((packet[3] & 0x78) << 4);
81195f75e91SYunkang Tang 	y = packet[2] | ((packet[4] & 0x78) << 4);
81295f75e91SYunkang Tang 	z = packet[5];
81395f75e91SYunkang Tang 	left = packet[3] & 0x01;
81495f75e91SYunkang Tang 	right = packet[3] & 0x02;
81595f75e91SYunkang Tang 
81695f75e91SYunkang Tang 	if (z > 30)
81795f75e91SYunkang Tang 		input_report_key(dev, BTN_TOUCH, 1);
81895f75e91SYunkang Tang 	if (z < 25)
81995f75e91SYunkang Tang 		input_report_key(dev, BTN_TOUCH, 0);
82095f75e91SYunkang Tang 
82195f75e91SYunkang Tang 	if (z > 0) {
82295f75e91SYunkang Tang 		input_report_abs(dev, ABS_X, x);
82395f75e91SYunkang Tang 		input_report_abs(dev, ABS_Y, y);
82495f75e91SYunkang Tang 	}
82595f75e91SYunkang Tang 
82695f75e91SYunkang Tang 	input_report_abs(dev, ABS_PRESSURE, z);
82795f75e91SYunkang Tang 	input_report_key(dev, BTN_TOOL_FINGER, z > 0);
82895f75e91SYunkang Tang 
82995f75e91SYunkang Tang 	/* v6 touchpad does not have middle button */
83095f75e91SYunkang Tang 	input_report_key(dev, BTN_LEFT, left);
83195f75e91SYunkang Tang 	input_report_key(dev, BTN_RIGHT, right);
83295f75e91SYunkang Tang 
83395f75e91SYunkang Tang 	input_sync(dev);
83495f75e91SYunkang Tang }
83595f75e91SYunkang Tang 
83625bded7cSSeth Forshee static void alps_process_packet_v4(struct psmouse *psmouse)
83725bded7cSSeth Forshee {
8383b7e09faSGeorge Pantalos 	struct alps_data *priv = psmouse->private;
83925bded7cSSeth Forshee 	unsigned char *packet = psmouse->packet;
84002d04254SHans de Goede 	struct alps_fields *f = &priv->f;
84168c21870SHans de Goede 	int offset;
8423b7e09faSGeorge Pantalos 
8433b7e09faSGeorge Pantalos 	/*
8443b7e09faSGeorge Pantalos 	 * v4 has a 6-byte encoding for bitmap data, but this data is
8453b7e09faSGeorge Pantalos 	 * broken up between 3 normal packets. Use priv->multi_packet to
8463b7e09faSGeorge Pantalos 	 * track our position in the bitmap packet.
8473b7e09faSGeorge Pantalos 	 */
8483b7e09faSGeorge Pantalos 	if (packet[6] & 0x40) {
8493b7e09faSGeorge Pantalos 		/* sync, reset position */
8503b7e09faSGeorge Pantalos 		priv->multi_packet = 0;
8513b7e09faSGeorge Pantalos 	}
8523b7e09faSGeorge Pantalos 
8533b7e09faSGeorge Pantalos 	if (WARN_ON_ONCE(priv->multi_packet > 2))
8543b7e09faSGeorge Pantalos 		return;
8553b7e09faSGeorge Pantalos 
8563b7e09faSGeorge Pantalos 	offset = 2 * priv->multi_packet;
8573b7e09faSGeorge Pantalos 	priv->multi_data[offset] = packet[6];
8583b7e09faSGeorge Pantalos 	priv->multi_data[offset + 1] = packet[7];
8593b7e09faSGeorge Pantalos 
8603b7e09faSGeorge Pantalos 	if (++priv->multi_packet > 2) {
8613b7e09faSGeorge Pantalos 		priv->multi_packet = 0;
8623b7e09faSGeorge Pantalos 
86302d04254SHans de Goede 		f->x_map = ((priv->multi_data[2] & 0x1f) << 10) |
8643b7e09faSGeorge Pantalos 			   ((priv->multi_data[3] & 0x60) << 3) |
8653b7e09faSGeorge Pantalos 			   ((priv->multi_data[0] & 0x3f) << 2) |
8663b7e09faSGeorge Pantalos 			   ((priv->multi_data[1] & 0x60) >> 5);
86702d04254SHans de Goede 		f->y_map = ((priv->multi_data[5] & 0x01) << 10) |
8683b7e09faSGeorge Pantalos 			   ((priv->multi_data[3] & 0x1f) << 5) |
8693b7e09faSGeorge Pantalos 			    (priv->multi_data[1] & 0x1f);
8703b7e09faSGeorge Pantalos 
87102d04254SHans de Goede 		f->fingers = alps_process_bitmap(priv, f);
8723b7e09faSGeorge Pantalos 	}
87325bded7cSSeth Forshee 
874b0cfb794SAndreas Bosch 	f->left = !!(packet[4] & 0x01);
875b0cfb794SAndreas Bosch 	f->right = !!(packet[4] & 0x02);
87625bded7cSSeth Forshee 
87702d04254SHans de Goede 	f->st.x = ((packet[1] & 0x7f) << 4) | ((packet[3] & 0x30) >> 2) |
87825bded7cSSeth Forshee 		  ((packet[0] & 0x30) >> 4);
87902d04254SHans de Goede 	f->st.y = ((packet[2] & 0x7f) << 4) | (packet[3] & 0x0f);
88002d04254SHans de Goede 	f->pressure = packet[5] & 0x7f;
88125bded7cSSeth Forshee 
88268c21870SHans de Goede 	alps_report_semi_mt_data(psmouse, f->fingers);
88325bded7cSSeth Forshee }
88425bded7cSSeth Forshee 
8853808843cSYunkang Tang static bool alps_is_valid_package_v7(struct psmouse *psmouse)
8863808843cSYunkang Tang {
8873808843cSYunkang Tang 	switch (psmouse->pktcnt) {
8883808843cSYunkang Tang 	case 3:
8893808843cSYunkang Tang 		return (psmouse->packet[2] & 0x40) == 0x40;
8903808843cSYunkang Tang 	case 4:
8913808843cSYunkang Tang 		return (psmouse->packet[3] & 0x48) == 0x48;
8923808843cSYunkang Tang 	case 6:
8933808843cSYunkang Tang 		return (psmouse->packet[5] & 0x40) == 0x00;
8943808843cSYunkang Tang 	}
8953808843cSYunkang Tang 	return true;
8963808843cSYunkang Tang }
8973808843cSYunkang Tang 
8983808843cSYunkang Tang static unsigned char alps_get_packet_id_v7(char *byte)
8993808843cSYunkang Tang {
9003808843cSYunkang Tang 	unsigned char packet_id;
9013808843cSYunkang Tang 
9023808843cSYunkang Tang 	if (byte[4] & 0x40)
9033808843cSYunkang Tang 		packet_id = V7_PACKET_ID_TWO;
9043808843cSYunkang Tang 	else if (byte[4] & 0x01)
9053808843cSYunkang Tang 		packet_id = V7_PACKET_ID_MULTI;
9063808843cSYunkang Tang 	else if ((byte[0] & 0x10) && !(byte[4] & 0x43))
9073808843cSYunkang Tang 		packet_id = V7_PACKET_ID_NEW;
9083808843cSYunkang Tang 	else if (byte[1] == 0x00 && byte[4] == 0x00)
9093808843cSYunkang Tang 		packet_id = V7_PACKET_ID_IDLE;
9103808843cSYunkang Tang 	else
9113808843cSYunkang Tang 		packet_id = V7_PACKET_ID_UNKNOWN;
9123808843cSYunkang Tang 
9133808843cSYunkang Tang 	return packet_id;
9143808843cSYunkang Tang }
9153808843cSYunkang Tang 
9163808843cSYunkang Tang static void alps_get_finger_coordinate_v7(struct input_mt_pos *mt,
9173808843cSYunkang Tang 					  unsigned char *pkt,
9183808843cSYunkang Tang 					  unsigned char pkt_id)
9193808843cSYunkang Tang {
9203808843cSYunkang Tang 	mt[0].x = ((pkt[2] & 0x80) << 4);
9213808843cSYunkang Tang 	mt[0].x |= ((pkt[2] & 0x3F) << 5);
9223808843cSYunkang Tang 	mt[0].x |= ((pkt[3] & 0x30) >> 1);
9233808843cSYunkang Tang 	mt[0].x |= (pkt[3] & 0x07);
9243808843cSYunkang Tang 	mt[0].y = (pkt[1] << 3) | (pkt[0] & 0x07);
9253808843cSYunkang Tang 
9263808843cSYunkang Tang 	mt[1].x = ((pkt[3] & 0x80) << 4);
9273808843cSYunkang Tang 	mt[1].x |= ((pkt[4] & 0x80) << 3);
9283808843cSYunkang Tang 	mt[1].x |= ((pkt[4] & 0x3F) << 4);
9293808843cSYunkang Tang 	mt[1].y = ((pkt[5] & 0x80) << 3);
9303808843cSYunkang Tang 	mt[1].y |= ((pkt[5] & 0x3F) << 4);
9313808843cSYunkang Tang 
9323808843cSYunkang Tang 	switch (pkt_id) {
9333808843cSYunkang Tang 	case V7_PACKET_ID_TWO:
9343808843cSYunkang Tang 		mt[1].x &= ~0x000F;
9353808843cSYunkang Tang 		mt[1].y |= 0x000F;
9363808843cSYunkang Tang 		break;
9373808843cSYunkang Tang 
9383808843cSYunkang Tang 	case V7_PACKET_ID_MULTI:
9393808843cSYunkang Tang 		mt[1].x &= ~0x003F;
9403808843cSYunkang Tang 		mt[1].y &= ~0x0020;
9413808843cSYunkang Tang 		mt[1].y |= ((pkt[4] & 0x02) << 4);
9423808843cSYunkang Tang 		mt[1].y |= 0x001F;
9433808843cSYunkang Tang 		break;
9443808843cSYunkang Tang 
9453808843cSYunkang Tang 	case V7_PACKET_ID_NEW:
9463808843cSYunkang Tang 		mt[1].x &= ~0x003F;
9473808843cSYunkang Tang 		mt[1].x |= (pkt[0] & 0x20);
9483808843cSYunkang Tang 		mt[1].y |= 0x000F;
9493808843cSYunkang Tang 		break;
9503808843cSYunkang Tang 	}
9513808843cSYunkang Tang 
9523808843cSYunkang Tang 	mt[0].y = 0x7FF - mt[0].y;
9533808843cSYunkang Tang 	mt[1].y = 0x7FF - mt[1].y;
9543808843cSYunkang Tang }
9553808843cSYunkang Tang 
9563808843cSYunkang Tang static int alps_get_mt_count(struct input_mt_pos *mt)
9573808843cSYunkang Tang {
9587091c443SHans de Goede 	int i, fingers = 0;
9593808843cSYunkang Tang 
9607091c443SHans de Goede 	for (i = 0; i < MAX_TOUCHES; i++) {
9617091c443SHans de Goede 		if (mt[i].x != 0 || mt[i].y != 0)
9627091c443SHans de Goede 			fingers++;
9637091c443SHans de Goede 	}
9643808843cSYunkang Tang 
9657091c443SHans de Goede 	return fingers;
9663808843cSYunkang Tang }
9673808843cSYunkang Tang 
9683808843cSYunkang Tang static int alps_decode_packet_v7(struct alps_fields *f,
9693808843cSYunkang Tang 				  unsigned char *p,
9703808843cSYunkang Tang 				  struct psmouse *psmouse)
9713808843cSYunkang Tang {
972d27eb793SHans de Goede 	struct alps_data *priv = psmouse->private;
9733808843cSYunkang Tang 	unsigned char pkt_id;
9743808843cSYunkang Tang 
9753808843cSYunkang Tang 	pkt_id = alps_get_packet_id_v7(p);
9763808843cSYunkang Tang 	if (pkt_id == V7_PACKET_ID_IDLE)
9773808843cSYunkang Tang 		return 0;
9783808843cSYunkang Tang 	if (pkt_id == V7_PACKET_ID_UNKNOWN)
9793808843cSYunkang Tang 		return -1;
9808b238115SHans de Goede 	/*
9818b238115SHans de Goede 	 * NEW packets are send to indicate a discontinuity in the finger
9828b238115SHans de Goede 	 * coordinate reporting. Specifically a finger may have moved from
9838b238115SHans de Goede 	 * slot 0 to 1 or vice versa. INPUT_MT_TRACK takes care of this for
9848b238115SHans de Goede 	 * us.
9858b238115SHans de Goede 	 *
9868b238115SHans de Goede 	 * NEW packets have 3 problems:
9878b238115SHans de Goede 	 * 1) They do not contain middle / right button info (on non clickpads)
9888b238115SHans de Goede 	 *    this can be worked around by preserving the old button state
9898b238115SHans de Goede 	 * 2) They do not contain an accurate fingercount, and they are
9908b238115SHans de Goede 	 *    typically send when the number of fingers changes. We cannot use
9918b238115SHans de Goede 	 *    the old finger count as that may mismatch with the amount of
9928b238115SHans de Goede 	 *    touch coordinates we've available in the NEW packet
9938b238115SHans de Goede 	 * 3) Their x data for the second touch is inaccurate leading to
9948b238115SHans de Goede 	 *    a possible jump of the x coordinate by 16 units when the first
9958b238115SHans de Goede 	 *    non NEW packet comes in
9968b238115SHans de Goede 	 * Since problems 2 & 3 cannot be worked around, just ignore them.
9978b238115SHans de Goede 	 */
9988b238115SHans de Goede 	if (pkt_id == V7_PACKET_ID_NEW)
9998b238115SHans de Goede 		return 1;
10003808843cSYunkang Tang 
10013808843cSYunkang Tang 	alps_get_finger_coordinate_v7(f->mt, p, pkt_id);
10023808843cSYunkang Tang 
10033808843cSYunkang Tang 	if (pkt_id == V7_PACKET_ID_TWO)
10043808843cSYunkang Tang 		f->fingers = alps_get_mt_count(f->mt);
10058b238115SHans de Goede 	else /* pkt_id == V7_PACKET_ID_MULTI */
10063808843cSYunkang Tang 		f->fingers = 3 + (p[5] & 0x03);
10073808843cSYunkang Tang 
1008d27eb793SHans de Goede 	f->left = (p[0] & 0x80) >> 7;
1009d27eb793SHans de Goede 	if (priv->flags & ALPS_BUTTONPAD) {
1010d27eb793SHans de Goede 		if (p[0] & 0x20)
1011d27eb793SHans de Goede 			f->fingers++;
1012d27eb793SHans de Goede 		if (p[0] & 0x10)
1013d27eb793SHans de Goede 			f->fingers++;
1014d27eb793SHans de Goede 	} else {
1015d27eb793SHans de Goede 		f->right = (p[0] & 0x20) >> 5;
1016d27eb793SHans de Goede 		f->middle = (p[0] & 0x10) >> 4;
1017d27eb793SHans de Goede 	}
1018d27eb793SHans de Goede 
10197091c443SHans de Goede 	/* Sometimes a single touch is reported in mt[1] rather then mt[0] */
10207091c443SHans de Goede 	if (f->fingers == 1 && f->mt[0].x == 0 && f->mt[0].y == 0) {
10217091c443SHans de Goede 		f->mt[0].x = f->mt[1].x;
10227091c443SHans de Goede 		f->mt[0].y = f->mt[1].y;
10237091c443SHans de Goede 		f->mt[1].x = 0;
10247091c443SHans de Goede 		f->mt[1].y = 0;
10257091c443SHans de Goede 	}
10267091c443SHans de Goede 
10273808843cSYunkang Tang 	return 0;
10283808843cSYunkang Tang }
10293808843cSYunkang Tang 
10303808843cSYunkang Tang static void alps_process_trackstick_packet_v7(struct psmouse *psmouse)
10313808843cSYunkang Tang {
10323808843cSYunkang Tang 	struct alps_data *priv = psmouse->private;
10333808843cSYunkang Tang 	unsigned char *packet = psmouse->packet;
10343808843cSYunkang Tang 	struct input_dev *dev2 = priv->dev2;
10353808843cSYunkang Tang 	int x, y, z, left, right, middle;
10363808843cSYunkang Tang 
103734412ba2SPali Rohár 	/* It should be a DualPoint when received trackstick packet */
103834412ba2SPali Rohár 	if (!(priv->flags & ALPS_DUALPOINT)) {
103934412ba2SPali Rohár 		psmouse_warn(psmouse,
104034412ba2SPali Rohár 			     "Rejected trackstick packet from non DualPoint device");
104134412ba2SPali Rohár 		return;
104234412ba2SPali Rohár 	}
104334412ba2SPali Rohár 
10443808843cSYunkang Tang 	x = ((packet[2] & 0xbf)) | ((packet[3] & 0x10) << 2);
10453808843cSYunkang Tang 	y = (packet[3] & 0x07) | (packet[4] & 0xb8) |
10463808843cSYunkang Tang 	    ((packet[3] & 0x20) << 1);
10473808843cSYunkang Tang 	z = (packet[5] & 0x3f) | ((packet[3] & 0x80) >> 1);
10483808843cSYunkang Tang 
10493808843cSYunkang Tang 	left = (packet[1] & 0x01);
10503808843cSYunkang Tang 	right = (packet[1] & 0x02) >> 1;
10513808843cSYunkang Tang 	middle = (packet[1] & 0x04) >> 2;
10523808843cSYunkang Tang 
10533808843cSYunkang Tang 	/* Divide 2 since trackpoint's speed is too fast */
10543808843cSYunkang Tang 	input_report_rel(dev2, REL_X, (char)x / 2);
10553808843cSYunkang Tang 	input_report_rel(dev2, REL_Y, -((char)y / 2));
10563808843cSYunkang Tang 
10573808843cSYunkang Tang 	input_report_key(dev2, BTN_LEFT, left);
10583808843cSYunkang Tang 	input_report_key(dev2, BTN_RIGHT, right);
10593808843cSYunkang Tang 	input_report_key(dev2, BTN_MIDDLE, middle);
10603808843cSYunkang Tang 
10613808843cSYunkang Tang 	input_sync(dev2);
10623808843cSYunkang Tang }
10633808843cSYunkang Tang 
10643808843cSYunkang Tang static void alps_process_touchpad_packet_v7(struct psmouse *psmouse)
10653808843cSYunkang Tang {
10663808843cSYunkang Tang 	struct alps_data *priv = psmouse->private;
10673808843cSYunkang Tang 	struct input_dev *dev = psmouse->dev;
10683808843cSYunkang Tang 	struct alps_fields *f = &priv->f;
10693808843cSYunkang Tang 
10703808843cSYunkang Tang 	memset(f, 0, sizeof(*f));
10713808843cSYunkang Tang 
10723808843cSYunkang Tang 	if (priv->decode_fields(f, psmouse->packet, psmouse))
10733808843cSYunkang Tang 		return;
10743808843cSYunkang Tang 
10753808843cSYunkang Tang 	alps_report_mt_data(psmouse, alps_get_mt_count(f->mt));
10763808843cSYunkang Tang 
10773808843cSYunkang Tang 	input_mt_report_finger_count(dev, f->fingers);
10783808843cSYunkang Tang 
10793808843cSYunkang Tang 	input_report_key(dev, BTN_LEFT, f->left);
10803808843cSYunkang Tang 	input_report_key(dev, BTN_RIGHT, f->right);
10813808843cSYunkang Tang 	input_report_key(dev, BTN_MIDDLE, f->middle);
10823808843cSYunkang Tang 
10833808843cSYunkang Tang 	input_sync(dev);
10843808843cSYunkang Tang }
10853808843cSYunkang Tang 
10863808843cSYunkang Tang static void alps_process_packet_v7(struct psmouse *psmouse)
10873808843cSYunkang Tang {
10883808843cSYunkang Tang 	unsigned char *packet = psmouse->packet;
10893808843cSYunkang Tang 
10903808843cSYunkang Tang 	if (packet[0] == 0x48 && (packet[4] & 0x47) == 0x06)
10913808843cSYunkang Tang 		alps_process_trackstick_packet_v7(psmouse);
10923808843cSYunkang Tang 	else
10933808843cSYunkang Tang 		alps_process_touchpad_packet_v7(psmouse);
10943808843cSYunkang Tang }
10953808843cSYunkang Tang 
109607f19e3dSFengguang Wu static unsigned char alps_get_pkt_id_ss4_v2(unsigned char *byte)
10973db5b9f7SMasaki Ota {
10983db5b9f7SMasaki Ota 	unsigned char pkt_id = SS4_PACKET_ID_IDLE;
10993db5b9f7SMasaki Ota 
11003db5b9f7SMasaki Ota 	if (byte[0] == 0x18 && byte[1] == 0x10 && byte[2] == 0x00 &&
11013db5b9f7SMasaki Ota 	    (byte[3] & 0x88) == 0x08 && byte[4] == 0x10 && byte[5] == 0x00) {
11023db5b9f7SMasaki Ota 		pkt_id = SS4_PACKET_ID_IDLE;
11033db5b9f7SMasaki Ota 	} else if (!(byte[3] & 0x10)) {
11043db5b9f7SMasaki Ota 		pkt_id = SS4_PACKET_ID_ONE;
11053db5b9f7SMasaki Ota 	} else if (!(byte[3] & 0x20)) {
11063db5b9f7SMasaki Ota 		pkt_id = SS4_PACKET_ID_TWO;
11073db5b9f7SMasaki Ota 	} else {
11083db5b9f7SMasaki Ota 		pkt_id = SS4_PACKET_ID_MULTI;
11093db5b9f7SMasaki Ota 	}
11103db5b9f7SMasaki Ota 
11113db5b9f7SMasaki Ota 	return pkt_id;
11123db5b9f7SMasaki Ota }
11133db5b9f7SMasaki Ota 
11143db5b9f7SMasaki Ota static int alps_decode_ss4_v2(struct alps_fields *f,
11153db5b9f7SMasaki Ota 			      unsigned char *p, struct psmouse *psmouse)
11163db5b9f7SMasaki Ota {
11173db5b9f7SMasaki Ota 	struct alps_data *priv = psmouse->private;
11183db5b9f7SMasaki Ota 	unsigned char pkt_id;
11193db5b9f7SMasaki Ota 	unsigned int no_data_x, no_data_y;
11203db5b9f7SMasaki Ota 
11213db5b9f7SMasaki Ota 	pkt_id = alps_get_pkt_id_ss4_v2(p);
11223db5b9f7SMasaki Ota 
11233db5b9f7SMasaki Ota 	/* Current packet is 1Finger coordinate packet */
11243db5b9f7SMasaki Ota 	switch (pkt_id) {
11253db5b9f7SMasaki Ota 	case SS4_PACKET_ID_ONE:
11263db5b9f7SMasaki Ota 		f->mt[0].x = SS4_1F_X_V2(p);
11273db5b9f7SMasaki Ota 		f->mt[0].y = SS4_1F_Y_V2(p);
11283db5b9f7SMasaki Ota 		f->pressure = ((SS4_1F_Z_V2(p)) * 2) & 0x7f;
11293db5b9f7SMasaki Ota 		f->fingers = 1;
11303db5b9f7SMasaki Ota 		f->first_mp = 0;
11313db5b9f7SMasaki Ota 		f->is_mp = 0;
11323db5b9f7SMasaki Ota 		break;
11333db5b9f7SMasaki Ota 
11343db5b9f7SMasaki Ota 	case SS4_PACKET_ID_TWO:
11353db5b9f7SMasaki Ota 		if (priv->flags & ALPS_BUTTONPAD) {
11363db5b9f7SMasaki Ota 			f->mt[0].x = SS4_BTL_MF_X_V2(p, 0);
11373db5b9f7SMasaki Ota 			f->mt[0].y = SS4_BTL_MF_Y_V2(p, 0);
11383db5b9f7SMasaki Ota 			f->mt[1].x = SS4_BTL_MF_X_V2(p, 1);
11393db5b9f7SMasaki Ota 			f->mt[1].y = SS4_BTL_MF_Y_V2(p, 1);
11403db5b9f7SMasaki Ota 		} else {
11413db5b9f7SMasaki Ota 			f->mt[0].x = SS4_STD_MF_X_V2(p, 0);
11423db5b9f7SMasaki Ota 			f->mt[0].y = SS4_STD_MF_Y_V2(p, 0);
11433db5b9f7SMasaki Ota 			f->mt[1].x = SS4_STD_MF_X_V2(p, 1);
11443db5b9f7SMasaki Ota 			f->mt[1].y = SS4_STD_MF_Y_V2(p, 1);
11453db5b9f7SMasaki Ota 		}
11463db5b9f7SMasaki Ota 		f->pressure = SS4_MF_Z_V2(p, 0) ? 0x30 : 0;
11473db5b9f7SMasaki Ota 
11483db5b9f7SMasaki Ota 		if (SS4_IS_MF_CONTINUE(p)) {
11493db5b9f7SMasaki Ota 			f->first_mp = 1;
11503db5b9f7SMasaki Ota 		} else {
11513db5b9f7SMasaki Ota 			f->fingers = 2;
11523db5b9f7SMasaki Ota 			f->first_mp = 0;
11533db5b9f7SMasaki Ota 		}
11543db5b9f7SMasaki Ota 		f->is_mp = 0;
11553db5b9f7SMasaki Ota 
11563db5b9f7SMasaki Ota 		break;
11573db5b9f7SMasaki Ota 
11583db5b9f7SMasaki Ota 	case SS4_PACKET_ID_MULTI:
11593db5b9f7SMasaki Ota 		if (priv->flags & ALPS_BUTTONPAD) {
11603db5b9f7SMasaki Ota 			f->mt[2].x = SS4_BTL_MF_X_V2(p, 0);
11613db5b9f7SMasaki Ota 			f->mt[2].y = SS4_BTL_MF_Y_V2(p, 0);
11623db5b9f7SMasaki Ota 			f->mt[3].x = SS4_BTL_MF_X_V2(p, 1);
11633db5b9f7SMasaki Ota 			f->mt[3].y = SS4_BTL_MF_Y_V2(p, 1);
11643db5b9f7SMasaki Ota 			no_data_x = SS4_MFPACKET_NO_AX_BL;
11653db5b9f7SMasaki Ota 			no_data_y = SS4_MFPACKET_NO_AY_BL;
11663db5b9f7SMasaki Ota 		} else {
11673db5b9f7SMasaki Ota 			f->mt[2].x = SS4_STD_MF_X_V2(p, 0);
11683db5b9f7SMasaki Ota 			f->mt[2].y = SS4_STD_MF_Y_V2(p, 0);
11693db5b9f7SMasaki Ota 			f->mt[3].x = SS4_STD_MF_X_V2(p, 1);
11703db5b9f7SMasaki Ota 			f->mt[3].y = SS4_STD_MF_Y_V2(p, 1);
11713db5b9f7SMasaki Ota 			no_data_x = SS4_MFPACKET_NO_AX;
11723db5b9f7SMasaki Ota 			no_data_y = SS4_MFPACKET_NO_AY;
11733db5b9f7SMasaki Ota 		}
11743db5b9f7SMasaki Ota 
11753db5b9f7SMasaki Ota 		f->first_mp = 0;
11763db5b9f7SMasaki Ota 		f->is_mp = 1;
11773db5b9f7SMasaki Ota 
11783db5b9f7SMasaki Ota 		if (SS4_IS_5F_DETECTED(p)) {
11793db5b9f7SMasaki Ota 			f->fingers = 5;
11803db5b9f7SMasaki Ota 		} else if (f->mt[3].x == no_data_x &&
11813db5b9f7SMasaki Ota 			     f->mt[3].y == no_data_y) {
11823db5b9f7SMasaki Ota 			f->mt[3].x = 0;
11833db5b9f7SMasaki Ota 			f->mt[3].y = 0;
11843db5b9f7SMasaki Ota 			f->fingers = 3;
11853db5b9f7SMasaki Ota 		} else {
11863db5b9f7SMasaki Ota 			f->fingers = 4;
11873db5b9f7SMasaki Ota 		}
11883db5b9f7SMasaki Ota 		break;
11893db5b9f7SMasaki Ota 
11903db5b9f7SMasaki Ota 	case SS4_PACKET_ID_IDLE:
11913db5b9f7SMasaki Ota 	default:
11923db5b9f7SMasaki Ota 		memset(f, 0, sizeof(struct alps_fields));
11933db5b9f7SMasaki Ota 		break;
11943db5b9f7SMasaki Ota 	}
11953db5b9f7SMasaki Ota 
11963db5b9f7SMasaki Ota 	f->left = !!(SS4_BTN_V2(p) & 0x01);
11973db5b9f7SMasaki Ota 	if (!(priv->flags & ALPS_BUTTONPAD)) {
11983db5b9f7SMasaki Ota 		f->right = !!(SS4_BTN_V2(p) & 0x02);
11993db5b9f7SMasaki Ota 		f->middle = !!(SS4_BTN_V2(p) & 0x04);
12003db5b9f7SMasaki Ota 	}
12013db5b9f7SMasaki Ota 
12023db5b9f7SMasaki Ota 	return 0;
12033db5b9f7SMasaki Ota }
12043db5b9f7SMasaki Ota 
12053db5b9f7SMasaki Ota static void alps_process_packet_ss4_v2(struct psmouse *psmouse)
12063db5b9f7SMasaki Ota {
12073db5b9f7SMasaki Ota 	struct alps_data *priv = psmouse->private;
12083db5b9f7SMasaki Ota 	unsigned char *packet = psmouse->packet;
12093db5b9f7SMasaki Ota 	struct input_dev *dev = psmouse->dev;
12103db5b9f7SMasaki Ota 	struct alps_fields *f = &priv->f;
12113db5b9f7SMasaki Ota 
12123db5b9f7SMasaki Ota 	memset(f, 0, sizeof(struct alps_fields));
12133db5b9f7SMasaki Ota 	priv->decode_fields(f, packet, psmouse);
12143db5b9f7SMasaki Ota 	if (priv->multi_packet) {
12153db5b9f7SMasaki Ota 		/*
12163db5b9f7SMasaki Ota 		 * Sometimes the first packet will indicate a multi-packet
12173db5b9f7SMasaki Ota 		 * sequence, but sometimes the next multi-packet would not
12183db5b9f7SMasaki Ota 		 * come. Check for this, and when it happens process the
12193db5b9f7SMasaki Ota 		 * position packet as usual.
12203db5b9f7SMasaki Ota 		 */
12213db5b9f7SMasaki Ota 		if (f->is_mp) {
12223db5b9f7SMasaki Ota 			/* Now process the 1st packet */
12233db5b9f7SMasaki Ota 			priv->decode_fields(f, priv->multi_data, psmouse);
12243db5b9f7SMasaki Ota 		} else {
12253db5b9f7SMasaki Ota 			priv->multi_packet = 0;
12263db5b9f7SMasaki Ota 		}
12273db5b9f7SMasaki Ota 	}
12283db5b9f7SMasaki Ota 
12293db5b9f7SMasaki Ota 	/*
12303db5b9f7SMasaki Ota 	 * "f.is_mp" would always be '0' after merging the 1st and 2nd packet.
12313db5b9f7SMasaki Ota 	 * When it is set, it means 2nd packet comes without 1st packet come.
12323db5b9f7SMasaki Ota 	 */
12333db5b9f7SMasaki Ota 	if (f->is_mp)
12343db5b9f7SMasaki Ota 		return;
12353db5b9f7SMasaki Ota 
12363db5b9f7SMasaki Ota 	/* Save the first packet */
12373db5b9f7SMasaki Ota 	if (!priv->multi_packet && f->first_mp) {
12383db5b9f7SMasaki Ota 		priv->multi_packet = 1;
12393db5b9f7SMasaki Ota 		memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
12403db5b9f7SMasaki Ota 		return;
12413db5b9f7SMasaki Ota 	}
12423db5b9f7SMasaki Ota 
12433db5b9f7SMasaki Ota 	priv->multi_packet = 0;
12443db5b9f7SMasaki Ota 
12453db5b9f7SMasaki Ota 	alps_report_mt_data(psmouse, (f->fingers <= 4) ? f->fingers : 4);
12463db5b9f7SMasaki Ota 
12473db5b9f7SMasaki Ota 	input_mt_report_finger_count(dev, f->fingers);
12483db5b9f7SMasaki Ota 
12493db5b9f7SMasaki Ota 	input_report_key(dev, BTN_LEFT, f->left);
12503db5b9f7SMasaki Ota 	input_report_key(dev, BTN_RIGHT, f->right);
12513db5b9f7SMasaki Ota 	input_report_key(dev, BTN_MIDDLE, f->middle);
12523db5b9f7SMasaki Ota 
12533db5b9f7SMasaki Ota 	input_report_abs(dev, ABS_PRESSURE, f->pressure);
12543db5b9f7SMasaki Ota 	input_sync(dev);
12553db5b9f7SMasaki Ota }
12563db5b9f7SMasaki Ota 
12573db5b9f7SMasaki Ota static bool alps_is_valid_package_ss4_v2(struct psmouse *psmouse)
12583db5b9f7SMasaki Ota {
12593db5b9f7SMasaki Ota 	if (psmouse->pktcnt == 4 && ((psmouse->packet[3] & 0x08) != 0x08))
12603db5b9f7SMasaki Ota 		return false;
12613db5b9f7SMasaki Ota 	if (psmouse->pktcnt == 6 && ((psmouse->packet[5] & 0x10) != 0x0))
12623db5b9f7SMasaki Ota 		return false;
12633db5b9f7SMasaki Ota 	return true;
12643db5b9f7SMasaki Ota }
12653db5b9f7SMasaki Ota 
126604aae283SPali Rohár static DEFINE_MUTEX(alps_mutex);
126704aae283SPali Rohár 
126804aae283SPali Rohár static void alps_register_bare_ps2_mouse(struct work_struct *work)
126904aae283SPali Rohár {
127004aae283SPali Rohár 	struct alps_data *priv =
127104aae283SPali Rohár 		container_of(work, struct alps_data, dev3_register_work.work);
127204aae283SPali Rohár 	struct psmouse *psmouse = priv->psmouse;
127304aae283SPali Rohár 	struct input_dev *dev3;
127404aae283SPali Rohár 	int error = 0;
127504aae283SPali Rohár 
127604aae283SPali Rohár 	mutex_lock(&alps_mutex);
127704aae283SPali Rohár 
127804aae283SPali Rohár 	if (priv->dev3)
127904aae283SPali Rohár 		goto out;
128004aae283SPali Rohár 
128104aae283SPali Rohár 	dev3 = input_allocate_device();
128204aae283SPali Rohár 	if (!dev3) {
128304aae283SPali Rohár 		psmouse_err(psmouse, "failed to allocate secondary device\n");
128404aae283SPali Rohár 		error = -ENOMEM;
128504aae283SPali Rohár 		goto out;
128604aae283SPali Rohár 	}
128704aae283SPali Rohár 
128804aae283SPali Rohár 	snprintf(priv->phys3, sizeof(priv->phys3), "%s/%s",
128904aae283SPali Rohár 		 psmouse->ps2dev.serio->phys,
129004aae283SPali Rohár 		 (priv->dev2 ? "input2" : "input1"));
129104aae283SPali Rohár 	dev3->phys = priv->phys3;
129204aae283SPali Rohár 
129304aae283SPali Rohár 	/*
129404aae283SPali Rohár 	 * format of input device name is: "protocol vendor name"
129504aae283SPali Rohár 	 * see function psmouse_switch_protocol() in psmouse-base.c
129604aae283SPali Rohár 	 */
129704aae283SPali Rohár 	dev3->name = "PS/2 ALPS Mouse";
129804aae283SPali Rohár 
129904aae283SPali Rohár 	dev3->id.bustype = BUS_I8042;
130004aae283SPali Rohár 	dev3->id.vendor  = 0x0002;
130104aae283SPali Rohár 	dev3->id.product = PSMOUSE_PS2;
130204aae283SPali Rohár 	dev3->id.version = 0x0000;
130304aae283SPali Rohár 	dev3->dev.parent = &psmouse->ps2dev.serio->dev;
130404aae283SPali Rohár 
130504aae283SPali Rohár 	input_set_capability(dev3, EV_REL, REL_X);
130604aae283SPali Rohár 	input_set_capability(dev3, EV_REL, REL_Y);
130704aae283SPali Rohár 	input_set_capability(dev3, EV_KEY, BTN_LEFT);
130804aae283SPali Rohár 	input_set_capability(dev3, EV_KEY, BTN_RIGHT);
130904aae283SPali Rohár 	input_set_capability(dev3, EV_KEY, BTN_MIDDLE);
131004aae283SPali Rohár 
131104aae283SPali Rohár 	__set_bit(INPUT_PROP_POINTER, dev3->propbit);
131204aae283SPali Rohár 
131304aae283SPali Rohár 	error = input_register_device(dev3);
131404aae283SPali Rohár 	if (error) {
131504aae283SPali Rohár 		psmouse_err(psmouse,
131604aae283SPali Rohár 			    "failed to register secondary device: %d\n",
131704aae283SPali Rohár 			    error);
131804aae283SPali Rohár 		input_free_device(dev3);
131904aae283SPali Rohár 		goto out;
132004aae283SPali Rohár 	}
132104aae283SPali Rohár 
132204aae283SPali Rohár 	priv->dev3 = dev3;
132304aae283SPali Rohár 
132404aae283SPali Rohár out:
132504aae283SPali Rohár 	/*
132604aae283SPali Rohár 	 * Save the error code so that we can detect that we
132704aae283SPali Rohár 	 * already tried to create the device.
132804aae283SPali Rohár 	 */
132904aae283SPali Rohár 	if (error)
133004aae283SPali Rohár 		priv->dev3 = ERR_PTR(error);
133104aae283SPali Rohár 
133204aae283SPali Rohár 	mutex_unlock(&alps_mutex);
133304aae283SPali Rohár }
133404aae283SPali Rohár 
133559c30afbSHans de Goede static void alps_report_bare_ps2_packet(struct psmouse *psmouse,
13361d9f2626SSebastian Kapfer 					unsigned char packet[],
13371d9f2626SSebastian Kapfer 					bool report_buttons)
13381d9f2626SSebastian Kapfer {
133959c30afbSHans de Goede 	struct alps_data *priv = psmouse->private;
134059c30afbSHans de Goede 	struct input_dev *dev;
134159c30afbSHans de Goede 
1342e3a79212SHans de Goede 	/* Figure out which device to use to report the bare packet */
1343e3a79212SHans de Goede 	if (priv->proto_version == ALPS_PROTO_V2 &&
1344e3a79212SHans de Goede 	    (priv->flags & ALPS_DUALPOINT)) {
1345e3a79212SHans de Goede 		/* On V2 devices the DualPoint Stick reports bare packets */
1346e3a79212SHans de Goede 		dev = priv->dev2;
1347e3a79212SHans de Goede 	} else if (unlikely(IS_ERR_OR_NULL(priv->dev3))) {
134859c30afbSHans de Goede 		/* Register dev3 mouse if we received PS/2 packet first time */
134959c30afbSHans de Goede 		if (!IS_ERR(priv->dev3))
135059c30afbSHans de Goede 			psmouse_queue_work(psmouse, &priv->dev3_register_work,
135159c30afbSHans de Goede 					   0);
135259c30afbSHans de Goede 		return;
135359c30afbSHans de Goede 	} else {
135459c30afbSHans de Goede 		dev = priv->dev3;
135559c30afbSHans de Goede 	}
135659c30afbSHans de Goede 
13571d9f2626SSebastian Kapfer 	if (report_buttons)
135804aae283SPali Rohár 		alps_report_buttons(dev, NULL,
13591d9f2626SSebastian Kapfer 				packet[0] & 1, packet[0] & 2, packet[0] & 4);
13601d9f2626SSebastian Kapfer 
136104aae283SPali Rohár 	input_report_rel(dev, REL_X,
13621d9f2626SSebastian Kapfer 		packet[1] ? packet[1] - ((packet[0] << 4) & 0x100) : 0);
136304aae283SPali Rohár 	input_report_rel(dev, REL_Y,
13641d9f2626SSebastian Kapfer 		packet[2] ? ((packet[0] << 3) & 0x100) - packet[2] : 0);
13651d9f2626SSebastian Kapfer 
136604aae283SPali Rohár 	input_sync(dev);
13671d9f2626SSebastian Kapfer }
13681d9f2626SSebastian Kapfer 
13691d9f2626SSebastian Kapfer static psmouse_ret_t alps_handle_interleaved_ps2(struct psmouse *psmouse)
13701da177e4SLinus Torvalds {
13711da177e4SLinus Torvalds 	struct alps_data *priv = psmouse->private;
13721da177e4SLinus Torvalds 
13731d9f2626SSebastian Kapfer 	if (psmouse->pktcnt < 6)
13741d9f2626SSebastian Kapfer 		return PSMOUSE_GOOD_DATA;
13751d9f2626SSebastian Kapfer 
13761d9f2626SSebastian Kapfer 	if (psmouse->pktcnt == 6) {
13771d9f2626SSebastian Kapfer 		/*
13781d9f2626SSebastian Kapfer 		 * Start a timer to flush the packet if it ends up last
13791d9f2626SSebastian Kapfer 		 * 6-byte packet in the stream. Timer needs to fire
13801d9f2626SSebastian Kapfer 		 * psmouse core times out itself. 20 ms should be enough
13811d9f2626SSebastian Kapfer 		 * to decide if we are getting more data or not.
13821d9f2626SSebastian Kapfer 		 */
13831d9f2626SSebastian Kapfer 		mod_timer(&priv->timer, jiffies + msecs_to_jiffies(20));
13841d9f2626SSebastian Kapfer 		return PSMOUSE_GOOD_DATA;
13851d9f2626SSebastian Kapfer 	}
13861d9f2626SSebastian Kapfer 
13871d9f2626SSebastian Kapfer 	del_timer(&priv->timer);
13881d9f2626SSebastian Kapfer 
13891d9f2626SSebastian Kapfer 	if (psmouse->packet[6] & 0x80) {
13901d9f2626SSebastian Kapfer 
13911d9f2626SSebastian Kapfer 		/*
13921d9f2626SSebastian Kapfer 		 * Highest bit is set - that means we either had
13931d9f2626SSebastian Kapfer 		 * complete ALPS packet and this is start of the
13941d9f2626SSebastian Kapfer 		 * next packet or we got garbage.
13951d9f2626SSebastian Kapfer 		 */
13961d9f2626SSebastian Kapfer 
13971d9f2626SSebastian Kapfer 		if (((psmouse->packet[3] |
13981d9f2626SSebastian Kapfer 		      psmouse->packet[4] |
13991d9f2626SSebastian Kapfer 		      psmouse->packet[5]) & 0x80) ||
140099df65e7SKevin Cernekee 		    (!alps_is_valid_first_byte(priv, psmouse->packet[6]))) {
1401b5d21704SDmitry Torokhov 			psmouse_dbg(psmouse,
14023b112923SAndy Shevchenko 				    "refusing packet %4ph (suspected interleaved ps/2)\n",
14033b112923SAndy Shevchenko 				    psmouse->packet + 3);
14041d9f2626SSebastian Kapfer 			return PSMOUSE_BAD_DATA;
14051d9f2626SSebastian Kapfer 		}
14061d9f2626SSebastian Kapfer 
140724af5cb9SKevin Cernekee 		priv->process_packet(psmouse);
14081d9f2626SSebastian Kapfer 
14091d9f2626SSebastian Kapfer 		/* Continue with the next packet */
14101d9f2626SSebastian Kapfer 		psmouse->packet[0] = psmouse->packet[6];
14111d9f2626SSebastian Kapfer 		psmouse->pktcnt = 1;
14121d9f2626SSebastian Kapfer 
14131d9f2626SSebastian Kapfer 	} else {
14141d9f2626SSebastian Kapfer 
14151d9f2626SSebastian Kapfer 		/*
14161d9f2626SSebastian Kapfer 		 * High bit is 0 - that means that we indeed got a PS/2
14171d9f2626SSebastian Kapfer 		 * packet in the middle of ALPS packet.
14181d9f2626SSebastian Kapfer 		 *
14191d9f2626SSebastian Kapfer 		 * There is also possibility that we got 6-byte ALPS
14201d9f2626SSebastian Kapfer 		 * packet followed  by 3-byte packet from trackpoint. We
14211d9f2626SSebastian Kapfer 		 * can not distinguish between these 2 scenarios but
1422b5d21704SDmitry Torokhov 		 * because the latter is unlikely to happen in course of
14231d9f2626SSebastian Kapfer 		 * normal operation (user would need to press all
14241d9f2626SSebastian Kapfer 		 * buttons on the pad and start moving trackpoint
14251d9f2626SSebastian Kapfer 		 * without touching the pad surface) we assume former.
14261d9f2626SSebastian Kapfer 		 * Even if we are wrong the wost thing that would happen
14271d9f2626SSebastian Kapfer 		 * the cursor would jump but we should not get protocol
1428b5d21704SDmitry Torokhov 		 * de-synchronization.
14291d9f2626SSebastian Kapfer 		 */
14301d9f2626SSebastian Kapfer 
143159c30afbSHans de Goede 		alps_report_bare_ps2_packet(psmouse, &psmouse->packet[3],
143259c30afbSHans de Goede 					    false);
14331d9f2626SSebastian Kapfer 
14341d9f2626SSebastian Kapfer 		/*
14351d9f2626SSebastian Kapfer 		 * Continue with the standard ALPS protocol handling,
14361d9f2626SSebastian Kapfer 		 * but make sure we won't process it as an interleaved
14371d9f2626SSebastian Kapfer 		 * packet again, which may happen if all buttons are
14381d9f2626SSebastian Kapfer 		 * pressed. To avoid this let's reset the 4th bit which
14391d9f2626SSebastian Kapfer 		 * is normally 1.
14401d9f2626SSebastian Kapfer 		 */
14411d9f2626SSebastian Kapfer 		psmouse->packet[3] = psmouse->packet[6] & 0xf7;
14421d9f2626SSebastian Kapfer 		psmouse->pktcnt = 4;
14431d9f2626SSebastian Kapfer 	}
14441d9f2626SSebastian Kapfer 
14451d9f2626SSebastian Kapfer 	return PSMOUSE_GOOD_DATA;
14461d9f2626SSebastian Kapfer }
14471d9f2626SSebastian Kapfer 
14481d9f2626SSebastian Kapfer static void alps_flush_packet(unsigned long data)
14491d9f2626SSebastian Kapfer {
14501d9f2626SSebastian Kapfer 	struct psmouse *psmouse = (struct psmouse *)data;
145124af5cb9SKevin Cernekee 	struct alps_data *priv = psmouse->private;
14521d9f2626SSebastian Kapfer 
14531d9f2626SSebastian Kapfer 	serio_pause_rx(psmouse->ps2dev.serio);
14541d9f2626SSebastian Kapfer 
1455b46615feSSeth Forshee 	if (psmouse->pktcnt == psmouse->pktsize) {
14561d9f2626SSebastian Kapfer 
14571d9f2626SSebastian Kapfer 		/*
14581d9f2626SSebastian Kapfer 		 * We did not any more data in reasonable amount of time.
14591d9f2626SSebastian Kapfer 		 * Validate the last 3 bytes and process as a standard
14601d9f2626SSebastian Kapfer 		 * ALPS packet.
14611d9f2626SSebastian Kapfer 		 */
14621d9f2626SSebastian Kapfer 		if ((psmouse->packet[3] |
14631d9f2626SSebastian Kapfer 		     psmouse->packet[4] |
14641d9f2626SSebastian Kapfer 		     psmouse->packet[5]) & 0x80) {
1465b5d21704SDmitry Torokhov 			psmouse_dbg(psmouse,
14663b112923SAndy Shevchenko 				    "refusing packet %3ph (suspected interleaved ps/2)\n",
14673b112923SAndy Shevchenko 				    psmouse->packet + 3);
14681d9f2626SSebastian Kapfer 		} else {
146924af5cb9SKevin Cernekee 			priv->process_packet(psmouse);
14701d9f2626SSebastian Kapfer 		}
14711d9f2626SSebastian Kapfer 		psmouse->pktcnt = 0;
14721d9f2626SSebastian Kapfer 	}
14731d9f2626SSebastian Kapfer 
14741d9f2626SSebastian Kapfer 	serio_continue_rx(psmouse->ps2dev.serio);
14751d9f2626SSebastian Kapfer }
14761d9f2626SSebastian Kapfer 
14771d9f2626SSebastian Kapfer static psmouse_ret_t alps_process_byte(struct psmouse *psmouse)
14781d9f2626SSebastian Kapfer {
14791d9f2626SSebastian Kapfer 	struct alps_data *priv = psmouse->private;
14801d9f2626SSebastian Kapfer 
14814ab8f7f3SPali Rohár 	/*
14824ab8f7f3SPali Rohár 	 * Check if we are dealing with a bare PS/2 packet, presumably from
14834ab8f7f3SPali Rohár 	 * a device connected to the external PS/2 port. Because bare PS/2
14844ab8f7f3SPali Rohár 	 * protocol does not have enough constant bits to self-synchronize
14854ab8f7f3SPali Rohár 	 * properly we only do this if the device is fully synchronized.
14863db5b9f7SMasaki Ota 	 * Can not distinguish V8's first byte from PS/2 packet's
14874ab8f7f3SPali Rohár 	 */
14883db5b9f7SMasaki Ota 	if (priv->proto_version != ALPS_PROTO_V8 &&
14893db5b9f7SMasaki Ota 	    !psmouse->out_of_sync_cnt &&
14903db5b9f7SMasaki Ota 	    (psmouse->packet[0] & 0xc8) == 0x08) {
14913db5b9f7SMasaki Ota 
14921da177e4SLinus Torvalds 		if (psmouse->pktcnt == 3) {
149359c30afbSHans de Goede 			alps_report_bare_ps2_packet(psmouse, psmouse->packet,
14941d9f2626SSebastian Kapfer 						    true);
14951da177e4SLinus Torvalds 			return PSMOUSE_FULL_PACKET;
14961da177e4SLinus Torvalds 		}
14971da177e4SLinus Torvalds 		return PSMOUSE_GOOD_DATA;
14981da177e4SLinus Torvalds 	}
14991da177e4SLinus Torvalds 
15001d9f2626SSebastian Kapfer 	/* Check for PS/2 packet stuffed in the middle of ALPS packet. */
15011d9f2626SSebastian Kapfer 
150299df65e7SKevin Cernekee 	if ((priv->flags & ALPS_PS2_INTERLEAVED) &&
15031d9f2626SSebastian Kapfer 	    psmouse->pktcnt >= 4 && (psmouse->packet[3] & 0x0f) == 0x0f) {
15041d9f2626SSebastian Kapfer 		return alps_handle_interleaved_ps2(psmouse);
15051d9f2626SSebastian Kapfer 	}
15061d9f2626SSebastian Kapfer 
150799df65e7SKevin Cernekee 	if (!alps_is_valid_first_byte(priv, psmouse->packet[0])) {
1508b5d21704SDmitry Torokhov 		psmouse_dbg(psmouse,
1509b5d21704SDmitry Torokhov 			    "refusing packet[0] = %x (mask0 = %x, byte0 = %x)\n",
151099df65e7SKevin Cernekee 			    psmouse->packet[0], priv->mask0, priv->byte0);
15111da177e4SLinus Torvalds 		return PSMOUSE_BAD_DATA;
15121d9f2626SSebastian Kapfer 	}
15131da177e4SLinus Torvalds 
1514b46615feSSeth Forshee 	/* Bytes 2 - pktsize should have 0 in the highest bit */
1515a7ef82aeSPali Rohár 	if (priv->proto_version < ALPS_PROTO_V5 &&
151675af9e56SDave Turvene 	    psmouse->pktcnt >= 2 && psmouse->pktcnt <= psmouse->pktsize &&
15171d9f2626SSebastian Kapfer 	    (psmouse->packet[psmouse->pktcnt - 1] & 0x80)) {
1518b5d21704SDmitry Torokhov 		psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
1519b5d21704SDmitry Torokhov 			    psmouse->pktcnt - 1,
1520b5d21704SDmitry Torokhov 			    psmouse->packet[psmouse->pktcnt - 1]);
1521a7ef82aeSPali Rohár 
1522fb2dd7a6SDmitry Torokhov 		if (priv->proto_version == ALPS_PROTO_V3_RUSHMORE &&
1523a7ef82aeSPali Rohár 		    psmouse->pktcnt == psmouse->pktsize) {
1524a7ef82aeSPali Rohár 			/*
1525a7ef82aeSPali Rohár 			 * Some Dell boxes, such as Latitude E6440 or E7440
1526a7ef82aeSPali Rohár 			 * with closed lid, quite often smash last byte of
1527a7ef82aeSPali Rohár 			 * otherwise valid packet with 0xff. Given that the
1528a7ef82aeSPali Rohár 			 * next packet is very likely to be valid let's
1529a7ef82aeSPali Rohár 			 * report PSMOUSE_FULL_PACKET but not process data,
1530a7ef82aeSPali Rohár 			 * rather than reporting PSMOUSE_BAD_DATA and
1531a7ef82aeSPali Rohár 			 * filling the logs.
1532a7ef82aeSPali Rohár 			 */
1533a7ef82aeSPali Rohár 			return PSMOUSE_FULL_PACKET;
1534a7ef82aeSPali Rohár 		}
1535a7ef82aeSPali Rohár 
15361da177e4SLinus Torvalds 		return PSMOUSE_BAD_DATA;
15371d9f2626SSebastian Kapfer 	}
15381da177e4SLinus Torvalds 
15393db5b9f7SMasaki Ota 	if ((priv->proto_version == ALPS_PROTO_V7 &&
15403db5b9f7SMasaki Ota 			!alps_is_valid_package_v7(psmouse)) ||
15413db5b9f7SMasaki Ota 	    (priv->proto_version == ALPS_PROTO_V8 &&
15423db5b9f7SMasaki Ota 			!alps_is_valid_package_ss4_v2(psmouse))) {
15433808843cSYunkang Tang 		psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
15443808843cSYunkang Tang 			    psmouse->pktcnt - 1,
15453808843cSYunkang Tang 			    psmouse->packet[psmouse->pktcnt - 1]);
15463808843cSYunkang Tang 		return PSMOUSE_BAD_DATA;
15473808843cSYunkang Tang 	}
15483808843cSYunkang Tang 
1549b46615feSSeth Forshee 	if (psmouse->pktcnt == psmouse->pktsize) {
155024af5cb9SKevin Cernekee 		priv->process_packet(psmouse);
15511da177e4SLinus Torvalds 		return PSMOUSE_FULL_PACKET;
15521da177e4SLinus Torvalds 	}
15531da177e4SLinus Torvalds 
15541da177e4SLinus Torvalds 	return PSMOUSE_GOOD_DATA;
15551da177e4SLinus Torvalds }
15561da177e4SLinus Torvalds 
155725bded7cSSeth Forshee static int alps_command_mode_send_nibble(struct psmouse *psmouse, int nibble)
155825bded7cSSeth Forshee {
155925bded7cSSeth Forshee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
156025bded7cSSeth Forshee 	struct alps_data *priv = psmouse->private;
156125bded7cSSeth Forshee 	int command;
156225bded7cSSeth Forshee 	unsigned char *param;
156325bded7cSSeth Forshee 	unsigned char dummy[4];
156425bded7cSSeth Forshee 
156525bded7cSSeth Forshee 	BUG_ON(nibble > 0xf);
156625bded7cSSeth Forshee 
156725bded7cSSeth Forshee 	command = priv->nibble_commands[nibble].command;
156825bded7cSSeth Forshee 	param = (command & 0x0f00) ?
156925bded7cSSeth Forshee 		dummy : (unsigned char *)&priv->nibble_commands[nibble].data;
157025bded7cSSeth Forshee 
157125bded7cSSeth Forshee 	if (ps2_command(ps2dev, param, command))
157225bded7cSSeth Forshee 		return -1;
157325bded7cSSeth Forshee 
157425bded7cSSeth Forshee 	return 0;
157525bded7cSSeth Forshee }
157625bded7cSSeth Forshee 
157725bded7cSSeth Forshee static int alps_command_mode_set_addr(struct psmouse *psmouse, int addr)
157825bded7cSSeth Forshee {
157925bded7cSSeth Forshee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
158025bded7cSSeth Forshee 	struct alps_data *priv = psmouse->private;
158125bded7cSSeth Forshee 	int i, nibble;
158225bded7cSSeth Forshee 
158325bded7cSSeth Forshee 	if (ps2_command(ps2dev, NULL, priv->addr_command))
158425bded7cSSeth Forshee 		return -1;
158525bded7cSSeth Forshee 
158625bded7cSSeth Forshee 	for (i = 12; i >= 0; i -= 4) {
158725bded7cSSeth Forshee 		nibble = (addr >> i) & 0xf;
158825bded7cSSeth Forshee 		if (alps_command_mode_send_nibble(psmouse, nibble))
158925bded7cSSeth Forshee 			return -1;
159025bded7cSSeth Forshee 	}
159125bded7cSSeth Forshee 
159225bded7cSSeth Forshee 	return 0;
159325bded7cSSeth Forshee }
159425bded7cSSeth Forshee 
159525bded7cSSeth Forshee static int __alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
159625bded7cSSeth Forshee {
159725bded7cSSeth Forshee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
159825bded7cSSeth Forshee 	unsigned char param[4];
159925bded7cSSeth Forshee 
160025bded7cSSeth Forshee 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
160125bded7cSSeth Forshee 		return -1;
160225bded7cSSeth Forshee 
160325bded7cSSeth Forshee 	/*
160425bded7cSSeth Forshee 	 * The address being read is returned in the first two bytes
160525bded7cSSeth Forshee 	 * of the result. Check that this address matches the expected
160625bded7cSSeth Forshee 	 * address.
160725bded7cSSeth Forshee 	 */
160825bded7cSSeth Forshee 	if (addr != ((param[0] << 8) | param[1]))
160925bded7cSSeth Forshee 		return -1;
161025bded7cSSeth Forshee 
161125bded7cSSeth Forshee 	return param[2];
161225bded7cSSeth Forshee }
161325bded7cSSeth Forshee 
161425bded7cSSeth Forshee static int alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
161525bded7cSSeth Forshee {
161625bded7cSSeth Forshee 	if (alps_command_mode_set_addr(psmouse, addr))
161725bded7cSSeth Forshee 		return -1;
161825bded7cSSeth Forshee 	return __alps_command_mode_read_reg(psmouse, addr);
161925bded7cSSeth Forshee }
162025bded7cSSeth Forshee 
162125bded7cSSeth Forshee static int __alps_command_mode_write_reg(struct psmouse *psmouse, u8 value)
162225bded7cSSeth Forshee {
162325bded7cSSeth Forshee 	if (alps_command_mode_send_nibble(psmouse, (value >> 4) & 0xf))
162425bded7cSSeth Forshee 		return -1;
162525bded7cSSeth Forshee 	if (alps_command_mode_send_nibble(psmouse, value & 0xf))
162625bded7cSSeth Forshee 		return -1;
162725bded7cSSeth Forshee 	return 0;
162825bded7cSSeth Forshee }
162925bded7cSSeth Forshee 
163025bded7cSSeth Forshee static int alps_command_mode_write_reg(struct psmouse *psmouse, int addr,
163125bded7cSSeth Forshee 				       u8 value)
163225bded7cSSeth Forshee {
163325bded7cSSeth Forshee 	if (alps_command_mode_set_addr(psmouse, addr))
163425bded7cSSeth Forshee 		return -1;
163525bded7cSSeth Forshee 	return __alps_command_mode_write_reg(psmouse, value);
163625bded7cSSeth Forshee }
163725bded7cSSeth Forshee 
163824ba9707SKevin Cernekee static int alps_rpt_cmd(struct psmouse *psmouse, int init_command,
163924ba9707SKevin Cernekee 			int repeated_command, unsigned char *param)
164024ba9707SKevin Cernekee {
164124ba9707SKevin Cernekee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
164224ba9707SKevin Cernekee 
164324ba9707SKevin Cernekee 	param[0] = 0;
164424ba9707SKevin Cernekee 	if (init_command && ps2_command(ps2dev, param, init_command))
164524ba9707SKevin Cernekee 		return -EIO;
164624ba9707SKevin Cernekee 
164724ba9707SKevin Cernekee 	if (ps2_command(ps2dev,  NULL, repeated_command) ||
164824ba9707SKevin Cernekee 	    ps2_command(ps2dev,  NULL, repeated_command) ||
164924ba9707SKevin Cernekee 	    ps2_command(ps2dev,  NULL, repeated_command))
165024ba9707SKevin Cernekee 		return -EIO;
165124ba9707SKevin Cernekee 
165224ba9707SKevin Cernekee 	param[0] = param[1] = param[2] = 0xff;
165324ba9707SKevin Cernekee 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
165424ba9707SKevin Cernekee 		return -EIO;
165524ba9707SKevin Cernekee 
165639fbe585SDmitry Torokhov 	psmouse_dbg(psmouse, "%2.2X report: %3ph\n",
165739fbe585SDmitry Torokhov 		    repeated_command, param);
165824ba9707SKevin Cernekee 	return 0;
165924ba9707SKevin Cernekee }
166024ba9707SKevin Cernekee 
16613808843cSYunkang Tang static bool alps_check_valid_firmware_id(unsigned char id[])
16623808843cSYunkang Tang {
16633808843cSYunkang Tang 	if (id[0] == 0x73)
16643808843cSYunkang Tang 		return true;
16653808843cSYunkang Tang 
16663808843cSYunkang Tang 	if (id[0] == 0x88 &&
16673808843cSYunkang Tang 	    (id[1] == 0x07 ||
16683808843cSYunkang Tang 	     id[1] == 0x08 ||
16693808843cSYunkang Tang 	     (id[1] & 0xf0) == 0xb0 ||
16703808843cSYunkang Tang 	     (id[1] & 0xf0) == 0xc0)) {
16713808843cSYunkang Tang 		return true;
16723808843cSYunkang Tang 	}
16733808843cSYunkang Tang 
16743808843cSYunkang Tang 	return false;
16753808843cSYunkang Tang }
16763808843cSYunkang Tang 
1677d18e53fcSKevin Cernekee static int alps_enter_command_mode(struct psmouse *psmouse)
167825bded7cSSeth Forshee {
167925bded7cSSeth Forshee 	unsigned char param[4];
168025bded7cSSeth Forshee 
168124ba9707SKevin Cernekee 	if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_RESET_WRAP, param)) {
168225bded7cSSeth Forshee 		psmouse_err(psmouse, "failed to enter command mode\n");
168325bded7cSSeth Forshee 		return -1;
168425bded7cSSeth Forshee 	}
168525bded7cSSeth Forshee 
16863808843cSYunkang Tang 	if (!alps_check_valid_firmware_id(param)) {
168725bded7cSSeth Forshee 		psmouse_dbg(psmouse,
168824ba9707SKevin Cernekee 			    "unknown response while entering command mode\n");
168925bded7cSSeth Forshee 		return -1;
169025bded7cSSeth Forshee 	}
169125bded7cSSeth Forshee 	return 0;
169225bded7cSSeth Forshee }
169325bded7cSSeth Forshee 
169425bded7cSSeth Forshee static inline int alps_exit_command_mode(struct psmouse *psmouse)
169525bded7cSSeth Forshee {
169625bded7cSSeth Forshee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
169725bded7cSSeth Forshee 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM))
169825bded7cSSeth Forshee 		return -1;
169925bded7cSSeth Forshee 	return 0;
170025bded7cSSeth Forshee }
170125bded7cSSeth Forshee 
17021da177e4SLinus Torvalds /*
17031da177e4SLinus Torvalds  * For DualPoint devices select the device that should respond to
17041da177e4SLinus Torvalds  * subsequent commands. It looks like glidepad is behind stickpointer,
17051da177e4SLinus Torvalds  * I'd thought it would be other way around...
17061da177e4SLinus Torvalds  */
170725bded7cSSeth Forshee static int alps_passthrough_mode_v2(struct psmouse *psmouse, bool enable)
17081da177e4SLinus Torvalds {
17091da177e4SLinus Torvalds 	struct ps2dev *ps2dev = &psmouse->ps2dev;
17101da177e4SLinus Torvalds 	int cmd = enable ? PSMOUSE_CMD_SETSCALE21 : PSMOUSE_CMD_SETSCALE11;
17111da177e4SLinus Torvalds 
17121da177e4SLinus Torvalds 	if (ps2_command(ps2dev, NULL, cmd) ||
17131da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, cmd) ||
17141da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, cmd) ||
17151da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
17161da177e4SLinus Torvalds 		return -1;
17171da177e4SLinus Torvalds 
17181da177e4SLinus Torvalds 	/* we may get 3 more bytes, just ignore them */
1719c611763dSDmitry Torokhov 	ps2_drain(ps2dev, 3, 100);
17201da177e4SLinus Torvalds 
17211da177e4SLinus Torvalds 	return 0;
17221da177e4SLinus Torvalds }
17231da177e4SLinus Torvalds 
172425bded7cSSeth Forshee static int alps_absolute_mode_v1_v2(struct psmouse *psmouse)
17251da177e4SLinus Torvalds {
17261da177e4SLinus Torvalds 	struct ps2dev *ps2dev = &psmouse->ps2dev;
17271da177e4SLinus Torvalds 
17281da177e4SLinus Torvalds 	/* Try ALPS magic knock - 4 disable before enable */
17291da177e4SLinus Torvalds 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
17301da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
17311da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
17321da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
17331da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
17341da177e4SLinus Torvalds 		return -1;
17351da177e4SLinus Torvalds 
17361da177e4SLinus Torvalds 	/*
17371da177e4SLinus Torvalds 	 * Switch mouse to poll (remote) mode so motion data will not
17381da177e4SLinus Torvalds 	 * get in our way
17391da177e4SLinus Torvalds 	 */
17401da177e4SLinus Torvalds 	return ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETPOLL);
17411da177e4SLinus Torvalds }
17421da177e4SLinus Torvalds 
174395f75e91SYunkang Tang static int alps_monitor_mode_send_word(struct psmouse *psmouse, u16 word)
174495f75e91SYunkang Tang {
174595f75e91SYunkang Tang 	int i, nibble;
174695f75e91SYunkang Tang 
174795f75e91SYunkang Tang 	/*
174895f75e91SYunkang Tang 	 * b0-b11 are valid bits, send sequence is inverse.
174995f75e91SYunkang Tang 	 * e.g. when word = 0x0123, nibble send sequence is 3, 2, 1
175095f75e91SYunkang Tang 	 */
175195f75e91SYunkang Tang 	for (i = 0; i <= 8; i += 4) {
175295f75e91SYunkang Tang 		nibble = (word >> i) & 0xf;
175395f75e91SYunkang Tang 		if (alps_command_mode_send_nibble(psmouse, nibble))
175495f75e91SYunkang Tang 			return -1;
175595f75e91SYunkang Tang 	}
175695f75e91SYunkang Tang 
175795f75e91SYunkang Tang 	return 0;
175895f75e91SYunkang Tang }
175995f75e91SYunkang Tang 
176095f75e91SYunkang Tang static int alps_monitor_mode_write_reg(struct psmouse *psmouse,
176195f75e91SYunkang Tang 				       u16 addr, u16 value)
176295f75e91SYunkang Tang {
176395f75e91SYunkang Tang 	struct ps2dev *ps2dev = &psmouse->ps2dev;
176495f75e91SYunkang Tang 
176595f75e91SYunkang Tang 	/* 0x0A0 is the command to write the word */
176695f75e91SYunkang Tang 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE) ||
176795f75e91SYunkang Tang 	    alps_monitor_mode_send_word(psmouse, 0x0A0) ||
176895f75e91SYunkang Tang 	    alps_monitor_mode_send_word(psmouse, addr) ||
176995f75e91SYunkang Tang 	    alps_monitor_mode_send_word(psmouse, value) ||
177095f75e91SYunkang Tang 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
177195f75e91SYunkang Tang 		return -1;
177295f75e91SYunkang Tang 
177395f75e91SYunkang Tang 	return 0;
177495f75e91SYunkang Tang }
177595f75e91SYunkang Tang 
177695f75e91SYunkang Tang static int alps_monitor_mode(struct psmouse *psmouse, bool enable)
177795f75e91SYunkang Tang {
177895f75e91SYunkang Tang 	struct ps2dev *ps2dev = &psmouse->ps2dev;
177995f75e91SYunkang Tang 
178095f75e91SYunkang Tang 	if (enable) {
178195f75e91SYunkang Tang 		/* EC E9 F5 F5 E7 E6 E7 E9 to enter monitor mode */
178295f75e91SYunkang Tang 		if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
178395f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO) ||
178495f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
178595f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
178695f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
178795f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
178895f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
178995f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO))
179095f75e91SYunkang Tang 			return -1;
179195f75e91SYunkang Tang 	} else {
179295f75e91SYunkang Tang 		/* EC to exit monitor mode */
179395f75e91SYunkang Tang 		if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP))
179495f75e91SYunkang Tang 			return -1;
179595f75e91SYunkang Tang 	}
179695f75e91SYunkang Tang 
179795f75e91SYunkang Tang 	return 0;
179895f75e91SYunkang Tang }
179995f75e91SYunkang Tang 
180095f75e91SYunkang Tang static int alps_absolute_mode_v6(struct psmouse *psmouse)
180195f75e91SYunkang Tang {
180295f75e91SYunkang Tang 	u16 reg_val = 0x181;
180395f75e91SYunkang Tang 	int ret = -1;
180495f75e91SYunkang Tang 
180595f75e91SYunkang Tang 	/* enter monitor mode, to write the register */
180695f75e91SYunkang Tang 	if (alps_monitor_mode(psmouse, true))
180795f75e91SYunkang Tang 		return -1;
180895f75e91SYunkang Tang 
180995f75e91SYunkang Tang 	ret = alps_monitor_mode_write_reg(psmouse, 0x000, reg_val);
181095f75e91SYunkang Tang 
181195f75e91SYunkang Tang 	if (alps_monitor_mode(psmouse, false))
181295f75e91SYunkang Tang 		ret = -1;
181395f75e91SYunkang Tang 
181495f75e91SYunkang Tang 	return ret;
181595f75e91SYunkang Tang }
181695f75e91SYunkang Tang 
18171da177e4SLinus Torvalds static int alps_get_status(struct psmouse *psmouse, char *param)
18181da177e4SLinus Torvalds {
18191da177e4SLinus Torvalds 	/* Get status: 0xF5 0xF5 0xF5 0xE9 */
182024ba9707SKevin Cernekee 	if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_DISABLE, param))
18211da177e4SLinus Torvalds 		return -1;
18221da177e4SLinus Torvalds 
18231da177e4SLinus Torvalds 	return 0;
18241da177e4SLinus Torvalds }
18251da177e4SLinus Torvalds 
18261da177e4SLinus Torvalds /*
18271da177e4SLinus Torvalds  * Turn touchpad tapping on or off. The sequences are:
18281da177e4SLinus Torvalds  * 0xE9 0xF5 0xF5 0xF3 0x0A to enable,
18291da177e4SLinus Torvalds  * 0xE9 0xF5 0xF5 0xE8 0x00 to disable.
18301da177e4SLinus Torvalds  * My guess that 0xE9 (GetInfo) is here as a sync point.
18311da177e4SLinus Torvalds  * For models that also have stickpointer (DualPoints) its tapping
18321da177e4SLinus Torvalds  * is controlled separately (0xE6 0xE6 0xE6 0xF3 0x14|0x0A) but
18331da177e4SLinus Torvalds  * we don't fiddle with it.
18341da177e4SLinus Torvalds  */
18351da177e4SLinus Torvalds static int alps_tap_mode(struct psmouse *psmouse, int enable)
18361da177e4SLinus Torvalds {
18371da177e4SLinus Torvalds 	struct ps2dev *ps2dev = &psmouse->ps2dev;
18381da177e4SLinus Torvalds 	int cmd = enable ? PSMOUSE_CMD_SETRATE : PSMOUSE_CMD_SETRES;
18391da177e4SLinus Torvalds 	unsigned char tap_arg = enable ? 0x0A : 0x00;
18401da177e4SLinus Torvalds 	unsigned char param[4];
18411da177e4SLinus Torvalds 
18421da177e4SLinus Torvalds 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO) ||
18431da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
18441da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
18451da177e4SLinus Torvalds 	    ps2_command(ps2dev, &tap_arg, cmd))
18461da177e4SLinus Torvalds 		return -1;
18471da177e4SLinus Torvalds 
18481da177e4SLinus Torvalds 	if (alps_get_status(psmouse, param))
18491da177e4SLinus Torvalds 		return -1;
18501da177e4SLinus Torvalds 
18511da177e4SLinus Torvalds 	return 0;
18521da177e4SLinus Torvalds }
18531da177e4SLinus Torvalds 
1854f0d5c6f4SDmitry Torokhov /*
1855f0d5c6f4SDmitry Torokhov  * alps_poll() - poll the touchpad for current motion packet.
1856f0d5c6f4SDmitry Torokhov  * Used in resync.
1857f0d5c6f4SDmitry Torokhov  */
1858f0d5c6f4SDmitry Torokhov static int alps_poll(struct psmouse *psmouse)
1859f0d5c6f4SDmitry Torokhov {
1860f0d5c6f4SDmitry Torokhov 	struct alps_data *priv = psmouse->private;
1861b46615feSSeth Forshee 	unsigned char buf[sizeof(psmouse->packet)];
1862b7802c5cSDmitry Torokhov 	bool poll_failed;
1863f0d5c6f4SDmitry Torokhov 
186499df65e7SKevin Cernekee 	if (priv->flags & ALPS_PASS)
186525bded7cSSeth Forshee 		alps_passthrough_mode_v2(psmouse, true);
1866f0d5c6f4SDmitry Torokhov 
1867f0d5c6f4SDmitry Torokhov 	poll_failed = ps2_command(&psmouse->ps2dev, buf,
1868f0d5c6f4SDmitry Torokhov 				  PSMOUSE_CMD_POLL | (psmouse->pktsize << 8)) < 0;
1869f0d5c6f4SDmitry Torokhov 
187099df65e7SKevin Cernekee 	if (priv->flags & ALPS_PASS)
187125bded7cSSeth Forshee 		alps_passthrough_mode_v2(psmouse, false);
1872f0d5c6f4SDmitry Torokhov 
187399df65e7SKevin Cernekee 	if (poll_failed || (buf[0] & priv->mask0) != priv->byte0)
1874f0d5c6f4SDmitry Torokhov 		return -1;
1875f0d5c6f4SDmitry Torokhov 
1876f0d5c6f4SDmitry Torokhov 	if ((psmouse->badbyte & 0xc8) == 0x08) {
1877f0d5c6f4SDmitry Torokhov /*
1878f0d5c6f4SDmitry Torokhov  * Poll the track stick ...
1879f0d5c6f4SDmitry Torokhov  */
1880f0d5c6f4SDmitry Torokhov 		if (ps2_command(&psmouse->ps2dev, buf, PSMOUSE_CMD_POLL | (3 << 8)))
1881f0d5c6f4SDmitry Torokhov 			return -1;
1882f0d5c6f4SDmitry Torokhov 	}
1883f0d5c6f4SDmitry Torokhov 
1884f0d5c6f4SDmitry Torokhov 	memcpy(psmouse->packet, buf, sizeof(buf));
1885f0d5c6f4SDmitry Torokhov 	return 0;
1886f0d5c6f4SDmitry Torokhov }
1887f0d5c6f4SDmitry Torokhov 
188825bded7cSSeth Forshee static int alps_hw_init_v1_v2(struct psmouse *psmouse)
18891da177e4SLinus Torvalds {
18901da177e4SLinus Torvalds 	struct alps_data *priv = psmouse->private;
18911da177e4SLinus Torvalds 
189299df65e7SKevin Cernekee 	if ((priv->flags & ALPS_PASS) &&
189325bded7cSSeth Forshee 	    alps_passthrough_mode_v2(psmouse, true)) {
18941da177e4SLinus Torvalds 		return -1;
1895b7802c5cSDmitry Torokhov 	}
18961da177e4SLinus Torvalds 
1897b7802c5cSDmitry Torokhov 	if (alps_tap_mode(psmouse, true)) {
1898b5d21704SDmitry Torokhov 		psmouse_warn(psmouse, "Failed to enable hardware tapping\n");
18991da177e4SLinus Torvalds 		return -1;
1900963f626dSPeter Osterlund 	}
19011da177e4SLinus Torvalds 
190225bded7cSSeth Forshee 	if (alps_absolute_mode_v1_v2(psmouse)) {
1903b5d21704SDmitry Torokhov 		psmouse_err(psmouse, "Failed to enable absolute mode\n");
19041da177e4SLinus Torvalds 		return -1;
19051da177e4SLinus Torvalds 	}
19061da177e4SLinus Torvalds 
190799df65e7SKevin Cernekee 	if ((priv->flags & ALPS_PASS) &&
190825bded7cSSeth Forshee 	    alps_passthrough_mode_v2(psmouse, false)) {
19091da177e4SLinus Torvalds 		return -1;
1910b7802c5cSDmitry Torokhov 	}
19111da177e4SLinus Torvalds 
19121e0c5b12SDmitry Torokhov 	/* ALPS needs stream mode, otherwise it won't report any data */
19131e0c5b12SDmitry Torokhov 	if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)) {
1914b5d21704SDmitry Torokhov 		psmouse_err(psmouse, "Failed to enable stream mode\n");
19151e0c5b12SDmitry Torokhov 		return -1;
19161e0c5b12SDmitry Torokhov 	}
19171e0c5b12SDmitry Torokhov 
19181e0c5b12SDmitry Torokhov 	return 0;
19191e0c5b12SDmitry Torokhov }
19201e0c5b12SDmitry Torokhov 
192195f75e91SYunkang Tang static int alps_hw_init_v6(struct psmouse *psmouse)
192295f75e91SYunkang Tang {
192395f75e91SYunkang Tang 	unsigned char param[2] = {0xC8, 0x14};
192495f75e91SYunkang Tang 
192595f75e91SYunkang Tang 	/* Enter passthrough mode to let trackpoint enter 6byte raw mode */
192695f75e91SYunkang Tang 	if (alps_passthrough_mode_v2(psmouse, true))
192795f75e91SYunkang Tang 		return -1;
192895f75e91SYunkang Tang 
192995f75e91SYunkang Tang 	if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
193095f75e91SYunkang Tang 	    ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
193195f75e91SYunkang Tang 	    ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
193295f75e91SYunkang Tang 	    ps2_command(&psmouse->ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
193395f75e91SYunkang Tang 	    ps2_command(&psmouse->ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
193495f75e91SYunkang Tang 		return -1;
193595f75e91SYunkang Tang 
193695f75e91SYunkang Tang 	if (alps_passthrough_mode_v2(psmouse, false))
193795f75e91SYunkang Tang 		return -1;
193895f75e91SYunkang Tang 
193995f75e91SYunkang Tang 	if (alps_absolute_mode_v6(psmouse)) {
194095f75e91SYunkang Tang 		psmouse_err(psmouse, "Failed to enable absolute mode\n");
194195f75e91SYunkang Tang 		return -1;
194295f75e91SYunkang Tang 	}
194395f75e91SYunkang Tang 
194495f75e91SYunkang Tang 	return 0;
194595f75e91SYunkang Tang }
194695f75e91SYunkang Tang 
194725bded7cSSeth Forshee /*
1948cd401204SKevin Cernekee  * Enable or disable passthrough mode to the trackstick.
194925bded7cSSeth Forshee  */
1950cd401204SKevin Cernekee static int alps_passthrough_mode_v3(struct psmouse *psmouse,
1951cd401204SKevin Cernekee 				    int reg_base, bool enable)
195225bded7cSSeth Forshee {
1953cd401204SKevin Cernekee 	int reg_val, ret = -1;
195425bded7cSSeth Forshee 
1955d18e53fcSKevin Cernekee 	if (alps_enter_command_mode(psmouse))
195625bded7cSSeth Forshee 		return -1;
195725bded7cSSeth Forshee 
1958cd401204SKevin Cernekee 	reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x0008);
1959cd401204SKevin Cernekee 	if (reg_val == -1)
1960cd401204SKevin Cernekee 		goto error;
1961cd401204SKevin Cernekee 
196225bded7cSSeth Forshee 	if (enable)
196325bded7cSSeth Forshee 		reg_val |= 0x01;
196425bded7cSSeth Forshee 	else
196525bded7cSSeth Forshee 		reg_val &= ~0x01;
196625bded7cSSeth Forshee 
1967cd401204SKevin Cernekee 	ret = __alps_command_mode_write_reg(psmouse, reg_val);
196825bded7cSSeth Forshee 
1969cd401204SKevin Cernekee error:
1970cd401204SKevin Cernekee 	if (alps_exit_command_mode(psmouse))
1971cd401204SKevin Cernekee 		ret = -1;
1972cd401204SKevin Cernekee 	return ret;
197325bded7cSSeth Forshee }
197425bded7cSSeth Forshee 
197525bded7cSSeth Forshee /* Must be in command mode when calling this function */
197625bded7cSSeth Forshee static int alps_absolute_mode_v3(struct psmouse *psmouse)
197725bded7cSSeth Forshee {
197825bded7cSSeth Forshee 	int reg_val;
197925bded7cSSeth Forshee 
198025bded7cSSeth Forshee 	reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
198125bded7cSSeth Forshee 	if (reg_val == -1)
198225bded7cSSeth Forshee 		return -1;
198325bded7cSSeth Forshee 
198425bded7cSSeth Forshee 	reg_val |= 0x06;
198525bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, reg_val))
198625bded7cSSeth Forshee 		return -1;
198725bded7cSSeth Forshee 
198825bded7cSSeth Forshee 	return 0;
198925bded7cSSeth Forshee }
199025bded7cSSeth Forshee 
1991cd401204SKevin Cernekee static int alps_probe_trackstick_v3(struct psmouse *psmouse, int reg_base)
199225bded7cSSeth Forshee {
1993cd401204SKevin Cernekee 	int ret = -EIO, reg_val;
199425bded7cSSeth Forshee 
1995d18e53fcSKevin Cernekee 	if (alps_enter_command_mode(psmouse))
199625bded7cSSeth Forshee 		goto error;
199725bded7cSSeth Forshee 
1998cd401204SKevin Cernekee 	reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x08);
199925bded7cSSeth Forshee 	if (reg_val == -1)
200025bded7cSSeth Forshee 		goto error;
2001cd401204SKevin Cernekee 
2002cd401204SKevin Cernekee 	/* bit 7: trackstick is present */
2003cd401204SKevin Cernekee 	ret = reg_val & 0x80 ? 0 : -ENODEV;
2004cd401204SKevin Cernekee 
2005cd401204SKevin Cernekee error:
2006cd401204SKevin Cernekee 	alps_exit_command_mode(psmouse);
2007cd401204SKevin Cernekee 	return ret;
2008cd401204SKevin Cernekee }
2009cd401204SKevin Cernekee 
2010cd401204SKevin Cernekee static int alps_setup_trackstick_v3(struct psmouse *psmouse, int reg_base)
2011cd401204SKevin Cernekee {
2012cd401204SKevin Cernekee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
2013cd401204SKevin Cernekee 	int ret = 0;
2014cd401204SKevin Cernekee 	unsigned char param[4];
2015cd401204SKevin Cernekee 
2016cd401204SKevin Cernekee 	if (alps_passthrough_mode_v3(psmouse, reg_base, true))
2017cd401204SKevin Cernekee 		return -EIO;
201825bded7cSSeth Forshee 
201925bded7cSSeth Forshee 	/*
202025bded7cSSeth Forshee 	 * E7 report for the trackstick
202125bded7cSSeth Forshee 	 *
202225bded7cSSeth Forshee 	 * There have been reports of failures to seem to trace back
202325bded7cSSeth Forshee 	 * to the above trackstick check failing. When these occur
202425bded7cSSeth Forshee 	 * this E7 report fails, so when that happens we continue
202525bded7cSSeth Forshee 	 * with the assumption that there isn't a trackstick after
202625bded7cSSeth Forshee 	 * all.
202725bded7cSSeth Forshee 	 */
2028cd401204SKevin Cernekee 	if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_SETSCALE21, param)) {
2029a09221e8SDmitry Torokhov 		psmouse_warn(psmouse, "Failed to initialize trackstick (E7 report failed)\n");
2030cd401204SKevin Cernekee 		ret = -ENODEV;
203125bded7cSSeth Forshee 	} else {
203239fbe585SDmitry Torokhov 		psmouse_dbg(psmouse, "trackstick E7 report: %3ph\n", param);
203325bded7cSSeth Forshee 
203425bded7cSSeth Forshee 		/*
203525bded7cSSeth Forshee 		 * Not sure what this does, but it is absolutely
203625bded7cSSeth Forshee 		 * essential. Without it, the touchpad does not
203725bded7cSSeth Forshee 		 * work at all and the trackstick just emits normal
203825bded7cSSeth Forshee 		 * PS/2 packets.
203925bded7cSSeth Forshee 		 */
204025bded7cSSeth Forshee 		if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
204125bded7cSSeth Forshee 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
204225bded7cSSeth Forshee 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
204325bded7cSSeth Forshee 		    alps_command_mode_send_nibble(psmouse, 0x9) ||
204425bded7cSSeth Forshee 		    alps_command_mode_send_nibble(psmouse, 0x4)) {
204525bded7cSSeth Forshee 			psmouse_err(psmouse,
204625bded7cSSeth Forshee 				    "Error sending magic E6 sequence\n");
2047cd401204SKevin Cernekee 			ret = -EIO;
204825bded7cSSeth Forshee 			goto error;
204925bded7cSSeth Forshee 		}
205025bded7cSSeth Forshee 
2051cd401204SKevin Cernekee 		/*
2052cd401204SKevin Cernekee 		 * This ensures the trackstick packets are in the format
2053cd401204SKevin Cernekee 		 * supported by this driver. If bit 1 isn't set the packet
2054cd401204SKevin Cernekee 		 * format is different.
2055cd401204SKevin Cernekee 		 */
2056d18e53fcSKevin Cernekee 		if (alps_enter_command_mode(psmouse) ||
2057cd401204SKevin Cernekee 		    alps_command_mode_write_reg(psmouse,
2058cd401204SKevin Cernekee 						reg_base + 0x08, 0x82) ||
2059cd401204SKevin Cernekee 		    alps_exit_command_mode(psmouse))
2060cd401204SKevin Cernekee 			ret = -EIO;
2061cd401204SKevin Cernekee 	}
2062cd401204SKevin Cernekee 
2063cd401204SKevin Cernekee error:
2064cd401204SKevin Cernekee 	if (alps_passthrough_mode_v3(psmouse, reg_base, false))
2065cd401204SKevin Cernekee 		ret = -EIO;
2066cd401204SKevin Cernekee 
2067cd401204SKevin Cernekee 	return ret;
2068cd401204SKevin Cernekee }
2069cd401204SKevin Cernekee 
2070cd401204SKevin Cernekee static int alps_hw_init_v3(struct psmouse *psmouse)
2071cd401204SKevin Cernekee {
2072cd401204SKevin Cernekee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
2073cd401204SKevin Cernekee 	int reg_val;
2074cd401204SKevin Cernekee 	unsigned char param[4];
2075cd401204SKevin Cernekee 
2076cd401204SKevin Cernekee 	reg_val = alps_probe_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE);
2077cd401204SKevin Cernekee 	if (reg_val == -EIO)
2078cd401204SKevin Cernekee 		goto error;
207939fbe585SDmitry Torokhov 
2080cd401204SKevin Cernekee 	if (reg_val == 0 &&
2081cd401204SKevin Cernekee 	    alps_setup_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE) == -EIO)
2082cd401204SKevin Cernekee 		goto error;
2083cd401204SKevin Cernekee 
2084d18e53fcSKevin Cernekee 	if (alps_enter_command_mode(psmouse) ||
2085cd401204SKevin Cernekee 	    alps_absolute_mode_v3(psmouse)) {
208625bded7cSSeth Forshee 		psmouse_err(psmouse, "Failed to enter absolute mode\n");
208725bded7cSSeth Forshee 		goto error;
208825bded7cSSeth Forshee 	}
208925bded7cSSeth Forshee 
209025bded7cSSeth Forshee 	reg_val = alps_command_mode_read_reg(psmouse, 0x0006);
209125bded7cSSeth Forshee 	if (reg_val == -1)
209225bded7cSSeth Forshee 		goto error;
209325bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
209425bded7cSSeth Forshee 		goto error;
209525bded7cSSeth Forshee 
209625bded7cSSeth Forshee 	reg_val = alps_command_mode_read_reg(psmouse, 0x0007);
209725bded7cSSeth Forshee 	if (reg_val == -1)
209825bded7cSSeth Forshee 		goto error;
209925bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
210025bded7cSSeth Forshee 		goto error;
210125bded7cSSeth Forshee 
210225bded7cSSeth Forshee 	if (alps_command_mode_read_reg(psmouse, 0x0144) == -1)
210325bded7cSSeth Forshee 		goto error;
210425bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, 0x04))
210525bded7cSSeth Forshee 		goto error;
210625bded7cSSeth Forshee 
210725bded7cSSeth Forshee 	if (alps_command_mode_read_reg(psmouse, 0x0159) == -1)
210825bded7cSSeth Forshee 		goto error;
210925bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, 0x03))
211025bded7cSSeth Forshee 		goto error;
211125bded7cSSeth Forshee 
211225bded7cSSeth Forshee 	if (alps_command_mode_read_reg(psmouse, 0x0163) == -1)
211325bded7cSSeth Forshee 		goto error;
211425bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0163, 0x03))
211525bded7cSSeth Forshee 		goto error;
211625bded7cSSeth Forshee 
211725bded7cSSeth Forshee 	if (alps_command_mode_read_reg(psmouse, 0x0162) == -1)
211825bded7cSSeth Forshee 		goto error;
211925bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0162, 0x04))
212025bded7cSSeth Forshee 		goto error;
212125bded7cSSeth Forshee 
212225bded7cSSeth Forshee 	alps_exit_command_mode(psmouse);
212325bded7cSSeth Forshee 
212425bded7cSSeth Forshee 	/* Set rate and enable data reporting */
212525bded7cSSeth Forshee 	param[0] = 0x64;
212625bded7cSSeth Forshee 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
212725bded7cSSeth Forshee 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
212825bded7cSSeth Forshee 		psmouse_err(psmouse, "Failed to enable data reporting\n");
212925bded7cSSeth Forshee 		return -1;
213025bded7cSSeth Forshee 	}
213125bded7cSSeth Forshee 
213225bded7cSSeth Forshee 	return 0;
213325bded7cSSeth Forshee 
213425bded7cSSeth Forshee error:
213525bded7cSSeth Forshee 	/*
213625bded7cSSeth Forshee 	 * Leaving the touchpad in command mode will essentially render
213725bded7cSSeth Forshee 	 * it unusable until the machine reboots, so exit it here just
213825bded7cSSeth Forshee 	 * to be safe
213925bded7cSSeth Forshee 	 */
214025bded7cSSeth Forshee 	alps_exit_command_mode(psmouse);
214125bded7cSSeth Forshee 	return -1;
214225bded7cSSeth Forshee }
214325bded7cSSeth Forshee 
2144f3f33c67SHans de Goede static int alps_get_v3_v7_resolution(struct psmouse *psmouse, int reg_pitch)
2145f3f33c67SHans de Goede {
2146f3f33c67SHans de Goede 	int reg, x_pitch, y_pitch, x_electrode, y_electrode, x_phys, y_phys;
2147f3f33c67SHans de Goede 	struct alps_data *priv = psmouse->private;
2148f3f33c67SHans de Goede 
2149f3f33c67SHans de Goede 	reg = alps_command_mode_read_reg(psmouse, reg_pitch);
2150f3f33c67SHans de Goede 	if (reg < 0)
2151f3f33c67SHans de Goede 		return reg;
2152f3f33c67SHans de Goede 
2153f3f33c67SHans de Goede 	x_pitch = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
2154f3f33c67SHans de Goede 	x_pitch = 50 + 2 * x_pitch; /* In 0.1 mm units */
2155f3f33c67SHans de Goede 
2156f3f33c67SHans de Goede 	y_pitch = (char)reg >> 4; /* sign extend upper 4 bits */
2157f3f33c67SHans de Goede 	y_pitch = 36 + 2 * y_pitch; /* In 0.1 mm units */
2158f3f33c67SHans de Goede 
2159f3f33c67SHans de Goede 	reg = alps_command_mode_read_reg(psmouse, reg_pitch + 1);
2160f3f33c67SHans de Goede 	if (reg < 0)
2161f3f33c67SHans de Goede 		return reg;
2162f3f33c67SHans de Goede 
2163f3f33c67SHans de Goede 	x_electrode = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
2164f3f33c67SHans de Goede 	x_electrode = 17 + x_electrode;
2165f3f33c67SHans de Goede 
2166f3f33c67SHans de Goede 	y_electrode = (char)reg >> 4; /* sign extend upper 4 bits */
2167f3f33c67SHans de Goede 	y_electrode = 13 + y_electrode;
2168f3f33c67SHans de Goede 
2169f3f33c67SHans de Goede 	x_phys = x_pitch * (x_electrode - 1); /* In 0.1 mm units */
2170f3f33c67SHans de Goede 	y_phys = y_pitch * (y_electrode - 1); /* In 0.1 mm units */
2171f3f33c67SHans de Goede 
2172f3f33c67SHans de Goede 	priv->x_res = priv->x_max * 10 / x_phys; /* units / mm */
2173f3f33c67SHans de Goede 	priv->y_res = priv->y_max * 10 / y_phys; /* units / mm */
2174f3f33c67SHans de Goede 
2175f3f33c67SHans de Goede 	psmouse_dbg(psmouse,
2176f3f33c67SHans de Goede 		    "pitch %dx%d num-electrodes %dx%d physical size %dx%d mm res %dx%d\n",
2177f3f33c67SHans de Goede 		    x_pitch, y_pitch, x_electrode, y_electrode,
2178f3f33c67SHans de Goede 		    x_phys / 10, y_phys / 10, priv->x_res, priv->y_res);
2179f3f33c67SHans de Goede 
2180f3f33c67SHans de Goede 	return 0;
2181f3f33c67SHans de Goede }
2182f3f33c67SHans de Goede 
21831302bac3SKevin Cernekee static int alps_hw_init_rushmore_v3(struct psmouse *psmouse)
21841302bac3SKevin Cernekee {
2185cd401204SKevin Cernekee 	struct alps_data *priv = psmouse->private;
21861302bac3SKevin Cernekee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
21871302bac3SKevin Cernekee 	int reg_val, ret = -1;
21881302bac3SKevin Cernekee 
2189cd401204SKevin Cernekee 	if (priv->flags & ALPS_DUALPOINT) {
2190cd401204SKevin Cernekee 		reg_val = alps_setup_trackstick_v3(psmouse,
2191cd401204SKevin Cernekee 						   ALPS_REG_BASE_RUSHMORE);
2192cd401204SKevin Cernekee 		if (reg_val == -EIO)
2193cd401204SKevin Cernekee 			goto error;
2194cd401204SKevin Cernekee 	}
2195cd401204SKevin Cernekee 
2196d18e53fcSKevin Cernekee 	if (alps_enter_command_mode(psmouse) ||
21971302bac3SKevin Cernekee 	    alps_command_mode_read_reg(psmouse, 0xc2d9) == -1 ||
21981302bac3SKevin Cernekee 	    alps_command_mode_write_reg(psmouse, 0xc2cb, 0x00))
21991302bac3SKevin Cernekee 		goto error;
22001302bac3SKevin Cernekee 
2201f3f33c67SHans de Goede 	if (alps_get_v3_v7_resolution(psmouse, 0xc2da))
2202f3f33c67SHans de Goede 		goto error;
2203f3f33c67SHans de Goede 
22041302bac3SKevin Cernekee 	reg_val = alps_command_mode_read_reg(psmouse, 0xc2c6);
22051302bac3SKevin Cernekee 	if (reg_val == -1)
22061302bac3SKevin Cernekee 		goto error;
22071302bac3SKevin Cernekee 	if (__alps_command_mode_write_reg(psmouse, reg_val & 0xfd))
22081302bac3SKevin Cernekee 		goto error;
22091302bac3SKevin Cernekee 
22101302bac3SKevin Cernekee 	if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
22111302bac3SKevin Cernekee 		goto error;
22121302bac3SKevin Cernekee 
22131302bac3SKevin Cernekee 	/* enter absolute mode */
22141302bac3SKevin Cernekee 	reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
22151302bac3SKevin Cernekee 	if (reg_val == -1)
22161302bac3SKevin Cernekee 		goto error;
22171302bac3SKevin Cernekee 	if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
22181302bac3SKevin Cernekee 		goto error;
22191302bac3SKevin Cernekee 
22201302bac3SKevin Cernekee 	alps_exit_command_mode(psmouse);
22211302bac3SKevin Cernekee 	return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
22221302bac3SKevin Cernekee 
22231302bac3SKevin Cernekee error:
22241302bac3SKevin Cernekee 	alps_exit_command_mode(psmouse);
22251302bac3SKevin Cernekee 	return ret;
22261302bac3SKevin Cernekee }
22271302bac3SKevin Cernekee 
222825bded7cSSeth Forshee /* Must be in command mode when calling this function */
222925bded7cSSeth Forshee static int alps_absolute_mode_v4(struct psmouse *psmouse)
223025bded7cSSeth Forshee {
223125bded7cSSeth Forshee 	int reg_val;
223225bded7cSSeth Forshee 
223325bded7cSSeth Forshee 	reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
223425bded7cSSeth Forshee 	if (reg_val == -1)
223525bded7cSSeth Forshee 		return -1;
223625bded7cSSeth Forshee 
223725bded7cSSeth Forshee 	reg_val |= 0x02;
223825bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, reg_val))
223925bded7cSSeth Forshee 		return -1;
224025bded7cSSeth Forshee 
224125bded7cSSeth Forshee 	return 0;
224225bded7cSSeth Forshee }
224325bded7cSSeth Forshee 
224425bded7cSSeth Forshee static int alps_hw_init_v4(struct psmouse *psmouse)
224525bded7cSSeth Forshee {
224625bded7cSSeth Forshee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
224725bded7cSSeth Forshee 	unsigned char param[4];
224825bded7cSSeth Forshee 
2249d18e53fcSKevin Cernekee 	if (alps_enter_command_mode(psmouse))
225025bded7cSSeth Forshee 		goto error;
225125bded7cSSeth Forshee 
225225bded7cSSeth Forshee 	if (alps_absolute_mode_v4(psmouse)) {
225325bded7cSSeth Forshee 		psmouse_err(psmouse, "Failed to enter absolute mode\n");
225425bded7cSSeth Forshee 		goto error;
225525bded7cSSeth Forshee 	}
225625bded7cSSeth Forshee 
225725bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0007, 0x8c))
225825bded7cSSeth Forshee 		goto error;
225925bded7cSSeth Forshee 
226025bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0149, 0x03))
226125bded7cSSeth Forshee 		goto error;
226225bded7cSSeth Forshee 
226325bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0160, 0x03))
226425bded7cSSeth Forshee 		goto error;
226525bded7cSSeth Forshee 
226625bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x017f, 0x15))
226725bded7cSSeth Forshee 		goto error;
226825bded7cSSeth Forshee 
226925bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0151, 0x01))
227025bded7cSSeth Forshee 		goto error;
227125bded7cSSeth Forshee 
227225bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0168, 0x03))
227325bded7cSSeth Forshee 		goto error;
227425bded7cSSeth Forshee 
227525bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x014a, 0x03))
227625bded7cSSeth Forshee 		goto error;
227725bded7cSSeth Forshee 
227825bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0161, 0x03))
227925bded7cSSeth Forshee 		goto error;
228025bded7cSSeth Forshee 
228125bded7cSSeth Forshee 	alps_exit_command_mode(psmouse);
228225bded7cSSeth Forshee 
228325bded7cSSeth Forshee 	/*
228425bded7cSSeth Forshee 	 * This sequence changes the output from a 9-byte to an
228525bded7cSSeth Forshee 	 * 8-byte format. All the same data seems to be present,
228625bded7cSSeth Forshee 	 * just in a more compact format.
228725bded7cSSeth Forshee 	 */
228825bded7cSSeth Forshee 	param[0] = 0xc8;
228925bded7cSSeth Forshee 	param[1] = 0x64;
229025bded7cSSeth Forshee 	param[2] = 0x50;
229125bded7cSSeth Forshee 	if (ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
229225bded7cSSeth Forshee 	    ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE) ||
229325bded7cSSeth Forshee 	    ps2_command(ps2dev, &param[2], PSMOUSE_CMD_SETRATE) ||
229425bded7cSSeth Forshee 	    ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
229525bded7cSSeth Forshee 		return -1;
229625bded7cSSeth Forshee 
229725bded7cSSeth Forshee 	/* Set rate and enable data reporting */
229825bded7cSSeth Forshee 	param[0] = 0x64;
229925bded7cSSeth Forshee 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
230025bded7cSSeth Forshee 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
230125bded7cSSeth Forshee 		psmouse_err(psmouse, "Failed to enable data reporting\n");
230225bded7cSSeth Forshee 		return -1;
230325bded7cSSeth Forshee 	}
230425bded7cSSeth Forshee 
230525bded7cSSeth Forshee 	return 0;
230625bded7cSSeth Forshee 
230725bded7cSSeth Forshee error:
230825bded7cSSeth Forshee 	/*
230925bded7cSSeth Forshee 	 * Leaving the touchpad in command mode will essentially render
231025bded7cSSeth Forshee 	 * it unusable until the machine reboots, so exit it here just
231125bded7cSSeth Forshee 	 * to be safe
231225bded7cSSeth Forshee 	 */
231325bded7cSSeth Forshee 	alps_exit_command_mode(psmouse);
231425bded7cSSeth Forshee 	return -1;
231525bded7cSSeth Forshee }
231625bded7cSSeth Forshee 
23173db5b9f7SMasaki Ota static int alps_get_otp_values_ss4_v2(struct psmouse *psmouse,
23183db5b9f7SMasaki Ota 				      unsigned char index, unsigned char otp[])
23193db5b9f7SMasaki Ota {
23203db5b9f7SMasaki Ota 	struct ps2dev *ps2dev = &psmouse->ps2dev;
23213db5b9f7SMasaki Ota 
23223db5b9f7SMasaki Ota 	switch (index) {
23233db5b9f7SMasaki Ota 	case 0:
23243db5b9f7SMasaki Ota 		if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)  ||
23253db5b9f7SMasaki Ota 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)  ||
23263db5b9f7SMasaki Ota 		    ps2_command(ps2dev, otp, PSMOUSE_CMD_GETINFO))
23273db5b9f7SMasaki Ota 			return -1;
23283db5b9f7SMasaki Ota 
23293db5b9f7SMasaki Ota 		break;
23303db5b9f7SMasaki Ota 
23313db5b9f7SMasaki Ota 	case 1:
23323db5b9f7SMasaki Ota 		if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL)  ||
23333db5b9f7SMasaki Ota 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL)  ||
23343db5b9f7SMasaki Ota 		    ps2_command(ps2dev, otp, PSMOUSE_CMD_GETINFO))
23353db5b9f7SMasaki Ota 			return -1;
23363db5b9f7SMasaki Ota 
23373db5b9f7SMasaki Ota 		break;
23383db5b9f7SMasaki Ota 	}
23393db5b9f7SMasaki Ota 
23403db5b9f7SMasaki Ota 	return 0;
23413db5b9f7SMasaki Ota }
23423db5b9f7SMasaki Ota 
234307f19e3dSFengguang Wu static int alps_update_device_area_ss4_v2(unsigned char otp[][4],
23443db5b9f7SMasaki Ota 					  struct alps_data *priv)
23453db5b9f7SMasaki Ota {
23463db5b9f7SMasaki Ota 	int num_x_electrode;
23473db5b9f7SMasaki Ota 	int num_y_electrode;
23483db5b9f7SMasaki Ota 	int x_pitch, y_pitch, x_phys, y_phys;
23493db5b9f7SMasaki Ota 
23503db5b9f7SMasaki Ota 	num_x_electrode = SS4_NUMSENSOR_XOFFSET + (otp[1][0] & 0x0F);
23513db5b9f7SMasaki Ota 	num_y_electrode = SS4_NUMSENSOR_YOFFSET + ((otp[1][0] >> 4) & 0x0F);
23523db5b9f7SMasaki Ota 
23533db5b9f7SMasaki Ota 	priv->x_max = (num_x_electrode - 1) * SS4_COUNT_PER_ELECTRODE;
23543db5b9f7SMasaki Ota 	priv->y_max = (num_y_electrode - 1) * SS4_COUNT_PER_ELECTRODE;
23553db5b9f7SMasaki Ota 
23563db5b9f7SMasaki Ota 	x_pitch = ((otp[1][2] >> 2) & 0x07) + SS4_MIN_PITCH_MM;
23573db5b9f7SMasaki Ota 	y_pitch = ((otp[1][2] >> 5) & 0x07) + SS4_MIN_PITCH_MM;
23583db5b9f7SMasaki Ota 
23593db5b9f7SMasaki Ota 	x_phys = x_pitch * (num_x_electrode - 1); /* In 0.1 mm units */
23603db5b9f7SMasaki Ota 	y_phys = y_pitch * (num_y_electrode - 1); /* In 0.1 mm units */
23613db5b9f7SMasaki Ota 
23623db5b9f7SMasaki Ota 	priv->x_res = priv->x_max * 10 / x_phys; /* units / mm */
23633db5b9f7SMasaki Ota 	priv->y_res = priv->y_max * 10 / y_phys; /* units / mm */
23643db5b9f7SMasaki Ota 
23653db5b9f7SMasaki Ota 	return 0;
23663db5b9f7SMasaki Ota }
23673db5b9f7SMasaki Ota 
236807f19e3dSFengguang Wu static int alps_update_btn_info_ss4_v2(unsigned char otp[][4],
236907f19e3dSFengguang Wu 				       struct alps_data *priv)
23703db5b9f7SMasaki Ota {
23713db5b9f7SMasaki Ota 	unsigned char is_btnless;
23723db5b9f7SMasaki Ota 
23733db5b9f7SMasaki Ota 	is_btnless = (otp[1][1] >> 3) & 0x01;
23743db5b9f7SMasaki Ota 
23753db5b9f7SMasaki Ota 	if (is_btnless)
23763db5b9f7SMasaki Ota 		priv->flags |= ALPS_BUTTONPAD;
23773db5b9f7SMasaki Ota 
23783db5b9f7SMasaki Ota 	return 0;
23793db5b9f7SMasaki Ota }
23803db5b9f7SMasaki Ota 
23813db5b9f7SMasaki Ota static int alps_set_defaults_ss4_v2(struct psmouse *psmouse,
23823db5b9f7SMasaki Ota 				    struct alps_data *priv)
23833db5b9f7SMasaki Ota {
23843db5b9f7SMasaki Ota 	unsigned char otp[2][4];
23853db5b9f7SMasaki Ota 
23863db5b9f7SMasaki Ota 	memset(otp, 0, sizeof(otp));
23873db5b9f7SMasaki Ota 
23883db5b9f7SMasaki Ota 	if (alps_get_otp_values_ss4_v2(psmouse, 0, &otp[0][0]) ||
23893db5b9f7SMasaki Ota 	    alps_get_otp_values_ss4_v2(psmouse, 1, &otp[1][0]))
23903db5b9f7SMasaki Ota 		return -1;
23913db5b9f7SMasaki Ota 
23923db5b9f7SMasaki Ota 	alps_update_device_area_ss4_v2(otp, priv);
23933db5b9f7SMasaki Ota 
23943db5b9f7SMasaki Ota 	alps_update_btn_info_ss4_v2(otp, priv);
23953db5b9f7SMasaki Ota 
23963db5b9f7SMasaki Ota 	return 0;
23973db5b9f7SMasaki Ota }
23983db5b9f7SMasaki Ota 
2399ee65d4b3SYunkang Tang static int alps_dolphin_get_device_area(struct psmouse *psmouse,
2400ee65d4b3SYunkang Tang 					struct alps_data *priv)
2401ee65d4b3SYunkang Tang {
2402ee65d4b3SYunkang Tang 	struct ps2dev *ps2dev = &psmouse->ps2dev;
2403ee65d4b3SYunkang Tang 	unsigned char param[4] = {0};
2404ee65d4b3SYunkang Tang 	int num_x_electrode, num_y_electrode;
2405ee65d4b3SYunkang Tang 
2406ee65d4b3SYunkang Tang 	if (alps_enter_command_mode(psmouse))
2407ee65d4b3SYunkang Tang 		return -1;
2408ee65d4b3SYunkang Tang 
2409ee65d4b3SYunkang Tang 	param[0] = 0x0a;
2410ee65d4b3SYunkang Tang 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
2411ee65d4b3SYunkang Tang 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2412ee65d4b3SYunkang Tang 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2413ee65d4b3SYunkang Tang 	    ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2414ee65d4b3SYunkang Tang 	    ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE))
2415ee65d4b3SYunkang Tang 		return -1;
2416ee65d4b3SYunkang Tang 
2417ee65d4b3SYunkang Tang 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
2418ee65d4b3SYunkang Tang 		return -1;
2419ee65d4b3SYunkang Tang 
2420ee65d4b3SYunkang Tang 	/*
2421ee65d4b3SYunkang Tang 	 * Dolphin's sensor line number is not fixed. It can be calculated
2422ee65d4b3SYunkang Tang 	 * by adding the device's register value with DOLPHIN_PROFILE_X/YOFFSET.
2423ee65d4b3SYunkang Tang 	 * Further more, we can get device's x_max and y_max by multiplying
2424ee65d4b3SYunkang Tang 	 * sensor line number with DOLPHIN_COUNT_PER_ELECTRODE.
2425ee65d4b3SYunkang Tang 	 *
2426ee65d4b3SYunkang Tang 	 * e.g. When we get register's sensor_x = 11 & sensor_y = 8,
2427ee65d4b3SYunkang Tang 	 *	real sensor line number X = 11 + 8 = 19, and
2428ee65d4b3SYunkang Tang 	 *	real sensor line number Y = 8 + 1 = 9.
2429ee65d4b3SYunkang Tang 	 *	So, x_max = (19 - 1) * 64 = 1152, and
2430ee65d4b3SYunkang Tang 	 *	    y_max = (9 - 1) * 64 = 512.
2431ee65d4b3SYunkang Tang 	 */
2432ee65d4b3SYunkang Tang 	num_x_electrode = DOLPHIN_PROFILE_XOFFSET + (param[2] & 0x0F);
2433ee65d4b3SYunkang Tang 	num_y_electrode = DOLPHIN_PROFILE_YOFFSET + ((param[2] >> 4) & 0x0F);
2434ee65d4b3SYunkang Tang 	priv->x_bits = num_x_electrode;
2435ee65d4b3SYunkang Tang 	priv->y_bits = num_y_electrode;
2436ee65d4b3SYunkang Tang 	priv->x_max = (num_x_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
2437ee65d4b3SYunkang Tang 	priv->y_max = (num_y_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
2438ee65d4b3SYunkang Tang 
2439ee65d4b3SYunkang Tang 	if (alps_exit_command_mode(psmouse))
2440ee65d4b3SYunkang Tang 		return -1;
2441ee65d4b3SYunkang Tang 
2442ee65d4b3SYunkang Tang 	return 0;
2443ee65d4b3SYunkang Tang }
2444ee65d4b3SYunkang Tang 
244575af9e56SDave Turvene static int alps_hw_init_dolphin_v1(struct psmouse *psmouse)
244675af9e56SDave Turvene {
244775af9e56SDave Turvene 	struct ps2dev *ps2dev = &psmouse->ps2dev;
244875af9e56SDave Turvene 	unsigned char param[2];
244975af9e56SDave Turvene 
245075af9e56SDave Turvene 	/* This is dolphin "v1" as empirically defined by florin9doi */
245175af9e56SDave Turvene 	param[0] = 0x64;
245275af9e56SDave Turvene 	param[1] = 0x28;
245375af9e56SDave Turvene 
245475af9e56SDave Turvene 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
245575af9e56SDave Turvene 	    ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
245675af9e56SDave Turvene 	    ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
245775af9e56SDave Turvene 		return -1;
245875af9e56SDave Turvene 
245975af9e56SDave Turvene 	return 0;
246075af9e56SDave Turvene }
246175af9e56SDave Turvene 
24623808843cSYunkang Tang static int alps_hw_init_v7(struct psmouse *psmouse)
24633808843cSYunkang Tang {
24643808843cSYunkang Tang 	struct ps2dev *ps2dev = &psmouse->ps2dev;
24653808843cSYunkang Tang 	int reg_val, ret = -1;
24663808843cSYunkang Tang 
24673808843cSYunkang Tang 	if (alps_enter_command_mode(psmouse) ||
24683808843cSYunkang Tang 	    alps_command_mode_read_reg(psmouse, 0xc2d9) == -1)
24693808843cSYunkang Tang 		goto error;
24703808843cSYunkang Tang 
2471f3f33c67SHans de Goede 	if (alps_get_v3_v7_resolution(psmouse, 0xc397))
2472f3f33c67SHans de Goede 		goto error;
2473f3f33c67SHans de Goede 
24743808843cSYunkang Tang 	if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
24753808843cSYunkang Tang 		goto error;
24763808843cSYunkang Tang 
24773808843cSYunkang Tang 	reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
24783808843cSYunkang Tang 	if (reg_val == -1)
24793808843cSYunkang Tang 		goto error;
24803808843cSYunkang Tang 	if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
24813808843cSYunkang Tang 		goto error;
24823808843cSYunkang Tang 
24833808843cSYunkang Tang 	alps_exit_command_mode(psmouse);
24843808843cSYunkang Tang 	return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
24853808843cSYunkang Tang 
24863808843cSYunkang Tang error:
24873808843cSYunkang Tang 	alps_exit_command_mode(psmouse);
24883808843cSYunkang Tang 	return ret;
24893808843cSYunkang Tang }
24903808843cSYunkang Tang 
24913db5b9f7SMasaki Ota static int alps_hw_init_ss4_v2(struct psmouse *psmouse)
24923db5b9f7SMasaki Ota {
24933db5b9f7SMasaki Ota 	struct ps2dev *ps2dev = &psmouse->ps2dev;
24943db5b9f7SMasaki Ota 	char param[2] = {0x64, 0x28};
24953db5b9f7SMasaki Ota 	int ret = -1;
24963db5b9f7SMasaki Ota 
24973db5b9f7SMasaki Ota 	/* enter absolute mode */
24983db5b9f7SMasaki Ota 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
24993db5b9f7SMasaki Ota 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
25003db5b9f7SMasaki Ota 	    ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
25013db5b9f7SMasaki Ota 	    ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE)) {
25023db5b9f7SMasaki Ota 		goto error;
25033db5b9f7SMasaki Ota 	}
25043db5b9f7SMasaki Ota 
25053db5b9f7SMasaki Ota 	/* T.B.D. Decread noise packet number, delete in the future */
25063db5b9f7SMasaki Ota 	if (alps_exit_command_mode(psmouse) ||
25073db5b9f7SMasaki Ota 	    alps_enter_command_mode(psmouse) ||
25083db5b9f7SMasaki Ota 	    alps_command_mode_write_reg(psmouse, 0x001D, 0x20)) {
25093db5b9f7SMasaki Ota 		goto error;
25103db5b9f7SMasaki Ota 	}
25113db5b9f7SMasaki Ota 	alps_exit_command_mode(psmouse);
25123db5b9f7SMasaki Ota 
25133db5b9f7SMasaki Ota 	return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
25143db5b9f7SMasaki Ota 
25153db5b9f7SMasaki Ota error:
25163db5b9f7SMasaki Ota 	alps_exit_command_mode(psmouse);
25173db5b9f7SMasaki Ota 	return ret;
25183db5b9f7SMasaki Ota }
25193db5b9f7SMasaki Ota 
25203296f71cSDmitry Torokhov static int alps_set_protocol(struct psmouse *psmouse,
25213296f71cSDmitry Torokhov 			     struct alps_data *priv,
25223296f71cSDmitry Torokhov 			     const struct alps_protocol_info *protocol)
252325bded7cSSeth Forshee {
25243296f71cSDmitry Torokhov 	psmouse->private = priv;
25253296f71cSDmitry Torokhov 
25263296f71cSDmitry Torokhov 	setup_timer(&priv->timer, alps_flush_packet, (unsigned long)psmouse);
25273296f71cSDmitry Torokhov 
25283296f71cSDmitry Torokhov 	priv->proto_version = protocol->version;
25293296f71cSDmitry Torokhov 	priv->byte0 = protocol->byte0;
25303296f71cSDmitry Torokhov 	priv->mask0 = protocol->mask0;
25313296f71cSDmitry Torokhov 	priv->flags = protocol->flags;
2532f673ceb1SKevin Cernekee 
25337a9f73e7SKevin Cernekee 	priv->x_max = 2000;
25347a9f73e7SKevin Cernekee 	priv->y_max = 1400;
25357a9f73e7SKevin Cernekee 	priv->x_bits = 15;
25367a9f73e7SKevin Cernekee 	priv->y_bits = 11;
25377a9f73e7SKevin Cernekee 
253899df65e7SKevin Cernekee 	switch (priv->proto_version) {
253925bded7cSSeth Forshee 	case ALPS_PROTO_V1:
254025bded7cSSeth Forshee 	case ALPS_PROTO_V2:
254124af5cb9SKevin Cernekee 		priv->hw_init = alps_hw_init_v1_v2;
254224af5cb9SKevin Cernekee 		priv->process_packet = alps_process_packet_v1_v2;
254324af5cb9SKevin Cernekee 		priv->set_abs_params = alps_set_abs_params_st;
254495f75e91SYunkang Tang 		priv->x_max = 1023;
254595f75e91SYunkang Tang 		priv->y_max = 767;
254625bded7cSSeth Forshee 		break;
2547fb2dd7a6SDmitry Torokhov 
254825bded7cSSeth Forshee 	case ALPS_PROTO_V3:
254924af5cb9SKevin Cernekee 		priv->hw_init = alps_hw_init_v3;
255024af5cb9SKevin Cernekee 		priv->process_packet = alps_process_packet_v3;
255124af5cb9SKevin Cernekee 		priv->set_abs_params = alps_set_abs_params_mt;
2552f85e5001SKevin Cernekee 		priv->decode_fields = alps_decode_pinnacle;
255350e8b216SKevin Cernekee 		priv->nibble_commands = alps_v3_nibble_commands;
255450e8b216SKevin Cernekee 		priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
255525bded7cSSeth Forshee 		break;
2556fb2dd7a6SDmitry Torokhov 
2557fb2dd7a6SDmitry Torokhov 	case ALPS_PROTO_V3_RUSHMORE:
2558fb2dd7a6SDmitry Torokhov 		priv->hw_init = alps_hw_init_rushmore_v3;
2559fb2dd7a6SDmitry Torokhov 		priv->process_packet = alps_process_packet_v3;
2560fb2dd7a6SDmitry Torokhov 		priv->set_abs_params = alps_set_abs_params_mt;
2561fb2dd7a6SDmitry Torokhov 		priv->decode_fields = alps_decode_rushmore;
2562fb2dd7a6SDmitry Torokhov 		priv->nibble_commands = alps_v3_nibble_commands;
2563fb2dd7a6SDmitry Torokhov 		priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2564fb2dd7a6SDmitry Torokhov 		priv->x_bits = 16;
2565fb2dd7a6SDmitry Torokhov 		priv->y_bits = 12;
25663296f71cSDmitry Torokhov 
25673296f71cSDmitry Torokhov 		if (alps_probe_trackstick_v3(psmouse,
25683296f71cSDmitry Torokhov 					     ALPS_REG_BASE_RUSHMORE) < 0)
25693296f71cSDmitry Torokhov 			priv->flags &= ~ALPS_DUALPOINT;
25703296f71cSDmitry Torokhov 
2571fb2dd7a6SDmitry Torokhov 		break;
2572fb2dd7a6SDmitry Torokhov 
257325bded7cSSeth Forshee 	case ALPS_PROTO_V4:
257424af5cb9SKevin Cernekee 		priv->hw_init = alps_hw_init_v4;
257524af5cb9SKevin Cernekee 		priv->process_packet = alps_process_packet_v4;
257624af5cb9SKevin Cernekee 		priv->set_abs_params = alps_set_abs_params_mt;
257750e8b216SKevin Cernekee 		priv->nibble_commands = alps_v4_nibble_commands;
257850e8b216SKevin Cernekee 		priv->addr_command = PSMOUSE_CMD_DISABLE;
257925bded7cSSeth Forshee 		break;
25803296f71cSDmitry Torokhov 
258175af9e56SDave Turvene 	case ALPS_PROTO_V5:
258275af9e56SDave Turvene 		priv->hw_init = alps_hw_init_dolphin_v1;
2583ee65d4b3SYunkang Tang 		priv->process_packet = alps_process_touchpad_packet_v3_v5;
258475af9e56SDave Turvene 		priv->decode_fields = alps_decode_dolphin;
258575af9e56SDave Turvene 		priv->set_abs_params = alps_set_abs_params_mt;
258675af9e56SDave Turvene 		priv->nibble_commands = alps_v3_nibble_commands;
258775af9e56SDave Turvene 		priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
258875af9e56SDave Turvene 		priv->x_bits = 23;
258975af9e56SDave Turvene 		priv->y_bits = 12;
2590c164c147SDmitry Torokhov 
2591c164c147SDmitry Torokhov 		if (alps_dolphin_get_device_area(psmouse, priv))
2592c164c147SDmitry Torokhov 			return -EIO;
2593c164c147SDmitry Torokhov 
259475af9e56SDave Turvene 		break;
25953296f71cSDmitry Torokhov 
259695f75e91SYunkang Tang 	case ALPS_PROTO_V6:
259795f75e91SYunkang Tang 		priv->hw_init = alps_hw_init_v6;
259895f75e91SYunkang Tang 		priv->process_packet = alps_process_packet_v6;
259995f75e91SYunkang Tang 		priv->set_abs_params = alps_set_abs_params_st;
260095f75e91SYunkang Tang 		priv->nibble_commands = alps_v6_nibble_commands;
260195f75e91SYunkang Tang 		priv->x_max = 2047;
260295f75e91SYunkang Tang 		priv->y_max = 1535;
260395f75e91SYunkang Tang 		break;
26043296f71cSDmitry Torokhov 
26053808843cSYunkang Tang 	case ALPS_PROTO_V7:
26063808843cSYunkang Tang 		priv->hw_init = alps_hw_init_v7;
26073808843cSYunkang Tang 		priv->process_packet = alps_process_packet_v7;
26083808843cSYunkang Tang 		priv->decode_fields = alps_decode_packet_v7;
26098eccd393SMasaki Ota 		priv->set_abs_params = alps_set_abs_params_v7;
26103808843cSYunkang Tang 		priv->nibble_commands = alps_v3_nibble_commands;
26113808843cSYunkang Tang 		priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2612c164c147SDmitry Torokhov 		priv->x_max = 0xfff;
2613c164c147SDmitry Torokhov 		priv->y_max = 0x7ff;
26143808843cSYunkang Tang 
26153808843cSYunkang Tang 		if (priv->fw_ver[1] != 0xba)
26163808843cSYunkang Tang 			priv->flags |= ALPS_BUTTONPAD;
26173296f71cSDmitry Torokhov 
26183808843cSYunkang Tang 		break;
26193db5b9f7SMasaki Ota 
26203db5b9f7SMasaki Ota 	case ALPS_PROTO_V8:
26213db5b9f7SMasaki Ota 		priv->hw_init = alps_hw_init_ss4_v2;
26223db5b9f7SMasaki Ota 		priv->process_packet = alps_process_packet_ss4_v2;
26233db5b9f7SMasaki Ota 		priv->decode_fields = alps_decode_ss4_v2;
26243db5b9f7SMasaki Ota 		priv->set_abs_params = alps_set_abs_params_ss4_v2;
26253db5b9f7SMasaki Ota 		priv->nibble_commands = alps_v3_nibble_commands;
26263db5b9f7SMasaki Ota 		priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
26273db5b9f7SMasaki Ota 
26283db5b9f7SMasaki Ota 		if (alps_set_defaults_ss4_v2(psmouse, priv))
26293db5b9f7SMasaki Ota 			return -EIO;
26303db5b9f7SMasaki Ota 
26313db5b9f7SMasaki Ota 		break;
263225bded7cSSeth Forshee 	}
26333296f71cSDmitry Torokhov 
26343296f71cSDmitry Torokhov 	return 0;
263525bded7cSSeth Forshee }
263625bded7cSSeth Forshee 
26373296f71cSDmitry Torokhov static const struct alps_protocol_info *alps_match_table(unsigned char *e7,
26383296f71cSDmitry Torokhov 							 unsigned char *ec)
26392e992cc0SKevin Cernekee {
2640b5d6b851SKevin Cernekee 	const struct alps_model_info *model;
26412e992cc0SKevin Cernekee 	int i;
26422e992cc0SKevin Cernekee 
2643b5d6b851SKevin Cernekee 	for (i = 0; i < ARRAY_SIZE(alps_model_data); i++) {
2644b5d6b851SKevin Cernekee 		model = &alps_model_data[i];
2645b5d6b851SKevin Cernekee 
2646b5d6b851SKevin Cernekee 		if (!memcmp(e7, model->signature, sizeof(model->signature)) &&
2647b5d6b851SKevin Cernekee 		    (!model->command_mode_resp ||
2648b5d6b851SKevin Cernekee 		     model->command_mode_resp == ec[2])) {
2649b5d6b851SKevin Cernekee 
26503296f71cSDmitry Torokhov 			return &model->protocol_info;
2651b5d6b851SKevin Cernekee 		}
2652b5d6b851SKevin Cernekee 	}
2653b5d6b851SKevin Cernekee 
26543296f71cSDmitry Torokhov 	return NULL;
2655b5d6b851SKevin Cernekee }
2656b5d6b851SKevin Cernekee 
2657b5d6b851SKevin Cernekee static int alps_identify(struct psmouse *psmouse, struct alps_data *priv)
2658b5d6b851SKevin Cernekee {
26593296f71cSDmitry Torokhov 	const struct alps_protocol_info *protocol;
2660b5d6b851SKevin Cernekee 	unsigned char e6[4], e7[4], ec[4];
2661a09221e8SDmitry Torokhov 	int error;
2662b5d6b851SKevin Cernekee 
26632e992cc0SKevin Cernekee 	/*
26642e992cc0SKevin Cernekee 	 * First try "E6 report".
26652e992cc0SKevin Cernekee 	 * ALPS should return 0,0,10 or 0,0,100 if no buttons are pressed.
26662e992cc0SKevin Cernekee 	 * The bits 0-2 of the first byte will be 1s if some buttons are
26672e992cc0SKevin Cernekee 	 * pressed.
26682e992cc0SKevin Cernekee 	 */
2669b5d6b851SKevin Cernekee 	if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2670b5d6b851SKevin Cernekee 			 PSMOUSE_CMD_SETSCALE11, e6))
2671b5d6b851SKevin Cernekee 		return -EIO;
26722e992cc0SKevin Cernekee 
2673b5d6b851SKevin Cernekee 	if ((e6[0] & 0xf8) != 0 || e6[1] != 0 || (e6[2] != 10 && e6[2] != 100))
2674b5d6b851SKevin Cernekee 		return -EINVAL;
26752e992cc0SKevin Cernekee 
26762e992cc0SKevin Cernekee 	/*
2677b5d6b851SKevin Cernekee 	 * Now get the "E7" and "EC" reports.  These will uniquely identify
2678b5d6b851SKevin Cernekee 	 * most ALPS touchpads.
26792e992cc0SKevin Cernekee 	 */
2680b5d6b851SKevin Cernekee 	if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2681b5d6b851SKevin Cernekee 			 PSMOUSE_CMD_SETSCALE21, e7) ||
2682b5d6b851SKevin Cernekee 	    alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2683b5d6b851SKevin Cernekee 			 PSMOUSE_CMD_RESET_WRAP, ec) ||
2684b5d6b851SKevin Cernekee 	    alps_exit_command_mode(psmouse))
2685b5d6b851SKevin Cernekee 		return -EIO;
26862e992cc0SKevin Cernekee 
26873296f71cSDmitry Torokhov 	protocol = alps_match_table(e7, ec);
26883296f71cSDmitry Torokhov 	if (!protocol) {
26893296f71cSDmitry Torokhov 		if (e7[0] == 0x73 && e7[1] == 0x03 && e7[2] == 0x50 &&
26903296f71cSDmitry Torokhov 			   ec[0] == 0x73 && (ec[1] == 0x01 || ec[1] == 0x02)) {
26913296f71cSDmitry Torokhov 			protocol = &alps_v5_protocol_data;
26923296f71cSDmitry Torokhov 		} else if (ec[0] == 0x88 &&
26933296f71cSDmitry Torokhov 			   ((ec[1] & 0xf0) == 0xb0 || (ec[1] & 0xf0) == 0xc0)) {
26943296f71cSDmitry Torokhov 			protocol = &alps_v7_protocol_data;
26953296f71cSDmitry Torokhov 		} else if (ec[0] == 0x88 && ec[1] == 0x08) {
26963296f71cSDmitry Torokhov 			protocol = &alps_v3_rushmore_data;
26973296f71cSDmitry Torokhov 		} else if (ec[0] == 0x88 && ec[1] == 0x07 &&
26983296f71cSDmitry Torokhov 			   ec[2] >= 0x90 && ec[2] <= 0x9d) {
26993296f71cSDmitry Torokhov 			protocol = &alps_v3_protocol_data;
27003db5b9f7SMasaki Ota 		} else if (e7[0] == 0x73 && e7[1] == 0x03 &&
27013db5b9f7SMasaki Ota 			   e7[2] == 0x14 && ec[1] == 0x02) {
27023db5b9f7SMasaki Ota 			protocol = &alps_v8_protocol_data;
27033296f71cSDmitry Torokhov 		} else {
27043296f71cSDmitry Torokhov 			psmouse_dbg(psmouse,
27053296f71cSDmitry Torokhov 				    "Likely not an ALPS touchpad: E7=%3ph, EC=%3ph\n", e7, ec);
27063296f71cSDmitry Torokhov 			return -EINVAL;
27073296f71cSDmitry Torokhov 		}
27083296f71cSDmitry Torokhov 	}
27093296f71cSDmitry Torokhov 
2710a09221e8SDmitry Torokhov 	if (priv) {
2711c0cd17f6SHans de Goede 		/* Save the Firmware version */
2712c0cd17f6SHans de Goede 		memcpy(priv->fw_ver, ec, 3);
2713a09221e8SDmitry Torokhov 		error = alps_set_protocol(psmouse, priv, protocol);
2714a09221e8SDmitry Torokhov 		if (error)
2715a09221e8SDmitry Torokhov 			return error;
2716a09221e8SDmitry Torokhov 	}
2717c0cd17f6SHans de Goede 
2718a09221e8SDmitry Torokhov 	return 0;
27192e992cc0SKevin Cernekee }
27202e992cc0SKevin Cernekee 
27211e0c5b12SDmitry Torokhov static int alps_reconnect(struct psmouse *psmouse)
27221e0c5b12SDmitry Torokhov {
2723b5d6b851SKevin Cernekee 	struct alps_data *priv = psmouse->private;
272471bb21b6SMaxim Levitsky 
27251e0c5b12SDmitry Torokhov 	psmouse_reset(psmouse);
27261e0c5b12SDmitry Torokhov 
2727b5d6b851SKevin Cernekee 	if (alps_identify(psmouse, priv) < 0)
27281e0c5b12SDmitry Torokhov 		return -1;
27291e0c5b12SDmitry Torokhov 
273024af5cb9SKevin Cernekee 	return priv->hw_init(psmouse);
27311da177e4SLinus Torvalds }
27321da177e4SLinus Torvalds 
27331da177e4SLinus Torvalds static void alps_disconnect(struct psmouse *psmouse)
27341da177e4SLinus Torvalds {
27351da177e4SLinus Torvalds 	struct alps_data *priv = psmouse->private;
27362e5b636bSDmitry Torokhov 
27371da177e4SLinus Torvalds 	psmouse_reset(psmouse);
27381d9f2626SSebastian Kapfer 	del_timer_sync(&priv->timer);
273904aae283SPali Rohár 	if (priv->dev2)
27402e5b636bSDmitry Torokhov 		input_unregister_device(priv->dev2);
274104aae283SPali Rohár 	if (!IS_ERR_OR_NULL(priv->dev3))
274204aae283SPali Rohár 		input_unregister_device(priv->dev3);
27431da177e4SLinus Torvalds 	kfree(priv);
27441da177e4SLinus Torvalds }
27451da177e4SLinus Torvalds 
274624af5cb9SKevin Cernekee static void alps_set_abs_params_st(struct alps_data *priv,
274724af5cb9SKevin Cernekee 				   struct input_dev *dev1)
274824af5cb9SKevin Cernekee {
274995f75e91SYunkang Tang 	input_set_abs_params(dev1, ABS_X, 0, priv->x_max, 0, 0);
275095f75e91SYunkang Tang 	input_set_abs_params(dev1, ABS_Y, 0, priv->y_max, 0, 0);
27518eccd393SMasaki Ota 	input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
275224af5cb9SKevin Cernekee }
275324af5cb9SKevin Cernekee 
27548eccd393SMasaki Ota static void alps_set_abs_params_mt_common(struct alps_data *priv,
275524af5cb9SKevin Cernekee 					  struct input_dev *dev1)
275624af5cb9SKevin Cernekee {
27577a9f73e7SKevin Cernekee 	input_set_abs_params(dev1, ABS_MT_POSITION_X, 0, priv->x_max, 0, 0);
27587a9f73e7SKevin Cernekee 	input_set_abs_params(dev1, ABS_MT_POSITION_Y, 0, priv->y_max, 0, 0);
275924af5cb9SKevin Cernekee 
2760f3f33c67SHans de Goede 	input_abs_set_res(dev1, ABS_MT_POSITION_X, priv->x_res);
2761f3f33c67SHans de Goede 	input_abs_set_res(dev1, ABS_MT_POSITION_Y, priv->y_res);
2762f3f33c67SHans de Goede 
276324af5cb9SKevin Cernekee 	set_bit(BTN_TOOL_TRIPLETAP, dev1->keybit);
276424af5cb9SKevin Cernekee 	set_bit(BTN_TOOL_QUADTAP, dev1->keybit);
27658eccd393SMasaki Ota }
27663808843cSYunkang Tang 
27678eccd393SMasaki Ota static void alps_set_abs_params_mt(struct alps_data *priv,
27688eccd393SMasaki Ota 				   struct input_dev *dev1)
27698eccd393SMasaki Ota {
27708eccd393SMasaki Ota 	alps_set_abs_params_mt_common(priv, dev1);
27718eccd393SMasaki Ota 	input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
27728eccd393SMasaki Ota 
27738eccd393SMasaki Ota 	input_mt_init_slots(dev1, MAX_TOUCHES,
27748eccd393SMasaki Ota 			    INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
27758eccd393SMasaki Ota 				INPUT_MT_TRACK | INPUT_MT_SEMI_MT);
27768eccd393SMasaki Ota }
27778eccd393SMasaki Ota 
27788eccd393SMasaki Ota static void alps_set_abs_params_v7(struct alps_data *priv,
27798eccd393SMasaki Ota 				   struct input_dev *dev1)
27808eccd393SMasaki Ota {
27818eccd393SMasaki Ota 	alps_set_abs_params_mt_common(priv, dev1);
27828d289842SMasaki Ota 	set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
27838eccd393SMasaki Ota 
27848eccd393SMasaki Ota 	input_mt_init_slots(dev1, MAX_TOUCHES,
27858eccd393SMasaki Ota 			    INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
27868eccd393SMasaki Ota 				INPUT_MT_TRACK);
27873db5b9f7SMasaki Ota 
27883db5b9f7SMasaki Ota 	set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
27893db5b9f7SMasaki Ota }
27903db5b9f7SMasaki Ota 
27913db5b9f7SMasaki Ota static void alps_set_abs_params_ss4_v2(struct alps_data *priv,
27923db5b9f7SMasaki Ota 				       struct input_dev *dev1)
27933db5b9f7SMasaki Ota {
27943db5b9f7SMasaki Ota 	alps_set_abs_params_mt_common(priv, dev1);
27953db5b9f7SMasaki Ota 	input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
27963db5b9f7SMasaki Ota 	set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
27973db5b9f7SMasaki Ota 
27983db5b9f7SMasaki Ota 	input_mt_init_slots(dev1, MAX_TOUCHES,
27993db5b9f7SMasaki Ota 			    INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
28003db5b9f7SMasaki Ota 				INPUT_MT_TRACK);
280124af5cb9SKevin Cernekee }
280224af5cb9SKevin Cernekee 
28031da177e4SLinus Torvalds int alps_init(struct psmouse *psmouse)
28041da177e4SLinus Torvalds {
2805a09221e8SDmitry Torokhov 	struct alps_data *priv = psmouse->private;
280604aae283SPali Rohár 	struct input_dev *dev1 = psmouse->dev;
2807a09221e8SDmitry Torokhov 	int error;
28081da177e4SLinus Torvalds 
2809a09221e8SDmitry Torokhov 	error = priv->hw_init(psmouse);
2810a09221e8SDmitry Torokhov 	if (error)
28111da177e4SLinus Torvalds 		goto init_fail;
28121da177e4SLinus Torvalds 
28137105d2eaSDmitry Torokhov 	/*
28147105d2eaSDmitry Torokhov 	 * Undo part of setup done for us by psmouse core since touchpad
28157105d2eaSDmitry Torokhov 	 * is not a relative device.
28167105d2eaSDmitry Torokhov 	 */
28177105d2eaSDmitry Torokhov 	__clear_bit(EV_REL, dev1->evbit);
28187105d2eaSDmitry Torokhov 	__clear_bit(REL_X, dev1->relbit);
28197105d2eaSDmitry Torokhov 	__clear_bit(REL_Y, dev1->relbit);
28207105d2eaSDmitry Torokhov 
28217105d2eaSDmitry Torokhov 	/*
28227105d2eaSDmitry Torokhov 	 * Now set up our capabilities.
28237105d2eaSDmitry Torokhov 	 */
28247b19ada2SJiri Slaby 	dev1->evbit[BIT_WORD(EV_KEY)] |= BIT_MASK(EV_KEY);
28257b19ada2SJiri Slaby 	dev1->keybit[BIT_WORD(BTN_TOUCH)] |= BIT_MASK(BTN_TOUCH);
28267b19ada2SJiri Slaby 	dev1->keybit[BIT_WORD(BTN_TOOL_FINGER)] |= BIT_MASK(BTN_TOOL_FINGER);
282771bb21b6SMaxim Levitsky 	dev1->keybit[BIT_WORD(BTN_LEFT)] |=
282871bb21b6SMaxim Levitsky 		BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
28291da177e4SLinus Torvalds 
28307b19ada2SJiri Slaby 	dev1->evbit[BIT_WORD(EV_ABS)] |= BIT_MASK(EV_ABS);
283125bded7cSSeth Forshee 
283224af5cb9SKevin Cernekee 	priv->set_abs_params(priv, dev1);
28331da177e4SLinus Torvalds 
283499df65e7SKevin Cernekee 	if (priv->flags & ALPS_WHEEL) {
28357b19ada2SJiri Slaby 		dev1->evbit[BIT_WORD(EV_REL)] |= BIT_MASK(EV_REL);
28367b19ada2SJiri Slaby 		dev1->relbit[BIT_WORD(REL_WHEEL)] |= BIT_MASK(REL_WHEEL);
28371da177e4SLinus Torvalds 	}
28381da177e4SLinus Torvalds 
283999df65e7SKevin Cernekee 	if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
28407b19ada2SJiri Slaby 		dev1->keybit[BIT_WORD(BTN_FORWARD)] |= BIT_MASK(BTN_FORWARD);
28417b19ada2SJiri Slaby 		dev1->keybit[BIT_WORD(BTN_BACK)] |= BIT_MASK(BTN_BACK);
28421da177e4SLinus Torvalds 	}
28431da177e4SLinus Torvalds 
284499df65e7SKevin Cernekee 	if (priv->flags & ALPS_FOUR_BUTTONS) {
284571bb21b6SMaxim Levitsky 		dev1->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_0);
284671bb21b6SMaxim Levitsky 		dev1->keybit[BIT_WORD(BTN_1)] |= BIT_MASK(BTN_1);
284771bb21b6SMaxim Levitsky 		dev1->keybit[BIT_WORD(BTN_2)] |= BIT_MASK(BTN_2);
284871bb21b6SMaxim Levitsky 		dev1->keybit[BIT_WORD(BTN_3)] |= BIT_MASK(BTN_3);
28493808843cSYunkang Tang 	} else if (priv->flags & ALPS_BUTTONPAD) {
28503808843cSYunkang Tang 		set_bit(INPUT_PROP_BUTTONPAD, dev1->propbit);
28513808843cSYunkang Tang 		clear_bit(BTN_RIGHT, dev1->keybit);
285271bb21b6SMaxim Levitsky 	} else {
285371bb21b6SMaxim Levitsky 		dev1->keybit[BIT_WORD(BTN_MIDDLE)] |= BIT_MASK(BTN_MIDDLE);
285471bb21b6SMaxim Levitsky 	}
285571bb21b6SMaxim Levitsky 
2856dfba8600SPali Rohár 	if (priv->flags & ALPS_DUALPOINT) {
285704aae283SPali Rohár 		struct input_dev *dev2;
285804aae283SPali Rohár 
285904aae283SPali Rohár 		dev2 = input_allocate_device();
286004aae283SPali Rohár 		if (!dev2) {
286104aae283SPali Rohár 			psmouse_err(psmouse,
286204aae283SPali Rohár 				    "failed to allocate trackstick device\n");
286304aae283SPali Rohár 			error = -ENOMEM;
286404aae283SPali Rohár 			goto init_fail;
286504aae283SPali Rohár 		}
286604aae283SPali Rohár 
286704aae283SPali Rohár 		snprintf(priv->phys2, sizeof(priv->phys2), "%s/input1",
286804aae283SPali Rohár 			 psmouse->ps2dev.serio->phys);
286904aae283SPali Rohár 		dev2->phys = priv->phys2;
287004aae283SPali Rohár 
2871dfba8600SPali Rohár 		/*
2872dfba8600SPali Rohár 		 * format of input device name is: "protocol vendor name"
2873dfba8600SPali Rohár 		 * see function psmouse_switch_protocol() in psmouse-base.c
2874dfba8600SPali Rohár 		 */
2875dfba8600SPali Rohár 		dev2->name = "AlpsPS/2 ALPS DualPoint Stick";
2876dfba8600SPali Rohár 
28772e5b636bSDmitry Torokhov 		dev2->id.bustype = BUS_I8042;
28782e5b636bSDmitry Torokhov 		dev2->id.vendor  = 0x0002;
287904aae283SPali Rohár 		dev2->id.product = PSMOUSE_ALPS;
288004aae283SPali Rohár 		dev2->id.version = priv->proto_version;
28811db3a345SDmitry Torokhov 		dev2->dev.parent = &psmouse->ps2dev.serio->dev;
28821da177e4SLinus Torvalds 
288304aae283SPali Rohár 		input_set_capability(dev2, EV_REL, REL_X);
288404aae283SPali Rohár 		input_set_capability(dev2, EV_REL, REL_Y);
288504aae283SPali Rohár 		input_set_capability(dev2, EV_KEY, BTN_LEFT);
288604aae283SPali Rohár 		input_set_capability(dev2, EV_KEY, BTN_RIGHT);
288704aae283SPali Rohár 		input_set_capability(dev2, EV_KEY, BTN_MIDDLE);
28881da177e4SLinus Torvalds 
288901d4cd5cSHans de Goede 		__set_bit(INPUT_PROP_POINTER, dev2->propbit);
28907611392fSHans de Goede 		__set_bit(INPUT_PROP_POINTING_STICK, dev2->propbit);
28917611392fSHans de Goede 
289204aae283SPali Rohár 		error = input_register_device(dev2);
289304aae283SPali Rohár 		if (error) {
289404aae283SPali Rohár 			psmouse_err(psmouse,
289504aae283SPali Rohár 				    "failed to register trackstick device: %d\n",
289604aae283SPali Rohár 				    error);
289704aae283SPali Rohár 			input_free_device(dev2);
2898f42649e8SDmitry Torokhov 			goto init_fail;
289904aae283SPali Rohár 		}
290004aae283SPali Rohár 
290104aae283SPali Rohár 		priv->dev2 = dev2;
290204aae283SPali Rohár 	}
290304aae283SPali Rohár 
290404aae283SPali Rohár 	priv->psmouse = psmouse;
290504aae283SPali Rohár 
290604aae283SPali Rohár 	INIT_DELAYED_WORK(&priv->dev3_register_work,
290704aae283SPali Rohár 			  alps_register_bare_ps2_mouse);
29081da177e4SLinus Torvalds 
29091da177e4SLinus Torvalds 	psmouse->protocol_handler = alps_process_byte;
2910f0d5c6f4SDmitry Torokhov 	psmouse->poll = alps_poll;
29111da177e4SLinus Torvalds 	psmouse->disconnect = alps_disconnect;
29121da177e4SLinus Torvalds 	psmouse->reconnect = alps_reconnect;
291399df65e7SKevin Cernekee 	psmouse->pktsize = priv->proto_version == ALPS_PROTO_V4 ? 8 : 6;
29141da177e4SLinus Torvalds 
2915f0d5c6f4SDmitry Torokhov 	/* We are having trouble resyncing ALPS touchpads so disable it for now */
2916f0d5c6f4SDmitry Torokhov 	psmouse->resync_time = 0;
2917f0d5c6f4SDmitry Torokhov 
29189d720b34SPali Rohár 	/* Allow 2 invalid packets without resetting device */
29199d720b34SPali Rohár 	psmouse->resetafter = psmouse->pktsize * 2;
29209d720b34SPali Rohár 
29211da177e4SLinus Torvalds 	return 0;
29221da177e4SLinus Torvalds 
29231da177e4SLinus Torvalds init_fail:
2924f42649e8SDmitry Torokhov 	psmouse_reset(psmouse);
2925a09221e8SDmitry Torokhov 	/*
2926a09221e8SDmitry Torokhov 	 * Even though we did not allocate psmouse->private we do free
2927a09221e8SDmitry Torokhov 	 * it here.
2928a09221e8SDmitry Torokhov 	 */
2929a09221e8SDmitry Torokhov 	kfree(psmouse->private);
29301e0c5b12SDmitry Torokhov 	psmouse->private = NULL;
2931a09221e8SDmitry Torokhov 	return error;
29321da177e4SLinus Torvalds }
29331da177e4SLinus Torvalds 
2934b7802c5cSDmitry Torokhov int alps_detect(struct psmouse *psmouse, bool set_properties)
29351da177e4SLinus Torvalds {
2936a09221e8SDmitry Torokhov 	struct alps_data *priv;
2937a09221e8SDmitry Torokhov 	int error;
29381da177e4SLinus Torvalds 
2939a09221e8SDmitry Torokhov 	error = alps_identify(psmouse, NULL);
2940a09221e8SDmitry Torokhov 	if (error)
2941a09221e8SDmitry Torokhov 		return error;
2942a09221e8SDmitry Torokhov 
2943a09221e8SDmitry Torokhov 	/*
2944a09221e8SDmitry Torokhov 	 * Reset the device to make sure it is fully operational:
2945a09221e8SDmitry Torokhov 	 * on some laptops, like certain Dell Latitudes, we may
2946a09221e8SDmitry Torokhov 	 * fail to properly detect presence of trackstick if device
2947a09221e8SDmitry Torokhov 	 * has not been reset.
2948a09221e8SDmitry Torokhov 	 */
2949a09221e8SDmitry Torokhov 	psmouse_reset(psmouse);
2950a09221e8SDmitry Torokhov 
2951a09221e8SDmitry Torokhov 	priv = kzalloc(sizeof(struct alps_data), GFP_KERNEL);
2952a09221e8SDmitry Torokhov 	if (!priv)
2953a09221e8SDmitry Torokhov 		return -ENOMEM;
2954a09221e8SDmitry Torokhov 
2955a09221e8SDmitry Torokhov 	error = alps_identify(psmouse, priv);
295693050db2SDmitry Torokhov 	if (error) {
295793050db2SDmitry Torokhov 		kfree(priv);
2958a09221e8SDmitry Torokhov 		return error;
295993050db2SDmitry Torokhov 	}
29601da177e4SLinus Torvalds 
29611da177e4SLinus Torvalds 	if (set_properties) {
29621da177e4SLinus Torvalds 		psmouse->vendor = "ALPS";
2963a09221e8SDmitry Torokhov 		psmouse->name = priv->flags & ALPS_DUALPOINT ?
2964968ac842SDmitry Torokhov 				"DualPoint TouchPad" : "GlidePoint";
2965a09221e8SDmitry Torokhov 		psmouse->model = priv->proto_version;
2966a09221e8SDmitry Torokhov 	} else {
2967a09221e8SDmitry Torokhov 		/*
2968a09221e8SDmitry Torokhov 		 * Destroy alps_data structure we allocated earlier since
2969a09221e8SDmitry Torokhov 		 * this was just a "trial run". Otherwise we'll keep it
2970a09221e8SDmitry Torokhov 		 * to be used by alps_init() which has to be called if
2971a09221e8SDmitry Torokhov 		 * we succeed and set_properties is true.
2972a09221e8SDmitry Torokhov 		 */
2973a09221e8SDmitry Torokhov 		kfree(priv);
2974a09221e8SDmitry Torokhov 		psmouse->private = NULL;
29751da177e4SLinus Torvalds 	}
2976a09221e8SDmitry Torokhov 
29771da177e4SLinus Torvalds 	return 0;
29781da177e4SLinus Torvalds }
29791da177e4SLinus Torvalds 
2980