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