blob: dd2fd397466ad62e12d1394083c58a34eddb6e7a [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
Pali Rohár34412ba2015-01-09 12:48:58 -0800478 /* It should be a DualPoint when received trackstick packet */
479 if (!(priv->flags & ALPS_DUALPOINT)) {
480 psmouse_warn(psmouse,
481 "Rejected trackstick packet from non DualPoint device");
482 return;
483 }
484
Seth Forshee25bded72011-11-07 19:53:36 -0800485 /* Sanity check packet */
486 if (!(packet[0] & 0x40)) {
487 psmouse_dbg(psmouse, "Bad trackstick packet, discarding\n");
488 return;
489 }
490
491 /*
492 * There's a special packet that seems to indicate the end
493 * of a stream of trackstick data. Filter these out.
494 */
495 if (packet[1] == 0x7f && packet[2] == 0x7f && packet[4] == 0x7f)
496 return;
497
498 x = (s8)(((packet[0] & 0x20) << 2) | (packet[1] & 0x7f));
499 y = (s8)(((packet[0] & 0x10) << 3) | (packet[2] & 0x7f));
500 z = (packet[4] & 0x7c) >> 2;
501
502 /*
503 * The x and y values tend to be quite large, and when used
504 * alone the trackstick is difficult to use. Scale them down
505 * to compensate.
506 */
507 x /= 8;
508 y /= 8;
509
510 input_report_rel(dev, REL_X, x);
511 input_report_rel(dev, REL_Y, -y);
512
513 /*
514 * Most ALPS models report the trackstick buttons in the touchpad
515 * packets, but a few report them here. No reliable way has been
516 * found to differentiate between the models upfront, so we enable
517 * the quirk in response to seeing a button press in the trackstick
518 * packet.
519 */
520 left = packet[3] & 0x01;
521 right = packet[3] & 0x02;
522 middle = packet[3] & 0x04;
523
524 if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) &&
525 (left || right || middle))
526 priv->quirks |= ALPS_QUIRK_TRACKSTICK_BUTTONS;
527
528 if (priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) {
529 input_report_key(dev, BTN_LEFT, left);
530 input_report_key(dev, BTN_RIGHT, right);
531 input_report_key(dev, BTN_MIDDLE, middle);
532 }
533
534 input_sync(dev);
535 return;
536}
537
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800538static void alps_decode_buttons_v3(struct alps_fields *f, unsigned char *p)
539{
540 f->left = !!(p[3] & 0x01);
541 f->right = !!(p[3] & 0x02);
542 f->middle = !!(p[3] & 0x04);
543
544 f->ts_left = !!(p[3] & 0x10);
545 f->ts_right = !!(p[3] & 0x20);
546 f->ts_middle = !!(p[3] & 0x40);
547}
548
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700549static int alps_decode_pinnacle(struct alps_fields *f, unsigned char *p,
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800550 struct psmouse *psmouse)
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800551{
552 f->first_mp = !!(p[4] & 0x40);
553 f->is_mp = !!(p[0] & 0x40);
554
555 f->fingers = (p[5] & 0x3) + 1;
556 f->x_map = ((p[4] & 0x7e) << 8) |
557 ((p[1] & 0x7f) << 2) |
558 ((p[0] & 0x30) >> 4);
559 f->y_map = ((p[3] & 0x70) << 4) |
560 ((p[2] & 0x7f) << 1) |
561 (p[4] & 0x01);
562
Hans de Goede02d04252014-07-25 22:43:35 -0700563 f->st.x = ((p[1] & 0x7f) << 4) | ((p[4] & 0x30) >> 2) |
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800564 ((p[0] & 0x30) >> 4);
Hans de Goede02d04252014-07-25 22:43:35 -0700565 f->st.y = ((p[2] & 0x7f) << 4) | (p[4] & 0x0f);
566 f->pressure = p[5] & 0x7f;
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800567
568 alps_decode_buttons_v3(f, p);
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700569
570 return 0;
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800571}
572
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700573static int alps_decode_rushmore(struct alps_fields *f, unsigned char *p,
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800574 struct psmouse *psmouse)
Kevin Cernekee1302bac2013-02-13 22:27:08 -0800575{
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800576 alps_decode_pinnacle(f, p, psmouse);
Kevin Cernekee1302bac2013-02-13 22:27:08 -0800577
Yunkang Tangf105e342014-07-25 22:29:24 -0700578 /* Rushmore's packet decode has a bit difference with Pinnacle's */
579 f->is_mp = !!(p[5] & 0x40);
580 f->fingers = max((p[5] & 0x3), ((p[5] >> 2) & 0x3)) + 1;
Kevin Cernekee1302bac2013-02-13 22:27:08 -0800581 f->x_map |= (p[5] & 0x10) << 11;
582 f->y_map |= (p[5] & 0x20) << 6;
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700583
584 return 0;
Kevin Cernekee1302bac2013-02-13 22:27:08 -0800585}
586
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700587static int alps_decode_dolphin(struct alps_fields *f, unsigned char *p,
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800588 struct psmouse *psmouse)
Dave Turvene75af9e52013-02-21 22:58:28 -0800589{
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800590 u64 palm_data = 0;
591 struct alps_data *priv = psmouse->private;
592
Dave Turvene75af9e52013-02-21 22:58:28 -0800593 f->first_mp = !!(p[0] & 0x02);
594 f->is_mp = !!(p[0] & 0x20);
595
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800596 if (!f->is_mp) {
Hans de Goede02d04252014-07-25 22:43:35 -0700597 f->st.x = ((p[1] & 0x7f) | ((p[4] & 0x0f) << 7));
598 f->st.y = ((p[2] & 0x7f) | ((p[4] & 0xf0) << 3));
599 f->pressure = (p[0] & 4) ? 0 : p[5] & 0x7f;
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800600 alps_decode_buttons_v3(f, p);
601 } else {
602 f->fingers = ((p[0] & 0x6) >> 1 |
Dave Turvene75af9e52013-02-21 22:58:28 -0800603 (p[0] & 0x10) >> 2);
Dave Turvene75af9e52013-02-21 22:58:28 -0800604
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800605 palm_data = (p[1] & 0x7f) |
606 ((p[2] & 0x7f) << 7) |
607 ((p[4] & 0x7f) << 14) |
608 ((p[5] & 0x7f) << 21) |
609 ((p[3] & 0x07) << 28) |
610 (((u64)p[3] & 0x70) << 27) |
611 (((u64)p[0] & 0x01) << 34);
Dave Turvene75af9e52013-02-21 22:58:28 -0800612
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800613 /* Y-profile is stored in P(0) to p(n-1), n = y_bits; */
614 f->y_map = palm_data & (BIT(priv->y_bits) - 1);
615
616 /* X-profile is stored in p(n) to p(n+m-1), m = x_bits; */
617 f->x_map = (palm_data >> priv->y_bits) &
618 (BIT(priv->x_bits) - 1);
619 }
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700620
621 return 0;
Dave Turvene75af9e52013-02-21 22:58:28 -0800622}
623
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800624static void alps_process_touchpad_packet_v3_v5(struct psmouse *psmouse)
Seth Forshee25bded72011-11-07 19:53:36 -0800625{
626 struct alps_data *priv = psmouse->private;
627 unsigned char *packet = psmouse->packet;
Seth Forshee25bded72011-11-07 19:53:36 -0800628 struct input_dev *dev2 = priv->dev2;
Hans de Goede02d04252014-07-25 22:43:35 -0700629 struct alps_fields *f = &priv->f;
630 int fingers = 0;
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800631
Hans de Goede02d04252014-07-25 22:43:35 -0700632 memset(f, 0, sizeof(*f));
633
634 priv->decode_fields(f, packet, psmouse);
Seth Forshee25bded72011-11-07 19:53:36 -0800635
636 /*
Seth Forshee01ce6612011-11-07 19:54:13 -0800637 * There's no single feature of touchpad position and bitmap packets
638 * that can be used to distinguish between them. We rely on the fact
639 * that a bitmap packet should always follow a position packet with
640 * bit 6 of packet[4] set.
Seth Forshee25bded72011-11-07 19:53:36 -0800641 */
642 if (priv->multi_packet) {
Seth Forshee25bded72011-11-07 19:53:36 -0800643 /*
644 * Sometimes a position packet will indicate a multi-packet
645 * sequence, but then what follows is another position
646 * packet. Check for this, and when it happens process the
647 * position packet as usual.
648 */
Hans de Goede02d04252014-07-25 22:43:35 -0700649 if (f->is_mp) {
650 fingers = f->fingers;
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800651 if (priv->proto_version == ALPS_PROTO_V3) {
Hans de Goede02d04252014-07-25 22:43:35 -0700652 if (alps_process_bitmap(priv, f) == 0)
Hans de Goede20bea682014-07-25 22:33:33 -0700653 fingers = 0; /* Use st data */
Seth Forshee01ce6612011-11-07 19:54:13 -0800654
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800655 /* Now process position packet */
Hans de Goede02d04252014-07-25 22:43:35 -0700656 priv->decode_fields(f, priv->multi_data,
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800657 psmouse);
658 } else {
659 /*
660 * Because Dolphin uses position packet's
661 * coordinate data as Pt1 and uses it to
662 * calculate Pt2, so we need to do position
663 * packet decode first.
664 */
Hans de Goede02d04252014-07-25 22:43:35 -0700665 priv->decode_fields(f, priv->multi_data,
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800666 psmouse);
667
668 /*
669 * Since Dolphin's finger number is reliable,
670 * there is no need to compare with bmap_fn.
671 */
Hans de Goede02d04252014-07-25 22:43:35 -0700672 alps_process_bitmap_dolphin(priv, f);
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800673 }
Seth Forshee01ce6612011-11-07 19:54:13 -0800674 } else {
675 priv->multi_packet = 0;
Seth Forshee25bded72011-11-07 19:53:36 -0800676 }
677 }
678
Seth Forshee01ce6612011-11-07 19:54:13 -0800679 /*
680 * Bit 6 of byte 0 is not usually set in position packets. The only
681 * times it seems to be set is in situations where the data is
682 * suspect anyway, e.g. a palm resting flat on the touchpad. Given
683 * this combined with the fact that this bit is useful for filtering
684 * out misidentified bitmap packets, we reject anything with this
685 * bit set.
686 */
Hans de Goede02d04252014-07-25 22:43:35 -0700687 if (f->is_mp)
Seth Forshee01ce6612011-11-07 19:54:13 -0800688 return;
689
Hans de Goede02d04252014-07-25 22:43:35 -0700690 if (!priv->multi_packet && f->first_mp) {
Seth Forshee25bded72011-11-07 19:53:36 -0800691 priv->multi_packet = 1;
Seth Forshee01ce6612011-11-07 19:54:13 -0800692 memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
693 return;
694 }
695
696 priv->multi_packet = 0;
Seth Forshee25bded72011-11-07 19:53:36 -0800697
Seth Forshee25bded72011-11-07 19:53:36 -0800698 /*
699 * Sometimes the hardware sends a single packet with z = 0
700 * in the middle of a stream. Real releases generate packets
701 * with x, y, and z all zero, so these seem to be flukes.
702 * Ignore them.
703 */
Hans de Goede02d04252014-07-25 22:43:35 -0700704 if (f->st.x && f->st.y && !f->pressure)
Seth Forshee25bded72011-11-07 19:53:36 -0800705 return;
706
Hans de Goede68c21872014-07-25 22:47:25 -0700707 alps_report_semi_mt_data(psmouse, fingers);
Seth Forshee25bded72011-11-07 19:53:36 -0800708
Pali Rohár34412ba2015-01-09 12:48:58 -0800709 if ((priv->flags & ALPS_DUALPOINT) &&
710 !(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS)) {
Hans de Goede02d04252014-07-25 22:43:35 -0700711 input_report_key(dev2, BTN_LEFT, f->ts_left);
712 input_report_key(dev2, BTN_RIGHT, f->ts_right);
713 input_report_key(dev2, BTN_MIDDLE, f->ts_middle);
Seth Forshee25bded72011-11-07 19:53:36 -0800714 input_sync(dev2);
715 }
716}
717
718static void alps_process_packet_v3(struct psmouse *psmouse)
719{
720 unsigned char *packet = psmouse->packet;
721
722 /*
723 * v3 protocol packets come in three types, two representing
724 * touchpad data and one representing trackstick data.
725 * Trackstick packets seem to be distinguished by always
726 * having 0x3f in the last byte. This value has never been
727 * observed in the last byte of either of the other types
728 * of packets.
729 */
730 if (packet[5] == 0x3f) {
731 alps_process_trackstick_packet_v3(psmouse);
732 return;
733 }
734
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800735 alps_process_touchpad_packet_v3_v5(psmouse);
Seth Forshee25bded72011-11-07 19:53:36 -0800736}
737
Yunkang Tang95f75e92013-12-01 22:33:52 -0800738static void alps_process_packet_v6(struct psmouse *psmouse)
739{
740 struct alps_data *priv = psmouse->private;
741 unsigned char *packet = psmouse->packet;
742 struct input_dev *dev = psmouse->dev;
743 struct input_dev *dev2 = priv->dev2;
744 int x, y, z, left, right, middle;
745
746 /*
747 * We can use Byte5 to distinguish if the packet is from Touchpad
748 * or Trackpoint.
749 * Touchpad: 0 - 0x7E
750 * Trackpoint: 0x7F
751 */
752 if (packet[5] == 0x7F) {
753 /* It should be a DualPoint when received Trackpoint packet */
Pali Rohár34412ba2015-01-09 12:48:58 -0800754 if (!(priv->flags & ALPS_DUALPOINT)) {
755 psmouse_warn(psmouse,
756 "Rejected trackstick packet from non DualPoint device");
Yunkang Tang95f75e92013-12-01 22:33:52 -0800757 return;
Pali Rohár34412ba2015-01-09 12:48:58 -0800758 }
Yunkang Tang95f75e92013-12-01 22:33:52 -0800759
760 /* Trackpoint packet */
761 x = packet[1] | ((packet[3] & 0x20) << 2);
762 y = packet[2] | ((packet[3] & 0x40) << 1);
763 z = packet[4];
764 left = packet[3] & 0x01;
765 right = packet[3] & 0x02;
766 middle = packet[3] & 0x04;
767
768 /* To prevent the cursor jump when finger lifted */
769 if (x == 0x7F && y == 0x7F && z == 0x7F)
770 x = y = z = 0;
771
772 /* Divide 4 since trackpoint's speed is too fast */
773 input_report_rel(dev2, REL_X, (char)x / 4);
774 input_report_rel(dev2, REL_Y, -((char)y / 4));
775
776 input_report_key(dev2, BTN_LEFT, left);
777 input_report_key(dev2, BTN_RIGHT, right);
778 input_report_key(dev2, BTN_MIDDLE, middle);
779
780 input_sync(dev2);
781 return;
782 }
783
784 /* Touchpad packet */
785 x = packet[1] | ((packet[3] & 0x78) << 4);
786 y = packet[2] | ((packet[4] & 0x78) << 4);
787 z = packet[5];
788 left = packet[3] & 0x01;
789 right = packet[3] & 0x02;
790
791 if (z > 30)
792 input_report_key(dev, BTN_TOUCH, 1);
793 if (z < 25)
794 input_report_key(dev, BTN_TOUCH, 0);
795
796 if (z > 0) {
797 input_report_abs(dev, ABS_X, x);
798 input_report_abs(dev, ABS_Y, y);
799 }
800
801 input_report_abs(dev, ABS_PRESSURE, z);
802 input_report_key(dev, BTN_TOOL_FINGER, z > 0);
803
804 /* v6 touchpad does not have middle button */
805 input_report_key(dev, BTN_LEFT, left);
806 input_report_key(dev, BTN_RIGHT, right);
807
808 input_sync(dev);
809}
810
Seth Forshee25bded72011-11-07 19:53:36 -0800811static void alps_process_packet_v4(struct psmouse *psmouse)
812{
George Pantalos3b7e09f2012-05-10 22:31:59 -0700813 struct alps_data *priv = psmouse->private;
Seth Forshee25bded72011-11-07 19:53:36 -0800814 unsigned char *packet = psmouse->packet;
Hans de Goede02d04252014-07-25 22:43:35 -0700815 struct alps_fields *f = &priv->f;
Hans de Goede68c21872014-07-25 22:47:25 -0700816 int offset;
George Pantalos3b7e09f2012-05-10 22:31:59 -0700817
818 /*
819 * v4 has a 6-byte encoding for bitmap data, but this data is
820 * broken up between 3 normal packets. Use priv->multi_packet to
821 * track our position in the bitmap packet.
822 */
823 if (packet[6] & 0x40) {
824 /* sync, reset position */
825 priv->multi_packet = 0;
826 }
827
828 if (WARN_ON_ONCE(priv->multi_packet > 2))
829 return;
830
831 offset = 2 * priv->multi_packet;
832 priv->multi_data[offset] = packet[6];
833 priv->multi_data[offset + 1] = packet[7];
834
835 if (++priv->multi_packet > 2) {
836 priv->multi_packet = 0;
837
Hans de Goede02d04252014-07-25 22:43:35 -0700838 f->x_map = ((priv->multi_data[2] & 0x1f) << 10) |
George Pantalos3b7e09f2012-05-10 22:31:59 -0700839 ((priv->multi_data[3] & 0x60) << 3) |
840 ((priv->multi_data[0] & 0x3f) << 2) |
841 ((priv->multi_data[1] & 0x60) >> 5);
Hans de Goede02d04252014-07-25 22:43:35 -0700842 f->y_map = ((priv->multi_data[5] & 0x01) << 10) |
George Pantalos3b7e09f2012-05-10 22:31:59 -0700843 ((priv->multi_data[3] & 0x1f) << 5) |
844 (priv->multi_data[1] & 0x1f);
845
Hans de Goede02d04252014-07-25 22:43:35 -0700846 f->fingers = alps_process_bitmap(priv, f);
George Pantalos3b7e09f2012-05-10 22:31:59 -0700847 }
Seth Forshee25bded72011-11-07 19:53:36 -0800848
Hans de Goede02d04252014-07-25 22:43:35 -0700849 f->left = packet[4] & 0x01;
850 f->right = packet[4] & 0x02;
Seth Forshee25bded72011-11-07 19:53:36 -0800851
Hans de Goede02d04252014-07-25 22:43:35 -0700852 f->st.x = ((packet[1] & 0x7f) << 4) | ((packet[3] & 0x30) >> 2) |
853 ((packet[0] & 0x30) >> 4);
854 f->st.y = ((packet[2] & 0x7f) << 4) | (packet[3] & 0x0f);
855 f->pressure = packet[5] & 0x7f;
Seth Forshee25bded72011-11-07 19:53:36 -0800856
Hans de Goede68c21872014-07-25 22:47:25 -0700857 alps_report_semi_mt_data(psmouse, f->fingers);
Seth Forshee25bded72011-11-07 19:53:36 -0800858}
859
Yunkang Tang38088432014-07-26 13:51:41 -0700860static bool alps_is_valid_package_v7(struct psmouse *psmouse)
861{
862 switch (psmouse->pktcnt) {
863 case 3:
864 return (psmouse->packet[2] & 0x40) == 0x40;
865 case 4:
866 return (psmouse->packet[3] & 0x48) == 0x48;
867 case 6:
868 return (psmouse->packet[5] & 0x40) == 0x00;
869 }
870 return true;
871}
872
873static unsigned char alps_get_packet_id_v7(char *byte)
874{
875 unsigned char packet_id;
876
877 if (byte[4] & 0x40)
878 packet_id = V7_PACKET_ID_TWO;
879 else if (byte[4] & 0x01)
880 packet_id = V7_PACKET_ID_MULTI;
881 else if ((byte[0] & 0x10) && !(byte[4] & 0x43))
882 packet_id = V7_PACKET_ID_NEW;
883 else if (byte[1] == 0x00 && byte[4] == 0x00)
884 packet_id = V7_PACKET_ID_IDLE;
885 else
886 packet_id = V7_PACKET_ID_UNKNOWN;
887
888 return packet_id;
889}
890
891static void alps_get_finger_coordinate_v7(struct input_mt_pos *mt,
892 unsigned char *pkt,
893 unsigned char pkt_id)
894{
Hans de Goede27a560b2014-12-18 09:54:50 -0800895 /*
896 * packet-fmt b7 b6 b5 b4 b3 b2 b1 b0
897 * Byte0 TWO & MULTI L 1 R M 1 Y0-2 Y0-1 Y0-0
898 * Byte0 NEW L 1 X1-5 1 1 Y0-2 Y0-1 Y0-0
899 * Byte1 Y0-10 Y0-9 Y0-8 Y0-7 Y0-6 Y0-5 Y0-4 Y0-3
900 * Byte2 X0-11 1 X0-10 X0-9 X0-8 X0-7 X0-6 X0-5
901 * Byte3 X1-11 1 X0-4 X0-3 1 X0-2 X0-1 X0-0
902 * Byte4 TWO X1-10 TWO X1-9 X1-8 X1-7 X1-6 X1-5 X1-4
903 * Byte4 MULTI X1-10 TWO X1-9 X1-8 X1-7 X1-6 Y1-5 1
904 * Byte4 NEW X1-10 TWO X1-9 X1-8 X1-7 X1-6 0 0
905 * Byte5 TWO & NEW Y1-10 0 Y1-9 Y1-8 Y1-7 Y1-6 Y1-5 Y1-4
906 * Byte5 MULTI Y1-10 0 Y1-9 Y1-8 Y1-7 Y1-6 F-1 F-0
907 * L: Left button
908 * R / M: Non-clickpads: Right / Middle button
909 * Clickpads: When > 2 fingers are down, and some fingers
910 * are in the button area, then the 2 coordinates reported
911 * are for fingers outside the button area and these report
912 * extra fingers being present in the right / left button
913 * area. Note these fingers are not added to the F field!
914 * so if a TWO packet is received and R = 1 then there are
915 * 3 fingers down, etc.
916 * TWO: 1: Two touches present, byte 0/4/5 are in TWO fmt
917 * 0: If byte 4 bit 0 is 1, then byte 0/4/5 are in MULTI fmt
918 * otherwise byte 0 bit 4 must be set and byte 0/4/5 are
919 * in NEW fmt
920 * F: Number of fingers - 3, 0 means 3 fingers, 1 means 4 ...
921 */
922
Yunkang Tang38088432014-07-26 13:51:41 -0700923 mt[0].x = ((pkt[2] & 0x80) << 4);
924 mt[0].x |= ((pkt[2] & 0x3F) << 5);
925 mt[0].x |= ((pkt[3] & 0x30) >> 1);
926 mt[0].x |= (pkt[3] & 0x07);
927 mt[0].y = (pkt[1] << 3) | (pkt[0] & 0x07);
928
929 mt[1].x = ((pkt[3] & 0x80) << 4);
930 mt[1].x |= ((pkt[4] & 0x80) << 3);
931 mt[1].x |= ((pkt[4] & 0x3F) << 4);
932 mt[1].y = ((pkt[5] & 0x80) << 3);
933 mt[1].y |= ((pkt[5] & 0x3F) << 4);
934
935 switch (pkt_id) {
936 case V7_PACKET_ID_TWO:
937 mt[1].x &= ~0x000F;
938 mt[1].y |= 0x000F;
939 break;
940
941 case V7_PACKET_ID_MULTI:
942 mt[1].x &= ~0x003F;
943 mt[1].y &= ~0x0020;
944 mt[1].y |= ((pkt[4] & 0x02) << 4);
945 mt[1].y |= 0x001F;
946 break;
947
948 case V7_PACKET_ID_NEW:
949 mt[1].x &= ~0x003F;
950 mt[1].x |= (pkt[0] & 0x20);
951 mt[1].y |= 0x000F;
952 break;
953 }
954
955 mt[0].y = 0x7FF - mt[0].y;
956 mt[1].y = 0x7FF - mt[1].y;
957}
958
959static int alps_get_mt_count(struct input_mt_pos *mt)
960{
Hans de Goede7091c442014-12-18 09:53:34 -0800961 int i, fingers = 0;
Yunkang Tang38088432014-07-26 13:51:41 -0700962
Hans de Goede7091c442014-12-18 09:53:34 -0800963 for (i = 0; i < MAX_TOUCHES; i++) {
964 if (mt[i].x != 0 || mt[i].y != 0)
965 fingers++;
966 }
Yunkang Tang38088432014-07-26 13:51:41 -0700967
Hans de Goede7091c442014-12-18 09:53:34 -0800968 return fingers;
Yunkang Tang38088432014-07-26 13:51:41 -0700969}
970
971static int alps_decode_packet_v7(struct alps_fields *f,
972 unsigned char *p,
973 struct psmouse *psmouse)
974{
Hans de Goeded27eb792014-12-18 09:55:14 -0800975 struct alps_data *priv = psmouse->private;
Yunkang Tang38088432014-07-26 13:51:41 -0700976 unsigned char pkt_id;
977
978 pkt_id = alps_get_packet_id_v7(p);
979 if (pkt_id == V7_PACKET_ID_IDLE)
980 return 0;
981 if (pkt_id == V7_PACKET_ID_UNKNOWN)
982 return -1;
Hans de Goede8b238112014-12-18 09:52:59 -0800983 /*
984 * NEW packets are send to indicate a discontinuity in the finger
985 * coordinate reporting. Specifically a finger may have moved from
986 * slot 0 to 1 or vice versa. INPUT_MT_TRACK takes care of this for
987 * us.
988 *
989 * NEW packets have 3 problems:
990 * 1) They do not contain middle / right button info (on non clickpads)
991 * this can be worked around by preserving the old button state
992 * 2) They do not contain an accurate fingercount, and they are
993 * typically send when the number of fingers changes. We cannot use
994 * the old finger count as that may mismatch with the amount of
995 * touch coordinates we've available in the NEW packet
996 * 3) Their x data for the second touch is inaccurate leading to
997 * a possible jump of the x coordinate by 16 units when the first
998 * non NEW packet comes in
999 * Since problems 2 & 3 cannot be worked around, just ignore them.
1000 */
1001 if (pkt_id == V7_PACKET_ID_NEW)
1002 return 1;
Yunkang Tang38088432014-07-26 13:51:41 -07001003
1004 alps_get_finger_coordinate_v7(f->mt, p, pkt_id);
1005
Yunkang Tang38088432014-07-26 13:51:41 -07001006 if (pkt_id == V7_PACKET_ID_TWO)
1007 f->fingers = alps_get_mt_count(f->mt);
Hans de Goede8b238112014-12-18 09:52:59 -08001008 else /* pkt_id == V7_PACKET_ID_MULTI */
Yunkang Tang38088432014-07-26 13:51:41 -07001009 f->fingers = 3 + (p[5] & 0x03);
1010
Hans de Goeded27eb792014-12-18 09:55:14 -08001011 f->left = (p[0] & 0x80) >> 7;
1012 if (priv->flags & ALPS_BUTTONPAD) {
1013 if (p[0] & 0x20)
1014 f->fingers++;
1015 if (p[0] & 0x10)
1016 f->fingers++;
1017 } else {
1018 f->right = (p[0] & 0x20) >> 5;
1019 f->middle = (p[0] & 0x10) >> 4;
1020 }
1021
Hans de Goede7091c442014-12-18 09:53:34 -08001022 /* Sometimes a single touch is reported in mt[1] rather then mt[0] */
1023 if (f->fingers == 1 && f->mt[0].x == 0 && f->mt[0].y == 0) {
1024 f->mt[0].x = f->mt[1].x;
1025 f->mt[0].y = f->mt[1].y;
1026 f->mt[1].x = 0;
1027 f->mt[1].y = 0;
1028 }
1029
Yunkang Tang38088432014-07-26 13:51:41 -07001030 return 0;
1031}
1032
1033static void alps_process_trackstick_packet_v7(struct psmouse *psmouse)
1034{
1035 struct alps_data *priv = psmouse->private;
1036 unsigned char *packet = psmouse->packet;
1037 struct input_dev *dev2 = priv->dev2;
1038 int x, y, z, left, right, middle;
1039
Pali Rohár34412ba2015-01-09 12:48:58 -08001040 /* It should be a DualPoint when received trackstick packet */
1041 if (!(priv->flags & ALPS_DUALPOINT)) {
1042 psmouse_warn(psmouse,
1043 "Rejected trackstick packet from non DualPoint device");
1044 return;
1045 }
1046
Yunkang Tang38088432014-07-26 13:51:41 -07001047 /*
1048 * b7 b6 b5 b4 b3 b2 b1 b0
1049 * Byte0 0 1 0 0 1 0 0 0
1050 * Byte1 1 1 * * 1 M R L
1051 * Byte2 X7 1 X5 X4 X3 X2 X1 X0
1052 * Byte3 Z6 1 Y6 X6 1 Y2 Y1 Y0
1053 * Byte4 Y7 0 Y5 Y4 Y3 1 1 0
1054 * Byte5 T&P 0 Z5 Z4 Z3 Z2 Z1 Z0
1055 * M / R / L: Middle / Right / Left button
1056 */
1057
1058 x = ((packet[2] & 0xbf)) | ((packet[3] & 0x10) << 2);
1059 y = (packet[3] & 0x07) | (packet[4] & 0xb8) |
1060 ((packet[3] & 0x20) << 1);
1061 z = (packet[5] & 0x3f) | ((packet[3] & 0x80) >> 1);
1062
1063 left = (packet[1] & 0x01);
1064 right = (packet[1] & 0x02) >> 1;
1065 middle = (packet[1] & 0x04) >> 2;
1066
1067 /* Divide 2 since trackpoint's speed is too fast */
1068 input_report_rel(dev2, REL_X, (char)x / 2);
1069 input_report_rel(dev2, REL_Y, -((char)y / 2));
1070
1071 input_report_key(dev2, BTN_LEFT, left);
1072 input_report_key(dev2, BTN_RIGHT, right);
1073 input_report_key(dev2, BTN_MIDDLE, middle);
1074
1075 input_sync(dev2);
1076}
1077
1078static void alps_process_touchpad_packet_v7(struct psmouse *psmouse)
1079{
1080 struct alps_data *priv = psmouse->private;
1081 struct input_dev *dev = psmouse->dev;
1082 struct alps_fields *f = &priv->f;
1083
1084 memset(f, 0, sizeof(*f));
1085
1086 if (priv->decode_fields(f, psmouse->packet, psmouse))
1087 return;
1088
1089 alps_report_mt_data(psmouse, alps_get_mt_count(f->mt));
1090
1091 input_mt_report_finger_count(dev, f->fingers);
1092
1093 input_report_key(dev, BTN_LEFT, f->left);
1094 input_report_key(dev, BTN_RIGHT, f->right);
1095 input_report_key(dev, BTN_MIDDLE, f->middle);
1096
1097 input_sync(dev);
1098}
1099
1100static void alps_process_packet_v7(struct psmouse *psmouse)
1101{
1102 unsigned char *packet = psmouse->packet;
1103
1104 if (packet[0] == 0x48 && (packet[4] & 0x47) == 0x06)
1105 alps_process_trackstick_packet_v7(psmouse);
1106 else
1107 alps_process_touchpad_packet_v7(psmouse);
1108}
1109
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001110static void alps_report_bare_ps2_packet(struct psmouse *psmouse,
1111 unsigned char packet[],
1112 bool report_buttons)
1113{
1114 struct alps_data *priv = psmouse->private;
1115 struct input_dev *dev2 = priv->dev2;
1116
1117 if (report_buttons)
1118 alps_report_buttons(psmouse, dev2, psmouse->dev,
1119 packet[0] & 1, packet[0] & 2, packet[0] & 4);
1120
1121 input_report_rel(dev2, REL_X,
1122 packet[1] ? packet[1] - ((packet[0] << 4) & 0x100) : 0);
1123 input_report_rel(dev2, REL_Y,
1124 packet[2] ? ((packet[0] << 3) & 0x100) - packet[2] : 0);
1125
1126 input_sync(dev2);
1127}
1128
1129static psmouse_ret_t alps_handle_interleaved_ps2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130{
1131 struct alps_data *priv = psmouse->private;
1132
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001133 if (psmouse->pktcnt < 6)
1134 return PSMOUSE_GOOD_DATA;
1135
1136 if (psmouse->pktcnt == 6) {
1137 /*
1138 * Start a timer to flush the packet if it ends up last
1139 * 6-byte packet in the stream. Timer needs to fire
1140 * psmouse core times out itself. 20 ms should be enough
1141 * to decide if we are getting more data or not.
1142 */
1143 mod_timer(&priv->timer, jiffies + msecs_to_jiffies(20));
1144 return PSMOUSE_GOOD_DATA;
1145 }
1146
1147 del_timer(&priv->timer);
1148
1149 if (psmouse->packet[6] & 0x80) {
1150
1151 /*
1152 * Highest bit is set - that means we either had
1153 * complete ALPS packet and this is start of the
1154 * next packet or we got garbage.
1155 */
1156
1157 if (((psmouse->packet[3] |
1158 psmouse->packet[4] |
1159 psmouse->packet[5]) & 0x80) ||
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001160 (!alps_is_valid_first_byte(priv, psmouse->packet[6]))) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001161 psmouse_dbg(psmouse,
Andy Shevchenko3b112922012-10-30 00:24:41 -07001162 "refusing packet %4ph (suspected interleaved ps/2)\n",
1163 psmouse->packet + 3);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001164 return PSMOUSE_BAD_DATA;
1165 }
1166
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001167 priv->process_packet(psmouse);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001168
1169 /* Continue with the next packet */
1170 psmouse->packet[0] = psmouse->packet[6];
1171 psmouse->pktcnt = 1;
1172
1173 } else {
1174
1175 /*
1176 * High bit is 0 - that means that we indeed got a PS/2
1177 * packet in the middle of ALPS packet.
1178 *
1179 * There is also possibility that we got 6-byte ALPS
1180 * packet followed by 3-byte packet from trackpoint. We
1181 * can not distinguish between these 2 scenarios but
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001182 * because the latter is unlikely to happen in course of
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001183 * normal operation (user would need to press all
1184 * buttons on the pad and start moving trackpoint
1185 * without touching the pad surface) we assume former.
1186 * Even if we are wrong the wost thing that would happen
1187 * the cursor would jump but we should not get protocol
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001188 * de-synchronization.
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001189 */
1190
1191 alps_report_bare_ps2_packet(psmouse, &psmouse->packet[3],
1192 false);
1193
1194 /*
1195 * Continue with the standard ALPS protocol handling,
1196 * but make sure we won't process it as an interleaved
1197 * packet again, which may happen if all buttons are
1198 * pressed. To avoid this let's reset the 4th bit which
1199 * is normally 1.
1200 */
1201 psmouse->packet[3] = psmouse->packet[6] & 0xf7;
1202 psmouse->pktcnt = 4;
1203 }
1204
1205 return PSMOUSE_GOOD_DATA;
1206}
1207
1208static void alps_flush_packet(unsigned long data)
1209{
1210 struct psmouse *psmouse = (struct psmouse *)data;
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001211 struct alps_data *priv = psmouse->private;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001212
1213 serio_pause_rx(psmouse->ps2dev.serio);
1214
Seth Forsheeb46615f2011-11-07 19:53:30 -08001215 if (psmouse->pktcnt == psmouse->pktsize) {
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001216
1217 /*
1218 * We did not any more data in reasonable amount of time.
1219 * Validate the last 3 bytes and process as a standard
1220 * ALPS packet.
1221 */
1222 if ((psmouse->packet[3] |
1223 psmouse->packet[4] |
1224 psmouse->packet[5]) & 0x80) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001225 psmouse_dbg(psmouse,
Andy Shevchenko3b112922012-10-30 00:24:41 -07001226 "refusing packet %3ph (suspected interleaved ps/2)\n",
1227 psmouse->packet + 3);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001228 } else {
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001229 priv->process_packet(psmouse);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001230 }
1231 psmouse->pktcnt = 0;
1232 }
1233
1234 serio_continue_rx(psmouse->ps2dev.serio);
1235}
1236
1237static psmouse_ret_t alps_process_byte(struct psmouse *psmouse)
1238{
1239 struct alps_data *priv = psmouse->private;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001240
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 if ((psmouse->packet[0] & 0xc8) == 0x08) { /* PS/2 packet */
1242 if (psmouse->pktcnt == 3) {
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001243 alps_report_bare_ps2_packet(psmouse, psmouse->packet,
1244 true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 return PSMOUSE_FULL_PACKET;
1246 }
1247 return PSMOUSE_GOOD_DATA;
1248 }
1249
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001250 /* Check for PS/2 packet stuffed in the middle of ALPS packet. */
1251
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001252 if ((priv->flags & ALPS_PS2_INTERLEAVED) &&
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001253 psmouse->pktcnt >= 4 && (psmouse->packet[3] & 0x0f) == 0x0f) {
1254 return alps_handle_interleaved_ps2(psmouse);
1255 }
1256
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001257 if (!alps_is_valid_first_byte(priv, psmouse->packet[0])) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001258 psmouse_dbg(psmouse,
1259 "refusing packet[0] = %x (mask0 = %x, byte0 = %x)\n",
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001260 psmouse->packet[0], priv->mask0, priv->byte0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 return PSMOUSE_BAD_DATA;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001262 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
Seth Forsheeb46615f2011-11-07 19:53:30 -08001264 /* Bytes 2 - pktsize should have 0 in the highest bit */
Yunkang Tang95f75e92013-12-01 22:33:52 -08001265 if ((priv->proto_version < ALPS_PROTO_V5) &&
Dave Turvene75af9e52013-02-21 22:58:28 -08001266 psmouse->pktcnt >= 2 && psmouse->pktcnt <= psmouse->pktsize &&
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001267 (psmouse->packet[psmouse->pktcnt - 1] & 0x80)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001268 psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
1269 psmouse->pktcnt - 1,
1270 psmouse->packet[psmouse->pktcnt - 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 return PSMOUSE_BAD_DATA;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
Yunkang Tang38088432014-07-26 13:51:41 -07001274 if (priv->proto_version == ALPS_PROTO_V7 &&
1275 !alps_is_valid_package_v7(psmouse)) {
1276 psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
1277 psmouse->pktcnt - 1,
1278 psmouse->packet[psmouse->pktcnt - 1]);
1279 return PSMOUSE_BAD_DATA;
1280 }
1281
Seth Forsheeb46615f2011-11-07 19:53:30 -08001282 if (psmouse->pktcnt == psmouse->pktsize) {
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001283 priv->process_packet(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 return PSMOUSE_FULL_PACKET;
1285 }
1286
1287 return PSMOUSE_GOOD_DATA;
1288}
1289
Seth Forshee25bded72011-11-07 19:53:36 -08001290static int alps_command_mode_send_nibble(struct psmouse *psmouse, int nibble)
1291{
1292 struct ps2dev *ps2dev = &psmouse->ps2dev;
1293 struct alps_data *priv = psmouse->private;
1294 int command;
1295 unsigned char *param;
1296 unsigned char dummy[4];
1297
1298 BUG_ON(nibble > 0xf);
1299
1300 command = priv->nibble_commands[nibble].command;
1301 param = (command & 0x0f00) ?
1302 dummy : (unsigned char *)&priv->nibble_commands[nibble].data;
1303
1304 if (ps2_command(ps2dev, param, command))
1305 return -1;
1306
1307 return 0;
1308}
1309
1310static int alps_command_mode_set_addr(struct psmouse *psmouse, int addr)
1311{
1312 struct ps2dev *ps2dev = &psmouse->ps2dev;
1313 struct alps_data *priv = psmouse->private;
1314 int i, nibble;
1315
1316 if (ps2_command(ps2dev, NULL, priv->addr_command))
1317 return -1;
1318
1319 for (i = 12; i >= 0; i -= 4) {
1320 nibble = (addr >> i) & 0xf;
1321 if (alps_command_mode_send_nibble(psmouse, nibble))
1322 return -1;
1323 }
1324
1325 return 0;
1326}
1327
1328static int __alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
1329{
1330 struct ps2dev *ps2dev = &psmouse->ps2dev;
1331 unsigned char param[4];
1332
1333 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1334 return -1;
1335
1336 /*
1337 * The address being read is returned in the first two bytes
1338 * of the result. Check that this address matches the expected
1339 * address.
1340 */
1341 if (addr != ((param[0] << 8) | param[1]))
1342 return -1;
1343
1344 return param[2];
1345}
1346
1347static int alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
1348{
1349 if (alps_command_mode_set_addr(psmouse, addr))
1350 return -1;
1351 return __alps_command_mode_read_reg(psmouse, addr);
1352}
1353
1354static int __alps_command_mode_write_reg(struct psmouse *psmouse, u8 value)
1355{
1356 if (alps_command_mode_send_nibble(psmouse, (value >> 4) & 0xf))
1357 return -1;
1358 if (alps_command_mode_send_nibble(psmouse, value & 0xf))
1359 return -1;
1360 return 0;
1361}
1362
1363static int alps_command_mode_write_reg(struct psmouse *psmouse, int addr,
1364 u8 value)
1365{
1366 if (alps_command_mode_set_addr(psmouse, addr))
1367 return -1;
1368 return __alps_command_mode_write_reg(psmouse, value);
1369}
1370
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001371static int alps_rpt_cmd(struct psmouse *psmouse, int init_command,
1372 int repeated_command, unsigned char *param)
1373{
1374 struct ps2dev *ps2dev = &psmouse->ps2dev;
1375
1376 param[0] = 0;
1377 if (init_command && ps2_command(ps2dev, param, init_command))
1378 return -EIO;
1379
1380 if (ps2_command(ps2dev, NULL, repeated_command) ||
1381 ps2_command(ps2dev, NULL, repeated_command) ||
1382 ps2_command(ps2dev, NULL, repeated_command))
1383 return -EIO;
1384
1385 param[0] = param[1] = param[2] = 0xff;
1386 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1387 return -EIO;
1388
Dmitry Torokhov39fbe582013-02-14 09:04:24 -08001389 psmouse_dbg(psmouse, "%2.2X report: %3ph\n",
1390 repeated_command, param);
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001391 return 0;
1392}
1393
Yunkang Tang38088432014-07-26 13:51:41 -07001394static bool alps_check_valid_firmware_id(unsigned char id[])
1395{
1396 if (id[0] == 0x73)
1397 return true;
1398
1399 if (id[0] == 0x88 &&
1400 (id[1] == 0x07 ||
1401 id[1] == 0x08 ||
1402 (id[1] & 0xf0) == 0xb0 ||
1403 (id[1] & 0xf0) == 0xc0)) {
1404 return true;
1405 }
1406
1407 return false;
1408}
1409
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08001410static int alps_enter_command_mode(struct psmouse *psmouse)
Seth Forshee25bded72011-11-07 19:53:36 -08001411{
1412 unsigned char param[4];
Seth Forshee25bded72011-11-07 19:53:36 -08001413
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001414 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_RESET_WRAP, param)) {
Seth Forshee25bded72011-11-07 19:53:36 -08001415 psmouse_err(psmouse, "failed to enter command mode\n");
1416 return -1;
1417 }
1418
Yunkang Tang38088432014-07-26 13:51:41 -07001419 if (!alps_check_valid_firmware_id(param)) {
Seth Forshee25bded72011-11-07 19:53:36 -08001420 psmouse_dbg(psmouse,
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001421 "unknown response while entering command mode\n");
Seth Forshee25bded72011-11-07 19:53:36 -08001422 return -1;
1423 }
Seth Forshee25bded72011-11-07 19:53:36 -08001424 return 0;
1425}
1426
1427static inline int alps_exit_command_mode(struct psmouse *psmouse)
1428{
1429 struct ps2dev *ps2dev = &psmouse->ps2dev;
1430 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM))
1431 return -1;
1432 return 0;
1433}
1434
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435/*
1436 * For DualPoint devices select the device that should respond to
1437 * subsequent commands. It looks like glidepad is behind stickpointer,
1438 * I'd thought it would be other way around...
1439 */
Seth Forshee25bded72011-11-07 19:53:36 -08001440static int alps_passthrough_mode_v2(struct psmouse *psmouse, bool enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441{
1442 struct ps2dev *ps2dev = &psmouse->ps2dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 int cmd = enable ? PSMOUSE_CMD_SETSCALE21 : PSMOUSE_CMD_SETSCALE11;
1444
1445 if (ps2_command(ps2dev, NULL, cmd) ||
1446 ps2_command(ps2dev, NULL, cmd) ||
1447 ps2_command(ps2dev, NULL, cmd) ||
1448 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1449 return -1;
1450
1451 /* we may get 3 more bytes, just ignore them */
Dmitry Torokhovc6117632005-06-01 02:39:51 -05001452 ps2_drain(ps2dev, 3, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
1454 return 0;
1455}
1456
Seth Forshee25bded72011-11-07 19:53:36 -08001457static int alps_absolute_mode_v1_v2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458{
1459 struct ps2dev *ps2dev = &psmouse->ps2dev;
1460
1461 /* Try ALPS magic knock - 4 disable before enable */
1462 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1463 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1464 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1465 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1466 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
1467 return -1;
1468
1469 /*
1470 * Switch mouse to poll (remote) mode so motion data will not
1471 * get in our way
1472 */
1473 return ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETPOLL);
1474}
1475
Yunkang Tang95f75e92013-12-01 22:33:52 -08001476static int alps_monitor_mode_send_word(struct psmouse *psmouse, u16 word)
1477{
1478 int i, nibble;
1479
1480 /*
1481 * b0-b11 are valid bits, send sequence is inverse.
1482 * e.g. when word = 0x0123, nibble send sequence is 3, 2, 1
1483 */
1484 for (i = 0; i <= 8; i += 4) {
1485 nibble = (word >> i) & 0xf;
1486 if (alps_command_mode_send_nibble(psmouse, nibble))
1487 return -1;
1488 }
1489
1490 return 0;
1491}
1492
1493static int alps_monitor_mode_write_reg(struct psmouse *psmouse,
1494 u16 addr, u16 value)
1495{
1496 struct ps2dev *ps2dev = &psmouse->ps2dev;
1497
1498 /* 0x0A0 is the command to write the word */
1499 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE) ||
1500 alps_monitor_mode_send_word(psmouse, 0x0A0) ||
1501 alps_monitor_mode_send_word(psmouse, addr) ||
1502 alps_monitor_mode_send_word(psmouse, value) ||
1503 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1504 return -1;
1505
1506 return 0;
1507}
1508
1509static int alps_monitor_mode(struct psmouse *psmouse, bool enable)
1510{
1511 struct ps2dev *ps2dev = &psmouse->ps2dev;
1512
1513 if (enable) {
1514 /* EC E9 F5 F5 E7 E6 E7 E9 to enter monitor mode */
1515 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
1516 ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO) ||
1517 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1518 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1519 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1520 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1521 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1522 ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO))
1523 return -1;
1524 } else {
1525 /* EC to exit monitor mode */
1526 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP))
1527 return -1;
1528 }
1529
1530 return 0;
1531}
1532
1533static int alps_absolute_mode_v6(struct psmouse *psmouse)
1534{
1535 u16 reg_val = 0x181;
1536 int ret = -1;
1537
1538 /* enter monitor mode, to write the register */
1539 if (alps_monitor_mode(psmouse, true))
1540 return -1;
1541
1542 ret = alps_monitor_mode_write_reg(psmouse, 0x000, reg_val);
1543
1544 if (alps_monitor_mode(psmouse, false))
1545 ret = -1;
1546
1547 return ret;
1548}
1549
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550static int alps_get_status(struct psmouse *psmouse, char *param)
1551{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 /* Get status: 0xF5 0xF5 0xF5 0xE9 */
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001553 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_DISABLE, param))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 return -1;
1555
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 return 0;
1557}
1558
1559/*
1560 * Turn touchpad tapping on or off. The sequences are:
1561 * 0xE9 0xF5 0xF5 0xF3 0x0A to enable,
1562 * 0xE9 0xF5 0xF5 0xE8 0x00 to disable.
1563 * My guess that 0xE9 (GetInfo) is here as a sync point.
1564 * For models that also have stickpointer (DualPoints) its tapping
1565 * is controlled separately (0xE6 0xE6 0xE6 0xF3 0x14|0x0A) but
1566 * we don't fiddle with it.
1567 */
1568static int alps_tap_mode(struct psmouse *psmouse, int enable)
1569{
1570 struct ps2dev *ps2dev = &psmouse->ps2dev;
1571 int cmd = enable ? PSMOUSE_CMD_SETRATE : PSMOUSE_CMD_SETRES;
1572 unsigned char tap_arg = enable ? 0x0A : 0x00;
1573 unsigned char param[4];
1574
1575 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO) ||
1576 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1577 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1578 ps2_command(ps2dev, &tap_arg, cmd))
1579 return -1;
1580
1581 if (alps_get_status(psmouse, param))
1582 return -1;
1583
1584 return 0;
1585}
1586
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001587/*
1588 * alps_poll() - poll the touchpad for current motion packet.
1589 * Used in resync.
1590 */
1591static int alps_poll(struct psmouse *psmouse)
1592{
1593 struct alps_data *priv = psmouse->private;
Seth Forsheeb46615f2011-11-07 19:53:30 -08001594 unsigned char buf[sizeof(psmouse->packet)];
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001595 bool poll_failed;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001596
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001597 if (priv->flags & ALPS_PASS)
Seth Forshee25bded72011-11-07 19:53:36 -08001598 alps_passthrough_mode_v2(psmouse, true);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001599
1600 poll_failed = ps2_command(&psmouse->ps2dev, buf,
1601 PSMOUSE_CMD_POLL | (psmouse->pktsize << 8)) < 0;
1602
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001603 if (priv->flags & ALPS_PASS)
Seth Forshee25bded72011-11-07 19:53:36 -08001604 alps_passthrough_mode_v2(psmouse, false);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001605
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001606 if (poll_failed || (buf[0] & priv->mask0) != priv->byte0)
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001607 return -1;
1608
1609 if ((psmouse->badbyte & 0xc8) == 0x08) {
1610/*
1611 * Poll the track stick ...
1612 */
1613 if (ps2_command(&psmouse->ps2dev, buf, PSMOUSE_CMD_POLL | (3 << 8)))
1614 return -1;
1615 }
1616
1617 memcpy(psmouse->packet, buf, sizeof(buf));
1618 return 0;
1619}
1620
Seth Forshee25bded72011-11-07 19:53:36 -08001621static int alps_hw_init_v1_v2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622{
1623 struct alps_data *priv = psmouse->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001625 if ((priv->flags & ALPS_PASS) &&
Seth Forshee25bded72011-11-07 19:53:36 -08001626 alps_passthrough_mode_v2(psmouse, true)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 return -1;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001628 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001630 if (alps_tap_mode(psmouse, true)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001631 psmouse_warn(psmouse, "Failed to enable hardware tapping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 return -1;
Peter Osterlund963f6262005-07-11 01:08:04 -05001633 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
Seth Forshee25bded72011-11-07 19:53:36 -08001635 if (alps_absolute_mode_v1_v2(psmouse)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001636 psmouse_err(psmouse, "Failed to enable absolute mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 return -1;
1638 }
1639
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001640 if ((priv->flags & ALPS_PASS) &&
Seth Forshee25bded72011-11-07 19:53:36 -08001641 alps_passthrough_mode_v2(psmouse, false)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 return -1;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001643 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04001645 /* ALPS needs stream mode, otherwise it won't report any data */
1646 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001647 psmouse_err(psmouse, "Failed to enable stream mode\n");
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04001648 return -1;
1649 }
1650
1651 return 0;
1652}
1653
Yunkang Tang95f75e92013-12-01 22:33:52 -08001654static int alps_hw_init_v6(struct psmouse *psmouse)
1655{
1656 unsigned char param[2] = {0xC8, 0x14};
1657
1658 /* Enter passthrough mode to let trackpoint enter 6byte raw mode */
1659 if (alps_passthrough_mode_v2(psmouse, true))
1660 return -1;
1661
1662 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1663 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1664 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1665 ps2_command(&psmouse->ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
1666 ps2_command(&psmouse->ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
1667 return -1;
1668
1669 if (alps_passthrough_mode_v2(psmouse, false))
1670 return -1;
1671
1672 if (alps_absolute_mode_v6(psmouse)) {
1673 psmouse_err(psmouse, "Failed to enable absolute mode\n");
1674 return -1;
1675 }
1676
1677 return 0;
1678}
1679
Seth Forshee25bded72011-11-07 19:53:36 -08001680/*
Kevin Cernekeecd401202013-02-13 22:28:07 -08001681 * Enable or disable passthrough mode to the trackstick.
Seth Forshee25bded72011-11-07 19:53:36 -08001682 */
Kevin Cernekeecd401202013-02-13 22:28:07 -08001683static int alps_passthrough_mode_v3(struct psmouse *psmouse,
1684 int reg_base, bool enable)
Seth Forshee25bded72011-11-07 19:53:36 -08001685{
Kevin Cernekeecd401202013-02-13 22:28:07 -08001686 int reg_val, ret = -1;
Seth Forshee25bded72011-11-07 19:53:36 -08001687
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08001688 if (alps_enter_command_mode(psmouse))
Seth Forshee25bded72011-11-07 19:53:36 -08001689 return -1;
1690
Kevin Cernekeecd401202013-02-13 22:28:07 -08001691 reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x0008);
1692 if (reg_val == -1)
1693 goto error;
1694
Seth Forshee25bded72011-11-07 19:53:36 -08001695 if (enable)
1696 reg_val |= 0x01;
1697 else
1698 reg_val &= ~0x01;
1699
Kevin Cernekeecd401202013-02-13 22:28:07 -08001700 ret = __alps_command_mode_write_reg(psmouse, reg_val);
Seth Forshee25bded72011-11-07 19:53:36 -08001701
Kevin Cernekeecd401202013-02-13 22:28:07 -08001702error:
1703 if (alps_exit_command_mode(psmouse))
1704 ret = -1;
1705 return ret;
Seth Forshee25bded72011-11-07 19:53:36 -08001706}
1707
1708/* Must be in command mode when calling this function */
1709static int alps_absolute_mode_v3(struct psmouse *psmouse)
1710{
1711 int reg_val;
1712
1713 reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
1714 if (reg_val == -1)
1715 return -1;
1716
1717 reg_val |= 0x06;
1718 if (__alps_command_mode_write_reg(psmouse, reg_val))
1719 return -1;
1720
1721 return 0;
1722}
1723
Kevin Cernekeecd401202013-02-13 22:28:07 -08001724static int alps_probe_trackstick_v3(struct psmouse *psmouse, int reg_base)
1725{
1726 int ret = -EIO, reg_val;
1727
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08001728 if (alps_enter_command_mode(psmouse))
Kevin Cernekeecd401202013-02-13 22:28:07 -08001729 goto error;
1730
1731 reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x08);
1732 if (reg_val == -1)
1733 goto error;
1734
1735 /* bit 7: trackstick is present */
1736 ret = reg_val & 0x80 ? 0 : -ENODEV;
1737
1738error:
1739 alps_exit_command_mode(psmouse);
1740 return ret;
1741}
1742
1743static int alps_setup_trackstick_v3(struct psmouse *psmouse, int reg_base)
1744{
1745 struct ps2dev *ps2dev = &psmouse->ps2dev;
1746 int ret = 0;
1747 unsigned char param[4];
1748
1749 if (alps_passthrough_mode_v3(psmouse, reg_base, true))
1750 return -EIO;
1751
1752 /*
1753 * E7 report for the trackstick
1754 *
1755 * There have been reports of failures to seem to trace back
1756 * to the above trackstick check failing. When these occur
1757 * this E7 report fails, so when that happens we continue
1758 * with the assumption that there isn't a trackstick after
1759 * all.
1760 */
1761 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_SETSCALE21, param)) {
1762 psmouse_warn(psmouse, "trackstick E7 report failed\n");
1763 ret = -ENODEV;
1764 } else {
Dmitry Torokhov39fbe582013-02-14 09:04:24 -08001765 psmouse_dbg(psmouse, "trackstick E7 report: %3ph\n", param);
Kevin Cernekeecd401202013-02-13 22:28:07 -08001766
1767 /*
1768 * Not sure what this does, but it is absolutely
1769 * essential. Without it, the touchpad does not
1770 * work at all and the trackstick just emits normal
1771 * PS/2 packets.
1772 */
1773 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1774 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1775 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1776 alps_command_mode_send_nibble(psmouse, 0x9) ||
1777 alps_command_mode_send_nibble(psmouse, 0x4)) {
1778 psmouse_err(psmouse,
1779 "Error sending magic E6 sequence\n");
1780 ret = -EIO;
1781 goto error;
1782 }
1783
1784 /*
1785 * This ensures the trackstick packets are in the format
1786 * supported by this driver. If bit 1 isn't set the packet
1787 * format is different.
1788 */
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08001789 if (alps_enter_command_mode(psmouse) ||
Kevin Cernekeecd401202013-02-13 22:28:07 -08001790 alps_command_mode_write_reg(psmouse,
1791 reg_base + 0x08, 0x82) ||
1792 alps_exit_command_mode(psmouse))
1793 ret = -EIO;
1794 }
1795
1796error:
1797 if (alps_passthrough_mode_v3(psmouse, reg_base, false))
1798 ret = -EIO;
1799
1800 return ret;
1801}
1802
Seth Forshee25bded72011-11-07 19:53:36 -08001803static int alps_hw_init_v3(struct psmouse *psmouse)
1804{
Seth Forshee25bded72011-11-07 19:53:36 -08001805 struct ps2dev *ps2dev = &psmouse->ps2dev;
1806 int reg_val;
1807 unsigned char param[4];
1808
Kevin Cernekeecd401202013-02-13 22:28:07 -08001809 reg_val = alps_probe_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE);
1810 if (reg_val == -EIO)
1811 goto error;
Dmitry Torokhov39fbe582013-02-14 09:04:24 -08001812
Kevin Cernekeecd401202013-02-13 22:28:07 -08001813 if (reg_val == 0 &&
1814 alps_setup_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE) == -EIO)
Seth Forshee25bded72011-11-07 19:53:36 -08001815 goto error;
1816
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08001817 if (alps_enter_command_mode(psmouse) ||
Kevin Cernekeecd401202013-02-13 22:28:07 -08001818 alps_absolute_mode_v3(psmouse)) {
Seth Forshee25bded72011-11-07 19:53:36 -08001819 psmouse_err(psmouse, "Failed to enter absolute mode\n");
1820 goto error;
1821 }
1822
1823 reg_val = alps_command_mode_read_reg(psmouse, 0x0006);
1824 if (reg_val == -1)
1825 goto error;
1826 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
1827 goto error;
1828
1829 reg_val = alps_command_mode_read_reg(psmouse, 0x0007);
1830 if (reg_val == -1)
1831 goto error;
1832 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
1833 goto error;
1834
1835 if (alps_command_mode_read_reg(psmouse, 0x0144) == -1)
1836 goto error;
1837 if (__alps_command_mode_write_reg(psmouse, 0x04))
1838 goto error;
1839
1840 if (alps_command_mode_read_reg(psmouse, 0x0159) == -1)
1841 goto error;
1842 if (__alps_command_mode_write_reg(psmouse, 0x03))
1843 goto error;
1844
1845 if (alps_command_mode_read_reg(psmouse, 0x0163) == -1)
1846 goto error;
1847 if (alps_command_mode_write_reg(psmouse, 0x0163, 0x03))
1848 goto error;
1849
1850 if (alps_command_mode_read_reg(psmouse, 0x0162) == -1)
1851 goto error;
1852 if (alps_command_mode_write_reg(psmouse, 0x0162, 0x04))
1853 goto error;
1854
Seth Forshee25bded72011-11-07 19:53:36 -08001855 alps_exit_command_mode(psmouse);
1856
1857 /* Set rate and enable data reporting */
1858 param[0] = 0x64;
1859 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
1860 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
1861 psmouse_err(psmouse, "Failed to enable data reporting\n");
1862 return -1;
1863 }
1864
1865 return 0;
1866
Seth Forshee25bded72011-11-07 19:53:36 -08001867error:
1868 /*
1869 * Leaving the touchpad in command mode will essentially render
1870 * it unusable until the machine reboots, so exit it here just
1871 * to be safe
1872 */
1873 alps_exit_command_mode(psmouse);
1874 return -1;
1875}
1876
Hans de Goedef3f33c62014-07-29 11:22:07 -07001877static int alps_get_v3_v7_resolution(struct psmouse *psmouse, int reg_pitch)
1878{
1879 int reg, x_pitch, y_pitch, x_electrode, y_electrode, x_phys, y_phys;
1880 struct alps_data *priv = psmouse->private;
1881
1882 reg = alps_command_mode_read_reg(psmouse, reg_pitch);
1883 if (reg < 0)
1884 return reg;
1885
1886 x_pitch = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
1887 x_pitch = 50 + 2 * x_pitch; /* In 0.1 mm units */
1888
1889 y_pitch = (char)reg >> 4; /* sign extend upper 4 bits */
1890 y_pitch = 36 + 2 * y_pitch; /* In 0.1 mm units */
1891
1892 reg = alps_command_mode_read_reg(psmouse, reg_pitch + 1);
1893 if (reg < 0)
1894 return reg;
1895
1896 x_electrode = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
1897 x_electrode = 17 + x_electrode;
1898
1899 y_electrode = (char)reg >> 4; /* sign extend upper 4 bits */
1900 y_electrode = 13 + y_electrode;
1901
1902 x_phys = x_pitch * (x_electrode - 1); /* In 0.1 mm units */
1903 y_phys = y_pitch * (y_electrode - 1); /* In 0.1 mm units */
1904
1905 priv->x_res = priv->x_max * 10 / x_phys; /* units / mm */
1906 priv->y_res = priv->y_max * 10 / y_phys; /* units / mm */
1907
1908 psmouse_dbg(psmouse,
1909 "pitch %dx%d num-electrodes %dx%d physical size %dx%d mm res %dx%d\n",
1910 x_pitch, y_pitch, x_electrode, y_electrode,
1911 x_phys / 10, y_phys / 10, priv->x_res, priv->y_res);
1912
1913 return 0;
1914}
1915
Kevin Cernekee1302bac2013-02-13 22:27:08 -08001916static int alps_hw_init_rushmore_v3(struct psmouse *psmouse)
1917{
Kevin Cernekeecd401202013-02-13 22:28:07 -08001918 struct alps_data *priv = psmouse->private;
Kevin Cernekee1302bac2013-02-13 22:27:08 -08001919 struct ps2dev *ps2dev = &psmouse->ps2dev;
1920 int reg_val, ret = -1;
1921
Kevin Cernekeecd401202013-02-13 22:28:07 -08001922 if (priv->flags & ALPS_DUALPOINT) {
1923 reg_val = alps_setup_trackstick_v3(psmouse,
1924 ALPS_REG_BASE_RUSHMORE);
1925 if (reg_val == -EIO)
1926 goto error;
1927 if (reg_val == -ENODEV)
1928 priv->flags &= ~ALPS_DUALPOINT;
1929 }
1930
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08001931 if (alps_enter_command_mode(psmouse) ||
Kevin Cernekee1302bac2013-02-13 22:27:08 -08001932 alps_command_mode_read_reg(psmouse, 0xc2d9) == -1 ||
1933 alps_command_mode_write_reg(psmouse, 0xc2cb, 0x00))
1934 goto error;
1935
Hans de Goedef3f33c62014-07-29 11:22:07 -07001936 if (alps_get_v3_v7_resolution(psmouse, 0xc2da))
1937 goto error;
1938
Kevin Cernekee1302bac2013-02-13 22:27:08 -08001939 reg_val = alps_command_mode_read_reg(psmouse, 0xc2c6);
1940 if (reg_val == -1)
1941 goto error;
1942 if (__alps_command_mode_write_reg(psmouse, reg_val & 0xfd))
1943 goto error;
1944
1945 if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
1946 goto error;
1947
1948 /* enter absolute mode */
1949 reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
1950 if (reg_val == -1)
1951 goto error;
1952 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
1953 goto error;
1954
1955 alps_exit_command_mode(psmouse);
1956 return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
1957
1958error:
1959 alps_exit_command_mode(psmouse);
1960 return ret;
1961}
1962
Seth Forshee25bded72011-11-07 19:53:36 -08001963/* Must be in command mode when calling this function */
1964static int alps_absolute_mode_v4(struct psmouse *psmouse)
1965{
1966 int reg_val;
1967
1968 reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
1969 if (reg_val == -1)
1970 return -1;
1971
1972 reg_val |= 0x02;
1973 if (__alps_command_mode_write_reg(psmouse, reg_val))
1974 return -1;
1975
1976 return 0;
1977}
1978
1979static int alps_hw_init_v4(struct psmouse *psmouse)
1980{
Seth Forshee25bded72011-11-07 19:53:36 -08001981 struct ps2dev *ps2dev = &psmouse->ps2dev;
1982 unsigned char param[4];
1983
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08001984 if (alps_enter_command_mode(psmouse))
Seth Forshee25bded72011-11-07 19:53:36 -08001985 goto error;
1986
1987 if (alps_absolute_mode_v4(psmouse)) {
1988 psmouse_err(psmouse, "Failed to enter absolute mode\n");
1989 goto error;
1990 }
1991
1992 if (alps_command_mode_write_reg(psmouse, 0x0007, 0x8c))
1993 goto error;
1994
1995 if (alps_command_mode_write_reg(psmouse, 0x0149, 0x03))
1996 goto error;
1997
1998 if (alps_command_mode_write_reg(psmouse, 0x0160, 0x03))
1999 goto error;
2000
2001 if (alps_command_mode_write_reg(psmouse, 0x017f, 0x15))
2002 goto error;
2003
2004 if (alps_command_mode_write_reg(psmouse, 0x0151, 0x01))
2005 goto error;
2006
2007 if (alps_command_mode_write_reg(psmouse, 0x0168, 0x03))
2008 goto error;
2009
2010 if (alps_command_mode_write_reg(psmouse, 0x014a, 0x03))
2011 goto error;
2012
2013 if (alps_command_mode_write_reg(psmouse, 0x0161, 0x03))
2014 goto error;
2015
2016 alps_exit_command_mode(psmouse);
2017
2018 /*
2019 * This sequence changes the output from a 9-byte to an
2020 * 8-byte format. All the same data seems to be present,
2021 * just in a more compact format.
2022 */
2023 param[0] = 0xc8;
2024 param[1] = 0x64;
2025 param[2] = 0x50;
2026 if (ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2027 ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE) ||
2028 ps2_command(ps2dev, &param[2], PSMOUSE_CMD_SETRATE) ||
2029 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
2030 return -1;
2031
2032 /* Set rate and enable data reporting */
2033 param[0] = 0x64;
2034 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
2035 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
2036 psmouse_err(psmouse, "Failed to enable data reporting\n");
2037 return -1;
2038 }
2039
2040 return 0;
2041
2042error:
2043 /*
2044 * Leaving the touchpad in command mode will essentially render
2045 * it unusable until the machine reboots, so exit it here just
2046 * to be safe
2047 */
2048 alps_exit_command_mode(psmouse);
2049 return -1;
2050}
2051
Yunkang Tangee65d4b2013-12-26 14:54:19 -08002052static int alps_dolphin_get_device_area(struct psmouse *psmouse,
2053 struct alps_data *priv)
2054{
2055 struct ps2dev *ps2dev = &psmouse->ps2dev;
2056 unsigned char param[4] = {0};
2057 int num_x_electrode, num_y_electrode;
2058
2059 if (alps_enter_command_mode(psmouse))
2060 return -1;
2061
2062 param[0] = 0x0a;
2063 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
2064 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2065 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2066 ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2067 ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE))
2068 return -1;
2069
2070 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
2071 return -1;
2072
2073 /*
2074 * Dolphin's sensor line number is not fixed. It can be calculated
2075 * by adding the device's register value with DOLPHIN_PROFILE_X/YOFFSET.
2076 * Further more, we can get device's x_max and y_max by multiplying
2077 * sensor line number with DOLPHIN_COUNT_PER_ELECTRODE.
2078 *
2079 * e.g. When we get register's sensor_x = 11 & sensor_y = 8,
2080 * real sensor line number X = 11 + 8 = 19, and
2081 * real sensor line number Y = 8 + 1 = 9.
2082 * So, x_max = (19 - 1) * 64 = 1152, and
2083 * y_max = (9 - 1) * 64 = 512.
2084 */
2085 num_x_electrode = DOLPHIN_PROFILE_XOFFSET + (param[2] & 0x0F);
2086 num_y_electrode = DOLPHIN_PROFILE_YOFFSET + ((param[2] >> 4) & 0x0F);
2087 priv->x_bits = num_x_electrode;
2088 priv->y_bits = num_y_electrode;
2089 priv->x_max = (num_x_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
2090 priv->y_max = (num_y_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
2091
2092 if (alps_exit_command_mode(psmouse))
2093 return -1;
2094
2095 return 0;
2096}
2097
Dave Turvene75af9e52013-02-21 22:58:28 -08002098static int alps_hw_init_dolphin_v1(struct psmouse *psmouse)
2099{
2100 struct ps2dev *ps2dev = &psmouse->ps2dev;
2101 unsigned char param[2];
2102
2103 /* This is dolphin "v1" as empirically defined by florin9doi */
2104 param[0] = 0x64;
2105 param[1] = 0x28;
2106
2107 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2108 ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2109 ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
2110 return -1;
2111
2112 return 0;
2113}
2114
Yunkang Tang38088432014-07-26 13:51:41 -07002115static int alps_hw_init_v7(struct psmouse *psmouse)
2116{
2117 struct ps2dev *ps2dev = &psmouse->ps2dev;
2118 int reg_val, ret = -1;
2119
2120 if (alps_enter_command_mode(psmouse) ||
2121 alps_command_mode_read_reg(psmouse, 0xc2d9) == -1)
2122 goto error;
2123
Hans de Goedef3f33c62014-07-29 11:22:07 -07002124 if (alps_get_v3_v7_resolution(psmouse, 0xc397))
2125 goto error;
2126
Yunkang Tang38088432014-07-26 13:51:41 -07002127 if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
2128 goto error;
2129
2130 reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
2131 if (reg_val == -1)
2132 goto error;
2133 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
2134 goto error;
2135
2136 alps_exit_command_mode(psmouse);
2137 return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
2138
2139error:
2140 alps_exit_command_mode(psmouse);
2141 return ret;
2142}
2143
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002144static void alps_set_defaults(struct alps_data *priv)
Seth Forshee25bded72011-11-07 19:53:36 -08002145{
Kevin Cernekeef673ceb2013-02-13 22:23:34 -08002146 priv->byte0 = 0x8f;
2147 priv->mask0 = 0x8f;
2148 priv->flags = ALPS_DUALPOINT;
2149
Kevin Cernekee7a9f73e2013-02-13 22:24:55 -08002150 priv->x_max = 2000;
2151 priv->y_max = 1400;
2152 priv->x_bits = 15;
2153 priv->y_bits = 11;
2154
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002155 switch (priv->proto_version) {
Seth Forshee25bded72011-11-07 19:53:36 -08002156 case ALPS_PROTO_V1:
2157 case ALPS_PROTO_V2:
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002158 priv->hw_init = alps_hw_init_v1_v2;
2159 priv->process_packet = alps_process_packet_v1_v2;
2160 priv->set_abs_params = alps_set_abs_params_st;
Yunkang Tang95f75e92013-12-01 22:33:52 -08002161 priv->x_max = 1023;
2162 priv->y_max = 767;
Seth Forshee25bded72011-11-07 19:53:36 -08002163 break;
2164 case ALPS_PROTO_V3:
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002165 priv->hw_init = alps_hw_init_v3;
2166 priv->process_packet = alps_process_packet_v3;
2167 priv->set_abs_params = alps_set_abs_params_mt;
Kevin Cernekeef85e5002013-02-13 22:26:11 -08002168 priv->decode_fields = alps_decode_pinnacle;
Kevin Cernekee50e8b212013-02-13 22:23:04 -08002169 priv->nibble_commands = alps_v3_nibble_commands;
2170 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
Seth Forshee25bded72011-11-07 19:53:36 -08002171 break;
2172 case ALPS_PROTO_V4:
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002173 priv->hw_init = alps_hw_init_v4;
2174 priv->process_packet = alps_process_packet_v4;
2175 priv->set_abs_params = alps_set_abs_params_mt;
Kevin Cernekee50e8b212013-02-13 22:23:04 -08002176 priv->nibble_commands = alps_v4_nibble_commands;
2177 priv->addr_command = PSMOUSE_CMD_DISABLE;
Seth Forshee25bded72011-11-07 19:53:36 -08002178 break;
Dave Turvene75af9e52013-02-21 22:58:28 -08002179 case ALPS_PROTO_V5:
2180 priv->hw_init = alps_hw_init_dolphin_v1;
Yunkang Tangee65d4b2013-12-26 14:54:19 -08002181 priv->process_packet = alps_process_touchpad_packet_v3_v5;
Dave Turvene75af9e52013-02-21 22:58:28 -08002182 priv->decode_fields = alps_decode_dolphin;
2183 priv->set_abs_params = alps_set_abs_params_mt;
2184 priv->nibble_commands = alps_v3_nibble_commands;
2185 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2186 priv->byte0 = 0xc8;
Yunkang Tangee65d4b2013-12-26 14:54:19 -08002187 priv->mask0 = 0xd8;
Dave Turvene75af9e52013-02-21 22:58:28 -08002188 priv->flags = 0;
2189 priv->x_max = 1360;
2190 priv->y_max = 660;
2191 priv->x_bits = 23;
2192 priv->y_bits = 12;
2193 break;
Yunkang Tang95f75e92013-12-01 22:33:52 -08002194 case ALPS_PROTO_V6:
2195 priv->hw_init = alps_hw_init_v6;
2196 priv->process_packet = alps_process_packet_v6;
2197 priv->set_abs_params = alps_set_abs_params_st;
2198 priv->nibble_commands = alps_v6_nibble_commands;
2199 priv->x_max = 2047;
2200 priv->y_max = 1535;
2201 break;
Yunkang Tang38088432014-07-26 13:51:41 -07002202 case ALPS_PROTO_V7:
2203 priv->hw_init = alps_hw_init_v7;
2204 priv->process_packet = alps_process_packet_v7;
2205 priv->decode_fields = alps_decode_packet_v7;
2206 priv->set_abs_params = alps_set_abs_params_mt;
2207 priv->nibble_commands = alps_v3_nibble_commands;
2208 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2209 priv->x_max = 0xfff;
2210 priv->y_max = 0x7ff;
2211 priv->byte0 = 0x48;
2212 priv->mask0 = 0x48;
2213
2214 if (priv->fw_ver[1] != 0xba)
2215 priv->flags |= ALPS_BUTTONPAD;
2216 break;
Seth Forshee25bded72011-11-07 19:53:36 -08002217 }
Seth Forshee25bded72011-11-07 19:53:36 -08002218}
2219
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002220static int alps_match_table(struct psmouse *psmouse, struct alps_data *priv,
2221 unsigned char *e7, unsigned char *ec)
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002222{
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002223 const struct alps_model_info *model;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002224 int i;
2225
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002226 for (i = 0; i < ARRAY_SIZE(alps_model_data); i++) {
2227 model = &alps_model_data[i];
2228
2229 if (!memcmp(e7, model->signature, sizeof(model->signature)) &&
2230 (!model->command_mode_resp ||
2231 model->command_mode_resp == ec[2])) {
2232
2233 priv->proto_version = model->proto_version;
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002234 alps_set_defaults(priv);
2235
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002236 priv->flags = model->flags;
2237 priv->byte0 = model->byte0;
2238 priv->mask0 = model->mask0;
2239
2240 return 0;
2241 }
2242 }
2243
2244 return -EINVAL;
2245}
2246
2247static int alps_identify(struct psmouse *psmouse, struct alps_data *priv)
2248{
2249 unsigned char e6[4], e7[4], ec[4];
2250
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002251 /*
2252 * First try "E6 report".
2253 * ALPS should return 0,0,10 or 0,0,100 if no buttons are pressed.
2254 * The bits 0-2 of the first byte will be 1s if some buttons are
2255 * pressed.
2256 */
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002257 if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2258 PSMOUSE_CMD_SETSCALE11, e6))
2259 return -EIO;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002260
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002261 if ((e6[0] & 0xf8) != 0 || e6[1] != 0 || (e6[2] != 10 && e6[2] != 100))
2262 return -EINVAL;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002263
2264 /*
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002265 * Now get the "E7" and "EC" reports. These will uniquely identify
2266 * most ALPS touchpads.
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002267 */
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002268 if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2269 PSMOUSE_CMD_SETSCALE21, e7) ||
2270 alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2271 PSMOUSE_CMD_RESET_WRAP, ec) ||
2272 alps_exit_command_mode(psmouse))
2273 return -EIO;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002274
Hans de Goedec0cd17f2014-07-25 22:49:14 -07002275 /* Save the Firmware version */
2276 memcpy(priv->fw_ver, ec, 3);
2277
Kevin Cernekeef673ceb2013-02-13 22:23:34 -08002278 if (alps_match_table(psmouse, priv, e7, ec) == 0) {
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002279 return 0;
Dave Turvene75af9e52013-02-21 22:58:28 -08002280 } else if (e7[0] == 0x73 && e7[1] == 0x03 && e7[2] == 0x50 &&
Yunkang Tangee65d4b2013-12-26 14:54:19 -08002281 ec[0] == 0x73 && (ec[1] == 0x01 || ec[1] == 0x02)) {
Dave Turvene75af9e52013-02-21 22:58:28 -08002282 priv->proto_version = ALPS_PROTO_V5;
2283 alps_set_defaults(priv);
Yunkang Tangee65d4b2013-12-26 14:54:19 -08002284 if (alps_dolphin_get_device_area(psmouse, priv))
2285 return -EIO;
2286 else
2287 return 0;
Yunkang Tang38088432014-07-26 13:51:41 -07002288 } else if (ec[0] == 0x88 &&
2289 ((ec[1] & 0xf0) == 0xb0 || (ec[1] & 0xf0) == 0xc0)) {
2290 priv->proto_version = ALPS_PROTO_V7;
2291 alps_set_defaults(priv);
2292
2293 return 0;
Kevin Cernekee1302bac2013-02-13 22:27:08 -08002294 } else if (ec[0] == 0x88 && ec[1] == 0x08) {
2295 priv->proto_version = ALPS_PROTO_V3;
2296 alps_set_defaults(priv);
2297
2298 priv->hw_init = alps_hw_init_rushmore_v3;
2299 priv->decode_fields = alps_decode_rushmore;
2300 priv->x_bits = 16;
2301 priv->y_bits = 12;
Hans de Goede40e8f532014-07-25 22:37:15 -07002302 priv->flags |= ALPS_IS_RUSHMORE;
Kevin Cernekee1302bac2013-02-13 22:27:08 -08002303
Kevin Cernekeecd401202013-02-13 22:28:07 -08002304 /* hack to make addr_command, nibble_command available */
2305 psmouse->private = priv;
2306
2307 if (alps_probe_trackstick_v3(psmouse, ALPS_REG_BASE_RUSHMORE))
2308 priv->flags &= ~ALPS_DUALPOINT;
2309
Kevin Cernekee1302bac2013-02-13 22:27:08 -08002310 return 0;
Kevin Cernekeef673ceb2013-02-13 22:23:34 -08002311 } else if (ec[0] == 0x88 && ec[1] == 0x07 &&
2312 ec[2] >= 0x90 && ec[2] <= 0x9d) {
2313 priv->proto_version = ALPS_PROTO_V3;
2314 alps_set_defaults(priv);
2315
2316 return 0;
2317 }
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002318
Dmitry Torokhova9e06212014-08-25 11:24:47 -07002319 psmouse_dbg(psmouse,
2320 "Likely not an ALPS touchpad: E7=%3ph, EC=%3ph\n", e7, ec);
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002321
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002322 return -EINVAL;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002323}
2324
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04002325static int alps_reconnect(struct psmouse *psmouse)
2326{
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002327 struct alps_data *priv = psmouse->private;
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08002328
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04002329 psmouse_reset(psmouse);
2330
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002331 if (alps_identify(psmouse, priv) < 0)
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04002332 return -1;
2333
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002334 return priv->hw_init(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335}
2336
2337static void alps_disconnect(struct psmouse *psmouse)
2338{
2339 struct alps_data *priv = psmouse->private;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05002340
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 psmouse_reset(psmouse);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08002342 del_timer_sync(&priv->timer);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05002343 input_unregister_device(priv->dev2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 kfree(priv);
2345}
2346
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002347static void alps_set_abs_params_st(struct alps_data *priv,
2348 struct input_dev *dev1)
2349{
Yunkang Tang95f75e92013-12-01 22:33:52 -08002350 input_set_abs_params(dev1, ABS_X, 0, priv->x_max, 0, 0);
2351 input_set_abs_params(dev1, ABS_Y, 0, priv->y_max, 0, 0);
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002352}
2353
2354static void alps_set_abs_params_mt(struct alps_data *priv,
2355 struct input_dev *dev1)
2356{
Kevin Cernekee7a9f73e2013-02-13 22:24:55 -08002357 input_set_abs_params(dev1, ABS_MT_POSITION_X, 0, priv->x_max, 0, 0);
2358 input_set_abs_params(dev1, ABS_MT_POSITION_Y, 0, priv->y_max, 0, 0);
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002359
Hans de Goedef3f33c62014-07-29 11:22:07 -07002360 input_abs_set_res(dev1, ABS_MT_POSITION_X, priv->x_res);
2361 input_abs_set_res(dev1, ABS_MT_POSITION_Y, priv->y_res);
2362
Hans de Goedecdf333e2014-07-25 22:44:42 -07002363 input_mt_init_slots(dev1, MAX_TOUCHES, INPUT_MT_POINTER |
2364 INPUT_MT_DROP_UNUSED | INPUT_MT_TRACK | INPUT_MT_SEMI_MT);
2365
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002366 set_bit(BTN_TOOL_TRIPLETAP, dev1->keybit);
2367 set_bit(BTN_TOOL_QUADTAP, dev1->keybit);
Yunkang Tang38088432014-07-26 13:51:41 -07002368
2369 /* V7 is real multi-touch */
2370 if (priv->proto_version == ALPS_PROTO_V7)
2371 clear_bit(INPUT_PROP_SEMI_MT, dev1->propbit);
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002372}
2373
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374int alps_init(struct psmouse *psmouse)
2375{
2376 struct alps_data *priv;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05002377 struct input_dev *dev1 = psmouse->dev, *dev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378
Dmitry Torokhovf42649e2007-04-12 01:31:13 -04002379 priv = kzalloc(sizeof(struct alps_data), GFP_KERNEL);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05002380 dev2 = input_allocate_device();
2381 if (!priv || !dev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 goto init_fail;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05002383
2384 priv->dev2 = dev2;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08002385 setup_timer(&priv->timer, alps_flush_packet, (unsigned long)psmouse);
2386
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04002387 psmouse->private = priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388
Seth Forshee25bded72011-11-07 19:53:36 -08002389 psmouse_reset(psmouse);
2390
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002391 if (alps_identify(psmouse, priv) < 0)
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08002392 goto init_fail;
2393
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002394 if (priv->hw_init(psmouse))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 goto init_fail;
2396
Dmitry Torokhov7105d2e2009-12-11 23:54:54 -08002397 /*
2398 * Undo part of setup done for us by psmouse core since touchpad
2399 * is not a relative device.
2400 */
2401 __clear_bit(EV_REL, dev1->evbit);
2402 __clear_bit(REL_X, dev1->relbit);
2403 __clear_bit(REL_Y, dev1->relbit);
2404
2405 /*
2406 * Now set up our capabilities.
2407 */
Jiri Slaby7b19ada2007-10-18 23:40:32 -07002408 dev1->evbit[BIT_WORD(EV_KEY)] |= BIT_MASK(EV_KEY);
2409 dev1->keybit[BIT_WORD(BTN_TOUCH)] |= BIT_MASK(BTN_TOUCH);
2410 dev1->keybit[BIT_WORD(BTN_TOOL_FINGER)] |= BIT_MASK(BTN_TOOL_FINGER);
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08002411 dev1->keybit[BIT_WORD(BTN_LEFT)] |=
2412 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413
Jiri Slaby7b19ada2007-10-18 23:40:32 -07002414 dev1->evbit[BIT_WORD(EV_ABS)] |= BIT_MASK(EV_ABS);
Seth Forshee25bded72011-11-07 19:53:36 -08002415
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002416 priv->set_abs_params(priv, dev1);
Yunkang Tang38088432014-07-26 13:51:41 -07002417 /* No pressure on V7 */
2418 if (priv->proto_version != ALPS_PROTO_V7)
2419 input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002421 if (priv->flags & ALPS_WHEEL) {
Jiri Slaby7b19ada2007-10-18 23:40:32 -07002422 dev1->evbit[BIT_WORD(EV_REL)] |= BIT_MASK(EV_REL);
2423 dev1->relbit[BIT_WORD(REL_WHEEL)] |= BIT_MASK(REL_WHEEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 }
2425
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002426 if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
Jiri Slaby7b19ada2007-10-18 23:40:32 -07002427 dev1->keybit[BIT_WORD(BTN_FORWARD)] |= BIT_MASK(BTN_FORWARD);
2428 dev1->keybit[BIT_WORD(BTN_BACK)] |= BIT_MASK(BTN_BACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 }
2430
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002431 if (priv->flags & ALPS_FOUR_BUTTONS) {
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08002432 dev1->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_0);
2433 dev1->keybit[BIT_WORD(BTN_1)] |= BIT_MASK(BTN_1);
2434 dev1->keybit[BIT_WORD(BTN_2)] |= BIT_MASK(BTN_2);
2435 dev1->keybit[BIT_WORD(BTN_3)] |= BIT_MASK(BTN_3);
Yunkang Tang38088432014-07-26 13:51:41 -07002436 } else if (priv->flags & ALPS_BUTTONPAD) {
2437 set_bit(INPUT_PROP_BUTTONPAD, dev1->propbit);
2438 clear_bit(BTN_RIGHT, dev1->keybit);
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08002439 } else {
2440 dev1->keybit[BIT_WORD(BTN_MIDDLE)] |= BIT_MASK(BTN_MIDDLE);
2441 }
2442
Pali Rohárdfba8602015-01-09 12:48:44 -08002443 if (priv->flags & ALPS_DUALPOINT) {
2444 /*
2445 * format of input device name is: "protocol vendor name"
2446 * see function psmouse_switch_protocol() in psmouse-base.c
2447 */
2448 dev2->name = "AlpsPS/2 ALPS DualPoint Stick";
2449 dev2->id.product = PSMOUSE_ALPS;
2450 dev2->id.version = priv->proto_version;
2451 } else {
2452 dev2->name = "PS/2 ALPS Mouse";
2453 dev2->id.product = PSMOUSE_PS2;
2454 dev2->id.version = 0x0000;
2455 }
2456
Dmitry Torokhov08ffce42006-06-26 01:45:10 -04002457 snprintf(priv->phys, sizeof(priv->phys), "%s/input1", psmouse->ps2dev.serio->phys);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05002458 dev2->phys = priv->phys;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05002459 dev2->id.bustype = BUS_I8042;
2460 dev2->id.vendor = 0x0002;
Dmitry Torokhov1db3a342008-03-18 00:29:18 -04002461 dev2->dev.parent = &psmouse->ps2dev.serio->dev;
Dmitry Torokhov968ac842005-05-29 02:28:29 -05002462
Jiri Slaby7b19ada2007-10-18 23:40:32 -07002463 dev2->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08002464 dev2->relbit[BIT_WORD(REL_X)] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
2465 dev2->keybit[BIT_WORD(BTN_LEFT)] =
2466 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_MIDDLE) | BIT_MASK(BTN_RIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467
Hans de Goede01d4cd52014-09-08 14:44:05 -07002468 __set_bit(INPUT_PROP_POINTER, dev2->propbit);
Hans de Goede76113922014-09-08 14:42:12 -07002469 if (priv->flags & ALPS_DUALPOINT)
2470 __set_bit(INPUT_PROP_POINTING_STICK, dev2->propbit);
2471
Dmitry Torokhovf42649e2007-04-12 01:31:13 -04002472 if (input_register_device(priv->dev2))
2473 goto init_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474
2475 psmouse->protocol_handler = alps_process_byte;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05002476 psmouse->poll = alps_poll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 psmouse->disconnect = alps_disconnect;
2478 psmouse->reconnect = alps_reconnect;
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002479 psmouse->pktsize = priv->proto_version == ALPS_PROTO_V4 ? 8 : 6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05002481 /* We are having trouble resyncing ALPS touchpads so disable it for now */
2482 psmouse->resync_time = 0;
2483
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 return 0;
2485
2486init_fail:
Dmitry Torokhovf42649e2007-04-12 01:31:13 -04002487 psmouse_reset(psmouse);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05002488 input_free_device(dev2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 kfree(priv);
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04002490 psmouse->private = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 return -1;
2492}
2493
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07002494int alps_detect(struct psmouse *psmouse, bool set_properties)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495{
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002496 struct alps_data dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002498 if (alps_identify(psmouse, &dummy) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 return -1;
2500
2501 if (set_properties) {
2502 psmouse->vendor = "ALPS";
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002503 psmouse->name = dummy.flags & ALPS_DUALPOINT ?
Dmitry Torokhov968ac842005-05-29 02:28:29 -05002504 "DualPoint TouchPad" : "GlidePoint";
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002505 psmouse->model = dummy.proto_version << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 }
2507 return 0;
2508}
2509