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