1 /*
2  * PS/2 mouse driver
3  *
4  * Copyright (c) 1999-2002 Vojtech Pavlik
5  * Copyright (c) 2003-2004 Dmitry Torokhov
6  */
7 
8 /*
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU General Public License version 2 as published by
11  * the Free Software Foundation.
12  */
13 
14 #define pr_fmt(fmt)		KBUILD_MODNAME ": " fmt
15 #define psmouse_fmt(fmt)	fmt
16 
17 #include <linux/delay.h>
18 #include <linux/module.h>
19 #include <linux/slab.h>
20 #include <linux/interrupt.h>
21 #include <linux/input.h>
22 #include <linux/serio.h>
23 #include <linux/init.h>
24 #include <linux/libps2.h>
25 #include <linux/mutex.h>
26 
27 #include "psmouse.h"
28 #include "synaptics.h"
29 #include "logips2pp.h"
30 #include "alps.h"
31 #include "hgpk.h"
32 #include "lifebook.h"
33 #include "trackpoint.h"
34 #include "touchkit_ps2.h"
35 #include "elantech.h"
36 #include "sentelic.h"
37 #include "cypress_ps2.h"
38 #include "focaltech.h"
39 
40 #define DRIVER_DESC	"PS/2 mouse driver"
41 
42 MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
43 MODULE_DESCRIPTION(DRIVER_DESC);
44 MODULE_LICENSE("GPL");
45 
46 static unsigned int psmouse_max_proto = PSMOUSE_AUTO;
47 static int psmouse_set_maxproto(const char *val, const struct kernel_param *);
48 static int psmouse_get_maxproto(char *buffer, const struct kernel_param *kp);
49 static struct kernel_param_ops param_ops_proto_abbrev = {
50 	.set = psmouse_set_maxproto,
51 	.get = psmouse_get_maxproto,
52 };
53 #define param_check_proto_abbrev(name, p)	__param_check(name, p, unsigned int)
54 module_param_named(proto, psmouse_max_proto, proto_abbrev, 0644);
55 MODULE_PARM_DESC(proto, "Highest protocol extension to probe (bare, imps, exps, any). Useful for KVM switches.");
56 
57 static unsigned int psmouse_resolution = 200;
58 module_param_named(resolution, psmouse_resolution, uint, 0644);
59 MODULE_PARM_DESC(resolution, "Resolution, in dpi.");
60 
61 static unsigned int psmouse_rate = 100;
62 module_param_named(rate, psmouse_rate, uint, 0644);
63 MODULE_PARM_DESC(rate, "Report rate, in reports per second.");
64 
65 static bool psmouse_smartscroll = 1;
66 module_param_named(smartscroll, psmouse_smartscroll, bool, 0644);
67 MODULE_PARM_DESC(smartscroll, "Logitech Smartscroll autorepeat, 1 = enabled (default), 0 = disabled.");
68 
69 static unsigned int psmouse_resetafter = 5;
70 module_param_named(resetafter, psmouse_resetafter, uint, 0644);
71 MODULE_PARM_DESC(resetafter, "Reset device after so many bad packets (0 = never).");
72 
73 static unsigned int psmouse_resync_time;
74 module_param_named(resync_time, psmouse_resync_time, uint, 0644);
75 MODULE_PARM_DESC(resync_time, "How long can mouse stay idle before forcing resync (in seconds, 0 = never).");
76 
77 PSMOUSE_DEFINE_ATTR(protocol, S_IWUSR | S_IRUGO,
78 			NULL,
79 			psmouse_attr_show_protocol, psmouse_attr_set_protocol);
80 PSMOUSE_DEFINE_ATTR(rate, S_IWUSR | S_IRUGO,
81 			(void *) offsetof(struct psmouse, rate),
82 			psmouse_show_int_attr, psmouse_attr_set_rate);
83 PSMOUSE_DEFINE_ATTR(resolution, S_IWUSR | S_IRUGO,
84 			(void *) offsetof(struct psmouse, resolution),
85 			psmouse_show_int_attr, psmouse_attr_set_resolution);
86 PSMOUSE_DEFINE_ATTR(resetafter, S_IWUSR | S_IRUGO,
87 			(void *) offsetof(struct psmouse, resetafter),
88 			psmouse_show_int_attr, psmouse_set_int_attr);
89 PSMOUSE_DEFINE_ATTR(resync_time, S_IWUSR | S_IRUGO,
90 			(void *) offsetof(struct psmouse, resync_time),
91 			psmouse_show_int_attr, psmouse_set_int_attr);
92 
93 static struct attribute *psmouse_attributes[] = {
94 	&psmouse_attr_protocol.dattr.attr,
95 	&psmouse_attr_rate.dattr.attr,
96 	&psmouse_attr_resolution.dattr.attr,
97 	&psmouse_attr_resetafter.dattr.attr,
98 	&psmouse_attr_resync_time.dattr.attr,
99 	NULL
100 };
101 
102 static struct attribute_group psmouse_attribute_group = {
103 	.attrs	= psmouse_attributes,
104 };
105 
106 /*
107  * psmouse_mutex protects all operations changing state of mouse
108  * (connecting, disconnecting, changing rate or resolution via
109  * sysfs). We could use a per-device semaphore but since there
110  * rarely more than one PS/2 mouse connected and since semaphore
111  * is taken in "slow" paths it is not worth it.
112  */
113 static DEFINE_MUTEX(psmouse_mutex);
114 
115 static struct workqueue_struct *kpsmoused_wq;
116 
117 struct psmouse_protocol {
118 	enum psmouse_type type;
119 	bool maxproto;
120 	bool ignore_parity; /* Protocol should ignore parity errors from KBC */
121 	const char *name;
122 	const char *alias;
123 	int (*detect)(struct psmouse *, bool);
124 	int (*init)(struct psmouse *);
125 };
126 
127 /*
128  * psmouse_process_byte() analyzes the PS/2 data stream and reports
129  * relevant events to the input module once full packet has arrived.
130  */
131 
132 psmouse_ret_t psmouse_process_byte(struct psmouse *psmouse)
133 {
134 	struct input_dev *dev = psmouse->dev;
135 	unsigned char *packet = psmouse->packet;
136 
137 	if (psmouse->pktcnt < psmouse->pktsize)
138 		return PSMOUSE_GOOD_DATA;
139 
140 /*
141  * Full packet accumulated, process it
142  */
143 
144 /*
145  * Scroll wheel on IntelliMice, scroll buttons on NetMice
146  */
147 
148 	if (psmouse->type == PSMOUSE_IMPS || psmouse->type == PSMOUSE_GENPS)
149 		input_report_rel(dev, REL_WHEEL, -(signed char) packet[3]);
150 
151 /*
152  * Scroll wheel and buttons on IntelliMouse Explorer
153  */
154 
155 	if (psmouse->type == PSMOUSE_IMEX) {
156 		switch (packet[3] & 0xC0) {
157 		case 0x80: /* vertical scroll on IntelliMouse Explorer 4.0 */
158 			input_report_rel(dev, REL_WHEEL, (int) (packet[3] & 32) - (int) (packet[3] & 31));
159 			break;
160 		case 0x40: /* horizontal scroll on IntelliMouse Explorer 4.0 */
161 			input_report_rel(dev, REL_HWHEEL, (int) (packet[3] & 32) - (int) (packet[3] & 31));
162 			break;
163 		case 0x00:
164 		case 0xC0:
165 			input_report_rel(dev, REL_WHEEL, (int) (packet[3] & 8) - (int) (packet[3] & 7));
166 			input_report_key(dev, BTN_SIDE, (packet[3] >> 4) & 1);
167 			input_report_key(dev, BTN_EXTRA, (packet[3] >> 5) & 1);
168 			break;
169 		}
170 	}
171 
172 /*
173  * Extra buttons on Genius NewNet 3D
174  */
175 
176 	if (psmouse->type == PSMOUSE_GENPS) {
177 		input_report_key(dev, BTN_SIDE, (packet[0] >> 6) & 1);
178 		input_report_key(dev, BTN_EXTRA, (packet[0] >> 7) & 1);
179 	}
180 
181 /*
182  * Extra button on ThinkingMouse
183  */
184 	if (psmouse->type == PSMOUSE_THINKPS) {
185 		input_report_key(dev, BTN_EXTRA, (packet[0] >> 3) & 1);
186 		/* Without this bit of weirdness moving up gives wildly high Y changes. */
187 		packet[1] |= (packet[0] & 0x40) << 1;
188 	}
189 
190 /*
191  * Cortron PS2 Trackball reports SIDE button on the 4th bit of the first
192  * byte.
193  */
194 	if (psmouse->type == PSMOUSE_CORTRON) {
195 		input_report_key(dev, BTN_SIDE, (packet[0] >> 3) & 1);
196 		packet[0] |= 0x08;
197 	}
198 
199 /*
200  * Generic PS/2 Mouse
201  */
202 
203 	input_report_key(dev, BTN_LEFT,    packet[0]       & 1);
204 	input_report_key(dev, BTN_MIDDLE, (packet[0] >> 2) & 1);
205 	input_report_key(dev, BTN_RIGHT,  (packet[0] >> 1) & 1);
206 
207 	input_report_rel(dev, REL_X, packet[1] ? (int) packet[1] - (int) ((packet[0] << 4) & 0x100) : 0);
208 	input_report_rel(dev, REL_Y, packet[2] ? (int) ((packet[0] << 3) & 0x100) - (int) packet[2] : 0);
209 
210 	input_sync(dev);
211 
212 	return PSMOUSE_FULL_PACKET;
213 }
214 
215 void psmouse_queue_work(struct psmouse *psmouse, struct delayed_work *work,
216 		unsigned long delay)
217 {
218 	queue_delayed_work(kpsmoused_wq, work, delay);
219 }
220 
221 /*
222  * __psmouse_set_state() sets new psmouse state and resets all flags.
223  */
224 
225 static inline void __psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
226 {
227 	psmouse->state = new_state;
228 	psmouse->pktcnt = psmouse->out_of_sync_cnt = 0;
229 	psmouse->ps2dev.flags = 0;
230 	psmouse->last = jiffies;
231 }
232 
233 
234 /*
235  * psmouse_set_state() sets new psmouse state and resets all flags and
236  * counters while holding serio lock so fighting with interrupt handler
237  * is not a concern.
238  */
239 
240 void psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
241 {
242 	serio_pause_rx(psmouse->ps2dev.serio);
243 	__psmouse_set_state(psmouse, new_state);
244 	serio_continue_rx(psmouse->ps2dev.serio);
245 }
246 
247 /*
248  * psmouse_handle_byte() processes one byte of the input data stream
249  * by calling corresponding protocol handler.
250  */
251 
252 static int psmouse_handle_byte(struct psmouse *psmouse)
253 {
254 	psmouse_ret_t rc = psmouse->protocol_handler(psmouse);
255 
256 	switch (rc) {
257 	case PSMOUSE_BAD_DATA:
258 		if (psmouse->state == PSMOUSE_ACTIVATED) {
259 			psmouse_warn(psmouse,
260 				     "%s at %s lost sync at byte %d\n",
261 				     psmouse->name, psmouse->phys,
262 				     psmouse->pktcnt);
263 			if (++psmouse->out_of_sync_cnt == psmouse->resetafter) {
264 				__psmouse_set_state(psmouse, PSMOUSE_IGNORE);
265 				psmouse_notice(psmouse,
266 						"issuing reconnect request\n");
267 				serio_reconnect(psmouse->ps2dev.serio);
268 				return -1;
269 			}
270 		}
271 		psmouse->pktcnt = 0;
272 		break;
273 
274 	case PSMOUSE_FULL_PACKET:
275 		psmouse->pktcnt = 0;
276 		if (psmouse->out_of_sync_cnt) {
277 			psmouse->out_of_sync_cnt = 0;
278 			psmouse_notice(psmouse,
279 					"%s at %s - driver resynced.\n",
280 					psmouse->name, psmouse->phys);
281 		}
282 		break;
283 
284 	case PSMOUSE_GOOD_DATA:
285 		break;
286 	}
287 	return 0;
288 }
289 
290 /*
291  * psmouse_interrupt() handles incoming characters, either passing them
292  * for normal processing or gathering them as command response.
293  */
294 
295 static irqreturn_t psmouse_interrupt(struct serio *serio,
296 		unsigned char data, unsigned int flags)
297 {
298 	struct psmouse *psmouse = serio_get_drvdata(serio);
299 
300 	if (psmouse->state == PSMOUSE_IGNORE)
301 		goto out;
302 
303 	if (unlikely((flags & SERIO_TIMEOUT) ||
304 		     ((flags & SERIO_PARITY) && !psmouse->ignore_parity))) {
305 
306 		if (psmouse->state == PSMOUSE_ACTIVATED)
307 			psmouse_warn(psmouse,
308 				     "bad data from KBC -%s%s\n",
309 				     flags & SERIO_TIMEOUT ? " timeout" : "",
310 				     flags & SERIO_PARITY ? " bad parity" : "");
311 		ps2_cmd_aborted(&psmouse->ps2dev);
312 		goto out;
313 	}
314 
315 	if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_ACK))
316 		if  (ps2_handle_ack(&psmouse->ps2dev, data))
317 			goto out;
318 
319 	if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_CMD))
320 		if  (ps2_handle_response(&psmouse->ps2dev, data))
321 			goto out;
322 
323 	if (psmouse->state <= PSMOUSE_RESYNCING)
324 		goto out;
325 
326 	if (psmouse->state == PSMOUSE_ACTIVATED &&
327 	    psmouse->pktcnt && time_after(jiffies, psmouse->last + HZ/2)) {
328 		psmouse_info(psmouse, "%s at %s lost synchronization, throwing %d bytes away.\n",
329 			     psmouse->name, psmouse->phys, psmouse->pktcnt);
330 		psmouse->badbyte = psmouse->packet[0];
331 		__psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
332 		psmouse_queue_work(psmouse, &psmouse->resync_work, 0);
333 		goto out;
334 	}
335 
336 	psmouse->packet[psmouse->pktcnt++] = data;
337 /*
338  * Check if this is a new device announcement (0xAA 0x00)
339  */
340 	if (unlikely(psmouse->packet[0] == PSMOUSE_RET_BAT && psmouse->pktcnt <= 2)) {
341 		if (psmouse->pktcnt == 1) {
342 			psmouse->last = jiffies;
343 			goto out;
344 		}
345 
346 		if (psmouse->packet[1] == PSMOUSE_RET_ID ||
347 		    (psmouse->type == PSMOUSE_HGPK &&
348 		     psmouse->packet[1] == PSMOUSE_RET_BAT)) {
349 			__psmouse_set_state(psmouse, PSMOUSE_IGNORE);
350 			serio_reconnect(serio);
351 			goto out;
352 		}
353 /*
354  * Not a new device, try processing first byte normally
355  */
356 		psmouse->pktcnt = 1;
357 		if (psmouse_handle_byte(psmouse))
358 			goto out;
359 
360 		psmouse->packet[psmouse->pktcnt++] = data;
361 	}
362 
363 /*
364  * See if we need to force resync because mouse was idle for too long
365  */
366 	if (psmouse->state == PSMOUSE_ACTIVATED &&
367 	    psmouse->pktcnt == 1 && psmouse->resync_time &&
368 	    time_after(jiffies, psmouse->last + psmouse->resync_time * HZ)) {
369 		psmouse->badbyte = psmouse->packet[0];
370 		__psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
371 		psmouse_queue_work(psmouse, &psmouse->resync_work, 0);
372 		goto out;
373 	}
374 
375 	psmouse->last = jiffies;
376 	psmouse_handle_byte(psmouse);
377 
378  out:
379 	return IRQ_HANDLED;
380 }
381 
382 
383 /*
384  * psmouse_sliced_command() sends an extended PS/2 command to the mouse
385  * using sliced syntax, understood by advanced devices, such as Logitech
386  * or Synaptics touchpads. The command is encoded as:
387  * 0xE6 0xE8 rr 0xE8 ss 0xE8 tt 0xE8 uu where (rr*64)+(ss*16)+(tt*4)+uu
388  * is the command.
389  */
390 int psmouse_sliced_command(struct psmouse *psmouse, unsigned char command)
391 {
392 	int i;
393 
394 	if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11))
395 		return -1;
396 
397 	for (i = 6; i >= 0; i -= 2) {
398 		unsigned char d = (command >> i) & 3;
399 		if (ps2_command(&psmouse->ps2dev, &d, PSMOUSE_CMD_SETRES))
400 			return -1;
401 	}
402 
403 	return 0;
404 }
405 
406 
407 /*
408  * psmouse_reset() resets the mouse into power-on state.
409  */
410 int psmouse_reset(struct psmouse *psmouse)
411 {
412 	unsigned char param[2];
413 
414 	if (ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_RESET_BAT))
415 		return -1;
416 
417 	if (param[0] != PSMOUSE_RET_BAT && param[1] != PSMOUSE_RET_ID)
418 		return -1;
419 
420 	return 0;
421 }
422 
423 /*
424  * Here we set the mouse resolution.
425  */
426 
427 void psmouse_set_resolution(struct psmouse *psmouse, unsigned int resolution)
428 {
429 	static const unsigned char params[] = { 0, 1, 2, 2, 3 };
430 	unsigned char p;
431 
432 	if (resolution == 0 || resolution > 200)
433 		resolution = 200;
434 
435 	p = params[resolution / 50];
436 	ps2_command(&psmouse->ps2dev, &p, PSMOUSE_CMD_SETRES);
437 	psmouse->resolution = 25 << p;
438 }
439 
440 /*
441  * Here we set the mouse report rate.
442  */
443 
444 static void psmouse_set_rate(struct psmouse *psmouse, unsigned int rate)
445 {
446 	static const unsigned char rates[] = { 200, 100, 80, 60, 40, 20, 10, 0 };
447 	unsigned char r;
448 	int i = 0;
449 
450 	while (rates[i] > rate) i++;
451 	r = rates[i];
452 	ps2_command(&psmouse->ps2dev, &r, PSMOUSE_CMD_SETRATE);
453 	psmouse->rate = r;
454 }
455 
456 /*
457  * psmouse_poll() - default poll handler. Everyone except for ALPS uses it.
458  */
459 
460 static int psmouse_poll(struct psmouse *psmouse)
461 {
462 	return ps2_command(&psmouse->ps2dev, psmouse->packet,
463 			   PSMOUSE_CMD_POLL | (psmouse->pktsize << 8));
464 }
465 
466 /*
467  * psmouse_matches_pnp_id - check if psmouse matches one of the passed in ids.
468  */
469 bool psmouse_matches_pnp_id(struct psmouse *psmouse, const char * const ids[])
470 {
471 	int i;
472 
473 	if (!strncmp(psmouse->ps2dev.serio->firmware_id, "PNP:", 4))
474 		for (i = 0; ids[i]; i++)
475 			if (strstr(psmouse->ps2dev.serio->firmware_id, ids[i]))
476 				return true;
477 
478 	return false;
479 }
480 
481 /*
482  * Genius NetMouse magic init.
483  */
484 static int genius_detect(struct psmouse *psmouse, bool set_properties)
485 {
486 	struct ps2dev *ps2dev = &psmouse->ps2dev;
487 	unsigned char param[4];
488 
489 	param[0] = 3;
490 	ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
491 	ps2_command(ps2dev,  NULL, PSMOUSE_CMD_SETSCALE11);
492 	ps2_command(ps2dev,  NULL, PSMOUSE_CMD_SETSCALE11);
493 	ps2_command(ps2dev,  NULL, PSMOUSE_CMD_SETSCALE11);
494 	ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO);
495 
496 	if (param[0] != 0x00 || param[1] != 0x33 || param[2] != 0x55)
497 		return -1;
498 
499 	if (set_properties) {
500 		__set_bit(BTN_MIDDLE, psmouse->dev->keybit);
501 		__set_bit(BTN_EXTRA, psmouse->dev->keybit);
502 		__set_bit(BTN_SIDE, psmouse->dev->keybit);
503 		__set_bit(REL_WHEEL, psmouse->dev->relbit);
504 
505 		psmouse->vendor = "Genius";
506 		psmouse->name = "Mouse";
507 		psmouse->pktsize = 4;
508 	}
509 
510 	return 0;
511 }
512 
513 /*
514  * IntelliMouse magic init.
515  */
516 static int intellimouse_detect(struct psmouse *psmouse, bool set_properties)
517 {
518 	struct ps2dev *ps2dev = &psmouse->ps2dev;
519 	unsigned char param[2];
520 
521 	param[0] = 200;
522 	ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
523 	param[0] = 100;
524 	ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
525 	param[0] =  80;
526 	ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
527 	ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
528 
529 	if (param[0] != 3)
530 		return -1;
531 
532 	if (set_properties) {
533 		__set_bit(BTN_MIDDLE, psmouse->dev->keybit);
534 		__set_bit(REL_WHEEL, psmouse->dev->relbit);
535 
536 		if (!psmouse->vendor)
537 			psmouse->vendor = "Generic";
538 		if (!psmouse->name)
539 			psmouse->name = "Wheel Mouse";
540 		psmouse->pktsize = 4;
541 	}
542 
543 	return 0;
544 }
545 
546 /*
547  * Try IntelliMouse/Explorer magic init.
548  */
549 static int im_explorer_detect(struct psmouse *psmouse, bool set_properties)
550 {
551 	struct ps2dev *ps2dev = &psmouse->ps2dev;
552 	unsigned char param[2];
553 
554 	intellimouse_detect(psmouse, 0);
555 
556 	param[0] = 200;
557 	ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
558 	param[0] = 200;
559 	ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
560 	param[0] =  80;
561 	ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
562 	ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
563 
564 	if (param[0] != 4)
565 		return -1;
566 
567 /* Magic to enable horizontal scrolling on IntelliMouse 4.0 */
568 	param[0] = 200;
569 	ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
570 	param[0] =  80;
571 	ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
572 	param[0] =  40;
573 	ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
574 
575 	if (set_properties) {
576 		__set_bit(BTN_MIDDLE, psmouse->dev->keybit);
577 		__set_bit(REL_WHEEL, psmouse->dev->relbit);
578 		__set_bit(REL_HWHEEL, psmouse->dev->relbit);
579 		__set_bit(BTN_SIDE, psmouse->dev->keybit);
580 		__set_bit(BTN_EXTRA, psmouse->dev->keybit);
581 
582 		if (!psmouse->vendor)
583 			psmouse->vendor = "Generic";
584 		if (!psmouse->name)
585 			psmouse->name = "Explorer Mouse";
586 		psmouse->pktsize = 4;
587 	}
588 
589 	return 0;
590 }
591 
592 /*
593  * Kensington ThinkingMouse / ExpertMouse magic init.
594  */
595 static int thinking_detect(struct psmouse *psmouse, bool set_properties)
596 {
597 	struct ps2dev *ps2dev = &psmouse->ps2dev;
598 	unsigned char param[2];
599 	static const unsigned char seq[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 };
600 	int i;
601 
602 	param[0] = 10;
603 	ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
604 	param[0] = 0;
605 	ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
606 	for (i = 0; i < ARRAY_SIZE(seq); i++) {
607 		param[0] = seq[i];
608 		ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
609 	}
610 	ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
611 
612 	if (param[0] != 2)
613 		return -1;
614 
615 	if (set_properties) {
616 		__set_bit(BTN_MIDDLE, psmouse->dev->keybit);
617 		__set_bit(BTN_EXTRA, psmouse->dev->keybit);
618 
619 		psmouse->vendor = "Kensington";
620 		psmouse->name = "ThinkingMouse";
621 	}
622 
623 	return 0;
624 }
625 
626 /*
627  * Bare PS/2 protocol "detection". Always succeeds.
628  */
629 static int ps2bare_detect(struct psmouse *psmouse, bool set_properties)
630 {
631 	if (set_properties) {
632 		if (!psmouse->vendor)
633 			psmouse->vendor = "Generic";
634 		if (!psmouse->name)
635 			psmouse->name = "Mouse";
636 
637 /*
638  * We have no way of figuring true number of buttons so let's
639  * assume that the device has 3.
640  */
641 		__set_bit(BTN_MIDDLE, psmouse->dev->keybit);
642 	}
643 
644 	return 0;
645 }
646 
647 /*
648  * Cortron PS/2 protocol detection. There's no special way to detect it, so it
649  * must be forced by sysfs protocol writing.
650  */
651 static int cortron_detect(struct psmouse *psmouse, bool set_properties)
652 {
653 	if (set_properties) {
654 		psmouse->vendor = "Cortron";
655 		psmouse->name = "PS/2 Trackball";
656 
657 		__set_bit(BTN_MIDDLE, psmouse->dev->keybit);
658 		__set_bit(BTN_SIDE, psmouse->dev->keybit);
659 	}
660 
661 	return 0;
662 }
663 
664 /*
665  * Apply default settings to the psmouse structure. Most of them will
666  * be overridden by individual protocol initialization routines.
667  */
668 
669 static void psmouse_apply_defaults(struct psmouse *psmouse)
670 {
671 	struct input_dev *input_dev = psmouse->dev;
672 
673 	memset(input_dev->evbit, 0, sizeof(input_dev->evbit));
674 	memset(input_dev->keybit, 0, sizeof(input_dev->keybit));
675 	memset(input_dev->relbit, 0, sizeof(input_dev->relbit));
676 	memset(input_dev->absbit, 0, sizeof(input_dev->absbit));
677 	memset(input_dev->mscbit, 0, sizeof(input_dev->mscbit));
678 
679 	__set_bit(EV_KEY, input_dev->evbit);
680 	__set_bit(EV_REL, input_dev->evbit);
681 
682 	__set_bit(BTN_LEFT, input_dev->keybit);
683 	__set_bit(BTN_RIGHT, input_dev->keybit);
684 
685 	__set_bit(REL_X, input_dev->relbit);
686 	__set_bit(REL_Y, input_dev->relbit);
687 
688 	__set_bit(INPUT_PROP_POINTER, input_dev->propbit);
689 
690 	psmouse->set_rate = psmouse_set_rate;
691 	psmouse->set_resolution = psmouse_set_resolution;
692 	psmouse->poll = psmouse_poll;
693 	psmouse->protocol_handler = psmouse_process_byte;
694 	psmouse->pktsize = 3;
695 	psmouse->reconnect = NULL;
696 	psmouse->disconnect = NULL;
697 	psmouse->cleanup = NULL;
698 	psmouse->pt_activate = NULL;
699 	psmouse->pt_deactivate = NULL;
700 }
701 
702 /*
703  * Apply default settings to the psmouse structure and call specified
704  * protocol detection or initialization routine.
705  */
706 static int psmouse_do_detect(int (*detect)(struct psmouse *psmouse,
707 					   bool set_properties),
708 			     struct psmouse *psmouse, bool set_properties)
709 {
710 	if (set_properties)
711 		psmouse_apply_defaults(psmouse);
712 
713 	return detect(psmouse, set_properties);
714 }
715 
716 /*
717  * psmouse_extensions() probes for any extensions to the basic PS/2 protocol
718  * the mouse may have.
719  */
720 
721 static int psmouse_extensions(struct psmouse *psmouse,
722 			      unsigned int max_proto, bool set_properties)
723 {
724 	bool synaptics_hardware = false;
725 
726 /* Always check for focaltech, this is safe as it uses pnp-id matching */
727 	if (psmouse_do_detect(focaltech_detect, psmouse, set_properties) == 0) {
728 		if (max_proto > PSMOUSE_IMEX) {
729 			if (!set_properties || focaltech_init(psmouse) == 0) {
730 				if (IS_ENABLED(CONFIG_MOUSE_PS2_FOCALTECH))
731 					return PSMOUSE_FOCALTECH;
732 				/*
733 				 * Note that we need to also restrict
734 				 * psmouse_max_proto so that psmouse_initialize()
735 				 * does not try to reset rate and resolution,
736 				 * because even that upsets the device.
737 				 */
738 				psmouse_max_proto = PSMOUSE_PS2;
739 				return PSMOUSE_PS2;
740 			}
741 		}
742 	}
743 
744 /*
745  * We always check for lifebook because it does not disturb mouse
746  * (it only checks DMI information).
747  */
748 	if (psmouse_do_detect(lifebook_detect, psmouse, set_properties) == 0) {
749 		if (max_proto > PSMOUSE_IMEX) {
750 			if (!set_properties || lifebook_init(psmouse) == 0)
751 				return PSMOUSE_LIFEBOOK;
752 		}
753 	}
754 
755 /*
756  * Try Kensington ThinkingMouse (we try first, because synaptics probe
757  * upsets the thinkingmouse).
758  */
759 
760 	if (max_proto > PSMOUSE_IMEX &&
761 	    psmouse_do_detect(thinking_detect, psmouse, set_properties) == 0) {
762 		return PSMOUSE_THINKPS;
763 	}
764 
765 /*
766  * Try Synaptics TouchPad. Note that probing is done even if Synaptics protocol
767  * support is disabled in config - we need to know if it is synaptics so we
768  * can reset it properly after probing for intellimouse.
769  */
770 	if (max_proto > PSMOUSE_PS2 &&
771 	    psmouse_do_detect(synaptics_detect, psmouse, set_properties) == 0) {
772 		synaptics_hardware = true;
773 
774 		if (max_proto > PSMOUSE_IMEX) {
775 /*
776  * Try activating protocol, but check if support is enabled first, since
777  * we try detecting Synaptics even when protocol is disabled.
778  */
779 			if (IS_ENABLED(CONFIG_MOUSE_PS2_SYNAPTICS) &&
780 			    (!set_properties || synaptics_init(psmouse) == 0)) {
781 				return PSMOUSE_SYNAPTICS;
782 			}
783 
784 /*
785  * Some Synaptics touchpads can emulate extended protocols (like IMPS/2).
786  * Unfortunately Logitech/Genius probes confuse some firmware versions so
787  * we'll have to skip them.
788  */
789 			max_proto = PSMOUSE_IMEX;
790 		}
791 /*
792  * Make sure that touchpad is in relative mode, gestures (taps) are enabled
793  */
794 		synaptics_reset(psmouse);
795 	}
796 
797 /*
798  * Try Cypress Trackpad.
799  * Must try it before Finger Sensing Pad because Finger Sensing Pad probe
800  * upsets some modules of Cypress Trackpads.
801  */
802 	if (max_proto > PSMOUSE_IMEX &&
803 			cypress_detect(psmouse, set_properties) == 0) {
804 		if (IS_ENABLED(CONFIG_MOUSE_PS2_CYPRESS)) {
805 			if (cypress_init(psmouse) == 0)
806 				return PSMOUSE_CYPRESS;
807 
808 			/*
809 			 * Finger Sensing Pad probe upsets some modules of
810 			 * Cypress Trackpad, must avoid Finger Sensing Pad
811 			 * probe if Cypress Trackpad device detected.
812 			 */
813 			return PSMOUSE_PS2;
814 		}
815 
816 		max_proto = PSMOUSE_IMEX;
817 	}
818 
819 /*
820  * Try ALPS TouchPad
821  */
822 	if (max_proto > PSMOUSE_IMEX) {
823 		ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
824 		if (psmouse_do_detect(alps_detect,
825 				      psmouse, set_properties) == 0) {
826 			if (!set_properties || alps_init(psmouse) == 0)
827 				return PSMOUSE_ALPS;
828 /*
829  * Init failed, try basic relative protocols
830  */
831 			max_proto = PSMOUSE_IMEX;
832 		}
833 	}
834 
835 /*
836  * Try OLPC HGPK touchpad.
837  */
838 	if (max_proto > PSMOUSE_IMEX &&
839 	    psmouse_do_detect(hgpk_detect, psmouse, set_properties) == 0) {
840 		if (!set_properties || hgpk_init(psmouse) == 0)
841 			return PSMOUSE_HGPK;
842 /*
843  * Init failed, try basic relative protocols
844  */
845 		max_proto = PSMOUSE_IMEX;
846 	}
847 
848 /*
849  * Try Elantech touchpad.
850  */
851 	if (max_proto > PSMOUSE_IMEX &&
852 	    psmouse_do_detect(elantech_detect, psmouse, set_properties) == 0) {
853 		if (!set_properties || elantech_init(psmouse) == 0)
854 			return PSMOUSE_ELANTECH;
855 /*
856  * Init failed, try basic relative protocols
857  */
858 		max_proto = PSMOUSE_IMEX;
859 	}
860 
861 	if (max_proto > PSMOUSE_IMEX) {
862 		if (psmouse_do_detect(genius_detect,
863 				      psmouse, set_properties) == 0)
864 			return PSMOUSE_GENPS;
865 
866 		if (psmouse_do_detect(ps2pp_init,
867 				      psmouse, set_properties) == 0)
868 			return PSMOUSE_PS2PP;
869 
870 		if (psmouse_do_detect(trackpoint_detect,
871 				      psmouse, set_properties) == 0)
872 			return PSMOUSE_TRACKPOINT;
873 
874 		if (psmouse_do_detect(touchkit_ps2_detect,
875 				      psmouse, set_properties) == 0)
876 			return PSMOUSE_TOUCHKIT_PS2;
877 	}
878 
879 /*
880  * Try Finger Sensing Pad. We do it here because its probe upsets
881  * Trackpoint devices (causing TP_READ_ID command to time out).
882  */
883 	if (max_proto > PSMOUSE_IMEX) {
884 		if (psmouse_do_detect(fsp_detect,
885 				      psmouse, set_properties) == 0) {
886 			if (!set_properties || fsp_init(psmouse) == 0)
887 				return PSMOUSE_FSP;
888 /*
889  * Init failed, try basic relative protocols
890  */
891 			max_proto = PSMOUSE_IMEX;
892 		}
893 	}
894 
895 /*
896  * Reset to defaults in case the device got confused by extended
897  * protocol probes. Note that we follow up with full reset because
898  * some mice put themselves to sleep when they see PSMOUSE_RESET_DIS.
899  */
900 	ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
901 	psmouse_reset(psmouse);
902 
903 	if (max_proto >= PSMOUSE_IMEX &&
904 	    psmouse_do_detect(im_explorer_detect,
905 			      psmouse, set_properties) == 0) {
906 		return PSMOUSE_IMEX;
907 	}
908 
909 	if (max_proto >= PSMOUSE_IMPS &&
910 	    psmouse_do_detect(intellimouse_detect,
911 			      psmouse, set_properties) == 0) {
912 		return PSMOUSE_IMPS;
913 	}
914 
915 /*
916  * Okay, all failed, we have a standard mouse here. The number of the buttons
917  * is still a question, though. We assume 3.
918  */
919 	psmouse_do_detect(ps2bare_detect, psmouse, set_properties);
920 
921 	if (synaptics_hardware) {
922 /*
923  * We detected Synaptics hardware but it did not respond to IMPS/2 probes.
924  * We need to reset the touchpad because if there is a track point on the
925  * pass through port it could get disabled while probing for protocol
926  * extensions.
927  */
928 		psmouse_reset(psmouse);
929 	}
930 
931 	return PSMOUSE_PS2;
932 }
933 
934 static const struct psmouse_protocol psmouse_protocols[] = {
935 	{
936 		.type		= PSMOUSE_PS2,
937 		.name		= "PS/2",
938 		.alias		= "bare",
939 		.maxproto	= true,
940 		.ignore_parity	= true,
941 		.detect		= ps2bare_detect,
942 	},
943 #ifdef CONFIG_MOUSE_PS2_LOGIPS2PP
944 	{
945 		.type		= PSMOUSE_PS2PP,
946 		.name		= "PS2++",
947 		.alias		= "logitech",
948 		.detect		= ps2pp_init,
949 	},
950 #endif
951 	{
952 		.type		= PSMOUSE_THINKPS,
953 		.name		= "ThinkPS/2",
954 		.alias		= "thinkps",
955 		.detect		= thinking_detect,
956 	},
957 #ifdef CONFIG_MOUSE_PS2_CYPRESS
958 	{
959 		.type		= PSMOUSE_CYPRESS,
960 		.name		= "CyPS/2",
961 		.alias		= "cypress",
962 		.detect		= cypress_detect,
963 		.init		= cypress_init,
964 	},
965 #endif
966 	{
967 		.type		= PSMOUSE_GENPS,
968 		.name		= "GenPS/2",
969 		.alias		= "genius",
970 		.detect		= genius_detect,
971 	},
972 	{
973 		.type		= PSMOUSE_IMPS,
974 		.name		= "ImPS/2",
975 		.alias		= "imps",
976 		.maxproto	= true,
977 		.ignore_parity	= true,
978 		.detect		= intellimouse_detect,
979 	},
980 	{
981 		.type		= PSMOUSE_IMEX,
982 		.name		= "ImExPS/2",
983 		.alias		= "exps",
984 		.maxproto	= true,
985 		.ignore_parity	= true,
986 		.detect		= im_explorer_detect,
987 	},
988 #ifdef CONFIG_MOUSE_PS2_SYNAPTICS
989 	{
990 		.type		= PSMOUSE_SYNAPTICS,
991 		.name		= "SynPS/2",
992 		.alias		= "synaptics",
993 		.detect		= synaptics_detect,
994 		.init		= synaptics_init,
995 	},
996 	{
997 		.type		= PSMOUSE_SYNAPTICS_RELATIVE,
998 		.name		= "SynRelPS/2",
999 		.alias		= "synaptics-relative",
1000 		.detect		= synaptics_detect,
1001 		.init		= synaptics_init_relative,
1002 	},
1003 #endif
1004 #ifdef CONFIG_MOUSE_PS2_ALPS
1005 	{
1006 		.type		= PSMOUSE_ALPS,
1007 		.name		= "AlpsPS/2",
1008 		.alias		= "alps",
1009 		.detect		= alps_detect,
1010 		.init		= alps_init,
1011 	},
1012 #endif
1013 #ifdef CONFIG_MOUSE_PS2_LIFEBOOK
1014 	{
1015 		.type		= PSMOUSE_LIFEBOOK,
1016 		.name		= "LBPS/2",
1017 		.alias		= "lifebook",
1018 		.init		= lifebook_init,
1019 	},
1020 #endif
1021 #ifdef CONFIG_MOUSE_PS2_TRACKPOINT
1022 	{
1023 		.type		= PSMOUSE_TRACKPOINT,
1024 		.name		= "TPPS/2",
1025 		.alias		= "trackpoint",
1026 		.detect		= trackpoint_detect,
1027 	},
1028 #endif
1029 #ifdef CONFIG_MOUSE_PS2_TOUCHKIT
1030 	{
1031 		.type		= PSMOUSE_TOUCHKIT_PS2,
1032 		.name		= "touchkitPS/2",
1033 		.alias		= "touchkit",
1034 		.detect		= touchkit_ps2_detect,
1035 	},
1036 #endif
1037 #ifdef CONFIG_MOUSE_PS2_OLPC
1038 	{
1039 		.type		= PSMOUSE_HGPK,
1040 		.name		= "OLPC HGPK",
1041 		.alias		= "hgpk",
1042 		.detect		= hgpk_detect,
1043 	},
1044 #endif
1045 #ifdef CONFIG_MOUSE_PS2_ELANTECH
1046 	{
1047 		.type		= PSMOUSE_ELANTECH,
1048 		.name		= "ETPS/2",
1049 		.alias		= "elantech",
1050 		.detect		= elantech_detect,
1051 		.init		= elantech_init,
1052 	},
1053 #endif
1054 #ifdef CONFIG_MOUSE_PS2_SENTELIC
1055 	{
1056 		.type		= PSMOUSE_FSP,
1057 		.name		= "FSPPS/2",
1058 		.alias		= "fsp",
1059 		.detect		= fsp_detect,
1060 		.init		= fsp_init,
1061 	},
1062 #endif
1063 	{
1064 		.type		= PSMOUSE_CORTRON,
1065 		.name		= "CortronPS/2",
1066 		.alias		= "cortps",
1067 		.detect		= cortron_detect,
1068 	},
1069 #ifdef CONFIG_MOUSE_PS2_FOCALTECH
1070 	{
1071 		.type		= PSMOUSE_FOCALTECH,
1072 		.name		= "FocalTechPS/2",
1073 		.alias		= "focaltech",
1074 		.detect		= focaltech_detect,
1075 		.init		= focaltech_init,
1076 	},
1077 #endif
1078 	{
1079 		.type		= PSMOUSE_AUTO,
1080 		.name		= "auto",
1081 		.alias		= "any",
1082 		.maxproto	= true,
1083 	},
1084 };
1085 
1086 static const struct psmouse_protocol *psmouse_protocol_by_type(enum psmouse_type type)
1087 {
1088 	int i;
1089 
1090 	for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++)
1091 		if (psmouse_protocols[i].type == type)
1092 			return &psmouse_protocols[i];
1093 
1094 	WARN_ON(1);
1095 	return &psmouse_protocols[0];
1096 }
1097 
1098 static const struct psmouse_protocol *psmouse_protocol_by_name(const char *name, size_t len)
1099 {
1100 	const struct psmouse_protocol *p;
1101 	int i;
1102 
1103 	for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++) {
1104 		p = &psmouse_protocols[i];
1105 
1106 		if ((strlen(p->name) == len && !strncmp(p->name, name, len)) ||
1107 		    (strlen(p->alias) == len && !strncmp(p->alias, name, len)))
1108 			return &psmouse_protocols[i];
1109 	}
1110 
1111 	return NULL;
1112 }
1113 
1114 
1115 /*
1116  * psmouse_probe() probes for a PS/2 mouse.
1117  */
1118 
1119 static int psmouse_probe(struct psmouse *psmouse)
1120 {
1121 	struct ps2dev *ps2dev = &psmouse->ps2dev;
1122 	unsigned char param[2];
1123 
1124 /*
1125  * First, we check if it's a mouse. It should send 0x00 or 0x03
1126  * in case of an IntelliMouse in 4-byte mode or 0x04 for IM Explorer.
1127  * Sunrex K8561 IR Keyboard/Mouse reports 0xff on second and subsequent
1128  * ID queries, probably due to a firmware bug.
1129  */
1130 
1131 	param[0] = 0xa5;
1132 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
1133 		return -1;
1134 
1135 	if (param[0] != 0x00 && param[0] != 0x03 &&
1136 	    param[0] != 0x04 && param[0] != 0xff)
1137 		return -1;
1138 
1139 /*
1140  * Then we reset and disable the mouse so that it doesn't generate events.
1141  */
1142 
1143 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_DIS))
1144 		psmouse_warn(psmouse, "Failed to reset mouse on %s\n",
1145 			     ps2dev->serio->phys);
1146 
1147 	return 0;
1148 }
1149 
1150 /*
1151  * psmouse_initialize() initializes the mouse to a sane state.
1152  */
1153 
1154 static void psmouse_initialize(struct psmouse *psmouse)
1155 {
1156 /*
1157  * We set the mouse report rate, resolution and scaling.
1158  */
1159 
1160 	if (psmouse_max_proto != PSMOUSE_PS2) {
1161 		psmouse->set_rate(psmouse, psmouse->rate);
1162 		psmouse->set_resolution(psmouse, psmouse->resolution);
1163 		ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
1164 	}
1165 }
1166 
1167 /*
1168  * psmouse_activate() enables the mouse so that we get motion reports from it.
1169  */
1170 
1171 int psmouse_activate(struct psmouse *psmouse)
1172 {
1173 	if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
1174 		psmouse_warn(psmouse, "Failed to enable mouse on %s\n",
1175 			     psmouse->ps2dev.serio->phys);
1176 		return -1;
1177 	}
1178 
1179 	psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
1180 	return 0;
1181 }
1182 
1183 /*
1184  * psmouse_deactivate() puts the mouse into poll mode so that we don't get motion
1185  * reports from it unless we explicitly request it.
1186  */
1187 
1188 int psmouse_deactivate(struct psmouse *psmouse)
1189 {
1190 	if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_DISABLE)) {
1191 		psmouse_warn(psmouse, "Failed to deactivate mouse on %s\n",
1192 			     psmouse->ps2dev.serio->phys);
1193 		return -1;
1194 	}
1195 
1196 	psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1197 	return 0;
1198 }
1199 
1200 
1201 /*
1202  * psmouse_resync() attempts to re-validate current protocol.
1203  */
1204 
1205 static void psmouse_resync(struct work_struct *work)
1206 {
1207 	struct psmouse *parent = NULL, *psmouse =
1208 		container_of(work, struct psmouse, resync_work.work);
1209 	struct serio *serio = psmouse->ps2dev.serio;
1210 	psmouse_ret_t rc = PSMOUSE_GOOD_DATA;
1211 	bool failed = false, enabled = false;
1212 	int i;
1213 
1214 	mutex_lock(&psmouse_mutex);
1215 
1216 	if (psmouse->state != PSMOUSE_RESYNCING)
1217 		goto out;
1218 
1219 	if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1220 		parent = serio_get_drvdata(serio->parent);
1221 		psmouse_deactivate(parent);
1222 	}
1223 
1224 /*
1225  * Some mice don't ACK commands sent while they are in the middle of
1226  * transmitting motion packet. To avoid delay we use ps2_sendbyte()
1227  * instead of ps2_command() which would wait for 200ms for an ACK
1228  * that may never come.
1229  * As an additional quirk ALPS touchpads may not only forget to ACK
1230  * disable command but will stop reporting taps, so if we see that
1231  * mouse at least once ACKs disable we will do full reconnect if ACK
1232  * is missing.
1233  */
1234 	psmouse->num_resyncs++;
1235 
1236 	if (ps2_sendbyte(&psmouse->ps2dev, PSMOUSE_CMD_DISABLE, 20)) {
1237 		if (psmouse->num_resyncs < 3 || psmouse->acks_disable_command)
1238 			failed = true;
1239 	} else
1240 		psmouse->acks_disable_command = true;
1241 
1242 /*
1243  * Poll the mouse. If it was reset the packet will be shorter than
1244  * psmouse->pktsize and ps2_command will fail. We do not expect and
1245  * do not handle scenario when mouse "upgrades" its protocol while
1246  * disconnected since it would require additional delay. If we ever
1247  * see a mouse that does it we'll adjust the code.
1248  */
1249 	if (!failed) {
1250 		if (psmouse->poll(psmouse))
1251 			failed = true;
1252 		else {
1253 			psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1254 			for (i = 0; i < psmouse->pktsize; i++) {
1255 				psmouse->pktcnt++;
1256 				rc = psmouse->protocol_handler(psmouse);
1257 				if (rc != PSMOUSE_GOOD_DATA)
1258 					break;
1259 			}
1260 			if (rc != PSMOUSE_FULL_PACKET)
1261 				failed = true;
1262 			psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
1263 		}
1264 	}
1265 /*
1266  * Now try to enable mouse. We try to do that even if poll failed and also
1267  * repeat our attempts 5 times, otherwise we may be left out with disabled
1268  * mouse.
1269  */
1270 	for (i = 0; i < 5; i++) {
1271 		if (!ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
1272 			enabled = true;
1273 			break;
1274 		}
1275 		msleep(200);
1276 	}
1277 
1278 	if (!enabled) {
1279 		psmouse_warn(psmouse, "failed to re-enable mouse on %s\n",
1280 			     psmouse->ps2dev.serio->phys);
1281 		failed = true;
1282 	}
1283 
1284 	if (failed) {
1285 		psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1286 		psmouse_info(psmouse,
1287 			     "resync failed, issuing reconnect request\n");
1288 		serio_reconnect(serio);
1289 	} else
1290 		psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
1291 
1292 	if (parent)
1293 		psmouse_activate(parent);
1294  out:
1295 	mutex_unlock(&psmouse_mutex);
1296 }
1297 
1298 /*
1299  * psmouse_cleanup() resets the mouse into power-on state.
1300  */
1301 
1302 static void psmouse_cleanup(struct serio *serio)
1303 {
1304 	struct psmouse *psmouse = serio_get_drvdata(serio);
1305 	struct psmouse *parent = NULL;
1306 
1307 	mutex_lock(&psmouse_mutex);
1308 
1309 	if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1310 		parent = serio_get_drvdata(serio->parent);
1311 		psmouse_deactivate(parent);
1312 	}
1313 
1314 	psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1315 
1316 	/*
1317 	 * Disable stream mode so cleanup routine can proceed undisturbed.
1318 	 */
1319 	if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1320 		psmouse_warn(psmouse, "Failed to disable mouse on %s\n",
1321 			     psmouse->ps2dev.serio->phys);
1322 
1323 	if (psmouse->cleanup)
1324 		psmouse->cleanup(psmouse);
1325 
1326 /*
1327  * Reset the mouse to defaults (bare PS/2 protocol).
1328  */
1329 	ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
1330 
1331 /*
1332  * Some boxes, such as HP nx7400, get terribly confused if mouse
1333  * is not fully enabled before suspending/shutting down.
1334  */
1335 	ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE);
1336 
1337 	if (parent) {
1338 		if (parent->pt_deactivate)
1339 			parent->pt_deactivate(parent);
1340 
1341 		psmouse_activate(parent);
1342 	}
1343 
1344 	mutex_unlock(&psmouse_mutex);
1345 }
1346 
1347 /*
1348  * psmouse_disconnect() closes and frees.
1349  */
1350 
1351 static void psmouse_disconnect(struct serio *serio)
1352 {
1353 	struct psmouse *psmouse, *parent = NULL;
1354 
1355 	psmouse = serio_get_drvdata(serio);
1356 
1357 	sysfs_remove_group(&serio->dev.kobj, &psmouse_attribute_group);
1358 
1359 	mutex_lock(&psmouse_mutex);
1360 
1361 	psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1362 
1363 	/* make sure we don't have a resync in progress */
1364 	mutex_unlock(&psmouse_mutex);
1365 	flush_workqueue(kpsmoused_wq);
1366 	mutex_lock(&psmouse_mutex);
1367 
1368 	if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1369 		parent = serio_get_drvdata(serio->parent);
1370 		psmouse_deactivate(parent);
1371 	}
1372 
1373 	if (psmouse->disconnect)
1374 		psmouse->disconnect(psmouse);
1375 
1376 	if (parent && parent->pt_deactivate)
1377 		parent->pt_deactivate(parent);
1378 
1379 	psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1380 
1381 	serio_close(serio);
1382 	serio_set_drvdata(serio, NULL);
1383 	input_unregister_device(psmouse->dev);
1384 	kfree(psmouse);
1385 
1386 	if (parent)
1387 		psmouse_activate(parent);
1388 
1389 	mutex_unlock(&psmouse_mutex);
1390 }
1391 
1392 static int psmouse_switch_protocol(struct psmouse *psmouse,
1393 				   const struct psmouse_protocol *proto)
1394 {
1395 	const struct psmouse_protocol *selected_proto;
1396 	struct input_dev *input_dev = psmouse->dev;
1397 
1398 	input_dev->dev.parent = &psmouse->ps2dev.serio->dev;
1399 
1400 	if (proto && (proto->detect || proto->init)) {
1401 		psmouse_apply_defaults(psmouse);
1402 
1403 		if (proto->detect && proto->detect(psmouse, true) < 0)
1404 			return -1;
1405 
1406 		if (proto->init && proto->init(psmouse) < 0)
1407 			return -1;
1408 
1409 		psmouse->type = proto->type;
1410 		selected_proto = proto;
1411 	} else {
1412 		psmouse->type = psmouse_extensions(psmouse,
1413 						   psmouse_max_proto, true);
1414 		selected_proto = psmouse_protocol_by_type(psmouse->type);
1415 	}
1416 
1417 	psmouse->ignore_parity = selected_proto->ignore_parity;
1418 
1419 	/*
1420 	 * If mouse's packet size is 3 there is no point in polling the
1421 	 * device in hopes to detect protocol reset - we won't get less
1422 	 * than 3 bytes response anyhow.
1423 	 */
1424 	if (psmouse->pktsize == 3)
1425 		psmouse->resync_time = 0;
1426 
1427 	/*
1428 	 * Some smart KVMs fake response to POLL command returning just
1429 	 * 3 bytes and messing up our resync logic, so if initial poll
1430 	 * fails we won't try polling the device anymore. Hopefully
1431 	 * such KVM will maintain initially selected protocol.
1432 	 */
1433 	if (psmouse->resync_time && psmouse->poll(psmouse))
1434 		psmouse->resync_time = 0;
1435 
1436 	snprintf(psmouse->devname, sizeof(psmouse->devname), "%s %s %s",
1437 		 selected_proto->name, psmouse->vendor, psmouse->name);
1438 
1439 	input_dev->name = psmouse->devname;
1440 	input_dev->phys = psmouse->phys;
1441 	input_dev->id.bustype = BUS_I8042;
1442 	input_dev->id.vendor = 0x0002;
1443 	input_dev->id.product = psmouse->type;
1444 	input_dev->id.version = psmouse->model;
1445 
1446 	return 0;
1447 }
1448 
1449 /*
1450  * psmouse_connect() is a callback from the serio module when
1451  * an unhandled serio port is found.
1452  */
1453 static int psmouse_connect(struct serio *serio, struct serio_driver *drv)
1454 {
1455 	struct psmouse *psmouse, *parent = NULL;
1456 	struct input_dev *input_dev;
1457 	int retval = 0, error = -ENOMEM;
1458 
1459 	mutex_lock(&psmouse_mutex);
1460 
1461 	/*
1462 	 * If this is a pass-through port deactivate parent so the device
1463 	 * connected to this port can be successfully identified
1464 	 */
1465 	if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1466 		parent = serio_get_drvdata(serio->parent);
1467 		psmouse_deactivate(parent);
1468 	}
1469 
1470 	psmouse = kzalloc(sizeof(struct psmouse), GFP_KERNEL);
1471 	input_dev = input_allocate_device();
1472 	if (!psmouse || !input_dev)
1473 		goto err_free;
1474 
1475 	ps2_init(&psmouse->ps2dev, serio);
1476 	INIT_DELAYED_WORK(&psmouse->resync_work, psmouse_resync);
1477 	psmouse->dev = input_dev;
1478 	snprintf(psmouse->phys, sizeof(psmouse->phys), "%s/input0", serio->phys);
1479 
1480 	psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1481 
1482 	serio_set_drvdata(serio, psmouse);
1483 
1484 	error = serio_open(serio, drv);
1485 	if (error)
1486 		goto err_clear_drvdata;
1487 
1488 	if (psmouse_probe(psmouse) < 0) {
1489 		error = -ENODEV;
1490 		goto err_close_serio;
1491 	}
1492 
1493 	psmouse->rate = psmouse_rate;
1494 	psmouse->resolution = psmouse_resolution;
1495 	psmouse->resetafter = psmouse_resetafter;
1496 	psmouse->resync_time = parent ? 0 : psmouse_resync_time;
1497 	psmouse->smartscroll = psmouse_smartscroll;
1498 
1499 	psmouse_switch_protocol(psmouse, NULL);
1500 
1501 	psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1502 	psmouse_initialize(psmouse);
1503 
1504 	error = input_register_device(psmouse->dev);
1505 	if (error)
1506 		goto err_protocol_disconnect;
1507 
1508 	if (parent && parent->pt_activate)
1509 		parent->pt_activate(parent);
1510 
1511 	error = sysfs_create_group(&serio->dev.kobj, &psmouse_attribute_group);
1512 	if (error)
1513 		goto err_pt_deactivate;
1514 
1515 	psmouse_activate(psmouse);
1516 
1517  out:
1518 	/* If this is a pass-through port the parent needs to be re-activated */
1519 	if (parent)
1520 		psmouse_activate(parent);
1521 
1522 	mutex_unlock(&psmouse_mutex);
1523 	return retval;
1524 
1525  err_pt_deactivate:
1526 	if (parent && parent->pt_deactivate)
1527 		parent->pt_deactivate(parent);
1528 	input_unregister_device(psmouse->dev);
1529 	input_dev = NULL; /* so we don't try to free it below */
1530  err_protocol_disconnect:
1531 	if (psmouse->disconnect)
1532 		psmouse->disconnect(psmouse);
1533 	psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1534  err_close_serio:
1535 	serio_close(serio);
1536  err_clear_drvdata:
1537 	serio_set_drvdata(serio, NULL);
1538  err_free:
1539 	input_free_device(input_dev);
1540 	kfree(psmouse);
1541 
1542 	retval = error;
1543 	goto out;
1544 }
1545 
1546 
1547 static int psmouse_reconnect(struct serio *serio)
1548 {
1549 	struct psmouse *psmouse = serio_get_drvdata(serio);
1550 	struct psmouse *parent = NULL;
1551 	unsigned char type;
1552 	int rc = -1;
1553 
1554 	mutex_lock(&psmouse_mutex);
1555 
1556 	if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1557 		parent = serio_get_drvdata(serio->parent);
1558 		psmouse_deactivate(parent);
1559 	}
1560 
1561 	psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1562 
1563 	if (psmouse->reconnect) {
1564 		if (psmouse->reconnect(psmouse))
1565 			goto out;
1566 	} else {
1567 		psmouse_reset(psmouse);
1568 
1569 		if (psmouse_probe(psmouse) < 0)
1570 			goto out;
1571 
1572 		type = psmouse_extensions(psmouse, psmouse_max_proto, false);
1573 		if (psmouse->type != type)
1574 			goto out;
1575 	}
1576 
1577 	/*
1578 	 * OK, the device type (and capabilities) match the old one,
1579 	 * we can continue using it, complete initialization
1580 	 */
1581 	psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1582 
1583 	psmouse_initialize(psmouse);
1584 
1585 	if (parent && parent->pt_activate)
1586 		parent->pt_activate(parent);
1587 
1588 	psmouse_activate(psmouse);
1589 	rc = 0;
1590 
1591 out:
1592 	/* If this is a pass-through port the parent waits to be activated */
1593 	if (parent)
1594 		psmouse_activate(parent);
1595 
1596 	mutex_unlock(&psmouse_mutex);
1597 	return rc;
1598 }
1599 
1600 static struct serio_device_id psmouse_serio_ids[] = {
1601 	{
1602 		.type	= SERIO_8042,
1603 		.proto	= SERIO_ANY,
1604 		.id	= SERIO_ANY,
1605 		.extra	= SERIO_ANY,
1606 	},
1607 	{
1608 		.type	= SERIO_PS_PSTHRU,
1609 		.proto	= SERIO_ANY,
1610 		.id	= SERIO_ANY,
1611 		.extra	= SERIO_ANY,
1612 	},
1613 	{ 0 }
1614 };
1615 
1616 MODULE_DEVICE_TABLE(serio, psmouse_serio_ids);
1617 
1618 static struct serio_driver psmouse_drv = {
1619 	.driver		= {
1620 		.name	= "psmouse",
1621 	},
1622 	.description	= DRIVER_DESC,
1623 	.id_table	= psmouse_serio_ids,
1624 	.interrupt	= psmouse_interrupt,
1625 	.connect	= psmouse_connect,
1626 	.reconnect	= psmouse_reconnect,
1627 	.disconnect	= psmouse_disconnect,
1628 	.cleanup	= psmouse_cleanup,
1629 };
1630 
1631 ssize_t psmouse_attr_show_helper(struct device *dev, struct device_attribute *devattr,
1632 				 char *buf)
1633 {
1634 	struct serio *serio = to_serio_port(dev);
1635 	struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1636 	struct psmouse *psmouse;
1637 
1638 	psmouse = serio_get_drvdata(serio);
1639 
1640 	return attr->show(psmouse, attr->data, buf);
1641 }
1642 
1643 ssize_t psmouse_attr_set_helper(struct device *dev, struct device_attribute *devattr,
1644 				const char *buf, size_t count)
1645 {
1646 	struct serio *serio = to_serio_port(dev);
1647 	struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1648 	struct psmouse *psmouse, *parent = NULL;
1649 	int retval;
1650 
1651 	retval = mutex_lock_interruptible(&psmouse_mutex);
1652 	if (retval)
1653 		goto out;
1654 
1655 	psmouse = serio_get_drvdata(serio);
1656 
1657 	if (attr->protect) {
1658 		if (psmouse->state == PSMOUSE_IGNORE) {
1659 			retval = -ENODEV;
1660 			goto out_unlock;
1661 		}
1662 
1663 		if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1664 			parent = serio_get_drvdata(serio->parent);
1665 			psmouse_deactivate(parent);
1666 		}
1667 
1668 		psmouse_deactivate(psmouse);
1669 	}
1670 
1671 	retval = attr->set(psmouse, attr->data, buf, count);
1672 
1673 	if (attr->protect) {
1674 		if (retval != -ENODEV)
1675 			psmouse_activate(psmouse);
1676 
1677 		if (parent)
1678 			psmouse_activate(parent);
1679 	}
1680 
1681  out_unlock:
1682 	mutex_unlock(&psmouse_mutex);
1683  out:
1684 	return retval;
1685 }
1686 
1687 static ssize_t psmouse_show_int_attr(struct psmouse *psmouse, void *offset, char *buf)
1688 {
1689 	unsigned int *field = (unsigned int *)((char *)psmouse + (size_t)offset);
1690 
1691 	return sprintf(buf, "%u\n", *field);
1692 }
1693 
1694 static ssize_t psmouse_set_int_attr(struct psmouse *psmouse, void *offset, const char *buf, size_t count)
1695 {
1696 	unsigned int *field = (unsigned int *)((char *)psmouse + (size_t)offset);
1697 	unsigned int value;
1698 	int err;
1699 
1700 	err = kstrtouint(buf, 10, &value);
1701 	if (err)
1702 		return err;
1703 
1704 	*field = value;
1705 
1706 	return count;
1707 }
1708 
1709 static ssize_t psmouse_attr_show_protocol(struct psmouse *psmouse, void *data, char *buf)
1710 {
1711 	return sprintf(buf, "%s\n", psmouse_protocol_by_type(psmouse->type)->name);
1712 }
1713 
1714 static ssize_t psmouse_attr_set_protocol(struct psmouse *psmouse, void *data, const char *buf, size_t count)
1715 {
1716 	struct serio *serio = psmouse->ps2dev.serio;
1717 	struct psmouse *parent = NULL;
1718 	struct input_dev *old_dev, *new_dev;
1719 	const struct psmouse_protocol *proto, *old_proto;
1720 	int error;
1721 	int retry = 0;
1722 
1723 	proto = psmouse_protocol_by_name(buf, count);
1724 	if (!proto)
1725 		return -EINVAL;
1726 
1727 	if (psmouse->type == proto->type)
1728 		return count;
1729 
1730 	new_dev = input_allocate_device();
1731 	if (!new_dev)
1732 		return -ENOMEM;
1733 
1734 	while (!list_empty(&serio->children)) {
1735 		if (++retry > 3) {
1736 			psmouse_warn(psmouse,
1737 				     "failed to destroy children ports, protocol change aborted.\n");
1738 			input_free_device(new_dev);
1739 			return -EIO;
1740 		}
1741 
1742 		mutex_unlock(&psmouse_mutex);
1743 		serio_unregister_child_port(serio);
1744 		mutex_lock(&psmouse_mutex);
1745 
1746 		if (serio->drv != &psmouse_drv) {
1747 			input_free_device(new_dev);
1748 			return -ENODEV;
1749 		}
1750 
1751 		if (psmouse->type == proto->type) {
1752 			input_free_device(new_dev);
1753 			return count; /* switched by other thread */
1754 		}
1755 	}
1756 
1757 	if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1758 		parent = serio_get_drvdata(serio->parent);
1759 		if (parent->pt_deactivate)
1760 			parent->pt_deactivate(parent);
1761 	}
1762 
1763 	old_dev = psmouse->dev;
1764 	old_proto = psmouse_protocol_by_type(psmouse->type);
1765 
1766 	if (psmouse->disconnect)
1767 		psmouse->disconnect(psmouse);
1768 
1769 	psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1770 
1771 	psmouse->dev = new_dev;
1772 	psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1773 
1774 	if (psmouse_switch_protocol(psmouse, proto) < 0) {
1775 		psmouse_reset(psmouse);
1776 		/* default to PSMOUSE_PS2 */
1777 		psmouse_switch_protocol(psmouse, &psmouse_protocols[0]);
1778 	}
1779 
1780 	psmouse_initialize(psmouse);
1781 	psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1782 
1783 	error = input_register_device(psmouse->dev);
1784 	if (error) {
1785 		if (psmouse->disconnect)
1786 			psmouse->disconnect(psmouse);
1787 
1788 		psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1789 		input_free_device(new_dev);
1790 		psmouse->dev = old_dev;
1791 		psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1792 		psmouse_switch_protocol(psmouse, old_proto);
1793 		psmouse_initialize(psmouse);
1794 		psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1795 
1796 		return error;
1797 	}
1798 
1799 	input_unregister_device(old_dev);
1800 
1801 	if (parent && parent->pt_activate)
1802 		parent->pt_activate(parent);
1803 
1804 	return count;
1805 }
1806 
1807 static ssize_t psmouse_attr_set_rate(struct psmouse *psmouse, void *data, const char *buf, size_t count)
1808 {
1809 	unsigned int value;
1810 	int err;
1811 
1812 	err = kstrtouint(buf, 10, &value);
1813 	if (err)
1814 		return err;
1815 
1816 	psmouse->set_rate(psmouse, value);
1817 	return count;
1818 }
1819 
1820 static ssize_t psmouse_attr_set_resolution(struct psmouse *psmouse, void *data, const char *buf, size_t count)
1821 {
1822 	unsigned int value;
1823 	int err;
1824 
1825 	err = kstrtouint(buf, 10, &value);
1826 	if (err)
1827 		return err;
1828 
1829 	psmouse->set_resolution(psmouse, value);
1830 	return count;
1831 }
1832 
1833 
1834 static int psmouse_set_maxproto(const char *val, const struct kernel_param *kp)
1835 {
1836 	const struct psmouse_protocol *proto;
1837 
1838 	if (!val)
1839 		return -EINVAL;
1840 
1841 	proto = psmouse_protocol_by_name(val, strlen(val));
1842 
1843 	if (!proto || !proto->maxproto)
1844 		return -EINVAL;
1845 
1846 	*((unsigned int *)kp->arg) = proto->type;
1847 
1848 	return 0;
1849 }
1850 
1851 static int psmouse_get_maxproto(char *buffer, const struct kernel_param *kp)
1852 {
1853 	int type = *((unsigned int *)kp->arg);
1854 
1855 	return sprintf(buffer, "%s", psmouse_protocol_by_type(type)->name);
1856 }
1857 
1858 static int __init psmouse_init(void)
1859 {
1860 	int err;
1861 
1862 	lifebook_module_init();
1863 	synaptics_module_init();
1864 	hgpk_module_init();
1865 
1866 	kpsmoused_wq = create_singlethread_workqueue("kpsmoused");
1867 	if (!kpsmoused_wq) {
1868 		pr_err("failed to create kpsmoused workqueue\n");
1869 		return -ENOMEM;
1870 	}
1871 
1872 	err = serio_register_driver(&psmouse_drv);
1873 	if (err)
1874 		destroy_workqueue(kpsmoused_wq);
1875 
1876 	return err;
1877 }
1878 
1879 static void __exit psmouse_exit(void)
1880 {
1881 	serio_unregister_driver(&psmouse_drv);
1882 	destroy_workqueue(kpsmoused_wq);
1883 }
1884 
1885 module_init(psmouse_init);
1886 module_exit(psmouse_exit);
1887