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