blob: 7490f1da4a532d8d04506320f840254deb91f351 [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
18#include <linux/input.h>
19#include <linux/serio.h>
20#include <linux/libps2.h>
21
22#include "psmouse.h"
23#include "alps.h"
24
25#undef DEBUG
26#ifdef DEBUG
27#define dbg(format, arg...) printk(KERN_INFO "alps.c: " format "\n", ## arg)
28#else
29#define dbg(format, arg...) do {} while (0)
30#endif
31
Maxim Levitsky71bb21b2009-11-16 22:12:22 -080032#define ALPS_OLDPROTO 0x01 /* old style input */
33#define ALPS_DUALPOINT 0x02 /* touchpad has trackstick */
34#define ALPS_PASS 0x04 /* device has a pass-through port */
35
36#define ALPS_WHEEL 0x08 /* hardware wheel present */
37#define ALPS_FW_BK_1 0x10 /* front & back buttons present */
38#define ALPS_FW_BK_2 0x20 /* front & back buttons present */
39#define ALPS_FOUR_BUTTONS 0x40 /* 4 direction button present */
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -080040#define ALPS_PS2_INTERLEAVED 0x80 /* 3-byte PS/2 packet interleaved with
41 6-byte ALPS packet */
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Helge Dellere38de672006-09-10 21:54:39 -040043static const struct alps_model_info alps_model_data[] = {
Dmitry Torokhovb75d1722009-04-23 19:25:51 -070044 { { 0x32, 0x02, 0x14 }, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT }, /* Toshiba Salellite Pro M10 */
Dmitry Torokhovaf27a692009-05-08 18:30:33 -070045 { { 0x33, 0x02, 0x0a }, 0x88, 0xf8, ALPS_OLDPROTO }, /* UMAX-530T */
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 { { 0x53, 0x02, 0x0a }, 0xf8, 0xf8, 0 },
47 { { 0x53, 0x02, 0x14 }, 0xf8, 0xf8, 0 },
Dmitry Torokhovaf27a692009-05-08 18:30:33 -070048 { { 0x60, 0x03, 0xc8 }, 0xf8, 0xf8, 0 }, /* HP ze1115 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 { { 0x63, 0x02, 0x0a }, 0xf8, 0xf8, 0 },
50 { { 0x63, 0x02, 0x14 }, 0xf8, 0xf8, 0 },
Dmitry Torokhovaf27a692009-05-08 18:30:33 -070051 { { 0x63, 0x02, 0x28 }, 0xf8, 0xf8, ALPS_FW_BK_2 }, /* Fujitsu Siemens S6010 */
52 { { 0x63, 0x02, 0x3c }, 0x8f, 0x8f, ALPS_WHEEL }, /* Toshiba Satellite S2400-103 */
53 { { 0x63, 0x02, 0x50 }, 0xef, 0xef, ALPS_FW_BK_1 }, /* NEC Versa L320 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 { { 0x63, 0x02, 0x64 }, 0xf8, 0xf8, 0 },
Dmitry Torokhovaf27a692009-05-08 18:30:33 -070055 { { 0x63, 0x03, 0xc8 }, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT }, /* Dell Latitude D800 */
56 { { 0x73, 0x00, 0x0a }, 0xf8, 0xf8, ALPS_DUALPOINT }, /* ThinkPad R61 8918-5QG */
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 { { 0x73, 0x02, 0x0a }, 0xf8, 0xf8, 0 },
Dmitry Torokhovaf27a692009-05-08 18:30:33 -070058 { { 0x73, 0x02, 0x14 }, 0xf8, 0xf8, ALPS_FW_BK_2 }, /* Ahtec Laptop */
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 { { 0x20, 0x02, 0x0e }, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT }, /* XXX */
60 { { 0x22, 0x02, 0x0a }, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT },
61 { { 0x22, 0x02, 0x14 }, 0xff, 0xff, ALPS_PASS | ALPS_DUALPOINT }, /* Dell Latitude D600 */
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -080062 /* Dell Latitude E5500, E6400, E6500, Precision M4400 */
63 { { 0x62, 0x02, 0x14 }, 0xcf, 0xcf,
64 ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED },
Maxim Levitsky71bb21b2009-11-16 22:12:22 -080065 { { 0x73, 0x02, 0x50 }, 0xcf, 0xcf, ALPS_FOUR_BUTTONS }, /* Dell Vostro 1400 */
Thomas Bächlereb8bff82010-03-09 20:38:48 -080066 { { 0x52, 0x01, 0x14 }, 0xff, 0xff,
67 ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED }, /* Toshiba Tecra A11-11L */
Linus Torvalds1da177e2005-04-16 15:20:36 -070068};
69
70/*
71 * XXX - this entry is suspicious. First byte has zero lower nibble,
72 * which is what a normal mouse would report. Also, the value 0x0e
73 * isn't valid per PS/2 spec.
74 */
75
76/*
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -080077 * PS/2 packet format
78 *
79 * byte 0: 0 0 YSGN XSGN 1 M R L
80 * byte 1: X7 X6 X5 X4 X3 X2 X1 X0
81 * byte 2: Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y0
82 *
83 * Note that the device never signals overflow condition.
84 *
85 * ALPS absolute Mode - new format
Dmitry Torokhov968ac842005-05-29 02:28:29 -050086 *
87 * byte 0: 1 ? ? ? 1 ? ? ?
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 * byte 1: 0 x6 x5 x4 x3 x2 x1 x0
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -080089 * byte 2: 0 x10 x9 x8 x7 ? fin ges
Dmitry Torokhov968ac842005-05-29 02:28:29 -050090 * byte 3: 0 y9 y8 y7 1 M R L
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 * byte 4: 0 y6 y5 y4 y3 y2 y1 y0
92 * byte 5: 0 z6 z5 z4 z3 z2 z1 z0
93 *
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -080094 * Dualpoint device -- interleaved packet format
95 *
96 * byte 0: 1 1 0 0 1 1 1 1
97 * byte 1: 0 x6 x5 x4 x3 x2 x1 x0
98 * byte 2: 0 x10 x9 x8 x7 0 fin ges
99 * byte 3: 0 0 YSGN XSGN 1 1 1 1
100 * byte 4: X7 X6 X5 X4 X3 X2 X1 X0
101 * byte 5: Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y0
102 * byte 6: 0 y9 y8 y7 1 m r l
103 * byte 7: 0 y6 y5 y4 y3 y2 y1 y0
104 * byte 8: 0 z6 z5 z4 z3 z2 z1 z0
105 *
106 * CAPITALS = stick, miniscules = touchpad
107 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 * ?'s can have different meanings on different models,
109 * such as wheel rotation, extra buttons, stick buttons
110 * on a dualpoint, etc.
111 */
112
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800113static bool alps_is_valid_first_byte(const struct alps_model_info *model,
114 unsigned char data)
115{
116 return (data & model->mask0) == model->byte0;
117}
118
119static void alps_report_buttons(struct psmouse *psmouse,
120 struct input_dev *dev1, struct input_dev *dev2,
121 int left, int right, int middle)
122{
Martin Buckc91ed052010-03-13 22:23:58 -0800123 struct input_dev *dev;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800124
Martin Buckc91ed052010-03-13 22:23:58 -0800125 /*
126 * If shared button has already been reported on the
127 * other device (dev2) then this event should be also
128 * sent through that device.
129 */
130 dev = test_bit(BTN_LEFT, dev2->key) ? dev2 : dev1;
131 input_report_key(dev, BTN_LEFT, left);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800132
Martin Buckc91ed052010-03-13 22:23:58 -0800133 dev = test_bit(BTN_RIGHT, dev2->key) ? dev2 : dev1;
134 input_report_key(dev, BTN_RIGHT, right);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800135
Martin Buckc91ed052010-03-13 22:23:58 -0800136 dev = test_bit(BTN_MIDDLE, dev2->key) ? dev2 : dev1;
137 input_report_key(dev, BTN_MIDDLE, middle);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800138
Martin Buckc91ed052010-03-13 22:23:58 -0800139 /*
140 * Sync the _other_ device now, we'll do the first
141 * device later once we report the rest of the events.
142 */
143 input_sync(dev2);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800144}
145
David Howells7d12e782006-10-05 14:55:46 +0100146static void alps_process_packet(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
148 struct alps_data *priv = psmouse->private;
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800149 const struct alps_model_info *model = priv->i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 unsigned char *packet = psmouse->packet;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500151 struct input_dev *dev = psmouse->dev;
152 struct input_dev *dev2 = priv->dev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 int x, y, z, ges, fin, left, right, middle;
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500154 int back = 0, forward = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800156 if (model->flags & ALPS_OLDPROTO) {
Yotam Medinid2f40122006-05-29 23:30:36 -0400157 left = packet[2] & 0x10;
158 right = packet[2] & 0x08;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 middle = 0;
160 x = packet[1] | ((packet[0] & 0x07) << 7);
161 y = packet[4] | ((packet[3] & 0x07) << 7);
162 z = packet[5];
163 } else {
164 left = packet[3] & 1;
165 right = packet[3] & 2;
166 middle = packet[3] & 4;
167 x = packet[1] | ((packet[2] & 0x78) << (7 - 3));
168 y = packet[4] | ((packet[3] & 0x70) << (7 - 4));
169 z = packet[5];
170 }
171
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800172 if (model->flags & ALPS_FW_BK_1) {
Laszlo Kajan3c00bb92008-03-18 00:39:55 -0400173 back = packet[0] & 0x10;
174 forward = packet[2] & 4;
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500175 }
176
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800177 if (model->flags & ALPS_FW_BK_2) {
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500178 back = packet[3] & 4;
179 forward = packet[2] & 4;
180 if ((middle = forward && back))
181 forward = back = 0;
182 }
183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 ges = packet[2] & 1;
185 fin = packet[2] & 2;
186
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800187 if ((model->flags & ALPS_DUALPOINT) && z == 127) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 input_report_rel(dev2, REL_X, (x > 383 ? (x - 768) : x));
189 input_report_rel(dev2, REL_Y, -(y > 255 ? (y - 512) : y));
Ulrich Dangeld7ed5d82009-06-11 00:15:09 -0700190
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800191 alps_report_buttons(psmouse, dev2, dev, left, right, middle);
Ulrich Dangeld7ed5d82009-06-11 00:15:09 -0700192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 input_sync(dev2);
194 return;
195 }
196
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800197 alps_report_buttons(psmouse, dev, dev2, left, right, middle);
Ulrich Dangeld7ed5d82009-06-11 00:15:09 -0700198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 /* Convert hardware tap to a reasonable Z value */
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800200 if (ges && !fin)
201 z = 40;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203 /*
204 * A "tap and drag" operation is reported by the hardware as a transition
205 * from (!fin && ges) to (fin && ges). This should be translated to the
206 * sequence Z>0, Z==0, Z>0, so the Z==0 event has to be generated manually.
207 */
208 if (ges && fin && !priv->prev_fin) {
209 input_report_abs(dev, ABS_X, x);
210 input_report_abs(dev, ABS_Y, y);
211 input_report_abs(dev, ABS_PRESSURE, 0);
212 input_report_key(dev, BTN_TOOL_FINGER, 0);
213 input_sync(dev);
214 }
215 priv->prev_fin = fin;
216
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800217 if (z > 30)
218 input_report_key(dev, BTN_TOUCH, 1);
219 if (z < 25)
220 input_report_key(dev, BTN_TOUCH, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222 if (z > 0) {
223 input_report_abs(dev, ABS_X, x);
224 input_report_abs(dev, ABS_Y, y);
225 }
226
227 input_report_abs(dev, ABS_PRESSURE, z);
228 input_report_key(dev, BTN_TOOL_FINGER, z > 0);
229
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800230 if (model->flags & ALPS_WHEEL)
Vojtech Pavlike6c047b2005-09-04 01:40:43 -0500231 input_report_rel(dev, REL_WHEEL, ((packet[2] << 1) & 0x08) - ((packet[0] >> 4) & 0x07));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800233 if (model->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500234 input_report_key(dev, BTN_FORWARD, forward);
235 input_report_key(dev, BTN_BACK, back);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 }
237
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800238 if (model->flags & ALPS_FOUR_BUTTONS) {
239 input_report_key(dev, BTN_0, packet[2] & 4);
240 input_report_key(dev, BTN_1, packet[0] & 0x10);
241 input_report_key(dev, BTN_2, packet[3] & 4);
242 input_report_key(dev, BTN_3, packet[0] & 0x20);
243 }
244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 input_sync(dev);
246}
247
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800248static void alps_report_bare_ps2_packet(struct psmouse *psmouse,
249 unsigned char packet[],
250 bool report_buttons)
251{
252 struct alps_data *priv = psmouse->private;
253 struct input_dev *dev2 = priv->dev2;
254
255 if (report_buttons)
256 alps_report_buttons(psmouse, dev2, psmouse->dev,
257 packet[0] & 1, packet[0] & 2, packet[0] & 4);
258
259 input_report_rel(dev2, REL_X,
260 packet[1] ? packet[1] - ((packet[0] << 4) & 0x100) : 0);
261 input_report_rel(dev2, REL_Y,
262 packet[2] ? ((packet[0] << 3) & 0x100) - packet[2] : 0);
263
264 input_sync(dev2);
265}
266
267static psmouse_ret_t alps_handle_interleaved_ps2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
269 struct alps_data *priv = psmouse->private;
270
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800271 if (psmouse->pktcnt < 6)
272 return PSMOUSE_GOOD_DATA;
273
274 if (psmouse->pktcnt == 6) {
275 /*
276 * Start a timer to flush the packet if it ends up last
277 * 6-byte packet in the stream. Timer needs to fire
278 * psmouse core times out itself. 20 ms should be enough
279 * to decide if we are getting more data or not.
280 */
281 mod_timer(&priv->timer, jiffies + msecs_to_jiffies(20));
282 return PSMOUSE_GOOD_DATA;
283 }
284
285 del_timer(&priv->timer);
286
287 if (psmouse->packet[6] & 0x80) {
288
289 /*
290 * Highest bit is set - that means we either had
291 * complete ALPS packet and this is start of the
292 * next packet or we got garbage.
293 */
294
295 if (((psmouse->packet[3] |
296 psmouse->packet[4] |
297 psmouse->packet[5]) & 0x80) ||
298 (!alps_is_valid_first_byte(priv->i, psmouse->packet[6]))) {
299 dbg("refusing packet %x %x %x %x "
300 "(suspected interleaved ps/2)\n",
301 psmouse->packet[3], psmouse->packet[4],
302 psmouse->packet[5], psmouse->packet[6]);
303 return PSMOUSE_BAD_DATA;
304 }
305
306 alps_process_packet(psmouse);
307
308 /* Continue with the next packet */
309 psmouse->packet[0] = psmouse->packet[6];
310 psmouse->pktcnt = 1;
311
312 } else {
313
314 /*
315 * High bit is 0 - that means that we indeed got a PS/2
316 * packet in the middle of ALPS packet.
317 *
318 * There is also possibility that we got 6-byte ALPS
319 * packet followed by 3-byte packet from trackpoint. We
320 * can not distinguish between these 2 scenarios but
321 * becase the latter is unlikely to happen in course of
322 * normal operation (user would need to press all
323 * buttons on the pad and start moving trackpoint
324 * without touching the pad surface) we assume former.
325 * Even if we are wrong the wost thing that would happen
326 * the cursor would jump but we should not get protocol
327 * desynchronization.
328 */
329
330 alps_report_bare_ps2_packet(psmouse, &psmouse->packet[3],
331 false);
332
333 /*
334 * Continue with the standard ALPS protocol handling,
335 * but make sure we won't process it as an interleaved
336 * packet again, which may happen if all buttons are
337 * pressed. To avoid this let's reset the 4th bit which
338 * is normally 1.
339 */
340 psmouse->packet[3] = psmouse->packet[6] & 0xf7;
341 psmouse->pktcnt = 4;
342 }
343
344 return PSMOUSE_GOOD_DATA;
345}
346
347static void alps_flush_packet(unsigned long data)
348{
349 struct psmouse *psmouse = (struct psmouse *)data;
350
351 serio_pause_rx(psmouse->ps2dev.serio);
352
353 if (psmouse->pktcnt == 6) {
354
355 /*
356 * We did not any more data in reasonable amount of time.
357 * Validate the last 3 bytes and process as a standard
358 * ALPS packet.
359 */
360 if ((psmouse->packet[3] |
361 psmouse->packet[4] |
362 psmouse->packet[5]) & 0x80) {
363 dbg("refusing packet %x %x %x "
364 "(suspected interleaved ps/2)\n",
365 psmouse->packet[3], psmouse->packet[4],
366 psmouse->packet[5]);
367 } else {
368 alps_process_packet(psmouse);
369 }
370 psmouse->pktcnt = 0;
371 }
372
373 serio_continue_rx(psmouse->ps2dev.serio);
374}
375
376static psmouse_ret_t alps_process_byte(struct psmouse *psmouse)
377{
378 struct alps_data *priv = psmouse->private;
379 const struct alps_model_info *model = priv->i;
380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 if ((psmouse->packet[0] & 0xc8) == 0x08) { /* PS/2 packet */
382 if (psmouse->pktcnt == 3) {
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800383 alps_report_bare_ps2_packet(psmouse, psmouse->packet,
384 true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 return PSMOUSE_FULL_PACKET;
386 }
387 return PSMOUSE_GOOD_DATA;
388 }
389
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800390 /* Check for PS/2 packet stuffed in the middle of ALPS packet. */
391
392 if ((model->flags & ALPS_PS2_INTERLEAVED) &&
393 psmouse->pktcnt >= 4 && (psmouse->packet[3] & 0x0f) == 0x0f) {
394 return alps_handle_interleaved_ps2(psmouse);
395 }
396
397 if (!alps_is_valid_first_byte(model, psmouse->packet[0])) {
398 dbg("refusing packet[0] = %x (mask0 = %x, byte0 = %x)\n",
399 psmouse->packet[0], model->mask0, model->byte0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 return PSMOUSE_BAD_DATA;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800401 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403 /* Bytes 2 - 6 should have 0 in the highest bit */
404 if (psmouse->pktcnt >= 2 && psmouse->pktcnt <= 6 &&
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800405 (psmouse->packet[psmouse->pktcnt - 1] & 0x80)) {
406 dbg("refusing packet[%i] = %x\n",
407 psmouse->pktcnt - 1, psmouse->packet[psmouse->pktcnt - 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 return PSMOUSE_BAD_DATA;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
411 if (psmouse->pktcnt == 6) {
David Howells7d12e782006-10-05 14:55:46 +0100412 alps_process_packet(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 return PSMOUSE_FULL_PACKET;
414 }
415
416 return PSMOUSE_GOOD_DATA;
417}
418
Helge Dellere38de672006-09-10 21:54:39 -0400419static const struct alps_model_info *alps_get_model(struct psmouse *psmouse, int *version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
421 struct ps2dev *ps2dev = &psmouse->ps2dev;
Helge Dellere38de672006-09-10 21:54:39 -0400422 static const unsigned char rates[] = { 0, 10, 20, 40, 60, 80, 100, 200 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 unsigned char param[4];
424 int i;
425
426 /*
427 * First try "E6 report".
428 * ALPS should return 0,0,10 or 0,0,100
429 */
430 param[0] = 0;
431 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES) ||
432 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
433 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
434 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11))
435 return NULL;
436
437 param[0] = param[1] = param[2] = 0xff;
438 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
439 return NULL;
440
441 dbg("E6 report: %2.2x %2.2x %2.2x", param[0], param[1], param[2]);
442
443 if (param[0] != 0 || param[1] != 0 || (param[2] != 10 && param[2] != 100))
444 return NULL;
445
446 /*
447 * Now try "E7 report". Allowed responses are in
448 * alps_model_data[].signature
449 */
450 param[0] = 0;
451 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES) ||
452 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
453 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
454 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21))
455 return NULL;
456
457 param[0] = param[1] = param[2] = 0xff;
458 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
459 return NULL;
460
461 dbg("E7 report: %2.2x %2.2x %2.2x", param[0], param[1], param[2]);
462
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -0400463 if (version) {
464 for (i = 0; i < ARRAY_SIZE(rates) && param[2] != rates[i]; i++)
465 /* empty */;
466 *version = (param[0] << 8) | (param[1] << 4) | i;
467 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
469 for (i = 0; i < ARRAY_SIZE(alps_model_data); i++)
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -0400470 if (!memcmp(param, alps_model_data[i].signature,
471 sizeof(alps_model_data[i].signature)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 return alps_model_data + i;
473
474 return NULL;
475}
476
477/*
478 * For DualPoint devices select the device that should respond to
479 * subsequent commands. It looks like glidepad is behind stickpointer,
480 * I'd thought it would be other way around...
481 */
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700482static int alps_passthrough_mode(struct psmouse *psmouse, bool enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483{
484 struct ps2dev *ps2dev = &psmouse->ps2dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 int cmd = enable ? PSMOUSE_CMD_SETSCALE21 : PSMOUSE_CMD_SETSCALE11;
486
487 if (ps2_command(ps2dev, NULL, cmd) ||
488 ps2_command(ps2dev, NULL, cmd) ||
489 ps2_command(ps2dev, NULL, cmd) ||
490 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
491 return -1;
492
493 /* we may get 3 more bytes, just ignore them */
Dmitry Torokhovc6117632005-06-01 02:39:51 -0500494 ps2_drain(ps2dev, 3, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
496 return 0;
497}
498
499static int alps_absolute_mode(struct psmouse *psmouse)
500{
501 struct ps2dev *ps2dev = &psmouse->ps2dev;
502
503 /* Try ALPS magic knock - 4 disable before enable */
504 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
505 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
506 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
507 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
508 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
509 return -1;
510
511 /*
512 * Switch mouse to poll (remote) mode so motion data will not
513 * get in our way
514 */
515 return ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETPOLL);
516}
517
518static int alps_get_status(struct psmouse *psmouse, char *param)
519{
520 struct ps2dev *ps2dev = &psmouse->ps2dev;
521
522 /* Get status: 0xF5 0xF5 0xF5 0xE9 */
523 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
524 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
525 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
526 ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
527 return -1;
528
529 dbg("Status: %2.2x %2.2x %2.2x", param[0], param[1], param[2]);
530
531 return 0;
532}
533
534/*
535 * Turn touchpad tapping on or off. The sequences are:
536 * 0xE9 0xF5 0xF5 0xF3 0x0A to enable,
537 * 0xE9 0xF5 0xF5 0xE8 0x00 to disable.
538 * My guess that 0xE9 (GetInfo) is here as a sync point.
539 * For models that also have stickpointer (DualPoints) its tapping
540 * is controlled separately (0xE6 0xE6 0xE6 0xF3 0x14|0x0A) but
541 * we don't fiddle with it.
542 */
543static int alps_tap_mode(struct psmouse *psmouse, int enable)
544{
545 struct ps2dev *ps2dev = &psmouse->ps2dev;
546 int cmd = enable ? PSMOUSE_CMD_SETRATE : PSMOUSE_CMD_SETRES;
547 unsigned char tap_arg = enable ? 0x0A : 0x00;
548 unsigned char param[4];
549
550 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO) ||
551 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
552 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
553 ps2_command(ps2dev, &tap_arg, cmd))
554 return -1;
555
556 if (alps_get_status(psmouse, param))
557 return -1;
558
559 return 0;
560}
561
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500562/*
563 * alps_poll() - poll the touchpad for current motion packet.
564 * Used in resync.
565 */
566static int alps_poll(struct psmouse *psmouse)
567{
568 struct alps_data *priv = psmouse->private;
569 unsigned char buf[6];
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700570 bool poll_failed;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500571
572 if (priv->i->flags & ALPS_PASS)
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700573 alps_passthrough_mode(psmouse, true);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500574
575 poll_failed = ps2_command(&psmouse->ps2dev, buf,
576 PSMOUSE_CMD_POLL | (psmouse->pktsize << 8)) < 0;
577
578 if (priv->i->flags & ALPS_PASS)
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700579 alps_passthrough_mode(psmouse, false);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500580
581 if (poll_failed || (buf[0] & priv->i->mask0) != priv->i->byte0)
582 return -1;
583
584 if ((psmouse->badbyte & 0xc8) == 0x08) {
585/*
586 * Poll the track stick ...
587 */
588 if (ps2_command(&psmouse->ps2dev, buf, PSMOUSE_CMD_POLL | (3 << 8)))
589 return -1;
590 }
591
592 memcpy(psmouse->packet, buf, sizeof(buf));
593 return 0;
594}
595
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800596static int alps_hw_init(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597{
598 struct alps_data *priv = psmouse->private;
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800599 const struct alps_model_info *model = priv->i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800601 if ((model->flags & ALPS_PASS) &&
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700602 alps_passthrough_mode(psmouse, true)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 return -1;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700604 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700606 if (alps_tap_mode(psmouse, true)) {
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -0400607 printk(KERN_WARNING "alps.c: Failed to enable hardware tapping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 return -1;
Peter Osterlund963f6262005-07-11 01:08:04 -0500609 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
611 if (alps_absolute_mode(psmouse)) {
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -0400612 printk(KERN_ERR "alps.c: Failed to enable absolute mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 return -1;
614 }
615
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800616 if ((model->flags & ALPS_PASS) &&
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700617 alps_passthrough_mode(psmouse, false)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 return -1;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700619 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -0400621 /* ALPS needs stream mode, otherwise it won't report any data */
622 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)) {
623 printk(KERN_ERR "alps.c: Failed to enable stream mode\n");
624 return -1;
625 }
626
627 return 0;
628}
629
630static int alps_reconnect(struct psmouse *psmouse)
631{
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800632 const struct alps_model_info *model;
633
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -0400634 psmouse_reset(psmouse);
635
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800636 model = alps_get_model(psmouse, NULL);
637 if (!model)
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -0400638 return -1;
639
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800640 return alps_hw_init(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641}
642
643static void alps_disconnect(struct psmouse *psmouse)
644{
645 struct alps_data *priv = psmouse->private;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500646
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 psmouse_reset(psmouse);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800648 del_timer_sync(&priv->timer);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500649 input_unregister_device(priv->dev2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 kfree(priv);
651}
652
653int alps_init(struct psmouse *psmouse)
654{
655 struct alps_data *priv;
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800656 const struct alps_model_info *model;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500657 struct input_dev *dev1 = psmouse->dev, *dev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 int version;
659
Dmitry Torokhovf42649e2007-04-12 01:31:13 -0400660 priv = kzalloc(sizeof(struct alps_data), GFP_KERNEL);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500661 dev2 = input_allocate_device();
662 if (!priv || !dev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 goto init_fail;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500664
665 priv->dev2 = dev2;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800666 setup_timer(&priv->timer, alps_flush_packet, (unsigned long)psmouse);
667
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -0400668 psmouse->private = priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800670 model = alps_get_model(psmouse, &version);
671 if (!model)
672 goto init_fail;
673
674 priv->i = model;
675
676 if (alps_hw_init(psmouse))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 goto init_fail;
678
Dmitry Torokhov7105d2e2009-12-11 23:54:54 -0800679 /*
680 * Undo part of setup done for us by psmouse core since touchpad
681 * is not a relative device.
682 */
683 __clear_bit(EV_REL, dev1->evbit);
684 __clear_bit(REL_X, dev1->relbit);
685 __clear_bit(REL_Y, dev1->relbit);
686
687 /*
688 * Now set up our capabilities.
689 */
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700690 dev1->evbit[BIT_WORD(EV_KEY)] |= BIT_MASK(EV_KEY);
691 dev1->keybit[BIT_WORD(BTN_TOUCH)] |= BIT_MASK(BTN_TOUCH);
692 dev1->keybit[BIT_WORD(BTN_TOOL_FINGER)] |= BIT_MASK(BTN_TOOL_FINGER);
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800693 dev1->keybit[BIT_WORD(BTN_LEFT)] |=
694 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700696 dev1->evbit[BIT_WORD(EV_ABS)] |= BIT_MASK(EV_ABS);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500697 input_set_abs_params(dev1, ABS_X, 0, 1023, 0, 0);
698 input_set_abs_params(dev1, ABS_Y, 0, 767, 0, 0);
699 input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800701 if (model->flags & ALPS_WHEEL) {
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700702 dev1->evbit[BIT_WORD(EV_REL)] |= BIT_MASK(EV_REL);
703 dev1->relbit[BIT_WORD(REL_WHEEL)] |= BIT_MASK(REL_WHEEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 }
705
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800706 if (model->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700707 dev1->keybit[BIT_WORD(BTN_FORWARD)] |= BIT_MASK(BTN_FORWARD);
708 dev1->keybit[BIT_WORD(BTN_BACK)] |= BIT_MASK(BTN_BACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 }
710
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800711 if (model->flags & ALPS_FOUR_BUTTONS) {
712 dev1->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_0);
713 dev1->keybit[BIT_WORD(BTN_1)] |= BIT_MASK(BTN_1);
714 dev1->keybit[BIT_WORD(BTN_2)] |= BIT_MASK(BTN_2);
715 dev1->keybit[BIT_WORD(BTN_3)] |= BIT_MASK(BTN_3);
716 } else {
717 dev1->keybit[BIT_WORD(BTN_MIDDLE)] |= BIT_MASK(BTN_MIDDLE);
718 }
719
Dmitry Torokhov08ffce42006-06-26 01:45:10 -0400720 snprintf(priv->phys, sizeof(priv->phys), "%s/input1", psmouse->ps2dev.serio->phys);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500721 dev2->phys = priv->phys;
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800722 dev2->name = (model->flags & ALPS_DUALPOINT) ? "DualPoint Stick" : "PS/2 Mouse";
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500723 dev2->id.bustype = BUS_I8042;
724 dev2->id.vendor = 0x0002;
725 dev2->id.product = PSMOUSE_ALPS;
726 dev2->id.version = 0x0000;
Dmitry Torokhov1db3a342008-03-18 00:29:18 -0400727 dev2->dev.parent = &psmouse->ps2dev.serio->dev;
Dmitry Torokhov968ac842005-05-29 02:28:29 -0500728
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700729 dev2->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800730 dev2->relbit[BIT_WORD(REL_X)] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
731 dev2->keybit[BIT_WORD(BTN_LEFT)] =
732 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_MIDDLE) | BIT_MASK(BTN_RIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Dmitry Torokhovf42649e2007-04-12 01:31:13 -0400734 if (input_register_device(priv->dev2))
735 goto init_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
737 psmouse->protocol_handler = alps_process_byte;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500738 psmouse->poll = alps_poll;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 psmouse->disconnect = alps_disconnect;
740 psmouse->reconnect = alps_reconnect;
741 psmouse->pktsize = 6;
742
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500743 /* We are having trouble resyncing ALPS touchpads so disable it for now */
744 psmouse->resync_time = 0;
745
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 return 0;
747
748init_fail:
Dmitry Torokhovf42649e2007-04-12 01:31:13 -0400749 psmouse_reset(psmouse);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500750 input_free_device(dev2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 kfree(priv);
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -0400752 psmouse->private = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 return -1;
754}
755
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700756int alps_detect(struct psmouse *psmouse, bool set_properties)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
758 int version;
Helge Dellere38de672006-09-10 21:54:39 -0400759 const struct alps_model_info *model;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
Dmitry Torokhovf42649e2007-04-12 01:31:13 -0400761 model = alps_get_model(psmouse, &version);
762 if (!model)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 return -1;
764
765 if (set_properties) {
766 psmouse->vendor = "ALPS";
Dmitry Torokhov968ac842005-05-29 02:28:29 -0500767 psmouse->name = model->flags & ALPS_DUALPOINT ?
768 "DualPoint TouchPad" : "GlidePoint";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 psmouse->model = version;
770 }
771 return 0;
772}
773