Linux Audio

Check our new training course

Loading...
v6.8
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * ALPS touchpad PS/2 mouse driver
   4 *
   5 * Copyright (c) 2003 Neil Brown <neilb@cse.unsw.edu.au>
   6 * Copyright (c) 2003-2005 Peter Osterlund <petero2@telia.com>
   7 * Copyright (c) 2004 Dmitry Torokhov <dtor@mail.ru>
   8 * Copyright (c) 2005 Vojtech Pavlik <vojtech@suse.cz>
   9 * Copyright (c) 2009 Sebastian Kapfer <sebastian_kapfer@gmx.net>
  10 *
  11 * ALPS detection, tap switching and status querying info is taken from
  12 * tpconfig utility (by C. Scott Ananian and Bruce Kall).
 
 
 
 
  13 */
  14
  15#include <linux/slab.h>
  16#include <linux/input.h>
  17#include <linux/input/mt.h>
  18#include <linux/serio.h>
  19#include <linux/libps2.h>
  20#include <linux/dmi.h>
  21
  22#include "psmouse.h"
  23#include "alps.h"
  24#include "trackpoint.h"
  25
  26/*
  27 * Definitions for ALPS version 3 and 4 command mode protocol
  28 */
  29#define ALPS_CMD_NIBBLE_10	0x01f2
  30
  31#define ALPS_REG_BASE_RUSHMORE	0xc2c0
  32#define ALPS_REG_BASE_V7	0xc2c0
  33#define ALPS_REG_BASE_PINNACLE	0x0000
  34
  35static const struct alps_nibble_commands alps_v3_nibble_commands[] = {
  36	{ PSMOUSE_CMD_SETPOLL,		0x00 }, /* 0 */
  37	{ PSMOUSE_CMD_RESET_DIS,	0x00 }, /* 1 */
  38	{ PSMOUSE_CMD_SETSCALE21,	0x00 }, /* 2 */
  39	{ PSMOUSE_CMD_SETRATE,		0x0a }, /* 3 */
  40	{ PSMOUSE_CMD_SETRATE,		0x14 }, /* 4 */
  41	{ PSMOUSE_CMD_SETRATE,		0x28 }, /* 5 */
  42	{ PSMOUSE_CMD_SETRATE,		0x3c }, /* 6 */
  43	{ PSMOUSE_CMD_SETRATE,		0x50 }, /* 7 */
  44	{ PSMOUSE_CMD_SETRATE,		0x64 }, /* 8 */
  45	{ PSMOUSE_CMD_SETRATE,		0xc8 }, /* 9 */
  46	{ ALPS_CMD_NIBBLE_10,		0x00 }, /* a */
  47	{ PSMOUSE_CMD_SETRES,		0x00 }, /* b */
  48	{ PSMOUSE_CMD_SETRES,		0x01 }, /* c */
  49	{ PSMOUSE_CMD_SETRES,		0x02 }, /* d */
  50	{ PSMOUSE_CMD_SETRES,		0x03 }, /* e */
  51	{ PSMOUSE_CMD_SETSCALE11,	0x00 }, /* f */
  52};
  53
  54static const struct alps_nibble_commands alps_v4_nibble_commands[] = {
  55	{ PSMOUSE_CMD_ENABLE,		0x00 }, /* 0 */
  56	{ PSMOUSE_CMD_RESET_DIS,	0x00 }, /* 1 */
  57	{ PSMOUSE_CMD_SETSCALE21,	0x00 }, /* 2 */
  58	{ PSMOUSE_CMD_SETRATE,		0x0a }, /* 3 */
  59	{ PSMOUSE_CMD_SETRATE,		0x14 }, /* 4 */
  60	{ PSMOUSE_CMD_SETRATE,		0x28 }, /* 5 */
  61	{ PSMOUSE_CMD_SETRATE,		0x3c }, /* 6 */
  62	{ PSMOUSE_CMD_SETRATE,		0x50 }, /* 7 */
  63	{ PSMOUSE_CMD_SETRATE,		0x64 }, /* 8 */
  64	{ PSMOUSE_CMD_SETRATE,		0xc8 }, /* 9 */
  65	{ ALPS_CMD_NIBBLE_10,		0x00 }, /* a */
  66	{ PSMOUSE_CMD_SETRES,		0x00 }, /* b */
  67	{ PSMOUSE_CMD_SETRES,		0x01 }, /* c */
  68	{ PSMOUSE_CMD_SETRES,		0x02 }, /* d */
  69	{ PSMOUSE_CMD_SETRES,		0x03 }, /* e */
  70	{ PSMOUSE_CMD_SETSCALE11,	0x00 }, /* f */
  71};
  72
  73static const struct alps_nibble_commands alps_v6_nibble_commands[] = {
  74	{ PSMOUSE_CMD_ENABLE,		0x00 }, /* 0 */
  75	{ PSMOUSE_CMD_SETRATE,		0x0a }, /* 1 */
  76	{ PSMOUSE_CMD_SETRATE,		0x14 }, /* 2 */
  77	{ PSMOUSE_CMD_SETRATE,		0x28 }, /* 3 */
  78	{ PSMOUSE_CMD_SETRATE,		0x3c }, /* 4 */
  79	{ PSMOUSE_CMD_SETRATE,		0x50 }, /* 5 */
  80	{ PSMOUSE_CMD_SETRATE,		0x64 }, /* 6 */
  81	{ PSMOUSE_CMD_SETRATE,		0xc8 }, /* 7 */
  82	{ PSMOUSE_CMD_GETID,		0x00 }, /* 8 */
  83	{ PSMOUSE_CMD_GETINFO,		0x00 }, /* 9 */
  84	{ PSMOUSE_CMD_SETRES,		0x00 }, /* a */
  85	{ PSMOUSE_CMD_SETRES,		0x01 }, /* b */
  86	{ PSMOUSE_CMD_SETRES,		0x02 }, /* c */
  87	{ PSMOUSE_CMD_SETRES,		0x03 }, /* d */
  88	{ PSMOUSE_CMD_SETSCALE21,	0x00 }, /* e */
  89	{ PSMOUSE_CMD_SETSCALE11,	0x00 }, /* f */
  90};
  91
  92
  93#define ALPS_DUALPOINT		0x02	/* touchpad has trackstick */
  94#define ALPS_PASS		0x04	/* device has a pass-through port */
  95
  96#define ALPS_WHEEL		0x08	/* hardware wheel present */
  97#define ALPS_FW_BK_1		0x10	/* front & back buttons present */
  98#define ALPS_FW_BK_2		0x20	/* front & back buttons present */
  99#define ALPS_FOUR_BUTTONS	0x40	/* 4 direction button present */
 100#define ALPS_PS2_INTERLEAVED	0x80	/* 3-byte PS/2 packet interleaved with
 101					   6-byte ALPS packet */
 102#define ALPS_STICK_BITS		0x100	/* separate stick button bits */
 103#define ALPS_BUTTONPAD		0x200	/* device is a clickpad */
 104#define ALPS_DUALPOINT_WITH_PRESSURE	0x400	/* device can report trackpoint pressure */
 105
 106static const struct alps_model_info alps_model_data[] = {
 107	/*
 108	 * XXX This entry is suspicious. First byte has zero lower nibble,
 109	 * which is what a normal mouse would report. Also, the value 0x0e
 110	 * isn't valid per PS/2 spec.
 111	 */
 112	{ { 0x20, 0x02, 0x0e }, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } },
 113
 114	{ { 0x22, 0x02, 0x0a }, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } },
 115	{ { 0x22, 0x02, 0x14 }, { ALPS_PROTO_V2, 0xff, 0xff, ALPS_PASS | ALPS_DUALPOINT } },	/* Dell Latitude D600 */
 116	{ { 0x32, 0x02, 0x14 }, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } },	/* Toshiba Salellite Pro M10 */
 117	{ { 0x33, 0x02, 0x0a }, { ALPS_PROTO_V1, 0x88, 0xf8, 0 } },				/* UMAX-530T */
 118	{ { 0x52, 0x01, 0x14 }, { ALPS_PROTO_V2, 0xff, 0xff,
 119		ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED } },				/* Toshiba Tecra A11-11L */
 120	{ { 0x53, 0x02, 0x0a }, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
 121	{ { 0x53, 0x02, 0x14 }, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
 122	{ { 0x60, 0x03, 0xc8 }, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },				/* HP ze1115 */
 123	{ { 0x62, 0x02, 0x14 }, { ALPS_PROTO_V2, 0xcf, 0xcf,
 124		ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED } },				/* Dell Latitude E5500, E6400, E6500, Precision M4400 */
 125	{ { 0x63, 0x02, 0x0a }, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
 126	{ { 0x63, 0x02, 0x14 }, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
 127	{ { 0x63, 0x02, 0x28 }, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 } },			/* Fujitsu Siemens S6010 */
 128	{ { 0x63, 0x02, 0x3c }, { ALPS_PROTO_V2, 0x8f, 0x8f, ALPS_WHEEL } },			/* Toshiba Satellite S2400-103 */
 129	{ { 0x63, 0x02, 0x50 }, { ALPS_PROTO_V2, 0xef, 0xef, ALPS_FW_BK_1 } },			/* NEC Versa L320 */
 130	{ { 0x63, 0x02, 0x64 }, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
 131	{ { 0x63, 0x03, 0xc8 }, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } },	/* Dell Latitude D800 */
 132	{ { 0x73, 0x00, 0x0a }, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_DUALPOINT } },		/* ThinkPad R61 8918-5QG */
 133	{ { 0x73, 0x00, 0x14 }, { ALPS_PROTO_V6, 0xff, 0xff, ALPS_DUALPOINT } },		/* Dell XT2 */
 134	{ { 0x73, 0x02, 0x0a }, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
 135	{ { 0x73, 0x02, 0x14 }, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 } },			/* Ahtec Laptop */
 136	{ { 0x73, 0x02, 0x50 }, { ALPS_PROTO_V2, 0xcf, 0xcf, ALPS_FOUR_BUTTONS } },		/* Dell Vostro 1400 */
 137};
 138
 139static const struct alps_protocol_info alps_v3_protocol_data = {
 140	ALPS_PROTO_V3, 0x8f, 0x8f, ALPS_DUALPOINT | ALPS_DUALPOINT_WITH_PRESSURE
 141};
 142
 143static const struct alps_protocol_info alps_v3_rushmore_data = {
 144	ALPS_PROTO_V3_RUSHMORE, 0x8f, 0x8f, ALPS_DUALPOINT | ALPS_DUALPOINT_WITH_PRESSURE
 145};
 146
 147static const struct alps_protocol_info alps_v4_protocol_data = {
 148	ALPS_PROTO_V4, 0x8f, 0x8f, 0
 149};
 150
 151static const struct alps_protocol_info alps_v5_protocol_data = {
 152	ALPS_PROTO_V5, 0xc8, 0xd8, 0
 153};
 154
 155static const struct alps_protocol_info alps_v7_protocol_data = {
 156	ALPS_PROTO_V7, 0x48, 0x48, ALPS_DUALPOINT | ALPS_DUALPOINT_WITH_PRESSURE
 157};
 158
 159static const struct alps_protocol_info alps_v8_protocol_data = {
 160	ALPS_PROTO_V8, 0x18, 0x18, 0
 161};
 162
 163static const struct alps_protocol_info alps_v9_protocol_data = {
 164	ALPS_PROTO_V9, 0xc8, 0xc8, 0
 165};
 166
 167/*
 168 * Some v2 models report the stick buttons in separate bits
 169 */
 170static const struct dmi_system_id alps_dmi_has_separate_stick_buttons[] = {
 171#if defined(CONFIG_DMI) && defined(CONFIG_X86)
 172	{
 173		/* Extrapolated from other entries */
 174		.matches = {
 175			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 176			DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D420"),
 177		},
 178	},
 179	{
 180		/* Reported-by: Hans de Bruin <jmdebruin@xmsnet.nl> */
 181		.matches = {
 182			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 183			DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D430"),
 184		},
 185	},
 186	{
 187		/* Reported-by: Hans de Goede <hdegoede@redhat.com> */
 188		.matches = {
 189			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 190			DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D620"),
 191		},
 192	},
 193	{
 194		/* Extrapolated from other entries */
 195		.matches = {
 196			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 197			DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D630"),
 198		},
 199	},
 200#endif
 201	{ }
 202};
 203
 204static void alps_set_abs_params_st(struct alps_data *priv,
 205				   struct input_dev *dev1);
 206static void alps_set_abs_params_semi_mt(struct alps_data *priv,
 207					struct input_dev *dev1);
 208static void alps_set_abs_params_v7(struct alps_data *priv,
 209				   struct input_dev *dev1);
 210static void alps_set_abs_params_ss4_v2(struct alps_data *priv,
 211				       struct input_dev *dev1);
 212
 213/* Packet formats are described in Documentation/input/devices/alps.rst */
 214
 215static bool alps_is_valid_first_byte(struct alps_data *priv,
 216				     unsigned char data)
 217{
 218	return (data & priv->mask0) == priv->byte0;
 219}
 220
 221static void alps_report_buttons(struct input_dev *dev1, struct input_dev *dev2,
 222				int left, int right, int middle)
 223{
 224	struct input_dev *dev;
 225
 226	/*
 227	 * If shared button has already been reported on the
 228	 * other device (dev2) then this event should be also
 229	 * sent through that device.
 230	 */
 231	dev = (dev2 && test_bit(BTN_LEFT, dev2->key)) ? dev2 : dev1;
 232	input_report_key(dev, BTN_LEFT, left);
 233
 234	dev = (dev2 && test_bit(BTN_RIGHT, dev2->key)) ? dev2 : dev1;
 235	input_report_key(dev, BTN_RIGHT, right);
 236
 237	dev = (dev2 && test_bit(BTN_MIDDLE, dev2->key)) ? dev2 : dev1;
 238	input_report_key(dev, BTN_MIDDLE, middle);
 239
 240	/*
 241	 * Sync the _other_ device now, we'll do the first
 242	 * device later once we report the rest of the events.
 243	 */
 244	if (dev2)
 245		input_sync(dev2);
 246}
 247
 248static void alps_process_packet_v1_v2(struct psmouse *psmouse)
 249{
 250	struct alps_data *priv = psmouse->private;
 251	unsigned char *packet = psmouse->packet;
 252	struct input_dev *dev = psmouse->dev;
 253	struct input_dev *dev2 = priv->dev2;
 254	int x, y, z, ges, fin, left, right, middle;
 255	int back = 0, forward = 0;
 256
 257	if (priv->proto_version == ALPS_PROTO_V1) {
 258		left = packet[2] & 0x10;
 259		right = packet[2] & 0x08;
 260		middle = 0;
 261		x = packet[1] | ((packet[0] & 0x07) << 7);
 262		y = packet[4] | ((packet[3] & 0x07) << 7);
 263		z = packet[5];
 264	} else {
 265		left = packet[3] & 1;
 266		right = packet[3] & 2;
 267		middle = packet[3] & 4;
 268		x = packet[1] | ((packet[2] & 0x78) << (7 - 3));
 269		y = packet[4] | ((packet[3] & 0x70) << (7 - 4));
 270		z = packet[5];
 271	}
 272
 273	if (priv->flags & ALPS_FW_BK_1) {
 274		back = packet[0] & 0x10;
 275		forward = packet[2] & 4;
 276	}
 277
 278	if (priv->flags & ALPS_FW_BK_2) {
 279		back = packet[3] & 4;
 280		forward = packet[2] & 4;
 281		if ((middle = forward && back))
 282			forward = back = 0;
 283	}
 284
 285	ges = packet[2] & 1;
 286	fin = packet[2] & 2;
 287
 288	if ((priv->flags & ALPS_DUALPOINT) && z == 127) {
 289		input_report_rel(dev2, REL_X,  (x > 383 ? (x - 768) : x));
 290		input_report_rel(dev2, REL_Y, -(y > 255 ? (y - 512) : y));
 291
 292		alps_report_buttons(dev2, dev, left, right, middle);
 293
 294		input_sync(dev2);
 295		return;
 296	}
 297
 298	/* Some models have separate stick button bits */
 299	if (priv->flags & ALPS_STICK_BITS) {
 300		left |= packet[0] & 1;
 301		right |= packet[0] & 2;
 302		middle |= packet[0] & 4;
 303	}
 304
 305	alps_report_buttons(dev, dev2, left, right, middle);
 306
 307	/* Convert hardware tap to a reasonable Z value */
 308	if (ges && !fin)
 309		z = 40;
 310
 311	/*
 312	 * A "tap and drag" operation is reported by the hardware as a transition
 313	 * from (!fin && ges) to (fin && ges). This should be translated to the
 314	 * sequence Z>0, Z==0, Z>0, so the Z==0 event has to be generated manually.
 315	 */
 316	if (ges && fin && !priv->prev_fin) {
 317		input_report_abs(dev, ABS_X, x);
 318		input_report_abs(dev, ABS_Y, y);
 319		input_report_abs(dev, ABS_PRESSURE, 0);
 320		input_report_key(dev, BTN_TOOL_FINGER, 0);
 321		input_sync(dev);
 322	}
 323	priv->prev_fin = fin;
 324
 325	if (z > 30)
 326		input_report_key(dev, BTN_TOUCH, 1);
 327	if (z < 25)
 328		input_report_key(dev, BTN_TOUCH, 0);
 329
 330	if (z > 0) {
 331		input_report_abs(dev, ABS_X, x);
 332		input_report_abs(dev, ABS_Y, y);
 333	}
 334
 335	input_report_abs(dev, ABS_PRESSURE, z);
 336	input_report_key(dev, BTN_TOOL_FINGER, z > 0);
 337
 338	if (priv->flags & ALPS_WHEEL)
 339		input_report_rel(dev, REL_WHEEL, ((packet[2] << 1) & 0x08) - ((packet[0] >> 4) & 0x07));
 340
 341	if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
 342		input_report_key(dev, BTN_FORWARD, forward);
 343		input_report_key(dev, BTN_BACK, back);
 344	}
 345
 346	if (priv->flags & ALPS_FOUR_BUTTONS) {
 347		input_report_key(dev, BTN_0, packet[2] & 4);
 348		input_report_key(dev, BTN_1, packet[0] & 0x10);
 349		input_report_key(dev, BTN_2, packet[3] & 4);
 350		input_report_key(dev, BTN_3, packet[0] & 0x20);
 351	}
 352
 353	input_sync(dev);
 354}
 355
 356static void alps_get_bitmap_points(unsigned int map,
 357				   struct alps_bitmap_point *low,
 358				   struct alps_bitmap_point *high,
 359				   int *fingers)
 360{
 361	struct alps_bitmap_point *point;
 362	int i, bit, prev_bit = 0;
 363
 364	point = low;
 365	for (i = 0; map != 0; i++, map >>= 1) {
 366		bit = map & 1;
 367		if (bit) {
 368			if (!prev_bit) {
 369				point->start_bit = i;
 370				point->num_bits = 0;
 371				(*fingers)++;
 372			}
 373			point->num_bits++;
 374		} else {
 375			if (prev_bit)
 376				point = high;
 377		}
 378		prev_bit = bit;
 379	}
 380}
 381
 382/*
 383 * Process bitmap data from semi-mt protocols. Returns the number of
 384 * fingers detected. A return value of 0 means at least one of the
 385 * bitmaps was empty.
 386 *
 387 * The bitmaps don't have enough data to track fingers, so this function
 388 * only generates points representing a bounding box of all contacts.
 389 * These points are returned in fields->mt when the return value
 390 * is greater than 0.
 391 */
 392static int alps_process_bitmap(struct alps_data *priv,
 393			       struct alps_fields *fields)
 394{
 395	int i, fingers_x = 0, fingers_y = 0, fingers, closest;
 396	struct alps_bitmap_point x_low = {0,}, x_high = {0,};
 397	struct alps_bitmap_point y_low = {0,}, y_high = {0,};
 398	struct input_mt_pos corner[4];
 399
 400	if (!fields->x_map || !fields->y_map)
 401		return 0;
 402
 403	alps_get_bitmap_points(fields->x_map, &x_low, &x_high, &fingers_x);
 404	alps_get_bitmap_points(fields->y_map, &y_low, &y_high, &fingers_y);
 405
 406	/*
 407	 * Fingers can overlap, so we use the maximum count of fingers
 408	 * on either axis as the finger count.
 409	 */
 410	fingers = max(fingers_x, fingers_y);
 411
 412	/*
 413	 * If an axis reports only a single contact, we have overlapping or
 414	 * adjacent fingers. Divide the single contact between the two points.
 415	 */
 416	if (fingers_x == 1) {
 417		i = (x_low.num_bits - 1) / 2;
 418		x_low.num_bits = x_low.num_bits - i;
 419		x_high.start_bit = x_low.start_bit + i;
 420		x_high.num_bits = max(i, 1);
 421	}
 422	if (fingers_y == 1) {
 423		i = (y_low.num_bits - 1) / 2;
 424		y_low.num_bits = y_low.num_bits - i;
 425		y_high.start_bit = y_low.start_bit + i;
 426		y_high.num_bits = max(i, 1);
 427	}
 428
 429	/* top-left corner */
 430	corner[0].x =
 431		(priv->x_max * (2 * x_low.start_bit + x_low.num_bits - 1)) /
 432		(2 * (priv->x_bits - 1));
 433	corner[0].y =
 434		(priv->y_max * (2 * y_low.start_bit + y_low.num_bits - 1)) /
 435		(2 * (priv->y_bits - 1));
 436
 437	/* top-right corner */
 438	corner[1].x =
 439		(priv->x_max * (2 * x_high.start_bit + x_high.num_bits - 1)) /
 440		(2 * (priv->x_bits - 1));
 441	corner[1].y =
 442		(priv->y_max * (2 * y_low.start_bit + y_low.num_bits - 1)) /
 443		(2 * (priv->y_bits - 1));
 444
 445	/* bottom-right corner */
 446	corner[2].x =
 447		(priv->x_max * (2 * x_high.start_bit + x_high.num_bits - 1)) /
 448		(2 * (priv->x_bits - 1));
 449	corner[2].y =
 450		(priv->y_max * (2 * y_high.start_bit + y_high.num_bits - 1)) /
 451		(2 * (priv->y_bits - 1));
 452
 453	/* bottom-left corner */
 454	corner[3].x =
 455		(priv->x_max * (2 * x_low.start_bit + x_low.num_bits - 1)) /
 456		(2 * (priv->x_bits - 1));
 457	corner[3].y =
 458		(priv->y_max * (2 * y_high.start_bit + y_high.num_bits - 1)) /
 459		(2 * (priv->y_bits - 1));
 460
 461	/* x-bitmap order is reversed on v5 touchpads  */
 462	if (priv->proto_version == ALPS_PROTO_V5) {
 463		for (i = 0; i < 4; i++)
 464			corner[i].x = priv->x_max - corner[i].x;
 465	}
 466
 467	/* y-bitmap order is reversed on v3 and v4 touchpads  */
 468	if (priv->proto_version == ALPS_PROTO_V3 ||
 469	    priv->proto_version == ALPS_PROTO_V4) {
 470		for (i = 0; i < 4; i++)
 471			corner[i].y = priv->y_max - corner[i].y;
 472	}
 473
 474	/*
 475	 * We only select a corner for the second touch once per 2 finger
 476	 * touch sequence to avoid the chosen corner (and thus the coordinates)
 477	 * jumping around when the first touch is in the middle.
 478	 */
 479	if (priv->second_touch == -1) {
 480		/* Find corner closest to our st coordinates */
 481		closest = 0x7fffffff;
 482		for (i = 0; i < 4; i++) {
 483			int dx = fields->st.x - corner[i].x;
 484			int dy = fields->st.y - corner[i].y;
 485			int distance = dx * dx + dy * dy;
 486
 487			if (distance < closest) {
 488				priv->second_touch = i;
 489				closest = distance;
 490			}
 491		}
 492		/* And select the opposite corner to use for the 2nd touch */
 493		priv->second_touch = (priv->second_touch + 2) % 4;
 494	}
 495
 496	fields->mt[0] = fields->st;
 497	fields->mt[1] = corner[priv->second_touch];
 498
 499	return fingers;
 500}
 501
 502static void alps_set_slot(struct input_dev *dev, int slot, int x, int y)
 503{
 504	input_mt_slot(dev, slot);
 505	input_mt_report_slot_state(dev, MT_TOOL_FINGER, true);
 506	input_report_abs(dev, ABS_MT_POSITION_X, x);
 507	input_report_abs(dev, ABS_MT_POSITION_Y, y);
 508}
 509
 510static void alps_report_mt_data(struct psmouse *psmouse, int n)
 511{
 512	struct alps_data *priv = psmouse->private;
 513	struct input_dev *dev = psmouse->dev;
 514	struct alps_fields *f = &priv->f;
 515	int i, slot[MAX_TOUCHES];
 516
 517	input_mt_assign_slots(dev, slot, f->mt, n, 0);
 518	for (i = 0; i < n; i++)
 519		alps_set_slot(dev, slot[i], f->mt[i].x, f->mt[i].y);
 520
 521	input_mt_sync_frame(dev);
 522}
 523
 524static void alps_report_semi_mt_data(struct psmouse *psmouse, int fingers)
 525{
 526	struct alps_data *priv = psmouse->private;
 527	struct input_dev *dev = psmouse->dev;
 528	struct alps_fields *f = &priv->f;
 529
 530	/* Use st data when we don't have mt data */
 531	if (fingers < 2) {
 532		f->mt[0].x = f->st.x;
 533		f->mt[0].y = f->st.y;
 534		fingers = f->pressure > 0 ? 1 : 0;
 535		priv->second_touch = -1;
 536	}
 537
 538	if (fingers >= 1)
 539		alps_set_slot(dev, 0, f->mt[0].x, f->mt[0].y);
 540	if (fingers >= 2)
 541		alps_set_slot(dev, 1, f->mt[1].x, f->mt[1].y);
 542	input_mt_sync_frame(dev);
 543
 544	input_mt_report_finger_count(dev, fingers);
 545
 546	input_report_key(dev, BTN_LEFT, f->left);
 547	input_report_key(dev, BTN_RIGHT, f->right);
 548	input_report_key(dev, BTN_MIDDLE, f->middle);
 549
 550	input_report_abs(dev, ABS_PRESSURE, f->pressure);
 551
 552	input_sync(dev);
 553}
 554
 555static void alps_process_trackstick_packet_v3(struct psmouse *psmouse)
 556{
 557	struct alps_data *priv = psmouse->private;
 558	unsigned char *packet = psmouse->packet;
 559	struct input_dev *dev = priv->dev2;
 560	int x, y, z, left, right, middle;
 561
 562	/* It should be a DualPoint when received trackstick packet */
 563	if (!(priv->flags & ALPS_DUALPOINT)) {
 564		psmouse_warn(psmouse,
 565			     "Rejected trackstick packet from non DualPoint device");
 566		return;
 567	}
 568
 569	/* Sanity check packet */
 570	if (!(packet[0] & 0x40)) {
 571		psmouse_dbg(psmouse, "Bad trackstick packet, discarding\n");
 572		return;
 573	}
 574
 575	/*
 576	 * There's a special packet that seems to indicate the end
 577	 * of a stream of trackstick data. Filter these out.
 578	 */
 579	if (packet[1] == 0x7f && packet[2] == 0x7f && packet[4] == 0x7f)
 580		return;
 581
 582	x = (s8)(((packet[0] & 0x20) << 2) | (packet[1] & 0x7f));
 583	y = (s8)(((packet[0] & 0x10) << 3) | (packet[2] & 0x7f));
 584	z = packet[4] & 0x7f;
 585
 586	/*
 587	 * The x and y values tend to be quite large, and when used
 588	 * alone the trackstick is difficult to use. Scale them down
 589	 * to compensate.
 590	 */
 591	x /= 8;
 592	y /= 8;
 593
 594	input_report_rel(dev, REL_X, x);
 595	input_report_rel(dev, REL_Y, -y);
 596	input_report_abs(dev, ABS_PRESSURE, z);
 597
 598	/*
 599	 * Most ALPS models report the trackstick buttons in the touchpad
 600	 * packets, but a few report them here. No reliable way has been
 601	 * found to differentiate between the models upfront, so we enable
 602	 * the quirk in response to seeing a button press in the trackstick
 603	 * packet.
 604	 */
 605	left = packet[3] & 0x01;
 606	right = packet[3] & 0x02;
 607	middle = packet[3] & 0x04;
 608
 609	if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) &&
 610	    (left || right || middle))
 611		priv->quirks |= ALPS_QUIRK_TRACKSTICK_BUTTONS;
 612
 613	if (priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) {
 614		input_report_key(dev, BTN_LEFT, left);
 615		input_report_key(dev, BTN_RIGHT, right);
 616		input_report_key(dev, BTN_MIDDLE, middle);
 617	}
 618
 619	input_sync(dev);
 620	return;
 621}
 622
 623static void alps_decode_buttons_v3(struct alps_fields *f, unsigned char *p)
 624{
 625	f->left = !!(p[3] & 0x01);
 626	f->right = !!(p[3] & 0x02);
 627	f->middle = !!(p[3] & 0x04);
 628
 629	f->ts_left = !!(p[3] & 0x10);
 630	f->ts_right = !!(p[3] & 0x20);
 631	f->ts_middle = !!(p[3] & 0x40);
 632}
 633
 634static int alps_decode_pinnacle(struct alps_fields *f, unsigned char *p,
 635				 struct psmouse *psmouse)
 636{
 637	f->first_mp = !!(p[4] & 0x40);
 638	f->is_mp = !!(p[0] & 0x40);
 639
 640	if (f->is_mp) {
 641		f->fingers = (p[5] & 0x3) + 1;
 642		f->x_map = ((p[4] & 0x7e) << 8) |
 643			   ((p[1] & 0x7f) << 2) |
 644			   ((p[0] & 0x30) >> 4);
 645		f->y_map = ((p[3] & 0x70) << 4) |
 646			   ((p[2] & 0x7f) << 1) |
 647			   (p[4] & 0x01);
 648	} else {
 649		f->st.x = ((p[1] & 0x7f) << 4) | ((p[4] & 0x30) >> 2) |
 650		       ((p[0] & 0x30) >> 4);
 651		f->st.y = ((p[2] & 0x7f) << 4) | (p[4] & 0x0f);
 652		f->pressure = p[5] & 0x7f;
 653
 654		alps_decode_buttons_v3(f, p);
 655	}
 656
 657	return 0;
 658}
 659
 660static int alps_decode_rushmore(struct alps_fields *f, unsigned char *p,
 661				 struct psmouse *psmouse)
 662{
 663	f->first_mp = !!(p[4] & 0x40);
 664	f->is_mp = !!(p[5] & 0x40);
 665
 666	if (f->is_mp) {
 667		f->fingers = max((p[5] & 0x3), ((p[5] >> 2) & 0x3)) + 1;
 668		f->x_map = ((p[5] & 0x10) << 11) |
 669			   ((p[4] & 0x7e) << 8) |
 670			   ((p[1] & 0x7f) << 2) |
 671			   ((p[0] & 0x30) >> 4);
 672		f->y_map = ((p[5] & 0x20) << 6) |
 673			   ((p[3] & 0x70) << 4) |
 674			   ((p[2] & 0x7f) << 1) |
 675			   (p[4] & 0x01);
 676	} else {
 677		f->st.x = ((p[1] & 0x7f) << 4) | ((p[4] & 0x30) >> 2) |
 678		       ((p[0] & 0x30) >> 4);
 679		f->st.y = ((p[2] & 0x7f) << 4) | (p[4] & 0x0f);
 680		f->pressure = p[5] & 0x7f;
 681
 682		alps_decode_buttons_v3(f, p);
 683	}
 684
 685	return 0;
 686}
 687
 688static int alps_decode_dolphin(struct alps_fields *f, unsigned char *p,
 689				struct psmouse *psmouse)
 690{
 691	u64 palm_data = 0;
 692	struct alps_data *priv = psmouse->private;
 693
 694	f->first_mp = !!(p[0] & 0x02);
 695	f->is_mp = !!(p[0] & 0x20);
 696
 697	if (!f->is_mp) {
 698		f->st.x = ((p[1] & 0x7f) | ((p[4] & 0x0f) << 7));
 699		f->st.y = ((p[2] & 0x7f) | ((p[4] & 0xf0) << 3));
 700		f->pressure = (p[0] & 4) ? 0 : p[5] & 0x7f;
 701		alps_decode_buttons_v3(f, p);
 702	} else {
 703		f->fingers = ((p[0] & 0x6) >> 1 |
 704		     (p[0] & 0x10) >> 2);
 705
 706		palm_data = (p[1] & 0x7f) |
 707			    ((p[2] & 0x7f) << 7) |
 708			    ((p[4] & 0x7f) << 14) |
 709			    ((p[5] & 0x7f) << 21) |
 710			    ((p[3] & 0x07) << 28) |
 711			    (((u64)p[3] & 0x70) << 27) |
 712			    (((u64)p[0] & 0x01) << 34);
 713
 714		/* Y-profile is stored in P(0) to p(n-1), n = y_bits; */
 715		f->y_map = palm_data & (BIT(priv->y_bits) - 1);
 716
 717		/* X-profile is stored in p(n) to p(n+m-1), m = x_bits; */
 718		f->x_map = (palm_data >> priv->y_bits) &
 719			   (BIT(priv->x_bits) - 1);
 720	}
 721
 722	return 0;
 723}
 724
 725static void alps_process_touchpad_packet_v3_v5(struct psmouse *psmouse)
 726{
 727	struct alps_data *priv = psmouse->private;
 728	unsigned char *packet = psmouse->packet;
 729	struct input_dev *dev2 = priv->dev2;
 730	struct alps_fields *f = &priv->f;
 731	int fingers = 0;
 732
 733	memset(f, 0, sizeof(*f));
 734
 735	priv->decode_fields(f, packet, psmouse);
 736
 737	/*
 738	 * There's no single feature of touchpad position and bitmap packets
 739	 * that can be used to distinguish between them. We rely on the fact
 740	 * that a bitmap packet should always follow a position packet with
 741	 * bit 6 of packet[4] set.
 742	 */
 743	if (priv->multi_packet) {
 744		/*
 745		 * Sometimes a position packet will indicate a multi-packet
 746		 * sequence, but then what follows is another position
 747		 * packet. Check for this, and when it happens process the
 748		 * position packet as usual.
 749		 */
 750		if (f->is_mp) {
 751			fingers = f->fingers;
 752			/*
 753			 * Bitmap processing uses position packet's coordinate
 754			 * data, so we need to do decode it first.
 755			 */
 756			priv->decode_fields(f, priv->multi_data, psmouse);
 757			if (alps_process_bitmap(priv, f) == 0)
 758				fingers = 0; /* Use st data */
 759		} else {
 760			priv->multi_packet = 0;
 761		}
 762	}
 763
 764	/*
 765	 * Bit 6 of byte 0 is not usually set in position packets. The only
 766	 * times it seems to be set is in situations where the data is
 767	 * suspect anyway, e.g. a palm resting flat on the touchpad. Given
 768	 * this combined with the fact that this bit is useful for filtering
 769	 * out misidentified bitmap packets, we reject anything with this
 770	 * bit set.
 771	 */
 772	if (f->is_mp)
 773		return;
 774
 775	if (!priv->multi_packet && f->first_mp) {
 776		priv->multi_packet = 1;
 777		memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
 778		return;
 779	}
 780
 781	priv->multi_packet = 0;
 782
 783	/*
 784	 * Sometimes the hardware sends a single packet with z = 0
 785	 * in the middle of a stream. Real releases generate packets
 786	 * with x, y, and z all zero, so these seem to be flukes.
 787	 * Ignore them.
 788	 */
 789	if (f->st.x && f->st.y && !f->pressure)
 790		return;
 791
 792	alps_report_semi_mt_data(psmouse, fingers);
 793
 794	if ((priv->flags & ALPS_DUALPOINT) &&
 795	    !(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS)) {
 796		input_report_key(dev2, BTN_LEFT, f->ts_left);
 797		input_report_key(dev2, BTN_RIGHT, f->ts_right);
 798		input_report_key(dev2, BTN_MIDDLE, f->ts_middle);
 799		input_sync(dev2);
 800	}
 801}
 802
 803static void alps_process_packet_v3(struct psmouse *psmouse)
 804{
 805	unsigned char *packet = psmouse->packet;
 806
 807	/*
 808	 * v3 protocol packets come in three types, two representing
 809	 * touchpad data and one representing trackstick data.
 810	 * Trackstick packets seem to be distinguished by always
 811	 * having 0x3f in the last byte. This value has never been
 812	 * observed in the last byte of either of the other types
 813	 * of packets.
 814	 */
 815	if (packet[5] == 0x3f) {
 816		alps_process_trackstick_packet_v3(psmouse);
 817		return;
 818	}
 819
 820	alps_process_touchpad_packet_v3_v5(psmouse);
 821}
 822
 823static void alps_process_packet_v6(struct psmouse *psmouse)
 824{
 825	struct alps_data *priv = psmouse->private;
 826	unsigned char *packet = psmouse->packet;
 827	struct input_dev *dev = psmouse->dev;
 828	struct input_dev *dev2 = priv->dev2;
 829	int x, y, z;
 830
 831	/*
 832	 * We can use Byte5 to distinguish if the packet is from Touchpad
 833	 * or Trackpoint.
 834	 * Touchpad:	0 - 0x7E
 835	 * Trackpoint:	0x7F
 836	 */
 837	if (packet[5] == 0x7F) {
 838		/* It should be a DualPoint when received Trackpoint packet */
 839		if (!(priv->flags & ALPS_DUALPOINT)) {
 840			psmouse_warn(psmouse,
 841				     "Rejected trackstick packet from non DualPoint device");
 842			return;
 843		}
 844
 845		/* Trackpoint packet */
 846		x = packet[1] | ((packet[3] & 0x20) << 2);
 847		y = packet[2] | ((packet[3] & 0x40) << 1);
 848		z = packet[4];
 849
 850		/* To prevent the cursor jump when finger lifted */
 851		if (x == 0x7F && y == 0x7F && z == 0x7F)
 852			x = y = z = 0;
 853
 854		/* Divide 4 since trackpoint's speed is too fast */
 855		input_report_rel(dev2, REL_X, (s8)x / 4);
 856		input_report_rel(dev2, REL_Y, -((s8)y / 4));
 857
 858		psmouse_report_standard_buttons(dev2, packet[3]);
 859
 860		input_sync(dev2);
 861		return;
 862	}
 863
 864	/* Touchpad packet */
 865	x = packet[1] | ((packet[3] & 0x78) << 4);
 866	y = packet[2] | ((packet[4] & 0x78) << 4);
 867	z = packet[5];
 868
 869	if (z > 30)
 870		input_report_key(dev, BTN_TOUCH, 1);
 871	if (z < 25)
 872		input_report_key(dev, BTN_TOUCH, 0);
 873
 874	if (z > 0) {
 875		input_report_abs(dev, ABS_X, x);
 876		input_report_abs(dev, ABS_Y, y);
 877	}
 878
 879	input_report_abs(dev, ABS_PRESSURE, z);
 880	input_report_key(dev, BTN_TOOL_FINGER, z > 0);
 881
 882	/* v6 touchpad does not have middle button */
 883	packet[3] &= ~BIT(2);
 884	psmouse_report_standard_buttons(dev2, packet[3]);
 885
 886	input_sync(dev);
 887}
 888
 889static void alps_process_packet_v4(struct psmouse *psmouse)
 890{
 891	struct alps_data *priv = psmouse->private;
 892	unsigned char *packet = psmouse->packet;
 893	struct alps_fields *f = &priv->f;
 894	int offset;
 895
 896	/*
 897	 * v4 has a 6-byte encoding for bitmap data, but this data is
 898	 * broken up between 3 normal packets. Use priv->multi_packet to
 899	 * track our position in the bitmap packet.
 900	 */
 901	if (packet[6] & 0x40) {
 902		/* sync, reset position */
 903		priv->multi_packet = 0;
 904	}
 905
 906	if (WARN_ON_ONCE(priv->multi_packet > 2))
 907		return;
 908
 909	offset = 2 * priv->multi_packet;
 910	priv->multi_data[offset] = packet[6];
 911	priv->multi_data[offset + 1] = packet[7];
 912
 913	f->left = !!(packet[4] & 0x01);
 914	f->right = !!(packet[4] & 0x02);
 915
 916	f->st.x = ((packet[1] & 0x7f) << 4) | ((packet[3] & 0x30) >> 2) |
 917		  ((packet[0] & 0x30) >> 4);
 918	f->st.y = ((packet[2] & 0x7f) << 4) | (packet[3] & 0x0f);
 919	f->pressure = packet[5] & 0x7f;
 920
 921	if (++priv->multi_packet > 2) {
 922		priv->multi_packet = 0;
 923
 924		f->x_map = ((priv->multi_data[2] & 0x1f) << 10) |
 925			   ((priv->multi_data[3] & 0x60) << 3) |
 926			   ((priv->multi_data[0] & 0x3f) << 2) |
 927			   ((priv->multi_data[1] & 0x60) >> 5);
 928		f->y_map = ((priv->multi_data[5] & 0x01) << 10) |
 929			   ((priv->multi_data[3] & 0x1f) << 5) |
 930			    (priv->multi_data[1] & 0x1f);
 931
 932		f->fingers = alps_process_bitmap(priv, f);
 933	}
 934
 935	alps_report_semi_mt_data(psmouse, f->fingers);
 936}
 937
 938static bool alps_is_valid_package_v7(struct psmouse *psmouse)
 939{
 940	switch (psmouse->pktcnt) {
 941	case 3:
 942		return (psmouse->packet[2] & 0x40) == 0x40;
 943	case 4:
 944		return (psmouse->packet[3] & 0x48) == 0x48;
 945	case 6:
 946		return (psmouse->packet[5] & 0x40) == 0x00;
 947	}
 948	return true;
 949}
 950
 951static unsigned char alps_get_packet_id_v7(char *byte)
 952{
 953	unsigned char packet_id;
 954
 955	if (byte[4] & 0x40)
 956		packet_id = V7_PACKET_ID_TWO;
 957	else if (byte[4] & 0x01)
 958		packet_id = V7_PACKET_ID_MULTI;
 959	else if ((byte[0] & 0x10) && !(byte[4] & 0x43))
 960		packet_id = V7_PACKET_ID_NEW;
 961	else if (byte[1] == 0x00 && byte[4] == 0x00)
 962		packet_id = V7_PACKET_ID_IDLE;
 963	else
 964		packet_id = V7_PACKET_ID_UNKNOWN;
 965
 966	return packet_id;
 967}
 968
 969static void alps_get_finger_coordinate_v7(struct input_mt_pos *mt,
 970					  unsigned char *pkt,
 971					  unsigned char pkt_id)
 972{
 973	mt[0].x = ((pkt[2] & 0x80) << 4);
 974	mt[0].x |= ((pkt[2] & 0x3F) << 5);
 975	mt[0].x |= ((pkt[3] & 0x30) >> 1);
 976	mt[0].x |= (pkt[3] & 0x07);
 977	mt[0].y = (pkt[1] << 3) | (pkt[0] & 0x07);
 978
 979	mt[1].x = ((pkt[3] & 0x80) << 4);
 980	mt[1].x |= ((pkt[4] & 0x80) << 3);
 981	mt[1].x |= ((pkt[4] & 0x3F) << 4);
 982	mt[1].y = ((pkt[5] & 0x80) << 3);
 983	mt[1].y |= ((pkt[5] & 0x3F) << 4);
 984
 985	switch (pkt_id) {
 986	case V7_PACKET_ID_TWO:
 987		mt[1].x &= ~0x000F;
 988		mt[1].y |= 0x000F;
 989		/* Detect false-positive touches where x & y report max value */
 990		if (mt[1].y == 0x7ff && mt[1].x == 0xff0) {
 991			mt[1].x = 0;
 992			/* y gets set to 0 at the end of this function */
 993		}
 994		break;
 995
 996	case V7_PACKET_ID_MULTI:
 997		mt[1].x &= ~0x003F;
 998		mt[1].y &= ~0x0020;
 999		mt[1].y |= ((pkt[4] & 0x02) << 4);
1000		mt[1].y |= 0x001F;
1001		break;
1002
1003	case V7_PACKET_ID_NEW:
1004		mt[1].x &= ~0x003F;
1005		mt[1].x |= (pkt[0] & 0x20);
1006		mt[1].y |= 0x000F;
1007		break;
1008	}
1009
1010	mt[0].y = 0x7FF - mt[0].y;
1011	mt[1].y = 0x7FF - mt[1].y;
1012}
1013
1014static int alps_get_mt_count(struct input_mt_pos *mt)
1015{
1016	int i, fingers = 0;
1017
1018	for (i = 0; i < MAX_TOUCHES; i++) {
1019		if (mt[i].x != 0 || mt[i].y != 0)
1020			fingers++;
1021	}
1022
1023	return fingers;
1024}
1025
1026static int alps_decode_packet_v7(struct alps_fields *f,
1027				  unsigned char *p,
1028				  struct psmouse *psmouse)
1029{
1030	struct alps_data *priv = psmouse->private;
1031	unsigned char pkt_id;
1032
1033	pkt_id = alps_get_packet_id_v7(p);
1034	if (pkt_id == V7_PACKET_ID_IDLE)
1035		return 0;
1036	if (pkt_id == V7_PACKET_ID_UNKNOWN)
1037		return -1;
1038	/*
1039	 * NEW packets are send to indicate a discontinuity in the finger
1040	 * coordinate reporting. Specifically a finger may have moved from
1041	 * slot 0 to 1 or vice versa. INPUT_MT_TRACK takes care of this for
1042	 * us.
1043	 *
1044	 * NEW packets have 3 problems:
1045	 * 1) They do not contain middle / right button info (on non clickpads)
1046	 *    this can be worked around by preserving the old button state
1047	 * 2) They do not contain an accurate fingercount, and they are
1048	 *    typically send when the number of fingers changes. We cannot use
1049	 *    the old finger count as that may mismatch with the amount of
1050	 *    touch coordinates we've available in the NEW packet
1051	 * 3) Their x data for the second touch is inaccurate leading to
1052	 *    a possible jump of the x coordinate by 16 units when the first
1053	 *    non NEW packet comes in
1054	 * Since problems 2 & 3 cannot be worked around, just ignore them.
1055	 */
1056	if (pkt_id == V7_PACKET_ID_NEW)
1057		return 1;
1058
1059	alps_get_finger_coordinate_v7(f->mt, p, pkt_id);
1060
1061	if (pkt_id == V7_PACKET_ID_TWO)
1062		f->fingers = alps_get_mt_count(f->mt);
1063	else /* pkt_id == V7_PACKET_ID_MULTI */
1064		f->fingers = 3 + (p[5] & 0x03);
1065
1066	f->left = (p[0] & 0x80) >> 7;
1067	if (priv->flags & ALPS_BUTTONPAD) {
1068		if (p[0] & 0x20)
1069			f->fingers++;
1070		if (p[0] & 0x10)
1071			f->fingers++;
1072	} else {
1073		f->right = (p[0] & 0x20) >> 5;
1074		f->middle = (p[0] & 0x10) >> 4;
1075	}
1076
1077	/* Sometimes a single touch is reported in mt[1] rather then mt[0] */
1078	if (f->fingers == 1 && f->mt[0].x == 0 && f->mt[0].y == 0) {
1079		f->mt[0].x = f->mt[1].x;
1080		f->mt[0].y = f->mt[1].y;
1081		f->mt[1].x = 0;
1082		f->mt[1].y = 0;
1083	}
1084
1085	return 0;
1086}
1087
1088static void alps_process_trackstick_packet_v7(struct psmouse *psmouse)
1089{
1090	struct alps_data *priv = psmouse->private;
1091	unsigned char *packet = psmouse->packet;
1092	struct input_dev *dev2 = priv->dev2;
1093	int x, y, z;
1094
1095	/* It should be a DualPoint when received trackstick packet */
1096	if (!(priv->flags & ALPS_DUALPOINT)) {
1097		psmouse_warn(psmouse,
1098			     "Rejected trackstick packet from non DualPoint device");
1099		return;
1100	}
1101
1102	x = ((packet[2] & 0xbf)) | ((packet[3] & 0x10) << 2);
1103	y = (packet[3] & 0x07) | (packet[4] & 0xb8) |
1104	    ((packet[3] & 0x20) << 1);
1105	z = (packet[5] & 0x3f) | ((packet[3] & 0x80) >> 1);
1106
1107	input_report_rel(dev2, REL_X, (s8)x);
1108	input_report_rel(dev2, REL_Y, -((s8)y));
1109	input_report_abs(dev2, ABS_PRESSURE, z);
1110
1111	psmouse_report_standard_buttons(dev2, packet[1]);
1112
1113	input_sync(dev2);
1114}
1115
1116static void alps_process_touchpad_packet_v7(struct psmouse *psmouse)
1117{
1118	struct alps_data *priv = psmouse->private;
1119	struct input_dev *dev = psmouse->dev;
1120	struct alps_fields *f = &priv->f;
1121
1122	memset(f, 0, sizeof(*f));
1123
1124	if (priv->decode_fields(f, psmouse->packet, psmouse))
1125		return;
1126
1127	alps_report_mt_data(psmouse, alps_get_mt_count(f->mt));
1128
1129	input_mt_report_finger_count(dev, f->fingers);
1130
1131	input_report_key(dev, BTN_LEFT, f->left);
1132	input_report_key(dev, BTN_RIGHT, f->right);
1133	input_report_key(dev, BTN_MIDDLE, f->middle);
1134
1135	input_sync(dev);
1136}
1137
1138static void alps_process_packet_v7(struct psmouse *psmouse)
1139{
1140	unsigned char *packet = psmouse->packet;
1141
1142	if (packet[0] == 0x48 && (packet[4] & 0x47) == 0x06)
1143		alps_process_trackstick_packet_v7(psmouse);
1144	else
1145		alps_process_touchpad_packet_v7(psmouse);
1146}
1147
1148static enum SS4_PACKET_ID alps_get_pkt_id_ss4_v2(unsigned char *byte)
1149{
1150	enum SS4_PACKET_ID pkt_id = SS4_PACKET_ID_IDLE;
1151
1152	switch (byte[3] & 0x30) {
1153	case 0x00:
1154		if (SS4_IS_IDLE_V2(byte)) {
1155			pkt_id = SS4_PACKET_ID_IDLE;
1156		} else {
1157			pkt_id = SS4_PACKET_ID_ONE;
1158		}
1159		break;
1160	case 0x10:
1161		/* two-finger finger positions */
1162		pkt_id = SS4_PACKET_ID_TWO;
1163		break;
1164	case 0x20:
1165		/* stick pointer */
1166		pkt_id = SS4_PACKET_ID_STICK;
1167		break;
1168	case 0x30:
1169		/* third and fourth finger positions */
1170		pkt_id = SS4_PACKET_ID_MULTI;
1171		break;
1172	}
1173
1174	return pkt_id;
1175}
1176
1177static int alps_decode_ss4_v2(struct alps_fields *f,
1178			      unsigned char *p, struct psmouse *psmouse)
1179{
1180	struct alps_data *priv = psmouse->private;
1181	enum SS4_PACKET_ID pkt_id;
1182	unsigned int no_data_x, no_data_y;
1183
1184	pkt_id = alps_get_pkt_id_ss4_v2(p);
1185
1186	/* Current packet is 1Finger coordinate packet */
1187	switch (pkt_id) {
1188	case SS4_PACKET_ID_ONE:
1189		f->mt[0].x = SS4_1F_X_V2(p);
1190		f->mt[0].y = SS4_1F_Y_V2(p);
1191		f->pressure = ((SS4_1F_Z_V2(p)) * 2) & 0x7f;
1192		/*
1193		 * When a button is held the device will give us events
1194		 * with x, y, and pressure of 0. This causes annoying jumps
1195		 * if a touch is released while the button is held.
1196		 * Handle this by claiming zero contacts.
1197		 */
1198		f->fingers = f->pressure > 0 ? 1 : 0;
1199		f->first_mp = 0;
1200		f->is_mp = 0;
1201		break;
1202
1203	case SS4_PACKET_ID_TWO:
1204		if (priv->flags & ALPS_BUTTONPAD) {
1205			if (IS_SS4PLUS_DEV(priv->dev_id)) {
1206				f->mt[0].x = SS4_PLUS_BTL_MF_X_V2(p, 0);
1207				f->mt[1].x = SS4_PLUS_BTL_MF_X_V2(p, 1);
1208			} else {
1209				f->mt[0].x = SS4_BTL_MF_X_V2(p, 0);
1210				f->mt[1].x = SS4_BTL_MF_X_V2(p, 1);
1211			}
1212			f->mt[0].y = SS4_BTL_MF_Y_V2(p, 0);
1213			f->mt[1].y = SS4_BTL_MF_Y_V2(p, 1);
1214		} else {
1215			if (IS_SS4PLUS_DEV(priv->dev_id)) {
1216				f->mt[0].x = SS4_PLUS_STD_MF_X_V2(p, 0);
1217				f->mt[1].x = SS4_PLUS_STD_MF_X_V2(p, 1);
1218			} else {
1219				f->mt[0].x = SS4_STD_MF_X_V2(p, 0);
1220				f->mt[1].x = SS4_STD_MF_X_V2(p, 1);
1221			}
1222			f->mt[0].y = SS4_STD_MF_Y_V2(p, 0);
1223			f->mt[1].y = SS4_STD_MF_Y_V2(p, 1);
1224		}
1225		f->pressure = SS4_MF_Z_V2(p, 0) ? 0x30 : 0;
1226
1227		if (SS4_IS_MF_CONTINUE(p)) {
1228			f->first_mp = 1;
1229		} else {
1230			f->fingers = 2;
1231			f->first_mp = 0;
1232		}
1233		f->is_mp = 0;
1234
1235		break;
1236
1237	case SS4_PACKET_ID_MULTI:
1238		if (priv->flags & ALPS_BUTTONPAD) {
1239			if (IS_SS4PLUS_DEV(priv->dev_id)) {
1240				f->mt[2].x = SS4_PLUS_BTL_MF_X_V2(p, 0);
1241				f->mt[3].x = SS4_PLUS_BTL_MF_X_V2(p, 1);
1242				no_data_x = SS4_PLUS_MFPACKET_NO_AX_BL;
1243			} else {
1244				f->mt[2].x = SS4_BTL_MF_X_V2(p, 0);
1245				f->mt[3].x = SS4_BTL_MF_X_V2(p, 1);
1246				no_data_x = SS4_MFPACKET_NO_AX_BL;
1247			}
1248			no_data_y = SS4_MFPACKET_NO_AY_BL;
1249
1250			f->mt[2].y = SS4_BTL_MF_Y_V2(p, 0);
1251			f->mt[3].y = SS4_BTL_MF_Y_V2(p, 1);
1252		} else {
1253			if (IS_SS4PLUS_DEV(priv->dev_id)) {
1254				f->mt[2].x = SS4_PLUS_STD_MF_X_V2(p, 0);
1255				f->mt[3].x = SS4_PLUS_STD_MF_X_V2(p, 1);
1256				no_data_x = SS4_PLUS_MFPACKET_NO_AX;
1257			} else {
1258				f->mt[2].x = SS4_STD_MF_X_V2(p, 0);
1259				f->mt[3].x = SS4_STD_MF_X_V2(p, 1);
1260				no_data_x = SS4_MFPACKET_NO_AX;
1261			}
1262			no_data_y = SS4_MFPACKET_NO_AY;
1263
1264			f->mt[2].y = SS4_STD_MF_Y_V2(p, 0);
1265			f->mt[3].y = SS4_STD_MF_Y_V2(p, 1);
1266		}
1267
1268		f->first_mp = 0;
1269		f->is_mp = 1;
1270
1271		if (SS4_IS_5F_DETECTED(p)) {
1272			f->fingers = 5;
1273		} else if (f->mt[3].x == no_data_x &&
1274			     f->mt[3].y == no_data_y) {
1275			f->mt[3].x = 0;
1276			f->mt[3].y = 0;
1277			f->fingers = 3;
1278		} else {
1279			f->fingers = 4;
1280		}
1281		break;
1282
1283	case SS4_PACKET_ID_STICK:
1284		/*
1285		 * x, y, and pressure are decoded in
1286		 * alps_process_packet_ss4_v2()
1287		 */
1288		f->first_mp = 0;
1289		f->is_mp = 0;
1290		break;
1291
1292	case SS4_PACKET_ID_IDLE:
1293	default:
1294		memset(f, 0, sizeof(struct alps_fields));
1295		break;
1296	}
1297
1298	/* handle buttons */
1299	if (pkt_id == SS4_PACKET_ID_STICK) {
1300		f->ts_left = !!(SS4_BTN_V2(p) & 0x01);
1301		f->ts_right = !!(SS4_BTN_V2(p) & 0x02);
1302		f->ts_middle = !!(SS4_BTN_V2(p) & 0x04);
1303	} else {
1304		f->left = !!(SS4_BTN_V2(p) & 0x01);
1305		if (!(priv->flags & ALPS_BUTTONPAD)) {
1306			f->right = !!(SS4_BTN_V2(p) & 0x02);
1307			f->middle = !!(SS4_BTN_V2(p) & 0x04);
1308		}
1309	}
1310
1311	return 0;
1312}
1313
1314static void alps_process_packet_ss4_v2(struct psmouse *psmouse)
1315{
1316	struct alps_data *priv = psmouse->private;
1317	unsigned char *packet = psmouse->packet;
1318	struct input_dev *dev = psmouse->dev;
1319	struct input_dev *dev2 = priv->dev2;
1320	struct alps_fields *f = &priv->f;
1321
1322	memset(f, 0, sizeof(struct alps_fields));
1323	priv->decode_fields(f, packet, psmouse);
1324	if (priv->multi_packet) {
1325		/*
1326		 * Sometimes the first packet will indicate a multi-packet
1327		 * sequence, but sometimes the next multi-packet would not
1328		 * come. Check for this, and when it happens process the
1329		 * position packet as usual.
1330		 */
1331		if (f->is_mp) {
1332			/* Now process the 1st packet */
1333			priv->decode_fields(f, priv->multi_data, psmouse);
1334		} else {
1335			priv->multi_packet = 0;
1336		}
1337	}
1338
1339	/*
1340	 * "f.is_mp" would always be '0' after merging the 1st and 2nd packet.
1341	 * When it is set, it means 2nd packet comes without 1st packet come.
1342	 */
1343	if (f->is_mp)
1344		return;
1345
1346	/* Save the first packet */
1347	if (!priv->multi_packet && f->first_mp) {
1348		priv->multi_packet = 1;
1349		memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
1350		return;
1351	}
1352
1353	priv->multi_packet = 0;
1354
1355	/* Report trackstick */
1356	if (alps_get_pkt_id_ss4_v2(packet) == SS4_PACKET_ID_STICK) {
1357		if (!(priv->flags & ALPS_DUALPOINT)) {
1358			psmouse_warn(psmouse,
1359				     "Rejected trackstick packet from non DualPoint device");
1360			return;
1361		}
1362
1363		input_report_rel(dev2, REL_X, SS4_TS_X_V2(packet));
1364		input_report_rel(dev2, REL_Y, SS4_TS_Y_V2(packet));
1365		input_report_abs(dev2, ABS_PRESSURE, SS4_TS_Z_V2(packet));
1366
1367		input_report_key(dev2, BTN_LEFT, f->ts_left);
1368		input_report_key(dev2, BTN_RIGHT, f->ts_right);
1369		input_report_key(dev2, BTN_MIDDLE, f->ts_middle);
1370
1371		input_sync(dev2);
1372		return;
1373	}
1374
1375	/* Report touchpad */
1376	alps_report_mt_data(psmouse, (f->fingers <= 4) ? f->fingers : 4);
1377
1378	input_mt_report_finger_count(dev, f->fingers);
1379
1380	input_report_key(dev, BTN_LEFT, f->left);
1381	input_report_key(dev, BTN_RIGHT, f->right);
1382	input_report_key(dev, BTN_MIDDLE, f->middle);
1383
1384	input_report_abs(dev, ABS_PRESSURE, f->pressure);
1385	input_sync(dev);
1386}
1387
1388static bool alps_is_valid_package_ss4_v2(struct psmouse *psmouse)
1389{
1390	if (psmouse->pktcnt == 4 && ((psmouse->packet[3] & 0x08) != 0x08))
1391		return false;
1392	if (psmouse->pktcnt == 6 && ((psmouse->packet[5] & 0x10) != 0x0))
1393		return false;
1394	return true;
1395}
1396
1397static DEFINE_MUTEX(alps_mutex);
1398
1399static void alps_register_bare_ps2_mouse(struct work_struct *work)
1400{
1401	struct alps_data *priv =
1402		container_of(work, struct alps_data, dev3_register_work.work);
1403	struct psmouse *psmouse = priv->psmouse;
1404	struct input_dev *dev3;
1405	int error = 0;
1406
1407	mutex_lock(&alps_mutex);
1408
1409	if (priv->dev3)
1410		goto out;
1411
1412	dev3 = input_allocate_device();
1413	if (!dev3) {
1414		psmouse_err(psmouse, "failed to allocate secondary device\n");
1415		error = -ENOMEM;
1416		goto out;
1417	}
1418
1419	snprintf(priv->phys3, sizeof(priv->phys3), "%s/%s",
1420		 psmouse->ps2dev.serio->phys,
1421		 (priv->dev2 ? "input2" : "input1"));
1422	dev3->phys = priv->phys3;
1423
1424	/*
1425	 * format of input device name is: "protocol vendor name"
1426	 * see function psmouse_switch_protocol() in psmouse-base.c
1427	 */
1428	dev3->name = "PS/2 ALPS Mouse";
1429
1430	dev3->id.bustype = BUS_I8042;
1431	dev3->id.vendor  = 0x0002;
1432	dev3->id.product = PSMOUSE_PS2;
1433	dev3->id.version = 0x0000;
1434	dev3->dev.parent = &psmouse->ps2dev.serio->dev;
1435
1436	input_set_capability(dev3, EV_REL, REL_X);
1437	input_set_capability(dev3, EV_REL, REL_Y);
1438	input_set_capability(dev3, EV_KEY, BTN_LEFT);
1439	input_set_capability(dev3, EV_KEY, BTN_RIGHT);
1440	input_set_capability(dev3, EV_KEY, BTN_MIDDLE);
1441
1442	__set_bit(INPUT_PROP_POINTER, dev3->propbit);
1443
1444	error = input_register_device(dev3);
1445	if (error) {
1446		psmouse_err(psmouse,
1447			    "failed to register secondary device: %d\n",
1448			    error);
1449		input_free_device(dev3);
1450		goto out;
1451	}
1452
1453	priv->dev3 = dev3;
1454
1455out:
1456	/*
1457	 * Save the error code so that we can detect that we
1458	 * already tried to create the device.
1459	 */
1460	if (error)
1461		priv->dev3 = ERR_PTR(error);
1462
1463	mutex_unlock(&alps_mutex);
1464}
1465
1466static void alps_report_bare_ps2_packet(struct psmouse *psmouse,
1467					unsigned char packet[],
1468					bool report_buttons)
1469{
1470	struct alps_data *priv = psmouse->private;
1471	struct input_dev *dev, *dev2 = NULL;
1472
1473	/* Figure out which device to use to report the bare packet */
1474	if (priv->proto_version == ALPS_PROTO_V2 &&
1475	    (priv->flags & ALPS_DUALPOINT)) {
1476		/* On V2 devices the DualPoint Stick reports bare packets */
1477		dev = priv->dev2;
1478		dev2 = psmouse->dev;
1479	} else if (unlikely(IS_ERR_OR_NULL(priv->dev3))) {
1480		/* Register dev3 mouse if we received PS/2 packet first time */
1481		if (!IS_ERR(priv->dev3))
1482			psmouse_queue_work(psmouse, &priv->dev3_register_work,
1483					   0);
1484		return;
1485	} else {
1486		dev = priv->dev3;
1487	}
1488
1489	if (report_buttons)
1490		alps_report_buttons(dev, dev2,
1491				packet[0] & 1, packet[0] & 2, packet[0] & 4);
1492
1493	psmouse_report_standard_motion(dev, packet);
1494
1495	input_sync(dev);
1496}
1497
1498static psmouse_ret_t alps_handle_interleaved_ps2(struct psmouse *psmouse)
1499{
1500	struct alps_data *priv = psmouse->private;
1501
1502	if (psmouse->pktcnt < 6)
1503		return PSMOUSE_GOOD_DATA;
1504
1505	if (psmouse->pktcnt == 6) {
1506		/*
1507		 * Start a timer to flush the packet if it ends up last
1508		 * 6-byte packet in the stream. Timer needs to fire
1509		 * psmouse core times out itself. 20 ms should be enough
1510		 * to decide if we are getting more data or not.
1511		 */
1512		mod_timer(&priv->timer, jiffies + msecs_to_jiffies(20));
1513		return PSMOUSE_GOOD_DATA;
1514	}
1515
1516	del_timer(&priv->timer);
1517
1518	if (psmouse->packet[6] & 0x80) {
1519
1520		/*
1521		 * Highest bit is set - that means we either had
1522		 * complete ALPS packet and this is start of the
1523		 * next packet or we got garbage.
1524		 */
1525
1526		if (((psmouse->packet[3] |
1527		      psmouse->packet[4] |
1528		      psmouse->packet[5]) & 0x80) ||
1529		    (!alps_is_valid_first_byte(priv, psmouse->packet[6]))) {
1530			psmouse_dbg(psmouse,
1531				    "refusing packet %4ph (suspected interleaved ps/2)\n",
1532				    psmouse->packet + 3);
1533			return PSMOUSE_BAD_DATA;
1534		}
1535
1536		priv->process_packet(psmouse);
1537
1538		/* Continue with the next packet */
1539		psmouse->packet[0] = psmouse->packet[6];
1540		psmouse->pktcnt = 1;
1541
1542	} else {
1543
1544		/*
1545		 * High bit is 0 - that means that we indeed got a PS/2
1546		 * packet in the middle of ALPS packet.
1547		 *
1548		 * There is also possibility that we got 6-byte ALPS
1549		 * packet followed  by 3-byte packet from trackpoint. We
1550		 * can not distinguish between these 2 scenarios but
1551		 * because the latter is unlikely to happen in course of
1552		 * normal operation (user would need to press all
1553		 * buttons on the pad and start moving trackpoint
1554		 * without touching the pad surface) we assume former.
1555		 * Even if we are wrong the wost thing that would happen
1556		 * the cursor would jump but we should not get protocol
1557		 * de-synchronization.
1558		 */
1559
1560		alps_report_bare_ps2_packet(psmouse, &psmouse->packet[3],
1561					    false);
1562
1563		/*
1564		 * Continue with the standard ALPS protocol handling,
1565		 * but make sure we won't process it as an interleaved
1566		 * packet again, which may happen if all buttons are
1567		 * pressed. To avoid this let's reset the 4th bit which
1568		 * is normally 1.
1569		 */
1570		psmouse->packet[3] = psmouse->packet[6] & 0xf7;
1571		psmouse->pktcnt = 4;
1572	}
1573
1574	return PSMOUSE_GOOD_DATA;
1575}
1576
1577static void alps_flush_packet(struct timer_list *t)
1578{
1579	struct alps_data *priv = from_timer(priv, t, timer);
1580	struct psmouse *psmouse = priv->psmouse;
1581
1582	serio_pause_rx(psmouse->ps2dev.serio);
1583
1584	if (psmouse->pktcnt == psmouse->pktsize) {
1585
1586		/*
1587		 * We did not any more data in reasonable amount of time.
1588		 * Validate the last 3 bytes and process as a standard
1589		 * ALPS packet.
1590		 */
1591		if ((psmouse->packet[3] |
1592		     psmouse->packet[4] |
1593		     psmouse->packet[5]) & 0x80) {
1594			psmouse_dbg(psmouse,
1595				    "refusing packet %3ph (suspected interleaved ps/2)\n",
1596				    psmouse->packet + 3);
1597		} else {
1598			priv->process_packet(psmouse);
1599		}
1600		psmouse->pktcnt = 0;
1601	}
1602
1603	serio_continue_rx(psmouse->ps2dev.serio);
1604}
1605
1606static psmouse_ret_t alps_process_byte(struct psmouse *psmouse)
1607{
1608	struct alps_data *priv = psmouse->private;
1609
1610	/*
1611	 * Check if we are dealing with a bare PS/2 packet, presumably from
1612	 * a device connected to the external PS/2 port. Because bare PS/2
1613	 * protocol does not have enough constant bits to self-synchronize
1614	 * properly we only do this if the device is fully synchronized.
1615	 * Can not distinguish V8's first byte from PS/2 packet's
1616	 */
1617	if (priv->proto_version != ALPS_PROTO_V8 &&
1618	    !psmouse->out_of_sync_cnt &&
1619	    (psmouse->packet[0] & 0xc8) == 0x08) {
1620
1621		if (psmouse->pktcnt == 3) {
1622			alps_report_bare_ps2_packet(psmouse, psmouse->packet,
1623						    true);
1624			return PSMOUSE_FULL_PACKET;
1625		}
1626		return PSMOUSE_GOOD_DATA;
1627	}
1628
1629	/* Check for PS/2 packet stuffed in the middle of ALPS packet. */
1630
1631	if ((priv->flags & ALPS_PS2_INTERLEAVED) &&
1632	    psmouse->pktcnt >= 4 && (psmouse->packet[3] & 0x0f) == 0x0f) {
1633		return alps_handle_interleaved_ps2(psmouse);
1634	}
1635
1636	if (!alps_is_valid_first_byte(priv, psmouse->packet[0])) {
1637		psmouse_dbg(psmouse,
1638			    "refusing packet[0] = %x (mask0 = %x, byte0 = %x)\n",
1639			    psmouse->packet[0], priv->mask0, priv->byte0);
1640		return PSMOUSE_BAD_DATA;
1641	}
1642
1643	/* Bytes 2 - pktsize should have 0 in the highest bit */
1644	if (priv->proto_version < ALPS_PROTO_V5 &&
1645	    psmouse->pktcnt >= 2 && psmouse->pktcnt <= psmouse->pktsize &&
1646	    (psmouse->packet[psmouse->pktcnt - 1] & 0x80)) {
1647		psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
1648			    psmouse->pktcnt - 1,
1649			    psmouse->packet[psmouse->pktcnt - 1]);
1650
1651		if (priv->proto_version == ALPS_PROTO_V3_RUSHMORE &&
1652		    psmouse->pktcnt == psmouse->pktsize) {
1653			/*
1654			 * Some Dell boxes, such as Latitude E6440 or E7440
1655			 * with closed lid, quite often smash last byte of
1656			 * otherwise valid packet with 0xff. Given that the
1657			 * next packet is very likely to be valid let's
1658			 * report PSMOUSE_FULL_PACKET but not process data,
1659			 * rather than reporting PSMOUSE_BAD_DATA and
1660			 * filling the logs.
1661			 */
1662			return PSMOUSE_FULL_PACKET;
1663		}
1664
1665		return PSMOUSE_BAD_DATA;
1666	}
1667
1668	if ((priv->proto_version == ALPS_PROTO_V7 &&
1669			!alps_is_valid_package_v7(psmouse)) ||
1670	    (priv->proto_version == ALPS_PROTO_V8 &&
1671			!alps_is_valid_package_ss4_v2(psmouse))) {
1672		psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
1673			    psmouse->pktcnt - 1,
1674			    psmouse->packet[psmouse->pktcnt - 1]);
1675		return PSMOUSE_BAD_DATA;
1676	}
1677
1678	if (psmouse->pktcnt == psmouse->pktsize) {
1679		priv->process_packet(psmouse);
1680		return PSMOUSE_FULL_PACKET;
1681	}
1682
1683	return PSMOUSE_GOOD_DATA;
1684}
1685
1686static int alps_command_mode_send_nibble(struct psmouse *psmouse, int nibble)
1687{
1688	struct ps2dev *ps2dev = &psmouse->ps2dev;
1689	struct alps_data *priv = psmouse->private;
1690	int command;
1691	unsigned char *param;
1692	unsigned char dummy[4];
1693
1694	BUG_ON(nibble > 0xf);
1695
1696	command = priv->nibble_commands[nibble].command;
1697	param = (command & 0x0f00) ?
1698		dummy : (unsigned char *)&priv->nibble_commands[nibble].data;
1699
1700	if (ps2_command(ps2dev, param, command))
1701		return -1;
1702
1703	return 0;
1704}
1705
1706static int alps_command_mode_set_addr(struct psmouse *psmouse, int addr)
1707{
1708	struct ps2dev *ps2dev = &psmouse->ps2dev;
1709	struct alps_data *priv = psmouse->private;
1710	int i, nibble;
1711
1712	if (ps2_command(ps2dev, NULL, priv->addr_command))
1713		return -1;
1714
1715	for (i = 12; i >= 0; i -= 4) {
1716		nibble = (addr >> i) & 0xf;
1717		if (alps_command_mode_send_nibble(psmouse, nibble))
1718			return -1;
1719	}
1720
1721	return 0;
1722}
1723
1724static int __alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
1725{
1726	struct ps2dev *ps2dev = &psmouse->ps2dev;
1727	unsigned char param[4];
1728
1729	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1730		return -1;
1731
1732	/*
1733	 * The address being read is returned in the first two bytes
1734	 * of the result. Check that this address matches the expected
1735	 * address.
1736	 */
1737	if (addr != ((param[0] << 8) | param[1]))
1738		return -1;
1739
1740	return param[2];
1741}
1742
1743static int alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
1744{
1745	if (alps_command_mode_set_addr(psmouse, addr))
1746		return -1;
1747	return __alps_command_mode_read_reg(psmouse, addr);
1748}
1749
1750static int __alps_command_mode_write_reg(struct psmouse *psmouse, u8 value)
1751{
1752	if (alps_command_mode_send_nibble(psmouse, (value >> 4) & 0xf))
1753		return -1;
1754	if (alps_command_mode_send_nibble(psmouse, value & 0xf))
1755		return -1;
1756	return 0;
1757}
1758
1759static int alps_command_mode_write_reg(struct psmouse *psmouse, int addr,
1760				       u8 value)
1761{
1762	if (alps_command_mode_set_addr(psmouse, addr))
1763		return -1;
1764	return __alps_command_mode_write_reg(psmouse, value);
1765}
1766
1767static int alps_rpt_cmd(struct psmouse *psmouse, int init_command,
1768			int repeated_command, unsigned char *param)
1769{
1770	struct ps2dev *ps2dev = &psmouse->ps2dev;
1771
1772	param[0] = 0;
1773	if (init_command && ps2_command(ps2dev, param, init_command))
1774		return -EIO;
1775
1776	if (ps2_command(ps2dev,  NULL, repeated_command) ||
1777	    ps2_command(ps2dev,  NULL, repeated_command) ||
1778	    ps2_command(ps2dev,  NULL, repeated_command))
1779		return -EIO;
1780
1781	param[0] = param[1] = param[2] = 0xff;
1782	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1783		return -EIO;
1784
1785	psmouse_dbg(psmouse, "%2.2X report: %3ph\n",
1786		    repeated_command, param);
1787	return 0;
1788}
1789
1790static bool alps_check_valid_firmware_id(unsigned char id[])
1791{
1792	if (id[0] == 0x73)
1793		return true;
1794
1795	if (id[0] == 0x88 &&
1796	    (id[1] == 0x07 ||
1797	     id[1] == 0x08 ||
1798	     (id[1] & 0xf0) == 0xb0 ||
1799	     (id[1] & 0xf0) == 0xc0)) {
1800		return true;
1801	}
1802
1803	return false;
1804}
1805
1806static int alps_enter_command_mode(struct psmouse *psmouse)
1807{
1808	unsigned char param[4];
1809
1810	if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_RESET_WRAP, param)) {
1811		psmouse_err(psmouse, "failed to enter command mode\n");
1812		return -1;
1813	}
1814
1815	if (!alps_check_valid_firmware_id(param)) {
1816		psmouse_dbg(psmouse,
1817			    "unknown response while entering command mode\n");
1818		return -1;
1819	}
1820	return 0;
1821}
1822
1823static inline int alps_exit_command_mode(struct psmouse *psmouse)
1824{
1825	struct ps2dev *ps2dev = &psmouse->ps2dev;
1826	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM))
1827		return -1;
1828	return 0;
1829}
1830
1831/*
1832 * For DualPoint devices select the device that should respond to
1833 * subsequent commands. It looks like glidepad is behind stickpointer,
1834 * I'd thought it would be other way around...
1835 */
1836static int alps_passthrough_mode_v2(struct psmouse *psmouse, bool enable)
1837{
1838	struct ps2dev *ps2dev = &psmouse->ps2dev;
1839	int cmd = enable ? PSMOUSE_CMD_SETSCALE21 : PSMOUSE_CMD_SETSCALE11;
1840
1841	if (ps2_command(ps2dev, NULL, cmd) ||
1842	    ps2_command(ps2dev, NULL, cmd) ||
1843	    ps2_command(ps2dev, NULL, cmd) ||
1844	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1845		return -1;
1846
1847	/* we may get 3 more bytes, just ignore them */
1848	ps2_drain(ps2dev, 3, 100);
1849
1850	return 0;
1851}
1852
1853static int alps_absolute_mode_v1_v2(struct psmouse *psmouse)
1854{
1855	struct ps2dev *ps2dev = &psmouse->ps2dev;
1856
1857	/* Try ALPS magic knock - 4 disable before enable */
1858	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1859	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1860	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1861	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1862	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
1863		return -1;
1864
1865	/*
1866	 * Switch mouse to poll (remote) mode so motion data will not
1867	 * get in our way
1868	 */
1869	return ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL);
1870}
1871
1872static int alps_monitor_mode_send_word(struct psmouse *psmouse, u16 word)
1873{
1874	int i, nibble;
1875
1876	/*
1877	 * b0-b11 are valid bits, send sequence is inverse.
1878	 * e.g. when word = 0x0123, nibble send sequence is 3, 2, 1
1879	 */
1880	for (i = 0; i <= 8; i += 4) {
1881		nibble = (word >> i) & 0xf;
1882		if (alps_command_mode_send_nibble(psmouse, nibble))
1883			return -1;
1884	}
1885
1886	return 0;
1887}
1888
1889static int alps_monitor_mode_write_reg(struct psmouse *psmouse,
1890				       u16 addr, u16 value)
1891{
1892	struct ps2dev *ps2dev = &psmouse->ps2dev;
1893
1894	/* 0x0A0 is the command to write the word */
1895	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE) ||
1896	    alps_monitor_mode_send_word(psmouse, 0x0A0) ||
1897	    alps_monitor_mode_send_word(psmouse, addr) ||
1898	    alps_monitor_mode_send_word(psmouse, value) ||
1899	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1900		return -1;
1901
1902	return 0;
1903}
1904
1905static int alps_monitor_mode(struct psmouse *psmouse, bool enable)
1906{
1907	struct ps2dev *ps2dev = &psmouse->ps2dev;
1908
1909	if (enable) {
1910		/* EC E9 F5 F5 E7 E6 E7 E9 to enter monitor mode */
1911		if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
1912		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO) ||
1913		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1914		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1915		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1916		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1917		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1918		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO))
1919			return -1;
1920	} else {
1921		/* EC to exit monitor mode */
1922		if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP))
1923			return -1;
1924	}
1925
1926	return 0;
1927}
1928
1929static int alps_absolute_mode_v6(struct psmouse *psmouse)
1930{
1931	u16 reg_val = 0x181;
1932	int ret;
1933
1934	/* enter monitor mode, to write the register */
1935	if (alps_monitor_mode(psmouse, true))
1936		return -1;
1937
1938	ret = alps_monitor_mode_write_reg(psmouse, 0x000, reg_val);
1939
1940	if (alps_monitor_mode(psmouse, false))
1941		ret = -1;
1942
1943	return ret;
1944}
1945
1946static int alps_get_status(struct psmouse *psmouse, char *param)
1947{
1948	/* Get status: 0xF5 0xF5 0xF5 0xE9 */
1949	if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_DISABLE, param))
1950		return -1;
1951
1952	return 0;
1953}
1954
1955/*
1956 * Turn touchpad tapping on or off. The sequences are:
1957 * 0xE9 0xF5 0xF5 0xF3 0x0A to enable,
1958 * 0xE9 0xF5 0xF5 0xE8 0x00 to disable.
1959 * My guess that 0xE9 (GetInfo) is here as a sync point.
1960 * For models that also have stickpointer (DualPoints) its tapping
1961 * is controlled separately (0xE6 0xE6 0xE6 0xF3 0x14|0x0A) but
1962 * we don't fiddle with it.
1963 */
1964static int alps_tap_mode(struct psmouse *psmouse, int enable)
1965{
1966	struct ps2dev *ps2dev = &psmouse->ps2dev;
1967	int cmd = enable ? PSMOUSE_CMD_SETRATE : PSMOUSE_CMD_SETRES;
1968	unsigned char tap_arg = enable ? 0x0A : 0x00;
1969	unsigned char param[4];
1970
1971	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO) ||
1972	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1973	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1974	    ps2_command(ps2dev, &tap_arg, cmd))
1975		return -1;
1976
1977	if (alps_get_status(psmouse, param))
1978		return -1;
1979
1980	return 0;
1981}
1982
1983/*
1984 * alps_poll() - poll the touchpad for current motion packet.
1985 * Used in resync.
1986 */
1987static int alps_poll(struct psmouse *psmouse)
1988{
1989	struct alps_data *priv = psmouse->private;
1990	unsigned char buf[sizeof(psmouse->packet)];
1991	bool poll_failed;
1992
1993	if (priv->flags & ALPS_PASS)
1994		alps_passthrough_mode_v2(psmouse, true);
1995
1996	poll_failed = ps2_command(&psmouse->ps2dev, buf,
1997				  PSMOUSE_CMD_POLL | (psmouse->pktsize << 8)) < 0;
1998
1999	if (priv->flags & ALPS_PASS)
2000		alps_passthrough_mode_v2(psmouse, false);
2001
2002	if (poll_failed || (buf[0] & priv->mask0) != priv->byte0)
2003		return -1;
2004
2005	if ((psmouse->badbyte & 0xc8) == 0x08) {
2006/*
2007 * Poll the track stick ...
2008 */
2009		if (ps2_command(&psmouse->ps2dev, buf, PSMOUSE_CMD_POLL | (3 << 8)))
2010			return -1;
2011	}
2012
2013	memcpy(psmouse->packet, buf, sizeof(buf));
2014	return 0;
2015}
2016
2017static int alps_hw_init_v1_v2(struct psmouse *psmouse)
2018{
2019	struct alps_data *priv = psmouse->private;
2020
2021	if ((priv->flags & ALPS_PASS) &&
2022	    alps_passthrough_mode_v2(psmouse, true)) {
2023		return -1;
2024	}
2025
2026	if (alps_tap_mode(psmouse, true)) {
2027		psmouse_warn(psmouse, "Failed to enable hardware tapping\n");
2028		return -1;
2029	}
2030
2031	if (alps_absolute_mode_v1_v2(psmouse)) {
2032		psmouse_err(psmouse, "Failed to enable absolute mode\n");
2033		return -1;
2034	}
2035
2036	if ((priv->flags & ALPS_PASS) &&
2037	    alps_passthrough_mode_v2(psmouse, false)) {
2038		return -1;
2039	}
2040
2041	/* ALPS needs stream mode, otherwise it won't report any data */
2042	if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)) {
2043		psmouse_err(psmouse, "Failed to enable stream mode\n");
2044		return -1;
2045	}
2046
2047	return 0;
2048}
2049
2050/* Must be in passthrough mode when calling this function */
2051static int alps_trackstick_enter_extended_mode_v3_v6(struct psmouse *psmouse)
2052{
2053	unsigned char param[2] = {0xC8, 0x14};
2054
 
 
 
 
2055	if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
2056	    ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
2057	    ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
2058	    ps2_command(&psmouse->ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2059	    ps2_command(&psmouse->ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
2060		return -1;
2061
2062	return 0;
2063}
2064
2065static int alps_hw_init_v6(struct psmouse *psmouse)
2066{
2067	int ret;
2068
2069	/* Enter passthrough mode to let trackpoint enter 6byte raw mode */
2070	if (alps_passthrough_mode_v2(psmouse, true))
2071		return -1;
2072
2073	ret = alps_trackstick_enter_extended_mode_v3_v6(psmouse);
2074
2075	if (alps_passthrough_mode_v2(psmouse, false))
2076		return -1;
2077
2078	if (ret)
2079		return ret;
2080
2081	if (alps_absolute_mode_v6(psmouse)) {
2082		psmouse_err(psmouse, "Failed to enable absolute mode\n");
2083		return -1;
2084	}
2085
2086	return 0;
2087}
2088
2089/*
2090 * Enable or disable passthrough mode to the trackstick.
2091 */
2092static int alps_passthrough_mode_v3(struct psmouse *psmouse,
2093				    int reg_base, bool enable)
2094{
2095	int reg_val, ret = -1;
2096
2097	if (alps_enter_command_mode(psmouse))
2098		return -1;
2099
2100	reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x0008);
2101	if (reg_val == -1)
2102		goto error;
2103
2104	if (enable)
2105		reg_val |= 0x01;
2106	else
2107		reg_val &= ~0x01;
2108
2109	ret = __alps_command_mode_write_reg(psmouse, reg_val);
2110
2111error:
2112	if (alps_exit_command_mode(psmouse))
2113		ret = -1;
2114	return ret;
2115}
2116
2117/* Must be in command mode when calling this function */
2118static int alps_absolute_mode_v3(struct psmouse *psmouse)
2119{
2120	int reg_val;
2121
2122	reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
2123	if (reg_val == -1)
2124		return -1;
2125
2126	reg_val |= 0x06;
2127	if (__alps_command_mode_write_reg(psmouse, reg_val))
2128		return -1;
2129
2130	return 0;
2131}
2132
2133static int alps_probe_trackstick_v3_v7(struct psmouse *psmouse, int reg_base)
2134{
2135	int ret = -EIO, reg_val;
2136
2137	if (alps_enter_command_mode(psmouse))
2138		goto error;
2139
2140	reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x08);
2141	if (reg_val == -1)
2142		goto error;
2143
2144	/* bit 7: trackstick is present */
2145	ret = reg_val & 0x80 ? 0 : -ENODEV;
2146
2147error:
2148	alps_exit_command_mode(psmouse);
2149	return ret;
2150}
2151
2152static int alps_setup_trackstick_v3(struct psmouse *psmouse, int reg_base)
2153{
 
2154	int ret = 0;
2155	int reg_val;
2156	unsigned char param[4];
2157
2158	/*
2159	 * We need to configure trackstick to report data for touchpad in
2160	 * extended format. And also we need to tell touchpad to expect data
2161	 * from trackstick in extended format. Without this configuration
2162	 * trackstick packets sent from touchpad are in basic format which is
2163	 * different from what we expect.
2164	 */
2165
2166	if (alps_passthrough_mode_v3(psmouse, reg_base, true))
2167		return -EIO;
2168
2169	/*
2170	 * E7 report for the trackstick
2171	 *
2172	 * There have been reports of failures to seem to trace back
2173	 * to the above trackstick check failing. When these occur
2174	 * this E7 report fails, so when that happens we continue
2175	 * with the assumption that there isn't a trackstick after
2176	 * all.
2177	 */
2178	if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_SETSCALE21, param)) {
2179		psmouse_warn(psmouse, "Failed to initialize trackstick (E7 report failed)\n");
2180		ret = -ENODEV;
2181	} else {
2182		psmouse_dbg(psmouse, "trackstick E7 report: %3ph\n", param);
2183		if (alps_trackstick_enter_extended_mode_v3_v6(psmouse)) {
2184			psmouse_err(psmouse, "Failed to enter into trackstick extended mode\n");
 
 
 
 
 
 
 
 
 
 
 
 
2185			ret = -EIO;
 
2186		}
2187	}
2188
2189	if (alps_passthrough_mode_v3(psmouse, reg_base, false))
2190		return -EIO;
2191
2192	if (ret)
2193		return ret;
2194
2195	if (alps_enter_command_mode(psmouse))
2196		return -EIO;
2197
2198	reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x08);
2199	if (reg_val == -1) {
2200		ret = -EIO;
2201	} else {
2202		/*
2203		 * Tell touchpad that trackstick is now in extended mode.
2204		 * If bit 1 isn't set the packet format is different.
 
2205		 */
2206		reg_val |= BIT(1);
2207		if (__alps_command_mode_write_reg(psmouse, reg_val))
 
 
2208			ret = -EIO;
2209	}
2210
2211	if (alps_exit_command_mode(psmouse))
2212		return -EIO;
 
2213
2214	return ret;
2215}
2216
2217static int alps_hw_init_v3(struct psmouse *psmouse)
2218{
2219	struct alps_data *priv = psmouse->private;
2220	struct ps2dev *ps2dev = &psmouse->ps2dev;
2221	int reg_val;
2222	unsigned char param[4];
2223
2224	if ((priv->flags & ALPS_DUALPOINT) &&
2225	    alps_setup_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE) == -EIO)
2226		goto error;
2227
2228	if (alps_enter_command_mode(psmouse) ||
2229	    alps_absolute_mode_v3(psmouse)) {
2230		psmouse_err(psmouse, "Failed to enter absolute mode\n");
2231		goto error;
2232	}
2233
2234	reg_val = alps_command_mode_read_reg(psmouse, 0x0006);
2235	if (reg_val == -1)
2236		goto error;
2237	if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
2238		goto error;
2239
2240	reg_val = alps_command_mode_read_reg(psmouse, 0x0007);
2241	if (reg_val == -1)
2242		goto error;
2243	if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
2244		goto error;
2245
2246	if (alps_command_mode_read_reg(psmouse, 0x0144) == -1)
2247		goto error;
2248	if (__alps_command_mode_write_reg(psmouse, 0x04))
2249		goto error;
2250
2251	if (alps_command_mode_read_reg(psmouse, 0x0159) == -1)
2252		goto error;
2253	if (__alps_command_mode_write_reg(psmouse, 0x03))
2254		goto error;
2255
2256	if (alps_command_mode_read_reg(psmouse, 0x0163) == -1)
2257		goto error;
2258	if (alps_command_mode_write_reg(psmouse, 0x0163, 0x03))
2259		goto error;
2260
2261	if (alps_command_mode_read_reg(psmouse, 0x0162) == -1)
2262		goto error;
2263	if (alps_command_mode_write_reg(psmouse, 0x0162, 0x04))
2264		goto error;
2265
2266	alps_exit_command_mode(psmouse);
2267
2268	/* Set rate and enable data reporting */
2269	param[0] = 0x64;
2270	if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
2271	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
2272		psmouse_err(psmouse, "Failed to enable data reporting\n");
2273		return -1;
2274	}
2275
2276	return 0;
2277
2278error:
2279	/*
2280	 * Leaving the touchpad in command mode will essentially render
2281	 * it unusable until the machine reboots, so exit it here just
2282	 * to be safe
2283	 */
2284	alps_exit_command_mode(psmouse);
2285	return -1;
2286}
2287
2288static int alps_get_v3_v7_resolution(struct psmouse *psmouse, int reg_pitch)
2289{
2290	int reg, x_pitch, y_pitch, x_electrode, y_electrode, x_phys, y_phys;
2291	struct alps_data *priv = psmouse->private;
2292
2293	reg = alps_command_mode_read_reg(psmouse, reg_pitch);
2294	if (reg < 0)
2295		return reg;
2296
2297	x_pitch = (s8)(reg << 4) >> 4; /* sign extend lower 4 bits */
2298	x_pitch = 50 + 2 * x_pitch; /* In 0.1 mm units */
2299
2300	y_pitch = (s8)reg >> 4; /* sign extend upper 4 bits */
2301	y_pitch = 36 + 2 * y_pitch; /* In 0.1 mm units */
2302
2303	reg = alps_command_mode_read_reg(psmouse, reg_pitch + 1);
2304	if (reg < 0)
2305		return reg;
2306
2307	x_electrode = (s8)(reg << 4) >> 4; /* sign extend lower 4 bits */
2308	x_electrode = 17 + x_electrode;
2309
2310	y_electrode = (s8)reg >> 4; /* sign extend upper 4 bits */
2311	y_electrode = 13 + y_electrode;
2312
2313	x_phys = x_pitch * (x_electrode - 1); /* In 0.1 mm units */
2314	y_phys = y_pitch * (y_electrode - 1); /* In 0.1 mm units */
2315
2316	priv->x_res = priv->x_max * 10 / x_phys; /* units / mm */
2317	priv->y_res = priv->y_max * 10 / y_phys; /* units / mm */
2318
2319	psmouse_dbg(psmouse,
2320		    "pitch %dx%d num-electrodes %dx%d physical size %dx%d mm res %dx%d\n",
2321		    x_pitch, y_pitch, x_electrode, y_electrode,
2322		    x_phys / 10, y_phys / 10, priv->x_res, priv->y_res);
2323
2324	return 0;
2325}
2326
2327static int alps_hw_init_rushmore_v3(struct psmouse *psmouse)
2328{
2329	struct alps_data *priv = psmouse->private;
2330	struct ps2dev *ps2dev = &psmouse->ps2dev;
2331	int reg_val, ret = -1;
2332
2333	if (priv->flags & ALPS_DUALPOINT) {
2334		reg_val = alps_setup_trackstick_v3(psmouse,
2335						   ALPS_REG_BASE_RUSHMORE);
2336		if (reg_val == -EIO)
2337			goto error;
2338	}
2339
2340	if (alps_enter_command_mode(psmouse) ||
2341	    alps_command_mode_read_reg(psmouse, 0xc2d9) == -1 ||
2342	    alps_command_mode_write_reg(psmouse, 0xc2cb, 0x00))
2343		goto error;
2344
2345	if (alps_get_v3_v7_resolution(psmouse, 0xc2da))
2346		goto error;
2347
2348	reg_val = alps_command_mode_read_reg(psmouse, 0xc2c6);
2349	if (reg_val == -1)
2350		goto error;
2351	if (__alps_command_mode_write_reg(psmouse, reg_val & 0xfd))
2352		goto error;
2353
2354	if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
2355		goto error;
2356
2357	/* enter absolute mode */
2358	reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
2359	if (reg_val == -1)
2360		goto error;
2361	if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
2362		goto error;
2363
2364	alps_exit_command_mode(psmouse);
2365	return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
2366
2367error:
2368	alps_exit_command_mode(psmouse);
2369	return ret;
2370}
2371
2372/* Must be in command mode when calling this function */
2373static int alps_absolute_mode_v4(struct psmouse *psmouse)
2374{
2375	int reg_val;
2376
2377	reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
2378	if (reg_val == -1)
2379		return -1;
2380
2381	reg_val |= 0x02;
2382	if (__alps_command_mode_write_reg(psmouse, reg_val))
2383		return -1;
2384
2385	return 0;
2386}
2387
2388static int alps_hw_init_v4(struct psmouse *psmouse)
2389{
2390	struct ps2dev *ps2dev = &psmouse->ps2dev;
2391	unsigned char param[4];
2392
2393	if (alps_enter_command_mode(psmouse))
2394		goto error;
2395
2396	if (alps_absolute_mode_v4(psmouse)) {
2397		psmouse_err(psmouse, "Failed to enter absolute mode\n");
2398		goto error;
2399	}
2400
2401	if (alps_command_mode_write_reg(psmouse, 0x0007, 0x8c))
2402		goto error;
2403
2404	if (alps_command_mode_write_reg(psmouse, 0x0149, 0x03))
2405		goto error;
2406
2407	if (alps_command_mode_write_reg(psmouse, 0x0160, 0x03))
2408		goto error;
2409
2410	if (alps_command_mode_write_reg(psmouse, 0x017f, 0x15))
2411		goto error;
2412
2413	if (alps_command_mode_write_reg(psmouse, 0x0151, 0x01))
2414		goto error;
2415
2416	if (alps_command_mode_write_reg(psmouse, 0x0168, 0x03))
2417		goto error;
2418
2419	if (alps_command_mode_write_reg(psmouse, 0x014a, 0x03))
2420		goto error;
2421
2422	if (alps_command_mode_write_reg(psmouse, 0x0161, 0x03))
2423		goto error;
2424
2425	alps_exit_command_mode(psmouse);
2426
2427	/*
2428	 * This sequence changes the output from a 9-byte to an
2429	 * 8-byte format. All the same data seems to be present,
2430	 * just in a more compact format.
2431	 */
2432	param[0] = 0xc8;
2433	param[1] = 0x64;
2434	param[2] = 0x50;
2435	if (ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2436	    ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE) ||
2437	    ps2_command(ps2dev, &param[2], PSMOUSE_CMD_SETRATE) ||
2438	    ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
2439		return -1;
2440
2441	/* Set rate and enable data reporting */
2442	param[0] = 0x64;
2443	if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
2444	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
2445		psmouse_err(psmouse, "Failed to enable data reporting\n");
2446		return -1;
2447	}
2448
2449	return 0;
2450
2451error:
2452	/*
2453	 * Leaving the touchpad in command mode will essentially render
2454	 * it unusable until the machine reboots, so exit it here just
2455	 * to be safe
2456	 */
2457	alps_exit_command_mode(psmouse);
2458	return -1;
2459}
2460
2461static int alps_get_otp_values_ss4_v2(struct psmouse *psmouse,
2462				      unsigned char index, unsigned char otp[])
2463{
2464	struct ps2dev *ps2dev = &psmouse->ps2dev;
2465
2466	switch (index) {
2467	case 0:
2468		if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)  ||
2469		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)  ||
2470		    ps2_command(ps2dev, otp, PSMOUSE_CMD_GETINFO))
2471			return -1;
2472
2473		break;
2474
2475	case 1:
2476		if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL)  ||
2477		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL)  ||
2478		    ps2_command(ps2dev, otp, PSMOUSE_CMD_GETINFO))
2479			return -1;
2480
2481		break;
2482	}
2483
2484	return 0;
2485}
2486
2487static int alps_update_device_area_ss4_v2(unsigned char otp[][4],
2488					  struct alps_data *priv)
2489{
2490	int num_x_electrode;
2491	int num_y_electrode;
2492	int x_pitch, y_pitch, x_phys, y_phys;
2493
2494	if (IS_SS4PLUS_DEV(priv->dev_id)) {
2495		num_x_electrode =
2496			SS4PLUS_NUMSENSOR_XOFFSET + (otp[0][2] & 0x0F);
2497		num_y_electrode =
2498			SS4PLUS_NUMSENSOR_YOFFSET + ((otp[0][2] >> 4) & 0x0F);
2499
2500		priv->x_max =
2501			(num_x_electrode - 1) * SS4PLUS_COUNT_PER_ELECTRODE;
2502		priv->y_max =
2503			(num_y_electrode - 1) * SS4PLUS_COUNT_PER_ELECTRODE;
2504
2505		x_pitch = (otp[0][1] & 0x0F) + SS4PLUS_MIN_PITCH_MM;
2506		y_pitch = ((otp[0][1] >> 4) & 0x0F) + SS4PLUS_MIN_PITCH_MM;
2507
2508	} else {
2509		num_x_electrode =
2510			SS4_NUMSENSOR_XOFFSET + (otp[1][0] & 0x0F);
2511		num_y_electrode =
2512			SS4_NUMSENSOR_YOFFSET + ((otp[1][0] >> 4) & 0x0F);
2513
2514		priv->x_max =
2515			(num_x_electrode - 1) * SS4_COUNT_PER_ELECTRODE;
2516		priv->y_max =
2517			(num_y_electrode - 1) * SS4_COUNT_PER_ELECTRODE;
2518
2519		x_pitch = ((otp[1][2] >> 2) & 0x07) + SS4_MIN_PITCH_MM;
2520		y_pitch = ((otp[1][2] >> 5) & 0x07) + SS4_MIN_PITCH_MM;
2521	}
2522
2523	x_phys = x_pitch * (num_x_electrode - 1); /* In 0.1 mm units */
2524	y_phys = y_pitch * (num_y_electrode - 1); /* In 0.1 mm units */
2525
2526	priv->x_res = priv->x_max * 10 / x_phys; /* units / mm */
2527	priv->y_res = priv->y_max * 10 / y_phys; /* units / mm */
2528
2529	return 0;
2530}
2531
2532static int alps_update_btn_info_ss4_v2(unsigned char otp[][4],
2533				       struct alps_data *priv)
2534{
2535	unsigned char is_btnless;
2536
2537	if (IS_SS4PLUS_DEV(priv->dev_id))
2538		is_btnless = (otp[1][0] >> 1) & 0x01;
2539	else
2540		is_btnless = (otp[1][1] >> 3) & 0x01;
2541
2542	if (is_btnless)
2543		priv->flags |= ALPS_BUTTONPAD;
2544
2545	return 0;
2546}
2547
2548static int alps_update_dual_info_ss4_v2(unsigned char otp[][4],
2549					struct alps_data *priv,
2550					struct psmouse *psmouse)
2551{
2552	bool is_dual = false;
2553	int reg_val = 0;
2554	struct ps2dev *ps2dev = &psmouse->ps2dev;
2555
2556	if (IS_SS4PLUS_DEV(priv->dev_id)) {
2557		is_dual = (otp[0][0] >> 4) & 0x01;
2558
2559		if (!is_dual) {
2560			/* For support TrackStick of Thinkpad L/E series */
2561			if (alps_exit_command_mode(psmouse) == 0 &&
2562				alps_enter_command_mode(psmouse) == 0) {
2563				reg_val = alps_command_mode_read_reg(psmouse,
2564									0xD7);
2565			}
2566			alps_exit_command_mode(psmouse);
2567			ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
2568
2569			if (reg_val == 0x0C || reg_val == 0x1D)
2570				is_dual = true;
2571		}
2572	}
2573
2574	if (is_dual)
2575		priv->flags |= ALPS_DUALPOINT |
2576					ALPS_DUALPOINT_WITH_PRESSURE;
2577
2578	return 0;
2579}
2580
2581static int alps_set_defaults_ss4_v2(struct psmouse *psmouse,
2582				    struct alps_data *priv)
2583{
2584	unsigned char otp[2][4];
2585
2586	memset(otp, 0, sizeof(otp));
2587
2588	if (alps_get_otp_values_ss4_v2(psmouse, 1, &otp[1][0]) ||
2589	    alps_get_otp_values_ss4_v2(psmouse, 0, &otp[0][0]))
2590		return -1;
2591
2592	alps_update_device_area_ss4_v2(otp, priv);
2593
2594	alps_update_btn_info_ss4_v2(otp, priv);
2595
2596	alps_update_dual_info_ss4_v2(otp, priv, psmouse);
2597
2598	return 0;
2599}
2600
2601static int alps_dolphin_get_device_area(struct psmouse *psmouse,
2602					struct alps_data *priv)
2603{
2604	struct ps2dev *ps2dev = &psmouse->ps2dev;
2605	unsigned char param[4] = {0};
2606	int num_x_electrode, num_y_electrode;
2607
2608	if (alps_enter_command_mode(psmouse))
2609		return -1;
2610
2611	param[0] = 0x0a;
2612	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
2613	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2614	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2615	    ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2616	    ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE))
2617		return -1;
2618
2619	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
2620		return -1;
2621
2622	/*
2623	 * Dolphin's sensor line number is not fixed. It can be calculated
2624	 * by adding the device's register value with DOLPHIN_PROFILE_X/YOFFSET.
2625	 * Further more, we can get device's x_max and y_max by multiplying
2626	 * sensor line number with DOLPHIN_COUNT_PER_ELECTRODE.
2627	 *
2628	 * e.g. When we get register's sensor_x = 11 & sensor_y = 8,
2629	 *	real sensor line number X = 11 + 8 = 19, and
2630	 *	real sensor line number Y = 8 + 1 = 9.
2631	 *	So, x_max = (19 - 1) * 64 = 1152, and
2632	 *	    y_max = (9 - 1) * 64 = 512.
2633	 */
2634	num_x_electrode = DOLPHIN_PROFILE_XOFFSET + (param[2] & 0x0F);
2635	num_y_electrode = DOLPHIN_PROFILE_YOFFSET + ((param[2] >> 4) & 0x0F);
2636	priv->x_bits = num_x_electrode;
2637	priv->y_bits = num_y_electrode;
2638	priv->x_max = (num_x_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
2639	priv->y_max = (num_y_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
2640
2641	if (alps_exit_command_mode(psmouse))
2642		return -1;
2643
2644	return 0;
2645}
2646
2647static int alps_hw_init_dolphin_v1(struct psmouse *psmouse)
2648{
2649	struct ps2dev *ps2dev = &psmouse->ps2dev;
2650	unsigned char param[2];
2651
2652	/* This is dolphin "v1" as empirically defined by florin9doi */
2653	param[0] = 0x64;
2654	param[1] = 0x28;
2655
2656	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2657	    ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2658	    ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
2659		return -1;
2660
2661	return 0;
2662}
2663
2664static int alps_hw_init_v7(struct psmouse *psmouse)
2665{
2666	struct ps2dev *ps2dev = &psmouse->ps2dev;
2667	int reg_val, ret = -1;
2668
2669	if (alps_enter_command_mode(psmouse) ||
2670	    alps_command_mode_read_reg(psmouse, 0xc2d9) == -1)
2671		goto error;
2672
2673	if (alps_get_v3_v7_resolution(psmouse, 0xc397))
2674		goto error;
2675
2676	if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
2677		goto error;
2678
2679	reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
2680	if (reg_val == -1)
2681		goto error;
2682	if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
2683		goto error;
2684
2685	alps_exit_command_mode(psmouse);
2686	return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
2687
2688error:
2689	alps_exit_command_mode(psmouse);
2690	return ret;
2691}
2692
2693static int alps_hw_init_ss4_v2(struct psmouse *psmouse)
2694{
2695	struct ps2dev *ps2dev = &psmouse->ps2dev;
2696	char param[2] = {0x64, 0x28};
2697	int ret = -1;
2698
2699	/* enter absolute mode */
2700	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2701	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2702	    ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2703	    ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE)) {
2704		goto error;
2705	}
2706
2707	/* T.B.D. Decread noise packet number, delete in the future */
2708	if (alps_exit_command_mode(psmouse) ||
2709	    alps_enter_command_mode(psmouse) ||
2710	    alps_command_mode_write_reg(psmouse, 0x001D, 0x20)) {
2711		goto error;
2712	}
2713	alps_exit_command_mode(psmouse);
2714
2715	return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
2716
2717error:
2718	alps_exit_command_mode(psmouse);
2719	return ret;
2720}
2721
2722static int alps_set_protocol(struct psmouse *psmouse,
2723			     struct alps_data *priv,
2724			     const struct alps_protocol_info *protocol)
2725{
2726	psmouse->private = priv;
2727
2728	timer_setup(&priv->timer, alps_flush_packet, 0);
2729
2730	priv->proto_version = protocol->version;
2731	priv->byte0 = protocol->byte0;
2732	priv->mask0 = protocol->mask0;
2733	priv->flags = protocol->flags;
2734
2735	priv->x_max = 2000;
2736	priv->y_max = 1400;
2737	priv->x_bits = 15;
2738	priv->y_bits = 11;
2739
2740	switch (priv->proto_version) {
2741	case ALPS_PROTO_V1:
2742	case ALPS_PROTO_V2:
2743		priv->hw_init = alps_hw_init_v1_v2;
2744		priv->process_packet = alps_process_packet_v1_v2;
2745		priv->set_abs_params = alps_set_abs_params_st;
2746		priv->x_max = 1023;
2747		priv->y_max = 767;
2748		if (dmi_check_system(alps_dmi_has_separate_stick_buttons))
2749			priv->flags |= ALPS_STICK_BITS;
2750		break;
2751
2752	case ALPS_PROTO_V3:
2753		priv->hw_init = alps_hw_init_v3;
2754		priv->process_packet = alps_process_packet_v3;
2755		priv->set_abs_params = alps_set_abs_params_semi_mt;
2756		priv->decode_fields = alps_decode_pinnacle;
2757		priv->nibble_commands = alps_v3_nibble_commands;
2758		priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2759
2760		if (alps_probe_trackstick_v3_v7(psmouse,
2761						ALPS_REG_BASE_PINNACLE) < 0)
2762			priv->flags &= ~ALPS_DUALPOINT;
2763
2764		break;
2765
2766	case ALPS_PROTO_V3_RUSHMORE:
2767		priv->hw_init = alps_hw_init_rushmore_v3;
2768		priv->process_packet = alps_process_packet_v3;
2769		priv->set_abs_params = alps_set_abs_params_semi_mt;
2770		priv->decode_fields = alps_decode_rushmore;
2771		priv->nibble_commands = alps_v3_nibble_commands;
2772		priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2773		priv->x_bits = 16;
2774		priv->y_bits = 12;
2775
2776		if (alps_probe_trackstick_v3_v7(psmouse,
2777						ALPS_REG_BASE_RUSHMORE) < 0)
2778			priv->flags &= ~ALPS_DUALPOINT;
2779
2780		break;
2781
2782	case ALPS_PROTO_V4:
2783		priv->hw_init = alps_hw_init_v4;
2784		priv->process_packet = alps_process_packet_v4;
2785		priv->set_abs_params = alps_set_abs_params_semi_mt;
2786		priv->nibble_commands = alps_v4_nibble_commands;
2787		priv->addr_command = PSMOUSE_CMD_DISABLE;
2788		break;
2789
2790	case ALPS_PROTO_V5:
2791		priv->hw_init = alps_hw_init_dolphin_v1;
2792		priv->process_packet = alps_process_touchpad_packet_v3_v5;
2793		priv->decode_fields = alps_decode_dolphin;
2794		priv->set_abs_params = alps_set_abs_params_semi_mt;
2795		priv->nibble_commands = alps_v3_nibble_commands;
2796		priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2797		priv->x_bits = 23;
2798		priv->y_bits = 12;
2799
2800		if (alps_dolphin_get_device_area(psmouse, priv))
2801			return -EIO;
2802
2803		break;
2804
2805	case ALPS_PROTO_V6:
2806		priv->hw_init = alps_hw_init_v6;
2807		priv->process_packet = alps_process_packet_v6;
2808		priv->set_abs_params = alps_set_abs_params_st;
2809		priv->nibble_commands = alps_v6_nibble_commands;
2810		priv->x_max = 2047;
2811		priv->y_max = 1535;
2812		break;
2813
2814	case ALPS_PROTO_V7:
2815		priv->hw_init = alps_hw_init_v7;
2816		priv->process_packet = alps_process_packet_v7;
2817		priv->decode_fields = alps_decode_packet_v7;
2818		priv->set_abs_params = alps_set_abs_params_v7;
2819		priv->nibble_commands = alps_v3_nibble_commands;
2820		priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2821		priv->x_max = 0xfff;
2822		priv->y_max = 0x7ff;
2823
2824		if (priv->fw_ver[1] != 0xba)
2825			priv->flags |= ALPS_BUTTONPAD;
2826
2827		if (alps_probe_trackstick_v3_v7(psmouse, ALPS_REG_BASE_V7) < 0)
2828			priv->flags &= ~ALPS_DUALPOINT;
2829
2830		break;
2831
2832	case ALPS_PROTO_V8:
2833		priv->hw_init = alps_hw_init_ss4_v2;
2834		priv->process_packet = alps_process_packet_ss4_v2;
2835		priv->decode_fields = alps_decode_ss4_v2;
2836		priv->set_abs_params = alps_set_abs_params_ss4_v2;
2837		priv->nibble_commands = alps_v3_nibble_commands;
2838		priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2839
2840		if (alps_set_defaults_ss4_v2(psmouse, priv))
2841			return -EIO;
2842
2843		break;
2844	}
2845
2846	return 0;
2847}
2848
2849static const struct alps_protocol_info *alps_match_table(unsigned char *e7,
2850							 unsigned char *ec)
2851{
2852	const struct alps_model_info *model;
2853	int i;
2854
2855	for (i = 0; i < ARRAY_SIZE(alps_model_data); i++) {
2856		model = &alps_model_data[i];
2857
2858		if (!memcmp(e7, model->signature, sizeof(model->signature)))
2859			return &model->protocol_info;
2860	}
2861
2862	return NULL;
2863}
2864
2865static bool alps_is_cs19_trackpoint(struct psmouse *psmouse)
2866{
2867	u8 param[2] = { 0 };
2868
2869	if (ps2_command(&psmouse->ps2dev,
2870			param, MAKE_PS2_CMD(0, 2, TP_READ_ID)))
2871		return false;
2872
2873	/*
2874	 * param[0] contains the trackpoint device variant_id while
2875	 * param[1] contains the firmware_id. So far all alps
2876	 * trackpoint-only devices have their variant_ids equal
2877	 * TP_VARIANT_ALPS and their firmware_ids are in 0x20~0x2f range.
2878	 */
2879	return param[0] == TP_VARIANT_ALPS && ((param[1] & 0xf0) == 0x20);
2880}
2881
2882static int alps_identify(struct psmouse *psmouse, struct alps_data *priv)
2883{
2884	const struct alps_protocol_info *protocol;
2885	unsigned char e6[4], e7[4], ec[4];
2886	int error;
2887
2888	/*
2889	 * First try "E6 report".
2890	 * ALPS should return 0,0,10 or 0,0,100 if no buttons are pressed.
2891	 * The bits 0-2 of the first byte will be 1s if some buttons are
2892	 * pressed.
2893	 */
2894	if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2895			 PSMOUSE_CMD_SETSCALE11, e6))
2896		return -EIO;
2897
2898	if ((e6[0] & 0xf8) != 0 || e6[1] != 0 || (e6[2] != 10 && e6[2] != 100))
2899		return -EINVAL;
2900
2901	/*
2902	 * Now get the "E7" and "EC" reports.  These will uniquely identify
2903	 * most ALPS touchpads.
2904	 */
2905	if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2906			 PSMOUSE_CMD_SETSCALE21, e7) ||
2907	    alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2908			 PSMOUSE_CMD_RESET_WRAP, ec) ||
2909	    alps_exit_command_mode(psmouse))
2910		return -EIO;
2911
2912	protocol = alps_match_table(e7, ec);
2913	if (!protocol) {
2914		if (e7[0] == 0x73 && e7[1] == 0x02 && e7[2] == 0x64 &&
2915			   ec[2] == 0x8a) {
2916			protocol = &alps_v4_protocol_data;
2917		} else if (e7[0] == 0x73 && e7[1] == 0x03 && e7[2] == 0x50 &&
2918			   ec[0] == 0x73 && (ec[1] == 0x01 || ec[1] == 0x02)) {
2919			protocol = &alps_v5_protocol_data;
2920		} else if (ec[0] == 0x88 &&
2921			   ((ec[1] & 0xf0) == 0xb0 || (ec[1] & 0xf0) == 0xc0)) {
2922			protocol = &alps_v7_protocol_data;
2923		} else if (ec[0] == 0x88 && ec[1] == 0x08) {
2924			protocol = &alps_v3_rushmore_data;
2925		} else if (ec[0] == 0x88 && ec[1] == 0x07 &&
2926			   ec[2] >= 0x90 && ec[2] <= 0x9d) {
2927			protocol = &alps_v3_protocol_data;
2928		} else if (e7[0] == 0x73 && e7[1] == 0x03 &&
2929			   (e7[2] == 0x14 || e7[2] == 0x28)) {
2930			protocol = &alps_v8_protocol_data;
2931		} else if (e7[0] == 0x73 && e7[1] == 0x03 && e7[2] == 0xc8) {
2932			protocol = &alps_v9_protocol_data;
2933			psmouse_warn(psmouse,
2934				     "Unsupported ALPS V9 touchpad: E7=%3ph, EC=%3ph\n",
2935				     e7, ec);
2936			return -EINVAL;
2937		} else {
2938			psmouse_dbg(psmouse,
2939				    "Likely not an ALPS touchpad: E7=%3ph, EC=%3ph\n", e7, ec);
2940			return -EINVAL;
2941		}
2942	}
2943
2944	if (priv) {
2945		/* Save Device ID and Firmware version */
2946		memcpy(priv->dev_id, e7, 3);
2947		memcpy(priv->fw_ver, ec, 3);
2948		error = alps_set_protocol(psmouse, priv, protocol);
2949		if (error)
2950			return error;
2951	}
2952
2953	return 0;
2954}
2955
2956static int alps_reconnect(struct psmouse *psmouse)
2957{
2958	struct alps_data *priv = psmouse->private;
2959
2960	psmouse_reset(psmouse);
2961
2962	if (alps_identify(psmouse, priv) < 0)
2963		return -1;
2964
2965	return priv->hw_init(psmouse);
2966}
2967
2968static void alps_disconnect(struct psmouse *psmouse)
2969{
2970	struct alps_data *priv = psmouse->private;
2971
2972	psmouse_reset(psmouse);
2973	timer_shutdown_sync(&priv->timer);
2974	if (priv->dev2)
2975		input_unregister_device(priv->dev2);
2976	if (!IS_ERR_OR_NULL(priv->dev3))
2977		input_unregister_device(priv->dev3);
2978	kfree(priv);
2979}
2980
2981static void alps_set_abs_params_st(struct alps_data *priv,
2982				   struct input_dev *dev1)
2983{
2984	input_set_abs_params(dev1, ABS_X, 0, priv->x_max, 0, 0);
2985	input_set_abs_params(dev1, ABS_Y, 0, priv->y_max, 0, 0);
2986	input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
2987}
2988
2989static void alps_set_abs_params_mt_common(struct alps_data *priv,
2990					  struct input_dev *dev1)
2991{
2992	input_set_abs_params(dev1, ABS_MT_POSITION_X, 0, priv->x_max, 0, 0);
2993	input_set_abs_params(dev1, ABS_MT_POSITION_Y, 0, priv->y_max, 0, 0);
2994
2995	input_abs_set_res(dev1, ABS_MT_POSITION_X, priv->x_res);
2996	input_abs_set_res(dev1, ABS_MT_POSITION_Y, priv->y_res);
2997
2998	set_bit(BTN_TOOL_TRIPLETAP, dev1->keybit);
2999	set_bit(BTN_TOOL_QUADTAP, dev1->keybit);
3000}
3001
3002static void alps_set_abs_params_semi_mt(struct alps_data *priv,
3003					struct input_dev *dev1)
3004{
3005	alps_set_abs_params_mt_common(priv, dev1);
3006	input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
3007
3008	input_mt_init_slots(dev1, MAX_TOUCHES,
3009			    INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
3010				INPUT_MT_SEMI_MT);
3011}
3012
3013static void alps_set_abs_params_v7(struct alps_data *priv,
3014				   struct input_dev *dev1)
3015{
3016	alps_set_abs_params_mt_common(priv, dev1);
3017	set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
3018
3019	input_mt_init_slots(dev1, MAX_TOUCHES,
3020			    INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
3021				INPUT_MT_TRACK);
3022
3023	set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
3024}
3025
3026static void alps_set_abs_params_ss4_v2(struct alps_data *priv,
3027				       struct input_dev *dev1)
3028{
3029	alps_set_abs_params_mt_common(priv, dev1);
3030	input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
3031	set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
3032
3033	input_mt_init_slots(dev1, MAX_TOUCHES,
3034			    INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
3035				INPUT_MT_TRACK);
3036}
3037
3038int alps_init(struct psmouse *psmouse)
3039{
3040	struct alps_data *priv = psmouse->private;
3041	struct input_dev *dev1 = psmouse->dev;
3042	int error;
3043
3044	error = priv->hw_init(psmouse);
3045	if (error)
3046		goto init_fail;
3047
3048	/*
3049	 * Undo part of setup done for us by psmouse core since touchpad
3050	 * is not a relative device.
3051	 */
3052	__clear_bit(EV_REL, dev1->evbit);
3053	__clear_bit(REL_X, dev1->relbit);
3054	__clear_bit(REL_Y, dev1->relbit);
3055
3056	/*
3057	 * Now set up our capabilities.
3058	 */
3059	dev1->evbit[BIT_WORD(EV_KEY)] |= BIT_MASK(EV_KEY);
3060	dev1->keybit[BIT_WORD(BTN_TOUCH)] |= BIT_MASK(BTN_TOUCH);
3061	dev1->keybit[BIT_WORD(BTN_TOOL_FINGER)] |= BIT_MASK(BTN_TOOL_FINGER);
3062	dev1->keybit[BIT_WORD(BTN_LEFT)] |=
3063		BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
3064
3065	dev1->evbit[BIT_WORD(EV_ABS)] |= BIT_MASK(EV_ABS);
3066
3067	priv->set_abs_params(priv, dev1);
3068
3069	if (priv->flags & ALPS_WHEEL) {
3070		dev1->evbit[BIT_WORD(EV_REL)] |= BIT_MASK(EV_REL);
3071		dev1->relbit[BIT_WORD(REL_WHEEL)] |= BIT_MASK(REL_WHEEL);
3072	}
3073
3074	if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
3075		dev1->keybit[BIT_WORD(BTN_FORWARD)] |= BIT_MASK(BTN_FORWARD);
3076		dev1->keybit[BIT_WORD(BTN_BACK)] |= BIT_MASK(BTN_BACK);
3077	}
3078
3079	if (priv->flags & ALPS_FOUR_BUTTONS) {
3080		dev1->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_0);
3081		dev1->keybit[BIT_WORD(BTN_1)] |= BIT_MASK(BTN_1);
3082		dev1->keybit[BIT_WORD(BTN_2)] |= BIT_MASK(BTN_2);
3083		dev1->keybit[BIT_WORD(BTN_3)] |= BIT_MASK(BTN_3);
3084	} else if (priv->flags & ALPS_BUTTONPAD) {
3085		set_bit(INPUT_PROP_BUTTONPAD, dev1->propbit);
3086		clear_bit(BTN_RIGHT, dev1->keybit);
3087	} else {
3088		dev1->keybit[BIT_WORD(BTN_MIDDLE)] |= BIT_MASK(BTN_MIDDLE);
3089	}
3090
3091	if (priv->flags & ALPS_DUALPOINT) {
3092		struct input_dev *dev2;
3093
3094		dev2 = input_allocate_device();
3095		if (!dev2) {
3096			psmouse_err(psmouse,
3097				    "failed to allocate trackstick device\n");
3098			error = -ENOMEM;
3099			goto init_fail;
3100		}
3101
3102		snprintf(priv->phys2, sizeof(priv->phys2), "%s/input1",
3103			 psmouse->ps2dev.serio->phys);
3104		dev2->phys = priv->phys2;
3105
3106		/*
3107		 * format of input device name is: "protocol vendor name"
3108		 * see function psmouse_switch_protocol() in psmouse-base.c
3109		 */
3110		dev2->name = "AlpsPS/2 ALPS DualPoint Stick";
3111
3112		dev2->id.bustype = BUS_I8042;
3113		dev2->id.vendor  = 0x0002;
3114		dev2->id.product = PSMOUSE_ALPS;
3115		dev2->id.version = priv->proto_version;
3116		dev2->dev.parent = &psmouse->ps2dev.serio->dev;
3117
3118		input_set_capability(dev2, EV_REL, REL_X);
3119		input_set_capability(dev2, EV_REL, REL_Y);
3120		if (priv->flags & ALPS_DUALPOINT_WITH_PRESSURE) {
3121			input_set_capability(dev2, EV_ABS, ABS_PRESSURE);
3122			input_set_abs_params(dev2, ABS_PRESSURE, 0, 127, 0, 0);
3123		}
3124		input_set_capability(dev2, EV_KEY, BTN_LEFT);
3125		input_set_capability(dev2, EV_KEY, BTN_RIGHT);
3126		input_set_capability(dev2, EV_KEY, BTN_MIDDLE);
3127
3128		__set_bit(INPUT_PROP_POINTER, dev2->propbit);
3129		__set_bit(INPUT_PROP_POINTING_STICK, dev2->propbit);
3130
3131		error = input_register_device(dev2);
3132		if (error) {
3133			psmouse_err(psmouse,
3134				    "failed to register trackstick device: %d\n",
3135				    error);
3136			input_free_device(dev2);
3137			goto init_fail;
3138		}
3139
3140		priv->dev2 = dev2;
3141	}
3142
3143	priv->psmouse = psmouse;
3144
3145	INIT_DELAYED_WORK(&priv->dev3_register_work,
3146			  alps_register_bare_ps2_mouse);
3147
3148	psmouse->protocol_handler = alps_process_byte;
3149	psmouse->poll = alps_poll;
3150	psmouse->disconnect = alps_disconnect;
3151	psmouse->reconnect = alps_reconnect;
3152	psmouse->pktsize = priv->proto_version == ALPS_PROTO_V4 ? 8 : 6;
3153
3154	/* We are having trouble resyncing ALPS touchpads so disable it for now */
3155	psmouse->resync_time = 0;
3156
3157	/* Allow 2 invalid packets without resetting device */
3158	psmouse->resetafter = psmouse->pktsize * 2;
3159
3160	return 0;
3161
3162init_fail:
3163	psmouse_reset(psmouse);
3164	/*
3165	 * Even though we did not allocate psmouse->private we do free
3166	 * it here.
3167	 */
3168	kfree(psmouse->private);
3169	psmouse->private = NULL;
3170	return error;
3171}
3172
3173int alps_detect(struct psmouse *psmouse, bool set_properties)
3174{
3175	struct alps_data *priv;
3176	int error;
3177
3178	error = alps_identify(psmouse, NULL);
3179	if (error)
3180		return error;
3181
3182	/*
3183	 * ALPS cs19 is a trackpoint-only device, and uses different
3184	 * protocol than DualPoint ones, so we return -EINVAL here and let
3185	 * trackpoint.c drive this device. If the trackpoint driver is not
3186	 * enabled, the device will fall back to a bare PS/2 mouse.
3187	 * If ps2_command() fails here, we depend on the immediately
3188	 * followed psmouse_reset() to reset the device to normal state.
3189	 */
3190	if (alps_is_cs19_trackpoint(psmouse)) {
3191		psmouse_dbg(psmouse,
3192			    "ALPS CS19 trackpoint-only device detected, ignoring\n");
3193		return -EINVAL;
3194	}
3195
3196	/*
3197	 * Reset the device to make sure it is fully operational:
3198	 * on some laptops, like certain Dell Latitudes, we may
3199	 * fail to properly detect presence of trackstick if device
3200	 * has not been reset.
3201	 */
3202	psmouse_reset(psmouse);
3203
3204	priv = kzalloc(sizeof(struct alps_data), GFP_KERNEL);
3205	if (!priv)
3206		return -ENOMEM;
3207
3208	error = alps_identify(psmouse, priv);
3209	if (error) {
3210		kfree(priv);
3211		return error;
3212	}
3213
3214	if (set_properties) {
3215		psmouse->vendor = "ALPS";
3216		psmouse->name = priv->flags & ALPS_DUALPOINT ?
3217				"DualPoint TouchPad" : "GlidePoint";
3218		psmouse->model = priv->proto_version;
3219	} else {
3220		/*
3221		 * Destroy alps_data structure we allocated earlier since
3222		 * this was just a "trial run". Otherwise we'll keep it
3223		 * to be used by alps_init() which has to be called if
3224		 * we succeed and set_properties is true.
3225		 */
3226		kfree(priv);
3227		psmouse->private = NULL;
3228	}
3229
3230	return 0;
3231}
3232
v4.17
 
   1/*
   2 * ALPS touchpad PS/2 mouse driver
   3 *
   4 * Copyright (c) 2003 Neil Brown <neilb@cse.unsw.edu.au>
   5 * Copyright (c) 2003-2005 Peter Osterlund <petero2@telia.com>
   6 * Copyright (c) 2004 Dmitry Torokhov <dtor@mail.ru>
   7 * Copyright (c) 2005 Vojtech Pavlik <vojtech@suse.cz>
   8 * Copyright (c) 2009 Sebastian Kapfer <sebastian_kapfer@gmx.net>
   9 *
  10 * ALPS detection, tap switching and status querying info is taken from
  11 * tpconfig utility (by C. Scott Ananian and Bruce Kall).
  12 *
  13 * This program is free software; you can redistribute it and/or modify it
  14 * under the terms of the GNU General Public License version 2 as published by
  15 * the Free Software Foundation.
  16 */
  17
  18#include <linux/slab.h>
  19#include <linux/input.h>
  20#include <linux/input/mt.h>
  21#include <linux/serio.h>
  22#include <linux/libps2.h>
  23#include <linux/dmi.h>
  24
  25#include "psmouse.h"
  26#include "alps.h"
 
  27
  28/*
  29 * Definitions for ALPS version 3 and 4 command mode protocol
  30 */
  31#define ALPS_CMD_NIBBLE_10	0x01f2
  32
  33#define ALPS_REG_BASE_RUSHMORE	0xc2c0
  34#define ALPS_REG_BASE_V7	0xc2c0
  35#define ALPS_REG_BASE_PINNACLE	0x0000
  36
  37static const struct alps_nibble_commands alps_v3_nibble_commands[] = {
  38	{ PSMOUSE_CMD_SETPOLL,		0x00 }, /* 0 */
  39	{ PSMOUSE_CMD_RESET_DIS,	0x00 }, /* 1 */
  40	{ PSMOUSE_CMD_SETSCALE21,	0x00 }, /* 2 */
  41	{ PSMOUSE_CMD_SETRATE,		0x0a }, /* 3 */
  42	{ PSMOUSE_CMD_SETRATE,		0x14 }, /* 4 */
  43	{ PSMOUSE_CMD_SETRATE,		0x28 }, /* 5 */
  44	{ PSMOUSE_CMD_SETRATE,		0x3c }, /* 6 */
  45	{ PSMOUSE_CMD_SETRATE,		0x50 }, /* 7 */
  46	{ PSMOUSE_CMD_SETRATE,		0x64 }, /* 8 */
  47	{ PSMOUSE_CMD_SETRATE,		0xc8 }, /* 9 */
  48	{ ALPS_CMD_NIBBLE_10,		0x00 }, /* a */
  49	{ PSMOUSE_CMD_SETRES,		0x00 }, /* b */
  50	{ PSMOUSE_CMD_SETRES,		0x01 }, /* c */
  51	{ PSMOUSE_CMD_SETRES,		0x02 }, /* d */
  52	{ PSMOUSE_CMD_SETRES,		0x03 }, /* e */
  53	{ PSMOUSE_CMD_SETSCALE11,	0x00 }, /* f */
  54};
  55
  56static const struct alps_nibble_commands alps_v4_nibble_commands[] = {
  57	{ PSMOUSE_CMD_ENABLE,		0x00 }, /* 0 */
  58	{ PSMOUSE_CMD_RESET_DIS,	0x00 }, /* 1 */
  59	{ PSMOUSE_CMD_SETSCALE21,	0x00 }, /* 2 */
  60	{ PSMOUSE_CMD_SETRATE,		0x0a }, /* 3 */
  61	{ PSMOUSE_CMD_SETRATE,		0x14 }, /* 4 */
  62	{ PSMOUSE_CMD_SETRATE,		0x28 }, /* 5 */
  63	{ PSMOUSE_CMD_SETRATE,		0x3c }, /* 6 */
  64	{ PSMOUSE_CMD_SETRATE,		0x50 }, /* 7 */
  65	{ PSMOUSE_CMD_SETRATE,		0x64 }, /* 8 */
  66	{ PSMOUSE_CMD_SETRATE,		0xc8 }, /* 9 */
  67	{ ALPS_CMD_NIBBLE_10,		0x00 }, /* a */
  68	{ PSMOUSE_CMD_SETRES,		0x00 }, /* b */
  69	{ PSMOUSE_CMD_SETRES,		0x01 }, /* c */
  70	{ PSMOUSE_CMD_SETRES,		0x02 }, /* d */
  71	{ PSMOUSE_CMD_SETRES,		0x03 }, /* e */
  72	{ PSMOUSE_CMD_SETSCALE11,	0x00 }, /* f */
  73};
  74
  75static const struct alps_nibble_commands alps_v6_nibble_commands[] = {
  76	{ PSMOUSE_CMD_ENABLE,		0x00 }, /* 0 */
  77	{ PSMOUSE_CMD_SETRATE,		0x0a }, /* 1 */
  78	{ PSMOUSE_CMD_SETRATE,		0x14 }, /* 2 */
  79	{ PSMOUSE_CMD_SETRATE,		0x28 }, /* 3 */
  80	{ PSMOUSE_CMD_SETRATE,		0x3c }, /* 4 */
  81	{ PSMOUSE_CMD_SETRATE,		0x50 }, /* 5 */
  82	{ PSMOUSE_CMD_SETRATE,		0x64 }, /* 6 */
  83	{ PSMOUSE_CMD_SETRATE,		0xc8 }, /* 7 */
  84	{ PSMOUSE_CMD_GETID,		0x00 }, /* 8 */
  85	{ PSMOUSE_CMD_GETINFO,		0x00 }, /* 9 */
  86	{ PSMOUSE_CMD_SETRES,		0x00 }, /* a */
  87	{ PSMOUSE_CMD_SETRES,		0x01 }, /* b */
  88	{ PSMOUSE_CMD_SETRES,		0x02 }, /* c */
  89	{ PSMOUSE_CMD_SETRES,		0x03 }, /* d */
  90	{ PSMOUSE_CMD_SETSCALE21,	0x00 }, /* e */
  91	{ PSMOUSE_CMD_SETSCALE11,	0x00 }, /* f */
  92};
  93
  94
  95#define ALPS_DUALPOINT		0x02	/* touchpad has trackstick */
  96#define ALPS_PASS		0x04	/* device has a pass-through port */
  97
  98#define ALPS_WHEEL		0x08	/* hardware wheel present */
  99#define ALPS_FW_BK_1		0x10	/* front & back buttons present */
 100#define ALPS_FW_BK_2		0x20	/* front & back buttons present */
 101#define ALPS_FOUR_BUTTONS	0x40	/* 4 direction button present */
 102#define ALPS_PS2_INTERLEAVED	0x80	/* 3-byte PS/2 packet interleaved with
 103					   6-byte ALPS packet */
 104#define ALPS_STICK_BITS		0x100	/* separate stick button bits */
 105#define ALPS_BUTTONPAD		0x200	/* device is a clickpad */
 106#define ALPS_DUALPOINT_WITH_PRESSURE	0x400	/* device can report trackpoint pressure */
 107
 108static const struct alps_model_info alps_model_data[] = {
 109	/*
 110	 * XXX This entry is suspicious. First byte has zero lower nibble,
 111	 * which is what a normal mouse would report. Also, the value 0x0e
 112	 * isn't valid per PS/2 spec.
 113	 */
 114	{ { 0x20, 0x02, 0x0e }, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } },
 115
 116	{ { 0x22, 0x02, 0x0a }, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } },
 117	{ { 0x22, 0x02, 0x14 }, { ALPS_PROTO_V2, 0xff, 0xff, ALPS_PASS | ALPS_DUALPOINT } },	/* Dell Latitude D600 */
 118	{ { 0x32, 0x02, 0x14 }, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } },	/* Toshiba Salellite Pro M10 */
 119	{ { 0x33, 0x02, 0x0a }, { ALPS_PROTO_V1, 0x88, 0xf8, 0 } },				/* UMAX-530T */
 120	{ { 0x52, 0x01, 0x14 }, { ALPS_PROTO_V2, 0xff, 0xff,
 121		ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED } },				/* Toshiba Tecra A11-11L */
 122	{ { 0x53, 0x02, 0x0a }, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
 123	{ { 0x53, 0x02, 0x14 }, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
 124	{ { 0x60, 0x03, 0xc8 }, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },				/* HP ze1115 */
 125	{ { 0x62, 0x02, 0x14 }, { ALPS_PROTO_V2, 0xcf, 0xcf,
 126		ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED } },				/* Dell Latitude E5500, E6400, E6500, Precision M4400 */
 127	{ { 0x63, 0x02, 0x0a }, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
 128	{ { 0x63, 0x02, 0x14 }, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
 129	{ { 0x63, 0x02, 0x28 }, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 } },			/* Fujitsu Siemens S6010 */
 130	{ { 0x63, 0x02, 0x3c }, { ALPS_PROTO_V2, 0x8f, 0x8f, ALPS_WHEEL } },			/* Toshiba Satellite S2400-103 */
 131	{ { 0x63, 0x02, 0x50 }, { ALPS_PROTO_V2, 0xef, 0xef, ALPS_FW_BK_1 } },			/* NEC Versa L320 */
 132	{ { 0x63, 0x02, 0x64 }, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
 133	{ { 0x63, 0x03, 0xc8 }, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } },	/* Dell Latitude D800 */
 134	{ { 0x73, 0x00, 0x0a }, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_DUALPOINT } },		/* ThinkPad R61 8918-5QG */
 135	{ { 0x73, 0x00, 0x14 }, { ALPS_PROTO_V6, 0xff, 0xff, ALPS_DUALPOINT } },		/* Dell XT2 */
 136	{ { 0x73, 0x02, 0x0a }, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
 137	{ { 0x73, 0x02, 0x14 }, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 } },			/* Ahtec Laptop */
 138	{ { 0x73, 0x02, 0x50 }, { ALPS_PROTO_V2, 0xcf, 0xcf, ALPS_FOUR_BUTTONS } },		/* Dell Vostro 1400 */
 139};
 140
 141static const struct alps_protocol_info alps_v3_protocol_data = {
 142	ALPS_PROTO_V3, 0x8f, 0x8f, ALPS_DUALPOINT | ALPS_DUALPOINT_WITH_PRESSURE
 143};
 144
 145static const struct alps_protocol_info alps_v3_rushmore_data = {
 146	ALPS_PROTO_V3_RUSHMORE, 0x8f, 0x8f, ALPS_DUALPOINT | ALPS_DUALPOINT_WITH_PRESSURE
 147};
 148
 149static const struct alps_protocol_info alps_v4_protocol_data = {
 150	ALPS_PROTO_V4, 0x8f, 0x8f, 0
 151};
 152
 153static const struct alps_protocol_info alps_v5_protocol_data = {
 154	ALPS_PROTO_V5, 0xc8, 0xd8, 0
 155};
 156
 157static const struct alps_protocol_info alps_v7_protocol_data = {
 158	ALPS_PROTO_V7, 0x48, 0x48, ALPS_DUALPOINT | ALPS_DUALPOINT_WITH_PRESSURE
 159};
 160
 161static const struct alps_protocol_info alps_v8_protocol_data = {
 162	ALPS_PROTO_V8, 0x18, 0x18, 0
 163};
 164
 165static const struct alps_protocol_info alps_v9_protocol_data = {
 166	ALPS_PROTO_V9, 0xc8, 0xc8, 0
 167};
 168
 169/*
 170 * Some v2 models report the stick buttons in separate bits
 171 */
 172static const struct dmi_system_id alps_dmi_has_separate_stick_buttons[] = {
 173#if defined(CONFIG_DMI) && defined(CONFIG_X86)
 174	{
 175		/* Extrapolated from other entries */
 176		.matches = {
 177			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 178			DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D420"),
 179		},
 180	},
 181	{
 182		/* Reported-by: Hans de Bruin <jmdebruin@xmsnet.nl> */
 183		.matches = {
 184			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 185			DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D430"),
 186		},
 187	},
 188	{
 189		/* Reported-by: Hans de Goede <hdegoede@redhat.com> */
 190		.matches = {
 191			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 192			DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D620"),
 193		},
 194	},
 195	{
 196		/* Extrapolated from other entries */
 197		.matches = {
 198			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 199			DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D630"),
 200		},
 201	},
 202#endif
 203	{ }
 204};
 205
 206static void alps_set_abs_params_st(struct alps_data *priv,
 207				   struct input_dev *dev1);
 208static void alps_set_abs_params_semi_mt(struct alps_data *priv,
 209					struct input_dev *dev1);
 210static void alps_set_abs_params_v7(struct alps_data *priv,
 211				   struct input_dev *dev1);
 212static void alps_set_abs_params_ss4_v2(struct alps_data *priv,
 213				       struct input_dev *dev1);
 214
 215/* Packet formats are described in Documentation/input/alps.txt */
 216
 217static bool alps_is_valid_first_byte(struct alps_data *priv,
 218				     unsigned char data)
 219{
 220	return (data & priv->mask0) == priv->byte0;
 221}
 222
 223static void alps_report_buttons(struct input_dev *dev1, struct input_dev *dev2,
 224				int left, int right, int middle)
 225{
 226	struct input_dev *dev;
 227
 228	/*
 229	 * If shared button has already been reported on the
 230	 * other device (dev2) then this event should be also
 231	 * sent through that device.
 232	 */
 233	dev = (dev2 && test_bit(BTN_LEFT, dev2->key)) ? dev2 : dev1;
 234	input_report_key(dev, BTN_LEFT, left);
 235
 236	dev = (dev2 && test_bit(BTN_RIGHT, dev2->key)) ? dev2 : dev1;
 237	input_report_key(dev, BTN_RIGHT, right);
 238
 239	dev = (dev2 && test_bit(BTN_MIDDLE, dev2->key)) ? dev2 : dev1;
 240	input_report_key(dev, BTN_MIDDLE, middle);
 241
 242	/*
 243	 * Sync the _other_ device now, we'll do the first
 244	 * device later once we report the rest of the events.
 245	 */
 246	if (dev2)
 247		input_sync(dev2);
 248}
 249
 250static void alps_process_packet_v1_v2(struct psmouse *psmouse)
 251{
 252	struct alps_data *priv = psmouse->private;
 253	unsigned char *packet = psmouse->packet;
 254	struct input_dev *dev = psmouse->dev;
 255	struct input_dev *dev2 = priv->dev2;
 256	int x, y, z, ges, fin, left, right, middle;
 257	int back = 0, forward = 0;
 258
 259	if (priv->proto_version == ALPS_PROTO_V1) {
 260		left = packet[2] & 0x10;
 261		right = packet[2] & 0x08;
 262		middle = 0;
 263		x = packet[1] | ((packet[0] & 0x07) << 7);
 264		y = packet[4] | ((packet[3] & 0x07) << 7);
 265		z = packet[5];
 266	} else {
 267		left = packet[3] & 1;
 268		right = packet[3] & 2;
 269		middle = packet[3] & 4;
 270		x = packet[1] | ((packet[2] & 0x78) << (7 - 3));
 271		y = packet[4] | ((packet[3] & 0x70) << (7 - 4));
 272		z = packet[5];
 273	}
 274
 275	if (priv->flags & ALPS_FW_BK_1) {
 276		back = packet[0] & 0x10;
 277		forward = packet[2] & 4;
 278	}
 279
 280	if (priv->flags & ALPS_FW_BK_2) {
 281		back = packet[3] & 4;
 282		forward = packet[2] & 4;
 283		if ((middle = forward && back))
 284			forward = back = 0;
 285	}
 286
 287	ges = packet[2] & 1;
 288	fin = packet[2] & 2;
 289
 290	if ((priv->flags & ALPS_DUALPOINT) && z == 127) {
 291		input_report_rel(dev2, REL_X,  (x > 383 ? (x - 768) : x));
 292		input_report_rel(dev2, REL_Y, -(y > 255 ? (y - 512) : y));
 293
 294		alps_report_buttons(dev2, dev, left, right, middle);
 295
 296		input_sync(dev2);
 297		return;
 298	}
 299
 300	/* Some models have separate stick button bits */
 301	if (priv->flags & ALPS_STICK_BITS) {
 302		left |= packet[0] & 1;
 303		right |= packet[0] & 2;
 304		middle |= packet[0] & 4;
 305	}
 306
 307	alps_report_buttons(dev, dev2, left, right, middle);
 308
 309	/* Convert hardware tap to a reasonable Z value */
 310	if (ges && !fin)
 311		z = 40;
 312
 313	/*
 314	 * A "tap and drag" operation is reported by the hardware as a transition
 315	 * from (!fin && ges) to (fin && ges). This should be translated to the
 316	 * sequence Z>0, Z==0, Z>0, so the Z==0 event has to be generated manually.
 317	 */
 318	if (ges && fin && !priv->prev_fin) {
 319		input_report_abs(dev, ABS_X, x);
 320		input_report_abs(dev, ABS_Y, y);
 321		input_report_abs(dev, ABS_PRESSURE, 0);
 322		input_report_key(dev, BTN_TOOL_FINGER, 0);
 323		input_sync(dev);
 324	}
 325	priv->prev_fin = fin;
 326
 327	if (z > 30)
 328		input_report_key(dev, BTN_TOUCH, 1);
 329	if (z < 25)
 330		input_report_key(dev, BTN_TOUCH, 0);
 331
 332	if (z > 0) {
 333		input_report_abs(dev, ABS_X, x);
 334		input_report_abs(dev, ABS_Y, y);
 335	}
 336
 337	input_report_abs(dev, ABS_PRESSURE, z);
 338	input_report_key(dev, BTN_TOOL_FINGER, z > 0);
 339
 340	if (priv->flags & ALPS_WHEEL)
 341		input_report_rel(dev, REL_WHEEL, ((packet[2] << 1) & 0x08) - ((packet[0] >> 4) & 0x07));
 342
 343	if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
 344		input_report_key(dev, BTN_FORWARD, forward);
 345		input_report_key(dev, BTN_BACK, back);
 346	}
 347
 348	if (priv->flags & ALPS_FOUR_BUTTONS) {
 349		input_report_key(dev, BTN_0, packet[2] & 4);
 350		input_report_key(dev, BTN_1, packet[0] & 0x10);
 351		input_report_key(dev, BTN_2, packet[3] & 4);
 352		input_report_key(dev, BTN_3, packet[0] & 0x20);
 353	}
 354
 355	input_sync(dev);
 356}
 357
 358static void alps_get_bitmap_points(unsigned int map,
 359				   struct alps_bitmap_point *low,
 360				   struct alps_bitmap_point *high,
 361				   int *fingers)
 362{
 363	struct alps_bitmap_point *point;
 364	int i, bit, prev_bit = 0;
 365
 366	point = low;
 367	for (i = 0; map != 0; i++, map >>= 1) {
 368		bit = map & 1;
 369		if (bit) {
 370			if (!prev_bit) {
 371				point->start_bit = i;
 372				point->num_bits = 0;
 373				(*fingers)++;
 374			}
 375			point->num_bits++;
 376		} else {
 377			if (prev_bit)
 378				point = high;
 379		}
 380		prev_bit = bit;
 381	}
 382}
 383
 384/*
 385 * Process bitmap data from semi-mt protocols. Returns the number of
 386 * fingers detected. A return value of 0 means at least one of the
 387 * bitmaps was empty.
 388 *
 389 * The bitmaps don't have enough data to track fingers, so this function
 390 * only generates points representing a bounding box of all contacts.
 391 * These points are returned in fields->mt when the return value
 392 * is greater than 0.
 393 */
 394static int alps_process_bitmap(struct alps_data *priv,
 395			       struct alps_fields *fields)
 396{
 397	int i, fingers_x = 0, fingers_y = 0, fingers, closest;
 398	struct alps_bitmap_point x_low = {0,}, x_high = {0,};
 399	struct alps_bitmap_point y_low = {0,}, y_high = {0,};
 400	struct input_mt_pos corner[4];
 401
 402	if (!fields->x_map || !fields->y_map)
 403		return 0;
 404
 405	alps_get_bitmap_points(fields->x_map, &x_low, &x_high, &fingers_x);
 406	alps_get_bitmap_points(fields->y_map, &y_low, &y_high, &fingers_y);
 407
 408	/*
 409	 * Fingers can overlap, so we use the maximum count of fingers
 410	 * on either axis as the finger count.
 411	 */
 412	fingers = max(fingers_x, fingers_y);
 413
 414	/*
 415	 * If an axis reports only a single contact, we have overlapping or
 416	 * adjacent fingers. Divide the single contact between the two points.
 417	 */
 418	if (fingers_x == 1) {
 419		i = (x_low.num_bits - 1) / 2;
 420		x_low.num_bits = x_low.num_bits - i;
 421		x_high.start_bit = x_low.start_bit + i;
 422		x_high.num_bits = max(i, 1);
 423	}
 424	if (fingers_y == 1) {
 425		i = (y_low.num_bits - 1) / 2;
 426		y_low.num_bits = y_low.num_bits - i;
 427		y_high.start_bit = y_low.start_bit + i;
 428		y_high.num_bits = max(i, 1);
 429	}
 430
 431	/* top-left corner */
 432	corner[0].x =
 433		(priv->x_max * (2 * x_low.start_bit + x_low.num_bits - 1)) /
 434		(2 * (priv->x_bits - 1));
 435	corner[0].y =
 436		(priv->y_max * (2 * y_low.start_bit + y_low.num_bits - 1)) /
 437		(2 * (priv->y_bits - 1));
 438
 439	/* top-right corner */
 440	corner[1].x =
 441		(priv->x_max * (2 * x_high.start_bit + x_high.num_bits - 1)) /
 442		(2 * (priv->x_bits - 1));
 443	corner[1].y =
 444		(priv->y_max * (2 * y_low.start_bit + y_low.num_bits - 1)) /
 445		(2 * (priv->y_bits - 1));
 446
 447	/* bottom-right corner */
 448	corner[2].x =
 449		(priv->x_max * (2 * x_high.start_bit + x_high.num_bits - 1)) /
 450		(2 * (priv->x_bits - 1));
 451	corner[2].y =
 452		(priv->y_max * (2 * y_high.start_bit + y_high.num_bits - 1)) /
 453		(2 * (priv->y_bits - 1));
 454
 455	/* bottom-left corner */
 456	corner[3].x =
 457		(priv->x_max * (2 * x_low.start_bit + x_low.num_bits - 1)) /
 458		(2 * (priv->x_bits - 1));
 459	corner[3].y =
 460		(priv->y_max * (2 * y_high.start_bit + y_high.num_bits - 1)) /
 461		(2 * (priv->y_bits - 1));
 462
 463	/* x-bitmap order is reversed on v5 touchpads  */
 464	if (priv->proto_version == ALPS_PROTO_V5) {
 465		for (i = 0; i < 4; i++)
 466			corner[i].x = priv->x_max - corner[i].x;
 467	}
 468
 469	/* y-bitmap order is reversed on v3 and v4 touchpads  */
 470	if (priv->proto_version == ALPS_PROTO_V3 ||
 471	    priv->proto_version == ALPS_PROTO_V4) {
 472		for (i = 0; i < 4; i++)
 473			corner[i].y = priv->y_max - corner[i].y;
 474	}
 475
 476	/*
 477	 * We only select a corner for the second touch once per 2 finger
 478	 * touch sequence to avoid the chosen corner (and thus the coordinates)
 479	 * jumping around when the first touch is in the middle.
 480	 */
 481	if (priv->second_touch == -1) {
 482		/* Find corner closest to our st coordinates */
 483		closest = 0x7fffffff;
 484		for (i = 0; i < 4; i++) {
 485			int dx = fields->st.x - corner[i].x;
 486			int dy = fields->st.y - corner[i].y;
 487			int distance = dx * dx + dy * dy;
 488
 489			if (distance < closest) {
 490				priv->second_touch = i;
 491				closest = distance;
 492			}
 493		}
 494		/* And select the opposite corner to use for the 2nd touch */
 495		priv->second_touch = (priv->second_touch + 2) % 4;
 496	}
 497
 498	fields->mt[0] = fields->st;
 499	fields->mt[1] = corner[priv->second_touch];
 500
 501	return fingers;
 502}
 503
 504static void alps_set_slot(struct input_dev *dev, int slot, int x, int y)
 505{
 506	input_mt_slot(dev, slot);
 507	input_mt_report_slot_state(dev, MT_TOOL_FINGER, true);
 508	input_report_abs(dev, ABS_MT_POSITION_X, x);
 509	input_report_abs(dev, ABS_MT_POSITION_Y, y);
 510}
 511
 512static void alps_report_mt_data(struct psmouse *psmouse, int n)
 513{
 514	struct alps_data *priv = psmouse->private;
 515	struct input_dev *dev = psmouse->dev;
 516	struct alps_fields *f = &priv->f;
 517	int i, slot[MAX_TOUCHES];
 518
 519	input_mt_assign_slots(dev, slot, f->mt, n, 0);
 520	for (i = 0; i < n; i++)
 521		alps_set_slot(dev, slot[i], f->mt[i].x, f->mt[i].y);
 522
 523	input_mt_sync_frame(dev);
 524}
 525
 526static void alps_report_semi_mt_data(struct psmouse *psmouse, int fingers)
 527{
 528	struct alps_data *priv = psmouse->private;
 529	struct input_dev *dev = psmouse->dev;
 530	struct alps_fields *f = &priv->f;
 531
 532	/* Use st data when we don't have mt data */
 533	if (fingers < 2) {
 534		f->mt[0].x = f->st.x;
 535		f->mt[0].y = f->st.y;
 536		fingers = f->pressure > 0 ? 1 : 0;
 537		priv->second_touch = -1;
 538	}
 539
 540	if (fingers >= 1)
 541		alps_set_slot(dev, 0, f->mt[0].x, f->mt[0].y);
 542	if (fingers >= 2)
 543		alps_set_slot(dev, 1, f->mt[1].x, f->mt[1].y);
 544	input_mt_sync_frame(dev);
 545
 546	input_mt_report_finger_count(dev, fingers);
 547
 548	input_report_key(dev, BTN_LEFT, f->left);
 549	input_report_key(dev, BTN_RIGHT, f->right);
 550	input_report_key(dev, BTN_MIDDLE, f->middle);
 551
 552	input_report_abs(dev, ABS_PRESSURE, f->pressure);
 553
 554	input_sync(dev);
 555}
 556
 557static void alps_process_trackstick_packet_v3(struct psmouse *psmouse)
 558{
 559	struct alps_data *priv = psmouse->private;
 560	unsigned char *packet = psmouse->packet;
 561	struct input_dev *dev = priv->dev2;
 562	int x, y, z, left, right, middle;
 563
 564	/* It should be a DualPoint when received trackstick packet */
 565	if (!(priv->flags & ALPS_DUALPOINT)) {
 566		psmouse_warn(psmouse,
 567			     "Rejected trackstick packet from non DualPoint device");
 568		return;
 569	}
 570
 571	/* Sanity check packet */
 572	if (!(packet[0] & 0x40)) {
 573		psmouse_dbg(psmouse, "Bad trackstick packet, discarding\n");
 574		return;
 575	}
 576
 577	/*
 578	 * There's a special packet that seems to indicate the end
 579	 * of a stream of trackstick data. Filter these out.
 580	 */
 581	if (packet[1] == 0x7f && packet[2] == 0x7f && packet[4] == 0x7f)
 582		return;
 583
 584	x = (s8)(((packet[0] & 0x20) << 2) | (packet[1] & 0x7f));
 585	y = (s8)(((packet[0] & 0x10) << 3) | (packet[2] & 0x7f));
 586	z = packet[4] & 0x7f;
 587
 588	/*
 589	 * The x and y values tend to be quite large, and when used
 590	 * alone the trackstick is difficult to use. Scale them down
 591	 * to compensate.
 592	 */
 593	x /= 8;
 594	y /= 8;
 595
 596	input_report_rel(dev, REL_X, x);
 597	input_report_rel(dev, REL_Y, -y);
 598	input_report_abs(dev, ABS_PRESSURE, z);
 599
 600	/*
 601	 * Most ALPS models report the trackstick buttons in the touchpad
 602	 * packets, but a few report them here. No reliable way has been
 603	 * found to differentiate between the models upfront, so we enable
 604	 * the quirk in response to seeing a button press in the trackstick
 605	 * packet.
 606	 */
 607	left = packet[3] & 0x01;
 608	right = packet[3] & 0x02;
 609	middle = packet[3] & 0x04;
 610
 611	if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) &&
 612	    (left || right || middle))
 613		priv->quirks |= ALPS_QUIRK_TRACKSTICK_BUTTONS;
 614
 615	if (priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) {
 616		input_report_key(dev, BTN_LEFT, left);
 617		input_report_key(dev, BTN_RIGHT, right);
 618		input_report_key(dev, BTN_MIDDLE, middle);
 619	}
 620
 621	input_sync(dev);
 622	return;
 623}
 624
 625static void alps_decode_buttons_v3(struct alps_fields *f, unsigned char *p)
 626{
 627	f->left = !!(p[3] & 0x01);
 628	f->right = !!(p[3] & 0x02);
 629	f->middle = !!(p[3] & 0x04);
 630
 631	f->ts_left = !!(p[3] & 0x10);
 632	f->ts_right = !!(p[3] & 0x20);
 633	f->ts_middle = !!(p[3] & 0x40);
 634}
 635
 636static int alps_decode_pinnacle(struct alps_fields *f, unsigned char *p,
 637				 struct psmouse *psmouse)
 638{
 639	f->first_mp = !!(p[4] & 0x40);
 640	f->is_mp = !!(p[0] & 0x40);
 641
 642	if (f->is_mp) {
 643		f->fingers = (p[5] & 0x3) + 1;
 644		f->x_map = ((p[4] & 0x7e) << 8) |
 645			   ((p[1] & 0x7f) << 2) |
 646			   ((p[0] & 0x30) >> 4);
 647		f->y_map = ((p[3] & 0x70) << 4) |
 648			   ((p[2] & 0x7f) << 1) |
 649			   (p[4] & 0x01);
 650	} else {
 651		f->st.x = ((p[1] & 0x7f) << 4) | ((p[4] & 0x30) >> 2) |
 652		       ((p[0] & 0x30) >> 4);
 653		f->st.y = ((p[2] & 0x7f) << 4) | (p[4] & 0x0f);
 654		f->pressure = p[5] & 0x7f;
 655
 656		alps_decode_buttons_v3(f, p);
 657	}
 658
 659	return 0;
 660}
 661
 662static int alps_decode_rushmore(struct alps_fields *f, unsigned char *p,
 663				 struct psmouse *psmouse)
 664{
 665	f->first_mp = !!(p[4] & 0x40);
 666	f->is_mp = !!(p[5] & 0x40);
 667
 668	if (f->is_mp) {
 669		f->fingers = max((p[5] & 0x3), ((p[5] >> 2) & 0x3)) + 1;
 670		f->x_map = ((p[5] & 0x10) << 11) |
 671			   ((p[4] & 0x7e) << 8) |
 672			   ((p[1] & 0x7f) << 2) |
 673			   ((p[0] & 0x30) >> 4);
 674		f->y_map = ((p[5] & 0x20) << 6) |
 675			   ((p[3] & 0x70) << 4) |
 676			   ((p[2] & 0x7f) << 1) |
 677			   (p[4] & 0x01);
 678	} else {
 679		f->st.x = ((p[1] & 0x7f) << 4) | ((p[4] & 0x30) >> 2) |
 680		       ((p[0] & 0x30) >> 4);
 681		f->st.y = ((p[2] & 0x7f) << 4) | (p[4] & 0x0f);
 682		f->pressure = p[5] & 0x7f;
 683
 684		alps_decode_buttons_v3(f, p);
 685	}
 686
 687	return 0;
 688}
 689
 690static int alps_decode_dolphin(struct alps_fields *f, unsigned char *p,
 691				struct psmouse *psmouse)
 692{
 693	u64 palm_data = 0;
 694	struct alps_data *priv = psmouse->private;
 695
 696	f->first_mp = !!(p[0] & 0x02);
 697	f->is_mp = !!(p[0] & 0x20);
 698
 699	if (!f->is_mp) {
 700		f->st.x = ((p[1] & 0x7f) | ((p[4] & 0x0f) << 7));
 701		f->st.y = ((p[2] & 0x7f) | ((p[4] & 0xf0) << 3));
 702		f->pressure = (p[0] & 4) ? 0 : p[5] & 0x7f;
 703		alps_decode_buttons_v3(f, p);
 704	} else {
 705		f->fingers = ((p[0] & 0x6) >> 1 |
 706		     (p[0] & 0x10) >> 2);
 707
 708		palm_data = (p[1] & 0x7f) |
 709			    ((p[2] & 0x7f) << 7) |
 710			    ((p[4] & 0x7f) << 14) |
 711			    ((p[5] & 0x7f) << 21) |
 712			    ((p[3] & 0x07) << 28) |
 713			    (((u64)p[3] & 0x70) << 27) |
 714			    (((u64)p[0] & 0x01) << 34);
 715
 716		/* Y-profile is stored in P(0) to p(n-1), n = y_bits; */
 717		f->y_map = palm_data & (BIT(priv->y_bits) - 1);
 718
 719		/* X-profile is stored in p(n) to p(n+m-1), m = x_bits; */
 720		f->x_map = (palm_data >> priv->y_bits) &
 721			   (BIT(priv->x_bits) - 1);
 722	}
 723
 724	return 0;
 725}
 726
 727static void alps_process_touchpad_packet_v3_v5(struct psmouse *psmouse)
 728{
 729	struct alps_data *priv = psmouse->private;
 730	unsigned char *packet = psmouse->packet;
 731	struct input_dev *dev2 = priv->dev2;
 732	struct alps_fields *f = &priv->f;
 733	int fingers = 0;
 734
 735	memset(f, 0, sizeof(*f));
 736
 737	priv->decode_fields(f, packet, psmouse);
 738
 739	/*
 740	 * There's no single feature of touchpad position and bitmap packets
 741	 * that can be used to distinguish between them. We rely on the fact
 742	 * that a bitmap packet should always follow a position packet with
 743	 * bit 6 of packet[4] set.
 744	 */
 745	if (priv->multi_packet) {
 746		/*
 747		 * Sometimes a position packet will indicate a multi-packet
 748		 * sequence, but then what follows is another position
 749		 * packet. Check for this, and when it happens process the
 750		 * position packet as usual.
 751		 */
 752		if (f->is_mp) {
 753			fingers = f->fingers;
 754			/*
 755			 * Bitmap processing uses position packet's coordinate
 756			 * data, so we need to do decode it first.
 757			 */
 758			priv->decode_fields(f, priv->multi_data, psmouse);
 759			if (alps_process_bitmap(priv, f) == 0)
 760				fingers = 0; /* Use st data */
 761		} else {
 762			priv->multi_packet = 0;
 763		}
 764	}
 765
 766	/*
 767	 * Bit 6 of byte 0 is not usually set in position packets. The only
 768	 * times it seems to be set is in situations where the data is
 769	 * suspect anyway, e.g. a palm resting flat on the touchpad. Given
 770	 * this combined with the fact that this bit is useful for filtering
 771	 * out misidentified bitmap packets, we reject anything with this
 772	 * bit set.
 773	 */
 774	if (f->is_mp)
 775		return;
 776
 777	if (!priv->multi_packet && f->first_mp) {
 778		priv->multi_packet = 1;
 779		memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
 780		return;
 781	}
 782
 783	priv->multi_packet = 0;
 784
 785	/*
 786	 * Sometimes the hardware sends a single packet with z = 0
 787	 * in the middle of a stream. Real releases generate packets
 788	 * with x, y, and z all zero, so these seem to be flukes.
 789	 * Ignore them.
 790	 */
 791	if (f->st.x && f->st.y && !f->pressure)
 792		return;
 793
 794	alps_report_semi_mt_data(psmouse, fingers);
 795
 796	if ((priv->flags & ALPS_DUALPOINT) &&
 797	    !(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS)) {
 798		input_report_key(dev2, BTN_LEFT, f->ts_left);
 799		input_report_key(dev2, BTN_RIGHT, f->ts_right);
 800		input_report_key(dev2, BTN_MIDDLE, f->ts_middle);
 801		input_sync(dev2);
 802	}
 803}
 804
 805static void alps_process_packet_v3(struct psmouse *psmouse)
 806{
 807	unsigned char *packet = psmouse->packet;
 808
 809	/*
 810	 * v3 protocol packets come in three types, two representing
 811	 * touchpad data and one representing trackstick data.
 812	 * Trackstick packets seem to be distinguished by always
 813	 * having 0x3f in the last byte. This value has never been
 814	 * observed in the last byte of either of the other types
 815	 * of packets.
 816	 */
 817	if (packet[5] == 0x3f) {
 818		alps_process_trackstick_packet_v3(psmouse);
 819		return;
 820	}
 821
 822	alps_process_touchpad_packet_v3_v5(psmouse);
 823}
 824
 825static void alps_process_packet_v6(struct psmouse *psmouse)
 826{
 827	struct alps_data *priv = psmouse->private;
 828	unsigned char *packet = psmouse->packet;
 829	struct input_dev *dev = psmouse->dev;
 830	struct input_dev *dev2 = priv->dev2;
 831	int x, y, z;
 832
 833	/*
 834	 * We can use Byte5 to distinguish if the packet is from Touchpad
 835	 * or Trackpoint.
 836	 * Touchpad:	0 - 0x7E
 837	 * Trackpoint:	0x7F
 838	 */
 839	if (packet[5] == 0x7F) {
 840		/* It should be a DualPoint when received Trackpoint packet */
 841		if (!(priv->flags & ALPS_DUALPOINT)) {
 842			psmouse_warn(psmouse,
 843				     "Rejected trackstick packet from non DualPoint device");
 844			return;
 845		}
 846
 847		/* Trackpoint packet */
 848		x = packet[1] | ((packet[3] & 0x20) << 2);
 849		y = packet[2] | ((packet[3] & 0x40) << 1);
 850		z = packet[4];
 851
 852		/* To prevent the cursor jump when finger lifted */
 853		if (x == 0x7F && y == 0x7F && z == 0x7F)
 854			x = y = z = 0;
 855
 856		/* Divide 4 since trackpoint's speed is too fast */
 857		input_report_rel(dev2, REL_X, (char)x / 4);
 858		input_report_rel(dev2, REL_Y, -((char)y / 4));
 859
 860		psmouse_report_standard_buttons(dev2, packet[3]);
 861
 862		input_sync(dev2);
 863		return;
 864	}
 865
 866	/* Touchpad packet */
 867	x = packet[1] | ((packet[3] & 0x78) << 4);
 868	y = packet[2] | ((packet[4] & 0x78) << 4);
 869	z = packet[5];
 870
 871	if (z > 30)
 872		input_report_key(dev, BTN_TOUCH, 1);
 873	if (z < 25)
 874		input_report_key(dev, BTN_TOUCH, 0);
 875
 876	if (z > 0) {
 877		input_report_abs(dev, ABS_X, x);
 878		input_report_abs(dev, ABS_Y, y);
 879	}
 880
 881	input_report_abs(dev, ABS_PRESSURE, z);
 882	input_report_key(dev, BTN_TOOL_FINGER, z > 0);
 883
 884	/* v6 touchpad does not have middle button */
 885	packet[3] &= ~BIT(2);
 886	psmouse_report_standard_buttons(dev2, packet[3]);
 887
 888	input_sync(dev);
 889}
 890
 891static void alps_process_packet_v4(struct psmouse *psmouse)
 892{
 893	struct alps_data *priv = psmouse->private;
 894	unsigned char *packet = psmouse->packet;
 895	struct alps_fields *f = &priv->f;
 896	int offset;
 897
 898	/*
 899	 * v4 has a 6-byte encoding for bitmap data, but this data is
 900	 * broken up between 3 normal packets. Use priv->multi_packet to
 901	 * track our position in the bitmap packet.
 902	 */
 903	if (packet[6] & 0x40) {
 904		/* sync, reset position */
 905		priv->multi_packet = 0;
 906	}
 907
 908	if (WARN_ON_ONCE(priv->multi_packet > 2))
 909		return;
 910
 911	offset = 2 * priv->multi_packet;
 912	priv->multi_data[offset] = packet[6];
 913	priv->multi_data[offset + 1] = packet[7];
 914
 915	f->left = !!(packet[4] & 0x01);
 916	f->right = !!(packet[4] & 0x02);
 917
 918	f->st.x = ((packet[1] & 0x7f) << 4) | ((packet[3] & 0x30) >> 2) |
 919		  ((packet[0] & 0x30) >> 4);
 920	f->st.y = ((packet[2] & 0x7f) << 4) | (packet[3] & 0x0f);
 921	f->pressure = packet[5] & 0x7f;
 922
 923	if (++priv->multi_packet > 2) {
 924		priv->multi_packet = 0;
 925
 926		f->x_map = ((priv->multi_data[2] & 0x1f) << 10) |
 927			   ((priv->multi_data[3] & 0x60) << 3) |
 928			   ((priv->multi_data[0] & 0x3f) << 2) |
 929			   ((priv->multi_data[1] & 0x60) >> 5);
 930		f->y_map = ((priv->multi_data[5] & 0x01) << 10) |
 931			   ((priv->multi_data[3] & 0x1f) << 5) |
 932			    (priv->multi_data[1] & 0x1f);
 933
 934		f->fingers = alps_process_bitmap(priv, f);
 935	}
 936
 937	alps_report_semi_mt_data(psmouse, f->fingers);
 938}
 939
 940static bool alps_is_valid_package_v7(struct psmouse *psmouse)
 941{
 942	switch (psmouse->pktcnt) {
 943	case 3:
 944		return (psmouse->packet[2] & 0x40) == 0x40;
 945	case 4:
 946		return (psmouse->packet[3] & 0x48) == 0x48;
 947	case 6:
 948		return (psmouse->packet[5] & 0x40) == 0x00;
 949	}
 950	return true;
 951}
 952
 953static unsigned char alps_get_packet_id_v7(char *byte)
 954{
 955	unsigned char packet_id;
 956
 957	if (byte[4] & 0x40)
 958		packet_id = V7_PACKET_ID_TWO;
 959	else if (byte[4] & 0x01)
 960		packet_id = V7_PACKET_ID_MULTI;
 961	else if ((byte[0] & 0x10) && !(byte[4] & 0x43))
 962		packet_id = V7_PACKET_ID_NEW;
 963	else if (byte[1] == 0x00 && byte[4] == 0x00)
 964		packet_id = V7_PACKET_ID_IDLE;
 965	else
 966		packet_id = V7_PACKET_ID_UNKNOWN;
 967
 968	return packet_id;
 969}
 970
 971static void alps_get_finger_coordinate_v7(struct input_mt_pos *mt,
 972					  unsigned char *pkt,
 973					  unsigned char pkt_id)
 974{
 975	mt[0].x = ((pkt[2] & 0x80) << 4);
 976	mt[0].x |= ((pkt[2] & 0x3F) << 5);
 977	mt[0].x |= ((pkt[3] & 0x30) >> 1);
 978	mt[0].x |= (pkt[3] & 0x07);
 979	mt[0].y = (pkt[1] << 3) | (pkt[0] & 0x07);
 980
 981	mt[1].x = ((pkt[3] & 0x80) << 4);
 982	mt[1].x |= ((pkt[4] & 0x80) << 3);
 983	mt[1].x |= ((pkt[4] & 0x3F) << 4);
 984	mt[1].y = ((pkt[5] & 0x80) << 3);
 985	mt[1].y |= ((pkt[5] & 0x3F) << 4);
 986
 987	switch (pkt_id) {
 988	case V7_PACKET_ID_TWO:
 989		mt[1].x &= ~0x000F;
 990		mt[1].y |= 0x000F;
 991		/* Detect false-postive touches where x & y report max value */
 992		if (mt[1].y == 0x7ff && mt[1].x == 0xff0) {
 993			mt[1].x = 0;
 994			/* y gets set to 0 at the end of this function */
 995		}
 996		break;
 997
 998	case V7_PACKET_ID_MULTI:
 999		mt[1].x &= ~0x003F;
1000		mt[1].y &= ~0x0020;
1001		mt[1].y |= ((pkt[4] & 0x02) << 4);
1002		mt[1].y |= 0x001F;
1003		break;
1004
1005	case V7_PACKET_ID_NEW:
1006		mt[1].x &= ~0x003F;
1007		mt[1].x |= (pkt[0] & 0x20);
1008		mt[1].y |= 0x000F;
1009		break;
1010	}
1011
1012	mt[0].y = 0x7FF - mt[0].y;
1013	mt[1].y = 0x7FF - mt[1].y;
1014}
1015
1016static int alps_get_mt_count(struct input_mt_pos *mt)
1017{
1018	int i, fingers = 0;
1019
1020	for (i = 0; i < MAX_TOUCHES; i++) {
1021		if (mt[i].x != 0 || mt[i].y != 0)
1022			fingers++;
1023	}
1024
1025	return fingers;
1026}
1027
1028static int alps_decode_packet_v7(struct alps_fields *f,
1029				  unsigned char *p,
1030				  struct psmouse *psmouse)
1031{
1032	struct alps_data *priv = psmouse->private;
1033	unsigned char pkt_id;
1034
1035	pkt_id = alps_get_packet_id_v7(p);
1036	if (pkt_id == V7_PACKET_ID_IDLE)
1037		return 0;
1038	if (pkt_id == V7_PACKET_ID_UNKNOWN)
1039		return -1;
1040	/*
1041	 * NEW packets are send to indicate a discontinuity in the finger
1042	 * coordinate reporting. Specifically a finger may have moved from
1043	 * slot 0 to 1 or vice versa. INPUT_MT_TRACK takes care of this for
1044	 * us.
1045	 *
1046	 * NEW packets have 3 problems:
1047	 * 1) They do not contain middle / right button info (on non clickpads)
1048	 *    this can be worked around by preserving the old button state
1049	 * 2) They do not contain an accurate fingercount, and they are
1050	 *    typically send when the number of fingers changes. We cannot use
1051	 *    the old finger count as that may mismatch with the amount of
1052	 *    touch coordinates we've available in the NEW packet
1053	 * 3) Their x data for the second touch is inaccurate leading to
1054	 *    a possible jump of the x coordinate by 16 units when the first
1055	 *    non NEW packet comes in
1056	 * Since problems 2 & 3 cannot be worked around, just ignore them.
1057	 */
1058	if (pkt_id == V7_PACKET_ID_NEW)
1059		return 1;
1060
1061	alps_get_finger_coordinate_v7(f->mt, p, pkt_id);
1062
1063	if (pkt_id == V7_PACKET_ID_TWO)
1064		f->fingers = alps_get_mt_count(f->mt);
1065	else /* pkt_id == V7_PACKET_ID_MULTI */
1066		f->fingers = 3 + (p[5] & 0x03);
1067
1068	f->left = (p[0] & 0x80) >> 7;
1069	if (priv->flags & ALPS_BUTTONPAD) {
1070		if (p[0] & 0x20)
1071			f->fingers++;
1072		if (p[0] & 0x10)
1073			f->fingers++;
1074	} else {
1075		f->right = (p[0] & 0x20) >> 5;
1076		f->middle = (p[0] & 0x10) >> 4;
1077	}
1078
1079	/* Sometimes a single touch is reported in mt[1] rather then mt[0] */
1080	if (f->fingers == 1 && f->mt[0].x == 0 && f->mt[0].y == 0) {
1081		f->mt[0].x = f->mt[1].x;
1082		f->mt[0].y = f->mt[1].y;
1083		f->mt[1].x = 0;
1084		f->mt[1].y = 0;
1085	}
1086
1087	return 0;
1088}
1089
1090static void alps_process_trackstick_packet_v7(struct psmouse *psmouse)
1091{
1092	struct alps_data *priv = psmouse->private;
1093	unsigned char *packet = psmouse->packet;
1094	struct input_dev *dev2 = priv->dev2;
1095	int x, y, z;
1096
1097	/* It should be a DualPoint when received trackstick packet */
1098	if (!(priv->flags & ALPS_DUALPOINT)) {
1099		psmouse_warn(psmouse,
1100			     "Rejected trackstick packet from non DualPoint device");
1101		return;
1102	}
1103
1104	x = ((packet[2] & 0xbf)) | ((packet[3] & 0x10) << 2);
1105	y = (packet[3] & 0x07) | (packet[4] & 0xb8) |
1106	    ((packet[3] & 0x20) << 1);
1107	z = (packet[5] & 0x3f) | ((packet[3] & 0x80) >> 1);
1108
1109	input_report_rel(dev2, REL_X, (char)x);
1110	input_report_rel(dev2, REL_Y, -((char)y));
1111	input_report_abs(dev2, ABS_PRESSURE, z);
1112
1113	psmouse_report_standard_buttons(dev2, packet[1]);
1114
1115	input_sync(dev2);
1116}
1117
1118static void alps_process_touchpad_packet_v7(struct psmouse *psmouse)
1119{
1120	struct alps_data *priv = psmouse->private;
1121	struct input_dev *dev = psmouse->dev;
1122	struct alps_fields *f = &priv->f;
1123
1124	memset(f, 0, sizeof(*f));
1125
1126	if (priv->decode_fields(f, psmouse->packet, psmouse))
1127		return;
1128
1129	alps_report_mt_data(psmouse, alps_get_mt_count(f->mt));
1130
1131	input_mt_report_finger_count(dev, f->fingers);
1132
1133	input_report_key(dev, BTN_LEFT, f->left);
1134	input_report_key(dev, BTN_RIGHT, f->right);
1135	input_report_key(dev, BTN_MIDDLE, f->middle);
1136
1137	input_sync(dev);
1138}
1139
1140static void alps_process_packet_v7(struct psmouse *psmouse)
1141{
1142	unsigned char *packet = psmouse->packet;
1143
1144	if (packet[0] == 0x48 && (packet[4] & 0x47) == 0x06)
1145		alps_process_trackstick_packet_v7(psmouse);
1146	else
1147		alps_process_touchpad_packet_v7(psmouse);
1148}
1149
1150static enum SS4_PACKET_ID alps_get_pkt_id_ss4_v2(unsigned char *byte)
1151{
1152	enum SS4_PACKET_ID pkt_id = SS4_PACKET_ID_IDLE;
1153
1154	switch (byte[3] & 0x30) {
1155	case 0x00:
1156		if (SS4_IS_IDLE_V2(byte)) {
1157			pkt_id = SS4_PACKET_ID_IDLE;
1158		} else {
1159			pkt_id = SS4_PACKET_ID_ONE;
1160		}
1161		break;
1162	case 0x10:
1163		/* two-finger finger positions */
1164		pkt_id = SS4_PACKET_ID_TWO;
1165		break;
1166	case 0x20:
1167		/* stick pointer */
1168		pkt_id = SS4_PACKET_ID_STICK;
1169		break;
1170	case 0x30:
1171		/* third and fourth finger positions */
1172		pkt_id = SS4_PACKET_ID_MULTI;
1173		break;
1174	}
1175
1176	return pkt_id;
1177}
1178
1179static int alps_decode_ss4_v2(struct alps_fields *f,
1180			      unsigned char *p, struct psmouse *psmouse)
1181{
1182	struct alps_data *priv = psmouse->private;
1183	enum SS4_PACKET_ID pkt_id;
1184	unsigned int no_data_x, no_data_y;
1185
1186	pkt_id = alps_get_pkt_id_ss4_v2(p);
1187
1188	/* Current packet is 1Finger coordinate packet */
1189	switch (pkt_id) {
1190	case SS4_PACKET_ID_ONE:
1191		f->mt[0].x = SS4_1F_X_V2(p);
1192		f->mt[0].y = SS4_1F_Y_V2(p);
1193		f->pressure = ((SS4_1F_Z_V2(p)) * 2) & 0x7f;
1194		/*
1195		 * When a button is held the device will give us events
1196		 * with x, y, and pressure of 0. This causes annoying jumps
1197		 * if a touch is released while the button is held.
1198		 * Handle this by claiming zero contacts.
1199		 */
1200		f->fingers = f->pressure > 0 ? 1 : 0;
1201		f->first_mp = 0;
1202		f->is_mp = 0;
1203		break;
1204
1205	case SS4_PACKET_ID_TWO:
1206		if (priv->flags & ALPS_BUTTONPAD) {
1207			if (IS_SS4PLUS_DEV(priv->dev_id)) {
1208				f->mt[0].x = SS4_PLUS_BTL_MF_X_V2(p, 0);
1209				f->mt[1].x = SS4_PLUS_BTL_MF_X_V2(p, 1);
1210			} else {
1211				f->mt[0].x = SS4_BTL_MF_X_V2(p, 0);
1212				f->mt[1].x = SS4_BTL_MF_X_V2(p, 1);
1213			}
1214			f->mt[0].y = SS4_BTL_MF_Y_V2(p, 0);
1215			f->mt[1].y = SS4_BTL_MF_Y_V2(p, 1);
1216		} else {
1217			if (IS_SS4PLUS_DEV(priv->dev_id)) {
1218				f->mt[0].x = SS4_PLUS_STD_MF_X_V2(p, 0);
1219				f->mt[1].x = SS4_PLUS_STD_MF_X_V2(p, 1);
1220			} else {
1221				f->mt[0].x = SS4_STD_MF_X_V2(p, 0);
1222				f->mt[1].x = SS4_STD_MF_X_V2(p, 1);
1223			}
1224			f->mt[0].y = SS4_STD_MF_Y_V2(p, 0);
1225			f->mt[1].y = SS4_STD_MF_Y_V2(p, 1);
1226		}
1227		f->pressure = SS4_MF_Z_V2(p, 0) ? 0x30 : 0;
1228
1229		if (SS4_IS_MF_CONTINUE(p)) {
1230			f->first_mp = 1;
1231		} else {
1232			f->fingers = 2;
1233			f->first_mp = 0;
1234		}
1235		f->is_mp = 0;
1236
1237		break;
1238
1239	case SS4_PACKET_ID_MULTI:
1240		if (priv->flags & ALPS_BUTTONPAD) {
1241			if (IS_SS4PLUS_DEV(priv->dev_id)) {
1242				f->mt[2].x = SS4_PLUS_BTL_MF_X_V2(p, 0);
1243				f->mt[3].x = SS4_PLUS_BTL_MF_X_V2(p, 1);
1244				no_data_x = SS4_PLUS_MFPACKET_NO_AX_BL;
1245			} else {
1246				f->mt[2].x = SS4_BTL_MF_X_V2(p, 0);
1247				f->mt[3].x = SS4_BTL_MF_X_V2(p, 1);
1248				no_data_x = SS4_MFPACKET_NO_AX_BL;
1249			}
1250			no_data_y = SS4_MFPACKET_NO_AY_BL;
1251
1252			f->mt[2].y = SS4_BTL_MF_Y_V2(p, 0);
1253			f->mt[3].y = SS4_BTL_MF_Y_V2(p, 1);
1254		} else {
1255			if (IS_SS4PLUS_DEV(priv->dev_id)) {
1256				f->mt[2].x = SS4_PLUS_STD_MF_X_V2(p, 0);
1257				f->mt[3].x = SS4_PLUS_STD_MF_X_V2(p, 1);
1258				no_data_x = SS4_PLUS_MFPACKET_NO_AX;
1259			} else {
1260				f->mt[2].x = SS4_STD_MF_X_V2(p, 0);
1261				f->mt[3].x = SS4_STD_MF_X_V2(p, 1);
1262				no_data_x = SS4_MFPACKET_NO_AX;
1263			}
1264			no_data_y = SS4_MFPACKET_NO_AY;
1265
1266			f->mt[2].y = SS4_STD_MF_Y_V2(p, 0);
1267			f->mt[3].y = SS4_STD_MF_Y_V2(p, 1);
1268		}
1269
1270		f->first_mp = 0;
1271		f->is_mp = 1;
1272
1273		if (SS4_IS_5F_DETECTED(p)) {
1274			f->fingers = 5;
1275		} else if (f->mt[3].x == no_data_x &&
1276			     f->mt[3].y == no_data_y) {
1277			f->mt[3].x = 0;
1278			f->mt[3].y = 0;
1279			f->fingers = 3;
1280		} else {
1281			f->fingers = 4;
1282		}
1283		break;
1284
1285	case SS4_PACKET_ID_STICK:
1286		/*
1287		 * x, y, and pressure are decoded in
1288		 * alps_process_packet_ss4_v2()
1289		 */
1290		f->first_mp = 0;
1291		f->is_mp = 0;
1292		break;
1293
1294	case SS4_PACKET_ID_IDLE:
1295	default:
1296		memset(f, 0, sizeof(struct alps_fields));
1297		break;
1298	}
1299
1300	/* handle buttons */
1301	if (pkt_id == SS4_PACKET_ID_STICK) {
1302		f->ts_left = !!(SS4_BTN_V2(p) & 0x01);
1303		f->ts_right = !!(SS4_BTN_V2(p) & 0x02);
1304		f->ts_middle = !!(SS4_BTN_V2(p) & 0x04);
1305	} else {
1306		f->left = !!(SS4_BTN_V2(p) & 0x01);
1307		if (!(priv->flags & ALPS_BUTTONPAD)) {
1308			f->right = !!(SS4_BTN_V2(p) & 0x02);
1309			f->middle = !!(SS4_BTN_V2(p) & 0x04);
1310		}
1311	}
1312
1313	return 0;
1314}
1315
1316static void alps_process_packet_ss4_v2(struct psmouse *psmouse)
1317{
1318	struct alps_data *priv = psmouse->private;
1319	unsigned char *packet = psmouse->packet;
1320	struct input_dev *dev = psmouse->dev;
1321	struct input_dev *dev2 = priv->dev2;
1322	struct alps_fields *f = &priv->f;
1323
1324	memset(f, 0, sizeof(struct alps_fields));
1325	priv->decode_fields(f, packet, psmouse);
1326	if (priv->multi_packet) {
1327		/*
1328		 * Sometimes the first packet will indicate a multi-packet
1329		 * sequence, but sometimes the next multi-packet would not
1330		 * come. Check for this, and when it happens process the
1331		 * position packet as usual.
1332		 */
1333		if (f->is_mp) {
1334			/* Now process the 1st packet */
1335			priv->decode_fields(f, priv->multi_data, psmouse);
1336		} else {
1337			priv->multi_packet = 0;
1338		}
1339	}
1340
1341	/*
1342	 * "f.is_mp" would always be '0' after merging the 1st and 2nd packet.
1343	 * When it is set, it means 2nd packet comes without 1st packet come.
1344	 */
1345	if (f->is_mp)
1346		return;
1347
1348	/* Save the first packet */
1349	if (!priv->multi_packet && f->first_mp) {
1350		priv->multi_packet = 1;
1351		memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
1352		return;
1353	}
1354
1355	priv->multi_packet = 0;
1356
1357	/* Report trackstick */
1358	if (alps_get_pkt_id_ss4_v2(packet) == SS4_PACKET_ID_STICK) {
1359		if (!(priv->flags & ALPS_DUALPOINT)) {
1360			psmouse_warn(psmouse,
1361				     "Rejected trackstick packet from non DualPoint device");
1362			return;
1363		}
1364
1365		input_report_rel(dev2, REL_X, SS4_TS_X_V2(packet));
1366		input_report_rel(dev2, REL_Y, SS4_TS_Y_V2(packet));
1367		input_report_abs(dev2, ABS_PRESSURE, SS4_TS_Z_V2(packet));
1368
1369		input_report_key(dev2, BTN_LEFT, f->ts_left);
1370		input_report_key(dev2, BTN_RIGHT, f->ts_right);
1371		input_report_key(dev2, BTN_MIDDLE, f->ts_middle);
1372
1373		input_sync(dev2);
1374		return;
1375	}
1376
1377	/* Report touchpad */
1378	alps_report_mt_data(psmouse, (f->fingers <= 4) ? f->fingers : 4);
1379
1380	input_mt_report_finger_count(dev, f->fingers);
1381
1382	input_report_key(dev, BTN_LEFT, f->left);
1383	input_report_key(dev, BTN_RIGHT, f->right);
1384	input_report_key(dev, BTN_MIDDLE, f->middle);
1385
1386	input_report_abs(dev, ABS_PRESSURE, f->pressure);
1387	input_sync(dev);
1388}
1389
1390static bool alps_is_valid_package_ss4_v2(struct psmouse *psmouse)
1391{
1392	if (psmouse->pktcnt == 4 && ((psmouse->packet[3] & 0x08) != 0x08))
1393		return false;
1394	if (psmouse->pktcnt == 6 && ((psmouse->packet[5] & 0x10) != 0x0))
1395		return false;
1396	return true;
1397}
1398
1399static DEFINE_MUTEX(alps_mutex);
1400
1401static void alps_register_bare_ps2_mouse(struct work_struct *work)
1402{
1403	struct alps_data *priv =
1404		container_of(work, struct alps_data, dev3_register_work.work);
1405	struct psmouse *psmouse = priv->psmouse;
1406	struct input_dev *dev3;
1407	int error = 0;
1408
1409	mutex_lock(&alps_mutex);
1410
1411	if (priv->dev3)
1412		goto out;
1413
1414	dev3 = input_allocate_device();
1415	if (!dev3) {
1416		psmouse_err(psmouse, "failed to allocate secondary device\n");
1417		error = -ENOMEM;
1418		goto out;
1419	}
1420
1421	snprintf(priv->phys3, sizeof(priv->phys3), "%s/%s",
1422		 psmouse->ps2dev.serio->phys,
1423		 (priv->dev2 ? "input2" : "input1"));
1424	dev3->phys = priv->phys3;
1425
1426	/*
1427	 * format of input device name is: "protocol vendor name"
1428	 * see function psmouse_switch_protocol() in psmouse-base.c
1429	 */
1430	dev3->name = "PS/2 ALPS Mouse";
1431
1432	dev3->id.bustype = BUS_I8042;
1433	dev3->id.vendor  = 0x0002;
1434	dev3->id.product = PSMOUSE_PS2;
1435	dev3->id.version = 0x0000;
1436	dev3->dev.parent = &psmouse->ps2dev.serio->dev;
1437
1438	input_set_capability(dev3, EV_REL, REL_X);
1439	input_set_capability(dev3, EV_REL, REL_Y);
1440	input_set_capability(dev3, EV_KEY, BTN_LEFT);
1441	input_set_capability(dev3, EV_KEY, BTN_RIGHT);
1442	input_set_capability(dev3, EV_KEY, BTN_MIDDLE);
1443
1444	__set_bit(INPUT_PROP_POINTER, dev3->propbit);
1445
1446	error = input_register_device(dev3);
1447	if (error) {
1448		psmouse_err(psmouse,
1449			    "failed to register secondary device: %d\n",
1450			    error);
1451		input_free_device(dev3);
1452		goto out;
1453	}
1454
1455	priv->dev3 = dev3;
1456
1457out:
1458	/*
1459	 * Save the error code so that we can detect that we
1460	 * already tried to create the device.
1461	 */
1462	if (error)
1463		priv->dev3 = ERR_PTR(error);
1464
1465	mutex_unlock(&alps_mutex);
1466}
1467
1468static void alps_report_bare_ps2_packet(struct psmouse *psmouse,
1469					unsigned char packet[],
1470					bool report_buttons)
1471{
1472	struct alps_data *priv = psmouse->private;
1473	struct input_dev *dev, *dev2 = NULL;
1474
1475	/* Figure out which device to use to report the bare packet */
1476	if (priv->proto_version == ALPS_PROTO_V2 &&
1477	    (priv->flags & ALPS_DUALPOINT)) {
1478		/* On V2 devices the DualPoint Stick reports bare packets */
1479		dev = priv->dev2;
1480		dev2 = psmouse->dev;
1481	} else if (unlikely(IS_ERR_OR_NULL(priv->dev3))) {
1482		/* Register dev3 mouse if we received PS/2 packet first time */
1483		if (!IS_ERR(priv->dev3))
1484			psmouse_queue_work(psmouse, &priv->dev3_register_work,
1485					   0);
1486		return;
1487	} else {
1488		dev = priv->dev3;
1489	}
1490
1491	if (report_buttons)
1492		alps_report_buttons(dev, dev2,
1493				packet[0] & 1, packet[0] & 2, packet[0] & 4);
1494
1495	psmouse_report_standard_motion(dev, packet);
1496
1497	input_sync(dev);
1498}
1499
1500static psmouse_ret_t alps_handle_interleaved_ps2(struct psmouse *psmouse)
1501{
1502	struct alps_data *priv = psmouse->private;
1503
1504	if (psmouse->pktcnt < 6)
1505		return PSMOUSE_GOOD_DATA;
1506
1507	if (psmouse->pktcnt == 6) {
1508		/*
1509		 * Start a timer to flush the packet if it ends up last
1510		 * 6-byte packet in the stream. Timer needs to fire
1511		 * psmouse core times out itself. 20 ms should be enough
1512		 * to decide if we are getting more data or not.
1513		 */
1514		mod_timer(&priv->timer, jiffies + msecs_to_jiffies(20));
1515		return PSMOUSE_GOOD_DATA;
1516	}
1517
1518	del_timer(&priv->timer);
1519
1520	if (psmouse->packet[6] & 0x80) {
1521
1522		/*
1523		 * Highest bit is set - that means we either had
1524		 * complete ALPS packet and this is start of the
1525		 * next packet or we got garbage.
1526		 */
1527
1528		if (((psmouse->packet[3] |
1529		      psmouse->packet[4] |
1530		      psmouse->packet[5]) & 0x80) ||
1531		    (!alps_is_valid_first_byte(priv, psmouse->packet[6]))) {
1532			psmouse_dbg(psmouse,
1533				    "refusing packet %4ph (suspected interleaved ps/2)\n",
1534				    psmouse->packet + 3);
1535			return PSMOUSE_BAD_DATA;
1536		}
1537
1538		priv->process_packet(psmouse);
1539
1540		/* Continue with the next packet */
1541		psmouse->packet[0] = psmouse->packet[6];
1542		psmouse->pktcnt = 1;
1543
1544	} else {
1545
1546		/*
1547		 * High bit is 0 - that means that we indeed got a PS/2
1548		 * packet in the middle of ALPS packet.
1549		 *
1550		 * There is also possibility that we got 6-byte ALPS
1551		 * packet followed  by 3-byte packet from trackpoint. We
1552		 * can not distinguish between these 2 scenarios but
1553		 * because the latter is unlikely to happen in course of
1554		 * normal operation (user would need to press all
1555		 * buttons on the pad and start moving trackpoint
1556		 * without touching the pad surface) we assume former.
1557		 * Even if we are wrong the wost thing that would happen
1558		 * the cursor would jump but we should not get protocol
1559		 * de-synchronization.
1560		 */
1561
1562		alps_report_bare_ps2_packet(psmouse, &psmouse->packet[3],
1563					    false);
1564
1565		/*
1566		 * Continue with the standard ALPS protocol handling,
1567		 * but make sure we won't process it as an interleaved
1568		 * packet again, which may happen if all buttons are
1569		 * pressed. To avoid this let's reset the 4th bit which
1570		 * is normally 1.
1571		 */
1572		psmouse->packet[3] = psmouse->packet[6] & 0xf7;
1573		psmouse->pktcnt = 4;
1574	}
1575
1576	return PSMOUSE_GOOD_DATA;
1577}
1578
1579static void alps_flush_packet(struct timer_list *t)
1580{
1581	struct alps_data *priv = from_timer(priv, t, timer);
1582	struct psmouse *psmouse = priv->psmouse;
1583
1584	serio_pause_rx(psmouse->ps2dev.serio);
1585
1586	if (psmouse->pktcnt == psmouse->pktsize) {
1587
1588		/*
1589		 * We did not any more data in reasonable amount of time.
1590		 * Validate the last 3 bytes and process as a standard
1591		 * ALPS packet.
1592		 */
1593		if ((psmouse->packet[3] |
1594		     psmouse->packet[4] |
1595		     psmouse->packet[5]) & 0x80) {
1596			psmouse_dbg(psmouse,
1597				    "refusing packet %3ph (suspected interleaved ps/2)\n",
1598				    psmouse->packet + 3);
1599		} else {
1600			priv->process_packet(psmouse);
1601		}
1602		psmouse->pktcnt = 0;
1603	}
1604
1605	serio_continue_rx(psmouse->ps2dev.serio);
1606}
1607
1608static psmouse_ret_t alps_process_byte(struct psmouse *psmouse)
1609{
1610	struct alps_data *priv = psmouse->private;
1611
1612	/*
1613	 * Check if we are dealing with a bare PS/2 packet, presumably from
1614	 * a device connected to the external PS/2 port. Because bare PS/2
1615	 * protocol does not have enough constant bits to self-synchronize
1616	 * properly we only do this if the device is fully synchronized.
1617	 * Can not distinguish V8's first byte from PS/2 packet's
1618	 */
1619	if (priv->proto_version != ALPS_PROTO_V8 &&
1620	    !psmouse->out_of_sync_cnt &&
1621	    (psmouse->packet[0] & 0xc8) == 0x08) {
1622
1623		if (psmouse->pktcnt == 3) {
1624			alps_report_bare_ps2_packet(psmouse, psmouse->packet,
1625						    true);
1626			return PSMOUSE_FULL_PACKET;
1627		}
1628		return PSMOUSE_GOOD_DATA;
1629	}
1630
1631	/* Check for PS/2 packet stuffed in the middle of ALPS packet. */
1632
1633	if ((priv->flags & ALPS_PS2_INTERLEAVED) &&
1634	    psmouse->pktcnt >= 4 && (psmouse->packet[3] & 0x0f) == 0x0f) {
1635		return alps_handle_interleaved_ps2(psmouse);
1636	}
1637
1638	if (!alps_is_valid_first_byte(priv, psmouse->packet[0])) {
1639		psmouse_dbg(psmouse,
1640			    "refusing packet[0] = %x (mask0 = %x, byte0 = %x)\n",
1641			    psmouse->packet[0], priv->mask0, priv->byte0);
1642		return PSMOUSE_BAD_DATA;
1643	}
1644
1645	/* Bytes 2 - pktsize should have 0 in the highest bit */
1646	if (priv->proto_version < ALPS_PROTO_V5 &&
1647	    psmouse->pktcnt >= 2 && psmouse->pktcnt <= psmouse->pktsize &&
1648	    (psmouse->packet[psmouse->pktcnt - 1] & 0x80)) {
1649		psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
1650			    psmouse->pktcnt - 1,
1651			    psmouse->packet[psmouse->pktcnt - 1]);
1652
1653		if (priv->proto_version == ALPS_PROTO_V3_RUSHMORE &&
1654		    psmouse->pktcnt == psmouse->pktsize) {
1655			/*
1656			 * Some Dell boxes, such as Latitude E6440 or E7440
1657			 * with closed lid, quite often smash last byte of
1658			 * otherwise valid packet with 0xff. Given that the
1659			 * next packet is very likely to be valid let's
1660			 * report PSMOUSE_FULL_PACKET but not process data,
1661			 * rather than reporting PSMOUSE_BAD_DATA and
1662			 * filling the logs.
1663			 */
1664			return PSMOUSE_FULL_PACKET;
1665		}
1666
1667		return PSMOUSE_BAD_DATA;
1668	}
1669
1670	if ((priv->proto_version == ALPS_PROTO_V7 &&
1671			!alps_is_valid_package_v7(psmouse)) ||
1672	    (priv->proto_version == ALPS_PROTO_V8 &&
1673			!alps_is_valid_package_ss4_v2(psmouse))) {
1674		psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
1675			    psmouse->pktcnt - 1,
1676			    psmouse->packet[psmouse->pktcnt - 1]);
1677		return PSMOUSE_BAD_DATA;
1678	}
1679
1680	if (psmouse->pktcnt == psmouse->pktsize) {
1681		priv->process_packet(psmouse);
1682		return PSMOUSE_FULL_PACKET;
1683	}
1684
1685	return PSMOUSE_GOOD_DATA;
1686}
1687
1688static int alps_command_mode_send_nibble(struct psmouse *psmouse, int nibble)
1689{
1690	struct ps2dev *ps2dev = &psmouse->ps2dev;
1691	struct alps_data *priv = psmouse->private;
1692	int command;
1693	unsigned char *param;
1694	unsigned char dummy[4];
1695
1696	BUG_ON(nibble > 0xf);
1697
1698	command = priv->nibble_commands[nibble].command;
1699	param = (command & 0x0f00) ?
1700		dummy : (unsigned char *)&priv->nibble_commands[nibble].data;
1701
1702	if (ps2_command(ps2dev, param, command))
1703		return -1;
1704
1705	return 0;
1706}
1707
1708static int alps_command_mode_set_addr(struct psmouse *psmouse, int addr)
1709{
1710	struct ps2dev *ps2dev = &psmouse->ps2dev;
1711	struct alps_data *priv = psmouse->private;
1712	int i, nibble;
1713
1714	if (ps2_command(ps2dev, NULL, priv->addr_command))
1715		return -1;
1716
1717	for (i = 12; i >= 0; i -= 4) {
1718		nibble = (addr >> i) & 0xf;
1719		if (alps_command_mode_send_nibble(psmouse, nibble))
1720			return -1;
1721	}
1722
1723	return 0;
1724}
1725
1726static int __alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
1727{
1728	struct ps2dev *ps2dev = &psmouse->ps2dev;
1729	unsigned char param[4];
1730
1731	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1732		return -1;
1733
1734	/*
1735	 * The address being read is returned in the first two bytes
1736	 * of the result. Check that this address matches the expected
1737	 * address.
1738	 */
1739	if (addr != ((param[0] << 8) | param[1]))
1740		return -1;
1741
1742	return param[2];
1743}
1744
1745static int alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
1746{
1747	if (alps_command_mode_set_addr(psmouse, addr))
1748		return -1;
1749	return __alps_command_mode_read_reg(psmouse, addr);
1750}
1751
1752static int __alps_command_mode_write_reg(struct psmouse *psmouse, u8 value)
1753{
1754	if (alps_command_mode_send_nibble(psmouse, (value >> 4) & 0xf))
1755		return -1;
1756	if (alps_command_mode_send_nibble(psmouse, value & 0xf))
1757		return -1;
1758	return 0;
1759}
1760
1761static int alps_command_mode_write_reg(struct psmouse *psmouse, int addr,
1762				       u8 value)
1763{
1764	if (alps_command_mode_set_addr(psmouse, addr))
1765		return -1;
1766	return __alps_command_mode_write_reg(psmouse, value);
1767}
1768
1769static int alps_rpt_cmd(struct psmouse *psmouse, int init_command,
1770			int repeated_command, unsigned char *param)
1771{
1772	struct ps2dev *ps2dev = &psmouse->ps2dev;
1773
1774	param[0] = 0;
1775	if (init_command && ps2_command(ps2dev, param, init_command))
1776		return -EIO;
1777
1778	if (ps2_command(ps2dev,  NULL, repeated_command) ||
1779	    ps2_command(ps2dev,  NULL, repeated_command) ||
1780	    ps2_command(ps2dev,  NULL, repeated_command))
1781		return -EIO;
1782
1783	param[0] = param[1] = param[2] = 0xff;
1784	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1785		return -EIO;
1786
1787	psmouse_dbg(psmouse, "%2.2X report: %3ph\n",
1788		    repeated_command, param);
1789	return 0;
1790}
1791
1792static bool alps_check_valid_firmware_id(unsigned char id[])
1793{
1794	if (id[0] == 0x73)
1795		return true;
1796
1797	if (id[0] == 0x88 &&
1798	    (id[1] == 0x07 ||
1799	     id[1] == 0x08 ||
1800	     (id[1] & 0xf0) == 0xb0 ||
1801	     (id[1] & 0xf0) == 0xc0)) {
1802		return true;
1803	}
1804
1805	return false;
1806}
1807
1808static int alps_enter_command_mode(struct psmouse *psmouse)
1809{
1810	unsigned char param[4];
1811
1812	if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_RESET_WRAP, param)) {
1813		psmouse_err(psmouse, "failed to enter command mode\n");
1814		return -1;
1815	}
1816
1817	if (!alps_check_valid_firmware_id(param)) {
1818		psmouse_dbg(psmouse,
1819			    "unknown response while entering command mode\n");
1820		return -1;
1821	}
1822	return 0;
1823}
1824
1825static inline int alps_exit_command_mode(struct psmouse *psmouse)
1826{
1827	struct ps2dev *ps2dev = &psmouse->ps2dev;
1828	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM))
1829		return -1;
1830	return 0;
1831}
1832
1833/*
1834 * For DualPoint devices select the device that should respond to
1835 * subsequent commands. It looks like glidepad is behind stickpointer,
1836 * I'd thought it would be other way around...
1837 */
1838static int alps_passthrough_mode_v2(struct psmouse *psmouse, bool enable)
1839{
1840	struct ps2dev *ps2dev = &psmouse->ps2dev;
1841	int cmd = enable ? PSMOUSE_CMD_SETSCALE21 : PSMOUSE_CMD_SETSCALE11;
1842
1843	if (ps2_command(ps2dev, NULL, cmd) ||
1844	    ps2_command(ps2dev, NULL, cmd) ||
1845	    ps2_command(ps2dev, NULL, cmd) ||
1846	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1847		return -1;
1848
1849	/* we may get 3 more bytes, just ignore them */
1850	ps2_drain(ps2dev, 3, 100);
1851
1852	return 0;
1853}
1854
1855static int alps_absolute_mode_v1_v2(struct psmouse *psmouse)
1856{
1857	struct ps2dev *ps2dev = &psmouse->ps2dev;
1858
1859	/* Try ALPS magic knock - 4 disable before enable */
1860	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1861	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1862	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1863	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1864	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
1865		return -1;
1866
1867	/*
1868	 * Switch mouse to poll (remote) mode so motion data will not
1869	 * get in our way
1870	 */
1871	return ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL);
1872}
1873
1874static int alps_monitor_mode_send_word(struct psmouse *psmouse, u16 word)
1875{
1876	int i, nibble;
1877
1878	/*
1879	 * b0-b11 are valid bits, send sequence is inverse.
1880	 * e.g. when word = 0x0123, nibble send sequence is 3, 2, 1
1881	 */
1882	for (i = 0; i <= 8; i += 4) {
1883		nibble = (word >> i) & 0xf;
1884		if (alps_command_mode_send_nibble(psmouse, nibble))
1885			return -1;
1886	}
1887
1888	return 0;
1889}
1890
1891static int alps_monitor_mode_write_reg(struct psmouse *psmouse,
1892				       u16 addr, u16 value)
1893{
1894	struct ps2dev *ps2dev = &psmouse->ps2dev;
1895
1896	/* 0x0A0 is the command to write the word */
1897	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE) ||
1898	    alps_monitor_mode_send_word(psmouse, 0x0A0) ||
1899	    alps_monitor_mode_send_word(psmouse, addr) ||
1900	    alps_monitor_mode_send_word(psmouse, value) ||
1901	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1902		return -1;
1903
1904	return 0;
1905}
1906
1907static int alps_monitor_mode(struct psmouse *psmouse, bool enable)
1908{
1909	struct ps2dev *ps2dev = &psmouse->ps2dev;
1910
1911	if (enable) {
1912		/* EC E9 F5 F5 E7 E6 E7 E9 to enter monitor mode */
1913		if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
1914		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO) ||
1915		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1916		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1917		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1918		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1919		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1920		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO))
1921			return -1;
1922	} else {
1923		/* EC to exit monitor mode */
1924		if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP))
1925			return -1;
1926	}
1927
1928	return 0;
1929}
1930
1931static int alps_absolute_mode_v6(struct psmouse *psmouse)
1932{
1933	u16 reg_val = 0x181;
1934	int ret = -1;
1935
1936	/* enter monitor mode, to write the register */
1937	if (alps_monitor_mode(psmouse, true))
1938		return -1;
1939
1940	ret = alps_monitor_mode_write_reg(psmouse, 0x000, reg_val);
1941
1942	if (alps_monitor_mode(psmouse, false))
1943		ret = -1;
1944
1945	return ret;
1946}
1947
1948static int alps_get_status(struct psmouse *psmouse, char *param)
1949{
1950	/* Get status: 0xF5 0xF5 0xF5 0xE9 */
1951	if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_DISABLE, param))
1952		return -1;
1953
1954	return 0;
1955}
1956
1957/*
1958 * Turn touchpad tapping on or off. The sequences are:
1959 * 0xE9 0xF5 0xF5 0xF3 0x0A to enable,
1960 * 0xE9 0xF5 0xF5 0xE8 0x00 to disable.
1961 * My guess that 0xE9 (GetInfo) is here as a sync point.
1962 * For models that also have stickpointer (DualPoints) its tapping
1963 * is controlled separately (0xE6 0xE6 0xE6 0xF3 0x14|0x0A) but
1964 * we don't fiddle with it.
1965 */
1966static int alps_tap_mode(struct psmouse *psmouse, int enable)
1967{
1968	struct ps2dev *ps2dev = &psmouse->ps2dev;
1969	int cmd = enable ? PSMOUSE_CMD_SETRATE : PSMOUSE_CMD_SETRES;
1970	unsigned char tap_arg = enable ? 0x0A : 0x00;
1971	unsigned char param[4];
1972
1973	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO) ||
1974	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1975	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1976	    ps2_command(ps2dev, &tap_arg, cmd))
1977		return -1;
1978
1979	if (alps_get_status(psmouse, param))
1980		return -1;
1981
1982	return 0;
1983}
1984
1985/*
1986 * alps_poll() - poll the touchpad for current motion packet.
1987 * Used in resync.
1988 */
1989static int alps_poll(struct psmouse *psmouse)
1990{
1991	struct alps_data *priv = psmouse->private;
1992	unsigned char buf[sizeof(psmouse->packet)];
1993	bool poll_failed;
1994
1995	if (priv->flags & ALPS_PASS)
1996		alps_passthrough_mode_v2(psmouse, true);
1997
1998	poll_failed = ps2_command(&psmouse->ps2dev, buf,
1999				  PSMOUSE_CMD_POLL | (psmouse->pktsize << 8)) < 0;
2000
2001	if (priv->flags & ALPS_PASS)
2002		alps_passthrough_mode_v2(psmouse, false);
2003
2004	if (poll_failed || (buf[0] & priv->mask0) != priv->byte0)
2005		return -1;
2006
2007	if ((psmouse->badbyte & 0xc8) == 0x08) {
2008/*
2009 * Poll the track stick ...
2010 */
2011		if (ps2_command(&psmouse->ps2dev, buf, PSMOUSE_CMD_POLL | (3 << 8)))
2012			return -1;
2013	}
2014
2015	memcpy(psmouse->packet, buf, sizeof(buf));
2016	return 0;
2017}
2018
2019static int alps_hw_init_v1_v2(struct psmouse *psmouse)
2020{
2021	struct alps_data *priv = psmouse->private;
2022
2023	if ((priv->flags & ALPS_PASS) &&
2024	    alps_passthrough_mode_v2(psmouse, true)) {
2025		return -1;
2026	}
2027
2028	if (alps_tap_mode(psmouse, true)) {
2029		psmouse_warn(psmouse, "Failed to enable hardware tapping\n");
2030		return -1;
2031	}
2032
2033	if (alps_absolute_mode_v1_v2(psmouse)) {
2034		psmouse_err(psmouse, "Failed to enable absolute mode\n");
2035		return -1;
2036	}
2037
2038	if ((priv->flags & ALPS_PASS) &&
2039	    alps_passthrough_mode_v2(psmouse, false)) {
2040		return -1;
2041	}
2042
2043	/* ALPS needs stream mode, otherwise it won't report any data */
2044	if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)) {
2045		psmouse_err(psmouse, "Failed to enable stream mode\n");
2046		return -1;
2047	}
2048
2049	return 0;
2050}
2051
2052static int alps_hw_init_v6(struct psmouse *psmouse)
 
2053{
2054	unsigned char param[2] = {0xC8, 0x14};
2055
2056	/* Enter passthrough mode to let trackpoint enter 6byte raw mode */
2057	if (alps_passthrough_mode_v2(psmouse, true))
2058		return -1;
2059
2060	if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
2061	    ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
2062	    ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
2063	    ps2_command(&psmouse->ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2064	    ps2_command(&psmouse->ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
2065		return -1;
2066
 
 
 
 
 
 
 
 
 
 
 
 
 
2067	if (alps_passthrough_mode_v2(psmouse, false))
2068		return -1;
2069
 
 
 
2070	if (alps_absolute_mode_v6(psmouse)) {
2071		psmouse_err(psmouse, "Failed to enable absolute mode\n");
2072		return -1;
2073	}
2074
2075	return 0;
2076}
2077
2078/*
2079 * Enable or disable passthrough mode to the trackstick.
2080 */
2081static int alps_passthrough_mode_v3(struct psmouse *psmouse,
2082				    int reg_base, bool enable)
2083{
2084	int reg_val, ret = -1;
2085
2086	if (alps_enter_command_mode(psmouse))
2087		return -1;
2088
2089	reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x0008);
2090	if (reg_val == -1)
2091		goto error;
2092
2093	if (enable)
2094		reg_val |= 0x01;
2095	else
2096		reg_val &= ~0x01;
2097
2098	ret = __alps_command_mode_write_reg(psmouse, reg_val);
2099
2100error:
2101	if (alps_exit_command_mode(psmouse))
2102		ret = -1;
2103	return ret;
2104}
2105
2106/* Must be in command mode when calling this function */
2107static int alps_absolute_mode_v3(struct psmouse *psmouse)
2108{
2109	int reg_val;
2110
2111	reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
2112	if (reg_val == -1)
2113		return -1;
2114
2115	reg_val |= 0x06;
2116	if (__alps_command_mode_write_reg(psmouse, reg_val))
2117		return -1;
2118
2119	return 0;
2120}
2121
2122static int alps_probe_trackstick_v3_v7(struct psmouse *psmouse, int reg_base)
2123{
2124	int ret = -EIO, reg_val;
2125
2126	if (alps_enter_command_mode(psmouse))
2127		goto error;
2128
2129	reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x08);
2130	if (reg_val == -1)
2131		goto error;
2132
2133	/* bit 7: trackstick is present */
2134	ret = reg_val & 0x80 ? 0 : -ENODEV;
2135
2136error:
2137	alps_exit_command_mode(psmouse);
2138	return ret;
2139}
2140
2141static int alps_setup_trackstick_v3(struct psmouse *psmouse, int reg_base)
2142{
2143	struct ps2dev *ps2dev = &psmouse->ps2dev;
2144	int ret = 0;
 
2145	unsigned char param[4];
2146
 
 
 
 
 
 
 
 
2147	if (alps_passthrough_mode_v3(psmouse, reg_base, true))
2148		return -EIO;
2149
2150	/*
2151	 * E7 report for the trackstick
2152	 *
2153	 * There have been reports of failures to seem to trace back
2154	 * to the above trackstick check failing. When these occur
2155	 * this E7 report fails, so when that happens we continue
2156	 * with the assumption that there isn't a trackstick after
2157	 * all.
2158	 */
2159	if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_SETSCALE21, param)) {
2160		psmouse_warn(psmouse, "Failed to initialize trackstick (E7 report failed)\n");
2161		ret = -ENODEV;
2162	} else {
2163		psmouse_dbg(psmouse, "trackstick E7 report: %3ph\n", param);
2164
2165		/*
2166		 * Not sure what this does, but it is absolutely
2167		 * essential. Without it, the touchpad does not
2168		 * work at all and the trackstick just emits normal
2169		 * PS/2 packets.
2170		 */
2171		if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
2172		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
2173		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
2174		    alps_command_mode_send_nibble(psmouse, 0x9) ||
2175		    alps_command_mode_send_nibble(psmouse, 0x4)) {
2176			psmouse_err(psmouse,
2177				    "Error sending magic E6 sequence\n");
2178			ret = -EIO;
2179			goto error;
2180		}
 
 
 
 
 
 
 
 
 
 
2181
 
 
 
 
2182		/*
2183		 * This ensures the trackstick packets are in the format
2184		 * supported by this driver. If bit 1 isn't set the packet
2185		 * format is different.
2186		 */
2187		if (alps_enter_command_mode(psmouse) ||
2188		    alps_command_mode_write_reg(psmouse,
2189						reg_base + 0x08, 0x82) ||
2190		    alps_exit_command_mode(psmouse))
2191			ret = -EIO;
2192	}
2193
2194error:
2195	if (alps_passthrough_mode_v3(psmouse, reg_base, false))
2196		ret = -EIO;
2197
2198	return ret;
2199}
2200
2201static int alps_hw_init_v3(struct psmouse *psmouse)
2202{
2203	struct alps_data *priv = psmouse->private;
2204	struct ps2dev *ps2dev = &psmouse->ps2dev;
2205	int reg_val;
2206	unsigned char param[4];
2207
2208	if ((priv->flags & ALPS_DUALPOINT) &&
2209	    alps_setup_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE) == -EIO)
2210		goto error;
2211
2212	if (alps_enter_command_mode(psmouse) ||
2213	    alps_absolute_mode_v3(psmouse)) {
2214		psmouse_err(psmouse, "Failed to enter absolute mode\n");
2215		goto error;
2216	}
2217
2218	reg_val = alps_command_mode_read_reg(psmouse, 0x0006);
2219	if (reg_val == -1)
2220		goto error;
2221	if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
2222		goto error;
2223
2224	reg_val = alps_command_mode_read_reg(psmouse, 0x0007);
2225	if (reg_val == -1)
2226		goto error;
2227	if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
2228		goto error;
2229
2230	if (alps_command_mode_read_reg(psmouse, 0x0144) == -1)
2231		goto error;
2232	if (__alps_command_mode_write_reg(psmouse, 0x04))
2233		goto error;
2234
2235	if (alps_command_mode_read_reg(psmouse, 0x0159) == -1)
2236		goto error;
2237	if (__alps_command_mode_write_reg(psmouse, 0x03))
2238		goto error;
2239
2240	if (alps_command_mode_read_reg(psmouse, 0x0163) == -1)
2241		goto error;
2242	if (alps_command_mode_write_reg(psmouse, 0x0163, 0x03))
2243		goto error;
2244
2245	if (alps_command_mode_read_reg(psmouse, 0x0162) == -1)
2246		goto error;
2247	if (alps_command_mode_write_reg(psmouse, 0x0162, 0x04))
2248		goto error;
2249
2250	alps_exit_command_mode(psmouse);
2251
2252	/* Set rate and enable data reporting */
2253	param[0] = 0x64;
2254	if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
2255	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
2256		psmouse_err(psmouse, "Failed to enable data reporting\n");
2257		return -1;
2258	}
2259
2260	return 0;
2261
2262error:
2263	/*
2264	 * Leaving the touchpad in command mode will essentially render
2265	 * it unusable until the machine reboots, so exit it here just
2266	 * to be safe
2267	 */
2268	alps_exit_command_mode(psmouse);
2269	return -1;
2270}
2271
2272static int alps_get_v3_v7_resolution(struct psmouse *psmouse, int reg_pitch)
2273{
2274	int reg, x_pitch, y_pitch, x_electrode, y_electrode, x_phys, y_phys;
2275	struct alps_data *priv = psmouse->private;
2276
2277	reg = alps_command_mode_read_reg(psmouse, reg_pitch);
2278	if (reg < 0)
2279		return reg;
2280
2281	x_pitch = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
2282	x_pitch = 50 + 2 * x_pitch; /* In 0.1 mm units */
2283
2284	y_pitch = (char)reg >> 4; /* sign extend upper 4 bits */
2285	y_pitch = 36 + 2 * y_pitch; /* In 0.1 mm units */
2286
2287	reg = alps_command_mode_read_reg(psmouse, reg_pitch + 1);
2288	if (reg < 0)
2289		return reg;
2290
2291	x_electrode = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
2292	x_electrode = 17 + x_electrode;
2293
2294	y_electrode = (char)reg >> 4; /* sign extend upper 4 bits */
2295	y_electrode = 13 + y_electrode;
2296
2297	x_phys = x_pitch * (x_electrode - 1); /* In 0.1 mm units */
2298	y_phys = y_pitch * (y_electrode - 1); /* In 0.1 mm units */
2299
2300	priv->x_res = priv->x_max * 10 / x_phys; /* units / mm */
2301	priv->y_res = priv->y_max * 10 / y_phys; /* units / mm */
2302
2303	psmouse_dbg(psmouse,
2304		    "pitch %dx%d num-electrodes %dx%d physical size %dx%d mm res %dx%d\n",
2305		    x_pitch, y_pitch, x_electrode, y_electrode,
2306		    x_phys / 10, y_phys / 10, priv->x_res, priv->y_res);
2307
2308	return 0;
2309}
2310
2311static int alps_hw_init_rushmore_v3(struct psmouse *psmouse)
2312{
2313	struct alps_data *priv = psmouse->private;
2314	struct ps2dev *ps2dev = &psmouse->ps2dev;
2315	int reg_val, ret = -1;
2316
2317	if (priv->flags & ALPS_DUALPOINT) {
2318		reg_val = alps_setup_trackstick_v3(psmouse,
2319						   ALPS_REG_BASE_RUSHMORE);
2320		if (reg_val == -EIO)
2321			goto error;
2322	}
2323
2324	if (alps_enter_command_mode(psmouse) ||
2325	    alps_command_mode_read_reg(psmouse, 0xc2d9) == -1 ||
2326	    alps_command_mode_write_reg(psmouse, 0xc2cb, 0x00))
2327		goto error;
2328
2329	if (alps_get_v3_v7_resolution(psmouse, 0xc2da))
2330		goto error;
2331
2332	reg_val = alps_command_mode_read_reg(psmouse, 0xc2c6);
2333	if (reg_val == -1)
2334		goto error;
2335	if (__alps_command_mode_write_reg(psmouse, reg_val & 0xfd))
2336		goto error;
2337
2338	if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
2339		goto error;
2340
2341	/* enter absolute mode */
2342	reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
2343	if (reg_val == -1)
2344		goto error;
2345	if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
2346		goto error;
2347
2348	alps_exit_command_mode(psmouse);
2349	return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
2350
2351error:
2352	alps_exit_command_mode(psmouse);
2353	return ret;
2354}
2355
2356/* Must be in command mode when calling this function */
2357static int alps_absolute_mode_v4(struct psmouse *psmouse)
2358{
2359	int reg_val;
2360
2361	reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
2362	if (reg_val == -1)
2363		return -1;
2364
2365	reg_val |= 0x02;
2366	if (__alps_command_mode_write_reg(psmouse, reg_val))
2367		return -1;
2368
2369	return 0;
2370}
2371
2372static int alps_hw_init_v4(struct psmouse *psmouse)
2373{
2374	struct ps2dev *ps2dev = &psmouse->ps2dev;
2375	unsigned char param[4];
2376
2377	if (alps_enter_command_mode(psmouse))
2378		goto error;
2379
2380	if (alps_absolute_mode_v4(psmouse)) {
2381		psmouse_err(psmouse, "Failed to enter absolute mode\n");
2382		goto error;
2383	}
2384
2385	if (alps_command_mode_write_reg(psmouse, 0x0007, 0x8c))
2386		goto error;
2387
2388	if (alps_command_mode_write_reg(psmouse, 0x0149, 0x03))
2389		goto error;
2390
2391	if (alps_command_mode_write_reg(psmouse, 0x0160, 0x03))
2392		goto error;
2393
2394	if (alps_command_mode_write_reg(psmouse, 0x017f, 0x15))
2395		goto error;
2396
2397	if (alps_command_mode_write_reg(psmouse, 0x0151, 0x01))
2398		goto error;
2399
2400	if (alps_command_mode_write_reg(psmouse, 0x0168, 0x03))
2401		goto error;
2402
2403	if (alps_command_mode_write_reg(psmouse, 0x014a, 0x03))
2404		goto error;
2405
2406	if (alps_command_mode_write_reg(psmouse, 0x0161, 0x03))
2407		goto error;
2408
2409	alps_exit_command_mode(psmouse);
2410
2411	/*
2412	 * This sequence changes the output from a 9-byte to an
2413	 * 8-byte format. All the same data seems to be present,
2414	 * just in a more compact format.
2415	 */
2416	param[0] = 0xc8;
2417	param[1] = 0x64;
2418	param[2] = 0x50;
2419	if (ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2420	    ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE) ||
2421	    ps2_command(ps2dev, &param[2], PSMOUSE_CMD_SETRATE) ||
2422	    ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
2423		return -1;
2424
2425	/* Set rate and enable data reporting */
2426	param[0] = 0x64;
2427	if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
2428	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
2429		psmouse_err(psmouse, "Failed to enable data reporting\n");
2430		return -1;
2431	}
2432
2433	return 0;
2434
2435error:
2436	/*
2437	 * Leaving the touchpad in command mode will essentially render
2438	 * it unusable until the machine reboots, so exit it here just
2439	 * to be safe
2440	 */
2441	alps_exit_command_mode(psmouse);
2442	return -1;
2443}
2444
2445static int alps_get_otp_values_ss4_v2(struct psmouse *psmouse,
2446				      unsigned char index, unsigned char otp[])
2447{
2448	struct ps2dev *ps2dev = &psmouse->ps2dev;
2449
2450	switch (index) {
2451	case 0:
2452		if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)  ||
2453		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)  ||
2454		    ps2_command(ps2dev, otp, PSMOUSE_CMD_GETINFO))
2455			return -1;
2456
2457		break;
2458
2459	case 1:
2460		if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL)  ||
2461		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL)  ||
2462		    ps2_command(ps2dev, otp, PSMOUSE_CMD_GETINFO))
2463			return -1;
2464
2465		break;
2466	}
2467
2468	return 0;
2469}
2470
2471static int alps_update_device_area_ss4_v2(unsigned char otp[][4],
2472					  struct alps_data *priv)
2473{
2474	int num_x_electrode;
2475	int num_y_electrode;
2476	int x_pitch, y_pitch, x_phys, y_phys;
2477
2478	if (IS_SS4PLUS_DEV(priv->dev_id)) {
2479		num_x_electrode =
2480			SS4PLUS_NUMSENSOR_XOFFSET + (otp[0][2] & 0x0F);
2481		num_y_electrode =
2482			SS4PLUS_NUMSENSOR_YOFFSET + ((otp[0][2] >> 4) & 0x0F);
2483
2484		priv->x_max =
2485			(num_x_electrode - 1) * SS4PLUS_COUNT_PER_ELECTRODE;
2486		priv->y_max =
2487			(num_y_electrode - 1) * SS4PLUS_COUNT_PER_ELECTRODE;
2488
2489		x_pitch = (otp[0][1] & 0x0F) + SS4PLUS_MIN_PITCH_MM;
2490		y_pitch = ((otp[0][1] >> 4) & 0x0F) + SS4PLUS_MIN_PITCH_MM;
2491
2492	} else {
2493		num_x_electrode =
2494			SS4_NUMSENSOR_XOFFSET + (otp[1][0] & 0x0F);
2495		num_y_electrode =
2496			SS4_NUMSENSOR_YOFFSET + ((otp[1][0] >> 4) & 0x0F);
2497
2498		priv->x_max =
2499			(num_x_electrode - 1) * SS4_COUNT_PER_ELECTRODE;
2500		priv->y_max =
2501			(num_y_electrode - 1) * SS4_COUNT_PER_ELECTRODE;
2502
2503		x_pitch = ((otp[1][2] >> 2) & 0x07) + SS4_MIN_PITCH_MM;
2504		y_pitch = ((otp[1][2] >> 5) & 0x07) + SS4_MIN_PITCH_MM;
2505	}
2506
2507	x_phys = x_pitch * (num_x_electrode - 1); /* In 0.1 mm units */
2508	y_phys = y_pitch * (num_y_electrode - 1); /* In 0.1 mm units */
2509
2510	priv->x_res = priv->x_max * 10 / x_phys; /* units / mm */
2511	priv->y_res = priv->y_max * 10 / y_phys; /* units / mm */
2512
2513	return 0;
2514}
2515
2516static int alps_update_btn_info_ss4_v2(unsigned char otp[][4],
2517				       struct alps_data *priv)
2518{
2519	unsigned char is_btnless;
2520
2521	if (IS_SS4PLUS_DEV(priv->dev_id))
2522		is_btnless = (otp[1][0] >> 1) & 0x01;
2523	else
2524		is_btnless = (otp[1][1] >> 3) & 0x01;
2525
2526	if (is_btnless)
2527		priv->flags |= ALPS_BUTTONPAD;
2528
2529	return 0;
2530}
2531
2532static int alps_update_dual_info_ss4_v2(unsigned char otp[][4],
2533					struct alps_data *priv,
2534					struct psmouse *psmouse)
2535{
2536	bool is_dual = false;
2537	int reg_val = 0;
2538	struct ps2dev *ps2dev = &psmouse->ps2dev;
2539
2540	if (IS_SS4PLUS_DEV(priv->dev_id)) {
2541		is_dual = (otp[0][0] >> 4) & 0x01;
2542
2543		if (!is_dual) {
2544			/* For support TrackStick of Thinkpad L/E series */
2545			if (alps_exit_command_mode(psmouse) == 0 &&
2546				alps_enter_command_mode(psmouse) == 0) {
2547				reg_val = alps_command_mode_read_reg(psmouse,
2548									0xD7);
2549			}
2550			alps_exit_command_mode(psmouse);
2551			ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
2552
2553			if (reg_val == 0x0C || reg_val == 0x1D)
2554				is_dual = true;
2555		}
2556	}
2557
2558	if (is_dual)
2559		priv->flags |= ALPS_DUALPOINT |
2560					ALPS_DUALPOINT_WITH_PRESSURE;
2561
2562	return 0;
2563}
2564
2565static int alps_set_defaults_ss4_v2(struct psmouse *psmouse,
2566				    struct alps_data *priv)
2567{
2568	unsigned char otp[2][4];
2569
2570	memset(otp, 0, sizeof(otp));
2571
2572	if (alps_get_otp_values_ss4_v2(psmouse, 1, &otp[1][0]) ||
2573	    alps_get_otp_values_ss4_v2(psmouse, 0, &otp[0][0]))
2574		return -1;
2575
2576	alps_update_device_area_ss4_v2(otp, priv);
2577
2578	alps_update_btn_info_ss4_v2(otp, priv);
2579
2580	alps_update_dual_info_ss4_v2(otp, priv, psmouse);
2581
2582	return 0;
2583}
2584
2585static int alps_dolphin_get_device_area(struct psmouse *psmouse,
2586					struct alps_data *priv)
2587{
2588	struct ps2dev *ps2dev = &psmouse->ps2dev;
2589	unsigned char param[4] = {0};
2590	int num_x_electrode, num_y_electrode;
2591
2592	if (alps_enter_command_mode(psmouse))
2593		return -1;
2594
2595	param[0] = 0x0a;
2596	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
2597	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2598	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2599	    ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2600	    ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE))
2601		return -1;
2602
2603	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
2604		return -1;
2605
2606	/*
2607	 * Dolphin's sensor line number is not fixed. It can be calculated
2608	 * by adding the device's register value with DOLPHIN_PROFILE_X/YOFFSET.
2609	 * Further more, we can get device's x_max and y_max by multiplying
2610	 * sensor line number with DOLPHIN_COUNT_PER_ELECTRODE.
2611	 *
2612	 * e.g. When we get register's sensor_x = 11 & sensor_y = 8,
2613	 *	real sensor line number X = 11 + 8 = 19, and
2614	 *	real sensor line number Y = 8 + 1 = 9.
2615	 *	So, x_max = (19 - 1) * 64 = 1152, and
2616	 *	    y_max = (9 - 1) * 64 = 512.
2617	 */
2618	num_x_electrode = DOLPHIN_PROFILE_XOFFSET + (param[2] & 0x0F);
2619	num_y_electrode = DOLPHIN_PROFILE_YOFFSET + ((param[2] >> 4) & 0x0F);
2620	priv->x_bits = num_x_electrode;
2621	priv->y_bits = num_y_electrode;
2622	priv->x_max = (num_x_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
2623	priv->y_max = (num_y_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
2624
2625	if (alps_exit_command_mode(psmouse))
2626		return -1;
2627
2628	return 0;
2629}
2630
2631static int alps_hw_init_dolphin_v1(struct psmouse *psmouse)
2632{
2633	struct ps2dev *ps2dev = &psmouse->ps2dev;
2634	unsigned char param[2];
2635
2636	/* This is dolphin "v1" as empirically defined by florin9doi */
2637	param[0] = 0x64;
2638	param[1] = 0x28;
2639
2640	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2641	    ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2642	    ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
2643		return -1;
2644
2645	return 0;
2646}
2647
2648static int alps_hw_init_v7(struct psmouse *psmouse)
2649{
2650	struct ps2dev *ps2dev = &psmouse->ps2dev;
2651	int reg_val, ret = -1;
2652
2653	if (alps_enter_command_mode(psmouse) ||
2654	    alps_command_mode_read_reg(psmouse, 0xc2d9) == -1)
2655		goto error;
2656
2657	if (alps_get_v3_v7_resolution(psmouse, 0xc397))
2658		goto error;
2659
2660	if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
2661		goto error;
2662
2663	reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
2664	if (reg_val == -1)
2665		goto error;
2666	if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
2667		goto error;
2668
2669	alps_exit_command_mode(psmouse);
2670	return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
2671
2672error:
2673	alps_exit_command_mode(psmouse);
2674	return ret;
2675}
2676
2677static int alps_hw_init_ss4_v2(struct psmouse *psmouse)
2678{
2679	struct ps2dev *ps2dev = &psmouse->ps2dev;
2680	char param[2] = {0x64, 0x28};
2681	int ret = -1;
2682
2683	/* enter absolute mode */
2684	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2685	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2686	    ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2687	    ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE)) {
2688		goto error;
2689	}
2690
2691	/* T.B.D. Decread noise packet number, delete in the future */
2692	if (alps_exit_command_mode(psmouse) ||
2693	    alps_enter_command_mode(psmouse) ||
2694	    alps_command_mode_write_reg(psmouse, 0x001D, 0x20)) {
2695		goto error;
2696	}
2697	alps_exit_command_mode(psmouse);
2698
2699	return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
2700
2701error:
2702	alps_exit_command_mode(psmouse);
2703	return ret;
2704}
2705
2706static int alps_set_protocol(struct psmouse *psmouse,
2707			     struct alps_data *priv,
2708			     const struct alps_protocol_info *protocol)
2709{
2710	psmouse->private = priv;
2711
2712	timer_setup(&priv->timer, alps_flush_packet, 0);
2713
2714	priv->proto_version = protocol->version;
2715	priv->byte0 = protocol->byte0;
2716	priv->mask0 = protocol->mask0;
2717	priv->flags = protocol->flags;
2718
2719	priv->x_max = 2000;
2720	priv->y_max = 1400;
2721	priv->x_bits = 15;
2722	priv->y_bits = 11;
2723
2724	switch (priv->proto_version) {
2725	case ALPS_PROTO_V1:
2726	case ALPS_PROTO_V2:
2727		priv->hw_init = alps_hw_init_v1_v2;
2728		priv->process_packet = alps_process_packet_v1_v2;
2729		priv->set_abs_params = alps_set_abs_params_st;
2730		priv->x_max = 1023;
2731		priv->y_max = 767;
2732		if (dmi_check_system(alps_dmi_has_separate_stick_buttons))
2733			priv->flags |= ALPS_STICK_BITS;
2734		break;
2735
2736	case ALPS_PROTO_V3:
2737		priv->hw_init = alps_hw_init_v3;
2738		priv->process_packet = alps_process_packet_v3;
2739		priv->set_abs_params = alps_set_abs_params_semi_mt;
2740		priv->decode_fields = alps_decode_pinnacle;
2741		priv->nibble_commands = alps_v3_nibble_commands;
2742		priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2743
2744		if (alps_probe_trackstick_v3_v7(psmouse,
2745						ALPS_REG_BASE_PINNACLE) < 0)
2746			priv->flags &= ~ALPS_DUALPOINT;
2747
2748		break;
2749
2750	case ALPS_PROTO_V3_RUSHMORE:
2751		priv->hw_init = alps_hw_init_rushmore_v3;
2752		priv->process_packet = alps_process_packet_v3;
2753		priv->set_abs_params = alps_set_abs_params_semi_mt;
2754		priv->decode_fields = alps_decode_rushmore;
2755		priv->nibble_commands = alps_v3_nibble_commands;
2756		priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2757		priv->x_bits = 16;
2758		priv->y_bits = 12;
2759
2760		if (alps_probe_trackstick_v3_v7(psmouse,
2761						ALPS_REG_BASE_RUSHMORE) < 0)
2762			priv->flags &= ~ALPS_DUALPOINT;
2763
2764		break;
2765
2766	case ALPS_PROTO_V4:
2767		priv->hw_init = alps_hw_init_v4;
2768		priv->process_packet = alps_process_packet_v4;
2769		priv->set_abs_params = alps_set_abs_params_semi_mt;
2770		priv->nibble_commands = alps_v4_nibble_commands;
2771		priv->addr_command = PSMOUSE_CMD_DISABLE;
2772		break;
2773
2774	case ALPS_PROTO_V5:
2775		priv->hw_init = alps_hw_init_dolphin_v1;
2776		priv->process_packet = alps_process_touchpad_packet_v3_v5;
2777		priv->decode_fields = alps_decode_dolphin;
2778		priv->set_abs_params = alps_set_abs_params_semi_mt;
2779		priv->nibble_commands = alps_v3_nibble_commands;
2780		priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2781		priv->x_bits = 23;
2782		priv->y_bits = 12;
2783
2784		if (alps_dolphin_get_device_area(psmouse, priv))
2785			return -EIO;
2786
2787		break;
2788
2789	case ALPS_PROTO_V6:
2790		priv->hw_init = alps_hw_init_v6;
2791		priv->process_packet = alps_process_packet_v6;
2792		priv->set_abs_params = alps_set_abs_params_st;
2793		priv->nibble_commands = alps_v6_nibble_commands;
2794		priv->x_max = 2047;
2795		priv->y_max = 1535;
2796		break;
2797
2798	case ALPS_PROTO_V7:
2799		priv->hw_init = alps_hw_init_v7;
2800		priv->process_packet = alps_process_packet_v7;
2801		priv->decode_fields = alps_decode_packet_v7;
2802		priv->set_abs_params = alps_set_abs_params_v7;
2803		priv->nibble_commands = alps_v3_nibble_commands;
2804		priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2805		priv->x_max = 0xfff;
2806		priv->y_max = 0x7ff;
2807
2808		if (priv->fw_ver[1] != 0xba)
2809			priv->flags |= ALPS_BUTTONPAD;
2810
2811		if (alps_probe_trackstick_v3_v7(psmouse, ALPS_REG_BASE_V7) < 0)
2812			priv->flags &= ~ALPS_DUALPOINT;
2813
2814		break;
2815
2816	case ALPS_PROTO_V8:
2817		priv->hw_init = alps_hw_init_ss4_v2;
2818		priv->process_packet = alps_process_packet_ss4_v2;
2819		priv->decode_fields = alps_decode_ss4_v2;
2820		priv->set_abs_params = alps_set_abs_params_ss4_v2;
2821		priv->nibble_commands = alps_v3_nibble_commands;
2822		priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2823
2824		if (alps_set_defaults_ss4_v2(psmouse, priv))
2825			return -EIO;
2826
2827		break;
2828	}
2829
2830	return 0;
2831}
2832
2833static const struct alps_protocol_info *alps_match_table(unsigned char *e7,
2834							 unsigned char *ec)
2835{
2836	const struct alps_model_info *model;
2837	int i;
2838
2839	for (i = 0; i < ARRAY_SIZE(alps_model_data); i++) {
2840		model = &alps_model_data[i];
2841
2842		if (!memcmp(e7, model->signature, sizeof(model->signature)))
2843			return &model->protocol_info;
2844	}
2845
2846	return NULL;
2847}
2848
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2849static int alps_identify(struct psmouse *psmouse, struct alps_data *priv)
2850{
2851	const struct alps_protocol_info *protocol;
2852	unsigned char e6[4], e7[4], ec[4];
2853	int error;
2854
2855	/*
2856	 * First try "E6 report".
2857	 * ALPS should return 0,0,10 or 0,0,100 if no buttons are pressed.
2858	 * The bits 0-2 of the first byte will be 1s if some buttons are
2859	 * pressed.
2860	 */
2861	if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2862			 PSMOUSE_CMD_SETSCALE11, e6))
2863		return -EIO;
2864
2865	if ((e6[0] & 0xf8) != 0 || e6[1] != 0 || (e6[2] != 10 && e6[2] != 100))
2866		return -EINVAL;
2867
2868	/*
2869	 * Now get the "E7" and "EC" reports.  These will uniquely identify
2870	 * most ALPS touchpads.
2871	 */
2872	if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2873			 PSMOUSE_CMD_SETSCALE21, e7) ||
2874	    alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2875			 PSMOUSE_CMD_RESET_WRAP, ec) ||
2876	    alps_exit_command_mode(psmouse))
2877		return -EIO;
2878
2879	protocol = alps_match_table(e7, ec);
2880	if (!protocol) {
2881		if (e7[0] == 0x73 && e7[1] == 0x02 && e7[2] == 0x64 &&
2882			   ec[2] == 0x8a) {
2883			protocol = &alps_v4_protocol_data;
2884		} else if (e7[0] == 0x73 && e7[1] == 0x03 && e7[2] == 0x50 &&
2885			   ec[0] == 0x73 && (ec[1] == 0x01 || ec[1] == 0x02)) {
2886			protocol = &alps_v5_protocol_data;
2887		} else if (ec[0] == 0x88 &&
2888			   ((ec[1] & 0xf0) == 0xb0 || (ec[1] & 0xf0) == 0xc0)) {
2889			protocol = &alps_v7_protocol_data;
2890		} else if (ec[0] == 0x88 && ec[1] == 0x08) {
2891			protocol = &alps_v3_rushmore_data;
2892		} else if (ec[0] == 0x88 && ec[1] == 0x07 &&
2893			   ec[2] >= 0x90 && ec[2] <= 0x9d) {
2894			protocol = &alps_v3_protocol_data;
2895		} else if (e7[0] == 0x73 && e7[1] == 0x03 &&
2896			   (e7[2] == 0x14 || e7[2] == 0x28)) {
2897			protocol = &alps_v8_protocol_data;
2898		} else if (e7[0] == 0x73 && e7[1] == 0x03 && e7[2] == 0xc8) {
2899			protocol = &alps_v9_protocol_data;
2900			psmouse_warn(psmouse,
2901				     "Unsupported ALPS V9 touchpad: E7=%3ph, EC=%3ph\n",
2902				     e7, ec);
2903			return -EINVAL;
2904		} else {
2905			psmouse_dbg(psmouse,
2906				    "Likely not an ALPS touchpad: E7=%3ph, EC=%3ph\n", e7, ec);
2907			return -EINVAL;
2908		}
2909	}
2910
2911	if (priv) {
2912		/* Save Device ID and Firmware version */
2913		memcpy(priv->dev_id, e7, 3);
2914		memcpy(priv->fw_ver, ec, 3);
2915		error = alps_set_protocol(psmouse, priv, protocol);
2916		if (error)
2917			return error;
2918	}
2919
2920	return 0;
2921}
2922
2923static int alps_reconnect(struct psmouse *psmouse)
2924{
2925	struct alps_data *priv = psmouse->private;
2926
2927	psmouse_reset(psmouse);
2928
2929	if (alps_identify(psmouse, priv) < 0)
2930		return -1;
2931
2932	return priv->hw_init(psmouse);
2933}
2934
2935static void alps_disconnect(struct psmouse *psmouse)
2936{
2937	struct alps_data *priv = psmouse->private;
2938
2939	psmouse_reset(psmouse);
2940	del_timer_sync(&priv->timer);
2941	if (priv->dev2)
2942		input_unregister_device(priv->dev2);
2943	if (!IS_ERR_OR_NULL(priv->dev3))
2944		input_unregister_device(priv->dev3);
2945	kfree(priv);
2946}
2947
2948static void alps_set_abs_params_st(struct alps_data *priv,
2949				   struct input_dev *dev1)
2950{
2951	input_set_abs_params(dev1, ABS_X, 0, priv->x_max, 0, 0);
2952	input_set_abs_params(dev1, ABS_Y, 0, priv->y_max, 0, 0);
2953	input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
2954}
2955
2956static void alps_set_abs_params_mt_common(struct alps_data *priv,
2957					  struct input_dev *dev1)
2958{
2959	input_set_abs_params(dev1, ABS_MT_POSITION_X, 0, priv->x_max, 0, 0);
2960	input_set_abs_params(dev1, ABS_MT_POSITION_Y, 0, priv->y_max, 0, 0);
2961
2962	input_abs_set_res(dev1, ABS_MT_POSITION_X, priv->x_res);
2963	input_abs_set_res(dev1, ABS_MT_POSITION_Y, priv->y_res);
2964
2965	set_bit(BTN_TOOL_TRIPLETAP, dev1->keybit);
2966	set_bit(BTN_TOOL_QUADTAP, dev1->keybit);
2967}
2968
2969static void alps_set_abs_params_semi_mt(struct alps_data *priv,
2970					struct input_dev *dev1)
2971{
2972	alps_set_abs_params_mt_common(priv, dev1);
2973	input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
2974
2975	input_mt_init_slots(dev1, MAX_TOUCHES,
2976			    INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
2977				INPUT_MT_SEMI_MT);
2978}
2979
2980static void alps_set_abs_params_v7(struct alps_data *priv,
2981				   struct input_dev *dev1)
2982{
2983	alps_set_abs_params_mt_common(priv, dev1);
2984	set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
2985
2986	input_mt_init_slots(dev1, MAX_TOUCHES,
2987			    INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
2988				INPUT_MT_TRACK);
2989
2990	set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
2991}
2992
2993static void alps_set_abs_params_ss4_v2(struct alps_data *priv,
2994				       struct input_dev *dev1)
2995{
2996	alps_set_abs_params_mt_common(priv, dev1);
2997	input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
2998	set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
2999
3000	input_mt_init_slots(dev1, MAX_TOUCHES,
3001			    INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
3002				INPUT_MT_TRACK);
3003}
3004
3005int alps_init(struct psmouse *psmouse)
3006{
3007	struct alps_data *priv = psmouse->private;
3008	struct input_dev *dev1 = psmouse->dev;
3009	int error;
3010
3011	error = priv->hw_init(psmouse);
3012	if (error)
3013		goto init_fail;
3014
3015	/*
3016	 * Undo part of setup done for us by psmouse core since touchpad
3017	 * is not a relative device.
3018	 */
3019	__clear_bit(EV_REL, dev1->evbit);
3020	__clear_bit(REL_X, dev1->relbit);
3021	__clear_bit(REL_Y, dev1->relbit);
3022
3023	/*
3024	 * Now set up our capabilities.
3025	 */
3026	dev1->evbit[BIT_WORD(EV_KEY)] |= BIT_MASK(EV_KEY);
3027	dev1->keybit[BIT_WORD(BTN_TOUCH)] |= BIT_MASK(BTN_TOUCH);
3028	dev1->keybit[BIT_WORD(BTN_TOOL_FINGER)] |= BIT_MASK(BTN_TOOL_FINGER);
3029	dev1->keybit[BIT_WORD(BTN_LEFT)] |=
3030		BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
3031
3032	dev1->evbit[BIT_WORD(EV_ABS)] |= BIT_MASK(EV_ABS);
3033
3034	priv->set_abs_params(priv, dev1);
3035
3036	if (priv->flags & ALPS_WHEEL) {
3037		dev1->evbit[BIT_WORD(EV_REL)] |= BIT_MASK(EV_REL);
3038		dev1->relbit[BIT_WORD(REL_WHEEL)] |= BIT_MASK(REL_WHEEL);
3039	}
3040
3041	if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
3042		dev1->keybit[BIT_WORD(BTN_FORWARD)] |= BIT_MASK(BTN_FORWARD);
3043		dev1->keybit[BIT_WORD(BTN_BACK)] |= BIT_MASK(BTN_BACK);
3044	}
3045
3046	if (priv->flags & ALPS_FOUR_BUTTONS) {
3047		dev1->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_0);
3048		dev1->keybit[BIT_WORD(BTN_1)] |= BIT_MASK(BTN_1);
3049		dev1->keybit[BIT_WORD(BTN_2)] |= BIT_MASK(BTN_2);
3050		dev1->keybit[BIT_WORD(BTN_3)] |= BIT_MASK(BTN_3);
3051	} else if (priv->flags & ALPS_BUTTONPAD) {
3052		set_bit(INPUT_PROP_BUTTONPAD, dev1->propbit);
3053		clear_bit(BTN_RIGHT, dev1->keybit);
3054	} else {
3055		dev1->keybit[BIT_WORD(BTN_MIDDLE)] |= BIT_MASK(BTN_MIDDLE);
3056	}
3057
3058	if (priv->flags & ALPS_DUALPOINT) {
3059		struct input_dev *dev2;
3060
3061		dev2 = input_allocate_device();
3062		if (!dev2) {
3063			psmouse_err(psmouse,
3064				    "failed to allocate trackstick device\n");
3065			error = -ENOMEM;
3066			goto init_fail;
3067		}
3068
3069		snprintf(priv->phys2, sizeof(priv->phys2), "%s/input1",
3070			 psmouse->ps2dev.serio->phys);
3071		dev2->phys = priv->phys2;
3072
3073		/*
3074		 * format of input device name is: "protocol vendor name"
3075		 * see function psmouse_switch_protocol() in psmouse-base.c
3076		 */
3077		dev2->name = "AlpsPS/2 ALPS DualPoint Stick";
3078
3079		dev2->id.bustype = BUS_I8042;
3080		dev2->id.vendor  = 0x0002;
3081		dev2->id.product = PSMOUSE_ALPS;
3082		dev2->id.version = priv->proto_version;
3083		dev2->dev.parent = &psmouse->ps2dev.serio->dev;
3084
3085		input_set_capability(dev2, EV_REL, REL_X);
3086		input_set_capability(dev2, EV_REL, REL_Y);
3087		if (priv->flags & ALPS_DUALPOINT_WITH_PRESSURE) {
3088			input_set_capability(dev2, EV_ABS, ABS_PRESSURE);
3089			input_set_abs_params(dev2, ABS_PRESSURE, 0, 127, 0, 0);
3090		}
3091		input_set_capability(dev2, EV_KEY, BTN_LEFT);
3092		input_set_capability(dev2, EV_KEY, BTN_RIGHT);
3093		input_set_capability(dev2, EV_KEY, BTN_MIDDLE);
3094
3095		__set_bit(INPUT_PROP_POINTER, dev2->propbit);
3096		__set_bit(INPUT_PROP_POINTING_STICK, dev2->propbit);
3097
3098		error = input_register_device(dev2);
3099		if (error) {
3100			psmouse_err(psmouse,
3101				    "failed to register trackstick device: %d\n",
3102				    error);
3103			input_free_device(dev2);
3104			goto init_fail;
3105		}
3106
3107		priv->dev2 = dev2;
3108	}
3109
3110	priv->psmouse = psmouse;
3111
3112	INIT_DELAYED_WORK(&priv->dev3_register_work,
3113			  alps_register_bare_ps2_mouse);
3114
3115	psmouse->protocol_handler = alps_process_byte;
3116	psmouse->poll = alps_poll;
3117	psmouse->disconnect = alps_disconnect;
3118	psmouse->reconnect = alps_reconnect;
3119	psmouse->pktsize = priv->proto_version == ALPS_PROTO_V4 ? 8 : 6;
3120
3121	/* We are having trouble resyncing ALPS touchpads so disable it for now */
3122	psmouse->resync_time = 0;
3123
3124	/* Allow 2 invalid packets without resetting device */
3125	psmouse->resetafter = psmouse->pktsize * 2;
3126
3127	return 0;
3128
3129init_fail:
3130	psmouse_reset(psmouse);
3131	/*
3132	 * Even though we did not allocate psmouse->private we do free
3133	 * it here.
3134	 */
3135	kfree(psmouse->private);
3136	psmouse->private = NULL;
3137	return error;
3138}
3139
3140int alps_detect(struct psmouse *psmouse, bool set_properties)
3141{
3142	struct alps_data *priv;
3143	int error;
3144
3145	error = alps_identify(psmouse, NULL);
3146	if (error)
3147		return error;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3148
3149	/*
3150	 * Reset the device to make sure it is fully operational:
3151	 * on some laptops, like certain Dell Latitudes, we may
3152	 * fail to properly detect presence of trackstick if device
3153	 * has not been reset.
3154	 */
3155	psmouse_reset(psmouse);
3156
3157	priv = kzalloc(sizeof(struct alps_data), GFP_KERNEL);
3158	if (!priv)
3159		return -ENOMEM;
3160
3161	error = alps_identify(psmouse, priv);
3162	if (error) {
3163		kfree(priv);
3164		return error;
3165	}
3166
3167	if (set_properties) {
3168		psmouse->vendor = "ALPS";
3169		psmouse->name = priv->flags & ALPS_DUALPOINT ?
3170				"DualPoint TouchPad" : "GlidePoint";
3171		psmouse->model = priv->proto_version;
3172	} else {
3173		/*
3174		 * Destroy alps_data structure we allocated earlier since
3175		 * this was just a "trial run". Otherwise we'll keep it
3176		 * to be used by alps_init() which has to be called if
3177		 * we succeed and set_properties is true.
3178		 */
3179		kfree(priv);
3180		psmouse->private = NULL;
3181	}
3182
3183	return 0;
3184}
3185