xref: /openbmc/linux/drivers/input/mouse/alps.c (revision 754ff506)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * ALPS touchpad PS/2 mouse driver
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * Copyright (c) 2003 Neil Brown <neilb@cse.unsw.edu.au>
6963f626dSPeter Osterlund  * Copyright (c) 2003-2005 Peter Osterlund <petero2@telia.com>
71da177e4SLinus Torvalds  * Copyright (c) 2004 Dmitry Torokhov <dtor@mail.ru>
81da177e4SLinus Torvalds  * Copyright (c) 2005 Vojtech Pavlik <vojtech@suse.cz>
91d9f2626SSebastian Kapfer  * Copyright (c) 2009 Sebastian Kapfer <sebastian_kapfer@gmx.net>
101da177e4SLinus Torvalds  *
111da177e4SLinus Torvalds  * ALPS detection, tap switching and status querying info is taken from
121da177e4SLinus Torvalds  * tpconfig utility (by C. Scott Ananian and Bruce Kall).
131da177e4SLinus Torvalds  */
141da177e4SLinus Torvalds 
155a0e3ad6STejun Heo #include <linux/slab.h>
161da177e4SLinus Torvalds #include <linux/input.h>
1701ce661fSSeth Forshee #include <linux/input/mt.h>
181da177e4SLinus Torvalds #include <linux/serio.h>
191da177e4SLinus Torvalds #include <linux/libps2.h>
20073e570dSHans de Goede #include <linux/dmi.h>
211da177e4SLinus Torvalds 
221da177e4SLinus Torvalds #include "psmouse.h"
231da177e4SLinus Torvalds #include "alps.h"
247e4935ccSHui Wang #include "trackpoint.h"
251da177e4SLinus Torvalds 
2625bded7cSSeth Forshee /*
2725bded7cSSeth Forshee  * Definitions for ALPS version 3 and 4 command mode protocol
2825bded7cSSeth Forshee  */
2925bded7cSSeth Forshee #define ALPS_CMD_NIBBLE_10	0x01f2
3025bded7cSSeth Forshee 
31cd401204SKevin Cernekee #define ALPS_REG_BASE_RUSHMORE	0xc2c0
32dae928ecSPali Rohár #define ALPS_REG_BASE_V7	0xc2c0
33cd401204SKevin Cernekee #define ALPS_REG_BASE_PINNACLE	0x0000
34cd401204SKevin Cernekee 
3525bded7cSSeth Forshee static const struct alps_nibble_commands alps_v3_nibble_commands[] = {
3625bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETPOLL,		0x00 }, /* 0 */
3725bded7cSSeth Forshee 	{ PSMOUSE_CMD_RESET_DIS,	0x00 }, /* 1 */
3825bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETSCALE21,	0x00 }, /* 2 */
3925bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0x0a }, /* 3 */
4025bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0x14 }, /* 4 */
4125bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0x28 }, /* 5 */
4225bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0x3c }, /* 6 */
4325bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0x50 }, /* 7 */
4425bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0x64 }, /* 8 */
4525bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0xc8 }, /* 9 */
4625bded7cSSeth Forshee 	{ ALPS_CMD_NIBBLE_10,		0x00 }, /* a */
4725bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRES,		0x00 }, /* b */
4825bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRES,		0x01 }, /* c */
4925bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRES,		0x02 }, /* d */
5025bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRES,		0x03 }, /* e */
5125bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETSCALE11,	0x00 }, /* f */
5225bded7cSSeth Forshee };
5325bded7cSSeth Forshee 
5425bded7cSSeth Forshee static const struct alps_nibble_commands alps_v4_nibble_commands[] = {
5525bded7cSSeth Forshee 	{ PSMOUSE_CMD_ENABLE,		0x00 }, /* 0 */
5625bded7cSSeth Forshee 	{ PSMOUSE_CMD_RESET_DIS,	0x00 }, /* 1 */
5725bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETSCALE21,	0x00 }, /* 2 */
5825bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0x0a }, /* 3 */
5925bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0x14 }, /* 4 */
6025bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0x28 }, /* 5 */
6125bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0x3c }, /* 6 */
6225bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0x50 }, /* 7 */
6325bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0x64 }, /* 8 */
6425bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0xc8 }, /* 9 */
6525bded7cSSeth Forshee 	{ ALPS_CMD_NIBBLE_10,		0x00 }, /* a */
6625bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRES,		0x00 }, /* b */
6725bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRES,		0x01 }, /* c */
6825bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRES,		0x02 }, /* d */
6925bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRES,		0x03 }, /* e */
7025bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETSCALE11,	0x00 }, /* f */
7125bded7cSSeth Forshee };
7225bded7cSSeth Forshee 
7395f75e91SYunkang Tang static const struct alps_nibble_commands alps_v6_nibble_commands[] = {
7495f75e91SYunkang Tang 	{ PSMOUSE_CMD_ENABLE,		0x00 }, /* 0 */
7595f75e91SYunkang Tang 	{ PSMOUSE_CMD_SETRATE,		0x0a }, /* 1 */
7695f75e91SYunkang Tang 	{ PSMOUSE_CMD_SETRATE,		0x14 }, /* 2 */
7795f75e91SYunkang Tang 	{ PSMOUSE_CMD_SETRATE,		0x28 }, /* 3 */
7895f75e91SYunkang Tang 	{ PSMOUSE_CMD_SETRATE,		0x3c }, /* 4 */
7995f75e91SYunkang Tang 	{ PSMOUSE_CMD_SETRATE,		0x50 }, /* 5 */
8095f75e91SYunkang Tang 	{ PSMOUSE_CMD_SETRATE,		0x64 }, /* 6 */
8195f75e91SYunkang Tang 	{ PSMOUSE_CMD_SETRATE,		0xc8 }, /* 7 */
8295f75e91SYunkang Tang 	{ PSMOUSE_CMD_GETID,		0x00 }, /* 8 */
8395f75e91SYunkang Tang 	{ PSMOUSE_CMD_GETINFO,		0x00 }, /* 9 */
8495f75e91SYunkang Tang 	{ PSMOUSE_CMD_SETRES,		0x00 }, /* a */
8595f75e91SYunkang Tang 	{ PSMOUSE_CMD_SETRES,		0x01 }, /* b */
8695f75e91SYunkang Tang 	{ PSMOUSE_CMD_SETRES,		0x02 }, /* c */
8795f75e91SYunkang Tang 	{ PSMOUSE_CMD_SETRES,		0x03 }, /* d */
8895f75e91SYunkang Tang 	{ PSMOUSE_CMD_SETSCALE21,	0x00 }, /* e */
8995f75e91SYunkang Tang 	{ PSMOUSE_CMD_SETSCALE11,	0x00 }, /* f */
9095f75e91SYunkang Tang };
9195f75e91SYunkang Tang 
9225bded7cSSeth Forshee 
9371bb21b6SMaxim Levitsky #define ALPS_DUALPOINT		0x02	/* touchpad has trackstick */
9471bb21b6SMaxim Levitsky #define ALPS_PASS		0x04	/* device has a pass-through port */
9571bb21b6SMaxim Levitsky 
9671bb21b6SMaxim Levitsky #define ALPS_WHEEL		0x08	/* hardware wheel present */
9771bb21b6SMaxim Levitsky #define ALPS_FW_BK_1		0x10	/* front & back buttons present */
9871bb21b6SMaxim Levitsky #define ALPS_FW_BK_2		0x20	/* front & back buttons present */
9971bb21b6SMaxim Levitsky #define ALPS_FOUR_BUTTONS	0x40	/* 4 direction button present */
1001d9f2626SSebastian Kapfer #define ALPS_PS2_INTERLEAVED	0x80	/* 3-byte PS/2 packet interleaved with
1011d9f2626SSebastian Kapfer 					   6-byte ALPS packet */
10219556219SHans de Goede #define ALPS_STICK_BITS		0x100	/* separate stick button bits */
1033808843cSYunkang Tang #define ALPS_BUTTONPAD		0x200	/* device is a clickpad */
1047ad8a106SBen Gamari #define ALPS_DUALPOINT_WITH_PRESSURE	0x400	/* device can report trackpoint pressure */
1051da177e4SLinus Torvalds 
106e38de678SHelge Deller static const struct alps_model_info alps_model_data[] = {
107626b9da0SDmitry Torokhov 	/*
108626b9da0SDmitry Torokhov 	 * XXX This entry is suspicious. First byte has zero lower nibble,
109626b9da0SDmitry Torokhov 	 * which is what a normal mouse would report. Also, the value 0x0e
110626b9da0SDmitry Torokhov 	 * isn't valid per PS/2 spec.
111626b9da0SDmitry Torokhov 	 */
112a3cbfd56SPali Rohár 	{ { 0x20, 0x02, 0x0e }, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } },
113626b9da0SDmitry Torokhov 
114a3cbfd56SPali Rohár 	{ { 0x22, 0x02, 0x0a }, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } },
115a3cbfd56SPali Rohár 	{ { 0x22, 0x02, 0x14 }, { ALPS_PROTO_V2, 0xff, 0xff, ALPS_PASS | ALPS_DUALPOINT } },	/* Dell Latitude D600 */
116a3cbfd56SPali Rohár 	{ { 0x32, 0x02, 0x14 }, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } },	/* Toshiba Salellite Pro M10 */
117a3cbfd56SPali Rohár 	{ { 0x33, 0x02, 0x0a }, { ALPS_PROTO_V1, 0x88, 0xf8, 0 } },				/* UMAX-530T */
118a3cbfd56SPali Rohár 	{ { 0x52, 0x01, 0x14 }, { ALPS_PROTO_V2, 0xff, 0xff,
1198326bb57SDmitry Torokhov 		ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED } },				/* Toshiba Tecra A11-11L */
120a3cbfd56SPali Rohár 	{ { 0x53, 0x02, 0x0a }, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
121a3cbfd56SPali Rohár 	{ { 0x53, 0x02, 0x14 }, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
122a3cbfd56SPali Rohár 	{ { 0x60, 0x03, 0xc8 }, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },				/* HP ze1115 */
123a3cbfd56SPali Rohár 	{ { 0x62, 0x02, 0x14 }, { ALPS_PROTO_V2, 0xcf, 0xcf,
124a3cbfd56SPali Rohár 		ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED } },				/* Dell Latitude E5500, E6400, E6500, Precision M4400 */
125a3cbfd56SPali Rohár 	{ { 0x63, 0x02, 0x0a }, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
126a3cbfd56SPali Rohár 	{ { 0x63, 0x02, 0x14 }, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
127a3cbfd56SPali Rohár 	{ { 0x63, 0x02, 0x28 }, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 } },			/* Fujitsu Siemens S6010 */
128a3cbfd56SPali Rohár 	{ { 0x63, 0x02, 0x3c }, { ALPS_PROTO_V2, 0x8f, 0x8f, ALPS_WHEEL } },			/* Toshiba Satellite S2400-103 */
129a3cbfd56SPali Rohár 	{ { 0x63, 0x02, 0x50 }, { ALPS_PROTO_V2, 0xef, 0xef, ALPS_FW_BK_1 } },			/* NEC Versa L320 */
130a3cbfd56SPali Rohár 	{ { 0x63, 0x02, 0x64 }, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
131a3cbfd56SPali Rohár 	{ { 0x63, 0x03, 0xc8 }, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } },	/* Dell Latitude D800 */
132a3cbfd56SPali Rohár 	{ { 0x73, 0x00, 0x0a }, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_DUALPOINT } },		/* ThinkPad R61 8918-5QG */
133a3cbfd56SPali Rohár 	{ { 0x73, 0x00, 0x14 }, { ALPS_PROTO_V6, 0xff, 0xff, ALPS_DUALPOINT } },		/* Dell XT2 */
134a3cbfd56SPali Rohár 	{ { 0x73, 0x02, 0x0a }, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
135a3cbfd56SPali Rohár 	{ { 0x73, 0x02, 0x14 }, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 } },			/* Ahtec Laptop */
136a3cbfd56SPali Rohár 	{ { 0x73, 0x02, 0x50 }, { ALPS_PROTO_V2, 0xcf, 0xcf, ALPS_FOUR_BUTTONS } },		/* Dell Vostro 1400 */
1371da177e4SLinus Torvalds };
1381da177e4SLinus Torvalds 
1393296f71cSDmitry Torokhov static const struct alps_protocol_info alps_v3_protocol_data = {
1404621c966SPali Rohár 	ALPS_PROTO_V3, 0x8f, 0x8f, ALPS_DUALPOINT | ALPS_DUALPOINT_WITH_PRESSURE
1413296f71cSDmitry Torokhov };
1423296f71cSDmitry Torokhov 
1433296f71cSDmitry Torokhov static const struct alps_protocol_info alps_v3_rushmore_data = {
1444621c966SPali Rohár 	ALPS_PROTO_V3_RUSHMORE, 0x8f, 0x8f, ALPS_DUALPOINT | ALPS_DUALPOINT_WITH_PRESSURE
1453296f71cSDmitry Torokhov };
1463296f71cSDmitry Torokhov 
14709c398bcSPali Rohár static const struct alps_protocol_info alps_v4_protocol_data = {
14809c398bcSPali Rohár 	ALPS_PROTO_V4, 0x8f, 0x8f, 0
14909c398bcSPali Rohár };
15009c398bcSPali Rohár 
1513296f71cSDmitry Torokhov static const struct alps_protocol_info alps_v5_protocol_data = {
1523296f71cSDmitry Torokhov 	ALPS_PROTO_V5, 0xc8, 0xd8, 0
1533296f71cSDmitry Torokhov };
1543296f71cSDmitry Torokhov 
1553296f71cSDmitry Torokhov static const struct alps_protocol_info alps_v7_protocol_data = {
1564621c966SPali Rohár 	ALPS_PROTO_V7, 0x48, 0x48, ALPS_DUALPOINT | ALPS_DUALPOINT_WITH_PRESSURE
1573296f71cSDmitry Torokhov };
1583296f71cSDmitry Torokhov 
1593db5b9f7SMasaki Ota static const struct alps_protocol_info alps_v8_protocol_data = {
1603db5b9f7SMasaki Ota 	ALPS_PROTO_V8, 0x18, 0x18, 0
1613db5b9f7SMasaki Ota };
1623db5b9f7SMasaki Ota 
163c9815232SPali Rohár static const struct alps_protocol_info alps_v9_protocol_data = {
164c9815232SPali Rohár 	ALPS_PROTO_V9, 0xc8, 0xc8, 0
165c9815232SPali Rohár };
166c9815232SPali Rohár 
16719556219SHans de Goede /*
16819556219SHans de Goede  * Some v2 models report the stick buttons in separate bits
16919556219SHans de Goede  */
17019556219SHans de Goede static const struct dmi_system_id alps_dmi_has_separate_stick_buttons[] = {
17119556219SHans de Goede #if defined(CONFIG_DMI) && defined(CONFIG_X86)
17219556219SHans de Goede 	{
17319556219SHans de Goede 		/* Extrapolated from other entries */
17419556219SHans de Goede 		.matches = {
17519556219SHans de Goede 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
17619556219SHans de Goede 			DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D420"),
17719556219SHans de Goede 		},
17819556219SHans de Goede 	},
17919556219SHans de Goede 	{
18019556219SHans de Goede 		/* Reported-by: Hans de Bruin <jmdebruin@xmsnet.nl> */
18119556219SHans de Goede 		.matches = {
18219556219SHans de Goede 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
18319556219SHans de Goede 			DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D430"),
18419556219SHans de Goede 		},
18519556219SHans de Goede 	},
18619556219SHans de Goede 	{
18719556219SHans de Goede 		/* Reported-by: Hans de Goede <hdegoede@redhat.com> */
18819556219SHans de Goede 		.matches = {
18919556219SHans de Goede 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
19019556219SHans de Goede 			DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D620"),
19119556219SHans de Goede 		},
19219556219SHans de Goede 	},
19319556219SHans de Goede 	{
19419556219SHans de Goede 		/* Extrapolated from other entries */
19519556219SHans de Goede 		.matches = {
19619556219SHans de Goede 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
19719556219SHans de Goede 			DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D630"),
19819556219SHans de Goede 		},
19919556219SHans de Goede 	},
20019556219SHans de Goede #endif
20119556219SHans de Goede 	{ }
20219556219SHans de Goede };
20319556219SHans de Goede 
20424af5cb9SKevin Cernekee static void alps_set_abs_params_st(struct alps_data *priv,
20524af5cb9SKevin Cernekee 				   struct input_dev *dev1);
206688ea364SHans de Goede static void alps_set_abs_params_semi_mt(struct alps_data *priv,
20724af5cb9SKevin Cernekee 					struct input_dev *dev1);
2088eccd393SMasaki Ota static void alps_set_abs_params_v7(struct alps_data *priv,
2098eccd393SMasaki Ota 				   struct input_dev *dev1);
2103db5b9f7SMasaki Ota static void alps_set_abs_params_ss4_v2(struct alps_data *priv,
2113db5b9f7SMasaki Ota 				       struct input_dev *dev1);
21224af5cb9SKevin Cernekee 
2135fb94e9cSMauro Carvalho Chehab /* Packet formats are described in Documentation/input/devices/alps.rst */
2141da177e4SLinus Torvalds 
alps_is_valid_first_byte(struct alps_data * priv,unsigned char data)21599df65e7SKevin Cernekee static bool alps_is_valid_first_byte(struct alps_data *priv,
2161d9f2626SSebastian Kapfer 				     unsigned char data)
2171d9f2626SSebastian Kapfer {
21899df65e7SKevin Cernekee 	return (data & priv->mask0) == priv->byte0;
2191d9f2626SSebastian Kapfer }
2201d9f2626SSebastian Kapfer 
alps_report_buttons(struct input_dev * dev1,struct input_dev * dev2,int left,int right,int middle)22104aae283SPali Rohár static void alps_report_buttons(struct input_dev *dev1, struct input_dev *dev2,
2221d9f2626SSebastian Kapfer 				int left, int right, int middle)
2231d9f2626SSebastian Kapfer {
2241d9f2626SSebastian Kapfer 	struct input_dev *dev;
2251d9f2626SSebastian Kapfer 
2261d9f2626SSebastian Kapfer 	/*
2271d9f2626SSebastian Kapfer 	 * If shared button has already been reported on the
2281d9f2626SSebastian Kapfer 	 * other device (dev2) then this event should be also
2291d9f2626SSebastian Kapfer 	 * sent through that device.
2301d9f2626SSebastian Kapfer 	 */
23104aae283SPali Rohár 	dev = (dev2 && test_bit(BTN_LEFT, dev2->key)) ? dev2 : dev1;
2321d9f2626SSebastian Kapfer 	input_report_key(dev, BTN_LEFT, left);
2331d9f2626SSebastian Kapfer 
23404aae283SPali Rohár 	dev = (dev2 && test_bit(BTN_RIGHT, dev2->key)) ? dev2 : dev1;
2351d9f2626SSebastian Kapfer 	input_report_key(dev, BTN_RIGHT, right);
2361d9f2626SSebastian Kapfer 
23704aae283SPali Rohár 	dev = (dev2 && test_bit(BTN_MIDDLE, dev2->key)) ? dev2 : dev1;
2381d9f2626SSebastian Kapfer 	input_report_key(dev, BTN_MIDDLE, middle);
2391d9f2626SSebastian Kapfer 
2401d9f2626SSebastian Kapfer 	/*
2411d9f2626SSebastian Kapfer 	 * Sync the _other_ device now, we'll do the first
2421d9f2626SSebastian Kapfer 	 * device later once we report the rest of the events.
2431d9f2626SSebastian Kapfer 	 */
24404aae283SPali Rohár 	if (dev2)
2451d9f2626SSebastian Kapfer 		input_sync(dev2);
2461d9f2626SSebastian Kapfer }
2471d9f2626SSebastian Kapfer 
alps_process_packet_v1_v2(struct psmouse * psmouse)24825bded7cSSeth Forshee static void alps_process_packet_v1_v2(struct psmouse *psmouse)
2491da177e4SLinus Torvalds {
2501da177e4SLinus Torvalds 	struct alps_data *priv = psmouse->private;
2511da177e4SLinus Torvalds 	unsigned char *packet = psmouse->packet;
2522e5b636bSDmitry Torokhov 	struct input_dev *dev = psmouse->dev;
2532e5b636bSDmitry Torokhov 	struct input_dev *dev2 = priv->dev2;
2541da177e4SLinus Torvalds 	int x, y, z, ges, fin, left, right, middle;
255c30b4c10SIvan Casado Ruiz 	int back = 0, forward = 0;
2561da177e4SLinus Torvalds 
25799df65e7SKevin Cernekee 	if (priv->proto_version == ALPS_PROTO_V1) {
258d2f4012fSYotam Medini 		left = packet[2] & 0x10;
259d2f4012fSYotam Medini 		right = packet[2] & 0x08;
2601da177e4SLinus Torvalds 		middle = 0;
2611da177e4SLinus Torvalds 		x = packet[1] | ((packet[0] & 0x07) << 7);
2621da177e4SLinus Torvalds 		y = packet[4] | ((packet[3] & 0x07) << 7);
2631da177e4SLinus Torvalds 		z = packet[5];
2641da177e4SLinus Torvalds 	} else {
2651da177e4SLinus Torvalds 		left = packet[3] & 1;
2661da177e4SLinus Torvalds 		right = packet[3] & 2;
2671da177e4SLinus Torvalds 		middle = packet[3] & 4;
2681da177e4SLinus Torvalds 		x = packet[1] | ((packet[2] & 0x78) << (7 - 3));
2691da177e4SLinus Torvalds 		y = packet[4] | ((packet[3] & 0x70) << (7 - 4));
2701da177e4SLinus Torvalds 		z = packet[5];
2711da177e4SLinus Torvalds 	}
2721da177e4SLinus Torvalds 
27399df65e7SKevin Cernekee 	if (priv->flags & ALPS_FW_BK_1) {
2743c00bb96SLaszlo Kajan 		back = packet[0] & 0x10;
2753c00bb96SLaszlo Kajan 		forward = packet[2] & 4;
276c30b4c10SIvan Casado Ruiz 	}
277c30b4c10SIvan Casado Ruiz 
27899df65e7SKevin Cernekee 	if (priv->flags & ALPS_FW_BK_2) {
279c30b4c10SIvan Casado Ruiz 		back = packet[3] & 4;
280c30b4c10SIvan Casado Ruiz 		forward = packet[2] & 4;
281c30b4c10SIvan Casado Ruiz 		if ((middle = forward && back))
282c30b4c10SIvan Casado Ruiz 			forward = back = 0;
283c30b4c10SIvan Casado Ruiz 	}
284c30b4c10SIvan Casado Ruiz 
2851da177e4SLinus Torvalds 	ges = packet[2] & 1;
2861da177e4SLinus Torvalds 	fin = packet[2] & 2;
2871da177e4SLinus Torvalds 
28899df65e7SKevin Cernekee 	if ((priv->flags & ALPS_DUALPOINT) && z == 127) {
2891da177e4SLinus Torvalds 		input_report_rel(dev2, REL_X,  (x > 383 ? (x - 768) : x));
2901da177e4SLinus Torvalds 		input_report_rel(dev2, REL_Y, -(y > 255 ? (y - 512) : y));
291d7ed5d88SUlrich Dangel 
29204aae283SPali Rohár 		alps_report_buttons(dev2, dev, left, right, middle);
293d7ed5d88SUlrich Dangel 
2941da177e4SLinus Torvalds 		input_sync(dev2);
2951da177e4SLinus Torvalds 		return;
2961da177e4SLinus Torvalds 	}
2971da177e4SLinus Torvalds 
29819556219SHans de Goede 	/* Some models have separate stick button bits */
29919556219SHans de Goede 	if (priv->flags & ALPS_STICK_BITS) {
30092bac83dSHans de Goede 		left |= packet[0] & 1;
30192bac83dSHans de Goede 		right |= packet[0] & 2;
30292bac83dSHans de Goede 		middle |= packet[0] & 4;
30392bac83dSHans de Goede 	}
30492bac83dSHans de Goede 
30504aae283SPali Rohár 	alps_report_buttons(dev, dev2, left, right, middle);
306d7ed5d88SUlrich Dangel 
3071da177e4SLinus Torvalds 	/* Convert hardware tap to a reasonable Z value */
30871bb21b6SMaxim Levitsky 	if (ges && !fin)
30971bb21b6SMaxim Levitsky 		z = 40;
3101da177e4SLinus Torvalds 
3111da177e4SLinus Torvalds 	/*
3121da177e4SLinus Torvalds 	 * A "tap and drag" operation is reported by the hardware as a transition
3131da177e4SLinus Torvalds 	 * from (!fin && ges) to (fin && ges). This should be translated to the
3141da177e4SLinus Torvalds 	 * sequence Z>0, Z==0, Z>0, so the Z==0 event has to be generated manually.
3151da177e4SLinus Torvalds 	 */
3161da177e4SLinus Torvalds 	if (ges && fin && !priv->prev_fin) {
3171da177e4SLinus Torvalds 		input_report_abs(dev, ABS_X, x);
3181da177e4SLinus Torvalds 		input_report_abs(dev, ABS_Y, y);
3191da177e4SLinus Torvalds 		input_report_abs(dev, ABS_PRESSURE, 0);
3201da177e4SLinus Torvalds 		input_report_key(dev, BTN_TOOL_FINGER, 0);
3211da177e4SLinus Torvalds 		input_sync(dev);
3221da177e4SLinus Torvalds 	}
3231da177e4SLinus Torvalds 	priv->prev_fin = fin;
3241da177e4SLinus Torvalds 
32571bb21b6SMaxim Levitsky 	if (z > 30)
32671bb21b6SMaxim Levitsky 		input_report_key(dev, BTN_TOUCH, 1);
32771bb21b6SMaxim Levitsky 	if (z < 25)
32871bb21b6SMaxim Levitsky 		input_report_key(dev, BTN_TOUCH, 0);
3291da177e4SLinus Torvalds 
3301da177e4SLinus Torvalds 	if (z > 0) {
3311da177e4SLinus Torvalds 		input_report_abs(dev, ABS_X, x);
3321da177e4SLinus Torvalds 		input_report_abs(dev, ABS_Y, y);
3331da177e4SLinus Torvalds 	}
3341da177e4SLinus Torvalds 
3351da177e4SLinus Torvalds 	input_report_abs(dev, ABS_PRESSURE, z);
3361da177e4SLinus Torvalds 	input_report_key(dev, BTN_TOOL_FINGER, z > 0);
3371da177e4SLinus Torvalds 
33899df65e7SKevin Cernekee 	if (priv->flags & ALPS_WHEEL)
339e6c047b9SVojtech Pavlik 		input_report_rel(dev, REL_WHEEL, ((packet[2] << 1) & 0x08) - ((packet[0] >> 4) & 0x07));
3401da177e4SLinus Torvalds 
34199df65e7SKevin Cernekee 	if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
342c30b4c10SIvan Casado Ruiz 		input_report_key(dev, BTN_FORWARD, forward);
343c30b4c10SIvan Casado Ruiz 		input_report_key(dev, BTN_BACK, back);
3441da177e4SLinus Torvalds 	}
3451da177e4SLinus Torvalds 
34699df65e7SKevin Cernekee 	if (priv->flags & ALPS_FOUR_BUTTONS) {
34771bb21b6SMaxim Levitsky 		input_report_key(dev, BTN_0, packet[2] & 4);
34871bb21b6SMaxim Levitsky 		input_report_key(dev, BTN_1, packet[0] & 0x10);
34971bb21b6SMaxim Levitsky 		input_report_key(dev, BTN_2, packet[3] & 4);
35071bb21b6SMaxim Levitsky 		input_report_key(dev, BTN_3, packet[0] & 0x20);
35171bb21b6SMaxim Levitsky 	}
35271bb21b6SMaxim Levitsky 
3531da177e4SLinus Torvalds 	input_sync(dev);
3541da177e4SLinus Torvalds }
3551da177e4SLinus Torvalds 
alps_get_bitmap_points(unsigned int map,struct alps_bitmap_point * low,struct alps_bitmap_point * high,int * fingers)356036e6c7bSHans de Goede static void alps_get_bitmap_points(unsigned int map,
357036e6c7bSHans de Goede 				   struct alps_bitmap_point *low,
358036e6c7bSHans de Goede 				   struct alps_bitmap_point *high,
359036e6c7bSHans de Goede 				   int *fingers)
360036e6c7bSHans de Goede {
361036e6c7bSHans de Goede 	struct alps_bitmap_point *point;
362036e6c7bSHans de Goede 	int i, bit, prev_bit = 0;
363036e6c7bSHans de Goede 
364036e6c7bSHans de Goede 	point = low;
365036e6c7bSHans de Goede 	for (i = 0; map != 0; i++, map >>= 1) {
366036e6c7bSHans de Goede 		bit = map & 1;
367036e6c7bSHans de Goede 		if (bit) {
368036e6c7bSHans de Goede 			if (!prev_bit) {
369036e6c7bSHans de Goede 				point->start_bit = i;
370105affbfSHans de Goede 				point->num_bits = 0;
371036e6c7bSHans de Goede 				(*fingers)++;
372036e6c7bSHans de Goede 			}
373036e6c7bSHans de Goede 			point->num_bits++;
374036e6c7bSHans de Goede 		} else {
375036e6c7bSHans de Goede 			if (prev_bit)
376036e6c7bSHans de Goede 				point = high;
377036e6c7bSHans de Goede 		}
378036e6c7bSHans de Goede 		prev_bit = bit;
379036e6c7bSHans de Goede 	}
380036e6c7bSHans de Goede }
381036e6c7bSHans de Goede 
382ee65d4b3SYunkang Tang /*
383dccf1dd8SHans de Goede  * Process bitmap data from semi-mt protocols. Returns the number of
38401ce661fSSeth Forshee  * fingers detected. A return value of 0 means at least one of the
38501ce661fSSeth Forshee  * bitmaps was empty.
38601ce661fSSeth Forshee  *
38701ce661fSSeth Forshee  * The bitmaps don't have enough data to track fingers, so this function
38801ce661fSSeth Forshee  * only generates points representing a bounding box of all contacts.
38902d04254SHans de Goede  * These points are returned in fields->mt when the return value
39001ce661fSSeth Forshee  * is greater than 0.
39101ce661fSSeth Forshee  */
alps_process_bitmap(struct alps_data * priv,struct alps_fields * fields)3927a9f73e7SKevin Cernekee static int alps_process_bitmap(struct alps_data *priv,
39302d04254SHans de Goede 			       struct alps_fields *fields)
39401ce661fSSeth Forshee {
3954dd26573SHans de Goede 	int i, fingers_x = 0, fingers_y = 0, fingers, closest;
39601ce661fSSeth Forshee 	struct alps_bitmap_point x_low = {0,}, x_high = {0,};
39701ce661fSSeth Forshee 	struct alps_bitmap_point y_low = {0,}, y_high = {0,};
3984dd26573SHans de Goede 	struct input_mt_pos corner[4];
39901ce661fSSeth Forshee 
40002d04254SHans de Goede 	if (!fields->x_map || !fields->y_map)
40101ce661fSSeth Forshee 		return 0;
40201ce661fSSeth Forshee 
40302d04254SHans de Goede 	alps_get_bitmap_points(fields->x_map, &x_low, &x_high, &fingers_x);
40402d04254SHans de Goede 	alps_get_bitmap_points(fields->y_map, &y_low, &y_high, &fingers_y);
40501ce661fSSeth Forshee 
40601ce661fSSeth Forshee 	/*
40701ce661fSSeth Forshee 	 * Fingers can overlap, so we use the maximum count of fingers
40801ce661fSSeth Forshee 	 * on either axis as the finger count.
40901ce661fSSeth Forshee 	 */
41001ce661fSSeth Forshee 	fingers = max(fingers_x, fingers_y);
41101ce661fSSeth Forshee 
41201ce661fSSeth Forshee 	/*
41320bea68bSHans de Goede 	 * If an axis reports only a single contact, we have overlapping or
41420bea68bSHans de Goede 	 * adjacent fingers. Divide the single contact between the two points.
41501ce661fSSeth Forshee 	 */
41601ce661fSSeth Forshee 	if (fingers_x == 1) {
41728835f45SHans de Goede 		i = (x_low.num_bits - 1) / 2;
41801ce661fSSeth Forshee 		x_low.num_bits = x_low.num_bits - i;
41901ce661fSSeth Forshee 		x_high.start_bit = x_low.start_bit + i;
42001ce661fSSeth Forshee 		x_high.num_bits = max(i, 1);
42120bea68bSHans de Goede 	}
42220bea68bSHans de Goede 	if (fingers_y == 1) {
42328835f45SHans de Goede 		i = (y_low.num_bits - 1) / 2;
42401ce661fSSeth Forshee 		y_low.num_bits = y_low.num_bits - i;
42501ce661fSSeth Forshee 		y_high.start_bit = y_low.start_bit + i;
42601ce661fSSeth Forshee 		y_high.num_bits = max(i, 1);
42701ce661fSSeth Forshee 	}
42801ce661fSSeth Forshee 
4294dd26573SHans de Goede 	/* top-left corner */
4304dd26573SHans de Goede 	corner[0].x =
43102d04254SHans de Goede 		(priv->x_max * (2 * x_low.start_bit + x_low.num_bits - 1)) /
4327a9f73e7SKevin Cernekee 		(2 * (priv->x_bits - 1));
4334dd26573SHans de Goede 	corner[0].y =
43402d04254SHans de Goede 		(priv->y_max * (2 * y_low.start_bit + y_low.num_bits - 1)) /
4357a9f73e7SKevin Cernekee 		(2 * (priv->y_bits - 1));
43601ce661fSSeth Forshee 
4374dd26573SHans de Goede 	/* top-right corner */
4384dd26573SHans de Goede 	corner[1].x =
43902d04254SHans de Goede 		(priv->x_max * (2 * x_high.start_bit + x_high.num_bits - 1)) /
4407a9f73e7SKevin Cernekee 		(2 * (priv->x_bits - 1));
4414dd26573SHans de Goede 	corner[1].y =
4424dd26573SHans de Goede 		(priv->y_max * (2 * y_low.start_bit + y_low.num_bits - 1)) /
4434dd26573SHans de Goede 		(2 * (priv->y_bits - 1));
4444dd26573SHans de Goede 
4454dd26573SHans de Goede 	/* bottom-right corner */
4464dd26573SHans de Goede 	corner[2].x =
4474dd26573SHans de Goede 		(priv->x_max * (2 * x_high.start_bit + x_high.num_bits - 1)) /
4484dd26573SHans de Goede 		(2 * (priv->x_bits - 1));
4494dd26573SHans de Goede 	corner[2].y =
45002d04254SHans de Goede 		(priv->y_max * (2 * y_high.start_bit + y_high.num_bits - 1)) /
4517a9f73e7SKevin Cernekee 		(2 * (priv->y_bits - 1));
45201ce661fSSeth Forshee 
4534dd26573SHans de Goede 	/* bottom-left corner */
4544dd26573SHans de Goede 	corner[3].x =
4554dd26573SHans de Goede 		(priv->x_max * (2 * x_low.start_bit + x_low.num_bits - 1)) /
4564dd26573SHans de Goede 		(2 * (priv->x_bits - 1));
4574dd26573SHans de Goede 	corner[3].y =
45801ce661fSSeth Forshee 		(priv->y_max * (2 * y_high.start_bit + y_high.num_bits - 1)) /
45901ce661fSSeth Forshee 		(2 * (priv->y_bits - 1));
46001ce661fSSeth Forshee 
461dccf1dd8SHans de Goede 	/* x-bitmap order is reversed on v5 touchpads  */
462dccf1dd8SHans de Goede 	if (priv->proto_version == ALPS_PROTO_V5) {
463dccf1dd8SHans de Goede 		for (i = 0; i < 4; i++)
464dccf1dd8SHans de Goede 			corner[i].x = priv->x_max - corner[i].x;
46540e8f53bSHans de Goede 	}
46640e8f53bSHans de Goede 
467dccf1dd8SHans de Goede 	/* y-bitmap order is reversed on v3 and v4 touchpads  */
468dccf1dd8SHans de Goede 	if (priv->proto_version == ALPS_PROTO_V3 ||
469dccf1dd8SHans de Goede 	    priv->proto_version == ALPS_PROTO_V4) {
4704dd26573SHans de Goede 		for (i = 0; i < 4; i++)
4714dd26573SHans de Goede 			corner[i].y = priv->y_max - corner[i].y;
47201ce661fSSeth Forshee 	}
47301ce661fSSeth Forshee 
4744dd26573SHans de Goede 	/*
4754dd26573SHans de Goede 	 * We only select a corner for the second touch once per 2 finger
4764dd26573SHans de Goede 	 * touch sequence to avoid the chosen corner (and thus the coordinates)
4774dd26573SHans de Goede 	 * jumping around when the first touch is in the middle.
4784dd26573SHans de Goede 	 */
4794dd26573SHans de Goede 	if (priv->second_touch == -1) {
4804dd26573SHans de Goede 		/* Find corner closest to our st coordinates */
4814dd26573SHans de Goede 		closest = 0x7fffffff;
4824dd26573SHans de Goede 		for (i = 0; i < 4; i++) {
4834dd26573SHans de Goede 			int dx = fields->st.x - corner[i].x;
4844dd26573SHans de Goede 			int dy = fields->st.y - corner[i].y;
4854dd26573SHans de Goede 			int distance = dx * dx + dy * dy;
4864dd26573SHans de Goede 
4874dd26573SHans de Goede 			if (distance < closest) {
4884dd26573SHans de Goede 				priv->second_touch = i;
4894dd26573SHans de Goede 				closest = distance;
4904dd26573SHans de Goede 			}
4914dd26573SHans de Goede 		}
4924dd26573SHans de Goede 		/* And select the opposite corner to use for the 2nd touch */
4934dd26573SHans de Goede 		priv->second_touch = (priv->second_touch + 2) % 4;
4944dd26573SHans de Goede 	}
4954dd26573SHans de Goede 
4964dd26573SHans de Goede 	fields->mt[0] = fields->st;
4974dd26573SHans de Goede 	fields->mt[1] = corner[priv->second_touch];
4984dd26573SHans de Goede 
49901ce661fSSeth Forshee 	return fingers;
50001ce661fSSeth Forshee }
50101ce661fSSeth Forshee 
alps_set_slot(struct input_dev * dev,int slot,int x,int y)502cdf333efSHans de Goede static void alps_set_slot(struct input_dev *dev, int slot, int x, int y)
50301ce661fSSeth Forshee {
50401ce661fSSeth Forshee 	input_mt_slot(dev, slot);
505cdf333efSHans de Goede 	input_mt_report_slot_state(dev, MT_TOOL_FINGER, true);
50601ce661fSSeth Forshee 	input_report_abs(dev, ABS_MT_POSITION_X, x);
50701ce661fSSeth Forshee 	input_report_abs(dev, ABS_MT_POSITION_Y, y);
50801ce661fSSeth Forshee }
50901ce661fSSeth Forshee 
alps_report_mt_data(struct psmouse * psmouse,int n)510cdf333efSHans de Goede static void alps_report_mt_data(struct psmouse *psmouse, int n)
51101ce661fSSeth Forshee {
51202d04254SHans de Goede 	struct alps_data *priv = psmouse->private;
51302d04254SHans de Goede 	struct input_dev *dev = psmouse->dev;
51402d04254SHans de Goede 	struct alps_fields *f = &priv->f;
515cdf333efSHans de Goede 	int i, slot[MAX_TOUCHES];
51602d04254SHans de Goede 
517448c7f38SHenrik Rydberg 	input_mt_assign_slots(dev, slot, f->mt, n, 0);
518cdf333efSHans de Goede 	for (i = 0; i < n; i++)
519cdf333efSHans de Goede 		alps_set_slot(dev, slot[i], f->mt[i].x, f->mt[i].y);
520cdf333efSHans de Goede 
521cdf333efSHans de Goede 	input_mt_sync_frame(dev);
52201ce661fSSeth Forshee }
52301ce661fSSeth Forshee 
alps_report_semi_mt_data(struct psmouse * psmouse,int fingers)52468c21870SHans de Goede static void alps_report_semi_mt_data(struct psmouse *psmouse, int fingers)
52568c21870SHans de Goede {
52668c21870SHans de Goede 	struct alps_data *priv = psmouse->private;
52768c21870SHans de Goede 	struct input_dev *dev = psmouse->dev;
52868c21870SHans de Goede 	struct alps_fields *f = &priv->f;
52968c21870SHans de Goede 
53068c21870SHans de Goede 	/* Use st data when we don't have mt data */
53168c21870SHans de Goede 	if (fingers < 2) {
53268c21870SHans de Goede 		f->mt[0].x = f->st.x;
53368c21870SHans de Goede 		f->mt[0].y = f->st.y;
53468c21870SHans de Goede 		fingers = f->pressure > 0 ? 1 : 0;
5354dd26573SHans de Goede 		priv->second_touch = -1;
53668c21870SHans de Goede 	}
53768c21870SHans de Goede 
5381662c033SHans de Goede 	if (fingers >= 1)
5391662c033SHans de Goede 		alps_set_slot(dev, 0, f->mt[0].x, f->mt[0].y);
5401662c033SHans de Goede 	if (fingers >= 2)
5411662c033SHans de Goede 		alps_set_slot(dev, 1, f->mt[1].x, f->mt[1].y);
5421662c033SHans de Goede 	input_mt_sync_frame(dev);
54368c21870SHans de Goede 
54468c21870SHans de Goede 	input_mt_report_finger_count(dev, fingers);
54568c21870SHans de Goede 
54668c21870SHans de Goede 	input_report_key(dev, BTN_LEFT, f->left);
54768c21870SHans de Goede 	input_report_key(dev, BTN_RIGHT, f->right);
54868c21870SHans de Goede 	input_report_key(dev, BTN_MIDDLE, f->middle);
54968c21870SHans de Goede 
55068c21870SHans de Goede 	input_report_abs(dev, ABS_PRESSURE, f->pressure);
55168c21870SHans de Goede 
55268c21870SHans de Goede 	input_sync(dev);
55368c21870SHans de Goede }
55468c21870SHans de Goede 
alps_process_trackstick_packet_v3(struct psmouse * psmouse)55525bded7cSSeth Forshee static void alps_process_trackstick_packet_v3(struct psmouse *psmouse)
55625bded7cSSeth Forshee {
55725bded7cSSeth Forshee 	struct alps_data *priv = psmouse->private;
55825bded7cSSeth Forshee 	unsigned char *packet = psmouse->packet;
55925bded7cSSeth Forshee 	struct input_dev *dev = priv->dev2;
56025bded7cSSeth Forshee 	int x, y, z, left, right, middle;
56125bded7cSSeth Forshee 
56234412ba2SPali Rohár 	/* It should be a DualPoint when received trackstick packet */
56334412ba2SPali Rohár 	if (!(priv->flags & ALPS_DUALPOINT)) {
56434412ba2SPali Rohár 		psmouse_warn(psmouse,
56534412ba2SPali Rohár 			     "Rejected trackstick packet from non DualPoint device");
56634412ba2SPali Rohár 		return;
56734412ba2SPali Rohár 	}
56834412ba2SPali Rohár 
56925bded7cSSeth Forshee 	/* Sanity check packet */
57025bded7cSSeth Forshee 	if (!(packet[0] & 0x40)) {
57125bded7cSSeth Forshee 		psmouse_dbg(psmouse, "Bad trackstick packet, discarding\n");
57225bded7cSSeth Forshee 		return;
57325bded7cSSeth Forshee 	}
57425bded7cSSeth Forshee 
57525bded7cSSeth Forshee 	/*
57625bded7cSSeth Forshee 	 * There's a special packet that seems to indicate the end
57725bded7cSSeth Forshee 	 * of a stream of trackstick data. Filter these out.
57825bded7cSSeth Forshee 	 */
57925bded7cSSeth Forshee 	if (packet[1] == 0x7f && packet[2] == 0x7f && packet[4] == 0x7f)
58025bded7cSSeth Forshee 		return;
58125bded7cSSeth Forshee 
58225bded7cSSeth Forshee 	x = (s8)(((packet[0] & 0x20) << 2) | (packet[1] & 0x7f));
58325bded7cSSeth Forshee 	y = (s8)(((packet[0] & 0x10) << 3) | (packet[2] & 0x7f));
5849c71b2c5SPali Rohár 	z = packet[4] & 0x7f;
58525bded7cSSeth Forshee 
58625bded7cSSeth Forshee 	/*
58725bded7cSSeth Forshee 	 * The x and y values tend to be quite large, and when used
58825bded7cSSeth Forshee 	 * alone the trackstick is difficult to use. Scale them down
58925bded7cSSeth Forshee 	 * to compensate.
59025bded7cSSeth Forshee 	 */
59125bded7cSSeth Forshee 	x /= 8;
59225bded7cSSeth Forshee 	y /= 8;
59325bded7cSSeth Forshee 
59425bded7cSSeth Forshee 	input_report_rel(dev, REL_X, x);
59525bded7cSSeth Forshee 	input_report_rel(dev, REL_Y, -y);
5964621c966SPali Rohár 	input_report_abs(dev, ABS_PRESSURE, z);
59725bded7cSSeth Forshee 
59825bded7cSSeth Forshee 	/*
59925bded7cSSeth Forshee 	 * Most ALPS models report the trackstick buttons in the touchpad
60025bded7cSSeth Forshee 	 * packets, but a few report them here. No reliable way has been
60125bded7cSSeth Forshee 	 * found to differentiate between the models upfront, so we enable
60225bded7cSSeth Forshee 	 * the quirk in response to seeing a button press in the trackstick
60325bded7cSSeth Forshee 	 * packet.
60425bded7cSSeth Forshee 	 */
60525bded7cSSeth Forshee 	left = packet[3] & 0x01;
60625bded7cSSeth Forshee 	right = packet[3] & 0x02;
60725bded7cSSeth Forshee 	middle = packet[3] & 0x04;
60825bded7cSSeth Forshee 
60925bded7cSSeth Forshee 	if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) &&
61025bded7cSSeth Forshee 	    (left || right || middle))
61125bded7cSSeth Forshee 		priv->quirks |= ALPS_QUIRK_TRACKSTICK_BUTTONS;
61225bded7cSSeth Forshee 
61325bded7cSSeth Forshee 	if (priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) {
61425bded7cSSeth Forshee 		input_report_key(dev, BTN_LEFT, left);
61525bded7cSSeth Forshee 		input_report_key(dev, BTN_RIGHT, right);
61625bded7cSSeth Forshee 		input_report_key(dev, BTN_MIDDLE, middle);
61725bded7cSSeth Forshee 	}
61825bded7cSSeth Forshee 
61925bded7cSSeth Forshee 	input_sync(dev);
62025bded7cSSeth Forshee 	return;
62125bded7cSSeth Forshee }
62225bded7cSSeth Forshee 
alps_decode_buttons_v3(struct alps_fields * f,unsigned char * p)623f85e5001SKevin Cernekee static void alps_decode_buttons_v3(struct alps_fields *f, unsigned char *p)
624f85e5001SKevin Cernekee {
625f85e5001SKevin Cernekee 	f->left = !!(p[3] & 0x01);
626f85e5001SKevin Cernekee 	f->right = !!(p[3] & 0x02);
627f85e5001SKevin Cernekee 	f->middle = !!(p[3] & 0x04);
628f85e5001SKevin Cernekee 
629f85e5001SKevin Cernekee 	f->ts_left = !!(p[3] & 0x10);
630f85e5001SKevin Cernekee 	f->ts_right = !!(p[3] & 0x20);
631f85e5001SKevin Cernekee 	f->ts_middle = !!(p[3] & 0x40);
632f85e5001SKevin Cernekee }
633f85e5001SKevin Cernekee 
alps_decode_pinnacle(struct alps_fields * f,unsigned char * p,struct psmouse * psmouse)63438c11eaaSHans de Goede static int alps_decode_pinnacle(struct alps_fields *f, unsigned char *p,
635ee65d4b3SYunkang Tang 				 struct psmouse *psmouse)
636f85e5001SKevin Cernekee {
637f85e5001SKevin Cernekee 	f->first_mp = !!(p[4] & 0x40);
638f85e5001SKevin Cernekee 	f->is_mp = !!(p[0] & 0x40);
639f85e5001SKevin Cernekee 
640a839cd57SHans de Goede 	if (f->is_mp) {
641f85e5001SKevin Cernekee 		f->fingers = (p[5] & 0x3) + 1;
642f85e5001SKevin Cernekee 		f->x_map = ((p[4] & 0x7e) << 8) |
643f85e5001SKevin Cernekee 			   ((p[1] & 0x7f) << 2) |
644f85e5001SKevin Cernekee 			   ((p[0] & 0x30) >> 4);
645f85e5001SKevin Cernekee 		f->y_map = ((p[3] & 0x70) << 4) |
646f85e5001SKevin Cernekee 			   ((p[2] & 0x7f) << 1) |
647f85e5001SKevin Cernekee 			   (p[4] & 0x01);
648a839cd57SHans de Goede 	} else {
64902d04254SHans de Goede 		f->st.x = ((p[1] & 0x7f) << 4) | ((p[4] & 0x30) >> 2) |
650f85e5001SKevin Cernekee 		       ((p[0] & 0x30) >> 4);
65102d04254SHans de Goede 		f->st.y = ((p[2] & 0x7f) << 4) | (p[4] & 0x0f);
65202d04254SHans de Goede 		f->pressure = p[5] & 0x7f;
653f85e5001SKevin Cernekee 
654f85e5001SKevin Cernekee 		alps_decode_buttons_v3(f, p);
655a839cd57SHans de Goede 	}
65638c11eaaSHans de Goede 
65738c11eaaSHans de Goede 	return 0;
658f85e5001SKevin Cernekee }
659f85e5001SKevin Cernekee 
alps_decode_rushmore(struct alps_fields * f,unsigned char * p,struct psmouse * psmouse)66038c11eaaSHans de Goede static int alps_decode_rushmore(struct alps_fields *f, unsigned char *p,
661ee65d4b3SYunkang Tang 				 struct psmouse *psmouse)
6621302bac3SKevin Cernekee {
663aab9cf7bSHans de Goede 	f->first_mp = !!(p[4] & 0x40);
664f105e34aSYunkang Tang 	f->is_mp = !!(p[5] & 0x40);
665aab9cf7bSHans de Goede 
666a839cd57SHans de Goede 	if (f->is_mp) {
667f105e34aSYunkang Tang 		f->fingers = max((p[5] & 0x3), ((p[5] >> 2) & 0x3)) + 1;
668aab9cf7bSHans de Goede 		f->x_map = ((p[5] & 0x10) << 11) |
669aab9cf7bSHans de Goede 			   ((p[4] & 0x7e) << 8) |
670aab9cf7bSHans de Goede 			   ((p[1] & 0x7f) << 2) |
671aab9cf7bSHans de Goede 			   ((p[0] & 0x30) >> 4);
672aab9cf7bSHans de Goede 		f->y_map = ((p[5] & 0x20) << 6) |
673aab9cf7bSHans de Goede 			   ((p[3] & 0x70) << 4) |
674aab9cf7bSHans de Goede 			   ((p[2] & 0x7f) << 1) |
675aab9cf7bSHans de Goede 			   (p[4] & 0x01);
676a839cd57SHans de Goede 	} else {
677aab9cf7bSHans de Goede 		f->st.x = ((p[1] & 0x7f) << 4) | ((p[4] & 0x30) >> 2) |
678aab9cf7bSHans de Goede 		       ((p[0] & 0x30) >> 4);
679aab9cf7bSHans de Goede 		f->st.y = ((p[2] & 0x7f) << 4) | (p[4] & 0x0f);
680aab9cf7bSHans de Goede 		f->pressure = p[5] & 0x7f;
681aab9cf7bSHans de Goede 
682aab9cf7bSHans de Goede 		alps_decode_buttons_v3(f, p);
683a839cd57SHans de Goede 	}
68438c11eaaSHans de Goede 
68538c11eaaSHans de Goede 	return 0;
6861302bac3SKevin Cernekee }
6871302bac3SKevin Cernekee 
alps_decode_dolphin(struct alps_fields * f,unsigned char * p,struct psmouse * psmouse)68838c11eaaSHans de Goede static int alps_decode_dolphin(struct alps_fields *f, unsigned char *p,
689ee65d4b3SYunkang Tang 				struct psmouse *psmouse)
69075af9e56SDave Turvene {
691ee65d4b3SYunkang Tang 	u64 palm_data = 0;
692ee65d4b3SYunkang Tang 	struct alps_data *priv = psmouse->private;
693ee65d4b3SYunkang Tang 
69475af9e56SDave Turvene 	f->first_mp = !!(p[0] & 0x02);
69575af9e56SDave Turvene 	f->is_mp = !!(p[0] & 0x20);
69675af9e56SDave Turvene 
697ee65d4b3SYunkang Tang 	if (!f->is_mp) {
69802d04254SHans de Goede 		f->st.x = ((p[1] & 0x7f) | ((p[4] & 0x0f) << 7));
69902d04254SHans de Goede 		f->st.y = ((p[2] & 0x7f) | ((p[4] & 0xf0) << 3));
70002d04254SHans de Goede 		f->pressure = (p[0] & 4) ? 0 : p[5] & 0x7f;
70175af9e56SDave Turvene 		alps_decode_buttons_v3(f, p);
702ee65d4b3SYunkang Tang 	} else {
703ee65d4b3SYunkang Tang 		f->fingers = ((p[0] & 0x6) >> 1 |
704ee65d4b3SYunkang Tang 		     (p[0] & 0x10) >> 2);
705ee65d4b3SYunkang Tang 
706ee65d4b3SYunkang Tang 		palm_data = (p[1] & 0x7f) |
707ee65d4b3SYunkang Tang 			    ((p[2] & 0x7f) << 7) |
708ee65d4b3SYunkang Tang 			    ((p[4] & 0x7f) << 14) |
709ee65d4b3SYunkang Tang 			    ((p[5] & 0x7f) << 21) |
710ee65d4b3SYunkang Tang 			    ((p[3] & 0x07) << 28) |
711ee65d4b3SYunkang Tang 			    (((u64)p[3] & 0x70) << 27) |
712ee65d4b3SYunkang Tang 			    (((u64)p[0] & 0x01) << 34);
713ee65d4b3SYunkang Tang 
714ee65d4b3SYunkang Tang 		/* Y-profile is stored in P(0) to p(n-1), n = y_bits; */
715ee65d4b3SYunkang Tang 		f->y_map = palm_data & (BIT(priv->y_bits) - 1);
716ee65d4b3SYunkang Tang 
717ee65d4b3SYunkang Tang 		/* X-profile is stored in p(n) to p(n+m-1), m = x_bits; */
718ee65d4b3SYunkang Tang 		f->x_map = (palm_data >> priv->y_bits) &
719ee65d4b3SYunkang Tang 			   (BIT(priv->x_bits) - 1);
720ee65d4b3SYunkang Tang 	}
72138c11eaaSHans de Goede 
72238c11eaaSHans de Goede 	return 0;
72375af9e56SDave Turvene }
72475af9e56SDave Turvene 
alps_process_touchpad_packet_v3_v5(struct psmouse * psmouse)725ee65d4b3SYunkang Tang static void alps_process_touchpad_packet_v3_v5(struct psmouse *psmouse)
72625bded7cSSeth Forshee {
72725bded7cSSeth Forshee 	struct alps_data *priv = psmouse->private;
72825bded7cSSeth Forshee 	unsigned char *packet = psmouse->packet;
72925bded7cSSeth Forshee 	struct input_dev *dev2 = priv->dev2;
73002d04254SHans de Goede 	struct alps_fields *f = &priv->f;
73102d04254SHans de Goede 	int fingers = 0;
732f85e5001SKevin Cernekee 
73302d04254SHans de Goede 	memset(f, 0, sizeof(*f));
73402d04254SHans de Goede 
73502d04254SHans de Goede 	priv->decode_fields(f, packet, psmouse);
73625bded7cSSeth Forshee 
73725bded7cSSeth Forshee 	/*
73801ce661fSSeth Forshee 	 * There's no single feature of touchpad position and bitmap packets
73901ce661fSSeth Forshee 	 * that can be used to distinguish between them. We rely on the fact
74001ce661fSSeth Forshee 	 * that a bitmap packet should always follow a position packet with
74101ce661fSSeth Forshee 	 * bit 6 of packet[4] set.
74225bded7cSSeth Forshee 	 */
74325bded7cSSeth Forshee 	if (priv->multi_packet) {
74425bded7cSSeth Forshee 		/*
74525bded7cSSeth Forshee 		 * Sometimes a position packet will indicate a multi-packet
74625bded7cSSeth Forshee 		 * sequence, but then what follows is another position
74725bded7cSSeth Forshee 		 * packet. Check for this, and when it happens process the
74825bded7cSSeth Forshee 		 * position packet as usual.
74925bded7cSSeth Forshee 		 */
75002d04254SHans de Goede 		if (f->is_mp) {
75102d04254SHans de Goede 			fingers = f->fingers;
75244b77f38SHans de Goede 			/*
75344b77f38SHans de Goede 			 * Bitmap processing uses position packet's coordinate
75444b77f38SHans de Goede 			 * data, so we need to do decode it first.
75544b77f38SHans de Goede 			 */
75644b77f38SHans de Goede 			priv->decode_fields(f, priv->multi_data, psmouse);
75702d04254SHans de Goede 			if (alps_process_bitmap(priv, f) == 0)
75820bea68bSHans de Goede 				fingers = 0; /* Use st data */
75901ce661fSSeth Forshee 		} else {
76001ce661fSSeth Forshee 			priv->multi_packet = 0;
76125bded7cSSeth Forshee 		}
76225bded7cSSeth Forshee 	}
76325bded7cSSeth Forshee 
76401ce661fSSeth Forshee 	/*
76501ce661fSSeth Forshee 	 * Bit 6 of byte 0 is not usually set in position packets. The only
76601ce661fSSeth Forshee 	 * times it seems to be set is in situations where the data is
76701ce661fSSeth Forshee 	 * suspect anyway, e.g. a palm resting flat on the touchpad. Given
76801ce661fSSeth Forshee 	 * this combined with the fact that this bit is useful for filtering
76901ce661fSSeth Forshee 	 * out misidentified bitmap packets, we reject anything with this
77001ce661fSSeth Forshee 	 * bit set.
77101ce661fSSeth Forshee 	 */
77202d04254SHans de Goede 	if (f->is_mp)
77301ce661fSSeth Forshee 		return;
77401ce661fSSeth Forshee 
77502d04254SHans de Goede 	if (!priv->multi_packet && f->first_mp) {
77625bded7cSSeth Forshee 		priv->multi_packet = 1;
77701ce661fSSeth Forshee 		memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
77801ce661fSSeth Forshee 		return;
77901ce661fSSeth Forshee 	}
78001ce661fSSeth Forshee 
78125bded7cSSeth Forshee 	priv->multi_packet = 0;
78225bded7cSSeth Forshee 
78325bded7cSSeth Forshee 	/*
78425bded7cSSeth Forshee 	 * Sometimes the hardware sends a single packet with z = 0
78525bded7cSSeth Forshee 	 * in the middle of a stream. Real releases generate packets
78625bded7cSSeth Forshee 	 * with x, y, and z all zero, so these seem to be flukes.
78725bded7cSSeth Forshee 	 * Ignore them.
78825bded7cSSeth Forshee 	 */
78902d04254SHans de Goede 	if (f->st.x && f->st.y && !f->pressure)
79025bded7cSSeth Forshee 		return;
79125bded7cSSeth Forshee 
79268c21870SHans de Goede 	alps_report_semi_mt_data(psmouse, fingers);
79325bded7cSSeth Forshee 
79434412ba2SPali Rohár 	if ((priv->flags & ALPS_DUALPOINT) &&
79534412ba2SPali Rohár 	    !(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS)) {
79602d04254SHans de Goede 		input_report_key(dev2, BTN_LEFT, f->ts_left);
79702d04254SHans de Goede 		input_report_key(dev2, BTN_RIGHT, f->ts_right);
79802d04254SHans de Goede 		input_report_key(dev2, BTN_MIDDLE, f->ts_middle);
79925bded7cSSeth Forshee 		input_sync(dev2);
80025bded7cSSeth Forshee 	}
80125bded7cSSeth Forshee }
80225bded7cSSeth Forshee 
alps_process_packet_v3(struct psmouse * psmouse)80325bded7cSSeth Forshee static void alps_process_packet_v3(struct psmouse *psmouse)
80425bded7cSSeth Forshee {
80525bded7cSSeth Forshee 	unsigned char *packet = psmouse->packet;
80625bded7cSSeth Forshee 
80725bded7cSSeth Forshee 	/*
80825bded7cSSeth Forshee 	 * v3 protocol packets come in three types, two representing
80925bded7cSSeth Forshee 	 * touchpad data and one representing trackstick data.
81025bded7cSSeth Forshee 	 * Trackstick packets seem to be distinguished by always
81125bded7cSSeth Forshee 	 * having 0x3f in the last byte. This value has never been
81225bded7cSSeth Forshee 	 * observed in the last byte of either of the other types
81325bded7cSSeth Forshee 	 * of packets.
81425bded7cSSeth Forshee 	 */
81525bded7cSSeth Forshee 	if (packet[5] == 0x3f) {
81625bded7cSSeth Forshee 		alps_process_trackstick_packet_v3(psmouse);
81725bded7cSSeth Forshee 		return;
81825bded7cSSeth Forshee 	}
81925bded7cSSeth Forshee 
820ee65d4b3SYunkang Tang 	alps_process_touchpad_packet_v3_v5(psmouse);
82125bded7cSSeth Forshee }
82225bded7cSSeth Forshee 
alps_process_packet_v6(struct psmouse * psmouse)82395f75e91SYunkang Tang static void alps_process_packet_v6(struct psmouse *psmouse)
82495f75e91SYunkang Tang {
82595f75e91SYunkang Tang 	struct alps_data *priv = psmouse->private;
82695f75e91SYunkang Tang 	unsigned char *packet = psmouse->packet;
82795f75e91SYunkang Tang 	struct input_dev *dev = psmouse->dev;
82895f75e91SYunkang Tang 	struct input_dev *dev2 = priv->dev2;
8291ef85805SDmitry Torokhov 	int x, y, z;
83095f75e91SYunkang Tang 
83195f75e91SYunkang Tang 	/*
83295f75e91SYunkang Tang 	 * We can use Byte5 to distinguish if the packet is from Touchpad
83395f75e91SYunkang Tang 	 * or Trackpoint.
83495f75e91SYunkang Tang 	 * Touchpad:	0 - 0x7E
83595f75e91SYunkang Tang 	 * Trackpoint:	0x7F
83695f75e91SYunkang Tang 	 */
83795f75e91SYunkang Tang 	if (packet[5] == 0x7F) {
83895f75e91SYunkang Tang 		/* It should be a DualPoint when received Trackpoint packet */
83934412ba2SPali Rohár 		if (!(priv->flags & ALPS_DUALPOINT)) {
84034412ba2SPali Rohár 			psmouse_warn(psmouse,
84134412ba2SPali Rohár 				     "Rejected trackstick packet from non DualPoint device");
84295f75e91SYunkang Tang 			return;
84334412ba2SPali Rohár 		}
84495f75e91SYunkang Tang 
84595f75e91SYunkang Tang 		/* Trackpoint packet */
84695f75e91SYunkang Tang 		x = packet[1] | ((packet[3] & 0x20) << 2);
84795f75e91SYunkang Tang 		y = packet[2] | ((packet[3] & 0x40) << 1);
84895f75e91SYunkang Tang 		z = packet[4];
84995f75e91SYunkang Tang 
85095f75e91SYunkang Tang 		/* To prevent the cursor jump when finger lifted */
85195f75e91SYunkang Tang 		if (x == 0x7F && y == 0x7F && z == 0x7F)
85295f75e91SYunkang Tang 			x = y = z = 0;
85395f75e91SYunkang Tang 
85495f75e91SYunkang Tang 		/* Divide 4 since trackpoint's speed is too fast */
855*754ff506Smsizanoen 		input_report_rel(dev2, REL_X, (s8)x / 4);
856*754ff506Smsizanoen 		input_report_rel(dev2, REL_Y, -((s8)y / 4));
85795f75e91SYunkang Tang 
8581ef85805SDmitry Torokhov 		psmouse_report_standard_buttons(dev2, packet[3]);
85995f75e91SYunkang Tang 
86095f75e91SYunkang Tang 		input_sync(dev2);
86195f75e91SYunkang Tang 		return;
86295f75e91SYunkang Tang 	}
86395f75e91SYunkang Tang 
86495f75e91SYunkang Tang 	/* Touchpad packet */
86595f75e91SYunkang Tang 	x = packet[1] | ((packet[3] & 0x78) << 4);
86695f75e91SYunkang Tang 	y = packet[2] | ((packet[4] & 0x78) << 4);
86795f75e91SYunkang Tang 	z = packet[5];
86895f75e91SYunkang Tang 
86995f75e91SYunkang Tang 	if (z > 30)
87095f75e91SYunkang Tang 		input_report_key(dev, BTN_TOUCH, 1);
87195f75e91SYunkang Tang 	if (z < 25)
87295f75e91SYunkang Tang 		input_report_key(dev, BTN_TOUCH, 0);
87395f75e91SYunkang Tang 
87495f75e91SYunkang Tang 	if (z > 0) {
87595f75e91SYunkang Tang 		input_report_abs(dev, ABS_X, x);
87695f75e91SYunkang Tang 		input_report_abs(dev, ABS_Y, y);
87795f75e91SYunkang Tang 	}
87895f75e91SYunkang Tang 
87995f75e91SYunkang Tang 	input_report_abs(dev, ABS_PRESSURE, z);
88095f75e91SYunkang Tang 	input_report_key(dev, BTN_TOOL_FINGER, z > 0);
88195f75e91SYunkang Tang 
88295f75e91SYunkang Tang 	/* v6 touchpad does not have middle button */
8831ef85805SDmitry Torokhov 	packet[3] &= ~BIT(2);
8841ef85805SDmitry Torokhov 	psmouse_report_standard_buttons(dev2, packet[3]);
88595f75e91SYunkang Tang 
88695f75e91SYunkang Tang 	input_sync(dev);
88795f75e91SYunkang Tang }
88895f75e91SYunkang Tang 
alps_process_packet_v4(struct psmouse * psmouse)88925bded7cSSeth Forshee static void alps_process_packet_v4(struct psmouse *psmouse)
89025bded7cSSeth Forshee {
8913b7e09faSGeorge Pantalos 	struct alps_data *priv = psmouse->private;
89225bded7cSSeth Forshee 	unsigned char *packet = psmouse->packet;
89302d04254SHans de Goede 	struct alps_fields *f = &priv->f;
89468c21870SHans de Goede 	int offset;
8953b7e09faSGeorge Pantalos 
8963b7e09faSGeorge Pantalos 	/*
8973b7e09faSGeorge Pantalos 	 * v4 has a 6-byte encoding for bitmap data, but this data is
8983b7e09faSGeorge Pantalos 	 * broken up between 3 normal packets. Use priv->multi_packet to
8993b7e09faSGeorge Pantalos 	 * track our position in the bitmap packet.
9003b7e09faSGeorge Pantalos 	 */
9013b7e09faSGeorge Pantalos 	if (packet[6] & 0x40) {
9023b7e09faSGeorge Pantalos 		/* sync, reset position */
9033b7e09faSGeorge Pantalos 		priv->multi_packet = 0;
9043b7e09faSGeorge Pantalos 	}
9053b7e09faSGeorge Pantalos 
9063b7e09faSGeorge Pantalos 	if (WARN_ON_ONCE(priv->multi_packet > 2))
9073b7e09faSGeorge Pantalos 		return;
9083b7e09faSGeorge Pantalos 
9093b7e09faSGeorge Pantalos 	offset = 2 * priv->multi_packet;
9103b7e09faSGeorge Pantalos 	priv->multi_data[offset] = packet[6];
9113b7e09faSGeorge Pantalos 	priv->multi_data[offset + 1] = packet[7];
9123b7e09faSGeorge Pantalos 
91344b77f38SHans de Goede 	f->left = !!(packet[4] & 0x01);
91444b77f38SHans de Goede 	f->right = !!(packet[4] & 0x02);
91544b77f38SHans de Goede 
91644b77f38SHans de Goede 	f->st.x = ((packet[1] & 0x7f) << 4) | ((packet[3] & 0x30) >> 2) |
91744b77f38SHans de Goede 		  ((packet[0] & 0x30) >> 4);
91844b77f38SHans de Goede 	f->st.y = ((packet[2] & 0x7f) << 4) | (packet[3] & 0x0f);
91944b77f38SHans de Goede 	f->pressure = packet[5] & 0x7f;
92044b77f38SHans de Goede 
9213b7e09faSGeorge Pantalos 	if (++priv->multi_packet > 2) {
9223b7e09faSGeorge Pantalos 		priv->multi_packet = 0;
9233b7e09faSGeorge Pantalos 
92402d04254SHans de Goede 		f->x_map = ((priv->multi_data[2] & 0x1f) << 10) |
9253b7e09faSGeorge Pantalos 			   ((priv->multi_data[3] & 0x60) << 3) |
9263b7e09faSGeorge Pantalos 			   ((priv->multi_data[0] & 0x3f) << 2) |
9273b7e09faSGeorge Pantalos 			   ((priv->multi_data[1] & 0x60) >> 5);
92802d04254SHans de Goede 		f->y_map = ((priv->multi_data[5] & 0x01) << 10) |
9293b7e09faSGeorge Pantalos 			   ((priv->multi_data[3] & 0x1f) << 5) |
9303b7e09faSGeorge Pantalos 			    (priv->multi_data[1] & 0x1f);
9313b7e09faSGeorge Pantalos 
93202d04254SHans de Goede 		f->fingers = alps_process_bitmap(priv, f);
9333b7e09faSGeorge Pantalos 	}
93425bded7cSSeth Forshee 
93568c21870SHans de Goede 	alps_report_semi_mt_data(psmouse, f->fingers);
93625bded7cSSeth Forshee }
93725bded7cSSeth Forshee 
alps_is_valid_package_v7(struct psmouse * psmouse)9383808843cSYunkang Tang static bool alps_is_valid_package_v7(struct psmouse *psmouse)
9393808843cSYunkang Tang {
9403808843cSYunkang Tang 	switch (psmouse->pktcnt) {
9413808843cSYunkang Tang 	case 3:
9423808843cSYunkang Tang 		return (psmouse->packet[2] & 0x40) == 0x40;
9433808843cSYunkang Tang 	case 4:
9443808843cSYunkang Tang 		return (psmouse->packet[3] & 0x48) == 0x48;
9453808843cSYunkang Tang 	case 6:
9463808843cSYunkang Tang 		return (psmouse->packet[5] & 0x40) == 0x00;
9473808843cSYunkang Tang 	}
9483808843cSYunkang Tang 	return true;
9493808843cSYunkang Tang }
9503808843cSYunkang Tang 
alps_get_packet_id_v7(char * byte)9513808843cSYunkang Tang static unsigned char alps_get_packet_id_v7(char *byte)
9523808843cSYunkang Tang {
9533808843cSYunkang Tang 	unsigned char packet_id;
9543808843cSYunkang Tang 
9553808843cSYunkang Tang 	if (byte[4] & 0x40)
9563808843cSYunkang Tang 		packet_id = V7_PACKET_ID_TWO;
9573808843cSYunkang Tang 	else if (byte[4] & 0x01)
9583808843cSYunkang Tang 		packet_id = V7_PACKET_ID_MULTI;
9593808843cSYunkang Tang 	else if ((byte[0] & 0x10) && !(byte[4] & 0x43))
9603808843cSYunkang Tang 		packet_id = V7_PACKET_ID_NEW;
9613808843cSYunkang Tang 	else if (byte[1] == 0x00 && byte[4] == 0x00)
9623808843cSYunkang Tang 		packet_id = V7_PACKET_ID_IDLE;
9633808843cSYunkang Tang 	else
9643808843cSYunkang Tang 		packet_id = V7_PACKET_ID_UNKNOWN;
9653808843cSYunkang Tang 
9663808843cSYunkang Tang 	return packet_id;
9673808843cSYunkang Tang }
9683808843cSYunkang Tang 
alps_get_finger_coordinate_v7(struct input_mt_pos * mt,unsigned char * pkt,unsigned char pkt_id)9693808843cSYunkang Tang static void alps_get_finger_coordinate_v7(struct input_mt_pos *mt,
9703808843cSYunkang Tang 					  unsigned char *pkt,
9713808843cSYunkang Tang 					  unsigned char pkt_id)
9723808843cSYunkang Tang {
9733808843cSYunkang Tang 	mt[0].x = ((pkt[2] & 0x80) << 4);
9743808843cSYunkang Tang 	mt[0].x |= ((pkt[2] & 0x3F) << 5);
9753808843cSYunkang Tang 	mt[0].x |= ((pkt[3] & 0x30) >> 1);
9763808843cSYunkang Tang 	mt[0].x |= (pkt[3] & 0x07);
9773808843cSYunkang Tang 	mt[0].y = (pkt[1] << 3) | (pkt[0] & 0x07);
9783808843cSYunkang Tang 
9793808843cSYunkang Tang 	mt[1].x = ((pkt[3] & 0x80) << 4);
9803808843cSYunkang Tang 	mt[1].x |= ((pkt[4] & 0x80) << 3);
9813808843cSYunkang Tang 	mt[1].x |= ((pkt[4] & 0x3F) << 4);
9823808843cSYunkang Tang 	mt[1].y = ((pkt[5] & 0x80) << 3);
9833808843cSYunkang Tang 	mt[1].y |= ((pkt[5] & 0x3F) << 4);
9843808843cSYunkang Tang 
9853808843cSYunkang Tang 	switch (pkt_id) {
9863808843cSYunkang Tang 	case V7_PACKET_ID_TWO:
9873808843cSYunkang Tang 		mt[1].x &= ~0x000F;
9883808843cSYunkang Tang 		mt[1].y |= 0x000F;
9892896a27fSBhaskar Chowdhury 		/* Detect false-positive touches where x & y report max value */
99072eceab7SHans de Goede 		if (mt[1].y == 0x7ff && mt[1].x == 0xff0) {
99172eceab7SHans de Goede 			mt[1].x = 0;
99272eceab7SHans de Goede 			/* y gets set to 0 at the end of this function */
99372eceab7SHans de Goede 		}
9943808843cSYunkang Tang 		break;
9953808843cSYunkang Tang 
9963808843cSYunkang Tang 	case V7_PACKET_ID_MULTI:
9973808843cSYunkang Tang 		mt[1].x &= ~0x003F;
9983808843cSYunkang Tang 		mt[1].y &= ~0x0020;
9993808843cSYunkang Tang 		mt[1].y |= ((pkt[4] & 0x02) << 4);
10003808843cSYunkang Tang 		mt[1].y |= 0x001F;
10013808843cSYunkang Tang 		break;
10023808843cSYunkang Tang 
10033808843cSYunkang Tang 	case V7_PACKET_ID_NEW:
10043808843cSYunkang Tang 		mt[1].x &= ~0x003F;
10053808843cSYunkang Tang 		mt[1].x |= (pkt[0] & 0x20);
10063808843cSYunkang Tang 		mt[1].y |= 0x000F;
10073808843cSYunkang Tang 		break;
10083808843cSYunkang Tang 	}
10093808843cSYunkang Tang 
10103808843cSYunkang Tang 	mt[0].y = 0x7FF - mt[0].y;
10113808843cSYunkang Tang 	mt[1].y = 0x7FF - mt[1].y;
10123808843cSYunkang Tang }
10133808843cSYunkang Tang 
alps_get_mt_count(struct input_mt_pos * mt)10143808843cSYunkang Tang static int alps_get_mt_count(struct input_mt_pos *mt)
10153808843cSYunkang Tang {
10167091c443SHans de Goede 	int i, fingers = 0;
10173808843cSYunkang Tang 
10187091c443SHans de Goede 	for (i = 0; i < MAX_TOUCHES; i++) {
10197091c443SHans de Goede 		if (mt[i].x != 0 || mt[i].y != 0)
10207091c443SHans de Goede 			fingers++;
10217091c443SHans de Goede 	}
10223808843cSYunkang Tang 
10237091c443SHans de Goede 	return fingers;
10243808843cSYunkang Tang }
10253808843cSYunkang Tang 
alps_decode_packet_v7(struct alps_fields * f,unsigned char * p,struct psmouse * psmouse)10263808843cSYunkang Tang static int alps_decode_packet_v7(struct alps_fields *f,
10273808843cSYunkang Tang 				  unsigned char *p,
10283808843cSYunkang Tang 				  struct psmouse *psmouse)
10293808843cSYunkang Tang {
1030d27eb793SHans de Goede 	struct alps_data *priv = psmouse->private;
10313808843cSYunkang Tang 	unsigned char pkt_id;
10323808843cSYunkang Tang 
10333808843cSYunkang Tang 	pkt_id = alps_get_packet_id_v7(p);
10343808843cSYunkang Tang 	if (pkt_id == V7_PACKET_ID_IDLE)
10353808843cSYunkang Tang 		return 0;
10363808843cSYunkang Tang 	if (pkt_id == V7_PACKET_ID_UNKNOWN)
10373808843cSYunkang Tang 		return -1;
10388b238115SHans de Goede 	/*
10398b238115SHans de Goede 	 * NEW packets are send to indicate a discontinuity in the finger
10408b238115SHans de Goede 	 * coordinate reporting. Specifically a finger may have moved from
10418b238115SHans de Goede 	 * slot 0 to 1 or vice versa. INPUT_MT_TRACK takes care of this for
10428b238115SHans de Goede 	 * us.
10438b238115SHans de Goede 	 *
10448b238115SHans de Goede 	 * NEW packets have 3 problems:
10458b238115SHans de Goede 	 * 1) They do not contain middle / right button info (on non clickpads)
10468b238115SHans de Goede 	 *    this can be worked around by preserving the old button state
10478b238115SHans de Goede 	 * 2) They do not contain an accurate fingercount, and they are
10488b238115SHans de Goede 	 *    typically send when the number of fingers changes. We cannot use
10498b238115SHans de Goede 	 *    the old finger count as that may mismatch with the amount of
10508b238115SHans de Goede 	 *    touch coordinates we've available in the NEW packet
10518b238115SHans de Goede 	 * 3) Their x data for the second touch is inaccurate leading to
10528b238115SHans de Goede 	 *    a possible jump of the x coordinate by 16 units when the first
10538b238115SHans de Goede 	 *    non NEW packet comes in
10548b238115SHans de Goede 	 * Since problems 2 & 3 cannot be worked around, just ignore them.
10558b238115SHans de Goede 	 */
10568b238115SHans de Goede 	if (pkt_id == V7_PACKET_ID_NEW)
10578b238115SHans de Goede 		return 1;
10583808843cSYunkang Tang 
10593808843cSYunkang Tang 	alps_get_finger_coordinate_v7(f->mt, p, pkt_id);
10603808843cSYunkang Tang 
10613808843cSYunkang Tang 	if (pkt_id == V7_PACKET_ID_TWO)
10623808843cSYunkang Tang 		f->fingers = alps_get_mt_count(f->mt);
10638b238115SHans de Goede 	else /* pkt_id == V7_PACKET_ID_MULTI */
10643808843cSYunkang Tang 		f->fingers = 3 + (p[5] & 0x03);
10653808843cSYunkang Tang 
1066d27eb793SHans de Goede 	f->left = (p[0] & 0x80) >> 7;
1067d27eb793SHans de Goede 	if (priv->flags & ALPS_BUTTONPAD) {
1068d27eb793SHans de Goede 		if (p[0] & 0x20)
1069d27eb793SHans de Goede 			f->fingers++;
1070d27eb793SHans de Goede 		if (p[0] & 0x10)
1071d27eb793SHans de Goede 			f->fingers++;
1072d27eb793SHans de Goede 	} else {
1073d27eb793SHans de Goede 		f->right = (p[0] & 0x20) >> 5;
1074d27eb793SHans de Goede 		f->middle = (p[0] & 0x10) >> 4;
1075d27eb793SHans de Goede 	}
1076d27eb793SHans de Goede 
10777091c443SHans de Goede 	/* Sometimes a single touch is reported in mt[1] rather then mt[0] */
10787091c443SHans de Goede 	if (f->fingers == 1 && f->mt[0].x == 0 && f->mt[0].y == 0) {
10797091c443SHans de Goede 		f->mt[0].x = f->mt[1].x;
10807091c443SHans de Goede 		f->mt[0].y = f->mt[1].y;
10817091c443SHans de Goede 		f->mt[1].x = 0;
10827091c443SHans de Goede 		f->mt[1].y = 0;
10837091c443SHans de Goede 	}
10847091c443SHans de Goede 
10853808843cSYunkang Tang 	return 0;
10863808843cSYunkang Tang }
10873808843cSYunkang Tang 
alps_process_trackstick_packet_v7(struct psmouse * psmouse)10883808843cSYunkang Tang static void alps_process_trackstick_packet_v7(struct psmouse *psmouse)
10893808843cSYunkang Tang {
10903808843cSYunkang Tang 	struct alps_data *priv = psmouse->private;
10913808843cSYunkang Tang 	unsigned char *packet = psmouse->packet;
10923808843cSYunkang Tang 	struct input_dev *dev2 = priv->dev2;
10931ef85805SDmitry Torokhov 	int x, y, z;
10943808843cSYunkang Tang 
109534412ba2SPali Rohár 	/* It should be a DualPoint when received trackstick packet */
109634412ba2SPali Rohár 	if (!(priv->flags & ALPS_DUALPOINT)) {
109734412ba2SPali Rohár 		psmouse_warn(psmouse,
109834412ba2SPali Rohár 			     "Rejected trackstick packet from non DualPoint device");
109934412ba2SPali Rohár 		return;
110034412ba2SPali Rohár 	}
110134412ba2SPali Rohár 
11023808843cSYunkang Tang 	x = ((packet[2] & 0xbf)) | ((packet[3] & 0x10) << 2);
11033808843cSYunkang Tang 	y = (packet[3] & 0x07) | (packet[4] & 0xb8) |
11043808843cSYunkang Tang 	    ((packet[3] & 0x20) << 1);
11053808843cSYunkang Tang 	z = (packet[5] & 0x3f) | ((packet[3] & 0x80) >> 1);
11063808843cSYunkang Tang 
1107*754ff506Smsizanoen 	input_report_rel(dev2, REL_X, (s8)x);
1108*754ff506Smsizanoen 	input_report_rel(dev2, REL_Y, -((s8)y));
11094621c966SPali Rohár 	input_report_abs(dev2, ABS_PRESSURE, z);
11103808843cSYunkang Tang 
11111ef85805SDmitry Torokhov 	psmouse_report_standard_buttons(dev2, packet[1]);
11123808843cSYunkang Tang 
11133808843cSYunkang Tang 	input_sync(dev2);
11143808843cSYunkang Tang }
11153808843cSYunkang Tang 
alps_process_touchpad_packet_v7(struct psmouse * psmouse)11163808843cSYunkang Tang static void alps_process_touchpad_packet_v7(struct psmouse *psmouse)
11173808843cSYunkang Tang {
11183808843cSYunkang Tang 	struct alps_data *priv = psmouse->private;
11193808843cSYunkang Tang 	struct input_dev *dev = psmouse->dev;
11203808843cSYunkang Tang 	struct alps_fields *f = &priv->f;
11213808843cSYunkang Tang 
11223808843cSYunkang Tang 	memset(f, 0, sizeof(*f));
11233808843cSYunkang Tang 
11243808843cSYunkang Tang 	if (priv->decode_fields(f, psmouse->packet, psmouse))
11253808843cSYunkang Tang 		return;
11263808843cSYunkang Tang 
11273808843cSYunkang Tang 	alps_report_mt_data(psmouse, alps_get_mt_count(f->mt));
11283808843cSYunkang Tang 
11293808843cSYunkang Tang 	input_mt_report_finger_count(dev, f->fingers);
11303808843cSYunkang Tang 
11313808843cSYunkang Tang 	input_report_key(dev, BTN_LEFT, f->left);
11323808843cSYunkang Tang 	input_report_key(dev, BTN_RIGHT, f->right);
11333808843cSYunkang Tang 	input_report_key(dev, BTN_MIDDLE, f->middle);
11343808843cSYunkang Tang 
11353808843cSYunkang Tang 	input_sync(dev);
11363808843cSYunkang Tang }
11373808843cSYunkang Tang 
alps_process_packet_v7(struct psmouse * psmouse)11383808843cSYunkang Tang static void alps_process_packet_v7(struct psmouse *psmouse)
11393808843cSYunkang Tang {
11403808843cSYunkang Tang 	unsigned char *packet = psmouse->packet;
11413808843cSYunkang Tang 
11423808843cSYunkang Tang 	if (packet[0] == 0x48 && (packet[4] & 0x47) == 0x06)
11433808843cSYunkang Tang 		alps_process_trackstick_packet_v7(psmouse);
11443808843cSYunkang Tang 	else
11453808843cSYunkang Tang 		alps_process_touchpad_packet_v7(psmouse);
11463808843cSYunkang Tang }
11473808843cSYunkang Tang 
alps_get_pkt_id_ss4_v2(unsigned char * byte)114823fce365SPaul Donohue static enum SS4_PACKET_ID alps_get_pkt_id_ss4_v2(unsigned char *byte)
11493db5b9f7SMasaki Ota {
115023fce365SPaul Donohue 	enum SS4_PACKET_ID pkt_id = SS4_PACKET_ID_IDLE;
11513db5b9f7SMasaki Ota 
11524777ac22SBen Gamari 	switch (byte[3] & 0x30) {
11534777ac22SBen Gamari 	case 0x00:
115423fce365SPaul Donohue 		if (SS4_IS_IDLE_V2(byte)) {
11553db5b9f7SMasaki Ota 			pkt_id = SS4_PACKET_ID_IDLE;
11563db5b9f7SMasaki Ota 		} else {
11574777ac22SBen Gamari 			pkt_id = SS4_PACKET_ID_ONE;
11584777ac22SBen Gamari 		}
11594777ac22SBen Gamari 		break;
11604777ac22SBen Gamari 	case 0x10:
11614777ac22SBen Gamari 		/* two-finger finger positions */
11624777ac22SBen Gamari 		pkt_id = SS4_PACKET_ID_TWO;
11634777ac22SBen Gamari 		break;
11644777ac22SBen Gamari 	case 0x20:
11654777ac22SBen Gamari 		/* stick pointer */
11664777ac22SBen Gamari 		pkt_id = SS4_PACKET_ID_STICK;
11674777ac22SBen Gamari 		break;
11684777ac22SBen Gamari 	case 0x30:
11694777ac22SBen Gamari 		/* third and fourth finger positions */
11703db5b9f7SMasaki Ota 		pkt_id = SS4_PACKET_ID_MULTI;
11714777ac22SBen Gamari 		break;
11723db5b9f7SMasaki Ota 	}
11733db5b9f7SMasaki Ota 
11743db5b9f7SMasaki Ota 	return pkt_id;
11753db5b9f7SMasaki Ota }
11763db5b9f7SMasaki Ota 
alps_decode_ss4_v2(struct alps_fields * f,unsigned char * p,struct psmouse * psmouse)11773db5b9f7SMasaki Ota static int alps_decode_ss4_v2(struct alps_fields *f,
11783db5b9f7SMasaki Ota 			      unsigned char *p, struct psmouse *psmouse)
11793db5b9f7SMasaki Ota {
11803db5b9f7SMasaki Ota 	struct alps_data *priv = psmouse->private;
118123fce365SPaul Donohue 	enum SS4_PACKET_ID pkt_id;
11823db5b9f7SMasaki Ota 	unsigned int no_data_x, no_data_y;
11833db5b9f7SMasaki Ota 
11843db5b9f7SMasaki Ota 	pkt_id = alps_get_pkt_id_ss4_v2(p);
11853db5b9f7SMasaki Ota 
11863db5b9f7SMasaki Ota 	/* Current packet is 1Finger coordinate packet */
11873db5b9f7SMasaki Ota 	switch (pkt_id) {
11883db5b9f7SMasaki Ota 	case SS4_PACKET_ID_ONE:
11893db5b9f7SMasaki Ota 		f->mt[0].x = SS4_1F_X_V2(p);
11903db5b9f7SMasaki Ota 		f->mt[0].y = SS4_1F_Y_V2(p);
11913db5b9f7SMasaki Ota 		f->pressure = ((SS4_1F_Z_V2(p)) * 2) & 0x7f;
1192a8317763SBen Gamari 		/*
1193a8317763SBen Gamari 		 * When a button is held the device will give us events
1194a8317763SBen Gamari 		 * with x, y, and pressure of 0. This causes annoying jumps
1195a8317763SBen Gamari 		 * if a touch is released while the button is held.
1196a8317763SBen Gamari 		 * Handle this by claiming zero contacts.
1197a8317763SBen Gamari 		 */
1198a8317763SBen Gamari 		f->fingers = f->pressure > 0 ? 1 : 0;
11993db5b9f7SMasaki Ota 		f->first_mp = 0;
12003db5b9f7SMasaki Ota 		f->is_mp = 0;
12013db5b9f7SMasaki Ota 		break;
12023db5b9f7SMasaki Ota 
12033db5b9f7SMasaki Ota 	case SS4_PACKET_ID_TWO:
12043db5b9f7SMasaki Ota 		if (priv->flags & ALPS_BUTTONPAD) {
12054a646580SMasaki Ota 			if (IS_SS4PLUS_DEV(priv->dev_id)) {
12064a646580SMasaki Ota 				f->mt[0].x = SS4_PLUS_BTL_MF_X_V2(p, 0);
12074a646580SMasaki Ota 				f->mt[1].x = SS4_PLUS_BTL_MF_X_V2(p, 1);
12084a646580SMasaki Ota 			} else {
12093db5b9f7SMasaki Ota 				f->mt[0].x = SS4_BTL_MF_X_V2(p, 0);
12103db5b9f7SMasaki Ota 				f->mt[1].x = SS4_BTL_MF_X_V2(p, 1);
12114a646580SMasaki Ota 			}
12124a646580SMasaki Ota 			f->mt[0].y = SS4_BTL_MF_Y_V2(p, 0);
12133db5b9f7SMasaki Ota 			f->mt[1].y = SS4_BTL_MF_Y_V2(p, 1);
12143db5b9f7SMasaki Ota 		} else {
12154a646580SMasaki Ota 			if (IS_SS4PLUS_DEV(priv->dev_id)) {
12164a646580SMasaki Ota 				f->mt[0].x = SS4_PLUS_STD_MF_X_V2(p, 0);
12174a646580SMasaki Ota 				f->mt[1].x = SS4_PLUS_STD_MF_X_V2(p, 1);
12184a646580SMasaki Ota 			} else {
12193db5b9f7SMasaki Ota 				f->mt[0].x = SS4_STD_MF_X_V2(p, 0);
12203db5b9f7SMasaki Ota 				f->mt[1].x = SS4_STD_MF_X_V2(p, 1);
12214a646580SMasaki Ota 			}
12224a646580SMasaki Ota 			f->mt[0].y = SS4_STD_MF_Y_V2(p, 0);
12233db5b9f7SMasaki Ota 			f->mt[1].y = SS4_STD_MF_Y_V2(p, 1);
12243db5b9f7SMasaki Ota 		}
12253db5b9f7SMasaki Ota 		f->pressure = SS4_MF_Z_V2(p, 0) ? 0x30 : 0;
12263db5b9f7SMasaki Ota 
12273db5b9f7SMasaki Ota 		if (SS4_IS_MF_CONTINUE(p)) {
12283db5b9f7SMasaki Ota 			f->first_mp = 1;
12293db5b9f7SMasaki Ota 		} else {
12303db5b9f7SMasaki Ota 			f->fingers = 2;
12313db5b9f7SMasaki Ota 			f->first_mp = 0;
12323db5b9f7SMasaki Ota 		}
12333db5b9f7SMasaki Ota 		f->is_mp = 0;
12343db5b9f7SMasaki Ota 
12353db5b9f7SMasaki Ota 		break;
12363db5b9f7SMasaki Ota 
12373db5b9f7SMasaki Ota 	case SS4_PACKET_ID_MULTI:
12383db5b9f7SMasaki Ota 		if (priv->flags & ALPS_BUTTONPAD) {
12394a646580SMasaki Ota 			if (IS_SS4PLUS_DEV(priv->dev_id)) {
12404d94e776SNir Perry 				f->mt[2].x = SS4_PLUS_BTL_MF_X_V2(p, 0);
12414d94e776SNir Perry 				f->mt[3].x = SS4_PLUS_BTL_MF_X_V2(p, 1);
12424d94e776SNir Perry 				no_data_x = SS4_PLUS_MFPACKET_NO_AX_BL;
12434a646580SMasaki Ota 			} else {
12443db5b9f7SMasaki Ota 				f->mt[2].x = SS4_BTL_MF_X_V2(p, 0);
12453db5b9f7SMasaki Ota 				f->mt[3].x = SS4_BTL_MF_X_V2(p, 1);
12464d94e776SNir Perry 				no_data_x = SS4_MFPACKET_NO_AX_BL;
12474a646580SMasaki Ota 			}
12484d94e776SNir Perry 			no_data_y = SS4_MFPACKET_NO_AY_BL;
12494a646580SMasaki Ota 
12504a646580SMasaki Ota 			f->mt[2].y = SS4_BTL_MF_Y_V2(p, 0);
12513db5b9f7SMasaki Ota 			f->mt[3].y = SS4_BTL_MF_Y_V2(p, 1);
12523db5b9f7SMasaki Ota 		} else {
12534a646580SMasaki Ota 			if (IS_SS4PLUS_DEV(priv->dev_id)) {
12544d94e776SNir Perry 				f->mt[2].x = SS4_PLUS_STD_MF_X_V2(p, 0);
12554d94e776SNir Perry 				f->mt[3].x = SS4_PLUS_STD_MF_X_V2(p, 1);
12564d94e776SNir Perry 				no_data_x = SS4_PLUS_MFPACKET_NO_AX;
12574a646580SMasaki Ota 			} else {
12584d94e776SNir Perry 				f->mt[2].x = SS4_STD_MF_X_V2(p, 0);
12594d94e776SNir Perry 				f->mt[3].x = SS4_STD_MF_X_V2(p, 1);
12604d94e776SNir Perry 				no_data_x = SS4_MFPACKET_NO_AX;
12614a646580SMasaki Ota 			}
12624d94e776SNir Perry 			no_data_y = SS4_MFPACKET_NO_AY;
12634d94e776SNir Perry 
12643db5b9f7SMasaki Ota 			f->mt[2].y = SS4_STD_MF_Y_V2(p, 0);
12653db5b9f7SMasaki Ota 			f->mt[3].y = SS4_STD_MF_Y_V2(p, 1);
12663db5b9f7SMasaki Ota 		}
12673db5b9f7SMasaki Ota 
12683db5b9f7SMasaki Ota 		f->first_mp = 0;
12693db5b9f7SMasaki Ota 		f->is_mp = 1;
12703db5b9f7SMasaki Ota 
12713db5b9f7SMasaki Ota 		if (SS4_IS_5F_DETECTED(p)) {
12723db5b9f7SMasaki Ota 			f->fingers = 5;
12733db5b9f7SMasaki Ota 		} else if (f->mt[3].x == no_data_x &&
12743db5b9f7SMasaki Ota 			     f->mt[3].y == no_data_y) {
12753db5b9f7SMasaki Ota 			f->mt[3].x = 0;
12763db5b9f7SMasaki Ota 			f->mt[3].y = 0;
12773db5b9f7SMasaki Ota 			f->fingers = 3;
12783db5b9f7SMasaki Ota 		} else {
12793db5b9f7SMasaki Ota 			f->fingers = 4;
12803db5b9f7SMasaki Ota 		}
12813db5b9f7SMasaki Ota 		break;
12823db5b9f7SMasaki Ota 
12834777ac22SBen Gamari 	case SS4_PACKET_ID_STICK:
12847229c58cSPaul Donohue 		/*
12857229c58cSPaul Donohue 		 * x, y, and pressure are decoded in
12867229c58cSPaul Donohue 		 * alps_process_packet_ss4_v2()
12877229c58cSPaul Donohue 		 */
12887229c58cSPaul Donohue 		f->first_mp = 0;
12897229c58cSPaul Donohue 		f->is_mp = 0;
12904777ac22SBen Gamari 		break;
12914777ac22SBen Gamari 
12923db5b9f7SMasaki Ota 	case SS4_PACKET_ID_IDLE:
12933db5b9f7SMasaki Ota 	default:
12943db5b9f7SMasaki Ota 		memset(f, 0, sizeof(struct alps_fields));
12953db5b9f7SMasaki Ota 		break;
12963db5b9f7SMasaki Ota 	}
12973db5b9f7SMasaki Ota 
12984777ac22SBen Gamari 	/* handle buttons */
12994777ac22SBen Gamari 	if (pkt_id == SS4_PACKET_ID_STICK) {
13004777ac22SBen Gamari 		f->ts_left = !!(SS4_BTN_V2(p) & 0x01);
13014777ac22SBen Gamari 		f->ts_right = !!(SS4_BTN_V2(p) & 0x02);
13024777ac22SBen Gamari 		f->ts_middle = !!(SS4_BTN_V2(p) & 0x04);
13034777ac22SBen Gamari 	} else {
13043db5b9f7SMasaki Ota 		f->left = !!(SS4_BTN_V2(p) & 0x01);
13053db5b9f7SMasaki Ota 		if (!(priv->flags & ALPS_BUTTONPAD)) {
13063db5b9f7SMasaki Ota 			f->right = !!(SS4_BTN_V2(p) & 0x02);
13073db5b9f7SMasaki Ota 			f->middle = !!(SS4_BTN_V2(p) & 0x04);
13083db5b9f7SMasaki Ota 		}
13094777ac22SBen Gamari 	}
13103db5b9f7SMasaki Ota 
13113db5b9f7SMasaki Ota 	return 0;
13123db5b9f7SMasaki Ota }
13133db5b9f7SMasaki Ota 
alps_process_packet_ss4_v2(struct psmouse * psmouse)13143db5b9f7SMasaki Ota static void alps_process_packet_ss4_v2(struct psmouse *psmouse)
13153db5b9f7SMasaki Ota {
13163db5b9f7SMasaki Ota 	struct alps_data *priv = psmouse->private;
13173db5b9f7SMasaki Ota 	unsigned char *packet = psmouse->packet;
13183db5b9f7SMasaki Ota 	struct input_dev *dev = psmouse->dev;
13194777ac22SBen Gamari 	struct input_dev *dev2 = priv->dev2;
13203db5b9f7SMasaki Ota 	struct alps_fields *f = &priv->f;
13213db5b9f7SMasaki Ota 
13223db5b9f7SMasaki Ota 	memset(f, 0, sizeof(struct alps_fields));
13233db5b9f7SMasaki Ota 	priv->decode_fields(f, packet, psmouse);
13243db5b9f7SMasaki Ota 	if (priv->multi_packet) {
13253db5b9f7SMasaki Ota 		/*
13263db5b9f7SMasaki Ota 		 * Sometimes the first packet will indicate a multi-packet
13273db5b9f7SMasaki Ota 		 * sequence, but sometimes the next multi-packet would not
13283db5b9f7SMasaki Ota 		 * come. Check for this, and when it happens process the
13293db5b9f7SMasaki Ota 		 * position packet as usual.
13303db5b9f7SMasaki Ota 		 */
13313db5b9f7SMasaki Ota 		if (f->is_mp) {
13323db5b9f7SMasaki Ota 			/* Now process the 1st packet */
13333db5b9f7SMasaki Ota 			priv->decode_fields(f, priv->multi_data, psmouse);
13343db5b9f7SMasaki Ota 		} else {
13353db5b9f7SMasaki Ota 			priv->multi_packet = 0;
13363db5b9f7SMasaki Ota 		}
13373db5b9f7SMasaki Ota 	}
13383db5b9f7SMasaki Ota 
13393db5b9f7SMasaki Ota 	/*
13403db5b9f7SMasaki Ota 	 * "f.is_mp" would always be '0' after merging the 1st and 2nd packet.
13413db5b9f7SMasaki Ota 	 * When it is set, it means 2nd packet comes without 1st packet come.
13423db5b9f7SMasaki Ota 	 */
13433db5b9f7SMasaki Ota 	if (f->is_mp)
13443db5b9f7SMasaki Ota 		return;
13453db5b9f7SMasaki Ota 
13463db5b9f7SMasaki Ota 	/* Save the first packet */
13473db5b9f7SMasaki Ota 	if (!priv->multi_packet && f->first_mp) {
13483db5b9f7SMasaki Ota 		priv->multi_packet = 1;
13493db5b9f7SMasaki Ota 		memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
13503db5b9f7SMasaki Ota 		return;
13513db5b9f7SMasaki Ota 	}
13523db5b9f7SMasaki Ota 
13533db5b9f7SMasaki Ota 	priv->multi_packet = 0;
13543db5b9f7SMasaki Ota 
1355864db929SPaul Donohue 	/* Report trackstick */
1356864db929SPaul Donohue 	if (alps_get_pkt_id_ss4_v2(packet) == SS4_PACKET_ID_STICK) {
13577229c58cSPaul Donohue 		if (!(priv->flags & ALPS_DUALPOINT)) {
13587229c58cSPaul Donohue 			psmouse_warn(psmouse,
13597229c58cSPaul Donohue 				     "Rejected trackstick packet from non DualPoint device");
13607229c58cSPaul Donohue 			return;
13617229c58cSPaul Donohue 		}
13627229c58cSPaul Donohue 
136323fce365SPaul Donohue 		input_report_rel(dev2, REL_X, SS4_TS_X_V2(packet));
136423fce365SPaul Donohue 		input_report_rel(dev2, REL_Y, SS4_TS_Y_V2(packet));
136523fce365SPaul Donohue 		input_report_abs(dev2, ABS_PRESSURE, SS4_TS_Z_V2(packet));
13667229c58cSPaul Donohue 
1367864db929SPaul Donohue 		input_report_key(dev2, BTN_LEFT, f->ts_left);
1368864db929SPaul Donohue 		input_report_key(dev2, BTN_RIGHT, f->ts_right);
1369864db929SPaul Donohue 		input_report_key(dev2, BTN_MIDDLE, f->ts_middle);
13707229c58cSPaul Donohue 
1371864db929SPaul Donohue 		input_sync(dev2);
1372864db929SPaul Donohue 		return;
1373864db929SPaul Donohue 	}
1374864db929SPaul Donohue 
1375864db929SPaul Donohue 	/* Report touchpad */
13763db5b9f7SMasaki Ota 	alps_report_mt_data(psmouse, (f->fingers <= 4) ? f->fingers : 4);
13773db5b9f7SMasaki Ota 
13783db5b9f7SMasaki Ota 	input_mt_report_finger_count(dev, f->fingers);
13793db5b9f7SMasaki Ota 
13803db5b9f7SMasaki Ota 	input_report_key(dev, BTN_LEFT, f->left);
13813db5b9f7SMasaki Ota 	input_report_key(dev, BTN_RIGHT, f->right);
13823db5b9f7SMasaki Ota 	input_report_key(dev, BTN_MIDDLE, f->middle);
13833db5b9f7SMasaki Ota 
13843db5b9f7SMasaki Ota 	input_report_abs(dev, ABS_PRESSURE, f->pressure);
13853db5b9f7SMasaki Ota 	input_sync(dev);
13863db5b9f7SMasaki Ota }
13873db5b9f7SMasaki Ota 
alps_is_valid_package_ss4_v2(struct psmouse * psmouse)13883db5b9f7SMasaki Ota static bool alps_is_valid_package_ss4_v2(struct psmouse *psmouse)
13893db5b9f7SMasaki Ota {
13903db5b9f7SMasaki Ota 	if (psmouse->pktcnt == 4 && ((psmouse->packet[3] & 0x08) != 0x08))
13913db5b9f7SMasaki Ota 		return false;
13923db5b9f7SMasaki Ota 	if (psmouse->pktcnt == 6 && ((psmouse->packet[5] & 0x10) != 0x0))
13933db5b9f7SMasaki Ota 		return false;
13943db5b9f7SMasaki Ota 	return true;
13953db5b9f7SMasaki Ota }
13963db5b9f7SMasaki Ota 
139704aae283SPali Rohár static DEFINE_MUTEX(alps_mutex);
139804aae283SPali Rohár 
alps_register_bare_ps2_mouse(struct work_struct * work)139904aae283SPali Rohár static void alps_register_bare_ps2_mouse(struct work_struct *work)
140004aae283SPali Rohár {
140104aae283SPali Rohár 	struct alps_data *priv =
140204aae283SPali Rohár 		container_of(work, struct alps_data, dev3_register_work.work);
140304aae283SPali Rohár 	struct psmouse *psmouse = priv->psmouse;
140404aae283SPali Rohár 	struct input_dev *dev3;
140504aae283SPali Rohár 	int error = 0;
140604aae283SPali Rohár 
140704aae283SPali Rohár 	mutex_lock(&alps_mutex);
140804aae283SPali Rohár 
140904aae283SPali Rohár 	if (priv->dev3)
141004aae283SPali Rohár 		goto out;
141104aae283SPali Rohár 
141204aae283SPali Rohár 	dev3 = input_allocate_device();
141304aae283SPali Rohár 	if (!dev3) {
141404aae283SPali Rohár 		psmouse_err(psmouse, "failed to allocate secondary device\n");
141504aae283SPali Rohár 		error = -ENOMEM;
141604aae283SPali Rohár 		goto out;
141704aae283SPali Rohár 	}
141804aae283SPali Rohár 
141904aae283SPali Rohár 	snprintf(priv->phys3, sizeof(priv->phys3), "%s/%s",
142004aae283SPali Rohár 		 psmouse->ps2dev.serio->phys,
142104aae283SPali Rohár 		 (priv->dev2 ? "input2" : "input1"));
142204aae283SPali Rohár 	dev3->phys = priv->phys3;
142304aae283SPali Rohár 
142404aae283SPali Rohár 	/*
142504aae283SPali Rohár 	 * format of input device name is: "protocol vendor name"
142604aae283SPali Rohár 	 * see function psmouse_switch_protocol() in psmouse-base.c
142704aae283SPali Rohár 	 */
142804aae283SPali Rohár 	dev3->name = "PS/2 ALPS Mouse";
142904aae283SPali Rohár 
143004aae283SPali Rohár 	dev3->id.bustype = BUS_I8042;
143104aae283SPali Rohár 	dev3->id.vendor  = 0x0002;
143204aae283SPali Rohár 	dev3->id.product = PSMOUSE_PS2;
143304aae283SPali Rohár 	dev3->id.version = 0x0000;
143404aae283SPali Rohár 	dev3->dev.parent = &psmouse->ps2dev.serio->dev;
143504aae283SPali Rohár 
143604aae283SPali Rohár 	input_set_capability(dev3, EV_REL, REL_X);
143704aae283SPali Rohár 	input_set_capability(dev3, EV_REL, REL_Y);
143804aae283SPali Rohár 	input_set_capability(dev3, EV_KEY, BTN_LEFT);
143904aae283SPali Rohár 	input_set_capability(dev3, EV_KEY, BTN_RIGHT);
144004aae283SPali Rohár 	input_set_capability(dev3, EV_KEY, BTN_MIDDLE);
144104aae283SPali Rohár 
144204aae283SPali Rohár 	__set_bit(INPUT_PROP_POINTER, dev3->propbit);
144304aae283SPali Rohár 
144404aae283SPali Rohár 	error = input_register_device(dev3);
144504aae283SPali Rohár 	if (error) {
144604aae283SPali Rohár 		psmouse_err(psmouse,
144704aae283SPali Rohár 			    "failed to register secondary device: %d\n",
144804aae283SPali Rohár 			    error);
144904aae283SPali Rohár 		input_free_device(dev3);
145004aae283SPali Rohár 		goto out;
145104aae283SPali Rohár 	}
145204aae283SPali Rohár 
145304aae283SPali Rohár 	priv->dev3 = dev3;
145404aae283SPali Rohár 
145504aae283SPali Rohár out:
145604aae283SPali Rohár 	/*
145704aae283SPali Rohár 	 * Save the error code so that we can detect that we
145804aae283SPali Rohár 	 * already tried to create the device.
145904aae283SPali Rohár 	 */
146004aae283SPali Rohár 	if (error)
146104aae283SPali Rohár 		priv->dev3 = ERR_PTR(error);
146204aae283SPali Rohár 
146304aae283SPali Rohár 	mutex_unlock(&alps_mutex);
146404aae283SPali Rohár }
146504aae283SPali Rohár 
alps_report_bare_ps2_packet(struct psmouse * psmouse,unsigned char packet[],bool report_buttons)146659c30afbSHans de Goede static void alps_report_bare_ps2_packet(struct psmouse *psmouse,
14671d9f2626SSebastian Kapfer 					unsigned char packet[],
14681d9f2626SSebastian Kapfer 					bool report_buttons)
14691d9f2626SSebastian Kapfer {
147059c30afbSHans de Goede 	struct alps_data *priv = psmouse->private;
14716bcca19fSHans de Goede 	struct input_dev *dev, *dev2 = NULL;
147259c30afbSHans de Goede 
1473e3a79212SHans de Goede 	/* Figure out which device to use to report the bare packet */
1474e3a79212SHans de Goede 	if (priv->proto_version == ALPS_PROTO_V2 &&
1475e3a79212SHans de Goede 	    (priv->flags & ALPS_DUALPOINT)) {
1476e3a79212SHans de Goede 		/* On V2 devices the DualPoint Stick reports bare packets */
1477e3a79212SHans de Goede 		dev = priv->dev2;
14786bcca19fSHans de Goede 		dev2 = psmouse->dev;
1479e3a79212SHans de Goede 	} else if (unlikely(IS_ERR_OR_NULL(priv->dev3))) {
148059c30afbSHans de Goede 		/* Register dev3 mouse if we received PS/2 packet first time */
148159c30afbSHans de Goede 		if (!IS_ERR(priv->dev3))
148259c30afbSHans de Goede 			psmouse_queue_work(psmouse, &priv->dev3_register_work,
148359c30afbSHans de Goede 					   0);
148459c30afbSHans de Goede 		return;
148559c30afbSHans de Goede 	} else {
148659c30afbSHans de Goede 		dev = priv->dev3;
148759c30afbSHans de Goede 	}
148859c30afbSHans de Goede 
14891d9f2626SSebastian Kapfer 	if (report_buttons)
14906bcca19fSHans de Goede 		alps_report_buttons(dev, dev2,
14911d9f2626SSebastian Kapfer 				packet[0] & 1, packet[0] & 2, packet[0] & 4);
14921d9f2626SSebastian Kapfer 
14931ef85805SDmitry Torokhov 	psmouse_report_standard_motion(dev, packet);
14941d9f2626SSebastian Kapfer 
149504aae283SPali Rohár 	input_sync(dev);
14961d9f2626SSebastian Kapfer }
14971d9f2626SSebastian Kapfer 
alps_handle_interleaved_ps2(struct psmouse * psmouse)14981d9f2626SSebastian Kapfer static psmouse_ret_t alps_handle_interleaved_ps2(struct psmouse *psmouse)
14991da177e4SLinus Torvalds {
15001da177e4SLinus Torvalds 	struct alps_data *priv = psmouse->private;
15011da177e4SLinus Torvalds 
15021d9f2626SSebastian Kapfer 	if (psmouse->pktcnt < 6)
15031d9f2626SSebastian Kapfer 		return PSMOUSE_GOOD_DATA;
15041d9f2626SSebastian Kapfer 
15051d9f2626SSebastian Kapfer 	if (psmouse->pktcnt == 6) {
15061d9f2626SSebastian Kapfer 		/*
15071d9f2626SSebastian Kapfer 		 * Start a timer to flush the packet if it ends up last
15081d9f2626SSebastian Kapfer 		 * 6-byte packet in the stream. Timer needs to fire
15091d9f2626SSebastian Kapfer 		 * psmouse core times out itself. 20 ms should be enough
15101d9f2626SSebastian Kapfer 		 * to decide if we are getting more data or not.
15111d9f2626SSebastian Kapfer 		 */
15121d9f2626SSebastian Kapfer 		mod_timer(&priv->timer, jiffies + msecs_to_jiffies(20));
15131d9f2626SSebastian Kapfer 		return PSMOUSE_GOOD_DATA;
15141d9f2626SSebastian Kapfer 	}
15151d9f2626SSebastian Kapfer 
15161d9f2626SSebastian Kapfer 	del_timer(&priv->timer);
15171d9f2626SSebastian Kapfer 
15181d9f2626SSebastian Kapfer 	if (psmouse->packet[6] & 0x80) {
15191d9f2626SSebastian Kapfer 
15201d9f2626SSebastian Kapfer 		/*
15211d9f2626SSebastian Kapfer 		 * Highest bit is set - that means we either had
15221d9f2626SSebastian Kapfer 		 * complete ALPS packet and this is start of the
15231d9f2626SSebastian Kapfer 		 * next packet or we got garbage.
15241d9f2626SSebastian Kapfer 		 */
15251d9f2626SSebastian Kapfer 
15261d9f2626SSebastian Kapfer 		if (((psmouse->packet[3] |
15271d9f2626SSebastian Kapfer 		      psmouse->packet[4] |
15281d9f2626SSebastian Kapfer 		      psmouse->packet[5]) & 0x80) ||
152999df65e7SKevin Cernekee 		    (!alps_is_valid_first_byte(priv, psmouse->packet[6]))) {
1530b5d21704SDmitry Torokhov 			psmouse_dbg(psmouse,
15313b112923SAndy Shevchenko 				    "refusing packet %4ph (suspected interleaved ps/2)\n",
15323b112923SAndy Shevchenko 				    psmouse->packet + 3);
15331d9f2626SSebastian Kapfer 			return PSMOUSE_BAD_DATA;
15341d9f2626SSebastian Kapfer 		}
15351d9f2626SSebastian Kapfer 
153624af5cb9SKevin Cernekee 		priv->process_packet(psmouse);
15371d9f2626SSebastian Kapfer 
15381d9f2626SSebastian Kapfer 		/* Continue with the next packet */
15391d9f2626SSebastian Kapfer 		psmouse->packet[0] = psmouse->packet[6];
15401d9f2626SSebastian Kapfer 		psmouse->pktcnt = 1;
15411d9f2626SSebastian Kapfer 
15421d9f2626SSebastian Kapfer 	} else {
15431d9f2626SSebastian Kapfer 
15441d9f2626SSebastian Kapfer 		/*
15451d9f2626SSebastian Kapfer 		 * High bit is 0 - that means that we indeed got a PS/2
15461d9f2626SSebastian Kapfer 		 * packet in the middle of ALPS packet.
15471d9f2626SSebastian Kapfer 		 *
15481d9f2626SSebastian Kapfer 		 * There is also possibility that we got 6-byte ALPS
15491d9f2626SSebastian Kapfer 		 * packet followed  by 3-byte packet from trackpoint. We
15501d9f2626SSebastian Kapfer 		 * can not distinguish between these 2 scenarios but
1551b5d21704SDmitry Torokhov 		 * because the latter is unlikely to happen in course of
15521d9f2626SSebastian Kapfer 		 * normal operation (user would need to press all
15531d9f2626SSebastian Kapfer 		 * buttons on the pad and start moving trackpoint
15541d9f2626SSebastian Kapfer 		 * without touching the pad surface) we assume former.
15551d9f2626SSebastian Kapfer 		 * Even if we are wrong the wost thing that would happen
15561d9f2626SSebastian Kapfer 		 * the cursor would jump but we should not get protocol
1557b5d21704SDmitry Torokhov 		 * de-synchronization.
15581d9f2626SSebastian Kapfer 		 */
15591d9f2626SSebastian Kapfer 
156059c30afbSHans de Goede 		alps_report_bare_ps2_packet(psmouse, &psmouse->packet[3],
156159c30afbSHans de Goede 					    false);
15621d9f2626SSebastian Kapfer 
15631d9f2626SSebastian Kapfer 		/*
15641d9f2626SSebastian Kapfer 		 * Continue with the standard ALPS protocol handling,
15651d9f2626SSebastian Kapfer 		 * but make sure we won't process it as an interleaved
15661d9f2626SSebastian Kapfer 		 * packet again, which may happen if all buttons are
15671d9f2626SSebastian Kapfer 		 * pressed. To avoid this let's reset the 4th bit which
15681d9f2626SSebastian Kapfer 		 * is normally 1.
15691d9f2626SSebastian Kapfer 		 */
15701d9f2626SSebastian Kapfer 		psmouse->packet[3] = psmouse->packet[6] & 0xf7;
15711d9f2626SSebastian Kapfer 		psmouse->pktcnt = 4;
15721d9f2626SSebastian Kapfer 	}
15731d9f2626SSebastian Kapfer 
15741d9f2626SSebastian Kapfer 	return PSMOUSE_GOOD_DATA;
15751d9f2626SSebastian Kapfer }
15761d9f2626SSebastian Kapfer 
alps_flush_packet(struct timer_list * t)157717a58edcSKees Cook static void alps_flush_packet(struct timer_list *t)
15781d9f2626SSebastian Kapfer {
157917a58edcSKees Cook 	struct alps_data *priv = from_timer(priv, t, timer);
158017a58edcSKees Cook 	struct psmouse *psmouse = priv->psmouse;
15811d9f2626SSebastian Kapfer 
15821d9f2626SSebastian Kapfer 	serio_pause_rx(psmouse->ps2dev.serio);
15831d9f2626SSebastian Kapfer 
1584b46615feSSeth Forshee 	if (psmouse->pktcnt == psmouse->pktsize) {
15851d9f2626SSebastian Kapfer 
15861d9f2626SSebastian Kapfer 		/*
15871d9f2626SSebastian Kapfer 		 * We did not any more data in reasonable amount of time.
15881d9f2626SSebastian Kapfer 		 * Validate the last 3 bytes and process as a standard
15891d9f2626SSebastian Kapfer 		 * ALPS packet.
15901d9f2626SSebastian Kapfer 		 */
15911d9f2626SSebastian Kapfer 		if ((psmouse->packet[3] |
15921d9f2626SSebastian Kapfer 		     psmouse->packet[4] |
15931d9f2626SSebastian Kapfer 		     psmouse->packet[5]) & 0x80) {
1594b5d21704SDmitry Torokhov 			psmouse_dbg(psmouse,
15953b112923SAndy Shevchenko 				    "refusing packet %3ph (suspected interleaved ps/2)\n",
15963b112923SAndy Shevchenko 				    psmouse->packet + 3);
15971d9f2626SSebastian Kapfer 		} else {
159824af5cb9SKevin Cernekee 			priv->process_packet(psmouse);
15991d9f2626SSebastian Kapfer 		}
16001d9f2626SSebastian Kapfer 		psmouse->pktcnt = 0;
16011d9f2626SSebastian Kapfer 	}
16021d9f2626SSebastian Kapfer 
16031d9f2626SSebastian Kapfer 	serio_continue_rx(psmouse->ps2dev.serio);
16041d9f2626SSebastian Kapfer }
16051d9f2626SSebastian Kapfer 
alps_process_byte(struct psmouse * psmouse)16061d9f2626SSebastian Kapfer static psmouse_ret_t alps_process_byte(struct psmouse *psmouse)
16071d9f2626SSebastian Kapfer {
16081d9f2626SSebastian Kapfer 	struct alps_data *priv = psmouse->private;
16091d9f2626SSebastian Kapfer 
16104ab8f7f3SPali Rohár 	/*
16114ab8f7f3SPali Rohár 	 * Check if we are dealing with a bare PS/2 packet, presumably from
16124ab8f7f3SPali Rohár 	 * a device connected to the external PS/2 port. Because bare PS/2
16134ab8f7f3SPali Rohár 	 * protocol does not have enough constant bits to self-synchronize
16144ab8f7f3SPali Rohár 	 * properly we only do this if the device is fully synchronized.
16153db5b9f7SMasaki Ota 	 * Can not distinguish V8's first byte from PS/2 packet's
16164ab8f7f3SPali Rohár 	 */
16173db5b9f7SMasaki Ota 	if (priv->proto_version != ALPS_PROTO_V8 &&
16183db5b9f7SMasaki Ota 	    !psmouse->out_of_sync_cnt &&
16193db5b9f7SMasaki Ota 	    (psmouse->packet[0] & 0xc8) == 0x08) {
16203db5b9f7SMasaki Ota 
16211da177e4SLinus Torvalds 		if (psmouse->pktcnt == 3) {
162259c30afbSHans de Goede 			alps_report_bare_ps2_packet(psmouse, psmouse->packet,
16231d9f2626SSebastian Kapfer 						    true);
16241da177e4SLinus Torvalds 			return PSMOUSE_FULL_PACKET;
16251da177e4SLinus Torvalds 		}
16261da177e4SLinus Torvalds 		return PSMOUSE_GOOD_DATA;
16271da177e4SLinus Torvalds 	}
16281da177e4SLinus Torvalds 
16291d9f2626SSebastian Kapfer 	/* Check for PS/2 packet stuffed in the middle of ALPS packet. */
16301d9f2626SSebastian Kapfer 
163199df65e7SKevin Cernekee 	if ((priv->flags & ALPS_PS2_INTERLEAVED) &&
16321d9f2626SSebastian Kapfer 	    psmouse->pktcnt >= 4 && (psmouse->packet[3] & 0x0f) == 0x0f) {
16331d9f2626SSebastian Kapfer 		return alps_handle_interleaved_ps2(psmouse);
16341d9f2626SSebastian Kapfer 	}
16351d9f2626SSebastian Kapfer 
163699df65e7SKevin Cernekee 	if (!alps_is_valid_first_byte(priv, psmouse->packet[0])) {
1637b5d21704SDmitry Torokhov 		psmouse_dbg(psmouse,
1638b5d21704SDmitry Torokhov 			    "refusing packet[0] = %x (mask0 = %x, byte0 = %x)\n",
163999df65e7SKevin Cernekee 			    psmouse->packet[0], priv->mask0, priv->byte0);
16401da177e4SLinus Torvalds 		return PSMOUSE_BAD_DATA;
16411d9f2626SSebastian Kapfer 	}
16421da177e4SLinus Torvalds 
1643b46615feSSeth Forshee 	/* Bytes 2 - pktsize should have 0 in the highest bit */
1644a7ef82aeSPali Rohár 	if (priv->proto_version < ALPS_PROTO_V5 &&
164575af9e56SDave Turvene 	    psmouse->pktcnt >= 2 && psmouse->pktcnt <= psmouse->pktsize &&
16461d9f2626SSebastian Kapfer 	    (psmouse->packet[psmouse->pktcnt - 1] & 0x80)) {
1647b5d21704SDmitry Torokhov 		psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
1648b5d21704SDmitry Torokhov 			    psmouse->pktcnt - 1,
1649b5d21704SDmitry Torokhov 			    psmouse->packet[psmouse->pktcnt - 1]);
1650a7ef82aeSPali Rohár 
1651fb2dd7a6SDmitry Torokhov 		if (priv->proto_version == ALPS_PROTO_V3_RUSHMORE &&
1652a7ef82aeSPali Rohár 		    psmouse->pktcnt == psmouse->pktsize) {
1653a7ef82aeSPali Rohár 			/*
1654a7ef82aeSPali Rohár 			 * Some Dell boxes, such as Latitude E6440 or E7440
1655a7ef82aeSPali Rohár 			 * with closed lid, quite often smash last byte of
1656a7ef82aeSPali Rohár 			 * otherwise valid packet with 0xff. Given that the
1657a7ef82aeSPali Rohár 			 * next packet is very likely to be valid let's
1658a7ef82aeSPali Rohár 			 * report PSMOUSE_FULL_PACKET but not process data,
1659a7ef82aeSPali Rohár 			 * rather than reporting PSMOUSE_BAD_DATA and
1660a7ef82aeSPali Rohár 			 * filling the logs.
1661a7ef82aeSPali Rohár 			 */
1662a7ef82aeSPali Rohár 			return PSMOUSE_FULL_PACKET;
1663a7ef82aeSPali Rohár 		}
1664a7ef82aeSPali Rohár 
16651da177e4SLinus Torvalds 		return PSMOUSE_BAD_DATA;
16661d9f2626SSebastian Kapfer 	}
16671da177e4SLinus Torvalds 
16683db5b9f7SMasaki Ota 	if ((priv->proto_version == ALPS_PROTO_V7 &&
16693db5b9f7SMasaki Ota 			!alps_is_valid_package_v7(psmouse)) ||
16703db5b9f7SMasaki Ota 	    (priv->proto_version == ALPS_PROTO_V8 &&
16713db5b9f7SMasaki Ota 			!alps_is_valid_package_ss4_v2(psmouse))) {
16723808843cSYunkang Tang 		psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
16733808843cSYunkang Tang 			    psmouse->pktcnt - 1,
16743808843cSYunkang Tang 			    psmouse->packet[psmouse->pktcnt - 1]);
16753808843cSYunkang Tang 		return PSMOUSE_BAD_DATA;
16763808843cSYunkang Tang 	}
16773808843cSYunkang Tang 
1678b46615feSSeth Forshee 	if (psmouse->pktcnt == psmouse->pktsize) {
167924af5cb9SKevin Cernekee 		priv->process_packet(psmouse);
16801da177e4SLinus Torvalds 		return PSMOUSE_FULL_PACKET;
16811da177e4SLinus Torvalds 	}
16821da177e4SLinus Torvalds 
16831da177e4SLinus Torvalds 	return PSMOUSE_GOOD_DATA;
16841da177e4SLinus Torvalds }
16851da177e4SLinus Torvalds 
alps_command_mode_send_nibble(struct psmouse * psmouse,int nibble)168625bded7cSSeth Forshee static int alps_command_mode_send_nibble(struct psmouse *psmouse, int nibble)
168725bded7cSSeth Forshee {
168825bded7cSSeth Forshee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
168925bded7cSSeth Forshee 	struct alps_data *priv = psmouse->private;
169025bded7cSSeth Forshee 	int command;
169125bded7cSSeth Forshee 	unsigned char *param;
169225bded7cSSeth Forshee 	unsigned char dummy[4];
169325bded7cSSeth Forshee 
169425bded7cSSeth Forshee 	BUG_ON(nibble > 0xf);
169525bded7cSSeth Forshee 
169625bded7cSSeth Forshee 	command = priv->nibble_commands[nibble].command;
169725bded7cSSeth Forshee 	param = (command & 0x0f00) ?
169825bded7cSSeth Forshee 		dummy : (unsigned char *)&priv->nibble_commands[nibble].data;
169925bded7cSSeth Forshee 
170025bded7cSSeth Forshee 	if (ps2_command(ps2dev, param, command))
170125bded7cSSeth Forshee 		return -1;
170225bded7cSSeth Forshee 
170325bded7cSSeth Forshee 	return 0;
170425bded7cSSeth Forshee }
170525bded7cSSeth Forshee 
alps_command_mode_set_addr(struct psmouse * psmouse,int addr)170625bded7cSSeth Forshee static int alps_command_mode_set_addr(struct psmouse *psmouse, int addr)
170725bded7cSSeth Forshee {
170825bded7cSSeth Forshee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
170925bded7cSSeth Forshee 	struct alps_data *priv = psmouse->private;
171025bded7cSSeth Forshee 	int i, nibble;
171125bded7cSSeth Forshee 
171225bded7cSSeth Forshee 	if (ps2_command(ps2dev, NULL, priv->addr_command))
171325bded7cSSeth Forshee 		return -1;
171425bded7cSSeth Forshee 
171525bded7cSSeth Forshee 	for (i = 12; i >= 0; i -= 4) {
171625bded7cSSeth Forshee 		nibble = (addr >> i) & 0xf;
171725bded7cSSeth Forshee 		if (alps_command_mode_send_nibble(psmouse, nibble))
171825bded7cSSeth Forshee 			return -1;
171925bded7cSSeth Forshee 	}
172025bded7cSSeth Forshee 
172125bded7cSSeth Forshee 	return 0;
172225bded7cSSeth Forshee }
172325bded7cSSeth Forshee 
__alps_command_mode_read_reg(struct psmouse * psmouse,int addr)172425bded7cSSeth Forshee static int __alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
172525bded7cSSeth Forshee {
172625bded7cSSeth Forshee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
172725bded7cSSeth Forshee 	unsigned char param[4];
172825bded7cSSeth Forshee 
172925bded7cSSeth Forshee 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
173025bded7cSSeth Forshee 		return -1;
173125bded7cSSeth Forshee 
173225bded7cSSeth Forshee 	/*
173325bded7cSSeth Forshee 	 * The address being read is returned in the first two bytes
173425bded7cSSeth Forshee 	 * of the result. Check that this address matches the expected
173525bded7cSSeth Forshee 	 * address.
173625bded7cSSeth Forshee 	 */
173725bded7cSSeth Forshee 	if (addr != ((param[0] << 8) | param[1]))
173825bded7cSSeth Forshee 		return -1;
173925bded7cSSeth Forshee 
174025bded7cSSeth Forshee 	return param[2];
174125bded7cSSeth Forshee }
174225bded7cSSeth Forshee 
alps_command_mode_read_reg(struct psmouse * psmouse,int addr)174325bded7cSSeth Forshee static int alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
174425bded7cSSeth Forshee {
174525bded7cSSeth Forshee 	if (alps_command_mode_set_addr(psmouse, addr))
174625bded7cSSeth Forshee 		return -1;
174725bded7cSSeth Forshee 	return __alps_command_mode_read_reg(psmouse, addr);
174825bded7cSSeth Forshee }
174925bded7cSSeth Forshee 
__alps_command_mode_write_reg(struct psmouse * psmouse,u8 value)175025bded7cSSeth Forshee static int __alps_command_mode_write_reg(struct psmouse *psmouse, u8 value)
175125bded7cSSeth Forshee {
175225bded7cSSeth Forshee 	if (alps_command_mode_send_nibble(psmouse, (value >> 4) & 0xf))
175325bded7cSSeth Forshee 		return -1;
175425bded7cSSeth Forshee 	if (alps_command_mode_send_nibble(psmouse, value & 0xf))
175525bded7cSSeth Forshee 		return -1;
175625bded7cSSeth Forshee 	return 0;
175725bded7cSSeth Forshee }
175825bded7cSSeth Forshee 
alps_command_mode_write_reg(struct psmouse * psmouse,int addr,u8 value)175925bded7cSSeth Forshee static int alps_command_mode_write_reg(struct psmouse *psmouse, int addr,
176025bded7cSSeth Forshee 				       u8 value)
176125bded7cSSeth Forshee {
176225bded7cSSeth Forshee 	if (alps_command_mode_set_addr(psmouse, addr))
176325bded7cSSeth Forshee 		return -1;
176425bded7cSSeth Forshee 	return __alps_command_mode_write_reg(psmouse, value);
176525bded7cSSeth Forshee }
176625bded7cSSeth Forshee 
alps_rpt_cmd(struct psmouse * psmouse,int init_command,int repeated_command,unsigned char * param)176724ba9707SKevin Cernekee static int alps_rpt_cmd(struct psmouse *psmouse, int init_command,
176824ba9707SKevin Cernekee 			int repeated_command, unsigned char *param)
176924ba9707SKevin Cernekee {
177024ba9707SKevin Cernekee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
177124ba9707SKevin Cernekee 
177224ba9707SKevin Cernekee 	param[0] = 0;
177324ba9707SKevin Cernekee 	if (init_command && ps2_command(ps2dev, param, init_command))
177424ba9707SKevin Cernekee 		return -EIO;
177524ba9707SKevin Cernekee 
177624ba9707SKevin Cernekee 	if (ps2_command(ps2dev,  NULL, repeated_command) ||
177724ba9707SKevin Cernekee 	    ps2_command(ps2dev,  NULL, repeated_command) ||
177824ba9707SKevin Cernekee 	    ps2_command(ps2dev,  NULL, repeated_command))
177924ba9707SKevin Cernekee 		return -EIO;
178024ba9707SKevin Cernekee 
178124ba9707SKevin Cernekee 	param[0] = param[1] = param[2] = 0xff;
178224ba9707SKevin Cernekee 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
178324ba9707SKevin Cernekee 		return -EIO;
178424ba9707SKevin Cernekee 
178539fbe585SDmitry Torokhov 	psmouse_dbg(psmouse, "%2.2X report: %3ph\n",
178639fbe585SDmitry Torokhov 		    repeated_command, param);
178724ba9707SKevin Cernekee 	return 0;
178824ba9707SKevin Cernekee }
178924ba9707SKevin Cernekee 
alps_check_valid_firmware_id(unsigned char id[])17903808843cSYunkang Tang static bool alps_check_valid_firmware_id(unsigned char id[])
17913808843cSYunkang Tang {
17923808843cSYunkang Tang 	if (id[0] == 0x73)
17933808843cSYunkang Tang 		return true;
17943808843cSYunkang Tang 
17953808843cSYunkang Tang 	if (id[0] == 0x88 &&
17963808843cSYunkang Tang 	    (id[1] == 0x07 ||
17973808843cSYunkang Tang 	     id[1] == 0x08 ||
17983808843cSYunkang Tang 	     (id[1] & 0xf0) == 0xb0 ||
17993808843cSYunkang Tang 	     (id[1] & 0xf0) == 0xc0)) {
18003808843cSYunkang Tang 		return true;
18013808843cSYunkang Tang 	}
18023808843cSYunkang Tang 
18033808843cSYunkang Tang 	return false;
18043808843cSYunkang Tang }
18053808843cSYunkang Tang 
alps_enter_command_mode(struct psmouse * psmouse)1806d18e53fcSKevin Cernekee static int alps_enter_command_mode(struct psmouse *psmouse)
180725bded7cSSeth Forshee {
180825bded7cSSeth Forshee 	unsigned char param[4];
180925bded7cSSeth Forshee 
181024ba9707SKevin Cernekee 	if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_RESET_WRAP, param)) {
181125bded7cSSeth Forshee 		psmouse_err(psmouse, "failed to enter command mode\n");
181225bded7cSSeth Forshee 		return -1;
181325bded7cSSeth Forshee 	}
181425bded7cSSeth Forshee 
18153808843cSYunkang Tang 	if (!alps_check_valid_firmware_id(param)) {
181625bded7cSSeth Forshee 		psmouse_dbg(psmouse,
181724ba9707SKevin Cernekee 			    "unknown response while entering command mode\n");
181825bded7cSSeth Forshee 		return -1;
181925bded7cSSeth Forshee 	}
182025bded7cSSeth Forshee 	return 0;
182125bded7cSSeth Forshee }
182225bded7cSSeth Forshee 
alps_exit_command_mode(struct psmouse * psmouse)182325bded7cSSeth Forshee static inline int alps_exit_command_mode(struct psmouse *psmouse)
182425bded7cSSeth Forshee {
182525bded7cSSeth Forshee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
182625bded7cSSeth Forshee 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM))
182725bded7cSSeth Forshee 		return -1;
182825bded7cSSeth Forshee 	return 0;
182925bded7cSSeth Forshee }
183025bded7cSSeth Forshee 
18311da177e4SLinus Torvalds /*
18321da177e4SLinus Torvalds  * For DualPoint devices select the device that should respond to
18331da177e4SLinus Torvalds  * subsequent commands. It looks like glidepad is behind stickpointer,
18341da177e4SLinus Torvalds  * I'd thought it would be other way around...
18351da177e4SLinus Torvalds  */
alps_passthrough_mode_v2(struct psmouse * psmouse,bool enable)183625bded7cSSeth Forshee static int alps_passthrough_mode_v2(struct psmouse *psmouse, bool enable)
18371da177e4SLinus Torvalds {
18381da177e4SLinus Torvalds 	struct ps2dev *ps2dev = &psmouse->ps2dev;
18391da177e4SLinus Torvalds 	int cmd = enable ? PSMOUSE_CMD_SETSCALE21 : PSMOUSE_CMD_SETSCALE11;
18401da177e4SLinus Torvalds 
18411da177e4SLinus Torvalds 	if (ps2_command(ps2dev, NULL, cmd) ||
18421da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, cmd) ||
18431da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, cmd) ||
18441da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
18451da177e4SLinus Torvalds 		return -1;
18461da177e4SLinus Torvalds 
18471da177e4SLinus Torvalds 	/* we may get 3 more bytes, just ignore them */
1848c611763dSDmitry Torokhov 	ps2_drain(ps2dev, 3, 100);
18491da177e4SLinus Torvalds 
18501da177e4SLinus Torvalds 	return 0;
18511da177e4SLinus Torvalds }
18521da177e4SLinus Torvalds 
alps_absolute_mode_v1_v2(struct psmouse * psmouse)185325bded7cSSeth Forshee static int alps_absolute_mode_v1_v2(struct psmouse *psmouse)
18541da177e4SLinus Torvalds {
18551da177e4SLinus Torvalds 	struct ps2dev *ps2dev = &psmouse->ps2dev;
18561da177e4SLinus Torvalds 
18571da177e4SLinus Torvalds 	/* Try ALPS magic knock - 4 disable before enable */
18581da177e4SLinus Torvalds 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
18591da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
18601da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
18611da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
18621da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
18631da177e4SLinus Torvalds 		return -1;
18641da177e4SLinus Torvalds 
18651da177e4SLinus Torvalds 	/*
18661da177e4SLinus Torvalds 	 * Switch mouse to poll (remote) mode so motion data will not
18671da177e4SLinus Torvalds 	 * get in our way
18681da177e4SLinus Torvalds 	 */
1869ad56814fSGuenter Roeck 	return ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL);
18701da177e4SLinus Torvalds }
18711da177e4SLinus Torvalds 
alps_monitor_mode_send_word(struct psmouse * psmouse,u16 word)187295f75e91SYunkang Tang static int alps_monitor_mode_send_word(struct psmouse *psmouse, u16 word)
187395f75e91SYunkang Tang {
187495f75e91SYunkang Tang 	int i, nibble;
187595f75e91SYunkang Tang 
187695f75e91SYunkang Tang 	/*
187795f75e91SYunkang Tang 	 * b0-b11 are valid bits, send sequence is inverse.
187895f75e91SYunkang Tang 	 * e.g. when word = 0x0123, nibble send sequence is 3, 2, 1
187995f75e91SYunkang Tang 	 */
188095f75e91SYunkang Tang 	for (i = 0; i <= 8; i += 4) {
188195f75e91SYunkang Tang 		nibble = (word >> i) & 0xf;
188295f75e91SYunkang Tang 		if (alps_command_mode_send_nibble(psmouse, nibble))
188395f75e91SYunkang Tang 			return -1;
188495f75e91SYunkang Tang 	}
188595f75e91SYunkang Tang 
188695f75e91SYunkang Tang 	return 0;
188795f75e91SYunkang Tang }
188895f75e91SYunkang Tang 
alps_monitor_mode_write_reg(struct psmouse * psmouse,u16 addr,u16 value)188995f75e91SYunkang Tang static int alps_monitor_mode_write_reg(struct psmouse *psmouse,
189095f75e91SYunkang Tang 				       u16 addr, u16 value)
189195f75e91SYunkang Tang {
189295f75e91SYunkang Tang 	struct ps2dev *ps2dev = &psmouse->ps2dev;
189395f75e91SYunkang Tang 
189495f75e91SYunkang Tang 	/* 0x0A0 is the command to write the word */
189595f75e91SYunkang Tang 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE) ||
189695f75e91SYunkang Tang 	    alps_monitor_mode_send_word(psmouse, 0x0A0) ||
189795f75e91SYunkang Tang 	    alps_monitor_mode_send_word(psmouse, addr) ||
189895f75e91SYunkang Tang 	    alps_monitor_mode_send_word(psmouse, value) ||
189995f75e91SYunkang Tang 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
190095f75e91SYunkang Tang 		return -1;
190195f75e91SYunkang Tang 
190295f75e91SYunkang Tang 	return 0;
190395f75e91SYunkang Tang }
190495f75e91SYunkang Tang 
alps_monitor_mode(struct psmouse * psmouse,bool enable)190595f75e91SYunkang Tang static int alps_monitor_mode(struct psmouse *psmouse, bool enable)
190695f75e91SYunkang Tang {
190795f75e91SYunkang Tang 	struct ps2dev *ps2dev = &psmouse->ps2dev;
190895f75e91SYunkang Tang 
190995f75e91SYunkang Tang 	if (enable) {
191095f75e91SYunkang Tang 		/* EC E9 F5 F5 E7 E6 E7 E9 to enter monitor mode */
191195f75e91SYunkang Tang 		if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
191295f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO) ||
191395f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
191495f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
191595f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
191695f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
191795f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
191895f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO))
191995f75e91SYunkang Tang 			return -1;
192095f75e91SYunkang Tang 	} else {
192195f75e91SYunkang Tang 		/* EC to exit monitor mode */
192295f75e91SYunkang Tang 		if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP))
192395f75e91SYunkang Tang 			return -1;
192495f75e91SYunkang Tang 	}
192595f75e91SYunkang Tang 
192695f75e91SYunkang Tang 	return 0;
192795f75e91SYunkang Tang }
192895f75e91SYunkang Tang 
alps_absolute_mode_v6(struct psmouse * psmouse)192995f75e91SYunkang Tang static int alps_absolute_mode_v6(struct psmouse *psmouse)
193095f75e91SYunkang Tang {
193195f75e91SYunkang Tang 	u16 reg_val = 0x181;
193247fcd0f9SColin Ian King 	int ret;
193395f75e91SYunkang Tang 
193495f75e91SYunkang Tang 	/* enter monitor mode, to write the register */
193595f75e91SYunkang Tang 	if (alps_monitor_mode(psmouse, true))
193695f75e91SYunkang Tang 		return -1;
193795f75e91SYunkang Tang 
193895f75e91SYunkang Tang 	ret = alps_monitor_mode_write_reg(psmouse, 0x000, reg_val);
193995f75e91SYunkang Tang 
194095f75e91SYunkang Tang 	if (alps_monitor_mode(psmouse, false))
194195f75e91SYunkang Tang 		ret = -1;
194295f75e91SYunkang Tang 
194395f75e91SYunkang Tang 	return ret;
194495f75e91SYunkang Tang }
194595f75e91SYunkang Tang 
alps_get_status(struct psmouse * psmouse,char * param)19461da177e4SLinus Torvalds static int alps_get_status(struct psmouse *psmouse, char *param)
19471da177e4SLinus Torvalds {
19481da177e4SLinus Torvalds 	/* Get status: 0xF5 0xF5 0xF5 0xE9 */
194924ba9707SKevin Cernekee 	if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_DISABLE, param))
19501da177e4SLinus Torvalds 		return -1;
19511da177e4SLinus Torvalds 
19521da177e4SLinus Torvalds 	return 0;
19531da177e4SLinus Torvalds }
19541da177e4SLinus Torvalds 
19551da177e4SLinus Torvalds /*
19561da177e4SLinus Torvalds  * Turn touchpad tapping on or off. The sequences are:
19571da177e4SLinus Torvalds  * 0xE9 0xF5 0xF5 0xF3 0x0A to enable,
19581da177e4SLinus Torvalds  * 0xE9 0xF5 0xF5 0xE8 0x00 to disable.
19591da177e4SLinus Torvalds  * My guess that 0xE9 (GetInfo) is here as a sync point.
19601da177e4SLinus Torvalds  * For models that also have stickpointer (DualPoints) its tapping
19611da177e4SLinus Torvalds  * is controlled separately (0xE6 0xE6 0xE6 0xF3 0x14|0x0A) but
19621da177e4SLinus Torvalds  * we don't fiddle with it.
19631da177e4SLinus Torvalds  */
alps_tap_mode(struct psmouse * psmouse,int enable)19641da177e4SLinus Torvalds static int alps_tap_mode(struct psmouse *psmouse, int enable)
19651da177e4SLinus Torvalds {
19661da177e4SLinus Torvalds 	struct ps2dev *ps2dev = &psmouse->ps2dev;
19671da177e4SLinus Torvalds 	int cmd = enable ? PSMOUSE_CMD_SETRATE : PSMOUSE_CMD_SETRES;
19681da177e4SLinus Torvalds 	unsigned char tap_arg = enable ? 0x0A : 0x00;
19691da177e4SLinus Torvalds 	unsigned char param[4];
19701da177e4SLinus Torvalds 
19711da177e4SLinus Torvalds 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO) ||
19721da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
19731da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
19741da177e4SLinus Torvalds 	    ps2_command(ps2dev, &tap_arg, cmd))
19751da177e4SLinus Torvalds 		return -1;
19761da177e4SLinus Torvalds 
19771da177e4SLinus Torvalds 	if (alps_get_status(psmouse, param))
19781da177e4SLinus Torvalds 		return -1;
19791da177e4SLinus Torvalds 
19801da177e4SLinus Torvalds 	return 0;
19811da177e4SLinus Torvalds }
19821da177e4SLinus Torvalds 
1983f0d5c6f4SDmitry Torokhov /*
1984f0d5c6f4SDmitry Torokhov  * alps_poll() - poll the touchpad for current motion packet.
1985f0d5c6f4SDmitry Torokhov  * Used in resync.
1986f0d5c6f4SDmitry Torokhov  */
alps_poll(struct psmouse * psmouse)1987f0d5c6f4SDmitry Torokhov static int alps_poll(struct psmouse *psmouse)
1988f0d5c6f4SDmitry Torokhov {
1989f0d5c6f4SDmitry Torokhov 	struct alps_data *priv = psmouse->private;
1990b46615feSSeth Forshee 	unsigned char buf[sizeof(psmouse->packet)];
1991b7802c5cSDmitry Torokhov 	bool poll_failed;
1992f0d5c6f4SDmitry Torokhov 
199399df65e7SKevin Cernekee 	if (priv->flags & ALPS_PASS)
199425bded7cSSeth Forshee 		alps_passthrough_mode_v2(psmouse, true);
1995f0d5c6f4SDmitry Torokhov 
1996f0d5c6f4SDmitry Torokhov 	poll_failed = ps2_command(&psmouse->ps2dev, buf,
1997f0d5c6f4SDmitry Torokhov 				  PSMOUSE_CMD_POLL | (psmouse->pktsize << 8)) < 0;
1998f0d5c6f4SDmitry Torokhov 
199999df65e7SKevin Cernekee 	if (priv->flags & ALPS_PASS)
200025bded7cSSeth Forshee 		alps_passthrough_mode_v2(psmouse, false);
2001f0d5c6f4SDmitry Torokhov 
200299df65e7SKevin Cernekee 	if (poll_failed || (buf[0] & priv->mask0) != priv->byte0)
2003f0d5c6f4SDmitry Torokhov 		return -1;
2004f0d5c6f4SDmitry Torokhov 
2005f0d5c6f4SDmitry Torokhov 	if ((psmouse->badbyte & 0xc8) == 0x08) {
2006f0d5c6f4SDmitry Torokhov /*
2007f0d5c6f4SDmitry Torokhov  * Poll the track stick ...
2008f0d5c6f4SDmitry Torokhov  */
2009f0d5c6f4SDmitry Torokhov 		if (ps2_command(&psmouse->ps2dev, buf, PSMOUSE_CMD_POLL | (3 << 8)))
2010f0d5c6f4SDmitry Torokhov 			return -1;
2011f0d5c6f4SDmitry Torokhov 	}
2012f0d5c6f4SDmitry Torokhov 
2013f0d5c6f4SDmitry Torokhov 	memcpy(psmouse->packet, buf, sizeof(buf));
2014f0d5c6f4SDmitry Torokhov 	return 0;
2015f0d5c6f4SDmitry Torokhov }
2016f0d5c6f4SDmitry Torokhov 
alps_hw_init_v1_v2(struct psmouse * psmouse)201725bded7cSSeth Forshee static int alps_hw_init_v1_v2(struct psmouse *psmouse)
20181da177e4SLinus Torvalds {
20191da177e4SLinus Torvalds 	struct alps_data *priv = psmouse->private;
20201da177e4SLinus Torvalds 
202199df65e7SKevin Cernekee 	if ((priv->flags & ALPS_PASS) &&
202225bded7cSSeth Forshee 	    alps_passthrough_mode_v2(psmouse, true)) {
20231da177e4SLinus Torvalds 		return -1;
2024b7802c5cSDmitry Torokhov 	}
20251da177e4SLinus Torvalds 
2026b7802c5cSDmitry Torokhov 	if (alps_tap_mode(psmouse, true)) {
2027b5d21704SDmitry Torokhov 		psmouse_warn(psmouse, "Failed to enable hardware tapping\n");
20281da177e4SLinus Torvalds 		return -1;
2029963f626dSPeter Osterlund 	}
20301da177e4SLinus Torvalds 
203125bded7cSSeth Forshee 	if (alps_absolute_mode_v1_v2(psmouse)) {
2032b5d21704SDmitry Torokhov 		psmouse_err(psmouse, "Failed to enable absolute mode\n");
20331da177e4SLinus Torvalds 		return -1;
20341da177e4SLinus Torvalds 	}
20351da177e4SLinus Torvalds 
203699df65e7SKevin Cernekee 	if ((priv->flags & ALPS_PASS) &&
203725bded7cSSeth Forshee 	    alps_passthrough_mode_v2(psmouse, false)) {
20381da177e4SLinus Torvalds 		return -1;
2039b7802c5cSDmitry Torokhov 	}
20401da177e4SLinus Torvalds 
20411e0c5b12SDmitry Torokhov 	/* ALPS needs stream mode, otherwise it won't report any data */
20421e0c5b12SDmitry Torokhov 	if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)) {
2043b5d21704SDmitry Torokhov 		psmouse_err(psmouse, "Failed to enable stream mode\n");
20441e0c5b12SDmitry Torokhov 		return -1;
20451e0c5b12SDmitry Torokhov 	}
20461e0c5b12SDmitry Torokhov 
20471e0c5b12SDmitry Torokhov 	return 0;
20481e0c5b12SDmitry Torokhov }
20491e0c5b12SDmitry Torokhov 
2050d91abc21SPali Rohár /* Must be in passthrough mode when calling this function */
alps_trackstick_enter_extended_mode_v3_v6(struct psmouse * psmouse)2051d91abc21SPali Rohár static int alps_trackstick_enter_extended_mode_v3_v6(struct psmouse *psmouse)
205295f75e91SYunkang Tang {
205395f75e91SYunkang Tang 	unsigned char param[2] = {0xC8, 0x14};
205495f75e91SYunkang Tang 
205595f75e91SYunkang Tang 	if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
205695f75e91SYunkang Tang 	    ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
205795f75e91SYunkang Tang 	    ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
205895f75e91SYunkang Tang 	    ps2_command(&psmouse->ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
205995f75e91SYunkang Tang 	    ps2_command(&psmouse->ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
206095f75e91SYunkang Tang 		return -1;
206195f75e91SYunkang Tang 
2062d91abc21SPali Rohár 	return 0;
2063d91abc21SPali Rohár }
2064d91abc21SPali Rohár 
alps_hw_init_v6(struct psmouse * psmouse)2065d91abc21SPali Rohár static int alps_hw_init_v6(struct psmouse *psmouse)
2066d91abc21SPali Rohár {
2067d91abc21SPali Rohár 	int ret;
2068d91abc21SPali Rohár 
2069d91abc21SPali Rohár 	/* Enter passthrough mode to let trackpoint enter 6byte raw mode */
2070d91abc21SPali Rohár 	if (alps_passthrough_mode_v2(psmouse, true))
2071d91abc21SPali Rohár 		return -1;
2072d91abc21SPali Rohár 
2073d91abc21SPali Rohár 	ret = alps_trackstick_enter_extended_mode_v3_v6(psmouse);
2074d91abc21SPali Rohár 
207595f75e91SYunkang Tang 	if (alps_passthrough_mode_v2(psmouse, false))
207695f75e91SYunkang Tang 		return -1;
207795f75e91SYunkang Tang 
2078d91abc21SPali Rohár 	if (ret)
2079d91abc21SPali Rohár 		return ret;
2080d91abc21SPali Rohár 
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  */
alps_passthrough_mode_v3(struct psmouse * psmouse,int reg_base,bool enable)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 */
alps_absolute_mode_v3(struct psmouse * psmouse)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 
alps_probe_trackstick_v3_v7(struct psmouse * psmouse,int reg_base)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 
alps_setup_trackstick_v3(struct psmouse * psmouse,int reg_base)2152cd401204SKevin Cernekee static int alps_setup_trackstick_v3(struct psmouse *psmouse, int reg_base)
2153cd401204SKevin Cernekee {
2154cd401204SKevin Cernekee 	int ret = 0;
2155d91abc21SPali Rohár 	int reg_val;
2156cd401204SKevin Cernekee 	unsigned char param[4];
2157cd401204SKevin Cernekee 
2158d91abc21SPali Rohár 	/*
2159d91abc21SPali Rohár 	 * We need to configure trackstick to report data for touchpad in
2160d91abc21SPali Rohár 	 * extended format. And also we need to tell touchpad to expect data
2161d91abc21SPali Rohár 	 * from trackstick in extended format. Without this configuration
2162d91abc21SPali Rohár 	 * trackstick packets sent from touchpad are in basic format which is
2163d91abc21SPali Rohár 	 * different from what we expect.
2164d91abc21SPali Rohár 	 */
2165d91abc21SPali Rohár 
2166cd401204SKevin Cernekee 	if (alps_passthrough_mode_v3(psmouse, reg_base, true))
2167cd401204SKevin Cernekee 		return -EIO;
216825bded7cSSeth Forshee 
216925bded7cSSeth Forshee 	/*
217025bded7cSSeth Forshee 	 * E7 report for the trackstick
217125bded7cSSeth Forshee 	 *
217225bded7cSSeth Forshee 	 * There have been reports of failures to seem to trace back
217325bded7cSSeth Forshee 	 * to the above trackstick check failing. When these occur
217425bded7cSSeth Forshee 	 * this E7 report fails, so when that happens we continue
217525bded7cSSeth Forshee 	 * with the assumption that there isn't a trackstick after
217625bded7cSSeth Forshee 	 * all.
217725bded7cSSeth Forshee 	 */
2178cd401204SKevin Cernekee 	if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_SETSCALE21, param)) {
2179a09221e8SDmitry Torokhov 		psmouse_warn(psmouse, "Failed to initialize trackstick (E7 report failed)\n");
2180cd401204SKevin Cernekee 		ret = -ENODEV;
218125bded7cSSeth Forshee 	} else {
218239fbe585SDmitry Torokhov 		psmouse_dbg(psmouse, "trackstick E7 report: %3ph\n", param);
2183d91abc21SPali Rohár 		if (alps_trackstick_enter_extended_mode_v3_v6(psmouse)) {
2184d91abc21SPali Rohár 			psmouse_err(psmouse, "Failed to enter into trackstick extended mode\n");
2185cd401204SKevin Cernekee 			ret = -EIO;
2186cd401204SKevin Cernekee 		}
2187d91abc21SPali Rohár 	}
2188cd401204SKevin Cernekee 
2189cd401204SKevin Cernekee 	if (alps_passthrough_mode_v3(psmouse, reg_base, false))
2190d91abc21SPali Rohár 		return -EIO;
2191d91abc21SPali Rohár 
2192d91abc21SPali Rohár 	if (ret)
2193d91abc21SPali Rohár 		return ret;
2194d91abc21SPali Rohár 
2195d91abc21SPali Rohár 	if (alps_enter_command_mode(psmouse))
2196d91abc21SPali Rohár 		return -EIO;
2197d91abc21SPali Rohár 
2198d91abc21SPali Rohár 	reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x08);
2199d91abc21SPali Rohár 	if (reg_val == -1) {
2200cd401204SKevin Cernekee 		ret = -EIO;
2201d91abc21SPali Rohár 	} else {
2202d91abc21SPali Rohár 		/*
2203d91abc21SPali Rohár 		 * Tell touchpad that trackstick is now in extended mode.
2204d91abc21SPali Rohár 		 * If bit 1 isn't set the packet format is different.
2205d91abc21SPali Rohár 		 */
2206d91abc21SPali Rohár 		reg_val |= BIT(1);
2207d91abc21SPali Rohár 		if (__alps_command_mode_write_reg(psmouse, reg_val))
2208d91abc21SPali Rohár 			ret = -EIO;
2209d91abc21SPali Rohár 	}
2210d91abc21SPali Rohár 
2211d91abc21SPali Rohár 	if (alps_exit_command_mode(psmouse))
2212d91abc21SPali Rohár 		return -EIO;
2213cd401204SKevin Cernekee 
2214cd401204SKevin Cernekee 	return ret;
2215cd401204SKevin Cernekee }
2216cd401204SKevin Cernekee 
alps_hw_init_v3(struct psmouse * psmouse)2217cd401204SKevin Cernekee static int alps_hw_init_v3(struct psmouse *psmouse)
2218cd401204SKevin Cernekee {
22194b1af853SPali Rohár 	struct alps_data *priv = psmouse->private;
2220cd401204SKevin Cernekee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
2221cd401204SKevin Cernekee 	int reg_val;
2222cd401204SKevin Cernekee 	unsigned char param[4];
2223cd401204SKevin Cernekee 
22244b1af853SPali Rohár 	if ((priv->flags & ALPS_DUALPOINT) &&
2225cd401204SKevin Cernekee 	    alps_setup_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE) == -EIO)
2226cd401204SKevin Cernekee 		goto error;
2227cd401204SKevin Cernekee 
2228d18e53fcSKevin Cernekee 	if (alps_enter_command_mode(psmouse) ||
2229cd401204SKevin Cernekee 	    alps_absolute_mode_v3(psmouse)) {
223025bded7cSSeth Forshee 		psmouse_err(psmouse, "Failed to enter absolute mode\n");
223125bded7cSSeth Forshee 		goto error;
223225bded7cSSeth Forshee 	}
223325bded7cSSeth Forshee 
223425bded7cSSeth Forshee 	reg_val = alps_command_mode_read_reg(psmouse, 0x0006);
223525bded7cSSeth Forshee 	if (reg_val == -1)
223625bded7cSSeth Forshee 		goto error;
223725bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
223825bded7cSSeth Forshee 		goto error;
223925bded7cSSeth Forshee 
224025bded7cSSeth Forshee 	reg_val = alps_command_mode_read_reg(psmouse, 0x0007);
224125bded7cSSeth Forshee 	if (reg_val == -1)
224225bded7cSSeth Forshee 		goto error;
224325bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
224425bded7cSSeth Forshee 		goto error;
224525bded7cSSeth Forshee 
224625bded7cSSeth Forshee 	if (alps_command_mode_read_reg(psmouse, 0x0144) == -1)
224725bded7cSSeth Forshee 		goto error;
224825bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, 0x04))
224925bded7cSSeth Forshee 		goto error;
225025bded7cSSeth Forshee 
225125bded7cSSeth Forshee 	if (alps_command_mode_read_reg(psmouse, 0x0159) == -1)
225225bded7cSSeth Forshee 		goto error;
225325bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, 0x03))
225425bded7cSSeth Forshee 		goto error;
225525bded7cSSeth Forshee 
225625bded7cSSeth Forshee 	if (alps_command_mode_read_reg(psmouse, 0x0163) == -1)
225725bded7cSSeth Forshee 		goto error;
225825bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0163, 0x03))
225925bded7cSSeth Forshee 		goto error;
226025bded7cSSeth Forshee 
226125bded7cSSeth Forshee 	if (alps_command_mode_read_reg(psmouse, 0x0162) == -1)
226225bded7cSSeth Forshee 		goto error;
226325bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0162, 0x04))
226425bded7cSSeth Forshee 		goto error;
226525bded7cSSeth Forshee 
226625bded7cSSeth Forshee 	alps_exit_command_mode(psmouse);
226725bded7cSSeth Forshee 
226825bded7cSSeth Forshee 	/* Set rate and enable data reporting */
226925bded7cSSeth Forshee 	param[0] = 0x64;
227025bded7cSSeth Forshee 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
227125bded7cSSeth Forshee 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
227225bded7cSSeth Forshee 		psmouse_err(psmouse, "Failed to enable data reporting\n");
227325bded7cSSeth Forshee 		return -1;
227425bded7cSSeth Forshee 	}
227525bded7cSSeth Forshee 
227625bded7cSSeth Forshee 	return 0;
227725bded7cSSeth Forshee 
227825bded7cSSeth Forshee error:
227925bded7cSSeth Forshee 	/*
228025bded7cSSeth Forshee 	 * Leaving the touchpad in command mode will essentially render
228125bded7cSSeth Forshee 	 * it unusable until the machine reboots, so exit it here just
228225bded7cSSeth Forshee 	 * to be safe
228325bded7cSSeth Forshee 	 */
228425bded7cSSeth Forshee 	alps_exit_command_mode(psmouse);
228525bded7cSSeth Forshee 	return -1;
228625bded7cSSeth Forshee }
228725bded7cSSeth Forshee 
alps_get_v3_v7_resolution(struct psmouse * psmouse,int reg_pitch)2288f3f33c67SHans de Goede static int alps_get_v3_v7_resolution(struct psmouse *psmouse, int reg_pitch)
2289f3f33c67SHans de Goede {
2290f3f33c67SHans de Goede 	int reg, x_pitch, y_pitch, x_electrode, y_electrode, x_phys, y_phys;
2291f3f33c67SHans de Goede 	struct alps_data *priv = psmouse->private;
2292f3f33c67SHans de Goede 
2293f3f33c67SHans de Goede 	reg = alps_command_mode_read_reg(psmouse, reg_pitch);
2294f3f33c67SHans de Goede 	if (reg < 0)
2295f3f33c67SHans de Goede 		return reg;
2296f3f33c67SHans de Goede 
2297*754ff506Smsizanoen 	x_pitch = (s8)(reg << 4) >> 4; /* sign extend lower 4 bits */
2298f3f33c67SHans de Goede 	x_pitch = 50 + 2 * x_pitch; /* In 0.1 mm units */
2299f3f33c67SHans de Goede 
2300*754ff506Smsizanoen 	y_pitch = (s8)reg >> 4; /* sign extend upper 4 bits */
2301f3f33c67SHans de Goede 	y_pitch = 36 + 2 * y_pitch; /* In 0.1 mm units */
2302f3f33c67SHans de Goede 
2303f3f33c67SHans de Goede 	reg = alps_command_mode_read_reg(psmouse, reg_pitch + 1);
2304f3f33c67SHans de Goede 	if (reg < 0)
2305f3f33c67SHans de Goede 		return reg;
2306f3f33c67SHans de Goede 
2307*754ff506Smsizanoen 	x_electrode = (s8)(reg << 4) >> 4; /* sign extend lower 4 bits */
2308f3f33c67SHans de Goede 	x_electrode = 17 + x_electrode;
2309f3f33c67SHans de Goede 
2310*754ff506Smsizanoen 	y_electrode = (s8)reg >> 4; /* sign extend upper 4 bits */
2311f3f33c67SHans de Goede 	y_electrode = 13 + y_electrode;
2312f3f33c67SHans de Goede 
2313f3f33c67SHans de Goede 	x_phys = x_pitch * (x_electrode - 1); /* In 0.1 mm units */
2314f3f33c67SHans de Goede 	y_phys = y_pitch * (y_electrode - 1); /* In 0.1 mm units */
2315f3f33c67SHans de Goede 
2316f3f33c67SHans de Goede 	priv->x_res = priv->x_max * 10 / x_phys; /* units / mm */
2317f3f33c67SHans de Goede 	priv->y_res = priv->y_max * 10 / y_phys; /* units / mm */
2318f3f33c67SHans de Goede 
2319f3f33c67SHans de Goede 	psmouse_dbg(psmouse,
2320f3f33c67SHans de Goede 		    "pitch %dx%d num-electrodes %dx%d physical size %dx%d mm res %dx%d\n",
2321f3f33c67SHans de Goede 		    x_pitch, y_pitch, x_electrode, y_electrode,
2322f3f33c67SHans de Goede 		    x_phys / 10, y_phys / 10, priv->x_res, priv->y_res);
2323f3f33c67SHans de Goede 
2324f3f33c67SHans de Goede 	return 0;
2325f3f33c67SHans de Goede }
2326f3f33c67SHans de Goede 
alps_hw_init_rushmore_v3(struct psmouse * psmouse)23271302bac3SKevin Cernekee static int alps_hw_init_rushmore_v3(struct psmouse *psmouse)
23281302bac3SKevin Cernekee {
2329cd401204SKevin Cernekee 	struct alps_data *priv = psmouse->private;
23301302bac3SKevin Cernekee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
23311302bac3SKevin Cernekee 	int reg_val, ret = -1;
23321302bac3SKevin Cernekee 
2333cd401204SKevin Cernekee 	if (priv->flags & ALPS_DUALPOINT) {
2334cd401204SKevin Cernekee 		reg_val = alps_setup_trackstick_v3(psmouse,
2335cd401204SKevin Cernekee 						   ALPS_REG_BASE_RUSHMORE);
2336cd401204SKevin Cernekee 		if (reg_val == -EIO)
2337cd401204SKevin Cernekee 			goto error;
2338cd401204SKevin Cernekee 	}
2339cd401204SKevin Cernekee 
2340d18e53fcSKevin Cernekee 	if (alps_enter_command_mode(psmouse) ||
23411302bac3SKevin Cernekee 	    alps_command_mode_read_reg(psmouse, 0xc2d9) == -1 ||
23421302bac3SKevin Cernekee 	    alps_command_mode_write_reg(psmouse, 0xc2cb, 0x00))
23431302bac3SKevin Cernekee 		goto error;
23441302bac3SKevin Cernekee 
2345f3f33c67SHans de Goede 	if (alps_get_v3_v7_resolution(psmouse, 0xc2da))
2346f3f33c67SHans de Goede 		goto error;
2347f3f33c67SHans de Goede 
23481302bac3SKevin Cernekee 	reg_val = alps_command_mode_read_reg(psmouse, 0xc2c6);
23491302bac3SKevin Cernekee 	if (reg_val == -1)
23501302bac3SKevin Cernekee 		goto error;
23511302bac3SKevin Cernekee 	if (__alps_command_mode_write_reg(psmouse, reg_val & 0xfd))
23521302bac3SKevin Cernekee 		goto error;
23531302bac3SKevin Cernekee 
23541302bac3SKevin Cernekee 	if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
23551302bac3SKevin Cernekee 		goto error;
23561302bac3SKevin Cernekee 
23571302bac3SKevin Cernekee 	/* enter absolute mode */
23581302bac3SKevin Cernekee 	reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
23591302bac3SKevin Cernekee 	if (reg_val == -1)
23601302bac3SKevin Cernekee 		goto error;
23611302bac3SKevin Cernekee 	if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
23621302bac3SKevin Cernekee 		goto error;
23631302bac3SKevin Cernekee 
23641302bac3SKevin Cernekee 	alps_exit_command_mode(psmouse);
23651302bac3SKevin Cernekee 	return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
23661302bac3SKevin Cernekee 
23671302bac3SKevin Cernekee error:
23681302bac3SKevin Cernekee 	alps_exit_command_mode(psmouse);
23691302bac3SKevin Cernekee 	return ret;
23701302bac3SKevin Cernekee }
23711302bac3SKevin Cernekee 
237225bded7cSSeth Forshee /* Must be in command mode when calling this function */
alps_absolute_mode_v4(struct psmouse * psmouse)237325bded7cSSeth Forshee static int alps_absolute_mode_v4(struct psmouse *psmouse)
237425bded7cSSeth Forshee {
237525bded7cSSeth Forshee 	int reg_val;
237625bded7cSSeth Forshee 
237725bded7cSSeth Forshee 	reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
237825bded7cSSeth Forshee 	if (reg_val == -1)
237925bded7cSSeth Forshee 		return -1;
238025bded7cSSeth Forshee 
238125bded7cSSeth Forshee 	reg_val |= 0x02;
238225bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, reg_val))
238325bded7cSSeth Forshee 		return -1;
238425bded7cSSeth Forshee 
238525bded7cSSeth Forshee 	return 0;
238625bded7cSSeth Forshee }
238725bded7cSSeth Forshee 
alps_hw_init_v4(struct psmouse * psmouse)238825bded7cSSeth Forshee static int alps_hw_init_v4(struct psmouse *psmouse)
238925bded7cSSeth Forshee {
239025bded7cSSeth Forshee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
239125bded7cSSeth Forshee 	unsigned char param[4];
239225bded7cSSeth Forshee 
2393d18e53fcSKevin Cernekee 	if (alps_enter_command_mode(psmouse))
239425bded7cSSeth Forshee 		goto error;
239525bded7cSSeth Forshee 
239625bded7cSSeth Forshee 	if (alps_absolute_mode_v4(psmouse)) {
239725bded7cSSeth Forshee 		psmouse_err(psmouse, "Failed to enter absolute mode\n");
239825bded7cSSeth Forshee 		goto error;
239925bded7cSSeth Forshee 	}
240025bded7cSSeth Forshee 
240125bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0007, 0x8c))
240225bded7cSSeth Forshee 		goto error;
240325bded7cSSeth Forshee 
240425bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0149, 0x03))
240525bded7cSSeth Forshee 		goto error;
240625bded7cSSeth Forshee 
240725bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0160, 0x03))
240825bded7cSSeth Forshee 		goto error;
240925bded7cSSeth Forshee 
241025bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x017f, 0x15))
241125bded7cSSeth Forshee 		goto error;
241225bded7cSSeth Forshee 
241325bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0151, 0x01))
241425bded7cSSeth Forshee 		goto error;
241525bded7cSSeth Forshee 
241625bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0168, 0x03))
241725bded7cSSeth Forshee 		goto error;
241825bded7cSSeth Forshee 
241925bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x014a, 0x03))
242025bded7cSSeth Forshee 		goto error;
242125bded7cSSeth Forshee 
242225bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0161, 0x03))
242325bded7cSSeth Forshee 		goto error;
242425bded7cSSeth Forshee 
242525bded7cSSeth Forshee 	alps_exit_command_mode(psmouse);
242625bded7cSSeth Forshee 
242725bded7cSSeth Forshee 	/*
242825bded7cSSeth Forshee 	 * This sequence changes the output from a 9-byte to an
242925bded7cSSeth Forshee 	 * 8-byte format. All the same data seems to be present,
243025bded7cSSeth Forshee 	 * just in a more compact format.
243125bded7cSSeth Forshee 	 */
243225bded7cSSeth Forshee 	param[0] = 0xc8;
243325bded7cSSeth Forshee 	param[1] = 0x64;
243425bded7cSSeth Forshee 	param[2] = 0x50;
243525bded7cSSeth Forshee 	if (ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
243625bded7cSSeth Forshee 	    ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE) ||
243725bded7cSSeth Forshee 	    ps2_command(ps2dev, &param[2], PSMOUSE_CMD_SETRATE) ||
243825bded7cSSeth Forshee 	    ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
243925bded7cSSeth Forshee 		return -1;
244025bded7cSSeth Forshee 
244125bded7cSSeth Forshee 	/* Set rate and enable data reporting */
244225bded7cSSeth Forshee 	param[0] = 0x64;
244325bded7cSSeth Forshee 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
244425bded7cSSeth Forshee 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
244525bded7cSSeth Forshee 		psmouse_err(psmouse, "Failed to enable data reporting\n");
244625bded7cSSeth Forshee 		return -1;
244725bded7cSSeth Forshee 	}
244825bded7cSSeth Forshee 
244925bded7cSSeth Forshee 	return 0;
245025bded7cSSeth Forshee 
245125bded7cSSeth Forshee error:
245225bded7cSSeth Forshee 	/*
245325bded7cSSeth Forshee 	 * Leaving the touchpad in command mode will essentially render
245425bded7cSSeth Forshee 	 * it unusable until the machine reboots, so exit it here just
245525bded7cSSeth Forshee 	 * to be safe
245625bded7cSSeth Forshee 	 */
245725bded7cSSeth Forshee 	alps_exit_command_mode(psmouse);
245825bded7cSSeth Forshee 	return -1;
245925bded7cSSeth Forshee }
246025bded7cSSeth Forshee 
alps_get_otp_values_ss4_v2(struct psmouse * psmouse,unsigned char index,unsigned char otp[])24613db5b9f7SMasaki Ota static int alps_get_otp_values_ss4_v2(struct psmouse *psmouse,
24623db5b9f7SMasaki Ota 				      unsigned char index, unsigned char otp[])
24633db5b9f7SMasaki Ota {
24643db5b9f7SMasaki Ota 	struct ps2dev *ps2dev = &psmouse->ps2dev;
24653db5b9f7SMasaki Ota 
24663db5b9f7SMasaki Ota 	switch (index) {
24673db5b9f7SMasaki Ota 	case 0:
24683db5b9f7SMasaki Ota 		if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)  ||
24693db5b9f7SMasaki Ota 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)  ||
24703db5b9f7SMasaki Ota 		    ps2_command(ps2dev, otp, PSMOUSE_CMD_GETINFO))
24713db5b9f7SMasaki Ota 			return -1;
24723db5b9f7SMasaki Ota 
24733db5b9f7SMasaki Ota 		break;
24743db5b9f7SMasaki Ota 
24753db5b9f7SMasaki Ota 	case 1:
24763db5b9f7SMasaki Ota 		if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL)  ||
24773db5b9f7SMasaki Ota 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL)  ||
24783db5b9f7SMasaki Ota 		    ps2_command(ps2dev, otp, PSMOUSE_CMD_GETINFO))
24793db5b9f7SMasaki Ota 			return -1;
24803db5b9f7SMasaki Ota 
24813db5b9f7SMasaki Ota 		break;
24823db5b9f7SMasaki Ota 	}
24833db5b9f7SMasaki Ota 
24843db5b9f7SMasaki Ota 	return 0;
24853db5b9f7SMasaki Ota }
24863db5b9f7SMasaki Ota 
alps_update_device_area_ss4_v2(unsigned char otp[][4],struct alps_data * priv)248707f19e3dSFengguang Wu static int alps_update_device_area_ss4_v2(unsigned char otp[][4],
24883db5b9f7SMasaki Ota 					  struct alps_data *priv)
24893db5b9f7SMasaki Ota {
24903db5b9f7SMasaki Ota 	int num_x_electrode;
24913db5b9f7SMasaki Ota 	int num_y_electrode;
24923db5b9f7SMasaki Ota 	int x_pitch, y_pitch, x_phys, y_phys;
24933db5b9f7SMasaki Ota 
2494e7348396SMasaki Ota 	if (IS_SS4PLUS_DEV(priv->dev_id)) {
2495e7348396SMasaki Ota 		num_x_electrode =
2496e7348396SMasaki Ota 			SS4PLUS_NUMSENSOR_XOFFSET + (otp[0][2] & 0x0F);
2497e7348396SMasaki Ota 		num_y_electrode =
2498e7348396SMasaki Ota 			SS4PLUS_NUMSENSOR_YOFFSET + ((otp[0][2] >> 4) & 0x0F);
24993db5b9f7SMasaki Ota 
2500e7348396SMasaki Ota 		priv->x_max =
2501e7348396SMasaki Ota 			(num_x_electrode - 1) * SS4PLUS_COUNT_PER_ELECTRODE;
2502e7348396SMasaki Ota 		priv->y_max =
2503e7348396SMasaki Ota 			(num_y_electrode - 1) * SS4PLUS_COUNT_PER_ELECTRODE;
2504e7348396SMasaki Ota 
2505e7348396SMasaki Ota 		x_pitch = (otp[0][1] & 0x0F) + SS4PLUS_MIN_PITCH_MM;
2506e7348396SMasaki Ota 		y_pitch = ((otp[0][1] >> 4) & 0x0F) + SS4PLUS_MIN_PITCH_MM;
2507e7348396SMasaki Ota 
2508e7348396SMasaki Ota 	} else {
2509e7348396SMasaki Ota 		num_x_electrode =
2510e7348396SMasaki Ota 			SS4_NUMSENSOR_XOFFSET + (otp[1][0] & 0x0F);
2511e7348396SMasaki Ota 		num_y_electrode =
2512e7348396SMasaki Ota 			SS4_NUMSENSOR_YOFFSET + ((otp[1][0] >> 4) & 0x0F);
2513e7348396SMasaki Ota 
2514e7348396SMasaki Ota 		priv->x_max =
2515e7348396SMasaki Ota 			(num_x_electrode - 1) * SS4_COUNT_PER_ELECTRODE;
2516e7348396SMasaki Ota 		priv->y_max =
2517e7348396SMasaki Ota 			(num_y_electrode - 1) * SS4_COUNT_PER_ELECTRODE;
25183db5b9f7SMasaki Ota 
25193db5b9f7SMasaki Ota 		x_pitch = ((otp[1][2] >> 2) & 0x07) + SS4_MIN_PITCH_MM;
25203db5b9f7SMasaki Ota 		y_pitch = ((otp[1][2] >> 5) & 0x07) + SS4_MIN_PITCH_MM;
2521e7348396SMasaki Ota 	}
25223db5b9f7SMasaki Ota 
25233db5b9f7SMasaki Ota 	x_phys = x_pitch * (num_x_electrode - 1); /* In 0.1 mm units */
25243db5b9f7SMasaki Ota 	y_phys = y_pitch * (num_y_electrode - 1); /* In 0.1 mm units */
25253db5b9f7SMasaki Ota 
25263db5b9f7SMasaki Ota 	priv->x_res = priv->x_max * 10 / x_phys; /* units / mm */
25273db5b9f7SMasaki Ota 	priv->y_res = priv->y_max * 10 / y_phys; /* units / mm */
25283db5b9f7SMasaki Ota 
25293db5b9f7SMasaki Ota 	return 0;
25303db5b9f7SMasaki Ota }
25313db5b9f7SMasaki Ota 
alps_update_btn_info_ss4_v2(unsigned char otp[][4],struct alps_data * priv)253207f19e3dSFengguang Wu static int alps_update_btn_info_ss4_v2(unsigned char otp[][4],
253307f19e3dSFengguang Wu 				       struct alps_data *priv)
25343db5b9f7SMasaki Ota {
25353db5b9f7SMasaki Ota 	unsigned char is_btnless;
25363db5b9f7SMasaki Ota 
2537e7348396SMasaki Ota 	if (IS_SS4PLUS_DEV(priv->dev_id))
2538e7348396SMasaki Ota 		is_btnless = (otp[1][0] >> 1) & 0x01;
2539e7348396SMasaki Ota 	else
25403db5b9f7SMasaki Ota 		is_btnless = (otp[1][1] >> 3) & 0x01;
25413db5b9f7SMasaki Ota 
25423db5b9f7SMasaki Ota 	if (is_btnless)
25433db5b9f7SMasaki Ota 		priv->flags |= ALPS_BUTTONPAD;
25443db5b9f7SMasaki Ota 
25453db5b9f7SMasaki Ota 	return 0;
25463db5b9f7SMasaki Ota }
25473db5b9f7SMasaki Ota 
alps_update_dual_info_ss4_v2(unsigned char otp[][4],struct alps_data * priv,struct psmouse * psmouse)2548e7348396SMasaki Ota static int alps_update_dual_info_ss4_v2(unsigned char otp[][4],
2549567b9b54SMasaki Ota 					struct alps_data *priv,
2550567b9b54SMasaki Ota 					struct psmouse *psmouse)
2551e7348396SMasaki Ota {
2552e7348396SMasaki Ota 	bool is_dual = false;
2553567b9b54SMasaki Ota 	int reg_val = 0;
2554567b9b54SMasaki Ota 	struct ps2dev *ps2dev = &psmouse->ps2dev;
2555e7348396SMasaki Ota 
2556567b9b54SMasaki Ota 	if (IS_SS4PLUS_DEV(priv->dev_id)) {
2557e7348396SMasaki Ota 		is_dual = (otp[0][0] >> 4) & 0x01;
2558e7348396SMasaki Ota 
2559567b9b54SMasaki Ota 		if (!is_dual) {
2560567b9b54SMasaki Ota 			/* For support TrackStick of Thinkpad L/E series */
2561567b9b54SMasaki Ota 			if (alps_exit_command_mode(psmouse) == 0 &&
2562567b9b54SMasaki Ota 				alps_enter_command_mode(psmouse) == 0) {
2563567b9b54SMasaki Ota 				reg_val = alps_command_mode_read_reg(psmouse,
2564567b9b54SMasaki Ota 									0xD7);
2565567b9b54SMasaki Ota 			}
2566567b9b54SMasaki Ota 			alps_exit_command_mode(psmouse);
2567567b9b54SMasaki Ota 			ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
2568567b9b54SMasaki Ota 
2569567b9b54SMasaki Ota 			if (reg_val == 0x0C || reg_val == 0x1D)
2570567b9b54SMasaki Ota 				is_dual = true;
2571567b9b54SMasaki Ota 		}
2572567b9b54SMasaki Ota 	}
2573567b9b54SMasaki Ota 
2574e7348396SMasaki Ota 	if (is_dual)
2575e7348396SMasaki Ota 		priv->flags |= ALPS_DUALPOINT |
2576e7348396SMasaki Ota 					ALPS_DUALPOINT_WITH_PRESSURE;
2577e7348396SMasaki Ota 
2578e7348396SMasaki Ota 	return 0;
2579e7348396SMasaki Ota }
2580e7348396SMasaki Ota 
alps_set_defaults_ss4_v2(struct psmouse * psmouse,struct alps_data * priv)25813db5b9f7SMasaki Ota static int alps_set_defaults_ss4_v2(struct psmouse *psmouse,
25823db5b9f7SMasaki Ota 				    struct alps_data *priv)
25833db5b9f7SMasaki Ota {
25843db5b9f7SMasaki Ota 	unsigned char otp[2][4];
25853db5b9f7SMasaki Ota 
25863db5b9f7SMasaki Ota 	memset(otp, 0, sizeof(otp));
25873db5b9f7SMasaki Ota 
25884a646580SMasaki Ota 	if (alps_get_otp_values_ss4_v2(psmouse, 1, &otp[1][0]) ||
25894a646580SMasaki Ota 	    alps_get_otp_values_ss4_v2(psmouse, 0, &otp[0][0]))
25903db5b9f7SMasaki Ota 		return -1;
25913db5b9f7SMasaki Ota 
25923db5b9f7SMasaki Ota 	alps_update_device_area_ss4_v2(otp, priv);
25933db5b9f7SMasaki Ota 
25943db5b9f7SMasaki Ota 	alps_update_btn_info_ss4_v2(otp, priv);
25953db5b9f7SMasaki Ota 
2596567b9b54SMasaki Ota 	alps_update_dual_info_ss4_v2(otp, priv, psmouse);
2597e7348396SMasaki Ota 
25983db5b9f7SMasaki Ota 	return 0;
25993db5b9f7SMasaki Ota }
26003db5b9f7SMasaki Ota 
alps_dolphin_get_device_area(struct psmouse * psmouse,struct alps_data * priv)2601ee65d4b3SYunkang Tang static int alps_dolphin_get_device_area(struct psmouse *psmouse,
2602ee65d4b3SYunkang Tang 					struct alps_data *priv)
2603ee65d4b3SYunkang Tang {
2604ee65d4b3SYunkang Tang 	struct ps2dev *ps2dev = &psmouse->ps2dev;
2605ee65d4b3SYunkang Tang 	unsigned char param[4] = {0};
2606ee65d4b3SYunkang Tang 	int num_x_electrode, num_y_electrode;
2607ee65d4b3SYunkang Tang 
2608ee65d4b3SYunkang Tang 	if (alps_enter_command_mode(psmouse))
2609ee65d4b3SYunkang Tang 		return -1;
2610ee65d4b3SYunkang Tang 
2611ee65d4b3SYunkang Tang 	param[0] = 0x0a;
2612ee65d4b3SYunkang Tang 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
2613ee65d4b3SYunkang Tang 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2614ee65d4b3SYunkang Tang 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2615ee65d4b3SYunkang Tang 	    ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2616ee65d4b3SYunkang Tang 	    ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE))
2617ee65d4b3SYunkang Tang 		return -1;
2618ee65d4b3SYunkang Tang 
2619ee65d4b3SYunkang Tang 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
2620ee65d4b3SYunkang Tang 		return -1;
2621ee65d4b3SYunkang Tang 
2622ee65d4b3SYunkang Tang 	/*
2623ee65d4b3SYunkang Tang 	 * Dolphin's sensor line number is not fixed. It can be calculated
2624ee65d4b3SYunkang Tang 	 * by adding the device's register value with DOLPHIN_PROFILE_X/YOFFSET.
2625ee65d4b3SYunkang Tang 	 * Further more, we can get device's x_max and y_max by multiplying
2626ee65d4b3SYunkang Tang 	 * sensor line number with DOLPHIN_COUNT_PER_ELECTRODE.
2627ee65d4b3SYunkang Tang 	 *
2628ee65d4b3SYunkang Tang 	 * e.g. When we get register's sensor_x = 11 & sensor_y = 8,
2629ee65d4b3SYunkang Tang 	 *	real sensor line number X = 11 + 8 = 19, and
2630ee65d4b3SYunkang Tang 	 *	real sensor line number Y = 8 + 1 = 9.
2631ee65d4b3SYunkang Tang 	 *	So, x_max = (19 - 1) * 64 = 1152, and
2632ee65d4b3SYunkang Tang 	 *	    y_max = (9 - 1) * 64 = 512.
2633ee65d4b3SYunkang Tang 	 */
2634ee65d4b3SYunkang Tang 	num_x_electrode = DOLPHIN_PROFILE_XOFFSET + (param[2] & 0x0F);
2635ee65d4b3SYunkang Tang 	num_y_electrode = DOLPHIN_PROFILE_YOFFSET + ((param[2] >> 4) & 0x0F);
2636ee65d4b3SYunkang Tang 	priv->x_bits = num_x_electrode;
2637ee65d4b3SYunkang Tang 	priv->y_bits = num_y_electrode;
2638ee65d4b3SYunkang Tang 	priv->x_max = (num_x_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
2639ee65d4b3SYunkang Tang 	priv->y_max = (num_y_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
2640ee65d4b3SYunkang Tang 
2641ee65d4b3SYunkang Tang 	if (alps_exit_command_mode(psmouse))
2642ee65d4b3SYunkang Tang 		return -1;
2643ee65d4b3SYunkang Tang 
2644ee65d4b3SYunkang Tang 	return 0;
2645ee65d4b3SYunkang Tang }
2646ee65d4b3SYunkang Tang 
alps_hw_init_dolphin_v1(struct psmouse * psmouse)264775af9e56SDave Turvene static int alps_hw_init_dolphin_v1(struct psmouse *psmouse)
264875af9e56SDave Turvene {
264975af9e56SDave Turvene 	struct ps2dev *ps2dev = &psmouse->ps2dev;
265075af9e56SDave Turvene 	unsigned char param[2];
265175af9e56SDave Turvene 
265275af9e56SDave Turvene 	/* This is dolphin "v1" as empirically defined by florin9doi */
265375af9e56SDave Turvene 	param[0] = 0x64;
265475af9e56SDave Turvene 	param[1] = 0x28;
265575af9e56SDave Turvene 
265675af9e56SDave Turvene 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
265775af9e56SDave Turvene 	    ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
265875af9e56SDave Turvene 	    ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
265975af9e56SDave Turvene 		return -1;
266075af9e56SDave Turvene 
266175af9e56SDave Turvene 	return 0;
266275af9e56SDave Turvene }
266375af9e56SDave Turvene 
alps_hw_init_v7(struct psmouse * psmouse)26643808843cSYunkang Tang static int alps_hw_init_v7(struct psmouse *psmouse)
26653808843cSYunkang Tang {
26663808843cSYunkang Tang 	struct ps2dev *ps2dev = &psmouse->ps2dev;
26673808843cSYunkang Tang 	int reg_val, ret = -1;
26683808843cSYunkang Tang 
26693808843cSYunkang Tang 	if (alps_enter_command_mode(psmouse) ||
26703808843cSYunkang Tang 	    alps_command_mode_read_reg(psmouse, 0xc2d9) == -1)
26713808843cSYunkang Tang 		goto error;
26723808843cSYunkang Tang 
2673f3f33c67SHans de Goede 	if (alps_get_v3_v7_resolution(psmouse, 0xc397))
2674f3f33c67SHans de Goede 		goto error;
2675f3f33c67SHans de Goede 
26763808843cSYunkang Tang 	if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
26773808843cSYunkang Tang 		goto error;
26783808843cSYunkang Tang 
26793808843cSYunkang Tang 	reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
26803808843cSYunkang Tang 	if (reg_val == -1)
26813808843cSYunkang Tang 		goto error;
26823808843cSYunkang Tang 	if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
26833808843cSYunkang Tang 		goto error;
26843808843cSYunkang Tang 
26853808843cSYunkang Tang 	alps_exit_command_mode(psmouse);
26863808843cSYunkang Tang 	return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
26873808843cSYunkang Tang 
26883808843cSYunkang Tang error:
26893808843cSYunkang Tang 	alps_exit_command_mode(psmouse);
26903808843cSYunkang Tang 	return ret;
26913808843cSYunkang Tang }
26923808843cSYunkang Tang 
alps_hw_init_ss4_v2(struct psmouse * psmouse)26933db5b9f7SMasaki Ota static int alps_hw_init_ss4_v2(struct psmouse *psmouse)
26943db5b9f7SMasaki Ota {
26953db5b9f7SMasaki Ota 	struct ps2dev *ps2dev = &psmouse->ps2dev;
26963db5b9f7SMasaki Ota 	char param[2] = {0x64, 0x28};
26973db5b9f7SMasaki Ota 	int ret = -1;
26983db5b9f7SMasaki Ota 
26993db5b9f7SMasaki Ota 	/* enter absolute mode */
27003db5b9f7SMasaki Ota 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
27013db5b9f7SMasaki Ota 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
27023db5b9f7SMasaki Ota 	    ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
27033db5b9f7SMasaki Ota 	    ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE)) {
27043db5b9f7SMasaki Ota 		goto error;
27053db5b9f7SMasaki Ota 	}
27063db5b9f7SMasaki Ota 
27073db5b9f7SMasaki Ota 	/* T.B.D. Decread noise packet number, delete in the future */
27083db5b9f7SMasaki Ota 	if (alps_exit_command_mode(psmouse) ||
27093db5b9f7SMasaki Ota 	    alps_enter_command_mode(psmouse) ||
27103db5b9f7SMasaki Ota 	    alps_command_mode_write_reg(psmouse, 0x001D, 0x20)) {
27113db5b9f7SMasaki Ota 		goto error;
27123db5b9f7SMasaki Ota 	}
27133db5b9f7SMasaki Ota 	alps_exit_command_mode(psmouse);
27143db5b9f7SMasaki Ota 
27153db5b9f7SMasaki Ota 	return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
27163db5b9f7SMasaki Ota 
27173db5b9f7SMasaki Ota error:
27183db5b9f7SMasaki Ota 	alps_exit_command_mode(psmouse);
27193db5b9f7SMasaki Ota 	return ret;
27203db5b9f7SMasaki Ota }
27213db5b9f7SMasaki Ota 
alps_set_protocol(struct psmouse * psmouse,struct alps_data * priv,const struct alps_protocol_info * protocol)27223296f71cSDmitry Torokhov static int alps_set_protocol(struct psmouse *psmouse,
27233296f71cSDmitry Torokhov 			     struct alps_data *priv,
27243296f71cSDmitry Torokhov 			     const struct alps_protocol_info *protocol)
272525bded7cSSeth Forshee {
27263296f71cSDmitry Torokhov 	psmouse->private = priv;
27273296f71cSDmitry Torokhov 
272817a58edcSKees Cook 	timer_setup(&priv->timer, alps_flush_packet, 0);
27293296f71cSDmitry Torokhov 
27303296f71cSDmitry Torokhov 	priv->proto_version = protocol->version;
27313296f71cSDmitry Torokhov 	priv->byte0 = protocol->byte0;
27323296f71cSDmitry Torokhov 	priv->mask0 = protocol->mask0;
27333296f71cSDmitry Torokhov 	priv->flags = protocol->flags;
2734f673ceb1SKevin Cernekee 
27357a9f73e7SKevin Cernekee 	priv->x_max = 2000;
27367a9f73e7SKevin Cernekee 	priv->y_max = 1400;
27377a9f73e7SKevin Cernekee 	priv->x_bits = 15;
27387a9f73e7SKevin Cernekee 	priv->y_bits = 11;
27397a9f73e7SKevin Cernekee 
274099df65e7SKevin Cernekee 	switch (priv->proto_version) {
274125bded7cSSeth Forshee 	case ALPS_PROTO_V1:
274225bded7cSSeth Forshee 	case ALPS_PROTO_V2:
274324af5cb9SKevin Cernekee 		priv->hw_init = alps_hw_init_v1_v2;
274424af5cb9SKevin Cernekee 		priv->process_packet = alps_process_packet_v1_v2;
274524af5cb9SKevin Cernekee 		priv->set_abs_params = alps_set_abs_params_st;
274695f75e91SYunkang Tang 		priv->x_max = 1023;
274795f75e91SYunkang Tang 		priv->y_max = 767;
274819556219SHans de Goede 		if (dmi_check_system(alps_dmi_has_separate_stick_buttons))
274919556219SHans de Goede 			priv->flags |= ALPS_STICK_BITS;
275025bded7cSSeth Forshee 		break;
2751fb2dd7a6SDmitry Torokhov 
275225bded7cSSeth Forshee 	case ALPS_PROTO_V3:
275324af5cb9SKevin Cernekee 		priv->hw_init = alps_hw_init_v3;
275424af5cb9SKevin Cernekee 		priv->process_packet = alps_process_packet_v3;
2755688ea364SHans de Goede 		priv->set_abs_params = alps_set_abs_params_semi_mt;
2756f85e5001SKevin Cernekee 		priv->decode_fields = alps_decode_pinnacle;
275750e8b216SKevin Cernekee 		priv->nibble_commands = alps_v3_nibble_commands;
275850e8b216SKevin Cernekee 		priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
27594b1af853SPali Rohár 
27604b1af853SPali Rohár 		if (alps_probe_trackstick_v3_v7(psmouse,
27614b1af853SPali Rohár 						ALPS_REG_BASE_PINNACLE) < 0)
27624b1af853SPali Rohár 			priv->flags &= ~ALPS_DUALPOINT;
27634b1af853SPali Rohár 
276425bded7cSSeth Forshee 		break;
2765fb2dd7a6SDmitry Torokhov 
2766fb2dd7a6SDmitry Torokhov 	case ALPS_PROTO_V3_RUSHMORE:
2767fb2dd7a6SDmitry Torokhov 		priv->hw_init = alps_hw_init_rushmore_v3;
2768fb2dd7a6SDmitry Torokhov 		priv->process_packet = alps_process_packet_v3;
2769688ea364SHans de Goede 		priv->set_abs_params = alps_set_abs_params_semi_mt;
2770fb2dd7a6SDmitry Torokhov 		priv->decode_fields = alps_decode_rushmore;
2771fb2dd7a6SDmitry Torokhov 		priv->nibble_commands = alps_v3_nibble_commands;
2772fb2dd7a6SDmitry Torokhov 		priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2773fb2dd7a6SDmitry Torokhov 		priv->x_bits = 16;
2774fb2dd7a6SDmitry Torokhov 		priv->y_bits = 12;
27753296f71cSDmitry Torokhov 
2776dae928ecSPali Rohár 		if (alps_probe_trackstick_v3_v7(psmouse,
27773296f71cSDmitry Torokhov 						ALPS_REG_BASE_RUSHMORE) < 0)
27783296f71cSDmitry Torokhov 			priv->flags &= ~ALPS_DUALPOINT;
27793296f71cSDmitry Torokhov 
2780fb2dd7a6SDmitry Torokhov 		break;
2781fb2dd7a6SDmitry Torokhov 
278225bded7cSSeth Forshee 	case ALPS_PROTO_V4:
278324af5cb9SKevin Cernekee 		priv->hw_init = alps_hw_init_v4;
278424af5cb9SKevin Cernekee 		priv->process_packet = alps_process_packet_v4;
2785688ea364SHans de Goede 		priv->set_abs_params = alps_set_abs_params_semi_mt;
278650e8b216SKevin Cernekee 		priv->nibble_commands = alps_v4_nibble_commands;
278750e8b216SKevin Cernekee 		priv->addr_command = PSMOUSE_CMD_DISABLE;
278825bded7cSSeth Forshee 		break;
27893296f71cSDmitry Torokhov 
279075af9e56SDave Turvene 	case ALPS_PROTO_V5:
279175af9e56SDave Turvene 		priv->hw_init = alps_hw_init_dolphin_v1;
2792ee65d4b3SYunkang Tang 		priv->process_packet = alps_process_touchpad_packet_v3_v5;
279375af9e56SDave Turvene 		priv->decode_fields = alps_decode_dolphin;
2794688ea364SHans de Goede 		priv->set_abs_params = alps_set_abs_params_semi_mt;
279575af9e56SDave Turvene 		priv->nibble_commands = alps_v3_nibble_commands;
279675af9e56SDave Turvene 		priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
279775af9e56SDave Turvene 		priv->x_bits = 23;
279875af9e56SDave Turvene 		priv->y_bits = 12;
2799c164c147SDmitry Torokhov 
2800c164c147SDmitry Torokhov 		if (alps_dolphin_get_device_area(psmouse, priv))
2801c164c147SDmitry Torokhov 			return -EIO;
2802c164c147SDmitry Torokhov 
280375af9e56SDave Turvene 		break;
28043296f71cSDmitry Torokhov 
280595f75e91SYunkang Tang 	case ALPS_PROTO_V6:
280695f75e91SYunkang Tang 		priv->hw_init = alps_hw_init_v6;
280795f75e91SYunkang Tang 		priv->process_packet = alps_process_packet_v6;
280895f75e91SYunkang Tang 		priv->set_abs_params = alps_set_abs_params_st;
280995f75e91SYunkang Tang 		priv->nibble_commands = alps_v6_nibble_commands;
281095f75e91SYunkang Tang 		priv->x_max = 2047;
281195f75e91SYunkang Tang 		priv->y_max = 1535;
281295f75e91SYunkang Tang 		break;
28133296f71cSDmitry Torokhov 
28143808843cSYunkang Tang 	case ALPS_PROTO_V7:
28153808843cSYunkang Tang 		priv->hw_init = alps_hw_init_v7;
28163808843cSYunkang Tang 		priv->process_packet = alps_process_packet_v7;
28173808843cSYunkang Tang 		priv->decode_fields = alps_decode_packet_v7;
28188eccd393SMasaki Ota 		priv->set_abs_params = alps_set_abs_params_v7;
28193808843cSYunkang Tang 		priv->nibble_commands = alps_v3_nibble_commands;
28203808843cSYunkang Tang 		priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2821c164c147SDmitry Torokhov 		priv->x_max = 0xfff;
2822c164c147SDmitry Torokhov 		priv->y_max = 0x7ff;
28233808843cSYunkang Tang 
28243808843cSYunkang Tang 		if (priv->fw_ver[1] != 0xba)
28253808843cSYunkang Tang 			priv->flags |= ALPS_BUTTONPAD;
28263296f71cSDmitry Torokhov 
2827dae928ecSPali Rohár 		if (alps_probe_trackstick_v3_v7(psmouse, ALPS_REG_BASE_V7) < 0)
2828dae928ecSPali Rohár 			priv->flags &= ~ALPS_DUALPOINT;
2829dae928ecSPali Rohár 
28303808843cSYunkang Tang 		break;
28313db5b9f7SMasaki Ota 
28323db5b9f7SMasaki Ota 	case ALPS_PROTO_V8:
28333db5b9f7SMasaki Ota 		priv->hw_init = alps_hw_init_ss4_v2;
28343db5b9f7SMasaki Ota 		priv->process_packet = alps_process_packet_ss4_v2;
28353db5b9f7SMasaki Ota 		priv->decode_fields = alps_decode_ss4_v2;
28363db5b9f7SMasaki Ota 		priv->set_abs_params = alps_set_abs_params_ss4_v2;
28373db5b9f7SMasaki Ota 		priv->nibble_commands = alps_v3_nibble_commands;
28383db5b9f7SMasaki Ota 		priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
28393db5b9f7SMasaki Ota 
28403db5b9f7SMasaki Ota 		if (alps_set_defaults_ss4_v2(psmouse, priv))
28413db5b9f7SMasaki Ota 			return -EIO;
28423db5b9f7SMasaki Ota 
28433db5b9f7SMasaki Ota 		break;
284425bded7cSSeth Forshee 	}
28453296f71cSDmitry Torokhov 
28463296f71cSDmitry Torokhov 	return 0;
284725bded7cSSeth Forshee }
284825bded7cSSeth Forshee 
alps_match_table(unsigned char * e7,unsigned char * ec)28493296f71cSDmitry Torokhov static const struct alps_protocol_info *alps_match_table(unsigned char *e7,
28503296f71cSDmitry Torokhov 							 unsigned char *ec)
28512e992cc0SKevin Cernekee {
2852b5d6b851SKevin Cernekee 	const struct alps_model_info *model;
28532e992cc0SKevin Cernekee 	int i;
28542e992cc0SKevin Cernekee 
2855b5d6b851SKevin Cernekee 	for (i = 0; i < ARRAY_SIZE(alps_model_data); i++) {
2856b5d6b851SKevin Cernekee 		model = &alps_model_data[i];
2857b5d6b851SKevin Cernekee 
2858a3cbfd56SPali Rohár 		if (!memcmp(e7, model->signature, sizeof(model->signature)))
28593296f71cSDmitry Torokhov 			return &model->protocol_info;
2860b5d6b851SKevin Cernekee 	}
2861b5d6b851SKevin Cernekee 
28623296f71cSDmitry Torokhov 	return NULL;
2863b5d6b851SKevin Cernekee }
2864b5d6b851SKevin Cernekee 
alps_is_cs19_trackpoint(struct psmouse * psmouse)28657e4935ccSHui Wang static bool alps_is_cs19_trackpoint(struct psmouse *psmouse)
28667e4935ccSHui Wang {
28677e4935ccSHui Wang 	u8 param[2] = { 0 };
28687e4935ccSHui Wang 
28697e4935ccSHui Wang 	if (ps2_command(&psmouse->ps2dev,
28707e4935ccSHui Wang 			param, MAKE_PS2_CMD(0, 2, TP_READ_ID)))
28717e4935ccSHui Wang 		return false;
28727e4935ccSHui Wang 
28737e4935ccSHui Wang 	/*
28747e4935ccSHui Wang 	 * param[0] contains the trackpoint device variant_id while
28757e4935ccSHui Wang 	 * param[1] contains the firmware_id. So far all alps
28767e4935ccSHui Wang 	 * trackpoint-only devices have their variant_ids equal
28777e4935ccSHui Wang 	 * TP_VARIANT_ALPS and their firmware_ids are in 0x20~0x2f range.
28787e4935ccSHui Wang 	 */
2879771a081eSHui Wang 	return param[0] == TP_VARIANT_ALPS && ((param[1] & 0xf0) == 0x20);
28807e4935ccSHui Wang }
28817e4935ccSHui Wang 
alps_identify(struct psmouse * psmouse,struct alps_data * priv)2882b5d6b851SKevin Cernekee static int alps_identify(struct psmouse *psmouse, struct alps_data *priv)
2883b5d6b851SKevin Cernekee {
28843296f71cSDmitry Torokhov 	const struct alps_protocol_info *protocol;
2885b5d6b851SKevin Cernekee 	unsigned char e6[4], e7[4], ec[4];
2886a09221e8SDmitry Torokhov 	int error;
2887b5d6b851SKevin Cernekee 
28882e992cc0SKevin Cernekee 	/*
28892e992cc0SKevin Cernekee 	 * First try "E6 report".
28902e992cc0SKevin Cernekee 	 * ALPS should return 0,0,10 or 0,0,100 if no buttons are pressed.
28912e992cc0SKevin Cernekee 	 * The bits 0-2 of the first byte will be 1s if some buttons are
28922e992cc0SKevin Cernekee 	 * pressed.
28932e992cc0SKevin Cernekee 	 */
2894b5d6b851SKevin Cernekee 	if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2895b5d6b851SKevin Cernekee 			 PSMOUSE_CMD_SETSCALE11, e6))
2896b5d6b851SKevin Cernekee 		return -EIO;
28972e992cc0SKevin Cernekee 
2898b5d6b851SKevin Cernekee 	if ((e6[0] & 0xf8) != 0 || e6[1] != 0 || (e6[2] != 10 && e6[2] != 100))
2899b5d6b851SKevin Cernekee 		return -EINVAL;
29002e992cc0SKevin Cernekee 
29012e992cc0SKevin Cernekee 	/*
2902b5d6b851SKevin Cernekee 	 * Now get the "E7" and "EC" reports.  These will uniquely identify
2903b5d6b851SKevin Cernekee 	 * most ALPS touchpads.
29042e992cc0SKevin Cernekee 	 */
2905b5d6b851SKevin Cernekee 	if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2906b5d6b851SKevin Cernekee 			 PSMOUSE_CMD_SETSCALE21, e7) ||
2907b5d6b851SKevin Cernekee 	    alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2908b5d6b851SKevin Cernekee 			 PSMOUSE_CMD_RESET_WRAP, ec) ||
2909b5d6b851SKevin Cernekee 	    alps_exit_command_mode(psmouse))
2910b5d6b851SKevin Cernekee 		return -EIO;
29112e992cc0SKevin Cernekee 
29123296f71cSDmitry Torokhov 	protocol = alps_match_table(e7, ec);
29133296f71cSDmitry Torokhov 	if (!protocol) {
291409c398bcSPali Rohár 		if (e7[0] == 0x73 && e7[1] == 0x02 && e7[2] == 0x64 &&
291509c398bcSPali Rohár 			   ec[2] == 0x8a) {
291609c398bcSPali Rohár 			protocol = &alps_v4_protocol_data;
291709c398bcSPali Rohár 		} else if (e7[0] == 0x73 && e7[1] == 0x03 && e7[2] == 0x50 &&
29183296f71cSDmitry Torokhov 			   ec[0] == 0x73 && (ec[1] == 0x01 || ec[1] == 0x02)) {
29193296f71cSDmitry Torokhov 			protocol = &alps_v5_protocol_data;
29203296f71cSDmitry Torokhov 		} else if (ec[0] == 0x88 &&
29213296f71cSDmitry Torokhov 			   ((ec[1] & 0xf0) == 0xb0 || (ec[1] & 0xf0) == 0xc0)) {
29223296f71cSDmitry Torokhov 			protocol = &alps_v7_protocol_data;
29233296f71cSDmitry Torokhov 		} else if (ec[0] == 0x88 && ec[1] == 0x08) {
29243296f71cSDmitry Torokhov 			protocol = &alps_v3_rushmore_data;
29253296f71cSDmitry Torokhov 		} else if (ec[0] == 0x88 && ec[1] == 0x07 &&
29263296f71cSDmitry Torokhov 			   ec[2] >= 0x90 && ec[2] <= 0x9d) {
29273296f71cSDmitry Torokhov 			protocol = &alps_v3_protocol_data;
29283db5b9f7SMasaki Ota 		} else if (e7[0] == 0x73 && e7[1] == 0x03 &&
2929e7348396SMasaki Ota 			   (e7[2] == 0x14 || e7[2] == 0x28)) {
2930aeaa881fSBen Gamari 			protocol = &alps_v8_protocol_data;
2931c9815232SPali Rohár 		} else if (e7[0] == 0x73 && e7[1] == 0x03 && e7[2] == 0xc8) {
2932c9815232SPali Rohár 			protocol = &alps_v9_protocol_data;
2933c9815232SPali Rohár 			psmouse_warn(psmouse,
2934c9815232SPali Rohár 				     "Unsupported ALPS V9 touchpad: E7=%3ph, EC=%3ph\n",
2935c9815232SPali Rohár 				     e7, ec);
2936c9815232SPali Rohár 			return -EINVAL;
29373296f71cSDmitry Torokhov 		} else {
29383296f71cSDmitry Torokhov 			psmouse_dbg(psmouse,
29393296f71cSDmitry Torokhov 				    "Likely not an ALPS touchpad: E7=%3ph, EC=%3ph\n", e7, ec);
29403296f71cSDmitry Torokhov 			return -EINVAL;
29413296f71cSDmitry Torokhov 		}
29423296f71cSDmitry Torokhov 	}
29433296f71cSDmitry Torokhov 
2944a09221e8SDmitry Torokhov 	if (priv) {
2945e7348396SMasaki Ota 		/* Save Device ID and Firmware version */
2946e7348396SMasaki Ota 		memcpy(priv->dev_id, e7, 3);
2947c0cd17f6SHans de Goede 		memcpy(priv->fw_ver, ec, 3);
2948a09221e8SDmitry Torokhov 		error = alps_set_protocol(psmouse, priv, protocol);
2949a09221e8SDmitry Torokhov 		if (error)
2950a09221e8SDmitry Torokhov 			return error;
2951a09221e8SDmitry Torokhov 	}
2952c0cd17f6SHans de Goede 
2953a09221e8SDmitry Torokhov 	return 0;
29542e992cc0SKevin Cernekee }
29552e992cc0SKevin Cernekee 
alps_reconnect(struct psmouse * psmouse)29561e0c5b12SDmitry Torokhov static int alps_reconnect(struct psmouse *psmouse)
29571e0c5b12SDmitry Torokhov {
2958b5d6b851SKevin Cernekee 	struct alps_data *priv = psmouse->private;
295971bb21b6SMaxim Levitsky 
29601e0c5b12SDmitry Torokhov 	psmouse_reset(psmouse);
29611e0c5b12SDmitry Torokhov 
2962b5d6b851SKevin Cernekee 	if (alps_identify(psmouse, priv) < 0)
29631e0c5b12SDmitry Torokhov 		return -1;
29641e0c5b12SDmitry Torokhov 
296524af5cb9SKevin Cernekee 	return priv->hw_init(psmouse);
29661da177e4SLinus Torvalds }
29671da177e4SLinus Torvalds 
alps_disconnect(struct psmouse * psmouse)29681da177e4SLinus Torvalds static void alps_disconnect(struct psmouse *psmouse)
29691da177e4SLinus Torvalds {
29701da177e4SLinus Torvalds 	struct alps_data *priv = psmouse->private;
29712e5b636bSDmitry Torokhov 
29721da177e4SLinus Torvalds 	psmouse_reset(psmouse);
2973292a089dSSteven Rostedt (Google) 	timer_shutdown_sync(&priv->timer);
297404aae283SPali Rohár 	if (priv->dev2)
29752e5b636bSDmitry Torokhov 		input_unregister_device(priv->dev2);
297604aae283SPali Rohár 	if (!IS_ERR_OR_NULL(priv->dev3))
297704aae283SPali Rohár 		input_unregister_device(priv->dev3);
29781da177e4SLinus Torvalds 	kfree(priv);
29791da177e4SLinus Torvalds }
29801da177e4SLinus Torvalds 
alps_set_abs_params_st(struct alps_data * priv,struct input_dev * dev1)298124af5cb9SKevin Cernekee static void alps_set_abs_params_st(struct alps_data *priv,
298224af5cb9SKevin Cernekee 				   struct input_dev *dev1)
298324af5cb9SKevin Cernekee {
298495f75e91SYunkang Tang 	input_set_abs_params(dev1, ABS_X, 0, priv->x_max, 0, 0);
298595f75e91SYunkang Tang 	input_set_abs_params(dev1, ABS_Y, 0, priv->y_max, 0, 0);
29868eccd393SMasaki Ota 	input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
298724af5cb9SKevin Cernekee }
298824af5cb9SKevin Cernekee 
alps_set_abs_params_mt_common(struct alps_data * priv,struct input_dev * dev1)29898eccd393SMasaki Ota static void alps_set_abs_params_mt_common(struct alps_data *priv,
299024af5cb9SKevin Cernekee 					  struct input_dev *dev1)
299124af5cb9SKevin Cernekee {
29927a9f73e7SKevin Cernekee 	input_set_abs_params(dev1, ABS_MT_POSITION_X, 0, priv->x_max, 0, 0);
29937a9f73e7SKevin Cernekee 	input_set_abs_params(dev1, ABS_MT_POSITION_Y, 0, priv->y_max, 0, 0);
299424af5cb9SKevin Cernekee 
2995f3f33c67SHans de Goede 	input_abs_set_res(dev1, ABS_MT_POSITION_X, priv->x_res);
2996f3f33c67SHans de Goede 	input_abs_set_res(dev1, ABS_MT_POSITION_Y, priv->y_res);
2997f3f33c67SHans de Goede 
299824af5cb9SKevin Cernekee 	set_bit(BTN_TOOL_TRIPLETAP, dev1->keybit);
299924af5cb9SKevin Cernekee 	set_bit(BTN_TOOL_QUADTAP, dev1->keybit);
30008eccd393SMasaki Ota }
30013808843cSYunkang Tang 
alps_set_abs_params_semi_mt(struct alps_data * priv,struct input_dev * dev1)3002688ea364SHans de Goede static void alps_set_abs_params_semi_mt(struct alps_data *priv,
30038eccd393SMasaki Ota 					struct input_dev *dev1)
30048eccd393SMasaki Ota {
30058eccd393SMasaki Ota 	alps_set_abs_params_mt_common(priv, dev1);
30068eccd393SMasaki Ota 	input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
30078eccd393SMasaki Ota 
30088eccd393SMasaki Ota 	input_mt_init_slots(dev1, MAX_TOUCHES,
30098eccd393SMasaki Ota 			    INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
30101662c033SHans de Goede 				INPUT_MT_SEMI_MT);
30118eccd393SMasaki Ota }
30128eccd393SMasaki Ota 
alps_set_abs_params_v7(struct alps_data * priv,struct input_dev * dev1)30138eccd393SMasaki Ota static void alps_set_abs_params_v7(struct alps_data *priv,
30148eccd393SMasaki Ota 				   struct input_dev *dev1)
30158eccd393SMasaki Ota {
30168eccd393SMasaki Ota 	alps_set_abs_params_mt_common(priv, dev1);
30178d289842SMasaki Ota 	set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
30188eccd393SMasaki Ota 
30198eccd393SMasaki Ota 	input_mt_init_slots(dev1, MAX_TOUCHES,
30208eccd393SMasaki Ota 			    INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
30218eccd393SMasaki Ota 				INPUT_MT_TRACK);
30223db5b9f7SMasaki Ota 
30233db5b9f7SMasaki Ota 	set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
30243db5b9f7SMasaki Ota }
30253db5b9f7SMasaki Ota 
alps_set_abs_params_ss4_v2(struct alps_data * priv,struct input_dev * dev1)30263db5b9f7SMasaki Ota static void alps_set_abs_params_ss4_v2(struct alps_data *priv,
30273db5b9f7SMasaki Ota 				       struct input_dev *dev1)
30283db5b9f7SMasaki Ota {
30293db5b9f7SMasaki Ota 	alps_set_abs_params_mt_common(priv, dev1);
30303db5b9f7SMasaki Ota 	input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
30313db5b9f7SMasaki Ota 	set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
30323db5b9f7SMasaki Ota 
30333db5b9f7SMasaki Ota 	input_mt_init_slots(dev1, MAX_TOUCHES,
30343db5b9f7SMasaki Ota 			    INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
30353db5b9f7SMasaki Ota 				INPUT_MT_TRACK);
303624af5cb9SKevin Cernekee }
303724af5cb9SKevin Cernekee 
alps_init(struct psmouse * psmouse)30381da177e4SLinus Torvalds int alps_init(struct psmouse *psmouse)
30391da177e4SLinus Torvalds {
3040a09221e8SDmitry Torokhov 	struct alps_data *priv = psmouse->private;
304104aae283SPali Rohár 	struct input_dev *dev1 = psmouse->dev;
3042a09221e8SDmitry Torokhov 	int error;
30431da177e4SLinus Torvalds 
3044a09221e8SDmitry Torokhov 	error = priv->hw_init(psmouse);
3045a09221e8SDmitry Torokhov 	if (error)
30461da177e4SLinus Torvalds 		goto init_fail;
30471da177e4SLinus Torvalds 
30487105d2eaSDmitry Torokhov 	/*
30497105d2eaSDmitry Torokhov 	 * Undo part of setup done for us by psmouse core since touchpad
30507105d2eaSDmitry Torokhov 	 * is not a relative device.
30517105d2eaSDmitry Torokhov 	 */
30527105d2eaSDmitry Torokhov 	__clear_bit(EV_REL, dev1->evbit);
30537105d2eaSDmitry Torokhov 	__clear_bit(REL_X, dev1->relbit);
30547105d2eaSDmitry Torokhov 	__clear_bit(REL_Y, dev1->relbit);
30557105d2eaSDmitry Torokhov 
30567105d2eaSDmitry Torokhov 	/*
30577105d2eaSDmitry Torokhov 	 * Now set up our capabilities.
30587105d2eaSDmitry Torokhov 	 */
30597b19ada2SJiri Slaby 	dev1->evbit[BIT_WORD(EV_KEY)] |= BIT_MASK(EV_KEY);
30607b19ada2SJiri Slaby 	dev1->keybit[BIT_WORD(BTN_TOUCH)] |= BIT_MASK(BTN_TOUCH);
30617b19ada2SJiri Slaby 	dev1->keybit[BIT_WORD(BTN_TOOL_FINGER)] |= BIT_MASK(BTN_TOOL_FINGER);
306271bb21b6SMaxim Levitsky 	dev1->keybit[BIT_WORD(BTN_LEFT)] |=
306371bb21b6SMaxim Levitsky 		BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
30641da177e4SLinus Torvalds 
30657b19ada2SJiri Slaby 	dev1->evbit[BIT_WORD(EV_ABS)] |= BIT_MASK(EV_ABS);
306625bded7cSSeth Forshee 
306724af5cb9SKevin Cernekee 	priv->set_abs_params(priv, dev1);
30681da177e4SLinus Torvalds 
306999df65e7SKevin Cernekee 	if (priv->flags & ALPS_WHEEL) {
30707b19ada2SJiri Slaby 		dev1->evbit[BIT_WORD(EV_REL)] |= BIT_MASK(EV_REL);
30717b19ada2SJiri Slaby 		dev1->relbit[BIT_WORD(REL_WHEEL)] |= BIT_MASK(REL_WHEEL);
30721da177e4SLinus Torvalds 	}
30731da177e4SLinus Torvalds 
307499df65e7SKevin Cernekee 	if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
30757b19ada2SJiri Slaby 		dev1->keybit[BIT_WORD(BTN_FORWARD)] |= BIT_MASK(BTN_FORWARD);
30767b19ada2SJiri Slaby 		dev1->keybit[BIT_WORD(BTN_BACK)] |= BIT_MASK(BTN_BACK);
30771da177e4SLinus Torvalds 	}
30781da177e4SLinus Torvalds 
307999df65e7SKevin Cernekee 	if (priv->flags & ALPS_FOUR_BUTTONS) {
308071bb21b6SMaxim Levitsky 		dev1->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_0);
308171bb21b6SMaxim Levitsky 		dev1->keybit[BIT_WORD(BTN_1)] |= BIT_MASK(BTN_1);
308271bb21b6SMaxim Levitsky 		dev1->keybit[BIT_WORD(BTN_2)] |= BIT_MASK(BTN_2);
308371bb21b6SMaxim Levitsky 		dev1->keybit[BIT_WORD(BTN_3)] |= BIT_MASK(BTN_3);
30843808843cSYunkang Tang 	} else if (priv->flags & ALPS_BUTTONPAD) {
30853808843cSYunkang Tang 		set_bit(INPUT_PROP_BUTTONPAD, dev1->propbit);
30863808843cSYunkang Tang 		clear_bit(BTN_RIGHT, dev1->keybit);
308771bb21b6SMaxim Levitsky 	} else {
308871bb21b6SMaxim Levitsky 		dev1->keybit[BIT_WORD(BTN_MIDDLE)] |= BIT_MASK(BTN_MIDDLE);
308971bb21b6SMaxim Levitsky 	}
309071bb21b6SMaxim Levitsky 
3091dfba8600SPali Rohár 	if (priv->flags & ALPS_DUALPOINT) {
309204aae283SPali Rohár 		struct input_dev *dev2;
309304aae283SPali Rohár 
309404aae283SPali Rohár 		dev2 = input_allocate_device();
309504aae283SPali Rohár 		if (!dev2) {
309604aae283SPali Rohár 			psmouse_err(psmouse,
309704aae283SPali Rohár 				    "failed to allocate trackstick device\n");
309804aae283SPali Rohár 			error = -ENOMEM;
309904aae283SPali Rohár 			goto init_fail;
310004aae283SPali Rohár 		}
310104aae283SPali Rohár 
310204aae283SPali Rohár 		snprintf(priv->phys2, sizeof(priv->phys2), "%s/input1",
310304aae283SPali Rohár 			 psmouse->ps2dev.serio->phys);
310404aae283SPali Rohár 		dev2->phys = priv->phys2;
310504aae283SPali Rohár 
3106dfba8600SPali Rohár 		/*
3107dfba8600SPali Rohár 		 * format of input device name is: "protocol vendor name"
3108dfba8600SPali Rohár 		 * see function psmouse_switch_protocol() in psmouse-base.c
3109dfba8600SPali Rohár 		 */
3110dfba8600SPali Rohár 		dev2->name = "AlpsPS/2 ALPS DualPoint Stick";
3111dfba8600SPali Rohár 
31122e5b636bSDmitry Torokhov 		dev2->id.bustype = BUS_I8042;
31132e5b636bSDmitry Torokhov 		dev2->id.vendor  = 0x0002;
311404aae283SPali Rohár 		dev2->id.product = PSMOUSE_ALPS;
311504aae283SPali Rohár 		dev2->id.version = priv->proto_version;
31161db3a345SDmitry Torokhov 		dev2->dev.parent = &psmouse->ps2dev.serio->dev;
31171da177e4SLinus Torvalds 
311804aae283SPali Rohár 		input_set_capability(dev2, EV_REL, REL_X);
311904aae283SPali Rohár 		input_set_capability(dev2, EV_REL, REL_Y);
31207ad8a106SBen Gamari 		if (priv->flags & ALPS_DUALPOINT_WITH_PRESSURE) {
31217ad8a106SBen Gamari 			input_set_capability(dev2, EV_ABS, ABS_PRESSURE);
31227ad8a106SBen Gamari 			input_set_abs_params(dev2, ABS_PRESSURE, 0, 127, 0, 0);
31237ad8a106SBen Gamari 		}
312404aae283SPali Rohár 		input_set_capability(dev2, EV_KEY, BTN_LEFT);
312504aae283SPali Rohár 		input_set_capability(dev2, EV_KEY, BTN_RIGHT);
312604aae283SPali Rohár 		input_set_capability(dev2, EV_KEY, BTN_MIDDLE);
31271da177e4SLinus Torvalds 
312801d4cd5cSHans de Goede 		__set_bit(INPUT_PROP_POINTER, dev2->propbit);
31297611392fSHans de Goede 		__set_bit(INPUT_PROP_POINTING_STICK, dev2->propbit);
31307611392fSHans de Goede 
313104aae283SPali Rohár 		error = input_register_device(dev2);
313204aae283SPali Rohár 		if (error) {
313304aae283SPali Rohár 			psmouse_err(psmouse,
313404aae283SPali Rohár 				    "failed to register trackstick device: %d\n",
313504aae283SPali Rohár 				    error);
313604aae283SPali Rohár 			input_free_device(dev2);
3137f42649e8SDmitry Torokhov 			goto init_fail;
313804aae283SPali Rohár 		}
313904aae283SPali Rohár 
314004aae283SPali Rohár 		priv->dev2 = dev2;
314104aae283SPali Rohár 	}
314204aae283SPali Rohár 
314304aae283SPali Rohár 	priv->psmouse = psmouse;
314404aae283SPali Rohár 
314504aae283SPali Rohár 	INIT_DELAYED_WORK(&priv->dev3_register_work,
314604aae283SPali Rohár 			  alps_register_bare_ps2_mouse);
31471da177e4SLinus Torvalds 
31481da177e4SLinus Torvalds 	psmouse->protocol_handler = alps_process_byte;
3149f0d5c6f4SDmitry Torokhov 	psmouse->poll = alps_poll;
31501da177e4SLinus Torvalds 	psmouse->disconnect = alps_disconnect;
31511da177e4SLinus Torvalds 	psmouse->reconnect = alps_reconnect;
315299df65e7SKevin Cernekee 	psmouse->pktsize = priv->proto_version == ALPS_PROTO_V4 ? 8 : 6;
31531da177e4SLinus Torvalds 
3154f0d5c6f4SDmitry Torokhov 	/* We are having trouble resyncing ALPS touchpads so disable it for now */
3155f0d5c6f4SDmitry Torokhov 	psmouse->resync_time = 0;
3156f0d5c6f4SDmitry Torokhov 
31579d720b34SPali Rohár 	/* Allow 2 invalid packets without resetting device */
31589d720b34SPali Rohár 	psmouse->resetafter = psmouse->pktsize * 2;
31599d720b34SPali Rohár 
31601da177e4SLinus Torvalds 	return 0;
31611da177e4SLinus Torvalds 
31621da177e4SLinus Torvalds init_fail:
3163f42649e8SDmitry Torokhov 	psmouse_reset(psmouse);
3164a09221e8SDmitry Torokhov 	/*
3165a09221e8SDmitry Torokhov 	 * Even though we did not allocate psmouse->private we do free
3166a09221e8SDmitry Torokhov 	 * it here.
3167a09221e8SDmitry Torokhov 	 */
3168a09221e8SDmitry Torokhov 	kfree(psmouse->private);
31691e0c5b12SDmitry Torokhov 	psmouse->private = NULL;
3170a09221e8SDmitry Torokhov 	return error;
31711da177e4SLinus Torvalds }
31721da177e4SLinus Torvalds 
alps_detect(struct psmouse * psmouse,bool set_properties)3173b7802c5cSDmitry Torokhov int alps_detect(struct psmouse *psmouse, bool set_properties)
31741da177e4SLinus Torvalds {
3175a09221e8SDmitry Torokhov 	struct alps_data *priv;
3176a09221e8SDmitry Torokhov 	int error;
31771da177e4SLinus Torvalds 
3178a09221e8SDmitry Torokhov 	error = alps_identify(psmouse, NULL);
3179a09221e8SDmitry Torokhov 	if (error)
3180a09221e8SDmitry Torokhov 		return error;
3181a09221e8SDmitry Torokhov 
3182a09221e8SDmitry Torokhov 	/*
31837e4935ccSHui Wang 	 * ALPS cs19 is a trackpoint-only device, and uses different
31847e4935ccSHui Wang 	 * protocol than DualPoint ones, so we return -EINVAL here and let
31857e4935ccSHui Wang 	 * trackpoint.c drive this device. If the trackpoint driver is not
31867e4935ccSHui Wang 	 * enabled, the device will fall back to a bare PS/2 mouse.
31877e4935ccSHui Wang 	 * If ps2_command() fails here, we depend on the immediately
31887e4935ccSHui Wang 	 * followed psmouse_reset() to reset the device to normal state.
31897e4935ccSHui Wang 	 */
31907e4935ccSHui Wang 	if (alps_is_cs19_trackpoint(psmouse)) {
31917e4935ccSHui Wang 		psmouse_dbg(psmouse,
31927e4935ccSHui Wang 			    "ALPS CS19 trackpoint-only device detected, ignoring\n");
31937e4935ccSHui Wang 		return -EINVAL;
31947e4935ccSHui Wang 	}
31957e4935ccSHui Wang 
31967e4935ccSHui Wang 	/*
3197a09221e8SDmitry Torokhov 	 * Reset the device to make sure it is fully operational:
3198a09221e8SDmitry Torokhov 	 * on some laptops, like certain Dell Latitudes, we may
3199a09221e8SDmitry Torokhov 	 * fail to properly detect presence of trackstick if device
3200a09221e8SDmitry Torokhov 	 * has not been reset.
3201a09221e8SDmitry Torokhov 	 */
3202a09221e8SDmitry Torokhov 	psmouse_reset(psmouse);
3203a09221e8SDmitry Torokhov 
3204a09221e8SDmitry Torokhov 	priv = kzalloc(sizeof(struct alps_data), GFP_KERNEL);
3205a09221e8SDmitry Torokhov 	if (!priv)
3206a09221e8SDmitry Torokhov 		return -ENOMEM;
3207a09221e8SDmitry Torokhov 
3208a09221e8SDmitry Torokhov 	error = alps_identify(psmouse, priv);
320993050db2SDmitry Torokhov 	if (error) {
321093050db2SDmitry Torokhov 		kfree(priv);
3211a09221e8SDmitry Torokhov 		return error;
321293050db2SDmitry Torokhov 	}
32131da177e4SLinus Torvalds 
32141da177e4SLinus Torvalds 	if (set_properties) {
32151da177e4SLinus Torvalds 		psmouse->vendor = "ALPS";
3216a09221e8SDmitry Torokhov 		psmouse->name = priv->flags & ALPS_DUALPOINT ?
3217968ac842SDmitry Torokhov 				"DualPoint TouchPad" : "GlidePoint";
3218a09221e8SDmitry Torokhov 		psmouse->model = priv->proto_version;
3219a09221e8SDmitry Torokhov 	} else {
3220a09221e8SDmitry Torokhov 		/*
3221a09221e8SDmitry Torokhov 		 * Destroy alps_data structure we allocated earlier since
3222a09221e8SDmitry Torokhov 		 * this was just a "trial run". Otherwise we'll keep it
3223a09221e8SDmitry Torokhov 		 * to be used by alps_init() which has to be called if
3224a09221e8SDmitry Torokhov 		 * we succeed and set_properties is true.
3225a09221e8SDmitry Torokhov 		 */
3226a09221e8SDmitry Torokhov 		kfree(priv);
3227a09221e8SDmitry Torokhov 		psmouse->private = NULL;
32281da177e4SLinus Torvalds 	}
3229a09221e8SDmitry Torokhov 
32301da177e4SLinus Torvalds 	return 0;
32311da177e4SLinus Torvalds }
32321da177e4SLinus Torvalds 
3233