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