blob: ecd93894c806d6ed3b089787202cbcafd32d8d3a [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>
Seth Forshee01ce6612011-11-07 19:54:13 -080020#include <linux/input/mt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/serio.h>
22#include <linux/libps2.h>
23
24#include "psmouse.h"
25#include "alps.h"
26
Seth Forshee25bded72011-11-07 19:53:36 -080027/*
28 * Definitions for ALPS version 3 and 4 command mode protocol
29 */
Seth Forshee01ce6612011-11-07 19:54:13 -080030#define ALPS_V3_X_MAX 2000
31#define ALPS_V3_Y_MAX 1400
32
33#define ALPS_BITMAP_X_BITS 15
34#define ALPS_BITMAP_Y_BITS 11
35
Seth Forshee25bded72011-11-07 19:53:36 -080036#define ALPS_CMD_NIBBLE_10 0x01f2
37
38static const struct alps_nibble_commands alps_v3_nibble_commands[] = {
39 { PSMOUSE_CMD_SETPOLL, 0x00 }, /* 0 */
40 { PSMOUSE_CMD_RESET_DIS, 0x00 }, /* 1 */
41 { PSMOUSE_CMD_SETSCALE21, 0x00 }, /* 2 */
42 { PSMOUSE_CMD_SETRATE, 0x0a }, /* 3 */
43 { PSMOUSE_CMD_SETRATE, 0x14 }, /* 4 */
44 { PSMOUSE_CMD_SETRATE, 0x28 }, /* 5 */
45 { PSMOUSE_CMD_SETRATE, 0x3c }, /* 6 */
46 { PSMOUSE_CMD_SETRATE, 0x50 }, /* 7 */
47 { PSMOUSE_CMD_SETRATE, 0x64 }, /* 8 */
48 { PSMOUSE_CMD_SETRATE, 0xc8 }, /* 9 */
49 { ALPS_CMD_NIBBLE_10, 0x00 }, /* a */
50 { PSMOUSE_CMD_SETRES, 0x00 }, /* b */
51 { PSMOUSE_CMD_SETRES, 0x01 }, /* c */
52 { PSMOUSE_CMD_SETRES, 0x02 }, /* d */
53 { PSMOUSE_CMD_SETRES, 0x03 }, /* e */
54 { PSMOUSE_CMD_SETSCALE11, 0x00 }, /* f */
55};
56
57static const struct alps_nibble_commands alps_v4_nibble_commands[] = {
58 { PSMOUSE_CMD_ENABLE, 0x00 }, /* 0 */
59 { PSMOUSE_CMD_RESET_DIS, 0x00 }, /* 1 */
60 { PSMOUSE_CMD_SETSCALE21, 0x00 }, /* 2 */
61 { PSMOUSE_CMD_SETRATE, 0x0a }, /* 3 */
62 { PSMOUSE_CMD_SETRATE, 0x14 }, /* 4 */
63 { PSMOUSE_CMD_SETRATE, 0x28 }, /* 5 */
64 { PSMOUSE_CMD_SETRATE, 0x3c }, /* 6 */
65 { PSMOUSE_CMD_SETRATE, 0x50 }, /* 7 */
66 { PSMOUSE_CMD_SETRATE, 0x64 }, /* 8 */
67 { PSMOUSE_CMD_SETRATE, 0xc8 }, /* 9 */
68 { ALPS_CMD_NIBBLE_10, 0x00 }, /* a */
69 { PSMOUSE_CMD_SETRES, 0x00 }, /* b */
70 { PSMOUSE_CMD_SETRES, 0x01 }, /* c */
71 { PSMOUSE_CMD_SETRES, 0x02 }, /* d */
72 { PSMOUSE_CMD_SETRES, 0x03 }, /* e */
73 { PSMOUSE_CMD_SETSCALE11, 0x00 }, /* f */
74};
75
76
Maxim Levitsky71bb21b2009-11-16 22:12:22 -080077#define ALPS_DUALPOINT 0x02 /* touchpad has trackstick */
78#define ALPS_PASS 0x04 /* device has a pass-through port */
79
80#define ALPS_WHEEL 0x08 /* hardware wheel present */
81#define ALPS_FW_BK_1 0x10 /* front & back buttons present */
82#define ALPS_FW_BK_2 0x20 /* front & back buttons present */
83#define ALPS_FOUR_BUTTONS 0x40 /* 4 direction button present */
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -080084#define ALPS_PS2_INTERLEAVED 0x80 /* 3-byte PS/2 packet interleaved with
85 6-byte ALPS packet */
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Helge Dellere38de672006-09-10 21:54:39 -040087static const struct alps_model_info alps_model_data[] = {
Seth Forshee25bded72011-11-07 19:53:36 -080088 { { 0x32, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT }, /* Toshiba Salellite Pro M10 */
89 { { 0x33, 0x02, 0x0a }, 0x00, ALPS_PROTO_V1, 0x88, 0xf8, 0 }, /* UMAX-530T */
90 { { 0x53, 0x02, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
91 { { 0x53, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
92 { { 0x60, 0x03, 0xc8 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 }, /* HP ze1115 */
93 { { 0x63, 0x02, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
94 { { 0x63, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
95 { { 0x63, 0x02, 0x28 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 }, /* Fujitsu Siemens S6010 */
96 { { 0x63, 0x02, 0x3c }, 0x00, ALPS_PROTO_V2, 0x8f, 0x8f, ALPS_WHEEL }, /* Toshiba Satellite S2400-103 */
97 { { 0x63, 0x02, 0x50 }, 0x00, ALPS_PROTO_V2, 0xef, 0xef, ALPS_FW_BK_1 }, /* NEC Versa L320 */
98 { { 0x63, 0x02, 0x64 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
99 { { 0x63, 0x03, 0xc8 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT }, /* Dell Latitude D800 */
100 { { 0x73, 0x00, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_DUALPOINT }, /* ThinkPad R61 8918-5QG */
101 { { 0x73, 0x02, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
102 { { 0x73, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 }, /* Ahtec Laptop */
103 { { 0x20, 0x02, 0x0e }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT }, /* XXX */
104 { { 0x22, 0x02, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT },
105 { { 0x22, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xff, 0xff, ALPS_PASS | ALPS_DUALPOINT }, /* Dell Latitude D600 */
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800106 /* Dell Latitude E5500, E6400, E6500, Precision M4400 */
Seth Forshee25bded72011-11-07 19:53:36 -0800107 { { 0x62, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xcf, 0xcf,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800108 ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED },
Seth Forshee25bded72011-11-07 19:53:36 -0800109 { { 0x73, 0x02, 0x50 }, 0x00, ALPS_PROTO_V2, 0xcf, 0xcf, ALPS_FOUR_BUTTONS }, /* Dell Vostro 1400 */
110 { { 0x52, 0x01, 0x14 }, 0x00, ALPS_PROTO_V2, 0xff, 0xff,
111 ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED }, /* Toshiba Tecra A11-11L */
112 { { 0x73, 0x02, 0x64 }, 0x9b, ALPS_PROTO_V3, 0x8f, 0x8f, ALPS_DUALPOINT },
113 { { 0x73, 0x02, 0x64 }, 0x9d, ALPS_PROTO_V3, 0x8f, 0x8f, ALPS_DUALPOINT },
114 { { 0x73, 0x02, 0x64 }, 0x8a, ALPS_PROTO_V4, 0x8f, 0x8f, 0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115};
116
117/*
118 * XXX - this entry is suspicious. First byte has zero lower nibble,
119 * which is what a normal mouse would report. Also, the value 0x0e
120 * isn't valid per PS/2 spec.
121 */
122
Seth Forsheed4b347b2011-11-07 19:53:15 -0800123/* Packet formats are described in Documentation/input/alps.txt */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800125static bool alps_is_valid_first_byte(const struct alps_model_info *model,
126 unsigned char data)
127{
128 return (data & model->mask0) == model->byte0;
129}
130
131static void alps_report_buttons(struct psmouse *psmouse,
132 struct input_dev *dev1, struct input_dev *dev2,
133 int left, int right, int middle)
134{
Martin Buckc91ed052010-03-13 22:23:58 -0800135 struct input_dev *dev;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800136
Martin Buckc91ed052010-03-13 22:23:58 -0800137 /*
138 * If shared button has already been reported on the
139 * other device (dev2) then this event should be also
140 * sent through that device.
141 */
142 dev = test_bit(BTN_LEFT, dev2->key) ? dev2 : dev1;
143 input_report_key(dev, BTN_LEFT, left);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800144
Martin Buckc91ed052010-03-13 22:23:58 -0800145 dev = test_bit(BTN_RIGHT, dev2->key) ? dev2 : dev1;
146 input_report_key(dev, BTN_RIGHT, right);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800147
Martin Buckc91ed052010-03-13 22:23:58 -0800148 dev = test_bit(BTN_MIDDLE, dev2->key) ? dev2 : dev1;
149 input_report_key(dev, BTN_MIDDLE, middle);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800150
Martin Buckc91ed052010-03-13 22:23:58 -0800151 /*
152 * Sync the _other_ device now, we'll do the first
153 * device later once we report the rest of the events.
154 */
155 input_sync(dev2);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800156}
157
Seth Forshee25bded72011-11-07 19:53:36 -0800158static void alps_process_packet_v1_v2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
160 struct alps_data *priv = psmouse->private;
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800161 const struct alps_model_info *model = priv->i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 unsigned char *packet = psmouse->packet;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500163 struct input_dev *dev = psmouse->dev;
164 struct input_dev *dev2 = priv->dev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 int x, y, z, ges, fin, left, right, middle;
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500166 int back = 0, forward = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Seth Forsheefa629ef2011-11-07 19:53:24 -0800168 if (model->proto_version == ALPS_PROTO_V1) {
Yotam Medinid2f40122006-05-29 23:30:36 -0400169 left = packet[2] & 0x10;
170 right = packet[2] & 0x08;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 middle = 0;
172 x = packet[1] | ((packet[0] & 0x07) << 7);
173 y = packet[4] | ((packet[3] & 0x07) << 7);
174 z = packet[5];
175 } else {
176 left = packet[3] & 1;
177 right = packet[3] & 2;
178 middle = packet[3] & 4;
179 x = packet[1] | ((packet[2] & 0x78) << (7 - 3));
180 y = packet[4] | ((packet[3] & 0x70) << (7 - 4));
181 z = packet[5];
182 }
183
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800184 if (model->flags & ALPS_FW_BK_1) {
Laszlo Kajan3c00bb92008-03-18 00:39:55 -0400185 back = packet[0] & 0x10;
186 forward = packet[2] & 4;
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500187 }
188
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800189 if (model->flags & ALPS_FW_BK_2) {
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500190 back = packet[3] & 4;
191 forward = packet[2] & 4;
192 if ((middle = forward && back))
193 forward = back = 0;
194 }
195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 ges = packet[2] & 1;
197 fin = packet[2] & 2;
198
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800199 if ((model->flags & ALPS_DUALPOINT) && z == 127) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 input_report_rel(dev2, REL_X, (x > 383 ? (x - 768) : x));
201 input_report_rel(dev2, REL_Y, -(y > 255 ? (y - 512) : y));
Ulrich Dangeld7ed5d82009-06-11 00:15:09 -0700202
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800203 alps_report_buttons(psmouse, dev2, dev, left, right, middle);
Ulrich Dangeld7ed5d82009-06-11 00:15:09 -0700204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 input_sync(dev2);
206 return;
207 }
208
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800209 alps_report_buttons(psmouse, dev, dev2, left, right, middle);
Ulrich Dangeld7ed5d82009-06-11 00:15:09 -0700210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 /* Convert hardware tap to a reasonable Z value */
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800212 if (ges && !fin)
213 z = 40;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
215 /*
216 * A "tap and drag" operation is reported by the hardware as a transition
217 * from (!fin && ges) to (fin && ges). This should be translated to the
218 * sequence Z>0, Z==0, Z>0, so the Z==0 event has to be generated manually.
219 */
220 if (ges && fin && !priv->prev_fin) {
221 input_report_abs(dev, ABS_X, x);
222 input_report_abs(dev, ABS_Y, y);
223 input_report_abs(dev, ABS_PRESSURE, 0);
224 input_report_key(dev, BTN_TOOL_FINGER, 0);
225 input_sync(dev);
226 }
227 priv->prev_fin = fin;
228
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800229 if (z > 30)
230 input_report_key(dev, BTN_TOUCH, 1);
231 if (z < 25)
232 input_report_key(dev, BTN_TOUCH, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 if (z > 0) {
235 input_report_abs(dev, ABS_X, x);
236 input_report_abs(dev, ABS_Y, y);
237 }
238
239 input_report_abs(dev, ABS_PRESSURE, z);
240 input_report_key(dev, BTN_TOOL_FINGER, z > 0);
241
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800242 if (model->flags & ALPS_WHEEL)
Vojtech Pavlike6c047b2005-09-04 01:40:43 -0500243 input_report_rel(dev, REL_WHEEL, ((packet[2] << 1) & 0x08) - ((packet[0] >> 4) & 0x07));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800245 if (model->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500246 input_report_key(dev, BTN_FORWARD, forward);
247 input_report_key(dev, BTN_BACK, back);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 }
249
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800250 if (model->flags & ALPS_FOUR_BUTTONS) {
251 input_report_key(dev, BTN_0, packet[2] & 4);
252 input_report_key(dev, BTN_1, packet[0] & 0x10);
253 input_report_key(dev, BTN_2, packet[3] & 4);
254 input_report_key(dev, BTN_3, packet[0] & 0x20);
255 }
256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 input_sync(dev);
258}
259
Seth Forshee01ce6612011-11-07 19:54:13 -0800260/*
261 * Process bitmap data from v3 and v4 protocols. Returns the number of
262 * fingers detected. A return value of 0 means at least one of the
263 * bitmaps was empty.
264 *
265 * The bitmaps don't have enough data to track fingers, so this function
266 * only generates points representing a bounding box of all contacts.
267 * These points are returned in x1, y1, x2, and y2 when the return value
268 * is greater than 0.
269 */
270static int alps_process_bitmap(unsigned int x_map, unsigned int y_map,
271 int *x1, int *y1, int *x2, int *y2)
272{
273 struct alps_bitmap_point {
274 int start_bit;
275 int num_bits;
276 };
277
278 int fingers_x = 0, fingers_y = 0, fingers;
279 int i, bit, prev_bit;
280 struct alps_bitmap_point x_low = {0,}, x_high = {0,};
281 struct alps_bitmap_point y_low = {0,}, y_high = {0,};
282 struct alps_bitmap_point *point;
283
284 if (!x_map || !y_map)
285 return 0;
286
287 *x1 = *y1 = *x2 = *y2 = 0;
288
289 prev_bit = 0;
290 point = &x_low;
291 for (i = 0; x_map != 0; i++, x_map >>= 1) {
292 bit = x_map & 1;
293 if (bit) {
294 if (!prev_bit) {
295 point->start_bit = i;
296 fingers_x++;
297 }
298 point->num_bits++;
299 } else {
300 if (prev_bit)
301 point = &x_high;
302 else
303 point->num_bits = 0;
304 }
305 prev_bit = bit;
306 }
307
308 /*
309 * y bitmap is reversed for what we need (lower positions are in
310 * higher bits), so we process from the top end.
311 */
312 y_map = y_map << (sizeof(y_map) * BITS_PER_BYTE - ALPS_BITMAP_Y_BITS);
313 prev_bit = 0;
314 point = &y_low;
315 for (i = 0; y_map != 0; i++, y_map <<= 1) {
316 bit = y_map & (1 << (sizeof(y_map) * BITS_PER_BYTE - 1));
317 if (bit) {
318 if (!prev_bit) {
319 point->start_bit = i;
320 fingers_y++;
321 }
322 point->num_bits++;
323 } else {
324 if (prev_bit)
325 point = &y_high;
326 else
327 point->num_bits = 0;
328 }
329 prev_bit = bit;
330 }
331
332 /*
333 * Fingers can overlap, so we use the maximum count of fingers
334 * on either axis as the finger count.
335 */
336 fingers = max(fingers_x, fingers_y);
337
338 /*
339 * If total fingers is > 1 but either axis reports only a single
340 * contact, we have overlapping or adjacent fingers. For the
341 * purposes of creating a bounding box, divide the single contact
342 * (roughly) equally between the two points.
343 */
344 if (fingers > 1) {
345 if (fingers_x == 1) {
346 i = x_low.num_bits / 2;
347 x_low.num_bits = x_low.num_bits - i;
348 x_high.start_bit = x_low.start_bit + i;
349 x_high.num_bits = max(i, 1);
350 } else if (fingers_y == 1) {
351 i = y_low.num_bits / 2;
352 y_low.num_bits = y_low.num_bits - i;
353 y_high.start_bit = y_low.start_bit + i;
354 y_high.num_bits = max(i, 1);
355 }
356 }
357
358 *x1 = (ALPS_V3_X_MAX * (2 * x_low.start_bit + x_low.num_bits - 1)) /
359 (2 * (ALPS_BITMAP_X_BITS - 1));
360 *y1 = (ALPS_V3_Y_MAX * (2 * y_low.start_bit + y_low.num_bits - 1)) /
361 (2 * (ALPS_BITMAP_Y_BITS - 1));
362
363 if (fingers > 1) {
364 *x2 = (ALPS_V3_X_MAX * (2 * x_high.start_bit + x_high.num_bits - 1)) /
365 (2 * (ALPS_BITMAP_X_BITS - 1));
366 *y2 = (ALPS_V3_Y_MAX * (2 * y_high.start_bit + y_high.num_bits - 1)) /
367 (2 * (ALPS_BITMAP_Y_BITS - 1));
368 }
369
370 return fingers;
371}
372
373static void alps_set_slot(struct input_dev *dev, int slot, bool active,
374 int x, int y)
375{
376 input_mt_slot(dev, slot);
377 input_mt_report_slot_state(dev, MT_TOOL_FINGER, active);
378 if (active) {
379 input_report_abs(dev, ABS_MT_POSITION_X, x);
380 input_report_abs(dev, ABS_MT_POSITION_Y, y);
381 }
382}
383
384static void alps_report_semi_mt_data(struct input_dev *dev, int num_fingers,
385 int x1, int y1, int x2, int y2)
386{
387 alps_set_slot(dev, 0, num_fingers != 0, x1, y1);
388 alps_set_slot(dev, 1, num_fingers == 2, x2, y2);
389}
390
Seth Forshee25bded72011-11-07 19:53:36 -0800391static void alps_process_trackstick_packet_v3(struct psmouse *psmouse)
392{
393 struct alps_data *priv = psmouse->private;
394 unsigned char *packet = psmouse->packet;
395 struct input_dev *dev = priv->dev2;
396 int x, y, z, left, right, middle;
397
398 /* Sanity check packet */
399 if (!(packet[0] & 0x40)) {
400 psmouse_dbg(psmouse, "Bad trackstick packet, discarding\n");
401 return;
402 }
403
404 /*
405 * There's a special packet that seems to indicate the end
406 * of a stream of trackstick data. Filter these out.
407 */
408 if (packet[1] == 0x7f && packet[2] == 0x7f && packet[4] == 0x7f)
409 return;
410
411 x = (s8)(((packet[0] & 0x20) << 2) | (packet[1] & 0x7f));
412 y = (s8)(((packet[0] & 0x10) << 3) | (packet[2] & 0x7f));
413 z = (packet[4] & 0x7c) >> 2;
414
415 /*
416 * The x and y values tend to be quite large, and when used
417 * alone the trackstick is difficult to use. Scale them down
418 * to compensate.
419 */
420 x /= 8;
421 y /= 8;
422
423 input_report_rel(dev, REL_X, x);
424 input_report_rel(dev, REL_Y, -y);
425
426 /*
427 * Most ALPS models report the trackstick buttons in the touchpad
428 * packets, but a few report them here. No reliable way has been
429 * found to differentiate between the models upfront, so we enable
430 * the quirk in response to seeing a button press in the trackstick
431 * packet.
432 */
433 left = packet[3] & 0x01;
434 right = packet[3] & 0x02;
435 middle = packet[3] & 0x04;
436
437 if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) &&
438 (left || right || middle))
439 priv->quirks |= ALPS_QUIRK_TRACKSTICK_BUTTONS;
440
441 if (priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) {
442 input_report_key(dev, BTN_LEFT, left);
443 input_report_key(dev, BTN_RIGHT, right);
444 input_report_key(dev, BTN_MIDDLE, middle);
445 }
446
447 input_sync(dev);
448 return;
449}
450
451static void alps_process_touchpad_packet_v3(struct psmouse *psmouse)
452{
453 struct alps_data *priv = psmouse->private;
454 unsigned char *packet = psmouse->packet;
455 struct input_dev *dev = psmouse->dev;
456 struct input_dev *dev2 = priv->dev2;
457 int x, y, z;
458 int left, right, middle;
Seth Forshee01ce6612011-11-07 19:54:13 -0800459 int x1 = 0, y1 = 0, x2 = 0, y2 = 0;
460 int fingers = 0, bmap_fingers;
461 unsigned int x_bitmap, y_bitmap;
Seth Forshee25bded72011-11-07 19:53:36 -0800462
463 /*
Seth Forshee01ce6612011-11-07 19:54:13 -0800464 * There's no single feature of touchpad position and bitmap packets
465 * that can be used to distinguish between them. We rely on the fact
466 * that a bitmap packet should always follow a position packet with
467 * bit 6 of packet[4] set.
Seth Forshee25bded72011-11-07 19:53:36 -0800468 */
469 if (priv->multi_packet) {
Seth Forshee25bded72011-11-07 19:53:36 -0800470 /*
471 * Sometimes a position packet will indicate a multi-packet
472 * sequence, but then what follows is another position
473 * packet. Check for this, and when it happens process the
474 * position packet as usual.
475 */
476 if (packet[0] & 0x40) {
Seth Forshee01ce6612011-11-07 19:54:13 -0800477 fingers = (packet[5] & 0x3) + 1;
478 x_bitmap = ((packet[4] & 0x7e) << 8) |
479 ((packet[1] & 0x7f) << 2) |
480 ((packet[0] & 0x30) >> 4);
481 y_bitmap = ((packet[3] & 0x70) << 4) |
482 ((packet[2] & 0x7f) << 1) |
483 (packet[4] & 0x01);
484
485 bmap_fingers = alps_process_bitmap(x_bitmap, y_bitmap,
486 &x1, &y1, &x2, &y2);
487
Seth Forshee25bded72011-11-07 19:53:36 -0800488 /*
Seth Forshee01ce6612011-11-07 19:54:13 -0800489 * We shouldn't report more than one finger if
490 * we don't have two coordinates.
Seth Forshee25bded72011-11-07 19:53:36 -0800491 */
Seth Forshee01ce6612011-11-07 19:54:13 -0800492 if (fingers > 1 && bmap_fingers < 2)
493 fingers = bmap_fingers;
494
495 /* Now process position packet */
496 packet = priv->multi_data;
497 } else {
498 priv->multi_packet = 0;
Seth Forshee25bded72011-11-07 19:53:36 -0800499 }
500 }
501
Seth Forshee01ce6612011-11-07 19:54:13 -0800502 /*
503 * Bit 6 of byte 0 is not usually set in position packets. The only
504 * times it seems to be set is in situations where the data is
505 * suspect anyway, e.g. a palm resting flat on the touchpad. Given
506 * this combined with the fact that this bit is useful for filtering
507 * out misidentified bitmap packets, we reject anything with this
508 * bit set.
509 */
510 if (packet[0] & 0x40)
511 return;
512
513 if (!priv->multi_packet && (packet[4] & 0x40)) {
Seth Forshee25bded72011-11-07 19:53:36 -0800514 priv->multi_packet = 1;
Seth Forshee01ce6612011-11-07 19:54:13 -0800515 memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
516 return;
517 }
518
519 priv->multi_packet = 0;
Seth Forshee25bded72011-11-07 19:53:36 -0800520
521 left = packet[3] & 0x01;
522 right = packet[3] & 0x02;
523 middle = packet[3] & 0x04;
524
525 x = ((packet[1] & 0x7f) << 4) | ((packet[4] & 0x30) >> 2) |
526 ((packet[0] & 0x30) >> 4);
527 y = ((packet[2] & 0x7f) << 4) | (packet[4] & 0x0f);
528 z = packet[5] & 0x7f;
529
530 /*
531 * Sometimes the hardware sends a single packet with z = 0
532 * in the middle of a stream. Real releases generate packets
533 * with x, y, and z all zero, so these seem to be flukes.
534 * Ignore them.
535 */
536 if (x && y && !z)
537 return;
538
Seth Forshee01ce6612011-11-07 19:54:13 -0800539 /*
540 * If we don't have MT data or the bitmaps were empty, we have
541 * to rely on ST data.
542 */
543 if (!fingers) {
544 x1 = x;
545 y1 = y;
546 fingers = z > 0 ? 1 : 0;
547 }
548
Seth Forshee25bded72011-11-07 19:53:36 -0800549 if (z >= 64)
550 input_report_key(dev, BTN_TOUCH, 1);
551 else
552 input_report_key(dev, BTN_TOUCH, 0);
553
Seth Forshee01ce6612011-11-07 19:54:13 -0800554 alps_report_semi_mt_data(dev, fingers, x1, y1, x2, y2);
555
556 input_report_key(dev, BTN_TOOL_FINGER, fingers == 1);
557 input_report_key(dev, BTN_TOOL_DOUBLETAP, fingers == 2);
558 input_report_key(dev, BTN_TOOL_TRIPLETAP, fingers == 3);
559 input_report_key(dev, BTN_TOOL_QUADTAP, fingers == 4);
560
561 input_report_key(dev, BTN_LEFT, left);
562 input_report_key(dev, BTN_RIGHT, right);
563 input_report_key(dev, BTN_MIDDLE, middle);
564
Seth Forshee25bded72011-11-07 19:53:36 -0800565 if (z > 0) {
566 input_report_abs(dev, ABS_X, x);
567 input_report_abs(dev, ABS_Y, y);
568 }
569 input_report_abs(dev, ABS_PRESSURE, z);
570
Seth Forshee25bded72011-11-07 19:53:36 -0800571 input_sync(dev);
572
573 if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS)) {
574 left = packet[3] & 0x10;
575 right = packet[3] & 0x20;
576 middle = packet[3] & 0x40;
577
578 input_report_key(dev2, BTN_LEFT, left);
579 input_report_key(dev2, BTN_RIGHT, right);
580 input_report_key(dev2, BTN_MIDDLE, middle);
581 input_sync(dev2);
582 }
583}
584
585static void alps_process_packet_v3(struct psmouse *psmouse)
586{
587 unsigned char *packet = psmouse->packet;
588
589 /*
590 * v3 protocol packets come in three types, two representing
591 * touchpad data and one representing trackstick data.
592 * Trackstick packets seem to be distinguished by always
593 * having 0x3f in the last byte. This value has never been
594 * observed in the last byte of either of the other types
595 * of packets.
596 */
597 if (packet[5] == 0x3f) {
598 alps_process_trackstick_packet_v3(psmouse);
599 return;
600 }
601
602 alps_process_touchpad_packet_v3(psmouse);
603}
604
605static void alps_process_packet_v4(struct psmouse *psmouse)
606{
George Pantalos3b7e09f2012-05-10 22:31:59 -0700607 struct alps_data *priv = psmouse->private;
Seth Forshee25bded72011-11-07 19:53:36 -0800608 unsigned char *packet = psmouse->packet;
609 struct input_dev *dev = psmouse->dev;
George Pantalos3b7e09f2012-05-10 22:31:59 -0700610 int offset;
Seth Forshee25bded72011-11-07 19:53:36 -0800611 int x, y, z;
612 int left, right;
George Pantalos3b7e09f2012-05-10 22:31:59 -0700613 int x1, y1, x2, y2;
614 int fingers = 0;
615 unsigned int x_bitmap, y_bitmap;
616
617 /*
618 * v4 has a 6-byte encoding for bitmap data, but this data is
619 * broken up between 3 normal packets. Use priv->multi_packet to
620 * track our position in the bitmap packet.
621 */
622 if (packet[6] & 0x40) {
623 /* sync, reset position */
624 priv->multi_packet = 0;
625 }
626
627 if (WARN_ON_ONCE(priv->multi_packet > 2))
628 return;
629
630 offset = 2 * priv->multi_packet;
631 priv->multi_data[offset] = packet[6];
632 priv->multi_data[offset + 1] = packet[7];
633
634 if (++priv->multi_packet > 2) {
635 priv->multi_packet = 0;
636
637 x_bitmap = ((priv->multi_data[2] & 0x1f) << 10) |
638 ((priv->multi_data[3] & 0x60) << 3) |
639 ((priv->multi_data[0] & 0x3f) << 2) |
640 ((priv->multi_data[1] & 0x60) >> 5);
641 y_bitmap = ((priv->multi_data[5] & 0x01) << 10) |
642 ((priv->multi_data[3] & 0x1f) << 5) |
643 (priv->multi_data[1] & 0x1f);
644
645 fingers = alps_process_bitmap(x_bitmap, y_bitmap,
646 &x1, &y1, &x2, &y2);
647
648 /* Store MT data.*/
649 priv->fingers = fingers;
650 priv->x1 = x1;
651 priv->x2 = x2;
652 priv->y1 = y1;
653 priv->y2 = y2;
654 }
Seth Forshee25bded72011-11-07 19:53:36 -0800655
656 left = packet[4] & 0x01;
657 right = packet[4] & 0x02;
658
659 x = ((packet[1] & 0x7f) << 4) | ((packet[3] & 0x30) >> 2) |
660 ((packet[0] & 0x30) >> 4);
661 y = ((packet[2] & 0x7f) << 4) | (packet[3] & 0x0f);
662 z = packet[5] & 0x7f;
663
George Pantalos3b7e09f2012-05-10 22:31:59 -0700664 /*
665 * If there were no contacts in the bitmap, use ST
666 * points in MT reports.
667 * If there were two contacts or more, report MT data.
668 */
669 if (priv->fingers < 2) {
670 x1 = x;
671 y1 = y;
672 fingers = z > 0 ? 1 : 0;
673 } else {
674 fingers = priv->fingers;
675 x1 = priv->x1;
676 x2 = priv->x2;
677 y1 = priv->y1;
678 y2 = priv->y2;
679 }
680
Seth Forshee25bded72011-11-07 19:53:36 -0800681 if (z >= 64)
682 input_report_key(dev, BTN_TOUCH, 1);
683 else
684 input_report_key(dev, BTN_TOUCH, 0);
685
George Pantalos3b7e09f2012-05-10 22:31:59 -0700686 alps_report_semi_mt_data(dev, fingers, x1, y1, x2, y2);
687
688 input_report_key(dev, BTN_TOOL_FINGER, fingers == 1);
689 input_report_key(dev, BTN_TOOL_DOUBLETAP, fingers == 2);
690 input_report_key(dev, BTN_TOOL_TRIPLETAP, fingers == 3);
691 input_report_key(dev, BTN_TOOL_QUADTAP, fingers == 4);
692
693 input_report_key(dev, BTN_LEFT, left);
694 input_report_key(dev, BTN_RIGHT, right);
695
Seth Forshee25bded72011-11-07 19:53:36 -0800696 if (z > 0) {
697 input_report_abs(dev, ABS_X, x);
698 input_report_abs(dev, ABS_Y, y);
699 }
700 input_report_abs(dev, ABS_PRESSURE, z);
701
Seth Forshee25bded72011-11-07 19:53:36 -0800702 input_sync(dev);
703}
704
705static void alps_process_packet(struct psmouse *psmouse)
706{
707 struct alps_data *priv = psmouse->private;
708 const struct alps_model_info *model = priv->i;
709
710 switch (model->proto_version) {
711 case ALPS_PROTO_V1:
712 case ALPS_PROTO_V2:
713 alps_process_packet_v1_v2(psmouse);
714 break;
715 case ALPS_PROTO_V3:
716 alps_process_packet_v3(psmouse);
717 break;
718 case ALPS_PROTO_V4:
719 alps_process_packet_v4(psmouse);
720 break;
721 }
722}
723
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800724static void alps_report_bare_ps2_packet(struct psmouse *psmouse,
725 unsigned char packet[],
726 bool report_buttons)
727{
728 struct alps_data *priv = psmouse->private;
729 struct input_dev *dev2 = priv->dev2;
730
731 if (report_buttons)
732 alps_report_buttons(psmouse, dev2, psmouse->dev,
733 packet[0] & 1, packet[0] & 2, packet[0] & 4);
734
735 input_report_rel(dev2, REL_X,
736 packet[1] ? packet[1] - ((packet[0] << 4) & 0x100) : 0);
737 input_report_rel(dev2, REL_Y,
738 packet[2] ? ((packet[0] << 3) & 0x100) - packet[2] : 0);
739
740 input_sync(dev2);
741}
742
743static psmouse_ret_t alps_handle_interleaved_ps2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744{
745 struct alps_data *priv = psmouse->private;
746
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800747 if (psmouse->pktcnt < 6)
748 return PSMOUSE_GOOD_DATA;
749
750 if (psmouse->pktcnt == 6) {
751 /*
752 * Start a timer to flush the packet if it ends up last
753 * 6-byte packet in the stream. Timer needs to fire
754 * psmouse core times out itself. 20 ms should be enough
755 * to decide if we are getting more data or not.
756 */
757 mod_timer(&priv->timer, jiffies + msecs_to_jiffies(20));
758 return PSMOUSE_GOOD_DATA;
759 }
760
761 del_timer(&priv->timer);
762
763 if (psmouse->packet[6] & 0x80) {
764
765 /*
766 * Highest bit is set - that means we either had
767 * complete ALPS packet and this is start of the
768 * next packet or we got garbage.
769 */
770
771 if (((psmouse->packet[3] |
772 psmouse->packet[4] |
773 psmouse->packet[5]) & 0x80) ||
774 (!alps_is_valid_first_byte(priv->i, psmouse->packet[6]))) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700775 psmouse_dbg(psmouse,
776 "refusing packet %x %x %x %x (suspected interleaved ps/2)\n",
777 psmouse->packet[3], psmouse->packet[4],
778 psmouse->packet[5], psmouse->packet[6]);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800779 return PSMOUSE_BAD_DATA;
780 }
781
782 alps_process_packet(psmouse);
783
784 /* Continue with the next packet */
785 psmouse->packet[0] = psmouse->packet[6];
786 psmouse->pktcnt = 1;
787
788 } else {
789
790 /*
791 * High bit is 0 - that means that we indeed got a PS/2
792 * packet in the middle of ALPS packet.
793 *
794 * There is also possibility that we got 6-byte ALPS
795 * packet followed by 3-byte packet from trackpoint. We
796 * can not distinguish between these 2 scenarios but
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700797 * because the latter is unlikely to happen in course of
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800798 * normal operation (user would need to press all
799 * buttons on the pad and start moving trackpoint
800 * without touching the pad surface) we assume former.
801 * Even if we are wrong the wost thing that would happen
802 * the cursor would jump but we should not get protocol
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700803 * de-synchronization.
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800804 */
805
806 alps_report_bare_ps2_packet(psmouse, &psmouse->packet[3],
807 false);
808
809 /*
810 * Continue with the standard ALPS protocol handling,
811 * but make sure we won't process it as an interleaved
812 * packet again, which may happen if all buttons are
813 * pressed. To avoid this let's reset the 4th bit which
814 * is normally 1.
815 */
816 psmouse->packet[3] = psmouse->packet[6] & 0xf7;
817 psmouse->pktcnt = 4;
818 }
819
820 return PSMOUSE_GOOD_DATA;
821}
822
823static void alps_flush_packet(unsigned long data)
824{
825 struct psmouse *psmouse = (struct psmouse *)data;
826
827 serio_pause_rx(psmouse->ps2dev.serio);
828
Seth Forsheeb46615f2011-11-07 19:53:30 -0800829 if (psmouse->pktcnt == psmouse->pktsize) {
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800830
831 /*
832 * We did not any more data in reasonable amount of time.
833 * Validate the last 3 bytes and process as a standard
834 * ALPS packet.
835 */
836 if ((psmouse->packet[3] |
837 psmouse->packet[4] |
838 psmouse->packet[5]) & 0x80) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700839 psmouse_dbg(psmouse,
840 "refusing packet %x %x %x (suspected interleaved ps/2)\n",
841 psmouse->packet[3], psmouse->packet[4],
842 psmouse->packet[5]);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800843 } else {
844 alps_process_packet(psmouse);
845 }
846 psmouse->pktcnt = 0;
847 }
848
849 serio_continue_rx(psmouse->ps2dev.serio);
850}
851
852static psmouse_ret_t alps_process_byte(struct psmouse *psmouse)
853{
854 struct alps_data *priv = psmouse->private;
855 const struct alps_model_info *model = priv->i;
856
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 if ((psmouse->packet[0] & 0xc8) == 0x08) { /* PS/2 packet */
858 if (psmouse->pktcnt == 3) {
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800859 alps_report_bare_ps2_packet(psmouse, psmouse->packet,
860 true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 return PSMOUSE_FULL_PACKET;
862 }
863 return PSMOUSE_GOOD_DATA;
864 }
865
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800866 /* Check for PS/2 packet stuffed in the middle of ALPS packet. */
867
868 if ((model->flags & ALPS_PS2_INTERLEAVED) &&
869 psmouse->pktcnt >= 4 && (psmouse->packet[3] & 0x0f) == 0x0f) {
870 return alps_handle_interleaved_ps2(psmouse);
871 }
872
873 if (!alps_is_valid_first_byte(model, psmouse->packet[0])) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700874 psmouse_dbg(psmouse,
875 "refusing packet[0] = %x (mask0 = %x, byte0 = %x)\n",
876 psmouse->packet[0], model->mask0, model->byte0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 return PSMOUSE_BAD_DATA;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800878 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
Seth Forsheeb46615f2011-11-07 19:53:30 -0800880 /* Bytes 2 - pktsize should have 0 in the highest bit */
881 if (psmouse->pktcnt >= 2 && psmouse->pktcnt <= psmouse->pktsize &&
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800882 (psmouse->packet[psmouse->pktcnt - 1] & 0x80)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700883 psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
884 psmouse->pktcnt - 1,
885 psmouse->packet[psmouse->pktcnt - 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 return PSMOUSE_BAD_DATA;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800887 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Seth Forsheeb46615f2011-11-07 19:53:30 -0800889 if (psmouse->pktcnt == psmouse->pktsize) {
David Howells7d12e782006-10-05 14:55:46 +0100890 alps_process_packet(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 return PSMOUSE_FULL_PACKET;
892 }
893
894 return PSMOUSE_GOOD_DATA;
895}
896
Seth Forshee25bded72011-11-07 19:53:36 -0800897static int alps_command_mode_send_nibble(struct psmouse *psmouse, int nibble)
898{
899 struct ps2dev *ps2dev = &psmouse->ps2dev;
900 struct alps_data *priv = psmouse->private;
901 int command;
902 unsigned char *param;
903 unsigned char dummy[4];
904
905 BUG_ON(nibble > 0xf);
906
907 command = priv->nibble_commands[nibble].command;
908 param = (command & 0x0f00) ?
909 dummy : (unsigned char *)&priv->nibble_commands[nibble].data;
910
911 if (ps2_command(ps2dev, param, command))
912 return -1;
913
914 return 0;
915}
916
917static int alps_command_mode_set_addr(struct psmouse *psmouse, int addr)
918{
919 struct ps2dev *ps2dev = &psmouse->ps2dev;
920 struct alps_data *priv = psmouse->private;
921 int i, nibble;
922
923 if (ps2_command(ps2dev, NULL, priv->addr_command))
924 return -1;
925
926 for (i = 12; i >= 0; i -= 4) {
927 nibble = (addr >> i) & 0xf;
928 if (alps_command_mode_send_nibble(psmouse, nibble))
929 return -1;
930 }
931
932 return 0;
933}
934
935static int __alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
936{
937 struct ps2dev *ps2dev = &psmouse->ps2dev;
938 unsigned char param[4];
939
940 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
941 return -1;
942
943 /*
944 * The address being read is returned in the first two bytes
945 * of the result. Check that this address matches the expected
946 * address.
947 */
948 if (addr != ((param[0] << 8) | param[1]))
949 return -1;
950
951 return param[2];
952}
953
954static int alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
955{
956 if (alps_command_mode_set_addr(psmouse, addr))
957 return -1;
958 return __alps_command_mode_read_reg(psmouse, addr);
959}
960
961static int __alps_command_mode_write_reg(struct psmouse *psmouse, u8 value)
962{
963 if (alps_command_mode_send_nibble(psmouse, (value >> 4) & 0xf))
964 return -1;
965 if (alps_command_mode_send_nibble(psmouse, value & 0xf))
966 return -1;
967 return 0;
968}
969
970static int alps_command_mode_write_reg(struct psmouse *psmouse, int addr,
971 u8 value)
972{
973 if (alps_command_mode_set_addr(psmouse, addr))
974 return -1;
975 return __alps_command_mode_write_reg(psmouse, value);
976}
977
978static int alps_enter_command_mode(struct psmouse *psmouse,
979 unsigned char *resp)
980{
981 unsigned char param[4];
982 struct ps2dev *ps2dev = &psmouse->ps2dev;
983
984 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
985 ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
986 ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
987 ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO)) {
988 psmouse_err(psmouse, "failed to enter command mode\n");
989 return -1;
990 }
991
992 if (param[0] != 0x88 && param[1] != 0x07) {
993 psmouse_dbg(psmouse,
994 "unknown response while entering command mode: %2.2x %2.2x %2.2x\n",
995 param[0], param[1], param[2]);
996 return -1;
997 }
998
999 if (resp)
1000 *resp = param[2];
1001 return 0;
1002}
1003
1004static inline int alps_exit_command_mode(struct psmouse *psmouse)
1005{
1006 struct ps2dev *ps2dev = &psmouse->ps2dev;
1007 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM))
1008 return -1;
1009 return 0;
1010}
1011
Helge Dellere38de672006-09-10 21:54:39 -04001012static const struct alps_model_info *alps_get_model(struct psmouse *psmouse, int *version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013{
1014 struct ps2dev *ps2dev = &psmouse->ps2dev;
Helge Dellere38de672006-09-10 21:54:39 -04001015 static const unsigned char rates[] = { 0, 10, 20, 40, 60, 80, 100, 200 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 unsigned char param[4];
Seth Forshee25bded72011-11-07 19:53:36 -08001017 const struct alps_model_info *model = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 int i;
1019
1020 /*
1021 * First try "E6 report".
Akio Idehara99c90ab2012-02-24 00:33:22 -08001022 * ALPS should return 0,0,10 or 0,0,100 if no buttons are pressed.
1023 * The bits 0-2 of the first byte will be 1s if some buttons are
1024 * pressed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 */
1026 param[0] = 0;
1027 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES) ||
1028 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1029 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1030 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11))
1031 return NULL;
1032
1033 param[0] = param[1] = param[2] = 0xff;
1034 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1035 return NULL;
1036
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001037 psmouse_dbg(psmouse, "E6 report: %2.2x %2.2x %2.2x",
1038 param[0], param[1], param[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
Akio Idehara99c90ab2012-02-24 00:33:22 -08001040 if ((param[0] & 0xf8) != 0 || param[1] != 0 ||
1041 (param[2] != 10 && param[2] != 100))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 return NULL;
1043
1044 /*
1045 * Now try "E7 report". Allowed responses are in
1046 * alps_model_data[].signature
1047 */
1048 param[0] = 0;
1049 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES) ||
1050 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1051 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1052 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21))
1053 return NULL;
1054
1055 param[0] = param[1] = param[2] = 0xff;
1056 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1057 return NULL;
1058
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001059 psmouse_dbg(psmouse, "E7 report: %2.2x %2.2x %2.2x",
1060 param[0], param[1], param[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04001062 if (version) {
1063 for (i = 0; i < ARRAY_SIZE(rates) && param[2] != rates[i]; i++)
1064 /* empty */;
1065 *version = (param[0] << 8) | (param[1] << 4) | i;
1066 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
Seth Forshee25bded72011-11-07 19:53:36 -08001068 for (i = 0; i < ARRAY_SIZE(alps_model_data); i++) {
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04001069 if (!memcmp(param, alps_model_data[i].signature,
Seth Forshee25bded72011-11-07 19:53:36 -08001070 sizeof(alps_model_data[i].signature))) {
1071 model = alps_model_data + i;
1072 break;
1073 }
1074 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
Seth Forshee25bded72011-11-07 19:53:36 -08001076 if (model && model->proto_version > ALPS_PROTO_V2) {
1077 /*
1078 * Need to check command mode response to identify
1079 * model
1080 */
1081 model = NULL;
1082 if (alps_enter_command_mode(psmouse, param)) {
1083 psmouse_warn(psmouse,
1084 "touchpad failed to enter command mode\n");
1085 } else {
1086 for (i = 0; i < ARRAY_SIZE(alps_model_data); i++) {
1087 if (alps_model_data[i].proto_version > ALPS_PROTO_V2 &&
1088 alps_model_data[i].command_mode_resp == param[0]) {
1089 model = alps_model_data + i;
1090 break;
1091 }
1092 }
1093 alps_exit_command_mode(psmouse);
1094
1095 if (!model)
1096 psmouse_dbg(psmouse,
1097 "Unknown command mode response %2.2x\n",
1098 param[0]);
1099 }
1100 }
1101
1102 return model;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103}
1104
1105/*
1106 * For DualPoint devices select the device that should respond to
1107 * subsequent commands. It looks like glidepad is behind stickpointer,
1108 * I'd thought it would be other way around...
1109 */
Seth Forshee25bded72011-11-07 19:53:36 -08001110static int alps_passthrough_mode_v2(struct psmouse *psmouse, bool enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111{
1112 struct ps2dev *ps2dev = &psmouse->ps2dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 int cmd = enable ? PSMOUSE_CMD_SETSCALE21 : PSMOUSE_CMD_SETSCALE11;
1114
1115 if (ps2_command(ps2dev, NULL, cmd) ||
1116 ps2_command(ps2dev, NULL, cmd) ||
1117 ps2_command(ps2dev, NULL, cmd) ||
1118 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1119 return -1;
1120
1121 /* we may get 3 more bytes, just ignore them */
Dmitry Torokhovc6117632005-06-01 02:39:51 -05001122 ps2_drain(ps2dev, 3, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
1124 return 0;
1125}
1126
Seth Forshee25bded72011-11-07 19:53:36 -08001127static int alps_absolute_mode_v1_v2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128{
1129 struct ps2dev *ps2dev = &psmouse->ps2dev;
1130
1131 /* Try ALPS magic knock - 4 disable before enable */
1132 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1133 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1134 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1135 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1136 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
1137 return -1;
1138
1139 /*
1140 * Switch mouse to poll (remote) mode so motion data will not
1141 * get in our way
1142 */
1143 return ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETPOLL);
1144}
1145
1146static int alps_get_status(struct psmouse *psmouse, char *param)
1147{
1148 struct ps2dev *ps2dev = &psmouse->ps2dev;
1149
1150 /* Get status: 0xF5 0xF5 0xF5 0xE9 */
1151 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1152 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1153 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1154 ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1155 return -1;
1156
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001157 psmouse_dbg(psmouse, "Status: %2.2x %2.2x %2.2x",
1158 param[0], param[1], param[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
1160 return 0;
1161}
1162
1163/*
1164 * Turn touchpad tapping on or off. The sequences are:
1165 * 0xE9 0xF5 0xF5 0xF3 0x0A to enable,
1166 * 0xE9 0xF5 0xF5 0xE8 0x00 to disable.
1167 * My guess that 0xE9 (GetInfo) is here as a sync point.
1168 * For models that also have stickpointer (DualPoints) its tapping
1169 * is controlled separately (0xE6 0xE6 0xE6 0xF3 0x14|0x0A) but
1170 * we don't fiddle with it.
1171 */
1172static int alps_tap_mode(struct psmouse *psmouse, int enable)
1173{
1174 struct ps2dev *ps2dev = &psmouse->ps2dev;
1175 int cmd = enable ? PSMOUSE_CMD_SETRATE : PSMOUSE_CMD_SETRES;
1176 unsigned char tap_arg = enable ? 0x0A : 0x00;
1177 unsigned char param[4];
1178
1179 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO) ||
1180 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1181 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1182 ps2_command(ps2dev, &tap_arg, cmd))
1183 return -1;
1184
1185 if (alps_get_status(psmouse, param))
1186 return -1;
1187
1188 return 0;
1189}
1190
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001191/*
1192 * alps_poll() - poll the touchpad for current motion packet.
1193 * Used in resync.
1194 */
1195static int alps_poll(struct psmouse *psmouse)
1196{
1197 struct alps_data *priv = psmouse->private;
Seth Forsheeb46615f2011-11-07 19:53:30 -08001198 unsigned char buf[sizeof(psmouse->packet)];
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001199 bool poll_failed;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001200
1201 if (priv->i->flags & ALPS_PASS)
Seth Forshee25bded72011-11-07 19:53:36 -08001202 alps_passthrough_mode_v2(psmouse, true);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001203
1204 poll_failed = ps2_command(&psmouse->ps2dev, buf,
1205 PSMOUSE_CMD_POLL | (psmouse->pktsize << 8)) < 0;
1206
1207 if (priv->i->flags & ALPS_PASS)
Seth Forshee25bded72011-11-07 19:53:36 -08001208 alps_passthrough_mode_v2(psmouse, false);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001209
1210 if (poll_failed || (buf[0] & priv->i->mask0) != priv->i->byte0)
1211 return -1;
1212
1213 if ((psmouse->badbyte & 0xc8) == 0x08) {
1214/*
1215 * Poll the track stick ...
1216 */
1217 if (ps2_command(&psmouse->ps2dev, buf, PSMOUSE_CMD_POLL | (3 << 8)))
1218 return -1;
1219 }
1220
1221 memcpy(psmouse->packet, buf, sizeof(buf));
1222 return 0;
1223}
1224
Seth Forshee25bded72011-11-07 19:53:36 -08001225static int alps_hw_init_v1_v2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226{
1227 struct alps_data *priv = psmouse->private;
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08001228 const struct alps_model_info *model = priv->i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08001230 if ((model->flags & ALPS_PASS) &&
Seth Forshee25bded72011-11-07 19:53:36 -08001231 alps_passthrough_mode_v2(psmouse, true)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 return -1;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001235 if (alps_tap_mode(psmouse, true)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001236 psmouse_warn(psmouse, "Failed to enable hardware tapping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 return -1;
Peter Osterlund963f6262005-07-11 01:08:04 -05001238 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
Seth Forshee25bded72011-11-07 19:53:36 -08001240 if (alps_absolute_mode_v1_v2(psmouse)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001241 psmouse_err(psmouse, "Failed to enable absolute mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 return -1;
1243 }
1244
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08001245 if ((model->flags & ALPS_PASS) &&
Seth Forshee25bded72011-11-07 19:53:36 -08001246 alps_passthrough_mode_v2(psmouse, false)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 return -1;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04001250 /* ALPS needs stream mode, otherwise it won't report any data */
1251 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001252 psmouse_err(psmouse, "Failed to enable stream mode\n");
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04001253 return -1;
1254 }
1255
1256 return 0;
1257}
1258
Seth Forshee25bded72011-11-07 19:53:36 -08001259/*
1260 * Enable or disable passthrough mode to the trackstick. Must be in
1261 * command mode when calling this function.
1262 */
1263static int alps_passthrough_mode_v3(struct psmouse *psmouse, bool enable)
1264{
1265 int reg_val;
1266
1267 reg_val = alps_command_mode_read_reg(psmouse, 0x0008);
1268 if (reg_val == -1)
1269 return -1;
1270
1271 if (enable)
1272 reg_val |= 0x01;
1273 else
1274 reg_val &= ~0x01;
1275
1276 if (__alps_command_mode_write_reg(psmouse, reg_val))
1277 return -1;
1278
1279 return 0;
1280}
1281
1282/* Must be in command mode when calling this function */
1283static int alps_absolute_mode_v3(struct psmouse *psmouse)
1284{
1285 int reg_val;
1286
1287 reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
1288 if (reg_val == -1)
1289 return -1;
1290
1291 reg_val |= 0x06;
1292 if (__alps_command_mode_write_reg(psmouse, reg_val))
1293 return -1;
1294
1295 return 0;
1296}
1297
1298static int alps_hw_init_v3(struct psmouse *psmouse)
1299{
1300 struct alps_data *priv = psmouse->private;
1301 struct ps2dev *ps2dev = &psmouse->ps2dev;
1302 int reg_val;
1303 unsigned char param[4];
1304
1305 priv->nibble_commands = alps_v3_nibble_commands;
1306 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
1307
1308 if (alps_enter_command_mode(psmouse, NULL))
1309 goto error;
1310
1311 /* Check for trackstick */
1312 reg_val = alps_command_mode_read_reg(psmouse, 0x0008);
1313 if (reg_val == -1)
1314 goto error;
1315 if (reg_val & 0x80) {
1316 if (alps_passthrough_mode_v3(psmouse, true))
1317 goto error;
1318 if (alps_exit_command_mode(psmouse))
1319 goto error;
1320
1321 /*
1322 * E7 report for the trackstick
1323 *
1324 * There have been reports of failures to seem to trace back
1325 * to the above trackstick check failing. When these occur
1326 * this E7 report fails, so when that happens we continue
1327 * with the assumption that there isn't a trackstick after
1328 * all.
1329 */
1330 param[0] = 0x64;
1331 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1332 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1333 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1334 ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO)) {
1335 psmouse_warn(psmouse, "trackstick E7 report failed\n");
1336 } else {
1337 psmouse_dbg(psmouse,
1338 "trackstick E7 report: %2.2x %2.2x %2.2x\n",
1339 param[0], param[1], param[2]);
1340
1341 /*
1342 * Not sure what this does, but it is absolutely
1343 * essential. Without it, the touchpad does not
1344 * work at all and the trackstick just emits normal
1345 * PS/2 packets.
1346 */
1347 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1348 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1349 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1350 alps_command_mode_send_nibble(psmouse, 0x9) ||
1351 alps_command_mode_send_nibble(psmouse, 0x4)) {
1352 psmouse_err(psmouse,
1353 "Error sending magic E6 sequence\n");
1354 goto error_passthrough;
1355 }
1356 }
1357
1358 if (alps_enter_command_mode(psmouse, NULL))
1359 goto error_passthrough;
1360 if (alps_passthrough_mode_v3(psmouse, false))
1361 goto error;
1362 }
1363
1364 if (alps_absolute_mode_v3(psmouse)) {
1365 psmouse_err(psmouse, "Failed to enter absolute mode\n");
1366 goto error;
1367 }
1368
1369 reg_val = alps_command_mode_read_reg(psmouse, 0x0006);
1370 if (reg_val == -1)
1371 goto error;
1372 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
1373 goto error;
1374
1375 reg_val = alps_command_mode_read_reg(psmouse, 0x0007);
1376 if (reg_val == -1)
1377 goto error;
1378 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
1379 goto error;
1380
1381 if (alps_command_mode_read_reg(psmouse, 0x0144) == -1)
1382 goto error;
1383 if (__alps_command_mode_write_reg(psmouse, 0x04))
1384 goto error;
1385
1386 if (alps_command_mode_read_reg(psmouse, 0x0159) == -1)
1387 goto error;
1388 if (__alps_command_mode_write_reg(psmouse, 0x03))
1389 goto error;
1390
1391 if (alps_command_mode_read_reg(psmouse, 0x0163) == -1)
1392 goto error;
1393 if (alps_command_mode_write_reg(psmouse, 0x0163, 0x03))
1394 goto error;
1395
1396 if (alps_command_mode_read_reg(psmouse, 0x0162) == -1)
1397 goto error;
1398 if (alps_command_mode_write_reg(psmouse, 0x0162, 0x04))
1399 goto error;
1400
1401 /*
1402 * This ensures the trackstick packets are in the format
1403 * supported by this driver. If bit 1 isn't set the packet
1404 * format is different.
1405 */
1406 if (alps_command_mode_write_reg(psmouse, 0x0008, 0x82))
1407 goto error;
1408
1409 alps_exit_command_mode(psmouse);
1410
1411 /* Set rate and enable data reporting */
1412 param[0] = 0x64;
1413 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
1414 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
1415 psmouse_err(psmouse, "Failed to enable data reporting\n");
1416 return -1;
1417 }
1418
1419 return 0;
1420
1421error_passthrough:
1422 /* Something failed while in passthrough mode, so try to get out */
1423 if (!alps_enter_command_mode(psmouse, NULL))
1424 alps_passthrough_mode_v3(psmouse, false);
1425error:
1426 /*
1427 * Leaving the touchpad in command mode will essentially render
1428 * it unusable until the machine reboots, so exit it here just
1429 * to be safe
1430 */
1431 alps_exit_command_mode(psmouse);
1432 return -1;
1433}
1434
1435/* Must be in command mode when calling this function */
1436static int alps_absolute_mode_v4(struct psmouse *psmouse)
1437{
1438 int reg_val;
1439
1440 reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
1441 if (reg_val == -1)
1442 return -1;
1443
1444 reg_val |= 0x02;
1445 if (__alps_command_mode_write_reg(psmouse, reg_val))
1446 return -1;
1447
1448 return 0;
1449}
1450
1451static int alps_hw_init_v4(struct psmouse *psmouse)
1452{
1453 struct alps_data *priv = psmouse->private;
1454 struct ps2dev *ps2dev = &psmouse->ps2dev;
1455 unsigned char param[4];
1456
1457 priv->nibble_commands = alps_v4_nibble_commands;
1458 priv->addr_command = PSMOUSE_CMD_DISABLE;
1459
1460 if (alps_enter_command_mode(psmouse, NULL))
1461 goto error;
1462
1463 if (alps_absolute_mode_v4(psmouse)) {
1464 psmouse_err(psmouse, "Failed to enter absolute mode\n");
1465 goto error;
1466 }
1467
1468 if (alps_command_mode_write_reg(psmouse, 0x0007, 0x8c))
1469 goto error;
1470
1471 if (alps_command_mode_write_reg(psmouse, 0x0149, 0x03))
1472 goto error;
1473
1474 if (alps_command_mode_write_reg(psmouse, 0x0160, 0x03))
1475 goto error;
1476
1477 if (alps_command_mode_write_reg(psmouse, 0x017f, 0x15))
1478 goto error;
1479
1480 if (alps_command_mode_write_reg(psmouse, 0x0151, 0x01))
1481 goto error;
1482
1483 if (alps_command_mode_write_reg(psmouse, 0x0168, 0x03))
1484 goto error;
1485
1486 if (alps_command_mode_write_reg(psmouse, 0x014a, 0x03))
1487 goto error;
1488
1489 if (alps_command_mode_write_reg(psmouse, 0x0161, 0x03))
1490 goto error;
1491
1492 alps_exit_command_mode(psmouse);
1493
1494 /*
1495 * This sequence changes the output from a 9-byte to an
1496 * 8-byte format. All the same data seems to be present,
1497 * just in a more compact format.
1498 */
1499 param[0] = 0xc8;
1500 param[1] = 0x64;
1501 param[2] = 0x50;
1502 if (ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
1503 ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE) ||
1504 ps2_command(ps2dev, &param[2], PSMOUSE_CMD_SETRATE) ||
1505 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
1506 return -1;
1507
1508 /* Set rate and enable data reporting */
1509 param[0] = 0x64;
1510 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
1511 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
1512 psmouse_err(psmouse, "Failed to enable data reporting\n");
1513 return -1;
1514 }
1515
1516 return 0;
1517
1518error:
1519 /*
1520 * Leaving the touchpad in command mode will essentially render
1521 * it unusable until the machine reboots, so exit it here just
1522 * to be safe
1523 */
1524 alps_exit_command_mode(psmouse);
1525 return -1;
1526}
1527
1528static int alps_hw_init(struct psmouse *psmouse)
1529{
1530 struct alps_data *priv = psmouse->private;
1531 const struct alps_model_info *model = priv->i;
1532 int ret = -1;
1533
1534 switch (model->proto_version) {
1535 case ALPS_PROTO_V1:
1536 case ALPS_PROTO_V2:
1537 ret = alps_hw_init_v1_v2(psmouse);
1538 break;
1539 case ALPS_PROTO_V3:
1540 ret = alps_hw_init_v3(psmouse);
1541 break;
1542 case ALPS_PROTO_V4:
1543 ret = alps_hw_init_v4(psmouse);
1544 break;
1545 }
1546
1547 return ret;
1548}
1549
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04001550static int alps_reconnect(struct psmouse *psmouse)
1551{
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08001552 const struct alps_model_info *model;
1553
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04001554 psmouse_reset(psmouse);
1555
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08001556 model = alps_get_model(psmouse, NULL);
1557 if (!model)
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04001558 return -1;
1559
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08001560 return alps_hw_init(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561}
1562
1563static void alps_disconnect(struct psmouse *psmouse)
1564{
1565 struct alps_data *priv = psmouse->private;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001566
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 psmouse_reset(psmouse);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001568 del_timer_sync(&priv->timer);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001569 input_unregister_device(priv->dev2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 kfree(priv);
1571}
1572
1573int alps_init(struct psmouse *psmouse)
1574{
1575 struct alps_data *priv;
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08001576 const struct alps_model_info *model;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001577 struct input_dev *dev1 = psmouse->dev, *dev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 int version;
1579
Dmitry Torokhovf42649e2007-04-12 01:31:13 -04001580 priv = kzalloc(sizeof(struct alps_data), GFP_KERNEL);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001581 dev2 = input_allocate_device();
1582 if (!priv || !dev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 goto init_fail;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001584
1585 priv->dev2 = dev2;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001586 setup_timer(&priv->timer, alps_flush_packet, (unsigned long)psmouse);
1587
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04001588 psmouse->private = priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
Seth Forshee25bded72011-11-07 19:53:36 -08001590 psmouse_reset(psmouse);
1591
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08001592 model = alps_get_model(psmouse, &version);
1593 if (!model)
1594 goto init_fail;
1595
1596 priv->i = model;
1597
1598 if (alps_hw_init(psmouse))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 goto init_fail;
1600
Dmitry Torokhov7105d2e2009-12-11 23:54:54 -08001601 /*
1602 * Undo part of setup done for us by psmouse core since touchpad
1603 * is not a relative device.
1604 */
1605 __clear_bit(EV_REL, dev1->evbit);
1606 __clear_bit(REL_X, dev1->relbit);
1607 __clear_bit(REL_Y, dev1->relbit);
1608
1609 /*
1610 * Now set up our capabilities.
1611 */
Jiri Slaby7b19ada2007-10-18 23:40:32 -07001612 dev1->evbit[BIT_WORD(EV_KEY)] |= BIT_MASK(EV_KEY);
1613 dev1->keybit[BIT_WORD(BTN_TOUCH)] |= BIT_MASK(BTN_TOUCH);
1614 dev1->keybit[BIT_WORD(BTN_TOOL_FINGER)] |= BIT_MASK(BTN_TOOL_FINGER);
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08001615 dev1->keybit[BIT_WORD(BTN_LEFT)] |=
1616 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
Jiri Slaby7b19ada2007-10-18 23:40:32 -07001618 dev1->evbit[BIT_WORD(EV_ABS)] |= BIT_MASK(EV_ABS);
Seth Forshee25bded72011-11-07 19:53:36 -08001619
1620 switch (model->proto_version) {
1621 case ALPS_PROTO_V1:
1622 case ALPS_PROTO_V2:
1623 input_set_abs_params(dev1, ABS_X, 0, 1023, 0, 0);
1624 input_set_abs_params(dev1, ABS_Y, 0, 767, 0, 0);
1625 break;
1626 case ALPS_PROTO_V3:
George Pantalos3b7e09f2012-05-10 22:31:59 -07001627 case ALPS_PROTO_V4:
Seth Forshee01ce6612011-11-07 19:54:13 -08001628 set_bit(INPUT_PROP_SEMI_MT, dev1->propbit);
1629 input_mt_init_slots(dev1, 2);
1630 input_set_abs_params(dev1, ABS_MT_POSITION_X, 0, ALPS_V3_X_MAX, 0, 0);
1631 input_set_abs_params(dev1, ABS_MT_POSITION_Y, 0, ALPS_V3_Y_MAX, 0, 0);
1632
1633 set_bit(BTN_TOOL_DOUBLETAP, dev1->keybit);
1634 set_bit(BTN_TOOL_TRIPLETAP, dev1->keybit);
1635 set_bit(BTN_TOOL_QUADTAP, dev1->keybit);
George Pantalos3b7e09f2012-05-10 22:31:59 -07001636
Seth Forshee01ce6612011-11-07 19:54:13 -08001637 input_set_abs_params(dev1, ABS_X, 0, ALPS_V3_X_MAX, 0, 0);
1638 input_set_abs_params(dev1, ABS_Y, 0, ALPS_V3_Y_MAX, 0, 0);
Seth Forshee25bded72011-11-07 19:53:36 -08001639 break;
1640 }
1641
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001642 input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08001644 if (model->flags & ALPS_WHEEL) {
Jiri Slaby7b19ada2007-10-18 23:40:32 -07001645 dev1->evbit[BIT_WORD(EV_REL)] |= BIT_MASK(EV_REL);
1646 dev1->relbit[BIT_WORD(REL_WHEEL)] |= BIT_MASK(REL_WHEEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 }
1648
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08001649 if (model->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
Jiri Slaby7b19ada2007-10-18 23:40:32 -07001650 dev1->keybit[BIT_WORD(BTN_FORWARD)] |= BIT_MASK(BTN_FORWARD);
1651 dev1->keybit[BIT_WORD(BTN_BACK)] |= BIT_MASK(BTN_BACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 }
1653
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08001654 if (model->flags & ALPS_FOUR_BUTTONS) {
1655 dev1->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_0);
1656 dev1->keybit[BIT_WORD(BTN_1)] |= BIT_MASK(BTN_1);
1657 dev1->keybit[BIT_WORD(BTN_2)] |= BIT_MASK(BTN_2);
1658 dev1->keybit[BIT_WORD(BTN_3)] |= BIT_MASK(BTN_3);
1659 } else {
1660 dev1->keybit[BIT_WORD(BTN_MIDDLE)] |= BIT_MASK(BTN_MIDDLE);
1661 }
1662
Dmitry Torokhov08ffce42006-06-26 01:45:10 -04001663 snprintf(priv->phys, sizeof(priv->phys), "%s/input1", psmouse->ps2dev.serio->phys);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001664 dev2->phys = priv->phys;
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08001665 dev2->name = (model->flags & ALPS_DUALPOINT) ? "DualPoint Stick" : "PS/2 Mouse";
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001666 dev2->id.bustype = BUS_I8042;
1667 dev2->id.vendor = 0x0002;
1668 dev2->id.product = PSMOUSE_ALPS;
1669 dev2->id.version = 0x0000;
Dmitry Torokhov1db3a342008-03-18 00:29:18 -04001670 dev2->dev.parent = &psmouse->ps2dev.serio->dev;
Dmitry Torokhov968ac842005-05-29 02:28:29 -05001671
Jiri Slaby7b19ada2007-10-18 23:40:32 -07001672 dev2->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08001673 dev2->relbit[BIT_WORD(REL_X)] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
1674 dev2->keybit[BIT_WORD(BTN_LEFT)] =
1675 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_MIDDLE) | BIT_MASK(BTN_RIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
Dmitry Torokhovf42649e2007-04-12 01:31:13 -04001677 if (input_register_device(priv->dev2))
1678 goto init_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
1680 psmouse->protocol_handler = alps_process_byte;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001681 psmouse->poll = alps_poll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 psmouse->disconnect = alps_disconnect;
1683 psmouse->reconnect = alps_reconnect;
Seth Forshee25bded72011-11-07 19:53:36 -08001684 psmouse->pktsize = model->proto_version == ALPS_PROTO_V4 ? 8 : 6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001686 /* We are having trouble resyncing ALPS touchpads so disable it for now */
1687 psmouse->resync_time = 0;
1688
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 return 0;
1690
1691init_fail:
Dmitry Torokhovf42649e2007-04-12 01:31:13 -04001692 psmouse_reset(psmouse);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001693 input_free_device(dev2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 kfree(priv);
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04001695 psmouse->private = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 return -1;
1697}
1698
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001699int alps_detect(struct psmouse *psmouse, bool set_properties)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700{
1701 int version;
Helge Dellere38de672006-09-10 21:54:39 -04001702 const struct alps_model_info *model;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
Dmitry Torokhovf42649e2007-04-12 01:31:13 -04001704 model = alps_get_model(psmouse, &version);
1705 if (!model)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 return -1;
1707
1708 if (set_properties) {
1709 psmouse->vendor = "ALPS";
Dmitry Torokhov968ac842005-05-29 02:28:29 -05001710 psmouse->name = model->flags & ALPS_DUALPOINT ?
1711 "DualPoint TouchPad" : "GlidePoint";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 psmouse->model = version;
1713 }
1714 return 0;
1715}
1716