]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/input/mouse/alps.c
Input: alps - allow up to 2 invalid packets without resetting device
[mirror_ubuntu-zesty-kernel.git] / drivers / input / mouse / alps.c
CommitLineData
1da177e4
LT
1/*
2 * ALPS touchpad PS/2 mouse driver
3 *
4 * Copyright (c) 2003 Neil Brown <neilb@cse.unsw.edu.au>
963f626d 5 * Copyright (c) 2003-2005 Peter Osterlund <petero2@telia.com>
1da177e4
LT
6 * Copyright (c) 2004 Dmitry Torokhov <dtor@mail.ru>
7 * Copyright (c) 2005 Vojtech Pavlik <vojtech@suse.cz>
1d9f2626 8 * Copyright (c) 2009 Sebastian Kapfer <sebastian_kapfer@gmx.net>
1da177e4
LT
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
5a0e3ad6 18#include <linux/slab.h>
1da177e4 19#include <linux/input.h>
01ce661f 20#include <linux/input/mt.h>
1da177e4
LT
21#include <linux/serio.h>
22#include <linux/libps2.h>
23
24#include "psmouse.h"
25#include "alps.h"
26
25bded7c
SF
27/*
28 * Definitions for ALPS version 3 and 4 command mode protocol
29 */
30#define ALPS_CMD_NIBBLE_10 0x01f2
31
cd401204
KC
32#define ALPS_REG_BASE_RUSHMORE 0xc2c0
33#define ALPS_REG_BASE_PINNACLE 0x0000
34
25bded7c
SF
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
95f75e91
YT
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
25bded7c 92
71bb21b6
ML
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 */
1d9f2626
SK
100#define ALPS_PS2_INTERLEAVED 0x80 /* 3-byte PS/2 packet interleaved with
101 6-byte ALPS packet */
40e8f53b 102#define ALPS_IS_RUSHMORE 0x100 /* device is a rushmore */
3808843c 103#define ALPS_BUTTONPAD 0x200 /* device is a clickpad */
1da177e4 104
e38de678 105static const struct alps_model_info alps_model_data[] = {
25bded7c
SF
106 { { 0x32, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT }, /* Toshiba Salellite Pro M10 */
107 { { 0x33, 0x02, 0x0a }, 0x00, ALPS_PROTO_V1, 0x88, 0xf8, 0 }, /* UMAX-530T */
108 { { 0x53, 0x02, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
109 { { 0x53, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
110 { { 0x60, 0x03, 0xc8 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 }, /* HP ze1115 */
111 { { 0x63, 0x02, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
112 { { 0x63, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
113 { { 0x63, 0x02, 0x28 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 }, /* Fujitsu Siemens S6010 */
114 { { 0x63, 0x02, 0x3c }, 0x00, ALPS_PROTO_V2, 0x8f, 0x8f, ALPS_WHEEL }, /* Toshiba Satellite S2400-103 */
115 { { 0x63, 0x02, 0x50 }, 0x00, ALPS_PROTO_V2, 0xef, 0xef, ALPS_FW_BK_1 }, /* NEC Versa L320 */
116 { { 0x63, 0x02, 0x64 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
117 { { 0x63, 0x03, 0xc8 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT }, /* Dell Latitude D800 */
118 { { 0x73, 0x00, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_DUALPOINT }, /* ThinkPad R61 8918-5QG */
119 { { 0x73, 0x02, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
120 { { 0x73, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 }, /* Ahtec Laptop */
121 { { 0x20, 0x02, 0x0e }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT }, /* XXX */
122 { { 0x22, 0x02, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT },
123 { { 0x22, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xff, 0xff, ALPS_PASS | ALPS_DUALPOINT }, /* Dell Latitude D600 */
1d9f2626 124 /* Dell Latitude E5500, E6400, E6500, Precision M4400 */
25bded7c 125 { { 0x62, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xcf, 0xcf,
1d9f2626 126 ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED },
95f75e91 127 { { 0x73, 0x00, 0x14 }, 0x00, ALPS_PROTO_V6, 0xff, 0xff, ALPS_DUALPOINT }, /* Dell XT2 */
25bded7c
SF
128 { { 0x73, 0x02, 0x50 }, 0x00, ALPS_PROTO_V2, 0xcf, 0xcf, ALPS_FOUR_BUTTONS }, /* Dell Vostro 1400 */
129 { { 0x52, 0x01, 0x14 }, 0x00, ALPS_PROTO_V2, 0xff, 0xff,
130 ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED }, /* Toshiba Tecra A11-11L */
25bded7c 131 { { 0x73, 0x02, 0x64 }, 0x8a, ALPS_PROTO_V4, 0x8f, 0x8f, 0 },
1da177e4
LT
132};
133
24af5cb9
KC
134static void alps_set_abs_params_st(struct alps_data *priv,
135 struct input_dev *dev1);
136static void alps_set_abs_params_mt(struct alps_data *priv,
137 struct input_dev *dev1);
138
1da177e4
LT
139/*
140 * XXX - this entry is suspicious. First byte has zero lower nibble,
141 * which is what a normal mouse would report. Also, the value 0x0e
142 * isn't valid per PS/2 spec.
143 */
144
d4b347b2 145/* Packet formats are described in Documentation/input/alps.txt */
1da177e4 146
99df65e7 147static bool alps_is_valid_first_byte(struct alps_data *priv,
1d9f2626
SK
148 unsigned char data)
149{
99df65e7 150 return (data & priv->mask0) == priv->byte0;
1d9f2626
SK
151}
152
153static void alps_report_buttons(struct psmouse *psmouse,
154 struct input_dev *dev1, struct input_dev *dev2,
155 int left, int right, int middle)
156{
c91ed059
MB
157 struct input_dev *dev;
158
159 /*
160 * If shared button has already been reported on the
161 * other device (dev2) then this event should be also
162 * sent through that device.
163 */
164 dev = test_bit(BTN_LEFT, dev2->key) ? dev2 : dev1;
165 input_report_key(dev, BTN_LEFT, left);
166
167 dev = test_bit(BTN_RIGHT, dev2->key) ? dev2 : dev1;
168 input_report_key(dev, BTN_RIGHT, right);
169
170 dev = test_bit(BTN_MIDDLE, dev2->key) ? dev2 : dev1;
171 input_report_key(dev, BTN_MIDDLE, middle);
172
173 /*
174 * Sync the _other_ device now, we'll do the first
175 * device later once we report the rest of the events.
176 */
177 input_sync(dev2);
1d9f2626
SK
178}
179
25bded7c 180static void alps_process_packet_v1_v2(struct psmouse *psmouse)
1da177e4
LT
181{
182 struct alps_data *priv = psmouse->private;
183 unsigned char *packet = psmouse->packet;
2e5b636b
DT
184 struct input_dev *dev = psmouse->dev;
185 struct input_dev *dev2 = priv->dev2;
1da177e4 186 int x, y, z, ges, fin, left, right, middle;
c30b4c10 187 int back = 0, forward = 0;
1da177e4 188
99df65e7 189 if (priv->proto_version == ALPS_PROTO_V1) {
d2f4012f
YM
190 left = packet[2] & 0x10;
191 right = packet[2] & 0x08;
1da177e4
LT
192 middle = 0;
193 x = packet[1] | ((packet[0] & 0x07) << 7);
194 y = packet[4] | ((packet[3] & 0x07) << 7);
195 z = packet[5];
196 } else {
197 left = packet[3] & 1;
198 right = packet[3] & 2;
199 middle = packet[3] & 4;
200 x = packet[1] | ((packet[2] & 0x78) << (7 - 3));
201 y = packet[4] | ((packet[3] & 0x70) << (7 - 4));
202 z = packet[5];
203 }
204
99df65e7 205 if (priv->flags & ALPS_FW_BK_1) {
3c00bb96
LK
206 back = packet[0] & 0x10;
207 forward = packet[2] & 4;
c30b4c10
ICR
208 }
209
99df65e7 210 if (priv->flags & ALPS_FW_BK_2) {
c30b4c10
ICR
211 back = packet[3] & 4;
212 forward = packet[2] & 4;
213 if ((middle = forward && back))
214 forward = back = 0;
215 }
216
1da177e4
LT
217 ges = packet[2] & 1;
218 fin = packet[2] & 2;
219
99df65e7 220 if ((priv->flags & ALPS_DUALPOINT) && z == 127) {
1da177e4
LT
221 input_report_rel(dev2, REL_X, (x > 383 ? (x - 768) : x));
222 input_report_rel(dev2, REL_Y, -(y > 255 ? (y - 512) : y));
d7ed5d88 223
1d9f2626 224 alps_report_buttons(psmouse, dev2, dev, left, right, middle);
d7ed5d88 225
1da177e4
LT
226 input_sync(dev2);
227 return;
228 }
229
1d9f2626 230 alps_report_buttons(psmouse, dev, dev2, left, right, middle);
d7ed5d88 231
1da177e4 232 /* Convert hardware tap to a reasonable Z value */
71bb21b6
ML
233 if (ges && !fin)
234 z = 40;
1da177e4
LT
235
236 /*
237 * A "tap and drag" operation is reported by the hardware as a transition
238 * from (!fin && ges) to (fin && ges). This should be translated to the
239 * sequence Z>0, Z==0, Z>0, so the Z==0 event has to be generated manually.
240 */
241 if (ges && fin && !priv->prev_fin) {
242 input_report_abs(dev, ABS_X, x);
243 input_report_abs(dev, ABS_Y, y);
244 input_report_abs(dev, ABS_PRESSURE, 0);
245 input_report_key(dev, BTN_TOOL_FINGER, 0);
246 input_sync(dev);
247 }
248 priv->prev_fin = fin;
249
71bb21b6
ML
250 if (z > 30)
251 input_report_key(dev, BTN_TOUCH, 1);
252 if (z < 25)
253 input_report_key(dev, BTN_TOUCH, 0);
1da177e4
LT
254
255 if (z > 0) {
256 input_report_abs(dev, ABS_X, x);
257 input_report_abs(dev, ABS_Y, y);
258 }
259
260 input_report_abs(dev, ABS_PRESSURE, z);
261 input_report_key(dev, BTN_TOOL_FINGER, z > 0);
262
99df65e7 263 if (priv->flags & ALPS_WHEEL)
e6c047b9 264 input_report_rel(dev, REL_WHEEL, ((packet[2] << 1) & 0x08) - ((packet[0] >> 4) & 0x07));
1da177e4 265
99df65e7 266 if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
c30b4c10
ICR
267 input_report_key(dev, BTN_FORWARD, forward);
268 input_report_key(dev, BTN_BACK, back);
1da177e4
LT
269 }
270
99df65e7 271 if (priv->flags & ALPS_FOUR_BUTTONS) {
71bb21b6
ML
272 input_report_key(dev, BTN_0, packet[2] & 4);
273 input_report_key(dev, BTN_1, packet[0] & 0x10);
274 input_report_key(dev, BTN_2, packet[3] & 4);
275 input_report_key(dev, BTN_3, packet[0] & 0x20);
276 }
277
1da177e4
LT
278 input_sync(dev);
279}
280
ee65d4b3
YT
281/*
282 * Process bitmap data for V5 protocols. Return value is null.
283 *
284 * The bitmaps don't have enough data to track fingers, so this function
285 * only generates points representing a bounding box of at most two contacts.
02d04254 286 * These two points are returned in fields->mt.
ee65d4b3
YT
287 */
288static void alps_process_bitmap_dolphin(struct alps_data *priv,
02d04254 289 struct alps_fields *fields)
ee65d4b3
YT
290{
291 int box_middle_x, box_middle_y;
292 unsigned int x_map, y_map;
293 unsigned char start_bit, end_bit;
294 unsigned char x_msb, x_lsb, y_msb, y_lsb;
295
296 x_map = fields->x_map;
297 y_map = fields->y_map;
298
299 if (!x_map || !y_map)
300 return;
301
302 /* Get Most-significant and Least-significant bit */
303 x_msb = fls(x_map);
304 x_lsb = ffs(x_map);
305 y_msb = fls(y_map);
306 y_lsb = ffs(y_map);
307
308 /* Most-significant bit should never exceed max sensor line number */
309 if (x_msb > priv->x_bits || y_msb > priv->y_bits)
310 return;
311
ee65d4b3
YT
312 if (fields->fingers > 1) {
313 start_bit = priv->x_bits - x_msb;
314 end_bit = priv->x_bits - x_lsb;
315 box_middle_x = (priv->x_max * (start_bit + end_bit)) /
316 (2 * (priv->x_bits - 1));
317
318 start_bit = y_lsb - 1;
319 end_bit = y_msb - 1;
320 box_middle_y = (priv->y_max * (start_bit + end_bit)) /
321 (2 * (priv->y_bits - 1));
02d04254
HG
322 fields->mt[0] = fields->st;
323 fields->mt[1].x = 2 * box_middle_x - fields->mt[0].x;
324 fields->mt[1].y = 2 * box_middle_y - fields->mt[0].y;
ee65d4b3
YT
325 }
326}
327
036e6c7b
HG
328static void alps_get_bitmap_points(unsigned int map,
329 struct alps_bitmap_point *low,
330 struct alps_bitmap_point *high,
331 int *fingers)
332{
333 struct alps_bitmap_point *point;
334 int i, bit, prev_bit = 0;
335
336 point = low;
337 for (i = 0; map != 0; i++, map >>= 1) {
338 bit = map & 1;
339 if (bit) {
340 if (!prev_bit) {
341 point->start_bit = i;
105affbf 342 point->num_bits = 0;
036e6c7b
HG
343 (*fingers)++;
344 }
345 point->num_bits++;
346 } else {
347 if (prev_bit)
348 point = high;
036e6c7b
HG
349 }
350 prev_bit = bit;
351 }
352}
353
01ce661f
SF
354/*
355 * Process bitmap data from v3 and v4 protocols. Returns the number of
356 * fingers detected. A return value of 0 means at least one of the
357 * bitmaps was empty.
358 *
359 * The bitmaps don't have enough data to track fingers, so this function
360 * only generates points representing a bounding box of all contacts.
02d04254 361 * These points are returned in fields->mt when the return value
01ce661f
SF
362 * is greater than 0.
363 */
7a9f73e7 364static int alps_process_bitmap(struct alps_data *priv,
02d04254 365 struct alps_fields *fields)
01ce661f 366{
036e6c7b 367 int i, fingers_x = 0, fingers_y = 0, fingers;
01ce661f
SF
368 struct alps_bitmap_point x_low = {0,}, x_high = {0,};
369 struct alps_bitmap_point y_low = {0,}, y_high = {0,};
01ce661f 370
02d04254 371 if (!fields->x_map || !fields->y_map)
01ce661f
SF
372 return 0;
373
02d04254
HG
374 alps_get_bitmap_points(fields->x_map, &x_low, &x_high, &fingers_x);
375 alps_get_bitmap_points(fields->y_map, &y_low, &y_high, &fingers_y);
01ce661f
SF
376
377 /*
378 * Fingers can overlap, so we use the maximum count of fingers
379 * on either axis as the finger count.
380 */
381 fingers = max(fingers_x, fingers_y);
382
383 /*
20bea68b
HG
384 * If an axis reports only a single contact, we have overlapping or
385 * adjacent fingers. Divide the single contact between the two points.
01ce661f 386 */
20bea68b 387 if (fingers_x == 1) {
28835f45 388 i = (x_low.num_bits - 1) / 2;
20bea68b
HG
389 x_low.num_bits = x_low.num_bits - i;
390 x_high.start_bit = x_low.start_bit + i;
391 x_high.num_bits = max(i, 1);
392 }
393 if (fingers_y == 1) {
28835f45 394 i = (y_low.num_bits - 1) / 2;
20bea68b
HG
395 y_low.num_bits = y_low.num_bits - i;
396 y_high.start_bit = y_low.start_bit + i;
397 y_high.num_bits = max(i, 1);
01ce661f
SF
398 }
399
02d04254
HG
400 fields->mt[0].x =
401 (priv->x_max * (2 * x_low.start_bit + x_low.num_bits - 1)) /
402 (2 * (priv->x_bits - 1));
403 fields->mt[0].y =
404 (priv->y_max * (2 * y_low.start_bit + y_low.num_bits - 1)) /
405 (2 * (priv->y_bits - 1));
01ce661f 406
02d04254
HG
407 fields->mt[1].x =
408 (priv->x_max * (2 * x_high.start_bit + x_high.num_bits - 1)) /
409 (2 * (priv->x_bits - 1));
410 fields->mt[1].y =
411 (priv->y_max * (2 * y_high.start_bit + y_high.num_bits - 1)) /
412 (2 * (priv->y_bits - 1));
01ce661f 413
40e8f53b
HG
414 /* y-bitmap order is reversed, except on rushmore */
415 if (!(priv->flags & ALPS_IS_RUSHMORE)) {
02d04254
HG
416 fields->mt[0].y = priv->y_max - fields->mt[0].y;
417 fields->mt[1].y = priv->y_max - fields->mt[1].y;
40e8f53b
HG
418 }
419
01ce661f
SF
420 return fingers;
421}
422
cdf333ef 423static void alps_set_slot(struct input_dev *dev, int slot, int x, int y)
01ce661f
SF
424{
425 input_mt_slot(dev, slot);
cdf333ef
HG
426 input_mt_report_slot_state(dev, MT_TOOL_FINGER, true);
427 input_report_abs(dev, ABS_MT_POSITION_X, x);
428 input_report_abs(dev, ABS_MT_POSITION_Y, y);
01ce661f
SF
429}
430
cdf333ef 431static void alps_report_mt_data(struct psmouse *psmouse, int n)
01ce661f 432{
02d04254
HG
433 struct alps_data *priv = psmouse->private;
434 struct input_dev *dev = psmouse->dev;
435 struct alps_fields *f = &priv->f;
cdf333ef
HG
436 int i, slot[MAX_TOUCHES];
437
438 input_mt_assign_slots(dev, slot, f->mt, n);
439 for (i = 0; i < n; i++)
440 alps_set_slot(dev, slot[i], f->mt[i].x, f->mt[i].y);
02d04254 441
cdf333ef 442 input_mt_sync_frame(dev);
01ce661f
SF
443}
444
68c21870
HG
445static void alps_report_semi_mt_data(struct psmouse *psmouse, int fingers)
446{
447 struct alps_data *priv = psmouse->private;
448 struct input_dev *dev = psmouse->dev;
449 struct alps_fields *f = &priv->f;
450
451 /* Use st data when we don't have mt data */
452 if (fingers < 2) {
453 f->mt[0].x = f->st.x;
454 f->mt[0].y = f->st.y;
455 fingers = f->pressure > 0 ? 1 : 0;
456 }
457
99d9996c 458 alps_report_mt_data(psmouse, (fingers <= 2) ? fingers : 2);
68c21870
HG
459
460 input_mt_report_finger_count(dev, fingers);
461
462 input_report_key(dev, BTN_LEFT, f->left);
463 input_report_key(dev, BTN_RIGHT, f->right);
464 input_report_key(dev, BTN_MIDDLE, f->middle);
465
466 input_report_abs(dev, ABS_PRESSURE, f->pressure);
467
468 input_sync(dev);
469}
470
25bded7c
SF
471static void alps_process_trackstick_packet_v3(struct psmouse *psmouse)
472{
473 struct alps_data *priv = psmouse->private;
474 unsigned char *packet = psmouse->packet;
475 struct input_dev *dev = priv->dev2;
476 int x, y, z, left, right, middle;
477
478 /* Sanity check packet */
479 if (!(packet[0] & 0x40)) {
480 psmouse_dbg(psmouse, "Bad trackstick packet, discarding\n");
481 return;
482 }
483
484 /*
485 * There's a special packet that seems to indicate the end
486 * of a stream of trackstick data. Filter these out.
487 */
488 if (packet[1] == 0x7f && packet[2] == 0x7f && packet[4] == 0x7f)
489 return;
490
491 x = (s8)(((packet[0] & 0x20) << 2) | (packet[1] & 0x7f));
492 y = (s8)(((packet[0] & 0x10) << 3) | (packet[2] & 0x7f));
493 z = (packet[4] & 0x7c) >> 2;
494
495 /*
496 * The x and y values tend to be quite large, and when used
497 * alone the trackstick is difficult to use. Scale them down
498 * to compensate.
499 */
500 x /= 8;
501 y /= 8;
502
503 input_report_rel(dev, REL_X, x);
504 input_report_rel(dev, REL_Y, -y);
505
506 /*
507 * Most ALPS models report the trackstick buttons in the touchpad
508 * packets, but a few report them here. No reliable way has been
509 * found to differentiate between the models upfront, so we enable
510 * the quirk in response to seeing a button press in the trackstick
511 * packet.
512 */
513 left = packet[3] & 0x01;
514 right = packet[3] & 0x02;
515 middle = packet[3] & 0x04;
516
517 if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) &&
518 (left || right || middle))
519 priv->quirks |= ALPS_QUIRK_TRACKSTICK_BUTTONS;
520
521 if (priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) {
522 input_report_key(dev, BTN_LEFT, left);
523 input_report_key(dev, BTN_RIGHT, right);
524 input_report_key(dev, BTN_MIDDLE, middle);
525 }
526
527 input_sync(dev);
528 return;
529}
530
f85e5001
KC
531static void alps_decode_buttons_v3(struct alps_fields *f, unsigned char *p)
532{
533 f->left = !!(p[3] & 0x01);
534 f->right = !!(p[3] & 0x02);
535 f->middle = !!(p[3] & 0x04);
536
537 f->ts_left = !!(p[3] & 0x10);
538 f->ts_right = !!(p[3] & 0x20);
539 f->ts_middle = !!(p[3] & 0x40);
540}
541
38c11eaa 542static int alps_decode_pinnacle(struct alps_fields *f, unsigned char *p,
ee65d4b3 543 struct psmouse *psmouse)
f85e5001
KC
544{
545 f->first_mp = !!(p[4] & 0x40);
546 f->is_mp = !!(p[0] & 0x40);
547
548 f->fingers = (p[5] & 0x3) + 1;
549 f->x_map = ((p[4] & 0x7e) << 8) |
550 ((p[1] & 0x7f) << 2) |
551 ((p[0] & 0x30) >> 4);
552 f->y_map = ((p[3] & 0x70) << 4) |
553 ((p[2] & 0x7f) << 1) |
554 (p[4] & 0x01);
555
02d04254 556 f->st.x = ((p[1] & 0x7f) << 4) | ((p[4] & 0x30) >> 2) |
f85e5001 557 ((p[0] & 0x30) >> 4);
02d04254
HG
558 f->st.y = ((p[2] & 0x7f) << 4) | (p[4] & 0x0f);
559 f->pressure = p[5] & 0x7f;
f85e5001
KC
560
561 alps_decode_buttons_v3(f, p);
38c11eaa
HG
562
563 return 0;
f85e5001
KC
564}
565
38c11eaa 566static int alps_decode_rushmore(struct alps_fields *f, unsigned char *p,
ee65d4b3 567 struct psmouse *psmouse)
1302bac3 568{
ee65d4b3 569 alps_decode_pinnacle(f, p, psmouse);
1302bac3 570
f105e34a
YT
571 /* Rushmore's packet decode has a bit difference with Pinnacle's */
572 f->is_mp = !!(p[5] & 0x40);
573 f->fingers = max((p[5] & 0x3), ((p[5] >> 2) & 0x3)) + 1;
1302bac3
KC
574 f->x_map |= (p[5] & 0x10) << 11;
575 f->y_map |= (p[5] & 0x20) << 6;
38c11eaa
HG
576
577 return 0;
1302bac3
KC
578}
579
38c11eaa 580static int alps_decode_dolphin(struct alps_fields *f, unsigned char *p,
ee65d4b3 581 struct psmouse *psmouse)
75af9e56 582{
ee65d4b3
YT
583 u64 palm_data = 0;
584 struct alps_data *priv = psmouse->private;
585
75af9e56
DT
586 f->first_mp = !!(p[0] & 0x02);
587 f->is_mp = !!(p[0] & 0x20);
588
ee65d4b3 589 if (!f->is_mp) {
02d04254
HG
590 f->st.x = ((p[1] & 0x7f) | ((p[4] & 0x0f) << 7));
591 f->st.y = ((p[2] & 0x7f) | ((p[4] & 0xf0) << 3));
592 f->pressure = (p[0] & 4) ? 0 : p[5] & 0x7f;
ee65d4b3
YT
593 alps_decode_buttons_v3(f, p);
594 } else {
595 f->fingers = ((p[0] & 0x6) >> 1 |
75af9e56 596 (p[0] & 0x10) >> 2);
75af9e56 597
ee65d4b3
YT
598 palm_data = (p[1] & 0x7f) |
599 ((p[2] & 0x7f) << 7) |
600 ((p[4] & 0x7f) << 14) |
601 ((p[5] & 0x7f) << 21) |
602 ((p[3] & 0x07) << 28) |
603 (((u64)p[3] & 0x70) << 27) |
604 (((u64)p[0] & 0x01) << 34);
605
606 /* Y-profile is stored in P(0) to p(n-1), n = y_bits; */
607 f->y_map = palm_data & (BIT(priv->y_bits) - 1);
608
609 /* X-profile is stored in p(n) to p(n+m-1), m = x_bits; */
610 f->x_map = (palm_data >> priv->y_bits) &
611 (BIT(priv->x_bits) - 1);
612 }
38c11eaa
HG
613
614 return 0;
75af9e56
DT
615}
616
ee65d4b3 617static void alps_process_touchpad_packet_v3_v5(struct psmouse *psmouse)
25bded7c
SF
618{
619 struct alps_data *priv = psmouse->private;
620 unsigned char *packet = psmouse->packet;
25bded7c 621 struct input_dev *dev2 = priv->dev2;
02d04254
HG
622 struct alps_fields *f = &priv->f;
623 int fingers = 0;
624
625 memset(f, 0, sizeof(*f));
f85e5001 626
02d04254 627 priv->decode_fields(f, packet, psmouse);
25bded7c
SF
628
629 /*
01ce661f
SF
630 * There's no single feature of touchpad position and bitmap packets
631 * that can be used to distinguish between them. We rely on the fact
632 * that a bitmap packet should always follow a position packet with
633 * bit 6 of packet[4] set.
25bded7c
SF
634 */
635 if (priv->multi_packet) {
25bded7c
SF
636 /*
637 * Sometimes a position packet will indicate a multi-packet
638 * sequence, but then what follows is another position
639 * packet. Check for this, and when it happens process the
640 * position packet as usual.
641 */
02d04254
HG
642 if (f->is_mp) {
643 fingers = f->fingers;
ee65d4b3 644 if (priv->proto_version == ALPS_PROTO_V3) {
02d04254 645 if (alps_process_bitmap(priv, f) == 0)
20bea68b 646 fingers = 0; /* Use st data */
ee65d4b3
YT
647
648 /* Now process position packet */
02d04254 649 priv->decode_fields(f, priv->multi_data,
ee65d4b3
YT
650 psmouse);
651 } else {
652 /*
653 * Because Dolphin uses position packet's
654 * coordinate data as Pt1 and uses it to
655 * calculate Pt2, so we need to do position
656 * packet decode first.
657 */
02d04254 658 priv->decode_fields(f, priv->multi_data,
ee65d4b3
YT
659 psmouse);
660
661 /*
662 * Since Dolphin's finger number is reliable,
663 * there is no need to compare with bmap_fn.
664 */
02d04254 665 alps_process_bitmap_dolphin(priv, f);
ee65d4b3 666 }
01ce661f
SF
667 } else {
668 priv->multi_packet = 0;
25bded7c
SF
669 }
670 }
671
01ce661f
SF
672 /*
673 * Bit 6 of byte 0 is not usually set in position packets. The only
674 * times it seems to be set is in situations where the data is
675 * suspect anyway, e.g. a palm resting flat on the touchpad. Given
676 * this combined with the fact that this bit is useful for filtering
677 * out misidentified bitmap packets, we reject anything with this
678 * bit set.
679 */
02d04254 680 if (f->is_mp)
01ce661f
SF
681 return;
682
02d04254 683 if (!priv->multi_packet && f->first_mp) {
25bded7c 684 priv->multi_packet = 1;
01ce661f
SF
685 memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
686 return;
687 }
688
689 priv->multi_packet = 0;
25bded7c 690
25bded7c
SF
691 /*
692 * Sometimes the hardware sends a single packet with z = 0
693 * in the middle of a stream. Real releases generate packets
694 * with x, y, and z all zero, so these seem to be flukes.
695 * Ignore them.
696 */
02d04254 697 if (f->st.x && f->st.y && !f->pressure)
25bded7c
SF
698 return;
699
68c21870 700 alps_report_semi_mt_data(psmouse, fingers);
25bded7c
SF
701
702 if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS)) {
02d04254
HG
703 input_report_key(dev2, BTN_LEFT, f->ts_left);
704 input_report_key(dev2, BTN_RIGHT, f->ts_right);
705 input_report_key(dev2, BTN_MIDDLE, f->ts_middle);
25bded7c
SF
706 input_sync(dev2);
707 }
708}
709
710static void alps_process_packet_v3(struct psmouse *psmouse)
711{
712 unsigned char *packet = psmouse->packet;
713
714 /*
715 * v3 protocol packets come in three types, two representing
716 * touchpad data and one representing trackstick data.
717 * Trackstick packets seem to be distinguished by always
718 * having 0x3f in the last byte. This value has never been
719 * observed in the last byte of either of the other types
720 * of packets.
721 */
722 if (packet[5] == 0x3f) {
723 alps_process_trackstick_packet_v3(psmouse);
724 return;
725 }
726
ee65d4b3 727 alps_process_touchpad_packet_v3_v5(psmouse);
25bded7c
SF
728}
729
95f75e91
YT
730static void alps_process_packet_v6(struct psmouse *psmouse)
731{
732 struct alps_data *priv = psmouse->private;
733 unsigned char *packet = psmouse->packet;
734 struct input_dev *dev = psmouse->dev;
735 struct input_dev *dev2 = priv->dev2;
736 int x, y, z, left, right, middle;
737
738 /*
739 * We can use Byte5 to distinguish if the packet is from Touchpad
740 * or Trackpoint.
741 * Touchpad: 0 - 0x7E
742 * Trackpoint: 0x7F
743 */
744 if (packet[5] == 0x7F) {
745 /* It should be a DualPoint when received Trackpoint packet */
746 if (!(priv->flags & ALPS_DUALPOINT))
747 return;
748
749 /* Trackpoint packet */
750 x = packet[1] | ((packet[3] & 0x20) << 2);
751 y = packet[2] | ((packet[3] & 0x40) << 1);
752 z = packet[4];
753 left = packet[3] & 0x01;
754 right = packet[3] & 0x02;
755 middle = packet[3] & 0x04;
756
757 /* To prevent the cursor jump when finger lifted */
758 if (x == 0x7F && y == 0x7F && z == 0x7F)
759 x = y = z = 0;
760
761 /* Divide 4 since trackpoint's speed is too fast */
762 input_report_rel(dev2, REL_X, (char)x / 4);
763 input_report_rel(dev2, REL_Y, -((char)y / 4));
764
765 input_report_key(dev2, BTN_LEFT, left);
766 input_report_key(dev2, BTN_RIGHT, right);
767 input_report_key(dev2, BTN_MIDDLE, middle);
768
769 input_sync(dev2);
770 return;
771 }
772
773 /* Touchpad packet */
774 x = packet[1] | ((packet[3] & 0x78) << 4);
775 y = packet[2] | ((packet[4] & 0x78) << 4);
776 z = packet[5];
777 left = packet[3] & 0x01;
778 right = packet[3] & 0x02;
779
780 if (z > 30)
781 input_report_key(dev, BTN_TOUCH, 1);
782 if (z < 25)
783 input_report_key(dev, BTN_TOUCH, 0);
784
785 if (z > 0) {
786 input_report_abs(dev, ABS_X, x);
787 input_report_abs(dev, ABS_Y, y);
788 }
789
790 input_report_abs(dev, ABS_PRESSURE, z);
791 input_report_key(dev, BTN_TOOL_FINGER, z > 0);
792
793 /* v6 touchpad does not have middle button */
794 input_report_key(dev, BTN_LEFT, left);
795 input_report_key(dev, BTN_RIGHT, right);
796
797 input_sync(dev);
798}
799
25bded7c
SF
800static void alps_process_packet_v4(struct psmouse *psmouse)
801{
3b7e09fa 802 struct alps_data *priv = psmouse->private;
25bded7c 803 unsigned char *packet = psmouse->packet;
02d04254 804 struct alps_fields *f = &priv->f;
68c21870 805 int offset;
3b7e09fa
GP
806
807 /*
808 * v4 has a 6-byte encoding for bitmap data, but this data is
809 * broken up between 3 normal packets. Use priv->multi_packet to
810 * track our position in the bitmap packet.
811 */
812 if (packet[6] & 0x40) {
813 /* sync, reset position */
814 priv->multi_packet = 0;
815 }
816
817 if (WARN_ON_ONCE(priv->multi_packet > 2))
818 return;
819
820 offset = 2 * priv->multi_packet;
821 priv->multi_data[offset] = packet[6];
822 priv->multi_data[offset + 1] = packet[7];
823
824 if (++priv->multi_packet > 2) {
825 priv->multi_packet = 0;
826
02d04254 827 f->x_map = ((priv->multi_data[2] & 0x1f) << 10) |
3b7e09fa
GP
828 ((priv->multi_data[3] & 0x60) << 3) |
829 ((priv->multi_data[0] & 0x3f) << 2) |
830 ((priv->multi_data[1] & 0x60) >> 5);
02d04254 831 f->y_map = ((priv->multi_data[5] & 0x01) << 10) |
3b7e09fa
GP
832 ((priv->multi_data[3] & 0x1f) << 5) |
833 (priv->multi_data[1] & 0x1f);
834
02d04254 835 f->fingers = alps_process_bitmap(priv, f);
3b7e09fa 836 }
25bded7c 837
b0cfb794
AB
838 f->left = !!(packet[4] & 0x01);
839 f->right = !!(packet[4] & 0x02);
25bded7c 840
02d04254
HG
841 f->st.x = ((packet[1] & 0x7f) << 4) | ((packet[3] & 0x30) >> 2) |
842 ((packet[0] & 0x30) >> 4);
843 f->st.y = ((packet[2] & 0x7f) << 4) | (packet[3] & 0x0f);
844 f->pressure = packet[5] & 0x7f;
25bded7c 845
68c21870 846 alps_report_semi_mt_data(psmouse, f->fingers);
25bded7c
SF
847}
848
3808843c
YT
849static bool alps_is_valid_package_v7(struct psmouse *psmouse)
850{
851 switch (psmouse->pktcnt) {
852 case 3:
853 return (psmouse->packet[2] & 0x40) == 0x40;
854 case 4:
855 return (psmouse->packet[3] & 0x48) == 0x48;
856 case 6:
857 return (psmouse->packet[5] & 0x40) == 0x00;
858 }
859 return true;
860}
861
862static unsigned char alps_get_packet_id_v7(char *byte)
863{
864 unsigned char packet_id;
865
866 if (byte[4] & 0x40)
867 packet_id = V7_PACKET_ID_TWO;
868 else if (byte[4] & 0x01)
869 packet_id = V7_PACKET_ID_MULTI;
870 else if ((byte[0] & 0x10) && !(byte[4] & 0x43))
871 packet_id = V7_PACKET_ID_NEW;
872 else if (byte[1] == 0x00 && byte[4] == 0x00)
873 packet_id = V7_PACKET_ID_IDLE;
874 else
875 packet_id = V7_PACKET_ID_UNKNOWN;
876
877 return packet_id;
878}
879
880static void alps_get_finger_coordinate_v7(struct input_mt_pos *mt,
881 unsigned char *pkt,
882 unsigned char pkt_id)
883{
884 mt[0].x = ((pkt[2] & 0x80) << 4);
885 mt[0].x |= ((pkt[2] & 0x3F) << 5);
886 mt[0].x |= ((pkt[3] & 0x30) >> 1);
887 mt[0].x |= (pkt[3] & 0x07);
888 mt[0].y = (pkt[1] << 3) | (pkt[0] & 0x07);
889
890 mt[1].x = ((pkt[3] & 0x80) << 4);
891 mt[1].x |= ((pkt[4] & 0x80) << 3);
892 mt[1].x |= ((pkt[4] & 0x3F) << 4);
893 mt[1].y = ((pkt[5] & 0x80) << 3);
894 mt[1].y |= ((pkt[5] & 0x3F) << 4);
895
896 switch (pkt_id) {
897 case V7_PACKET_ID_TWO:
898 mt[1].x &= ~0x000F;
899 mt[1].y |= 0x000F;
900 break;
901
902 case V7_PACKET_ID_MULTI:
903 mt[1].x &= ~0x003F;
904 mt[1].y &= ~0x0020;
905 mt[1].y |= ((pkt[4] & 0x02) << 4);
906 mt[1].y |= 0x001F;
907 break;
908
909 case V7_PACKET_ID_NEW:
910 mt[1].x &= ~0x003F;
911 mt[1].x |= (pkt[0] & 0x20);
912 mt[1].y |= 0x000F;
913 break;
914 }
915
916 mt[0].y = 0x7FF - mt[0].y;
917 mt[1].y = 0x7FF - mt[1].y;
918}
919
920static int alps_get_mt_count(struct input_mt_pos *mt)
921{
922 int i;
923
924 for (i = 0; i < MAX_TOUCHES && mt[i].x != 0 && mt[i].y != 0; i++)
925 /* empty */;
926
927 return i;
928}
929
930static int alps_decode_packet_v7(struct alps_fields *f,
931 unsigned char *p,
932 struct psmouse *psmouse)
933{
934 unsigned char pkt_id;
935
936 pkt_id = alps_get_packet_id_v7(p);
937 if (pkt_id == V7_PACKET_ID_IDLE)
938 return 0;
939 if (pkt_id == V7_PACKET_ID_UNKNOWN)
940 return -1;
941
942 alps_get_finger_coordinate_v7(f->mt, p, pkt_id);
943
944 if (pkt_id == V7_PACKET_ID_TWO || pkt_id == V7_PACKET_ID_MULTI) {
945 f->left = (p[0] & 0x80) >> 7;
946 f->right = (p[0] & 0x20) >> 5;
947 f->middle = (p[0] & 0x10) >> 4;
948 }
949
950 if (pkt_id == V7_PACKET_ID_TWO)
951 f->fingers = alps_get_mt_count(f->mt);
952 else if (pkt_id == V7_PACKET_ID_MULTI)
953 f->fingers = 3 + (p[5] & 0x03);
954
955 return 0;
956}
957
958static void alps_process_trackstick_packet_v7(struct psmouse *psmouse)
959{
960 struct alps_data *priv = psmouse->private;
961 unsigned char *packet = psmouse->packet;
962 struct input_dev *dev2 = priv->dev2;
963 int x, y, z, left, right, middle;
964
965 /*
966 * b7 b6 b5 b4 b3 b2 b1 b0
967 * Byte0 0 1 0 0 1 0 0 0
968 * Byte1 1 1 * * 1 M R L
969 * Byte2 X7 1 X5 X4 X3 X2 X1 X0
970 * Byte3 Z6 1 Y6 X6 1 Y2 Y1 Y0
971 * Byte4 Y7 0 Y5 Y4 Y3 1 1 0
972 * Byte5 T&P 0 Z5 Z4 Z3 Z2 Z1 Z0
973 * M / R / L: Middle / Right / Left button
974 */
975
976 x = ((packet[2] & 0xbf)) | ((packet[3] & 0x10) << 2);
977 y = (packet[3] & 0x07) | (packet[4] & 0xb8) |
978 ((packet[3] & 0x20) << 1);
979 z = (packet[5] & 0x3f) | ((packet[3] & 0x80) >> 1);
980
981 left = (packet[1] & 0x01);
982 right = (packet[1] & 0x02) >> 1;
983 middle = (packet[1] & 0x04) >> 2;
984
985 /* Divide 2 since trackpoint's speed is too fast */
986 input_report_rel(dev2, REL_X, (char)x / 2);
987 input_report_rel(dev2, REL_Y, -((char)y / 2));
988
989 input_report_key(dev2, BTN_LEFT, left);
990 input_report_key(dev2, BTN_RIGHT, right);
991 input_report_key(dev2, BTN_MIDDLE, middle);
992
993 input_sync(dev2);
994}
995
996static void alps_process_touchpad_packet_v7(struct psmouse *psmouse)
997{
998 struct alps_data *priv = psmouse->private;
999 struct input_dev *dev = psmouse->dev;
1000 struct alps_fields *f = &priv->f;
1001
1002 memset(f, 0, sizeof(*f));
1003
1004 if (priv->decode_fields(f, psmouse->packet, psmouse))
1005 return;
1006
1007 alps_report_mt_data(psmouse, alps_get_mt_count(f->mt));
1008
1009 input_mt_report_finger_count(dev, f->fingers);
1010
1011 input_report_key(dev, BTN_LEFT, f->left);
1012 input_report_key(dev, BTN_RIGHT, f->right);
1013 input_report_key(dev, BTN_MIDDLE, f->middle);
1014
1015 input_sync(dev);
1016}
1017
1018static void alps_process_packet_v7(struct psmouse *psmouse)
1019{
1020 unsigned char *packet = psmouse->packet;
1021
1022 if (packet[0] == 0x48 && (packet[4] & 0x47) == 0x06)
1023 alps_process_trackstick_packet_v7(psmouse);
1024 else
1025 alps_process_touchpad_packet_v7(psmouse);
1026}
1027
1d9f2626
SK
1028static void alps_report_bare_ps2_packet(struct psmouse *psmouse,
1029 unsigned char packet[],
1030 bool report_buttons)
1031{
1032 struct alps_data *priv = psmouse->private;
1033 struct input_dev *dev2 = priv->dev2;
1034
1035 if (report_buttons)
1036 alps_report_buttons(psmouse, dev2, psmouse->dev,
1037 packet[0] & 1, packet[0] & 2, packet[0] & 4);
1038
1039 input_report_rel(dev2, REL_X,
1040 packet[1] ? packet[1] - ((packet[0] << 4) & 0x100) : 0);
1041 input_report_rel(dev2, REL_Y,
1042 packet[2] ? ((packet[0] << 3) & 0x100) - packet[2] : 0);
1043
1044 input_sync(dev2);
1045}
1046
1047static psmouse_ret_t alps_handle_interleaved_ps2(struct psmouse *psmouse)
1048{
1049 struct alps_data *priv = psmouse->private;
1050
1051 if (psmouse->pktcnt < 6)
1052 return PSMOUSE_GOOD_DATA;
1053
1054 if (psmouse->pktcnt == 6) {
1055 /*
1056 * Start a timer to flush the packet if it ends up last
1057 * 6-byte packet in the stream. Timer needs to fire
1058 * psmouse core times out itself. 20 ms should be enough
1059 * to decide if we are getting more data or not.
1060 */
1061 mod_timer(&priv->timer, jiffies + msecs_to_jiffies(20));
1062 return PSMOUSE_GOOD_DATA;
1063 }
1064
1065 del_timer(&priv->timer);
1066
1067 if (psmouse->packet[6] & 0x80) {
1068
1069 /*
1070 * Highest bit is set - that means we either had
1071 * complete ALPS packet and this is start of the
1072 * next packet or we got garbage.
1073 */
1074
1075 if (((psmouse->packet[3] |
1076 psmouse->packet[4] |
1077 psmouse->packet[5]) & 0x80) ||
99df65e7 1078 (!alps_is_valid_first_byte(priv, psmouse->packet[6]))) {
b5d21704 1079 psmouse_dbg(psmouse,
3b112923
AS
1080 "refusing packet %4ph (suspected interleaved ps/2)\n",
1081 psmouse->packet + 3);
1d9f2626
SK
1082 return PSMOUSE_BAD_DATA;
1083 }
1084
24af5cb9 1085 priv->process_packet(psmouse);
1d9f2626
SK
1086
1087 /* Continue with the next packet */
1088 psmouse->packet[0] = psmouse->packet[6];
1089 psmouse->pktcnt = 1;
1090
1091 } else {
1092
1093 /*
1094 * High bit is 0 - that means that we indeed got a PS/2
1095 * packet in the middle of ALPS packet.
1096 *
1097 * There is also possibility that we got 6-byte ALPS
1098 * packet followed by 3-byte packet from trackpoint. We
1099 * can not distinguish between these 2 scenarios but
b5d21704 1100 * because the latter is unlikely to happen in course of
1d9f2626
SK
1101 * normal operation (user would need to press all
1102 * buttons on the pad and start moving trackpoint
1103 * without touching the pad surface) we assume former.
1104 * Even if we are wrong the wost thing that would happen
1105 * the cursor would jump but we should not get protocol
b5d21704 1106 * de-synchronization.
1d9f2626
SK
1107 */
1108
1109 alps_report_bare_ps2_packet(psmouse, &psmouse->packet[3],
1110 false);
1111
1112 /*
1113 * Continue with the standard ALPS protocol handling,
1114 * but make sure we won't process it as an interleaved
1115 * packet again, which may happen if all buttons are
1116 * pressed. To avoid this let's reset the 4th bit which
1117 * is normally 1.
1118 */
1119 psmouse->packet[3] = psmouse->packet[6] & 0xf7;
1120 psmouse->pktcnt = 4;
1121 }
1122
1123 return PSMOUSE_GOOD_DATA;
1124}
1125
1126static void alps_flush_packet(unsigned long data)
1127{
1128 struct psmouse *psmouse = (struct psmouse *)data;
24af5cb9 1129 struct alps_data *priv = psmouse->private;
1d9f2626
SK
1130
1131 serio_pause_rx(psmouse->ps2dev.serio);
1132
b46615fe 1133 if (psmouse->pktcnt == psmouse->pktsize) {
1d9f2626
SK
1134
1135 /*
1136 * We did not any more data in reasonable amount of time.
1137 * Validate the last 3 bytes and process as a standard
1138 * ALPS packet.
1139 */
1140 if ((psmouse->packet[3] |
1141 psmouse->packet[4] |
1142 psmouse->packet[5]) & 0x80) {
b5d21704 1143 psmouse_dbg(psmouse,
3b112923
AS
1144 "refusing packet %3ph (suspected interleaved ps/2)\n",
1145 psmouse->packet + 3);
1d9f2626 1146 } else {
24af5cb9 1147 priv->process_packet(psmouse);
1d9f2626
SK
1148 }
1149 psmouse->pktcnt = 0;
1150 }
1151
1152 serio_continue_rx(psmouse->ps2dev.serio);
1153}
1154
7d12e780 1155static psmouse_ret_t alps_process_byte(struct psmouse *psmouse)
1da177e4
LT
1156{
1157 struct alps_data *priv = psmouse->private;
1158
4ab8f7f3
PR
1159 /*
1160 * Check if we are dealing with a bare PS/2 packet, presumably from
1161 * a device connected to the external PS/2 port. Because bare PS/2
1162 * protocol does not have enough constant bits to self-synchronize
1163 * properly we only do this if the device is fully synchronized.
1164 */
1165 if (!psmouse->out_of_sync_cnt && (psmouse->packet[0] & 0xc8) == 0x08) {
1da177e4 1166 if (psmouse->pktcnt == 3) {
1d9f2626
SK
1167 alps_report_bare_ps2_packet(psmouse, psmouse->packet,
1168 true);
1da177e4
LT
1169 return PSMOUSE_FULL_PACKET;
1170 }
1171 return PSMOUSE_GOOD_DATA;
1172 }
1173
1d9f2626
SK
1174 /* Check for PS/2 packet stuffed in the middle of ALPS packet. */
1175
99df65e7 1176 if ((priv->flags & ALPS_PS2_INTERLEAVED) &&
1d9f2626
SK
1177 psmouse->pktcnt >= 4 && (psmouse->packet[3] & 0x0f) == 0x0f) {
1178 return alps_handle_interleaved_ps2(psmouse);
1179 }
1180
99df65e7 1181 if (!alps_is_valid_first_byte(priv, psmouse->packet[0])) {
b5d21704
DT
1182 psmouse_dbg(psmouse,
1183 "refusing packet[0] = %x (mask0 = %x, byte0 = %x)\n",
99df65e7 1184 psmouse->packet[0], priv->mask0, priv->byte0);
1da177e4 1185 return PSMOUSE_BAD_DATA;
1d9f2626 1186 }
1da177e4 1187
b46615fe 1188 /* Bytes 2 - pktsize should have 0 in the highest bit */
95f75e91 1189 if ((priv->proto_version < ALPS_PROTO_V5) &&
75af9e56 1190 psmouse->pktcnt >= 2 && psmouse->pktcnt <= psmouse->pktsize &&
1d9f2626 1191 (psmouse->packet[psmouse->pktcnt - 1] & 0x80)) {
b5d21704
DT
1192 psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
1193 psmouse->pktcnt - 1,
1194 psmouse->packet[psmouse->pktcnt - 1]);
1da177e4 1195 return PSMOUSE_BAD_DATA;
1d9f2626 1196 }
1da177e4 1197
3808843c
YT
1198 if (priv->proto_version == ALPS_PROTO_V7 &&
1199 !alps_is_valid_package_v7(psmouse)) {
1200 psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
1201 psmouse->pktcnt - 1,
1202 psmouse->packet[psmouse->pktcnt - 1]);
1203 return PSMOUSE_BAD_DATA;
1204 }
1205
b46615fe 1206 if (psmouse->pktcnt == psmouse->pktsize) {
24af5cb9 1207 priv->process_packet(psmouse);
1da177e4
LT
1208 return PSMOUSE_FULL_PACKET;
1209 }
1210
1211 return PSMOUSE_GOOD_DATA;
1212}
1213
25bded7c
SF
1214static int alps_command_mode_send_nibble(struct psmouse *psmouse, int nibble)
1215{
1216 struct ps2dev *ps2dev = &psmouse->ps2dev;
1217 struct alps_data *priv = psmouse->private;
1218 int command;
1219 unsigned char *param;
1220 unsigned char dummy[4];
1221
1222 BUG_ON(nibble > 0xf);
1223
1224 command = priv->nibble_commands[nibble].command;
1225 param = (command & 0x0f00) ?
1226 dummy : (unsigned char *)&priv->nibble_commands[nibble].data;
1227
1228 if (ps2_command(ps2dev, param, command))
1229 return -1;
1230
1231 return 0;
1232}
1233
1234static int alps_command_mode_set_addr(struct psmouse *psmouse, int addr)
1235{
1236 struct ps2dev *ps2dev = &psmouse->ps2dev;
1237 struct alps_data *priv = psmouse->private;
1238 int i, nibble;
1239
1240 if (ps2_command(ps2dev, NULL, priv->addr_command))
1241 return -1;
1242
1243 for (i = 12; i >= 0; i -= 4) {
1244 nibble = (addr >> i) & 0xf;
1245 if (alps_command_mode_send_nibble(psmouse, nibble))
1246 return -1;
1247 }
1248
1249 return 0;
1250}
1251
1252static int __alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
1253{
1254 struct ps2dev *ps2dev = &psmouse->ps2dev;
1255 unsigned char param[4];
1256
1257 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1258 return -1;
1259
1260 /*
1261 * The address being read is returned in the first two bytes
1262 * of the result. Check that this address matches the expected
1263 * address.
1264 */
1265 if (addr != ((param[0] << 8) | param[1]))
1266 return -1;
1267
1268 return param[2];
1269}
1270
1271static int alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
1272{
1273 if (alps_command_mode_set_addr(psmouse, addr))
1274 return -1;
1275 return __alps_command_mode_read_reg(psmouse, addr);
1276}
1277
1278static int __alps_command_mode_write_reg(struct psmouse *psmouse, u8 value)
1279{
1280 if (alps_command_mode_send_nibble(psmouse, (value >> 4) & 0xf))
1281 return -1;
1282 if (alps_command_mode_send_nibble(psmouse, value & 0xf))
1283 return -1;
1284 return 0;
1285}
1286
1287static int alps_command_mode_write_reg(struct psmouse *psmouse, int addr,
1288 u8 value)
1289{
1290 if (alps_command_mode_set_addr(psmouse, addr))
1291 return -1;
1292 return __alps_command_mode_write_reg(psmouse, value);
1293}
1294
24ba9707
KC
1295static int alps_rpt_cmd(struct psmouse *psmouse, int init_command,
1296 int repeated_command, unsigned char *param)
1297{
1298 struct ps2dev *ps2dev = &psmouse->ps2dev;
1299
1300 param[0] = 0;
1301 if (init_command && ps2_command(ps2dev, param, init_command))
1302 return -EIO;
1303
1304 if (ps2_command(ps2dev, NULL, repeated_command) ||
1305 ps2_command(ps2dev, NULL, repeated_command) ||
1306 ps2_command(ps2dev, NULL, repeated_command))
1307 return -EIO;
1308
1309 param[0] = param[1] = param[2] = 0xff;
1310 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1311 return -EIO;
1312
39fbe585
DT
1313 psmouse_dbg(psmouse, "%2.2X report: %3ph\n",
1314 repeated_command, param);
24ba9707
KC
1315 return 0;
1316}
1317
3808843c
YT
1318static bool alps_check_valid_firmware_id(unsigned char id[])
1319{
1320 if (id[0] == 0x73)
1321 return true;
1322
1323 if (id[0] == 0x88 &&
1324 (id[1] == 0x07 ||
1325 id[1] == 0x08 ||
1326 (id[1] & 0xf0) == 0xb0 ||
1327 (id[1] & 0xf0) == 0xc0)) {
1328 return true;
1329 }
1330
1331 return false;
1332}
1333
d18e53fc 1334static int alps_enter_command_mode(struct psmouse *psmouse)
25bded7c
SF
1335{
1336 unsigned char param[4];
25bded7c 1337
24ba9707 1338 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_RESET_WRAP, param)) {
25bded7c
SF
1339 psmouse_err(psmouse, "failed to enter command mode\n");
1340 return -1;
1341 }
1342
3808843c 1343 if (!alps_check_valid_firmware_id(param)) {
25bded7c 1344 psmouse_dbg(psmouse,
24ba9707 1345 "unknown response while entering command mode\n");
25bded7c
SF
1346 return -1;
1347 }
25bded7c
SF
1348 return 0;
1349}
1350
1351static inline int alps_exit_command_mode(struct psmouse *psmouse)
1352{
1353 struct ps2dev *ps2dev = &psmouse->ps2dev;
1354 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM))
1355 return -1;
1356 return 0;
1357}
1358
1da177e4
LT
1359/*
1360 * For DualPoint devices select the device that should respond to
1361 * subsequent commands. It looks like glidepad is behind stickpointer,
1362 * I'd thought it would be other way around...
1363 */
25bded7c 1364static int alps_passthrough_mode_v2(struct psmouse *psmouse, bool enable)
1da177e4
LT
1365{
1366 struct ps2dev *ps2dev = &psmouse->ps2dev;
1da177e4
LT
1367 int cmd = enable ? PSMOUSE_CMD_SETSCALE21 : PSMOUSE_CMD_SETSCALE11;
1368
1369 if (ps2_command(ps2dev, NULL, cmd) ||
1370 ps2_command(ps2dev, NULL, cmd) ||
1371 ps2_command(ps2dev, NULL, cmd) ||
1372 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1373 return -1;
1374
1375 /* we may get 3 more bytes, just ignore them */
c611763d 1376 ps2_drain(ps2dev, 3, 100);
1da177e4
LT
1377
1378 return 0;
1379}
1380
25bded7c 1381static int alps_absolute_mode_v1_v2(struct psmouse *psmouse)
1da177e4
LT
1382{
1383 struct ps2dev *ps2dev = &psmouse->ps2dev;
1384
1385 /* Try ALPS magic knock - 4 disable before enable */
1386 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1387 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1388 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1389 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1390 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
1391 return -1;
1392
1393 /*
1394 * Switch mouse to poll (remote) mode so motion data will not
1395 * get in our way
1396 */
1397 return ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETPOLL);
1398}
1399
95f75e91
YT
1400static int alps_monitor_mode_send_word(struct psmouse *psmouse, u16 word)
1401{
1402 int i, nibble;
1403
1404 /*
1405 * b0-b11 are valid bits, send sequence is inverse.
1406 * e.g. when word = 0x0123, nibble send sequence is 3, 2, 1
1407 */
1408 for (i = 0; i <= 8; i += 4) {
1409 nibble = (word >> i) & 0xf;
1410 if (alps_command_mode_send_nibble(psmouse, nibble))
1411 return -1;
1412 }
1413
1414 return 0;
1415}
1416
1417static int alps_monitor_mode_write_reg(struct psmouse *psmouse,
1418 u16 addr, u16 value)
1419{
1420 struct ps2dev *ps2dev = &psmouse->ps2dev;
1421
1422 /* 0x0A0 is the command to write the word */
1423 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE) ||
1424 alps_monitor_mode_send_word(psmouse, 0x0A0) ||
1425 alps_monitor_mode_send_word(psmouse, addr) ||
1426 alps_monitor_mode_send_word(psmouse, value) ||
1427 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1428 return -1;
1429
1430 return 0;
1431}
1432
1433static int alps_monitor_mode(struct psmouse *psmouse, bool enable)
1434{
1435 struct ps2dev *ps2dev = &psmouse->ps2dev;
1436
1437 if (enable) {
1438 /* EC E9 F5 F5 E7 E6 E7 E9 to enter monitor mode */
1439 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
1440 ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO) ||
1441 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1442 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1443 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1444 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1445 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1446 ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO))
1447 return -1;
1448 } else {
1449 /* EC to exit monitor mode */
1450 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP))
1451 return -1;
1452 }
1453
1454 return 0;
1455}
1456
1457static int alps_absolute_mode_v6(struct psmouse *psmouse)
1458{
1459 u16 reg_val = 0x181;
1460 int ret = -1;
1461
1462 /* enter monitor mode, to write the register */
1463 if (alps_monitor_mode(psmouse, true))
1464 return -1;
1465
1466 ret = alps_monitor_mode_write_reg(psmouse, 0x000, reg_val);
1467
1468 if (alps_monitor_mode(psmouse, false))
1469 ret = -1;
1470
1471 return ret;
1472}
1473
1da177e4
LT
1474static int alps_get_status(struct psmouse *psmouse, char *param)
1475{
1da177e4 1476 /* Get status: 0xF5 0xF5 0xF5 0xE9 */
24ba9707 1477 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_DISABLE, param))
1da177e4
LT
1478 return -1;
1479
1da177e4
LT
1480 return 0;
1481}
1482
1483/*
1484 * Turn touchpad tapping on or off. The sequences are:
1485 * 0xE9 0xF5 0xF5 0xF3 0x0A to enable,
1486 * 0xE9 0xF5 0xF5 0xE8 0x00 to disable.
1487 * My guess that 0xE9 (GetInfo) is here as a sync point.
1488 * For models that also have stickpointer (DualPoints) its tapping
1489 * is controlled separately (0xE6 0xE6 0xE6 0xF3 0x14|0x0A) but
1490 * we don't fiddle with it.
1491 */
1492static int alps_tap_mode(struct psmouse *psmouse, int enable)
1493{
1494 struct ps2dev *ps2dev = &psmouse->ps2dev;
1495 int cmd = enable ? PSMOUSE_CMD_SETRATE : PSMOUSE_CMD_SETRES;
1496 unsigned char tap_arg = enable ? 0x0A : 0x00;
1497 unsigned char param[4];
1498
1499 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO) ||
1500 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1501 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1502 ps2_command(ps2dev, &tap_arg, cmd))
1503 return -1;
1504
1505 if (alps_get_status(psmouse, param))
1506 return -1;
1507
1508 return 0;
1509}
1510
f0d5c6f4
DT
1511/*
1512 * alps_poll() - poll the touchpad for current motion packet.
1513 * Used in resync.
1514 */
1515static int alps_poll(struct psmouse *psmouse)
1516{
1517 struct alps_data *priv = psmouse->private;
b46615fe 1518 unsigned char buf[sizeof(psmouse->packet)];
b7802c5c 1519 bool poll_failed;
f0d5c6f4 1520
99df65e7 1521 if (priv->flags & ALPS_PASS)
25bded7c 1522 alps_passthrough_mode_v2(psmouse, true);
f0d5c6f4
DT
1523
1524 poll_failed = ps2_command(&psmouse->ps2dev, buf,
1525 PSMOUSE_CMD_POLL | (psmouse->pktsize << 8)) < 0;
1526
99df65e7 1527 if (priv->flags & ALPS_PASS)
25bded7c 1528 alps_passthrough_mode_v2(psmouse, false);
f0d5c6f4 1529
99df65e7 1530 if (poll_failed || (buf[0] & priv->mask0) != priv->byte0)
f0d5c6f4
DT
1531 return -1;
1532
1533 if ((psmouse->badbyte & 0xc8) == 0x08) {
1534/*
1535 * Poll the track stick ...
1536 */
1537 if (ps2_command(&psmouse->ps2dev, buf, PSMOUSE_CMD_POLL | (3 << 8)))
1538 return -1;
1539 }
1540
1541 memcpy(psmouse->packet, buf, sizeof(buf));
1542 return 0;
1543}
1544
25bded7c 1545static int alps_hw_init_v1_v2(struct psmouse *psmouse)
1da177e4
LT
1546{
1547 struct alps_data *priv = psmouse->private;
f3a5c73d 1548
99df65e7 1549 if ((priv->flags & ALPS_PASS) &&
25bded7c 1550 alps_passthrough_mode_v2(psmouse, true)) {
1da177e4 1551 return -1;
b7802c5c 1552 }
1da177e4 1553
b7802c5c 1554 if (alps_tap_mode(psmouse, true)) {
b5d21704 1555 psmouse_warn(psmouse, "Failed to enable hardware tapping\n");
1da177e4 1556 return -1;
963f626d 1557 }
1da177e4 1558
25bded7c 1559 if (alps_absolute_mode_v1_v2(psmouse)) {
b5d21704 1560 psmouse_err(psmouse, "Failed to enable absolute mode\n");
1da177e4
LT
1561 return -1;
1562 }
1563
99df65e7 1564 if ((priv->flags & ALPS_PASS) &&
25bded7c 1565 alps_passthrough_mode_v2(psmouse, false)) {
1da177e4 1566 return -1;
b7802c5c 1567 }
1da177e4 1568
1e0c5b12
DT
1569 /* ALPS needs stream mode, otherwise it won't report any data */
1570 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)) {
b5d21704 1571 psmouse_err(psmouse, "Failed to enable stream mode\n");
1e0c5b12
DT
1572 return -1;
1573 }
1574
1575 return 0;
1576}
1577
95f75e91
YT
1578static int alps_hw_init_v6(struct psmouse *psmouse)
1579{
1580 unsigned char param[2] = {0xC8, 0x14};
1581
1582 /* Enter passthrough mode to let trackpoint enter 6byte raw mode */
1583 if (alps_passthrough_mode_v2(psmouse, true))
1584 return -1;
1585
1586 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1587 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1588 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1589 ps2_command(&psmouse->ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
1590 ps2_command(&psmouse->ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
1591 return -1;
1592
1593 if (alps_passthrough_mode_v2(psmouse, false))
1594 return -1;
1595
1596 if (alps_absolute_mode_v6(psmouse)) {
1597 psmouse_err(psmouse, "Failed to enable absolute mode\n");
1598 return -1;
1599 }
1600
1601 return 0;
1602}
1603
25bded7c 1604/*
cd401204 1605 * Enable or disable passthrough mode to the trackstick.
25bded7c 1606 */
cd401204
KC
1607static int alps_passthrough_mode_v3(struct psmouse *psmouse,
1608 int reg_base, bool enable)
25bded7c 1609{
cd401204 1610 int reg_val, ret = -1;
25bded7c 1611
d18e53fc 1612 if (alps_enter_command_mode(psmouse))
25bded7c
SF
1613 return -1;
1614
cd401204
KC
1615 reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x0008);
1616 if (reg_val == -1)
1617 goto error;
1618
25bded7c
SF
1619 if (enable)
1620 reg_val |= 0x01;
1621 else
1622 reg_val &= ~0x01;
1623
cd401204 1624 ret = __alps_command_mode_write_reg(psmouse, reg_val);
25bded7c 1625
cd401204
KC
1626error:
1627 if (alps_exit_command_mode(psmouse))
1628 ret = -1;
1629 return ret;
25bded7c
SF
1630}
1631
1632/* Must be in command mode when calling this function */
1633static int alps_absolute_mode_v3(struct psmouse *psmouse)
1634{
1635 int reg_val;
1636
1637 reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
1638 if (reg_val == -1)
1639 return -1;
1640
1641 reg_val |= 0x06;
1642 if (__alps_command_mode_write_reg(psmouse, reg_val))
1643 return -1;
1644
1645 return 0;
1646}
1647
cd401204 1648static int alps_probe_trackstick_v3(struct psmouse *psmouse, int reg_base)
25bded7c 1649{
cd401204 1650 int ret = -EIO, reg_val;
25bded7c 1651
d18e53fc 1652 if (alps_enter_command_mode(psmouse))
25bded7c
SF
1653 goto error;
1654
cd401204 1655 reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x08);
25bded7c
SF
1656 if (reg_val == -1)
1657 goto error;
cd401204
KC
1658
1659 /* bit 7: trackstick is present */
1660 ret = reg_val & 0x80 ? 0 : -ENODEV;
1661
1662error:
1663 alps_exit_command_mode(psmouse);
1664 return ret;
1665}
1666
1667static int alps_setup_trackstick_v3(struct psmouse *psmouse, int reg_base)
1668{
1669 struct ps2dev *ps2dev = &psmouse->ps2dev;
1670 int ret = 0;
1671 unsigned char param[4];
1672
1673 if (alps_passthrough_mode_v3(psmouse, reg_base, true))
1674 return -EIO;
1675
1676 /*
1677 * E7 report for the trackstick
1678 *
1679 * There have been reports of failures to seem to trace back
1680 * to the above trackstick check failing. When these occur
1681 * this E7 report fails, so when that happens we continue
1682 * with the assumption that there isn't a trackstick after
1683 * all.
1684 */
1685 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_SETSCALE21, param)) {
1686 psmouse_warn(psmouse, "trackstick E7 report failed\n");
1687 ret = -ENODEV;
1688 } else {
39fbe585 1689 psmouse_dbg(psmouse, "trackstick E7 report: %3ph\n", param);
25bded7c
SF
1690
1691 /*
cd401204
KC
1692 * Not sure what this does, but it is absolutely
1693 * essential. Without it, the touchpad does not
1694 * work at all and the trackstick just emits normal
1695 * PS/2 packets.
25bded7c 1696 */
cd401204
KC
1697 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1698 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1699 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1700 alps_command_mode_send_nibble(psmouse, 0x9) ||
1701 alps_command_mode_send_nibble(psmouse, 0x4)) {
1702 psmouse_err(psmouse,
1703 "Error sending magic E6 sequence\n");
1704 ret = -EIO;
1705 goto error;
25bded7c
SF
1706 }
1707
cd401204
KC
1708 /*
1709 * This ensures the trackstick packets are in the format
1710 * supported by this driver. If bit 1 isn't set the packet
1711 * format is different.
1712 */
d18e53fc 1713 if (alps_enter_command_mode(psmouse) ||
cd401204
KC
1714 alps_command_mode_write_reg(psmouse,
1715 reg_base + 0x08, 0x82) ||
1716 alps_exit_command_mode(psmouse))
1717 ret = -EIO;
25bded7c
SF
1718 }
1719
cd401204
KC
1720error:
1721 if (alps_passthrough_mode_v3(psmouse, reg_base, false))
1722 ret = -EIO;
1723
1724 return ret;
1725}
1726
1727static int alps_hw_init_v3(struct psmouse *psmouse)
1728{
1729 struct ps2dev *ps2dev = &psmouse->ps2dev;
1730 int reg_val;
1731 unsigned char param[4];
1732
1733 reg_val = alps_probe_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE);
1734 if (reg_val == -EIO)
1735 goto error;
39fbe585 1736
cd401204
KC
1737 if (reg_val == 0 &&
1738 alps_setup_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE) == -EIO)
1739 goto error;
1740
d18e53fc 1741 if (alps_enter_command_mode(psmouse) ||
cd401204 1742 alps_absolute_mode_v3(psmouse)) {
25bded7c
SF
1743 psmouse_err(psmouse, "Failed to enter absolute mode\n");
1744 goto error;
1745 }
1746
1747 reg_val = alps_command_mode_read_reg(psmouse, 0x0006);
1748 if (reg_val == -1)
1749 goto error;
1750 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
1751 goto error;
1752
1753 reg_val = alps_command_mode_read_reg(psmouse, 0x0007);
1754 if (reg_val == -1)
1755 goto error;
1756 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
1757 goto error;
1758
1759 if (alps_command_mode_read_reg(psmouse, 0x0144) == -1)
1760 goto error;
1761 if (__alps_command_mode_write_reg(psmouse, 0x04))
1762 goto error;
1763
1764 if (alps_command_mode_read_reg(psmouse, 0x0159) == -1)
1765 goto error;
1766 if (__alps_command_mode_write_reg(psmouse, 0x03))
1767 goto error;
1768
1769 if (alps_command_mode_read_reg(psmouse, 0x0163) == -1)
1770 goto error;
1771 if (alps_command_mode_write_reg(psmouse, 0x0163, 0x03))
1772 goto error;
1773
1774 if (alps_command_mode_read_reg(psmouse, 0x0162) == -1)
1775 goto error;
1776 if (alps_command_mode_write_reg(psmouse, 0x0162, 0x04))
1777 goto error;
1778
25bded7c
SF
1779 alps_exit_command_mode(psmouse);
1780
1781 /* Set rate and enable data reporting */
1782 param[0] = 0x64;
1783 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
1784 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
1785 psmouse_err(psmouse, "Failed to enable data reporting\n");
1786 return -1;
1787 }
1788
1789 return 0;
1790
25bded7c
SF
1791error:
1792 /*
1793 * Leaving the touchpad in command mode will essentially render
1794 * it unusable until the machine reboots, so exit it here just
1795 * to be safe
1796 */
1797 alps_exit_command_mode(psmouse);
1798 return -1;
1799}
1800
f3f33c67
HG
1801static int alps_get_v3_v7_resolution(struct psmouse *psmouse, int reg_pitch)
1802{
1803 int reg, x_pitch, y_pitch, x_electrode, y_electrode, x_phys, y_phys;
1804 struct alps_data *priv = psmouse->private;
1805
1806 reg = alps_command_mode_read_reg(psmouse, reg_pitch);
1807 if (reg < 0)
1808 return reg;
1809
1810 x_pitch = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
1811 x_pitch = 50 + 2 * x_pitch; /* In 0.1 mm units */
1812
1813 y_pitch = (char)reg >> 4; /* sign extend upper 4 bits */
1814 y_pitch = 36 + 2 * y_pitch; /* In 0.1 mm units */
1815
1816 reg = alps_command_mode_read_reg(psmouse, reg_pitch + 1);
1817 if (reg < 0)
1818 return reg;
1819
1820 x_electrode = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
1821 x_electrode = 17 + x_electrode;
1822
1823 y_electrode = (char)reg >> 4; /* sign extend upper 4 bits */
1824 y_electrode = 13 + y_electrode;
1825
1826 x_phys = x_pitch * (x_electrode - 1); /* In 0.1 mm units */
1827 y_phys = y_pitch * (y_electrode - 1); /* In 0.1 mm units */
1828
1829 priv->x_res = priv->x_max * 10 / x_phys; /* units / mm */
1830 priv->y_res = priv->y_max * 10 / y_phys; /* units / mm */
1831
1832 psmouse_dbg(psmouse,
1833 "pitch %dx%d num-electrodes %dx%d physical size %dx%d mm res %dx%d\n",
1834 x_pitch, y_pitch, x_electrode, y_electrode,
1835 x_phys / 10, y_phys / 10, priv->x_res, priv->y_res);
1836
1837 return 0;
1838}
1839
1302bac3
KC
1840static int alps_hw_init_rushmore_v3(struct psmouse *psmouse)
1841{
cd401204 1842 struct alps_data *priv = psmouse->private;
1302bac3
KC
1843 struct ps2dev *ps2dev = &psmouse->ps2dev;
1844 int reg_val, ret = -1;
1845
cd401204
KC
1846 if (priv->flags & ALPS_DUALPOINT) {
1847 reg_val = alps_setup_trackstick_v3(psmouse,
1848 ALPS_REG_BASE_RUSHMORE);
1849 if (reg_val == -EIO)
1850 goto error;
1851 if (reg_val == -ENODEV)
1852 priv->flags &= ~ALPS_DUALPOINT;
1853 }
1854
d18e53fc 1855 if (alps_enter_command_mode(psmouse) ||
1302bac3
KC
1856 alps_command_mode_read_reg(psmouse, 0xc2d9) == -1 ||
1857 alps_command_mode_write_reg(psmouse, 0xc2cb, 0x00))
1858 goto error;
1859
f3f33c67
HG
1860 if (alps_get_v3_v7_resolution(psmouse, 0xc2da))
1861 goto error;
1862
1302bac3
KC
1863 reg_val = alps_command_mode_read_reg(psmouse, 0xc2c6);
1864 if (reg_val == -1)
1865 goto error;
1866 if (__alps_command_mode_write_reg(psmouse, reg_val & 0xfd))
1867 goto error;
1868
1869 if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
1870 goto error;
1871
1872 /* enter absolute mode */
1873 reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
1874 if (reg_val == -1)
1875 goto error;
1876 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
1877 goto error;
1878
1879 alps_exit_command_mode(psmouse);
1880 return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
1881
1882error:
1883 alps_exit_command_mode(psmouse);
1884 return ret;
1885}
1886
25bded7c
SF
1887/* Must be in command mode when calling this function */
1888static int alps_absolute_mode_v4(struct psmouse *psmouse)
1889{
1890 int reg_val;
1891
1892 reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
1893 if (reg_val == -1)
1894 return -1;
1895
1896 reg_val |= 0x02;
1897 if (__alps_command_mode_write_reg(psmouse, reg_val))
1898 return -1;
1899
1900 return 0;
1901}
1902
1903static int alps_hw_init_v4(struct psmouse *psmouse)
1904{
25bded7c
SF
1905 struct ps2dev *ps2dev = &psmouse->ps2dev;
1906 unsigned char param[4];
1907
d18e53fc 1908 if (alps_enter_command_mode(psmouse))
25bded7c
SF
1909 goto error;
1910
1911 if (alps_absolute_mode_v4(psmouse)) {
1912 psmouse_err(psmouse, "Failed to enter absolute mode\n");
1913 goto error;
1914 }
1915
1916 if (alps_command_mode_write_reg(psmouse, 0x0007, 0x8c))
1917 goto error;
1918
1919 if (alps_command_mode_write_reg(psmouse, 0x0149, 0x03))
1920 goto error;
1921
1922 if (alps_command_mode_write_reg(psmouse, 0x0160, 0x03))
1923 goto error;
1924
1925 if (alps_command_mode_write_reg(psmouse, 0x017f, 0x15))
1926 goto error;
1927
1928 if (alps_command_mode_write_reg(psmouse, 0x0151, 0x01))
1929 goto error;
1930
1931 if (alps_command_mode_write_reg(psmouse, 0x0168, 0x03))
1932 goto error;
1933
1934 if (alps_command_mode_write_reg(psmouse, 0x014a, 0x03))
1935 goto error;
1936
1937 if (alps_command_mode_write_reg(psmouse, 0x0161, 0x03))
1938 goto error;
1939
1940 alps_exit_command_mode(psmouse);
1941
1942 /*
1943 * This sequence changes the output from a 9-byte to an
1944 * 8-byte format. All the same data seems to be present,
1945 * just in a more compact format.
1946 */
1947 param[0] = 0xc8;
1948 param[1] = 0x64;
1949 param[2] = 0x50;
1950 if (ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
1951 ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE) ||
1952 ps2_command(ps2dev, &param[2], PSMOUSE_CMD_SETRATE) ||
1953 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
1954 return -1;
1955
1956 /* Set rate and enable data reporting */
1957 param[0] = 0x64;
1958 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
1959 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
1960 psmouse_err(psmouse, "Failed to enable data reporting\n");
1961 return -1;
1962 }
1963
1964 return 0;
1965
1966error:
1967 /*
1968 * Leaving the touchpad in command mode will essentially render
1969 * it unusable until the machine reboots, so exit it here just
1970 * to be safe
1971 */
1972 alps_exit_command_mode(psmouse);
1973 return -1;
1974}
1975
ee65d4b3
YT
1976static int alps_dolphin_get_device_area(struct psmouse *psmouse,
1977 struct alps_data *priv)
1978{
1979 struct ps2dev *ps2dev = &psmouse->ps2dev;
1980 unsigned char param[4] = {0};
1981 int num_x_electrode, num_y_electrode;
1982
1983 if (alps_enter_command_mode(psmouse))
1984 return -1;
1985
1986 param[0] = 0x0a;
1987 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
1988 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
1989 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
1990 ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
1991 ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE))
1992 return -1;
1993
1994 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1995 return -1;
1996
1997 /*
1998 * Dolphin's sensor line number is not fixed. It can be calculated
1999 * by adding the device's register value with DOLPHIN_PROFILE_X/YOFFSET.
2000 * Further more, we can get device's x_max and y_max by multiplying
2001 * sensor line number with DOLPHIN_COUNT_PER_ELECTRODE.
2002 *
2003 * e.g. When we get register's sensor_x = 11 & sensor_y = 8,
2004 * real sensor line number X = 11 + 8 = 19, and
2005 * real sensor line number Y = 8 + 1 = 9.
2006 * So, x_max = (19 - 1) * 64 = 1152, and
2007 * y_max = (9 - 1) * 64 = 512.
2008 */
2009 num_x_electrode = DOLPHIN_PROFILE_XOFFSET + (param[2] & 0x0F);
2010 num_y_electrode = DOLPHIN_PROFILE_YOFFSET + ((param[2] >> 4) & 0x0F);
2011 priv->x_bits = num_x_electrode;
2012 priv->y_bits = num_y_electrode;
2013 priv->x_max = (num_x_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
2014 priv->y_max = (num_y_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
2015
2016 if (alps_exit_command_mode(psmouse))
2017 return -1;
2018
2019 return 0;
2020}
2021
75af9e56
DT
2022static int alps_hw_init_dolphin_v1(struct psmouse *psmouse)
2023{
2024 struct ps2dev *ps2dev = &psmouse->ps2dev;
2025 unsigned char param[2];
2026
2027 /* This is dolphin "v1" as empirically defined by florin9doi */
2028 param[0] = 0x64;
2029 param[1] = 0x28;
2030
2031 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2032 ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2033 ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
2034 return -1;
2035
2036 return 0;
2037}
2038
3808843c
YT
2039static int alps_hw_init_v7(struct psmouse *psmouse)
2040{
2041 struct ps2dev *ps2dev = &psmouse->ps2dev;
2042 int reg_val, ret = -1;
2043
2044 if (alps_enter_command_mode(psmouse) ||
2045 alps_command_mode_read_reg(psmouse, 0xc2d9) == -1)
2046 goto error;
2047
f3f33c67
HG
2048 if (alps_get_v3_v7_resolution(psmouse, 0xc397))
2049 goto error;
2050
3808843c
YT
2051 if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
2052 goto error;
2053
2054 reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
2055 if (reg_val == -1)
2056 goto error;
2057 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
2058 goto error;
2059
2060 alps_exit_command_mode(psmouse);
2061 return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
2062
2063error:
2064 alps_exit_command_mode(psmouse);
2065 return ret;
2066}
2067
24af5cb9 2068static void alps_set_defaults(struct alps_data *priv)
25bded7c 2069{
f673ceb1
KC
2070 priv->byte0 = 0x8f;
2071 priv->mask0 = 0x8f;
2072 priv->flags = ALPS_DUALPOINT;
2073
7a9f73e7
KC
2074 priv->x_max = 2000;
2075 priv->y_max = 1400;
2076 priv->x_bits = 15;
2077 priv->y_bits = 11;
2078
99df65e7 2079 switch (priv->proto_version) {
25bded7c
SF
2080 case ALPS_PROTO_V1:
2081 case ALPS_PROTO_V2:
24af5cb9
KC
2082 priv->hw_init = alps_hw_init_v1_v2;
2083 priv->process_packet = alps_process_packet_v1_v2;
2084 priv->set_abs_params = alps_set_abs_params_st;
95f75e91
YT
2085 priv->x_max = 1023;
2086 priv->y_max = 767;
25bded7c
SF
2087 break;
2088 case ALPS_PROTO_V3:
24af5cb9
KC
2089 priv->hw_init = alps_hw_init_v3;
2090 priv->process_packet = alps_process_packet_v3;
2091 priv->set_abs_params = alps_set_abs_params_mt;
f85e5001 2092 priv->decode_fields = alps_decode_pinnacle;
50e8b216
KC
2093 priv->nibble_commands = alps_v3_nibble_commands;
2094 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
25bded7c
SF
2095 break;
2096 case ALPS_PROTO_V4:
24af5cb9
KC
2097 priv->hw_init = alps_hw_init_v4;
2098 priv->process_packet = alps_process_packet_v4;
2099 priv->set_abs_params = alps_set_abs_params_mt;
50e8b216
KC
2100 priv->nibble_commands = alps_v4_nibble_commands;
2101 priv->addr_command = PSMOUSE_CMD_DISABLE;
25bded7c 2102 break;
75af9e56
DT
2103 case ALPS_PROTO_V5:
2104 priv->hw_init = alps_hw_init_dolphin_v1;
ee65d4b3 2105 priv->process_packet = alps_process_touchpad_packet_v3_v5;
75af9e56
DT
2106 priv->decode_fields = alps_decode_dolphin;
2107 priv->set_abs_params = alps_set_abs_params_mt;
2108 priv->nibble_commands = alps_v3_nibble_commands;
2109 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2110 priv->byte0 = 0xc8;
ee65d4b3 2111 priv->mask0 = 0xd8;
75af9e56
DT
2112 priv->flags = 0;
2113 priv->x_max = 1360;
2114 priv->y_max = 660;
2115 priv->x_bits = 23;
2116 priv->y_bits = 12;
2117 break;
95f75e91
YT
2118 case ALPS_PROTO_V6:
2119 priv->hw_init = alps_hw_init_v6;
2120 priv->process_packet = alps_process_packet_v6;
2121 priv->set_abs_params = alps_set_abs_params_st;
2122 priv->nibble_commands = alps_v6_nibble_commands;
2123 priv->x_max = 2047;
2124 priv->y_max = 1535;
2125 break;
3808843c
YT
2126 case ALPS_PROTO_V7:
2127 priv->hw_init = alps_hw_init_v7;
2128 priv->process_packet = alps_process_packet_v7;
2129 priv->decode_fields = alps_decode_packet_v7;
2130 priv->set_abs_params = alps_set_abs_params_mt;
2131 priv->nibble_commands = alps_v3_nibble_commands;
2132 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2133 priv->x_max = 0xfff;
2134 priv->y_max = 0x7ff;
2135 priv->byte0 = 0x48;
2136 priv->mask0 = 0x48;
2137
2138 if (priv->fw_ver[1] != 0xba)
2139 priv->flags |= ALPS_BUTTONPAD;
2140 break;
25bded7c 2141 }
25bded7c
SF
2142}
2143
b5d6b851
KC
2144static int alps_match_table(struct psmouse *psmouse, struct alps_data *priv,
2145 unsigned char *e7, unsigned char *ec)
2e992cc0 2146{
b5d6b851 2147 const struct alps_model_info *model;
2e992cc0
KC
2148 int i;
2149
b5d6b851
KC
2150 for (i = 0; i < ARRAY_SIZE(alps_model_data); i++) {
2151 model = &alps_model_data[i];
2152
2153 if (!memcmp(e7, model->signature, sizeof(model->signature)) &&
2154 (!model->command_mode_resp ||
2155 model->command_mode_resp == ec[2])) {
2156
2157 priv->proto_version = model->proto_version;
24af5cb9
KC
2158 alps_set_defaults(priv);
2159
b5d6b851
KC
2160 priv->flags = model->flags;
2161 priv->byte0 = model->byte0;
2162 priv->mask0 = model->mask0;
2163
2164 return 0;
2165 }
2166 }
2167
2168 return -EINVAL;
2169}
2170
2171static int alps_identify(struct psmouse *psmouse, struct alps_data *priv)
2172{
2173 unsigned char e6[4], e7[4], ec[4];
2174
2e992cc0
KC
2175 /*
2176 * First try "E6 report".
2177 * ALPS should return 0,0,10 or 0,0,100 if no buttons are pressed.
2178 * The bits 0-2 of the first byte will be 1s if some buttons are
2179 * pressed.
2180 */
b5d6b851
KC
2181 if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2182 PSMOUSE_CMD_SETSCALE11, e6))
2183 return -EIO;
2e992cc0 2184
b5d6b851
KC
2185 if ((e6[0] & 0xf8) != 0 || e6[1] != 0 || (e6[2] != 10 && e6[2] != 100))
2186 return -EINVAL;
2e992cc0
KC
2187
2188 /*
b5d6b851
KC
2189 * Now get the "E7" and "EC" reports. These will uniquely identify
2190 * most ALPS touchpads.
2e992cc0 2191 */
b5d6b851
KC
2192 if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2193 PSMOUSE_CMD_SETSCALE21, e7) ||
2194 alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2195 PSMOUSE_CMD_RESET_WRAP, ec) ||
2196 alps_exit_command_mode(psmouse))
2197 return -EIO;
2e992cc0 2198
c0cd17f6
HG
2199 /* Save the Firmware version */
2200 memcpy(priv->fw_ver, ec, 3);
2201
f673ceb1 2202 if (alps_match_table(psmouse, priv, e7, ec) == 0) {
75af9e56
DT
2203 return 0;
2204 } else if (e7[0] == 0x73 && e7[1] == 0x03 && e7[2] == 0x50 &&
ee65d4b3 2205 ec[0] == 0x73 && (ec[1] == 0x01 || ec[1] == 0x02)) {
75af9e56
DT
2206 priv->proto_version = ALPS_PROTO_V5;
2207 alps_set_defaults(priv);
ee65d4b3
YT
2208 if (alps_dolphin_get_device_area(psmouse, priv))
2209 return -EIO;
2210 else
2211 return 0;
3808843c
YT
2212 } else if (ec[0] == 0x88 &&
2213 ((ec[1] & 0xf0) == 0xb0 || (ec[1] & 0xf0) == 0xc0)) {
2214 priv->proto_version = ALPS_PROTO_V7;
2215 alps_set_defaults(priv);
2216
2217 return 0;
1302bac3
KC
2218 } else if (ec[0] == 0x88 && ec[1] == 0x08) {
2219 priv->proto_version = ALPS_PROTO_V3;
2220 alps_set_defaults(priv);
2221
2222 priv->hw_init = alps_hw_init_rushmore_v3;
2223 priv->decode_fields = alps_decode_rushmore;
2224 priv->x_bits = 16;
2225 priv->y_bits = 12;
40e8f53b 2226 priv->flags |= ALPS_IS_RUSHMORE;
1302bac3 2227
cd401204
KC
2228 /* hack to make addr_command, nibble_command available */
2229 psmouse->private = priv;
2230
2231 if (alps_probe_trackstick_v3(psmouse, ALPS_REG_BASE_RUSHMORE))
2232 priv->flags &= ~ALPS_DUALPOINT;
2233
f673ceb1
KC
2234 return 0;
2235 } else if (ec[0] == 0x88 && ec[1] == 0x07 &&
2236 ec[2] >= 0x90 && ec[2] <= 0x9d) {
2237 priv->proto_version = ALPS_PROTO_V3;
2238 alps_set_defaults(priv);
2239
b5d6b851 2240 return 0;
f673ceb1 2241 }
2e992cc0 2242
a9e06219
DT
2243 psmouse_dbg(psmouse,
2244 "Likely not an ALPS touchpad: E7=%3ph, EC=%3ph\n", e7, ec);
2e992cc0 2245
b5d6b851 2246 return -EINVAL;
2e992cc0
KC
2247}
2248
1e0c5b12
DT
2249static int alps_reconnect(struct psmouse *psmouse)
2250{
b5d6b851 2251 struct alps_data *priv = psmouse->private;
71bb21b6 2252
1e0c5b12
DT
2253 psmouse_reset(psmouse);
2254
b5d6b851 2255 if (alps_identify(psmouse, priv) < 0)
1e0c5b12
DT
2256 return -1;
2257
24af5cb9 2258 return priv->hw_init(psmouse);
1da177e4
LT
2259}
2260
2261static void alps_disconnect(struct psmouse *psmouse)
2262{
2263 struct alps_data *priv = psmouse->private;
2e5b636b 2264
1da177e4 2265 psmouse_reset(psmouse);
1d9f2626 2266 del_timer_sync(&priv->timer);
2e5b636b 2267 input_unregister_device(priv->dev2);
1da177e4
LT
2268 kfree(priv);
2269}
2270
24af5cb9
KC
2271static void alps_set_abs_params_st(struct alps_data *priv,
2272 struct input_dev *dev1)
2273{
95f75e91
YT
2274 input_set_abs_params(dev1, ABS_X, 0, priv->x_max, 0, 0);
2275 input_set_abs_params(dev1, ABS_Y, 0, priv->y_max, 0, 0);
24af5cb9
KC
2276}
2277
2278static void alps_set_abs_params_mt(struct alps_data *priv,
2279 struct input_dev *dev1)
2280{
7a9f73e7
KC
2281 input_set_abs_params(dev1, ABS_MT_POSITION_X, 0, priv->x_max, 0, 0);
2282 input_set_abs_params(dev1, ABS_MT_POSITION_Y, 0, priv->y_max, 0, 0);
24af5cb9 2283
f3f33c67
HG
2284 input_abs_set_res(dev1, ABS_MT_POSITION_X, priv->x_res);
2285 input_abs_set_res(dev1, ABS_MT_POSITION_Y, priv->y_res);
2286
cdf333ef
HG
2287 input_mt_init_slots(dev1, MAX_TOUCHES, INPUT_MT_POINTER |
2288 INPUT_MT_DROP_UNUSED | INPUT_MT_TRACK | INPUT_MT_SEMI_MT);
2289
24af5cb9
KC
2290 set_bit(BTN_TOOL_TRIPLETAP, dev1->keybit);
2291 set_bit(BTN_TOOL_QUADTAP, dev1->keybit);
3808843c
YT
2292
2293 /* V7 is real multi-touch */
2294 if (priv->proto_version == ALPS_PROTO_V7)
2295 clear_bit(INPUT_PROP_SEMI_MT, dev1->propbit);
24af5cb9
KC
2296}
2297
1da177e4
LT
2298int alps_init(struct psmouse *psmouse)
2299{
2300 struct alps_data *priv;
2e5b636b 2301 struct input_dev *dev1 = psmouse->dev, *dev2;
1da177e4 2302
f42649e8 2303 priv = kzalloc(sizeof(struct alps_data), GFP_KERNEL);
2e5b636b
DT
2304 dev2 = input_allocate_device();
2305 if (!priv || !dev2)
1da177e4 2306 goto init_fail;
2e5b636b
DT
2307
2308 priv->dev2 = dev2;
1d9f2626
SK
2309 setup_timer(&priv->timer, alps_flush_packet, (unsigned long)psmouse);
2310
1e0c5b12 2311 psmouse->private = priv;
1da177e4 2312
25bded7c
SF
2313 psmouse_reset(psmouse);
2314
b5d6b851 2315 if (alps_identify(psmouse, priv) < 0)
71bb21b6
ML
2316 goto init_fail;
2317
24af5cb9 2318 if (priv->hw_init(psmouse))
1da177e4
LT
2319 goto init_fail;
2320
7105d2ea
DT
2321 /*
2322 * Undo part of setup done for us by psmouse core since touchpad
2323 * is not a relative device.
2324 */
2325 __clear_bit(EV_REL, dev1->evbit);
2326 __clear_bit(REL_X, dev1->relbit);
2327 __clear_bit(REL_Y, dev1->relbit);
2328
2329 /*
2330 * Now set up our capabilities.
2331 */
7b19ada2
JS
2332 dev1->evbit[BIT_WORD(EV_KEY)] |= BIT_MASK(EV_KEY);
2333 dev1->keybit[BIT_WORD(BTN_TOUCH)] |= BIT_MASK(BTN_TOUCH);
2334 dev1->keybit[BIT_WORD(BTN_TOOL_FINGER)] |= BIT_MASK(BTN_TOOL_FINGER);
71bb21b6
ML
2335 dev1->keybit[BIT_WORD(BTN_LEFT)] |=
2336 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
1da177e4 2337
7b19ada2 2338 dev1->evbit[BIT_WORD(EV_ABS)] |= BIT_MASK(EV_ABS);
25bded7c 2339
24af5cb9 2340 priv->set_abs_params(priv, dev1);
3808843c
YT
2341 /* No pressure on V7 */
2342 if (priv->proto_version != ALPS_PROTO_V7)
2343 input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
1da177e4 2344
99df65e7 2345 if (priv->flags & ALPS_WHEEL) {
7b19ada2
JS
2346 dev1->evbit[BIT_WORD(EV_REL)] |= BIT_MASK(EV_REL);
2347 dev1->relbit[BIT_WORD(REL_WHEEL)] |= BIT_MASK(REL_WHEEL);
1da177e4
LT
2348 }
2349
99df65e7 2350 if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
7b19ada2
JS
2351 dev1->keybit[BIT_WORD(BTN_FORWARD)] |= BIT_MASK(BTN_FORWARD);
2352 dev1->keybit[BIT_WORD(BTN_BACK)] |= BIT_MASK(BTN_BACK);
1da177e4
LT
2353 }
2354
99df65e7 2355 if (priv->flags & ALPS_FOUR_BUTTONS) {
71bb21b6
ML
2356 dev1->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_0);
2357 dev1->keybit[BIT_WORD(BTN_1)] |= BIT_MASK(BTN_1);
2358 dev1->keybit[BIT_WORD(BTN_2)] |= BIT_MASK(BTN_2);
2359 dev1->keybit[BIT_WORD(BTN_3)] |= BIT_MASK(BTN_3);
3808843c
YT
2360 } else if (priv->flags & ALPS_BUTTONPAD) {
2361 set_bit(INPUT_PROP_BUTTONPAD, dev1->propbit);
2362 clear_bit(BTN_RIGHT, dev1->keybit);
71bb21b6
ML
2363 } else {
2364 dev1->keybit[BIT_WORD(BTN_MIDDLE)] |= BIT_MASK(BTN_MIDDLE);
2365 }
2366
08ffce45 2367 snprintf(priv->phys, sizeof(priv->phys), "%s/input1", psmouse->ps2dev.serio->phys);
2e5b636b 2368 dev2->phys = priv->phys;
99df65e7 2369 dev2->name = (priv->flags & ALPS_DUALPOINT) ?
9354f263 2370 "DualPoint Stick" : "ALPS PS/2 Device";
2e5b636b
DT
2371 dev2->id.bustype = BUS_I8042;
2372 dev2->id.vendor = 0x0002;
2373 dev2->id.product = PSMOUSE_ALPS;
2374 dev2->id.version = 0x0000;
1db3a345 2375 dev2->dev.parent = &psmouse->ps2dev.serio->dev;
1da177e4 2376
7b19ada2 2377 dev2->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
71bb21b6
ML
2378 dev2->relbit[BIT_WORD(REL_X)] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
2379 dev2->keybit[BIT_WORD(BTN_LEFT)] =
2380 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_MIDDLE) | BIT_MASK(BTN_RIGHT);
1da177e4 2381
01d4cd5c 2382 __set_bit(INPUT_PROP_POINTER, dev2->propbit);
7611392f
HG
2383 if (priv->flags & ALPS_DUALPOINT)
2384 __set_bit(INPUT_PROP_POINTING_STICK, dev2->propbit);
2385
f42649e8
DT
2386 if (input_register_device(priv->dev2))
2387 goto init_fail;
1da177e4
LT
2388
2389 psmouse->protocol_handler = alps_process_byte;
f0d5c6f4 2390 psmouse->poll = alps_poll;
1da177e4
LT
2391 psmouse->disconnect = alps_disconnect;
2392 psmouse->reconnect = alps_reconnect;
99df65e7 2393 psmouse->pktsize = priv->proto_version == ALPS_PROTO_V4 ? 8 : 6;
1da177e4 2394
f0d5c6f4
DT
2395 /* We are having trouble resyncing ALPS touchpads so disable it for now */
2396 psmouse->resync_time = 0;
2397
9d720b34
PR
2398 /* Allow 2 invalid packets without resetting device */
2399 psmouse->resetafter = psmouse->pktsize * 2;
2400
1da177e4
LT
2401 return 0;
2402
2403init_fail:
f42649e8 2404 psmouse_reset(psmouse);
2e5b636b 2405 input_free_device(dev2);
1da177e4 2406 kfree(priv);
1e0c5b12 2407 psmouse->private = NULL;
1da177e4
LT
2408 return -1;
2409}
2410
b7802c5c 2411int alps_detect(struct psmouse *psmouse, bool set_properties)
1da177e4 2412{
b5d6b851 2413 struct alps_data dummy;
1da177e4 2414
b5d6b851 2415 if (alps_identify(psmouse, &dummy) < 0)
1da177e4
LT
2416 return -1;
2417
2418 if (set_properties) {
2419 psmouse->vendor = "ALPS";
b5d6b851 2420 psmouse->name = dummy.flags & ALPS_DUALPOINT ?
968ac842 2421 "DualPoint TouchPad" : "GlidePoint";
b5d6b851 2422 psmouse->model = dummy.proto_version << 8;
1da177e4
LT
2423 }
2424 return 0;
2425}
2426