xref: /openbmc/linux/drivers/input/mouse/alps.c (revision 4621c966)
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>
23073e570dSHans de Goede #include <linux/dmi.h>
241da177e4SLinus Torvalds 
251da177e4SLinus Torvalds #include "psmouse.h"
261da177e4SLinus Torvalds #include "alps.h"
271da177e4SLinus Torvalds 
2825bded7cSSeth Forshee /*
2925bded7cSSeth Forshee  * Definitions for ALPS version 3 and 4 command mode protocol
3025bded7cSSeth Forshee  */
3125bded7cSSeth Forshee #define ALPS_CMD_NIBBLE_10	0x01f2
3225bded7cSSeth Forshee 
33cd401204SKevin Cernekee #define ALPS_REG_BASE_RUSHMORE	0xc2c0
34dae928ecSPali Rohár #define ALPS_REG_BASE_V7	0xc2c0
35cd401204SKevin Cernekee #define ALPS_REG_BASE_PINNACLE	0x0000
36cd401204SKevin Cernekee 
3725bded7cSSeth Forshee static const struct alps_nibble_commands alps_v3_nibble_commands[] = {
3825bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETPOLL,		0x00 }, /* 0 */
3925bded7cSSeth Forshee 	{ PSMOUSE_CMD_RESET_DIS,	0x00 }, /* 1 */
4025bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETSCALE21,	0x00 }, /* 2 */
4125bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0x0a }, /* 3 */
4225bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0x14 }, /* 4 */
4325bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0x28 }, /* 5 */
4425bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0x3c }, /* 6 */
4525bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0x50 }, /* 7 */
4625bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0x64 }, /* 8 */
4725bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0xc8 }, /* 9 */
4825bded7cSSeth Forshee 	{ ALPS_CMD_NIBBLE_10,		0x00 }, /* a */
4925bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRES,		0x00 }, /* b */
5025bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRES,		0x01 }, /* c */
5125bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRES,		0x02 }, /* d */
5225bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRES,		0x03 }, /* e */
5325bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETSCALE11,	0x00 }, /* f */
5425bded7cSSeth Forshee };
5525bded7cSSeth Forshee 
5625bded7cSSeth Forshee static const struct alps_nibble_commands alps_v4_nibble_commands[] = {
5725bded7cSSeth Forshee 	{ PSMOUSE_CMD_ENABLE,		0x00 }, /* 0 */
5825bded7cSSeth Forshee 	{ PSMOUSE_CMD_RESET_DIS,	0x00 }, /* 1 */
5925bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETSCALE21,	0x00 }, /* 2 */
6025bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0x0a }, /* 3 */
6125bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0x14 }, /* 4 */
6225bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0x28 }, /* 5 */
6325bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0x3c }, /* 6 */
6425bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0x50 }, /* 7 */
6525bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0x64 }, /* 8 */
6625bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0xc8 }, /* 9 */
6725bded7cSSeth Forshee 	{ ALPS_CMD_NIBBLE_10,		0x00 }, /* a */
6825bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRES,		0x00 }, /* b */
6925bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRES,		0x01 }, /* c */
7025bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRES,		0x02 }, /* d */
7125bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRES,		0x03 }, /* e */
7225bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETSCALE11,	0x00 }, /* f */
7325bded7cSSeth Forshee };
7425bded7cSSeth Forshee 
7595f75e91SYunkang Tang static const struct alps_nibble_commands alps_v6_nibble_commands[] = {
7695f75e91SYunkang Tang 	{ PSMOUSE_CMD_ENABLE,		0x00 }, /* 0 */
7795f75e91SYunkang Tang 	{ PSMOUSE_CMD_SETRATE,		0x0a }, /* 1 */
7895f75e91SYunkang Tang 	{ PSMOUSE_CMD_SETRATE,		0x14 }, /* 2 */
7995f75e91SYunkang Tang 	{ PSMOUSE_CMD_SETRATE,		0x28 }, /* 3 */
8095f75e91SYunkang Tang 	{ PSMOUSE_CMD_SETRATE,		0x3c }, /* 4 */
8195f75e91SYunkang Tang 	{ PSMOUSE_CMD_SETRATE,		0x50 }, /* 5 */
8295f75e91SYunkang Tang 	{ PSMOUSE_CMD_SETRATE,		0x64 }, /* 6 */
8395f75e91SYunkang Tang 	{ PSMOUSE_CMD_SETRATE,		0xc8 }, /* 7 */
8495f75e91SYunkang Tang 	{ PSMOUSE_CMD_GETID,		0x00 }, /* 8 */
8595f75e91SYunkang Tang 	{ PSMOUSE_CMD_GETINFO,		0x00 }, /* 9 */
8695f75e91SYunkang Tang 	{ PSMOUSE_CMD_SETRES,		0x00 }, /* a */
8795f75e91SYunkang Tang 	{ PSMOUSE_CMD_SETRES,		0x01 }, /* b */
8895f75e91SYunkang Tang 	{ PSMOUSE_CMD_SETRES,		0x02 }, /* c */
8995f75e91SYunkang Tang 	{ PSMOUSE_CMD_SETRES,		0x03 }, /* d */
9095f75e91SYunkang Tang 	{ PSMOUSE_CMD_SETSCALE21,	0x00 }, /* e */
9195f75e91SYunkang Tang 	{ PSMOUSE_CMD_SETSCALE11,	0x00 }, /* f */
9295f75e91SYunkang Tang };
9395f75e91SYunkang Tang 
9425bded7cSSeth Forshee 
9571bb21b6SMaxim Levitsky #define ALPS_DUALPOINT		0x02	/* touchpad has trackstick */
9671bb21b6SMaxim Levitsky #define ALPS_PASS		0x04	/* device has a pass-through port */
9771bb21b6SMaxim Levitsky 
9871bb21b6SMaxim Levitsky #define ALPS_WHEEL		0x08	/* hardware wheel present */
9971bb21b6SMaxim Levitsky #define ALPS_FW_BK_1		0x10	/* front & back buttons present */
10071bb21b6SMaxim Levitsky #define ALPS_FW_BK_2		0x20	/* front & back buttons present */
10171bb21b6SMaxim Levitsky #define ALPS_FOUR_BUTTONS	0x40	/* 4 direction button present */
1021d9f2626SSebastian Kapfer #define ALPS_PS2_INTERLEAVED	0x80	/* 3-byte PS/2 packet interleaved with
1031d9f2626SSebastian Kapfer 					   6-byte ALPS packet */
10419556219SHans de Goede #define ALPS_STICK_BITS		0x100	/* separate stick button bits */
1053808843cSYunkang Tang #define ALPS_BUTTONPAD		0x200	/* device is a clickpad */
1067ad8a106SBen Gamari #define ALPS_DUALPOINT_WITH_PRESSURE	0x400	/* device can report trackpoint pressure */
1071da177e4SLinus Torvalds 
108e38de678SHelge Deller static const struct alps_model_info alps_model_data[] = {
109626b9da0SDmitry Torokhov 	/*
110626b9da0SDmitry Torokhov 	 * XXX This entry is suspicious. First byte has zero lower nibble,
111626b9da0SDmitry Torokhov 	 * which is what a normal mouse would report. Also, the value 0x0e
112626b9da0SDmitry Torokhov 	 * isn't valid per PS/2 spec.
113626b9da0SDmitry Torokhov 	 */
114a3cbfd56SPali Rohár 	{ { 0x20, 0x02, 0x0e }, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } },
115626b9da0SDmitry Torokhov 
116a3cbfd56SPali Rohár 	{ { 0x22, 0x02, 0x0a }, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } },
117a3cbfd56SPali Rohár 	{ { 0x22, 0x02, 0x14 }, { ALPS_PROTO_V2, 0xff, 0xff, ALPS_PASS | ALPS_DUALPOINT } },	/* Dell Latitude D600 */
118a3cbfd56SPali Rohár 	{ { 0x32, 0x02, 0x14 }, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } },	/* Toshiba Salellite Pro M10 */
119a3cbfd56SPali Rohár 	{ { 0x33, 0x02, 0x0a }, { ALPS_PROTO_V1, 0x88, 0xf8, 0 } },				/* UMAX-530T */
120a3cbfd56SPali Rohár 	{ { 0x52, 0x01, 0x14 }, { ALPS_PROTO_V2, 0xff, 0xff,
1218326bb57SDmitry Torokhov 		ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED } },				/* Toshiba Tecra A11-11L */
122a3cbfd56SPali Rohár 	{ { 0x53, 0x02, 0x0a }, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
123a3cbfd56SPali Rohár 	{ { 0x53, 0x02, 0x14 }, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
124a3cbfd56SPali Rohár 	{ { 0x60, 0x03, 0xc8 }, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },				/* HP ze1115 */
125a3cbfd56SPali Rohár 	{ { 0x62, 0x02, 0x14 }, { ALPS_PROTO_V2, 0xcf, 0xcf,
126a3cbfd56SPali Rohár 		ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED } },				/* Dell Latitude E5500, E6400, E6500, Precision M4400 */
127a3cbfd56SPali Rohár 	{ { 0x63, 0x02, 0x0a }, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
128a3cbfd56SPali Rohár 	{ { 0x63, 0x02, 0x14 }, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
129a3cbfd56SPali Rohár 	{ { 0x63, 0x02, 0x28 }, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 } },			/* Fujitsu Siemens S6010 */
130a3cbfd56SPali Rohár 	{ { 0x63, 0x02, 0x3c }, { ALPS_PROTO_V2, 0x8f, 0x8f, ALPS_WHEEL } },			/* Toshiba Satellite S2400-103 */
131a3cbfd56SPali Rohár 	{ { 0x63, 0x02, 0x50 }, { ALPS_PROTO_V2, 0xef, 0xef, ALPS_FW_BK_1 } },			/* NEC Versa L320 */
132a3cbfd56SPali Rohár 	{ { 0x63, 0x02, 0x64 }, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
133a3cbfd56SPali Rohár 	{ { 0x63, 0x03, 0xc8 }, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } },	/* Dell Latitude D800 */
134a3cbfd56SPali Rohár 	{ { 0x73, 0x00, 0x0a }, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_DUALPOINT } },		/* ThinkPad R61 8918-5QG */
135a3cbfd56SPali Rohár 	{ { 0x73, 0x00, 0x14 }, { ALPS_PROTO_V6, 0xff, 0xff, ALPS_DUALPOINT } },		/* Dell XT2 */
136a3cbfd56SPali Rohár 	{ { 0x73, 0x02, 0x0a }, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
137a3cbfd56SPali Rohár 	{ { 0x73, 0x02, 0x14 }, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 } },			/* Ahtec Laptop */
138a3cbfd56SPali Rohár 	{ { 0x73, 0x02, 0x50 }, { ALPS_PROTO_V2, 0xcf, 0xcf, ALPS_FOUR_BUTTONS } },		/* Dell Vostro 1400 */
1391da177e4SLinus Torvalds };
1401da177e4SLinus Torvalds 
1413296f71cSDmitry Torokhov static const struct alps_protocol_info alps_v3_protocol_data = {
1424621c966SPali Rohár 	ALPS_PROTO_V3, 0x8f, 0x8f, ALPS_DUALPOINT | ALPS_DUALPOINT_WITH_PRESSURE
1433296f71cSDmitry Torokhov };
1443296f71cSDmitry Torokhov 
1453296f71cSDmitry Torokhov static const struct alps_protocol_info alps_v3_rushmore_data = {
1464621c966SPali Rohár 	ALPS_PROTO_V3_RUSHMORE, 0x8f, 0x8f, ALPS_DUALPOINT | ALPS_DUALPOINT_WITH_PRESSURE
1473296f71cSDmitry Torokhov };
1483296f71cSDmitry Torokhov 
14909c398bcSPali Rohár static const struct alps_protocol_info alps_v4_protocol_data = {
15009c398bcSPali Rohár 	ALPS_PROTO_V4, 0x8f, 0x8f, 0
15109c398bcSPali Rohár };
15209c398bcSPali Rohár 
1533296f71cSDmitry Torokhov static const struct alps_protocol_info alps_v5_protocol_data = {
1543296f71cSDmitry Torokhov 	ALPS_PROTO_V5, 0xc8, 0xd8, 0
1553296f71cSDmitry Torokhov };
1563296f71cSDmitry Torokhov 
1573296f71cSDmitry Torokhov static const struct alps_protocol_info alps_v7_protocol_data = {
1584621c966SPali Rohár 	ALPS_PROTO_V7, 0x48, 0x48, ALPS_DUALPOINT | ALPS_DUALPOINT_WITH_PRESSURE
1593296f71cSDmitry Torokhov };
1603296f71cSDmitry Torokhov 
1613db5b9f7SMasaki Ota static const struct alps_protocol_info alps_v8_protocol_data = {
1623db5b9f7SMasaki Ota 	ALPS_PROTO_V8, 0x18, 0x18, 0
1633db5b9f7SMasaki Ota };
1643db5b9f7SMasaki Ota 
165c9815232SPali Rohár static const struct alps_protocol_info alps_v9_protocol_data = {
166c9815232SPali Rohár 	ALPS_PROTO_V9, 0xc8, 0xc8, 0
167c9815232SPali Rohár };
168c9815232SPali Rohár 
16919556219SHans de Goede /*
17019556219SHans de Goede  * Some v2 models report the stick buttons in separate bits
17119556219SHans de Goede  */
17219556219SHans de Goede static const struct dmi_system_id alps_dmi_has_separate_stick_buttons[] = {
17319556219SHans de Goede #if defined(CONFIG_DMI) && defined(CONFIG_X86)
17419556219SHans de Goede 	{
17519556219SHans de Goede 		/* Extrapolated from other entries */
17619556219SHans de Goede 		.matches = {
17719556219SHans de Goede 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
17819556219SHans de Goede 			DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D420"),
17919556219SHans de Goede 		},
18019556219SHans de Goede 	},
18119556219SHans de Goede 	{
18219556219SHans de Goede 		/* Reported-by: Hans de Bruin <jmdebruin@xmsnet.nl> */
18319556219SHans de Goede 		.matches = {
18419556219SHans de Goede 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
18519556219SHans de Goede 			DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D430"),
18619556219SHans de Goede 		},
18719556219SHans de Goede 	},
18819556219SHans de Goede 	{
18919556219SHans de Goede 		/* Reported-by: Hans de Goede <hdegoede@redhat.com> */
19019556219SHans de Goede 		.matches = {
19119556219SHans de Goede 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
19219556219SHans de Goede 			DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D620"),
19319556219SHans de Goede 		},
19419556219SHans de Goede 	},
19519556219SHans de Goede 	{
19619556219SHans de Goede 		/* Extrapolated from other entries */
19719556219SHans de Goede 		.matches = {
19819556219SHans de Goede 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
19919556219SHans de Goede 			DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D630"),
20019556219SHans de Goede 		},
20119556219SHans de Goede 	},
20219556219SHans de Goede #endif
20319556219SHans de Goede 	{ }
20419556219SHans de Goede };
20519556219SHans de Goede 
20624af5cb9SKevin Cernekee static void alps_set_abs_params_st(struct alps_data *priv,
20724af5cb9SKevin Cernekee 				   struct input_dev *dev1);
208688ea364SHans de Goede static void alps_set_abs_params_semi_mt(struct alps_data *priv,
20924af5cb9SKevin Cernekee 					struct input_dev *dev1);
2108eccd393SMasaki Ota static void alps_set_abs_params_v7(struct alps_data *priv,
2118eccd393SMasaki Ota 				   struct input_dev *dev1);
2123db5b9f7SMasaki Ota static void alps_set_abs_params_ss4_v2(struct alps_data *priv,
2133db5b9f7SMasaki Ota 				       struct input_dev *dev1);
21424af5cb9SKevin Cernekee 
215d4b347b2SSeth Forshee /* Packet formats are described in Documentation/input/alps.txt */
2161da177e4SLinus Torvalds 
21799df65e7SKevin Cernekee static bool alps_is_valid_first_byte(struct alps_data *priv,
2181d9f2626SSebastian Kapfer 				     unsigned char data)
2191d9f2626SSebastian Kapfer {
22099df65e7SKevin Cernekee 	return (data & priv->mask0) == priv->byte0;
2211d9f2626SSebastian Kapfer }
2221d9f2626SSebastian Kapfer 
22304aae283SPali Rohár static void alps_report_buttons(struct input_dev *dev1, struct input_dev *dev2,
2241d9f2626SSebastian Kapfer 				int left, int right, int middle)
2251d9f2626SSebastian Kapfer {
2261d9f2626SSebastian Kapfer 	struct input_dev *dev;
2271d9f2626SSebastian Kapfer 
2281d9f2626SSebastian Kapfer 	/*
2291d9f2626SSebastian Kapfer 	 * If shared button has already been reported on the
2301d9f2626SSebastian Kapfer 	 * other device (dev2) then this event should be also
2311d9f2626SSebastian Kapfer 	 * sent through that device.
2321d9f2626SSebastian Kapfer 	 */
23304aae283SPali Rohár 	dev = (dev2 && test_bit(BTN_LEFT, dev2->key)) ? dev2 : dev1;
2341d9f2626SSebastian Kapfer 	input_report_key(dev, BTN_LEFT, left);
2351d9f2626SSebastian Kapfer 
23604aae283SPali Rohár 	dev = (dev2 && test_bit(BTN_RIGHT, dev2->key)) ? dev2 : dev1;
2371d9f2626SSebastian Kapfer 	input_report_key(dev, BTN_RIGHT, right);
2381d9f2626SSebastian Kapfer 
23904aae283SPali Rohár 	dev = (dev2 && test_bit(BTN_MIDDLE, dev2->key)) ? dev2 : dev1;
2401d9f2626SSebastian Kapfer 	input_report_key(dev, BTN_MIDDLE, middle);
2411d9f2626SSebastian Kapfer 
2421d9f2626SSebastian Kapfer 	/*
2431d9f2626SSebastian Kapfer 	 * Sync the _other_ device now, we'll do the first
2441d9f2626SSebastian Kapfer 	 * device later once we report the rest of the events.
2451d9f2626SSebastian Kapfer 	 */
24604aae283SPali Rohár 	if (dev2)
2471d9f2626SSebastian Kapfer 		input_sync(dev2);
2481d9f2626SSebastian Kapfer }
2491d9f2626SSebastian Kapfer 
25025bded7cSSeth Forshee static void alps_process_packet_v1_v2(struct psmouse *psmouse)
2511da177e4SLinus Torvalds {
2521da177e4SLinus Torvalds 	struct alps_data *priv = psmouse->private;
2531da177e4SLinus Torvalds 	unsigned char *packet = psmouse->packet;
2542e5b636bSDmitry Torokhov 	struct input_dev *dev = psmouse->dev;
2552e5b636bSDmitry Torokhov 	struct input_dev *dev2 = priv->dev2;
2561da177e4SLinus Torvalds 	int x, y, z, ges, fin, left, right, middle;
257c30b4c10SIvan Casado Ruiz 	int back = 0, forward = 0;
2581da177e4SLinus Torvalds 
25999df65e7SKevin Cernekee 	if (priv->proto_version == ALPS_PROTO_V1) {
260d2f4012fSYotam Medini 		left = packet[2] & 0x10;
261d2f4012fSYotam Medini 		right = packet[2] & 0x08;
2621da177e4SLinus Torvalds 		middle = 0;
2631da177e4SLinus Torvalds 		x = packet[1] | ((packet[0] & 0x07) << 7);
2641da177e4SLinus Torvalds 		y = packet[4] | ((packet[3] & 0x07) << 7);
2651da177e4SLinus Torvalds 		z = packet[5];
2661da177e4SLinus Torvalds 	} else {
2671da177e4SLinus Torvalds 		left = packet[3] & 1;
2681da177e4SLinus Torvalds 		right = packet[3] & 2;
2691da177e4SLinus Torvalds 		middle = packet[3] & 4;
2701da177e4SLinus Torvalds 		x = packet[1] | ((packet[2] & 0x78) << (7 - 3));
2711da177e4SLinus Torvalds 		y = packet[4] | ((packet[3] & 0x70) << (7 - 4));
2721da177e4SLinus Torvalds 		z = packet[5];
2731da177e4SLinus Torvalds 	}
2741da177e4SLinus Torvalds 
27599df65e7SKevin Cernekee 	if (priv->flags & ALPS_FW_BK_1) {
2763c00bb96SLaszlo Kajan 		back = packet[0] & 0x10;
2773c00bb96SLaszlo Kajan 		forward = packet[2] & 4;
278c30b4c10SIvan Casado Ruiz 	}
279c30b4c10SIvan Casado Ruiz 
28099df65e7SKevin Cernekee 	if (priv->flags & ALPS_FW_BK_2) {
281c30b4c10SIvan Casado Ruiz 		back = packet[3] & 4;
282c30b4c10SIvan Casado Ruiz 		forward = packet[2] & 4;
283c30b4c10SIvan Casado Ruiz 		if ((middle = forward && back))
284c30b4c10SIvan Casado Ruiz 			forward = back = 0;
285c30b4c10SIvan Casado Ruiz 	}
286c30b4c10SIvan Casado Ruiz 
2871da177e4SLinus Torvalds 	ges = packet[2] & 1;
2881da177e4SLinus Torvalds 	fin = packet[2] & 2;
2891da177e4SLinus Torvalds 
29099df65e7SKevin Cernekee 	if ((priv->flags & ALPS_DUALPOINT) && z == 127) {
2911da177e4SLinus Torvalds 		input_report_rel(dev2, REL_X,  (x > 383 ? (x - 768) : x));
2921da177e4SLinus Torvalds 		input_report_rel(dev2, REL_Y, -(y > 255 ? (y - 512) : y));
293d7ed5d88SUlrich Dangel 
29404aae283SPali Rohár 		alps_report_buttons(dev2, dev, left, right, middle);
295d7ed5d88SUlrich Dangel 
2961da177e4SLinus Torvalds 		input_sync(dev2);
2971da177e4SLinus Torvalds 		return;
2981da177e4SLinus Torvalds 	}
2991da177e4SLinus Torvalds 
30019556219SHans de Goede 	/* Some models have separate stick button bits */
30119556219SHans de Goede 	if (priv->flags & ALPS_STICK_BITS) {
30292bac83dSHans de Goede 		left |= packet[0] & 1;
30392bac83dSHans de Goede 		right |= packet[0] & 2;
30492bac83dSHans de Goede 		middle |= packet[0] & 4;
30592bac83dSHans de Goede 	}
30692bac83dSHans de Goede 
30704aae283SPali Rohár 	alps_report_buttons(dev, dev2, left, right, middle);
308d7ed5d88SUlrich Dangel 
3091da177e4SLinus Torvalds 	/* Convert hardware tap to a reasonable Z value */
31071bb21b6SMaxim Levitsky 	if (ges && !fin)
31171bb21b6SMaxim Levitsky 		z = 40;
3121da177e4SLinus Torvalds 
3131da177e4SLinus Torvalds 	/*
3141da177e4SLinus Torvalds 	 * A "tap and drag" operation is reported by the hardware as a transition
3151da177e4SLinus Torvalds 	 * from (!fin && ges) to (fin && ges). This should be translated to the
3161da177e4SLinus Torvalds 	 * sequence Z>0, Z==0, Z>0, so the Z==0 event has to be generated manually.
3171da177e4SLinus Torvalds 	 */
3181da177e4SLinus Torvalds 	if (ges && fin && !priv->prev_fin) {
3191da177e4SLinus Torvalds 		input_report_abs(dev, ABS_X, x);
3201da177e4SLinus Torvalds 		input_report_abs(dev, ABS_Y, y);
3211da177e4SLinus Torvalds 		input_report_abs(dev, ABS_PRESSURE, 0);
3221da177e4SLinus Torvalds 		input_report_key(dev, BTN_TOOL_FINGER, 0);
3231da177e4SLinus Torvalds 		input_sync(dev);
3241da177e4SLinus Torvalds 	}
3251da177e4SLinus Torvalds 	priv->prev_fin = fin;
3261da177e4SLinus Torvalds 
32771bb21b6SMaxim Levitsky 	if (z > 30)
32871bb21b6SMaxim Levitsky 		input_report_key(dev, BTN_TOUCH, 1);
32971bb21b6SMaxim Levitsky 	if (z < 25)
33071bb21b6SMaxim Levitsky 		input_report_key(dev, BTN_TOUCH, 0);
3311da177e4SLinus Torvalds 
3321da177e4SLinus Torvalds 	if (z > 0) {
3331da177e4SLinus Torvalds 		input_report_abs(dev, ABS_X, x);
3341da177e4SLinus Torvalds 		input_report_abs(dev, ABS_Y, y);
3351da177e4SLinus Torvalds 	}
3361da177e4SLinus Torvalds 
3371da177e4SLinus Torvalds 	input_report_abs(dev, ABS_PRESSURE, z);
3381da177e4SLinus Torvalds 	input_report_key(dev, BTN_TOOL_FINGER, z > 0);
3391da177e4SLinus Torvalds 
34099df65e7SKevin Cernekee 	if (priv->flags & ALPS_WHEEL)
341e6c047b9SVojtech Pavlik 		input_report_rel(dev, REL_WHEEL, ((packet[2] << 1) & 0x08) - ((packet[0] >> 4) & 0x07));
3421da177e4SLinus Torvalds 
34399df65e7SKevin Cernekee 	if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
344c30b4c10SIvan Casado Ruiz 		input_report_key(dev, BTN_FORWARD, forward);
345c30b4c10SIvan Casado Ruiz 		input_report_key(dev, BTN_BACK, back);
3461da177e4SLinus Torvalds 	}
3471da177e4SLinus Torvalds 
34899df65e7SKevin Cernekee 	if (priv->flags & ALPS_FOUR_BUTTONS) {
34971bb21b6SMaxim Levitsky 		input_report_key(dev, BTN_0, packet[2] & 4);
35071bb21b6SMaxim Levitsky 		input_report_key(dev, BTN_1, packet[0] & 0x10);
35171bb21b6SMaxim Levitsky 		input_report_key(dev, BTN_2, packet[3] & 4);
35271bb21b6SMaxim Levitsky 		input_report_key(dev, BTN_3, packet[0] & 0x20);
35371bb21b6SMaxim Levitsky 	}
35471bb21b6SMaxim Levitsky 
3551da177e4SLinus Torvalds 	input_sync(dev);
3561da177e4SLinus Torvalds }
3571da177e4SLinus Torvalds 
358036e6c7bSHans de Goede static void alps_get_bitmap_points(unsigned int map,
359036e6c7bSHans de Goede 				   struct alps_bitmap_point *low,
360036e6c7bSHans de Goede 				   struct alps_bitmap_point *high,
361036e6c7bSHans de Goede 				   int *fingers)
362036e6c7bSHans de Goede {
363036e6c7bSHans de Goede 	struct alps_bitmap_point *point;
364036e6c7bSHans de Goede 	int i, bit, prev_bit = 0;
365036e6c7bSHans de Goede 
366036e6c7bSHans de Goede 	point = low;
367036e6c7bSHans de Goede 	for (i = 0; map != 0; i++, map >>= 1) {
368036e6c7bSHans de Goede 		bit = map & 1;
369036e6c7bSHans de Goede 		if (bit) {
370036e6c7bSHans de Goede 			if (!prev_bit) {
371036e6c7bSHans de Goede 				point->start_bit = i;
372105affbfSHans de Goede 				point->num_bits = 0;
373036e6c7bSHans de Goede 				(*fingers)++;
374036e6c7bSHans de Goede 			}
375036e6c7bSHans de Goede 			point->num_bits++;
376036e6c7bSHans de Goede 		} else {
377036e6c7bSHans de Goede 			if (prev_bit)
378036e6c7bSHans de Goede 				point = high;
379036e6c7bSHans de Goede 		}
380036e6c7bSHans de Goede 		prev_bit = bit;
381036e6c7bSHans de Goede 	}
382036e6c7bSHans de Goede }
383036e6c7bSHans de Goede 
384ee65d4b3SYunkang Tang /*
385dccf1dd8SHans de Goede  * Process bitmap data from semi-mt protocols. Returns the number of
38601ce661fSSeth Forshee  * fingers detected. A return value of 0 means at least one of the
38701ce661fSSeth Forshee  * bitmaps was empty.
38801ce661fSSeth Forshee  *
38901ce661fSSeth Forshee  * The bitmaps don't have enough data to track fingers, so this function
39001ce661fSSeth Forshee  * only generates points representing a bounding box of all contacts.
39102d04254SHans de Goede  * These points are returned in fields->mt when the return value
39201ce661fSSeth Forshee  * is greater than 0.
39301ce661fSSeth Forshee  */
3947a9f73e7SKevin Cernekee static int alps_process_bitmap(struct alps_data *priv,
39502d04254SHans de Goede 			       struct alps_fields *fields)
39601ce661fSSeth Forshee {
3974dd26573SHans de Goede 	int i, fingers_x = 0, fingers_y = 0, fingers, closest;
39801ce661fSSeth Forshee 	struct alps_bitmap_point x_low = {0,}, x_high = {0,};
39901ce661fSSeth Forshee 	struct alps_bitmap_point y_low = {0,}, y_high = {0,};
4004dd26573SHans de Goede 	struct input_mt_pos corner[4];
40101ce661fSSeth Forshee 
40202d04254SHans de Goede 	if (!fields->x_map || !fields->y_map)
40301ce661fSSeth Forshee 		return 0;
40401ce661fSSeth Forshee 
40502d04254SHans de Goede 	alps_get_bitmap_points(fields->x_map, &x_low, &x_high, &fingers_x);
40602d04254SHans de Goede 	alps_get_bitmap_points(fields->y_map, &y_low, &y_high, &fingers_y);
40701ce661fSSeth Forshee 
40801ce661fSSeth Forshee 	/*
40901ce661fSSeth Forshee 	 * Fingers can overlap, so we use the maximum count of fingers
41001ce661fSSeth Forshee 	 * on either axis as the finger count.
41101ce661fSSeth Forshee 	 */
41201ce661fSSeth Forshee 	fingers = max(fingers_x, fingers_y);
41301ce661fSSeth Forshee 
41401ce661fSSeth Forshee 	/*
41520bea68bSHans de Goede 	 * If an axis reports only a single contact, we have overlapping or
41620bea68bSHans de Goede 	 * adjacent fingers. Divide the single contact between the two points.
41701ce661fSSeth Forshee 	 */
41801ce661fSSeth Forshee 	if (fingers_x == 1) {
41928835f45SHans de Goede 		i = (x_low.num_bits - 1) / 2;
42001ce661fSSeth Forshee 		x_low.num_bits = x_low.num_bits - i;
42101ce661fSSeth Forshee 		x_high.start_bit = x_low.start_bit + i;
42201ce661fSSeth Forshee 		x_high.num_bits = max(i, 1);
42320bea68bSHans de Goede 	}
42420bea68bSHans de Goede 	if (fingers_y == 1) {
42528835f45SHans de Goede 		i = (y_low.num_bits - 1) / 2;
42601ce661fSSeth Forshee 		y_low.num_bits = y_low.num_bits - i;
42701ce661fSSeth Forshee 		y_high.start_bit = y_low.start_bit + i;
42801ce661fSSeth Forshee 		y_high.num_bits = max(i, 1);
42901ce661fSSeth Forshee 	}
43001ce661fSSeth Forshee 
4314dd26573SHans de Goede 	/* top-left corner */
4324dd26573SHans de Goede 	corner[0].x =
43302d04254SHans de Goede 		(priv->x_max * (2 * x_low.start_bit + x_low.num_bits - 1)) /
4347a9f73e7SKevin Cernekee 		(2 * (priv->x_bits - 1));
4354dd26573SHans de Goede 	corner[0].y =
43602d04254SHans de Goede 		(priv->y_max * (2 * y_low.start_bit + y_low.num_bits - 1)) /
4377a9f73e7SKevin Cernekee 		(2 * (priv->y_bits - 1));
43801ce661fSSeth Forshee 
4394dd26573SHans de Goede 	/* top-right corner */
4404dd26573SHans de Goede 	corner[1].x =
44102d04254SHans de Goede 		(priv->x_max * (2 * x_high.start_bit + x_high.num_bits - 1)) /
4427a9f73e7SKevin Cernekee 		(2 * (priv->x_bits - 1));
4434dd26573SHans de Goede 	corner[1].y =
4444dd26573SHans de Goede 		(priv->y_max * (2 * y_low.start_bit + y_low.num_bits - 1)) /
4454dd26573SHans de Goede 		(2 * (priv->y_bits - 1));
4464dd26573SHans de Goede 
4474dd26573SHans de Goede 	/* bottom-right corner */
4484dd26573SHans de Goede 	corner[2].x =
4494dd26573SHans de Goede 		(priv->x_max * (2 * x_high.start_bit + x_high.num_bits - 1)) /
4504dd26573SHans de Goede 		(2 * (priv->x_bits - 1));
4514dd26573SHans de Goede 	corner[2].y =
45202d04254SHans de Goede 		(priv->y_max * (2 * y_high.start_bit + y_high.num_bits - 1)) /
4537a9f73e7SKevin Cernekee 		(2 * (priv->y_bits - 1));
45401ce661fSSeth Forshee 
4554dd26573SHans de Goede 	/* bottom-left corner */
4564dd26573SHans de Goede 	corner[3].x =
4574dd26573SHans de Goede 		(priv->x_max * (2 * x_low.start_bit + x_low.num_bits - 1)) /
4584dd26573SHans de Goede 		(2 * (priv->x_bits - 1));
4594dd26573SHans de Goede 	corner[3].y =
46001ce661fSSeth Forshee 		(priv->y_max * (2 * y_high.start_bit + y_high.num_bits - 1)) /
46101ce661fSSeth Forshee 		(2 * (priv->y_bits - 1));
46201ce661fSSeth Forshee 
463dccf1dd8SHans de Goede 	/* x-bitmap order is reversed on v5 touchpads  */
464dccf1dd8SHans de Goede 	if (priv->proto_version == ALPS_PROTO_V5) {
465dccf1dd8SHans de Goede 		for (i = 0; i < 4; i++)
466dccf1dd8SHans de Goede 			corner[i].x = priv->x_max - corner[i].x;
46740e8f53bSHans de Goede 	}
46840e8f53bSHans de Goede 
469dccf1dd8SHans de Goede 	/* y-bitmap order is reversed on v3 and v4 touchpads  */
470dccf1dd8SHans de Goede 	if (priv->proto_version == ALPS_PROTO_V3 ||
471dccf1dd8SHans de Goede 	    priv->proto_version == ALPS_PROTO_V4) {
4724dd26573SHans de Goede 		for (i = 0; i < 4; i++)
4734dd26573SHans de Goede 			corner[i].y = priv->y_max - corner[i].y;
47401ce661fSSeth Forshee 	}
47501ce661fSSeth Forshee 
4764dd26573SHans de Goede 	/*
4774dd26573SHans de Goede 	 * We only select a corner for the second touch once per 2 finger
4784dd26573SHans de Goede 	 * touch sequence to avoid the chosen corner (and thus the coordinates)
4794dd26573SHans de Goede 	 * jumping around when the first touch is in the middle.
4804dd26573SHans de Goede 	 */
4814dd26573SHans de Goede 	if (priv->second_touch == -1) {
4824dd26573SHans de Goede 		/* Find corner closest to our st coordinates */
4834dd26573SHans de Goede 		closest = 0x7fffffff;
4844dd26573SHans de Goede 		for (i = 0; i < 4; i++) {
4854dd26573SHans de Goede 			int dx = fields->st.x - corner[i].x;
4864dd26573SHans de Goede 			int dy = fields->st.y - corner[i].y;
4874dd26573SHans de Goede 			int distance = dx * dx + dy * dy;
4884dd26573SHans de Goede 
4894dd26573SHans de Goede 			if (distance < closest) {
4904dd26573SHans de Goede 				priv->second_touch = i;
4914dd26573SHans de Goede 				closest = distance;
4924dd26573SHans de Goede 			}
4934dd26573SHans de Goede 		}
4944dd26573SHans de Goede 		/* And select the opposite corner to use for the 2nd touch */
4954dd26573SHans de Goede 		priv->second_touch = (priv->second_touch + 2) % 4;
4964dd26573SHans de Goede 	}
4974dd26573SHans de Goede 
4984dd26573SHans de Goede 	fields->mt[0] = fields->st;
4994dd26573SHans de Goede 	fields->mt[1] = corner[priv->second_touch];
5004dd26573SHans de Goede 
50101ce661fSSeth Forshee 	return fingers;
50201ce661fSSeth Forshee }
50301ce661fSSeth Forshee 
504cdf333efSHans de Goede static void alps_set_slot(struct input_dev *dev, int slot, int x, int y)
50501ce661fSSeth Forshee {
50601ce661fSSeth Forshee 	input_mt_slot(dev, slot);
507cdf333efSHans de Goede 	input_mt_report_slot_state(dev, MT_TOOL_FINGER, true);
50801ce661fSSeth Forshee 	input_report_abs(dev, ABS_MT_POSITION_X, x);
50901ce661fSSeth Forshee 	input_report_abs(dev, ABS_MT_POSITION_Y, y);
51001ce661fSSeth Forshee }
51101ce661fSSeth Forshee 
512cdf333efSHans de Goede static void alps_report_mt_data(struct psmouse *psmouse, int n)
51301ce661fSSeth Forshee {
51402d04254SHans de Goede 	struct alps_data *priv = psmouse->private;
51502d04254SHans de Goede 	struct input_dev *dev = psmouse->dev;
51602d04254SHans de Goede 	struct alps_fields *f = &priv->f;
517cdf333efSHans de Goede 	int i, slot[MAX_TOUCHES];
51802d04254SHans de Goede 
519448c7f38SHenrik Rydberg 	input_mt_assign_slots(dev, slot, f->mt, n, 0);
520cdf333efSHans de Goede 	for (i = 0; i < n; i++)
521cdf333efSHans de Goede 		alps_set_slot(dev, slot[i], f->mt[i].x, f->mt[i].y);
522cdf333efSHans de Goede 
523cdf333efSHans de Goede 	input_mt_sync_frame(dev);
52401ce661fSSeth Forshee }
52501ce661fSSeth Forshee 
52668c21870SHans de Goede static void alps_report_semi_mt_data(struct psmouse *psmouse, int fingers)
52768c21870SHans de Goede {
52868c21870SHans de Goede 	struct alps_data *priv = psmouse->private;
52968c21870SHans de Goede 	struct input_dev *dev = psmouse->dev;
53068c21870SHans de Goede 	struct alps_fields *f = &priv->f;
53168c21870SHans de Goede 
53268c21870SHans de Goede 	/* Use st data when we don't have mt data */
53368c21870SHans de Goede 	if (fingers < 2) {
53468c21870SHans de Goede 		f->mt[0].x = f->st.x;
53568c21870SHans de Goede 		f->mt[0].y = f->st.y;
53668c21870SHans de Goede 		fingers = f->pressure > 0 ? 1 : 0;
5374dd26573SHans de Goede 		priv->second_touch = -1;
53868c21870SHans de Goede 	}
53968c21870SHans de Goede 
5401662c033SHans de Goede 	if (fingers >= 1)
5411662c033SHans de Goede 		alps_set_slot(dev, 0, f->mt[0].x, f->mt[0].y);
5421662c033SHans de Goede 	if (fingers >= 2)
5431662c033SHans de Goede 		alps_set_slot(dev, 1, f->mt[1].x, f->mt[1].y);
5441662c033SHans de Goede 	input_mt_sync_frame(dev);
54568c21870SHans de Goede 
54668c21870SHans de Goede 	input_mt_report_finger_count(dev, fingers);
54768c21870SHans de Goede 
54868c21870SHans de Goede 	input_report_key(dev, BTN_LEFT, f->left);
54968c21870SHans de Goede 	input_report_key(dev, BTN_RIGHT, f->right);
55068c21870SHans de Goede 	input_report_key(dev, BTN_MIDDLE, f->middle);
55168c21870SHans de Goede 
55268c21870SHans de Goede 	input_report_abs(dev, ABS_PRESSURE, f->pressure);
55368c21870SHans de Goede 
55468c21870SHans de Goede 	input_sync(dev);
55568c21870SHans de Goede }
55668c21870SHans de Goede 
55725bded7cSSeth Forshee static void alps_process_trackstick_packet_v3(struct psmouse *psmouse)
55825bded7cSSeth Forshee {
55925bded7cSSeth Forshee 	struct alps_data *priv = psmouse->private;
56025bded7cSSeth Forshee 	unsigned char *packet = psmouse->packet;
56125bded7cSSeth Forshee 	struct input_dev *dev = priv->dev2;
56225bded7cSSeth Forshee 	int x, y, z, left, right, middle;
56325bded7cSSeth Forshee 
56434412ba2SPali Rohár 	/* It should be a DualPoint when received trackstick packet */
56534412ba2SPali Rohár 	if (!(priv->flags & ALPS_DUALPOINT)) {
56634412ba2SPali Rohár 		psmouse_warn(psmouse,
56734412ba2SPali Rohár 			     "Rejected trackstick packet from non DualPoint device");
56834412ba2SPali Rohár 		return;
56934412ba2SPali Rohár 	}
57034412ba2SPali Rohár 
57125bded7cSSeth Forshee 	/* Sanity check packet */
57225bded7cSSeth Forshee 	if (!(packet[0] & 0x40)) {
57325bded7cSSeth Forshee 		psmouse_dbg(psmouse, "Bad trackstick packet, discarding\n");
57425bded7cSSeth Forshee 		return;
57525bded7cSSeth Forshee 	}
57625bded7cSSeth Forshee 
57725bded7cSSeth Forshee 	/*
57825bded7cSSeth Forshee 	 * There's a special packet that seems to indicate the end
57925bded7cSSeth Forshee 	 * of a stream of trackstick data. Filter these out.
58025bded7cSSeth Forshee 	 */
58125bded7cSSeth Forshee 	if (packet[1] == 0x7f && packet[2] == 0x7f && packet[4] == 0x7f)
58225bded7cSSeth Forshee 		return;
58325bded7cSSeth Forshee 
58425bded7cSSeth Forshee 	x = (s8)(((packet[0] & 0x20) << 2) | (packet[1] & 0x7f));
58525bded7cSSeth Forshee 	y = (s8)(((packet[0] & 0x10) << 3) | (packet[2] & 0x7f));
5864621c966SPali Rohár 	z = packet[4] & 0x7c;
58725bded7cSSeth Forshee 
58825bded7cSSeth Forshee 	/*
58925bded7cSSeth Forshee 	 * The x and y values tend to be quite large, and when used
59025bded7cSSeth Forshee 	 * alone the trackstick is difficult to use. Scale them down
59125bded7cSSeth Forshee 	 * to compensate.
59225bded7cSSeth Forshee 	 */
59325bded7cSSeth Forshee 	x /= 8;
59425bded7cSSeth Forshee 	y /= 8;
59525bded7cSSeth Forshee 
59625bded7cSSeth Forshee 	input_report_rel(dev, REL_X, x);
59725bded7cSSeth Forshee 	input_report_rel(dev, REL_Y, -y);
5984621c966SPali Rohár 	input_report_abs(dev, ABS_PRESSURE, z);
59925bded7cSSeth Forshee 
60025bded7cSSeth Forshee 	/*
60125bded7cSSeth Forshee 	 * Most ALPS models report the trackstick buttons in the touchpad
60225bded7cSSeth Forshee 	 * packets, but a few report them here. No reliable way has been
60325bded7cSSeth Forshee 	 * found to differentiate between the models upfront, so we enable
60425bded7cSSeth Forshee 	 * the quirk in response to seeing a button press in the trackstick
60525bded7cSSeth Forshee 	 * packet.
60625bded7cSSeth Forshee 	 */
60725bded7cSSeth Forshee 	left = packet[3] & 0x01;
60825bded7cSSeth Forshee 	right = packet[3] & 0x02;
60925bded7cSSeth Forshee 	middle = packet[3] & 0x04;
61025bded7cSSeth Forshee 
61125bded7cSSeth Forshee 	if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) &&
61225bded7cSSeth Forshee 	    (left || right || middle))
61325bded7cSSeth Forshee 		priv->quirks |= ALPS_QUIRK_TRACKSTICK_BUTTONS;
61425bded7cSSeth Forshee 
61525bded7cSSeth Forshee 	if (priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) {
61625bded7cSSeth Forshee 		input_report_key(dev, BTN_LEFT, left);
61725bded7cSSeth Forshee 		input_report_key(dev, BTN_RIGHT, right);
61825bded7cSSeth Forshee 		input_report_key(dev, BTN_MIDDLE, middle);
61925bded7cSSeth Forshee 	}
62025bded7cSSeth Forshee 
62125bded7cSSeth Forshee 	input_sync(dev);
62225bded7cSSeth Forshee 	return;
62325bded7cSSeth Forshee }
62425bded7cSSeth Forshee 
625f85e5001SKevin Cernekee static void alps_decode_buttons_v3(struct alps_fields *f, unsigned char *p)
626f85e5001SKevin Cernekee {
627f85e5001SKevin Cernekee 	f->left = !!(p[3] & 0x01);
628f85e5001SKevin Cernekee 	f->right = !!(p[3] & 0x02);
629f85e5001SKevin Cernekee 	f->middle = !!(p[3] & 0x04);
630f85e5001SKevin Cernekee 
631f85e5001SKevin Cernekee 	f->ts_left = !!(p[3] & 0x10);
632f85e5001SKevin Cernekee 	f->ts_right = !!(p[3] & 0x20);
633f85e5001SKevin Cernekee 	f->ts_middle = !!(p[3] & 0x40);
634f85e5001SKevin Cernekee }
635f85e5001SKevin Cernekee 
63638c11eaaSHans de Goede static int alps_decode_pinnacle(struct alps_fields *f, unsigned char *p,
637ee65d4b3SYunkang Tang 				 struct psmouse *psmouse)
638f85e5001SKevin Cernekee {
639f85e5001SKevin Cernekee 	f->first_mp = !!(p[4] & 0x40);
640f85e5001SKevin Cernekee 	f->is_mp = !!(p[0] & 0x40);
641f85e5001SKevin Cernekee 
642a839cd57SHans de Goede 	if (f->is_mp) {
643f85e5001SKevin Cernekee 		f->fingers = (p[5] & 0x3) + 1;
644f85e5001SKevin Cernekee 		f->x_map = ((p[4] & 0x7e) << 8) |
645f85e5001SKevin Cernekee 			   ((p[1] & 0x7f) << 2) |
646f85e5001SKevin Cernekee 			   ((p[0] & 0x30) >> 4);
647f85e5001SKevin Cernekee 		f->y_map = ((p[3] & 0x70) << 4) |
648f85e5001SKevin Cernekee 			   ((p[2] & 0x7f) << 1) |
649f85e5001SKevin Cernekee 			   (p[4] & 0x01);
650a839cd57SHans de Goede 	} else {
65102d04254SHans de Goede 		f->st.x = ((p[1] & 0x7f) << 4) | ((p[4] & 0x30) >> 2) |
652f85e5001SKevin Cernekee 		       ((p[0] & 0x30) >> 4);
65302d04254SHans de Goede 		f->st.y = ((p[2] & 0x7f) << 4) | (p[4] & 0x0f);
65402d04254SHans de Goede 		f->pressure = p[5] & 0x7f;
655f85e5001SKevin Cernekee 
656f85e5001SKevin Cernekee 		alps_decode_buttons_v3(f, p);
657a839cd57SHans de Goede 	}
65838c11eaaSHans de Goede 
65938c11eaaSHans de Goede 	return 0;
660f85e5001SKevin Cernekee }
661f85e5001SKevin Cernekee 
66238c11eaaSHans de Goede static int alps_decode_rushmore(struct alps_fields *f, unsigned char *p,
663ee65d4b3SYunkang Tang 				 struct psmouse *psmouse)
6641302bac3SKevin Cernekee {
665aab9cf7bSHans de Goede 	f->first_mp = !!(p[4] & 0x40);
666f105e34aSYunkang Tang 	f->is_mp = !!(p[5] & 0x40);
667aab9cf7bSHans de Goede 
668a839cd57SHans de Goede 	if (f->is_mp) {
669f105e34aSYunkang Tang 		f->fingers = max((p[5] & 0x3), ((p[5] >> 2) & 0x3)) + 1;
670aab9cf7bSHans de Goede 		f->x_map = ((p[5] & 0x10) << 11) |
671aab9cf7bSHans de Goede 			   ((p[4] & 0x7e) << 8) |
672aab9cf7bSHans de Goede 			   ((p[1] & 0x7f) << 2) |
673aab9cf7bSHans de Goede 			   ((p[0] & 0x30) >> 4);
674aab9cf7bSHans de Goede 		f->y_map = ((p[5] & 0x20) << 6) |
675aab9cf7bSHans de Goede 			   ((p[3] & 0x70) << 4) |
676aab9cf7bSHans de Goede 			   ((p[2] & 0x7f) << 1) |
677aab9cf7bSHans de Goede 			   (p[4] & 0x01);
678a839cd57SHans de Goede 	} else {
679aab9cf7bSHans de Goede 		f->st.x = ((p[1] & 0x7f) << 4) | ((p[4] & 0x30) >> 2) |
680aab9cf7bSHans de Goede 		       ((p[0] & 0x30) >> 4);
681aab9cf7bSHans de Goede 		f->st.y = ((p[2] & 0x7f) << 4) | (p[4] & 0x0f);
682aab9cf7bSHans de Goede 		f->pressure = p[5] & 0x7f;
683aab9cf7bSHans de Goede 
684aab9cf7bSHans de Goede 		alps_decode_buttons_v3(f, p);
685a839cd57SHans de Goede 	}
68638c11eaaSHans de Goede 
68738c11eaaSHans de Goede 	return 0;
6881302bac3SKevin Cernekee }
6891302bac3SKevin Cernekee 
69038c11eaaSHans de Goede static int alps_decode_dolphin(struct alps_fields *f, unsigned char *p,
691ee65d4b3SYunkang Tang 				struct psmouse *psmouse)
69275af9e56SDave Turvene {
693ee65d4b3SYunkang Tang 	u64 palm_data = 0;
694ee65d4b3SYunkang Tang 	struct alps_data *priv = psmouse->private;
695ee65d4b3SYunkang Tang 
69675af9e56SDave Turvene 	f->first_mp = !!(p[0] & 0x02);
69775af9e56SDave Turvene 	f->is_mp = !!(p[0] & 0x20);
69875af9e56SDave Turvene 
699ee65d4b3SYunkang Tang 	if (!f->is_mp) {
70002d04254SHans de Goede 		f->st.x = ((p[1] & 0x7f) | ((p[4] & 0x0f) << 7));
70102d04254SHans de Goede 		f->st.y = ((p[2] & 0x7f) | ((p[4] & 0xf0) << 3));
70202d04254SHans de Goede 		f->pressure = (p[0] & 4) ? 0 : p[5] & 0x7f;
70375af9e56SDave Turvene 		alps_decode_buttons_v3(f, p);
704ee65d4b3SYunkang Tang 	} else {
705ee65d4b3SYunkang Tang 		f->fingers = ((p[0] & 0x6) >> 1 |
706ee65d4b3SYunkang Tang 		     (p[0] & 0x10) >> 2);
707ee65d4b3SYunkang Tang 
708ee65d4b3SYunkang Tang 		palm_data = (p[1] & 0x7f) |
709ee65d4b3SYunkang Tang 			    ((p[2] & 0x7f) << 7) |
710ee65d4b3SYunkang Tang 			    ((p[4] & 0x7f) << 14) |
711ee65d4b3SYunkang Tang 			    ((p[5] & 0x7f) << 21) |
712ee65d4b3SYunkang Tang 			    ((p[3] & 0x07) << 28) |
713ee65d4b3SYunkang Tang 			    (((u64)p[3] & 0x70) << 27) |
714ee65d4b3SYunkang Tang 			    (((u64)p[0] & 0x01) << 34);
715ee65d4b3SYunkang Tang 
716ee65d4b3SYunkang Tang 		/* Y-profile is stored in P(0) to p(n-1), n = y_bits; */
717ee65d4b3SYunkang Tang 		f->y_map = palm_data & (BIT(priv->y_bits) - 1);
718ee65d4b3SYunkang Tang 
719ee65d4b3SYunkang Tang 		/* X-profile is stored in p(n) to p(n+m-1), m = x_bits; */
720ee65d4b3SYunkang Tang 		f->x_map = (palm_data >> priv->y_bits) &
721ee65d4b3SYunkang Tang 			   (BIT(priv->x_bits) - 1);
722ee65d4b3SYunkang Tang 	}
72338c11eaaSHans de Goede 
72438c11eaaSHans de Goede 	return 0;
72575af9e56SDave Turvene }
72675af9e56SDave Turvene 
727ee65d4b3SYunkang Tang static void alps_process_touchpad_packet_v3_v5(struct psmouse *psmouse)
72825bded7cSSeth Forshee {
72925bded7cSSeth Forshee 	struct alps_data *priv = psmouse->private;
73025bded7cSSeth Forshee 	unsigned char *packet = psmouse->packet;
73125bded7cSSeth Forshee 	struct input_dev *dev2 = priv->dev2;
73202d04254SHans de Goede 	struct alps_fields *f = &priv->f;
73302d04254SHans de Goede 	int fingers = 0;
734f85e5001SKevin Cernekee 
73502d04254SHans de Goede 	memset(f, 0, sizeof(*f));
73602d04254SHans de Goede 
73702d04254SHans de Goede 	priv->decode_fields(f, packet, psmouse);
73825bded7cSSeth Forshee 
73925bded7cSSeth Forshee 	/*
74001ce661fSSeth Forshee 	 * There's no single feature of touchpad position and bitmap packets
74101ce661fSSeth Forshee 	 * that can be used to distinguish between them. We rely on the fact
74201ce661fSSeth Forshee 	 * that a bitmap packet should always follow a position packet with
74301ce661fSSeth Forshee 	 * bit 6 of packet[4] set.
74425bded7cSSeth Forshee 	 */
74525bded7cSSeth Forshee 	if (priv->multi_packet) {
74625bded7cSSeth Forshee 		/*
74725bded7cSSeth Forshee 		 * Sometimes a position packet will indicate a multi-packet
74825bded7cSSeth Forshee 		 * sequence, but then what follows is another position
74925bded7cSSeth Forshee 		 * packet. Check for this, and when it happens process the
75025bded7cSSeth Forshee 		 * position packet as usual.
75125bded7cSSeth Forshee 		 */
75202d04254SHans de Goede 		if (f->is_mp) {
75302d04254SHans de Goede 			fingers = f->fingers;
75444b77f38SHans de Goede 			/*
75544b77f38SHans de Goede 			 * Bitmap processing uses position packet's coordinate
75644b77f38SHans de Goede 			 * data, so we need to do decode it first.
75744b77f38SHans de Goede 			 */
75844b77f38SHans de Goede 			priv->decode_fields(f, priv->multi_data, psmouse);
75902d04254SHans de Goede 			if (alps_process_bitmap(priv, f) == 0)
76020bea68bSHans de Goede 				fingers = 0; /* Use st data */
76101ce661fSSeth Forshee 		} else {
76201ce661fSSeth Forshee 			priv->multi_packet = 0;
76325bded7cSSeth Forshee 		}
76425bded7cSSeth Forshee 	}
76525bded7cSSeth Forshee 
76601ce661fSSeth Forshee 	/*
76701ce661fSSeth Forshee 	 * Bit 6 of byte 0 is not usually set in position packets. The only
76801ce661fSSeth Forshee 	 * times it seems to be set is in situations where the data is
76901ce661fSSeth Forshee 	 * suspect anyway, e.g. a palm resting flat on the touchpad. Given
77001ce661fSSeth Forshee 	 * this combined with the fact that this bit is useful for filtering
77101ce661fSSeth Forshee 	 * out misidentified bitmap packets, we reject anything with this
77201ce661fSSeth Forshee 	 * bit set.
77301ce661fSSeth Forshee 	 */
77402d04254SHans de Goede 	if (f->is_mp)
77501ce661fSSeth Forshee 		return;
77601ce661fSSeth Forshee 
77702d04254SHans de Goede 	if (!priv->multi_packet && f->first_mp) {
77825bded7cSSeth Forshee 		priv->multi_packet = 1;
77901ce661fSSeth Forshee 		memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
78001ce661fSSeth Forshee 		return;
78101ce661fSSeth Forshee 	}
78201ce661fSSeth Forshee 
78325bded7cSSeth Forshee 	priv->multi_packet = 0;
78425bded7cSSeth Forshee 
78525bded7cSSeth Forshee 	/*
78625bded7cSSeth Forshee 	 * Sometimes the hardware sends a single packet with z = 0
78725bded7cSSeth Forshee 	 * in the middle of a stream. Real releases generate packets
78825bded7cSSeth Forshee 	 * with x, y, and z all zero, so these seem to be flukes.
78925bded7cSSeth Forshee 	 * Ignore them.
79025bded7cSSeth Forshee 	 */
79102d04254SHans de Goede 	if (f->st.x && f->st.y && !f->pressure)
79225bded7cSSeth Forshee 		return;
79325bded7cSSeth Forshee 
79468c21870SHans de Goede 	alps_report_semi_mt_data(psmouse, fingers);
79525bded7cSSeth Forshee 
79634412ba2SPali Rohár 	if ((priv->flags & ALPS_DUALPOINT) &&
79734412ba2SPali Rohár 	    !(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS)) {
79802d04254SHans de Goede 		input_report_key(dev2, BTN_LEFT, f->ts_left);
79902d04254SHans de Goede 		input_report_key(dev2, BTN_RIGHT, f->ts_right);
80002d04254SHans de Goede 		input_report_key(dev2, BTN_MIDDLE, f->ts_middle);
80125bded7cSSeth Forshee 		input_sync(dev2);
80225bded7cSSeth Forshee 	}
80325bded7cSSeth Forshee }
80425bded7cSSeth Forshee 
80525bded7cSSeth Forshee static void alps_process_packet_v3(struct psmouse *psmouse)
80625bded7cSSeth Forshee {
80725bded7cSSeth Forshee 	unsigned char *packet = psmouse->packet;
80825bded7cSSeth Forshee 
80925bded7cSSeth Forshee 	/*
81025bded7cSSeth Forshee 	 * v3 protocol packets come in three types, two representing
81125bded7cSSeth Forshee 	 * touchpad data and one representing trackstick data.
81225bded7cSSeth Forshee 	 * Trackstick packets seem to be distinguished by always
81325bded7cSSeth Forshee 	 * having 0x3f in the last byte. This value has never been
81425bded7cSSeth Forshee 	 * observed in the last byte of either of the other types
81525bded7cSSeth Forshee 	 * of packets.
81625bded7cSSeth Forshee 	 */
81725bded7cSSeth Forshee 	if (packet[5] == 0x3f) {
81825bded7cSSeth Forshee 		alps_process_trackstick_packet_v3(psmouse);
81925bded7cSSeth Forshee 		return;
82025bded7cSSeth Forshee 	}
82125bded7cSSeth Forshee 
822ee65d4b3SYunkang Tang 	alps_process_touchpad_packet_v3_v5(psmouse);
82325bded7cSSeth Forshee }
82425bded7cSSeth Forshee 
82595f75e91SYunkang Tang static void alps_process_packet_v6(struct psmouse *psmouse)
82695f75e91SYunkang Tang {
82795f75e91SYunkang Tang 	struct alps_data *priv = psmouse->private;
82895f75e91SYunkang Tang 	unsigned char *packet = psmouse->packet;
82995f75e91SYunkang Tang 	struct input_dev *dev = psmouse->dev;
83095f75e91SYunkang Tang 	struct input_dev *dev2 = priv->dev2;
8311ef85805SDmitry Torokhov 	int x, y, z;
83295f75e91SYunkang Tang 
83395f75e91SYunkang Tang 	/*
83495f75e91SYunkang Tang 	 * We can use Byte5 to distinguish if the packet is from Touchpad
83595f75e91SYunkang Tang 	 * or Trackpoint.
83695f75e91SYunkang Tang 	 * Touchpad:	0 - 0x7E
83795f75e91SYunkang Tang 	 * Trackpoint:	0x7F
83895f75e91SYunkang Tang 	 */
83995f75e91SYunkang Tang 	if (packet[5] == 0x7F) {
84095f75e91SYunkang Tang 		/* It should be a DualPoint when received Trackpoint packet */
84134412ba2SPali Rohár 		if (!(priv->flags & ALPS_DUALPOINT)) {
84234412ba2SPali Rohár 			psmouse_warn(psmouse,
84334412ba2SPali Rohár 				     "Rejected trackstick packet from non DualPoint device");
84495f75e91SYunkang Tang 			return;
84534412ba2SPali Rohár 		}
84695f75e91SYunkang Tang 
84795f75e91SYunkang Tang 		/* Trackpoint packet */
84895f75e91SYunkang Tang 		x = packet[1] | ((packet[3] & 0x20) << 2);
84995f75e91SYunkang Tang 		y = packet[2] | ((packet[3] & 0x40) << 1);
85095f75e91SYunkang Tang 		z = packet[4];
85195f75e91SYunkang Tang 
85295f75e91SYunkang Tang 		/* To prevent the cursor jump when finger lifted */
85395f75e91SYunkang Tang 		if (x == 0x7F && y == 0x7F && z == 0x7F)
85495f75e91SYunkang Tang 			x = y = z = 0;
85595f75e91SYunkang Tang 
85695f75e91SYunkang Tang 		/* Divide 4 since trackpoint's speed is too fast */
85795f75e91SYunkang Tang 		input_report_rel(dev2, REL_X, (char)x / 4);
85895f75e91SYunkang Tang 		input_report_rel(dev2, REL_Y, -((char)y / 4));
85995f75e91SYunkang Tang 
8601ef85805SDmitry Torokhov 		psmouse_report_standard_buttons(dev2, packet[3]);
86195f75e91SYunkang Tang 
86295f75e91SYunkang Tang 		input_sync(dev2);
86395f75e91SYunkang Tang 		return;
86495f75e91SYunkang Tang 	}
86595f75e91SYunkang Tang 
86695f75e91SYunkang Tang 	/* Touchpad packet */
86795f75e91SYunkang Tang 	x = packet[1] | ((packet[3] & 0x78) << 4);
86895f75e91SYunkang Tang 	y = packet[2] | ((packet[4] & 0x78) << 4);
86995f75e91SYunkang Tang 	z = packet[5];
87095f75e91SYunkang Tang 
87195f75e91SYunkang Tang 	if (z > 30)
87295f75e91SYunkang Tang 		input_report_key(dev, BTN_TOUCH, 1);
87395f75e91SYunkang Tang 	if (z < 25)
87495f75e91SYunkang Tang 		input_report_key(dev, BTN_TOUCH, 0);
87595f75e91SYunkang Tang 
87695f75e91SYunkang Tang 	if (z > 0) {
87795f75e91SYunkang Tang 		input_report_abs(dev, ABS_X, x);
87895f75e91SYunkang Tang 		input_report_abs(dev, ABS_Y, y);
87995f75e91SYunkang Tang 	}
88095f75e91SYunkang Tang 
88195f75e91SYunkang Tang 	input_report_abs(dev, ABS_PRESSURE, z);
88295f75e91SYunkang Tang 	input_report_key(dev, BTN_TOOL_FINGER, z > 0);
88395f75e91SYunkang Tang 
88495f75e91SYunkang Tang 	/* v6 touchpad does not have middle button */
8851ef85805SDmitry Torokhov 	packet[3] &= ~BIT(2);
8861ef85805SDmitry Torokhov 	psmouse_report_standard_buttons(dev2, packet[3]);
88795f75e91SYunkang Tang 
88895f75e91SYunkang Tang 	input_sync(dev);
88995f75e91SYunkang Tang }
89095f75e91SYunkang Tang 
89125bded7cSSeth Forshee static void alps_process_packet_v4(struct psmouse *psmouse)
89225bded7cSSeth Forshee {
8933b7e09faSGeorge Pantalos 	struct alps_data *priv = psmouse->private;
89425bded7cSSeth Forshee 	unsigned char *packet = psmouse->packet;
89502d04254SHans de Goede 	struct alps_fields *f = &priv->f;
89668c21870SHans de Goede 	int offset;
8973b7e09faSGeorge Pantalos 
8983b7e09faSGeorge Pantalos 	/*
8993b7e09faSGeorge Pantalos 	 * v4 has a 6-byte encoding for bitmap data, but this data is
9003b7e09faSGeorge Pantalos 	 * broken up between 3 normal packets. Use priv->multi_packet to
9013b7e09faSGeorge Pantalos 	 * track our position in the bitmap packet.
9023b7e09faSGeorge Pantalos 	 */
9033b7e09faSGeorge Pantalos 	if (packet[6] & 0x40) {
9043b7e09faSGeorge Pantalos 		/* sync, reset position */
9053b7e09faSGeorge Pantalos 		priv->multi_packet = 0;
9063b7e09faSGeorge Pantalos 	}
9073b7e09faSGeorge Pantalos 
9083b7e09faSGeorge Pantalos 	if (WARN_ON_ONCE(priv->multi_packet > 2))
9093b7e09faSGeorge Pantalos 		return;
9103b7e09faSGeorge Pantalos 
9113b7e09faSGeorge Pantalos 	offset = 2 * priv->multi_packet;
9123b7e09faSGeorge Pantalos 	priv->multi_data[offset] = packet[6];
9133b7e09faSGeorge Pantalos 	priv->multi_data[offset + 1] = packet[7];
9143b7e09faSGeorge Pantalos 
91544b77f38SHans de Goede 	f->left = !!(packet[4] & 0x01);
91644b77f38SHans de Goede 	f->right = !!(packet[4] & 0x02);
91744b77f38SHans de Goede 
91844b77f38SHans de Goede 	f->st.x = ((packet[1] & 0x7f) << 4) | ((packet[3] & 0x30) >> 2) |
91944b77f38SHans de Goede 		  ((packet[0] & 0x30) >> 4);
92044b77f38SHans de Goede 	f->st.y = ((packet[2] & 0x7f) << 4) | (packet[3] & 0x0f);
92144b77f38SHans de Goede 	f->pressure = packet[5] & 0x7f;
92244b77f38SHans de Goede 
9233b7e09faSGeorge Pantalos 	if (++priv->multi_packet > 2) {
9243b7e09faSGeorge Pantalos 		priv->multi_packet = 0;
9253b7e09faSGeorge Pantalos 
92602d04254SHans de Goede 		f->x_map = ((priv->multi_data[2] & 0x1f) << 10) |
9273b7e09faSGeorge Pantalos 			   ((priv->multi_data[3] & 0x60) << 3) |
9283b7e09faSGeorge Pantalos 			   ((priv->multi_data[0] & 0x3f) << 2) |
9293b7e09faSGeorge Pantalos 			   ((priv->multi_data[1] & 0x60) >> 5);
93002d04254SHans de Goede 		f->y_map = ((priv->multi_data[5] & 0x01) << 10) |
9313b7e09faSGeorge Pantalos 			   ((priv->multi_data[3] & 0x1f) << 5) |
9323b7e09faSGeorge Pantalos 			    (priv->multi_data[1] & 0x1f);
9333b7e09faSGeorge Pantalos 
93402d04254SHans de Goede 		f->fingers = alps_process_bitmap(priv, f);
9353b7e09faSGeorge Pantalos 	}
93625bded7cSSeth Forshee 
93768c21870SHans de Goede 	alps_report_semi_mt_data(psmouse, f->fingers);
93825bded7cSSeth Forshee }
93925bded7cSSeth Forshee 
9403808843cSYunkang Tang static bool alps_is_valid_package_v7(struct psmouse *psmouse)
9413808843cSYunkang Tang {
9423808843cSYunkang Tang 	switch (psmouse->pktcnt) {
9433808843cSYunkang Tang 	case 3:
9443808843cSYunkang Tang 		return (psmouse->packet[2] & 0x40) == 0x40;
9453808843cSYunkang Tang 	case 4:
9463808843cSYunkang Tang 		return (psmouse->packet[3] & 0x48) == 0x48;
9473808843cSYunkang Tang 	case 6:
9483808843cSYunkang Tang 		return (psmouse->packet[5] & 0x40) == 0x00;
9493808843cSYunkang Tang 	}
9503808843cSYunkang Tang 	return true;
9513808843cSYunkang Tang }
9523808843cSYunkang Tang 
9533808843cSYunkang Tang static unsigned char alps_get_packet_id_v7(char *byte)
9543808843cSYunkang Tang {
9553808843cSYunkang Tang 	unsigned char packet_id;
9563808843cSYunkang Tang 
9573808843cSYunkang Tang 	if (byte[4] & 0x40)
9583808843cSYunkang Tang 		packet_id = V7_PACKET_ID_TWO;
9593808843cSYunkang Tang 	else if (byte[4] & 0x01)
9603808843cSYunkang Tang 		packet_id = V7_PACKET_ID_MULTI;
9613808843cSYunkang Tang 	else if ((byte[0] & 0x10) && !(byte[4] & 0x43))
9623808843cSYunkang Tang 		packet_id = V7_PACKET_ID_NEW;
9633808843cSYunkang Tang 	else if (byte[1] == 0x00 && byte[4] == 0x00)
9643808843cSYunkang Tang 		packet_id = V7_PACKET_ID_IDLE;
9653808843cSYunkang Tang 	else
9663808843cSYunkang Tang 		packet_id = V7_PACKET_ID_UNKNOWN;
9673808843cSYunkang Tang 
9683808843cSYunkang Tang 	return packet_id;
9693808843cSYunkang Tang }
9703808843cSYunkang Tang 
9713808843cSYunkang Tang static void alps_get_finger_coordinate_v7(struct input_mt_pos *mt,
9723808843cSYunkang Tang 					  unsigned char *pkt,
9733808843cSYunkang Tang 					  unsigned char pkt_id)
9743808843cSYunkang Tang {
9753808843cSYunkang Tang 	mt[0].x = ((pkt[2] & 0x80) << 4);
9763808843cSYunkang Tang 	mt[0].x |= ((pkt[2] & 0x3F) << 5);
9773808843cSYunkang Tang 	mt[0].x |= ((pkt[3] & 0x30) >> 1);
9783808843cSYunkang Tang 	mt[0].x |= (pkt[3] & 0x07);
9793808843cSYunkang Tang 	mt[0].y = (pkt[1] << 3) | (pkt[0] & 0x07);
9803808843cSYunkang Tang 
9813808843cSYunkang Tang 	mt[1].x = ((pkt[3] & 0x80) << 4);
9823808843cSYunkang Tang 	mt[1].x |= ((pkt[4] & 0x80) << 3);
9833808843cSYunkang Tang 	mt[1].x |= ((pkt[4] & 0x3F) << 4);
9843808843cSYunkang Tang 	mt[1].y = ((pkt[5] & 0x80) << 3);
9853808843cSYunkang Tang 	mt[1].y |= ((pkt[5] & 0x3F) << 4);
9863808843cSYunkang Tang 
9873808843cSYunkang Tang 	switch (pkt_id) {
9883808843cSYunkang Tang 	case V7_PACKET_ID_TWO:
9893808843cSYunkang Tang 		mt[1].x &= ~0x000F;
9903808843cSYunkang Tang 		mt[1].y |= 0x000F;
99172eceab7SHans de Goede 		/* Detect false-postive touches where x & y report max value */
99272eceab7SHans de Goede 		if (mt[1].y == 0x7ff && mt[1].x == 0xff0) {
99372eceab7SHans de Goede 			mt[1].x = 0;
99472eceab7SHans de Goede 			/* y gets set to 0 at the end of this function */
99572eceab7SHans de Goede 		}
9963808843cSYunkang Tang 		break;
9973808843cSYunkang Tang 
9983808843cSYunkang Tang 	case V7_PACKET_ID_MULTI:
9993808843cSYunkang Tang 		mt[1].x &= ~0x003F;
10003808843cSYunkang Tang 		mt[1].y &= ~0x0020;
10013808843cSYunkang Tang 		mt[1].y |= ((pkt[4] & 0x02) << 4);
10023808843cSYunkang Tang 		mt[1].y |= 0x001F;
10033808843cSYunkang Tang 		break;
10043808843cSYunkang Tang 
10053808843cSYunkang Tang 	case V7_PACKET_ID_NEW:
10063808843cSYunkang Tang 		mt[1].x &= ~0x003F;
10073808843cSYunkang Tang 		mt[1].x |= (pkt[0] & 0x20);
10083808843cSYunkang Tang 		mt[1].y |= 0x000F;
10093808843cSYunkang Tang 		break;
10103808843cSYunkang Tang 	}
10113808843cSYunkang Tang 
10123808843cSYunkang Tang 	mt[0].y = 0x7FF - mt[0].y;
10133808843cSYunkang Tang 	mt[1].y = 0x7FF - mt[1].y;
10143808843cSYunkang Tang }
10153808843cSYunkang Tang 
10163808843cSYunkang Tang static int alps_get_mt_count(struct input_mt_pos *mt)
10173808843cSYunkang Tang {
10187091c443SHans de Goede 	int i, fingers = 0;
10193808843cSYunkang Tang 
10207091c443SHans de Goede 	for (i = 0; i < MAX_TOUCHES; i++) {
10217091c443SHans de Goede 		if (mt[i].x != 0 || mt[i].y != 0)
10227091c443SHans de Goede 			fingers++;
10237091c443SHans de Goede 	}
10243808843cSYunkang Tang 
10257091c443SHans de Goede 	return fingers;
10263808843cSYunkang Tang }
10273808843cSYunkang Tang 
10283808843cSYunkang Tang static int alps_decode_packet_v7(struct alps_fields *f,
10293808843cSYunkang Tang 				  unsigned char *p,
10303808843cSYunkang Tang 				  struct psmouse *psmouse)
10313808843cSYunkang Tang {
1032d27eb793SHans de Goede 	struct alps_data *priv = psmouse->private;
10333808843cSYunkang Tang 	unsigned char pkt_id;
10343808843cSYunkang Tang 
10353808843cSYunkang Tang 	pkt_id = alps_get_packet_id_v7(p);
10363808843cSYunkang Tang 	if (pkt_id == V7_PACKET_ID_IDLE)
10373808843cSYunkang Tang 		return 0;
10383808843cSYunkang Tang 	if (pkt_id == V7_PACKET_ID_UNKNOWN)
10393808843cSYunkang Tang 		return -1;
10408b238115SHans de Goede 	/*
10418b238115SHans de Goede 	 * NEW packets are send to indicate a discontinuity in the finger
10428b238115SHans de Goede 	 * coordinate reporting. Specifically a finger may have moved from
10438b238115SHans de Goede 	 * slot 0 to 1 or vice versa. INPUT_MT_TRACK takes care of this for
10448b238115SHans de Goede 	 * us.
10458b238115SHans de Goede 	 *
10468b238115SHans de Goede 	 * NEW packets have 3 problems:
10478b238115SHans de Goede 	 * 1) They do not contain middle / right button info (on non clickpads)
10488b238115SHans de Goede 	 *    this can be worked around by preserving the old button state
10498b238115SHans de Goede 	 * 2) They do not contain an accurate fingercount, and they are
10508b238115SHans de Goede 	 *    typically send when the number of fingers changes. We cannot use
10518b238115SHans de Goede 	 *    the old finger count as that may mismatch with the amount of
10528b238115SHans de Goede 	 *    touch coordinates we've available in the NEW packet
10538b238115SHans de Goede 	 * 3) Their x data for the second touch is inaccurate leading to
10548b238115SHans de Goede 	 *    a possible jump of the x coordinate by 16 units when the first
10558b238115SHans de Goede 	 *    non NEW packet comes in
10568b238115SHans de Goede 	 * Since problems 2 & 3 cannot be worked around, just ignore them.
10578b238115SHans de Goede 	 */
10588b238115SHans de Goede 	if (pkt_id == V7_PACKET_ID_NEW)
10598b238115SHans de Goede 		return 1;
10603808843cSYunkang Tang 
10613808843cSYunkang Tang 	alps_get_finger_coordinate_v7(f->mt, p, pkt_id);
10623808843cSYunkang Tang 
10633808843cSYunkang Tang 	if (pkt_id == V7_PACKET_ID_TWO)
10643808843cSYunkang Tang 		f->fingers = alps_get_mt_count(f->mt);
10658b238115SHans de Goede 	else /* pkt_id == V7_PACKET_ID_MULTI */
10663808843cSYunkang Tang 		f->fingers = 3 + (p[5] & 0x03);
10673808843cSYunkang Tang 
1068d27eb793SHans de Goede 	f->left = (p[0] & 0x80) >> 7;
1069d27eb793SHans de Goede 	if (priv->flags & ALPS_BUTTONPAD) {
1070d27eb793SHans de Goede 		if (p[0] & 0x20)
1071d27eb793SHans de Goede 			f->fingers++;
1072d27eb793SHans de Goede 		if (p[0] & 0x10)
1073d27eb793SHans de Goede 			f->fingers++;
1074d27eb793SHans de Goede 	} else {
1075d27eb793SHans de Goede 		f->right = (p[0] & 0x20) >> 5;
1076d27eb793SHans de Goede 		f->middle = (p[0] & 0x10) >> 4;
1077d27eb793SHans de Goede 	}
1078d27eb793SHans de Goede 
10797091c443SHans de Goede 	/* Sometimes a single touch is reported in mt[1] rather then mt[0] */
10807091c443SHans de Goede 	if (f->fingers == 1 && f->mt[0].x == 0 && f->mt[0].y == 0) {
10817091c443SHans de Goede 		f->mt[0].x = f->mt[1].x;
10827091c443SHans de Goede 		f->mt[0].y = f->mt[1].y;
10837091c443SHans de Goede 		f->mt[1].x = 0;
10847091c443SHans de Goede 		f->mt[1].y = 0;
10857091c443SHans de Goede 	}
10867091c443SHans de Goede 
10873808843cSYunkang Tang 	return 0;
10883808843cSYunkang Tang }
10893808843cSYunkang Tang 
10903808843cSYunkang Tang static void alps_process_trackstick_packet_v7(struct psmouse *psmouse)
10913808843cSYunkang Tang {
10923808843cSYunkang Tang 	struct alps_data *priv = psmouse->private;
10933808843cSYunkang Tang 	unsigned char *packet = psmouse->packet;
10943808843cSYunkang Tang 	struct input_dev *dev2 = priv->dev2;
10951ef85805SDmitry Torokhov 	int x, y, z;
10963808843cSYunkang Tang 
109734412ba2SPali Rohár 	/* It should be a DualPoint when received trackstick packet */
109834412ba2SPali Rohár 	if (!(priv->flags & ALPS_DUALPOINT)) {
109934412ba2SPali Rohár 		psmouse_warn(psmouse,
110034412ba2SPali Rohár 			     "Rejected trackstick packet from non DualPoint device");
110134412ba2SPali Rohár 		return;
110234412ba2SPali Rohár 	}
110334412ba2SPali Rohár 
11043808843cSYunkang Tang 	x = ((packet[2] & 0xbf)) | ((packet[3] & 0x10) << 2);
11053808843cSYunkang Tang 	y = (packet[3] & 0x07) | (packet[4] & 0xb8) |
11063808843cSYunkang Tang 	    ((packet[3] & 0x20) << 1);
11073808843cSYunkang Tang 	z = (packet[5] & 0x3f) | ((packet[3] & 0x80) >> 1);
11083808843cSYunkang Tang 
1109088df2ccSHans de Goede 	input_report_rel(dev2, REL_X, (char)x);
1110088df2ccSHans de Goede 	input_report_rel(dev2, REL_Y, -((char)y));
11114621c966SPali Rohár 	input_report_abs(dev2, ABS_PRESSURE, z);
11123808843cSYunkang Tang 
11131ef85805SDmitry Torokhov 	psmouse_report_standard_buttons(dev2, packet[1]);
11143808843cSYunkang Tang 
11153808843cSYunkang Tang 	input_sync(dev2);
11163808843cSYunkang Tang }
11173808843cSYunkang Tang 
11183808843cSYunkang Tang static void alps_process_touchpad_packet_v7(struct psmouse *psmouse)
11193808843cSYunkang Tang {
11203808843cSYunkang Tang 	struct alps_data *priv = psmouse->private;
11213808843cSYunkang Tang 	struct input_dev *dev = psmouse->dev;
11223808843cSYunkang Tang 	struct alps_fields *f = &priv->f;
11233808843cSYunkang Tang 
11243808843cSYunkang Tang 	memset(f, 0, sizeof(*f));
11253808843cSYunkang Tang 
11263808843cSYunkang Tang 	if (priv->decode_fields(f, psmouse->packet, psmouse))
11273808843cSYunkang Tang 		return;
11283808843cSYunkang Tang 
11293808843cSYunkang Tang 	alps_report_mt_data(psmouse, alps_get_mt_count(f->mt));
11303808843cSYunkang Tang 
11313808843cSYunkang Tang 	input_mt_report_finger_count(dev, f->fingers);
11323808843cSYunkang Tang 
11333808843cSYunkang Tang 	input_report_key(dev, BTN_LEFT, f->left);
11343808843cSYunkang Tang 	input_report_key(dev, BTN_RIGHT, f->right);
11353808843cSYunkang Tang 	input_report_key(dev, BTN_MIDDLE, f->middle);
11363808843cSYunkang Tang 
11373808843cSYunkang Tang 	input_sync(dev);
11383808843cSYunkang Tang }
11393808843cSYunkang Tang 
11403808843cSYunkang Tang static void alps_process_packet_v7(struct psmouse *psmouse)
11413808843cSYunkang Tang {
11423808843cSYunkang Tang 	unsigned char *packet = psmouse->packet;
11433808843cSYunkang Tang 
11443808843cSYunkang Tang 	if (packet[0] == 0x48 && (packet[4] & 0x47) == 0x06)
11453808843cSYunkang Tang 		alps_process_trackstick_packet_v7(psmouse);
11463808843cSYunkang Tang 	else
11473808843cSYunkang Tang 		alps_process_touchpad_packet_v7(psmouse);
11483808843cSYunkang Tang }
11493808843cSYunkang Tang 
115023fce365SPaul Donohue static enum SS4_PACKET_ID alps_get_pkt_id_ss4_v2(unsigned char *byte)
11513db5b9f7SMasaki Ota {
115223fce365SPaul Donohue 	enum SS4_PACKET_ID pkt_id = SS4_PACKET_ID_IDLE;
11533db5b9f7SMasaki Ota 
11544777ac22SBen Gamari 	switch (byte[3] & 0x30) {
11554777ac22SBen Gamari 	case 0x00:
115623fce365SPaul Donohue 		if (SS4_IS_IDLE_V2(byte)) {
11573db5b9f7SMasaki Ota 			pkt_id = SS4_PACKET_ID_IDLE;
11583db5b9f7SMasaki Ota 		} else {
11594777ac22SBen Gamari 			pkt_id = SS4_PACKET_ID_ONE;
11604777ac22SBen Gamari 		}
11614777ac22SBen Gamari 		break;
11624777ac22SBen Gamari 	case 0x10:
11634777ac22SBen Gamari 		/* two-finger finger positions */
11644777ac22SBen Gamari 		pkt_id = SS4_PACKET_ID_TWO;
11654777ac22SBen Gamari 		break;
11664777ac22SBen Gamari 	case 0x20:
11674777ac22SBen Gamari 		/* stick pointer */
11684777ac22SBen Gamari 		pkt_id = SS4_PACKET_ID_STICK;
11694777ac22SBen Gamari 		break;
11704777ac22SBen Gamari 	case 0x30:
11714777ac22SBen Gamari 		/* third and fourth finger positions */
11723db5b9f7SMasaki Ota 		pkt_id = SS4_PACKET_ID_MULTI;
11734777ac22SBen Gamari 		break;
11743db5b9f7SMasaki Ota 	}
11753db5b9f7SMasaki Ota 
11763db5b9f7SMasaki Ota 	return pkt_id;
11773db5b9f7SMasaki Ota }
11783db5b9f7SMasaki Ota 
11793db5b9f7SMasaki Ota static int alps_decode_ss4_v2(struct alps_fields *f,
11803db5b9f7SMasaki Ota 			      unsigned char *p, struct psmouse *psmouse)
11813db5b9f7SMasaki Ota {
11823db5b9f7SMasaki Ota 	struct alps_data *priv = psmouse->private;
118323fce365SPaul Donohue 	enum SS4_PACKET_ID pkt_id;
11843db5b9f7SMasaki Ota 	unsigned int no_data_x, no_data_y;
11853db5b9f7SMasaki Ota 
11863db5b9f7SMasaki Ota 	pkt_id = alps_get_pkt_id_ss4_v2(p);
11873db5b9f7SMasaki Ota 
11883db5b9f7SMasaki Ota 	/* Current packet is 1Finger coordinate packet */
11893db5b9f7SMasaki Ota 	switch (pkt_id) {
11903db5b9f7SMasaki Ota 	case SS4_PACKET_ID_ONE:
11913db5b9f7SMasaki Ota 		f->mt[0].x = SS4_1F_X_V2(p);
11923db5b9f7SMasaki Ota 		f->mt[0].y = SS4_1F_Y_V2(p);
11933db5b9f7SMasaki Ota 		f->pressure = ((SS4_1F_Z_V2(p)) * 2) & 0x7f;
1194a8317763SBen Gamari 		/*
1195a8317763SBen Gamari 		 * When a button is held the device will give us events
1196a8317763SBen Gamari 		 * with x, y, and pressure of 0. This causes annoying jumps
1197a8317763SBen Gamari 		 * if a touch is released while the button is held.
1198a8317763SBen Gamari 		 * Handle this by claiming zero contacts.
1199a8317763SBen Gamari 		 */
1200a8317763SBen Gamari 		f->fingers = f->pressure > 0 ? 1 : 0;
12013db5b9f7SMasaki Ota 		f->first_mp = 0;
12023db5b9f7SMasaki Ota 		f->is_mp = 0;
12033db5b9f7SMasaki Ota 		break;
12043db5b9f7SMasaki Ota 
12053db5b9f7SMasaki Ota 	case SS4_PACKET_ID_TWO:
12063db5b9f7SMasaki Ota 		if (priv->flags & ALPS_BUTTONPAD) {
12074a646580SMasaki Ota 			if (IS_SS4PLUS_DEV(priv->dev_id)) {
12084a646580SMasaki Ota 				f->mt[0].x = SS4_PLUS_BTL_MF_X_V2(p, 0);
12094a646580SMasaki Ota 				f->mt[1].x = SS4_PLUS_BTL_MF_X_V2(p, 1);
12104a646580SMasaki Ota 			} else {
12113db5b9f7SMasaki Ota 				f->mt[0].x = SS4_BTL_MF_X_V2(p, 0);
12123db5b9f7SMasaki Ota 				f->mt[1].x = SS4_BTL_MF_X_V2(p, 1);
12134a646580SMasaki Ota 			}
12144a646580SMasaki Ota 			f->mt[0].y = SS4_BTL_MF_Y_V2(p, 0);
12153db5b9f7SMasaki Ota 			f->mt[1].y = SS4_BTL_MF_Y_V2(p, 1);
12163db5b9f7SMasaki Ota 		} else {
12174a646580SMasaki Ota 			if (IS_SS4PLUS_DEV(priv->dev_id)) {
12184a646580SMasaki Ota 				f->mt[0].x = SS4_PLUS_STD_MF_X_V2(p, 0);
12194a646580SMasaki Ota 				f->mt[1].x = SS4_PLUS_STD_MF_X_V2(p, 1);
12204a646580SMasaki Ota 			} else {
12213db5b9f7SMasaki Ota 				f->mt[0].x = SS4_STD_MF_X_V2(p, 0);
12223db5b9f7SMasaki Ota 				f->mt[1].x = SS4_STD_MF_X_V2(p, 1);
12234a646580SMasaki Ota 			}
12244a646580SMasaki Ota 			f->mt[0].y = SS4_STD_MF_Y_V2(p, 0);
12253db5b9f7SMasaki Ota 			f->mt[1].y = SS4_STD_MF_Y_V2(p, 1);
12263db5b9f7SMasaki Ota 		}
12273db5b9f7SMasaki Ota 		f->pressure = SS4_MF_Z_V2(p, 0) ? 0x30 : 0;
12283db5b9f7SMasaki Ota 
12293db5b9f7SMasaki Ota 		if (SS4_IS_MF_CONTINUE(p)) {
12303db5b9f7SMasaki Ota 			f->first_mp = 1;
12313db5b9f7SMasaki Ota 		} else {
12323db5b9f7SMasaki Ota 			f->fingers = 2;
12333db5b9f7SMasaki Ota 			f->first_mp = 0;
12343db5b9f7SMasaki Ota 		}
12353db5b9f7SMasaki Ota 		f->is_mp = 0;
12363db5b9f7SMasaki Ota 
12373db5b9f7SMasaki Ota 		break;
12383db5b9f7SMasaki Ota 
12393db5b9f7SMasaki Ota 	case SS4_PACKET_ID_MULTI:
12403db5b9f7SMasaki Ota 		if (priv->flags & ALPS_BUTTONPAD) {
12414a646580SMasaki Ota 			if (IS_SS4PLUS_DEV(priv->dev_id)) {
12424d94e776SNir Perry 				f->mt[2].x = SS4_PLUS_BTL_MF_X_V2(p, 0);
12434d94e776SNir Perry 				f->mt[3].x = SS4_PLUS_BTL_MF_X_V2(p, 1);
12444d94e776SNir Perry 				no_data_x = SS4_PLUS_MFPACKET_NO_AX_BL;
12454a646580SMasaki Ota 			} else {
12463db5b9f7SMasaki Ota 				f->mt[2].x = SS4_BTL_MF_X_V2(p, 0);
12473db5b9f7SMasaki Ota 				f->mt[3].x = SS4_BTL_MF_X_V2(p, 1);
12484d94e776SNir Perry 				no_data_x = SS4_MFPACKET_NO_AX_BL;
12494a646580SMasaki Ota 			}
12504d94e776SNir Perry 			no_data_y = SS4_MFPACKET_NO_AY_BL;
12514a646580SMasaki Ota 
12524a646580SMasaki Ota 			f->mt[2].y = SS4_BTL_MF_Y_V2(p, 0);
12533db5b9f7SMasaki Ota 			f->mt[3].y = SS4_BTL_MF_Y_V2(p, 1);
12543db5b9f7SMasaki Ota 		} else {
12554a646580SMasaki Ota 			if (IS_SS4PLUS_DEV(priv->dev_id)) {
12564d94e776SNir Perry 				f->mt[2].x = SS4_PLUS_STD_MF_X_V2(p, 0);
12574d94e776SNir Perry 				f->mt[3].x = SS4_PLUS_STD_MF_X_V2(p, 1);
12584d94e776SNir Perry 				no_data_x = SS4_PLUS_MFPACKET_NO_AX;
12594a646580SMasaki Ota 			} else {
12604d94e776SNir Perry 				f->mt[2].x = SS4_STD_MF_X_V2(p, 0);
12614d94e776SNir Perry 				f->mt[3].x = SS4_STD_MF_X_V2(p, 1);
12624d94e776SNir Perry 				no_data_x = SS4_MFPACKET_NO_AX;
12634a646580SMasaki Ota 			}
12644d94e776SNir Perry 			no_data_y = SS4_MFPACKET_NO_AY;
12654d94e776SNir Perry 
12663db5b9f7SMasaki Ota 			f->mt[2].y = SS4_STD_MF_Y_V2(p, 0);
12673db5b9f7SMasaki Ota 			f->mt[3].y = SS4_STD_MF_Y_V2(p, 1);
12683db5b9f7SMasaki Ota 		}
12693db5b9f7SMasaki Ota 
12703db5b9f7SMasaki Ota 		f->first_mp = 0;
12713db5b9f7SMasaki Ota 		f->is_mp = 1;
12723db5b9f7SMasaki Ota 
12733db5b9f7SMasaki Ota 		if (SS4_IS_5F_DETECTED(p)) {
12743db5b9f7SMasaki Ota 			f->fingers = 5;
12753db5b9f7SMasaki Ota 		} else if (f->mt[3].x == no_data_x &&
12763db5b9f7SMasaki Ota 			     f->mt[3].y == no_data_y) {
12773db5b9f7SMasaki Ota 			f->mt[3].x = 0;
12783db5b9f7SMasaki Ota 			f->mt[3].y = 0;
12793db5b9f7SMasaki Ota 			f->fingers = 3;
12803db5b9f7SMasaki Ota 		} else {
12813db5b9f7SMasaki Ota 			f->fingers = 4;
12823db5b9f7SMasaki Ota 		}
12833db5b9f7SMasaki Ota 		break;
12843db5b9f7SMasaki Ota 
12854777ac22SBen Gamari 	case SS4_PACKET_ID_STICK:
12867229c58cSPaul Donohue 		/*
12877229c58cSPaul Donohue 		 * x, y, and pressure are decoded in
12887229c58cSPaul Donohue 		 * alps_process_packet_ss4_v2()
12897229c58cSPaul Donohue 		 */
12907229c58cSPaul Donohue 		f->first_mp = 0;
12917229c58cSPaul Donohue 		f->is_mp = 0;
12924777ac22SBen Gamari 		break;
12934777ac22SBen Gamari 
12943db5b9f7SMasaki Ota 	case SS4_PACKET_ID_IDLE:
12953db5b9f7SMasaki Ota 	default:
12963db5b9f7SMasaki Ota 		memset(f, 0, sizeof(struct alps_fields));
12973db5b9f7SMasaki Ota 		break;
12983db5b9f7SMasaki Ota 	}
12993db5b9f7SMasaki Ota 
13004777ac22SBen Gamari 	/* handle buttons */
13014777ac22SBen Gamari 	if (pkt_id == SS4_PACKET_ID_STICK) {
13024777ac22SBen Gamari 		f->ts_left = !!(SS4_BTN_V2(p) & 0x01);
13034777ac22SBen Gamari 		f->ts_right = !!(SS4_BTN_V2(p) & 0x02);
13044777ac22SBen Gamari 		f->ts_middle = !!(SS4_BTN_V2(p) & 0x04);
13054777ac22SBen Gamari 	} else {
13063db5b9f7SMasaki Ota 		f->left = !!(SS4_BTN_V2(p) & 0x01);
13073db5b9f7SMasaki Ota 		if (!(priv->flags & ALPS_BUTTONPAD)) {
13083db5b9f7SMasaki Ota 			f->right = !!(SS4_BTN_V2(p) & 0x02);
13093db5b9f7SMasaki Ota 			f->middle = !!(SS4_BTN_V2(p) & 0x04);
13103db5b9f7SMasaki Ota 		}
13114777ac22SBen Gamari 	}
13123db5b9f7SMasaki Ota 
13133db5b9f7SMasaki Ota 	return 0;
13143db5b9f7SMasaki Ota }
13153db5b9f7SMasaki Ota 
13163db5b9f7SMasaki Ota static void alps_process_packet_ss4_v2(struct psmouse *psmouse)
13173db5b9f7SMasaki Ota {
13183db5b9f7SMasaki Ota 	struct alps_data *priv = psmouse->private;
13193db5b9f7SMasaki Ota 	unsigned char *packet = psmouse->packet;
13203db5b9f7SMasaki Ota 	struct input_dev *dev = psmouse->dev;
13214777ac22SBen Gamari 	struct input_dev *dev2 = priv->dev2;
13223db5b9f7SMasaki Ota 	struct alps_fields *f = &priv->f;
13233db5b9f7SMasaki Ota 
13243db5b9f7SMasaki Ota 	memset(f, 0, sizeof(struct alps_fields));
13253db5b9f7SMasaki Ota 	priv->decode_fields(f, packet, psmouse);
13263db5b9f7SMasaki Ota 	if (priv->multi_packet) {
13273db5b9f7SMasaki Ota 		/*
13283db5b9f7SMasaki Ota 		 * Sometimes the first packet will indicate a multi-packet
13293db5b9f7SMasaki Ota 		 * sequence, but sometimes the next multi-packet would not
13303db5b9f7SMasaki Ota 		 * come. Check for this, and when it happens process the
13313db5b9f7SMasaki Ota 		 * position packet as usual.
13323db5b9f7SMasaki Ota 		 */
13333db5b9f7SMasaki Ota 		if (f->is_mp) {
13343db5b9f7SMasaki Ota 			/* Now process the 1st packet */
13353db5b9f7SMasaki Ota 			priv->decode_fields(f, priv->multi_data, psmouse);
13363db5b9f7SMasaki Ota 		} else {
13373db5b9f7SMasaki Ota 			priv->multi_packet = 0;
13383db5b9f7SMasaki Ota 		}
13393db5b9f7SMasaki Ota 	}
13403db5b9f7SMasaki Ota 
13413db5b9f7SMasaki Ota 	/*
13423db5b9f7SMasaki Ota 	 * "f.is_mp" would always be '0' after merging the 1st and 2nd packet.
13433db5b9f7SMasaki Ota 	 * When it is set, it means 2nd packet comes without 1st packet come.
13443db5b9f7SMasaki Ota 	 */
13453db5b9f7SMasaki Ota 	if (f->is_mp)
13463db5b9f7SMasaki Ota 		return;
13473db5b9f7SMasaki Ota 
13483db5b9f7SMasaki Ota 	/* Save the first packet */
13493db5b9f7SMasaki Ota 	if (!priv->multi_packet && f->first_mp) {
13503db5b9f7SMasaki Ota 		priv->multi_packet = 1;
13513db5b9f7SMasaki Ota 		memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
13523db5b9f7SMasaki Ota 		return;
13533db5b9f7SMasaki Ota 	}
13543db5b9f7SMasaki Ota 
13553db5b9f7SMasaki Ota 	priv->multi_packet = 0;
13563db5b9f7SMasaki Ota 
1357864db929SPaul Donohue 	/* Report trackstick */
1358864db929SPaul Donohue 	if (alps_get_pkt_id_ss4_v2(packet) == SS4_PACKET_ID_STICK) {
13597229c58cSPaul Donohue 		if (!(priv->flags & ALPS_DUALPOINT)) {
13607229c58cSPaul Donohue 			psmouse_warn(psmouse,
13617229c58cSPaul Donohue 				     "Rejected trackstick packet from non DualPoint device");
13627229c58cSPaul Donohue 			return;
13637229c58cSPaul Donohue 		}
13647229c58cSPaul Donohue 
136523fce365SPaul Donohue 		input_report_rel(dev2, REL_X, SS4_TS_X_V2(packet));
136623fce365SPaul Donohue 		input_report_rel(dev2, REL_Y, SS4_TS_Y_V2(packet));
136723fce365SPaul Donohue 		input_report_abs(dev2, ABS_PRESSURE, SS4_TS_Z_V2(packet));
13687229c58cSPaul Donohue 
1369864db929SPaul Donohue 		input_report_key(dev2, BTN_LEFT, f->ts_left);
1370864db929SPaul Donohue 		input_report_key(dev2, BTN_RIGHT, f->ts_right);
1371864db929SPaul Donohue 		input_report_key(dev2, BTN_MIDDLE, f->ts_middle);
13727229c58cSPaul Donohue 
1373864db929SPaul Donohue 		input_sync(dev2);
1374864db929SPaul Donohue 		return;
1375864db929SPaul Donohue 	}
1376864db929SPaul Donohue 
1377864db929SPaul Donohue 	/* Report touchpad */
13783db5b9f7SMasaki Ota 	alps_report_mt_data(psmouse, (f->fingers <= 4) ? f->fingers : 4);
13793db5b9f7SMasaki Ota 
13803db5b9f7SMasaki Ota 	input_mt_report_finger_count(dev, f->fingers);
13813db5b9f7SMasaki Ota 
13823db5b9f7SMasaki Ota 	input_report_key(dev, BTN_LEFT, f->left);
13833db5b9f7SMasaki Ota 	input_report_key(dev, BTN_RIGHT, f->right);
13843db5b9f7SMasaki Ota 	input_report_key(dev, BTN_MIDDLE, f->middle);
13853db5b9f7SMasaki Ota 
13863db5b9f7SMasaki Ota 	input_report_abs(dev, ABS_PRESSURE, f->pressure);
13873db5b9f7SMasaki Ota 	input_sync(dev);
13883db5b9f7SMasaki Ota }
13893db5b9f7SMasaki Ota 
13903db5b9f7SMasaki Ota static bool alps_is_valid_package_ss4_v2(struct psmouse *psmouse)
13913db5b9f7SMasaki Ota {
13923db5b9f7SMasaki Ota 	if (psmouse->pktcnt == 4 && ((psmouse->packet[3] & 0x08) != 0x08))
13933db5b9f7SMasaki Ota 		return false;
13943db5b9f7SMasaki Ota 	if (psmouse->pktcnt == 6 && ((psmouse->packet[5] & 0x10) != 0x0))
13953db5b9f7SMasaki Ota 		return false;
13963db5b9f7SMasaki Ota 	return true;
13973db5b9f7SMasaki Ota }
13983db5b9f7SMasaki Ota 
139904aae283SPali Rohár static DEFINE_MUTEX(alps_mutex);
140004aae283SPali Rohár 
140104aae283SPali Rohár static void alps_register_bare_ps2_mouse(struct work_struct *work)
140204aae283SPali Rohár {
140304aae283SPali Rohár 	struct alps_data *priv =
140404aae283SPali Rohár 		container_of(work, struct alps_data, dev3_register_work.work);
140504aae283SPali Rohár 	struct psmouse *psmouse = priv->psmouse;
140604aae283SPali Rohár 	struct input_dev *dev3;
140704aae283SPali Rohár 	int error = 0;
140804aae283SPali Rohár 
140904aae283SPali Rohár 	mutex_lock(&alps_mutex);
141004aae283SPali Rohár 
141104aae283SPali Rohár 	if (priv->dev3)
141204aae283SPali Rohár 		goto out;
141304aae283SPali Rohár 
141404aae283SPali Rohár 	dev3 = input_allocate_device();
141504aae283SPali Rohár 	if (!dev3) {
141604aae283SPali Rohár 		psmouse_err(psmouse, "failed to allocate secondary device\n");
141704aae283SPali Rohár 		error = -ENOMEM;
141804aae283SPali Rohár 		goto out;
141904aae283SPali Rohár 	}
142004aae283SPali Rohár 
142104aae283SPali Rohár 	snprintf(priv->phys3, sizeof(priv->phys3), "%s/%s",
142204aae283SPali Rohár 		 psmouse->ps2dev.serio->phys,
142304aae283SPali Rohár 		 (priv->dev2 ? "input2" : "input1"));
142404aae283SPali Rohár 	dev3->phys = priv->phys3;
142504aae283SPali Rohár 
142604aae283SPali Rohár 	/*
142704aae283SPali Rohár 	 * format of input device name is: "protocol vendor name"
142804aae283SPali Rohár 	 * see function psmouse_switch_protocol() in psmouse-base.c
142904aae283SPali Rohár 	 */
143004aae283SPali Rohár 	dev3->name = "PS/2 ALPS Mouse";
143104aae283SPali Rohár 
143204aae283SPali Rohár 	dev3->id.bustype = BUS_I8042;
143304aae283SPali Rohár 	dev3->id.vendor  = 0x0002;
143404aae283SPali Rohár 	dev3->id.product = PSMOUSE_PS2;
143504aae283SPali Rohár 	dev3->id.version = 0x0000;
143604aae283SPali Rohár 	dev3->dev.parent = &psmouse->ps2dev.serio->dev;
143704aae283SPali Rohár 
143804aae283SPali Rohár 	input_set_capability(dev3, EV_REL, REL_X);
143904aae283SPali Rohár 	input_set_capability(dev3, EV_REL, REL_Y);
144004aae283SPali Rohár 	input_set_capability(dev3, EV_KEY, BTN_LEFT);
144104aae283SPali Rohár 	input_set_capability(dev3, EV_KEY, BTN_RIGHT);
144204aae283SPali Rohár 	input_set_capability(dev3, EV_KEY, BTN_MIDDLE);
144304aae283SPali Rohár 
144404aae283SPali Rohár 	__set_bit(INPUT_PROP_POINTER, dev3->propbit);
144504aae283SPali Rohár 
144604aae283SPali Rohár 	error = input_register_device(dev3);
144704aae283SPali Rohár 	if (error) {
144804aae283SPali Rohár 		psmouse_err(psmouse,
144904aae283SPali Rohár 			    "failed to register secondary device: %d\n",
145004aae283SPali Rohár 			    error);
145104aae283SPali Rohár 		input_free_device(dev3);
145204aae283SPali Rohár 		goto out;
145304aae283SPali Rohár 	}
145404aae283SPali Rohár 
145504aae283SPali Rohár 	priv->dev3 = dev3;
145604aae283SPali Rohár 
145704aae283SPali Rohár out:
145804aae283SPali Rohár 	/*
145904aae283SPali Rohár 	 * Save the error code so that we can detect that we
146004aae283SPali Rohár 	 * already tried to create the device.
146104aae283SPali Rohár 	 */
146204aae283SPali Rohár 	if (error)
146304aae283SPali Rohár 		priv->dev3 = ERR_PTR(error);
146404aae283SPali Rohár 
146504aae283SPali Rohár 	mutex_unlock(&alps_mutex);
146604aae283SPali Rohár }
146704aae283SPali Rohár 
146859c30afbSHans de Goede static void alps_report_bare_ps2_packet(struct psmouse *psmouse,
14691d9f2626SSebastian Kapfer 					unsigned char packet[],
14701d9f2626SSebastian Kapfer 					bool report_buttons)
14711d9f2626SSebastian Kapfer {
147259c30afbSHans de Goede 	struct alps_data *priv = psmouse->private;
14736bcca19fSHans de Goede 	struct input_dev *dev, *dev2 = NULL;
147459c30afbSHans de Goede 
1475e3a79212SHans de Goede 	/* Figure out which device to use to report the bare packet */
1476e3a79212SHans de Goede 	if (priv->proto_version == ALPS_PROTO_V2 &&
1477e3a79212SHans de Goede 	    (priv->flags & ALPS_DUALPOINT)) {
1478e3a79212SHans de Goede 		/* On V2 devices the DualPoint Stick reports bare packets */
1479e3a79212SHans de Goede 		dev = priv->dev2;
14806bcca19fSHans de Goede 		dev2 = psmouse->dev;
1481e3a79212SHans de Goede 	} else if (unlikely(IS_ERR_OR_NULL(priv->dev3))) {
148259c30afbSHans de Goede 		/* Register dev3 mouse if we received PS/2 packet first time */
148359c30afbSHans de Goede 		if (!IS_ERR(priv->dev3))
148459c30afbSHans de Goede 			psmouse_queue_work(psmouse, &priv->dev3_register_work,
148559c30afbSHans de Goede 					   0);
148659c30afbSHans de Goede 		return;
148759c30afbSHans de Goede 	} else {
148859c30afbSHans de Goede 		dev = priv->dev3;
148959c30afbSHans de Goede 	}
149059c30afbSHans de Goede 
14911d9f2626SSebastian Kapfer 	if (report_buttons)
14926bcca19fSHans de Goede 		alps_report_buttons(dev, dev2,
14931d9f2626SSebastian Kapfer 				packet[0] & 1, packet[0] & 2, packet[0] & 4);
14941d9f2626SSebastian Kapfer 
14951ef85805SDmitry Torokhov 	psmouse_report_standard_motion(dev, packet);
14961d9f2626SSebastian Kapfer 
149704aae283SPali Rohár 	input_sync(dev);
14981d9f2626SSebastian Kapfer }
14991d9f2626SSebastian Kapfer 
15001d9f2626SSebastian Kapfer static psmouse_ret_t alps_handle_interleaved_ps2(struct psmouse *psmouse)
15011da177e4SLinus Torvalds {
15021da177e4SLinus Torvalds 	struct alps_data *priv = psmouse->private;
15031da177e4SLinus Torvalds 
15041d9f2626SSebastian Kapfer 	if (psmouse->pktcnt < 6)
15051d9f2626SSebastian Kapfer 		return PSMOUSE_GOOD_DATA;
15061d9f2626SSebastian Kapfer 
15071d9f2626SSebastian Kapfer 	if (psmouse->pktcnt == 6) {
15081d9f2626SSebastian Kapfer 		/*
15091d9f2626SSebastian Kapfer 		 * Start a timer to flush the packet if it ends up last
15101d9f2626SSebastian Kapfer 		 * 6-byte packet in the stream. Timer needs to fire
15111d9f2626SSebastian Kapfer 		 * psmouse core times out itself. 20 ms should be enough
15121d9f2626SSebastian Kapfer 		 * to decide if we are getting more data or not.
15131d9f2626SSebastian Kapfer 		 */
15141d9f2626SSebastian Kapfer 		mod_timer(&priv->timer, jiffies + msecs_to_jiffies(20));
15151d9f2626SSebastian Kapfer 		return PSMOUSE_GOOD_DATA;
15161d9f2626SSebastian Kapfer 	}
15171d9f2626SSebastian Kapfer 
15181d9f2626SSebastian Kapfer 	del_timer(&priv->timer);
15191d9f2626SSebastian Kapfer 
15201d9f2626SSebastian Kapfer 	if (psmouse->packet[6] & 0x80) {
15211d9f2626SSebastian Kapfer 
15221d9f2626SSebastian Kapfer 		/*
15231d9f2626SSebastian Kapfer 		 * Highest bit is set - that means we either had
15241d9f2626SSebastian Kapfer 		 * complete ALPS packet and this is start of the
15251d9f2626SSebastian Kapfer 		 * next packet or we got garbage.
15261d9f2626SSebastian Kapfer 		 */
15271d9f2626SSebastian Kapfer 
15281d9f2626SSebastian Kapfer 		if (((psmouse->packet[3] |
15291d9f2626SSebastian Kapfer 		      psmouse->packet[4] |
15301d9f2626SSebastian Kapfer 		      psmouse->packet[5]) & 0x80) ||
153199df65e7SKevin Cernekee 		    (!alps_is_valid_first_byte(priv, psmouse->packet[6]))) {
1532b5d21704SDmitry Torokhov 			psmouse_dbg(psmouse,
15333b112923SAndy Shevchenko 				    "refusing packet %4ph (suspected interleaved ps/2)\n",
15343b112923SAndy Shevchenko 				    psmouse->packet + 3);
15351d9f2626SSebastian Kapfer 			return PSMOUSE_BAD_DATA;
15361d9f2626SSebastian Kapfer 		}
15371d9f2626SSebastian Kapfer 
153824af5cb9SKevin Cernekee 		priv->process_packet(psmouse);
15391d9f2626SSebastian Kapfer 
15401d9f2626SSebastian Kapfer 		/* Continue with the next packet */
15411d9f2626SSebastian Kapfer 		psmouse->packet[0] = psmouse->packet[6];
15421d9f2626SSebastian Kapfer 		psmouse->pktcnt = 1;
15431d9f2626SSebastian Kapfer 
15441d9f2626SSebastian Kapfer 	} else {
15451d9f2626SSebastian Kapfer 
15461d9f2626SSebastian Kapfer 		/*
15471d9f2626SSebastian Kapfer 		 * High bit is 0 - that means that we indeed got a PS/2
15481d9f2626SSebastian Kapfer 		 * packet in the middle of ALPS packet.
15491d9f2626SSebastian Kapfer 		 *
15501d9f2626SSebastian Kapfer 		 * There is also possibility that we got 6-byte ALPS
15511d9f2626SSebastian Kapfer 		 * packet followed  by 3-byte packet from trackpoint. We
15521d9f2626SSebastian Kapfer 		 * can not distinguish between these 2 scenarios but
1553b5d21704SDmitry Torokhov 		 * because the latter is unlikely to happen in course of
15541d9f2626SSebastian Kapfer 		 * normal operation (user would need to press all
15551d9f2626SSebastian Kapfer 		 * buttons on the pad and start moving trackpoint
15561d9f2626SSebastian Kapfer 		 * without touching the pad surface) we assume former.
15571d9f2626SSebastian Kapfer 		 * Even if we are wrong the wost thing that would happen
15581d9f2626SSebastian Kapfer 		 * the cursor would jump but we should not get protocol
1559b5d21704SDmitry Torokhov 		 * de-synchronization.
15601d9f2626SSebastian Kapfer 		 */
15611d9f2626SSebastian Kapfer 
156259c30afbSHans de Goede 		alps_report_bare_ps2_packet(psmouse, &psmouse->packet[3],
156359c30afbSHans de Goede 					    false);
15641d9f2626SSebastian Kapfer 
15651d9f2626SSebastian Kapfer 		/*
15661d9f2626SSebastian Kapfer 		 * Continue with the standard ALPS protocol handling,
15671d9f2626SSebastian Kapfer 		 * but make sure we won't process it as an interleaved
15681d9f2626SSebastian Kapfer 		 * packet again, which may happen if all buttons are
15691d9f2626SSebastian Kapfer 		 * pressed. To avoid this let's reset the 4th bit which
15701d9f2626SSebastian Kapfer 		 * is normally 1.
15711d9f2626SSebastian Kapfer 		 */
15721d9f2626SSebastian Kapfer 		psmouse->packet[3] = psmouse->packet[6] & 0xf7;
15731d9f2626SSebastian Kapfer 		psmouse->pktcnt = 4;
15741d9f2626SSebastian Kapfer 	}
15751d9f2626SSebastian Kapfer 
15761d9f2626SSebastian Kapfer 	return PSMOUSE_GOOD_DATA;
15771d9f2626SSebastian Kapfer }
15781d9f2626SSebastian Kapfer 
157917a58edcSKees Cook static void alps_flush_packet(struct timer_list *t)
15801d9f2626SSebastian Kapfer {
158117a58edcSKees Cook 	struct alps_data *priv = from_timer(priv, t, timer);
158217a58edcSKees Cook 	struct psmouse *psmouse = priv->psmouse;
15831d9f2626SSebastian Kapfer 
15841d9f2626SSebastian Kapfer 	serio_pause_rx(psmouse->ps2dev.serio);
15851d9f2626SSebastian Kapfer 
1586b46615feSSeth Forshee 	if (psmouse->pktcnt == psmouse->pktsize) {
15871d9f2626SSebastian Kapfer 
15881d9f2626SSebastian Kapfer 		/*
15891d9f2626SSebastian Kapfer 		 * We did not any more data in reasonable amount of time.
15901d9f2626SSebastian Kapfer 		 * Validate the last 3 bytes and process as a standard
15911d9f2626SSebastian Kapfer 		 * ALPS packet.
15921d9f2626SSebastian Kapfer 		 */
15931d9f2626SSebastian Kapfer 		if ((psmouse->packet[3] |
15941d9f2626SSebastian Kapfer 		     psmouse->packet[4] |
15951d9f2626SSebastian Kapfer 		     psmouse->packet[5]) & 0x80) {
1596b5d21704SDmitry Torokhov 			psmouse_dbg(psmouse,
15973b112923SAndy Shevchenko 				    "refusing packet %3ph (suspected interleaved ps/2)\n",
15983b112923SAndy Shevchenko 				    psmouse->packet + 3);
15991d9f2626SSebastian Kapfer 		} else {
160024af5cb9SKevin Cernekee 			priv->process_packet(psmouse);
16011d9f2626SSebastian Kapfer 		}
16021d9f2626SSebastian Kapfer 		psmouse->pktcnt = 0;
16031d9f2626SSebastian Kapfer 	}
16041d9f2626SSebastian Kapfer 
16051d9f2626SSebastian Kapfer 	serio_continue_rx(psmouse->ps2dev.serio);
16061d9f2626SSebastian Kapfer }
16071d9f2626SSebastian Kapfer 
16081d9f2626SSebastian Kapfer static psmouse_ret_t alps_process_byte(struct psmouse *psmouse)
16091d9f2626SSebastian Kapfer {
16101d9f2626SSebastian Kapfer 	struct alps_data *priv = psmouse->private;
16111d9f2626SSebastian Kapfer 
16124ab8f7f3SPali Rohár 	/*
16134ab8f7f3SPali Rohár 	 * Check if we are dealing with a bare PS/2 packet, presumably from
16144ab8f7f3SPali Rohár 	 * a device connected to the external PS/2 port. Because bare PS/2
16154ab8f7f3SPali Rohár 	 * protocol does not have enough constant bits to self-synchronize
16164ab8f7f3SPali Rohár 	 * properly we only do this if the device is fully synchronized.
16173db5b9f7SMasaki Ota 	 * Can not distinguish V8's first byte from PS/2 packet's
16184ab8f7f3SPali Rohár 	 */
16193db5b9f7SMasaki Ota 	if (priv->proto_version != ALPS_PROTO_V8 &&
16203db5b9f7SMasaki Ota 	    !psmouse->out_of_sync_cnt &&
16213db5b9f7SMasaki Ota 	    (psmouse->packet[0] & 0xc8) == 0x08) {
16223db5b9f7SMasaki Ota 
16231da177e4SLinus Torvalds 		if (psmouse->pktcnt == 3) {
162459c30afbSHans de Goede 			alps_report_bare_ps2_packet(psmouse, psmouse->packet,
16251d9f2626SSebastian Kapfer 						    true);
16261da177e4SLinus Torvalds 			return PSMOUSE_FULL_PACKET;
16271da177e4SLinus Torvalds 		}
16281da177e4SLinus Torvalds 		return PSMOUSE_GOOD_DATA;
16291da177e4SLinus Torvalds 	}
16301da177e4SLinus Torvalds 
16311d9f2626SSebastian Kapfer 	/* Check for PS/2 packet stuffed in the middle of ALPS packet. */
16321d9f2626SSebastian Kapfer 
163399df65e7SKevin Cernekee 	if ((priv->flags & ALPS_PS2_INTERLEAVED) &&
16341d9f2626SSebastian Kapfer 	    psmouse->pktcnt >= 4 && (psmouse->packet[3] & 0x0f) == 0x0f) {
16351d9f2626SSebastian Kapfer 		return alps_handle_interleaved_ps2(psmouse);
16361d9f2626SSebastian Kapfer 	}
16371d9f2626SSebastian Kapfer 
163899df65e7SKevin Cernekee 	if (!alps_is_valid_first_byte(priv, psmouse->packet[0])) {
1639b5d21704SDmitry Torokhov 		psmouse_dbg(psmouse,
1640b5d21704SDmitry Torokhov 			    "refusing packet[0] = %x (mask0 = %x, byte0 = %x)\n",
164199df65e7SKevin Cernekee 			    psmouse->packet[0], priv->mask0, priv->byte0);
16421da177e4SLinus Torvalds 		return PSMOUSE_BAD_DATA;
16431d9f2626SSebastian Kapfer 	}
16441da177e4SLinus Torvalds 
1645b46615feSSeth Forshee 	/* Bytes 2 - pktsize should have 0 in the highest bit */
1646a7ef82aeSPali Rohár 	if (priv->proto_version < ALPS_PROTO_V5 &&
164775af9e56SDave Turvene 	    psmouse->pktcnt >= 2 && psmouse->pktcnt <= psmouse->pktsize &&
16481d9f2626SSebastian Kapfer 	    (psmouse->packet[psmouse->pktcnt - 1] & 0x80)) {
1649b5d21704SDmitry Torokhov 		psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
1650b5d21704SDmitry Torokhov 			    psmouse->pktcnt - 1,
1651b5d21704SDmitry Torokhov 			    psmouse->packet[psmouse->pktcnt - 1]);
1652a7ef82aeSPali Rohár 
1653fb2dd7a6SDmitry Torokhov 		if (priv->proto_version == ALPS_PROTO_V3_RUSHMORE &&
1654a7ef82aeSPali Rohár 		    psmouse->pktcnt == psmouse->pktsize) {
1655a7ef82aeSPali Rohár 			/*
1656a7ef82aeSPali Rohár 			 * Some Dell boxes, such as Latitude E6440 or E7440
1657a7ef82aeSPali Rohár 			 * with closed lid, quite often smash last byte of
1658a7ef82aeSPali Rohár 			 * otherwise valid packet with 0xff. Given that the
1659a7ef82aeSPali Rohár 			 * next packet is very likely to be valid let's
1660a7ef82aeSPali Rohár 			 * report PSMOUSE_FULL_PACKET but not process data,
1661a7ef82aeSPali Rohár 			 * rather than reporting PSMOUSE_BAD_DATA and
1662a7ef82aeSPali Rohár 			 * filling the logs.
1663a7ef82aeSPali Rohár 			 */
1664a7ef82aeSPali Rohár 			return PSMOUSE_FULL_PACKET;
1665a7ef82aeSPali Rohár 		}
1666a7ef82aeSPali Rohár 
16671da177e4SLinus Torvalds 		return PSMOUSE_BAD_DATA;
16681d9f2626SSebastian Kapfer 	}
16691da177e4SLinus Torvalds 
16703db5b9f7SMasaki Ota 	if ((priv->proto_version == ALPS_PROTO_V7 &&
16713db5b9f7SMasaki Ota 			!alps_is_valid_package_v7(psmouse)) ||
16723db5b9f7SMasaki Ota 	    (priv->proto_version == ALPS_PROTO_V8 &&
16733db5b9f7SMasaki Ota 			!alps_is_valid_package_ss4_v2(psmouse))) {
16743808843cSYunkang Tang 		psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
16753808843cSYunkang Tang 			    psmouse->pktcnt - 1,
16763808843cSYunkang Tang 			    psmouse->packet[psmouse->pktcnt - 1]);
16773808843cSYunkang Tang 		return PSMOUSE_BAD_DATA;
16783808843cSYunkang Tang 	}
16793808843cSYunkang Tang 
1680b46615feSSeth Forshee 	if (psmouse->pktcnt == psmouse->pktsize) {
168124af5cb9SKevin Cernekee 		priv->process_packet(psmouse);
16821da177e4SLinus Torvalds 		return PSMOUSE_FULL_PACKET;
16831da177e4SLinus Torvalds 	}
16841da177e4SLinus Torvalds 
16851da177e4SLinus Torvalds 	return PSMOUSE_GOOD_DATA;
16861da177e4SLinus Torvalds }
16871da177e4SLinus Torvalds 
168825bded7cSSeth Forshee static int alps_command_mode_send_nibble(struct psmouse *psmouse, int nibble)
168925bded7cSSeth Forshee {
169025bded7cSSeth Forshee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
169125bded7cSSeth Forshee 	struct alps_data *priv = psmouse->private;
169225bded7cSSeth Forshee 	int command;
169325bded7cSSeth Forshee 	unsigned char *param;
169425bded7cSSeth Forshee 	unsigned char dummy[4];
169525bded7cSSeth Forshee 
169625bded7cSSeth Forshee 	BUG_ON(nibble > 0xf);
169725bded7cSSeth Forshee 
169825bded7cSSeth Forshee 	command = priv->nibble_commands[nibble].command;
169925bded7cSSeth Forshee 	param = (command & 0x0f00) ?
170025bded7cSSeth Forshee 		dummy : (unsigned char *)&priv->nibble_commands[nibble].data;
170125bded7cSSeth Forshee 
170225bded7cSSeth Forshee 	if (ps2_command(ps2dev, param, command))
170325bded7cSSeth Forshee 		return -1;
170425bded7cSSeth Forshee 
170525bded7cSSeth Forshee 	return 0;
170625bded7cSSeth Forshee }
170725bded7cSSeth Forshee 
170825bded7cSSeth Forshee static int alps_command_mode_set_addr(struct psmouse *psmouse, int addr)
170925bded7cSSeth Forshee {
171025bded7cSSeth Forshee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
171125bded7cSSeth Forshee 	struct alps_data *priv = psmouse->private;
171225bded7cSSeth Forshee 	int i, nibble;
171325bded7cSSeth Forshee 
171425bded7cSSeth Forshee 	if (ps2_command(ps2dev, NULL, priv->addr_command))
171525bded7cSSeth Forshee 		return -1;
171625bded7cSSeth Forshee 
171725bded7cSSeth Forshee 	for (i = 12; i >= 0; i -= 4) {
171825bded7cSSeth Forshee 		nibble = (addr >> i) & 0xf;
171925bded7cSSeth Forshee 		if (alps_command_mode_send_nibble(psmouse, nibble))
172025bded7cSSeth Forshee 			return -1;
172125bded7cSSeth Forshee 	}
172225bded7cSSeth Forshee 
172325bded7cSSeth Forshee 	return 0;
172425bded7cSSeth Forshee }
172525bded7cSSeth Forshee 
172625bded7cSSeth Forshee static int __alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
172725bded7cSSeth Forshee {
172825bded7cSSeth Forshee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
172925bded7cSSeth Forshee 	unsigned char param[4];
173025bded7cSSeth Forshee 
173125bded7cSSeth Forshee 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
173225bded7cSSeth Forshee 		return -1;
173325bded7cSSeth Forshee 
173425bded7cSSeth Forshee 	/*
173525bded7cSSeth Forshee 	 * The address being read is returned in the first two bytes
173625bded7cSSeth Forshee 	 * of the result. Check that this address matches the expected
173725bded7cSSeth Forshee 	 * address.
173825bded7cSSeth Forshee 	 */
173925bded7cSSeth Forshee 	if (addr != ((param[0] << 8) | param[1]))
174025bded7cSSeth Forshee 		return -1;
174125bded7cSSeth Forshee 
174225bded7cSSeth Forshee 	return param[2];
174325bded7cSSeth Forshee }
174425bded7cSSeth Forshee 
174525bded7cSSeth Forshee static int alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
174625bded7cSSeth Forshee {
174725bded7cSSeth Forshee 	if (alps_command_mode_set_addr(psmouse, addr))
174825bded7cSSeth Forshee 		return -1;
174925bded7cSSeth Forshee 	return __alps_command_mode_read_reg(psmouse, addr);
175025bded7cSSeth Forshee }
175125bded7cSSeth Forshee 
175225bded7cSSeth Forshee static int __alps_command_mode_write_reg(struct psmouse *psmouse, u8 value)
175325bded7cSSeth Forshee {
175425bded7cSSeth Forshee 	if (alps_command_mode_send_nibble(psmouse, (value >> 4) & 0xf))
175525bded7cSSeth Forshee 		return -1;
175625bded7cSSeth Forshee 	if (alps_command_mode_send_nibble(psmouse, value & 0xf))
175725bded7cSSeth Forshee 		return -1;
175825bded7cSSeth Forshee 	return 0;
175925bded7cSSeth Forshee }
176025bded7cSSeth Forshee 
176125bded7cSSeth Forshee static int alps_command_mode_write_reg(struct psmouse *psmouse, int addr,
176225bded7cSSeth Forshee 				       u8 value)
176325bded7cSSeth Forshee {
176425bded7cSSeth Forshee 	if (alps_command_mode_set_addr(psmouse, addr))
176525bded7cSSeth Forshee 		return -1;
176625bded7cSSeth Forshee 	return __alps_command_mode_write_reg(psmouse, value);
176725bded7cSSeth Forshee }
176825bded7cSSeth Forshee 
176924ba9707SKevin Cernekee static int alps_rpt_cmd(struct psmouse *psmouse, int init_command,
177024ba9707SKevin Cernekee 			int repeated_command, unsigned char *param)
177124ba9707SKevin Cernekee {
177224ba9707SKevin Cernekee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
177324ba9707SKevin Cernekee 
177424ba9707SKevin Cernekee 	param[0] = 0;
177524ba9707SKevin Cernekee 	if (init_command && ps2_command(ps2dev, param, init_command))
177624ba9707SKevin Cernekee 		return -EIO;
177724ba9707SKevin Cernekee 
177824ba9707SKevin Cernekee 	if (ps2_command(ps2dev,  NULL, repeated_command) ||
177924ba9707SKevin Cernekee 	    ps2_command(ps2dev,  NULL, repeated_command) ||
178024ba9707SKevin Cernekee 	    ps2_command(ps2dev,  NULL, repeated_command))
178124ba9707SKevin Cernekee 		return -EIO;
178224ba9707SKevin Cernekee 
178324ba9707SKevin Cernekee 	param[0] = param[1] = param[2] = 0xff;
178424ba9707SKevin Cernekee 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
178524ba9707SKevin Cernekee 		return -EIO;
178624ba9707SKevin Cernekee 
178739fbe585SDmitry Torokhov 	psmouse_dbg(psmouse, "%2.2X report: %3ph\n",
178839fbe585SDmitry Torokhov 		    repeated_command, param);
178924ba9707SKevin Cernekee 	return 0;
179024ba9707SKevin Cernekee }
179124ba9707SKevin Cernekee 
17923808843cSYunkang Tang static bool alps_check_valid_firmware_id(unsigned char id[])
17933808843cSYunkang Tang {
17943808843cSYunkang Tang 	if (id[0] == 0x73)
17953808843cSYunkang Tang 		return true;
17963808843cSYunkang Tang 
17973808843cSYunkang Tang 	if (id[0] == 0x88 &&
17983808843cSYunkang Tang 	    (id[1] == 0x07 ||
17993808843cSYunkang Tang 	     id[1] == 0x08 ||
18003808843cSYunkang Tang 	     (id[1] & 0xf0) == 0xb0 ||
18013808843cSYunkang Tang 	     (id[1] & 0xf0) == 0xc0)) {
18023808843cSYunkang Tang 		return true;
18033808843cSYunkang Tang 	}
18043808843cSYunkang Tang 
18053808843cSYunkang Tang 	return false;
18063808843cSYunkang Tang }
18073808843cSYunkang Tang 
1808d18e53fcSKevin Cernekee static int alps_enter_command_mode(struct psmouse *psmouse)
180925bded7cSSeth Forshee {
181025bded7cSSeth Forshee 	unsigned char param[4];
181125bded7cSSeth Forshee 
181224ba9707SKevin Cernekee 	if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_RESET_WRAP, param)) {
181325bded7cSSeth Forshee 		psmouse_err(psmouse, "failed to enter command mode\n");
181425bded7cSSeth Forshee 		return -1;
181525bded7cSSeth Forshee 	}
181625bded7cSSeth Forshee 
18173808843cSYunkang Tang 	if (!alps_check_valid_firmware_id(param)) {
181825bded7cSSeth Forshee 		psmouse_dbg(psmouse,
181924ba9707SKevin Cernekee 			    "unknown response while entering command mode\n");
182025bded7cSSeth Forshee 		return -1;
182125bded7cSSeth Forshee 	}
182225bded7cSSeth Forshee 	return 0;
182325bded7cSSeth Forshee }
182425bded7cSSeth Forshee 
182525bded7cSSeth Forshee static inline int alps_exit_command_mode(struct psmouse *psmouse)
182625bded7cSSeth Forshee {
182725bded7cSSeth Forshee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
182825bded7cSSeth Forshee 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM))
182925bded7cSSeth Forshee 		return -1;
183025bded7cSSeth Forshee 	return 0;
183125bded7cSSeth Forshee }
183225bded7cSSeth Forshee 
18331da177e4SLinus Torvalds /*
18341da177e4SLinus Torvalds  * For DualPoint devices select the device that should respond to
18351da177e4SLinus Torvalds  * subsequent commands. It looks like glidepad is behind stickpointer,
18361da177e4SLinus Torvalds  * I'd thought it would be other way around...
18371da177e4SLinus Torvalds  */
183825bded7cSSeth Forshee static int alps_passthrough_mode_v2(struct psmouse *psmouse, bool enable)
18391da177e4SLinus Torvalds {
18401da177e4SLinus Torvalds 	struct ps2dev *ps2dev = &psmouse->ps2dev;
18411da177e4SLinus Torvalds 	int cmd = enable ? PSMOUSE_CMD_SETSCALE21 : PSMOUSE_CMD_SETSCALE11;
18421da177e4SLinus Torvalds 
18431da177e4SLinus Torvalds 	if (ps2_command(ps2dev, NULL, cmd) ||
18441da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, cmd) ||
18451da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, cmd) ||
18461da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
18471da177e4SLinus Torvalds 		return -1;
18481da177e4SLinus Torvalds 
18491da177e4SLinus Torvalds 	/* we may get 3 more bytes, just ignore them */
1850c611763dSDmitry Torokhov 	ps2_drain(ps2dev, 3, 100);
18511da177e4SLinus Torvalds 
18521da177e4SLinus Torvalds 	return 0;
18531da177e4SLinus Torvalds }
18541da177e4SLinus Torvalds 
185525bded7cSSeth Forshee static int alps_absolute_mode_v1_v2(struct psmouse *psmouse)
18561da177e4SLinus Torvalds {
18571da177e4SLinus Torvalds 	struct ps2dev *ps2dev = &psmouse->ps2dev;
18581da177e4SLinus Torvalds 
18591da177e4SLinus Torvalds 	/* Try ALPS magic knock - 4 disable before enable */
18601da177e4SLinus Torvalds 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
18611da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
18621da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
18631da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
18641da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
18651da177e4SLinus Torvalds 		return -1;
18661da177e4SLinus Torvalds 
18671da177e4SLinus Torvalds 	/*
18681da177e4SLinus Torvalds 	 * Switch mouse to poll (remote) mode so motion data will not
18691da177e4SLinus Torvalds 	 * get in our way
18701da177e4SLinus Torvalds 	 */
1871ad56814fSGuenter Roeck 	return ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL);
18721da177e4SLinus Torvalds }
18731da177e4SLinus Torvalds 
187495f75e91SYunkang Tang static int alps_monitor_mode_send_word(struct psmouse *psmouse, u16 word)
187595f75e91SYunkang Tang {
187695f75e91SYunkang Tang 	int i, nibble;
187795f75e91SYunkang Tang 
187895f75e91SYunkang Tang 	/*
187995f75e91SYunkang Tang 	 * b0-b11 are valid bits, send sequence is inverse.
188095f75e91SYunkang Tang 	 * e.g. when word = 0x0123, nibble send sequence is 3, 2, 1
188195f75e91SYunkang Tang 	 */
188295f75e91SYunkang Tang 	for (i = 0; i <= 8; i += 4) {
188395f75e91SYunkang Tang 		nibble = (word >> i) & 0xf;
188495f75e91SYunkang Tang 		if (alps_command_mode_send_nibble(psmouse, nibble))
188595f75e91SYunkang Tang 			return -1;
188695f75e91SYunkang Tang 	}
188795f75e91SYunkang Tang 
188895f75e91SYunkang Tang 	return 0;
188995f75e91SYunkang Tang }
189095f75e91SYunkang Tang 
189195f75e91SYunkang Tang static int alps_monitor_mode_write_reg(struct psmouse *psmouse,
189295f75e91SYunkang Tang 				       u16 addr, u16 value)
189395f75e91SYunkang Tang {
189495f75e91SYunkang Tang 	struct ps2dev *ps2dev = &psmouse->ps2dev;
189595f75e91SYunkang Tang 
189695f75e91SYunkang Tang 	/* 0x0A0 is the command to write the word */
189795f75e91SYunkang Tang 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE) ||
189895f75e91SYunkang Tang 	    alps_monitor_mode_send_word(psmouse, 0x0A0) ||
189995f75e91SYunkang Tang 	    alps_monitor_mode_send_word(psmouse, addr) ||
190095f75e91SYunkang Tang 	    alps_monitor_mode_send_word(psmouse, value) ||
190195f75e91SYunkang Tang 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
190295f75e91SYunkang Tang 		return -1;
190395f75e91SYunkang Tang 
190495f75e91SYunkang Tang 	return 0;
190595f75e91SYunkang Tang }
190695f75e91SYunkang Tang 
190795f75e91SYunkang Tang static int alps_monitor_mode(struct psmouse *psmouse, bool enable)
190895f75e91SYunkang Tang {
190995f75e91SYunkang Tang 	struct ps2dev *ps2dev = &psmouse->ps2dev;
191095f75e91SYunkang Tang 
191195f75e91SYunkang Tang 	if (enable) {
191295f75e91SYunkang Tang 		/* EC E9 F5 F5 E7 E6 E7 E9 to enter monitor mode */
191395f75e91SYunkang Tang 		if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
191495f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO) ||
191595f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
191695f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
191795f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
191895f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
191995f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
192095f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO))
192195f75e91SYunkang Tang 			return -1;
192295f75e91SYunkang Tang 	} else {
192395f75e91SYunkang Tang 		/* EC to exit monitor mode */
192495f75e91SYunkang Tang 		if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP))
192595f75e91SYunkang Tang 			return -1;
192695f75e91SYunkang Tang 	}
192795f75e91SYunkang Tang 
192895f75e91SYunkang Tang 	return 0;
192995f75e91SYunkang Tang }
193095f75e91SYunkang Tang 
193195f75e91SYunkang Tang static int alps_absolute_mode_v6(struct psmouse *psmouse)
193295f75e91SYunkang Tang {
193395f75e91SYunkang Tang 	u16 reg_val = 0x181;
193495f75e91SYunkang Tang 	int ret = -1;
193595f75e91SYunkang Tang 
193695f75e91SYunkang Tang 	/* enter monitor mode, to write the register */
193795f75e91SYunkang Tang 	if (alps_monitor_mode(psmouse, true))
193895f75e91SYunkang Tang 		return -1;
193995f75e91SYunkang Tang 
194095f75e91SYunkang Tang 	ret = alps_monitor_mode_write_reg(psmouse, 0x000, reg_val);
194195f75e91SYunkang Tang 
194295f75e91SYunkang Tang 	if (alps_monitor_mode(psmouse, false))
194395f75e91SYunkang Tang 		ret = -1;
194495f75e91SYunkang Tang 
194595f75e91SYunkang Tang 	return ret;
194695f75e91SYunkang Tang }
194795f75e91SYunkang Tang 
19481da177e4SLinus Torvalds static int alps_get_status(struct psmouse *psmouse, char *param)
19491da177e4SLinus Torvalds {
19501da177e4SLinus Torvalds 	/* Get status: 0xF5 0xF5 0xF5 0xE9 */
195124ba9707SKevin Cernekee 	if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_DISABLE, param))
19521da177e4SLinus Torvalds 		return -1;
19531da177e4SLinus Torvalds 
19541da177e4SLinus Torvalds 	return 0;
19551da177e4SLinus Torvalds }
19561da177e4SLinus Torvalds 
19571da177e4SLinus Torvalds /*
19581da177e4SLinus Torvalds  * Turn touchpad tapping on or off. The sequences are:
19591da177e4SLinus Torvalds  * 0xE9 0xF5 0xF5 0xF3 0x0A to enable,
19601da177e4SLinus Torvalds  * 0xE9 0xF5 0xF5 0xE8 0x00 to disable.
19611da177e4SLinus Torvalds  * My guess that 0xE9 (GetInfo) is here as a sync point.
19621da177e4SLinus Torvalds  * For models that also have stickpointer (DualPoints) its tapping
19631da177e4SLinus Torvalds  * is controlled separately (0xE6 0xE6 0xE6 0xF3 0x14|0x0A) but
19641da177e4SLinus Torvalds  * we don't fiddle with it.
19651da177e4SLinus Torvalds  */
19661da177e4SLinus Torvalds static int alps_tap_mode(struct psmouse *psmouse, int enable)
19671da177e4SLinus Torvalds {
19681da177e4SLinus Torvalds 	struct ps2dev *ps2dev = &psmouse->ps2dev;
19691da177e4SLinus Torvalds 	int cmd = enable ? PSMOUSE_CMD_SETRATE : PSMOUSE_CMD_SETRES;
19701da177e4SLinus Torvalds 	unsigned char tap_arg = enable ? 0x0A : 0x00;
19711da177e4SLinus Torvalds 	unsigned char param[4];
19721da177e4SLinus Torvalds 
19731da177e4SLinus Torvalds 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO) ||
19741da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
19751da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
19761da177e4SLinus Torvalds 	    ps2_command(ps2dev, &tap_arg, cmd))
19771da177e4SLinus Torvalds 		return -1;
19781da177e4SLinus Torvalds 
19791da177e4SLinus Torvalds 	if (alps_get_status(psmouse, param))
19801da177e4SLinus Torvalds 		return -1;
19811da177e4SLinus Torvalds 
19821da177e4SLinus Torvalds 	return 0;
19831da177e4SLinus Torvalds }
19841da177e4SLinus Torvalds 
1985f0d5c6f4SDmitry Torokhov /*
1986f0d5c6f4SDmitry Torokhov  * alps_poll() - poll the touchpad for current motion packet.
1987f0d5c6f4SDmitry Torokhov  * Used in resync.
1988f0d5c6f4SDmitry Torokhov  */
1989f0d5c6f4SDmitry Torokhov static int alps_poll(struct psmouse *psmouse)
1990f0d5c6f4SDmitry Torokhov {
1991f0d5c6f4SDmitry Torokhov 	struct alps_data *priv = psmouse->private;
1992b46615feSSeth Forshee 	unsigned char buf[sizeof(psmouse->packet)];
1993b7802c5cSDmitry Torokhov 	bool poll_failed;
1994f0d5c6f4SDmitry Torokhov 
199599df65e7SKevin Cernekee 	if (priv->flags & ALPS_PASS)
199625bded7cSSeth Forshee 		alps_passthrough_mode_v2(psmouse, true);
1997f0d5c6f4SDmitry Torokhov 
1998f0d5c6f4SDmitry Torokhov 	poll_failed = ps2_command(&psmouse->ps2dev, buf,
1999f0d5c6f4SDmitry Torokhov 				  PSMOUSE_CMD_POLL | (psmouse->pktsize << 8)) < 0;
2000f0d5c6f4SDmitry Torokhov 
200199df65e7SKevin Cernekee 	if (priv->flags & ALPS_PASS)
200225bded7cSSeth Forshee 		alps_passthrough_mode_v2(psmouse, false);
2003f0d5c6f4SDmitry Torokhov 
200499df65e7SKevin Cernekee 	if (poll_failed || (buf[0] & priv->mask0) != priv->byte0)
2005f0d5c6f4SDmitry Torokhov 		return -1;
2006f0d5c6f4SDmitry Torokhov 
2007f0d5c6f4SDmitry Torokhov 	if ((psmouse->badbyte & 0xc8) == 0x08) {
2008f0d5c6f4SDmitry Torokhov /*
2009f0d5c6f4SDmitry Torokhov  * Poll the track stick ...
2010f0d5c6f4SDmitry Torokhov  */
2011f0d5c6f4SDmitry Torokhov 		if (ps2_command(&psmouse->ps2dev, buf, PSMOUSE_CMD_POLL | (3 << 8)))
2012f0d5c6f4SDmitry Torokhov 			return -1;
2013f0d5c6f4SDmitry Torokhov 	}
2014f0d5c6f4SDmitry Torokhov 
2015f0d5c6f4SDmitry Torokhov 	memcpy(psmouse->packet, buf, sizeof(buf));
2016f0d5c6f4SDmitry Torokhov 	return 0;
2017f0d5c6f4SDmitry Torokhov }
2018f0d5c6f4SDmitry Torokhov 
201925bded7cSSeth Forshee static int alps_hw_init_v1_v2(struct psmouse *psmouse)
20201da177e4SLinus Torvalds {
20211da177e4SLinus Torvalds 	struct alps_data *priv = psmouse->private;
20221da177e4SLinus Torvalds 
202399df65e7SKevin Cernekee 	if ((priv->flags & ALPS_PASS) &&
202425bded7cSSeth Forshee 	    alps_passthrough_mode_v2(psmouse, true)) {
20251da177e4SLinus Torvalds 		return -1;
2026b7802c5cSDmitry Torokhov 	}
20271da177e4SLinus Torvalds 
2028b7802c5cSDmitry Torokhov 	if (alps_tap_mode(psmouse, true)) {
2029b5d21704SDmitry Torokhov 		psmouse_warn(psmouse, "Failed to enable hardware tapping\n");
20301da177e4SLinus Torvalds 		return -1;
2031963f626dSPeter Osterlund 	}
20321da177e4SLinus Torvalds 
203325bded7cSSeth Forshee 	if (alps_absolute_mode_v1_v2(psmouse)) {
2034b5d21704SDmitry Torokhov 		psmouse_err(psmouse, "Failed to enable absolute mode\n");
20351da177e4SLinus Torvalds 		return -1;
20361da177e4SLinus Torvalds 	}
20371da177e4SLinus Torvalds 
203899df65e7SKevin Cernekee 	if ((priv->flags & ALPS_PASS) &&
203925bded7cSSeth Forshee 	    alps_passthrough_mode_v2(psmouse, false)) {
20401da177e4SLinus Torvalds 		return -1;
2041b7802c5cSDmitry Torokhov 	}
20421da177e4SLinus Torvalds 
20431e0c5b12SDmitry Torokhov 	/* ALPS needs stream mode, otherwise it won't report any data */
20441e0c5b12SDmitry Torokhov 	if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)) {
2045b5d21704SDmitry Torokhov 		psmouse_err(psmouse, "Failed to enable stream mode\n");
20461e0c5b12SDmitry Torokhov 		return -1;
20471e0c5b12SDmitry Torokhov 	}
20481e0c5b12SDmitry Torokhov 
20491e0c5b12SDmitry Torokhov 	return 0;
20501e0c5b12SDmitry Torokhov }
20511e0c5b12SDmitry Torokhov 
205295f75e91SYunkang Tang static int alps_hw_init_v6(struct psmouse *psmouse)
205395f75e91SYunkang Tang {
205495f75e91SYunkang Tang 	unsigned char param[2] = {0xC8, 0x14};
205595f75e91SYunkang Tang 
205695f75e91SYunkang Tang 	/* Enter passthrough mode to let trackpoint enter 6byte raw mode */
205795f75e91SYunkang Tang 	if (alps_passthrough_mode_v2(psmouse, true))
205895f75e91SYunkang Tang 		return -1;
205995f75e91SYunkang Tang 
206095f75e91SYunkang Tang 	if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
206195f75e91SYunkang Tang 	    ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
206295f75e91SYunkang Tang 	    ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
206395f75e91SYunkang Tang 	    ps2_command(&psmouse->ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
206495f75e91SYunkang Tang 	    ps2_command(&psmouse->ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
206595f75e91SYunkang Tang 		return -1;
206695f75e91SYunkang Tang 
206795f75e91SYunkang Tang 	if (alps_passthrough_mode_v2(psmouse, false))
206895f75e91SYunkang Tang 		return -1;
206995f75e91SYunkang Tang 
207095f75e91SYunkang Tang 	if (alps_absolute_mode_v6(psmouse)) {
207195f75e91SYunkang Tang 		psmouse_err(psmouse, "Failed to enable absolute mode\n");
207295f75e91SYunkang Tang 		return -1;
207395f75e91SYunkang Tang 	}
207495f75e91SYunkang Tang 
207595f75e91SYunkang Tang 	return 0;
207695f75e91SYunkang Tang }
207795f75e91SYunkang Tang 
207825bded7cSSeth Forshee /*
2079cd401204SKevin Cernekee  * Enable or disable passthrough mode to the trackstick.
208025bded7cSSeth Forshee  */
2081cd401204SKevin Cernekee static int alps_passthrough_mode_v3(struct psmouse *psmouse,
2082cd401204SKevin Cernekee 				    int reg_base, bool enable)
208325bded7cSSeth Forshee {
2084cd401204SKevin Cernekee 	int reg_val, ret = -1;
208525bded7cSSeth Forshee 
2086d18e53fcSKevin Cernekee 	if (alps_enter_command_mode(psmouse))
208725bded7cSSeth Forshee 		return -1;
208825bded7cSSeth Forshee 
2089cd401204SKevin Cernekee 	reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x0008);
2090cd401204SKevin Cernekee 	if (reg_val == -1)
2091cd401204SKevin Cernekee 		goto error;
2092cd401204SKevin Cernekee 
209325bded7cSSeth Forshee 	if (enable)
209425bded7cSSeth Forshee 		reg_val |= 0x01;
209525bded7cSSeth Forshee 	else
209625bded7cSSeth Forshee 		reg_val &= ~0x01;
209725bded7cSSeth Forshee 
2098cd401204SKevin Cernekee 	ret = __alps_command_mode_write_reg(psmouse, reg_val);
209925bded7cSSeth Forshee 
2100cd401204SKevin Cernekee error:
2101cd401204SKevin Cernekee 	if (alps_exit_command_mode(psmouse))
2102cd401204SKevin Cernekee 		ret = -1;
2103cd401204SKevin Cernekee 	return ret;
210425bded7cSSeth Forshee }
210525bded7cSSeth Forshee 
210625bded7cSSeth Forshee /* Must be in command mode when calling this function */
210725bded7cSSeth Forshee static int alps_absolute_mode_v3(struct psmouse *psmouse)
210825bded7cSSeth Forshee {
210925bded7cSSeth Forshee 	int reg_val;
211025bded7cSSeth Forshee 
211125bded7cSSeth Forshee 	reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
211225bded7cSSeth Forshee 	if (reg_val == -1)
211325bded7cSSeth Forshee 		return -1;
211425bded7cSSeth Forshee 
211525bded7cSSeth Forshee 	reg_val |= 0x06;
211625bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, reg_val))
211725bded7cSSeth Forshee 		return -1;
211825bded7cSSeth Forshee 
211925bded7cSSeth Forshee 	return 0;
212025bded7cSSeth Forshee }
212125bded7cSSeth Forshee 
2122dae928ecSPali Rohár static int alps_probe_trackstick_v3_v7(struct psmouse *psmouse, int reg_base)
212325bded7cSSeth Forshee {
2124cd401204SKevin Cernekee 	int ret = -EIO, reg_val;
212525bded7cSSeth Forshee 
2126d18e53fcSKevin Cernekee 	if (alps_enter_command_mode(psmouse))
212725bded7cSSeth Forshee 		goto error;
212825bded7cSSeth Forshee 
2129cd401204SKevin Cernekee 	reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x08);
213025bded7cSSeth Forshee 	if (reg_val == -1)
213125bded7cSSeth Forshee 		goto error;
2132cd401204SKevin Cernekee 
2133cd401204SKevin Cernekee 	/* bit 7: trackstick is present */
2134cd401204SKevin Cernekee 	ret = reg_val & 0x80 ? 0 : -ENODEV;
2135cd401204SKevin Cernekee 
2136cd401204SKevin Cernekee error:
2137cd401204SKevin Cernekee 	alps_exit_command_mode(psmouse);
2138cd401204SKevin Cernekee 	return ret;
2139cd401204SKevin Cernekee }
2140cd401204SKevin Cernekee 
2141cd401204SKevin Cernekee static int alps_setup_trackstick_v3(struct psmouse *psmouse, int reg_base)
2142cd401204SKevin Cernekee {
2143cd401204SKevin Cernekee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
2144cd401204SKevin Cernekee 	int ret = 0;
2145cd401204SKevin Cernekee 	unsigned char param[4];
2146cd401204SKevin Cernekee 
2147cd401204SKevin Cernekee 	if (alps_passthrough_mode_v3(psmouse, reg_base, true))
2148cd401204SKevin Cernekee 		return -EIO;
214925bded7cSSeth Forshee 
215025bded7cSSeth Forshee 	/*
215125bded7cSSeth Forshee 	 * E7 report for the trackstick
215225bded7cSSeth Forshee 	 *
215325bded7cSSeth Forshee 	 * There have been reports of failures to seem to trace back
215425bded7cSSeth Forshee 	 * to the above trackstick check failing. When these occur
215525bded7cSSeth Forshee 	 * this E7 report fails, so when that happens we continue
215625bded7cSSeth Forshee 	 * with the assumption that there isn't a trackstick after
215725bded7cSSeth Forshee 	 * all.
215825bded7cSSeth Forshee 	 */
2159cd401204SKevin Cernekee 	if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_SETSCALE21, param)) {
2160a09221e8SDmitry Torokhov 		psmouse_warn(psmouse, "Failed to initialize trackstick (E7 report failed)\n");
2161cd401204SKevin Cernekee 		ret = -ENODEV;
216225bded7cSSeth Forshee 	} else {
216339fbe585SDmitry Torokhov 		psmouse_dbg(psmouse, "trackstick E7 report: %3ph\n", param);
216425bded7cSSeth Forshee 
216525bded7cSSeth Forshee 		/*
216625bded7cSSeth Forshee 		 * Not sure what this does, but it is absolutely
216725bded7cSSeth Forshee 		 * essential. Without it, the touchpad does not
216825bded7cSSeth Forshee 		 * work at all and the trackstick just emits normal
216925bded7cSSeth Forshee 		 * PS/2 packets.
217025bded7cSSeth Forshee 		 */
217125bded7cSSeth Forshee 		if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
217225bded7cSSeth Forshee 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
217325bded7cSSeth Forshee 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
217425bded7cSSeth Forshee 		    alps_command_mode_send_nibble(psmouse, 0x9) ||
217525bded7cSSeth Forshee 		    alps_command_mode_send_nibble(psmouse, 0x4)) {
217625bded7cSSeth Forshee 			psmouse_err(psmouse,
217725bded7cSSeth Forshee 				    "Error sending magic E6 sequence\n");
2178cd401204SKevin Cernekee 			ret = -EIO;
217925bded7cSSeth Forshee 			goto error;
218025bded7cSSeth Forshee 		}
218125bded7cSSeth Forshee 
2182cd401204SKevin Cernekee 		/*
2183cd401204SKevin Cernekee 		 * This ensures the trackstick packets are in the format
2184cd401204SKevin Cernekee 		 * supported by this driver. If bit 1 isn't set the packet
2185cd401204SKevin Cernekee 		 * format is different.
2186cd401204SKevin Cernekee 		 */
2187d18e53fcSKevin Cernekee 		if (alps_enter_command_mode(psmouse) ||
2188cd401204SKevin Cernekee 		    alps_command_mode_write_reg(psmouse,
2189cd401204SKevin Cernekee 						reg_base + 0x08, 0x82) ||
2190cd401204SKevin Cernekee 		    alps_exit_command_mode(psmouse))
2191cd401204SKevin Cernekee 			ret = -EIO;
2192cd401204SKevin Cernekee 	}
2193cd401204SKevin Cernekee 
2194cd401204SKevin Cernekee error:
2195cd401204SKevin Cernekee 	if (alps_passthrough_mode_v3(psmouse, reg_base, false))
2196cd401204SKevin Cernekee 		ret = -EIO;
2197cd401204SKevin Cernekee 
2198cd401204SKevin Cernekee 	return ret;
2199cd401204SKevin Cernekee }
2200cd401204SKevin Cernekee 
2201cd401204SKevin Cernekee static int alps_hw_init_v3(struct psmouse *psmouse)
2202cd401204SKevin Cernekee {
22034b1af853SPali Rohár 	struct alps_data *priv = psmouse->private;
2204cd401204SKevin Cernekee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
2205cd401204SKevin Cernekee 	int reg_val;
2206cd401204SKevin Cernekee 	unsigned char param[4];
2207cd401204SKevin Cernekee 
22084b1af853SPali Rohár 	if ((priv->flags & ALPS_DUALPOINT) &&
2209cd401204SKevin Cernekee 	    alps_setup_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE) == -EIO)
2210cd401204SKevin Cernekee 		goto error;
2211cd401204SKevin Cernekee 
2212d18e53fcSKevin Cernekee 	if (alps_enter_command_mode(psmouse) ||
2213cd401204SKevin Cernekee 	    alps_absolute_mode_v3(psmouse)) {
221425bded7cSSeth Forshee 		psmouse_err(psmouse, "Failed to enter absolute mode\n");
221525bded7cSSeth Forshee 		goto error;
221625bded7cSSeth Forshee 	}
221725bded7cSSeth Forshee 
221825bded7cSSeth Forshee 	reg_val = alps_command_mode_read_reg(psmouse, 0x0006);
221925bded7cSSeth Forshee 	if (reg_val == -1)
222025bded7cSSeth Forshee 		goto error;
222125bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
222225bded7cSSeth Forshee 		goto error;
222325bded7cSSeth Forshee 
222425bded7cSSeth Forshee 	reg_val = alps_command_mode_read_reg(psmouse, 0x0007);
222525bded7cSSeth Forshee 	if (reg_val == -1)
222625bded7cSSeth Forshee 		goto error;
222725bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
222825bded7cSSeth Forshee 		goto error;
222925bded7cSSeth Forshee 
223025bded7cSSeth Forshee 	if (alps_command_mode_read_reg(psmouse, 0x0144) == -1)
223125bded7cSSeth Forshee 		goto error;
223225bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, 0x04))
223325bded7cSSeth Forshee 		goto error;
223425bded7cSSeth Forshee 
223525bded7cSSeth Forshee 	if (alps_command_mode_read_reg(psmouse, 0x0159) == -1)
223625bded7cSSeth Forshee 		goto error;
223725bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, 0x03))
223825bded7cSSeth Forshee 		goto error;
223925bded7cSSeth Forshee 
224025bded7cSSeth Forshee 	if (alps_command_mode_read_reg(psmouse, 0x0163) == -1)
224125bded7cSSeth Forshee 		goto error;
224225bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0163, 0x03))
224325bded7cSSeth Forshee 		goto error;
224425bded7cSSeth Forshee 
224525bded7cSSeth Forshee 	if (alps_command_mode_read_reg(psmouse, 0x0162) == -1)
224625bded7cSSeth Forshee 		goto error;
224725bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0162, 0x04))
224825bded7cSSeth Forshee 		goto error;
224925bded7cSSeth Forshee 
225025bded7cSSeth Forshee 	alps_exit_command_mode(psmouse);
225125bded7cSSeth Forshee 
225225bded7cSSeth Forshee 	/* Set rate and enable data reporting */
225325bded7cSSeth Forshee 	param[0] = 0x64;
225425bded7cSSeth Forshee 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
225525bded7cSSeth Forshee 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
225625bded7cSSeth Forshee 		psmouse_err(psmouse, "Failed to enable data reporting\n");
225725bded7cSSeth Forshee 		return -1;
225825bded7cSSeth Forshee 	}
225925bded7cSSeth Forshee 
226025bded7cSSeth Forshee 	return 0;
226125bded7cSSeth Forshee 
226225bded7cSSeth Forshee error:
226325bded7cSSeth Forshee 	/*
226425bded7cSSeth Forshee 	 * Leaving the touchpad in command mode will essentially render
226525bded7cSSeth Forshee 	 * it unusable until the machine reboots, so exit it here just
226625bded7cSSeth Forshee 	 * to be safe
226725bded7cSSeth Forshee 	 */
226825bded7cSSeth Forshee 	alps_exit_command_mode(psmouse);
226925bded7cSSeth Forshee 	return -1;
227025bded7cSSeth Forshee }
227125bded7cSSeth Forshee 
2272f3f33c67SHans de Goede static int alps_get_v3_v7_resolution(struct psmouse *psmouse, int reg_pitch)
2273f3f33c67SHans de Goede {
2274f3f33c67SHans de Goede 	int reg, x_pitch, y_pitch, x_electrode, y_electrode, x_phys, y_phys;
2275f3f33c67SHans de Goede 	struct alps_data *priv = psmouse->private;
2276f3f33c67SHans de Goede 
2277f3f33c67SHans de Goede 	reg = alps_command_mode_read_reg(psmouse, reg_pitch);
2278f3f33c67SHans de Goede 	if (reg < 0)
2279f3f33c67SHans de Goede 		return reg;
2280f3f33c67SHans de Goede 
2281f3f33c67SHans de Goede 	x_pitch = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
2282f3f33c67SHans de Goede 	x_pitch = 50 + 2 * x_pitch; /* In 0.1 mm units */
2283f3f33c67SHans de Goede 
2284f3f33c67SHans de Goede 	y_pitch = (char)reg >> 4; /* sign extend upper 4 bits */
2285f3f33c67SHans de Goede 	y_pitch = 36 + 2 * y_pitch; /* In 0.1 mm units */
2286f3f33c67SHans de Goede 
2287f3f33c67SHans de Goede 	reg = alps_command_mode_read_reg(psmouse, reg_pitch + 1);
2288f3f33c67SHans de Goede 	if (reg < 0)
2289f3f33c67SHans de Goede 		return reg;
2290f3f33c67SHans de Goede 
2291f3f33c67SHans de Goede 	x_electrode = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
2292f3f33c67SHans de Goede 	x_electrode = 17 + x_electrode;
2293f3f33c67SHans de Goede 
2294f3f33c67SHans de Goede 	y_electrode = (char)reg >> 4; /* sign extend upper 4 bits */
2295f3f33c67SHans de Goede 	y_electrode = 13 + y_electrode;
2296f3f33c67SHans de Goede 
2297f3f33c67SHans de Goede 	x_phys = x_pitch * (x_electrode - 1); /* In 0.1 mm units */
2298f3f33c67SHans de Goede 	y_phys = y_pitch * (y_electrode - 1); /* In 0.1 mm units */
2299f3f33c67SHans de Goede 
2300f3f33c67SHans de Goede 	priv->x_res = priv->x_max * 10 / x_phys; /* units / mm */
2301f3f33c67SHans de Goede 	priv->y_res = priv->y_max * 10 / y_phys; /* units / mm */
2302f3f33c67SHans de Goede 
2303f3f33c67SHans de Goede 	psmouse_dbg(psmouse,
2304f3f33c67SHans de Goede 		    "pitch %dx%d num-electrodes %dx%d physical size %dx%d mm res %dx%d\n",
2305f3f33c67SHans de Goede 		    x_pitch, y_pitch, x_electrode, y_electrode,
2306f3f33c67SHans de Goede 		    x_phys / 10, y_phys / 10, priv->x_res, priv->y_res);
2307f3f33c67SHans de Goede 
2308f3f33c67SHans de Goede 	return 0;
2309f3f33c67SHans de Goede }
2310f3f33c67SHans de Goede 
23111302bac3SKevin Cernekee static int alps_hw_init_rushmore_v3(struct psmouse *psmouse)
23121302bac3SKevin Cernekee {
2313cd401204SKevin Cernekee 	struct alps_data *priv = psmouse->private;
23141302bac3SKevin Cernekee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
23151302bac3SKevin Cernekee 	int reg_val, ret = -1;
23161302bac3SKevin Cernekee 
2317cd401204SKevin Cernekee 	if (priv->flags & ALPS_DUALPOINT) {
2318cd401204SKevin Cernekee 		reg_val = alps_setup_trackstick_v3(psmouse,
2319cd401204SKevin Cernekee 						   ALPS_REG_BASE_RUSHMORE);
2320cd401204SKevin Cernekee 		if (reg_val == -EIO)
2321cd401204SKevin Cernekee 			goto error;
2322cd401204SKevin Cernekee 	}
2323cd401204SKevin Cernekee 
2324d18e53fcSKevin Cernekee 	if (alps_enter_command_mode(psmouse) ||
23251302bac3SKevin Cernekee 	    alps_command_mode_read_reg(psmouse, 0xc2d9) == -1 ||
23261302bac3SKevin Cernekee 	    alps_command_mode_write_reg(psmouse, 0xc2cb, 0x00))
23271302bac3SKevin Cernekee 		goto error;
23281302bac3SKevin Cernekee 
2329f3f33c67SHans de Goede 	if (alps_get_v3_v7_resolution(psmouse, 0xc2da))
2330f3f33c67SHans de Goede 		goto error;
2331f3f33c67SHans de Goede 
23321302bac3SKevin Cernekee 	reg_val = alps_command_mode_read_reg(psmouse, 0xc2c6);
23331302bac3SKevin Cernekee 	if (reg_val == -1)
23341302bac3SKevin Cernekee 		goto error;
23351302bac3SKevin Cernekee 	if (__alps_command_mode_write_reg(psmouse, reg_val & 0xfd))
23361302bac3SKevin Cernekee 		goto error;
23371302bac3SKevin Cernekee 
23381302bac3SKevin Cernekee 	if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
23391302bac3SKevin Cernekee 		goto error;
23401302bac3SKevin Cernekee 
23411302bac3SKevin Cernekee 	/* enter absolute mode */
23421302bac3SKevin Cernekee 	reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
23431302bac3SKevin Cernekee 	if (reg_val == -1)
23441302bac3SKevin Cernekee 		goto error;
23451302bac3SKevin Cernekee 	if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
23461302bac3SKevin Cernekee 		goto error;
23471302bac3SKevin Cernekee 
23481302bac3SKevin Cernekee 	alps_exit_command_mode(psmouse);
23491302bac3SKevin Cernekee 	return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
23501302bac3SKevin Cernekee 
23511302bac3SKevin Cernekee error:
23521302bac3SKevin Cernekee 	alps_exit_command_mode(psmouse);
23531302bac3SKevin Cernekee 	return ret;
23541302bac3SKevin Cernekee }
23551302bac3SKevin Cernekee 
235625bded7cSSeth Forshee /* Must be in command mode when calling this function */
235725bded7cSSeth Forshee static int alps_absolute_mode_v4(struct psmouse *psmouse)
235825bded7cSSeth Forshee {
235925bded7cSSeth Forshee 	int reg_val;
236025bded7cSSeth Forshee 
236125bded7cSSeth Forshee 	reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
236225bded7cSSeth Forshee 	if (reg_val == -1)
236325bded7cSSeth Forshee 		return -1;
236425bded7cSSeth Forshee 
236525bded7cSSeth Forshee 	reg_val |= 0x02;
236625bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, reg_val))
236725bded7cSSeth Forshee 		return -1;
236825bded7cSSeth Forshee 
236925bded7cSSeth Forshee 	return 0;
237025bded7cSSeth Forshee }
237125bded7cSSeth Forshee 
237225bded7cSSeth Forshee static int alps_hw_init_v4(struct psmouse *psmouse)
237325bded7cSSeth Forshee {
237425bded7cSSeth Forshee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
237525bded7cSSeth Forshee 	unsigned char param[4];
237625bded7cSSeth Forshee 
2377d18e53fcSKevin Cernekee 	if (alps_enter_command_mode(psmouse))
237825bded7cSSeth Forshee 		goto error;
237925bded7cSSeth Forshee 
238025bded7cSSeth Forshee 	if (alps_absolute_mode_v4(psmouse)) {
238125bded7cSSeth Forshee 		psmouse_err(psmouse, "Failed to enter absolute mode\n");
238225bded7cSSeth Forshee 		goto error;
238325bded7cSSeth Forshee 	}
238425bded7cSSeth Forshee 
238525bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0007, 0x8c))
238625bded7cSSeth Forshee 		goto error;
238725bded7cSSeth Forshee 
238825bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0149, 0x03))
238925bded7cSSeth Forshee 		goto error;
239025bded7cSSeth Forshee 
239125bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0160, 0x03))
239225bded7cSSeth Forshee 		goto error;
239325bded7cSSeth Forshee 
239425bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x017f, 0x15))
239525bded7cSSeth Forshee 		goto error;
239625bded7cSSeth Forshee 
239725bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0151, 0x01))
239825bded7cSSeth Forshee 		goto error;
239925bded7cSSeth Forshee 
240025bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0168, 0x03))
240125bded7cSSeth Forshee 		goto error;
240225bded7cSSeth Forshee 
240325bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x014a, 0x03))
240425bded7cSSeth Forshee 		goto error;
240525bded7cSSeth Forshee 
240625bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0161, 0x03))
240725bded7cSSeth Forshee 		goto error;
240825bded7cSSeth Forshee 
240925bded7cSSeth Forshee 	alps_exit_command_mode(psmouse);
241025bded7cSSeth Forshee 
241125bded7cSSeth Forshee 	/*
241225bded7cSSeth Forshee 	 * This sequence changes the output from a 9-byte to an
241325bded7cSSeth Forshee 	 * 8-byte format. All the same data seems to be present,
241425bded7cSSeth Forshee 	 * just in a more compact format.
241525bded7cSSeth Forshee 	 */
241625bded7cSSeth Forshee 	param[0] = 0xc8;
241725bded7cSSeth Forshee 	param[1] = 0x64;
241825bded7cSSeth Forshee 	param[2] = 0x50;
241925bded7cSSeth Forshee 	if (ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
242025bded7cSSeth Forshee 	    ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE) ||
242125bded7cSSeth Forshee 	    ps2_command(ps2dev, &param[2], PSMOUSE_CMD_SETRATE) ||
242225bded7cSSeth Forshee 	    ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
242325bded7cSSeth Forshee 		return -1;
242425bded7cSSeth Forshee 
242525bded7cSSeth Forshee 	/* Set rate and enable data reporting */
242625bded7cSSeth Forshee 	param[0] = 0x64;
242725bded7cSSeth Forshee 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
242825bded7cSSeth Forshee 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
242925bded7cSSeth Forshee 		psmouse_err(psmouse, "Failed to enable data reporting\n");
243025bded7cSSeth Forshee 		return -1;
243125bded7cSSeth Forshee 	}
243225bded7cSSeth Forshee 
243325bded7cSSeth Forshee 	return 0;
243425bded7cSSeth Forshee 
243525bded7cSSeth Forshee error:
243625bded7cSSeth Forshee 	/*
243725bded7cSSeth Forshee 	 * Leaving the touchpad in command mode will essentially render
243825bded7cSSeth Forshee 	 * it unusable until the machine reboots, so exit it here just
243925bded7cSSeth Forshee 	 * to be safe
244025bded7cSSeth Forshee 	 */
244125bded7cSSeth Forshee 	alps_exit_command_mode(psmouse);
244225bded7cSSeth Forshee 	return -1;
244325bded7cSSeth Forshee }
244425bded7cSSeth Forshee 
24453db5b9f7SMasaki Ota static int alps_get_otp_values_ss4_v2(struct psmouse *psmouse,
24463db5b9f7SMasaki Ota 				      unsigned char index, unsigned char otp[])
24473db5b9f7SMasaki Ota {
24483db5b9f7SMasaki Ota 	struct ps2dev *ps2dev = &psmouse->ps2dev;
24493db5b9f7SMasaki Ota 
24503db5b9f7SMasaki Ota 	switch (index) {
24513db5b9f7SMasaki Ota 	case 0:
24523db5b9f7SMasaki Ota 		if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)  ||
24533db5b9f7SMasaki Ota 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)  ||
24543db5b9f7SMasaki Ota 		    ps2_command(ps2dev, otp, PSMOUSE_CMD_GETINFO))
24553db5b9f7SMasaki Ota 			return -1;
24563db5b9f7SMasaki Ota 
24573db5b9f7SMasaki Ota 		break;
24583db5b9f7SMasaki Ota 
24593db5b9f7SMasaki Ota 	case 1:
24603db5b9f7SMasaki Ota 		if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL)  ||
24613db5b9f7SMasaki Ota 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL)  ||
24623db5b9f7SMasaki Ota 		    ps2_command(ps2dev, otp, PSMOUSE_CMD_GETINFO))
24633db5b9f7SMasaki Ota 			return -1;
24643db5b9f7SMasaki Ota 
24653db5b9f7SMasaki Ota 		break;
24663db5b9f7SMasaki Ota 	}
24673db5b9f7SMasaki Ota 
24683db5b9f7SMasaki Ota 	return 0;
24693db5b9f7SMasaki Ota }
24703db5b9f7SMasaki Ota 
247107f19e3dSFengguang Wu static int alps_update_device_area_ss4_v2(unsigned char otp[][4],
24723db5b9f7SMasaki Ota 					  struct alps_data *priv)
24733db5b9f7SMasaki Ota {
24743db5b9f7SMasaki Ota 	int num_x_electrode;
24753db5b9f7SMasaki Ota 	int num_y_electrode;
24763db5b9f7SMasaki Ota 	int x_pitch, y_pitch, x_phys, y_phys;
24773db5b9f7SMasaki Ota 
2478e7348396SMasaki Ota 	if (IS_SS4PLUS_DEV(priv->dev_id)) {
2479e7348396SMasaki Ota 		num_x_electrode =
2480e7348396SMasaki Ota 			SS4PLUS_NUMSENSOR_XOFFSET + (otp[0][2] & 0x0F);
2481e7348396SMasaki Ota 		num_y_electrode =
2482e7348396SMasaki Ota 			SS4PLUS_NUMSENSOR_YOFFSET + ((otp[0][2] >> 4) & 0x0F);
24833db5b9f7SMasaki Ota 
2484e7348396SMasaki Ota 		priv->x_max =
2485e7348396SMasaki Ota 			(num_x_electrode - 1) * SS4PLUS_COUNT_PER_ELECTRODE;
2486e7348396SMasaki Ota 		priv->y_max =
2487e7348396SMasaki Ota 			(num_y_electrode - 1) * SS4PLUS_COUNT_PER_ELECTRODE;
2488e7348396SMasaki Ota 
2489e7348396SMasaki Ota 		x_pitch = (otp[0][1] & 0x0F) + SS4PLUS_MIN_PITCH_MM;
2490e7348396SMasaki Ota 		y_pitch = ((otp[0][1] >> 4) & 0x0F) + SS4PLUS_MIN_PITCH_MM;
2491e7348396SMasaki Ota 
2492e7348396SMasaki Ota 	} else {
2493e7348396SMasaki Ota 		num_x_electrode =
2494e7348396SMasaki Ota 			SS4_NUMSENSOR_XOFFSET + (otp[1][0] & 0x0F);
2495e7348396SMasaki Ota 		num_y_electrode =
2496e7348396SMasaki Ota 			SS4_NUMSENSOR_YOFFSET + ((otp[1][0] >> 4) & 0x0F);
2497e7348396SMasaki Ota 
2498e7348396SMasaki Ota 		priv->x_max =
2499e7348396SMasaki Ota 			(num_x_electrode - 1) * SS4_COUNT_PER_ELECTRODE;
2500e7348396SMasaki Ota 		priv->y_max =
2501e7348396SMasaki Ota 			(num_y_electrode - 1) * SS4_COUNT_PER_ELECTRODE;
25023db5b9f7SMasaki Ota 
25033db5b9f7SMasaki Ota 		x_pitch = ((otp[1][2] >> 2) & 0x07) + SS4_MIN_PITCH_MM;
25043db5b9f7SMasaki Ota 		y_pitch = ((otp[1][2] >> 5) & 0x07) + SS4_MIN_PITCH_MM;
2505e7348396SMasaki Ota 	}
25063db5b9f7SMasaki Ota 
25073db5b9f7SMasaki Ota 	x_phys = x_pitch * (num_x_electrode - 1); /* In 0.1 mm units */
25083db5b9f7SMasaki Ota 	y_phys = y_pitch * (num_y_electrode - 1); /* In 0.1 mm units */
25093db5b9f7SMasaki Ota 
25103db5b9f7SMasaki Ota 	priv->x_res = priv->x_max * 10 / x_phys; /* units / mm */
25113db5b9f7SMasaki Ota 	priv->y_res = priv->y_max * 10 / y_phys; /* units / mm */
25123db5b9f7SMasaki Ota 
25133db5b9f7SMasaki Ota 	return 0;
25143db5b9f7SMasaki Ota }
25153db5b9f7SMasaki Ota 
251607f19e3dSFengguang Wu static int alps_update_btn_info_ss4_v2(unsigned char otp[][4],
251707f19e3dSFengguang Wu 				       struct alps_data *priv)
25183db5b9f7SMasaki Ota {
25193db5b9f7SMasaki Ota 	unsigned char is_btnless;
25203db5b9f7SMasaki Ota 
2521e7348396SMasaki Ota 	if (IS_SS4PLUS_DEV(priv->dev_id))
2522e7348396SMasaki Ota 		is_btnless = (otp[1][0] >> 1) & 0x01;
2523e7348396SMasaki Ota 	else
25243db5b9f7SMasaki Ota 		is_btnless = (otp[1][1] >> 3) & 0x01;
25253db5b9f7SMasaki Ota 
25263db5b9f7SMasaki Ota 	if (is_btnless)
25273db5b9f7SMasaki Ota 		priv->flags |= ALPS_BUTTONPAD;
25283db5b9f7SMasaki Ota 
25293db5b9f7SMasaki Ota 	return 0;
25303db5b9f7SMasaki Ota }
25313db5b9f7SMasaki Ota 
2532e7348396SMasaki Ota static int alps_update_dual_info_ss4_v2(unsigned char otp[][4],
2533e7348396SMasaki Ota 				       struct alps_data *priv)
2534e7348396SMasaki Ota {
2535e7348396SMasaki Ota 	bool is_dual = false;
2536e7348396SMasaki Ota 
2537e7348396SMasaki Ota 	if (IS_SS4PLUS_DEV(priv->dev_id))
2538e7348396SMasaki Ota 		is_dual = (otp[0][0] >> 4) & 0x01;
2539e7348396SMasaki Ota 
2540e7348396SMasaki Ota 	if (is_dual)
2541e7348396SMasaki Ota 		priv->flags |= ALPS_DUALPOINT |
2542e7348396SMasaki Ota 					ALPS_DUALPOINT_WITH_PRESSURE;
2543e7348396SMasaki Ota 
2544e7348396SMasaki Ota 	return 0;
2545e7348396SMasaki Ota }
2546e7348396SMasaki Ota 
25473db5b9f7SMasaki Ota static int alps_set_defaults_ss4_v2(struct psmouse *psmouse,
25483db5b9f7SMasaki Ota 				    struct alps_data *priv)
25493db5b9f7SMasaki Ota {
25503db5b9f7SMasaki Ota 	unsigned char otp[2][4];
25513db5b9f7SMasaki Ota 
25523db5b9f7SMasaki Ota 	memset(otp, 0, sizeof(otp));
25533db5b9f7SMasaki Ota 
25544a646580SMasaki Ota 	if (alps_get_otp_values_ss4_v2(psmouse, 1, &otp[1][0]) ||
25554a646580SMasaki Ota 	    alps_get_otp_values_ss4_v2(psmouse, 0, &otp[0][0]))
25563db5b9f7SMasaki Ota 		return -1;
25573db5b9f7SMasaki Ota 
25583db5b9f7SMasaki Ota 	alps_update_device_area_ss4_v2(otp, priv);
25593db5b9f7SMasaki Ota 
25603db5b9f7SMasaki Ota 	alps_update_btn_info_ss4_v2(otp, priv);
25613db5b9f7SMasaki Ota 
2562e7348396SMasaki Ota 	alps_update_dual_info_ss4_v2(otp, priv);
2563e7348396SMasaki Ota 
25643db5b9f7SMasaki Ota 	return 0;
25653db5b9f7SMasaki Ota }
25663db5b9f7SMasaki Ota 
2567ee65d4b3SYunkang Tang static int alps_dolphin_get_device_area(struct psmouse *psmouse,
2568ee65d4b3SYunkang Tang 					struct alps_data *priv)
2569ee65d4b3SYunkang Tang {
2570ee65d4b3SYunkang Tang 	struct ps2dev *ps2dev = &psmouse->ps2dev;
2571ee65d4b3SYunkang Tang 	unsigned char param[4] = {0};
2572ee65d4b3SYunkang Tang 	int num_x_electrode, num_y_electrode;
2573ee65d4b3SYunkang Tang 
2574ee65d4b3SYunkang Tang 	if (alps_enter_command_mode(psmouse))
2575ee65d4b3SYunkang Tang 		return -1;
2576ee65d4b3SYunkang Tang 
2577ee65d4b3SYunkang Tang 	param[0] = 0x0a;
2578ee65d4b3SYunkang Tang 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
2579ee65d4b3SYunkang Tang 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2580ee65d4b3SYunkang Tang 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2581ee65d4b3SYunkang Tang 	    ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2582ee65d4b3SYunkang Tang 	    ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE))
2583ee65d4b3SYunkang Tang 		return -1;
2584ee65d4b3SYunkang Tang 
2585ee65d4b3SYunkang Tang 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
2586ee65d4b3SYunkang Tang 		return -1;
2587ee65d4b3SYunkang Tang 
2588ee65d4b3SYunkang Tang 	/*
2589ee65d4b3SYunkang Tang 	 * Dolphin's sensor line number is not fixed. It can be calculated
2590ee65d4b3SYunkang Tang 	 * by adding the device's register value with DOLPHIN_PROFILE_X/YOFFSET.
2591ee65d4b3SYunkang Tang 	 * Further more, we can get device's x_max and y_max by multiplying
2592ee65d4b3SYunkang Tang 	 * sensor line number with DOLPHIN_COUNT_PER_ELECTRODE.
2593ee65d4b3SYunkang Tang 	 *
2594ee65d4b3SYunkang Tang 	 * e.g. When we get register's sensor_x = 11 & sensor_y = 8,
2595ee65d4b3SYunkang Tang 	 *	real sensor line number X = 11 + 8 = 19, and
2596ee65d4b3SYunkang Tang 	 *	real sensor line number Y = 8 + 1 = 9.
2597ee65d4b3SYunkang Tang 	 *	So, x_max = (19 - 1) * 64 = 1152, and
2598ee65d4b3SYunkang Tang 	 *	    y_max = (9 - 1) * 64 = 512.
2599ee65d4b3SYunkang Tang 	 */
2600ee65d4b3SYunkang Tang 	num_x_electrode = DOLPHIN_PROFILE_XOFFSET + (param[2] & 0x0F);
2601ee65d4b3SYunkang Tang 	num_y_electrode = DOLPHIN_PROFILE_YOFFSET + ((param[2] >> 4) & 0x0F);
2602ee65d4b3SYunkang Tang 	priv->x_bits = num_x_electrode;
2603ee65d4b3SYunkang Tang 	priv->y_bits = num_y_electrode;
2604ee65d4b3SYunkang Tang 	priv->x_max = (num_x_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
2605ee65d4b3SYunkang Tang 	priv->y_max = (num_y_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
2606ee65d4b3SYunkang Tang 
2607ee65d4b3SYunkang Tang 	if (alps_exit_command_mode(psmouse))
2608ee65d4b3SYunkang Tang 		return -1;
2609ee65d4b3SYunkang Tang 
2610ee65d4b3SYunkang Tang 	return 0;
2611ee65d4b3SYunkang Tang }
2612ee65d4b3SYunkang Tang 
261375af9e56SDave Turvene static int alps_hw_init_dolphin_v1(struct psmouse *psmouse)
261475af9e56SDave Turvene {
261575af9e56SDave Turvene 	struct ps2dev *ps2dev = &psmouse->ps2dev;
261675af9e56SDave Turvene 	unsigned char param[2];
261775af9e56SDave Turvene 
261875af9e56SDave Turvene 	/* This is dolphin "v1" as empirically defined by florin9doi */
261975af9e56SDave Turvene 	param[0] = 0x64;
262075af9e56SDave Turvene 	param[1] = 0x28;
262175af9e56SDave Turvene 
262275af9e56SDave Turvene 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
262375af9e56SDave Turvene 	    ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
262475af9e56SDave Turvene 	    ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
262575af9e56SDave Turvene 		return -1;
262675af9e56SDave Turvene 
262775af9e56SDave Turvene 	return 0;
262875af9e56SDave Turvene }
262975af9e56SDave Turvene 
26303808843cSYunkang Tang static int alps_hw_init_v7(struct psmouse *psmouse)
26313808843cSYunkang Tang {
26323808843cSYunkang Tang 	struct ps2dev *ps2dev = &psmouse->ps2dev;
26333808843cSYunkang Tang 	int reg_val, ret = -1;
26343808843cSYunkang Tang 
26353808843cSYunkang Tang 	if (alps_enter_command_mode(psmouse) ||
26363808843cSYunkang Tang 	    alps_command_mode_read_reg(psmouse, 0xc2d9) == -1)
26373808843cSYunkang Tang 		goto error;
26383808843cSYunkang Tang 
2639f3f33c67SHans de Goede 	if (alps_get_v3_v7_resolution(psmouse, 0xc397))
2640f3f33c67SHans de Goede 		goto error;
2641f3f33c67SHans de Goede 
26423808843cSYunkang Tang 	if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
26433808843cSYunkang Tang 		goto error;
26443808843cSYunkang Tang 
26453808843cSYunkang Tang 	reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
26463808843cSYunkang Tang 	if (reg_val == -1)
26473808843cSYunkang Tang 		goto error;
26483808843cSYunkang Tang 	if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
26493808843cSYunkang Tang 		goto error;
26503808843cSYunkang Tang 
26513808843cSYunkang Tang 	alps_exit_command_mode(psmouse);
26523808843cSYunkang Tang 	return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
26533808843cSYunkang Tang 
26543808843cSYunkang Tang error:
26553808843cSYunkang Tang 	alps_exit_command_mode(psmouse);
26563808843cSYunkang Tang 	return ret;
26573808843cSYunkang Tang }
26583808843cSYunkang Tang 
26593db5b9f7SMasaki Ota static int alps_hw_init_ss4_v2(struct psmouse *psmouse)
26603db5b9f7SMasaki Ota {
26613db5b9f7SMasaki Ota 	struct ps2dev *ps2dev = &psmouse->ps2dev;
26623db5b9f7SMasaki Ota 	char param[2] = {0x64, 0x28};
26633db5b9f7SMasaki Ota 	int ret = -1;
26643db5b9f7SMasaki Ota 
26653db5b9f7SMasaki Ota 	/* enter absolute mode */
26663db5b9f7SMasaki Ota 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
26673db5b9f7SMasaki Ota 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
26683db5b9f7SMasaki Ota 	    ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
26693db5b9f7SMasaki Ota 	    ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE)) {
26703db5b9f7SMasaki Ota 		goto error;
26713db5b9f7SMasaki Ota 	}
26723db5b9f7SMasaki Ota 
26733db5b9f7SMasaki Ota 	/* T.B.D. Decread noise packet number, delete in the future */
26743db5b9f7SMasaki Ota 	if (alps_exit_command_mode(psmouse) ||
26753db5b9f7SMasaki Ota 	    alps_enter_command_mode(psmouse) ||
26763db5b9f7SMasaki Ota 	    alps_command_mode_write_reg(psmouse, 0x001D, 0x20)) {
26773db5b9f7SMasaki Ota 		goto error;
26783db5b9f7SMasaki Ota 	}
26793db5b9f7SMasaki Ota 	alps_exit_command_mode(psmouse);
26803db5b9f7SMasaki Ota 
26813db5b9f7SMasaki Ota 	return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
26823db5b9f7SMasaki Ota 
26833db5b9f7SMasaki Ota error:
26843db5b9f7SMasaki Ota 	alps_exit_command_mode(psmouse);
26853db5b9f7SMasaki Ota 	return ret;
26863db5b9f7SMasaki Ota }
26873db5b9f7SMasaki Ota 
26883296f71cSDmitry Torokhov static int alps_set_protocol(struct psmouse *psmouse,
26893296f71cSDmitry Torokhov 			     struct alps_data *priv,
26903296f71cSDmitry Torokhov 			     const struct alps_protocol_info *protocol)
269125bded7cSSeth Forshee {
26923296f71cSDmitry Torokhov 	psmouse->private = priv;
26933296f71cSDmitry Torokhov 
269417a58edcSKees Cook 	timer_setup(&priv->timer, alps_flush_packet, 0);
26953296f71cSDmitry Torokhov 
26963296f71cSDmitry Torokhov 	priv->proto_version = protocol->version;
26973296f71cSDmitry Torokhov 	priv->byte0 = protocol->byte0;
26983296f71cSDmitry Torokhov 	priv->mask0 = protocol->mask0;
26993296f71cSDmitry Torokhov 	priv->flags = protocol->flags;
2700f673ceb1SKevin Cernekee 
27017a9f73e7SKevin Cernekee 	priv->x_max = 2000;
27027a9f73e7SKevin Cernekee 	priv->y_max = 1400;
27037a9f73e7SKevin Cernekee 	priv->x_bits = 15;
27047a9f73e7SKevin Cernekee 	priv->y_bits = 11;
27057a9f73e7SKevin Cernekee 
270699df65e7SKevin Cernekee 	switch (priv->proto_version) {
270725bded7cSSeth Forshee 	case ALPS_PROTO_V1:
270825bded7cSSeth Forshee 	case ALPS_PROTO_V2:
270924af5cb9SKevin Cernekee 		priv->hw_init = alps_hw_init_v1_v2;
271024af5cb9SKevin Cernekee 		priv->process_packet = alps_process_packet_v1_v2;
271124af5cb9SKevin Cernekee 		priv->set_abs_params = alps_set_abs_params_st;
271295f75e91SYunkang Tang 		priv->x_max = 1023;
271395f75e91SYunkang Tang 		priv->y_max = 767;
271419556219SHans de Goede 		if (dmi_check_system(alps_dmi_has_separate_stick_buttons))
271519556219SHans de Goede 			priv->flags |= ALPS_STICK_BITS;
271625bded7cSSeth Forshee 		break;
2717fb2dd7a6SDmitry Torokhov 
271825bded7cSSeth Forshee 	case ALPS_PROTO_V3:
271924af5cb9SKevin Cernekee 		priv->hw_init = alps_hw_init_v3;
272024af5cb9SKevin Cernekee 		priv->process_packet = alps_process_packet_v3;
2721688ea364SHans de Goede 		priv->set_abs_params = alps_set_abs_params_semi_mt;
2722f85e5001SKevin Cernekee 		priv->decode_fields = alps_decode_pinnacle;
272350e8b216SKevin Cernekee 		priv->nibble_commands = alps_v3_nibble_commands;
272450e8b216SKevin Cernekee 		priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
27254b1af853SPali Rohár 
27264b1af853SPali Rohár 		if (alps_probe_trackstick_v3_v7(psmouse,
27274b1af853SPali Rohár 						ALPS_REG_BASE_PINNACLE) < 0)
27284b1af853SPali Rohár 			priv->flags &= ~ALPS_DUALPOINT;
27294b1af853SPali Rohár 
273025bded7cSSeth Forshee 		break;
2731fb2dd7a6SDmitry Torokhov 
2732fb2dd7a6SDmitry Torokhov 	case ALPS_PROTO_V3_RUSHMORE:
2733fb2dd7a6SDmitry Torokhov 		priv->hw_init = alps_hw_init_rushmore_v3;
2734fb2dd7a6SDmitry Torokhov 		priv->process_packet = alps_process_packet_v3;
2735688ea364SHans de Goede 		priv->set_abs_params = alps_set_abs_params_semi_mt;
2736fb2dd7a6SDmitry Torokhov 		priv->decode_fields = alps_decode_rushmore;
2737fb2dd7a6SDmitry Torokhov 		priv->nibble_commands = alps_v3_nibble_commands;
2738fb2dd7a6SDmitry Torokhov 		priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2739fb2dd7a6SDmitry Torokhov 		priv->x_bits = 16;
2740fb2dd7a6SDmitry Torokhov 		priv->y_bits = 12;
27413296f71cSDmitry Torokhov 
2742dae928ecSPali Rohár 		if (alps_probe_trackstick_v3_v7(psmouse,
27433296f71cSDmitry Torokhov 						ALPS_REG_BASE_RUSHMORE) < 0)
27443296f71cSDmitry Torokhov 			priv->flags &= ~ALPS_DUALPOINT;
27453296f71cSDmitry Torokhov 
2746fb2dd7a6SDmitry Torokhov 		break;
2747fb2dd7a6SDmitry Torokhov 
274825bded7cSSeth Forshee 	case ALPS_PROTO_V4:
274924af5cb9SKevin Cernekee 		priv->hw_init = alps_hw_init_v4;
275024af5cb9SKevin Cernekee 		priv->process_packet = alps_process_packet_v4;
2751688ea364SHans de Goede 		priv->set_abs_params = alps_set_abs_params_semi_mt;
275250e8b216SKevin Cernekee 		priv->nibble_commands = alps_v4_nibble_commands;
275350e8b216SKevin Cernekee 		priv->addr_command = PSMOUSE_CMD_DISABLE;
275425bded7cSSeth Forshee 		break;
27553296f71cSDmitry Torokhov 
275675af9e56SDave Turvene 	case ALPS_PROTO_V5:
275775af9e56SDave Turvene 		priv->hw_init = alps_hw_init_dolphin_v1;
2758ee65d4b3SYunkang Tang 		priv->process_packet = alps_process_touchpad_packet_v3_v5;
275975af9e56SDave Turvene 		priv->decode_fields = alps_decode_dolphin;
2760688ea364SHans de Goede 		priv->set_abs_params = alps_set_abs_params_semi_mt;
276175af9e56SDave Turvene 		priv->nibble_commands = alps_v3_nibble_commands;
276275af9e56SDave Turvene 		priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
276375af9e56SDave Turvene 		priv->x_bits = 23;
276475af9e56SDave Turvene 		priv->y_bits = 12;
2765c164c147SDmitry Torokhov 
2766c164c147SDmitry Torokhov 		if (alps_dolphin_get_device_area(psmouse, priv))
2767c164c147SDmitry Torokhov 			return -EIO;
2768c164c147SDmitry Torokhov 
276975af9e56SDave Turvene 		break;
27703296f71cSDmitry Torokhov 
277195f75e91SYunkang Tang 	case ALPS_PROTO_V6:
277295f75e91SYunkang Tang 		priv->hw_init = alps_hw_init_v6;
277395f75e91SYunkang Tang 		priv->process_packet = alps_process_packet_v6;
277495f75e91SYunkang Tang 		priv->set_abs_params = alps_set_abs_params_st;
277595f75e91SYunkang Tang 		priv->nibble_commands = alps_v6_nibble_commands;
277695f75e91SYunkang Tang 		priv->x_max = 2047;
277795f75e91SYunkang Tang 		priv->y_max = 1535;
277895f75e91SYunkang Tang 		break;
27793296f71cSDmitry Torokhov 
27803808843cSYunkang Tang 	case ALPS_PROTO_V7:
27813808843cSYunkang Tang 		priv->hw_init = alps_hw_init_v7;
27823808843cSYunkang Tang 		priv->process_packet = alps_process_packet_v7;
27833808843cSYunkang Tang 		priv->decode_fields = alps_decode_packet_v7;
27848eccd393SMasaki Ota 		priv->set_abs_params = alps_set_abs_params_v7;
27853808843cSYunkang Tang 		priv->nibble_commands = alps_v3_nibble_commands;
27863808843cSYunkang Tang 		priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2787c164c147SDmitry Torokhov 		priv->x_max = 0xfff;
2788c164c147SDmitry Torokhov 		priv->y_max = 0x7ff;
27893808843cSYunkang Tang 
27903808843cSYunkang Tang 		if (priv->fw_ver[1] != 0xba)
27913808843cSYunkang Tang 			priv->flags |= ALPS_BUTTONPAD;
27923296f71cSDmitry Torokhov 
2793dae928ecSPali Rohár 		if (alps_probe_trackstick_v3_v7(psmouse, ALPS_REG_BASE_V7) < 0)
2794dae928ecSPali Rohár 			priv->flags &= ~ALPS_DUALPOINT;
2795dae928ecSPali Rohár 
27963808843cSYunkang Tang 		break;
27973db5b9f7SMasaki Ota 
27983db5b9f7SMasaki Ota 	case ALPS_PROTO_V8:
27993db5b9f7SMasaki Ota 		priv->hw_init = alps_hw_init_ss4_v2;
28003db5b9f7SMasaki Ota 		priv->process_packet = alps_process_packet_ss4_v2;
28013db5b9f7SMasaki Ota 		priv->decode_fields = alps_decode_ss4_v2;
28023db5b9f7SMasaki Ota 		priv->set_abs_params = alps_set_abs_params_ss4_v2;
28033db5b9f7SMasaki Ota 		priv->nibble_commands = alps_v3_nibble_commands;
28043db5b9f7SMasaki Ota 		priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
28053db5b9f7SMasaki Ota 
28063db5b9f7SMasaki Ota 		if (alps_set_defaults_ss4_v2(psmouse, priv))
28073db5b9f7SMasaki Ota 			return -EIO;
28083db5b9f7SMasaki Ota 
28093db5b9f7SMasaki Ota 		break;
281025bded7cSSeth Forshee 	}
28113296f71cSDmitry Torokhov 
28123296f71cSDmitry Torokhov 	return 0;
281325bded7cSSeth Forshee }
281425bded7cSSeth Forshee 
28153296f71cSDmitry Torokhov static const struct alps_protocol_info *alps_match_table(unsigned char *e7,
28163296f71cSDmitry Torokhov 							 unsigned char *ec)
28172e992cc0SKevin Cernekee {
2818b5d6b851SKevin Cernekee 	const struct alps_model_info *model;
28192e992cc0SKevin Cernekee 	int i;
28202e992cc0SKevin Cernekee 
2821b5d6b851SKevin Cernekee 	for (i = 0; i < ARRAY_SIZE(alps_model_data); i++) {
2822b5d6b851SKevin Cernekee 		model = &alps_model_data[i];
2823b5d6b851SKevin Cernekee 
2824a3cbfd56SPali Rohár 		if (!memcmp(e7, model->signature, sizeof(model->signature)))
28253296f71cSDmitry Torokhov 			return &model->protocol_info;
2826b5d6b851SKevin Cernekee 	}
2827b5d6b851SKevin Cernekee 
28283296f71cSDmitry Torokhov 	return NULL;
2829b5d6b851SKevin Cernekee }
2830b5d6b851SKevin Cernekee 
2831b5d6b851SKevin Cernekee static int alps_identify(struct psmouse *psmouse, struct alps_data *priv)
2832b5d6b851SKevin Cernekee {
28333296f71cSDmitry Torokhov 	const struct alps_protocol_info *protocol;
2834b5d6b851SKevin Cernekee 	unsigned char e6[4], e7[4], ec[4];
2835a09221e8SDmitry Torokhov 	int error;
2836b5d6b851SKevin Cernekee 
28372e992cc0SKevin Cernekee 	/*
28382e992cc0SKevin Cernekee 	 * First try "E6 report".
28392e992cc0SKevin Cernekee 	 * ALPS should return 0,0,10 or 0,0,100 if no buttons are pressed.
28402e992cc0SKevin Cernekee 	 * The bits 0-2 of the first byte will be 1s if some buttons are
28412e992cc0SKevin Cernekee 	 * pressed.
28422e992cc0SKevin Cernekee 	 */
2843b5d6b851SKevin Cernekee 	if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2844b5d6b851SKevin Cernekee 			 PSMOUSE_CMD_SETSCALE11, e6))
2845b5d6b851SKevin Cernekee 		return -EIO;
28462e992cc0SKevin Cernekee 
2847b5d6b851SKevin Cernekee 	if ((e6[0] & 0xf8) != 0 || e6[1] != 0 || (e6[2] != 10 && e6[2] != 100))
2848b5d6b851SKevin Cernekee 		return -EINVAL;
28492e992cc0SKevin Cernekee 
28502e992cc0SKevin Cernekee 	/*
2851b5d6b851SKevin Cernekee 	 * Now get the "E7" and "EC" reports.  These will uniquely identify
2852b5d6b851SKevin Cernekee 	 * most ALPS touchpads.
28532e992cc0SKevin Cernekee 	 */
2854b5d6b851SKevin Cernekee 	if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2855b5d6b851SKevin Cernekee 			 PSMOUSE_CMD_SETSCALE21, e7) ||
2856b5d6b851SKevin Cernekee 	    alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2857b5d6b851SKevin Cernekee 			 PSMOUSE_CMD_RESET_WRAP, ec) ||
2858b5d6b851SKevin Cernekee 	    alps_exit_command_mode(psmouse))
2859b5d6b851SKevin Cernekee 		return -EIO;
28602e992cc0SKevin Cernekee 
28613296f71cSDmitry Torokhov 	protocol = alps_match_table(e7, ec);
28623296f71cSDmitry Torokhov 	if (!protocol) {
286309c398bcSPali Rohár 		if (e7[0] == 0x73 && e7[1] == 0x02 && e7[2] == 0x64 &&
286409c398bcSPali Rohár 			   ec[2] == 0x8a) {
286509c398bcSPali Rohár 			protocol = &alps_v4_protocol_data;
286609c398bcSPali Rohár 		} else if (e7[0] == 0x73 && e7[1] == 0x03 && e7[2] == 0x50 &&
28673296f71cSDmitry Torokhov 			   ec[0] == 0x73 && (ec[1] == 0x01 || ec[1] == 0x02)) {
28683296f71cSDmitry Torokhov 			protocol = &alps_v5_protocol_data;
28693296f71cSDmitry Torokhov 		} else if (ec[0] == 0x88 &&
28703296f71cSDmitry Torokhov 			   ((ec[1] & 0xf0) == 0xb0 || (ec[1] & 0xf0) == 0xc0)) {
28713296f71cSDmitry Torokhov 			protocol = &alps_v7_protocol_data;
28723296f71cSDmitry Torokhov 		} else if (ec[0] == 0x88 && ec[1] == 0x08) {
28733296f71cSDmitry Torokhov 			protocol = &alps_v3_rushmore_data;
28743296f71cSDmitry Torokhov 		} else if (ec[0] == 0x88 && ec[1] == 0x07 &&
28753296f71cSDmitry Torokhov 			   ec[2] >= 0x90 && ec[2] <= 0x9d) {
28763296f71cSDmitry Torokhov 			protocol = &alps_v3_protocol_data;
28773db5b9f7SMasaki Ota 		} else if (e7[0] == 0x73 && e7[1] == 0x03 &&
2878e7348396SMasaki Ota 			   (e7[2] == 0x14 || e7[2] == 0x28)) {
2879aeaa881fSBen Gamari 			protocol = &alps_v8_protocol_data;
2880c9815232SPali Rohár 		} else if (e7[0] == 0x73 && e7[1] == 0x03 && e7[2] == 0xc8) {
2881c9815232SPali Rohár 			protocol = &alps_v9_protocol_data;
2882c9815232SPali Rohár 			psmouse_warn(psmouse,
2883c9815232SPali Rohár 				     "Unsupported ALPS V9 touchpad: E7=%3ph, EC=%3ph\n",
2884c9815232SPali Rohár 				     e7, ec);
2885c9815232SPali Rohár 			return -EINVAL;
28863296f71cSDmitry Torokhov 		} else {
28873296f71cSDmitry Torokhov 			psmouse_dbg(psmouse,
28883296f71cSDmitry Torokhov 				    "Likely not an ALPS touchpad: E7=%3ph, EC=%3ph\n", e7, ec);
28893296f71cSDmitry Torokhov 			return -EINVAL;
28903296f71cSDmitry Torokhov 		}
28913296f71cSDmitry Torokhov 	}
28923296f71cSDmitry Torokhov 
2893a09221e8SDmitry Torokhov 	if (priv) {
2894e7348396SMasaki Ota 		/* Save Device ID and Firmware version */
2895e7348396SMasaki Ota 		memcpy(priv->dev_id, e7, 3);
2896c0cd17f6SHans de Goede 		memcpy(priv->fw_ver, ec, 3);
2897a09221e8SDmitry Torokhov 		error = alps_set_protocol(psmouse, priv, protocol);
2898a09221e8SDmitry Torokhov 		if (error)
2899a09221e8SDmitry Torokhov 			return error;
2900a09221e8SDmitry Torokhov 	}
2901c0cd17f6SHans de Goede 
2902a09221e8SDmitry Torokhov 	return 0;
29032e992cc0SKevin Cernekee }
29042e992cc0SKevin Cernekee 
29051e0c5b12SDmitry Torokhov static int alps_reconnect(struct psmouse *psmouse)
29061e0c5b12SDmitry Torokhov {
2907b5d6b851SKevin Cernekee 	struct alps_data *priv = psmouse->private;
290871bb21b6SMaxim Levitsky 
29091e0c5b12SDmitry Torokhov 	psmouse_reset(psmouse);
29101e0c5b12SDmitry Torokhov 
2911b5d6b851SKevin Cernekee 	if (alps_identify(psmouse, priv) < 0)
29121e0c5b12SDmitry Torokhov 		return -1;
29131e0c5b12SDmitry Torokhov 
291424af5cb9SKevin Cernekee 	return priv->hw_init(psmouse);
29151da177e4SLinus Torvalds }
29161da177e4SLinus Torvalds 
29171da177e4SLinus Torvalds static void alps_disconnect(struct psmouse *psmouse)
29181da177e4SLinus Torvalds {
29191da177e4SLinus Torvalds 	struct alps_data *priv = psmouse->private;
29202e5b636bSDmitry Torokhov 
29211da177e4SLinus Torvalds 	psmouse_reset(psmouse);
29221d9f2626SSebastian Kapfer 	del_timer_sync(&priv->timer);
292304aae283SPali Rohár 	if (priv->dev2)
29242e5b636bSDmitry Torokhov 		input_unregister_device(priv->dev2);
292504aae283SPali Rohár 	if (!IS_ERR_OR_NULL(priv->dev3))
292604aae283SPali Rohár 		input_unregister_device(priv->dev3);
29271da177e4SLinus Torvalds 	kfree(priv);
29281da177e4SLinus Torvalds }
29291da177e4SLinus Torvalds 
293024af5cb9SKevin Cernekee static void alps_set_abs_params_st(struct alps_data *priv,
293124af5cb9SKevin Cernekee 				   struct input_dev *dev1)
293224af5cb9SKevin Cernekee {
293395f75e91SYunkang Tang 	input_set_abs_params(dev1, ABS_X, 0, priv->x_max, 0, 0);
293495f75e91SYunkang Tang 	input_set_abs_params(dev1, ABS_Y, 0, priv->y_max, 0, 0);
29358eccd393SMasaki Ota 	input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
293624af5cb9SKevin Cernekee }
293724af5cb9SKevin Cernekee 
29388eccd393SMasaki Ota static void alps_set_abs_params_mt_common(struct alps_data *priv,
293924af5cb9SKevin Cernekee 					  struct input_dev *dev1)
294024af5cb9SKevin Cernekee {
29417a9f73e7SKevin Cernekee 	input_set_abs_params(dev1, ABS_MT_POSITION_X, 0, priv->x_max, 0, 0);
29427a9f73e7SKevin Cernekee 	input_set_abs_params(dev1, ABS_MT_POSITION_Y, 0, priv->y_max, 0, 0);
294324af5cb9SKevin Cernekee 
2944f3f33c67SHans de Goede 	input_abs_set_res(dev1, ABS_MT_POSITION_X, priv->x_res);
2945f3f33c67SHans de Goede 	input_abs_set_res(dev1, ABS_MT_POSITION_Y, priv->y_res);
2946f3f33c67SHans de Goede 
294724af5cb9SKevin Cernekee 	set_bit(BTN_TOOL_TRIPLETAP, dev1->keybit);
294824af5cb9SKevin Cernekee 	set_bit(BTN_TOOL_QUADTAP, dev1->keybit);
29498eccd393SMasaki Ota }
29503808843cSYunkang Tang 
2951688ea364SHans de Goede static void alps_set_abs_params_semi_mt(struct alps_data *priv,
29528eccd393SMasaki Ota 					struct input_dev *dev1)
29538eccd393SMasaki Ota {
29548eccd393SMasaki Ota 	alps_set_abs_params_mt_common(priv, dev1);
29558eccd393SMasaki Ota 	input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
29568eccd393SMasaki Ota 
29578eccd393SMasaki Ota 	input_mt_init_slots(dev1, MAX_TOUCHES,
29588eccd393SMasaki Ota 			    INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
29591662c033SHans de Goede 				INPUT_MT_SEMI_MT);
29608eccd393SMasaki Ota }
29618eccd393SMasaki Ota 
29628eccd393SMasaki Ota static void alps_set_abs_params_v7(struct alps_data *priv,
29638eccd393SMasaki Ota 				   struct input_dev *dev1)
29648eccd393SMasaki Ota {
29658eccd393SMasaki Ota 	alps_set_abs_params_mt_common(priv, dev1);
29668d289842SMasaki Ota 	set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
29678eccd393SMasaki Ota 
29688eccd393SMasaki Ota 	input_mt_init_slots(dev1, MAX_TOUCHES,
29698eccd393SMasaki Ota 			    INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
29708eccd393SMasaki Ota 				INPUT_MT_TRACK);
29713db5b9f7SMasaki Ota 
29723db5b9f7SMasaki Ota 	set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
29733db5b9f7SMasaki Ota }
29743db5b9f7SMasaki Ota 
29753db5b9f7SMasaki Ota static void alps_set_abs_params_ss4_v2(struct alps_data *priv,
29763db5b9f7SMasaki Ota 				       struct input_dev *dev1)
29773db5b9f7SMasaki Ota {
29783db5b9f7SMasaki Ota 	alps_set_abs_params_mt_common(priv, dev1);
29793db5b9f7SMasaki Ota 	input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
29803db5b9f7SMasaki Ota 	set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
29813db5b9f7SMasaki Ota 
29823db5b9f7SMasaki Ota 	input_mt_init_slots(dev1, MAX_TOUCHES,
29833db5b9f7SMasaki Ota 			    INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
29843db5b9f7SMasaki Ota 				INPUT_MT_TRACK);
298524af5cb9SKevin Cernekee }
298624af5cb9SKevin Cernekee 
29871da177e4SLinus Torvalds int alps_init(struct psmouse *psmouse)
29881da177e4SLinus Torvalds {
2989a09221e8SDmitry Torokhov 	struct alps_data *priv = psmouse->private;
299004aae283SPali Rohár 	struct input_dev *dev1 = psmouse->dev;
2991a09221e8SDmitry Torokhov 	int error;
29921da177e4SLinus Torvalds 
2993a09221e8SDmitry Torokhov 	error = priv->hw_init(psmouse);
2994a09221e8SDmitry Torokhov 	if (error)
29951da177e4SLinus Torvalds 		goto init_fail;
29961da177e4SLinus Torvalds 
29977105d2eaSDmitry Torokhov 	/*
29987105d2eaSDmitry Torokhov 	 * Undo part of setup done for us by psmouse core since touchpad
29997105d2eaSDmitry Torokhov 	 * is not a relative device.
30007105d2eaSDmitry Torokhov 	 */
30017105d2eaSDmitry Torokhov 	__clear_bit(EV_REL, dev1->evbit);
30027105d2eaSDmitry Torokhov 	__clear_bit(REL_X, dev1->relbit);
30037105d2eaSDmitry Torokhov 	__clear_bit(REL_Y, dev1->relbit);
30047105d2eaSDmitry Torokhov 
30057105d2eaSDmitry Torokhov 	/*
30067105d2eaSDmitry Torokhov 	 * Now set up our capabilities.
30077105d2eaSDmitry Torokhov 	 */
30087b19ada2SJiri Slaby 	dev1->evbit[BIT_WORD(EV_KEY)] |= BIT_MASK(EV_KEY);
30097b19ada2SJiri Slaby 	dev1->keybit[BIT_WORD(BTN_TOUCH)] |= BIT_MASK(BTN_TOUCH);
30107b19ada2SJiri Slaby 	dev1->keybit[BIT_WORD(BTN_TOOL_FINGER)] |= BIT_MASK(BTN_TOOL_FINGER);
301171bb21b6SMaxim Levitsky 	dev1->keybit[BIT_WORD(BTN_LEFT)] |=
301271bb21b6SMaxim Levitsky 		BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
30131da177e4SLinus Torvalds 
30147b19ada2SJiri Slaby 	dev1->evbit[BIT_WORD(EV_ABS)] |= BIT_MASK(EV_ABS);
301525bded7cSSeth Forshee 
301624af5cb9SKevin Cernekee 	priv->set_abs_params(priv, dev1);
30171da177e4SLinus Torvalds 
301899df65e7SKevin Cernekee 	if (priv->flags & ALPS_WHEEL) {
30197b19ada2SJiri Slaby 		dev1->evbit[BIT_WORD(EV_REL)] |= BIT_MASK(EV_REL);
30207b19ada2SJiri Slaby 		dev1->relbit[BIT_WORD(REL_WHEEL)] |= BIT_MASK(REL_WHEEL);
30211da177e4SLinus Torvalds 	}
30221da177e4SLinus Torvalds 
302399df65e7SKevin Cernekee 	if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
30247b19ada2SJiri Slaby 		dev1->keybit[BIT_WORD(BTN_FORWARD)] |= BIT_MASK(BTN_FORWARD);
30257b19ada2SJiri Slaby 		dev1->keybit[BIT_WORD(BTN_BACK)] |= BIT_MASK(BTN_BACK);
30261da177e4SLinus Torvalds 	}
30271da177e4SLinus Torvalds 
302899df65e7SKevin Cernekee 	if (priv->flags & ALPS_FOUR_BUTTONS) {
302971bb21b6SMaxim Levitsky 		dev1->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_0);
303071bb21b6SMaxim Levitsky 		dev1->keybit[BIT_WORD(BTN_1)] |= BIT_MASK(BTN_1);
303171bb21b6SMaxim Levitsky 		dev1->keybit[BIT_WORD(BTN_2)] |= BIT_MASK(BTN_2);
303271bb21b6SMaxim Levitsky 		dev1->keybit[BIT_WORD(BTN_3)] |= BIT_MASK(BTN_3);
30333808843cSYunkang Tang 	} else if (priv->flags & ALPS_BUTTONPAD) {
30343808843cSYunkang Tang 		set_bit(INPUT_PROP_BUTTONPAD, dev1->propbit);
30353808843cSYunkang Tang 		clear_bit(BTN_RIGHT, dev1->keybit);
303671bb21b6SMaxim Levitsky 	} else {
303771bb21b6SMaxim Levitsky 		dev1->keybit[BIT_WORD(BTN_MIDDLE)] |= BIT_MASK(BTN_MIDDLE);
303871bb21b6SMaxim Levitsky 	}
303971bb21b6SMaxim Levitsky 
3040dfba8600SPali Rohár 	if (priv->flags & ALPS_DUALPOINT) {
304104aae283SPali Rohár 		struct input_dev *dev2;
304204aae283SPali Rohár 
304304aae283SPali Rohár 		dev2 = input_allocate_device();
304404aae283SPali Rohár 		if (!dev2) {
304504aae283SPali Rohár 			psmouse_err(psmouse,
304604aae283SPali Rohár 				    "failed to allocate trackstick device\n");
304704aae283SPali Rohár 			error = -ENOMEM;
304804aae283SPali Rohár 			goto init_fail;
304904aae283SPali Rohár 		}
305004aae283SPali Rohár 
305104aae283SPali Rohár 		snprintf(priv->phys2, sizeof(priv->phys2), "%s/input1",
305204aae283SPali Rohár 			 psmouse->ps2dev.serio->phys);
305304aae283SPali Rohár 		dev2->phys = priv->phys2;
305404aae283SPali Rohár 
3055dfba8600SPali Rohár 		/*
3056dfba8600SPali Rohár 		 * format of input device name is: "protocol vendor name"
3057dfba8600SPali Rohár 		 * see function psmouse_switch_protocol() in psmouse-base.c
3058dfba8600SPali Rohár 		 */
3059dfba8600SPali Rohár 		dev2->name = "AlpsPS/2 ALPS DualPoint Stick";
3060dfba8600SPali Rohár 
30612e5b636bSDmitry Torokhov 		dev2->id.bustype = BUS_I8042;
30622e5b636bSDmitry Torokhov 		dev2->id.vendor  = 0x0002;
306304aae283SPali Rohár 		dev2->id.product = PSMOUSE_ALPS;
306404aae283SPali Rohár 		dev2->id.version = priv->proto_version;
30651db3a345SDmitry Torokhov 		dev2->dev.parent = &psmouse->ps2dev.serio->dev;
30661da177e4SLinus Torvalds 
306704aae283SPali Rohár 		input_set_capability(dev2, EV_REL, REL_X);
306804aae283SPali Rohár 		input_set_capability(dev2, EV_REL, REL_Y);
30697ad8a106SBen Gamari 		if (priv->flags & ALPS_DUALPOINT_WITH_PRESSURE) {
30707ad8a106SBen Gamari 			input_set_capability(dev2, EV_ABS, ABS_PRESSURE);
30717ad8a106SBen Gamari 			input_set_abs_params(dev2, ABS_PRESSURE, 0, 127, 0, 0);
30727ad8a106SBen Gamari 		}
307304aae283SPali Rohár 		input_set_capability(dev2, EV_KEY, BTN_LEFT);
307404aae283SPali Rohár 		input_set_capability(dev2, EV_KEY, BTN_RIGHT);
307504aae283SPali Rohár 		input_set_capability(dev2, EV_KEY, BTN_MIDDLE);
30761da177e4SLinus Torvalds 
307701d4cd5cSHans de Goede 		__set_bit(INPUT_PROP_POINTER, dev2->propbit);
30787611392fSHans de Goede 		__set_bit(INPUT_PROP_POINTING_STICK, dev2->propbit);
30797611392fSHans de Goede 
308004aae283SPali Rohár 		error = input_register_device(dev2);
308104aae283SPali Rohár 		if (error) {
308204aae283SPali Rohár 			psmouse_err(psmouse,
308304aae283SPali Rohár 				    "failed to register trackstick device: %d\n",
308404aae283SPali Rohár 				    error);
308504aae283SPali Rohár 			input_free_device(dev2);
3086f42649e8SDmitry Torokhov 			goto init_fail;
308704aae283SPali Rohár 		}
308804aae283SPali Rohár 
308904aae283SPali Rohár 		priv->dev2 = dev2;
309004aae283SPali Rohár 	}
309104aae283SPali Rohár 
309204aae283SPali Rohár 	priv->psmouse = psmouse;
309304aae283SPali Rohár 
309404aae283SPali Rohár 	INIT_DELAYED_WORK(&priv->dev3_register_work,
309504aae283SPali Rohár 			  alps_register_bare_ps2_mouse);
30961da177e4SLinus Torvalds 
30971da177e4SLinus Torvalds 	psmouse->protocol_handler = alps_process_byte;
3098f0d5c6f4SDmitry Torokhov 	psmouse->poll = alps_poll;
30991da177e4SLinus Torvalds 	psmouse->disconnect = alps_disconnect;
31001da177e4SLinus Torvalds 	psmouse->reconnect = alps_reconnect;
310199df65e7SKevin Cernekee 	psmouse->pktsize = priv->proto_version == ALPS_PROTO_V4 ? 8 : 6;
31021da177e4SLinus Torvalds 
3103f0d5c6f4SDmitry Torokhov 	/* We are having trouble resyncing ALPS touchpads so disable it for now */
3104f0d5c6f4SDmitry Torokhov 	psmouse->resync_time = 0;
3105f0d5c6f4SDmitry Torokhov 
31069d720b34SPali Rohár 	/* Allow 2 invalid packets without resetting device */
31079d720b34SPali Rohár 	psmouse->resetafter = psmouse->pktsize * 2;
31089d720b34SPali Rohár 
31091da177e4SLinus Torvalds 	return 0;
31101da177e4SLinus Torvalds 
31111da177e4SLinus Torvalds init_fail:
3112f42649e8SDmitry Torokhov 	psmouse_reset(psmouse);
3113a09221e8SDmitry Torokhov 	/*
3114a09221e8SDmitry Torokhov 	 * Even though we did not allocate psmouse->private we do free
3115a09221e8SDmitry Torokhov 	 * it here.
3116a09221e8SDmitry Torokhov 	 */
3117a09221e8SDmitry Torokhov 	kfree(psmouse->private);
31181e0c5b12SDmitry Torokhov 	psmouse->private = NULL;
3119a09221e8SDmitry Torokhov 	return error;
31201da177e4SLinus Torvalds }
31211da177e4SLinus Torvalds 
3122b7802c5cSDmitry Torokhov int alps_detect(struct psmouse *psmouse, bool set_properties)
31231da177e4SLinus Torvalds {
3124a09221e8SDmitry Torokhov 	struct alps_data *priv;
3125a09221e8SDmitry Torokhov 	int error;
31261da177e4SLinus Torvalds 
3127a09221e8SDmitry Torokhov 	error = alps_identify(psmouse, NULL);
3128a09221e8SDmitry Torokhov 	if (error)
3129a09221e8SDmitry Torokhov 		return error;
3130a09221e8SDmitry Torokhov 
3131a09221e8SDmitry Torokhov 	/*
3132a09221e8SDmitry Torokhov 	 * Reset the device to make sure it is fully operational:
3133a09221e8SDmitry Torokhov 	 * on some laptops, like certain Dell Latitudes, we may
3134a09221e8SDmitry Torokhov 	 * fail to properly detect presence of trackstick if device
3135a09221e8SDmitry Torokhov 	 * has not been reset.
3136a09221e8SDmitry Torokhov 	 */
3137a09221e8SDmitry Torokhov 	psmouse_reset(psmouse);
3138a09221e8SDmitry Torokhov 
3139a09221e8SDmitry Torokhov 	priv = kzalloc(sizeof(struct alps_data), GFP_KERNEL);
3140a09221e8SDmitry Torokhov 	if (!priv)
3141a09221e8SDmitry Torokhov 		return -ENOMEM;
3142a09221e8SDmitry Torokhov 
3143a09221e8SDmitry Torokhov 	error = alps_identify(psmouse, priv);
314493050db2SDmitry Torokhov 	if (error) {
314593050db2SDmitry Torokhov 		kfree(priv);
3146a09221e8SDmitry Torokhov 		return error;
314793050db2SDmitry Torokhov 	}
31481da177e4SLinus Torvalds 
31491da177e4SLinus Torvalds 	if (set_properties) {
31501da177e4SLinus Torvalds 		psmouse->vendor = "ALPS";
3151a09221e8SDmitry Torokhov 		psmouse->name = priv->flags & ALPS_DUALPOINT ?
3152968ac842SDmitry Torokhov 				"DualPoint TouchPad" : "GlidePoint";
3153a09221e8SDmitry Torokhov 		psmouse->model = priv->proto_version;
3154a09221e8SDmitry Torokhov 	} else {
3155a09221e8SDmitry Torokhov 		/*
3156a09221e8SDmitry Torokhov 		 * Destroy alps_data structure we allocated earlier since
3157a09221e8SDmitry Torokhov 		 * this was just a "trial run". Otherwise we'll keep it
3158a09221e8SDmitry Torokhov 		 * to be used by alps_init() which has to be called if
3159a09221e8SDmitry Torokhov 		 * we succeed and set_properties is true.
3160a09221e8SDmitry Torokhov 		 */
3161a09221e8SDmitry Torokhov 		kfree(priv);
3162a09221e8SDmitry Torokhov 		psmouse->private = NULL;
31631da177e4SLinus Torvalds 	}
3164a09221e8SDmitry Torokhov 
31651da177e4SLinus Torvalds 	return 0;
31661da177e4SLinus Torvalds }
31671da177e4SLinus Torvalds 
3168