Commit | Line | Data |
---|---|---|
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 | ||
32 | static const struct alps_nibble_commands alps_v3_nibble_commands[] = { | |
33 | { PSMOUSE_CMD_SETPOLL, 0x00 }, /* 0 */ | |
34 | { PSMOUSE_CMD_RESET_DIS, 0x00 }, /* 1 */ | |
35 | { PSMOUSE_CMD_SETSCALE21, 0x00 }, /* 2 */ | |
36 | { PSMOUSE_CMD_SETRATE, 0x0a }, /* 3 */ | |
37 | { PSMOUSE_CMD_SETRATE, 0x14 }, /* 4 */ | |
38 | { PSMOUSE_CMD_SETRATE, 0x28 }, /* 5 */ | |
39 | { PSMOUSE_CMD_SETRATE, 0x3c }, /* 6 */ | |
40 | { PSMOUSE_CMD_SETRATE, 0x50 }, /* 7 */ | |
41 | { PSMOUSE_CMD_SETRATE, 0x64 }, /* 8 */ | |
42 | { PSMOUSE_CMD_SETRATE, 0xc8 }, /* 9 */ | |
43 | { ALPS_CMD_NIBBLE_10, 0x00 }, /* a */ | |
44 | { PSMOUSE_CMD_SETRES, 0x00 }, /* b */ | |
45 | { PSMOUSE_CMD_SETRES, 0x01 }, /* c */ | |
46 | { PSMOUSE_CMD_SETRES, 0x02 }, /* d */ | |
47 | { PSMOUSE_CMD_SETRES, 0x03 }, /* e */ | |
48 | { PSMOUSE_CMD_SETSCALE11, 0x00 }, /* f */ | |
49 | }; | |
50 | ||
51 | static const struct alps_nibble_commands alps_v4_nibble_commands[] = { | |
52 | { PSMOUSE_CMD_ENABLE, 0x00 }, /* 0 */ | |
53 | { PSMOUSE_CMD_RESET_DIS, 0x00 }, /* 1 */ | |
54 | { PSMOUSE_CMD_SETSCALE21, 0x00 }, /* 2 */ | |
55 | { PSMOUSE_CMD_SETRATE, 0x0a }, /* 3 */ | |
56 | { PSMOUSE_CMD_SETRATE, 0x14 }, /* 4 */ | |
57 | { PSMOUSE_CMD_SETRATE, 0x28 }, /* 5 */ | |
58 | { PSMOUSE_CMD_SETRATE, 0x3c }, /* 6 */ | |
59 | { PSMOUSE_CMD_SETRATE, 0x50 }, /* 7 */ | |
60 | { PSMOUSE_CMD_SETRATE, 0x64 }, /* 8 */ | |
61 | { PSMOUSE_CMD_SETRATE, 0xc8 }, /* 9 */ | |
62 | { ALPS_CMD_NIBBLE_10, 0x00 }, /* a */ | |
63 | { PSMOUSE_CMD_SETRES, 0x00 }, /* b */ | |
64 | { PSMOUSE_CMD_SETRES, 0x01 }, /* c */ | |
65 | { PSMOUSE_CMD_SETRES, 0x02 }, /* d */ | |
66 | { PSMOUSE_CMD_SETRES, 0x03 }, /* e */ | |
67 | { PSMOUSE_CMD_SETSCALE11, 0x00 }, /* f */ | |
68 | }; | |
69 | ||
70 | ||
71bb21b6 ML |
71 | #define ALPS_DUALPOINT 0x02 /* touchpad has trackstick */ |
72 | #define ALPS_PASS 0x04 /* device has a pass-through port */ | |
73 | ||
74 | #define ALPS_WHEEL 0x08 /* hardware wheel present */ | |
75 | #define ALPS_FW_BK_1 0x10 /* front & back buttons present */ | |
76 | #define ALPS_FW_BK_2 0x20 /* front & back buttons present */ | |
77 | #define ALPS_FOUR_BUTTONS 0x40 /* 4 direction button present */ | |
1d9f2626 SK |
78 | #define ALPS_PS2_INTERLEAVED 0x80 /* 3-byte PS/2 packet interleaved with |
79 | 6-byte ALPS packet */ | |
1da177e4 | 80 | |
e38de678 | 81 | static const struct alps_model_info alps_model_data[] = { |
25bded7c SF |
82 | { { 0x32, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT }, /* Toshiba Salellite Pro M10 */ |
83 | { { 0x33, 0x02, 0x0a }, 0x00, ALPS_PROTO_V1, 0x88, 0xf8, 0 }, /* UMAX-530T */ | |
84 | { { 0x53, 0x02, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 }, | |
85 | { { 0x53, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 }, | |
86 | { { 0x60, 0x03, 0xc8 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 }, /* HP ze1115 */ | |
87 | { { 0x63, 0x02, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 }, | |
88 | { { 0x63, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 }, | |
89 | { { 0x63, 0x02, 0x28 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 }, /* Fujitsu Siemens S6010 */ | |
90 | { { 0x63, 0x02, 0x3c }, 0x00, ALPS_PROTO_V2, 0x8f, 0x8f, ALPS_WHEEL }, /* Toshiba Satellite S2400-103 */ | |
91 | { { 0x63, 0x02, 0x50 }, 0x00, ALPS_PROTO_V2, 0xef, 0xef, ALPS_FW_BK_1 }, /* NEC Versa L320 */ | |
92 | { { 0x63, 0x02, 0x64 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 }, | |
93 | { { 0x63, 0x03, 0xc8 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT }, /* Dell Latitude D800 */ | |
94 | { { 0x73, 0x00, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_DUALPOINT }, /* ThinkPad R61 8918-5QG */ | |
95 | { { 0x73, 0x02, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 }, | |
96 | { { 0x73, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 }, /* Ahtec Laptop */ | |
97 | { { 0x20, 0x02, 0x0e }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT }, /* XXX */ | |
98 | { { 0x22, 0x02, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT }, | |
99 | { { 0x22, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xff, 0xff, ALPS_PASS | ALPS_DUALPOINT }, /* Dell Latitude D600 */ | |
1d9f2626 | 100 | /* Dell Latitude E5500, E6400, E6500, Precision M4400 */ |
25bded7c | 101 | { { 0x62, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xcf, 0xcf, |
1d9f2626 | 102 | ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED }, |
25bded7c SF |
103 | { { 0x73, 0x02, 0x50 }, 0x00, ALPS_PROTO_V2, 0xcf, 0xcf, ALPS_FOUR_BUTTONS }, /* Dell Vostro 1400 */ |
104 | { { 0x52, 0x01, 0x14 }, 0x00, ALPS_PROTO_V2, 0xff, 0xff, | |
105 | ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED }, /* Toshiba Tecra A11-11L */ | |
25bded7c | 106 | { { 0x73, 0x02, 0x64 }, 0x8a, ALPS_PROTO_V4, 0x8f, 0x8f, 0 }, |
1da177e4 LT |
107 | }; |
108 | ||
24af5cb9 KC |
109 | static void alps_set_abs_params_st(struct alps_data *priv, |
110 | struct input_dev *dev1); | |
111 | static void alps_set_abs_params_mt(struct alps_data *priv, | |
112 | struct input_dev *dev1); | |
113 | ||
1da177e4 LT |
114 | /* |
115 | * XXX - this entry is suspicious. First byte has zero lower nibble, | |
116 | * which is what a normal mouse would report. Also, the value 0x0e | |
117 | * isn't valid per PS/2 spec. | |
118 | */ | |
119 | ||
d4b347b2 | 120 | /* Packet formats are described in Documentation/input/alps.txt */ |
1da177e4 | 121 | |
99df65e7 | 122 | static bool alps_is_valid_first_byte(struct alps_data *priv, |
1d9f2626 SK |
123 | unsigned char data) |
124 | { | |
99df65e7 | 125 | return (data & priv->mask0) == priv->byte0; |
1d9f2626 SK |
126 | } |
127 | ||
128 | static void alps_report_buttons(struct psmouse *psmouse, | |
129 | struct input_dev *dev1, struct input_dev *dev2, | |
130 | int left, int right, int middle) | |
131 | { | |
c91ed059 MB |
132 | struct input_dev *dev; |
133 | ||
134 | /* | |
135 | * If shared button has already been reported on the | |
136 | * other device (dev2) then this event should be also | |
137 | * sent through that device. | |
138 | */ | |
139 | dev = test_bit(BTN_LEFT, dev2->key) ? dev2 : dev1; | |
140 | input_report_key(dev, BTN_LEFT, left); | |
141 | ||
142 | dev = test_bit(BTN_RIGHT, dev2->key) ? dev2 : dev1; | |
143 | input_report_key(dev, BTN_RIGHT, right); | |
144 | ||
145 | dev = test_bit(BTN_MIDDLE, dev2->key) ? dev2 : dev1; | |
146 | input_report_key(dev, BTN_MIDDLE, middle); | |
147 | ||
148 | /* | |
149 | * Sync the _other_ device now, we'll do the first | |
150 | * device later once we report the rest of the events. | |
151 | */ | |
152 | input_sync(dev2); | |
1d9f2626 SK |
153 | } |
154 | ||
25bded7c | 155 | static void alps_process_packet_v1_v2(struct psmouse *psmouse) |
1da177e4 LT |
156 | { |
157 | struct alps_data *priv = psmouse->private; | |
158 | unsigned char *packet = psmouse->packet; | |
2e5b636b DT |
159 | struct input_dev *dev = psmouse->dev; |
160 | struct input_dev *dev2 = priv->dev2; | |
1da177e4 | 161 | int x, y, z, ges, fin, left, right, middle; |
c30b4c10 | 162 | int back = 0, forward = 0; |
1da177e4 | 163 | |
99df65e7 | 164 | if (priv->proto_version == ALPS_PROTO_V1) { |
d2f4012f YM |
165 | left = packet[2] & 0x10; |
166 | right = packet[2] & 0x08; | |
1da177e4 LT |
167 | middle = 0; |
168 | x = packet[1] | ((packet[0] & 0x07) << 7); | |
169 | y = packet[4] | ((packet[3] & 0x07) << 7); | |
170 | z = packet[5]; | |
171 | } else { | |
172 | left = packet[3] & 1; | |
173 | right = packet[3] & 2; | |
174 | middle = packet[3] & 4; | |
175 | x = packet[1] | ((packet[2] & 0x78) << (7 - 3)); | |
176 | y = packet[4] | ((packet[3] & 0x70) << (7 - 4)); | |
177 | z = packet[5]; | |
178 | } | |
179 | ||
99df65e7 | 180 | if (priv->flags & ALPS_FW_BK_1) { |
3c00bb96 LK |
181 | back = packet[0] & 0x10; |
182 | forward = packet[2] & 4; | |
c30b4c10 ICR |
183 | } |
184 | ||
99df65e7 | 185 | if (priv->flags & ALPS_FW_BK_2) { |
c30b4c10 ICR |
186 | back = packet[3] & 4; |
187 | forward = packet[2] & 4; | |
188 | if ((middle = forward && back)) | |
189 | forward = back = 0; | |
190 | } | |
191 | ||
1da177e4 LT |
192 | ges = packet[2] & 1; |
193 | fin = packet[2] & 2; | |
194 | ||
99df65e7 | 195 | if ((priv->flags & ALPS_DUALPOINT) && z == 127) { |
1da177e4 LT |
196 | input_report_rel(dev2, REL_X, (x > 383 ? (x - 768) : x)); |
197 | input_report_rel(dev2, REL_Y, -(y > 255 ? (y - 512) : y)); | |
d7ed5d88 | 198 | |
1d9f2626 | 199 | alps_report_buttons(psmouse, dev2, dev, left, right, middle); |
d7ed5d88 | 200 | |
1da177e4 LT |
201 | input_sync(dev2); |
202 | return; | |
203 | } | |
204 | ||
1d9f2626 | 205 | alps_report_buttons(psmouse, dev, dev2, left, right, middle); |
d7ed5d88 | 206 | |
1da177e4 | 207 | /* Convert hardware tap to a reasonable Z value */ |
71bb21b6 ML |
208 | if (ges && !fin) |
209 | z = 40; | |
1da177e4 LT |
210 | |
211 | /* | |
212 | * A "tap and drag" operation is reported by the hardware as a transition | |
213 | * from (!fin && ges) to (fin && ges). This should be translated to the | |
214 | * sequence Z>0, Z==0, Z>0, so the Z==0 event has to be generated manually. | |
215 | */ | |
216 | if (ges && fin && !priv->prev_fin) { | |
217 | input_report_abs(dev, ABS_X, x); | |
218 | input_report_abs(dev, ABS_Y, y); | |
219 | input_report_abs(dev, ABS_PRESSURE, 0); | |
220 | input_report_key(dev, BTN_TOOL_FINGER, 0); | |
221 | input_sync(dev); | |
222 | } | |
223 | priv->prev_fin = fin; | |
224 | ||
71bb21b6 ML |
225 | if (z > 30) |
226 | input_report_key(dev, BTN_TOUCH, 1); | |
227 | if (z < 25) | |
228 | input_report_key(dev, BTN_TOUCH, 0); | |
1da177e4 LT |
229 | |
230 | if (z > 0) { | |
231 | input_report_abs(dev, ABS_X, x); | |
232 | input_report_abs(dev, ABS_Y, y); | |
233 | } | |
234 | ||
235 | input_report_abs(dev, ABS_PRESSURE, z); | |
236 | input_report_key(dev, BTN_TOOL_FINGER, z > 0); | |
237 | ||
99df65e7 | 238 | if (priv->flags & ALPS_WHEEL) |
e6c047b9 | 239 | input_report_rel(dev, REL_WHEEL, ((packet[2] << 1) & 0x08) - ((packet[0] >> 4) & 0x07)); |
1da177e4 | 240 | |
99df65e7 | 241 | if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) { |
c30b4c10 ICR |
242 | input_report_key(dev, BTN_FORWARD, forward); |
243 | input_report_key(dev, BTN_BACK, back); | |
1da177e4 LT |
244 | } |
245 | ||
99df65e7 | 246 | if (priv->flags & ALPS_FOUR_BUTTONS) { |
71bb21b6 ML |
247 | input_report_key(dev, BTN_0, packet[2] & 4); |
248 | input_report_key(dev, BTN_1, packet[0] & 0x10); | |
249 | input_report_key(dev, BTN_2, packet[3] & 4); | |
250 | input_report_key(dev, BTN_3, packet[0] & 0x20); | |
251 | } | |
252 | ||
1da177e4 LT |
253 | input_sync(dev); |
254 | } | |
255 | ||
01ce661f SF |
256 | /* |
257 | * Process bitmap data from v3 and v4 protocols. Returns the number of | |
258 | * fingers detected. A return value of 0 means at least one of the | |
259 | * bitmaps was empty. | |
260 | * | |
261 | * The bitmaps don't have enough data to track fingers, so this function | |
262 | * only generates points representing a bounding box of all contacts. | |
263 | * These points are returned in x1, y1, x2, and y2 when the return value | |
264 | * is greater than 0. | |
265 | */ | |
7a9f73e7 KC |
266 | static int alps_process_bitmap(struct alps_data *priv, |
267 | unsigned int x_map, unsigned int y_map, | |
01ce661f SF |
268 | int *x1, int *y1, int *x2, int *y2) |
269 | { | |
270 | struct alps_bitmap_point { | |
271 | int start_bit; | |
272 | int num_bits; | |
273 | }; | |
274 | ||
275 | int fingers_x = 0, fingers_y = 0, fingers; | |
276 | int i, bit, prev_bit; | |
277 | struct alps_bitmap_point x_low = {0,}, x_high = {0,}; | |
278 | struct alps_bitmap_point y_low = {0,}, y_high = {0,}; | |
279 | struct alps_bitmap_point *point; | |
280 | ||
281 | if (!x_map || !y_map) | |
282 | return 0; | |
283 | ||
284 | *x1 = *y1 = *x2 = *y2 = 0; | |
285 | ||
286 | prev_bit = 0; | |
287 | point = &x_low; | |
288 | for (i = 0; x_map != 0; i++, x_map >>= 1) { | |
289 | bit = x_map & 1; | |
290 | if (bit) { | |
291 | if (!prev_bit) { | |
292 | point->start_bit = i; | |
293 | fingers_x++; | |
294 | } | |
295 | point->num_bits++; | |
296 | } else { | |
297 | if (prev_bit) | |
298 | point = &x_high; | |
299 | else | |
300 | point->num_bits = 0; | |
301 | } | |
302 | prev_bit = bit; | |
303 | } | |
304 | ||
305 | /* | |
306 | * y bitmap is reversed for what we need (lower positions are in | |
307 | * higher bits), so we process from the top end. | |
308 | */ | |
7a9f73e7 | 309 | y_map = y_map << (sizeof(y_map) * BITS_PER_BYTE - priv->y_bits); |
01ce661f SF |
310 | prev_bit = 0; |
311 | point = &y_low; | |
312 | for (i = 0; y_map != 0; i++, y_map <<= 1) { | |
313 | bit = y_map & (1 << (sizeof(y_map) * BITS_PER_BYTE - 1)); | |
314 | if (bit) { | |
315 | if (!prev_bit) { | |
316 | point->start_bit = i; | |
317 | fingers_y++; | |
318 | } | |
319 | point->num_bits++; | |
320 | } else { | |
321 | if (prev_bit) | |
322 | point = &y_high; | |
323 | else | |
324 | point->num_bits = 0; | |
325 | } | |
326 | prev_bit = bit; | |
327 | } | |
328 | ||
329 | /* | |
330 | * Fingers can overlap, so we use the maximum count of fingers | |
331 | * on either axis as the finger count. | |
332 | */ | |
333 | fingers = max(fingers_x, fingers_y); | |
334 | ||
335 | /* | |
336 | * If total fingers is > 1 but either axis reports only a single | |
337 | * contact, we have overlapping or adjacent fingers. For the | |
338 | * purposes of creating a bounding box, divide the single contact | |
339 | * (roughly) equally between the two points. | |
340 | */ | |
341 | if (fingers > 1) { | |
342 | if (fingers_x == 1) { | |
343 | i = x_low.num_bits / 2; | |
344 | x_low.num_bits = x_low.num_bits - i; | |
345 | x_high.start_bit = x_low.start_bit + i; | |
346 | x_high.num_bits = max(i, 1); | |
347 | } else if (fingers_y == 1) { | |
348 | i = y_low.num_bits / 2; | |
349 | y_low.num_bits = y_low.num_bits - i; | |
350 | y_high.start_bit = y_low.start_bit + i; | |
351 | y_high.num_bits = max(i, 1); | |
352 | } | |
353 | } | |
354 | ||
7a9f73e7 KC |
355 | *x1 = (priv->x_max * (2 * x_low.start_bit + x_low.num_bits - 1)) / |
356 | (2 * (priv->x_bits - 1)); | |
357 | *y1 = (priv->y_max * (2 * y_low.start_bit + y_low.num_bits - 1)) / | |
358 | (2 * (priv->y_bits - 1)); | |
01ce661f SF |
359 | |
360 | if (fingers > 1) { | |
7a9f73e7 KC |
361 | *x2 = (priv->x_max * |
362 | (2 * x_high.start_bit + x_high.num_bits - 1)) / | |
363 | (2 * (priv->x_bits - 1)); | |
364 | *y2 = (priv->y_max * | |
365 | (2 * y_high.start_bit + y_high.num_bits - 1)) / | |
366 | (2 * (priv->y_bits - 1)); | |
01ce661f SF |
367 | } |
368 | ||
369 | return fingers; | |
370 | } | |
371 | ||
372 | static void alps_set_slot(struct input_dev *dev, int slot, bool active, | |
373 | int x, int y) | |
374 | { | |
375 | input_mt_slot(dev, slot); | |
376 | input_mt_report_slot_state(dev, MT_TOOL_FINGER, active); | |
377 | if (active) { | |
378 | input_report_abs(dev, ABS_MT_POSITION_X, x); | |
379 | input_report_abs(dev, ABS_MT_POSITION_Y, y); | |
380 | } | |
381 | } | |
382 | ||
383 | static void alps_report_semi_mt_data(struct input_dev *dev, int num_fingers, | |
384 | int x1, int y1, int x2, int y2) | |
385 | { | |
386 | alps_set_slot(dev, 0, num_fingers != 0, x1, y1); | |
387 | alps_set_slot(dev, 1, num_fingers == 2, x2, y2); | |
388 | } | |
389 | ||
25bded7c SF |
390 | static void alps_process_trackstick_packet_v3(struct psmouse *psmouse) |
391 | { | |
392 | struct alps_data *priv = psmouse->private; | |
393 | unsigned char *packet = psmouse->packet; | |
394 | struct input_dev *dev = priv->dev2; | |
395 | int x, y, z, left, right, middle; | |
396 | ||
397 | /* Sanity check packet */ | |
398 | if (!(packet[0] & 0x40)) { | |
399 | psmouse_dbg(psmouse, "Bad trackstick packet, discarding\n"); | |
400 | return; | |
401 | } | |
402 | ||
403 | /* | |
404 | * There's a special packet that seems to indicate the end | |
405 | * of a stream of trackstick data. Filter these out. | |
406 | */ | |
407 | if (packet[1] == 0x7f && packet[2] == 0x7f && packet[4] == 0x7f) | |
408 | return; | |
409 | ||
410 | x = (s8)(((packet[0] & 0x20) << 2) | (packet[1] & 0x7f)); | |
411 | y = (s8)(((packet[0] & 0x10) << 3) | (packet[2] & 0x7f)); | |
412 | z = (packet[4] & 0x7c) >> 2; | |
413 | ||
414 | /* | |
415 | * The x and y values tend to be quite large, and when used | |
416 | * alone the trackstick is difficult to use. Scale them down | |
417 | * to compensate. | |
418 | */ | |
419 | x /= 8; | |
420 | y /= 8; | |
421 | ||
422 | input_report_rel(dev, REL_X, x); | |
423 | input_report_rel(dev, REL_Y, -y); | |
424 | ||
425 | /* | |
426 | * Most ALPS models report the trackstick buttons in the touchpad | |
427 | * packets, but a few report them here. No reliable way has been | |
428 | * found to differentiate between the models upfront, so we enable | |
429 | * the quirk in response to seeing a button press in the trackstick | |
430 | * packet. | |
431 | */ | |
432 | left = packet[3] & 0x01; | |
433 | right = packet[3] & 0x02; | |
434 | middle = packet[3] & 0x04; | |
435 | ||
436 | if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) && | |
437 | (left || right || middle)) | |
438 | priv->quirks |= ALPS_QUIRK_TRACKSTICK_BUTTONS; | |
439 | ||
440 | if (priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) { | |
441 | input_report_key(dev, BTN_LEFT, left); | |
442 | input_report_key(dev, BTN_RIGHT, right); | |
443 | input_report_key(dev, BTN_MIDDLE, middle); | |
444 | } | |
445 | ||
446 | input_sync(dev); | |
447 | return; | |
448 | } | |
449 | ||
f85e5001 KC |
450 | static void alps_decode_buttons_v3(struct alps_fields *f, unsigned char *p) |
451 | { | |
452 | f->left = !!(p[3] & 0x01); | |
453 | f->right = !!(p[3] & 0x02); | |
454 | f->middle = !!(p[3] & 0x04); | |
455 | ||
456 | f->ts_left = !!(p[3] & 0x10); | |
457 | f->ts_right = !!(p[3] & 0x20); | |
458 | f->ts_middle = !!(p[3] & 0x40); | |
459 | } | |
460 | ||
461 | static void alps_decode_pinnacle(struct alps_fields *f, unsigned char *p) | |
462 | { | |
463 | f->first_mp = !!(p[4] & 0x40); | |
464 | f->is_mp = !!(p[0] & 0x40); | |
465 | ||
466 | f->fingers = (p[5] & 0x3) + 1; | |
467 | f->x_map = ((p[4] & 0x7e) << 8) | | |
468 | ((p[1] & 0x7f) << 2) | | |
469 | ((p[0] & 0x30) >> 4); | |
470 | f->y_map = ((p[3] & 0x70) << 4) | | |
471 | ((p[2] & 0x7f) << 1) | | |
472 | (p[4] & 0x01); | |
473 | ||
474 | f->x = ((p[1] & 0x7f) << 4) | ((p[4] & 0x30) >> 2) | | |
475 | ((p[0] & 0x30) >> 4); | |
476 | f->y = ((p[2] & 0x7f) << 4) | (p[4] & 0x0f); | |
477 | f->z = p[5] & 0x7f; | |
478 | ||
479 | alps_decode_buttons_v3(f, p); | |
480 | } | |
481 | ||
25bded7c SF |
482 | static void alps_process_touchpad_packet_v3(struct psmouse *psmouse) |
483 | { | |
484 | struct alps_data *priv = psmouse->private; | |
485 | unsigned char *packet = psmouse->packet; | |
486 | struct input_dev *dev = psmouse->dev; | |
487 | struct input_dev *dev2 = priv->dev2; | |
01ce661f SF |
488 | int x1 = 0, y1 = 0, x2 = 0, y2 = 0; |
489 | int fingers = 0, bmap_fingers; | |
f85e5001 KC |
490 | struct alps_fields f; |
491 | ||
492 | priv->decode_fields(&f, packet); | |
25bded7c SF |
493 | |
494 | /* | |
01ce661f SF |
495 | * There's no single feature of touchpad position and bitmap packets |
496 | * that can be used to distinguish between them. We rely on the fact | |
497 | * that a bitmap packet should always follow a position packet with | |
498 | * bit 6 of packet[4] set. | |
25bded7c SF |
499 | */ |
500 | if (priv->multi_packet) { | |
25bded7c SF |
501 | /* |
502 | * Sometimes a position packet will indicate a multi-packet | |
503 | * sequence, but then what follows is another position | |
504 | * packet. Check for this, and when it happens process the | |
505 | * position packet as usual. | |
506 | */ | |
f85e5001 KC |
507 | if (f.is_mp) { |
508 | fingers = f.fingers; | |
7a9f73e7 | 509 | bmap_fingers = alps_process_bitmap(priv, |
f85e5001 | 510 | f.x_map, f.y_map, |
01ce661f SF |
511 | &x1, &y1, &x2, &y2); |
512 | ||
25bded7c | 513 | /* |
01ce661f SF |
514 | * We shouldn't report more than one finger if |
515 | * we don't have two coordinates. | |
25bded7c | 516 | */ |
01ce661f SF |
517 | if (fingers > 1 && bmap_fingers < 2) |
518 | fingers = bmap_fingers; | |
519 | ||
520 | /* Now process position packet */ | |
f85e5001 | 521 | priv->decode_fields(&f, priv->multi_data); |
01ce661f SF |
522 | } else { |
523 | priv->multi_packet = 0; | |
25bded7c SF |
524 | } |
525 | } | |
526 | ||
01ce661f SF |
527 | /* |
528 | * Bit 6 of byte 0 is not usually set in position packets. The only | |
529 | * times it seems to be set is in situations where the data is | |
530 | * suspect anyway, e.g. a palm resting flat on the touchpad. Given | |
531 | * this combined with the fact that this bit is useful for filtering | |
532 | * out misidentified bitmap packets, we reject anything with this | |
533 | * bit set. | |
534 | */ | |
f85e5001 | 535 | if (f.is_mp) |
01ce661f SF |
536 | return; |
537 | ||
f85e5001 | 538 | if (!priv->multi_packet && f.first_mp) { |
25bded7c | 539 | priv->multi_packet = 1; |
01ce661f SF |
540 | memcpy(priv->multi_data, packet, sizeof(priv->multi_data)); |
541 | return; | |
542 | } | |
543 | ||
544 | priv->multi_packet = 0; | |
25bded7c | 545 | |
25bded7c SF |
546 | /* |
547 | * Sometimes the hardware sends a single packet with z = 0 | |
548 | * in the middle of a stream. Real releases generate packets | |
549 | * with x, y, and z all zero, so these seem to be flukes. | |
550 | * Ignore them. | |
551 | */ | |
f85e5001 | 552 | if (f.x && f.y && !f.z) |
25bded7c SF |
553 | return; |
554 | ||
01ce661f SF |
555 | /* |
556 | * If we don't have MT data or the bitmaps were empty, we have | |
557 | * to rely on ST data. | |
558 | */ | |
559 | if (!fingers) { | |
f85e5001 KC |
560 | x1 = f.x; |
561 | y1 = f.y; | |
562 | fingers = f.z > 0 ? 1 : 0; | |
01ce661f SF |
563 | } |
564 | ||
f85e5001 | 565 | if (f.z >= 64) |
25bded7c SF |
566 | input_report_key(dev, BTN_TOUCH, 1); |
567 | else | |
568 | input_report_key(dev, BTN_TOUCH, 0); | |
569 | ||
01ce661f SF |
570 | alps_report_semi_mt_data(dev, fingers, x1, y1, x2, y2); |
571 | ||
616575c2 | 572 | input_mt_report_finger_count(dev, fingers); |
01ce661f | 573 | |
f85e5001 KC |
574 | input_report_key(dev, BTN_LEFT, f.left); |
575 | input_report_key(dev, BTN_RIGHT, f.right); | |
576 | input_report_key(dev, BTN_MIDDLE, f.middle); | |
01ce661f | 577 | |
f85e5001 KC |
578 | if (f.z > 0) { |
579 | input_report_abs(dev, ABS_X, f.x); | |
580 | input_report_abs(dev, ABS_Y, f.y); | |
25bded7c | 581 | } |
f85e5001 | 582 | input_report_abs(dev, ABS_PRESSURE, f.z); |
25bded7c | 583 | |
25bded7c SF |
584 | input_sync(dev); |
585 | ||
586 | if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS)) { | |
f85e5001 KC |
587 | input_report_key(dev2, BTN_LEFT, f.ts_left); |
588 | input_report_key(dev2, BTN_RIGHT, f.ts_right); | |
589 | input_report_key(dev2, BTN_MIDDLE, f.ts_middle); | |
25bded7c SF |
590 | input_sync(dev2); |
591 | } | |
592 | } | |
593 | ||
594 | static void alps_process_packet_v3(struct psmouse *psmouse) | |
595 | { | |
596 | unsigned char *packet = psmouse->packet; | |
597 | ||
598 | /* | |
599 | * v3 protocol packets come in three types, two representing | |
600 | * touchpad data and one representing trackstick data. | |
601 | * Trackstick packets seem to be distinguished by always | |
602 | * having 0x3f in the last byte. This value has never been | |
603 | * observed in the last byte of either of the other types | |
604 | * of packets. | |
605 | */ | |
606 | if (packet[5] == 0x3f) { | |
607 | alps_process_trackstick_packet_v3(psmouse); | |
608 | return; | |
609 | } | |
610 | ||
611 | alps_process_touchpad_packet_v3(psmouse); | |
612 | } | |
613 | ||
614 | static void alps_process_packet_v4(struct psmouse *psmouse) | |
615 | { | |
3b7e09fa | 616 | struct alps_data *priv = psmouse->private; |
25bded7c SF |
617 | unsigned char *packet = psmouse->packet; |
618 | struct input_dev *dev = psmouse->dev; | |
3b7e09fa | 619 | int offset; |
25bded7c SF |
620 | int x, y, z; |
621 | int left, right; | |
3b7e09fa GP |
622 | int x1, y1, x2, y2; |
623 | int fingers = 0; | |
624 | unsigned int x_bitmap, y_bitmap; | |
625 | ||
626 | /* | |
627 | * v4 has a 6-byte encoding for bitmap data, but this data is | |
628 | * broken up between 3 normal packets. Use priv->multi_packet to | |
629 | * track our position in the bitmap packet. | |
630 | */ | |
631 | if (packet[6] & 0x40) { | |
632 | /* sync, reset position */ | |
633 | priv->multi_packet = 0; | |
634 | } | |
635 | ||
636 | if (WARN_ON_ONCE(priv->multi_packet > 2)) | |
637 | return; | |
638 | ||
639 | offset = 2 * priv->multi_packet; | |
640 | priv->multi_data[offset] = packet[6]; | |
641 | priv->multi_data[offset + 1] = packet[7]; | |
642 | ||
643 | if (++priv->multi_packet > 2) { | |
644 | priv->multi_packet = 0; | |
645 | ||
646 | x_bitmap = ((priv->multi_data[2] & 0x1f) << 10) | | |
647 | ((priv->multi_data[3] & 0x60) << 3) | | |
648 | ((priv->multi_data[0] & 0x3f) << 2) | | |
649 | ((priv->multi_data[1] & 0x60) >> 5); | |
650 | y_bitmap = ((priv->multi_data[5] & 0x01) << 10) | | |
651 | ((priv->multi_data[3] & 0x1f) << 5) | | |
652 | (priv->multi_data[1] & 0x1f); | |
653 | ||
7a9f73e7 | 654 | fingers = alps_process_bitmap(priv, x_bitmap, y_bitmap, |
3b7e09fa GP |
655 | &x1, &y1, &x2, &y2); |
656 | ||
657 | /* Store MT data.*/ | |
658 | priv->fingers = fingers; | |
659 | priv->x1 = x1; | |
660 | priv->x2 = x2; | |
661 | priv->y1 = y1; | |
662 | priv->y2 = y2; | |
663 | } | |
25bded7c SF |
664 | |
665 | left = packet[4] & 0x01; | |
666 | right = packet[4] & 0x02; | |
667 | ||
668 | x = ((packet[1] & 0x7f) << 4) | ((packet[3] & 0x30) >> 2) | | |
669 | ((packet[0] & 0x30) >> 4); | |
670 | y = ((packet[2] & 0x7f) << 4) | (packet[3] & 0x0f); | |
671 | z = packet[5] & 0x7f; | |
672 | ||
3b7e09fa GP |
673 | /* |
674 | * If there were no contacts in the bitmap, use ST | |
675 | * points in MT reports. | |
676 | * If there were two contacts or more, report MT data. | |
677 | */ | |
678 | if (priv->fingers < 2) { | |
679 | x1 = x; | |
680 | y1 = y; | |
681 | fingers = z > 0 ? 1 : 0; | |
682 | } else { | |
683 | fingers = priv->fingers; | |
684 | x1 = priv->x1; | |
685 | x2 = priv->x2; | |
686 | y1 = priv->y1; | |
687 | y2 = priv->y2; | |
688 | } | |
689 | ||
25bded7c SF |
690 | if (z >= 64) |
691 | input_report_key(dev, BTN_TOUCH, 1); | |
692 | else | |
693 | input_report_key(dev, BTN_TOUCH, 0); | |
694 | ||
3b7e09fa GP |
695 | alps_report_semi_mt_data(dev, fingers, x1, y1, x2, y2); |
696 | ||
616575c2 | 697 | input_mt_report_finger_count(dev, fingers); |
3b7e09fa GP |
698 | |
699 | input_report_key(dev, BTN_LEFT, left); | |
700 | input_report_key(dev, BTN_RIGHT, right); | |
701 | ||
25bded7c SF |
702 | if (z > 0) { |
703 | input_report_abs(dev, ABS_X, x); | |
704 | input_report_abs(dev, ABS_Y, y); | |
705 | } | |
706 | input_report_abs(dev, ABS_PRESSURE, z); | |
707 | ||
25bded7c SF |
708 | input_sync(dev); |
709 | } | |
710 | ||
1d9f2626 SK |
711 | static void alps_report_bare_ps2_packet(struct psmouse *psmouse, |
712 | unsigned char packet[], | |
713 | bool report_buttons) | |
714 | { | |
715 | struct alps_data *priv = psmouse->private; | |
716 | struct input_dev *dev2 = priv->dev2; | |
717 | ||
718 | if (report_buttons) | |
719 | alps_report_buttons(psmouse, dev2, psmouse->dev, | |
720 | packet[0] & 1, packet[0] & 2, packet[0] & 4); | |
721 | ||
722 | input_report_rel(dev2, REL_X, | |
723 | packet[1] ? packet[1] - ((packet[0] << 4) & 0x100) : 0); | |
724 | input_report_rel(dev2, REL_Y, | |
725 | packet[2] ? ((packet[0] << 3) & 0x100) - packet[2] : 0); | |
726 | ||
727 | input_sync(dev2); | |
728 | } | |
729 | ||
730 | static psmouse_ret_t alps_handle_interleaved_ps2(struct psmouse *psmouse) | |
731 | { | |
732 | struct alps_data *priv = psmouse->private; | |
733 | ||
734 | if (psmouse->pktcnt < 6) | |
735 | return PSMOUSE_GOOD_DATA; | |
736 | ||
737 | if (psmouse->pktcnt == 6) { | |
738 | /* | |
739 | * Start a timer to flush the packet if it ends up last | |
740 | * 6-byte packet in the stream. Timer needs to fire | |
741 | * psmouse core times out itself. 20 ms should be enough | |
742 | * to decide if we are getting more data or not. | |
743 | */ | |
744 | mod_timer(&priv->timer, jiffies + msecs_to_jiffies(20)); | |
745 | return PSMOUSE_GOOD_DATA; | |
746 | } | |
747 | ||
748 | del_timer(&priv->timer); | |
749 | ||
750 | if (psmouse->packet[6] & 0x80) { | |
751 | ||
752 | /* | |
753 | * Highest bit is set - that means we either had | |
754 | * complete ALPS packet and this is start of the | |
755 | * next packet or we got garbage. | |
756 | */ | |
757 | ||
758 | if (((psmouse->packet[3] | | |
759 | psmouse->packet[4] | | |
760 | psmouse->packet[5]) & 0x80) || | |
99df65e7 | 761 | (!alps_is_valid_first_byte(priv, psmouse->packet[6]))) { |
b5d21704 | 762 | psmouse_dbg(psmouse, |
3b112923 AS |
763 | "refusing packet %4ph (suspected interleaved ps/2)\n", |
764 | psmouse->packet + 3); | |
1d9f2626 SK |
765 | return PSMOUSE_BAD_DATA; |
766 | } | |
767 | ||
24af5cb9 | 768 | priv->process_packet(psmouse); |
1d9f2626 SK |
769 | |
770 | /* Continue with the next packet */ | |
771 | psmouse->packet[0] = psmouse->packet[6]; | |
772 | psmouse->pktcnt = 1; | |
773 | ||
774 | } else { | |
775 | ||
776 | /* | |
777 | * High bit is 0 - that means that we indeed got a PS/2 | |
778 | * packet in the middle of ALPS packet. | |
779 | * | |
780 | * There is also possibility that we got 6-byte ALPS | |
781 | * packet followed by 3-byte packet from trackpoint. We | |
782 | * can not distinguish between these 2 scenarios but | |
b5d21704 | 783 | * because the latter is unlikely to happen in course of |
1d9f2626 SK |
784 | * normal operation (user would need to press all |
785 | * buttons on the pad and start moving trackpoint | |
786 | * without touching the pad surface) we assume former. | |
787 | * Even if we are wrong the wost thing that would happen | |
788 | * the cursor would jump but we should not get protocol | |
b5d21704 | 789 | * de-synchronization. |
1d9f2626 SK |
790 | */ |
791 | ||
792 | alps_report_bare_ps2_packet(psmouse, &psmouse->packet[3], | |
793 | false); | |
794 | ||
795 | /* | |
796 | * Continue with the standard ALPS protocol handling, | |
797 | * but make sure we won't process it as an interleaved | |
798 | * packet again, which may happen if all buttons are | |
799 | * pressed. To avoid this let's reset the 4th bit which | |
800 | * is normally 1. | |
801 | */ | |
802 | psmouse->packet[3] = psmouse->packet[6] & 0xf7; | |
803 | psmouse->pktcnt = 4; | |
804 | } | |
805 | ||
806 | return PSMOUSE_GOOD_DATA; | |
807 | } | |
808 | ||
809 | static void alps_flush_packet(unsigned long data) | |
810 | { | |
811 | struct psmouse *psmouse = (struct psmouse *)data; | |
24af5cb9 | 812 | struct alps_data *priv = psmouse->private; |
1d9f2626 SK |
813 | |
814 | serio_pause_rx(psmouse->ps2dev.serio); | |
815 | ||
b46615fe | 816 | if (psmouse->pktcnt == psmouse->pktsize) { |
1d9f2626 SK |
817 | |
818 | /* | |
819 | * We did not any more data in reasonable amount of time. | |
820 | * Validate the last 3 bytes and process as a standard | |
821 | * ALPS packet. | |
822 | */ | |
823 | if ((psmouse->packet[3] | | |
824 | psmouse->packet[4] | | |
825 | psmouse->packet[5]) & 0x80) { | |
b5d21704 | 826 | psmouse_dbg(psmouse, |
3b112923 AS |
827 | "refusing packet %3ph (suspected interleaved ps/2)\n", |
828 | psmouse->packet + 3); | |
1d9f2626 | 829 | } else { |
24af5cb9 | 830 | priv->process_packet(psmouse); |
1d9f2626 SK |
831 | } |
832 | psmouse->pktcnt = 0; | |
833 | } | |
834 | ||
835 | serio_continue_rx(psmouse->ps2dev.serio); | |
836 | } | |
837 | ||
7d12e780 | 838 | static psmouse_ret_t alps_process_byte(struct psmouse *psmouse) |
1da177e4 LT |
839 | { |
840 | struct alps_data *priv = psmouse->private; | |
841 | ||
842 | if ((psmouse->packet[0] & 0xc8) == 0x08) { /* PS/2 packet */ | |
843 | if (psmouse->pktcnt == 3) { | |
1d9f2626 SK |
844 | alps_report_bare_ps2_packet(psmouse, psmouse->packet, |
845 | true); | |
1da177e4 LT |
846 | return PSMOUSE_FULL_PACKET; |
847 | } | |
848 | return PSMOUSE_GOOD_DATA; | |
849 | } | |
850 | ||
1d9f2626 SK |
851 | /* Check for PS/2 packet stuffed in the middle of ALPS packet. */ |
852 | ||
99df65e7 | 853 | if ((priv->flags & ALPS_PS2_INTERLEAVED) && |
1d9f2626 SK |
854 | psmouse->pktcnt >= 4 && (psmouse->packet[3] & 0x0f) == 0x0f) { |
855 | return alps_handle_interleaved_ps2(psmouse); | |
856 | } | |
857 | ||
99df65e7 | 858 | if (!alps_is_valid_first_byte(priv, psmouse->packet[0])) { |
b5d21704 DT |
859 | psmouse_dbg(psmouse, |
860 | "refusing packet[0] = %x (mask0 = %x, byte0 = %x)\n", | |
99df65e7 | 861 | psmouse->packet[0], priv->mask0, priv->byte0); |
1da177e4 | 862 | return PSMOUSE_BAD_DATA; |
1d9f2626 | 863 | } |
1da177e4 | 864 | |
b46615fe SF |
865 | /* Bytes 2 - pktsize should have 0 in the highest bit */ |
866 | if (psmouse->pktcnt >= 2 && psmouse->pktcnt <= psmouse->pktsize && | |
1d9f2626 | 867 | (psmouse->packet[psmouse->pktcnt - 1] & 0x80)) { |
b5d21704 DT |
868 | psmouse_dbg(psmouse, "refusing packet[%i] = %x\n", |
869 | psmouse->pktcnt - 1, | |
870 | psmouse->packet[psmouse->pktcnt - 1]); | |
1da177e4 | 871 | return PSMOUSE_BAD_DATA; |
1d9f2626 | 872 | } |
1da177e4 | 873 | |
b46615fe | 874 | if (psmouse->pktcnt == psmouse->pktsize) { |
24af5cb9 | 875 | priv->process_packet(psmouse); |
1da177e4 LT |
876 | return PSMOUSE_FULL_PACKET; |
877 | } | |
878 | ||
879 | return PSMOUSE_GOOD_DATA; | |
880 | } | |
881 | ||
25bded7c SF |
882 | static int alps_command_mode_send_nibble(struct psmouse *psmouse, int nibble) |
883 | { | |
884 | struct ps2dev *ps2dev = &psmouse->ps2dev; | |
885 | struct alps_data *priv = psmouse->private; | |
886 | int command; | |
887 | unsigned char *param; | |
888 | unsigned char dummy[4]; | |
889 | ||
890 | BUG_ON(nibble > 0xf); | |
891 | ||
892 | command = priv->nibble_commands[nibble].command; | |
893 | param = (command & 0x0f00) ? | |
894 | dummy : (unsigned char *)&priv->nibble_commands[nibble].data; | |
895 | ||
896 | if (ps2_command(ps2dev, param, command)) | |
897 | return -1; | |
898 | ||
899 | return 0; | |
900 | } | |
901 | ||
902 | static int alps_command_mode_set_addr(struct psmouse *psmouse, int addr) | |
903 | { | |
904 | struct ps2dev *ps2dev = &psmouse->ps2dev; | |
905 | struct alps_data *priv = psmouse->private; | |
906 | int i, nibble; | |
907 | ||
908 | if (ps2_command(ps2dev, NULL, priv->addr_command)) | |
909 | return -1; | |
910 | ||
911 | for (i = 12; i >= 0; i -= 4) { | |
912 | nibble = (addr >> i) & 0xf; | |
913 | if (alps_command_mode_send_nibble(psmouse, nibble)) | |
914 | return -1; | |
915 | } | |
916 | ||
917 | return 0; | |
918 | } | |
919 | ||
920 | static int __alps_command_mode_read_reg(struct psmouse *psmouse, int addr) | |
921 | { | |
922 | struct ps2dev *ps2dev = &psmouse->ps2dev; | |
923 | unsigned char param[4]; | |
924 | ||
925 | if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO)) | |
926 | return -1; | |
927 | ||
928 | /* | |
929 | * The address being read is returned in the first two bytes | |
930 | * of the result. Check that this address matches the expected | |
931 | * address. | |
932 | */ | |
933 | if (addr != ((param[0] << 8) | param[1])) | |
934 | return -1; | |
935 | ||
936 | return param[2]; | |
937 | } | |
938 | ||
939 | static int alps_command_mode_read_reg(struct psmouse *psmouse, int addr) | |
940 | { | |
941 | if (alps_command_mode_set_addr(psmouse, addr)) | |
942 | return -1; | |
943 | return __alps_command_mode_read_reg(psmouse, addr); | |
944 | } | |
945 | ||
946 | static int __alps_command_mode_write_reg(struct psmouse *psmouse, u8 value) | |
947 | { | |
948 | if (alps_command_mode_send_nibble(psmouse, (value >> 4) & 0xf)) | |
949 | return -1; | |
950 | if (alps_command_mode_send_nibble(psmouse, value & 0xf)) | |
951 | return -1; | |
952 | return 0; | |
953 | } | |
954 | ||
955 | static int alps_command_mode_write_reg(struct psmouse *psmouse, int addr, | |
956 | u8 value) | |
957 | { | |
958 | if (alps_command_mode_set_addr(psmouse, addr)) | |
959 | return -1; | |
960 | return __alps_command_mode_write_reg(psmouse, value); | |
961 | } | |
962 | ||
24ba9707 KC |
963 | static int alps_rpt_cmd(struct psmouse *psmouse, int init_command, |
964 | int repeated_command, unsigned char *param) | |
965 | { | |
966 | struct ps2dev *ps2dev = &psmouse->ps2dev; | |
967 | ||
968 | param[0] = 0; | |
969 | if (init_command && ps2_command(ps2dev, param, init_command)) | |
970 | return -EIO; | |
971 | ||
972 | if (ps2_command(ps2dev, NULL, repeated_command) || | |
973 | ps2_command(ps2dev, NULL, repeated_command) || | |
974 | ps2_command(ps2dev, NULL, repeated_command)) | |
975 | return -EIO; | |
976 | ||
977 | param[0] = param[1] = param[2] = 0xff; | |
978 | if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO)) | |
979 | return -EIO; | |
980 | ||
981 | psmouse_dbg(psmouse, "%2.2X report: %2.2x %2.2x %2.2x\n", | |
982 | repeated_command, param[0], param[1], param[2]); | |
983 | return 0; | |
984 | } | |
985 | ||
25bded7c SF |
986 | static int alps_enter_command_mode(struct psmouse *psmouse, |
987 | unsigned char *resp) | |
988 | { | |
989 | unsigned char param[4]; | |
25bded7c | 990 | |
24ba9707 | 991 | if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_RESET_WRAP, param)) { |
25bded7c SF |
992 | psmouse_err(psmouse, "failed to enter command mode\n"); |
993 | return -1; | |
994 | } | |
995 | ||
56fd340e | 996 | if (param[0] != 0x88 || (param[1] != 0x07 && param[1] != 0x08)) { |
25bded7c | 997 | psmouse_dbg(psmouse, |
24ba9707 | 998 | "unknown response while entering command mode\n"); |
25bded7c SF |
999 | return -1; |
1000 | } | |
1001 | ||
1002 | if (resp) | |
1003 | *resp = param[2]; | |
1004 | return 0; | |
1005 | } | |
1006 | ||
1007 | static inline int alps_exit_command_mode(struct psmouse *psmouse) | |
1008 | { | |
1009 | struct ps2dev *ps2dev = &psmouse->ps2dev; | |
1010 | if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)) | |
1011 | return -1; | |
1012 | return 0; | |
1013 | } | |
1014 | ||
1da177e4 LT |
1015 | /* |
1016 | * For DualPoint devices select the device that should respond to | |
1017 | * subsequent commands. It looks like glidepad is behind stickpointer, | |
1018 | * I'd thought it would be other way around... | |
1019 | */ | |
25bded7c | 1020 | static int alps_passthrough_mode_v2(struct psmouse *psmouse, bool enable) |
1da177e4 LT |
1021 | { |
1022 | struct ps2dev *ps2dev = &psmouse->ps2dev; | |
1da177e4 LT |
1023 | int cmd = enable ? PSMOUSE_CMD_SETSCALE21 : PSMOUSE_CMD_SETSCALE11; |
1024 | ||
1025 | if (ps2_command(ps2dev, NULL, cmd) || | |
1026 | ps2_command(ps2dev, NULL, cmd) || | |
1027 | ps2_command(ps2dev, NULL, cmd) || | |
1028 | ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE)) | |
1029 | return -1; | |
1030 | ||
1031 | /* we may get 3 more bytes, just ignore them */ | |
c611763d | 1032 | ps2_drain(ps2dev, 3, 100); |
1da177e4 LT |
1033 | |
1034 | return 0; | |
1035 | } | |
1036 | ||
25bded7c | 1037 | static int alps_absolute_mode_v1_v2(struct psmouse *psmouse) |
1da177e4 LT |
1038 | { |
1039 | struct ps2dev *ps2dev = &psmouse->ps2dev; | |
1040 | ||
1041 | /* Try ALPS magic knock - 4 disable before enable */ | |
1042 | if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) || | |
1043 | ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) || | |
1044 | ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) || | |
1045 | ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) || | |
1046 | ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) | |
1047 | return -1; | |
1048 | ||
1049 | /* | |
1050 | * Switch mouse to poll (remote) mode so motion data will not | |
1051 | * get in our way | |
1052 | */ | |
1053 | return ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETPOLL); | |
1054 | } | |
1055 | ||
1056 | static int alps_get_status(struct psmouse *psmouse, char *param) | |
1057 | { | |
1da177e4 | 1058 | /* Get status: 0xF5 0xF5 0xF5 0xE9 */ |
24ba9707 | 1059 | if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_DISABLE, param)) |
1da177e4 LT |
1060 | return -1; |
1061 | ||
1da177e4 LT |
1062 | return 0; |
1063 | } | |
1064 | ||
1065 | /* | |
1066 | * Turn touchpad tapping on or off. The sequences are: | |
1067 | * 0xE9 0xF5 0xF5 0xF3 0x0A to enable, | |
1068 | * 0xE9 0xF5 0xF5 0xE8 0x00 to disable. | |
1069 | * My guess that 0xE9 (GetInfo) is here as a sync point. | |
1070 | * For models that also have stickpointer (DualPoints) its tapping | |
1071 | * is controlled separately (0xE6 0xE6 0xE6 0xF3 0x14|0x0A) but | |
1072 | * we don't fiddle with it. | |
1073 | */ | |
1074 | static int alps_tap_mode(struct psmouse *psmouse, int enable) | |
1075 | { | |
1076 | struct ps2dev *ps2dev = &psmouse->ps2dev; | |
1077 | int cmd = enable ? PSMOUSE_CMD_SETRATE : PSMOUSE_CMD_SETRES; | |
1078 | unsigned char tap_arg = enable ? 0x0A : 0x00; | |
1079 | unsigned char param[4]; | |
1080 | ||
1081 | if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO) || | |
1082 | ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) || | |
1083 | ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) || | |
1084 | ps2_command(ps2dev, &tap_arg, cmd)) | |
1085 | return -1; | |
1086 | ||
1087 | if (alps_get_status(psmouse, param)) | |
1088 | return -1; | |
1089 | ||
1090 | return 0; | |
1091 | } | |
1092 | ||
f0d5c6f4 DT |
1093 | /* |
1094 | * alps_poll() - poll the touchpad for current motion packet. | |
1095 | * Used in resync. | |
1096 | */ | |
1097 | static int alps_poll(struct psmouse *psmouse) | |
1098 | { | |
1099 | struct alps_data *priv = psmouse->private; | |
b46615fe | 1100 | unsigned char buf[sizeof(psmouse->packet)]; |
b7802c5c | 1101 | bool poll_failed; |
f0d5c6f4 | 1102 | |
99df65e7 | 1103 | if (priv->flags & ALPS_PASS) |
25bded7c | 1104 | alps_passthrough_mode_v2(psmouse, true); |
f0d5c6f4 DT |
1105 | |
1106 | poll_failed = ps2_command(&psmouse->ps2dev, buf, | |
1107 | PSMOUSE_CMD_POLL | (psmouse->pktsize << 8)) < 0; | |
1108 | ||
99df65e7 | 1109 | if (priv->flags & ALPS_PASS) |
25bded7c | 1110 | alps_passthrough_mode_v2(psmouse, false); |
f0d5c6f4 | 1111 | |
99df65e7 | 1112 | if (poll_failed || (buf[0] & priv->mask0) != priv->byte0) |
f0d5c6f4 DT |
1113 | return -1; |
1114 | ||
1115 | if ((psmouse->badbyte & 0xc8) == 0x08) { | |
1116 | /* | |
1117 | * Poll the track stick ... | |
1118 | */ | |
1119 | if (ps2_command(&psmouse->ps2dev, buf, PSMOUSE_CMD_POLL | (3 << 8))) | |
1120 | return -1; | |
1121 | } | |
1122 | ||
1123 | memcpy(psmouse->packet, buf, sizeof(buf)); | |
1124 | return 0; | |
1125 | } | |
1126 | ||
25bded7c | 1127 | static int alps_hw_init_v1_v2(struct psmouse *psmouse) |
1da177e4 LT |
1128 | { |
1129 | struct alps_data *priv = psmouse->private; | |
f3a5c73d | 1130 | |
99df65e7 | 1131 | if ((priv->flags & ALPS_PASS) && |
25bded7c | 1132 | alps_passthrough_mode_v2(psmouse, true)) { |
1da177e4 | 1133 | return -1; |
b7802c5c | 1134 | } |
1da177e4 | 1135 | |
b7802c5c | 1136 | if (alps_tap_mode(psmouse, true)) { |
b5d21704 | 1137 | psmouse_warn(psmouse, "Failed to enable hardware tapping\n"); |
1da177e4 | 1138 | return -1; |
963f626d | 1139 | } |
1da177e4 | 1140 | |
25bded7c | 1141 | if (alps_absolute_mode_v1_v2(psmouse)) { |
b5d21704 | 1142 | psmouse_err(psmouse, "Failed to enable absolute mode\n"); |
1da177e4 LT |
1143 | return -1; |
1144 | } | |
1145 | ||
99df65e7 | 1146 | if ((priv->flags & ALPS_PASS) && |
25bded7c | 1147 | alps_passthrough_mode_v2(psmouse, false)) { |
1da177e4 | 1148 | return -1; |
b7802c5c | 1149 | } |
1da177e4 | 1150 | |
1e0c5b12 DT |
1151 | /* ALPS needs stream mode, otherwise it won't report any data */ |
1152 | if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)) { | |
b5d21704 | 1153 | psmouse_err(psmouse, "Failed to enable stream mode\n"); |
1e0c5b12 DT |
1154 | return -1; |
1155 | } | |
1156 | ||
1157 | return 0; | |
1158 | } | |
1159 | ||
25bded7c SF |
1160 | /* |
1161 | * Enable or disable passthrough mode to the trackstick. Must be in | |
1162 | * command mode when calling this function. | |
1163 | */ | |
1164 | static int alps_passthrough_mode_v3(struct psmouse *psmouse, bool enable) | |
1165 | { | |
1166 | int reg_val; | |
1167 | ||
1168 | reg_val = alps_command_mode_read_reg(psmouse, 0x0008); | |
1169 | if (reg_val == -1) | |
1170 | return -1; | |
1171 | ||
1172 | if (enable) | |
1173 | reg_val |= 0x01; | |
1174 | else | |
1175 | reg_val &= ~0x01; | |
1176 | ||
1177 | if (__alps_command_mode_write_reg(psmouse, reg_val)) | |
1178 | return -1; | |
1179 | ||
1180 | return 0; | |
1181 | } | |
1182 | ||
1183 | /* Must be in command mode when calling this function */ | |
1184 | static int alps_absolute_mode_v3(struct psmouse *psmouse) | |
1185 | { | |
1186 | int reg_val; | |
1187 | ||
1188 | reg_val = alps_command_mode_read_reg(psmouse, 0x0004); | |
1189 | if (reg_val == -1) | |
1190 | return -1; | |
1191 | ||
1192 | reg_val |= 0x06; | |
1193 | if (__alps_command_mode_write_reg(psmouse, reg_val)) | |
1194 | return -1; | |
1195 | ||
1196 | return 0; | |
1197 | } | |
1198 | ||
1199 | static int alps_hw_init_v3(struct psmouse *psmouse) | |
1200 | { | |
25bded7c SF |
1201 | struct ps2dev *ps2dev = &psmouse->ps2dev; |
1202 | int reg_val; | |
1203 | unsigned char param[4]; | |
1204 | ||
25bded7c SF |
1205 | if (alps_enter_command_mode(psmouse, NULL)) |
1206 | goto error; | |
1207 | ||
1208 | /* Check for trackstick */ | |
1209 | reg_val = alps_command_mode_read_reg(psmouse, 0x0008); | |
1210 | if (reg_val == -1) | |
1211 | goto error; | |
1212 | if (reg_val & 0x80) { | |
1213 | if (alps_passthrough_mode_v3(psmouse, true)) | |
1214 | goto error; | |
1215 | if (alps_exit_command_mode(psmouse)) | |
1216 | goto error; | |
1217 | ||
1218 | /* | |
1219 | * E7 report for the trackstick | |
1220 | * | |
1221 | * There have been reports of failures to seem to trace back | |
1222 | * to the above trackstick check failing. When these occur | |
1223 | * this E7 report fails, so when that happens we continue | |
1224 | * with the assumption that there isn't a trackstick after | |
1225 | * all. | |
1226 | */ | |
1227 | param[0] = 0x64; | |
1228 | if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) || | |
1229 | ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) || | |
1230 | ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) || | |
1231 | ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO)) { | |
1232 | psmouse_warn(psmouse, "trackstick E7 report failed\n"); | |
1233 | } else { | |
1234 | psmouse_dbg(psmouse, | |
1235 | "trackstick E7 report: %2.2x %2.2x %2.2x\n", | |
1236 | param[0], param[1], param[2]); | |
1237 | ||
1238 | /* | |
1239 | * Not sure what this does, but it is absolutely | |
1240 | * essential. Without it, the touchpad does not | |
1241 | * work at all and the trackstick just emits normal | |
1242 | * PS/2 packets. | |
1243 | */ | |
1244 | if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) || | |
1245 | ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) || | |
1246 | ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) || | |
1247 | alps_command_mode_send_nibble(psmouse, 0x9) || | |
1248 | alps_command_mode_send_nibble(psmouse, 0x4)) { | |
1249 | psmouse_err(psmouse, | |
1250 | "Error sending magic E6 sequence\n"); | |
1251 | goto error_passthrough; | |
1252 | } | |
1253 | } | |
1254 | ||
1255 | if (alps_enter_command_mode(psmouse, NULL)) | |
1256 | goto error_passthrough; | |
1257 | if (alps_passthrough_mode_v3(psmouse, false)) | |
1258 | goto error; | |
1259 | } | |
1260 | ||
1261 | if (alps_absolute_mode_v3(psmouse)) { | |
1262 | psmouse_err(psmouse, "Failed to enter absolute mode\n"); | |
1263 | goto error; | |
1264 | } | |
1265 | ||
1266 | reg_val = alps_command_mode_read_reg(psmouse, 0x0006); | |
1267 | if (reg_val == -1) | |
1268 | goto error; | |
1269 | if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01)) | |
1270 | goto error; | |
1271 | ||
1272 | reg_val = alps_command_mode_read_reg(psmouse, 0x0007); | |
1273 | if (reg_val == -1) | |
1274 | goto error; | |
1275 | if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01)) | |
1276 | goto error; | |
1277 | ||
1278 | if (alps_command_mode_read_reg(psmouse, 0x0144) == -1) | |
1279 | goto error; | |
1280 | if (__alps_command_mode_write_reg(psmouse, 0x04)) | |
1281 | goto error; | |
1282 | ||
1283 | if (alps_command_mode_read_reg(psmouse, 0x0159) == -1) | |
1284 | goto error; | |
1285 | if (__alps_command_mode_write_reg(psmouse, 0x03)) | |
1286 | goto error; | |
1287 | ||
1288 | if (alps_command_mode_read_reg(psmouse, 0x0163) == -1) | |
1289 | goto error; | |
1290 | if (alps_command_mode_write_reg(psmouse, 0x0163, 0x03)) | |
1291 | goto error; | |
1292 | ||
1293 | if (alps_command_mode_read_reg(psmouse, 0x0162) == -1) | |
1294 | goto error; | |
1295 | if (alps_command_mode_write_reg(psmouse, 0x0162, 0x04)) | |
1296 | goto error; | |
1297 | ||
1298 | /* | |
1299 | * This ensures the trackstick packets are in the format | |
1300 | * supported by this driver. If bit 1 isn't set the packet | |
1301 | * format is different. | |
1302 | */ | |
1303 | if (alps_command_mode_write_reg(psmouse, 0x0008, 0x82)) | |
1304 | goto error; | |
1305 | ||
1306 | alps_exit_command_mode(psmouse); | |
1307 | ||
1308 | /* Set rate and enable data reporting */ | |
1309 | param[0] = 0x64; | |
1310 | if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) || | |
1311 | ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) { | |
1312 | psmouse_err(psmouse, "Failed to enable data reporting\n"); | |
1313 | return -1; | |
1314 | } | |
1315 | ||
1316 | return 0; | |
1317 | ||
1318 | error_passthrough: | |
1319 | /* Something failed while in passthrough mode, so try to get out */ | |
1320 | if (!alps_enter_command_mode(psmouse, NULL)) | |
1321 | alps_passthrough_mode_v3(psmouse, false); | |
1322 | error: | |
1323 | /* | |
1324 | * Leaving the touchpad in command mode will essentially render | |
1325 | * it unusable until the machine reboots, so exit it here just | |
1326 | * to be safe | |
1327 | */ | |
1328 | alps_exit_command_mode(psmouse); | |
1329 | return -1; | |
1330 | } | |
1331 | ||
1332 | /* Must be in command mode when calling this function */ | |
1333 | static int alps_absolute_mode_v4(struct psmouse *psmouse) | |
1334 | { | |
1335 | int reg_val; | |
1336 | ||
1337 | reg_val = alps_command_mode_read_reg(psmouse, 0x0004); | |
1338 | if (reg_val == -1) | |
1339 | return -1; | |
1340 | ||
1341 | reg_val |= 0x02; | |
1342 | if (__alps_command_mode_write_reg(psmouse, reg_val)) | |
1343 | return -1; | |
1344 | ||
1345 | return 0; | |
1346 | } | |
1347 | ||
1348 | static int alps_hw_init_v4(struct psmouse *psmouse) | |
1349 | { | |
25bded7c SF |
1350 | struct ps2dev *ps2dev = &psmouse->ps2dev; |
1351 | unsigned char param[4]; | |
1352 | ||
25bded7c SF |
1353 | if (alps_enter_command_mode(psmouse, NULL)) |
1354 | goto error; | |
1355 | ||
1356 | if (alps_absolute_mode_v4(psmouse)) { | |
1357 | psmouse_err(psmouse, "Failed to enter absolute mode\n"); | |
1358 | goto error; | |
1359 | } | |
1360 | ||
1361 | if (alps_command_mode_write_reg(psmouse, 0x0007, 0x8c)) | |
1362 | goto error; | |
1363 | ||
1364 | if (alps_command_mode_write_reg(psmouse, 0x0149, 0x03)) | |
1365 | goto error; | |
1366 | ||
1367 | if (alps_command_mode_write_reg(psmouse, 0x0160, 0x03)) | |
1368 | goto error; | |
1369 | ||
1370 | if (alps_command_mode_write_reg(psmouse, 0x017f, 0x15)) | |
1371 | goto error; | |
1372 | ||
1373 | if (alps_command_mode_write_reg(psmouse, 0x0151, 0x01)) | |
1374 | goto error; | |
1375 | ||
1376 | if (alps_command_mode_write_reg(psmouse, 0x0168, 0x03)) | |
1377 | goto error; | |
1378 | ||
1379 | if (alps_command_mode_write_reg(psmouse, 0x014a, 0x03)) | |
1380 | goto error; | |
1381 | ||
1382 | if (alps_command_mode_write_reg(psmouse, 0x0161, 0x03)) | |
1383 | goto error; | |
1384 | ||
1385 | alps_exit_command_mode(psmouse); | |
1386 | ||
1387 | /* | |
1388 | * This sequence changes the output from a 9-byte to an | |
1389 | * 8-byte format. All the same data seems to be present, | |
1390 | * just in a more compact format. | |
1391 | */ | |
1392 | param[0] = 0xc8; | |
1393 | param[1] = 0x64; | |
1394 | param[2] = 0x50; | |
1395 | if (ps2_command(ps2dev, ¶m[0], PSMOUSE_CMD_SETRATE) || | |
1396 | ps2_command(ps2dev, ¶m[1], PSMOUSE_CMD_SETRATE) || | |
1397 | ps2_command(ps2dev, ¶m[2], PSMOUSE_CMD_SETRATE) || | |
1398 | ps2_command(ps2dev, param, PSMOUSE_CMD_GETID)) | |
1399 | return -1; | |
1400 | ||
1401 | /* Set rate and enable data reporting */ | |
1402 | param[0] = 0x64; | |
1403 | if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) || | |
1404 | ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) { | |
1405 | psmouse_err(psmouse, "Failed to enable data reporting\n"); | |
1406 | return -1; | |
1407 | } | |
1408 | ||
1409 | return 0; | |
1410 | ||
1411 | error: | |
1412 | /* | |
1413 | * Leaving the touchpad in command mode will essentially render | |
1414 | * it unusable until the machine reboots, so exit it here just | |
1415 | * to be safe | |
1416 | */ | |
1417 | alps_exit_command_mode(psmouse); | |
1418 | return -1; | |
1419 | } | |
1420 | ||
24af5cb9 | 1421 | static void alps_set_defaults(struct alps_data *priv) |
25bded7c | 1422 | { |
f673ceb1 KC |
1423 | priv->byte0 = 0x8f; |
1424 | priv->mask0 = 0x8f; | |
1425 | priv->flags = ALPS_DUALPOINT; | |
1426 | ||
7a9f73e7 KC |
1427 | priv->x_max = 2000; |
1428 | priv->y_max = 1400; | |
1429 | priv->x_bits = 15; | |
1430 | priv->y_bits = 11; | |
1431 | ||
99df65e7 | 1432 | switch (priv->proto_version) { |
25bded7c SF |
1433 | case ALPS_PROTO_V1: |
1434 | case ALPS_PROTO_V2: | |
24af5cb9 KC |
1435 | priv->hw_init = alps_hw_init_v1_v2; |
1436 | priv->process_packet = alps_process_packet_v1_v2; | |
1437 | priv->set_abs_params = alps_set_abs_params_st; | |
25bded7c SF |
1438 | break; |
1439 | case ALPS_PROTO_V3: | |
24af5cb9 KC |
1440 | priv->hw_init = alps_hw_init_v3; |
1441 | priv->process_packet = alps_process_packet_v3; | |
1442 | priv->set_abs_params = alps_set_abs_params_mt; | |
f85e5001 | 1443 | priv->decode_fields = alps_decode_pinnacle; |
50e8b216 KC |
1444 | priv->nibble_commands = alps_v3_nibble_commands; |
1445 | priv->addr_command = PSMOUSE_CMD_RESET_WRAP; | |
25bded7c SF |
1446 | break; |
1447 | case ALPS_PROTO_V4: | |
24af5cb9 KC |
1448 | priv->hw_init = alps_hw_init_v4; |
1449 | priv->process_packet = alps_process_packet_v4; | |
1450 | priv->set_abs_params = alps_set_abs_params_mt; | |
50e8b216 KC |
1451 | priv->nibble_commands = alps_v4_nibble_commands; |
1452 | priv->addr_command = PSMOUSE_CMD_DISABLE; | |
25bded7c SF |
1453 | break; |
1454 | } | |
25bded7c SF |
1455 | } |
1456 | ||
b5d6b851 KC |
1457 | static int alps_match_table(struct psmouse *psmouse, struct alps_data *priv, |
1458 | unsigned char *e7, unsigned char *ec) | |
2e992cc0 | 1459 | { |
b5d6b851 | 1460 | const struct alps_model_info *model; |
2e992cc0 KC |
1461 | int i; |
1462 | ||
b5d6b851 KC |
1463 | for (i = 0; i < ARRAY_SIZE(alps_model_data); i++) { |
1464 | model = &alps_model_data[i]; | |
1465 | ||
1466 | if (!memcmp(e7, model->signature, sizeof(model->signature)) && | |
1467 | (!model->command_mode_resp || | |
1468 | model->command_mode_resp == ec[2])) { | |
1469 | ||
1470 | priv->proto_version = model->proto_version; | |
24af5cb9 KC |
1471 | alps_set_defaults(priv); |
1472 | ||
b5d6b851 KC |
1473 | priv->flags = model->flags; |
1474 | priv->byte0 = model->byte0; | |
1475 | priv->mask0 = model->mask0; | |
1476 | ||
1477 | return 0; | |
1478 | } | |
1479 | } | |
1480 | ||
1481 | return -EINVAL; | |
1482 | } | |
1483 | ||
1484 | static int alps_identify(struct psmouse *psmouse, struct alps_data *priv) | |
1485 | { | |
1486 | unsigned char e6[4], e7[4], ec[4]; | |
1487 | ||
2e992cc0 KC |
1488 | /* |
1489 | * First try "E6 report". | |
1490 | * ALPS should return 0,0,10 or 0,0,100 if no buttons are pressed. | |
1491 | * The bits 0-2 of the first byte will be 1s if some buttons are | |
1492 | * pressed. | |
1493 | */ | |
b5d6b851 KC |
1494 | if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES, |
1495 | PSMOUSE_CMD_SETSCALE11, e6)) | |
1496 | return -EIO; | |
2e992cc0 | 1497 | |
b5d6b851 KC |
1498 | if ((e6[0] & 0xf8) != 0 || e6[1] != 0 || (e6[2] != 10 && e6[2] != 100)) |
1499 | return -EINVAL; | |
2e992cc0 KC |
1500 | |
1501 | /* | |
b5d6b851 KC |
1502 | * Now get the "E7" and "EC" reports. These will uniquely identify |
1503 | * most ALPS touchpads. | |
2e992cc0 | 1504 | */ |
b5d6b851 KC |
1505 | if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES, |
1506 | PSMOUSE_CMD_SETSCALE21, e7) || | |
1507 | alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES, | |
1508 | PSMOUSE_CMD_RESET_WRAP, ec) || | |
1509 | alps_exit_command_mode(psmouse)) | |
1510 | return -EIO; | |
2e992cc0 | 1511 | |
f673ceb1 KC |
1512 | if (alps_match_table(psmouse, priv, e7, ec) == 0) { |
1513 | return 0; | |
1514 | } else if (ec[0] == 0x88 && ec[1] == 0x07 && | |
1515 | ec[2] >= 0x90 && ec[2] <= 0x9d) { | |
1516 | priv->proto_version = ALPS_PROTO_V3; | |
1517 | alps_set_defaults(priv); | |
1518 | ||
b5d6b851 | 1519 | return 0; |
f673ceb1 | 1520 | } |
2e992cc0 | 1521 | |
b5d6b851 KC |
1522 | psmouse_info(psmouse, |
1523 | "Unknown ALPS touchpad: E7=%2.2x %2.2x %2.2x, EC=%2.2x %2.2x %2.2x\n", | |
1524 | e7[0], e7[1], e7[2], ec[0], ec[1], ec[2]); | |
2e992cc0 | 1525 | |
b5d6b851 | 1526 | return -EINVAL; |
2e992cc0 KC |
1527 | } |
1528 | ||
1e0c5b12 DT |
1529 | static int alps_reconnect(struct psmouse *psmouse) |
1530 | { | |
b5d6b851 | 1531 | struct alps_data *priv = psmouse->private; |
71bb21b6 | 1532 | |
1e0c5b12 DT |
1533 | psmouse_reset(psmouse); |
1534 | ||
b5d6b851 | 1535 | if (alps_identify(psmouse, priv) < 0) |
1e0c5b12 DT |
1536 | return -1; |
1537 | ||
24af5cb9 | 1538 | return priv->hw_init(psmouse); |
1da177e4 LT |
1539 | } |
1540 | ||
1541 | static void alps_disconnect(struct psmouse *psmouse) | |
1542 | { | |
1543 | struct alps_data *priv = psmouse->private; | |
2e5b636b | 1544 | |
1da177e4 | 1545 | psmouse_reset(psmouse); |
1d9f2626 | 1546 | del_timer_sync(&priv->timer); |
2e5b636b | 1547 | input_unregister_device(priv->dev2); |
1da177e4 LT |
1548 | kfree(priv); |
1549 | } | |
1550 | ||
24af5cb9 KC |
1551 | static void alps_set_abs_params_st(struct alps_data *priv, |
1552 | struct input_dev *dev1) | |
1553 | { | |
1554 | input_set_abs_params(dev1, ABS_X, 0, 1023, 0, 0); | |
1555 | input_set_abs_params(dev1, ABS_Y, 0, 767, 0, 0); | |
1556 | } | |
1557 | ||
1558 | static void alps_set_abs_params_mt(struct alps_data *priv, | |
1559 | struct input_dev *dev1) | |
1560 | { | |
1561 | set_bit(INPUT_PROP_SEMI_MT, dev1->propbit); | |
1562 | input_mt_init_slots(dev1, 2, 0); | |
7a9f73e7 KC |
1563 | input_set_abs_params(dev1, ABS_MT_POSITION_X, 0, priv->x_max, 0, 0); |
1564 | input_set_abs_params(dev1, ABS_MT_POSITION_Y, 0, priv->y_max, 0, 0); | |
24af5cb9 KC |
1565 | |
1566 | set_bit(BTN_TOOL_DOUBLETAP, dev1->keybit); | |
1567 | set_bit(BTN_TOOL_TRIPLETAP, dev1->keybit); | |
1568 | set_bit(BTN_TOOL_QUADTAP, dev1->keybit); | |
1569 | ||
7a9f73e7 KC |
1570 | input_set_abs_params(dev1, ABS_X, 0, priv->x_max, 0, 0); |
1571 | input_set_abs_params(dev1, ABS_Y, 0, priv->y_max, 0, 0); | |
24af5cb9 KC |
1572 | } |
1573 | ||
1da177e4 LT |
1574 | int alps_init(struct psmouse *psmouse) |
1575 | { | |
1576 | struct alps_data *priv; | |
2e5b636b | 1577 | struct input_dev *dev1 = psmouse->dev, *dev2; |
1da177e4 | 1578 | |
f42649e8 | 1579 | priv = kzalloc(sizeof(struct alps_data), GFP_KERNEL); |
2e5b636b DT |
1580 | dev2 = input_allocate_device(); |
1581 | if (!priv || !dev2) | |
1da177e4 | 1582 | goto init_fail; |
2e5b636b DT |
1583 | |
1584 | priv->dev2 = dev2; | |
1d9f2626 SK |
1585 | setup_timer(&priv->timer, alps_flush_packet, (unsigned long)psmouse); |
1586 | ||
1e0c5b12 | 1587 | psmouse->private = priv; |
1da177e4 | 1588 | |
25bded7c SF |
1589 | psmouse_reset(psmouse); |
1590 | ||
b5d6b851 | 1591 | if (alps_identify(psmouse, priv) < 0) |
71bb21b6 ML |
1592 | goto init_fail; |
1593 | ||
24af5cb9 | 1594 | if (priv->hw_init(psmouse)) |
1da177e4 LT |
1595 | goto init_fail; |
1596 | ||
7105d2ea DT |
1597 | /* |
1598 | * Undo part of setup done for us by psmouse core since touchpad | |
1599 | * is not a relative device. | |
1600 | */ | |
1601 | __clear_bit(EV_REL, dev1->evbit); | |
1602 | __clear_bit(REL_X, dev1->relbit); | |
1603 | __clear_bit(REL_Y, dev1->relbit); | |
1604 | ||
1605 | /* | |
1606 | * Now set up our capabilities. | |
1607 | */ | |
7b19ada2 JS |
1608 | dev1->evbit[BIT_WORD(EV_KEY)] |= BIT_MASK(EV_KEY); |
1609 | dev1->keybit[BIT_WORD(BTN_TOUCH)] |= BIT_MASK(BTN_TOUCH); | |
1610 | dev1->keybit[BIT_WORD(BTN_TOOL_FINGER)] |= BIT_MASK(BTN_TOOL_FINGER); | |
71bb21b6 ML |
1611 | dev1->keybit[BIT_WORD(BTN_LEFT)] |= |
1612 | BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT); | |
1da177e4 | 1613 | |
7b19ada2 | 1614 | dev1->evbit[BIT_WORD(EV_ABS)] |= BIT_MASK(EV_ABS); |
25bded7c | 1615 | |
24af5cb9 | 1616 | priv->set_abs_params(priv, dev1); |
2e5b636b | 1617 | input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0); |
1da177e4 | 1618 | |
99df65e7 | 1619 | if (priv->flags & ALPS_WHEEL) { |
7b19ada2 JS |
1620 | dev1->evbit[BIT_WORD(EV_REL)] |= BIT_MASK(EV_REL); |
1621 | dev1->relbit[BIT_WORD(REL_WHEEL)] |= BIT_MASK(REL_WHEEL); | |
1da177e4 LT |
1622 | } |
1623 | ||
99df65e7 | 1624 | if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) { |
7b19ada2 JS |
1625 | dev1->keybit[BIT_WORD(BTN_FORWARD)] |= BIT_MASK(BTN_FORWARD); |
1626 | dev1->keybit[BIT_WORD(BTN_BACK)] |= BIT_MASK(BTN_BACK); | |
1da177e4 LT |
1627 | } |
1628 | ||
99df65e7 | 1629 | if (priv->flags & ALPS_FOUR_BUTTONS) { |
71bb21b6 ML |
1630 | dev1->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_0); |
1631 | dev1->keybit[BIT_WORD(BTN_1)] |= BIT_MASK(BTN_1); | |
1632 | dev1->keybit[BIT_WORD(BTN_2)] |= BIT_MASK(BTN_2); | |
1633 | dev1->keybit[BIT_WORD(BTN_3)] |= BIT_MASK(BTN_3); | |
1634 | } else { | |
1635 | dev1->keybit[BIT_WORD(BTN_MIDDLE)] |= BIT_MASK(BTN_MIDDLE); | |
1636 | } | |
1637 | ||
08ffce45 | 1638 | snprintf(priv->phys, sizeof(priv->phys), "%s/input1", psmouse->ps2dev.serio->phys); |
2e5b636b | 1639 | dev2->phys = priv->phys; |
99df65e7 KC |
1640 | dev2->name = (priv->flags & ALPS_DUALPOINT) ? |
1641 | "DualPoint Stick" : "PS/2 Mouse"; | |
2e5b636b DT |
1642 | dev2->id.bustype = BUS_I8042; |
1643 | dev2->id.vendor = 0x0002; | |
1644 | dev2->id.product = PSMOUSE_ALPS; | |
1645 | dev2->id.version = 0x0000; | |
1db3a345 | 1646 | dev2->dev.parent = &psmouse->ps2dev.serio->dev; |
1da177e4 | 1647 | |
7b19ada2 | 1648 | dev2->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL); |
71bb21b6 ML |
1649 | dev2->relbit[BIT_WORD(REL_X)] = BIT_MASK(REL_X) | BIT_MASK(REL_Y); |
1650 | dev2->keybit[BIT_WORD(BTN_LEFT)] = | |
1651 | BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_MIDDLE) | BIT_MASK(BTN_RIGHT); | |
1da177e4 | 1652 | |
f42649e8 DT |
1653 | if (input_register_device(priv->dev2)) |
1654 | goto init_fail; | |
1da177e4 LT |
1655 | |
1656 | psmouse->protocol_handler = alps_process_byte; | |
f0d5c6f4 | 1657 | psmouse->poll = alps_poll; |
1da177e4 LT |
1658 | psmouse->disconnect = alps_disconnect; |
1659 | psmouse->reconnect = alps_reconnect; | |
99df65e7 | 1660 | psmouse->pktsize = priv->proto_version == ALPS_PROTO_V4 ? 8 : 6; |
1da177e4 | 1661 | |
f0d5c6f4 DT |
1662 | /* We are having trouble resyncing ALPS touchpads so disable it for now */ |
1663 | psmouse->resync_time = 0; | |
1664 | ||
1da177e4 LT |
1665 | return 0; |
1666 | ||
1667 | init_fail: | |
f42649e8 | 1668 | psmouse_reset(psmouse); |
2e5b636b | 1669 | input_free_device(dev2); |
1da177e4 | 1670 | kfree(priv); |
1e0c5b12 | 1671 | psmouse->private = NULL; |
1da177e4 LT |
1672 | return -1; |
1673 | } | |
1674 | ||
b7802c5c | 1675 | int alps_detect(struct psmouse *psmouse, bool set_properties) |
1da177e4 | 1676 | { |
b5d6b851 | 1677 | struct alps_data dummy; |
1da177e4 | 1678 | |
b5d6b851 | 1679 | if (alps_identify(psmouse, &dummy) < 0) |
1da177e4 LT |
1680 | return -1; |
1681 | ||
1682 | if (set_properties) { | |
1683 | psmouse->vendor = "ALPS"; | |
b5d6b851 | 1684 | psmouse->name = dummy.flags & ALPS_DUALPOINT ? |
968ac842 | 1685 | "DualPoint TouchPad" : "GlidePoint"; |
b5d6b851 | 1686 | psmouse->model = dummy.proto_version << 8; |
1da177e4 LT |
1687 | } |
1688 | return 0; | |
1689 | } | |
1690 |