blob: 77b776d993773bc16ccf137026a4c5b3afc20462 [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_DUALPOINT 0x02 /* touchpad has trackstick */
27#define ALPS_PASS 0x04 /* device has a pass-through port */
28
29#define ALPS_WHEEL 0x08 /* hardware wheel present */
30#define ALPS_FW_BK_1 0x10 /* front & back buttons present */
31#define ALPS_FW_BK_2 0x20 /* front & back buttons present */
32#define ALPS_FOUR_BUTTONS 0x40 /* 4 direction button present */
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -080033#define ALPS_PS2_INTERLEAVED 0x80 /* 3-byte PS/2 packet interleaved with
34 6-byte ALPS packet */
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Helge Dellere38de672006-09-10 21:54:39 -040036static const struct alps_model_info alps_model_data[] = {
Seth Forsheefa629ef2011-11-07 19:53:24 -080037 { { 0x32, 0x02, 0x14 }, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT }, /* Toshiba Salellite Pro M10 */
38 { { 0x33, 0x02, 0x0a }, ALPS_PROTO_V1, 0x88, 0xf8, 0 }, /* UMAX-530T */
39 { { 0x53, 0x02, 0x0a }, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
40 { { 0x53, 0x02, 0x14 }, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
41 { { 0x60, 0x03, 0xc8 }, ALPS_PROTO_V2, 0xf8, 0xf8, 0 }, /* HP ze1115 */
42 { { 0x63, 0x02, 0x0a }, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
43 { { 0x63, 0x02, 0x14 }, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
44 { { 0x63, 0x02, 0x28 }, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 }, /* Fujitsu Siemens S6010 */
45 { { 0x63, 0x02, 0x3c }, ALPS_PROTO_V2, 0x8f, 0x8f, ALPS_WHEEL }, /* Toshiba Satellite S2400-103 */
46 { { 0x63, 0x02, 0x50 }, ALPS_PROTO_V2, 0xef, 0xef, ALPS_FW_BK_1 }, /* NEC Versa L320 */
47 { { 0x63, 0x02, 0x64 }, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
48 { { 0x63, 0x03, 0xc8 }, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT }, /* Dell Latitude D800 */
49 { { 0x73, 0x00, 0x0a }, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_DUALPOINT }, /* ThinkPad R61 8918-5QG */
50 { { 0x73, 0x02, 0x0a }, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
51 { { 0x73, 0x02, 0x14 }, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 }, /* Ahtec Laptop */
52 { { 0x20, 0x02, 0x0e }, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT }, /* XXX */
53 { { 0x22, 0x02, 0x0a }, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT },
54 { { 0x22, 0x02, 0x14 }, ALPS_PROTO_V2, 0xff, 0xff, ALPS_PASS | ALPS_DUALPOINT }, /* Dell Latitude D600 */
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -080055 /* Dell Latitude E5500, E6400, E6500, Precision M4400 */
Seth Forsheefa629ef2011-11-07 19:53:24 -080056 { { 0x62, 0x02, 0x14 }, ALPS_PROTO_V2, 0xcf, 0xcf,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -080057 ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED },
Seth Forsheefa629ef2011-11-07 19:53:24 -080058 { { 0x73, 0x02, 0x50 }, ALPS_PROTO_V2, 0xcf, 0xcf, ALPS_FOUR_BUTTONS }, /* Dell Vostro 1400 */
59 { { 0x52, 0x01, 0x14 }, ALPS_PROTO_V2, 0xff, 0xff,
60 ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED }, /* Toshiba Tecra A11-11L */
Linus Torvalds1da177e2005-04-16 15:20:36 -070061};
62
63/*
64 * XXX - this entry is suspicious. First byte has zero lower nibble,
65 * which is what a normal mouse would report. Also, the value 0x0e
66 * isn't valid per PS/2 spec.
67 */
68
Seth Forsheed4b347b2011-11-07 19:53:15 -080069/* Packet formats are described in Documentation/input/alps.txt */
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -080071static bool alps_is_valid_first_byte(const struct alps_model_info *model,
72 unsigned char data)
73{
74 return (data & model->mask0) == model->byte0;
75}
76
77static void alps_report_buttons(struct psmouse *psmouse,
78 struct input_dev *dev1, struct input_dev *dev2,
79 int left, int right, int middle)
80{
Martin Buckc91ed052010-03-13 22:23:58 -080081 struct input_dev *dev;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -080082
Martin Buckc91ed052010-03-13 22:23:58 -080083 /*
84 * If shared button has already been reported on the
85 * other device (dev2) then this event should be also
86 * sent through that device.
87 */
88 dev = test_bit(BTN_LEFT, dev2->key) ? dev2 : dev1;
89 input_report_key(dev, BTN_LEFT, left);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -080090
Martin Buckc91ed052010-03-13 22:23:58 -080091 dev = test_bit(BTN_RIGHT, dev2->key) ? dev2 : dev1;
92 input_report_key(dev, BTN_RIGHT, right);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -080093
Martin Buckc91ed052010-03-13 22:23:58 -080094 dev = test_bit(BTN_MIDDLE, dev2->key) ? dev2 : dev1;
95 input_report_key(dev, BTN_MIDDLE, middle);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -080096
Martin Buckc91ed052010-03-13 22:23:58 -080097 /*
98 * Sync the _other_ device now, we'll do the first
99 * device later once we report the rest of the events.
100 */
101 input_sync(dev2);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800102}
103
David Howells7d12e782006-10-05 14:55:46 +0100104static void alps_process_packet(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
106 struct alps_data *priv = psmouse->private;
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800107 const struct alps_model_info *model = priv->i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 unsigned char *packet = psmouse->packet;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500109 struct input_dev *dev = psmouse->dev;
110 struct input_dev *dev2 = priv->dev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 int x, y, z, ges, fin, left, right, middle;
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500112 int back = 0, forward = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Seth Forsheefa629ef2011-11-07 19:53:24 -0800114 if (model->proto_version == ALPS_PROTO_V1) {
Yotam Medinid2f40122006-05-29 23:30:36 -0400115 left = packet[2] & 0x10;
116 right = packet[2] & 0x08;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 middle = 0;
118 x = packet[1] | ((packet[0] & 0x07) << 7);
119 y = packet[4] | ((packet[3] & 0x07) << 7);
120 z = packet[5];
121 } else {
122 left = packet[3] & 1;
123 right = packet[3] & 2;
124 middle = packet[3] & 4;
125 x = packet[1] | ((packet[2] & 0x78) << (7 - 3));
126 y = packet[4] | ((packet[3] & 0x70) << (7 - 4));
127 z = packet[5];
128 }
129
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800130 if (model->flags & ALPS_FW_BK_1) {
Laszlo Kajan3c00bb92008-03-18 00:39:55 -0400131 back = packet[0] & 0x10;
132 forward = packet[2] & 4;
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500133 }
134
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800135 if (model->flags & ALPS_FW_BK_2) {
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500136 back = packet[3] & 4;
137 forward = packet[2] & 4;
138 if ((middle = forward && back))
139 forward = back = 0;
140 }
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 ges = packet[2] & 1;
143 fin = packet[2] & 2;
144
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800145 if ((model->flags & ALPS_DUALPOINT) && z == 127) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 input_report_rel(dev2, REL_X, (x > 383 ? (x - 768) : x));
147 input_report_rel(dev2, REL_Y, -(y > 255 ? (y - 512) : y));
Ulrich Dangeld7ed5d82009-06-11 00:15:09 -0700148
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800149 alps_report_buttons(psmouse, dev2, dev, left, right, middle);
Ulrich Dangeld7ed5d82009-06-11 00:15:09 -0700150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 input_sync(dev2);
152 return;
153 }
154
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800155 alps_report_buttons(psmouse, dev, dev2, left, right, middle);
Ulrich Dangeld7ed5d82009-06-11 00:15:09 -0700156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 /* Convert hardware tap to a reasonable Z value */
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800158 if (ges && !fin)
159 z = 40;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
161 /*
162 * A "tap and drag" operation is reported by the hardware as a transition
163 * from (!fin && ges) to (fin && ges). This should be translated to the
164 * sequence Z>0, Z==0, Z>0, so the Z==0 event has to be generated manually.
165 */
166 if (ges && fin && !priv->prev_fin) {
167 input_report_abs(dev, ABS_X, x);
168 input_report_abs(dev, ABS_Y, y);
169 input_report_abs(dev, ABS_PRESSURE, 0);
170 input_report_key(dev, BTN_TOOL_FINGER, 0);
171 input_sync(dev);
172 }
173 priv->prev_fin = fin;
174
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800175 if (z > 30)
176 input_report_key(dev, BTN_TOUCH, 1);
177 if (z < 25)
178 input_report_key(dev, BTN_TOUCH, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180 if (z > 0) {
181 input_report_abs(dev, ABS_X, x);
182 input_report_abs(dev, ABS_Y, y);
183 }
184
185 input_report_abs(dev, ABS_PRESSURE, z);
186 input_report_key(dev, BTN_TOOL_FINGER, z > 0);
187
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800188 if (model->flags & ALPS_WHEEL)
Vojtech Pavlike6c047b2005-09-04 01:40:43 -0500189 input_report_rel(dev, REL_WHEEL, ((packet[2] << 1) & 0x08) - ((packet[0] >> 4) & 0x07));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800191 if (model->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500192 input_report_key(dev, BTN_FORWARD, forward);
193 input_report_key(dev, BTN_BACK, back);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 }
195
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800196 if (model->flags & ALPS_FOUR_BUTTONS) {
197 input_report_key(dev, BTN_0, packet[2] & 4);
198 input_report_key(dev, BTN_1, packet[0] & 0x10);
199 input_report_key(dev, BTN_2, packet[3] & 4);
200 input_report_key(dev, BTN_3, packet[0] & 0x20);
201 }
202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 input_sync(dev);
204}
205
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800206static void alps_report_bare_ps2_packet(struct psmouse *psmouse,
207 unsigned char packet[],
208 bool report_buttons)
209{
210 struct alps_data *priv = psmouse->private;
211 struct input_dev *dev2 = priv->dev2;
212
213 if (report_buttons)
214 alps_report_buttons(psmouse, dev2, psmouse->dev,
215 packet[0] & 1, packet[0] & 2, packet[0] & 4);
216
217 input_report_rel(dev2, REL_X,
218 packet[1] ? packet[1] - ((packet[0] << 4) & 0x100) : 0);
219 input_report_rel(dev2, REL_Y,
220 packet[2] ? ((packet[0] << 3) & 0x100) - packet[2] : 0);
221
222 input_sync(dev2);
223}
224
225static psmouse_ret_t alps_handle_interleaved_ps2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
227 struct alps_data *priv = psmouse->private;
228
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800229 if (psmouse->pktcnt < 6)
230 return PSMOUSE_GOOD_DATA;
231
232 if (psmouse->pktcnt == 6) {
233 /*
234 * Start a timer to flush the packet if it ends up last
235 * 6-byte packet in the stream. Timer needs to fire
236 * psmouse core times out itself. 20 ms should be enough
237 * to decide if we are getting more data or not.
238 */
239 mod_timer(&priv->timer, jiffies + msecs_to_jiffies(20));
240 return PSMOUSE_GOOD_DATA;
241 }
242
243 del_timer(&priv->timer);
244
245 if (psmouse->packet[6] & 0x80) {
246
247 /*
248 * Highest bit is set - that means we either had
249 * complete ALPS packet and this is start of the
250 * next packet or we got garbage.
251 */
252
253 if (((psmouse->packet[3] |
254 psmouse->packet[4] |
255 psmouse->packet[5]) & 0x80) ||
256 (!alps_is_valid_first_byte(priv->i, psmouse->packet[6]))) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700257 psmouse_dbg(psmouse,
258 "refusing packet %x %x %x %x (suspected interleaved ps/2)\n",
259 psmouse->packet[3], psmouse->packet[4],
260 psmouse->packet[5], psmouse->packet[6]);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800261 return PSMOUSE_BAD_DATA;
262 }
263
264 alps_process_packet(psmouse);
265
266 /* Continue with the next packet */
267 psmouse->packet[0] = psmouse->packet[6];
268 psmouse->pktcnt = 1;
269
270 } else {
271
272 /*
273 * High bit is 0 - that means that we indeed got a PS/2
274 * packet in the middle of ALPS packet.
275 *
276 * There is also possibility that we got 6-byte ALPS
277 * packet followed by 3-byte packet from trackpoint. We
278 * can not distinguish between these 2 scenarios but
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700279 * because the latter is unlikely to happen in course of
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800280 * normal operation (user would need to press all
281 * buttons on the pad and start moving trackpoint
282 * without touching the pad surface) we assume former.
283 * Even if we are wrong the wost thing that would happen
284 * the cursor would jump but we should not get protocol
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700285 * de-synchronization.
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800286 */
287
288 alps_report_bare_ps2_packet(psmouse, &psmouse->packet[3],
289 false);
290
291 /*
292 * Continue with the standard ALPS protocol handling,
293 * but make sure we won't process it as an interleaved
294 * packet again, which may happen if all buttons are
295 * pressed. To avoid this let's reset the 4th bit which
296 * is normally 1.
297 */
298 psmouse->packet[3] = psmouse->packet[6] & 0xf7;
299 psmouse->pktcnt = 4;
300 }
301
302 return PSMOUSE_GOOD_DATA;
303}
304
305static void alps_flush_packet(unsigned long data)
306{
307 struct psmouse *psmouse = (struct psmouse *)data;
308
309 serio_pause_rx(psmouse->ps2dev.serio);
310
311 if (psmouse->pktcnt == 6) {
312
313 /*
314 * We did not any more data in reasonable amount of time.
315 * Validate the last 3 bytes and process as a standard
316 * ALPS packet.
317 */
318 if ((psmouse->packet[3] |
319 psmouse->packet[4] |
320 psmouse->packet[5]) & 0x80) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700321 psmouse_dbg(psmouse,
322 "refusing packet %x %x %x (suspected interleaved ps/2)\n",
323 psmouse->packet[3], psmouse->packet[4],
324 psmouse->packet[5]);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800325 } else {
326 alps_process_packet(psmouse);
327 }
328 psmouse->pktcnt = 0;
329 }
330
331 serio_continue_rx(psmouse->ps2dev.serio);
332}
333
334static psmouse_ret_t alps_process_byte(struct psmouse *psmouse)
335{
336 struct alps_data *priv = psmouse->private;
337 const struct alps_model_info *model = priv->i;
338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 if ((psmouse->packet[0] & 0xc8) == 0x08) { /* PS/2 packet */
340 if (psmouse->pktcnt == 3) {
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800341 alps_report_bare_ps2_packet(psmouse, psmouse->packet,
342 true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 return PSMOUSE_FULL_PACKET;
344 }
345 return PSMOUSE_GOOD_DATA;
346 }
347
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800348 /* Check for PS/2 packet stuffed in the middle of ALPS packet. */
349
350 if ((model->flags & ALPS_PS2_INTERLEAVED) &&
351 psmouse->pktcnt >= 4 && (psmouse->packet[3] & 0x0f) == 0x0f) {
352 return alps_handle_interleaved_ps2(psmouse);
353 }
354
355 if (!alps_is_valid_first_byte(model, psmouse->packet[0])) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700356 psmouse_dbg(psmouse,
357 "refusing packet[0] = %x (mask0 = %x, byte0 = %x)\n",
358 psmouse->packet[0], model->mask0, model->byte0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 return PSMOUSE_BAD_DATA;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800360 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362 /* Bytes 2 - 6 should have 0 in the highest bit */
363 if (psmouse->pktcnt >= 2 && psmouse->pktcnt <= 6 &&
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800364 (psmouse->packet[psmouse->pktcnt - 1] & 0x80)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700365 psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
366 psmouse->pktcnt - 1,
367 psmouse->packet[psmouse->pktcnt - 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 return PSMOUSE_BAD_DATA;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
371 if (psmouse->pktcnt == 6) {
David Howells7d12e782006-10-05 14:55:46 +0100372 alps_process_packet(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 return PSMOUSE_FULL_PACKET;
374 }
375
376 return PSMOUSE_GOOD_DATA;
377}
378
Helge Dellere38de672006-09-10 21:54:39 -0400379static const struct alps_model_info *alps_get_model(struct psmouse *psmouse, int *version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
381 struct ps2dev *ps2dev = &psmouse->ps2dev;
Helge Dellere38de672006-09-10 21:54:39 -0400382 static const unsigned char rates[] = { 0, 10, 20, 40, 60, 80, 100, 200 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 unsigned char param[4];
384 int i;
385
386 /*
387 * First try "E6 report".
388 * ALPS should return 0,0,10 or 0,0,100
389 */
390 param[0] = 0;
391 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES) ||
392 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
393 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
394 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11))
395 return NULL;
396
397 param[0] = param[1] = param[2] = 0xff;
398 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
399 return NULL;
400
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700401 psmouse_dbg(psmouse, "E6 report: %2.2x %2.2x %2.2x",
402 param[0], param[1], param[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404 if (param[0] != 0 || param[1] != 0 || (param[2] != 10 && param[2] != 100))
405 return NULL;
406
407 /*
408 * Now try "E7 report". Allowed responses are in
409 * alps_model_data[].signature
410 */
411 param[0] = 0;
412 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES) ||
413 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
414 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
415 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21))
416 return NULL;
417
418 param[0] = param[1] = param[2] = 0xff;
419 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
420 return NULL;
421
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700422 psmouse_dbg(psmouse, "E7 report: %2.2x %2.2x %2.2x",
423 param[0], param[1], param[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -0400425 if (version) {
426 for (i = 0; i < ARRAY_SIZE(rates) && param[2] != rates[i]; i++)
427 /* empty */;
428 *version = (param[0] << 8) | (param[1] << 4) | i;
429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431 for (i = 0; i < ARRAY_SIZE(alps_model_data); i++)
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -0400432 if (!memcmp(param, alps_model_data[i].signature,
433 sizeof(alps_model_data[i].signature)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 return alps_model_data + i;
435
436 return NULL;
437}
438
439/*
440 * For DualPoint devices select the device that should respond to
441 * subsequent commands. It looks like glidepad is behind stickpointer,
442 * I'd thought it would be other way around...
443 */
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700444static int alps_passthrough_mode(struct psmouse *psmouse, bool enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
446 struct ps2dev *ps2dev = &psmouse->ps2dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 int cmd = enable ? PSMOUSE_CMD_SETSCALE21 : PSMOUSE_CMD_SETSCALE11;
448
449 if (ps2_command(ps2dev, NULL, cmd) ||
450 ps2_command(ps2dev, NULL, cmd) ||
451 ps2_command(ps2dev, NULL, cmd) ||
452 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
453 return -1;
454
455 /* we may get 3 more bytes, just ignore them */
Dmitry Torokhovc6117632005-06-01 02:39:51 -0500456 ps2_drain(ps2dev, 3, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458 return 0;
459}
460
461static int alps_absolute_mode(struct psmouse *psmouse)
462{
463 struct ps2dev *ps2dev = &psmouse->ps2dev;
464
465 /* Try ALPS magic knock - 4 disable before enable */
466 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
467 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
468 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
469 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
470 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
471 return -1;
472
473 /*
474 * Switch mouse to poll (remote) mode so motion data will not
475 * get in our way
476 */
477 return ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETPOLL);
478}
479
480static int alps_get_status(struct psmouse *psmouse, char *param)
481{
482 struct ps2dev *ps2dev = &psmouse->ps2dev;
483
484 /* Get status: 0xF5 0xF5 0xF5 0xE9 */
485 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
486 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
487 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
488 ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
489 return -1;
490
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700491 psmouse_dbg(psmouse, "Status: %2.2x %2.2x %2.2x",
492 param[0], param[1], param[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 return 0;
495}
496
497/*
498 * Turn touchpad tapping on or off. The sequences are:
499 * 0xE9 0xF5 0xF5 0xF3 0x0A to enable,
500 * 0xE9 0xF5 0xF5 0xE8 0x00 to disable.
501 * My guess that 0xE9 (GetInfo) is here as a sync point.
502 * For models that also have stickpointer (DualPoints) its tapping
503 * is controlled separately (0xE6 0xE6 0xE6 0xF3 0x14|0x0A) but
504 * we don't fiddle with it.
505 */
506static int alps_tap_mode(struct psmouse *psmouse, int enable)
507{
508 struct ps2dev *ps2dev = &psmouse->ps2dev;
509 int cmd = enable ? PSMOUSE_CMD_SETRATE : PSMOUSE_CMD_SETRES;
510 unsigned char tap_arg = enable ? 0x0A : 0x00;
511 unsigned char param[4];
512
513 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO) ||
514 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
515 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
516 ps2_command(ps2dev, &tap_arg, cmd))
517 return -1;
518
519 if (alps_get_status(psmouse, param))
520 return -1;
521
522 return 0;
523}
524
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500525/*
526 * alps_poll() - poll the touchpad for current motion packet.
527 * Used in resync.
528 */
529static int alps_poll(struct psmouse *psmouse)
530{
531 struct alps_data *priv = psmouse->private;
532 unsigned char buf[6];
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700533 bool poll_failed;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500534
535 if (priv->i->flags & ALPS_PASS)
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700536 alps_passthrough_mode(psmouse, true);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500537
538 poll_failed = ps2_command(&psmouse->ps2dev, buf,
539 PSMOUSE_CMD_POLL | (psmouse->pktsize << 8)) < 0;
540
541 if (priv->i->flags & ALPS_PASS)
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700542 alps_passthrough_mode(psmouse, false);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500543
544 if (poll_failed || (buf[0] & priv->i->mask0) != priv->i->byte0)
545 return -1;
546
547 if ((psmouse->badbyte & 0xc8) == 0x08) {
548/*
549 * Poll the track stick ...
550 */
551 if (ps2_command(&psmouse->ps2dev, buf, PSMOUSE_CMD_POLL | (3 << 8)))
552 return -1;
553 }
554
555 memcpy(psmouse->packet, buf, sizeof(buf));
556 return 0;
557}
558
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800559static int alps_hw_init(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560{
561 struct alps_data *priv = psmouse->private;
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800562 const struct alps_model_info *model = priv->i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800564 if ((model->flags & ALPS_PASS) &&
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700565 alps_passthrough_mode(psmouse, true)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 return -1;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700567 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700569 if (alps_tap_mode(psmouse, true)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700570 psmouse_warn(psmouse, "Failed to enable hardware tapping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 return -1;
Peter Osterlund963f6262005-07-11 01:08:04 -0500572 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
574 if (alps_absolute_mode(psmouse)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700575 psmouse_err(psmouse, "Failed to enable absolute mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 return -1;
577 }
578
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800579 if ((model->flags & ALPS_PASS) &&
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700580 alps_passthrough_mode(psmouse, false)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 return -1;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700582 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -0400584 /* ALPS needs stream mode, otherwise it won't report any data */
585 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700586 psmouse_err(psmouse, "Failed to enable stream mode\n");
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -0400587 return -1;
588 }
589
590 return 0;
591}
592
593static int alps_reconnect(struct psmouse *psmouse)
594{
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800595 const struct alps_model_info *model;
596
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -0400597 psmouse_reset(psmouse);
598
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800599 model = alps_get_model(psmouse, NULL);
600 if (!model)
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -0400601 return -1;
602
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800603 return alps_hw_init(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604}
605
606static void alps_disconnect(struct psmouse *psmouse)
607{
608 struct alps_data *priv = psmouse->private;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 psmouse_reset(psmouse);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800611 del_timer_sync(&priv->timer);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500612 input_unregister_device(priv->dev2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 kfree(priv);
614}
615
616int alps_init(struct psmouse *psmouse)
617{
618 struct alps_data *priv;
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800619 const struct alps_model_info *model;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500620 struct input_dev *dev1 = psmouse->dev, *dev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 int version;
622
Dmitry Torokhovf42649e2007-04-12 01:31:13 -0400623 priv = kzalloc(sizeof(struct alps_data), GFP_KERNEL);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500624 dev2 = input_allocate_device();
625 if (!priv || !dev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 goto init_fail;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500627
628 priv->dev2 = dev2;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800629 setup_timer(&priv->timer, alps_flush_packet, (unsigned long)psmouse);
630
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -0400631 psmouse->private = priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800633 model = alps_get_model(psmouse, &version);
634 if (!model)
635 goto init_fail;
636
637 priv->i = model;
638
639 if (alps_hw_init(psmouse))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 goto init_fail;
641
Dmitry Torokhov7105d2e2009-12-11 23:54:54 -0800642 /*
643 * Undo part of setup done for us by psmouse core since touchpad
644 * is not a relative device.
645 */
646 __clear_bit(EV_REL, dev1->evbit);
647 __clear_bit(REL_X, dev1->relbit);
648 __clear_bit(REL_Y, dev1->relbit);
649
650 /*
651 * Now set up our capabilities.
652 */
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700653 dev1->evbit[BIT_WORD(EV_KEY)] |= BIT_MASK(EV_KEY);
654 dev1->keybit[BIT_WORD(BTN_TOUCH)] |= BIT_MASK(BTN_TOUCH);
655 dev1->keybit[BIT_WORD(BTN_TOOL_FINGER)] |= BIT_MASK(BTN_TOOL_FINGER);
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800656 dev1->keybit[BIT_WORD(BTN_LEFT)] |=
657 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700659 dev1->evbit[BIT_WORD(EV_ABS)] |= BIT_MASK(EV_ABS);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500660 input_set_abs_params(dev1, ABS_X, 0, 1023, 0, 0);
661 input_set_abs_params(dev1, ABS_Y, 0, 767, 0, 0);
662 input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800664 if (model->flags & ALPS_WHEEL) {
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700665 dev1->evbit[BIT_WORD(EV_REL)] |= BIT_MASK(EV_REL);
666 dev1->relbit[BIT_WORD(REL_WHEEL)] |= BIT_MASK(REL_WHEEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 }
668
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800669 if (model->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700670 dev1->keybit[BIT_WORD(BTN_FORWARD)] |= BIT_MASK(BTN_FORWARD);
671 dev1->keybit[BIT_WORD(BTN_BACK)] |= BIT_MASK(BTN_BACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 }
673
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800674 if (model->flags & ALPS_FOUR_BUTTONS) {
675 dev1->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_0);
676 dev1->keybit[BIT_WORD(BTN_1)] |= BIT_MASK(BTN_1);
677 dev1->keybit[BIT_WORD(BTN_2)] |= BIT_MASK(BTN_2);
678 dev1->keybit[BIT_WORD(BTN_3)] |= BIT_MASK(BTN_3);
679 } else {
680 dev1->keybit[BIT_WORD(BTN_MIDDLE)] |= BIT_MASK(BTN_MIDDLE);
681 }
682
Dmitry Torokhov08ffce42006-06-26 01:45:10 -0400683 snprintf(priv->phys, sizeof(priv->phys), "%s/input1", psmouse->ps2dev.serio->phys);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500684 dev2->phys = priv->phys;
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800685 dev2->name = (model->flags & ALPS_DUALPOINT) ? "DualPoint Stick" : "PS/2 Mouse";
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500686 dev2->id.bustype = BUS_I8042;
687 dev2->id.vendor = 0x0002;
688 dev2->id.product = PSMOUSE_ALPS;
689 dev2->id.version = 0x0000;
Dmitry Torokhov1db3a342008-03-18 00:29:18 -0400690 dev2->dev.parent = &psmouse->ps2dev.serio->dev;
Dmitry Torokhov968ac842005-05-29 02:28:29 -0500691
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700692 dev2->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800693 dev2->relbit[BIT_WORD(REL_X)] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
694 dev2->keybit[BIT_WORD(BTN_LEFT)] =
695 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_MIDDLE) | BIT_MASK(BTN_RIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Dmitry Torokhovf42649e2007-04-12 01:31:13 -0400697 if (input_register_device(priv->dev2))
698 goto init_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
700 psmouse->protocol_handler = alps_process_byte;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500701 psmouse->poll = alps_poll;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 psmouse->disconnect = alps_disconnect;
703 psmouse->reconnect = alps_reconnect;
704 psmouse->pktsize = 6;
705
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500706 /* We are having trouble resyncing ALPS touchpads so disable it for now */
707 psmouse->resync_time = 0;
708
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 return 0;
710
711init_fail:
Dmitry Torokhovf42649e2007-04-12 01:31:13 -0400712 psmouse_reset(psmouse);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500713 input_free_device(dev2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 kfree(priv);
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -0400715 psmouse->private = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 return -1;
717}
718
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700719int alps_detect(struct psmouse *psmouse, bool set_properties)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720{
721 int version;
Helge Dellere38de672006-09-10 21:54:39 -0400722 const struct alps_model_info *model;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Dmitry Torokhovf42649e2007-04-12 01:31:13 -0400724 model = alps_get_model(psmouse, &version);
725 if (!model)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 return -1;
727
728 if (set_properties) {
729 psmouse->vendor = "ALPS";
Dmitry Torokhov968ac842005-05-29 02:28:29 -0500730 psmouse->name = model->flags & ALPS_DUALPOINT ?
731 "DualPoint TouchPad" : "GlidePoint";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 psmouse->model = version;
733 }
734 return 0;
735}
736