blob: e15daea568644e0a08eca3beefe49552bdd7b984 [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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Helge Dellere38de672006-09-10 21:54:39 -0400103static const struct alps_model_info alps_model_data[] = {
Seth Forshee25bded72011-11-07 19:53:36 -0800104 { { 0x32, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT }, /* Toshiba Salellite Pro M10 */
105 { { 0x33, 0x02, 0x0a }, 0x00, ALPS_PROTO_V1, 0x88, 0xf8, 0 }, /* UMAX-530T */
106 { { 0x53, 0x02, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
107 { { 0x53, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
108 { { 0x60, 0x03, 0xc8 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 }, /* HP ze1115 */
109 { { 0x63, 0x02, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
110 { { 0x63, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
111 { { 0x63, 0x02, 0x28 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 }, /* Fujitsu Siemens S6010 */
112 { { 0x63, 0x02, 0x3c }, 0x00, ALPS_PROTO_V2, 0x8f, 0x8f, ALPS_WHEEL }, /* Toshiba Satellite S2400-103 */
113 { { 0x63, 0x02, 0x50 }, 0x00, ALPS_PROTO_V2, 0xef, 0xef, ALPS_FW_BK_1 }, /* NEC Versa L320 */
114 { { 0x63, 0x02, 0x64 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
115 { { 0x63, 0x03, 0xc8 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT }, /* Dell Latitude D800 */
116 { { 0x73, 0x00, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_DUALPOINT }, /* ThinkPad R61 8918-5QG */
117 { { 0x73, 0x02, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
118 { { 0x73, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 }, /* Ahtec Laptop */
119 { { 0x20, 0x02, 0x0e }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT }, /* XXX */
120 { { 0x22, 0x02, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT },
121 { { 0x22, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xff, 0xff, ALPS_PASS | ALPS_DUALPOINT }, /* Dell Latitude D600 */
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800122 /* Dell Latitude E5500, E6400, E6500, Precision M4400 */
Seth Forshee25bded72011-11-07 19:53:36 -0800123 { { 0x62, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xcf, 0xcf,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800124 ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED },
Yunkang Tang95f75e92013-12-01 22:33:52 -0800125 { { 0x73, 0x00, 0x14 }, 0x00, ALPS_PROTO_V6, 0xff, 0xff, ALPS_DUALPOINT }, /* Dell XT2 */
Seth Forshee25bded72011-11-07 19:53:36 -0800126 { { 0x73, 0x02, 0x50 }, 0x00, ALPS_PROTO_V2, 0xcf, 0xcf, ALPS_FOUR_BUTTONS }, /* Dell Vostro 1400 */
127 { { 0x52, 0x01, 0x14 }, 0x00, ALPS_PROTO_V2, 0xff, 0xff,
128 ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED }, /* Toshiba Tecra A11-11L */
Seth Forshee25bded72011-11-07 19:53:36 -0800129 { { 0x73, 0x02, 0x64 }, 0x8a, ALPS_PROTO_V4, 0x8f, 0x8f, 0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130};
131
Kevin Cernekee24af5cb2013-02-13 22:22:08 -0800132static void alps_set_abs_params_st(struct alps_data *priv,
133 struct input_dev *dev1);
134static void alps_set_abs_params_mt(struct alps_data *priv,
135 struct input_dev *dev1);
136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137/*
138 * XXX - this entry is suspicious. First byte has zero lower nibble,
139 * which is what a normal mouse would report. Also, the value 0x0e
140 * isn't valid per PS/2 spec.
141 */
142
Seth Forsheed4b347b2011-11-07 19:53:15 -0800143/* Packet formats are described in Documentation/input/alps.txt */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800145static bool alps_is_valid_first_byte(struct alps_data *priv,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800146 unsigned char data)
147{
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800148 return (data & priv->mask0) == priv->byte0;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800149}
150
151static void alps_report_buttons(struct psmouse *psmouse,
152 struct input_dev *dev1, struct input_dev *dev2,
153 int left, int right, int middle)
154{
Martin Buckc91ed052010-03-13 22:23:58 -0800155 struct input_dev *dev;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800156
Martin Buckc91ed052010-03-13 22:23:58 -0800157 /*
158 * If shared button has already been reported on the
159 * other device (dev2) then this event should be also
160 * sent through that device.
161 */
162 dev = test_bit(BTN_LEFT, dev2->key) ? dev2 : dev1;
163 input_report_key(dev, BTN_LEFT, left);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800164
Martin Buckc91ed052010-03-13 22:23:58 -0800165 dev = test_bit(BTN_RIGHT, dev2->key) ? dev2 : dev1;
166 input_report_key(dev, BTN_RIGHT, right);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800167
Martin Buckc91ed052010-03-13 22:23:58 -0800168 dev = test_bit(BTN_MIDDLE, dev2->key) ? dev2 : dev1;
169 input_report_key(dev, BTN_MIDDLE, middle);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800170
Martin Buckc91ed052010-03-13 22:23:58 -0800171 /*
172 * Sync the _other_ device now, we'll do the first
173 * device later once we report the rest of the events.
174 */
175 input_sync(dev2);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800176}
177
Seth Forshee25bded72011-11-07 19:53:36 -0800178static void alps_process_packet_v1_v2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
180 struct alps_data *priv = psmouse->private;
181 unsigned char *packet = psmouse->packet;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500182 struct input_dev *dev = psmouse->dev;
183 struct input_dev *dev2 = priv->dev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 int x, y, z, ges, fin, left, right, middle;
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500185 int back = 0, forward = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800187 if (priv->proto_version == ALPS_PROTO_V1) {
Yotam Medinid2f40122006-05-29 23:30:36 -0400188 left = packet[2] & 0x10;
189 right = packet[2] & 0x08;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 middle = 0;
191 x = packet[1] | ((packet[0] & 0x07) << 7);
192 y = packet[4] | ((packet[3] & 0x07) << 7);
193 z = packet[5];
194 } else {
195 left = packet[3] & 1;
196 right = packet[3] & 2;
197 middle = packet[3] & 4;
198 x = packet[1] | ((packet[2] & 0x78) << (7 - 3));
199 y = packet[4] | ((packet[3] & 0x70) << (7 - 4));
200 z = packet[5];
201 }
202
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800203 if (priv->flags & ALPS_FW_BK_1) {
Laszlo Kajan3c00bb92008-03-18 00:39:55 -0400204 back = packet[0] & 0x10;
205 forward = packet[2] & 4;
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500206 }
207
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800208 if (priv->flags & ALPS_FW_BK_2) {
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500209 back = packet[3] & 4;
210 forward = packet[2] & 4;
211 if ((middle = forward && back))
212 forward = back = 0;
213 }
214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 ges = packet[2] & 1;
216 fin = packet[2] & 2;
217
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800218 if ((priv->flags & ALPS_DUALPOINT) && z == 127) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 input_report_rel(dev2, REL_X, (x > 383 ? (x - 768) : x));
220 input_report_rel(dev2, REL_Y, -(y > 255 ? (y - 512) : y));
Ulrich Dangeld7ed5d82009-06-11 00:15:09 -0700221
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800222 alps_report_buttons(psmouse, dev2, dev, left, right, middle);
Ulrich Dangeld7ed5d82009-06-11 00:15:09 -0700223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 input_sync(dev2);
225 return;
226 }
227
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800228 alps_report_buttons(psmouse, dev, dev2, left, right, middle);
Ulrich Dangeld7ed5d82009-06-11 00:15:09 -0700229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 /* Convert hardware tap to a reasonable Z value */
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800231 if (ges && !fin)
232 z = 40;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 /*
235 * A "tap and drag" operation is reported by the hardware as a transition
236 * from (!fin && ges) to (fin && ges). This should be translated to the
237 * sequence Z>0, Z==0, Z>0, so the Z==0 event has to be generated manually.
238 */
239 if (ges && fin && !priv->prev_fin) {
240 input_report_abs(dev, ABS_X, x);
241 input_report_abs(dev, ABS_Y, y);
242 input_report_abs(dev, ABS_PRESSURE, 0);
243 input_report_key(dev, BTN_TOOL_FINGER, 0);
244 input_sync(dev);
245 }
246 priv->prev_fin = fin;
247
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800248 if (z > 30)
249 input_report_key(dev, BTN_TOUCH, 1);
250 if (z < 25)
251 input_report_key(dev, BTN_TOUCH, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253 if (z > 0) {
254 input_report_abs(dev, ABS_X, x);
255 input_report_abs(dev, ABS_Y, y);
256 }
257
258 input_report_abs(dev, ABS_PRESSURE, z);
259 input_report_key(dev, BTN_TOOL_FINGER, z > 0);
260
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800261 if (priv->flags & ALPS_WHEEL)
Vojtech Pavlike6c047b2005-09-04 01:40:43 -0500262 input_report_rel(dev, REL_WHEEL, ((packet[2] << 1) & 0x08) - ((packet[0] >> 4) & 0x07));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800264 if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500265 input_report_key(dev, BTN_FORWARD, forward);
266 input_report_key(dev, BTN_BACK, back);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 }
268
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800269 if (priv->flags & ALPS_FOUR_BUTTONS) {
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800270 input_report_key(dev, BTN_0, packet[2] & 4);
271 input_report_key(dev, BTN_1, packet[0] & 0x10);
272 input_report_key(dev, BTN_2, packet[3] & 4);
273 input_report_key(dev, BTN_3, packet[0] & 0x20);
274 }
275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 input_sync(dev);
277}
278
Seth Forshee01ce6612011-11-07 19:54:13 -0800279/*
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800280 * Process bitmap data for V5 protocols. Return value is null.
281 *
282 * The bitmaps don't have enough data to track fingers, so this function
283 * only generates points representing a bounding box of at most two contacts.
284 * These two points are returned in x1, y1, x2, and y2.
285 */
286static void alps_process_bitmap_dolphin(struct alps_data *priv,
287 struct alps_fields *fields,
288 int *x1, int *y1, int *x2, int *y2)
289{
290 int box_middle_x, box_middle_y;
291 unsigned int x_map, y_map;
292 unsigned char start_bit, end_bit;
293 unsigned char x_msb, x_lsb, y_msb, y_lsb;
294
295 x_map = fields->x_map;
296 y_map = fields->y_map;
297
298 if (!x_map || !y_map)
299 return;
300
301 /* Get Most-significant and Least-significant bit */
302 x_msb = fls(x_map);
303 x_lsb = ffs(x_map);
304 y_msb = fls(y_map);
305 y_lsb = ffs(y_map);
306
307 /* Most-significant bit should never exceed max sensor line number */
308 if (x_msb > priv->x_bits || y_msb > priv->y_bits)
309 return;
310
311 *x1 = *y1 = *x2 = *y2 = 0;
312
313 if (fields->fingers > 1) {
314 start_bit = priv->x_bits - x_msb;
315 end_bit = priv->x_bits - x_lsb;
316 box_middle_x = (priv->x_max * (start_bit + end_bit)) /
317 (2 * (priv->x_bits - 1));
318
319 start_bit = y_lsb - 1;
320 end_bit = y_msb - 1;
321 box_middle_y = (priv->y_max * (start_bit + end_bit)) /
322 (2 * (priv->y_bits - 1));
323 *x1 = fields->x;
324 *y1 = fields->y;
325 *x2 = 2 * box_middle_x - *x1;
326 *y2 = 2 * box_middle_y - *y1;
327 }
328}
329
330/*
Seth Forshee01ce6612011-11-07 19:54:13 -0800331 * Process bitmap data from v3 and v4 protocols. Returns the number of
332 * fingers detected. A return value of 0 means at least one of the
333 * bitmaps was empty.
334 *
335 * The bitmaps don't have enough data to track fingers, so this function
336 * only generates points representing a bounding box of all contacts.
337 * These points are returned in x1, y1, x2, and y2 when the return value
338 * is greater than 0.
339 */
Kevin Cernekee7a9f73e2013-02-13 22:24:55 -0800340static int alps_process_bitmap(struct alps_data *priv,
341 unsigned int x_map, unsigned int y_map,
Seth Forshee01ce6612011-11-07 19:54:13 -0800342 int *x1, int *y1, int *x2, int *y2)
343{
344 struct alps_bitmap_point {
345 int start_bit;
346 int num_bits;
347 };
348
349 int fingers_x = 0, fingers_y = 0, fingers;
350 int i, bit, prev_bit;
351 struct alps_bitmap_point x_low = {0,}, x_high = {0,};
352 struct alps_bitmap_point y_low = {0,}, y_high = {0,};
353 struct alps_bitmap_point *point;
354
355 if (!x_map || !y_map)
356 return 0;
357
358 *x1 = *y1 = *x2 = *y2 = 0;
359
360 prev_bit = 0;
361 point = &x_low;
362 for (i = 0; x_map != 0; i++, x_map >>= 1) {
363 bit = x_map & 1;
364 if (bit) {
365 if (!prev_bit) {
366 point->start_bit = i;
367 fingers_x++;
368 }
369 point->num_bits++;
370 } else {
371 if (prev_bit)
372 point = &x_high;
373 else
374 point->num_bits = 0;
375 }
376 prev_bit = bit;
377 }
378
379 /*
380 * y bitmap is reversed for what we need (lower positions are in
381 * higher bits), so we process from the top end.
382 */
Kevin Cernekee7a9f73e2013-02-13 22:24:55 -0800383 y_map = y_map << (sizeof(y_map) * BITS_PER_BYTE - priv->y_bits);
Seth Forshee01ce6612011-11-07 19:54:13 -0800384 prev_bit = 0;
385 point = &y_low;
386 for (i = 0; y_map != 0; i++, y_map <<= 1) {
387 bit = y_map & (1 << (sizeof(y_map) * BITS_PER_BYTE - 1));
388 if (bit) {
389 if (!prev_bit) {
390 point->start_bit = i;
391 fingers_y++;
392 }
393 point->num_bits++;
394 } else {
395 if (prev_bit)
396 point = &y_high;
397 else
398 point->num_bits = 0;
399 }
400 prev_bit = bit;
401 }
402
403 /*
404 * Fingers can overlap, so we use the maximum count of fingers
405 * on either axis as the finger count.
406 */
407 fingers = max(fingers_x, fingers_y);
408
409 /*
410 * If total fingers is > 1 but either axis reports only a single
411 * contact, we have overlapping or adjacent fingers. For the
412 * purposes of creating a bounding box, divide the single contact
413 * (roughly) equally between the two points.
414 */
415 if (fingers > 1) {
416 if (fingers_x == 1) {
417 i = x_low.num_bits / 2;
418 x_low.num_bits = x_low.num_bits - i;
419 x_high.start_bit = x_low.start_bit + i;
420 x_high.num_bits = max(i, 1);
421 } else if (fingers_y == 1) {
422 i = y_low.num_bits / 2;
423 y_low.num_bits = y_low.num_bits - i;
424 y_high.start_bit = y_low.start_bit + i;
425 y_high.num_bits = max(i, 1);
426 }
427 }
428
Kevin Cernekee7a9f73e2013-02-13 22:24:55 -0800429 *x1 = (priv->x_max * (2 * x_low.start_bit + x_low.num_bits - 1)) /
430 (2 * (priv->x_bits - 1));
431 *y1 = (priv->y_max * (2 * y_low.start_bit + y_low.num_bits - 1)) /
432 (2 * (priv->y_bits - 1));
Seth Forshee01ce6612011-11-07 19:54:13 -0800433
434 if (fingers > 1) {
Kevin Cernekee7a9f73e2013-02-13 22:24:55 -0800435 *x2 = (priv->x_max *
436 (2 * x_high.start_bit + x_high.num_bits - 1)) /
437 (2 * (priv->x_bits - 1));
438 *y2 = (priv->y_max *
439 (2 * y_high.start_bit + y_high.num_bits - 1)) /
440 (2 * (priv->y_bits - 1));
Seth Forshee01ce6612011-11-07 19:54:13 -0800441 }
442
443 return fingers;
444}
445
446static void alps_set_slot(struct input_dev *dev, int slot, bool active,
447 int x, int y)
448{
449 input_mt_slot(dev, slot);
450 input_mt_report_slot_state(dev, MT_TOOL_FINGER, active);
451 if (active) {
452 input_report_abs(dev, ABS_MT_POSITION_X, x);
453 input_report_abs(dev, ABS_MT_POSITION_Y, y);
454 }
455}
456
457static void alps_report_semi_mt_data(struct input_dev *dev, int num_fingers,
458 int x1, int y1, int x2, int y2)
459{
460 alps_set_slot(dev, 0, num_fingers != 0, x1, y1);
461 alps_set_slot(dev, 1, num_fingers == 2, x2, y2);
462}
463
Seth Forshee25bded72011-11-07 19:53:36 -0800464static void alps_process_trackstick_packet_v3(struct psmouse *psmouse)
465{
466 struct alps_data *priv = psmouse->private;
467 unsigned char *packet = psmouse->packet;
468 struct input_dev *dev = priv->dev2;
469 int x, y, z, left, right, middle;
470
471 /* Sanity check packet */
472 if (!(packet[0] & 0x40)) {
473 psmouse_dbg(psmouse, "Bad trackstick packet, discarding\n");
474 return;
475 }
476
477 /*
478 * There's a special packet that seems to indicate the end
479 * of a stream of trackstick data. Filter these out.
480 */
481 if (packet[1] == 0x7f && packet[2] == 0x7f && packet[4] == 0x7f)
482 return;
483
484 x = (s8)(((packet[0] & 0x20) << 2) | (packet[1] & 0x7f));
485 y = (s8)(((packet[0] & 0x10) << 3) | (packet[2] & 0x7f));
486 z = (packet[4] & 0x7c) >> 2;
487
488 /*
489 * The x and y values tend to be quite large, and when used
490 * alone the trackstick is difficult to use. Scale them down
491 * to compensate.
492 */
493 x /= 8;
494 y /= 8;
495
496 input_report_rel(dev, REL_X, x);
497 input_report_rel(dev, REL_Y, -y);
498
499 /*
500 * Most ALPS models report the trackstick buttons in the touchpad
501 * packets, but a few report them here. No reliable way has been
502 * found to differentiate between the models upfront, so we enable
503 * the quirk in response to seeing a button press in the trackstick
504 * packet.
505 */
506 left = packet[3] & 0x01;
507 right = packet[3] & 0x02;
508 middle = packet[3] & 0x04;
509
510 if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) &&
511 (left || right || middle))
512 priv->quirks |= ALPS_QUIRK_TRACKSTICK_BUTTONS;
513
514 if (priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) {
515 input_report_key(dev, BTN_LEFT, left);
516 input_report_key(dev, BTN_RIGHT, right);
517 input_report_key(dev, BTN_MIDDLE, middle);
518 }
519
520 input_sync(dev);
521 return;
522}
523
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800524static void alps_decode_buttons_v3(struct alps_fields *f, unsigned char *p)
525{
526 f->left = !!(p[3] & 0x01);
527 f->right = !!(p[3] & 0x02);
528 f->middle = !!(p[3] & 0x04);
529
530 f->ts_left = !!(p[3] & 0x10);
531 f->ts_right = !!(p[3] & 0x20);
532 f->ts_middle = !!(p[3] & 0x40);
533}
534
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800535static void alps_decode_pinnacle(struct alps_fields *f, unsigned char *p,
536 struct psmouse *psmouse)
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800537{
538 f->first_mp = !!(p[4] & 0x40);
539 f->is_mp = !!(p[0] & 0x40);
540
541 f->fingers = (p[5] & 0x3) + 1;
542 f->x_map = ((p[4] & 0x7e) << 8) |
543 ((p[1] & 0x7f) << 2) |
544 ((p[0] & 0x30) >> 4);
545 f->y_map = ((p[3] & 0x70) << 4) |
546 ((p[2] & 0x7f) << 1) |
547 (p[4] & 0x01);
548
549 f->x = ((p[1] & 0x7f) << 4) | ((p[4] & 0x30) >> 2) |
550 ((p[0] & 0x30) >> 4);
551 f->y = ((p[2] & 0x7f) << 4) | (p[4] & 0x0f);
552 f->z = p[5] & 0x7f;
553
554 alps_decode_buttons_v3(f, p);
555}
556
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800557static void alps_decode_rushmore(struct alps_fields *f, unsigned char *p,
558 struct psmouse *psmouse)
Kevin Cernekee1302bac2013-02-13 22:27:08 -0800559{
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800560 alps_decode_pinnacle(f, p, psmouse);
Kevin Cernekee1302bac2013-02-13 22:27:08 -0800561
Yunkang Tangf105e342014-07-25 22:29:24 -0700562 /* Rushmore's packet decode has a bit difference with Pinnacle's */
563 f->is_mp = !!(p[5] & 0x40);
564 f->fingers = max((p[5] & 0x3), ((p[5] >> 2) & 0x3)) + 1;
Kevin Cernekee1302bac2013-02-13 22:27:08 -0800565 f->x_map |= (p[5] & 0x10) << 11;
566 f->y_map |= (p[5] & 0x20) << 6;
567}
568
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800569static void alps_decode_dolphin(struct alps_fields *f, unsigned char *p,
570 struct psmouse *psmouse)
Dave Turvene75af9e52013-02-21 22:58:28 -0800571{
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800572 u64 palm_data = 0;
573 struct alps_data *priv = psmouse->private;
574
Dave Turvene75af9e52013-02-21 22:58:28 -0800575 f->first_mp = !!(p[0] & 0x02);
576 f->is_mp = !!(p[0] & 0x20);
577
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800578 if (!f->is_mp) {
579 f->x = ((p[1] & 0x7f) | ((p[4] & 0x0f) << 7));
580 f->y = ((p[2] & 0x7f) | ((p[4] & 0xf0) << 3));
581 f->z = (p[0] & 4) ? 0 : p[5] & 0x7f;
582 alps_decode_buttons_v3(f, p);
583 } else {
584 f->fingers = ((p[0] & 0x6) >> 1 |
Dave Turvene75af9e52013-02-21 22:58:28 -0800585 (p[0] & 0x10) >> 2);
Dave Turvene75af9e52013-02-21 22:58:28 -0800586
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800587 palm_data = (p[1] & 0x7f) |
588 ((p[2] & 0x7f) << 7) |
589 ((p[4] & 0x7f) << 14) |
590 ((p[5] & 0x7f) << 21) |
591 ((p[3] & 0x07) << 28) |
592 (((u64)p[3] & 0x70) << 27) |
593 (((u64)p[0] & 0x01) << 34);
Dave Turvene75af9e52013-02-21 22:58:28 -0800594
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800595 /* Y-profile is stored in P(0) to p(n-1), n = y_bits; */
596 f->y_map = palm_data & (BIT(priv->y_bits) - 1);
597
598 /* X-profile is stored in p(n) to p(n+m-1), m = x_bits; */
599 f->x_map = (palm_data >> priv->y_bits) &
600 (BIT(priv->x_bits) - 1);
601 }
Dave Turvene75af9e52013-02-21 22:58:28 -0800602}
603
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800604static void alps_process_touchpad_packet_v3_v5(struct psmouse *psmouse)
Seth Forshee25bded72011-11-07 19:53:36 -0800605{
606 struct alps_data *priv = psmouse->private;
607 unsigned char *packet = psmouse->packet;
608 struct input_dev *dev = psmouse->dev;
609 struct input_dev *dev2 = priv->dev2;
Seth Forshee01ce6612011-11-07 19:54:13 -0800610 int x1 = 0, y1 = 0, x2 = 0, y2 = 0;
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800611 int fingers = 0, bmap_fn;
612 struct alps_fields f = {0};
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800613
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800614 priv->decode_fields(&f, packet, psmouse);
Seth Forshee25bded72011-11-07 19:53:36 -0800615
616 /*
Seth Forshee01ce6612011-11-07 19:54:13 -0800617 * There's no single feature of touchpad position and bitmap packets
618 * that can be used to distinguish between them. We rely on the fact
619 * that a bitmap packet should always follow a position packet with
620 * bit 6 of packet[4] set.
Seth Forshee25bded72011-11-07 19:53:36 -0800621 */
622 if (priv->multi_packet) {
Seth Forshee25bded72011-11-07 19:53:36 -0800623 /*
624 * Sometimes a position packet will indicate a multi-packet
625 * sequence, but then what follows is another position
626 * packet. Check for this, and when it happens process the
627 * position packet as usual.
628 */
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800629 if (f.is_mp) {
630 fingers = f.fingers;
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800631 if (priv->proto_version == ALPS_PROTO_V3) {
632 bmap_fn = alps_process_bitmap(priv, f.x_map,
633 f.y_map, &x1, &y1,
634 &x2, &y2);
Seth Forshee01ce6612011-11-07 19:54:13 -0800635
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800636 /*
637 * We shouldn't report more than one finger if
638 * we don't have two coordinates.
639 */
640 if (fingers > 1 && bmap_fn < 2)
641 fingers = bmap_fn;
Seth Forshee01ce6612011-11-07 19:54:13 -0800642
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800643 /* Now process position packet */
644 priv->decode_fields(&f, priv->multi_data,
645 psmouse);
646 } else {
647 /*
648 * Because Dolphin uses position packet's
649 * coordinate data as Pt1 and uses it to
650 * calculate Pt2, so we need to do position
651 * packet decode first.
652 */
653 priv->decode_fields(&f, priv->multi_data,
654 psmouse);
655
656 /*
657 * Since Dolphin's finger number is reliable,
658 * there is no need to compare with bmap_fn.
659 */
660 alps_process_bitmap_dolphin(priv, &f, &x1, &y1,
661 &x2, &y2);
662 }
Seth Forshee01ce6612011-11-07 19:54:13 -0800663 } else {
664 priv->multi_packet = 0;
Seth Forshee25bded72011-11-07 19:53:36 -0800665 }
666 }
667
Seth Forshee01ce6612011-11-07 19:54:13 -0800668 /*
669 * Bit 6 of byte 0 is not usually set in position packets. The only
670 * times it seems to be set is in situations where the data is
671 * suspect anyway, e.g. a palm resting flat on the touchpad. Given
672 * this combined with the fact that this bit is useful for filtering
673 * out misidentified bitmap packets, we reject anything with this
674 * bit set.
675 */
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800676 if (f.is_mp)
Seth Forshee01ce6612011-11-07 19:54:13 -0800677 return;
678
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800679 if (!priv->multi_packet && f.first_mp) {
Seth Forshee25bded72011-11-07 19:53:36 -0800680 priv->multi_packet = 1;
Seth Forshee01ce6612011-11-07 19:54:13 -0800681 memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
682 return;
683 }
684
685 priv->multi_packet = 0;
Seth Forshee25bded72011-11-07 19:53:36 -0800686
Seth Forshee25bded72011-11-07 19:53:36 -0800687 /*
688 * Sometimes the hardware sends a single packet with z = 0
689 * in the middle of a stream. Real releases generate packets
690 * with x, y, and z all zero, so these seem to be flukes.
691 * Ignore them.
692 */
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800693 if (f.x && f.y && !f.z)
Seth Forshee25bded72011-11-07 19:53:36 -0800694 return;
695
Seth Forshee01ce6612011-11-07 19:54:13 -0800696 /*
697 * If we don't have MT data or the bitmaps were empty, we have
698 * to rely on ST data.
699 */
700 if (!fingers) {
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800701 x1 = f.x;
702 y1 = f.y;
703 fingers = f.z > 0 ? 1 : 0;
Seth Forshee01ce6612011-11-07 19:54:13 -0800704 }
705
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800706 if (f.z >= 64)
Seth Forshee25bded72011-11-07 19:53:36 -0800707 input_report_key(dev, BTN_TOUCH, 1);
708 else
709 input_report_key(dev, BTN_TOUCH, 0);
710
Seth Forshee01ce6612011-11-07 19:54:13 -0800711 alps_report_semi_mt_data(dev, fingers, x1, y1, x2, y2);
712
Dmitry Torokhov616575c2012-05-10 22:31:59 -0700713 input_mt_report_finger_count(dev, fingers);
Seth Forshee01ce6612011-11-07 19:54:13 -0800714
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800715 input_report_key(dev, BTN_LEFT, f.left);
716 input_report_key(dev, BTN_RIGHT, f.right);
717 input_report_key(dev, BTN_MIDDLE, f.middle);
Seth Forshee01ce6612011-11-07 19:54:13 -0800718
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800719 if (f.z > 0) {
720 input_report_abs(dev, ABS_X, f.x);
721 input_report_abs(dev, ABS_Y, f.y);
Seth Forshee25bded72011-11-07 19:53:36 -0800722 }
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800723 input_report_abs(dev, ABS_PRESSURE, f.z);
Seth Forshee25bded72011-11-07 19:53:36 -0800724
Seth Forshee25bded72011-11-07 19:53:36 -0800725 input_sync(dev);
726
727 if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS)) {
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800728 input_report_key(dev2, BTN_LEFT, f.ts_left);
729 input_report_key(dev2, BTN_RIGHT, f.ts_right);
730 input_report_key(dev2, BTN_MIDDLE, f.ts_middle);
Seth Forshee25bded72011-11-07 19:53:36 -0800731 input_sync(dev2);
732 }
733}
734
735static void alps_process_packet_v3(struct psmouse *psmouse)
736{
737 unsigned char *packet = psmouse->packet;
738
739 /*
740 * v3 protocol packets come in three types, two representing
741 * touchpad data and one representing trackstick data.
742 * Trackstick packets seem to be distinguished by always
743 * having 0x3f in the last byte. This value has never been
744 * observed in the last byte of either of the other types
745 * of packets.
746 */
747 if (packet[5] == 0x3f) {
748 alps_process_trackstick_packet_v3(psmouse);
749 return;
750 }
751
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800752 alps_process_touchpad_packet_v3_v5(psmouse);
Seth Forshee25bded72011-11-07 19:53:36 -0800753}
754
Yunkang Tang95f75e92013-12-01 22:33:52 -0800755static void alps_process_packet_v6(struct psmouse *psmouse)
756{
757 struct alps_data *priv = psmouse->private;
758 unsigned char *packet = psmouse->packet;
759 struct input_dev *dev = psmouse->dev;
760 struct input_dev *dev2 = priv->dev2;
761 int x, y, z, left, right, middle;
762
763 /*
764 * We can use Byte5 to distinguish if the packet is from Touchpad
765 * or Trackpoint.
766 * Touchpad: 0 - 0x7E
767 * Trackpoint: 0x7F
768 */
769 if (packet[5] == 0x7F) {
770 /* It should be a DualPoint when received Trackpoint packet */
771 if (!(priv->flags & ALPS_DUALPOINT))
772 return;
773
774 /* Trackpoint packet */
775 x = packet[1] | ((packet[3] & 0x20) << 2);
776 y = packet[2] | ((packet[3] & 0x40) << 1);
777 z = packet[4];
778 left = packet[3] & 0x01;
779 right = packet[3] & 0x02;
780 middle = packet[3] & 0x04;
781
782 /* To prevent the cursor jump when finger lifted */
783 if (x == 0x7F && y == 0x7F && z == 0x7F)
784 x = y = z = 0;
785
786 /* Divide 4 since trackpoint's speed is too fast */
787 input_report_rel(dev2, REL_X, (char)x / 4);
788 input_report_rel(dev2, REL_Y, -((char)y / 4));
789
790 input_report_key(dev2, BTN_LEFT, left);
791 input_report_key(dev2, BTN_RIGHT, right);
792 input_report_key(dev2, BTN_MIDDLE, middle);
793
794 input_sync(dev2);
795 return;
796 }
797
798 /* Touchpad packet */
799 x = packet[1] | ((packet[3] & 0x78) << 4);
800 y = packet[2] | ((packet[4] & 0x78) << 4);
801 z = packet[5];
802 left = packet[3] & 0x01;
803 right = packet[3] & 0x02;
804
805 if (z > 30)
806 input_report_key(dev, BTN_TOUCH, 1);
807 if (z < 25)
808 input_report_key(dev, BTN_TOUCH, 0);
809
810 if (z > 0) {
811 input_report_abs(dev, ABS_X, x);
812 input_report_abs(dev, ABS_Y, y);
813 }
814
815 input_report_abs(dev, ABS_PRESSURE, z);
816 input_report_key(dev, BTN_TOOL_FINGER, z > 0);
817
818 /* v6 touchpad does not have middle button */
819 input_report_key(dev, BTN_LEFT, left);
820 input_report_key(dev, BTN_RIGHT, right);
821
822 input_sync(dev);
823}
824
Seth Forshee25bded72011-11-07 19:53:36 -0800825static void alps_process_packet_v4(struct psmouse *psmouse)
826{
George Pantalos3b7e09f2012-05-10 22:31:59 -0700827 struct alps_data *priv = psmouse->private;
Seth Forshee25bded72011-11-07 19:53:36 -0800828 unsigned char *packet = psmouse->packet;
829 struct input_dev *dev = psmouse->dev;
George Pantalos3b7e09f2012-05-10 22:31:59 -0700830 int offset;
Seth Forshee25bded72011-11-07 19:53:36 -0800831 int x, y, z;
832 int left, right;
George Pantalos3b7e09f2012-05-10 22:31:59 -0700833 int x1, y1, x2, y2;
834 int fingers = 0;
835 unsigned int x_bitmap, y_bitmap;
836
837 /*
838 * v4 has a 6-byte encoding for bitmap data, but this data is
839 * broken up between 3 normal packets. Use priv->multi_packet to
840 * track our position in the bitmap packet.
841 */
842 if (packet[6] & 0x40) {
843 /* sync, reset position */
844 priv->multi_packet = 0;
845 }
846
847 if (WARN_ON_ONCE(priv->multi_packet > 2))
848 return;
849
850 offset = 2 * priv->multi_packet;
851 priv->multi_data[offset] = packet[6];
852 priv->multi_data[offset + 1] = packet[7];
853
854 if (++priv->multi_packet > 2) {
855 priv->multi_packet = 0;
856
857 x_bitmap = ((priv->multi_data[2] & 0x1f) << 10) |
858 ((priv->multi_data[3] & 0x60) << 3) |
859 ((priv->multi_data[0] & 0x3f) << 2) |
860 ((priv->multi_data[1] & 0x60) >> 5);
861 y_bitmap = ((priv->multi_data[5] & 0x01) << 10) |
862 ((priv->multi_data[3] & 0x1f) << 5) |
863 (priv->multi_data[1] & 0x1f);
864
Kevin Cernekee7a9f73e2013-02-13 22:24:55 -0800865 fingers = alps_process_bitmap(priv, x_bitmap, y_bitmap,
George Pantalos3b7e09f2012-05-10 22:31:59 -0700866 &x1, &y1, &x2, &y2);
867
868 /* Store MT data.*/
869 priv->fingers = fingers;
870 priv->x1 = x1;
871 priv->x2 = x2;
872 priv->y1 = y1;
873 priv->y2 = y2;
874 }
Seth Forshee25bded72011-11-07 19:53:36 -0800875
876 left = packet[4] & 0x01;
877 right = packet[4] & 0x02;
878
879 x = ((packet[1] & 0x7f) << 4) | ((packet[3] & 0x30) >> 2) |
880 ((packet[0] & 0x30) >> 4);
881 y = ((packet[2] & 0x7f) << 4) | (packet[3] & 0x0f);
882 z = packet[5] & 0x7f;
883
George Pantalos3b7e09f2012-05-10 22:31:59 -0700884 /*
885 * If there were no contacts in the bitmap, use ST
886 * points in MT reports.
887 * If there were two contacts or more, report MT data.
888 */
889 if (priv->fingers < 2) {
890 x1 = x;
891 y1 = y;
892 fingers = z > 0 ? 1 : 0;
893 } else {
894 fingers = priv->fingers;
895 x1 = priv->x1;
896 x2 = priv->x2;
897 y1 = priv->y1;
898 y2 = priv->y2;
899 }
900
Seth Forshee25bded72011-11-07 19:53:36 -0800901 if (z >= 64)
902 input_report_key(dev, BTN_TOUCH, 1);
903 else
904 input_report_key(dev, BTN_TOUCH, 0);
905
George Pantalos3b7e09f2012-05-10 22:31:59 -0700906 alps_report_semi_mt_data(dev, fingers, x1, y1, x2, y2);
907
Dmitry Torokhov616575c2012-05-10 22:31:59 -0700908 input_mt_report_finger_count(dev, fingers);
George Pantalos3b7e09f2012-05-10 22:31:59 -0700909
910 input_report_key(dev, BTN_LEFT, left);
911 input_report_key(dev, BTN_RIGHT, right);
912
Seth Forshee25bded72011-11-07 19:53:36 -0800913 if (z > 0) {
914 input_report_abs(dev, ABS_X, x);
915 input_report_abs(dev, ABS_Y, y);
916 }
917 input_report_abs(dev, ABS_PRESSURE, z);
918
Seth Forshee25bded72011-11-07 19:53:36 -0800919 input_sync(dev);
920}
921
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800922static void alps_report_bare_ps2_packet(struct psmouse *psmouse,
923 unsigned char packet[],
924 bool report_buttons)
925{
926 struct alps_data *priv = psmouse->private;
927 struct input_dev *dev2 = priv->dev2;
928
929 if (report_buttons)
930 alps_report_buttons(psmouse, dev2, psmouse->dev,
931 packet[0] & 1, packet[0] & 2, packet[0] & 4);
932
933 input_report_rel(dev2, REL_X,
934 packet[1] ? packet[1] - ((packet[0] << 4) & 0x100) : 0);
935 input_report_rel(dev2, REL_Y,
936 packet[2] ? ((packet[0] << 3) & 0x100) - packet[2] : 0);
937
938 input_sync(dev2);
939}
940
941static psmouse_ret_t alps_handle_interleaved_ps2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942{
943 struct alps_data *priv = psmouse->private;
944
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800945 if (psmouse->pktcnt < 6)
946 return PSMOUSE_GOOD_DATA;
947
948 if (psmouse->pktcnt == 6) {
949 /*
950 * Start a timer to flush the packet if it ends up last
951 * 6-byte packet in the stream. Timer needs to fire
952 * psmouse core times out itself. 20 ms should be enough
953 * to decide if we are getting more data or not.
954 */
955 mod_timer(&priv->timer, jiffies + msecs_to_jiffies(20));
956 return PSMOUSE_GOOD_DATA;
957 }
958
959 del_timer(&priv->timer);
960
961 if (psmouse->packet[6] & 0x80) {
962
963 /*
964 * Highest bit is set - that means we either had
965 * complete ALPS packet and this is start of the
966 * next packet or we got garbage.
967 */
968
969 if (((psmouse->packet[3] |
970 psmouse->packet[4] |
971 psmouse->packet[5]) & 0x80) ||
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800972 (!alps_is_valid_first_byte(priv, psmouse->packet[6]))) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700973 psmouse_dbg(psmouse,
Andy Shevchenko3b112922012-10-30 00:24:41 -0700974 "refusing packet %4ph (suspected interleaved ps/2)\n",
975 psmouse->packet + 3);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800976 return PSMOUSE_BAD_DATA;
977 }
978
Kevin Cernekee24af5cb2013-02-13 22:22:08 -0800979 priv->process_packet(psmouse);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800980
981 /* Continue with the next packet */
982 psmouse->packet[0] = psmouse->packet[6];
983 psmouse->pktcnt = 1;
984
985 } else {
986
987 /*
988 * High bit is 0 - that means that we indeed got a PS/2
989 * packet in the middle of ALPS packet.
990 *
991 * There is also possibility that we got 6-byte ALPS
992 * packet followed by 3-byte packet from trackpoint. We
993 * can not distinguish between these 2 scenarios but
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700994 * because the latter is unlikely to happen in course of
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800995 * normal operation (user would need to press all
996 * buttons on the pad and start moving trackpoint
997 * without touching the pad surface) we assume former.
998 * Even if we are wrong the wost thing that would happen
999 * the cursor would jump but we should not get protocol
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001000 * de-synchronization.
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001001 */
1002
1003 alps_report_bare_ps2_packet(psmouse, &psmouse->packet[3],
1004 false);
1005
1006 /*
1007 * Continue with the standard ALPS protocol handling,
1008 * but make sure we won't process it as an interleaved
1009 * packet again, which may happen if all buttons are
1010 * pressed. To avoid this let's reset the 4th bit which
1011 * is normally 1.
1012 */
1013 psmouse->packet[3] = psmouse->packet[6] & 0xf7;
1014 psmouse->pktcnt = 4;
1015 }
1016
1017 return PSMOUSE_GOOD_DATA;
1018}
1019
1020static void alps_flush_packet(unsigned long data)
1021{
1022 struct psmouse *psmouse = (struct psmouse *)data;
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001023 struct alps_data *priv = psmouse->private;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001024
1025 serio_pause_rx(psmouse->ps2dev.serio);
1026
Seth Forsheeb46615f2011-11-07 19:53:30 -08001027 if (psmouse->pktcnt == psmouse->pktsize) {
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001028
1029 /*
1030 * We did not any more data in reasonable amount of time.
1031 * Validate the last 3 bytes and process as a standard
1032 * ALPS packet.
1033 */
1034 if ((psmouse->packet[3] |
1035 psmouse->packet[4] |
1036 psmouse->packet[5]) & 0x80) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001037 psmouse_dbg(psmouse,
Andy Shevchenko3b112922012-10-30 00:24:41 -07001038 "refusing packet %3ph (suspected interleaved ps/2)\n",
1039 psmouse->packet + 3);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001040 } else {
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001041 priv->process_packet(psmouse);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001042 }
1043 psmouse->pktcnt = 0;
1044 }
1045
1046 serio_continue_rx(psmouse->ps2dev.serio);
1047}
1048
1049static psmouse_ret_t alps_process_byte(struct psmouse *psmouse)
1050{
1051 struct alps_data *priv = psmouse->private;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001052
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 if ((psmouse->packet[0] & 0xc8) == 0x08) { /* PS/2 packet */
1054 if (psmouse->pktcnt == 3) {
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001055 alps_report_bare_ps2_packet(psmouse, psmouse->packet,
1056 true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 return PSMOUSE_FULL_PACKET;
1058 }
1059 return PSMOUSE_GOOD_DATA;
1060 }
1061
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001062 /* Check for PS/2 packet stuffed in the middle of ALPS packet. */
1063
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001064 if ((priv->flags & ALPS_PS2_INTERLEAVED) &&
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001065 psmouse->pktcnt >= 4 && (psmouse->packet[3] & 0x0f) == 0x0f) {
1066 return alps_handle_interleaved_ps2(psmouse);
1067 }
1068
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001069 if (!alps_is_valid_first_byte(priv, psmouse->packet[0])) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001070 psmouse_dbg(psmouse,
1071 "refusing packet[0] = %x (mask0 = %x, byte0 = %x)\n",
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001072 psmouse->packet[0], priv->mask0, priv->byte0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 return PSMOUSE_BAD_DATA;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001074 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
Seth Forsheeb46615f2011-11-07 19:53:30 -08001076 /* Bytes 2 - pktsize should have 0 in the highest bit */
Yunkang Tang95f75e92013-12-01 22:33:52 -08001077 if ((priv->proto_version < ALPS_PROTO_V5) &&
Dave Turvene75af9e52013-02-21 22:58:28 -08001078 psmouse->pktcnt >= 2 && psmouse->pktcnt <= psmouse->pktsize &&
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001079 (psmouse->packet[psmouse->pktcnt - 1] & 0x80)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001080 psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
1081 psmouse->pktcnt - 1,
1082 psmouse->packet[psmouse->pktcnt - 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 return PSMOUSE_BAD_DATA;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001084 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
Seth Forsheeb46615f2011-11-07 19:53:30 -08001086 if (psmouse->pktcnt == psmouse->pktsize) {
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001087 priv->process_packet(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 return PSMOUSE_FULL_PACKET;
1089 }
1090
1091 return PSMOUSE_GOOD_DATA;
1092}
1093
Seth Forshee25bded72011-11-07 19:53:36 -08001094static int alps_command_mode_send_nibble(struct psmouse *psmouse, int nibble)
1095{
1096 struct ps2dev *ps2dev = &psmouse->ps2dev;
1097 struct alps_data *priv = psmouse->private;
1098 int command;
1099 unsigned char *param;
1100 unsigned char dummy[4];
1101
1102 BUG_ON(nibble > 0xf);
1103
1104 command = priv->nibble_commands[nibble].command;
1105 param = (command & 0x0f00) ?
1106 dummy : (unsigned char *)&priv->nibble_commands[nibble].data;
1107
1108 if (ps2_command(ps2dev, param, command))
1109 return -1;
1110
1111 return 0;
1112}
1113
1114static int alps_command_mode_set_addr(struct psmouse *psmouse, int addr)
1115{
1116 struct ps2dev *ps2dev = &psmouse->ps2dev;
1117 struct alps_data *priv = psmouse->private;
1118 int i, nibble;
1119
1120 if (ps2_command(ps2dev, NULL, priv->addr_command))
1121 return -1;
1122
1123 for (i = 12; i >= 0; i -= 4) {
1124 nibble = (addr >> i) & 0xf;
1125 if (alps_command_mode_send_nibble(psmouse, nibble))
1126 return -1;
1127 }
1128
1129 return 0;
1130}
1131
1132static int __alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
1133{
1134 struct ps2dev *ps2dev = &psmouse->ps2dev;
1135 unsigned char param[4];
1136
1137 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1138 return -1;
1139
1140 /*
1141 * The address being read is returned in the first two bytes
1142 * of the result. Check that this address matches the expected
1143 * address.
1144 */
1145 if (addr != ((param[0] << 8) | param[1]))
1146 return -1;
1147
1148 return param[2];
1149}
1150
1151static int alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
1152{
1153 if (alps_command_mode_set_addr(psmouse, addr))
1154 return -1;
1155 return __alps_command_mode_read_reg(psmouse, addr);
1156}
1157
1158static int __alps_command_mode_write_reg(struct psmouse *psmouse, u8 value)
1159{
1160 if (alps_command_mode_send_nibble(psmouse, (value >> 4) & 0xf))
1161 return -1;
1162 if (alps_command_mode_send_nibble(psmouse, value & 0xf))
1163 return -1;
1164 return 0;
1165}
1166
1167static int alps_command_mode_write_reg(struct psmouse *psmouse, int addr,
1168 u8 value)
1169{
1170 if (alps_command_mode_set_addr(psmouse, addr))
1171 return -1;
1172 return __alps_command_mode_write_reg(psmouse, value);
1173}
1174
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001175static int alps_rpt_cmd(struct psmouse *psmouse, int init_command,
1176 int repeated_command, unsigned char *param)
1177{
1178 struct ps2dev *ps2dev = &psmouse->ps2dev;
1179
1180 param[0] = 0;
1181 if (init_command && ps2_command(ps2dev, param, init_command))
1182 return -EIO;
1183
1184 if (ps2_command(ps2dev, NULL, repeated_command) ||
1185 ps2_command(ps2dev, NULL, repeated_command) ||
1186 ps2_command(ps2dev, NULL, repeated_command))
1187 return -EIO;
1188
1189 param[0] = param[1] = param[2] = 0xff;
1190 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1191 return -EIO;
1192
Dmitry Torokhov39fbe582013-02-14 09:04:24 -08001193 psmouse_dbg(psmouse, "%2.2X report: %3ph\n",
1194 repeated_command, param);
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001195 return 0;
1196}
1197
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08001198static int alps_enter_command_mode(struct psmouse *psmouse)
Seth Forshee25bded72011-11-07 19:53:36 -08001199{
1200 unsigned char param[4];
Seth Forshee25bded72011-11-07 19:53:36 -08001201
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001202 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_RESET_WRAP, param)) {
Seth Forshee25bded72011-11-07 19:53:36 -08001203 psmouse_err(psmouse, "failed to enter command mode\n");
1204 return -1;
1205 }
1206
Dave Turvene75af9e52013-02-21 22:58:28 -08001207 if ((param[0] != 0x88 || (param[1] != 0x07 && param[1] != 0x08)) &&
1208 param[0] != 0x73) {
Seth Forshee25bded72011-11-07 19:53:36 -08001209 psmouse_dbg(psmouse,
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001210 "unknown response while entering command mode\n");
Seth Forshee25bded72011-11-07 19:53:36 -08001211 return -1;
1212 }
Seth Forshee25bded72011-11-07 19:53:36 -08001213 return 0;
1214}
1215
1216static inline int alps_exit_command_mode(struct psmouse *psmouse)
1217{
1218 struct ps2dev *ps2dev = &psmouse->ps2dev;
1219 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM))
1220 return -1;
1221 return 0;
1222}
1223
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224/*
1225 * For DualPoint devices select the device that should respond to
1226 * subsequent commands. It looks like glidepad is behind stickpointer,
1227 * I'd thought it would be other way around...
1228 */
Seth Forshee25bded72011-11-07 19:53:36 -08001229static int alps_passthrough_mode_v2(struct psmouse *psmouse, bool enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230{
1231 struct ps2dev *ps2dev = &psmouse->ps2dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 int cmd = enable ? PSMOUSE_CMD_SETSCALE21 : PSMOUSE_CMD_SETSCALE11;
1233
1234 if (ps2_command(ps2dev, NULL, cmd) ||
1235 ps2_command(ps2dev, NULL, cmd) ||
1236 ps2_command(ps2dev, NULL, cmd) ||
1237 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1238 return -1;
1239
1240 /* we may get 3 more bytes, just ignore them */
Dmitry Torokhovc6117632005-06-01 02:39:51 -05001241 ps2_drain(ps2dev, 3, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
1243 return 0;
1244}
1245
Seth Forshee25bded72011-11-07 19:53:36 -08001246static int alps_absolute_mode_v1_v2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247{
1248 struct ps2dev *ps2dev = &psmouse->ps2dev;
1249
1250 /* Try ALPS magic knock - 4 disable before enable */
1251 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1252 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1253 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1254 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1255 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
1256 return -1;
1257
1258 /*
1259 * Switch mouse to poll (remote) mode so motion data will not
1260 * get in our way
1261 */
1262 return ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETPOLL);
1263}
1264
Yunkang Tang95f75e92013-12-01 22:33:52 -08001265static int alps_monitor_mode_send_word(struct psmouse *psmouse, u16 word)
1266{
1267 int i, nibble;
1268
1269 /*
1270 * b0-b11 are valid bits, send sequence is inverse.
1271 * e.g. when word = 0x0123, nibble send sequence is 3, 2, 1
1272 */
1273 for (i = 0; i <= 8; i += 4) {
1274 nibble = (word >> i) & 0xf;
1275 if (alps_command_mode_send_nibble(psmouse, nibble))
1276 return -1;
1277 }
1278
1279 return 0;
1280}
1281
1282static int alps_monitor_mode_write_reg(struct psmouse *psmouse,
1283 u16 addr, u16 value)
1284{
1285 struct ps2dev *ps2dev = &psmouse->ps2dev;
1286
1287 /* 0x0A0 is the command to write the word */
1288 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE) ||
1289 alps_monitor_mode_send_word(psmouse, 0x0A0) ||
1290 alps_monitor_mode_send_word(psmouse, addr) ||
1291 alps_monitor_mode_send_word(psmouse, value) ||
1292 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1293 return -1;
1294
1295 return 0;
1296}
1297
1298static int alps_monitor_mode(struct psmouse *psmouse, bool enable)
1299{
1300 struct ps2dev *ps2dev = &psmouse->ps2dev;
1301
1302 if (enable) {
1303 /* EC E9 F5 F5 E7 E6 E7 E9 to enter monitor mode */
1304 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
1305 ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO) ||
1306 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1307 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1308 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1309 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1310 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1311 ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO))
1312 return -1;
1313 } else {
1314 /* EC to exit monitor mode */
1315 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP))
1316 return -1;
1317 }
1318
1319 return 0;
1320}
1321
1322static int alps_absolute_mode_v6(struct psmouse *psmouse)
1323{
1324 u16 reg_val = 0x181;
1325 int ret = -1;
1326
1327 /* enter monitor mode, to write the register */
1328 if (alps_monitor_mode(psmouse, true))
1329 return -1;
1330
1331 ret = alps_monitor_mode_write_reg(psmouse, 0x000, reg_val);
1332
1333 if (alps_monitor_mode(psmouse, false))
1334 ret = -1;
1335
1336 return ret;
1337}
1338
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339static int alps_get_status(struct psmouse *psmouse, char *param)
1340{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 /* Get status: 0xF5 0xF5 0xF5 0xE9 */
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001342 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_DISABLE, param))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 return -1;
1344
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 return 0;
1346}
1347
1348/*
1349 * Turn touchpad tapping on or off. The sequences are:
1350 * 0xE9 0xF5 0xF5 0xF3 0x0A to enable,
1351 * 0xE9 0xF5 0xF5 0xE8 0x00 to disable.
1352 * My guess that 0xE9 (GetInfo) is here as a sync point.
1353 * For models that also have stickpointer (DualPoints) its tapping
1354 * is controlled separately (0xE6 0xE6 0xE6 0xF3 0x14|0x0A) but
1355 * we don't fiddle with it.
1356 */
1357static int alps_tap_mode(struct psmouse *psmouse, int enable)
1358{
1359 struct ps2dev *ps2dev = &psmouse->ps2dev;
1360 int cmd = enable ? PSMOUSE_CMD_SETRATE : PSMOUSE_CMD_SETRES;
1361 unsigned char tap_arg = enable ? 0x0A : 0x00;
1362 unsigned char param[4];
1363
1364 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO) ||
1365 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1366 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1367 ps2_command(ps2dev, &tap_arg, cmd))
1368 return -1;
1369
1370 if (alps_get_status(psmouse, param))
1371 return -1;
1372
1373 return 0;
1374}
1375
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001376/*
1377 * alps_poll() - poll the touchpad for current motion packet.
1378 * Used in resync.
1379 */
1380static int alps_poll(struct psmouse *psmouse)
1381{
1382 struct alps_data *priv = psmouse->private;
Seth Forsheeb46615f2011-11-07 19:53:30 -08001383 unsigned char buf[sizeof(psmouse->packet)];
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001384 bool poll_failed;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001385
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001386 if (priv->flags & ALPS_PASS)
Seth Forshee25bded72011-11-07 19:53:36 -08001387 alps_passthrough_mode_v2(psmouse, true);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001388
1389 poll_failed = ps2_command(&psmouse->ps2dev, buf,
1390 PSMOUSE_CMD_POLL | (psmouse->pktsize << 8)) < 0;
1391
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001392 if (priv->flags & ALPS_PASS)
Seth Forshee25bded72011-11-07 19:53:36 -08001393 alps_passthrough_mode_v2(psmouse, false);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001394
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001395 if (poll_failed || (buf[0] & priv->mask0) != priv->byte0)
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001396 return -1;
1397
1398 if ((psmouse->badbyte & 0xc8) == 0x08) {
1399/*
1400 * Poll the track stick ...
1401 */
1402 if (ps2_command(&psmouse->ps2dev, buf, PSMOUSE_CMD_POLL | (3 << 8)))
1403 return -1;
1404 }
1405
1406 memcpy(psmouse->packet, buf, sizeof(buf));
1407 return 0;
1408}
1409
Seth Forshee25bded72011-11-07 19:53:36 -08001410static int alps_hw_init_v1_v2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411{
1412 struct alps_data *priv = psmouse->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001414 if ((priv->flags & ALPS_PASS) &&
Seth Forshee25bded72011-11-07 19:53:36 -08001415 alps_passthrough_mode_v2(psmouse, true)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 return -1;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001417 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001419 if (alps_tap_mode(psmouse, true)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001420 psmouse_warn(psmouse, "Failed to enable hardware tapping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 return -1;
Peter Osterlund963f6262005-07-11 01:08:04 -05001422 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
Seth Forshee25bded72011-11-07 19:53:36 -08001424 if (alps_absolute_mode_v1_v2(psmouse)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001425 psmouse_err(psmouse, "Failed to enable absolute mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 return -1;
1427 }
1428
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001429 if ((priv->flags & ALPS_PASS) &&
Seth Forshee25bded72011-11-07 19:53:36 -08001430 alps_passthrough_mode_v2(psmouse, false)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 return -1;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04001434 /* ALPS needs stream mode, otherwise it won't report any data */
1435 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001436 psmouse_err(psmouse, "Failed to enable stream mode\n");
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04001437 return -1;
1438 }
1439
1440 return 0;
1441}
1442
Yunkang Tang95f75e92013-12-01 22:33:52 -08001443static int alps_hw_init_v6(struct psmouse *psmouse)
1444{
1445 unsigned char param[2] = {0xC8, 0x14};
1446
1447 /* Enter passthrough mode to let trackpoint enter 6byte raw mode */
1448 if (alps_passthrough_mode_v2(psmouse, true))
1449 return -1;
1450
1451 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1452 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1453 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1454 ps2_command(&psmouse->ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
1455 ps2_command(&psmouse->ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
1456 return -1;
1457
1458 if (alps_passthrough_mode_v2(psmouse, false))
1459 return -1;
1460
1461 if (alps_absolute_mode_v6(psmouse)) {
1462 psmouse_err(psmouse, "Failed to enable absolute mode\n");
1463 return -1;
1464 }
1465
1466 return 0;
1467}
1468
Seth Forshee25bded72011-11-07 19:53:36 -08001469/*
Kevin Cernekeecd401202013-02-13 22:28:07 -08001470 * Enable or disable passthrough mode to the trackstick.
Seth Forshee25bded72011-11-07 19:53:36 -08001471 */
Kevin Cernekeecd401202013-02-13 22:28:07 -08001472static int alps_passthrough_mode_v3(struct psmouse *psmouse,
1473 int reg_base, bool enable)
Seth Forshee25bded72011-11-07 19:53:36 -08001474{
Kevin Cernekeecd401202013-02-13 22:28:07 -08001475 int reg_val, ret = -1;
Seth Forshee25bded72011-11-07 19:53:36 -08001476
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08001477 if (alps_enter_command_mode(psmouse))
Seth Forshee25bded72011-11-07 19:53:36 -08001478 return -1;
1479
Kevin Cernekeecd401202013-02-13 22:28:07 -08001480 reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x0008);
1481 if (reg_val == -1)
1482 goto error;
1483
Seth Forshee25bded72011-11-07 19:53:36 -08001484 if (enable)
1485 reg_val |= 0x01;
1486 else
1487 reg_val &= ~0x01;
1488
Kevin Cernekeecd401202013-02-13 22:28:07 -08001489 ret = __alps_command_mode_write_reg(psmouse, reg_val);
Seth Forshee25bded72011-11-07 19:53:36 -08001490
Kevin Cernekeecd401202013-02-13 22:28:07 -08001491error:
1492 if (alps_exit_command_mode(psmouse))
1493 ret = -1;
1494 return ret;
Seth Forshee25bded72011-11-07 19:53:36 -08001495}
1496
1497/* Must be in command mode when calling this function */
1498static int alps_absolute_mode_v3(struct psmouse *psmouse)
1499{
1500 int reg_val;
1501
1502 reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
1503 if (reg_val == -1)
1504 return -1;
1505
1506 reg_val |= 0x06;
1507 if (__alps_command_mode_write_reg(psmouse, reg_val))
1508 return -1;
1509
1510 return 0;
1511}
1512
Kevin Cernekeecd401202013-02-13 22:28:07 -08001513static int alps_probe_trackstick_v3(struct psmouse *psmouse, int reg_base)
1514{
1515 int ret = -EIO, reg_val;
1516
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08001517 if (alps_enter_command_mode(psmouse))
Kevin Cernekeecd401202013-02-13 22:28:07 -08001518 goto error;
1519
1520 reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x08);
1521 if (reg_val == -1)
1522 goto error;
1523
1524 /* bit 7: trackstick is present */
1525 ret = reg_val & 0x80 ? 0 : -ENODEV;
1526
1527error:
1528 alps_exit_command_mode(psmouse);
1529 return ret;
1530}
1531
1532static int alps_setup_trackstick_v3(struct psmouse *psmouse, int reg_base)
1533{
1534 struct ps2dev *ps2dev = &psmouse->ps2dev;
1535 int ret = 0;
1536 unsigned char param[4];
1537
1538 if (alps_passthrough_mode_v3(psmouse, reg_base, true))
1539 return -EIO;
1540
1541 /*
1542 * E7 report for the trackstick
1543 *
1544 * There have been reports of failures to seem to trace back
1545 * to the above trackstick check failing. When these occur
1546 * this E7 report fails, so when that happens we continue
1547 * with the assumption that there isn't a trackstick after
1548 * all.
1549 */
1550 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_SETSCALE21, param)) {
1551 psmouse_warn(psmouse, "trackstick E7 report failed\n");
1552 ret = -ENODEV;
1553 } else {
Dmitry Torokhov39fbe582013-02-14 09:04:24 -08001554 psmouse_dbg(psmouse, "trackstick E7 report: %3ph\n", param);
Kevin Cernekeecd401202013-02-13 22:28:07 -08001555
1556 /*
1557 * Not sure what this does, but it is absolutely
1558 * essential. Without it, the touchpad does not
1559 * work at all and the trackstick just emits normal
1560 * PS/2 packets.
1561 */
1562 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1563 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1564 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1565 alps_command_mode_send_nibble(psmouse, 0x9) ||
1566 alps_command_mode_send_nibble(psmouse, 0x4)) {
1567 psmouse_err(psmouse,
1568 "Error sending magic E6 sequence\n");
1569 ret = -EIO;
1570 goto error;
1571 }
1572
1573 /*
1574 * This ensures the trackstick packets are in the format
1575 * supported by this driver. If bit 1 isn't set the packet
1576 * format is different.
1577 */
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08001578 if (alps_enter_command_mode(psmouse) ||
Kevin Cernekeecd401202013-02-13 22:28:07 -08001579 alps_command_mode_write_reg(psmouse,
1580 reg_base + 0x08, 0x82) ||
1581 alps_exit_command_mode(psmouse))
1582 ret = -EIO;
1583 }
1584
1585error:
1586 if (alps_passthrough_mode_v3(psmouse, reg_base, false))
1587 ret = -EIO;
1588
1589 return ret;
1590}
1591
Seth Forshee25bded72011-11-07 19:53:36 -08001592static int alps_hw_init_v3(struct psmouse *psmouse)
1593{
Seth Forshee25bded72011-11-07 19:53:36 -08001594 struct ps2dev *ps2dev = &psmouse->ps2dev;
1595 int reg_val;
1596 unsigned char param[4];
1597
Kevin Cernekeecd401202013-02-13 22:28:07 -08001598 reg_val = alps_probe_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE);
1599 if (reg_val == -EIO)
1600 goto error;
Dmitry Torokhov39fbe582013-02-14 09:04:24 -08001601
Kevin Cernekeecd401202013-02-13 22:28:07 -08001602 if (reg_val == 0 &&
1603 alps_setup_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE) == -EIO)
Seth Forshee25bded72011-11-07 19:53:36 -08001604 goto error;
1605
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08001606 if (alps_enter_command_mode(psmouse) ||
Kevin Cernekeecd401202013-02-13 22:28:07 -08001607 alps_absolute_mode_v3(psmouse)) {
Seth Forshee25bded72011-11-07 19:53:36 -08001608 psmouse_err(psmouse, "Failed to enter absolute mode\n");
1609 goto error;
1610 }
1611
1612 reg_val = alps_command_mode_read_reg(psmouse, 0x0006);
1613 if (reg_val == -1)
1614 goto error;
1615 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
1616 goto error;
1617
1618 reg_val = alps_command_mode_read_reg(psmouse, 0x0007);
1619 if (reg_val == -1)
1620 goto error;
1621 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
1622 goto error;
1623
1624 if (alps_command_mode_read_reg(psmouse, 0x0144) == -1)
1625 goto error;
1626 if (__alps_command_mode_write_reg(psmouse, 0x04))
1627 goto error;
1628
1629 if (alps_command_mode_read_reg(psmouse, 0x0159) == -1)
1630 goto error;
1631 if (__alps_command_mode_write_reg(psmouse, 0x03))
1632 goto error;
1633
1634 if (alps_command_mode_read_reg(psmouse, 0x0163) == -1)
1635 goto error;
1636 if (alps_command_mode_write_reg(psmouse, 0x0163, 0x03))
1637 goto error;
1638
1639 if (alps_command_mode_read_reg(psmouse, 0x0162) == -1)
1640 goto error;
1641 if (alps_command_mode_write_reg(psmouse, 0x0162, 0x04))
1642 goto error;
1643
Seth Forshee25bded72011-11-07 19:53:36 -08001644 alps_exit_command_mode(psmouse);
1645
1646 /* Set rate and enable data reporting */
1647 param[0] = 0x64;
1648 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
1649 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
1650 psmouse_err(psmouse, "Failed to enable data reporting\n");
1651 return -1;
1652 }
1653
1654 return 0;
1655
Seth Forshee25bded72011-11-07 19:53:36 -08001656error:
1657 /*
1658 * Leaving the touchpad in command mode will essentially render
1659 * it unusable until the machine reboots, so exit it here just
1660 * to be safe
1661 */
1662 alps_exit_command_mode(psmouse);
1663 return -1;
1664}
1665
Kevin Cernekee1302bac2013-02-13 22:27:08 -08001666static int alps_hw_init_rushmore_v3(struct psmouse *psmouse)
1667{
Kevin Cernekeecd401202013-02-13 22:28:07 -08001668 struct alps_data *priv = psmouse->private;
Kevin Cernekee1302bac2013-02-13 22:27:08 -08001669 struct ps2dev *ps2dev = &psmouse->ps2dev;
1670 int reg_val, ret = -1;
1671
Kevin Cernekeecd401202013-02-13 22:28:07 -08001672 if (priv->flags & ALPS_DUALPOINT) {
1673 reg_val = alps_setup_trackstick_v3(psmouse,
1674 ALPS_REG_BASE_RUSHMORE);
1675 if (reg_val == -EIO)
1676 goto error;
1677 if (reg_val == -ENODEV)
1678 priv->flags &= ~ALPS_DUALPOINT;
1679 }
1680
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08001681 if (alps_enter_command_mode(psmouse) ||
Kevin Cernekee1302bac2013-02-13 22:27:08 -08001682 alps_command_mode_read_reg(psmouse, 0xc2d9) == -1 ||
1683 alps_command_mode_write_reg(psmouse, 0xc2cb, 0x00))
1684 goto error;
1685
1686 reg_val = alps_command_mode_read_reg(psmouse, 0xc2c6);
1687 if (reg_val == -1)
1688 goto error;
1689 if (__alps_command_mode_write_reg(psmouse, reg_val & 0xfd))
1690 goto error;
1691
1692 if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
1693 goto error;
1694
1695 /* enter absolute mode */
1696 reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
1697 if (reg_val == -1)
1698 goto error;
1699 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
1700 goto error;
1701
1702 alps_exit_command_mode(psmouse);
1703 return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
1704
1705error:
1706 alps_exit_command_mode(psmouse);
1707 return ret;
1708}
1709
Seth Forshee25bded72011-11-07 19:53:36 -08001710/* Must be in command mode when calling this function */
1711static int alps_absolute_mode_v4(struct psmouse *psmouse)
1712{
1713 int reg_val;
1714
1715 reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
1716 if (reg_val == -1)
1717 return -1;
1718
1719 reg_val |= 0x02;
1720 if (__alps_command_mode_write_reg(psmouse, reg_val))
1721 return -1;
1722
1723 return 0;
1724}
1725
1726static int alps_hw_init_v4(struct psmouse *psmouse)
1727{
Seth Forshee25bded72011-11-07 19:53:36 -08001728 struct ps2dev *ps2dev = &psmouse->ps2dev;
1729 unsigned char param[4];
1730
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08001731 if (alps_enter_command_mode(psmouse))
Seth Forshee25bded72011-11-07 19:53:36 -08001732 goto error;
1733
1734 if (alps_absolute_mode_v4(psmouse)) {
1735 psmouse_err(psmouse, "Failed to enter absolute mode\n");
1736 goto error;
1737 }
1738
1739 if (alps_command_mode_write_reg(psmouse, 0x0007, 0x8c))
1740 goto error;
1741
1742 if (alps_command_mode_write_reg(psmouse, 0x0149, 0x03))
1743 goto error;
1744
1745 if (alps_command_mode_write_reg(psmouse, 0x0160, 0x03))
1746 goto error;
1747
1748 if (alps_command_mode_write_reg(psmouse, 0x017f, 0x15))
1749 goto error;
1750
1751 if (alps_command_mode_write_reg(psmouse, 0x0151, 0x01))
1752 goto error;
1753
1754 if (alps_command_mode_write_reg(psmouse, 0x0168, 0x03))
1755 goto error;
1756
1757 if (alps_command_mode_write_reg(psmouse, 0x014a, 0x03))
1758 goto error;
1759
1760 if (alps_command_mode_write_reg(psmouse, 0x0161, 0x03))
1761 goto error;
1762
1763 alps_exit_command_mode(psmouse);
1764
1765 /*
1766 * This sequence changes the output from a 9-byte to an
1767 * 8-byte format. All the same data seems to be present,
1768 * just in a more compact format.
1769 */
1770 param[0] = 0xc8;
1771 param[1] = 0x64;
1772 param[2] = 0x50;
1773 if (ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
1774 ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE) ||
1775 ps2_command(ps2dev, &param[2], PSMOUSE_CMD_SETRATE) ||
1776 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
1777 return -1;
1778
1779 /* Set rate and enable data reporting */
1780 param[0] = 0x64;
1781 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
1782 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
1783 psmouse_err(psmouse, "Failed to enable data reporting\n");
1784 return -1;
1785 }
1786
1787 return 0;
1788
1789error:
1790 /*
1791 * Leaving the touchpad in command mode will essentially render
1792 * it unusable until the machine reboots, so exit it here just
1793 * to be safe
1794 */
1795 alps_exit_command_mode(psmouse);
1796 return -1;
1797}
1798
Yunkang Tangee65d4b2013-12-26 14:54:19 -08001799static int alps_dolphin_get_device_area(struct psmouse *psmouse,
1800 struct alps_data *priv)
1801{
1802 struct ps2dev *ps2dev = &psmouse->ps2dev;
1803 unsigned char param[4] = {0};
1804 int num_x_electrode, num_y_electrode;
1805
1806 if (alps_enter_command_mode(psmouse))
1807 return -1;
1808
1809 param[0] = 0x0a;
1810 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
1811 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
1812 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
1813 ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
1814 ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE))
1815 return -1;
1816
1817 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1818 return -1;
1819
1820 /*
1821 * Dolphin's sensor line number is not fixed. It can be calculated
1822 * by adding the device's register value with DOLPHIN_PROFILE_X/YOFFSET.
1823 * Further more, we can get device's x_max and y_max by multiplying
1824 * sensor line number with DOLPHIN_COUNT_PER_ELECTRODE.
1825 *
1826 * e.g. When we get register's sensor_x = 11 & sensor_y = 8,
1827 * real sensor line number X = 11 + 8 = 19, and
1828 * real sensor line number Y = 8 + 1 = 9.
1829 * So, x_max = (19 - 1) * 64 = 1152, and
1830 * y_max = (9 - 1) * 64 = 512.
1831 */
1832 num_x_electrode = DOLPHIN_PROFILE_XOFFSET + (param[2] & 0x0F);
1833 num_y_electrode = DOLPHIN_PROFILE_YOFFSET + ((param[2] >> 4) & 0x0F);
1834 priv->x_bits = num_x_electrode;
1835 priv->y_bits = num_y_electrode;
1836 priv->x_max = (num_x_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
1837 priv->y_max = (num_y_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
1838
1839 if (alps_exit_command_mode(psmouse))
1840 return -1;
1841
1842 return 0;
1843}
1844
Dave Turvene75af9e52013-02-21 22:58:28 -08001845static int alps_hw_init_dolphin_v1(struct psmouse *psmouse)
1846{
1847 struct ps2dev *ps2dev = &psmouse->ps2dev;
1848 unsigned char param[2];
1849
1850 /* This is dolphin "v1" as empirically defined by florin9doi */
1851 param[0] = 0x64;
1852 param[1] = 0x28;
1853
1854 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
1855 ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
1856 ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
1857 return -1;
1858
1859 return 0;
1860}
1861
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001862static void alps_set_defaults(struct alps_data *priv)
Seth Forshee25bded72011-11-07 19:53:36 -08001863{
Kevin Cernekeef673ceb2013-02-13 22:23:34 -08001864 priv->byte0 = 0x8f;
1865 priv->mask0 = 0x8f;
1866 priv->flags = ALPS_DUALPOINT;
1867
Kevin Cernekee7a9f73e2013-02-13 22:24:55 -08001868 priv->x_max = 2000;
1869 priv->y_max = 1400;
1870 priv->x_bits = 15;
1871 priv->y_bits = 11;
1872
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001873 switch (priv->proto_version) {
Seth Forshee25bded72011-11-07 19:53:36 -08001874 case ALPS_PROTO_V1:
1875 case ALPS_PROTO_V2:
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001876 priv->hw_init = alps_hw_init_v1_v2;
1877 priv->process_packet = alps_process_packet_v1_v2;
1878 priv->set_abs_params = alps_set_abs_params_st;
Yunkang Tang95f75e92013-12-01 22:33:52 -08001879 priv->x_max = 1023;
1880 priv->y_max = 767;
Seth Forshee25bded72011-11-07 19:53:36 -08001881 break;
1882 case ALPS_PROTO_V3:
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001883 priv->hw_init = alps_hw_init_v3;
1884 priv->process_packet = alps_process_packet_v3;
1885 priv->set_abs_params = alps_set_abs_params_mt;
Kevin Cernekeef85e5002013-02-13 22:26:11 -08001886 priv->decode_fields = alps_decode_pinnacle;
Kevin Cernekee50e8b212013-02-13 22:23:04 -08001887 priv->nibble_commands = alps_v3_nibble_commands;
1888 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
Seth Forshee25bded72011-11-07 19:53:36 -08001889 break;
1890 case ALPS_PROTO_V4:
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001891 priv->hw_init = alps_hw_init_v4;
1892 priv->process_packet = alps_process_packet_v4;
1893 priv->set_abs_params = alps_set_abs_params_mt;
Kevin Cernekee50e8b212013-02-13 22:23:04 -08001894 priv->nibble_commands = alps_v4_nibble_commands;
1895 priv->addr_command = PSMOUSE_CMD_DISABLE;
Seth Forshee25bded72011-11-07 19:53:36 -08001896 break;
Dave Turvene75af9e52013-02-21 22:58:28 -08001897 case ALPS_PROTO_V5:
1898 priv->hw_init = alps_hw_init_dolphin_v1;
Yunkang Tangee65d4b2013-12-26 14:54:19 -08001899 priv->process_packet = alps_process_touchpad_packet_v3_v5;
Dave Turvene75af9e52013-02-21 22:58:28 -08001900 priv->decode_fields = alps_decode_dolphin;
1901 priv->set_abs_params = alps_set_abs_params_mt;
1902 priv->nibble_commands = alps_v3_nibble_commands;
1903 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
1904 priv->byte0 = 0xc8;
Yunkang Tangee65d4b2013-12-26 14:54:19 -08001905 priv->mask0 = 0xd8;
Dave Turvene75af9e52013-02-21 22:58:28 -08001906 priv->flags = 0;
1907 priv->x_max = 1360;
1908 priv->y_max = 660;
1909 priv->x_bits = 23;
1910 priv->y_bits = 12;
1911 break;
Yunkang Tang95f75e92013-12-01 22:33:52 -08001912 case ALPS_PROTO_V6:
1913 priv->hw_init = alps_hw_init_v6;
1914 priv->process_packet = alps_process_packet_v6;
1915 priv->set_abs_params = alps_set_abs_params_st;
1916 priv->nibble_commands = alps_v6_nibble_commands;
1917 priv->x_max = 2047;
1918 priv->y_max = 1535;
1919 break;
Seth Forshee25bded72011-11-07 19:53:36 -08001920 }
Seth Forshee25bded72011-11-07 19:53:36 -08001921}
1922
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001923static int alps_match_table(struct psmouse *psmouse, struct alps_data *priv,
1924 unsigned char *e7, unsigned char *ec)
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08001925{
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001926 const struct alps_model_info *model;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08001927 int i;
1928
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001929 for (i = 0; i < ARRAY_SIZE(alps_model_data); i++) {
1930 model = &alps_model_data[i];
1931
1932 if (!memcmp(e7, model->signature, sizeof(model->signature)) &&
1933 (!model->command_mode_resp ||
1934 model->command_mode_resp == ec[2])) {
1935
1936 priv->proto_version = model->proto_version;
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001937 alps_set_defaults(priv);
1938
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001939 priv->flags = model->flags;
1940 priv->byte0 = model->byte0;
1941 priv->mask0 = model->mask0;
1942
1943 return 0;
1944 }
1945 }
1946
1947 return -EINVAL;
1948}
1949
1950static int alps_identify(struct psmouse *psmouse, struct alps_data *priv)
1951{
1952 unsigned char e6[4], e7[4], ec[4];
1953
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08001954 /*
1955 * First try "E6 report".
1956 * ALPS should return 0,0,10 or 0,0,100 if no buttons are pressed.
1957 * The bits 0-2 of the first byte will be 1s if some buttons are
1958 * pressed.
1959 */
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001960 if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
1961 PSMOUSE_CMD_SETSCALE11, e6))
1962 return -EIO;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08001963
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001964 if ((e6[0] & 0xf8) != 0 || e6[1] != 0 || (e6[2] != 10 && e6[2] != 100))
1965 return -EINVAL;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08001966
1967 /*
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001968 * Now get the "E7" and "EC" reports. These will uniquely identify
1969 * most ALPS touchpads.
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08001970 */
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001971 if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
1972 PSMOUSE_CMD_SETSCALE21, e7) ||
1973 alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
1974 PSMOUSE_CMD_RESET_WRAP, ec) ||
1975 alps_exit_command_mode(psmouse))
1976 return -EIO;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08001977
Kevin Cernekeef673ceb2013-02-13 22:23:34 -08001978 if (alps_match_table(psmouse, priv, e7, ec) == 0) {
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001979 return 0;
Dave Turvene75af9e52013-02-21 22:58:28 -08001980 } else if (e7[0] == 0x73 && e7[1] == 0x03 && e7[2] == 0x50 &&
Yunkang Tangee65d4b2013-12-26 14:54:19 -08001981 ec[0] == 0x73 && (ec[1] == 0x01 || ec[1] == 0x02)) {
Dave Turvene75af9e52013-02-21 22:58:28 -08001982 priv->proto_version = ALPS_PROTO_V5;
1983 alps_set_defaults(priv);
Yunkang Tangee65d4b2013-12-26 14:54:19 -08001984 if (alps_dolphin_get_device_area(psmouse, priv))
1985 return -EIO;
1986 else
1987 return 0;
Kevin Cernekee1302bac2013-02-13 22:27:08 -08001988 } else if (ec[0] == 0x88 && ec[1] == 0x08) {
1989 priv->proto_version = ALPS_PROTO_V3;
1990 alps_set_defaults(priv);
1991
1992 priv->hw_init = alps_hw_init_rushmore_v3;
1993 priv->decode_fields = alps_decode_rushmore;
1994 priv->x_bits = 16;
1995 priv->y_bits = 12;
1996
Kevin Cernekeecd401202013-02-13 22:28:07 -08001997 /* hack to make addr_command, nibble_command available */
1998 psmouse->private = priv;
1999
2000 if (alps_probe_trackstick_v3(psmouse, ALPS_REG_BASE_RUSHMORE))
2001 priv->flags &= ~ALPS_DUALPOINT;
2002
Kevin Cernekee1302bac2013-02-13 22:27:08 -08002003 return 0;
Kevin Cernekeef673ceb2013-02-13 22:23:34 -08002004 } else if (ec[0] == 0x88 && ec[1] == 0x07 &&
2005 ec[2] >= 0x90 && ec[2] <= 0x9d) {
2006 priv->proto_version = ALPS_PROTO_V3;
2007 alps_set_defaults(priv);
2008
2009 return 0;
2010 }
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002011
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002012 psmouse_info(psmouse,
Dmitry Torokhov39fbe582013-02-14 09:04:24 -08002013 "Unknown ALPS touchpad: E7=%3ph, EC=%3ph\n", e7, ec);
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002014
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002015 return -EINVAL;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002016}
2017
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04002018static int alps_reconnect(struct psmouse *psmouse)
2019{
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002020 struct alps_data *priv = psmouse->private;
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08002021
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04002022 psmouse_reset(psmouse);
2023
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002024 if (alps_identify(psmouse, priv) < 0)
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04002025 return -1;
2026
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002027 return priv->hw_init(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028}
2029
2030static void alps_disconnect(struct psmouse *psmouse)
2031{
2032 struct alps_data *priv = psmouse->private;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05002033
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 psmouse_reset(psmouse);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08002035 del_timer_sync(&priv->timer);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05002036 input_unregister_device(priv->dev2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 kfree(priv);
2038}
2039
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002040static void alps_set_abs_params_st(struct alps_data *priv,
2041 struct input_dev *dev1)
2042{
Yunkang Tang95f75e92013-12-01 22:33:52 -08002043 input_set_abs_params(dev1, ABS_X, 0, priv->x_max, 0, 0);
2044 input_set_abs_params(dev1, ABS_Y, 0, priv->y_max, 0, 0);
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002045}
2046
2047static void alps_set_abs_params_mt(struct alps_data *priv,
2048 struct input_dev *dev1)
2049{
2050 set_bit(INPUT_PROP_SEMI_MT, dev1->propbit);
2051 input_mt_init_slots(dev1, 2, 0);
Kevin Cernekee7a9f73e2013-02-13 22:24:55 -08002052 input_set_abs_params(dev1, ABS_MT_POSITION_X, 0, priv->x_max, 0, 0);
2053 input_set_abs_params(dev1, ABS_MT_POSITION_Y, 0, priv->y_max, 0, 0);
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002054
2055 set_bit(BTN_TOOL_DOUBLETAP, dev1->keybit);
2056 set_bit(BTN_TOOL_TRIPLETAP, dev1->keybit);
2057 set_bit(BTN_TOOL_QUADTAP, dev1->keybit);
2058
Kevin Cernekee7a9f73e2013-02-13 22:24:55 -08002059 input_set_abs_params(dev1, ABS_X, 0, priv->x_max, 0, 0);
2060 input_set_abs_params(dev1, ABS_Y, 0, priv->y_max, 0, 0);
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002061}
2062
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063int alps_init(struct psmouse *psmouse)
2064{
2065 struct alps_data *priv;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05002066 struct input_dev *dev1 = psmouse->dev, *dev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067
Dmitry Torokhovf42649e2007-04-12 01:31:13 -04002068 priv = kzalloc(sizeof(struct alps_data), GFP_KERNEL);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05002069 dev2 = input_allocate_device();
2070 if (!priv || !dev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 goto init_fail;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05002072
2073 priv->dev2 = dev2;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08002074 setup_timer(&priv->timer, alps_flush_packet, (unsigned long)psmouse);
2075
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04002076 psmouse->private = priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077
Seth Forshee25bded72011-11-07 19:53:36 -08002078 psmouse_reset(psmouse);
2079
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002080 if (alps_identify(psmouse, priv) < 0)
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08002081 goto init_fail;
2082
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002083 if (priv->hw_init(psmouse))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 goto init_fail;
2085
Dmitry Torokhov7105d2e2009-12-11 23:54:54 -08002086 /*
2087 * Undo part of setup done for us by psmouse core since touchpad
2088 * is not a relative device.
2089 */
2090 __clear_bit(EV_REL, dev1->evbit);
2091 __clear_bit(REL_X, dev1->relbit);
2092 __clear_bit(REL_Y, dev1->relbit);
2093
2094 /*
2095 * Now set up our capabilities.
2096 */
Jiri Slaby7b19ada2007-10-18 23:40:32 -07002097 dev1->evbit[BIT_WORD(EV_KEY)] |= BIT_MASK(EV_KEY);
2098 dev1->keybit[BIT_WORD(BTN_TOUCH)] |= BIT_MASK(BTN_TOUCH);
2099 dev1->keybit[BIT_WORD(BTN_TOOL_FINGER)] |= BIT_MASK(BTN_TOOL_FINGER);
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08002100 dev1->keybit[BIT_WORD(BTN_LEFT)] |=
2101 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102
Jiri Slaby7b19ada2007-10-18 23:40:32 -07002103 dev1->evbit[BIT_WORD(EV_ABS)] |= BIT_MASK(EV_ABS);
Seth Forshee25bded72011-11-07 19:53:36 -08002104
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002105 priv->set_abs_params(priv, dev1);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05002106 input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002108 if (priv->flags & ALPS_WHEEL) {
Jiri Slaby7b19ada2007-10-18 23:40:32 -07002109 dev1->evbit[BIT_WORD(EV_REL)] |= BIT_MASK(EV_REL);
2110 dev1->relbit[BIT_WORD(REL_WHEEL)] |= BIT_MASK(REL_WHEEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 }
2112
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002113 if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
Jiri Slaby7b19ada2007-10-18 23:40:32 -07002114 dev1->keybit[BIT_WORD(BTN_FORWARD)] |= BIT_MASK(BTN_FORWARD);
2115 dev1->keybit[BIT_WORD(BTN_BACK)] |= BIT_MASK(BTN_BACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 }
2117
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002118 if (priv->flags & ALPS_FOUR_BUTTONS) {
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08002119 dev1->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_0);
2120 dev1->keybit[BIT_WORD(BTN_1)] |= BIT_MASK(BTN_1);
2121 dev1->keybit[BIT_WORD(BTN_2)] |= BIT_MASK(BTN_2);
2122 dev1->keybit[BIT_WORD(BTN_3)] |= BIT_MASK(BTN_3);
2123 } else {
2124 dev1->keybit[BIT_WORD(BTN_MIDDLE)] |= BIT_MASK(BTN_MIDDLE);
2125 }
2126
Dmitry Torokhov08ffce42006-06-26 01:45:10 -04002127 snprintf(priv->phys, sizeof(priv->phys), "%s/input1", psmouse->ps2dev.serio->phys);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05002128 dev2->phys = priv->phys;
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002129 dev2->name = (priv->flags & ALPS_DUALPOINT) ?
Yunkang Tang9354f262013-10-15 23:50:38 -07002130 "DualPoint Stick" : "ALPS PS/2 Device";
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05002131 dev2->id.bustype = BUS_I8042;
2132 dev2->id.vendor = 0x0002;
2133 dev2->id.product = PSMOUSE_ALPS;
2134 dev2->id.version = 0x0000;
Dmitry Torokhov1db3a342008-03-18 00:29:18 -04002135 dev2->dev.parent = &psmouse->ps2dev.serio->dev;
Dmitry Torokhov968ac842005-05-29 02:28:29 -05002136
Jiri Slaby7b19ada2007-10-18 23:40:32 -07002137 dev2->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08002138 dev2->relbit[BIT_WORD(REL_X)] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
2139 dev2->keybit[BIT_WORD(BTN_LEFT)] =
2140 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_MIDDLE) | BIT_MASK(BTN_RIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141
Dmitry Torokhovf42649e2007-04-12 01:31:13 -04002142 if (input_register_device(priv->dev2))
2143 goto init_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144
2145 psmouse->protocol_handler = alps_process_byte;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05002146 psmouse->poll = alps_poll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 psmouse->disconnect = alps_disconnect;
2148 psmouse->reconnect = alps_reconnect;
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002149 psmouse->pktsize = priv->proto_version == ALPS_PROTO_V4 ? 8 : 6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05002151 /* We are having trouble resyncing ALPS touchpads so disable it for now */
2152 psmouse->resync_time = 0;
2153
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 return 0;
2155
2156init_fail:
Dmitry Torokhovf42649e2007-04-12 01:31:13 -04002157 psmouse_reset(psmouse);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05002158 input_free_device(dev2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 kfree(priv);
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04002160 psmouse->private = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 return -1;
2162}
2163
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07002164int alps_detect(struct psmouse *psmouse, bool set_properties)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165{
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002166 struct alps_data dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002168 if (alps_identify(psmouse, &dummy) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 return -1;
2170
2171 if (set_properties) {
2172 psmouse->vendor = "ALPS";
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002173 psmouse->name = dummy.flags & ALPS_DUALPOINT ?
Dmitry Torokhov968ac842005-05-29 02:28:29 -05002174 "DualPoint TouchPad" : "GlidePoint";
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002175 psmouse->model = dummy.proto_version << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 }
2177 return 0;
2178}
2179