blob: 5b35f4fc4d2f7915154c762be209a6f968e75a76 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * ALPS touchpad PS/2 mouse driver
3 *
4 * Copyright (c) 2003 Neil Brown <neilb@cse.unsw.edu.au>
Peter Osterlund963f6262005-07-11 01:08:04 -05005 * Copyright (c) 2003-2005 Peter Osterlund <petero2@telia.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Copyright (c) 2004 Dmitry Torokhov <dtor@mail.ru>
7 * Copyright (c) 2005 Vojtech Pavlik <vojtech@suse.cz>
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08008 * Copyright (c) 2009 Sebastian Kapfer <sebastian_kapfer@gmx.net>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
10 * ALPS detection, tap switching and status querying info is taken from
11 * tpconfig utility (by C. Scott Ananian and Bruce Kall).
12 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License version 2 as published by
15 * the Free Software Foundation.
16 */
17
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/input.h>
Seth Forshee01ce6612011-11-07 19:54:13 -080020#include <linux/input/mt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/serio.h>
22#include <linux/libps2.h>
23
24#include "psmouse.h"
25#include "alps.h"
26
Seth Forshee25bded72011-11-07 19:53:36 -080027/*
28 * Definitions for ALPS version 3 and 4 command mode protocol
29 */
30#define ALPS_CMD_NIBBLE_10 0x01f2
31
Kevin Cernekeecd401202013-02-13 22:28:07 -080032#define ALPS_REG_BASE_RUSHMORE 0xc2c0
33#define ALPS_REG_BASE_PINNACLE 0x0000
34
Seth Forshee25bded72011-11-07 19:53:36 -080035static const struct alps_nibble_commands alps_v3_nibble_commands[] = {
36 { PSMOUSE_CMD_SETPOLL, 0x00 }, /* 0 */
37 { PSMOUSE_CMD_RESET_DIS, 0x00 }, /* 1 */
38 { PSMOUSE_CMD_SETSCALE21, 0x00 }, /* 2 */
39 { PSMOUSE_CMD_SETRATE, 0x0a }, /* 3 */
40 { PSMOUSE_CMD_SETRATE, 0x14 }, /* 4 */
41 { PSMOUSE_CMD_SETRATE, 0x28 }, /* 5 */
42 { PSMOUSE_CMD_SETRATE, 0x3c }, /* 6 */
43 { PSMOUSE_CMD_SETRATE, 0x50 }, /* 7 */
44 { PSMOUSE_CMD_SETRATE, 0x64 }, /* 8 */
45 { PSMOUSE_CMD_SETRATE, 0xc8 }, /* 9 */
46 { ALPS_CMD_NIBBLE_10, 0x00 }, /* a */
47 { PSMOUSE_CMD_SETRES, 0x00 }, /* b */
48 { PSMOUSE_CMD_SETRES, 0x01 }, /* c */
49 { PSMOUSE_CMD_SETRES, 0x02 }, /* d */
50 { PSMOUSE_CMD_SETRES, 0x03 }, /* e */
51 { PSMOUSE_CMD_SETSCALE11, 0x00 }, /* f */
52};
53
54static const struct alps_nibble_commands alps_v4_nibble_commands[] = {
55 { PSMOUSE_CMD_ENABLE, 0x00 }, /* 0 */
56 { PSMOUSE_CMD_RESET_DIS, 0x00 }, /* 1 */
57 { PSMOUSE_CMD_SETSCALE21, 0x00 }, /* 2 */
58 { PSMOUSE_CMD_SETRATE, 0x0a }, /* 3 */
59 { PSMOUSE_CMD_SETRATE, 0x14 }, /* 4 */
60 { PSMOUSE_CMD_SETRATE, 0x28 }, /* 5 */
61 { PSMOUSE_CMD_SETRATE, 0x3c }, /* 6 */
62 { PSMOUSE_CMD_SETRATE, 0x50 }, /* 7 */
63 { PSMOUSE_CMD_SETRATE, 0x64 }, /* 8 */
64 { PSMOUSE_CMD_SETRATE, 0xc8 }, /* 9 */
65 { ALPS_CMD_NIBBLE_10, 0x00 }, /* a */
66 { PSMOUSE_CMD_SETRES, 0x00 }, /* b */
67 { PSMOUSE_CMD_SETRES, 0x01 }, /* c */
68 { PSMOUSE_CMD_SETRES, 0x02 }, /* d */
69 { PSMOUSE_CMD_SETRES, 0x03 }, /* e */
70 { PSMOUSE_CMD_SETSCALE11, 0x00 }, /* f */
71};
72
Yunkang Tang95f75e92013-12-01 22:33:52 -080073static const struct alps_nibble_commands alps_v6_nibble_commands[] = {
74 { PSMOUSE_CMD_ENABLE, 0x00 }, /* 0 */
75 { PSMOUSE_CMD_SETRATE, 0x0a }, /* 1 */
76 { PSMOUSE_CMD_SETRATE, 0x14 }, /* 2 */
77 { PSMOUSE_CMD_SETRATE, 0x28 }, /* 3 */
78 { PSMOUSE_CMD_SETRATE, 0x3c }, /* 4 */
79 { PSMOUSE_CMD_SETRATE, 0x50 }, /* 5 */
80 { PSMOUSE_CMD_SETRATE, 0x64 }, /* 6 */
81 { PSMOUSE_CMD_SETRATE, 0xc8 }, /* 7 */
82 { PSMOUSE_CMD_GETID, 0x00 }, /* 8 */
83 { PSMOUSE_CMD_GETINFO, 0x00 }, /* 9 */
84 { PSMOUSE_CMD_SETRES, 0x00 }, /* a */
85 { PSMOUSE_CMD_SETRES, 0x01 }, /* b */
86 { PSMOUSE_CMD_SETRES, 0x02 }, /* c */
87 { PSMOUSE_CMD_SETRES, 0x03 }, /* d */
88 { PSMOUSE_CMD_SETSCALE21, 0x00 }, /* e */
89 { PSMOUSE_CMD_SETSCALE11, 0x00 }, /* f */
90};
91
Seth Forshee25bded72011-11-07 19:53:36 -080092
Maxim Levitsky71bb21b2009-11-16 22:12:22 -080093#define ALPS_DUALPOINT 0x02 /* touchpad has trackstick */
94#define ALPS_PASS 0x04 /* device has a pass-through port */
95
96#define ALPS_WHEEL 0x08 /* hardware wheel present */
97#define ALPS_FW_BK_1 0x10 /* front & back buttons present */
98#define ALPS_FW_BK_2 0x20 /* front & back buttons present */
99#define ALPS_FOUR_BUTTONS 0x40 /* 4 direction button present */
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800100#define ALPS_PS2_INTERLEAVED 0x80 /* 3-byte PS/2 packet interleaved with
101 6-byte ALPS packet */
Hans de Goede40e8f532014-07-25 22:37:15 -0700102#define ALPS_IS_RUSHMORE 0x100 /* device is a rushmore */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Helge Dellere38de672006-09-10 21:54:39 -0400104static const struct alps_model_info alps_model_data[] = {
Seth Forshee25bded72011-11-07 19:53:36 -0800105 { { 0x32, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT }, /* Toshiba Salellite Pro M10 */
106 { { 0x33, 0x02, 0x0a }, 0x00, ALPS_PROTO_V1, 0x88, 0xf8, 0 }, /* UMAX-530T */
107 { { 0x53, 0x02, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
108 { { 0x53, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
109 { { 0x60, 0x03, 0xc8 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 }, /* HP ze1115 */
110 { { 0x63, 0x02, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
111 { { 0x63, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
112 { { 0x63, 0x02, 0x28 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 }, /* Fujitsu Siemens S6010 */
113 { { 0x63, 0x02, 0x3c }, 0x00, ALPS_PROTO_V2, 0x8f, 0x8f, ALPS_WHEEL }, /* Toshiba Satellite S2400-103 */
114 { { 0x63, 0x02, 0x50 }, 0x00, ALPS_PROTO_V2, 0xef, 0xef, ALPS_FW_BK_1 }, /* NEC Versa L320 */
115 { { 0x63, 0x02, 0x64 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
116 { { 0x63, 0x03, 0xc8 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT }, /* Dell Latitude D800 */
117 { { 0x73, 0x00, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_DUALPOINT }, /* ThinkPad R61 8918-5QG */
118 { { 0x73, 0x02, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
119 { { 0x73, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 }, /* Ahtec Laptop */
120 { { 0x20, 0x02, 0x0e }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT }, /* XXX */
121 { { 0x22, 0x02, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT },
122 { { 0x22, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xff, 0xff, ALPS_PASS | ALPS_DUALPOINT }, /* Dell Latitude D600 */
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800123 /* Dell Latitude E5500, E6400, E6500, Precision M4400 */
Seth Forshee25bded72011-11-07 19:53:36 -0800124 { { 0x62, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xcf, 0xcf,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800125 ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED },
Yunkang Tang95f75e92013-12-01 22:33:52 -0800126 { { 0x73, 0x00, 0x14 }, 0x00, ALPS_PROTO_V6, 0xff, 0xff, ALPS_DUALPOINT }, /* Dell XT2 */
Seth Forshee25bded72011-11-07 19:53:36 -0800127 { { 0x73, 0x02, 0x50 }, 0x00, ALPS_PROTO_V2, 0xcf, 0xcf, ALPS_FOUR_BUTTONS }, /* Dell Vostro 1400 */
128 { { 0x52, 0x01, 0x14 }, 0x00, ALPS_PROTO_V2, 0xff, 0xff,
129 ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED }, /* Toshiba Tecra A11-11L */
Seth Forshee25bded72011-11-07 19:53:36 -0800130 { { 0x73, 0x02, 0x64 }, 0x8a, ALPS_PROTO_V4, 0x8f, 0x8f, 0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131};
132
Kevin Cernekee24af5cb2013-02-13 22:22:08 -0800133static void alps_set_abs_params_st(struct alps_data *priv,
134 struct input_dev *dev1);
135static void alps_set_abs_params_mt(struct alps_data *priv,
136 struct input_dev *dev1);
137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138/*
139 * XXX - this entry is suspicious. First byte has zero lower nibble,
140 * which is what a normal mouse would report. Also, the value 0x0e
141 * isn't valid per PS/2 spec.
142 */
143
Seth Forsheed4b347b2011-11-07 19:53:15 -0800144/* Packet formats are described in Documentation/input/alps.txt */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800146static bool alps_is_valid_first_byte(struct alps_data *priv,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800147 unsigned char data)
148{
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800149 return (data & priv->mask0) == priv->byte0;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800150}
151
152static void alps_report_buttons(struct psmouse *psmouse,
153 struct input_dev *dev1, struct input_dev *dev2,
154 int left, int right, int middle)
155{
Martin Buckc91ed052010-03-13 22:23:58 -0800156 struct input_dev *dev;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800157
Martin Buckc91ed052010-03-13 22:23:58 -0800158 /*
159 * If shared button has already been reported on the
160 * other device (dev2) then this event should be also
161 * sent through that device.
162 */
163 dev = test_bit(BTN_LEFT, dev2->key) ? dev2 : dev1;
164 input_report_key(dev, BTN_LEFT, left);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800165
Martin Buckc91ed052010-03-13 22:23:58 -0800166 dev = test_bit(BTN_RIGHT, dev2->key) ? dev2 : dev1;
167 input_report_key(dev, BTN_RIGHT, right);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800168
Martin Buckc91ed052010-03-13 22:23:58 -0800169 dev = test_bit(BTN_MIDDLE, dev2->key) ? dev2 : dev1;
170 input_report_key(dev, BTN_MIDDLE, middle);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800171
Martin Buckc91ed052010-03-13 22:23:58 -0800172 /*
173 * Sync the _other_ device now, we'll do the first
174 * device later once we report the rest of the events.
175 */
176 input_sync(dev2);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800177}
178
Seth Forshee25bded72011-11-07 19:53:36 -0800179static void alps_process_packet_v1_v2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
181 struct alps_data *priv = psmouse->private;
182 unsigned char *packet = psmouse->packet;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500183 struct input_dev *dev = psmouse->dev;
184 struct input_dev *dev2 = priv->dev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 int x, y, z, ges, fin, left, right, middle;
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500186 int back = 0, forward = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800188 if (priv->proto_version == ALPS_PROTO_V1) {
Yotam Medinid2f40122006-05-29 23:30:36 -0400189 left = packet[2] & 0x10;
190 right = packet[2] & 0x08;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 middle = 0;
192 x = packet[1] | ((packet[0] & 0x07) << 7);
193 y = packet[4] | ((packet[3] & 0x07) << 7);
194 z = packet[5];
195 } else {
196 left = packet[3] & 1;
197 right = packet[3] & 2;
198 middle = packet[3] & 4;
199 x = packet[1] | ((packet[2] & 0x78) << (7 - 3));
200 y = packet[4] | ((packet[3] & 0x70) << (7 - 4));
201 z = packet[5];
202 }
203
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800204 if (priv->flags & ALPS_FW_BK_1) {
Laszlo Kajan3c00bb92008-03-18 00:39:55 -0400205 back = packet[0] & 0x10;
206 forward = packet[2] & 4;
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500207 }
208
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800209 if (priv->flags & ALPS_FW_BK_2) {
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500210 back = packet[3] & 4;
211 forward = packet[2] & 4;
212 if ((middle = forward && back))
213 forward = back = 0;
214 }
215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 ges = packet[2] & 1;
217 fin = packet[2] & 2;
218
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800219 if ((priv->flags & ALPS_DUALPOINT) && z == 127) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 input_report_rel(dev2, REL_X, (x > 383 ? (x - 768) : x));
221 input_report_rel(dev2, REL_Y, -(y > 255 ? (y - 512) : y));
Ulrich Dangeld7ed5d82009-06-11 00:15:09 -0700222
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800223 alps_report_buttons(psmouse, dev2, dev, left, right, middle);
Ulrich Dangeld7ed5d82009-06-11 00:15:09 -0700224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 input_sync(dev2);
226 return;
227 }
228
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800229 alps_report_buttons(psmouse, dev, dev2, left, right, middle);
Ulrich Dangeld7ed5d82009-06-11 00:15:09 -0700230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 /* Convert hardware tap to a reasonable Z value */
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800232 if (ges && !fin)
233 z = 40;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
235 /*
236 * A "tap and drag" operation is reported by the hardware as a transition
237 * from (!fin && ges) to (fin && ges). This should be translated to the
238 * sequence Z>0, Z==0, Z>0, so the Z==0 event has to be generated manually.
239 */
240 if (ges && fin && !priv->prev_fin) {
241 input_report_abs(dev, ABS_X, x);
242 input_report_abs(dev, ABS_Y, y);
243 input_report_abs(dev, ABS_PRESSURE, 0);
244 input_report_key(dev, BTN_TOOL_FINGER, 0);
245 input_sync(dev);
246 }
247 priv->prev_fin = fin;
248
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800249 if (z > 30)
250 input_report_key(dev, BTN_TOUCH, 1);
251 if (z < 25)
252 input_report_key(dev, BTN_TOUCH, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254 if (z > 0) {
255 input_report_abs(dev, ABS_X, x);
256 input_report_abs(dev, ABS_Y, y);
257 }
258
259 input_report_abs(dev, ABS_PRESSURE, z);
260 input_report_key(dev, BTN_TOOL_FINGER, z > 0);
261
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800262 if (priv->flags & ALPS_WHEEL)
Vojtech Pavlike6c047b2005-09-04 01:40:43 -0500263 input_report_rel(dev, REL_WHEEL, ((packet[2] << 1) & 0x08) - ((packet[0] >> 4) & 0x07));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800265 if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500266 input_report_key(dev, BTN_FORWARD, forward);
267 input_report_key(dev, BTN_BACK, back);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 }
269
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800270 if (priv->flags & ALPS_FOUR_BUTTONS) {
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800271 input_report_key(dev, BTN_0, packet[2] & 4);
272 input_report_key(dev, BTN_1, packet[0] & 0x10);
273 input_report_key(dev, BTN_2, packet[3] & 4);
274 input_report_key(dev, BTN_3, packet[0] & 0x20);
275 }
276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 input_sync(dev);
278}
279
Seth Forshee01ce6612011-11-07 19:54:13 -0800280/*
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800281 * Process bitmap data for V5 protocols. Return value is null.
282 *
283 * The bitmaps don't have enough data to track fingers, so this function
284 * only generates points representing a bounding box of at most two contacts.
285 * These two points are returned in x1, y1, x2, and y2.
286 */
287static void alps_process_bitmap_dolphin(struct alps_data *priv,
288 struct alps_fields *fields,
289 int *x1, int *y1, int *x2, int *y2)
290{
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
312 *x1 = *y1 = *x2 = *y2 = 0;
313
314 if (fields->fingers > 1) {
315 start_bit = priv->x_bits - x_msb;
316 end_bit = priv->x_bits - x_lsb;
317 box_middle_x = (priv->x_max * (start_bit + end_bit)) /
318 (2 * (priv->x_bits - 1));
319
320 start_bit = y_lsb - 1;
321 end_bit = y_msb - 1;
322 box_middle_y = (priv->y_max * (start_bit + end_bit)) /
323 (2 * (priv->y_bits - 1));
324 *x1 = fields->x;
325 *y1 = fields->y;
326 *x2 = 2 * box_middle_x - *x1;
327 *y2 = 2 * box_middle_y - *y1;
328 }
329}
330
Hans de Goede036e6c72014-07-25 22:38:51 -0700331static void alps_get_bitmap_points(unsigned int map,
332 struct alps_bitmap_point *low,
333 struct alps_bitmap_point *high,
334 int *fingers)
335{
336 struct alps_bitmap_point *point;
337 int i, bit, prev_bit = 0;
338
339 point = low;
340 for (i = 0; map != 0; i++, map >>= 1) {
341 bit = map & 1;
342 if (bit) {
343 if (!prev_bit) {
344 point->start_bit = i;
Hans de Goede105affb2014-07-25 22:41:51 -0700345 point->num_bits = 0;
Hans de Goede036e6c72014-07-25 22:38:51 -0700346 (*fingers)++;
347 }
348 point->num_bits++;
349 } else {
350 if (prev_bit)
351 point = high;
Hans de Goede036e6c72014-07-25 22:38:51 -0700352 }
353 prev_bit = bit;
354 }
355}
356
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800357/*
Seth Forshee01ce6612011-11-07 19:54:13 -0800358 * Process bitmap data from v3 and v4 protocols. Returns the number of
359 * fingers detected. A return value of 0 means at least one of the
360 * bitmaps was empty.
361 *
362 * The bitmaps don't have enough data to track fingers, so this function
363 * only generates points representing a bounding box of all contacts.
364 * These points are returned in x1, y1, x2, and y2 when the return value
365 * is greater than 0.
366 */
Kevin Cernekee7a9f73e2013-02-13 22:24:55 -0800367static int alps_process_bitmap(struct alps_data *priv,
368 unsigned int x_map, unsigned int y_map,
Seth Forshee01ce6612011-11-07 19:54:13 -0800369 int *x1, int *y1, int *x2, int *y2)
370{
Hans de Goede036e6c72014-07-25 22:38:51 -0700371 int i, fingers_x = 0, fingers_y = 0, fingers;
Seth Forshee01ce6612011-11-07 19:54:13 -0800372 struct alps_bitmap_point x_low = {0,}, x_high = {0,};
373 struct alps_bitmap_point y_low = {0,}, y_high = {0,};
Seth Forshee01ce6612011-11-07 19:54:13 -0800374
375 if (!x_map || !y_map)
376 return 0;
377
378 *x1 = *y1 = *x2 = *y2 = 0;
379
Hans de Goede036e6c72014-07-25 22:38:51 -0700380 alps_get_bitmap_points(x_map, &x_low, &x_high, &fingers_x);
381 alps_get_bitmap_points(y_map, &y_low, &y_high, &fingers_y);
Seth Forshee01ce6612011-11-07 19:54:13 -0800382
383 /*
384 * Fingers can overlap, so we use the maximum count of fingers
385 * on either axis as the finger count.
386 */
387 fingers = max(fingers_x, fingers_y);
388
389 /*
Hans de Goede20bea682014-07-25 22:33:33 -0700390 * If an axis reports only a single contact, we have overlapping or
391 * adjacent fingers. Divide the single contact between the two points.
Seth Forshee01ce6612011-11-07 19:54:13 -0800392 */
Hans de Goede20bea682014-07-25 22:33:33 -0700393 if (fingers_x == 1) {
394 i = x_low.num_bits / 2;
395 x_low.num_bits = x_low.num_bits - i;
396 x_high.start_bit = x_low.start_bit + i;
397 x_high.num_bits = max(i, 1);
398 }
399 if (fingers_y == 1) {
400 i = y_low.num_bits / 2;
401 y_low.num_bits = y_low.num_bits - i;
402 y_high.start_bit = y_low.start_bit + i;
403 y_high.num_bits = max(i, 1);
Seth Forshee01ce6612011-11-07 19:54:13 -0800404 }
405
Kevin Cernekee7a9f73e2013-02-13 22:24:55 -0800406 *x1 = (priv->x_max * (2 * x_low.start_bit + x_low.num_bits - 1)) /
407 (2 * (priv->x_bits - 1));
408 *y1 = (priv->y_max * (2 * y_low.start_bit + y_low.num_bits - 1)) /
409 (2 * (priv->y_bits - 1));
Seth Forshee01ce6612011-11-07 19:54:13 -0800410
Hans de Goede20bea682014-07-25 22:33:33 -0700411 *x2 = (priv->x_max *
412 (2 * x_high.start_bit + x_high.num_bits - 1)) /
413 (2 * (priv->x_bits - 1));
414 *y2 = (priv->y_max *
415 (2 * y_high.start_bit + y_high.num_bits - 1)) /
416 (2 * (priv->y_bits - 1));
Seth Forshee01ce6612011-11-07 19:54:13 -0800417
Hans de Goede40e8f532014-07-25 22:37:15 -0700418 /* y-bitmap order is reversed, except on rushmore */
419 if (!(priv->flags & ALPS_IS_RUSHMORE)) {
420 *y1 = priv->y_max - *y1;
421 *y2 = priv->y_max - *y2;
422 }
423
Seth Forshee01ce6612011-11-07 19:54:13 -0800424 return fingers;
425}
426
427static void alps_set_slot(struct input_dev *dev, int slot, bool active,
428 int x, int y)
429{
430 input_mt_slot(dev, slot);
431 input_mt_report_slot_state(dev, MT_TOOL_FINGER, active);
432 if (active) {
433 input_report_abs(dev, ABS_MT_POSITION_X, x);
434 input_report_abs(dev, ABS_MT_POSITION_Y, y);
435 }
436}
437
438static void alps_report_semi_mt_data(struct input_dev *dev, int num_fingers,
439 int x1, int y1, int x2, int y2)
440{
441 alps_set_slot(dev, 0, num_fingers != 0, x1, y1);
442 alps_set_slot(dev, 1, num_fingers == 2, x2, y2);
443}
444
Seth Forshee25bded72011-11-07 19:53:36 -0800445static void alps_process_trackstick_packet_v3(struct psmouse *psmouse)
446{
447 struct alps_data *priv = psmouse->private;
448 unsigned char *packet = psmouse->packet;
449 struct input_dev *dev = priv->dev2;
450 int x, y, z, left, right, middle;
451
452 /* Sanity check packet */
453 if (!(packet[0] & 0x40)) {
454 psmouse_dbg(psmouse, "Bad trackstick packet, discarding\n");
455 return;
456 }
457
458 /*
459 * There's a special packet that seems to indicate the end
460 * of a stream of trackstick data. Filter these out.
461 */
462 if (packet[1] == 0x7f && packet[2] == 0x7f && packet[4] == 0x7f)
463 return;
464
465 x = (s8)(((packet[0] & 0x20) << 2) | (packet[1] & 0x7f));
466 y = (s8)(((packet[0] & 0x10) << 3) | (packet[2] & 0x7f));
467 z = (packet[4] & 0x7c) >> 2;
468
469 /*
470 * The x and y values tend to be quite large, and when used
471 * alone the trackstick is difficult to use. Scale them down
472 * to compensate.
473 */
474 x /= 8;
475 y /= 8;
476
477 input_report_rel(dev, REL_X, x);
478 input_report_rel(dev, REL_Y, -y);
479
480 /*
481 * Most ALPS models report the trackstick buttons in the touchpad
482 * packets, but a few report them here. No reliable way has been
483 * found to differentiate between the models upfront, so we enable
484 * the quirk in response to seeing a button press in the trackstick
485 * packet.
486 */
487 left = packet[3] & 0x01;
488 right = packet[3] & 0x02;
489 middle = packet[3] & 0x04;
490
491 if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) &&
492 (left || right || middle))
493 priv->quirks |= ALPS_QUIRK_TRACKSTICK_BUTTONS;
494
495 if (priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) {
496 input_report_key(dev, BTN_LEFT, left);
497 input_report_key(dev, BTN_RIGHT, right);
498 input_report_key(dev, BTN_MIDDLE, middle);
499 }
500
501 input_sync(dev);
502 return;
503}
504
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800505static void alps_decode_buttons_v3(struct alps_fields *f, unsigned char *p)
506{
507 f->left = !!(p[3] & 0x01);
508 f->right = !!(p[3] & 0x02);
509 f->middle = !!(p[3] & 0x04);
510
511 f->ts_left = !!(p[3] & 0x10);
512 f->ts_right = !!(p[3] & 0x20);
513 f->ts_middle = !!(p[3] & 0x40);
514}
515
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800516static void alps_decode_pinnacle(struct alps_fields *f, unsigned char *p,
517 struct psmouse *psmouse)
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800518{
519 f->first_mp = !!(p[4] & 0x40);
520 f->is_mp = !!(p[0] & 0x40);
521
522 f->fingers = (p[5] & 0x3) + 1;
523 f->x_map = ((p[4] & 0x7e) << 8) |
524 ((p[1] & 0x7f) << 2) |
525 ((p[0] & 0x30) >> 4);
526 f->y_map = ((p[3] & 0x70) << 4) |
527 ((p[2] & 0x7f) << 1) |
528 (p[4] & 0x01);
529
530 f->x = ((p[1] & 0x7f) << 4) | ((p[4] & 0x30) >> 2) |
531 ((p[0] & 0x30) >> 4);
532 f->y = ((p[2] & 0x7f) << 4) | (p[4] & 0x0f);
533 f->z = p[5] & 0x7f;
534
535 alps_decode_buttons_v3(f, p);
536}
537
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800538static void alps_decode_rushmore(struct alps_fields *f, unsigned char *p,
539 struct psmouse *psmouse)
Kevin Cernekee1302bac2013-02-13 22:27:08 -0800540{
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800541 alps_decode_pinnacle(f, p, psmouse);
Kevin Cernekee1302bac2013-02-13 22:27:08 -0800542
Yunkang Tangf105e342014-07-25 22:29:24 -0700543 /* Rushmore's packet decode has a bit difference with Pinnacle's */
544 f->is_mp = !!(p[5] & 0x40);
545 f->fingers = max((p[5] & 0x3), ((p[5] >> 2) & 0x3)) + 1;
Kevin Cernekee1302bac2013-02-13 22:27:08 -0800546 f->x_map |= (p[5] & 0x10) << 11;
547 f->y_map |= (p[5] & 0x20) << 6;
548}
549
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800550static void alps_decode_dolphin(struct alps_fields *f, unsigned char *p,
551 struct psmouse *psmouse)
Dave Turvene75af9e52013-02-21 22:58:28 -0800552{
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800553 u64 palm_data = 0;
554 struct alps_data *priv = psmouse->private;
555
Dave Turvene75af9e52013-02-21 22:58:28 -0800556 f->first_mp = !!(p[0] & 0x02);
557 f->is_mp = !!(p[0] & 0x20);
558
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800559 if (!f->is_mp) {
560 f->x = ((p[1] & 0x7f) | ((p[4] & 0x0f) << 7));
561 f->y = ((p[2] & 0x7f) | ((p[4] & 0xf0) << 3));
562 f->z = (p[0] & 4) ? 0 : p[5] & 0x7f;
563 alps_decode_buttons_v3(f, p);
564 } else {
565 f->fingers = ((p[0] & 0x6) >> 1 |
Dave Turvene75af9e52013-02-21 22:58:28 -0800566 (p[0] & 0x10) >> 2);
Dave Turvene75af9e52013-02-21 22:58:28 -0800567
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800568 palm_data = (p[1] & 0x7f) |
569 ((p[2] & 0x7f) << 7) |
570 ((p[4] & 0x7f) << 14) |
571 ((p[5] & 0x7f) << 21) |
572 ((p[3] & 0x07) << 28) |
573 (((u64)p[3] & 0x70) << 27) |
574 (((u64)p[0] & 0x01) << 34);
Dave Turvene75af9e52013-02-21 22:58:28 -0800575
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800576 /* Y-profile is stored in P(0) to p(n-1), n = y_bits; */
577 f->y_map = palm_data & (BIT(priv->y_bits) - 1);
578
579 /* X-profile is stored in p(n) to p(n+m-1), m = x_bits; */
580 f->x_map = (palm_data >> priv->y_bits) &
581 (BIT(priv->x_bits) - 1);
582 }
Dave Turvene75af9e52013-02-21 22:58:28 -0800583}
584
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800585static void alps_process_touchpad_packet_v3_v5(struct psmouse *psmouse)
Seth Forshee25bded72011-11-07 19:53:36 -0800586{
587 struct alps_data *priv = psmouse->private;
588 unsigned char *packet = psmouse->packet;
589 struct input_dev *dev = psmouse->dev;
590 struct input_dev *dev2 = priv->dev2;
Hans de Goede20bea682014-07-25 22:33:33 -0700591 int x1 = 0, y1 = 0, x2 = 0, y2 = 0, fingers = 0;
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800592 struct alps_fields f = {0};
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800593
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800594 priv->decode_fields(&f, packet, psmouse);
Seth Forshee25bded72011-11-07 19:53:36 -0800595
596 /*
Seth Forshee01ce6612011-11-07 19:54:13 -0800597 * There's no single feature of touchpad position and bitmap packets
598 * that can be used to distinguish between them. We rely on the fact
599 * that a bitmap packet should always follow a position packet with
600 * bit 6 of packet[4] set.
Seth Forshee25bded72011-11-07 19:53:36 -0800601 */
602 if (priv->multi_packet) {
Seth Forshee25bded72011-11-07 19:53:36 -0800603 /*
604 * Sometimes a position packet will indicate a multi-packet
605 * sequence, but then what follows is another position
606 * packet. Check for this, and when it happens process the
607 * position packet as usual.
608 */
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800609 if (f.is_mp) {
610 fingers = f.fingers;
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800611 if (priv->proto_version == ALPS_PROTO_V3) {
Hans de Goede20bea682014-07-25 22:33:33 -0700612 if (alps_process_bitmap(priv, f.x_map,
613 f.y_map, &x1, &y1,
614 &x2, &y2) == 0)
615 fingers = 0; /* Use st data */
Seth Forshee01ce6612011-11-07 19:54:13 -0800616
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800617 /* Now process position packet */
618 priv->decode_fields(&f, priv->multi_data,
619 psmouse);
620 } else {
621 /*
622 * Because Dolphin uses position packet's
623 * coordinate data as Pt1 and uses it to
624 * calculate Pt2, so we need to do position
625 * packet decode first.
626 */
627 priv->decode_fields(&f, priv->multi_data,
628 psmouse);
629
630 /*
631 * Since Dolphin's finger number is reliable,
632 * there is no need to compare with bmap_fn.
633 */
634 alps_process_bitmap_dolphin(priv, &f, &x1, &y1,
635 &x2, &y2);
636 }
Seth Forshee01ce6612011-11-07 19:54:13 -0800637 } else {
638 priv->multi_packet = 0;
Seth Forshee25bded72011-11-07 19:53:36 -0800639 }
640 }
641
Seth Forshee01ce6612011-11-07 19:54:13 -0800642 /*
643 * Bit 6 of byte 0 is not usually set in position packets. The only
644 * times it seems to be set is in situations where the data is
645 * suspect anyway, e.g. a palm resting flat on the touchpad. Given
646 * this combined with the fact that this bit is useful for filtering
647 * out misidentified bitmap packets, we reject anything with this
648 * bit set.
649 */
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800650 if (f.is_mp)
Seth Forshee01ce6612011-11-07 19:54:13 -0800651 return;
652
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800653 if (!priv->multi_packet && f.first_mp) {
Seth Forshee25bded72011-11-07 19:53:36 -0800654 priv->multi_packet = 1;
Seth Forshee01ce6612011-11-07 19:54:13 -0800655 memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
656 return;
657 }
658
659 priv->multi_packet = 0;
Seth Forshee25bded72011-11-07 19:53:36 -0800660
Seth Forshee25bded72011-11-07 19:53:36 -0800661 /*
662 * Sometimes the hardware sends a single packet with z = 0
663 * in the middle of a stream. Real releases generate packets
664 * with x, y, and z all zero, so these seem to be flukes.
665 * Ignore them.
666 */
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800667 if (f.x && f.y && !f.z)
Seth Forshee25bded72011-11-07 19:53:36 -0800668 return;
669
Seth Forshee01ce6612011-11-07 19:54:13 -0800670 /*
671 * If we don't have MT data or the bitmaps were empty, we have
672 * to rely on ST data.
673 */
674 if (!fingers) {
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800675 x1 = f.x;
676 y1 = f.y;
677 fingers = f.z > 0 ? 1 : 0;
Seth Forshee01ce6612011-11-07 19:54:13 -0800678 }
679
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800680 if (f.z >= 64)
Seth Forshee25bded72011-11-07 19:53:36 -0800681 input_report_key(dev, BTN_TOUCH, 1);
682 else
683 input_report_key(dev, BTN_TOUCH, 0);
684
Seth Forshee01ce6612011-11-07 19:54:13 -0800685 alps_report_semi_mt_data(dev, fingers, x1, y1, x2, y2);
686
Dmitry Torokhov616575c2012-05-10 22:31:59 -0700687 input_mt_report_finger_count(dev, fingers);
Seth Forshee01ce6612011-11-07 19:54:13 -0800688
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800689 input_report_key(dev, BTN_LEFT, f.left);
690 input_report_key(dev, BTN_RIGHT, f.right);
691 input_report_key(dev, BTN_MIDDLE, f.middle);
Seth Forshee01ce6612011-11-07 19:54:13 -0800692
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800693 if (f.z > 0) {
694 input_report_abs(dev, ABS_X, f.x);
695 input_report_abs(dev, ABS_Y, f.y);
Seth Forshee25bded72011-11-07 19:53:36 -0800696 }
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800697 input_report_abs(dev, ABS_PRESSURE, f.z);
Seth Forshee25bded72011-11-07 19:53:36 -0800698
Seth Forshee25bded72011-11-07 19:53:36 -0800699 input_sync(dev);
700
701 if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS)) {
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800702 input_report_key(dev2, BTN_LEFT, f.ts_left);
703 input_report_key(dev2, BTN_RIGHT, f.ts_right);
704 input_report_key(dev2, BTN_MIDDLE, f.ts_middle);
Seth Forshee25bded72011-11-07 19:53:36 -0800705 input_sync(dev2);
706 }
707}
708
709static void alps_process_packet_v3(struct psmouse *psmouse)
710{
711 unsigned char *packet = psmouse->packet;
712
713 /*
714 * v3 protocol packets come in three types, two representing
715 * touchpad data and one representing trackstick data.
716 * Trackstick packets seem to be distinguished by always
717 * having 0x3f in the last byte. This value has never been
718 * observed in the last byte of either of the other types
719 * of packets.
720 */
721 if (packet[5] == 0x3f) {
722 alps_process_trackstick_packet_v3(psmouse);
723 return;
724 }
725
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800726 alps_process_touchpad_packet_v3_v5(psmouse);
Seth Forshee25bded72011-11-07 19:53:36 -0800727}
728
Yunkang Tang95f75e92013-12-01 22:33:52 -0800729static void alps_process_packet_v6(struct psmouse *psmouse)
730{
731 struct alps_data *priv = psmouse->private;
732 unsigned char *packet = psmouse->packet;
733 struct input_dev *dev = psmouse->dev;
734 struct input_dev *dev2 = priv->dev2;
735 int x, y, z, left, right, middle;
736
737 /*
738 * We can use Byte5 to distinguish if the packet is from Touchpad
739 * or Trackpoint.
740 * Touchpad: 0 - 0x7E
741 * Trackpoint: 0x7F
742 */
743 if (packet[5] == 0x7F) {
744 /* It should be a DualPoint when received Trackpoint packet */
745 if (!(priv->flags & ALPS_DUALPOINT))
746 return;
747
748 /* Trackpoint packet */
749 x = packet[1] | ((packet[3] & 0x20) << 2);
750 y = packet[2] | ((packet[3] & 0x40) << 1);
751 z = packet[4];
752 left = packet[3] & 0x01;
753 right = packet[3] & 0x02;
754 middle = packet[3] & 0x04;
755
756 /* To prevent the cursor jump when finger lifted */
757 if (x == 0x7F && y == 0x7F && z == 0x7F)
758 x = y = z = 0;
759
760 /* Divide 4 since trackpoint's speed is too fast */
761 input_report_rel(dev2, REL_X, (char)x / 4);
762 input_report_rel(dev2, REL_Y, -((char)y / 4));
763
764 input_report_key(dev2, BTN_LEFT, left);
765 input_report_key(dev2, BTN_RIGHT, right);
766 input_report_key(dev2, BTN_MIDDLE, middle);
767
768 input_sync(dev2);
769 return;
770 }
771
772 /* Touchpad packet */
773 x = packet[1] | ((packet[3] & 0x78) << 4);
774 y = packet[2] | ((packet[4] & 0x78) << 4);
775 z = packet[5];
776 left = packet[3] & 0x01;
777 right = packet[3] & 0x02;
778
779 if (z > 30)
780 input_report_key(dev, BTN_TOUCH, 1);
781 if (z < 25)
782 input_report_key(dev, BTN_TOUCH, 0);
783
784 if (z > 0) {
785 input_report_abs(dev, ABS_X, x);
786 input_report_abs(dev, ABS_Y, y);
787 }
788
789 input_report_abs(dev, ABS_PRESSURE, z);
790 input_report_key(dev, BTN_TOOL_FINGER, z > 0);
791
792 /* v6 touchpad does not have middle button */
793 input_report_key(dev, BTN_LEFT, left);
794 input_report_key(dev, BTN_RIGHT, right);
795
796 input_sync(dev);
797}
798
Seth Forshee25bded72011-11-07 19:53:36 -0800799static void alps_process_packet_v4(struct psmouse *psmouse)
800{
George Pantalos3b7e09f2012-05-10 22:31:59 -0700801 struct alps_data *priv = psmouse->private;
Seth Forshee25bded72011-11-07 19:53:36 -0800802 unsigned char *packet = psmouse->packet;
803 struct input_dev *dev = psmouse->dev;
George Pantalos3b7e09f2012-05-10 22:31:59 -0700804 int offset;
Seth Forshee25bded72011-11-07 19:53:36 -0800805 int x, y, z;
806 int left, right;
George Pantalos3b7e09f2012-05-10 22:31:59 -0700807 int x1, y1, x2, y2;
808 int fingers = 0;
809 unsigned int x_bitmap, y_bitmap;
810
811 /*
812 * v4 has a 6-byte encoding for bitmap data, but this data is
813 * broken up between 3 normal packets. Use priv->multi_packet to
814 * track our position in the bitmap packet.
815 */
816 if (packet[6] & 0x40) {
817 /* sync, reset position */
818 priv->multi_packet = 0;
819 }
820
821 if (WARN_ON_ONCE(priv->multi_packet > 2))
822 return;
823
824 offset = 2 * priv->multi_packet;
825 priv->multi_data[offset] = packet[6];
826 priv->multi_data[offset + 1] = packet[7];
827
828 if (++priv->multi_packet > 2) {
829 priv->multi_packet = 0;
830
831 x_bitmap = ((priv->multi_data[2] & 0x1f) << 10) |
832 ((priv->multi_data[3] & 0x60) << 3) |
833 ((priv->multi_data[0] & 0x3f) << 2) |
834 ((priv->multi_data[1] & 0x60) >> 5);
835 y_bitmap = ((priv->multi_data[5] & 0x01) << 10) |
836 ((priv->multi_data[3] & 0x1f) << 5) |
837 (priv->multi_data[1] & 0x1f);
838
Kevin Cernekee7a9f73e2013-02-13 22:24:55 -0800839 fingers = alps_process_bitmap(priv, x_bitmap, y_bitmap,
George Pantalos3b7e09f2012-05-10 22:31:59 -0700840 &x1, &y1, &x2, &y2);
841
842 /* Store MT data.*/
843 priv->fingers = fingers;
844 priv->x1 = x1;
845 priv->x2 = x2;
846 priv->y1 = y1;
847 priv->y2 = y2;
848 }
Seth Forshee25bded72011-11-07 19:53:36 -0800849
850 left = packet[4] & 0x01;
851 right = packet[4] & 0x02;
852
853 x = ((packet[1] & 0x7f) << 4) | ((packet[3] & 0x30) >> 2) |
854 ((packet[0] & 0x30) >> 4);
855 y = ((packet[2] & 0x7f) << 4) | (packet[3] & 0x0f);
856 z = packet[5] & 0x7f;
857
George Pantalos3b7e09f2012-05-10 22:31:59 -0700858 /*
859 * If there were no contacts in the bitmap, use ST
860 * points in MT reports.
861 * If there were two contacts or more, report MT data.
862 */
863 if (priv->fingers < 2) {
864 x1 = x;
865 y1 = y;
866 fingers = z > 0 ? 1 : 0;
867 } else {
868 fingers = priv->fingers;
869 x1 = priv->x1;
870 x2 = priv->x2;
871 y1 = priv->y1;
872 y2 = priv->y2;
873 }
874
Seth Forshee25bded72011-11-07 19:53:36 -0800875 if (z >= 64)
876 input_report_key(dev, BTN_TOUCH, 1);
877 else
878 input_report_key(dev, BTN_TOUCH, 0);
879
George Pantalos3b7e09f2012-05-10 22:31:59 -0700880 alps_report_semi_mt_data(dev, fingers, x1, y1, x2, y2);
881
Dmitry Torokhov616575c2012-05-10 22:31:59 -0700882 input_mt_report_finger_count(dev, fingers);
George Pantalos3b7e09f2012-05-10 22:31:59 -0700883
884 input_report_key(dev, BTN_LEFT, left);
885 input_report_key(dev, BTN_RIGHT, right);
886
Seth Forshee25bded72011-11-07 19:53:36 -0800887 if (z > 0) {
888 input_report_abs(dev, ABS_X, x);
889 input_report_abs(dev, ABS_Y, y);
890 }
891 input_report_abs(dev, ABS_PRESSURE, z);
892
Seth Forshee25bded72011-11-07 19:53:36 -0800893 input_sync(dev);
894}
895
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800896static void alps_report_bare_ps2_packet(struct psmouse *psmouse,
897 unsigned char packet[],
898 bool report_buttons)
899{
900 struct alps_data *priv = psmouse->private;
901 struct input_dev *dev2 = priv->dev2;
902
903 if (report_buttons)
904 alps_report_buttons(psmouse, dev2, psmouse->dev,
905 packet[0] & 1, packet[0] & 2, packet[0] & 4);
906
907 input_report_rel(dev2, REL_X,
908 packet[1] ? packet[1] - ((packet[0] << 4) & 0x100) : 0);
909 input_report_rel(dev2, REL_Y,
910 packet[2] ? ((packet[0] << 3) & 0x100) - packet[2] : 0);
911
912 input_sync(dev2);
913}
914
915static psmouse_ret_t alps_handle_interleaved_ps2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916{
917 struct alps_data *priv = psmouse->private;
918
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800919 if (psmouse->pktcnt < 6)
920 return PSMOUSE_GOOD_DATA;
921
922 if (psmouse->pktcnt == 6) {
923 /*
924 * Start a timer to flush the packet if it ends up last
925 * 6-byte packet in the stream. Timer needs to fire
926 * psmouse core times out itself. 20 ms should be enough
927 * to decide if we are getting more data or not.
928 */
929 mod_timer(&priv->timer, jiffies + msecs_to_jiffies(20));
930 return PSMOUSE_GOOD_DATA;
931 }
932
933 del_timer(&priv->timer);
934
935 if (psmouse->packet[6] & 0x80) {
936
937 /*
938 * Highest bit is set - that means we either had
939 * complete ALPS packet and this is start of the
940 * next packet or we got garbage.
941 */
942
943 if (((psmouse->packet[3] |
944 psmouse->packet[4] |
945 psmouse->packet[5]) & 0x80) ||
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800946 (!alps_is_valid_first_byte(priv, psmouse->packet[6]))) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700947 psmouse_dbg(psmouse,
Andy Shevchenko3b112922012-10-30 00:24:41 -0700948 "refusing packet %4ph (suspected interleaved ps/2)\n",
949 psmouse->packet + 3);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800950 return PSMOUSE_BAD_DATA;
951 }
952
Kevin Cernekee24af5cb2013-02-13 22:22:08 -0800953 priv->process_packet(psmouse);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800954
955 /* Continue with the next packet */
956 psmouse->packet[0] = psmouse->packet[6];
957 psmouse->pktcnt = 1;
958
959 } else {
960
961 /*
962 * High bit is 0 - that means that we indeed got a PS/2
963 * packet in the middle of ALPS packet.
964 *
965 * There is also possibility that we got 6-byte ALPS
966 * packet followed by 3-byte packet from trackpoint. We
967 * can not distinguish between these 2 scenarios but
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700968 * because the latter is unlikely to happen in course of
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800969 * normal operation (user would need to press all
970 * buttons on the pad and start moving trackpoint
971 * without touching the pad surface) we assume former.
972 * Even if we are wrong the wost thing that would happen
973 * the cursor would jump but we should not get protocol
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700974 * de-synchronization.
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800975 */
976
977 alps_report_bare_ps2_packet(psmouse, &psmouse->packet[3],
978 false);
979
980 /*
981 * Continue with the standard ALPS protocol handling,
982 * but make sure we won't process it as an interleaved
983 * packet again, which may happen if all buttons are
984 * pressed. To avoid this let's reset the 4th bit which
985 * is normally 1.
986 */
987 psmouse->packet[3] = psmouse->packet[6] & 0xf7;
988 psmouse->pktcnt = 4;
989 }
990
991 return PSMOUSE_GOOD_DATA;
992}
993
994static void alps_flush_packet(unsigned long data)
995{
996 struct psmouse *psmouse = (struct psmouse *)data;
Kevin Cernekee24af5cb2013-02-13 22:22:08 -0800997 struct alps_data *priv = psmouse->private;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800998
999 serio_pause_rx(psmouse->ps2dev.serio);
1000
Seth Forsheeb46615f2011-11-07 19:53:30 -08001001 if (psmouse->pktcnt == psmouse->pktsize) {
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001002
1003 /*
1004 * We did not any more data in reasonable amount of time.
1005 * Validate the last 3 bytes and process as a standard
1006 * ALPS packet.
1007 */
1008 if ((psmouse->packet[3] |
1009 psmouse->packet[4] |
1010 psmouse->packet[5]) & 0x80) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001011 psmouse_dbg(psmouse,
Andy Shevchenko3b112922012-10-30 00:24:41 -07001012 "refusing packet %3ph (suspected interleaved ps/2)\n",
1013 psmouse->packet + 3);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001014 } else {
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001015 priv->process_packet(psmouse);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001016 }
1017 psmouse->pktcnt = 0;
1018 }
1019
1020 serio_continue_rx(psmouse->ps2dev.serio);
1021}
1022
1023static psmouse_ret_t alps_process_byte(struct psmouse *psmouse)
1024{
1025 struct alps_data *priv = psmouse->private;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001026
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 if ((psmouse->packet[0] & 0xc8) == 0x08) { /* PS/2 packet */
1028 if (psmouse->pktcnt == 3) {
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001029 alps_report_bare_ps2_packet(psmouse, psmouse->packet,
1030 true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 return PSMOUSE_FULL_PACKET;
1032 }
1033 return PSMOUSE_GOOD_DATA;
1034 }
1035
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001036 /* Check for PS/2 packet stuffed in the middle of ALPS packet. */
1037
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001038 if ((priv->flags & ALPS_PS2_INTERLEAVED) &&
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001039 psmouse->pktcnt >= 4 && (psmouse->packet[3] & 0x0f) == 0x0f) {
1040 return alps_handle_interleaved_ps2(psmouse);
1041 }
1042
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001043 if (!alps_is_valid_first_byte(priv, psmouse->packet[0])) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001044 psmouse_dbg(psmouse,
1045 "refusing packet[0] = %x (mask0 = %x, byte0 = %x)\n",
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001046 psmouse->packet[0], priv->mask0, priv->byte0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 return PSMOUSE_BAD_DATA;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001048 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
Seth Forsheeb46615f2011-11-07 19:53:30 -08001050 /* Bytes 2 - pktsize should have 0 in the highest bit */
Yunkang Tang95f75e92013-12-01 22:33:52 -08001051 if ((priv->proto_version < ALPS_PROTO_V5) &&
Dave Turvene75af9e52013-02-21 22:58:28 -08001052 psmouse->pktcnt >= 2 && psmouse->pktcnt <= psmouse->pktsize &&
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001053 (psmouse->packet[psmouse->pktcnt - 1] & 0x80)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001054 psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
1055 psmouse->pktcnt - 1,
1056 psmouse->packet[psmouse->pktcnt - 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 return PSMOUSE_BAD_DATA;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001058 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
Seth Forsheeb46615f2011-11-07 19:53:30 -08001060 if (psmouse->pktcnt == psmouse->pktsize) {
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001061 priv->process_packet(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 return PSMOUSE_FULL_PACKET;
1063 }
1064
1065 return PSMOUSE_GOOD_DATA;
1066}
1067
Seth Forshee25bded72011-11-07 19:53:36 -08001068static int alps_command_mode_send_nibble(struct psmouse *psmouse, int nibble)
1069{
1070 struct ps2dev *ps2dev = &psmouse->ps2dev;
1071 struct alps_data *priv = psmouse->private;
1072 int command;
1073 unsigned char *param;
1074 unsigned char dummy[4];
1075
1076 BUG_ON(nibble > 0xf);
1077
1078 command = priv->nibble_commands[nibble].command;
1079 param = (command & 0x0f00) ?
1080 dummy : (unsigned char *)&priv->nibble_commands[nibble].data;
1081
1082 if (ps2_command(ps2dev, param, command))
1083 return -1;
1084
1085 return 0;
1086}
1087
1088static int alps_command_mode_set_addr(struct psmouse *psmouse, int addr)
1089{
1090 struct ps2dev *ps2dev = &psmouse->ps2dev;
1091 struct alps_data *priv = psmouse->private;
1092 int i, nibble;
1093
1094 if (ps2_command(ps2dev, NULL, priv->addr_command))
1095 return -1;
1096
1097 for (i = 12; i >= 0; i -= 4) {
1098 nibble = (addr >> i) & 0xf;
1099 if (alps_command_mode_send_nibble(psmouse, nibble))
1100 return -1;
1101 }
1102
1103 return 0;
1104}
1105
1106static int __alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
1107{
1108 struct ps2dev *ps2dev = &psmouse->ps2dev;
1109 unsigned char param[4];
1110
1111 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1112 return -1;
1113
1114 /*
1115 * The address being read is returned in the first two bytes
1116 * of the result. Check that this address matches the expected
1117 * address.
1118 */
1119 if (addr != ((param[0] << 8) | param[1]))
1120 return -1;
1121
1122 return param[2];
1123}
1124
1125static int alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
1126{
1127 if (alps_command_mode_set_addr(psmouse, addr))
1128 return -1;
1129 return __alps_command_mode_read_reg(psmouse, addr);
1130}
1131
1132static int __alps_command_mode_write_reg(struct psmouse *psmouse, u8 value)
1133{
1134 if (alps_command_mode_send_nibble(psmouse, (value >> 4) & 0xf))
1135 return -1;
1136 if (alps_command_mode_send_nibble(psmouse, value & 0xf))
1137 return -1;
1138 return 0;
1139}
1140
1141static int alps_command_mode_write_reg(struct psmouse *psmouse, int addr,
1142 u8 value)
1143{
1144 if (alps_command_mode_set_addr(psmouse, addr))
1145 return -1;
1146 return __alps_command_mode_write_reg(psmouse, value);
1147}
1148
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001149static int alps_rpt_cmd(struct psmouse *psmouse, int init_command,
1150 int repeated_command, unsigned char *param)
1151{
1152 struct ps2dev *ps2dev = &psmouse->ps2dev;
1153
1154 param[0] = 0;
1155 if (init_command && ps2_command(ps2dev, param, init_command))
1156 return -EIO;
1157
1158 if (ps2_command(ps2dev, NULL, repeated_command) ||
1159 ps2_command(ps2dev, NULL, repeated_command) ||
1160 ps2_command(ps2dev, NULL, repeated_command))
1161 return -EIO;
1162
1163 param[0] = param[1] = param[2] = 0xff;
1164 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1165 return -EIO;
1166
Dmitry Torokhov39fbe582013-02-14 09:04:24 -08001167 psmouse_dbg(psmouse, "%2.2X report: %3ph\n",
1168 repeated_command, param);
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001169 return 0;
1170}
1171
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08001172static int alps_enter_command_mode(struct psmouse *psmouse)
Seth Forshee25bded72011-11-07 19:53:36 -08001173{
1174 unsigned char param[4];
Seth Forshee25bded72011-11-07 19:53:36 -08001175
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001176 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_RESET_WRAP, param)) {
Seth Forshee25bded72011-11-07 19:53:36 -08001177 psmouse_err(psmouse, "failed to enter command mode\n");
1178 return -1;
1179 }
1180
Dave Turvene75af9e52013-02-21 22:58:28 -08001181 if ((param[0] != 0x88 || (param[1] != 0x07 && param[1] != 0x08)) &&
1182 param[0] != 0x73) {
Seth Forshee25bded72011-11-07 19:53:36 -08001183 psmouse_dbg(psmouse,
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001184 "unknown response while entering command mode\n");
Seth Forshee25bded72011-11-07 19:53:36 -08001185 return -1;
1186 }
Seth Forshee25bded72011-11-07 19:53:36 -08001187 return 0;
1188}
1189
1190static inline int alps_exit_command_mode(struct psmouse *psmouse)
1191{
1192 struct ps2dev *ps2dev = &psmouse->ps2dev;
1193 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM))
1194 return -1;
1195 return 0;
1196}
1197
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198/*
1199 * For DualPoint devices select the device that should respond to
1200 * subsequent commands. It looks like glidepad is behind stickpointer,
1201 * I'd thought it would be other way around...
1202 */
Seth Forshee25bded72011-11-07 19:53:36 -08001203static int alps_passthrough_mode_v2(struct psmouse *psmouse, bool enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204{
1205 struct ps2dev *ps2dev = &psmouse->ps2dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 int cmd = enable ? PSMOUSE_CMD_SETSCALE21 : PSMOUSE_CMD_SETSCALE11;
1207
1208 if (ps2_command(ps2dev, NULL, cmd) ||
1209 ps2_command(ps2dev, NULL, cmd) ||
1210 ps2_command(ps2dev, NULL, cmd) ||
1211 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1212 return -1;
1213
1214 /* we may get 3 more bytes, just ignore them */
Dmitry Torokhovc6117632005-06-01 02:39:51 -05001215 ps2_drain(ps2dev, 3, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
1217 return 0;
1218}
1219
Seth Forshee25bded72011-11-07 19:53:36 -08001220static int alps_absolute_mode_v1_v2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221{
1222 struct ps2dev *ps2dev = &psmouse->ps2dev;
1223
1224 /* Try ALPS magic knock - 4 disable before enable */
1225 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1226 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1227 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1228 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1229 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
1230 return -1;
1231
1232 /*
1233 * Switch mouse to poll (remote) mode so motion data will not
1234 * get in our way
1235 */
1236 return ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETPOLL);
1237}
1238
Yunkang Tang95f75e92013-12-01 22:33:52 -08001239static int alps_monitor_mode_send_word(struct psmouse *psmouse, u16 word)
1240{
1241 int i, nibble;
1242
1243 /*
1244 * b0-b11 are valid bits, send sequence is inverse.
1245 * e.g. when word = 0x0123, nibble send sequence is 3, 2, 1
1246 */
1247 for (i = 0; i <= 8; i += 4) {
1248 nibble = (word >> i) & 0xf;
1249 if (alps_command_mode_send_nibble(psmouse, nibble))
1250 return -1;
1251 }
1252
1253 return 0;
1254}
1255
1256static int alps_monitor_mode_write_reg(struct psmouse *psmouse,
1257 u16 addr, u16 value)
1258{
1259 struct ps2dev *ps2dev = &psmouse->ps2dev;
1260
1261 /* 0x0A0 is the command to write the word */
1262 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE) ||
1263 alps_monitor_mode_send_word(psmouse, 0x0A0) ||
1264 alps_monitor_mode_send_word(psmouse, addr) ||
1265 alps_monitor_mode_send_word(psmouse, value) ||
1266 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1267 return -1;
1268
1269 return 0;
1270}
1271
1272static int alps_monitor_mode(struct psmouse *psmouse, bool enable)
1273{
1274 struct ps2dev *ps2dev = &psmouse->ps2dev;
1275
1276 if (enable) {
1277 /* EC E9 F5 F5 E7 E6 E7 E9 to enter monitor mode */
1278 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
1279 ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO) ||
1280 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1281 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1282 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1283 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1284 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1285 ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO))
1286 return -1;
1287 } else {
1288 /* EC to exit monitor mode */
1289 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP))
1290 return -1;
1291 }
1292
1293 return 0;
1294}
1295
1296static int alps_absolute_mode_v6(struct psmouse *psmouse)
1297{
1298 u16 reg_val = 0x181;
1299 int ret = -1;
1300
1301 /* enter monitor mode, to write the register */
1302 if (alps_monitor_mode(psmouse, true))
1303 return -1;
1304
1305 ret = alps_monitor_mode_write_reg(psmouse, 0x000, reg_val);
1306
1307 if (alps_monitor_mode(psmouse, false))
1308 ret = -1;
1309
1310 return ret;
1311}
1312
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313static int alps_get_status(struct psmouse *psmouse, char *param)
1314{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 /* Get status: 0xF5 0xF5 0xF5 0xE9 */
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001316 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_DISABLE, param))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 return -1;
1318
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 return 0;
1320}
1321
1322/*
1323 * Turn touchpad tapping on or off. The sequences are:
1324 * 0xE9 0xF5 0xF5 0xF3 0x0A to enable,
1325 * 0xE9 0xF5 0xF5 0xE8 0x00 to disable.
1326 * My guess that 0xE9 (GetInfo) is here as a sync point.
1327 * For models that also have stickpointer (DualPoints) its tapping
1328 * is controlled separately (0xE6 0xE6 0xE6 0xF3 0x14|0x0A) but
1329 * we don't fiddle with it.
1330 */
1331static int alps_tap_mode(struct psmouse *psmouse, int enable)
1332{
1333 struct ps2dev *ps2dev = &psmouse->ps2dev;
1334 int cmd = enable ? PSMOUSE_CMD_SETRATE : PSMOUSE_CMD_SETRES;
1335 unsigned char tap_arg = enable ? 0x0A : 0x00;
1336 unsigned char param[4];
1337
1338 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO) ||
1339 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1340 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1341 ps2_command(ps2dev, &tap_arg, cmd))
1342 return -1;
1343
1344 if (alps_get_status(psmouse, param))
1345 return -1;
1346
1347 return 0;
1348}
1349
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001350/*
1351 * alps_poll() - poll the touchpad for current motion packet.
1352 * Used in resync.
1353 */
1354static int alps_poll(struct psmouse *psmouse)
1355{
1356 struct alps_data *priv = psmouse->private;
Seth Forsheeb46615f2011-11-07 19:53:30 -08001357 unsigned char buf[sizeof(psmouse->packet)];
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001358 bool poll_failed;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001359
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001360 if (priv->flags & ALPS_PASS)
Seth Forshee25bded72011-11-07 19:53:36 -08001361 alps_passthrough_mode_v2(psmouse, true);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001362
1363 poll_failed = ps2_command(&psmouse->ps2dev, buf,
1364 PSMOUSE_CMD_POLL | (psmouse->pktsize << 8)) < 0;
1365
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001366 if (priv->flags & ALPS_PASS)
Seth Forshee25bded72011-11-07 19:53:36 -08001367 alps_passthrough_mode_v2(psmouse, false);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001368
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001369 if (poll_failed || (buf[0] & priv->mask0) != priv->byte0)
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001370 return -1;
1371
1372 if ((psmouse->badbyte & 0xc8) == 0x08) {
1373/*
1374 * Poll the track stick ...
1375 */
1376 if (ps2_command(&psmouse->ps2dev, buf, PSMOUSE_CMD_POLL | (3 << 8)))
1377 return -1;
1378 }
1379
1380 memcpy(psmouse->packet, buf, sizeof(buf));
1381 return 0;
1382}
1383
Seth Forshee25bded72011-11-07 19:53:36 -08001384static int alps_hw_init_v1_v2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385{
1386 struct alps_data *priv = psmouse->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001388 if ((priv->flags & ALPS_PASS) &&
Seth Forshee25bded72011-11-07 19:53:36 -08001389 alps_passthrough_mode_v2(psmouse, true)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 return -1;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001391 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001393 if (alps_tap_mode(psmouse, true)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001394 psmouse_warn(psmouse, "Failed to enable hardware tapping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 return -1;
Peter Osterlund963f6262005-07-11 01:08:04 -05001396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
Seth Forshee25bded72011-11-07 19:53:36 -08001398 if (alps_absolute_mode_v1_v2(psmouse)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001399 psmouse_err(psmouse, "Failed to enable absolute mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 return -1;
1401 }
1402
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001403 if ((priv->flags & ALPS_PASS) &&
Seth Forshee25bded72011-11-07 19:53:36 -08001404 alps_passthrough_mode_v2(psmouse, false)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 return -1;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04001408 /* ALPS needs stream mode, otherwise it won't report any data */
1409 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001410 psmouse_err(psmouse, "Failed to enable stream mode\n");
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04001411 return -1;
1412 }
1413
1414 return 0;
1415}
1416
Yunkang Tang95f75e92013-12-01 22:33:52 -08001417static int alps_hw_init_v6(struct psmouse *psmouse)
1418{
1419 unsigned char param[2] = {0xC8, 0x14};
1420
1421 /* Enter passthrough mode to let trackpoint enter 6byte raw mode */
1422 if (alps_passthrough_mode_v2(psmouse, true))
1423 return -1;
1424
1425 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1426 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1427 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1428 ps2_command(&psmouse->ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
1429 ps2_command(&psmouse->ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
1430 return -1;
1431
1432 if (alps_passthrough_mode_v2(psmouse, false))
1433 return -1;
1434
1435 if (alps_absolute_mode_v6(psmouse)) {
1436 psmouse_err(psmouse, "Failed to enable absolute mode\n");
1437 return -1;
1438 }
1439
1440 return 0;
1441}
1442
Seth Forshee25bded72011-11-07 19:53:36 -08001443/*
Kevin Cernekeecd401202013-02-13 22:28:07 -08001444 * Enable or disable passthrough mode to the trackstick.
Seth Forshee25bded72011-11-07 19:53:36 -08001445 */
Kevin Cernekeecd401202013-02-13 22:28:07 -08001446static int alps_passthrough_mode_v3(struct psmouse *psmouse,
1447 int reg_base, bool enable)
Seth Forshee25bded72011-11-07 19:53:36 -08001448{
Kevin Cernekeecd401202013-02-13 22:28:07 -08001449 int reg_val, ret = -1;
Seth Forshee25bded72011-11-07 19:53:36 -08001450
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08001451 if (alps_enter_command_mode(psmouse))
Seth Forshee25bded72011-11-07 19:53:36 -08001452 return -1;
1453
Kevin Cernekeecd401202013-02-13 22:28:07 -08001454 reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x0008);
1455 if (reg_val == -1)
1456 goto error;
1457
Seth Forshee25bded72011-11-07 19:53:36 -08001458 if (enable)
1459 reg_val |= 0x01;
1460 else
1461 reg_val &= ~0x01;
1462
Kevin Cernekeecd401202013-02-13 22:28:07 -08001463 ret = __alps_command_mode_write_reg(psmouse, reg_val);
Seth Forshee25bded72011-11-07 19:53:36 -08001464
Kevin Cernekeecd401202013-02-13 22:28:07 -08001465error:
1466 if (alps_exit_command_mode(psmouse))
1467 ret = -1;
1468 return ret;
Seth Forshee25bded72011-11-07 19:53:36 -08001469}
1470
1471/* Must be in command mode when calling this function */
1472static int alps_absolute_mode_v3(struct psmouse *psmouse)
1473{
1474 int reg_val;
1475
1476 reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
1477 if (reg_val == -1)
1478 return -1;
1479
1480 reg_val |= 0x06;
1481 if (__alps_command_mode_write_reg(psmouse, reg_val))
1482 return -1;
1483
1484 return 0;
1485}
1486
Kevin Cernekeecd401202013-02-13 22:28:07 -08001487static int alps_probe_trackstick_v3(struct psmouse *psmouse, int reg_base)
1488{
1489 int ret = -EIO, reg_val;
1490
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08001491 if (alps_enter_command_mode(psmouse))
Kevin Cernekeecd401202013-02-13 22:28:07 -08001492 goto error;
1493
1494 reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x08);
1495 if (reg_val == -1)
1496 goto error;
1497
1498 /* bit 7: trackstick is present */
1499 ret = reg_val & 0x80 ? 0 : -ENODEV;
1500
1501error:
1502 alps_exit_command_mode(psmouse);
1503 return ret;
1504}
1505
1506static int alps_setup_trackstick_v3(struct psmouse *psmouse, int reg_base)
1507{
1508 struct ps2dev *ps2dev = &psmouse->ps2dev;
1509 int ret = 0;
1510 unsigned char param[4];
1511
1512 if (alps_passthrough_mode_v3(psmouse, reg_base, true))
1513 return -EIO;
1514
1515 /*
1516 * E7 report for the trackstick
1517 *
1518 * There have been reports of failures to seem to trace back
1519 * to the above trackstick check failing. When these occur
1520 * this E7 report fails, so when that happens we continue
1521 * with the assumption that there isn't a trackstick after
1522 * all.
1523 */
1524 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_SETSCALE21, param)) {
1525 psmouse_warn(psmouse, "trackstick E7 report failed\n");
1526 ret = -ENODEV;
1527 } else {
Dmitry Torokhov39fbe582013-02-14 09:04:24 -08001528 psmouse_dbg(psmouse, "trackstick E7 report: %3ph\n", param);
Kevin Cernekeecd401202013-02-13 22:28:07 -08001529
1530 /*
1531 * Not sure what this does, but it is absolutely
1532 * essential. Without it, the touchpad does not
1533 * work at all and the trackstick just emits normal
1534 * PS/2 packets.
1535 */
1536 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1537 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1538 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1539 alps_command_mode_send_nibble(psmouse, 0x9) ||
1540 alps_command_mode_send_nibble(psmouse, 0x4)) {
1541 psmouse_err(psmouse,
1542 "Error sending magic E6 sequence\n");
1543 ret = -EIO;
1544 goto error;
1545 }
1546
1547 /*
1548 * This ensures the trackstick packets are in the format
1549 * supported by this driver. If bit 1 isn't set the packet
1550 * format is different.
1551 */
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08001552 if (alps_enter_command_mode(psmouse) ||
Kevin Cernekeecd401202013-02-13 22:28:07 -08001553 alps_command_mode_write_reg(psmouse,
1554 reg_base + 0x08, 0x82) ||
1555 alps_exit_command_mode(psmouse))
1556 ret = -EIO;
1557 }
1558
1559error:
1560 if (alps_passthrough_mode_v3(psmouse, reg_base, false))
1561 ret = -EIO;
1562
1563 return ret;
1564}
1565
Seth Forshee25bded72011-11-07 19:53:36 -08001566static int alps_hw_init_v3(struct psmouse *psmouse)
1567{
Seth Forshee25bded72011-11-07 19:53:36 -08001568 struct ps2dev *ps2dev = &psmouse->ps2dev;
1569 int reg_val;
1570 unsigned char param[4];
1571
Kevin Cernekeecd401202013-02-13 22:28:07 -08001572 reg_val = alps_probe_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE);
1573 if (reg_val == -EIO)
1574 goto error;
Dmitry Torokhov39fbe582013-02-14 09:04:24 -08001575
Kevin Cernekeecd401202013-02-13 22:28:07 -08001576 if (reg_val == 0 &&
1577 alps_setup_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE) == -EIO)
Seth Forshee25bded72011-11-07 19:53:36 -08001578 goto error;
1579
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08001580 if (alps_enter_command_mode(psmouse) ||
Kevin Cernekeecd401202013-02-13 22:28:07 -08001581 alps_absolute_mode_v3(psmouse)) {
Seth Forshee25bded72011-11-07 19:53:36 -08001582 psmouse_err(psmouse, "Failed to enter absolute mode\n");
1583 goto error;
1584 }
1585
1586 reg_val = alps_command_mode_read_reg(psmouse, 0x0006);
1587 if (reg_val == -1)
1588 goto error;
1589 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
1590 goto error;
1591
1592 reg_val = alps_command_mode_read_reg(psmouse, 0x0007);
1593 if (reg_val == -1)
1594 goto error;
1595 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
1596 goto error;
1597
1598 if (alps_command_mode_read_reg(psmouse, 0x0144) == -1)
1599 goto error;
1600 if (__alps_command_mode_write_reg(psmouse, 0x04))
1601 goto error;
1602
1603 if (alps_command_mode_read_reg(psmouse, 0x0159) == -1)
1604 goto error;
1605 if (__alps_command_mode_write_reg(psmouse, 0x03))
1606 goto error;
1607
1608 if (alps_command_mode_read_reg(psmouse, 0x0163) == -1)
1609 goto error;
1610 if (alps_command_mode_write_reg(psmouse, 0x0163, 0x03))
1611 goto error;
1612
1613 if (alps_command_mode_read_reg(psmouse, 0x0162) == -1)
1614 goto error;
1615 if (alps_command_mode_write_reg(psmouse, 0x0162, 0x04))
1616 goto error;
1617
Seth Forshee25bded72011-11-07 19:53:36 -08001618 alps_exit_command_mode(psmouse);
1619
1620 /* Set rate and enable data reporting */
1621 param[0] = 0x64;
1622 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
1623 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
1624 psmouse_err(psmouse, "Failed to enable data reporting\n");
1625 return -1;
1626 }
1627
1628 return 0;
1629
Seth Forshee25bded72011-11-07 19:53:36 -08001630error:
1631 /*
1632 * Leaving the touchpad in command mode will essentially render
1633 * it unusable until the machine reboots, so exit it here just
1634 * to be safe
1635 */
1636 alps_exit_command_mode(psmouse);
1637 return -1;
1638}
1639
Kevin Cernekee1302bac2013-02-13 22:27:08 -08001640static int alps_hw_init_rushmore_v3(struct psmouse *psmouse)
1641{
Kevin Cernekeecd401202013-02-13 22:28:07 -08001642 struct alps_data *priv = psmouse->private;
Kevin Cernekee1302bac2013-02-13 22:27:08 -08001643 struct ps2dev *ps2dev = &psmouse->ps2dev;
1644 int reg_val, ret = -1;
1645
Kevin Cernekeecd401202013-02-13 22:28:07 -08001646 if (priv->flags & ALPS_DUALPOINT) {
1647 reg_val = alps_setup_trackstick_v3(psmouse,
1648 ALPS_REG_BASE_RUSHMORE);
1649 if (reg_val == -EIO)
1650 goto error;
1651 if (reg_val == -ENODEV)
1652 priv->flags &= ~ALPS_DUALPOINT;
1653 }
1654
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08001655 if (alps_enter_command_mode(psmouse) ||
Kevin Cernekee1302bac2013-02-13 22:27:08 -08001656 alps_command_mode_read_reg(psmouse, 0xc2d9) == -1 ||
1657 alps_command_mode_write_reg(psmouse, 0xc2cb, 0x00))
1658 goto error;
1659
1660 reg_val = alps_command_mode_read_reg(psmouse, 0xc2c6);
1661 if (reg_val == -1)
1662 goto error;
1663 if (__alps_command_mode_write_reg(psmouse, reg_val & 0xfd))
1664 goto error;
1665
1666 if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
1667 goto error;
1668
1669 /* enter absolute mode */
1670 reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
1671 if (reg_val == -1)
1672 goto error;
1673 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
1674 goto error;
1675
1676 alps_exit_command_mode(psmouse);
1677 return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
1678
1679error:
1680 alps_exit_command_mode(psmouse);
1681 return ret;
1682}
1683
Seth Forshee25bded72011-11-07 19:53:36 -08001684/* Must be in command mode when calling this function */
1685static int alps_absolute_mode_v4(struct psmouse *psmouse)
1686{
1687 int reg_val;
1688
1689 reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
1690 if (reg_val == -1)
1691 return -1;
1692
1693 reg_val |= 0x02;
1694 if (__alps_command_mode_write_reg(psmouse, reg_val))
1695 return -1;
1696
1697 return 0;
1698}
1699
1700static int alps_hw_init_v4(struct psmouse *psmouse)
1701{
Seth Forshee25bded72011-11-07 19:53:36 -08001702 struct ps2dev *ps2dev = &psmouse->ps2dev;
1703 unsigned char param[4];
1704
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08001705 if (alps_enter_command_mode(psmouse))
Seth Forshee25bded72011-11-07 19:53:36 -08001706 goto error;
1707
1708 if (alps_absolute_mode_v4(psmouse)) {
1709 psmouse_err(psmouse, "Failed to enter absolute mode\n");
1710 goto error;
1711 }
1712
1713 if (alps_command_mode_write_reg(psmouse, 0x0007, 0x8c))
1714 goto error;
1715
1716 if (alps_command_mode_write_reg(psmouse, 0x0149, 0x03))
1717 goto error;
1718
1719 if (alps_command_mode_write_reg(psmouse, 0x0160, 0x03))
1720 goto error;
1721
1722 if (alps_command_mode_write_reg(psmouse, 0x017f, 0x15))
1723 goto error;
1724
1725 if (alps_command_mode_write_reg(psmouse, 0x0151, 0x01))
1726 goto error;
1727
1728 if (alps_command_mode_write_reg(psmouse, 0x0168, 0x03))
1729 goto error;
1730
1731 if (alps_command_mode_write_reg(psmouse, 0x014a, 0x03))
1732 goto error;
1733
1734 if (alps_command_mode_write_reg(psmouse, 0x0161, 0x03))
1735 goto error;
1736
1737 alps_exit_command_mode(psmouse);
1738
1739 /*
1740 * This sequence changes the output from a 9-byte to an
1741 * 8-byte format. All the same data seems to be present,
1742 * just in a more compact format.
1743 */
1744 param[0] = 0xc8;
1745 param[1] = 0x64;
1746 param[2] = 0x50;
1747 if (ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
1748 ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE) ||
1749 ps2_command(ps2dev, &param[2], PSMOUSE_CMD_SETRATE) ||
1750 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
1751 return -1;
1752
1753 /* Set rate and enable data reporting */
1754 param[0] = 0x64;
1755 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
1756 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
1757 psmouse_err(psmouse, "Failed to enable data reporting\n");
1758 return -1;
1759 }
1760
1761 return 0;
1762
1763error:
1764 /*
1765 * Leaving the touchpad in command mode will essentially render
1766 * it unusable until the machine reboots, so exit it here just
1767 * to be safe
1768 */
1769 alps_exit_command_mode(psmouse);
1770 return -1;
1771}
1772
Yunkang Tangee65d4b2013-12-26 14:54:19 -08001773static int alps_dolphin_get_device_area(struct psmouse *psmouse,
1774 struct alps_data *priv)
1775{
1776 struct ps2dev *ps2dev = &psmouse->ps2dev;
1777 unsigned char param[4] = {0};
1778 int num_x_electrode, num_y_electrode;
1779
1780 if (alps_enter_command_mode(psmouse))
1781 return -1;
1782
1783 param[0] = 0x0a;
1784 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
1785 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
1786 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
1787 ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
1788 ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE))
1789 return -1;
1790
1791 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1792 return -1;
1793
1794 /*
1795 * Dolphin's sensor line number is not fixed. It can be calculated
1796 * by adding the device's register value with DOLPHIN_PROFILE_X/YOFFSET.
1797 * Further more, we can get device's x_max and y_max by multiplying
1798 * sensor line number with DOLPHIN_COUNT_PER_ELECTRODE.
1799 *
1800 * e.g. When we get register's sensor_x = 11 & sensor_y = 8,
1801 * real sensor line number X = 11 + 8 = 19, and
1802 * real sensor line number Y = 8 + 1 = 9.
1803 * So, x_max = (19 - 1) * 64 = 1152, and
1804 * y_max = (9 - 1) * 64 = 512.
1805 */
1806 num_x_electrode = DOLPHIN_PROFILE_XOFFSET + (param[2] & 0x0F);
1807 num_y_electrode = DOLPHIN_PROFILE_YOFFSET + ((param[2] >> 4) & 0x0F);
1808 priv->x_bits = num_x_electrode;
1809 priv->y_bits = num_y_electrode;
1810 priv->x_max = (num_x_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
1811 priv->y_max = (num_y_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
1812
1813 if (alps_exit_command_mode(psmouse))
1814 return -1;
1815
1816 return 0;
1817}
1818
Dave Turvene75af9e52013-02-21 22:58:28 -08001819static int alps_hw_init_dolphin_v1(struct psmouse *psmouse)
1820{
1821 struct ps2dev *ps2dev = &psmouse->ps2dev;
1822 unsigned char param[2];
1823
1824 /* This is dolphin "v1" as empirically defined by florin9doi */
1825 param[0] = 0x64;
1826 param[1] = 0x28;
1827
1828 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
1829 ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
1830 ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
1831 return -1;
1832
1833 return 0;
1834}
1835
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001836static void alps_set_defaults(struct alps_data *priv)
Seth Forshee25bded72011-11-07 19:53:36 -08001837{
Kevin Cernekeef673ceb2013-02-13 22:23:34 -08001838 priv->byte0 = 0x8f;
1839 priv->mask0 = 0x8f;
1840 priv->flags = ALPS_DUALPOINT;
1841
Kevin Cernekee7a9f73e2013-02-13 22:24:55 -08001842 priv->x_max = 2000;
1843 priv->y_max = 1400;
1844 priv->x_bits = 15;
1845 priv->y_bits = 11;
1846
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001847 switch (priv->proto_version) {
Seth Forshee25bded72011-11-07 19:53:36 -08001848 case ALPS_PROTO_V1:
1849 case ALPS_PROTO_V2:
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001850 priv->hw_init = alps_hw_init_v1_v2;
1851 priv->process_packet = alps_process_packet_v1_v2;
1852 priv->set_abs_params = alps_set_abs_params_st;
Yunkang Tang95f75e92013-12-01 22:33:52 -08001853 priv->x_max = 1023;
1854 priv->y_max = 767;
Seth Forshee25bded72011-11-07 19:53:36 -08001855 break;
1856 case ALPS_PROTO_V3:
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001857 priv->hw_init = alps_hw_init_v3;
1858 priv->process_packet = alps_process_packet_v3;
1859 priv->set_abs_params = alps_set_abs_params_mt;
Kevin Cernekeef85e5002013-02-13 22:26:11 -08001860 priv->decode_fields = alps_decode_pinnacle;
Kevin Cernekee50e8b212013-02-13 22:23:04 -08001861 priv->nibble_commands = alps_v3_nibble_commands;
1862 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
Seth Forshee25bded72011-11-07 19:53:36 -08001863 break;
1864 case ALPS_PROTO_V4:
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001865 priv->hw_init = alps_hw_init_v4;
1866 priv->process_packet = alps_process_packet_v4;
1867 priv->set_abs_params = alps_set_abs_params_mt;
Kevin Cernekee50e8b212013-02-13 22:23:04 -08001868 priv->nibble_commands = alps_v4_nibble_commands;
1869 priv->addr_command = PSMOUSE_CMD_DISABLE;
Seth Forshee25bded72011-11-07 19:53:36 -08001870 break;
Dave Turvene75af9e52013-02-21 22:58:28 -08001871 case ALPS_PROTO_V5:
1872 priv->hw_init = alps_hw_init_dolphin_v1;
Yunkang Tangee65d4b2013-12-26 14:54:19 -08001873 priv->process_packet = alps_process_touchpad_packet_v3_v5;
Dave Turvene75af9e52013-02-21 22:58:28 -08001874 priv->decode_fields = alps_decode_dolphin;
1875 priv->set_abs_params = alps_set_abs_params_mt;
1876 priv->nibble_commands = alps_v3_nibble_commands;
1877 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
1878 priv->byte0 = 0xc8;
Yunkang Tangee65d4b2013-12-26 14:54:19 -08001879 priv->mask0 = 0xd8;
Dave Turvene75af9e52013-02-21 22:58:28 -08001880 priv->flags = 0;
1881 priv->x_max = 1360;
1882 priv->y_max = 660;
1883 priv->x_bits = 23;
1884 priv->y_bits = 12;
1885 break;
Yunkang Tang95f75e92013-12-01 22:33:52 -08001886 case ALPS_PROTO_V6:
1887 priv->hw_init = alps_hw_init_v6;
1888 priv->process_packet = alps_process_packet_v6;
1889 priv->set_abs_params = alps_set_abs_params_st;
1890 priv->nibble_commands = alps_v6_nibble_commands;
1891 priv->x_max = 2047;
1892 priv->y_max = 1535;
1893 break;
Seth Forshee25bded72011-11-07 19:53:36 -08001894 }
Seth Forshee25bded72011-11-07 19:53:36 -08001895}
1896
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001897static int alps_match_table(struct psmouse *psmouse, struct alps_data *priv,
1898 unsigned char *e7, unsigned char *ec)
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08001899{
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001900 const struct alps_model_info *model;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08001901 int i;
1902
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001903 for (i = 0; i < ARRAY_SIZE(alps_model_data); i++) {
1904 model = &alps_model_data[i];
1905
1906 if (!memcmp(e7, model->signature, sizeof(model->signature)) &&
1907 (!model->command_mode_resp ||
1908 model->command_mode_resp == ec[2])) {
1909
1910 priv->proto_version = model->proto_version;
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001911 alps_set_defaults(priv);
1912
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001913 priv->flags = model->flags;
1914 priv->byte0 = model->byte0;
1915 priv->mask0 = model->mask0;
1916
1917 return 0;
1918 }
1919 }
1920
1921 return -EINVAL;
1922}
1923
1924static int alps_identify(struct psmouse *psmouse, struct alps_data *priv)
1925{
1926 unsigned char e6[4], e7[4], ec[4];
1927
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08001928 /*
1929 * First try "E6 report".
1930 * ALPS should return 0,0,10 or 0,0,100 if no buttons are pressed.
1931 * The bits 0-2 of the first byte will be 1s if some buttons are
1932 * pressed.
1933 */
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001934 if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
1935 PSMOUSE_CMD_SETSCALE11, e6))
1936 return -EIO;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08001937
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001938 if ((e6[0] & 0xf8) != 0 || e6[1] != 0 || (e6[2] != 10 && e6[2] != 100))
1939 return -EINVAL;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08001940
1941 /*
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001942 * Now get the "E7" and "EC" reports. These will uniquely identify
1943 * most ALPS touchpads.
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08001944 */
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001945 if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
1946 PSMOUSE_CMD_SETSCALE21, e7) ||
1947 alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
1948 PSMOUSE_CMD_RESET_WRAP, ec) ||
1949 alps_exit_command_mode(psmouse))
1950 return -EIO;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08001951
Kevin Cernekeef673ceb2013-02-13 22:23:34 -08001952 if (alps_match_table(psmouse, priv, e7, ec) == 0) {
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001953 return 0;
Dave Turvene75af9e52013-02-21 22:58:28 -08001954 } else if (e7[0] == 0x73 && e7[1] == 0x03 && e7[2] == 0x50 &&
Yunkang Tangee65d4b2013-12-26 14:54:19 -08001955 ec[0] == 0x73 && (ec[1] == 0x01 || ec[1] == 0x02)) {
Dave Turvene75af9e52013-02-21 22:58:28 -08001956 priv->proto_version = ALPS_PROTO_V5;
1957 alps_set_defaults(priv);
Yunkang Tangee65d4b2013-12-26 14:54:19 -08001958 if (alps_dolphin_get_device_area(psmouse, priv))
1959 return -EIO;
1960 else
1961 return 0;
Kevin Cernekee1302bac2013-02-13 22:27:08 -08001962 } else if (ec[0] == 0x88 && ec[1] == 0x08) {
1963 priv->proto_version = ALPS_PROTO_V3;
1964 alps_set_defaults(priv);
1965
1966 priv->hw_init = alps_hw_init_rushmore_v3;
1967 priv->decode_fields = alps_decode_rushmore;
1968 priv->x_bits = 16;
1969 priv->y_bits = 12;
Hans de Goede40e8f532014-07-25 22:37:15 -07001970 priv->flags |= ALPS_IS_RUSHMORE;
Kevin Cernekee1302bac2013-02-13 22:27:08 -08001971
Kevin Cernekeecd401202013-02-13 22:28:07 -08001972 /* hack to make addr_command, nibble_command available */
1973 psmouse->private = priv;
1974
1975 if (alps_probe_trackstick_v3(psmouse, ALPS_REG_BASE_RUSHMORE))
1976 priv->flags &= ~ALPS_DUALPOINT;
1977
Kevin Cernekee1302bac2013-02-13 22:27:08 -08001978 return 0;
Kevin Cernekeef673ceb2013-02-13 22:23:34 -08001979 } else if (ec[0] == 0x88 && ec[1] == 0x07 &&
1980 ec[2] >= 0x90 && ec[2] <= 0x9d) {
1981 priv->proto_version = ALPS_PROTO_V3;
1982 alps_set_defaults(priv);
1983
1984 return 0;
1985 }
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08001986
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001987 psmouse_info(psmouse,
Dmitry Torokhov39fbe582013-02-14 09:04:24 -08001988 "Unknown ALPS touchpad: E7=%3ph, EC=%3ph\n", e7, ec);
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08001989
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001990 return -EINVAL;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08001991}
1992
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04001993static int alps_reconnect(struct psmouse *psmouse)
1994{
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001995 struct alps_data *priv = psmouse->private;
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08001996
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04001997 psmouse_reset(psmouse);
1998
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001999 if (alps_identify(psmouse, priv) < 0)
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04002000 return -1;
2001
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002002 return priv->hw_init(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003}
2004
2005static void alps_disconnect(struct psmouse *psmouse)
2006{
2007 struct alps_data *priv = psmouse->private;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05002008
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 psmouse_reset(psmouse);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08002010 del_timer_sync(&priv->timer);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05002011 input_unregister_device(priv->dev2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 kfree(priv);
2013}
2014
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002015static void alps_set_abs_params_st(struct alps_data *priv,
2016 struct input_dev *dev1)
2017{
Yunkang Tang95f75e92013-12-01 22:33:52 -08002018 input_set_abs_params(dev1, ABS_X, 0, priv->x_max, 0, 0);
2019 input_set_abs_params(dev1, ABS_Y, 0, priv->y_max, 0, 0);
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002020}
2021
2022static void alps_set_abs_params_mt(struct alps_data *priv,
2023 struct input_dev *dev1)
2024{
2025 set_bit(INPUT_PROP_SEMI_MT, dev1->propbit);
2026 input_mt_init_slots(dev1, 2, 0);
Kevin Cernekee7a9f73e2013-02-13 22:24:55 -08002027 input_set_abs_params(dev1, ABS_MT_POSITION_X, 0, priv->x_max, 0, 0);
2028 input_set_abs_params(dev1, ABS_MT_POSITION_Y, 0, priv->y_max, 0, 0);
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002029
2030 set_bit(BTN_TOOL_DOUBLETAP, dev1->keybit);
2031 set_bit(BTN_TOOL_TRIPLETAP, dev1->keybit);
2032 set_bit(BTN_TOOL_QUADTAP, dev1->keybit);
2033
Kevin Cernekee7a9f73e2013-02-13 22:24:55 -08002034 input_set_abs_params(dev1, ABS_X, 0, priv->x_max, 0, 0);
2035 input_set_abs_params(dev1, ABS_Y, 0, priv->y_max, 0, 0);
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002036}
2037
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038int alps_init(struct psmouse *psmouse)
2039{
2040 struct alps_data *priv;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05002041 struct input_dev *dev1 = psmouse->dev, *dev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042
Dmitry Torokhovf42649e2007-04-12 01:31:13 -04002043 priv = kzalloc(sizeof(struct alps_data), GFP_KERNEL);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05002044 dev2 = input_allocate_device();
2045 if (!priv || !dev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 goto init_fail;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05002047
2048 priv->dev2 = dev2;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08002049 setup_timer(&priv->timer, alps_flush_packet, (unsigned long)psmouse);
2050
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04002051 psmouse->private = priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052
Seth Forshee25bded72011-11-07 19:53:36 -08002053 psmouse_reset(psmouse);
2054
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002055 if (alps_identify(psmouse, priv) < 0)
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08002056 goto init_fail;
2057
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002058 if (priv->hw_init(psmouse))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 goto init_fail;
2060
Dmitry Torokhov7105d2e2009-12-11 23:54:54 -08002061 /*
2062 * Undo part of setup done for us by psmouse core since touchpad
2063 * is not a relative device.
2064 */
2065 __clear_bit(EV_REL, dev1->evbit);
2066 __clear_bit(REL_X, dev1->relbit);
2067 __clear_bit(REL_Y, dev1->relbit);
2068
2069 /*
2070 * Now set up our capabilities.
2071 */
Jiri Slaby7b19ada2007-10-18 23:40:32 -07002072 dev1->evbit[BIT_WORD(EV_KEY)] |= BIT_MASK(EV_KEY);
2073 dev1->keybit[BIT_WORD(BTN_TOUCH)] |= BIT_MASK(BTN_TOUCH);
2074 dev1->keybit[BIT_WORD(BTN_TOOL_FINGER)] |= BIT_MASK(BTN_TOOL_FINGER);
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08002075 dev1->keybit[BIT_WORD(BTN_LEFT)] |=
2076 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077
Jiri Slaby7b19ada2007-10-18 23:40:32 -07002078 dev1->evbit[BIT_WORD(EV_ABS)] |= BIT_MASK(EV_ABS);
Seth Forshee25bded72011-11-07 19:53:36 -08002079
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002080 priv->set_abs_params(priv, dev1);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05002081 input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002083 if (priv->flags & ALPS_WHEEL) {
Jiri Slaby7b19ada2007-10-18 23:40:32 -07002084 dev1->evbit[BIT_WORD(EV_REL)] |= BIT_MASK(EV_REL);
2085 dev1->relbit[BIT_WORD(REL_WHEEL)] |= BIT_MASK(REL_WHEEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 }
2087
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002088 if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
Jiri Slaby7b19ada2007-10-18 23:40:32 -07002089 dev1->keybit[BIT_WORD(BTN_FORWARD)] |= BIT_MASK(BTN_FORWARD);
2090 dev1->keybit[BIT_WORD(BTN_BACK)] |= BIT_MASK(BTN_BACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 }
2092
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002093 if (priv->flags & ALPS_FOUR_BUTTONS) {
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08002094 dev1->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_0);
2095 dev1->keybit[BIT_WORD(BTN_1)] |= BIT_MASK(BTN_1);
2096 dev1->keybit[BIT_WORD(BTN_2)] |= BIT_MASK(BTN_2);
2097 dev1->keybit[BIT_WORD(BTN_3)] |= BIT_MASK(BTN_3);
2098 } else {
2099 dev1->keybit[BIT_WORD(BTN_MIDDLE)] |= BIT_MASK(BTN_MIDDLE);
2100 }
2101
Dmitry Torokhov08ffce42006-06-26 01:45:10 -04002102 snprintf(priv->phys, sizeof(priv->phys), "%s/input1", psmouse->ps2dev.serio->phys);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05002103 dev2->phys = priv->phys;
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002104 dev2->name = (priv->flags & ALPS_DUALPOINT) ?
Yunkang Tang9354f262013-10-15 23:50:38 -07002105 "DualPoint Stick" : "ALPS PS/2 Device";
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05002106 dev2->id.bustype = BUS_I8042;
2107 dev2->id.vendor = 0x0002;
2108 dev2->id.product = PSMOUSE_ALPS;
2109 dev2->id.version = 0x0000;
Dmitry Torokhov1db3a342008-03-18 00:29:18 -04002110 dev2->dev.parent = &psmouse->ps2dev.serio->dev;
Dmitry Torokhov968ac842005-05-29 02:28:29 -05002111
Jiri Slaby7b19ada2007-10-18 23:40:32 -07002112 dev2->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08002113 dev2->relbit[BIT_WORD(REL_X)] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
2114 dev2->keybit[BIT_WORD(BTN_LEFT)] =
2115 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_MIDDLE) | BIT_MASK(BTN_RIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116
Dmitry Torokhovf42649e2007-04-12 01:31:13 -04002117 if (input_register_device(priv->dev2))
2118 goto init_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119
2120 psmouse->protocol_handler = alps_process_byte;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05002121 psmouse->poll = alps_poll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 psmouse->disconnect = alps_disconnect;
2123 psmouse->reconnect = alps_reconnect;
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002124 psmouse->pktsize = priv->proto_version == ALPS_PROTO_V4 ? 8 : 6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05002126 /* We are having trouble resyncing ALPS touchpads so disable it for now */
2127 psmouse->resync_time = 0;
2128
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 return 0;
2130
2131init_fail:
Dmitry Torokhovf42649e2007-04-12 01:31:13 -04002132 psmouse_reset(psmouse);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05002133 input_free_device(dev2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 kfree(priv);
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04002135 psmouse->private = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 return -1;
2137}
2138
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07002139int alps_detect(struct psmouse *psmouse, bool set_properties)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140{
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002141 struct alps_data dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002143 if (alps_identify(psmouse, &dummy) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 return -1;
2145
2146 if (set_properties) {
2147 psmouse->vendor = "ALPS";
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002148 psmouse->name = dummy.flags & ALPS_DUALPOINT ?
Dmitry Torokhov968ac842005-05-29 02:28:29 -05002149 "DualPoint TouchPad" : "GlidePoint";
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002150 psmouse->model = dummy.proto_version << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 }
2152 return 0;
2153}
2154