xref: /openbmc/linux/drivers/input/mouse/alps.c (revision 4a646580)
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 = {
1423296f71cSDmitry Torokhov 	ALPS_PROTO_V3, 0x8f, 0x8f, ALPS_DUALPOINT
1433296f71cSDmitry Torokhov };
1443296f71cSDmitry Torokhov 
1453296f71cSDmitry Torokhov static const struct alps_protocol_info alps_v3_rushmore_data = {
1463296f71cSDmitry Torokhov 	ALPS_PROTO_V3_RUSHMORE, 0x8f, 0x8f, ALPS_DUALPOINT
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 = {
1583296f71cSDmitry Torokhov 	ALPS_PROTO_V7, 0x48, 0x48, ALPS_DUALPOINT
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));
58625bded7cSSeth Forshee 	z = (packet[4] & 0x7c) >> 2;
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);
59825bded7cSSeth Forshee 
59925bded7cSSeth Forshee 	/*
60025bded7cSSeth Forshee 	 * Most ALPS models report the trackstick buttons in the touchpad
60125bded7cSSeth Forshee 	 * packets, but a few report them here. No reliable way has been
60225bded7cSSeth Forshee 	 * found to differentiate between the models upfront, so we enable
60325bded7cSSeth Forshee 	 * the quirk in response to seeing a button press in the trackstick
60425bded7cSSeth Forshee 	 * packet.
60525bded7cSSeth Forshee 	 */
60625bded7cSSeth Forshee 	left = packet[3] & 0x01;
60725bded7cSSeth Forshee 	right = packet[3] & 0x02;
60825bded7cSSeth Forshee 	middle = packet[3] & 0x04;
60925bded7cSSeth Forshee 
61025bded7cSSeth Forshee 	if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) &&
61125bded7cSSeth Forshee 	    (left || right || middle))
61225bded7cSSeth Forshee 		priv->quirks |= ALPS_QUIRK_TRACKSTICK_BUTTONS;
61325bded7cSSeth Forshee 
61425bded7cSSeth Forshee 	if (priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) {
61525bded7cSSeth Forshee 		input_report_key(dev, BTN_LEFT, left);
61625bded7cSSeth Forshee 		input_report_key(dev, BTN_RIGHT, right);
61725bded7cSSeth Forshee 		input_report_key(dev, BTN_MIDDLE, middle);
61825bded7cSSeth Forshee 	}
61925bded7cSSeth Forshee 
62025bded7cSSeth Forshee 	input_sync(dev);
62125bded7cSSeth Forshee 	return;
62225bded7cSSeth Forshee }
62325bded7cSSeth Forshee 
624f85e5001SKevin Cernekee static void alps_decode_buttons_v3(struct alps_fields *f, unsigned char *p)
625f85e5001SKevin Cernekee {
626f85e5001SKevin Cernekee 	f->left = !!(p[3] & 0x01);
627f85e5001SKevin Cernekee 	f->right = !!(p[3] & 0x02);
628f85e5001SKevin Cernekee 	f->middle = !!(p[3] & 0x04);
629f85e5001SKevin Cernekee 
630f85e5001SKevin Cernekee 	f->ts_left = !!(p[3] & 0x10);
631f85e5001SKevin Cernekee 	f->ts_right = !!(p[3] & 0x20);
632f85e5001SKevin Cernekee 	f->ts_middle = !!(p[3] & 0x40);
633f85e5001SKevin Cernekee }
634f85e5001SKevin Cernekee 
63538c11eaaSHans de Goede static int alps_decode_pinnacle(struct alps_fields *f, unsigned char *p,
636ee65d4b3SYunkang Tang 				 struct psmouse *psmouse)
637f85e5001SKevin Cernekee {
638f85e5001SKevin Cernekee 	f->first_mp = !!(p[4] & 0x40);
639f85e5001SKevin Cernekee 	f->is_mp = !!(p[0] & 0x40);
640f85e5001SKevin Cernekee 
641a839cd57SHans de Goede 	if (f->is_mp) {
642f85e5001SKevin Cernekee 		f->fingers = (p[5] & 0x3) + 1;
643f85e5001SKevin Cernekee 		f->x_map = ((p[4] & 0x7e) << 8) |
644f85e5001SKevin Cernekee 			   ((p[1] & 0x7f) << 2) |
645f85e5001SKevin Cernekee 			   ((p[0] & 0x30) >> 4);
646f85e5001SKevin Cernekee 		f->y_map = ((p[3] & 0x70) << 4) |
647f85e5001SKevin Cernekee 			   ((p[2] & 0x7f) << 1) |
648f85e5001SKevin Cernekee 			   (p[4] & 0x01);
649a839cd57SHans de Goede 	} else {
65002d04254SHans de Goede 		f->st.x = ((p[1] & 0x7f) << 4) | ((p[4] & 0x30) >> 2) |
651f85e5001SKevin Cernekee 		       ((p[0] & 0x30) >> 4);
65202d04254SHans de Goede 		f->st.y = ((p[2] & 0x7f) << 4) | (p[4] & 0x0f);
65302d04254SHans de Goede 		f->pressure = p[5] & 0x7f;
654f85e5001SKevin Cernekee 
655f85e5001SKevin Cernekee 		alps_decode_buttons_v3(f, p);
656a839cd57SHans de Goede 	}
65738c11eaaSHans de Goede 
65838c11eaaSHans de Goede 	return 0;
659f85e5001SKevin Cernekee }
660f85e5001SKevin Cernekee 
66138c11eaaSHans de Goede static int alps_decode_rushmore(struct alps_fields *f, unsigned char *p,
662ee65d4b3SYunkang Tang 				 struct psmouse *psmouse)
6631302bac3SKevin Cernekee {
664aab9cf7bSHans de Goede 	f->first_mp = !!(p[4] & 0x40);
665f105e34aSYunkang Tang 	f->is_mp = !!(p[5] & 0x40);
666aab9cf7bSHans de Goede 
667a839cd57SHans de Goede 	if (f->is_mp) {
668f105e34aSYunkang Tang 		f->fingers = max((p[5] & 0x3), ((p[5] >> 2) & 0x3)) + 1;
669aab9cf7bSHans de Goede 		f->x_map = ((p[5] & 0x10) << 11) |
670aab9cf7bSHans de Goede 			   ((p[4] & 0x7e) << 8) |
671aab9cf7bSHans de Goede 			   ((p[1] & 0x7f) << 2) |
672aab9cf7bSHans de Goede 			   ((p[0] & 0x30) >> 4);
673aab9cf7bSHans de Goede 		f->y_map = ((p[5] & 0x20) << 6) |
674aab9cf7bSHans de Goede 			   ((p[3] & 0x70) << 4) |
675aab9cf7bSHans de Goede 			   ((p[2] & 0x7f) << 1) |
676aab9cf7bSHans de Goede 			   (p[4] & 0x01);
677a839cd57SHans de Goede 	} else {
678aab9cf7bSHans de Goede 		f->st.x = ((p[1] & 0x7f) << 4) | ((p[4] & 0x30) >> 2) |
679aab9cf7bSHans de Goede 		       ((p[0] & 0x30) >> 4);
680aab9cf7bSHans de Goede 		f->st.y = ((p[2] & 0x7f) << 4) | (p[4] & 0x0f);
681aab9cf7bSHans de Goede 		f->pressure = p[5] & 0x7f;
682aab9cf7bSHans de Goede 
683aab9cf7bSHans de Goede 		alps_decode_buttons_v3(f, p);
684a839cd57SHans de Goede 	}
68538c11eaaSHans de Goede 
68638c11eaaSHans de Goede 	return 0;
6871302bac3SKevin Cernekee }
6881302bac3SKevin Cernekee 
68938c11eaaSHans de Goede static int alps_decode_dolphin(struct alps_fields *f, unsigned char *p,
690ee65d4b3SYunkang Tang 				struct psmouse *psmouse)
69175af9e56SDave Turvene {
692ee65d4b3SYunkang Tang 	u64 palm_data = 0;
693ee65d4b3SYunkang Tang 	struct alps_data *priv = psmouse->private;
694ee65d4b3SYunkang Tang 
69575af9e56SDave Turvene 	f->first_mp = !!(p[0] & 0x02);
69675af9e56SDave Turvene 	f->is_mp = !!(p[0] & 0x20);
69775af9e56SDave Turvene 
698ee65d4b3SYunkang Tang 	if (!f->is_mp) {
69902d04254SHans de Goede 		f->st.x = ((p[1] & 0x7f) | ((p[4] & 0x0f) << 7));
70002d04254SHans de Goede 		f->st.y = ((p[2] & 0x7f) | ((p[4] & 0xf0) << 3));
70102d04254SHans de Goede 		f->pressure = (p[0] & 4) ? 0 : p[5] & 0x7f;
70275af9e56SDave Turvene 		alps_decode_buttons_v3(f, p);
703ee65d4b3SYunkang Tang 	} else {
704ee65d4b3SYunkang Tang 		f->fingers = ((p[0] & 0x6) >> 1 |
705ee65d4b3SYunkang Tang 		     (p[0] & 0x10) >> 2);
706ee65d4b3SYunkang Tang 
707ee65d4b3SYunkang Tang 		palm_data = (p[1] & 0x7f) |
708ee65d4b3SYunkang Tang 			    ((p[2] & 0x7f) << 7) |
709ee65d4b3SYunkang Tang 			    ((p[4] & 0x7f) << 14) |
710ee65d4b3SYunkang Tang 			    ((p[5] & 0x7f) << 21) |
711ee65d4b3SYunkang Tang 			    ((p[3] & 0x07) << 28) |
712ee65d4b3SYunkang Tang 			    (((u64)p[3] & 0x70) << 27) |
713ee65d4b3SYunkang Tang 			    (((u64)p[0] & 0x01) << 34);
714ee65d4b3SYunkang Tang 
715ee65d4b3SYunkang Tang 		/* Y-profile is stored in P(0) to p(n-1), n = y_bits; */
716ee65d4b3SYunkang Tang 		f->y_map = palm_data & (BIT(priv->y_bits) - 1);
717ee65d4b3SYunkang Tang 
718ee65d4b3SYunkang Tang 		/* X-profile is stored in p(n) to p(n+m-1), m = x_bits; */
719ee65d4b3SYunkang Tang 		f->x_map = (palm_data >> priv->y_bits) &
720ee65d4b3SYunkang Tang 			   (BIT(priv->x_bits) - 1);
721ee65d4b3SYunkang Tang 	}
72238c11eaaSHans de Goede 
72338c11eaaSHans de Goede 	return 0;
72475af9e56SDave Turvene }
72575af9e56SDave Turvene 
726ee65d4b3SYunkang Tang static void alps_process_touchpad_packet_v3_v5(struct psmouse *psmouse)
72725bded7cSSeth Forshee {
72825bded7cSSeth Forshee 	struct alps_data *priv = psmouse->private;
72925bded7cSSeth Forshee 	unsigned char *packet = psmouse->packet;
73025bded7cSSeth Forshee 	struct input_dev *dev2 = priv->dev2;
73102d04254SHans de Goede 	struct alps_fields *f = &priv->f;
73202d04254SHans de Goede 	int fingers = 0;
733f85e5001SKevin Cernekee 
73402d04254SHans de Goede 	memset(f, 0, sizeof(*f));
73502d04254SHans de Goede 
73602d04254SHans de Goede 	priv->decode_fields(f, packet, psmouse);
73725bded7cSSeth Forshee 
73825bded7cSSeth Forshee 	/*
73901ce661fSSeth Forshee 	 * There's no single feature of touchpad position and bitmap packets
74001ce661fSSeth Forshee 	 * that can be used to distinguish between them. We rely on the fact
74101ce661fSSeth Forshee 	 * that a bitmap packet should always follow a position packet with
74201ce661fSSeth Forshee 	 * bit 6 of packet[4] set.
74325bded7cSSeth Forshee 	 */
74425bded7cSSeth Forshee 	if (priv->multi_packet) {
74525bded7cSSeth Forshee 		/*
74625bded7cSSeth Forshee 		 * Sometimes a position packet will indicate a multi-packet
74725bded7cSSeth Forshee 		 * sequence, but then what follows is another position
74825bded7cSSeth Forshee 		 * packet. Check for this, and when it happens process the
74925bded7cSSeth Forshee 		 * position packet as usual.
75025bded7cSSeth Forshee 		 */
75102d04254SHans de Goede 		if (f->is_mp) {
75202d04254SHans de Goede 			fingers = f->fingers;
75344b77f38SHans de Goede 			/*
75444b77f38SHans de Goede 			 * Bitmap processing uses position packet's coordinate
75544b77f38SHans de Goede 			 * data, so we need to do decode it first.
75644b77f38SHans de Goede 			 */
75744b77f38SHans de Goede 			priv->decode_fields(f, priv->multi_data, psmouse);
75802d04254SHans de Goede 			if (alps_process_bitmap(priv, f) == 0)
75920bea68bSHans de Goede 				fingers = 0; /* Use st data */
76001ce661fSSeth Forshee 		} else {
76101ce661fSSeth Forshee 			priv->multi_packet = 0;
76225bded7cSSeth Forshee 		}
76325bded7cSSeth Forshee 	}
76425bded7cSSeth Forshee 
76501ce661fSSeth Forshee 	/*
76601ce661fSSeth Forshee 	 * Bit 6 of byte 0 is not usually set in position packets. The only
76701ce661fSSeth Forshee 	 * times it seems to be set is in situations where the data is
76801ce661fSSeth Forshee 	 * suspect anyway, e.g. a palm resting flat on the touchpad. Given
76901ce661fSSeth Forshee 	 * this combined with the fact that this bit is useful for filtering
77001ce661fSSeth Forshee 	 * out misidentified bitmap packets, we reject anything with this
77101ce661fSSeth Forshee 	 * bit set.
77201ce661fSSeth Forshee 	 */
77302d04254SHans de Goede 	if (f->is_mp)
77401ce661fSSeth Forshee 		return;
77501ce661fSSeth Forshee 
77602d04254SHans de Goede 	if (!priv->multi_packet && f->first_mp) {
77725bded7cSSeth Forshee 		priv->multi_packet = 1;
77801ce661fSSeth Forshee 		memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
77901ce661fSSeth Forshee 		return;
78001ce661fSSeth Forshee 	}
78101ce661fSSeth Forshee 
78225bded7cSSeth Forshee 	priv->multi_packet = 0;
78325bded7cSSeth Forshee 
78425bded7cSSeth Forshee 	/*
78525bded7cSSeth Forshee 	 * Sometimes the hardware sends a single packet with z = 0
78625bded7cSSeth Forshee 	 * in the middle of a stream. Real releases generate packets
78725bded7cSSeth Forshee 	 * with x, y, and z all zero, so these seem to be flukes.
78825bded7cSSeth Forshee 	 * Ignore them.
78925bded7cSSeth Forshee 	 */
79002d04254SHans de Goede 	if (f->st.x && f->st.y && !f->pressure)
79125bded7cSSeth Forshee 		return;
79225bded7cSSeth Forshee 
79368c21870SHans de Goede 	alps_report_semi_mt_data(psmouse, fingers);
79425bded7cSSeth Forshee 
79534412ba2SPali Rohár 	if ((priv->flags & ALPS_DUALPOINT) &&
79634412ba2SPali Rohár 	    !(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS)) {
79702d04254SHans de Goede 		input_report_key(dev2, BTN_LEFT, f->ts_left);
79802d04254SHans de Goede 		input_report_key(dev2, BTN_RIGHT, f->ts_right);
79902d04254SHans de Goede 		input_report_key(dev2, BTN_MIDDLE, f->ts_middle);
80025bded7cSSeth Forshee 		input_sync(dev2);
80125bded7cSSeth Forshee 	}
80225bded7cSSeth Forshee }
80325bded7cSSeth Forshee 
80425bded7cSSeth Forshee static void alps_process_packet_v3(struct psmouse *psmouse)
80525bded7cSSeth Forshee {
80625bded7cSSeth Forshee 	unsigned char *packet = psmouse->packet;
80725bded7cSSeth Forshee 
80825bded7cSSeth Forshee 	/*
80925bded7cSSeth Forshee 	 * v3 protocol packets come in three types, two representing
81025bded7cSSeth Forshee 	 * touchpad data and one representing trackstick data.
81125bded7cSSeth Forshee 	 * Trackstick packets seem to be distinguished by always
81225bded7cSSeth Forshee 	 * having 0x3f in the last byte. This value has never been
81325bded7cSSeth Forshee 	 * observed in the last byte of either of the other types
81425bded7cSSeth Forshee 	 * of packets.
81525bded7cSSeth Forshee 	 */
81625bded7cSSeth Forshee 	if (packet[5] == 0x3f) {
81725bded7cSSeth Forshee 		alps_process_trackstick_packet_v3(psmouse);
81825bded7cSSeth Forshee 		return;
81925bded7cSSeth Forshee 	}
82025bded7cSSeth Forshee 
821ee65d4b3SYunkang Tang 	alps_process_touchpad_packet_v3_v5(psmouse);
82225bded7cSSeth Forshee }
82325bded7cSSeth Forshee 
82495f75e91SYunkang Tang static void alps_process_packet_v6(struct psmouse *psmouse)
82595f75e91SYunkang Tang {
82695f75e91SYunkang Tang 	struct alps_data *priv = psmouse->private;
82795f75e91SYunkang Tang 	unsigned char *packet = psmouse->packet;
82895f75e91SYunkang Tang 	struct input_dev *dev = psmouse->dev;
82995f75e91SYunkang Tang 	struct input_dev *dev2 = priv->dev2;
83095f75e91SYunkang Tang 	int x, y, z, left, right, middle;
83195f75e91SYunkang Tang 
83295f75e91SYunkang Tang 	/*
83395f75e91SYunkang Tang 	 * We can use Byte5 to distinguish if the packet is from Touchpad
83495f75e91SYunkang Tang 	 * or Trackpoint.
83595f75e91SYunkang Tang 	 * Touchpad:	0 - 0x7E
83695f75e91SYunkang Tang 	 * Trackpoint:	0x7F
83795f75e91SYunkang Tang 	 */
83895f75e91SYunkang Tang 	if (packet[5] == 0x7F) {
83995f75e91SYunkang Tang 		/* It should be a DualPoint when received Trackpoint packet */
84034412ba2SPali Rohár 		if (!(priv->flags & ALPS_DUALPOINT)) {
84134412ba2SPali Rohár 			psmouse_warn(psmouse,
84234412ba2SPali Rohár 				     "Rejected trackstick packet from non DualPoint device");
84395f75e91SYunkang Tang 			return;
84434412ba2SPali Rohár 		}
84595f75e91SYunkang Tang 
84695f75e91SYunkang Tang 		/* Trackpoint packet */
84795f75e91SYunkang Tang 		x = packet[1] | ((packet[3] & 0x20) << 2);
84895f75e91SYunkang Tang 		y = packet[2] | ((packet[3] & 0x40) << 1);
84995f75e91SYunkang Tang 		z = packet[4];
85095f75e91SYunkang Tang 		left = packet[3] & 0x01;
85195f75e91SYunkang Tang 		right = packet[3] & 0x02;
85295f75e91SYunkang Tang 		middle = packet[3] & 0x04;
85395f75e91SYunkang Tang 
85495f75e91SYunkang Tang 		/* To prevent the cursor jump when finger lifted */
85595f75e91SYunkang Tang 		if (x == 0x7F && y == 0x7F && z == 0x7F)
85695f75e91SYunkang Tang 			x = y = z = 0;
85795f75e91SYunkang Tang 
85895f75e91SYunkang Tang 		/* Divide 4 since trackpoint's speed is too fast */
85995f75e91SYunkang Tang 		input_report_rel(dev2, REL_X, (char)x / 4);
86095f75e91SYunkang Tang 		input_report_rel(dev2, REL_Y, -((char)y / 4));
86195f75e91SYunkang Tang 
86295f75e91SYunkang Tang 		input_report_key(dev2, BTN_LEFT, left);
86395f75e91SYunkang Tang 		input_report_key(dev2, BTN_RIGHT, right);
86495f75e91SYunkang Tang 		input_report_key(dev2, BTN_MIDDLE, middle);
86595f75e91SYunkang Tang 
86695f75e91SYunkang Tang 		input_sync(dev2);
86795f75e91SYunkang Tang 		return;
86895f75e91SYunkang Tang 	}
86995f75e91SYunkang Tang 
87095f75e91SYunkang Tang 	/* Touchpad packet */
87195f75e91SYunkang Tang 	x = packet[1] | ((packet[3] & 0x78) << 4);
87295f75e91SYunkang Tang 	y = packet[2] | ((packet[4] & 0x78) << 4);
87395f75e91SYunkang Tang 	z = packet[5];
87495f75e91SYunkang Tang 	left = packet[3] & 0x01;
87595f75e91SYunkang Tang 	right = packet[3] & 0x02;
87695f75e91SYunkang Tang 
87795f75e91SYunkang Tang 	if (z > 30)
87895f75e91SYunkang Tang 		input_report_key(dev, BTN_TOUCH, 1);
87995f75e91SYunkang Tang 	if (z < 25)
88095f75e91SYunkang Tang 		input_report_key(dev, BTN_TOUCH, 0);
88195f75e91SYunkang Tang 
88295f75e91SYunkang Tang 	if (z > 0) {
88395f75e91SYunkang Tang 		input_report_abs(dev, ABS_X, x);
88495f75e91SYunkang Tang 		input_report_abs(dev, ABS_Y, y);
88595f75e91SYunkang Tang 	}
88695f75e91SYunkang Tang 
88795f75e91SYunkang Tang 	input_report_abs(dev, ABS_PRESSURE, z);
88895f75e91SYunkang Tang 	input_report_key(dev, BTN_TOOL_FINGER, z > 0);
88995f75e91SYunkang Tang 
89095f75e91SYunkang Tang 	/* v6 touchpad does not have middle button */
89195f75e91SYunkang Tang 	input_report_key(dev, BTN_LEFT, left);
89295f75e91SYunkang Tang 	input_report_key(dev, BTN_RIGHT, right);
89395f75e91SYunkang Tang 
89495f75e91SYunkang Tang 	input_sync(dev);
89595f75e91SYunkang Tang }
89695f75e91SYunkang Tang 
89725bded7cSSeth Forshee static void alps_process_packet_v4(struct psmouse *psmouse)
89825bded7cSSeth Forshee {
8993b7e09faSGeorge Pantalos 	struct alps_data *priv = psmouse->private;
90025bded7cSSeth Forshee 	unsigned char *packet = psmouse->packet;
90102d04254SHans de Goede 	struct alps_fields *f = &priv->f;
90268c21870SHans de Goede 	int offset;
9033b7e09faSGeorge Pantalos 
9043b7e09faSGeorge Pantalos 	/*
9053b7e09faSGeorge Pantalos 	 * v4 has a 6-byte encoding for bitmap data, but this data is
9063b7e09faSGeorge Pantalos 	 * broken up between 3 normal packets. Use priv->multi_packet to
9073b7e09faSGeorge Pantalos 	 * track our position in the bitmap packet.
9083b7e09faSGeorge Pantalos 	 */
9093b7e09faSGeorge Pantalos 	if (packet[6] & 0x40) {
9103b7e09faSGeorge Pantalos 		/* sync, reset position */
9113b7e09faSGeorge Pantalos 		priv->multi_packet = 0;
9123b7e09faSGeorge Pantalos 	}
9133b7e09faSGeorge Pantalos 
9143b7e09faSGeorge Pantalos 	if (WARN_ON_ONCE(priv->multi_packet > 2))
9153b7e09faSGeorge Pantalos 		return;
9163b7e09faSGeorge Pantalos 
9173b7e09faSGeorge Pantalos 	offset = 2 * priv->multi_packet;
9183b7e09faSGeorge Pantalos 	priv->multi_data[offset] = packet[6];
9193b7e09faSGeorge Pantalos 	priv->multi_data[offset + 1] = packet[7];
9203b7e09faSGeorge Pantalos 
92144b77f38SHans de Goede 	f->left = !!(packet[4] & 0x01);
92244b77f38SHans de Goede 	f->right = !!(packet[4] & 0x02);
92344b77f38SHans de Goede 
92444b77f38SHans de Goede 	f->st.x = ((packet[1] & 0x7f) << 4) | ((packet[3] & 0x30) >> 2) |
92544b77f38SHans de Goede 		  ((packet[0] & 0x30) >> 4);
92644b77f38SHans de Goede 	f->st.y = ((packet[2] & 0x7f) << 4) | (packet[3] & 0x0f);
92744b77f38SHans de Goede 	f->pressure = packet[5] & 0x7f;
92844b77f38SHans de Goede 
9293b7e09faSGeorge Pantalos 	if (++priv->multi_packet > 2) {
9303b7e09faSGeorge Pantalos 		priv->multi_packet = 0;
9313b7e09faSGeorge Pantalos 
93202d04254SHans de Goede 		f->x_map = ((priv->multi_data[2] & 0x1f) << 10) |
9333b7e09faSGeorge Pantalos 			   ((priv->multi_data[3] & 0x60) << 3) |
9343b7e09faSGeorge Pantalos 			   ((priv->multi_data[0] & 0x3f) << 2) |
9353b7e09faSGeorge Pantalos 			   ((priv->multi_data[1] & 0x60) >> 5);
93602d04254SHans de Goede 		f->y_map = ((priv->multi_data[5] & 0x01) << 10) |
9373b7e09faSGeorge Pantalos 			   ((priv->multi_data[3] & 0x1f) << 5) |
9383b7e09faSGeorge Pantalos 			    (priv->multi_data[1] & 0x1f);
9393b7e09faSGeorge Pantalos 
94002d04254SHans de Goede 		f->fingers = alps_process_bitmap(priv, f);
9413b7e09faSGeorge Pantalos 	}
94225bded7cSSeth Forshee 
94368c21870SHans de Goede 	alps_report_semi_mt_data(psmouse, f->fingers);
94425bded7cSSeth Forshee }
94525bded7cSSeth Forshee 
9463808843cSYunkang Tang static bool alps_is_valid_package_v7(struct psmouse *psmouse)
9473808843cSYunkang Tang {
9483808843cSYunkang Tang 	switch (psmouse->pktcnt) {
9493808843cSYunkang Tang 	case 3:
9503808843cSYunkang Tang 		return (psmouse->packet[2] & 0x40) == 0x40;
9513808843cSYunkang Tang 	case 4:
9523808843cSYunkang Tang 		return (psmouse->packet[3] & 0x48) == 0x48;
9533808843cSYunkang Tang 	case 6:
9543808843cSYunkang Tang 		return (psmouse->packet[5] & 0x40) == 0x00;
9553808843cSYunkang Tang 	}
9563808843cSYunkang Tang 	return true;
9573808843cSYunkang Tang }
9583808843cSYunkang Tang 
9593808843cSYunkang Tang static unsigned char alps_get_packet_id_v7(char *byte)
9603808843cSYunkang Tang {
9613808843cSYunkang Tang 	unsigned char packet_id;
9623808843cSYunkang Tang 
9633808843cSYunkang Tang 	if (byte[4] & 0x40)
9643808843cSYunkang Tang 		packet_id = V7_PACKET_ID_TWO;
9653808843cSYunkang Tang 	else if (byte[4] & 0x01)
9663808843cSYunkang Tang 		packet_id = V7_PACKET_ID_MULTI;
9673808843cSYunkang Tang 	else if ((byte[0] & 0x10) && !(byte[4] & 0x43))
9683808843cSYunkang Tang 		packet_id = V7_PACKET_ID_NEW;
9693808843cSYunkang Tang 	else if (byte[1] == 0x00 && byte[4] == 0x00)
9703808843cSYunkang Tang 		packet_id = V7_PACKET_ID_IDLE;
9713808843cSYunkang Tang 	else
9723808843cSYunkang Tang 		packet_id = V7_PACKET_ID_UNKNOWN;
9733808843cSYunkang Tang 
9743808843cSYunkang Tang 	return packet_id;
9753808843cSYunkang Tang }
9763808843cSYunkang Tang 
9773808843cSYunkang Tang static void alps_get_finger_coordinate_v7(struct input_mt_pos *mt,
9783808843cSYunkang Tang 					  unsigned char *pkt,
9793808843cSYunkang Tang 					  unsigned char pkt_id)
9803808843cSYunkang Tang {
9813808843cSYunkang Tang 	mt[0].x = ((pkt[2] & 0x80) << 4);
9823808843cSYunkang Tang 	mt[0].x |= ((pkt[2] & 0x3F) << 5);
9833808843cSYunkang Tang 	mt[0].x |= ((pkt[3] & 0x30) >> 1);
9843808843cSYunkang Tang 	mt[0].x |= (pkt[3] & 0x07);
9853808843cSYunkang Tang 	mt[0].y = (pkt[1] << 3) | (pkt[0] & 0x07);
9863808843cSYunkang Tang 
9873808843cSYunkang Tang 	mt[1].x = ((pkt[3] & 0x80) << 4);
9883808843cSYunkang Tang 	mt[1].x |= ((pkt[4] & 0x80) << 3);
9893808843cSYunkang Tang 	mt[1].x |= ((pkt[4] & 0x3F) << 4);
9903808843cSYunkang Tang 	mt[1].y = ((pkt[5] & 0x80) << 3);
9913808843cSYunkang Tang 	mt[1].y |= ((pkt[5] & 0x3F) << 4);
9923808843cSYunkang Tang 
9933808843cSYunkang Tang 	switch (pkt_id) {
9943808843cSYunkang Tang 	case V7_PACKET_ID_TWO:
9953808843cSYunkang Tang 		mt[1].x &= ~0x000F;
9963808843cSYunkang Tang 		mt[1].y |= 0x000F;
99772eceab7SHans de Goede 		/* Detect false-postive touches where x & y report max value */
99872eceab7SHans de Goede 		if (mt[1].y == 0x7ff && mt[1].x == 0xff0) {
99972eceab7SHans de Goede 			mt[1].x = 0;
100072eceab7SHans de Goede 			/* y gets set to 0 at the end of this function */
100172eceab7SHans de Goede 		}
10023808843cSYunkang Tang 		break;
10033808843cSYunkang Tang 
10043808843cSYunkang Tang 	case V7_PACKET_ID_MULTI:
10053808843cSYunkang Tang 		mt[1].x &= ~0x003F;
10063808843cSYunkang Tang 		mt[1].y &= ~0x0020;
10073808843cSYunkang Tang 		mt[1].y |= ((pkt[4] & 0x02) << 4);
10083808843cSYunkang Tang 		mt[1].y |= 0x001F;
10093808843cSYunkang Tang 		break;
10103808843cSYunkang Tang 
10113808843cSYunkang Tang 	case V7_PACKET_ID_NEW:
10123808843cSYunkang Tang 		mt[1].x &= ~0x003F;
10133808843cSYunkang Tang 		mt[1].x |= (pkt[0] & 0x20);
10143808843cSYunkang Tang 		mt[1].y |= 0x000F;
10153808843cSYunkang Tang 		break;
10163808843cSYunkang Tang 	}
10173808843cSYunkang Tang 
10183808843cSYunkang Tang 	mt[0].y = 0x7FF - mt[0].y;
10193808843cSYunkang Tang 	mt[1].y = 0x7FF - mt[1].y;
10203808843cSYunkang Tang }
10213808843cSYunkang Tang 
10223808843cSYunkang Tang static int alps_get_mt_count(struct input_mt_pos *mt)
10233808843cSYunkang Tang {
10247091c443SHans de Goede 	int i, fingers = 0;
10253808843cSYunkang Tang 
10267091c443SHans de Goede 	for (i = 0; i < MAX_TOUCHES; i++) {
10277091c443SHans de Goede 		if (mt[i].x != 0 || mt[i].y != 0)
10287091c443SHans de Goede 			fingers++;
10297091c443SHans de Goede 	}
10303808843cSYunkang Tang 
10317091c443SHans de Goede 	return fingers;
10323808843cSYunkang Tang }
10333808843cSYunkang Tang 
10343808843cSYunkang Tang static int alps_decode_packet_v7(struct alps_fields *f,
10353808843cSYunkang Tang 				  unsigned char *p,
10363808843cSYunkang Tang 				  struct psmouse *psmouse)
10373808843cSYunkang Tang {
1038d27eb793SHans de Goede 	struct alps_data *priv = psmouse->private;
10393808843cSYunkang Tang 	unsigned char pkt_id;
10403808843cSYunkang Tang 
10413808843cSYunkang Tang 	pkt_id = alps_get_packet_id_v7(p);
10423808843cSYunkang Tang 	if (pkt_id == V7_PACKET_ID_IDLE)
10433808843cSYunkang Tang 		return 0;
10443808843cSYunkang Tang 	if (pkt_id == V7_PACKET_ID_UNKNOWN)
10453808843cSYunkang Tang 		return -1;
10468b238115SHans de Goede 	/*
10478b238115SHans de Goede 	 * NEW packets are send to indicate a discontinuity in the finger
10488b238115SHans de Goede 	 * coordinate reporting. Specifically a finger may have moved from
10498b238115SHans de Goede 	 * slot 0 to 1 or vice versa. INPUT_MT_TRACK takes care of this for
10508b238115SHans de Goede 	 * us.
10518b238115SHans de Goede 	 *
10528b238115SHans de Goede 	 * NEW packets have 3 problems:
10538b238115SHans de Goede 	 * 1) They do not contain middle / right button info (on non clickpads)
10548b238115SHans de Goede 	 *    this can be worked around by preserving the old button state
10558b238115SHans de Goede 	 * 2) They do not contain an accurate fingercount, and they are
10568b238115SHans de Goede 	 *    typically send when the number of fingers changes. We cannot use
10578b238115SHans de Goede 	 *    the old finger count as that may mismatch with the amount of
10588b238115SHans de Goede 	 *    touch coordinates we've available in the NEW packet
10598b238115SHans de Goede 	 * 3) Their x data for the second touch is inaccurate leading to
10608b238115SHans de Goede 	 *    a possible jump of the x coordinate by 16 units when the first
10618b238115SHans de Goede 	 *    non NEW packet comes in
10628b238115SHans de Goede 	 * Since problems 2 & 3 cannot be worked around, just ignore them.
10638b238115SHans de Goede 	 */
10648b238115SHans de Goede 	if (pkt_id == V7_PACKET_ID_NEW)
10658b238115SHans de Goede 		return 1;
10663808843cSYunkang Tang 
10673808843cSYunkang Tang 	alps_get_finger_coordinate_v7(f->mt, p, pkt_id);
10683808843cSYunkang Tang 
10693808843cSYunkang Tang 	if (pkt_id == V7_PACKET_ID_TWO)
10703808843cSYunkang Tang 		f->fingers = alps_get_mt_count(f->mt);
10718b238115SHans de Goede 	else /* pkt_id == V7_PACKET_ID_MULTI */
10723808843cSYunkang Tang 		f->fingers = 3 + (p[5] & 0x03);
10733808843cSYunkang Tang 
1074d27eb793SHans de Goede 	f->left = (p[0] & 0x80) >> 7;
1075d27eb793SHans de Goede 	if (priv->flags & ALPS_BUTTONPAD) {
1076d27eb793SHans de Goede 		if (p[0] & 0x20)
1077d27eb793SHans de Goede 			f->fingers++;
1078d27eb793SHans de Goede 		if (p[0] & 0x10)
1079d27eb793SHans de Goede 			f->fingers++;
1080d27eb793SHans de Goede 	} else {
1081d27eb793SHans de Goede 		f->right = (p[0] & 0x20) >> 5;
1082d27eb793SHans de Goede 		f->middle = (p[0] & 0x10) >> 4;
1083d27eb793SHans de Goede 	}
1084d27eb793SHans de Goede 
10857091c443SHans de Goede 	/* Sometimes a single touch is reported in mt[1] rather then mt[0] */
10867091c443SHans de Goede 	if (f->fingers == 1 && f->mt[0].x == 0 && f->mt[0].y == 0) {
10877091c443SHans de Goede 		f->mt[0].x = f->mt[1].x;
10887091c443SHans de Goede 		f->mt[0].y = f->mt[1].y;
10897091c443SHans de Goede 		f->mt[1].x = 0;
10907091c443SHans de Goede 		f->mt[1].y = 0;
10917091c443SHans de Goede 	}
10927091c443SHans de Goede 
10933808843cSYunkang Tang 	return 0;
10943808843cSYunkang Tang }
10953808843cSYunkang Tang 
10963808843cSYunkang Tang static void alps_process_trackstick_packet_v7(struct psmouse *psmouse)
10973808843cSYunkang Tang {
10983808843cSYunkang Tang 	struct alps_data *priv = psmouse->private;
10993808843cSYunkang Tang 	unsigned char *packet = psmouse->packet;
11003808843cSYunkang Tang 	struct input_dev *dev2 = priv->dev2;
11013808843cSYunkang Tang 	int x, y, z, left, right, middle;
11023808843cSYunkang Tang 
110334412ba2SPali Rohár 	/* It should be a DualPoint when received trackstick packet */
110434412ba2SPali Rohár 	if (!(priv->flags & ALPS_DUALPOINT)) {
110534412ba2SPali Rohár 		psmouse_warn(psmouse,
110634412ba2SPali Rohár 			     "Rejected trackstick packet from non DualPoint device");
110734412ba2SPali Rohár 		return;
110834412ba2SPali Rohár 	}
110934412ba2SPali Rohár 
11103808843cSYunkang Tang 	x = ((packet[2] & 0xbf)) | ((packet[3] & 0x10) << 2);
11113808843cSYunkang Tang 	y = (packet[3] & 0x07) | (packet[4] & 0xb8) |
11123808843cSYunkang Tang 	    ((packet[3] & 0x20) << 1);
11133808843cSYunkang Tang 	z = (packet[5] & 0x3f) | ((packet[3] & 0x80) >> 1);
11143808843cSYunkang Tang 
11153808843cSYunkang Tang 	left = (packet[1] & 0x01);
11163808843cSYunkang Tang 	right = (packet[1] & 0x02) >> 1;
11173808843cSYunkang Tang 	middle = (packet[1] & 0x04) >> 2;
11183808843cSYunkang Tang 
1119088df2ccSHans de Goede 	input_report_rel(dev2, REL_X, (char)x);
1120088df2ccSHans de Goede 	input_report_rel(dev2, REL_Y, -((char)y));
11213808843cSYunkang Tang 
11223808843cSYunkang Tang 	input_report_key(dev2, BTN_LEFT, left);
11233808843cSYunkang Tang 	input_report_key(dev2, BTN_RIGHT, right);
11243808843cSYunkang Tang 	input_report_key(dev2, BTN_MIDDLE, middle);
11253808843cSYunkang Tang 
11263808843cSYunkang Tang 	input_sync(dev2);
11273808843cSYunkang Tang }
11283808843cSYunkang Tang 
11293808843cSYunkang Tang static void alps_process_touchpad_packet_v7(struct psmouse *psmouse)
11303808843cSYunkang Tang {
11313808843cSYunkang Tang 	struct alps_data *priv = psmouse->private;
11323808843cSYunkang Tang 	struct input_dev *dev = psmouse->dev;
11333808843cSYunkang Tang 	struct alps_fields *f = &priv->f;
11343808843cSYunkang Tang 
11353808843cSYunkang Tang 	memset(f, 0, sizeof(*f));
11363808843cSYunkang Tang 
11373808843cSYunkang Tang 	if (priv->decode_fields(f, psmouse->packet, psmouse))
11383808843cSYunkang Tang 		return;
11393808843cSYunkang Tang 
11403808843cSYunkang Tang 	alps_report_mt_data(psmouse, alps_get_mt_count(f->mt));
11413808843cSYunkang Tang 
11423808843cSYunkang Tang 	input_mt_report_finger_count(dev, f->fingers);
11433808843cSYunkang Tang 
11443808843cSYunkang Tang 	input_report_key(dev, BTN_LEFT, f->left);
11453808843cSYunkang Tang 	input_report_key(dev, BTN_RIGHT, f->right);
11463808843cSYunkang Tang 	input_report_key(dev, BTN_MIDDLE, f->middle);
11473808843cSYunkang Tang 
11483808843cSYunkang Tang 	input_sync(dev);
11493808843cSYunkang Tang }
11503808843cSYunkang Tang 
11513808843cSYunkang Tang static void alps_process_packet_v7(struct psmouse *psmouse)
11523808843cSYunkang Tang {
11533808843cSYunkang Tang 	unsigned char *packet = psmouse->packet;
11543808843cSYunkang Tang 
11553808843cSYunkang Tang 	if (packet[0] == 0x48 && (packet[4] & 0x47) == 0x06)
11563808843cSYunkang Tang 		alps_process_trackstick_packet_v7(psmouse);
11573808843cSYunkang Tang 	else
11583808843cSYunkang Tang 		alps_process_touchpad_packet_v7(psmouse);
11593808843cSYunkang Tang }
11603808843cSYunkang Tang 
116123fce365SPaul Donohue static enum SS4_PACKET_ID alps_get_pkt_id_ss4_v2(unsigned char *byte)
11623db5b9f7SMasaki Ota {
116323fce365SPaul Donohue 	enum SS4_PACKET_ID pkt_id = SS4_PACKET_ID_IDLE;
11643db5b9f7SMasaki Ota 
11654777ac22SBen Gamari 	switch (byte[3] & 0x30) {
11664777ac22SBen Gamari 	case 0x00:
116723fce365SPaul Donohue 		if (SS4_IS_IDLE_V2(byte)) {
11683db5b9f7SMasaki Ota 			pkt_id = SS4_PACKET_ID_IDLE;
11693db5b9f7SMasaki Ota 		} else {
11704777ac22SBen Gamari 			pkt_id = SS4_PACKET_ID_ONE;
11714777ac22SBen Gamari 		}
11724777ac22SBen Gamari 		break;
11734777ac22SBen Gamari 	case 0x10:
11744777ac22SBen Gamari 		/* two-finger finger positions */
11754777ac22SBen Gamari 		pkt_id = SS4_PACKET_ID_TWO;
11764777ac22SBen Gamari 		break;
11774777ac22SBen Gamari 	case 0x20:
11784777ac22SBen Gamari 		/* stick pointer */
11794777ac22SBen Gamari 		pkt_id = SS4_PACKET_ID_STICK;
11804777ac22SBen Gamari 		break;
11814777ac22SBen Gamari 	case 0x30:
11824777ac22SBen Gamari 		/* third and fourth finger positions */
11833db5b9f7SMasaki Ota 		pkt_id = SS4_PACKET_ID_MULTI;
11844777ac22SBen Gamari 		break;
11853db5b9f7SMasaki Ota 	}
11863db5b9f7SMasaki Ota 
11873db5b9f7SMasaki Ota 	return pkt_id;
11883db5b9f7SMasaki Ota }
11893db5b9f7SMasaki Ota 
11903db5b9f7SMasaki Ota static int alps_decode_ss4_v2(struct alps_fields *f,
11913db5b9f7SMasaki Ota 			      unsigned char *p, struct psmouse *psmouse)
11923db5b9f7SMasaki Ota {
11933db5b9f7SMasaki Ota 	struct alps_data *priv = psmouse->private;
119423fce365SPaul Donohue 	enum SS4_PACKET_ID pkt_id;
11953db5b9f7SMasaki Ota 	unsigned int no_data_x, no_data_y;
11963db5b9f7SMasaki Ota 
11973db5b9f7SMasaki Ota 	pkt_id = alps_get_pkt_id_ss4_v2(p);
11983db5b9f7SMasaki Ota 
11993db5b9f7SMasaki Ota 	/* Current packet is 1Finger coordinate packet */
12003db5b9f7SMasaki Ota 	switch (pkt_id) {
12013db5b9f7SMasaki Ota 	case SS4_PACKET_ID_ONE:
12023db5b9f7SMasaki Ota 		f->mt[0].x = SS4_1F_X_V2(p);
12033db5b9f7SMasaki Ota 		f->mt[0].y = SS4_1F_Y_V2(p);
12043db5b9f7SMasaki Ota 		f->pressure = ((SS4_1F_Z_V2(p)) * 2) & 0x7f;
1205a8317763SBen Gamari 		/*
1206a8317763SBen Gamari 		 * When a button is held the device will give us events
1207a8317763SBen Gamari 		 * with x, y, and pressure of 0. This causes annoying jumps
1208a8317763SBen Gamari 		 * if a touch is released while the button is held.
1209a8317763SBen Gamari 		 * Handle this by claiming zero contacts.
1210a8317763SBen Gamari 		 */
1211a8317763SBen Gamari 		f->fingers = f->pressure > 0 ? 1 : 0;
12123db5b9f7SMasaki Ota 		f->first_mp = 0;
12133db5b9f7SMasaki Ota 		f->is_mp = 0;
12143db5b9f7SMasaki Ota 		break;
12153db5b9f7SMasaki Ota 
12163db5b9f7SMasaki Ota 	case SS4_PACKET_ID_TWO:
12173db5b9f7SMasaki Ota 		if (priv->flags & ALPS_BUTTONPAD) {
12184a646580SMasaki Ota 			if (IS_SS4PLUS_DEV(priv->dev_id)) {
12194a646580SMasaki Ota 				f->mt[0].x = SS4_PLUS_BTL_MF_X_V2(p, 0);
12204a646580SMasaki Ota 				f->mt[1].x = SS4_PLUS_BTL_MF_X_V2(p, 1);
12214a646580SMasaki Ota 			} else {
12223db5b9f7SMasaki Ota 				f->mt[0].x = SS4_BTL_MF_X_V2(p, 0);
12233db5b9f7SMasaki Ota 				f->mt[1].x = SS4_BTL_MF_X_V2(p, 1);
12244a646580SMasaki Ota 			}
12254a646580SMasaki Ota 			f->mt[0].y = SS4_BTL_MF_Y_V2(p, 0);
12263db5b9f7SMasaki Ota 			f->mt[1].y = SS4_BTL_MF_Y_V2(p, 1);
12273db5b9f7SMasaki Ota 		} else {
12284a646580SMasaki Ota 			if (IS_SS4PLUS_DEV(priv->dev_id)) {
12294a646580SMasaki Ota 				f->mt[0].x = SS4_PLUS_STD_MF_X_V2(p, 0);
12304a646580SMasaki Ota 				f->mt[1].x = SS4_PLUS_STD_MF_X_V2(p, 1);
12314a646580SMasaki Ota 			} else {
12323db5b9f7SMasaki Ota 				f->mt[0].x = SS4_STD_MF_X_V2(p, 0);
12333db5b9f7SMasaki Ota 				f->mt[1].x = SS4_STD_MF_X_V2(p, 1);
12344a646580SMasaki Ota 			}
12354a646580SMasaki Ota 			f->mt[0].y = SS4_STD_MF_Y_V2(p, 0);
12363db5b9f7SMasaki Ota 			f->mt[1].y = SS4_STD_MF_Y_V2(p, 1);
12373db5b9f7SMasaki Ota 		}
12383db5b9f7SMasaki Ota 		f->pressure = SS4_MF_Z_V2(p, 0) ? 0x30 : 0;
12393db5b9f7SMasaki Ota 
12403db5b9f7SMasaki Ota 		if (SS4_IS_MF_CONTINUE(p)) {
12413db5b9f7SMasaki Ota 			f->first_mp = 1;
12423db5b9f7SMasaki Ota 		} else {
12433db5b9f7SMasaki Ota 			f->fingers = 2;
12443db5b9f7SMasaki Ota 			f->first_mp = 0;
12453db5b9f7SMasaki Ota 		}
12463db5b9f7SMasaki Ota 		f->is_mp = 0;
12473db5b9f7SMasaki Ota 
12483db5b9f7SMasaki Ota 		break;
12493db5b9f7SMasaki Ota 
12503db5b9f7SMasaki Ota 	case SS4_PACKET_ID_MULTI:
12513db5b9f7SMasaki Ota 		if (priv->flags & ALPS_BUTTONPAD) {
12524a646580SMasaki Ota 			if (IS_SS4PLUS_DEV(priv->dev_id)) {
12534a646580SMasaki Ota 				f->mt[0].x = SS4_PLUS_BTL_MF_X_V2(p, 0);
12544a646580SMasaki Ota 				f->mt[1].x = SS4_PLUS_BTL_MF_X_V2(p, 1);
12554a646580SMasaki Ota 			} else {
12563db5b9f7SMasaki Ota 				f->mt[2].x = SS4_BTL_MF_X_V2(p, 0);
12573db5b9f7SMasaki Ota 				f->mt[3].x = SS4_BTL_MF_X_V2(p, 1);
12584a646580SMasaki Ota 			}
12594a646580SMasaki Ota 
12604a646580SMasaki Ota 			f->mt[2].y = SS4_BTL_MF_Y_V2(p, 0);
12613db5b9f7SMasaki Ota 			f->mt[3].y = SS4_BTL_MF_Y_V2(p, 1);
12623db5b9f7SMasaki Ota 			no_data_x = SS4_MFPACKET_NO_AX_BL;
12633db5b9f7SMasaki Ota 			no_data_y = SS4_MFPACKET_NO_AY_BL;
12643db5b9f7SMasaki Ota 		} else {
12654a646580SMasaki Ota 			if (IS_SS4PLUS_DEV(priv->dev_id)) {
12664a646580SMasaki Ota 				f->mt[0].x = SS4_PLUS_STD_MF_X_V2(p, 0);
12674a646580SMasaki Ota 				f->mt[1].x = SS4_PLUS_STD_MF_X_V2(p, 1);
12684a646580SMasaki Ota 			} else {
12694a646580SMasaki Ota 				f->mt[0].x = SS4_STD_MF_X_V2(p, 0);
12704a646580SMasaki Ota 				f->mt[1].x = SS4_STD_MF_X_V2(p, 1);
12714a646580SMasaki Ota 			}
12723db5b9f7SMasaki Ota 			f->mt[2].y = SS4_STD_MF_Y_V2(p, 0);
12733db5b9f7SMasaki Ota 			f->mt[3].y = SS4_STD_MF_Y_V2(p, 1);
12743db5b9f7SMasaki Ota 			no_data_x = SS4_MFPACKET_NO_AX;
12753db5b9f7SMasaki Ota 			no_data_y = SS4_MFPACKET_NO_AY;
12763db5b9f7SMasaki Ota 		}
12773db5b9f7SMasaki Ota 
12783db5b9f7SMasaki Ota 		f->first_mp = 0;
12793db5b9f7SMasaki Ota 		f->is_mp = 1;
12803db5b9f7SMasaki Ota 
12813db5b9f7SMasaki Ota 		if (SS4_IS_5F_DETECTED(p)) {
12823db5b9f7SMasaki Ota 			f->fingers = 5;
12833db5b9f7SMasaki Ota 		} else if (f->mt[3].x == no_data_x &&
12843db5b9f7SMasaki Ota 			     f->mt[3].y == no_data_y) {
12853db5b9f7SMasaki Ota 			f->mt[3].x = 0;
12863db5b9f7SMasaki Ota 			f->mt[3].y = 0;
12873db5b9f7SMasaki Ota 			f->fingers = 3;
12883db5b9f7SMasaki Ota 		} else {
12893db5b9f7SMasaki Ota 			f->fingers = 4;
12903db5b9f7SMasaki Ota 		}
12913db5b9f7SMasaki Ota 		break;
12923db5b9f7SMasaki Ota 
12934777ac22SBen Gamari 	case SS4_PACKET_ID_STICK:
12947229c58cSPaul Donohue 		/*
12957229c58cSPaul Donohue 		 * x, y, and pressure are decoded in
12967229c58cSPaul Donohue 		 * alps_process_packet_ss4_v2()
12977229c58cSPaul Donohue 		 */
12987229c58cSPaul Donohue 		f->first_mp = 0;
12997229c58cSPaul Donohue 		f->is_mp = 0;
13004777ac22SBen Gamari 		break;
13014777ac22SBen Gamari 
13023db5b9f7SMasaki Ota 	case SS4_PACKET_ID_IDLE:
13033db5b9f7SMasaki Ota 	default:
13043db5b9f7SMasaki Ota 		memset(f, 0, sizeof(struct alps_fields));
13053db5b9f7SMasaki Ota 		break;
13063db5b9f7SMasaki Ota 	}
13073db5b9f7SMasaki Ota 
13084777ac22SBen Gamari 	/* handle buttons */
13094777ac22SBen Gamari 	if (pkt_id == SS4_PACKET_ID_STICK) {
13104777ac22SBen Gamari 		f->ts_left = !!(SS4_BTN_V2(p) & 0x01);
13114777ac22SBen Gamari 		f->ts_right = !!(SS4_BTN_V2(p) & 0x02);
13124777ac22SBen Gamari 		f->ts_middle = !!(SS4_BTN_V2(p) & 0x04);
13134777ac22SBen Gamari 	} else {
13143db5b9f7SMasaki Ota 		f->left = !!(SS4_BTN_V2(p) & 0x01);
13153db5b9f7SMasaki Ota 		if (!(priv->flags & ALPS_BUTTONPAD)) {
13163db5b9f7SMasaki Ota 			f->right = !!(SS4_BTN_V2(p) & 0x02);
13173db5b9f7SMasaki Ota 			f->middle = !!(SS4_BTN_V2(p) & 0x04);
13183db5b9f7SMasaki Ota 		}
13194777ac22SBen Gamari 	}
13203db5b9f7SMasaki Ota 
13213db5b9f7SMasaki Ota 	return 0;
13223db5b9f7SMasaki Ota }
13233db5b9f7SMasaki Ota 
13243db5b9f7SMasaki Ota static void alps_process_packet_ss4_v2(struct psmouse *psmouse)
13253db5b9f7SMasaki Ota {
13263db5b9f7SMasaki Ota 	struct alps_data *priv = psmouse->private;
13273db5b9f7SMasaki Ota 	unsigned char *packet = psmouse->packet;
13283db5b9f7SMasaki Ota 	struct input_dev *dev = psmouse->dev;
13294777ac22SBen Gamari 	struct input_dev *dev2 = priv->dev2;
13303db5b9f7SMasaki Ota 	struct alps_fields *f = &priv->f;
13313db5b9f7SMasaki Ota 
13323db5b9f7SMasaki Ota 	memset(f, 0, sizeof(struct alps_fields));
13333db5b9f7SMasaki Ota 	priv->decode_fields(f, packet, psmouse);
13343db5b9f7SMasaki Ota 	if (priv->multi_packet) {
13353db5b9f7SMasaki Ota 		/*
13363db5b9f7SMasaki Ota 		 * Sometimes the first packet will indicate a multi-packet
13373db5b9f7SMasaki Ota 		 * sequence, but sometimes the next multi-packet would not
13383db5b9f7SMasaki Ota 		 * come. Check for this, and when it happens process the
13393db5b9f7SMasaki Ota 		 * position packet as usual.
13403db5b9f7SMasaki Ota 		 */
13413db5b9f7SMasaki Ota 		if (f->is_mp) {
13423db5b9f7SMasaki Ota 			/* Now process the 1st packet */
13433db5b9f7SMasaki Ota 			priv->decode_fields(f, priv->multi_data, psmouse);
13443db5b9f7SMasaki Ota 		} else {
13453db5b9f7SMasaki Ota 			priv->multi_packet = 0;
13463db5b9f7SMasaki Ota 		}
13473db5b9f7SMasaki Ota 	}
13483db5b9f7SMasaki Ota 
13493db5b9f7SMasaki Ota 	/*
13503db5b9f7SMasaki Ota 	 * "f.is_mp" would always be '0' after merging the 1st and 2nd packet.
13513db5b9f7SMasaki Ota 	 * When it is set, it means 2nd packet comes without 1st packet come.
13523db5b9f7SMasaki Ota 	 */
13533db5b9f7SMasaki Ota 	if (f->is_mp)
13543db5b9f7SMasaki Ota 		return;
13553db5b9f7SMasaki Ota 
13563db5b9f7SMasaki Ota 	/* Save the first packet */
13573db5b9f7SMasaki Ota 	if (!priv->multi_packet && f->first_mp) {
13583db5b9f7SMasaki Ota 		priv->multi_packet = 1;
13593db5b9f7SMasaki Ota 		memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
13603db5b9f7SMasaki Ota 		return;
13613db5b9f7SMasaki Ota 	}
13623db5b9f7SMasaki Ota 
13633db5b9f7SMasaki Ota 	priv->multi_packet = 0;
13643db5b9f7SMasaki Ota 
1365864db929SPaul Donohue 	/* Report trackstick */
1366864db929SPaul Donohue 	if (alps_get_pkt_id_ss4_v2(packet) == SS4_PACKET_ID_STICK) {
13677229c58cSPaul Donohue 		if (!(priv->flags & ALPS_DUALPOINT)) {
13687229c58cSPaul Donohue 			psmouse_warn(psmouse,
13697229c58cSPaul Donohue 				     "Rejected trackstick packet from non DualPoint device");
13707229c58cSPaul Donohue 			return;
13717229c58cSPaul Donohue 		}
13727229c58cSPaul Donohue 
137323fce365SPaul Donohue 		input_report_rel(dev2, REL_X, SS4_TS_X_V2(packet));
137423fce365SPaul Donohue 		input_report_rel(dev2, REL_Y, SS4_TS_Y_V2(packet));
137523fce365SPaul Donohue 		input_report_abs(dev2, ABS_PRESSURE, SS4_TS_Z_V2(packet));
13767229c58cSPaul Donohue 
1377864db929SPaul Donohue 		input_report_key(dev2, BTN_LEFT, f->ts_left);
1378864db929SPaul Donohue 		input_report_key(dev2, BTN_RIGHT, f->ts_right);
1379864db929SPaul Donohue 		input_report_key(dev2, BTN_MIDDLE, f->ts_middle);
13807229c58cSPaul Donohue 
1381864db929SPaul Donohue 		input_sync(dev2);
1382864db929SPaul Donohue 		return;
1383864db929SPaul Donohue 	}
1384864db929SPaul Donohue 
1385864db929SPaul Donohue 	/* Report touchpad */
13863db5b9f7SMasaki Ota 	alps_report_mt_data(psmouse, (f->fingers <= 4) ? f->fingers : 4);
13873db5b9f7SMasaki Ota 
13883db5b9f7SMasaki Ota 	input_mt_report_finger_count(dev, f->fingers);
13893db5b9f7SMasaki Ota 
13903db5b9f7SMasaki Ota 	input_report_key(dev, BTN_LEFT, f->left);
13913db5b9f7SMasaki Ota 	input_report_key(dev, BTN_RIGHT, f->right);
13923db5b9f7SMasaki Ota 	input_report_key(dev, BTN_MIDDLE, f->middle);
13933db5b9f7SMasaki Ota 
13943db5b9f7SMasaki Ota 	input_report_abs(dev, ABS_PRESSURE, f->pressure);
13953db5b9f7SMasaki Ota 	input_sync(dev);
13963db5b9f7SMasaki Ota }
13973db5b9f7SMasaki Ota 
13983db5b9f7SMasaki Ota static bool alps_is_valid_package_ss4_v2(struct psmouse *psmouse)
13993db5b9f7SMasaki Ota {
14003db5b9f7SMasaki Ota 	if (psmouse->pktcnt == 4 && ((psmouse->packet[3] & 0x08) != 0x08))
14013db5b9f7SMasaki Ota 		return false;
14023db5b9f7SMasaki Ota 	if (psmouse->pktcnt == 6 && ((psmouse->packet[5] & 0x10) != 0x0))
14033db5b9f7SMasaki Ota 		return false;
14043db5b9f7SMasaki Ota 	return true;
14053db5b9f7SMasaki Ota }
14063db5b9f7SMasaki Ota 
140704aae283SPali Rohár static DEFINE_MUTEX(alps_mutex);
140804aae283SPali Rohár 
140904aae283SPali Rohár static void alps_register_bare_ps2_mouse(struct work_struct *work)
141004aae283SPali Rohár {
141104aae283SPali Rohár 	struct alps_data *priv =
141204aae283SPali Rohár 		container_of(work, struct alps_data, dev3_register_work.work);
141304aae283SPali Rohár 	struct psmouse *psmouse = priv->psmouse;
141404aae283SPali Rohár 	struct input_dev *dev3;
141504aae283SPali Rohár 	int error = 0;
141604aae283SPali Rohár 
141704aae283SPali Rohár 	mutex_lock(&alps_mutex);
141804aae283SPali Rohár 
141904aae283SPali Rohár 	if (priv->dev3)
142004aae283SPali Rohár 		goto out;
142104aae283SPali Rohár 
142204aae283SPali Rohár 	dev3 = input_allocate_device();
142304aae283SPali Rohár 	if (!dev3) {
142404aae283SPali Rohár 		psmouse_err(psmouse, "failed to allocate secondary device\n");
142504aae283SPali Rohár 		error = -ENOMEM;
142604aae283SPali Rohár 		goto out;
142704aae283SPali Rohár 	}
142804aae283SPali Rohár 
142904aae283SPali Rohár 	snprintf(priv->phys3, sizeof(priv->phys3), "%s/%s",
143004aae283SPali Rohár 		 psmouse->ps2dev.serio->phys,
143104aae283SPali Rohár 		 (priv->dev2 ? "input2" : "input1"));
143204aae283SPali Rohár 	dev3->phys = priv->phys3;
143304aae283SPali Rohár 
143404aae283SPali Rohár 	/*
143504aae283SPali Rohár 	 * format of input device name is: "protocol vendor name"
143604aae283SPali Rohár 	 * see function psmouse_switch_protocol() in psmouse-base.c
143704aae283SPali Rohár 	 */
143804aae283SPali Rohár 	dev3->name = "PS/2 ALPS Mouse";
143904aae283SPali Rohár 
144004aae283SPali Rohár 	dev3->id.bustype = BUS_I8042;
144104aae283SPali Rohár 	dev3->id.vendor  = 0x0002;
144204aae283SPali Rohár 	dev3->id.product = PSMOUSE_PS2;
144304aae283SPali Rohár 	dev3->id.version = 0x0000;
144404aae283SPali Rohár 	dev3->dev.parent = &psmouse->ps2dev.serio->dev;
144504aae283SPali Rohár 
144604aae283SPali Rohár 	input_set_capability(dev3, EV_REL, REL_X);
144704aae283SPali Rohár 	input_set_capability(dev3, EV_REL, REL_Y);
144804aae283SPali Rohár 	input_set_capability(dev3, EV_KEY, BTN_LEFT);
144904aae283SPali Rohár 	input_set_capability(dev3, EV_KEY, BTN_RIGHT);
145004aae283SPali Rohár 	input_set_capability(dev3, EV_KEY, BTN_MIDDLE);
145104aae283SPali Rohár 
145204aae283SPali Rohár 	__set_bit(INPUT_PROP_POINTER, dev3->propbit);
145304aae283SPali Rohár 
145404aae283SPali Rohár 	error = input_register_device(dev3);
145504aae283SPali Rohár 	if (error) {
145604aae283SPali Rohár 		psmouse_err(psmouse,
145704aae283SPali Rohár 			    "failed to register secondary device: %d\n",
145804aae283SPali Rohár 			    error);
145904aae283SPali Rohár 		input_free_device(dev3);
146004aae283SPali Rohár 		goto out;
146104aae283SPali Rohár 	}
146204aae283SPali Rohár 
146304aae283SPali Rohár 	priv->dev3 = dev3;
146404aae283SPali Rohár 
146504aae283SPali Rohár out:
146604aae283SPali Rohár 	/*
146704aae283SPali Rohár 	 * Save the error code so that we can detect that we
146804aae283SPali Rohár 	 * already tried to create the device.
146904aae283SPali Rohár 	 */
147004aae283SPali Rohár 	if (error)
147104aae283SPali Rohár 		priv->dev3 = ERR_PTR(error);
147204aae283SPali Rohár 
147304aae283SPali Rohár 	mutex_unlock(&alps_mutex);
147404aae283SPali Rohár }
147504aae283SPali Rohár 
147659c30afbSHans de Goede static void alps_report_bare_ps2_packet(struct psmouse *psmouse,
14771d9f2626SSebastian Kapfer 					unsigned char packet[],
14781d9f2626SSebastian Kapfer 					bool report_buttons)
14791d9f2626SSebastian Kapfer {
148059c30afbSHans de Goede 	struct alps_data *priv = psmouse->private;
14816bcca19fSHans de Goede 	struct input_dev *dev, *dev2 = NULL;
148259c30afbSHans de Goede 
1483e3a79212SHans de Goede 	/* Figure out which device to use to report the bare packet */
1484e3a79212SHans de Goede 	if (priv->proto_version == ALPS_PROTO_V2 &&
1485e3a79212SHans de Goede 	    (priv->flags & ALPS_DUALPOINT)) {
1486e3a79212SHans de Goede 		/* On V2 devices the DualPoint Stick reports bare packets */
1487e3a79212SHans de Goede 		dev = priv->dev2;
14886bcca19fSHans de Goede 		dev2 = psmouse->dev;
1489e3a79212SHans de Goede 	} else if (unlikely(IS_ERR_OR_NULL(priv->dev3))) {
149059c30afbSHans de Goede 		/* Register dev3 mouse if we received PS/2 packet first time */
149159c30afbSHans de Goede 		if (!IS_ERR(priv->dev3))
149259c30afbSHans de Goede 			psmouse_queue_work(psmouse, &priv->dev3_register_work,
149359c30afbSHans de Goede 					   0);
149459c30afbSHans de Goede 		return;
149559c30afbSHans de Goede 	} else {
149659c30afbSHans de Goede 		dev = priv->dev3;
149759c30afbSHans de Goede 	}
149859c30afbSHans de Goede 
14991d9f2626SSebastian Kapfer 	if (report_buttons)
15006bcca19fSHans de Goede 		alps_report_buttons(dev, dev2,
15011d9f2626SSebastian Kapfer 				packet[0] & 1, packet[0] & 2, packet[0] & 4);
15021d9f2626SSebastian Kapfer 
150304aae283SPali Rohár 	input_report_rel(dev, REL_X,
15041d9f2626SSebastian Kapfer 		packet[1] ? packet[1] - ((packet[0] << 4) & 0x100) : 0);
150504aae283SPali Rohár 	input_report_rel(dev, REL_Y,
15061d9f2626SSebastian Kapfer 		packet[2] ? ((packet[0] << 3) & 0x100) - packet[2] : 0);
15071d9f2626SSebastian Kapfer 
150804aae283SPali Rohár 	input_sync(dev);
15091d9f2626SSebastian Kapfer }
15101d9f2626SSebastian Kapfer 
15111d9f2626SSebastian Kapfer static psmouse_ret_t alps_handle_interleaved_ps2(struct psmouse *psmouse)
15121da177e4SLinus Torvalds {
15131da177e4SLinus Torvalds 	struct alps_data *priv = psmouse->private;
15141da177e4SLinus Torvalds 
15151d9f2626SSebastian Kapfer 	if (psmouse->pktcnt < 6)
15161d9f2626SSebastian Kapfer 		return PSMOUSE_GOOD_DATA;
15171d9f2626SSebastian Kapfer 
15181d9f2626SSebastian Kapfer 	if (psmouse->pktcnt == 6) {
15191d9f2626SSebastian Kapfer 		/*
15201d9f2626SSebastian Kapfer 		 * Start a timer to flush the packet if it ends up last
15211d9f2626SSebastian Kapfer 		 * 6-byte packet in the stream. Timer needs to fire
15221d9f2626SSebastian Kapfer 		 * psmouse core times out itself. 20 ms should be enough
15231d9f2626SSebastian Kapfer 		 * to decide if we are getting more data or not.
15241d9f2626SSebastian Kapfer 		 */
15251d9f2626SSebastian Kapfer 		mod_timer(&priv->timer, jiffies + msecs_to_jiffies(20));
15261d9f2626SSebastian Kapfer 		return PSMOUSE_GOOD_DATA;
15271d9f2626SSebastian Kapfer 	}
15281d9f2626SSebastian Kapfer 
15291d9f2626SSebastian Kapfer 	del_timer(&priv->timer);
15301d9f2626SSebastian Kapfer 
15311d9f2626SSebastian Kapfer 	if (psmouse->packet[6] & 0x80) {
15321d9f2626SSebastian Kapfer 
15331d9f2626SSebastian Kapfer 		/*
15341d9f2626SSebastian Kapfer 		 * Highest bit is set - that means we either had
15351d9f2626SSebastian Kapfer 		 * complete ALPS packet and this is start of the
15361d9f2626SSebastian Kapfer 		 * next packet or we got garbage.
15371d9f2626SSebastian Kapfer 		 */
15381d9f2626SSebastian Kapfer 
15391d9f2626SSebastian Kapfer 		if (((psmouse->packet[3] |
15401d9f2626SSebastian Kapfer 		      psmouse->packet[4] |
15411d9f2626SSebastian Kapfer 		      psmouse->packet[5]) & 0x80) ||
154299df65e7SKevin Cernekee 		    (!alps_is_valid_first_byte(priv, psmouse->packet[6]))) {
1543b5d21704SDmitry Torokhov 			psmouse_dbg(psmouse,
15443b112923SAndy Shevchenko 				    "refusing packet %4ph (suspected interleaved ps/2)\n",
15453b112923SAndy Shevchenko 				    psmouse->packet + 3);
15461d9f2626SSebastian Kapfer 			return PSMOUSE_BAD_DATA;
15471d9f2626SSebastian Kapfer 		}
15481d9f2626SSebastian Kapfer 
154924af5cb9SKevin Cernekee 		priv->process_packet(psmouse);
15501d9f2626SSebastian Kapfer 
15511d9f2626SSebastian Kapfer 		/* Continue with the next packet */
15521d9f2626SSebastian Kapfer 		psmouse->packet[0] = psmouse->packet[6];
15531d9f2626SSebastian Kapfer 		psmouse->pktcnt = 1;
15541d9f2626SSebastian Kapfer 
15551d9f2626SSebastian Kapfer 	} else {
15561d9f2626SSebastian Kapfer 
15571d9f2626SSebastian Kapfer 		/*
15581d9f2626SSebastian Kapfer 		 * High bit is 0 - that means that we indeed got a PS/2
15591d9f2626SSebastian Kapfer 		 * packet in the middle of ALPS packet.
15601d9f2626SSebastian Kapfer 		 *
15611d9f2626SSebastian Kapfer 		 * There is also possibility that we got 6-byte ALPS
15621d9f2626SSebastian Kapfer 		 * packet followed  by 3-byte packet from trackpoint. We
15631d9f2626SSebastian Kapfer 		 * can not distinguish between these 2 scenarios but
1564b5d21704SDmitry Torokhov 		 * because the latter is unlikely to happen in course of
15651d9f2626SSebastian Kapfer 		 * normal operation (user would need to press all
15661d9f2626SSebastian Kapfer 		 * buttons on the pad and start moving trackpoint
15671d9f2626SSebastian Kapfer 		 * without touching the pad surface) we assume former.
15681d9f2626SSebastian Kapfer 		 * Even if we are wrong the wost thing that would happen
15691d9f2626SSebastian Kapfer 		 * the cursor would jump but we should not get protocol
1570b5d21704SDmitry Torokhov 		 * de-synchronization.
15711d9f2626SSebastian Kapfer 		 */
15721d9f2626SSebastian Kapfer 
157359c30afbSHans de Goede 		alps_report_bare_ps2_packet(psmouse, &psmouse->packet[3],
157459c30afbSHans de Goede 					    false);
15751d9f2626SSebastian Kapfer 
15761d9f2626SSebastian Kapfer 		/*
15771d9f2626SSebastian Kapfer 		 * Continue with the standard ALPS protocol handling,
15781d9f2626SSebastian Kapfer 		 * but make sure we won't process it as an interleaved
15791d9f2626SSebastian Kapfer 		 * packet again, which may happen if all buttons are
15801d9f2626SSebastian Kapfer 		 * pressed. To avoid this let's reset the 4th bit which
15811d9f2626SSebastian Kapfer 		 * is normally 1.
15821d9f2626SSebastian Kapfer 		 */
15831d9f2626SSebastian Kapfer 		psmouse->packet[3] = psmouse->packet[6] & 0xf7;
15841d9f2626SSebastian Kapfer 		psmouse->pktcnt = 4;
15851d9f2626SSebastian Kapfer 	}
15861d9f2626SSebastian Kapfer 
15871d9f2626SSebastian Kapfer 	return PSMOUSE_GOOD_DATA;
15881d9f2626SSebastian Kapfer }
15891d9f2626SSebastian Kapfer 
15901d9f2626SSebastian Kapfer static void alps_flush_packet(unsigned long data)
15911d9f2626SSebastian Kapfer {
15921d9f2626SSebastian Kapfer 	struct psmouse *psmouse = (struct psmouse *)data;
159324af5cb9SKevin Cernekee 	struct alps_data *priv = psmouse->private;
15941d9f2626SSebastian Kapfer 
15951d9f2626SSebastian Kapfer 	serio_pause_rx(psmouse->ps2dev.serio);
15961d9f2626SSebastian Kapfer 
1597b46615feSSeth Forshee 	if (psmouse->pktcnt == psmouse->pktsize) {
15981d9f2626SSebastian Kapfer 
15991d9f2626SSebastian Kapfer 		/*
16001d9f2626SSebastian Kapfer 		 * We did not any more data in reasonable amount of time.
16011d9f2626SSebastian Kapfer 		 * Validate the last 3 bytes and process as a standard
16021d9f2626SSebastian Kapfer 		 * ALPS packet.
16031d9f2626SSebastian Kapfer 		 */
16041d9f2626SSebastian Kapfer 		if ((psmouse->packet[3] |
16051d9f2626SSebastian Kapfer 		     psmouse->packet[4] |
16061d9f2626SSebastian Kapfer 		     psmouse->packet[5]) & 0x80) {
1607b5d21704SDmitry Torokhov 			psmouse_dbg(psmouse,
16083b112923SAndy Shevchenko 				    "refusing packet %3ph (suspected interleaved ps/2)\n",
16093b112923SAndy Shevchenko 				    psmouse->packet + 3);
16101d9f2626SSebastian Kapfer 		} else {
161124af5cb9SKevin Cernekee 			priv->process_packet(psmouse);
16121d9f2626SSebastian Kapfer 		}
16131d9f2626SSebastian Kapfer 		psmouse->pktcnt = 0;
16141d9f2626SSebastian Kapfer 	}
16151d9f2626SSebastian Kapfer 
16161d9f2626SSebastian Kapfer 	serio_continue_rx(psmouse->ps2dev.serio);
16171d9f2626SSebastian Kapfer }
16181d9f2626SSebastian Kapfer 
16191d9f2626SSebastian Kapfer static psmouse_ret_t alps_process_byte(struct psmouse *psmouse)
16201d9f2626SSebastian Kapfer {
16211d9f2626SSebastian Kapfer 	struct alps_data *priv = psmouse->private;
16221d9f2626SSebastian Kapfer 
16234ab8f7f3SPali Rohár 	/*
16244ab8f7f3SPali Rohár 	 * Check if we are dealing with a bare PS/2 packet, presumably from
16254ab8f7f3SPali Rohár 	 * a device connected to the external PS/2 port. Because bare PS/2
16264ab8f7f3SPali Rohár 	 * protocol does not have enough constant bits to self-synchronize
16274ab8f7f3SPali Rohár 	 * properly we only do this if the device is fully synchronized.
16283db5b9f7SMasaki Ota 	 * Can not distinguish V8's first byte from PS/2 packet's
16294ab8f7f3SPali Rohár 	 */
16303db5b9f7SMasaki Ota 	if (priv->proto_version != ALPS_PROTO_V8 &&
16313db5b9f7SMasaki Ota 	    !psmouse->out_of_sync_cnt &&
16323db5b9f7SMasaki Ota 	    (psmouse->packet[0] & 0xc8) == 0x08) {
16333db5b9f7SMasaki Ota 
16341da177e4SLinus Torvalds 		if (psmouse->pktcnt == 3) {
163559c30afbSHans de Goede 			alps_report_bare_ps2_packet(psmouse, psmouse->packet,
16361d9f2626SSebastian Kapfer 						    true);
16371da177e4SLinus Torvalds 			return PSMOUSE_FULL_PACKET;
16381da177e4SLinus Torvalds 		}
16391da177e4SLinus Torvalds 		return PSMOUSE_GOOD_DATA;
16401da177e4SLinus Torvalds 	}
16411da177e4SLinus Torvalds 
16421d9f2626SSebastian Kapfer 	/* Check for PS/2 packet stuffed in the middle of ALPS packet. */
16431d9f2626SSebastian Kapfer 
164499df65e7SKevin Cernekee 	if ((priv->flags & ALPS_PS2_INTERLEAVED) &&
16451d9f2626SSebastian Kapfer 	    psmouse->pktcnt >= 4 && (psmouse->packet[3] & 0x0f) == 0x0f) {
16461d9f2626SSebastian Kapfer 		return alps_handle_interleaved_ps2(psmouse);
16471d9f2626SSebastian Kapfer 	}
16481d9f2626SSebastian Kapfer 
164999df65e7SKevin Cernekee 	if (!alps_is_valid_first_byte(priv, psmouse->packet[0])) {
1650b5d21704SDmitry Torokhov 		psmouse_dbg(psmouse,
1651b5d21704SDmitry Torokhov 			    "refusing packet[0] = %x (mask0 = %x, byte0 = %x)\n",
165299df65e7SKevin Cernekee 			    psmouse->packet[0], priv->mask0, priv->byte0);
16531da177e4SLinus Torvalds 		return PSMOUSE_BAD_DATA;
16541d9f2626SSebastian Kapfer 	}
16551da177e4SLinus Torvalds 
1656b46615feSSeth Forshee 	/* Bytes 2 - pktsize should have 0 in the highest bit */
1657a7ef82aeSPali Rohár 	if (priv->proto_version < ALPS_PROTO_V5 &&
165875af9e56SDave Turvene 	    psmouse->pktcnt >= 2 && psmouse->pktcnt <= psmouse->pktsize &&
16591d9f2626SSebastian Kapfer 	    (psmouse->packet[psmouse->pktcnt - 1] & 0x80)) {
1660b5d21704SDmitry Torokhov 		psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
1661b5d21704SDmitry Torokhov 			    psmouse->pktcnt - 1,
1662b5d21704SDmitry Torokhov 			    psmouse->packet[psmouse->pktcnt - 1]);
1663a7ef82aeSPali Rohár 
1664fb2dd7a6SDmitry Torokhov 		if (priv->proto_version == ALPS_PROTO_V3_RUSHMORE &&
1665a7ef82aeSPali Rohár 		    psmouse->pktcnt == psmouse->pktsize) {
1666a7ef82aeSPali Rohár 			/*
1667a7ef82aeSPali Rohár 			 * Some Dell boxes, such as Latitude E6440 or E7440
1668a7ef82aeSPali Rohár 			 * with closed lid, quite often smash last byte of
1669a7ef82aeSPali Rohár 			 * otherwise valid packet with 0xff. Given that the
1670a7ef82aeSPali Rohár 			 * next packet is very likely to be valid let's
1671a7ef82aeSPali Rohár 			 * report PSMOUSE_FULL_PACKET but not process data,
1672a7ef82aeSPali Rohár 			 * rather than reporting PSMOUSE_BAD_DATA and
1673a7ef82aeSPali Rohár 			 * filling the logs.
1674a7ef82aeSPali Rohár 			 */
1675a7ef82aeSPali Rohár 			return PSMOUSE_FULL_PACKET;
1676a7ef82aeSPali Rohár 		}
1677a7ef82aeSPali Rohár 
16781da177e4SLinus Torvalds 		return PSMOUSE_BAD_DATA;
16791d9f2626SSebastian Kapfer 	}
16801da177e4SLinus Torvalds 
16813db5b9f7SMasaki Ota 	if ((priv->proto_version == ALPS_PROTO_V7 &&
16823db5b9f7SMasaki Ota 			!alps_is_valid_package_v7(psmouse)) ||
16833db5b9f7SMasaki Ota 	    (priv->proto_version == ALPS_PROTO_V8 &&
16843db5b9f7SMasaki Ota 			!alps_is_valid_package_ss4_v2(psmouse))) {
16853808843cSYunkang Tang 		psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
16863808843cSYunkang Tang 			    psmouse->pktcnt - 1,
16873808843cSYunkang Tang 			    psmouse->packet[psmouse->pktcnt - 1]);
16883808843cSYunkang Tang 		return PSMOUSE_BAD_DATA;
16893808843cSYunkang Tang 	}
16903808843cSYunkang Tang 
1691b46615feSSeth Forshee 	if (psmouse->pktcnt == psmouse->pktsize) {
169224af5cb9SKevin Cernekee 		priv->process_packet(psmouse);
16931da177e4SLinus Torvalds 		return PSMOUSE_FULL_PACKET;
16941da177e4SLinus Torvalds 	}
16951da177e4SLinus Torvalds 
16961da177e4SLinus Torvalds 	return PSMOUSE_GOOD_DATA;
16971da177e4SLinus Torvalds }
16981da177e4SLinus Torvalds 
169925bded7cSSeth Forshee static int alps_command_mode_send_nibble(struct psmouse *psmouse, int nibble)
170025bded7cSSeth Forshee {
170125bded7cSSeth Forshee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
170225bded7cSSeth Forshee 	struct alps_data *priv = psmouse->private;
170325bded7cSSeth Forshee 	int command;
170425bded7cSSeth Forshee 	unsigned char *param;
170525bded7cSSeth Forshee 	unsigned char dummy[4];
170625bded7cSSeth Forshee 
170725bded7cSSeth Forshee 	BUG_ON(nibble > 0xf);
170825bded7cSSeth Forshee 
170925bded7cSSeth Forshee 	command = priv->nibble_commands[nibble].command;
171025bded7cSSeth Forshee 	param = (command & 0x0f00) ?
171125bded7cSSeth Forshee 		dummy : (unsigned char *)&priv->nibble_commands[nibble].data;
171225bded7cSSeth Forshee 
171325bded7cSSeth Forshee 	if (ps2_command(ps2dev, param, command))
171425bded7cSSeth Forshee 		return -1;
171525bded7cSSeth Forshee 
171625bded7cSSeth Forshee 	return 0;
171725bded7cSSeth Forshee }
171825bded7cSSeth Forshee 
171925bded7cSSeth Forshee static int alps_command_mode_set_addr(struct psmouse *psmouse, int addr)
172025bded7cSSeth Forshee {
172125bded7cSSeth Forshee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
172225bded7cSSeth Forshee 	struct alps_data *priv = psmouse->private;
172325bded7cSSeth Forshee 	int i, nibble;
172425bded7cSSeth Forshee 
172525bded7cSSeth Forshee 	if (ps2_command(ps2dev, NULL, priv->addr_command))
172625bded7cSSeth Forshee 		return -1;
172725bded7cSSeth Forshee 
172825bded7cSSeth Forshee 	for (i = 12; i >= 0; i -= 4) {
172925bded7cSSeth Forshee 		nibble = (addr >> i) & 0xf;
173025bded7cSSeth Forshee 		if (alps_command_mode_send_nibble(psmouse, nibble))
173125bded7cSSeth Forshee 			return -1;
173225bded7cSSeth Forshee 	}
173325bded7cSSeth Forshee 
173425bded7cSSeth Forshee 	return 0;
173525bded7cSSeth Forshee }
173625bded7cSSeth Forshee 
173725bded7cSSeth Forshee static int __alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
173825bded7cSSeth Forshee {
173925bded7cSSeth Forshee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
174025bded7cSSeth Forshee 	unsigned char param[4];
174125bded7cSSeth Forshee 
174225bded7cSSeth Forshee 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
174325bded7cSSeth Forshee 		return -1;
174425bded7cSSeth Forshee 
174525bded7cSSeth Forshee 	/*
174625bded7cSSeth Forshee 	 * The address being read is returned in the first two bytes
174725bded7cSSeth Forshee 	 * of the result. Check that this address matches the expected
174825bded7cSSeth Forshee 	 * address.
174925bded7cSSeth Forshee 	 */
175025bded7cSSeth Forshee 	if (addr != ((param[0] << 8) | param[1]))
175125bded7cSSeth Forshee 		return -1;
175225bded7cSSeth Forshee 
175325bded7cSSeth Forshee 	return param[2];
175425bded7cSSeth Forshee }
175525bded7cSSeth Forshee 
175625bded7cSSeth Forshee static int alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
175725bded7cSSeth Forshee {
175825bded7cSSeth Forshee 	if (alps_command_mode_set_addr(psmouse, addr))
175925bded7cSSeth Forshee 		return -1;
176025bded7cSSeth Forshee 	return __alps_command_mode_read_reg(psmouse, addr);
176125bded7cSSeth Forshee }
176225bded7cSSeth Forshee 
176325bded7cSSeth Forshee static int __alps_command_mode_write_reg(struct psmouse *psmouse, u8 value)
176425bded7cSSeth Forshee {
176525bded7cSSeth Forshee 	if (alps_command_mode_send_nibble(psmouse, (value >> 4) & 0xf))
176625bded7cSSeth Forshee 		return -1;
176725bded7cSSeth Forshee 	if (alps_command_mode_send_nibble(psmouse, value & 0xf))
176825bded7cSSeth Forshee 		return -1;
176925bded7cSSeth Forshee 	return 0;
177025bded7cSSeth Forshee }
177125bded7cSSeth Forshee 
177225bded7cSSeth Forshee static int alps_command_mode_write_reg(struct psmouse *psmouse, int addr,
177325bded7cSSeth Forshee 				       u8 value)
177425bded7cSSeth Forshee {
177525bded7cSSeth Forshee 	if (alps_command_mode_set_addr(psmouse, addr))
177625bded7cSSeth Forshee 		return -1;
177725bded7cSSeth Forshee 	return __alps_command_mode_write_reg(psmouse, value);
177825bded7cSSeth Forshee }
177925bded7cSSeth Forshee 
178024ba9707SKevin Cernekee static int alps_rpt_cmd(struct psmouse *psmouse, int init_command,
178124ba9707SKevin Cernekee 			int repeated_command, unsigned char *param)
178224ba9707SKevin Cernekee {
178324ba9707SKevin Cernekee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
178424ba9707SKevin Cernekee 
178524ba9707SKevin Cernekee 	param[0] = 0;
178624ba9707SKevin Cernekee 	if (init_command && ps2_command(ps2dev, param, init_command))
178724ba9707SKevin Cernekee 		return -EIO;
178824ba9707SKevin Cernekee 
178924ba9707SKevin Cernekee 	if (ps2_command(ps2dev,  NULL, repeated_command) ||
179024ba9707SKevin Cernekee 	    ps2_command(ps2dev,  NULL, repeated_command) ||
179124ba9707SKevin Cernekee 	    ps2_command(ps2dev,  NULL, repeated_command))
179224ba9707SKevin Cernekee 		return -EIO;
179324ba9707SKevin Cernekee 
179424ba9707SKevin Cernekee 	param[0] = param[1] = param[2] = 0xff;
179524ba9707SKevin Cernekee 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
179624ba9707SKevin Cernekee 		return -EIO;
179724ba9707SKevin Cernekee 
179839fbe585SDmitry Torokhov 	psmouse_dbg(psmouse, "%2.2X report: %3ph\n",
179939fbe585SDmitry Torokhov 		    repeated_command, param);
180024ba9707SKevin Cernekee 	return 0;
180124ba9707SKevin Cernekee }
180224ba9707SKevin Cernekee 
18033808843cSYunkang Tang static bool alps_check_valid_firmware_id(unsigned char id[])
18043808843cSYunkang Tang {
18053808843cSYunkang Tang 	if (id[0] == 0x73)
18063808843cSYunkang Tang 		return true;
18073808843cSYunkang Tang 
18083808843cSYunkang Tang 	if (id[0] == 0x88 &&
18093808843cSYunkang Tang 	    (id[1] == 0x07 ||
18103808843cSYunkang Tang 	     id[1] == 0x08 ||
18113808843cSYunkang Tang 	     (id[1] & 0xf0) == 0xb0 ||
18123808843cSYunkang Tang 	     (id[1] & 0xf0) == 0xc0)) {
18133808843cSYunkang Tang 		return true;
18143808843cSYunkang Tang 	}
18153808843cSYunkang Tang 
18163808843cSYunkang Tang 	return false;
18173808843cSYunkang Tang }
18183808843cSYunkang Tang 
1819d18e53fcSKevin Cernekee static int alps_enter_command_mode(struct psmouse *psmouse)
182025bded7cSSeth Forshee {
182125bded7cSSeth Forshee 	unsigned char param[4];
182225bded7cSSeth Forshee 
182324ba9707SKevin Cernekee 	if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_RESET_WRAP, param)) {
182425bded7cSSeth Forshee 		psmouse_err(psmouse, "failed to enter command mode\n");
182525bded7cSSeth Forshee 		return -1;
182625bded7cSSeth Forshee 	}
182725bded7cSSeth Forshee 
18283808843cSYunkang Tang 	if (!alps_check_valid_firmware_id(param)) {
182925bded7cSSeth Forshee 		psmouse_dbg(psmouse,
183024ba9707SKevin Cernekee 			    "unknown response while entering command mode\n");
183125bded7cSSeth Forshee 		return -1;
183225bded7cSSeth Forshee 	}
183325bded7cSSeth Forshee 	return 0;
183425bded7cSSeth Forshee }
183525bded7cSSeth Forshee 
183625bded7cSSeth Forshee static inline int alps_exit_command_mode(struct psmouse *psmouse)
183725bded7cSSeth Forshee {
183825bded7cSSeth Forshee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
183925bded7cSSeth Forshee 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM))
184025bded7cSSeth Forshee 		return -1;
184125bded7cSSeth Forshee 	return 0;
184225bded7cSSeth Forshee }
184325bded7cSSeth Forshee 
18441da177e4SLinus Torvalds /*
18451da177e4SLinus Torvalds  * For DualPoint devices select the device that should respond to
18461da177e4SLinus Torvalds  * subsequent commands. It looks like glidepad is behind stickpointer,
18471da177e4SLinus Torvalds  * I'd thought it would be other way around...
18481da177e4SLinus Torvalds  */
184925bded7cSSeth Forshee static int alps_passthrough_mode_v2(struct psmouse *psmouse, bool enable)
18501da177e4SLinus Torvalds {
18511da177e4SLinus Torvalds 	struct ps2dev *ps2dev = &psmouse->ps2dev;
18521da177e4SLinus Torvalds 	int cmd = enable ? PSMOUSE_CMD_SETSCALE21 : PSMOUSE_CMD_SETSCALE11;
18531da177e4SLinus Torvalds 
18541da177e4SLinus Torvalds 	if (ps2_command(ps2dev, NULL, cmd) ||
18551da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, cmd) ||
18561da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, cmd) ||
18571da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
18581da177e4SLinus Torvalds 		return -1;
18591da177e4SLinus Torvalds 
18601da177e4SLinus Torvalds 	/* we may get 3 more bytes, just ignore them */
1861c611763dSDmitry Torokhov 	ps2_drain(ps2dev, 3, 100);
18621da177e4SLinus Torvalds 
18631da177e4SLinus Torvalds 	return 0;
18641da177e4SLinus Torvalds }
18651da177e4SLinus Torvalds 
186625bded7cSSeth Forshee static int alps_absolute_mode_v1_v2(struct psmouse *psmouse)
18671da177e4SLinus Torvalds {
18681da177e4SLinus Torvalds 	struct ps2dev *ps2dev = &psmouse->ps2dev;
18691da177e4SLinus Torvalds 
18701da177e4SLinus Torvalds 	/* Try ALPS magic knock - 4 disable before enable */
18711da177e4SLinus Torvalds 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
18721da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
18731da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
18741da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
18751da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
18761da177e4SLinus Torvalds 		return -1;
18771da177e4SLinus Torvalds 
18781da177e4SLinus Torvalds 	/*
18791da177e4SLinus Torvalds 	 * Switch mouse to poll (remote) mode so motion data will not
18801da177e4SLinus Torvalds 	 * get in our way
18811da177e4SLinus Torvalds 	 */
1882ad56814fSGuenter Roeck 	return ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL);
18831da177e4SLinus Torvalds }
18841da177e4SLinus Torvalds 
188595f75e91SYunkang Tang static int alps_monitor_mode_send_word(struct psmouse *psmouse, u16 word)
188695f75e91SYunkang Tang {
188795f75e91SYunkang Tang 	int i, nibble;
188895f75e91SYunkang Tang 
188995f75e91SYunkang Tang 	/*
189095f75e91SYunkang Tang 	 * b0-b11 are valid bits, send sequence is inverse.
189195f75e91SYunkang Tang 	 * e.g. when word = 0x0123, nibble send sequence is 3, 2, 1
189295f75e91SYunkang Tang 	 */
189395f75e91SYunkang Tang 	for (i = 0; i <= 8; i += 4) {
189495f75e91SYunkang Tang 		nibble = (word >> i) & 0xf;
189595f75e91SYunkang Tang 		if (alps_command_mode_send_nibble(psmouse, nibble))
189695f75e91SYunkang Tang 			return -1;
189795f75e91SYunkang Tang 	}
189895f75e91SYunkang Tang 
189995f75e91SYunkang Tang 	return 0;
190095f75e91SYunkang Tang }
190195f75e91SYunkang Tang 
190295f75e91SYunkang Tang static int alps_monitor_mode_write_reg(struct psmouse *psmouse,
190395f75e91SYunkang Tang 				       u16 addr, u16 value)
190495f75e91SYunkang Tang {
190595f75e91SYunkang Tang 	struct ps2dev *ps2dev = &psmouse->ps2dev;
190695f75e91SYunkang Tang 
190795f75e91SYunkang Tang 	/* 0x0A0 is the command to write the word */
190895f75e91SYunkang Tang 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE) ||
190995f75e91SYunkang Tang 	    alps_monitor_mode_send_word(psmouse, 0x0A0) ||
191095f75e91SYunkang Tang 	    alps_monitor_mode_send_word(psmouse, addr) ||
191195f75e91SYunkang Tang 	    alps_monitor_mode_send_word(psmouse, value) ||
191295f75e91SYunkang Tang 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
191395f75e91SYunkang Tang 		return -1;
191495f75e91SYunkang Tang 
191595f75e91SYunkang Tang 	return 0;
191695f75e91SYunkang Tang }
191795f75e91SYunkang Tang 
191895f75e91SYunkang Tang static int alps_monitor_mode(struct psmouse *psmouse, bool enable)
191995f75e91SYunkang Tang {
192095f75e91SYunkang Tang 	struct ps2dev *ps2dev = &psmouse->ps2dev;
192195f75e91SYunkang Tang 
192295f75e91SYunkang Tang 	if (enable) {
192395f75e91SYunkang Tang 		/* EC E9 F5 F5 E7 E6 E7 E9 to enter monitor mode */
192495f75e91SYunkang Tang 		if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
192595f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO) ||
192695f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
192795f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
192895f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
192995f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
193095f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
193195f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO))
193295f75e91SYunkang Tang 			return -1;
193395f75e91SYunkang Tang 	} else {
193495f75e91SYunkang Tang 		/* EC to exit monitor mode */
193595f75e91SYunkang Tang 		if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP))
193695f75e91SYunkang Tang 			return -1;
193795f75e91SYunkang Tang 	}
193895f75e91SYunkang Tang 
193995f75e91SYunkang Tang 	return 0;
194095f75e91SYunkang Tang }
194195f75e91SYunkang Tang 
194295f75e91SYunkang Tang static int alps_absolute_mode_v6(struct psmouse *psmouse)
194395f75e91SYunkang Tang {
194495f75e91SYunkang Tang 	u16 reg_val = 0x181;
194595f75e91SYunkang Tang 	int ret = -1;
194695f75e91SYunkang Tang 
194795f75e91SYunkang Tang 	/* enter monitor mode, to write the register */
194895f75e91SYunkang Tang 	if (alps_monitor_mode(psmouse, true))
194995f75e91SYunkang Tang 		return -1;
195095f75e91SYunkang Tang 
195195f75e91SYunkang Tang 	ret = alps_monitor_mode_write_reg(psmouse, 0x000, reg_val);
195295f75e91SYunkang Tang 
195395f75e91SYunkang Tang 	if (alps_monitor_mode(psmouse, false))
195495f75e91SYunkang Tang 		ret = -1;
195595f75e91SYunkang Tang 
195695f75e91SYunkang Tang 	return ret;
195795f75e91SYunkang Tang }
195895f75e91SYunkang Tang 
19591da177e4SLinus Torvalds static int alps_get_status(struct psmouse *psmouse, char *param)
19601da177e4SLinus Torvalds {
19611da177e4SLinus Torvalds 	/* Get status: 0xF5 0xF5 0xF5 0xE9 */
196224ba9707SKevin Cernekee 	if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_DISABLE, param))
19631da177e4SLinus Torvalds 		return -1;
19641da177e4SLinus Torvalds 
19651da177e4SLinus Torvalds 	return 0;
19661da177e4SLinus Torvalds }
19671da177e4SLinus Torvalds 
19681da177e4SLinus Torvalds /*
19691da177e4SLinus Torvalds  * Turn touchpad tapping on or off. The sequences are:
19701da177e4SLinus Torvalds  * 0xE9 0xF5 0xF5 0xF3 0x0A to enable,
19711da177e4SLinus Torvalds  * 0xE9 0xF5 0xF5 0xE8 0x00 to disable.
19721da177e4SLinus Torvalds  * My guess that 0xE9 (GetInfo) is here as a sync point.
19731da177e4SLinus Torvalds  * For models that also have stickpointer (DualPoints) its tapping
19741da177e4SLinus Torvalds  * is controlled separately (0xE6 0xE6 0xE6 0xF3 0x14|0x0A) but
19751da177e4SLinus Torvalds  * we don't fiddle with it.
19761da177e4SLinus Torvalds  */
19771da177e4SLinus Torvalds static int alps_tap_mode(struct psmouse *psmouse, int enable)
19781da177e4SLinus Torvalds {
19791da177e4SLinus Torvalds 	struct ps2dev *ps2dev = &psmouse->ps2dev;
19801da177e4SLinus Torvalds 	int cmd = enable ? PSMOUSE_CMD_SETRATE : PSMOUSE_CMD_SETRES;
19811da177e4SLinus Torvalds 	unsigned char tap_arg = enable ? 0x0A : 0x00;
19821da177e4SLinus Torvalds 	unsigned char param[4];
19831da177e4SLinus Torvalds 
19841da177e4SLinus Torvalds 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO) ||
19851da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
19861da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
19871da177e4SLinus Torvalds 	    ps2_command(ps2dev, &tap_arg, cmd))
19881da177e4SLinus Torvalds 		return -1;
19891da177e4SLinus Torvalds 
19901da177e4SLinus Torvalds 	if (alps_get_status(psmouse, param))
19911da177e4SLinus Torvalds 		return -1;
19921da177e4SLinus Torvalds 
19931da177e4SLinus Torvalds 	return 0;
19941da177e4SLinus Torvalds }
19951da177e4SLinus Torvalds 
1996f0d5c6f4SDmitry Torokhov /*
1997f0d5c6f4SDmitry Torokhov  * alps_poll() - poll the touchpad for current motion packet.
1998f0d5c6f4SDmitry Torokhov  * Used in resync.
1999f0d5c6f4SDmitry Torokhov  */
2000f0d5c6f4SDmitry Torokhov static int alps_poll(struct psmouse *psmouse)
2001f0d5c6f4SDmitry Torokhov {
2002f0d5c6f4SDmitry Torokhov 	struct alps_data *priv = psmouse->private;
2003b46615feSSeth Forshee 	unsigned char buf[sizeof(psmouse->packet)];
2004b7802c5cSDmitry Torokhov 	bool poll_failed;
2005f0d5c6f4SDmitry Torokhov 
200699df65e7SKevin Cernekee 	if (priv->flags & ALPS_PASS)
200725bded7cSSeth Forshee 		alps_passthrough_mode_v2(psmouse, true);
2008f0d5c6f4SDmitry Torokhov 
2009f0d5c6f4SDmitry Torokhov 	poll_failed = ps2_command(&psmouse->ps2dev, buf,
2010f0d5c6f4SDmitry Torokhov 				  PSMOUSE_CMD_POLL | (psmouse->pktsize << 8)) < 0;
2011f0d5c6f4SDmitry Torokhov 
201299df65e7SKevin Cernekee 	if (priv->flags & ALPS_PASS)
201325bded7cSSeth Forshee 		alps_passthrough_mode_v2(psmouse, false);
2014f0d5c6f4SDmitry Torokhov 
201599df65e7SKevin Cernekee 	if (poll_failed || (buf[0] & priv->mask0) != priv->byte0)
2016f0d5c6f4SDmitry Torokhov 		return -1;
2017f0d5c6f4SDmitry Torokhov 
2018f0d5c6f4SDmitry Torokhov 	if ((psmouse->badbyte & 0xc8) == 0x08) {
2019f0d5c6f4SDmitry Torokhov /*
2020f0d5c6f4SDmitry Torokhov  * Poll the track stick ...
2021f0d5c6f4SDmitry Torokhov  */
2022f0d5c6f4SDmitry Torokhov 		if (ps2_command(&psmouse->ps2dev, buf, PSMOUSE_CMD_POLL | (3 << 8)))
2023f0d5c6f4SDmitry Torokhov 			return -1;
2024f0d5c6f4SDmitry Torokhov 	}
2025f0d5c6f4SDmitry Torokhov 
2026f0d5c6f4SDmitry Torokhov 	memcpy(psmouse->packet, buf, sizeof(buf));
2027f0d5c6f4SDmitry Torokhov 	return 0;
2028f0d5c6f4SDmitry Torokhov }
2029f0d5c6f4SDmitry Torokhov 
203025bded7cSSeth Forshee static int alps_hw_init_v1_v2(struct psmouse *psmouse)
20311da177e4SLinus Torvalds {
20321da177e4SLinus Torvalds 	struct alps_data *priv = psmouse->private;
20331da177e4SLinus Torvalds 
203499df65e7SKevin Cernekee 	if ((priv->flags & ALPS_PASS) &&
203525bded7cSSeth Forshee 	    alps_passthrough_mode_v2(psmouse, true)) {
20361da177e4SLinus Torvalds 		return -1;
2037b7802c5cSDmitry Torokhov 	}
20381da177e4SLinus Torvalds 
2039b7802c5cSDmitry Torokhov 	if (alps_tap_mode(psmouse, true)) {
2040b5d21704SDmitry Torokhov 		psmouse_warn(psmouse, "Failed to enable hardware tapping\n");
20411da177e4SLinus Torvalds 		return -1;
2042963f626dSPeter Osterlund 	}
20431da177e4SLinus Torvalds 
204425bded7cSSeth Forshee 	if (alps_absolute_mode_v1_v2(psmouse)) {
2045b5d21704SDmitry Torokhov 		psmouse_err(psmouse, "Failed to enable absolute mode\n");
20461da177e4SLinus Torvalds 		return -1;
20471da177e4SLinus Torvalds 	}
20481da177e4SLinus Torvalds 
204999df65e7SKevin Cernekee 	if ((priv->flags & ALPS_PASS) &&
205025bded7cSSeth Forshee 	    alps_passthrough_mode_v2(psmouse, false)) {
20511da177e4SLinus Torvalds 		return -1;
2052b7802c5cSDmitry Torokhov 	}
20531da177e4SLinus Torvalds 
20541e0c5b12SDmitry Torokhov 	/* ALPS needs stream mode, otherwise it won't report any data */
20551e0c5b12SDmitry Torokhov 	if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)) {
2056b5d21704SDmitry Torokhov 		psmouse_err(psmouse, "Failed to enable stream mode\n");
20571e0c5b12SDmitry Torokhov 		return -1;
20581e0c5b12SDmitry Torokhov 	}
20591e0c5b12SDmitry Torokhov 
20601e0c5b12SDmitry Torokhov 	return 0;
20611e0c5b12SDmitry Torokhov }
20621e0c5b12SDmitry Torokhov 
206395f75e91SYunkang Tang static int alps_hw_init_v6(struct psmouse *psmouse)
206495f75e91SYunkang Tang {
206595f75e91SYunkang Tang 	unsigned char param[2] = {0xC8, 0x14};
206695f75e91SYunkang Tang 
206795f75e91SYunkang Tang 	/* Enter passthrough mode to let trackpoint enter 6byte raw mode */
206895f75e91SYunkang Tang 	if (alps_passthrough_mode_v2(psmouse, true))
206995f75e91SYunkang Tang 		return -1;
207095f75e91SYunkang Tang 
207195f75e91SYunkang Tang 	if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
207295f75e91SYunkang Tang 	    ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
207395f75e91SYunkang Tang 	    ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
207495f75e91SYunkang Tang 	    ps2_command(&psmouse->ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
207595f75e91SYunkang Tang 	    ps2_command(&psmouse->ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
207695f75e91SYunkang Tang 		return -1;
207795f75e91SYunkang Tang 
207895f75e91SYunkang Tang 	if (alps_passthrough_mode_v2(psmouse, false))
207995f75e91SYunkang Tang 		return -1;
208095f75e91SYunkang Tang 
208195f75e91SYunkang Tang 	if (alps_absolute_mode_v6(psmouse)) {
208295f75e91SYunkang Tang 		psmouse_err(psmouse, "Failed to enable absolute mode\n");
208395f75e91SYunkang Tang 		return -1;
208495f75e91SYunkang Tang 	}
208595f75e91SYunkang Tang 
208695f75e91SYunkang Tang 	return 0;
208795f75e91SYunkang Tang }
208895f75e91SYunkang Tang 
208925bded7cSSeth Forshee /*
2090cd401204SKevin Cernekee  * Enable or disable passthrough mode to the trackstick.
209125bded7cSSeth Forshee  */
2092cd401204SKevin Cernekee static int alps_passthrough_mode_v3(struct psmouse *psmouse,
2093cd401204SKevin Cernekee 				    int reg_base, bool enable)
209425bded7cSSeth Forshee {
2095cd401204SKevin Cernekee 	int reg_val, ret = -1;
209625bded7cSSeth Forshee 
2097d18e53fcSKevin Cernekee 	if (alps_enter_command_mode(psmouse))
209825bded7cSSeth Forshee 		return -1;
209925bded7cSSeth Forshee 
2100cd401204SKevin Cernekee 	reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x0008);
2101cd401204SKevin Cernekee 	if (reg_val == -1)
2102cd401204SKevin Cernekee 		goto error;
2103cd401204SKevin Cernekee 
210425bded7cSSeth Forshee 	if (enable)
210525bded7cSSeth Forshee 		reg_val |= 0x01;
210625bded7cSSeth Forshee 	else
210725bded7cSSeth Forshee 		reg_val &= ~0x01;
210825bded7cSSeth Forshee 
2109cd401204SKevin Cernekee 	ret = __alps_command_mode_write_reg(psmouse, reg_val);
211025bded7cSSeth Forshee 
2111cd401204SKevin Cernekee error:
2112cd401204SKevin Cernekee 	if (alps_exit_command_mode(psmouse))
2113cd401204SKevin Cernekee 		ret = -1;
2114cd401204SKevin Cernekee 	return ret;
211525bded7cSSeth Forshee }
211625bded7cSSeth Forshee 
211725bded7cSSeth Forshee /* Must be in command mode when calling this function */
211825bded7cSSeth Forshee static int alps_absolute_mode_v3(struct psmouse *psmouse)
211925bded7cSSeth Forshee {
212025bded7cSSeth Forshee 	int reg_val;
212125bded7cSSeth Forshee 
212225bded7cSSeth Forshee 	reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
212325bded7cSSeth Forshee 	if (reg_val == -1)
212425bded7cSSeth Forshee 		return -1;
212525bded7cSSeth Forshee 
212625bded7cSSeth Forshee 	reg_val |= 0x06;
212725bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, reg_val))
212825bded7cSSeth Forshee 		return -1;
212925bded7cSSeth Forshee 
213025bded7cSSeth Forshee 	return 0;
213125bded7cSSeth Forshee }
213225bded7cSSeth Forshee 
2133dae928ecSPali Rohár static int alps_probe_trackstick_v3_v7(struct psmouse *psmouse, int reg_base)
213425bded7cSSeth Forshee {
2135cd401204SKevin Cernekee 	int ret = -EIO, reg_val;
213625bded7cSSeth Forshee 
2137d18e53fcSKevin Cernekee 	if (alps_enter_command_mode(psmouse))
213825bded7cSSeth Forshee 		goto error;
213925bded7cSSeth Forshee 
2140cd401204SKevin Cernekee 	reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x08);
214125bded7cSSeth Forshee 	if (reg_val == -1)
214225bded7cSSeth Forshee 		goto error;
2143cd401204SKevin Cernekee 
2144cd401204SKevin Cernekee 	/* bit 7: trackstick is present */
2145cd401204SKevin Cernekee 	ret = reg_val & 0x80 ? 0 : -ENODEV;
2146cd401204SKevin Cernekee 
2147cd401204SKevin Cernekee error:
2148cd401204SKevin Cernekee 	alps_exit_command_mode(psmouse);
2149cd401204SKevin Cernekee 	return ret;
2150cd401204SKevin Cernekee }
2151cd401204SKevin Cernekee 
2152cd401204SKevin Cernekee static int alps_setup_trackstick_v3(struct psmouse *psmouse, int reg_base)
2153cd401204SKevin Cernekee {
2154cd401204SKevin Cernekee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
2155cd401204SKevin Cernekee 	int ret = 0;
2156cd401204SKevin Cernekee 	unsigned char param[4];
2157cd401204SKevin Cernekee 
2158cd401204SKevin Cernekee 	if (alps_passthrough_mode_v3(psmouse, reg_base, true))
2159cd401204SKevin Cernekee 		return -EIO;
216025bded7cSSeth Forshee 
216125bded7cSSeth Forshee 	/*
216225bded7cSSeth Forshee 	 * E7 report for the trackstick
216325bded7cSSeth Forshee 	 *
216425bded7cSSeth Forshee 	 * There have been reports of failures to seem to trace back
216525bded7cSSeth Forshee 	 * to the above trackstick check failing. When these occur
216625bded7cSSeth Forshee 	 * this E7 report fails, so when that happens we continue
216725bded7cSSeth Forshee 	 * with the assumption that there isn't a trackstick after
216825bded7cSSeth Forshee 	 * all.
216925bded7cSSeth Forshee 	 */
2170cd401204SKevin Cernekee 	if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_SETSCALE21, param)) {
2171a09221e8SDmitry Torokhov 		psmouse_warn(psmouse, "Failed to initialize trackstick (E7 report failed)\n");
2172cd401204SKevin Cernekee 		ret = -ENODEV;
217325bded7cSSeth Forshee 	} else {
217439fbe585SDmitry Torokhov 		psmouse_dbg(psmouse, "trackstick E7 report: %3ph\n", param);
217525bded7cSSeth Forshee 
217625bded7cSSeth Forshee 		/*
217725bded7cSSeth Forshee 		 * Not sure what this does, but it is absolutely
217825bded7cSSeth Forshee 		 * essential. Without it, the touchpad does not
217925bded7cSSeth Forshee 		 * work at all and the trackstick just emits normal
218025bded7cSSeth Forshee 		 * PS/2 packets.
218125bded7cSSeth Forshee 		 */
218225bded7cSSeth Forshee 		if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
218325bded7cSSeth Forshee 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
218425bded7cSSeth Forshee 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
218525bded7cSSeth Forshee 		    alps_command_mode_send_nibble(psmouse, 0x9) ||
218625bded7cSSeth Forshee 		    alps_command_mode_send_nibble(psmouse, 0x4)) {
218725bded7cSSeth Forshee 			psmouse_err(psmouse,
218825bded7cSSeth Forshee 				    "Error sending magic E6 sequence\n");
2189cd401204SKevin Cernekee 			ret = -EIO;
219025bded7cSSeth Forshee 			goto error;
219125bded7cSSeth Forshee 		}
219225bded7cSSeth Forshee 
2193cd401204SKevin Cernekee 		/*
2194cd401204SKevin Cernekee 		 * This ensures the trackstick packets are in the format
2195cd401204SKevin Cernekee 		 * supported by this driver. If bit 1 isn't set the packet
2196cd401204SKevin Cernekee 		 * format is different.
2197cd401204SKevin Cernekee 		 */
2198d18e53fcSKevin Cernekee 		if (alps_enter_command_mode(psmouse) ||
2199cd401204SKevin Cernekee 		    alps_command_mode_write_reg(psmouse,
2200cd401204SKevin Cernekee 						reg_base + 0x08, 0x82) ||
2201cd401204SKevin Cernekee 		    alps_exit_command_mode(psmouse))
2202cd401204SKevin Cernekee 			ret = -EIO;
2203cd401204SKevin Cernekee 	}
2204cd401204SKevin Cernekee 
2205cd401204SKevin Cernekee error:
2206cd401204SKevin Cernekee 	if (alps_passthrough_mode_v3(psmouse, reg_base, false))
2207cd401204SKevin Cernekee 		ret = -EIO;
2208cd401204SKevin Cernekee 
2209cd401204SKevin Cernekee 	return ret;
2210cd401204SKevin Cernekee }
2211cd401204SKevin Cernekee 
2212cd401204SKevin Cernekee static int alps_hw_init_v3(struct psmouse *psmouse)
2213cd401204SKevin Cernekee {
22144b1af853SPali Rohár 	struct alps_data *priv = psmouse->private;
2215cd401204SKevin Cernekee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
2216cd401204SKevin Cernekee 	int reg_val;
2217cd401204SKevin Cernekee 	unsigned char param[4];
2218cd401204SKevin Cernekee 
22194b1af853SPali Rohár 	if ((priv->flags & ALPS_DUALPOINT) &&
2220cd401204SKevin Cernekee 	    alps_setup_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE) == -EIO)
2221cd401204SKevin Cernekee 		goto error;
2222cd401204SKevin Cernekee 
2223d18e53fcSKevin Cernekee 	if (alps_enter_command_mode(psmouse) ||
2224cd401204SKevin Cernekee 	    alps_absolute_mode_v3(psmouse)) {
222525bded7cSSeth Forshee 		psmouse_err(psmouse, "Failed to enter absolute mode\n");
222625bded7cSSeth Forshee 		goto error;
222725bded7cSSeth Forshee 	}
222825bded7cSSeth Forshee 
222925bded7cSSeth Forshee 	reg_val = alps_command_mode_read_reg(psmouse, 0x0006);
223025bded7cSSeth Forshee 	if (reg_val == -1)
223125bded7cSSeth Forshee 		goto error;
223225bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
223325bded7cSSeth Forshee 		goto error;
223425bded7cSSeth Forshee 
223525bded7cSSeth Forshee 	reg_val = alps_command_mode_read_reg(psmouse, 0x0007);
223625bded7cSSeth Forshee 	if (reg_val == -1)
223725bded7cSSeth Forshee 		goto error;
223825bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
223925bded7cSSeth Forshee 		goto error;
224025bded7cSSeth Forshee 
224125bded7cSSeth Forshee 	if (alps_command_mode_read_reg(psmouse, 0x0144) == -1)
224225bded7cSSeth Forshee 		goto error;
224325bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, 0x04))
224425bded7cSSeth Forshee 		goto error;
224525bded7cSSeth Forshee 
224625bded7cSSeth Forshee 	if (alps_command_mode_read_reg(psmouse, 0x0159) == -1)
224725bded7cSSeth Forshee 		goto error;
224825bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, 0x03))
224925bded7cSSeth Forshee 		goto error;
225025bded7cSSeth Forshee 
225125bded7cSSeth Forshee 	if (alps_command_mode_read_reg(psmouse, 0x0163) == -1)
225225bded7cSSeth Forshee 		goto error;
225325bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0163, 0x03))
225425bded7cSSeth Forshee 		goto error;
225525bded7cSSeth Forshee 
225625bded7cSSeth Forshee 	if (alps_command_mode_read_reg(psmouse, 0x0162) == -1)
225725bded7cSSeth Forshee 		goto error;
225825bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0162, 0x04))
225925bded7cSSeth Forshee 		goto error;
226025bded7cSSeth Forshee 
226125bded7cSSeth Forshee 	alps_exit_command_mode(psmouse);
226225bded7cSSeth Forshee 
226325bded7cSSeth Forshee 	/* Set rate and enable data reporting */
226425bded7cSSeth Forshee 	param[0] = 0x64;
226525bded7cSSeth Forshee 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
226625bded7cSSeth Forshee 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
226725bded7cSSeth Forshee 		psmouse_err(psmouse, "Failed to enable data reporting\n");
226825bded7cSSeth Forshee 		return -1;
226925bded7cSSeth Forshee 	}
227025bded7cSSeth Forshee 
227125bded7cSSeth Forshee 	return 0;
227225bded7cSSeth Forshee 
227325bded7cSSeth Forshee error:
227425bded7cSSeth Forshee 	/*
227525bded7cSSeth Forshee 	 * Leaving the touchpad in command mode will essentially render
227625bded7cSSeth Forshee 	 * it unusable until the machine reboots, so exit it here just
227725bded7cSSeth Forshee 	 * to be safe
227825bded7cSSeth Forshee 	 */
227925bded7cSSeth Forshee 	alps_exit_command_mode(psmouse);
228025bded7cSSeth Forshee 	return -1;
228125bded7cSSeth Forshee }
228225bded7cSSeth Forshee 
2283f3f33c67SHans de Goede static int alps_get_v3_v7_resolution(struct psmouse *psmouse, int reg_pitch)
2284f3f33c67SHans de Goede {
2285f3f33c67SHans de Goede 	int reg, x_pitch, y_pitch, x_electrode, y_electrode, x_phys, y_phys;
2286f3f33c67SHans de Goede 	struct alps_data *priv = psmouse->private;
2287f3f33c67SHans de Goede 
2288f3f33c67SHans de Goede 	reg = alps_command_mode_read_reg(psmouse, reg_pitch);
2289f3f33c67SHans de Goede 	if (reg < 0)
2290f3f33c67SHans de Goede 		return reg;
2291f3f33c67SHans de Goede 
2292f3f33c67SHans de Goede 	x_pitch = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
2293f3f33c67SHans de Goede 	x_pitch = 50 + 2 * x_pitch; /* In 0.1 mm units */
2294f3f33c67SHans de Goede 
2295f3f33c67SHans de Goede 	y_pitch = (char)reg >> 4; /* sign extend upper 4 bits */
2296f3f33c67SHans de Goede 	y_pitch = 36 + 2 * y_pitch; /* In 0.1 mm units */
2297f3f33c67SHans de Goede 
2298f3f33c67SHans de Goede 	reg = alps_command_mode_read_reg(psmouse, reg_pitch + 1);
2299f3f33c67SHans de Goede 	if (reg < 0)
2300f3f33c67SHans de Goede 		return reg;
2301f3f33c67SHans de Goede 
2302f3f33c67SHans de Goede 	x_electrode = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
2303f3f33c67SHans de Goede 	x_electrode = 17 + x_electrode;
2304f3f33c67SHans de Goede 
2305f3f33c67SHans de Goede 	y_electrode = (char)reg >> 4; /* sign extend upper 4 bits */
2306f3f33c67SHans de Goede 	y_electrode = 13 + y_electrode;
2307f3f33c67SHans de Goede 
2308f3f33c67SHans de Goede 	x_phys = x_pitch * (x_electrode - 1); /* In 0.1 mm units */
2309f3f33c67SHans de Goede 	y_phys = y_pitch * (y_electrode - 1); /* In 0.1 mm units */
2310f3f33c67SHans de Goede 
2311f3f33c67SHans de Goede 	priv->x_res = priv->x_max * 10 / x_phys; /* units / mm */
2312f3f33c67SHans de Goede 	priv->y_res = priv->y_max * 10 / y_phys; /* units / mm */
2313f3f33c67SHans de Goede 
2314f3f33c67SHans de Goede 	psmouse_dbg(psmouse,
2315f3f33c67SHans de Goede 		    "pitch %dx%d num-electrodes %dx%d physical size %dx%d mm res %dx%d\n",
2316f3f33c67SHans de Goede 		    x_pitch, y_pitch, x_electrode, y_electrode,
2317f3f33c67SHans de Goede 		    x_phys / 10, y_phys / 10, priv->x_res, priv->y_res);
2318f3f33c67SHans de Goede 
2319f3f33c67SHans de Goede 	return 0;
2320f3f33c67SHans de Goede }
2321f3f33c67SHans de Goede 
23221302bac3SKevin Cernekee static int alps_hw_init_rushmore_v3(struct psmouse *psmouse)
23231302bac3SKevin Cernekee {
2324cd401204SKevin Cernekee 	struct alps_data *priv = psmouse->private;
23251302bac3SKevin Cernekee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
23261302bac3SKevin Cernekee 	int reg_val, ret = -1;
23271302bac3SKevin Cernekee 
2328cd401204SKevin Cernekee 	if (priv->flags & ALPS_DUALPOINT) {
2329cd401204SKevin Cernekee 		reg_val = alps_setup_trackstick_v3(psmouse,
2330cd401204SKevin Cernekee 						   ALPS_REG_BASE_RUSHMORE);
2331cd401204SKevin Cernekee 		if (reg_val == -EIO)
2332cd401204SKevin Cernekee 			goto error;
2333cd401204SKevin Cernekee 	}
2334cd401204SKevin Cernekee 
2335d18e53fcSKevin Cernekee 	if (alps_enter_command_mode(psmouse) ||
23361302bac3SKevin Cernekee 	    alps_command_mode_read_reg(psmouse, 0xc2d9) == -1 ||
23371302bac3SKevin Cernekee 	    alps_command_mode_write_reg(psmouse, 0xc2cb, 0x00))
23381302bac3SKevin Cernekee 		goto error;
23391302bac3SKevin Cernekee 
2340f3f33c67SHans de Goede 	if (alps_get_v3_v7_resolution(psmouse, 0xc2da))
2341f3f33c67SHans de Goede 		goto error;
2342f3f33c67SHans de Goede 
23431302bac3SKevin Cernekee 	reg_val = alps_command_mode_read_reg(psmouse, 0xc2c6);
23441302bac3SKevin Cernekee 	if (reg_val == -1)
23451302bac3SKevin Cernekee 		goto error;
23461302bac3SKevin Cernekee 	if (__alps_command_mode_write_reg(psmouse, reg_val & 0xfd))
23471302bac3SKevin Cernekee 		goto error;
23481302bac3SKevin Cernekee 
23491302bac3SKevin Cernekee 	if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
23501302bac3SKevin Cernekee 		goto error;
23511302bac3SKevin Cernekee 
23521302bac3SKevin Cernekee 	/* enter absolute mode */
23531302bac3SKevin Cernekee 	reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
23541302bac3SKevin Cernekee 	if (reg_val == -1)
23551302bac3SKevin Cernekee 		goto error;
23561302bac3SKevin Cernekee 	if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
23571302bac3SKevin Cernekee 		goto error;
23581302bac3SKevin Cernekee 
23591302bac3SKevin Cernekee 	alps_exit_command_mode(psmouse);
23601302bac3SKevin Cernekee 	return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
23611302bac3SKevin Cernekee 
23621302bac3SKevin Cernekee error:
23631302bac3SKevin Cernekee 	alps_exit_command_mode(psmouse);
23641302bac3SKevin Cernekee 	return ret;
23651302bac3SKevin Cernekee }
23661302bac3SKevin Cernekee 
236725bded7cSSeth Forshee /* Must be in command mode when calling this function */
236825bded7cSSeth Forshee static int alps_absolute_mode_v4(struct psmouse *psmouse)
236925bded7cSSeth Forshee {
237025bded7cSSeth Forshee 	int reg_val;
237125bded7cSSeth Forshee 
237225bded7cSSeth Forshee 	reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
237325bded7cSSeth Forshee 	if (reg_val == -1)
237425bded7cSSeth Forshee 		return -1;
237525bded7cSSeth Forshee 
237625bded7cSSeth Forshee 	reg_val |= 0x02;
237725bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, reg_val))
237825bded7cSSeth Forshee 		return -1;
237925bded7cSSeth Forshee 
238025bded7cSSeth Forshee 	return 0;
238125bded7cSSeth Forshee }
238225bded7cSSeth Forshee 
238325bded7cSSeth Forshee static int alps_hw_init_v4(struct psmouse *psmouse)
238425bded7cSSeth Forshee {
238525bded7cSSeth Forshee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
238625bded7cSSeth Forshee 	unsigned char param[4];
238725bded7cSSeth Forshee 
2388d18e53fcSKevin Cernekee 	if (alps_enter_command_mode(psmouse))
238925bded7cSSeth Forshee 		goto error;
239025bded7cSSeth Forshee 
239125bded7cSSeth Forshee 	if (alps_absolute_mode_v4(psmouse)) {
239225bded7cSSeth Forshee 		psmouse_err(psmouse, "Failed to enter absolute mode\n");
239325bded7cSSeth Forshee 		goto error;
239425bded7cSSeth Forshee 	}
239525bded7cSSeth Forshee 
239625bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0007, 0x8c))
239725bded7cSSeth Forshee 		goto error;
239825bded7cSSeth Forshee 
239925bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0149, 0x03))
240025bded7cSSeth Forshee 		goto error;
240125bded7cSSeth Forshee 
240225bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0160, 0x03))
240325bded7cSSeth Forshee 		goto error;
240425bded7cSSeth Forshee 
240525bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x017f, 0x15))
240625bded7cSSeth Forshee 		goto error;
240725bded7cSSeth Forshee 
240825bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0151, 0x01))
240925bded7cSSeth Forshee 		goto error;
241025bded7cSSeth Forshee 
241125bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0168, 0x03))
241225bded7cSSeth Forshee 		goto error;
241325bded7cSSeth Forshee 
241425bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x014a, 0x03))
241525bded7cSSeth Forshee 		goto error;
241625bded7cSSeth Forshee 
241725bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0161, 0x03))
241825bded7cSSeth Forshee 		goto error;
241925bded7cSSeth Forshee 
242025bded7cSSeth Forshee 	alps_exit_command_mode(psmouse);
242125bded7cSSeth Forshee 
242225bded7cSSeth Forshee 	/*
242325bded7cSSeth Forshee 	 * This sequence changes the output from a 9-byte to an
242425bded7cSSeth Forshee 	 * 8-byte format. All the same data seems to be present,
242525bded7cSSeth Forshee 	 * just in a more compact format.
242625bded7cSSeth Forshee 	 */
242725bded7cSSeth Forshee 	param[0] = 0xc8;
242825bded7cSSeth Forshee 	param[1] = 0x64;
242925bded7cSSeth Forshee 	param[2] = 0x50;
243025bded7cSSeth Forshee 	if (ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
243125bded7cSSeth Forshee 	    ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE) ||
243225bded7cSSeth Forshee 	    ps2_command(ps2dev, &param[2], PSMOUSE_CMD_SETRATE) ||
243325bded7cSSeth Forshee 	    ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
243425bded7cSSeth Forshee 		return -1;
243525bded7cSSeth Forshee 
243625bded7cSSeth Forshee 	/* Set rate and enable data reporting */
243725bded7cSSeth Forshee 	param[0] = 0x64;
243825bded7cSSeth Forshee 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
243925bded7cSSeth Forshee 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
244025bded7cSSeth Forshee 		psmouse_err(psmouse, "Failed to enable data reporting\n");
244125bded7cSSeth Forshee 		return -1;
244225bded7cSSeth Forshee 	}
244325bded7cSSeth Forshee 
244425bded7cSSeth Forshee 	return 0;
244525bded7cSSeth Forshee 
244625bded7cSSeth Forshee error:
244725bded7cSSeth Forshee 	/*
244825bded7cSSeth Forshee 	 * Leaving the touchpad in command mode will essentially render
244925bded7cSSeth Forshee 	 * it unusable until the machine reboots, so exit it here just
245025bded7cSSeth Forshee 	 * to be safe
245125bded7cSSeth Forshee 	 */
245225bded7cSSeth Forshee 	alps_exit_command_mode(psmouse);
245325bded7cSSeth Forshee 	return -1;
245425bded7cSSeth Forshee }
245525bded7cSSeth Forshee 
24563db5b9f7SMasaki Ota static int alps_get_otp_values_ss4_v2(struct psmouse *psmouse,
24573db5b9f7SMasaki Ota 				      unsigned char index, unsigned char otp[])
24583db5b9f7SMasaki Ota {
24593db5b9f7SMasaki Ota 	struct ps2dev *ps2dev = &psmouse->ps2dev;
24603db5b9f7SMasaki Ota 
24613db5b9f7SMasaki Ota 	switch (index) {
24623db5b9f7SMasaki Ota 	case 0:
24633db5b9f7SMasaki Ota 		if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)  ||
24643db5b9f7SMasaki Ota 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)  ||
24653db5b9f7SMasaki Ota 		    ps2_command(ps2dev, otp, PSMOUSE_CMD_GETINFO))
24663db5b9f7SMasaki Ota 			return -1;
24673db5b9f7SMasaki Ota 
24683db5b9f7SMasaki Ota 		break;
24693db5b9f7SMasaki Ota 
24703db5b9f7SMasaki Ota 	case 1:
24713db5b9f7SMasaki Ota 		if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL)  ||
24723db5b9f7SMasaki Ota 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL)  ||
24733db5b9f7SMasaki Ota 		    ps2_command(ps2dev, otp, PSMOUSE_CMD_GETINFO))
24743db5b9f7SMasaki Ota 			return -1;
24753db5b9f7SMasaki Ota 
24763db5b9f7SMasaki Ota 		break;
24773db5b9f7SMasaki Ota 	}
24783db5b9f7SMasaki Ota 
24793db5b9f7SMasaki Ota 	return 0;
24803db5b9f7SMasaki Ota }
24813db5b9f7SMasaki Ota 
248207f19e3dSFengguang Wu static int alps_update_device_area_ss4_v2(unsigned char otp[][4],
24833db5b9f7SMasaki Ota 					  struct alps_data *priv)
24843db5b9f7SMasaki Ota {
24853db5b9f7SMasaki Ota 	int num_x_electrode;
24863db5b9f7SMasaki Ota 	int num_y_electrode;
24873db5b9f7SMasaki Ota 	int x_pitch, y_pitch, x_phys, y_phys;
24883db5b9f7SMasaki Ota 
2489e7348396SMasaki Ota 	if (IS_SS4PLUS_DEV(priv->dev_id)) {
2490e7348396SMasaki Ota 		num_x_electrode =
2491e7348396SMasaki Ota 			SS4PLUS_NUMSENSOR_XOFFSET + (otp[0][2] & 0x0F);
2492e7348396SMasaki Ota 		num_y_electrode =
2493e7348396SMasaki Ota 			SS4PLUS_NUMSENSOR_YOFFSET + ((otp[0][2] >> 4) & 0x0F);
24943db5b9f7SMasaki Ota 
2495e7348396SMasaki Ota 		priv->x_max =
2496e7348396SMasaki Ota 			(num_x_electrode - 1) * SS4PLUS_COUNT_PER_ELECTRODE;
2497e7348396SMasaki Ota 		priv->y_max =
2498e7348396SMasaki Ota 			(num_y_electrode - 1) * SS4PLUS_COUNT_PER_ELECTRODE;
2499e7348396SMasaki Ota 
2500e7348396SMasaki Ota 		x_pitch = (otp[0][1] & 0x0F) + SS4PLUS_MIN_PITCH_MM;
2501e7348396SMasaki Ota 		y_pitch = ((otp[0][1] >> 4) & 0x0F) + SS4PLUS_MIN_PITCH_MM;
2502e7348396SMasaki Ota 
2503e7348396SMasaki Ota 	} else {
2504e7348396SMasaki Ota 		num_x_electrode =
2505e7348396SMasaki Ota 			SS4_NUMSENSOR_XOFFSET + (otp[1][0] & 0x0F);
2506e7348396SMasaki Ota 		num_y_electrode =
2507e7348396SMasaki Ota 			SS4_NUMSENSOR_YOFFSET + ((otp[1][0] >> 4) & 0x0F);
2508e7348396SMasaki Ota 
2509e7348396SMasaki Ota 		priv->x_max =
2510e7348396SMasaki Ota 			(num_x_electrode - 1) * SS4_COUNT_PER_ELECTRODE;
2511e7348396SMasaki Ota 		priv->y_max =
2512e7348396SMasaki Ota 			(num_y_electrode - 1) * SS4_COUNT_PER_ELECTRODE;
25133db5b9f7SMasaki Ota 
25143db5b9f7SMasaki Ota 		x_pitch = ((otp[1][2] >> 2) & 0x07) + SS4_MIN_PITCH_MM;
25153db5b9f7SMasaki Ota 		y_pitch = ((otp[1][2] >> 5) & 0x07) + SS4_MIN_PITCH_MM;
2516e7348396SMasaki Ota 	}
25173db5b9f7SMasaki Ota 
25183db5b9f7SMasaki Ota 	x_phys = x_pitch * (num_x_electrode - 1); /* In 0.1 mm units */
25193db5b9f7SMasaki Ota 	y_phys = y_pitch * (num_y_electrode - 1); /* In 0.1 mm units */
25203db5b9f7SMasaki Ota 
25213db5b9f7SMasaki Ota 	priv->x_res = priv->x_max * 10 / x_phys; /* units / mm */
25223db5b9f7SMasaki Ota 	priv->y_res = priv->y_max * 10 / y_phys; /* units / mm */
25233db5b9f7SMasaki Ota 
25243db5b9f7SMasaki Ota 	return 0;
25253db5b9f7SMasaki Ota }
25263db5b9f7SMasaki Ota 
252707f19e3dSFengguang Wu static int alps_update_btn_info_ss4_v2(unsigned char otp[][4],
252807f19e3dSFengguang Wu 				       struct alps_data *priv)
25293db5b9f7SMasaki Ota {
25303db5b9f7SMasaki Ota 	unsigned char is_btnless;
25313db5b9f7SMasaki Ota 
2532e7348396SMasaki Ota 	if (IS_SS4PLUS_DEV(priv->dev_id))
2533e7348396SMasaki Ota 		is_btnless = (otp[1][0] >> 1) & 0x01;
2534e7348396SMasaki Ota 	else
25353db5b9f7SMasaki Ota 		is_btnless = (otp[1][1] >> 3) & 0x01;
25363db5b9f7SMasaki Ota 
25373db5b9f7SMasaki Ota 	if (is_btnless)
25383db5b9f7SMasaki Ota 		priv->flags |= ALPS_BUTTONPAD;
25393db5b9f7SMasaki Ota 
25403db5b9f7SMasaki Ota 	return 0;
25413db5b9f7SMasaki Ota }
25423db5b9f7SMasaki Ota 
2543e7348396SMasaki Ota static int alps_update_dual_info_ss4_v2(unsigned char otp[][4],
2544e7348396SMasaki Ota 				       struct alps_data *priv)
2545e7348396SMasaki Ota {
2546e7348396SMasaki Ota 	bool is_dual = false;
2547e7348396SMasaki Ota 
2548e7348396SMasaki Ota 	if (IS_SS4PLUS_DEV(priv->dev_id))
2549e7348396SMasaki Ota 		is_dual = (otp[0][0] >> 4) & 0x01;
2550e7348396SMasaki Ota 
2551e7348396SMasaki Ota 	if (is_dual)
2552e7348396SMasaki Ota 		priv->flags |= ALPS_DUALPOINT |
2553e7348396SMasaki Ota 					ALPS_DUALPOINT_WITH_PRESSURE;
2554e7348396SMasaki Ota 
2555e7348396SMasaki Ota 	return 0;
2556e7348396SMasaki Ota }
2557e7348396SMasaki Ota 
25583db5b9f7SMasaki Ota static int alps_set_defaults_ss4_v2(struct psmouse *psmouse,
25593db5b9f7SMasaki Ota 				    struct alps_data *priv)
25603db5b9f7SMasaki Ota {
25613db5b9f7SMasaki Ota 	unsigned char otp[2][4];
25623db5b9f7SMasaki Ota 
25633db5b9f7SMasaki Ota 	memset(otp, 0, sizeof(otp));
25643db5b9f7SMasaki Ota 
25654a646580SMasaki Ota 	if (alps_get_otp_values_ss4_v2(psmouse, 1, &otp[1][0]) ||
25664a646580SMasaki Ota 	    alps_get_otp_values_ss4_v2(psmouse, 0, &otp[0][0]))
25673db5b9f7SMasaki Ota 		return -1;
25683db5b9f7SMasaki Ota 
25693db5b9f7SMasaki Ota 	alps_update_device_area_ss4_v2(otp, priv);
25703db5b9f7SMasaki Ota 
25713db5b9f7SMasaki Ota 	alps_update_btn_info_ss4_v2(otp, priv);
25723db5b9f7SMasaki Ota 
2573e7348396SMasaki Ota 	alps_update_dual_info_ss4_v2(otp, priv);
2574e7348396SMasaki Ota 
25753db5b9f7SMasaki Ota 	return 0;
25763db5b9f7SMasaki Ota }
25773db5b9f7SMasaki Ota 
2578ee65d4b3SYunkang Tang static int alps_dolphin_get_device_area(struct psmouse *psmouse,
2579ee65d4b3SYunkang Tang 					struct alps_data *priv)
2580ee65d4b3SYunkang Tang {
2581ee65d4b3SYunkang Tang 	struct ps2dev *ps2dev = &psmouse->ps2dev;
2582ee65d4b3SYunkang Tang 	unsigned char param[4] = {0};
2583ee65d4b3SYunkang Tang 	int num_x_electrode, num_y_electrode;
2584ee65d4b3SYunkang Tang 
2585ee65d4b3SYunkang Tang 	if (alps_enter_command_mode(psmouse))
2586ee65d4b3SYunkang Tang 		return -1;
2587ee65d4b3SYunkang Tang 
2588ee65d4b3SYunkang Tang 	param[0] = 0x0a;
2589ee65d4b3SYunkang Tang 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
2590ee65d4b3SYunkang Tang 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2591ee65d4b3SYunkang Tang 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2592ee65d4b3SYunkang Tang 	    ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2593ee65d4b3SYunkang Tang 	    ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE))
2594ee65d4b3SYunkang Tang 		return -1;
2595ee65d4b3SYunkang Tang 
2596ee65d4b3SYunkang Tang 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
2597ee65d4b3SYunkang Tang 		return -1;
2598ee65d4b3SYunkang Tang 
2599ee65d4b3SYunkang Tang 	/*
2600ee65d4b3SYunkang Tang 	 * Dolphin's sensor line number is not fixed. It can be calculated
2601ee65d4b3SYunkang Tang 	 * by adding the device's register value with DOLPHIN_PROFILE_X/YOFFSET.
2602ee65d4b3SYunkang Tang 	 * Further more, we can get device's x_max and y_max by multiplying
2603ee65d4b3SYunkang Tang 	 * sensor line number with DOLPHIN_COUNT_PER_ELECTRODE.
2604ee65d4b3SYunkang Tang 	 *
2605ee65d4b3SYunkang Tang 	 * e.g. When we get register's sensor_x = 11 & sensor_y = 8,
2606ee65d4b3SYunkang Tang 	 *	real sensor line number X = 11 + 8 = 19, and
2607ee65d4b3SYunkang Tang 	 *	real sensor line number Y = 8 + 1 = 9.
2608ee65d4b3SYunkang Tang 	 *	So, x_max = (19 - 1) * 64 = 1152, and
2609ee65d4b3SYunkang Tang 	 *	    y_max = (9 - 1) * 64 = 512.
2610ee65d4b3SYunkang Tang 	 */
2611ee65d4b3SYunkang Tang 	num_x_electrode = DOLPHIN_PROFILE_XOFFSET + (param[2] & 0x0F);
2612ee65d4b3SYunkang Tang 	num_y_electrode = DOLPHIN_PROFILE_YOFFSET + ((param[2] >> 4) & 0x0F);
2613ee65d4b3SYunkang Tang 	priv->x_bits = num_x_electrode;
2614ee65d4b3SYunkang Tang 	priv->y_bits = num_y_electrode;
2615ee65d4b3SYunkang Tang 	priv->x_max = (num_x_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
2616ee65d4b3SYunkang Tang 	priv->y_max = (num_y_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
2617ee65d4b3SYunkang Tang 
2618ee65d4b3SYunkang Tang 	if (alps_exit_command_mode(psmouse))
2619ee65d4b3SYunkang Tang 		return -1;
2620ee65d4b3SYunkang Tang 
2621ee65d4b3SYunkang Tang 	return 0;
2622ee65d4b3SYunkang Tang }
2623ee65d4b3SYunkang Tang 
262475af9e56SDave Turvene static int alps_hw_init_dolphin_v1(struct psmouse *psmouse)
262575af9e56SDave Turvene {
262675af9e56SDave Turvene 	struct ps2dev *ps2dev = &psmouse->ps2dev;
262775af9e56SDave Turvene 	unsigned char param[2];
262875af9e56SDave Turvene 
262975af9e56SDave Turvene 	/* This is dolphin "v1" as empirically defined by florin9doi */
263075af9e56SDave Turvene 	param[0] = 0x64;
263175af9e56SDave Turvene 	param[1] = 0x28;
263275af9e56SDave Turvene 
263375af9e56SDave Turvene 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
263475af9e56SDave Turvene 	    ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
263575af9e56SDave Turvene 	    ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
263675af9e56SDave Turvene 		return -1;
263775af9e56SDave Turvene 
263875af9e56SDave Turvene 	return 0;
263975af9e56SDave Turvene }
264075af9e56SDave Turvene 
26413808843cSYunkang Tang static int alps_hw_init_v7(struct psmouse *psmouse)
26423808843cSYunkang Tang {
26433808843cSYunkang Tang 	struct ps2dev *ps2dev = &psmouse->ps2dev;
26443808843cSYunkang Tang 	int reg_val, ret = -1;
26453808843cSYunkang Tang 
26463808843cSYunkang Tang 	if (alps_enter_command_mode(psmouse) ||
26473808843cSYunkang Tang 	    alps_command_mode_read_reg(psmouse, 0xc2d9) == -1)
26483808843cSYunkang Tang 		goto error;
26493808843cSYunkang Tang 
2650f3f33c67SHans de Goede 	if (alps_get_v3_v7_resolution(psmouse, 0xc397))
2651f3f33c67SHans de Goede 		goto error;
2652f3f33c67SHans de Goede 
26533808843cSYunkang Tang 	if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
26543808843cSYunkang Tang 		goto error;
26553808843cSYunkang Tang 
26563808843cSYunkang Tang 	reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
26573808843cSYunkang Tang 	if (reg_val == -1)
26583808843cSYunkang Tang 		goto error;
26593808843cSYunkang Tang 	if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
26603808843cSYunkang Tang 		goto error;
26613808843cSYunkang Tang 
26623808843cSYunkang Tang 	alps_exit_command_mode(psmouse);
26633808843cSYunkang Tang 	return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
26643808843cSYunkang Tang 
26653808843cSYunkang Tang error:
26663808843cSYunkang Tang 	alps_exit_command_mode(psmouse);
26673808843cSYunkang Tang 	return ret;
26683808843cSYunkang Tang }
26693808843cSYunkang Tang 
26703db5b9f7SMasaki Ota static int alps_hw_init_ss4_v2(struct psmouse *psmouse)
26713db5b9f7SMasaki Ota {
26723db5b9f7SMasaki Ota 	struct ps2dev *ps2dev = &psmouse->ps2dev;
26733db5b9f7SMasaki Ota 	char param[2] = {0x64, 0x28};
26743db5b9f7SMasaki Ota 	int ret = -1;
26753db5b9f7SMasaki Ota 
26763db5b9f7SMasaki Ota 	/* enter absolute mode */
26773db5b9f7SMasaki Ota 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
26783db5b9f7SMasaki Ota 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
26793db5b9f7SMasaki Ota 	    ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
26803db5b9f7SMasaki Ota 	    ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE)) {
26813db5b9f7SMasaki Ota 		goto error;
26823db5b9f7SMasaki Ota 	}
26833db5b9f7SMasaki Ota 
26843db5b9f7SMasaki Ota 	/* T.B.D. Decread noise packet number, delete in the future */
26853db5b9f7SMasaki Ota 	if (alps_exit_command_mode(psmouse) ||
26863db5b9f7SMasaki Ota 	    alps_enter_command_mode(psmouse) ||
26873db5b9f7SMasaki Ota 	    alps_command_mode_write_reg(psmouse, 0x001D, 0x20)) {
26883db5b9f7SMasaki Ota 		goto error;
26893db5b9f7SMasaki Ota 	}
26903db5b9f7SMasaki Ota 	alps_exit_command_mode(psmouse);
26913db5b9f7SMasaki Ota 
26923db5b9f7SMasaki Ota 	return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
26933db5b9f7SMasaki Ota 
26943db5b9f7SMasaki Ota error:
26953db5b9f7SMasaki Ota 	alps_exit_command_mode(psmouse);
26963db5b9f7SMasaki Ota 	return ret;
26973db5b9f7SMasaki Ota }
26983db5b9f7SMasaki Ota 
26993296f71cSDmitry Torokhov static int alps_set_protocol(struct psmouse *psmouse,
27003296f71cSDmitry Torokhov 			     struct alps_data *priv,
27013296f71cSDmitry Torokhov 			     const struct alps_protocol_info *protocol)
270225bded7cSSeth Forshee {
27033296f71cSDmitry Torokhov 	psmouse->private = priv;
27043296f71cSDmitry Torokhov 
27053296f71cSDmitry Torokhov 	setup_timer(&priv->timer, alps_flush_packet, (unsigned long)psmouse);
27063296f71cSDmitry Torokhov 
27073296f71cSDmitry Torokhov 	priv->proto_version = protocol->version;
27083296f71cSDmitry Torokhov 	priv->byte0 = protocol->byte0;
27093296f71cSDmitry Torokhov 	priv->mask0 = protocol->mask0;
27103296f71cSDmitry Torokhov 	priv->flags = protocol->flags;
2711f673ceb1SKevin Cernekee 
27127a9f73e7SKevin Cernekee 	priv->x_max = 2000;
27137a9f73e7SKevin Cernekee 	priv->y_max = 1400;
27147a9f73e7SKevin Cernekee 	priv->x_bits = 15;
27157a9f73e7SKevin Cernekee 	priv->y_bits = 11;
27167a9f73e7SKevin Cernekee 
271799df65e7SKevin Cernekee 	switch (priv->proto_version) {
271825bded7cSSeth Forshee 	case ALPS_PROTO_V1:
271925bded7cSSeth Forshee 	case ALPS_PROTO_V2:
272024af5cb9SKevin Cernekee 		priv->hw_init = alps_hw_init_v1_v2;
272124af5cb9SKevin Cernekee 		priv->process_packet = alps_process_packet_v1_v2;
272224af5cb9SKevin Cernekee 		priv->set_abs_params = alps_set_abs_params_st;
272395f75e91SYunkang Tang 		priv->x_max = 1023;
272495f75e91SYunkang Tang 		priv->y_max = 767;
272519556219SHans de Goede 		if (dmi_check_system(alps_dmi_has_separate_stick_buttons))
272619556219SHans de Goede 			priv->flags |= ALPS_STICK_BITS;
272725bded7cSSeth Forshee 		break;
2728fb2dd7a6SDmitry Torokhov 
272925bded7cSSeth Forshee 	case ALPS_PROTO_V3:
273024af5cb9SKevin Cernekee 		priv->hw_init = alps_hw_init_v3;
273124af5cb9SKevin Cernekee 		priv->process_packet = alps_process_packet_v3;
2732688ea364SHans de Goede 		priv->set_abs_params = alps_set_abs_params_semi_mt;
2733f85e5001SKevin Cernekee 		priv->decode_fields = alps_decode_pinnacle;
273450e8b216SKevin Cernekee 		priv->nibble_commands = alps_v3_nibble_commands;
273550e8b216SKevin Cernekee 		priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
27364b1af853SPali Rohár 
27374b1af853SPali Rohár 		if (alps_probe_trackstick_v3_v7(psmouse,
27384b1af853SPali Rohár 						ALPS_REG_BASE_PINNACLE) < 0)
27394b1af853SPali Rohár 			priv->flags &= ~ALPS_DUALPOINT;
27404b1af853SPali Rohár 
274125bded7cSSeth Forshee 		break;
2742fb2dd7a6SDmitry Torokhov 
2743fb2dd7a6SDmitry Torokhov 	case ALPS_PROTO_V3_RUSHMORE:
2744fb2dd7a6SDmitry Torokhov 		priv->hw_init = alps_hw_init_rushmore_v3;
2745fb2dd7a6SDmitry Torokhov 		priv->process_packet = alps_process_packet_v3;
2746688ea364SHans de Goede 		priv->set_abs_params = alps_set_abs_params_semi_mt;
2747fb2dd7a6SDmitry Torokhov 		priv->decode_fields = alps_decode_rushmore;
2748fb2dd7a6SDmitry Torokhov 		priv->nibble_commands = alps_v3_nibble_commands;
2749fb2dd7a6SDmitry Torokhov 		priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2750fb2dd7a6SDmitry Torokhov 		priv->x_bits = 16;
2751fb2dd7a6SDmitry Torokhov 		priv->y_bits = 12;
27523296f71cSDmitry Torokhov 
2753dae928ecSPali Rohár 		if (alps_probe_trackstick_v3_v7(psmouse,
27543296f71cSDmitry Torokhov 						ALPS_REG_BASE_RUSHMORE) < 0)
27553296f71cSDmitry Torokhov 			priv->flags &= ~ALPS_DUALPOINT;
27563296f71cSDmitry Torokhov 
2757fb2dd7a6SDmitry Torokhov 		break;
2758fb2dd7a6SDmitry Torokhov 
275925bded7cSSeth Forshee 	case ALPS_PROTO_V4:
276024af5cb9SKevin Cernekee 		priv->hw_init = alps_hw_init_v4;
276124af5cb9SKevin Cernekee 		priv->process_packet = alps_process_packet_v4;
2762688ea364SHans de Goede 		priv->set_abs_params = alps_set_abs_params_semi_mt;
276350e8b216SKevin Cernekee 		priv->nibble_commands = alps_v4_nibble_commands;
276450e8b216SKevin Cernekee 		priv->addr_command = PSMOUSE_CMD_DISABLE;
276525bded7cSSeth Forshee 		break;
27663296f71cSDmitry Torokhov 
276775af9e56SDave Turvene 	case ALPS_PROTO_V5:
276875af9e56SDave Turvene 		priv->hw_init = alps_hw_init_dolphin_v1;
2769ee65d4b3SYunkang Tang 		priv->process_packet = alps_process_touchpad_packet_v3_v5;
277075af9e56SDave Turvene 		priv->decode_fields = alps_decode_dolphin;
2771688ea364SHans de Goede 		priv->set_abs_params = alps_set_abs_params_semi_mt;
277275af9e56SDave Turvene 		priv->nibble_commands = alps_v3_nibble_commands;
277375af9e56SDave Turvene 		priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
277475af9e56SDave Turvene 		priv->x_bits = 23;
277575af9e56SDave Turvene 		priv->y_bits = 12;
2776c164c147SDmitry Torokhov 
2777c164c147SDmitry Torokhov 		if (alps_dolphin_get_device_area(psmouse, priv))
2778c164c147SDmitry Torokhov 			return -EIO;
2779c164c147SDmitry Torokhov 
278075af9e56SDave Turvene 		break;
27813296f71cSDmitry Torokhov 
278295f75e91SYunkang Tang 	case ALPS_PROTO_V6:
278395f75e91SYunkang Tang 		priv->hw_init = alps_hw_init_v6;
278495f75e91SYunkang Tang 		priv->process_packet = alps_process_packet_v6;
278595f75e91SYunkang Tang 		priv->set_abs_params = alps_set_abs_params_st;
278695f75e91SYunkang Tang 		priv->nibble_commands = alps_v6_nibble_commands;
278795f75e91SYunkang Tang 		priv->x_max = 2047;
278895f75e91SYunkang Tang 		priv->y_max = 1535;
278995f75e91SYunkang Tang 		break;
27903296f71cSDmitry Torokhov 
27913808843cSYunkang Tang 	case ALPS_PROTO_V7:
27923808843cSYunkang Tang 		priv->hw_init = alps_hw_init_v7;
27933808843cSYunkang Tang 		priv->process_packet = alps_process_packet_v7;
27943808843cSYunkang Tang 		priv->decode_fields = alps_decode_packet_v7;
27958eccd393SMasaki Ota 		priv->set_abs_params = alps_set_abs_params_v7;
27963808843cSYunkang Tang 		priv->nibble_commands = alps_v3_nibble_commands;
27973808843cSYunkang Tang 		priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2798c164c147SDmitry Torokhov 		priv->x_max = 0xfff;
2799c164c147SDmitry Torokhov 		priv->y_max = 0x7ff;
28003808843cSYunkang Tang 
28013808843cSYunkang Tang 		if (priv->fw_ver[1] != 0xba)
28023808843cSYunkang Tang 			priv->flags |= ALPS_BUTTONPAD;
28033296f71cSDmitry Torokhov 
2804dae928ecSPali Rohár 		if (alps_probe_trackstick_v3_v7(psmouse, ALPS_REG_BASE_V7) < 0)
2805dae928ecSPali Rohár 			priv->flags &= ~ALPS_DUALPOINT;
2806dae928ecSPali Rohár 
28073808843cSYunkang Tang 		break;
28083db5b9f7SMasaki Ota 
28093db5b9f7SMasaki Ota 	case ALPS_PROTO_V8:
28103db5b9f7SMasaki Ota 		priv->hw_init = alps_hw_init_ss4_v2;
28113db5b9f7SMasaki Ota 		priv->process_packet = alps_process_packet_ss4_v2;
28123db5b9f7SMasaki Ota 		priv->decode_fields = alps_decode_ss4_v2;
28133db5b9f7SMasaki Ota 		priv->set_abs_params = alps_set_abs_params_ss4_v2;
28143db5b9f7SMasaki Ota 		priv->nibble_commands = alps_v3_nibble_commands;
28153db5b9f7SMasaki Ota 		priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
28163db5b9f7SMasaki Ota 
28173db5b9f7SMasaki Ota 		if (alps_set_defaults_ss4_v2(psmouse, priv))
28183db5b9f7SMasaki Ota 			return -EIO;
28193db5b9f7SMasaki Ota 
28203db5b9f7SMasaki Ota 		break;
282125bded7cSSeth Forshee 	}
28223296f71cSDmitry Torokhov 
28233296f71cSDmitry Torokhov 	return 0;
282425bded7cSSeth Forshee }
282525bded7cSSeth Forshee 
28263296f71cSDmitry Torokhov static const struct alps_protocol_info *alps_match_table(unsigned char *e7,
28273296f71cSDmitry Torokhov 							 unsigned char *ec)
28282e992cc0SKevin Cernekee {
2829b5d6b851SKevin Cernekee 	const struct alps_model_info *model;
28302e992cc0SKevin Cernekee 	int i;
28312e992cc0SKevin Cernekee 
2832b5d6b851SKevin Cernekee 	for (i = 0; i < ARRAY_SIZE(alps_model_data); i++) {
2833b5d6b851SKevin Cernekee 		model = &alps_model_data[i];
2834b5d6b851SKevin Cernekee 
2835a3cbfd56SPali Rohár 		if (!memcmp(e7, model->signature, sizeof(model->signature)))
28363296f71cSDmitry Torokhov 			return &model->protocol_info;
2837b5d6b851SKevin Cernekee 	}
2838b5d6b851SKevin Cernekee 
28393296f71cSDmitry Torokhov 	return NULL;
2840b5d6b851SKevin Cernekee }
2841b5d6b851SKevin Cernekee 
2842b5d6b851SKevin Cernekee static int alps_identify(struct psmouse *psmouse, struct alps_data *priv)
2843b5d6b851SKevin Cernekee {
28443296f71cSDmitry Torokhov 	const struct alps_protocol_info *protocol;
2845b5d6b851SKevin Cernekee 	unsigned char e6[4], e7[4], ec[4];
2846a09221e8SDmitry Torokhov 	int error;
2847b5d6b851SKevin Cernekee 
28482e992cc0SKevin Cernekee 	/*
28492e992cc0SKevin Cernekee 	 * First try "E6 report".
28502e992cc0SKevin Cernekee 	 * ALPS should return 0,0,10 or 0,0,100 if no buttons are pressed.
28512e992cc0SKevin Cernekee 	 * The bits 0-2 of the first byte will be 1s if some buttons are
28522e992cc0SKevin Cernekee 	 * pressed.
28532e992cc0SKevin Cernekee 	 */
2854b5d6b851SKevin Cernekee 	if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2855b5d6b851SKevin Cernekee 			 PSMOUSE_CMD_SETSCALE11, e6))
2856b5d6b851SKevin Cernekee 		return -EIO;
28572e992cc0SKevin Cernekee 
2858b5d6b851SKevin Cernekee 	if ((e6[0] & 0xf8) != 0 || e6[1] != 0 || (e6[2] != 10 && e6[2] != 100))
2859b5d6b851SKevin Cernekee 		return -EINVAL;
28602e992cc0SKevin Cernekee 
28612e992cc0SKevin Cernekee 	/*
2862b5d6b851SKevin Cernekee 	 * Now get the "E7" and "EC" reports.  These will uniquely identify
2863b5d6b851SKevin Cernekee 	 * most ALPS touchpads.
28642e992cc0SKevin Cernekee 	 */
2865b5d6b851SKevin Cernekee 	if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2866b5d6b851SKevin Cernekee 			 PSMOUSE_CMD_SETSCALE21, e7) ||
2867b5d6b851SKevin Cernekee 	    alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2868b5d6b851SKevin Cernekee 			 PSMOUSE_CMD_RESET_WRAP, ec) ||
2869b5d6b851SKevin Cernekee 	    alps_exit_command_mode(psmouse))
2870b5d6b851SKevin Cernekee 		return -EIO;
28712e992cc0SKevin Cernekee 
28723296f71cSDmitry Torokhov 	protocol = alps_match_table(e7, ec);
28733296f71cSDmitry Torokhov 	if (!protocol) {
287409c398bcSPali Rohár 		if (e7[0] == 0x73 && e7[1] == 0x02 && e7[2] == 0x64 &&
287509c398bcSPali Rohár 			   ec[2] == 0x8a) {
287609c398bcSPali Rohár 			protocol = &alps_v4_protocol_data;
287709c398bcSPali Rohár 		} else if (e7[0] == 0x73 && e7[1] == 0x03 && e7[2] == 0x50 &&
28783296f71cSDmitry Torokhov 			   ec[0] == 0x73 && (ec[1] == 0x01 || ec[1] == 0x02)) {
28793296f71cSDmitry Torokhov 			protocol = &alps_v5_protocol_data;
28803296f71cSDmitry Torokhov 		} else if (ec[0] == 0x88 &&
28813296f71cSDmitry Torokhov 			   ((ec[1] & 0xf0) == 0xb0 || (ec[1] & 0xf0) == 0xc0)) {
28823296f71cSDmitry Torokhov 			protocol = &alps_v7_protocol_data;
28833296f71cSDmitry Torokhov 		} else if (ec[0] == 0x88 && ec[1] == 0x08) {
28843296f71cSDmitry Torokhov 			protocol = &alps_v3_rushmore_data;
28853296f71cSDmitry Torokhov 		} else if (ec[0] == 0x88 && ec[1] == 0x07 &&
28863296f71cSDmitry Torokhov 			   ec[2] >= 0x90 && ec[2] <= 0x9d) {
28873296f71cSDmitry Torokhov 			protocol = &alps_v3_protocol_data;
28883db5b9f7SMasaki Ota 		} else if (e7[0] == 0x73 && e7[1] == 0x03 &&
2889e7348396SMasaki Ota 			   (e7[2] == 0x14 || e7[2] == 0x28)) {
2890aeaa881fSBen Gamari 			protocol = &alps_v8_protocol_data;
2891c9815232SPali Rohár 		} else if (e7[0] == 0x73 && e7[1] == 0x03 && e7[2] == 0xc8) {
2892c9815232SPali Rohár 			protocol = &alps_v9_protocol_data;
2893c9815232SPali Rohár 			psmouse_warn(psmouse,
2894c9815232SPali Rohár 				     "Unsupported ALPS V9 touchpad: E7=%3ph, EC=%3ph\n",
2895c9815232SPali Rohár 				     e7, ec);
2896c9815232SPali Rohár 			return -EINVAL;
28973296f71cSDmitry Torokhov 		} else {
28983296f71cSDmitry Torokhov 			psmouse_dbg(psmouse,
28993296f71cSDmitry Torokhov 				    "Likely not an ALPS touchpad: E7=%3ph, EC=%3ph\n", e7, ec);
29003296f71cSDmitry Torokhov 			return -EINVAL;
29013296f71cSDmitry Torokhov 		}
29023296f71cSDmitry Torokhov 	}
29033296f71cSDmitry Torokhov 
2904a09221e8SDmitry Torokhov 	if (priv) {
2905e7348396SMasaki Ota 		/* Save Device ID and Firmware version */
2906e7348396SMasaki Ota 		memcpy(priv->dev_id, e7, 3);
2907c0cd17f6SHans de Goede 		memcpy(priv->fw_ver, ec, 3);
2908a09221e8SDmitry Torokhov 		error = alps_set_protocol(psmouse, priv, protocol);
2909a09221e8SDmitry Torokhov 		if (error)
2910a09221e8SDmitry Torokhov 			return error;
2911a09221e8SDmitry Torokhov 	}
2912c0cd17f6SHans de Goede 
2913a09221e8SDmitry Torokhov 	return 0;
29142e992cc0SKevin Cernekee }
29152e992cc0SKevin Cernekee 
29161e0c5b12SDmitry Torokhov static int alps_reconnect(struct psmouse *psmouse)
29171e0c5b12SDmitry Torokhov {
2918b5d6b851SKevin Cernekee 	struct alps_data *priv = psmouse->private;
291971bb21b6SMaxim Levitsky 
29201e0c5b12SDmitry Torokhov 	psmouse_reset(psmouse);
29211e0c5b12SDmitry Torokhov 
2922b5d6b851SKevin Cernekee 	if (alps_identify(psmouse, priv) < 0)
29231e0c5b12SDmitry Torokhov 		return -1;
29241e0c5b12SDmitry Torokhov 
292524af5cb9SKevin Cernekee 	return priv->hw_init(psmouse);
29261da177e4SLinus Torvalds }
29271da177e4SLinus Torvalds 
29281da177e4SLinus Torvalds static void alps_disconnect(struct psmouse *psmouse)
29291da177e4SLinus Torvalds {
29301da177e4SLinus Torvalds 	struct alps_data *priv = psmouse->private;
29312e5b636bSDmitry Torokhov 
29321da177e4SLinus Torvalds 	psmouse_reset(psmouse);
29331d9f2626SSebastian Kapfer 	del_timer_sync(&priv->timer);
293404aae283SPali Rohár 	if (priv->dev2)
29352e5b636bSDmitry Torokhov 		input_unregister_device(priv->dev2);
293604aae283SPali Rohár 	if (!IS_ERR_OR_NULL(priv->dev3))
293704aae283SPali Rohár 		input_unregister_device(priv->dev3);
29381da177e4SLinus Torvalds 	kfree(priv);
29391da177e4SLinus Torvalds }
29401da177e4SLinus Torvalds 
294124af5cb9SKevin Cernekee static void alps_set_abs_params_st(struct alps_data *priv,
294224af5cb9SKevin Cernekee 				   struct input_dev *dev1)
294324af5cb9SKevin Cernekee {
294495f75e91SYunkang Tang 	input_set_abs_params(dev1, ABS_X, 0, priv->x_max, 0, 0);
294595f75e91SYunkang Tang 	input_set_abs_params(dev1, ABS_Y, 0, priv->y_max, 0, 0);
29468eccd393SMasaki Ota 	input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
294724af5cb9SKevin Cernekee }
294824af5cb9SKevin Cernekee 
29498eccd393SMasaki Ota static void alps_set_abs_params_mt_common(struct alps_data *priv,
295024af5cb9SKevin Cernekee 					  struct input_dev *dev1)
295124af5cb9SKevin Cernekee {
29527a9f73e7SKevin Cernekee 	input_set_abs_params(dev1, ABS_MT_POSITION_X, 0, priv->x_max, 0, 0);
29537a9f73e7SKevin Cernekee 	input_set_abs_params(dev1, ABS_MT_POSITION_Y, 0, priv->y_max, 0, 0);
295424af5cb9SKevin Cernekee 
2955f3f33c67SHans de Goede 	input_abs_set_res(dev1, ABS_MT_POSITION_X, priv->x_res);
2956f3f33c67SHans de Goede 	input_abs_set_res(dev1, ABS_MT_POSITION_Y, priv->y_res);
2957f3f33c67SHans de Goede 
295824af5cb9SKevin Cernekee 	set_bit(BTN_TOOL_TRIPLETAP, dev1->keybit);
295924af5cb9SKevin Cernekee 	set_bit(BTN_TOOL_QUADTAP, dev1->keybit);
29608eccd393SMasaki Ota }
29613808843cSYunkang Tang 
2962688ea364SHans de Goede static void alps_set_abs_params_semi_mt(struct alps_data *priv,
29638eccd393SMasaki Ota 					struct input_dev *dev1)
29648eccd393SMasaki Ota {
29658eccd393SMasaki Ota 	alps_set_abs_params_mt_common(priv, dev1);
29668eccd393SMasaki Ota 	input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
29678eccd393SMasaki Ota 
29688eccd393SMasaki Ota 	input_mt_init_slots(dev1, MAX_TOUCHES,
29698eccd393SMasaki Ota 			    INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
29701662c033SHans de Goede 				INPUT_MT_SEMI_MT);
29718eccd393SMasaki Ota }
29728eccd393SMasaki Ota 
29738eccd393SMasaki Ota static void alps_set_abs_params_v7(struct alps_data *priv,
29748eccd393SMasaki Ota 				   struct input_dev *dev1)
29758eccd393SMasaki Ota {
29768eccd393SMasaki Ota 	alps_set_abs_params_mt_common(priv, dev1);
29778d289842SMasaki Ota 	set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
29788eccd393SMasaki Ota 
29798eccd393SMasaki Ota 	input_mt_init_slots(dev1, MAX_TOUCHES,
29808eccd393SMasaki Ota 			    INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
29818eccd393SMasaki Ota 				INPUT_MT_TRACK);
29823db5b9f7SMasaki Ota 
29833db5b9f7SMasaki Ota 	set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
29843db5b9f7SMasaki Ota }
29853db5b9f7SMasaki Ota 
29863db5b9f7SMasaki Ota static void alps_set_abs_params_ss4_v2(struct alps_data *priv,
29873db5b9f7SMasaki Ota 				       struct input_dev *dev1)
29883db5b9f7SMasaki Ota {
29893db5b9f7SMasaki Ota 	alps_set_abs_params_mt_common(priv, dev1);
29903db5b9f7SMasaki Ota 	input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
29913db5b9f7SMasaki Ota 	set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
29923db5b9f7SMasaki Ota 
29933db5b9f7SMasaki Ota 	input_mt_init_slots(dev1, MAX_TOUCHES,
29943db5b9f7SMasaki Ota 			    INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
29953db5b9f7SMasaki Ota 				INPUT_MT_TRACK);
299624af5cb9SKevin Cernekee }
299724af5cb9SKevin Cernekee 
29981da177e4SLinus Torvalds int alps_init(struct psmouse *psmouse)
29991da177e4SLinus Torvalds {
3000a09221e8SDmitry Torokhov 	struct alps_data *priv = psmouse->private;
300104aae283SPali Rohár 	struct input_dev *dev1 = psmouse->dev;
3002a09221e8SDmitry Torokhov 	int error;
30031da177e4SLinus Torvalds 
3004a09221e8SDmitry Torokhov 	error = priv->hw_init(psmouse);
3005a09221e8SDmitry Torokhov 	if (error)
30061da177e4SLinus Torvalds 		goto init_fail;
30071da177e4SLinus Torvalds 
30087105d2eaSDmitry Torokhov 	/*
30097105d2eaSDmitry Torokhov 	 * Undo part of setup done for us by psmouse core since touchpad
30107105d2eaSDmitry Torokhov 	 * is not a relative device.
30117105d2eaSDmitry Torokhov 	 */
30127105d2eaSDmitry Torokhov 	__clear_bit(EV_REL, dev1->evbit);
30137105d2eaSDmitry Torokhov 	__clear_bit(REL_X, dev1->relbit);
30147105d2eaSDmitry Torokhov 	__clear_bit(REL_Y, dev1->relbit);
30157105d2eaSDmitry Torokhov 
30167105d2eaSDmitry Torokhov 	/*
30177105d2eaSDmitry Torokhov 	 * Now set up our capabilities.
30187105d2eaSDmitry Torokhov 	 */
30197b19ada2SJiri Slaby 	dev1->evbit[BIT_WORD(EV_KEY)] |= BIT_MASK(EV_KEY);
30207b19ada2SJiri Slaby 	dev1->keybit[BIT_WORD(BTN_TOUCH)] |= BIT_MASK(BTN_TOUCH);
30217b19ada2SJiri Slaby 	dev1->keybit[BIT_WORD(BTN_TOOL_FINGER)] |= BIT_MASK(BTN_TOOL_FINGER);
302271bb21b6SMaxim Levitsky 	dev1->keybit[BIT_WORD(BTN_LEFT)] |=
302371bb21b6SMaxim Levitsky 		BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
30241da177e4SLinus Torvalds 
30257b19ada2SJiri Slaby 	dev1->evbit[BIT_WORD(EV_ABS)] |= BIT_MASK(EV_ABS);
302625bded7cSSeth Forshee 
302724af5cb9SKevin Cernekee 	priv->set_abs_params(priv, dev1);
30281da177e4SLinus Torvalds 
302999df65e7SKevin Cernekee 	if (priv->flags & ALPS_WHEEL) {
30307b19ada2SJiri Slaby 		dev1->evbit[BIT_WORD(EV_REL)] |= BIT_MASK(EV_REL);
30317b19ada2SJiri Slaby 		dev1->relbit[BIT_WORD(REL_WHEEL)] |= BIT_MASK(REL_WHEEL);
30321da177e4SLinus Torvalds 	}
30331da177e4SLinus Torvalds 
303499df65e7SKevin Cernekee 	if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
30357b19ada2SJiri Slaby 		dev1->keybit[BIT_WORD(BTN_FORWARD)] |= BIT_MASK(BTN_FORWARD);
30367b19ada2SJiri Slaby 		dev1->keybit[BIT_WORD(BTN_BACK)] |= BIT_MASK(BTN_BACK);
30371da177e4SLinus Torvalds 	}
30381da177e4SLinus Torvalds 
303999df65e7SKevin Cernekee 	if (priv->flags & ALPS_FOUR_BUTTONS) {
304071bb21b6SMaxim Levitsky 		dev1->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_0);
304171bb21b6SMaxim Levitsky 		dev1->keybit[BIT_WORD(BTN_1)] |= BIT_MASK(BTN_1);
304271bb21b6SMaxim Levitsky 		dev1->keybit[BIT_WORD(BTN_2)] |= BIT_MASK(BTN_2);
304371bb21b6SMaxim Levitsky 		dev1->keybit[BIT_WORD(BTN_3)] |= BIT_MASK(BTN_3);
30443808843cSYunkang Tang 	} else if (priv->flags & ALPS_BUTTONPAD) {
30453808843cSYunkang Tang 		set_bit(INPUT_PROP_BUTTONPAD, dev1->propbit);
30463808843cSYunkang Tang 		clear_bit(BTN_RIGHT, dev1->keybit);
304771bb21b6SMaxim Levitsky 	} else {
304871bb21b6SMaxim Levitsky 		dev1->keybit[BIT_WORD(BTN_MIDDLE)] |= BIT_MASK(BTN_MIDDLE);
304971bb21b6SMaxim Levitsky 	}
305071bb21b6SMaxim Levitsky 
3051dfba8600SPali Rohár 	if (priv->flags & ALPS_DUALPOINT) {
305204aae283SPali Rohár 		struct input_dev *dev2;
305304aae283SPali Rohár 
305404aae283SPali Rohár 		dev2 = input_allocate_device();
305504aae283SPali Rohár 		if (!dev2) {
305604aae283SPali Rohár 			psmouse_err(psmouse,
305704aae283SPali Rohár 				    "failed to allocate trackstick device\n");
305804aae283SPali Rohár 			error = -ENOMEM;
305904aae283SPali Rohár 			goto init_fail;
306004aae283SPali Rohár 		}
306104aae283SPali Rohár 
306204aae283SPali Rohár 		snprintf(priv->phys2, sizeof(priv->phys2), "%s/input1",
306304aae283SPali Rohár 			 psmouse->ps2dev.serio->phys);
306404aae283SPali Rohár 		dev2->phys = priv->phys2;
306504aae283SPali Rohár 
3066dfba8600SPali Rohár 		/*
3067dfba8600SPali Rohár 		 * format of input device name is: "protocol vendor name"
3068dfba8600SPali Rohár 		 * see function psmouse_switch_protocol() in psmouse-base.c
3069dfba8600SPali Rohár 		 */
3070dfba8600SPali Rohár 		dev2->name = "AlpsPS/2 ALPS DualPoint Stick";
3071dfba8600SPali Rohár 
30722e5b636bSDmitry Torokhov 		dev2->id.bustype = BUS_I8042;
30732e5b636bSDmitry Torokhov 		dev2->id.vendor  = 0x0002;
307404aae283SPali Rohár 		dev2->id.product = PSMOUSE_ALPS;
307504aae283SPali Rohár 		dev2->id.version = priv->proto_version;
30761db3a345SDmitry Torokhov 		dev2->dev.parent = &psmouse->ps2dev.serio->dev;
30771da177e4SLinus Torvalds 
307804aae283SPali Rohár 		input_set_capability(dev2, EV_REL, REL_X);
307904aae283SPali Rohár 		input_set_capability(dev2, EV_REL, REL_Y);
30807ad8a106SBen Gamari 		if (priv->flags & ALPS_DUALPOINT_WITH_PRESSURE) {
30817ad8a106SBen Gamari 			input_set_capability(dev2, EV_ABS, ABS_PRESSURE);
30827ad8a106SBen Gamari 			input_set_abs_params(dev2, ABS_PRESSURE, 0, 127, 0, 0);
30837ad8a106SBen Gamari 		}
308404aae283SPali Rohár 		input_set_capability(dev2, EV_KEY, BTN_LEFT);
308504aae283SPali Rohár 		input_set_capability(dev2, EV_KEY, BTN_RIGHT);
308604aae283SPali Rohár 		input_set_capability(dev2, EV_KEY, BTN_MIDDLE);
30871da177e4SLinus Torvalds 
308801d4cd5cSHans de Goede 		__set_bit(INPUT_PROP_POINTER, dev2->propbit);
30897611392fSHans de Goede 		__set_bit(INPUT_PROP_POINTING_STICK, dev2->propbit);
30907611392fSHans de Goede 
309104aae283SPali Rohár 		error = input_register_device(dev2);
309204aae283SPali Rohár 		if (error) {
309304aae283SPali Rohár 			psmouse_err(psmouse,
309404aae283SPali Rohár 				    "failed to register trackstick device: %d\n",
309504aae283SPali Rohár 				    error);
309604aae283SPali Rohár 			input_free_device(dev2);
3097f42649e8SDmitry Torokhov 			goto init_fail;
309804aae283SPali Rohár 		}
309904aae283SPali Rohár 
310004aae283SPali Rohár 		priv->dev2 = dev2;
310104aae283SPali Rohár 	}
310204aae283SPali Rohár 
310304aae283SPali Rohár 	priv->psmouse = psmouse;
310404aae283SPali Rohár 
310504aae283SPali Rohár 	INIT_DELAYED_WORK(&priv->dev3_register_work,
310604aae283SPali Rohár 			  alps_register_bare_ps2_mouse);
31071da177e4SLinus Torvalds 
31081da177e4SLinus Torvalds 	psmouse->protocol_handler = alps_process_byte;
3109f0d5c6f4SDmitry Torokhov 	psmouse->poll = alps_poll;
31101da177e4SLinus Torvalds 	psmouse->disconnect = alps_disconnect;
31111da177e4SLinus Torvalds 	psmouse->reconnect = alps_reconnect;
311299df65e7SKevin Cernekee 	psmouse->pktsize = priv->proto_version == ALPS_PROTO_V4 ? 8 : 6;
31131da177e4SLinus Torvalds 
3114f0d5c6f4SDmitry Torokhov 	/* We are having trouble resyncing ALPS touchpads so disable it for now */
3115f0d5c6f4SDmitry Torokhov 	psmouse->resync_time = 0;
3116f0d5c6f4SDmitry Torokhov 
31179d720b34SPali Rohár 	/* Allow 2 invalid packets without resetting device */
31189d720b34SPali Rohár 	psmouse->resetafter = psmouse->pktsize * 2;
31199d720b34SPali Rohár 
31201da177e4SLinus Torvalds 	return 0;
31211da177e4SLinus Torvalds 
31221da177e4SLinus Torvalds init_fail:
3123f42649e8SDmitry Torokhov 	psmouse_reset(psmouse);
3124a09221e8SDmitry Torokhov 	/*
3125a09221e8SDmitry Torokhov 	 * Even though we did not allocate psmouse->private we do free
3126a09221e8SDmitry Torokhov 	 * it here.
3127a09221e8SDmitry Torokhov 	 */
3128a09221e8SDmitry Torokhov 	kfree(psmouse->private);
31291e0c5b12SDmitry Torokhov 	psmouse->private = NULL;
3130a09221e8SDmitry Torokhov 	return error;
31311da177e4SLinus Torvalds }
31321da177e4SLinus Torvalds 
3133b7802c5cSDmitry Torokhov int alps_detect(struct psmouse *psmouse, bool set_properties)
31341da177e4SLinus Torvalds {
3135a09221e8SDmitry Torokhov 	struct alps_data *priv;
3136a09221e8SDmitry Torokhov 	int error;
31371da177e4SLinus Torvalds 
3138a09221e8SDmitry Torokhov 	error = alps_identify(psmouse, NULL);
3139a09221e8SDmitry Torokhov 	if (error)
3140a09221e8SDmitry Torokhov 		return error;
3141a09221e8SDmitry Torokhov 
3142a09221e8SDmitry Torokhov 	/*
3143a09221e8SDmitry Torokhov 	 * Reset the device to make sure it is fully operational:
3144a09221e8SDmitry Torokhov 	 * on some laptops, like certain Dell Latitudes, we may
3145a09221e8SDmitry Torokhov 	 * fail to properly detect presence of trackstick if device
3146a09221e8SDmitry Torokhov 	 * has not been reset.
3147a09221e8SDmitry Torokhov 	 */
3148a09221e8SDmitry Torokhov 	psmouse_reset(psmouse);
3149a09221e8SDmitry Torokhov 
3150a09221e8SDmitry Torokhov 	priv = kzalloc(sizeof(struct alps_data), GFP_KERNEL);
3151a09221e8SDmitry Torokhov 	if (!priv)
3152a09221e8SDmitry Torokhov 		return -ENOMEM;
3153a09221e8SDmitry Torokhov 
3154a09221e8SDmitry Torokhov 	error = alps_identify(psmouse, priv);
315593050db2SDmitry Torokhov 	if (error) {
315693050db2SDmitry Torokhov 		kfree(priv);
3157a09221e8SDmitry Torokhov 		return error;
315893050db2SDmitry Torokhov 	}
31591da177e4SLinus Torvalds 
31601da177e4SLinus Torvalds 	if (set_properties) {
31611da177e4SLinus Torvalds 		psmouse->vendor = "ALPS";
3162a09221e8SDmitry Torokhov 		psmouse->name = priv->flags & ALPS_DUALPOINT ?
3163968ac842SDmitry Torokhov 				"DualPoint TouchPad" : "GlidePoint";
3164a09221e8SDmitry Torokhov 		psmouse->model = priv->proto_version;
3165a09221e8SDmitry Torokhov 	} else {
3166a09221e8SDmitry Torokhov 		/*
3167a09221e8SDmitry Torokhov 		 * Destroy alps_data structure we allocated earlier since
3168a09221e8SDmitry Torokhov 		 * this was just a "trial run". Otherwise we'll keep it
3169a09221e8SDmitry Torokhov 		 * to be used by alps_init() which has to be called if
3170a09221e8SDmitry Torokhov 		 * we succeed and set_properties is true.
3171a09221e8SDmitry Torokhov 		 */
3172a09221e8SDmitry Torokhov 		kfree(priv);
3173a09221e8SDmitry Torokhov 		psmouse->private = NULL;
31741da177e4SLinus Torvalds 	}
3175a09221e8SDmitry Torokhov 
31761da177e4SLinus Torvalds 	return 0;
31771da177e4SLinus Torvalds }
31781da177e4SLinus Torvalds 
3179