xref: /openbmc/linux/drivers/input/mouse/alps.c (revision cdf333ef)
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 
44425bded7cSSeth Forshee static void alps_process_trackstick_packet_v3(struct psmouse *psmouse)
44525bded7cSSeth Forshee {
44625bded7cSSeth Forshee 	struct alps_data *priv = psmouse->private;
44725bded7cSSeth Forshee 	unsigned char *packet = psmouse->packet;
44825bded7cSSeth Forshee 	struct input_dev *dev = priv->dev2;
44925bded7cSSeth Forshee 	int x, y, z, left, right, middle;
45025bded7cSSeth Forshee 
45125bded7cSSeth Forshee 	/* Sanity check packet */
45225bded7cSSeth Forshee 	if (!(packet[0] & 0x40)) {
45325bded7cSSeth Forshee 		psmouse_dbg(psmouse, "Bad trackstick packet, discarding\n");
45425bded7cSSeth Forshee 		return;
45525bded7cSSeth Forshee 	}
45625bded7cSSeth Forshee 
45725bded7cSSeth Forshee 	/*
45825bded7cSSeth Forshee 	 * There's a special packet that seems to indicate the end
45925bded7cSSeth Forshee 	 * of a stream of trackstick data. Filter these out.
46025bded7cSSeth Forshee 	 */
46125bded7cSSeth Forshee 	if (packet[1] == 0x7f && packet[2] == 0x7f && packet[4] == 0x7f)
46225bded7cSSeth Forshee 		return;
46325bded7cSSeth Forshee 
46425bded7cSSeth Forshee 	x = (s8)(((packet[0] & 0x20) << 2) | (packet[1] & 0x7f));
46525bded7cSSeth Forshee 	y = (s8)(((packet[0] & 0x10) << 3) | (packet[2] & 0x7f));
46625bded7cSSeth Forshee 	z = (packet[4] & 0x7c) >> 2;
46725bded7cSSeth Forshee 
46825bded7cSSeth Forshee 	/*
46925bded7cSSeth Forshee 	 * The x and y values tend to be quite large, and when used
47025bded7cSSeth Forshee 	 * alone the trackstick is difficult to use. Scale them down
47125bded7cSSeth Forshee 	 * to compensate.
47225bded7cSSeth Forshee 	 */
47325bded7cSSeth Forshee 	x /= 8;
47425bded7cSSeth Forshee 	y /= 8;
47525bded7cSSeth Forshee 
47625bded7cSSeth Forshee 	input_report_rel(dev, REL_X, x);
47725bded7cSSeth Forshee 	input_report_rel(dev, REL_Y, -y);
47825bded7cSSeth Forshee 
47925bded7cSSeth Forshee 	/*
48025bded7cSSeth Forshee 	 * Most ALPS models report the trackstick buttons in the touchpad
48125bded7cSSeth Forshee 	 * packets, but a few report them here. No reliable way has been
48225bded7cSSeth Forshee 	 * found to differentiate between the models upfront, so we enable
48325bded7cSSeth Forshee 	 * the quirk in response to seeing a button press in the trackstick
48425bded7cSSeth Forshee 	 * packet.
48525bded7cSSeth Forshee 	 */
48625bded7cSSeth Forshee 	left = packet[3] & 0x01;
48725bded7cSSeth Forshee 	right = packet[3] & 0x02;
48825bded7cSSeth Forshee 	middle = packet[3] & 0x04;
48925bded7cSSeth Forshee 
49025bded7cSSeth Forshee 	if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) &&
49125bded7cSSeth Forshee 	    (left || right || middle))
49225bded7cSSeth Forshee 		priv->quirks |= ALPS_QUIRK_TRACKSTICK_BUTTONS;
49325bded7cSSeth Forshee 
49425bded7cSSeth Forshee 	if (priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) {
49525bded7cSSeth Forshee 		input_report_key(dev, BTN_LEFT, left);
49625bded7cSSeth Forshee 		input_report_key(dev, BTN_RIGHT, right);
49725bded7cSSeth Forshee 		input_report_key(dev, BTN_MIDDLE, middle);
49825bded7cSSeth Forshee 	}
49925bded7cSSeth Forshee 
50025bded7cSSeth Forshee 	input_sync(dev);
50125bded7cSSeth Forshee 	return;
50225bded7cSSeth Forshee }
50325bded7cSSeth Forshee 
504f85e5001SKevin Cernekee static void alps_decode_buttons_v3(struct alps_fields *f, unsigned char *p)
505f85e5001SKevin Cernekee {
506f85e5001SKevin Cernekee 	f->left = !!(p[3] & 0x01);
507f85e5001SKevin Cernekee 	f->right = !!(p[3] & 0x02);
508f85e5001SKevin Cernekee 	f->middle = !!(p[3] & 0x04);
509f85e5001SKevin Cernekee 
510f85e5001SKevin Cernekee 	f->ts_left = !!(p[3] & 0x10);
511f85e5001SKevin Cernekee 	f->ts_right = !!(p[3] & 0x20);
512f85e5001SKevin Cernekee 	f->ts_middle = !!(p[3] & 0x40);
513f85e5001SKevin Cernekee }
514f85e5001SKevin Cernekee 
515ee65d4b3SYunkang Tang static void alps_decode_pinnacle(struct alps_fields *f, unsigned char *p,
516ee65d4b3SYunkang Tang 				 struct psmouse *psmouse)
517f85e5001SKevin Cernekee {
518f85e5001SKevin Cernekee 	f->first_mp = !!(p[4] & 0x40);
519f85e5001SKevin Cernekee 	f->is_mp = !!(p[0] & 0x40);
520f85e5001SKevin Cernekee 
521f85e5001SKevin Cernekee 	f->fingers = (p[5] & 0x3) + 1;
522f85e5001SKevin Cernekee 	f->x_map = ((p[4] & 0x7e) << 8) |
523f85e5001SKevin Cernekee 		   ((p[1] & 0x7f) << 2) |
524f85e5001SKevin Cernekee 		   ((p[0] & 0x30) >> 4);
525f85e5001SKevin Cernekee 	f->y_map = ((p[3] & 0x70) << 4) |
526f85e5001SKevin Cernekee 		   ((p[2] & 0x7f) << 1) |
527f85e5001SKevin Cernekee 		   (p[4] & 0x01);
528f85e5001SKevin Cernekee 
52902d04254SHans de Goede 	f->st.x = ((p[1] & 0x7f) << 4) | ((p[4] & 0x30) >> 2) |
530f85e5001SKevin Cernekee 	       ((p[0] & 0x30) >> 4);
53102d04254SHans de Goede 	f->st.y = ((p[2] & 0x7f) << 4) | (p[4] & 0x0f);
53202d04254SHans de Goede 	f->pressure = p[5] & 0x7f;
533f85e5001SKevin Cernekee 
534f85e5001SKevin Cernekee 	alps_decode_buttons_v3(f, p);
535f85e5001SKevin Cernekee }
536f85e5001SKevin Cernekee 
537ee65d4b3SYunkang Tang static void alps_decode_rushmore(struct alps_fields *f, unsigned char *p,
538ee65d4b3SYunkang Tang 				 struct psmouse *psmouse)
5391302bac3SKevin Cernekee {
540ee65d4b3SYunkang Tang 	alps_decode_pinnacle(f, p, psmouse);
5411302bac3SKevin Cernekee 
542f105e34aSYunkang Tang 	/* Rushmore's packet decode has a bit difference with Pinnacle's */
543f105e34aSYunkang Tang 	f->is_mp = !!(p[5] & 0x40);
544f105e34aSYunkang Tang 	f->fingers = max((p[5] & 0x3), ((p[5] >> 2) & 0x3)) + 1;
5451302bac3SKevin Cernekee 	f->x_map |= (p[5] & 0x10) << 11;
5461302bac3SKevin Cernekee 	f->y_map |= (p[5] & 0x20) << 6;
5471302bac3SKevin Cernekee }
5481302bac3SKevin Cernekee 
549ee65d4b3SYunkang Tang static void alps_decode_dolphin(struct alps_fields *f, unsigned char *p,
550ee65d4b3SYunkang Tang 				struct psmouse *psmouse)
55175af9e56SDave Turvene {
552ee65d4b3SYunkang Tang 	u64 palm_data = 0;
553ee65d4b3SYunkang Tang 	struct alps_data *priv = psmouse->private;
554ee65d4b3SYunkang Tang 
55575af9e56SDave Turvene 	f->first_mp = !!(p[0] & 0x02);
55675af9e56SDave Turvene 	f->is_mp = !!(p[0] & 0x20);
55775af9e56SDave Turvene 
558ee65d4b3SYunkang Tang 	if (!f->is_mp) {
55902d04254SHans de Goede 		f->st.x = ((p[1] & 0x7f) | ((p[4] & 0x0f) << 7));
56002d04254SHans de Goede 		f->st.y = ((p[2] & 0x7f) | ((p[4] & 0xf0) << 3));
56102d04254SHans de Goede 		f->pressure = (p[0] & 4) ? 0 : p[5] & 0x7f;
56275af9e56SDave Turvene 		alps_decode_buttons_v3(f, p);
563ee65d4b3SYunkang Tang 	} else {
564ee65d4b3SYunkang Tang 		f->fingers = ((p[0] & 0x6) >> 1 |
565ee65d4b3SYunkang Tang 		     (p[0] & 0x10) >> 2);
566ee65d4b3SYunkang Tang 
567ee65d4b3SYunkang Tang 		palm_data = (p[1] & 0x7f) |
568ee65d4b3SYunkang Tang 			    ((p[2] & 0x7f) << 7) |
569ee65d4b3SYunkang Tang 			    ((p[4] & 0x7f) << 14) |
570ee65d4b3SYunkang Tang 			    ((p[5] & 0x7f) << 21) |
571ee65d4b3SYunkang Tang 			    ((p[3] & 0x07) << 28) |
572ee65d4b3SYunkang Tang 			    (((u64)p[3] & 0x70) << 27) |
573ee65d4b3SYunkang Tang 			    (((u64)p[0] & 0x01) << 34);
574ee65d4b3SYunkang Tang 
575ee65d4b3SYunkang Tang 		/* Y-profile is stored in P(0) to p(n-1), n = y_bits; */
576ee65d4b3SYunkang Tang 		f->y_map = palm_data & (BIT(priv->y_bits) - 1);
577ee65d4b3SYunkang Tang 
578ee65d4b3SYunkang Tang 		/* X-profile is stored in p(n) to p(n+m-1), m = x_bits; */
579ee65d4b3SYunkang Tang 		f->x_map = (palm_data >> priv->y_bits) &
580ee65d4b3SYunkang Tang 			   (BIT(priv->x_bits) - 1);
581ee65d4b3SYunkang Tang 	}
58275af9e56SDave Turvene }
58375af9e56SDave Turvene 
584ee65d4b3SYunkang Tang static void alps_process_touchpad_packet_v3_v5(struct psmouse *psmouse)
58525bded7cSSeth Forshee {
58625bded7cSSeth Forshee 	struct alps_data *priv = psmouse->private;
58725bded7cSSeth Forshee 	unsigned char *packet = psmouse->packet;
58825bded7cSSeth Forshee 	struct input_dev *dev = psmouse->dev;
58925bded7cSSeth Forshee 	struct input_dev *dev2 = priv->dev2;
59002d04254SHans de Goede 	struct alps_fields *f = &priv->f;
59102d04254SHans de Goede 	int fingers = 0;
592f85e5001SKevin Cernekee 
59302d04254SHans de Goede 	memset(f, 0, sizeof(*f));
59402d04254SHans de Goede 
59502d04254SHans de Goede 	priv->decode_fields(f, packet, psmouse);
59625bded7cSSeth Forshee 
59725bded7cSSeth Forshee 	/*
59801ce661fSSeth Forshee 	 * There's no single feature of touchpad position and bitmap packets
59901ce661fSSeth Forshee 	 * that can be used to distinguish between them. We rely on the fact
60001ce661fSSeth Forshee 	 * that a bitmap packet should always follow a position packet with
60101ce661fSSeth Forshee 	 * bit 6 of packet[4] set.
60225bded7cSSeth Forshee 	 */
60325bded7cSSeth Forshee 	if (priv->multi_packet) {
60425bded7cSSeth Forshee 		/*
60525bded7cSSeth Forshee 		 * Sometimes a position packet will indicate a multi-packet
60625bded7cSSeth Forshee 		 * sequence, but then what follows is another position
60725bded7cSSeth Forshee 		 * packet. Check for this, and when it happens process the
60825bded7cSSeth Forshee 		 * position packet as usual.
60925bded7cSSeth Forshee 		 */
61002d04254SHans de Goede 		if (f->is_mp) {
61102d04254SHans de Goede 			fingers = f->fingers;
612ee65d4b3SYunkang Tang 			if (priv->proto_version == ALPS_PROTO_V3) {
61302d04254SHans de Goede 				if (alps_process_bitmap(priv, f) == 0)
61420bea68bSHans de Goede 					fingers = 0; /* Use st data */
61501ce661fSSeth Forshee 
61601ce661fSSeth Forshee 				/* Now process position packet */
61702d04254SHans de Goede 				priv->decode_fields(f, priv->multi_data,
618ee65d4b3SYunkang Tang 						    psmouse);
619ee65d4b3SYunkang Tang 			} else {
620ee65d4b3SYunkang Tang 				/*
621ee65d4b3SYunkang Tang 				 * Because Dolphin uses position packet's
622ee65d4b3SYunkang Tang 				 * coordinate data as Pt1 and uses it to
623ee65d4b3SYunkang Tang 				 * calculate Pt2, so we need to do position
624ee65d4b3SYunkang Tang 				 * packet decode first.
625ee65d4b3SYunkang Tang 				 */
62602d04254SHans de Goede 				priv->decode_fields(f, priv->multi_data,
627ee65d4b3SYunkang Tang 						    psmouse);
628ee65d4b3SYunkang Tang 
629ee65d4b3SYunkang Tang 				/*
630ee65d4b3SYunkang Tang 				 * Since Dolphin's finger number is reliable,
631ee65d4b3SYunkang Tang 				 * there is no need to compare with bmap_fn.
632ee65d4b3SYunkang Tang 				 */
63302d04254SHans de Goede 				alps_process_bitmap_dolphin(priv, f);
634ee65d4b3SYunkang Tang 			}
63501ce661fSSeth Forshee 		} else {
63601ce661fSSeth Forshee 			priv->multi_packet = 0;
63725bded7cSSeth Forshee 		}
63825bded7cSSeth Forshee 	}
63925bded7cSSeth Forshee 
64001ce661fSSeth Forshee 	/*
64101ce661fSSeth Forshee 	 * Bit 6 of byte 0 is not usually set in position packets. The only
64201ce661fSSeth Forshee 	 * times it seems to be set is in situations where the data is
64301ce661fSSeth Forshee 	 * suspect anyway, e.g. a palm resting flat on the touchpad. Given
64401ce661fSSeth Forshee 	 * this combined with the fact that this bit is useful for filtering
64501ce661fSSeth Forshee 	 * out misidentified bitmap packets, we reject anything with this
64601ce661fSSeth Forshee 	 * bit set.
64701ce661fSSeth Forshee 	 */
64802d04254SHans de Goede 	if (f->is_mp)
64901ce661fSSeth Forshee 		return;
65001ce661fSSeth Forshee 
65102d04254SHans de Goede 	if (!priv->multi_packet && f->first_mp) {
65225bded7cSSeth Forshee 		priv->multi_packet = 1;
65301ce661fSSeth Forshee 		memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
65401ce661fSSeth Forshee 		return;
65501ce661fSSeth Forshee 	}
65601ce661fSSeth Forshee 
65725bded7cSSeth Forshee 	priv->multi_packet = 0;
65825bded7cSSeth Forshee 
65925bded7cSSeth Forshee 	/*
66025bded7cSSeth Forshee 	 * Sometimes the hardware sends a single packet with z = 0
66125bded7cSSeth Forshee 	 * in the middle of a stream. Real releases generate packets
66225bded7cSSeth Forshee 	 * with x, y, and z all zero, so these seem to be flukes.
66325bded7cSSeth Forshee 	 * Ignore them.
66425bded7cSSeth Forshee 	 */
66502d04254SHans de Goede 	if (f->st.x && f->st.y && !f->pressure)
66625bded7cSSeth Forshee 		return;
66725bded7cSSeth Forshee 
66801ce661fSSeth Forshee 	/*
66901ce661fSSeth Forshee 	 * If we don't have MT data or the bitmaps were empty, we have
67001ce661fSSeth Forshee 	 * to rely on ST data.
67101ce661fSSeth Forshee 	 */
67201ce661fSSeth Forshee 	if (!fingers) {
67302d04254SHans de Goede 		f->mt[0].x = f->st.x;
67402d04254SHans de Goede 		f->mt[0].y = f->st.y;
67502d04254SHans de Goede 		fingers = f->pressure > 0 ? 1 : 0;
67601ce661fSSeth Forshee 	}
67701ce661fSSeth Forshee 
678cdf333efSHans de Goede 	alps_report_mt_data(psmouse, (fingers <= 2) ? fingers : 1);
67901ce661fSSeth Forshee 
680616575c2SDmitry Torokhov 	input_mt_report_finger_count(dev, fingers);
68101ce661fSSeth Forshee 
68202d04254SHans de Goede 	input_report_key(dev, BTN_LEFT, f->left);
68302d04254SHans de Goede 	input_report_key(dev, BTN_RIGHT, f->right);
68402d04254SHans de Goede 	input_report_key(dev, BTN_MIDDLE, f->middle);
68501ce661fSSeth Forshee 
68602d04254SHans de Goede 	input_report_abs(dev, ABS_PRESSURE, f->pressure);
68725bded7cSSeth Forshee 
68825bded7cSSeth Forshee 	input_sync(dev);
68925bded7cSSeth Forshee 
69025bded7cSSeth Forshee 	if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS)) {
69102d04254SHans de Goede 		input_report_key(dev2, BTN_LEFT, f->ts_left);
69202d04254SHans de Goede 		input_report_key(dev2, BTN_RIGHT, f->ts_right);
69302d04254SHans de Goede 		input_report_key(dev2, BTN_MIDDLE, f->ts_middle);
69425bded7cSSeth Forshee 		input_sync(dev2);
69525bded7cSSeth Forshee 	}
69625bded7cSSeth Forshee }
69725bded7cSSeth Forshee 
69825bded7cSSeth Forshee static void alps_process_packet_v3(struct psmouse *psmouse)
69925bded7cSSeth Forshee {
70025bded7cSSeth Forshee 	unsigned char *packet = psmouse->packet;
70125bded7cSSeth Forshee 
70225bded7cSSeth Forshee 	/*
70325bded7cSSeth Forshee 	 * v3 protocol packets come in three types, two representing
70425bded7cSSeth Forshee 	 * touchpad data and one representing trackstick data.
70525bded7cSSeth Forshee 	 * Trackstick packets seem to be distinguished by always
70625bded7cSSeth Forshee 	 * having 0x3f in the last byte. This value has never been
70725bded7cSSeth Forshee 	 * observed in the last byte of either of the other types
70825bded7cSSeth Forshee 	 * of packets.
70925bded7cSSeth Forshee 	 */
71025bded7cSSeth Forshee 	if (packet[5] == 0x3f) {
71125bded7cSSeth Forshee 		alps_process_trackstick_packet_v3(psmouse);
71225bded7cSSeth Forshee 		return;
71325bded7cSSeth Forshee 	}
71425bded7cSSeth Forshee 
715ee65d4b3SYunkang Tang 	alps_process_touchpad_packet_v3_v5(psmouse);
71625bded7cSSeth Forshee }
71725bded7cSSeth Forshee 
71895f75e91SYunkang Tang static void alps_process_packet_v6(struct psmouse *psmouse)
71995f75e91SYunkang Tang {
72095f75e91SYunkang Tang 	struct alps_data *priv = psmouse->private;
72195f75e91SYunkang Tang 	unsigned char *packet = psmouse->packet;
72295f75e91SYunkang Tang 	struct input_dev *dev = psmouse->dev;
72395f75e91SYunkang Tang 	struct input_dev *dev2 = priv->dev2;
72495f75e91SYunkang Tang 	int x, y, z, left, right, middle;
72595f75e91SYunkang Tang 
72695f75e91SYunkang Tang 	/*
72795f75e91SYunkang Tang 	 * We can use Byte5 to distinguish if the packet is from Touchpad
72895f75e91SYunkang Tang 	 * or Trackpoint.
72995f75e91SYunkang Tang 	 * Touchpad:	0 - 0x7E
73095f75e91SYunkang Tang 	 * Trackpoint:	0x7F
73195f75e91SYunkang Tang 	 */
73295f75e91SYunkang Tang 	if (packet[5] == 0x7F) {
73395f75e91SYunkang Tang 		/* It should be a DualPoint when received Trackpoint packet */
73495f75e91SYunkang Tang 		if (!(priv->flags & ALPS_DUALPOINT))
73595f75e91SYunkang Tang 			return;
73695f75e91SYunkang Tang 
73795f75e91SYunkang Tang 		/* Trackpoint packet */
73895f75e91SYunkang Tang 		x = packet[1] | ((packet[3] & 0x20) << 2);
73995f75e91SYunkang Tang 		y = packet[2] | ((packet[3] & 0x40) << 1);
74095f75e91SYunkang Tang 		z = packet[4];
74195f75e91SYunkang Tang 		left = packet[3] & 0x01;
74295f75e91SYunkang Tang 		right = packet[3] & 0x02;
74395f75e91SYunkang Tang 		middle = packet[3] & 0x04;
74495f75e91SYunkang Tang 
74595f75e91SYunkang Tang 		/* To prevent the cursor jump when finger lifted */
74695f75e91SYunkang Tang 		if (x == 0x7F && y == 0x7F && z == 0x7F)
74795f75e91SYunkang Tang 			x = y = z = 0;
74895f75e91SYunkang Tang 
74995f75e91SYunkang Tang 		/* Divide 4 since trackpoint's speed is too fast */
75095f75e91SYunkang Tang 		input_report_rel(dev2, REL_X, (char)x / 4);
75195f75e91SYunkang Tang 		input_report_rel(dev2, REL_Y, -((char)y / 4));
75295f75e91SYunkang Tang 
75395f75e91SYunkang Tang 		input_report_key(dev2, BTN_LEFT, left);
75495f75e91SYunkang Tang 		input_report_key(dev2, BTN_RIGHT, right);
75595f75e91SYunkang Tang 		input_report_key(dev2, BTN_MIDDLE, middle);
75695f75e91SYunkang Tang 
75795f75e91SYunkang Tang 		input_sync(dev2);
75895f75e91SYunkang Tang 		return;
75995f75e91SYunkang Tang 	}
76095f75e91SYunkang Tang 
76195f75e91SYunkang Tang 	/* Touchpad packet */
76295f75e91SYunkang Tang 	x = packet[1] | ((packet[3] & 0x78) << 4);
76395f75e91SYunkang Tang 	y = packet[2] | ((packet[4] & 0x78) << 4);
76495f75e91SYunkang Tang 	z = packet[5];
76595f75e91SYunkang Tang 	left = packet[3] & 0x01;
76695f75e91SYunkang Tang 	right = packet[3] & 0x02;
76795f75e91SYunkang Tang 
76895f75e91SYunkang Tang 	if (z > 30)
76995f75e91SYunkang Tang 		input_report_key(dev, BTN_TOUCH, 1);
77095f75e91SYunkang Tang 	if (z < 25)
77195f75e91SYunkang Tang 		input_report_key(dev, BTN_TOUCH, 0);
77295f75e91SYunkang Tang 
77395f75e91SYunkang Tang 	if (z > 0) {
77495f75e91SYunkang Tang 		input_report_abs(dev, ABS_X, x);
77595f75e91SYunkang Tang 		input_report_abs(dev, ABS_Y, y);
77695f75e91SYunkang Tang 	}
77795f75e91SYunkang Tang 
77895f75e91SYunkang Tang 	input_report_abs(dev, ABS_PRESSURE, z);
77995f75e91SYunkang Tang 	input_report_key(dev, BTN_TOOL_FINGER, z > 0);
78095f75e91SYunkang Tang 
78195f75e91SYunkang Tang 	/* v6 touchpad does not have middle button */
78295f75e91SYunkang Tang 	input_report_key(dev, BTN_LEFT, left);
78395f75e91SYunkang Tang 	input_report_key(dev, BTN_RIGHT, right);
78495f75e91SYunkang Tang 
78595f75e91SYunkang Tang 	input_sync(dev);
78695f75e91SYunkang Tang }
78795f75e91SYunkang Tang 
78825bded7cSSeth Forshee static void alps_process_packet_v4(struct psmouse *psmouse)
78925bded7cSSeth Forshee {
7903b7e09faSGeorge Pantalos 	struct alps_data *priv = psmouse->private;
79125bded7cSSeth Forshee 	unsigned char *packet = psmouse->packet;
79225bded7cSSeth Forshee 	struct input_dev *dev = psmouse->dev;
79302d04254SHans de Goede 	struct alps_fields *f = &priv->f;
79402d04254SHans de Goede 	int offset, fingers = 0;
7953b7e09faSGeorge Pantalos 
7963b7e09faSGeorge Pantalos 	/*
7973b7e09faSGeorge Pantalos 	 * v4 has a 6-byte encoding for bitmap data, but this data is
7983b7e09faSGeorge Pantalos 	 * broken up between 3 normal packets. Use priv->multi_packet to
7993b7e09faSGeorge Pantalos 	 * track our position in the bitmap packet.
8003b7e09faSGeorge Pantalos 	 */
8013b7e09faSGeorge Pantalos 	if (packet[6] & 0x40) {
8023b7e09faSGeorge Pantalos 		/* sync, reset position */
8033b7e09faSGeorge Pantalos 		priv->multi_packet = 0;
8043b7e09faSGeorge Pantalos 	}
8053b7e09faSGeorge Pantalos 
8063b7e09faSGeorge Pantalos 	if (WARN_ON_ONCE(priv->multi_packet > 2))
8073b7e09faSGeorge Pantalos 		return;
8083b7e09faSGeorge Pantalos 
8093b7e09faSGeorge Pantalos 	offset = 2 * priv->multi_packet;
8103b7e09faSGeorge Pantalos 	priv->multi_data[offset] = packet[6];
8113b7e09faSGeorge Pantalos 	priv->multi_data[offset + 1] = packet[7];
8123b7e09faSGeorge Pantalos 
8133b7e09faSGeorge Pantalos 	if (++priv->multi_packet > 2) {
8143b7e09faSGeorge Pantalos 		priv->multi_packet = 0;
8153b7e09faSGeorge Pantalos 
81602d04254SHans de Goede 		f->x_map = ((priv->multi_data[2] & 0x1f) << 10) |
8173b7e09faSGeorge Pantalos 			   ((priv->multi_data[3] & 0x60) << 3) |
8183b7e09faSGeorge Pantalos 			   ((priv->multi_data[0] & 0x3f) << 2) |
8193b7e09faSGeorge Pantalos 			   ((priv->multi_data[1] & 0x60) >> 5);
82002d04254SHans de Goede 		f->y_map = ((priv->multi_data[5] & 0x01) << 10) |
8213b7e09faSGeorge Pantalos 			   ((priv->multi_data[3] & 0x1f) << 5) |
8223b7e09faSGeorge Pantalos 			    (priv->multi_data[1] & 0x1f);
8233b7e09faSGeorge Pantalos 
82402d04254SHans de Goede 		f->fingers = alps_process_bitmap(priv, f);
8253b7e09faSGeorge Pantalos 	}
82625bded7cSSeth Forshee 
82702d04254SHans de Goede 	f->left = packet[4] & 0x01;
82802d04254SHans de Goede 	f->right = packet[4] & 0x02;
82925bded7cSSeth Forshee 
83002d04254SHans de Goede 	f->st.x = ((packet[1] & 0x7f) << 4) | ((packet[3] & 0x30) >> 2) |
83125bded7cSSeth Forshee 		  ((packet[0] & 0x30) >> 4);
83202d04254SHans de Goede 	f->st.y = ((packet[2] & 0x7f) << 4) | (packet[3] & 0x0f);
83302d04254SHans de Goede 	f->pressure = packet[5] & 0x7f;
83425bded7cSSeth Forshee 
8353b7e09faSGeorge Pantalos 	/*
8363b7e09faSGeorge Pantalos 	 * If there were no contacts in the bitmap, use ST
8373b7e09faSGeorge Pantalos 	 * points in MT reports.
8383b7e09faSGeorge Pantalos 	 * If there were two contacts or more, report MT data.
8393b7e09faSGeorge Pantalos 	 */
84002d04254SHans de Goede 	if (f->fingers < 2) {
84102d04254SHans de Goede 		f->mt[0].x = f->st.x;
84202d04254SHans de Goede 		f->mt[0].y = f->st.y;
84302d04254SHans de Goede 		fingers = f->pressure > 0 ? 1 : 0;
8443b7e09faSGeorge Pantalos 	} else {
84502d04254SHans de Goede 		fingers = f->fingers;
8463b7e09faSGeorge Pantalos 	}
8473b7e09faSGeorge Pantalos 
848cdf333efSHans de Goede 	alps_report_mt_data(psmouse, (fingers <= 2) ? fingers : 1);
8493b7e09faSGeorge Pantalos 
850616575c2SDmitry Torokhov 	input_mt_report_finger_count(dev, fingers);
8513b7e09faSGeorge Pantalos 
85202d04254SHans de Goede 	input_report_key(dev, BTN_LEFT, f->left);
85302d04254SHans de Goede 	input_report_key(dev, BTN_RIGHT, f->right);
8543b7e09faSGeorge Pantalos 
85502d04254SHans de Goede 	input_report_abs(dev, ABS_PRESSURE, f->pressure);
85625bded7cSSeth Forshee 
85725bded7cSSeth Forshee 	input_sync(dev);
85825bded7cSSeth Forshee }
85925bded7cSSeth Forshee 
8601d9f2626SSebastian Kapfer static void alps_report_bare_ps2_packet(struct psmouse *psmouse,
8611d9f2626SSebastian Kapfer 					unsigned char packet[],
8621d9f2626SSebastian Kapfer 					bool report_buttons)
8631d9f2626SSebastian Kapfer {
8641d9f2626SSebastian Kapfer 	struct alps_data *priv = psmouse->private;
8651d9f2626SSebastian Kapfer 	struct input_dev *dev2 = priv->dev2;
8661d9f2626SSebastian Kapfer 
8671d9f2626SSebastian Kapfer 	if (report_buttons)
8681d9f2626SSebastian Kapfer 		alps_report_buttons(psmouse, dev2, psmouse->dev,
8691d9f2626SSebastian Kapfer 				packet[0] & 1, packet[0] & 2, packet[0] & 4);
8701d9f2626SSebastian Kapfer 
8711d9f2626SSebastian Kapfer 	input_report_rel(dev2, REL_X,
8721d9f2626SSebastian Kapfer 		packet[1] ? packet[1] - ((packet[0] << 4) & 0x100) : 0);
8731d9f2626SSebastian Kapfer 	input_report_rel(dev2, REL_Y,
8741d9f2626SSebastian Kapfer 		packet[2] ? ((packet[0] << 3) & 0x100) - packet[2] : 0);
8751d9f2626SSebastian Kapfer 
8761d9f2626SSebastian Kapfer 	input_sync(dev2);
8771d9f2626SSebastian Kapfer }
8781d9f2626SSebastian Kapfer 
8791d9f2626SSebastian Kapfer static psmouse_ret_t alps_handle_interleaved_ps2(struct psmouse *psmouse)
8801da177e4SLinus Torvalds {
8811da177e4SLinus Torvalds 	struct alps_data *priv = psmouse->private;
8821da177e4SLinus Torvalds 
8831d9f2626SSebastian Kapfer 	if (psmouse->pktcnt < 6)
8841d9f2626SSebastian Kapfer 		return PSMOUSE_GOOD_DATA;
8851d9f2626SSebastian Kapfer 
8861d9f2626SSebastian Kapfer 	if (psmouse->pktcnt == 6) {
8871d9f2626SSebastian Kapfer 		/*
8881d9f2626SSebastian Kapfer 		 * Start a timer to flush the packet if it ends up last
8891d9f2626SSebastian Kapfer 		 * 6-byte packet in the stream. Timer needs to fire
8901d9f2626SSebastian Kapfer 		 * psmouse core times out itself. 20 ms should be enough
8911d9f2626SSebastian Kapfer 		 * to decide if we are getting more data or not.
8921d9f2626SSebastian Kapfer 		 */
8931d9f2626SSebastian Kapfer 		mod_timer(&priv->timer, jiffies + msecs_to_jiffies(20));
8941d9f2626SSebastian Kapfer 		return PSMOUSE_GOOD_DATA;
8951d9f2626SSebastian Kapfer 	}
8961d9f2626SSebastian Kapfer 
8971d9f2626SSebastian Kapfer 	del_timer(&priv->timer);
8981d9f2626SSebastian Kapfer 
8991d9f2626SSebastian Kapfer 	if (psmouse->packet[6] & 0x80) {
9001d9f2626SSebastian Kapfer 
9011d9f2626SSebastian Kapfer 		/*
9021d9f2626SSebastian Kapfer 		 * Highest bit is set - that means we either had
9031d9f2626SSebastian Kapfer 		 * complete ALPS packet and this is start of the
9041d9f2626SSebastian Kapfer 		 * next packet or we got garbage.
9051d9f2626SSebastian Kapfer 		 */
9061d9f2626SSebastian Kapfer 
9071d9f2626SSebastian Kapfer 		if (((psmouse->packet[3] |
9081d9f2626SSebastian Kapfer 		      psmouse->packet[4] |
9091d9f2626SSebastian Kapfer 		      psmouse->packet[5]) & 0x80) ||
91099df65e7SKevin Cernekee 		    (!alps_is_valid_first_byte(priv, psmouse->packet[6]))) {
911b5d21704SDmitry Torokhov 			psmouse_dbg(psmouse,
9123b112923SAndy Shevchenko 				    "refusing packet %4ph (suspected interleaved ps/2)\n",
9133b112923SAndy Shevchenko 				    psmouse->packet + 3);
9141d9f2626SSebastian Kapfer 			return PSMOUSE_BAD_DATA;
9151d9f2626SSebastian Kapfer 		}
9161d9f2626SSebastian Kapfer 
91724af5cb9SKevin Cernekee 		priv->process_packet(psmouse);
9181d9f2626SSebastian Kapfer 
9191d9f2626SSebastian Kapfer 		/* Continue with the next packet */
9201d9f2626SSebastian Kapfer 		psmouse->packet[0] = psmouse->packet[6];
9211d9f2626SSebastian Kapfer 		psmouse->pktcnt = 1;
9221d9f2626SSebastian Kapfer 
9231d9f2626SSebastian Kapfer 	} else {
9241d9f2626SSebastian Kapfer 
9251d9f2626SSebastian Kapfer 		/*
9261d9f2626SSebastian Kapfer 		 * High bit is 0 - that means that we indeed got a PS/2
9271d9f2626SSebastian Kapfer 		 * packet in the middle of ALPS packet.
9281d9f2626SSebastian Kapfer 		 *
9291d9f2626SSebastian Kapfer 		 * There is also possibility that we got 6-byte ALPS
9301d9f2626SSebastian Kapfer 		 * packet followed  by 3-byte packet from trackpoint. We
9311d9f2626SSebastian Kapfer 		 * can not distinguish between these 2 scenarios but
932b5d21704SDmitry Torokhov 		 * because the latter is unlikely to happen in course of
9331d9f2626SSebastian Kapfer 		 * normal operation (user would need to press all
9341d9f2626SSebastian Kapfer 		 * buttons on the pad and start moving trackpoint
9351d9f2626SSebastian Kapfer 		 * without touching the pad surface) we assume former.
9361d9f2626SSebastian Kapfer 		 * Even if we are wrong the wost thing that would happen
9371d9f2626SSebastian Kapfer 		 * the cursor would jump but we should not get protocol
938b5d21704SDmitry Torokhov 		 * de-synchronization.
9391d9f2626SSebastian Kapfer 		 */
9401d9f2626SSebastian Kapfer 
9411d9f2626SSebastian Kapfer 		alps_report_bare_ps2_packet(psmouse, &psmouse->packet[3],
9421d9f2626SSebastian Kapfer 					    false);
9431d9f2626SSebastian Kapfer 
9441d9f2626SSebastian Kapfer 		/*
9451d9f2626SSebastian Kapfer 		 * Continue with the standard ALPS protocol handling,
9461d9f2626SSebastian Kapfer 		 * but make sure we won't process it as an interleaved
9471d9f2626SSebastian Kapfer 		 * packet again, which may happen if all buttons are
9481d9f2626SSebastian Kapfer 		 * pressed. To avoid this let's reset the 4th bit which
9491d9f2626SSebastian Kapfer 		 * is normally 1.
9501d9f2626SSebastian Kapfer 		 */
9511d9f2626SSebastian Kapfer 		psmouse->packet[3] = psmouse->packet[6] & 0xf7;
9521d9f2626SSebastian Kapfer 		psmouse->pktcnt = 4;
9531d9f2626SSebastian Kapfer 	}
9541d9f2626SSebastian Kapfer 
9551d9f2626SSebastian Kapfer 	return PSMOUSE_GOOD_DATA;
9561d9f2626SSebastian Kapfer }
9571d9f2626SSebastian Kapfer 
9581d9f2626SSebastian Kapfer static void alps_flush_packet(unsigned long data)
9591d9f2626SSebastian Kapfer {
9601d9f2626SSebastian Kapfer 	struct psmouse *psmouse = (struct psmouse *)data;
96124af5cb9SKevin Cernekee 	struct alps_data *priv = psmouse->private;
9621d9f2626SSebastian Kapfer 
9631d9f2626SSebastian Kapfer 	serio_pause_rx(psmouse->ps2dev.serio);
9641d9f2626SSebastian Kapfer 
965b46615feSSeth Forshee 	if (psmouse->pktcnt == psmouse->pktsize) {
9661d9f2626SSebastian Kapfer 
9671d9f2626SSebastian Kapfer 		/*
9681d9f2626SSebastian Kapfer 		 * We did not any more data in reasonable amount of time.
9691d9f2626SSebastian Kapfer 		 * Validate the last 3 bytes and process as a standard
9701d9f2626SSebastian Kapfer 		 * ALPS packet.
9711d9f2626SSebastian Kapfer 		 */
9721d9f2626SSebastian Kapfer 		if ((psmouse->packet[3] |
9731d9f2626SSebastian Kapfer 		     psmouse->packet[4] |
9741d9f2626SSebastian Kapfer 		     psmouse->packet[5]) & 0x80) {
975b5d21704SDmitry Torokhov 			psmouse_dbg(psmouse,
9763b112923SAndy Shevchenko 				    "refusing packet %3ph (suspected interleaved ps/2)\n",
9773b112923SAndy Shevchenko 				    psmouse->packet + 3);
9781d9f2626SSebastian Kapfer 		} else {
97924af5cb9SKevin Cernekee 			priv->process_packet(psmouse);
9801d9f2626SSebastian Kapfer 		}
9811d9f2626SSebastian Kapfer 		psmouse->pktcnt = 0;
9821d9f2626SSebastian Kapfer 	}
9831d9f2626SSebastian Kapfer 
9841d9f2626SSebastian Kapfer 	serio_continue_rx(psmouse->ps2dev.serio);
9851d9f2626SSebastian Kapfer }
9861d9f2626SSebastian Kapfer 
9871d9f2626SSebastian Kapfer static psmouse_ret_t alps_process_byte(struct psmouse *psmouse)
9881d9f2626SSebastian Kapfer {
9891d9f2626SSebastian Kapfer 	struct alps_data *priv = psmouse->private;
9901d9f2626SSebastian Kapfer 
9911da177e4SLinus Torvalds 	if ((psmouse->packet[0] & 0xc8) == 0x08) { /* PS/2 packet */
9921da177e4SLinus Torvalds 		if (psmouse->pktcnt == 3) {
9931d9f2626SSebastian Kapfer 			alps_report_bare_ps2_packet(psmouse, psmouse->packet,
9941d9f2626SSebastian Kapfer 						    true);
9951da177e4SLinus Torvalds 			return PSMOUSE_FULL_PACKET;
9961da177e4SLinus Torvalds 		}
9971da177e4SLinus Torvalds 		return PSMOUSE_GOOD_DATA;
9981da177e4SLinus Torvalds 	}
9991da177e4SLinus Torvalds 
10001d9f2626SSebastian Kapfer 	/* Check for PS/2 packet stuffed in the middle of ALPS packet. */
10011d9f2626SSebastian Kapfer 
100299df65e7SKevin Cernekee 	if ((priv->flags & ALPS_PS2_INTERLEAVED) &&
10031d9f2626SSebastian Kapfer 	    psmouse->pktcnt >= 4 && (psmouse->packet[3] & 0x0f) == 0x0f) {
10041d9f2626SSebastian Kapfer 		return alps_handle_interleaved_ps2(psmouse);
10051d9f2626SSebastian Kapfer 	}
10061d9f2626SSebastian Kapfer 
100799df65e7SKevin Cernekee 	if (!alps_is_valid_first_byte(priv, psmouse->packet[0])) {
1008b5d21704SDmitry Torokhov 		psmouse_dbg(psmouse,
1009b5d21704SDmitry Torokhov 			    "refusing packet[0] = %x (mask0 = %x, byte0 = %x)\n",
101099df65e7SKevin Cernekee 			    psmouse->packet[0], priv->mask0, priv->byte0);
10111da177e4SLinus Torvalds 		return PSMOUSE_BAD_DATA;
10121d9f2626SSebastian Kapfer 	}
10131da177e4SLinus Torvalds 
1014b46615feSSeth Forshee 	/* Bytes 2 - pktsize should have 0 in the highest bit */
101595f75e91SYunkang Tang 	if ((priv->proto_version < ALPS_PROTO_V5) &&
101675af9e56SDave Turvene 	    psmouse->pktcnt >= 2 && psmouse->pktcnt <= psmouse->pktsize &&
10171d9f2626SSebastian Kapfer 	    (psmouse->packet[psmouse->pktcnt - 1] & 0x80)) {
1018b5d21704SDmitry Torokhov 		psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
1019b5d21704SDmitry Torokhov 			    psmouse->pktcnt - 1,
1020b5d21704SDmitry Torokhov 			    psmouse->packet[psmouse->pktcnt - 1]);
10211da177e4SLinus Torvalds 		return PSMOUSE_BAD_DATA;
10221d9f2626SSebastian Kapfer 	}
10231da177e4SLinus Torvalds 
1024b46615feSSeth Forshee 	if (psmouse->pktcnt == psmouse->pktsize) {
102524af5cb9SKevin Cernekee 		priv->process_packet(psmouse);
10261da177e4SLinus Torvalds 		return PSMOUSE_FULL_PACKET;
10271da177e4SLinus Torvalds 	}
10281da177e4SLinus Torvalds 
10291da177e4SLinus Torvalds 	return PSMOUSE_GOOD_DATA;
10301da177e4SLinus Torvalds }
10311da177e4SLinus Torvalds 
103225bded7cSSeth Forshee static int alps_command_mode_send_nibble(struct psmouse *psmouse, int nibble)
103325bded7cSSeth Forshee {
103425bded7cSSeth Forshee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
103525bded7cSSeth Forshee 	struct alps_data *priv = psmouse->private;
103625bded7cSSeth Forshee 	int command;
103725bded7cSSeth Forshee 	unsigned char *param;
103825bded7cSSeth Forshee 	unsigned char dummy[4];
103925bded7cSSeth Forshee 
104025bded7cSSeth Forshee 	BUG_ON(nibble > 0xf);
104125bded7cSSeth Forshee 
104225bded7cSSeth Forshee 	command = priv->nibble_commands[nibble].command;
104325bded7cSSeth Forshee 	param = (command & 0x0f00) ?
104425bded7cSSeth Forshee 		dummy : (unsigned char *)&priv->nibble_commands[nibble].data;
104525bded7cSSeth Forshee 
104625bded7cSSeth Forshee 	if (ps2_command(ps2dev, param, command))
104725bded7cSSeth Forshee 		return -1;
104825bded7cSSeth Forshee 
104925bded7cSSeth Forshee 	return 0;
105025bded7cSSeth Forshee }
105125bded7cSSeth Forshee 
105225bded7cSSeth Forshee static int alps_command_mode_set_addr(struct psmouse *psmouse, int addr)
105325bded7cSSeth Forshee {
105425bded7cSSeth Forshee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
105525bded7cSSeth Forshee 	struct alps_data *priv = psmouse->private;
105625bded7cSSeth Forshee 	int i, nibble;
105725bded7cSSeth Forshee 
105825bded7cSSeth Forshee 	if (ps2_command(ps2dev, NULL, priv->addr_command))
105925bded7cSSeth Forshee 		return -1;
106025bded7cSSeth Forshee 
106125bded7cSSeth Forshee 	for (i = 12; i >= 0; i -= 4) {
106225bded7cSSeth Forshee 		nibble = (addr >> i) & 0xf;
106325bded7cSSeth Forshee 		if (alps_command_mode_send_nibble(psmouse, nibble))
106425bded7cSSeth Forshee 			return -1;
106525bded7cSSeth Forshee 	}
106625bded7cSSeth Forshee 
106725bded7cSSeth Forshee 	return 0;
106825bded7cSSeth Forshee }
106925bded7cSSeth Forshee 
107025bded7cSSeth Forshee static int __alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
107125bded7cSSeth Forshee {
107225bded7cSSeth Forshee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
107325bded7cSSeth Forshee 	unsigned char param[4];
107425bded7cSSeth Forshee 
107525bded7cSSeth Forshee 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
107625bded7cSSeth Forshee 		return -1;
107725bded7cSSeth Forshee 
107825bded7cSSeth Forshee 	/*
107925bded7cSSeth Forshee 	 * The address being read is returned in the first two bytes
108025bded7cSSeth Forshee 	 * of the result. Check that this address matches the expected
108125bded7cSSeth Forshee 	 * address.
108225bded7cSSeth Forshee 	 */
108325bded7cSSeth Forshee 	if (addr != ((param[0] << 8) | param[1]))
108425bded7cSSeth Forshee 		return -1;
108525bded7cSSeth Forshee 
108625bded7cSSeth Forshee 	return param[2];
108725bded7cSSeth Forshee }
108825bded7cSSeth Forshee 
108925bded7cSSeth Forshee static int alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
109025bded7cSSeth Forshee {
109125bded7cSSeth Forshee 	if (alps_command_mode_set_addr(psmouse, addr))
109225bded7cSSeth Forshee 		return -1;
109325bded7cSSeth Forshee 	return __alps_command_mode_read_reg(psmouse, addr);
109425bded7cSSeth Forshee }
109525bded7cSSeth Forshee 
109625bded7cSSeth Forshee static int __alps_command_mode_write_reg(struct psmouse *psmouse, u8 value)
109725bded7cSSeth Forshee {
109825bded7cSSeth Forshee 	if (alps_command_mode_send_nibble(psmouse, (value >> 4) & 0xf))
109925bded7cSSeth Forshee 		return -1;
110025bded7cSSeth Forshee 	if (alps_command_mode_send_nibble(psmouse, value & 0xf))
110125bded7cSSeth Forshee 		return -1;
110225bded7cSSeth Forshee 	return 0;
110325bded7cSSeth Forshee }
110425bded7cSSeth Forshee 
110525bded7cSSeth Forshee static int alps_command_mode_write_reg(struct psmouse *psmouse, int addr,
110625bded7cSSeth Forshee 				       u8 value)
110725bded7cSSeth Forshee {
110825bded7cSSeth Forshee 	if (alps_command_mode_set_addr(psmouse, addr))
110925bded7cSSeth Forshee 		return -1;
111025bded7cSSeth Forshee 	return __alps_command_mode_write_reg(psmouse, value);
111125bded7cSSeth Forshee }
111225bded7cSSeth Forshee 
111324ba9707SKevin Cernekee static int alps_rpt_cmd(struct psmouse *psmouse, int init_command,
111424ba9707SKevin Cernekee 			int repeated_command, unsigned char *param)
111524ba9707SKevin Cernekee {
111624ba9707SKevin Cernekee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
111724ba9707SKevin Cernekee 
111824ba9707SKevin Cernekee 	param[0] = 0;
111924ba9707SKevin Cernekee 	if (init_command && ps2_command(ps2dev, param, init_command))
112024ba9707SKevin Cernekee 		return -EIO;
112124ba9707SKevin Cernekee 
112224ba9707SKevin Cernekee 	if (ps2_command(ps2dev,  NULL, repeated_command) ||
112324ba9707SKevin Cernekee 	    ps2_command(ps2dev,  NULL, repeated_command) ||
112424ba9707SKevin Cernekee 	    ps2_command(ps2dev,  NULL, repeated_command))
112524ba9707SKevin Cernekee 		return -EIO;
112624ba9707SKevin Cernekee 
112724ba9707SKevin Cernekee 	param[0] = param[1] = param[2] = 0xff;
112824ba9707SKevin Cernekee 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
112924ba9707SKevin Cernekee 		return -EIO;
113024ba9707SKevin Cernekee 
113139fbe585SDmitry Torokhov 	psmouse_dbg(psmouse, "%2.2X report: %3ph\n",
113239fbe585SDmitry Torokhov 		    repeated_command, param);
113324ba9707SKevin Cernekee 	return 0;
113424ba9707SKevin Cernekee }
113524ba9707SKevin Cernekee 
1136d18e53fcSKevin Cernekee static int alps_enter_command_mode(struct psmouse *psmouse)
113725bded7cSSeth Forshee {
113825bded7cSSeth Forshee 	unsigned char param[4];
113925bded7cSSeth Forshee 
114024ba9707SKevin Cernekee 	if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_RESET_WRAP, param)) {
114125bded7cSSeth Forshee 		psmouse_err(psmouse, "failed to enter command mode\n");
114225bded7cSSeth Forshee 		return -1;
114325bded7cSSeth Forshee 	}
114425bded7cSSeth Forshee 
114575af9e56SDave Turvene 	if ((param[0] != 0x88 || (param[1] != 0x07 && param[1] != 0x08)) &&
114675af9e56SDave Turvene 	    param[0] != 0x73) {
114725bded7cSSeth Forshee 		psmouse_dbg(psmouse,
114824ba9707SKevin Cernekee 			    "unknown response while entering command mode\n");
114925bded7cSSeth Forshee 		return -1;
115025bded7cSSeth Forshee 	}
115125bded7cSSeth Forshee 	return 0;
115225bded7cSSeth Forshee }
115325bded7cSSeth Forshee 
115425bded7cSSeth Forshee static inline int alps_exit_command_mode(struct psmouse *psmouse)
115525bded7cSSeth Forshee {
115625bded7cSSeth Forshee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
115725bded7cSSeth Forshee 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM))
115825bded7cSSeth Forshee 		return -1;
115925bded7cSSeth Forshee 	return 0;
116025bded7cSSeth Forshee }
116125bded7cSSeth Forshee 
11621da177e4SLinus Torvalds /*
11631da177e4SLinus Torvalds  * For DualPoint devices select the device that should respond to
11641da177e4SLinus Torvalds  * subsequent commands. It looks like glidepad is behind stickpointer,
11651da177e4SLinus Torvalds  * I'd thought it would be other way around...
11661da177e4SLinus Torvalds  */
116725bded7cSSeth Forshee static int alps_passthrough_mode_v2(struct psmouse *psmouse, bool enable)
11681da177e4SLinus Torvalds {
11691da177e4SLinus Torvalds 	struct ps2dev *ps2dev = &psmouse->ps2dev;
11701da177e4SLinus Torvalds 	int cmd = enable ? PSMOUSE_CMD_SETSCALE21 : PSMOUSE_CMD_SETSCALE11;
11711da177e4SLinus Torvalds 
11721da177e4SLinus Torvalds 	if (ps2_command(ps2dev, NULL, cmd) ||
11731da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, cmd) ||
11741da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, cmd) ||
11751da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
11761da177e4SLinus Torvalds 		return -1;
11771da177e4SLinus Torvalds 
11781da177e4SLinus Torvalds 	/* we may get 3 more bytes, just ignore them */
1179c611763dSDmitry Torokhov 	ps2_drain(ps2dev, 3, 100);
11801da177e4SLinus Torvalds 
11811da177e4SLinus Torvalds 	return 0;
11821da177e4SLinus Torvalds }
11831da177e4SLinus Torvalds 
118425bded7cSSeth Forshee static int alps_absolute_mode_v1_v2(struct psmouse *psmouse)
11851da177e4SLinus Torvalds {
11861da177e4SLinus Torvalds 	struct ps2dev *ps2dev = &psmouse->ps2dev;
11871da177e4SLinus Torvalds 
11881da177e4SLinus Torvalds 	/* Try ALPS magic knock - 4 disable before enable */
11891da177e4SLinus Torvalds 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
11901da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
11911da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
11921da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
11931da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
11941da177e4SLinus Torvalds 		return -1;
11951da177e4SLinus Torvalds 
11961da177e4SLinus Torvalds 	/*
11971da177e4SLinus Torvalds 	 * Switch mouse to poll (remote) mode so motion data will not
11981da177e4SLinus Torvalds 	 * get in our way
11991da177e4SLinus Torvalds 	 */
12001da177e4SLinus Torvalds 	return ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETPOLL);
12011da177e4SLinus Torvalds }
12021da177e4SLinus Torvalds 
120395f75e91SYunkang Tang static int alps_monitor_mode_send_word(struct psmouse *psmouse, u16 word)
120495f75e91SYunkang Tang {
120595f75e91SYunkang Tang 	int i, nibble;
120695f75e91SYunkang Tang 
120795f75e91SYunkang Tang 	/*
120895f75e91SYunkang Tang 	 * b0-b11 are valid bits, send sequence is inverse.
120995f75e91SYunkang Tang 	 * e.g. when word = 0x0123, nibble send sequence is 3, 2, 1
121095f75e91SYunkang Tang 	 */
121195f75e91SYunkang Tang 	for (i = 0; i <= 8; i += 4) {
121295f75e91SYunkang Tang 		nibble = (word >> i) & 0xf;
121395f75e91SYunkang Tang 		if (alps_command_mode_send_nibble(psmouse, nibble))
121495f75e91SYunkang Tang 			return -1;
121595f75e91SYunkang Tang 	}
121695f75e91SYunkang Tang 
121795f75e91SYunkang Tang 	return 0;
121895f75e91SYunkang Tang }
121995f75e91SYunkang Tang 
122095f75e91SYunkang Tang static int alps_monitor_mode_write_reg(struct psmouse *psmouse,
122195f75e91SYunkang Tang 				       u16 addr, u16 value)
122295f75e91SYunkang Tang {
122395f75e91SYunkang Tang 	struct ps2dev *ps2dev = &psmouse->ps2dev;
122495f75e91SYunkang Tang 
122595f75e91SYunkang Tang 	/* 0x0A0 is the command to write the word */
122695f75e91SYunkang Tang 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE) ||
122795f75e91SYunkang Tang 	    alps_monitor_mode_send_word(psmouse, 0x0A0) ||
122895f75e91SYunkang Tang 	    alps_monitor_mode_send_word(psmouse, addr) ||
122995f75e91SYunkang Tang 	    alps_monitor_mode_send_word(psmouse, value) ||
123095f75e91SYunkang Tang 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
123195f75e91SYunkang Tang 		return -1;
123295f75e91SYunkang Tang 
123395f75e91SYunkang Tang 	return 0;
123495f75e91SYunkang Tang }
123595f75e91SYunkang Tang 
123695f75e91SYunkang Tang static int alps_monitor_mode(struct psmouse *psmouse, bool enable)
123795f75e91SYunkang Tang {
123895f75e91SYunkang Tang 	struct ps2dev *ps2dev = &psmouse->ps2dev;
123995f75e91SYunkang Tang 
124095f75e91SYunkang Tang 	if (enable) {
124195f75e91SYunkang Tang 		/* EC E9 F5 F5 E7 E6 E7 E9 to enter monitor mode */
124295f75e91SYunkang Tang 		if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
124395f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO) ||
124495f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
124595f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
124695f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
124795f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
124895f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
124995f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO))
125095f75e91SYunkang Tang 			return -1;
125195f75e91SYunkang Tang 	} else {
125295f75e91SYunkang Tang 		/* EC to exit monitor mode */
125395f75e91SYunkang Tang 		if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP))
125495f75e91SYunkang Tang 			return -1;
125595f75e91SYunkang Tang 	}
125695f75e91SYunkang Tang 
125795f75e91SYunkang Tang 	return 0;
125895f75e91SYunkang Tang }
125995f75e91SYunkang Tang 
126095f75e91SYunkang Tang static int alps_absolute_mode_v6(struct psmouse *psmouse)
126195f75e91SYunkang Tang {
126295f75e91SYunkang Tang 	u16 reg_val = 0x181;
126395f75e91SYunkang Tang 	int ret = -1;
126495f75e91SYunkang Tang 
126595f75e91SYunkang Tang 	/* enter monitor mode, to write the register */
126695f75e91SYunkang Tang 	if (alps_monitor_mode(psmouse, true))
126795f75e91SYunkang Tang 		return -1;
126895f75e91SYunkang Tang 
126995f75e91SYunkang Tang 	ret = alps_monitor_mode_write_reg(psmouse, 0x000, reg_val);
127095f75e91SYunkang Tang 
127195f75e91SYunkang Tang 	if (alps_monitor_mode(psmouse, false))
127295f75e91SYunkang Tang 		ret = -1;
127395f75e91SYunkang Tang 
127495f75e91SYunkang Tang 	return ret;
127595f75e91SYunkang Tang }
127695f75e91SYunkang Tang 
12771da177e4SLinus Torvalds static int alps_get_status(struct psmouse *psmouse, char *param)
12781da177e4SLinus Torvalds {
12791da177e4SLinus Torvalds 	/* Get status: 0xF5 0xF5 0xF5 0xE9 */
128024ba9707SKevin Cernekee 	if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_DISABLE, param))
12811da177e4SLinus Torvalds 		return -1;
12821da177e4SLinus Torvalds 
12831da177e4SLinus Torvalds 	return 0;
12841da177e4SLinus Torvalds }
12851da177e4SLinus Torvalds 
12861da177e4SLinus Torvalds /*
12871da177e4SLinus Torvalds  * Turn touchpad tapping on or off. The sequences are:
12881da177e4SLinus Torvalds  * 0xE9 0xF5 0xF5 0xF3 0x0A to enable,
12891da177e4SLinus Torvalds  * 0xE9 0xF5 0xF5 0xE8 0x00 to disable.
12901da177e4SLinus Torvalds  * My guess that 0xE9 (GetInfo) is here as a sync point.
12911da177e4SLinus Torvalds  * For models that also have stickpointer (DualPoints) its tapping
12921da177e4SLinus Torvalds  * is controlled separately (0xE6 0xE6 0xE6 0xF3 0x14|0x0A) but
12931da177e4SLinus Torvalds  * we don't fiddle with it.
12941da177e4SLinus Torvalds  */
12951da177e4SLinus Torvalds static int alps_tap_mode(struct psmouse *psmouse, int enable)
12961da177e4SLinus Torvalds {
12971da177e4SLinus Torvalds 	struct ps2dev *ps2dev = &psmouse->ps2dev;
12981da177e4SLinus Torvalds 	int cmd = enable ? PSMOUSE_CMD_SETRATE : PSMOUSE_CMD_SETRES;
12991da177e4SLinus Torvalds 	unsigned char tap_arg = enable ? 0x0A : 0x00;
13001da177e4SLinus Torvalds 	unsigned char param[4];
13011da177e4SLinus Torvalds 
13021da177e4SLinus Torvalds 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO) ||
13031da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
13041da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
13051da177e4SLinus Torvalds 	    ps2_command(ps2dev, &tap_arg, cmd))
13061da177e4SLinus Torvalds 		return -1;
13071da177e4SLinus Torvalds 
13081da177e4SLinus Torvalds 	if (alps_get_status(psmouse, param))
13091da177e4SLinus Torvalds 		return -1;
13101da177e4SLinus Torvalds 
13111da177e4SLinus Torvalds 	return 0;
13121da177e4SLinus Torvalds }
13131da177e4SLinus Torvalds 
1314f0d5c6f4SDmitry Torokhov /*
1315f0d5c6f4SDmitry Torokhov  * alps_poll() - poll the touchpad for current motion packet.
1316f0d5c6f4SDmitry Torokhov  * Used in resync.
1317f0d5c6f4SDmitry Torokhov  */
1318f0d5c6f4SDmitry Torokhov static int alps_poll(struct psmouse *psmouse)
1319f0d5c6f4SDmitry Torokhov {
1320f0d5c6f4SDmitry Torokhov 	struct alps_data *priv = psmouse->private;
1321b46615feSSeth Forshee 	unsigned char buf[sizeof(psmouse->packet)];
1322b7802c5cSDmitry Torokhov 	bool poll_failed;
1323f0d5c6f4SDmitry Torokhov 
132499df65e7SKevin Cernekee 	if (priv->flags & ALPS_PASS)
132525bded7cSSeth Forshee 		alps_passthrough_mode_v2(psmouse, true);
1326f0d5c6f4SDmitry Torokhov 
1327f0d5c6f4SDmitry Torokhov 	poll_failed = ps2_command(&psmouse->ps2dev, buf,
1328f0d5c6f4SDmitry Torokhov 				  PSMOUSE_CMD_POLL | (psmouse->pktsize << 8)) < 0;
1329f0d5c6f4SDmitry Torokhov 
133099df65e7SKevin Cernekee 	if (priv->flags & ALPS_PASS)
133125bded7cSSeth Forshee 		alps_passthrough_mode_v2(psmouse, false);
1332f0d5c6f4SDmitry Torokhov 
133399df65e7SKevin Cernekee 	if (poll_failed || (buf[0] & priv->mask0) != priv->byte0)
1334f0d5c6f4SDmitry Torokhov 		return -1;
1335f0d5c6f4SDmitry Torokhov 
1336f0d5c6f4SDmitry Torokhov 	if ((psmouse->badbyte & 0xc8) == 0x08) {
1337f0d5c6f4SDmitry Torokhov /*
1338f0d5c6f4SDmitry Torokhov  * Poll the track stick ...
1339f0d5c6f4SDmitry Torokhov  */
1340f0d5c6f4SDmitry Torokhov 		if (ps2_command(&psmouse->ps2dev, buf, PSMOUSE_CMD_POLL | (3 << 8)))
1341f0d5c6f4SDmitry Torokhov 			return -1;
1342f0d5c6f4SDmitry Torokhov 	}
1343f0d5c6f4SDmitry Torokhov 
1344f0d5c6f4SDmitry Torokhov 	memcpy(psmouse->packet, buf, sizeof(buf));
1345f0d5c6f4SDmitry Torokhov 	return 0;
1346f0d5c6f4SDmitry Torokhov }
1347f0d5c6f4SDmitry Torokhov 
134825bded7cSSeth Forshee static int alps_hw_init_v1_v2(struct psmouse *psmouse)
13491da177e4SLinus Torvalds {
13501da177e4SLinus Torvalds 	struct alps_data *priv = psmouse->private;
13511da177e4SLinus Torvalds 
135299df65e7SKevin Cernekee 	if ((priv->flags & ALPS_PASS) &&
135325bded7cSSeth Forshee 	    alps_passthrough_mode_v2(psmouse, true)) {
13541da177e4SLinus Torvalds 		return -1;
1355b7802c5cSDmitry Torokhov 	}
13561da177e4SLinus Torvalds 
1357b7802c5cSDmitry Torokhov 	if (alps_tap_mode(psmouse, true)) {
1358b5d21704SDmitry Torokhov 		psmouse_warn(psmouse, "Failed to enable hardware tapping\n");
13591da177e4SLinus Torvalds 		return -1;
1360963f626dSPeter Osterlund 	}
13611da177e4SLinus Torvalds 
136225bded7cSSeth Forshee 	if (alps_absolute_mode_v1_v2(psmouse)) {
1363b5d21704SDmitry Torokhov 		psmouse_err(psmouse, "Failed to enable absolute mode\n");
13641da177e4SLinus Torvalds 		return -1;
13651da177e4SLinus Torvalds 	}
13661da177e4SLinus Torvalds 
136799df65e7SKevin Cernekee 	if ((priv->flags & ALPS_PASS) &&
136825bded7cSSeth Forshee 	    alps_passthrough_mode_v2(psmouse, false)) {
13691da177e4SLinus Torvalds 		return -1;
1370b7802c5cSDmitry Torokhov 	}
13711da177e4SLinus Torvalds 
13721e0c5b12SDmitry Torokhov 	/* ALPS needs stream mode, otherwise it won't report any data */
13731e0c5b12SDmitry Torokhov 	if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)) {
1374b5d21704SDmitry Torokhov 		psmouse_err(psmouse, "Failed to enable stream mode\n");
13751e0c5b12SDmitry Torokhov 		return -1;
13761e0c5b12SDmitry Torokhov 	}
13771e0c5b12SDmitry Torokhov 
13781e0c5b12SDmitry Torokhov 	return 0;
13791e0c5b12SDmitry Torokhov }
13801e0c5b12SDmitry Torokhov 
138195f75e91SYunkang Tang static int alps_hw_init_v6(struct psmouse *psmouse)
138295f75e91SYunkang Tang {
138395f75e91SYunkang Tang 	unsigned char param[2] = {0xC8, 0x14};
138495f75e91SYunkang Tang 
138595f75e91SYunkang Tang 	/* Enter passthrough mode to let trackpoint enter 6byte raw mode */
138695f75e91SYunkang Tang 	if (alps_passthrough_mode_v2(psmouse, true))
138795f75e91SYunkang Tang 		return -1;
138895f75e91SYunkang Tang 
138995f75e91SYunkang Tang 	if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
139095f75e91SYunkang Tang 	    ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
139195f75e91SYunkang Tang 	    ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
139295f75e91SYunkang Tang 	    ps2_command(&psmouse->ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
139395f75e91SYunkang Tang 	    ps2_command(&psmouse->ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
139495f75e91SYunkang Tang 		return -1;
139595f75e91SYunkang Tang 
139695f75e91SYunkang Tang 	if (alps_passthrough_mode_v2(psmouse, false))
139795f75e91SYunkang Tang 		return -1;
139895f75e91SYunkang Tang 
139995f75e91SYunkang Tang 	if (alps_absolute_mode_v6(psmouse)) {
140095f75e91SYunkang Tang 		psmouse_err(psmouse, "Failed to enable absolute mode\n");
140195f75e91SYunkang Tang 		return -1;
140295f75e91SYunkang Tang 	}
140395f75e91SYunkang Tang 
140495f75e91SYunkang Tang 	return 0;
140595f75e91SYunkang Tang }
140695f75e91SYunkang Tang 
140725bded7cSSeth Forshee /*
1408cd401204SKevin Cernekee  * Enable or disable passthrough mode to the trackstick.
140925bded7cSSeth Forshee  */
1410cd401204SKevin Cernekee static int alps_passthrough_mode_v3(struct psmouse *psmouse,
1411cd401204SKevin Cernekee 				    int reg_base, bool enable)
141225bded7cSSeth Forshee {
1413cd401204SKevin Cernekee 	int reg_val, ret = -1;
141425bded7cSSeth Forshee 
1415d18e53fcSKevin Cernekee 	if (alps_enter_command_mode(psmouse))
141625bded7cSSeth Forshee 		return -1;
141725bded7cSSeth Forshee 
1418cd401204SKevin Cernekee 	reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x0008);
1419cd401204SKevin Cernekee 	if (reg_val == -1)
1420cd401204SKevin Cernekee 		goto error;
1421cd401204SKevin Cernekee 
142225bded7cSSeth Forshee 	if (enable)
142325bded7cSSeth Forshee 		reg_val |= 0x01;
142425bded7cSSeth Forshee 	else
142525bded7cSSeth Forshee 		reg_val &= ~0x01;
142625bded7cSSeth Forshee 
1427cd401204SKevin Cernekee 	ret = __alps_command_mode_write_reg(psmouse, reg_val);
142825bded7cSSeth Forshee 
1429cd401204SKevin Cernekee error:
1430cd401204SKevin Cernekee 	if (alps_exit_command_mode(psmouse))
1431cd401204SKevin Cernekee 		ret = -1;
1432cd401204SKevin Cernekee 	return ret;
143325bded7cSSeth Forshee }
143425bded7cSSeth Forshee 
143525bded7cSSeth Forshee /* Must be in command mode when calling this function */
143625bded7cSSeth Forshee static int alps_absolute_mode_v3(struct psmouse *psmouse)
143725bded7cSSeth Forshee {
143825bded7cSSeth Forshee 	int reg_val;
143925bded7cSSeth Forshee 
144025bded7cSSeth Forshee 	reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
144125bded7cSSeth Forshee 	if (reg_val == -1)
144225bded7cSSeth Forshee 		return -1;
144325bded7cSSeth Forshee 
144425bded7cSSeth Forshee 	reg_val |= 0x06;
144525bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, reg_val))
144625bded7cSSeth Forshee 		return -1;
144725bded7cSSeth Forshee 
144825bded7cSSeth Forshee 	return 0;
144925bded7cSSeth Forshee }
145025bded7cSSeth Forshee 
1451cd401204SKevin Cernekee static int alps_probe_trackstick_v3(struct psmouse *psmouse, int reg_base)
145225bded7cSSeth Forshee {
1453cd401204SKevin Cernekee 	int ret = -EIO, reg_val;
145425bded7cSSeth Forshee 
1455d18e53fcSKevin Cernekee 	if (alps_enter_command_mode(psmouse))
145625bded7cSSeth Forshee 		goto error;
145725bded7cSSeth Forshee 
1458cd401204SKevin Cernekee 	reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x08);
145925bded7cSSeth Forshee 	if (reg_val == -1)
146025bded7cSSeth Forshee 		goto error;
1461cd401204SKevin Cernekee 
1462cd401204SKevin Cernekee 	/* bit 7: trackstick is present */
1463cd401204SKevin Cernekee 	ret = reg_val & 0x80 ? 0 : -ENODEV;
1464cd401204SKevin Cernekee 
1465cd401204SKevin Cernekee error:
1466cd401204SKevin Cernekee 	alps_exit_command_mode(psmouse);
1467cd401204SKevin Cernekee 	return ret;
1468cd401204SKevin Cernekee }
1469cd401204SKevin Cernekee 
1470cd401204SKevin Cernekee static int alps_setup_trackstick_v3(struct psmouse *psmouse, int reg_base)
1471cd401204SKevin Cernekee {
1472cd401204SKevin Cernekee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
1473cd401204SKevin Cernekee 	int ret = 0;
1474cd401204SKevin Cernekee 	unsigned char param[4];
1475cd401204SKevin Cernekee 
1476cd401204SKevin Cernekee 	if (alps_passthrough_mode_v3(psmouse, reg_base, true))
1477cd401204SKevin Cernekee 		return -EIO;
147825bded7cSSeth Forshee 
147925bded7cSSeth Forshee 	/*
148025bded7cSSeth Forshee 	 * E7 report for the trackstick
148125bded7cSSeth Forshee 	 *
148225bded7cSSeth Forshee 	 * There have been reports of failures to seem to trace back
148325bded7cSSeth Forshee 	 * to the above trackstick check failing. When these occur
148425bded7cSSeth Forshee 	 * this E7 report fails, so when that happens we continue
148525bded7cSSeth Forshee 	 * with the assumption that there isn't a trackstick after
148625bded7cSSeth Forshee 	 * all.
148725bded7cSSeth Forshee 	 */
1488cd401204SKevin Cernekee 	if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_SETSCALE21, param)) {
148925bded7cSSeth Forshee 		psmouse_warn(psmouse, "trackstick E7 report failed\n");
1490cd401204SKevin Cernekee 		ret = -ENODEV;
149125bded7cSSeth Forshee 	} else {
149239fbe585SDmitry Torokhov 		psmouse_dbg(psmouse, "trackstick E7 report: %3ph\n", param);
149325bded7cSSeth Forshee 
149425bded7cSSeth Forshee 		/*
149525bded7cSSeth Forshee 		 * Not sure what this does, but it is absolutely
149625bded7cSSeth Forshee 		 * essential. Without it, the touchpad does not
149725bded7cSSeth Forshee 		 * work at all and the trackstick just emits normal
149825bded7cSSeth Forshee 		 * PS/2 packets.
149925bded7cSSeth Forshee 		 */
150025bded7cSSeth Forshee 		if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
150125bded7cSSeth Forshee 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
150225bded7cSSeth Forshee 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
150325bded7cSSeth Forshee 		    alps_command_mode_send_nibble(psmouse, 0x9) ||
150425bded7cSSeth Forshee 		    alps_command_mode_send_nibble(psmouse, 0x4)) {
150525bded7cSSeth Forshee 			psmouse_err(psmouse,
150625bded7cSSeth Forshee 				    "Error sending magic E6 sequence\n");
1507cd401204SKevin Cernekee 			ret = -EIO;
150825bded7cSSeth Forshee 			goto error;
150925bded7cSSeth Forshee 		}
151025bded7cSSeth Forshee 
1511cd401204SKevin Cernekee 		/*
1512cd401204SKevin Cernekee 		 * This ensures the trackstick packets are in the format
1513cd401204SKevin Cernekee 		 * supported by this driver. If bit 1 isn't set the packet
1514cd401204SKevin Cernekee 		 * format is different.
1515cd401204SKevin Cernekee 		 */
1516d18e53fcSKevin Cernekee 		if (alps_enter_command_mode(psmouse) ||
1517cd401204SKevin Cernekee 		    alps_command_mode_write_reg(psmouse,
1518cd401204SKevin Cernekee 						reg_base + 0x08, 0x82) ||
1519cd401204SKevin Cernekee 		    alps_exit_command_mode(psmouse))
1520cd401204SKevin Cernekee 			ret = -EIO;
1521cd401204SKevin Cernekee 	}
1522cd401204SKevin Cernekee 
1523cd401204SKevin Cernekee error:
1524cd401204SKevin Cernekee 	if (alps_passthrough_mode_v3(psmouse, reg_base, false))
1525cd401204SKevin Cernekee 		ret = -EIO;
1526cd401204SKevin Cernekee 
1527cd401204SKevin Cernekee 	return ret;
1528cd401204SKevin Cernekee }
1529cd401204SKevin Cernekee 
1530cd401204SKevin Cernekee static int alps_hw_init_v3(struct psmouse *psmouse)
1531cd401204SKevin Cernekee {
1532cd401204SKevin Cernekee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
1533cd401204SKevin Cernekee 	int reg_val;
1534cd401204SKevin Cernekee 	unsigned char param[4];
1535cd401204SKevin Cernekee 
1536cd401204SKevin Cernekee 	reg_val = alps_probe_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE);
1537cd401204SKevin Cernekee 	if (reg_val == -EIO)
1538cd401204SKevin Cernekee 		goto error;
153939fbe585SDmitry Torokhov 
1540cd401204SKevin Cernekee 	if (reg_val == 0 &&
1541cd401204SKevin Cernekee 	    alps_setup_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE) == -EIO)
1542cd401204SKevin Cernekee 		goto error;
1543cd401204SKevin Cernekee 
1544d18e53fcSKevin Cernekee 	if (alps_enter_command_mode(psmouse) ||
1545cd401204SKevin Cernekee 	    alps_absolute_mode_v3(psmouse)) {
154625bded7cSSeth Forshee 		psmouse_err(psmouse, "Failed to enter absolute mode\n");
154725bded7cSSeth Forshee 		goto error;
154825bded7cSSeth Forshee 	}
154925bded7cSSeth Forshee 
155025bded7cSSeth Forshee 	reg_val = alps_command_mode_read_reg(psmouse, 0x0006);
155125bded7cSSeth Forshee 	if (reg_val == -1)
155225bded7cSSeth Forshee 		goto error;
155325bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
155425bded7cSSeth Forshee 		goto error;
155525bded7cSSeth Forshee 
155625bded7cSSeth Forshee 	reg_val = alps_command_mode_read_reg(psmouse, 0x0007);
155725bded7cSSeth Forshee 	if (reg_val == -1)
155825bded7cSSeth Forshee 		goto error;
155925bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
156025bded7cSSeth Forshee 		goto error;
156125bded7cSSeth Forshee 
156225bded7cSSeth Forshee 	if (alps_command_mode_read_reg(psmouse, 0x0144) == -1)
156325bded7cSSeth Forshee 		goto error;
156425bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, 0x04))
156525bded7cSSeth Forshee 		goto error;
156625bded7cSSeth Forshee 
156725bded7cSSeth Forshee 	if (alps_command_mode_read_reg(psmouse, 0x0159) == -1)
156825bded7cSSeth Forshee 		goto error;
156925bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, 0x03))
157025bded7cSSeth Forshee 		goto error;
157125bded7cSSeth Forshee 
157225bded7cSSeth Forshee 	if (alps_command_mode_read_reg(psmouse, 0x0163) == -1)
157325bded7cSSeth Forshee 		goto error;
157425bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0163, 0x03))
157525bded7cSSeth Forshee 		goto error;
157625bded7cSSeth Forshee 
157725bded7cSSeth Forshee 	if (alps_command_mode_read_reg(psmouse, 0x0162) == -1)
157825bded7cSSeth Forshee 		goto error;
157925bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0162, 0x04))
158025bded7cSSeth Forshee 		goto error;
158125bded7cSSeth Forshee 
158225bded7cSSeth Forshee 	alps_exit_command_mode(psmouse);
158325bded7cSSeth Forshee 
158425bded7cSSeth Forshee 	/* Set rate and enable data reporting */
158525bded7cSSeth Forshee 	param[0] = 0x64;
158625bded7cSSeth Forshee 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
158725bded7cSSeth Forshee 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
158825bded7cSSeth Forshee 		psmouse_err(psmouse, "Failed to enable data reporting\n");
158925bded7cSSeth Forshee 		return -1;
159025bded7cSSeth Forshee 	}
159125bded7cSSeth Forshee 
159225bded7cSSeth Forshee 	return 0;
159325bded7cSSeth Forshee 
159425bded7cSSeth Forshee error:
159525bded7cSSeth Forshee 	/*
159625bded7cSSeth Forshee 	 * Leaving the touchpad in command mode will essentially render
159725bded7cSSeth Forshee 	 * it unusable until the machine reboots, so exit it here just
159825bded7cSSeth Forshee 	 * to be safe
159925bded7cSSeth Forshee 	 */
160025bded7cSSeth Forshee 	alps_exit_command_mode(psmouse);
160125bded7cSSeth Forshee 	return -1;
160225bded7cSSeth Forshee }
160325bded7cSSeth Forshee 
16041302bac3SKevin Cernekee static int alps_hw_init_rushmore_v3(struct psmouse *psmouse)
16051302bac3SKevin Cernekee {
1606cd401204SKevin Cernekee 	struct alps_data *priv = psmouse->private;
16071302bac3SKevin Cernekee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
16081302bac3SKevin Cernekee 	int reg_val, ret = -1;
16091302bac3SKevin Cernekee 
1610cd401204SKevin Cernekee 	if (priv->flags & ALPS_DUALPOINT) {
1611cd401204SKevin Cernekee 		reg_val = alps_setup_trackstick_v3(psmouse,
1612cd401204SKevin Cernekee 						   ALPS_REG_BASE_RUSHMORE);
1613cd401204SKevin Cernekee 		if (reg_val == -EIO)
1614cd401204SKevin Cernekee 			goto error;
1615cd401204SKevin Cernekee 		if (reg_val == -ENODEV)
1616cd401204SKevin Cernekee 			priv->flags &= ~ALPS_DUALPOINT;
1617cd401204SKevin Cernekee 	}
1618cd401204SKevin Cernekee 
1619d18e53fcSKevin Cernekee 	if (alps_enter_command_mode(psmouse) ||
16201302bac3SKevin Cernekee 	    alps_command_mode_read_reg(psmouse, 0xc2d9) == -1 ||
16211302bac3SKevin Cernekee 	    alps_command_mode_write_reg(psmouse, 0xc2cb, 0x00))
16221302bac3SKevin Cernekee 		goto error;
16231302bac3SKevin Cernekee 
16241302bac3SKevin Cernekee 	reg_val = alps_command_mode_read_reg(psmouse, 0xc2c6);
16251302bac3SKevin Cernekee 	if (reg_val == -1)
16261302bac3SKevin Cernekee 		goto error;
16271302bac3SKevin Cernekee 	if (__alps_command_mode_write_reg(psmouse, reg_val & 0xfd))
16281302bac3SKevin Cernekee 		goto error;
16291302bac3SKevin Cernekee 
16301302bac3SKevin Cernekee 	if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
16311302bac3SKevin Cernekee 		goto error;
16321302bac3SKevin Cernekee 
16331302bac3SKevin Cernekee 	/* enter absolute mode */
16341302bac3SKevin Cernekee 	reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
16351302bac3SKevin Cernekee 	if (reg_val == -1)
16361302bac3SKevin Cernekee 		goto error;
16371302bac3SKevin Cernekee 	if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
16381302bac3SKevin Cernekee 		goto error;
16391302bac3SKevin Cernekee 
16401302bac3SKevin Cernekee 	alps_exit_command_mode(psmouse);
16411302bac3SKevin Cernekee 	return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
16421302bac3SKevin Cernekee 
16431302bac3SKevin Cernekee error:
16441302bac3SKevin Cernekee 	alps_exit_command_mode(psmouse);
16451302bac3SKevin Cernekee 	return ret;
16461302bac3SKevin Cernekee }
16471302bac3SKevin Cernekee 
164825bded7cSSeth Forshee /* Must be in command mode when calling this function */
164925bded7cSSeth Forshee static int alps_absolute_mode_v4(struct psmouse *psmouse)
165025bded7cSSeth Forshee {
165125bded7cSSeth Forshee 	int reg_val;
165225bded7cSSeth Forshee 
165325bded7cSSeth Forshee 	reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
165425bded7cSSeth Forshee 	if (reg_val == -1)
165525bded7cSSeth Forshee 		return -1;
165625bded7cSSeth Forshee 
165725bded7cSSeth Forshee 	reg_val |= 0x02;
165825bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, reg_val))
165925bded7cSSeth Forshee 		return -1;
166025bded7cSSeth Forshee 
166125bded7cSSeth Forshee 	return 0;
166225bded7cSSeth Forshee }
166325bded7cSSeth Forshee 
166425bded7cSSeth Forshee static int alps_hw_init_v4(struct psmouse *psmouse)
166525bded7cSSeth Forshee {
166625bded7cSSeth Forshee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
166725bded7cSSeth Forshee 	unsigned char param[4];
166825bded7cSSeth Forshee 
1669d18e53fcSKevin Cernekee 	if (alps_enter_command_mode(psmouse))
167025bded7cSSeth Forshee 		goto error;
167125bded7cSSeth Forshee 
167225bded7cSSeth Forshee 	if (alps_absolute_mode_v4(psmouse)) {
167325bded7cSSeth Forshee 		psmouse_err(psmouse, "Failed to enter absolute mode\n");
167425bded7cSSeth Forshee 		goto error;
167525bded7cSSeth Forshee 	}
167625bded7cSSeth Forshee 
167725bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0007, 0x8c))
167825bded7cSSeth Forshee 		goto error;
167925bded7cSSeth Forshee 
168025bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0149, 0x03))
168125bded7cSSeth Forshee 		goto error;
168225bded7cSSeth Forshee 
168325bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0160, 0x03))
168425bded7cSSeth Forshee 		goto error;
168525bded7cSSeth Forshee 
168625bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x017f, 0x15))
168725bded7cSSeth Forshee 		goto error;
168825bded7cSSeth Forshee 
168925bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0151, 0x01))
169025bded7cSSeth Forshee 		goto error;
169125bded7cSSeth Forshee 
169225bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0168, 0x03))
169325bded7cSSeth Forshee 		goto error;
169425bded7cSSeth Forshee 
169525bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x014a, 0x03))
169625bded7cSSeth Forshee 		goto error;
169725bded7cSSeth Forshee 
169825bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0161, 0x03))
169925bded7cSSeth Forshee 		goto error;
170025bded7cSSeth Forshee 
170125bded7cSSeth Forshee 	alps_exit_command_mode(psmouse);
170225bded7cSSeth Forshee 
170325bded7cSSeth Forshee 	/*
170425bded7cSSeth Forshee 	 * This sequence changes the output from a 9-byte to an
170525bded7cSSeth Forshee 	 * 8-byte format. All the same data seems to be present,
170625bded7cSSeth Forshee 	 * just in a more compact format.
170725bded7cSSeth Forshee 	 */
170825bded7cSSeth Forshee 	param[0] = 0xc8;
170925bded7cSSeth Forshee 	param[1] = 0x64;
171025bded7cSSeth Forshee 	param[2] = 0x50;
171125bded7cSSeth Forshee 	if (ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
171225bded7cSSeth Forshee 	    ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE) ||
171325bded7cSSeth Forshee 	    ps2_command(ps2dev, &param[2], PSMOUSE_CMD_SETRATE) ||
171425bded7cSSeth Forshee 	    ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
171525bded7cSSeth Forshee 		return -1;
171625bded7cSSeth Forshee 
171725bded7cSSeth Forshee 	/* Set rate and enable data reporting */
171825bded7cSSeth Forshee 	param[0] = 0x64;
171925bded7cSSeth Forshee 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
172025bded7cSSeth Forshee 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
172125bded7cSSeth Forshee 		psmouse_err(psmouse, "Failed to enable data reporting\n");
172225bded7cSSeth Forshee 		return -1;
172325bded7cSSeth Forshee 	}
172425bded7cSSeth Forshee 
172525bded7cSSeth Forshee 	return 0;
172625bded7cSSeth Forshee 
172725bded7cSSeth Forshee error:
172825bded7cSSeth Forshee 	/*
172925bded7cSSeth Forshee 	 * Leaving the touchpad in command mode will essentially render
173025bded7cSSeth Forshee 	 * it unusable until the machine reboots, so exit it here just
173125bded7cSSeth Forshee 	 * to be safe
173225bded7cSSeth Forshee 	 */
173325bded7cSSeth Forshee 	alps_exit_command_mode(psmouse);
173425bded7cSSeth Forshee 	return -1;
173525bded7cSSeth Forshee }
173625bded7cSSeth Forshee 
1737ee65d4b3SYunkang Tang static int alps_dolphin_get_device_area(struct psmouse *psmouse,
1738ee65d4b3SYunkang Tang 					struct alps_data *priv)
1739ee65d4b3SYunkang Tang {
1740ee65d4b3SYunkang Tang 	struct ps2dev *ps2dev = &psmouse->ps2dev;
1741ee65d4b3SYunkang Tang 	unsigned char param[4] = {0};
1742ee65d4b3SYunkang Tang 	int num_x_electrode, num_y_electrode;
1743ee65d4b3SYunkang Tang 
1744ee65d4b3SYunkang Tang 	if (alps_enter_command_mode(psmouse))
1745ee65d4b3SYunkang Tang 		return -1;
1746ee65d4b3SYunkang Tang 
1747ee65d4b3SYunkang Tang 	param[0] = 0x0a;
1748ee65d4b3SYunkang Tang 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
1749ee65d4b3SYunkang Tang 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
1750ee65d4b3SYunkang Tang 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
1751ee65d4b3SYunkang Tang 	    ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
1752ee65d4b3SYunkang Tang 	    ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE))
1753ee65d4b3SYunkang Tang 		return -1;
1754ee65d4b3SYunkang Tang 
1755ee65d4b3SYunkang Tang 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1756ee65d4b3SYunkang Tang 		return -1;
1757ee65d4b3SYunkang Tang 
1758ee65d4b3SYunkang Tang 	/*
1759ee65d4b3SYunkang Tang 	 * Dolphin's sensor line number is not fixed. It can be calculated
1760ee65d4b3SYunkang Tang 	 * by adding the device's register value with DOLPHIN_PROFILE_X/YOFFSET.
1761ee65d4b3SYunkang Tang 	 * Further more, we can get device's x_max and y_max by multiplying
1762ee65d4b3SYunkang Tang 	 * sensor line number with DOLPHIN_COUNT_PER_ELECTRODE.
1763ee65d4b3SYunkang Tang 	 *
1764ee65d4b3SYunkang Tang 	 * e.g. When we get register's sensor_x = 11 & sensor_y = 8,
1765ee65d4b3SYunkang Tang 	 *	real sensor line number X = 11 + 8 = 19, and
1766ee65d4b3SYunkang Tang 	 *	real sensor line number Y = 8 + 1 = 9.
1767ee65d4b3SYunkang Tang 	 *	So, x_max = (19 - 1) * 64 = 1152, and
1768ee65d4b3SYunkang Tang 	 *	    y_max = (9 - 1) * 64 = 512.
1769ee65d4b3SYunkang Tang 	 */
1770ee65d4b3SYunkang Tang 	num_x_electrode = DOLPHIN_PROFILE_XOFFSET + (param[2] & 0x0F);
1771ee65d4b3SYunkang Tang 	num_y_electrode = DOLPHIN_PROFILE_YOFFSET + ((param[2] >> 4) & 0x0F);
1772ee65d4b3SYunkang Tang 	priv->x_bits = num_x_electrode;
1773ee65d4b3SYunkang Tang 	priv->y_bits = num_y_electrode;
1774ee65d4b3SYunkang Tang 	priv->x_max = (num_x_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
1775ee65d4b3SYunkang Tang 	priv->y_max = (num_y_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
1776ee65d4b3SYunkang Tang 
1777ee65d4b3SYunkang Tang 	if (alps_exit_command_mode(psmouse))
1778ee65d4b3SYunkang Tang 		return -1;
1779ee65d4b3SYunkang Tang 
1780ee65d4b3SYunkang Tang 	return 0;
1781ee65d4b3SYunkang Tang }
1782ee65d4b3SYunkang Tang 
178375af9e56SDave Turvene static int alps_hw_init_dolphin_v1(struct psmouse *psmouse)
178475af9e56SDave Turvene {
178575af9e56SDave Turvene 	struct ps2dev *ps2dev = &psmouse->ps2dev;
178675af9e56SDave Turvene 	unsigned char param[2];
178775af9e56SDave Turvene 
178875af9e56SDave Turvene 	/* This is dolphin "v1" as empirically defined by florin9doi */
178975af9e56SDave Turvene 	param[0] = 0x64;
179075af9e56SDave Turvene 	param[1] = 0x28;
179175af9e56SDave Turvene 
179275af9e56SDave Turvene 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
179375af9e56SDave Turvene 	    ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
179475af9e56SDave Turvene 	    ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
179575af9e56SDave Turvene 		return -1;
179675af9e56SDave Turvene 
179775af9e56SDave Turvene 	return 0;
179875af9e56SDave Turvene }
179975af9e56SDave Turvene 
180024af5cb9SKevin Cernekee static void alps_set_defaults(struct alps_data *priv)
180125bded7cSSeth Forshee {
1802f673ceb1SKevin Cernekee 	priv->byte0 = 0x8f;
1803f673ceb1SKevin Cernekee 	priv->mask0 = 0x8f;
1804f673ceb1SKevin Cernekee 	priv->flags = ALPS_DUALPOINT;
1805f673ceb1SKevin Cernekee 
18067a9f73e7SKevin Cernekee 	priv->x_max = 2000;
18077a9f73e7SKevin Cernekee 	priv->y_max = 1400;
18087a9f73e7SKevin Cernekee 	priv->x_bits = 15;
18097a9f73e7SKevin Cernekee 	priv->y_bits = 11;
18107a9f73e7SKevin Cernekee 
181199df65e7SKevin Cernekee 	switch (priv->proto_version) {
181225bded7cSSeth Forshee 	case ALPS_PROTO_V1:
181325bded7cSSeth Forshee 	case ALPS_PROTO_V2:
181424af5cb9SKevin Cernekee 		priv->hw_init = alps_hw_init_v1_v2;
181524af5cb9SKevin Cernekee 		priv->process_packet = alps_process_packet_v1_v2;
181624af5cb9SKevin Cernekee 		priv->set_abs_params = alps_set_abs_params_st;
181795f75e91SYunkang Tang 		priv->x_max = 1023;
181895f75e91SYunkang Tang 		priv->y_max = 767;
181925bded7cSSeth Forshee 		break;
182025bded7cSSeth Forshee 	case ALPS_PROTO_V3:
182124af5cb9SKevin Cernekee 		priv->hw_init = alps_hw_init_v3;
182224af5cb9SKevin Cernekee 		priv->process_packet = alps_process_packet_v3;
182324af5cb9SKevin Cernekee 		priv->set_abs_params = alps_set_abs_params_mt;
1824f85e5001SKevin Cernekee 		priv->decode_fields = alps_decode_pinnacle;
182550e8b216SKevin Cernekee 		priv->nibble_commands = alps_v3_nibble_commands;
182650e8b216SKevin Cernekee 		priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
182725bded7cSSeth Forshee 		break;
182825bded7cSSeth Forshee 	case ALPS_PROTO_V4:
182924af5cb9SKevin Cernekee 		priv->hw_init = alps_hw_init_v4;
183024af5cb9SKevin Cernekee 		priv->process_packet = alps_process_packet_v4;
183124af5cb9SKevin Cernekee 		priv->set_abs_params = alps_set_abs_params_mt;
183250e8b216SKevin Cernekee 		priv->nibble_commands = alps_v4_nibble_commands;
183350e8b216SKevin Cernekee 		priv->addr_command = PSMOUSE_CMD_DISABLE;
183425bded7cSSeth Forshee 		break;
183575af9e56SDave Turvene 	case ALPS_PROTO_V5:
183675af9e56SDave Turvene 		priv->hw_init = alps_hw_init_dolphin_v1;
1837ee65d4b3SYunkang Tang 		priv->process_packet = alps_process_touchpad_packet_v3_v5;
183875af9e56SDave Turvene 		priv->decode_fields = alps_decode_dolphin;
183975af9e56SDave Turvene 		priv->set_abs_params = alps_set_abs_params_mt;
184075af9e56SDave Turvene 		priv->nibble_commands = alps_v3_nibble_commands;
184175af9e56SDave Turvene 		priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
184275af9e56SDave Turvene 		priv->byte0 = 0xc8;
1843ee65d4b3SYunkang Tang 		priv->mask0 = 0xd8;
184475af9e56SDave Turvene 		priv->flags = 0;
184575af9e56SDave Turvene 		priv->x_max = 1360;
184675af9e56SDave Turvene 		priv->y_max = 660;
184775af9e56SDave Turvene 		priv->x_bits = 23;
184875af9e56SDave Turvene 		priv->y_bits = 12;
184975af9e56SDave Turvene 		break;
185095f75e91SYunkang Tang 	case ALPS_PROTO_V6:
185195f75e91SYunkang Tang 		priv->hw_init = alps_hw_init_v6;
185295f75e91SYunkang Tang 		priv->process_packet = alps_process_packet_v6;
185395f75e91SYunkang Tang 		priv->set_abs_params = alps_set_abs_params_st;
185495f75e91SYunkang Tang 		priv->nibble_commands = alps_v6_nibble_commands;
185595f75e91SYunkang Tang 		priv->x_max = 2047;
185695f75e91SYunkang Tang 		priv->y_max = 1535;
185795f75e91SYunkang Tang 		break;
185825bded7cSSeth Forshee 	}
185925bded7cSSeth Forshee }
186025bded7cSSeth Forshee 
1861b5d6b851SKevin Cernekee static int alps_match_table(struct psmouse *psmouse, struct alps_data *priv,
1862b5d6b851SKevin Cernekee 			    unsigned char *e7, unsigned char *ec)
18632e992cc0SKevin Cernekee {
1864b5d6b851SKevin Cernekee 	const struct alps_model_info *model;
18652e992cc0SKevin Cernekee 	int i;
18662e992cc0SKevin Cernekee 
1867b5d6b851SKevin Cernekee 	for (i = 0; i < ARRAY_SIZE(alps_model_data); i++) {
1868b5d6b851SKevin Cernekee 		model = &alps_model_data[i];
1869b5d6b851SKevin Cernekee 
1870b5d6b851SKevin Cernekee 		if (!memcmp(e7, model->signature, sizeof(model->signature)) &&
1871b5d6b851SKevin Cernekee 		    (!model->command_mode_resp ||
1872b5d6b851SKevin Cernekee 		     model->command_mode_resp == ec[2])) {
1873b5d6b851SKevin Cernekee 
1874b5d6b851SKevin Cernekee 			priv->proto_version = model->proto_version;
187524af5cb9SKevin Cernekee 			alps_set_defaults(priv);
187624af5cb9SKevin Cernekee 
1877b5d6b851SKevin Cernekee 			priv->flags = model->flags;
1878b5d6b851SKevin Cernekee 			priv->byte0 = model->byte0;
1879b5d6b851SKevin Cernekee 			priv->mask0 = model->mask0;
1880b5d6b851SKevin Cernekee 
1881b5d6b851SKevin Cernekee 			return 0;
1882b5d6b851SKevin Cernekee 		}
1883b5d6b851SKevin Cernekee 	}
1884b5d6b851SKevin Cernekee 
1885b5d6b851SKevin Cernekee 	return -EINVAL;
1886b5d6b851SKevin Cernekee }
1887b5d6b851SKevin Cernekee 
1888b5d6b851SKevin Cernekee static int alps_identify(struct psmouse *psmouse, struct alps_data *priv)
1889b5d6b851SKevin Cernekee {
1890b5d6b851SKevin Cernekee 	unsigned char e6[4], e7[4], ec[4];
1891b5d6b851SKevin Cernekee 
18922e992cc0SKevin Cernekee 	/*
18932e992cc0SKevin Cernekee 	 * First try "E6 report".
18942e992cc0SKevin Cernekee 	 * ALPS should return 0,0,10 or 0,0,100 if no buttons are pressed.
18952e992cc0SKevin Cernekee 	 * The bits 0-2 of the first byte will be 1s if some buttons are
18962e992cc0SKevin Cernekee 	 * pressed.
18972e992cc0SKevin Cernekee 	 */
1898b5d6b851SKevin Cernekee 	if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
1899b5d6b851SKevin Cernekee 			 PSMOUSE_CMD_SETSCALE11, e6))
1900b5d6b851SKevin Cernekee 		return -EIO;
19012e992cc0SKevin Cernekee 
1902b5d6b851SKevin Cernekee 	if ((e6[0] & 0xf8) != 0 || e6[1] != 0 || (e6[2] != 10 && e6[2] != 100))
1903b5d6b851SKevin Cernekee 		return -EINVAL;
19042e992cc0SKevin Cernekee 
19052e992cc0SKevin Cernekee 	/*
1906b5d6b851SKevin Cernekee 	 * Now get the "E7" and "EC" reports.  These will uniquely identify
1907b5d6b851SKevin Cernekee 	 * most ALPS touchpads.
19082e992cc0SKevin Cernekee 	 */
1909b5d6b851SKevin Cernekee 	if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
1910b5d6b851SKevin Cernekee 			 PSMOUSE_CMD_SETSCALE21, e7) ||
1911b5d6b851SKevin Cernekee 	    alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
1912b5d6b851SKevin Cernekee 			 PSMOUSE_CMD_RESET_WRAP, ec) ||
1913b5d6b851SKevin Cernekee 	    alps_exit_command_mode(psmouse))
1914b5d6b851SKevin Cernekee 		return -EIO;
19152e992cc0SKevin Cernekee 
1916f673ceb1SKevin Cernekee 	if (alps_match_table(psmouse, priv, e7, ec) == 0) {
1917b5d6b851SKevin Cernekee 		return 0;
191875af9e56SDave Turvene 	} else if (e7[0] == 0x73 && e7[1] == 0x03 && e7[2] == 0x50 &&
1919ee65d4b3SYunkang Tang 		   ec[0] == 0x73 && (ec[1] == 0x01 || ec[1] == 0x02)) {
192075af9e56SDave Turvene 		priv->proto_version = ALPS_PROTO_V5;
192175af9e56SDave Turvene 		alps_set_defaults(priv);
1922ee65d4b3SYunkang Tang 		if (alps_dolphin_get_device_area(psmouse, priv))
1923ee65d4b3SYunkang Tang 			return -EIO;
1924ee65d4b3SYunkang Tang 		else
192575af9e56SDave Turvene 			return 0;
19261302bac3SKevin Cernekee 	} else if (ec[0] == 0x88 && ec[1] == 0x08) {
19271302bac3SKevin Cernekee 		priv->proto_version = ALPS_PROTO_V3;
19281302bac3SKevin Cernekee 		alps_set_defaults(priv);
19291302bac3SKevin Cernekee 
19301302bac3SKevin Cernekee 		priv->hw_init = alps_hw_init_rushmore_v3;
19311302bac3SKevin Cernekee 		priv->decode_fields = alps_decode_rushmore;
19321302bac3SKevin Cernekee 		priv->x_bits = 16;
19331302bac3SKevin Cernekee 		priv->y_bits = 12;
193440e8f53bSHans de Goede 		priv->flags |= ALPS_IS_RUSHMORE;
19351302bac3SKevin Cernekee 
1936cd401204SKevin Cernekee 		/* hack to make addr_command, nibble_command available */
1937cd401204SKevin Cernekee 		psmouse->private = priv;
1938cd401204SKevin Cernekee 
1939cd401204SKevin Cernekee 		if (alps_probe_trackstick_v3(psmouse, ALPS_REG_BASE_RUSHMORE))
1940cd401204SKevin Cernekee 			priv->flags &= ~ALPS_DUALPOINT;
1941cd401204SKevin Cernekee 
19421302bac3SKevin Cernekee 		return 0;
1943f673ceb1SKevin Cernekee 	} else if (ec[0] == 0x88 && ec[1] == 0x07 &&
1944f673ceb1SKevin Cernekee 		   ec[2] >= 0x90 && ec[2] <= 0x9d) {
1945f673ceb1SKevin Cernekee 		priv->proto_version = ALPS_PROTO_V3;
1946f673ceb1SKevin Cernekee 		alps_set_defaults(priv);
1947f673ceb1SKevin Cernekee 
1948f673ceb1SKevin Cernekee 		return 0;
1949f673ceb1SKevin Cernekee 	}
19502e992cc0SKevin Cernekee 
1951b5d6b851SKevin Cernekee 	psmouse_info(psmouse,
195239fbe585SDmitry Torokhov 		     "Unknown ALPS touchpad: E7=%3ph, EC=%3ph\n", e7, ec);
19532e992cc0SKevin Cernekee 
1954b5d6b851SKevin Cernekee 	return -EINVAL;
19552e992cc0SKevin Cernekee }
19562e992cc0SKevin Cernekee 
19571e0c5b12SDmitry Torokhov static int alps_reconnect(struct psmouse *psmouse)
19581e0c5b12SDmitry Torokhov {
1959b5d6b851SKevin Cernekee 	struct alps_data *priv = psmouse->private;
196071bb21b6SMaxim Levitsky 
19611e0c5b12SDmitry Torokhov 	psmouse_reset(psmouse);
19621e0c5b12SDmitry Torokhov 
1963b5d6b851SKevin Cernekee 	if (alps_identify(psmouse, priv) < 0)
19641e0c5b12SDmitry Torokhov 		return -1;
19651e0c5b12SDmitry Torokhov 
196624af5cb9SKevin Cernekee 	return priv->hw_init(psmouse);
19671da177e4SLinus Torvalds }
19681da177e4SLinus Torvalds 
19691da177e4SLinus Torvalds static void alps_disconnect(struct psmouse *psmouse)
19701da177e4SLinus Torvalds {
19711da177e4SLinus Torvalds 	struct alps_data *priv = psmouse->private;
19722e5b636bSDmitry Torokhov 
19731da177e4SLinus Torvalds 	psmouse_reset(psmouse);
19741d9f2626SSebastian Kapfer 	del_timer_sync(&priv->timer);
19752e5b636bSDmitry Torokhov 	input_unregister_device(priv->dev2);
19761da177e4SLinus Torvalds 	kfree(priv);
19771da177e4SLinus Torvalds }
19781da177e4SLinus Torvalds 
197924af5cb9SKevin Cernekee static void alps_set_abs_params_st(struct alps_data *priv,
198024af5cb9SKevin Cernekee 				   struct input_dev *dev1)
198124af5cb9SKevin Cernekee {
198295f75e91SYunkang Tang 	input_set_abs_params(dev1, ABS_X, 0, priv->x_max, 0, 0);
198395f75e91SYunkang Tang 	input_set_abs_params(dev1, ABS_Y, 0, priv->y_max, 0, 0);
198424af5cb9SKevin Cernekee }
198524af5cb9SKevin Cernekee 
198624af5cb9SKevin Cernekee static void alps_set_abs_params_mt(struct alps_data *priv,
198724af5cb9SKevin Cernekee 				   struct input_dev *dev1)
198824af5cb9SKevin Cernekee {
19897a9f73e7SKevin Cernekee 	input_set_abs_params(dev1, ABS_MT_POSITION_X, 0, priv->x_max, 0, 0);
19907a9f73e7SKevin Cernekee 	input_set_abs_params(dev1, ABS_MT_POSITION_Y, 0, priv->y_max, 0, 0);
199124af5cb9SKevin Cernekee 
1992cdf333efSHans de Goede 	input_mt_init_slots(dev1, MAX_TOUCHES, INPUT_MT_POINTER |
1993cdf333efSHans de Goede 		INPUT_MT_DROP_UNUSED | INPUT_MT_TRACK | INPUT_MT_SEMI_MT);
1994cdf333efSHans de Goede 
199524af5cb9SKevin Cernekee 	set_bit(BTN_TOOL_TRIPLETAP, dev1->keybit);
199624af5cb9SKevin Cernekee 	set_bit(BTN_TOOL_QUADTAP, dev1->keybit);
199724af5cb9SKevin Cernekee }
199824af5cb9SKevin Cernekee 
19991da177e4SLinus Torvalds int alps_init(struct psmouse *psmouse)
20001da177e4SLinus Torvalds {
20011da177e4SLinus Torvalds 	struct alps_data *priv;
20022e5b636bSDmitry Torokhov 	struct input_dev *dev1 = psmouse->dev, *dev2;
20031da177e4SLinus Torvalds 
2004f42649e8SDmitry Torokhov 	priv = kzalloc(sizeof(struct alps_data), GFP_KERNEL);
20052e5b636bSDmitry Torokhov 	dev2 = input_allocate_device();
20062e5b636bSDmitry Torokhov 	if (!priv || !dev2)
20071da177e4SLinus Torvalds 		goto init_fail;
20082e5b636bSDmitry Torokhov 
20092e5b636bSDmitry Torokhov 	priv->dev2 = dev2;
20101d9f2626SSebastian Kapfer 	setup_timer(&priv->timer, alps_flush_packet, (unsigned long)psmouse);
20111d9f2626SSebastian Kapfer 
20121e0c5b12SDmitry Torokhov 	psmouse->private = priv;
20131da177e4SLinus Torvalds 
201425bded7cSSeth Forshee 	psmouse_reset(psmouse);
201525bded7cSSeth Forshee 
2016b5d6b851SKevin Cernekee 	if (alps_identify(psmouse, priv) < 0)
201771bb21b6SMaxim Levitsky 		goto init_fail;
201871bb21b6SMaxim Levitsky 
201924af5cb9SKevin Cernekee 	if (priv->hw_init(psmouse))
20201da177e4SLinus Torvalds 		goto init_fail;
20211da177e4SLinus Torvalds 
20227105d2eaSDmitry Torokhov 	/*
20237105d2eaSDmitry Torokhov 	 * Undo part of setup done for us by psmouse core since touchpad
20247105d2eaSDmitry Torokhov 	 * is not a relative device.
20257105d2eaSDmitry Torokhov 	 */
20267105d2eaSDmitry Torokhov 	__clear_bit(EV_REL, dev1->evbit);
20277105d2eaSDmitry Torokhov 	__clear_bit(REL_X, dev1->relbit);
20287105d2eaSDmitry Torokhov 	__clear_bit(REL_Y, dev1->relbit);
20297105d2eaSDmitry Torokhov 
20307105d2eaSDmitry Torokhov 	/*
20317105d2eaSDmitry Torokhov 	 * Now set up our capabilities.
20327105d2eaSDmitry Torokhov 	 */
20337b19ada2SJiri Slaby 	dev1->evbit[BIT_WORD(EV_KEY)] |= BIT_MASK(EV_KEY);
20347b19ada2SJiri Slaby 	dev1->keybit[BIT_WORD(BTN_TOUCH)] |= BIT_MASK(BTN_TOUCH);
20357b19ada2SJiri Slaby 	dev1->keybit[BIT_WORD(BTN_TOOL_FINGER)] |= BIT_MASK(BTN_TOOL_FINGER);
203671bb21b6SMaxim Levitsky 	dev1->keybit[BIT_WORD(BTN_LEFT)] |=
203771bb21b6SMaxim Levitsky 		BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
20381da177e4SLinus Torvalds 
20397b19ada2SJiri Slaby 	dev1->evbit[BIT_WORD(EV_ABS)] |= BIT_MASK(EV_ABS);
204025bded7cSSeth Forshee 
204124af5cb9SKevin Cernekee 	priv->set_abs_params(priv, dev1);
20422e5b636bSDmitry Torokhov 	input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
20431da177e4SLinus Torvalds 
204499df65e7SKevin Cernekee 	if (priv->flags & ALPS_WHEEL) {
20457b19ada2SJiri Slaby 		dev1->evbit[BIT_WORD(EV_REL)] |= BIT_MASK(EV_REL);
20467b19ada2SJiri Slaby 		dev1->relbit[BIT_WORD(REL_WHEEL)] |= BIT_MASK(REL_WHEEL);
20471da177e4SLinus Torvalds 	}
20481da177e4SLinus Torvalds 
204999df65e7SKevin Cernekee 	if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
20507b19ada2SJiri Slaby 		dev1->keybit[BIT_WORD(BTN_FORWARD)] |= BIT_MASK(BTN_FORWARD);
20517b19ada2SJiri Slaby 		dev1->keybit[BIT_WORD(BTN_BACK)] |= BIT_MASK(BTN_BACK);
20521da177e4SLinus Torvalds 	}
20531da177e4SLinus Torvalds 
205499df65e7SKevin Cernekee 	if (priv->flags & ALPS_FOUR_BUTTONS) {
205571bb21b6SMaxim Levitsky 		dev1->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_0);
205671bb21b6SMaxim Levitsky 		dev1->keybit[BIT_WORD(BTN_1)] |= BIT_MASK(BTN_1);
205771bb21b6SMaxim Levitsky 		dev1->keybit[BIT_WORD(BTN_2)] |= BIT_MASK(BTN_2);
205871bb21b6SMaxim Levitsky 		dev1->keybit[BIT_WORD(BTN_3)] |= BIT_MASK(BTN_3);
205971bb21b6SMaxim Levitsky 	} else {
206071bb21b6SMaxim Levitsky 		dev1->keybit[BIT_WORD(BTN_MIDDLE)] |= BIT_MASK(BTN_MIDDLE);
206171bb21b6SMaxim Levitsky 	}
206271bb21b6SMaxim Levitsky 
206308ffce45SDmitry Torokhov 	snprintf(priv->phys, sizeof(priv->phys), "%s/input1", psmouse->ps2dev.serio->phys);
20642e5b636bSDmitry Torokhov 	dev2->phys = priv->phys;
206599df65e7SKevin Cernekee 	dev2->name = (priv->flags & ALPS_DUALPOINT) ?
20669354f263SYunkang Tang 		     "DualPoint Stick" : "ALPS PS/2 Device";
20672e5b636bSDmitry Torokhov 	dev2->id.bustype = BUS_I8042;
20682e5b636bSDmitry Torokhov 	dev2->id.vendor  = 0x0002;
20692e5b636bSDmitry Torokhov 	dev2->id.product = PSMOUSE_ALPS;
20702e5b636bSDmitry Torokhov 	dev2->id.version = 0x0000;
20711db3a345SDmitry Torokhov 	dev2->dev.parent = &psmouse->ps2dev.serio->dev;
20721da177e4SLinus Torvalds 
20737b19ada2SJiri Slaby 	dev2->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
207471bb21b6SMaxim Levitsky 	dev2->relbit[BIT_WORD(REL_X)] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
207571bb21b6SMaxim Levitsky 	dev2->keybit[BIT_WORD(BTN_LEFT)] =
207671bb21b6SMaxim Levitsky 		BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_MIDDLE) | BIT_MASK(BTN_RIGHT);
20771da177e4SLinus Torvalds 
2078f42649e8SDmitry Torokhov 	if (input_register_device(priv->dev2))
2079f42649e8SDmitry Torokhov 		goto init_fail;
20801da177e4SLinus Torvalds 
20811da177e4SLinus Torvalds 	psmouse->protocol_handler = alps_process_byte;
2082f0d5c6f4SDmitry Torokhov 	psmouse->poll = alps_poll;
20831da177e4SLinus Torvalds 	psmouse->disconnect = alps_disconnect;
20841da177e4SLinus Torvalds 	psmouse->reconnect = alps_reconnect;
208599df65e7SKevin Cernekee 	psmouse->pktsize = priv->proto_version == ALPS_PROTO_V4 ? 8 : 6;
20861da177e4SLinus Torvalds 
2087f0d5c6f4SDmitry Torokhov 	/* We are having trouble resyncing ALPS touchpads so disable it for now */
2088f0d5c6f4SDmitry Torokhov 	psmouse->resync_time = 0;
2089f0d5c6f4SDmitry Torokhov 
20901da177e4SLinus Torvalds 	return 0;
20911da177e4SLinus Torvalds 
20921da177e4SLinus Torvalds init_fail:
2093f42649e8SDmitry Torokhov 	psmouse_reset(psmouse);
20942e5b636bSDmitry Torokhov 	input_free_device(dev2);
20951da177e4SLinus Torvalds 	kfree(priv);
20961e0c5b12SDmitry Torokhov 	psmouse->private = NULL;
20971da177e4SLinus Torvalds 	return -1;
20981da177e4SLinus Torvalds }
20991da177e4SLinus Torvalds 
2100b7802c5cSDmitry Torokhov int alps_detect(struct psmouse *psmouse, bool set_properties)
21011da177e4SLinus Torvalds {
2102b5d6b851SKevin Cernekee 	struct alps_data dummy;
21031da177e4SLinus Torvalds 
2104b5d6b851SKevin Cernekee 	if (alps_identify(psmouse, &dummy) < 0)
21051da177e4SLinus Torvalds 		return -1;
21061da177e4SLinus Torvalds 
21071da177e4SLinus Torvalds 	if (set_properties) {
21081da177e4SLinus Torvalds 		psmouse->vendor = "ALPS";
2109b5d6b851SKevin Cernekee 		psmouse->name = dummy.flags & ALPS_DUALPOINT ?
2110968ac842SDmitry Torokhov 				"DualPoint TouchPad" : "GlidePoint";
2111b5d6b851SKevin Cernekee 		psmouse->model = dummy.proto_version << 8;
21121da177e4SLinus Torvalds 	}
21131da177e4SLinus Torvalds 	return 0;
21141da177e4SLinus Torvalds }
21151da177e4SLinus Torvalds 
2116