blob: 1ca854b0b012ae4851847d070055c493e95dd604 [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
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800125static bool alps_is_valid_first_byte(struct alps_data *priv,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800126 unsigned char data)
127{
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800128 return (data & priv->mask0) == priv->byte0;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800129}
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;
161 unsigned char *packet = psmouse->packet;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500162 struct input_dev *dev = psmouse->dev;
163 struct input_dev *dev2 = priv->dev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 int x, y, z, ges, fin, left, right, middle;
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500165 int back = 0, forward = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800167 if (priv->proto_version == ALPS_PROTO_V1) {
Yotam Medinid2f40122006-05-29 23:30:36 -0400168 left = packet[2] & 0x10;
169 right = packet[2] & 0x08;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 middle = 0;
171 x = packet[1] | ((packet[0] & 0x07) << 7);
172 y = packet[4] | ((packet[3] & 0x07) << 7);
173 z = packet[5];
174 } else {
175 left = packet[3] & 1;
176 right = packet[3] & 2;
177 middle = packet[3] & 4;
178 x = packet[1] | ((packet[2] & 0x78) << (7 - 3));
179 y = packet[4] | ((packet[3] & 0x70) << (7 - 4));
180 z = packet[5];
181 }
182
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800183 if (priv->flags & ALPS_FW_BK_1) {
Laszlo Kajan3c00bb92008-03-18 00:39:55 -0400184 back = packet[0] & 0x10;
185 forward = packet[2] & 4;
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500186 }
187
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800188 if (priv->flags & ALPS_FW_BK_2) {
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500189 back = packet[3] & 4;
190 forward = packet[2] & 4;
191 if ((middle = forward && back))
192 forward = back = 0;
193 }
194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 ges = packet[2] & 1;
196 fin = packet[2] & 2;
197
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800198 if ((priv->flags & ALPS_DUALPOINT) && z == 127) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 input_report_rel(dev2, REL_X, (x > 383 ? (x - 768) : x));
200 input_report_rel(dev2, REL_Y, -(y > 255 ? (y - 512) : y));
Ulrich Dangeld7ed5d82009-06-11 00:15:09 -0700201
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800202 alps_report_buttons(psmouse, dev2, dev, left, right, middle);
Ulrich Dangeld7ed5d82009-06-11 00:15:09 -0700203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 input_sync(dev2);
205 return;
206 }
207
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800208 alps_report_buttons(psmouse, dev, dev2, left, right, middle);
Ulrich Dangeld7ed5d82009-06-11 00:15:09 -0700209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 /* Convert hardware tap to a reasonable Z value */
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800211 if (ges && !fin)
212 z = 40;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214 /*
215 * A "tap and drag" operation is reported by the hardware as a transition
216 * from (!fin && ges) to (fin && ges). This should be translated to the
217 * sequence Z>0, Z==0, Z>0, so the Z==0 event has to be generated manually.
218 */
219 if (ges && fin && !priv->prev_fin) {
220 input_report_abs(dev, ABS_X, x);
221 input_report_abs(dev, ABS_Y, y);
222 input_report_abs(dev, ABS_PRESSURE, 0);
223 input_report_key(dev, BTN_TOOL_FINGER, 0);
224 input_sync(dev);
225 }
226 priv->prev_fin = fin;
227
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800228 if (z > 30)
229 input_report_key(dev, BTN_TOUCH, 1);
230 if (z < 25)
231 input_report_key(dev, BTN_TOUCH, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233 if (z > 0) {
234 input_report_abs(dev, ABS_X, x);
235 input_report_abs(dev, ABS_Y, y);
236 }
237
238 input_report_abs(dev, ABS_PRESSURE, z);
239 input_report_key(dev, BTN_TOOL_FINGER, z > 0);
240
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800241 if (priv->flags & ALPS_WHEEL)
Vojtech Pavlike6c047b2005-09-04 01:40:43 -0500242 input_report_rel(dev, REL_WHEEL, ((packet[2] << 1) & 0x08) - ((packet[0] >> 4) & 0x07));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800244 if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500245 input_report_key(dev, BTN_FORWARD, forward);
246 input_report_key(dev, BTN_BACK, back);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 }
248
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800249 if (priv->flags & ALPS_FOUR_BUTTONS) {
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800250 input_report_key(dev, BTN_0, packet[2] & 4);
251 input_report_key(dev, BTN_1, packet[0] & 0x10);
252 input_report_key(dev, BTN_2, packet[3] & 4);
253 input_report_key(dev, BTN_3, packet[0] & 0x20);
254 }
255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 input_sync(dev);
257}
258
Seth Forshee01ce6612011-11-07 19:54:13 -0800259/*
260 * Process bitmap data from v3 and v4 protocols. Returns the number of
261 * fingers detected. A return value of 0 means at least one of the
262 * bitmaps was empty.
263 *
264 * The bitmaps don't have enough data to track fingers, so this function
265 * only generates points representing a bounding box of all contacts.
266 * These points are returned in x1, y1, x2, and y2 when the return value
267 * is greater than 0.
268 */
269static int alps_process_bitmap(unsigned int x_map, unsigned int y_map,
270 int *x1, int *y1, int *x2, int *y2)
271{
272 struct alps_bitmap_point {
273 int start_bit;
274 int num_bits;
275 };
276
277 int fingers_x = 0, fingers_y = 0, fingers;
278 int i, bit, prev_bit;
279 struct alps_bitmap_point x_low = {0,}, x_high = {0,};
280 struct alps_bitmap_point y_low = {0,}, y_high = {0,};
281 struct alps_bitmap_point *point;
282
283 if (!x_map || !y_map)
284 return 0;
285
286 *x1 = *y1 = *x2 = *y2 = 0;
287
288 prev_bit = 0;
289 point = &x_low;
290 for (i = 0; x_map != 0; i++, x_map >>= 1) {
291 bit = x_map & 1;
292 if (bit) {
293 if (!prev_bit) {
294 point->start_bit = i;
295 fingers_x++;
296 }
297 point->num_bits++;
298 } else {
299 if (prev_bit)
300 point = &x_high;
301 else
302 point->num_bits = 0;
303 }
304 prev_bit = bit;
305 }
306
307 /*
308 * y bitmap is reversed for what we need (lower positions are in
309 * higher bits), so we process from the top end.
310 */
311 y_map = y_map << (sizeof(y_map) * BITS_PER_BYTE - ALPS_BITMAP_Y_BITS);
312 prev_bit = 0;
313 point = &y_low;
314 for (i = 0; y_map != 0; i++, y_map <<= 1) {
315 bit = y_map & (1 << (sizeof(y_map) * BITS_PER_BYTE - 1));
316 if (bit) {
317 if (!prev_bit) {
318 point->start_bit = i;
319 fingers_y++;
320 }
321 point->num_bits++;
322 } else {
323 if (prev_bit)
324 point = &y_high;
325 else
326 point->num_bits = 0;
327 }
328 prev_bit = bit;
329 }
330
331 /*
332 * Fingers can overlap, so we use the maximum count of fingers
333 * on either axis as the finger count.
334 */
335 fingers = max(fingers_x, fingers_y);
336
337 /*
338 * If total fingers is > 1 but either axis reports only a single
339 * contact, we have overlapping or adjacent fingers. For the
340 * purposes of creating a bounding box, divide the single contact
341 * (roughly) equally between the two points.
342 */
343 if (fingers > 1) {
344 if (fingers_x == 1) {
345 i = x_low.num_bits / 2;
346 x_low.num_bits = x_low.num_bits - i;
347 x_high.start_bit = x_low.start_bit + i;
348 x_high.num_bits = max(i, 1);
349 } else if (fingers_y == 1) {
350 i = y_low.num_bits / 2;
351 y_low.num_bits = y_low.num_bits - i;
352 y_high.start_bit = y_low.start_bit + i;
353 y_high.num_bits = max(i, 1);
354 }
355 }
356
357 *x1 = (ALPS_V3_X_MAX * (2 * x_low.start_bit + x_low.num_bits - 1)) /
358 (2 * (ALPS_BITMAP_X_BITS - 1));
359 *y1 = (ALPS_V3_Y_MAX * (2 * y_low.start_bit + y_low.num_bits - 1)) /
360 (2 * (ALPS_BITMAP_Y_BITS - 1));
361
362 if (fingers > 1) {
363 *x2 = (ALPS_V3_X_MAX * (2 * x_high.start_bit + x_high.num_bits - 1)) /
364 (2 * (ALPS_BITMAP_X_BITS - 1));
365 *y2 = (ALPS_V3_Y_MAX * (2 * y_high.start_bit + y_high.num_bits - 1)) /
366 (2 * (ALPS_BITMAP_Y_BITS - 1));
367 }
368
369 return fingers;
370}
371
372static void alps_set_slot(struct input_dev *dev, int slot, bool active,
373 int x, int y)
374{
375 input_mt_slot(dev, slot);
376 input_mt_report_slot_state(dev, MT_TOOL_FINGER, active);
377 if (active) {
378 input_report_abs(dev, ABS_MT_POSITION_X, x);
379 input_report_abs(dev, ABS_MT_POSITION_Y, y);
380 }
381}
382
383static void alps_report_semi_mt_data(struct input_dev *dev, int num_fingers,
384 int x1, int y1, int x2, int y2)
385{
386 alps_set_slot(dev, 0, num_fingers != 0, x1, y1);
387 alps_set_slot(dev, 1, num_fingers == 2, x2, y2);
388}
389
Seth Forshee25bded72011-11-07 19:53:36 -0800390static void alps_process_trackstick_packet_v3(struct psmouse *psmouse)
391{
392 struct alps_data *priv = psmouse->private;
393 unsigned char *packet = psmouse->packet;
394 struct input_dev *dev = priv->dev2;
395 int x, y, z, left, right, middle;
396
397 /* Sanity check packet */
398 if (!(packet[0] & 0x40)) {
399 psmouse_dbg(psmouse, "Bad trackstick packet, discarding\n");
400 return;
401 }
402
403 /*
404 * There's a special packet that seems to indicate the end
405 * of a stream of trackstick data. Filter these out.
406 */
407 if (packet[1] == 0x7f && packet[2] == 0x7f && packet[4] == 0x7f)
408 return;
409
410 x = (s8)(((packet[0] & 0x20) << 2) | (packet[1] & 0x7f));
411 y = (s8)(((packet[0] & 0x10) << 3) | (packet[2] & 0x7f));
412 z = (packet[4] & 0x7c) >> 2;
413
414 /*
415 * The x and y values tend to be quite large, and when used
416 * alone the trackstick is difficult to use. Scale them down
417 * to compensate.
418 */
419 x /= 8;
420 y /= 8;
421
422 input_report_rel(dev, REL_X, x);
423 input_report_rel(dev, REL_Y, -y);
424
425 /*
426 * Most ALPS models report the trackstick buttons in the touchpad
427 * packets, but a few report them here. No reliable way has been
428 * found to differentiate between the models upfront, so we enable
429 * the quirk in response to seeing a button press in the trackstick
430 * packet.
431 */
432 left = packet[3] & 0x01;
433 right = packet[3] & 0x02;
434 middle = packet[3] & 0x04;
435
436 if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) &&
437 (left || right || middle))
438 priv->quirks |= ALPS_QUIRK_TRACKSTICK_BUTTONS;
439
440 if (priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) {
441 input_report_key(dev, BTN_LEFT, left);
442 input_report_key(dev, BTN_RIGHT, right);
443 input_report_key(dev, BTN_MIDDLE, middle);
444 }
445
446 input_sync(dev);
447 return;
448}
449
450static void alps_process_touchpad_packet_v3(struct psmouse *psmouse)
451{
452 struct alps_data *priv = psmouse->private;
453 unsigned char *packet = psmouse->packet;
454 struct input_dev *dev = psmouse->dev;
455 struct input_dev *dev2 = priv->dev2;
456 int x, y, z;
457 int left, right, middle;
Seth Forshee01ce6612011-11-07 19:54:13 -0800458 int x1 = 0, y1 = 0, x2 = 0, y2 = 0;
459 int fingers = 0, bmap_fingers;
460 unsigned int x_bitmap, y_bitmap;
Seth Forshee25bded72011-11-07 19:53:36 -0800461
462 /*
Seth Forshee01ce6612011-11-07 19:54:13 -0800463 * There's no single feature of touchpad position and bitmap packets
464 * that can be used to distinguish between them. We rely on the fact
465 * that a bitmap packet should always follow a position packet with
466 * bit 6 of packet[4] set.
Seth Forshee25bded72011-11-07 19:53:36 -0800467 */
468 if (priv->multi_packet) {
Seth Forshee25bded72011-11-07 19:53:36 -0800469 /*
470 * Sometimes a position packet will indicate a multi-packet
471 * sequence, but then what follows is another position
472 * packet. Check for this, and when it happens process the
473 * position packet as usual.
474 */
475 if (packet[0] & 0x40) {
Seth Forshee01ce6612011-11-07 19:54:13 -0800476 fingers = (packet[5] & 0x3) + 1;
477 x_bitmap = ((packet[4] & 0x7e) << 8) |
478 ((packet[1] & 0x7f) << 2) |
479 ((packet[0] & 0x30) >> 4);
480 y_bitmap = ((packet[3] & 0x70) << 4) |
481 ((packet[2] & 0x7f) << 1) |
482 (packet[4] & 0x01);
483
484 bmap_fingers = alps_process_bitmap(x_bitmap, y_bitmap,
485 &x1, &y1, &x2, &y2);
486
Seth Forshee25bded72011-11-07 19:53:36 -0800487 /*
Seth Forshee01ce6612011-11-07 19:54:13 -0800488 * We shouldn't report more than one finger if
489 * we don't have two coordinates.
Seth Forshee25bded72011-11-07 19:53:36 -0800490 */
Seth Forshee01ce6612011-11-07 19:54:13 -0800491 if (fingers > 1 && bmap_fingers < 2)
492 fingers = bmap_fingers;
493
494 /* Now process position packet */
495 packet = priv->multi_data;
496 } else {
497 priv->multi_packet = 0;
Seth Forshee25bded72011-11-07 19:53:36 -0800498 }
499 }
500
Seth Forshee01ce6612011-11-07 19:54:13 -0800501 /*
502 * Bit 6 of byte 0 is not usually set in position packets. The only
503 * times it seems to be set is in situations where the data is
504 * suspect anyway, e.g. a palm resting flat on the touchpad. Given
505 * this combined with the fact that this bit is useful for filtering
506 * out misidentified bitmap packets, we reject anything with this
507 * bit set.
508 */
509 if (packet[0] & 0x40)
510 return;
511
512 if (!priv->multi_packet && (packet[4] & 0x40)) {
Seth Forshee25bded72011-11-07 19:53:36 -0800513 priv->multi_packet = 1;
Seth Forshee01ce6612011-11-07 19:54:13 -0800514 memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
515 return;
516 }
517
518 priv->multi_packet = 0;
Seth Forshee25bded72011-11-07 19:53:36 -0800519
520 left = packet[3] & 0x01;
521 right = packet[3] & 0x02;
522 middle = packet[3] & 0x04;
523
524 x = ((packet[1] & 0x7f) << 4) | ((packet[4] & 0x30) >> 2) |
525 ((packet[0] & 0x30) >> 4);
526 y = ((packet[2] & 0x7f) << 4) | (packet[4] & 0x0f);
527 z = packet[5] & 0x7f;
528
529 /*
530 * Sometimes the hardware sends a single packet with z = 0
531 * in the middle of a stream. Real releases generate packets
532 * with x, y, and z all zero, so these seem to be flukes.
533 * Ignore them.
534 */
535 if (x && y && !z)
536 return;
537
Seth Forshee01ce6612011-11-07 19:54:13 -0800538 /*
539 * If we don't have MT data or the bitmaps were empty, we have
540 * to rely on ST data.
541 */
542 if (!fingers) {
543 x1 = x;
544 y1 = y;
545 fingers = z > 0 ? 1 : 0;
546 }
547
Seth Forshee25bded72011-11-07 19:53:36 -0800548 if (z >= 64)
549 input_report_key(dev, BTN_TOUCH, 1);
550 else
551 input_report_key(dev, BTN_TOUCH, 0);
552
Seth Forshee01ce6612011-11-07 19:54:13 -0800553 alps_report_semi_mt_data(dev, fingers, x1, y1, x2, y2);
554
Dmitry Torokhov616575c2012-05-10 22:31:59 -0700555 input_mt_report_finger_count(dev, fingers);
Seth Forshee01ce6612011-11-07 19:54:13 -0800556
557 input_report_key(dev, BTN_LEFT, left);
558 input_report_key(dev, BTN_RIGHT, right);
559 input_report_key(dev, BTN_MIDDLE, middle);
560
Seth Forshee25bded72011-11-07 19:53:36 -0800561 if (z > 0) {
562 input_report_abs(dev, ABS_X, x);
563 input_report_abs(dev, ABS_Y, y);
564 }
565 input_report_abs(dev, ABS_PRESSURE, z);
566
Seth Forshee25bded72011-11-07 19:53:36 -0800567 input_sync(dev);
568
569 if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS)) {
570 left = packet[3] & 0x10;
571 right = packet[3] & 0x20;
572 middle = packet[3] & 0x40;
573
574 input_report_key(dev2, BTN_LEFT, left);
575 input_report_key(dev2, BTN_RIGHT, right);
576 input_report_key(dev2, BTN_MIDDLE, middle);
577 input_sync(dev2);
578 }
579}
580
581static void alps_process_packet_v3(struct psmouse *psmouse)
582{
583 unsigned char *packet = psmouse->packet;
584
585 /*
586 * v3 protocol packets come in three types, two representing
587 * touchpad data and one representing trackstick data.
588 * Trackstick packets seem to be distinguished by always
589 * having 0x3f in the last byte. This value has never been
590 * observed in the last byte of either of the other types
591 * of packets.
592 */
593 if (packet[5] == 0x3f) {
594 alps_process_trackstick_packet_v3(psmouse);
595 return;
596 }
597
598 alps_process_touchpad_packet_v3(psmouse);
599}
600
601static void alps_process_packet_v4(struct psmouse *psmouse)
602{
George Pantalos3b7e09f2012-05-10 22:31:59 -0700603 struct alps_data *priv = psmouse->private;
Seth Forshee25bded72011-11-07 19:53:36 -0800604 unsigned char *packet = psmouse->packet;
605 struct input_dev *dev = psmouse->dev;
George Pantalos3b7e09f2012-05-10 22:31:59 -0700606 int offset;
Seth Forshee25bded72011-11-07 19:53:36 -0800607 int x, y, z;
608 int left, right;
George Pantalos3b7e09f2012-05-10 22:31:59 -0700609 int x1, y1, x2, y2;
610 int fingers = 0;
611 unsigned int x_bitmap, y_bitmap;
612
613 /*
614 * v4 has a 6-byte encoding for bitmap data, but this data is
615 * broken up between 3 normal packets. Use priv->multi_packet to
616 * track our position in the bitmap packet.
617 */
618 if (packet[6] & 0x40) {
619 /* sync, reset position */
620 priv->multi_packet = 0;
621 }
622
623 if (WARN_ON_ONCE(priv->multi_packet > 2))
624 return;
625
626 offset = 2 * priv->multi_packet;
627 priv->multi_data[offset] = packet[6];
628 priv->multi_data[offset + 1] = packet[7];
629
630 if (++priv->multi_packet > 2) {
631 priv->multi_packet = 0;
632
633 x_bitmap = ((priv->multi_data[2] & 0x1f) << 10) |
634 ((priv->multi_data[3] & 0x60) << 3) |
635 ((priv->multi_data[0] & 0x3f) << 2) |
636 ((priv->multi_data[1] & 0x60) >> 5);
637 y_bitmap = ((priv->multi_data[5] & 0x01) << 10) |
638 ((priv->multi_data[3] & 0x1f) << 5) |
639 (priv->multi_data[1] & 0x1f);
640
641 fingers = alps_process_bitmap(x_bitmap, y_bitmap,
642 &x1, &y1, &x2, &y2);
643
644 /* Store MT data.*/
645 priv->fingers = fingers;
646 priv->x1 = x1;
647 priv->x2 = x2;
648 priv->y1 = y1;
649 priv->y2 = y2;
650 }
Seth Forshee25bded72011-11-07 19:53:36 -0800651
652 left = packet[4] & 0x01;
653 right = packet[4] & 0x02;
654
655 x = ((packet[1] & 0x7f) << 4) | ((packet[3] & 0x30) >> 2) |
656 ((packet[0] & 0x30) >> 4);
657 y = ((packet[2] & 0x7f) << 4) | (packet[3] & 0x0f);
658 z = packet[5] & 0x7f;
659
George Pantalos3b7e09f2012-05-10 22:31:59 -0700660 /*
661 * If there were no contacts in the bitmap, use ST
662 * points in MT reports.
663 * If there were two contacts or more, report MT data.
664 */
665 if (priv->fingers < 2) {
666 x1 = x;
667 y1 = y;
668 fingers = z > 0 ? 1 : 0;
669 } else {
670 fingers = priv->fingers;
671 x1 = priv->x1;
672 x2 = priv->x2;
673 y1 = priv->y1;
674 y2 = priv->y2;
675 }
676
Seth Forshee25bded72011-11-07 19:53:36 -0800677 if (z >= 64)
678 input_report_key(dev, BTN_TOUCH, 1);
679 else
680 input_report_key(dev, BTN_TOUCH, 0);
681
George Pantalos3b7e09f2012-05-10 22:31:59 -0700682 alps_report_semi_mt_data(dev, fingers, x1, y1, x2, y2);
683
Dmitry Torokhov616575c2012-05-10 22:31:59 -0700684 input_mt_report_finger_count(dev, fingers);
George Pantalos3b7e09f2012-05-10 22:31:59 -0700685
686 input_report_key(dev, BTN_LEFT, left);
687 input_report_key(dev, BTN_RIGHT, right);
688
Seth Forshee25bded72011-11-07 19:53:36 -0800689 if (z > 0) {
690 input_report_abs(dev, ABS_X, x);
691 input_report_abs(dev, ABS_Y, y);
692 }
693 input_report_abs(dev, ABS_PRESSURE, z);
694
Seth Forshee25bded72011-11-07 19:53:36 -0800695 input_sync(dev);
696}
697
698static void alps_process_packet(struct psmouse *psmouse)
699{
700 struct alps_data *priv = psmouse->private;
Seth Forshee25bded72011-11-07 19:53:36 -0800701
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800702 switch (priv->proto_version) {
Seth Forshee25bded72011-11-07 19:53:36 -0800703 case ALPS_PROTO_V1:
704 case ALPS_PROTO_V2:
705 alps_process_packet_v1_v2(psmouse);
706 break;
707 case ALPS_PROTO_V3:
708 alps_process_packet_v3(psmouse);
709 break;
710 case ALPS_PROTO_V4:
711 alps_process_packet_v4(psmouse);
712 break;
713 }
714}
715
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800716static void alps_report_bare_ps2_packet(struct psmouse *psmouse,
717 unsigned char packet[],
718 bool report_buttons)
719{
720 struct alps_data *priv = psmouse->private;
721 struct input_dev *dev2 = priv->dev2;
722
723 if (report_buttons)
724 alps_report_buttons(psmouse, dev2, psmouse->dev,
725 packet[0] & 1, packet[0] & 2, packet[0] & 4);
726
727 input_report_rel(dev2, REL_X,
728 packet[1] ? packet[1] - ((packet[0] << 4) & 0x100) : 0);
729 input_report_rel(dev2, REL_Y,
730 packet[2] ? ((packet[0] << 3) & 0x100) - packet[2] : 0);
731
732 input_sync(dev2);
733}
734
735static psmouse_ret_t alps_handle_interleaved_ps2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736{
737 struct alps_data *priv = psmouse->private;
738
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800739 if (psmouse->pktcnt < 6)
740 return PSMOUSE_GOOD_DATA;
741
742 if (psmouse->pktcnt == 6) {
743 /*
744 * Start a timer to flush the packet if it ends up last
745 * 6-byte packet in the stream. Timer needs to fire
746 * psmouse core times out itself. 20 ms should be enough
747 * to decide if we are getting more data or not.
748 */
749 mod_timer(&priv->timer, jiffies + msecs_to_jiffies(20));
750 return PSMOUSE_GOOD_DATA;
751 }
752
753 del_timer(&priv->timer);
754
755 if (psmouse->packet[6] & 0x80) {
756
757 /*
758 * Highest bit is set - that means we either had
759 * complete ALPS packet and this is start of the
760 * next packet or we got garbage.
761 */
762
763 if (((psmouse->packet[3] |
764 psmouse->packet[4] |
765 psmouse->packet[5]) & 0x80) ||
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800766 (!alps_is_valid_first_byte(priv, psmouse->packet[6]))) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700767 psmouse_dbg(psmouse,
Andy Shevchenko3b112922012-10-30 00:24:41 -0700768 "refusing packet %4ph (suspected interleaved ps/2)\n",
769 psmouse->packet + 3);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800770 return PSMOUSE_BAD_DATA;
771 }
772
773 alps_process_packet(psmouse);
774
775 /* Continue with the next packet */
776 psmouse->packet[0] = psmouse->packet[6];
777 psmouse->pktcnt = 1;
778
779 } else {
780
781 /*
782 * High bit is 0 - that means that we indeed got a PS/2
783 * packet in the middle of ALPS packet.
784 *
785 * There is also possibility that we got 6-byte ALPS
786 * packet followed by 3-byte packet from trackpoint. We
787 * can not distinguish between these 2 scenarios but
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700788 * because the latter is unlikely to happen in course of
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800789 * normal operation (user would need to press all
790 * buttons on the pad and start moving trackpoint
791 * without touching the pad surface) we assume former.
792 * Even if we are wrong the wost thing that would happen
793 * the cursor would jump but we should not get protocol
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700794 * de-synchronization.
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800795 */
796
797 alps_report_bare_ps2_packet(psmouse, &psmouse->packet[3],
798 false);
799
800 /*
801 * Continue with the standard ALPS protocol handling,
802 * but make sure we won't process it as an interleaved
803 * packet again, which may happen if all buttons are
804 * pressed. To avoid this let's reset the 4th bit which
805 * is normally 1.
806 */
807 psmouse->packet[3] = psmouse->packet[6] & 0xf7;
808 psmouse->pktcnt = 4;
809 }
810
811 return PSMOUSE_GOOD_DATA;
812}
813
814static void alps_flush_packet(unsigned long data)
815{
816 struct psmouse *psmouse = (struct psmouse *)data;
817
818 serio_pause_rx(psmouse->ps2dev.serio);
819
Seth Forsheeb46615f2011-11-07 19:53:30 -0800820 if (psmouse->pktcnt == psmouse->pktsize) {
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800821
822 /*
823 * We did not any more data in reasonable amount of time.
824 * Validate the last 3 bytes and process as a standard
825 * ALPS packet.
826 */
827 if ((psmouse->packet[3] |
828 psmouse->packet[4] |
829 psmouse->packet[5]) & 0x80) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700830 psmouse_dbg(psmouse,
Andy Shevchenko3b112922012-10-30 00:24:41 -0700831 "refusing packet %3ph (suspected interleaved ps/2)\n",
832 psmouse->packet + 3);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800833 } else {
834 alps_process_packet(psmouse);
835 }
836 psmouse->pktcnt = 0;
837 }
838
839 serio_continue_rx(psmouse->ps2dev.serio);
840}
841
842static psmouse_ret_t alps_process_byte(struct psmouse *psmouse)
843{
844 struct alps_data *priv = psmouse->private;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800845
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 if ((psmouse->packet[0] & 0xc8) == 0x08) { /* PS/2 packet */
847 if (psmouse->pktcnt == 3) {
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800848 alps_report_bare_ps2_packet(psmouse, psmouse->packet,
849 true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 return PSMOUSE_FULL_PACKET;
851 }
852 return PSMOUSE_GOOD_DATA;
853 }
854
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800855 /* Check for PS/2 packet stuffed in the middle of ALPS packet. */
856
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800857 if ((priv->flags & ALPS_PS2_INTERLEAVED) &&
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800858 psmouse->pktcnt >= 4 && (psmouse->packet[3] & 0x0f) == 0x0f) {
859 return alps_handle_interleaved_ps2(psmouse);
860 }
861
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800862 if (!alps_is_valid_first_byte(priv, psmouse->packet[0])) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700863 psmouse_dbg(psmouse,
864 "refusing packet[0] = %x (mask0 = %x, byte0 = %x)\n",
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800865 psmouse->packet[0], priv->mask0, priv->byte0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 return PSMOUSE_BAD_DATA;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800867 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
Seth Forsheeb46615f2011-11-07 19:53:30 -0800869 /* Bytes 2 - pktsize should have 0 in the highest bit */
870 if (psmouse->pktcnt >= 2 && psmouse->pktcnt <= psmouse->pktsize &&
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800871 (psmouse->packet[psmouse->pktcnt - 1] & 0x80)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700872 psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
873 psmouse->pktcnt - 1,
874 psmouse->packet[psmouse->pktcnt - 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 return PSMOUSE_BAD_DATA;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800876 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
Seth Forsheeb46615f2011-11-07 19:53:30 -0800878 if (psmouse->pktcnt == psmouse->pktsize) {
David Howells7d12e782006-10-05 14:55:46 +0100879 alps_process_packet(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 return PSMOUSE_FULL_PACKET;
881 }
882
883 return PSMOUSE_GOOD_DATA;
884}
885
Seth Forshee25bded72011-11-07 19:53:36 -0800886static int alps_command_mode_send_nibble(struct psmouse *psmouse, int nibble)
887{
888 struct ps2dev *ps2dev = &psmouse->ps2dev;
889 struct alps_data *priv = psmouse->private;
890 int command;
891 unsigned char *param;
892 unsigned char dummy[4];
893
894 BUG_ON(nibble > 0xf);
895
896 command = priv->nibble_commands[nibble].command;
897 param = (command & 0x0f00) ?
898 dummy : (unsigned char *)&priv->nibble_commands[nibble].data;
899
900 if (ps2_command(ps2dev, param, command))
901 return -1;
902
903 return 0;
904}
905
906static int alps_command_mode_set_addr(struct psmouse *psmouse, int addr)
907{
908 struct ps2dev *ps2dev = &psmouse->ps2dev;
909 struct alps_data *priv = psmouse->private;
910 int i, nibble;
911
912 if (ps2_command(ps2dev, NULL, priv->addr_command))
913 return -1;
914
915 for (i = 12; i >= 0; i -= 4) {
916 nibble = (addr >> i) & 0xf;
917 if (alps_command_mode_send_nibble(psmouse, nibble))
918 return -1;
919 }
920
921 return 0;
922}
923
924static int __alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
925{
926 struct ps2dev *ps2dev = &psmouse->ps2dev;
927 unsigned char param[4];
928
929 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
930 return -1;
931
932 /*
933 * The address being read is returned in the first two bytes
934 * of the result. Check that this address matches the expected
935 * address.
936 */
937 if (addr != ((param[0] << 8) | param[1]))
938 return -1;
939
940 return param[2];
941}
942
943static int alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
944{
945 if (alps_command_mode_set_addr(psmouse, addr))
946 return -1;
947 return __alps_command_mode_read_reg(psmouse, addr);
948}
949
950static int __alps_command_mode_write_reg(struct psmouse *psmouse, u8 value)
951{
952 if (alps_command_mode_send_nibble(psmouse, (value >> 4) & 0xf))
953 return -1;
954 if (alps_command_mode_send_nibble(psmouse, value & 0xf))
955 return -1;
956 return 0;
957}
958
959static int alps_command_mode_write_reg(struct psmouse *psmouse, int addr,
960 u8 value)
961{
962 if (alps_command_mode_set_addr(psmouse, addr))
963 return -1;
964 return __alps_command_mode_write_reg(psmouse, value);
965}
966
Kevin Cernekee24ba9702013-02-13 22:19:01 -0800967static int alps_rpt_cmd(struct psmouse *psmouse, int init_command,
968 int repeated_command, unsigned char *param)
969{
970 struct ps2dev *ps2dev = &psmouse->ps2dev;
971
972 param[0] = 0;
973 if (init_command && ps2_command(ps2dev, param, init_command))
974 return -EIO;
975
976 if (ps2_command(ps2dev, NULL, repeated_command) ||
977 ps2_command(ps2dev, NULL, repeated_command) ||
978 ps2_command(ps2dev, NULL, repeated_command))
979 return -EIO;
980
981 param[0] = param[1] = param[2] = 0xff;
982 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
983 return -EIO;
984
985 psmouse_dbg(psmouse, "%2.2X report: %2.2x %2.2x %2.2x\n",
986 repeated_command, param[0], param[1], param[2]);
987 return 0;
988}
989
Seth Forshee25bded72011-11-07 19:53:36 -0800990static int alps_enter_command_mode(struct psmouse *psmouse,
991 unsigned char *resp)
992{
993 unsigned char param[4];
Seth Forshee25bded72011-11-07 19:53:36 -0800994
Kevin Cernekee24ba9702013-02-13 22:19:01 -0800995 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_RESET_WRAP, param)) {
Seth Forshee25bded72011-11-07 19:53:36 -0800996 psmouse_err(psmouse, "failed to enter command mode\n");
997 return -1;
998 }
999
1000 if (param[0] != 0x88 && param[1] != 0x07) {
1001 psmouse_dbg(psmouse,
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001002 "unknown response while entering command mode\n");
Seth Forshee25bded72011-11-07 19:53:36 -08001003 return -1;
1004 }
1005
1006 if (resp)
1007 *resp = param[2];
1008 return 0;
1009}
1010
1011static inline int alps_exit_command_mode(struct psmouse *psmouse)
1012{
1013 struct ps2dev *ps2dev = &psmouse->ps2dev;
1014 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM))
1015 return -1;
1016 return 0;
1017}
1018
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019/*
1020 * For DualPoint devices select the device that should respond to
1021 * subsequent commands. It looks like glidepad is behind stickpointer,
1022 * I'd thought it would be other way around...
1023 */
Seth Forshee25bded72011-11-07 19:53:36 -08001024static int alps_passthrough_mode_v2(struct psmouse *psmouse, bool enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025{
1026 struct ps2dev *ps2dev = &psmouse->ps2dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 int cmd = enable ? PSMOUSE_CMD_SETSCALE21 : PSMOUSE_CMD_SETSCALE11;
1028
1029 if (ps2_command(ps2dev, NULL, cmd) ||
1030 ps2_command(ps2dev, NULL, cmd) ||
1031 ps2_command(ps2dev, NULL, cmd) ||
1032 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1033 return -1;
1034
1035 /* we may get 3 more bytes, just ignore them */
Dmitry Torokhovc6117632005-06-01 02:39:51 -05001036 ps2_drain(ps2dev, 3, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
1038 return 0;
1039}
1040
Seth Forshee25bded72011-11-07 19:53:36 -08001041static int alps_absolute_mode_v1_v2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042{
1043 struct ps2dev *ps2dev = &psmouse->ps2dev;
1044
1045 /* Try ALPS magic knock - 4 disable before enable */
1046 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1047 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1048 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1049 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1050 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
1051 return -1;
1052
1053 /*
1054 * Switch mouse to poll (remote) mode so motion data will not
1055 * get in our way
1056 */
1057 return ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETPOLL);
1058}
1059
1060static int alps_get_status(struct psmouse *psmouse, char *param)
1061{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 /* Get status: 0xF5 0xF5 0xF5 0xE9 */
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001063 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_DISABLE, param))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 return -1;
1065
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 return 0;
1067}
1068
1069/*
1070 * Turn touchpad tapping on or off. The sequences are:
1071 * 0xE9 0xF5 0xF5 0xF3 0x0A to enable,
1072 * 0xE9 0xF5 0xF5 0xE8 0x00 to disable.
1073 * My guess that 0xE9 (GetInfo) is here as a sync point.
1074 * For models that also have stickpointer (DualPoints) its tapping
1075 * is controlled separately (0xE6 0xE6 0xE6 0xF3 0x14|0x0A) but
1076 * we don't fiddle with it.
1077 */
1078static int alps_tap_mode(struct psmouse *psmouse, int enable)
1079{
1080 struct ps2dev *ps2dev = &psmouse->ps2dev;
1081 int cmd = enable ? PSMOUSE_CMD_SETRATE : PSMOUSE_CMD_SETRES;
1082 unsigned char tap_arg = enable ? 0x0A : 0x00;
1083 unsigned char param[4];
1084
1085 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO) ||
1086 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1087 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1088 ps2_command(ps2dev, &tap_arg, cmd))
1089 return -1;
1090
1091 if (alps_get_status(psmouse, param))
1092 return -1;
1093
1094 return 0;
1095}
1096
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001097/*
1098 * alps_poll() - poll the touchpad for current motion packet.
1099 * Used in resync.
1100 */
1101static int alps_poll(struct psmouse *psmouse)
1102{
1103 struct alps_data *priv = psmouse->private;
Seth Forsheeb46615f2011-11-07 19:53:30 -08001104 unsigned char buf[sizeof(psmouse->packet)];
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001105 bool poll_failed;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001106
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001107 if (priv->flags & ALPS_PASS)
Seth Forshee25bded72011-11-07 19:53:36 -08001108 alps_passthrough_mode_v2(psmouse, true);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001109
1110 poll_failed = ps2_command(&psmouse->ps2dev, buf,
1111 PSMOUSE_CMD_POLL | (psmouse->pktsize << 8)) < 0;
1112
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001113 if (priv->flags & ALPS_PASS)
Seth Forshee25bded72011-11-07 19:53:36 -08001114 alps_passthrough_mode_v2(psmouse, false);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001115
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001116 if (poll_failed || (buf[0] & priv->mask0) != priv->byte0)
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001117 return -1;
1118
1119 if ((psmouse->badbyte & 0xc8) == 0x08) {
1120/*
1121 * Poll the track stick ...
1122 */
1123 if (ps2_command(&psmouse->ps2dev, buf, PSMOUSE_CMD_POLL | (3 << 8)))
1124 return -1;
1125 }
1126
1127 memcpy(psmouse->packet, buf, sizeof(buf));
1128 return 0;
1129}
1130
Seth Forshee25bded72011-11-07 19:53:36 -08001131static int alps_hw_init_v1_v2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132{
1133 struct alps_data *priv = psmouse->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001135 if ((priv->flags & ALPS_PASS) &&
Seth Forshee25bded72011-11-07 19:53:36 -08001136 alps_passthrough_mode_v2(psmouse, true)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 return -1;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001138 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001140 if (alps_tap_mode(psmouse, true)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001141 psmouse_warn(psmouse, "Failed to enable hardware tapping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 return -1;
Peter Osterlund963f6262005-07-11 01:08:04 -05001143 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
Seth Forshee25bded72011-11-07 19:53:36 -08001145 if (alps_absolute_mode_v1_v2(psmouse)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001146 psmouse_err(psmouse, "Failed to enable absolute mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 return -1;
1148 }
1149
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001150 if ((priv->flags & ALPS_PASS) &&
Seth Forshee25bded72011-11-07 19:53:36 -08001151 alps_passthrough_mode_v2(psmouse, false)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 return -1;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04001155 /* ALPS needs stream mode, otherwise it won't report any data */
1156 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001157 psmouse_err(psmouse, "Failed to enable stream mode\n");
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04001158 return -1;
1159 }
1160
1161 return 0;
1162}
1163
Seth Forshee25bded72011-11-07 19:53:36 -08001164/*
1165 * Enable or disable passthrough mode to the trackstick. Must be in
1166 * command mode when calling this function.
1167 */
1168static int alps_passthrough_mode_v3(struct psmouse *psmouse, bool enable)
1169{
1170 int reg_val;
1171
1172 reg_val = alps_command_mode_read_reg(psmouse, 0x0008);
1173 if (reg_val == -1)
1174 return -1;
1175
1176 if (enable)
1177 reg_val |= 0x01;
1178 else
1179 reg_val &= ~0x01;
1180
1181 if (__alps_command_mode_write_reg(psmouse, reg_val))
1182 return -1;
1183
1184 return 0;
1185}
1186
1187/* Must be in command mode when calling this function */
1188static int alps_absolute_mode_v3(struct psmouse *psmouse)
1189{
1190 int reg_val;
1191
1192 reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
1193 if (reg_val == -1)
1194 return -1;
1195
1196 reg_val |= 0x06;
1197 if (__alps_command_mode_write_reg(psmouse, reg_val))
1198 return -1;
1199
1200 return 0;
1201}
1202
1203static int alps_hw_init_v3(struct psmouse *psmouse)
1204{
1205 struct alps_data *priv = psmouse->private;
1206 struct ps2dev *ps2dev = &psmouse->ps2dev;
1207 int reg_val;
1208 unsigned char param[4];
1209
1210 priv->nibble_commands = alps_v3_nibble_commands;
1211 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
1212
1213 if (alps_enter_command_mode(psmouse, NULL))
1214 goto error;
1215
1216 /* Check for trackstick */
1217 reg_val = alps_command_mode_read_reg(psmouse, 0x0008);
1218 if (reg_val == -1)
1219 goto error;
1220 if (reg_val & 0x80) {
1221 if (alps_passthrough_mode_v3(psmouse, true))
1222 goto error;
1223 if (alps_exit_command_mode(psmouse))
1224 goto error;
1225
1226 /*
1227 * E7 report for the trackstick
1228 *
1229 * There have been reports of failures to seem to trace back
1230 * to the above trackstick check failing. When these occur
1231 * this E7 report fails, so when that happens we continue
1232 * with the assumption that there isn't a trackstick after
1233 * all.
1234 */
1235 param[0] = 0x64;
1236 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1237 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1238 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1239 ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO)) {
1240 psmouse_warn(psmouse, "trackstick E7 report failed\n");
1241 } else {
1242 psmouse_dbg(psmouse,
1243 "trackstick E7 report: %2.2x %2.2x %2.2x\n",
1244 param[0], param[1], param[2]);
1245
1246 /*
1247 * Not sure what this does, but it is absolutely
1248 * essential. Without it, the touchpad does not
1249 * work at all and the trackstick just emits normal
1250 * PS/2 packets.
1251 */
1252 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1253 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1254 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1255 alps_command_mode_send_nibble(psmouse, 0x9) ||
1256 alps_command_mode_send_nibble(psmouse, 0x4)) {
1257 psmouse_err(psmouse,
1258 "Error sending magic E6 sequence\n");
1259 goto error_passthrough;
1260 }
1261 }
1262
1263 if (alps_enter_command_mode(psmouse, NULL))
1264 goto error_passthrough;
1265 if (alps_passthrough_mode_v3(psmouse, false))
1266 goto error;
1267 }
1268
1269 if (alps_absolute_mode_v3(psmouse)) {
1270 psmouse_err(psmouse, "Failed to enter absolute mode\n");
1271 goto error;
1272 }
1273
1274 reg_val = alps_command_mode_read_reg(psmouse, 0x0006);
1275 if (reg_val == -1)
1276 goto error;
1277 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
1278 goto error;
1279
1280 reg_val = alps_command_mode_read_reg(psmouse, 0x0007);
1281 if (reg_val == -1)
1282 goto error;
1283 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
1284 goto error;
1285
1286 if (alps_command_mode_read_reg(psmouse, 0x0144) == -1)
1287 goto error;
1288 if (__alps_command_mode_write_reg(psmouse, 0x04))
1289 goto error;
1290
1291 if (alps_command_mode_read_reg(psmouse, 0x0159) == -1)
1292 goto error;
1293 if (__alps_command_mode_write_reg(psmouse, 0x03))
1294 goto error;
1295
1296 if (alps_command_mode_read_reg(psmouse, 0x0163) == -1)
1297 goto error;
1298 if (alps_command_mode_write_reg(psmouse, 0x0163, 0x03))
1299 goto error;
1300
1301 if (alps_command_mode_read_reg(psmouse, 0x0162) == -1)
1302 goto error;
1303 if (alps_command_mode_write_reg(psmouse, 0x0162, 0x04))
1304 goto error;
1305
1306 /*
1307 * This ensures the trackstick packets are in the format
1308 * supported by this driver. If bit 1 isn't set the packet
1309 * format is different.
1310 */
1311 if (alps_command_mode_write_reg(psmouse, 0x0008, 0x82))
1312 goto error;
1313
1314 alps_exit_command_mode(psmouse);
1315
1316 /* Set rate and enable data reporting */
1317 param[0] = 0x64;
1318 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
1319 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
1320 psmouse_err(psmouse, "Failed to enable data reporting\n");
1321 return -1;
1322 }
1323
1324 return 0;
1325
1326error_passthrough:
1327 /* Something failed while in passthrough mode, so try to get out */
1328 if (!alps_enter_command_mode(psmouse, NULL))
1329 alps_passthrough_mode_v3(psmouse, false);
1330error:
1331 /*
1332 * Leaving the touchpad in command mode will essentially render
1333 * it unusable until the machine reboots, so exit it here just
1334 * to be safe
1335 */
1336 alps_exit_command_mode(psmouse);
1337 return -1;
1338}
1339
1340/* Must be in command mode when calling this function */
1341static int alps_absolute_mode_v4(struct psmouse *psmouse)
1342{
1343 int reg_val;
1344
1345 reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
1346 if (reg_val == -1)
1347 return -1;
1348
1349 reg_val |= 0x02;
1350 if (__alps_command_mode_write_reg(psmouse, reg_val))
1351 return -1;
1352
1353 return 0;
1354}
1355
1356static int alps_hw_init_v4(struct psmouse *psmouse)
1357{
1358 struct alps_data *priv = psmouse->private;
1359 struct ps2dev *ps2dev = &psmouse->ps2dev;
1360 unsigned char param[4];
1361
1362 priv->nibble_commands = alps_v4_nibble_commands;
1363 priv->addr_command = PSMOUSE_CMD_DISABLE;
1364
1365 if (alps_enter_command_mode(psmouse, NULL))
1366 goto error;
1367
1368 if (alps_absolute_mode_v4(psmouse)) {
1369 psmouse_err(psmouse, "Failed to enter absolute mode\n");
1370 goto error;
1371 }
1372
1373 if (alps_command_mode_write_reg(psmouse, 0x0007, 0x8c))
1374 goto error;
1375
1376 if (alps_command_mode_write_reg(psmouse, 0x0149, 0x03))
1377 goto error;
1378
1379 if (alps_command_mode_write_reg(psmouse, 0x0160, 0x03))
1380 goto error;
1381
1382 if (alps_command_mode_write_reg(psmouse, 0x017f, 0x15))
1383 goto error;
1384
1385 if (alps_command_mode_write_reg(psmouse, 0x0151, 0x01))
1386 goto error;
1387
1388 if (alps_command_mode_write_reg(psmouse, 0x0168, 0x03))
1389 goto error;
1390
1391 if (alps_command_mode_write_reg(psmouse, 0x014a, 0x03))
1392 goto error;
1393
1394 if (alps_command_mode_write_reg(psmouse, 0x0161, 0x03))
1395 goto error;
1396
1397 alps_exit_command_mode(psmouse);
1398
1399 /*
1400 * This sequence changes the output from a 9-byte to an
1401 * 8-byte format. All the same data seems to be present,
1402 * just in a more compact format.
1403 */
1404 param[0] = 0xc8;
1405 param[1] = 0x64;
1406 param[2] = 0x50;
1407 if (ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
1408 ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE) ||
1409 ps2_command(ps2dev, &param[2], PSMOUSE_CMD_SETRATE) ||
1410 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
1411 return -1;
1412
1413 /* Set rate and enable data reporting */
1414 param[0] = 0x64;
1415 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
1416 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
1417 psmouse_err(psmouse, "Failed to enable data reporting\n");
1418 return -1;
1419 }
1420
1421 return 0;
1422
1423error:
1424 /*
1425 * Leaving the touchpad in command mode will essentially render
1426 * it unusable until the machine reboots, so exit it here just
1427 * to be safe
1428 */
1429 alps_exit_command_mode(psmouse);
1430 return -1;
1431}
1432
1433static int alps_hw_init(struct psmouse *psmouse)
1434{
1435 struct alps_data *priv = psmouse->private;
Seth Forshee25bded72011-11-07 19:53:36 -08001436 int ret = -1;
1437
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001438 switch (priv->proto_version) {
Seth Forshee25bded72011-11-07 19:53:36 -08001439 case ALPS_PROTO_V1:
1440 case ALPS_PROTO_V2:
1441 ret = alps_hw_init_v1_v2(psmouse);
1442 break;
1443 case ALPS_PROTO_V3:
1444 ret = alps_hw_init_v3(psmouse);
1445 break;
1446 case ALPS_PROTO_V4:
1447 ret = alps_hw_init_v4(psmouse);
1448 break;
1449 }
1450
1451 return ret;
1452}
1453
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08001454static const struct alps_model_info *alps_get_model(struct psmouse *psmouse, int *version)
1455{
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08001456 static const unsigned char rates[] = { 0, 10, 20, 40, 60, 80, 100, 200 };
1457 unsigned char param[4];
1458 const struct alps_model_info *model = NULL;
1459 int i;
1460
1461 /*
1462 * First try "E6 report".
1463 * ALPS should return 0,0,10 or 0,0,100 if no buttons are pressed.
1464 * The bits 0-2 of the first byte will be 1s if some buttons are
1465 * pressed.
1466 */
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001467 if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES, PSMOUSE_CMD_SETSCALE11,
1468 param))
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08001469 return NULL;
1470
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08001471 if ((param[0] & 0xf8) != 0 || param[1] != 0 ||
1472 (param[2] != 10 && param[2] != 100))
1473 return NULL;
1474
1475 /*
1476 * Now try "E7 report". Allowed responses are in
1477 * alps_model_data[].signature
1478 */
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001479 if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES, PSMOUSE_CMD_SETSCALE21,
1480 param))
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08001481 return NULL;
1482
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08001483 if (version) {
1484 for (i = 0; i < ARRAY_SIZE(rates) && param[2] != rates[i]; i++)
1485 /* empty */;
1486 *version = (param[0] << 8) | (param[1] << 4) | i;
1487 }
1488
1489 for (i = 0; i < ARRAY_SIZE(alps_model_data); i++) {
1490 if (!memcmp(param, alps_model_data[i].signature,
1491 sizeof(alps_model_data[i].signature))) {
1492 model = alps_model_data + i;
1493 break;
1494 }
1495 }
1496
1497 if (model && model->proto_version > ALPS_PROTO_V2) {
1498 /*
1499 * Need to check command mode response to identify
1500 * model
1501 */
1502 model = NULL;
1503 if (alps_enter_command_mode(psmouse, param)) {
1504 psmouse_warn(psmouse,
1505 "touchpad failed to enter command mode\n");
1506 } else {
1507 for (i = 0; i < ARRAY_SIZE(alps_model_data); i++) {
1508 if (alps_model_data[i].proto_version > ALPS_PROTO_V2 &&
1509 alps_model_data[i].command_mode_resp == param[0]) {
1510 model = alps_model_data + i;
1511 break;
1512 }
1513 }
1514 alps_exit_command_mode(psmouse);
1515
1516 if (!model)
1517 psmouse_dbg(psmouse,
1518 "Unknown command mode response %2.2x\n",
1519 param[0]);
1520 }
1521 }
1522
1523 return model;
1524}
1525
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04001526static int alps_reconnect(struct psmouse *psmouse)
1527{
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08001528 const struct alps_model_info *model;
1529
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04001530 psmouse_reset(psmouse);
1531
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08001532 model = alps_get_model(psmouse, NULL);
1533 if (!model)
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04001534 return -1;
1535
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08001536 return alps_hw_init(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537}
1538
1539static void alps_disconnect(struct psmouse *psmouse)
1540{
1541 struct alps_data *priv = psmouse->private;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001542
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 psmouse_reset(psmouse);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001544 del_timer_sync(&priv->timer);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001545 input_unregister_device(priv->dev2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 kfree(priv);
1547}
1548
1549int alps_init(struct psmouse *psmouse)
1550{
1551 struct alps_data *priv;
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08001552 const struct alps_model_info *model;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001553 struct input_dev *dev1 = psmouse->dev, *dev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 int version;
1555
Dmitry Torokhovf42649e2007-04-12 01:31:13 -04001556 priv = kzalloc(sizeof(struct alps_data), GFP_KERNEL);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001557 dev2 = input_allocate_device();
1558 if (!priv || !dev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 goto init_fail;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001560
1561 priv->dev2 = dev2;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001562 setup_timer(&priv->timer, alps_flush_packet, (unsigned long)psmouse);
1563
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04001564 psmouse->private = priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
Seth Forshee25bded72011-11-07 19:53:36 -08001566 psmouse_reset(psmouse);
1567
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08001568 model = alps_get_model(psmouse, &version);
1569 if (!model)
1570 goto init_fail;
1571
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001572 priv->proto_version = model->proto_version;
1573 priv->byte0 = model->byte0;
1574 priv->mask0 = model->mask0;
1575 priv->flags = model->flags;
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08001576
1577 if (alps_hw_init(psmouse))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 goto init_fail;
1579
Dmitry Torokhov7105d2e2009-12-11 23:54:54 -08001580 /*
1581 * Undo part of setup done for us by psmouse core since touchpad
1582 * is not a relative device.
1583 */
1584 __clear_bit(EV_REL, dev1->evbit);
1585 __clear_bit(REL_X, dev1->relbit);
1586 __clear_bit(REL_Y, dev1->relbit);
1587
1588 /*
1589 * Now set up our capabilities.
1590 */
Jiri Slaby7b19ada2007-10-18 23:40:32 -07001591 dev1->evbit[BIT_WORD(EV_KEY)] |= BIT_MASK(EV_KEY);
1592 dev1->keybit[BIT_WORD(BTN_TOUCH)] |= BIT_MASK(BTN_TOUCH);
1593 dev1->keybit[BIT_WORD(BTN_TOOL_FINGER)] |= BIT_MASK(BTN_TOOL_FINGER);
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08001594 dev1->keybit[BIT_WORD(BTN_LEFT)] |=
1595 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
Jiri Slaby7b19ada2007-10-18 23:40:32 -07001597 dev1->evbit[BIT_WORD(EV_ABS)] |= BIT_MASK(EV_ABS);
Seth Forshee25bded72011-11-07 19:53:36 -08001598
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001599 switch (priv->proto_version) {
Seth Forshee25bded72011-11-07 19:53:36 -08001600 case ALPS_PROTO_V1:
1601 case ALPS_PROTO_V2:
1602 input_set_abs_params(dev1, ABS_X, 0, 1023, 0, 0);
1603 input_set_abs_params(dev1, ABS_Y, 0, 767, 0, 0);
1604 break;
1605 case ALPS_PROTO_V3:
George Pantalos3b7e09f2012-05-10 22:31:59 -07001606 case ALPS_PROTO_V4:
Seth Forshee01ce6612011-11-07 19:54:13 -08001607 set_bit(INPUT_PROP_SEMI_MT, dev1->propbit);
Henrik Rydbergb4adbbe2012-08-11 22:07:55 +02001608 input_mt_init_slots(dev1, 2, 0);
Seth Forshee01ce6612011-11-07 19:54:13 -08001609 input_set_abs_params(dev1, ABS_MT_POSITION_X, 0, ALPS_V3_X_MAX, 0, 0);
1610 input_set_abs_params(dev1, ABS_MT_POSITION_Y, 0, ALPS_V3_Y_MAX, 0, 0);
1611
1612 set_bit(BTN_TOOL_DOUBLETAP, dev1->keybit);
1613 set_bit(BTN_TOOL_TRIPLETAP, dev1->keybit);
1614 set_bit(BTN_TOOL_QUADTAP, dev1->keybit);
George Pantalos3b7e09f2012-05-10 22:31:59 -07001615
Seth Forshee01ce6612011-11-07 19:54:13 -08001616 input_set_abs_params(dev1, ABS_X, 0, ALPS_V3_X_MAX, 0, 0);
1617 input_set_abs_params(dev1, ABS_Y, 0, ALPS_V3_Y_MAX, 0, 0);
Seth Forshee25bded72011-11-07 19:53:36 -08001618 break;
1619 }
1620
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001621 input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001623 if (priv->flags & ALPS_WHEEL) {
Jiri Slaby7b19ada2007-10-18 23:40:32 -07001624 dev1->evbit[BIT_WORD(EV_REL)] |= BIT_MASK(EV_REL);
1625 dev1->relbit[BIT_WORD(REL_WHEEL)] |= BIT_MASK(REL_WHEEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 }
1627
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001628 if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
Jiri Slaby7b19ada2007-10-18 23:40:32 -07001629 dev1->keybit[BIT_WORD(BTN_FORWARD)] |= BIT_MASK(BTN_FORWARD);
1630 dev1->keybit[BIT_WORD(BTN_BACK)] |= BIT_MASK(BTN_BACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 }
1632
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001633 if (priv->flags & ALPS_FOUR_BUTTONS) {
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08001634 dev1->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_0);
1635 dev1->keybit[BIT_WORD(BTN_1)] |= BIT_MASK(BTN_1);
1636 dev1->keybit[BIT_WORD(BTN_2)] |= BIT_MASK(BTN_2);
1637 dev1->keybit[BIT_WORD(BTN_3)] |= BIT_MASK(BTN_3);
1638 } else {
1639 dev1->keybit[BIT_WORD(BTN_MIDDLE)] |= BIT_MASK(BTN_MIDDLE);
1640 }
1641
Dmitry Torokhov08ffce42006-06-26 01:45:10 -04001642 snprintf(priv->phys, sizeof(priv->phys), "%s/input1", psmouse->ps2dev.serio->phys);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001643 dev2->phys = priv->phys;
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001644 dev2->name = (priv->flags & ALPS_DUALPOINT) ?
1645 "DualPoint Stick" : "PS/2 Mouse";
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001646 dev2->id.bustype = BUS_I8042;
1647 dev2->id.vendor = 0x0002;
1648 dev2->id.product = PSMOUSE_ALPS;
1649 dev2->id.version = 0x0000;
Dmitry Torokhov1db3a342008-03-18 00:29:18 -04001650 dev2->dev.parent = &psmouse->ps2dev.serio->dev;
Dmitry Torokhov968ac842005-05-29 02:28:29 -05001651
Jiri Slaby7b19ada2007-10-18 23:40:32 -07001652 dev2->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08001653 dev2->relbit[BIT_WORD(REL_X)] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
1654 dev2->keybit[BIT_WORD(BTN_LEFT)] =
1655 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_MIDDLE) | BIT_MASK(BTN_RIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
Dmitry Torokhovf42649e2007-04-12 01:31:13 -04001657 if (input_register_device(priv->dev2))
1658 goto init_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
1660 psmouse->protocol_handler = alps_process_byte;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001661 psmouse->poll = alps_poll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 psmouse->disconnect = alps_disconnect;
1663 psmouse->reconnect = alps_reconnect;
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001664 psmouse->pktsize = priv->proto_version == ALPS_PROTO_V4 ? 8 : 6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001666 /* We are having trouble resyncing ALPS touchpads so disable it for now */
1667 psmouse->resync_time = 0;
1668
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 return 0;
1670
1671init_fail:
Dmitry Torokhovf42649e2007-04-12 01:31:13 -04001672 psmouse_reset(psmouse);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001673 input_free_device(dev2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 kfree(priv);
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04001675 psmouse->private = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 return -1;
1677}
1678
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001679int alps_detect(struct psmouse *psmouse, bool set_properties)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680{
1681 int version;
Helge Dellere38de672006-09-10 21:54:39 -04001682 const struct alps_model_info *model;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
Dmitry Torokhovf42649e2007-04-12 01:31:13 -04001684 model = alps_get_model(psmouse, &version);
1685 if (!model)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 return -1;
1687
1688 if (set_properties) {
1689 psmouse->vendor = "ALPS";
Dmitry Torokhov968ac842005-05-29 02:28:29 -05001690 psmouse->name = model->flags & ALPS_DUALPOINT ?
1691 "DualPoint TouchPad" : "GlidePoint";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 psmouse->model = version;
1693 }
1694 return 0;
1695}
1696