blob: 003587c71f43edd8d6ad315d5ffaf514ad3705d7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * ALPS touchpad PS/2 mouse driver
3 *
4 * Copyright (c) 2003 Neil Brown <neilb@cse.unsw.edu.au>
Peter Osterlund963f6262005-07-11 01:08:04 -05005 * Copyright (c) 2003-2005 Peter Osterlund <petero2@telia.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Copyright (c) 2004 Dmitry Torokhov <dtor@mail.ru>
7 * Copyright (c) 2005 Vojtech Pavlik <vojtech@suse.cz>
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08008 * Copyright (c) 2009 Sebastian Kapfer <sebastian_kapfer@gmx.net>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
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
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/input.h>
20#include <linux/serio.h>
21#include <linux/libps2.h>
22
23#include "psmouse.h"
24#include "alps.h"
25
Maxim Levitsky71bb21b2009-11-16 22:12:22 -080026#define ALPS_OLDPROTO 0x01 /* old style input */
27#define ALPS_DUALPOINT 0x02 /* touchpad has trackstick */
28#define ALPS_PASS 0x04 /* device has a pass-through port */
29
30#define ALPS_WHEEL 0x08 /* hardware wheel present */
31#define ALPS_FW_BK_1 0x10 /* front & back buttons present */
32#define ALPS_FW_BK_2 0x20 /* front & back buttons present */
33#define ALPS_FOUR_BUTTONS 0x40 /* 4 direction button present */
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -080034#define ALPS_PS2_INTERLEAVED 0x80 /* 3-byte PS/2 packet interleaved with
35 6-byte ALPS packet */
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Helge Dellere38de672006-09-10 21:54:39 -040037static const struct alps_model_info alps_model_data[] = {
Dmitry Torokhovb75d1722009-04-23 19:25:51 -070038 { { 0x32, 0x02, 0x14 }, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT }, /* Toshiba Salellite Pro M10 */
Dmitry Torokhovaf27a692009-05-08 18:30:33 -070039 { { 0x33, 0x02, 0x0a }, 0x88, 0xf8, ALPS_OLDPROTO }, /* UMAX-530T */
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 { { 0x53, 0x02, 0x0a }, 0xf8, 0xf8, 0 },
41 { { 0x53, 0x02, 0x14 }, 0xf8, 0xf8, 0 },
Dmitry Torokhovaf27a692009-05-08 18:30:33 -070042 { { 0x60, 0x03, 0xc8 }, 0xf8, 0xf8, 0 }, /* HP ze1115 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 { { 0x63, 0x02, 0x0a }, 0xf8, 0xf8, 0 },
44 { { 0x63, 0x02, 0x14 }, 0xf8, 0xf8, 0 },
Dmitry Torokhovaf27a692009-05-08 18:30:33 -070045 { { 0x63, 0x02, 0x28 }, 0xf8, 0xf8, ALPS_FW_BK_2 }, /* Fujitsu Siemens S6010 */
46 { { 0x63, 0x02, 0x3c }, 0x8f, 0x8f, ALPS_WHEEL }, /* Toshiba Satellite S2400-103 */
47 { { 0x63, 0x02, 0x50 }, 0xef, 0xef, ALPS_FW_BK_1 }, /* NEC Versa L320 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 { { 0x63, 0x02, 0x64 }, 0xf8, 0xf8, 0 },
Dmitry Torokhovaf27a692009-05-08 18:30:33 -070049 { { 0x63, 0x03, 0xc8 }, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT }, /* Dell Latitude D800 */
50 { { 0x73, 0x00, 0x0a }, 0xf8, 0xf8, ALPS_DUALPOINT }, /* ThinkPad R61 8918-5QG */
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 { { 0x73, 0x02, 0x0a }, 0xf8, 0xf8, 0 },
Dmitry Torokhovaf27a692009-05-08 18:30:33 -070052 { { 0x73, 0x02, 0x14 }, 0xf8, 0xf8, ALPS_FW_BK_2 }, /* Ahtec Laptop */
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 { { 0x20, 0x02, 0x0e }, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT }, /* XXX */
54 { { 0x22, 0x02, 0x0a }, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT },
55 { { 0x22, 0x02, 0x14 }, 0xff, 0xff, ALPS_PASS | ALPS_DUALPOINT }, /* Dell Latitude D600 */
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -080056 /* Dell Latitude E5500, E6400, E6500, Precision M4400 */
57 { { 0x62, 0x02, 0x14 }, 0xcf, 0xcf,
58 ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED },
Maxim Levitsky71bb21b2009-11-16 22:12:22 -080059 { { 0x73, 0x02, 0x50 }, 0xcf, 0xcf, ALPS_FOUR_BUTTONS }, /* Dell Vostro 1400 */
Thomas Bächlereb8bff82010-03-09 20:38:48 -080060 { { 0x52, 0x01, 0x14 }, 0xff, 0xff,
61 ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED }, /* Toshiba Tecra A11-11L */
Linus Torvalds1da177e2005-04-16 15:20:36 -070062};
63
64/*
65 * XXX - this entry is suspicious. First byte has zero lower nibble,
66 * which is what a normal mouse would report. Also, the value 0x0e
67 * isn't valid per PS/2 spec.
68 */
69
70/*
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -080071 * PS/2 packet format
72 *
73 * byte 0: 0 0 YSGN XSGN 1 M R L
74 * byte 1: X7 X6 X5 X4 X3 X2 X1 X0
75 * byte 2: Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y0
76 *
77 * Note that the device never signals overflow condition.
78 *
79 * ALPS absolute Mode - new format
Dmitry Torokhov968ac842005-05-29 02:28:29 -050080 *
81 * byte 0: 1 ? ? ? 1 ? ? ?
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 * byte 1: 0 x6 x5 x4 x3 x2 x1 x0
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -080083 * byte 2: 0 x10 x9 x8 x7 ? fin ges
Dmitry Torokhov968ac842005-05-29 02:28:29 -050084 * byte 3: 0 y9 y8 y7 1 M R L
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 * byte 4: 0 y6 y5 y4 y3 y2 y1 y0
86 * byte 5: 0 z6 z5 z4 z3 z2 z1 z0
87 *
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -080088 * Dualpoint device -- interleaved packet format
89 *
90 * byte 0: 1 1 0 0 1 1 1 1
91 * byte 1: 0 x6 x5 x4 x3 x2 x1 x0
92 * byte 2: 0 x10 x9 x8 x7 0 fin ges
93 * byte 3: 0 0 YSGN XSGN 1 1 1 1
94 * byte 4: X7 X6 X5 X4 X3 X2 X1 X0
95 * byte 5: Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y0
96 * byte 6: 0 y9 y8 y7 1 m r l
97 * byte 7: 0 y6 y5 y4 y3 y2 y1 y0
98 * byte 8: 0 z6 z5 z4 z3 z2 z1 z0
99 *
100 * CAPITALS = stick, miniscules = touchpad
101 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 * ?'s can have different meanings on different models,
103 * such as wheel rotation, extra buttons, stick buttons
104 * on a dualpoint, etc.
105 */
106
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800107static bool alps_is_valid_first_byte(const struct alps_model_info *model,
108 unsigned char data)
109{
110 return (data & model->mask0) == model->byte0;
111}
112
113static void alps_report_buttons(struct psmouse *psmouse,
114 struct input_dev *dev1, struct input_dev *dev2,
115 int left, int right, int middle)
116{
Martin Buckc91ed052010-03-13 22:23:58 -0800117 struct input_dev *dev;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800118
Martin Buckc91ed052010-03-13 22:23:58 -0800119 /*
120 * If shared button has already been reported on the
121 * other device (dev2) then this event should be also
122 * sent through that device.
123 */
124 dev = test_bit(BTN_LEFT, dev2->key) ? dev2 : dev1;
125 input_report_key(dev, BTN_LEFT, left);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800126
Martin Buckc91ed052010-03-13 22:23:58 -0800127 dev = test_bit(BTN_RIGHT, dev2->key) ? dev2 : dev1;
128 input_report_key(dev, BTN_RIGHT, right);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800129
Martin Buckc91ed052010-03-13 22:23:58 -0800130 dev = test_bit(BTN_MIDDLE, dev2->key) ? dev2 : dev1;
131 input_report_key(dev, BTN_MIDDLE, middle);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800132
Martin Buckc91ed052010-03-13 22:23:58 -0800133 /*
134 * Sync the _other_ device now, we'll do the first
135 * device later once we report the rest of the events.
136 */
137 input_sync(dev2);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800138}
139
David Howells7d12e782006-10-05 14:55:46 +0100140static void alps_process_packet(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
142 struct alps_data *priv = psmouse->private;
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800143 const struct alps_model_info *model = priv->i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 unsigned char *packet = psmouse->packet;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500145 struct input_dev *dev = psmouse->dev;
146 struct input_dev *dev2 = priv->dev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 int x, y, z, ges, fin, left, right, middle;
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500148 int back = 0, forward = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800150 if (model->flags & ALPS_OLDPROTO) {
Yotam Medinid2f40122006-05-29 23:30:36 -0400151 left = packet[2] & 0x10;
152 right = packet[2] & 0x08;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 middle = 0;
154 x = packet[1] | ((packet[0] & 0x07) << 7);
155 y = packet[4] | ((packet[3] & 0x07) << 7);
156 z = packet[5];
157 } else {
158 left = packet[3] & 1;
159 right = packet[3] & 2;
160 middle = packet[3] & 4;
161 x = packet[1] | ((packet[2] & 0x78) << (7 - 3));
162 y = packet[4] | ((packet[3] & 0x70) << (7 - 4));
163 z = packet[5];
164 }
165
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800166 if (model->flags & ALPS_FW_BK_1) {
Laszlo Kajan3c00bb92008-03-18 00:39:55 -0400167 back = packet[0] & 0x10;
168 forward = packet[2] & 4;
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500169 }
170
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800171 if (model->flags & ALPS_FW_BK_2) {
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500172 back = packet[3] & 4;
173 forward = packet[2] & 4;
174 if ((middle = forward && back))
175 forward = back = 0;
176 }
177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 ges = packet[2] & 1;
179 fin = packet[2] & 2;
180
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800181 if ((model->flags & ALPS_DUALPOINT) && z == 127) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 input_report_rel(dev2, REL_X, (x > 383 ? (x - 768) : x));
183 input_report_rel(dev2, REL_Y, -(y > 255 ? (y - 512) : y));
Ulrich Dangeld7ed5d82009-06-11 00:15:09 -0700184
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800185 alps_report_buttons(psmouse, dev2, dev, left, right, middle);
Ulrich Dangeld7ed5d82009-06-11 00:15:09 -0700186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 input_sync(dev2);
188 return;
189 }
190
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800191 alps_report_buttons(psmouse, dev, dev2, left, right, middle);
Ulrich Dangeld7ed5d82009-06-11 00:15:09 -0700192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 /* Convert hardware tap to a reasonable Z value */
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800194 if (ges && !fin)
195 z = 40;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197 /*
198 * A "tap and drag" operation is reported by the hardware as a transition
199 * from (!fin && ges) to (fin && ges). This should be translated to the
200 * sequence Z>0, Z==0, Z>0, so the Z==0 event has to be generated manually.
201 */
202 if (ges && fin && !priv->prev_fin) {
203 input_report_abs(dev, ABS_X, x);
204 input_report_abs(dev, ABS_Y, y);
205 input_report_abs(dev, ABS_PRESSURE, 0);
206 input_report_key(dev, BTN_TOOL_FINGER, 0);
207 input_sync(dev);
208 }
209 priv->prev_fin = fin;
210
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800211 if (z > 30)
212 input_report_key(dev, BTN_TOUCH, 1);
213 if (z < 25)
214 input_report_key(dev, BTN_TOUCH, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
216 if (z > 0) {
217 input_report_abs(dev, ABS_X, x);
218 input_report_abs(dev, ABS_Y, y);
219 }
220
221 input_report_abs(dev, ABS_PRESSURE, z);
222 input_report_key(dev, BTN_TOOL_FINGER, z > 0);
223
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800224 if (model->flags & ALPS_WHEEL)
Vojtech Pavlike6c047b2005-09-04 01:40:43 -0500225 input_report_rel(dev, REL_WHEEL, ((packet[2] << 1) & 0x08) - ((packet[0] >> 4) & 0x07));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800227 if (model->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500228 input_report_key(dev, BTN_FORWARD, forward);
229 input_report_key(dev, BTN_BACK, back);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 }
231
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800232 if (model->flags & ALPS_FOUR_BUTTONS) {
233 input_report_key(dev, BTN_0, packet[2] & 4);
234 input_report_key(dev, BTN_1, packet[0] & 0x10);
235 input_report_key(dev, BTN_2, packet[3] & 4);
236 input_report_key(dev, BTN_3, packet[0] & 0x20);
237 }
238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 input_sync(dev);
240}
241
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800242static void alps_report_bare_ps2_packet(struct psmouse *psmouse,
243 unsigned char packet[],
244 bool report_buttons)
245{
246 struct alps_data *priv = psmouse->private;
247 struct input_dev *dev2 = priv->dev2;
248
249 if (report_buttons)
250 alps_report_buttons(psmouse, dev2, psmouse->dev,
251 packet[0] & 1, packet[0] & 2, packet[0] & 4);
252
253 input_report_rel(dev2, REL_X,
254 packet[1] ? packet[1] - ((packet[0] << 4) & 0x100) : 0);
255 input_report_rel(dev2, REL_Y,
256 packet[2] ? ((packet[0] << 3) & 0x100) - packet[2] : 0);
257
258 input_sync(dev2);
259}
260
261static psmouse_ret_t alps_handle_interleaved_ps2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
263 struct alps_data *priv = psmouse->private;
264
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800265 if (psmouse->pktcnt < 6)
266 return PSMOUSE_GOOD_DATA;
267
268 if (psmouse->pktcnt == 6) {
269 /*
270 * Start a timer to flush the packet if it ends up last
271 * 6-byte packet in the stream. Timer needs to fire
272 * psmouse core times out itself. 20 ms should be enough
273 * to decide if we are getting more data or not.
274 */
275 mod_timer(&priv->timer, jiffies + msecs_to_jiffies(20));
276 return PSMOUSE_GOOD_DATA;
277 }
278
279 del_timer(&priv->timer);
280
281 if (psmouse->packet[6] & 0x80) {
282
283 /*
284 * Highest bit is set - that means we either had
285 * complete ALPS packet and this is start of the
286 * next packet or we got garbage.
287 */
288
289 if (((psmouse->packet[3] |
290 psmouse->packet[4] |
291 psmouse->packet[5]) & 0x80) ||
292 (!alps_is_valid_first_byte(priv->i, psmouse->packet[6]))) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700293 psmouse_dbg(psmouse,
294 "refusing packet %x %x %x %x (suspected interleaved ps/2)\n",
295 psmouse->packet[3], psmouse->packet[4],
296 psmouse->packet[5], psmouse->packet[6]);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800297 return PSMOUSE_BAD_DATA;
298 }
299
300 alps_process_packet(psmouse);
301
302 /* Continue with the next packet */
303 psmouse->packet[0] = psmouse->packet[6];
304 psmouse->pktcnt = 1;
305
306 } else {
307
308 /*
309 * High bit is 0 - that means that we indeed got a PS/2
310 * packet in the middle of ALPS packet.
311 *
312 * There is also possibility that we got 6-byte ALPS
313 * packet followed by 3-byte packet from trackpoint. We
314 * can not distinguish between these 2 scenarios but
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700315 * because the latter is unlikely to happen in course of
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800316 * normal operation (user would need to press all
317 * buttons on the pad and start moving trackpoint
318 * without touching the pad surface) we assume former.
319 * Even if we are wrong the wost thing that would happen
320 * the cursor would jump but we should not get protocol
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700321 * de-synchronization.
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800322 */
323
324 alps_report_bare_ps2_packet(psmouse, &psmouse->packet[3],
325 false);
326
327 /*
328 * Continue with the standard ALPS protocol handling,
329 * but make sure we won't process it as an interleaved
330 * packet again, which may happen if all buttons are
331 * pressed. To avoid this let's reset the 4th bit which
332 * is normally 1.
333 */
334 psmouse->packet[3] = psmouse->packet[6] & 0xf7;
335 psmouse->pktcnt = 4;
336 }
337
338 return PSMOUSE_GOOD_DATA;
339}
340
341static void alps_flush_packet(unsigned long data)
342{
343 struct psmouse *psmouse = (struct psmouse *)data;
344
345 serio_pause_rx(psmouse->ps2dev.serio);
346
347 if (psmouse->pktcnt == 6) {
348
349 /*
350 * We did not any more data in reasonable amount of time.
351 * Validate the last 3 bytes and process as a standard
352 * ALPS packet.
353 */
354 if ((psmouse->packet[3] |
355 psmouse->packet[4] |
356 psmouse->packet[5]) & 0x80) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700357 psmouse_dbg(psmouse,
358 "refusing packet %x %x %x (suspected interleaved ps/2)\n",
359 psmouse->packet[3], psmouse->packet[4],
360 psmouse->packet[5]);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800361 } else {
362 alps_process_packet(psmouse);
363 }
364 psmouse->pktcnt = 0;
365 }
366
367 serio_continue_rx(psmouse->ps2dev.serio);
368}
369
370static psmouse_ret_t alps_process_byte(struct psmouse *psmouse)
371{
372 struct alps_data *priv = psmouse->private;
373 const struct alps_model_info *model = priv->i;
374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 if ((psmouse->packet[0] & 0xc8) == 0x08) { /* PS/2 packet */
376 if (psmouse->pktcnt == 3) {
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800377 alps_report_bare_ps2_packet(psmouse, psmouse->packet,
378 true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 return PSMOUSE_FULL_PACKET;
380 }
381 return PSMOUSE_GOOD_DATA;
382 }
383
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800384 /* Check for PS/2 packet stuffed in the middle of ALPS packet. */
385
386 if ((model->flags & ALPS_PS2_INTERLEAVED) &&
387 psmouse->pktcnt >= 4 && (psmouse->packet[3] & 0x0f) == 0x0f) {
388 return alps_handle_interleaved_ps2(psmouse);
389 }
390
391 if (!alps_is_valid_first_byte(model, psmouse->packet[0])) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700392 psmouse_dbg(psmouse,
393 "refusing packet[0] = %x (mask0 = %x, byte0 = %x)\n",
394 psmouse->packet[0], model->mask0, model->byte0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 return PSMOUSE_BAD_DATA;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
398 /* Bytes 2 - 6 should have 0 in the highest bit */
399 if (psmouse->pktcnt >= 2 && psmouse->pktcnt <= 6 &&
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800400 (psmouse->packet[psmouse->pktcnt - 1] & 0x80)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700401 psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
402 psmouse->pktcnt - 1,
403 psmouse->packet[psmouse->pktcnt - 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 return PSMOUSE_BAD_DATA;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800405 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
407 if (psmouse->pktcnt == 6) {
David Howells7d12e782006-10-05 14:55:46 +0100408 alps_process_packet(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 return PSMOUSE_FULL_PACKET;
410 }
411
412 return PSMOUSE_GOOD_DATA;
413}
414
Helge Dellere38de672006-09-10 21:54:39 -0400415static const struct alps_model_info *alps_get_model(struct psmouse *psmouse, int *version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416{
417 struct ps2dev *ps2dev = &psmouse->ps2dev;
Helge Dellere38de672006-09-10 21:54:39 -0400418 static const unsigned char rates[] = { 0, 10, 20, 40, 60, 80, 100, 200 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 unsigned char param[4];
420 int i;
421
422 /*
423 * First try "E6 report".
424 * ALPS should return 0,0,10 or 0,0,100
425 */
426 param[0] = 0;
427 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES) ||
428 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
429 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
430 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11))
431 return NULL;
432
433 param[0] = param[1] = param[2] = 0xff;
434 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
435 return NULL;
436
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700437 psmouse_dbg(psmouse, "E6 report: %2.2x %2.2x %2.2x",
438 param[0], param[1], param[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
440 if (param[0] != 0 || param[1] != 0 || (param[2] != 10 && param[2] != 100))
441 return NULL;
442
443 /*
444 * Now try "E7 report". Allowed responses are in
445 * alps_model_data[].signature
446 */
447 param[0] = 0;
448 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES) ||
449 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
450 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
451 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21))
452 return NULL;
453
454 param[0] = param[1] = param[2] = 0xff;
455 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
456 return NULL;
457
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700458 psmouse_dbg(psmouse, "E7 report: %2.2x %2.2x %2.2x",
459 param[0], param[1], param[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -0400461 if (version) {
462 for (i = 0; i < ARRAY_SIZE(rates) && param[2] != rates[i]; i++)
463 /* empty */;
464 *version = (param[0] << 8) | (param[1] << 4) | i;
465 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
467 for (i = 0; i < ARRAY_SIZE(alps_model_data); i++)
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -0400468 if (!memcmp(param, alps_model_data[i].signature,
469 sizeof(alps_model_data[i].signature)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 return alps_model_data + i;
471
472 return NULL;
473}
474
475/*
476 * For DualPoint devices select the device that should respond to
477 * subsequent commands. It looks like glidepad is behind stickpointer,
478 * I'd thought it would be other way around...
479 */
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700480static int alps_passthrough_mode(struct psmouse *psmouse, bool enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
482 struct ps2dev *ps2dev = &psmouse->ps2dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 int cmd = enable ? PSMOUSE_CMD_SETSCALE21 : PSMOUSE_CMD_SETSCALE11;
484
485 if (ps2_command(ps2dev, NULL, cmd) ||
486 ps2_command(ps2dev, NULL, cmd) ||
487 ps2_command(ps2dev, NULL, cmd) ||
488 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
489 return -1;
490
491 /* we may get 3 more bytes, just ignore them */
Dmitry Torokhovc6117632005-06-01 02:39:51 -0500492 ps2_drain(ps2dev, 3, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 return 0;
495}
496
497static int alps_absolute_mode(struct psmouse *psmouse)
498{
499 struct ps2dev *ps2dev = &psmouse->ps2dev;
500
501 /* Try ALPS magic knock - 4 disable before enable */
502 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
503 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
504 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
505 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
506 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
507 return -1;
508
509 /*
510 * Switch mouse to poll (remote) mode so motion data will not
511 * get in our way
512 */
513 return ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETPOLL);
514}
515
516static int alps_get_status(struct psmouse *psmouse, char *param)
517{
518 struct ps2dev *ps2dev = &psmouse->ps2dev;
519
520 /* Get status: 0xF5 0xF5 0xF5 0xE9 */
521 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
522 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
523 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
524 ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
525 return -1;
526
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700527 psmouse_dbg(psmouse, "Status: %2.2x %2.2x %2.2x",
528 param[0], param[1], param[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
530 return 0;
531}
532
533/*
534 * Turn touchpad tapping on or off. The sequences are:
535 * 0xE9 0xF5 0xF5 0xF3 0x0A to enable,
536 * 0xE9 0xF5 0xF5 0xE8 0x00 to disable.
537 * My guess that 0xE9 (GetInfo) is here as a sync point.
538 * For models that also have stickpointer (DualPoints) its tapping
539 * is controlled separately (0xE6 0xE6 0xE6 0xF3 0x14|0x0A) but
540 * we don't fiddle with it.
541 */
542static int alps_tap_mode(struct psmouse *psmouse, int enable)
543{
544 struct ps2dev *ps2dev = &psmouse->ps2dev;
545 int cmd = enable ? PSMOUSE_CMD_SETRATE : PSMOUSE_CMD_SETRES;
546 unsigned char tap_arg = enable ? 0x0A : 0x00;
547 unsigned char param[4];
548
549 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO) ||
550 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
551 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
552 ps2_command(ps2dev, &tap_arg, cmd))
553 return -1;
554
555 if (alps_get_status(psmouse, param))
556 return -1;
557
558 return 0;
559}
560
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500561/*
562 * alps_poll() - poll the touchpad for current motion packet.
563 * Used in resync.
564 */
565static int alps_poll(struct psmouse *psmouse)
566{
567 struct alps_data *priv = psmouse->private;
568 unsigned char buf[6];
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700569 bool poll_failed;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500570
571 if (priv->i->flags & ALPS_PASS)
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700572 alps_passthrough_mode(psmouse, true);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500573
574 poll_failed = ps2_command(&psmouse->ps2dev, buf,
575 PSMOUSE_CMD_POLL | (psmouse->pktsize << 8)) < 0;
576
577 if (priv->i->flags & ALPS_PASS)
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700578 alps_passthrough_mode(psmouse, false);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500579
580 if (poll_failed || (buf[0] & priv->i->mask0) != priv->i->byte0)
581 return -1;
582
583 if ((psmouse->badbyte & 0xc8) == 0x08) {
584/*
585 * Poll the track stick ...
586 */
587 if (ps2_command(&psmouse->ps2dev, buf, PSMOUSE_CMD_POLL | (3 << 8)))
588 return -1;
589 }
590
591 memcpy(psmouse->packet, buf, sizeof(buf));
592 return 0;
593}
594
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800595static int alps_hw_init(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596{
597 struct alps_data *priv = psmouse->private;
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800598 const struct alps_model_info *model = priv->i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800600 if ((model->flags & ALPS_PASS) &&
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700601 alps_passthrough_mode(psmouse, true)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 return -1;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700603 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700605 if (alps_tap_mode(psmouse, true)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700606 psmouse_warn(psmouse, "Failed to enable hardware tapping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 return -1;
Peter Osterlund963f6262005-07-11 01:08:04 -0500608 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
610 if (alps_absolute_mode(psmouse)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700611 psmouse_err(psmouse, "Failed to enable absolute mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 return -1;
613 }
614
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800615 if ((model->flags & ALPS_PASS) &&
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700616 alps_passthrough_mode(psmouse, false)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 return -1;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700618 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -0400620 /* ALPS needs stream mode, otherwise it won't report any data */
621 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700622 psmouse_err(psmouse, "Failed to enable stream mode\n");
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -0400623 return -1;
624 }
625
626 return 0;
627}
628
629static int alps_reconnect(struct psmouse *psmouse)
630{
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800631 const struct alps_model_info *model;
632
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -0400633 psmouse_reset(psmouse);
634
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800635 model = alps_get_model(psmouse, NULL);
636 if (!model)
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -0400637 return -1;
638
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800639 return alps_hw_init(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640}
641
642static void alps_disconnect(struct psmouse *psmouse)
643{
644 struct alps_data *priv = psmouse->private;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 psmouse_reset(psmouse);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800647 del_timer_sync(&priv->timer);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500648 input_unregister_device(priv->dev2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 kfree(priv);
650}
651
652int alps_init(struct psmouse *psmouse)
653{
654 struct alps_data *priv;
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800655 const struct alps_model_info *model;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500656 struct input_dev *dev1 = psmouse->dev, *dev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 int version;
658
Dmitry Torokhovf42649e2007-04-12 01:31:13 -0400659 priv = kzalloc(sizeof(struct alps_data), GFP_KERNEL);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500660 dev2 = input_allocate_device();
661 if (!priv || !dev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 goto init_fail;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500663
664 priv->dev2 = dev2;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800665 setup_timer(&priv->timer, alps_flush_packet, (unsigned long)psmouse);
666
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -0400667 psmouse->private = priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800669 model = alps_get_model(psmouse, &version);
670 if (!model)
671 goto init_fail;
672
673 priv->i = model;
674
675 if (alps_hw_init(psmouse))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 goto init_fail;
677
Dmitry Torokhov7105d2e2009-12-11 23:54:54 -0800678 /*
679 * Undo part of setup done for us by psmouse core since touchpad
680 * is not a relative device.
681 */
682 __clear_bit(EV_REL, dev1->evbit);
683 __clear_bit(REL_X, dev1->relbit);
684 __clear_bit(REL_Y, dev1->relbit);
685
686 /*
687 * Now set up our capabilities.
688 */
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700689 dev1->evbit[BIT_WORD(EV_KEY)] |= BIT_MASK(EV_KEY);
690 dev1->keybit[BIT_WORD(BTN_TOUCH)] |= BIT_MASK(BTN_TOUCH);
691 dev1->keybit[BIT_WORD(BTN_TOOL_FINGER)] |= BIT_MASK(BTN_TOOL_FINGER);
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800692 dev1->keybit[BIT_WORD(BTN_LEFT)] |=
693 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700695 dev1->evbit[BIT_WORD(EV_ABS)] |= BIT_MASK(EV_ABS);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500696 input_set_abs_params(dev1, ABS_X, 0, 1023, 0, 0);
697 input_set_abs_params(dev1, ABS_Y, 0, 767, 0, 0);
698 input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800700 if (model->flags & ALPS_WHEEL) {
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700701 dev1->evbit[BIT_WORD(EV_REL)] |= BIT_MASK(EV_REL);
702 dev1->relbit[BIT_WORD(REL_WHEEL)] |= BIT_MASK(REL_WHEEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 }
704
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800705 if (model->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700706 dev1->keybit[BIT_WORD(BTN_FORWARD)] |= BIT_MASK(BTN_FORWARD);
707 dev1->keybit[BIT_WORD(BTN_BACK)] |= BIT_MASK(BTN_BACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 }
709
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800710 if (model->flags & ALPS_FOUR_BUTTONS) {
711 dev1->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_0);
712 dev1->keybit[BIT_WORD(BTN_1)] |= BIT_MASK(BTN_1);
713 dev1->keybit[BIT_WORD(BTN_2)] |= BIT_MASK(BTN_2);
714 dev1->keybit[BIT_WORD(BTN_3)] |= BIT_MASK(BTN_3);
715 } else {
716 dev1->keybit[BIT_WORD(BTN_MIDDLE)] |= BIT_MASK(BTN_MIDDLE);
717 }
718
Dmitry Torokhov08ffce42006-06-26 01:45:10 -0400719 snprintf(priv->phys, sizeof(priv->phys), "%s/input1", psmouse->ps2dev.serio->phys);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500720 dev2->phys = priv->phys;
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800721 dev2->name = (model->flags & ALPS_DUALPOINT) ? "DualPoint Stick" : "PS/2 Mouse";
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500722 dev2->id.bustype = BUS_I8042;
723 dev2->id.vendor = 0x0002;
724 dev2->id.product = PSMOUSE_ALPS;
725 dev2->id.version = 0x0000;
Dmitry Torokhov1db3a342008-03-18 00:29:18 -0400726 dev2->dev.parent = &psmouse->ps2dev.serio->dev;
Dmitry Torokhov968ac842005-05-29 02:28:29 -0500727
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700728 dev2->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800729 dev2->relbit[BIT_WORD(REL_X)] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
730 dev2->keybit[BIT_WORD(BTN_LEFT)] =
731 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_MIDDLE) | BIT_MASK(BTN_RIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Dmitry Torokhovf42649e2007-04-12 01:31:13 -0400733 if (input_register_device(priv->dev2))
734 goto init_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
736 psmouse->protocol_handler = alps_process_byte;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500737 psmouse->poll = alps_poll;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 psmouse->disconnect = alps_disconnect;
739 psmouse->reconnect = alps_reconnect;
740 psmouse->pktsize = 6;
741
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500742 /* We are having trouble resyncing ALPS touchpads so disable it for now */
743 psmouse->resync_time = 0;
744
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 return 0;
746
747init_fail:
Dmitry Torokhovf42649e2007-04-12 01:31:13 -0400748 psmouse_reset(psmouse);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500749 input_free_device(dev2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 kfree(priv);
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -0400751 psmouse->private = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 return -1;
753}
754
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700755int alps_detect(struct psmouse *psmouse, bool set_properties)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756{
757 int version;
Helge Dellere38de672006-09-10 21:54:39 -0400758 const struct alps_model_info *model;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Dmitry Torokhovf42649e2007-04-12 01:31:13 -0400760 model = alps_get_model(psmouse, &version);
761 if (!model)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 return -1;
763
764 if (set_properties) {
765 psmouse->vendor = "ALPS";
Dmitry Torokhov968ac842005-05-29 02:28:29 -0500766 psmouse->name = model->flags & ALPS_DUALPOINT ?
767 "DualPoint TouchPad" : "GlidePoint";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 psmouse->model = version;
769 }
770 return 0;
771}
772