xref: /openbmc/linux/drivers/input/mouse/alps.c (revision 99d9996c)
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 */
10240e8f53bSHans de Goede #define ALPS_IS_RUSHMORE	0x100	/* device is a rushmore */
1031da177e4SLinus Torvalds 
104e38de678SHelge Deller static const struct alps_model_info alps_model_data[] = {
10525bded7cSSeth Forshee 	{ { 0x32, 0x02, 0x14 },	0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT },	/* Toshiba Salellite Pro M10 */
10625bded7cSSeth Forshee 	{ { 0x33, 0x02, 0x0a },	0x00, ALPS_PROTO_V1, 0x88, 0xf8, 0 },				/* UMAX-530T */
10725bded7cSSeth Forshee 	{ { 0x53, 0x02, 0x0a },	0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
10825bded7cSSeth Forshee 	{ { 0x53, 0x02, 0x14 },	0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
10925bded7cSSeth Forshee 	{ { 0x60, 0x03, 0xc8 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },				/* HP ze1115 */
11025bded7cSSeth Forshee 	{ { 0x63, 0x02, 0x0a },	0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
11125bded7cSSeth Forshee 	{ { 0x63, 0x02, 0x14 },	0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
11225bded7cSSeth Forshee 	{ { 0x63, 0x02, 0x28 },	0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 },		/* Fujitsu Siemens S6010 */
11325bded7cSSeth Forshee 	{ { 0x63, 0x02, 0x3c },	0x00, ALPS_PROTO_V2, 0x8f, 0x8f, ALPS_WHEEL },			/* Toshiba Satellite S2400-103 */
11425bded7cSSeth Forshee 	{ { 0x63, 0x02, 0x50 },	0x00, ALPS_PROTO_V2, 0xef, 0xef, ALPS_FW_BK_1 },		/* NEC Versa L320 */
11525bded7cSSeth Forshee 	{ { 0x63, 0x02, 0x64 },	0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
11625bded7cSSeth Forshee 	{ { 0x63, 0x03, 0xc8 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT },	/* Dell Latitude D800 */
11725bded7cSSeth Forshee 	{ { 0x73, 0x00, 0x0a },	0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_DUALPOINT },		/* ThinkPad R61 8918-5QG */
11825bded7cSSeth Forshee 	{ { 0x73, 0x02, 0x0a },	0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
11925bded7cSSeth Forshee 	{ { 0x73, 0x02, 0x14 },	0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 },		/* Ahtec Laptop */
12025bded7cSSeth Forshee 	{ { 0x20, 0x02, 0x0e },	0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT },	/* XXX */
12125bded7cSSeth Forshee 	{ { 0x22, 0x02, 0x0a },	0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT },
12225bded7cSSeth Forshee 	{ { 0x22, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xff, 0xff, ALPS_PASS | ALPS_DUALPOINT },	/* Dell Latitude D600 */
1231d9f2626SSebastian Kapfer 	/* Dell Latitude E5500, E6400, E6500, Precision M4400 */
12425bded7cSSeth Forshee 	{ { 0x62, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xcf, 0xcf,
1251d9f2626SSebastian Kapfer 		ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED },
12695f75e91SYunkang Tang 	{ { 0x73, 0x00, 0x14 }, 0x00, ALPS_PROTO_V6, 0xff, 0xff, ALPS_DUALPOINT },		/* Dell XT2 */
12725bded7cSSeth Forshee 	{ { 0x73, 0x02, 0x50 }, 0x00, ALPS_PROTO_V2, 0xcf, 0xcf, ALPS_FOUR_BUTTONS },		/* Dell Vostro 1400 */
12825bded7cSSeth Forshee 	{ { 0x52, 0x01, 0x14 }, 0x00, ALPS_PROTO_V2, 0xff, 0xff,
129eb8bff85SThomas Bächler 		ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED },				/* Toshiba Tecra A11-11L */
13025bded7cSSeth Forshee 	{ { 0x73, 0x02, 0x64 },	0x8a, ALPS_PROTO_V4, 0x8f, 0x8f, 0 },
1311da177e4SLinus Torvalds };
1321da177e4SLinus Torvalds 
13324af5cb9SKevin Cernekee static void alps_set_abs_params_st(struct alps_data *priv,
13424af5cb9SKevin Cernekee 				   struct input_dev *dev1);
13524af5cb9SKevin Cernekee static void alps_set_abs_params_mt(struct alps_data *priv,
13624af5cb9SKevin Cernekee 				   struct input_dev *dev1);
13724af5cb9SKevin Cernekee 
1381da177e4SLinus Torvalds /*
1391da177e4SLinus Torvalds  * XXX - this entry is suspicious. First byte has zero lower nibble,
1401da177e4SLinus Torvalds  * which is what a normal mouse would report. Also, the value 0x0e
1411da177e4SLinus Torvalds  * isn't valid per PS/2 spec.
1421da177e4SLinus Torvalds  */
1431da177e4SLinus Torvalds 
144d4b347b2SSeth Forshee /* Packet formats are described in Documentation/input/alps.txt */
1451da177e4SLinus Torvalds 
14699df65e7SKevin Cernekee static bool alps_is_valid_first_byte(struct alps_data *priv,
1471d9f2626SSebastian Kapfer 				     unsigned char data)
1481d9f2626SSebastian Kapfer {
14999df65e7SKevin Cernekee 	return (data & priv->mask0) == priv->byte0;
1501d9f2626SSebastian Kapfer }
1511d9f2626SSebastian Kapfer 
1521d9f2626SSebastian Kapfer static void alps_report_buttons(struct psmouse *psmouse,
1531d9f2626SSebastian Kapfer 				struct input_dev *dev1, struct input_dev *dev2,
1541d9f2626SSebastian Kapfer 				int left, int right, int middle)
1551d9f2626SSebastian Kapfer {
1561d9f2626SSebastian Kapfer 	struct input_dev *dev;
1571d9f2626SSebastian Kapfer 
1581d9f2626SSebastian Kapfer 	/*
1591d9f2626SSebastian Kapfer 	 * If shared button has already been reported on the
1601d9f2626SSebastian Kapfer 	 * other device (dev2) then this event should be also
1611d9f2626SSebastian Kapfer 	 * sent through that device.
1621d9f2626SSebastian Kapfer 	 */
1631d9f2626SSebastian Kapfer 	dev = test_bit(BTN_LEFT, dev2->key) ? dev2 : dev1;
1641d9f2626SSebastian Kapfer 	input_report_key(dev, BTN_LEFT, left);
1651d9f2626SSebastian Kapfer 
1661d9f2626SSebastian Kapfer 	dev = test_bit(BTN_RIGHT, dev2->key) ? dev2 : dev1;
1671d9f2626SSebastian Kapfer 	input_report_key(dev, BTN_RIGHT, right);
1681d9f2626SSebastian Kapfer 
1691d9f2626SSebastian Kapfer 	dev = test_bit(BTN_MIDDLE, dev2->key) ? dev2 : dev1;
1701d9f2626SSebastian Kapfer 	input_report_key(dev, BTN_MIDDLE, middle);
1711d9f2626SSebastian Kapfer 
1721d9f2626SSebastian Kapfer 	/*
1731d9f2626SSebastian Kapfer 	 * Sync the _other_ device now, we'll do the first
1741d9f2626SSebastian Kapfer 	 * device later once we report the rest of the events.
1751d9f2626SSebastian Kapfer 	 */
1761d9f2626SSebastian Kapfer 	input_sync(dev2);
1771d9f2626SSebastian Kapfer }
1781d9f2626SSebastian Kapfer 
17925bded7cSSeth Forshee static void alps_process_packet_v1_v2(struct psmouse *psmouse)
1801da177e4SLinus Torvalds {
1811da177e4SLinus Torvalds 	struct alps_data *priv = psmouse->private;
1821da177e4SLinus Torvalds 	unsigned char *packet = psmouse->packet;
1832e5b636bSDmitry Torokhov 	struct input_dev *dev = psmouse->dev;
1842e5b636bSDmitry Torokhov 	struct input_dev *dev2 = priv->dev2;
1851da177e4SLinus Torvalds 	int x, y, z, ges, fin, left, right, middle;
186c30b4c10SIvan Casado Ruiz 	int back = 0, forward = 0;
1871da177e4SLinus Torvalds 
18899df65e7SKevin Cernekee 	if (priv->proto_version == ALPS_PROTO_V1) {
189d2f4012fSYotam Medini 		left = packet[2] & 0x10;
190d2f4012fSYotam Medini 		right = packet[2] & 0x08;
1911da177e4SLinus Torvalds 		middle = 0;
1921da177e4SLinus Torvalds 		x = packet[1] | ((packet[0] & 0x07) << 7);
1931da177e4SLinus Torvalds 		y = packet[4] | ((packet[3] & 0x07) << 7);
1941da177e4SLinus Torvalds 		z = packet[5];
1951da177e4SLinus Torvalds 	} else {
1961da177e4SLinus Torvalds 		left = packet[3] & 1;
1971da177e4SLinus Torvalds 		right = packet[3] & 2;
1981da177e4SLinus Torvalds 		middle = packet[3] & 4;
1991da177e4SLinus Torvalds 		x = packet[1] | ((packet[2] & 0x78) << (7 - 3));
2001da177e4SLinus Torvalds 		y = packet[4] | ((packet[3] & 0x70) << (7 - 4));
2011da177e4SLinus Torvalds 		z = packet[5];
2021da177e4SLinus Torvalds 	}
2031da177e4SLinus Torvalds 
20499df65e7SKevin Cernekee 	if (priv->flags & ALPS_FW_BK_1) {
2053c00bb96SLaszlo Kajan 		back = packet[0] & 0x10;
2063c00bb96SLaszlo Kajan 		forward = packet[2] & 4;
207c30b4c10SIvan Casado Ruiz 	}
208c30b4c10SIvan Casado Ruiz 
20999df65e7SKevin Cernekee 	if (priv->flags & ALPS_FW_BK_2) {
210c30b4c10SIvan Casado Ruiz 		back = packet[3] & 4;
211c30b4c10SIvan Casado Ruiz 		forward = packet[2] & 4;
212c30b4c10SIvan Casado Ruiz 		if ((middle = forward && back))
213c30b4c10SIvan Casado Ruiz 			forward = back = 0;
214c30b4c10SIvan Casado Ruiz 	}
215c30b4c10SIvan Casado Ruiz 
2161da177e4SLinus Torvalds 	ges = packet[2] & 1;
2171da177e4SLinus Torvalds 	fin = packet[2] & 2;
2181da177e4SLinus Torvalds 
21999df65e7SKevin Cernekee 	if ((priv->flags & ALPS_DUALPOINT) && z == 127) {
2201da177e4SLinus Torvalds 		input_report_rel(dev2, REL_X,  (x > 383 ? (x - 768) : x));
2211da177e4SLinus Torvalds 		input_report_rel(dev2, REL_Y, -(y > 255 ? (y - 512) : y));
222d7ed5d88SUlrich Dangel 
2231d9f2626SSebastian Kapfer 		alps_report_buttons(psmouse, dev2, dev, left, right, middle);
224d7ed5d88SUlrich Dangel 
2251da177e4SLinus Torvalds 		input_sync(dev2);
2261da177e4SLinus Torvalds 		return;
2271da177e4SLinus Torvalds 	}
2281da177e4SLinus Torvalds 
2291d9f2626SSebastian Kapfer 	alps_report_buttons(psmouse, dev, dev2, left, right, middle);
230d7ed5d88SUlrich Dangel 
2311da177e4SLinus Torvalds 	/* Convert hardware tap to a reasonable Z value */
23271bb21b6SMaxim Levitsky 	if (ges && !fin)
23371bb21b6SMaxim Levitsky 		z = 40;
2341da177e4SLinus Torvalds 
2351da177e4SLinus Torvalds 	/*
2361da177e4SLinus Torvalds 	 * A "tap and drag" operation is reported by the hardware as a transition
2371da177e4SLinus Torvalds 	 * from (!fin && ges) to (fin && ges). This should be translated to the
2381da177e4SLinus Torvalds 	 * sequence Z>0, Z==0, Z>0, so the Z==0 event has to be generated manually.
2391da177e4SLinus Torvalds 	 */
2401da177e4SLinus Torvalds 	if (ges && fin && !priv->prev_fin) {
2411da177e4SLinus Torvalds 		input_report_abs(dev, ABS_X, x);
2421da177e4SLinus Torvalds 		input_report_abs(dev, ABS_Y, y);
2431da177e4SLinus Torvalds 		input_report_abs(dev, ABS_PRESSURE, 0);
2441da177e4SLinus Torvalds 		input_report_key(dev, BTN_TOOL_FINGER, 0);
2451da177e4SLinus Torvalds 		input_sync(dev);
2461da177e4SLinus Torvalds 	}
2471da177e4SLinus Torvalds 	priv->prev_fin = fin;
2481da177e4SLinus Torvalds 
24971bb21b6SMaxim Levitsky 	if (z > 30)
25071bb21b6SMaxim Levitsky 		input_report_key(dev, BTN_TOUCH, 1);
25171bb21b6SMaxim Levitsky 	if (z < 25)
25271bb21b6SMaxim Levitsky 		input_report_key(dev, BTN_TOUCH, 0);
2531da177e4SLinus Torvalds 
2541da177e4SLinus Torvalds 	if (z > 0) {
2551da177e4SLinus Torvalds 		input_report_abs(dev, ABS_X, x);
2561da177e4SLinus Torvalds 		input_report_abs(dev, ABS_Y, y);
2571da177e4SLinus Torvalds 	}
2581da177e4SLinus Torvalds 
2591da177e4SLinus Torvalds 	input_report_abs(dev, ABS_PRESSURE, z);
2601da177e4SLinus Torvalds 	input_report_key(dev, BTN_TOOL_FINGER, z > 0);
2611da177e4SLinus Torvalds 
26299df65e7SKevin Cernekee 	if (priv->flags & ALPS_WHEEL)
263e6c047b9SVojtech Pavlik 		input_report_rel(dev, REL_WHEEL, ((packet[2] << 1) & 0x08) - ((packet[0] >> 4) & 0x07));
2641da177e4SLinus Torvalds 
26599df65e7SKevin Cernekee 	if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
266c30b4c10SIvan Casado Ruiz 		input_report_key(dev, BTN_FORWARD, forward);
267c30b4c10SIvan Casado Ruiz 		input_report_key(dev, BTN_BACK, back);
2681da177e4SLinus Torvalds 	}
2691da177e4SLinus Torvalds 
27099df65e7SKevin Cernekee 	if (priv->flags & ALPS_FOUR_BUTTONS) {
27171bb21b6SMaxim Levitsky 		input_report_key(dev, BTN_0, packet[2] & 4);
27271bb21b6SMaxim Levitsky 		input_report_key(dev, BTN_1, packet[0] & 0x10);
27371bb21b6SMaxim Levitsky 		input_report_key(dev, BTN_2, packet[3] & 4);
27471bb21b6SMaxim Levitsky 		input_report_key(dev, BTN_3, packet[0] & 0x20);
27571bb21b6SMaxim Levitsky 	}
27671bb21b6SMaxim Levitsky 
2771da177e4SLinus Torvalds 	input_sync(dev);
2781da177e4SLinus Torvalds }
2791da177e4SLinus Torvalds 
28001ce661fSSeth Forshee /*
281ee65d4b3SYunkang Tang  * Process bitmap data for V5 protocols. Return value is null.
282ee65d4b3SYunkang Tang  *
283ee65d4b3SYunkang Tang  * The bitmaps don't have enough data to track fingers, so this function
284ee65d4b3SYunkang Tang  * only generates points representing a bounding box of at most two contacts.
28502d04254SHans de Goede  * These two points are returned in fields->mt.
286ee65d4b3SYunkang Tang  */
287ee65d4b3SYunkang Tang static void alps_process_bitmap_dolphin(struct alps_data *priv,
28802d04254SHans de Goede 					struct alps_fields *fields)
289ee65d4b3SYunkang Tang {
290ee65d4b3SYunkang Tang 	int box_middle_x, box_middle_y;
291ee65d4b3SYunkang Tang 	unsigned int x_map, y_map;
292ee65d4b3SYunkang Tang 	unsigned char start_bit, end_bit;
293ee65d4b3SYunkang Tang 	unsigned char x_msb, x_lsb, y_msb, y_lsb;
294ee65d4b3SYunkang Tang 
295ee65d4b3SYunkang Tang 	x_map = fields->x_map;
296ee65d4b3SYunkang Tang 	y_map = fields->y_map;
297ee65d4b3SYunkang Tang 
298ee65d4b3SYunkang Tang 	if (!x_map || !y_map)
299ee65d4b3SYunkang Tang 		return;
300ee65d4b3SYunkang Tang 
301ee65d4b3SYunkang Tang 	/* Get Most-significant and Least-significant bit */
302ee65d4b3SYunkang Tang 	x_msb = fls(x_map);
303ee65d4b3SYunkang Tang 	x_lsb = ffs(x_map);
304ee65d4b3SYunkang Tang 	y_msb = fls(y_map);
305ee65d4b3SYunkang Tang 	y_lsb = ffs(y_map);
306ee65d4b3SYunkang Tang 
307ee65d4b3SYunkang Tang 	/* Most-significant bit should never exceed max sensor line number */
308ee65d4b3SYunkang Tang 	if (x_msb > priv->x_bits || y_msb > priv->y_bits)
309ee65d4b3SYunkang Tang 		return;
310ee65d4b3SYunkang Tang 
311ee65d4b3SYunkang Tang 	if (fields->fingers > 1) {
312ee65d4b3SYunkang Tang 		start_bit = priv->x_bits - x_msb;
313ee65d4b3SYunkang Tang 		end_bit = priv->x_bits - x_lsb;
314ee65d4b3SYunkang Tang 		box_middle_x = (priv->x_max * (start_bit + end_bit)) /
315ee65d4b3SYunkang Tang 				(2 * (priv->x_bits - 1));
316ee65d4b3SYunkang Tang 
317ee65d4b3SYunkang Tang 		start_bit = y_lsb - 1;
318ee65d4b3SYunkang Tang 		end_bit = y_msb - 1;
319ee65d4b3SYunkang Tang 		box_middle_y = (priv->y_max * (start_bit + end_bit)) /
320ee65d4b3SYunkang Tang 				(2 * (priv->y_bits - 1));
32102d04254SHans de Goede 		fields->mt[0] = fields->st;
32202d04254SHans de Goede 		fields->mt[1].x = 2 * box_middle_x - fields->mt[0].x;
32302d04254SHans de Goede 		fields->mt[1].y = 2 * box_middle_y - fields->mt[0].y;
324ee65d4b3SYunkang Tang 	}
325ee65d4b3SYunkang Tang }
326ee65d4b3SYunkang Tang 
327036e6c7bSHans de Goede static void alps_get_bitmap_points(unsigned int map,
328036e6c7bSHans de Goede 				   struct alps_bitmap_point *low,
329036e6c7bSHans de Goede 				   struct alps_bitmap_point *high,
330036e6c7bSHans de Goede 				   int *fingers)
331036e6c7bSHans de Goede {
332036e6c7bSHans de Goede 	struct alps_bitmap_point *point;
333036e6c7bSHans de Goede 	int i, bit, prev_bit = 0;
334036e6c7bSHans de Goede 
335036e6c7bSHans de Goede 	point = low;
336036e6c7bSHans de Goede 	for (i = 0; map != 0; i++, map >>= 1) {
337036e6c7bSHans de Goede 		bit = map & 1;
338036e6c7bSHans de Goede 		if (bit) {
339036e6c7bSHans de Goede 			if (!prev_bit) {
340036e6c7bSHans de Goede 				point->start_bit = i;
341105affbfSHans de Goede 				point->num_bits = 0;
342036e6c7bSHans de Goede 				(*fingers)++;
343036e6c7bSHans de Goede 			}
344036e6c7bSHans de Goede 			point->num_bits++;
345036e6c7bSHans de Goede 		} else {
346036e6c7bSHans de Goede 			if (prev_bit)
347036e6c7bSHans de Goede 				point = high;
348036e6c7bSHans de Goede 		}
349036e6c7bSHans de Goede 		prev_bit = bit;
350036e6c7bSHans de Goede 	}
351036e6c7bSHans de Goede }
352036e6c7bSHans de Goede 
353ee65d4b3SYunkang Tang /*
35401ce661fSSeth Forshee  * Process bitmap data from v3 and v4 protocols. Returns the number of
35501ce661fSSeth Forshee  * fingers detected. A return value of 0 means at least one of the
35601ce661fSSeth Forshee  * bitmaps was empty.
35701ce661fSSeth Forshee  *
35801ce661fSSeth Forshee  * The bitmaps don't have enough data to track fingers, so this function
35901ce661fSSeth Forshee  * only generates points representing a bounding box of all contacts.
36002d04254SHans de Goede  * These points are returned in fields->mt when the return value
36101ce661fSSeth Forshee  * is greater than 0.
36201ce661fSSeth Forshee  */
3637a9f73e7SKevin Cernekee static int alps_process_bitmap(struct alps_data *priv,
36402d04254SHans de Goede 			       struct alps_fields *fields)
36501ce661fSSeth Forshee {
366036e6c7bSHans de Goede 	int i, fingers_x = 0, fingers_y = 0, fingers;
36701ce661fSSeth Forshee 	struct alps_bitmap_point x_low = {0,}, x_high = {0,};
36801ce661fSSeth Forshee 	struct alps_bitmap_point y_low = {0,}, y_high = {0,};
36901ce661fSSeth Forshee 
37002d04254SHans de Goede 	if (!fields->x_map || !fields->y_map)
37101ce661fSSeth Forshee 		return 0;
37201ce661fSSeth Forshee 
37302d04254SHans de Goede 	alps_get_bitmap_points(fields->x_map, &x_low, &x_high, &fingers_x);
37402d04254SHans de Goede 	alps_get_bitmap_points(fields->y_map, &y_low, &y_high, &fingers_y);
37501ce661fSSeth Forshee 
37601ce661fSSeth Forshee 	/*
37701ce661fSSeth Forshee 	 * Fingers can overlap, so we use the maximum count of fingers
37801ce661fSSeth Forshee 	 * on either axis as the finger count.
37901ce661fSSeth Forshee 	 */
38001ce661fSSeth Forshee 	fingers = max(fingers_x, fingers_y);
38101ce661fSSeth Forshee 
38201ce661fSSeth Forshee 	/*
38320bea68bSHans de Goede 	 * If an axis reports only a single contact, we have overlapping or
38420bea68bSHans de Goede 	 * adjacent fingers. Divide the single contact between the two points.
38501ce661fSSeth Forshee 	 */
38601ce661fSSeth Forshee 	if (fingers_x == 1) {
38728835f45SHans de Goede 		i = (x_low.num_bits - 1) / 2;
38801ce661fSSeth Forshee 		x_low.num_bits = x_low.num_bits - i;
38901ce661fSSeth Forshee 		x_high.start_bit = x_low.start_bit + i;
39001ce661fSSeth Forshee 		x_high.num_bits = max(i, 1);
39120bea68bSHans de Goede 	}
39220bea68bSHans de Goede 	if (fingers_y == 1) {
39328835f45SHans de Goede 		i = (y_low.num_bits - 1) / 2;
39401ce661fSSeth Forshee 		y_low.num_bits = y_low.num_bits - i;
39501ce661fSSeth Forshee 		y_high.start_bit = y_low.start_bit + i;
39601ce661fSSeth Forshee 		y_high.num_bits = max(i, 1);
39701ce661fSSeth Forshee 	}
39801ce661fSSeth Forshee 
39902d04254SHans de Goede 	fields->mt[0].x =
40002d04254SHans de Goede 		(priv->x_max * (2 * x_low.start_bit + x_low.num_bits - 1)) /
4017a9f73e7SKevin Cernekee 		(2 * (priv->x_bits - 1));
40202d04254SHans de Goede 	fields->mt[0].y =
40302d04254SHans de Goede 		(priv->y_max * (2 * y_low.start_bit + y_low.num_bits - 1)) /
4047a9f73e7SKevin Cernekee 		(2 * (priv->y_bits - 1));
40501ce661fSSeth Forshee 
40602d04254SHans de Goede 	fields->mt[1].x =
40702d04254SHans de Goede 		(priv->x_max * (2 * x_high.start_bit + x_high.num_bits - 1)) /
4087a9f73e7SKevin Cernekee 		(2 * (priv->x_bits - 1));
40902d04254SHans de Goede 	fields->mt[1].y =
41002d04254SHans de Goede 		(priv->y_max * (2 * y_high.start_bit + y_high.num_bits - 1)) /
4117a9f73e7SKevin Cernekee 		(2 * (priv->y_bits - 1));
41201ce661fSSeth Forshee 
41340e8f53bSHans de Goede 	/* y-bitmap order is reversed, except on rushmore */
41440e8f53bSHans de Goede 	if (!(priv->flags & ALPS_IS_RUSHMORE)) {
41502d04254SHans de Goede 		fields->mt[0].y = priv->y_max - fields->mt[0].y;
41602d04254SHans de Goede 		fields->mt[1].y = priv->y_max - fields->mt[1].y;
41740e8f53bSHans de Goede 	}
41840e8f53bSHans de Goede 
41901ce661fSSeth Forshee 	return fingers;
42001ce661fSSeth Forshee }
42101ce661fSSeth Forshee 
422cdf333efSHans de Goede static void alps_set_slot(struct input_dev *dev, int slot, int x, int y)
42301ce661fSSeth Forshee {
42401ce661fSSeth Forshee 	input_mt_slot(dev, slot);
425cdf333efSHans de Goede 	input_mt_report_slot_state(dev, MT_TOOL_FINGER, true);
42601ce661fSSeth Forshee 	input_report_abs(dev, ABS_MT_POSITION_X, x);
42701ce661fSSeth Forshee 	input_report_abs(dev, ABS_MT_POSITION_Y, y);
42801ce661fSSeth Forshee }
42901ce661fSSeth Forshee 
430cdf333efSHans de Goede static void alps_report_mt_data(struct psmouse *psmouse, int n)
43101ce661fSSeth Forshee {
43202d04254SHans de Goede 	struct alps_data *priv = psmouse->private;
43302d04254SHans de Goede 	struct input_dev *dev = psmouse->dev;
43402d04254SHans de Goede 	struct alps_fields *f = &priv->f;
435cdf333efSHans de Goede 	int i, slot[MAX_TOUCHES];
43602d04254SHans de Goede 
437cdf333efSHans de Goede 	input_mt_assign_slots(dev, slot, f->mt, n);
438cdf333efSHans de Goede 	for (i = 0; i < n; i++)
439cdf333efSHans de Goede 		alps_set_slot(dev, slot[i], f->mt[i].x, f->mt[i].y);
440cdf333efSHans de Goede 
441cdf333efSHans de Goede 	input_mt_sync_frame(dev);
44201ce661fSSeth Forshee }
44301ce661fSSeth Forshee 
44468c21870SHans de Goede static void alps_report_semi_mt_data(struct psmouse *psmouse, int fingers)
44568c21870SHans de Goede {
44668c21870SHans de Goede 	struct alps_data *priv = psmouse->private;
44768c21870SHans de Goede 	struct input_dev *dev = psmouse->dev;
44868c21870SHans de Goede 	struct alps_fields *f = &priv->f;
44968c21870SHans de Goede 
45068c21870SHans de Goede 	/* Use st data when we don't have mt data */
45168c21870SHans de Goede 	if (fingers < 2) {
45268c21870SHans de Goede 		f->mt[0].x = f->st.x;
45368c21870SHans de Goede 		f->mt[0].y = f->st.y;
45468c21870SHans de Goede 		fingers = f->pressure > 0 ? 1 : 0;
45568c21870SHans de Goede 	}
45668c21870SHans de Goede 
45799d9996cSHans de Goede 	alps_report_mt_data(psmouse, (fingers <= 2) ? fingers : 2);
45868c21870SHans de Goede 
45968c21870SHans de Goede 	input_mt_report_finger_count(dev, fingers);
46068c21870SHans de Goede 
46168c21870SHans de Goede 	input_report_key(dev, BTN_LEFT, f->left);
46268c21870SHans de Goede 	input_report_key(dev, BTN_RIGHT, f->right);
46368c21870SHans de Goede 	input_report_key(dev, BTN_MIDDLE, f->middle);
46468c21870SHans de Goede 
46568c21870SHans de Goede 	input_report_abs(dev, ABS_PRESSURE, f->pressure);
46668c21870SHans de Goede 
46768c21870SHans de Goede 	input_sync(dev);
46868c21870SHans de Goede }
46968c21870SHans de Goede 
47025bded7cSSeth Forshee static void alps_process_trackstick_packet_v3(struct psmouse *psmouse)
47125bded7cSSeth Forshee {
47225bded7cSSeth Forshee 	struct alps_data *priv = psmouse->private;
47325bded7cSSeth Forshee 	unsigned char *packet = psmouse->packet;
47425bded7cSSeth Forshee 	struct input_dev *dev = priv->dev2;
47525bded7cSSeth Forshee 	int x, y, z, left, right, middle;
47625bded7cSSeth Forshee 
47725bded7cSSeth Forshee 	/* Sanity check packet */
47825bded7cSSeth Forshee 	if (!(packet[0] & 0x40)) {
47925bded7cSSeth Forshee 		psmouse_dbg(psmouse, "Bad trackstick packet, discarding\n");
48025bded7cSSeth Forshee 		return;
48125bded7cSSeth Forshee 	}
48225bded7cSSeth Forshee 
48325bded7cSSeth Forshee 	/*
48425bded7cSSeth Forshee 	 * There's a special packet that seems to indicate the end
48525bded7cSSeth Forshee 	 * of a stream of trackstick data. Filter these out.
48625bded7cSSeth Forshee 	 */
48725bded7cSSeth Forshee 	if (packet[1] == 0x7f && packet[2] == 0x7f && packet[4] == 0x7f)
48825bded7cSSeth Forshee 		return;
48925bded7cSSeth Forshee 
49025bded7cSSeth Forshee 	x = (s8)(((packet[0] & 0x20) << 2) | (packet[1] & 0x7f));
49125bded7cSSeth Forshee 	y = (s8)(((packet[0] & 0x10) << 3) | (packet[2] & 0x7f));
49225bded7cSSeth Forshee 	z = (packet[4] & 0x7c) >> 2;
49325bded7cSSeth Forshee 
49425bded7cSSeth Forshee 	/*
49525bded7cSSeth Forshee 	 * The x and y values tend to be quite large, and when used
49625bded7cSSeth Forshee 	 * alone the trackstick is difficult to use. Scale them down
49725bded7cSSeth Forshee 	 * to compensate.
49825bded7cSSeth Forshee 	 */
49925bded7cSSeth Forshee 	x /= 8;
50025bded7cSSeth Forshee 	y /= 8;
50125bded7cSSeth Forshee 
50225bded7cSSeth Forshee 	input_report_rel(dev, REL_X, x);
50325bded7cSSeth Forshee 	input_report_rel(dev, REL_Y, -y);
50425bded7cSSeth Forshee 
50525bded7cSSeth Forshee 	/*
50625bded7cSSeth Forshee 	 * Most ALPS models report the trackstick buttons in the touchpad
50725bded7cSSeth Forshee 	 * packets, but a few report them here. No reliable way has been
50825bded7cSSeth Forshee 	 * found to differentiate between the models upfront, so we enable
50925bded7cSSeth Forshee 	 * the quirk in response to seeing a button press in the trackstick
51025bded7cSSeth Forshee 	 * packet.
51125bded7cSSeth Forshee 	 */
51225bded7cSSeth Forshee 	left = packet[3] & 0x01;
51325bded7cSSeth Forshee 	right = packet[3] & 0x02;
51425bded7cSSeth Forshee 	middle = packet[3] & 0x04;
51525bded7cSSeth Forshee 
51625bded7cSSeth Forshee 	if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) &&
51725bded7cSSeth Forshee 	    (left || right || middle))
51825bded7cSSeth Forshee 		priv->quirks |= ALPS_QUIRK_TRACKSTICK_BUTTONS;
51925bded7cSSeth Forshee 
52025bded7cSSeth Forshee 	if (priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) {
52125bded7cSSeth Forshee 		input_report_key(dev, BTN_LEFT, left);
52225bded7cSSeth Forshee 		input_report_key(dev, BTN_RIGHT, right);
52325bded7cSSeth Forshee 		input_report_key(dev, BTN_MIDDLE, middle);
52425bded7cSSeth Forshee 	}
52525bded7cSSeth Forshee 
52625bded7cSSeth Forshee 	input_sync(dev);
52725bded7cSSeth Forshee 	return;
52825bded7cSSeth Forshee }
52925bded7cSSeth Forshee 
530f85e5001SKevin Cernekee static void alps_decode_buttons_v3(struct alps_fields *f, unsigned char *p)
531f85e5001SKevin Cernekee {
532f85e5001SKevin Cernekee 	f->left = !!(p[3] & 0x01);
533f85e5001SKevin Cernekee 	f->right = !!(p[3] & 0x02);
534f85e5001SKevin Cernekee 	f->middle = !!(p[3] & 0x04);
535f85e5001SKevin Cernekee 
536f85e5001SKevin Cernekee 	f->ts_left = !!(p[3] & 0x10);
537f85e5001SKevin Cernekee 	f->ts_right = !!(p[3] & 0x20);
538f85e5001SKevin Cernekee 	f->ts_middle = !!(p[3] & 0x40);
539f85e5001SKevin Cernekee }
540f85e5001SKevin Cernekee 
541ee65d4b3SYunkang Tang static void alps_decode_pinnacle(struct alps_fields *f, unsigned char *p,
542ee65d4b3SYunkang Tang 				 struct psmouse *psmouse)
543f85e5001SKevin Cernekee {
544f85e5001SKevin Cernekee 	f->first_mp = !!(p[4] & 0x40);
545f85e5001SKevin Cernekee 	f->is_mp = !!(p[0] & 0x40);
546f85e5001SKevin Cernekee 
547f85e5001SKevin Cernekee 	f->fingers = (p[5] & 0x3) + 1;
548f85e5001SKevin Cernekee 	f->x_map = ((p[4] & 0x7e) << 8) |
549f85e5001SKevin Cernekee 		   ((p[1] & 0x7f) << 2) |
550f85e5001SKevin Cernekee 		   ((p[0] & 0x30) >> 4);
551f85e5001SKevin Cernekee 	f->y_map = ((p[3] & 0x70) << 4) |
552f85e5001SKevin Cernekee 		   ((p[2] & 0x7f) << 1) |
553f85e5001SKevin Cernekee 		   (p[4] & 0x01);
554f85e5001SKevin Cernekee 
55502d04254SHans de Goede 	f->st.x = ((p[1] & 0x7f) << 4) | ((p[4] & 0x30) >> 2) |
556f85e5001SKevin Cernekee 	       ((p[0] & 0x30) >> 4);
55702d04254SHans de Goede 	f->st.y = ((p[2] & 0x7f) << 4) | (p[4] & 0x0f);
55802d04254SHans de Goede 	f->pressure = p[5] & 0x7f;
559f85e5001SKevin Cernekee 
560f85e5001SKevin Cernekee 	alps_decode_buttons_v3(f, p);
561f85e5001SKevin Cernekee }
562f85e5001SKevin Cernekee 
563ee65d4b3SYunkang Tang static void alps_decode_rushmore(struct alps_fields *f, unsigned char *p,
564ee65d4b3SYunkang Tang 				 struct psmouse *psmouse)
5651302bac3SKevin Cernekee {
566ee65d4b3SYunkang Tang 	alps_decode_pinnacle(f, p, psmouse);
5671302bac3SKevin Cernekee 
568f105e34aSYunkang Tang 	/* Rushmore's packet decode has a bit difference with Pinnacle's */
569f105e34aSYunkang Tang 	f->is_mp = !!(p[5] & 0x40);
570f105e34aSYunkang Tang 	f->fingers = max((p[5] & 0x3), ((p[5] >> 2) & 0x3)) + 1;
5711302bac3SKevin Cernekee 	f->x_map |= (p[5] & 0x10) << 11;
5721302bac3SKevin Cernekee 	f->y_map |= (p[5] & 0x20) << 6;
5731302bac3SKevin Cernekee }
5741302bac3SKevin Cernekee 
575ee65d4b3SYunkang Tang static void alps_decode_dolphin(struct alps_fields *f, unsigned char *p,
576ee65d4b3SYunkang Tang 				struct psmouse *psmouse)
57775af9e56SDave Turvene {
578ee65d4b3SYunkang Tang 	u64 palm_data = 0;
579ee65d4b3SYunkang Tang 	struct alps_data *priv = psmouse->private;
580ee65d4b3SYunkang Tang 
58175af9e56SDave Turvene 	f->first_mp = !!(p[0] & 0x02);
58275af9e56SDave Turvene 	f->is_mp = !!(p[0] & 0x20);
58375af9e56SDave Turvene 
584ee65d4b3SYunkang Tang 	if (!f->is_mp) {
58502d04254SHans de Goede 		f->st.x = ((p[1] & 0x7f) | ((p[4] & 0x0f) << 7));
58602d04254SHans de Goede 		f->st.y = ((p[2] & 0x7f) | ((p[4] & 0xf0) << 3));
58702d04254SHans de Goede 		f->pressure = (p[0] & 4) ? 0 : p[5] & 0x7f;
58875af9e56SDave Turvene 		alps_decode_buttons_v3(f, p);
589ee65d4b3SYunkang Tang 	} else {
590ee65d4b3SYunkang Tang 		f->fingers = ((p[0] & 0x6) >> 1 |
591ee65d4b3SYunkang Tang 		     (p[0] & 0x10) >> 2);
592ee65d4b3SYunkang Tang 
593ee65d4b3SYunkang Tang 		palm_data = (p[1] & 0x7f) |
594ee65d4b3SYunkang Tang 			    ((p[2] & 0x7f) << 7) |
595ee65d4b3SYunkang Tang 			    ((p[4] & 0x7f) << 14) |
596ee65d4b3SYunkang Tang 			    ((p[5] & 0x7f) << 21) |
597ee65d4b3SYunkang Tang 			    ((p[3] & 0x07) << 28) |
598ee65d4b3SYunkang Tang 			    (((u64)p[3] & 0x70) << 27) |
599ee65d4b3SYunkang Tang 			    (((u64)p[0] & 0x01) << 34);
600ee65d4b3SYunkang Tang 
601ee65d4b3SYunkang Tang 		/* Y-profile is stored in P(0) to p(n-1), n = y_bits; */
602ee65d4b3SYunkang Tang 		f->y_map = palm_data & (BIT(priv->y_bits) - 1);
603ee65d4b3SYunkang Tang 
604ee65d4b3SYunkang Tang 		/* X-profile is stored in p(n) to p(n+m-1), m = x_bits; */
605ee65d4b3SYunkang Tang 		f->x_map = (palm_data >> priv->y_bits) &
606ee65d4b3SYunkang Tang 			   (BIT(priv->x_bits) - 1);
607ee65d4b3SYunkang Tang 	}
60875af9e56SDave Turvene }
60975af9e56SDave Turvene 
610ee65d4b3SYunkang Tang static void alps_process_touchpad_packet_v3_v5(struct psmouse *psmouse)
61125bded7cSSeth Forshee {
61225bded7cSSeth Forshee 	struct alps_data *priv = psmouse->private;
61325bded7cSSeth Forshee 	unsigned char *packet = psmouse->packet;
61425bded7cSSeth Forshee 	struct input_dev *dev2 = priv->dev2;
61502d04254SHans de Goede 	struct alps_fields *f = &priv->f;
61602d04254SHans de Goede 	int fingers = 0;
617f85e5001SKevin Cernekee 
61802d04254SHans de Goede 	memset(f, 0, sizeof(*f));
61902d04254SHans de Goede 
62002d04254SHans de Goede 	priv->decode_fields(f, packet, psmouse);
62125bded7cSSeth Forshee 
62225bded7cSSeth Forshee 	/*
62301ce661fSSeth Forshee 	 * There's no single feature of touchpad position and bitmap packets
62401ce661fSSeth Forshee 	 * that can be used to distinguish between them. We rely on the fact
62501ce661fSSeth Forshee 	 * that a bitmap packet should always follow a position packet with
62601ce661fSSeth Forshee 	 * bit 6 of packet[4] set.
62725bded7cSSeth Forshee 	 */
62825bded7cSSeth Forshee 	if (priv->multi_packet) {
62925bded7cSSeth Forshee 		/*
63025bded7cSSeth Forshee 		 * Sometimes a position packet will indicate a multi-packet
63125bded7cSSeth Forshee 		 * sequence, but then what follows is another position
63225bded7cSSeth Forshee 		 * packet. Check for this, and when it happens process the
63325bded7cSSeth Forshee 		 * position packet as usual.
63425bded7cSSeth Forshee 		 */
63502d04254SHans de Goede 		if (f->is_mp) {
63602d04254SHans de Goede 			fingers = f->fingers;
637ee65d4b3SYunkang Tang 			if (priv->proto_version == ALPS_PROTO_V3) {
63802d04254SHans de Goede 				if (alps_process_bitmap(priv, f) == 0)
63920bea68bSHans de Goede 					fingers = 0; /* Use st data */
64001ce661fSSeth Forshee 
64101ce661fSSeth Forshee 				/* Now process position packet */
64202d04254SHans de Goede 				priv->decode_fields(f, priv->multi_data,
643ee65d4b3SYunkang Tang 						    psmouse);
644ee65d4b3SYunkang Tang 			} else {
645ee65d4b3SYunkang Tang 				/*
646ee65d4b3SYunkang Tang 				 * Because Dolphin uses position packet's
647ee65d4b3SYunkang Tang 				 * coordinate data as Pt1 and uses it to
648ee65d4b3SYunkang Tang 				 * calculate Pt2, so we need to do position
649ee65d4b3SYunkang Tang 				 * packet decode first.
650ee65d4b3SYunkang Tang 				 */
65102d04254SHans de Goede 				priv->decode_fields(f, priv->multi_data,
652ee65d4b3SYunkang Tang 						    psmouse);
653ee65d4b3SYunkang Tang 
654ee65d4b3SYunkang Tang 				/*
655ee65d4b3SYunkang Tang 				 * Since Dolphin's finger number is reliable,
656ee65d4b3SYunkang Tang 				 * there is no need to compare with bmap_fn.
657ee65d4b3SYunkang Tang 				 */
65802d04254SHans de Goede 				alps_process_bitmap_dolphin(priv, f);
659ee65d4b3SYunkang Tang 			}
66001ce661fSSeth Forshee 		} else {
66101ce661fSSeth Forshee 			priv->multi_packet = 0;
66225bded7cSSeth Forshee 		}
66325bded7cSSeth Forshee 	}
66425bded7cSSeth Forshee 
66501ce661fSSeth Forshee 	/*
66601ce661fSSeth Forshee 	 * Bit 6 of byte 0 is not usually set in position packets. The only
66701ce661fSSeth Forshee 	 * times it seems to be set is in situations where the data is
66801ce661fSSeth Forshee 	 * suspect anyway, e.g. a palm resting flat on the touchpad. Given
66901ce661fSSeth Forshee 	 * this combined with the fact that this bit is useful for filtering
67001ce661fSSeth Forshee 	 * out misidentified bitmap packets, we reject anything with this
67101ce661fSSeth Forshee 	 * bit set.
67201ce661fSSeth Forshee 	 */
67302d04254SHans de Goede 	if (f->is_mp)
67401ce661fSSeth Forshee 		return;
67501ce661fSSeth Forshee 
67602d04254SHans de Goede 	if (!priv->multi_packet && f->first_mp) {
67725bded7cSSeth Forshee 		priv->multi_packet = 1;
67801ce661fSSeth Forshee 		memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
67901ce661fSSeth Forshee 		return;
68001ce661fSSeth Forshee 	}
68101ce661fSSeth Forshee 
68225bded7cSSeth Forshee 	priv->multi_packet = 0;
68325bded7cSSeth Forshee 
68425bded7cSSeth Forshee 	/*
68525bded7cSSeth Forshee 	 * Sometimes the hardware sends a single packet with z = 0
68625bded7cSSeth Forshee 	 * in the middle of a stream. Real releases generate packets
68725bded7cSSeth Forshee 	 * with x, y, and z all zero, so these seem to be flukes.
68825bded7cSSeth Forshee 	 * Ignore them.
68925bded7cSSeth Forshee 	 */
69002d04254SHans de Goede 	if (f->st.x && f->st.y && !f->pressure)
69125bded7cSSeth Forshee 		return;
69225bded7cSSeth Forshee 
69368c21870SHans de Goede 	alps_report_semi_mt_data(psmouse, fingers);
69425bded7cSSeth Forshee 
69525bded7cSSeth Forshee 	if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS)) {
69602d04254SHans de Goede 		input_report_key(dev2, BTN_LEFT, f->ts_left);
69702d04254SHans de Goede 		input_report_key(dev2, BTN_RIGHT, f->ts_right);
69802d04254SHans de Goede 		input_report_key(dev2, BTN_MIDDLE, f->ts_middle);
69925bded7cSSeth Forshee 		input_sync(dev2);
70025bded7cSSeth Forshee 	}
70125bded7cSSeth Forshee }
70225bded7cSSeth Forshee 
70325bded7cSSeth Forshee static void alps_process_packet_v3(struct psmouse *psmouse)
70425bded7cSSeth Forshee {
70525bded7cSSeth Forshee 	unsigned char *packet = psmouse->packet;
70625bded7cSSeth Forshee 
70725bded7cSSeth Forshee 	/*
70825bded7cSSeth Forshee 	 * v3 protocol packets come in three types, two representing
70925bded7cSSeth Forshee 	 * touchpad data and one representing trackstick data.
71025bded7cSSeth Forshee 	 * Trackstick packets seem to be distinguished by always
71125bded7cSSeth Forshee 	 * having 0x3f in the last byte. This value has never been
71225bded7cSSeth Forshee 	 * observed in the last byte of either of the other types
71325bded7cSSeth Forshee 	 * of packets.
71425bded7cSSeth Forshee 	 */
71525bded7cSSeth Forshee 	if (packet[5] == 0x3f) {
71625bded7cSSeth Forshee 		alps_process_trackstick_packet_v3(psmouse);
71725bded7cSSeth Forshee 		return;
71825bded7cSSeth Forshee 	}
71925bded7cSSeth Forshee 
720ee65d4b3SYunkang Tang 	alps_process_touchpad_packet_v3_v5(psmouse);
72125bded7cSSeth Forshee }
72225bded7cSSeth Forshee 
72395f75e91SYunkang Tang static void alps_process_packet_v6(struct psmouse *psmouse)
72495f75e91SYunkang Tang {
72595f75e91SYunkang Tang 	struct alps_data *priv = psmouse->private;
72695f75e91SYunkang Tang 	unsigned char *packet = psmouse->packet;
72795f75e91SYunkang Tang 	struct input_dev *dev = psmouse->dev;
72895f75e91SYunkang Tang 	struct input_dev *dev2 = priv->dev2;
72995f75e91SYunkang Tang 	int x, y, z, left, right, middle;
73095f75e91SYunkang Tang 
73195f75e91SYunkang Tang 	/*
73295f75e91SYunkang Tang 	 * We can use Byte5 to distinguish if the packet is from Touchpad
73395f75e91SYunkang Tang 	 * or Trackpoint.
73495f75e91SYunkang Tang 	 * Touchpad:	0 - 0x7E
73595f75e91SYunkang Tang 	 * Trackpoint:	0x7F
73695f75e91SYunkang Tang 	 */
73795f75e91SYunkang Tang 	if (packet[5] == 0x7F) {
73895f75e91SYunkang Tang 		/* It should be a DualPoint when received Trackpoint packet */
73995f75e91SYunkang Tang 		if (!(priv->flags & ALPS_DUALPOINT))
74095f75e91SYunkang Tang 			return;
74195f75e91SYunkang Tang 
74295f75e91SYunkang Tang 		/* Trackpoint packet */
74395f75e91SYunkang Tang 		x = packet[1] | ((packet[3] & 0x20) << 2);
74495f75e91SYunkang Tang 		y = packet[2] | ((packet[3] & 0x40) << 1);
74595f75e91SYunkang Tang 		z = packet[4];
74695f75e91SYunkang Tang 		left = packet[3] & 0x01;
74795f75e91SYunkang Tang 		right = packet[3] & 0x02;
74895f75e91SYunkang Tang 		middle = packet[3] & 0x04;
74995f75e91SYunkang Tang 
75095f75e91SYunkang Tang 		/* To prevent the cursor jump when finger lifted */
75195f75e91SYunkang Tang 		if (x == 0x7F && y == 0x7F && z == 0x7F)
75295f75e91SYunkang Tang 			x = y = z = 0;
75395f75e91SYunkang Tang 
75495f75e91SYunkang Tang 		/* Divide 4 since trackpoint's speed is too fast */
75595f75e91SYunkang Tang 		input_report_rel(dev2, REL_X, (char)x / 4);
75695f75e91SYunkang Tang 		input_report_rel(dev2, REL_Y, -((char)y / 4));
75795f75e91SYunkang Tang 
75895f75e91SYunkang Tang 		input_report_key(dev2, BTN_LEFT, left);
75995f75e91SYunkang Tang 		input_report_key(dev2, BTN_RIGHT, right);
76095f75e91SYunkang Tang 		input_report_key(dev2, BTN_MIDDLE, middle);
76195f75e91SYunkang Tang 
76295f75e91SYunkang Tang 		input_sync(dev2);
76395f75e91SYunkang Tang 		return;
76495f75e91SYunkang Tang 	}
76595f75e91SYunkang Tang 
76695f75e91SYunkang Tang 	/* Touchpad packet */
76795f75e91SYunkang Tang 	x = packet[1] | ((packet[3] & 0x78) << 4);
76895f75e91SYunkang Tang 	y = packet[2] | ((packet[4] & 0x78) << 4);
76995f75e91SYunkang Tang 	z = packet[5];
77095f75e91SYunkang Tang 	left = packet[3] & 0x01;
77195f75e91SYunkang Tang 	right = packet[3] & 0x02;
77295f75e91SYunkang Tang 
77395f75e91SYunkang Tang 	if (z > 30)
77495f75e91SYunkang Tang 		input_report_key(dev, BTN_TOUCH, 1);
77595f75e91SYunkang Tang 	if (z < 25)
77695f75e91SYunkang Tang 		input_report_key(dev, BTN_TOUCH, 0);
77795f75e91SYunkang Tang 
77895f75e91SYunkang Tang 	if (z > 0) {
77995f75e91SYunkang Tang 		input_report_abs(dev, ABS_X, x);
78095f75e91SYunkang Tang 		input_report_abs(dev, ABS_Y, y);
78195f75e91SYunkang Tang 	}
78295f75e91SYunkang Tang 
78395f75e91SYunkang Tang 	input_report_abs(dev, ABS_PRESSURE, z);
78495f75e91SYunkang Tang 	input_report_key(dev, BTN_TOOL_FINGER, z > 0);
78595f75e91SYunkang Tang 
78695f75e91SYunkang Tang 	/* v6 touchpad does not have middle button */
78795f75e91SYunkang Tang 	input_report_key(dev, BTN_LEFT, left);
78895f75e91SYunkang Tang 	input_report_key(dev, BTN_RIGHT, right);
78995f75e91SYunkang Tang 
79095f75e91SYunkang Tang 	input_sync(dev);
79195f75e91SYunkang Tang }
79295f75e91SYunkang Tang 
79325bded7cSSeth Forshee static void alps_process_packet_v4(struct psmouse *psmouse)
79425bded7cSSeth Forshee {
7953b7e09faSGeorge Pantalos 	struct alps_data *priv = psmouse->private;
79625bded7cSSeth Forshee 	unsigned char *packet = psmouse->packet;
79702d04254SHans de Goede 	struct alps_fields *f = &priv->f;
79868c21870SHans de Goede 	int offset;
7993b7e09faSGeorge Pantalos 
8003b7e09faSGeorge Pantalos 	/*
8013b7e09faSGeorge Pantalos 	 * v4 has a 6-byte encoding for bitmap data, but this data is
8023b7e09faSGeorge Pantalos 	 * broken up between 3 normal packets. Use priv->multi_packet to
8033b7e09faSGeorge Pantalos 	 * track our position in the bitmap packet.
8043b7e09faSGeorge Pantalos 	 */
8053b7e09faSGeorge Pantalos 	if (packet[6] & 0x40) {
8063b7e09faSGeorge Pantalos 		/* sync, reset position */
8073b7e09faSGeorge Pantalos 		priv->multi_packet = 0;
8083b7e09faSGeorge Pantalos 	}
8093b7e09faSGeorge Pantalos 
8103b7e09faSGeorge Pantalos 	if (WARN_ON_ONCE(priv->multi_packet > 2))
8113b7e09faSGeorge Pantalos 		return;
8123b7e09faSGeorge Pantalos 
8133b7e09faSGeorge Pantalos 	offset = 2 * priv->multi_packet;
8143b7e09faSGeorge Pantalos 	priv->multi_data[offset] = packet[6];
8153b7e09faSGeorge Pantalos 	priv->multi_data[offset + 1] = packet[7];
8163b7e09faSGeorge Pantalos 
8173b7e09faSGeorge Pantalos 	if (++priv->multi_packet > 2) {
8183b7e09faSGeorge Pantalos 		priv->multi_packet = 0;
8193b7e09faSGeorge Pantalos 
82002d04254SHans de Goede 		f->x_map = ((priv->multi_data[2] & 0x1f) << 10) |
8213b7e09faSGeorge Pantalos 			   ((priv->multi_data[3] & 0x60) << 3) |
8223b7e09faSGeorge Pantalos 			   ((priv->multi_data[0] & 0x3f) << 2) |
8233b7e09faSGeorge Pantalos 			   ((priv->multi_data[1] & 0x60) >> 5);
82402d04254SHans de Goede 		f->y_map = ((priv->multi_data[5] & 0x01) << 10) |
8253b7e09faSGeorge Pantalos 			   ((priv->multi_data[3] & 0x1f) << 5) |
8263b7e09faSGeorge Pantalos 			    (priv->multi_data[1] & 0x1f);
8273b7e09faSGeorge Pantalos 
82802d04254SHans de Goede 		f->fingers = alps_process_bitmap(priv, f);
8293b7e09faSGeorge Pantalos 	}
83025bded7cSSeth Forshee 
83102d04254SHans de Goede 	f->left = packet[4] & 0x01;
83202d04254SHans de Goede 	f->right = packet[4] & 0x02;
83325bded7cSSeth Forshee 
83402d04254SHans de Goede 	f->st.x = ((packet[1] & 0x7f) << 4) | ((packet[3] & 0x30) >> 2) |
83525bded7cSSeth Forshee 		  ((packet[0] & 0x30) >> 4);
83602d04254SHans de Goede 	f->st.y = ((packet[2] & 0x7f) << 4) | (packet[3] & 0x0f);
83702d04254SHans de Goede 	f->pressure = packet[5] & 0x7f;
83825bded7cSSeth Forshee 
83968c21870SHans de Goede 	alps_report_semi_mt_data(psmouse, f->fingers);
84025bded7cSSeth Forshee }
84125bded7cSSeth Forshee 
8421d9f2626SSebastian Kapfer static void alps_report_bare_ps2_packet(struct psmouse *psmouse,
8431d9f2626SSebastian Kapfer 					unsigned char packet[],
8441d9f2626SSebastian Kapfer 					bool report_buttons)
8451d9f2626SSebastian Kapfer {
8461d9f2626SSebastian Kapfer 	struct alps_data *priv = psmouse->private;
8471d9f2626SSebastian Kapfer 	struct input_dev *dev2 = priv->dev2;
8481d9f2626SSebastian Kapfer 
8491d9f2626SSebastian Kapfer 	if (report_buttons)
8501d9f2626SSebastian Kapfer 		alps_report_buttons(psmouse, dev2, psmouse->dev,
8511d9f2626SSebastian Kapfer 				packet[0] & 1, packet[0] & 2, packet[0] & 4);
8521d9f2626SSebastian Kapfer 
8531d9f2626SSebastian Kapfer 	input_report_rel(dev2, REL_X,
8541d9f2626SSebastian Kapfer 		packet[1] ? packet[1] - ((packet[0] << 4) & 0x100) : 0);
8551d9f2626SSebastian Kapfer 	input_report_rel(dev2, REL_Y,
8561d9f2626SSebastian Kapfer 		packet[2] ? ((packet[0] << 3) & 0x100) - packet[2] : 0);
8571d9f2626SSebastian Kapfer 
8581d9f2626SSebastian Kapfer 	input_sync(dev2);
8591d9f2626SSebastian Kapfer }
8601d9f2626SSebastian Kapfer 
8611d9f2626SSebastian Kapfer static psmouse_ret_t alps_handle_interleaved_ps2(struct psmouse *psmouse)
8621da177e4SLinus Torvalds {
8631da177e4SLinus Torvalds 	struct alps_data *priv = psmouse->private;
8641da177e4SLinus Torvalds 
8651d9f2626SSebastian Kapfer 	if (psmouse->pktcnt < 6)
8661d9f2626SSebastian Kapfer 		return PSMOUSE_GOOD_DATA;
8671d9f2626SSebastian Kapfer 
8681d9f2626SSebastian Kapfer 	if (psmouse->pktcnt == 6) {
8691d9f2626SSebastian Kapfer 		/*
8701d9f2626SSebastian Kapfer 		 * Start a timer to flush the packet if it ends up last
8711d9f2626SSebastian Kapfer 		 * 6-byte packet in the stream. Timer needs to fire
8721d9f2626SSebastian Kapfer 		 * psmouse core times out itself. 20 ms should be enough
8731d9f2626SSebastian Kapfer 		 * to decide if we are getting more data or not.
8741d9f2626SSebastian Kapfer 		 */
8751d9f2626SSebastian Kapfer 		mod_timer(&priv->timer, jiffies + msecs_to_jiffies(20));
8761d9f2626SSebastian Kapfer 		return PSMOUSE_GOOD_DATA;
8771d9f2626SSebastian Kapfer 	}
8781d9f2626SSebastian Kapfer 
8791d9f2626SSebastian Kapfer 	del_timer(&priv->timer);
8801d9f2626SSebastian Kapfer 
8811d9f2626SSebastian Kapfer 	if (psmouse->packet[6] & 0x80) {
8821d9f2626SSebastian Kapfer 
8831d9f2626SSebastian Kapfer 		/*
8841d9f2626SSebastian Kapfer 		 * Highest bit is set - that means we either had
8851d9f2626SSebastian Kapfer 		 * complete ALPS packet and this is start of the
8861d9f2626SSebastian Kapfer 		 * next packet or we got garbage.
8871d9f2626SSebastian Kapfer 		 */
8881d9f2626SSebastian Kapfer 
8891d9f2626SSebastian Kapfer 		if (((psmouse->packet[3] |
8901d9f2626SSebastian Kapfer 		      psmouse->packet[4] |
8911d9f2626SSebastian Kapfer 		      psmouse->packet[5]) & 0x80) ||
89299df65e7SKevin Cernekee 		    (!alps_is_valid_first_byte(priv, psmouse->packet[6]))) {
893b5d21704SDmitry Torokhov 			psmouse_dbg(psmouse,
8943b112923SAndy Shevchenko 				    "refusing packet %4ph (suspected interleaved ps/2)\n",
8953b112923SAndy Shevchenko 				    psmouse->packet + 3);
8961d9f2626SSebastian Kapfer 			return PSMOUSE_BAD_DATA;
8971d9f2626SSebastian Kapfer 		}
8981d9f2626SSebastian Kapfer 
89924af5cb9SKevin Cernekee 		priv->process_packet(psmouse);
9001d9f2626SSebastian Kapfer 
9011d9f2626SSebastian Kapfer 		/* Continue with the next packet */
9021d9f2626SSebastian Kapfer 		psmouse->packet[0] = psmouse->packet[6];
9031d9f2626SSebastian Kapfer 		psmouse->pktcnt = 1;
9041d9f2626SSebastian Kapfer 
9051d9f2626SSebastian Kapfer 	} else {
9061d9f2626SSebastian Kapfer 
9071d9f2626SSebastian Kapfer 		/*
9081d9f2626SSebastian Kapfer 		 * High bit is 0 - that means that we indeed got a PS/2
9091d9f2626SSebastian Kapfer 		 * packet in the middle of ALPS packet.
9101d9f2626SSebastian Kapfer 		 *
9111d9f2626SSebastian Kapfer 		 * There is also possibility that we got 6-byte ALPS
9121d9f2626SSebastian Kapfer 		 * packet followed  by 3-byte packet from trackpoint. We
9131d9f2626SSebastian Kapfer 		 * can not distinguish between these 2 scenarios but
914b5d21704SDmitry Torokhov 		 * because the latter is unlikely to happen in course of
9151d9f2626SSebastian Kapfer 		 * normal operation (user would need to press all
9161d9f2626SSebastian Kapfer 		 * buttons on the pad and start moving trackpoint
9171d9f2626SSebastian Kapfer 		 * without touching the pad surface) we assume former.
9181d9f2626SSebastian Kapfer 		 * Even if we are wrong the wost thing that would happen
9191d9f2626SSebastian Kapfer 		 * the cursor would jump but we should not get protocol
920b5d21704SDmitry Torokhov 		 * de-synchronization.
9211d9f2626SSebastian Kapfer 		 */
9221d9f2626SSebastian Kapfer 
9231d9f2626SSebastian Kapfer 		alps_report_bare_ps2_packet(psmouse, &psmouse->packet[3],
9241d9f2626SSebastian Kapfer 					    false);
9251d9f2626SSebastian Kapfer 
9261d9f2626SSebastian Kapfer 		/*
9271d9f2626SSebastian Kapfer 		 * Continue with the standard ALPS protocol handling,
9281d9f2626SSebastian Kapfer 		 * but make sure we won't process it as an interleaved
9291d9f2626SSebastian Kapfer 		 * packet again, which may happen if all buttons are
9301d9f2626SSebastian Kapfer 		 * pressed. To avoid this let's reset the 4th bit which
9311d9f2626SSebastian Kapfer 		 * is normally 1.
9321d9f2626SSebastian Kapfer 		 */
9331d9f2626SSebastian Kapfer 		psmouse->packet[3] = psmouse->packet[6] & 0xf7;
9341d9f2626SSebastian Kapfer 		psmouse->pktcnt = 4;
9351d9f2626SSebastian Kapfer 	}
9361d9f2626SSebastian Kapfer 
9371d9f2626SSebastian Kapfer 	return PSMOUSE_GOOD_DATA;
9381d9f2626SSebastian Kapfer }
9391d9f2626SSebastian Kapfer 
9401d9f2626SSebastian Kapfer static void alps_flush_packet(unsigned long data)
9411d9f2626SSebastian Kapfer {
9421d9f2626SSebastian Kapfer 	struct psmouse *psmouse = (struct psmouse *)data;
94324af5cb9SKevin Cernekee 	struct alps_data *priv = psmouse->private;
9441d9f2626SSebastian Kapfer 
9451d9f2626SSebastian Kapfer 	serio_pause_rx(psmouse->ps2dev.serio);
9461d9f2626SSebastian Kapfer 
947b46615feSSeth Forshee 	if (psmouse->pktcnt == psmouse->pktsize) {
9481d9f2626SSebastian Kapfer 
9491d9f2626SSebastian Kapfer 		/*
9501d9f2626SSebastian Kapfer 		 * We did not any more data in reasonable amount of time.
9511d9f2626SSebastian Kapfer 		 * Validate the last 3 bytes and process as a standard
9521d9f2626SSebastian Kapfer 		 * ALPS packet.
9531d9f2626SSebastian Kapfer 		 */
9541d9f2626SSebastian Kapfer 		if ((psmouse->packet[3] |
9551d9f2626SSebastian Kapfer 		     psmouse->packet[4] |
9561d9f2626SSebastian Kapfer 		     psmouse->packet[5]) & 0x80) {
957b5d21704SDmitry Torokhov 			psmouse_dbg(psmouse,
9583b112923SAndy Shevchenko 				    "refusing packet %3ph (suspected interleaved ps/2)\n",
9593b112923SAndy Shevchenko 				    psmouse->packet + 3);
9601d9f2626SSebastian Kapfer 		} else {
96124af5cb9SKevin Cernekee 			priv->process_packet(psmouse);
9621d9f2626SSebastian Kapfer 		}
9631d9f2626SSebastian Kapfer 		psmouse->pktcnt = 0;
9641d9f2626SSebastian Kapfer 	}
9651d9f2626SSebastian Kapfer 
9661d9f2626SSebastian Kapfer 	serio_continue_rx(psmouse->ps2dev.serio);
9671d9f2626SSebastian Kapfer }
9681d9f2626SSebastian Kapfer 
9691d9f2626SSebastian Kapfer static psmouse_ret_t alps_process_byte(struct psmouse *psmouse)
9701d9f2626SSebastian Kapfer {
9711d9f2626SSebastian Kapfer 	struct alps_data *priv = psmouse->private;
9721d9f2626SSebastian Kapfer 
9731da177e4SLinus Torvalds 	if ((psmouse->packet[0] & 0xc8) == 0x08) { /* PS/2 packet */
9741da177e4SLinus Torvalds 		if (psmouse->pktcnt == 3) {
9751d9f2626SSebastian Kapfer 			alps_report_bare_ps2_packet(psmouse, psmouse->packet,
9761d9f2626SSebastian Kapfer 						    true);
9771da177e4SLinus Torvalds 			return PSMOUSE_FULL_PACKET;
9781da177e4SLinus Torvalds 		}
9791da177e4SLinus Torvalds 		return PSMOUSE_GOOD_DATA;
9801da177e4SLinus Torvalds 	}
9811da177e4SLinus Torvalds 
9821d9f2626SSebastian Kapfer 	/* Check for PS/2 packet stuffed in the middle of ALPS packet. */
9831d9f2626SSebastian Kapfer 
98499df65e7SKevin Cernekee 	if ((priv->flags & ALPS_PS2_INTERLEAVED) &&
9851d9f2626SSebastian Kapfer 	    psmouse->pktcnt >= 4 && (psmouse->packet[3] & 0x0f) == 0x0f) {
9861d9f2626SSebastian Kapfer 		return alps_handle_interleaved_ps2(psmouse);
9871d9f2626SSebastian Kapfer 	}
9881d9f2626SSebastian Kapfer 
98999df65e7SKevin Cernekee 	if (!alps_is_valid_first_byte(priv, psmouse->packet[0])) {
990b5d21704SDmitry Torokhov 		psmouse_dbg(psmouse,
991b5d21704SDmitry Torokhov 			    "refusing packet[0] = %x (mask0 = %x, byte0 = %x)\n",
99299df65e7SKevin Cernekee 			    psmouse->packet[0], priv->mask0, priv->byte0);
9931da177e4SLinus Torvalds 		return PSMOUSE_BAD_DATA;
9941d9f2626SSebastian Kapfer 	}
9951da177e4SLinus Torvalds 
996b46615feSSeth Forshee 	/* Bytes 2 - pktsize should have 0 in the highest bit */
99795f75e91SYunkang Tang 	if ((priv->proto_version < ALPS_PROTO_V5) &&
99875af9e56SDave Turvene 	    psmouse->pktcnt >= 2 && psmouse->pktcnt <= psmouse->pktsize &&
9991d9f2626SSebastian Kapfer 	    (psmouse->packet[psmouse->pktcnt - 1] & 0x80)) {
1000b5d21704SDmitry Torokhov 		psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
1001b5d21704SDmitry Torokhov 			    psmouse->pktcnt - 1,
1002b5d21704SDmitry Torokhov 			    psmouse->packet[psmouse->pktcnt - 1]);
10031da177e4SLinus Torvalds 		return PSMOUSE_BAD_DATA;
10041d9f2626SSebastian Kapfer 	}
10051da177e4SLinus Torvalds 
1006b46615feSSeth Forshee 	if (psmouse->pktcnt == psmouse->pktsize) {
100724af5cb9SKevin Cernekee 		priv->process_packet(psmouse);
10081da177e4SLinus Torvalds 		return PSMOUSE_FULL_PACKET;
10091da177e4SLinus Torvalds 	}
10101da177e4SLinus Torvalds 
10111da177e4SLinus Torvalds 	return PSMOUSE_GOOD_DATA;
10121da177e4SLinus Torvalds }
10131da177e4SLinus Torvalds 
101425bded7cSSeth Forshee static int alps_command_mode_send_nibble(struct psmouse *psmouse, int nibble)
101525bded7cSSeth Forshee {
101625bded7cSSeth Forshee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
101725bded7cSSeth Forshee 	struct alps_data *priv = psmouse->private;
101825bded7cSSeth Forshee 	int command;
101925bded7cSSeth Forshee 	unsigned char *param;
102025bded7cSSeth Forshee 	unsigned char dummy[4];
102125bded7cSSeth Forshee 
102225bded7cSSeth Forshee 	BUG_ON(nibble > 0xf);
102325bded7cSSeth Forshee 
102425bded7cSSeth Forshee 	command = priv->nibble_commands[nibble].command;
102525bded7cSSeth Forshee 	param = (command & 0x0f00) ?
102625bded7cSSeth Forshee 		dummy : (unsigned char *)&priv->nibble_commands[nibble].data;
102725bded7cSSeth Forshee 
102825bded7cSSeth Forshee 	if (ps2_command(ps2dev, param, command))
102925bded7cSSeth Forshee 		return -1;
103025bded7cSSeth Forshee 
103125bded7cSSeth Forshee 	return 0;
103225bded7cSSeth Forshee }
103325bded7cSSeth Forshee 
103425bded7cSSeth Forshee static int alps_command_mode_set_addr(struct psmouse *psmouse, int addr)
103525bded7cSSeth Forshee {
103625bded7cSSeth Forshee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
103725bded7cSSeth Forshee 	struct alps_data *priv = psmouse->private;
103825bded7cSSeth Forshee 	int i, nibble;
103925bded7cSSeth Forshee 
104025bded7cSSeth Forshee 	if (ps2_command(ps2dev, NULL, priv->addr_command))
104125bded7cSSeth Forshee 		return -1;
104225bded7cSSeth Forshee 
104325bded7cSSeth Forshee 	for (i = 12; i >= 0; i -= 4) {
104425bded7cSSeth Forshee 		nibble = (addr >> i) & 0xf;
104525bded7cSSeth Forshee 		if (alps_command_mode_send_nibble(psmouse, nibble))
104625bded7cSSeth Forshee 			return -1;
104725bded7cSSeth Forshee 	}
104825bded7cSSeth Forshee 
104925bded7cSSeth Forshee 	return 0;
105025bded7cSSeth Forshee }
105125bded7cSSeth Forshee 
105225bded7cSSeth Forshee static int __alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
105325bded7cSSeth Forshee {
105425bded7cSSeth Forshee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
105525bded7cSSeth Forshee 	unsigned char param[4];
105625bded7cSSeth Forshee 
105725bded7cSSeth Forshee 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
105825bded7cSSeth Forshee 		return -1;
105925bded7cSSeth Forshee 
106025bded7cSSeth Forshee 	/*
106125bded7cSSeth Forshee 	 * The address being read is returned in the first two bytes
106225bded7cSSeth Forshee 	 * of the result. Check that this address matches the expected
106325bded7cSSeth Forshee 	 * address.
106425bded7cSSeth Forshee 	 */
106525bded7cSSeth Forshee 	if (addr != ((param[0] << 8) | param[1]))
106625bded7cSSeth Forshee 		return -1;
106725bded7cSSeth Forshee 
106825bded7cSSeth Forshee 	return param[2];
106925bded7cSSeth Forshee }
107025bded7cSSeth Forshee 
107125bded7cSSeth Forshee static int alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
107225bded7cSSeth Forshee {
107325bded7cSSeth Forshee 	if (alps_command_mode_set_addr(psmouse, addr))
107425bded7cSSeth Forshee 		return -1;
107525bded7cSSeth Forshee 	return __alps_command_mode_read_reg(psmouse, addr);
107625bded7cSSeth Forshee }
107725bded7cSSeth Forshee 
107825bded7cSSeth Forshee static int __alps_command_mode_write_reg(struct psmouse *psmouse, u8 value)
107925bded7cSSeth Forshee {
108025bded7cSSeth Forshee 	if (alps_command_mode_send_nibble(psmouse, (value >> 4) & 0xf))
108125bded7cSSeth Forshee 		return -1;
108225bded7cSSeth Forshee 	if (alps_command_mode_send_nibble(psmouse, value & 0xf))
108325bded7cSSeth Forshee 		return -1;
108425bded7cSSeth Forshee 	return 0;
108525bded7cSSeth Forshee }
108625bded7cSSeth Forshee 
108725bded7cSSeth Forshee static int alps_command_mode_write_reg(struct psmouse *psmouse, int addr,
108825bded7cSSeth Forshee 				       u8 value)
108925bded7cSSeth Forshee {
109025bded7cSSeth Forshee 	if (alps_command_mode_set_addr(psmouse, addr))
109125bded7cSSeth Forshee 		return -1;
109225bded7cSSeth Forshee 	return __alps_command_mode_write_reg(psmouse, value);
109325bded7cSSeth Forshee }
109425bded7cSSeth Forshee 
109524ba9707SKevin Cernekee static int alps_rpt_cmd(struct psmouse *psmouse, int init_command,
109624ba9707SKevin Cernekee 			int repeated_command, unsigned char *param)
109724ba9707SKevin Cernekee {
109824ba9707SKevin Cernekee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
109924ba9707SKevin Cernekee 
110024ba9707SKevin Cernekee 	param[0] = 0;
110124ba9707SKevin Cernekee 	if (init_command && ps2_command(ps2dev, param, init_command))
110224ba9707SKevin Cernekee 		return -EIO;
110324ba9707SKevin Cernekee 
110424ba9707SKevin Cernekee 	if (ps2_command(ps2dev,  NULL, repeated_command) ||
110524ba9707SKevin Cernekee 	    ps2_command(ps2dev,  NULL, repeated_command) ||
110624ba9707SKevin Cernekee 	    ps2_command(ps2dev,  NULL, repeated_command))
110724ba9707SKevin Cernekee 		return -EIO;
110824ba9707SKevin Cernekee 
110924ba9707SKevin Cernekee 	param[0] = param[1] = param[2] = 0xff;
111024ba9707SKevin Cernekee 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
111124ba9707SKevin Cernekee 		return -EIO;
111224ba9707SKevin Cernekee 
111339fbe585SDmitry Torokhov 	psmouse_dbg(psmouse, "%2.2X report: %3ph\n",
111439fbe585SDmitry Torokhov 		    repeated_command, param);
111524ba9707SKevin Cernekee 	return 0;
111624ba9707SKevin Cernekee }
111724ba9707SKevin Cernekee 
1118d18e53fcSKevin Cernekee static int alps_enter_command_mode(struct psmouse *psmouse)
111925bded7cSSeth Forshee {
112025bded7cSSeth Forshee 	unsigned char param[4];
112125bded7cSSeth Forshee 
112224ba9707SKevin Cernekee 	if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_RESET_WRAP, param)) {
112325bded7cSSeth Forshee 		psmouse_err(psmouse, "failed to enter command mode\n");
112425bded7cSSeth Forshee 		return -1;
112525bded7cSSeth Forshee 	}
112625bded7cSSeth Forshee 
112775af9e56SDave Turvene 	if ((param[0] != 0x88 || (param[1] != 0x07 && param[1] != 0x08)) &&
112875af9e56SDave Turvene 	    param[0] != 0x73) {
112925bded7cSSeth Forshee 		psmouse_dbg(psmouse,
113024ba9707SKevin Cernekee 			    "unknown response while entering command mode\n");
113125bded7cSSeth Forshee 		return -1;
113225bded7cSSeth Forshee 	}
113325bded7cSSeth Forshee 	return 0;
113425bded7cSSeth Forshee }
113525bded7cSSeth Forshee 
113625bded7cSSeth Forshee static inline int alps_exit_command_mode(struct psmouse *psmouse)
113725bded7cSSeth Forshee {
113825bded7cSSeth Forshee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
113925bded7cSSeth Forshee 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM))
114025bded7cSSeth Forshee 		return -1;
114125bded7cSSeth Forshee 	return 0;
114225bded7cSSeth Forshee }
114325bded7cSSeth Forshee 
11441da177e4SLinus Torvalds /*
11451da177e4SLinus Torvalds  * For DualPoint devices select the device that should respond to
11461da177e4SLinus Torvalds  * subsequent commands. It looks like glidepad is behind stickpointer,
11471da177e4SLinus Torvalds  * I'd thought it would be other way around...
11481da177e4SLinus Torvalds  */
114925bded7cSSeth Forshee static int alps_passthrough_mode_v2(struct psmouse *psmouse, bool enable)
11501da177e4SLinus Torvalds {
11511da177e4SLinus Torvalds 	struct ps2dev *ps2dev = &psmouse->ps2dev;
11521da177e4SLinus Torvalds 	int cmd = enable ? PSMOUSE_CMD_SETSCALE21 : PSMOUSE_CMD_SETSCALE11;
11531da177e4SLinus Torvalds 
11541da177e4SLinus Torvalds 	if (ps2_command(ps2dev, NULL, cmd) ||
11551da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, cmd) ||
11561da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, cmd) ||
11571da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
11581da177e4SLinus Torvalds 		return -1;
11591da177e4SLinus Torvalds 
11601da177e4SLinus Torvalds 	/* we may get 3 more bytes, just ignore them */
1161c611763dSDmitry Torokhov 	ps2_drain(ps2dev, 3, 100);
11621da177e4SLinus Torvalds 
11631da177e4SLinus Torvalds 	return 0;
11641da177e4SLinus Torvalds }
11651da177e4SLinus Torvalds 
116625bded7cSSeth Forshee static int alps_absolute_mode_v1_v2(struct psmouse *psmouse)
11671da177e4SLinus Torvalds {
11681da177e4SLinus Torvalds 	struct ps2dev *ps2dev = &psmouse->ps2dev;
11691da177e4SLinus Torvalds 
11701da177e4SLinus Torvalds 	/* Try ALPS magic knock - 4 disable before enable */
11711da177e4SLinus Torvalds 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
11721da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
11731da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
11741da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
11751da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
11761da177e4SLinus Torvalds 		return -1;
11771da177e4SLinus Torvalds 
11781da177e4SLinus Torvalds 	/*
11791da177e4SLinus Torvalds 	 * Switch mouse to poll (remote) mode so motion data will not
11801da177e4SLinus Torvalds 	 * get in our way
11811da177e4SLinus Torvalds 	 */
11821da177e4SLinus Torvalds 	return ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETPOLL);
11831da177e4SLinus Torvalds }
11841da177e4SLinus Torvalds 
118595f75e91SYunkang Tang static int alps_monitor_mode_send_word(struct psmouse *psmouse, u16 word)
118695f75e91SYunkang Tang {
118795f75e91SYunkang Tang 	int i, nibble;
118895f75e91SYunkang Tang 
118995f75e91SYunkang Tang 	/*
119095f75e91SYunkang Tang 	 * b0-b11 are valid bits, send sequence is inverse.
119195f75e91SYunkang Tang 	 * e.g. when word = 0x0123, nibble send sequence is 3, 2, 1
119295f75e91SYunkang Tang 	 */
119395f75e91SYunkang Tang 	for (i = 0; i <= 8; i += 4) {
119495f75e91SYunkang Tang 		nibble = (word >> i) & 0xf;
119595f75e91SYunkang Tang 		if (alps_command_mode_send_nibble(psmouse, nibble))
119695f75e91SYunkang Tang 			return -1;
119795f75e91SYunkang Tang 	}
119895f75e91SYunkang Tang 
119995f75e91SYunkang Tang 	return 0;
120095f75e91SYunkang Tang }
120195f75e91SYunkang Tang 
120295f75e91SYunkang Tang static int alps_monitor_mode_write_reg(struct psmouse *psmouse,
120395f75e91SYunkang Tang 				       u16 addr, u16 value)
120495f75e91SYunkang Tang {
120595f75e91SYunkang Tang 	struct ps2dev *ps2dev = &psmouse->ps2dev;
120695f75e91SYunkang Tang 
120795f75e91SYunkang Tang 	/* 0x0A0 is the command to write the word */
120895f75e91SYunkang Tang 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE) ||
120995f75e91SYunkang Tang 	    alps_monitor_mode_send_word(psmouse, 0x0A0) ||
121095f75e91SYunkang Tang 	    alps_monitor_mode_send_word(psmouse, addr) ||
121195f75e91SYunkang Tang 	    alps_monitor_mode_send_word(psmouse, value) ||
121295f75e91SYunkang Tang 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
121395f75e91SYunkang Tang 		return -1;
121495f75e91SYunkang Tang 
121595f75e91SYunkang Tang 	return 0;
121695f75e91SYunkang Tang }
121795f75e91SYunkang Tang 
121895f75e91SYunkang Tang static int alps_monitor_mode(struct psmouse *psmouse, bool enable)
121995f75e91SYunkang Tang {
122095f75e91SYunkang Tang 	struct ps2dev *ps2dev = &psmouse->ps2dev;
122195f75e91SYunkang Tang 
122295f75e91SYunkang Tang 	if (enable) {
122395f75e91SYunkang Tang 		/* EC E9 F5 F5 E7 E6 E7 E9 to enter monitor mode */
122495f75e91SYunkang Tang 		if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
122595f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO) ||
122695f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
122795f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
122895f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
122995f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
123095f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
123195f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO))
123295f75e91SYunkang Tang 			return -1;
123395f75e91SYunkang Tang 	} else {
123495f75e91SYunkang Tang 		/* EC to exit monitor mode */
123595f75e91SYunkang Tang 		if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP))
123695f75e91SYunkang Tang 			return -1;
123795f75e91SYunkang Tang 	}
123895f75e91SYunkang Tang 
123995f75e91SYunkang Tang 	return 0;
124095f75e91SYunkang Tang }
124195f75e91SYunkang Tang 
124295f75e91SYunkang Tang static int alps_absolute_mode_v6(struct psmouse *psmouse)
124395f75e91SYunkang Tang {
124495f75e91SYunkang Tang 	u16 reg_val = 0x181;
124595f75e91SYunkang Tang 	int ret = -1;
124695f75e91SYunkang Tang 
124795f75e91SYunkang Tang 	/* enter monitor mode, to write the register */
124895f75e91SYunkang Tang 	if (alps_monitor_mode(psmouse, true))
124995f75e91SYunkang Tang 		return -1;
125095f75e91SYunkang Tang 
125195f75e91SYunkang Tang 	ret = alps_monitor_mode_write_reg(psmouse, 0x000, reg_val);
125295f75e91SYunkang Tang 
125395f75e91SYunkang Tang 	if (alps_monitor_mode(psmouse, false))
125495f75e91SYunkang Tang 		ret = -1;
125595f75e91SYunkang Tang 
125695f75e91SYunkang Tang 	return ret;
125795f75e91SYunkang Tang }
125895f75e91SYunkang Tang 
12591da177e4SLinus Torvalds static int alps_get_status(struct psmouse *psmouse, char *param)
12601da177e4SLinus Torvalds {
12611da177e4SLinus Torvalds 	/* Get status: 0xF5 0xF5 0xF5 0xE9 */
126224ba9707SKevin Cernekee 	if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_DISABLE, param))
12631da177e4SLinus Torvalds 		return -1;
12641da177e4SLinus Torvalds 
12651da177e4SLinus Torvalds 	return 0;
12661da177e4SLinus Torvalds }
12671da177e4SLinus Torvalds 
12681da177e4SLinus Torvalds /*
12691da177e4SLinus Torvalds  * Turn touchpad tapping on or off. The sequences are:
12701da177e4SLinus Torvalds  * 0xE9 0xF5 0xF5 0xF3 0x0A to enable,
12711da177e4SLinus Torvalds  * 0xE9 0xF5 0xF5 0xE8 0x00 to disable.
12721da177e4SLinus Torvalds  * My guess that 0xE9 (GetInfo) is here as a sync point.
12731da177e4SLinus Torvalds  * For models that also have stickpointer (DualPoints) its tapping
12741da177e4SLinus Torvalds  * is controlled separately (0xE6 0xE6 0xE6 0xF3 0x14|0x0A) but
12751da177e4SLinus Torvalds  * we don't fiddle with it.
12761da177e4SLinus Torvalds  */
12771da177e4SLinus Torvalds static int alps_tap_mode(struct psmouse *psmouse, int enable)
12781da177e4SLinus Torvalds {
12791da177e4SLinus Torvalds 	struct ps2dev *ps2dev = &psmouse->ps2dev;
12801da177e4SLinus Torvalds 	int cmd = enable ? PSMOUSE_CMD_SETRATE : PSMOUSE_CMD_SETRES;
12811da177e4SLinus Torvalds 	unsigned char tap_arg = enable ? 0x0A : 0x00;
12821da177e4SLinus Torvalds 	unsigned char param[4];
12831da177e4SLinus Torvalds 
12841da177e4SLinus Torvalds 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO) ||
12851da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
12861da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
12871da177e4SLinus Torvalds 	    ps2_command(ps2dev, &tap_arg, cmd))
12881da177e4SLinus Torvalds 		return -1;
12891da177e4SLinus Torvalds 
12901da177e4SLinus Torvalds 	if (alps_get_status(psmouse, param))
12911da177e4SLinus Torvalds 		return -1;
12921da177e4SLinus Torvalds 
12931da177e4SLinus Torvalds 	return 0;
12941da177e4SLinus Torvalds }
12951da177e4SLinus Torvalds 
1296f0d5c6f4SDmitry Torokhov /*
1297f0d5c6f4SDmitry Torokhov  * alps_poll() - poll the touchpad for current motion packet.
1298f0d5c6f4SDmitry Torokhov  * Used in resync.
1299f0d5c6f4SDmitry Torokhov  */
1300f0d5c6f4SDmitry Torokhov static int alps_poll(struct psmouse *psmouse)
1301f0d5c6f4SDmitry Torokhov {
1302f0d5c6f4SDmitry Torokhov 	struct alps_data *priv = psmouse->private;
1303b46615feSSeth Forshee 	unsigned char buf[sizeof(psmouse->packet)];
1304b7802c5cSDmitry Torokhov 	bool poll_failed;
1305f0d5c6f4SDmitry Torokhov 
130699df65e7SKevin Cernekee 	if (priv->flags & ALPS_PASS)
130725bded7cSSeth Forshee 		alps_passthrough_mode_v2(psmouse, true);
1308f0d5c6f4SDmitry Torokhov 
1309f0d5c6f4SDmitry Torokhov 	poll_failed = ps2_command(&psmouse->ps2dev, buf,
1310f0d5c6f4SDmitry Torokhov 				  PSMOUSE_CMD_POLL | (psmouse->pktsize << 8)) < 0;
1311f0d5c6f4SDmitry Torokhov 
131299df65e7SKevin Cernekee 	if (priv->flags & ALPS_PASS)
131325bded7cSSeth Forshee 		alps_passthrough_mode_v2(psmouse, false);
1314f0d5c6f4SDmitry Torokhov 
131599df65e7SKevin Cernekee 	if (poll_failed || (buf[0] & priv->mask0) != priv->byte0)
1316f0d5c6f4SDmitry Torokhov 		return -1;
1317f0d5c6f4SDmitry Torokhov 
1318f0d5c6f4SDmitry Torokhov 	if ((psmouse->badbyte & 0xc8) == 0x08) {
1319f0d5c6f4SDmitry Torokhov /*
1320f0d5c6f4SDmitry Torokhov  * Poll the track stick ...
1321f0d5c6f4SDmitry Torokhov  */
1322f0d5c6f4SDmitry Torokhov 		if (ps2_command(&psmouse->ps2dev, buf, PSMOUSE_CMD_POLL | (3 << 8)))
1323f0d5c6f4SDmitry Torokhov 			return -1;
1324f0d5c6f4SDmitry Torokhov 	}
1325f0d5c6f4SDmitry Torokhov 
1326f0d5c6f4SDmitry Torokhov 	memcpy(psmouse->packet, buf, sizeof(buf));
1327f0d5c6f4SDmitry Torokhov 	return 0;
1328f0d5c6f4SDmitry Torokhov }
1329f0d5c6f4SDmitry Torokhov 
133025bded7cSSeth Forshee static int alps_hw_init_v1_v2(struct psmouse *psmouse)
13311da177e4SLinus Torvalds {
13321da177e4SLinus Torvalds 	struct alps_data *priv = psmouse->private;
13331da177e4SLinus Torvalds 
133499df65e7SKevin Cernekee 	if ((priv->flags & ALPS_PASS) &&
133525bded7cSSeth Forshee 	    alps_passthrough_mode_v2(psmouse, true)) {
13361da177e4SLinus Torvalds 		return -1;
1337b7802c5cSDmitry Torokhov 	}
13381da177e4SLinus Torvalds 
1339b7802c5cSDmitry Torokhov 	if (alps_tap_mode(psmouse, true)) {
1340b5d21704SDmitry Torokhov 		psmouse_warn(psmouse, "Failed to enable hardware tapping\n");
13411da177e4SLinus Torvalds 		return -1;
1342963f626dSPeter Osterlund 	}
13431da177e4SLinus Torvalds 
134425bded7cSSeth Forshee 	if (alps_absolute_mode_v1_v2(psmouse)) {
1345b5d21704SDmitry Torokhov 		psmouse_err(psmouse, "Failed to enable absolute mode\n");
13461da177e4SLinus Torvalds 		return -1;
13471da177e4SLinus Torvalds 	}
13481da177e4SLinus Torvalds 
134999df65e7SKevin Cernekee 	if ((priv->flags & ALPS_PASS) &&
135025bded7cSSeth Forshee 	    alps_passthrough_mode_v2(psmouse, false)) {
13511da177e4SLinus Torvalds 		return -1;
1352b7802c5cSDmitry Torokhov 	}
13531da177e4SLinus Torvalds 
13541e0c5b12SDmitry Torokhov 	/* ALPS needs stream mode, otherwise it won't report any data */
13551e0c5b12SDmitry Torokhov 	if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)) {
1356b5d21704SDmitry Torokhov 		psmouse_err(psmouse, "Failed to enable stream mode\n");
13571e0c5b12SDmitry Torokhov 		return -1;
13581e0c5b12SDmitry Torokhov 	}
13591e0c5b12SDmitry Torokhov 
13601e0c5b12SDmitry Torokhov 	return 0;
13611e0c5b12SDmitry Torokhov }
13621e0c5b12SDmitry Torokhov 
136395f75e91SYunkang Tang static int alps_hw_init_v6(struct psmouse *psmouse)
136495f75e91SYunkang Tang {
136595f75e91SYunkang Tang 	unsigned char param[2] = {0xC8, 0x14};
136695f75e91SYunkang Tang 
136795f75e91SYunkang Tang 	/* Enter passthrough mode to let trackpoint enter 6byte raw mode */
136895f75e91SYunkang Tang 	if (alps_passthrough_mode_v2(psmouse, true))
136995f75e91SYunkang Tang 		return -1;
137095f75e91SYunkang Tang 
137195f75e91SYunkang Tang 	if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
137295f75e91SYunkang Tang 	    ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
137395f75e91SYunkang Tang 	    ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
137495f75e91SYunkang Tang 	    ps2_command(&psmouse->ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
137595f75e91SYunkang Tang 	    ps2_command(&psmouse->ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
137695f75e91SYunkang Tang 		return -1;
137795f75e91SYunkang Tang 
137895f75e91SYunkang Tang 	if (alps_passthrough_mode_v2(psmouse, false))
137995f75e91SYunkang Tang 		return -1;
138095f75e91SYunkang Tang 
138195f75e91SYunkang Tang 	if (alps_absolute_mode_v6(psmouse)) {
138295f75e91SYunkang Tang 		psmouse_err(psmouse, "Failed to enable absolute mode\n");
138395f75e91SYunkang Tang 		return -1;
138495f75e91SYunkang Tang 	}
138595f75e91SYunkang Tang 
138695f75e91SYunkang Tang 	return 0;
138795f75e91SYunkang Tang }
138895f75e91SYunkang Tang 
138925bded7cSSeth Forshee /*
1390cd401204SKevin Cernekee  * Enable or disable passthrough mode to the trackstick.
139125bded7cSSeth Forshee  */
1392cd401204SKevin Cernekee static int alps_passthrough_mode_v3(struct psmouse *psmouse,
1393cd401204SKevin Cernekee 				    int reg_base, bool enable)
139425bded7cSSeth Forshee {
1395cd401204SKevin Cernekee 	int reg_val, ret = -1;
139625bded7cSSeth Forshee 
1397d18e53fcSKevin Cernekee 	if (alps_enter_command_mode(psmouse))
139825bded7cSSeth Forshee 		return -1;
139925bded7cSSeth Forshee 
1400cd401204SKevin Cernekee 	reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x0008);
1401cd401204SKevin Cernekee 	if (reg_val == -1)
1402cd401204SKevin Cernekee 		goto error;
1403cd401204SKevin Cernekee 
140425bded7cSSeth Forshee 	if (enable)
140525bded7cSSeth Forshee 		reg_val |= 0x01;
140625bded7cSSeth Forshee 	else
140725bded7cSSeth Forshee 		reg_val &= ~0x01;
140825bded7cSSeth Forshee 
1409cd401204SKevin Cernekee 	ret = __alps_command_mode_write_reg(psmouse, reg_val);
141025bded7cSSeth Forshee 
1411cd401204SKevin Cernekee error:
1412cd401204SKevin Cernekee 	if (alps_exit_command_mode(psmouse))
1413cd401204SKevin Cernekee 		ret = -1;
1414cd401204SKevin Cernekee 	return ret;
141525bded7cSSeth Forshee }
141625bded7cSSeth Forshee 
141725bded7cSSeth Forshee /* Must be in command mode when calling this function */
141825bded7cSSeth Forshee static int alps_absolute_mode_v3(struct psmouse *psmouse)
141925bded7cSSeth Forshee {
142025bded7cSSeth Forshee 	int reg_val;
142125bded7cSSeth Forshee 
142225bded7cSSeth Forshee 	reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
142325bded7cSSeth Forshee 	if (reg_val == -1)
142425bded7cSSeth Forshee 		return -1;
142525bded7cSSeth Forshee 
142625bded7cSSeth Forshee 	reg_val |= 0x06;
142725bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, reg_val))
142825bded7cSSeth Forshee 		return -1;
142925bded7cSSeth Forshee 
143025bded7cSSeth Forshee 	return 0;
143125bded7cSSeth Forshee }
143225bded7cSSeth Forshee 
1433cd401204SKevin Cernekee static int alps_probe_trackstick_v3(struct psmouse *psmouse, int reg_base)
143425bded7cSSeth Forshee {
1435cd401204SKevin Cernekee 	int ret = -EIO, reg_val;
143625bded7cSSeth Forshee 
1437d18e53fcSKevin Cernekee 	if (alps_enter_command_mode(psmouse))
143825bded7cSSeth Forshee 		goto error;
143925bded7cSSeth Forshee 
1440cd401204SKevin Cernekee 	reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x08);
144125bded7cSSeth Forshee 	if (reg_val == -1)
144225bded7cSSeth Forshee 		goto error;
1443cd401204SKevin Cernekee 
1444cd401204SKevin Cernekee 	/* bit 7: trackstick is present */
1445cd401204SKevin Cernekee 	ret = reg_val & 0x80 ? 0 : -ENODEV;
1446cd401204SKevin Cernekee 
1447cd401204SKevin Cernekee error:
1448cd401204SKevin Cernekee 	alps_exit_command_mode(psmouse);
1449cd401204SKevin Cernekee 	return ret;
1450cd401204SKevin Cernekee }
1451cd401204SKevin Cernekee 
1452cd401204SKevin Cernekee static int alps_setup_trackstick_v3(struct psmouse *psmouse, int reg_base)
1453cd401204SKevin Cernekee {
1454cd401204SKevin Cernekee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
1455cd401204SKevin Cernekee 	int ret = 0;
1456cd401204SKevin Cernekee 	unsigned char param[4];
1457cd401204SKevin Cernekee 
1458cd401204SKevin Cernekee 	if (alps_passthrough_mode_v3(psmouse, reg_base, true))
1459cd401204SKevin Cernekee 		return -EIO;
146025bded7cSSeth Forshee 
146125bded7cSSeth Forshee 	/*
146225bded7cSSeth Forshee 	 * E7 report for the trackstick
146325bded7cSSeth Forshee 	 *
146425bded7cSSeth Forshee 	 * There have been reports of failures to seem to trace back
146525bded7cSSeth Forshee 	 * to the above trackstick check failing. When these occur
146625bded7cSSeth Forshee 	 * this E7 report fails, so when that happens we continue
146725bded7cSSeth Forshee 	 * with the assumption that there isn't a trackstick after
146825bded7cSSeth Forshee 	 * all.
146925bded7cSSeth Forshee 	 */
1470cd401204SKevin Cernekee 	if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_SETSCALE21, param)) {
147125bded7cSSeth Forshee 		psmouse_warn(psmouse, "trackstick E7 report failed\n");
1472cd401204SKevin Cernekee 		ret = -ENODEV;
147325bded7cSSeth Forshee 	} else {
147439fbe585SDmitry Torokhov 		psmouse_dbg(psmouse, "trackstick E7 report: %3ph\n", param);
147525bded7cSSeth Forshee 
147625bded7cSSeth Forshee 		/*
147725bded7cSSeth Forshee 		 * Not sure what this does, but it is absolutely
147825bded7cSSeth Forshee 		 * essential. Without it, the touchpad does not
147925bded7cSSeth Forshee 		 * work at all and the trackstick just emits normal
148025bded7cSSeth Forshee 		 * PS/2 packets.
148125bded7cSSeth Forshee 		 */
148225bded7cSSeth Forshee 		if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
148325bded7cSSeth Forshee 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
148425bded7cSSeth Forshee 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
148525bded7cSSeth Forshee 		    alps_command_mode_send_nibble(psmouse, 0x9) ||
148625bded7cSSeth Forshee 		    alps_command_mode_send_nibble(psmouse, 0x4)) {
148725bded7cSSeth Forshee 			psmouse_err(psmouse,
148825bded7cSSeth Forshee 				    "Error sending magic E6 sequence\n");
1489cd401204SKevin Cernekee 			ret = -EIO;
149025bded7cSSeth Forshee 			goto error;
149125bded7cSSeth Forshee 		}
149225bded7cSSeth Forshee 
1493cd401204SKevin Cernekee 		/*
1494cd401204SKevin Cernekee 		 * This ensures the trackstick packets are in the format
1495cd401204SKevin Cernekee 		 * supported by this driver. If bit 1 isn't set the packet
1496cd401204SKevin Cernekee 		 * format is different.
1497cd401204SKevin Cernekee 		 */
1498d18e53fcSKevin Cernekee 		if (alps_enter_command_mode(psmouse) ||
1499cd401204SKevin Cernekee 		    alps_command_mode_write_reg(psmouse,
1500cd401204SKevin Cernekee 						reg_base + 0x08, 0x82) ||
1501cd401204SKevin Cernekee 		    alps_exit_command_mode(psmouse))
1502cd401204SKevin Cernekee 			ret = -EIO;
1503cd401204SKevin Cernekee 	}
1504cd401204SKevin Cernekee 
1505cd401204SKevin Cernekee error:
1506cd401204SKevin Cernekee 	if (alps_passthrough_mode_v3(psmouse, reg_base, false))
1507cd401204SKevin Cernekee 		ret = -EIO;
1508cd401204SKevin Cernekee 
1509cd401204SKevin Cernekee 	return ret;
1510cd401204SKevin Cernekee }
1511cd401204SKevin Cernekee 
1512cd401204SKevin Cernekee static int alps_hw_init_v3(struct psmouse *psmouse)
1513cd401204SKevin Cernekee {
1514cd401204SKevin Cernekee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
1515cd401204SKevin Cernekee 	int reg_val;
1516cd401204SKevin Cernekee 	unsigned char param[4];
1517cd401204SKevin Cernekee 
1518cd401204SKevin Cernekee 	reg_val = alps_probe_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE);
1519cd401204SKevin Cernekee 	if (reg_val == -EIO)
1520cd401204SKevin Cernekee 		goto error;
152139fbe585SDmitry Torokhov 
1522cd401204SKevin Cernekee 	if (reg_val == 0 &&
1523cd401204SKevin Cernekee 	    alps_setup_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE) == -EIO)
1524cd401204SKevin Cernekee 		goto error;
1525cd401204SKevin Cernekee 
1526d18e53fcSKevin Cernekee 	if (alps_enter_command_mode(psmouse) ||
1527cd401204SKevin Cernekee 	    alps_absolute_mode_v3(psmouse)) {
152825bded7cSSeth Forshee 		psmouse_err(psmouse, "Failed to enter absolute mode\n");
152925bded7cSSeth Forshee 		goto error;
153025bded7cSSeth Forshee 	}
153125bded7cSSeth Forshee 
153225bded7cSSeth Forshee 	reg_val = alps_command_mode_read_reg(psmouse, 0x0006);
153325bded7cSSeth Forshee 	if (reg_val == -1)
153425bded7cSSeth Forshee 		goto error;
153525bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
153625bded7cSSeth Forshee 		goto error;
153725bded7cSSeth Forshee 
153825bded7cSSeth Forshee 	reg_val = alps_command_mode_read_reg(psmouse, 0x0007);
153925bded7cSSeth Forshee 	if (reg_val == -1)
154025bded7cSSeth Forshee 		goto error;
154125bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
154225bded7cSSeth Forshee 		goto error;
154325bded7cSSeth Forshee 
154425bded7cSSeth Forshee 	if (alps_command_mode_read_reg(psmouse, 0x0144) == -1)
154525bded7cSSeth Forshee 		goto error;
154625bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, 0x04))
154725bded7cSSeth Forshee 		goto error;
154825bded7cSSeth Forshee 
154925bded7cSSeth Forshee 	if (alps_command_mode_read_reg(psmouse, 0x0159) == -1)
155025bded7cSSeth Forshee 		goto error;
155125bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, 0x03))
155225bded7cSSeth Forshee 		goto error;
155325bded7cSSeth Forshee 
155425bded7cSSeth Forshee 	if (alps_command_mode_read_reg(psmouse, 0x0163) == -1)
155525bded7cSSeth Forshee 		goto error;
155625bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0163, 0x03))
155725bded7cSSeth Forshee 		goto error;
155825bded7cSSeth Forshee 
155925bded7cSSeth Forshee 	if (alps_command_mode_read_reg(psmouse, 0x0162) == -1)
156025bded7cSSeth Forshee 		goto error;
156125bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0162, 0x04))
156225bded7cSSeth Forshee 		goto error;
156325bded7cSSeth Forshee 
156425bded7cSSeth Forshee 	alps_exit_command_mode(psmouse);
156525bded7cSSeth Forshee 
156625bded7cSSeth Forshee 	/* Set rate and enable data reporting */
156725bded7cSSeth Forshee 	param[0] = 0x64;
156825bded7cSSeth Forshee 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
156925bded7cSSeth Forshee 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
157025bded7cSSeth Forshee 		psmouse_err(psmouse, "Failed to enable data reporting\n");
157125bded7cSSeth Forshee 		return -1;
157225bded7cSSeth Forshee 	}
157325bded7cSSeth Forshee 
157425bded7cSSeth Forshee 	return 0;
157525bded7cSSeth Forshee 
157625bded7cSSeth Forshee error:
157725bded7cSSeth Forshee 	/*
157825bded7cSSeth Forshee 	 * Leaving the touchpad in command mode will essentially render
157925bded7cSSeth Forshee 	 * it unusable until the machine reboots, so exit it here just
158025bded7cSSeth Forshee 	 * to be safe
158125bded7cSSeth Forshee 	 */
158225bded7cSSeth Forshee 	alps_exit_command_mode(psmouse);
158325bded7cSSeth Forshee 	return -1;
158425bded7cSSeth Forshee }
158525bded7cSSeth Forshee 
15861302bac3SKevin Cernekee static int alps_hw_init_rushmore_v3(struct psmouse *psmouse)
15871302bac3SKevin Cernekee {
1588cd401204SKevin Cernekee 	struct alps_data *priv = psmouse->private;
15891302bac3SKevin Cernekee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
15901302bac3SKevin Cernekee 	int reg_val, ret = -1;
15911302bac3SKevin Cernekee 
1592cd401204SKevin Cernekee 	if (priv->flags & ALPS_DUALPOINT) {
1593cd401204SKevin Cernekee 		reg_val = alps_setup_trackstick_v3(psmouse,
1594cd401204SKevin Cernekee 						   ALPS_REG_BASE_RUSHMORE);
1595cd401204SKevin Cernekee 		if (reg_val == -EIO)
1596cd401204SKevin Cernekee 			goto error;
1597cd401204SKevin Cernekee 		if (reg_val == -ENODEV)
1598cd401204SKevin Cernekee 			priv->flags &= ~ALPS_DUALPOINT;
1599cd401204SKevin Cernekee 	}
1600cd401204SKevin Cernekee 
1601d18e53fcSKevin Cernekee 	if (alps_enter_command_mode(psmouse) ||
16021302bac3SKevin Cernekee 	    alps_command_mode_read_reg(psmouse, 0xc2d9) == -1 ||
16031302bac3SKevin Cernekee 	    alps_command_mode_write_reg(psmouse, 0xc2cb, 0x00))
16041302bac3SKevin Cernekee 		goto error;
16051302bac3SKevin Cernekee 
16061302bac3SKevin Cernekee 	reg_val = alps_command_mode_read_reg(psmouse, 0xc2c6);
16071302bac3SKevin Cernekee 	if (reg_val == -1)
16081302bac3SKevin Cernekee 		goto error;
16091302bac3SKevin Cernekee 	if (__alps_command_mode_write_reg(psmouse, reg_val & 0xfd))
16101302bac3SKevin Cernekee 		goto error;
16111302bac3SKevin Cernekee 
16121302bac3SKevin Cernekee 	if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
16131302bac3SKevin Cernekee 		goto error;
16141302bac3SKevin Cernekee 
16151302bac3SKevin Cernekee 	/* enter absolute mode */
16161302bac3SKevin Cernekee 	reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
16171302bac3SKevin Cernekee 	if (reg_val == -1)
16181302bac3SKevin Cernekee 		goto error;
16191302bac3SKevin Cernekee 	if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
16201302bac3SKevin Cernekee 		goto error;
16211302bac3SKevin Cernekee 
16221302bac3SKevin Cernekee 	alps_exit_command_mode(psmouse);
16231302bac3SKevin Cernekee 	return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
16241302bac3SKevin Cernekee 
16251302bac3SKevin Cernekee error:
16261302bac3SKevin Cernekee 	alps_exit_command_mode(psmouse);
16271302bac3SKevin Cernekee 	return ret;
16281302bac3SKevin Cernekee }
16291302bac3SKevin Cernekee 
163025bded7cSSeth Forshee /* Must be in command mode when calling this function */
163125bded7cSSeth Forshee static int alps_absolute_mode_v4(struct psmouse *psmouse)
163225bded7cSSeth Forshee {
163325bded7cSSeth Forshee 	int reg_val;
163425bded7cSSeth Forshee 
163525bded7cSSeth Forshee 	reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
163625bded7cSSeth Forshee 	if (reg_val == -1)
163725bded7cSSeth Forshee 		return -1;
163825bded7cSSeth Forshee 
163925bded7cSSeth Forshee 	reg_val |= 0x02;
164025bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, reg_val))
164125bded7cSSeth Forshee 		return -1;
164225bded7cSSeth Forshee 
164325bded7cSSeth Forshee 	return 0;
164425bded7cSSeth Forshee }
164525bded7cSSeth Forshee 
164625bded7cSSeth Forshee static int alps_hw_init_v4(struct psmouse *psmouse)
164725bded7cSSeth Forshee {
164825bded7cSSeth Forshee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
164925bded7cSSeth Forshee 	unsigned char param[4];
165025bded7cSSeth Forshee 
1651d18e53fcSKevin Cernekee 	if (alps_enter_command_mode(psmouse))
165225bded7cSSeth Forshee 		goto error;
165325bded7cSSeth Forshee 
165425bded7cSSeth Forshee 	if (alps_absolute_mode_v4(psmouse)) {
165525bded7cSSeth Forshee 		psmouse_err(psmouse, "Failed to enter absolute mode\n");
165625bded7cSSeth Forshee 		goto error;
165725bded7cSSeth Forshee 	}
165825bded7cSSeth Forshee 
165925bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0007, 0x8c))
166025bded7cSSeth Forshee 		goto error;
166125bded7cSSeth Forshee 
166225bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0149, 0x03))
166325bded7cSSeth Forshee 		goto error;
166425bded7cSSeth Forshee 
166525bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0160, 0x03))
166625bded7cSSeth Forshee 		goto error;
166725bded7cSSeth Forshee 
166825bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x017f, 0x15))
166925bded7cSSeth Forshee 		goto error;
167025bded7cSSeth Forshee 
167125bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0151, 0x01))
167225bded7cSSeth Forshee 		goto error;
167325bded7cSSeth Forshee 
167425bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0168, 0x03))
167525bded7cSSeth Forshee 		goto error;
167625bded7cSSeth Forshee 
167725bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x014a, 0x03))
167825bded7cSSeth Forshee 		goto error;
167925bded7cSSeth Forshee 
168025bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0161, 0x03))
168125bded7cSSeth Forshee 		goto error;
168225bded7cSSeth Forshee 
168325bded7cSSeth Forshee 	alps_exit_command_mode(psmouse);
168425bded7cSSeth Forshee 
168525bded7cSSeth Forshee 	/*
168625bded7cSSeth Forshee 	 * This sequence changes the output from a 9-byte to an
168725bded7cSSeth Forshee 	 * 8-byte format. All the same data seems to be present,
168825bded7cSSeth Forshee 	 * just in a more compact format.
168925bded7cSSeth Forshee 	 */
169025bded7cSSeth Forshee 	param[0] = 0xc8;
169125bded7cSSeth Forshee 	param[1] = 0x64;
169225bded7cSSeth Forshee 	param[2] = 0x50;
169325bded7cSSeth Forshee 	if (ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
169425bded7cSSeth Forshee 	    ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE) ||
169525bded7cSSeth Forshee 	    ps2_command(ps2dev, &param[2], PSMOUSE_CMD_SETRATE) ||
169625bded7cSSeth Forshee 	    ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
169725bded7cSSeth Forshee 		return -1;
169825bded7cSSeth Forshee 
169925bded7cSSeth Forshee 	/* Set rate and enable data reporting */
170025bded7cSSeth Forshee 	param[0] = 0x64;
170125bded7cSSeth Forshee 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
170225bded7cSSeth Forshee 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
170325bded7cSSeth Forshee 		psmouse_err(psmouse, "Failed to enable data reporting\n");
170425bded7cSSeth Forshee 		return -1;
170525bded7cSSeth Forshee 	}
170625bded7cSSeth Forshee 
170725bded7cSSeth Forshee 	return 0;
170825bded7cSSeth Forshee 
170925bded7cSSeth Forshee error:
171025bded7cSSeth Forshee 	/*
171125bded7cSSeth Forshee 	 * Leaving the touchpad in command mode will essentially render
171225bded7cSSeth Forshee 	 * it unusable until the machine reboots, so exit it here just
171325bded7cSSeth Forshee 	 * to be safe
171425bded7cSSeth Forshee 	 */
171525bded7cSSeth Forshee 	alps_exit_command_mode(psmouse);
171625bded7cSSeth Forshee 	return -1;
171725bded7cSSeth Forshee }
171825bded7cSSeth Forshee 
1719ee65d4b3SYunkang Tang static int alps_dolphin_get_device_area(struct psmouse *psmouse,
1720ee65d4b3SYunkang Tang 					struct alps_data *priv)
1721ee65d4b3SYunkang Tang {
1722ee65d4b3SYunkang Tang 	struct ps2dev *ps2dev = &psmouse->ps2dev;
1723ee65d4b3SYunkang Tang 	unsigned char param[4] = {0};
1724ee65d4b3SYunkang Tang 	int num_x_electrode, num_y_electrode;
1725ee65d4b3SYunkang Tang 
1726ee65d4b3SYunkang Tang 	if (alps_enter_command_mode(psmouse))
1727ee65d4b3SYunkang Tang 		return -1;
1728ee65d4b3SYunkang Tang 
1729ee65d4b3SYunkang Tang 	param[0] = 0x0a;
1730ee65d4b3SYunkang Tang 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
1731ee65d4b3SYunkang Tang 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
1732ee65d4b3SYunkang Tang 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
1733ee65d4b3SYunkang Tang 	    ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
1734ee65d4b3SYunkang Tang 	    ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE))
1735ee65d4b3SYunkang Tang 		return -1;
1736ee65d4b3SYunkang Tang 
1737ee65d4b3SYunkang Tang 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1738ee65d4b3SYunkang Tang 		return -1;
1739ee65d4b3SYunkang Tang 
1740ee65d4b3SYunkang Tang 	/*
1741ee65d4b3SYunkang Tang 	 * Dolphin's sensor line number is not fixed. It can be calculated
1742ee65d4b3SYunkang Tang 	 * by adding the device's register value with DOLPHIN_PROFILE_X/YOFFSET.
1743ee65d4b3SYunkang Tang 	 * Further more, we can get device's x_max and y_max by multiplying
1744ee65d4b3SYunkang Tang 	 * sensor line number with DOLPHIN_COUNT_PER_ELECTRODE.
1745ee65d4b3SYunkang Tang 	 *
1746ee65d4b3SYunkang Tang 	 * e.g. When we get register's sensor_x = 11 & sensor_y = 8,
1747ee65d4b3SYunkang Tang 	 *	real sensor line number X = 11 + 8 = 19, and
1748ee65d4b3SYunkang Tang 	 *	real sensor line number Y = 8 + 1 = 9.
1749ee65d4b3SYunkang Tang 	 *	So, x_max = (19 - 1) * 64 = 1152, and
1750ee65d4b3SYunkang Tang 	 *	    y_max = (9 - 1) * 64 = 512.
1751ee65d4b3SYunkang Tang 	 */
1752ee65d4b3SYunkang Tang 	num_x_electrode = DOLPHIN_PROFILE_XOFFSET + (param[2] & 0x0F);
1753ee65d4b3SYunkang Tang 	num_y_electrode = DOLPHIN_PROFILE_YOFFSET + ((param[2] >> 4) & 0x0F);
1754ee65d4b3SYunkang Tang 	priv->x_bits = num_x_electrode;
1755ee65d4b3SYunkang Tang 	priv->y_bits = num_y_electrode;
1756ee65d4b3SYunkang Tang 	priv->x_max = (num_x_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
1757ee65d4b3SYunkang Tang 	priv->y_max = (num_y_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
1758ee65d4b3SYunkang Tang 
1759ee65d4b3SYunkang Tang 	if (alps_exit_command_mode(psmouse))
1760ee65d4b3SYunkang Tang 		return -1;
1761ee65d4b3SYunkang Tang 
1762ee65d4b3SYunkang Tang 	return 0;
1763ee65d4b3SYunkang Tang }
1764ee65d4b3SYunkang Tang 
176575af9e56SDave Turvene static int alps_hw_init_dolphin_v1(struct psmouse *psmouse)
176675af9e56SDave Turvene {
176775af9e56SDave Turvene 	struct ps2dev *ps2dev = &psmouse->ps2dev;
176875af9e56SDave Turvene 	unsigned char param[2];
176975af9e56SDave Turvene 
177075af9e56SDave Turvene 	/* This is dolphin "v1" as empirically defined by florin9doi */
177175af9e56SDave Turvene 	param[0] = 0x64;
177275af9e56SDave Turvene 	param[1] = 0x28;
177375af9e56SDave Turvene 
177475af9e56SDave Turvene 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
177575af9e56SDave Turvene 	    ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
177675af9e56SDave Turvene 	    ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
177775af9e56SDave Turvene 		return -1;
177875af9e56SDave Turvene 
177975af9e56SDave Turvene 	return 0;
178075af9e56SDave Turvene }
178175af9e56SDave Turvene 
178224af5cb9SKevin Cernekee static void alps_set_defaults(struct alps_data *priv)
178325bded7cSSeth Forshee {
1784f673ceb1SKevin Cernekee 	priv->byte0 = 0x8f;
1785f673ceb1SKevin Cernekee 	priv->mask0 = 0x8f;
1786f673ceb1SKevin Cernekee 	priv->flags = ALPS_DUALPOINT;
1787f673ceb1SKevin Cernekee 
17887a9f73e7SKevin Cernekee 	priv->x_max = 2000;
17897a9f73e7SKevin Cernekee 	priv->y_max = 1400;
17907a9f73e7SKevin Cernekee 	priv->x_bits = 15;
17917a9f73e7SKevin Cernekee 	priv->y_bits = 11;
17927a9f73e7SKevin Cernekee 
179399df65e7SKevin Cernekee 	switch (priv->proto_version) {
179425bded7cSSeth Forshee 	case ALPS_PROTO_V1:
179525bded7cSSeth Forshee 	case ALPS_PROTO_V2:
179624af5cb9SKevin Cernekee 		priv->hw_init = alps_hw_init_v1_v2;
179724af5cb9SKevin Cernekee 		priv->process_packet = alps_process_packet_v1_v2;
179824af5cb9SKevin Cernekee 		priv->set_abs_params = alps_set_abs_params_st;
179995f75e91SYunkang Tang 		priv->x_max = 1023;
180095f75e91SYunkang Tang 		priv->y_max = 767;
180125bded7cSSeth Forshee 		break;
180225bded7cSSeth Forshee 	case ALPS_PROTO_V3:
180324af5cb9SKevin Cernekee 		priv->hw_init = alps_hw_init_v3;
180424af5cb9SKevin Cernekee 		priv->process_packet = alps_process_packet_v3;
180524af5cb9SKevin Cernekee 		priv->set_abs_params = alps_set_abs_params_mt;
1806f85e5001SKevin Cernekee 		priv->decode_fields = alps_decode_pinnacle;
180750e8b216SKevin Cernekee 		priv->nibble_commands = alps_v3_nibble_commands;
180850e8b216SKevin Cernekee 		priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
180925bded7cSSeth Forshee 		break;
181025bded7cSSeth Forshee 	case ALPS_PROTO_V4:
181124af5cb9SKevin Cernekee 		priv->hw_init = alps_hw_init_v4;
181224af5cb9SKevin Cernekee 		priv->process_packet = alps_process_packet_v4;
181324af5cb9SKevin Cernekee 		priv->set_abs_params = alps_set_abs_params_mt;
181450e8b216SKevin Cernekee 		priv->nibble_commands = alps_v4_nibble_commands;
181550e8b216SKevin Cernekee 		priv->addr_command = PSMOUSE_CMD_DISABLE;
181625bded7cSSeth Forshee 		break;
181775af9e56SDave Turvene 	case ALPS_PROTO_V5:
181875af9e56SDave Turvene 		priv->hw_init = alps_hw_init_dolphin_v1;
1819ee65d4b3SYunkang Tang 		priv->process_packet = alps_process_touchpad_packet_v3_v5;
182075af9e56SDave Turvene 		priv->decode_fields = alps_decode_dolphin;
182175af9e56SDave Turvene 		priv->set_abs_params = alps_set_abs_params_mt;
182275af9e56SDave Turvene 		priv->nibble_commands = alps_v3_nibble_commands;
182375af9e56SDave Turvene 		priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
182475af9e56SDave Turvene 		priv->byte0 = 0xc8;
1825ee65d4b3SYunkang Tang 		priv->mask0 = 0xd8;
182675af9e56SDave Turvene 		priv->flags = 0;
182775af9e56SDave Turvene 		priv->x_max = 1360;
182875af9e56SDave Turvene 		priv->y_max = 660;
182975af9e56SDave Turvene 		priv->x_bits = 23;
183075af9e56SDave Turvene 		priv->y_bits = 12;
183175af9e56SDave Turvene 		break;
183295f75e91SYunkang Tang 	case ALPS_PROTO_V6:
183395f75e91SYunkang Tang 		priv->hw_init = alps_hw_init_v6;
183495f75e91SYunkang Tang 		priv->process_packet = alps_process_packet_v6;
183595f75e91SYunkang Tang 		priv->set_abs_params = alps_set_abs_params_st;
183695f75e91SYunkang Tang 		priv->nibble_commands = alps_v6_nibble_commands;
183795f75e91SYunkang Tang 		priv->x_max = 2047;
183895f75e91SYunkang Tang 		priv->y_max = 1535;
183995f75e91SYunkang Tang 		break;
184025bded7cSSeth Forshee 	}
184125bded7cSSeth Forshee }
184225bded7cSSeth Forshee 
1843b5d6b851SKevin Cernekee static int alps_match_table(struct psmouse *psmouse, struct alps_data *priv,
1844b5d6b851SKevin Cernekee 			    unsigned char *e7, unsigned char *ec)
18452e992cc0SKevin Cernekee {
1846b5d6b851SKevin Cernekee 	const struct alps_model_info *model;
18472e992cc0SKevin Cernekee 	int i;
18482e992cc0SKevin Cernekee 
1849b5d6b851SKevin Cernekee 	for (i = 0; i < ARRAY_SIZE(alps_model_data); i++) {
1850b5d6b851SKevin Cernekee 		model = &alps_model_data[i];
1851b5d6b851SKevin Cernekee 
1852b5d6b851SKevin Cernekee 		if (!memcmp(e7, model->signature, sizeof(model->signature)) &&
1853b5d6b851SKevin Cernekee 		    (!model->command_mode_resp ||
1854b5d6b851SKevin Cernekee 		     model->command_mode_resp == ec[2])) {
1855b5d6b851SKevin Cernekee 
1856b5d6b851SKevin Cernekee 			priv->proto_version = model->proto_version;
185724af5cb9SKevin Cernekee 			alps_set_defaults(priv);
185824af5cb9SKevin Cernekee 
1859b5d6b851SKevin Cernekee 			priv->flags = model->flags;
1860b5d6b851SKevin Cernekee 			priv->byte0 = model->byte0;
1861b5d6b851SKevin Cernekee 			priv->mask0 = model->mask0;
1862b5d6b851SKevin Cernekee 
1863b5d6b851SKevin Cernekee 			return 0;
1864b5d6b851SKevin Cernekee 		}
1865b5d6b851SKevin Cernekee 	}
1866b5d6b851SKevin Cernekee 
1867b5d6b851SKevin Cernekee 	return -EINVAL;
1868b5d6b851SKevin Cernekee }
1869b5d6b851SKevin Cernekee 
1870b5d6b851SKevin Cernekee static int alps_identify(struct psmouse *psmouse, struct alps_data *priv)
1871b5d6b851SKevin Cernekee {
1872b5d6b851SKevin Cernekee 	unsigned char e6[4], e7[4], ec[4];
1873b5d6b851SKevin Cernekee 
18742e992cc0SKevin Cernekee 	/*
18752e992cc0SKevin Cernekee 	 * First try "E6 report".
18762e992cc0SKevin Cernekee 	 * ALPS should return 0,0,10 or 0,0,100 if no buttons are pressed.
18772e992cc0SKevin Cernekee 	 * The bits 0-2 of the first byte will be 1s if some buttons are
18782e992cc0SKevin Cernekee 	 * pressed.
18792e992cc0SKevin Cernekee 	 */
1880b5d6b851SKevin Cernekee 	if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
1881b5d6b851SKevin Cernekee 			 PSMOUSE_CMD_SETSCALE11, e6))
1882b5d6b851SKevin Cernekee 		return -EIO;
18832e992cc0SKevin Cernekee 
1884b5d6b851SKevin Cernekee 	if ((e6[0] & 0xf8) != 0 || e6[1] != 0 || (e6[2] != 10 && e6[2] != 100))
1885b5d6b851SKevin Cernekee 		return -EINVAL;
18862e992cc0SKevin Cernekee 
18872e992cc0SKevin Cernekee 	/*
1888b5d6b851SKevin Cernekee 	 * Now get the "E7" and "EC" reports.  These will uniquely identify
1889b5d6b851SKevin Cernekee 	 * most ALPS touchpads.
18902e992cc0SKevin Cernekee 	 */
1891b5d6b851SKevin Cernekee 	if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
1892b5d6b851SKevin Cernekee 			 PSMOUSE_CMD_SETSCALE21, e7) ||
1893b5d6b851SKevin Cernekee 	    alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
1894b5d6b851SKevin Cernekee 			 PSMOUSE_CMD_RESET_WRAP, ec) ||
1895b5d6b851SKevin Cernekee 	    alps_exit_command_mode(psmouse))
1896b5d6b851SKevin Cernekee 		return -EIO;
18972e992cc0SKevin Cernekee 
1898f673ceb1SKevin Cernekee 	if (alps_match_table(psmouse, priv, e7, ec) == 0) {
1899b5d6b851SKevin Cernekee 		return 0;
190075af9e56SDave Turvene 	} else if (e7[0] == 0x73 && e7[1] == 0x03 && e7[2] == 0x50 &&
1901ee65d4b3SYunkang Tang 		   ec[0] == 0x73 && (ec[1] == 0x01 || ec[1] == 0x02)) {
190275af9e56SDave Turvene 		priv->proto_version = ALPS_PROTO_V5;
190375af9e56SDave Turvene 		alps_set_defaults(priv);
1904ee65d4b3SYunkang Tang 		if (alps_dolphin_get_device_area(psmouse, priv))
1905ee65d4b3SYunkang Tang 			return -EIO;
1906ee65d4b3SYunkang Tang 		else
190775af9e56SDave Turvene 			return 0;
19081302bac3SKevin Cernekee 	} else if (ec[0] == 0x88 && ec[1] == 0x08) {
19091302bac3SKevin Cernekee 		priv->proto_version = ALPS_PROTO_V3;
19101302bac3SKevin Cernekee 		alps_set_defaults(priv);
19111302bac3SKevin Cernekee 
19121302bac3SKevin Cernekee 		priv->hw_init = alps_hw_init_rushmore_v3;
19131302bac3SKevin Cernekee 		priv->decode_fields = alps_decode_rushmore;
19141302bac3SKevin Cernekee 		priv->x_bits = 16;
19151302bac3SKevin Cernekee 		priv->y_bits = 12;
191640e8f53bSHans de Goede 		priv->flags |= ALPS_IS_RUSHMORE;
19171302bac3SKevin Cernekee 
1918cd401204SKevin Cernekee 		/* hack to make addr_command, nibble_command available */
1919cd401204SKevin Cernekee 		psmouse->private = priv;
1920cd401204SKevin Cernekee 
1921cd401204SKevin Cernekee 		if (alps_probe_trackstick_v3(psmouse, ALPS_REG_BASE_RUSHMORE))
1922cd401204SKevin Cernekee 			priv->flags &= ~ALPS_DUALPOINT;
1923cd401204SKevin Cernekee 
19241302bac3SKevin Cernekee 		return 0;
1925f673ceb1SKevin Cernekee 	} else if (ec[0] == 0x88 && ec[1] == 0x07 &&
1926f673ceb1SKevin Cernekee 		   ec[2] >= 0x90 && ec[2] <= 0x9d) {
1927f673ceb1SKevin Cernekee 		priv->proto_version = ALPS_PROTO_V3;
1928f673ceb1SKevin Cernekee 		alps_set_defaults(priv);
1929f673ceb1SKevin Cernekee 
1930f673ceb1SKevin Cernekee 		return 0;
1931f673ceb1SKevin Cernekee 	}
19322e992cc0SKevin Cernekee 
1933b5d6b851SKevin Cernekee 	psmouse_info(psmouse,
193439fbe585SDmitry Torokhov 		     "Unknown ALPS touchpad: E7=%3ph, EC=%3ph\n", e7, ec);
19352e992cc0SKevin Cernekee 
1936b5d6b851SKevin Cernekee 	return -EINVAL;
19372e992cc0SKevin Cernekee }
19382e992cc0SKevin Cernekee 
19391e0c5b12SDmitry Torokhov static int alps_reconnect(struct psmouse *psmouse)
19401e0c5b12SDmitry Torokhov {
1941b5d6b851SKevin Cernekee 	struct alps_data *priv = psmouse->private;
194271bb21b6SMaxim Levitsky 
19431e0c5b12SDmitry Torokhov 	psmouse_reset(psmouse);
19441e0c5b12SDmitry Torokhov 
1945b5d6b851SKevin Cernekee 	if (alps_identify(psmouse, priv) < 0)
19461e0c5b12SDmitry Torokhov 		return -1;
19471e0c5b12SDmitry Torokhov 
194824af5cb9SKevin Cernekee 	return priv->hw_init(psmouse);
19491da177e4SLinus Torvalds }
19501da177e4SLinus Torvalds 
19511da177e4SLinus Torvalds static void alps_disconnect(struct psmouse *psmouse)
19521da177e4SLinus Torvalds {
19531da177e4SLinus Torvalds 	struct alps_data *priv = psmouse->private;
19542e5b636bSDmitry Torokhov 
19551da177e4SLinus Torvalds 	psmouse_reset(psmouse);
19561d9f2626SSebastian Kapfer 	del_timer_sync(&priv->timer);
19572e5b636bSDmitry Torokhov 	input_unregister_device(priv->dev2);
19581da177e4SLinus Torvalds 	kfree(priv);
19591da177e4SLinus Torvalds }
19601da177e4SLinus Torvalds 
196124af5cb9SKevin Cernekee static void alps_set_abs_params_st(struct alps_data *priv,
196224af5cb9SKevin Cernekee 				   struct input_dev *dev1)
196324af5cb9SKevin Cernekee {
196495f75e91SYunkang Tang 	input_set_abs_params(dev1, ABS_X, 0, priv->x_max, 0, 0);
196595f75e91SYunkang Tang 	input_set_abs_params(dev1, ABS_Y, 0, priv->y_max, 0, 0);
196624af5cb9SKevin Cernekee }
196724af5cb9SKevin Cernekee 
196824af5cb9SKevin Cernekee static void alps_set_abs_params_mt(struct alps_data *priv,
196924af5cb9SKevin Cernekee 				   struct input_dev *dev1)
197024af5cb9SKevin Cernekee {
19717a9f73e7SKevin Cernekee 	input_set_abs_params(dev1, ABS_MT_POSITION_X, 0, priv->x_max, 0, 0);
19727a9f73e7SKevin Cernekee 	input_set_abs_params(dev1, ABS_MT_POSITION_Y, 0, priv->y_max, 0, 0);
197324af5cb9SKevin Cernekee 
1974cdf333efSHans de Goede 	input_mt_init_slots(dev1, MAX_TOUCHES, INPUT_MT_POINTER |
1975cdf333efSHans de Goede 		INPUT_MT_DROP_UNUSED | INPUT_MT_TRACK | INPUT_MT_SEMI_MT);
1976cdf333efSHans de Goede 
197724af5cb9SKevin Cernekee 	set_bit(BTN_TOOL_TRIPLETAP, dev1->keybit);
197824af5cb9SKevin Cernekee 	set_bit(BTN_TOOL_QUADTAP, dev1->keybit);
197924af5cb9SKevin Cernekee }
198024af5cb9SKevin Cernekee 
19811da177e4SLinus Torvalds int alps_init(struct psmouse *psmouse)
19821da177e4SLinus Torvalds {
19831da177e4SLinus Torvalds 	struct alps_data *priv;
19842e5b636bSDmitry Torokhov 	struct input_dev *dev1 = psmouse->dev, *dev2;
19851da177e4SLinus Torvalds 
1986f42649e8SDmitry Torokhov 	priv = kzalloc(sizeof(struct alps_data), GFP_KERNEL);
19872e5b636bSDmitry Torokhov 	dev2 = input_allocate_device();
19882e5b636bSDmitry Torokhov 	if (!priv || !dev2)
19891da177e4SLinus Torvalds 		goto init_fail;
19902e5b636bSDmitry Torokhov 
19912e5b636bSDmitry Torokhov 	priv->dev2 = dev2;
19921d9f2626SSebastian Kapfer 	setup_timer(&priv->timer, alps_flush_packet, (unsigned long)psmouse);
19931d9f2626SSebastian Kapfer 
19941e0c5b12SDmitry Torokhov 	psmouse->private = priv;
19951da177e4SLinus Torvalds 
199625bded7cSSeth Forshee 	psmouse_reset(psmouse);
199725bded7cSSeth Forshee 
1998b5d6b851SKevin Cernekee 	if (alps_identify(psmouse, priv) < 0)
199971bb21b6SMaxim Levitsky 		goto init_fail;
200071bb21b6SMaxim Levitsky 
200124af5cb9SKevin Cernekee 	if (priv->hw_init(psmouse))
20021da177e4SLinus Torvalds 		goto init_fail;
20031da177e4SLinus Torvalds 
20047105d2eaSDmitry Torokhov 	/*
20057105d2eaSDmitry Torokhov 	 * Undo part of setup done for us by psmouse core since touchpad
20067105d2eaSDmitry Torokhov 	 * is not a relative device.
20077105d2eaSDmitry Torokhov 	 */
20087105d2eaSDmitry Torokhov 	__clear_bit(EV_REL, dev1->evbit);
20097105d2eaSDmitry Torokhov 	__clear_bit(REL_X, dev1->relbit);
20107105d2eaSDmitry Torokhov 	__clear_bit(REL_Y, dev1->relbit);
20117105d2eaSDmitry Torokhov 
20127105d2eaSDmitry Torokhov 	/*
20137105d2eaSDmitry Torokhov 	 * Now set up our capabilities.
20147105d2eaSDmitry Torokhov 	 */
20157b19ada2SJiri Slaby 	dev1->evbit[BIT_WORD(EV_KEY)] |= BIT_MASK(EV_KEY);
20167b19ada2SJiri Slaby 	dev1->keybit[BIT_WORD(BTN_TOUCH)] |= BIT_MASK(BTN_TOUCH);
20177b19ada2SJiri Slaby 	dev1->keybit[BIT_WORD(BTN_TOOL_FINGER)] |= BIT_MASK(BTN_TOOL_FINGER);
201871bb21b6SMaxim Levitsky 	dev1->keybit[BIT_WORD(BTN_LEFT)] |=
201971bb21b6SMaxim Levitsky 		BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
20201da177e4SLinus Torvalds 
20217b19ada2SJiri Slaby 	dev1->evbit[BIT_WORD(EV_ABS)] |= BIT_MASK(EV_ABS);
202225bded7cSSeth Forshee 
202324af5cb9SKevin Cernekee 	priv->set_abs_params(priv, dev1);
20242e5b636bSDmitry Torokhov 	input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
20251da177e4SLinus Torvalds 
202699df65e7SKevin Cernekee 	if (priv->flags & ALPS_WHEEL) {
20277b19ada2SJiri Slaby 		dev1->evbit[BIT_WORD(EV_REL)] |= BIT_MASK(EV_REL);
20287b19ada2SJiri Slaby 		dev1->relbit[BIT_WORD(REL_WHEEL)] |= BIT_MASK(REL_WHEEL);
20291da177e4SLinus Torvalds 	}
20301da177e4SLinus Torvalds 
203199df65e7SKevin Cernekee 	if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
20327b19ada2SJiri Slaby 		dev1->keybit[BIT_WORD(BTN_FORWARD)] |= BIT_MASK(BTN_FORWARD);
20337b19ada2SJiri Slaby 		dev1->keybit[BIT_WORD(BTN_BACK)] |= BIT_MASK(BTN_BACK);
20341da177e4SLinus Torvalds 	}
20351da177e4SLinus Torvalds 
203699df65e7SKevin Cernekee 	if (priv->flags & ALPS_FOUR_BUTTONS) {
203771bb21b6SMaxim Levitsky 		dev1->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_0);
203871bb21b6SMaxim Levitsky 		dev1->keybit[BIT_WORD(BTN_1)] |= BIT_MASK(BTN_1);
203971bb21b6SMaxim Levitsky 		dev1->keybit[BIT_WORD(BTN_2)] |= BIT_MASK(BTN_2);
204071bb21b6SMaxim Levitsky 		dev1->keybit[BIT_WORD(BTN_3)] |= BIT_MASK(BTN_3);
204171bb21b6SMaxim Levitsky 	} else {
204271bb21b6SMaxim Levitsky 		dev1->keybit[BIT_WORD(BTN_MIDDLE)] |= BIT_MASK(BTN_MIDDLE);
204371bb21b6SMaxim Levitsky 	}
204471bb21b6SMaxim Levitsky 
204508ffce45SDmitry Torokhov 	snprintf(priv->phys, sizeof(priv->phys), "%s/input1", psmouse->ps2dev.serio->phys);
20462e5b636bSDmitry Torokhov 	dev2->phys = priv->phys;
204799df65e7SKevin Cernekee 	dev2->name = (priv->flags & ALPS_DUALPOINT) ?
20489354f263SYunkang Tang 		     "DualPoint Stick" : "ALPS PS/2 Device";
20492e5b636bSDmitry Torokhov 	dev2->id.bustype = BUS_I8042;
20502e5b636bSDmitry Torokhov 	dev2->id.vendor  = 0x0002;
20512e5b636bSDmitry Torokhov 	dev2->id.product = PSMOUSE_ALPS;
20522e5b636bSDmitry Torokhov 	dev2->id.version = 0x0000;
20531db3a345SDmitry Torokhov 	dev2->dev.parent = &psmouse->ps2dev.serio->dev;
20541da177e4SLinus Torvalds 
20557b19ada2SJiri Slaby 	dev2->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
205671bb21b6SMaxim Levitsky 	dev2->relbit[BIT_WORD(REL_X)] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
205771bb21b6SMaxim Levitsky 	dev2->keybit[BIT_WORD(BTN_LEFT)] =
205871bb21b6SMaxim Levitsky 		BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_MIDDLE) | BIT_MASK(BTN_RIGHT);
20591da177e4SLinus Torvalds 
2060f42649e8SDmitry Torokhov 	if (input_register_device(priv->dev2))
2061f42649e8SDmitry Torokhov 		goto init_fail;
20621da177e4SLinus Torvalds 
20631da177e4SLinus Torvalds 	psmouse->protocol_handler = alps_process_byte;
2064f0d5c6f4SDmitry Torokhov 	psmouse->poll = alps_poll;
20651da177e4SLinus Torvalds 	psmouse->disconnect = alps_disconnect;
20661da177e4SLinus Torvalds 	psmouse->reconnect = alps_reconnect;
206799df65e7SKevin Cernekee 	psmouse->pktsize = priv->proto_version == ALPS_PROTO_V4 ? 8 : 6;
20681da177e4SLinus Torvalds 
2069f0d5c6f4SDmitry Torokhov 	/* We are having trouble resyncing ALPS touchpads so disable it for now */
2070f0d5c6f4SDmitry Torokhov 	psmouse->resync_time = 0;
2071f0d5c6f4SDmitry Torokhov 
20721da177e4SLinus Torvalds 	return 0;
20731da177e4SLinus Torvalds 
20741da177e4SLinus Torvalds init_fail:
2075f42649e8SDmitry Torokhov 	psmouse_reset(psmouse);
20762e5b636bSDmitry Torokhov 	input_free_device(dev2);
20771da177e4SLinus Torvalds 	kfree(priv);
20781e0c5b12SDmitry Torokhov 	psmouse->private = NULL;
20791da177e4SLinus Torvalds 	return -1;
20801da177e4SLinus Torvalds }
20811da177e4SLinus Torvalds 
2082b7802c5cSDmitry Torokhov int alps_detect(struct psmouse *psmouse, bool set_properties)
20831da177e4SLinus Torvalds {
2084b5d6b851SKevin Cernekee 	struct alps_data dummy;
20851da177e4SLinus Torvalds 
2086b5d6b851SKevin Cernekee 	if (alps_identify(psmouse, &dummy) < 0)
20871da177e4SLinus Torvalds 		return -1;
20881da177e4SLinus Torvalds 
20891da177e4SLinus Torvalds 	if (set_properties) {
20901da177e4SLinus Torvalds 		psmouse->vendor = "ALPS";
2091b5d6b851SKevin Cernekee 		psmouse->name = dummy.flags & ALPS_DUALPOINT ?
2092968ac842SDmitry Torokhov 				"DualPoint TouchPad" : "GlidePoint";
2093b5d6b851SKevin Cernekee 		psmouse->model = dummy.proto_version << 8;
20941da177e4SLinus Torvalds 	}
20951da177e4SLinus Torvalds 	return 0;
20961da177e4SLinus Torvalds }
20971da177e4SLinus Torvalds 
2098