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