blob: b48c6fb317edb89edccc4ffbf1ec14360f7751eb [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 */
Yunkang Tang38088432014-07-26 13:51:41 -0700103#define ALPS_BUTTONPAD 0x200 /* device is a clickpad */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Helge Dellere38de672006-09-10 21:54:39 -0400105static const struct alps_model_info alps_model_data[] = {
Seth Forshee25bded72011-11-07 19:53:36 -0800106 { { 0x32, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT }, /* Toshiba Salellite Pro M10 */
107 { { 0x33, 0x02, 0x0a }, 0x00, ALPS_PROTO_V1, 0x88, 0xf8, 0 }, /* UMAX-530T */
108 { { 0x53, 0x02, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
109 { { 0x53, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
110 { { 0x60, 0x03, 0xc8 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 }, /* HP ze1115 */
111 { { 0x63, 0x02, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
112 { { 0x63, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
113 { { 0x63, 0x02, 0x28 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 }, /* Fujitsu Siemens S6010 */
114 { { 0x63, 0x02, 0x3c }, 0x00, ALPS_PROTO_V2, 0x8f, 0x8f, ALPS_WHEEL }, /* Toshiba Satellite S2400-103 */
115 { { 0x63, 0x02, 0x50 }, 0x00, ALPS_PROTO_V2, 0xef, 0xef, ALPS_FW_BK_1 }, /* NEC Versa L320 */
116 { { 0x63, 0x02, 0x64 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
117 { { 0x63, 0x03, 0xc8 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT }, /* Dell Latitude D800 */
118 { { 0x73, 0x00, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_DUALPOINT }, /* ThinkPad R61 8918-5QG */
119 { { 0x73, 0x02, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
120 { { 0x73, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 }, /* Ahtec Laptop */
121 { { 0x20, 0x02, 0x0e }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT }, /* XXX */
122 { { 0x22, 0x02, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT },
123 { { 0x22, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xff, 0xff, ALPS_PASS | ALPS_DUALPOINT }, /* Dell Latitude D600 */
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800124 /* Dell Latitude E5500, E6400, E6500, Precision M4400 */
Seth Forshee25bded72011-11-07 19:53:36 -0800125 { { 0x62, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xcf, 0xcf,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800126 ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED },
Yunkang Tang95f75e92013-12-01 22:33:52 -0800127 { { 0x73, 0x00, 0x14 }, 0x00, ALPS_PROTO_V6, 0xff, 0xff, ALPS_DUALPOINT }, /* Dell XT2 */
Seth Forshee25bded72011-11-07 19:53:36 -0800128 { { 0x73, 0x02, 0x50 }, 0x00, ALPS_PROTO_V2, 0xcf, 0xcf, ALPS_FOUR_BUTTONS }, /* Dell Vostro 1400 */
129 { { 0x52, 0x01, 0x14 }, 0x00, ALPS_PROTO_V2, 0xff, 0xff,
130 ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED }, /* Toshiba Tecra A11-11L */
Seth Forshee25bded72011-11-07 19:53:36 -0800131 { { 0x73, 0x02, 0x64 }, 0x8a, ALPS_PROTO_V4, 0x8f, 0x8f, 0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132};
133
Kevin Cernekee24af5cb2013-02-13 22:22:08 -0800134static void alps_set_abs_params_st(struct alps_data *priv,
135 struct input_dev *dev1);
136static void alps_set_abs_params_mt(struct alps_data *priv,
137 struct input_dev *dev1);
138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139/*
140 * XXX - this entry is suspicious. First byte has zero lower nibble,
141 * which is what a normal mouse would report. Also, the value 0x0e
142 * isn't valid per PS/2 spec.
143 */
144
Seth Forsheed4b347b2011-11-07 19:53:15 -0800145/* Packet formats are described in Documentation/input/alps.txt */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800147static bool alps_is_valid_first_byte(struct alps_data *priv,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800148 unsigned char data)
149{
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800150 return (data & priv->mask0) == priv->byte0;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800151}
152
153static void alps_report_buttons(struct psmouse *psmouse,
154 struct input_dev *dev1, struct input_dev *dev2,
155 int left, int right, int middle)
156{
Martin Buckc91ed052010-03-13 22:23:58 -0800157 struct input_dev *dev;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800158
Martin Buckc91ed052010-03-13 22:23:58 -0800159 /*
160 * If shared button has already been reported on the
161 * other device (dev2) then this event should be also
162 * sent through that device.
163 */
164 dev = test_bit(BTN_LEFT, dev2->key) ? dev2 : dev1;
165 input_report_key(dev, BTN_LEFT, left);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800166
Martin Buckc91ed052010-03-13 22:23:58 -0800167 dev = test_bit(BTN_RIGHT, dev2->key) ? dev2 : dev1;
168 input_report_key(dev, BTN_RIGHT, right);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800169
Martin Buckc91ed052010-03-13 22:23:58 -0800170 dev = test_bit(BTN_MIDDLE, dev2->key) ? dev2 : dev1;
171 input_report_key(dev, BTN_MIDDLE, middle);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800172
Martin Buckc91ed052010-03-13 22:23:58 -0800173 /*
174 * Sync the _other_ device now, we'll do the first
175 * device later once we report the rest of the events.
176 */
177 input_sync(dev2);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800178}
179
Seth Forshee25bded72011-11-07 19:53:36 -0800180static void alps_process_packet_v1_v2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
182 struct alps_data *priv = psmouse->private;
183 unsigned char *packet = psmouse->packet;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500184 struct input_dev *dev = psmouse->dev;
185 struct input_dev *dev2 = priv->dev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 int x, y, z, ges, fin, left, right, middle;
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500187 int back = 0, forward = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800189 if (priv->proto_version == ALPS_PROTO_V1) {
Yotam Medinid2f40122006-05-29 23:30:36 -0400190 left = packet[2] & 0x10;
191 right = packet[2] & 0x08;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 middle = 0;
193 x = packet[1] | ((packet[0] & 0x07) << 7);
194 y = packet[4] | ((packet[3] & 0x07) << 7);
195 z = packet[5];
196 } else {
197 left = packet[3] & 1;
198 right = packet[3] & 2;
199 middle = packet[3] & 4;
200 x = packet[1] | ((packet[2] & 0x78) << (7 - 3));
201 y = packet[4] | ((packet[3] & 0x70) << (7 - 4));
202 z = packet[5];
203 }
204
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800205 if (priv->flags & ALPS_FW_BK_1) {
Laszlo Kajan3c00bb92008-03-18 00:39:55 -0400206 back = packet[0] & 0x10;
207 forward = packet[2] & 4;
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500208 }
209
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800210 if (priv->flags & ALPS_FW_BK_2) {
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500211 back = packet[3] & 4;
212 forward = packet[2] & 4;
213 if ((middle = forward && back))
214 forward = back = 0;
215 }
216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 ges = packet[2] & 1;
218 fin = packet[2] & 2;
219
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800220 if ((priv->flags & ALPS_DUALPOINT) && z == 127) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 input_report_rel(dev2, REL_X, (x > 383 ? (x - 768) : x));
222 input_report_rel(dev2, REL_Y, -(y > 255 ? (y - 512) : y));
Ulrich Dangeld7ed5d82009-06-11 00:15:09 -0700223
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800224 alps_report_buttons(psmouse, dev2, dev, left, right, middle);
Ulrich Dangeld7ed5d82009-06-11 00:15:09 -0700225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 input_sync(dev2);
227 return;
228 }
229
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800230 alps_report_buttons(psmouse, dev, dev2, left, right, middle);
Ulrich Dangeld7ed5d82009-06-11 00:15:09 -0700231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 /* Convert hardware tap to a reasonable Z value */
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800233 if (ges && !fin)
234 z = 40;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 /*
237 * A "tap and drag" operation is reported by the hardware as a transition
238 * from (!fin && ges) to (fin && ges). This should be translated to the
239 * sequence Z>0, Z==0, Z>0, so the Z==0 event has to be generated manually.
240 */
241 if (ges && fin && !priv->prev_fin) {
242 input_report_abs(dev, ABS_X, x);
243 input_report_abs(dev, ABS_Y, y);
244 input_report_abs(dev, ABS_PRESSURE, 0);
245 input_report_key(dev, BTN_TOOL_FINGER, 0);
246 input_sync(dev);
247 }
248 priv->prev_fin = fin;
249
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800250 if (z > 30)
251 input_report_key(dev, BTN_TOUCH, 1);
252 if (z < 25)
253 input_report_key(dev, BTN_TOUCH, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255 if (z > 0) {
256 input_report_abs(dev, ABS_X, x);
257 input_report_abs(dev, ABS_Y, y);
258 }
259
260 input_report_abs(dev, ABS_PRESSURE, z);
261 input_report_key(dev, BTN_TOOL_FINGER, z > 0);
262
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800263 if (priv->flags & ALPS_WHEEL)
Vojtech Pavlike6c047b2005-09-04 01:40:43 -0500264 input_report_rel(dev, REL_WHEEL, ((packet[2] << 1) & 0x08) - ((packet[0] >> 4) & 0x07));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800266 if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500267 input_report_key(dev, BTN_FORWARD, forward);
268 input_report_key(dev, BTN_BACK, back);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 }
270
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800271 if (priv->flags & ALPS_FOUR_BUTTONS) {
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800272 input_report_key(dev, BTN_0, packet[2] & 4);
273 input_report_key(dev, BTN_1, packet[0] & 0x10);
274 input_report_key(dev, BTN_2, packet[3] & 4);
275 input_report_key(dev, BTN_3, packet[0] & 0x20);
276 }
277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 input_sync(dev);
279}
280
Seth Forshee01ce6612011-11-07 19:54:13 -0800281/*
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800282 * Process bitmap data for V5 protocols. Return value is null.
283 *
284 * The bitmaps don't have enough data to track fingers, so this function
285 * only generates points representing a bounding box of at most two contacts.
Hans de Goede02d04252014-07-25 22:43:35 -0700286 * These two points are returned in fields->mt.
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800287 */
288static void alps_process_bitmap_dolphin(struct alps_data *priv,
Hans de Goede02d04252014-07-25 22:43:35 -0700289 struct alps_fields *fields)
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800290{
291 int box_middle_x, box_middle_y;
292 unsigned int x_map, y_map;
293 unsigned char start_bit, end_bit;
294 unsigned char x_msb, x_lsb, y_msb, y_lsb;
295
296 x_map = fields->x_map;
297 y_map = fields->y_map;
298
299 if (!x_map || !y_map)
300 return;
301
302 /* Get Most-significant and Least-significant bit */
303 x_msb = fls(x_map);
304 x_lsb = ffs(x_map);
305 y_msb = fls(y_map);
306 y_lsb = ffs(y_map);
307
308 /* Most-significant bit should never exceed max sensor line number */
309 if (x_msb > priv->x_bits || y_msb > priv->y_bits)
310 return;
311
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800312 if (fields->fingers > 1) {
313 start_bit = priv->x_bits - x_msb;
314 end_bit = priv->x_bits - x_lsb;
315 box_middle_x = (priv->x_max * (start_bit + end_bit)) /
316 (2 * (priv->x_bits - 1));
317
318 start_bit = y_lsb - 1;
319 end_bit = y_msb - 1;
320 box_middle_y = (priv->y_max * (start_bit + end_bit)) /
321 (2 * (priv->y_bits - 1));
Hans de Goede02d04252014-07-25 22:43:35 -0700322 fields->mt[0] = fields->st;
323 fields->mt[1].x = 2 * box_middle_x - fields->mt[0].x;
324 fields->mt[1].y = 2 * box_middle_y - fields->mt[0].y;
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800325 }
326}
327
Hans de Goede036e6c72014-07-25 22:38:51 -0700328static void alps_get_bitmap_points(unsigned int map,
329 struct alps_bitmap_point *low,
330 struct alps_bitmap_point *high,
331 int *fingers)
332{
333 struct alps_bitmap_point *point;
334 int i, bit, prev_bit = 0;
335
336 point = low;
337 for (i = 0; map != 0; i++, map >>= 1) {
338 bit = map & 1;
339 if (bit) {
340 if (!prev_bit) {
341 point->start_bit = i;
Hans de Goede105affb2014-07-25 22:41:51 -0700342 point->num_bits = 0;
Hans de Goede036e6c72014-07-25 22:38:51 -0700343 (*fingers)++;
344 }
345 point->num_bits++;
346 } else {
347 if (prev_bit)
348 point = high;
Hans de Goede036e6c72014-07-25 22:38:51 -0700349 }
350 prev_bit = bit;
351 }
352}
353
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800354/*
Seth Forshee01ce6612011-11-07 19:54:13 -0800355 * Process bitmap data from v3 and v4 protocols. Returns the number of
356 * fingers detected. A return value of 0 means at least one of the
357 * bitmaps was empty.
358 *
359 * The bitmaps don't have enough data to track fingers, so this function
360 * only generates points representing a bounding box of all contacts.
Hans de Goede02d04252014-07-25 22:43:35 -0700361 * These points are returned in fields->mt when the return value
Seth Forshee01ce6612011-11-07 19:54:13 -0800362 * is greater than 0.
363 */
Kevin Cernekee7a9f73e2013-02-13 22:24:55 -0800364static int alps_process_bitmap(struct alps_data *priv,
Hans de Goede02d04252014-07-25 22:43:35 -0700365 struct alps_fields *fields)
Seth Forshee01ce6612011-11-07 19:54:13 -0800366{
Hans de Goede036e6c72014-07-25 22:38:51 -0700367 int i, fingers_x = 0, fingers_y = 0, fingers;
Seth Forshee01ce6612011-11-07 19:54:13 -0800368 struct alps_bitmap_point x_low = {0,}, x_high = {0,};
369 struct alps_bitmap_point y_low = {0,}, y_high = {0,};
Seth Forshee01ce6612011-11-07 19:54:13 -0800370
Hans de Goede02d04252014-07-25 22:43:35 -0700371 if (!fields->x_map || !fields->y_map)
Seth Forshee01ce6612011-11-07 19:54:13 -0800372 return 0;
373
Hans de Goede02d04252014-07-25 22:43:35 -0700374 alps_get_bitmap_points(fields->x_map, &x_low, &x_high, &fingers_x);
375 alps_get_bitmap_points(fields->y_map, &y_low, &y_high, &fingers_y);
Seth Forshee01ce6612011-11-07 19:54:13 -0800376
377 /*
378 * Fingers can overlap, so we use the maximum count of fingers
379 * on either axis as the finger count.
380 */
381 fingers = max(fingers_x, fingers_y);
382
383 /*
Hans de Goede20bea682014-07-25 22:33:33 -0700384 * If an axis reports only a single contact, we have overlapping or
385 * adjacent fingers. Divide the single contact between the two points.
Seth Forshee01ce6612011-11-07 19:54:13 -0800386 */
Hans de Goede20bea682014-07-25 22:33:33 -0700387 if (fingers_x == 1) {
Hans de Goede28835f42014-07-25 22:42:53 -0700388 i = (x_low.num_bits - 1) / 2;
Hans de Goede20bea682014-07-25 22:33:33 -0700389 x_low.num_bits = x_low.num_bits - i;
390 x_high.start_bit = x_low.start_bit + i;
391 x_high.num_bits = max(i, 1);
392 }
393 if (fingers_y == 1) {
Hans de Goede28835f42014-07-25 22:42:53 -0700394 i = (y_low.num_bits - 1) / 2;
Hans de Goede20bea682014-07-25 22:33:33 -0700395 y_low.num_bits = y_low.num_bits - i;
396 y_high.start_bit = y_low.start_bit + i;
397 y_high.num_bits = max(i, 1);
Seth Forshee01ce6612011-11-07 19:54:13 -0800398 }
399
Hans de Goede02d04252014-07-25 22:43:35 -0700400 fields->mt[0].x =
401 (priv->x_max * (2 * x_low.start_bit + x_low.num_bits - 1)) /
402 (2 * (priv->x_bits - 1));
403 fields->mt[0].y =
404 (priv->y_max * (2 * y_low.start_bit + y_low.num_bits - 1)) /
405 (2 * (priv->y_bits - 1));
Seth Forshee01ce6612011-11-07 19:54:13 -0800406
Hans de Goede02d04252014-07-25 22:43:35 -0700407 fields->mt[1].x =
408 (priv->x_max * (2 * x_high.start_bit + x_high.num_bits - 1)) /
409 (2 * (priv->x_bits - 1));
410 fields->mt[1].y =
411 (priv->y_max * (2 * y_high.start_bit + y_high.num_bits - 1)) /
412 (2 * (priv->y_bits - 1));
Seth Forshee01ce6612011-11-07 19:54:13 -0800413
Hans de Goede40e8f532014-07-25 22:37:15 -0700414 /* y-bitmap order is reversed, except on rushmore */
415 if (!(priv->flags & ALPS_IS_RUSHMORE)) {
Hans de Goede02d04252014-07-25 22:43:35 -0700416 fields->mt[0].y = priv->y_max - fields->mt[0].y;
417 fields->mt[1].y = priv->y_max - fields->mt[1].y;
Hans de Goede40e8f532014-07-25 22:37:15 -0700418 }
419
Seth Forshee01ce6612011-11-07 19:54:13 -0800420 return fingers;
421}
422
Hans de Goedecdf333e2014-07-25 22:44:42 -0700423static void alps_set_slot(struct input_dev *dev, int slot, int x, int y)
Seth Forshee01ce6612011-11-07 19:54:13 -0800424{
425 input_mt_slot(dev, slot);
Hans de Goedecdf333e2014-07-25 22:44:42 -0700426 input_mt_report_slot_state(dev, MT_TOOL_FINGER, true);
427 input_report_abs(dev, ABS_MT_POSITION_X, x);
428 input_report_abs(dev, ABS_MT_POSITION_Y, y);
Seth Forshee01ce6612011-11-07 19:54:13 -0800429}
430
Hans de Goedecdf333e2014-07-25 22:44:42 -0700431static void alps_report_mt_data(struct psmouse *psmouse, int n)
Seth Forshee01ce6612011-11-07 19:54:13 -0800432{
Hans de Goede02d04252014-07-25 22:43:35 -0700433 struct alps_data *priv = psmouse->private;
434 struct input_dev *dev = psmouse->dev;
435 struct alps_fields *f = &priv->f;
Hans de Goedecdf333e2014-07-25 22:44:42 -0700436 int i, slot[MAX_TOUCHES];
Hans de Goede02d04252014-07-25 22:43:35 -0700437
Hans de Goedecdf333e2014-07-25 22:44:42 -0700438 input_mt_assign_slots(dev, slot, f->mt, n);
439 for (i = 0; i < n; i++)
440 alps_set_slot(dev, slot[i], f->mt[i].x, f->mt[i].y);
441
442 input_mt_sync_frame(dev);
Seth Forshee01ce6612011-11-07 19:54:13 -0800443}
444
Hans de Goede68c21872014-07-25 22:47:25 -0700445static void alps_report_semi_mt_data(struct psmouse *psmouse, int fingers)
446{
447 struct alps_data *priv = psmouse->private;
448 struct input_dev *dev = psmouse->dev;
449 struct alps_fields *f = &priv->f;
450
451 /* Use st data when we don't have mt data */
452 if (fingers < 2) {
453 f->mt[0].x = f->st.x;
454 f->mt[0].y = f->st.y;
455 fingers = f->pressure > 0 ? 1 : 0;
456 }
457
Hans de Goede99d99962014-07-25 22:48:02 -0700458 alps_report_mt_data(psmouse, (fingers <= 2) ? fingers : 2);
Hans de Goede68c21872014-07-25 22:47:25 -0700459
460 input_mt_report_finger_count(dev, fingers);
461
462 input_report_key(dev, BTN_LEFT, f->left);
463 input_report_key(dev, BTN_RIGHT, f->right);
464 input_report_key(dev, BTN_MIDDLE, f->middle);
465
466 input_report_abs(dev, ABS_PRESSURE, f->pressure);
467
468 input_sync(dev);
469}
470
Seth Forshee25bded72011-11-07 19:53:36 -0800471static void alps_process_trackstick_packet_v3(struct psmouse *psmouse)
472{
473 struct alps_data *priv = psmouse->private;
474 unsigned char *packet = psmouse->packet;
475 struct input_dev *dev = priv->dev2;
476 int x, y, z, left, right, middle;
477
478 /* Sanity check packet */
479 if (!(packet[0] & 0x40)) {
480 psmouse_dbg(psmouse, "Bad trackstick packet, discarding\n");
481 return;
482 }
483
484 /*
485 * There's a special packet that seems to indicate the end
486 * of a stream of trackstick data. Filter these out.
487 */
488 if (packet[1] == 0x7f && packet[2] == 0x7f && packet[4] == 0x7f)
489 return;
490
491 x = (s8)(((packet[0] & 0x20) << 2) | (packet[1] & 0x7f));
492 y = (s8)(((packet[0] & 0x10) << 3) | (packet[2] & 0x7f));
493 z = (packet[4] & 0x7c) >> 2;
494
495 /*
496 * The x and y values tend to be quite large, and when used
497 * alone the trackstick is difficult to use. Scale them down
498 * to compensate.
499 */
500 x /= 8;
501 y /= 8;
502
503 input_report_rel(dev, REL_X, x);
504 input_report_rel(dev, REL_Y, -y);
505
506 /*
507 * Most ALPS models report the trackstick buttons in the touchpad
508 * packets, but a few report them here. No reliable way has been
509 * found to differentiate between the models upfront, so we enable
510 * the quirk in response to seeing a button press in the trackstick
511 * packet.
512 */
513 left = packet[3] & 0x01;
514 right = packet[3] & 0x02;
515 middle = packet[3] & 0x04;
516
517 if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) &&
518 (left || right || middle))
519 priv->quirks |= ALPS_QUIRK_TRACKSTICK_BUTTONS;
520
521 if (priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) {
522 input_report_key(dev, BTN_LEFT, left);
523 input_report_key(dev, BTN_RIGHT, right);
524 input_report_key(dev, BTN_MIDDLE, middle);
525 }
526
527 input_sync(dev);
528 return;
529}
530
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800531static void alps_decode_buttons_v3(struct alps_fields *f, unsigned char *p)
532{
533 f->left = !!(p[3] & 0x01);
534 f->right = !!(p[3] & 0x02);
535 f->middle = !!(p[3] & 0x04);
536
537 f->ts_left = !!(p[3] & 0x10);
538 f->ts_right = !!(p[3] & 0x20);
539 f->ts_middle = !!(p[3] & 0x40);
540}
541
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700542static int alps_decode_pinnacle(struct alps_fields *f, unsigned char *p,
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800543 struct psmouse *psmouse)
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800544{
545 f->first_mp = !!(p[4] & 0x40);
546 f->is_mp = !!(p[0] & 0x40);
547
548 f->fingers = (p[5] & 0x3) + 1;
549 f->x_map = ((p[4] & 0x7e) << 8) |
550 ((p[1] & 0x7f) << 2) |
551 ((p[0] & 0x30) >> 4);
552 f->y_map = ((p[3] & 0x70) << 4) |
553 ((p[2] & 0x7f) << 1) |
554 (p[4] & 0x01);
555
Hans de Goede02d04252014-07-25 22:43:35 -0700556 f->st.x = ((p[1] & 0x7f) << 4) | ((p[4] & 0x30) >> 2) |
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800557 ((p[0] & 0x30) >> 4);
Hans de Goede02d04252014-07-25 22:43:35 -0700558 f->st.y = ((p[2] & 0x7f) << 4) | (p[4] & 0x0f);
559 f->pressure = p[5] & 0x7f;
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800560
561 alps_decode_buttons_v3(f, p);
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700562
563 return 0;
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800564}
565
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700566static int alps_decode_rushmore(struct alps_fields *f, unsigned char *p,
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800567 struct psmouse *psmouse)
Kevin Cernekee1302bac2013-02-13 22:27:08 -0800568{
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800569 alps_decode_pinnacle(f, p, psmouse);
Kevin Cernekee1302bac2013-02-13 22:27:08 -0800570
Yunkang Tangf105e342014-07-25 22:29:24 -0700571 /* Rushmore's packet decode has a bit difference with Pinnacle's */
572 f->is_mp = !!(p[5] & 0x40);
573 f->fingers = max((p[5] & 0x3), ((p[5] >> 2) & 0x3)) + 1;
Kevin Cernekee1302bac2013-02-13 22:27:08 -0800574 f->x_map |= (p[5] & 0x10) << 11;
575 f->y_map |= (p[5] & 0x20) << 6;
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700576
577 return 0;
Kevin Cernekee1302bac2013-02-13 22:27:08 -0800578}
579
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700580static int alps_decode_dolphin(struct alps_fields *f, unsigned char *p,
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800581 struct psmouse *psmouse)
Dave Turvene75af9e52013-02-21 22:58:28 -0800582{
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800583 u64 palm_data = 0;
584 struct alps_data *priv = psmouse->private;
585
Dave Turvene75af9e52013-02-21 22:58:28 -0800586 f->first_mp = !!(p[0] & 0x02);
587 f->is_mp = !!(p[0] & 0x20);
588
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800589 if (!f->is_mp) {
Hans de Goede02d04252014-07-25 22:43:35 -0700590 f->st.x = ((p[1] & 0x7f) | ((p[4] & 0x0f) << 7));
591 f->st.y = ((p[2] & 0x7f) | ((p[4] & 0xf0) << 3));
592 f->pressure = (p[0] & 4) ? 0 : p[5] & 0x7f;
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800593 alps_decode_buttons_v3(f, p);
594 } else {
595 f->fingers = ((p[0] & 0x6) >> 1 |
Dave Turvene75af9e52013-02-21 22:58:28 -0800596 (p[0] & 0x10) >> 2);
Dave Turvene75af9e52013-02-21 22:58:28 -0800597
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800598 palm_data = (p[1] & 0x7f) |
599 ((p[2] & 0x7f) << 7) |
600 ((p[4] & 0x7f) << 14) |
601 ((p[5] & 0x7f) << 21) |
602 ((p[3] & 0x07) << 28) |
603 (((u64)p[3] & 0x70) << 27) |
604 (((u64)p[0] & 0x01) << 34);
Dave Turvene75af9e52013-02-21 22:58:28 -0800605
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800606 /* Y-profile is stored in P(0) to p(n-1), n = y_bits; */
607 f->y_map = palm_data & (BIT(priv->y_bits) - 1);
608
609 /* X-profile is stored in p(n) to p(n+m-1), m = x_bits; */
610 f->x_map = (palm_data >> priv->y_bits) &
611 (BIT(priv->x_bits) - 1);
612 }
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700613
614 return 0;
Dave Turvene75af9e52013-02-21 22:58:28 -0800615}
616
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800617static void alps_process_touchpad_packet_v3_v5(struct psmouse *psmouse)
Seth Forshee25bded72011-11-07 19:53:36 -0800618{
619 struct alps_data *priv = psmouse->private;
620 unsigned char *packet = psmouse->packet;
Seth Forshee25bded72011-11-07 19:53:36 -0800621 struct input_dev *dev2 = priv->dev2;
Hans de Goede02d04252014-07-25 22:43:35 -0700622 struct alps_fields *f = &priv->f;
623 int fingers = 0;
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800624
Hans de Goede02d04252014-07-25 22:43:35 -0700625 memset(f, 0, sizeof(*f));
626
627 priv->decode_fields(f, packet, psmouse);
Seth Forshee25bded72011-11-07 19:53:36 -0800628
629 /*
Seth Forshee01ce6612011-11-07 19:54:13 -0800630 * There's no single feature of touchpad position and bitmap packets
631 * that can be used to distinguish between them. We rely on the fact
632 * that a bitmap packet should always follow a position packet with
633 * bit 6 of packet[4] set.
Seth Forshee25bded72011-11-07 19:53:36 -0800634 */
635 if (priv->multi_packet) {
Seth Forshee25bded72011-11-07 19:53:36 -0800636 /*
637 * Sometimes a position packet will indicate a multi-packet
638 * sequence, but then what follows is another position
639 * packet. Check for this, and when it happens process the
640 * position packet as usual.
641 */
Hans de Goede02d04252014-07-25 22:43:35 -0700642 if (f->is_mp) {
643 fingers = f->fingers;
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800644 if (priv->proto_version == ALPS_PROTO_V3) {
Hans de Goede02d04252014-07-25 22:43:35 -0700645 if (alps_process_bitmap(priv, f) == 0)
Hans de Goede20bea682014-07-25 22:33:33 -0700646 fingers = 0; /* Use st data */
Seth Forshee01ce6612011-11-07 19:54:13 -0800647
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800648 /* Now process position packet */
Hans de Goede02d04252014-07-25 22:43:35 -0700649 priv->decode_fields(f, priv->multi_data,
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800650 psmouse);
651 } else {
652 /*
653 * Because Dolphin uses position packet's
654 * coordinate data as Pt1 and uses it to
655 * calculate Pt2, so we need to do position
656 * packet decode first.
657 */
Hans de Goede02d04252014-07-25 22:43:35 -0700658 priv->decode_fields(f, priv->multi_data,
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800659 psmouse);
660
661 /*
662 * Since Dolphin's finger number is reliable,
663 * there is no need to compare with bmap_fn.
664 */
Hans de Goede02d04252014-07-25 22:43:35 -0700665 alps_process_bitmap_dolphin(priv, f);
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800666 }
Seth Forshee01ce6612011-11-07 19:54:13 -0800667 } else {
668 priv->multi_packet = 0;
Seth Forshee25bded72011-11-07 19:53:36 -0800669 }
670 }
671
Seth Forshee01ce6612011-11-07 19:54:13 -0800672 /*
673 * Bit 6 of byte 0 is not usually set in position packets. The only
674 * times it seems to be set is in situations where the data is
675 * suspect anyway, e.g. a palm resting flat on the touchpad. Given
676 * this combined with the fact that this bit is useful for filtering
677 * out misidentified bitmap packets, we reject anything with this
678 * bit set.
679 */
Hans de Goede02d04252014-07-25 22:43:35 -0700680 if (f->is_mp)
Seth Forshee01ce6612011-11-07 19:54:13 -0800681 return;
682
Hans de Goede02d04252014-07-25 22:43:35 -0700683 if (!priv->multi_packet && f->first_mp) {
Seth Forshee25bded72011-11-07 19:53:36 -0800684 priv->multi_packet = 1;
Seth Forshee01ce6612011-11-07 19:54:13 -0800685 memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
686 return;
687 }
688
689 priv->multi_packet = 0;
Seth Forshee25bded72011-11-07 19:53:36 -0800690
Seth Forshee25bded72011-11-07 19:53:36 -0800691 /*
692 * Sometimes the hardware sends a single packet with z = 0
693 * in the middle of a stream. Real releases generate packets
694 * with x, y, and z all zero, so these seem to be flukes.
695 * Ignore them.
696 */
Hans de Goede02d04252014-07-25 22:43:35 -0700697 if (f->st.x && f->st.y && !f->pressure)
Seth Forshee25bded72011-11-07 19:53:36 -0800698 return;
699
Hans de Goede68c21872014-07-25 22:47:25 -0700700 alps_report_semi_mt_data(psmouse, fingers);
Seth Forshee25bded72011-11-07 19:53:36 -0800701
702 if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS)) {
Hans de Goede02d04252014-07-25 22:43:35 -0700703 input_report_key(dev2, BTN_LEFT, f->ts_left);
704 input_report_key(dev2, BTN_RIGHT, f->ts_right);
705 input_report_key(dev2, BTN_MIDDLE, f->ts_middle);
Seth Forshee25bded72011-11-07 19:53:36 -0800706 input_sync(dev2);
707 }
708}
709
710static void alps_process_packet_v3(struct psmouse *psmouse)
711{
712 unsigned char *packet = psmouse->packet;
713
714 /*
715 * v3 protocol packets come in three types, two representing
716 * touchpad data and one representing trackstick data.
717 * Trackstick packets seem to be distinguished by always
718 * having 0x3f in the last byte. This value has never been
719 * observed in the last byte of either of the other types
720 * of packets.
721 */
722 if (packet[5] == 0x3f) {
723 alps_process_trackstick_packet_v3(psmouse);
724 return;
725 }
726
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800727 alps_process_touchpad_packet_v3_v5(psmouse);
Seth Forshee25bded72011-11-07 19:53:36 -0800728}
729
Yunkang Tang95f75e92013-12-01 22:33:52 -0800730static void alps_process_packet_v6(struct psmouse *psmouse)
731{
732 struct alps_data *priv = psmouse->private;
733 unsigned char *packet = psmouse->packet;
734 struct input_dev *dev = psmouse->dev;
735 struct input_dev *dev2 = priv->dev2;
736 int x, y, z, left, right, middle;
737
738 /*
739 * We can use Byte5 to distinguish if the packet is from Touchpad
740 * or Trackpoint.
741 * Touchpad: 0 - 0x7E
742 * Trackpoint: 0x7F
743 */
744 if (packet[5] == 0x7F) {
745 /* It should be a DualPoint when received Trackpoint packet */
746 if (!(priv->flags & ALPS_DUALPOINT))
747 return;
748
749 /* Trackpoint packet */
750 x = packet[1] | ((packet[3] & 0x20) << 2);
751 y = packet[2] | ((packet[3] & 0x40) << 1);
752 z = packet[4];
753 left = packet[3] & 0x01;
754 right = packet[3] & 0x02;
755 middle = packet[3] & 0x04;
756
757 /* To prevent the cursor jump when finger lifted */
758 if (x == 0x7F && y == 0x7F && z == 0x7F)
759 x = y = z = 0;
760
761 /* Divide 4 since trackpoint's speed is too fast */
762 input_report_rel(dev2, REL_X, (char)x / 4);
763 input_report_rel(dev2, REL_Y, -((char)y / 4));
764
765 input_report_key(dev2, BTN_LEFT, left);
766 input_report_key(dev2, BTN_RIGHT, right);
767 input_report_key(dev2, BTN_MIDDLE, middle);
768
769 input_sync(dev2);
770 return;
771 }
772
773 /* Touchpad packet */
774 x = packet[1] | ((packet[3] & 0x78) << 4);
775 y = packet[2] | ((packet[4] & 0x78) << 4);
776 z = packet[5];
777 left = packet[3] & 0x01;
778 right = packet[3] & 0x02;
779
780 if (z > 30)
781 input_report_key(dev, BTN_TOUCH, 1);
782 if (z < 25)
783 input_report_key(dev, BTN_TOUCH, 0);
784
785 if (z > 0) {
786 input_report_abs(dev, ABS_X, x);
787 input_report_abs(dev, ABS_Y, y);
788 }
789
790 input_report_abs(dev, ABS_PRESSURE, z);
791 input_report_key(dev, BTN_TOOL_FINGER, z > 0);
792
793 /* v6 touchpad does not have middle button */
794 input_report_key(dev, BTN_LEFT, left);
795 input_report_key(dev, BTN_RIGHT, right);
796
797 input_sync(dev);
798}
799
Seth Forshee25bded72011-11-07 19:53:36 -0800800static void alps_process_packet_v4(struct psmouse *psmouse)
801{
George Pantalos3b7e09f2012-05-10 22:31:59 -0700802 struct alps_data *priv = psmouse->private;
Seth Forshee25bded72011-11-07 19:53:36 -0800803 unsigned char *packet = psmouse->packet;
Hans de Goede02d04252014-07-25 22:43:35 -0700804 struct alps_fields *f = &priv->f;
Hans de Goede68c21872014-07-25 22:47:25 -0700805 int offset;
George Pantalos3b7e09f2012-05-10 22:31:59 -0700806
807 /*
808 * v4 has a 6-byte encoding for bitmap data, but this data is
809 * broken up between 3 normal packets. Use priv->multi_packet to
810 * track our position in the bitmap packet.
811 */
812 if (packet[6] & 0x40) {
813 /* sync, reset position */
814 priv->multi_packet = 0;
815 }
816
817 if (WARN_ON_ONCE(priv->multi_packet > 2))
818 return;
819
820 offset = 2 * priv->multi_packet;
821 priv->multi_data[offset] = packet[6];
822 priv->multi_data[offset + 1] = packet[7];
823
824 if (++priv->multi_packet > 2) {
825 priv->multi_packet = 0;
826
Hans de Goede02d04252014-07-25 22:43:35 -0700827 f->x_map = ((priv->multi_data[2] & 0x1f) << 10) |
George Pantalos3b7e09f2012-05-10 22:31:59 -0700828 ((priv->multi_data[3] & 0x60) << 3) |
829 ((priv->multi_data[0] & 0x3f) << 2) |
830 ((priv->multi_data[1] & 0x60) >> 5);
Hans de Goede02d04252014-07-25 22:43:35 -0700831 f->y_map = ((priv->multi_data[5] & 0x01) << 10) |
George Pantalos3b7e09f2012-05-10 22:31:59 -0700832 ((priv->multi_data[3] & 0x1f) << 5) |
833 (priv->multi_data[1] & 0x1f);
834
Hans de Goede02d04252014-07-25 22:43:35 -0700835 f->fingers = alps_process_bitmap(priv, f);
George Pantalos3b7e09f2012-05-10 22:31:59 -0700836 }
Seth Forshee25bded72011-11-07 19:53:36 -0800837
Hans de Goede02d04252014-07-25 22:43:35 -0700838 f->left = packet[4] & 0x01;
839 f->right = packet[4] & 0x02;
Seth Forshee25bded72011-11-07 19:53:36 -0800840
Hans de Goede02d04252014-07-25 22:43:35 -0700841 f->st.x = ((packet[1] & 0x7f) << 4) | ((packet[3] & 0x30) >> 2) |
842 ((packet[0] & 0x30) >> 4);
843 f->st.y = ((packet[2] & 0x7f) << 4) | (packet[3] & 0x0f);
844 f->pressure = packet[5] & 0x7f;
Seth Forshee25bded72011-11-07 19:53:36 -0800845
Hans de Goede68c21872014-07-25 22:47:25 -0700846 alps_report_semi_mt_data(psmouse, f->fingers);
Seth Forshee25bded72011-11-07 19:53:36 -0800847}
848
Yunkang Tang38088432014-07-26 13:51:41 -0700849static bool alps_is_valid_package_v7(struct psmouse *psmouse)
850{
851 switch (psmouse->pktcnt) {
852 case 3:
853 return (psmouse->packet[2] & 0x40) == 0x40;
854 case 4:
855 return (psmouse->packet[3] & 0x48) == 0x48;
856 case 6:
857 return (psmouse->packet[5] & 0x40) == 0x00;
858 }
859 return true;
860}
861
862static unsigned char alps_get_packet_id_v7(char *byte)
863{
864 unsigned char packet_id;
865
866 if (byte[4] & 0x40)
867 packet_id = V7_PACKET_ID_TWO;
868 else if (byte[4] & 0x01)
869 packet_id = V7_PACKET_ID_MULTI;
870 else if ((byte[0] & 0x10) && !(byte[4] & 0x43))
871 packet_id = V7_PACKET_ID_NEW;
872 else if (byte[1] == 0x00 && byte[4] == 0x00)
873 packet_id = V7_PACKET_ID_IDLE;
874 else
875 packet_id = V7_PACKET_ID_UNKNOWN;
876
877 return packet_id;
878}
879
880static void alps_get_finger_coordinate_v7(struct input_mt_pos *mt,
881 unsigned char *pkt,
882 unsigned char pkt_id)
883{
884 mt[0].x = ((pkt[2] & 0x80) << 4);
885 mt[0].x |= ((pkt[2] & 0x3F) << 5);
886 mt[0].x |= ((pkt[3] & 0x30) >> 1);
887 mt[0].x |= (pkt[3] & 0x07);
888 mt[0].y = (pkt[1] << 3) | (pkt[0] & 0x07);
889
890 mt[1].x = ((pkt[3] & 0x80) << 4);
891 mt[1].x |= ((pkt[4] & 0x80) << 3);
892 mt[1].x |= ((pkt[4] & 0x3F) << 4);
893 mt[1].y = ((pkt[5] & 0x80) << 3);
894 mt[1].y |= ((pkt[5] & 0x3F) << 4);
895
896 switch (pkt_id) {
897 case V7_PACKET_ID_TWO:
898 mt[1].x &= ~0x000F;
899 mt[1].y |= 0x000F;
900 break;
901
902 case V7_PACKET_ID_MULTI:
903 mt[1].x &= ~0x003F;
904 mt[1].y &= ~0x0020;
905 mt[1].y |= ((pkt[4] & 0x02) << 4);
906 mt[1].y |= 0x001F;
907 break;
908
909 case V7_PACKET_ID_NEW:
910 mt[1].x &= ~0x003F;
911 mt[1].x |= (pkt[0] & 0x20);
912 mt[1].y |= 0x000F;
913 break;
914 }
915
916 mt[0].y = 0x7FF - mt[0].y;
917 mt[1].y = 0x7FF - mt[1].y;
918}
919
920static int alps_get_mt_count(struct input_mt_pos *mt)
921{
Hans de Goede7091c442014-12-18 09:53:34 -0800922 int i, fingers = 0;
Yunkang Tang38088432014-07-26 13:51:41 -0700923
Hans de Goede7091c442014-12-18 09:53:34 -0800924 for (i = 0; i < MAX_TOUCHES; i++) {
925 if (mt[i].x != 0 || mt[i].y != 0)
926 fingers++;
927 }
Yunkang Tang38088432014-07-26 13:51:41 -0700928
Hans de Goede7091c442014-12-18 09:53:34 -0800929 return fingers;
Yunkang Tang38088432014-07-26 13:51:41 -0700930}
931
932static int alps_decode_packet_v7(struct alps_fields *f,
933 unsigned char *p,
934 struct psmouse *psmouse)
935{
Hans de Goeded27eb792014-12-18 09:55:14 -0800936 struct alps_data *priv = psmouse->private;
Yunkang Tang38088432014-07-26 13:51:41 -0700937 unsigned char pkt_id;
938
939 pkt_id = alps_get_packet_id_v7(p);
940 if (pkt_id == V7_PACKET_ID_IDLE)
941 return 0;
942 if (pkt_id == V7_PACKET_ID_UNKNOWN)
943 return -1;
Hans de Goede8b238112014-12-18 09:52:59 -0800944 /*
945 * NEW packets are send to indicate a discontinuity in the finger
946 * coordinate reporting. Specifically a finger may have moved from
947 * slot 0 to 1 or vice versa. INPUT_MT_TRACK takes care of this for
948 * us.
949 *
950 * NEW packets have 3 problems:
951 * 1) They do not contain middle / right button info (on non clickpads)
952 * this can be worked around by preserving the old button state
953 * 2) They do not contain an accurate fingercount, and they are
954 * typically send when the number of fingers changes. We cannot use
955 * the old finger count as that may mismatch with the amount of
956 * touch coordinates we've available in the NEW packet
957 * 3) Their x data for the second touch is inaccurate leading to
958 * a possible jump of the x coordinate by 16 units when the first
959 * non NEW packet comes in
960 * Since problems 2 & 3 cannot be worked around, just ignore them.
961 */
962 if (pkt_id == V7_PACKET_ID_NEW)
963 return 1;
Yunkang Tang38088432014-07-26 13:51:41 -0700964
965 alps_get_finger_coordinate_v7(f->mt, p, pkt_id);
966
Yunkang Tang38088432014-07-26 13:51:41 -0700967 if (pkt_id == V7_PACKET_ID_TWO)
968 f->fingers = alps_get_mt_count(f->mt);
Hans de Goede8b238112014-12-18 09:52:59 -0800969 else /* pkt_id == V7_PACKET_ID_MULTI */
Yunkang Tang38088432014-07-26 13:51:41 -0700970 f->fingers = 3 + (p[5] & 0x03);
971
Hans de Goeded27eb792014-12-18 09:55:14 -0800972 f->left = (p[0] & 0x80) >> 7;
973 if (priv->flags & ALPS_BUTTONPAD) {
974 if (p[0] & 0x20)
975 f->fingers++;
976 if (p[0] & 0x10)
977 f->fingers++;
978 } else {
979 f->right = (p[0] & 0x20) >> 5;
980 f->middle = (p[0] & 0x10) >> 4;
981 }
982
Hans de Goede7091c442014-12-18 09:53:34 -0800983 /* Sometimes a single touch is reported in mt[1] rather then mt[0] */
984 if (f->fingers == 1 && f->mt[0].x == 0 && f->mt[0].y == 0) {
985 f->mt[0].x = f->mt[1].x;
986 f->mt[0].y = f->mt[1].y;
987 f->mt[1].x = 0;
988 f->mt[1].y = 0;
989 }
990
Yunkang Tang38088432014-07-26 13:51:41 -0700991 return 0;
992}
993
994static void alps_process_trackstick_packet_v7(struct psmouse *psmouse)
995{
996 struct alps_data *priv = psmouse->private;
997 unsigned char *packet = psmouse->packet;
998 struct input_dev *dev2 = priv->dev2;
999 int x, y, z, left, right, middle;
1000
1001 /*
1002 * b7 b6 b5 b4 b3 b2 b1 b0
1003 * Byte0 0 1 0 0 1 0 0 0
1004 * Byte1 1 1 * * 1 M R L
1005 * Byte2 X7 1 X5 X4 X3 X2 X1 X0
1006 * Byte3 Z6 1 Y6 X6 1 Y2 Y1 Y0
1007 * Byte4 Y7 0 Y5 Y4 Y3 1 1 0
1008 * Byte5 T&P 0 Z5 Z4 Z3 Z2 Z1 Z0
1009 * M / R / L: Middle / Right / Left button
1010 */
1011
1012 x = ((packet[2] & 0xbf)) | ((packet[3] & 0x10) << 2);
1013 y = (packet[3] & 0x07) | (packet[4] & 0xb8) |
1014 ((packet[3] & 0x20) << 1);
1015 z = (packet[5] & 0x3f) | ((packet[3] & 0x80) >> 1);
1016
1017 left = (packet[1] & 0x01);
1018 right = (packet[1] & 0x02) >> 1;
1019 middle = (packet[1] & 0x04) >> 2;
1020
1021 /* Divide 2 since trackpoint's speed is too fast */
1022 input_report_rel(dev2, REL_X, (char)x / 2);
1023 input_report_rel(dev2, REL_Y, -((char)y / 2));
1024
1025 input_report_key(dev2, BTN_LEFT, left);
1026 input_report_key(dev2, BTN_RIGHT, right);
1027 input_report_key(dev2, BTN_MIDDLE, middle);
1028
1029 input_sync(dev2);
1030}
1031
1032static void alps_process_touchpad_packet_v7(struct psmouse *psmouse)
1033{
1034 struct alps_data *priv = psmouse->private;
1035 struct input_dev *dev = psmouse->dev;
1036 struct alps_fields *f = &priv->f;
1037
1038 memset(f, 0, sizeof(*f));
1039
1040 if (priv->decode_fields(f, psmouse->packet, psmouse))
1041 return;
1042
1043 alps_report_mt_data(psmouse, alps_get_mt_count(f->mt));
1044
1045 input_mt_report_finger_count(dev, f->fingers);
1046
1047 input_report_key(dev, BTN_LEFT, f->left);
1048 input_report_key(dev, BTN_RIGHT, f->right);
1049 input_report_key(dev, BTN_MIDDLE, f->middle);
1050
1051 input_sync(dev);
1052}
1053
1054static void alps_process_packet_v7(struct psmouse *psmouse)
1055{
1056 unsigned char *packet = psmouse->packet;
1057
1058 if (packet[0] == 0x48 && (packet[4] & 0x47) == 0x06)
1059 alps_process_trackstick_packet_v7(psmouse);
1060 else
1061 alps_process_touchpad_packet_v7(psmouse);
1062}
1063
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001064static void alps_report_bare_ps2_packet(struct psmouse *psmouse,
1065 unsigned char packet[],
1066 bool report_buttons)
1067{
1068 struct alps_data *priv = psmouse->private;
1069 struct input_dev *dev2 = priv->dev2;
1070
1071 if (report_buttons)
1072 alps_report_buttons(psmouse, dev2, psmouse->dev,
1073 packet[0] & 1, packet[0] & 2, packet[0] & 4);
1074
1075 input_report_rel(dev2, REL_X,
1076 packet[1] ? packet[1] - ((packet[0] << 4) & 0x100) : 0);
1077 input_report_rel(dev2, REL_Y,
1078 packet[2] ? ((packet[0] << 3) & 0x100) - packet[2] : 0);
1079
1080 input_sync(dev2);
1081}
1082
1083static psmouse_ret_t alps_handle_interleaved_ps2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084{
1085 struct alps_data *priv = psmouse->private;
1086
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001087 if (psmouse->pktcnt < 6)
1088 return PSMOUSE_GOOD_DATA;
1089
1090 if (psmouse->pktcnt == 6) {
1091 /*
1092 * Start a timer to flush the packet if it ends up last
1093 * 6-byte packet in the stream. Timer needs to fire
1094 * psmouse core times out itself. 20 ms should be enough
1095 * to decide if we are getting more data or not.
1096 */
1097 mod_timer(&priv->timer, jiffies + msecs_to_jiffies(20));
1098 return PSMOUSE_GOOD_DATA;
1099 }
1100
1101 del_timer(&priv->timer);
1102
1103 if (psmouse->packet[6] & 0x80) {
1104
1105 /*
1106 * Highest bit is set - that means we either had
1107 * complete ALPS packet and this is start of the
1108 * next packet or we got garbage.
1109 */
1110
1111 if (((psmouse->packet[3] |
1112 psmouse->packet[4] |
1113 psmouse->packet[5]) & 0x80) ||
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001114 (!alps_is_valid_first_byte(priv, psmouse->packet[6]))) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001115 psmouse_dbg(psmouse,
Andy Shevchenko3b112922012-10-30 00:24:41 -07001116 "refusing packet %4ph (suspected interleaved ps/2)\n",
1117 psmouse->packet + 3);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001118 return PSMOUSE_BAD_DATA;
1119 }
1120
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001121 priv->process_packet(psmouse);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001122
1123 /* Continue with the next packet */
1124 psmouse->packet[0] = psmouse->packet[6];
1125 psmouse->pktcnt = 1;
1126
1127 } else {
1128
1129 /*
1130 * High bit is 0 - that means that we indeed got a PS/2
1131 * packet in the middle of ALPS packet.
1132 *
1133 * There is also possibility that we got 6-byte ALPS
1134 * packet followed by 3-byte packet from trackpoint. We
1135 * can not distinguish between these 2 scenarios but
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001136 * because the latter is unlikely to happen in course of
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001137 * normal operation (user would need to press all
1138 * buttons on the pad and start moving trackpoint
1139 * without touching the pad surface) we assume former.
1140 * Even if we are wrong the wost thing that would happen
1141 * the cursor would jump but we should not get protocol
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001142 * de-synchronization.
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001143 */
1144
1145 alps_report_bare_ps2_packet(psmouse, &psmouse->packet[3],
1146 false);
1147
1148 /*
1149 * Continue with the standard ALPS protocol handling,
1150 * but make sure we won't process it as an interleaved
1151 * packet again, which may happen if all buttons are
1152 * pressed. To avoid this let's reset the 4th bit which
1153 * is normally 1.
1154 */
1155 psmouse->packet[3] = psmouse->packet[6] & 0xf7;
1156 psmouse->pktcnt = 4;
1157 }
1158
1159 return PSMOUSE_GOOD_DATA;
1160}
1161
1162static void alps_flush_packet(unsigned long data)
1163{
1164 struct psmouse *psmouse = (struct psmouse *)data;
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001165 struct alps_data *priv = psmouse->private;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001166
1167 serio_pause_rx(psmouse->ps2dev.serio);
1168
Seth Forsheeb46615f2011-11-07 19:53:30 -08001169 if (psmouse->pktcnt == psmouse->pktsize) {
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001170
1171 /*
1172 * We did not any more data in reasonable amount of time.
1173 * Validate the last 3 bytes and process as a standard
1174 * ALPS packet.
1175 */
1176 if ((psmouse->packet[3] |
1177 psmouse->packet[4] |
1178 psmouse->packet[5]) & 0x80) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001179 psmouse_dbg(psmouse,
Andy Shevchenko3b112922012-10-30 00:24:41 -07001180 "refusing packet %3ph (suspected interleaved ps/2)\n",
1181 psmouse->packet + 3);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001182 } else {
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001183 priv->process_packet(psmouse);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001184 }
1185 psmouse->pktcnt = 0;
1186 }
1187
1188 serio_continue_rx(psmouse->ps2dev.serio);
1189}
1190
1191static psmouse_ret_t alps_process_byte(struct psmouse *psmouse)
1192{
1193 struct alps_data *priv = psmouse->private;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001194
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 if ((psmouse->packet[0] & 0xc8) == 0x08) { /* PS/2 packet */
1196 if (psmouse->pktcnt == 3) {
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001197 alps_report_bare_ps2_packet(psmouse, psmouse->packet,
1198 true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 return PSMOUSE_FULL_PACKET;
1200 }
1201 return PSMOUSE_GOOD_DATA;
1202 }
1203
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001204 /* Check for PS/2 packet stuffed in the middle of ALPS packet. */
1205
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001206 if ((priv->flags & ALPS_PS2_INTERLEAVED) &&
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001207 psmouse->pktcnt >= 4 && (psmouse->packet[3] & 0x0f) == 0x0f) {
1208 return alps_handle_interleaved_ps2(psmouse);
1209 }
1210
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001211 if (!alps_is_valid_first_byte(priv, psmouse->packet[0])) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001212 psmouse_dbg(psmouse,
1213 "refusing packet[0] = %x (mask0 = %x, byte0 = %x)\n",
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001214 psmouse->packet[0], priv->mask0, priv->byte0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 return PSMOUSE_BAD_DATA;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001216 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
Seth Forsheeb46615f2011-11-07 19:53:30 -08001218 /* Bytes 2 - pktsize should have 0 in the highest bit */
Yunkang Tang95f75e92013-12-01 22:33:52 -08001219 if ((priv->proto_version < ALPS_PROTO_V5) &&
Dave Turvene75af9e52013-02-21 22:58:28 -08001220 psmouse->pktcnt >= 2 && psmouse->pktcnt <= psmouse->pktsize &&
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001221 (psmouse->packet[psmouse->pktcnt - 1] & 0x80)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001222 psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
1223 psmouse->pktcnt - 1,
1224 psmouse->packet[psmouse->pktcnt - 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 return PSMOUSE_BAD_DATA;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
Yunkang Tang38088432014-07-26 13:51:41 -07001228 if (priv->proto_version == ALPS_PROTO_V7 &&
1229 !alps_is_valid_package_v7(psmouse)) {
1230 psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
1231 psmouse->pktcnt - 1,
1232 psmouse->packet[psmouse->pktcnt - 1]);
1233 return PSMOUSE_BAD_DATA;
1234 }
1235
Seth Forsheeb46615f2011-11-07 19:53:30 -08001236 if (psmouse->pktcnt == psmouse->pktsize) {
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001237 priv->process_packet(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 return PSMOUSE_FULL_PACKET;
1239 }
1240
1241 return PSMOUSE_GOOD_DATA;
1242}
1243
Seth Forshee25bded72011-11-07 19:53:36 -08001244static int alps_command_mode_send_nibble(struct psmouse *psmouse, int nibble)
1245{
1246 struct ps2dev *ps2dev = &psmouse->ps2dev;
1247 struct alps_data *priv = psmouse->private;
1248 int command;
1249 unsigned char *param;
1250 unsigned char dummy[4];
1251
1252 BUG_ON(nibble > 0xf);
1253
1254 command = priv->nibble_commands[nibble].command;
1255 param = (command & 0x0f00) ?
1256 dummy : (unsigned char *)&priv->nibble_commands[nibble].data;
1257
1258 if (ps2_command(ps2dev, param, command))
1259 return -1;
1260
1261 return 0;
1262}
1263
1264static int alps_command_mode_set_addr(struct psmouse *psmouse, int addr)
1265{
1266 struct ps2dev *ps2dev = &psmouse->ps2dev;
1267 struct alps_data *priv = psmouse->private;
1268 int i, nibble;
1269
1270 if (ps2_command(ps2dev, NULL, priv->addr_command))
1271 return -1;
1272
1273 for (i = 12; i >= 0; i -= 4) {
1274 nibble = (addr >> i) & 0xf;
1275 if (alps_command_mode_send_nibble(psmouse, nibble))
1276 return -1;
1277 }
1278
1279 return 0;
1280}
1281
1282static int __alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
1283{
1284 struct ps2dev *ps2dev = &psmouse->ps2dev;
1285 unsigned char param[4];
1286
1287 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1288 return -1;
1289
1290 /*
1291 * The address being read is returned in the first two bytes
1292 * of the result. Check that this address matches the expected
1293 * address.
1294 */
1295 if (addr != ((param[0] << 8) | param[1]))
1296 return -1;
1297
1298 return param[2];
1299}
1300
1301static int alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
1302{
1303 if (alps_command_mode_set_addr(psmouse, addr))
1304 return -1;
1305 return __alps_command_mode_read_reg(psmouse, addr);
1306}
1307
1308static int __alps_command_mode_write_reg(struct psmouse *psmouse, u8 value)
1309{
1310 if (alps_command_mode_send_nibble(psmouse, (value >> 4) & 0xf))
1311 return -1;
1312 if (alps_command_mode_send_nibble(psmouse, value & 0xf))
1313 return -1;
1314 return 0;
1315}
1316
1317static int alps_command_mode_write_reg(struct psmouse *psmouse, int addr,
1318 u8 value)
1319{
1320 if (alps_command_mode_set_addr(psmouse, addr))
1321 return -1;
1322 return __alps_command_mode_write_reg(psmouse, value);
1323}
1324
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001325static int alps_rpt_cmd(struct psmouse *psmouse, int init_command,
1326 int repeated_command, unsigned char *param)
1327{
1328 struct ps2dev *ps2dev = &psmouse->ps2dev;
1329
1330 param[0] = 0;
1331 if (init_command && ps2_command(ps2dev, param, init_command))
1332 return -EIO;
1333
1334 if (ps2_command(ps2dev, NULL, repeated_command) ||
1335 ps2_command(ps2dev, NULL, repeated_command) ||
1336 ps2_command(ps2dev, NULL, repeated_command))
1337 return -EIO;
1338
1339 param[0] = param[1] = param[2] = 0xff;
1340 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1341 return -EIO;
1342
Dmitry Torokhov39fbe582013-02-14 09:04:24 -08001343 psmouse_dbg(psmouse, "%2.2X report: %3ph\n",
1344 repeated_command, param);
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001345 return 0;
1346}
1347
Yunkang Tang38088432014-07-26 13:51:41 -07001348static bool alps_check_valid_firmware_id(unsigned char id[])
1349{
1350 if (id[0] == 0x73)
1351 return true;
1352
1353 if (id[0] == 0x88 &&
1354 (id[1] == 0x07 ||
1355 id[1] == 0x08 ||
1356 (id[1] & 0xf0) == 0xb0 ||
1357 (id[1] & 0xf0) == 0xc0)) {
1358 return true;
1359 }
1360
1361 return false;
1362}
1363
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08001364static int alps_enter_command_mode(struct psmouse *psmouse)
Seth Forshee25bded72011-11-07 19:53:36 -08001365{
1366 unsigned char param[4];
Seth Forshee25bded72011-11-07 19:53:36 -08001367
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001368 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_RESET_WRAP, param)) {
Seth Forshee25bded72011-11-07 19:53:36 -08001369 psmouse_err(psmouse, "failed to enter command mode\n");
1370 return -1;
1371 }
1372
Yunkang Tang38088432014-07-26 13:51:41 -07001373 if (!alps_check_valid_firmware_id(param)) {
Seth Forshee25bded72011-11-07 19:53:36 -08001374 psmouse_dbg(psmouse,
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001375 "unknown response while entering command mode\n");
Seth Forshee25bded72011-11-07 19:53:36 -08001376 return -1;
1377 }
Seth Forshee25bded72011-11-07 19:53:36 -08001378 return 0;
1379}
1380
1381static inline int alps_exit_command_mode(struct psmouse *psmouse)
1382{
1383 struct ps2dev *ps2dev = &psmouse->ps2dev;
1384 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM))
1385 return -1;
1386 return 0;
1387}
1388
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389/*
1390 * For DualPoint devices select the device that should respond to
1391 * subsequent commands. It looks like glidepad is behind stickpointer,
1392 * I'd thought it would be other way around...
1393 */
Seth Forshee25bded72011-11-07 19:53:36 -08001394static int alps_passthrough_mode_v2(struct psmouse *psmouse, bool enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395{
1396 struct ps2dev *ps2dev = &psmouse->ps2dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 int cmd = enable ? PSMOUSE_CMD_SETSCALE21 : PSMOUSE_CMD_SETSCALE11;
1398
1399 if (ps2_command(ps2dev, NULL, cmd) ||
1400 ps2_command(ps2dev, NULL, cmd) ||
1401 ps2_command(ps2dev, NULL, cmd) ||
1402 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1403 return -1;
1404
1405 /* we may get 3 more bytes, just ignore them */
Dmitry Torokhovc6117632005-06-01 02:39:51 -05001406 ps2_drain(ps2dev, 3, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
1408 return 0;
1409}
1410
Seth Forshee25bded72011-11-07 19:53:36 -08001411static int alps_absolute_mode_v1_v2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412{
1413 struct ps2dev *ps2dev = &psmouse->ps2dev;
1414
1415 /* Try ALPS magic knock - 4 disable before enable */
1416 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1417 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1418 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1419 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1420 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
1421 return -1;
1422
1423 /*
1424 * Switch mouse to poll (remote) mode so motion data will not
1425 * get in our way
1426 */
1427 return ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETPOLL);
1428}
1429
Yunkang Tang95f75e92013-12-01 22:33:52 -08001430static int alps_monitor_mode_send_word(struct psmouse *psmouse, u16 word)
1431{
1432 int i, nibble;
1433
1434 /*
1435 * b0-b11 are valid bits, send sequence is inverse.
1436 * e.g. when word = 0x0123, nibble send sequence is 3, 2, 1
1437 */
1438 for (i = 0; i <= 8; i += 4) {
1439 nibble = (word >> i) & 0xf;
1440 if (alps_command_mode_send_nibble(psmouse, nibble))
1441 return -1;
1442 }
1443
1444 return 0;
1445}
1446
1447static int alps_monitor_mode_write_reg(struct psmouse *psmouse,
1448 u16 addr, u16 value)
1449{
1450 struct ps2dev *ps2dev = &psmouse->ps2dev;
1451
1452 /* 0x0A0 is the command to write the word */
1453 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE) ||
1454 alps_monitor_mode_send_word(psmouse, 0x0A0) ||
1455 alps_monitor_mode_send_word(psmouse, addr) ||
1456 alps_monitor_mode_send_word(psmouse, value) ||
1457 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1458 return -1;
1459
1460 return 0;
1461}
1462
1463static int alps_monitor_mode(struct psmouse *psmouse, bool enable)
1464{
1465 struct ps2dev *ps2dev = &psmouse->ps2dev;
1466
1467 if (enable) {
1468 /* EC E9 F5 F5 E7 E6 E7 E9 to enter monitor mode */
1469 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
1470 ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO) ||
1471 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1472 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1473 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1474 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1475 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1476 ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO))
1477 return -1;
1478 } else {
1479 /* EC to exit monitor mode */
1480 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP))
1481 return -1;
1482 }
1483
1484 return 0;
1485}
1486
1487static int alps_absolute_mode_v6(struct psmouse *psmouse)
1488{
1489 u16 reg_val = 0x181;
1490 int ret = -1;
1491
1492 /* enter monitor mode, to write the register */
1493 if (alps_monitor_mode(psmouse, true))
1494 return -1;
1495
1496 ret = alps_monitor_mode_write_reg(psmouse, 0x000, reg_val);
1497
1498 if (alps_monitor_mode(psmouse, false))
1499 ret = -1;
1500
1501 return ret;
1502}
1503
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504static int alps_get_status(struct psmouse *psmouse, char *param)
1505{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 /* Get status: 0xF5 0xF5 0xF5 0xE9 */
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001507 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_DISABLE, param))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 return -1;
1509
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 return 0;
1511}
1512
1513/*
1514 * Turn touchpad tapping on or off. The sequences are:
1515 * 0xE9 0xF5 0xF5 0xF3 0x0A to enable,
1516 * 0xE9 0xF5 0xF5 0xE8 0x00 to disable.
1517 * My guess that 0xE9 (GetInfo) is here as a sync point.
1518 * For models that also have stickpointer (DualPoints) its tapping
1519 * is controlled separately (0xE6 0xE6 0xE6 0xF3 0x14|0x0A) but
1520 * we don't fiddle with it.
1521 */
1522static int alps_tap_mode(struct psmouse *psmouse, int enable)
1523{
1524 struct ps2dev *ps2dev = &psmouse->ps2dev;
1525 int cmd = enable ? PSMOUSE_CMD_SETRATE : PSMOUSE_CMD_SETRES;
1526 unsigned char tap_arg = enable ? 0x0A : 0x00;
1527 unsigned char param[4];
1528
1529 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO) ||
1530 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1531 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1532 ps2_command(ps2dev, &tap_arg, cmd))
1533 return -1;
1534
1535 if (alps_get_status(psmouse, param))
1536 return -1;
1537
1538 return 0;
1539}
1540
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001541/*
1542 * alps_poll() - poll the touchpad for current motion packet.
1543 * Used in resync.
1544 */
1545static int alps_poll(struct psmouse *psmouse)
1546{
1547 struct alps_data *priv = psmouse->private;
Seth Forsheeb46615f2011-11-07 19:53:30 -08001548 unsigned char buf[sizeof(psmouse->packet)];
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001549 bool poll_failed;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001550
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001551 if (priv->flags & ALPS_PASS)
Seth Forshee25bded72011-11-07 19:53:36 -08001552 alps_passthrough_mode_v2(psmouse, true);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001553
1554 poll_failed = ps2_command(&psmouse->ps2dev, buf,
1555 PSMOUSE_CMD_POLL | (psmouse->pktsize << 8)) < 0;
1556
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001557 if (priv->flags & ALPS_PASS)
Seth Forshee25bded72011-11-07 19:53:36 -08001558 alps_passthrough_mode_v2(psmouse, false);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001559
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001560 if (poll_failed || (buf[0] & priv->mask0) != priv->byte0)
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001561 return -1;
1562
1563 if ((psmouse->badbyte & 0xc8) == 0x08) {
1564/*
1565 * Poll the track stick ...
1566 */
1567 if (ps2_command(&psmouse->ps2dev, buf, PSMOUSE_CMD_POLL | (3 << 8)))
1568 return -1;
1569 }
1570
1571 memcpy(psmouse->packet, buf, sizeof(buf));
1572 return 0;
1573}
1574
Seth Forshee25bded72011-11-07 19:53:36 -08001575static int alps_hw_init_v1_v2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576{
1577 struct alps_data *priv = psmouse->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001579 if ((priv->flags & ALPS_PASS) &&
Seth Forshee25bded72011-11-07 19:53:36 -08001580 alps_passthrough_mode_v2(psmouse, true)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 return -1;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001582 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001584 if (alps_tap_mode(psmouse, true)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001585 psmouse_warn(psmouse, "Failed to enable hardware tapping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 return -1;
Peter Osterlund963f6262005-07-11 01:08:04 -05001587 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
Seth Forshee25bded72011-11-07 19:53:36 -08001589 if (alps_absolute_mode_v1_v2(psmouse)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001590 psmouse_err(psmouse, "Failed to enable absolute mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 return -1;
1592 }
1593
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001594 if ((priv->flags & ALPS_PASS) &&
Seth Forshee25bded72011-11-07 19:53:36 -08001595 alps_passthrough_mode_v2(psmouse, false)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 return -1;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001597 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04001599 /* ALPS needs stream mode, otherwise it won't report any data */
1600 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001601 psmouse_err(psmouse, "Failed to enable stream mode\n");
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04001602 return -1;
1603 }
1604
1605 return 0;
1606}
1607
Yunkang Tang95f75e92013-12-01 22:33:52 -08001608static int alps_hw_init_v6(struct psmouse *psmouse)
1609{
1610 unsigned char param[2] = {0xC8, 0x14};
1611
1612 /* Enter passthrough mode to let trackpoint enter 6byte raw mode */
1613 if (alps_passthrough_mode_v2(psmouse, true))
1614 return -1;
1615
1616 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1617 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1618 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1619 ps2_command(&psmouse->ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
1620 ps2_command(&psmouse->ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
1621 return -1;
1622
1623 if (alps_passthrough_mode_v2(psmouse, false))
1624 return -1;
1625
1626 if (alps_absolute_mode_v6(psmouse)) {
1627 psmouse_err(psmouse, "Failed to enable absolute mode\n");
1628 return -1;
1629 }
1630
1631 return 0;
1632}
1633
Seth Forshee25bded72011-11-07 19:53:36 -08001634/*
Kevin Cernekeecd401202013-02-13 22:28:07 -08001635 * Enable or disable passthrough mode to the trackstick.
Seth Forshee25bded72011-11-07 19:53:36 -08001636 */
Kevin Cernekeecd401202013-02-13 22:28:07 -08001637static int alps_passthrough_mode_v3(struct psmouse *psmouse,
1638 int reg_base, bool enable)
Seth Forshee25bded72011-11-07 19:53:36 -08001639{
Kevin Cernekeecd401202013-02-13 22:28:07 -08001640 int reg_val, ret = -1;
Seth Forshee25bded72011-11-07 19:53:36 -08001641
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08001642 if (alps_enter_command_mode(psmouse))
Seth Forshee25bded72011-11-07 19:53:36 -08001643 return -1;
1644
Kevin Cernekeecd401202013-02-13 22:28:07 -08001645 reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x0008);
1646 if (reg_val == -1)
1647 goto error;
1648
Seth Forshee25bded72011-11-07 19:53:36 -08001649 if (enable)
1650 reg_val |= 0x01;
1651 else
1652 reg_val &= ~0x01;
1653
Kevin Cernekeecd401202013-02-13 22:28:07 -08001654 ret = __alps_command_mode_write_reg(psmouse, reg_val);
Seth Forshee25bded72011-11-07 19:53:36 -08001655
Kevin Cernekeecd401202013-02-13 22:28:07 -08001656error:
1657 if (alps_exit_command_mode(psmouse))
1658 ret = -1;
1659 return ret;
Seth Forshee25bded72011-11-07 19:53:36 -08001660}
1661
1662/* Must be in command mode when calling this function */
1663static int alps_absolute_mode_v3(struct psmouse *psmouse)
1664{
1665 int reg_val;
1666
1667 reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
1668 if (reg_val == -1)
1669 return -1;
1670
1671 reg_val |= 0x06;
1672 if (__alps_command_mode_write_reg(psmouse, reg_val))
1673 return -1;
1674
1675 return 0;
1676}
1677
Kevin Cernekeecd401202013-02-13 22:28:07 -08001678static int alps_probe_trackstick_v3(struct psmouse *psmouse, int reg_base)
1679{
1680 int ret = -EIO, reg_val;
1681
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08001682 if (alps_enter_command_mode(psmouse))
Kevin Cernekeecd401202013-02-13 22:28:07 -08001683 goto error;
1684
1685 reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x08);
1686 if (reg_val == -1)
1687 goto error;
1688
1689 /* bit 7: trackstick is present */
1690 ret = reg_val & 0x80 ? 0 : -ENODEV;
1691
1692error:
1693 alps_exit_command_mode(psmouse);
1694 return ret;
1695}
1696
1697static int alps_setup_trackstick_v3(struct psmouse *psmouse, int reg_base)
1698{
1699 struct ps2dev *ps2dev = &psmouse->ps2dev;
1700 int ret = 0;
1701 unsigned char param[4];
1702
1703 if (alps_passthrough_mode_v3(psmouse, reg_base, true))
1704 return -EIO;
1705
1706 /*
1707 * E7 report for the trackstick
1708 *
1709 * There have been reports of failures to seem to trace back
1710 * to the above trackstick check failing. When these occur
1711 * this E7 report fails, so when that happens we continue
1712 * with the assumption that there isn't a trackstick after
1713 * all.
1714 */
1715 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_SETSCALE21, param)) {
1716 psmouse_warn(psmouse, "trackstick E7 report failed\n");
1717 ret = -ENODEV;
1718 } else {
Dmitry Torokhov39fbe582013-02-14 09:04:24 -08001719 psmouse_dbg(psmouse, "trackstick E7 report: %3ph\n", param);
Kevin Cernekeecd401202013-02-13 22:28:07 -08001720
1721 /*
1722 * Not sure what this does, but it is absolutely
1723 * essential. Without it, the touchpad does not
1724 * work at all and the trackstick just emits normal
1725 * PS/2 packets.
1726 */
1727 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1728 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1729 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1730 alps_command_mode_send_nibble(psmouse, 0x9) ||
1731 alps_command_mode_send_nibble(psmouse, 0x4)) {
1732 psmouse_err(psmouse,
1733 "Error sending magic E6 sequence\n");
1734 ret = -EIO;
1735 goto error;
1736 }
1737
1738 /*
1739 * This ensures the trackstick packets are in the format
1740 * supported by this driver. If bit 1 isn't set the packet
1741 * format is different.
1742 */
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08001743 if (alps_enter_command_mode(psmouse) ||
Kevin Cernekeecd401202013-02-13 22:28:07 -08001744 alps_command_mode_write_reg(psmouse,
1745 reg_base + 0x08, 0x82) ||
1746 alps_exit_command_mode(psmouse))
1747 ret = -EIO;
1748 }
1749
1750error:
1751 if (alps_passthrough_mode_v3(psmouse, reg_base, false))
1752 ret = -EIO;
1753
1754 return ret;
1755}
1756
Seth Forshee25bded72011-11-07 19:53:36 -08001757static int alps_hw_init_v3(struct psmouse *psmouse)
1758{
Seth Forshee25bded72011-11-07 19:53:36 -08001759 struct ps2dev *ps2dev = &psmouse->ps2dev;
1760 int reg_val;
1761 unsigned char param[4];
1762
Kevin Cernekeecd401202013-02-13 22:28:07 -08001763 reg_val = alps_probe_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE);
1764 if (reg_val == -EIO)
1765 goto error;
Dmitry Torokhov39fbe582013-02-14 09:04:24 -08001766
Kevin Cernekeecd401202013-02-13 22:28:07 -08001767 if (reg_val == 0 &&
1768 alps_setup_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE) == -EIO)
Seth Forshee25bded72011-11-07 19:53:36 -08001769 goto error;
1770
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08001771 if (alps_enter_command_mode(psmouse) ||
Kevin Cernekeecd401202013-02-13 22:28:07 -08001772 alps_absolute_mode_v3(psmouse)) {
Seth Forshee25bded72011-11-07 19:53:36 -08001773 psmouse_err(psmouse, "Failed to enter absolute mode\n");
1774 goto error;
1775 }
1776
1777 reg_val = alps_command_mode_read_reg(psmouse, 0x0006);
1778 if (reg_val == -1)
1779 goto error;
1780 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
1781 goto error;
1782
1783 reg_val = alps_command_mode_read_reg(psmouse, 0x0007);
1784 if (reg_val == -1)
1785 goto error;
1786 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
1787 goto error;
1788
1789 if (alps_command_mode_read_reg(psmouse, 0x0144) == -1)
1790 goto error;
1791 if (__alps_command_mode_write_reg(psmouse, 0x04))
1792 goto error;
1793
1794 if (alps_command_mode_read_reg(psmouse, 0x0159) == -1)
1795 goto error;
1796 if (__alps_command_mode_write_reg(psmouse, 0x03))
1797 goto error;
1798
1799 if (alps_command_mode_read_reg(psmouse, 0x0163) == -1)
1800 goto error;
1801 if (alps_command_mode_write_reg(psmouse, 0x0163, 0x03))
1802 goto error;
1803
1804 if (alps_command_mode_read_reg(psmouse, 0x0162) == -1)
1805 goto error;
1806 if (alps_command_mode_write_reg(psmouse, 0x0162, 0x04))
1807 goto error;
1808
Seth Forshee25bded72011-11-07 19:53:36 -08001809 alps_exit_command_mode(psmouse);
1810
1811 /* Set rate and enable data reporting */
1812 param[0] = 0x64;
1813 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
1814 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
1815 psmouse_err(psmouse, "Failed to enable data reporting\n");
1816 return -1;
1817 }
1818
1819 return 0;
1820
Seth Forshee25bded72011-11-07 19:53:36 -08001821error:
1822 /*
1823 * Leaving the touchpad in command mode will essentially render
1824 * it unusable until the machine reboots, so exit it here just
1825 * to be safe
1826 */
1827 alps_exit_command_mode(psmouse);
1828 return -1;
1829}
1830
Hans de Goedef3f33c62014-07-29 11:22:07 -07001831static int alps_get_v3_v7_resolution(struct psmouse *psmouse, int reg_pitch)
1832{
1833 int reg, x_pitch, y_pitch, x_electrode, y_electrode, x_phys, y_phys;
1834 struct alps_data *priv = psmouse->private;
1835
1836 reg = alps_command_mode_read_reg(psmouse, reg_pitch);
1837 if (reg < 0)
1838 return reg;
1839
1840 x_pitch = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
1841 x_pitch = 50 + 2 * x_pitch; /* In 0.1 mm units */
1842
1843 y_pitch = (char)reg >> 4; /* sign extend upper 4 bits */
1844 y_pitch = 36 + 2 * y_pitch; /* In 0.1 mm units */
1845
1846 reg = alps_command_mode_read_reg(psmouse, reg_pitch + 1);
1847 if (reg < 0)
1848 return reg;
1849
1850 x_electrode = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
1851 x_electrode = 17 + x_electrode;
1852
1853 y_electrode = (char)reg >> 4; /* sign extend upper 4 bits */
1854 y_electrode = 13 + y_electrode;
1855
1856 x_phys = x_pitch * (x_electrode - 1); /* In 0.1 mm units */
1857 y_phys = y_pitch * (y_electrode - 1); /* In 0.1 mm units */
1858
1859 priv->x_res = priv->x_max * 10 / x_phys; /* units / mm */
1860 priv->y_res = priv->y_max * 10 / y_phys; /* units / mm */
1861
1862 psmouse_dbg(psmouse,
1863 "pitch %dx%d num-electrodes %dx%d physical size %dx%d mm res %dx%d\n",
1864 x_pitch, y_pitch, x_electrode, y_electrode,
1865 x_phys / 10, y_phys / 10, priv->x_res, priv->y_res);
1866
1867 return 0;
1868}
1869
Kevin Cernekee1302bac2013-02-13 22:27:08 -08001870static int alps_hw_init_rushmore_v3(struct psmouse *psmouse)
1871{
Kevin Cernekeecd401202013-02-13 22:28:07 -08001872 struct alps_data *priv = psmouse->private;
Kevin Cernekee1302bac2013-02-13 22:27:08 -08001873 struct ps2dev *ps2dev = &psmouse->ps2dev;
1874 int reg_val, ret = -1;
1875
Kevin Cernekeecd401202013-02-13 22:28:07 -08001876 if (priv->flags & ALPS_DUALPOINT) {
1877 reg_val = alps_setup_trackstick_v3(psmouse,
1878 ALPS_REG_BASE_RUSHMORE);
1879 if (reg_val == -EIO)
1880 goto error;
1881 if (reg_val == -ENODEV)
1882 priv->flags &= ~ALPS_DUALPOINT;
1883 }
1884
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08001885 if (alps_enter_command_mode(psmouse) ||
Kevin Cernekee1302bac2013-02-13 22:27:08 -08001886 alps_command_mode_read_reg(psmouse, 0xc2d9) == -1 ||
1887 alps_command_mode_write_reg(psmouse, 0xc2cb, 0x00))
1888 goto error;
1889
Hans de Goedef3f33c62014-07-29 11:22:07 -07001890 if (alps_get_v3_v7_resolution(psmouse, 0xc2da))
1891 goto error;
1892
Kevin Cernekee1302bac2013-02-13 22:27:08 -08001893 reg_val = alps_command_mode_read_reg(psmouse, 0xc2c6);
1894 if (reg_val == -1)
1895 goto error;
1896 if (__alps_command_mode_write_reg(psmouse, reg_val & 0xfd))
1897 goto error;
1898
1899 if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
1900 goto error;
1901
1902 /* enter absolute mode */
1903 reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
1904 if (reg_val == -1)
1905 goto error;
1906 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
1907 goto error;
1908
1909 alps_exit_command_mode(psmouse);
1910 return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
1911
1912error:
1913 alps_exit_command_mode(psmouse);
1914 return ret;
1915}
1916
Seth Forshee25bded72011-11-07 19:53:36 -08001917/* Must be in command mode when calling this function */
1918static int alps_absolute_mode_v4(struct psmouse *psmouse)
1919{
1920 int reg_val;
1921
1922 reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
1923 if (reg_val == -1)
1924 return -1;
1925
1926 reg_val |= 0x02;
1927 if (__alps_command_mode_write_reg(psmouse, reg_val))
1928 return -1;
1929
1930 return 0;
1931}
1932
1933static int alps_hw_init_v4(struct psmouse *psmouse)
1934{
Seth Forshee25bded72011-11-07 19:53:36 -08001935 struct ps2dev *ps2dev = &psmouse->ps2dev;
1936 unsigned char param[4];
1937
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08001938 if (alps_enter_command_mode(psmouse))
Seth Forshee25bded72011-11-07 19:53:36 -08001939 goto error;
1940
1941 if (alps_absolute_mode_v4(psmouse)) {
1942 psmouse_err(psmouse, "Failed to enter absolute mode\n");
1943 goto error;
1944 }
1945
1946 if (alps_command_mode_write_reg(psmouse, 0x0007, 0x8c))
1947 goto error;
1948
1949 if (alps_command_mode_write_reg(psmouse, 0x0149, 0x03))
1950 goto error;
1951
1952 if (alps_command_mode_write_reg(psmouse, 0x0160, 0x03))
1953 goto error;
1954
1955 if (alps_command_mode_write_reg(psmouse, 0x017f, 0x15))
1956 goto error;
1957
1958 if (alps_command_mode_write_reg(psmouse, 0x0151, 0x01))
1959 goto error;
1960
1961 if (alps_command_mode_write_reg(psmouse, 0x0168, 0x03))
1962 goto error;
1963
1964 if (alps_command_mode_write_reg(psmouse, 0x014a, 0x03))
1965 goto error;
1966
1967 if (alps_command_mode_write_reg(psmouse, 0x0161, 0x03))
1968 goto error;
1969
1970 alps_exit_command_mode(psmouse);
1971
1972 /*
1973 * This sequence changes the output from a 9-byte to an
1974 * 8-byte format. All the same data seems to be present,
1975 * just in a more compact format.
1976 */
1977 param[0] = 0xc8;
1978 param[1] = 0x64;
1979 param[2] = 0x50;
1980 if (ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
1981 ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE) ||
1982 ps2_command(ps2dev, &param[2], PSMOUSE_CMD_SETRATE) ||
1983 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
1984 return -1;
1985
1986 /* Set rate and enable data reporting */
1987 param[0] = 0x64;
1988 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
1989 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
1990 psmouse_err(psmouse, "Failed to enable data reporting\n");
1991 return -1;
1992 }
1993
1994 return 0;
1995
1996error:
1997 /*
1998 * Leaving the touchpad in command mode will essentially render
1999 * it unusable until the machine reboots, so exit it here just
2000 * to be safe
2001 */
2002 alps_exit_command_mode(psmouse);
2003 return -1;
2004}
2005
Yunkang Tangee65d4b2013-12-26 14:54:19 -08002006static int alps_dolphin_get_device_area(struct psmouse *psmouse,
2007 struct alps_data *priv)
2008{
2009 struct ps2dev *ps2dev = &psmouse->ps2dev;
2010 unsigned char param[4] = {0};
2011 int num_x_electrode, num_y_electrode;
2012
2013 if (alps_enter_command_mode(psmouse))
2014 return -1;
2015
2016 param[0] = 0x0a;
2017 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
2018 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2019 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2020 ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2021 ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE))
2022 return -1;
2023
2024 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
2025 return -1;
2026
2027 /*
2028 * Dolphin's sensor line number is not fixed. It can be calculated
2029 * by adding the device's register value with DOLPHIN_PROFILE_X/YOFFSET.
2030 * Further more, we can get device's x_max and y_max by multiplying
2031 * sensor line number with DOLPHIN_COUNT_PER_ELECTRODE.
2032 *
2033 * e.g. When we get register's sensor_x = 11 & sensor_y = 8,
2034 * real sensor line number X = 11 + 8 = 19, and
2035 * real sensor line number Y = 8 + 1 = 9.
2036 * So, x_max = (19 - 1) * 64 = 1152, and
2037 * y_max = (9 - 1) * 64 = 512.
2038 */
2039 num_x_electrode = DOLPHIN_PROFILE_XOFFSET + (param[2] & 0x0F);
2040 num_y_electrode = DOLPHIN_PROFILE_YOFFSET + ((param[2] >> 4) & 0x0F);
2041 priv->x_bits = num_x_electrode;
2042 priv->y_bits = num_y_electrode;
2043 priv->x_max = (num_x_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
2044 priv->y_max = (num_y_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
2045
2046 if (alps_exit_command_mode(psmouse))
2047 return -1;
2048
2049 return 0;
2050}
2051
Dave Turvene75af9e52013-02-21 22:58:28 -08002052static int alps_hw_init_dolphin_v1(struct psmouse *psmouse)
2053{
2054 struct ps2dev *ps2dev = &psmouse->ps2dev;
2055 unsigned char param[2];
2056
2057 /* This is dolphin "v1" as empirically defined by florin9doi */
2058 param[0] = 0x64;
2059 param[1] = 0x28;
2060
2061 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2062 ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2063 ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
2064 return -1;
2065
2066 return 0;
2067}
2068
Yunkang Tang38088432014-07-26 13:51:41 -07002069static int alps_hw_init_v7(struct psmouse *psmouse)
2070{
2071 struct ps2dev *ps2dev = &psmouse->ps2dev;
2072 int reg_val, ret = -1;
2073
2074 if (alps_enter_command_mode(psmouse) ||
2075 alps_command_mode_read_reg(psmouse, 0xc2d9) == -1)
2076 goto error;
2077
Hans de Goedef3f33c62014-07-29 11:22:07 -07002078 if (alps_get_v3_v7_resolution(psmouse, 0xc397))
2079 goto error;
2080
Yunkang Tang38088432014-07-26 13:51:41 -07002081 if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
2082 goto error;
2083
2084 reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
2085 if (reg_val == -1)
2086 goto error;
2087 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
2088 goto error;
2089
2090 alps_exit_command_mode(psmouse);
2091 return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
2092
2093error:
2094 alps_exit_command_mode(psmouse);
2095 return ret;
2096}
2097
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002098static void alps_set_defaults(struct alps_data *priv)
Seth Forshee25bded72011-11-07 19:53:36 -08002099{
Kevin Cernekeef673ceb2013-02-13 22:23:34 -08002100 priv->byte0 = 0x8f;
2101 priv->mask0 = 0x8f;
2102 priv->flags = ALPS_DUALPOINT;
2103
Kevin Cernekee7a9f73e2013-02-13 22:24:55 -08002104 priv->x_max = 2000;
2105 priv->y_max = 1400;
2106 priv->x_bits = 15;
2107 priv->y_bits = 11;
2108
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002109 switch (priv->proto_version) {
Seth Forshee25bded72011-11-07 19:53:36 -08002110 case ALPS_PROTO_V1:
2111 case ALPS_PROTO_V2:
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002112 priv->hw_init = alps_hw_init_v1_v2;
2113 priv->process_packet = alps_process_packet_v1_v2;
2114 priv->set_abs_params = alps_set_abs_params_st;
Yunkang Tang95f75e92013-12-01 22:33:52 -08002115 priv->x_max = 1023;
2116 priv->y_max = 767;
Seth Forshee25bded72011-11-07 19:53:36 -08002117 break;
2118 case ALPS_PROTO_V3:
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002119 priv->hw_init = alps_hw_init_v3;
2120 priv->process_packet = alps_process_packet_v3;
2121 priv->set_abs_params = alps_set_abs_params_mt;
Kevin Cernekeef85e5002013-02-13 22:26:11 -08002122 priv->decode_fields = alps_decode_pinnacle;
Kevin Cernekee50e8b212013-02-13 22:23:04 -08002123 priv->nibble_commands = alps_v3_nibble_commands;
2124 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
Seth Forshee25bded72011-11-07 19:53:36 -08002125 break;
2126 case ALPS_PROTO_V4:
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002127 priv->hw_init = alps_hw_init_v4;
2128 priv->process_packet = alps_process_packet_v4;
2129 priv->set_abs_params = alps_set_abs_params_mt;
Kevin Cernekee50e8b212013-02-13 22:23:04 -08002130 priv->nibble_commands = alps_v4_nibble_commands;
2131 priv->addr_command = PSMOUSE_CMD_DISABLE;
Seth Forshee25bded72011-11-07 19:53:36 -08002132 break;
Dave Turvene75af9e52013-02-21 22:58:28 -08002133 case ALPS_PROTO_V5:
2134 priv->hw_init = alps_hw_init_dolphin_v1;
Yunkang Tangee65d4b2013-12-26 14:54:19 -08002135 priv->process_packet = alps_process_touchpad_packet_v3_v5;
Dave Turvene75af9e52013-02-21 22:58:28 -08002136 priv->decode_fields = alps_decode_dolphin;
2137 priv->set_abs_params = alps_set_abs_params_mt;
2138 priv->nibble_commands = alps_v3_nibble_commands;
2139 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2140 priv->byte0 = 0xc8;
Yunkang Tangee65d4b2013-12-26 14:54:19 -08002141 priv->mask0 = 0xd8;
Dave Turvene75af9e52013-02-21 22:58:28 -08002142 priv->flags = 0;
2143 priv->x_max = 1360;
2144 priv->y_max = 660;
2145 priv->x_bits = 23;
2146 priv->y_bits = 12;
2147 break;
Yunkang Tang95f75e92013-12-01 22:33:52 -08002148 case ALPS_PROTO_V6:
2149 priv->hw_init = alps_hw_init_v6;
2150 priv->process_packet = alps_process_packet_v6;
2151 priv->set_abs_params = alps_set_abs_params_st;
2152 priv->nibble_commands = alps_v6_nibble_commands;
2153 priv->x_max = 2047;
2154 priv->y_max = 1535;
2155 break;
Yunkang Tang38088432014-07-26 13:51:41 -07002156 case ALPS_PROTO_V7:
2157 priv->hw_init = alps_hw_init_v7;
2158 priv->process_packet = alps_process_packet_v7;
2159 priv->decode_fields = alps_decode_packet_v7;
2160 priv->set_abs_params = alps_set_abs_params_mt;
2161 priv->nibble_commands = alps_v3_nibble_commands;
2162 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2163 priv->x_max = 0xfff;
2164 priv->y_max = 0x7ff;
2165 priv->byte0 = 0x48;
2166 priv->mask0 = 0x48;
2167
2168 if (priv->fw_ver[1] != 0xba)
2169 priv->flags |= ALPS_BUTTONPAD;
2170 break;
Seth Forshee25bded72011-11-07 19:53:36 -08002171 }
Seth Forshee25bded72011-11-07 19:53:36 -08002172}
2173
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002174static int alps_match_table(struct psmouse *psmouse, struct alps_data *priv,
2175 unsigned char *e7, unsigned char *ec)
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002176{
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002177 const struct alps_model_info *model;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002178 int i;
2179
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002180 for (i = 0; i < ARRAY_SIZE(alps_model_data); i++) {
2181 model = &alps_model_data[i];
2182
2183 if (!memcmp(e7, model->signature, sizeof(model->signature)) &&
2184 (!model->command_mode_resp ||
2185 model->command_mode_resp == ec[2])) {
2186
2187 priv->proto_version = model->proto_version;
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002188 alps_set_defaults(priv);
2189
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002190 priv->flags = model->flags;
2191 priv->byte0 = model->byte0;
2192 priv->mask0 = model->mask0;
2193
2194 return 0;
2195 }
2196 }
2197
2198 return -EINVAL;
2199}
2200
2201static int alps_identify(struct psmouse *psmouse, struct alps_data *priv)
2202{
2203 unsigned char e6[4], e7[4], ec[4];
2204
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002205 /*
2206 * First try "E6 report".
2207 * ALPS should return 0,0,10 or 0,0,100 if no buttons are pressed.
2208 * The bits 0-2 of the first byte will be 1s if some buttons are
2209 * pressed.
2210 */
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002211 if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2212 PSMOUSE_CMD_SETSCALE11, e6))
2213 return -EIO;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002214
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002215 if ((e6[0] & 0xf8) != 0 || e6[1] != 0 || (e6[2] != 10 && e6[2] != 100))
2216 return -EINVAL;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002217
2218 /*
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002219 * Now get the "E7" and "EC" reports. These will uniquely identify
2220 * most ALPS touchpads.
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002221 */
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002222 if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2223 PSMOUSE_CMD_SETSCALE21, e7) ||
2224 alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2225 PSMOUSE_CMD_RESET_WRAP, ec) ||
2226 alps_exit_command_mode(psmouse))
2227 return -EIO;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002228
Hans de Goedec0cd17f2014-07-25 22:49:14 -07002229 /* Save the Firmware version */
2230 memcpy(priv->fw_ver, ec, 3);
2231
Kevin Cernekeef673ceb2013-02-13 22:23:34 -08002232 if (alps_match_table(psmouse, priv, e7, ec) == 0) {
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002233 return 0;
Dave Turvene75af9e52013-02-21 22:58:28 -08002234 } else if (e7[0] == 0x73 && e7[1] == 0x03 && e7[2] == 0x50 &&
Yunkang Tangee65d4b2013-12-26 14:54:19 -08002235 ec[0] == 0x73 && (ec[1] == 0x01 || ec[1] == 0x02)) {
Dave Turvene75af9e52013-02-21 22:58:28 -08002236 priv->proto_version = ALPS_PROTO_V5;
2237 alps_set_defaults(priv);
Yunkang Tangee65d4b2013-12-26 14:54:19 -08002238 if (alps_dolphin_get_device_area(psmouse, priv))
2239 return -EIO;
2240 else
2241 return 0;
Yunkang Tang38088432014-07-26 13:51:41 -07002242 } else if (ec[0] == 0x88 &&
2243 ((ec[1] & 0xf0) == 0xb0 || (ec[1] & 0xf0) == 0xc0)) {
2244 priv->proto_version = ALPS_PROTO_V7;
2245 alps_set_defaults(priv);
2246
2247 return 0;
Kevin Cernekee1302bac2013-02-13 22:27:08 -08002248 } else if (ec[0] == 0x88 && ec[1] == 0x08) {
2249 priv->proto_version = ALPS_PROTO_V3;
2250 alps_set_defaults(priv);
2251
2252 priv->hw_init = alps_hw_init_rushmore_v3;
2253 priv->decode_fields = alps_decode_rushmore;
2254 priv->x_bits = 16;
2255 priv->y_bits = 12;
Hans de Goede40e8f532014-07-25 22:37:15 -07002256 priv->flags |= ALPS_IS_RUSHMORE;
Kevin Cernekee1302bac2013-02-13 22:27:08 -08002257
Kevin Cernekeecd401202013-02-13 22:28:07 -08002258 /* hack to make addr_command, nibble_command available */
2259 psmouse->private = priv;
2260
2261 if (alps_probe_trackstick_v3(psmouse, ALPS_REG_BASE_RUSHMORE))
2262 priv->flags &= ~ALPS_DUALPOINT;
2263
Kevin Cernekee1302bac2013-02-13 22:27:08 -08002264 return 0;
Kevin Cernekeef673ceb2013-02-13 22:23:34 -08002265 } else if (ec[0] == 0x88 && ec[1] == 0x07 &&
2266 ec[2] >= 0x90 && ec[2] <= 0x9d) {
2267 priv->proto_version = ALPS_PROTO_V3;
2268 alps_set_defaults(priv);
2269
2270 return 0;
2271 }
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002272
Dmitry Torokhova9e06212014-08-25 11:24:47 -07002273 psmouse_dbg(psmouse,
2274 "Likely not an ALPS touchpad: E7=%3ph, EC=%3ph\n", e7, ec);
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002275
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002276 return -EINVAL;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002277}
2278
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04002279static int alps_reconnect(struct psmouse *psmouse)
2280{
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002281 struct alps_data *priv = psmouse->private;
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08002282
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04002283 psmouse_reset(psmouse);
2284
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002285 if (alps_identify(psmouse, priv) < 0)
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04002286 return -1;
2287
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002288 return priv->hw_init(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289}
2290
2291static void alps_disconnect(struct psmouse *psmouse)
2292{
2293 struct alps_data *priv = psmouse->private;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05002294
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 psmouse_reset(psmouse);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08002296 del_timer_sync(&priv->timer);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05002297 input_unregister_device(priv->dev2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 kfree(priv);
2299}
2300
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002301static void alps_set_abs_params_st(struct alps_data *priv,
2302 struct input_dev *dev1)
2303{
Yunkang Tang95f75e92013-12-01 22:33:52 -08002304 input_set_abs_params(dev1, ABS_X, 0, priv->x_max, 0, 0);
2305 input_set_abs_params(dev1, ABS_Y, 0, priv->y_max, 0, 0);
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002306}
2307
2308static void alps_set_abs_params_mt(struct alps_data *priv,
2309 struct input_dev *dev1)
2310{
Kevin Cernekee7a9f73e2013-02-13 22:24:55 -08002311 input_set_abs_params(dev1, ABS_MT_POSITION_X, 0, priv->x_max, 0, 0);
2312 input_set_abs_params(dev1, ABS_MT_POSITION_Y, 0, priv->y_max, 0, 0);
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002313
Hans de Goedef3f33c62014-07-29 11:22:07 -07002314 input_abs_set_res(dev1, ABS_MT_POSITION_X, priv->x_res);
2315 input_abs_set_res(dev1, ABS_MT_POSITION_Y, priv->y_res);
2316
Hans de Goedecdf333e2014-07-25 22:44:42 -07002317 input_mt_init_slots(dev1, MAX_TOUCHES, INPUT_MT_POINTER |
2318 INPUT_MT_DROP_UNUSED | INPUT_MT_TRACK | INPUT_MT_SEMI_MT);
2319
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002320 set_bit(BTN_TOOL_TRIPLETAP, dev1->keybit);
2321 set_bit(BTN_TOOL_QUADTAP, dev1->keybit);
Yunkang Tang38088432014-07-26 13:51:41 -07002322
2323 /* V7 is real multi-touch */
2324 if (priv->proto_version == ALPS_PROTO_V7)
2325 clear_bit(INPUT_PROP_SEMI_MT, dev1->propbit);
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002326}
2327
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328int alps_init(struct psmouse *psmouse)
2329{
2330 struct alps_data *priv;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05002331 struct input_dev *dev1 = psmouse->dev, *dev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332
Dmitry Torokhovf42649e2007-04-12 01:31:13 -04002333 priv = kzalloc(sizeof(struct alps_data), GFP_KERNEL);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05002334 dev2 = input_allocate_device();
2335 if (!priv || !dev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 goto init_fail;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05002337
2338 priv->dev2 = dev2;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08002339 setup_timer(&priv->timer, alps_flush_packet, (unsigned long)psmouse);
2340
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04002341 psmouse->private = priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342
Seth Forshee25bded72011-11-07 19:53:36 -08002343 psmouse_reset(psmouse);
2344
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002345 if (alps_identify(psmouse, priv) < 0)
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08002346 goto init_fail;
2347
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002348 if (priv->hw_init(psmouse))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 goto init_fail;
2350
Dmitry Torokhov7105d2e2009-12-11 23:54:54 -08002351 /*
2352 * Undo part of setup done for us by psmouse core since touchpad
2353 * is not a relative device.
2354 */
2355 __clear_bit(EV_REL, dev1->evbit);
2356 __clear_bit(REL_X, dev1->relbit);
2357 __clear_bit(REL_Y, dev1->relbit);
2358
2359 /*
2360 * Now set up our capabilities.
2361 */
Jiri Slaby7b19ada2007-10-18 23:40:32 -07002362 dev1->evbit[BIT_WORD(EV_KEY)] |= BIT_MASK(EV_KEY);
2363 dev1->keybit[BIT_WORD(BTN_TOUCH)] |= BIT_MASK(BTN_TOUCH);
2364 dev1->keybit[BIT_WORD(BTN_TOOL_FINGER)] |= BIT_MASK(BTN_TOOL_FINGER);
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08002365 dev1->keybit[BIT_WORD(BTN_LEFT)] |=
2366 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367
Jiri Slaby7b19ada2007-10-18 23:40:32 -07002368 dev1->evbit[BIT_WORD(EV_ABS)] |= BIT_MASK(EV_ABS);
Seth Forshee25bded72011-11-07 19:53:36 -08002369
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002370 priv->set_abs_params(priv, dev1);
Yunkang Tang38088432014-07-26 13:51:41 -07002371 /* No pressure on V7 */
2372 if (priv->proto_version != ALPS_PROTO_V7)
2373 input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002375 if (priv->flags & ALPS_WHEEL) {
Jiri Slaby7b19ada2007-10-18 23:40:32 -07002376 dev1->evbit[BIT_WORD(EV_REL)] |= BIT_MASK(EV_REL);
2377 dev1->relbit[BIT_WORD(REL_WHEEL)] |= BIT_MASK(REL_WHEEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 }
2379
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002380 if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
Jiri Slaby7b19ada2007-10-18 23:40:32 -07002381 dev1->keybit[BIT_WORD(BTN_FORWARD)] |= BIT_MASK(BTN_FORWARD);
2382 dev1->keybit[BIT_WORD(BTN_BACK)] |= BIT_MASK(BTN_BACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 }
2384
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002385 if (priv->flags & ALPS_FOUR_BUTTONS) {
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08002386 dev1->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_0);
2387 dev1->keybit[BIT_WORD(BTN_1)] |= BIT_MASK(BTN_1);
2388 dev1->keybit[BIT_WORD(BTN_2)] |= BIT_MASK(BTN_2);
2389 dev1->keybit[BIT_WORD(BTN_3)] |= BIT_MASK(BTN_3);
Yunkang Tang38088432014-07-26 13:51:41 -07002390 } else if (priv->flags & ALPS_BUTTONPAD) {
2391 set_bit(INPUT_PROP_BUTTONPAD, dev1->propbit);
2392 clear_bit(BTN_RIGHT, dev1->keybit);
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08002393 } else {
2394 dev1->keybit[BIT_WORD(BTN_MIDDLE)] |= BIT_MASK(BTN_MIDDLE);
2395 }
2396
Dmitry Torokhov08ffce42006-06-26 01:45:10 -04002397 snprintf(priv->phys, sizeof(priv->phys), "%s/input1", psmouse->ps2dev.serio->phys);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05002398 dev2->phys = priv->phys;
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002399 dev2->name = (priv->flags & ALPS_DUALPOINT) ?
Yunkang Tang9354f262013-10-15 23:50:38 -07002400 "DualPoint Stick" : "ALPS PS/2 Device";
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05002401 dev2->id.bustype = BUS_I8042;
2402 dev2->id.vendor = 0x0002;
2403 dev2->id.product = PSMOUSE_ALPS;
2404 dev2->id.version = 0x0000;
Dmitry Torokhov1db3a342008-03-18 00:29:18 -04002405 dev2->dev.parent = &psmouse->ps2dev.serio->dev;
Dmitry Torokhov968ac842005-05-29 02:28:29 -05002406
Jiri Slaby7b19ada2007-10-18 23:40:32 -07002407 dev2->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08002408 dev2->relbit[BIT_WORD(REL_X)] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
2409 dev2->keybit[BIT_WORD(BTN_LEFT)] =
2410 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_MIDDLE) | BIT_MASK(BTN_RIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411
Hans de Goede01d4cd52014-09-08 14:44:05 -07002412 __set_bit(INPUT_PROP_POINTER, dev2->propbit);
Hans de Goede76113922014-09-08 14:42:12 -07002413 if (priv->flags & ALPS_DUALPOINT)
2414 __set_bit(INPUT_PROP_POINTING_STICK, dev2->propbit);
2415
Dmitry Torokhovf42649e2007-04-12 01:31:13 -04002416 if (input_register_device(priv->dev2))
2417 goto init_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418
2419 psmouse->protocol_handler = alps_process_byte;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05002420 psmouse->poll = alps_poll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 psmouse->disconnect = alps_disconnect;
2422 psmouse->reconnect = alps_reconnect;
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002423 psmouse->pktsize = priv->proto_version == ALPS_PROTO_V4 ? 8 : 6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05002425 /* We are having trouble resyncing ALPS touchpads so disable it for now */
2426 psmouse->resync_time = 0;
2427
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 return 0;
2429
2430init_fail:
Dmitry Torokhovf42649e2007-04-12 01:31:13 -04002431 psmouse_reset(psmouse);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05002432 input_free_device(dev2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 kfree(priv);
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04002434 psmouse->private = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 return -1;
2436}
2437
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07002438int alps_detect(struct psmouse *psmouse, bool set_properties)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439{
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002440 struct alps_data dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002442 if (alps_identify(psmouse, &dummy) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 return -1;
2444
2445 if (set_properties) {
2446 psmouse->vendor = "ALPS";
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002447 psmouse->name = dummy.flags & ALPS_DUALPOINT ?
Dmitry Torokhov968ac842005-05-29 02:28:29 -05002448 "DualPoint TouchPad" : "GlidePoint";
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002449 psmouse->model = dummy.proto_version << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 }
2451 return 0;
2452}
2453