1 // SPDX-License-Identifier: GPL-2.0-only
3 * i8042 keyboard and mouse controller driver for Linux
5 * Copyright (c) 1999-2004 Vojtech Pavlik
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
28 MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
29 MODULE_DESCRIPTION("i8042 keyboard and mouse controller driver");
30 MODULE_LICENSE("GPL");
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.");
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.");
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.");
44 static bool i8042_unlock;
45 module_param_named(unlock, i8042_unlock, bool, 0);
46 MODULE_PARM_DESC(unlock, "Ignore keyboard lock.");
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.");
52 enum i8042_controller_reset_mode {
56 #define I8042_RESET_DEFAULT I8042_RESET_ON_S2RAM
58 static enum i8042_controller_reset_mode i8042_reset = I8042_RESET_DEFAULT;
59 static int i8042_set_reset(const char *val, const struct kernel_param *kp)
61 enum i8042_controller_reset_mode *arg = kp->arg;
66 error = kstrtobool(val, &reset);
73 *arg = reset ? I8042_RESET_ALWAYS : I8042_RESET_NEVER;
77 static const struct kernel_param_ops param_ops_reset_param = {
78 .flags = KERNEL_PARAM_OPS_FL_NOARG,
79 .set = i8042_set_reset,
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");
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.");
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");
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");
98 static bool i8042_notimeout;
99 module_param_named(notimeout, i8042_notimeout, bool, 0);
100 MODULE_PARM_DESC(notimeout, "Ignore timeouts signalled by i8042");
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");
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");
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");
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");
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");
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]");
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;
142 * i8042_lock protects serialization between i8042_command and
143 * the interrupt handler.
145 static DEFINE_SPINLOCK(i8042_lock);
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).
155 static DEFINE_MUTEX(i8042_mutex);
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)
170 static struct i8042_port i8042_ports[I8042_NUM_PORTS];
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;
181 static bool i8042_handle_data(int irq);
182 static i8042_filter_t i8042_platform_filter;
183 static void *i8042_platform_filter_context;
185 void i8042_lock_chip(void)
187 mutex_lock(&i8042_mutex);
189 EXPORT_SYMBOL(i8042_lock_chip);
191 void i8042_unlock_chip(void)
193 mutex_unlock(&i8042_mutex);
195 EXPORT_SYMBOL(i8042_unlock_chip);
197 int i8042_install_filter(i8042_filter_t filter, void *context)
199 guard(spinlock_irqsave)(&i8042_lock);
201 if (i8042_platform_filter)
204 i8042_platform_filter = filter;
205 i8042_platform_filter_context = context;
208 EXPORT_SYMBOL(i8042_install_filter);
210 int i8042_remove_filter(i8042_filter_t filter)
212 guard(spinlock_irqsave)(&i8042_lock);
214 if (i8042_platform_filter != filter)
217 i8042_platform_filter = NULL;
218 i8042_platform_filter_context = NULL;
221 EXPORT_SYMBOL(i8042_remove_filter);
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.
229 static int i8042_wait_read(void)
233 while ((~i8042_read_status() & I8042_STR_OBF) && (i < I8042_CTL_TIMEOUT)) {
237 return -(i == I8042_CTL_TIMEOUT);
240 static int i8042_wait_write(void)
244 while ((i8042_read_status() & I8042_STR_IBF) && (i < I8042_CTL_TIMEOUT)) {
248 return -(i == I8042_CTL_TIMEOUT);
252 * i8042_flush() flushes all data that may be in the keyboard and mouse buffers
253 * of the i8042 down the toilet.
256 static int i8042_flush(void)
258 unsigned char data, str;
261 guard(spinlock_irqsave)(&i8042_lock);
263 while ((str = i8042_read_status()) & I8042_STR_OBF) {
264 if (count++ >= I8042_BUFFER_SIZE)
268 data = i8042_read_data();
269 dbg("%02x <- i8042 (flush, %s)\n",
270 data, str & I8042_STR_AUXDATA ? "aux" : "kbd");
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.
284 static int __i8042_command(unsigned char *param, int command)
288 if (i8042_noloop && command == I8042_CMD_AUX_LOOP)
291 error = i8042_wait_write();
295 dbg("%02x -> i8042 (command)\n", command & 0xff);
296 i8042_write_command(command & 0xff);
298 for (i = 0; i < ((command >> 12) & 0xf); i++) {
299 error = i8042_wait_write();
301 dbg(" -- i8042 (wait write timeout)\n");
304 dbg("%02x -> i8042 (parameter)\n", param[i]);
305 i8042_write_data(param[i]);
308 for (i = 0; i < ((command >> 8) & 0xf); i++) {
309 error = i8042_wait_read();
311 dbg(" -- i8042 (wait read timeout)\n");
315 if (command == I8042_CMD_AUX_LOOP &&
316 !(i8042_read_status() & I8042_STR_AUXDATA)) {
317 dbg(" -- i8042 (auxerr)\n");
321 param[i] = i8042_read_data();
322 dbg("%02x <- i8042 (return)\n", param[i]);
328 int i8042_command(unsigned char *param, int command)
333 guard(spinlock_irqsave)(&i8042_lock);
335 return __i8042_command(param, command);
337 EXPORT_SYMBOL(i8042_command);
340 * i8042_kbd_write() sends a byte out through the keyboard interface.
343 static int i8042_kbd_write(struct serio *port, unsigned char c)
347 guard(spinlock_irqsave)(&i8042_lock);
349 error = i8042_wait_write();
353 dbg("%02x -> i8042 (kbd-data)\n", c);
360 * i8042_aux_write() sends a byte out through the aux interface.
363 static int i8042_aux_write(struct serio *serio, unsigned char c)
365 struct i8042_port *port = serio->port_data;
367 return i8042_command(&c, port->mux == -1 ?
369 I8042_CMD_MUX_SEND + port->mux);
374 * i8042_port_close attempts to clear AUX or KBD port state by disabling
375 * and then re-enabling it.
378 static void i8042_port_close(struct serio *serio)
382 const char *port_name;
384 if (serio == i8042_ports[I8042_AUX_PORT_NO].serio) {
385 irq_bit = I8042_CTR_AUXINT;
386 disable_bit = I8042_CTR_AUXDIS;
389 irq_bit = I8042_CTR_KBDINT;
390 disable_bit = I8042_CTR_KBDDIS;
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);
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);
406 * See if there is any data appeared while we were messing with
409 i8042_handle_data(0);
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.
417 static int i8042_start(struct serio *serio)
419 struct i8042_port *port = serio->port_data;
421 device_set_wakeup_capable(&serio->dev, true);
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)
430 if (pm_suspend_default_s2idle() &&
431 serio == i8042_ports[I8042_KBD_PORT_NO].serio) {
432 device_set_wakeup_enable(&serio->dev, true);
435 guard(spinlock_irq)(&i8042_lock);
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.
446 static void i8042_stop(struct serio *serio)
448 struct i8042_port *port = serio->port_data;
450 scoped_guard(spinlock_irq, &i8042_lock) {
451 port->exists = false;
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
462 synchronize_irq(I8042_AUX_IRQ);
463 synchronize_irq(I8042_KBD_IRQ);
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.
471 static bool i8042_filter(unsigned char data, unsigned char str,
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");
483 if (!i8042_platform_filter)
486 if (i8042_platform_filter(data, str, serio, i8042_platform_filter_context)) {
487 dbg("Filtered out by platform filter\n");
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
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).
510 static int i8042_handle_mux(u8 str, u8 *data, unsigned int *dfl)
512 static unsigned long last_transmit;
513 static unsigned long last_port;
514 unsigned int mux_port;
516 mux_port = (str >> 6) & 3;
519 if (str & I8042_STR_MUXERR) {
520 dbg("MUX error, status is %02x, data is %02x\n",
525 if (time_before(jiffies, last_transmit + HZ/10)) {
526 mux_port = last_port;
529 fallthrough; /* report timeout */
533 *dfl = SERIO_TIMEOUT;
543 last_port = mux_port;
544 last_transmit = jiffies;
546 return I8042_MUX_PORT_NO + mux_port;
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.
554 * Returns true if there was data waiting, false otherwise.
556 static bool i8042_handle_data(int irq)
558 struct i8042_port *port;
560 unsigned char str, data;
562 unsigned int port_no;
565 scoped_guard(spinlock_irqsave, &i8042_lock) {
566 str = i8042_read_status();
567 if (unlikely(~str & I8042_STR_OBF))
570 data = i8042_read_data();
572 if (i8042_mux_present && (str & I8042_STR_AUXDATA)) {
573 port_no = i8042_handle_mux(str, &data, &dfl);
576 dfl = (str & I8042_STR_PARITY) ? SERIO_PARITY : 0;
577 if ((str & I8042_STR_TIMEOUT) && !i8042_notimeout)
578 dfl |= SERIO_TIMEOUT;
580 port_no = (str & I8042_STR_AUXDATA) ?
581 I8042_AUX_PORT_NO : I8042_KBD_PORT_NO;
584 port = &i8042_ports[port_no];
585 serio = port->exists ? port->serio : NULL;
587 filter_dbg(port->driver_bound,
588 data, "<- i8042 (interrupt, %d, %d%s%s)\n",
590 dfl & SERIO_PARITY ? ", bad parity" : "",
591 dfl & SERIO_TIMEOUT ? ", timeout" : "");
593 filtered = i8042_filter(data, str, serio);
596 if (likely(serio && !filtered))
597 serio_interrupt(serio, data, dfl);
602 static irqreturn_t i8042_interrupt(int irq, void *dev_id)
604 if (unlikely(!i8042_handle_data(irq))) {
605 dbg("Interrupt %d, without any data\n", irq);
613 * i8042_enable_kbd_port enables keyboard port on chip
616 static int i8042_enable_kbd_port(void)
618 i8042_ctr &= ~I8042_CTR_KBDDIS;
619 i8042_ctr |= I8042_CTR_KBDINT;
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");
632 * i8042_enable_aux_port enables AUX (mouse) port on chip
635 static int i8042_enable_aux_port(void)
637 i8042_ctr &= ~I8042_CTR_AUXDIS;
638 i8042_ctr |= I8042_CTR_AUXINT;
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");
651 * i8042_enable_mux_ports enables 4 individual AUX ports after
652 * the controller has been switched into Multiplexed mode
655 static int i8042_enable_mux_ports(void)
660 for (i = 0; i < I8042_NUM_MUX_PORTS; i++) {
661 i8042_command(¶m, I8042_CMD_MUX_PFX + i);
662 i8042_command(¶m, I8042_CMD_AUX_ENABLE);
665 return i8042_enable_aux_port();
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.
674 static int i8042_set_mux_mode(bool multiplex, unsigned char *mux_version)
677 unsigned char param, val;
679 * Get rid of bytes in the queue.
685 * Internal loopback test - send three bytes, they should come back from the
686 * mouse interface, the last should be version.
690 if (i8042_command(¶m, I8042_CMD_AUX_LOOP) || param != val)
692 param = val = multiplex ? 0x56 : 0xf6;
693 if (i8042_command(¶m, I8042_CMD_AUX_LOOP) || param != val)
695 param = val = multiplex ? 0xa4 : 0xa5;
696 if (i8042_command(¶m, I8042_CMD_AUX_LOOP) || param == val)
700 * Workaround for interference with USB Legacy emulation
701 * that causes a v10.12 MUX to be found.
707 *mux_version = param;
713 * i8042_check_mux() checks whether the controller supports the PS/2 Active
714 * Multiplexing specification by Synaptics, Phoenix, Insyde and
718 static int i8042_check_mux(void)
720 unsigned char mux_version;
722 if (i8042_set_mux_mode(true, &mux_version))
725 pr_info("Detected active multiplexing controller, rev %d.%d\n",
726 (mux_version >> 4) & 0xf, mux_version & 0xf);
729 * Disable all muxed ports by disabling AUX.
731 i8042_ctr |= I8042_CTR_AUXDIS;
732 i8042_ctr &= ~I8042_CTR_AUXINT;
734 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
735 pr_err("Failed to disable AUX port, can't use MUX\n");
739 i8042_mux_present = true;
745 * The following is used to test AUX IRQ delivery.
747 static struct completion i8042_aux_irq_delivered;
748 static bool i8042_irq_being_tested;
750 static irqreturn_t i8042_aux_test_irq(int irq, void *dev_id)
752 unsigned char str, data;
754 guard(spinlock_irqsave)(&i8042_lock);
756 str = i8042_read_status();
757 if (!(str & I8042_STR_OBF))
760 data = i8042_read_data();
761 dbg("%02x <- i8042 (aux_test_irq, %s)\n",
762 data, str & I8042_STR_AUXDATA ? "aux" : "kbd");
764 if (i8042_irq_being_tested && data == 0xa5 && (str & I8042_STR_AUXDATA))
765 complete(&i8042_aux_irq_delivered);
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
775 static int i8042_toggle_aux(bool on)
780 if (i8042_command(¶m,
781 on ? I8042_CMD_AUX_ENABLE : I8042_CMD_AUX_DISABLE))
784 /* some chips need some time to set the I8042_CTR_AUXDIS bit */
785 for (i = 0; i < 100; i++) {
788 if (i8042_command(¶m, I8042_CMD_CTL_RCTR))
791 if (!(param & I8042_CTR_AUXDIS) == on)
799 * i8042_check_aux() applies as much paranoia as it can at detecting
800 * the presence of an AUX interface.
803 static int i8042_check_aux(void)
806 bool irq_registered = false;
807 bool aux_loop_broken = false;
811 * Get rid of bytes in the queue.
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.
823 retval = i8042_command(¶m, I8042_CMD_AUX_LOOP);
824 if (retval || param != 0x5a) {
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.
834 if (i8042_command(¶m, I8042_CMD_AUX_TEST) ||
835 (param && param != 0xfa && param != 0xff))
839 * If AUX_LOOP completed without error but returned unexpected data
843 aux_loop_broken = true;
847 * Bit assignment test - filters out PS/2 i8042's in AT mode
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");
855 if (i8042_toggle_aux(true))
859 * Reset keyboard (needed on some laptops to successfully detect
860 * touchpad, e.g., some Gigabyte laptop models with Elantech
863 if (i8042_kbdreset) {
864 pr_warn("Attempting to reset device connected to KBD port\n");
865 i8042_kbd_write(NULL, (unsigned char) 0xff);
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.
873 if (i8042_noloop || i8042_bypass_aux_irq_test || aux_loop_broken) {
875 * Without LOOP command we can't test AUX IRQ delivery. Assume the port
876 * is working and hope we are right.
882 if (request_irq(I8042_AUX_IRQ, i8042_aux_test_irq, IRQF_SHARED,
883 "i8042", i8042_platform_device))
886 irq_registered = true;
888 if (i8042_enable_aux_port())
891 scoped_guard(spinlock_irqsave, &i8042_lock) {
892 init_completion(&i8042_aux_irq_delivered);
893 i8042_irq_being_tested = true;
896 retval = __i8042_command(¶m, I8042_CMD_AUX_LOOP & 0xf0ff);
901 if (wait_for_completion_timeout(&i8042_aux_irq_delivered,
902 msecs_to_jiffies(250)) == 0) {
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.
907 dbg(" -- i8042 (aux irq test timeout)\n");
915 * Disable the interface.
918 i8042_ctr |= I8042_CTR_AUXDIS;
919 i8042_ctr &= ~I8042_CTR_AUXINT;
921 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR))
925 free_irq(I8042_AUX_IRQ, i8042_platform_device);
930 static int i8042_controller_check(void)
933 pr_info("No controller found\n");
940 static int i8042_controller_selftest(void)
946 * We try this 5 times; on some really fragile systems this does not
947 * take the first time...
951 if (i8042_command(¶m, I8042_CMD_CTL_TEST)) {
952 pr_err("i8042 controller selftest timeout\n");
956 if (param == I8042_RET_CTL_TEST)
959 dbg("i8042 controller selftest: %#x != %#x\n",
960 param, I8042_RET_CTL_TEST);
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.
971 pr_info("giving up on controller selftest, continuing anyway...\n");
974 pr_err("i8042 controller selftest failed\n");
980 * i8042_controller_init initializes the i8042 controller, and,
981 * most importantly, sets it into non-xlated mode if that's
985 static int i8042_controller_init(void)
988 unsigned char ctr[2];
991 * Save the CTR for restore on unload / reboot.
996 pr_err("Unable to get stable CTR read\n");
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;
1008 } while (n < 2 || ctr[0] != ctr[1]);
1010 i8042_initial_ctr = i8042_ctr = ctr[0];
1013 * Disable the keyboard interface and interrupt.
1016 i8042_ctr |= I8042_CTR_KBDDIS;
1017 i8042_ctr &= ~I8042_CTR_KBDINT;
1023 scoped_guard(spinlock_irqsave, &i8042_lock) {
1024 if (~i8042_read_status() & I8042_STR_KEYLOCK) {
1026 i8042_ctr |= I8042_CTR_IGNKEYLOCK;
1028 pr_warn("Warning: Keylock active\n");
1033 * If the chip is configured into nontranslated mode by the BIOS, don't
1034 * bother enabling translating and be happy.
1037 if (~i8042_ctr & I8042_CTR_XLATE)
1038 i8042_direct = true;
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
1048 i8042_ctr &= ~I8042_CTR_XLATE;
1054 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
1055 pr_err("Can't write CTR while initializing i8042\n");
1060 * Flush whatever accumulated while we were disabling keyboard port.
1070 * Reset the controller and reset CRT to the original value set by BIOS.
1073 static void i8042_controller_reset(bool s2r_wants_reset)
1078 * Disable both KBD and AUX interfaces so they don't get in the way
1081 i8042_ctr |= I8042_CTR_KBDDIS | I8042_CTR_AUXDIS;
1082 i8042_ctr &= ~(I8042_CTR_KBDINT | I8042_CTR_AUXINT);
1084 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR))
1085 pr_warn("Can't write CTR while resetting\n");
1088 * Disable MUX mode if present.
1091 if (i8042_mux_present)
1092 i8042_set_mux_mode(false, NULL);
1095 * Reset the controller if requested.
1098 if (i8042_reset == I8042_RESET_ALWAYS ||
1099 (i8042_reset == I8042_RESET_ON_S2RAM && s2r_wants_reset)) {
1100 i8042_controller_selftest();
1104 * Restore the original control register setting.
1107 if (i8042_command(&i8042_initial_ctr, I8042_CMD_CTL_WCTR))
1108 pr_warn("Can't restore CTR\n");
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"
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
1122 #define DELAY do { mdelay(1); if (++delay > 10) return delay; } while(0)
1124 static long i8042_panic_blink(int state)
1129 led = (state) ? 0x01 | 0x04 : 0;
1130 while (i8042_read_status() & I8042_STR_IBF)
1132 dbg("%02x -> i8042 (panic blink)\n", 0xed);
1133 i8042_suppress_kbd_ack = 2;
1134 i8042_write_data(0xed); /* set leds */
1136 while (i8042_read_status() & I8042_STR_IBF)
1139 dbg("%02x -> i8042 (panic blink)\n", led);
1140 i8042_write_data(led);
1148 static void i8042_dritek_enable(void)
1150 unsigned char param = 0x90;
1153 error = i8042_command(¶m, 0x1059);
1155 pr_warn("Failed to enable DRITEK extension: %d\n", error);
1162 * Here we try to reset everything back to a state we had
1163 * before suspending.
1166 static int i8042_controller_resume(bool s2r_wants_reset)
1170 error = i8042_controller_check();
1174 if (i8042_reset == I8042_RESET_ALWAYS ||
1175 (i8042_reset == I8042_RESET_ON_S2RAM && s2r_wants_reset)) {
1176 error = i8042_controller_selftest();
1182 * Restore original CTR value and disable all ports
1185 i8042_ctr = i8042_initial_ctr;
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");
1193 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
1194 pr_err("CTR write retry failed\n");
1202 i8042_dritek_enable();
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();
1212 if (i8042_ports[I8042_KBD_PORT_NO].serio)
1213 i8042_enable_kbd_port();
1215 i8042_handle_data(0);
1221 * Here we try to restore the original BIOS settings to avoid
1225 static int i8042_pm_suspend(struct device *dev)
1229 if (!i8042_forcenorestore && pm_suspend_via_firmware())
1230 i8042_controller_reset(true);
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;
1236 if (serio && device_may_wakeup(&serio->dev))
1237 enable_irq_wake(i8042_ports[i].irq);
1243 static int i8042_pm_resume_noirq(struct device *dev)
1245 if (i8042_forcenorestore || !pm_resume_via_firmware())
1246 i8042_handle_data(0);
1251 static int i8042_pm_resume(struct device *dev)
1256 for (i = 0; i < I8042_NUM_PORTS; i++) {
1257 struct serio *serio = i8042_ports[i].serio;
1259 if (serio && device_may_wakeup(&serio->dev))
1260 disable_irq_wake(i8042_ports[i].irq);
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.
1268 if (i8042_forcenorestore || !pm_suspend_via_firmware())
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
1276 want_reset = pm_resume_via_firmware();
1278 return i8042_controller_resume(want_reset);
1281 static int i8042_pm_thaw(struct device *dev)
1283 i8042_handle_data(0);
1288 static int i8042_pm_reset(struct device *dev)
1290 i8042_controller_reset(false);
1295 static int i8042_pm_restore(struct device *dev)
1297 return i8042_controller_resume(false);
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,
1309 #endif /* CONFIG_PM */
1312 * We need to reset the 8042 back to original mode on system shutdown,
1313 * because otherwise BIOSes will be confused.
1316 static void i8042_shutdown(struct platform_device *dev)
1318 i8042_controller_reset(false);
1321 static int i8042_create_kbd_port(void)
1323 struct serio *serio;
1324 struct i8042_port *port = &i8042_ports[I8042_KBD_PORT_NO];
1326 serio = kzalloc(sizeof(*serio), GFP_KERNEL);
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);
1344 port->serio = serio;
1345 port->irq = I8042_KBD_IRQ;
1350 static int i8042_create_aux_port(int idx)
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];
1356 serio = kzalloc(sizeof(*serio), GFP_KERNEL);
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;
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;
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));
1380 port->serio = serio;
1382 port->irq = I8042_AUX_IRQ;
1387 static void i8042_free_kbd_port(void)
1389 kfree(i8042_ports[I8042_KBD_PORT_NO].serio);
1390 i8042_ports[I8042_KBD_PORT_NO].serio = NULL;
1393 static void i8042_free_aux_ports(void)
1397 for (i = I8042_AUX_PORT_NO; i < I8042_NUM_PORTS; i++) {
1398 kfree(i8042_ports[i].serio);
1399 i8042_ports[i].serio = NULL;
1403 static void i8042_register_ports(void)
1407 for (i = 0; i < I8042_NUM_PORTS; i++) {
1408 struct serio *serio = i8042_ports[i].serio;
1413 printk(KERN_INFO "serio: %s at %#lx,%#lx irq %d\n",
1415 (unsigned long) I8042_DATA_REG,
1416 (unsigned long) I8042_COMMAND_REG,
1417 i8042_ports[i].irq);
1418 serio_register_port(serio);
1422 static void i8042_unregister_ports(void)
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;
1434 static void i8042_free_irqs(void)
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);
1441 i8042_aux_irq_registered = i8042_kbd_irq_registered = false;
1444 static int i8042_setup_aux(void)
1446 int (*aux_enable)(void);
1450 if (i8042_check_aux())
1453 if (i8042_nomux || i8042_check_mux()) {
1454 error = i8042_create_aux_port(-1);
1456 goto err_free_ports;
1457 aux_enable = i8042_enable_aux_port;
1459 for (i = 0; i < I8042_NUM_MUX_PORTS; i++) {
1460 error = i8042_create_aux_port(i);
1462 goto err_free_ports;
1464 aux_enable = i8042_enable_mux_ports;
1467 error = request_irq(I8042_AUX_IRQ, i8042_interrupt, IRQF_SHARED,
1468 "i8042", i8042_platform_device);
1470 goto err_free_ports;
1472 error = aux_enable();
1476 i8042_aux_irq_registered = true;
1480 free_irq(I8042_AUX_IRQ, i8042_platform_device);
1482 i8042_free_aux_ports();
1486 static int i8042_setup_kbd(void)
1490 error = i8042_create_kbd_port();
1494 error = request_irq(I8042_KBD_IRQ, i8042_interrupt, IRQF_SHARED,
1495 "i8042", i8042_platform_device);
1499 error = i8042_enable_kbd_port();
1503 i8042_kbd_irq_registered = true;
1507 free_irq(I8042_KBD_IRQ, i8042_platform_device);
1509 i8042_free_kbd_port();
1513 static int i8042_kbd_bind_notifier(struct notifier_block *nb,
1514 unsigned long action, void *data)
1516 struct device *dev = data;
1517 struct serio *serio = to_serio_port(dev);
1518 struct i8042_port *port = serio->port_data;
1520 if (serio != i8042_ports[I8042_KBD_PORT_NO].serio)
1524 case BUS_NOTIFY_BOUND_DRIVER:
1525 port->driver_bound = true;
1528 case BUS_NOTIFY_UNBIND_DRIVER:
1529 port->driver_bound = false;
1536 static int i8042_probe(struct platform_device *dev)
1540 if (i8042_reset == I8042_RESET_ALWAYS) {
1541 error = i8042_controller_selftest();
1546 error = i8042_controller_init();
1552 i8042_dritek_enable();
1556 error = i8042_setup_aux();
1557 if (error && error != -ENODEV && error != -EBUSY)
1562 error = i8042_setup_kbd();
1567 * Ok, everything is ready, let's register all serio ports
1569 i8042_register_ports();
1574 i8042_free_aux_ports(); /* in case KBD failed but AUX not */
1576 i8042_controller_reset(false);
1581 static void i8042_remove(struct platform_device *dev)
1583 i8042_unregister_ports();
1585 i8042_controller_reset(false);
1588 static struct platform_driver i8042_driver = {
1592 .pm = &i8042_pm_ops,
1595 .probe = i8042_probe,
1596 .remove = i8042_remove,
1597 .shutdown = i8042_shutdown,
1600 static struct notifier_block i8042_kbd_bind_notifier_block = {
1601 .notifier_call = i8042_kbd_bind_notifier,
1604 static int __init i8042_init(void)
1610 err = i8042_platform_init();
1612 return (err == -ENODEV) ? 0 : err;
1614 err = i8042_controller_check();
1616 goto err_platform_exit;
1618 /* Set this before creating the dev to allow i8042_command to work right away */
1619 i8042_present = true;
1621 err = platform_driver_register(&i8042_driver);
1623 goto err_platform_exit;
1625 i8042_platform_device = platform_device_alloc("i8042", -1);
1626 if (!i8042_platform_device) {
1628 goto err_unregister_driver;
1631 err = platform_device_add(i8042_platform_device);
1633 goto err_free_device;
1635 bus_register_notifier(&serio_bus, &i8042_kbd_bind_notifier_block);
1636 panic_blink = i8042_panic_blink;
1641 platform_device_put(i8042_platform_device);
1642 err_unregister_driver:
1643 platform_driver_unregister(&i8042_driver);
1645 i8042_platform_exit();
1649 static void __exit i8042_exit(void)
1654 platform_device_unregister(i8042_platform_device);
1655 platform_driver_unregister(&i8042_driver);
1656 i8042_platform_exit();
1658 bus_unregister_notifier(&serio_bus, &i8042_kbd_bind_notifier_block);
1662 module_init(i8042_init);
1663 module_exit(i8042_exit);