alps.c 47.9 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4
/*
 * ALPS touchpad PS/2 mouse driver
 *
 * Copyright (c) 2003 Neil Brown <neilb@cse.unsw.edu.au>
5
 * Copyright (c) 2003-2005 Peter Osterlund <petero2@telia.com>
L
Linus Torvalds 已提交
6 7
 * Copyright (c) 2004 Dmitry Torokhov <dtor@mail.ru>
 * Copyright (c) 2005 Vojtech Pavlik <vojtech@suse.cz>
8
 * Copyright (c) 2009 Sebastian Kapfer <sebastian_kapfer@gmx.net>
L
Linus Torvalds 已提交
9 10 11 12 13 14 15 16 17
 *
 * ALPS detection, tap switching and status querying info is taken from
 * tpconfig utility (by C. Scott Ananian and Bruce Kall).
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as published by
 * the Free Software Foundation.
 */

18
#include <linux/slab.h>
L
Linus Torvalds 已提交
19
#include <linux/input.h>
20
#include <linux/input/mt.h>
L
Linus Torvalds 已提交
21 22 23 24 25 26
#include <linux/serio.h>
#include <linux/libps2.h>

#include "psmouse.h"
#include "alps.h"

27 28 29 30 31
/*
 * Definitions for ALPS version 3 and 4 command mode protocol
 */
#define ALPS_CMD_NIBBLE_10	0x01f2

32 33 34
#define ALPS_REG_BASE_RUSHMORE	0xc2c0
#define ALPS_REG_BASE_PINNACLE	0x0000

35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
static const struct alps_nibble_commands alps_v3_nibble_commands[] = {
	{ PSMOUSE_CMD_SETPOLL,		0x00 }, /* 0 */
	{ PSMOUSE_CMD_RESET_DIS,	0x00 }, /* 1 */
	{ PSMOUSE_CMD_SETSCALE21,	0x00 }, /* 2 */
	{ PSMOUSE_CMD_SETRATE,		0x0a }, /* 3 */
	{ PSMOUSE_CMD_SETRATE,		0x14 }, /* 4 */
	{ PSMOUSE_CMD_SETRATE,		0x28 }, /* 5 */
	{ PSMOUSE_CMD_SETRATE,		0x3c }, /* 6 */
	{ PSMOUSE_CMD_SETRATE,		0x50 }, /* 7 */
	{ PSMOUSE_CMD_SETRATE,		0x64 }, /* 8 */
	{ PSMOUSE_CMD_SETRATE,		0xc8 }, /* 9 */
	{ ALPS_CMD_NIBBLE_10,		0x00 }, /* a */
	{ PSMOUSE_CMD_SETRES,		0x00 }, /* b */
	{ PSMOUSE_CMD_SETRES,		0x01 }, /* c */
	{ PSMOUSE_CMD_SETRES,		0x02 }, /* d */
	{ PSMOUSE_CMD_SETRES,		0x03 }, /* e */
	{ PSMOUSE_CMD_SETSCALE11,	0x00 }, /* f */
};

static const struct alps_nibble_commands alps_v4_nibble_commands[] = {
	{ PSMOUSE_CMD_ENABLE,		0x00 }, /* 0 */
	{ PSMOUSE_CMD_RESET_DIS,	0x00 }, /* 1 */
	{ PSMOUSE_CMD_SETSCALE21,	0x00 }, /* 2 */
	{ PSMOUSE_CMD_SETRATE,		0x0a }, /* 3 */
	{ PSMOUSE_CMD_SETRATE,		0x14 }, /* 4 */
	{ PSMOUSE_CMD_SETRATE,		0x28 }, /* 5 */
	{ PSMOUSE_CMD_SETRATE,		0x3c }, /* 6 */
	{ PSMOUSE_CMD_SETRATE,		0x50 }, /* 7 */
	{ PSMOUSE_CMD_SETRATE,		0x64 }, /* 8 */
	{ PSMOUSE_CMD_SETRATE,		0xc8 }, /* 9 */
	{ ALPS_CMD_NIBBLE_10,		0x00 }, /* a */
	{ PSMOUSE_CMD_SETRES,		0x00 }, /* b */
	{ PSMOUSE_CMD_SETRES,		0x01 }, /* c */
	{ PSMOUSE_CMD_SETRES,		0x02 }, /* d */
	{ PSMOUSE_CMD_SETRES,		0x03 }, /* e */
	{ PSMOUSE_CMD_SETSCALE11,	0x00 }, /* f */
};


74 75 76 77 78 79 80
#define ALPS_DUALPOINT		0x02	/* touchpad has trackstick */
#define ALPS_PASS		0x04	/* device has a pass-through port */

#define ALPS_WHEEL		0x08	/* hardware wheel present */
#define ALPS_FW_BK_1		0x10	/* front & back buttons present */
#define ALPS_FW_BK_2		0x20	/* front & back buttons present */
#define ALPS_FOUR_BUTTONS	0x40	/* 4 direction button present */
81 82
#define ALPS_PS2_INTERLEAVED	0x80	/* 3-byte PS/2 packet interleaved with
					   6-byte ALPS packet */
L
Linus Torvalds 已提交
83

H
Helge Deller 已提交
84
static const struct alps_model_info alps_model_data[] = {
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
	{ { 0x32, 0x02, 0x14 },	0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT },	/* Toshiba Salellite Pro M10 */
	{ { 0x33, 0x02, 0x0a },	0x00, ALPS_PROTO_V1, 0x88, 0xf8, 0 },				/* UMAX-530T */
	{ { 0x53, 0x02, 0x0a },	0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
	{ { 0x53, 0x02, 0x14 },	0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
	{ { 0x60, 0x03, 0xc8 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },				/* HP ze1115 */
	{ { 0x63, 0x02, 0x0a },	0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
	{ { 0x63, 0x02, 0x14 },	0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
	{ { 0x63, 0x02, 0x28 },	0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 },		/* Fujitsu Siemens S6010 */
	{ { 0x63, 0x02, 0x3c },	0x00, ALPS_PROTO_V2, 0x8f, 0x8f, ALPS_WHEEL },			/* Toshiba Satellite S2400-103 */
	{ { 0x63, 0x02, 0x50 },	0x00, ALPS_PROTO_V2, 0xef, 0xef, ALPS_FW_BK_1 },		/* NEC Versa L320 */
	{ { 0x63, 0x02, 0x64 },	0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
	{ { 0x63, 0x03, 0xc8 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT },	/* Dell Latitude D800 */
	{ { 0x73, 0x00, 0x0a },	0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_DUALPOINT },		/* ThinkPad R61 8918-5QG */
	{ { 0x73, 0x02, 0x0a },	0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
	{ { 0x73, 0x02, 0x14 },	0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 },		/* Ahtec Laptop */
	{ { 0x20, 0x02, 0x0e },	0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT },	/* XXX */
	{ { 0x22, 0x02, 0x0a },	0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT },
	{ { 0x22, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xff, 0xff, ALPS_PASS | ALPS_DUALPOINT },	/* Dell Latitude D600 */
103
	/* Dell Latitude E5500, E6400, E6500, Precision M4400 */
104
	{ { 0x62, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xcf, 0xcf,
105
		ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED },
106 107 108 109
	{ { 0x73, 0x02, 0x50 }, 0x00, ALPS_PROTO_V2, 0xcf, 0xcf, ALPS_FOUR_BUTTONS },		/* Dell Vostro 1400 */
	{ { 0x52, 0x01, 0x14 }, 0x00, ALPS_PROTO_V2, 0xff, 0xff,
		ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED },				/* Toshiba Tecra A11-11L */
	{ { 0x73, 0x02, 0x64 },	0x8a, ALPS_PROTO_V4, 0x8f, 0x8f, 0 },
L
Linus Torvalds 已提交
110 111
};

112 113 114 115 116
static void alps_set_abs_params_st(struct alps_data *priv,
				   struct input_dev *dev1);
static void alps_set_abs_params_mt(struct alps_data *priv,
				   struct input_dev *dev1);

L
Linus Torvalds 已提交
117 118 119 120 121 122
/*
 * XXX - this entry is suspicious. First byte has zero lower nibble,
 * which is what a normal mouse would report. Also, the value 0x0e
 * isn't valid per PS/2 spec.
 */

123
/* Packet formats are described in Documentation/input/alps.txt */
L
Linus Torvalds 已提交
124

125
static bool alps_is_valid_first_byte(struct alps_data *priv,
126 127
				     unsigned char data)
{
128
	return (data & priv->mask0) == priv->byte0;
129 130 131 132 133 134
}

static void alps_report_buttons(struct psmouse *psmouse,
				struct input_dev *dev1, struct input_dev *dev2,
				int left, int right, int middle)
{
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
	struct input_dev *dev;

	/*
	 * If shared button has already been reported on the
	 * other device (dev2) then this event should be also
	 * sent through that device.
	 */
	dev = test_bit(BTN_LEFT, dev2->key) ? dev2 : dev1;
	input_report_key(dev, BTN_LEFT, left);

	dev = test_bit(BTN_RIGHT, dev2->key) ? dev2 : dev1;
	input_report_key(dev, BTN_RIGHT, right);

	dev = test_bit(BTN_MIDDLE, dev2->key) ? dev2 : dev1;
	input_report_key(dev, BTN_MIDDLE, middle);

	/*
	 * Sync the _other_ device now, we'll do the first
	 * device later once we report the rest of the events.
	 */
	input_sync(dev2);
156 157
}

158
static void alps_process_packet_v1_v2(struct psmouse *psmouse)
L
Linus Torvalds 已提交
159 160 161
{
	struct alps_data *priv = psmouse->private;
	unsigned char *packet = psmouse->packet;
162 163
	struct input_dev *dev = psmouse->dev;
	struct input_dev *dev2 = priv->dev2;
L
Linus Torvalds 已提交
164
	int x, y, z, ges, fin, left, right, middle;
165
	int back = 0, forward = 0;
L
Linus Torvalds 已提交
166

167
	if (priv->proto_version == ALPS_PROTO_V1) {
168 169
		left = packet[2] & 0x10;
		right = packet[2] & 0x08;
L
Linus Torvalds 已提交
170 171 172 173 174 175 176 177 178 179 180 181 182
		middle = 0;
		x = packet[1] | ((packet[0] & 0x07) << 7);
		y = packet[4] | ((packet[3] & 0x07) << 7);
		z = packet[5];
	} else {
		left = packet[3] & 1;
		right = packet[3] & 2;
		middle = packet[3] & 4;
		x = packet[1] | ((packet[2] & 0x78) << (7 - 3));
		y = packet[4] | ((packet[3] & 0x70) << (7 - 4));
		z = packet[5];
	}

183
	if (priv->flags & ALPS_FW_BK_1) {
184 185
		back = packet[0] & 0x10;
		forward = packet[2] & 4;
186 187
	}

188
	if (priv->flags & ALPS_FW_BK_2) {
189 190 191 192 193 194
		back = packet[3] & 4;
		forward = packet[2] & 4;
		if ((middle = forward && back))
			forward = back = 0;
	}

L
Linus Torvalds 已提交
195 196 197
	ges = packet[2] & 1;
	fin = packet[2] & 2;

198
	if ((priv->flags & ALPS_DUALPOINT) && z == 127) {
L
Linus Torvalds 已提交
199 200
		input_report_rel(dev2, REL_X,  (x > 383 ? (x - 768) : x));
		input_report_rel(dev2, REL_Y, -(y > 255 ? (y - 512) : y));
201

202
		alps_report_buttons(psmouse, dev2, dev, left, right, middle);
203

L
Linus Torvalds 已提交
204 205 206 207
		input_sync(dev2);
		return;
	}

208
	alps_report_buttons(psmouse, dev, dev2, left, right, middle);
209

L
Linus Torvalds 已提交
210
	/* Convert hardware tap to a reasonable Z value */
211 212
	if (ges && !fin)
		z = 40;
L
Linus Torvalds 已提交
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227

	/*
	 * A "tap and drag" operation is reported by the hardware as a transition
	 * from (!fin && ges) to (fin && ges). This should be translated to the
	 * sequence Z>0, Z==0, Z>0, so the Z==0 event has to be generated manually.
	 */
	if (ges && fin && !priv->prev_fin) {
		input_report_abs(dev, ABS_X, x);
		input_report_abs(dev, ABS_Y, y);
		input_report_abs(dev, ABS_PRESSURE, 0);
		input_report_key(dev, BTN_TOOL_FINGER, 0);
		input_sync(dev);
	}
	priv->prev_fin = fin;

228 229 230 231
	if (z > 30)
		input_report_key(dev, BTN_TOUCH, 1);
	if (z < 25)
		input_report_key(dev, BTN_TOUCH, 0);
L
Linus Torvalds 已提交
232 233 234 235 236 237 238 239 240

	if (z > 0) {
		input_report_abs(dev, ABS_X, x);
		input_report_abs(dev, ABS_Y, y);
	}

	input_report_abs(dev, ABS_PRESSURE, z);
	input_report_key(dev, BTN_TOOL_FINGER, z > 0);

241
	if (priv->flags & ALPS_WHEEL)
242
		input_report_rel(dev, REL_WHEEL, ((packet[2] << 1) & 0x08) - ((packet[0] >> 4) & 0x07));
L
Linus Torvalds 已提交
243

244
	if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
245 246
		input_report_key(dev, BTN_FORWARD, forward);
		input_report_key(dev, BTN_BACK, back);
L
Linus Torvalds 已提交
247 248
	}

249
	if (priv->flags & ALPS_FOUR_BUTTONS) {
250 251 252 253 254 255
		input_report_key(dev, BTN_0, packet[2] & 4);
		input_report_key(dev, BTN_1, packet[0] & 0x10);
		input_report_key(dev, BTN_2, packet[3] & 4);
		input_report_key(dev, BTN_3, packet[0] & 0x20);
	}

L
Linus Torvalds 已提交
256 257 258
	input_sync(dev);
}

259 260 261 262 263 264 265 266 267 268
/*
 * Process bitmap data from v3 and v4 protocols. Returns the number of
 * fingers detected. A return value of 0 means at least one of the
 * bitmaps was empty.
 *
 * The bitmaps don't have enough data to track fingers, so this function
 * only generates points representing a bounding box of all contacts.
 * These points are returned in x1, y1, x2, and y2 when the return value
 * is greater than 0.
 */
269 270
static int alps_process_bitmap(struct alps_data *priv,
			       unsigned int x_map, unsigned int y_map,
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
			       int *x1, int *y1, int *x2, int *y2)
{
	struct alps_bitmap_point {
		int start_bit;
		int num_bits;
	};

	int fingers_x = 0, fingers_y = 0, fingers;
	int i, bit, prev_bit;
	struct alps_bitmap_point x_low = {0,}, x_high = {0,};
	struct alps_bitmap_point y_low = {0,}, y_high = {0,};
	struct alps_bitmap_point *point;

	if (!x_map || !y_map)
		return 0;

	*x1 = *y1 = *x2 = *y2 = 0;

	prev_bit = 0;
	point = &x_low;
	for (i = 0; x_map != 0; i++, x_map >>= 1) {
		bit = x_map & 1;
		if (bit) {
			if (!prev_bit) {
				point->start_bit = i;
				fingers_x++;
			}
			point->num_bits++;
		} else {
			if (prev_bit)
				point = &x_high;
			else
				point->num_bits = 0;
		}
		prev_bit = bit;
	}

	/*
	 * y bitmap is reversed for what we need (lower positions are in
	 * higher bits), so we process from the top end.
	 */
312
	y_map = y_map << (sizeof(y_map) * BITS_PER_BYTE - priv->y_bits);
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
	prev_bit = 0;
	point = &y_low;
	for (i = 0; y_map != 0; i++, y_map <<= 1) {
		bit = y_map & (1 << (sizeof(y_map) * BITS_PER_BYTE - 1));
		if (bit) {
			if (!prev_bit) {
				point->start_bit = i;
				fingers_y++;
			}
			point->num_bits++;
		} else {
			if (prev_bit)
				point = &y_high;
			else
				point->num_bits = 0;
		}
		prev_bit = bit;
	}

	/*
	 * Fingers can overlap, so we use the maximum count of fingers
	 * on either axis as the finger count.
	 */
	fingers = max(fingers_x, fingers_y);

	/*
	 * If total fingers is > 1 but either axis reports only a single
	 * contact, we have overlapping or adjacent fingers. For the
	 * purposes of creating a bounding box, divide the single contact
	 * (roughly) equally between the two points.
	 */
	if (fingers > 1) {
		if (fingers_x == 1) {
			i = x_low.num_bits / 2;
			x_low.num_bits = x_low.num_bits - i;
			x_high.start_bit = x_low.start_bit + i;
			x_high.num_bits = max(i, 1);
		} else if (fingers_y == 1) {
			i = y_low.num_bits / 2;
			y_low.num_bits = y_low.num_bits - i;
			y_high.start_bit = y_low.start_bit + i;
			y_high.num_bits = max(i, 1);
		}
	}

358 359 360 361
	*x1 = (priv->x_max * (2 * x_low.start_bit + x_low.num_bits - 1)) /
	      (2 * (priv->x_bits - 1));
	*y1 = (priv->y_max * (2 * y_low.start_bit + y_low.num_bits - 1)) /
	      (2 * (priv->y_bits - 1));
362 363

	if (fingers > 1) {
364 365 366 367 368 369
		*x2 = (priv->x_max *
		       (2 * x_high.start_bit + x_high.num_bits - 1)) /
		      (2 * (priv->x_bits - 1));
		*y2 = (priv->y_max *
		       (2 * y_high.start_bit + y_high.num_bits - 1)) /
		      (2 * (priv->y_bits - 1));
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
	}

	return fingers;
}

static void alps_set_slot(struct input_dev *dev, int slot, bool active,
			  int x, int y)
{
	input_mt_slot(dev, slot);
	input_mt_report_slot_state(dev, MT_TOOL_FINGER, active);
	if (active) {
		input_report_abs(dev, ABS_MT_POSITION_X, x);
		input_report_abs(dev, ABS_MT_POSITION_Y, y);
	}
}

static void alps_report_semi_mt_data(struct input_dev *dev, int num_fingers,
				     int x1, int y1, int x2, int y2)
{
	alps_set_slot(dev, 0, num_fingers != 0, x1, y1);
	alps_set_slot(dev, 1, num_fingers == 2, x2, y2);
}

393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
static void alps_process_trackstick_packet_v3(struct psmouse *psmouse)
{
	struct alps_data *priv = psmouse->private;
	unsigned char *packet = psmouse->packet;
	struct input_dev *dev = priv->dev2;
	int x, y, z, left, right, middle;

	/* Sanity check packet */
	if (!(packet[0] & 0x40)) {
		psmouse_dbg(psmouse, "Bad trackstick packet, discarding\n");
		return;
	}

	/*
	 * There's a special packet that seems to indicate the end
	 * of a stream of trackstick data. Filter these out.
	 */
	if (packet[1] == 0x7f && packet[2] == 0x7f && packet[4] == 0x7f)
		return;

	x = (s8)(((packet[0] & 0x20) << 2) | (packet[1] & 0x7f));
	y = (s8)(((packet[0] & 0x10) << 3) | (packet[2] & 0x7f));
	z = (packet[4] & 0x7c) >> 2;

	/*
	 * The x and y values tend to be quite large, and when used
	 * alone the trackstick is difficult to use. Scale them down
	 * to compensate.
	 */
	x /= 8;
	y /= 8;

	input_report_rel(dev, REL_X, x);
	input_report_rel(dev, REL_Y, -y);

	/*
	 * Most ALPS models report the trackstick buttons in the touchpad
	 * packets, but a few report them here. No reliable way has been
	 * found to differentiate between the models upfront, so we enable
	 * the quirk in response to seeing a button press in the trackstick
	 * packet.
	 */
	left = packet[3] & 0x01;
	right = packet[3] & 0x02;
	middle = packet[3] & 0x04;

	if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) &&
	    (left || right || middle))
		priv->quirks |= ALPS_QUIRK_TRACKSTICK_BUTTONS;

	if (priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) {
		input_report_key(dev, BTN_LEFT, left);
		input_report_key(dev, BTN_RIGHT, right);
		input_report_key(dev, BTN_MIDDLE, middle);
	}

	input_sync(dev);
	return;
}

453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
static void alps_decode_buttons_v3(struct alps_fields *f, unsigned char *p)
{
	f->left = !!(p[3] & 0x01);
	f->right = !!(p[3] & 0x02);
	f->middle = !!(p[3] & 0x04);

	f->ts_left = !!(p[3] & 0x10);
	f->ts_right = !!(p[3] & 0x20);
	f->ts_middle = !!(p[3] & 0x40);
}

static void alps_decode_pinnacle(struct alps_fields *f, unsigned char *p)
{
	f->first_mp = !!(p[4] & 0x40);
	f->is_mp = !!(p[0] & 0x40);

	f->fingers = (p[5] & 0x3) + 1;
	f->x_map = ((p[4] & 0x7e) << 8) |
		   ((p[1] & 0x7f) << 2) |
		   ((p[0] & 0x30) >> 4);
	f->y_map = ((p[3] & 0x70) << 4) |
		   ((p[2] & 0x7f) << 1) |
		   (p[4] & 0x01);

	f->x = ((p[1] & 0x7f) << 4) | ((p[4] & 0x30) >> 2) |
	       ((p[0] & 0x30) >> 4);
	f->y = ((p[2] & 0x7f) << 4) | (p[4] & 0x0f);
	f->z = p[5] & 0x7f;

	alps_decode_buttons_v3(f, p);
}

485 486 487 488 489 490 491 492
static void alps_decode_rushmore(struct alps_fields *f, unsigned char *p)
{
	alps_decode_pinnacle(f, p);

	f->x_map |= (p[5] & 0x10) << 11;
	f->y_map |= (p[5] & 0x20) << 6;
}

493 494 495 496 497 498
static void alps_process_touchpad_packet_v3(struct psmouse *psmouse)
{
	struct alps_data *priv = psmouse->private;
	unsigned char *packet = psmouse->packet;
	struct input_dev *dev = psmouse->dev;
	struct input_dev *dev2 = priv->dev2;
499 500
	int x1 = 0, y1 = 0, x2 = 0, y2 = 0;
	int fingers = 0, bmap_fingers;
501 502 503
	struct alps_fields f;

	priv->decode_fields(&f, packet);
504 505

	/*
506 507 508 509
	 * There's no single feature of touchpad position and bitmap packets
	 * that can be used to distinguish between them. We rely on the fact
	 * that a bitmap packet should always follow a position packet with
	 * bit 6 of packet[4] set.
510 511 512 513 514 515 516 517
	 */
	if (priv->multi_packet) {
		/*
		 * Sometimes a position packet will indicate a multi-packet
		 * sequence, but then what follows is another position
		 * packet. Check for this, and when it happens process the
		 * position packet as usual.
		 */
518 519
		if (f.is_mp) {
			fingers = f.fingers;
520
			bmap_fingers = alps_process_bitmap(priv,
521
							   f.x_map, f.y_map,
522 523
							   &x1, &y1, &x2, &y2);

524
			/*
525 526
			 * We shouldn't report more than one finger if
			 * we don't have two coordinates.
527
			 */
528 529 530 531
			if (fingers > 1 && bmap_fingers < 2)
				fingers = bmap_fingers;

			/* Now process position packet */
532
			priv->decode_fields(&f, priv->multi_data);
533 534
		} else {
			priv->multi_packet = 0;
535 536 537
		}
	}

538 539 540 541 542 543 544 545
	/*
	 * Bit 6 of byte 0 is not usually set in position packets. The only
	 * times it seems to be set is in situations where the data is
	 * suspect anyway, e.g. a palm resting flat on the touchpad. Given
	 * this combined with the fact that this bit is useful for filtering
	 * out misidentified bitmap packets, we reject anything with this
	 * bit set.
	 */
546
	if (f.is_mp)
547 548
		return;

549
	if (!priv->multi_packet && f.first_mp) {
550
		priv->multi_packet = 1;
551 552 553 554 555
		memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
		return;
	}

	priv->multi_packet = 0;
556 557 558 559 560 561 562

	/*
	 * Sometimes the hardware sends a single packet with z = 0
	 * in the middle of a stream. Real releases generate packets
	 * with x, y, and z all zero, so these seem to be flukes.
	 * Ignore them.
	 */
563
	if (f.x && f.y && !f.z)
564 565
		return;

566 567 568 569 570
	/*
	 * If we don't have MT data or the bitmaps were empty, we have
	 * to rely on ST data.
	 */
	if (!fingers) {
571 572 573
		x1 = f.x;
		y1 = f.y;
		fingers = f.z > 0 ? 1 : 0;
574 575
	}

576
	if (f.z >= 64)
577 578 579 580
		input_report_key(dev, BTN_TOUCH, 1);
	else
		input_report_key(dev, BTN_TOUCH, 0);

581 582
	alps_report_semi_mt_data(dev, fingers, x1, y1, x2, y2);

583
	input_mt_report_finger_count(dev, fingers);
584

585 586 587
	input_report_key(dev, BTN_LEFT, f.left);
	input_report_key(dev, BTN_RIGHT, f.right);
	input_report_key(dev, BTN_MIDDLE, f.middle);
588

589 590 591
	if (f.z > 0) {
		input_report_abs(dev, ABS_X, f.x);
		input_report_abs(dev, ABS_Y, f.y);
592
	}
593
	input_report_abs(dev, ABS_PRESSURE, f.z);
594 595 596 597

	input_sync(dev);

	if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS)) {
598 599 600
		input_report_key(dev2, BTN_LEFT, f.ts_left);
		input_report_key(dev2, BTN_RIGHT, f.ts_right);
		input_report_key(dev2, BTN_MIDDLE, f.ts_middle);
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626
		input_sync(dev2);
	}
}

static void alps_process_packet_v3(struct psmouse *psmouse)
{
	unsigned char *packet = psmouse->packet;

	/*
	 * v3 protocol packets come in three types, two representing
	 * touchpad data and one representing trackstick data.
	 * Trackstick packets seem to be distinguished by always
	 * having 0x3f in the last byte. This value has never been
	 * observed in the last byte of either of the other types
	 * of packets.
	 */
	if (packet[5] == 0x3f) {
		alps_process_trackstick_packet_v3(psmouse);
		return;
	}

	alps_process_touchpad_packet_v3(psmouse);
}

static void alps_process_packet_v4(struct psmouse *psmouse)
{
627
	struct alps_data *priv = psmouse->private;
628 629
	unsigned char *packet = psmouse->packet;
	struct input_dev *dev = psmouse->dev;
630
	int offset;
631 632
	int x, y, z;
	int left, right;
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664
	int x1, y1, x2, y2;
	int fingers = 0;
	unsigned int x_bitmap, y_bitmap;

	/*
	 * v4 has a 6-byte encoding for bitmap data, but this data is
	 * broken up between 3 normal packets. Use priv->multi_packet to
	 * track our position in the bitmap packet.
	 */
	if (packet[6] & 0x40) {
		/* sync, reset position */
		priv->multi_packet = 0;
	}

	if (WARN_ON_ONCE(priv->multi_packet > 2))
		return;

	offset = 2 * priv->multi_packet;
	priv->multi_data[offset] = packet[6];
	priv->multi_data[offset + 1] = packet[7];

	if (++priv->multi_packet > 2) {
		priv->multi_packet = 0;

		x_bitmap = ((priv->multi_data[2] & 0x1f) << 10) |
			   ((priv->multi_data[3] & 0x60) << 3) |
			   ((priv->multi_data[0] & 0x3f) << 2) |
			   ((priv->multi_data[1] & 0x60) >> 5);
		y_bitmap = ((priv->multi_data[5] & 0x01) << 10) |
			   ((priv->multi_data[3] & 0x1f) << 5) |
			    (priv->multi_data[1] & 0x1f);

665
		fingers = alps_process_bitmap(priv, x_bitmap, y_bitmap,
666 667 668 669 670 671 672 673 674
					      &x1, &y1, &x2, &y2);

		/* Store MT data.*/
		priv->fingers = fingers;
		priv->x1 = x1;
		priv->x2 = x2;
		priv->y1 = y1;
		priv->y2 = y2;
	}
675 676 677 678 679 680 681 682 683

	left = packet[4] & 0x01;
	right = packet[4] & 0x02;

	x = ((packet[1] & 0x7f) << 4) | ((packet[3] & 0x30) >> 2) |
	    ((packet[0] & 0x30) >> 4);
	y = ((packet[2] & 0x7f) << 4) | (packet[3] & 0x0f);
	z = packet[5] & 0x7f;

684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700
	/*
	 * If there were no contacts in the bitmap, use ST
	 * points in MT reports.
	 * If there were two contacts or more, report MT data.
	 */
	if (priv->fingers < 2) {
		x1 = x;
		y1 = y;
		fingers = z > 0 ? 1 : 0;
	} else {
		fingers = priv->fingers;
		x1 = priv->x1;
		x2 = priv->x2;
		y1 = priv->y1;
		y2 = priv->y2;
	}

701 702 703 704 705
	if (z >= 64)
		input_report_key(dev, BTN_TOUCH, 1);
	else
		input_report_key(dev, BTN_TOUCH, 0);

706 707
	alps_report_semi_mt_data(dev, fingers, x1, y1, x2, y2);

708
	input_mt_report_finger_count(dev, fingers);
709 710 711 712

	input_report_key(dev, BTN_LEFT, left);
	input_report_key(dev, BTN_RIGHT, right);

713 714 715 716 717 718 719 720 721
	if (z > 0) {
		input_report_abs(dev, ABS_X, x);
		input_report_abs(dev, ABS_Y, y);
	}
	input_report_abs(dev, ABS_PRESSURE, z);

	input_sync(dev);
}

722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771
static void alps_report_bare_ps2_packet(struct psmouse *psmouse,
					unsigned char packet[],
					bool report_buttons)
{
	struct alps_data *priv = psmouse->private;
	struct input_dev *dev2 = priv->dev2;

	if (report_buttons)
		alps_report_buttons(psmouse, dev2, psmouse->dev,
				packet[0] & 1, packet[0] & 2, packet[0] & 4);

	input_report_rel(dev2, REL_X,
		packet[1] ? packet[1] - ((packet[0] << 4) & 0x100) : 0);
	input_report_rel(dev2, REL_Y,
		packet[2] ? ((packet[0] << 3) & 0x100) - packet[2] : 0);

	input_sync(dev2);
}

static psmouse_ret_t alps_handle_interleaved_ps2(struct psmouse *psmouse)
{
	struct alps_data *priv = psmouse->private;

	if (psmouse->pktcnt < 6)
		return PSMOUSE_GOOD_DATA;

	if (psmouse->pktcnt == 6) {
		/*
		 * Start a timer to flush the packet if it ends up last
		 * 6-byte packet in the stream. Timer needs to fire
		 * psmouse core times out itself. 20 ms should be enough
		 * to decide if we are getting more data or not.
		 */
		mod_timer(&priv->timer, jiffies + msecs_to_jiffies(20));
		return PSMOUSE_GOOD_DATA;
	}

	del_timer(&priv->timer);

	if (psmouse->packet[6] & 0x80) {

		/*
		 * Highest bit is set - that means we either had
		 * complete ALPS packet and this is start of the
		 * next packet or we got garbage.
		 */

		if (((psmouse->packet[3] |
		      psmouse->packet[4] |
		      psmouse->packet[5]) & 0x80) ||
772
		    (!alps_is_valid_first_byte(priv, psmouse->packet[6]))) {
773
			psmouse_dbg(psmouse,
774 775
				    "refusing packet %4ph (suspected interleaved ps/2)\n",
				    psmouse->packet + 3);
776 777 778
			return PSMOUSE_BAD_DATA;
		}

779
		priv->process_packet(psmouse);
780 781 782 783 784 785 786 787 788 789 790 791 792 793

		/* Continue with the next packet */
		psmouse->packet[0] = psmouse->packet[6];
		psmouse->pktcnt = 1;

	} else {

		/*
		 * High bit is 0 - that means that we indeed got a PS/2
		 * packet in the middle of ALPS packet.
		 *
		 * There is also possibility that we got 6-byte ALPS
		 * packet followed  by 3-byte packet from trackpoint. We
		 * can not distinguish between these 2 scenarios but
794
		 * because the latter is unlikely to happen in course of
795 796 797 798 799
		 * normal operation (user would need to press all
		 * buttons on the pad and start moving trackpoint
		 * without touching the pad surface) we assume former.
		 * Even if we are wrong the wost thing that would happen
		 * the cursor would jump but we should not get protocol
800
		 * de-synchronization.
801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822
		 */

		alps_report_bare_ps2_packet(psmouse, &psmouse->packet[3],
					    false);

		/*
		 * Continue with the standard ALPS protocol handling,
		 * but make sure we won't process it as an interleaved
		 * packet again, which may happen if all buttons are
		 * pressed. To avoid this let's reset the 4th bit which
		 * is normally 1.
		 */
		psmouse->packet[3] = psmouse->packet[6] & 0xf7;
		psmouse->pktcnt = 4;
	}

	return PSMOUSE_GOOD_DATA;
}

static void alps_flush_packet(unsigned long data)
{
	struct psmouse *psmouse = (struct psmouse *)data;
823
	struct alps_data *priv = psmouse->private;
824 825 826

	serio_pause_rx(psmouse->ps2dev.serio);

827
	if (psmouse->pktcnt == psmouse->pktsize) {
828 829 830 831 832 833 834 835 836

		/*
		 * We did not any more data in reasonable amount of time.
		 * Validate the last 3 bytes and process as a standard
		 * ALPS packet.
		 */
		if ((psmouse->packet[3] |
		     psmouse->packet[4] |
		     psmouse->packet[5]) & 0x80) {
837
			psmouse_dbg(psmouse,
838 839
				    "refusing packet %3ph (suspected interleaved ps/2)\n",
				    psmouse->packet + 3);
840
		} else {
841
			priv->process_packet(psmouse);
842 843 844 845 846 847 848
		}
		psmouse->pktcnt = 0;
	}

	serio_continue_rx(psmouse->ps2dev.serio);
}

849
static psmouse_ret_t alps_process_byte(struct psmouse *psmouse)
L
Linus Torvalds 已提交
850 851 852 853 854
{
	struct alps_data *priv = psmouse->private;

	if ((psmouse->packet[0] & 0xc8) == 0x08) { /* PS/2 packet */
		if (psmouse->pktcnt == 3) {
855 856
			alps_report_bare_ps2_packet(psmouse, psmouse->packet,
						    true);
L
Linus Torvalds 已提交
857 858 859 860 861
			return PSMOUSE_FULL_PACKET;
		}
		return PSMOUSE_GOOD_DATA;
	}

862 863
	/* Check for PS/2 packet stuffed in the middle of ALPS packet. */

864
	if ((priv->flags & ALPS_PS2_INTERLEAVED) &&
865 866 867 868
	    psmouse->pktcnt >= 4 && (psmouse->packet[3] & 0x0f) == 0x0f) {
		return alps_handle_interleaved_ps2(psmouse);
	}

869
	if (!alps_is_valid_first_byte(priv, psmouse->packet[0])) {
870 871
		psmouse_dbg(psmouse,
			    "refusing packet[0] = %x (mask0 = %x, byte0 = %x)\n",
872
			    psmouse->packet[0], priv->mask0, priv->byte0);
L
Linus Torvalds 已提交
873
		return PSMOUSE_BAD_DATA;
874
	}
L
Linus Torvalds 已提交
875

876 877
	/* Bytes 2 - pktsize should have 0 in the highest bit */
	if (psmouse->pktcnt >= 2 && psmouse->pktcnt <= psmouse->pktsize &&
878
	    (psmouse->packet[psmouse->pktcnt - 1] & 0x80)) {
879 880 881
		psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
			    psmouse->pktcnt - 1,
			    psmouse->packet[psmouse->pktcnt - 1]);
L
Linus Torvalds 已提交
882
		return PSMOUSE_BAD_DATA;
883
	}
L
Linus Torvalds 已提交
884

885
	if (psmouse->pktcnt == psmouse->pktsize) {
886
		priv->process_packet(psmouse);
L
Linus Torvalds 已提交
887 888 889 890 891 892
		return PSMOUSE_FULL_PACKET;
	}

	return PSMOUSE_GOOD_DATA;
}

893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973
static int alps_command_mode_send_nibble(struct psmouse *psmouse, int nibble)
{
	struct ps2dev *ps2dev = &psmouse->ps2dev;
	struct alps_data *priv = psmouse->private;
	int command;
	unsigned char *param;
	unsigned char dummy[4];

	BUG_ON(nibble > 0xf);

	command = priv->nibble_commands[nibble].command;
	param = (command & 0x0f00) ?
		dummy : (unsigned char *)&priv->nibble_commands[nibble].data;

	if (ps2_command(ps2dev, param, command))
		return -1;

	return 0;
}

static int alps_command_mode_set_addr(struct psmouse *psmouse, int addr)
{
	struct ps2dev *ps2dev = &psmouse->ps2dev;
	struct alps_data *priv = psmouse->private;
	int i, nibble;

	if (ps2_command(ps2dev, NULL, priv->addr_command))
		return -1;

	for (i = 12; i >= 0; i -= 4) {
		nibble = (addr >> i) & 0xf;
		if (alps_command_mode_send_nibble(psmouse, nibble))
			return -1;
	}

	return 0;
}

static int __alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
{
	struct ps2dev *ps2dev = &psmouse->ps2dev;
	unsigned char param[4];

	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
		return -1;

	/*
	 * The address being read is returned in the first two bytes
	 * of the result. Check that this address matches the expected
	 * address.
	 */
	if (addr != ((param[0] << 8) | param[1]))
		return -1;

	return param[2];
}

static int alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
{
	if (alps_command_mode_set_addr(psmouse, addr))
		return -1;
	return __alps_command_mode_read_reg(psmouse, addr);
}

static int __alps_command_mode_write_reg(struct psmouse *psmouse, u8 value)
{
	if (alps_command_mode_send_nibble(psmouse, (value >> 4) & 0xf))
		return -1;
	if (alps_command_mode_send_nibble(psmouse, value & 0xf))
		return -1;
	return 0;
}

static int alps_command_mode_write_reg(struct psmouse *psmouse, int addr,
				       u8 value)
{
	if (alps_command_mode_set_addr(psmouse, addr))
		return -1;
	return __alps_command_mode_write_reg(psmouse, value);
}

974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996
static int alps_rpt_cmd(struct psmouse *psmouse, int init_command,
			int repeated_command, unsigned char *param)
{
	struct ps2dev *ps2dev = &psmouse->ps2dev;

	param[0] = 0;
	if (init_command && ps2_command(ps2dev, param, init_command))
		return -EIO;

	if (ps2_command(ps2dev,  NULL, repeated_command) ||
	    ps2_command(ps2dev,  NULL, repeated_command) ||
	    ps2_command(ps2dev,  NULL, repeated_command))
		return -EIO;

	param[0] = param[1] = param[2] = 0xff;
	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
		return -EIO;

	psmouse_dbg(psmouse, "%2.2X report: %2.2x %2.2x %2.2x\n",
		    repeated_command, param[0], param[1], param[2]);
	return 0;
}

997
static int alps_enter_command_mode(struct psmouse *psmouse)
998 999 1000
{
	unsigned char param[4];

1001
	if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_RESET_WRAP, param)) {
1002 1003 1004 1005
		psmouse_err(psmouse, "failed to enter command mode\n");
		return -1;
	}

1006
	if (param[0] != 0x88 || (param[1] != 0x07 && param[1] != 0x08)) {
1007
		psmouse_dbg(psmouse,
1008
			    "unknown response while entering command mode\n");
1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
		return -1;
	}
	return 0;
}

static inline int alps_exit_command_mode(struct psmouse *psmouse)
{
	struct ps2dev *ps2dev = &psmouse->ps2dev;
	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM))
		return -1;
	return 0;
}

L
Linus Torvalds 已提交
1022 1023 1024 1025 1026
/*
 * For DualPoint devices select the device that should respond to
 * subsequent commands. It looks like glidepad is behind stickpointer,
 * I'd thought it would be other way around...
 */
1027
static int alps_passthrough_mode_v2(struct psmouse *psmouse, bool enable)
L
Linus Torvalds 已提交
1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
{
	struct ps2dev *ps2dev = &psmouse->ps2dev;
	int cmd = enable ? PSMOUSE_CMD_SETSCALE21 : PSMOUSE_CMD_SETSCALE11;

	if (ps2_command(ps2dev, NULL, cmd) ||
	    ps2_command(ps2dev, NULL, cmd) ||
	    ps2_command(ps2dev, NULL, cmd) ||
	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
		return -1;

	/* we may get 3 more bytes, just ignore them */
1039
	ps2_drain(ps2dev, 3, 100);
L
Linus Torvalds 已提交
1040 1041 1042 1043

	return 0;
}

1044
static int alps_absolute_mode_v1_v2(struct psmouse *psmouse)
L
Linus Torvalds 已提交
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
{
	struct ps2dev *ps2dev = &psmouse->ps2dev;

	/* Try ALPS magic knock - 4 disable before enable */
	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
		return -1;

	/*
	 * Switch mouse to poll (remote) mode so motion data will not
	 * get in our way
	 */
	return ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETPOLL);
}

static int alps_get_status(struct psmouse *psmouse, char *param)
{
	/* Get status: 0xF5 0xF5 0xF5 0xE9 */
1066
	if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_DISABLE, param))
L
Linus Torvalds 已提交
1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099
		return -1;

	return 0;
}

/*
 * Turn touchpad tapping on or off. The sequences are:
 * 0xE9 0xF5 0xF5 0xF3 0x0A to enable,
 * 0xE9 0xF5 0xF5 0xE8 0x00 to disable.
 * My guess that 0xE9 (GetInfo) is here as a sync point.
 * For models that also have stickpointer (DualPoints) its tapping
 * is controlled separately (0xE6 0xE6 0xE6 0xF3 0x14|0x0A) but
 * we don't fiddle with it.
 */
static int alps_tap_mode(struct psmouse *psmouse, int enable)
{
	struct ps2dev *ps2dev = &psmouse->ps2dev;
	int cmd = enable ? PSMOUSE_CMD_SETRATE : PSMOUSE_CMD_SETRES;
	unsigned char tap_arg = enable ? 0x0A : 0x00;
	unsigned char param[4];

	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO) ||
	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
	    ps2_command(ps2dev, &tap_arg, cmd))
		return -1;

	if (alps_get_status(psmouse, param))
		return -1;

	return 0;
}

1100 1101 1102 1103 1104 1105 1106
/*
 * alps_poll() - poll the touchpad for current motion packet.
 * Used in resync.
 */
static int alps_poll(struct psmouse *psmouse)
{
	struct alps_data *priv = psmouse->private;
1107
	unsigned char buf[sizeof(psmouse->packet)];
1108
	bool poll_failed;
1109

1110
	if (priv->flags & ALPS_PASS)
1111
		alps_passthrough_mode_v2(psmouse, true);
1112 1113 1114 1115

	poll_failed = ps2_command(&psmouse->ps2dev, buf,
				  PSMOUSE_CMD_POLL | (psmouse->pktsize << 8)) < 0;

1116
	if (priv->flags & ALPS_PASS)
1117
		alps_passthrough_mode_v2(psmouse, false);
1118

1119
	if (poll_failed || (buf[0] & priv->mask0) != priv->byte0)
1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133
		return -1;

	if ((psmouse->badbyte & 0xc8) == 0x08) {
/*
 * Poll the track stick ...
 */
		if (ps2_command(&psmouse->ps2dev, buf, PSMOUSE_CMD_POLL | (3 << 8)))
			return -1;
	}

	memcpy(psmouse->packet, buf, sizeof(buf));
	return 0;
}

1134
static int alps_hw_init_v1_v2(struct psmouse *psmouse)
L
Linus Torvalds 已提交
1135 1136
{
	struct alps_data *priv = psmouse->private;
D
Dmitry Torokhov 已提交
1137

1138
	if ((priv->flags & ALPS_PASS) &&
1139
	    alps_passthrough_mode_v2(psmouse, true)) {
L
Linus Torvalds 已提交
1140
		return -1;
1141
	}
L
Linus Torvalds 已提交
1142

1143
	if (alps_tap_mode(psmouse, true)) {
1144
		psmouse_warn(psmouse, "Failed to enable hardware tapping\n");
L
Linus Torvalds 已提交
1145
		return -1;
1146
	}
L
Linus Torvalds 已提交
1147

1148
	if (alps_absolute_mode_v1_v2(psmouse)) {
1149
		psmouse_err(psmouse, "Failed to enable absolute mode\n");
L
Linus Torvalds 已提交
1150 1151 1152
		return -1;
	}

1153
	if ((priv->flags & ALPS_PASS) &&
1154
	    alps_passthrough_mode_v2(psmouse, false)) {
L
Linus Torvalds 已提交
1155
		return -1;
1156
	}
L
Linus Torvalds 已提交
1157

1158 1159
	/* ALPS needs stream mode, otherwise it won't report any data */
	if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)) {
1160
		psmouse_err(psmouse, "Failed to enable stream mode\n");
1161 1162 1163 1164 1165 1166
		return -1;
	}

	return 0;
}

1167
/*
1168
 * Enable or disable passthrough mode to the trackstick.
1169
 */
1170 1171
static int alps_passthrough_mode_v3(struct psmouse *psmouse,
				    int reg_base, bool enable)
1172
{
1173
	int reg_val, ret = -1;
1174

1175
	if (alps_enter_command_mode(psmouse))
1176 1177
		return -1;

1178 1179 1180 1181
	reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x0008);
	if (reg_val == -1)
		goto error;

1182 1183 1184 1185 1186
	if (enable)
		reg_val |= 0x01;
	else
		reg_val &= ~0x01;

1187
	ret = __alps_command_mode_write_reg(psmouse, reg_val);
1188

1189 1190 1191 1192
error:
	if (alps_exit_command_mode(psmouse))
		ret = -1;
	return ret;
1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210
}

/* Must be in command mode when calling this function */
static int alps_absolute_mode_v3(struct psmouse *psmouse)
{
	int reg_val;

	reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
	if (reg_val == -1)
		return -1;

	reg_val |= 0x06;
	if (__alps_command_mode_write_reg(psmouse, reg_val))
		return -1;

	return 0;
}

1211
static int alps_probe_trackstick_v3(struct psmouse *psmouse, int reg_base)
1212
{
1213
	int ret = -EIO, reg_val;
1214

1215
	if (alps_enter_command_mode(psmouse))
1216 1217
		goto error;

1218
	reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x08);
1219 1220
	if (reg_val == -1)
		goto error;
1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254

	/* bit 7: trackstick is present */
	ret = reg_val & 0x80 ? 0 : -ENODEV;

error:
	alps_exit_command_mode(psmouse);
	return ret;
}

static int alps_setup_trackstick_v3(struct psmouse *psmouse, int reg_base)
{
	struct ps2dev *ps2dev = &psmouse->ps2dev;
	int ret = 0;
	unsigned char param[4];

	if (alps_passthrough_mode_v3(psmouse, reg_base, true))
		return -EIO;

	/*
	 * E7 report for the trackstick
	 *
	 * There have been reports of failures to seem to trace back
	 * to the above trackstick check failing. When these occur
	 * this E7 report fails, so when that happens we continue
	 * with the assumption that there isn't a trackstick after
	 * all.
	 */
	if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_SETSCALE21, param)) {
		psmouse_warn(psmouse, "trackstick E7 report failed\n");
		ret = -ENODEV;
	} else {
		psmouse_dbg(psmouse,
			    "trackstick E7 report: %2.2x %2.2x %2.2x\n",
			    param[0], param[1], param[2]);
1255 1256

		/*
1257 1258 1259 1260
		 * Not sure what this does, but it is absolutely
		 * essential. Without it, the touchpad does not
		 * work at all and the trackstick just emits normal
		 * PS/2 packets.
1261
		 */
1262 1263 1264 1265 1266 1267 1268 1269 1270
		if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
		    alps_command_mode_send_nibble(psmouse, 0x9) ||
		    alps_command_mode_send_nibble(psmouse, 0x4)) {
			psmouse_err(psmouse,
				    "Error sending magic E6 sequence\n");
			ret = -EIO;
			goto error;
1271 1272
		}

1273 1274 1275 1276 1277
		/*
		 * This ensures the trackstick packets are in the format
		 * supported by this driver. If bit 1 isn't set the packet
		 * format is different.
		 */
1278
		if (alps_enter_command_mode(psmouse) ||
1279 1280 1281 1282
		    alps_command_mode_write_reg(psmouse,
						reg_base + 0x08, 0x82) ||
		    alps_exit_command_mode(psmouse))
			ret = -EIO;
1283 1284
	}

1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304
error:
	if (alps_passthrough_mode_v3(psmouse, reg_base, false))
		ret = -EIO;

	return ret;
}

static int alps_hw_init_v3(struct psmouse *psmouse)
{
	struct ps2dev *ps2dev = &psmouse->ps2dev;
	int reg_val;
	unsigned char param[4];

	reg_val = alps_probe_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE);
	if (reg_val == -EIO)
		goto error;
	if (reg_val == 0 &&
	    alps_setup_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE) == -EIO)
		goto error;

1305
	if (alps_enter_command_mode(psmouse) ||
1306
	    alps_absolute_mode_v3(psmouse)) {
1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364
		psmouse_err(psmouse, "Failed to enter absolute mode\n");
		goto error;
	}

	reg_val = alps_command_mode_read_reg(psmouse, 0x0006);
	if (reg_val == -1)
		goto error;
	if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
		goto error;

	reg_val = alps_command_mode_read_reg(psmouse, 0x0007);
	if (reg_val == -1)
		goto error;
	if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
		goto error;

	if (alps_command_mode_read_reg(psmouse, 0x0144) == -1)
		goto error;
	if (__alps_command_mode_write_reg(psmouse, 0x04))
		goto error;

	if (alps_command_mode_read_reg(psmouse, 0x0159) == -1)
		goto error;
	if (__alps_command_mode_write_reg(psmouse, 0x03))
		goto error;

	if (alps_command_mode_read_reg(psmouse, 0x0163) == -1)
		goto error;
	if (alps_command_mode_write_reg(psmouse, 0x0163, 0x03))
		goto error;

	if (alps_command_mode_read_reg(psmouse, 0x0162) == -1)
		goto error;
	if (alps_command_mode_write_reg(psmouse, 0x0162, 0x04))
		goto error;

	alps_exit_command_mode(psmouse);

	/* Set rate and enable data reporting */
	param[0] = 0x64;
	if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
		psmouse_err(psmouse, "Failed to enable data reporting\n");
		return -1;
	}

	return 0;

error:
	/*
	 * Leaving the touchpad in command mode will essentially render
	 * it unusable until the machine reboots, so exit it here just
	 * to be safe
	 */
	alps_exit_command_mode(psmouse);
	return -1;
}

1365 1366
static int alps_hw_init_rushmore_v3(struct psmouse *psmouse)
{
1367
	struct alps_data *priv = psmouse->private;
1368 1369 1370
	struct ps2dev *ps2dev = &psmouse->ps2dev;
	int reg_val, ret = -1;

1371 1372 1373 1374 1375 1376 1377 1378 1379
	if (priv->flags & ALPS_DUALPOINT) {
		reg_val = alps_setup_trackstick_v3(psmouse,
						   ALPS_REG_BASE_RUSHMORE);
		if (reg_val == -EIO)
			goto error;
		if (reg_val == -ENODEV)
			priv->flags &= ~ALPS_DUALPOINT;
	}

1380
	if (alps_enter_command_mode(psmouse) ||
1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408
	    alps_command_mode_read_reg(psmouse, 0xc2d9) == -1 ||
	    alps_command_mode_write_reg(psmouse, 0xc2cb, 0x00))
		goto error;

	reg_val = alps_command_mode_read_reg(psmouse, 0xc2c6);
	if (reg_val == -1)
		goto error;
	if (__alps_command_mode_write_reg(psmouse, reg_val & 0xfd))
		goto error;

	if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
		goto error;

	/* enter absolute mode */
	reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
	if (reg_val == -1)
		goto error;
	if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
		goto error;

	alps_exit_command_mode(psmouse);
	return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);

error:
	alps_exit_command_mode(psmouse);
	return ret;
}

1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429
/* Must be in command mode when calling this function */
static int alps_absolute_mode_v4(struct psmouse *psmouse)
{
	int reg_val;

	reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
	if (reg_val == -1)
		return -1;

	reg_val |= 0x02;
	if (__alps_command_mode_write_reg(psmouse, reg_val))
		return -1;

	return 0;
}

static int alps_hw_init_v4(struct psmouse *psmouse)
{
	struct ps2dev *ps2dev = &psmouse->ps2dev;
	unsigned char param[4];

1430
	if (alps_enter_command_mode(psmouse))
1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497
		goto error;

	if (alps_absolute_mode_v4(psmouse)) {
		psmouse_err(psmouse, "Failed to enter absolute mode\n");
		goto error;
	}

	if (alps_command_mode_write_reg(psmouse, 0x0007, 0x8c))
		goto error;

	if (alps_command_mode_write_reg(psmouse, 0x0149, 0x03))
		goto error;

	if (alps_command_mode_write_reg(psmouse, 0x0160, 0x03))
		goto error;

	if (alps_command_mode_write_reg(psmouse, 0x017f, 0x15))
		goto error;

	if (alps_command_mode_write_reg(psmouse, 0x0151, 0x01))
		goto error;

	if (alps_command_mode_write_reg(psmouse, 0x0168, 0x03))
		goto error;

	if (alps_command_mode_write_reg(psmouse, 0x014a, 0x03))
		goto error;

	if (alps_command_mode_write_reg(psmouse, 0x0161, 0x03))
		goto error;

	alps_exit_command_mode(psmouse);

	/*
	 * This sequence changes the output from a 9-byte to an
	 * 8-byte format. All the same data seems to be present,
	 * just in a more compact format.
	 */
	param[0] = 0xc8;
	param[1] = 0x64;
	param[2] = 0x50;
	if (ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
	    ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE) ||
	    ps2_command(ps2dev, &param[2], PSMOUSE_CMD_SETRATE) ||
	    ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
		return -1;

	/* Set rate and enable data reporting */
	param[0] = 0x64;
	if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
		psmouse_err(psmouse, "Failed to enable data reporting\n");
		return -1;
	}

	return 0;

error:
	/*
	 * Leaving the touchpad in command mode will essentially render
	 * it unusable until the machine reboots, so exit it here just
	 * to be safe
	 */
	alps_exit_command_mode(psmouse);
	return -1;
}

1498
static void alps_set_defaults(struct alps_data *priv)
1499
{
1500 1501 1502 1503
	priv->byte0 = 0x8f;
	priv->mask0 = 0x8f;
	priv->flags = ALPS_DUALPOINT;

1504 1505 1506 1507 1508
	priv->x_max = 2000;
	priv->y_max = 1400;
	priv->x_bits = 15;
	priv->y_bits = 11;

1509
	switch (priv->proto_version) {
1510 1511
	case ALPS_PROTO_V1:
	case ALPS_PROTO_V2:
1512 1513 1514
		priv->hw_init = alps_hw_init_v1_v2;
		priv->process_packet = alps_process_packet_v1_v2;
		priv->set_abs_params = alps_set_abs_params_st;
1515 1516
		break;
	case ALPS_PROTO_V3:
1517 1518 1519
		priv->hw_init = alps_hw_init_v3;
		priv->process_packet = alps_process_packet_v3;
		priv->set_abs_params = alps_set_abs_params_mt;
1520
		priv->decode_fields = alps_decode_pinnacle;
1521 1522
		priv->nibble_commands = alps_v3_nibble_commands;
		priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
1523 1524
		break;
	case ALPS_PROTO_V4:
1525 1526 1527
		priv->hw_init = alps_hw_init_v4;
		priv->process_packet = alps_process_packet_v4;
		priv->set_abs_params = alps_set_abs_params_mt;
1528 1529
		priv->nibble_commands = alps_v4_nibble_commands;
		priv->addr_command = PSMOUSE_CMD_DISABLE;
1530 1531 1532 1533
		break;
	}
}

1534 1535
static int alps_match_table(struct psmouse *psmouse, struct alps_data *priv,
			    unsigned char *e7, unsigned char *ec)
1536
{
1537
	const struct alps_model_info *model;
1538 1539
	int i;

1540 1541 1542 1543 1544 1545 1546 1547
	for (i = 0; i < ARRAY_SIZE(alps_model_data); i++) {
		model = &alps_model_data[i];

		if (!memcmp(e7, model->signature, sizeof(model->signature)) &&
		    (!model->command_mode_resp ||
		     model->command_mode_resp == ec[2])) {

			priv->proto_version = model->proto_version;
1548 1549
			alps_set_defaults(priv);

1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564
			priv->flags = model->flags;
			priv->byte0 = model->byte0;
			priv->mask0 = model->mask0;

			return 0;
		}
	}

	return -EINVAL;
}

static int alps_identify(struct psmouse *psmouse, struct alps_data *priv)
{
	unsigned char e6[4], e7[4], ec[4];

1565 1566 1567 1568 1569 1570
	/*
	 * First try "E6 report".
	 * ALPS should return 0,0,10 or 0,0,100 if no buttons are pressed.
	 * The bits 0-2 of the first byte will be 1s if some buttons are
	 * pressed.
	 */
1571 1572 1573
	if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
			 PSMOUSE_CMD_SETSCALE11, e6))
		return -EIO;
1574

1575 1576
	if ((e6[0] & 0xf8) != 0 || e6[1] != 0 || (e6[2] != 10 && e6[2] != 100))
		return -EINVAL;
1577 1578

	/*
1579 1580
	 * Now get the "E7" and "EC" reports.  These will uniquely identify
	 * most ALPS touchpads.
1581
	 */
1582 1583 1584 1585 1586 1587
	if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
			 PSMOUSE_CMD_SETSCALE21, e7) ||
	    alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
			 PSMOUSE_CMD_RESET_WRAP, ec) ||
	    alps_exit_command_mode(psmouse))
		return -EIO;
1588

1589
	if (alps_match_table(psmouse, priv, e7, ec) == 0) {
1590 1591 1592 1593 1594 1595 1596 1597 1598 1599
		return 0;
	} else if (ec[0] == 0x88 && ec[1] == 0x08) {
		priv->proto_version = ALPS_PROTO_V3;
		alps_set_defaults(priv);

		priv->hw_init = alps_hw_init_rushmore_v3;
		priv->decode_fields = alps_decode_rushmore;
		priv->x_bits = 16;
		priv->y_bits = 12;

1600 1601 1602 1603 1604 1605
		/* hack to make addr_command, nibble_command available */
		psmouse->private = priv;

		if (alps_probe_trackstick_v3(psmouse, ALPS_REG_BASE_RUSHMORE))
			priv->flags &= ~ALPS_DUALPOINT;

1606 1607 1608 1609 1610 1611
		return 0;
	} else if (ec[0] == 0x88 && ec[1] == 0x07 &&
		   ec[2] >= 0x90 && ec[2] <= 0x9d) {
		priv->proto_version = ALPS_PROTO_V3;
		alps_set_defaults(priv);

1612
		return 0;
1613
	}
1614

1615 1616 1617
	psmouse_info(psmouse,
		"Unknown ALPS touchpad: E7=%2.2x %2.2x %2.2x, EC=%2.2x %2.2x %2.2x\n",
		e7[0], e7[1], e7[2], ec[0], ec[1], ec[2]);
1618

1619
	return -EINVAL;
1620 1621
}

1622 1623
static int alps_reconnect(struct psmouse *psmouse)
{
1624
	struct alps_data *priv = psmouse->private;
1625

1626 1627
	psmouse_reset(psmouse);

1628
	if (alps_identify(psmouse, priv) < 0)
1629 1630
		return -1;

1631
	return priv->hw_init(psmouse);
L
Linus Torvalds 已提交
1632 1633 1634 1635 1636
}

static void alps_disconnect(struct psmouse *psmouse)
{
	struct alps_data *priv = psmouse->private;
1637

L
Linus Torvalds 已提交
1638
	psmouse_reset(psmouse);
1639
	del_timer_sync(&priv->timer);
1640
	input_unregister_device(priv->dev2);
L
Linus Torvalds 已提交
1641 1642 1643
	kfree(priv);
}

1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655
static void alps_set_abs_params_st(struct alps_data *priv,
				   struct input_dev *dev1)
{
	input_set_abs_params(dev1, ABS_X, 0, 1023, 0, 0);
	input_set_abs_params(dev1, ABS_Y, 0, 767, 0, 0);
}

static void alps_set_abs_params_mt(struct alps_data *priv,
				   struct input_dev *dev1)
{
	set_bit(INPUT_PROP_SEMI_MT, dev1->propbit);
	input_mt_init_slots(dev1, 2, 0);
1656 1657
	input_set_abs_params(dev1, ABS_MT_POSITION_X, 0, priv->x_max, 0, 0);
	input_set_abs_params(dev1, ABS_MT_POSITION_Y, 0, priv->y_max, 0, 0);
1658 1659 1660 1661 1662

	set_bit(BTN_TOOL_DOUBLETAP, dev1->keybit);
	set_bit(BTN_TOOL_TRIPLETAP, dev1->keybit);
	set_bit(BTN_TOOL_QUADTAP, dev1->keybit);

1663 1664
	input_set_abs_params(dev1, ABS_X, 0, priv->x_max, 0, 0);
	input_set_abs_params(dev1, ABS_Y, 0, priv->y_max, 0, 0);
1665 1666
}

L
Linus Torvalds 已提交
1667 1668 1669
int alps_init(struct psmouse *psmouse)
{
	struct alps_data *priv;
1670
	struct input_dev *dev1 = psmouse->dev, *dev2;
L
Linus Torvalds 已提交
1671

1672
	priv = kzalloc(sizeof(struct alps_data), GFP_KERNEL);
1673 1674
	dev2 = input_allocate_device();
	if (!priv || !dev2)
L
Linus Torvalds 已提交
1675
		goto init_fail;
1676 1677

	priv->dev2 = dev2;
1678 1679
	setup_timer(&priv->timer, alps_flush_packet, (unsigned long)psmouse);

1680
	psmouse->private = priv;
L
Linus Torvalds 已提交
1681

1682 1683
	psmouse_reset(psmouse);

1684
	if (alps_identify(psmouse, priv) < 0)
1685 1686
		goto init_fail;

1687
	if (priv->hw_init(psmouse))
L
Linus Torvalds 已提交
1688 1689
		goto init_fail;

1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700
	/*
	 * Undo part of setup done for us by psmouse core since touchpad
	 * is not a relative device.
	 */
	__clear_bit(EV_REL, dev1->evbit);
	__clear_bit(REL_X, dev1->relbit);
	__clear_bit(REL_Y, dev1->relbit);

	/*
	 * Now set up our capabilities.
	 */
1701 1702 1703
	dev1->evbit[BIT_WORD(EV_KEY)] |= BIT_MASK(EV_KEY);
	dev1->keybit[BIT_WORD(BTN_TOUCH)] |= BIT_MASK(BTN_TOUCH);
	dev1->keybit[BIT_WORD(BTN_TOOL_FINGER)] |= BIT_MASK(BTN_TOOL_FINGER);
1704 1705
	dev1->keybit[BIT_WORD(BTN_LEFT)] |=
		BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
L
Linus Torvalds 已提交
1706

1707
	dev1->evbit[BIT_WORD(EV_ABS)] |= BIT_MASK(EV_ABS);
1708

1709
	priv->set_abs_params(priv, dev1);
1710
	input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
L
Linus Torvalds 已提交
1711

1712
	if (priv->flags & ALPS_WHEEL) {
1713 1714
		dev1->evbit[BIT_WORD(EV_REL)] |= BIT_MASK(EV_REL);
		dev1->relbit[BIT_WORD(REL_WHEEL)] |= BIT_MASK(REL_WHEEL);
L
Linus Torvalds 已提交
1715 1716
	}

1717
	if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
1718 1719
		dev1->keybit[BIT_WORD(BTN_FORWARD)] |= BIT_MASK(BTN_FORWARD);
		dev1->keybit[BIT_WORD(BTN_BACK)] |= BIT_MASK(BTN_BACK);
L
Linus Torvalds 已提交
1720 1721
	}

1722
	if (priv->flags & ALPS_FOUR_BUTTONS) {
1723 1724 1725 1726 1727 1728 1729 1730
		dev1->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_0);
		dev1->keybit[BIT_WORD(BTN_1)] |= BIT_MASK(BTN_1);
		dev1->keybit[BIT_WORD(BTN_2)] |= BIT_MASK(BTN_2);
		dev1->keybit[BIT_WORD(BTN_3)] |= BIT_MASK(BTN_3);
	} else {
		dev1->keybit[BIT_WORD(BTN_MIDDLE)] |= BIT_MASK(BTN_MIDDLE);
	}

1731
	snprintf(priv->phys, sizeof(priv->phys), "%s/input1", psmouse->ps2dev.serio->phys);
1732
	dev2->phys = priv->phys;
1733 1734
	dev2->name = (priv->flags & ALPS_DUALPOINT) ?
		     "DualPoint Stick" : "PS/2 Mouse";
1735 1736 1737 1738
	dev2->id.bustype = BUS_I8042;
	dev2->id.vendor  = 0x0002;
	dev2->id.product = PSMOUSE_ALPS;
	dev2->id.version = 0x0000;
1739
	dev2->dev.parent = &psmouse->ps2dev.serio->dev;
L
Linus Torvalds 已提交
1740

1741
	dev2->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
1742 1743 1744
	dev2->relbit[BIT_WORD(REL_X)] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
	dev2->keybit[BIT_WORD(BTN_LEFT)] =
		BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_MIDDLE) | BIT_MASK(BTN_RIGHT);
L
Linus Torvalds 已提交
1745

1746 1747
	if (input_register_device(priv->dev2))
		goto init_fail;
L
Linus Torvalds 已提交
1748 1749

	psmouse->protocol_handler = alps_process_byte;
1750
	psmouse->poll = alps_poll;
L
Linus Torvalds 已提交
1751 1752
	psmouse->disconnect = alps_disconnect;
	psmouse->reconnect = alps_reconnect;
1753
	psmouse->pktsize = priv->proto_version == ALPS_PROTO_V4 ? 8 : 6;
L
Linus Torvalds 已提交
1754

1755 1756 1757
	/* We are having trouble resyncing ALPS touchpads so disable it for now */
	psmouse->resync_time = 0;

L
Linus Torvalds 已提交
1758 1759 1760
	return 0;

init_fail:
1761
	psmouse_reset(psmouse);
1762
	input_free_device(dev2);
L
Linus Torvalds 已提交
1763
	kfree(priv);
1764
	psmouse->private = NULL;
L
Linus Torvalds 已提交
1765 1766 1767
	return -1;
}

1768
int alps_detect(struct psmouse *psmouse, bool set_properties)
L
Linus Torvalds 已提交
1769
{
1770
	struct alps_data dummy;
L
Linus Torvalds 已提交
1771

1772
	if (alps_identify(psmouse, &dummy) < 0)
L
Linus Torvalds 已提交
1773 1774 1775 1776
		return -1;

	if (set_properties) {
		psmouse->vendor = "ALPS";
1777
		psmouse->name = dummy.flags & ALPS_DUALPOINT ?
1778
				"DualPoint TouchPad" : "GlidePoint";
1779
		psmouse->model = dummy.proto_version << 8;
L
Linus Torvalds 已提交
1780 1781 1782 1783
	}
	return 0;
}