1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  i8042 keyboard and mouse controller driver for Linux
4  *
5  *  Copyright (c) 1999-2004 Vojtech Pavlik
6  */
7 
8 
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 
11 #include <linux/types.h>
12 #include <linux/delay.h>
13 #include <linux/module.h>
14 #include <linux/interrupt.h>
15 #include <linux/ioport.h>
16 #include <linux/init.h>
17 #include <linux/serio.h>
18 #include <linux/err.h>
19 #include <linux/rcupdate.h>
20 #include <linux/platform_device.h>
21 #include <linux/i8042.h>
22 #include <linux/slab.h>
23 #include <linux/suspend.h>
24 #include <linux/property.h>
25 
26 #include <asm/io.h>
27 
28 MODULE_AUTHOR("Vojtech Pavlik <[email protected]>");
29 MODULE_DESCRIPTION("i8042 keyboard and mouse controller driver");
30 MODULE_LICENSE("GPL");
31 
32 static bool i8042_nokbd;
33 module_param_named(nokbd, i8042_nokbd, bool, 0);
34 MODULE_PARM_DESC(nokbd, "Do not probe or use KBD port.");
35 
36 static bool i8042_noaux;
37 module_param_named(noaux, i8042_noaux, bool, 0);
38 MODULE_PARM_DESC(noaux, "Do not probe or use AUX (mouse) port.");
39 
40 static bool i8042_nomux;
41 module_param_named(nomux, i8042_nomux, bool, 0);
42 MODULE_PARM_DESC(nomux, "Do not check whether an active multiplexing controller is present.");
43 
44 static bool i8042_unlock;
45 module_param_named(unlock, i8042_unlock, bool, 0);
46 MODULE_PARM_DESC(unlock, "Ignore keyboard lock.");
47 
48 static bool i8042_probe_defer;
49 module_param_named(probe_defer, i8042_probe_defer, bool, 0);
50 MODULE_PARM_DESC(probe_defer, "Allow deferred probing.");
51 
52 enum i8042_controller_reset_mode {
53 	I8042_RESET_NEVER,
54 	I8042_RESET_ALWAYS,
55 	I8042_RESET_ON_S2RAM,
56 #define I8042_RESET_DEFAULT	I8042_RESET_ON_S2RAM
57 };
58 static enum i8042_controller_reset_mode i8042_reset = I8042_RESET_DEFAULT;
i8042_set_reset(const char * val,const struct kernel_param * kp)59 static int i8042_set_reset(const char *val, const struct kernel_param *kp)
60 {
61 	enum i8042_controller_reset_mode *arg = kp->arg;
62 	int error;
63 	bool reset;
64 
65 	if (val) {
66 		error = kstrtobool(val, &reset);
67 		if (error)
68 			return error;
69 	} else {
70 		reset = true;
71 	}
72 
73 	*arg = reset ? I8042_RESET_ALWAYS : I8042_RESET_NEVER;
74 	return 0;
75 }
76 
77 static const struct kernel_param_ops param_ops_reset_param = {
78 	.flags = KERNEL_PARAM_OPS_FL_NOARG,
79 	.set = i8042_set_reset,
80 };
81 #define param_check_reset_param(name, p)	\
82 	__param_check(name, p, enum i8042_controller_reset_mode)
83 module_param_named(reset, i8042_reset, reset_param, 0);
84 MODULE_PARM_DESC(reset, "Reset controller on resume, cleanup or both");
85 
86 static bool i8042_direct;
87 module_param_named(direct, i8042_direct, bool, 0);
88 MODULE_PARM_DESC(direct, "Put keyboard port into non-translated mode.");
89 
90 static bool i8042_dumbkbd;
91 module_param_named(dumbkbd, i8042_dumbkbd, bool, 0);
92 MODULE_PARM_DESC(dumbkbd, "Pretend that controller can only read data from keyboard");
93 
94 static bool i8042_noloop;
95 module_param_named(noloop, i8042_noloop, bool, 0);
96 MODULE_PARM_DESC(noloop, "Disable the AUX Loopback command while probing for the AUX port");
97 
98 static bool i8042_notimeout;
99 module_param_named(notimeout, i8042_notimeout, bool, 0);
100 MODULE_PARM_DESC(notimeout, "Ignore timeouts signalled by i8042");
101 
102 static bool i8042_kbdreset;
103 module_param_named(kbdreset, i8042_kbdreset, bool, 0);
104 MODULE_PARM_DESC(kbdreset, "Reset device connected to KBD port");
105 
106 #ifdef CONFIG_X86
107 static bool i8042_dritek;
108 module_param_named(dritek, i8042_dritek, bool, 0);
109 MODULE_PARM_DESC(dritek, "Force enable the Dritek keyboard extension");
110 #endif
111 
112 #ifdef CONFIG_PNP
113 static bool i8042_nopnp;
114 module_param_named(nopnp, i8042_nopnp, bool, 0);
115 MODULE_PARM_DESC(nopnp, "Do not use PNP to detect controller settings");
116 #endif
117 
118 static bool i8042_forcenorestore;
119 module_param_named(forcenorestore, i8042_forcenorestore, bool, 0);
120 MODULE_PARM_DESC(forcenorestore, "Force no restore on s3 resume, copying s2idle behaviour");
121 
122 #define DEBUG
123 #ifdef DEBUG
124 static bool i8042_debug;
125 module_param_named(debug, i8042_debug, bool, 0600);
126 MODULE_PARM_DESC(debug, "Turn i8042 debugging mode on and off");
127 
128 static bool i8042_unmask_kbd_data;
129 module_param_named(unmask_kbd_data, i8042_unmask_kbd_data, bool, 0600);
130 MODULE_PARM_DESC(unmask_kbd_data, "Unconditional enable (may reveal sensitive data) of normally sanitize-filtered kbd data traffic debug log [pre-condition: i8042.debug=1 enabled]");
131 #endif
132 
133 static bool i8042_present;
134 static bool i8042_bypass_aux_irq_test;
135 static char i8042_kbd_firmware_id[128];
136 static char i8042_aux_firmware_id[128];
137 static struct fwnode_handle *i8042_kbd_fwnode;
138 
139 #include "i8042.h"
140 
141 /*
142  * i8042_lock protects serialization between i8042_command and
143  * the interrupt handler.
144  */
145 static DEFINE_SPINLOCK(i8042_lock);
146 
147 /*
148  * Writers to AUX and KBD ports as well as users issuing i8042_command
149  * directly should acquire i8042_mutex (by means of calling
150  * i8042_lock_chip() and i8042_unlock_chip() helpers) to ensure that
151  * they do not disturb each other (unfortunately in many i8042
152  * implementations write to one of the ports will immediately abort
153  * command that is being processed by another port).
154  */
155 static DEFINE_MUTEX(i8042_mutex);
156 
157 struct i8042_port {
158 	struct serio *serio;
159 	int irq;
160 	bool exists;
161 	bool driver_bound;
162 	signed char mux;
163 };
164 
165 #define I8042_KBD_PORT_NO	0
166 #define I8042_AUX_PORT_NO	1
167 #define I8042_MUX_PORT_NO	2
168 #define I8042_NUM_PORTS		(I8042_NUM_MUX_PORTS + 2)
169 
170 static struct i8042_port i8042_ports[I8042_NUM_PORTS];
171 
172 static unsigned char i8042_initial_ctr;
173 static unsigned char i8042_ctr;
174 static bool i8042_mux_present;
175 static bool i8042_kbd_irq_registered;
176 static bool i8042_aux_irq_registered;
177 static unsigned char i8042_suppress_kbd_ack;
178 static struct platform_device *i8042_platform_device;
179 static struct notifier_block i8042_kbd_bind_notifier_block;
180 
181 static bool i8042_handle_data(int irq);
182 static i8042_filter_t i8042_platform_filter;
183 static void *i8042_platform_filter_context;
184 
i8042_lock_chip(void)185 void i8042_lock_chip(void)
186 {
187 	mutex_lock(&i8042_mutex);
188 }
189 EXPORT_SYMBOL(i8042_lock_chip);
190 
i8042_unlock_chip(void)191 void i8042_unlock_chip(void)
192 {
193 	mutex_unlock(&i8042_mutex);
194 }
195 EXPORT_SYMBOL(i8042_unlock_chip);
196 
i8042_install_filter(i8042_filter_t filter,void * context)197 int i8042_install_filter(i8042_filter_t filter, void *context)
198 {
199 	guard(spinlock_irqsave)(&i8042_lock);
200 
201 	if (i8042_platform_filter)
202 		return -EBUSY;
203 
204 	i8042_platform_filter = filter;
205 	i8042_platform_filter_context = context;
206 	return 0;
207 }
208 EXPORT_SYMBOL(i8042_install_filter);
209 
i8042_remove_filter(i8042_filter_t filter)210 int i8042_remove_filter(i8042_filter_t filter)
211 {
212 	guard(spinlock_irqsave)(&i8042_lock);
213 
214 	if (i8042_platform_filter != filter)
215 		return -EINVAL;
216 
217 	i8042_platform_filter = NULL;
218 	i8042_platform_filter_context = NULL;
219 	return 0;
220 }
221 EXPORT_SYMBOL(i8042_remove_filter);
222 
223 /*
224  * The i8042_wait_read() and i8042_wait_write functions wait for the i8042 to
225  * be ready for reading values from it / writing values to it.
226  * Called always with i8042_lock held.
227  */
228 
i8042_wait_read(void)229 static int i8042_wait_read(void)
230 {
231 	int i = 0;
232 
233 	while ((~i8042_read_status() & I8042_STR_OBF) && (i < I8042_CTL_TIMEOUT)) {
234 		udelay(50);
235 		i++;
236 	}
237 	return -(i == I8042_CTL_TIMEOUT);
238 }
239 
i8042_wait_write(void)240 static int i8042_wait_write(void)
241 {
242 	int i = 0;
243 
244 	while ((i8042_read_status() & I8042_STR_IBF) && (i < I8042_CTL_TIMEOUT)) {
245 		udelay(50);
246 		i++;
247 	}
248 	return -(i == I8042_CTL_TIMEOUT);
249 }
250 
251 /*
252  * i8042_flush() flushes all data that may be in the keyboard and mouse buffers
253  * of the i8042 down the toilet.
254  */
255 
i8042_flush(void)256 static int i8042_flush(void)
257 {
258 	unsigned char data, str;
259 	int count = 0;
260 
261 	guard(spinlock_irqsave)(&i8042_lock);
262 
263 	while ((str = i8042_read_status()) & I8042_STR_OBF) {
264 		if (count++ >= I8042_BUFFER_SIZE)
265 			return -EIO;
266 
267 		udelay(50);
268 		data = i8042_read_data();
269 		dbg("%02x <- i8042 (flush, %s)\n",
270 		    data, str & I8042_STR_AUXDATA ? "aux" : "kbd");
271 	}
272 
273 	return 0;
274 }
275 
276 /*
277  * i8042_command() executes a command on the i8042. It also sends the input
278  * parameter(s) of the commands to it, and receives the output value(s). The
279  * parameters are to be stored in the param array, and the output is placed
280  * into the same array. The number of the parameters and output values is
281  * encoded in bits 8-11 of the command number.
282  */
283 
__i8042_command(unsigned char * param,int command)284 static int __i8042_command(unsigned char *param, int command)
285 {
286 	int i, error;
287 
288 	if (i8042_noloop && command == I8042_CMD_AUX_LOOP)
289 		return -1;
290 
291 	error = i8042_wait_write();
292 	if (error)
293 		return error;
294 
295 	dbg("%02x -> i8042 (command)\n", command & 0xff);
296 	i8042_write_command(command & 0xff);
297 
298 	for (i = 0; i < ((command >> 12) & 0xf); i++) {
299 		error = i8042_wait_write();
300 		if (error) {
301 			dbg("     -- i8042 (wait write timeout)\n");
302 			return error;
303 		}
304 		dbg("%02x -> i8042 (parameter)\n", param[i]);
305 		i8042_write_data(param[i]);
306 	}
307 
308 	for (i = 0; i < ((command >> 8) & 0xf); i++) {
309 		error = i8042_wait_read();
310 		if (error) {
311 			dbg("     -- i8042 (wait read timeout)\n");
312 			return error;
313 		}
314 
315 		if (command == I8042_CMD_AUX_LOOP &&
316 		    !(i8042_read_status() & I8042_STR_AUXDATA)) {
317 			dbg("     -- i8042 (auxerr)\n");
318 			return -1;
319 		}
320 
321 		param[i] = i8042_read_data();
322 		dbg("%02x <- i8042 (return)\n", param[i]);
323 	}
324 
325 	return 0;
326 }
327 
i8042_command(unsigned char * param,int command)328 int i8042_command(unsigned char *param, int command)
329 {
330 	if (!i8042_present)
331 		return -1;
332 
333 	guard(spinlock_irqsave)(&i8042_lock);
334 
335 	return __i8042_command(param, command);
336 }
337 EXPORT_SYMBOL(i8042_command);
338 
339 /*
340  * i8042_kbd_write() sends a byte out through the keyboard interface.
341  */
342 
i8042_kbd_write(struct serio * port,unsigned char c)343 static int i8042_kbd_write(struct serio *port, unsigned char c)
344 {
345 	int error;
346 
347 	guard(spinlock_irqsave)(&i8042_lock);
348 
349 	error = i8042_wait_write();
350 	if (error)
351 		return error;
352 
353 	dbg("%02x -> i8042 (kbd-data)\n", c);
354 	i8042_write_data(c);
355 
356 	return 0;
357 }
358 
359 /*
360  * i8042_aux_write() sends a byte out through the aux interface.
361  */
362 
i8042_aux_write(struct serio * serio,unsigned char c)363 static int i8042_aux_write(struct serio *serio, unsigned char c)
364 {
365 	struct i8042_port *port = serio->port_data;
366 
367 	return i8042_command(&c, port->mux == -1 ?
368 					I8042_CMD_AUX_SEND :
369 					I8042_CMD_MUX_SEND + port->mux);
370 }
371 
372 
373 /*
374  * i8042_port_close attempts to clear AUX or KBD port state by disabling
375  * and then re-enabling it.
376  */
377 
i8042_port_close(struct serio * serio)378 static void i8042_port_close(struct serio *serio)
379 {
380 	int irq_bit;
381 	int disable_bit;
382 	const char *port_name;
383 
384 	if (serio == i8042_ports[I8042_AUX_PORT_NO].serio) {
385 		irq_bit = I8042_CTR_AUXINT;
386 		disable_bit = I8042_CTR_AUXDIS;
387 		port_name = "AUX";
388 	} else {
389 		irq_bit = I8042_CTR_KBDINT;
390 		disable_bit = I8042_CTR_KBDDIS;
391 		port_name = "KBD";
392 	}
393 
394 	i8042_ctr &= ~irq_bit;
395 	if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR))
396 		pr_warn("Can't write CTR while closing %s port\n", port_name);
397 
398 	udelay(50);
399 
400 	i8042_ctr &= ~disable_bit;
401 	i8042_ctr |= irq_bit;
402 	if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR))
403 		pr_err("Can't reactivate %s port\n", port_name);
404 
405 	/*
406 	 * See if there is any data appeared while we were messing with
407 	 * port state.
408 	 */
409 	i8042_handle_data(0);
410 }
411 
412 /*
413  * i8042_start() is called by serio core when port is about to finish
414  * registering. It will mark port as existing so i8042_interrupt can
415  * start sending data through it.
416  */
i8042_start(struct serio * serio)417 static int i8042_start(struct serio *serio)
418 {
419 	struct i8042_port *port = serio->port_data;
420 
421 	device_set_wakeup_capable(&serio->dev, true);
422 
423 	/*
424 	 * On platforms using suspend-to-idle, allow the keyboard to
425 	 * wake up the system from sleep by enabling keyboard wakeups
426 	 * by default.  This is consistent with keyboard wakeup
427 	 * behavior on many platforms using suspend-to-RAM (ACPI S3)
428 	 * by default.
429 	 */
430 	if (pm_suspend_default_s2idle() &&
431 	    serio == i8042_ports[I8042_KBD_PORT_NO].serio) {
432 		device_set_wakeup_enable(&serio->dev, true);
433 	}
434 
435 	guard(spinlock_irq)(&i8042_lock);
436 	port->exists = true;
437 
438 	return 0;
439 }
440 
441 /*
442  * i8042_stop() marks serio port as non-existing so i8042_interrupt
443  * will not try to send data to the port that is about to go away.
444  * The function is called by serio core as part of unregister procedure.
445  */
i8042_stop(struct serio * serio)446 static void i8042_stop(struct serio *serio)
447 {
448 	struct i8042_port *port = serio->port_data;
449 
450 	scoped_guard(spinlock_irq, &i8042_lock) {
451 		port->exists = false;
452 		port->serio = NULL;
453 	}
454 
455 	/*
456 	 * We need to make sure that interrupt handler finishes using
457 	 * our serio port before we return from this function.
458 	 * We synchronize with both AUX and KBD IRQs because there is
459 	 * a (very unlikely) chance that AUX IRQ is raised for KBD port
460 	 * and vice versa.
461 	 */
462 	synchronize_irq(I8042_AUX_IRQ);
463 	synchronize_irq(I8042_KBD_IRQ);
464 }
465 
466 /*
467  * i8042_filter() filters out unwanted bytes from the input data stream.
468  * It is called from i8042_interrupt and thus is running with interrupts
469  * off and i8042_lock held.
470  */
i8042_filter(unsigned char data,unsigned char str,struct serio * serio)471 static bool i8042_filter(unsigned char data, unsigned char str,
472 			 struct serio *serio)
473 {
474 	if (unlikely(i8042_suppress_kbd_ack)) {
475 		if ((~str & I8042_STR_AUXDATA) &&
476 		    (data == 0xfa || data == 0xfe)) {
477 			i8042_suppress_kbd_ack--;
478 			dbg("Extra keyboard ACK - filtered out\n");
479 			return true;
480 		}
481 	}
482 
483 	if (!i8042_platform_filter)
484 		return false;
485 
486 	if (i8042_platform_filter(data, str, serio, i8042_platform_filter_context)) {
487 		dbg("Filtered out by platform filter\n");
488 		return true;
489 	}
490 
491 	return false;
492 }
493 
494 /*
495  * i8042_handle_mux() handles case when data is coming from one of
496  * the multiplexed ports. It would be simple if not for quirks with
497  * handling errors:
498  *
499  * When MUXERR condition is signalled the data register can only contain
500  * 0xfd, 0xfe or 0xff if implementation follows the spec. Unfortunately
501  * it is not always the case. Some KBCs also report 0xfc when there is
502  * nothing connected to the port while others sometimes get confused which
503  * port the data came from and signal error leaving the data intact. They
504  * _do not_ revert to legacy mode (actually I've never seen KBC reverting
505  * to legacy mode yet, when we see one we'll add proper handling).
506  * Anyway, we process 0xfc, 0xfd, 0xfe and 0xff as timeouts, and for the
507  * rest assume that the data came from the same serio last byte
508  * was transmitted (if transmission happened not too long ago).
509  */
i8042_handle_mux(u8 str,u8 * data,unsigned int * dfl)510 static int i8042_handle_mux(u8 str, u8 *data, unsigned int *dfl)
511 {
512 	static unsigned long last_transmit;
513 	static unsigned long last_port;
514 	unsigned int mux_port;
515 
516 	mux_port = (str >> 6) & 3;
517 	*dfl = 0;
518 
519 	if (str & I8042_STR_MUXERR) {
520 		dbg("MUX error, status is %02x, data is %02x\n",
521 		    str, *data);
522 
523 		switch (*data) {
524 		default:
525 			if (time_before(jiffies, last_transmit + HZ/10)) {
526 				mux_port = last_port;
527 				break;
528 			}
529 			fallthrough;	/* report timeout */
530 		case 0xfc:
531 		case 0xfd:
532 		case 0xfe:
533 			*dfl = SERIO_TIMEOUT;
534 			*data = 0xfe;
535 			break;
536 		case 0xff:
537 			*dfl = SERIO_PARITY;
538 			*data = 0xfe;
539 			break;
540 		}
541 	}
542 
543 	last_port = mux_port;
544 	last_transmit = jiffies;
545 
546 	return I8042_MUX_PORT_NO + mux_port;
547 }
548 
549 /*
550  * i8042_handle_data() is the most important function in this driver -
551  * it reads the data from the i8042, determines its destination serio
552  * port, and sends received byte to the upper layers.
553  *
554  * Returns true if there was data waiting, false otherwise.
555  */
i8042_handle_data(int irq)556 static bool i8042_handle_data(int irq)
557 {
558 	struct i8042_port *port;
559 	struct serio *serio;
560 	unsigned char str, data;
561 	unsigned int dfl;
562 	unsigned int port_no;
563 	bool filtered;
564 
565 	scoped_guard(spinlock_irqsave, &i8042_lock) {
566 		str = i8042_read_status();
567 		if (unlikely(~str & I8042_STR_OBF))
568 			return false;
569 
570 		data = i8042_read_data();
571 
572 		if (i8042_mux_present && (str & I8042_STR_AUXDATA)) {
573 			port_no = i8042_handle_mux(str, &data, &dfl);
574 		} else {
575 
576 			dfl = (str & I8042_STR_PARITY) ? SERIO_PARITY : 0;
577 			if ((str & I8042_STR_TIMEOUT) && !i8042_notimeout)
578 				dfl |= SERIO_TIMEOUT;
579 
580 			port_no = (str & I8042_STR_AUXDATA) ?
581 					I8042_AUX_PORT_NO : I8042_KBD_PORT_NO;
582 		}
583 
584 		port = &i8042_ports[port_no];
585 		serio = port->exists ? port->serio : NULL;
586 
587 		filter_dbg(port->driver_bound,
588 			   data, "<- i8042 (interrupt, %d, %d%s%s)\n",
589 			   port_no, irq,
590 			   dfl & SERIO_PARITY ? ", bad parity" : "",
591 			   dfl & SERIO_TIMEOUT ? ", timeout" : "");
592 
593 		filtered = i8042_filter(data, str, serio);
594 	}
595 
596 	if (likely(serio && !filtered))
597 		serio_interrupt(serio, data, dfl);
598 
599 	return true;
600 }
601 
i8042_interrupt(int irq,void * dev_id)602 static irqreturn_t i8042_interrupt(int irq, void *dev_id)
603 {
604 	if (unlikely(!i8042_handle_data(irq))) {
605 		dbg("Interrupt %d, without any data\n", irq);
606 		return IRQ_NONE;
607 	}
608 
609 	return IRQ_HANDLED;
610 }
611 
612 /*
613  * i8042_enable_kbd_port enables keyboard port on chip
614  */
615 
i8042_enable_kbd_port(void)616 static int i8042_enable_kbd_port(void)
617 {
618 	i8042_ctr &= ~I8042_CTR_KBDDIS;
619 	i8042_ctr |= I8042_CTR_KBDINT;
620 
621 	if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
622 		i8042_ctr &= ~I8042_CTR_KBDINT;
623 		i8042_ctr |= I8042_CTR_KBDDIS;
624 		pr_err("Failed to enable KBD port\n");
625 		return -EIO;
626 	}
627 
628 	return 0;
629 }
630 
631 /*
632  * i8042_enable_aux_port enables AUX (mouse) port on chip
633  */
634 
i8042_enable_aux_port(void)635 static int i8042_enable_aux_port(void)
636 {
637 	i8042_ctr &= ~I8042_CTR_AUXDIS;
638 	i8042_ctr |= I8042_CTR_AUXINT;
639 
640 	if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
641 		i8042_ctr &= ~I8042_CTR_AUXINT;
642 		i8042_ctr |= I8042_CTR_AUXDIS;
643 		pr_err("Failed to enable AUX port\n");
644 		return -EIO;
645 	}
646 
647 	return 0;
648 }
649 
650 /*
651  * i8042_enable_mux_ports enables 4 individual AUX ports after
652  * the controller has been switched into Multiplexed mode
653  */
654 
i8042_enable_mux_ports(void)655 static int i8042_enable_mux_ports(void)
656 {
657 	unsigned char param;
658 	int i;
659 
660 	for (i = 0; i < I8042_NUM_MUX_PORTS; i++) {
661 		i8042_command(&param, I8042_CMD_MUX_PFX + i);
662 		i8042_command(&param, I8042_CMD_AUX_ENABLE);
663 	}
664 
665 	return i8042_enable_aux_port();
666 }
667 
668 /*
669  * i8042_set_mux_mode checks whether the controller has an
670  * active multiplexor and puts the chip into Multiplexed (true)
671  * or Legacy (false) mode.
672  */
673 
i8042_set_mux_mode(bool multiplex,unsigned char * mux_version)674 static int i8042_set_mux_mode(bool multiplex, unsigned char *mux_version)
675 {
676 
677 	unsigned char param, val;
678 /*
679  * Get rid of bytes in the queue.
680  */
681 
682 	i8042_flush();
683 
684 /*
685  * Internal loopback test - send three bytes, they should come back from the
686  * mouse interface, the last should be version.
687  */
688 
689 	param = val = 0xf0;
690 	if (i8042_command(&param, I8042_CMD_AUX_LOOP) || param != val)
691 		return -1;
692 	param = val = multiplex ? 0x56 : 0xf6;
693 	if (i8042_command(&param, I8042_CMD_AUX_LOOP) || param != val)
694 		return -1;
695 	param = val = multiplex ? 0xa4 : 0xa5;
696 	if (i8042_command(&param, I8042_CMD_AUX_LOOP) || param == val)
697 		return -1;
698 
699 /*
700  * Workaround for interference with USB Legacy emulation
701  * that causes a v10.12 MUX to be found.
702  */
703 	if (param == 0xac)
704 		return -1;
705 
706 	if (mux_version)
707 		*mux_version = param;
708 
709 	return 0;
710 }
711 
712 /*
713  * i8042_check_mux() checks whether the controller supports the PS/2 Active
714  * Multiplexing specification by Synaptics, Phoenix, Insyde and
715  * LCS/Telegraphics.
716  */
717 
i8042_check_mux(void)718 static int i8042_check_mux(void)
719 {
720 	unsigned char mux_version;
721 
722 	if (i8042_set_mux_mode(true, &mux_version))
723 		return -1;
724 
725 	pr_info("Detected active multiplexing controller, rev %d.%d\n",
726 		(mux_version >> 4) & 0xf, mux_version & 0xf);
727 
728 /*
729  * Disable all muxed ports by disabling AUX.
730  */
731 	i8042_ctr |= I8042_CTR_AUXDIS;
732 	i8042_ctr &= ~I8042_CTR_AUXINT;
733 
734 	if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
735 		pr_err("Failed to disable AUX port, can't use MUX\n");
736 		return -EIO;
737 	}
738 
739 	i8042_mux_present = true;
740 
741 	return 0;
742 }
743 
744 /*
745  * The following is used to test AUX IRQ delivery.
746  */
747 static struct completion i8042_aux_irq_delivered;
748 static bool i8042_irq_being_tested;
749 
i8042_aux_test_irq(int irq,void * dev_id)750 static irqreturn_t i8042_aux_test_irq(int irq, void *dev_id)
751 {
752 	unsigned char str, data;
753 
754 	guard(spinlock_irqsave)(&i8042_lock);
755 
756 	str = i8042_read_status();
757 	if (!(str & I8042_STR_OBF))
758 		return IRQ_NONE;
759 
760 	data = i8042_read_data();
761 	dbg("%02x <- i8042 (aux_test_irq, %s)\n",
762 	    data, str & I8042_STR_AUXDATA ? "aux" : "kbd");
763 
764 	if (i8042_irq_being_tested && data == 0xa5 && (str & I8042_STR_AUXDATA))
765 		complete(&i8042_aux_irq_delivered);
766 
767 	return IRQ_HANDLED;
768 }
769 
770 /*
771  * i8042_toggle_aux - enables or disables AUX port on i8042 via command and
772  * verifies success by readinng CTR. Used when testing for presence of AUX
773  * port.
774  */
i8042_toggle_aux(bool on)775 static int i8042_toggle_aux(bool on)
776 {
777 	unsigned char param;
778 	int i;
779 
780 	if (i8042_command(&param,
781 			on ? I8042_CMD_AUX_ENABLE : I8042_CMD_AUX_DISABLE))
782 		return -1;
783 
784 	/* some chips need some time to set the I8042_CTR_AUXDIS bit */
785 	for (i = 0; i < 100; i++) {
786 		udelay(50);
787 
788 		if (i8042_command(&param, I8042_CMD_CTL_RCTR))
789 			return -1;
790 
791 		if (!(param & I8042_CTR_AUXDIS) == on)
792 			return 0;
793 	}
794 
795 	return -1;
796 }
797 
798 /*
799  * i8042_check_aux() applies as much paranoia as it can at detecting
800  * the presence of an AUX interface.
801  */
802 
i8042_check_aux(void)803 static int i8042_check_aux(void)
804 {
805 	int retval = -1;
806 	bool irq_registered = false;
807 	bool aux_loop_broken = false;
808 	unsigned char param;
809 
810 /*
811  * Get rid of bytes in the queue.
812  */
813 
814 	i8042_flush();
815 
816 /*
817  * Internal loopback test - filters out AT-type i8042's. Unfortunately
818  * SiS screwed up and their 5597 doesn't support the LOOP command even
819  * though it has an AUX port.
820  */
821 
822 	param = 0x5a;
823 	retval = i8042_command(&param, I8042_CMD_AUX_LOOP);
824 	if (retval || param != 0x5a) {
825 
826 /*
827  * External connection test - filters out AT-soldered PS/2 i8042's
828  * 0x00 - no error, 0x01-0x03 - clock/data stuck, 0xff - general error
829  * 0xfa - no error on some notebooks which ignore the spec
830  * Because it's common for chipsets to return error on perfectly functioning
831  * AUX ports, we test for this only when the LOOP command failed.
832  */
833 
834 		if (i8042_command(&param, I8042_CMD_AUX_TEST) ||
835 		    (param && param != 0xfa && param != 0xff))
836 			return -1;
837 
838 /*
839  * If AUX_LOOP completed without error but returned unexpected data
840  * mark it as broken
841  */
842 		if (!retval)
843 			aux_loop_broken = true;
844 	}
845 
846 /*
847  * Bit assignment test - filters out PS/2 i8042's in AT mode
848  */
849 
850 	if (i8042_toggle_aux(false)) {
851 		pr_warn("Failed to disable AUX port, but continuing anyway... Is this a SiS?\n");
852 		pr_warn("If AUX port is really absent please use the 'i8042.noaux' option\n");
853 	}
854 
855 	if (i8042_toggle_aux(true))
856 		return -1;
857 
858 /*
859  * Reset keyboard (needed on some laptops to successfully detect
860  * touchpad, e.g., some Gigabyte laptop models with Elantech
861  * touchpads).
862  */
863 	if (i8042_kbdreset) {
864 		pr_warn("Attempting to reset device connected to KBD port\n");
865 		i8042_kbd_write(NULL, (unsigned char) 0xff);
866 	}
867 
868 /*
869  * Test AUX IRQ delivery to make sure BIOS did not grab the IRQ and
870  * used it for a PCI card or somethig else.
871  */
872 
873 	if (i8042_noloop || i8042_bypass_aux_irq_test || aux_loop_broken) {
874 /*
875  * Without LOOP command we can't test AUX IRQ delivery. Assume the port
876  * is working and hope we are right.
877  */
878 		retval = 0;
879 		goto out;
880 	}
881 
882 	if (request_irq(I8042_AUX_IRQ, i8042_aux_test_irq, IRQF_SHARED,
883 			"i8042", i8042_platform_device))
884 		goto out;
885 
886 	irq_registered = true;
887 
888 	if (i8042_enable_aux_port())
889 		goto out;
890 
891 	scoped_guard(spinlock_irqsave, &i8042_lock) {
892 		init_completion(&i8042_aux_irq_delivered);
893 		i8042_irq_being_tested = true;
894 
895 		param = 0xa5;
896 		retval = __i8042_command(&param, I8042_CMD_AUX_LOOP & 0xf0ff);
897 		if (retval)
898 			goto out;
899 	}
900 
901 	if (wait_for_completion_timeout(&i8042_aux_irq_delivered,
902 					msecs_to_jiffies(250)) == 0) {
903 /*
904  * AUX IRQ was never delivered so we need to flush the controller to
905  * get rid of the byte we put there; otherwise keyboard may not work.
906  */
907 		dbg("     -- i8042 (aux irq test timeout)\n");
908 		i8042_flush();
909 		retval = -1;
910 	}
911 
912  out:
913 
914 /*
915  * Disable the interface.
916  */
917 
918 	i8042_ctr |= I8042_CTR_AUXDIS;
919 	i8042_ctr &= ~I8042_CTR_AUXINT;
920 
921 	if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR))
922 		retval = -1;
923 
924 	if (irq_registered)
925 		free_irq(I8042_AUX_IRQ, i8042_platform_device);
926 
927 	return retval;
928 }
929 
i8042_controller_check(void)930 static int i8042_controller_check(void)
931 {
932 	if (i8042_flush()) {
933 		pr_info("No controller found\n");
934 		return -ENODEV;
935 	}
936 
937 	return 0;
938 }
939 
i8042_controller_selftest(void)940 static int i8042_controller_selftest(void)
941 {
942 	unsigned char param;
943 	int i = 0;
944 
945 	/*
946 	 * We try this 5 times; on some really fragile systems this does not
947 	 * take the first time...
948 	 */
949 	do {
950 
951 		if (i8042_command(&param, I8042_CMD_CTL_TEST)) {
952 			pr_err("i8042 controller selftest timeout\n");
953 			return -ENODEV;
954 		}
955 
956 		if (param == I8042_RET_CTL_TEST)
957 			return 0;
958 
959 		dbg("i8042 controller selftest: %#x != %#x\n",
960 		    param, I8042_RET_CTL_TEST);
961 		msleep(50);
962 	} while (i++ < 5);
963 
964 #ifdef CONFIG_X86
965 	/*
966 	 * On x86, we don't fail entire i8042 initialization if controller
967 	 * reset fails in hopes that keyboard port will still be functional
968 	 * and user will still get a working keyboard. This is especially
969 	 * important on netbooks. On other arches we trust hardware more.
970 	 */
971 	pr_info("giving up on controller selftest, continuing anyway...\n");
972 	return 0;
973 #else
974 	pr_err("i8042 controller selftest failed\n");
975 	return -EIO;
976 #endif
977 }
978 
979 /*
980  * i8042_controller_init initializes the i8042 controller, and,
981  * most importantly, sets it into non-xlated mode if that's
982  * desired.
983  */
984 
i8042_controller_init(void)985 static int i8042_controller_init(void)
986 {
987 	int n = 0;
988 	unsigned char ctr[2];
989 
990 /*
991  * Save the CTR for restore on unload / reboot.
992  */
993 
994 	do {
995 		if (n >= 10) {
996 			pr_err("Unable to get stable CTR read\n");
997 			return -EIO;
998 		}
999 
1000 		if (n != 0)
1001 			udelay(50);
1002 
1003 		if (i8042_command(&ctr[n++ % 2], I8042_CMD_CTL_RCTR)) {
1004 			pr_err("Can't read CTR while initializing i8042\n");
1005 			return i8042_probe_defer ? -EPROBE_DEFER : -EIO;
1006 		}
1007 
1008 	} while (n < 2 || ctr[0] != ctr[1]);
1009 
1010 	i8042_initial_ctr = i8042_ctr = ctr[0];
1011 
1012 /*
1013  * Disable the keyboard interface and interrupt.
1014  */
1015 
1016 	i8042_ctr |= I8042_CTR_KBDDIS;
1017 	i8042_ctr &= ~I8042_CTR_KBDINT;
1018 
1019 /*
1020  * Handle keylock.
1021  */
1022 
1023 	scoped_guard(spinlock_irqsave, &i8042_lock) {
1024 		if (~i8042_read_status() & I8042_STR_KEYLOCK) {
1025 			if (i8042_unlock)
1026 				i8042_ctr |= I8042_CTR_IGNKEYLOCK;
1027 			else
1028 				pr_warn("Warning: Keylock active\n");
1029 		}
1030 	}
1031 
1032 /*
1033  * If the chip is configured into nontranslated mode by the BIOS, don't
1034  * bother enabling translating and be happy.
1035  */
1036 
1037 	if (~i8042_ctr & I8042_CTR_XLATE)
1038 		i8042_direct = true;
1039 
1040 /*
1041  * Set nontranslated mode for the kbd interface if requested by an option.
1042  * After this the kbd interface becomes a simple serial in/out, like the aux
1043  * interface is. We don't do this by default, since it can confuse notebook
1044  * BIOSes.
1045  */
1046 
1047 	if (i8042_direct)
1048 		i8042_ctr &= ~I8042_CTR_XLATE;
1049 
1050 /*
1051  * Write CTR back.
1052  */
1053 
1054 	if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
1055 		pr_err("Can't write CTR while initializing i8042\n");
1056 		return -EIO;
1057 	}
1058 
1059 /*
1060  * Flush whatever accumulated while we were disabling keyboard port.
1061  */
1062 
1063 	i8042_flush();
1064 
1065 	return 0;
1066 }
1067 
1068 
1069 /*
1070  * Reset the controller and reset CRT to the original value set by BIOS.
1071  */
1072 
i8042_controller_reset(bool s2r_wants_reset)1073 static void i8042_controller_reset(bool s2r_wants_reset)
1074 {
1075 	i8042_flush();
1076 
1077 /*
1078  * Disable both KBD and AUX interfaces so they don't get in the way
1079  */
1080 
1081 	i8042_ctr |= I8042_CTR_KBDDIS | I8042_CTR_AUXDIS;
1082 	i8042_ctr &= ~(I8042_CTR_KBDINT | I8042_CTR_AUXINT);
1083 
1084 	if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR))
1085 		pr_warn("Can't write CTR while resetting\n");
1086 
1087 /*
1088  * Disable MUX mode if present.
1089  */
1090 
1091 	if (i8042_mux_present)
1092 		i8042_set_mux_mode(false, NULL);
1093 
1094 /*
1095  * Reset the controller if requested.
1096  */
1097 
1098 	if (i8042_reset == I8042_RESET_ALWAYS ||
1099 	    (i8042_reset == I8042_RESET_ON_S2RAM && s2r_wants_reset)) {
1100 		i8042_controller_selftest();
1101 	}
1102 
1103 /*
1104  * Restore the original control register setting.
1105  */
1106 
1107 	if (i8042_command(&i8042_initial_ctr, I8042_CMD_CTL_WCTR))
1108 		pr_warn("Can't restore CTR\n");
1109 }
1110 
1111 
1112 /*
1113  * i8042_panic_blink() will turn the keyboard LEDs on or off and is called
1114  * when kernel panics. Flashing LEDs is useful for users running X who may
1115  * not see the console and will help distinguishing panics from "real"
1116  * lockups.
1117  *
1118  * Note that DELAY has a limit of 10ms so we will not get stuck here
1119  * waiting for KBC to free up even if KBD interrupt is off
1120  */
1121 
1122 #define DELAY do { mdelay(1); if (++delay > 10) return delay; } while(0)
1123 
i8042_panic_blink(int state)1124 static long i8042_panic_blink(int state)
1125 {
1126 	long delay = 0;
1127 	char led;
1128 
1129 	led = (state) ? 0x01 | 0x04 : 0;
1130 	while (i8042_read_status() & I8042_STR_IBF)
1131 		DELAY;
1132 	dbg("%02x -> i8042 (panic blink)\n", 0xed);
1133 	i8042_suppress_kbd_ack = 2;
1134 	i8042_write_data(0xed); /* set leds */
1135 	DELAY;
1136 	while (i8042_read_status() & I8042_STR_IBF)
1137 		DELAY;
1138 	DELAY;
1139 	dbg("%02x -> i8042 (panic blink)\n", led);
1140 	i8042_write_data(led);
1141 	DELAY;
1142 	return delay;
1143 }
1144 
1145 #undef DELAY
1146 
1147 #ifdef CONFIG_X86
i8042_dritek_enable(void)1148 static void i8042_dritek_enable(void)
1149 {
1150 	unsigned char param = 0x90;
1151 	int error;
1152 
1153 	error = i8042_command(&param, 0x1059);
1154 	if (error)
1155 		pr_warn("Failed to enable DRITEK extension: %d\n", error);
1156 }
1157 #endif
1158 
1159 #ifdef CONFIG_PM
1160 
1161 /*
1162  * Here we try to reset everything back to a state we had
1163  * before suspending.
1164  */
1165 
i8042_controller_resume(bool s2r_wants_reset)1166 static int i8042_controller_resume(bool s2r_wants_reset)
1167 {
1168 	int error;
1169 
1170 	error = i8042_controller_check();
1171 	if (error)
1172 		return error;
1173 
1174 	if (i8042_reset == I8042_RESET_ALWAYS ||
1175 	    (i8042_reset == I8042_RESET_ON_S2RAM && s2r_wants_reset)) {
1176 		error = i8042_controller_selftest();
1177 		if (error)
1178 			return error;
1179 	}
1180 
1181 /*
1182  * Restore original CTR value and disable all ports
1183  */
1184 
1185 	i8042_ctr = i8042_initial_ctr;
1186 	if (i8042_direct)
1187 		i8042_ctr &= ~I8042_CTR_XLATE;
1188 	i8042_ctr |= I8042_CTR_AUXDIS | I8042_CTR_KBDDIS;
1189 	i8042_ctr &= ~(I8042_CTR_AUXINT | I8042_CTR_KBDINT);
1190 	if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
1191 		pr_warn("Can't write CTR to resume, retrying...\n");
1192 		msleep(50);
1193 		if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
1194 			pr_err("CTR write retry failed\n");
1195 			return -EIO;
1196 		}
1197 	}
1198 
1199 
1200 #ifdef CONFIG_X86
1201 	if (i8042_dritek)
1202 		i8042_dritek_enable();
1203 #endif
1204 
1205 	if (i8042_mux_present) {
1206 		if (i8042_set_mux_mode(true, NULL) || i8042_enable_mux_ports())
1207 			pr_warn("failed to resume active multiplexor, mouse won't work\n");
1208 	} else if (i8042_ports[I8042_AUX_PORT_NO].serio) {
1209 		i8042_enable_aux_port();
1210 	}
1211 
1212 	if (i8042_ports[I8042_KBD_PORT_NO].serio)
1213 		i8042_enable_kbd_port();
1214 
1215 	i8042_handle_data(0);
1216 
1217 	return 0;
1218 }
1219 
1220 /*
1221  * Here we try to restore the original BIOS settings to avoid
1222  * upsetting it.
1223  */
1224 
i8042_pm_suspend(struct device * dev)1225 static int i8042_pm_suspend(struct device *dev)
1226 {
1227 	int i;
1228 
1229 	if (!i8042_forcenorestore && pm_suspend_via_firmware())
1230 		i8042_controller_reset(true);
1231 
1232 	/* Set up serio interrupts for system wakeup. */
1233 	for (i = 0; i < I8042_NUM_PORTS; i++) {
1234 		struct serio *serio = i8042_ports[i].serio;
1235 
1236 		if (serio && device_may_wakeup(&serio->dev))
1237 			enable_irq_wake(i8042_ports[i].irq);
1238 	}
1239 
1240 	return 0;
1241 }
1242 
i8042_pm_resume_noirq(struct device * dev)1243 static int i8042_pm_resume_noirq(struct device *dev)
1244 {
1245 	if (i8042_forcenorestore || !pm_resume_via_firmware())
1246 		i8042_handle_data(0);
1247 
1248 	return 0;
1249 }
1250 
i8042_pm_resume(struct device * dev)1251 static int i8042_pm_resume(struct device *dev)
1252 {
1253 	bool want_reset;
1254 	int i;
1255 
1256 	for (i = 0; i < I8042_NUM_PORTS; i++) {
1257 		struct serio *serio = i8042_ports[i].serio;
1258 
1259 		if (serio && device_may_wakeup(&serio->dev))
1260 			disable_irq_wake(i8042_ports[i].irq);
1261 	}
1262 
1263 	/*
1264 	 * If platform firmware was not going to be involved in suspend, we did
1265 	 * not restore the controller state to whatever it had been at boot
1266 	 * time, so we do not need to do anything.
1267 	 */
1268 	if (i8042_forcenorestore || !pm_suspend_via_firmware())
1269 		return 0;
1270 
1271 	/*
1272 	 * We only need to reset the controller if we are resuming after handing
1273 	 * off control to the platform firmware, otherwise we can simply restore
1274 	 * the mode.
1275 	 */
1276 	want_reset = pm_resume_via_firmware();
1277 
1278 	return i8042_controller_resume(want_reset);
1279 }
1280 
i8042_pm_thaw(struct device * dev)1281 static int i8042_pm_thaw(struct device *dev)
1282 {
1283 	i8042_handle_data(0);
1284 
1285 	return 0;
1286 }
1287 
i8042_pm_reset(struct device * dev)1288 static int i8042_pm_reset(struct device *dev)
1289 {
1290 	i8042_controller_reset(false);
1291 
1292 	return 0;
1293 }
1294 
i8042_pm_restore(struct device * dev)1295 static int i8042_pm_restore(struct device *dev)
1296 {
1297 	return i8042_controller_resume(false);
1298 }
1299 
1300 static const struct dev_pm_ops i8042_pm_ops = {
1301 	.suspend	= i8042_pm_suspend,
1302 	.resume_noirq	= i8042_pm_resume_noirq,
1303 	.resume		= i8042_pm_resume,
1304 	.thaw		= i8042_pm_thaw,
1305 	.poweroff	= i8042_pm_reset,
1306 	.restore	= i8042_pm_restore,
1307 };
1308 
1309 #endif /* CONFIG_PM */
1310 
1311 /*
1312  * We need to reset the 8042 back to original mode on system shutdown,
1313  * because otherwise BIOSes will be confused.
1314  */
1315 
i8042_shutdown(struct platform_device * dev)1316 static void i8042_shutdown(struct platform_device *dev)
1317 {
1318 	i8042_controller_reset(false);
1319 }
1320 
i8042_create_kbd_port(void)1321 static int i8042_create_kbd_port(void)
1322 {
1323 	struct serio *serio;
1324 	struct i8042_port *port = &i8042_ports[I8042_KBD_PORT_NO];
1325 
1326 	serio = kzalloc(sizeof(*serio), GFP_KERNEL);
1327 	if (!serio)
1328 		return -ENOMEM;
1329 
1330 	serio->id.type		= i8042_direct ? SERIO_8042 : SERIO_8042_XL;
1331 	serio->write		= i8042_dumbkbd ? NULL : i8042_kbd_write;
1332 	serio->start		= i8042_start;
1333 	serio->stop		= i8042_stop;
1334 	serio->close		= i8042_port_close;
1335 	serio->ps2_cmd_mutex	= &i8042_mutex;
1336 	serio->port_data	= port;
1337 	serio->dev.parent	= &i8042_platform_device->dev;
1338 	strscpy(serio->name, "i8042 KBD port", sizeof(serio->name));
1339 	strscpy(serio->phys, I8042_KBD_PHYS_DESC, sizeof(serio->phys));
1340 	strscpy(serio->firmware_id, i8042_kbd_firmware_id,
1341 		sizeof(serio->firmware_id));
1342 	set_primary_fwnode(&serio->dev, i8042_kbd_fwnode);
1343 
1344 	port->serio = serio;
1345 	port->irq = I8042_KBD_IRQ;
1346 
1347 	return 0;
1348 }
1349 
i8042_create_aux_port(int idx)1350 static int i8042_create_aux_port(int idx)
1351 {
1352 	struct serio *serio;
1353 	int port_no = idx < 0 ? I8042_AUX_PORT_NO : I8042_MUX_PORT_NO + idx;
1354 	struct i8042_port *port = &i8042_ports[port_no];
1355 
1356 	serio = kzalloc(sizeof(*serio), GFP_KERNEL);
1357 	if (!serio)
1358 		return -ENOMEM;
1359 
1360 	serio->id.type		= SERIO_8042;
1361 	serio->write		= i8042_aux_write;
1362 	serio->start		= i8042_start;
1363 	serio->stop		= i8042_stop;
1364 	serio->ps2_cmd_mutex	= &i8042_mutex;
1365 	serio->port_data	= port;
1366 	serio->dev.parent	= &i8042_platform_device->dev;
1367 	if (idx < 0) {
1368 		strscpy(serio->name, "i8042 AUX port", sizeof(serio->name));
1369 		strscpy(serio->phys, I8042_AUX_PHYS_DESC, sizeof(serio->phys));
1370 		strscpy(serio->firmware_id, i8042_aux_firmware_id,
1371 			sizeof(serio->firmware_id));
1372 		serio->close = i8042_port_close;
1373 	} else {
1374 		snprintf(serio->name, sizeof(serio->name), "i8042 AUX%d port", idx);
1375 		snprintf(serio->phys, sizeof(serio->phys), I8042_MUX_PHYS_DESC, idx + 1);
1376 		strscpy(serio->firmware_id, i8042_aux_firmware_id,
1377 			sizeof(serio->firmware_id));
1378 	}
1379 
1380 	port->serio = serio;
1381 	port->mux = idx;
1382 	port->irq = I8042_AUX_IRQ;
1383 
1384 	return 0;
1385 }
1386 
i8042_free_kbd_port(void)1387 static void i8042_free_kbd_port(void)
1388 {
1389 	kfree(i8042_ports[I8042_KBD_PORT_NO].serio);
1390 	i8042_ports[I8042_KBD_PORT_NO].serio = NULL;
1391 }
1392 
i8042_free_aux_ports(void)1393 static void i8042_free_aux_ports(void)
1394 {
1395 	int i;
1396 
1397 	for (i = I8042_AUX_PORT_NO; i < I8042_NUM_PORTS; i++) {
1398 		kfree(i8042_ports[i].serio);
1399 		i8042_ports[i].serio = NULL;
1400 	}
1401 }
1402 
i8042_register_ports(void)1403 static void i8042_register_ports(void)
1404 {
1405 	int i;
1406 
1407 	for (i = 0; i < I8042_NUM_PORTS; i++) {
1408 		struct serio *serio = i8042_ports[i].serio;
1409 
1410 		if (!serio)
1411 			continue;
1412 
1413 		printk(KERN_INFO "serio: %s at %#lx,%#lx irq %d\n",
1414 			serio->name,
1415 			(unsigned long) I8042_DATA_REG,
1416 			(unsigned long) I8042_COMMAND_REG,
1417 			i8042_ports[i].irq);
1418 		serio_register_port(serio);
1419 	}
1420 }
1421 
i8042_unregister_ports(void)1422 static void i8042_unregister_ports(void)
1423 {
1424 	int i;
1425 
1426 	for (i = 0; i < I8042_NUM_PORTS; i++) {
1427 		if (i8042_ports[i].serio) {
1428 			serio_unregister_port(i8042_ports[i].serio);
1429 			i8042_ports[i].serio = NULL;
1430 		}
1431 	}
1432 }
1433 
i8042_free_irqs(void)1434 static void i8042_free_irqs(void)
1435 {
1436 	if (i8042_aux_irq_registered)
1437 		free_irq(I8042_AUX_IRQ, i8042_platform_device);
1438 	if (i8042_kbd_irq_registered)
1439 		free_irq(I8042_KBD_IRQ, i8042_platform_device);
1440 
1441 	i8042_aux_irq_registered = i8042_kbd_irq_registered = false;
1442 }
1443 
i8042_setup_aux(void)1444 static int i8042_setup_aux(void)
1445 {
1446 	int (*aux_enable)(void);
1447 	int error;
1448 	int i;
1449 
1450 	if (i8042_check_aux())
1451 		return -ENODEV;
1452 
1453 	if (i8042_nomux || i8042_check_mux()) {
1454 		error = i8042_create_aux_port(-1);
1455 		if (error)
1456 			goto err_free_ports;
1457 		aux_enable = i8042_enable_aux_port;
1458 	} else {
1459 		for (i = 0; i < I8042_NUM_MUX_PORTS; i++) {
1460 			error = i8042_create_aux_port(i);
1461 			if (error)
1462 				goto err_free_ports;
1463 		}
1464 		aux_enable = i8042_enable_mux_ports;
1465 	}
1466 
1467 	error = request_irq(I8042_AUX_IRQ, i8042_interrupt, IRQF_SHARED,
1468 			    "i8042", i8042_platform_device);
1469 	if (error)
1470 		goto err_free_ports;
1471 
1472 	error = aux_enable();
1473 	if (error)
1474 		goto err_free_irq;
1475 
1476 	i8042_aux_irq_registered = true;
1477 	return 0;
1478 
1479  err_free_irq:
1480 	free_irq(I8042_AUX_IRQ, i8042_platform_device);
1481  err_free_ports:
1482 	i8042_free_aux_ports();
1483 	return error;
1484 }
1485 
i8042_setup_kbd(void)1486 static int i8042_setup_kbd(void)
1487 {
1488 	int error;
1489 
1490 	error = i8042_create_kbd_port();
1491 	if (error)
1492 		return error;
1493 
1494 	error = request_irq(I8042_KBD_IRQ, i8042_interrupt, IRQF_SHARED,
1495 			    "i8042", i8042_platform_device);
1496 	if (error)
1497 		goto err_free_port;
1498 
1499 	error = i8042_enable_kbd_port();
1500 	if (error)
1501 		goto err_free_irq;
1502 
1503 	i8042_kbd_irq_registered = true;
1504 	return 0;
1505 
1506  err_free_irq:
1507 	free_irq(I8042_KBD_IRQ, i8042_platform_device);
1508  err_free_port:
1509 	i8042_free_kbd_port();
1510 	return error;
1511 }
1512 
i8042_kbd_bind_notifier(struct notifier_block * nb,unsigned long action,void * data)1513 static int i8042_kbd_bind_notifier(struct notifier_block *nb,
1514 				   unsigned long action, void *data)
1515 {
1516 	struct device *dev = data;
1517 	struct serio *serio = to_serio_port(dev);
1518 	struct i8042_port *port = serio->port_data;
1519 
1520 	if (serio != i8042_ports[I8042_KBD_PORT_NO].serio)
1521 		return 0;
1522 
1523 	switch (action) {
1524 	case BUS_NOTIFY_BOUND_DRIVER:
1525 		port->driver_bound = true;
1526 		break;
1527 
1528 	case BUS_NOTIFY_UNBIND_DRIVER:
1529 		port->driver_bound = false;
1530 		break;
1531 	}
1532 
1533 	return 0;
1534 }
1535 
i8042_probe(struct platform_device * dev)1536 static int i8042_probe(struct platform_device *dev)
1537 {
1538 	int error;
1539 
1540 	if (i8042_reset == I8042_RESET_ALWAYS) {
1541 		error = i8042_controller_selftest();
1542 		if (error)
1543 			return error;
1544 	}
1545 
1546 	error = i8042_controller_init();
1547 	if (error)
1548 		return error;
1549 
1550 #ifdef CONFIG_X86
1551 	if (i8042_dritek)
1552 		i8042_dritek_enable();
1553 #endif
1554 
1555 	if (!i8042_noaux) {
1556 		error = i8042_setup_aux();
1557 		if (error && error != -ENODEV && error != -EBUSY)
1558 			goto out_fail;
1559 	}
1560 
1561 	if (!i8042_nokbd) {
1562 		error = i8042_setup_kbd();
1563 		if (error)
1564 			goto out_fail;
1565 	}
1566 /*
1567  * Ok, everything is ready, let's register all serio ports
1568  */
1569 	i8042_register_ports();
1570 
1571 	return 0;
1572 
1573  out_fail:
1574 	i8042_free_aux_ports();	/* in case KBD failed but AUX not */
1575 	i8042_free_irqs();
1576 	i8042_controller_reset(false);
1577 
1578 	return error;
1579 }
1580 
i8042_remove(struct platform_device * dev)1581 static void i8042_remove(struct platform_device *dev)
1582 {
1583 	i8042_unregister_ports();
1584 	i8042_free_irqs();
1585 	i8042_controller_reset(false);
1586 }
1587 
1588 static struct platform_driver i8042_driver = {
1589 	.driver		= {
1590 		.name	= "i8042",
1591 #ifdef CONFIG_PM
1592 		.pm	= &i8042_pm_ops,
1593 #endif
1594 	},
1595 	.probe		= i8042_probe,
1596 	.remove		= i8042_remove,
1597 	.shutdown	= i8042_shutdown,
1598 };
1599 
1600 static struct notifier_block i8042_kbd_bind_notifier_block = {
1601 	.notifier_call = i8042_kbd_bind_notifier,
1602 };
1603 
i8042_init(void)1604 static int __init i8042_init(void)
1605 {
1606 	int err;
1607 
1608 	dbg_init();
1609 
1610 	err = i8042_platform_init();
1611 	if (err)
1612 		return (err == -ENODEV) ? 0 : err;
1613 
1614 	err = i8042_controller_check();
1615 	if (err)
1616 		goto err_platform_exit;
1617 
1618 	/* Set this before creating the dev to allow i8042_command to work right away */
1619 	i8042_present = true;
1620 
1621 	err = platform_driver_register(&i8042_driver);
1622 	if (err)
1623 		goto err_platform_exit;
1624 
1625 	i8042_platform_device = platform_device_alloc("i8042", -1);
1626 	if (!i8042_platform_device) {
1627 		err = -ENOMEM;
1628 		goto err_unregister_driver;
1629 	}
1630 
1631 	err = platform_device_add(i8042_platform_device);
1632 	if (err)
1633 		goto err_free_device;
1634 
1635 	bus_register_notifier(&serio_bus, &i8042_kbd_bind_notifier_block);
1636 	panic_blink = i8042_panic_blink;
1637 
1638 	return 0;
1639 
1640 err_free_device:
1641 	platform_device_put(i8042_platform_device);
1642 err_unregister_driver:
1643 	platform_driver_unregister(&i8042_driver);
1644  err_platform_exit:
1645 	i8042_platform_exit();
1646 	return err;
1647 }
1648 
i8042_exit(void)1649 static void __exit i8042_exit(void)
1650 {
1651 	if (!i8042_present)
1652 		return;
1653 
1654 	platform_device_unregister(i8042_platform_device);
1655 	platform_driver_unregister(&i8042_driver);
1656 	i8042_platform_exit();
1657 
1658 	bus_unregister_notifier(&serio_bus, &i8042_kbd_bind_notifier_block);
1659 	panic_blink = NULL;
1660 }
1661 
1662 module_init(i8042_init);
1663 module_exit(i8042_exit);
1664