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>
30 MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
31 MODULE_DESCRIPTION("i8042 keyboard and mouse controller driver");
32 MODULE_LICENSE("GPL");
34 static bool i8042_nokbd;
35 module_param_named(nokbd, i8042_nokbd, bool, 0);
36 MODULE_PARM_DESC(nokbd, "Do not probe or use KBD port.");
38 static bool i8042_noaux;
39 module_param_named(noaux, i8042_noaux, bool, 0);
40 MODULE_PARM_DESC(noaux, "Do not probe or use AUX (mouse) port.");
42 static bool i8042_nomux;
43 module_param_named(nomux, i8042_nomux, bool, 0);
44 MODULE_PARM_DESC(nomux, "Do not check whether an active multiplexing controller is present.");
46 static bool i8042_unlock;
47 module_param_named(unlock, i8042_unlock, bool, 0);
48 MODULE_PARM_DESC(unlock, "Ignore keyboard lock.");
50 static bool i8042_reset;
51 module_param_named(reset, i8042_reset, bool, 0);
52 MODULE_PARM_DESC(reset, "Reset controller during init and cleanup.");
54 static bool i8042_direct;
55 module_param_named(direct, i8042_direct, bool, 0);
56 MODULE_PARM_DESC(direct, "Put keyboard port into non-translated mode.");
58 static bool i8042_dumbkbd;
59 module_param_named(dumbkbd, i8042_dumbkbd, bool, 0);
60 MODULE_PARM_DESC(dumbkbd, "Pretend that controller can only read data from keyboard");
62 static bool i8042_noloop;
63 module_param_named(noloop, i8042_noloop, bool, 0);
64 MODULE_PARM_DESC(noloop, "Disable the AUX Loopback command while probing for the AUX port");
66 static bool i8042_notimeout;
67 module_param_named(notimeout, i8042_notimeout, bool, 0);
68 MODULE_PARM_DESC(notimeout, "Ignore timeouts signalled by i8042");
70 static bool i8042_kbdreset;
71 module_param_named(kbdreset, i8042_kbdreset, bool, 0);
72 MODULE_PARM_DESC(kbdreset, "Reset device connected to KBD port");
75 static bool i8042_dritek;
76 module_param_named(dritek, i8042_dritek, bool, 0);
77 MODULE_PARM_DESC(dritek, "Force enable the Dritek keyboard extension");
81 static bool i8042_nopnp;
82 module_param_named(nopnp, i8042_nopnp, bool, 0);
83 MODULE_PARM_DESC(nopnp, "Do not use PNP to detect controller settings");
88 static bool i8042_debug;
89 module_param_named(debug, i8042_debug, bool, 0600);
90 MODULE_PARM_DESC(debug, "Turn i8042 debugging mode on and off");
93 static bool i8042_bypass_aux_irq_test;
98 * i8042_lock protects serialization between i8042_command and
99 * the interrupt handler.
101 static DEFINE_SPINLOCK(i8042_lock);
104 * Writers to AUX and KBD ports as well as users issuing i8042_command
105 * directly should acquire i8042_mutex (by means of calling
106 * i8042_lock_chip() and i8042_unlock_ship() helpers) to ensure that
107 * they do not disturb each other (unfortunately in many i8042
108 * implementations write to one of the ports will immediately abort
109 * command that is being processed by another port).
111 static DEFINE_MUTEX(i8042_mutex);
120 #define I8042_KBD_PORT_NO 0
121 #define I8042_AUX_PORT_NO 1
122 #define I8042_MUX_PORT_NO 2
123 #define I8042_NUM_PORTS (I8042_NUM_MUX_PORTS + 2)
125 static struct i8042_port i8042_ports[I8042_NUM_PORTS];
127 static unsigned char i8042_initial_ctr;
128 static unsigned char i8042_ctr;
129 static bool i8042_mux_present;
130 static bool i8042_kbd_irq_registered;
131 static bool i8042_aux_irq_registered;
132 static unsigned char i8042_suppress_kbd_ack;
133 static struct platform_device *i8042_platform_device;
135 static irqreturn_t i8042_interrupt(int irq, void *dev_id);
136 static bool (*i8042_platform_filter)(unsigned char data, unsigned char str,
137 struct serio *serio);
139 void i8042_lock_chip(void)
141 mutex_lock(&i8042_mutex);
143 EXPORT_SYMBOL(i8042_lock_chip);
145 void i8042_unlock_chip(void)
147 mutex_unlock(&i8042_mutex);
149 EXPORT_SYMBOL(i8042_unlock_chip);
151 int i8042_install_filter(bool (*filter)(unsigned char data, unsigned char str,
152 struct serio *serio))
157 spin_lock_irqsave(&i8042_lock, flags);
159 if (i8042_platform_filter) {
164 i8042_platform_filter = filter;
167 spin_unlock_irqrestore(&i8042_lock, flags);
170 EXPORT_SYMBOL(i8042_install_filter);
172 int i8042_remove_filter(bool (*filter)(unsigned char data, unsigned char str,
178 spin_lock_irqsave(&i8042_lock, flags);
180 if (i8042_platform_filter != filter) {
185 i8042_platform_filter = NULL;
188 spin_unlock_irqrestore(&i8042_lock, flags);
191 EXPORT_SYMBOL(i8042_remove_filter);
194 * The i8042_wait_read() and i8042_wait_write functions wait for the i8042 to
195 * be ready for reading values from it / writing values to it.
196 * Called always with i8042_lock held.
199 static int i8042_wait_read(void)
203 while ((~i8042_read_status() & I8042_STR_OBF) && (i < I8042_CTL_TIMEOUT)) {
207 return -(i == I8042_CTL_TIMEOUT);
210 static int i8042_wait_write(void)
214 while ((i8042_read_status() & I8042_STR_IBF) && (i < I8042_CTL_TIMEOUT)) {
218 return -(i == I8042_CTL_TIMEOUT);
222 * i8042_flush() flushes all data that may be in the keyboard and mouse buffers
223 * of the i8042 down the toilet.
226 static int i8042_flush(void)
229 unsigned char data, str;
232 spin_lock_irqsave(&i8042_lock, flags);
234 while (((str = i8042_read_status()) & I8042_STR_OBF) && (i < I8042_BUFFER_SIZE)) {
236 data = i8042_read_data();
238 dbg("%02x <- i8042 (flush, %s)\n",
239 data, str & I8042_STR_AUXDATA ? "aux" : "kbd");
242 spin_unlock_irqrestore(&i8042_lock, flags);
248 * i8042_command() executes a command on the i8042. It also sends the input
249 * parameter(s) of the commands to it, and receives the output value(s). The
250 * parameters are to be stored in the param array, and the output is placed
251 * into the same array. The number of the parameters and output values is
252 * encoded in bits 8-11 of the command number.
255 static int __i8042_command(unsigned char *param, int command)
259 if (i8042_noloop && command == I8042_CMD_AUX_LOOP)
262 error = i8042_wait_write();
266 dbg("%02x -> i8042 (command)\n", command & 0xff);
267 i8042_write_command(command & 0xff);
269 for (i = 0; i < ((command >> 12) & 0xf); i++) {
270 error = i8042_wait_write();
273 dbg("%02x -> i8042 (parameter)\n", param[i]);
274 i8042_write_data(param[i]);
277 for (i = 0; i < ((command >> 8) & 0xf); i++) {
278 error = i8042_wait_read();
280 dbg(" -- i8042 (timeout)\n");
284 if (command == I8042_CMD_AUX_LOOP &&
285 !(i8042_read_status() & I8042_STR_AUXDATA)) {
286 dbg(" -- i8042 (auxerr)\n");
290 param[i] = i8042_read_data();
291 dbg("%02x <- i8042 (return)\n", param[i]);
297 int i8042_command(unsigned char *param, int command)
302 spin_lock_irqsave(&i8042_lock, flags);
303 retval = __i8042_command(param, command);
304 spin_unlock_irqrestore(&i8042_lock, flags);
308 EXPORT_SYMBOL(i8042_command);
311 * i8042_kbd_write() sends a byte out through the keyboard interface.
314 static int i8042_kbd_write(struct serio *port, unsigned char c)
319 spin_lock_irqsave(&i8042_lock, flags);
321 if (!(retval = i8042_wait_write())) {
322 dbg("%02x -> i8042 (kbd-data)\n", c);
326 spin_unlock_irqrestore(&i8042_lock, flags);
332 * i8042_aux_write() sends a byte out through the aux interface.
335 static int i8042_aux_write(struct serio *serio, unsigned char c)
337 struct i8042_port *port = serio->port_data;
339 return i8042_command(&c, port->mux == -1 ?
341 I8042_CMD_MUX_SEND + port->mux);
346 * i8042_aux_close attempts to clear AUX or KBD port state by disabling
347 * and then re-enabling it.
350 static void i8042_port_close(struct serio *serio)
354 const char *port_name;
356 if (serio == i8042_ports[I8042_AUX_PORT_NO].serio) {
357 irq_bit = I8042_CTR_AUXINT;
358 disable_bit = I8042_CTR_AUXDIS;
361 irq_bit = I8042_CTR_KBDINT;
362 disable_bit = I8042_CTR_KBDDIS;
366 i8042_ctr &= ~irq_bit;
367 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR))
368 pr_warn("Can't write CTR while closing %s port\n", port_name);
372 i8042_ctr &= ~disable_bit;
373 i8042_ctr |= irq_bit;
374 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR))
375 pr_err("Can't reactivate %s port\n", port_name);
378 * See if there is any data appeared while we were messing with
381 i8042_interrupt(0, NULL);
385 * i8042_start() is called by serio core when port is about to finish
386 * registering. It will mark port as existing so i8042_interrupt can
387 * start sending data through it.
389 static int i8042_start(struct serio *serio)
391 struct i8042_port *port = serio->port_data;
399 * i8042_stop() marks serio port as non-existing so i8042_interrupt
400 * will not try to send data to the port that is about to go away.
401 * The function is called by serio core as part of unregister procedure.
403 static void i8042_stop(struct serio *serio)
405 struct i8042_port *port = serio->port_data;
407 port->exists = false;
410 * We synchronize with both AUX and KBD IRQs because there is
411 * a (very unlikely) chance that AUX IRQ is raised for KBD port
414 synchronize_irq(I8042_AUX_IRQ);
415 synchronize_irq(I8042_KBD_IRQ);
420 * i8042_filter() filters out unwanted bytes from the input data stream.
421 * It is called from i8042_interrupt and thus is running with interrupts
422 * off and i8042_lock held.
424 static bool i8042_filter(unsigned char data, unsigned char str,
427 if (unlikely(i8042_suppress_kbd_ack)) {
428 if ((~str & I8042_STR_AUXDATA) &&
429 (data == 0xfa || data == 0xfe)) {
430 i8042_suppress_kbd_ack--;
431 dbg("Extra keyboard ACK - filtered out\n");
436 if (i8042_platform_filter && i8042_platform_filter(data, str, serio)) {
437 dbg("Filtered out by platform filter\n");
445 * i8042_interrupt() is the most important function in this driver -
446 * it handles the interrupts from the i8042, and sends incoming bytes
447 * to the upper layers.
450 static irqreturn_t i8042_interrupt(int irq, void *dev_id)
452 struct i8042_port *port;
455 unsigned char str, data;
457 unsigned int port_no;
461 spin_lock_irqsave(&i8042_lock, flags);
463 str = i8042_read_status();
464 if (unlikely(~str & I8042_STR_OBF)) {
465 spin_unlock_irqrestore(&i8042_lock, flags);
467 dbg("Interrupt %d, without any data\n", irq);
472 data = i8042_read_data();
474 if (i8042_mux_present && (str & I8042_STR_AUXDATA)) {
475 static unsigned long last_transmit;
476 static unsigned char last_str;
479 if (str & I8042_STR_MUXERR) {
480 dbg("MUX error, status is %02x, data is %02x\n",
483 * When MUXERR condition is signalled the data register can only contain
484 * 0xfd, 0xfe or 0xff if implementation follows the spec. Unfortunately
485 * it is not always the case. Some KBCs also report 0xfc when there is
486 * nothing connected to the port while others sometimes get confused which
487 * port the data came from and signal error leaving the data intact. They
488 * _do not_ revert to legacy mode (actually I've never seen KBC reverting
489 * to legacy mode yet, when we see one we'll add proper handling).
490 * Anyway, we process 0xfc, 0xfd, 0xfe and 0xff as timeouts, and for the
491 * rest assume that the data came from the same serio last byte
492 * was transmitted (if transmission happened not too long ago).
497 if (time_before(jiffies, last_transmit + HZ/10)) {
501 /* fall through - report timeout */
504 case 0xfe: dfl = SERIO_TIMEOUT; data = 0xfe; break;
505 case 0xff: dfl = SERIO_PARITY; data = 0xfe; break;
509 port_no = I8042_MUX_PORT_NO + ((str >> 6) & 3);
511 last_transmit = jiffies;
514 dfl = ((str & I8042_STR_PARITY) ? SERIO_PARITY : 0) |
515 ((str & I8042_STR_TIMEOUT && !i8042_notimeout) ? SERIO_TIMEOUT : 0);
517 port_no = (str & I8042_STR_AUXDATA) ?
518 I8042_AUX_PORT_NO : I8042_KBD_PORT_NO;
521 port = &i8042_ports[port_no];
522 serio = port->exists ? port->serio : NULL;
524 dbg("%02x <- i8042 (interrupt, %d, %d%s%s)\n",
526 dfl & SERIO_PARITY ? ", bad parity" : "",
527 dfl & SERIO_TIMEOUT ? ", timeout" : "");
529 filtered = i8042_filter(data, str, serio);
531 spin_unlock_irqrestore(&i8042_lock, flags);
533 if (likely(port->exists && !filtered))
534 serio_interrupt(serio, data, dfl);
537 return IRQ_RETVAL(ret);
541 * i8042_enable_kbd_port enables keyboard port on chip
544 static int i8042_enable_kbd_port(void)
546 i8042_ctr &= ~I8042_CTR_KBDDIS;
547 i8042_ctr |= I8042_CTR_KBDINT;
549 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
550 i8042_ctr &= ~I8042_CTR_KBDINT;
551 i8042_ctr |= I8042_CTR_KBDDIS;
552 pr_err("Failed to enable KBD port\n");
560 * i8042_enable_aux_port enables AUX (mouse) port on chip
563 static int i8042_enable_aux_port(void)
565 i8042_ctr &= ~I8042_CTR_AUXDIS;
566 i8042_ctr |= I8042_CTR_AUXINT;
568 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
569 i8042_ctr &= ~I8042_CTR_AUXINT;
570 i8042_ctr |= I8042_CTR_AUXDIS;
571 pr_err("Failed to enable AUX port\n");
579 * i8042_enable_mux_ports enables 4 individual AUX ports after
580 * the controller has been switched into Multiplexed mode
583 static int i8042_enable_mux_ports(void)
588 for (i = 0; i < I8042_NUM_MUX_PORTS; i++) {
589 i8042_command(¶m, I8042_CMD_MUX_PFX + i);
590 i8042_command(¶m, I8042_CMD_AUX_ENABLE);
593 return i8042_enable_aux_port();
597 * i8042_set_mux_mode checks whether the controller has an
598 * active multiplexor and puts the chip into Multiplexed (true)
599 * or Legacy (false) mode.
602 static int i8042_set_mux_mode(bool multiplex, unsigned char *mux_version)
605 unsigned char param, val;
607 * Get rid of bytes in the queue.
613 * Internal loopback test - send three bytes, they should come back from the
614 * mouse interface, the last should be version.
618 if (i8042_command(¶m, I8042_CMD_AUX_LOOP) || param != val)
620 param = val = multiplex ? 0x56 : 0xf6;
621 if (i8042_command(¶m, I8042_CMD_AUX_LOOP) || param != val)
623 param = val = multiplex ? 0xa4 : 0xa5;
624 if (i8042_command(¶m, I8042_CMD_AUX_LOOP) || param == val)
628 * Workaround for interference with USB Legacy emulation
629 * that causes a v10.12 MUX to be found.
635 *mux_version = param;
641 * i8042_check_mux() checks whether the controller supports the PS/2 Active
642 * Multiplexing specification by Synaptics, Phoenix, Insyde and
646 static int __init i8042_check_mux(void)
648 unsigned char mux_version;
650 if (i8042_set_mux_mode(true, &mux_version))
653 pr_info("Detected active multiplexing controller, rev %d.%d\n",
654 (mux_version >> 4) & 0xf, mux_version & 0xf);
657 * Disable all muxed ports by disabling AUX.
659 i8042_ctr |= I8042_CTR_AUXDIS;
660 i8042_ctr &= ~I8042_CTR_AUXINT;
662 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
663 pr_err("Failed to disable AUX port, can't use MUX\n");
667 i8042_mux_present = true;
673 * The following is used to test AUX IRQ delivery.
675 static struct completion i8042_aux_irq_delivered __initdata;
676 static bool i8042_irq_being_tested __initdata;
678 static irqreturn_t __init i8042_aux_test_irq(int irq, void *dev_id)
681 unsigned char str, data;
684 spin_lock_irqsave(&i8042_lock, flags);
685 str = i8042_read_status();
686 if (str & I8042_STR_OBF) {
687 data = i8042_read_data();
688 dbg("%02x <- i8042 (aux_test_irq, %s)\n",
689 data, str & I8042_STR_AUXDATA ? "aux" : "kbd");
690 if (i8042_irq_being_tested &&
691 data == 0xa5 && (str & I8042_STR_AUXDATA))
692 complete(&i8042_aux_irq_delivered);
695 spin_unlock_irqrestore(&i8042_lock, flags);
697 return IRQ_RETVAL(ret);
701 * i8042_toggle_aux - enables or disables AUX port on i8042 via command and
702 * verifies success by readinng CTR. Used when testing for presence of AUX
705 static int __init i8042_toggle_aux(bool on)
710 if (i8042_command(¶m,
711 on ? I8042_CMD_AUX_ENABLE : I8042_CMD_AUX_DISABLE))
714 /* some chips need some time to set the I8042_CTR_AUXDIS bit */
715 for (i = 0; i < 100; i++) {
718 if (i8042_command(¶m, I8042_CMD_CTL_RCTR))
721 if (!(param & I8042_CTR_AUXDIS) == on)
729 * i8042_check_aux() applies as much paranoia as it can at detecting
730 * the presence of an AUX interface.
733 static int __init i8042_check_aux(void)
736 bool irq_registered = false;
737 bool aux_loop_broken = false;
742 * Get rid of bytes in the queue.
748 * Internal loopback test - filters out AT-type i8042's. Unfortunately
749 * SiS screwed up and their 5597 doesn't support the LOOP command even
750 * though it has an AUX port.
754 retval = i8042_command(¶m, I8042_CMD_AUX_LOOP);
755 if (retval || param != 0x5a) {
758 * External connection test - filters out AT-soldered PS/2 i8042's
759 * 0x00 - no error, 0x01-0x03 - clock/data stuck, 0xff - general error
760 * 0xfa - no error on some notebooks which ignore the spec
761 * Because it's common for chipsets to return error on perfectly functioning
762 * AUX ports, we test for this only when the LOOP command failed.
765 if (i8042_command(¶m, I8042_CMD_AUX_TEST) ||
766 (param && param != 0xfa && param != 0xff))
770 * If AUX_LOOP completed without error but returned unexpected data
774 aux_loop_broken = true;
778 * Bit assignment test - filters out PS/2 i8042's in AT mode
781 if (i8042_toggle_aux(false)) {
782 pr_warn("Failed to disable AUX port, but continuing anyway... Is this a SiS?\n");
783 pr_warn("If AUX port is really absent please use the 'i8042.noaux' option\n");
786 if (i8042_toggle_aux(true))
790 * Reset keyboard (needed on some laptops to successfully detect
791 * touchpad, e.g., some Gigabyte laptop models with Elantech
794 if (i8042_kbdreset) {
795 pr_warn("Attempting to reset device connected to KBD port\n");
796 i8042_kbd_write(NULL, (unsigned char) 0xff);
800 * Test AUX IRQ delivery to make sure BIOS did not grab the IRQ and
801 * used it for a PCI card or somethig else.
804 if (i8042_noloop || i8042_bypass_aux_irq_test || aux_loop_broken) {
806 * Without LOOP command we can't test AUX IRQ delivery. Assume the port
807 * is working and hope we are right.
813 if (request_irq(I8042_AUX_IRQ, i8042_aux_test_irq, IRQF_SHARED,
814 "i8042", i8042_platform_device))
817 irq_registered = true;
819 if (i8042_enable_aux_port())
822 spin_lock_irqsave(&i8042_lock, flags);
824 init_completion(&i8042_aux_irq_delivered);
825 i8042_irq_being_tested = true;
828 retval = __i8042_command(¶m, I8042_CMD_AUX_LOOP & 0xf0ff);
830 spin_unlock_irqrestore(&i8042_lock, flags);
835 if (wait_for_completion_timeout(&i8042_aux_irq_delivered,
836 msecs_to_jiffies(250)) == 0) {
838 * AUX IRQ was never delivered so we need to flush the controller to
839 * get rid of the byte we put there; otherwise keyboard may not work.
841 dbg(" -- i8042 (aux irq test timeout)\n");
849 * Disable the interface.
852 i8042_ctr |= I8042_CTR_AUXDIS;
853 i8042_ctr &= ~I8042_CTR_AUXINT;
855 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR))
859 free_irq(I8042_AUX_IRQ, i8042_platform_device);
864 static int i8042_controller_check(void)
866 if (i8042_flush() == I8042_BUFFER_SIZE) {
867 pr_err("No controller found\n");
874 static int i8042_controller_selftest(void)
880 * We try this 5 times; on some really fragile systems this does not
881 * take the first time...
885 if (i8042_command(¶m, I8042_CMD_CTL_TEST)) {
886 pr_err("i8042 controller selftest timeout\n");
890 if (param == I8042_RET_CTL_TEST)
893 dbg("i8042 controller selftest: %#x != %#x\n",
894 param, I8042_RET_CTL_TEST);
900 * On x86, we don't fail entire i8042 initialization if controller
901 * reset fails in hopes that keyboard port will still be functional
902 * and user will still get a working keyboard. This is especially
903 * important on netbooks. On other arches we trust hardware more.
905 pr_info("giving up on controller selftest, continuing anyway...\n");
908 pr_err("i8042 controller selftest failed\n");
914 * i8042_controller init initializes the i8042 controller, and,
915 * most importantly, sets it into non-xlated mode if that's
919 static int i8042_controller_init(void)
923 unsigned char ctr[2];
926 * Save the CTR for restore on unload / reboot.
931 pr_err("Unable to get stable CTR read\n");
938 if (i8042_command(&ctr[n++ % 2], I8042_CMD_CTL_RCTR)) {
939 pr_err("Can't read CTR while initializing i8042\n");
943 } while (n < 2 || ctr[0] != ctr[1]);
945 i8042_initial_ctr = i8042_ctr = ctr[0];
948 * Disable the keyboard interface and interrupt.
951 i8042_ctr |= I8042_CTR_KBDDIS;
952 i8042_ctr &= ~I8042_CTR_KBDINT;
958 spin_lock_irqsave(&i8042_lock, flags);
959 if (~i8042_read_status() & I8042_STR_KEYLOCK) {
961 i8042_ctr |= I8042_CTR_IGNKEYLOCK;
963 pr_warn("Warning: Keylock active\n");
965 spin_unlock_irqrestore(&i8042_lock, flags);
968 * If the chip is configured into nontranslated mode by the BIOS, don't
969 * bother enabling translating and be happy.
972 if (~i8042_ctr & I8042_CTR_XLATE)
976 * Set nontranslated mode for the kbd interface if requested by an option.
977 * After this the kbd interface becomes a simple serial in/out, like the aux
978 * interface is. We don't do this by default, since it can confuse notebook
983 i8042_ctr &= ~I8042_CTR_XLATE;
989 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
990 pr_err("Can't write CTR while initializing i8042\n");
995 * Flush whatever accumulated while we were disabling keyboard port.
1005 * Reset the controller and reset CRT to the original value set by BIOS.
1008 static void i8042_controller_reset(bool force_reset)
1013 * Disable both KBD and AUX interfaces so they don't get in the way
1016 i8042_ctr |= I8042_CTR_KBDDIS | I8042_CTR_AUXDIS;
1017 i8042_ctr &= ~(I8042_CTR_KBDINT | I8042_CTR_AUXINT);
1019 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR))
1020 pr_warn("Can't write CTR while resetting\n");
1023 * Disable MUX mode if present.
1026 if (i8042_mux_present)
1027 i8042_set_mux_mode(false, NULL);
1030 * Reset the controller if requested.
1033 if (i8042_reset || force_reset)
1034 i8042_controller_selftest();
1037 * Restore the original control register setting.
1040 if (i8042_command(&i8042_initial_ctr, I8042_CMD_CTL_WCTR))
1041 pr_warn("Can't restore CTR\n");
1046 * i8042_panic_blink() will turn the keyboard LEDs on or off and is called
1047 * when kernel panics. Flashing LEDs is useful for users running X who may
1048 * not see the console and will help distingushing panics from "real"
1051 * Note that DELAY has a limit of 10ms so we will not get stuck here
1052 * waiting for KBC to free up even if KBD interrupt is off
1055 #define DELAY do { mdelay(1); if (++delay > 10) return delay; } while(0)
1057 static long i8042_panic_blink(int state)
1062 led = (state) ? 0x01 | 0x04 : 0;
1063 while (i8042_read_status() & I8042_STR_IBF)
1065 dbg("%02x -> i8042 (panic blink)\n", 0xed);
1066 i8042_suppress_kbd_ack = 2;
1067 i8042_write_data(0xed); /* set leds */
1069 while (i8042_read_status() & I8042_STR_IBF)
1072 dbg("%02x -> i8042 (panic blink)\n", led);
1073 i8042_write_data(led);
1081 static void i8042_dritek_enable(void)
1083 unsigned char param = 0x90;
1086 error = i8042_command(¶m, 0x1059);
1088 pr_warn("Failed to enable DRITEK extension: %d\n", error);
1095 * Here we try to reset everything back to a state we had
1096 * before suspending.
1099 static int i8042_controller_resume(bool force_reset)
1103 error = i8042_controller_check();
1107 if (i8042_reset || force_reset) {
1108 error = i8042_controller_selftest();
1114 * Restore original CTR value and disable all ports
1117 i8042_ctr = i8042_initial_ctr;
1119 i8042_ctr &= ~I8042_CTR_XLATE;
1120 i8042_ctr |= I8042_CTR_AUXDIS | I8042_CTR_KBDDIS;
1121 i8042_ctr &= ~(I8042_CTR_AUXINT | I8042_CTR_KBDINT);
1122 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
1123 pr_warn("Can't write CTR to resume, retrying...\n");
1125 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
1126 pr_err("CTR write retry failed\n");
1134 i8042_dritek_enable();
1137 if (i8042_mux_present) {
1138 if (i8042_set_mux_mode(true, NULL) || i8042_enable_mux_ports())
1139 pr_warn("failed to resume active multiplexor, mouse won't work\n");
1140 } else if (i8042_ports[I8042_AUX_PORT_NO].serio)
1141 i8042_enable_aux_port();
1143 if (i8042_ports[I8042_KBD_PORT_NO].serio)
1144 i8042_enable_kbd_port();
1146 i8042_interrupt(0, NULL);
1152 * Here we try to restore the original BIOS settings to avoid
1156 static int i8042_pm_suspend(struct device *dev)
1158 i8042_controller_reset(true);
1163 static int i8042_pm_resume(struct device *dev)
1166 * On resume from S2R we always try to reset the controller
1167 * to bring it in a sane state. (In case of S2D we expect
1168 * BIOS to reset the controller for us.)
1170 return i8042_controller_resume(true);
1173 static int i8042_pm_thaw(struct device *dev)
1175 i8042_interrupt(0, NULL);
1180 static int i8042_pm_reset(struct device *dev)
1182 i8042_controller_reset(false);
1187 static int i8042_pm_restore(struct device *dev)
1189 return i8042_controller_resume(false);
1192 static const struct dev_pm_ops i8042_pm_ops = {
1193 .suspend = i8042_pm_suspend,
1194 .resume = i8042_pm_resume,
1195 .thaw = i8042_pm_thaw,
1196 .poweroff = i8042_pm_reset,
1197 .restore = i8042_pm_restore,
1200 #endif /* CONFIG_PM */
1203 * We need to reset the 8042 back to original mode on system shutdown,
1204 * because otherwise BIOSes will be confused.
1207 static void i8042_shutdown(struct platform_device *dev)
1209 i8042_controller_reset(false);
1212 static int __init i8042_create_kbd_port(void)
1214 struct serio *serio;
1215 struct i8042_port *port = &i8042_ports[I8042_KBD_PORT_NO];
1217 serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
1221 serio->id.type = i8042_direct ? SERIO_8042 : SERIO_8042_XL;
1222 serio->write = i8042_dumbkbd ? NULL : i8042_kbd_write;
1223 serio->start = i8042_start;
1224 serio->stop = i8042_stop;
1225 serio->close = i8042_port_close;
1226 serio->port_data = port;
1227 serio->dev.parent = &i8042_platform_device->dev;
1228 strlcpy(serio->name, "i8042 KBD port", sizeof(serio->name));
1229 strlcpy(serio->phys, I8042_KBD_PHYS_DESC, sizeof(serio->phys));
1231 port->serio = serio;
1232 port->irq = I8042_KBD_IRQ;
1237 static int __init i8042_create_aux_port(int idx)
1239 struct serio *serio;
1240 int port_no = idx < 0 ? I8042_AUX_PORT_NO : I8042_MUX_PORT_NO + idx;
1241 struct i8042_port *port = &i8042_ports[port_no];
1243 serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
1247 serio->id.type = SERIO_8042;
1248 serio->write = i8042_aux_write;
1249 serio->start = i8042_start;
1250 serio->stop = i8042_stop;
1251 serio->port_data = port;
1252 serio->dev.parent = &i8042_platform_device->dev;
1254 strlcpy(serio->name, "i8042 AUX port", sizeof(serio->name));
1255 strlcpy(serio->phys, I8042_AUX_PHYS_DESC, sizeof(serio->phys));
1256 serio->close = i8042_port_close;
1258 snprintf(serio->name, sizeof(serio->name), "i8042 AUX%d port", idx);
1259 snprintf(serio->phys, sizeof(serio->phys), I8042_MUX_PHYS_DESC, idx + 1);
1262 port->serio = serio;
1264 port->irq = I8042_AUX_IRQ;
1269 static void __init i8042_free_kbd_port(void)
1271 kfree(i8042_ports[I8042_KBD_PORT_NO].serio);
1272 i8042_ports[I8042_KBD_PORT_NO].serio = NULL;
1275 static void __init i8042_free_aux_ports(void)
1279 for (i = I8042_AUX_PORT_NO; i < I8042_NUM_PORTS; i++) {
1280 kfree(i8042_ports[i].serio);
1281 i8042_ports[i].serio = NULL;
1285 static void __init i8042_register_ports(void)
1289 for (i = 0; i < I8042_NUM_PORTS; i++) {
1290 if (i8042_ports[i].serio) {
1291 printk(KERN_INFO "serio: %s at %#lx,%#lx irq %d\n",
1292 i8042_ports[i].serio->name,
1293 (unsigned long) I8042_DATA_REG,
1294 (unsigned long) I8042_COMMAND_REG,
1295 i8042_ports[i].irq);
1296 serio_register_port(i8042_ports[i].serio);
1301 static void __devexit i8042_unregister_ports(void)
1305 for (i = 0; i < I8042_NUM_PORTS; i++) {
1306 if (i8042_ports[i].serio) {
1307 serio_unregister_port(i8042_ports[i].serio);
1308 i8042_ports[i].serio = NULL;
1314 * Checks whether port belongs to i8042 controller.
1316 bool i8042_check_port_owner(const struct serio *port)
1320 for (i = 0; i < I8042_NUM_PORTS; i++)
1321 if (i8042_ports[i].serio == port)
1326 EXPORT_SYMBOL(i8042_check_port_owner);
1328 static void i8042_free_irqs(void)
1330 if (i8042_aux_irq_registered)
1331 free_irq(I8042_AUX_IRQ, i8042_platform_device);
1332 if (i8042_kbd_irq_registered)
1333 free_irq(I8042_KBD_IRQ, i8042_platform_device);
1335 i8042_aux_irq_registered = i8042_kbd_irq_registered = false;
1338 static int __init i8042_setup_aux(void)
1340 int (*aux_enable)(void);
1344 if (i8042_check_aux())
1347 if (i8042_nomux || i8042_check_mux()) {
1348 error = i8042_create_aux_port(-1);
1350 goto err_free_ports;
1351 aux_enable = i8042_enable_aux_port;
1353 for (i = 0; i < I8042_NUM_MUX_PORTS; i++) {
1354 error = i8042_create_aux_port(i);
1356 goto err_free_ports;
1358 aux_enable = i8042_enable_mux_ports;
1361 error = request_irq(I8042_AUX_IRQ, i8042_interrupt, IRQF_SHARED,
1362 "i8042", i8042_platform_device);
1364 goto err_free_ports;
1369 i8042_aux_irq_registered = true;
1373 free_irq(I8042_AUX_IRQ, i8042_platform_device);
1375 i8042_free_aux_ports();
1379 static int __init i8042_setup_kbd(void)
1383 error = i8042_create_kbd_port();
1387 error = request_irq(I8042_KBD_IRQ, i8042_interrupt, IRQF_SHARED,
1388 "i8042", i8042_platform_device);
1392 error = i8042_enable_kbd_port();
1396 i8042_kbd_irq_registered = true;
1400 free_irq(I8042_KBD_IRQ, i8042_platform_device);
1402 i8042_free_kbd_port();
1406 static int __init i8042_probe(struct platform_device *dev)
1410 i8042_platform_device = dev;
1413 error = i8042_controller_selftest();
1418 error = i8042_controller_init();
1424 i8042_dritek_enable();
1428 error = i8042_setup_aux();
1429 if (error && error != -ENODEV && error != -EBUSY)
1434 error = i8042_setup_kbd();
1439 * Ok, everything is ready, let's register all serio ports
1441 i8042_register_ports();
1446 i8042_free_aux_ports(); /* in case KBD failed but AUX not */
1448 i8042_controller_reset(false);
1449 i8042_platform_device = NULL;
1454 static int __devexit i8042_remove(struct platform_device *dev)
1456 i8042_unregister_ports();
1458 i8042_controller_reset(false);
1459 i8042_platform_device = NULL;
1464 static struct platform_driver i8042_driver = {
1467 .owner = THIS_MODULE,
1469 .pm = &i8042_pm_ops,
1472 .remove = __devexit_p(i8042_remove),
1473 .shutdown = i8042_shutdown,
1476 static int __init i8042_init(void)
1478 struct platform_device *pdev;
1483 err = i8042_platform_init();
1487 err = i8042_controller_check();
1489 goto err_platform_exit;
1491 pdev = platform_create_bundle(&i8042_driver, i8042_probe, NULL, 0, NULL, 0);
1493 err = PTR_ERR(pdev);
1494 goto err_platform_exit;
1497 panic_blink = i8042_panic_blink;
1502 i8042_platform_exit();
1506 static void __exit i8042_exit(void)
1508 platform_device_unregister(i8042_platform_device);
1509 platform_driver_unregister(&i8042_driver);
1510 i8042_platform_exit();
1515 module_init(i8042_init);
1516 module_exit(i8042_exit);