Linux Audio

Check our new training course

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