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 #include <linux/types.h>
14 #include <linux/delay.h>
15 #include <linux/module.h>
16 #include <linux/interrupt.h>
17 #include <linux/ioport.h>
18 #include <linux/init.h>
19 #include <linux/serio.h>
20 #include <linux/err.h>
21 #include <linux/rcupdate.h>
22 #include <linux/platform_device.h>
23 #include <linux/i8042.h>
27 MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
28 MODULE_DESCRIPTION("i8042 keyboard and mouse controller driver");
29 MODULE_LICENSE("GPL");
31 static bool i8042_nokbd
;
32 module_param_named(nokbd
, i8042_nokbd
, bool, 0);
33 MODULE_PARM_DESC(nokbd
, "Do not probe or use KBD port.");
35 static bool i8042_noaux
;
36 module_param_named(noaux
, i8042_noaux
, bool, 0);
37 MODULE_PARM_DESC(noaux
, "Do not probe or use AUX (mouse) port.");
39 static bool i8042_nomux
;
40 module_param_named(nomux
, i8042_nomux
, bool, 0);
41 MODULE_PARM_DESC(nomux
, "Do not check whether an active multiplexing conrtoller is present.");
43 static bool i8042_unlock
;
44 module_param_named(unlock
, i8042_unlock
, bool, 0);
45 MODULE_PARM_DESC(unlock
, "Ignore keyboard lock.");
47 static bool i8042_reset
;
48 module_param_named(reset
, i8042_reset
, bool, 0);
49 MODULE_PARM_DESC(reset
, "Reset controller during init and cleanup.");
51 static bool i8042_direct
;
52 module_param_named(direct
, i8042_direct
, bool, 0);
53 MODULE_PARM_DESC(direct
, "Put keyboard port into non-translated mode.");
55 static bool i8042_dumbkbd
;
56 module_param_named(dumbkbd
, i8042_dumbkbd
, bool, 0);
57 MODULE_PARM_DESC(dumbkbd
, "Pretend that controller can only read data from keyboard");
59 static bool i8042_noloop
;
60 module_param_named(noloop
, i8042_noloop
, bool, 0);
61 MODULE_PARM_DESC(noloop
, "Disable the AUX Loopback command while probing for the AUX port");
63 static unsigned int i8042_blink_frequency
= 500;
64 module_param_named(panicblink
, i8042_blink_frequency
, uint
, 0600);
65 MODULE_PARM_DESC(panicblink
, "Frequency with which keyboard LEDs should blink when kernel panics");
67 static bool i8042_notimeout
;
68 module_param_named(notimeout
, i8042_notimeout
, bool, 0);
69 MODULE_PARM_DESC(notimeout
, "Ignore timeouts signalled by i8042");
72 static bool i8042_dritek
;
73 module_param_named(dritek
, i8042_dritek
, bool, 0);
74 MODULE_PARM_DESC(dritek
, "Force enable the Dritek keyboard extension");
78 static bool i8042_nopnp
;
79 module_param_named(nopnp
, i8042_nopnp
, bool, 0);
80 MODULE_PARM_DESC(nopnp
, "Do not use PNP to detect controller settings");
85 static bool i8042_debug
;
86 module_param_named(debug
, i8042_debug
, bool, 0600);
87 MODULE_PARM_DESC(debug
, "Turn i8042 debugging mode on and off");
90 static bool i8042_bypass_aux_irq_test
;
95 * i8042_lock protects serialization between i8042_command and
96 * the interrupt handler.
98 static DEFINE_SPINLOCK(i8042_lock
);
101 * Writers to AUX and KBD ports as well as users issuing i8042_command
102 * directly should acquire i8042_mutex (by means of calling
103 * i8042_lock_chip() and i8042_unlock_ship() helpers) to ensure that
104 * they do not disturb each other (unfortunately in many i8042
105 * implementations write to one of the ports will immediately abort
106 * command that is being processed by another port).
108 static DEFINE_MUTEX(i8042_mutex
);
117 #define I8042_KBD_PORT_NO 0
118 #define I8042_AUX_PORT_NO 1
119 #define I8042_MUX_PORT_NO 2
120 #define I8042_NUM_PORTS (I8042_NUM_MUX_PORTS + 2)
122 static struct i8042_port i8042_ports
[I8042_NUM_PORTS
];
124 static unsigned char i8042_initial_ctr
;
125 static unsigned char i8042_ctr
;
126 static bool i8042_mux_present
;
127 static bool i8042_kbd_irq_registered
;
128 static bool i8042_aux_irq_registered
;
129 static unsigned char i8042_suppress_kbd_ack
;
130 static struct platform_device
*i8042_platform_device
;
132 static irqreturn_t
i8042_interrupt(int irq
, void *dev_id
);
134 void i8042_lock_chip(void)
136 mutex_lock(&i8042_mutex
);
138 EXPORT_SYMBOL(i8042_lock_chip
);
140 void i8042_unlock_chip(void)
142 mutex_unlock(&i8042_mutex
);
144 EXPORT_SYMBOL(i8042_unlock_chip
);
147 * The i8042_wait_read() and i8042_wait_write functions wait for the i8042 to
148 * be ready for reading values from it / writing values to it.
149 * Called always with i8042_lock held.
152 static int i8042_wait_read(void)
156 while ((~i8042_read_status() & I8042_STR_OBF
) && (i
< I8042_CTL_TIMEOUT
)) {
160 return -(i
== I8042_CTL_TIMEOUT
);
163 static int i8042_wait_write(void)
167 while ((i8042_read_status() & I8042_STR_IBF
) && (i
< I8042_CTL_TIMEOUT
)) {
171 return -(i
== I8042_CTL_TIMEOUT
);
175 * i8042_flush() flushes all data that may be in the keyboard and mouse buffers
176 * of the i8042 down the toilet.
179 static int i8042_flush(void)
182 unsigned char data
, str
;
185 spin_lock_irqsave(&i8042_lock
, flags
);
187 while (((str
= i8042_read_status()) & I8042_STR_OBF
) && (i
< I8042_BUFFER_SIZE
)) {
189 data
= i8042_read_data();
191 dbg("%02x <- i8042 (flush, %s)", data
,
192 str
& I8042_STR_AUXDATA
? "aux" : "kbd");
195 spin_unlock_irqrestore(&i8042_lock
, flags
);
201 * i8042_command() executes a command on the i8042. It also sends the input
202 * parameter(s) of the commands to it, and receives the output value(s). The
203 * parameters are to be stored in the param array, and the output is placed
204 * into the same array. The number of the parameters and output values is
205 * encoded in bits 8-11 of the command number.
208 static int __i8042_command(unsigned char *param
, int command
)
212 if (i8042_noloop
&& command
== I8042_CMD_AUX_LOOP
)
215 error
= i8042_wait_write();
219 dbg("%02x -> i8042 (command)", command
& 0xff);
220 i8042_write_command(command
& 0xff);
222 for (i
= 0; i
< ((command
>> 12) & 0xf); i
++) {
223 error
= i8042_wait_write();
226 dbg("%02x -> i8042 (parameter)", param
[i
]);
227 i8042_write_data(param
[i
]);
230 for (i
= 0; i
< ((command
>> 8) & 0xf); i
++) {
231 error
= i8042_wait_read();
233 dbg(" -- i8042 (timeout)");
237 if (command
== I8042_CMD_AUX_LOOP
&&
238 !(i8042_read_status() & I8042_STR_AUXDATA
)) {
239 dbg(" -- i8042 (auxerr)");
243 param
[i
] = i8042_read_data();
244 dbg("%02x <- i8042 (return)", param
[i
]);
250 int i8042_command(unsigned char *param
, int command
)
255 spin_lock_irqsave(&i8042_lock
, flags
);
256 retval
= __i8042_command(param
, command
);
257 spin_unlock_irqrestore(&i8042_lock
, flags
);
261 EXPORT_SYMBOL(i8042_command
);
264 * i8042_kbd_write() sends a byte out through the keyboard interface.
267 static int i8042_kbd_write(struct serio
*port
, unsigned char c
)
272 spin_lock_irqsave(&i8042_lock
, flags
);
274 if (!(retval
= i8042_wait_write())) {
275 dbg("%02x -> i8042 (kbd-data)", c
);
279 spin_unlock_irqrestore(&i8042_lock
, flags
);
285 * i8042_aux_write() sends a byte out through the aux interface.
288 static int i8042_aux_write(struct serio
*serio
, unsigned char c
)
290 struct i8042_port
*port
= serio
->port_data
;
292 return i8042_command(&c
, port
->mux
== -1 ?
294 I8042_CMD_MUX_SEND
+ port
->mux
);
299 * i8042_aux_close attempts to clear AUX or KBD port state by disabling
300 * and then re-enabling it.
303 static void i8042_port_close(struct serio
*serio
)
307 const char *port_name
;
309 if (serio
== i8042_ports
[I8042_AUX_PORT_NO
].serio
) {
310 irq_bit
= I8042_CTR_AUXINT
;
311 disable_bit
= I8042_CTR_AUXDIS
;
314 irq_bit
= I8042_CTR_KBDINT
;
315 disable_bit
= I8042_CTR_KBDDIS
;
319 i8042_ctr
&= ~irq_bit
;
320 if (i8042_command(&i8042_ctr
, I8042_CMD_CTL_WCTR
))
322 "i8042.c: Can't write CTR while closing %s port.\n",
327 i8042_ctr
&= ~disable_bit
;
328 i8042_ctr
|= irq_bit
;
329 if (i8042_command(&i8042_ctr
, I8042_CMD_CTL_WCTR
))
330 printk(KERN_ERR
"i8042.c: Can't reactivate %s port.\n",
334 * See if there is any data appeared while we were messing with
337 i8042_interrupt(0, NULL
);
341 * i8042_start() is called by serio core when port is about to finish
342 * registering. It will mark port as existing so i8042_interrupt can
343 * start sending data through it.
345 static int i8042_start(struct serio
*serio
)
347 struct i8042_port
*port
= serio
->port_data
;
355 * i8042_stop() marks serio port as non-existing so i8042_interrupt
356 * will not try to send data to the port that is about to go away.
357 * The function is called by serio core as part of unregister procedure.
359 static void i8042_stop(struct serio
*serio
)
361 struct i8042_port
*port
= serio
->port_data
;
363 port
->exists
= false;
366 * We synchronize with both AUX and KBD IRQs because there is
367 * a (very unlikely) chance that AUX IRQ is raised for KBD port
370 synchronize_irq(I8042_AUX_IRQ
);
371 synchronize_irq(I8042_KBD_IRQ
);
376 * i8042_interrupt() is the most important function in this driver -
377 * it handles the interrupts from the i8042, and sends incoming bytes
378 * to the upper layers.
381 static irqreturn_t
i8042_interrupt(int irq
, void *dev_id
)
383 struct i8042_port
*port
;
385 unsigned char str
, data
;
387 unsigned int port_no
;
390 spin_lock_irqsave(&i8042_lock
, flags
);
391 str
= i8042_read_status();
392 if (unlikely(~str
& I8042_STR_OBF
)) {
393 spin_unlock_irqrestore(&i8042_lock
, flags
);
394 if (irq
) dbg("Interrupt %d, without any data", irq
);
398 data
= i8042_read_data();
399 spin_unlock_irqrestore(&i8042_lock
, flags
);
401 if (i8042_mux_present
&& (str
& I8042_STR_AUXDATA
)) {
402 static unsigned long last_transmit
;
403 static unsigned char last_str
;
406 if (str
& I8042_STR_MUXERR
) {
407 dbg("MUX error, status is %02x, data is %02x", str
, data
);
409 * When MUXERR condition is signalled the data register can only contain
410 * 0xfd, 0xfe or 0xff if implementation follows the spec. Unfortunately
411 * it is not always the case. Some KBCs also report 0xfc when there is
412 * nothing connected to the port while others sometimes get confused which
413 * port the data came from and signal error leaving the data intact. They
414 * _do not_ revert to legacy mode (actually I've never seen KBC reverting
415 * to legacy mode yet, when we see one we'll add proper handling).
416 * Anyway, we process 0xfc, 0xfd, 0xfe and 0xff as timeouts, and for the
417 * rest assume that the data came from the same serio last byte
418 * was transmitted (if transmission happened not too long ago).
423 if (time_before(jiffies
, last_transmit
+ HZ
/10)) {
427 /* fall through - report timeout */
430 case 0xfe: dfl
= SERIO_TIMEOUT
; data
= 0xfe; break;
431 case 0xff: dfl
= SERIO_PARITY
; data
= 0xfe; break;
435 port_no
= I8042_MUX_PORT_NO
+ ((str
>> 6) & 3);
437 last_transmit
= jiffies
;
440 dfl
= ((str
& I8042_STR_PARITY
) ? SERIO_PARITY
: 0) |
441 ((str
& I8042_STR_TIMEOUT
&& !i8042_notimeout
) ? SERIO_TIMEOUT
: 0);
443 port_no
= (str
& I8042_STR_AUXDATA
) ?
444 I8042_AUX_PORT_NO
: I8042_KBD_PORT_NO
;
447 port
= &i8042_ports
[port_no
];
449 dbg("%02x <- i8042 (interrupt, %d, %d%s%s)",
451 dfl
& SERIO_PARITY
? ", bad parity" : "",
452 dfl
& SERIO_TIMEOUT
? ", timeout" : "");
454 if (unlikely(i8042_suppress_kbd_ack
))
455 if (port_no
== I8042_KBD_PORT_NO
&&
456 (data
== 0xfa || data
== 0xfe)) {
457 i8042_suppress_kbd_ack
--;
461 if (likely(port
->exists
))
462 serio_interrupt(port
->serio
, data
, dfl
);
465 return IRQ_RETVAL(ret
);
469 * i8042_enable_kbd_port enables keyboard port on chip
472 static int i8042_enable_kbd_port(void)
474 i8042_ctr
&= ~I8042_CTR_KBDDIS
;
475 i8042_ctr
|= I8042_CTR_KBDINT
;
477 if (i8042_command(&i8042_ctr
, I8042_CMD_CTL_WCTR
)) {
478 i8042_ctr
&= ~I8042_CTR_KBDINT
;
479 i8042_ctr
|= I8042_CTR_KBDDIS
;
480 printk(KERN_ERR
"i8042.c: Failed to enable KBD port.\n");
488 * i8042_enable_aux_port enables AUX (mouse) port on chip
491 static int i8042_enable_aux_port(void)
493 i8042_ctr
&= ~I8042_CTR_AUXDIS
;
494 i8042_ctr
|= I8042_CTR_AUXINT
;
496 if (i8042_command(&i8042_ctr
, I8042_CMD_CTL_WCTR
)) {
497 i8042_ctr
&= ~I8042_CTR_AUXINT
;
498 i8042_ctr
|= I8042_CTR_AUXDIS
;
499 printk(KERN_ERR
"i8042.c: Failed to enable AUX port.\n");
507 * i8042_enable_mux_ports enables 4 individual AUX ports after
508 * the controller has been switched into Multiplexed mode
511 static int i8042_enable_mux_ports(void)
516 for (i
= 0; i
< I8042_NUM_MUX_PORTS
; i
++) {
517 i8042_command(¶m
, I8042_CMD_MUX_PFX
+ i
);
518 i8042_command(¶m
, I8042_CMD_AUX_ENABLE
);
521 return i8042_enable_aux_port();
525 * i8042_set_mux_mode checks whether the controller has an
526 * active multiplexor and puts the chip into Multiplexed (true)
527 * or Legacy (false) mode.
530 static int i8042_set_mux_mode(bool multiplex
, unsigned char *mux_version
)
533 unsigned char param
, val
;
535 * Get rid of bytes in the queue.
541 * Internal loopback test - send three bytes, they should come back from the
542 * mouse interface, the last should be version.
546 if (i8042_command(¶m
, I8042_CMD_AUX_LOOP
) || param
!= val
)
548 param
= val
= multiplex
? 0x56 : 0xf6;
549 if (i8042_command(¶m
, I8042_CMD_AUX_LOOP
) || param
!= val
)
551 param
= val
= multiplex
? 0xa4 : 0xa5;
552 if (i8042_command(¶m
, I8042_CMD_AUX_LOOP
) || param
== val
)
556 * Workaround for interference with USB Legacy emulation
557 * that causes a v10.12 MUX to be found.
563 *mux_version
= param
;
569 * i8042_check_mux() checks whether the controller supports the PS/2 Active
570 * Multiplexing specification by Synaptics, Phoenix, Insyde and
574 static int __init
i8042_check_mux(void)
576 unsigned char mux_version
;
578 if (i8042_set_mux_mode(true, &mux_version
))
581 printk(KERN_INFO
"i8042.c: Detected active multiplexing controller, rev %d.%d.\n",
582 (mux_version
>> 4) & 0xf, mux_version
& 0xf);
585 * Disable all muxed ports by disabling AUX.
587 i8042_ctr
|= I8042_CTR_AUXDIS
;
588 i8042_ctr
&= ~I8042_CTR_AUXINT
;
590 if (i8042_command(&i8042_ctr
, I8042_CMD_CTL_WCTR
)) {
591 printk(KERN_ERR
"i8042.c: Failed to disable AUX port, can't use MUX.\n");
595 i8042_mux_present
= true;
601 * The following is used to test AUX IRQ delivery.
603 static struct completion i8042_aux_irq_delivered __initdata
;
604 static bool i8042_irq_being_tested __initdata
;
606 static irqreturn_t __init
i8042_aux_test_irq(int irq
, void *dev_id
)
609 unsigned char str
, data
;
612 spin_lock_irqsave(&i8042_lock
, flags
);
613 str
= i8042_read_status();
614 if (str
& I8042_STR_OBF
) {
615 data
= i8042_read_data();
616 dbg("%02x <- i8042 (aux_test_irq, %s)",
617 data
, str
& I8042_STR_AUXDATA
? "aux" : "kbd");
618 if (i8042_irq_being_tested
&&
619 data
== 0xa5 && (str
& I8042_STR_AUXDATA
))
620 complete(&i8042_aux_irq_delivered
);
623 spin_unlock_irqrestore(&i8042_lock
, flags
);
625 return IRQ_RETVAL(ret
);
629 * i8042_toggle_aux - enables or disables AUX port on i8042 via command and
630 * verifies success by readinng CTR. Used when testing for presence of AUX
633 static int __init
i8042_toggle_aux(bool on
)
638 if (i8042_command(¶m
,
639 on
? I8042_CMD_AUX_ENABLE
: I8042_CMD_AUX_DISABLE
))
642 /* some chips need some time to set the I8042_CTR_AUXDIS bit */
643 for (i
= 0; i
< 100; i
++) {
646 if (i8042_command(¶m
, I8042_CMD_CTL_RCTR
))
649 if (!(param
& I8042_CTR_AUXDIS
) == on
)
657 * i8042_check_aux() applies as much paranoia as it can at detecting
658 * the presence of an AUX interface.
661 static int __init
i8042_check_aux(void)
664 bool irq_registered
= false;
665 bool aux_loop_broken
= false;
670 * Get rid of bytes in the queue.
676 * Internal loopback test - filters out AT-type i8042's. Unfortunately
677 * SiS screwed up and their 5597 doesn't support the LOOP command even
678 * though it has an AUX port.
682 retval
= i8042_command(¶m
, I8042_CMD_AUX_LOOP
);
683 if (retval
|| param
!= 0x5a) {
686 * External connection test - filters out AT-soldered PS/2 i8042's
687 * 0x00 - no error, 0x01-0x03 - clock/data stuck, 0xff - general error
688 * 0xfa - no error on some notebooks which ignore the spec
689 * Because it's common for chipsets to return error on perfectly functioning
690 * AUX ports, we test for this only when the LOOP command failed.
693 if (i8042_command(¶m
, I8042_CMD_AUX_TEST
) ||
694 (param
&& param
!= 0xfa && param
!= 0xff))
698 * If AUX_LOOP completed without error but returned unexpected data
702 aux_loop_broken
= true;
706 * Bit assignment test - filters out PS/2 i8042's in AT mode
709 if (i8042_toggle_aux(false)) {
710 printk(KERN_WARNING
"Failed to disable AUX port, but continuing anyway... Is this a SiS?\n");
711 printk(KERN_WARNING
"If AUX port is really absent please use the 'i8042.noaux' option.\n");
714 if (i8042_toggle_aux(true))
718 * Test AUX IRQ delivery to make sure BIOS did not grab the IRQ and
719 * used it for a PCI card or somethig else.
722 if (i8042_noloop
|| i8042_bypass_aux_irq_test
|| aux_loop_broken
) {
724 * Without LOOP command we can't test AUX IRQ delivery. Assume the port
725 * is working and hope we are right.
731 if (request_irq(I8042_AUX_IRQ
, i8042_aux_test_irq
, IRQF_SHARED
,
732 "i8042", i8042_platform_device
))
735 irq_registered
= true;
737 if (i8042_enable_aux_port())
740 spin_lock_irqsave(&i8042_lock
, flags
);
742 init_completion(&i8042_aux_irq_delivered
);
743 i8042_irq_being_tested
= true;
746 retval
= __i8042_command(¶m
, I8042_CMD_AUX_LOOP
& 0xf0ff);
748 spin_unlock_irqrestore(&i8042_lock
, flags
);
753 if (wait_for_completion_timeout(&i8042_aux_irq_delivered
,
754 msecs_to_jiffies(250)) == 0) {
756 * AUX IRQ was never delivered so we need to flush the controller to
757 * get rid of the byte we put there; otherwise keyboard may not work.
759 dbg(" -- i8042 (aux irq test timeout)");
767 * Disable the interface.
770 i8042_ctr
|= I8042_CTR_AUXDIS
;
771 i8042_ctr
&= ~I8042_CTR_AUXINT
;
773 if (i8042_command(&i8042_ctr
, I8042_CMD_CTL_WCTR
))
777 free_irq(I8042_AUX_IRQ
, i8042_platform_device
);
782 static int i8042_controller_check(void)
784 if (i8042_flush() == I8042_BUFFER_SIZE
) {
785 printk(KERN_ERR
"i8042.c: No controller found.\n");
792 static int i8042_controller_selftest(void)
801 * We try this 5 times; on some really fragile systems this does not
802 * take the first time...
806 if (i8042_command(¶m
, I8042_CMD_CTL_TEST
)) {
807 printk(KERN_ERR
"i8042.c: i8042 controller self test timeout.\n");
811 if (param
== I8042_RET_CTL_TEST
)
814 printk(KERN_ERR
"i8042.c: i8042 controller selftest failed. (%#x != %#x)\n",
815 param
, I8042_RET_CTL_TEST
);
821 * On x86, we don't fail entire i8042 initialization if controller
822 * reset fails in hopes that keyboard port will still be functional
823 * and user will still get a working keyboard. This is especially
824 * important on netbooks. On other arches we trust hardware more.
827 "i8042: giving up on controller selftest, continuing anyway...\n");
835 * i8042_controller init initializes the i8042 controller, and,
836 * most importantly, sets it into non-xlated mode if that's
840 static int i8042_controller_init(void)
844 unsigned char ctr
[2];
847 * Save the CTR for restore on unload / reboot.
853 "i8042.c: Unable to get stable CTR read.\n");
860 if (i8042_command(&ctr
[n
++ % 2], I8042_CMD_CTL_RCTR
)) {
862 "i8042.c: Can't read CTR while initializing i8042.\n");
866 } while (n
< 2 || ctr
[0] != ctr
[1]);
868 i8042_initial_ctr
= i8042_ctr
= ctr
[0];
871 * Disable the keyboard interface and interrupt.
874 i8042_ctr
|= I8042_CTR_KBDDIS
;
875 i8042_ctr
&= ~I8042_CTR_KBDINT
;
881 spin_lock_irqsave(&i8042_lock
, flags
);
882 if (~i8042_read_status() & I8042_STR_KEYLOCK
) {
884 i8042_ctr
|= I8042_CTR_IGNKEYLOCK
;
886 printk(KERN_WARNING
"i8042.c: Warning: Keylock active.\n");
888 spin_unlock_irqrestore(&i8042_lock
, flags
);
891 * If the chip is configured into nontranslated mode by the BIOS, don't
892 * bother enabling translating and be happy.
895 if (~i8042_ctr
& I8042_CTR_XLATE
)
899 * Set nontranslated mode for the kbd interface if requested by an option.
900 * After this the kbd interface becomes a simple serial in/out, like the aux
901 * interface is. We don't do this by default, since it can confuse notebook
906 i8042_ctr
&= ~I8042_CTR_XLATE
;
912 if (i8042_command(&i8042_ctr
, I8042_CMD_CTL_WCTR
)) {
913 printk(KERN_ERR
"i8042.c: Can't write CTR while initializing i8042.\n");
918 * Flush whatever accumulated while we were disabling keyboard port.
928 * Reset the controller and reset CRT to the original value set by BIOS.
931 static void i8042_controller_reset(void)
936 * Disable both KBD and AUX interfaces so they don't get in the way
939 i8042_ctr
|= I8042_CTR_KBDDIS
| I8042_CTR_AUXDIS
;
940 i8042_ctr
&= ~(I8042_CTR_KBDINT
| I8042_CTR_AUXINT
);
942 if (i8042_command(&i8042_ctr
, I8042_CMD_CTL_WCTR
))
943 printk(KERN_WARNING
"i8042.c: Can't write CTR while resetting.\n");
946 * Disable MUX mode if present.
949 if (i8042_mux_present
)
950 i8042_set_mux_mode(false, NULL
);
953 * Reset the controller if requested.
956 i8042_controller_selftest();
959 * Restore the original control register setting.
962 if (i8042_command(&i8042_initial_ctr
, I8042_CMD_CTL_WCTR
))
963 printk(KERN_WARNING
"i8042.c: Can't restore CTR.\n");
968 * i8042_panic_blink() will flash the keyboard LEDs and is called when
969 * kernel panics. Flashing LEDs is useful for users running X who may
970 * not see the console and will help distingushing panics from "real"
973 * Note that DELAY has a limit of 10ms so we will not get stuck here
974 * waiting for KBC to free up even if KBD interrupt is off
977 #define DELAY do { mdelay(1); if (++delay > 10) return delay; } while(0)
979 static long i8042_panic_blink(long count
)
982 static long last_blink
;
986 * We expect frequency to be about 1/2s. KDB uses about 1s.
987 * Make sure they are different.
989 if (!i8042_blink_frequency
)
991 if (count
- last_blink
< i8042_blink_frequency
)
995 while (i8042_read_status() & I8042_STR_IBF
)
997 dbg("%02x -> i8042 (panic blink)", 0xed);
998 i8042_suppress_kbd_ack
= 2;
999 i8042_write_data(0xed); /* set leds */
1001 while (i8042_read_status() & I8042_STR_IBF
)
1004 dbg("%02x -> i8042 (panic blink)", led
);
1005 i8042_write_data(led
);
1014 static void i8042_dritek_enable(void)
1019 error
= i8042_command(¶m
, 0x1059);
1022 "Failed to enable DRITEK extension: %d\n",
1030 * Here we try to restore the original BIOS settings to avoid
1034 static int i8042_pm_reset(struct device
*dev
)
1036 i8042_controller_reset();
1042 * Here we try to reset everything back to a state we had
1043 * before suspending.
1046 static int i8042_pm_restore(struct device
*dev
)
1050 error
= i8042_controller_check();
1054 error
= i8042_controller_selftest();
1059 * Restore original CTR value and disable all ports
1062 i8042_ctr
= i8042_initial_ctr
;
1064 i8042_ctr
&= ~I8042_CTR_XLATE
;
1065 i8042_ctr
|= I8042_CTR_AUXDIS
| I8042_CTR_KBDDIS
;
1066 i8042_ctr
&= ~(I8042_CTR_AUXINT
| I8042_CTR_KBDINT
);
1067 if (i8042_command(&i8042_ctr
, I8042_CMD_CTL_WCTR
)) {
1068 printk(KERN_WARNING
"i8042: Can't write CTR to resume, retrying...\n");
1070 if (i8042_command(&i8042_ctr
, I8042_CMD_CTL_WCTR
)) {
1071 printk(KERN_ERR
"i8042: CTR write retry failed\n");
1079 i8042_dritek_enable();
1082 if (i8042_mux_present
) {
1083 if (i8042_set_mux_mode(true, NULL
) || i8042_enable_mux_ports())
1085 "i8042: failed to resume active multiplexor, "
1086 "mouse won't work.\n");
1087 } else if (i8042_ports
[I8042_AUX_PORT_NO
].serio
)
1088 i8042_enable_aux_port();
1090 if (i8042_ports
[I8042_KBD_PORT_NO
].serio
)
1091 i8042_enable_kbd_port();
1093 i8042_interrupt(0, NULL
);
1098 static const struct dev_pm_ops i8042_pm_ops
= {
1099 .suspend
= i8042_pm_reset
,
1100 .resume
= i8042_pm_restore
,
1101 .poweroff
= i8042_pm_reset
,
1102 .restore
= i8042_pm_restore
,
1105 #endif /* CONFIG_PM */
1108 * We need to reset the 8042 back to original mode on system shutdown,
1109 * because otherwise BIOSes will be confused.
1112 static void i8042_shutdown(struct platform_device
*dev
)
1114 i8042_controller_reset();
1117 static int __init
i8042_create_kbd_port(void)
1119 struct serio
*serio
;
1120 struct i8042_port
*port
= &i8042_ports
[I8042_KBD_PORT_NO
];
1122 serio
= kzalloc(sizeof(struct serio
), GFP_KERNEL
);
1126 serio
->id
.type
= i8042_direct
? SERIO_8042
: SERIO_8042_XL
;
1127 serio
->write
= i8042_dumbkbd
? NULL
: i8042_kbd_write
;
1128 serio
->start
= i8042_start
;
1129 serio
->stop
= i8042_stop
;
1130 serio
->close
= i8042_port_close
;
1131 serio
->port_data
= port
;
1132 serio
->dev
.parent
= &i8042_platform_device
->dev
;
1133 strlcpy(serio
->name
, "i8042 KBD port", sizeof(serio
->name
));
1134 strlcpy(serio
->phys
, I8042_KBD_PHYS_DESC
, sizeof(serio
->phys
));
1136 port
->serio
= serio
;
1137 port
->irq
= I8042_KBD_IRQ
;
1142 static int __init
i8042_create_aux_port(int idx
)
1144 struct serio
*serio
;
1145 int port_no
= idx
< 0 ? I8042_AUX_PORT_NO
: I8042_MUX_PORT_NO
+ idx
;
1146 struct i8042_port
*port
= &i8042_ports
[port_no
];
1148 serio
= kzalloc(sizeof(struct serio
), GFP_KERNEL
);
1152 serio
->id
.type
= SERIO_8042
;
1153 serio
->write
= i8042_aux_write
;
1154 serio
->start
= i8042_start
;
1155 serio
->stop
= i8042_stop
;
1156 serio
->port_data
= port
;
1157 serio
->dev
.parent
= &i8042_platform_device
->dev
;
1159 strlcpy(serio
->name
, "i8042 AUX port", sizeof(serio
->name
));
1160 strlcpy(serio
->phys
, I8042_AUX_PHYS_DESC
, sizeof(serio
->phys
));
1161 serio
->close
= i8042_port_close
;
1163 snprintf(serio
->name
, sizeof(serio
->name
), "i8042 AUX%d port", idx
);
1164 snprintf(serio
->phys
, sizeof(serio
->phys
), I8042_MUX_PHYS_DESC
, idx
+ 1);
1167 port
->serio
= serio
;
1169 port
->irq
= I8042_AUX_IRQ
;
1174 static void __init
i8042_free_kbd_port(void)
1176 kfree(i8042_ports
[I8042_KBD_PORT_NO
].serio
);
1177 i8042_ports
[I8042_KBD_PORT_NO
].serio
= NULL
;
1180 static void __init
i8042_free_aux_ports(void)
1184 for (i
= I8042_AUX_PORT_NO
; i
< I8042_NUM_PORTS
; i
++) {
1185 kfree(i8042_ports
[i
].serio
);
1186 i8042_ports
[i
].serio
= NULL
;
1190 static void __init
i8042_register_ports(void)
1194 for (i
= 0; i
< I8042_NUM_PORTS
; i
++) {
1195 if (i8042_ports
[i
].serio
) {
1196 printk(KERN_INFO
"serio: %s at %#lx,%#lx irq %d\n",
1197 i8042_ports
[i
].serio
->name
,
1198 (unsigned long) I8042_DATA_REG
,
1199 (unsigned long) I8042_COMMAND_REG
,
1200 i8042_ports
[i
].irq
);
1201 serio_register_port(i8042_ports
[i
].serio
);
1206 static void __devexit
i8042_unregister_ports(void)
1210 for (i
= 0; i
< I8042_NUM_PORTS
; i
++) {
1211 if (i8042_ports
[i
].serio
) {
1212 serio_unregister_port(i8042_ports
[i
].serio
);
1213 i8042_ports
[i
].serio
= NULL
;
1219 * Checks whether port belongs to i8042 controller.
1221 bool i8042_check_port_owner(const struct serio
*port
)
1225 for (i
= 0; i
< I8042_NUM_PORTS
; i
++)
1226 if (i8042_ports
[i
].serio
== port
)
1231 EXPORT_SYMBOL(i8042_check_port_owner
);
1233 static void i8042_free_irqs(void)
1235 if (i8042_aux_irq_registered
)
1236 free_irq(I8042_AUX_IRQ
, i8042_platform_device
);
1237 if (i8042_kbd_irq_registered
)
1238 free_irq(I8042_KBD_IRQ
, i8042_platform_device
);
1240 i8042_aux_irq_registered
= i8042_kbd_irq_registered
= false;
1243 static int __init
i8042_setup_aux(void)
1245 int (*aux_enable
)(void);
1249 if (i8042_check_aux())
1252 if (i8042_nomux
|| i8042_check_mux()) {
1253 error
= i8042_create_aux_port(-1);
1255 goto err_free_ports
;
1256 aux_enable
= i8042_enable_aux_port
;
1258 for (i
= 0; i
< I8042_NUM_MUX_PORTS
; i
++) {
1259 error
= i8042_create_aux_port(i
);
1261 goto err_free_ports
;
1263 aux_enable
= i8042_enable_mux_ports
;
1266 error
= request_irq(I8042_AUX_IRQ
, i8042_interrupt
, IRQF_SHARED
,
1267 "i8042", i8042_platform_device
);
1269 goto err_free_ports
;
1274 i8042_aux_irq_registered
= true;
1278 free_irq(I8042_AUX_IRQ
, i8042_platform_device
);
1280 i8042_free_aux_ports();
1284 static int __init
i8042_setup_kbd(void)
1288 error
= i8042_create_kbd_port();
1292 error
= request_irq(I8042_KBD_IRQ
, i8042_interrupt
, IRQF_SHARED
,
1293 "i8042", i8042_platform_device
);
1297 error
= i8042_enable_kbd_port();
1301 i8042_kbd_irq_registered
= true;
1305 free_irq(I8042_KBD_IRQ
, i8042_platform_device
);
1307 i8042_free_kbd_port();
1311 static int __init
i8042_probe(struct platform_device
*dev
)
1315 error
= i8042_controller_selftest();
1319 error
= i8042_controller_init();
1325 i8042_dritek_enable();
1329 error
= i8042_setup_aux();
1330 if (error
&& error
!= -ENODEV
&& error
!= -EBUSY
)
1335 error
= i8042_setup_kbd();
1340 * Ok, everything is ready, let's register all serio ports
1342 i8042_register_ports();
1347 i8042_free_aux_ports(); /* in case KBD failed but AUX not */
1349 i8042_controller_reset();
1354 static int __devexit
i8042_remove(struct platform_device
*dev
)
1356 i8042_unregister_ports();
1358 i8042_controller_reset();
1363 static struct platform_driver i8042_driver
= {
1366 .owner
= THIS_MODULE
,
1368 .pm
= &i8042_pm_ops
,
1371 .remove
= __devexit_p(i8042_remove
),
1372 .shutdown
= i8042_shutdown
,
1375 static int __init
i8042_init(void)
1381 err
= i8042_platform_init();
1385 err
= i8042_controller_check();
1387 goto err_platform_exit
;
1389 i8042_platform_device
= platform_device_alloc("i8042", -1);
1390 if (!i8042_platform_device
) {
1392 goto err_platform_exit
;
1395 err
= platform_device_add(i8042_platform_device
);
1397 goto err_free_device
;
1399 err
= platform_driver_probe(&i8042_driver
, i8042_probe
);
1401 goto err_del_device
;
1403 panic_blink
= i8042_panic_blink
;
1408 platform_device_del(i8042_platform_device
);
1410 platform_device_put(i8042_platform_device
);
1412 i8042_platform_exit();
1417 static void __exit
i8042_exit(void)
1419 platform_device_unregister(i8042_platform_device
);
1420 platform_driver_unregister(&i8042_driver
);
1421 i8042_platform_exit();
1426 module_init(i8042_init
);
1427 module_exit(i8042_exit
);