xref: /openbmc/linux/drivers/input/mouse/alps.c (revision 9d720b34)
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 */
1033808843cSYunkang Tang #define ALPS_BUTTONPAD		0x200	/* device is a clickpad */
1041da177e4SLinus Torvalds 
105e38de678SHelge Deller static const struct alps_model_info alps_model_data[] = {
10625bded7cSSeth Forshee 	{ { 0x32, 0x02, 0x14 },	0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT },	/* Toshiba Salellite Pro M10 */
10725bded7cSSeth Forshee 	{ { 0x33, 0x02, 0x0a },	0x00, ALPS_PROTO_V1, 0x88, 0xf8, 0 },				/* UMAX-530T */
10825bded7cSSeth Forshee 	{ { 0x53, 0x02, 0x0a },	0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
10925bded7cSSeth Forshee 	{ { 0x53, 0x02, 0x14 },	0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
11025bded7cSSeth Forshee 	{ { 0x60, 0x03, 0xc8 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },				/* HP ze1115 */
11125bded7cSSeth Forshee 	{ { 0x63, 0x02, 0x0a },	0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
11225bded7cSSeth Forshee 	{ { 0x63, 0x02, 0x14 },	0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
11325bded7cSSeth Forshee 	{ { 0x63, 0x02, 0x28 },	0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 },		/* Fujitsu Siemens S6010 */
11425bded7cSSeth Forshee 	{ { 0x63, 0x02, 0x3c },	0x00, ALPS_PROTO_V2, 0x8f, 0x8f, ALPS_WHEEL },			/* Toshiba Satellite S2400-103 */
11525bded7cSSeth Forshee 	{ { 0x63, 0x02, 0x50 },	0x00, ALPS_PROTO_V2, 0xef, 0xef, ALPS_FW_BK_1 },		/* NEC Versa L320 */
11625bded7cSSeth Forshee 	{ { 0x63, 0x02, 0x64 },	0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
11725bded7cSSeth Forshee 	{ { 0x63, 0x03, 0xc8 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT },	/* Dell Latitude D800 */
11825bded7cSSeth Forshee 	{ { 0x73, 0x00, 0x0a },	0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_DUALPOINT },		/* ThinkPad R61 8918-5QG */
11925bded7cSSeth Forshee 	{ { 0x73, 0x02, 0x0a },	0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
12025bded7cSSeth Forshee 	{ { 0x73, 0x02, 0x14 },	0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 },		/* Ahtec Laptop */
12125bded7cSSeth Forshee 	{ { 0x20, 0x02, 0x0e },	0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT },	/* XXX */
12225bded7cSSeth Forshee 	{ { 0x22, 0x02, 0x0a },	0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT },
12325bded7cSSeth Forshee 	{ { 0x22, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xff, 0xff, ALPS_PASS | ALPS_DUALPOINT },	/* Dell Latitude D600 */
1241d9f2626SSebastian Kapfer 	/* Dell Latitude E5500, E6400, E6500, Precision M4400 */
12525bded7cSSeth Forshee 	{ { 0x62, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xcf, 0xcf,
1261d9f2626SSebastian Kapfer 		ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED },
12795f75e91SYunkang Tang 	{ { 0x73, 0x00, 0x14 }, 0x00, ALPS_PROTO_V6, 0xff, 0xff, ALPS_DUALPOINT },		/* Dell XT2 */
12825bded7cSSeth Forshee 	{ { 0x73, 0x02, 0x50 }, 0x00, ALPS_PROTO_V2, 0xcf, 0xcf, ALPS_FOUR_BUTTONS },		/* Dell Vostro 1400 */
12925bded7cSSeth Forshee 	{ { 0x52, 0x01, 0x14 }, 0x00, ALPS_PROTO_V2, 0xff, 0xff,
130eb8bff85SThomas Bächler 		ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED },				/* Toshiba Tecra A11-11L */
13125bded7cSSeth Forshee 	{ { 0x73, 0x02, 0x64 },	0x8a, ALPS_PROTO_V4, 0x8f, 0x8f, 0 },
1321da177e4SLinus Torvalds };
1331da177e4SLinus Torvalds 
13424af5cb9SKevin Cernekee static void alps_set_abs_params_st(struct alps_data *priv,
13524af5cb9SKevin Cernekee 				   struct input_dev *dev1);
13624af5cb9SKevin Cernekee static void alps_set_abs_params_mt(struct alps_data *priv,
13724af5cb9SKevin Cernekee 				   struct input_dev *dev1);
13824af5cb9SKevin Cernekee 
1391da177e4SLinus Torvalds /*
1401da177e4SLinus Torvalds  * XXX - this entry is suspicious. First byte has zero lower nibble,
1411da177e4SLinus Torvalds  * which is what a normal mouse would report. Also, the value 0x0e
1421da177e4SLinus Torvalds  * isn't valid per PS/2 spec.
1431da177e4SLinus Torvalds  */
1441da177e4SLinus Torvalds 
145d4b347b2SSeth Forshee /* Packet formats are described in Documentation/input/alps.txt */
1461da177e4SLinus Torvalds 
14799df65e7SKevin Cernekee static bool alps_is_valid_first_byte(struct alps_data *priv,
1481d9f2626SSebastian Kapfer 				     unsigned char data)
1491d9f2626SSebastian Kapfer {
15099df65e7SKevin Cernekee 	return (data & priv->mask0) == priv->byte0;
1511d9f2626SSebastian Kapfer }
1521d9f2626SSebastian Kapfer 
1531d9f2626SSebastian Kapfer static void alps_report_buttons(struct psmouse *psmouse,
1541d9f2626SSebastian Kapfer 				struct input_dev *dev1, struct input_dev *dev2,
1551d9f2626SSebastian Kapfer 				int left, int right, int middle)
1561d9f2626SSebastian Kapfer {
1571d9f2626SSebastian Kapfer 	struct input_dev *dev;
1581d9f2626SSebastian Kapfer 
1591d9f2626SSebastian Kapfer 	/*
1601d9f2626SSebastian Kapfer 	 * If shared button has already been reported on the
1611d9f2626SSebastian Kapfer 	 * other device (dev2) then this event should be also
1621d9f2626SSebastian Kapfer 	 * sent through that device.
1631d9f2626SSebastian Kapfer 	 */
1641d9f2626SSebastian Kapfer 	dev = test_bit(BTN_LEFT, dev2->key) ? dev2 : dev1;
1651d9f2626SSebastian Kapfer 	input_report_key(dev, BTN_LEFT, left);
1661d9f2626SSebastian Kapfer 
1671d9f2626SSebastian Kapfer 	dev = test_bit(BTN_RIGHT, dev2->key) ? dev2 : dev1;
1681d9f2626SSebastian Kapfer 	input_report_key(dev, BTN_RIGHT, right);
1691d9f2626SSebastian Kapfer 
1701d9f2626SSebastian Kapfer 	dev = test_bit(BTN_MIDDLE, dev2->key) ? dev2 : dev1;
1711d9f2626SSebastian Kapfer 	input_report_key(dev, BTN_MIDDLE, middle);
1721d9f2626SSebastian Kapfer 
1731d9f2626SSebastian Kapfer 	/*
1741d9f2626SSebastian Kapfer 	 * Sync the _other_ device now, we'll do the first
1751d9f2626SSebastian Kapfer 	 * device later once we report the rest of the events.
1761d9f2626SSebastian Kapfer 	 */
1771d9f2626SSebastian Kapfer 	input_sync(dev2);
1781d9f2626SSebastian Kapfer }
1791d9f2626SSebastian Kapfer 
18025bded7cSSeth Forshee static void alps_process_packet_v1_v2(struct psmouse *psmouse)
1811da177e4SLinus Torvalds {
1821da177e4SLinus Torvalds 	struct alps_data *priv = psmouse->private;
1831da177e4SLinus Torvalds 	unsigned char *packet = psmouse->packet;
1842e5b636bSDmitry Torokhov 	struct input_dev *dev = psmouse->dev;
1852e5b636bSDmitry Torokhov 	struct input_dev *dev2 = priv->dev2;
1861da177e4SLinus Torvalds 	int x, y, z, ges, fin, left, right, middle;
187c30b4c10SIvan Casado Ruiz 	int back = 0, forward = 0;
1881da177e4SLinus Torvalds 
18999df65e7SKevin Cernekee 	if (priv->proto_version == ALPS_PROTO_V1) {
190d2f4012fSYotam Medini 		left = packet[2] & 0x10;
191d2f4012fSYotam Medini 		right = packet[2] & 0x08;
1921da177e4SLinus Torvalds 		middle = 0;
1931da177e4SLinus Torvalds 		x = packet[1] | ((packet[0] & 0x07) << 7);
1941da177e4SLinus Torvalds 		y = packet[4] | ((packet[3] & 0x07) << 7);
1951da177e4SLinus Torvalds 		z = packet[5];
1961da177e4SLinus Torvalds 	} else {
1971da177e4SLinus Torvalds 		left = packet[3] & 1;
1981da177e4SLinus Torvalds 		right = packet[3] & 2;
1991da177e4SLinus Torvalds 		middle = packet[3] & 4;
2001da177e4SLinus Torvalds 		x = packet[1] | ((packet[2] & 0x78) << (7 - 3));
2011da177e4SLinus Torvalds 		y = packet[4] | ((packet[3] & 0x70) << (7 - 4));
2021da177e4SLinus Torvalds 		z = packet[5];
2031da177e4SLinus Torvalds 	}
2041da177e4SLinus Torvalds 
20599df65e7SKevin Cernekee 	if (priv->flags & ALPS_FW_BK_1) {
2063c00bb96SLaszlo Kajan 		back = packet[0] & 0x10;
2073c00bb96SLaszlo Kajan 		forward = packet[2] & 4;
208c30b4c10SIvan Casado Ruiz 	}
209c30b4c10SIvan Casado Ruiz 
21099df65e7SKevin Cernekee 	if (priv->flags & ALPS_FW_BK_2) {
211c30b4c10SIvan Casado Ruiz 		back = packet[3] & 4;
212c30b4c10SIvan Casado Ruiz 		forward = packet[2] & 4;
213c30b4c10SIvan Casado Ruiz 		if ((middle = forward && back))
214c30b4c10SIvan Casado Ruiz 			forward = back = 0;
215c30b4c10SIvan Casado Ruiz 	}
216c30b4c10SIvan Casado Ruiz 
2171da177e4SLinus Torvalds 	ges = packet[2] & 1;
2181da177e4SLinus Torvalds 	fin = packet[2] & 2;
2191da177e4SLinus Torvalds 
22099df65e7SKevin Cernekee 	if ((priv->flags & ALPS_DUALPOINT) && z == 127) {
2211da177e4SLinus Torvalds 		input_report_rel(dev2, REL_X,  (x > 383 ? (x - 768) : x));
2221da177e4SLinus Torvalds 		input_report_rel(dev2, REL_Y, -(y > 255 ? (y - 512) : y));
223d7ed5d88SUlrich Dangel 
2241d9f2626SSebastian Kapfer 		alps_report_buttons(psmouse, dev2, dev, left, right, middle);
225d7ed5d88SUlrich Dangel 
2261da177e4SLinus Torvalds 		input_sync(dev2);
2271da177e4SLinus Torvalds 		return;
2281da177e4SLinus Torvalds 	}
2291da177e4SLinus Torvalds 
2301d9f2626SSebastian Kapfer 	alps_report_buttons(psmouse, dev, dev2, left, right, middle);
231d7ed5d88SUlrich Dangel 
2321da177e4SLinus Torvalds 	/* Convert hardware tap to a reasonable Z value */
23371bb21b6SMaxim Levitsky 	if (ges && !fin)
23471bb21b6SMaxim Levitsky 		z = 40;
2351da177e4SLinus Torvalds 
2361da177e4SLinus Torvalds 	/*
2371da177e4SLinus Torvalds 	 * A "tap and drag" operation is reported by the hardware as a transition
2381da177e4SLinus Torvalds 	 * from (!fin && ges) to (fin && ges). This should be translated to the
2391da177e4SLinus Torvalds 	 * sequence Z>0, Z==0, Z>0, so the Z==0 event has to be generated manually.
2401da177e4SLinus Torvalds 	 */
2411da177e4SLinus Torvalds 	if (ges && fin && !priv->prev_fin) {
2421da177e4SLinus Torvalds 		input_report_abs(dev, ABS_X, x);
2431da177e4SLinus Torvalds 		input_report_abs(dev, ABS_Y, y);
2441da177e4SLinus Torvalds 		input_report_abs(dev, ABS_PRESSURE, 0);
2451da177e4SLinus Torvalds 		input_report_key(dev, BTN_TOOL_FINGER, 0);
2461da177e4SLinus Torvalds 		input_sync(dev);
2471da177e4SLinus Torvalds 	}
2481da177e4SLinus Torvalds 	priv->prev_fin = fin;
2491da177e4SLinus Torvalds 
25071bb21b6SMaxim Levitsky 	if (z > 30)
25171bb21b6SMaxim Levitsky 		input_report_key(dev, BTN_TOUCH, 1);
25271bb21b6SMaxim Levitsky 	if (z < 25)
25371bb21b6SMaxim Levitsky 		input_report_key(dev, BTN_TOUCH, 0);
2541da177e4SLinus Torvalds 
2551da177e4SLinus Torvalds 	if (z > 0) {
2561da177e4SLinus Torvalds 		input_report_abs(dev, ABS_X, x);
2571da177e4SLinus Torvalds 		input_report_abs(dev, ABS_Y, y);
2581da177e4SLinus Torvalds 	}
2591da177e4SLinus Torvalds 
2601da177e4SLinus Torvalds 	input_report_abs(dev, ABS_PRESSURE, z);
2611da177e4SLinus Torvalds 	input_report_key(dev, BTN_TOOL_FINGER, z > 0);
2621da177e4SLinus Torvalds 
26399df65e7SKevin Cernekee 	if (priv->flags & ALPS_WHEEL)
264e6c047b9SVojtech Pavlik 		input_report_rel(dev, REL_WHEEL, ((packet[2] << 1) & 0x08) - ((packet[0] >> 4) & 0x07));
2651da177e4SLinus Torvalds 
26699df65e7SKevin Cernekee 	if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
267c30b4c10SIvan Casado Ruiz 		input_report_key(dev, BTN_FORWARD, forward);
268c30b4c10SIvan Casado Ruiz 		input_report_key(dev, BTN_BACK, back);
2691da177e4SLinus Torvalds 	}
2701da177e4SLinus Torvalds 
27199df65e7SKevin Cernekee 	if (priv->flags & ALPS_FOUR_BUTTONS) {
27271bb21b6SMaxim Levitsky 		input_report_key(dev, BTN_0, packet[2] & 4);
27371bb21b6SMaxim Levitsky 		input_report_key(dev, BTN_1, packet[0] & 0x10);
27471bb21b6SMaxim Levitsky 		input_report_key(dev, BTN_2, packet[3] & 4);
27571bb21b6SMaxim Levitsky 		input_report_key(dev, BTN_3, packet[0] & 0x20);
27671bb21b6SMaxim Levitsky 	}
27771bb21b6SMaxim Levitsky 
2781da177e4SLinus Torvalds 	input_sync(dev);
2791da177e4SLinus Torvalds }
2801da177e4SLinus Torvalds 
28101ce661fSSeth Forshee /*
282ee65d4b3SYunkang Tang  * Process bitmap data for V5 protocols. Return value is null.
283ee65d4b3SYunkang Tang  *
284ee65d4b3SYunkang Tang  * The bitmaps don't have enough data to track fingers, so this function
285ee65d4b3SYunkang Tang  * only generates points representing a bounding box of at most two contacts.
28602d04254SHans de Goede  * These two points are returned in fields->mt.
287ee65d4b3SYunkang Tang  */
288ee65d4b3SYunkang Tang static void alps_process_bitmap_dolphin(struct alps_data *priv,
28902d04254SHans de Goede 					struct alps_fields *fields)
290ee65d4b3SYunkang Tang {
291ee65d4b3SYunkang Tang 	int box_middle_x, box_middle_y;
292ee65d4b3SYunkang Tang 	unsigned int x_map, y_map;
293ee65d4b3SYunkang Tang 	unsigned char start_bit, end_bit;
294ee65d4b3SYunkang Tang 	unsigned char x_msb, x_lsb, y_msb, y_lsb;
295ee65d4b3SYunkang Tang 
296ee65d4b3SYunkang Tang 	x_map = fields->x_map;
297ee65d4b3SYunkang Tang 	y_map = fields->y_map;
298ee65d4b3SYunkang Tang 
299ee65d4b3SYunkang Tang 	if (!x_map || !y_map)
300ee65d4b3SYunkang Tang 		return;
301ee65d4b3SYunkang Tang 
302ee65d4b3SYunkang Tang 	/* Get Most-significant and Least-significant bit */
303ee65d4b3SYunkang Tang 	x_msb = fls(x_map);
304ee65d4b3SYunkang Tang 	x_lsb = ffs(x_map);
305ee65d4b3SYunkang Tang 	y_msb = fls(y_map);
306ee65d4b3SYunkang Tang 	y_lsb = ffs(y_map);
307ee65d4b3SYunkang Tang 
308ee65d4b3SYunkang Tang 	/* Most-significant bit should never exceed max sensor line number */
309ee65d4b3SYunkang Tang 	if (x_msb > priv->x_bits || y_msb > priv->y_bits)
310ee65d4b3SYunkang Tang 		return;
311ee65d4b3SYunkang Tang 
312ee65d4b3SYunkang Tang 	if (fields->fingers > 1) {
313ee65d4b3SYunkang Tang 		start_bit = priv->x_bits - x_msb;
314ee65d4b3SYunkang Tang 		end_bit = priv->x_bits - x_lsb;
315ee65d4b3SYunkang Tang 		box_middle_x = (priv->x_max * (start_bit + end_bit)) /
316ee65d4b3SYunkang Tang 				(2 * (priv->x_bits - 1));
317ee65d4b3SYunkang Tang 
318ee65d4b3SYunkang Tang 		start_bit = y_lsb - 1;
319ee65d4b3SYunkang Tang 		end_bit = y_msb - 1;
320ee65d4b3SYunkang Tang 		box_middle_y = (priv->y_max * (start_bit + end_bit)) /
321ee65d4b3SYunkang Tang 				(2 * (priv->y_bits - 1));
32202d04254SHans de Goede 		fields->mt[0] = fields->st;
32302d04254SHans de Goede 		fields->mt[1].x = 2 * box_middle_x - fields->mt[0].x;
32402d04254SHans de Goede 		fields->mt[1].y = 2 * box_middle_y - fields->mt[0].y;
325ee65d4b3SYunkang Tang 	}
326ee65d4b3SYunkang Tang }
327ee65d4b3SYunkang Tang 
328036e6c7bSHans de Goede static void alps_get_bitmap_points(unsigned int map,
329036e6c7bSHans de Goede 				   struct alps_bitmap_point *low,
330036e6c7bSHans de Goede 				   struct alps_bitmap_point *high,
331036e6c7bSHans de Goede 				   int *fingers)
332036e6c7bSHans de Goede {
333036e6c7bSHans de Goede 	struct alps_bitmap_point *point;
334036e6c7bSHans de Goede 	int i, bit, prev_bit = 0;
335036e6c7bSHans de Goede 
336036e6c7bSHans de Goede 	point = low;
337036e6c7bSHans de Goede 	for (i = 0; map != 0; i++, map >>= 1) {
338036e6c7bSHans de Goede 		bit = map & 1;
339036e6c7bSHans de Goede 		if (bit) {
340036e6c7bSHans de Goede 			if (!prev_bit) {
341036e6c7bSHans de Goede 				point->start_bit = i;
342105affbfSHans de Goede 				point->num_bits = 0;
343036e6c7bSHans de Goede 				(*fingers)++;
344036e6c7bSHans de Goede 			}
345036e6c7bSHans de Goede 			point->num_bits++;
346036e6c7bSHans de Goede 		} else {
347036e6c7bSHans de Goede 			if (prev_bit)
348036e6c7bSHans de Goede 				point = high;
349036e6c7bSHans de Goede 		}
350036e6c7bSHans de Goede 		prev_bit = bit;
351036e6c7bSHans de Goede 	}
352036e6c7bSHans de Goede }
353036e6c7bSHans de Goede 
354ee65d4b3SYunkang Tang /*
35501ce661fSSeth Forshee  * Process bitmap data from v3 and v4 protocols. Returns the number of
35601ce661fSSeth Forshee  * fingers detected. A return value of 0 means at least one of the
35701ce661fSSeth Forshee  * bitmaps was empty.
35801ce661fSSeth Forshee  *
35901ce661fSSeth Forshee  * The bitmaps don't have enough data to track fingers, so this function
36001ce661fSSeth Forshee  * only generates points representing a bounding box of all contacts.
36102d04254SHans de Goede  * These points are returned in fields->mt when the return value
36201ce661fSSeth Forshee  * is greater than 0.
36301ce661fSSeth Forshee  */
3647a9f73e7SKevin Cernekee static int alps_process_bitmap(struct alps_data *priv,
36502d04254SHans de Goede 			       struct alps_fields *fields)
36601ce661fSSeth Forshee {
367036e6c7bSHans de Goede 	int i, fingers_x = 0, fingers_y = 0, fingers;
36801ce661fSSeth Forshee 	struct alps_bitmap_point x_low = {0,}, x_high = {0,};
36901ce661fSSeth Forshee 	struct alps_bitmap_point y_low = {0,}, y_high = {0,};
37001ce661fSSeth Forshee 
37102d04254SHans de Goede 	if (!fields->x_map || !fields->y_map)
37201ce661fSSeth Forshee 		return 0;
37301ce661fSSeth Forshee 
37402d04254SHans de Goede 	alps_get_bitmap_points(fields->x_map, &x_low, &x_high, &fingers_x);
37502d04254SHans de Goede 	alps_get_bitmap_points(fields->y_map, &y_low, &y_high, &fingers_y);
37601ce661fSSeth Forshee 
37701ce661fSSeth Forshee 	/*
37801ce661fSSeth Forshee 	 * Fingers can overlap, so we use the maximum count of fingers
37901ce661fSSeth Forshee 	 * on either axis as the finger count.
38001ce661fSSeth Forshee 	 */
38101ce661fSSeth Forshee 	fingers = max(fingers_x, fingers_y);
38201ce661fSSeth Forshee 
38301ce661fSSeth Forshee 	/*
38420bea68bSHans de Goede 	 * If an axis reports only a single contact, we have overlapping or
38520bea68bSHans de Goede 	 * adjacent fingers. Divide the single contact between the two points.
38601ce661fSSeth Forshee 	 */
38701ce661fSSeth Forshee 	if (fingers_x == 1) {
38828835f45SHans de Goede 		i = (x_low.num_bits - 1) / 2;
38901ce661fSSeth Forshee 		x_low.num_bits = x_low.num_bits - i;
39001ce661fSSeth Forshee 		x_high.start_bit = x_low.start_bit + i;
39101ce661fSSeth Forshee 		x_high.num_bits = max(i, 1);
39220bea68bSHans de Goede 	}
39320bea68bSHans de Goede 	if (fingers_y == 1) {
39428835f45SHans de Goede 		i = (y_low.num_bits - 1) / 2;
39501ce661fSSeth Forshee 		y_low.num_bits = y_low.num_bits - i;
39601ce661fSSeth Forshee 		y_high.start_bit = y_low.start_bit + i;
39701ce661fSSeth Forshee 		y_high.num_bits = max(i, 1);
39801ce661fSSeth Forshee 	}
39901ce661fSSeth Forshee 
40002d04254SHans de Goede 	fields->mt[0].x =
40102d04254SHans de Goede 		(priv->x_max * (2 * x_low.start_bit + x_low.num_bits - 1)) /
4027a9f73e7SKevin Cernekee 		(2 * (priv->x_bits - 1));
40302d04254SHans de Goede 	fields->mt[0].y =
40402d04254SHans de Goede 		(priv->y_max * (2 * y_low.start_bit + y_low.num_bits - 1)) /
4057a9f73e7SKevin Cernekee 		(2 * (priv->y_bits - 1));
40601ce661fSSeth Forshee 
40702d04254SHans de Goede 	fields->mt[1].x =
40802d04254SHans de Goede 		(priv->x_max * (2 * x_high.start_bit + x_high.num_bits - 1)) /
4097a9f73e7SKevin Cernekee 		(2 * (priv->x_bits - 1));
41002d04254SHans de Goede 	fields->mt[1].y =
41102d04254SHans de Goede 		(priv->y_max * (2 * y_high.start_bit + y_high.num_bits - 1)) /
4127a9f73e7SKevin Cernekee 		(2 * (priv->y_bits - 1));
41301ce661fSSeth Forshee 
41440e8f53bSHans de Goede 	/* y-bitmap order is reversed, except on rushmore */
41540e8f53bSHans de Goede 	if (!(priv->flags & ALPS_IS_RUSHMORE)) {
41602d04254SHans de Goede 		fields->mt[0].y = priv->y_max - fields->mt[0].y;
41702d04254SHans de Goede 		fields->mt[1].y = priv->y_max - fields->mt[1].y;
41840e8f53bSHans de Goede 	}
41940e8f53bSHans de Goede 
42001ce661fSSeth Forshee 	return fingers;
42101ce661fSSeth Forshee }
42201ce661fSSeth Forshee 
423cdf333efSHans de Goede static void alps_set_slot(struct input_dev *dev, int slot, int x, int y)
42401ce661fSSeth Forshee {
42501ce661fSSeth Forshee 	input_mt_slot(dev, slot);
426cdf333efSHans de Goede 	input_mt_report_slot_state(dev, MT_TOOL_FINGER, true);
42701ce661fSSeth Forshee 	input_report_abs(dev, ABS_MT_POSITION_X, x);
42801ce661fSSeth Forshee 	input_report_abs(dev, ABS_MT_POSITION_Y, y);
42901ce661fSSeth Forshee }
43001ce661fSSeth Forshee 
431cdf333efSHans de Goede static void alps_report_mt_data(struct psmouse *psmouse, int n)
43201ce661fSSeth Forshee {
43302d04254SHans de Goede 	struct alps_data *priv = psmouse->private;
43402d04254SHans de Goede 	struct input_dev *dev = psmouse->dev;
43502d04254SHans de Goede 	struct alps_fields *f = &priv->f;
436cdf333efSHans de Goede 	int i, slot[MAX_TOUCHES];
43702d04254SHans de Goede 
438cdf333efSHans de Goede 	input_mt_assign_slots(dev, slot, f->mt, n);
439cdf333efSHans de Goede 	for (i = 0; i < n; i++)
440cdf333efSHans de Goede 		alps_set_slot(dev, slot[i], f->mt[i].x, f->mt[i].y);
441cdf333efSHans de Goede 
442cdf333efSHans de Goede 	input_mt_sync_frame(dev);
44301ce661fSSeth Forshee }
44401ce661fSSeth Forshee 
44568c21870SHans de Goede static void alps_report_semi_mt_data(struct psmouse *psmouse, int fingers)
44668c21870SHans de Goede {
44768c21870SHans de Goede 	struct alps_data *priv = psmouse->private;
44868c21870SHans de Goede 	struct input_dev *dev = psmouse->dev;
44968c21870SHans de Goede 	struct alps_fields *f = &priv->f;
45068c21870SHans de Goede 
45168c21870SHans de Goede 	/* Use st data when we don't have mt data */
45268c21870SHans de Goede 	if (fingers < 2) {
45368c21870SHans de Goede 		f->mt[0].x = f->st.x;
45468c21870SHans de Goede 		f->mt[0].y = f->st.y;
45568c21870SHans de Goede 		fingers = f->pressure > 0 ? 1 : 0;
45668c21870SHans de Goede 	}
45768c21870SHans de Goede 
45899d9996cSHans de Goede 	alps_report_mt_data(psmouse, (fingers <= 2) ? fingers : 2);
45968c21870SHans de Goede 
46068c21870SHans de Goede 	input_mt_report_finger_count(dev, fingers);
46168c21870SHans de Goede 
46268c21870SHans de Goede 	input_report_key(dev, BTN_LEFT, f->left);
46368c21870SHans de Goede 	input_report_key(dev, BTN_RIGHT, f->right);
46468c21870SHans de Goede 	input_report_key(dev, BTN_MIDDLE, f->middle);
46568c21870SHans de Goede 
46668c21870SHans de Goede 	input_report_abs(dev, ABS_PRESSURE, f->pressure);
46768c21870SHans de Goede 
46868c21870SHans de Goede 	input_sync(dev);
46968c21870SHans de Goede }
47068c21870SHans de Goede 
47125bded7cSSeth Forshee static void alps_process_trackstick_packet_v3(struct psmouse *psmouse)
47225bded7cSSeth Forshee {
47325bded7cSSeth Forshee 	struct alps_data *priv = psmouse->private;
47425bded7cSSeth Forshee 	unsigned char *packet = psmouse->packet;
47525bded7cSSeth Forshee 	struct input_dev *dev = priv->dev2;
47625bded7cSSeth Forshee 	int x, y, z, left, right, middle;
47725bded7cSSeth Forshee 
47825bded7cSSeth Forshee 	/* Sanity check packet */
47925bded7cSSeth Forshee 	if (!(packet[0] & 0x40)) {
48025bded7cSSeth Forshee 		psmouse_dbg(psmouse, "Bad trackstick packet, discarding\n");
48125bded7cSSeth Forshee 		return;
48225bded7cSSeth Forshee 	}
48325bded7cSSeth Forshee 
48425bded7cSSeth Forshee 	/*
48525bded7cSSeth Forshee 	 * There's a special packet that seems to indicate the end
48625bded7cSSeth Forshee 	 * of a stream of trackstick data. Filter these out.
48725bded7cSSeth Forshee 	 */
48825bded7cSSeth Forshee 	if (packet[1] == 0x7f && packet[2] == 0x7f && packet[4] == 0x7f)
48925bded7cSSeth Forshee 		return;
49025bded7cSSeth Forshee 
49125bded7cSSeth Forshee 	x = (s8)(((packet[0] & 0x20) << 2) | (packet[1] & 0x7f));
49225bded7cSSeth Forshee 	y = (s8)(((packet[0] & 0x10) << 3) | (packet[2] & 0x7f));
49325bded7cSSeth Forshee 	z = (packet[4] & 0x7c) >> 2;
49425bded7cSSeth Forshee 
49525bded7cSSeth Forshee 	/*
49625bded7cSSeth Forshee 	 * The x and y values tend to be quite large, and when used
49725bded7cSSeth Forshee 	 * alone the trackstick is difficult to use. Scale them down
49825bded7cSSeth Forshee 	 * to compensate.
49925bded7cSSeth Forshee 	 */
50025bded7cSSeth Forshee 	x /= 8;
50125bded7cSSeth Forshee 	y /= 8;
50225bded7cSSeth Forshee 
50325bded7cSSeth Forshee 	input_report_rel(dev, REL_X, x);
50425bded7cSSeth Forshee 	input_report_rel(dev, REL_Y, -y);
50525bded7cSSeth Forshee 
50625bded7cSSeth Forshee 	/*
50725bded7cSSeth Forshee 	 * Most ALPS models report the trackstick buttons in the touchpad
50825bded7cSSeth Forshee 	 * packets, but a few report them here. No reliable way has been
50925bded7cSSeth Forshee 	 * found to differentiate between the models upfront, so we enable
51025bded7cSSeth Forshee 	 * the quirk in response to seeing a button press in the trackstick
51125bded7cSSeth Forshee 	 * packet.
51225bded7cSSeth Forshee 	 */
51325bded7cSSeth Forshee 	left = packet[3] & 0x01;
51425bded7cSSeth Forshee 	right = packet[3] & 0x02;
51525bded7cSSeth Forshee 	middle = packet[3] & 0x04;
51625bded7cSSeth Forshee 
51725bded7cSSeth Forshee 	if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) &&
51825bded7cSSeth Forshee 	    (left || right || middle))
51925bded7cSSeth Forshee 		priv->quirks |= ALPS_QUIRK_TRACKSTICK_BUTTONS;
52025bded7cSSeth Forshee 
52125bded7cSSeth Forshee 	if (priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) {
52225bded7cSSeth Forshee 		input_report_key(dev, BTN_LEFT, left);
52325bded7cSSeth Forshee 		input_report_key(dev, BTN_RIGHT, right);
52425bded7cSSeth Forshee 		input_report_key(dev, BTN_MIDDLE, middle);
52525bded7cSSeth Forshee 	}
52625bded7cSSeth Forshee 
52725bded7cSSeth Forshee 	input_sync(dev);
52825bded7cSSeth Forshee 	return;
52925bded7cSSeth Forshee }
53025bded7cSSeth Forshee 
531f85e5001SKevin Cernekee static void alps_decode_buttons_v3(struct alps_fields *f, unsigned char *p)
532f85e5001SKevin Cernekee {
533f85e5001SKevin Cernekee 	f->left = !!(p[3] & 0x01);
534f85e5001SKevin Cernekee 	f->right = !!(p[3] & 0x02);
535f85e5001SKevin Cernekee 	f->middle = !!(p[3] & 0x04);
536f85e5001SKevin Cernekee 
537f85e5001SKevin Cernekee 	f->ts_left = !!(p[3] & 0x10);
538f85e5001SKevin Cernekee 	f->ts_right = !!(p[3] & 0x20);
539f85e5001SKevin Cernekee 	f->ts_middle = !!(p[3] & 0x40);
540f85e5001SKevin Cernekee }
541f85e5001SKevin Cernekee 
54238c11eaaSHans de Goede static int alps_decode_pinnacle(struct alps_fields *f, unsigned char *p,
543ee65d4b3SYunkang Tang 				 struct psmouse *psmouse)
544f85e5001SKevin Cernekee {
545f85e5001SKevin Cernekee 	f->first_mp = !!(p[4] & 0x40);
546f85e5001SKevin Cernekee 	f->is_mp = !!(p[0] & 0x40);
547f85e5001SKevin Cernekee 
548f85e5001SKevin Cernekee 	f->fingers = (p[5] & 0x3) + 1;
549f85e5001SKevin Cernekee 	f->x_map = ((p[4] & 0x7e) << 8) |
550f85e5001SKevin Cernekee 		   ((p[1] & 0x7f) << 2) |
551f85e5001SKevin Cernekee 		   ((p[0] & 0x30) >> 4);
552f85e5001SKevin Cernekee 	f->y_map = ((p[3] & 0x70) << 4) |
553f85e5001SKevin Cernekee 		   ((p[2] & 0x7f) << 1) |
554f85e5001SKevin Cernekee 		   (p[4] & 0x01);
555f85e5001SKevin Cernekee 
55602d04254SHans de Goede 	f->st.x = ((p[1] & 0x7f) << 4) | ((p[4] & 0x30) >> 2) |
557f85e5001SKevin Cernekee 	       ((p[0] & 0x30) >> 4);
55802d04254SHans de Goede 	f->st.y = ((p[2] & 0x7f) << 4) | (p[4] & 0x0f);
55902d04254SHans de Goede 	f->pressure = p[5] & 0x7f;
560f85e5001SKevin Cernekee 
561f85e5001SKevin Cernekee 	alps_decode_buttons_v3(f, p);
56238c11eaaSHans de Goede 
56338c11eaaSHans de Goede 	return 0;
564f85e5001SKevin Cernekee }
565f85e5001SKevin Cernekee 
56638c11eaaSHans de Goede static int alps_decode_rushmore(struct alps_fields *f, unsigned char *p,
567ee65d4b3SYunkang Tang 				 struct psmouse *psmouse)
5681302bac3SKevin Cernekee {
569ee65d4b3SYunkang Tang 	alps_decode_pinnacle(f, p, psmouse);
5701302bac3SKevin Cernekee 
571f105e34aSYunkang Tang 	/* Rushmore's packet decode has a bit difference with Pinnacle's */
572f105e34aSYunkang Tang 	f->is_mp = !!(p[5] & 0x40);
573f105e34aSYunkang Tang 	f->fingers = max((p[5] & 0x3), ((p[5] >> 2) & 0x3)) + 1;
5741302bac3SKevin Cernekee 	f->x_map |= (p[5] & 0x10) << 11;
5751302bac3SKevin Cernekee 	f->y_map |= (p[5] & 0x20) << 6;
57638c11eaaSHans de Goede 
57738c11eaaSHans de Goede 	return 0;
5781302bac3SKevin Cernekee }
5791302bac3SKevin Cernekee 
58038c11eaaSHans de Goede static int alps_decode_dolphin(struct alps_fields *f, unsigned char *p,
581ee65d4b3SYunkang Tang 				struct psmouse *psmouse)
58275af9e56SDave Turvene {
583ee65d4b3SYunkang Tang 	u64 palm_data = 0;
584ee65d4b3SYunkang Tang 	struct alps_data *priv = psmouse->private;
585ee65d4b3SYunkang Tang 
58675af9e56SDave Turvene 	f->first_mp = !!(p[0] & 0x02);
58775af9e56SDave Turvene 	f->is_mp = !!(p[0] & 0x20);
58875af9e56SDave Turvene 
589ee65d4b3SYunkang Tang 	if (!f->is_mp) {
59002d04254SHans de Goede 		f->st.x = ((p[1] & 0x7f) | ((p[4] & 0x0f) << 7));
59102d04254SHans de Goede 		f->st.y = ((p[2] & 0x7f) | ((p[4] & 0xf0) << 3));
59202d04254SHans de Goede 		f->pressure = (p[0] & 4) ? 0 : p[5] & 0x7f;
59375af9e56SDave Turvene 		alps_decode_buttons_v3(f, p);
594ee65d4b3SYunkang Tang 	} else {
595ee65d4b3SYunkang Tang 		f->fingers = ((p[0] & 0x6) >> 1 |
596ee65d4b3SYunkang Tang 		     (p[0] & 0x10) >> 2);
597ee65d4b3SYunkang Tang 
598ee65d4b3SYunkang Tang 		palm_data = (p[1] & 0x7f) |
599ee65d4b3SYunkang Tang 			    ((p[2] & 0x7f) << 7) |
600ee65d4b3SYunkang Tang 			    ((p[4] & 0x7f) << 14) |
601ee65d4b3SYunkang Tang 			    ((p[5] & 0x7f) << 21) |
602ee65d4b3SYunkang Tang 			    ((p[3] & 0x07) << 28) |
603ee65d4b3SYunkang Tang 			    (((u64)p[3] & 0x70) << 27) |
604ee65d4b3SYunkang Tang 			    (((u64)p[0] & 0x01) << 34);
605ee65d4b3SYunkang Tang 
606ee65d4b3SYunkang Tang 		/* Y-profile is stored in P(0) to p(n-1), n = y_bits; */
607ee65d4b3SYunkang Tang 		f->y_map = palm_data & (BIT(priv->y_bits) - 1);
608ee65d4b3SYunkang Tang 
609ee65d4b3SYunkang Tang 		/* X-profile is stored in p(n) to p(n+m-1), m = x_bits; */
610ee65d4b3SYunkang Tang 		f->x_map = (palm_data >> priv->y_bits) &
611ee65d4b3SYunkang Tang 			   (BIT(priv->x_bits) - 1);
612ee65d4b3SYunkang Tang 	}
61338c11eaaSHans de Goede 
61438c11eaaSHans de Goede 	return 0;
61575af9e56SDave Turvene }
61675af9e56SDave Turvene 
617ee65d4b3SYunkang Tang static void alps_process_touchpad_packet_v3_v5(struct psmouse *psmouse)
61825bded7cSSeth Forshee {
61925bded7cSSeth Forshee 	struct alps_data *priv = psmouse->private;
62025bded7cSSeth Forshee 	unsigned char *packet = psmouse->packet;
62125bded7cSSeth Forshee 	struct input_dev *dev2 = priv->dev2;
62202d04254SHans de Goede 	struct alps_fields *f = &priv->f;
62302d04254SHans de Goede 	int fingers = 0;
624f85e5001SKevin Cernekee 
62502d04254SHans de Goede 	memset(f, 0, sizeof(*f));
62602d04254SHans de Goede 
62702d04254SHans de Goede 	priv->decode_fields(f, packet, psmouse);
62825bded7cSSeth Forshee 
62925bded7cSSeth Forshee 	/*
63001ce661fSSeth Forshee 	 * There's no single feature of touchpad position and bitmap packets
63101ce661fSSeth Forshee 	 * that can be used to distinguish between them. We rely on the fact
63201ce661fSSeth Forshee 	 * that a bitmap packet should always follow a position packet with
63301ce661fSSeth Forshee 	 * bit 6 of packet[4] set.
63425bded7cSSeth Forshee 	 */
63525bded7cSSeth Forshee 	if (priv->multi_packet) {
63625bded7cSSeth Forshee 		/*
63725bded7cSSeth Forshee 		 * Sometimes a position packet will indicate a multi-packet
63825bded7cSSeth Forshee 		 * sequence, but then what follows is another position
63925bded7cSSeth Forshee 		 * packet. Check for this, and when it happens process the
64025bded7cSSeth Forshee 		 * position packet as usual.
64125bded7cSSeth Forshee 		 */
64202d04254SHans de Goede 		if (f->is_mp) {
64302d04254SHans de Goede 			fingers = f->fingers;
644ee65d4b3SYunkang Tang 			if (priv->proto_version == ALPS_PROTO_V3) {
64502d04254SHans de Goede 				if (alps_process_bitmap(priv, f) == 0)
64620bea68bSHans de Goede 					fingers = 0; /* Use st data */
64701ce661fSSeth Forshee 
64801ce661fSSeth Forshee 				/* Now process position packet */
64902d04254SHans de Goede 				priv->decode_fields(f, priv->multi_data,
650ee65d4b3SYunkang Tang 						    psmouse);
651ee65d4b3SYunkang Tang 			} else {
652ee65d4b3SYunkang Tang 				/*
653ee65d4b3SYunkang Tang 				 * Because Dolphin uses position packet's
654ee65d4b3SYunkang Tang 				 * coordinate data as Pt1 and uses it to
655ee65d4b3SYunkang Tang 				 * calculate Pt2, so we need to do position
656ee65d4b3SYunkang Tang 				 * packet decode first.
657ee65d4b3SYunkang Tang 				 */
65802d04254SHans de Goede 				priv->decode_fields(f, priv->multi_data,
659ee65d4b3SYunkang Tang 						    psmouse);
660ee65d4b3SYunkang Tang 
661ee65d4b3SYunkang Tang 				/*
662ee65d4b3SYunkang Tang 				 * Since Dolphin's finger number is reliable,
663ee65d4b3SYunkang Tang 				 * there is no need to compare with bmap_fn.
664ee65d4b3SYunkang Tang 				 */
66502d04254SHans de Goede 				alps_process_bitmap_dolphin(priv, f);
666ee65d4b3SYunkang Tang 			}
66701ce661fSSeth Forshee 		} else {
66801ce661fSSeth Forshee 			priv->multi_packet = 0;
66925bded7cSSeth Forshee 		}
67025bded7cSSeth Forshee 	}
67125bded7cSSeth Forshee 
67201ce661fSSeth Forshee 	/*
67301ce661fSSeth Forshee 	 * Bit 6 of byte 0 is not usually set in position packets. The only
67401ce661fSSeth Forshee 	 * times it seems to be set is in situations where the data is
67501ce661fSSeth Forshee 	 * suspect anyway, e.g. a palm resting flat on the touchpad. Given
67601ce661fSSeth Forshee 	 * this combined with the fact that this bit is useful for filtering
67701ce661fSSeth Forshee 	 * out misidentified bitmap packets, we reject anything with this
67801ce661fSSeth Forshee 	 * bit set.
67901ce661fSSeth Forshee 	 */
68002d04254SHans de Goede 	if (f->is_mp)
68101ce661fSSeth Forshee 		return;
68201ce661fSSeth Forshee 
68302d04254SHans de Goede 	if (!priv->multi_packet && f->first_mp) {
68425bded7cSSeth Forshee 		priv->multi_packet = 1;
68501ce661fSSeth Forshee 		memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
68601ce661fSSeth Forshee 		return;
68701ce661fSSeth Forshee 	}
68801ce661fSSeth Forshee 
68925bded7cSSeth Forshee 	priv->multi_packet = 0;
69025bded7cSSeth Forshee 
69125bded7cSSeth Forshee 	/*
69225bded7cSSeth Forshee 	 * Sometimes the hardware sends a single packet with z = 0
69325bded7cSSeth Forshee 	 * in the middle of a stream. Real releases generate packets
69425bded7cSSeth Forshee 	 * with x, y, and z all zero, so these seem to be flukes.
69525bded7cSSeth Forshee 	 * Ignore them.
69625bded7cSSeth Forshee 	 */
69702d04254SHans de Goede 	if (f->st.x && f->st.y && !f->pressure)
69825bded7cSSeth Forshee 		return;
69925bded7cSSeth Forshee 
70068c21870SHans de Goede 	alps_report_semi_mt_data(psmouse, fingers);
70125bded7cSSeth Forshee 
70225bded7cSSeth Forshee 	if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS)) {
70302d04254SHans de Goede 		input_report_key(dev2, BTN_LEFT, f->ts_left);
70402d04254SHans de Goede 		input_report_key(dev2, BTN_RIGHT, f->ts_right);
70502d04254SHans de Goede 		input_report_key(dev2, BTN_MIDDLE, f->ts_middle);
70625bded7cSSeth Forshee 		input_sync(dev2);
70725bded7cSSeth Forshee 	}
70825bded7cSSeth Forshee }
70925bded7cSSeth Forshee 
71025bded7cSSeth Forshee static void alps_process_packet_v3(struct psmouse *psmouse)
71125bded7cSSeth Forshee {
71225bded7cSSeth Forshee 	unsigned char *packet = psmouse->packet;
71325bded7cSSeth Forshee 
71425bded7cSSeth Forshee 	/*
71525bded7cSSeth Forshee 	 * v3 protocol packets come in three types, two representing
71625bded7cSSeth Forshee 	 * touchpad data and one representing trackstick data.
71725bded7cSSeth Forshee 	 * Trackstick packets seem to be distinguished by always
71825bded7cSSeth Forshee 	 * having 0x3f in the last byte. This value has never been
71925bded7cSSeth Forshee 	 * observed in the last byte of either of the other types
72025bded7cSSeth Forshee 	 * of packets.
72125bded7cSSeth Forshee 	 */
72225bded7cSSeth Forshee 	if (packet[5] == 0x3f) {
72325bded7cSSeth Forshee 		alps_process_trackstick_packet_v3(psmouse);
72425bded7cSSeth Forshee 		return;
72525bded7cSSeth Forshee 	}
72625bded7cSSeth Forshee 
727ee65d4b3SYunkang Tang 	alps_process_touchpad_packet_v3_v5(psmouse);
72825bded7cSSeth Forshee }
72925bded7cSSeth Forshee 
73095f75e91SYunkang Tang static void alps_process_packet_v6(struct psmouse *psmouse)
73195f75e91SYunkang Tang {
73295f75e91SYunkang Tang 	struct alps_data *priv = psmouse->private;
73395f75e91SYunkang Tang 	unsigned char *packet = psmouse->packet;
73495f75e91SYunkang Tang 	struct input_dev *dev = psmouse->dev;
73595f75e91SYunkang Tang 	struct input_dev *dev2 = priv->dev2;
73695f75e91SYunkang Tang 	int x, y, z, left, right, middle;
73795f75e91SYunkang Tang 
73895f75e91SYunkang Tang 	/*
73995f75e91SYunkang Tang 	 * We can use Byte5 to distinguish if the packet is from Touchpad
74095f75e91SYunkang Tang 	 * or Trackpoint.
74195f75e91SYunkang Tang 	 * Touchpad:	0 - 0x7E
74295f75e91SYunkang Tang 	 * Trackpoint:	0x7F
74395f75e91SYunkang Tang 	 */
74495f75e91SYunkang Tang 	if (packet[5] == 0x7F) {
74595f75e91SYunkang Tang 		/* It should be a DualPoint when received Trackpoint packet */
74695f75e91SYunkang Tang 		if (!(priv->flags & ALPS_DUALPOINT))
74795f75e91SYunkang Tang 			return;
74895f75e91SYunkang Tang 
74995f75e91SYunkang Tang 		/* Trackpoint packet */
75095f75e91SYunkang Tang 		x = packet[1] | ((packet[3] & 0x20) << 2);
75195f75e91SYunkang Tang 		y = packet[2] | ((packet[3] & 0x40) << 1);
75295f75e91SYunkang Tang 		z = packet[4];
75395f75e91SYunkang Tang 		left = packet[3] & 0x01;
75495f75e91SYunkang Tang 		right = packet[3] & 0x02;
75595f75e91SYunkang Tang 		middle = packet[3] & 0x04;
75695f75e91SYunkang Tang 
75795f75e91SYunkang Tang 		/* To prevent the cursor jump when finger lifted */
75895f75e91SYunkang Tang 		if (x == 0x7F && y == 0x7F && z == 0x7F)
75995f75e91SYunkang Tang 			x = y = z = 0;
76095f75e91SYunkang Tang 
76195f75e91SYunkang Tang 		/* Divide 4 since trackpoint's speed is too fast */
76295f75e91SYunkang Tang 		input_report_rel(dev2, REL_X, (char)x / 4);
76395f75e91SYunkang Tang 		input_report_rel(dev2, REL_Y, -((char)y / 4));
76495f75e91SYunkang Tang 
76595f75e91SYunkang Tang 		input_report_key(dev2, BTN_LEFT, left);
76695f75e91SYunkang Tang 		input_report_key(dev2, BTN_RIGHT, right);
76795f75e91SYunkang Tang 		input_report_key(dev2, BTN_MIDDLE, middle);
76895f75e91SYunkang Tang 
76995f75e91SYunkang Tang 		input_sync(dev2);
77095f75e91SYunkang Tang 		return;
77195f75e91SYunkang Tang 	}
77295f75e91SYunkang Tang 
77395f75e91SYunkang Tang 	/* Touchpad packet */
77495f75e91SYunkang Tang 	x = packet[1] | ((packet[3] & 0x78) << 4);
77595f75e91SYunkang Tang 	y = packet[2] | ((packet[4] & 0x78) << 4);
77695f75e91SYunkang Tang 	z = packet[5];
77795f75e91SYunkang Tang 	left = packet[3] & 0x01;
77895f75e91SYunkang Tang 	right = packet[3] & 0x02;
77995f75e91SYunkang Tang 
78095f75e91SYunkang Tang 	if (z > 30)
78195f75e91SYunkang Tang 		input_report_key(dev, BTN_TOUCH, 1);
78295f75e91SYunkang Tang 	if (z < 25)
78395f75e91SYunkang Tang 		input_report_key(dev, BTN_TOUCH, 0);
78495f75e91SYunkang Tang 
78595f75e91SYunkang Tang 	if (z > 0) {
78695f75e91SYunkang Tang 		input_report_abs(dev, ABS_X, x);
78795f75e91SYunkang Tang 		input_report_abs(dev, ABS_Y, y);
78895f75e91SYunkang Tang 	}
78995f75e91SYunkang Tang 
79095f75e91SYunkang Tang 	input_report_abs(dev, ABS_PRESSURE, z);
79195f75e91SYunkang Tang 	input_report_key(dev, BTN_TOOL_FINGER, z > 0);
79295f75e91SYunkang Tang 
79395f75e91SYunkang Tang 	/* v6 touchpad does not have middle button */
79495f75e91SYunkang Tang 	input_report_key(dev, BTN_LEFT, left);
79595f75e91SYunkang Tang 	input_report_key(dev, BTN_RIGHT, right);
79695f75e91SYunkang Tang 
79795f75e91SYunkang Tang 	input_sync(dev);
79895f75e91SYunkang Tang }
79995f75e91SYunkang Tang 
80025bded7cSSeth Forshee static void alps_process_packet_v4(struct psmouse *psmouse)
80125bded7cSSeth Forshee {
8023b7e09faSGeorge Pantalos 	struct alps_data *priv = psmouse->private;
80325bded7cSSeth Forshee 	unsigned char *packet = psmouse->packet;
80402d04254SHans de Goede 	struct alps_fields *f = &priv->f;
80568c21870SHans de Goede 	int offset;
8063b7e09faSGeorge Pantalos 
8073b7e09faSGeorge Pantalos 	/*
8083b7e09faSGeorge Pantalos 	 * v4 has a 6-byte encoding for bitmap data, but this data is
8093b7e09faSGeorge Pantalos 	 * broken up between 3 normal packets. Use priv->multi_packet to
8103b7e09faSGeorge Pantalos 	 * track our position in the bitmap packet.
8113b7e09faSGeorge Pantalos 	 */
8123b7e09faSGeorge Pantalos 	if (packet[6] & 0x40) {
8133b7e09faSGeorge Pantalos 		/* sync, reset position */
8143b7e09faSGeorge Pantalos 		priv->multi_packet = 0;
8153b7e09faSGeorge Pantalos 	}
8163b7e09faSGeorge Pantalos 
8173b7e09faSGeorge Pantalos 	if (WARN_ON_ONCE(priv->multi_packet > 2))
8183b7e09faSGeorge Pantalos 		return;
8193b7e09faSGeorge Pantalos 
8203b7e09faSGeorge Pantalos 	offset = 2 * priv->multi_packet;
8213b7e09faSGeorge Pantalos 	priv->multi_data[offset] = packet[6];
8223b7e09faSGeorge Pantalos 	priv->multi_data[offset + 1] = packet[7];
8233b7e09faSGeorge Pantalos 
8243b7e09faSGeorge Pantalos 	if (++priv->multi_packet > 2) {
8253b7e09faSGeorge Pantalos 		priv->multi_packet = 0;
8263b7e09faSGeorge Pantalos 
82702d04254SHans de Goede 		f->x_map = ((priv->multi_data[2] & 0x1f) << 10) |
8283b7e09faSGeorge Pantalos 			   ((priv->multi_data[3] & 0x60) << 3) |
8293b7e09faSGeorge Pantalos 			   ((priv->multi_data[0] & 0x3f) << 2) |
8303b7e09faSGeorge Pantalos 			   ((priv->multi_data[1] & 0x60) >> 5);
83102d04254SHans de Goede 		f->y_map = ((priv->multi_data[5] & 0x01) << 10) |
8323b7e09faSGeorge Pantalos 			   ((priv->multi_data[3] & 0x1f) << 5) |
8333b7e09faSGeorge Pantalos 			    (priv->multi_data[1] & 0x1f);
8343b7e09faSGeorge Pantalos 
83502d04254SHans de Goede 		f->fingers = alps_process_bitmap(priv, f);
8363b7e09faSGeorge Pantalos 	}
83725bded7cSSeth Forshee 
838b0cfb794SAndreas Bosch 	f->left = !!(packet[4] & 0x01);
839b0cfb794SAndreas Bosch 	f->right = !!(packet[4] & 0x02);
84025bded7cSSeth Forshee 
84102d04254SHans de Goede 	f->st.x = ((packet[1] & 0x7f) << 4) | ((packet[3] & 0x30) >> 2) |
84225bded7cSSeth Forshee 		  ((packet[0] & 0x30) >> 4);
84302d04254SHans de Goede 	f->st.y = ((packet[2] & 0x7f) << 4) | (packet[3] & 0x0f);
84402d04254SHans de Goede 	f->pressure = packet[5] & 0x7f;
84525bded7cSSeth Forshee 
84668c21870SHans de Goede 	alps_report_semi_mt_data(psmouse, f->fingers);
84725bded7cSSeth Forshee }
84825bded7cSSeth Forshee 
8493808843cSYunkang Tang static bool alps_is_valid_package_v7(struct psmouse *psmouse)
8503808843cSYunkang Tang {
8513808843cSYunkang Tang 	switch (psmouse->pktcnt) {
8523808843cSYunkang Tang 	case 3:
8533808843cSYunkang Tang 		return (psmouse->packet[2] & 0x40) == 0x40;
8543808843cSYunkang Tang 	case 4:
8553808843cSYunkang Tang 		return (psmouse->packet[3] & 0x48) == 0x48;
8563808843cSYunkang Tang 	case 6:
8573808843cSYunkang Tang 		return (psmouse->packet[5] & 0x40) == 0x00;
8583808843cSYunkang Tang 	}
8593808843cSYunkang Tang 	return true;
8603808843cSYunkang Tang }
8613808843cSYunkang Tang 
8623808843cSYunkang Tang static unsigned char alps_get_packet_id_v7(char *byte)
8633808843cSYunkang Tang {
8643808843cSYunkang Tang 	unsigned char packet_id;
8653808843cSYunkang Tang 
8663808843cSYunkang Tang 	if (byte[4] & 0x40)
8673808843cSYunkang Tang 		packet_id = V7_PACKET_ID_TWO;
8683808843cSYunkang Tang 	else if (byte[4] & 0x01)
8693808843cSYunkang Tang 		packet_id = V7_PACKET_ID_MULTI;
8703808843cSYunkang Tang 	else if ((byte[0] & 0x10) && !(byte[4] & 0x43))
8713808843cSYunkang Tang 		packet_id = V7_PACKET_ID_NEW;
8723808843cSYunkang Tang 	else if (byte[1] == 0x00 && byte[4] == 0x00)
8733808843cSYunkang Tang 		packet_id = V7_PACKET_ID_IDLE;
8743808843cSYunkang Tang 	else
8753808843cSYunkang Tang 		packet_id = V7_PACKET_ID_UNKNOWN;
8763808843cSYunkang Tang 
8773808843cSYunkang Tang 	return packet_id;
8783808843cSYunkang Tang }
8793808843cSYunkang Tang 
8803808843cSYunkang Tang static void alps_get_finger_coordinate_v7(struct input_mt_pos *mt,
8813808843cSYunkang Tang 					  unsigned char *pkt,
8823808843cSYunkang Tang 					  unsigned char pkt_id)
8833808843cSYunkang Tang {
8843808843cSYunkang Tang 	mt[0].x = ((pkt[2] & 0x80) << 4);
8853808843cSYunkang Tang 	mt[0].x |= ((pkt[2] & 0x3F) << 5);
8863808843cSYunkang Tang 	mt[0].x |= ((pkt[3] & 0x30) >> 1);
8873808843cSYunkang Tang 	mt[0].x |= (pkt[3] & 0x07);
8883808843cSYunkang Tang 	mt[0].y = (pkt[1] << 3) | (pkt[0] & 0x07);
8893808843cSYunkang Tang 
8903808843cSYunkang Tang 	mt[1].x = ((pkt[3] & 0x80) << 4);
8913808843cSYunkang Tang 	mt[1].x |= ((pkt[4] & 0x80) << 3);
8923808843cSYunkang Tang 	mt[1].x |= ((pkt[4] & 0x3F) << 4);
8933808843cSYunkang Tang 	mt[1].y = ((pkt[5] & 0x80) << 3);
8943808843cSYunkang Tang 	mt[1].y |= ((pkt[5] & 0x3F) << 4);
8953808843cSYunkang Tang 
8963808843cSYunkang Tang 	switch (pkt_id) {
8973808843cSYunkang Tang 	case V7_PACKET_ID_TWO:
8983808843cSYunkang Tang 		mt[1].x &= ~0x000F;
8993808843cSYunkang Tang 		mt[1].y |= 0x000F;
9003808843cSYunkang Tang 		break;
9013808843cSYunkang Tang 
9023808843cSYunkang Tang 	case V7_PACKET_ID_MULTI:
9033808843cSYunkang Tang 		mt[1].x &= ~0x003F;
9043808843cSYunkang Tang 		mt[1].y &= ~0x0020;
9053808843cSYunkang Tang 		mt[1].y |= ((pkt[4] & 0x02) << 4);
9063808843cSYunkang Tang 		mt[1].y |= 0x001F;
9073808843cSYunkang Tang 		break;
9083808843cSYunkang Tang 
9093808843cSYunkang Tang 	case V7_PACKET_ID_NEW:
9103808843cSYunkang Tang 		mt[1].x &= ~0x003F;
9113808843cSYunkang Tang 		mt[1].x |= (pkt[0] & 0x20);
9123808843cSYunkang Tang 		mt[1].y |= 0x000F;
9133808843cSYunkang Tang 		break;
9143808843cSYunkang Tang 	}
9153808843cSYunkang Tang 
9163808843cSYunkang Tang 	mt[0].y = 0x7FF - mt[0].y;
9173808843cSYunkang Tang 	mt[1].y = 0x7FF - mt[1].y;
9183808843cSYunkang Tang }
9193808843cSYunkang Tang 
9203808843cSYunkang Tang static int alps_get_mt_count(struct input_mt_pos *mt)
9213808843cSYunkang Tang {
9223808843cSYunkang Tang 	int i;
9233808843cSYunkang Tang 
9243808843cSYunkang Tang 	for (i = 0; i < MAX_TOUCHES && mt[i].x != 0 && mt[i].y != 0; i++)
9253808843cSYunkang Tang 		/* empty */;
9263808843cSYunkang Tang 
9273808843cSYunkang Tang 	return i;
9283808843cSYunkang Tang }
9293808843cSYunkang Tang 
9303808843cSYunkang Tang static int alps_decode_packet_v7(struct alps_fields *f,
9313808843cSYunkang Tang 				  unsigned char *p,
9323808843cSYunkang Tang 				  struct psmouse *psmouse)
9333808843cSYunkang Tang {
9343808843cSYunkang Tang 	unsigned char pkt_id;
9353808843cSYunkang Tang 
9363808843cSYunkang Tang 	pkt_id = alps_get_packet_id_v7(p);
9373808843cSYunkang Tang 	if (pkt_id == V7_PACKET_ID_IDLE)
9383808843cSYunkang Tang 		return 0;
9393808843cSYunkang Tang 	if (pkt_id == V7_PACKET_ID_UNKNOWN)
9403808843cSYunkang Tang 		return -1;
9413808843cSYunkang Tang 
9423808843cSYunkang Tang 	alps_get_finger_coordinate_v7(f->mt, p, pkt_id);
9433808843cSYunkang Tang 
9443808843cSYunkang Tang 	if (pkt_id == V7_PACKET_ID_TWO || pkt_id == V7_PACKET_ID_MULTI) {
9453808843cSYunkang Tang 		f->left = (p[0] & 0x80) >> 7;
9463808843cSYunkang Tang 		f->right = (p[0] & 0x20) >> 5;
9473808843cSYunkang Tang 		f->middle = (p[0] & 0x10) >> 4;
9483808843cSYunkang Tang 	}
9493808843cSYunkang Tang 
9503808843cSYunkang Tang 	if (pkt_id == V7_PACKET_ID_TWO)
9513808843cSYunkang Tang 		f->fingers = alps_get_mt_count(f->mt);
9523808843cSYunkang Tang 	else if (pkt_id == V7_PACKET_ID_MULTI)
9533808843cSYunkang Tang 		f->fingers = 3 + (p[5] & 0x03);
9543808843cSYunkang Tang 
9553808843cSYunkang Tang 	return 0;
9563808843cSYunkang Tang }
9573808843cSYunkang Tang 
9583808843cSYunkang Tang static void alps_process_trackstick_packet_v7(struct psmouse *psmouse)
9593808843cSYunkang Tang {
9603808843cSYunkang Tang 	struct alps_data *priv = psmouse->private;
9613808843cSYunkang Tang 	unsigned char *packet = psmouse->packet;
9623808843cSYunkang Tang 	struct input_dev *dev2 = priv->dev2;
9633808843cSYunkang Tang 	int x, y, z, left, right, middle;
9643808843cSYunkang Tang 
9653808843cSYunkang Tang 	/*
9663808843cSYunkang Tang 	 *        b7 b6 b5 b4 b3 b2 b1 b0
9673808843cSYunkang Tang 	 * Byte0   0  1  0  0  1  0  0  0
9683808843cSYunkang Tang 	 * Byte1   1  1  *  *  1  M  R  L
9693808843cSYunkang Tang 	 * Byte2  X7  1 X5 X4 X3 X2 X1 X0
9703808843cSYunkang Tang 	 * Byte3  Z6  1 Y6 X6  1 Y2 Y1 Y0
9713808843cSYunkang Tang 	 * Byte4  Y7  0 Y5 Y4 Y3  1  1  0
9723808843cSYunkang Tang 	 * Byte5 T&P  0 Z5 Z4 Z3 Z2 Z1 Z0
9733808843cSYunkang Tang 	 * M / R / L: Middle / Right / Left button
9743808843cSYunkang Tang 	 */
9753808843cSYunkang Tang 
9763808843cSYunkang Tang 	x = ((packet[2] & 0xbf)) | ((packet[3] & 0x10) << 2);
9773808843cSYunkang Tang 	y = (packet[3] & 0x07) | (packet[4] & 0xb8) |
9783808843cSYunkang Tang 	    ((packet[3] & 0x20) << 1);
9793808843cSYunkang Tang 	z = (packet[5] & 0x3f) | ((packet[3] & 0x80) >> 1);
9803808843cSYunkang Tang 
9813808843cSYunkang Tang 	left = (packet[1] & 0x01);
9823808843cSYunkang Tang 	right = (packet[1] & 0x02) >> 1;
9833808843cSYunkang Tang 	middle = (packet[1] & 0x04) >> 2;
9843808843cSYunkang Tang 
9853808843cSYunkang Tang 	/* Divide 2 since trackpoint's speed is too fast */
9863808843cSYunkang Tang 	input_report_rel(dev2, REL_X, (char)x / 2);
9873808843cSYunkang Tang 	input_report_rel(dev2, REL_Y, -((char)y / 2));
9883808843cSYunkang Tang 
9893808843cSYunkang Tang 	input_report_key(dev2, BTN_LEFT, left);
9903808843cSYunkang Tang 	input_report_key(dev2, BTN_RIGHT, right);
9913808843cSYunkang Tang 	input_report_key(dev2, BTN_MIDDLE, middle);
9923808843cSYunkang Tang 
9933808843cSYunkang Tang 	input_sync(dev2);
9943808843cSYunkang Tang }
9953808843cSYunkang Tang 
9963808843cSYunkang Tang static void alps_process_touchpad_packet_v7(struct psmouse *psmouse)
9973808843cSYunkang Tang {
9983808843cSYunkang Tang 	struct alps_data *priv = psmouse->private;
9993808843cSYunkang Tang 	struct input_dev *dev = psmouse->dev;
10003808843cSYunkang Tang 	struct alps_fields *f = &priv->f;
10013808843cSYunkang Tang 
10023808843cSYunkang Tang 	memset(f, 0, sizeof(*f));
10033808843cSYunkang Tang 
10043808843cSYunkang Tang 	if (priv->decode_fields(f, psmouse->packet, psmouse))
10053808843cSYunkang Tang 		return;
10063808843cSYunkang Tang 
10073808843cSYunkang Tang 	alps_report_mt_data(psmouse, alps_get_mt_count(f->mt));
10083808843cSYunkang Tang 
10093808843cSYunkang Tang 	input_mt_report_finger_count(dev, f->fingers);
10103808843cSYunkang Tang 
10113808843cSYunkang Tang 	input_report_key(dev, BTN_LEFT, f->left);
10123808843cSYunkang Tang 	input_report_key(dev, BTN_RIGHT, f->right);
10133808843cSYunkang Tang 	input_report_key(dev, BTN_MIDDLE, f->middle);
10143808843cSYunkang Tang 
10153808843cSYunkang Tang 	input_sync(dev);
10163808843cSYunkang Tang }
10173808843cSYunkang Tang 
10183808843cSYunkang Tang static void alps_process_packet_v7(struct psmouse *psmouse)
10193808843cSYunkang Tang {
10203808843cSYunkang Tang 	unsigned char *packet = psmouse->packet;
10213808843cSYunkang Tang 
10223808843cSYunkang Tang 	if (packet[0] == 0x48 && (packet[4] & 0x47) == 0x06)
10233808843cSYunkang Tang 		alps_process_trackstick_packet_v7(psmouse);
10243808843cSYunkang Tang 	else
10253808843cSYunkang Tang 		alps_process_touchpad_packet_v7(psmouse);
10263808843cSYunkang Tang }
10273808843cSYunkang Tang 
10281d9f2626SSebastian Kapfer static void alps_report_bare_ps2_packet(struct psmouse *psmouse,
10291d9f2626SSebastian Kapfer 					unsigned char packet[],
10301d9f2626SSebastian Kapfer 					bool report_buttons)
10311d9f2626SSebastian Kapfer {
10321d9f2626SSebastian Kapfer 	struct alps_data *priv = psmouse->private;
10331d9f2626SSebastian Kapfer 	struct input_dev *dev2 = priv->dev2;
10341d9f2626SSebastian Kapfer 
10351d9f2626SSebastian Kapfer 	if (report_buttons)
10361d9f2626SSebastian Kapfer 		alps_report_buttons(psmouse, dev2, psmouse->dev,
10371d9f2626SSebastian Kapfer 				packet[0] & 1, packet[0] & 2, packet[0] & 4);
10381d9f2626SSebastian Kapfer 
10391d9f2626SSebastian Kapfer 	input_report_rel(dev2, REL_X,
10401d9f2626SSebastian Kapfer 		packet[1] ? packet[1] - ((packet[0] << 4) & 0x100) : 0);
10411d9f2626SSebastian Kapfer 	input_report_rel(dev2, REL_Y,
10421d9f2626SSebastian Kapfer 		packet[2] ? ((packet[0] << 3) & 0x100) - packet[2] : 0);
10431d9f2626SSebastian Kapfer 
10441d9f2626SSebastian Kapfer 	input_sync(dev2);
10451d9f2626SSebastian Kapfer }
10461d9f2626SSebastian Kapfer 
10471d9f2626SSebastian Kapfer static psmouse_ret_t alps_handle_interleaved_ps2(struct psmouse *psmouse)
10481da177e4SLinus Torvalds {
10491da177e4SLinus Torvalds 	struct alps_data *priv = psmouse->private;
10501da177e4SLinus Torvalds 
10511d9f2626SSebastian Kapfer 	if (psmouse->pktcnt < 6)
10521d9f2626SSebastian Kapfer 		return PSMOUSE_GOOD_DATA;
10531d9f2626SSebastian Kapfer 
10541d9f2626SSebastian Kapfer 	if (psmouse->pktcnt == 6) {
10551d9f2626SSebastian Kapfer 		/*
10561d9f2626SSebastian Kapfer 		 * Start a timer to flush the packet if it ends up last
10571d9f2626SSebastian Kapfer 		 * 6-byte packet in the stream. Timer needs to fire
10581d9f2626SSebastian Kapfer 		 * psmouse core times out itself. 20 ms should be enough
10591d9f2626SSebastian Kapfer 		 * to decide if we are getting more data or not.
10601d9f2626SSebastian Kapfer 		 */
10611d9f2626SSebastian Kapfer 		mod_timer(&priv->timer, jiffies + msecs_to_jiffies(20));
10621d9f2626SSebastian Kapfer 		return PSMOUSE_GOOD_DATA;
10631d9f2626SSebastian Kapfer 	}
10641d9f2626SSebastian Kapfer 
10651d9f2626SSebastian Kapfer 	del_timer(&priv->timer);
10661d9f2626SSebastian Kapfer 
10671d9f2626SSebastian Kapfer 	if (psmouse->packet[6] & 0x80) {
10681d9f2626SSebastian Kapfer 
10691d9f2626SSebastian Kapfer 		/*
10701d9f2626SSebastian Kapfer 		 * Highest bit is set - that means we either had
10711d9f2626SSebastian Kapfer 		 * complete ALPS packet and this is start of the
10721d9f2626SSebastian Kapfer 		 * next packet or we got garbage.
10731d9f2626SSebastian Kapfer 		 */
10741d9f2626SSebastian Kapfer 
10751d9f2626SSebastian Kapfer 		if (((psmouse->packet[3] |
10761d9f2626SSebastian Kapfer 		      psmouse->packet[4] |
10771d9f2626SSebastian Kapfer 		      psmouse->packet[5]) & 0x80) ||
107899df65e7SKevin Cernekee 		    (!alps_is_valid_first_byte(priv, psmouse->packet[6]))) {
1079b5d21704SDmitry Torokhov 			psmouse_dbg(psmouse,
10803b112923SAndy Shevchenko 				    "refusing packet %4ph (suspected interleaved ps/2)\n",
10813b112923SAndy Shevchenko 				    psmouse->packet + 3);
10821d9f2626SSebastian Kapfer 			return PSMOUSE_BAD_DATA;
10831d9f2626SSebastian Kapfer 		}
10841d9f2626SSebastian Kapfer 
108524af5cb9SKevin Cernekee 		priv->process_packet(psmouse);
10861d9f2626SSebastian Kapfer 
10871d9f2626SSebastian Kapfer 		/* Continue with the next packet */
10881d9f2626SSebastian Kapfer 		psmouse->packet[0] = psmouse->packet[6];
10891d9f2626SSebastian Kapfer 		psmouse->pktcnt = 1;
10901d9f2626SSebastian Kapfer 
10911d9f2626SSebastian Kapfer 	} else {
10921d9f2626SSebastian Kapfer 
10931d9f2626SSebastian Kapfer 		/*
10941d9f2626SSebastian Kapfer 		 * High bit is 0 - that means that we indeed got a PS/2
10951d9f2626SSebastian Kapfer 		 * packet in the middle of ALPS packet.
10961d9f2626SSebastian Kapfer 		 *
10971d9f2626SSebastian Kapfer 		 * There is also possibility that we got 6-byte ALPS
10981d9f2626SSebastian Kapfer 		 * packet followed  by 3-byte packet from trackpoint. We
10991d9f2626SSebastian Kapfer 		 * can not distinguish between these 2 scenarios but
1100b5d21704SDmitry Torokhov 		 * because the latter is unlikely to happen in course of
11011d9f2626SSebastian Kapfer 		 * normal operation (user would need to press all
11021d9f2626SSebastian Kapfer 		 * buttons on the pad and start moving trackpoint
11031d9f2626SSebastian Kapfer 		 * without touching the pad surface) we assume former.
11041d9f2626SSebastian Kapfer 		 * Even if we are wrong the wost thing that would happen
11051d9f2626SSebastian Kapfer 		 * the cursor would jump but we should not get protocol
1106b5d21704SDmitry Torokhov 		 * de-synchronization.
11071d9f2626SSebastian Kapfer 		 */
11081d9f2626SSebastian Kapfer 
11091d9f2626SSebastian Kapfer 		alps_report_bare_ps2_packet(psmouse, &psmouse->packet[3],
11101d9f2626SSebastian Kapfer 					    false);
11111d9f2626SSebastian Kapfer 
11121d9f2626SSebastian Kapfer 		/*
11131d9f2626SSebastian Kapfer 		 * Continue with the standard ALPS protocol handling,
11141d9f2626SSebastian Kapfer 		 * but make sure we won't process it as an interleaved
11151d9f2626SSebastian Kapfer 		 * packet again, which may happen if all buttons are
11161d9f2626SSebastian Kapfer 		 * pressed. To avoid this let's reset the 4th bit which
11171d9f2626SSebastian Kapfer 		 * is normally 1.
11181d9f2626SSebastian Kapfer 		 */
11191d9f2626SSebastian Kapfer 		psmouse->packet[3] = psmouse->packet[6] & 0xf7;
11201d9f2626SSebastian Kapfer 		psmouse->pktcnt = 4;
11211d9f2626SSebastian Kapfer 	}
11221d9f2626SSebastian Kapfer 
11231d9f2626SSebastian Kapfer 	return PSMOUSE_GOOD_DATA;
11241d9f2626SSebastian Kapfer }
11251d9f2626SSebastian Kapfer 
11261d9f2626SSebastian Kapfer static void alps_flush_packet(unsigned long data)
11271d9f2626SSebastian Kapfer {
11281d9f2626SSebastian Kapfer 	struct psmouse *psmouse = (struct psmouse *)data;
112924af5cb9SKevin Cernekee 	struct alps_data *priv = psmouse->private;
11301d9f2626SSebastian Kapfer 
11311d9f2626SSebastian Kapfer 	serio_pause_rx(psmouse->ps2dev.serio);
11321d9f2626SSebastian Kapfer 
1133b46615feSSeth Forshee 	if (psmouse->pktcnt == psmouse->pktsize) {
11341d9f2626SSebastian Kapfer 
11351d9f2626SSebastian Kapfer 		/*
11361d9f2626SSebastian Kapfer 		 * We did not any more data in reasonable amount of time.
11371d9f2626SSebastian Kapfer 		 * Validate the last 3 bytes and process as a standard
11381d9f2626SSebastian Kapfer 		 * ALPS packet.
11391d9f2626SSebastian Kapfer 		 */
11401d9f2626SSebastian Kapfer 		if ((psmouse->packet[3] |
11411d9f2626SSebastian Kapfer 		     psmouse->packet[4] |
11421d9f2626SSebastian Kapfer 		     psmouse->packet[5]) & 0x80) {
1143b5d21704SDmitry Torokhov 			psmouse_dbg(psmouse,
11443b112923SAndy Shevchenko 				    "refusing packet %3ph (suspected interleaved ps/2)\n",
11453b112923SAndy Shevchenko 				    psmouse->packet + 3);
11461d9f2626SSebastian Kapfer 		} else {
114724af5cb9SKevin Cernekee 			priv->process_packet(psmouse);
11481d9f2626SSebastian Kapfer 		}
11491d9f2626SSebastian Kapfer 		psmouse->pktcnt = 0;
11501d9f2626SSebastian Kapfer 	}
11511d9f2626SSebastian Kapfer 
11521d9f2626SSebastian Kapfer 	serio_continue_rx(psmouse->ps2dev.serio);
11531d9f2626SSebastian Kapfer }
11541d9f2626SSebastian Kapfer 
11551d9f2626SSebastian Kapfer static psmouse_ret_t alps_process_byte(struct psmouse *psmouse)
11561d9f2626SSebastian Kapfer {
11571d9f2626SSebastian Kapfer 	struct alps_data *priv = psmouse->private;
11581d9f2626SSebastian Kapfer 
11594ab8f7f3SPali Rohár 	/*
11604ab8f7f3SPali Rohár 	 * Check if we are dealing with a bare PS/2 packet, presumably from
11614ab8f7f3SPali Rohár 	 * a device connected to the external PS/2 port. Because bare PS/2
11624ab8f7f3SPali Rohár 	 * protocol does not have enough constant bits to self-synchronize
11634ab8f7f3SPali Rohár 	 * properly we only do this if the device is fully synchronized.
11644ab8f7f3SPali Rohár 	 */
11654ab8f7f3SPali Rohár 	if (!psmouse->out_of_sync_cnt && (psmouse->packet[0] & 0xc8) == 0x08) {
11661da177e4SLinus Torvalds 		if (psmouse->pktcnt == 3) {
11671d9f2626SSebastian Kapfer 			alps_report_bare_ps2_packet(psmouse, psmouse->packet,
11681d9f2626SSebastian Kapfer 						    true);
11691da177e4SLinus Torvalds 			return PSMOUSE_FULL_PACKET;
11701da177e4SLinus Torvalds 		}
11711da177e4SLinus Torvalds 		return PSMOUSE_GOOD_DATA;
11721da177e4SLinus Torvalds 	}
11731da177e4SLinus Torvalds 
11741d9f2626SSebastian Kapfer 	/* Check for PS/2 packet stuffed in the middle of ALPS packet. */
11751d9f2626SSebastian Kapfer 
117699df65e7SKevin Cernekee 	if ((priv->flags & ALPS_PS2_INTERLEAVED) &&
11771d9f2626SSebastian Kapfer 	    psmouse->pktcnt >= 4 && (psmouse->packet[3] & 0x0f) == 0x0f) {
11781d9f2626SSebastian Kapfer 		return alps_handle_interleaved_ps2(psmouse);
11791d9f2626SSebastian Kapfer 	}
11801d9f2626SSebastian Kapfer 
118199df65e7SKevin Cernekee 	if (!alps_is_valid_first_byte(priv, psmouse->packet[0])) {
1182b5d21704SDmitry Torokhov 		psmouse_dbg(psmouse,
1183b5d21704SDmitry Torokhov 			    "refusing packet[0] = %x (mask0 = %x, byte0 = %x)\n",
118499df65e7SKevin Cernekee 			    psmouse->packet[0], priv->mask0, priv->byte0);
11851da177e4SLinus Torvalds 		return PSMOUSE_BAD_DATA;
11861d9f2626SSebastian Kapfer 	}
11871da177e4SLinus Torvalds 
1188b46615feSSeth Forshee 	/* Bytes 2 - pktsize should have 0 in the highest bit */
118995f75e91SYunkang Tang 	if ((priv->proto_version < ALPS_PROTO_V5) &&
119075af9e56SDave Turvene 	    psmouse->pktcnt >= 2 && psmouse->pktcnt <= psmouse->pktsize &&
11911d9f2626SSebastian Kapfer 	    (psmouse->packet[psmouse->pktcnt - 1] & 0x80)) {
1192b5d21704SDmitry Torokhov 		psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
1193b5d21704SDmitry Torokhov 			    psmouse->pktcnt - 1,
1194b5d21704SDmitry Torokhov 			    psmouse->packet[psmouse->pktcnt - 1]);
11951da177e4SLinus Torvalds 		return PSMOUSE_BAD_DATA;
11961d9f2626SSebastian Kapfer 	}
11971da177e4SLinus Torvalds 
11983808843cSYunkang Tang 	if (priv->proto_version == ALPS_PROTO_V7 &&
11993808843cSYunkang Tang 	    !alps_is_valid_package_v7(psmouse)) {
12003808843cSYunkang Tang 		psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
12013808843cSYunkang Tang 			    psmouse->pktcnt - 1,
12023808843cSYunkang Tang 			    psmouse->packet[psmouse->pktcnt - 1]);
12033808843cSYunkang Tang 		return PSMOUSE_BAD_DATA;
12043808843cSYunkang Tang 	}
12053808843cSYunkang Tang 
1206b46615feSSeth Forshee 	if (psmouse->pktcnt == psmouse->pktsize) {
120724af5cb9SKevin Cernekee 		priv->process_packet(psmouse);
12081da177e4SLinus Torvalds 		return PSMOUSE_FULL_PACKET;
12091da177e4SLinus Torvalds 	}
12101da177e4SLinus Torvalds 
12111da177e4SLinus Torvalds 	return PSMOUSE_GOOD_DATA;
12121da177e4SLinus Torvalds }
12131da177e4SLinus Torvalds 
121425bded7cSSeth Forshee static int alps_command_mode_send_nibble(struct psmouse *psmouse, int nibble)
121525bded7cSSeth Forshee {
121625bded7cSSeth Forshee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
121725bded7cSSeth Forshee 	struct alps_data *priv = psmouse->private;
121825bded7cSSeth Forshee 	int command;
121925bded7cSSeth Forshee 	unsigned char *param;
122025bded7cSSeth Forshee 	unsigned char dummy[4];
122125bded7cSSeth Forshee 
122225bded7cSSeth Forshee 	BUG_ON(nibble > 0xf);
122325bded7cSSeth Forshee 
122425bded7cSSeth Forshee 	command = priv->nibble_commands[nibble].command;
122525bded7cSSeth Forshee 	param = (command & 0x0f00) ?
122625bded7cSSeth Forshee 		dummy : (unsigned char *)&priv->nibble_commands[nibble].data;
122725bded7cSSeth Forshee 
122825bded7cSSeth Forshee 	if (ps2_command(ps2dev, param, command))
122925bded7cSSeth Forshee 		return -1;
123025bded7cSSeth Forshee 
123125bded7cSSeth Forshee 	return 0;
123225bded7cSSeth Forshee }
123325bded7cSSeth Forshee 
123425bded7cSSeth Forshee static int alps_command_mode_set_addr(struct psmouse *psmouse, int addr)
123525bded7cSSeth Forshee {
123625bded7cSSeth Forshee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
123725bded7cSSeth Forshee 	struct alps_data *priv = psmouse->private;
123825bded7cSSeth Forshee 	int i, nibble;
123925bded7cSSeth Forshee 
124025bded7cSSeth Forshee 	if (ps2_command(ps2dev, NULL, priv->addr_command))
124125bded7cSSeth Forshee 		return -1;
124225bded7cSSeth Forshee 
124325bded7cSSeth Forshee 	for (i = 12; i >= 0; i -= 4) {
124425bded7cSSeth Forshee 		nibble = (addr >> i) & 0xf;
124525bded7cSSeth Forshee 		if (alps_command_mode_send_nibble(psmouse, nibble))
124625bded7cSSeth Forshee 			return -1;
124725bded7cSSeth Forshee 	}
124825bded7cSSeth Forshee 
124925bded7cSSeth Forshee 	return 0;
125025bded7cSSeth Forshee }
125125bded7cSSeth Forshee 
125225bded7cSSeth Forshee static int __alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
125325bded7cSSeth Forshee {
125425bded7cSSeth Forshee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
125525bded7cSSeth Forshee 	unsigned char param[4];
125625bded7cSSeth Forshee 
125725bded7cSSeth Forshee 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
125825bded7cSSeth Forshee 		return -1;
125925bded7cSSeth Forshee 
126025bded7cSSeth Forshee 	/*
126125bded7cSSeth Forshee 	 * The address being read is returned in the first two bytes
126225bded7cSSeth Forshee 	 * of the result. Check that this address matches the expected
126325bded7cSSeth Forshee 	 * address.
126425bded7cSSeth Forshee 	 */
126525bded7cSSeth Forshee 	if (addr != ((param[0] << 8) | param[1]))
126625bded7cSSeth Forshee 		return -1;
126725bded7cSSeth Forshee 
126825bded7cSSeth Forshee 	return param[2];
126925bded7cSSeth Forshee }
127025bded7cSSeth Forshee 
127125bded7cSSeth Forshee static int alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
127225bded7cSSeth Forshee {
127325bded7cSSeth Forshee 	if (alps_command_mode_set_addr(psmouse, addr))
127425bded7cSSeth Forshee 		return -1;
127525bded7cSSeth Forshee 	return __alps_command_mode_read_reg(psmouse, addr);
127625bded7cSSeth Forshee }
127725bded7cSSeth Forshee 
127825bded7cSSeth Forshee static int __alps_command_mode_write_reg(struct psmouse *psmouse, u8 value)
127925bded7cSSeth Forshee {
128025bded7cSSeth Forshee 	if (alps_command_mode_send_nibble(psmouse, (value >> 4) & 0xf))
128125bded7cSSeth Forshee 		return -1;
128225bded7cSSeth Forshee 	if (alps_command_mode_send_nibble(psmouse, value & 0xf))
128325bded7cSSeth Forshee 		return -1;
128425bded7cSSeth Forshee 	return 0;
128525bded7cSSeth Forshee }
128625bded7cSSeth Forshee 
128725bded7cSSeth Forshee static int alps_command_mode_write_reg(struct psmouse *psmouse, int addr,
128825bded7cSSeth Forshee 				       u8 value)
128925bded7cSSeth Forshee {
129025bded7cSSeth Forshee 	if (alps_command_mode_set_addr(psmouse, addr))
129125bded7cSSeth Forshee 		return -1;
129225bded7cSSeth Forshee 	return __alps_command_mode_write_reg(psmouse, value);
129325bded7cSSeth Forshee }
129425bded7cSSeth Forshee 
129524ba9707SKevin Cernekee static int alps_rpt_cmd(struct psmouse *psmouse, int init_command,
129624ba9707SKevin Cernekee 			int repeated_command, unsigned char *param)
129724ba9707SKevin Cernekee {
129824ba9707SKevin Cernekee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
129924ba9707SKevin Cernekee 
130024ba9707SKevin Cernekee 	param[0] = 0;
130124ba9707SKevin Cernekee 	if (init_command && ps2_command(ps2dev, param, init_command))
130224ba9707SKevin Cernekee 		return -EIO;
130324ba9707SKevin Cernekee 
130424ba9707SKevin Cernekee 	if (ps2_command(ps2dev,  NULL, repeated_command) ||
130524ba9707SKevin Cernekee 	    ps2_command(ps2dev,  NULL, repeated_command) ||
130624ba9707SKevin Cernekee 	    ps2_command(ps2dev,  NULL, repeated_command))
130724ba9707SKevin Cernekee 		return -EIO;
130824ba9707SKevin Cernekee 
130924ba9707SKevin Cernekee 	param[0] = param[1] = param[2] = 0xff;
131024ba9707SKevin Cernekee 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
131124ba9707SKevin Cernekee 		return -EIO;
131224ba9707SKevin Cernekee 
131339fbe585SDmitry Torokhov 	psmouse_dbg(psmouse, "%2.2X report: %3ph\n",
131439fbe585SDmitry Torokhov 		    repeated_command, param);
131524ba9707SKevin Cernekee 	return 0;
131624ba9707SKevin Cernekee }
131724ba9707SKevin Cernekee 
13183808843cSYunkang Tang static bool alps_check_valid_firmware_id(unsigned char id[])
13193808843cSYunkang Tang {
13203808843cSYunkang Tang 	if (id[0] == 0x73)
13213808843cSYunkang Tang 		return true;
13223808843cSYunkang Tang 
13233808843cSYunkang Tang 	if (id[0] == 0x88 &&
13243808843cSYunkang Tang 	    (id[1] == 0x07 ||
13253808843cSYunkang Tang 	     id[1] == 0x08 ||
13263808843cSYunkang Tang 	     (id[1] & 0xf0) == 0xb0 ||
13273808843cSYunkang Tang 	     (id[1] & 0xf0) == 0xc0)) {
13283808843cSYunkang Tang 		return true;
13293808843cSYunkang Tang 	}
13303808843cSYunkang Tang 
13313808843cSYunkang Tang 	return false;
13323808843cSYunkang Tang }
13333808843cSYunkang Tang 
1334d18e53fcSKevin Cernekee static int alps_enter_command_mode(struct psmouse *psmouse)
133525bded7cSSeth Forshee {
133625bded7cSSeth Forshee 	unsigned char param[4];
133725bded7cSSeth Forshee 
133824ba9707SKevin Cernekee 	if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_RESET_WRAP, param)) {
133925bded7cSSeth Forshee 		psmouse_err(psmouse, "failed to enter command mode\n");
134025bded7cSSeth Forshee 		return -1;
134125bded7cSSeth Forshee 	}
134225bded7cSSeth Forshee 
13433808843cSYunkang Tang 	if (!alps_check_valid_firmware_id(param)) {
134425bded7cSSeth Forshee 		psmouse_dbg(psmouse,
134524ba9707SKevin Cernekee 			    "unknown response while entering command mode\n");
134625bded7cSSeth Forshee 		return -1;
134725bded7cSSeth Forshee 	}
134825bded7cSSeth Forshee 	return 0;
134925bded7cSSeth Forshee }
135025bded7cSSeth Forshee 
135125bded7cSSeth Forshee static inline int alps_exit_command_mode(struct psmouse *psmouse)
135225bded7cSSeth Forshee {
135325bded7cSSeth Forshee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
135425bded7cSSeth Forshee 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM))
135525bded7cSSeth Forshee 		return -1;
135625bded7cSSeth Forshee 	return 0;
135725bded7cSSeth Forshee }
135825bded7cSSeth Forshee 
13591da177e4SLinus Torvalds /*
13601da177e4SLinus Torvalds  * For DualPoint devices select the device that should respond to
13611da177e4SLinus Torvalds  * subsequent commands. It looks like glidepad is behind stickpointer,
13621da177e4SLinus Torvalds  * I'd thought it would be other way around...
13631da177e4SLinus Torvalds  */
136425bded7cSSeth Forshee static int alps_passthrough_mode_v2(struct psmouse *psmouse, bool enable)
13651da177e4SLinus Torvalds {
13661da177e4SLinus Torvalds 	struct ps2dev *ps2dev = &psmouse->ps2dev;
13671da177e4SLinus Torvalds 	int cmd = enable ? PSMOUSE_CMD_SETSCALE21 : PSMOUSE_CMD_SETSCALE11;
13681da177e4SLinus Torvalds 
13691da177e4SLinus Torvalds 	if (ps2_command(ps2dev, NULL, cmd) ||
13701da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, cmd) ||
13711da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, cmd) ||
13721da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
13731da177e4SLinus Torvalds 		return -1;
13741da177e4SLinus Torvalds 
13751da177e4SLinus Torvalds 	/* we may get 3 more bytes, just ignore them */
1376c611763dSDmitry Torokhov 	ps2_drain(ps2dev, 3, 100);
13771da177e4SLinus Torvalds 
13781da177e4SLinus Torvalds 	return 0;
13791da177e4SLinus Torvalds }
13801da177e4SLinus Torvalds 
138125bded7cSSeth Forshee static int alps_absolute_mode_v1_v2(struct psmouse *psmouse)
13821da177e4SLinus Torvalds {
13831da177e4SLinus Torvalds 	struct ps2dev *ps2dev = &psmouse->ps2dev;
13841da177e4SLinus Torvalds 
13851da177e4SLinus Torvalds 	/* Try ALPS magic knock - 4 disable before enable */
13861da177e4SLinus Torvalds 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
13871da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
13881da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
13891da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
13901da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
13911da177e4SLinus Torvalds 		return -1;
13921da177e4SLinus Torvalds 
13931da177e4SLinus Torvalds 	/*
13941da177e4SLinus Torvalds 	 * Switch mouse to poll (remote) mode so motion data will not
13951da177e4SLinus Torvalds 	 * get in our way
13961da177e4SLinus Torvalds 	 */
13971da177e4SLinus Torvalds 	return ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETPOLL);
13981da177e4SLinus Torvalds }
13991da177e4SLinus Torvalds 
140095f75e91SYunkang Tang static int alps_monitor_mode_send_word(struct psmouse *psmouse, u16 word)
140195f75e91SYunkang Tang {
140295f75e91SYunkang Tang 	int i, nibble;
140395f75e91SYunkang Tang 
140495f75e91SYunkang Tang 	/*
140595f75e91SYunkang Tang 	 * b0-b11 are valid bits, send sequence is inverse.
140695f75e91SYunkang Tang 	 * e.g. when word = 0x0123, nibble send sequence is 3, 2, 1
140795f75e91SYunkang Tang 	 */
140895f75e91SYunkang Tang 	for (i = 0; i <= 8; i += 4) {
140995f75e91SYunkang Tang 		nibble = (word >> i) & 0xf;
141095f75e91SYunkang Tang 		if (alps_command_mode_send_nibble(psmouse, nibble))
141195f75e91SYunkang Tang 			return -1;
141295f75e91SYunkang Tang 	}
141395f75e91SYunkang Tang 
141495f75e91SYunkang Tang 	return 0;
141595f75e91SYunkang Tang }
141695f75e91SYunkang Tang 
141795f75e91SYunkang Tang static int alps_monitor_mode_write_reg(struct psmouse *psmouse,
141895f75e91SYunkang Tang 				       u16 addr, u16 value)
141995f75e91SYunkang Tang {
142095f75e91SYunkang Tang 	struct ps2dev *ps2dev = &psmouse->ps2dev;
142195f75e91SYunkang Tang 
142295f75e91SYunkang Tang 	/* 0x0A0 is the command to write the word */
142395f75e91SYunkang Tang 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE) ||
142495f75e91SYunkang Tang 	    alps_monitor_mode_send_word(psmouse, 0x0A0) ||
142595f75e91SYunkang Tang 	    alps_monitor_mode_send_word(psmouse, addr) ||
142695f75e91SYunkang Tang 	    alps_monitor_mode_send_word(psmouse, value) ||
142795f75e91SYunkang Tang 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
142895f75e91SYunkang Tang 		return -1;
142995f75e91SYunkang Tang 
143095f75e91SYunkang Tang 	return 0;
143195f75e91SYunkang Tang }
143295f75e91SYunkang Tang 
143395f75e91SYunkang Tang static int alps_monitor_mode(struct psmouse *psmouse, bool enable)
143495f75e91SYunkang Tang {
143595f75e91SYunkang Tang 	struct ps2dev *ps2dev = &psmouse->ps2dev;
143695f75e91SYunkang Tang 
143795f75e91SYunkang Tang 	if (enable) {
143895f75e91SYunkang Tang 		/* EC E9 F5 F5 E7 E6 E7 E9 to enter monitor mode */
143995f75e91SYunkang Tang 		if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
144095f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO) ||
144195f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
144295f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
144395f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
144495f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
144595f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
144695f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO))
144795f75e91SYunkang Tang 			return -1;
144895f75e91SYunkang Tang 	} else {
144995f75e91SYunkang Tang 		/* EC to exit monitor mode */
145095f75e91SYunkang Tang 		if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP))
145195f75e91SYunkang Tang 			return -1;
145295f75e91SYunkang Tang 	}
145395f75e91SYunkang Tang 
145495f75e91SYunkang Tang 	return 0;
145595f75e91SYunkang Tang }
145695f75e91SYunkang Tang 
145795f75e91SYunkang Tang static int alps_absolute_mode_v6(struct psmouse *psmouse)
145895f75e91SYunkang Tang {
145995f75e91SYunkang Tang 	u16 reg_val = 0x181;
146095f75e91SYunkang Tang 	int ret = -1;
146195f75e91SYunkang Tang 
146295f75e91SYunkang Tang 	/* enter monitor mode, to write the register */
146395f75e91SYunkang Tang 	if (alps_monitor_mode(psmouse, true))
146495f75e91SYunkang Tang 		return -1;
146595f75e91SYunkang Tang 
146695f75e91SYunkang Tang 	ret = alps_monitor_mode_write_reg(psmouse, 0x000, reg_val);
146795f75e91SYunkang Tang 
146895f75e91SYunkang Tang 	if (alps_monitor_mode(psmouse, false))
146995f75e91SYunkang Tang 		ret = -1;
147095f75e91SYunkang Tang 
147195f75e91SYunkang Tang 	return ret;
147295f75e91SYunkang Tang }
147395f75e91SYunkang Tang 
14741da177e4SLinus Torvalds static int alps_get_status(struct psmouse *psmouse, char *param)
14751da177e4SLinus Torvalds {
14761da177e4SLinus Torvalds 	/* Get status: 0xF5 0xF5 0xF5 0xE9 */
147724ba9707SKevin Cernekee 	if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_DISABLE, param))
14781da177e4SLinus Torvalds 		return -1;
14791da177e4SLinus Torvalds 
14801da177e4SLinus Torvalds 	return 0;
14811da177e4SLinus Torvalds }
14821da177e4SLinus Torvalds 
14831da177e4SLinus Torvalds /*
14841da177e4SLinus Torvalds  * Turn touchpad tapping on or off. The sequences are:
14851da177e4SLinus Torvalds  * 0xE9 0xF5 0xF5 0xF3 0x0A to enable,
14861da177e4SLinus Torvalds  * 0xE9 0xF5 0xF5 0xE8 0x00 to disable.
14871da177e4SLinus Torvalds  * My guess that 0xE9 (GetInfo) is here as a sync point.
14881da177e4SLinus Torvalds  * For models that also have stickpointer (DualPoints) its tapping
14891da177e4SLinus Torvalds  * is controlled separately (0xE6 0xE6 0xE6 0xF3 0x14|0x0A) but
14901da177e4SLinus Torvalds  * we don't fiddle with it.
14911da177e4SLinus Torvalds  */
14921da177e4SLinus Torvalds static int alps_tap_mode(struct psmouse *psmouse, int enable)
14931da177e4SLinus Torvalds {
14941da177e4SLinus Torvalds 	struct ps2dev *ps2dev = &psmouse->ps2dev;
14951da177e4SLinus Torvalds 	int cmd = enable ? PSMOUSE_CMD_SETRATE : PSMOUSE_CMD_SETRES;
14961da177e4SLinus Torvalds 	unsigned char tap_arg = enable ? 0x0A : 0x00;
14971da177e4SLinus Torvalds 	unsigned char param[4];
14981da177e4SLinus Torvalds 
14991da177e4SLinus Torvalds 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO) ||
15001da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
15011da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
15021da177e4SLinus Torvalds 	    ps2_command(ps2dev, &tap_arg, cmd))
15031da177e4SLinus Torvalds 		return -1;
15041da177e4SLinus Torvalds 
15051da177e4SLinus Torvalds 	if (alps_get_status(psmouse, param))
15061da177e4SLinus Torvalds 		return -1;
15071da177e4SLinus Torvalds 
15081da177e4SLinus Torvalds 	return 0;
15091da177e4SLinus Torvalds }
15101da177e4SLinus Torvalds 
1511f0d5c6f4SDmitry Torokhov /*
1512f0d5c6f4SDmitry Torokhov  * alps_poll() - poll the touchpad for current motion packet.
1513f0d5c6f4SDmitry Torokhov  * Used in resync.
1514f0d5c6f4SDmitry Torokhov  */
1515f0d5c6f4SDmitry Torokhov static int alps_poll(struct psmouse *psmouse)
1516f0d5c6f4SDmitry Torokhov {
1517f0d5c6f4SDmitry Torokhov 	struct alps_data *priv = psmouse->private;
1518b46615feSSeth Forshee 	unsigned char buf[sizeof(psmouse->packet)];
1519b7802c5cSDmitry Torokhov 	bool poll_failed;
1520f0d5c6f4SDmitry Torokhov 
152199df65e7SKevin Cernekee 	if (priv->flags & ALPS_PASS)
152225bded7cSSeth Forshee 		alps_passthrough_mode_v2(psmouse, true);
1523f0d5c6f4SDmitry Torokhov 
1524f0d5c6f4SDmitry Torokhov 	poll_failed = ps2_command(&psmouse->ps2dev, buf,
1525f0d5c6f4SDmitry Torokhov 				  PSMOUSE_CMD_POLL | (psmouse->pktsize << 8)) < 0;
1526f0d5c6f4SDmitry Torokhov 
152799df65e7SKevin Cernekee 	if (priv->flags & ALPS_PASS)
152825bded7cSSeth Forshee 		alps_passthrough_mode_v2(psmouse, false);
1529f0d5c6f4SDmitry Torokhov 
153099df65e7SKevin Cernekee 	if (poll_failed || (buf[0] & priv->mask0) != priv->byte0)
1531f0d5c6f4SDmitry Torokhov 		return -1;
1532f0d5c6f4SDmitry Torokhov 
1533f0d5c6f4SDmitry Torokhov 	if ((psmouse->badbyte & 0xc8) == 0x08) {
1534f0d5c6f4SDmitry Torokhov /*
1535f0d5c6f4SDmitry Torokhov  * Poll the track stick ...
1536f0d5c6f4SDmitry Torokhov  */
1537f0d5c6f4SDmitry Torokhov 		if (ps2_command(&psmouse->ps2dev, buf, PSMOUSE_CMD_POLL | (3 << 8)))
1538f0d5c6f4SDmitry Torokhov 			return -1;
1539f0d5c6f4SDmitry Torokhov 	}
1540f0d5c6f4SDmitry Torokhov 
1541f0d5c6f4SDmitry Torokhov 	memcpy(psmouse->packet, buf, sizeof(buf));
1542f0d5c6f4SDmitry Torokhov 	return 0;
1543f0d5c6f4SDmitry Torokhov }
1544f0d5c6f4SDmitry Torokhov 
154525bded7cSSeth Forshee static int alps_hw_init_v1_v2(struct psmouse *psmouse)
15461da177e4SLinus Torvalds {
15471da177e4SLinus Torvalds 	struct alps_data *priv = psmouse->private;
15481da177e4SLinus Torvalds 
154999df65e7SKevin Cernekee 	if ((priv->flags & ALPS_PASS) &&
155025bded7cSSeth Forshee 	    alps_passthrough_mode_v2(psmouse, true)) {
15511da177e4SLinus Torvalds 		return -1;
1552b7802c5cSDmitry Torokhov 	}
15531da177e4SLinus Torvalds 
1554b7802c5cSDmitry Torokhov 	if (alps_tap_mode(psmouse, true)) {
1555b5d21704SDmitry Torokhov 		psmouse_warn(psmouse, "Failed to enable hardware tapping\n");
15561da177e4SLinus Torvalds 		return -1;
1557963f626dSPeter Osterlund 	}
15581da177e4SLinus Torvalds 
155925bded7cSSeth Forshee 	if (alps_absolute_mode_v1_v2(psmouse)) {
1560b5d21704SDmitry Torokhov 		psmouse_err(psmouse, "Failed to enable absolute mode\n");
15611da177e4SLinus Torvalds 		return -1;
15621da177e4SLinus Torvalds 	}
15631da177e4SLinus Torvalds 
156499df65e7SKevin Cernekee 	if ((priv->flags & ALPS_PASS) &&
156525bded7cSSeth Forshee 	    alps_passthrough_mode_v2(psmouse, false)) {
15661da177e4SLinus Torvalds 		return -1;
1567b7802c5cSDmitry Torokhov 	}
15681da177e4SLinus Torvalds 
15691e0c5b12SDmitry Torokhov 	/* ALPS needs stream mode, otherwise it won't report any data */
15701e0c5b12SDmitry Torokhov 	if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)) {
1571b5d21704SDmitry Torokhov 		psmouse_err(psmouse, "Failed to enable stream mode\n");
15721e0c5b12SDmitry Torokhov 		return -1;
15731e0c5b12SDmitry Torokhov 	}
15741e0c5b12SDmitry Torokhov 
15751e0c5b12SDmitry Torokhov 	return 0;
15761e0c5b12SDmitry Torokhov }
15771e0c5b12SDmitry Torokhov 
157895f75e91SYunkang Tang static int alps_hw_init_v6(struct psmouse *psmouse)
157995f75e91SYunkang Tang {
158095f75e91SYunkang Tang 	unsigned char param[2] = {0xC8, 0x14};
158195f75e91SYunkang Tang 
158295f75e91SYunkang Tang 	/* Enter passthrough mode to let trackpoint enter 6byte raw mode */
158395f75e91SYunkang Tang 	if (alps_passthrough_mode_v2(psmouse, true))
158495f75e91SYunkang Tang 		return -1;
158595f75e91SYunkang Tang 
158695f75e91SYunkang Tang 	if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
158795f75e91SYunkang Tang 	    ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
158895f75e91SYunkang Tang 	    ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
158995f75e91SYunkang Tang 	    ps2_command(&psmouse->ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
159095f75e91SYunkang Tang 	    ps2_command(&psmouse->ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
159195f75e91SYunkang Tang 		return -1;
159295f75e91SYunkang Tang 
159395f75e91SYunkang Tang 	if (alps_passthrough_mode_v2(psmouse, false))
159495f75e91SYunkang Tang 		return -1;
159595f75e91SYunkang Tang 
159695f75e91SYunkang Tang 	if (alps_absolute_mode_v6(psmouse)) {
159795f75e91SYunkang Tang 		psmouse_err(psmouse, "Failed to enable absolute mode\n");
159895f75e91SYunkang Tang 		return -1;
159995f75e91SYunkang Tang 	}
160095f75e91SYunkang Tang 
160195f75e91SYunkang Tang 	return 0;
160295f75e91SYunkang Tang }
160395f75e91SYunkang Tang 
160425bded7cSSeth Forshee /*
1605cd401204SKevin Cernekee  * Enable or disable passthrough mode to the trackstick.
160625bded7cSSeth Forshee  */
1607cd401204SKevin Cernekee static int alps_passthrough_mode_v3(struct psmouse *psmouse,
1608cd401204SKevin Cernekee 				    int reg_base, bool enable)
160925bded7cSSeth Forshee {
1610cd401204SKevin Cernekee 	int reg_val, ret = -1;
161125bded7cSSeth Forshee 
1612d18e53fcSKevin Cernekee 	if (alps_enter_command_mode(psmouse))
161325bded7cSSeth Forshee 		return -1;
161425bded7cSSeth Forshee 
1615cd401204SKevin Cernekee 	reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x0008);
1616cd401204SKevin Cernekee 	if (reg_val == -1)
1617cd401204SKevin Cernekee 		goto error;
1618cd401204SKevin Cernekee 
161925bded7cSSeth Forshee 	if (enable)
162025bded7cSSeth Forshee 		reg_val |= 0x01;
162125bded7cSSeth Forshee 	else
162225bded7cSSeth Forshee 		reg_val &= ~0x01;
162325bded7cSSeth Forshee 
1624cd401204SKevin Cernekee 	ret = __alps_command_mode_write_reg(psmouse, reg_val);
162525bded7cSSeth Forshee 
1626cd401204SKevin Cernekee error:
1627cd401204SKevin Cernekee 	if (alps_exit_command_mode(psmouse))
1628cd401204SKevin Cernekee 		ret = -1;
1629cd401204SKevin Cernekee 	return ret;
163025bded7cSSeth Forshee }
163125bded7cSSeth Forshee 
163225bded7cSSeth Forshee /* Must be in command mode when calling this function */
163325bded7cSSeth Forshee static int alps_absolute_mode_v3(struct psmouse *psmouse)
163425bded7cSSeth Forshee {
163525bded7cSSeth Forshee 	int reg_val;
163625bded7cSSeth Forshee 
163725bded7cSSeth Forshee 	reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
163825bded7cSSeth Forshee 	if (reg_val == -1)
163925bded7cSSeth Forshee 		return -1;
164025bded7cSSeth Forshee 
164125bded7cSSeth Forshee 	reg_val |= 0x06;
164225bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, reg_val))
164325bded7cSSeth Forshee 		return -1;
164425bded7cSSeth Forshee 
164525bded7cSSeth Forshee 	return 0;
164625bded7cSSeth Forshee }
164725bded7cSSeth Forshee 
1648cd401204SKevin Cernekee static int alps_probe_trackstick_v3(struct psmouse *psmouse, int reg_base)
164925bded7cSSeth Forshee {
1650cd401204SKevin Cernekee 	int ret = -EIO, reg_val;
165125bded7cSSeth Forshee 
1652d18e53fcSKevin Cernekee 	if (alps_enter_command_mode(psmouse))
165325bded7cSSeth Forshee 		goto error;
165425bded7cSSeth Forshee 
1655cd401204SKevin Cernekee 	reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x08);
165625bded7cSSeth Forshee 	if (reg_val == -1)
165725bded7cSSeth Forshee 		goto error;
1658cd401204SKevin Cernekee 
1659cd401204SKevin Cernekee 	/* bit 7: trackstick is present */
1660cd401204SKevin Cernekee 	ret = reg_val & 0x80 ? 0 : -ENODEV;
1661cd401204SKevin Cernekee 
1662cd401204SKevin Cernekee error:
1663cd401204SKevin Cernekee 	alps_exit_command_mode(psmouse);
1664cd401204SKevin Cernekee 	return ret;
1665cd401204SKevin Cernekee }
1666cd401204SKevin Cernekee 
1667cd401204SKevin Cernekee static int alps_setup_trackstick_v3(struct psmouse *psmouse, int reg_base)
1668cd401204SKevin Cernekee {
1669cd401204SKevin Cernekee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
1670cd401204SKevin Cernekee 	int ret = 0;
1671cd401204SKevin Cernekee 	unsigned char param[4];
1672cd401204SKevin Cernekee 
1673cd401204SKevin Cernekee 	if (alps_passthrough_mode_v3(psmouse, reg_base, true))
1674cd401204SKevin Cernekee 		return -EIO;
167525bded7cSSeth Forshee 
167625bded7cSSeth Forshee 	/*
167725bded7cSSeth Forshee 	 * E7 report for the trackstick
167825bded7cSSeth Forshee 	 *
167925bded7cSSeth Forshee 	 * There have been reports of failures to seem to trace back
168025bded7cSSeth Forshee 	 * to the above trackstick check failing. When these occur
168125bded7cSSeth Forshee 	 * this E7 report fails, so when that happens we continue
168225bded7cSSeth Forshee 	 * with the assumption that there isn't a trackstick after
168325bded7cSSeth Forshee 	 * all.
168425bded7cSSeth Forshee 	 */
1685cd401204SKevin Cernekee 	if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_SETSCALE21, param)) {
168625bded7cSSeth Forshee 		psmouse_warn(psmouse, "trackstick E7 report failed\n");
1687cd401204SKevin Cernekee 		ret = -ENODEV;
168825bded7cSSeth Forshee 	} else {
168939fbe585SDmitry Torokhov 		psmouse_dbg(psmouse, "trackstick E7 report: %3ph\n", param);
169025bded7cSSeth Forshee 
169125bded7cSSeth Forshee 		/*
169225bded7cSSeth Forshee 		 * Not sure what this does, but it is absolutely
169325bded7cSSeth Forshee 		 * essential. Without it, the touchpad does not
169425bded7cSSeth Forshee 		 * work at all and the trackstick just emits normal
169525bded7cSSeth Forshee 		 * PS/2 packets.
169625bded7cSSeth Forshee 		 */
169725bded7cSSeth Forshee 		if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
169825bded7cSSeth Forshee 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
169925bded7cSSeth Forshee 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
170025bded7cSSeth Forshee 		    alps_command_mode_send_nibble(psmouse, 0x9) ||
170125bded7cSSeth Forshee 		    alps_command_mode_send_nibble(psmouse, 0x4)) {
170225bded7cSSeth Forshee 			psmouse_err(psmouse,
170325bded7cSSeth Forshee 				    "Error sending magic E6 sequence\n");
1704cd401204SKevin Cernekee 			ret = -EIO;
170525bded7cSSeth Forshee 			goto error;
170625bded7cSSeth Forshee 		}
170725bded7cSSeth Forshee 
1708cd401204SKevin Cernekee 		/*
1709cd401204SKevin Cernekee 		 * This ensures the trackstick packets are in the format
1710cd401204SKevin Cernekee 		 * supported by this driver. If bit 1 isn't set the packet
1711cd401204SKevin Cernekee 		 * format is different.
1712cd401204SKevin Cernekee 		 */
1713d18e53fcSKevin Cernekee 		if (alps_enter_command_mode(psmouse) ||
1714cd401204SKevin Cernekee 		    alps_command_mode_write_reg(psmouse,
1715cd401204SKevin Cernekee 						reg_base + 0x08, 0x82) ||
1716cd401204SKevin Cernekee 		    alps_exit_command_mode(psmouse))
1717cd401204SKevin Cernekee 			ret = -EIO;
1718cd401204SKevin Cernekee 	}
1719cd401204SKevin Cernekee 
1720cd401204SKevin Cernekee error:
1721cd401204SKevin Cernekee 	if (alps_passthrough_mode_v3(psmouse, reg_base, false))
1722cd401204SKevin Cernekee 		ret = -EIO;
1723cd401204SKevin Cernekee 
1724cd401204SKevin Cernekee 	return ret;
1725cd401204SKevin Cernekee }
1726cd401204SKevin Cernekee 
1727cd401204SKevin Cernekee static int alps_hw_init_v3(struct psmouse *psmouse)
1728cd401204SKevin Cernekee {
1729cd401204SKevin Cernekee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
1730cd401204SKevin Cernekee 	int reg_val;
1731cd401204SKevin Cernekee 	unsigned char param[4];
1732cd401204SKevin Cernekee 
1733cd401204SKevin Cernekee 	reg_val = alps_probe_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE);
1734cd401204SKevin Cernekee 	if (reg_val == -EIO)
1735cd401204SKevin Cernekee 		goto error;
173639fbe585SDmitry Torokhov 
1737cd401204SKevin Cernekee 	if (reg_val == 0 &&
1738cd401204SKevin Cernekee 	    alps_setup_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE) == -EIO)
1739cd401204SKevin Cernekee 		goto error;
1740cd401204SKevin Cernekee 
1741d18e53fcSKevin Cernekee 	if (alps_enter_command_mode(psmouse) ||
1742cd401204SKevin Cernekee 	    alps_absolute_mode_v3(psmouse)) {
174325bded7cSSeth Forshee 		psmouse_err(psmouse, "Failed to enter absolute mode\n");
174425bded7cSSeth Forshee 		goto error;
174525bded7cSSeth Forshee 	}
174625bded7cSSeth Forshee 
174725bded7cSSeth Forshee 	reg_val = alps_command_mode_read_reg(psmouse, 0x0006);
174825bded7cSSeth Forshee 	if (reg_val == -1)
174925bded7cSSeth Forshee 		goto error;
175025bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
175125bded7cSSeth Forshee 		goto error;
175225bded7cSSeth Forshee 
175325bded7cSSeth Forshee 	reg_val = alps_command_mode_read_reg(psmouse, 0x0007);
175425bded7cSSeth Forshee 	if (reg_val == -1)
175525bded7cSSeth Forshee 		goto error;
175625bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
175725bded7cSSeth Forshee 		goto error;
175825bded7cSSeth Forshee 
175925bded7cSSeth Forshee 	if (alps_command_mode_read_reg(psmouse, 0x0144) == -1)
176025bded7cSSeth Forshee 		goto error;
176125bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, 0x04))
176225bded7cSSeth Forshee 		goto error;
176325bded7cSSeth Forshee 
176425bded7cSSeth Forshee 	if (alps_command_mode_read_reg(psmouse, 0x0159) == -1)
176525bded7cSSeth Forshee 		goto error;
176625bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, 0x03))
176725bded7cSSeth Forshee 		goto error;
176825bded7cSSeth Forshee 
176925bded7cSSeth Forshee 	if (alps_command_mode_read_reg(psmouse, 0x0163) == -1)
177025bded7cSSeth Forshee 		goto error;
177125bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0163, 0x03))
177225bded7cSSeth Forshee 		goto error;
177325bded7cSSeth Forshee 
177425bded7cSSeth Forshee 	if (alps_command_mode_read_reg(psmouse, 0x0162) == -1)
177525bded7cSSeth Forshee 		goto error;
177625bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0162, 0x04))
177725bded7cSSeth Forshee 		goto error;
177825bded7cSSeth Forshee 
177925bded7cSSeth Forshee 	alps_exit_command_mode(psmouse);
178025bded7cSSeth Forshee 
178125bded7cSSeth Forshee 	/* Set rate and enable data reporting */
178225bded7cSSeth Forshee 	param[0] = 0x64;
178325bded7cSSeth Forshee 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
178425bded7cSSeth Forshee 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
178525bded7cSSeth Forshee 		psmouse_err(psmouse, "Failed to enable data reporting\n");
178625bded7cSSeth Forshee 		return -1;
178725bded7cSSeth Forshee 	}
178825bded7cSSeth Forshee 
178925bded7cSSeth Forshee 	return 0;
179025bded7cSSeth Forshee 
179125bded7cSSeth Forshee error:
179225bded7cSSeth Forshee 	/*
179325bded7cSSeth Forshee 	 * Leaving the touchpad in command mode will essentially render
179425bded7cSSeth Forshee 	 * it unusable until the machine reboots, so exit it here just
179525bded7cSSeth Forshee 	 * to be safe
179625bded7cSSeth Forshee 	 */
179725bded7cSSeth Forshee 	alps_exit_command_mode(psmouse);
179825bded7cSSeth Forshee 	return -1;
179925bded7cSSeth Forshee }
180025bded7cSSeth Forshee 
1801f3f33c67SHans de Goede static int alps_get_v3_v7_resolution(struct psmouse *psmouse, int reg_pitch)
1802f3f33c67SHans de Goede {
1803f3f33c67SHans de Goede 	int reg, x_pitch, y_pitch, x_electrode, y_electrode, x_phys, y_phys;
1804f3f33c67SHans de Goede 	struct alps_data *priv = psmouse->private;
1805f3f33c67SHans de Goede 
1806f3f33c67SHans de Goede 	reg = alps_command_mode_read_reg(psmouse, reg_pitch);
1807f3f33c67SHans de Goede 	if (reg < 0)
1808f3f33c67SHans de Goede 		return reg;
1809f3f33c67SHans de Goede 
1810f3f33c67SHans de Goede 	x_pitch = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
1811f3f33c67SHans de Goede 	x_pitch = 50 + 2 * x_pitch; /* In 0.1 mm units */
1812f3f33c67SHans de Goede 
1813f3f33c67SHans de Goede 	y_pitch = (char)reg >> 4; /* sign extend upper 4 bits */
1814f3f33c67SHans de Goede 	y_pitch = 36 + 2 * y_pitch; /* In 0.1 mm units */
1815f3f33c67SHans de Goede 
1816f3f33c67SHans de Goede 	reg = alps_command_mode_read_reg(psmouse, reg_pitch + 1);
1817f3f33c67SHans de Goede 	if (reg < 0)
1818f3f33c67SHans de Goede 		return reg;
1819f3f33c67SHans de Goede 
1820f3f33c67SHans de Goede 	x_electrode = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
1821f3f33c67SHans de Goede 	x_electrode = 17 + x_electrode;
1822f3f33c67SHans de Goede 
1823f3f33c67SHans de Goede 	y_electrode = (char)reg >> 4; /* sign extend upper 4 bits */
1824f3f33c67SHans de Goede 	y_electrode = 13 + y_electrode;
1825f3f33c67SHans de Goede 
1826f3f33c67SHans de Goede 	x_phys = x_pitch * (x_electrode - 1); /* In 0.1 mm units */
1827f3f33c67SHans de Goede 	y_phys = y_pitch * (y_electrode - 1); /* In 0.1 mm units */
1828f3f33c67SHans de Goede 
1829f3f33c67SHans de Goede 	priv->x_res = priv->x_max * 10 / x_phys; /* units / mm */
1830f3f33c67SHans de Goede 	priv->y_res = priv->y_max * 10 / y_phys; /* units / mm */
1831f3f33c67SHans de Goede 
1832f3f33c67SHans de Goede 	psmouse_dbg(psmouse,
1833f3f33c67SHans de Goede 		    "pitch %dx%d num-electrodes %dx%d physical size %dx%d mm res %dx%d\n",
1834f3f33c67SHans de Goede 		    x_pitch, y_pitch, x_electrode, y_electrode,
1835f3f33c67SHans de Goede 		    x_phys / 10, y_phys / 10, priv->x_res, priv->y_res);
1836f3f33c67SHans de Goede 
1837f3f33c67SHans de Goede 	return 0;
1838f3f33c67SHans de Goede }
1839f3f33c67SHans de Goede 
18401302bac3SKevin Cernekee static int alps_hw_init_rushmore_v3(struct psmouse *psmouse)
18411302bac3SKevin Cernekee {
1842cd401204SKevin Cernekee 	struct alps_data *priv = psmouse->private;
18431302bac3SKevin Cernekee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
18441302bac3SKevin Cernekee 	int reg_val, ret = -1;
18451302bac3SKevin Cernekee 
1846cd401204SKevin Cernekee 	if (priv->flags & ALPS_DUALPOINT) {
1847cd401204SKevin Cernekee 		reg_val = alps_setup_trackstick_v3(psmouse,
1848cd401204SKevin Cernekee 						   ALPS_REG_BASE_RUSHMORE);
1849cd401204SKevin Cernekee 		if (reg_val == -EIO)
1850cd401204SKevin Cernekee 			goto error;
1851cd401204SKevin Cernekee 		if (reg_val == -ENODEV)
1852cd401204SKevin Cernekee 			priv->flags &= ~ALPS_DUALPOINT;
1853cd401204SKevin Cernekee 	}
1854cd401204SKevin Cernekee 
1855d18e53fcSKevin Cernekee 	if (alps_enter_command_mode(psmouse) ||
18561302bac3SKevin Cernekee 	    alps_command_mode_read_reg(psmouse, 0xc2d9) == -1 ||
18571302bac3SKevin Cernekee 	    alps_command_mode_write_reg(psmouse, 0xc2cb, 0x00))
18581302bac3SKevin Cernekee 		goto error;
18591302bac3SKevin Cernekee 
1860f3f33c67SHans de Goede 	if (alps_get_v3_v7_resolution(psmouse, 0xc2da))
1861f3f33c67SHans de Goede 		goto error;
1862f3f33c67SHans de Goede 
18631302bac3SKevin Cernekee 	reg_val = alps_command_mode_read_reg(psmouse, 0xc2c6);
18641302bac3SKevin Cernekee 	if (reg_val == -1)
18651302bac3SKevin Cernekee 		goto error;
18661302bac3SKevin Cernekee 	if (__alps_command_mode_write_reg(psmouse, reg_val & 0xfd))
18671302bac3SKevin Cernekee 		goto error;
18681302bac3SKevin Cernekee 
18691302bac3SKevin Cernekee 	if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
18701302bac3SKevin Cernekee 		goto error;
18711302bac3SKevin Cernekee 
18721302bac3SKevin Cernekee 	/* enter absolute mode */
18731302bac3SKevin Cernekee 	reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
18741302bac3SKevin Cernekee 	if (reg_val == -1)
18751302bac3SKevin Cernekee 		goto error;
18761302bac3SKevin Cernekee 	if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
18771302bac3SKevin Cernekee 		goto error;
18781302bac3SKevin Cernekee 
18791302bac3SKevin Cernekee 	alps_exit_command_mode(psmouse);
18801302bac3SKevin Cernekee 	return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
18811302bac3SKevin Cernekee 
18821302bac3SKevin Cernekee error:
18831302bac3SKevin Cernekee 	alps_exit_command_mode(psmouse);
18841302bac3SKevin Cernekee 	return ret;
18851302bac3SKevin Cernekee }
18861302bac3SKevin Cernekee 
188725bded7cSSeth Forshee /* Must be in command mode when calling this function */
188825bded7cSSeth Forshee static int alps_absolute_mode_v4(struct psmouse *psmouse)
188925bded7cSSeth Forshee {
189025bded7cSSeth Forshee 	int reg_val;
189125bded7cSSeth Forshee 
189225bded7cSSeth Forshee 	reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
189325bded7cSSeth Forshee 	if (reg_val == -1)
189425bded7cSSeth Forshee 		return -1;
189525bded7cSSeth Forshee 
189625bded7cSSeth Forshee 	reg_val |= 0x02;
189725bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, reg_val))
189825bded7cSSeth Forshee 		return -1;
189925bded7cSSeth Forshee 
190025bded7cSSeth Forshee 	return 0;
190125bded7cSSeth Forshee }
190225bded7cSSeth Forshee 
190325bded7cSSeth Forshee static int alps_hw_init_v4(struct psmouse *psmouse)
190425bded7cSSeth Forshee {
190525bded7cSSeth Forshee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
190625bded7cSSeth Forshee 	unsigned char param[4];
190725bded7cSSeth Forshee 
1908d18e53fcSKevin Cernekee 	if (alps_enter_command_mode(psmouse))
190925bded7cSSeth Forshee 		goto error;
191025bded7cSSeth Forshee 
191125bded7cSSeth Forshee 	if (alps_absolute_mode_v4(psmouse)) {
191225bded7cSSeth Forshee 		psmouse_err(psmouse, "Failed to enter absolute mode\n");
191325bded7cSSeth Forshee 		goto error;
191425bded7cSSeth Forshee 	}
191525bded7cSSeth Forshee 
191625bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0007, 0x8c))
191725bded7cSSeth Forshee 		goto error;
191825bded7cSSeth Forshee 
191925bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0149, 0x03))
192025bded7cSSeth Forshee 		goto error;
192125bded7cSSeth Forshee 
192225bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0160, 0x03))
192325bded7cSSeth Forshee 		goto error;
192425bded7cSSeth Forshee 
192525bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x017f, 0x15))
192625bded7cSSeth Forshee 		goto error;
192725bded7cSSeth Forshee 
192825bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0151, 0x01))
192925bded7cSSeth Forshee 		goto error;
193025bded7cSSeth Forshee 
193125bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0168, 0x03))
193225bded7cSSeth Forshee 		goto error;
193325bded7cSSeth Forshee 
193425bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x014a, 0x03))
193525bded7cSSeth Forshee 		goto error;
193625bded7cSSeth Forshee 
193725bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0161, 0x03))
193825bded7cSSeth Forshee 		goto error;
193925bded7cSSeth Forshee 
194025bded7cSSeth Forshee 	alps_exit_command_mode(psmouse);
194125bded7cSSeth Forshee 
194225bded7cSSeth Forshee 	/*
194325bded7cSSeth Forshee 	 * This sequence changes the output from a 9-byte to an
194425bded7cSSeth Forshee 	 * 8-byte format. All the same data seems to be present,
194525bded7cSSeth Forshee 	 * just in a more compact format.
194625bded7cSSeth Forshee 	 */
194725bded7cSSeth Forshee 	param[0] = 0xc8;
194825bded7cSSeth Forshee 	param[1] = 0x64;
194925bded7cSSeth Forshee 	param[2] = 0x50;
195025bded7cSSeth Forshee 	if (ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
195125bded7cSSeth Forshee 	    ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE) ||
195225bded7cSSeth Forshee 	    ps2_command(ps2dev, &param[2], PSMOUSE_CMD_SETRATE) ||
195325bded7cSSeth Forshee 	    ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
195425bded7cSSeth Forshee 		return -1;
195525bded7cSSeth Forshee 
195625bded7cSSeth Forshee 	/* Set rate and enable data reporting */
195725bded7cSSeth Forshee 	param[0] = 0x64;
195825bded7cSSeth Forshee 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
195925bded7cSSeth Forshee 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
196025bded7cSSeth Forshee 		psmouse_err(psmouse, "Failed to enable data reporting\n");
196125bded7cSSeth Forshee 		return -1;
196225bded7cSSeth Forshee 	}
196325bded7cSSeth Forshee 
196425bded7cSSeth Forshee 	return 0;
196525bded7cSSeth Forshee 
196625bded7cSSeth Forshee error:
196725bded7cSSeth Forshee 	/*
196825bded7cSSeth Forshee 	 * Leaving the touchpad in command mode will essentially render
196925bded7cSSeth Forshee 	 * it unusable until the machine reboots, so exit it here just
197025bded7cSSeth Forshee 	 * to be safe
197125bded7cSSeth Forshee 	 */
197225bded7cSSeth Forshee 	alps_exit_command_mode(psmouse);
197325bded7cSSeth Forshee 	return -1;
197425bded7cSSeth Forshee }
197525bded7cSSeth Forshee 
1976ee65d4b3SYunkang Tang static int alps_dolphin_get_device_area(struct psmouse *psmouse,
1977ee65d4b3SYunkang Tang 					struct alps_data *priv)
1978ee65d4b3SYunkang Tang {
1979ee65d4b3SYunkang Tang 	struct ps2dev *ps2dev = &psmouse->ps2dev;
1980ee65d4b3SYunkang Tang 	unsigned char param[4] = {0};
1981ee65d4b3SYunkang Tang 	int num_x_electrode, num_y_electrode;
1982ee65d4b3SYunkang Tang 
1983ee65d4b3SYunkang Tang 	if (alps_enter_command_mode(psmouse))
1984ee65d4b3SYunkang Tang 		return -1;
1985ee65d4b3SYunkang Tang 
1986ee65d4b3SYunkang Tang 	param[0] = 0x0a;
1987ee65d4b3SYunkang Tang 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
1988ee65d4b3SYunkang Tang 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
1989ee65d4b3SYunkang Tang 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
1990ee65d4b3SYunkang Tang 	    ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
1991ee65d4b3SYunkang Tang 	    ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE))
1992ee65d4b3SYunkang Tang 		return -1;
1993ee65d4b3SYunkang Tang 
1994ee65d4b3SYunkang Tang 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1995ee65d4b3SYunkang Tang 		return -1;
1996ee65d4b3SYunkang Tang 
1997ee65d4b3SYunkang Tang 	/*
1998ee65d4b3SYunkang Tang 	 * Dolphin's sensor line number is not fixed. It can be calculated
1999ee65d4b3SYunkang Tang 	 * by adding the device's register value with DOLPHIN_PROFILE_X/YOFFSET.
2000ee65d4b3SYunkang Tang 	 * Further more, we can get device's x_max and y_max by multiplying
2001ee65d4b3SYunkang Tang 	 * sensor line number with DOLPHIN_COUNT_PER_ELECTRODE.
2002ee65d4b3SYunkang Tang 	 *
2003ee65d4b3SYunkang Tang 	 * e.g. When we get register's sensor_x = 11 & sensor_y = 8,
2004ee65d4b3SYunkang Tang 	 *	real sensor line number X = 11 + 8 = 19, and
2005ee65d4b3SYunkang Tang 	 *	real sensor line number Y = 8 + 1 = 9.
2006ee65d4b3SYunkang Tang 	 *	So, x_max = (19 - 1) * 64 = 1152, and
2007ee65d4b3SYunkang Tang 	 *	    y_max = (9 - 1) * 64 = 512.
2008ee65d4b3SYunkang Tang 	 */
2009ee65d4b3SYunkang Tang 	num_x_electrode = DOLPHIN_PROFILE_XOFFSET + (param[2] & 0x0F);
2010ee65d4b3SYunkang Tang 	num_y_electrode = DOLPHIN_PROFILE_YOFFSET + ((param[2] >> 4) & 0x0F);
2011ee65d4b3SYunkang Tang 	priv->x_bits = num_x_electrode;
2012ee65d4b3SYunkang Tang 	priv->y_bits = num_y_electrode;
2013ee65d4b3SYunkang Tang 	priv->x_max = (num_x_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
2014ee65d4b3SYunkang Tang 	priv->y_max = (num_y_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
2015ee65d4b3SYunkang Tang 
2016ee65d4b3SYunkang Tang 	if (alps_exit_command_mode(psmouse))
2017ee65d4b3SYunkang Tang 		return -1;
2018ee65d4b3SYunkang Tang 
2019ee65d4b3SYunkang Tang 	return 0;
2020ee65d4b3SYunkang Tang }
2021ee65d4b3SYunkang Tang 
202275af9e56SDave Turvene static int alps_hw_init_dolphin_v1(struct psmouse *psmouse)
202375af9e56SDave Turvene {
202475af9e56SDave Turvene 	struct ps2dev *ps2dev = &psmouse->ps2dev;
202575af9e56SDave Turvene 	unsigned char param[2];
202675af9e56SDave Turvene 
202775af9e56SDave Turvene 	/* This is dolphin "v1" as empirically defined by florin9doi */
202875af9e56SDave Turvene 	param[0] = 0x64;
202975af9e56SDave Turvene 	param[1] = 0x28;
203075af9e56SDave Turvene 
203175af9e56SDave Turvene 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
203275af9e56SDave Turvene 	    ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
203375af9e56SDave Turvene 	    ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
203475af9e56SDave Turvene 		return -1;
203575af9e56SDave Turvene 
203675af9e56SDave Turvene 	return 0;
203775af9e56SDave Turvene }
203875af9e56SDave Turvene 
20393808843cSYunkang Tang static int alps_hw_init_v7(struct psmouse *psmouse)
20403808843cSYunkang Tang {
20413808843cSYunkang Tang 	struct ps2dev *ps2dev = &psmouse->ps2dev;
20423808843cSYunkang Tang 	int reg_val, ret = -1;
20433808843cSYunkang Tang 
20443808843cSYunkang Tang 	if (alps_enter_command_mode(psmouse) ||
20453808843cSYunkang Tang 	    alps_command_mode_read_reg(psmouse, 0xc2d9) == -1)
20463808843cSYunkang Tang 		goto error;
20473808843cSYunkang Tang 
2048f3f33c67SHans de Goede 	if (alps_get_v3_v7_resolution(psmouse, 0xc397))
2049f3f33c67SHans de Goede 		goto error;
2050f3f33c67SHans de Goede 
20513808843cSYunkang Tang 	if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
20523808843cSYunkang Tang 		goto error;
20533808843cSYunkang Tang 
20543808843cSYunkang Tang 	reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
20553808843cSYunkang Tang 	if (reg_val == -1)
20563808843cSYunkang Tang 		goto error;
20573808843cSYunkang Tang 	if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
20583808843cSYunkang Tang 		goto error;
20593808843cSYunkang Tang 
20603808843cSYunkang Tang 	alps_exit_command_mode(psmouse);
20613808843cSYunkang Tang 	return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
20623808843cSYunkang Tang 
20633808843cSYunkang Tang error:
20643808843cSYunkang Tang 	alps_exit_command_mode(psmouse);
20653808843cSYunkang Tang 	return ret;
20663808843cSYunkang Tang }
20673808843cSYunkang Tang 
206824af5cb9SKevin Cernekee static void alps_set_defaults(struct alps_data *priv)
206925bded7cSSeth Forshee {
2070f673ceb1SKevin Cernekee 	priv->byte0 = 0x8f;
2071f673ceb1SKevin Cernekee 	priv->mask0 = 0x8f;
2072f673ceb1SKevin Cernekee 	priv->flags = ALPS_DUALPOINT;
2073f673ceb1SKevin Cernekee 
20747a9f73e7SKevin Cernekee 	priv->x_max = 2000;
20757a9f73e7SKevin Cernekee 	priv->y_max = 1400;
20767a9f73e7SKevin Cernekee 	priv->x_bits = 15;
20777a9f73e7SKevin Cernekee 	priv->y_bits = 11;
20787a9f73e7SKevin Cernekee 
207999df65e7SKevin Cernekee 	switch (priv->proto_version) {
208025bded7cSSeth Forshee 	case ALPS_PROTO_V1:
208125bded7cSSeth Forshee 	case ALPS_PROTO_V2:
208224af5cb9SKevin Cernekee 		priv->hw_init = alps_hw_init_v1_v2;
208324af5cb9SKevin Cernekee 		priv->process_packet = alps_process_packet_v1_v2;
208424af5cb9SKevin Cernekee 		priv->set_abs_params = alps_set_abs_params_st;
208595f75e91SYunkang Tang 		priv->x_max = 1023;
208695f75e91SYunkang Tang 		priv->y_max = 767;
208725bded7cSSeth Forshee 		break;
208825bded7cSSeth Forshee 	case ALPS_PROTO_V3:
208924af5cb9SKevin Cernekee 		priv->hw_init = alps_hw_init_v3;
209024af5cb9SKevin Cernekee 		priv->process_packet = alps_process_packet_v3;
209124af5cb9SKevin Cernekee 		priv->set_abs_params = alps_set_abs_params_mt;
2092f85e5001SKevin Cernekee 		priv->decode_fields = alps_decode_pinnacle;
209350e8b216SKevin Cernekee 		priv->nibble_commands = alps_v3_nibble_commands;
209450e8b216SKevin Cernekee 		priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
209525bded7cSSeth Forshee 		break;
209625bded7cSSeth Forshee 	case ALPS_PROTO_V4:
209724af5cb9SKevin Cernekee 		priv->hw_init = alps_hw_init_v4;
209824af5cb9SKevin Cernekee 		priv->process_packet = alps_process_packet_v4;
209924af5cb9SKevin Cernekee 		priv->set_abs_params = alps_set_abs_params_mt;
210050e8b216SKevin Cernekee 		priv->nibble_commands = alps_v4_nibble_commands;
210150e8b216SKevin Cernekee 		priv->addr_command = PSMOUSE_CMD_DISABLE;
210225bded7cSSeth Forshee 		break;
210375af9e56SDave Turvene 	case ALPS_PROTO_V5:
210475af9e56SDave Turvene 		priv->hw_init = alps_hw_init_dolphin_v1;
2105ee65d4b3SYunkang Tang 		priv->process_packet = alps_process_touchpad_packet_v3_v5;
210675af9e56SDave Turvene 		priv->decode_fields = alps_decode_dolphin;
210775af9e56SDave Turvene 		priv->set_abs_params = alps_set_abs_params_mt;
210875af9e56SDave Turvene 		priv->nibble_commands = alps_v3_nibble_commands;
210975af9e56SDave Turvene 		priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
211075af9e56SDave Turvene 		priv->byte0 = 0xc8;
2111ee65d4b3SYunkang Tang 		priv->mask0 = 0xd8;
211275af9e56SDave Turvene 		priv->flags = 0;
211375af9e56SDave Turvene 		priv->x_max = 1360;
211475af9e56SDave Turvene 		priv->y_max = 660;
211575af9e56SDave Turvene 		priv->x_bits = 23;
211675af9e56SDave Turvene 		priv->y_bits = 12;
211775af9e56SDave Turvene 		break;
211895f75e91SYunkang Tang 	case ALPS_PROTO_V6:
211995f75e91SYunkang Tang 		priv->hw_init = alps_hw_init_v6;
212095f75e91SYunkang Tang 		priv->process_packet = alps_process_packet_v6;
212195f75e91SYunkang Tang 		priv->set_abs_params = alps_set_abs_params_st;
212295f75e91SYunkang Tang 		priv->nibble_commands = alps_v6_nibble_commands;
212395f75e91SYunkang Tang 		priv->x_max = 2047;
212495f75e91SYunkang Tang 		priv->y_max = 1535;
212595f75e91SYunkang Tang 		break;
21263808843cSYunkang Tang 	case ALPS_PROTO_V7:
21273808843cSYunkang Tang 		priv->hw_init = alps_hw_init_v7;
21283808843cSYunkang Tang 		priv->process_packet = alps_process_packet_v7;
21293808843cSYunkang Tang 		priv->decode_fields = alps_decode_packet_v7;
21303808843cSYunkang Tang 		priv->set_abs_params = alps_set_abs_params_mt;
21313808843cSYunkang Tang 		priv->nibble_commands = alps_v3_nibble_commands;
21323808843cSYunkang Tang 		priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
21333808843cSYunkang Tang 		priv->x_max = 0xfff;
21343808843cSYunkang Tang 		priv->y_max = 0x7ff;
21353808843cSYunkang Tang 		priv->byte0 = 0x48;
21363808843cSYunkang Tang 		priv->mask0 = 0x48;
21373808843cSYunkang Tang 
21383808843cSYunkang Tang 		if (priv->fw_ver[1] != 0xba)
21393808843cSYunkang Tang 			priv->flags |= ALPS_BUTTONPAD;
21403808843cSYunkang Tang 		break;
214125bded7cSSeth Forshee 	}
214225bded7cSSeth Forshee }
214325bded7cSSeth Forshee 
2144b5d6b851SKevin Cernekee static int alps_match_table(struct psmouse *psmouse, struct alps_data *priv,
2145b5d6b851SKevin Cernekee 			    unsigned char *e7, unsigned char *ec)
21462e992cc0SKevin Cernekee {
2147b5d6b851SKevin Cernekee 	const struct alps_model_info *model;
21482e992cc0SKevin Cernekee 	int i;
21492e992cc0SKevin Cernekee 
2150b5d6b851SKevin Cernekee 	for (i = 0; i < ARRAY_SIZE(alps_model_data); i++) {
2151b5d6b851SKevin Cernekee 		model = &alps_model_data[i];
2152b5d6b851SKevin Cernekee 
2153b5d6b851SKevin Cernekee 		if (!memcmp(e7, model->signature, sizeof(model->signature)) &&
2154b5d6b851SKevin Cernekee 		    (!model->command_mode_resp ||
2155b5d6b851SKevin Cernekee 		     model->command_mode_resp == ec[2])) {
2156b5d6b851SKevin Cernekee 
2157b5d6b851SKevin Cernekee 			priv->proto_version = model->proto_version;
215824af5cb9SKevin Cernekee 			alps_set_defaults(priv);
215924af5cb9SKevin Cernekee 
2160b5d6b851SKevin Cernekee 			priv->flags = model->flags;
2161b5d6b851SKevin Cernekee 			priv->byte0 = model->byte0;
2162b5d6b851SKevin Cernekee 			priv->mask0 = model->mask0;
2163b5d6b851SKevin Cernekee 
2164b5d6b851SKevin Cernekee 			return 0;
2165b5d6b851SKevin Cernekee 		}
2166b5d6b851SKevin Cernekee 	}
2167b5d6b851SKevin Cernekee 
2168b5d6b851SKevin Cernekee 	return -EINVAL;
2169b5d6b851SKevin Cernekee }
2170b5d6b851SKevin Cernekee 
2171b5d6b851SKevin Cernekee static int alps_identify(struct psmouse *psmouse, struct alps_data *priv)
2172b5d6b851SKevin Cernekee {
2173b5d6b851SKevin Cernekee 	unsigned char e6[4], e7[4], ec[4];
2174b5d6b851SKevin Cernekee 
21752e992cc0SKevin Cernekee 	/*
21762e992cc0SKevin Cernekee 	 * First try "E6 report".
21772e992cc0SKevin Cernekee 	 * ALPS should return 0,0,10 or 0,0,100 if no buttons are pressed.
21782e992cc0SKevin Cernekee 	 * The bits 0-2 of the first byte will be 1s if some buttons are
21792e992cc0SKevin Cernekee 	 * pressed.
21802e992cc0SKevin Cernekee 	 */
2181b5d6b851SKevin Cernekee 	if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2182b5d6b851SKevin Cernekee 			 PSMOUSE_CMD_SETSCALE11, e6))
2183b5d6b851SKevin Cernekee 		return -EIO;
21842e992cc0SKevin Cernekee 
2185b5d6b851SKevin Cernekee 	if ((e6[0] & 0xf8) != 0 || e6[1] != 0 || (e6[2] != 10 && e6[2] != 100))
2186b5d6b851SKevin Cernekee 		return -EINVAL;
21872e992cc0SKevin Cernekee 
21882e992cc0SKevin Cernekee 	/*
2189b5d6b851SKevin Cernekee 	 * Now get the "E7" and "EC" reports.  These will uniquely identify
2190b5d6b851SKevin Cernekee 	 * most ALPS touchpads.
21912e992cc0SKevin Cernekee 	 */
2192b5d6b851SKevin Cernekee 	if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2193b5d6b851SKevin Cernekee 			 PSMOUSE_CMD_SETSCALE21, e7) ||
2194b5d6b851SKevin Cernekee 	    alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2195b5d6b851SKevin Cernekee 			 PSMOUSE_CMD_RESET_WRAP, ec) ||
2196b5d6b851SKevin Cernekee 	    alps_exit_command_mode(psmouse))
2197b5d6b851SKevin Cernekee 		return -EIO;
21982e992cc0SKevin Cernekee 
2199c0cd17f6SHans de Goede 	/* Save the Firmware version */
2200c0cd17f6SHans de Goede 	memcpy(priv->fw_ver, ec, 3);
2201c0cd17f6SHans de Goede 
2202f673ceb1SKevin Cernekee 	if (alps_match_table(psmouse, priv, e7, ec) == 0) {
2203b5d6b851SKevin Cernekee 		return 0;
220475af9e56SDave Turvene 	} else if (e7[0] == 0x73 && e7[1] == 0x03 && e7[2] == 0x50 &&
2205ee65d4b3SYunkang Tang 		   ec[0] == 0x73 && (ec[1] == 0x01 || ec[1] == 0x02)) {
220675af9e56SDave Turvene 		priv->proto_version = ALPS_PROTO_V5;
220775af9e56SDave Turvene 		alps_set_defaults(priv);
2208ee65d4b3SYunkang Tang 		if (alps_dolphin_get_device_area(psmouse, priv))
2209ee65d4b3SYunkang Tang 			return -EIO;
2210ee65d4b3SYunkang Tang 		else
221175af9e56SDave Turvene 			return 0;
22123808843cSYunkang Tang 	} else if (ec[0] == 0x88 &&
22133808843cSYunkang Tang 		   ((ec[1] & 0xf0) == 0xb0 || (ec[1] & 0xf0) == 0xc0)) {
22143808843cSYunkang Tang 		priv->proto_version = ALPS_PROTO_V7;
22153808843cSYunkang Tang 		alps_set_defaults(priv);
22163808843cSYunkang Tang 
22173808843cSYunkang Tang 		return 0;
22181302bac3SKevin Cernekee 	} else if (ec[0] == 0x88 && ec[1] == 0x08) {
22191302bac3SKevin Cernekee 		priv->proto_version = ALPS_PROTO_V3;
22201302bac3SKevin Cernekee 		alps_set_defaults(priv);
22211302bac3SKevin Cernekee 
22221302bac3SKevin Cernekee 		priv->hw_init = alps_hw_init_rushmore_v3;
22231302bac3SKevin Cernekee 		priv->decode_fields = alps_decode_rushmore;
22241302bac3SKevin Cernekee 		priv->x_bits = 16;
22251302bac3SKevin Cernekee 		priv->y_bits = 12;
222640e8f53bSHans de Goede 		priv->flags |= ALPS_IS_RUSHMORE;
22271302bac3SKevin Cernekee 
2228cd401204SKevin Cernekee 		/* hack to make addr_command, nibble_command available */
2229cd401204SKevin Cernekee 		psmouse->private = priv;
2230cd401204SKevin Cernekee 
2231cd401204SKevin Cernekee 		if (alps_probe_trackstick_v3(psmouse, ALPS_REG_BASE_RUSHMORE))
2232cd401204SKevin Cernekee 			priv->flags &= ~ALPS_DUALPOINT;
2233cd401204SKevin Cernekee 
22341302bac3SKevin Cernekee 		return 0;
2235f673ceb1SKevin Cernekee 	} else if (ec[0] == 0x88 && ec[1] == 0x07 &&
2236f673ceb1SKevin Cernekee 		   ec[2] >= 0x90 && ec[2] <= 0x9d) {
2237f673ceb1SKevin Cernekee 		priv->proto_version = ALPS_PROTO_V3;
2238f673ceb1SKevin Cernekee 		alps_set_defaults(priv);
2239f673ceb1SKevin Cernekee 
2240f673ceb1SKevin Cernekee 		return 0;
2241f673ceb1SKevin Cernekee 	}
22422e992cc0SKevin Cernekee 
2243a9e06219SDmitry Torokhov 	psmouse_dbg(psmouse,
2244a9e06219SDmitry Torokhov 		    "Likely not an ALPS touchpad: E7=%3ph, EC=%3ph\n", e7, ec);
22452e992cc0SKevin Cernekee 
2246b5d6b851SKevin Cernekee 	return -EINVAL;
22472e992cc0SKevin Cernekee }
22482e992cc0SKevin Cernekee 
22491e0c5b12SDmitry Torokhov static int alps_reconnect(struct psmouse *psmouse)
22501e0c5b12SDmitry Torokhov {
2251b5d6b851SKevin Cernekee 	struct alps_data *priv = psmouse->private;
225271bb21b6SMaxim Levitsky 
22531e0c5b12SDmitry Torokhov 	psmouse_reset(psmouse);
22541e0c5b12SDmitry Torokhov 
2255b5d6b851SKevin Cernekee 	if (alps_identify(psmouse, priv) < 0)
22561e0c5b12SDmitry Torokhov 		return -1;
22571e0c5b12SDmitry Torokhov 
225824af5cb9SKevin Cernekee 	return priv->hw_init(psmouse);
22591da177e4SLinus Torvalds }
22601da177e4SLinus Torvalds 
22611da177e4SLinus Torvalds static void alps_disconnect(struct psmouse *psmouse)
22621da177e4SLinus Torvalds {
22631da177e4SLinus Torvalds 	struct alps_data *priv = psmouse->private;
22642e5b636bSDmitry Torokhov 
22651da177e4SLinus Torvalds 	psmouse_reset(psmouse);
22661d9f2626SSebastian Kapfer 	del_timer_sync(&priv->timer);
22672e5b636bSDmitry Torokhov 	input_unregister_device(priv->dev2);
22681da177e4SLinus Torvalds 	kfree(priv);
22691da177e4SLinus Torvalds }
22701da177e4SLinus Torvalds 
227124af5cb9SKevin Cernekee static void alps_set_abs_params_st(struct alps_data *priv,
227224af5cb9SKevin Cernekee 				   struct input_dev *dev1)
227324af5cb9SKevin Cernekee {
227495f75e91SYunkang Tang 	input_set_abs_params(dev1, ABS_X, 0, priv->x_max, 0, 0);
227595f75e91SYunkang Tang 	input_set_abs_params(dev1, ABS_Y, 0, priv->y_max, 0, 0);
227624af5cb9SKevin Cernekee }
227724af5cb9SKevin Cernekee 
227824af5cb9SKevin Cernekee static void alps_set_abs_params_mt(struct alps_data *priv,
227924af5cb9SKevin Cernekee 				   struct input_dev *dev1)
228024af5cb9SKevin Cernekee {
22817a9f73e7SKevin Cernekee 	input_set_abs_params(dev1, ABS_MT_POSITION_X, 0, priv->x_max, 0, 0);
22827a9f73e7SKevin Cernekee 	input_set_abs_params(dev1, ABS_MT_POSITION_Y, 0, priv->y_max, 0, 0);
228324af5cb9SKevin Cernekee 
2284f3f33c67SHans de Goede 	input_abs_set_res(dev1, ABS_MT_POSITION_X, priv->x_res);
2285f3f33c67SHans de Goede 	input_abs_set_res(dev1, ABS_MT_POSITION_Y, priv->y_res);
2286f3f33c67SHans de Goede 
2287cdf333efSHans de Goede 	input_mt_init_slots(dev1, MAX_TOUCHES, INPUT_MT_POINTER |
2288cdf333efSHans de Goede 		INPUT_MT_DROP_UNUSED | INPUT_MT_TRACK | INPUT_MT_SEMI_MT);
2289cdf333efSHans de Goede 
229024af5cb9SKevin Cernekee 	set_bit(BTN_TOOL_TRIPLETAP, dev1->keybit);
229124af5cb9SKevin Cernekee 	set_bit(BTN_TOOL_QUADTAP, dev1->keybit);
22923808843cSYunkang Tang 
22933808843cSYunkang Tang 	/* V7 is real multi-touch */
22943808843cSYunkang Tang 	if (priv->proto_version == ALPS_PROTO_V7)
22953808843cSYunkang Tang 		clear_bit(INPUT_PROP_SEMI_MT, dev1->propbit);
229624af5cb9SKevin Cernekee }
229724af5cb9SKevin Cernekee 
22981da177e4SLinus Torvalds int alps_init(struct psmouse *psmouse)
22991da177e4SLinus Torvalds {
23001da177e4SLinus Torvalds 	struct alps_data *priv;
23012e5b636bSDmitry Torokhov 	struct input_dev *dev1 = psmouse->dev, *dev2;
23021da177e4SLinus Torvalds 
2303f42649e8SDmitry Torokhov 	priv = kzalloc(sizeof(struct alps_data), GFP_KERNEL);
23042e5b636bSDmitry Torokhov 	dev2 = input_allocate_device();
23052e5b636bSDmitry Torokhov 	if (!priv || !dev2)
23061da177e4SLinus Torvalds 		goto init_fail;
23072e5b636bSDmitry Torokhov 
23082e5b636bSDmitry Torokhov 	priv->dev2 = dev2;
23091d9f2626SSebastian Kapfer 	setup_timer(&priv->timer, alps_flush_packet, (unsigned long)psmouse);
23101d9f2626SSebastian Kapfer 
23111e0c5b12SDmitry Torokhov 	psmouse->private = priv;
23121da177e4SLinus Torvalds 
231325bded7cSSeth Forshee 	psmouse_reset(psmouse);
231425bded7cSSeth Forshee 
2315b5d6b851SKevin Cernekee 	if (alps_identify(psmouse, priv) < 0)
231671bb21b6SMaxim Levitsky 		goto init_fail;
231771bb21b6SMaxim Levitsky 
231824af5cb9SKevin Cernekee 	if (priv->hw_init(psmouse))
23191da177e4SLinus Torvalds 		goto init_fail;
23201da177e4SLinus Torvalds 
23217105d2eaSDmitry Torokhov 	/*
23227105d2eaSDmitry Torokhov 	 * Undo part of setup done for us by psmouse core since touchpad
23237105d2eaSDmitry Torokhov 	 * is not a relative device.
23247105d2eaSDmitry Torokhov 	 */
23257105d2eaSDmitry Torokhov 	__clear_bit(EV_REL, dev1->evbit);
23267105d2eaSDmitry Torokhov 	__clear_bit(REL_X, dev1->relbit);
23277105d2eaSDmitry Torokhov 	__clear_bit(REL_Y, dev1->relbit);
23287105d2eaSDmitry Torokhov 
23297105d2eaSDmitry Torokhov 	/*
23307105d2eaSDmitry Torokhov 	 * Now set up our capabilities.
23317105d2eaSDmitry Torokhov 	 */
23327b19ada2SJiri Slaby 	dev1->evbit[BIT_WORD(EV_KEY)] |= BIT_MASK(EV_KEY);
23337b19ada2SJiri Slaby 	dev1->keybit[BIT_WORD(BTN_TOUCH)] |= BIT_MASK(BTN_TOUCH);
23347b19ada2SJiri Slaby 	dev1->keybit[BIT_WORD(BTN_TOOL_FINGER)] |= BIT_MASK(BTN_TOOL_FINGER);
233571bb21b6SMaxim Levitsky 	dev1->keybit[BIT_WORD(BTN_LEFT)] |=
233671bb21b6SMaxim Levitsky 		BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
23371da177e4SLinus Torvalds 
23387b19ada2SJiri Slaby 	dev1->evbit[BIT_WORD(EV_ABS)] |= BIT_MASK(EV_ABS);
233925bded7cSSeth Forshee 
234024af5cb9SKevin Cernekee 	priv->set_abs_params(priv, dev1);
23413808843cSYunkang Tang 	/* No pressure on V7 */
23423808843cSYunkang Tang 	if (priv->proto_version != ALPS_PROTO_V7)
23432e5b636bSDmitry Torokhov 		input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
23441da177e4SLinus Torvalds 
234599df65e7SKevin Cernekee 	if (priv->flags & ALPS_WHEEL) {
23467b19ada2SJiri Slaby 		dev1->evbit[BIT_WORD(EV_REL)] |= BIT_MASK(EV_REL);
23477b19ada2SJiri Slaby 		dev1->relbit[BIT_WORD(REL_WHEEL)] |= BIT_MASK(REL_WHEEL);
23481da177e4SLinus Torvalds 	}
23491da177e4SLinus Torvalds 
235099df65e7SKevin Cernekee 	if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
23517b19ada2SJiri Slaby 		dev1->keybit[BIT_WORD(BTN_FORWARD)] |= BIT_MASK(BTN_FORWARD);
23527b19ada2SJiri Slaby 		dev1->keybit[BIT_WORD(BTN_BACK)] |= BIT_MASK(BTN_BACK);
23531da177e4SLinus Torvalds 	}
23541da177e4SLinus Torvalds 
235599df65e7SKevin Cernekee 	if (priv->flags & ALPS_FOUR_BUTTONS) {
235671bb21b6SMaxim Levitsky 		dev1->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_0);
235771bb21b6SMaxim Levitsky 		dev1->keybit[BIT_WORD(BTN_1)] |= BIT_MASK(BTN_1);
235871bb21b6SMaxim Levitsky 		dev1->keybit[BIT_WORD(BTN_2)] |= BIT_MASK(BTN_2);
235971bb21b6SMaxim Levitsky 		dev1->keybit[BIT_WORD(BTN_3)] |= BIT_MASK(BTN_3);
23603808843cSYunkang Tang 	} else if (priv->flags & ALPS_BUTTONPAD) {
23613808843cSYunkang Tang 		set_bit(INPUT_PROP_BUTTONPAD, dev1->propbit);
23623808843cSYunkang Tang 		clear_bit(BTN_RIGHT, dev1->keybit);
236371bb21b6SMaxim Levitsky 	} else {
236471bb21b6SMaxim Levitsky 		dev1->keybit[BIT_WORD(BTN_MIDDLE)] |= BIT_MASK(BTN_MIDDLE);
236571bb21b6SMaxim Levitsky 	}
236671bb21b6SMaxim Levitsky 
236708ffce45SDmitry Torokhov 	snprintf(priv->phys, sizeof(priv->phys), "%s/input1", psmouse->ps2dev.serio->phys);
23682e5b636bSDmitry Torokhov 	dev2->phys = priv->phys;
236999df65e7SKevin Cernekee 	dev2->name = (priv->flags & ALPS_DUALPOINT) ?
23709354f263SYunkang Tang 		     "DualPoint Stick" : "ALPS PS/2 Device";
23712e5b636bSDmitry Torokhov 	dev2->id.bustype = BUS_I8042;
23722e5b636bSDmitry Torokhov 	dev2->id.vendor  = 0x0002;
23732e5b636bSDmitry Torokhov 	dev2->id.product = PSMOUSE_ALPS;
23742e5b636bSDmitry Torokhov 	dev2->id.version = 0x0000;
23751db3a345SDmitry Torokhov 	dev2->dev.parent = &psmouse->ps2dev.serio->dev;
23761da177e4SLinus Torvalds 
23777b19ada2SJiri Slaby 	dev2->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
237871bb21b6SMaxim Levitsky 	dev2->relbit[BIT_WORD(REL_X)] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
237971bb21b6SMaxim Levitsky 	dev2->keybit[BIT_WORD(BTN_LEFT)] =
238071bb21b6SMaxim Levitsky 		BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_MIDDLE) | BIT_MASK(BTN_RIGHT);
23811da177e4SLinus Torvalds 
238201d4cd5cSHans de Goede 	__set_bit(INPUT_PROP_POINTER, dev2->propbit);
23837611392fSHans de Goede 	if (priv->flags & ALPS_DUALPOINT)
23847611392fSHans de Goede 		__set_bit(INPUT_PROP_POINTING_STICK, dev2->propbit);
23857611392fSHans de Goede 
2386f42649e8SDmitry Torokhov 	if (input_register_device(priv->dev2))
2387f42649e8SDmitry Torokhov 		goto init_fail;
23881da177e4SLinus Torvalds 
23891da177e4SLinus Torvalds 	psmouse->protocol_handler = alps_process_byte;
2390f0d5c6f4SDmitry Torokhov 	psmouse->poll = alps_poll;
23911da177e4SLinus Torvalds 	psmouse->disconnect = alps_disconnect;
23921da177e4SLinus Torvalds 	psmouse->reconnect = alps_reconnect;
239399df65e7SKevin Cernekee 	psmouse->pktsize = priv->proto_version == ALPS_PROTO_V4 ? 8 : 6;
23941da177e4SLinus Torvalds 
2395f0d5c6f4SDmitry Torokhov 	/* We are having trouble resyncing ALPS touchpads so disable it for now */
2396f0d5c6f4SDmitry Torokhov 	psmouse->resync_time = 0;
2397f0d5c6f4SDmitry Torokhov 
23989d720b34SPali Rohár 	/* Allow 2 invalid packets without resetting device */
23999d720b34SPali Rohár 	psmouse->resetafter = psmouse->pktsize * 2;
24009d720b34SPali Rohár 
24011da177e4SLinus Torvalds 	return 0;
24021da177e4SLinus Torvalds 
24031da177e4SLinus Torvalds init_fail:
2404f42649e8SDmitry Torokhov 	psmouse_reset(psmouse);
24052e5b636bSDmitry Torokhov 	input_free_device(dev2);
24061da177e4SLinus Torvalds 	kfree(priv);
24071e0c5b12SDmitry Torokhov 	psmouse->private = NULL;
24081da177e4SLinus Torvalds 	return -1;
24091da177e4SLinus Torvalds }
24101da177e4SLinus Torvalds 
2411b7802c5cSDmitry Torokhov int alps_detect(struct psmouse *psmouse, bool set_properties)
24121da177e4SLinus Torvalds {
2413b5d6b851SKevin Cernekee 	struct alps_data dummy;
24141da177e4SLinus Torvalds 
2415b5d6b851SKevin Cernekee 	if (alps_identify(psmouse, &dummy) < 0)
24161da177e4SLinus Torvalds 		return -1;
24171da177e4SLinus Torvalds 
24181da177e4SLinus Torvalds 	if (set_properties) {
24191da177e4SLinus Torvalds 		psmouse->vendor = "ALPS";
2420b5d6b851SKevin Cernekee 		psmouse->name = dummy.flags & ALPS_DUALPOINT ?
2421968ac842SDmitry Torokhov 				"DualPoint TouchPad" : "GlidePoint";
2422b5d6b851SKevin Cernekee 		psmouse->model = dummy.proto_version << 8;
24231da177e4SLinus Torvalds 	}
24241da177e4SLinus Torvalds 	return 0;
24251da177e4SLinus Torvalds }
24261da177e4SLinus Torvalds 
2427