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