xref: /linux/drivers/input/mouse/cypress_ps2.c (revision a3a02a52bcfcbcc4a637d4b68bf1bc391c9fad02)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Cypress Trackpad PS/2 mouse driver
4  *
5  * Copyright (c) 2012 Cypress Semiconductor Corporation.
6  *
7  * Author:
8  *   Dudley Du <dudl@cypress.com>
9  *
10  * Additional contributors include:
11  *   Kamal Mostafa <kamal@canonical.com>
12  *   Kyle Fazzari <git@status.e4ward.com>
13  */
14 
15 #include <linux/module.h>
16 #include <linux/kernel.h>
17 #include <linux/slab.h>
18 #include <linux/serio.h>
19 #include <linux/libps2.h>
20 #include <linux/input.h>
21 #include <linux/input/mt.h>
22 #include <linux/sched.h>
23 #include <linux/wait.h>
24 
25 #include "cypress_ps2.h"
26 
27 #undef CYTP_DEBUG_VERBOSE  /* define this and DEBUG for more verbose dump */
28 
29 static void cypress_set_packet_size(struct psmouse *psmouse, unsigned int n)
30 {
31 	struct cytp_data *cytp = psmouse->private;
32 	cytp->pkt_size = n;
33 }
34 
35 static const u8 cytp_rate[] = {10, 20, 40, 60, 100, 200};
36 static const u8 cytp_resolution[] = {0x00, 0x01, 0x02, 0x03};
37 
38 static int cypress_ps2_sendbyte(struct psmouse *psmouse, u8 cmd)
39 {
40 	struct ps2dev *ps2dev = &psmouse->ps2dev;
41 	int error;
42 
43 	error = ps2_sendbyte(ps2dev, cmd, CYTP_CMD_TIMEOUT);
44 	if (error) {
45 		psmouse_dbg(psmouse,
46 			    "sending command 0x%02x failed, resp 0x%02x, error %d\n",
47 			    cmd, ps2dev->nak, error);
48 		return error;
49 	}
50 
51 #ifdef CYTP_DEBUG_VERBOSE
52 	psmouse_dbg(psmouse, "sending command 0x%02x succeeded\n", cmd);
53 #endif
54 
55 	return 0;
56 }
57 
58 static int cypress_ps2_ext_cmd(struct psmouse *psmouse, u8 prefix, u8 nibble)
59 {
60 	struct ps2dev *ps2dev = &psmouse->ps2dev;
61 	int tries = CYTP_PS2_CMD_TRIES;
62 	int rc;
63 
64 	ps2_begin_command(ps2dev);
65 
66 	do {
67 		/*
68 		 * Send extension command byte (0xE8 or 0xF3).
69 		 * If sending the command fails, send recovery command
70 		 * to make the device return to the ready state.
71 		 */
72 		rc = cypress_ps2_sendbyte(psmouse, prefix);
73 		if (rc == -EAGAIN) {
74 			rc = cypress_ps2_sendbyte(psmouse, 0x00);
75 			if (rc == -EAGAIN)
76 				rc = cypress_ps2_sendbyte(psmouse, 0x0a);
77 		}
78 
79 		if (!rc) {
80 			rc = cypress_ps2_sendbyte(psmouse, nibble);
81 			if (rc == -EAGAIN)
82 				rc = cypress_ps2_sendbyte(psmouse, nibble);
83 
84 			if (!rc)
85 				break;
86 		}
87 	} while (--tries > 0);
88 
89 	ps2_end_command(ps2dev);
90 
91 	return rc;
92 }
93 
94 static int cypress_ps2_read_cmd_status(struct psmouse *psmouse,
95 				       u8 cmd, u8 *param)
96 {
97 	struct ps2dev *ps2dev = &psmouse->ps2dev;
98 	enum psmouse_state old_state;
99 	int pktsize;
100 	int rc;
101 
102 	ps2_begin_command(ps2dev);
103 
104 	old_state = psmouse->state;
105 	psmouse->state = PSMOUSE_CMD_MODE;
106 	psmouse->pktcnt = 0;
107 
108 	pktsize = (cmd == CYTP_CMD_READ_TP_METRICS) ? 8 : 3;
109 	memset(param, 0, pktsize);
110 
111 	rc = cypress_ps2_sendbyte(psmouse, PSMOUSE_CMD_GETINFO & 0xff);
112 	if (rc)
113 		goto out;
114 
115 	if (!wait_event_timeout(ps2dev->wait,
116 				psmouse->pktcnt >= pktsize,
117 				msecs_to_jiffies(CYTP_CMD_TIMEOUT))) {
118 		rc = -ETIMEDOUT;
119 		goto out;
120 	}
121 
122 	memcpy(param, psmouse->packet, pktsize);
123 
124 	psmouse_dbg(psmouse, "Command 0x%02x response data (0x): %*ph\n",
125 			cmd, pktsize, param);
126 
127 out:
128 	psmouse->state = old_state;
129 	psmouse->pktcnt = 0;
130 
131 	ps2_end_command(ps2dev);
132 
133 	return rc;
134 }
135 
136 static bool cypress_verify_cmd_state(struct psmouse *psmouse, u8 cmd, u8* param)
137 {
138 	bool rate_match = false;
139 	bool resolution_match = false;
140 	int i;
141 
142 	/* callers will do further checking. */
143 	if (cmd == CYTP_CMD_READ_CYPRESS_ID ||
144 	    cmd == CYTP_CMD_STANDARD_MODE ||
145 	    cmd == CYTP_CMD_READ_TP_METRICS)
146 		return true;
147 
148 	if ((~param[0] & DFLT_RESP_BITS_VALID) == DFLT_RESP_BITS_VALID &&
149 	    (param[0] & DFLT_RESP_BIT_MODE) == DFLT_RESP_STREAM_MODE) {
150 		for (i = 0; i < sizeof(cytp_resolution); i++)
151 			if (cytp_resolution[i] == param[1])
152 				resolution_match = true;
153 
154 		for (i = 0; i < sizeof(cytp_rate); i++)
155 			if (cytp_rate[i] == param[2])
156 				rate_match = true;
157 
158 		if (resolution_match && rate_match)
159 			return true;
160 	}
161 
162 	psmouse_dbg(psmouse, "verify cmd state failed.\n");
163 	return false;
164 }
165 
166 static int cypress_send_ext_cmd(struct psmouse *psmouse, u8 cmd, u8 *param)
167 {
168 	u8 cmd_prefix = PSMOUSE_CMD_SETRES & 0xff;
169 	int tries = CYTP_PS2_CMD_TRIES;
170 	int error;
171 
172 	psmouse_dbg(psmouse, "send extension cmd 0x%02x, [%d %d %d %d]\n",
173 		 cmd, DECODE_CMD_AA(cmd), DECODE_CMD_BB(cmd),
174 		 DECODE_CMD_CC(cmd), DECODE_CMD_DD(cmd));
175 
176 	do {
177 		cypress_ps2_ext_cmd(psmouse, cmd_prefix, DECODE_CMD_DD(cmd));
178 		cypress_ps2_ext_cmd(psmouse, cmd_prefix, DECODE_CMD_CC(cmd));
179 		cypress_ps2_ext_cmd(psmouse, cmd_prefix, DECODE_CMD_BB(cmd));
180 		cypress_ps2_ext_cmd(psmouse, cmd_prefix, DECODE_CMD_AA(cmd));
181 
182 		error = cypress_ps2_read_cmd_status(psmouse, cmd, param);
183 		if (!error && cypress_verify_cmd_state(psmouse, cmd, param))
184 			return 0;
185 
186 	} while (--tries > 0);
187 
188 	return -EIO;
189 }
190 
191 int cypress_detect(struct psmouse *psmouse, bool set_properties)
192 {
193 	u8 param[3];
194 
195 	if (cypress_send_ext_cmd(psmouse, CYTP_CMD_READ_CYPRESS_ID, param))
196 		return -ENODEV;
197 
198 	/* Check for Cypress Trackpad signature bytes: 0x33 0xCC */
199 	if (param[0] != 0x33 || param[1] != 0xCC)
200 		return -ENODEV;
201 
202 	if (set_properties) {
203 		psmouse->vendor = "Cypress";
204 		psmouse->name = "Trackpad";
205 	}
206 
207 	return 0;
208 }
209 
210 static int cypress_read_fw_version(struct psmouse *psmouse)
211 {
212 	struct cytp_data *cytp = psmouse->private;
213 	u8 param[3];
214 
215 	if (cypress_send_ext_cmd(psmouse, CYTP_CMD_READ_CYPRESS_ID, param))
216 		return -ENODEV;
217 
218 	/* Check for Cypress Trackpad signature bytes: 0x33 0xCC */
219 	if (param[0] != 0x33 || param[1] != 0xCC)
220 		return -ENODEV;
221 
222 	cytp->fw_version = param[2] & FW_VERSION_MASX;
223 	cytp->tp_metrics_supported = (param[2] & TP_METRICS_MASK) ? 1 : 0;
224 
225 	/*
226 	 * Trackpad fw_version 11 (in Dell XPS12) yields a bogus response to
227 	 * CYTP_CMD_READ_TP_METRICS so do not try to use it. LP: #1103594.
228 	 */
229 	if (cytp->fw_version >= 11)
230 		cytp->tp_metrics_supported = 0;
231 
232 	psmouse_dbg(psmouse, "cytp->fw_version = %d\n", cytp->fw_version);
233 	psmouse_dbg(psmouse, "cytp->tp_metrics_supported = %d\n",
234 		 cytp->tp_metrics_supported);
235 
236 	return 0;
237 }
238 
239 static int cypress_read_tp_metrics(struct psmouse *psmouse)
240 {
241 	struct cytp_data *cytp = psmouse->private;
242 	u8 param[8];
243 
244 	/* set default values for tp metrics. */
245 	cytp->tp_width = CYTP_DEFAULT_WIDTH;
246 	cytp->tp_high = CYTP_DEFAULT_HIGH;
247 	cytp->tp_max_abs_x = CYTP_ABS_MAX_X;
248 	cytp->tp_max_abs_y = CYTP_ABS_MAX_Y;
249 	cytp->tp_min_pressure = CYTP_MIN_PRESSURE;
250 	cytp->tp_max_pressure = CYTP_MAX_PRESSURE;
251 	cytp->tp_res_x = cytp->tp_max_abs_x / cytp->tp_width;
252 	cytp->tp_res_y = cytp->tp_max_abs_y / cytp->tp_high;
253 
254 	if (!cytp->tp_metrics_supported)
255 		return 0;
256 
257 	memset(param, 0, sizeof(param));
258 	if (cypress_send_ext_cmd(psmouse, CYTP_CMD_READ_TP_METRICS, param) == 0) {
259 		/* Update trackpad parameters. */
260 		cytp->tp_max_abs_x = (param[1] << 8) | param[0];
261 		cytp->tp_max_abs_y = (param[3] << 8) | param[2];
262 		cytp->tp_min_pressure = param[4];
263 		cytp->tp_max_pressure = param[5];
264 	}
265 
266 	if (!cytp->tp_max_pressure ||
267 	    cytp->tp_max_pressure < cytp->tp_min_pressure ||
268 	    !cytp->tp_width || !cytp->tp_high ||
269 	    !cytp->tp_max_abs_x ||
270 	    cytp->tp_max_abs_x < cytp->tp_width ||
271 	    !cytp->tp_max_abs_y ||
272 	    cytp->tp_max_abs_y < cytp->tp_high)
273 		return -EINVAL;
274 
275 	cytp->tp_res_x = cytp->tp_max_abs_x / cytp->tp_width;
276 	cytp->tp_res_y = cytp->tp_max_abs_y / cytp->tp_high;
277 
278 #ifdef CYTP_DEBUG_VERBOSE
279 	psmouse_dbg(psmouse, "Dump trackpad hardware configuration as below:\n");
280 	psmouse_dbg(psmouse, "cytp->tp_width = %d\n", cytp->tp_width);
281 	psmouse_dbg(psmouse, "cytp->tp_high = %d\n", cytp->tp_high);
282 	psmouse_dbg(psmouse, "cytp->tp_max_abs_x = %d\n", cytp->tp_max_abs_x);
283 	psmouse_dbg(psmouse, "cytp->tp_max_abs_y = %d\n", cytp->tp_max_abs_y);
284 	psmouse_dbg(psmouse, "cytp->tp_min_pressure = %d\n", cytp->tp_min_pressure);
285 	psmouse_dbg(psmouse, "cytp->tp_max_pressure = %d\n", cytp->tp_max_pressure);
286 	psmouse_dbg(psmouse, "cytp->tp_res_x = %d\n", cytp->tp_res_x);
287 	psmouse_dbg(psmouse, "cytp->tp_res_y = %d\n", cytp->tp_res_y);
288 
289 	psmouse_dbg(psmouse, "tp_type_APA = %d\n",
290 			(param[6] & TP_METRICS_BIT_APA) ? 1 : 0);
291 	psmouse_dbg(psmouse, "tp_type_MTG = %d\n",
292 			(param[6] & TP_METRICS_BIT_MTG) ? 1 : 0);
293 	psmouse_dbg(psmouse, "tp_palm = %d\n",
294 			(param[6] & TP_METRICS_BIT_PALM) ? 1 : 0);
295 	psmouse_dbg(psmouse, "tp_stubborn = %d\n",
296 			(param[6] & TP_METRICS_BIT_STUBBORN) ? 1 : 0);
297 	psmouse_dbg(psmouse, "tp_1f_jitter = %d\n",
298 			(param[6] & TP_METRICS_BIT_1F_JITTER) >> 2);
299 	psmouse_dbg(psmouse, "tp_2f_jitter = %d\n",
300 			(param[6] & TP_METRICS_BIT_2F_JITTER) >> 4);
301 	psmouse_dbg(psmouse, "tp_1f_spike = %d\n",
302 			param[7] & TP_METRICS_BIT_1F_SPIKE);
303 	psmouse_dbg(psmouse, "tp_2f_spike = %d\n",
304 			(param[7] & TP_METRICS_BIT_2F_SPIKE) >> 2);
305 	psmouse_dbg(psmouse, "tp_abs_packet_format_set = %d\n",
306 			(param[7] & TP_METRICS_BIT_ABS_PKT_FORMAT_SET) >> 4);
307 #endif
308 
309 	return 0;
310 }
311 
312 static int cypress_query_hardware(struct psmouse *psmouse)
313 {
314 	int error;
315 
316 	error = cypress_read_fw_version(psmouse);
317 	if (error)
318 		return error;
319 
320 	error = cypress_read_tp_metrics(psmouse);
321 	if (error)
322 		return error;
323 
324 	return 0;
325 }
326 
327 static int cypress_set_absolute_mode(struct psmouse *psmouse)
328 {
329 	struct cytp_data *cytp = psmouse->private;
330 	u8 param[3];
331 	int error;
332 
333 	error = cypress_send_ext_cmd(psmouse, CYTP_CMD_ABS_WITH_PRESSURE_MODE,
334 				     param);
335 	if (error)
336 		return error;
337 
338 	cytp->mode = (cytp->mode & ~CYTP_BIT_ABS_REL_MASK)
339 			| CYTP_BIT_ABS_PRESSURE;
340 	cypress_set_packet_size(psmouse, 5);
341 
342 	return 0;
343 }
344 
345 /*
346  * Reset trackpad device.
347  * This is also the default mode when trackpad powered on.
348  */
349 static void cypress_reset(struct psmouse *psmouse)
350 {
351 	struct cytp_data *cytp = psmouse->private;
352 
353 	cytp->mode = 0;
354 
355 	psmouse_reset(psmouse);
356 }
357 
358 static int cypress_set_input_params(struct input_dev *input,
359 				    struct cytp_data *cytp)
360 {
361 	int error;
362 
363 	if (!cytp->tp_res_x || !cytp->tp_res_y)
364 		return -EINVAL;
365 
366 	__set_bit(EV_ABS, input->evbit);
367 	input_set_abs_params(input, ABS_X, 0, cytp->tp_max_abs_x, 0, 0);
368 	input_set_abs_params(input, ABS_Y, 0, cytp->tp_max_abs_y, 0, 0);
369 	input_set_abs_params(input, ABS_PRESSURE,
370 			     cytp->tp_min_pressure, cytp->tp_max_pressure, 0, 0);
371 	input_set_abs_params(input, ABS_TOOL_WIDTH, 0, 255, 0, 0);
372 
373 	/* finger position */
374 	input_set_abs_params(input, ABS_MT_POSITION_X, 0, cytp->tp_max_abs_x, 0, 0);
375 	input_set_abs_params(input, ABS_MT_POSITION_Y, 0, cytp->tp_max_abs_y, 0, 0);
376 	input_set_abs_params(input, ABS_MT_PRESSURE, 0, 255, 0, 0);
377 
378 	error = input_mt_init_slots(input, CYTP_MAX_MT_SLOTS,
379 				    INPUT_MT_DROP_UNUSED | INPUT_MT_TRACK);
380 	if (error)
381 		return error;
382 
383 	__set_bit(INPUT_PROP_SEMI_MT, input->propbit);
384 
385 	input_abs_set_res(input, ABS_X, cytp->tp_res_x);
386 	input_abs_set_res(input, ABS_Y, cytp->tp_res_y);
387 
388 	input_abs_set_res(input, ABS_MT_POSITION_X, cytp->tp_res_x);
389 	input_abs_set_res(input, ABS_MT_POSITION_Y, cytp->tp_res_y);
390 
391 	__set_bit(BTN_TOUCH, input->keybit);
392 	__set_bit(BTN_TOOL_FINGER, input->keybit);
393 	__set_bit(BTN_TOOL_DOUBLETAP, input->keybit);
394 	__set_bit(BTN_TOOL_TRIPLETAP, input->keybit);
395 	__set_bit(BTN_TOOL_QUADTAP, input->keybit);
396 	__set_bit(BTN_TOOL_QUINTTAP, input->keybit);
397 
398 	__clear_bit(EV_REL, input->evbit);
399 	__clear_bit(REL_X, input->relbit);
400 	__clear_bit(REL_Y, input->relbit);
401 
402 	__set_bit(EV_KEY, input->evbit);
403 	__set_bit(BTN_LEFT, input->keybit);
404 	__set_bit(BTN_RIGHT, input->keybit);
405 	__set_bit(BTN_MIDDLE, input->keybit);
406 
407 	return 0;
408 }
409 
410 static int cypress_get_finger_count(u8 header_byte)
411 {
412 	u8 bits6_7;
413 	int finger_count;
414 
415 	bits6_7 = header_byte >> 6;
416 	finger_count = bits6_7 & 0x03;
417 
418 	if (finger_count == 1)
419 		return 1;
420 
421 	if (header_byte & ABS_HSCROLL_BIT) {
422 		/* HSCROLL gets added on to 0 finger count. */
423 		switch (finger_count) {
424 			case 0:	return 4;
425 			case 2: return 5;
426 			default:
427 				/* Invalid contact (e.g. palm). Ignore it. */
428 				return 0;
429 		}
430 	}
431 
432 	return finger_count;
433 }
434 
435 
436 static int cypress_parse_packet(struct psmouse *psmouse,
437 				struct cytp_data *cytp,
438 				struct cytp_report_data *report_data)
439 {
440 	u8 *packet = psmouse->packet;
441 	u8 header_byte = packet[0];
442 
443 	memset(report_data, 0, sizeof(struct cytp_report_data));
444 
445 	report_data->contact_cnt = cypress_get_finger_count(header_byte);
446 	report_data->tap = (header_byte & ABS_MULTIFINGER_TAP) ? 1 : 0;
447 
448 	if (report_data->contact_cnt == 1) {
449 		report_data->contacts[0].x =
450 			((packet[1] & 0x70) << 4) | packet[2];
451 		report_data->contacts[0].y =
452 			((packet[1] & 0x07) << 8) | packet[3];
453 		if (cytp->mode & CYTP_BIT_ABS_PRESSURE)
454 			report_data->contacts[0].z = packet[4];
455 
456 	} else if (report_data->contact_cnt >= 2) {
457 		report_data->contacts[0].x =
458 			((packet[1] & 0x70) << 4) | packet[2];
459 		report_data->contacts[0].y =
460 			((packet[1] & 0x07) << 8) | packet[3];
461 		if (cytp->mode & CYTP_BIT_ABS_PRESSURE)
462 			report_data->contacts[0].z = packet[4];
463 
464 		report_data->contacts[1].x =
465 			((packet[5] & 0xf0) << 4) | packet[6];
466 		report_data->contacts[1].y =
467 			((packet[5] & 0x0f) << 8) | packet[7];
468 		if (cytp->mode & CYTP_BIT_ABS_PRESSURE)
469 			report_data->contacts[1].z = report_data->contacts[0].z;
470 	}
471 
472 	report_data->left = (header_byte & BTN_LEFT_BIT) ? 1 : 0;
473 	report_data->right = (header_byte & BTN_RIGHT_BIT) ? 1 : 0;
474 
475 	/*
476 	 * This is only true if one of the mouse buttons were tapped.  Make
477 	 * sure it doesn't turn into a click. The regular tap-to-click
478 	 * functionality will handle that on its own. If we don't do this,
479 	 * disabling tap-to-click won't affect the mouse button zones.
480 	 */
481 	if (report_data->tap)
482 		report_data->left = 0;
483 
484 #ifdef CYTP_DEBUG_VERBOSE
485 	{
486 		int i;
487 		int n = report_data->contact_cnt;
488 		psmouse_dbg(psmouse, "Dump parsed report data as below:\n");
489 		psmouse_dbg(psmouse, "contact_cnt = %d\n",
490 			report_data->contact_cnt);
491 		if (n > CYTP_MAX_MT_SLOTS)
492 		    n = CYTP_MAX_MT_SLOTS;
493 		for (i = 0; i < n; i++)
494 			psmouse_dbg(psmouse, "contacts[%d] = {%d, %d, %d}\n", i,
495 					report_data->contacts[i].x,
496 					report_data->contacts[i].y,
497 					report_data->contacts[i].z);
498 		psmouse_dbg(psmouse, "left = %d\n", report_data->left);
499 		psmouse_dbg(psmouse, "right = %d\n", report_data->right);
500 		psmouse_dbg(psmouse, "middle = %d\n", report_data->middle);
501 	}
502 #endif
503 
504 	return 0;
505 }
506 
507 static void cypress_process_packet(struct psmouse *psmouse, bool zero_pkt)
508 {
509 	int i;
510 	struct input_dev *input = psmouse->dev;
511 	struct cytp_data *cytp = psmouse->private;
512 	struct cytp_report_data report_data;
513 	struct cytp_contact *contact;
514 	struct input_mt_pos pos[CYTP_MAX_MT_SLOTS];
515 	int slots[CYTP_MAX_MT_SLOTS];
516 	int n;
517 
518 	cypress_parse_packet(psmouse, cytp, &report_data);
519 
520 	n = report_data.contact_cnt;
521 	if (n > CYTP_MAX_MT_SLOTS)
522 		n = CYTP_MAX_MT_SLOTS;
523 
524 	for (i = 0; i < n; i++) {
525 		contact = &report_data.contacts[i];
526 		pos[i].x = contact->x;
527 		pos[i].y = contact->y;
528 	}
529 
530 	input_mt_assign_slots(input, slots, pos, n, 0);
531 
532 	for (i = 0; i < n; i++) {
533 		contact = &report_data.contacts[i];
534 		input_mt_slot(input, slots[i]);
535 		input_mt_report_slot_state(input, MT_TOOL_FINGER, true);
536 		input_report_abs(input, ABS_MT_POSITION_X, contact->x);
537 		input_report_abs(input, ABS_MT_POSITION_Y, contact->y);
538 		input_report_abs(input, ABS_MT_PRESSURE, contact->z);
539 	}
540 
541 	input_mt_sync_frame(input);
542 
543 	input_mt_report_finger_count(input, report_data.contact_cnt);
544 
545 	input_report_key(input, BTN_LEFT, report_data.left);
546 	input_report_key(input, BTN_RIGHT, report_data.right);
547 	input_report_key(input, BTN_MIDDLE, report_data.middle);
548 
549 	input_sync(input);
550 }
551 
552 static psmouse_ret_t cypress_validate_byte(struct psmouse *psmouse)
553 {
554 	int contact_cnt;
555 	int index = psmouse->pktcnt - 1;
556 	u8 *packet = psmouse->packet;
557 	struct cytp_data *cytp = psmouse->private;
558 
559 	if (index < 0 || index > cytp->pkt_size)
560 		return PSMOUSE_BAD_DATA;
561 
562 	if (index == 0 && (packet[0] & 0xfc) == 0) {
563 		/* call packet process for reporting finger leave. */
564 		cypress_process_packet(psmouse, 1);
565 		return PSMOUSE_FULL_PACKET;
566 	}
567 
568 	/*
569 	 * Perform validation (and adjust packet size) based only on the
570 	 * first byte; allow all further bytes through.
571 	 */
572 	if (index != 0)
573 		return PSMOUSE_GOOD_DATA;
574 
575 	/*
576 	 * If absolute/relative mode bit has not been set yet, just pass
577 	 * the byte through.
578 	 */
579 	if ((cytp->mode & CYTP_BIT_ABS_REL_MASK) == 0)
580 		return PSMOUSE_GOOD_DATA;
581 
582 	if ((packet[0] & 0x08) == 0x08)
583 		return PSMOUSE_BAD_DATA;
584 
585 	contact_cnt = cypress_get_finger_count(packet[0]);
586 	if (cytp->mode & CYTP_BIT_ABS_NO_PRESSURE)
587 		cypress_set_packet_size(psmouse, contact_cnt == 2 ? 7 : 4);
588 	else
589 		cypress_set_packet_size(psmouse, contact_cnt == 2 ? 8 : 5);
590 
591 	return PSMOUSE_GOOD_DATA;
592 }
593 
594 static psmouse_ret_t cypress_protocol_handler(struct psmouse *psmouse)
595 {
596 	struct cytp_data *cytp = psmouse->private;
597 
598 	if (psmouse->pktcnt >= cytp->pkt_size) {
599 		cypress_process_packet(psmouse, 0);
600 		return PSMOUSE_FULL_PACKET;
601 	}
602 
603 	return cypress_validate_byte(psmouse);
604 }
605 
606 static void cypress_set_rate(struct psmouse *psmouse, unsigned int rate)
607 {
608 	struct cytp_data *cytp = psmouse->private;
609 	u8 rate_param;
610 
611 	if (rate >= 80) {
612 		psmouse->rate = 80;
613 		cytp->mode |= CYTP_BIT_HIGH_RATE;
614 	} else {
615 		psmouse->rate = 40;
616 		cytp->mode &= ~CYTP_BIT_HIGH_RATE;
617 	}
618 
619 	rate_param = (u8)rate;
620 	ps2_command(&psmouse->ps2dev, &rate_param, PSMOUSE_CMD_SETRATE);
621 }
622 
623 static void cypress_disconnect(struct psmouse *psmouse)
624 {
625 	cypress_reset(psmouse);
626 	kfree(psmouse->private);
627 	psmouse->private = NULL;
628 }
629 
630 static int cypress_reconnect(struct psmouse *psmouse)
631 {
632 	int tries = CYTP_PS2_CMD_TRIES;
633 	int error;
634 
635 	do {
636 		cypress_reset(psmouse);
637 		error = cypress_detect(psmouse, false);
638 	} while (error && (--tries > 0));
639 
640 	if (error) {
641 		psmouse_err(psmouse, "Reconnect: unable to detect trackpad.\n");
642 		return error;
643 	}
644 
645 	error = cypress_set_absolute_mode(psmouse);
646 	if (error) {
647 		psmouse_err(psmouse, "Reconnect: Unable to initialize Cypress absolute mode.\n");
648 		return error;
649 	}
650 
651 	return 0;
652 }
653 
654 int cypress_init(struct psmouse *psmouse)
655 {
656 	struct cytp_data *cytp;
657 	int error;
658 
659 	cytp = kzalloc(sizeof(*cytp), GFP_KERNEL);
660 	if (!cytp)
661 		return -ENOMEM;
662 
663 	psmouse->private = cytp;
664 	psmouse->pktsize = 8;
665 
666 	cypress_reset(psmouse);
667 
668 	error = cypress_query_hardware(psmouse);
669 	if (error) {
670 		psmouse_err(psmouse, "Unable to query Trackpad hardware.\n");
671 		goto err_exit;
672 	}
673 
674 	error = cypress_set_absolute_mode(psmouse);
675 	if (error) {
676 		psmouse_err(psmouse, "init: Unable to initialize Cypress absolute mode.\n");
677 		goto err_exit;
678 	}
679 
680 	error = cypress_set_input_params(psmouse->dev, cytp);
681 	if (error) {
682 		psmouse_err(psmouse, "init: Unable to set input params.\n");
683 		goto err_exit;
684 	}
685 
686 	psmouse->model = 1;
687 	psmouse->protocol_handler = cypress_protocol_handler;
688 	psmouse->set_rate = cypress_set_rate;
689 	psmouse->disconnect = cypress_disconnect;
690 	psmouse->reconnect = cypress_reconnect;
691 	psmouse->cleanup = cypress_reset;
692 	psmouse->resync_time = 0;
693 
694 	return 0;
695 
696 err_exit:
697 	/*
698 	 * Reset Cypress Trackpad as a standard mouse. Then
699 	 * let psmouse driver communicating with it as default PS2 mouse.
700 	 */
701 	cypress_reset(psmouse);
702 
703 	psmouse->private = NULL;
704 	kfree(cytp);
705 
706 	return error;
707 }
708