xref: /openbmc/linux/drivers/input/mouse/alps.c (revision 7229c58c)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * ALPS touchpad PS/2 mouse driver
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Copyright (c) 2003 Neil Brown <neilb@cse.unsw.edu.au>
5963f626dSPeter Osterlund  * Copyright (c) 2003-2005 Peter Osterlund <petero2@telia.com>
61da177e4SLinus Torvalds  * Copyright (c) 2004 Dmitry Torokhov <dtor@mail.ru>
71da177e4SLinus Torvalds  * Copyright (c) 2005 Vojtech Pavlik <vojtech@suse.cz>
81d9f2626SSebastian Kapfer  * Copyright (c) 2009 Sebastian Kapfer <sebastian_kapfer@gmx.net>
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  * ALPS detection, tap switching and status querying info is taken from
111da177e4SLinus Torvalds  * tpconfig utility (by C. Scott Ananian and Bruce Kall).
121da177e4SLinus Torvalds  *
131da177e4SLinus Torvalds  * This program is free software; you can redistribute it and/or modify it
141da177e4SLinus Torvalds  * under the terms of the GNU General Public License version 2 as published by
151da177e4SLinus Torvalds  * the Free Software Foundation.
161da177e4SLinus Torvalds  */
171da177e4SLinus Torvalds 
185a0e3ad6STejun Heo #include <linux/slab.h>
191da177e4SLinus Torvalds #include <linux/input.h>
2001ce661fSSeth Forshee #include <linux/input/mt.h>
211da177e4SLinus Torvalds #include <linux/serio.h>
221da177e4SLinus Torvalds #include <linux/libps2.h>
23073e570dSHans de Goede #include <linux/dmi.h>
241da177e4SLinus Torvalds 
251da177e4SLinus Torvalds #include "psmouse.h"
261da177e4SLinus Torvalds #include "alps.h"
271da177e4SLinus Torvalds 
2825bded7cSSeth Forshee /*
2925bded7cSSeth Forshee  * Definitions for ALPS version 3 and 4 command mode protocol
3025bded7cSSeth Forshee  */
3125bded7cSSeth Forshee #define ALPS_CMD_NIBBLE_10	0x01f2
3225bded7cSSeth Forshee 
33cd401204SKevin Cernekee #define ALPS_REG_BASE_RUSHMORE	0xc2c0
34dae928ecSPali Rohár #define ALPS_REG_BASE_V7	0xc2c0
35cd401204SKevin Cernekee #define ALPS_REG_BASE_PINNACLE	0x0000
36cd401204SKevin Cernekee 
3725bded7cSSeth Forshee static const struct alps_nibble_commands alps_v3_nibble_commands[] = {
3825bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETPOLL,		0x00 }, /* 0 */
3925bded7cSSeth Forshee 	{ PSMOUSE_CMD_RESET_DIS,	0x00 }, /* 1 */
4025bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETSCALE21,	0x00 }, /* 2 */
4125bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0x0a }, /* 3 */
4225bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0x14 }, /* 4 */
4325bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0x28 }, /* 5 */
4425bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0x3c }, /* 6 */
4525bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0x50 }, /* 7 */
4625bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0x64 }, /* 8 */
4725bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0xc8 }, /* 9 */
4825bded7cSSeth Forshee 	{ ALPS_CMD_NIBBLE_10,		0x00 }, /* a */
4925bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRES,		0x00 }, /* b */
5025bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRES,		0x01 }, /* c */
5125bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRES,		0x02 }, /* d */
5225bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRES,		0x03 }, /* e */
5325bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETSCALE11,	0x00 }, /* f */
5425bded7cSSeth Forshee };
5525bded7cSSeth Forshee 
5625bded7cSSeth Forshee static const struct alps_nibble_commands alps_v4_nibble_commands[] = {
5725bded7cSSeth Forshee 	{ PSMOUSE_CMD_ENABLE,		0x00 }, /* 0 */
5825bded7cSSeth Forshee 	{ PSMOUSE_CMD_RESET_DIS,	0x00 }, /* 1 */
5925bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETSCALE21,	0x00 }, /* 2 */
6025bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0x0a }, /* 3 */
6125bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0x14 }, /* 4 */
6225bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0x28 }, /* 5 */
6325bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0x3c }, /* 6 */
6425bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0x50 }, /* 7 */
6525bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0x64 }, /* 8 */
6625bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRATE,		0xc8 }, /* 9 */
6725bded7cSSeth Forshee 	{ ALPS_CMD_NIBBLE_10,		0x00 }, /* a */
6825bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRES,		0x00 }, /* b */
6925bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRES,		0x01 }, /* c */
7025bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRES,		0x02 }, /* d */
7125bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETRES,		0x03 }, /* e */
7225bded7cSSeth Forshee 	{ PSMOUSE_CMD_SETSCALE11,	0x00 }, /* f */
7325bded7cSSeth Forshee };
7425bded7cSSeth Forshee 
7595f75e91SYunkang Tang static const struct alps_nibble_commands alps_v6_nibble_commands[] = {
7695f75e91SYunkang Tang 	{ PSMOUSE_CMD_ENABLE,		0x00 }, /* 0 */
7795f75e91SYunkang Tang 	{ PSMOUSE_CMD_SETRATE,		0x0a }, /* 1 */
7895f75e91SYunkang Tang 	{ PSMOUSE_CMD_SETRATE,		0x14 }, /* 2 */
7995f75e91SYunkang Tang 	{ PSMOUSE_CMD_SETRATE,		0x28 }, /* 3 */
8095f75e91SYunkang Tang 	{ PSMOUSE_CMD_SETRATE,		0x3c }, /* 4 */
8195f75e91SYunkang Tang 	{ PSMOUSE_CMD_SETRATE,		0x50 }, /* 5 */
8295f75e91SYunkang Tang 	{ PSMOUSE_CMD_SETRATE,		0x64 }, /* 6 */
8395f75e91SYunkang Tang 	{ PSMOUSE_CMD_SETRATE,		0xc8 }, /* 7 */
8495f75e91SYunkang Tang 	{ PSMOUSE_CMD_GETID,		0x00 }, /* 8 */
8595f75e91SYunkang Tang 	{ PSMOUSE_CMD_GETINFO,		0x00 }, /* 9 */
8695f75e91SYunkang Tang 	{ PSMOUSE_CMD_SETRES,		0x00 }, /* a */
8795f75e91SYunkang Tang 	{ PSMOUSE_CMD_SETRES,		0x01 }, /* b */
8895f75e91SYunkang Tang 	{ PSMOUSE_CMD_SETRES,		0x02 }, /* c */
8995f75e91SYunkang Tang 	{ PSMOUSE_CMD_SETRES,		0x03 }, /* d */
9095f75e91SYunkang Tang 	{ PSMOUSE_CMD_SETSCALE21,	0x00 }, /* e */
9195f75e91SYunkang Tang 	{ PSMOUSE_CMD_SETSCALE11,	0x00 }, /* f */
9295f75e91SYunkang Tang };
9395f75e91SYunkang Tang 
9425bded7cSSeth Forshee 
9571bb21b6SMaxim Levitsky #define ALPS_DUALPOINT		0x02	/* touchpad has trackstick */
9671bb21b6SMaxim Levitsky #define ALPS_PASS		0x04	/* device has a pass-through port */
9771bb21b6SMaxim Levitsky 
9871bb21b6SMaxim Levitsky #define ALPS_WHEEL		0x08	/* hardware wheel present */
9971bb21b6SMaxim Levitsky #define ALPS_FW_BK_1		0x10	/* front & back buttons present */
10071bb21b6SMaxim Levitsky #define ALPS_FW_BK_2		0x20	/* front & back buttons present */
10171bb21b6SMaxim Levitsky #define ALPS_FOUR_BUTTONS	0x40	/* 4 direction button present */
1021d9f2626SSebastian Kapfer #define ALPS_PS2_INTERLEAVED	0x80	/* 3-byte PS/2 packet interleaved with
1031d9f2626SSebastian Kapfer 					   6-byte ALPS packet */
10419556219SHans de Goede #define ALPS_STICK_BITS		0x100	/* separate stick button bits */
1053808843cSYunkang Tang #define ALPS_BUTTONPAD		0x200	/* device is a clickpad */
1067ad8a106SBen Gamari #define ALPS_DUALPOINT_WITH_PRESSURE	0x400	/* device can report trackpoint pressure */
1071da177e4SLinus Torvalds 
108e38de678SHelge Deller static const struct alps_model_info alps_model_data[] = {
1098326bb57SDmitry Torokhov 	{ { 0x32, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } },	/* Toshiba Salellite Pro M10 */
1108326bb57SDmitry Torokhov 	{ { 0x33, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V1, 0x88, 0xf8, 0 } },				/* UMAX-530T */
1118326bb57SDmitry Torokhov 	{ { 0x53, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
1128326bb57SDmitry Torokhov 	{ { 0x53, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
1138326bb57SDmitry Torokhov 	{ { 0x60, 0x03, 0xc8 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },				/* HP ze1115 */
1148326bb57SDmitry Torokhov 	{ { 0x63, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
1158326bb57SDmitry Torokhov 	{ { 0x63, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
1168326bb57SDmitry Torokhov 	{ { 0x63, 0x02, 0x28 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 } },		/* Fujitsu Siemens S6010 */
1178326bb57SDmitry Torokhov 	{ { 0x63, 0x02, 0x3c }, 0x00, { ALPS_PROTO_V2, 0x8f, 0x8f, ALPS_WHEEL } },		/* Toshiba Satellite S2400-103 */
1188326bb57SDmitry Torokhov 	{ { 0x63, 0x02, 0x50 }, 0x00, { ALPS_PROTO_V2, 0xef, 0xef, ALPS_FW_BK_1 } },		/* NEC Versa L320 */
1198326bb57SDmitry Torokhov 	{ { 0x63, 0x02, 0x64 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
1208326bb57SDmitry Torokhov 	{ { 0x63, 0x03, 0xc8 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } },	/* Dell Latitude D800 */
1218326bb57SDmitry Torokhov 	{ { 0x73, 0x00, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_DUALPOINT } },		/* ThinkPad R61 8918-5QG */
1228326bb57SDmitry Torokhov 	{ { 0x73, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
1238326bb57SDmitry Torokhov 	{ { 0x73, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 } },		/* Ahtec Laptop */
124626b9da0SDmitry Torokhov 
125626b9da0SDmitry Torokhov 	/*
126626b9da0SDmitry Torokhov 	 * XXX This entry is suspicious. First byte has zero lower nibble,
127626b9da0SDmitry Torokhov 	 * which is what a normal mouse would report. Also, the value 0x0e
128626b9da0SDmitry Torokhov 	 * isn't valid per PS/2 spec.
129626b9da0SDmitry Torokhov 	 */
130626b9da0SDmitry Torokhov 	{ { 0x20, 0x02, 0x0e }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } },
131626b9da0SDmitry Torokhov 
1328326bb57SDmitry Torokhov 	{ { 0x22, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } },
1338326bb57SDmitry Torokhov 	{ { 0x22, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xff, 0xff, ALPS_PASS | ALPS_DUALPOINT } },	/* Dell Latitude D600 */
1341d9f2626SSebastian Kapfer 	/* Dell Latitude E5500, E6400, E6500, Precision M4400 */
1358326bb57SDmitry Torokhov 	{ { 0x62, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xcf, 0xcf,
1368326bb57SDmitry Torokhov 		ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED } },
1378326bb57SDmitry Torokhov 	{ { 0x73, 0x00, 0x14 }, 0x00, { ALPS_PROTO_V6, 0xff, 0xff, ALPS_DUALPOINT } },		/* Dell XT2 */
1388326bb57SDmitry Torokhov 	{ { 0x73, 0x02, 0x50 }, 0x00, { ALPS_PROTO_V2, 0xcf, 0xcf, ALPS_FOUR_BUTTONS } },	/* Dell Vostro 1400 */
1398326bb57SDmitry Torokhov 	{ { 0x52, 0x01, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xff, 0xff,
1408326bb57SDmitry Torokhov 		ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED } },				/* Toshiba Tecra A11-11L */
1418326bb57SDmitry Torokhov 	{ { 0x73, 0x02, 0x64 }, 0x8a, { ALPS_PROTO_V4, 0x8f, 0x8f, 0 } },
1421da177e4SLinus Torvalds };
1431da177e4SLinus Torvalds 
1443296f71cSDmitry Torokhov static const struct alps_protocol_info alps_v3_protocol_data = {
1453296f71cSDmitry Torokhov 	ALPS_PROTO_V3, 0x8f, 0x8f, ALPS_DUALPOINT
1463296f71cSDmitry Torokhov };
1473296f71cSDmitry Torokhov 
1483296f71cSDmitry Torokhov static const struct alps_protocol_info alps_v3_rushmore_data = {
1493296f71cSDmitry Torokhov 	ALPS_PROTO_V3_RUSHMORE, 0x8f, 0x8f, ALPS_DUALPOINT
1503296f71cSDmitry Torokhov };
1513296f71cSDmitry Torokhov 
1523296f71cSDmitry Torokhov static const struct alps_protocol_info alps_v5_protocol_data = {
1533296f71cSDmitry Torokhov 	ALPS_PROTO_V5, 0xc8, 0xd8, 0
1543296f71cSDmitry Torokhov };
1553296f71cSDmitry Torokhov 
1563296f71cSDmitry Torokhov static const struct alps_protocol_info alps_v7_protocol_data = {
1573296f71cSDmitry Torokhov 	ALPS_PROTO_V7, 0x48, 0x48, ALPS_DUALPOINT
1583296f71cSDmitry Torokhov };
1593296f71cSDmitry Torokhov 
1603db5b9f7SMasaki Ota static const struct alps_protocol_info alps_v8_protocol_data = {
1613db5b9f7SMasaki Ota 	ALPS_PROTO_V8, 0x18, 0x18, 0
1623db5b9f7SMasaki Ota };
1633db5b9f7SMasaki Ota 
16419556219SHans de Goede /*
16519556219SHans de Goede  * Some v2 models report the stick buttons in separate bits
16619556219SHans de Goede  */
16719556219SHans de Goede static const struct dmi_system_id alps_dmi_has_separate_stick_buttons[] = {
16819556219SHans de Goede #if defined(CONFIG_DMI) && defined(CONFIG_X86)
16919556219SHans de Goede 	{
17019556219SHans de Goede 		/* Extrapolated from other entries */
17119556219SHans de Goede 		.matches = {
17219556219SHans de Goede 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
17319556219SHans de Goede 			DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D420"),
17419556219SHans de Goede 		},
17519556219SHans de Goede 	},
17619556219SHans de Goede 	{
17719556219SHans de Goede 		/* Reported-by: Hans de Bruin <jmdebruin@xmsnet.nl> */
17819556219SHans de Goede 		.matches = {
17919556219SHans de Goede 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
18019556219SHans de Goede 			DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D430"),
18119556219SHans de Goede 		},
18219556219SHans de Goede 	},
18319556219SHans de Goede 	{
18419556219SHans de Goede 		/* Reported-by: Hans de Goede <hdegoede@redhat.com> */
18519556219SHans de Goede 		.matches = {
18619556219SHans de Goede 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
18719556219SHans de Goede 			DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D620"),
18819556219SHans de Goede 		},
18919556219SHans de Goede 	},
19019556219SHans de Goede 	{
19119556219SHans de Goede 		/* Extrapolated from other entries */
19219556219SHans de Goede 		.matches = {
19319556219SHans de Goede 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
19419556219SHans de Goede 			DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D630"),
19519556219SHans de Goede 		},
19619556219SHans de Goede 	},
19719556219SHans de Goede #endif
19819556219SHans de Goede 	{ }
19919556219SHans de Goede };
20019556219SHans de Goede 
20124af5cb9SKevin Cernekee static void alps_set_abs_params_st(struct alps_data *priv,
20224af5cb9SKevin Cernekee 				   struct input_dev *dev1);
203688ea364SHans de Goede static void alps_set_abs_params_semi_mt(struct alps_data *priv,
20424af5cb9SKevin Cernekee 					struct input_dev *dev1);
2058eccd393SMasaki Ota static void alps_set_abs_params_v7(struct alps_data *priv,
2068eccd393SMasaki Ota 				   struct input_dev *dev1);
2073db5b9f7SMasaki Ota static void alps_set_abs_params_ss4_v2(struct alps_data *priv,
2083db5b9f7SMasaki Ota 				       struct input_dev *dev1);
20924af5cb9SKevin Cernekee 
210d4b347b2SSeth Forshee /* Packet formats are described in Documentation/input/alps.txt */
2111da177e4SLinus Torvalds 
21299df65e7SKevin Cernekee static bool alps_is_valid_first_byte(struct alps_data *priv,
2131d9f2626SSebastian Kapfer 				     unsigned char data)
2141d9f2626SSebastian Kapfer {
21599df65e7SKevin Cernekee 	return (data & priv->mask0) == priv->byte0;
2161d9f2626SSebastian Kapfer }
2171d9f2626SSebastian Kapfer 
21804aae283SPali Rohár static void alps_report_buttons(struct input_dev *dev1, struct input_dev *dev2,
2191d9f2626SSebastian Kapfer 				int left, int right, int middle)
2201d9f2626SSebastian Kapfer {
2211d9f2626SSebastian Kapfer 	struct input_dev *dev;
2221d9f2626SSebastian Kapfer 
2231d9f2626SSebastian Kapfer 	/*
2241d9f2626SSebastian Kapfer 	 * If shared button has already been reported on the
2251d9f2626SSebastian Kapfer 	 * other device (dev2) then this event should be also
2261d9f2626SSebastian Kapfer 	 * sent through that device.
2271d9f2626SSebastian Kapfer 	 */
22804aae283SPali Rohár 	dev = (dev2 && test_bit(BTN_LEFT, dev2->key)) ? dev2 : dev1;
2291d9f2626SSebastian Kapfer 	input_report_key(dev, BTN_LEFT, left);
2301d9f2626SSebastian Kapfer 
23104aae283SPali Rohár 	dev = (dev2 && test_bit(BTN_RIGHT, dev2->key)) ? dev2 : dev1;
2321d9f2626SSebastian Kapfer 	input_report_key(dev, BTN_RIGHT, right);
2331d9f2626SSebastian Kapfer 
23404aae283SPali Rohár 	dev = (dev2 && test_bit(BTN_MIDDLE, dev2->key)) ? dev2 : dev1;
2351d9f2626SSebastian Kapfer 	input_report_key(dev, BTN_MIDDLE, middle);
2361d9f2626SSebastian Kapfer 
2371d9f2626SSebastian Kapfer 	/*
2381d9f2626SSebastian Kapfer 	 * Sync the _other_ device now, we'll do the first
2391d9f2626SSebastian Kapfer 	 * device later once we report the rest of the events.
2401d9f2626SSebastian Kapfer 	 */
24104aae283SPali Rohár 	if (dev2)
2421d9f2626SSebastian Kapfer 		input_sync(dev2);
2431d9f2626SSebastian Kapfer }
2441d9f2626SSebastian Kapfer 
24525bded7cSSeth Forshee static void alps_process_packet_v1_v2(struct psmouse *psmouse)
2461da177e4SLinus Torvalds {
2471da177e4SLinus Torvalds 	struct alps_data *priv = psmouse->private;
2481da177e4SLinus Torvalds 	unsigned char *packet = psmouse->packet;
2492e5b636bSDmitry Torokhov 	struct input_dev *dev = psmouse->dev;
2502e5b636bSDmitry Torokhov 	struct input_dev *dev2 = priv->dev2;
2511da177e4SLinus Torvalds 	int x, y, z, ges, fin, left, right, middle;
252c30b4c10SIvan Casado Ruiz 	int back = 0, forward = 0;
2531da177e4SLinus Torvalds 
25499df65e7SKevin Cernekee 	if (priv->proto_version == ALPS_PROTO_V1) {
255d2f4012fSYotam Medini 		left = packet[2] & 0x10;
256d2f4012fSYotam Medini 		right = packet[2] & 0x08;
2571da177e4SLinus Torvalds 		middle = 0;
2581da177e4SLinus Torvalds 		x = packet[1] | ((packet[0] & 0x07) << 7);
2591da177e4SLinus Torvalds 		y = packet[4] | ((packet[3] & 0x07) << 7);
2601da177e4SLinus Torvalds 		z = packet[5];
2611da177e4SLinus Torvalds 	} else {
2621da177e4SLinus Torvalds 		left = packet[3] & 1;
2631da177e4SLinus Torvalds 		right = packet[3] & 2;
2641da177e4SLinus Torvalds 		middle = packet[3] & 4;
2651da177e4SLinus Torvalds 		x = packet[1] | ((packet[2] & 0x78) << (7 - 3));
2661da177e4SLinus Torvalds 		y = packet[4] | ((packet[3] & 0x70) << (7 - 4));
2671da177e4SLinus Torvalds 		z = packet[5];
2681da177e4SLinus Torvalds 	}
2691da177e4SLinus Torvalds 
27099df65e7SKevin Cernekee 	if (priv->flags & ALPS_FW_BK_1) {
2713c00bb96SLaszlo Kajan 		back = packet[0] & 0x10;
2723c00bb96SLaszlo Kajan 		forward = packet[2] & 4;
273c30b4c10SIvan Casado Ruiz 	}
274c30b4c10SIvan Casado Ruiz 
27599df65e7SKevin Cernekee 	if (priv->flags & ALPS_FW_BK_2) {
276c30b4c10SIvan Casado Ruiz 		back = packet[3] & 4;
277c30b4c10SIvan Casado Ruiz 		forward = packet[2] & 4;
278c30b4c10SIvan Casado Ruiz 		if ((middle = forward && back))
279c30b4c10SIvan Casado Ruiz 			forward = back = 0;
280c30b4c10SIvan Casado Ruiz 	}
281c30b4c10SIvan Casado Ruiz 
2821da177e4SLinus Torvalds 	ges = packet[2] & 1;
2831da177e4SLinus Torvalds 	fin = packet[2] & 2;
2841da177e4SLinus Torvalds 
28599df65e7SKevin Cernekee 	if ((priv->flags & ALPS_DUALPOINT) && z == 127) {
2861da177e4SLinus Torvalds 		input_report_rel(dev2, REL_X,  (x > 383 ? (x - 768) : x));
2871da177e4SLinus Torvalds 		input_report_rel(dev2, REL_Y, -(y > 255 ? (y - 512) : y));
288d7ed5d88SUlrich Dangel 
28904aae283SPali Rohár 		alps_report_buttons(dev2, dev, left, right, middle);
290d7ed5d88SUlrich Dangel 
2911da177e4SLinus Torvalds 		input_sync(dev2);
2921da177e4SLinus Torvalds 		return;
2931da177e4SLinus Torvalds 	}
2941da177e4SLinus Torvalds 
29519556219SHans de Goede 	/* Some models have separate stick button bits */
29619556219SHans de Goede 	if (priv->flags & ALPS_STICK_BITS) {
29792bac83dSHans de Goede 		left |= packet[0] & 1;
29892bac83dSHans de Goede 		right |= packet[0] & 2;
29992bac83dSHans de Goede 		middle |= packet[0] & 4;
30092bac83dSHans de Goede 	}
30192bac83dSHans de Goede 
30204aae283SPali Rohár 	alps_report_buttons(dev, dev2, left, right, middle);
303d7ed5d88SUlrich Dangel 
3041da177e4SLinus Torvalds 	/* Convert hardware tap to a reasonable Z value */
30571bb21b6SMaxim Levitsky 	if (ges && !fin)
30671bb21b6SMaxim Levitsky 		z = 40;
3071da177e4SLinus Torvalds 
3081da177e4SLinus Torvalds 	/*
3091da177e4SLinus Torvalds 	 * A "tap and drag" operation is reported by the hardware as a transition
3101da177e4SLinus Torvalds 	 * from (!fin && ges) to (fin && ges). This should be translated to the
3111da177e4SLinus Torvalds 	 * sequence Z>0, Z==0, Z>0, so the Z==0 event has to be generated manually.
3121da177e4SLinus Torvalds 	 */
3131da177e4SLinus Torvalds 	if (ges && fin && !priv->prev_fin) {
3141da177e4SLinus Torvalds 		input_report_abs(dev, ABS_X, x);
3151da177e4SLinus Torvalds 		input_report_abs(dev, ABS_Y, y);
3161da177e4SLinus Torvalds 		input_report_abs(dev, ABS_PRESSURE, 0);
3171da177e4SLinus Torvalds 		input_report_key(dev, BTN_TOOL_FINGER, 0);
3181da177e4SLinus Torvalds 		input_sync(dev);
3191da177e4SLinus Torvalds 	}
3201da177e4SLinus Torvalds 	priv->prev_fin = fin;
3211da177e4SLinus Torvalds 
32271bb21b6SMaxim Levitsky 	if (z > 30)
32371bb21b6SMaxim Levitsky 		input_report_key(dev, BTN_TOUCH, 1);
32471bb21b6SMaxim Levitsky 	if (z < 25)
32571bb21b6SMaxim Levitsky 		input_report_key(dev, BTN_TOUCH, 0);
3261da177e4SLinus Torvalds 
3271da177e4SLinus Torvalds 	if (z > 0) {
3281da177e4SLinus Torvalds 		input_report_abs(dev, ABS_X, x);
3291da177e4SLinus Torvalds 		input_report_abs(dev, ABS_Y, y);
3301da177e4SLinus Torvalds 	}
3311da177e4SLinus Torvalds 
3321da177e4SLinus Torvalds 	input_report_abs(dev, ABS_PRESSURE, z);
3331da177e4SLinus Torvalds 	input_report_key(dev, BTN_TOOL_FINGER, z > 0);
3341da177e4SLinus Torvalds 
33599df65e7SKevin Cernekee 	if (priv->flags & ALPS_WHEEL)
336e6c047b9SVojtech Pavlik 		input_report_rel(dev, REL_WHEEL, ((packet[2] << 1) & 0x08) - ((packet[0] >> 4) & 0x07));
3371da177e4SLinus Torvalds 
33899df65e7SKevin Cernekee 	if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
339c30b4c10SIvan Casado Ruiz 		input_report_key(dev, BTN_FORWARD, forward);
340c30b4c10SIvan Casado Ruiz 		input_report_key(dev, BTN_BACK, back);
3411da177e4SLinus Torvalds 	}
3421da177e4SLinus Torvalds 
34399df65e7SKevin Cernekee 	if (priv->flags & ALPS_FOUR_BUTTONS) {
34471bb21b6SMaxim Levitsky 		input_report_key(dev, BTN_0, packet[2] & 4);
34571bb21b6SMaxim Levitsky 		input_report_key(dev, BTN_1, packet[0] & 0x10);
34671bb21b6SMaxim Levitsky 		input_report_key(dev, BTN_2, packet[3] & 4);
34771bb21b6SMaxim Levitsky 		input_report_key(dev, BTN_3, packet[0] & 0x20);
34871bb21b6SMaxim Levitsky 	}
34971bb21b6SMaxim Levitsky 
3501da177e4SLinus Torvalds 	input_sync(dev);
3511da177e4SLinus Torvalds }
3521da177e4SLinus Torvalds 
353036e6c7bSHans de Goede static void alps_get_bitmap_points(unsigned int map,
354036e6c7bSHans de Goede 				   struct alps_bitmap_point *low,
355036e6c7bSHans de Goede 				   struct alps_bitmap_point *high,
356036e6c7bSHans de Goede 				   int *fingers)
357036e6c7bSHans de Goede {
358036e6c7bSHans de Goede 	struct alps_bitmap_point *point;
359036e6c7bSHans de Goede 	int i, bit, prev_bit = 0;
360036e6c7bSHans de Goede 
361036e6c7bSHans de Goede 	point = low;
362036e6c7bSHans de Goede 	for (i = 0; map != 0; i++, map >>= 1) {
363036e6c7bSHans de Goede 		bit = map & 1;
364036e6c7bSHans de Goede 		if (bit) {
365036e6c7bSHans de Goede 			if (!prev_bit) {
366036e6c7bSHans de Goede 				point->start_bit = i;
367105affbfSHans de Goede 				point->num_bits = 0;
368036e6c7bSHans de Goede 				(*fingers)++;
369036e6c7bSHans de Goede 			}
370036e6c7bSHans de Goede 			point->num_bits++;
371036e6c7bSHans de Goede 		} else {
372036e6c7bSHans de Goede 			if (prev_bit)
373036e6c7bSHans de Goede 				point = high;
374036e6c7bSHans de Goede 		}
375036e6c7bSHans de Goede 		prev_bit = bit;
376036e6c7bSHans de Goede 	}
377036e6c7bSHans de Goede }
378036e6c7bSHans de Goede 
379ee65d4b3SYunkang Tang /*
380dccf1dd8SHans de Goede  * Process bitmap data from semi-mt protocols. Returns the number of
38101ce661fSSeth Forshee  * fingers detected. A return value of 0 means at least one of the
38201ce661fSSeth Forshee  * bitmaps was empty.
38301ce661fSSeth Forshee  *
38401ce661fSSeth Forshee  * The bitmaps don't have enough data to track fingers, so this function
38501ce661fSSeth Forshee  * only generates points representing a bounding box of all contacts.
38602d04254SHans de Goede  * These points are returned in fields->mt when the return value
38701ce661fSSeth Forshee  * is greater than 0.
38801ce661fSSeth Forshee  */
3897a9f73e7SKevin Cernekee static int alps_process_bitmap(struct alps_data *priv,
39002d04254SHans de Goede 			       struct alps_fields *fields)
39101ce661fSSeth Forshee {
3924dd26573SHans de Goede 	int i, fingers_x = 0, fingers_y = 0, fingers, closest;
39301ce661fSSeth Forshee 	struct alps_bitmap_point x_low = {0,}, x_high = {0,};
39401ce661fSSeth Forshee 	struct alps_bitmap_point y_low = {0,}, y_high = {0,};
3954dd26573SHans de Goede 	struct input_mt_pos corner[4];
39601ce661fSSeth Forshee 
39702d04254SHans de Goede 	if (!fields->x_map || !fields->y_map)
39801ce661fSSeth Forshee 		return 0;
39901ce661fSSeth Forshee 
40002d04254SHans de Goede 	alps_get_bitmap_points(fields->x_map, &x_low, &x_high, &fingers_x);
40102d04254SHans de Goede 	alps_get_bitmap_points(fields->y_map, &y_low, &y_high, &fingers_y);
40201ce661fSSeth Forshee 
40301ce661fSSeth Forshee 	/*
40401ce661fSSeth Forshee 	 * Fingers can overlap, so we use the maximum count of fingers
40501ce661fSSeth Forshee 	 * on either axis as the finger count.
40601ce661fSSeth Forshee 	 */
40701ce661fSSeth Forshee 	fingers = max(fingers_x, fingers_y);
40801ce661fSSeth Forshee 
40901ce661fSSeth Forshee 	/*
41020bea68bSHans de Goede 	 * If an axis reports only a single contact, we have overlapping or
41120bea68bSHans de Goede 	 * adjacent fingers. Divide the single contact between the two points.
41201ce661fSSeth Forshee 	 */
41301ce661fSSeth Forshee 	if (fingers_x == 1) {
41428835f45SHans de Goede 		i = (x_low.num_bits - 1) / 2;
41501ce661fSSeth Forshee 		x_low.num_bits = x_low.num_bits - i;
41601ce661fSSeth Forshee 		x_high.start_bit = x_low.start_bit + i;
41701ce661fSSeth Forshee 		x_high.num_bits = max(i, 1);
41820bea68bSHans de Goede 	}
41920bea68bSHans de Goede 	if (fingers_y == 1) {
42028835f45SHans de Goede 		i = (y_low.num_bits - 1) / 2;
42101ce661fSSeth Forshee 		y_low.num_bits = y_low.num_bits - i;
42201ce661fSSeth Forshee 		y_high.start_bit = y_low.start_bit + i;
42301ce661fSSeth Forshee 		y_high.num_bits = max(i, 1);
42401ce661fSSeth Forshee 	}
42501ce661fSSeth Forshee 
4264dd26573SHans de Goede 	/* top-left corner */
4274dd26573SHans de Goede 	corner[0].x =
42802d04254SHans de Goede 		(priv->x_max * (2 * x_low.start_bit + x_low.num_bits - 1)) /
4297a9f73e7SKevin Cernekee 		(2 * (priv->x_bits - 1));
4304dd26573SHans de Goede 	corner[0].y =
43102d04254SHans de Goede 		(priv->y_max * (2 * y_low.start_bit + y_low.num_bits - 1)) /
4327a9f73e7SKevin Cernekee 		(2 * (priv->y_bits - 1));
43301ce661fSSeth Forshee 
4344dd26573SHans de Goede 	/* top-right corner */
4354dd26573SHans de Goede 	corner[1].x =
43602d04254SHans de Goede 		(priv->x_max * (2 * x_high.start_bit + x_high.num_bits - 1)) /
4377a9f73e7SKevin Cernekee 		(2 * (priv->x_bits - 1));
4384dd26573SHans de Goede 	corner[1].y =
4394dd26573SHans de Goede 		(priv->y_max * (2 * y_low.start_bit + y_low.num_bits - 1)) /
4404dd26573SHans de Goede 		(2 * (priv->y_bits - 1));
4414dd26573SHans de Goede 
4424dd26573SHans de Goede 	/* bottom-right corner */
4434dd26573SHans de Goede 	corner[2].x =
4444dd26573SHans de Goede 		(priv->x_max * (2 * x_high.start_bit + x_high.num_bits - 1)) /
4454dd26573SHans de Goede 		(2 * (priv->x_bits - 1));
4464dd26573SHans de Goede 	corner[2].y =
44702d04254SHans de Goede 		(priv->y_max * (2 * y_high.start_bit + y_high.num_bits - 1)) /
4487a9f73e7SKevin Cernekee 		(2 * (priv->y_bits - 1));
44901ce661fSSeth Forshee 
4504dd26573SHans de Goede 	/* bottom-left corner */
4514dd26573SHans de Goede 	corner[3].x =
4524dd26573SHans de Goede 		(priv->x_max * (2 * x_low.start_bit + x_low.num_bits - 1)) /
4534dd26573SHans de Goede 		(2 * (priv->x_bits - 1));
4544dd26573SHans de Goede 	corner[3].y =
45501ce661fSSeth Forshee 		(priv->y_max * (2 * y_high.start_bit + y_high.num_bits - 1)) /
45601ce661fSSeth Forshee 		(2 * (priv->y_bits - 1));
45701ce661fSSeth Forshee 
458dccf1dd8SHans de Goede 	/* x-bitmap order is reversed on v5 touchpads  */
459dccf1dd8SHans de Goede 	if (priv->proto_version == ALPS_PROTO_V5) {
460dccf1dd8SHans de Goede 		for (i = 0; i < 4; i++)
461dccf1dd8SHans de Goede 			corner[i].x = priv->x_max - corner[i].x;
46240e8f53bSHans de Goede 	}
46340e8f53bSHans de Goede 
464dccf1dd8SHans de Goede 	/* y-bitmap order is reversed on v3 and v4 touchpads  */
465dccf1dd8SHans de Goede 	if (priv->proto_version == ALPS_PROTO_V3 ||
466dccf1dd8SHans de Goede 	    priv->proto_version == ALPS_PROTO_V4) {
4674dd26573SHans de Goede 		for (i = 0; i < 4; i++)
4684dd26573SHans de Goede 			corner[i].y = priv->y_max - corner[i].y;
46901ce661fSSeth Forshee 	}
47001ce661fSSeth Forshee 
4714dd26573SHans de Goede 	/*
4724dd26573SHans de Goede 	 * We only select a corner for the second touch once per 2 finger
4734dd26573SHans de Goede 	 * touch sequence to avoid the chosen corner (and thus the coordinates)
4744dd26573SHans de Goede 	 * jumping around when the first touch is in the middle.
4754dd26573SHans de Goede 	 */
4764dd26573SHans de Goede 	if (priv->second_touch == -1) {
4774dd26573SHans de Goede 		/* Find corner closest to our st coordinates */
4784dd26573SHans de Goede 		closest = 0x7fffffff;
4794dd26573SHans de Goede 		for (i = 0; i < 4; i++) {
4804dd26573SHans de Goede 			int dx = fields->st.x - corner[i].x;
4814dd26573SHans de Goede 			int dy = fields->st.y - corner[i].y;
4824dd26573SHans de Goede 			int distance = dx * dx + dy * dy;
4834dd26573SHans de Goede 
4844dd26573SHans de Goede 			if (distance < closest) {
4854dd26573SHans de Goede 				priv->second_touch = i;
4864dd26573SHans de Goede 				closest = distance;
4874dd26573SHans de Goede 			}
4884dd26573SHans de Goede 		}
4894dd26573SHans de Goede 		/* And select the opposite corner to use for the 2nd touch */
4904dd26573SHans de Goede 		priv->second_touch = (priv->second_touch + 2) % 4;
4914dd26573SHans de Goede 	}
4924dd26573SHans de Goede 
4934dd26573SHans de Goede 	fields->mt[0] = fields->st;
4944dd26573SHans de Goede 	fields->mt[1] = corner[priv->second_touch];
4954dd26573SHans de Goede 
49601ce661fSSeth Forshee 	return fingers;
49701ce661fSSeth Forshee }
49801ce661fSSeth Forshee 
499cdf333efSHans de Goede static void alps_set_slot(struct input_dev *dev, int slot, int x, int y)
50001ce661fSSeth Forshee {
50101ce661fSSeth Forshee 	input_mt_slot(dev, slot);
502cdf333efSHans de Goede 	input_mt_report_slot_state(dev, MT_TOOL_FINGER, true);
50301ce661fSSeth Forshee 	input_report_abs(dev, ABS_MT_POSITION_X, x);
50401ce661fSSeth Forshee 	input_report_abs(dev, ABS_MT_POSITION_Y, y);
50501ce661fSSeth Forshee }
50601ce661fSSeth Forshee 
507cdf333efSHans de Goede static void alps_report_mt_data(struct psmouse *psmouse, int n)
50801ce661fSSeth Forshee {
50902d04254SHans de Goede 	struct alps_data *priv = psmouse->private;
51002d04254SHans de Goede 	struct input_dev *dev = psmouse->dev;
51102d04254SHans de Goede 	struct alps_fields *f = &priv->f;
512cdf333efSHans de Goede 	int i, slot[MAX_TOUCHES];
51302d04254SHans de Goede 
514448c7f38SHenrik Rydberg 	input_mt_assign_slots(dev, slot, f->mt, n, 0);
515cdf333efSHans de Goede 	for (i = 0; i < n; i++)
516cdf333efSHans de Goede 		alps_set_slot(dev, slot[i], f->mt[i].x, f->mt[i].y);
517cdf333efSHans de Goede 
518cdf333efSHans de Goede 	input_mt_sync_frame(dev);
51901ce661fSSeth Forshee }
52001ce661fSSeth Forshee 
52168c21870SHans de Goede static void alps_report_semi_mt_data(struct psmouse *psmouse, int fingers)
52268c21870SHans de Goede {
52368c21870SHans de Goede 	struct alps_data *priv = psmouse->private;
52468c21870SHans de Goede 	struct input_dev *dev = psmouse->dev;
52568c21870SHans de Goede 	struct alps_fields *f = &priv->f;
52668c21870SHans de Goede 
52768c21870SHans de Goede 	/* Use st data when we don't have mt data */
52868c21870SHans de Goede 	if (fingers < 2) {
52968c21870SHans de Goede 		f->mt[0].x = f->st.x;
53068c21870SHans de Goede 		f->mt[0].y = f->st.y;
53168c21870SHans de Goede 		fingers = f->pressure > 0 ? 1 : 0;
5324dd26573SHans de Goede 		priv->second_touch = -1;
53368c21870SHans de Goede 	}
53468c21870SHans de Goede 
5351662c033SHans de Goede 	if (fingers >= 1)
5361662c033SHans de Goede 		alps_set_slot(dev, 0, f->mt[0].x, f->mt[0].y);
5371662c033SHans de Goede 	if (fingers >= 2)
5381662c033SHans de Goede 		alps_set_slot(dev, 1, f->mt[1].x, f->mt[1].y);
5391662c033SHans de Goede 	input_mt_sync_frame(dev);
54068c21870SHans de Goede 
54168c21870SHans de Goede 	input_mt_report_finger_count(dev, fingers);
54268c21870SHans de Goede 
54368c21870SHans de Goede 	input_report_key(dev, BTN_LEFT, f->left);
54468c21870SHans de Goede 	input_report_key(dev, BTN_RIGHT, f->right);
54568c21870SHans de Goede 	input_report_key(dev, BTN_MIDDLE, f->middle);
54668c21870SHans de Goede 
54768c21870SHans de Goede 	input_report_abs(dev, ABS_PRESSURE, f->pressure);
54868c21870SHans de Goede 
54968c21870SHans de Goede 	input_sync(dev);
55068c21870SHans de Goede }
55168c21870SHans de Goede 
55225bded7cSSeth Forshee static void alps_process_trackstick_packet_v3(struct psmouse *psmouse)
55325bded7cSSeth Forshee {
55425bded7cSSeth Forshee 	struct alps_data *priv = psmouse->private;
55525bded7cSSeth Forshee 	unsigned char *packet = psmouse->packet;
55625bded7cSSeth Forshee 	struct input_dev *dev = priv->dev2;
55725bded7cSSeth Forshee 	int x, y, z, left, right, middle;
55825bded7cSSeth Forshee 
55934412ba2SPali Rohár 	/* It should be a DualPoint when received trackstick packet */
56034412ba2SPali Rohár 	if (!(priv->flags & ALPS_DUALPOINT)) {
56134412ba2SPali Rohár 		psmouse_warn(psmouse,
56234412ba2SPali Rohár 			     "Rejected trackstick packet from non DualPoint device");
56334412ba2SPali Rohár 		return;
56434412ba2SPali Rohár 	}
56534412ba2SPali Rohár 
56625bded7cSSeth Forshee 	/* Sanity check packet */
56725bded7cSSeth Forshee 	if (!(packet[0] & 0x40)) {
56825bded7cSSeth Forshee 		psmouse_dbg(psmouse, "Bad trackstick packet, discarding\n");
56925bded7cSSeth Forshee 		return;
57025bded7cSSeth Forshee 	}
57125bded7cSSeth Forshee 
57225bded7cSSeth Forshee 	/*
57325bded7cSSeth Forshee 	 * There's a special packet that seems to indicate the end
57425bded7cSSeth Forshee 	 * of a stream of trackstick data. Filter these out.
57525bded7cSSeth Forshee 	 */
57625bded7cSSeth Forshee 	if (packet[1] == 0x7f && packet[2] == 0x7f && packet[4] == 0x7f)
57725bded7cSSeth Forshee 		return;
57825bded7cSSeth Forshee 
57925bded7cSSeth Forshee 	x = (s8)(((packet[0] & 0x20) << 2) | (packet[1] & 0x7f));
58025bded7cSSeth Forshee 	y = (s8)(((packet[0] & 0x10) << 3) | (packet[2] & 0x7f));
58125bded7cSSeth Forshee 	z = (packet[4] & 0x7c) >> 2;
58225bded7cSSeth Forshee 
58325bded7cSSeth Forshee 	/*
58425bded7cSSeth Forshee 	 * The x and y values tend to be quite large, and when used
58525bded7cSSeth Forshee 	 * alone the trackstick is difficult to use. Scale them down
58625bded7cSSeth Forshee 	 * to compensate.
58725bded7cSSeth Forshee 	 */
58825bded7cSSeth Forshee 	x /= 8;
58925bded7cSSeth Forshee 	y /= 8;
59025bded7cSSeth Forshee 
59125bded7cSSeth Forshee 	input_report_rel(dev, REL_X, x);
59225bded7cSSeth Forshee 	input_report_rel(dev, REL_Y, -y);
59325bded7cSSeth Forshee 
59425bded7cSSeth Forshee 	/*
59525bded7cSSeth Forshee 	 * Most ALPS models report the trackstick buttons in the touchpad
59625bded7cSSeth Forshee 	 * packets, but a few report them here. No reliable way has been
59725bded7cSSeth Forshee 	 * found to differentiate between the models upfront, so we enable
59825bded7cSSeth Forshee 	 * the quirk in response to seeing a button press in the trackstick
59925bded7cSSeth Forshee 	 * packet.
60025bded7cSSeth Forshee 	 */
60125bded7cSSeth Forshee 	left = packet[3] & 0x01;
60225bded7cSSeth Forshee 	right = packet[3] & 0x02;
60325bded7cSSeth Forshee 	middle = packet[3] & 0x04;
60425bded7cSSeth Forshee 
60525bded7cSSeth Forshee 	if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) &&
60625bded7cSSeth Forshee 	    (left || right || middle))
60725bded7cSSeth Forshee 		priv->quirks |= ALPS_QUIRK_TRACKSTICK_BUTTONS;
60825bded7cSSeth Forshee 
60925bded7cSSeth Forshee 	if (priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) {
61025bded7cSSeth Forshee 		input_report_key(dev, BTN_LEFT, left);
61125bded7cSSeth Forshee 		input_report_key(dev, BTN_RIGHT, right);
61225bded7cSSeth Forshee 		input_report_key(dev, BTN_MIDDLE, middle);
61325bded7cSSeth Forshee 	}
61425bded7cSSeth Forshee 
61525bded7cSSeth Forshee 	input_sync(dev);
61625bded7cSSeth Forshee 	return;
61725bded7cSSeth Forshee }
61825bded7cSSeth Forshee 
619f85e5001SKevin Cernekee static void alps_decode_buttons_v3(struct alps_fields *f, unsigned char *p)
620f85e5001SKevin Cernekee {
621f85e5001SKevin Cernekee 	f->left = !!(p[3] & 0x01);
622f85e5001SKevin Cernekee 	f->right = !!(p[3] & 0x02);
623f85e5001SKevin Cernekee 	f->middle = !!(p[3] & 0x04);
624f85e5001SKevin Cernekee 
625f85e5001SKevin Cernekee 	f->ts_left = !!(p[3] & 0x10);
626f85e5001SKevin Cernekee 	f->ts_right = !!(p[3] & 0x20);
627f85e5001SKevin Cernekee 	f->ts_middle = !!(p[3] & 0x40);
628f85e5001SKevin Cernekee }
629f85e5001SKevin Cernekee 
63038c11eaaSHans de Goede static int alps_decode_pinnacle(struct alps_fields *f, unsigned char *p,
631ee65d4b3SYunkang Tang 				 struct psmouse *psmouse)
632f85e5001SKevin Cernekee {
633f85e5001SKevin Cernekee 	f->first_mp = !!(p[4] & 0x40);
634f85e5001SKevin Cernekee 	f->is_mp = !!(p[0] & 0x40);
635f85e5001SKevin Cernekee 
636a839cd57SHans de Goede 	if (f->is_mp) {
637f85e5001SKevin Cernekee 		f->fingers = (p[5] & 0x3) + 1;
638f85e5001SKevin Cernekee 		f->x_map = ((p[4] & 0x7e) << 8) |
639f85e5001SKevin Cernekee 			   ((p[1] & 0x7f) << 2) |
640f85e5001SKevin Cernekee 			   ((p[0] & 0x30) >> 4);
641f85e5001SKevin Cernekee 		f->y_map = ((p[3] & 0x70) << 4) |
642f85e5001SKevin Cernekee 			   ((p[2] & 0x7f) << 1) |
643f85e5001SKevin Cernekee 			   (p[4] & 0x01);
644a839cd57SHans de Goede 	} else {
64502d04254SHans de Goede 		f->st.x = ((p[1] & 0x7f) << 4) | ((p[4] & 0x30) >> 2) |
646f85e5001SKevin Cernekee 		       ((p[0] & 0x30) >> 4);
64702d04254SHans de Goede 		f->st.y = ((p[2] & 0x7f) << 4) | (p[4] & 0x0f);
64802d04254SHans de Goede 		f->pressure = p[5] & 0x7f;
649f85e5001SKevin Cernekee 
650f85e5001SKevin Cernekee 		alps_decode_buttons_v3(f, p);
651a839cd57SHans de Goede 	}
65238c11eaaSHans de Goede 
65338c11eaaSHans de Goede 	return 0;
654f85e5001SKevin Cernekee }
655f85e5001SKevin Cernekee 
65638c11eaaSHans de Goede static int alps_decode_rushmore(struct alps_fields *f, unsigned char *p,
657ee65d4b3SYunkang Tang 				 struct psmouse *psmouse)
6581302bac3SKevin Cernekee {
659aab9cf7bSHans de Goede 	f->first_mp = !!(p[4] & 0x40);
660f105e34aSYunkang Tang 	f->is_mp = !!(p[5] & 0x40);
661aab9cf7bSHans de Goede 
662a839cd57SHans de Goede 	if (f->is_mp) {
663f105e34aSYunkang Tang 		f->fingers = max((p[5] & 0x3), ((p[5] >> 2) & 0x3)) + 1;
664aab9cf7bSHans de Goede 		f->x_map = ((p[5] & 0x10) << 11) |
665aab9cf7bSHans de Goede 			   ((p[4] & 0x7e) << 8) |
666aab9cf7bSHans de Goede 			   ((p[1] & 0x7f) << 2) |
667aab9cf7bSHans de Goede 			   ((p[0] & 0x30) >> 4);
668aab9cf7bSHans de Goede 		f->y_map = ((p[5] & 0x20) << 6) |
669aab9cf7bSHans de Goede 			   ((p[3] & 0x70) << 4) |
670aab9cf7bSHans de Goede 			   ((p[2] & 0x7f) << 1) |
671aab9cf7bSHans de Goede 			   (p[4] & 0x01);
672a839cd57SHans de Goede 	} else {
673aab9cf7bSHans de Goede 		f->st.x = ((p[1] & 0x7f) << 4) | ((p[4] & 0x30) >> 2) |
674aab9cf7bSHans de Goede 		       ((p[0] & 0x30) >> 4);
675aab9cf7bSHans de Goede 		f->st.y = ((p[2] & 0x7f) << 4) | (p[4] & 0x0f);
676aab9cf7bSHans de Goede 		f->pressure = p[5] & 0x7f;
677aab9cf7bSHans de Goede 
678aab9cf7bSHans de Goede 		alps_decode_buttons_v3(f, p);
679a839cd57SHans de Goede 	}
68038c11eaaSHans de Goede 
68138c11eaaSHans de Goede 	return 0;
6821302bac3SKevin Cernekee }
6831302bac3SKevin Cernekee 
68438c11eaaSHans de Goede static int alps_decode_dolphin(struct alps_fields *f, unsigned char *p,
685ee65d4b3SYunkang Tang 				struct psmouse *psmouse)
68675af9e56SDave Turvene {
687ee65d4b3SYunkang Tang 	u64 palm_data = 0;
688ee65d4b3SYunkang Tang 	struct alps_data *priv = psmouse->private;
689ee65d4b3SYunkang Tang 
69075af9e56SDave Turvene 	f->first_mp = !!(p[0] & 0x02);
69175af9e56SDave Turvene 	f->is_mp = !!(p[0] & 0x20);
69275af9e56SDave Turvene 
693ee65d4b3SYunkang Tang 	if (!f->is_mp) {
69402d04254SHans de Goede 		f->st.x = ((p[1] & 0x7f) | ((p[4] & 0x0f) << 7));
69502d04254SHans de Goede 		f->st.y = ((p[2] & 0x7f) | ((p[4] & 0xf0) << 3));
69602d04254SHans de Goede 		f->pressure = (p[0] & 4) ? 0 : p[5] & 0x7f;
69775af9e56SDave Turvene 		alps_decode_buttons_v3(f, p);
698ee65d4b3SYunkang Tang 	} else {
699ee65d4b3SYunkang Tang 		f->fingers = ((p[0] & 0x6) >> 1 |
700ee65d4b3SYunkang Tang 		     (p[0] & 0x10) >> 2);
701ee65d4b3SYunkang Tang 
702ee65d4b3SYunkang Tang 		palm_data = (p[1] & 0x7f) |
703ee65d4b3SYunkang Tang 			    ((p[2] & 0x7f) << 7) |
704ee65d4b3SYunkang Tang 			    ((p[4] & 0x7f) << 14) |
705ee65d4b3SYunkang Tang 			    ((p[5] & 0x7f) << 21) |
706ee65d4b3SYunkang Tang 			    ((p[3] & 0x07) << 28) |
707ee65d4b3SYunkang Tang 			    (((u64)p[3] & 0x70) << 27) |
708ee65d4b3SYunkang Tang 			    (((u64)p[0] & 0x01) << 34);
709ee65d4b3SYunkang Tang 
710ee65d4b3SYunkang Tang 		/* Y-profile is stored in P(0) to p(n-1), n = y_bits; */
711ee65d4b3SYunkang Tang 		f->y_map = palm_data & (BIT(priv->y_bits) - 1);
712ee65d4b3SYunkang Tang 
713ee65d4b3SYunkang Tang 		/* X-profile is stored in p(n) to p(n+m-1), m = x_bits; */
714ee65d4b3SYunkang Tang 		f->x_map = (palm_data >> priv->y_bits) &
715ee65d4b3SYunkang Tang 			   (BIT(priv->x_bits) - 1);
716ee65d4b3SYunkang Tang 	}
71738c11eaaSHans de Goede 
71838c11eaaSHans de Goede 	return 0;
71975af9e56SDave Turvene }
72075af9e56SDave Turvene 
721ee65d4b3SYunkang Tang static void alps_process_touchpad_packet_v3_v5(struct psmouse *psmouse)
72225bded7cSSeth Forshee {
72325bded7cSSeth Forshee 	struct alps_data *priv = psmouse->private;
72425bded7cSSeth Forshee 	unsigned char *packet = psmouse->packet;
72525bded7cSSeth Forshee 	struct input_dev *dev2 = priv->dev2;
72602d04254SHans de Goede 	struct alps_fields *f = &priv->f;
72702d04254SHans de Goede 	int fingers = 0;
728f85e5001SKevin Cernekee 
72902d04254SHans de Goede 	memset(f, 0, sizeof(*f));
73002d04254SHans de Goede 
73102d04254SHans de Goede 	priv->decode_fields(f, packet, psmouse);
73225bded7cSSeth Forshee 
73325bded7cSSeth Forshee 	/*
73401ce661fSSeth Forshee 	 * There's no single feature of touchpad position and bitmap packets
73501ce661fSSeth Forshee 	 * that can be used to distinguish between them. We rely on the fact
73601ce661fSSeth Forshee 	 * that a bitmap packet should always follow a position packet with
73701ce661fSSeth Forshee 	 * bit 6 of packet[4] set.
73825bded7cSSeth Forshee 	 */
73925bded7cSSeth Forshee 	if (priv->multi_packet) {
74025bded7cSSeth Forshee 		/*
74125bded7cSSeth Forshee 		 * Sometimes a position packet will indicate a multi-packet
74225bded7cSSeth Forshee 		 * sequence, but then what follows is another position
74325bded7cSSeth Forshee 		 * packet. Check for this, and when it happens process the
74425bded7cSSeth Forshee 		 * position packet as usual.
74525bded7cSSeth Forshee 		 */
74602d04254SHans de Goede 		if (f->is_mp) {
74702d04254SHans de Goede 			fingers = f->fingers;
74844b77f38SHans de Goede 			/*
74944b77f38SHans de Goede 			 * Bitmap processing uses position packet's coordinate
75044b77f38SHans de Goede 			 * data, so we need to do decode it first.
75144b77f38SHans de Goede 			 */
75244b77f38SHans de Goede 			priv->decode_fields(f, priv->multi_data, psmouse);
75302d04254SHans de Goede 			if (alps_process_bitmap(priv, f) == 0)
75420bea68bSHans de Goede 				fingers = 0; /* Use st data */
75501ce661fSSeth Forshee 		} else {
75601ce661fSSeth Forshee 			priv->multi_packet = 0;
75725bded7cSSeth Forshee 		}
75825bded7cSSeth Forshee 	}
75925bded7cSSeth Forshee 
76001ce661fSSeth Forshee 	/*
76101ce661fSSeth Forshee 	 * Bit 6 of byte 0 is not usually set in position packets. The only
76201ce661fSSeth Forshee 	 * times it seems to be set is in situations where the data is
76301ce661fSSeth Forshee 	 * suspect anyway, e.g. a palm resting flat on the touchpad. Given
76401ce661fSSeth Forshee 	 * this combined with the fact that this bit is useful for filtering
76501ce661fSSeth Forshee 	 * out misidentified bitmap packets, we reject anything with this
76601ce661fSSeth Forshee 	 * bit set.
76701ce661fSSeth Forshee 	 */
76802d04254SHans de Goede 	if (f->is_mp)
76901ce661fSSeth Forshee 		return;
77001ce661fSSeth Forshee 
77102d04254SHans de Goede 	if (!priv->multi_packet && f->first_mp) {
77225bded7cSSeth Forshee 		priv->multi_packet = 1;
77301ce661fSSeth Forshee 		memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
77401ce661fSSeth Forshee 		return;
77501ce661fSSeth Forshee 	}
77601ce661fSSeth Forshee 
77725bded7cSSeth Forshee 	priv->multi_packet = 0;
77825bded7cSSeth Forshee 
77925bded7cSSeth Forshee 	/*
78025bded7cSSeth Forshee 	 * Sometimes the hardware sends a single packet with z = 0
78125bded7cSSeth Forshee 	 * in the middle of a stream. Real releases generate packets
78225bded7cSSeth Forshee 	 * with x, y, and z all zero, so these seem to be flukes.
78325bded7cSSeth Forshee 	 * Ignore them.
78425bded7cSSeth Forshee 	 */
78502d04254SHans de Goede 	if (f->st.x && f->st.y && !f->pressure)
78625bded7cSSeth Forshee 		return;
78725bded7cSSeth Forshee 
78868c21870SHans de Goede 	alps_report_semi_mt_data(psmouse, fingers);
78925bded7cSSeth Forshee 
79034412ba2SPali Rohár 	if ((priv->flags & ALPS_DUALPOINT) &&
79134412ba2SPali Rohár 	    !(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS)) {
79202d04254SHans de Goede 		input_report_key(dev2, BTN_LEFT, f->ts_left);
79302d04254SHans de Goede 		input_report_key(dev2, BTN_RIGHT, f->ts_right);
79402d04254SHans de Goede 		input_report_key(dev2, BTN_MIDDLE, f->ts_middle);
79525bded7cSSeth Forshee 		input_sync(dev2);
79625bded7cSSeth Forshee 	}
79725bded7cSSeth Forshee }
79825bded7cSSeth Forshee 
79925bded7cSSeth Forshee static void alps_process_packet_v3(struct psmouse *psmouse)
80025bded7cSSeth Forshee {
80125bded7cSSeth Forshee 	unsigned char *packet = psmouse->packet;
80225bded7cSSeth Forshee 
80325bded7cSSeth Forshee 	/*
80425bded7cSSeth Forshee 	 * v3 protocol packets come in three types, two representing
80525bded7cSSeth Forshee 	 * touchpad data and one representing trackstick data.
80625bded7cSSeth Forshee 	 * Trackstick packets seem to be distinguished by always
80725bded7cSSeth Forshee 	 * having 0x3f in the last byte. This value has never been
80825bded7cSSeth Forshee 	 * observed in the last byte of either of the other types
80925bded7cSSeth Forshee 	 * of packets.
81025bded7cSSeth Forshee 	 */
81125bded7cSSeth Forshee 	if (packet[5] == 0x3f) {
81225bded7cSSeth Forshee 		alps_process_trackstick_packet_v3(psmouse);
81325bded7cSSeth Forshee 		return;
81425bded7cSSeth Forshee 	}
81525bded7cSSeth Forshee 
816ee65d4b3SYunkang Tang 	alps_process_touchpad_packet_v3_v5(psmouse);
81725bded7cSSeth Forshee }
81825bded7cSSeth Forshee 
81995f75e91SYunkang Tang static void alps_process_packet_v6(struct psmouse *psmouse)
82095f75e91SYunkang Tang {
82195f75e91SYunkang Tang 	struct alps_data *priv = psmouse->private;
82295f75e91SYunkang Tang 	unsigned char *packet = psmouse->packet;
82395f75e91SYunkang Tang 	struct input_dev *dev = psmouse->dev;
82495f75e91SYunkang Tang 	struct input_dev *dev2 = priv->dev2;
82595f75e91SYunkang Tang 	int x, y, z, left, right, middle;
82695f75e91SYunkang Tang 
82795f75e91SYunkang Tang 	/*
82895f75e91SYunkang Tang 	 * We can use Byte5 to distinguish if the packet is from Touchpad
82995f75e91SYunkang Tang 	 * or Trackpoint.
83095f75e91SYunkang Tang 	 * Touchpad:	0 - 0x7E
83195f75e91SYunkang Tang 	 * Trackpoint:	0x7F
83295f75e91SYunkang Tang 	 */
83395f75e91SYunkang Tang 	if (packet[5] == 0x7F) {
83495f75e91SYunkang Tang 		/* It should be a DualPoint when received Trackpoint packet */
83534412ba2SPali Rohár 		if (!(priv->flags & ALPS_DUALPOINT)) {
83634412ba2SPali Rohár 			psmouse_warn(psmouse,
83734412ba2SPali Rohár 				     "Rejected trackstick packet from non DualPoint device");
83895f75e91SYunkang Tang 			return;
83934412ba2SPali Rohár 		}
84095f75e91SYunkang Tang 
84195f75e91SYunkang Tang 		/* Trackpoint packet */
84295f75e91SYunkang Tang 		x = packet[1] | ((packet[3] & 0x20) << 2);
84395f75e91SYunkang Tang 		y = packet[2] | ((packet[3] & 0x40) << 1);
84495f75e91SYunkang Tang 		z = packet[4];
84595f75e91SYunkang Tang 		left = packet[3] & 0x01;
84695f75e91SYunkang Tang 		right = packet[3] & 0x02;
84795f75e91SYunkang Tang 		middle = packet[3] & 0x04;
84895f75e91SYunkang Tang 
84995f75e91SYunkang Tang 		/* To prevent the cursor jump when finger lifted */
85095f75e91SYunkang Tang 		if (x == 0x7F && y == 0x7F && z == 0x7F)
85195f75e91SYunkang Tang 			x = y = z = 0;
85295f75e91SYunkang Tang 
85395f75e91SYunkang Tang 		/* Divide 4 since trackpoint's speed is too fast */
85495f75e91SYunkang Tang 		input_report_rel(dev2, REL_X, (char)x / 4);
85595f75e91SYunkang Tang 		input_report_rel(dev2, REL_Y, -((char)y / 4));
85695f75e91SYunkang Tang 
85795f75e91SYunkang Tang 		input_report_key(dev2, BTN_LEFT, left);
85895f75e91SYunkang Tang 		input_report_key(dev2, BTN_RIGHT, right);
85995f75e91SYunkang Tang 		input_report_key(dev2, BTN_MIDDLE, middle);
86095f75e91SYunkang Tang 
86195f75e91SYunkang Tang 		input_sync(dev2);
86295f75e91SYunkang Tang 		return;
86395f75e91SYunkang Tang 	}
86495f75e91SYunkang Tang 
86595f75e91SYunkang Tang 	/* Touchpad packet */
86695f75e91SYunkang Tang 	x = packet[1] | ((packet[3] & 0x78) << 4);
86795f75e91SYunkang Tang 	y = packet[2] | ((packet[4] & 0x78) << 4);
86895f75e91SYunkang Tang 	z = packet[5];
86995f75e91SYunkang Tang 	left = packet[3] & 0x01;
87095f75e91SYunkang Tang 	right = packet[3] & 0x02;
87195f75e91SYunkang Tang 
87295f75e91SYunkang Tang 	if (z > 30)
87395f75e91SYunkang Tang 		input_report_key(dev, BTN_TOUCH, 1);
87495f75e91SYunkang Tang 	if (z < 25)
87595f75e91SYunkang Tang 		input_report_key(dev, BTN_TOUCH, 0);
87695f75e91SYunkang Tang 
87795f75e91SYunkang Tang 	if (z > 0) {
87895f75e91SYunkang Tang 		input_report_abs(dev, ABS_X, x);
87995f75e91SYunkang Tang 		input_report_abs(dev, ABS_Y, y);
88095f75e91SYunkang Tang 	}
88195f75e91SYunkang Tang 
88295f75e91SYunkang Tang 	input_report_abs(dev, ABS_PRESSURE, z);
88395f75e91SYunkang Tang 	input_report_key(dev, BTN_TOOL_FINGER, z > 0);
88495f75e91SYunkang Tang 
88595f75e91SYunkang Tang 	/* v6 touchpad does not have middle button */
88695f75e91SYunkang Tang 	input_report_key(dev, BTN_LEFT, left);
88795f75e91SYunkang Tang 	input_report_key(dev, BTN_RIGHT, right);
88895f75e91SYunkang Tang 
88995f75e91SYunkang Tang 	input_sync(dev);
89095f75e91SYunkang Tang }
89195f75e91SYunkang Tang 
89225bded7cSSeth Forshee static void alps_process_packet_v4(struct psmouse *psmouse)
89325bded7cSSeth Forshee {
8943b7e09faSGeorge Pantalos 	struct alps_data *priv = psmouse->private;
89525bded7cSSeth Forshee 	unsigned char *packet = psmouse->packet;
89602d04254SHans de Goede 	struct alps_fields *f = &priv->f;
89768c21870SHans de Goede 	int offset;
8983b7e09faSGeorge Pantalos 
8993b7e09faSGeorge Pantalos 	/*
9003b7e09faSGeorge Pantalos 	 * v4 has a 6-byte encoding for bitmap data, but this data is
9013b7e09faSGeorge Pantalos 	 * broken up between 3 normal packets. Use priv->multi_packet to
9023b7e09faSGeorge Pantalos 	 * track our position in the bitmap packet.
9033b7e09faSGeorge Pantalos 	 */
9043b7e09faSGeorge Pantalos 	if (packet[6] & 0x40) {
9053b7e09faSGeorge Pantalos 		/* sync, reset position */
9063b7e09faSGeorge Pantalos 		priv->multi_packet = 0;
9073b7e09faSGeorge Pantalos 	}
9083b7e09faSGeorge Pantalos 
9093b7e09faSGeorge Pantalos 	if (WARN_ON_ONCE(priv->multi_packet > 2))
9103b7e09faSGeorge Pantalos 		return;
9113b7e09faSGeorge Pantalos 
9123b7e09faSGeorge Pantalos 	offset = 2 * priv->multi_packet;
9133b7e09faSGeorge Pantalos 	priv->multi_data[offset] = packet[6];
9143b7e09faSGeorge Pantalos 	priv->multi_data[offset + 1] = packet[7];
9153b7e09faSGeorge Pantalos 
91644b77f38SHans de Goede 	f->left = !!(packet[4] & 0x01);
91744b77f38SHans de Goede 	f->right = !!(packet[4] & 0x02);
91844b77f38SHans de Goede 
91944b77f38SHans de Goede 	f->st.x = ((packet[1] & 0x7f) << 4) | ((packet[3] & 0x30) >> 2) |
92044b77f38SHans de Goede 		  ((packet[0] & 0x30) >> 4);
92144b77f38SHans de Goede 	f->st.y = ((packet[2] & 0x7f) << 4) | (packet[3] & 0x0f);
92244b77f38SHans de Goede 	f->pressure = packet[5] & 0x7f;
92344b77f38SHans de Goede 
9243b7e09faSGeorge Pantalos 	if (++priv->multi_packet > 2) {
9253b7e09faSGeorge Pantalos 		priv->multi_packet = 0;
9263b7e09faSGeorge Pantalos 
92702d04254SHans de Goede 		f->x_map = ((priv->multi_data[2] & 0x1f) << 10) |
9283b7e09faSGeorge Pantalos 			   ((priv->multi_data[3] & 0x60) << 3) |
9293b7e09faSGeorge Pantalos 			   ((priv->multi_data[0] & 0x3f) << 2) |
9303b7e09faSGeorge Pantalos 			   ((priv->multi_data[1] & 0x60) >> 5);
93102d04254SHans de Goede 		f->y_map = ((priv->multi_data[5] & 0x01) << 10) |
9323b7e09faSGeorge Pantalos 			   ((priv->multi_data[3] & 0x1f) << 5) |
9333b7e09faSGeorge Pantalos 			    (priv->multi_data[1] & 0x1f);
9343b7e09faSGeorge Pantalos 
93502d04254SHans de Goede 		f->fingers = alps_process_bitmap(priv, f);
9363b7e09faSGeorge Pantalos 	}
93725bded7cSSeth Forshee 
93868c21870SHans de Goede 	alps_report_semi_mt_data(psmouse, f->fingers);
93925bded7cSSeth Forshee }
94025bded7cSSeth Forshee 
9413808843cSYunkang Tang static bool alps_is_valid_package_v7(struct psmouse *psmouse)
9423808843cSYunkang Tang {
9433808843cSYunkang Tang 	switch (psmouse->pktcnt) {
9443808843cSYunkang Tang 	case 3:
9453808843cSYunkang Tang 		return (psmouse->packet[2] & 0x40) == 0x40;
9463808843cSYunkang Tang 	case 4:
9473808843cSYunkang Tang 		return (psmouse->packet[3] & 0x48) == 0x48;
9483808843cSYunkang Tang 	case 6:
9493808843cSYunkang Tang 		return (psmouse->packet[5] & 0x40) == 0x00;
9503808843cSYunkang Tang 	}
9513808843cSYunkang Tang 	return true;
9523808843cSYunkang Tang }
9533808843cSYunkang Tang 
9543808843cSYunkang Tang static unsigned char alps_get_packet_id_v7(char *byte)
9553808843cSYunkang Tang {
9563808843cSYunkang Tang 	unsigned char packet_id;
9573808843cSYunkang Tang 
9583808843cSYunkang Tang 	if (byte[4] & 0x40)
9593808843cSYunkang Tang 		packet_id = V7_PACKET_ID_TWO;
9603808843cSYunkang Tang 	else if (byte[4] & 0x01)
9613808843cSYunkang Tang 		packet_id = V7_PACKET_ID_MULTI;
9623808843cSYunkang Tang 	else if ((byte[0] & 0x10) && !(byte[4] & 0x43))
9633808843cSYunkang Tang 		packet_id = V7_PACKET_ID_NEW;
9643808843cSYunkang Tang 	else if (byte[1] == 0x00 && byte[4] == 0x00)
9653808843cSYunkang Tang 		packet_id = V7_PACKET_ID_IDLE;
9663808843cSYunkang Tang 	else
9673808843cSYunkang Tang 		packet_id = V7_PACKET_ID_UNKNOWN;
9683808843cSYunkang Tang 
9693808843cSYunkang Tang 	return packet_id;
9703808843cSYunkang Tang }
9713808843cSYunkang Tang 
9723808843cSYunkang Tang static void alps_get_finger_coordinate_v7(struct input_mt_pos *mt,
9733808843cSYunkang Tang 					  unsigned char *pkt,
9743808843cSYunkang Tang 					  unsigned char pkt_id)
9753808843cSYunkang Tang {
9763808843cSYunkang Tang 	mt[0].x = ((pkt[2] & 0x80) << 4);
9773808843cSYunkang Tang 	mt[0].x |= ((pkt[2] & 0x3F) << 5);
9783808843cSYunkang Tang 	mt[0].x |= ((pkt[3] & 0x30) >> 1);
9793808843cSYunkang Tang 	mt[0].x |= (pkt[3] & 0x07);
9803808843cSYunkang Tang 	mt[0].y = (pkt[1] << 3) | (pkt[0] & 0x07);
9813808843cSYunkang Tang 
9823808843cSYunkang Tang 	mt[1].x = ((pkt[3] & 0x80) << 4);
9833808843cSYunkang Tang 	mt[1].x |= ((pkt[4] & 0x80) << 3);
9843808843cSYunkang Tang 	mt[1].x |= ((pkt[4] & 0x3F) << 4);
9853808843cSYunkang Tang 	mt[1].y = ((pkt[5] & 0x80) << 3);
9863808843cSYunkang Tang 	mt[1].y |= ((pkt[5] & 0x3F) << 4);
9873808843cSYunkang Tang 
9883808843cSYunkang Tang 	switch (pkt_id) {
9893808843cSYunkang Tang 	case V7_PACKET_ID_TWO:
9903808843cSYunkang Tang 		mt[1].x &= ~0x000F;
9913808843cSYunkang Tang 		mt[1].y |= 0x000F;
99272eceab7SHans de Goede 		/* Detect false-postive touches where x & y report max value */
99372eceab7SHans de Goede 		if (mt[1].y == 0x7ff && mt[1].x == 0xff0) {
99472eceab7SHans de Goede 			mt[1].x = 0;
99572eceab7SHans de Goede 			/* y gets set to 0 at the end of this function */
99672eceab7SHans de Goede 		}
9973808843cSYunkang Tang 		break;
9983808843cSYunkang Tang 
9993808843cSYunkang Tang 	case V7_PACKET_ID_MULTI:
10003808843cSYunkang Tang 		mt[1].x &= ~0x003F;
10013808843cSYunkang Tang 		mt[1].y &= ~0x0020;
10023808843cSYunkang Tang 		mt[1].y |= ((pkt[4] & 0x02) << 4);
10033808843cSYunkang Tang 		mt[1].y |= 0x001F;
10043808843cSYunkang Tang 		break;
10053808843cSYunkang Tang 
10063808843cSYunkang Tang 	case V7_PACKET_ID_NEW:
10073808843cSYunkang Tang 		mt[1].x &= ~0x003F;
10083808843cSYunkang Tang 		mt[1].x |= (pkt[0] & 0x20);
10093808843cSYunkang Tang 		mt[1].y |= 0x000F;
10103808843cSYunkang Tang 		break;
10113808843cSYunkang Tang 	}
10123808843cSYunkang Tang 
10133808843cSYunkang Tang 	mt[0].y = 0x7FF - mt[0].y;
10143808843cSYunkang Tang 	mt[1].y = 0x7FF - mt[1].y;
10153808843cSYunkang Tang }
10163808843cSYunkang Tang 
10173808843cSYunkang Tang static int alps_get_mt_count(struct input_mt_pos *mt)
10183808843cSYunkang Tang {
10197091c443SHans de Goede 	int i, fingers = 0;
10203808843cSYunkang Tang 
10217091c443SHans de Goede 	for (i = 0; i < MAX_TOUCHES; i++) {
10227091c443SHans de Goede 		if (mt[i].x != 0 || mt[i].y != 0)
10237091c443SHans de Goede 			fingers++;
10247091c443SHans de Goede 	}
10253808843cSYunkang Tang 
10267091c443SHans de Goede 	return fingers;
10273808843cSYunkang Tang }
10283808843cSYunkang Tang 
10293808843cSYunkang Tang static int alps_decode_packet_v7(struct alps_fields *f,
10303808843cSYunkang Tang 				  unsigned char *p,
10313808843cSYunkang Tang 				  struct psmouse *psmouse)
10323808843cSYunkang Tang {
1033d27eb793SHans de Goede 	struct alps_data *priv = psmouse->private;
10343808843cSYunkang Tang 	unsigned char pkt_id;
10353808843cSYunkang Tang 
10363808843cSYunkang Tang 	pkt_id = alps_get_packet_id_v7(p);
10373808843cSYunkang Tang 	if (pkt_id == V7_PACKET_ID_IDLE)
10383808843cSYunkang Tang 		return 0;
10393808843cSYunkang Tang 	if (pkt_id == V7_PACKET_ID_UNKNOWN)
10403808843cSYunkang Tang 		return -1;
10418b238115SHans de Goede 	/*
10428b238115SHans de Goede 	 * NEW packets are send to indicate a discontinuity in the finger
10438b238115SHans de Goede 	 * coordinate reporting. Specifically a finger may have moved from
10448b238115SHans de Goede 	 * slot 0 to 1 or vice versa. INPUT_MT_TRACK takes care of this for
10458b238115SHans de Goede 	 * us.
10468b238115SHans de Goede 	 *
10478b238115SHans de Goede 	 * NEW packets have 3 problems:
10488b238115SHans de Goede 	 * 1) They do not contain middle / right button info (on non clickpads)
10498b238115SHans de Goede 	 *    this can be worked around by preserving the old button state
10508b238115SHans de Goede 	 * 2) They do not contain an accurate fingercount, and they are
10518b238115SHans de Goede 	 *    typically send when the number of fingers changes. We cannot use
10528b238115SHans de Goede 	 *    the old finger count as that may mismatch with the amount of
10538b238115SHans de Goede 	 *    touch coordinates we've available in the NEW packet
10548b238115SHans de Goede 	 * 3) Their x data for the second touch is inaccurate leading to
10558b238115SHans de Goede 	 *    a possible jump of the x coordinate by 16 units when the first
10568b238115SHans de Goede 	 *    non NEW packet comes in
10578b238115SHans de Goede 	 * Since problems 2 & 3 cannot be worked around, just ignore them.
10588b238115SHans de Goede 	 */
10598b238115SHans de Goede 	if (pkt_id == V7_PACKET_ID_NEW)
10608b238115SHans de Goede 		return 1;
10613808843cSYunkang Tang 
10623808843cSYunkang Tang 	alps_get_finger_coordinate_v7(f->mt, p, pkt_id);
10633808843cSYunkang Tang 
10643808843cSYunkang Tang 	if (pkt_id == V7_PACKET_ID_TWO)
10653808843cSYunkang Tang 		f->fingers = alps_get_mt_count(f->mt);
10668b238115SHans de Goede 	else /* pkt_id == V7_PACKET_ID_MULTI */
10673808843cSYunkang Tang 		f->fingers = 3 + (p[5] & 0x03);
10683808843cSYunkang Tang 
1069d27eb793SHans de Goede 	f->left = (p[0] & 0x80) >> 7;
1070d27eb793SHans de Goede 	if (priv->flags & ALPS_BUTTONPAD) {
1071d27eb793SHans de Goede 		if (p[0] & 0x20)
1072d27eb793SHans de Goede 			f->fingers++;
1073d27eb793SHans de Goede 		if (p[0] & 0x10)
1074d27eb793SHans de Goede 			f->fingers++;
1075d27eb793SHans de Goede 	} else {
1076d27eb793SHans de Goede 		f->right = (p[0] & 0x20) >> 5;
1077d27eb793SHans de Goede 		f->middle = (p[0] & 0x10) >> 4;
1078d27eb793SHans de Goede 	}
1079d27eb793SHans de Goede 
10807091c443SHans de Goede 	/* Sometimes a single touch is reported in mt[1] rather then mt[0] */
10817091c443SHans de Goede 	if (f->fingers == 1 && f->mt[0].x == 0 && f->mt[0].y == 0) {
10827091c443SHans de Goede 		f->mt[0].x = f->mt[1].x;
10837091c443SHans de Goede 		f->mt[0].y = f->mt[1].y;
10847091c443SHans de Goede 		f->mt[1].x = 0;
10857091c443SHans de Goede 		f->mt[1].y = 0;
10867091c443SHans de Goede 	}
10877091c443SHans de Goede 
10883808843cSYunkang Tang 	return 0;
10893808843cSYunkang Tang }
10903808843cSYunkang Tang 
10913808843cSYunkang Tang static void alps_process_trackstick_packet_v7(struct psmouse *psmouse)
10923808843cSYunkang Tang {
10933808843cSYunkang Tang 	struct alps_data *priv = psmouse->private;
10943808843cSYunkang Tang 	unsigned char *packet = psmouse->packet;
10953808843cSYunkang Tang 	struct input_dev *dev2 = priv->dev2;
10963808843cSYunkang Tang 	int x, y, z, left, right, middle;
10973808843cSYunkang Tang 
109834412ba2SPali Rohár 	/* It should be a DualPoint when received trackstick packet */
109934412ba2SPali Rohár 	if (!(priv->flags & ALPS_DUALPOINT)) {
110034412ba2SPali Rohár 		psmouse_warn(psmouse,
110134412ba2SPali Rohár 			     "Rejected trackstick packet from non DualPoint device");
110234412ba2SPali Rohár 		return;
110334412ba2SPali Rohár 	}
110434412ba2SPali Rohár 
11053808843cSYunkang Tang 	x = ((packet[2] & 0xbf)) | ((packet[3] & 0x10) << 2);
11063808843cSYunkang Tang 	y = (packet[3] & 0x07) | (packet[4] & 0xb8) |
11073808843cSYunkang Tang 	    ((packet[3] & 0x20) << 1);
11083808843cSYunkang Tang 	z = (packet[5] & 0x3f) | ((packet[3] & 0x80) >> 1);
11093808843cSYunkang Tang 
11103808843cSYunkang Tang 	left = (packet[1] & 0x01);
11113808843cSYunkang Tang 	right = (packet[1] & 0x02) >> 1;
11123808843cSYunkang Tang 	middle = (packet[1] & 0x04) >> 2;
11133808843cSYunkang Tang 
1114088df2ccSHans de Goede 	input_report_rel(dev2, REL_X, (char)x);
1115088df2ccSHans de Goede 	input_report_rel(dev2, REL_Y, -((char)y));
11163808843cSYunkang Tang 
11173808843cSYunkang Tang 	input_report_key(dev2, BTN_LEFT, left);
11183808843cSYunkang Tang 	input_report_key(dev2, BTN_RIGHT, right);
11193808843cSYunkang Tang 	input_report_key(dev2, BTN_MIDDLE, middle);
11203808843cSYunkang Tang 
11213808843cSYunkang Tang 	input_sync(dev2);
11223808843cSYunkang Tang }
11233808843cSYunkang Tang 
11243808843cSYunkang Tang static void alps_process_touchpad_packet_v7(struct psmouse *psmouse)
11253808843cSYunkang Tang {
11263808843cSYunkang Tang 	struct alps_data *priv = psmouse->private;
11273808843cSYunkang Tang 	struct input_dev *dev = psmouse->dev;
11283808843cSYunkang Tang 	struct alps_fields *f = &priv->f;
11293808843cSYunkang Tang 
11303808843cSYunkang Tang 	memset(f, 0, sizeof(*f));
11313808843cSYunkang Tang 
11323808843cSYunkang Tang 	if (priv->decode_fields(f, psmouse->packet, psmouse))
11333808843cSYunkang Tang 		return;
11343808843cSYunkang Tang 
11353808843cSYunkang Tang 	alps_report_mt_data(psmouse, alps_get_mt_count(f->mt));
11363808843cSYunkang Tang 
11373808843cSYunkang Tang 	input_mt_report_finger_count(dev, f->fingers);
11383808843cSYunkang Tang 
11393808843cSYunkang Tang 	input_report_key(dev, BTN_LEFT, f->left);
11403808843cSYunkang Tang 	input_report_key(dev, BTN_RIGHT, f->right);
11413808843cSYunkang Tang 	input_report_key(dev, BTN_MIDDLE, f->middle);
11423808843cSYunkang Tang 
11433808843cSYunkang Tang 	input_sync(dev);
11443808843cSYunkang Tang }
11453808843cSYunkang Tang 
11463808843cSYunkang Tang static void alps_process_packet_v7(struct psmouse *psmouse)
11473808843cSYunkang Tang {
11483808843cSYunkang Tang 	unsigned char *packet = psmouse->packet;
11493808843cSYunkang Tang 
11503808843cSYunkang Tang 	if (packet[0] == 0x48 && (packet[4] & 0x47) == 0x06)
11513808843cSYunkang Tang 		alps_process_trackstick_packet_v7(psmouse);
11523808843cSYunkang Tang 	else
11533808843cSYunkang Tang 		alps_process_touchpad_packet_v7(psmouse);
11543808843cSYunkang Tang }
11553808843cSYunkang Tang 
115607f19e3dSFengguang Wu static unsigned char alps_get_pkt_id_ss4_v2(unsigned char *byte)
11573db5b9f7SMasaki Ota {
11583db5b9f7SMasaki Ota 	unsigned char pkt_id = SS4_PACKET_ID_IDLE;
11593db5b9f7SMasaki Ota 
11604777ac22SBen Gamari 	switch (byte[3] & 0x30) {
11614777ac22SBen Gamari 	case 0x00:
11623db5b9f7SMasaki Ota 		if (byte[0] == 0x18 && byte[1] == 0x10 && byte[2] == 0x00 &&
11634777ac22SBen Gamari 		    (byte[3] & 0x88) == 0x08 && byte[4] == 0x10 &&
11644777ac22SBen Gamari 		    byte[5] == 0x00) {
11653db5b9f7SMasaki Ota 			pkt_id = SS4_PACKET_ID_IDLE;
11663db5b9f7SMasaki Ota 		} else {
11674777ac22SBen Gamari 			pkt_id = SS4_PACKET_ID_ONE;
11684777ac22SBen Gamari 		}
11694777ac22SBen Gamari 		break;
11704777ac22SBen Gamari 	case 0x10:
11714777ac22SBen Gamari 		/* two-finger finger positions */
11724777ac22SBen Gamari 		pkt_id = SS4_PACKET_ID_TWO;
11734777ac22SBen Gamari 		break;
11744777ac22SBen Gamari 	case 0x20:
11754777ac22SBen Gamari 		/* stick pointer */
11764777ac22SBen Gamari 		pkt_id = SS4_PACKET_ID_STICK;
11774777ac22SBen Gamari 		break;
11784777ac22SBen Gamari 	case 0x30:
11794777ac22SBen Gamari 		/* third and fourth finger positions */
11803db5b9f7SMasaki Ota 		pkt_id = SS4_PACKET_ID_MULTI;
11814777ac22SBen Gamari 		break;
11823db5b9f7SMasaki Ota 	}
11833db5b9f7SMasaki Ota 
11843db5b9f7SMasaki Ota 	return pkt_id;
11853db5b9f7SMasaki Ota }
11863db5b9f7SMasaki Ota 
11873db5b9f7SMasaki Ota static int alps_decode_ss4_v2(struct alps_fields *f,
11883db5b9f7SMasaki Ota 			      unsigned char *p, struct psmouse *psmouse)
11893db5b9f7SMasaki Ota {
11903db5b9f7SMasaki Ota 	struct alps_data *priv = psmouse->private;
11913db5b9f7SMasaki Ota 	unsigned char pkt_id;
11923db5b9f7SMasaki Ota 	unsigned int no_data_x, no_data_y;
11933db5b9f7SMasaki Ota 
11943db5b9f7SMasaki Ota 	pkt_id = alps_get_pkt_id_ss4_v2(p);
11953db5b9f7SMasaki Ota 
11963db5b9f7SMasaki Ota 	/* Current packet is 1Finger coordinate packet */
11973db5b9f7SMasaki Ota 	switch (pkt_id) {
11983db5b9f7SMasaki Ota 	case SS4_PACKET_ID_ONE:
11993db5b9f7SMasaki Ota 		f->mt[0].x = SS4_1F_X_V2(p);
12003db5b9f7SMasaki Ota 		f->mt[0].y = SS4_1F_Y_V2(p);
12013db5b9f7SMasaki Ota 		f->pressure = ((SS4_1F_Z_V2(p)) * 2) & 0x7f;
1202a8317763SBen Gamari 		/*
1203a8317763SBen Gamari 		 * When a button is held the device will give us events
1204a8317763SBen Gamari 		 * with x, y, and pressure of 0. This causes annoying jumps
1205a8317763SBen Gamari 		 * if a touch is released while the button is held.
1206a8317763SBen Gamari 		 * Handle this by claiming zero contacts.
1207a8317763SBen Gamari 		 */
1208a8317763SBen Gamari 		f->fingers = f->pressure > 0 ? 1 : 0;
12093db5b9f7SMasaki Ota 		f->first_mp = 0;
12103db5b9f7SMasaki Ota 		f->is_mp = 0;
12113db5b9f7SMasaki Ota 		break;
12123db5b9f7SMasaki Ota 
12133db5b9f7SMasaki Ota 	case SS4_PACKET_ID_TWO:
12143db5b9f7SMasaki Ota 		if (priv->flags & ALPS_BUTTONPAD) {
12153db5b9f7SMasaki Ota 			f->mt[0].x = SS4_BTL_MF_X_V2(p, 0);
12163db5b9f7SMasaki Ota 			f->mt[0].y = SS4_BTL_MF_Y_V2(p, 0);
12173db5b9f7SMasaki Ota 			f->mt[1].x = SS4_BTL_MF_X_V2(p, 1);
12183db5b9f7SMasaki Ota 			f->mt[1].y = SS4_BTL_MF_Y_V2(p, 1);
12193db5b9f7SMasaki Ota 		} else {
12203db5b9f7SMasaki Ota 			f->mt[0].x = SS4_STD_MF_X_V2(p, 0);
12213db5b9f7SMasaki Ota 			f->mt[0].y = SS4_STD_MF_Y_V2(p, 0);
12223db5b9f7SMasaki Ota 			f->mt[1].x = SS4_STD_MF_X_V2(p, 1);
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) {
12393db5b9f7SMasaki Ota 			f->mt[2].x = SS4_BTL_MF_X_V2(p, 0);
12403db5b9f7SMasaki Ota 			f->mt[2].y = SS4_BTL_MF_Y_V2(p, 0);
12413db5b9f7SMasaki Ota 			f->mt[3].x = SS4_BTL_MF_X_V2(p, 1);
12423db5b9f7SMasaki Ota 			f->mt[3].y = SS4_BTL_MF_Y_V2(p, 1);
12433db5b9f7SMasaki Ota 			no_data_x = SS4_MFPACKET_NO_AX_BL;
12443db5b9f7SMasaki Ota 			no_data_y = SS4_MFPACKET_NO_AY_BL;
12453db5b9f7SMasaki Ota 		} else {
12463db5b9f7SMasaki Ota 			f->mt[2].x = SS4_STD_MF_X_V2(p, 0);
12473db5b9f7SMasaki Ota 			f->mt[2].y = SS4_STD_MF_Y_V2(p, 0);
12483db5b9f7SMasaki Ota 			f->mt[3].x = SS4_STD_MF_X_V2(p, 1);
12493db5b9f7SMasaki Ota 			f->mt[3].y = SS4_STD_MF_Y_V2(p, 1);
12503db5b9f7SMasaki Ota 			no_data_x = SS4_MFPACKET_NO_AX;
12513db5b9f7SMasaki Ota 			no_data_y = SS4_MFPACKET_NO_AY;
12523db5b9f7SMasaki Ota 		}
12533db5b9f7SMasaki Ota 
12543db5b9f7SMasaki Ota 		f->first_mp = 0;
12553db5b9f7SMasaki Ota 		f->is_mp = 1;
12563db5b9f7SMasaki Ota 
12573db5b9f7SMasaki Ota 		if (SS4_IS_5F_DETECTED(p)) {
12583db5b9f7SMasaki Ota 			f->fingers = 5;
12593db5b9f7SMasaki Ota 		} else if (f->mt[3].x == no_data_x &&
12603db5b9f7SMasaki Ota 			     f->mt[3].y == no_data_y) {
12613db5b9f7SMasaki Ota 			f->mt[3].x = 0;
12623db5b9f7SMasaki Ota 			f->mt[3].y = 0;
12633db5b9f7SMasaki Ota 			f->fingers = 3;
12643db5b9f7SMasaki Ota 		} else {
12653db5b9f7SMasaki Ota 			f->fingers = 4;
12663db5b9f7SMasaki Ota 		}
12673db5b9f7SMasaki Ota 		break;
12683db5b9f7SMasaki Ota 
12694777ac22SBen Gamari 	case SS4_PACKET_ID_STICK:
12707229c58cSPaul Donohue 		/*
12717229c58cSPaul Donohue 		 * x, y, and pressure are decoded in
12727229c58cSPaul Donohue 		 * alps_process_packet_ss4_v2()
12737229c58cSPaul Donohue 		 */
12747229c58cSPaul Donohue 		f->first_mp = 0;
12757229c58cSPaul Donohue 		f->is_mp = 0;
12764777ac22SBen Gamari 		break;
12774777ac22SBen Gamari 
12783db5b9f7SMasaki Ota 	case SS4_PACKET_ID_IDLE:
12793db5b9f7SMasaki Ota 	default:
12803db5b9f7SMasaki Ota 		memset(f, 0, sizeof(struct alps_fields));
12813db5b9f7SMasaki Ota 		break;
12823db5b9f7SMasaki Ota 	}
12833db5b9f7SMasaki Ota 
12844777ac22SBen Gamari 	/* handle buttons */
12854777ac22SBen Gamari 	if (pkt_id == SS4_PACKET_ID_STICK) {
12864777ac22SBen Gamari 		f->ts_left = !!(SS4_BTN_V2(p) & 0x01);
12874777ac22SBen Gamari 		if (!(priv->flags & ALPS_BUTTONPAD)) {
12884777ac22SBen Gamari 			f->ts_right = !!(SS4_BTN_V2(p) & 0x02);
12894777ac22SBen Gamari 			f->ts_middle = !!(SS4_BTN_V2(p) & 0x04);
12904777ac22SBen Gamari 		}
12914777ac22SBen Gamari 	} else {
12923db5b9f7SMasaki Ota 		f->left = !!(SS4_BTN_V2(p) & 0x01);
12933db5b9f7SMasaki Ota 		if (!(priv->flags & ALPS_BUTTONPAD)) {
12943db5b9f7SMasaki Ota 			f->right = !!(SS4_BTN_V2(p) & 0x02);
12953db5b9f7SMasaki Ota 			f->middle = !!(SS4_BTN_V2(p) & 0x04);
12963db5b9f7SMasaki Ota 		}
12974777ac22SBen Gamari 	}
12983db5b9f7SMasaki Ota 
12993db5b9f7SMasaki Ota 	return 0;
13003db5b9f7SMasaki Ota }
13013db5b9f7SMasaki Ota 
13023db5b9f7SMasaki Ota static void alps_process_packet_ss4_v2(struct psmouse *psmouse)
13033db5b9f7SMasaki Ota {
13043db5b9f7SMasaki Ota 	struct alps_data *priv = psmouse->private;
13053db5b9f7SMasaki Ota 	unsigned char *packet = psmouse->packet;
13063db5b9f7SMasaki Ota 	struct input_dev *dev = psmouse->dev;
13074777ac22SBen Gamari 	struct input_dev *dev2 = priv->dev2;
13083db5b9f7SMasaki Ota 	struct alps_fields *f = &priv->f;
13097229c58cSPaul Donohue 	int x, y, pressure;
13103db5b9f7SMasaki Ota 
13113db5b9f7SMasaki Ota 	memset(f, 0, sizeof(struct alps_fields));
13123db5b9f7SMasaki Ota 	priv->decode_fields(f, packet, psmouse);
13133db5b9f7SMasaki Ota 	if (priv->multi_packet) {
13143db5b9f7SMasaki Ota 		/*
13153db5b9f7SMasaki Ota 		 * Sometimes the first packet will indicate a multi-packet
13163db5b9f7SMasaki Ota 		 * sequence, but sometimes the next multi-packet would not
13173db5b9f7SMasaki Ota 		 * come. Check for this, and when it happens process the
13183db5b9f7SMasaki Ota 		 * position packet as usual.
13193db5b9f7SMasaki Ota 		 */
13203db5b9f7SMasaki Ota 		if (f->is_mp) {
13213db5b9f7SMasaki Ota 			/* Now process the 1st packet */
13223db5b9f7SMasaki Ota 			priv->decode_fields(f, priv->multi_data, psmouse);
13233db5b9f7SMasaki Ota 		} else {
13243db5b9f7SMasaki Ota 			priv->multi_packet = 0;
13253db5b9f7SMasaki Ota 		}
13263db5b9f7SMasaki Ota 	}
13273db5b9f7SMasaki Ota 
13283db5b9f7SMasaki Ota 	/*
13293db5b9f7SMasaki Ota 	 * "f.is_mp" would always be '0' after merging the 1st and 2nd packet.
13303db5b9f7SMasaki Ota 	 * When it is set, it means 2nd packet comes without 1st packet come.
13313db5b9f7SMasaki Ota 	 */
13323db5b9f7SMasaki Ota 	if (f->is_mp)
13333db5b9f7SMasaki Ota 		return;
13343db5b9f7SMasaki Ota 
13353db5b9f7SMasaki Ota 	/* Save the first packet */
13363db5b9f7SMasaki Ota 	if (!priv->multi_packet && f->first_mp) {
13373db5b9f7SMasaki Ota 		priv->multi_packet = 1;
13383db5b9f7SMasaki Ota 		memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
13393db5b9f7SMasaki Ota 		return;
13403db5b9f7SMasaki Ota 	}
13413db5b9f7SMasaki Ota 
13423db5b9f7SMasaki Ota 	priv->multi_packet = 0;
13433db5b9f7SMasaki Ota 
1344864db929SPaul Donohue 	/* Report trackstick */
1345864db929SPaul Donohue 	if (alps_get_pkt_id_ss4_v2(packet) == SS4_PACKET_ID_STICK) {
13467229c58cSPaul Donohue 		if (!(priv->flags & ALPS_DUALPOINT)) {
13477229c58cSPaul Donohue 			psmouse_warn(psmouse,
13487229c58cSPaul Donohue 				     "Rejected trackstick packet from non DualPoint device");
13497229c58cSPaul Donohue 			return;
13507229c58cSPaul Donohue 		}
13517229c58cSPaul Donohue 
13527229c58cSPaul Donohue 		x = (s8)(((packet[0] & 1) << 7) | (packet[1] & 0x7f));
13537229c58cSPaul Donohue 		y = (s8)(((packet[3] & 1) << 7) | (packet[2] & 0x7f));
13547229c58cSPaul Donohue 		pressure = (s8)(packet[4] & 0x7f);
13557229c58cSPaul Donohue 
13567229c58cSPaul Donohue 		input_report_rel(dev2, REL_X, x);
13577229c58cSPaul Donohue 		input_report_rel(dev2, REL_Y, -y);
13587229c58cSPaul Donohue 		input_report_abs(dev2, ABS_PRESSURE, pressure);
13597229c58cSPaul Donohue 
1360864db929SPaul Donohue 		input_report_key(dev2, BTN_LEFT, f->ts_left);
1361864db929SPaul Donohue 		input_report_key(dev2, BTN_RIGHT, f->ts_right);
1362864db929SPaul Donohue 		input_report_key(dev2, BTN_MIDDLE, f->ts_middle);
13637229c58cSPaul Donohue 
1364864db929SPaul Donohue 		input_sync(dev2);
1365864db929SPaul Donohue 		return;
1366864db929SPaul Donohue 	}
1367864db929SPaul Donohue 
1368864db929SPaul Donohue 	/* Report touchpad */
13693db5b9f7SMasaki Ota 	alps_report_mt_data(psmouse, (f->fingers <= 4) ? f->fingers : 4);
13703db5b9f7SMasaki Ota 
13713db5b9f7SMasaki Ota 	input_mt_report_finger_count(dev, f->fingers);
13723db5b9f7SMasaki Ota 
13733db5b9f7SMasaki Ota 	input_report_key(dev, BTN_LEFT, f->left);
13743db5b9f7SMasaki Ota 	input_report_key(dev, BTN_RIGHT, f->right);
13753db5b9f7SMasaki Ota 	input_report_key(dev, BTN_MIDDLE, f->middle);
13763db5b9f7SMasaki Ota 
13773db5b9f7SMasaki Ota 	input_report_abs(dev, ABS_PRESSURE, f->pressure);
13783db5b9f7SMasaki Ota 	input_sync(dev);
13793db5b9f7SMasaki Ota }
13803db5b9f7SMasaki Ota 
13813db5b9f7SMasaki Ota static bool alps_is_valid_package_ss4_v2(struct psmouse *psmouse)
13823db5b9f7SMasaki Ota {
13833db5b9f7SMasaki Ota 	if (psmouse->pktcnt == 4 && ((psmouse->packet[3] & 0x08) != 0x08))
13843db5b9f7SMasaki Ota 		return false;
13853db5b9f7SMasaki Ota 	if (psmouse->pktcnt == 6 && ((psmouse->packet[5] & 0x10) != 0x0))
13863db5b9f7SMasaki Ota 		return false;
13873db5b9f7SMasaki Ota 	return true;
13883db5b9f7SMasaki Ota }
13893db5b9f7SMasaki Ota 
139004aae283SPali Rohár static DEFINE_MUTEX(alps_mutex);
139104aae283SPali Rohár 
139204aae283SPali Rohár static void alps_register_bare_ps2_mouse(struct work_struct *work)
139304aae283SPali Rohár {
139404aae283SPali Rohár 	struct alps_data *priv =
139504aae283SPali Rohár 		container_of(work, struct alps_data, dev3_register_work.work);
139604aae283SPali Rohár 	struct psmouse *psmouse = priv->psmouse;
139704aae283SPali Rohár 	struct input_dev *dev3;
139804aae283SPali Rohár 	int error = 0;
139904aae283SPali Rohár 
140004aae283SPali Rohár 	mutex_lock(&alps_mutex);
140104aae283SPali Rohár 
140204aae283SPali Rohár 	if (priv->dev3)
140304aae283SPali Rohár 		goto out;
140404aae283SPali Rohár 
140504aae283SPali Rohár 	dev3 = input_allocate_device();
140604aae283SPali Rohár 	if (!dev3) {
140704aae283SPali Rohár 		psmouse_err(psmouse, "failed to allocate secondary device\n");
140804aae283SPali Rohár 		error = -ENOMEM;
140904aae283SPali Rohár 		goto out;
141004aae283SPali Rohár 	}
141104aae283SPali Rohár 
141204aae283SPali Rohár 	snprintf(priv->phys3, sizeof(priv->phys3), "%s/%s",
141304aae283SPali Rohár 		 psmouse->ps2dev.serio->phys,
141404aae283SPali Rohár 		 (priv->dev2 ? "input2" : "input1"));
141504aae283SPali Rohár 	dev3->phys = priv->phys3;
141604aae283SPali Rohár 
141704aae283SPali Rohár 	/*
141804aae283SPali Rohár 	 * format of input device name is: "protocol vendor name"
141904aae283SPali Rohár 	 * see function psmouse_switch_protocol() in psmouse-base.c
142004aae283SPali Rohár 	 */
142104aae283SPali Rohár 	dev3->name = "PS/2 ALPS Mouse";
142204aae283SPali Rohár 
142304aae283SPali Rohár 	dev3->id.bustype = BUS_I8042;
142404aae283SPali Rohár 	dev3->id.vendor  = 0x0002;
142504aae283SPali Rohár 	dev3->id.product = PSMOUSE_PS2;
142604aae283SPali Rohár 	dev3->id.version = 0x0000;
142704aae283SPali Rohár 	dev3->dev.parent = &psmouse->ps2dev.serio->dev;
142804aae283SPali Rohár 
142904aae283SPali Rohár 	input_set_capability(dev3, EV_REL, REL_X);
143004aae283SPali Rohár 	input_set_capability(dev3, EV_REL, REL_Y);
143104aae283SPali Rohár 	input_set_capability(dev3, EV_KEY, BTN_LEFT);
143204aae283SPali Rohár 	input_set_capability(dev3, EV_KEY, BTN_RIGHT);
143304aae283SPali Rohár 	input_set_capability(dev3, EV_KEY, BTN_MIDDLE);
143404aae283SPali Rohár 
143504aae283SPali Rohár 	__set_bit(INPUT_PROP_POINTER, dev3->propbit);
143604aae283SPali Rohár 
143704aae283SPali Rohár 	error = input_register_device(dev3);
143804aae283SPali Rohár 	if (error) {
143904aae283SPali Rohár 		psmouse_err(psmouse,
144004aae283SPali Rohár 			    "failed to register secondary device: %d\n",
144104aae283SPali Rohár 			    error);
144204aae283SPali Rohár 		input_free_device(dev3);
144304aae283SPali Rohár 		goto out;
144404aae283SPali Rohár 	}
144504aae283SPali Rohár 
144604aae283SPali Rohár 	priv->dev3 = dev3;
144704aae283SPali Rohár 
144804aae283SPali Rohár out:
144904aae283SPali Rohár 	/*
145004aae283SPali Rohár 	 * Save the error code so that we can detect that we
145104aae283SPali Rohár 	 * already tried to create the device.
145204aae283SPali Rohár 	 */
145304aae283SPali Rohár 	if (error)
145404aae283SPali Rohár 		priv->dev3 = ERR_PTR(error);
145504aae283SPali Rohár 
145604aae283SPali Rohár 	mutex_unlock(&alps_mutex);
145704aae283SPali Rohár }
145804aae283SPali Rohár 
145959c30afbSHans de Goede static void alps_report_bare_ps2_packet(struct psmouse *psmouse,
14601d9f2626SSebastian Kapfer 					unsigned char packet[],
14611d9f2626SSebastian Kapfer 					bool report_buttons)
14621d9f2626SSebastian Kapfer {
146359c30afbSHans de Goede 	struct alps_data *priv = psmouse->private;
14646bcca19fSHans de Goede 	struct input_dev *dev, *dev2 = NULL;
146559c30afbSHans de Goede 
1466e3a79212SHans de Goede 	/* Figure out which device to use to report the bare packet */
1467e3a79212SHans de Goede 	if (priv->proto_version == ALPS_PROTO_V2 &&
1468e3a79212SHans de Goede 	    (priv->flags & ALPS_DUALPOINT)) {
1469e3a79212SHans de Goede 		/* On V2 devices the DualPoint Stick reports bare packets */
1470e3a79212SHans de Goede 		dev = priv->dev2;
14716bcca19fSHans de Goede 		dev2 = psmouse->dev;
1472e3a79212SHans de Goede 	} else if (unlikely(IS_ERR_OR_NULL(priv->dev3))) {
147359c30afbSHans de Goede 		/* Register dev3 mouse if we received PS/2 packet first time */
147459c30afbSHans de Goede 		if (!IS_ERR(priv->dev3))
147559c30afbSHans de Goede 			psmouse_queue_work(psmouse, &priv->dev3_register_work,
147659c30afbSHans de Goede 					   0);
147759c30afbSHans de Goede 		return;
147859c30afbSHans de Goede 	} else {
147959c30afbSHans de Goede 		dev = priv->dev3;
148059c30afbSHans de Goede 	}
148159c30afbSHans de Goede 
14821d9f2626SSebastian Kapfer 	if (report_buttons)
14836bcca19fSHans de Goede 		alps_report_buttons(dev, dev2,
14841d9f2626SSebastian Kapfer 				packet[0] & 1, packet[0] & 2, packet[0] & 4);
14851d9f2626SSebastian Kapfer 
148604aae283SPali Rohár 	input_report_rel(dev, REL_X,
14871d9f2626SSebastian Kapfer 		packet[1] ? packet[1] - ((packet[0] << 4) & 0x100) : 0);
148804aae283SPali Rohár 	input_report_rel(dev, REL_Y,
14891d9f2626SSebastian Kapfer 		packet[2] ? ((packet[0] << 3) & 0x100) - packet[2] : 0);
14901d9f2626SSebastian Kapfer 
149104aae283SPali Rohár 	input_sync(dev);
14921d9f2626SSebastian Kapfer }
14931d9f2626SSebastian Kapfer 
14941d9f2626SSebastian Kapfer static psmouse_ret_t alps_handle_interleaved_ps2(struct psmouse *psmouse)
14951da177e4SLinus Torvalds {
14961da177e4SLinus Torvalds 	struct alps_data *priv = psmouse->private;
14971da177e4SLinus Torvalds 
14981d9f2626SSebastian Kapfer 	if (psmouse->pktcnt < 6)
14991d9f2626SSebastian Kapfer 		return PSMOUSE_GOOD_DATA;
15001d9f2626SSebastian Kapfer 
15011d9f2626SSebastian Kapfer 	if (psmouse->pktcnt == 6) {
15021d9f2626SSebastian Kapfer 		/*
15031d9f2626SSebastian Kapfer 		 * Start a timer to flush the packet if it ends up last
15041d9f2626SSebastian Kapfer 		 * 6-byte packet in the stream. Timer needs to fire
15051d9f2626SSebastian Kapfer 		 * psmouse core times out itself. 20 ms should be enough
15061d9f2626SSebastian Kapfer 		 * to decide if we are getting more data or not.
15071d9f2626SSebastian Kapfer 		 */
15081d9f2626SSebastian Kapfer 		mod_timer(&priv->timer, jiffies + msecs_to_jiffies(20));
15091d9f2626SSebastian Kapfer 		return PSMOUSE_GOOD_DATA;
15101d9f2626SSebastian Kapfer 	}
15111d9f2626SSebastian Kapfer 
15121d9f2626SSebastian Kapfer 	del_timer(&priv->timer);
15131d9f2626SSebastian Kapfer 
15141d9f2626SSebastian Kapfer 	if (psmouse->packet[6] & 0x80) {
15151d9f2626SSebastian Kapfer 
15161d9f2626SSebastian Kapfer 		/*
15171d9f2626SSebastian Kapfer 		 * Highest bit is set - that means we either had
15181d9f2626SSebastian Kapfer 		 * complete ALPS packet and this is start of the
15191d9f2626SSebastian Kapfer 		 * next packet or we got garbage.
15201d9f2626SSebastian Kapfer 		 */
15211d9f2626SSebastian Kapfer 
15221d9f2626SSebastian Kapfer 		if (((psmouse->packet[3] |
15231d9f2626SSebastian Kapfer 		      psmouse->packet[4] |
15241d9f2626SSebastian Kapfer 		      psmouse->packet[5]) & 0x80) ||
152599df65e7SKevin Cernekee 		    (!alps_is_valid_first_byte(priv, psmouse->packet[6]))) {
1526b5d21704SDmitry Torokhov 			psmouse_dbg(psmouse,
15273b112923SAndy Shevchenko 				    "refusing packet %4ph (suspected interleaved ps/2)\n",
15283b112923SAndy Shevchenko 				    psmouse->packet + 3);
15291d9f2626SSebastian Kapfer 			return PSMOUSE_BAD_DATA;
15301d9f2626SSebastian Kapfer 		}
15311d9f2626SSebastian Kapfer 
153224af5cb9SKevin Cernekee 		priv->process_packet(psmouse);
15331d9f2626SSebastian Kapfer 
15341d9f2626SSebastian Kapfer 		/* Continue with the next packet */
15351d9f2626SSebastian Kapfer 		psmouse->packet[0] = psmouse->packet[6];
15361d9f2626SSebastian Kapfer 		psmouse->pktcnt = 1;
15371d9f2626SSebastian Kapfer 
15381d9f2626SSebastian Kapfer 	} else {
15391d9f2626SSebastian Kapfer 
15401d9f2626SSebastian Kapfer 		/*
15411d9f2626SSebastian Kapfer 		 * High bit is 0 - that means that we indeed got a PS/2
15421d9f2626SSebastian Kapfer 		 * packet in the middle of ALPS packet.
15431d9f2626SSebastian Kapfer 		 *
15441d9f2626SSebastian Kapfer 		 * There is also possibility that we got 6-byte ALPS
15451d9f2626SSebastian Kapfer 		 * packet followed  by 3-byte packet from trackpoint. We
15461d9f2626SSebastian Kapfer 		 * can not distinguish between these 2 scenarios but
1547b5d21704SDmitry Torokhov 		 * because the latter is unlikely to happen in course of
15481d9f2626SSebastian Kapfer 		 * normal operation (user would need to press all
15491d9f2626SSebastian Kapfer 		 * buttons on the pad and start moving trackpoint
15501d9f2626SSebastian Kapfer 		 * without touching the pad surface) we assume former.
15511d9f2626SSebastian Kapfer 		 * Even if we are wrong the wost thing that would happen
15521d9f2626SSebastian Kapfer 		 * the cursor would jump but we should not get protocol
1553b5d21704SDmitry Torokhov 		 * de-synchronization.
15541d9f2626SSebastian Kapfer 		 */
15551d9f2626SSebastian Kapfer 
155659c30afbSHans de Goede 		alps_report_bare_ps2_packet(psmouse, &psmouse->packet[3],
155759c30afbSHans de Goede 					    false);
15581d9f2626SSebastian Kapfer 
15591d9f2626SSebastian Kapfer 		/*
15601d9f2626SSebastian Kapfer 		 * Continue with the standard ALPS protocol handling,
15611d9f2626SSebastian Kapfer 		 * but make sure we won't process it as an interleaved
15621d9f2626SSebastian Kapfer 		 * packet again, which may happen if all buttons are
15631d9f2626SSebastian Kapfer 		 * pressed. To avoid this let's reset the 4th bit which
15641d9f2626SSebastian Kapfer 		 * is normally 1.
15651d9f2626SSebastian Kapfer 		 */
15661d9f2626SSebastian Kapfer 		psmouse->packet[3] = psmouse->packet[6] & 0xf7;
15671d9f2626SSebastian Kapfer 		psmouse->pktcnt = 4;
15681d9f2626SSebastian Kapfer 	}
15691d9f2626SSebastian Kapfer 
15701d9f2626SSebastian Kapfer 	return PSMOUSE_GOOD_DATA;
15711d9f2626SSebastian Kapfer }
15721d9f2626SSebastian Kapfer 
15731d9f2626SSebastian Kapfer static void alps_flush_packet(unsigned long data)
15741d9f2626SSebastian Kapfer {
15751d9f2626SSebastian Kapfer 	struct psmouse *psmouse = (struct psmouse *)data;
157624af5cb9SKevin Cernekee 	struct alps_data *priv = psmouse->private;
15771d9f2626SSebastian Kapfer 
15781d9f2626SSebastian Kapfer 	serio_pause_rx(psmouse->ps2dev.serio);
15791d9f2626SSebastian Kapfer 
1580b46615feSSeth Forshee 	if (psmouse->pktcnt == psmouse->pktsize) {
15811d9f2626SSebastian Kapfer 
15821d9f2626SSebastian Kapfer 		/*
15831d9f2626SSebastian Kapfer 		 * We did not any more data in reasonable amount of time.
15841d9f2626SSebastian Kapfer 		 * Validate the last 3 bytes and process as a standard
15851d9f2626SSebastian Kapfer 		 * ALPS packet.
15861d9f2626SSebastian Kapfer 		 */
15871d9f2626SSebastian Kapfer 		if ((psmouse->packet[3] |
15881d9f2626SSebastian Kapfer 		     psmouse->packet[4] |
15891d9f2626SSebastian Kapfer 		     psmouse->packet[5]) & 0x80) {
1590b5d21704SDmitry Torokhov 			psmouse_dbg(psmouse,
15913b112923SAndy Shevchenko 				    "refusing packet %3ph (suspected interleaved ps/2)\n",
15923b112923SAndy Shevchenko 				    psmouse->packet + 3);
15931d9f2626SSebastian Kapfer 		} else {
159424af5cb9SKevin Cernekee 			priv->process_packet(psmouse);
15951d9f2626SSebastian Kapfer 		}
15961d9f2626SSebastian Kapfer 		psmouse->pktcnt = 0;
15971d9f2626SSebastian Kapfer 	}
15981d9f2626SSebastian Kapfer 
15991d9f2626SSebastian Kapfer 	serio_continue_rx(psmouse->ps2dev.serio);
16001d9f2626SSebastian Kapfer }
16011d9f2626SSebastian Kapfer 
16021d9f2626SSebastian Kapfer static psmouse_ret_t alps_process_byte(struct psmouse *psmouse)
16031d9f2626SSebastian Kapfer {
16041d9f2626SSebastian Kapfer 	struct alps_data *priv = psmouse->private;
16051d9f2626SSebastian Kapfer 
16064ab8f7f3SPali Rohár 	/*
16074ab8f7f3SPali Rohár 	 * Check if we are dealing with a bare PS/2 packet, presumably from
16084ab8f7f3SPali Rohár 	 * a device connected to the external PS/2 port. Because bare PS/2
16094ab8f7f3SPali Rohár 	 * protocol does not have enough constant bits to self-synchronize
16104ab8f7f3SPali Rohár 	 * properly we only do this if the device is fully synchronized.
16113db5b9f7SMasaki Ota 	 * Can not distinguish V8's first byte from PS/2 packet's
16124ab8f7f3SPali Rohár 	 */
16133db5b9f7SMasaki Ota 	if (priv->proto_version != ALPS_PROTO_V8 &&
16143db5b9f7SMasaki Ota 	    !psmouse->out_of_sync_cnt &&
16153db5b9f7SMasaki Ota 	    (psmouse->packet[0] & 0xc8) == 0x08) {
16163db5b9f7SMasaki Ota 
16171da177e4SLinus Torvalds 		if (psmouse->pktcnt == 3) {
161859c30afbSHans de Goede 			alps_report_bare_ps2_packet(psmouse, psmouse->packet,
16191d9f2626SSebastian Kapfer 						    true);
16201da177e4SLinus Torvalds 			return PSMOUSE_FULL_PACKET;
16211da177e4SLinus Torvalds 		}
16221da177e4SLinus Torvalds 		return PSMOUSE_GOOD_DATA;
16231da177e4SLinus Torvalds 	}
16241da177e4SLinus Torvalds 
16251d9f2626SSebastian Kapfer 	/* Check for PS/2 packet stuffed in the middle of ALPS packet. */
16261d9f2626SSebastian Kapfer 
162799df65e7SKevin Cernekee 	if ((priv->flags & ALPS_PS2_INTERLEAVED) &&
16281d9f2626SSebastian Kapfer 	    psmouse->pktcnt >= 4 && (psmouse->packet[3] & 0x0f) == 0x0f) {
16291d9f2626SSebastian Kapfer 		return alps_handle_interleaved_ps2(psmouse);
16301d9f2626SSebastian Kapfer 	}
16311d9f2626SSebastian Kapfer 
163299df65e7SKevin Cernekee 	if (!alps_is_valid_first_byte(priv, psmouse->packet[0])) {
1633b5d21704SDmitry Torokhov 		psmouse_dbg(psmouse,
1634b5d21704SDmitry Torokhov 			    "refusing packet[0] = %x (mask0 = %x, byte0 = %x)\n",
163599df65e7SKevin Cernekee 			    psmouse->packet[0], priv->mask0, priv->byte0);
16361da177e4SLinus Torvalds 		return PSMOUSE_BAD_DATA;
16371d9f2626SSebastian Kapfer 	}
16381da177e4SLinus Torvalds 
1639b46615feSSeth Forshee 	/* Bytes 2 - pktsize should have 0 in the highest bit */
1640a7ef82aeSPali Rohár 	if (priv->proto_version < ALPS_PROTO_V5 &&
164175af9e56SDave Turvene 	    psmouse->pktcnt >= 2 && psmouse->pktcnt <= psmouse->pktsize &&
16421d9f2626SSebastian Kapfer 	    (psmouse->packet[psmouse->pktcnt - 1] & 0x80)) {
1643b5d21704SDmitry Torokhov 		psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
1644b5d21704SDmitry Torokhov 			    psmouse->pktcnt - 1,
1645b5d21704SDmitry Torokhov 			    psmouse->packet[psmouse->pktcnt - 1]);
1646a7ef82aeSPali Rohár 
1647fb2dd7a6SDmitry Torokhov 		if (priv->proto_version == ALPS_PROTO_V3_RUSHMORE &&
1648a7ef82aeSPali Rohár 		    psmouse->pktcnt == psmouse->pktsize) {
1649a7ef82aeSPali Rohár 			/*
1650a7ef82aeSPali Rohár 			 * Some Dell boxes, such as Latitude E6440 or E7440
1651a7ef82aeSPali Rohár 			 * with closed lid, quite often smash last byte of
1652a7ef82aeSPali Rohár 			 * otherwise valid packet with 0xff. Given that the
1653a7ef82aeSPali Rohár 			 * next packet is very likely to be valid let's
1654a7ef82aeSPali Rohár 			 * report PSMOUSE_FULL_PACKET but not process data,
1655a7ef82aeSPali Rohár 			 * rather than reporting PSMOUSE_BAD_DATA and
1656a7ef82aeSPali Rohár 			 * filling the logs.
1657a7ef82aeSPali Rohár 			 */
1658a7ef82aeSPali Rohár 			return PSMOUSE_FULL_PACKET;
1659a7ef82aeSPali Rohár 		}
1660a7ef82aeSPali Rohár 
16611da177e4SLinus Torvalds 		return PSMOUSE_BAD_DATA;
16621d9f2626SSebastian Kapfer 	}
16631da177e4SLinus Torvalds 
16643db5b9f7SMasaki Ota 	if ((priv->proto_version == ALPS_PROTO_V7 &&
16653db5b9f7SMasaki Ota 			!alps_is_valid_package_v7(psmouse)) ||
16663db5b9f7SMasaki Ota 	    (priv->proto_version == ALPS_PROTO_V8 &&
16673db5b9f7SMasaki Ota 			!alps_is_valid_package_ss4_v2(psmouse))) {
16683808843cSYunkang Tang 		psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
16693808843cSYunkang Tang 			    psmouse->pktcnt - 1,
16703808843cSYunkang Tang 			    psmouse->packet[psmouse->pktcnt - 1]);
16713808843cSYunkang Tang 		return PSMOUSE_BAD_DATA;
16723808843cSYunkang Tang 	}
16733808843cSYunkang Tang 
1674b46615feSSeth Forshee 	if (psmouse->pktcnt == psmouse->pktsize) {
167524af5cb9SKevin Cernekee 		priv->process_packet(psmouse);
16761da177e4SLinus Torvalds 		return PSMOUSE_FULL_PACKET;
16771da177e4SLinus Torvalds 	}
16781da177e4SLinus Torvalds 
16791da177e4SLinus Torvalds 	return PSMOUSE_GOOD_DATA;
16801da177e4SLinus Torvalds }
16811da177e4SLinus Torvalds 
168225bded7cSSeth Forshee static int alps_command_mode_send_nibble(struct psmouse *psmouse, int nibble)
168325bded7cSSeth Forshee {
168425bded7cSSeth Forshee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
168525bded7cSSeth Forshee 	struct alps_data *priv = psmouse->private;
168625bded7cSSeth Forshee 	int command;
168725bded7cSSeth Forshee 	unsigned char *param;
168825bded7cSSeth Forshee 	unsigned char dummy[4];
168925bded7cSSeth Forshee 
169025bded7cSSeth Forshee 	BUG_ON(nibble > 0xf);
169125bded7cSSeth Forshee 
169225bded7cSSeth Forshee 	command = priv->nibble_commands[nibble].command;
169325bded7cSSeth Forshee 	param = (command & 0x0f00) ?
169425bded7cSSeth Forshee 		dummy : (unsigned char *)&priv->nibble_commands[nibble].data;
169525bded7cSSeth Forshee 
169625bded7cSSeth Forshee 	if (ps2_command(ps2dev, param, command))
169725bded7cSSeth Forshee 		return -1;
169825bded7cSSeth Forshee 
169925bded7cSSeth Forshee 	return 0;
170025bded7cSSeth Forshee }
170125bded7cSSeth Forshee 
170225bded7cSSeth Forshee static int alps_command_mode_set_addr(struct psmouse *psmouse, int addr)
170325bded7cSSeth Forshee {
170425bded7cSSeth Forshee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
170525bded7cSSeth Forshee 	struct alps_data *priv = psmouse->private;
170625bded7cSSeth Forshee 	int i, nibble;
170725bded7cSSeth Forshee 
170825bded7cSSeth Forshee 	if (ps2_command(ps2dev, NULL, priv->addr_command))
170925bded7cSSeth Forshee 		return -1;
171025bded7cSSeth Forshee 
171125bded7cSSeth Forshee 	for (i = 12; i >= 0; i -= 4) {
171225bded7cSSeth Forshee 		nibble = (addr >> i) & 0xf;
171325bded7cSSeth Forshee 		if (alps_command_mode_send_nibble(psmouse, nibble))
171425bded7cSSeth Forshee 			return -1;
171525bded7cSSeth Forshee 	}
171625bded7cSSeth Forshee 
171725bded7cSSeth Forshee 	return 0;
171825bded7cSSeth Forshee }
171925bded7cSSeth Forshee 
172025bded7cSSeth Forshee static int __alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
172125bded7cSSeth Forshee {
172225bded7cSSeth Forshee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
172325bded7cSSeth Forshee 	unsigned char param[4];
172425bded7cSSeth Forshee 
172525bded7cSSeth Forshee 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
172625bded7cSSeth Forshee 		return -1;
172725bded7cSSeth Forshee 
172825bded7cSSeth Forshee 	/*
172925bded7cSSeth Forshee 	 * The address being read is returned in the first two bytes
173025bded7cSSeth Forshee 	 * of the result. Check that this address matches the expected
173125bded7cSSeth Forshee 	 * address.
173225bded7cSSeth Forshee 	 */
173325bded7cSSeth Forshee 	if (addr != ((param[0] << 8) | param[1]))
173425bded7cSSeth Forshee 		return -1;
173525bded7cSSeth Forshee 
173625bded7cSSeth Forshee 	return param[2];
173725bded7cSSeth Forshee }
173825bded7cSSeth Forshee 
173925bded7cSSeth Forshee static int alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
174025bded7cSSeth Forshee {
174125bded7cSSeth Forshee 	if (alps_command_mode_set_addr(psmouse, addr))
174225bded7cSSeth Forshee 		return -1;
174325bded7cSSeth Forshee 	return __alps_command_mode_read_reg(psmouse, addr);
174425bded7cSSeth Forshee }
174525bded7cSSeth Forshee 
174625bded7cSSeth Forshee static int __alps_command_mode_write_reg(struct psmouse *psmouse, u8 value)
174725bded7cSSeth Forshee {
174825bded7cSSeth Forshee 	if (alps_command_mode_send_nibble(psmouse, (value >> 4) & 0xf))
174925bded7cSSeth Forshee 		return -1;
175025bded7cSSeth Forshee 	if (alps_command_mode_send_nibble(psmouse, value & 0xf))
175125bded7cSSeth Forshee 		return -1;
175225bded7cSSeth Forshee 	return 0;
175325bded7cSSeth Forshee }
175425bded7cSSeth Forshee 
175525bded7cSSeth Forshee static int alps_command_mode_write_reg(struct psmouse *psmouse, int addr,
175625bded7cSSeth Forshee 				       u8 value)
175725bded7cSSeth Forshee {
175825bded7cSSeth Forshee 	if (alps_command_mode_set_addr(psmouse, addr))
175925bded7cSSeth Forshee 		return -1;
176025bded7cSSeth Forshee 	return __alps_command_mode_write_reg(psmouse, value);
176125bded7cSSeth Forshee }
176225bded7cSSeth Forshee 
176324ba9707SKevin Cernekee static int alps_rpt_cmd(struct psmouse *psmouse, int init_command,
176424ba9707SKevin Cernekee 			int repeated_command, unsigned char *param)
176524ba9707SKevin Cernekee {
176624ba9707SKevin Cernekee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
176724ba9707SKevin Cernekee 
176824ba9707SKevin Cernekee 	param[0] = 0;
176924ba9707SKevin Cernekee 	if (init_command && ps2_command(ps2dev, param, init_command))
177024ba9707SKevin Cernekee 		return -EIO;
177124ba9707SKevin Cernekee 
177224ba9707SKevin Cernekee 	if (ps2_command(ps2dev,  NULL, repeated_command) ||
177324ba9707SKevin Cernekee 	    ps2_command(ps2dev,  NULL, repeated_command) ||
177424ba9707SKevin Cernekee 	    ps2_command(ps2dev,  NULL, repeated_command))
177524ba9707SKevin Cernekee 		return -EIO;
177624ba9707SKevin Cernekee 
177724ba9707SKevin Cernekee 	param[0] = param[1] = param[2] = 0xff;
177824ba9707SKevin Cernekee 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
177924ba9707SKevin Cernekee 		return -EIO;
178024ba9707SKevin Cernekee 
178139fbe585SDmitry Torokhov 	psmouse_dbg(psmouse, "%2.2X report: %3ph\n",
178239fbe585SDmitry Torokhov 		    repeated_command, param);
178324ba9707SKevin Cernekee 	return 0;
178424ba9707SKevin Cernekee }
178524ba9707SKevin Cernekee 
17863808843cSYunkang Tang static bool alps_check_valid_firmware_id(unsigned char id[])
17873808843cSYunkang Tang {
17883808843cSYunkang Tang 	if (id[0] == 0x73)
17893808843cSYunkang Tang 		return true;
17903808843cSYunkang Tang 
17913808843cSYunkang Tang 	if (id[0] == 0x88 &&
17923808843cSYunkang Tang 	    (id[1] == 0x07 ||
17933808843cSYunkang Tang 	     id[1] == 0x08 ||
17943808843cSYunkang Tang 	     (id[1] & 0xf0) == 0xb0 ||
17953808843cSYunkang Tang 	     (id[1] & 0xf0) == 0xc0)) {
17963808843cSYunkang Tang 		return true;
17973808843cSYunkang Tang 	}
17983808843cSYunkang Tang 
17993808843cSYunkang Tang 	return false;
18003808843cSYunkang Tang }
18013808843cSYunkang Tang 
1802d18e53fcSKevin Cernekee static int alps_enter_command_mode(struct psmouse *psmouse)
180325bded7cSSeth Forshee {
180425bded7cSSeth Forshee 	unsigned char param[4];
180525bded7cSSeth Forshee 
180624ba9707SKevin Cernekee 	if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_RESET_WRAP, param)) {
180725bded7cSSeth Forshee 		psmouse_err(psmouse, "failed to enter command mode\n");
180825bded7cSSeth Forshee 		return -1;
180925bded7cSSeth Forshee 	}
181025bded7cSSeth Forshee 
18113808843cSYunkang Tang 	if (!alps_check_valid_firmware_id(param)) {
181225bded7cSSeth Forshee 		psmouse_dbg(psmouse,
181324ba9707SKevin Cernekee 			    "unknown response while entering command mode\n");
181425bded7cSSeth Forshee 		return -1;
181525bded7cSSeth Forshee 	}
181625bded7cSSeth Forshee 	return 0;
181725bded7cSSeth Forshee }
181825bded7cSSeth Forshee 
181925bded7cSSeth Forshee static inline int alps_exit_command_mode(struct psmouse *psmouse)
182025bded7cSSeth Forshee {
182125bded7cSSeth Forshee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
182225bded7cSSeth Forshee 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM))
182325bded7cSSeth Forshee 		return -1;
182425bded7cSSeth Forshee 	return 0;
182525bded7cSSeth Forshee }
182625bded7cSSeth Forshee 
18271da177e4SLinus Torvalds /*
18281da177e4SLinus Torvalds  * For DualPoint devices select the device that should respond to
18291da177e4SLinus Torvalds  * subsequent commands. It looks like glidepad is behind stickpointer,
18301da177e4SLinus Torvalds  * I'd thought it would be other way around...
18311da177e4SLinus Torvalds  */
183225bded7cSSeth Forshee static int alps_passthrough_mode_v2(struct psmouse *psmouse, bool enable)
18331da177e4SLinus Torvalds {
18341da177e4SLinus Torvalds 	struct ps2dev *ps2dev = &psmouse->ps2dev;
18351da177e4SLinus Torvalds 	int cmd = enable ? PSMOUSE_CMD_SETSCALE21 : PSMOUSE_CMD_SETSCALE11;
18361da177e4SLinus Torvalds 
18371da177e4SLinus Torvalds 	if (ps2_command(ps2dev, NULL, cmd) ||
18381da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, cmd) ||
18391da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, cmd) ||
18401da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
18411da177e4SLinus Torvalds 		return -1;
18421da177e4SLinus Torvalds 
18431da177e4SLinus Torvalds 	/* we may get 3 more bytes, just ignore them */
1844c611763dSDmitry Torokhov 	ps2_drain(ps2dev, 3, 100);
18451da177e4SLinus Torvalds 
18461da177e4SLinus Torvalds 	return 0;
18471da177e4SLinus Torvalds }
18481da177e4SLinus Torvalds 
184925bded7cSSeth Forshee static int alps_absolute_mode_v1_v2(struct psmouse *psmouse)
18501da177e4SLinus Torvalds {
18511da177e4SLinus Torvalds 	struct ps2dev *ps2dev = &psmouse->ps2dev;
18521da177e4SLinus Torvalds 
18531da177e4SLinus Torvalds 	/* Try ALPS magic knock - 4 disable before enable */
18541da177e4SLinus Torvalds 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
18551da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
18561da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
18571da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
18581da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
18591da177e4SLinus Torvalds 		return -1;
18601da177e4SLinus Torvalds 
18611da177e4SLinus Torvalds 	/*
18621da177e4SLinus Torvalds 	 * Switch mouse to poll (remote) mode so motion data will not
18631da177e4SLinus Torvalds 	 * get in our way
18641da177e4SLinus Torvalds 	 */
18651da177e4SLinus Torvalds 	return ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETPOLL);
18661da177e4SLinus Torvalds }
18671da177e4SLinus Torvalds 
186895f75e91SYunkang Tang static int alps_monitor_mode_send_word(struct psmouse *psmouse, u16 word)
186995f75e91SYunkang Tang {
187095f75e91SYunkang Tang 	int i, nibble;
187195f75e91SYunkang Tang 
187295f75e91SYunkang Tang 	/*
187395f75e91SYunkang Tang 	 * b0-b11 are valid bits, send sequence is inverse.
187495f75e91SYunkang Tang 	 * e.g. when word = 0x0123, nibble send sequence is 3, 2, 1
187595f75e91SYunkang Tang 	 */
187695f75e91SYunkang Tang 	for (i = 0; i <= 8; i += 4) {
187795f75e91SYunkang Tang 		nibble = (word >> i) & 0xf;
187895f75e91SYunkang Tang 		if (alps_command_mode_send_nibble(psmouse, nibble))
187995f75e91SYunkang Tang 			return -1;
188095f75e91SYunkang Tang 	}
188195f75e91SYunkang Tang 
188295f75e91SYunkang Tang 	return 0;
188395f75e91SYunkang Tang }
188495f75e91SYunkang Tang 
188595f75e91SYunkang Tang static int alps_monitor_mode_write_reg(struct psmouse *psmouse,
188695f75e91SYunkang Tang 				       u16 addr, u16 value)
188795f75e91SYunkang Tang {
188895f75e91SYunkang Tang 	struct ps2dev *ps2dev = &psmouse->ps2dev;
188995f75e91SYunkang Tang 
189095f75e91SYunkang Tang 	/* 0x0A0 is the command to write the word */
189195f75e91SYunkang Tang 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE) ||
189295f75e91SYunkang Tang 	    alps_monitor_mode_send_word(psmouse, 0x0A0) ||
189395f75e91SYunkang Tang 	    alps_monitor_mode_send_word(psmouse, addr) ||
189495f75e91SYunkang Tang 	    alps_monitor_mode_send_word(psmouse, value) ||
189595f75e91SYunkang Tang 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
189695f75e91SYunkang Tang 		return -1;
189795f75e91SYunkang Tang 
189895f75e91SYunkang Tang 	return 0;
189995f75e91SYunkang Tang }
190095f75e91SYunkang Tang 
190195f75e91SYunkang Tang static int alps_monitor_mode(struct psmouse *psmouse, bool enable)
190295f75e91SYunkang Tang {
190395f75e91SYunkang Tang 	struct ps2dev *ps2dev = &psmouse->ps2dev;
190495f75e91SYunkang Tang 
190595f75e91SYunkang Tang 	if (enable) {
190695f75e91SYunkang Tang 		/* EC E9 F5 F5 E7 E6 E7 E9 to enter monitor mode */
190795f75e91SYunkang Tang 		if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
190895f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO) ||
190995f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
191095f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
191195f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
191295f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
191395f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
191495f75e91SYunkang Tang 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO))
191595f75e91SYunkang Tang 			return -1;
191695f75e91SYunkang Tang 	} else {
191795f75e91SYunkang Tang 		/* EC to exit monitor mode */
191895f75e91SYunkang Tang 		if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP))
191995f75e91SYunkang Tang 			return -1;
192095f75e91SYunkang Tang 	}
192195f75e91SYunkang Tang 
192295f75e91SYunkang Tang 	return 0;
192395f75e91SYunkang Tang }
192495f75e91SYunkang Tang 
192595f75e91SYunkang Tang static int alps_absolute_mode_v6(struct psmouse *psmouse)
192695f75e91SYunkang Tang {
192795f75e91SYunkang Tang 	u16 reg_val = 0x181;
192895f75e91SYunkang Tang 	int ret = -1;
192995f75e91SYunkang Tang 
193095f75e91SYunkang Tang 	/* enter monitor mode, to write the register */
193195f75e91SYunkang Tang 	if (alps_monitor_mode(psmouse, true))
193295f75e91SYunkang Tang 		return -1;
193395f75e91SYunkang Tang 
193495f75e91SYunkang Tang 	ret = alps_monitor_mode_write_reg(psmouse, 0x000, reg_val);
193595f75e91SYunkang Tang 
193695f75e91SYunkang Tang 	if (alps_monitor_mode(psmouse, false))
193795f75e91SYunkang Tang 		ret = -1;
193895f75e91SYunkang Tang 
193995f75e91SYunkang Tang 	return ret;
194095f75e91SYunkang Tang }
194195f75e91SYunkang Tang 
19421da177e4SLinus Torvalds static int alps_get_status(struct psmouse *psmouse, char *param)
19431da177e4SLinus Torvalds {
19441da177e4SLinus Torvalds 	/* Get status: 0xF5 0xF5 0xF5 0xE9 */
194524ba9707SKevin Cernekee 	if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_DISABLE, param))
19461da177e4SLinus Torvalds 		return -1;
19471da177e4SLinus Torvalds 
19481da177e4SLinus Torvalds 	return 0;
19491da177e4SLinus Torvalds }
19501da177e4SLinus Torvalds 
19511da177e4SLinus Torvalds /*
19521da177e4SLinus Torvalds  * Turn touchpad tapping on or off. The sequences are:
19531da177e4SLinus Torvalds  * 0xE9 0xF5 0xF5 0xF3 0x0A to enable,
19541da177e4SLinus Torvalds  * 0xE9 0xF5 0xF5 0xE8 0x00 to disable.
19551da177e4SLinus Torvalds  * My guess that 0xE9 (GetInfo) is here as a sync point.
19561da177e4SLinus Torvalds  * For models that also have stickpointer (DualPoints) its tapping
19571da177e4SLinus Torvalds  * is controlled separately (0xE6 0xE6 0xE6 0xF3 0x14|0x0A) but
19581da177e4SLinus Torvalds  * we don't fiddle with it.
19591da177e4SLinus Torvalds  */
19601da177e4SLinus Torvalds static int alps_tap_mode(struct psmouse *psmouse, int enable)
19611da177e4SLinus Torvalds {
19621da177e4SLinus Torvalds 	struct ps2dev *ps2dev = &psmouse->ps2dev;
19631da177e4SLinus Torvalds 	int cmd = enable ? PSMOUSE_CMD_SETRATE : PSMOUSE_CMD_SETRES;
19641da177e4SLinus Torvalds 	unsigned char tap_arg = enable ? 0x0A : 0x00;
19651da177e4SLinus Torvalds 	unsigned char param[4];
19661da177e4SLinus Torvalds 
19671da177e4SLinus Torvalds 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO) ||
19681da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
19691da177e4SLinus Torvalds 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
19701da177e4SLinus Torvalds 	    ps2_command(ps2dev, &tap_arg, cmd))
19711da177e4SLinus Torvalds 		return -1;
19721da177e4SLinus Torvalds 
19731da177e4SLinus Torvalds 	if (alps_get_status(psmouse, param))
19741da177e4SLinus Torvalds 		return -1;
19751da177e4SLinus Torvalds 
19761da177e4SLinus Torvalds 	return 0;
19771da177e4SLinus Torvalds }
19781da177e4SLinus Torvalds 
1979f0d5c6f4SDmitry Torokhov /*
1980f0d5c6f4SDmitry Torokhov  * alps_poll() - poll the touchpad for current motion packet.
1981f0d5c6f4SDmitry Torokhov  * Used in resync.
1982f0d5c6f4SDmitry Torokhov  */
1983f0d5c6f4SDmitry Torokhov static int alps_poll(struct psmouse *psmouse)
1984f0d5c6f4SDmitry Torokhov {
1985f0d5c6f4SDmitry Torokhov 	struct alps_data *priv = psmouse->private;
1986b46615feSSeth Forshee 	unsigned char buf[sizeof(psmouse->packet)];
1987b7802c5cSDmitry Torokhov 	bool poll_failed;
1988f0d5c6f4SDmitry Torokhov 
198999df65e7SKevin Cernekee 	if (priv->flags & ALPS_PASS)
199025bded7cSSeth Forshee 		alps_passthrough_mode_v2(psmouse, true);
1991f0d5c6f4SDmitry Torokhov 
1992f0d5c6f4SDmitry Torokhov 	poll_failed = ps2_command(&psmouse->ps2dev, buf,
1993f0d5c6f4SDmitry Torokhov 				  PSMOUSE_CMD_POLL | (psmouse->pktsize << 8)) < 0;
1994f0d5c6f4SDmitry Torokhov 
199599df65e7SKevin Cernekee 	if (priv->flags & ALPS_PASS)
199625bded7cSSeth Forshee 		alps_passthrough_mode_v2(psmouse, false);
1997f0d5c6f4SDmitry Torokhov 
199899df65e7SKevin Cernekee 	if (poll_failed || (buf[0] & priv->mask0) != priv->byte0)
1999f0d5c6f4SDmitry Torokhov 		return -1;
2000f0d5c6f4SDmitry Torokhov 
2001f0d5c6f4SDmitry Torokhov 	if ((psmouse->badbyte & 0xc8) == 0x08) {
2002f0d5c6f4SDmitry Torokhov /*
2003f0d5c6f4SDmitry Torokhov  * Poll the track stick ...
2004f0d5c6f4SDmitry Torokhov  */
2005f0d5c6f4SDmitry Torokhov 		if (ps2_command(&psmouse->ps2dev, buf, PSMOUSE_CMD_POLL | (3 << 8)))
2006f0d5c6f4SDmitry Torokhov 			return -1;
2007f0d5c6f4SDmitry Torokhov 	}
2008f0d5c6f4SDmitry Torokhov 
2009f0d5c6f4SDmitry Torokhov 	memcpy(psmouse->packet, buf, sizeof(buf));
2010f0d5c6f4SDmitry Torokhov 	return 0;
2011f0d5c6f4SDmitry Torokhov }
2012f0d5c6f4SDmitry Torokhov 
201325bded7cSSeth Forshee static int alps_hw_init_v1_v2(struct psmouse *psmouse)
20141da177e4SLinus Torvalds {
20151da177e4SLinus Torvalds 	struct alps_data *priv = psmouse->private;
20161da177e4SLinus Torvalds 
201799df65e7SKevin Cernekee 	if ((priv->flags & ALPS_PASS) &&
201825bded7cSSeth Forshee 	    alps_passthrough_mode_v2(psmouse, true)) {
20191da177e4SLinus Torvalds 		return -1;
2020b7802c5cSDmitry Torokhov 	}
20211da177e4SLinus Torvalds 
2022b7802c5cSDmitry Torokhov 	if (alps_tap_mode(psmouse, true)) {
2023b5d21704SDmitry Torokhov 		psmouse_warn(psmouse, "Failed to enable hardware tapping\n");
20241da177e4SLinus Torvalds 		return -1;
2025963f626dSPeter Osterlund 	}
20261da177e4SLinus Torvalds 
202725bded7cSSeth Forshee 	if (alps_absolute_mode_v1_v2(psmouse)) {
2028b5d21704SDmitry Torokhov 		psmouse_err(psmouse, "Failed to enable absolute mode\n");
20291da177e4SLinus Torvalds 		return -1;
20301da177e4SLinus Torvalds 	}
20311da177e4SLinus Torvalds 
203299df65e7SKevin Cernekee 	if ((priv->flags & ALPS_PASS) &&
203325bded7cSSeth Forshee 	    alps_passthrough_mode_v2(psmouse, false)) {
20341da177e4SLinus Torvalds 		return -1;
2035b7802c5cSDmitry Torokhov 	}
20361da177e4SLinus Torvalds 
20371e0c5b12SDmitry Torokhov 	/* ALPS needs stream mode, otherwise it won't report any data */
20381e0c5b12SDmitry Torokhov 	if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)) {
2039b5d21704SDmitry Torokhov 		psmouse_err(psmouse, "Failed to enable stream mode\n");
20401e0c5b12SDmitry Torokhov 		return -1;
20411e0c5b12SDmitry Torokhov 	}
20421e0c5b12SDmitry Torokhov 
20431e0c5b12SDmitry Torokhov 	return 0;
20441e0c5b12SDmitry Torokhov }
20451e0c5b12SDmitry Torokhov 
204695f75e91SYunkang Tang static int alps_hw_init_v6(struct psmouse *psmouse)
204795f75e91SYunkang Tang {
204895f75e91SYunkang Tang 	unsigned char param[2] = {0xC8, 0x14};
204995f75e91SYunkang Tang 
205095f75e91SYunkang Tang 	/* Enter passthrough mode to let trackpoint enter 6byte raw mode */
205195f75e91SYunkang Tang 	if (alps_passthrough_mode_v2(psmouse, true))
205295f75e91SYunkang Tang 		return -1;
205395f75e91SYunkang Tang 
205495f75e91SYunkang Tang 	if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
205595f75e91SYunkang Tang 	    ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
205695f75e91SYunkang Tang 	    ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
205795f75e91SYunkang Tang 	    ps2_command(&psmouse->ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
205895f75e91SYunkang Tang 	    ps2_command(&psmouse->ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
205995f75e91SYunkang Tang 		return -1;
206095f75e91SYunkang Tang 
206195f75e91SYunkang Tang 	if (alps_passthrough_mode_v2(psmouse, false))
206295f75e91SYunkang Tang 		return -1;
206395f75e91SYunkang Tang 
206495f75e91SYunkang Tang 	if (alps_absolute_mode_v6(psmouse)) {
206595f75e91SYunkang Tang 		psmouse_err(psmouse, "Failed to enable absolute mode\n");
206695f75e91SYunkang Tang 		return -1;
206795f75e91SYunkang Tang 	}
206895f75e91SYunkang Tang 
206995f75e91SYunkang Tang 	return 0;
207095f75e91SYunkang Tang }
207195f75e91SYunkang Tang 
207225bded7cSSeth Forshee /*
2073cd401204SKevin Cernekee  * Enable or disable passthrough mode to the trackstick.
207425bded7cSSeth Forshee  */
2075cd401204SKevin Cernekee static int alps_passthrough_mode_v3(struct psmouse *psmouse,
2076cd401204SKevin Cernekee 				    int reg_base, bool enable)
207725bded7cSSeth Forshee {
2078cd401204SKevin Cernekee 	int reg_val, ret = -1;
207925bded7cSSeth Forshee 
2080d18e53fcSKevin Cernekee 	if (alps_enter_command_mode(psmouse))
208125bded7cSSeth Forshee 		return -1;
208225bded7cSSeth Forshee 
2083cd401204SKevin Cernekee 	reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x0008);
2084cd401204SKevin Cernekee 	if (reg_val == -1)
2085cd401204SKevin Cernekee 		goto error;
2086cd401204SKevin Cernekee 
208725bded7cSSeth Forshee 	if (enable)
208825bded7cSSeth Forshee 		reg_val |= 0x01;
208925bded7cSSeth Forshee 	else
209025bded7cSSeth Forshee 		reg_val &= ~0x01;
209125bded7cSSeth Forshee 
2092cd401204SKevin Cernekee 	ret = __alps_command_mode_write_reg(psmouse, reg_val);
209325bded7cSSeth Forshee 
2094cd401204SKevin Cernekee error:
2095cd401204SKevin Cernekee 	if (alps_exit_command_mode(psmouse))
2096cd401204SKevin Cernekee 		ret = -1;
2097cd401204SKevin Cernekee 	return ret;
209825bded7cSSeth Forshee }
209925bded7cSSeth Forshee 
210025bded7cSSeth Forshee /* Must be in command mode when calling this function */
210125bded7cSSeth Forshee static int alps_absolute_mode_v3(struct psmouse *psmouse)
210225bded7cSSeth Forshee {
210325bded7cSSeth Forshee 	int reg_val;
210425bded7cSSeth Forshee 
210525bded7cSSeth Forshee 	reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
210625bded7cSSeth Forshee 	if (reg_val == -1)
210725bded7cSSeth Forshee 		return -1;
210825bded7cSSeth Forshee 
210925bded7cSSeth Forshee 	reg_val |= 0x06;
211025bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, reg_val))
211125bded7cSSeth Forshee 		return -1;
211225bded7cSSeth Forshee 
211325bded7cSSeth Forshee 	return 0;
211425bded7cSSeth Forshee }
211525bded7cSSeth Forshee 
2116dae928ecSPali Rohár static int alps_probe_trackstick_v3_v7(struct psmouse *psmouse, int reg_base)
211725bded7cSSeth Forshee {
2118cd401204SKevin Cernekee 	int ret = -EIO, reg_val;
211925bded7cSSeth Forshee 
2120d18e53fcSKevin Cernekee 	if (alps_enter_command_mode(psmouse))
212125bded7cSSeth Forshee 		goto error;
212225bded7cSSeth Forshee 
2123cd401204SKevin Cernekee 	reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x08);
212425bded7cSSeth Forshee 	if (reg_val == -1)
212525bded7cSSeth Forshee 		goto error;
2126cd401204SKevin Cernekee 
2127cd401204SKevin Cernekee 	/* bit 7: trackstick is present */
2128cd401204SKevin Cernekee 	ret = reg_val & 0x80 ? 0 : -ENODEV;
2129cd401204SKevin Cernekee 
2130cd401204SKevin Cernekee error:
2131cd401204SKevin Cernekee 	alps_exit_command_mode(psmouse);
2132cd401204SKevin Cernekee 	return ret;
2133cd401204SKevin Cernekee }
2134cd401204SKevin Cernekee 
2135cd401204SKevin Cernekee static int alps_setup_trackstick_v3(struct psmouse *psmouse, int reg_base)
2136cd401204SKevin Cernekee {
2137cd401204SKevin Cernekee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
2138cd401204SKevin Cernekee 	int ret = 0;
2139cd401204SKevin Cernekee 	unsigned char param[4];
2140cd401204SKevin Cernekee 
2141cd401204SKevin Cernekee 	if (alps_passthrough_mode_v3(psmouse, reg_base, true))
2142cd401204SKevin Cernekee 		return -EIO;
214325bded7cSSeth Forshee 
214425bded7cSSeth Forshee 	/*
214525bded7cSSeth Forshee 	 * E7 report for the trackstick
214625bded7cSSeth Forshee 	 *
214725bded7cSSeth Forshee 	 * There have been reports of failures to seem to trace back
214825bded7cSSeth Forshee 	 * to the above trackstick check failing. When these occur
214925bded7cSSeth Forshee 	 * this E7 report fails, so when that happens we continue
215025bded7cSSeth Forshee 	 * with the assumption that there isn't a trackstick after
215125bded7cSSeth Forshee 	 * all.
215225bded7cSSeth Forshee 	 */
2153cd401204SKevin Cernekee 	if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_SETSCALE21, param)) {
2154a09221e8SDmitry Torokhov 		psmouse_warn(psmouse, "Failed to initialize trackstick (E7 report failed)\n");
2155cd401204SKevin Cernekee 		ret = -ENODEV;
215625bded7cSSeth Forshee 	} else {
215739fbe585SDmitry Torokhov 		psmouse_dbg(psmouse, "trackstick E7 report: %3ph\n", param);
215825bded7cSSeth Forshee 
215925bded7cSSeth Forshee 		/*
216025bded7cSSeth Forshee 		 * Not sure what this does, but it is absolutely
216125bded7cSSeth Forshee 		 * essential. Without it, the touchpad does not
216225bded7cSSeth Forshee 		 * work at all and the trackstick just emits normal
216325bded7cSSeth Forshee 		 * PS/2 packets.
216425bded7cSSeth Forshee 		 */
216525bded7cSSeth Forshee 		if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
216625bded7cSSeth Forshee 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
216725bded7cSSeth Forshee 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
216825bded7cSSeth Forshee 		    alps_command_mode_send_nibble(psmouse, 0x9) ||
216925bded7cSSeth Forshee 		    alps_command_mode_send_nibble(psmouse, 0x4)) {
217025bded7cSSeth Forshee 			psmouse_err(psmouse,
217125bded7cSSeth Forshee 				    "Error sending magic E6 sequence\n");
2172cd401204SKevin Cernekee 			ret = -EIO;
217325bded7cSSeth Forshee 			goto error;
217425bded7cSSeth Forshee 		}
217525bded7cSSeth Forshee 
2176cd401204SKevin Cernekee 		/*
2177cd401204SKevin Cernekee 		 * This ensures the trackstick packets are in the format
2178cd401204SKevin Cernekee 		 * supported by this driver. If bit 1 isn't set the packet
2179cd401204SKevin Cernekee 		 * format is different.
2180cd401204SKevin Cernekee 		 */
2181d18e53fcSKevin Cernekee 		if (alps_enter_command_mode(psmouse) ||
2182cd401204SKevin Cernekee 		    alps_command_mode_write_reg(psmouse,
2183cd401204SKevin Cernekee 						reg_base + 0x08, 0x82) ||
2184cd401204SKevin Cernekee 		    alps_exit_command_mode(psmouse))
2185cd401204SKevin Cernekee 			ret = -EIO;
2186cd401204SKevin Cernekee 	}
2187cd401204SKevin Cernekee 
2188cd401204SKevin Cernekee error:
2189cd401204SKevin Cernekee 	if (alps_passthrough_mode_v3(psmouse, reg_base, false))
2190cd401204SKevin Cernekee 		ret = -EIO;
2191cd401204SKevin Cernekee 
2192cd401204SKevin Cernekee 	return ret;
2193cd401204SKevin Cernekee }
2194cd401204SKevin Cernekee 
2195cd401204SKevin Cernekee static int alps_hw_init_v3(struct psmouse *psmouse)
2196cd401204SKevin Cernekee {
21974b1af853SPali Rohár 	struct alps_data *priv = psmouse->private;
2198cd401204SKevin Cernekee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
2199cd401204SKevin Cernekee 	int reg_val;
2200cd401204SKevin Cernekee 	unsigned char param[4];
2201cd401204SKevin Cernekee 
22024b1af853SPali Rohár 	if ((priv->flags & ALPS_DUALPOINT) &&
2203cd401204SKevin Cernekee 	    alps_setup_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE) == -EIO)
2204cd401204SKevin Cernekee 		goto error;
2205cd401204SKevin Cernekee 
2206d18e53fcSKevin Cernekee 	if (alps_enter_command_mode(psmouse) ||
2207cd401204SKevin Cernekee 	    alps_absolute_mode_v3(psmouse)) {
220825bded7cSSeth Forshee 		psmouse_err(psmouse, "Failed to enter absolute mode\n");
220925bded7cSSeth Forshee 		goto error;
221025bded7cSSeth Forshee 	}
221125bded7cSSeth Forshee 
221225bded7cSSeth Forshee 	reg_val = alps_command_mode_read_reg(psmouse, 0x0006);
221325bded7cSSeth Forshee 	if (reg_val == -1)
221425bded7cSSeth Forshee 		goto error;
221525bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
221625bded7cSSeth Forshee 		goto error;
221725bded7cSSeth Forshee 
221825bded7cSSeth Forshee 	reg_val = alps_command_mode_read_reg(psmouse, 0x0007);
221925bded7cSSeth Forshee 	if (reg_val == -1)
222025bded7cSSeth Forshee 		goto error;
222125bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
222225bded7cSSeth Forshee 		goto error;
222325bded7cSSeth Forshee 
222425bded7cSSeth Forshee 	if (alps_command_mode_read_reg(psmouse, 0x0144) == -1)
222525bded7cSSeth Forshee 		goto error;
222625bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, 0x04))
222725bded7cSSeth Forshee 		goto error;
222825bded7cSSeth Forshee 
222925bded7cSSeth Forshee 	if (alps_command_mode_read_reg(psmouse, 0x0159) == -1)
223025bded7cSSeth Forshee 		goto error;
223125bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, 0x03))
223225bded7cSSeth Forshee 		goto error;
223325bded7cSSeth Forshee 
223425bded7cSSeth Forshee 	if (alps_command_mode_read_reg(psmouse, 0x0163) == -1)
223525bded7cSSeth Forshee 		goto error;
223625bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0163, 0x03))
223725bded7cSSeth Forshee 		goto error;
223825bded7cSSeth Forshee 
223925bded7cSSeth Forshee 	if (alps_command_mode_read_reg(psmouse, 0x0162) == -1)
224025bded7cSSeth Forshee 		goto error;
224125bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0162, 0x04))
224225bded7cSSeth Forshee 		goto error;
224325bded7cSSeth Forshee 
224425bded7cSSeth Forshee 	alps_exit_command_mode(psmouse);
224525bded7cSSeth Forshee 
224625bded7cSSeth Forshee 	/* Set rate and enable data reporting */
224725bded7cSSeth Forshee 	param[0] = 0x64;
224825bded7cSSeth Forshee 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
224925bded7cSSeth Forshee 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
225025bded7cSSeth Forshee 		psmouse_err(psmouse, "Failed to enable data reporting\n");
225125bded7cSSeth Forshee 		return -1;
225225bded7cSSeth Forshee 	}
225325bded7cSSeth Forshee 
225425bded7cSSeth Forshee 	return 0;
225525bded7cSSeth Forshee 
225625bded7cSSeth Forshee error:
225725bded7cSSeth Forshee 	/*
225825bded7cSSeth Forshee 	 * Leaving the touchpad in command mode will essentially render
225925bded7cSSeth Forshee 	 * it unusable until the machine reboots, so exit it here just
226025bded7cSSeth Forshee 	 * to be safe
226125bded7cSSeth Forshee 	 */
226225bded7cSSeth Forshee 	alps_exit_command_mode(psmouse);
226325bded7cSSeth Forshee 	return -1;
226425bded7cSSeth Forshee }
226525bded7cSSeth Forshee 
2266f3f33c67SHans de Goede static int alps_get_v3_v7_resolution(struct psmouse *psmouse, int reg_pitch)
2267f3f33c67SHans de Goede {
2268f3f33c67SHans de Goede 	int reg, x_pitch, y_pitch, x_electrode, y_electrode, x_phys, y_phys;
2269f3f33c67SHans de Goede 	struct alps_data *priv = psmouse->private;
2270f3f33c67SHans de Goede 
2271f3f33c67SHans de Goede 	reg = alps_command_mode_read_reg(psmouse, reg_pitch);
2272f3f33c67SHans de Goede 	if (reg < 0)
2273f3f33c67SHans de Goede 		return reg;
2274f3f33c67SHans de Goede 
2275f3f33c67SHans de Goede 	x_pitch = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
2276f3f33c67SHans de Goede 	x_pitch = 50 + 2 * x_pitch; /* In 0.1 mm units */
2277f3f33c67SHans de Goede 
2278f3f33c67SHans de Goede 	y_pitch = (char)reg >> 4; /* sign extend upper 4 bits */
2279f3f33c67SHans de Goede 	y_pitch = 36 + 2 * y_pitch; /* In 0.1 mm units */
2280f3f33c67SHans de Goede 
2281f3f33c67SHans de Goede 	reg = alps_command_mode_read_reg(psmouse, reg_pitch + 1);
2282f3f33c67SHans de Goede 	if (reg < 0)
2283f3f33c67SHans de Goede 		return reg;
2284f3f33c67SHans de Goede 
2285f3f33c67SHans de Goede 	x_electrode = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
2286f3f33c67SHans de Goede 	x_electrode = 17 + x_electrode;
2287f3f33c67SHans de Goede 
2288f3f33c67SHans de Goede 	y_electrode = (char)reg >> 4; /* sign extend upper 4 bits */
2289f3f33c67SHans de Goede 	y_electrode = 13 + y_electrode;
2290f3f33c67SHans de Goede 
2291f3f33c67SHans de Goede 	x_phys = x_pitch * (x_electrode - 1); /* In 0.1 mm units */
2292f3f33c67SHans de Goede 	y_phys = y_pitch * (y_electrode - 1); /* In 0.1 mm units */
2293f3f33c67SHans de Goede 
2294f3f33c67SHans de Goede 	priv->x_res = priv->x_max * 10 / x_phys; /* units / mm */
2295f3f33c67SHans de Goede 	priv->y_res = priv->y_max * 10 / y_phys; /* units / mm */
2296f3f33c67SHans de Goede 
2297f3f33c67SHans de Goede 	psmouse_dbg(psmouse,
2298f3f33c67SHans de Goede 		    "pitch %dx%d num-electrodes %dx%d physical size %dx%d mm res %dx%d\n",
2299f3f33c67SHans de Goede 		    x_pitch, y_pitch, x_electrode, y_electrode,
2300f3f33c67SHans de Goede 		    x_phys / 10, y_phys / 10, priv->x_res, priv->y_res);
2301f3f33c67SHans de Goede 
2302f3f33c67SHans de Goede 	return 0;
2303f3f33c67SHans de Goede }
2304f3f33c67SHans de Goede 
23051302bac3SKevin Cernekee static int alps_hw_init_rushmore_v3(struct psmouse *psmouse)
23061302bac3SKevin Cernekee {
2307cd401204SKevin Cernekee 	struct alps_data *priv = psmouse->private;
23081302bac3SKevin Cernekee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
23091302bac3SKevin Cernekee 	int reg_val, ret = -1;
23101302bac3SKevin Cernekee 
2311cd401204SKevin Cernekee 	if (priv->flags & ALPS_DUALPOINT) {
2312cd401204SKevin Cernekee 		reg_val = alps_setup_trackstick_v3(psmouse,
2313cd401204SKevin Cernekee 						   ALPS_REG_BASE_RUSHMORE);
2314cd401204SKevin Cernekee 		if (reg_val == -EIO)
2315cd401204SKevin Cernekee 			goto error;
2316cd401204SKevin Cernekee 	}
2317cd401204SKevin Cernekee 
2318d18e53fcSKevin Cernekee 	if (alps_enter_command_mode(psmouse) ||
23191302bac3SKevin Cernekee 	    alps_command_mode_read_reg(psmouse, 0xc2d9) == -1 ||
23201302bac3SKevin Cernekee 	    alps_command_mode_write_reg(psmouse, 0xc2cb, 0x00))
23211302bac3SKevin Cernekee 		goto error;
23221302bac3SKevin Cernekee 
2323f3f33c67SHans de Goede 	if (alps_get_v3_v7_resolution(psmouse, 0xc2da))
2324f3f33c67SHans de Goede 		goto error;
2325f3f33c67SHans de Goede 
23261302bac3SKevin Cernekee 	reg_val = alps_command_mode_read_reg(psmouse, 0xc2c6);
23271302bac3SKevin Cernekee 	if (reg_val == -1)
23281302bac3SKevin Cernekee 		goto error;
23291302bac3SKevin Cernekee 	if (__alps_command_mode_write_reg(psmouse, reg_val & 0xfd))
23301302bac3SKevin Cernekee 		goto error;
23311302bac3SKevin Cernekee 
23321302bac3SKevin Cernekee 	if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
23331302bac3SKevin Cernekee 		goto error;
23341302bac3SKevin Cernekee 
23351302bac3SKevin Cernekee 	/* enter absolute mode */
23361302bac3SKevin Cernekee 	reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
23371302bac3SKevin Cernekee 	if (reg_val == -1)
23381302bac3SKevin Cernekee 		goto error;
23391302bac3SKevin Cernekee 	if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
23401302bac3SKevin Cernekee 		goto error;
23411302bac3SKevin Cernekee 
23421302bac3SKevin Cernekee 	alps_exit_command_mode(psmouse);
23431302bac3SKevin Cernekee 	return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
23441302bac3SKevin Cernekee 
23451302bac3SKevin Cernekee error:
23461302bac3SKevin Cernekee 	alps_exit_command_mode(psmouse);
23471302bac3SKevin Cernekee 	return ret;
23481302bac3SKevin Cernekee }
23491302bac3SKevin Cernekee 
235025bded7cSSeth Forshee /* Must be in command mode when calling this function */
235125bded7cSSeth Forshee static int alps_absolute_mode_v4(struct psmouse *psmouse)
235225bded7cSSeth Forshee {
235325bded7cSSeth Forshee 	int reg_val;
235425bded7cSSeth Forshee 
235525bded7cSSeth Forshee 	reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
235625bded7cSSeth Forshee 	if (reg_val == -1)
235725bded7cSSeth Forshee 		return -1;
235825bded7cSSeth Forshee 
235925bded7cSSeth Forshee 	reg_val |= 0x02;
236025bded7cSSeth Forshee 	if (__alps_command_mode_write_reg(psmouse, reg_val))
236125bded7cSSeth Forshee 		return -1;
236225bded7cSSeth Forshee 
236325bded7cSSeth Forshee 	return 0;
236425bded7cSSeth Forshee }
236525bded7cSSeth Forshee 
236625bded7cSSeth Forshee static int alps_hw_init_v4(struct psmouse *psmouse)
236725bded7cSSeth Forshee {
236825bded7cSSeth Forshee 	struct ps2dev *ps2dev = &psmouse->ps2dev;
236925bded7cSSeth Forshee 	unsigned char param[4];
237025bded7cSSeth Forshee 
2371d18e53fcSKevin Cernekee 	if (alps_enter_command_mode(psmouse))
237225bded7cSSeth Forshee 		goto error;
237325bded7cSSeth Forshee 
237425bded7cSSeth Forshee 	if (alps_absolute_mode_v4(psmouse)) {
237525bded7cSSeth Forshee 		psmouse_err(psmouse, "Failed to enter absolute mode\n");
237625bded7cSSeth Forshee 		goto error;
237725bded7cSSeth Forshee 	}
237825bded7cSSeth Forshee 
237925bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0007, 0x8c))
238025bded7cSSeth Forshee 		goto error;
238125bded7cSSeth Forshee 
238225bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0149, 0x03))
238325bded7cSSeth Forshee 		goto error;
238425bded7cSSeth Forshee 
238525bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0160, 0x03))
238625bded7cSSeth Forshee 		goto error;
238725bded7cSSeth Forshee 
238825bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x017f, 0x15))
238925bded7cSSeth Forshee 		goto error;
239025bded7cSSeth Forshee 
239125bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0151, 0x01))
239225bded7cSSeth Forshee 		goto error;
239325bded7cSSeth Forshee 
239425bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0168, 0x03))
239525bded7cSSeth Forshee 		goto error;
239625bded7cSSeth Forshee 
239725bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x014a, 0x03))
239825bded7cSSeth Forshee 		goto error;
239925bded7cSSeth Forshee 
240025bded7cSSeth Forshee 	if (alps_command_mode_write_reg(psmouse, 0x0161, 0x03))
240125bded7cSSeth Forshee 		goto error;
240225bded7cSSeth Forshee 
240325bded7cSSeth Forshee 	alps_exit_command_mode(psmouse);
240425bded7cSSeth Forshee 
240525bded7cSSeth Forshee 	/*
240625bded7cSSeth Forshee 	 * This sequence changes the output from a 9-byte to an
240725bded7cSSeth Forshee 	 * 8-byte format. All the same data seems to be present,
240825bded7cSSeth Forshee 	 * just in a more compact format.
240925bded7cSSeth Forshee 	 */
241025bded7cSSeth Forshee 	param[0] = 0xc8;
241125bded7cSSeth Forshee 	param[1] = 0x64;
241225bded7cSSeth Forshee 	param[2] = 0x50;
241325bded7cSSeth Forshee 	if (ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
241425bded7cSSeth Forshee 	    ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE) ||
241525bded7cSSeth Forshee 	    ps2_command(ps2dev, &param[2], PSMOUSE_CMD_SETRATE) ||
241625bded7cSSeth Forshee 	    ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
241725bded7cSSeth Forshee 		return -1;
241825bded7cSSeth Forshee 
241925bded7cSSeth Forshee 	/* Set rate and enable data reporting */
242025bded7cSSeth Forshee 	param[0] = 0x64;
242125bded7cSSeth Forshee 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
242225bded7cSSeth Forshee 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
242325bded7cSSeth Forshee 		psmouse_err(psmouse, "Failed to enable data reporting\n");
242425bded7cSSeth Forshee 		return -1;
242525bded7cSSeth Forshee 	}
242625bded7cSSeth Forshee 
242725bded7cSSeth Forshee 	return 0;
242825bded7cSSeth Forshee 
242925bded7cSSeth Forshee error:
243025bded7cSSeth Forshee 	/*
243125bded7cSSeth Forshee 	 * Leaving the touchpad in command mode will essentially render
243225bded7cSSeth Forshee 	 * it unusable until the machine reboots, so exit it here just
243325bded7cSSeth Forshee 	 * to be safe
243425bded7cSSeth Forshee 	 */
243525bded7cSSeth Forshee 	alps_exit_command_mode(psmouse);
243625bded7cSSeth Forshee 	return -1;
243725bded7cSSeth Forshee }
243825bded7cSSeth Forshee 
24393db5b9f7SMasaki Ota static int alps_get_otp_values_ss4_v2(struct psmouse *psmouse,
24403db5b9f7SMasaki Ota 				      unsigned char index, unsigned char otp[])
24413db5b9f7SMasaki Ota {
24423db5b9f7SMasaki Ota 	struct ps2dev *ps2dev = &psmouse->ps2dev;
24433db5b9f7SMasaki Ota 
24443db5b9f7SMasaki Ota 	switch (index) {
24453db5b9f7SMasaki Ota 	case 0:
24463db5b9f7SMasaki Ota 		if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)  ||
24473db5b9f7SMasaki Ota 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)  ||
24483db5b9f7SMasaki Ota 		    ps2_command(ps2dev, otp, PSMOUSE_CMD_GETINFO))
24493db5b9f7SMasaki Ota 			return -1;
24503db5b9f7SMasaki Ota 
24513db5b9f7SMasaki Ota 		break;
24523db5b9f7SMasaki Ota 
24533db5b9f7SMasaki Ota 	case 1:
24543db5b9f7SMasaki Ota 		if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL)  ||
24553db5b9f7SMasaki Ota 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL)  ||
24563db5b9f7SMasaki Ota 		    ps2_command(ps2dev, otp, PSMOUSE_CMD_GETINFO))
24573db5b9f7SMasaki Ota 			return -1;
24583db5b9f7SMasaki Ota 
24593db5b9f7SMasaki Ota 		break;
24603db5b9f7SMasaki Ota 	}
24613db5b9f7SMasaki Ota 
24623db5b9f7SMasaki Ota 	return 0;
24633db5b9f7SMasaki Ota }
24643db5b9f7SMasaki Ota 
246507f19e3dSFengguang Wu static int alps_update_device_area_ss4_v2(unsigned char otp[][4],
24663db5b9f7SMasaki Ota 					  struct alps_data *priv)
24673db5b9f7SMasaki Ota {
24683db5b9f7SMasaki Ota 	int num_x_electrode;
24693db5b9f7SMasaki Ota 	int num_y_electrode;
24703db5b9f7SMasaki Ota 	int x_pitch, y_pitch, x_phys, y_phys;
24713db5b9f7SMasaki Ota 
24723db5b9f7SMasaki Ota 	num_x_electrode = SS4_NUMSENSOR_XOFFSET + (otp[1][0] & 0x0F);
24733db5b9f7SMasaki Ota 	num_y_electrode = SS4_NUMSENSOR_YOFFSET + ((otp[1][0] >> 4) & 0x0F);
24743db5b9f7SMasaki Ota 
24753db5b9f7SMasaki Ota 	priv->x_max = (num_x_electrode - 1) * SS4_COUNT_PER_ELECTRODE;
24763db5b9f7SMasaki Ota 	priv->y_max = (num_y_electrode - 1) * SS4_COUNT_PER_ELECTRODE;
24773db5b9f7SMasaki Ota 
24783db5b9f7SMasaki Ota 	x_pitch = ((otp[1][2] >> 2) & 0x07) + SS4_MIN_PITCH_MM;
24793db5b9f7SMasaki Ota 	y_pitch = ((otp[1][2] >> 5) & 0x07) + SS4_MIN_PITCH_MM;
24803db5b9f7SMasaki Ota 
24813db5b9f7SMasaki Ota 	x_phys = x_pitch * (num_x_electrode - 1); /* In 0.1 mm units */
24823db5b9f7SMasaki Ota 	y_phys = y_pitch * (num_y_electrode - 1); /* In 0.1 mm units */
24833db5b9f7SMasaki Ota 
24843db5b9f7SMasaki Ota 	priv->x_res = priv->x_max * 10 / x_phys; /* units / mm */
24853db5b9f7SMasaki Ota 	priv->y_res = priv->y_max * 10 / y_phys; /* units / mm */
24863db5b9f7SMasaki Ota 
24873db5b9f7SMasaki Ota 	return 0;
24883db5b9f7SMasaki Ota }
24893db5b9f7SMasaki Ota 
249007f19e3dSFengguang Wu static int alps_update_btn_info_ss4_v2(unsigned char otp[][4],
249107f19e3dSFengguang Wu 				       struct alps_data *priv)
24923db5b9f7SMasaki Ota {
24933db5b9f7SMasaki Ota 	unsigned char is_btnless;
24943db5b9f7SMasaki Ota 
24953db5b9f7SMasaki Ota 	is_btnless = (otp[1][1] >> 3) & 0x01;
24963db5b9f7SMasaki Ota 
24973db5b9f7SMasaki Ota 	if (is_btnless)
24983db5b9f7SMasaki Ota 		priv->flags |= ALPS_BUTTONPAD;
24993db5b9f7SMasaki Ota 
25003db5b9f7SMasaki Ota 	return 0;
25013db5b9f7SMasaki Ota }
25023db5b9f7SMasaki Ota 
25033db5b9f7SMasaki Ota static int alps_set_defaults_ss4_v2(struct psmouse *psmouse,
25043db5b9f7SMasaki Ota 				    struct alps_data *priv)
25053db5b9f7SMasaki Ota {
25063db5b9f7SMasaki Ota 	unsigned char otp[2][4];
25073db5b9f7SMasaki Ota 
25083db5b9f7SMasaki Ota 	memset(otp, 0, sizeof(otp));
25093db5b9f7SMasaki Ota 
25103db5b9f7SMasaki Ota 	if (alps_get_otp_values_ss4_v2(psmouse, 0, &otp[0][0]) ||
25113db5b9f7SMasaki Ota 	    alps_get_otp_values_ss4_v2(psmouse, 1, &otp[1][0]))
25123db5b9f7SMasaki Ota 		return -1;
25133db5b9f7SMasaki Ota 
25143db5b9f7SMasaki Ota 	alps_update_device_area_ss4_v2(otp, priv);
25153db5b9f7SMasaki Ota 
25163db5b9f7SMasaki Ota 	alps_update_btn_info_ss4_v2(otp, priv);
25173db5b9f7SMasaki Ota 
25183db5b9f7SMasaki Ota 	return 0;
25193db5b9f7SMasaki Ota }
25203db5b9f7SMasaki Ota 
2521ee65d4b3SYunkang Tang static int alps_dolphin_get_device_area(struct psmouse *psmouse,
2522ee65d4b3SYunkang Tang 					struct alps_data *priv)
2523ee65d4b3SYunkang Tang {
2524ee65d4b3SYunkang Tang 	struct ps2dev *ps2dev = &psmouse->ps2dev;
2525ee65d4b3SYunkang Tang 	unsigned char param[4] = {0};
2526ee65d4b3SYunkang Tang 	int num_x_electrode, num_y_electrode;
2527ee65d4b3SYunkang Tang 
2528ee65d4b3SYunkang Tang 	if (alps_enter_command_mode(psmouse))
2529ee65d4b3SYunkang Tang 		return -1;
2530ee65d4b3SYunkang Tang 
2531ee65d4b3SYunkang Tang 	param[0] = 0x0a;
2532ee65d4b3SYunkang Tang 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
2533ee65d4b3SYunkang Tang 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2534ee65d4b3SYunkang Tang 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2535ee65d4b3SYunkang Tang 	    ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2536ee65d4b3SYunkang Tang 	    ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE))
2537ee65d4b3SYunkang Tang 		return -1;
2538ee65d4b3SYunkang Tang 
2539ee65d4b3SYunkang Tang 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
2540ee65d4b3SYunkang Tang 		return -1;
2541ee65d4b3SYunkang Tang 
2542ee65d4b3SYunkang Tang 	/*
2543ee65d4b3SYunkang Tang 	 * Dolphin's sensor line number is not fixed. It can be calculated
2544ee65d4b3SYunkang Tang 	 * by adding the device's register value with DOLPHIN_PROFILE_X/YOFFSET.
2545ee65d4b3SYunkang Tang 	 * Further more, we can get device's x_max and y_max by multiplying
2546ee65d4b3SYunkang Tang 	 * sensor line number with DOLPHIN_COUNT_PER_ELECTRODE.
2547ee65d4b3SYunkang Tang 	 *
2548ee65d4b3SYunkang Tang 	 * e.g. When we get register's sensor_x = 11 & sensor_y = 8,
2549ee65d4b3SYunkang Tang 	 *	real sensor line number X = 11 + 8 = 19, and
2550ee65d4b3SYunkang Tang 	 *	real sensor line number Y = 8 + 1 = 9.
2551ee65d4b3SYunkang Tang 	 *	So, x_max = (19 - 1) * 64 = 1152, and
2552ee65d4b3SYunkang Tang 	 *	    y_max = (9 - 1) * 64 = 512.
2553ee65d4b3SYunkang Tang 	 */
2554ee65d4b3SYunkang Tang 	num_x_electrode = DOLPHIN_PROFILE_XOFFSET + (param[2] & 0x0F);
2555ee65d4b3SYunkang Tang 	num_y_electrode = DOLPHIN_PROFILE_YOFFSET + ((param[2] >> 4) & 0x0F);
2556ee65d4b3SYunkang Tang 	priv->x_bits = num_x_electrode;
2557ee65d4b3SYunkang Tang 	priv->y_bits = num_y_electrode;
2558ee65d4b3SYunkang Tang 	priv->x_max = (num_x_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
2559ee65d4b3SYunkang Tang 	priv->y_max = (num_y_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
2560ee65d4b3SYunkang Tang 
2561ee65d4b3SYunkang Tang 	if (alps_exit_command_mode(psmouse))
2562ee65d4b3SYunkang Tang 		return -1;
2563ee65d4b3SYunkang Tang 
2564ee65d4b3SYunkang Tang 	return 0;
2565ee65d4b3SYunkang Tang }
2566ee65d4b3SYunkang Tang 
256775af9e56SDave Turvene static int alps_hw_init_dolphin_v1(struct psmouse *psmouse)
256875af9e56SDave Turvene {
256975af9e56SDave Turvene 	struct ps2dev *ps2dev = &psmouse->ps2dev;
257075af9e56SDave Turvene 	unsigned char param[2];
257175af9e56SDave Turvene 
257275af9e56SDave Turvene 	/* This is dolphin "v1" as empirically defined by florin9doi */
257375af9e56SDave Turvene 	param[0] = 0x64;
257475af9e56SDave Turvene 	param[1] = 0x28;
257575af9e56SDave Turvene 
257675af9e56SDave Turvene 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
257775af9e56SDave Turvene 	    ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
257875af9e56SDave Turvene 	    ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
257975af9e56SDave Turvene 		return -1;
258075af9e56SDave Turvene 
258175af9e56SDave Turvene 	return 0;
258275af9e56SDave Turvene }
258375af9e56SDave Turvene 
25843808843cSYunkang Tang static int alps_hw_init_v7(struct psmouse *psmouse)
25853808843cSYunkang Tang {
25863808843cSYunkang Tang 	struct ps2dev *ps2dev = &psmouse->ps2dev;
25873808843cSYunkang Tang 	int reg_val, ret = -1;
25883808843cSYunkang Tang 
25893808843cSYunkang Tang 	if (alps_enter_command_mode(psmouse) ||
25903808843cSYunkang Tang 	    alps_command_mode_read_reg(psmouse, 0xc2d9) == -1)
25913808843cSYunkang Tang 		goto error;
25923808843cSYunkang Tang 
2593f3f33c67SHans de Goede 	if (alps_get_v3_v7_resolution(psmouse, 0xc397))
2594f3f33c67SHans de Goede 		goto error;
2595f3f33c67SHans de Goede 
25963808843cSYunkang Tang 	if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
25973808843cSYunkang Tang 		goto error;
25983808843cSYunkang Tang 
25993808843cSYunkang Tang 	reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
26003808843cSYunkang Tang 	if (reg_val == -1)
26013808843cSYunkang Tang 		goto error;
26023808843cSYunkang Tang 	if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
26033808843cSYunkang Tang 		goto error;
26043808843cSYunkang Tang 
26053808843cSYunkang Tang 	alps_exit_command_mode(psmouse);
26063808843cSYunkang Tang 	return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
26073808843cSYunkang Tang 
26083808843cSYunkang Tang error:
26093808843cSYunkang Tang 	alps_exit_command_mode(psmouse);
26103808843cSYunkang Tang 	return ret;
26113808843cSYunkang Tang }
26123808843cSYunkang Tang 
26133db5b9f7SMasaki Ota static int alps_hw_init_ss4_v2(struct psmouse *psmouse)
26143db5b9f7SMasaki Ota {
26153db5b9f7SMasaki Ota 	struct ps2dev *ps2dev = &psmouse->ps2dev;
26163db5b9f7SMasaki Ota 	char param[2] = {0x64, 0x28};
26173db5b9f7SMasaki Ota 	int ret = -1;
26183db5b9f7SMasaki Ota 
26193db5b9f7SMasaki Ota 	/* enter absolute mode */
26203db5b9f7SMasaki Ota 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
26213db5b9f7SMasaki Ota 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
26223db5b9f7SMasaki Ota 	    ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
26233db5b9f7SMasaki Ota 	    ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE)) {
26243db5b9f7SMasaki Ota 		goto error;
26253db5b9f7SMasaki Ota 	}
26263db5b9f7SMasaki Ota 
26273db5b9f7SMasaki Ota 	/* T.B.D. Decread noise packet number, delete in the future */
26283db5b9f7SMasaki Ota 	if (alps_exit_command_mode(psmouse) ||
26293db5b9f7SMasaki Ota 	    alps_enter_command_mode(psmouse) ||
26303db5b9f7SMasaki Ota 	    alps_command_mode_write_reg(psmouse, 0x001D, 0x20)) {
26313db5b9f7SMasaki Ota 		goto error;
26323db5b9f7SMasaki Ota 	}
26333db5b9f7SMasaki Ota 	alps_exit_command_mode(psmouse);
26343db5b9f7SMasaki Ota 
26353db5b9f7SMasaki Ota 	return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
26363db5b9f7SMasaki Ota 
26373db5b9f7SMasaki Ota error:
26383db5b9f7SMasaki Ota 	alps_exit_command_mode(psmouse);
26393db5b9f7SMasaki Ota 	return ret;
26403db5b9f7SMasaki Ota }
26413db5b9f7SMasaki Ota 
26423296f71cSDmitry Torokhov static int alps_set_protocol(struct psmouse *psmouse,
26433296f71cSDmitry Torokhov 			     struct alps_data *priv,
26443296f71cSDmitry Torokhov 			     const struct alps_protocol_info *protocol)
264525bded7cSSeth Forshee {
26463296f71cSDmitry Torokhov 	psmouse->private = priv;
26473296f71cSDmitry Torokhov 
26483296f71cSDmitry Torokhov 	setup_timer(&priv->timer, alps_flush_packet, (unsigned long)psmouse);
26493296f71cSDmitry Torokhov 
26503296f71cSDmitry Torokhov 	priv->proto_version = protocol->version;
26513296f71cSDmitry Torokhov 	priv->byte0 = protocol->byte0;
26523296f71cSDmitry Torokhov 	priv->mask0 = protocol->mask0;
26533296f71cSDmitry Torokhov 	priv->flags = protocol->flags;
2654f673ceb1SKevin Cernekee 
26557a9f73e7SKevin Cernekee 	priv->x_max = 2000;
26567a9f73e7SKevin Cernekee 	priv->y_max = 1400;
26577a9f73e7SKevin Cernekee 	priv->x_bits = 15;
26587a9f73e7SKevin Cernekee 	priv->y_bits = 11;
26597a9f73e7SKevin Cernekee 
266099df65e7SKevin Cernekee 	switch (priv->proto_version) {
266125bded7cSSeth Forshee 	case ALPS_PROTO_V1:
266225bded7cSSeth Forshee 	case ALPS_PROTO_V2:
266324af5cb9SKevin Cernekee 		priv->hw_init = alps_hw_init_v1_v2;
266424af5cb9SKevin Cernekee 		priv->process_packet = alps_process_packet_v1_v2;
266524af5cb9SKevin Cernekee 		priv->set_abs_params = alps_set_abs_params_st;
266695f75e91SYunkang Tang 		priv->x_max = 1023;
266795f75e91SYunkang Tang 		priv->y_max = 767;
266819556219SHans de Goede 		if (dmi_check_system(alps_dmi_has_separate_stick_buttons))
266919556219SHans de Goede 			priv->flags |= ALPS_STICK_BITS;
267025bded7cSSeth Forshee 		break;
2671fb2dd7a6SDmitry Torokhov 
267225bded7cSSeth Forshee 	case ALPS_PROTO_V3:
267324af5cb9SKevin Cernekee 		priv->hw_init = alps_hw_init_v3;
267424af5cb9SKevin Cernekee 		priv->process_packet = alps_process_packet_v3;
2675688ea364SHans de Goede 		priv->set_abs_params = alps_set_abs_params_semi_mt;
2676f85e5001SKevin Cernekee 		priv->decode_fields = alps_decode_pinnacle;
267750e8b216SKevin Cernekee 		priv->nibble_commands = alps_v3_nibble_commands;
267850e8b216SKevin Cernekee 		priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
26794b1af853SPali Rohár 
26804b1af853SPali Rohár 		if (alps_probe_trackstick_v3_v7(psmouse,
26814b1af853SPali Rohár 						ALPS_REG_BASE_PINNACLE) < 0)
26824b1af853SPali Rohár 			priv->flags &= ~ALPS_DUALPOINT;
26834b1af853SPali Rohár 
268425bded7cSSeth Forshee 		break;
2685fb2dd7a6SDmitry Torokhov 
2686fb2dd7a6SDmitry Torokhov 	case ALPS_PROTO_V3_RUSHMORE:
2687fb2dd7a6SDmitry Torokhov 		priv->hw_init = alps_hw_init_rushmore_v3;
2688fb2dd7a6SDmitry Torokhov 		priv->process_packet = alps_process_packet_v3;
2689688ea364SHans de Goede 		priv->set_abs_params = alps_set_abs_params_semi_mt;
2690fb2dd7a6SDmitry Torokhov 		priv->decode_fields = alps_decode_rushmore;
2691fb2dd7a6SDmitry Torokhov 		priv->nibble_commands = alps_v3_nibble_commands;
2692fb2dd7a6SDmitry Torokhov 		priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2693fb2dd7a6SDmitry Torokhov 		priv->x_bits = 16;
2694fb2dd7a6SDmitry Torokhov 		priv->y_bits = 12;
26953296f71cSDmitry Torokhov 
2696dae928ecSPali Rohár 		if (alps_probe_trackstick_v3_v7(psmouse,
26973296f71cSDmitry Torokhov 						ALPS_REG_BASE_RUSHMORE) < 0)
26983296f71cSDmitry Torokhov 			priv->flags &= ~ALPS_DUALPOINT;
26993296f71cSDmitry Torokhov 
2700fb2dd7a6SDmitry Torokhov 		break;
2701fb2dd7a6SDmitry Torokhov 
270225bded7cSSeth Forshee 	case ALPS_PROTO_V4:
270324af5cb9SKevin Cernekee 		priv->hw_init = alps_hw_init_v4;
270424af5cb9SKevin Cernekee 		priv->process_packet = alps_process_packet_v4;
2705688ea364SHans de Goede 		priv->set_abs_params = alps_set_abs_params_semi_mt;
270650e8b216SKevin Cernekee 		priv->nibble_commands = alps_v4_nibble_commands;
270750e8b216SKevin Cernekee 		priv->addr_command = PSMOUSE_CMD_DISABLE;
270825bded7cSSeth Forshee 		break;
27093296f71cSDmitry Torokhov 
271075af9e56SDave Turvene 	case ALPS_PROTO_V5:
271175af9e56SDave Turvene 		priv->hw_init = alps_hw_init_dolphin_v1;
2712ee65d4b3SYunkang Tang 		priv->process_packet = alps_process_touchpad_packet_v3_v5;
271375af9e56SDave Turvene 		priv->decode_fields = alps_decode_dolphin;
2714688ea364SHans de Goede 		priv->set_abs_params = alps_set_abs_params_semi_mt;
271575af9e56SDave Turvene 		priv->nibble_commands = alps_v3_nibble_commands;
271675af9e56SDave Turvene 		priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
271775af9e56SDave Turvene 		priv->x_bits = 23;
271875af9e56SDave Turvene 		priv->y_bits = 12;
2719c164c147SDmitry Torokhov 
2720c164c147SDmitry Torokhov 		if (alps_dolphin_get_device_area(psmouse, priv))
2721c164c147SDmitry Torokhov 			return -EIO;
2722c164c147SDmitry Torokhov 
272375af9e56SDave Turvene 		break;
27243296f71cSDmitry Torokhov 
272595f75e91SYunkang Tang 	case ALPS_PROTO_V6:
272695f75e91SYunkang Tang 		priv->hw_init = alps_hw_init_v6;
272795f75e91SYunkang Tang 		priv->process_packet = alps_process_packet_v6;
272895f75e91SYunkang Tang 		priv->set_abs_params = alps_set_abs_params_st;
272995f75e91SYunkang Tang 		priv->nibble_commands = alps_v6_nibble_commands;
273095f75e91SYunkang Tang 		priv->x_max = 2047;
273195f75e91SYunkang Tang 		priv->y_max = 1535;
273295f75e91SYunkang Tang 		break;
27333296f71cSDmitry Torokhov 
27343808843cSYunkang Tang 	case ALPS_PROTO_V7:
27353808843cSYunkang Tang 		priv->hw_init = alps_hw_init_v7;
27363808843cSYunkang Tang 		priv->process_packet = alps_process_packet_v7;
27373808843cSYunkang Tang 		priv->decode_fields = alps_decode_packet_v7;
27388eccd393SMasaki Ota 		priv->set_abs_params = alps_set_abs_params_v7;
27393808843cSYunkang Tang 		priv->nibble_commands = alps_v3_nibble_commands;
27403808843cSYunkang Tang 		priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2741c164c147SDmitry Torokhov 		priv->x_max = 0xfff;
2742c164c147SDmitry Torokhov 		priv->y_max = 0x7ff;
27433808843cSYunkang Tang 
27443808843cSYunkang Tang 		if (priv->fw_ver[1] != 0xba)
27453808843cSYunkang Tang 			priv->flags |= ALPS_BUTTONPAD;
27463296f71cSDmitry Torokhov 
2747dae928ecSPali Rohár 		if (alps_probe_trackstick_v3_v7(psmouse, ALPS_REG_BASE_V7) < 0)
2748dae928ecSPali Rohár 			priv->flags &= ~ALPS_DUALPOINT;
2749dae928ecSPali Rohár 
27503808843cSYunkang Tang 		break;
27513db5b9f7SMasaki Ota 
27523db5b9f7SMasaki Ota 	case ALPS_PROTO_V8:
27533db5b9f7SMasaki Ota 		priv->hw_init = alps_hw_init_ss4_v2;
27543db5b9f7SMasaki Ota 		priv->process_packet = alps_process_packet_ss4_v2;
27553db5b9f7SMasaki Ota 		priv->decode_fields = alps_decode_ss4_v2;
27563db5b9f7SMasaki Ota 		priv->set_abs_params = alps_set_abs_params_ss4_v2;
27573db5b9f7SMasaki Ota 		priv->nibble_commands = alps_v3_nibble_commands;
27583db5b9f7SMasaki Ota 		priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
27593db5b9f7SMasaki Ota 
27603db5b9f7SMasaki Ota 		if (alps_set_defaults_ss4_v2(psmouse, priv))
27613db5b9f7SMasaki Ota 			return -EIO;
27623db5b9f7SMasaki Ota 
2763aeaa881fSBen Gamari 		if (priv->fw_ver[1] == 0x1)
2764aeaa881fSBen Gamari 			priv->flags |= ALPS_DUALPOINT |
2765aeaa881fSBen Gamari 					ALPS_DUALPOINT_WITH_PRESSURE;
2766aeaa881fSBen Gamari 
27673db5b9f7SMasaki Ota 		break;
276825bded7cSSeth Forshee 	}
27693296f71cSDmitry Torokhov 
27703296f71cSDmitry Torokhov 	return 0;
277125bded7cSSeth Forshee }
277225bded7cSSeth Forshee 
27733296f71cSDmitry Torokhov static const struct alps_protocol_info *alps_match_table(unsigned char *e7,
27743296f71cSDmitry Torokhov 							 unsigned char *ec)
27752e992cc0SKevin Cernekee {
2776b5d6b851SKevin Cernekee 	const struct alps_model_info *model;
27772e992cc0SKevin Cernekee 	int i;
27782e992cc0SKevin Cernekee 
2779b5d6b851SKevin Cernekee 	for (i = 0; i < ARRAY_SIZE(alps_model_data); i++) {
2780b5d6b851SKevin Cernekee 		model = &alps_model_data[i];
2781b5d6b851SKevin Cernekee 
2782b5d6b851SKevin Cernekee 		if (!memcmp(e7, model->signature, sizeof(model->signature)) &&
2783b5d6b851SKevin Cernekee 		    (!model->command_mode_resp ||
2784b5d6b851SKevin Cernekee 		     model->command_mode_resp == ec[2])) {
2785b5d6b851SKevin Cernekee 
27863296f71cSDmitry Torokhov 			return &model->protocol_info;
2787b5d6b851SKevin Cernekee 		}
2788b5d6b851SKevin Cernekee 	}
2789b5d6b851SKevin Cernekee 
27903296f71cSDmitry Torokhov 	return NULL;
2791b5d6b851SKevin Cernekee }
2792b5d6b851SKevin Cernekee 
2793b5d6b851SKevin Cernekee static int alps_identify(struct psmouse *psmouse, struct alps_data *priv)
2794b5d6b851SKevin Cernekee {
27953296f71cSDmitry Torokhov 	const struct alps_protocol_info *protocol;
2796b5d6b851SKevin Cernekee 	unsigned char e6[4], e7[4], ec[4];
2797a09221e8SDmitry Torokhov 	int error;
2798b5d6b851SKevin Cernekee 
27992e992cc0SKevin Cernekee 	/*
28002e992cc0SKevin Cernekee 	 * First try "E6 report".
28012e992cc0SKevin Cernekee 	 * ALPS should return 0,0,10 or 0,0,100 if no buttons are pressed.
28022e992cc0SKevin Cernekee 	 * The bits 0-2 of the first byte will be 1s if some buttons are
28032e992cc0SKevin Cernekee 	 * pressed.
28042e992cc0SKevin Cernekee 	 */
2805b5d6b851SKevin Cernekee 	if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2806b5d6b851SKevin Cernekee 			 PSMOUSE_CMD_SETSCALE11, e6))
2807b5d6b851SKevin Cernekee 		return -EIO;
28082e992cc0SKevin Cernekee 
2809b5d6b851SKevin Cernekee 	if ((e6[0] & 0xf8) != 0 || e6[1] != 0 || (e6[2] != 10 && e6[2] != 100))
2810b5d6b851SKevin Cernekee 		return -EINVAL;
28112e992cc0SKevin Cernekee 
28122e992cc0SKevin Cernekee 	/*
2813b5d6b851SKevin Cernekee 	 * Now get the "E7" and "EC" reports.  These will uniquely identify
2814b5d6b851SKevin Cernekee 	 * most ALPS touchpads.
28152e992cc0SKevin Cernekee 	 */
2816b5d6b851SKevin Cernekee 	if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2817b5d6b851SKevin Cernekee 			 PSMOUSE_CMD_SETSCALE21, e7) ||
2818b5d6b851SKevin Cernekee 	    alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2819b5d6b851SKevin Cernekee 			 PSMOUSE_CMD_RESET_WRAP, ec) ||
2820b5d6b851SKevin Cernekee 	    alps_exit_command_mode(psmouse))
2821b5d6b851SKevin Cernekee 		return -EIO;
28222e992cc0SKevin Cernekee 
28233296f71cSDmitry Torokhov 	protocol = alps_match_table(e7, ec);
28243296f71cSDmitry Torokhov 	if (!protocol) {
28253296f71cSDmitry Torokhov 		if (e7[0] == 0x73 && e7[1] == 0x03 && e7[2] == 0x50 &&
28263296f71cSDmitry Torokhov 			   ec[0] == 0x73 && (ec[1] == 0x01 || ec[1] == 0x02)) {
28273296f71cSDmitry Torokhov 			protocol = &alps_v5_protocol_data;
28283296f71cSDmitry Torokhov 		} else if (ec[0] == 0x88 &&
28293296f71cSDmitry Torokhov 			   ((ec[1] & 0xf0) == 0xb0 || (ec[1] & 0xf0) == 0xc0)) {
28303296f71cSDmitry Torokhov 			protocol = &alps_v7_protocol_data;
28313296f71cSDmitry Torokhov 		} else if (ec[0] == 0x88 && ec[1] == 0x08) {
28323296f71cSDmitry Torokhov 			protocol = &alps_v3_rushmore_data;
28333296f71cSDmitry Torokhov 		} else if (ec[0] == 0x88 && ec[1] == 0x07 &&
28343296f71cSDmitry Torokhov 			   ec[2] >= 0x90 && ec[2] <= 0x9d) {
28353296f71cSDmitry Torokhov 			protocol = &alps_v3_protocol_data;
28363db5b9f7SMasaki Ota 		} else if (e7[0] == 0x73 && e7[1] == 0x03 &&
28373db5b9f7SMasaki Ota 			   e7[2] == 0x14 && ec[1] == 0x02) {
28383db5b9f7SMasaki Ota 			protocol = &alps_v8_protocol_data;
2839aeaa881fSBen Gamari 		} else if (e7[0] == 0x73 && e7[1] == 0x03 &&
2840aeaa881fSBen Gamari 			   e7[2] == 0x28 && ec[1] == 0x01) {
2841aeaa881fSBen Gamari 			protocol = &alps_v8_protocol_data;
28423296f71cSDmitry Torokhov 		} else {
28433296f71cSDmitry Torokhov 			psmouse_dbg(psmouse,
28443296f71cSDmitry Torokhov 				    "Likely not an ALPS touchpad: E7=%3ph, EC=%3ph\n", e7, ec);
28453296f71cSDmitry Torokhov 			return -EINVAL;
28463296f71cSDmitry Torokhov 		}
28473296f71cSDmitry Torokhov 	}
28483296f71cSDmitry Torokhov 
2849a09221e8SDmitry Torokhov 	if (priv) {
2850c0cd17f6SHans de Goede 		/* Save the Firmware version */
2851c0cd17f6SHans de Goede 		memcpy(priv->fw_ver, ec, 3);
2852a09221e8SDmitry Torokhov 		error = alps_set_protocol(psmouse, priv, protocol);
2853a09221e8SDmitry Torokhov 		if (error)
2854a09221e8SDmitry Torokhov 			return error;
2855a09221e8SDmitry Torokhov 	}
2856c0cd17f6SHans de Goede 
2857a09221e8SDmitry Torokhov 	return 0;
28582e992cc0SKevin Cernekee }
28592e992cc0SKevin Cernekee 
28601e0c5b12SDmitry Torokhov static int alps_reconnect(struct psmouse *psmouse)
28611e0c5b12SDmitry Torokhov {
2862b5d6b851SKevin Cernekee 	struct alps_data *priv = psmouse->private;
286371bb21b6SMaxim Levitsky 
28641e0c5b12SDmitry Torokhov 	psmouse_reset(psmouse);
28651e0c5b12SDmitry Torokhov 
2866b5d6b851SKevin Cernekee 	if (alps_identify(psmouse, priv) < 0)
28671e0c5b12SDmitry Torokhov 		return -1;
28681e0c5b12SDmitry Torokhov 
286924af5cb9SKevin Cernekee 	return priv->hw_init(psmouse);
28701da177e4SLinus Torvalds }
28711da177e4SLinus Torvalds 
28721da177e4SLinus Torvalds static void alps_disconnect(struct psmouse *psmouse)
28731da177e4SLinus Torvalds {
28741da177e4SLinus Torvalds 	struct alps_data *priv = psmouse->private;
28752e5b636bSDmitry Torokhov 
28761da177e4SLinus Torvalds 	psmouse_reset(psmouse);
28771d9f2626SSebastian Kapfer 	del_timer_sync(&priv->timer);
287804aae283SPali Rohár 	if (priv->dev2)
28792e5b636bSDmitry Torokhov 		input_unregister_device(priv->dev2);
288004aae283SPali Rohár 	if (!IS_ERR_OR_NULL(priv->dev3))
288104aae283SPali Rohár 		input_unregister_device(priv->dev3);
28821da177e4SLinus Torvalds 	kfree(priv);
28831da177e4SLinus Torvalds }
28841da177e4SLinus Torvalds 
288524af5cb9SKevin Cernekee static void alps_set_abs_params_st(struct alps_data *priv,
288624af5cb9SKevin Cernekee 				   struct input_dev *dev1)
288724af5cb9SKevin Cernekee {
288895f75e91SYunkang Tang 	input_set_abs_params(dev1, ABS_X, 0, priv->x_max, 0, 0);
288995f75e91SYunkang Tang 	input_set_abs_params(dev1, ABS_Y, 0, priv->y_max, 0, 0);
28908eccd393SMasaki Ota 	input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
289124af5cb9SKevin Cernekee }
289224af5cb9SKevin Cernekee 
28938eccd393SMasaki Ota static void alps_set_abs_params_mt_common(struct alps_data *priv,
289424af5cb9SKevin Cernekee 					  struct input_dev *dev1)
289524af5cb9SKevin Cernekee {
28967a9f73e7SKevin Cernekee 	input_set_abs_params(dev1, ABS_MT_POSITION_X, 0, priv->x_max, 0, 0);
28977a9f73e7SKevin Cernekee 	input_set_abs_params(dev1, ABS_MT_POSITION_Y, 0, priv->y_max, 0, 0);
289824af5cb9SKevin Cernekee 
2899f3f33c67SHans de Goede 	input_abs_set_res(dev1, ABS_MT_POSITION_X, priv->x_res);
2900f3f33c67SHans de Goede 	input_abs_set_res(dev1, ABS_MT_POSITION_Y, priv->y_res);
2901f3f33c67SHans de Goede 
290224af5cb9SKevin Cernekee 	set_bit(BTN_TOOL_TRIPLETAP, dev1->keybit);
290324af5cb9SKevin Cernekee 	set_bit(BTN_TOOL_QUADTAP, dev1->keybit);
29048eccd393SMasaki Ota }
29053808843cSYunkang Tang 
2906688ea364SHans de Goede static void alps_set_abs_params_semi_mt(struct alps_data *priv,
29078eccd393SMasaki Ota 					struct input_dev *dev1)
29088eccd393SMasaki Ota {
29098eccd393SMasaki Ota 	alps_set_abs_params_mt_common(priv, dev1);
29108eccd393SMasaki Ota 	input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
29118eccd393SMasaki Ota 
29128eccd393SMasaki Ota 	input_mt_init_slots(dev1, MAX_TOUCHES,
29138eccd393SMasaki Ota 			    INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
29141662c033SHans de Goede 				INPUT_MT_SEMI_MT);
29158eccd393SMasaki Ota }
29168eccd393SMasaki Ota 
29178eccd393SMasaki Ota static void alps_set_abs_params_v7(struct alps_data *priv,
29188eccd393SMasaki Ota 				   struct input_dev *dev1)
29198eccd393SMasaki Ota {
29208eccd393SMasaki Ota 	alps_set_abs_params_mt_common(priv, dev1);
29218d289842SMasaki Ota 	set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
29228eccd393SMasaki Ota 
29238eccd393SMasaki Ota 	input_mt_init_slots(dev1, MAX_TOUCHES,
29248eccd393SMasaki Ota 			    INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
29258eccd393SMasaki Ota 				INPUT_MT_TRACK);
29263db5b9f7SMasaki Ota 
29273db5b9f7SMasaki Ota 	set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
29283db5b9f7SMasaki Ota }
29293db5b9f7SMasaki Ota 
29303db5b9f7SMasaki Ota static void alps_set_abs_params_ss4_v2(struct alps_data *priv,
29313db5b9f7SMasaki Ota 				       struct input_dev *dev1)
29323db5b9f7SMasaki Ota {
29333db5b9f7SMasaki Ota 	alps_set_abs_params_mt_common(priv, dev1);
29343db5b9f7SMasaki Ota 	input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
29353db5b9f7SMasaki Ota 	set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
29363db5b9f7SMasaki Ota 
29373db5b9f7SMasaki Ota 	input_mt_init_slots(dev1, MAX_TOUCHES,
29383db5b9f7SMasaki Ota 			    INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
29393db5b9f7SMasaki Ota 				INPUT_MT_TRACK);
294024af5cb9SKevin Cernekee }
294124af5cb9SKevin Cernekee 
29421da177e4SLinus Torvalds int alps_init(struct psmouse *psmouse)
29431da177e4SLinus Torvalds {
2944a09221e8SDmitry Torokhov 	struct alps_data *priv = psmouse->private;
294504aae283SPali Rohár 	struct input_dev *dev1 = psmouse->dev;
2946a09221e8SDmitry Torokhov 	int error;
29471da177e4SLinus Torvalds 
2948a09221e8SDmitry Torokhov 	error = priv->hw_init(psmouse);
2949a09221e8SDmitry Torokhov 	if (error)
29501da177e4SLinus Torvalds 		goto init_fail;
29511da177e4SLinus Torvalds 
29527105d2eaSDmitry Torokhov 	/*
29537105d2eaSDmitry Torokhov 	 * Undo part of setup done for us by psmouse core since touchpad
29547105d2eaSDmitry Torokhov 	 * is not a relative device.
29557105d2eaSDmitry Torokhov 	 */
29567105d2eaSDmitry Torokhov 	__clear_bit(EV_REL, dev1->evbit);
29577105d2eaSDmitry Torokhov 	__clear_bit(REL_X, dev1->relbit);
29587105d2eaSDmitry Torokhov 	__clear_bit(REL_Y, dev1->relbit);
29597105d2eaSDmitry Torokhov 
29607105d2eaSDmitry Torokhov 	/*
29617105d2eaSDmitry Torokhov 	 * Now set up our capabilities.
29627105d2eaSDmitry Torokhov 	 */
29637b19ada2SJiri Slaby 	dev1->evbit[BIT_WORD(EV_KEY)] |= BIT_MASK(EV_KEY);
29647b19ada2SJiri Slaby 	dev1->keybit[BIT_WORD(BTN_TOUCH)] |= BIT_MASK(BTN_TOUCH);
29657b19ada2SJiri Slaby 	dev1->keybit[BIT_WORD(BTN_TOOL_FINGER)] |= BIT_MASK(BTN_TOOL_FINGER);
296671bb21b6SMaxim Levitsky 	dev1->keybit[BIT_WORD(BTN_LEFT)] |=
296771bb21b6SMaxim Levitsky 		BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
29681da177e4SLinus Torvalds 
29697b19ada2SJiri Slaby 	dev1->evbit[BIT_WORD(EV_ABS)] |= BIT_MASK(EV_ABS);
297025bded7cSSeth Forshee 
297124af5cb9SKevin Cernekee 	priv->set_abs_params(priv, dev1);
29721da177e4SLinus Torvalds 
297399df65e7SKevin Cernekee 	if (priv->flags & ALPS_WHEEL) {
29747b19ada2SJiri Slaby 		dev1->evbit[BIT_WORD(EV_REL)] |= BIT_MASK(EV_REL);
29757b19ada2SJiri Slaby 		dev1->relbit[BIT_WORD(REL_WHEEL)] |= BIT_MASK(REL_WHEEL);
29761da177e4SLinus Torvalds 	}
29771da177e4SLinus Torvalds 
297899df65e7SKevin Cernekee 	if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
29797b19ada2SJiri Slaby 		dev1->keybit[BIT_WORD(BTN_FORWARD)] |= BIT_MASK(BTN_FORWARD);
29807b19ada2SJiri Slaby 		dev1->keybit[BIT_WORD(BTN_BACK)] |= BIT_MASK(BTN_BACK);
29811da177e4SLinus Torvalds 	}
29821da177e4SLinus Torvalds 
298399df65e7SKevin Cernekee 	if (priv->flags & ALPS_FOUR_BUTTONS) {
298471bb21b6SMaxim Levitsky 		dev1->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_0);
298571bb21b6SMaxim Levitsky 		dev1->keybit[BIT_WORD(BTN_1)] |= BIT_MASK(BTN_1);
298671bb21b6SMaxim Levitsky 		dev1->keybit[BIT_WORD(BTN_2)] |= BIT_MASK(BTN_2);
298771bb21b6SMaxim Levitsky 		dev1->keybit[BIT_WORD(BTN_3)] |= BIT_MASK(BTN_3);
29883808843cSYunkang Tang 	} else if (priv->flags & ALPS_BUTTONPAD) {
29893808843cSYunkang Tang 		set_bit(INPUT_PROP_BUTTONPAD, dev1->propbit);
29903808843cSYunkang Tang 		clear_bit(BTN_RIGHT, dev1->keybit);
299171bb21b6SMaxim Levitsky 	} else {
299271bb21b6SMaxim Levitsky 		dev1->keybit[BIT_WORD(BTN_MIDDLE)] |= BIT_MASK(BTN_MIDDLE);
299371bb21b6SMaxim Levitsky 	}
299471bb21b6SMaxim Levitsky 
2995dfba8600SPali Rohár 	if (priv->flags & ALPS_DUALPOINT) {
299604aae283SPali Rohár 		struct input_dev *dev2;
299704aae283SPali Rohár 
299804aae283SPali Rohár 		dev2 = input_allocate_device();
299904aae283SPali Rohár 		if (!dev2) {
300004aae283SPali Rohár 			psmouse_err(psmouse,
300104aae283SPali Rohár 				    "failed to allocate trackstick device\n");
300204aae283SPali Rohár 			error = -ENOMEM;
300304aae283SPali Rohár 			goto init_fail;
300404aae283SPali Rohár 		}
300504aae283SPali Rohár 
300604aae283SPali Rohár 		snprintf(priv->phys2, sizeof(priv->phys2), "%s/input1",
300704aae283SPali Rohár 			 psmouse->ps2dev.serio->phys);
300804aae283SPali Rohár 		dev2->phys = priv->phys2;
300904aae283SPali Rohár 
3010dfba8600SPali Rohár 		/*
3011dfba8600SPali Rohár 		 * format of input device name is: "protocol vendor name"
3012dfba8600SPali Rohár 		 * see function psmouse_switch_protocol() in psmouse-base.c
3013dfba8600SPali Rohár 		 */
3014dfba8600SPali Rohár 		dev2->name = "AlpsPS/2 ALPS DualPoint Stick";
3015dfba8600SPali Rohár 
30162e5b636bSDmitry Torokhov 		dev2->id.bustype = BUS_I8042;
30172e5b636bSDmitry Torokhov 		dev2->id.vendor  = 0x0002;
301804aae283SPali Rohár 		dev2->id.product = PSMOUSE_ALPS;
301904aae283SPali Rohár 		dev2->id.version = priv->proto_version;
30201db3a345SDmitry Torokhov 		dev2->dev.parent = &psmouse->ps2dev.serio->dev;
30211da177e4SLinus Torvalds 
302204aae283SPali Rohár 		input_set_capability(dev2, EV_REL, REL_X);
302304aae283SPali Rohár 		input_set_capability(dev2, EV_REL, REL_Y);
30247ad8a106SBen Gamari 		if (priv->flags & ALPS_DUALPOINT_WITH_PRESSURE) {
30257ad8a106SBen Gamari 			input_set_capability(dev2, EV_ABS, ABS_PRESSURE);
30267ad8a106SBen Gamari 			input_set_abs_params(dev2, ABS_PRESSURE, 0, 127, 0, 0);
30277ad8a106SBen Gamari 		}
302804aae283SPali Rohár 		input_set_capability(dev2, EV_KEY, BTN_LEFT);
302904aae283SPali Rohár 		input_set_capability(dev2, EV_KEY, BTN_RIGHT);
303004aae283SPali Rohár 		input_set_capability(dev2, EV_KEY, BTN_MIDDLE);
30311da177e4SLinus Torvalds 
303201d4cd5cSHans de Goede 		__set_bit(INPUT_PROP_POINTER, dev2->propbit);
30337611392fSHans de Goede 		__set_bit(INPUT_PROP_POINTING_STICK, dev2->propbit);
30347611392fSHans de Goede 
303504aae283SPali Rohár 		error = input_register_device(dev2);
303604aae283SPali Rohár 		if (error) {
303704aae283SPali Rohár 			psmouse_err(psmouse,
303804aae283SPali Rohár 				    "failed to register trackstick device: %d\n",
303904aae283SPali Rohár 				    error);
304004aae283SPali Rohár 			input_free_device(dev2);
3041f42649e8SDmitry Torokhov 			goto init_fail;
304204aae283SPali Rohár 		}
304304aae283SPali Rohár 
304404aae283SPali Rohár 		priv->dev2 = dev2;
304504aae283SPali Rohár 	}
304604aae283SPali Rohár 
304704aae283SPali Rohár 	priv->psmouse = psmouse;
304804aae283SPali Rohár 
304904aae283SPali Rohár 	INIT_DELAYED_WORK(&priv->dev3_register_work,
305004aae283SPali Rohár 			  alps_register_bare_ps2_mouse);
30511da177e4SLinus Torvalds 
30521da177e4SLinus Torvalds 	psmouse->protocol_handler = alps_process_byte;
3053f0d5c6f4SDmitry Torokhov 	psmouse->poll = alps_poll;
30541da177e4SLinus Torvalds 	psmouse->disconnect = alps_disconnect;
30551da177e4SLinus Torvalds 	psmouse->reconnect = alps_reconnect;
305699df65e7SKevin Cernekee 	psmouse->pktsize = priv->proto_version == ALPS_PROTO_V4 ? 8 : 6;
30571da177e4SLinus Torvalds 
3058f0d5c6f4SDmitry Torokhov 	/* We are having trouble resyncing ALPS touchpads so disable it for now */
3059f0d5c6f4SDmitry Torokhov 	psmouse->resync_time = 0;
3060f0d5c6f4SDmitry Torokhov 
30619d720b34SPali Rohár 	/* Allow 2 invalid packets without resetting device */
30629d720b34SPali Rohár 	psmouse->resetafter = psmouse->pktsize * 2;
30639d720b34SPali Rohár 
30641da177e4SLinus Torvalds 	return 0;
30651da177e4SLinus Torvalds 
30661da177e4SLinus Torvalds init_fail:
3067f42649e8SDmitry Torokhov 	psmouse_reset(psmouse);
3068a09221e8SDmitry Torokhov 	/*
3069a09221e8SDmitry Torokhov 	 * Even though we did not allocate psmouse->private we do free
3070a09221e8SDmitry Torokhov 	 * it here.
3071a09221e8SDmitry Torokhov 	 */
3072a09221e8SDmitry Torokhov 	kfree(psmouse->private);
30731e0c5b12SDmitry Torokhov 	psmouse->private = NULL;
3074a09221e8SDmitry Torokhov 	return error;
30751da177e4SLinus Torvalds }
30761da177e4SLinus Torvalds 
3077b7802c5cSDmitry Torokhov int alps_detect(struct psmouse *psmouse, bool set_properties)
30781da177e4SLinus Torvalds {
3079a09221e8SDmitry Torokhov 	struct alps_data *priv;
3080a09221e8SDmitry Torokhov 	int error;
30811da177e4SLinus Torvalds 
3082a09221e8SDmitry Torokhov 	error = alps_identify(psmouse, NULL);
3083a09221e8SDmitry Torokhov 	if (error)
3084a09221e8SDmitry Torokhov 		return error;
3085a09221e8SDmitry Torokhov 
3086a09221e8SDmitry Torokhov 	/*
3087a09221e8SDmitry Torokhov 	 * Reset the device to make sure it is fully operational:
3088a09221e8SDmitry Torokhov 	 * on some laptops, like certain Dell Latitudes, we may
3089a09221e8SDmitry Torokhov 	 * fail to properly detect presence of trackstick if device
3090a09221e8SDmitry Torokhov 	 * has not been reset.
3091a09221e8SDmitry Torokhov 	 */
3092a09221e8SDmitry Torokhov 	psmouse_reset(psmouse);
3093a09221e8SDmitry Torokhov 
3094a09221e8SDmitry Torokhov 	priv = kzalloc(sizeof(struct alps_data), GFP_KERNEL);
3095a09221e8SDmitry Torokhov 	if (!priv)
3096a09221e8SDmitry Torokhov 		return -ENOMEM;
3097a09221e8SDmitry Torokhov 
3098a09221e8SDmitry Torokhov 	error = alps_identify(psmouse, priv);
309993050db2SDmitry Torokhov 	if (error) {
310093050db2SDmitry Torokhov 		kfree(priv);
3101a09221e8SDmitry Torokhov 		return error;
310293050db2SDmitry Torokhov 	}
31031da177e4SLinus Torvalds 
31041da177e4SLinus Torvalds 	if (set_properties) {
31051da177e4SLinus Torvalds 		psmouse->vendor = "ALPS";
3106a09221e8SDmitry Torokhov 		psmouse->name = priv->flags & ALPS_DUALPOINT ?
3107968ac842SDmitry Torokhov 				"DualPoint TouchPad" : "GlidePoint";
3108a09221e8SDmitry Torokhov 		psmouse->model = priv->proto_version;
3109a09221e8SDmitry Torokhov 	} else {
3110a09221e8SDmitry Torokhov 		/*
3111a09221e8SDmitry Torokhov 		 * Destroy alps_data structure we allocated earlier since
3112a09221e8SDmitry Torokhov 		 * this was just a "trial run". Otherwise we'll keep it
3113a09221e8SDmitry Torokhov 		 * to be used by alps_init() which has to be called if
3114a09221e8SDmitry Torokhov 		 * we succeed and set_properties is true.
3115a09221e8SDmitry Torokhov 		 */
3116a09221e8SDmitry Torokhov 		kfree(priv);
3117a09221e8SDmitry Torokhov 		psmouse->private = NULL;
31181da177e4SLinus Torvalds 	}
3119a09221e8SDmitry Torokhov 
31201da177e4SLinus Torvalds 	return 0;
31211da177e4SLinus Torvalds }
31221da177e4SLinus Torvalds 
3123