2 * i8042 keyboard and mouse controller driver for Linux
4 * Copyright (c) 1999-2004 Vojtech Pavlik
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License version 2 as published by
10 * the Free Software Foundation.
13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 #include <linux/types.h>
16 #include <linux/delay.h>
17 #include <linux/module.h>
18 #include <linux/interrupt.h>
19 #include <linux/ioport.h>
20 #include <linux/init.h>
21 #include <linux/serio.h>
22 #include <linux/err.h>
23 #include <linux/rcupdate.h>
24 #include <linux/platform_device.h>
25 #include <linux/i8042.h>
26 #include <linux/slab.h>
27 #include <linux/suspend.h>
31 MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
32 MODULE_DESCRIPTION("i8042 keyboard and mouse controller driver");
33 MODULE_LICENSE("GPL");
35 static bool i8042_nokbd;
36 module_param_named(nokbd, i8042_nokbd, bool, 0);
37 MODULE_PARM_DESC(nokbd, "Do not probe or use KBD port.");
39 static bool i8042_noaux;
40 module_param_named(noaux, i8042_noaux, bool, 0);
41 MODULE_PARM_DESC(noaux, "Do not probe or use AUX (mouse) port.");
43 static bool i8042_nomux;
44 module_param_named(nomux, i8042_nomux, bool, 0);
45 MODULE_PARM_DESC(nomux, "Do not check whether an active multiplexing controller is present.");
47 static bool i8042_unlock;
48 module_param_named(unlock, i8042_unlock, bool, 0);
49 MODULE_PARM_DESC(unlock, "Ignore keyboard lock.");
51 enum i8042_controller_reset_mode {
55 #define I8042_RESET_DEFAULT I8042_RESET_ON_S2RAM
57 static enum i8042_controller_reset_mode i8042_reset = I8042_RESET_DEFAULT;
58 static int i8042_set_reset(const char *val, const struct kernel_param *kp)
60 enum i8042_controller_reset_mode *arg = kp->arg;
65 error = kstrtobool(val, &reset);
72 *arg = reset ? I8042_RESET_ALWAYS : I8042_RESET_NEVER;
76 static const struct kernel_param_ops param_ops_reset_param = {
77 .flags = KERNEL_PARAM_OPS_FL_NOARG,
78 .set = i8042_set_reset,
80 #define param_check_reset_param(name, p) \
81 __param_check(name, p, enum i8042_controller_reset_mode)
82 module_param_named(reset, i8042_reset, reset_param, 0);
83 MODULE_PARM_DESC(reset, "Reset controller on resume, cleanup or both");
85 static bool i8042_direct;
86 module_param_named(direct, i8042_direct, bool, 0);
87 MODULE_PARM_DESC(direct, "Put keyboard port into non-translated mode.");
89 static bool i8042_dumbkbd;
90 module_param_named(dumbkbd, i8042_dumbkbd, bool, 0);
91 MODULE_PARM_DESC(dumbkbd, "Pretend that controller can only read data from keyboard");
93 static bool i8042_noloop;
94 module_param_named(noloop, i8042_noloop, bool, 0);
95 MODULE_PARM_DESC(noloop, "Disable the AUX Loopback command while probing for the AUX port");
97 static bool i8042_notimeout;
98 module_param_named(notimeout, i8042_notimeout, bool, 0);
99 MODULE_PARM_DESC(notimeout, "Ignore timeouts signalled by i8042");
101 static bool i8042_kbdreset;
102 module_param_named(kbdreset, i8042_kbdreset, bool, 0);
103 MODULE_PARM_DESC(kbdreset, "Reset device connected to KBD port");
106 static bool i8042_dritek;
107 module_param_named(dritek, i8042_dritek, bool, 0);
108 MODULE_PARM_DESC(dritek, "Force enable the Dritek keyboard extension");
112 static bool i8042_nopnp;
113 module_param_named(nopnp, i8042_nopnp, bool, 0);
114 MODULE_PARM_DESC(nopnp, "Do not use PNP to detect controller settings");
119 static bool i8042_debug;
120 module_param_named(debug, i8042_debug, bool, 0600);
121 MODULE_PARM_DESC(debug, "Turn i8042 debugging mode on and off");
123 static bool i8042_unmask_kbd_data;
124 module_param_named(unmask_kbd_data, i8042_unmask_kbd_data, bool, 0600);
125 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]");
128 static bool i8042_present;
129 static bool i8042_bypass_aux_irq_test;
130 static char i8042_kbd_firmware_id[128];
131 static char i8042_aux_firmware_id[128];
136 * i8042_lock protects serialization between i8042_command and
137 * the interrupt handler.
139 static DEFINE_SPINLOCK(i8042_lock);
142 * Writers to AUX and KBD ports as well as users issuing i8042_command
143 * directly should acquire i8042_mutex (by means of calling
144 * i8042_lock_chip() and i8042_unlock_ship() helpers) to ensure that
145 * they do not disturb each other (unfortunately in many i8042
146 * implementations write to one of the ports will immediately abort
147 * command that is being processed by another port).
149 static DEFINE_MUTEX(i8042_mutex);
159 #define I8042_KBD_PORT_NO 0
160 #define I8042_AUX_PORT_NO 1
161 #define I8042_MUX_PORT_NO 2
162 #define I8042_NUM_PORTS (I8042_NUM_MUX_PORTS + 2)
164 static struct i8042_port i8042_ports[I8042_NUM_PORTS];
166 static unsigned char i8042_initial_ctr;
167 static unsigned char i8042_ctr;
168 static bool i8042_mux_present;
169 static bool i8042_kbd_irq_registered;
170 static bool i8042_aux_irq_registered;
171 static unsigned char i8042_suppress_kbd_ack;
172 static struct platform_device *i8042_platform_device;
173 static struct notifier_block i8042_kbd_bind_notifier_block;
175 static irqreturn_t i8042_interrupt(int irq, void *dev_id);
176 static bool (*i8042_platform_filter)(unsigned char data, unsigned char str,
177 struct serio *serio);
179 void i8042_lock_chip(void)
181 mutex_lock(&i8042_mutex);
183 EXPORT_SYMBOL(i8042_lock_chip);
185 void i8042_unlock_chip(void)
187 mutex_unlock(&i8042_mutex);
189 EXPORT_SYMBOL(i8042_unlock_chip);
191 int i8042_install_filter(bool (*filter)(unsigned char data, unsigned char str,
192 struct serio *serio))
197 spin_lock_irqsave(&i8042_lock, flags);
199 if (i8042_platform_filter) {
204 i8042_platform_filter = filter;
207 spin_unlock_irqrestore(&i8042_lock, flags);
210 EXPORT_SYMBOL(i8042_install_filter);
212 int i8042_remove_filter(bool (*filter)(unsigned char data, unsigned char str,
218 spin_lock_irqsave(&i8042_lock, flags);
220 if (i8042_platform_filter != filter) {
225 i8042_platform_filter = NULL;
228 spin_unlock_irqrestore(&i8042_lock, flags);
231 EXPORT_SYMBOL(i8042_remove_filter);
234 * The i8042_wait_read() and i8042_wait_write functions wait for the i8042 to
235 * be ready for reading values from it / writing values to it.
236 * Called always with i8042_lock held.
239 static int i8042_wait_read(void)
243 while ((~i8042_read_status() & I8042_STR_OBF) && (i < I8042_CTL_TIMEOUT)) {
247 return -(i == I8042_CTL_TIMEOUT);
250 static int i8042_wait_write(void)
254 while ((i8042_read_status() & I8042_STR_IBF) && (i < I8042_CTL_TIMEOUT)) {
258 return -(i == I8042_CTL_TIMEOUT);
262 * i8042_flush() flushes all data that may be in the keyboard and mouse buffers
263 * of the i8042 down the toilet.
266 static int i8042_flush(void)
269 unsigned char data, str;
273 spin_lock_irqsave(&i8042_lock, flags);
275 while ((str = i8042_read_status()) & I8042_STR_OBF) {
276 if (count++ < I8042_BUFFER_SIZE) {
278 data = i8042_read_data();
279 dbg("%02x <- i8042 (flush, %s)\n",
280 data, str & I8042_STR_AUXDATA ? "aux" : "kbd");
287 spin_unlock_irqrestore(&i8042_lock, flags);
293 * i8042_command() executes a command on the i8042. It also sends the input
294 * parameter(s) of the commands to it, and receives the output value(s). The
295 * parameters are to be stored in the param array, and the output is placed
296 * into the same array. The number of the parameters and output values is
297 * encoded in bits 8-11 of the command number.
300 static int __i8042_command(unsigned char *param, int command)
304 if (i8042_noloop && command == I8042_CMD_AUX_LOOP)
307 error = i8042_wait_write();
311 dbg("%02x -> i8042 (command)\n", command & 0xff);
312 i8042_write_command(command & 0xff);
314 for (i = 0; i < ((command >> 12) & 0xf); i++) {
315 error = i8042_wait_write();
317 dbg(" -- i8042 (wait write timeout)\n");
320 dbg("%02x -> i8042 (parameter)\n", param[i]);
321 i8042_write_data(param[i]);
324 for (i = 0; i < ((command >> 8) & 0xf); i++) {
325 error = i8042_wait_read();
327 dbg(" -- i8042 (wait read timeout)\n");
331 if (command == I8042_CMD_AUX_LOOP &&
332 !(i8042_read_status() & I8042_STR_AUXDATA)) {
333 dbg(" -- i8042 (auxerr)\n");
337 param[i] = i8042_read_data();
338 dbg("%02x <- i8042 (return)\n", param[i]);
344 int i8042_command(unsigned char *param, int command)
352 spin_lock_irqsave(&i8042_lock, flags);
353 retval = __i8042_command(param, command);
354 spin_unlock_irqrestore(&i8042_lock, flags);
358 EXPORT_SYMBOL(i8042_command);
361 * i8042_kbd_write() sends a byte out through the keyboard interface.
364 static int i8042_kbd_write(struct serio *port, unsigned char c)
369 spin_lock_irqsave(&i8042_lock, flags);
371 if (!(retval = i8042_wait_write())) {
372 dbg("%02x -> i8042 (kbd-data)\n", c);
376 spin_unlock_irqrestore(&i8042_lock, flags);
382 * i8042_aux_write() sends a byte out through the aux interface.
385 static int i8042_aux_write(struct serio *serio, unsigned char c)
387 struct i8042_port *port = serio->port_data;
389 return i8042_command(&c, port->mux == -1 ?
391 I8042_CMD_MUX_SEND + port->mux);
396 * i8042_port_close attempts to clear AUX or KBD port state by disabling
397 * and then re-enabling it.
400 static void i8042_port_close(struct serio *serio)
404 const char *port_name;
406 if (serio == i8042_ports[I8042_AUX_PORT_NO].serio) {
407 irq_bit = I8042_CTR_AUXINT;
408 disable_bit = I8042_CTR_AUXDIS;
411 irq_bit = I8042_CTR_KBDINT;
412 disable_bit = I8042_CTR_KBDDIS;
416 i8042_ctr &= ~irq_bit;
417 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR))
418 pr_warn("Can't write CTR while closing %s port\n", port_name);
422 i8042_ctr &= ~disable_bit;
423 i8042_ctr |= irq_bit;
424 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR))
425 pr_err("Can't reactivate %s port\n", port_name);
428 * See if there is any data appeared while we were messing with
431 i8042_interrupt(0, NULL);
435 * i8042_start() is called by serio core when port is about to finish
436 * registering. It will mark port as existing so i8042_interrupt can
437 * start sending data through it.
439 static int i8042_start(struct serio *serio)
441 struct i8042_port *port = serio->port_data;
443 spin_lock_irq(&i8042_lock);
445 spin_unlock_irq(&i8042_lock);
451 * i8042_stop() marks serio port as non-existing so i8042_interrupt
452 * will not try to send data to the port that is about to go away.
453 * The function is called by serio core as part of unregister procedure.
455 static void i8042_stop(struct serio *serio)
457 struct i8042_port *port = serio->port_data;
459 spin_lock_irq(&i8042_lock);
460 port->exists = false;
462 spin_unlock_irq(&i8042_lock);
465 * We need to make sure that interrupt handler finishes using
466 * our serio port before we return from this function.
467 * We synchronize with both AUX and KBD IRQs because there is
468 * a (very unlikely) chance that AUX IRQ is raised for KBD port
471 synchronize_irq(I8042_AUX_IRQ);
472 synchronize_irq(I8042_KBD_IRQ);
476 * i8042_filter() filters out unwanted bytes from the input data stream.
477 * It is called from i8042_interrupt and thus is running with interrupts
478 * off and i8042_lock held.
480 static bool i8042_filter(unsigned char data, unsigned char str,
483 if (unlikely(i8042_suppress_kbd_ack)) {
484 if ((~str & I8042_STR_AUXDATA) &&
485 (data == 0xfa || data == 0xfe)) {
486 i8042_suppress_kbd_ack--;
487 dbg("Extra keyboard ACK - filtered out\n");
492 if (i8042_platform_filter && i8042_platform_filter(data, str, serio)) {
493 dbg("Filtered out by platform filter\n");
501 * i8042_interrupt() is the most important function in this driver -
502 * it handles the interrupts from the i8042, and sends incoming bytes
503 * to the upper layers.
506 static irqreturn_t i8042_interrupt(int irq, void *dev_id)
508 struct i8042_port *port;
511 unsigned char str, data;
513 unsigned int port_no;
517 spin_lock_irqsave(&i8042_lock, flags);
519 str = i8042_read_status();
520 if (unlikely(~str & I8042_STR_OBF)) {
521 spin_unlock_irqrestore(&i8042_lock, flags);
523 dbg("Interrupt %d, without any data\n", irq);
528 data = i8042_read_data();
530 if (i8042_mux_present && (str & I8042_STR_AUXDATA)) {
531 static unsigned long last_transmit;
532 static unsigned char last_str;
535 if (str & I8042_STR_MUXERR) {
536 dbg("MUX error, status is %02x, data is %02x\n",
539 * When MUXERR condition is signalled the data register can only contain
540 * 0xfd, 0xfe or 0xff if implementation follows the spec. Unfortunately
541 * it is not always the case. Some KBCs also report 0xfc when there is
542 * nothing connected to the port while others sometimes get confused which
543 * port the data came from and signal error leaving the data intact. They
544 * _do not_ revert to legacy mode (actually I've never seen KBC reverting
545 * to legacy mode yet, when we see one we'll add proper handling).
546 * Anyway, we process 0xfc, 0xfd, 0xfe and 0xff as timeouts, and for the
547 * rest assume that the data came from the same serio last byte
548 * was transmitted (if transmission happened not too long ago).
553 if (time_before(jiffies, last_transmit + HZ/10)) {
557 /* fall through - report timeout */
560 case 0xfe: dfl = SERIO_TIMEOUT; data = 0xfe; break;
561 case 0xff: dfl = SERIO_PARITY; data = 0xfe; break;
565 port_no = I8042_MUX_PORT_NO + ((str >> 6) & 3);
567 last_transmit = jiffies;
570 dfl = ((str & I8042_STR_PARITY) ? SERIO_PARITY : 0) |
571 ((str & I8042_STR_TIMEOUT && !i8042_notimeout) ? SERIO_TIMEOUT : 0);
573 port_no = (str & I8042_STR_AUXDATA) ?
574 I8042_AUX_PORT_NO : I8042_KBD_PORT_NO;
577 port = &i8042_ports[port_no];
578 serio = port->exists ? port->serio : NULL;
581 pm_wakeup_event(&serio->dev, 0);
583 filter_dbg(port->driver_bound, data, "<- i8042 (interrupt, %d, %d%s%s)\n",
585 dfl & SERIO_PARITY ? ", bad parity" : "",
586 dfl & SERIO_TIMEOUT ? ", timeout" : "");
588 filtered = i8042_filter(data, str, serio);
590 spin_unlock_irqrestore(&i8042_lock, flags);
592 if (likely(serio && !filtered))
593 serio_interrupt(serio, data, dfl);
596 return IRQ_RETVAL(ret);
600 * i8042_enable_kbd_port enables keyboard port on chip
603 static int i8042_enable_kbd_port(void)
605 i8042_ctr &= ~I8042_CTR_KBDDIS;
606 i8042_ctr |= I8042_CTR_KBDINT;
608 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
609 i8042_ctr &= ~I8042_CTR_KBDINT;
610 i8042_ctr |= I8042_CTR_KBDDIS;
611 pr_err("Failed to enable KBD port\n");
619 * i8042_enable_aux_port enables AUX (mouse) port on chip
622 static int i8042_enable_aux_port(void)
624 i8042_ctr &= ~I8042_CTR_AUXDIS;
625 i8042_ctr |= I8042_CTR_AUXINT;
627 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
628 i8042_ctr &= ~I8042_CTR_AUXINT;
629 i8042_ctr |= I8042_CTR_AUXDIS;
630 pr_err("Failed to enable AUX port\n");
638 * i8042_enable_mux_ports enables 4 individual AUX ports after
639 * the controller has been switched into Multiplexed mode
642 static int i8042_enable_mux_ports(void)
647 for (i = 0; i < I8042_NUM_MUX_PORTS; i++) {
648 i8042_command(¶m, I8042_CMD_MUX_PFX + i);
649 i8042_command(¶m, I8042_CMD_AUX_ENABLE);
652 return i8042_enable_aux_port();
656 * i8042_set_mux_mode checks whether the controller has an
657 * active multiplexor and puts the chip into Multiplexed (true)
658 * or Legacy (false) mode.
661 static int i8042_set_mux_mode(bool multiplex, unsigned char *mux_version)
664 unsigned char param, val;
666 * Get rid of bytes in the queue.
672 * Internal loopback test - send three bytes, they should come back from the
673 * mouse interface, the last should be version.
677 if (i8042_command(¶m, I8042_CMD_AUX_LOOP) || param != val)
679 param = val = multiplex ? 0x56 : 0xf6;
680 if (i8042_command(¶m, I8042_CMD_AUX_LOOP) || param != val)
682 param = val = multiplex ? 0xa4 : 0xa5;
683 if (i8042_command(¶m, I8042_CMD_AUX_LOOP) || param == val)
687 * Workaround for interference with USB Legacy emulation
688 * that causes a v10.12 MUX to be found.
694 *mux_version = param;
700 * i8042_check_mux() checks whether the controller supports the PS/2 Active
701 * Multiplexing specification by Synaptics, Phoenix, Insyde and
705 static int __init i8042_check_mux(void)
707 unsigned char mux_version;
709 if (i8042_set_mux_mode(true, &mux_version))
712 pr_info("Detected active multiplexing controller, rev %d.%d\n",
713 (mux_version >> 4) & 0xf, mux_version & 0xf);
716 * Disable all muxed ports by disabling AUX.
718 i8042_ctr |= I8042_CTR_AUXDIS;
719 i8042_ctr &= ~I8042_CTR_AUXINT;
721 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
722 pr_err("Failed to disable AUX port, can't use MUX\n");
726 i8042_mux_present = true;
732 * The following is used to test AUX IRQ delivery.
734 static struct completion i8042_aux_irq_delivered __initdata;
735 static bool i8042_irq_being_tested __initdata;
737 static irqreturn_t __init i8042_aux_test_irq(int irq, void *dev_id)
740 unsigned char str, data;
743 spin_lock_irqsave(&i8042_lock, flags);
744 str = i8042_read_status();
745 if (str & I8042_STR_OBF) {
746 data = i8042_read_data();
747 dbg("%02x <- i8042 (aux_test_irq, %s)\n",
748 data, str & I8042_STR_AUXDATA ? "aux" : "kbd");
749 if (i8042_irq_being_tested &&
750 data == 0xa5 && (str & I8042_STR_AUXDATA))
751 complete(&i8042_aux_irq_delivered);
754 spin_unlock_irqrestore(&i8042_lock, flags);
756 return IRQ_RETVAL(ret);
760 * i8042_toggle_aux - enables or disables AUX port on i8042 via command and
761 * verifies success by readinng CTR. Used when testing for presence of AUX
764 static int __init i8042_toggle_aux(bool on)
769 if (i8042_command(¶m,
770 on ? I8042_CMD_AUX_ENABLE : I8042_CMD_AUX_DISABLE))
773 /* some chips need some time to set the I8042_CTR_AUXDIS bit */
774 for (i = 0; i < 100; i++) {
777 if (i8042_command(¶m, I8042_CMD_CTL_RCTR))
780 if (!(param & I8042_CTR_AUXDIS) == on)
788 * i8042_check_aux() applies as much paranoia as it can at detecting
789 * the presence of an AUX interface.
792 static int __init i8042_check_aux(void)
795 bool irq_registered = false;
796 bool aux_loop_broken = false;
801 * Get rid of bytes in the queue.
807 * Internal loopback test - filters out AT-type i8042's. Unfortunately
808 * SiS screwed up and their 5597 doesn't support the LOOP command even
809 * though it has an AUX port.
813 retval = i8042_command(¶m, I8042_CMD_AUX_LOOP);
814 if (retval || param != 0x5a) {
817 * External connection test - filters out AT-soldered PS/2 i8042's
818 * 0x00 - no error, 0x01-0x03 - clock/data stuck, 0xff - general error
819 * 0xfa - no error on some notebooks which ignore the spec
820 * Because it's common for chipsets to return error on perfectly functioning
821 * AUX ports, we test for this only when the LOOP command failed.
824 if (i8042_command(¶m, I8042_CMD_AUX_TEST) ||
825 (param && param != 0xfa && param != 0xff))
829 * If AUX_LOOP completed without error but returned unexpected data
833 aux_loop_broken = true;
837 * Bit assignment test - filters out PS/2 i8042's in AT mode
840 if (i8042_toggle_aux(false)) {
841 pr_warn("Failed to disable AUX port, but continuing anyway... Is this a SiS?\n");
842 pr_warn("If AUX port is really absent please use the 'i8042.noaux' option\n");
845 if (i8042_toggle_aux(true))
849 * Reset keyboard (needed on some laptops to successfully detect
850 * touchpad, e.g., some Gigabyte laptop models with Elantech
853 if (i8042_kbdreset) {
854 pr_warn("Attempting to reset device connected to KBD port\n");
855 i8042_kbd_write(NULL, (unsigned char) 0xff);
859 * Test AUX IRQ delivery to make sure BIOS did not grab the IRQ and
860 * used it for a PCI card or somethig else.
863 if (i8042_noloop || i8042_bypass_aux_irq_test || aux_loop_broken) {
865 * Without LOOP command we can't test AUX IRQ delivery. Assume the port
866 * is working and hope we are right.
872 if (request_irq(I8042_AUX_IRQ, i8042_aux_test_irq, IRQF_SHARED,
873 "i8042", i8042_platform_device))
876 irq_registered = true;
878 if (i8042_enable_aux_port())
881 spin_lock_irqsave(&i8042_lock, flags);
883 init_completion(&i8042_aux_irq_delivered);
884 i8042_irq_being_tested = true;
887 retval = __i8042_command(¶m, I8042_CMD_AUX_LOOP & 0xf0ff);
889 spin_unlock_irqrestore(&i8042_lock, flags);
894 if (wait_for_completion_timeout(&i8042_aux_irq_delivered,
895 msecs_to_jiffies(250)) == 0) {
897 * AUX IRQ was never delivered so we need to flush the controller to
898 * get rid of the byte we put there; otherwise keyboard may not work.
900 dbg(" -- i8042 (aux irq test timeout)\n");
908 * Disable the interface.
911 i8042_ctr |= I8042_CTR_AUXDIS;
912 i8042_ctr &= ~I8042_CTR_AUXINT;
914 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR))
918 free_irq(I8042_AUX_IRQ, i8042_platform_device);
923 static int i8042_controller_check(void)
926 pr_info("No controller found\n");
933 static int i8042_controller_selftest(void)
939 * We try this 5 times; on some really fragile systems this does not
940 * take the first time...
944 if (i8042_command(¶m, I8042_CMD_CTL_TEST)) {
945 pr_err("i8042 controller selftest timeout\n");
949 if (param == I8042_RET_CTL_TEST)
952 dbg("i8042 controller selftest: %#x != %#x\n",
953 param, I8042_RET_CTL_TEST);
959 * On x86, we don't fail entire i8042 initialization if controller
960 * reset fails in hopes that keyboard port will still be functional
961 * and user will still get a working keyboard. This is especially
962 * important on netbooks. On other arches we trust hardware more.
964 pr_info("giving up on controller selftest, continuing anyway...\n");
967 pr_err("i8042 controller selftest failed\n");
973 * i8042_controller init initializes the i8042 controller, and,
974 * most importantly, sets it into non-xlated mode if that's
978 static int i8042_controller_init(void)
982 unsigned char ctr[2];
985 * Save the CTR for restore on unload / reboot.
990 pr_err("Unable to get stable CTR read\n");
997 if (i8042_command(&ctr[n++ % 2], I8042_CMD_CTL_RCTR)) {
998 pr_err("Can't read CTR while initializing i8042\n");
1002 } while (n < 2 || ctr[0] != ctr[1]);
1004 i8042_initial_ctr = i8042_ctr = ctr[0];
1007 * Disable the keyboard interface and interrupt.
1010 i8042_ctr |= I8042_CTR_KBDDIS;
1011 i8042_ctr &= ~I8042_CTR_KBDINT;
1017 spin_lock_irqsave(&i8042_lock, flags);
1018 if (~i8042_read_status() & I8042_STR_KEYLOCK) {
1020 i8042_ctr |= I8042_CTR_IGNKEYLOCK;
1022 pr_warn("Warning: Keylock active\n");
1024 spin_unlock_irqrestore(&i8042_lock, flags);
1027 * If the chip is configured into nontranslated mode by the BIOS, don't
1028 * bother enabling translating and be happy.
1031 if (~i8042_ctr & I8042_CTR_XLATE)
1032 i8042_direct = true;
1035 * Set nontranslated mode for the kbd interface if requested by an option.
1036 * After this the kbd interface becomes a simple serial in/out, like the aux
1037 * interface is. We don't do this by default, since it can confuse notebook
1042 i8042_ctr &= ~I8042_CTR_XLATE;
1048 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
1049 pr_err("Can't write CTR while initializing i8042\n");
1054 * Flush whatever accumulated while we were disabling keyboard port.
1064 * Reset the controller and reset CRT to the original value set by BIOS.
1067 static void i8042_controller_reset(bool s2r_wants_reset)
1072 * Disable both KBD and AUX interfaces so they don't get in the way
1075 i8042_ctr |= I8042_CTR_KBDDIS | I8042_CTR_AUXDIS;
1076 i8042_ctr &= ~(I8042_CTR_KBDINT | I8042_CTR_AUXINT);
1078 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR))
1079 pr_warn("Can't write CTR while resetting\n");
1082 * Disable MUX mode if present.
1085 if (i8042_mux_present)
1086 i8042_set_mux_mode(false, NULL);
1089 * Reset the controller if requested.
1092 if (i8042_reset == I8042_RESET_ALWAYS ||
1093 (i8042_reset == I8042_RESET_ON_S2RAM && s2r_wants_reset)) {
1094 i8042_controller_selftest();
1098 * Restore the original control register setting.
1101 if (i8042_command(&i8042_initial_ctr, I8042_CMD_CTL_WCTR))
1102 pr_warn("Can't restore CTR\n");
1107 * i8042_panic_blink() will turn the keyboard LEDs on or off and is called
1108 * when kernel panics. Flashing LEDs is useful for users running X who may
1109 * not see the console and will help distinguishing panics from "real"
1112 * Note that DELAY has a limit of 10ms so we will not get stuck here
1113 * waiting for KBC to free up even if KBD interrupt is off
1116 #define DELAY do { mdelay(1); if (++delay > 10) return delay; } while(0)
1118 static long i8042_panic_blink(int state)
1123 led = (state) ? 0x01 | 0x04 : 0;
1124 while (i8042_read_status() & I8042_STR_IBF)
1126 dbg("%02x -> i8042 (panic blink)\n", 0xed);
1127 i8042_suppress_kbd_ack = 2;
1128 i8042_write_data(0xed); /* set leds */
1130 while (i8042_read_status() & I8042_STR_IBF)
1133 dbg("%02x -> i8042 (panic blink)\n", led);
1134 i8042_write_data(led);
1142 static void i8042_dritek_enable(void)
1144 unsigned char param = 0x90;
1147 error = i8042_command(¶m, 0x1059);
1149 pr_warn("Failed to enable DRITEK extension: %d\n", error);
1156 * Here we try to reset everything back to a state we had
1157 * before suspending.
1160 static int i8042_controller_resume(bool s2r_wants_reset)
1164 error = i8042_controller_check();
1168 if (i8042_reset == I8042_RESET_ALWAYS ||
1169 (i8042_reset == I8042_RESET_ON_S2RAM && s2r_wants_reset)) {
1170 error = i8042_controller_selftest();
1176 * Restore original CTR value and disable all ports
1179 i8042_ctr = i8042_initial_ctr;
1181 i8042_ctr &= ~I8042_CTR_XLATE;
1182 i8042_ctr |= I8042_CTR_AUXDIS | I8042_CTR_KBDDIS;
1183 i8042_ctr &= ~(I8042_CTR_AUXINT | I8042_CTR_KBDINT);
1184 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
1185 pr_warn("Can't write CTR to resume, retrying...\n");
1187 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
1188 pr_err("CTR write retry failed\n");
1196 i8042_dritek_enable();
1199 if (i8042_mux_present) {
1200 if (i8042_set_mux_mode(true, NULL) || i8042_enable_mux_ports())
1201 pr_warn("failed to resume active multiplexor, mouse won't work\n");
1202 } else if (i8042_ports[I8042_AUX_PORT_NO].serio)
1203 i8042_enable_aux_port();
1205 if (i8042_ports[I8042_KBD_PORT_NO].serio)
1206 i8042_enable_kbd_port();
1208 i8042_interrupt(0, NULL);
1214 * Here we try to restore the original BIOS settings to avoid
1218 static int i8042_pm_suspend(struct device *dev)
1222 if (pm_suspend_via_firmware())
1223 i8042_controller_reset(true);
1225 /* Set up serio interrupts for system wakeup. */
1226 for (i = 0; i < I8042_NUM_PORTS; i++) {
1227 struct serio *serio = i8042_ports[i].serio;
1229 if (serio && device_may_wakeup(&serio->dev))
1230 enable_irq_wake(i8042_ports[i].irq);
1236 static int i8042_pm_resume_noirq(struct device *dev)
1238 if (!pm_resume_via_firmware())
1239 i8042_interrupt(0, NULL);
1244 static int i8042_pm_resume(struct device *dev)
1249 for (i = 0; i < I8042_NUM_PORTS; i++) {
1250 struct serio *serio = i8042_ports[i].serio;
1252 if (serio && device_may_wakeup(&serio->dev))
1253 disable_irq_wake(i8042_ports[i].irq);
1257 * If platform firmware was not going to be involved in suspend, we did
1258 * not restore the controller state to whatever it had been at boot
1259 * time, so we do not need to do anything.
1261 if (!pm_suspend_via_firmware())
1265 * We only need to reset the controller if we are resuming after handing
1266 * off control to the platform firmware, otherwise we can simply restore
1269 want_reset = pm_resume_via_firmware();
1271 return i8042_controller_resume(want_reset);
1274 static int i8042_pm_thaw(struct device *dev)
1276 i8042_interrupt(0, NULL);
1281 static int i8042_pm_reset(struct device *dev)
1283 i8042_controller_reset(false);
1288 static int i8042_pm_restore(struct device *dev)
1290 return i8042_controller_resume(false);
1293 static const struct dev_pm_ops i8042_pm_ops = {
1294 .suspend = i8042_pm_suspend,
1295 .resume_noirq = i8042_pm_resume_noirq,
1296 .resume = i8042_pm_resume,
1297 .thaw = i8042_pm_thaw,
1298 .poweroff = i8042_pm_reset,
1299 .restore = i8042_pm_restore,
1302 #endif /* CONFIG_PM */
1305 * We need to reset the 8042 back to original mode on system shutdown,
1306 * because otherwise BIOSes will be confused.
1309 static void i8042_shutdown(struct platform_device *dev)
1311 i8042_controller_reset(false);
1314 static int __init i8042_create_kbd_port(void)
1316 struct serio *serio;
1317 struct i8042_port *port = &i8042_ports[I8042_KBD_PORT_NO];
1319 serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
1323 serio->id.type = i8042_direct ? SERIO_8042 : SERIO_8042_XL;
1324 serio->write = i8042_dumbkbd ? NULL : i8042_kbd_write;
1325 serio->start = i8042_start;
1326 serio->stop = i8042_stop;
1327 serio->close = i8042_port_close;
1328 serio->ps2_cmd_mutex = &i8042_mutex;
1329 serio->port_data = port;
1330 serio->dev.parent = &i8042_platform_device->dev;
1331 strlcpy(serio->name, "i8042 KBD port", sizeof(serio->name));
1332 strlcpy(serio->phys, I8042_KBD_PHYS_DESC, sizeof(serio->phys));
1333 strlcpy(serio->firmware_id, i8042_kbd_firmware_id,
1334 sizeof(serio->firmware_id));
1336 port->serio = serio;
1337 port->irq = I8042_KBD_IRQ;
1342 static int __init i8042_create_aux_port(int idx)
1344 struct serio *serio;
1345 int port_no = idx < 0 ? I8042_AUX_PORT_NO : I8042_MUX_PORT_NO + idx;
1346 struct i8042_port *port = &i8042_ports[port_no];
1348 serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
1352 serio->id.type = SERIO_8042;
1353 serio->write = i8042_aux_write;
1354 serio->start = i8042_start;
1355 serio->stop = i8042_stop;
1356 serio->ps2_cmd_mutex = &i8042_mutex;
1357 serio->port_data = port;
1358 serio->dev.parent = &i8042_platform_device->dev;
1360 strlcpy(serio->name, "i8042 AUX port", sizeof(serio->name));
1361 strlcpy(serio->phys, I8042_AUX_PHYS_DESC, sizeof(serio->phys));
1362 strlcpy(serio->firmware_id, i8042_aux_firmware_id,
1363 sizeof(serio->firmware_id));
1364 serio->close = i8042_port_close;
1366 snprintf(serio->name, sizeof(serio->name), "i8042 AUX%d port", idx);
1367 snprintf(serio->phys, sizeof(serio->phys), I8042_MUX_PHYS_DESC, idx + 1);
1368 strlcpy(serio->firmware_id, i8042_aux_firmware_id,
1369 sizeof(serio->firmware_id));
1372 port->serio = serio;
1374 port->irq = I8042_AUX_IRQ;
1379 static void __init i8042_free_kbd_port(void)
1381 kfree(i8042_ports[I8042_KBD_PORT_NO].serio);
1382 i8042_ports[I8042_KBD_PORT_NO].serio = NULL;
1385 static void __init i8042_free_aux_ports(void)
1389 for (i = I8042_AUX_PORT_NO; i < I8042_NUM_PORTS; i++) {
1390 kfree(i8042_ports[i].serio);
1391 i8042_ports[i].serio = NULL;
1395 static void __init i8042_register_ports(void)
1399 for (i = 0; i < I8042_NUM_PORTS; i++) {
1400 struct serio *serio = i8042_ports[i].serio;
1405 printk(KERN_INFO "serio: %s at %#lx,%#lx irq %d\n",
1407 (unsigned long) I8042_DATA_REG,
1408 (unsigned long) I8042_COMMAND_REG,
1409 i8042_ports[i].irq);
1410 serio_register_port(serio);
1411 device_set_wakeup_capable(&serio->dev, true);
1414 * On platforms using suspend-to-idle, allow the keyboard to
1415 * wake up the system from sleep by enabling keyboard wakeups
1416 * by default. This is consistent with keyboard wakeup
1417 * behavior on many platforms using suspend-to-RAM (ACPI S3)
1420 if (pm_suspend_via_s2idle() && i == I8042_KBD_PORT_NO)
1421 device_set_wakeup_enable(&serio->dev, true);
1425 static void i8042_unregister_ports(void)
1429 for (i = 0; i < I8042_NUM_PORTS; i++) {
1430 if (i8042_ports[i].serio) {
1431 serio_unregister_port(i8042_ports[i].serio);
1432 i8042_ports[i].serio = NULL;
1437 static void i8042_free_irqs(void)
1439 if (i8042_aux_irq_registered)
1440 free_irq(I8042_AUX_IRQ, i8042_platform_device);
1441 if (i8042_kbd_irq_registered)
1442 free_irq(I8042_KBD_IRQ, i8042_platform_device);
1444 i8042_aux_irq_registered = i8042_kbd_irq_registered = false;
1447 static int __init i8042_setup_aux(void)
1449 int (*aux_enable)(void);
1453 if (i8042_check_aux())
1456 if (i8042_nomux || i8042_check_mux()) {
1457 error = i8042_create_aux_port(-1);
1459 goto err_free_ports;
1460 aux_enable = i8042_enable_aux_port;
1462 for (i = 0; i < I8042_NUM_MUX_PORTS; i++) {
1463 error = i8042_create_aux_port(i);
1465 goto err_free_ports;
1467 aux_enable = i8042_enable_mux_ports;
1470 error = request_irq(I8042_AUX_IRQ, i8042_interrupt, IRQF_SHARED,
1471 "i8042", i8042_platform_device);
1473 goto err_free_ports;
1475 error = aux_enable();
1479 i8042_aux_irq_registered = true;
1483 free_irq(I8042_AUX_IRQ, i8042_platform_device);
1485 i8042_free_aux_ports();
1489 static int __init i8042_setup_kbd(void)
1493 error = i8042_create_kbd_port();
1497 error = request_irq(I8042_KBD_IRQ, i8042_interrupt, IRQF_SHARED,
1498 "i8042", i8042_platform_device);
1502 error = i8042_enable_kbd_port();
1506 i8042_kbd_irq_registered = true;
1510 free_irq(I8042_KBD_IRQ, i8042_platform_device);
1512 i8042_free_kbd_port();
1516 static int i8042_kbd_bind_notifier(struct notifier_block *nb,
1517 unsigned long action, void *data)
1519 struct device *dev = data;
1520 struct serio *serio = to_serio_port(dev);
1521 struct i8042_port *port = serio->port_data;
1523 if (serio != i8042_ports[I8042_KBD_PORT_NO].serio)
1527 case BUS_NOTIFY_BOUND_DRIVER:
1528 port->driver_bound = true;
1531 case BUS_NOTIFY_UNBIND_DRIVER:
1532 port->driver_bound = false;
1539 static int __init i8042_probe(struct platform_device *dev)
1543 i8042_platform_device = dev;
1545 if (i8042_reset == I8042_RESET_ALWAYS) {
1546 error = i8042_controller_selftest();
1551 error = i8042_controller_init();
1557 i8042_dritek_enable();
1561 error = i8042_setup_aux();
1562 if (error && error != -ENODEV && error != -EBUSY)
1567 error = i8042_setup_kbd();
1572 * Ok, everything is ready, let's register all serio ports
1574 i8042_register_ports();
1579 i8042_free_aux_ports(); /* in case KBD failed but AUX not */
1581 i8042_controller_reset(false);
1582 i8042_platform_device = NULL;
1587 static int i8042_remove(struct platform_device *dev)
1589 i8042_unregister_ports();
1591 i8042_controller_reset(false);
1592 i8042_platform_device = NULL;
1597 static struct platform_driver i8042_driver = {
1601 .pm = &i8042_pm_ops,
1604 .remove = i8042_remove,
1605 .shutdown = i8042_shutdown,
1608 static struct notifier_block i8042_kbd_bind_notifier_block = {
1609 .notifier_call = i8042_kbd_bind_notifier,
1612 static int __init i8042_init(void)
1614 struct platform_device *pdev;
1619 err = i8042_platform_init();
1621 return (err == -ENODEV) ? 0 : err;
1623 err = i8042_controller_check();
1625 goto err_platform_exit;
1627 /* Set this before creating the dev to allow i8042_command to work right away */
1628 i8042_present = true;
1630 pdev = platform_create_bundle(&i8042_driver, i8042_probe, NULL, 0, NULL, 0);
1632 err = PTR_ERR(pdev);
1633 goto err_platform_exit;
1636 bus_register_notifier(&serio_bus, &i8042_kbd_bind_notifier_block);
1637 panic_blink = i8042_panic_blink;
1642 i8042_platform_exit();
1646 static void __exit i8042_exit(void)
1651 platform_device_unregister(i8042_platform_device);
1652 platform_driver_unregister(&i8042_driver);
1653 i8042_platform_exit();
1655 bus_unregister_notifier(&serio_bus, &i8042_kbd_bind_notifier_block);
1659 module_init(i8042_init);
1660 module_exit(i8042_exit);