aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/input/mouse
diff options
context:
space:
mode:
authorDudley Du <dudl@cypress.com>2013-01-05 03:14:22 -0500
committerDmitry Torokhov <dmitry.torokhov@gmail.com>2013-01-17 03:27:34 -0500
commit0799a924bc93ba46a23e8e7e6b1431ab585fd2ea (patch)
tree23e95e1d7cce9887aa25ea66dac40f988bf5ee14 /drivers/input/mouse
parent80524f083e2c3e70057f5bb476db92baa14cb22b (diff)
Input: add support for Cypress PS/2 Trackpads
This driver, submitted on behalf of Cypress Semiconductor Corporation and additional contributors, provides support for the Cypress PS/2 Trackpad. Original code contributed by Dudley Du (Cypress Semiconductor Corporation), modified by Kamal Mostafa and Kyle Fazzari. BugLink: http://launchpad.net/bugs/978807 Signed-off-by: Dudley Du <dudl@cypress.com> Signed-off-by: Kamal Mostafa <kamal@canonical.com> Signed-off-by: Kyle Fazzari <git@status.e4ward.com> Signed-off-by: Mario Limonciello <mario_limonciello@dell.com> Signed-off-by: Tim Gardner <tim.gardner@canonical.com> Acked-by: Herton Krzesinski <herton.krzesinski@canonical.com> Reviewed-by: Henrik Rydberg <rydberg@euromail.se> Reviewed-by: Dudley Du <dudl@cypress.com> Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
Diffstat (limited to 'drivers/input/mouse')
-rw-r--r--drivers/input/mouse/Kconfig10
-rw-r--r--drivers/input/mouse/Makefile1
-rw-r--r--drivers/input/mouse/cypress_ps2.c725
-rw-r--r--drivers/input/mouse/cypress_ps2.h191
-rw-r--r--drivers/input/mouse/psmouse-base.c32
-rw-r--r--drivers/input/mouse/psmouse.h1
6 files changed, 960 insertions, 0 deletions
diff --git a/drivers/input/mouse/Kconfig b/drivers/input/mouse/Kconfig
index cd6268cf7cd5..88954ddae6d9 100644
--- a/drivers/input/mouse/Kconfig
+++ b/drivers/input/mouse/Kconfig
@@ -68,6 +68,16 @@ config MOUSE_PS2_SYNAPTICS
68 68
69 If unsure, say Y. 69 If unsure, say Y.
70 70
71config MOUSE_PS2_CYPRESS
72 bool "Cypress PS/2 mouse protocol extension" if EXPERT
73 default y
74 depends on MOUSE_PS2
75 help
76 Say Y here if you have a Cypress PS/2 Trackpad connected to
77 your system.
78
79 If unsure, say Y.
80
71config MOUSE_PS2_LIFEBOOK 81config MOUSE_PS2_LIFEBOOK
72 bool "Fujitsu Lifebook PS/2 mouse protocol extension" if EXPERT 82 bool "Fujitsu Lifebook PS/2 mouse protocol extension" if EXPERT
73 default y 83 default y
diff --git a/drivers/input/mouse/Makefile b/drivers/input/mouse/Makefile
index 46ba7556fd4f..323e352189ba 100644
--- a/drivers/input/mouse/Makefile
+++ b/drivers/input/mouse/Makefile
@@ -32,3 +32,4 @@ psmouse-$(CONFIG_MOUSE_PS2_LIFEBOOK) += lifebook.o
32psmouse-$(CONFIG_MOUSE_PS2_SENTELIC) += sentelic.o 32psmouse-$(CONFIG_MOUSE_PS2_SENTELIC) += sentelic.o
33psmouse-$(CONFIG_MOUSE_PS2_TRACKPOINT) += trackpoint.o 33psmouse-$(CONFIG_MOUSE_PS2_TRACKPOINT) += trackpoint.o
34psmouse-$(CONFIG_MOUSE_PS2_TOUCHKIT) += touchkit_ps2.o 34psmouse-$(CONFIG_MOUSE_PS2_TOUCHKIT) += touchkit_ps2.o
35psmouse-$(CONFIG_MOUSE_PS2_CYPRESS) += cypress_ps2.o
diff --git a/drivers/input/mouse/cypress_ps2.c b/drivers/input/mouse/cypress_ps2.c
new file mode 100644
index 000000000000..1673dc6c8092
--- /dev/null
+++ b/drivers/input/mouse/cypress_ps2.c
@@ -0,0 +1,725 @@
1/*
2 * Cypress Trackpad PS/2 mouse driver
3 *
4 * Copyright (c) 2012 Cypress Semiconductor Corporation.
5 *
6 * Author:
7 * Dudley Du <dudl@cypress.com>
8 *
9 * Additional contributors include:
10 * Kamal Mostafa <kamal@canonical.com>
11 * Kyle Fazzari <git@status.e4ward.com>
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
18#include <linux/init.h>
19#include <linux/module.h>
20#include <linux/kernel.h>
21#include <linux/slab.h>
22#include <linux/serio.h>
23#include <linux/libps2.h>
24#include <linux/input.h>
25#include <linux/input/mt.h>
26#include <linux/sched.h>
27#include <linux/wait.h>
28
29#include "cypress_ps2.h"
30
31#undef CYTP_DEBUG_VERBOSE /* define this and DEBUG for more verbose dump */
32
33static void cypress_set_packet_size(struct psmouse *psmouse, unsigned int n)
34{
35 struct cytp_data *cytp = psmouse->private;
36 cytp->pkt_size = n;
37}
38
39static const unsigned char cytp_rate[] = {10, 20, 40, 60, 100, 200};
40static const unsigned char cytp_resolution[] = {0x00, 0x01, 0x02, 0x03};
41
42static int cypress_ps2_sendbyte(struct psmouse *psmouse, int value)
43{
44 struct ps2dev *ps2dev = &psmouse->ps2dev;
45
46 if (ps2_sendbyte(ps2dev, value & 0xff, CYTP_CMD_TIMEOUT) < 0) {
47 psmouse_dbg(psmouse,
48 "sending command 0x%02x failed, resp 0x%02x\n",
49 value & 0xff, ps2dev->nak);
50 if (ps2dev->nak == CYTP_PS2_RETRY)
51 return CYTP_PS2_RETRY;
52 else
53 return CYTP_PS2_ERROR;
54 }
55
56#ifdef CYTP_DEBUG_VERBOSE
57 psmouse_dbg(psmouse, "sending command 0x%02x succeeded, resp 0xfa\n",
58 value & 0xff);
59#endif
60
61 return 0;
62}
63
64static int cypress_ps2_ext_cmd(struct psmouse *psmouse, unsigned short cmd,
65 unsigned char data)
66{
67 struct ps2dev *ps2dev = &psmouse->ps2dev;
68 int tries = CYTP_PS2_CMD_TRIES;
69 int rc;
70
71 ps2_begin_command(ps2dev);
72
73 do {
74 /*
75 * Send extension command byte (0xE8 or 0xF3).
76 * If sending the command fails, send recovery command
77 * to make the device return to the ready state.
78 */
79 rc = cypress_ps2_sendbyte(psmouse, cmd & 0xff);
80 if (rc == CYTP_PS2_RETRY) {
81 rc = cypress_ps2_sendbyte(psmouse, 0x00);
82 if (rc == CYTP_PS2_RETRY)
83 rc = cypress_ps2_sendbyte(psmouse, 0x0a);
84 }
85 if (rc == CYTP_PS2_ERROR)
86 continue;
87
88 rc = cypress_ps2_sendbyte(psmouse, data);
89 if (rc == CYTP_PS2_RETRY)
90 rc = cypress_ps2_sendbyte(psmouse, data);
91 if (rc == CYTP_PS2_ERROR)
92 continue;
93 else
94 break;
95 } while (--tries > 0);
96
97 ps2_end_command(ps2dev);
98
99 return rc;
100}
101
102static int cypress_ps2_read_cmd_status(struct psmouse *psmouse,
103 unsigned char cmd,
104 unsigned char *param)
105{
106 int rc;
107 struct ps2dev *ps2dev = &psmouse->ps2dev;
108 enum psmouse_state old_state;
109 int pktsize;
110
111 ps2_begin_command(&psmouse->ps2dev);
112
113 old_state = psmouse->state;
114 psmouse->state = PSMOUSE_CMD_MODE;
115 psmouse->pktcnt = 0;
116
117 pktsize = (cmd == CYTP_CMD_READ_TP_METRICS) ? 8 : 3;
118 memset(param, 0, pktsize);
119
120 rc = cypress_ps2_sendbyte(psmouse, 0xe9);
121 if (rc < 0)
122 goto out;
123
124 wait_event_timeout(ps2dev->wait,
125 (psmouse->pktcnt >= pktsize),
126 msecs_to_jiffies(CYTP_CMD_TIMEOUT));
127
128 memcpy(param, psmouse->packet, pktsize);
129
130 psmouse_dbg(psmouse, "Command 0x%02x response data (0x): %*ph\n",
131 cmd, pktsize, param);
132
133out:
134 psmouse->state = old_state;
135 psmouse->pktcnt = 0;
136
137 ps2_end_command(&psmouse->ps2dev);
138
139 return rc;
140}
141
142static bool cypress_verify_cmd_state(struct psmouse *psmouse,
143 unsigned char cmd, unsigned char *param)
144{
145 bool rate_match = false;
146 bool resolution_match = false;
147 int i;
148
149 /* callers will do further checking. */
150 if (cmd == CYTP_CMD_READ_CYPRESS_ID ||
151 cmd == CYTP_CMD_STANDARD_MODE ||
152 cmd == CYTP_CMD_READ_TP_METRICS)
153 return true;
154
155 if ((~param[0] & DFLT_RESP_BITS_VALID) == DFLT_RESP_BITS_VALID &&
156 (param[0] & DFLT_RESP_BIT_MODE) == DFLT_RESP_STREAM_MODE) {
157 for (i = 0; i < sizeof(cytp_resolution); i++)
158 if (cytp_resolution[i] == param[1])
159 resolution_match = true;
160
161 for (i = 0; i < sizeof(cytp_rate); i++)
162 if (cytp_rate[i] == param[2])
163 rate_match = true;
164
165 if (resolution_match && rate_match)
166 return true;
167 }
168
169 psmouse_dbg(psmouse, "verify cmd state failed.\n");
170 return false;
171}
172
173static int cypress_send_ext_cmd(struct psmouse *psmouse, unsigned char cmd,
174 unsigned char *param)
175{
176 int tries = CYTP_PS2_CMD_TRIES;
177 int rc;
178
179 psmouse_dbg(psmouse, "send extension cmd 0x%02x, [%d %d %d %d]\n",
180 cmd, DECODE_CMD_AA(cmd), DECODE_CMD_BB(cmd),
181 DECODE_CMD_CC(cmd), DECODE_CMD_DD(cmd));
182
183 do {
184 cypress_ps2_ext_cmd(psmouse,
185 PSMOUSE_CMD_SETRES, DECODE_CMD_DD(cmd));
186 cypress_ps2_ext_cmd(psmouse,
187 PSMOUSE_CMD_SETRES, DECODE_CMD_CC(cmd));
188 cypress_ps2_ext_cmd(psmouse,
189 PSMOUSE_CMD_SETRES, DECODE_CMD_BB(cmd));
190 cypress_ps2_ext_cmd(psmouse,
191 PSMOUSE_CMD_SETRES, DECODE_CMD_AA(cmd));
192
193 rc = cypress_ps2_read_cmd_status(psmouse, cmd, param);
194 if (rc)
195 continue;
196
197 if (cypress_verify_cmd_state(psmouse, cmd, param))
198 return 0;
199
200 } while (--tries > 0);
201
202 return -EIO;
203}
204
205int cypress_detect(struct psmouse *psmouse, bool set_properties)
206{
207 unsigned char param[3];
208
209 if (cypress_send_ext_cmd(psmouse, CYTP_CMD_READ_CYPRESS_ID, param))
210 return -ENODEV;
211
212 /* Check for Cypress Trackpad signature bytes: 0x33 0xCC */
213 if (param[0] != 0x33 || param[1] != 0xCC)
214 return -ENODEV;
215
216 if (set_properties) {
217 psmouse->vendor = "Cypress";
218 psmouse->name = "Trackpad";
219 }
220
221 return 0;
222}
223
224static int cypress_read_fw_version(struct psmouse *psmouse)
225{
226 struct cytp_data *cytp = psmouse->private;
227 unsigned char param[3];
228
229 if (cypress_send_ext_cmd(psmouse, CYTP_CMD_READ_CYPRESS_ID, param))
230 return -ENODEV;
231
232 /* Check for Cypress Trackpad signature bytes: 0x33 0xCC */
233 if (param[0] != 0x33 || param[1] != 0xCC)
234 return -ENODEV;
235
236 cytp->fw_version = param[2] & FW_VERSION_MASX;
237 cytp->tp_metrics_supported = (param[2] & TP_METRICS_MASK) ? 1 : 0;
238
239 psmouse_dbg(psmouse, "cytp->fw_version = %d\n", cytp->fw_version);
240 psmouse_dbg(psmouse, "cytp->tp_metrics_supported = %d\n",
241 cytp->tp_metrics_supported);
242
243 return 0;
244}
245
246static int cypress_read_tp_metrics(struct psmouse *psmouse)
247{
248 struct cytp_data *cytp = psmouse->private;
249 unsigned char param[8];
250
251 /* set default values for tp metrics. */
252 cytp->tp_width = CYTP_DEFAULT_WIDTH;
253 cytp->tp_high = CYTP_DEFAULT_HIGH;
254 cytp->tp_max_abs_x = CYTP_ABS_MAX_X;
255 cytp->tp_max_abs_y = CYTP_ABS_MAX_Y;
256 cytp->tp_min_pressure = CYTP_MIN_PRESSURE;
257 cytp->tp_max_pressure = CYTP_MAX_PRESSURE;
258 cytp->tp_res_x = cytp->tp_max_abs_x / cytp->tp_width;
259 cytp->tp_res_y = cytp->tp_max_abs_y / cytp->tp_high;
260
261 memset(param, 0, sizeof(param));
262 if (cypress_send_ext_cmd(psmouse, CYTP_CMD_READ_TP_METRICS, param) == 0) {
263 /* Update trackpad parameters. */
264 cytp->tp_max_abs_x = (param[1] << 8) | param[0];
265 cytp->tp_max_abs_y = (param[3] << 8) | param[2];
266 cytp->tp_min_pressure = param[4];
267 cytp->tp_max_pressure = param[5];
268 }
269
270 if (!cytp->tp_max_pressure ||
271 cytp->tp_max_pressure < cytp->tp_min_pressure ||
272 !cytp->tp_width || !cytp->tp_high ||
273 !cytp->tp_max_abs_x ||
274 cytp->tp_max_abs_x < cytp->tp_width ||
275 !cytp->tp_max_abs_y ||
276 cytp->tp_max_abs_y < cytp->tp_high)
277 return -EINVAL;
278
279 cytp->tp_res_x = cytp->tp_max_abs_x / cytp->tp_width;
280 cytp->tp_res_y = cytp->tp_max_abs_y / cytp->tp_high;
281
282#ifdef CYTP_DEBUG_VERBOSE
283 psmouse_dbg(psmouse, "Dump trackpad hardware configuration as below:\n");
284 psmouse_dbg(psmouse, "cytp->tp_width = %d\n", cytp->tp_width);
285 psmouse_dbg(psmouse, "cytp->tp_high = %d\n", cytp->tp_high);
286 psmouse_dbg(psmouse, "cytp->tp_max_abs_x = %d\n", cytp->tp_max_abs_x);
287 psmouse_dbg(psmouse, "cytp->tp_max_abs_y = %d\n", cytp->tp_max_abs_y);
288 psmouse_dbg(psmouse, "cytp->tp_min_pressure = %d\n", cytp->tp_min_pressure);
289 psmouse_dbg(psmouse, "cytp->tp_max_pressure = %d\n", cytp->tp_max_pressure);
290 psmouse_dbg(psmouse, "cytp->tp_res_x = %d\n", cytp->tp_res_x);
291 psmouse_dbg(psmouse, "cytp->tp_res_y = %d\n", cytp->tp_res_y);
292
293 psmouse_dbg(psmouse, "tp_type_APA = %d\n",
294 (param[6] & TP_METRICS_BIT_APA) ? 1 : 0);
295 psmouse_dbg(psmouse, "tp_type_MTG = %d\n",
296 (param[6] & TP_METRICS_BIT_MTG) ? 1 : 0);
297 psmouse_dbg(psmouse, "tp_palm = %d\n",
298 (param[6] & TP_METRICS_BIT_PALM) ? 1 : 0);
299 psmouse_dbg(psmouse, "tp_stubborn = %d\n",
300 (param[6] & TP_METRICS_BIT_STUBBORN) ? 1 : 0);
301 psmouse_dbg(psmouse, "tp_1f_jitter = %d\n",
302 (param[6] & TP_METRICS_BIT_1F_JITTER) >> 2);
303 psmouse_dbg(psmouse, "tp_2f_jitter = %d\n",
304 (param[6] & TP_METRICS_BIT_2F_JITTER) >> 4);
305 psmouse_dbg(psmouse, "tp_1f_spike = %d\n",
306 param[7] & TP_METRICS_BIT_1F_SPIKE);
307 psmouse_dbg(psmouse, "tp_2f_spike = %d\n",
308 (param[7] & TP_METRICS_BIT_2F_SPIKE) >> 2);
309 psmouse_dbg(psmouse, "tp_abs_packet_format_set = %d\n",
310 (param[7] & TP_METRICS_BIT_ABS_PKT_FORMAT_SET) >> 4);
311#endif
312
313 return 0;
314}
315
316static int cypress_query_hardware(struct psmouse *psmouse)
317{
318 struct cytp_data *cytp = psmouse->private;
319 int ret;
320
321 ret = cypress_read_fw_version(psmouse);
322 if (ret)
323 return ret;
324
325 if (cytp->tp_metrics_supported) {
326 ret = cypress_read_tp_metrics(psmouse);
327 if (ret)
328 return ret;
329 }
330
331 return 0;
332}
333
334static int cypress_set_absolute_mode(struct psmouse *psmouse)
335{
336 struct cytp_data *cytp = psmouse->private;
337 unsigned char param[3];
338
339 if (cypress_send_ext_cmd(psmouse, CYTP_CMD_ABS_WITH_PRESSURE_MODE, param) < 0)
340 return -1;
341
342 cytp->mode = (cytp->mode & ~CYTP_BIT_ABS_REL_MASK)
343 | CYTP_BIT_ABS_PRESSURE;
344 cypress_set_packet_size(psmouse, 5);
345
346 return 0;
347}
348
349/*
350 * Reset trackpad device.
351 * This is also the default mode when trackpad powered on.
352 */
353static void cypress_reset(struct psmouse *psmouse)
354{
355 struct cytp_data *cytp = psmouse->private;
356
357 cytp->mode = 0;
358
359 psmouse_reset(psmouse);
360}
361
362static int cypress_set_input_params(struct input_dev *input,
363 struct cytp_data *cytp)
364{
365 int ret;
366
367 if (!cytp->tp_res_x || !cytp->tp_res_y)
368 return -EINVAL;
369
370 __set_bit(EV_ABS, input->evbit);
371 input_set_abs_params(input, ABS_X, 0, cytp->tp_max_abs_x, 0, 0);
372 input_set_abs_params(input, ABS_Y, 0, cytp->tp_max_abs_y, 0, 0);
373 input_set_abs_params(input, ABS_PRESSURE,
374 cytp->tp_min_pressure, cytp->tp_max_pressure, 0, 0);
375 input_set_abs_params(input, ABS_TOOL_WIDTH, 0, 255, 0, 0);
376
377 /* finger position */
378 input_set_abs_params(input, ABS_MT_POSITION_X, 0, cytp->tp_max_abs_x, 0, 0);
379 input_set_abs_params(input, ABS_MT_POSITION_Y, 0, cytp->tp_max_abs_y, 0, 0);
380 input_set_abs_params(input, ABS_MT_PRESSURE, 0, 255, 0, 0);
381
382 ret = input_mt_init_slots(input, CYTP_MAX_MT_SLOTS,
383 INPUT_MT_DROP_UNUSED|INPUT_MT_TRACK);
384 if (ret < 0)
385 return ret;
386
387 __set_bit(INPUT_PROP_SEMI_MT, input->propbit);
388
389 input_abs_set_res(input, ABS_X, cytp->tp_res_x);
390 input_abs_set_res(input, ABS_Y, cytp->tp_res_y);
391
392 input_abs_set_res(input, ABS_MT_POSITION_X, cytp->tp_res_x);
393 input_abs_set_res(input, ABS_MT_POSITION_Y, cytp->tp_res_y);
394
395 __set_bit(BTN_TOUCH, input->keybit);
396 __set_bit(BTN_TOOL_FINGER, input->keybit);
397 __set_bit(BTN_TOOL_DOUBLETAP, input->keybit);
398 __set_bit(BTN_TOOL_TRIPLETAP, input->keybit);
399 __set_bit(BTN_TOOL_QUADTAP, input->keybit);
400 __set_bit(BTN_TOOL_QUINTTAP, input->keybit);
401
402 __clear_bit(EV_REL, input->evbit);
403 __clear_bit(REL_X, input->relbit);
404 __clear_bit(REL_Y, input->relbit);
405
406 __set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
407 __set_bit(EV_KEY, input->evbit);
408 __set_bit(BTN_LEFT, input->keybit);
409 __set_bit(BTN_RIGHT, input->keybit);
410 __set_bit(BTN_MIDDLE, input->keybit);
411
412 input_set_drvdata(input, cytp);
413
414 return 0;
415}
416
417static int cypress_get_finger_count(unsigned char header_byte)
418{
419 unsigned char bits6_7;
420 int finger_count;
421
422 bits6_7 = header_byte >> 6;
423 finger_count = bits6_7 & 0x03;
424
425 if (finger_count == 1)
426 return 1;
427
428 if (header_byte & ABS_HSCROLL_BIT) {
429 /* HSCROLL gets added on to 0 finger count. */
430 switch (finger_count) {
431 case 0: return 4;
432 case 2: return 5;
433 default:
434 /* Invalid contact (e.g. palm). Ignore it. */
435 return -1;
436 }
437 }
438
439 return finger_count;
440}
441
442
443static int cypress_parse_packet(struct psmouse *psmouse,
444 struct cytp_data *cytp, struct cytp_report_data *report_data)
445{
446 unsigned char *packet = psmouse->packet;
447 unsigned char header_byte = packet[0];
448 int contact_cnt;
449
450 memset(report_data, 0, sizeof(struct cytp_report_data));
451
452 contact_cnt = cypress_get_finger_count(header_byte);
453
454 if (contact_cnt < 0) /* e.g. palm detect */
455 return -EINVAL;
456
457 report_data->contact_cnt = contact_cnt;
458
459 report_data->tap = (header_byte & ABS_MULTIFINGER_TAP) ? 1 : 0;
460
461 if (report_data->contact_cnt == 1) {
462 report_data->contacts[0].x =
463 ((packet[1] & 0x70) << 4) | packet[2];
464 report_data->contacts[0].y =
465 ((packet[1] & 0x07) << 8) | packet[3];
466 if (cytp->mode & CYTP_BIT_ABS_PRESSURE)
467 report_data->contacts[0].z = packet[4];
468
469 } else if (report_data->contact_cnt >= 2) {
470 report_data->contacts[0].x =
471 ((packet[1] & 0x70) << 4) | packet[2];
472 report_data->contacts[0].y =
473 ((packet[1] & 0x07) << 8) | packet[3];
474 if (cytp->mode & CYTP_BIT_ABS_PRESSURE)
475 report_data->contacts[0].z = packet[4];
476
477 report_data->contacts[1].x =
478 ((packet[5] & 0xf0) << 4) | packet[6];
479 report_data->contacts[1].y =
480 ((packet[5] & 0x0f) << 8) | packet[7];
481 if (cytp->mode & CYTP_BIT_ABS_PRESSURE)
482 report_data->contacts[1].z = report_data->contacts[0].z;
483 }
484
485 report_data->left = (header_byte & BTN_LEFT_BIT) ? 1 : 0;
486 report_data->right = (header_byte & BTN_RIGHT_BIT) ? 1 : 0;
487
488 /*
489 * This is only true if one of the mouse buttons were tapped. Make
490 * sure it doesn't turn into a click. The regular tap-to-click
491 * functionality will handle that on its own. If we don't do this,
492 * disabling tap-to-click won't affect the mouse button zones.
493 */
494 if (report_data->tap)
495 report_data->left = 0;
496
497#ifdef CYTP_DEBUG_VERBOSE
498 {
499 int i;
500 int n = report_data->contact_cnt;
501 psmouse_dbg(psmouse, "Dump parsed report data as below:\n");
502 psmouse_dbg(psmouse, "contact_cnt = %d\n",
503 report_data->contact_cnt);
504 if (n > CYTP_MAX_MT_SLOTS)
505 n = CYTP_MAX_MT_SLOTS;
506 for (i = 0; i < n; i++)
507 psmouse_dbg(psmouse, "contacts[%d] = {%d, %d, %d}\n", i,
508 report_data->contacts[i].x,
509 report_data->contacts[i].y,
510 report_data->contacts[i].z);
511 psmouse_dbg(psmouse, "left = %d\n", report_data->left);
512 psmouse_dbg(psmouse, "right = %d\n", report_data->right);
513 psmouse_dbg(psmouse, "middle = %d\n", report_data->middle);
514 }
515#endif
516
517 return 0;
518}
519
520static void cypress_process_packet(struct psmouse *psmouse, bool zero_pkt)
521{
522 int i;
523 struct input_dev *input = psmouse->dev;
524 struct cytp_data *cytp = psmouse->private;
525 struct cytp_report_data report_data;
526 struct cytp_contact *contact;
527 struct input_mt_pos pos[CYTP_MAX_MT_SLOTS];
528 int slots[CYTP_MAX_MT_SLOTS];
529 int n;
530
531 if (cypress_parse_packet(psmouse, cytp, &report_data))
532 return;
533
534 n = report_data.contact_cnt;
535
536 if (n > CYTP_MAX_MT_SLOTS)
537 n = CYTP_MAX_MT_SLOTS;
538
539 for (i = 0; i < n; i++) {
540 contact = &report_data.contacts[i];
541 pos[i].x = contact->x;
542 pos[i].y = contact->y;
543 }
544
545 input_mt_assign_slots(input, slots, pos, n);
546
547 for (i = 0; i < n; i++) {
548 contact = &report_data.contacts[i];
549 input_mt_slot(input, slots[i]);
550 input_mt_report_slot_state(input, MT_TOOL_FINGER, true);
551 input_report_abs(input, ABS_MT_POSITION_X, contact->x);
552 input_report_abs(input, ABS_MT_POSITION_Y, contact->y);
553 input_report_abs(input, ABS_MT_PRESSURE, contact->z);
554 }
555
556 input_mt_sync_frame(input);
557
558 input_mt_report_finger_count(input, report_data.contact_cnt);
559
560 input_report_key(input, BTN_LEFT, report_data.left);
561 input_report_key(input, BTN_RIGHT, report_data.right);
562 input_report_key(input, BTN_MIDDLE, report_data.middle);
563
564 input_sync(input);
565}
566
567static psmouse_ret_t cypress_validate_byte(struct psmouse *psmouse)
568{
569 int contact_cnt;
570 int index = psmouse->pktcnt - 1;
571 unsigned char *packet = psmouse->packet;
572 struct cytp_data *cytp = psmouse->private;
573
574 if (index < 0 || index > cytp->pkt_size)
575 return PSMOUSE_BAD_DATA;
576
577 if (index == 0 && (packet[0] & 0xfc) == 0) {
578 /* call packet process for reporting finger leave. */
579 cypress_process_packet(psmouse, 1);
580 return PSMOUSE_FULL_PACKET;
581 }
582
583 /*
584 * Perform validation (and adjust packet size) based only on the
585 * first byte; allow all further bytes through.
586 */
587 if (index != 0)
588 return PSMOUSE_GOOD_DATA;
589
590 /*
591 * If absolute/relative mode bit has not been set yet, just pass
592 * the byte through.
593 */
594 if ((cytp->mode & CYTP_BIT_ABS_REL_MASK) == 0)
595 return PSMOUSE_GOOD_DATA;
596
597 if ((packet[0] & 0x08) == 0x08)
598 return PSMOUSE_BAD_DATA;
599
600 contact_cnt = cypress_get_finger_count(packet[0]);
601
602 if (contact_cnt < 0)
603 return PSMOUSE_BAD_DATA;
604
605 if (cytp->mode & CYTP_BIT_ABS_NO_PRESSURE)
606 cypress_set_packet_size(psmouse, contact_cnt == 2 ? 7 : 4);
607 else
608 cypress_set_packet_size(psmouse, contact_cnt == 2 ? 8 : 5);
609
610 return PSMOUSE_GOOD_DATA;
611}
612
613static psmouse_ret_t cypress_protocol_handler(struct psmouse *psmouse)
614{
615 struct cytp_data *cytp = psmouse->private;
616
617 if (psmouse->pktcnt >= cytp->pkt_size) {
618 cypress_process_packet(psmouse, 0);
619 return PSMOUSE_FULL_PACKET;
620 }
621
622 return cypress_validate_byte(psmouse);
623}
624
625static void cypress_set_rate(struct psmouse *psmouse, unsigned int rate)
626{
627 struct cytp_data *cytp = psmouse->private;
628
629 if (rate >= 80) {
630 psmouse->rate = 80;
631 cytp->mode |= CYTP_BIT_HIGH_RATE;
632 } else {
633 psmouse->rate = 40;
634 cytp->mode &= ~CYTP_BIT_HIGH_RATE;
635 }
636
637 ps2_command(&psmouse->ps2dev, (unsigned char *)&psmouse->rate,
638 PSMOUSE_CMD_SETRATE);
639}
640
641static void cypress_disconnect(struct psmouse *psmouse)
642{
643 cypress_reset(psmouse);
644 kfree(psmouse->private);
645 psmouse->private = NULL;
646}
647
648static int cypress_reconnect(struct psmouse *psmouse)
649{
650 int tries = CYTP_PS2_CMD_TRIES;
651 int rc;
652
653 do {
654 cypress_reset(psmouse);
655 rc = cypress_detect(psmouse, false);
656 } while (rc && (--tries > 0));
657
658 if (rc) {
659 psmouse_err(psmouse, "Reconnect: unable to detect trackpad.\n");
660 return -1;
661 }
662
663 if (cypress_set_absolute_mode(psmouse)) {
664 psmouse_err(psmouse, "Reconnect: Unable to initialize Cypress absolute mode.\n");
665 return -1;
666 }
667
668 return 0;
669}
670
671int cypress_init(struct psmouse *psmouse)
672{
673 struct cytp_data *cytp;
674
675 cytp = (struct cytp_data *)kzalloc(sizeof(struct cytp_data), GFP_KERNEL);
676 psmouse->private = (void *)cytp;
677 if (cytp == NULL)
678 return -ENOMEM;
679
680 cypress_reset(psmouse);
681
682 psmouse->pktsize = 8;
683
684 if (cypress_query_hardware(psmouse)) {
685 psmouse_err(psmouse, "Unable to query Trackpad hardware.\n");
686 goto err_exit;
687 }
688
689 if (cypress_set_absolute_mode(psmouse)) {
690 psmouse_err(psmouse, "init: Unable to initialize Cypress absolute mode.\n");
691 goto err_exit;
692 }
693
694 if (cypress_set_input_params(psmouse->dev, cytp) < 0) {
695 psmouse_err(psmouse, "init: Unable to set input params.\n");
696 goto err_exit;
697 }
698
699 psmouse->model = 1;
700 psmouse->protocol_handler = cypress_protocol_handler;
701 psmouse->set_rate = cypress_set_rate;
702 psmouse->disconnect = cypress_disconnect;
703 psmouse->reconnect = cypress_reconnect;
704 psmouse->cleanup = cypress_reset;
705 psmouse->resync_time = 0;
706
707 return 0;
708
709err_exit:
710 /*
711 * Reset Cypress Trackpad as a standard mouse. Then
712 * let psmouse driver commmunicating with it as default PS2 mouse.
713 */
714 cypress_reset(psmouse);
715
716 psmouse->private = NULL;
717 kfree(cytp);
718
719 return -1;
720}
721
722bool cypress_supported(void)
723{
724 return true;
725}
diff --git a/drivers/input/mouse/cypress_ps2.h b/drivers/input/mouse/cypress_ps2.h
new file mode 100644
index 000000000000..4720f21d2d70
--- /dev/null
+++ b/drivers/input/mouse/cypress_ps2.h
@@ -0,0 +1,191 @@
1#ifndef _CYPRESS_PS2_H
2#define _CYPRESS_PS2_H
3
4#include "psmouse.h"
5
6#define CMD_BITS_MASK 0x03
7#define COMPOSIT(x, s) (((x) & CMD_BITS_MASK) << (s))
8
9#define ENCODE_CMD(aa, bb, cc, dd) \
10 (COMPOSIT((aa), 6) | COMPOSIT((bb), 4) | COMPOSIT((cc), 2) | COMPOSIT((dd), 0))
11#define CYTP_CMD_ABS_NO_PRESSURE_MODE ENCODE_CMD(0, 1, 0, 0)
12#define CYTP_CMD_ABS_WITH_PRESSURE_MODE ENCODE_CMD(0, 1, 0, 1)
13#define CYTP_CMD_SMBUS_MODE ENCODE_CMD(0, 1, 1, 0)
14#define CYTP_CMD_STANDARD_MODE ENCODE_CMD(0, 2, 0, 0) /* not implemented yet. */
15#define CYTP_CMD_CYPRESS_REL_MODE ENCODE_CMD(1, 1, 1, 1) /* not implemented yet. */
16#define CYTP_CMD_READ_CYPRESS_ID ENCODE_CMD(0, 0, 0, 0)
17#define CYTP_CMD_READ_TP_METRICS ENCODE_CMD(0, 0, 0, 1)
18#define CYTP_CMD_SET_HSCROLL_WIDTH(w) ENCODE_CMD(1, 1, 0, (w))
19#define CYTP_CMD_SET_HSCROLL_MASK ENCODE_CMD(1, 1, 0, 0)
20#define CYTP_CMD_SET_VSCROLL_WIDTH(w) ENCODE_CMD(1, 2, 0, (w))
21#define CYTP_CMD_SET_VSCROLL_MASK ENCODE_CMD(1, 2, 0, 0)
22#define CYTP_CMD_SET_PALM_GEOMETRY(e) ENCODE_CMD(1, 2, 1, (e))
23#define CYTP_CMD_PALM_GEMMETRY_MASK ENCODE_CMD(1, 2, 1, 0)
24#define CYTP_CMD_SET_PALM_SENSITIVITY(s) ENCODE_CMD(1, 2, 2, (s))
25#define CYTP_CMD_PALM_SENSITIVITY_MASK ENCODE_CMD(1, 2, 2, 0)
26#define CYTP_CMD_SET_MOUSE_SENSITIVITY(s) ENCODE_CMD(1, 3, ((s) >> 2), (s))
27#define CYTP_CMD_MOUSE_SENSITIVITY_MASK ENCODE_CMD(1, 3, 0, 0)
28#define CYTP_CMD_REQUEST_BASELINE_STATUS ENCODE_CMD(2, 0, 0, 1)
29#define CYTP_CMD_REQUEST_RECALIBRATION ENCODE_CMD(2, 0, 0, 3)
30
31#define DECODE_CMD_AA(x) (((x) >> 6) & CMD_BITS_MASK)
32#define DECODE_CMD_BB(x) (((x) >> 4) & CMD_BITS_MASK)
33#define DECODE_CMD_CC(x) (((x) >> 2) & CMD_BITS_MASK)
34#define DECODE_CMD_DD(x) ((x) & CMD_BITS_MASK)
35
36/* Cypress trackpad working mode. */
37#define CYTP_BIT_ABS_PRESSURE (1 << 3)
38#define CYTP_BIT_ABS_NO_PRESSURE (1 << 2)
39#define CYTP_BIT_CYPRESS_REL (1 << 1)
40#define CYTP_BIT_STANDARD_REL (1 << 0)
41#define CYTP_BIT_REL_MASK (CYTP_BIT_CYPRESS_REL | CYTP_BIT_STANDARD_REL)
42#define CYTP_BIT_ABS_MASK (CYTP_BIT_ABS_PRESSURE | CYTP_BIT_ABS_NO_PRESSURE)
43#define CYTP_BIT_ABS_REL_MASK (CYTP_BIT_ABS_MASK | CYTP_BIT_REL_MASK)
44
45#define CYTP_BIT_HIGH_RATE (1 << 4)
46/*
47 * report mode bit is set, firmware working in Remote Mode.
48 * report mode bit is cleared, firmware working in Stream Mode.
49 */
50#define CYTP_BIT_REPORT_MODE (1 << 5)
51
52/* scrolling width values for set HSCROLL and VSCROLL width command. */
53#define SCROLL_WIDTH_NARROW 1
54#define SCROLL_WIDTH_NORMAL 2
55#define SCROLL_WIDTH_WIDE 3
56
57#define PALM_GEOMETRY_ENABLE 1
58#define PALM_GEOMETRY_DISABLE 0
59
60#define TP_METRICS_MASK 0x80
61#define FW_VERSION_MASX 0x7f
62#define FW_VER_HIGH_MASK 0x70
63#define FW_VER_LOW_MASK 0x0f
64
65/* Times to retry a ps2_command and millisecond delay between tries. */
66#define CYTP_PS2_CMD_TRIES 3
67#define CYTP_PS2_CMD_DELAY 500
68
69/* time out for PS/2 command only in milliseconds. */
70#define CYTP_CMD_TIMEOUT 200
71#define CYTP_DATA_TIMEOUT 30
72
73#define CYTP_EXT_CMD 0xe8
74#define CYTP_PS2_RETRY 0xfe
75#define CYTP_PS2_ERROR 0xfc
76
77#define CYTP_RESP_RETRY 0x01
78#define CYTP_RESP_ERROR 0xfe
79
80
81#define CYTP_105001_WIDTH 97 /* Dell XPS 13 */
82#define CYTP_105001_HIGH 59
83#define CYTP_DEFAULT_WIDTH (CYTP_105001_WIDTH)
84#define CYTP_DEFAULT_HIGH (CYTP_105001_HIGH)
85
86#define CYTP_ABS_MAX_X 1600
87#define CYTP_ABS_MAX_Y 900
88#define CYTP_MAX_PRESSURE 255
89#define CYTP_MIN_PRESSURE 0
90
91/* header byte bits of relative package. */
92#define BTN_LEFT_BIT 0x01
93#define BTN_RIGHT_BIT 0x02
94#define BTN_MIDDLE_BIT 0x04
95#define REL_X_SIGN_BIT 0x10
96#define REL_Y_SIGN_BIT 0x20
97
98/* header byte bits of absolute package. */
99#define ABS_VSCROLL_BIT 0x10
100#define ABS_HSCROLL_BIT 0x20
101#define ABS_MULTIFINGER_TAP 0x04
102#define ABS_EDGE_MOTION_MASK 0x80
103
104#define DFLT_RESP_BITS_VALID 0x88 /* SMBus bit should not be set. */
105#define DFLT_RESP_SMBUS_BIT 0x80
106#define DFLT_SMBUS_MODE 0x80
107#define DFLT_PS2_MODE 0x00
108#define DFLT_RESP_BIT_MODE 0x40
109#define DFLT_RESP_REMOTE_MODE 0x40
110#define DFLT_RESP_STREAM_MODE 0x00
111#define DFLT_RESP_BIT_REPORTING 0x20
112#define DFLT_RESP_BIT_SCALING 0x10
113
114#define TP_METRICS_BIT_PALM 0x80
115#define TP_METRICS_BIT_STUBBORN 0x40
116#define TP_METRICS_BIT_2F_JITTER 0x30
117#define TP_METRICS_BIT_1F_JITTER 0x0c
118#define TP_METRICS_BIT_APA 0x02
119#define TP_METRICS_BIT_MTG 0x01
120#define TP_METRICS_BIT_ABS_PKT_FORMAT_SET 0xf0
121#define TP_METRICS_BIT_2F_SPIKE 0x0c
122#define TP_METRICS_BIT_1F_SPIKE 0x03
123
124/* bits of first byte response of E9h-Status Request command. */
125#define RESP_BTN_RIGHT_BIT 0x01
126#define RESP_BTN_MIDDLE_BIT 0x02
127#define RESP_BTN_LEFT_BIT 0x04
128#define RESP_SCALING_BIT 0x10
129#define RESP_ENABLE_BIT 0x20
130#define RESP_REMOTE_BIT 0x40
131#define RESP_SMBUS_BIT 0x80
132
133#define CYTP_MAX_MT_SLOTS 2
134
135struct cytp_contact {
136 int x;
137 int y;
138 int z; /* also named as touch pressure. */
139};
140
141/* The structure of Cypress Trackpad event data. */
142struct cytp_report_data {
143 int contact_cnt;
144 struct cytp_contact contacts[CYTP_MAX_MT_SLOTS];
145 unsigned int left:1;
146 unsigned int right:1;
147 unsigned int middle:1;
148 unsigned int tap:1; /* multi-finger tap detected. */
149};
150
151/* The structure of Cypress Trackpad device private data. */
152struct cytp_data {
153 int fw_version;
154
155 int pkt_size;
156 int mode;
157
158 int tp_min_pressure;
159 int tp_max_pressure;
160 int tp_width; /* X direction physical size in mm. */
161 int tp_high; /* Y direction physical size in mm. */
162 int tp_max_abs_x; /* Max X absolute units that can be reported. */
163 int tp_max_abs_y; /* Max Y absolute units that can be reported. */
164
165 int tp_res_x; /* X resolution in units/mm. */
166 int tp_res_y; /* Y resolution in units/mm. */
167
168 int tp_metrics_supported;
169};
170
171
172#ifdef CONFIG_MOUSE_PS2_CYPRESS
173int cypress_detect(struct psmouse *psmouse, bool set_properties);
174int cypress_init(struct psmouse *psmouse);
175bool cypress_supported(void);
176#else
177inline int cypress_detect(struct psmouse *psmouse, bool set_properties)
178{
179 return -ENOSYS;
180}
181inline int cypress_init(struct psmouse *psmouse)
182{
183 return -ENOSYS;
184}
185inline bool cypress_supported(void)
186{
187 return 0;
188}
189#endif /* CONFIG_MOUSE_PS2_CYPRESS */
190
191#endif /* _CYPRESS_PS2_H */
diff --git a/drivers/input/mouse/psmouse-base.c b/drivers/input/mouse/psmouse-base.c
index 22fe2547e169..cff065f6261c 100644
--- a/drivers/input/mouse/psmouse-base.c
+++ b/drivers/input/mouse/psmouse-base.c
@@ -34,6 +34,7 @@
34#include "touchkit_ps2.h" 34#include "touchkit_ps2.h"
35#include "elantech.h" 35#include "elantech.h"
36#include "sentelic.h" 36#include "sentelic.h"
37#include "cypress_ps2.h"
37 38
38#define DRIVER_DESC "PS/2 mouse driver" 39#define DRIVER_DESC "PS/2 mouse driver"
39 40
@@ -759,6 +760,28 @@ static int psmouse_extensions(struct psmouse *psmouse,
759 } 760 }
760 761
761/* 762/*
763 * Try Cypress Trackpad.
764 * Must try it before Finger Sensing Pad because Finger Sensing Pad probe
765 * upsets some modules of Cypress Trackpads.
766 */
767 if (max_proto > PSMOUSE_IMEX &&
768 cypress_detect(psmouse, set_properties) == 0) {
769 if (cypress_supported()) {
770 if (cypress_init(psmouse) == 0)
771 return PSMOUSE_CYPRESS;
772
773 /*
774 * Finger Sensing Pad probe upsets some modules of
775 * Cypress Trackpad, must avoid Finger Sensing Pad
776 * probe if Cypress Trackpad device detected.
777 */
778 return PSMOUSE_PS2;
779 }
780
781 max_proto = PSMOUSE_IMEX;
782 }
783
784/*
762 * Try ALPS TouchPad 785 * Try ALPS TouchPad
763 */ 786 */
764 if (max_proto > PSMOUSE_IMEX) { 787 if (max_proto > PSMOUSE_IMEX) {
@@ -896,6 +919,15 @@ static const struct psmouse_protocol psmouse_protocols[] = {
896 .alias = "thinkps", 919 .alias = "thinkps",
897 .detect = thinking_detect, 920 .detect = thinking_detect,
898 }, 921 },
922#ifdef CONFIG_MOUSE_PS2_CYPRESS
923 {
924 .type = PSMOUSE_CYPRESS,
925 .name = "CyPS/2",
926 .alias = "cypress",
927 .detect = cypress_detect,
928 .init = cypress_init,
929 },
930#endif
899 { 931 {
900 .type = PSMOUSE_GENPS, 932 .type = PSMOUSE_GENPS,
901 .name = "GenPS/2", 933 .name = "GenPS/2",
diff --git a/drivers/input/mouse/psmouse.h b/drivers/input/mouse/psmouse.h
index fe1df231ba4c..2f0b39d59a9b 100644
--- a/drivers/input/mouse/psmouse.h
+++ b/drivers/input/mouse/psmouse.h
@@ -95,6 +95,7 @@ enum psmouse_type {
95 PSMOUSE_ELANTECH, 95 PSMOUSE_ELANTECH,
96 PSMOUSE_FSP, 96 PSMOUSE_FSP,
97 PSMOUSE_SYNAPTICS_RELATIVE, 97 PSMOUSE_SYNAPTICS_RELATIVE,
98 PSMOUSE_CYPRESS,
98 PSMOUSE_AUTO /* This one should always be last */ 99 PSMOUSE_AUTO /* This one should always be last */
99}; 100};
100 101