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