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