blob: f16fe7c7d215e65c5d336d9b5341ab2f9e4a6c49 [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 */
30#define ALPS_CMD_NIBBLE_10 0x01f2
31
Kevin Cernekeecd401202013-02-13 22:28:07 -080032#define ALPS_REG_BASE_RUSHMORE 0xc2c0
33#define ALPS_REG_BASE_PINNACLE 0x0000
34
Seth Forshee25bded72011-11-07 19:53:36 -080035static const struct alps_nibble_commands alps_v3_nibble_commands[] = {
36 { PSMOUSE_CMD_SETPOLL, 0x00 }, /* 0 */
37 { PSMOUSE_CMD_RESET_DIS, 0x00 }, /* 1 */
38 { PSMOUSE_CMD_SETSCALE21, 0x00 }, /* 2 */
39 { PSMOUSE_CMD_SETRATE, 0x0a }, /* 3 */
40 { PSMOUSE_CMD_SETRATE, 0x14 }, /* 4 */
41 { PSMOUSE_CMD_SETRATE, 0x28 }, /* 5 */
42 { PSMOUSE_CMD_SETRATE, 0x3c }, /* 6 */
43 { PSMOUSE_CMD_SETRATE, 0x50 }, /* 7 */
44 { PSMOUSE_CMD_SETRATE, 0x64 }, /* 8 */
45 { PSMOUSE_CMD_SETRATE, 0xc8 }, /* 9 */
46 { ALPS_CMD_NIBBLE_10, 0x00 }, /* a */
47 { PSMOUSE_CMD_SETRES, 0x00 }, /* b */
48 { PSMOUSE_CMD_SETRES, 0x01 }, /* c */
49 { PSMOUSE_CMD_SETRES, 0x02 }, /* d */
50 { PSMOUSE_CMD_SETRES, 0x03 }, /* e */
51 { PSMOUSE_CMD_SETSCALE11, 0x00 }, /* f */
52};
53
54static const struct alps_nibble_commands alps_v4_nibble_commands[] = {
55 { PSMOUSE_CMD_ENABLE, 0x00 }, /* 0 */
56 { PSMOUSE_CMD_RESET_DIS, 0x00 }, /* 1 */
57 { PSMOUSE_CMD_SETSCALE21, 0x00 }, /* 2 */
58 { PSMOUSE_CMD_SETRATE, 0x0a }, /* 3 */
59 { PSMOUSE_CMD_SETRATE, 0x14 }, /* 4 */
60 { PSMOUSE_CMD_SETRATE, 0x28 }, /* 5 */
61 { PSMOUSE_CMD_SETRATE, 0x3c }, /* 6 */
62 { PSMOUSE_CMD_SETRATE, 0x50 }, /* 7 */
63 { PSMOUSE_CMD_SETRATE, 0x64 }, /* 8 */
64 { PSMOUSE_CMD_SETRATE, 0xc8 }, /* 9 */
65 { ALPS_CMD_NIBBLE_10, 0x00 }, /* a */
66 { PSMOUSE_CMD_SETRES, 0x00 }, /* b */
67 { PSMOUSE_CMD_SETRES, 0x01 }, /* c */
68 { PSMOUSE_CMD_SETRES, 0x02 }, /* d */
69 { PSMOUSE_CMD_SETRES, 0x03 }, /* e */
70 { PSMOUSE_CMD_SETSCALE11, 0x00 }, /* f */
71};
72
Yunkang Tang95f75e92013-12-01 22:33:52 -080073static const struct alps_nibble_commands alps_v6_nibble_commands[] = {
74 { PSMOUSE_CMD_ENABLE, 0x00 }, /* 0 */
75 { PSMOUSE_CMD_SETRATE, 0x0a }, /* 1 */
76 { PSMOUSE_CMD_SETRATE, 0x14 }, /* 2 */
77 { PSMOUSE_CMD_SETRATE, 0x28 }, /* 3 */
78 { PSMOUSE_CMD_SETRATE, 0x3c }, /* 4 */
79 { PSMOUSE_CMD_SETRATE, 0x50 }, /* 5 */
80 { PSMOUSE_CMD_SETRATE, 0x64 }, /* 6 */
81 { PSMOUSE_CMD_SETRATE, 0xc8 }, /* 7 */
82 { PSMOUSE_CMD_GETID, 0x00 }, /* 8 */
83 { PSMOUSE_CMD_GETINFO, 0x00 }, /* 9 */
84 { PSMOUSE_CMD_SETRES, 0x00 }, /* a */
85 { PSMOUSE_CMD_SETRES, 0x01 }, /* b */
86 { PSMOUSE_CMD_SETRES, 0x02 }, /* c */
87 { PSMOUSE_CMD_SETRES, 0x03 }, /* d */
88 { PSMOUSE_CMD_SETSCALE21, 0x00 }, /* e */
89 { PSMOUSE_CMD_SETSCALE11, 0x00 }, /* f */
90};
91
Seth Forshee25bded72011-11-07 19:53:36 -080092
Maxim Levitsky71bb21b2009-11-16 22:12:22 -080093#define ALPS_DUALPOINT 0x02 /* touchpad has trackstick */
94#define ALPS_PASS 0x04 /* device has a pass-through port */
95
96#define ALPS_WHEEL 0x08 /* hardware wheel present */
97#define ALPS_FW_BK_1 0x10 /* front & back buttons present */
98#define ALPS_FW_BK_2 0x20 /* front & back buttons present */
99#define ALPS_FOUR_BUTTONS 0x40 /* 4 direction button present */
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800100#define ALPS_PS2_INTERLEAVED 0x80 /* 3-byte PS/2 packet interleaved with
101 6-byte ALPS packet */
Hans de Goede40e8f532014-07-25 22:37:15 -0700102#define ALPS_IS_RUSHMORE 0x100 /* device is a rushmore */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Helge Dellere38de672006-09-10 21:54:39 -0400104static const struct alps_model_info alps_model_data[] = {
Seth Forshee25bded72011-11-07 19:53:36 -0800105 { { 0x32, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT }, /* Toshiba Salellite Pro M10 */
106 { { 0x33, 0x02, 0x0a }, 0x00, ALPS_PROTO_V1, 0x88, 0xf8, 0 }, /* UMAX-530T */
107 { { 0x53, 0x02, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
108 { { 0x53, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
109 { { 0x60, 0x03, 0xc8 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 }, /* HP ze1115 */
110 { { 0x63, 0x02, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
111 { { 0x63, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
112 { { 0x63, 0x02, 0x28 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 }, /* Fujitsu Siemens S6010 */
113 { { 0x63, 0x02, 0x3c }, 0x00, ALPS_PROTO_V2, 0x8f, 0x8f, ALPS_WHEEL }, /* Toshiba Satellite S2400-103 */
114 { { 0x63, 0x02, 0x50 }, 0x00, ALPS_PROTO_V2, 0xef, 0xef, ALPS_FW_BK_1 }, /* NEC Versa L320 */
115 { { 0x63, 0x02, 0x64 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
116 { { 0x63, 0x03, 0xc8 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT }, /* Dell Latitude D800 */
117 { { 0x73, 0x00, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_DUALPOINT }, /* ThinkPad R61 8918-5QG */
118 { { 0x73, 0x02, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
119 { { 0x73, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 }, /* Ahtec Laptop */
120 { { 0x20, 0x02, 0x0e }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT }, /* XXX */
121 { { 0x22, 0x02, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT },
122 { { 0x22, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xff, 0xff, ALPS_PASS | ALPS_DUALPOINT }, /* Dell Latitude D600 */
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800123 /* Dell Latitude E5500, E6400, E6500, Precision M4400 */
Seth Forshee25bded72011-11-07 19:53:36 -0800124 { { 0x62, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xcf, 0xcf,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800125 ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED },
Yunkang Tang95f75e92013-12-01 22:33:52 -0800126 { { 0x73, 0x00, 0x14 }, 0x00, ALPS_PROTO_V6, 0xff, 0xff, ALPS_DUALPOINT }, /* Dell XT2 */
Seth Forshee25bded72011-11-07 19:53:36 -0800127 { { 0x73, 0x02, 0x50 }, 0x00, ALPS_PROTO_V2, 0xcf, 0xcf, ALPS_FOUR_BUTTONS }, /* Dell Vostro 1400 */
128 { { 0x52, 0x01, 0x14 }, 0x00, ALPS_PROTO_V2, 0xff, 0xff,
129 ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED }, /* Toshiba Tecra A11-11L */
Seth Forshee25bded72011-11-07 19:53:36 -0800130 { { 0x73, 0x02, 0x64 }, 0x8a, ALPS_PROTO_V4, 0x8f, 0x8f, 0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131};
132
Kevin Cernekee24af5cb2013-02-13 22:22:08 -0800133static void alps_set_abs_params_st(struct alps_data *priv,
134 struct input_dev *dev1);
135static void alps_set_abs_params_mt(struct alps_data *priv,
136 struct input_dev *dev1);
137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138/*
139 * XXX - this entry is suspicious. First byte has zero lower nibble,
140 * which is what a normal mouse would report. Also, the value 0x0e
141 * isn't valid per PS/2 spec.
142 */
143
Seth Forsheed4b347b2011-11-07 19:53:15 -0800144/* Packet formats are described in Documentation/input/alps.txt */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800146static bool alps_is_valid_first_byte(struct alps_data *priv,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800147 unsigned char data)
148{
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800149 return (data & priv->mask0) == priv->byte0;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800150}
151
152static void alps_report_buttons(struct psmouse *psmouse,
153 struct input_dev *dev1, struct input_dev *dev2,
154 int left, int right, int middle)
155{
Martin Buckc91ed052010-03-13 22:23:58 -0800156 struct input_dev *dev;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800157
Martin Buckc91ed052010-03-13 22:23:58 -0800158 /*
159 * If shared button has already been reported on the
160 * other device (dev2) then this event should be also
161 * sent through that device.
162 */
163 dev = test_bit(BTN_LEFT, dev2->key) ? dev2 : dev1;
164 input_report_key(dev, BTN_LEFT, left);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800165
Martin Buckc91ed052010-03-13 22:23:58 -0800166 dev = test_bit(BTN_RIGHT, dev2->key) ? dev2 : dev1;
167 input_report_key(dev, BTN_RIGHT, right);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800168
Martin Buckc91ed052010-03-13 22:23:58 -0800169 dev = test_bit(BTN_MIDDLE, dev2->key) ? dev2 : dev1;
170 input_report_key(dev, BTN_MIDDLE, middle);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800171
Martin Buckc91ed052010-03-13 22:23:58 -0800172 /*
173 * Sync the _other_ device now, we'll do the first
174 * device later once we report the rest of the events.
175 */
176 input_sync(dev2);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800177}
178
Seth Forshee25bded72011-11-07 19:53:36 -0800179static void alps_process_packet_v1_v2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
181 struct alps_data *priv = psmouse->private;
182 unsigned char *packet = psmouse->packet;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500183 struct input_dev *dev = psmouse->dev;
184 struct input_dev *dev2 = priv->dev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 int x, y, z, ges, fin, left, right, middle;
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500186 int back = 0, forward = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800188 if (priv->proto_version == ALPS_PROTO_V1) {
Yotam Medinid2f40122006-05-29 23:30:36 -0400189 left = packet[2] & 0x10;
190 right = packet[2] & 0x08;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 middle = 0;
192 x = packet[1] | ((packet[0] & 0x07) << 7);
193 y = packet[4] | ((packet[3] & 0x07) << 7);
194 z = packet[5];
195 } else {
196 left = packet[3] & 1;
197 right = packet[3] & 2;
198 middle = packet[3] & 4;
199 x = packet[1] | ((packet[2] & 0x78) << (7 - 3));
200 y = packet[4] | ((packet[3] & 0x70) << (7 - 4));
201 z = packet[5];
202 }
203
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800204 if (priv->flags & ALPS_FW_BK_1) {
Laszlo Kajan3c00bb92008-03-18 00:39:55 -0400205 back = packet[0] & 0x10;
206 forward = packet[2] & 4;
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500207 }
208
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800209 if (priv->flags & ALPS_FW_BK_2) {
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500210 back = packet[3] & 4;
211 forward = packet[2] & 4;
212 if ((middle = forward && back))
213 forward = back = 0;
214 }
215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 ges = packet[2] & 1;
217 fin = packet[2] & 2;
218
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800219 if ((priv->flags & ALPS_DUALPOINT) && z == 127) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 input_report_rel(dev2, REL_X, (x > 383 ? (x - 768) : x));
221 input_report_rel(dev2, REL_Y, -(y > 255 ? (y - 512) : y));
Ulrich Dangeld7ed5d82009-06-11 00:15:09 -0700222
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800223 alps_report_buttons(psmouse, dev2, dev, left, right, middle);
Ulrich Dangeld7ed5d82009-06-11 00:15:09 -0700224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 input_sync(dev2);
226 return;
227 }
228
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800229 alps_report_buttons(psmouse, dev, dev2, left, right, middle);
Ulrich Dangeld7ed5d82009-06-11 00:15:09 -0700230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 /* Convert hardware tap to a reasonable Z value */
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800232 if (ges && !fin)
233 z = 40;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
235 /*
236 * A "tap and drag" operation is reported by the hardware as a transition
237 * from (!fin && ges) to (fin && ges). This should be translated to the
238 * sequence Z>0, Z==0, Z>0, so the Z==0 event has to be generated manually.
239 */
240 if (ges && fin && !priv->prev_fin) {
241 input_report_abs(dev, ABS_X, x);
242 input_report_abs(dev, ABS_Y, y);
243 input_report_abs(dev, ABS_PRESSURE, 0);
244 input_report_key(dev, BTN_TOOL_FINGER, 0);
245 input_sync(dev);
246 }
247 priv->prev_fin = fin;
248
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800249 if (z > 30)
250 input_report_key(dev, BTN_TOUCH, 1);
251 if (z < 25)
252 input_report_key(dev, BTN_TOUCH, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254 if (z > 0) {
255 input_report_abs(dev, ABS_X, x);
256 input_report_abs(dev, ABS_Y, y);
257 }
258
259 input_report_abs(dev, ABS_PRESSURE, z);
260 input_report_key(dev, BTN_TOOL_FINGER, z > 0);
261
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800262 if (priv->flags & ALPS_WHEEL)
Vojtech Pavlike6c047b2005-09-04 01:40:43 -0500263 input_report_rel(dev, REL_WHEEL, ((packet[2] << 1) & 0x08) - ((packet[0] >> 4) & 0x07));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800265 if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500266 input_report_key(dev, BTN_FORWARD, forward);
267 input_report_key(dev, BTN_BACK, back);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 }
269
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800270 if (priv->flags & ALPS_FOUR_BUTTONS) {
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800271 input_report_key(dev, BTN_0, packet[2] & 4);
272 input_report_key(dev, BTN_1, packet[0] & 0x10);
273 input_report_key(dev, BTN_2, packet[3] & 4);
274 input_report_key(dev, BTN_3, packet[0] & 0x20);
275 }
276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 input_sync(dev);
278}
279
Seth Forshee01ce6612011-11-07 19:54:13 -0800280/*
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800281 * Process bitmap data for V5 protocols. Return value is null.
282 *
283 * The bitmaps don't have enough data to track fingers, so this function
284 * only generates points representing a bounding box of at most two contacts.
Hans de Goede02d04252014-07-25 22:43:35 -0700285 * These two points are returned in fields->mt.
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800286 */
287static void alps_process_bitmap_dolphin(struct alps_data *priv,
Hans de Goede02d04252014-07-25 22:43:35 -0700288 struct alps_fields *fields)
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800289{
290 int box_middle_x, box_middle_y;
291 unsigned int x_map, y_map;
292 unsigned char start_bit, end_bit;
293 unsigned char x_msb, x_lsb, y_msb, y_lsb;
294
295 x_map = fields->x_map;
296 y_map = fields->y_map;
297
298 if (!x_map || !y_map)
299 return;
300
301 /* Get Most-significant and Least-significant bit */
302 x_msb = fls(x_map);
303 x_lsb = ffs(x_map);
304 y_msb = fls(y_map);
305 y_lsb = ffs(y_map);
306
307 /* Most-significant bit should never exceed max sensor line number */
308 if (x_msb > priv->x_bits || y_msb > priv->y_bits)
309 return;
310
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800311 if (fields->fingers > 1) {
312 start_bit = priv->x_bits - x_msb;
313 end_bit = priv->x_bits - x_lsb;
314 box_middle_x = (priv->x_max * (start_bit + end_bit)) /
315 (2 * (priv->x_bits - 1));
316
317 start_bit = y_lsb - 1;
318 end_bit = y_msb - 1;
319 box_middle_y = (priv->y_max * (start_bit + end_bit)) /
320 (2 * (priv->y_bits - 1));
Hans de Goede02d04252014-07-25 22:43:35 -0700321 fields->mt[0] = fields->st;
322 fields->mt[1].x = 2 * box_middle_x - fields->mt[0].x;
323 fields->mt[1].y = 2 * box_middle_y - fields->mt[0].y;
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800324 }
325}
326
Hans de Goede036e6c72014-07-25 22:38:51 -0700327static void alps_get_bitmap_points(unsigned int map,
328 struct alps_bitmap_point *low,
329 struct alps_bitmap_point *high,
330 int *fingers)
331{
332 struct alps_bitmap_point *point;
333 int i, bit, prev_bit = 0;
334
335 point = low;
336 for (i = 0; map != 0; i++, map >>= 1) {
337 bit = map & 1;
338 if (bit) {
339 if (!prev_bit) {
340 point->start_bit = i;
Hans de Goede105affb2014-07-25 22:41:51 -0700341 point->num_bits = 0;
Hans de Goede036e6c72014-07-25 22:38:51 -0700342 (*fingers)++;
343 }
344 point->num_bits++;
345 } else {
346 if (prev_bit)
347 point = high;
Hans de Goede036e6c72014-07-25 22:38:51 -0700348 }
349 prev_bit = bit;
350 }
351}
352
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800353/*
Seth Forshee01ce6612011-11-07 19:54:13 -0800354 * Process bitmap data from v3 and v4 protocols. Returns the number of
355 * fingers detected. A return value of 0 means at least one of the
356 * bitmaps was empty.
357 *
358 * The bitmaps don't have enough data to track fingers, so this function
359 * only generates points representing a bounding box of all contacts.
Hans de Goede02d04252014-07-25 22:43:35 -0700360 * These points are returned in fields->mt when the return value
Seth Forshee01ce6612011-11-07 19:54:13 -0800361 * is greater than 0.
362 */
Kevin Cernekee7a9f73e2013-02-13 22:24:55 -0800363static int alps_process_bitmap(struct alps_data *priv,
Hans de Goede02d04252014-07-25 22:43:35 -0700364 struct alps_fields *fields)
Seth Forshee01ce6612011-11-07 19:54:13 -0800365{
Hans de Goede036e6c72014-07-25 22:38:51 -0700366 int i, fingers_x = 0, fingers_y = 0, fingers;
Seth Forshee01ce6612011-11-07 19:54:13 -0800367 struct alps_bitmap_point x_low = {0,}, x_high = {0,};
368 struct alps_bitmap_point y_low = {0,}, y_high = {0,};
Seth Forshee01ce6612011-11-07 19:54:13 -0800369
Hans de Goede02d04252014-07-25 22:43:35 -0700370 if (!fields->x_map || !fields->y_map)
Seth Forshee01ce6612011-11-07 19:54:13 -0800371 return 0;
372
Hans de Goede02d04252014-07-25 22:43:35 -0700373 alps_get_bitmap_points(fields->x_map, &x_low, &x_high, &fingers_x);
374 alps_get_bitmap_points(fields->y_map, &y_low, &y_high, &fingers_y);
Seth Forshee01ce6612011-11-07 19:54:13 -0800375
376 /*
377 * Fingers can overlap, so we use the maximum count of fingers
378 * on either axis as the finger count.
379 */
380 fingers = max(fingers_x, fingers_y);
381
382 /*
Hans de Goede20bea682014-07-25 22:33:33 -0700383 * If an axis reports only a single contact, we have overlapping or
384 * adjacent fingers. Divide the single contact between the two points.
Seth Forshee01ce6612011-11-07 19:54:13 -0800385 */
Hans de Goede20bea682014-07-25 22:33:33 -0700386 if (fingers_x == 1) {
Hans de Goede28835f42014-07-25 22:42:53 -0700387 i = (x_low.num_bits - 1) / 2;
Hans de Goede20bea682014-07-25 22:33:33 -0700388 x_low.num_bits = x_low.num_bits - i;
389 x_high.start_bit = x_low.start_bit + i;
390 x_high.num_bits = max(i, 1);
391 }
392 if (fingers_y == 1) {
Hans de Goede28835f42014-07-25 22:42:53 -0700393 i = (y_low.num_bits - 1) / 2;
Hans de Goede20bea682014-07-25 22:33:33 -0700394 y_low.num_bits = y_low.num_bits - i;
395 y_high.start_bit = y_low.start_bit + i;
396 y_high.num_bits = max(i, 1);
Seth Forshee01ce6612011-11-07 19:54:13 -0800397 }
398
Hans de Goede02d04252014-07-25 22:43:35 -0700399 fields->mt[0].x =
400 (priv->x_max * (2 * x_low.start_bit + x_low.num_bits - 1)) /
401 (2 * (priv->x_bits - 1));
402 fields->mt[0].y =
403 (priv->y_max * (2 * y_low.start_bit + y_low.num_bits - 1)) /
404 (2 * (priv->y_bits - 1));
Seth Forshee01ce6612011-11-07 19:54:13 -0800405
Hans de Goede02d04252014-07-25 22:43:35 -0700406 fields->mt[1].x =
407 (priv->x_max * (2 * x_high.start_bit + x_high.num_bits - 1)) /
408 (2 * (priv->x_bits - 1));
409 fields->mt[1].y =
410 (priv->y_max * (2 * y_high.start_bit + y_high.num_bits - 1)) /
411 (2 * (priv->y_bits - 1));
Seth Forshee01ce6612011-11-07 19:54:13 -0800412
Hans de Goede40e8f532014-07-25 22:37:15 -0700413 /* y-bitmap order is reversed, except on rushmore */
414 if (!(priv->flags & ALPS_IS_RUSHMORE)) {
Hans de Goede02d04252014-07-25 22:43:35 -0700415 fields->mt[0].y = priv->y_max - fields->mt[0].y;
416 fields->mt[1].y = priv->y_max - fields->mt[1].y;
Hans de Goede40e8f532014-07-25 22:37:15 -0700417 }
418
Seth Forshee01ce6612011-11-07 19:54:13 -0800419 return fingers;
420}
421
Hans de Goedecdf333e2014-07-25 22:44:42 -0700422static void alps_set_slot(struct input_dev *dev, int slot, int x, int y)
Seth Forshee01ce6612011-11-07 19:54:13 -0800423{
424 input_mt_slot(dev, slot);
Hans de Goedecdf333e2014-07-25 22:44:42 -0700425 input_mt_report_slot_state(dev, MT_TOOL_FINGER, true);
426 input_report_abs(dev, ABS_MT_POSITION_X, x);
427 input_report_abs(dev, ABS_MT_POSITION_Y, y);
Seth Forshee01ce6612011-11-07 19:54:13 -0800428}
429
Hans de Goedecdf333e2014-07-25 22:44:42 -0700430static void alps_report_mt_data(struct psmouse *psmouse, int n)
Seth Forshee01ce6612011-11-07 19:54:13 -0800431{
Hans de Goede02d04252014-07-25 22:43:35 -0700432 struct alps_data *priv = psmouse->private;
433 struct input_dev *dev = psmouse->dev;
434 struct alps_fields *f = &priv->f;
Hans de Goedecdf333e2014-07-25 22:44:42 -0700435 int i, slot[MAX_TOUCHES];
Hans de Goede02d04252014-07-25 22:43:35 -0700436
Hans de Goedecdf333e2014-07-25 22:44:42 -0700437 input_mt_assign_slots(dev, slot, f->mt, n);
438 for (i = 0; i < n; i++)
439 alps_set_slot(dev, slot[i], f->mt[i].x, f->mt[i].y);
440
441 input_mt_sync_frame(dev);
Seth Forshee01ce6612011-11-07 19:54:13 -0800442}
443
Hans de Goede68c21872014-07-25 22:47:25 -0700444static void alps_report_semi_mt_data(struct psmouse *psmouse, int fingers)
445{
446 struct alps_data *priv = psmouse->private;
447 struct input_dev *dev = psmouse->dev;
448 struct alps_fields *f = &priv->f;
449
450 /* Use st data when we don't have mt data */
451 if (fingers < 2) {
452 f->mt[0].x = f->st.x;
453 f->mt[0].y = f->st.y;
454 fingers = f->pressure > 0 ? 1 : 0;
455 }
456
457 alps_report_mt_data(psmouse, (fingers <= 2) ? fingers : 1);
458
459 input_mt_report_finger_count(dev, fingers);
460
461 input_report_key(dev, BTN_LEFT, f->left);
462 input_report_key(dev, BTN_RIGHT, f->right);
463 input_report_key(dev, BTN_MIDDLE, f->middle);
464
465 input_report_abs(dev, ABS_PRESSURE, f->pressure);
466
467 input_sync(dev);
468}
469
Seth Forshee25bded72011-11-07 19:53:36 -0800470static void alps_process_trackstick_packet_v3(struct psmouse *psmouse)
471{
472 struct alps_data *priv = psmouse->private;
473 unsigned char *packet = psmouse->packet;
474 struct input_dev *dev = priv->dev2;
475 int x, y, z, left, right, middle;
476
477 /* Sanity check packet */
478 if (!(packet[0] & 0x40)) {
479 psmouse_dbg(psmouse, "Bad trackstick packet, discarding\n");
480 return;
481 }
482
483 /*
484 * There's a special packet that seems to indicate the end
485 * of a stream of trackstick data. Filter these out.
486 */
487 if (packet[1] == 0x7f && packet[2] == 0x7f && packet[4] == 0x7f)
488 return;
489
490 x = (s8)(((packet[0] & 0x20) << 2) | (packet[1] & 0x7f));
491 y = (s8)(((packet[0] & 0x10) << 3) | (packet[2] & 0x7f));
492 z = (packet[4] & 0x7c) >> 2;
493
494 /*
495 * The x and y values tend to be quite large, and when used
496 * alone the trackstick is difficult to use. Scale them down
497 * to compensate.
498 */
499 x /= 8;
500 y /= 8;
501
502 input_report_rel(dev, REL_X, x);
503 input_report_rel(dev, REL_Y, -y);
504
505 /*
506 * Most ALPS models report the trackstick buttons in the touchpad
507 * packets, but a few report them here. No reliable way has been
508 * found to differentiate between the models upfront, so we enable
509 * the quirk in response to seeing a button press in the trackstick
510 * packet.
511 */
512 left = packet[3] & 0x01;
513 right = packet[3] & 0x02;
514 middle = packet[3] & 0x04;
515
516 if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) &&
517 (left || right || middle))
518 priv->quirks |= ALPS_QUIRK_TRACKSTICK_BUTTONS;
519
520 if (priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) {
521 input_report_key(dev, BTN_LEFT, left);
522 input_report_key(dev, BTN_RIGHT, right);
523 input_report_key(dev, BTN_MIDDLE, middle);
524 }
525
526 input_sync(dev);
527 return;
528}
529
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800530static void alps_decode_buttons_v3(struct alps_fields *f, unsigned char *p)
531{
532 f->left = !!(p[3] & 0x01);
533 f->right = !!(p[3] & 0x02);
534 f->middle = !!(p[3] & 0x04);
535
536 f->ts_left = !!(p[3] & 0x10);
537 f->ts_right = !!(p[3] & 0x20);
538 f->ts_middle = !!(p[3] & 0x40);
539}
540
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800541static void alps_decode_pinnacle(struct alps_fields *f, unsigned char *p,
542 struct psmouse *psmouse)
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800543{
544 f->first_mp = !!(p[4] & 0x40);
545 f->is_mp = !!(p[0] & 0x40);
546
547 f->fingers = (p[5] & 0x3) + 1;
548 f->x_map = ((p[4] & 0x7e) << 8) |
549 ((p[1] & 0x7f) << 2) |
550 ((p[0] & 0x30) >> 4);
551 f->y_map = ((p[3] & 0x70) << 4) |
552 ((p[2] & 0x7f) << 1) |
553 (p[4] & 0x01);
554
Hans de Goede02d04252014-07-25 22:43:35 -0700555 f->st.x = ((p[1] & 0x7f) << 4) | ((p[4] & 0x30) >> 2) |
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800556 ((p[0] & 0x30) >> 4);
Hans de Goede02d04252014-07-25 22:43:35 -0700557 f->st.y = ((p[2] & 0x7f) << 4) | (p[4] & 0x0f);
558 f->pressure = p[5] & 0x7f;
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800559
560 alps_decode_buttons_v3(f, p);
561}
562
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800563static void alps_decode_rushmore(struct alps_fields *f, unsigned char *p,
564 struct psmouse *psmouse)
Kevin Cernekee1302bac2013-02-13 22:27:08 -0800565{
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800566 alps_decode_pinnacle(f, p, psmouse);
Kevin Cernekee1302bac2013-02-13 22:27:08 -0800567
Yunkang Tangf105e342014-07-25 22:29:24 -0700568 /* Rushmore's packet decode has a bit difference with Pinnacle's */
569 f->is_mp = !!(p[5] & 0x40);
570 f->fingers = max((p[5] & 0x3), ((p[5] >> 2) & 0x3)) + 1;
Kevin Cernekee1302bac2013-02-13 22:27:08 -0800571 f->x_map |= (p[5] & 0x10) << 11;
572 f->y_map |= (p[5] & 0x20) << 6;
573}
574
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800575static void alps_decode_dolphin(struct alps_fields *f, unsigned char *p,
576 struct psmouse *psmouse)
Dave Turvene75af9e52013-02-21 22:58:28 -0800577{
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800578 u64 palm_data = 0;
579 struct alps_data *priv = psmouse->private;
580
Dave Turvene75af9e52013-02-21 22:58:28 -0800581 f->first_mp = !!(p[0] & 0x02);
582 f->is_mp = !!(p[0] & 0x20);
583
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800584 if (!f->is_mp) {
Hans de Goede02d04252014-07-25 22:43:35 -0700585 f->st.x = ((p[1] & 0x7f) | ((p[4] & 0x0f) << 7));
586 f->st.y = ((p[2] & 0x7f) | ((p[4] & 0xf0) << 3));
587 f->pressure = (p[0] & 4) ? 0 : p[5] & 0x7f;
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800588 alps_decode_buttons_v3(f, p);
589 } else {
590 f->fingers = ((p[0] & 0x6) >> 1 |
Dave Turvene75af9e52013-02-21 22:58:28 -0800591 (p[0] & 0x10) >> 2);
Dave Turvene75af9e52013-02-21 22:58:28 -0800592
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800593 palm_data = (p[1] & 0x7f) |
594 ((p[2] & 0x7f) << 7) |
595 ((p[4] & 0x7f) << 14) |
596 ((p[5] & 0x7f) << 21) |
597 ((p[3] & 0x07) << 28) |
598 (((u64)p[3] & 0x70) << 27) |
599 (((u64)p[0] & 0x01) << 34);
Dave Turvene75af9e52013-02-21 22:58:28 -0800600
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800601 /* Y-profile is stored in P(0) to p(n-1), n = y_bits; */
602 f->y_map = palm_data & (BIT(priv->y_bits) - 1);
603
604 /* X-profile is stored in p(n) to p(n+m-1), m = x_bits; */
605 f->x_map = (palm_data >> priv->y_bits) &
606 (BIT(priv->x_bits) - 1);
607 }
Dave Turvene75af9e52013-02-21 22:58:28 -0800608}
609
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800610static void alps_process_touchpad_packet_v3_v5(struct psmouse *psmouse)
Seth Forshee25bded72011-11-07 19:53:36 -0800611{
612 struct alps_data *priv = psmouse->private;
613 unsigned char *packet = psmouse->packet;
Seth Forshee25bded72011-11-07 19:53:36 -0800614 struct input_dev *dev2 = priv->dev2;
Hans de Goede02d04252014-07-25 22:43:35 -0700615 struct alps_fields *f = &priv->f;
616 int fingers = 0;
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800617
Hans de Goede02d04252014-07-25 22:43:35 -0700618 memset(f, 0, sizeof(*f));
619
620 priv->decode_fields(f, packet, psmouse);
Seth Forshee25bded72011-11-07 19:53:36 -0800621
622 /*
Seth Forshee01ce6612011-11-07 19:54:13 -0800623 * There's no single feature of touchpad position and bitmap packets
624 * that can be used to distinguish between them. We rely on the fact
625 * that a bitmap packet should always follow a position packet with
626 * bit 6 of packet[4] set.
Seth Forshee25bded72011-11-07 19:53:36 -0800627 */
628 if (priv->multi_packet) {
Seth Forshee25bded72011-11-07 19:53:36 -0800629 /*
630 * Sometimes a position packet will indicate a multi-packet
631 * sequence, but then what follows is another position
632 * packet. Check for this, and when it happens process the
633 * position packet as usual.
634 */
Hans de Goede02d04252014-07-25 22:43:35 -0700635 if (f->is_mp) {
636 fingers = f->fingers;
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800637 if (priv->proto_version == ALPS_PROTO_V3) {
Hans de Goede02d04252014-07-25 22:43:35 -0700638 if (alps_process_bitmap(priv, f) == 0)
Hans de Goede20bea682014-07-25 22:33:33 -0700639 fingers = 0; /* Use st data */
Seth Forshee01ce6612011-11-07 19:54:13 -0800640
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800641 /* Now process position packet */
Hans de Goede02d04252014-07-25 22:43:35 -0700642 priv->decode_fields(f, priv->multi_data,
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800643 psmouse);
644 } else {
645 /*
646 * Because Dolphin uses position packet's
647 * coordinate data as Pt1 and uses it to
648 * calculate Pt2, so we need to do position
649 * packet decode first.
650 */
Hans de Goede02d04252014-07-25 22:43:35 -0700651 priv->decode_fields(f, priv->multi_data,
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800652 psmouse);
653
654 /*
655 * Since Dolphin's finger number is reliable,
656 * there is no need to compare with bmap_fn.
657 */
Hans de Goede02d04252014-07-25 22:43:35 -0700658 alps_process_bitmap_dolphin(priv, f);
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800659 }
Seth Forshee01ce6612011-11-07 19:54:13 -0800660 } else {
661 priv->multi_packet = 0;
Seth Forshee25bded72011-11-07 19:53:36 -0800662 }
663 }
664
Seth Forshee01ce6612011-11-07 19:54:13 -0800665 /*
666 * Bit 6 of byte 0 is not usually set in position packets. The only
667 * times it seems to be set is in situations where the data is
668 * suspect anyway, e.g. a palm resting flat on the touchpad. Given
669 * this combined with the fact that this bit is useful for filtering
670 * out misidentified bitmap packets, we reject anything with this
671 * bit set.
672 */
Hans de Goede02d04252014-07-25 22:43:35 -0700673 if (f->is_mp)
Seth Forshee01ce6612011-11-07 19:54:13 -0800674 return;
675
Hans de Goede02d04252014-07-25 22:43:35 -0700676 if (!priv->multi_packet && f->first_mp) {
Seth Forshee25bded72011-11-07 19:53:36 -0800677 priv->multi_packet = 1;
Seth Forshee01ce6612011-11-07 19:54:13 -0800678 memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
679 return;
680 }
681
682 priv->multi_packet = 0;
Seth Forshee25bded72011-11-07 19:53:36 -0800683
Seth Forshee25bded72011-11-07 19:53:36 -0800684 /*
685 * Sometimes the hardware sends a single packet with z = 0
686 * in the middle of a stream. Real releases generate packets
687 * with x, y, and z all zero, so these seem to be flukes.
688 * Ignore them.
689 */
Hans de Goede02d04252014-07-25 22:43:35 -0700690 if (f->st.x && f->st.y && !f->pressure)
Seth Forshee25bded72011-11-07 19:53:36 -0800691 return;
692
Hans de Goede68c21872014-07-25 22:47:25 -0700693 alps_report_semi_mt_data(psmouse, fingers);
Seth Forshee25bded72011-11-07 19:53:36 -0800694
695 if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS)) {
Hans de Goede02d04252014-07-25 22:43:35 -0700696 input_report_key(dev2, BTN_LEFT, f->ts_left);
697 input_report_key(dev2, BTN_RIGHT, f->ts_right);
698 input_report_key(dev2, BTN_MIDDLE, f->ts_middle);
Seth Forshee25bded72011-11-07 19:53:36 -0800699 input_sync(dev2);
700 }
701}
702
703static void alps_process_packet_v3(struct psmouse *psmouse)
704{
705 unsigned char *packet = psmouse->packet;
706
707 /*
708 * v3 protocol packets come in three types, two representing
709 * touchpad data and one representing trackstick data.
710 * Trackstick packets seem to be distinguished by always
711 * having 0x3f in the last byte. This value has never been
712 * observed in the last byte of either of the other types
713 * of packets.
714 */
715 if (packet[5] == 0x3f) {
716 alps_process_trackstick_packet_v3(psmouse);
717 return;
718 }
719
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800720 alps_process_touchpad_packet_v3_v5(psmouse);
Seth Forshee25bded72011-11-07 19:53:36 -0800721}
722
Yunkang Tang95f75e92013-12-01 22:33:52 -0800723static void alps_process_packet_v6(struct psmouse *psmouse)
724{
725 struct alps_data *priv = psmouse->private;
726 unsigned char *packet = psmouse->packet;
727 struct input_dev *dev = psmouse->dev;
728 struct input_dev *dev2 = priv->dev2;
729 int x, y, z, left, right, middle;
730
731 /*
732 * We can use Byte5 to distinguish if the packet is from Touchpad
733 * or Trackpoint.
734 * Touchpad: 0 - 0x7E
735 * Trackpoint: 0x7F
736 */
737 if (packet[5] == 0x7F) {
738 /* It should be a DualPoint when received Trackpoint packet */
739 if (!(priv->flags & ALPS_DUALPOINT))
740 return;
741
742 /* Trackpoint packet */
743 x = packet[1] | ((packet[3] & 0x20) << 2);
744 y = packet[2] | ((packet[3] & 0x40) << 1);
745 z = packet[4];
746 left = packet[3] & 0x01;
747 right = packet[3] & 0x02;
748 middle = packet[3] & 0x04;
749
750 /* To prevent the cursor jump when finger lifted */
751 if (x == 0x7F && y == 0x7F && z == 0x7F)
752 x = y = z = 0;
753
754 /* Divide 4 since trackpoint's speed is too fast */
755 input_report_rel(dev2, REL_X, (char)x / 4);
756 input_report_rel(dev2, REL_Y, -((char)y / 4));
757
758 input_report_key(dev2, BTN_LEFT, left);
759 input_report_key(dev2, BTN_RIGHT, right);
760 input_report_key(dev2, BTN_MIDDLE, middle);
761
762 input_sync(dev2);
763 return;
764 }
765
766 /* Touchpad packet */
767 x = packet[1] | ((packet[3] & 0x78) << 4);
768 y = packet[2] | ((packet[4] & 0x78) << 4);
769 z = packet[5];
770 left = packet[3] & 0x01;
771 right = packet[3] & 0x02;
772
773 if (z > 30)
774 input_report_key(dev, BTN_TOUCH, 1);
775 if (z < 25)
776 input_report_key(dev, BTN_TOUCH, 0);
777
778 if (z > 0) {
779 input_report_abs(dev, ABS_X, x);
780 input_report_abs(dev, ABS_Y, y);
781 }
782
783 input_report_abs(dev, ABS_PRESSURE, z);
784 input_report_key(dev, BTN_TOOL_FINGER, z > 0);
785
786 /* v6 touchpad does not have middle button */
787 input_report_key(dev, BTN_LEFT, left);
788 input_report_key(dev, BTN_RIGHT, right);
789
790 input_sync(dev);
791}
792
Seth Forshee25bded72011-11-07 19:53:36 -0800793static void alps_process_packet_v4(struct psmouse *psmouse)
794{
George Pantalos3b7e09f2012-05-10 22:31:59 -0700795 struct alps_data *priv = psmouse->private;
Seth Forshee25bded72011-11-07 19:53:36 -0800796 unsigned char *packet = psmouse->packet;
Hans de Goede02d04252014-07-25 22:43:35 -0700797 struct alps_fields *f = &priv->f;
Hans de Goede68c21872014-07-25 22:47:25 -0700798 int offset;
George Pantalos3b7e09f2012-05-10 22:31:59 -0700799
800 /*
801 * v4 has a 6-byte encoding for bitmap data, but this data is
802 * broken up between 3 normal packets. Use priv->multi_packet to
803 * track our position in the bitmap packet.
804 */
805 if (packet[6] & 0x40) {
806 /* sync, reset position */
807 priv->multi_packet = 0;
808 }
809
810 if (WARN_ON_ONCE(priv->multi_packet > 2))
811 return;
812
813 offset = 2 * priv->multi_packet;
814 priv->multi_data[offset] = packet[6];
815 priv->multi_data[offset + 1] = packet[7];
816
817 if (++priv->multi_packet > 2) {
818 priv->multi_packet = 0;
819
Hans de Goede02d04252014-07-25 22:43:35 -0700820 f->x_map = ((priv->multi_data[2] & 0x1f) << 10) |
George Pantalos3b7e09f2012-05-10 22:31:59 -0700821 ((priv->multi_data[3] & 0x60) << 3) |
822 ((priv->multi_data[0] & 0x3f) << 2) |
823 ((priv->multi_data[1] & 0x60) >> 5);
Hans de Goede02d04252014-07-25 22:43:35 -0700824 f->y_map = ((priv->multi_data[5] & 0x01) << 10) |
George Pantalos3b7e09f2012-05-10 22:31:59 -0700825 ((priv->multi_data[3] & 0x1f) << 5) |
826 (priv->multi_data[1] & 0x1f);
827
Hans de Goede02d04252014-07-25 22:43:35 -0700828 f->fingers = alps_process_bitmap(priv, f);
George Pantalos3b7e09f2012-05-10 22:31:59 -0700829 }
Seth Forshee25bded72011-11-07 19:53:36 -0800830
Hans de Goede02d04252014-07-25 22:43:35 -0700831 f->left = packet[4] & 0x01;
832 f->right = packet[4] & 0x02;
Seth Forshee25bded72011-11-07 19:53:36 -0800833
Hans de Goede02d04252014-07-25 22:43:35 -0700834 f->st.x = ((packet[1] & 0x7f) << 4) | ((packet[3] & 0x30) >> 2) |
835 ((packet[0] & 0x30) >> 4);
836 f->st.y = ((packet[2] & 0x7f) << 4) | (packet[3] & 0x0f);
837 f->pressure = packet[5] & 0x7f;
Seth Forshee25bded72011-11-07 19:53:36 -0800838
Hans de Goede68c21872014-07-25 22:47:25 -0700839 alps_report_semi_mt_data(psmouse, f->fingers);
Seth Forshee25bded72011-11-07 19:53:36 -0800840}
841
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800842static void alps_report_bare_ps2_packet(struct psmouse *psmouse,
843 unsigned char packet[],
844 bool report_buttons)
845{
846 struct alps_data *priv = psmouse->private;
847 struct input_dev *dev2 = priv->dev2;
848
849 if (report_buttons)
850 alps_report_buttons(psmouse, dev2, psmouse->dev,
851 packet[0] & 1, packet[0] & 2, packet[0] & 4);
852
853 input_report_rel(dev2, REL_X,
854 packet[1] ? packet[1] - ((packet[0] << 4) & 0x100) : 0);
855 input_report_rel(dev2, REL_Y,
856 packet[2] ? ((packet[0] << 3) & 0x100) - packet[2] : 0);
857
858 input_sync(dev2);
859}
860
861static psmouse_ret_t alps_handle_interleaved_ps2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862{
863 struct alps_data *priv = psmouse->private;
864
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800865 if (psmouse->pktcnt < 6)
866 return PSMOUSE_GOOD_DATA;
867
868 if (psmouse->pktcnt == 6) {
869 /*
870 * Start a timer to flush the packet if it ends up last
871 * 6-byte packet in the stream. Timer needs to fire
872 * psmouse core times out itself. 20 ms should be enough
873 * to decide if we are getting more data or not.
874 */
875 mod_timer(&priv->timer, jiffies + msecs_to_jiffies(20));
876 return PSMOUSE_GOOD_DATA;
877 }
878
879 del_timer(&priv->timer);
880
881 if (psmouse->packet[6] & 0x80) {
882
883 /*
884 * Highest bit is set - that means we either had
885 * complete ALPS packet and this is start of the
886 * next packet or we got garbage.
887 */
888
889 if (((psmouse->packet[3] |
890 psmouse->packet[4] |
891 psmouse->packet[5]) & 0x80) ||
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800892 (!alps_is_valid_first_byte(priv, psmouse->packet[6]))) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700893 psmouse_dbg(psmouse,
Andy Shevchenko3b112922012-10-30 00:24:41 -0700894 "refusing packet %4ph (suspected interleaved ps/2)\n",
895 psmouse->packet + 3);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800896 return PSMOUSE_BAD_DATA;
897 }
898
Kevin Cernekee24af5cb2013-02-13 22:22:08 -0800899 priv->process_packet(psmouse);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800900
901 /* Continue with the next packet */
902 psmouse->packet[0] = psmouse->packet[6];
903 psmouse->pktcnt = 1;
904
905 } else {
906
907 /*
908 * High bit is 0 - that means that we indeed got a PS/2
909 * packet in the middle of ALPS packet.
910 *
911 * There is also possibility that we got 6-byte ALPS
912 * packet followed by 3-byte packet from trackpoint. We
913 * can not distinguish between these 2 scenarios but
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700914 * because the latter is unlikely to happen in course of
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800915 * normal operation (user would need to press all
916 * buttons on the pad and start moving trackpoint
917 * without touching the pad surface) we assume former.
918 * Even if we are wrong the wost thing that would happen
919 * the cursor would jump but we should not get protocol
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700920 * de-synchronization.
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800921 */
922
923 alps_report_bare_ps2_packet(psmouse, &psmouse->packet[3],
924 false);
925
926 /*
927 * Continue with the standard ALPS protocol handling,
928 * but make sure we won't process it as an interleaved
929 * packet again, which may happen if all buttons are
930 * pressed. To avoid this let's reset the 4th bit which
931 * is normally 1.
932 */
933 psmouse->packet[3] = psmouse->packet[6] & 0xf7;
934 psmouse->pktcnt = 4;
935 }
936
937 return PSMOUSE_GOOD_DATA;
938}
939
940static void alps_flush_packet(unsigned long data)
941{
942 struct psmouse *psmouse = (struct psmouse *)data;
Kevin Cernekee24af5cb2013-02-13 22:22:08 -0800943 struct alps_data *priv = psmouse->private;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800944
945 serio_pause_rx(psmouse->ps2dev.serio);
946
Seth Forsheeb46615f2011-11-07 19:53:30 -0800947 if (psmouse->pktcnt == psmouse->pktsize) {
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800948
949 /*
950 * We did not any more data in reasonable amount of time.
951 * Validate the last 3 bytes and process as a standard
952 * ALPS packet.
953 */
954 if ((psmouse->packet[3] |
955 psmouse->packet[4] |
956 psmouse->packet[5]) & 0x80) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700957 psmouse_dbg(psmouse,
Andy Shevchenko3b112922012-10-30 00:24:41 -0700958 "refusing packet %3ph (suspected interleaved ps/2)\n",
959 psmouse->packet + 3);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800960 } else {
Kevin Cernekee24af5cb2013-02-13 22:22:08 -0800961 priv->process_packet(psmouse);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800962 }
963 psmouse->pktcnt = 0;
964 }
965
966 serio_continue_rx(psmouse->ps2dev.serio);
967}
968
969static psmouse_ret_t alps_process_byte(struct psmouse *psmouse)
970{
971 struct alps_data *priv = psmouse->private;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800972
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 if ((psmouse->packet[0] & 0xc8) == 0x08) { /* PS/2 packet */
974 if (psmouse->pktcnt == 3) {
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800975 alps_report_bare_ps2_packet(psmouse, psmouse->packet,
976 true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 return PSMOUSE_FULL_PACKET;
978 }
979 return PSMOUSE_GOOD_DATA;
980 }
981
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800982 /* Check for PS/2 packet stuffed in the middle of ALPS packet. */
983
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800984 if ((priv->flags & ALPS_PS2_INTERLEAVED) &&
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800985 psmouse->pktcnt >= 4 && (psmouse->packet[3] & 0x0f) == 0x0f) {
986 return alps_handle_interleaved_ps2(psmouse);
987 }
988
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800989 if (!alps_is_valid_first_byte(priv, psmouse->packet[0])) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700990 psmouse_dbg(psmouse,
991 "refusing packet[0] = %x (mask0 = %x, byte0 = %x)\n",
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800992 psmouse->packet[0], priv->mask0, priv->byte0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 return PSMOUSE_BAD_DATA;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800994 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
Seth Forsheeb46615f2011-11-07 19:53:30 -0800996 /* Bytes 2 - pktsize should have 0 in the highest bit */
Yunkang Tang95f75e92013-12-01 22:33:52 -0800997 if ((priv->proto_version < ALPS_PROTO_V5) &&
Dave Turvene75af9e52013-02-21 22:58:28 -0800998 psmouse->pktcnt >= 2 && psmouse->pktcnt <= psmouse->pktsize &&
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800999 (psmouse->packet[psmouse->pktcnt - 1] & 0x80)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001000 psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
1001 psmouse->pktcnt - 1,
1002 psmouse->packet[psmouse->pktcnt - 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 return PSMOUSE_BAD_DATA;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001004 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
Seth Forsheeb46615f2011-11-07 19:53:30 -08001006 if (psmouse->pktcnt == psmouse->pktsize) {
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001007 priv->process_packet(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 return PSMOUSE_FULL_PACKET;
1009 }
1010
1011 return PSMOUSE_GOOD_DATA;
1012}
1013
Seth Forshee25bded72011-11-07 19:53:36 -08001014static int alps_command_mode_send_nibble(struct psmouse *psmouse, int nibble)
1015{
1016 struct ps2dev *ps2dev = &psmouse->ps2dev;
1017 struct alps_data *priv = psmouse->private;
1018 int command;
1019 unsigned char *param;
1020 unsigned char dummy[4];
1021
1022 BUG_ON(nibble > 0xf);
1023
1024 command = priv->nibble_commands[nibble].command;
1025 param = (command & 0x0f00) ?
1026 dummy : (unsigned char *)&priv->nibble_commands[nibble].data;
1027
1028 if (ps2_command(ps2dev, param, command))
1029 return -1;
1030
1031 return 0;
1032}
1033
1034static int alps_command_mode_set_addr(struct psmouse *psmouse, int addr)
1035{
1036 struct ps2dev *ps2dev = &psmouse->ps2dev;
1037 struct alps_data *priv = psmouse->private;
1038 int i, nibble;
1039
1040 if (ps2_command(ps2dev, NULL, priv->addr_command))
1041 return -1;
1042
1043 for (i = 12; i >= 0; i -= 4) {
1044 nibble = (addr >> i) & 0xf;
1045 if (alps_command_mode_send_nibble(psmouse, nibble))
1046 return -1;
1047 }
1048
1049 return 0;
1050}
1051
1052static int __alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
1053{
1054 struct ps2dev *ps2dev = &psmouse->ps2dev;
1055 unsigned char param[4];
1056
1057 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1058 return -1;
1059
1060 /*
1061 * The address being read is returned in the first two bytes
1062 * of the result. Check that this address matches the expected
1063 * address.
1064 */
1065 if (addr != ((param[0] << 8) | param[1]))
1066 return -1;
1067
1068 return param[2];
1069}
1070
1071static int alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
1072{
1073 if (alps_command_mode_set_addr(psmouse, addr))
1074 return -1;
1075 return __alps_command_mode_read_reg(psmouse, addr);
1076}
1077
1078static int __alps_command_mode_write_reg(struct psmouse *psmouse, u8 value)
1079{
1080 if (alps_command_mode_send_nibble(psmouse, (value >> 4) & 0xf))
1081 return -1;
1082 if (alps_command_mode_send_nibble(psmouse, value & 0xf))
1083 return -1;
1084 return 0;
1085}
1086
1087static int alps_command_mode_write_reg(struct psmouse *psmouse, int addr,
1088 u8 value)
1089{
1090 if (alps_command_mode_set_addr(psmouse, addr))
1091 return -1;
1092 return __alps_command_mode_write_reg(psmouse, value);
1093}
1094
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001095static int alps_rpt_cmd(struct psmouse *psmouse, int init_command,
1096 int repeated_command, unsigned char *param)
1097{
1098 struct ps2dev *ps2dev = &psmouse->ps2dev;
1099
1100 param[0] = 0;
1101 if (init_command && ps2_command(ps2dev, param, init_command))
1102 return -EIO;
1103
1104 if (ps2_command(ps2dev, NULL, repeated_command) ||
1105 ps2_command(ps2dev, NULL, repeated_command) ||
1106 ps2_command(ps2dev, NULL, repeated_command))
1107 return -EIO;
1108
1109 param[0] = param[1] = param[2] = 0xff;
1110 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1111 return -EIO;
1112
Dmitry Torokhov39fbe582013-02-14 09:04:24 -08001113 psmouse_dbg(psmouse, "%2.2X report: %3ph\n",
1114 repeated_command, param);
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001115 return 0;
1116}
1117
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08001118static int alps_enter_command_mode(struct psmouse *psmouse)
Seth Forshee25bded72011-11-07 19:53:36 -08001119{
1120 unsigned char param[4];
Seth Forshee25bded72011-11-07 19:53:36 -08001121
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001122 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_RESET_WRAP, param)) {
Seth Forshee25bded72011-11-07 19:53:36 -08001123 psmouse_err(psmouse, "failed to enter command mode\n");
1124 return -1;
1125 }
1126
Dave Turvene75af9e52013-02-21 22:58:28 -08001127 if ((param[0] != 0x88 || (param[1] != 0x07 && param[1] != 0x08)) &&
1128 param[0] != 0x73) {
Seth Forshee25bded72011-11-07 19:53:36 -08001129 psmouse_dbg(psmouse,
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001130 "unknown response while entering command mode\n");
Seth Forshee25bded72011-11-07 19:53:36 -08001131 return -1;
1132 }
Seth Forshee25bded72011-11-07 19:53:36 -08001133 return 0;
1134}
1135
1136static inline int alps_exit_command_mode(struct psmouse *psmouse)
1137{
1138 struct ps2dev *ps2dev = &psmouse->ps2dev;
1139 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM))
1140 return -1;
1141 return 0;
1142}
1143
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144/*
1145 * For DualPoint devices select the device that should respond to
1146 * subsequent commands. It looks like glidepad is behind stickpointer,
1147 * I'd thought it would be other way around...
1148 */
Seth Forshee25bded72011-11-07 19:53:36 -08001149static int alps_passthrough_mode_v2(struct psmouse *psmouse, bool enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150{
1151 struct ps2dev *ps2dev = &psmouse->ps2dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 int cmd = enable ? PSMOUSE_CMD_SETSCALE21 : PSMOUSE_CMD_SETSCALE11;
1153
1154 if (ps2_command(ps2dev, NULL, cmd) ||
1155 ps2_command(ps2dev, NULL, cmd) ||
1156 ps2_command(ps2dev, NULL, cmd) ||
1157 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1158 return -1;
1159
1160 /* we may get 3 more bytes, just ignore them */
Dmitry Torokhovc6117632005-06-01 02:39:51 -05001161 ps2_drain(ps2dev, 3, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
1163 return 0;
1164}
1165
Seth Forshee25bded72011-11-07 19:53:36 -08001166static int alps_absolute_mode_v1_v2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167{
1168 struct ps2dev *ps2dev = &psmouse->ps2dev;
1169
1170 /* Try ALPS magic knock - 4 disable before enable */
1171 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1172 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1173 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1174 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1175 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
1176 return -1;
1177
1178 /*
1179 * Switch mouse to poll (remote) mode so motion data will not
1180 * get in our way
1181 */
1182 return ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETPOLL);
1183}
1184
Yunkang Tang95f75e92013-12-01 22:33:52 -08001185static int alps_monitor_mode_send_word(struct psmouse *psmouse, u16 word)
1186{
1187 int i, nibble;
1188
1189 /*
1190 * b0-b11 are valid bits, send sequence is inverse.
1191 * e.g. when word = 0x0123, nibble send sequence is 3, 2, 1
1192 */
1193 for (i = 0; i <= 8; i += 4) {
1194 nibble = (word >> i) & 0xf;
1195 if (alps_command_mode_send_nibble(psmouse, nibble))
1196 return -1;
1197 }
1198
1199 return 0;
1200}
1201
1202static int alps_monitor_mode_write_reg(struct psmouse *psmouse,
1203 u16 addr, u16 value)
1204{
1205 struct ps2dev *ps2dev = &psmouse->ps2dev;
1206
1207 /* 0x0A0 is the command to write the word */
1208 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE) ||
1209 alps_monitor_mode_send_word(psmouse, 0x0A0) ||
1210 alps_monitor_mode_send_word(psmouse, addr) ||
1211 alps_monitor_mode_send_word(psmouse, value) ||
1212 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1213 return -1;
1214
1215 return 0;
1216}
1217
1218static int alps_monitor_mode(struct psmouse *psmouse, bool enable)
1219{
1220 struct ps2dev *ps2dev = &psmouse->ps2dev;
1221
1222 if (enable) {
1223 /* EC E9 F5 F5 E7 E6 E7 E9 to enter monitor mode */
1224 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
1225 ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO) ||
1226 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1227 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1228 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1229 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1230 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1231 ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO))
1232 return -1;
1233 } else {
1234 /* EC to exit monitor mode */
1235 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP))
1236 return -1;
1237 }
1238
1239 return 0;
1240}
1241
1242static int alps_absolute_mode_v6(struct psmouse *psmouse)
1243{
1244 u16 reg_val = 0x181;
1245 int ret = -1;
1246
1247 /* enter monitor mode, to write the register */
1248 if (alps_monitor_mode(psmouse, true))
1249 return -1;
1250
1251 ret = alps_monitor_mode_write_reg(psmouse, 0x000, reg_val);
1252
1253 if (alps_monitor_mode(psmouse, false))
1254 ret = -1;
1255
1256 return ret;
1257}
1258
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259static int alps_get_status(struct psmouse *psmouse, char *param)
1260{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 /* Get status: 0xF5 0xF5 0xF5 0xE9 */
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001262 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_DISABLE, param))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 return -1;
1264
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 return 0;
1266}
1267
1268/*
1269 * Turn touchpad tapping on or off. The sequences are:
1270 * 0xE9 0xF5 0xF5 0xF3 0x0A to enable,
1271 * 0xE9 0xF5 0xF5 0xE8 0x00 to disable.
1272 * My guess that 0xE9 (GetInfo) is here as a sync point.
1273 * For models that also have stickpointer (DualPoints) its tapping
1274 * is controlled separately (0xE6 0xE6 0xE6 0xF3 0x14|0x0A) but
1275 * we don't fiddle with it.
1276 */
1277static int alps_tap_mode(struct psmouse *psmouse, int enable)
1278{
1279 struct ps2dev *ps2dev = &psmouse->ps2dev;
1280 int cmd = enable ? PSMOUSE_CMD_SETRATE : PSMOUSE_CMD_SETRES;
1281 unsigned char tap_arg = enable ? 0x0A : 0x00;
1282 unsigned char param[4];
1283
1284 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO) ||
1285 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1286 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1287 ps2_command(ps2dev, &tap_arg, cmd))
1288 return -1;
1289
1290 if (alps_get_status(psmouse, param))
1291 return -1;
1292
1293 return 0;
1294}
1295
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001296/*
1297 * alps_poll() - poll the touchpad for current motion packet.
1298 * Used in resync.
1299 */
1300static int alps_poll(struct psmouse *psmouse)
1301{
1302 struct alps_data *priv = psmouse->private;
Seth Forsheeb46615f2011-11-07 19:53:30 -08001303 unsigned char buf[sizeof(psmouse->packet)];
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001304 bool poll_failed;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001305
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001306 if (priv->flags & ALPS_PASS)
Seth Forshee25bded72011-11-07 19:53:36 -08001307 alps_passthrough_mode_v2(psmouse, true);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001308
1309 poll_failed = ps2_command(&psmouse->ps2dev, buf,
1310 PSMOUSE_CMD_POLL | (psmouse->pktsize << 8)) < 0;
1311
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001312 if (priv->flags & ALPS_PASS)
Seth Forshee25bded72011-11-07 19:53:36 -08001313 alps_passthrough_mode_v2(psmouse, false);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001314
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001315 if (poll_failed || (buf[0] & priv->mask0) != priv->byte0)
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001316 return -1;
1317
1318 if ((psmouse->badbyte & 0xc8) == 0x08) {
1319/*
1320 * Poll the track stick ...
1321 */
1322 if (ps2_command(&psmouse->ps2dev, buf, PSMOUSE_CMD_POLL | (3 << 8)))
1323 return -1;
1324 }
1325
1326 memcpy(psmouse->packet, buf, sizeof(buf));
1327 return 0;
1328}
1329
Seth Forshee25bded72011-11-07 19:53:36 -08001330static int alps_hw_init_v1_v2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331{
1332 struct alps_data *priv = psmouse->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001334 if ((priv->flags & ALPS_PASS) &&
Seth Forshee25bded72011-11-07 19:53:36 -08001335 alps_passthrough_mode_v2(psmouse, true)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 return -1;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001337 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001339 if (alps_tap_mode(psmouse, true)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001340 psmouse_warn(psmouse, "Failed to enable hardware tapping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 return -1;
Peter Osterlund963f6262005-07-11 01:08:04 -05001342 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
Seth Forshee25bded72011-11-07 19:53:36 -08001344 if (alps_absolute_mode_v1_v2(psmouse)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001345 psmouse_err(psmouse, "Failed to enable absolute mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 return -1;
1347 }
1348
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001349 if ((priv->flags & ALPS_PASS) &&
Seth Forshee25bded72011-11-07 19:53:36 -08001350 alps_passthrough_mode_v2(psmouse, false)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 return -1;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04001354 /* ALPS needs stream mode, otherwise it won't report any data */
1355 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001356 psmouse_err(psmouse, "Failed to enable stream mode\n");
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04001357 return -1;
1358 }
1359
1360 return 0;
1361}
1362
Yunkang Tang95f75e92013-12-01 22:33:52 -08001363static int alps_hw_init_v6(struct psmouse *psmouse)
1364{
1365 unsigned char param[2] = {0xC8, 0x14};
1366
1367 /* Enter passthrough mode to let trackpoint enter 6byte raw mode */
1368 if (alps_passthrough_mode_v2(psmouse, true))
1369 return -1;
1370
1371 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1372 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1373 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1374 ps2_command(&psmouse->ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
1375 ps2_command(&psmouse->ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
1376 return -1;
1377
1378 if (alps_passthrough_mode_v2(psmouse, false))
1379 return -1;
1380
1381 if (alps_absolute_mode_v6(psmouse)) {
1382 psmouse_err(psmouse, "Failed to enable absolute mode\n");
1383 return -1;
1384 }
1385
1386 return 0;
1387}
1388
Seth Forshee25bded72011-11-07 19:53:36 -08001389/*
Kevin Cernekeecd401202013-02-13 22:28:07 -08001390 * Enable or disable passthrough mode to the trackstick.
Seth Forshee25bded72011-11-07 19:53:36 -08001391 */
Kevin Cernekeecd401202013-02-13 22:28:07 -08001392static int alps_passthrough_mode_v3(struct psmouse *psmouse,
1393 int reg_base, bool enable)
Seth Forshee25bded72011-11-07 19:53:36 -08001394{
Kevin Cernekeecd401202013-02-13 22:28:07 -08001395 int reg_val, ret = -1;
Seth Forshee25bded72011-11-07 19:53:36 -08001396
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08001397 if (alps_enter_command_mode(psmouse))
Seth Forshee25bded72011-11-07 19:53:36 -08001398 return -1;
1399
Kevin Cernekeecd401202013-02-13 22:28:07 -08001400 reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x0008);
1401 if (reg_val == -1)
1402 goto error;
1403
Seth Forshee25bded72011-11-07 19:53:36 -08001404 if (enable)
1405 reg_val |= 0x01;
1406 else
1407 reg_val &= ~0x01;
1408
Kevin Cernekeecd401202013-02-13 22:28:07 -08001409 ret = __alps_command_mode_write_reg(psmouse, reg_val);
Seth Forshee25bded72011-11-07 19:53:36 -08001410
Kevin Cernekeecd401202013-02-13 22:28:07 -08001411error:
1412 if (alps_exit_command_mode(psmouse))
1413 ret = -1;
1414 return ret;
Seth Forshee25bded72011-11-07 19:53:36 -08001415}
1416
1417/* Must be in command mode when calling this function */
1418static int alps_absolute_mode_v3(struct psmouse *psmouse)
1419{
1420 int reg_val;
1421
1422 reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
1423 if (reg_val == -1)
1424 return -1;
1425
1426 reg_val |= 0x06;
1427 if (__alps_command_mode_write_reg(psmouse, reg_val))
1428 return -1;
1429
1430 return 0;
1431}
1432
Kevin Cernekeecd401202013-02-13 22:28:07 -08001433static int alps_probe_trackstick_v3(struct psmouse *psmouse, int reg_base)
1434{
1435 int ret = -EIO, reg_val;
1436
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08001437 if (alps_enter_command_mode(psmouse))
Kevin Cernekeecd401202013-02-13 22:28:07 -08001438 goto error;
1439
1440 reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x08);
1441 if (reg_val == -1)
1442 goto error;
1443
1444 /* bit 7: trackstick is present */
1445 ret = reg_val & 0x80 ? 0 : -ENODEV;
1446
1447error:
1448 alps_exit_command_mode(psmouse);
1449 return ret;
1450}
1451
1452static int alps_setup_trackstick_v3(struct psmouse *psmouse, int reg_base)
1453{
1454 struct ps2dev *ps2dev = &psmouse->ps2dev;
1455 int ret = 0;
1456 unsigned char param[4];
1457
1458 if (alps_passthrough_mode_v3(psmouse, reg_base, true))
1459 return -EIO;
1460
1461 /*
1462 * E7 report for the trackstick
1463 *
1464 * There have been reports of failures to seem to trace back
1465 * to the above trackstick check failing. When these occur
1466 * this E7 report fails, so when that happens we continue
1467 * with the assumption that there isn't a trackstick after
1468 * all.
1469 */
1470 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_SETSCALE21, param)) {
1471 psmouse_warn(psmouse, "trackstick E7 report failed\n");
1472 ret = -ENODEV;
1473 } else {
Dmitry Torokhov39fbe582013-02-14 09:04:24 -08001474 psmouse_dbg(psmouse, "trackstick E7 report: %3ph\n", param);
Kevin Cernekeecd401202013-02-13 22:28:07 -08001475
1476 /*
1477 * Not sure what this does, but it is absolutely
1478 * essential. Without it, the touchpad does not
1479 * work at all and the trackstick just emits normal
1480 * PS/2 packets.
1481 */
1482 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1483 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1484 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1485 alps_command_mode_send_nibble(psmouse, 0x9) ||
1486 alps_command_mode_send_nibble(psmouse, 0x4)) {
1487 psmouse_err(psmouse,
1488 "Error sending magic E6 sequence\n");
1489 ret = -EIO;
1490 goto error;
1491 }
1492
1493 /*
1494 * This ensures the trackstick packets are in the format
1495 * supported by this driver. If bit 1 isn't set the packet
1496 * format is different.
1497 */
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08001498 if (alps_enter_command_mode(psmouse) ||
Kevin Cernekeecd401202013-02-13 22:28:07 -08001499 alps_command_mode_write_reg(psmouse,
1500 reg_base + 0x08, 0x82) ||
1501 alps_exit_command_mode(psmouse))
1502 ret = -EIO;
1503 }
1504
1505error:
1506 if (alps_passthrough_mode_v3(psmouse, reg_base, false))
1507 ret = -EIO;
1508
1509 return ret;
1510}
1511
Seth Forshee25bded72011-11-07 19:53:36 -08001512static int alps_hw_init_v3(struct psmouse *psmouse)
1513{
Seth Forshee25bded72011-11-07 19:53:36 -08001514 struct ps2dev *ps2dev = &psmouse->ps2dev;
1515 int reg_val;
1516 unsigned char param[4];
1517
Kevin Cernekeecd401202013-02-13 22:28:07 -08001518 reg_val = alps_probe_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE);
1519 if (reg_val == -EIO)
1520 goto error;
Dmitry Torokhov39fbe582013-02-14 09:04:24 -08001521
Kevin Cernekeecd401202013-02-13 22:28:07 -08001522 if (reg_val == 0 &&
1523 alps_setup_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE) == -EIO)
Seth Forshee25bded72011-11-07 19:53:36 -08001524 goto error;
1525
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08001526 if (alps_enter_command_mode(psmouse) ||
Kevin Cernekeecd401202013-02-13 22:28:07 -08001527 alps_absolute_mode_v3(psmouse)) {
Seth Forshee25bded72011-11-07 19:53:36 -08001528 psmouse_err(psmouse, "Failed to enter absolute mode\n");
1529 goto error;
1530 }
1531
1532 reg_val = alps_command_mode_read_reg(psmouse, 0x0006);
1533 if (reg_val == -1)
1534 goto error;
1535 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
1536 goto error;
1537
1538 reg_val = alps_command_mode_read_reg(psmouse, 0x0007);
1539 if (reg_val == -1)
1540 goto error;
1541 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
1542 goto error;
1543
1544 if (alps_command_mode_read_reg(psmouse, 0x0144) == -1)
1545 goto error;
1546 if (__alps_command_mode_write_reg(psmouse, 0x04))
1547 goto error;
1548
1549 if (alps_command_mode_read_reg(psmouse, 0x0159) == -1)
1550 goto error;
1551 if (__alps_command_mode_write_reg(psmouse, 0x03))
1552 goto error;
1553
1554 if (alps_command_mode_read_reg(psmouse, 0x0163) == -1)
1555 goto error;
1556 if (alps_command_mode_write_reg(psmouse, 0x0163, 0x03))
1557 goto error;
1558
1559 if (alps_command_mode_read_reg(psmouse, 0x0162) == -1)
1560 goto error;
1561 if (alps_command_mode_write_reg(psmouse, 0x0162, 0x04))
1562 goto error;
1563
Seth Forshee25bded72011-11-07 19:53:36 -08001564 alps_exit_command_mode(psmouse);
1565
1566 /* Set rate and enable data reporting */
1567 param[0] = 0x64;
1568 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
1569 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
1570 psmouse_err(psmouse, "Failed to enable data reporting\n");
1571 return -1;
1572 }
1573
1574 return 0;
1575
Seth Forshee25bded72011-11-07 19:53:36 -08001576error:
1577 /*
1578 * Leaving the touchpad in command mode will essentially render
1579 * it unusable until the machine reboots, so exit it here just
1580 * to be safe
1581 */
1582 alps_exit_command_mode(psmouse);
1583 return -1;
1584}
1585
Kevin Cernekee1302bac2013-02-13 22:27:08 -08001586static int alps_hw_init_rushmore_v3(struct psmouse *psmouse)
1587{
Kevin Cernekeecd401202013-02-13 22:28:07 -08001588 struct alps_data *priv = psmouse->private;
Kevin Cernekee1302bac2013-02-13 22:27:08 -08001589 struct ps2dev *ps2dev = &psmouse->ps2dev;
1590 int reg_val, ret = -1;
1591
Kevin Cernekeecd401202013-02-13 22:28:07 -08001592 if (priv->flags & ALPS_DUALPOINT) {
1593 reg_val = alps_setup_trackstick_v3(psmouse,
1594 ALPS_REG_BASE_RUSHMORE);
1595 if (reg_val == -EIO)
1596 goto error;
1597 if (reg_val == -ENODEV)
1598 priv->flags &= ~ALPS_DUALPOINT;
1599 }
1600
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08001601 if (alps_enter_command_mode(psmouse) ||
Kevin Cernekee1302bac2013-02-13 22:27:08 -08001602 alps_command_mode_read_reg(psmouse, 0xc2d9) == -1 ||
1603 alps_command_mode_write_reg(psmouse, 0xc2cb, 0x00))
1604 goto error;
1605
1606 reg_val = alps_command_mode_read_reg(psmouse, 0xc2c6);
1607 if (reg_val == -1)
1608 goto error;
1609 if (__alps_command_mode_write_reg(psmouse, reg_val & 0xfd))
1610 goto error;
1611
1612 if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
1613 goto error;
1614
1615 /* enter absolute mode */
1616 reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
1617 if (reg_val == -1)
1618 goto error;
1619 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
1620 goto error;
1621
1622 alps_exit_command_mode(psmouse);
1623 return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
1624
1625error:
1626 alps_exit_command_mode(psmouse);
1627 return ret;
1628}
1629
Seth Forshee25bded72011-11-07 19:53:36 -08001630/* Must be in command mode when calling this function */
1631static int alps_absolute_mode_v4(struct psmouse *psmouse)
1632{
1633 int reg_val;
1634
1635 reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
1636 if (reg_val == -1)
1637 return -1;
1638
1639 reg_val |= 0x02;
1640 if (__alps_command_mode_write_reg(psmouse, reg_val))
1641 return -1;
1642
1643 return 0;
1644}
1645
1646static int alps_hw_init_v4(struct psmouse *psmouse)
1647{
Seth Forshee25bded72011-11-07 19:53:36 -08001648 struct ps2dev *ps2dev = &psmouse->ps2dev;
1649 unsigned char param[4];
1650
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08001651 if (alps_enter_command_mode(psmouse))
Seth Forshee25bded72011-11-07 19:53:36 -08001652 goto error;
1653
1654 if (alps_absolute_mode_v4(psmouse)) {
1655 psmouse_err(psmouse, "Failed to enter absolute mode\n");
1656 goto error;
1657 }
1658
1659 if (alps_command_mode_write_reg(psmouse, 0x0007, 0x8c))
1660 goto error;
1661
1662 if (alps_command_mode_write_reg(psmouse, 0x0149, 0x03))
1663 goto error;
1664
1665 if (alps_command_mode_write_reg(psmouse, 0x0160, 0x03))
1666 goto error;
1667
1668 if (alps_command_mode_write_reg(psmouse, 0x017f, 0x15))
1669 goto error;
1670
1671 if (alps_command_mode_write_reg(psmouse, 0x0151, 0x01))
1672 goto error;
1673
1674 if (alps_command_mode_write_reg(psmouse, 0x0168, 0x03))
1675 goto error;
1676
1677 if (alps_command_mode_write_reg(psmouse, 0x014a, 0x03))
1678 goto error;
1679
1680 if (alps_command_mode_write_reg(psmouse, 0x0161, 0x03))
1681 goto error;
1682
1683 alps_exit_command_mode(psmouse);
1684
1685 /*
1686 * This sequence changes the output from a 9-byte to an
1687 * 8-byte format. All the same data seems to be present,
1688 * just in a more compact format.
1689 */
1690 param[0] = 0xc8;
1691 param[1] = 0x64;
1692 param[2] = 0x50;
1693 if (ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
1694 ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE) ||
1695 ps2_command(ps2dev, &param[2], PSMOUSE_CMD_SETRATE) ||
1696 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
1697 return -1;
1698
1699 /* Set rate and enable data reporting */
1700 param[0] = 0x64;
1701 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
1702 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
1703 psmouse_err(psmouse, "Failed to enable data reporting\n");
1704 return -1;
1705 }
1706
1707 return 0;
1708
1709error:
1710 /*
1711 * Leaving the touchpad in command mode will essentially render
1712 * it unusable until the machine reboots, so exit it here just
1713 * to be safe
1714 */
1715 alps_exit_command_mode(psmouse);
1716 return -1;
1717}
1718
Yunkang Tangee65d4b2013-12-26 14:54:19 -08001719static int alps_dolphin_get_device_area(struct psmouse *psmouse,
1720 struct alps_data *priv)
1721{
1722 struct ps2dev *ps2dev = &psmouse->ps2dev;
1723 unsigned char param[4] = {0};
1724 int num_x_electrode, num_y_electrode;
1725
1726 if (alps_enter_command_mode(psmouse))
1727 return -1;
1728
1729 param[0] = 0x0a;
1730 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
1731 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
1732 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
1733 ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
1734 ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE))
1735 return -1;
1736
1737 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1738 return -1;
1739
1740 /*
1741 * Dolphin's sensor line number is not fixed. It can be calculated
1742 * by adding the device's register value with DOLPHIN_PROFILE_X/YOFFSET.
1743 * Further more, we can get device's x_max and y_max by multiplying
1744 * sensor line number with DOLPHIN_COUNT_PER_ELECTRODE.
1745 *
1746 * e.g. When we get register's sensor_x = 11 & sensor_y = 8,
1747 * real sensor line number X = 11 + 8 = 19, and
1748 * real sensor line number Y = 8 + 1 = 9.
1749 * So, x_max = (19 - 1) * 64 = 1152, and
1750 * y_max = (9 - 1) * 64 = 512.
1751 */
1752 num_x_electrode = DOLPHIN_PROFILE_XOFFSET + (param[2] & 0x0F);
1753 num_y_electrode = DOLPHIN_PROFILE_YOFFSET + ((param[2] >> 4) & 0x0F);
1754 priv->x_bits = num_x_electrode;
1755 priv->y_bits = num_y_electrode;
1756 priv->x_max = (num_x_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
1757 priv->y_max = (num_y_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
1758
1759 if (alps_exit_command_mode(psmouse))
1760 return -1;
1761
1762 return 0;
1763}
1764
Dave Turvene75af9e52013-02-21 22:58:28 -08001765static int alps_hw_init_dolphin_v1(struct psmouse *psmouse)
1766{
1767 struct ps2dev *ps2dev = &psmouse->ps2dev;
1768 unsigned char param[2];
1769
1770 /* This is dolphin "v1" as empirically defined by florin9doi */
1771 param[0] = 0x64;
1772 param[1] = 0x28;
1773
1774 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
1775 ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
1776 ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
1777 return -1;
1778
1779 return 0;
1780}
1781
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001782static void alps_set_defaults(struct alps_data *priv)
Seth Forshee25bded72011-11-07 19:53:36 -08001783{
Kevin Cernekeef673ceb2013-02-13 22:23:34 -08001784 priv->byte0 = 0x8f;
1785 priv->mask0 = 0x8f;
1786 priv->flags = ALPS_DUALPOINT;
1787
Kevin Cernekee7a9f73e2013-02-13 22:24:55 -08001788 priv->x_max = 2000;
1789 priv->y_max = 1400;
1790 priv->x_bits = 15;
1791 priv->y_bits = 11;
1792
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001793 switch (priv->proto_version) {
Seth Forshee25bded72011-11-07 19:53:36 -08001794 case ALPS_PROTO_V1:
1795 case ALPS_PROTO_V2:
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001796 priv->hw_init = alps_hw_init_v1_v2;
1797 priv->process_packet = alps_process_packet_v1_v2;
1798 priv->set_abs_params = alps_set_abs_params_st;
Yunkang Tang95f75e92013-12-01 22:33:52 -08001799 priv->x_max = 1023;
1800 priv->y_max = 767;
Seth Forshee25bded72011-11-07 19:53:36 -08001801 break;
1802 case ALPS_PROTO_V3:
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001803 priv->hw_init = alps_hw_init_v3;
1804 priv->process_packet = alps_process_packet_v3;
1805 priv->set_abs_params = alps_set_abs_params_mt;
Kevin Cernekeef85e5002013-02-13 22:26:11 -08001806 priv->decode_fields = alps_decode_pinnacle;
Kevin Cernekee50e8b212013-02-13 22:23:04 -08001807 priv->nibble_commands = alps_v3_nibble_commands;
1808 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
Seth Forshee25bded72011-11-07 19:53:36 -08001809 break;
1810 case ALPS_PROTO_V4:
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001811 priv->hw_init = alps_hw_init_v4;
1812 priv->process_packet = alps_process_packet_v4;
1813 priv->set_abs_params = alps_set_abs_params_mt;
Kevin Cernekee50e8b212013-02-13 22:23:04 -08001814 priv->nibble_commands = alps_v4_nibble_commands;
1815 priv->addr_command = PSMOUSE_CMD_DISABLE;
Seth Forshee25bded72011-11-07 19:53:36 -08001816 break;
Dave Turvene75af9e52013-02-21 22:58:28 -08001817 case ALPS_PROTO_V5:
1818 priv->hw_init = alps_hw_init_dolphin_v1;
Yunkang Tangee65d4b2013-12-26 14:54:19 -08001819 priv->process_packet = alps_process_touchpad_packet_v3_v5;
Dave Turvene75af9e52013-02-21 22:58:28 -08001820 priv->decode_fields = alps_decode_dolphin;
1821 priv->set_abs_params = alps_set_abs_params_mt;
1822 priv->nibble_commands = alps_v3_nibble_commands;
1823 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
1824 priv->byte0 = 0xc8;
Yunkang Tangee65d4b2013-12-26 14:54:19 -08001825 priv->mask0 = 0xd8;
Dave Turvene75af9e52013-02-21 22:58:28 -08001826 priv->flags = 0;
1827 priv->x_max = 1360;
1828 priv->y_max = 660;
1829 priv->x_bits = 23;
1830 priv->y_bits = 12;
1831 break;
Yunkang Tang95f75e92013-12-01 22:33:52 -08001832 case ALPS_PROTO_V6:
1833 priv->hw_init = alps_hw_init_v6;
1834 priv->process_packet = alps_process_packet_v6;
1835 priv->set_abs_params = alps_set_abs_params_st;
1836 priv->nibble_commands = alps_v6_nibble_commands;
1837 priv->x_max = 2047;
1838 priv->y_max = 1535;
1839 break;
Seth Forshee25bded72011-11-07 19:53:36 -08001840 }
Seth Forshee25bded72011-11-07 19:53:36 -08001841}
1842
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001843static int alps_match_table(struct psmouse *psmouse, struct alps_data *priv,
1844 unsigned char *e7, unsigned char *ec)
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08001845{
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001846 const struct alps_model_info *model;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08001847 int i;
1848
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001849 for (i = 0; i < ARRAY_SIZE(alps_model_data); i++) {
1850 model = &alps_model_data[i];
1851
1852 if (!memcmp(e7, model->signature, sizeof(model->signature)) &&
1853 (!model->command_mode_resp ||
1854 model->command_mode_resp == ec[2])) {
1855
1856 priv->proto_version = model->proto_version;
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001857 alps_set_defaults(priv);
1858
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001859 priv->flags = model->flags;
1860 priv->byte0 = model->byte0;
1861 priv->mask0 = model->mask0;
1862
1863 return 0;
1864 }
1865 }
1866
1867 return -EINVAL;
1868}
1869
1870static int alps_identify(struct psmouse *psmouse, struct alps_data *priv)
1871{
1872 unsigned char e6[4], e7[4], ec[4];
1873
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08001874 /*
1875 * First try "E6 report".
1876 * ALPS should return 0,0,10 or 0,0,100 if no buttons are pressed.
1877 * The bits 0-2 of the first byte will be 1s if some buttons are
1878 * pressed.
1879 */
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001880 if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
1881 PSMOUSE_CMD_SETSCALE11, e6))
1882 return -EIO;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08001883
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001884 if ((e6[0] & 0xf8) != 0 || e6[1] != 0 || (e6[2] != 10 && e6[2] != 100))
1885 return -EINVAL;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08001886
1887 /*
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001888 * Now get the "E7" and "EC" reports. These will uniquely identify
1889 * most ALPS touchpads.
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08001890 */
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001891 if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
1892 PSMOUSE_CMD_SETSCALE21, e7) ||
1893 alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
1894 PSMOUSE_CMD_RESET_WRAP, ec) ||
1895 alps_exit_command_mode(psmouse))
1896 return -EIO;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08001897
Kevin Cernekeef673ceb2013-02-13 22:23:34 -08001898 if (alps_match_table(psmouse, priv, e7, ec) == 0) {
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001899 return 0;
Dave Turvene75af9e52013-02-21 22:58:28 -08001900 } else if (e7[0] == 0x73 && e7[1] == 0x03 && e7[2] == 0x50 &&
Yunkang Tangee65d4b2013-12-26 14:54:19 -08001901 ec[0] == 0x73 && (ec[1] == 0x01 || ec[1] == 0x02)) {
Dave Turvene75af9e52013-02-21 22:58:28 -08001902 priv->proto_version = ALPS_PROTO_V5;
1903 alps_set_defaults(priv);
Yunkang Tangee65d4b2013-12-26 14:54:19 -08001904 if (alps_dolphin_get_device_area(psmouse, priv))
1905 return -EIO;
1906 else
1907 return 0;
Kevin Cernekee1302bac2013-02-13 22:27:08 -08001908 } else if (ec[0] == 0x88 && ec[1] == 0x08) {
1909 priv->proto_version = ALPS_PROTO_V3;
1910 alps_set_defaults(priv);
1911
1912 priv->hw_init = alps_hw_init_rushmore_v3;
1913 priv->decode_fields = alps_decode_rushmore;
1914 priv->x_bits = 16;
1915 priv->y_bits = 12;
Hans de Goede40e8f532014-07-25 22:37:15 -07001916 priv->flags |= ALPS_IS_RUSHMORE;
Kevin Cernekee1302bac2013-02-13 22:27:08 -08001917
Kevin Cernekeecd401202013-02-13 22:28:07 -08001918 /* hack to make addr_command, nibble_command available */
1919 psmouse->private = priv;
1920
1921 if (alps_probe_trackstick_v3(psmouse, ALPS_REG_BASE_RUSHMORE))
1922 priv->flags &= ~ALPS_DUALPOINT;
1923
Kevin Cernekee1302bac2013-02-13 22:27:08 -08001924 return 0;
Kevin Cernekeef673ceb2013-02-13 22:23:34 -08001925 } else if (ec[0] == 0x88 && ec[1] == 0x07 &&
1926 ec[2] >= 0x90 && ec[2] <= 0x9d) {
1927 priv->proto_version = ALPS_PROTO_V3;
1928 alps_set_defaults(priv);
1929
1930 return 0;
1931 }
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08001932
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001933 psmouse_info(psmouse,
Dmitry Torokhov39fbe582013-02-14 09:04:24 -08001934 "Unknown ALPS touchpad: E7=%3ph, EC=%3ph\n", e7, ec);
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08001935
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001936 return -EINVAL;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08001937}
1938
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04001939static int alps_reconnect(struct psmouse *psmouse)
1940{
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001941 struct alps_data *priv = psmouse->private;
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08001942
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04001943 psmouse_reset(psmouse);
1944
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001945 if (alps_identify(psmouse, priv) < 0)
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04001946 return -1;
1947
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001948 return priv->hw_init(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949}
1950
1951static void alps_disconnect(struct psmouse *psmouse)
1952{
1953 struct alps_data *priv = psmouse->private;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001954
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 psmouse_reset(psmouse);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001956 del_timer_sync(&priv->timer);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001957 input_unregister_device(priv->dev2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 kfree(priv);
1959}
1960
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001961static void alps_set_abs_params_st(struct alps_data *priv,
1962 struct input_dev *dev1)
1963{
Yunkang Tang95f75e92013-12-01 22:33:52 -08001964 input_set_abs_params(dev1, ABS_X, 0, priv->x_max, 0, 0);
1965 input_set_abs_params(dev1, ABS_Y, 0, priv->y_max, 0, 0);
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001966}
1967
1968static void alps_set_abs_params_mt(struct alps_data *priv,
1969 struct input_dev *dev1)
1970{
Kevin Cernekee7a9f73e2013-02-13 22:24:55 -08001971 input_set_abs_params(dev1, ABS_MT_POSITION_X, 0, priv->x_max, 0, 0);
1972 input_set_abs_params(dev1, ABS_MT_POSITION_Y, 0, priv->y_max, 0, 0);
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001973
Hans de Goedecdf333e2014-07-25 22:44:42 -07001974 input_mt_init_slots(dev1, MAX_TOUCHES, INPUT_MT_POINTER |
1975 INPUT_MT_DROP_UNUSED | INPUT_MT_TRACK | INPUT_MT_SEMI_MT);
1976
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001977 set_bit(BTN_TOOL_TRIPLETAP, dev1->keybit);
1978 set_bit(BTN_TOOL_QUADTAP, dev1->keybit);
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001979}
1980
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981int alps_init(struct psmouse *psmouse)
1982{
1983 struct alps_data *priv;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001984 struct input_dev *dev1 = psmouse->dev, *dev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
Dmitry Torokhovf42649e2007-04-12 01:31:13 -04001986 priv = kzalloc(sizeof(struct alps_data), GFP_KERNEL);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001987 dev2 = input_allocate_device();
1988 if (!priv || !dev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 goto init_fail;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001990
1991 priv->dev2 = dev2;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001992 setup_timer(&priv->timer, alps_flush_packet, (unsigned long)psmouse);
1993
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04001994 psmouse->private = priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
Seth Forshee25bded72011-11-07 19:53:36 -08001996 psmouse_reset(psmouse);
1997
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001998 if (alps_identify(psmouse, priv) < 0)
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08001999 goto init_fail;
2000
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002001 if (priv->hw_init(psmouse))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 goto init_fail;
2003
Dmitry Torokhov7105d2e2009-12-11 23:54:54 -08002004 /*
2005 * Undo part of setup done for us by psmouse core since touchpad
2006 * is not a relative device.
2007 */
2008 __clear_bit(EV_REL, dev1->evbit);
2009 __clear_bit(REL_X, dev1->relbit);
2010 __clear_bit(REL_Y, dev1->relbit);
2011
2012 /*
2013 * Now set up our capabilities.
2014 */
Jiri Slaby7b19ada2007-10-18 23:40:32 -07002015 dev1->evbit[BIT_WORD(EV_KEY)] |= BIT_MASK(EV_KEY);
2016 dev1->keybit[BIT_WORD(BTN_TOUCH)] |= BIT_MASK(BTN_TOUCH);
2017 dev1->keybit[BIT_WORD(BTN_TOOL_FINGER)] |= BIT_MASK(BTN_TOOL_FINGER);
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08002018 dev1->keybit[BIT_WORD(BTN_LEFT)] |=
2019 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
Jiri Slaby7b19ada2007-10-18 23:40:32 -07002021 dev1->evbit[BIT_WORD(EV_ABS)] |= BIT_MASK(EV_ABS);
Seth Forshee25bded72011-11-07 19:53:36 -08002022
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002023 priv->set_abs_params(priv, dev1);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05002024 input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002026 if (priv->flags & ALPS_WHEEL) {
Jiri Slaby7b19ada2007-10-18 23:40:32 -07002027 dev1->evbit[BIT_WORD(EV_REL)] |= BIT_MASK(EV_REL);
2028 dev1->relbit[BIT_WORD(REL_WHEEL)] |= BIT_MASK(REL_WHEEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 }
2030
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002031 if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
Jiri Slaby7b19ada2007-10-18 23:40:32 -07002032 dev1->keybit[BIT_WORD(BTN_FORWARD)] |= BIT_MASK(BTN_FORWARD);
2033 dev1->keybit[BIT_WORD(BTN_BACK)] |= BIT_MASK(BTN_BACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 }
2035
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002036 if (priv->flags & ALPS_FOUR_BUTTONS) {
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08002037 dev1->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_0);
2038 dev1->keybit[BIT_WORD(BTN_1)] |= BIT_MASK(BTN_1);
2039 dev1->keybit[BIT_WORD(BTN_2)] |= BIT_MASK(BTN_2);
2040 dev1->keybit[BIT_WORD(BTN_3)] |= BIT_MASK(BTN_3);
2041 } else {
2042 dev1->keybit[BIT_WORD(BTN_MIDDLE)] |= BIT_MASK(BTN_MIDDLE);
2043 }
2044
Dmitry Torokhov08ffce42006-06-26 01:45:10 -04002045 snprintf(priv->phys, sizeof(priv->phys), "%s/input1", psmouse->ps2dev.serio->phys);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05002046 dev2->phys = priv->phys;
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002047 dev2->name = (priv->flags & ALPS_DUALPOINT) ?
Yunkang Tang9354f262013-10-15 23:50:38 -07002048 "DualPoint Stick" : "ALPS PS/2 Device";
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05002049 dev2->id.bustype = BUS_I8042;
2050 dev2->id.vendor = 0x0002;
2051 dev2->id.product = PSMOUSE_ALPS;
2052 dev2->id.version = 0x0000;
Dmitry Torokhov1db3a342008-03-18 00:29:18 -04002053 dev2->dev.parent = &psmouse->ps2dev.serio->dev;
Dmitry Torokhov968ac842005-05-29 02:28:29 -05002054
Jiri Slaby7b19ada2007-10-18 23:40:32 -07002055 dev2->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08002056 dev2->relbit[BIT_WORD(REL_X)] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
2057 dev2->keybit[BIT_WORD(BTN_LEFT)] =
2058 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_MIDDLE) | BIT_MASK(BTN_RIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059
Dmitry Torokhovf42649e2007-04-12 01:31:13 -04002060 if (input_register_device(priv->dev2))
2061 goto init_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062
2063 psmouse->protocol_handler = alps_process_byte;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05002064 psmouse->poll = alps_poll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 psmouse->disconnect = alps_disconnect;
2066 psmouse->reconnect = alps_reconnect;
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002067 psmouse->pktsize = priv->proto_version == ALPS_PROTO_V4 ? 8 : 6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05002069 /* We are having trouble resyncing ALPS touchpads so disable it for now */
2070 psmouse->resync_time = 0;
2071
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 return 0;
2073
2074init_fail:
Dmitry Torokhovf42649e2007-04-12 01:31:13 -04002075 psmouse_reset(psmouse);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05002076 input_free_device(dev2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 kfree(priv);
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04002078 psmouse->private = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 return -1;
2080}
2081
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07002082int alps_detect(struct psmouse *psmouse, bool set_properties)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083{
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002084 struct alps_data dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002086 if (alps_identify(psmouse, &dummy) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 return -1;
2088
2089 if (set_properties) {
2090 psmouse->vendor = "ALPS";
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002091 psmouse->name = dummy.flags & ALPS_DUALPOINT ?
Dmitry Torokhov968ac842005-05-29 02:28:29 -05002092 "DualPoint TouchPad" : "GlidePoint";
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002093 psmouse->model = dummy.proto_version << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 }
2095 return 0;
2096}
2097