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