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