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 static bool i8042_reset
;
52 module_param_named(reset
, i8042_reset
, bool, 0);
53 MODULE_PARM_DESC(reset
, "Reset controller during init and cleanup.");
55 static bool i8042_direct
;
56 module_param_named(direct
, i8042_direct
, bool, 0);
57 MODULE_PARM_DESC(direct
, "Put keyboard port into non-translated mode.");
59 static bool i8042_dumbkbd
;
60 module_param_named(dumbkbd
, i8042_dumbkbd
, bool, 0);
61 MODULE_PARM_DESC(dumbkbd
, "Pretend that controller can only read data from keyboard");
63 static bool i8042_noloop
;
64 module_param_named(noloop
, i8042_noloop
, bool, 0);
65 MODULE_PARM_DESC(noloop
, "Disable the AUX Loopback command while probing for the AUX port");
67 static bool i8042_notimeout
;
68 module_param_named(notimeout
, i8042_notimeout
, bool, 0);
69 MODULE_PARM_DESC(notimeout
, "Ignore timeouts signalled by i8042");
71 static bool i8042_kbdreset
;
72 module_param_named(kbdreset
, i8042_kbdreset
, bool, 0);
73 MODULE_PARM_DESC(kbdreset
, "Reset device connected to KBD port");
76 static bool i8042_dritek
;
77 module_param_named(dritek
, i8042_dritek
, bool, 0);
78 MODULE_PARM_DESC(dritek
, "Force enable the Dritek keyboard extension");
82 static bool i8042_nopnp
;
83 module_param_named(nopnp
, i8042_nopnp
, bool, 0);
84 MODULE_PARM_DESC(nopnp
, "Do not use PNP to detect controller settings");
89 static bool i8042_debug
;
90 module_param_named(debug
, i8042_debug
, bool, 0600);
91 MODULE_PARM_DESC(debug
, "Turn i8042 debugging mode on and off");
93 static bool i8042_unmask_kbd_data
;
94 module_param_named(unmask_kbd_data
, i8042_unmask_kbd_data
, bool, 0600);
95 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]");
98 static bool i8042_bypass_aux_irq_test
;
99 static char i8042_kbd_firmware_id
[128];
100 static char i8042_aux_firmware_id
[128];
105 * i8042_lock protects serialization between i8042_command and
106 * the interrupt handler.
108 static DEFINE_SPINLOCK(i8042_lock
);
111 * Writers to AUX and KBD ports as well as users issuing i8042_command
112 * directly should acquire i8042_mutex (by means of calling
113 * i8042_lock_chip() and i8042_unlock_ship() helpers) to ensure that
114 * they do not disturb each other (unfortunately in many i8042
115 * implementations write to one of the ports will immediately abort
116 * command that is being processed by another port).
118 static DEFINE_MUTEX(i8042_mutex
);
128 #define I8042_KBD_PORT_NO 0
129 #define I8042_AUX_PORT_NO 1
130 #define I8042_MUX_PORT_NO 2
131 #define I8042_NUM_PORTS (I8042_NUM_MUX_PORTS + 2)
133 static struct i8042_port i8042_ports
[I8042_NUM_PORTS
];
135 static unsigned char i8042_initial_ctr
;
136 static unsigned char i8042_ctr
;
137 static bool i8042_mux_present
;
138 static bool i8042_kbd_irq_registered
;
139 static bool i8042_aux_irq_registered
;
140 static unsigned char i8042_suppress_kbd_ack
;
141 static struct platform_device
*i8042_platform_device
;
142 static struct notifier_block i8042_kbd_bind_notifier_block
;
144 static irqreturn_t
i8042_interrupt(int irq
, void *dev_id
);
145 static bool (*i8042_platform_filter
)(unsigned char data
, unsigned char str
,
146 struct serio
*serio
);
148 void i8042_lock_chip(void)
150 mutex_lock(&i8042_mutex
);
152 EXPORT_SYMBOL(i8042_lock_chip
);
154 void i8042_unlock_chip(void)
156 mutex_unlock(&i8042_mutex
);
158 EXPORT_SYMBOL(i8042_unlock_chip
);
160 int i8042_install_filter(bool (*filter
)(unsigned char data
, unsigned char str
,
161 struct serio
*serio
))
166 spin_lock_irqsave(&i8042_lock
, flags
);
168 if (i8042_platform_filter
) {
173 i8042_platform_filter
= filter
;
176 spin_unlock_irqrestore(&i8042_lock
, flags
);
179 EXPORT_SYMBOL(i8042_install_filter
);
181 int i8042_remove_filter(bool (*filter
)(unsigned char data
, unsigned char str
,
187 spin_lock_irqsave(&i8042_lock
, flags
);
189 if (i8042_platform_filter
!= filter
) {
194 i8042_platform_filter
= NULL
;
197 spin_unlock_irqrestore(&i8042_lock
, flags
);
200 EXPORT_SYMBOL(i8042_remove_filter
);
203 * The i8042_wait_read() and i8042_wait_write functions wait for the i8042 to
204 * be ready for reading values from it / writing values to it.
205 * Called always with i8042_lock held.
208 static int i8042_wait_read(void)
212 while ((~i8042_read_status() & I8042_STR_OBF
) && (i
< I8042_CTL_TIMEOUT
)) {
216 return -(i
== I8042_CTL_TIMEOUT
);
219 static int i8042_wait_write(void)
223 while ((i8042_read_status() & I8042_STR_IBF
) && (i
< I8042_CTL_TIMEOUT
)) {
227 return -(i
== I8042_CTL_TIMEOUT
);
231 * i8042_flush() flushes all data that may be in the keyboard and mouse buffers
232 * of the i8042 down the toilet.
235 static int i8042_flush(void)
238 unsigned char data
, str
;
242 spin_lock_irqsave(&i8042_lock
, flags
);
244 while ((str
= i8042_read_status()) & I8042_STR_OBF
) {
245 if (count
++ < I8042_BUFFER_SIZE
) {
247 data
= i8042_read_data();
248 dbg("%02x <- i8042 (flush, %s)\n",
249 data
, str
& I8042_STR_AUXDATA
? "aux" : "kbd");
256 spin_unlock_irqrestore(&i8042_lock
, flags
);
262 * i8042_command() executes a command on the i8042. It also sends the input
263 * parameter(s) of the commands to it, and receives the output value(s). The
264 * parameters are to be stored in the param array, and the output is placed
265 * into the same array. The number of the parameters and output values is
266 * encoded in bits 8-11 of the command number.
269 static int __i8042_command(unsigned char *param
, int command
)
273 if (i8042_noloop
&& command
== I8042_CMD_AUX_LOOP
)
276 error
= i8042_wait_write();
280 dbg("%02x -> i8042 (command)\n", command
& 0xff);
281 i8042_write_command(command
& 0xff);
283 for (i
= 0; i
< ((command
>> 12) & 0xf); i
++) {
284 error
= i8042_wait_write();
287 dbg("%02x -> i8042 (parameter)\n", param
[i
]);
288 i8042_write_data(param
[i
]);
291 for (i
= 0; i
< ((command
>> 8) & 0xf); i
++) {
292 error
= i8042_wait_read();
294 dbg(" -- i8042 (timeout)\n");
298 if (command
== I8042_CMD_AUX_LOOP
&&
299 !(i8042_read_status() & I8042_STR_AUXDATA
)) {
300 dbg(" -- i8042 (auxerr)\n");
304 param
[i
] = i8042_read_data();
305 dbg("%02x <- i8042 (return)\n", param
[i
]);
311 int i8042_command(unsigned char *param
, int command
)
316 spin_lock_irqsave(&i8042_lock
, flags
);
317 retval
= __i8042_command(param
, command
);
318 spin_unlock_irqrestore(&i8042_lock
, flags
);
322 EXPORT_SYMBOL(i8042_command
);
325 * i8042_kbd_write() sends a byte out through the keyboard interface.
328 static int i8042_kbd_write(struct serio
*port
, unsigned char c
)
333 spin_lock_irqsave(&i8042_lock
, flags
);
335 if (!(retval
= i8042_wait_write())) {
336 dbg("%02x -> i8042 (kbd-data)\n", c
);
340 spin_unlock_irqrestore(&i8042_lock
, flags
);
346 * i8042_aux_write() sends a byte out through the aux interface.
349 static int i8042_aux_write(struct serio
*serio
, unsigned char c
)
351 struct i8042_port
*port
= serio
->port_data
;
353 return i8042_command(&c
, port
->mux
== -1 ?
355 I8042_CMD_MUX_SEND
+ port
->mux
);
360 * i8042_aux_close attempts to clear AUX or KBD port state by disabling
361 * and then re-enabling it.
364 static void i8042_port_close(struct serio
*serio
)
368 const char *port_name
;
370 if (serio
== i8042_ports
[I8042_AUX_PORT_NO
].serio
) {
371 irq_bit
= I8042_CTR_AUXINT
;
372 disable_bit
= I8042_CTR_AUXDIS
;
375 irq_bit
= I8042_CTR_KBDINT
;
376 disable_bit
= I8042_CTR_KBDDIS
;
380 i8042_ctr
&= ~irq_bit
;
381 if (i8042_command(&i8042_ctr
, I8042_CMD_CTL_WCTR
))
382 pr_warn("Can't write CTR while closing %s port\n", port_name
);
386 i8042_ctr
&= ~disable_bit
;
387 i8042_ctr
|= irq_bit
;
388 if (i8042_command(&i8042_ctr
, I8042_CMD_CTL_WCTR
))
389 pr_err("Can't reactivate %s port\n", port_name
);
392 * See if there is any data appeared while we were messing with
395 i8042_interrupt(0, NULL
);
399 * i8042_start() is called by serio core when port is about to finish
400 * registering. It will mark port as existing so i8042_interrupt can
401 * start sending data through it.
403 static int i8042_start(struct serio
*serio
)
405 struct i8042_port
*port
= serio
->port_data
;
413 * i8042_stop() marks serio port as non-existing so i8042_interrupt
414 * will not try to send data to the port that is about to go away.
415 * The function is called by serio core as part of unregister procedure.
417 static void i8042_stop(struct serio
*serio
)
419 struct i8042_port
*port
= serio
->port_data
;
421 port
->exists
= false;
424 * We synchronize with both AUX and KBD IRQs because there is
425 * a (very unlikely) chance that AUX IRQ is raised for KBD port
428 synchronize_irq(I8042_AUX_IRQ
);
429 synchronize_irq(I8042_KBD_IRQ
);
434 * i8042_filter() filters out unwanted bytes from the input data stream.
435 * It is called from i8042_interrupt and thus is running with interrupts
436 * off and i8042_lock held.
438 static bool i8042_filter(unsigned char data
, unsigned char str
,
441 if (unlikely(i8042_suppress_kbd_ack
)) {
442 if ((~str
& I8042_STR_AUXDATA
) &&
443 (data
== 0xfa || data
== 0xfe)) {
444 i8042_suppress_kbd_ack
--;
445 dbg("Extra keyboard ACK - filtered out\n");
450 if (i8042_platform_filter
&& i8042_platform_filter(data
, str
, serio
)) {
451 dbg("Filtered out by platform filter\n");
459 * i8042_interrupt() is the most important function in this driver -
460 * it handles the interrupts from the i8042, and sends incoming bytes
461 * to the upper layers.
464 static irqreturn_t
i8042_interrupt(int irq
, void *dev_id
)
466 struct i8042_port
*port
;
469 unsigned char str
, data
;
471 unsigned int port_no
;
475 spin_lock_irqsave(&i8042_lock
, flags
);
477 str
= i8042_read_status();
478 if (unlikely(~str
& I8042_STR_OBF
)) {
479 spin_unlock_irqrestore(&i8042_lock
, flags
);
481 dbg("Interrupt %d, without any data\n", irq
);
486 data
= i8042_read_data();
488 if (i8042_mux_present
&& (str
& I8042_STR_AUXDATA
)) {
489 static unsigned long last_transmit
;
490 static unsigned char last_str
;
493 if (str
& I8042_STR_MUXERR
) {
494 dbg("MUX error, status is %02x, data is %02x\n",
497 * When MUXERR condition is signalled the data register can only contain
498 * 0xfd, 0xfe or 0xff if implementation follows the spec. Unfortunately
499 * it is not always the case. Some KBCs also report 0xfc when there is
500 * nothing connected to the port while others sometimes get confused which
501 * port the data came from and signal error leaving the data intact. They
502 * _do not_ revert to legacy mode (actually I've never seen KBC reverting
503 * to legacy mode yet, when we see one we'll add proper handling).
504 * Anyway, we process 0xfc, 0xfd, 0xfe and 0xff as timeouts, and for the
505 * rest assume that the data came from the same serio last byte
506 * was transmitted (if transmission happened not too long ago).
511 if (time_before(jiffies
, last_transmit
+ HZ
/10)) {
515 /* fall through - report timeout */
518 case 0xfe: dfl
= SERIO_TIMEOUT
; data
= 0xfe; break;
519 case 0xff: dfl
= SERIO_PARITY
; data
= 0xfe; break;
523 port_no
= I8042_MUX_PORT_NO
+ ((str
>> 6) & 3);
525 last_transmit
= jiffies
;
528 dfl
= ((str
& I8042_STR_PARITY
) ? SERIO_PARITY
: 0) |
529 ((str
& I8042_STR_TIMEOUT
&& !i8042_notimeout
) ? SERIO_TIMEOUT
: 0);
531 port_no
= (str
& I8042_STR_AUXDATA
) ?
532 I8042_AUX_PORT_NO
: I8042_KBD_PORT_NO
;
535 port
= &i8042_ports
[port_no
];
536 serio
= port
->exists
? port
->serio
: NULL
;
538 filter_dbg(port
->driver_bound
, data
, "<- i8042 (interrupt, %d, %d%s%s)\n",
540 dfl
& SERIO_PARITY
? ", bad parity" : "",
541 dfl
& SERIO_TIMEOUT
? ", timeout" : "");
543 filtered
= i8042_filter(data
, str
, serio
);
545 spin_unlock_irqrestore(&i8042_lock
, flags
);
547 if (likely(port
->exists
&& !filtered
))
548 serio_interrupt(serio
, data
, dfl
);
551 return IRQ_RETVAL(ret
);
555 * i8042_enable_kbd_port enables keyboard port on chip
558 static int i8042_enable_kbd_port(void)
560 i8042_ctr
&= ~I8042_CTR_KBDDIS
;
561 i8042_ctr
|= I8042_CTR_KBDINT
;
563 if (i8042_command(&i8042_ctr
, I8042_CMD_CTL_WCTR
)) {
564 i8042_ctr
&= ~I8042_CTR_KBDINT
;
565 i8042_ctr
|= I8042_CTR_KBDDIS
;
566 pr_err("Failed to enable KBD port\n");
574 * i8042_enable_aux_port enables AUX (mouse) port on chip
577 static int i8042_enable_aux_port(void)
579 i8042_ctr
&= ~I8042_CTR_AUXDIS
;
580 i8042_ctr
|= I8042_CTR_AUXINT
;
582 if (i8042_command(&i8042_ctr
, I8042_CMD_CTL_WCTR
)) {
583 i8042_ctr
&= ~I8042_CTR_AUXINT
;
584 i8042_ctr
|= I8042_CTR_AUXDIS
;
585 pr_err("Failed to enable AUX port\n");
593 * i8042_enable_mux_ports enables 4 individual AUX ports after
594 * the controller has been switched into Multiplexed mode
597 static int i8042_enable_mux_ports(void)
602 for (i
= 0; i
< I8042_NUM_MUX_PORTS
; i
++) {
603 i8042_command(¶m
, I8042_CMD_MUX_PFX
+ i
);
604 i8042_command(¶m
, I8042_CMD_AUX_ENABLE
);
607 return i8042_enable_aux_port();
611 * i8042_set_mux_mode checks whether the controller has an
612 * active multiplexor and puts the chip into Multiplexed (true)
613 * or Legacy (false) mode.
616 static int i8042_set_mux_mode(bool multiplex
, unsigned char *mux_version
)
619 unsigned char param
, val
;
621 * Get rid of bytes in the queue.
627 * Internal loopback test - send three bytes, they should come back from the
628 * mouse interface, the last should be version.
632 if (i8042_command(¶m
, I8042_CMD_AUX_LOOP
) || param
!= val
)
634 param
= val
= multiplex
? 0x56 : 0xf6;
635 if (i8042_command(¶m
, I8042_CMD_AUX_LOOP
) || param
!= val
)
637 param
= val
= multiplex
? 0xa4 : 0xa5;
638 if (i8042_command(¶m
, I8042_CMD_AUX_LOOP
) || param
== val
)
642 * Workaround for interference with USB Legacy emulation
643 * that causes a v10.12 MUX to be found.
649 *mux_version
= param
;
655 * i8042_check_mux() checks whether the controller supports the PS/2 Active
656 * Multiplexing specification by Synaptics, Phoenix, Insyde and
660 static int __init
i8042_check_mux(void)
662 unsigned char mux_version
;
664 if (i8042_set_mux_mode(true, &mux_version
))
667 pr_info("Detected active multiplexing controller, rev %d.%d\n",
668 (mux_version
>> 4) & 0xf, mux_version
& 0xf);
671 * Disable all muxed ports by disabling AUX.
673 i8042_ctr
|= I8042_CTR_AUXDIS
;
674 i8042_ctr
&= ~I8042_CTR_AUXINT
;
676 if (i8042_command(&i8042_ctr
, I8042_CMD_CTL_WCTR
)) {
677 pr_err("Failed to disable AUX port, can't use MUX\n");
681 i8042_mux_present
= true;
687 * The following is used to test AUX IRQ delivery.
689 static struct completion i8042_aux_irq_delivered __initdata
;
690 static bool i8042_irq_being_tested __initdata
;
692 static irqreturn_t __init
i8042_aux_test_irq(int irq
, void *dev_id
)
695 unsigned char str
, data
;
698 spin_lock_irqsave(&i8042_lock
, flags
);
699 str
= i8042_read_status();
700 if (str
& I8042_STR_OBF
) {
701 data
= i8042_read_data();
702 dbg("%02x <- i8042 (aux_test_irq, %s)\n",
703 data
, str
& I8042_STR_AUXDATA
? "aux" : "kbd");
704 if (i8042_irq_being_tested
&&
705 data
== 0xa5 && (str
& I8042_STR_AUXDATA
))
706 complete(&i8042_aux_irq_delivered
);
709 spin_unlock_irqrestore(&i8042_lock
, flags
);
711 return IRQ_RETVAL(ret
);
715 * i8042_toggle_aux - enables or disables AUX port on i8042 via command and
716 * verifies success by readinng CTR. Used when testing for presence of AUX
719 static int __init
i8042_toggle_aux(bool on
)
724 if (i8042_command(¶m
,
725 on
? I8042_CMD_AUX_ENABLE
: I8042_CMD_AUX_DISABLE
))
728 /* some chips need some time to set the I8042_CTR_AUXDIS bit */
729 for (i
= 0; i
< 100; i
++) {
732 if (i8042_command(¶m
, I8042_CMD_CTL_RCTR
))
735 if (!(param
& I8042_CTR_AUXDIS
) == on
)
743 * i8042_check_aux() applies as much paranoia as it can at detecting
744 * the presence of an AUX interface.
747 static int __init
i8042_check_aux(void)
750 bool irq_registered
= false;
751 bool aux_loop_broken
= false;
756 * Get rid of bytes in the queue.
762 * Internal loopback test - filters out AT-type i8042's. Unfortunately
763 * SiS screwed up and their 5597 doesn't support the LOOP command even
764 * though it has an AUX port.
768 retval
= i8042_command(¶m
, I8042_CMD_AUX_LOOP
);
769 if (retval
|| param
!= 0x5a) {
772 * External connection test - filters out AT-soldered PS/2 i8042's
773 * 0x00 - no error, 0x01-0x03 - clock/data stuck, 0xff - general error
774 * 0xfa - no error on some notebooks which ignore the spec
775 * Because it's common for chipsets to return error on perfectly functioning
776 * AUX ports, we test for this only when the LOOP command failed.
779 if (i8042_command(¶m
, I8042_CMD_AUX_TEST
) ||
780 (param
&& param
!= 0xfa && param
!= 0xff))
784 * If AUX_LOOP completed without error but returned unexpected data
788 aux_loop_broken
= true;
792 * Bit assignment test - filters out PS/2 i8042's in AT mode
795 if (i8042_toggle_aux(false)) {
796 pr_warn("Failed to disable AUX port, but continuing anyway... Is this a SiS?\n");
797 pr_warn("If AUX port is really absent please use the 'i8042.noaux' option\n");
800 if (i8042_toggle_aux(true))
804 * Reset keyboard (needed on some laptops to successfully detect
805 * touchpad, e.g., some Gigabyte laptop models with Elantech
808 if (i8042_kbdreset
) {
809 pr_warn("Attempting to reset device connected to KBD port\n");
810 i8042_kbd_write(NULL
, (unsigned char) 0xff);
814 * Test AUX IRQ delivery to make sure BIOS did not grab the IRQ and
815 * used it for a PCI card or somethig else.
818 if (i8042_noloop
|| i8042_bypass_aux_irq_test
|| aux_loop_broken
) {
820 * Without LOOP command we can't test AUX IRQ delivery. Assume the port
821 * is working and hope we are right.
827 if (request_irq(I8042_AUX_IRQ
, i8042_aux_test_irq
, IRQF_SHARED
,
828 "i8042", i8042_platform_device
))
831 irq_registered
= true;
833 if (i8042_enable_aux_port())
836 spin_lock_irqsave(&i8042_lock
, flags
);
838 init_completion(&i8042_aux_irq_delivered
);
839 i8042_irq_being_tested
= true;
842 retval
= __i8042_command(¶m
, I8042_CMD_AUX_LOOP
& 0xf0ff);
844 spin_unlock_irqrestore(&i8042_lock
, flags
);
849 if (wait_for_completion_timeout(&i8042_aux_irq_delivered
,
850 msecs_to_jiffies(250)) == 0) {
852 * AUX IRQ was never delivered so we need to flush the controller to
853 * get rid of the byte we put there; otherwise keyboard may not work.
855 dbg(" -- i8042 (aux irq test timeout)\n");
863 * Disable the interface.
866 i8042_ctr
|= I8042_CTR_AUXDIS
;
867 i8042_ctr
&= ~I8042_CTR_AUXINT
;
869 if (i8042_command(&i8042_ctr
, I8042_CMD_CTL_WCTR
))
873 free_irq(I8042_AUX_IRQ
, i8042_platform_device
);
878 static int i8042_controller_check(void)
881 pr_info("No controller found\n");
888 static int i8042_controller_selftest(void)
894 * We try this 5 times; on some really fragile systems this does not
895 * take the first time...
899 if (i8042_command(¶m
, I8042_CMD_CTL_TEST
)) {
900 pr_err("i8042 controller selftest timeout\n");
904 if (param
== I8042_RET_CTL_TEST
)
907 dbg("i8042 controller selftest: %#x != %#x\n",
908 param
, I8042_RET_CTL_TEST
);
914 * On x86, we don't fail entire i8042 initialization if controller
915 * reset fails in hopes that keyboard port will still be functional
916 * and user will still get a working keyboard. This is especially
917 * important on netbooks. On other arches we trust hardware more.
919 pr_info("giving up on controller selftest, continuing anyway...\n");
922 pr_err("i8042 controller selftest failed\n");
928 * i8042_controller init initializes the i8042 controller, and,
929 * most importantly, sets it into non-xlated mode if that's
933 static int i8042_controller_init(void)
937 unsigned char ctr
[2];
940 * Save the CTR for restore on unload / reboot.
945 pr_err("Unable to get stable CTR read\n");
952 if (i8042_command(&ctr
[n
++ % 2], I8042_CMD_CTL_RCTR
)) {
953 pr_err("Can't read CTR while initializing i8042\n");
957 } while (n
< 2 || ctr
[0] != ctr
[1]);
959 i8042_initial_ctr
= i8042_ctr
= ctr
[0];
962 * Disable the keyboard interface and interrupt.
965 i8042_ctr
|= I8042_CTR_KBDDIS
;
966 i8042_ctr
&= ~I8042_CTR_KBDINT
;
972 spin_lock_irqsave(&i8042_lock
, flags
);
973 if (~i8042_read_status() & I8042_STR_KEYLOCK
) {
975 i8042_ctr
|= I8042_CTR_IGNKEYLOCK
;
977 pr_warn("Warning: Keylock active\n");
979 spin_unlock_irqrestore(&i8042_lock
, flags
);
982 * If the chip is configured into nontranslated mode by the BIOS, don't
983 * bother enabling translating and be happy.
986 if (~i8042_ctr
& I8042_CTR_XLATE
)
990 * Set nontranslated mode for the kbd interface if requested by an option.
991 * After this the kbd interface becomes a simple serial in/out, like the aux
992 * interface is. We don't do this by default, since it can confuse notebook
997 i8042_ctr
&= ~I8042_CTR_XLATE
;
1003 if (i8042_command(&i8042_ctr
, I8042_CMD_CTL_WCTR
)) {
1004 pr_err("Can't write CTR while initializing i8042\n");
1009 * Flush whatever accumulated while we were disabling keyboard port.
1019 * Reset the controller and reset CRT to the original value set by BIOS.
1022 static void i8042_controller_reset(bool force_reset
)
1027 * Disable both KBD and AUX interfaces so they don't get in the way
1030 i8042_ctr
|= I8042_CTR_KBDDIS
| I8042_CTR_AUXDIS
;
1031 i8042_ctr
&= ~(I8042_CTR_KBDINT
| I8042_CTR_AUXINT
);
1033 if (i8042_command(&i8042_ctr
, I8042_CMD_CTL_WCTR
))
1034 pr_warn("Can't write CTR while resetting\n");
1037 * Disable MUX mode if present.
1040 if (i8042_mux_present
)
1041 i8042_set_mux_mode(false, NULL
);
1044 * Reset the controller if requested.
1047 if (i8042_reset
|| force_reset
)
1048 i8042_controller_selftest();
1051 * Restore the original control register setting.
1054 if (i8042_command(&i8042_initial_ctr
, I8042_CMD_CTL_WCTR
))
1055 pr_warn("Can't restore CTR\n");
1060 * i8042_panic_blink() will turn the keyboard LEDs on or off and is called
1061 * when kernel panics. Flashing LEDs is useful for users running X who may
1062 * not see the console and will help distinguishing panics from "real"
1065 * Note that DELAY has a limit of 10ms so we will not get stuck here
1066 * waiting for KBC to free up even if KBD interrupt is off
1069 #define DELAY do { mdelay(1); if (++delay > 10) return delay; } while(0)
1071 static long i8042_panic_blink(int state
)
1076 led
= (state
) ? 0x01 | 0x04 : 0;
1077 while (i8042_read_status() & I8042_STR_IBF
)
1079 dbg("%02x -> i8042 (panic blink)\n", 0xed);
1080 i8042_suppress_kbd_ack
= 2;
1081 i8042_write_data(0xed); /* set leds */
1083 while (i8042_read_status() & I8042_STR_IBF
)
1086 dbg("%02x -> i8042 (panic blink)\n", led
);
1087 i8042_write_data(led
);
1095 static void i8042_dritek_enable(void)
1097 unsigned char param
= 0x90;
1100 error
= i8042_command(¶m
, 0x1059);
1102 pr_warn("Failed to enable DRITEK extension: %d\n", error
);
1109 * Here we try to reset everything back to a state we had
1110 * before suspending.
1113 static int i8042_controller_resume(bool force_reset
)
1117 error
= i8042_controller_check();
1121 if (i8042_reset
|| force_reset
) {
1122 error
= i8042_controller_selftest();
1128 * Restore original CTR value and disable all ports
1131 i8042_ctr
= i8042_initial_ctr
;
1133 i8042_ctr
&= ~I8042_CTR_XLATE
;
1134 i8042_ctr
|= I8042_CTR_AUXDIS
| I8042_CTR_KBDDIS
;
1135 i8042_ctr
&= ~(I8042_CTR_AUXINT
| I8042_CTR_KBDINT
);
1136 if (i8042_command(&i8042_ctr
, I8042_CMD_CTL_WCTR
)) {
1137 pr_warn("Can't write CTR to resume, retrying...\n");
1139 if (i8042_command(&i8042_ctr
, I8042_CMD_CTL_WCTR
)) {
1140 pr_err("CTR write retry failed\n");
1148 i8042_dritek_enable();
1151 if (i8042_mux_present
) {
1152 if (i8042_set_mux_mode(true, NULL
) || i8042_enable_mux_ports())
1153 pr_warn("failed to resume active multiplexor, mouse won't work\n");
1154 } else if (i8042_ports
[I8042_AUX_PORT_NO
].serio
)
1155 i8042_enable_aux_port();
1157 if (i8042_ports
[I8042_KBD_PORT_NO
].serio
)
1158 i8042_enable_kbd_port();
1160 i8042_interrupt(0, NULL
);
1166 * Here we try to restore the original BIOS settings to avoid
1170 static int i8042_pm_suspend(struct device
*dev
)
1174 if (pm_suspend_via_firmware())
1175 i8042_controller_reset(true);
1177 /* Set up serio interrupts for system wakeup. */
1178 for (i
= 0; i
< I8042_NUM_PORTS
; i
++) {
1179 struct serio
*serio
= i8042_ports
[i
].serio
;
1181 if (serio
&& device_may_wakeup(&serio
->dev
))
1182 enable_irq_wake(i8042_ports
[i
].irq
);
1188 static int i8042_pm_resume_noirq(struct device
*dev
)
1190 if (!pm_resume_via_firmware())
1191 i8042_interrupt(0, NULL
);
1196 static int i8042_pm_resume(struct device
*dev
)
1201 for (i
= 0; i
< I8042_NUM_PORTS
; i
++) {
1202 struct serio
*serio
= i8042_ports
[i
].serio
;
1204 if (serio
&& device_may_wakeup(&serio
->dev
))
1205 disable_irq_wake(i8042_ports
[i
].irq
);
1209 * If platform firmware was not going to be involved in suspend, we did
1210 * not restore the controller state to whatever it had been at boot
1211 * time, so we do not need to do anything.
1213 if (!pm_suspend_via_firmware())
1217 * We only need to reset the controller if we are resuming after handing
1218 * off control to the platform firmware, otherwise we can simply restore
1221 force_reset
= pm_resume_via_firmware();
1223 return i8042_controller_resume(force_reset
);
1226 static int i8042_pm_thaw(struct device
*dev
)
1228 i8042_interrupt(0, NULL
);
1233 static int i8042_pm_reset(struct device
*dev
)
1235 i8042_controller_reset(false);
1240 static int i8042_pm_restore(struct device
*dev
)
1242 return i8042_controller_resume(false);
1245 static const struct dev_pm_ops i8042_pm_ops
= {
1246 .suspend
= i8042_pm_suspend
,
1247 .resume_noirq
= i8042_pm_resume_noirq
,
1248 .resume
= i8042_pm_resume
,
1249 .thaw
= i8042_pm_thaw
,
1250 .poweroff
= i8042_pm_reset
,
1251 .restore
= i8042_pm_restore
,
1254 #endif /* CONFIG_PM */
1257 * We need to reset the 8042 back to original mode on system shutdown,
1258 * because otherwise BIOSes will be confused.
1261 static void i8042_shutdown(struct platform_device
*dev
)
1263 i8042_controller_reset(false);
1266 static int __init
i8042_create_kbd_port(void)
1268 struct serio
*serio
;
1269 struct i8042_port
*port
= &i8042_ports
[I8042_KBD_PORT_NO
];
1271 serio
= kzalloc(sizeof(struct serio
), GFP_KERNEL
);
1275 serio
->id
.type
= i8042_direct
? SERIO_8042
: SERIO_8042_XL
;
1276 serio
->write
= i8042_dumbkbd
? NULL
: i8042_kbd_write
;
1277 serio
->start
= i8042_start
;
1278 serio
->stop
= i8042_stop
;
1279 serio
->close
= i8042_port_close
;
1280 serio
->port_data
= port
;
1281 serio
->dev
.parent
= &i8042_platform_device
->dev
;
1282 strlcpy(serio
->name
, "i8042 KBD port", sizeof(serio
->name
));
1283 strlcpy(serio
->phys
, I8042_KBD_PHYS_DESC
, sizeof(serio
->phys
));
1284 strlcpy(serio
->firmware_id
, i8042_kbd_firmware_id
,
1285 sizeof(serio
->firmware_id
));
1287 port
->serio
= serio
;
1288 port
->irq
= I8042_KBD_IRQ
;
1293 static int __init
i8042_create_aux_port(int idx
)
1295 struct serio
*serio
;
1296 int port_no
= idx
< 0 ? I8042_AUX_PORT_NO
: I8042_MUX_PORT_NO
+ idx
;
1297 struct i8042_port
*port
= &i8042_ports
[port_no
];
1299 serio
= kzalloc(sizeof(struct serio
), GFP_KERNEL
);
1303 serio
->id
.type
= SERIO_8042
;
1304 serio
->write
= i8042_aux_write
;
1305 serio
->start
= i8042_start
;
1306 serio
->stop
= i8042_stop
;
1307 serio
->port_data
= port
;
1308 serio
->dev
.parent
= &i8042_platform_device
->dev
;
1310 strlcpy(serio
->name
, "i8042 AUX port", sizeof(serio
->name
));
1311 strlcpy(serio
->phys
, I8042_AUX_PHYS_DESC
, sizeof(serio
->phys
));
1312 strlcpy(serio
->firmware_id
, i8042_aux_firmware_id
,
1313 sizeof(serio
->firmware_id
));
1314 serio
->close
= i8042_port_close
;
1316 snprintf(serio
->name
, sizeof(serio
->name
), "i8042 AUX%d port", idx
);
1317 snprintf(serio
->phys
, sizeof(serio
->phys
), I8042_MUX_PHYS_DESC
, idx
+ 1);
1318 strlcpy(serio
->firmware_id
, i8042_aux_firmware_id
,
1319 sizeof(serio
->firmware_id
));
1322 port
->serio
= serio
;
1324 port
->irq
= I8042_AUX_IRQ
;
1329 static void __init
i8042_free_kbd_port(void)
1331 kfree(i8042_ports
[I8042_KBD_PORT_NO
].serio
);
1332 i8042_ports
[I8042_KBD_PORT_NO
].serio
= NULL
;
1335 static void __init
i8042_free_aux_ports(void)
1339 for (i
= I8042_AUX_PORT_NO
; i
< I8042_NUM_PORTS
; i
++) {
1340 kfree(i8042_ports
[i
].serio
);
1341 i8042_ports
[i
].serio
= NULL
;
1345 static void __init
i8042_register_ports(void)
1349 for (i
= 0; i
< I8042_NUM_PORTS
; i
++) {
1350 struct serio
*serio
= i8042_ports
[i
].serio
;
1353 printk(KERN_INFO
"serio: %s at %#lx,%#lx irq %d\n",
1355 (unsigned long) I8042_DATA_REG
,
1356 (unsigned long) I8042_COMMAND_REG
,
1357 i8042_ports
[i
].irq
);
1358 serio_register_port(serio
);
1359 device_set_wakeup_capable(&serio
->dev
, true);
1364 static void i8042_unregister_ports(void)
1368 for (i
= 0; i
< I8042_NUM_PORTS
; i
++) {
1369 if (i8042_ports
[i
].serio
) {
1370 serio_unregister_port(i8042_ports
[i
].serio
);
1371 i8042_ports
[i
].serio
= NULL
;
1377 * Checks whether port belongs to i8042 controller.
1379 bool i8042_check_port_owner(const struct serio
*port
)
1383 for (i
= 0; i
< I8042_NUM_PORTS
; i
++)
1384 if (i8042_ports
[i
].serio
== port
)
1389 EXPORT_SYMBOL(i8042_check_port_owner
);
1391 static void i8042_free_irqs(void)
1393 if (i8042_aux_irq_registered
)
1394 free_irq(I8042_AUX_IRQ
, i8042_platform_device
);
1395 if (i8042_kbd_irq_registered
)
1396 free_irq(I8042_KBD_IRQ
, i8042_platform_device
);
1398 i8042_aux_irq_registered
= i8042_kbd_irq_registered
= false;
1401 static int __init
i8042_setup_aux(void)
1403 int (*aux_enable
)(void);
1407 if (i8042_check_aux())
1410 if (i8042_nomux
|| i8042_check_mux()) {
1411 error
= i8042_create_aux_port(-1);
1413 goto err_free_ports
;
1414 aux_enable
= i8042_enable_aux_port
;
1416 for (i
= 0; i
< I8042_NUM_MUX_PORTS
; i
++) {
1417 error
= i8042_create_aux_port(i
);
1419 goto err_free_ports
;
1421 aux_enable
= i8042_enable_mux_ports
;
1424 error
= request_irq(I8042_AUX_IRQ
, i8042_interrupt
, IRQF_SHARED
,
1425 "i8042", i8042_platform_device
);
1427 goto err_free_ports
;
1432 i8042_aux_irq_registered
= true;
1436 free_irq(I8042_AUX_IRQ
, i8042_platform_device
);
1438 i8042_free_aux_ports();
1442 static int __init
i8042_setup_kbd(void)
1446 error
= i8042_create_kbd_port();
1450 error
= request_irq(I8042_KBD_IRQ
, i8042_interrupt
, IRQF_SHARED
,
1451 "i8042", i8042_platform_device
);
1455 error
= i8042_enable_kbd_port();
1459 i8042_kbd_irq_registered
= true;
1463 free_irq(I8042_KBD_IRQ
, i8042_platform_device
);
1465 i8042_free_kbd_port();
1469 static int i8042_kbd_bind_notifier(struct notifier_block
*nb
,
1470 unsigned long action
, void *data
)
1472 struct device
*dev
= data
;
1473 struct serio
*serio
= to_serio_port(dev
);
1474 struct i8042_port
*port
= serio
->port_data
;
1476 if (serio
!= i8042_ports
[I8042_KBD_PORT_NO
].serio
)
1480 case BUS_NOTIFY_BOUND_DRIVER
:
1481 port
->driver_bound
= true;
1484 case BUS_NOTIFY_UNBIND_DRIVER
:
1485 port
->driver_bound
= false;
1492 static int __init
i8042_probe(struct platform_device
*dev
)
1496 i8042_platform_device
= dev
;
1499 error
= i8042_controller_selftest();
1504 error
= i8042_controller_init();
1510 i8042_dritek_enable();
1514 error
= i8042_setup_aux();
1515 if (error
&& error
!= -ENODEV
&& error
!= -EBUSY
)
1520 error
= i8042_setup_kbd();
1525 * Ok, everything is ready, let's register all serio ports
1527 i8042_register_ports();
1532 i8042_free_aux_ports(); /* in case KBD failed but AUX not */
1534 i8042_controller_reset(false);
1535 i8042_platform_device
= NULL
;
1540 static int i8042_remove(struct platform_device
*dev
)
1542 i8042_unregister_ports();
1544 i8042_controller_reset(false);
1545 i8042_platform_device
= NULL
;
1550 static struct platform_driver i8042_driver
= {
1554 .pm
= &i8042_pm_ops
,
1557 .remove
= i8042_remove
,
1558 .shutdown
= i8042_shutdown
,
1561 static struct notifier_block i8042_kbd_bind_notifier_block
= {
1562 .notifier_call
= i8042_kbd_bind_notifier
,
1565 static int __init
i8042_init(void)
1567 struct platform_device
*pdev
;
1572 err
= i8042_platform_init();
1576 err
= i8042_controller_check();
1578 goto err_platform_exit
;
1580 pdev
= platform_create_bundle(&i8042_driver
, i8042_probe
, NULL
, 0, NULL
, 0);
1582 err
= PTR_ERR(pdev
);
1583 goto err_platform_exit
;
1586 bus_register_notifier(&serio_bus
, &i8042_kbd_bind_notifier_block
);
1587 panic_blink
= i8042_panic_blink
;
1592 i8042_platform_exit();
1596 static void __exit
i8042_exit(void)
1598 platform_device_unregister(i8042_platform_device
);
1599 platform_driver_unregister(&i8042_driver
);
1600 i8042_platform_exit();
1602 bus_unregister_notifier(&serio_bus
, &i8042_kbd_bind_notifier_block
);
1606 module_init(i8042_init
);
1607 module_exit(i8042_exit
);