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