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