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");
71 static bool i8042_dritek
;
72 module_param_named(dritek
, i8042_dritek
, bool, 0);
73 MODULE_PARM_DESC(dritek
, "Force enable the Dritek keyboard extension");
77 static bool i8042_nopnp
;
78 module_param_named(nopnp
, i8042_nopnp
, bool, 0);
79 MODULE_PARM_DESC(nopnp
, "Do not use PNP to detect controller settings");
84 static bool i8042_debug
;
85 module_param_named(debug
, i8042_debug
, bool, 0600);
86 MODULE_PARM_DESC(debug
, "Turn i8042 debugging mode on and off");
89 static bool i8042_bypass_aux_irq_test
;
94 * i8042_lock protects serialization between i8042_command and
95 * the interrupt handler.
97 static DEFINE_SPINLOCK(i8042_lock
);
100 * Writers to AUX and KBD ports as well as users issuing i8042_command
101 * directly should acquire i8042_mutex (by means of calling
102 * i8042_lock_chip() and i8042_unlock_ship() helpers) to ensure that
103 * they do not disturb each other (unfortunately in many i8042
104 * implementations write to one of the ports will immediately abort
105 * command that is being processed by another port).
107 static DEFINE_MUTEX(i8042_mutex
);
116 #define I8042_KBD_PORT_NO 0
117 #define I8042_AUX_PORT_NO 1
118 #define I8042_MUX_PORT_NO 2
119 #define I8042_NUM_PORTS (I8042_NUM_MUX_PORTS + 2)
121 static struct i8042_port i8042_ports
[I8042_NUM_PORTS
];
123 static unsigned char i8042_initial_ctr
;
124 static unsigned char i8042_ctr
;
125 static bool i8042_mux_present
;
126 static bool i8042_kbd_irq_registered
;
127 static bool i8042_aux_irq_registered
;
128 static unsigned char i8042_suppress_kbd_ack
;
129 static struct platform_device
*i8042_platform_device
;
131 static irqreturn_t
i8042_interrupt(int irq
, void *dev_id
);
132 static bool (*i8042_platform_filter
)(unsigned char data
, unsigned char str
,
133 struct serio
*serio
);
135 void i8042_lock_chip(void)
137 mutex_lock(&i8042_mutex
);
139 EXPORT_SYMBOL(i8042_lock_chip
);
141 void i8042_unlock_chip(void)
143 mutex_unlock(&i8042_mutex
);
145 EXPORT_SYMBOL(i8042_unlock_chip
);
147 int i8042_install_filter(bool (*filter
)(unsigned char data
, unsigned char str
,
148 struct serio
*serio
))
153 spin_lock_irqsave(&i8042_lock
, flags
);
155 if (i8042_platform_filter
) {
160 i8042_platform_filter
= filter
;
163 spin_unlock_irqrestore(&i8042_lock
, flags
);
166 EXPORT_SYMBOL(i8042_install_filter
);
168 int i8042_remove_filter(bool (*filter
)(unsigned char data
, unsigned char str
,
174 spin_lock_irqsave(&i8042_lock
, flags
);
176 if (i8042_platform_filter
!= filter
) {
181 i8042_platform_filter
= NULL
;
184 spin_unlock_irqrestore(&i8042_lock
, flags
);
187 EXPORT_SYMBOL(i8042_remove_filter
);
190 * The i8042_wait_read() and i8042_wait_write functions wait for the i8042 to
191 * be ready for reading values from it / writing values to it.
192 * Called always with i8042_lock held.
195 static int i8042_wait_read(void)
199 while ((~i8042_read_status() & I8042_STR_OBF
) && (i
< I8042_CTL_TIMEOUT
)) {
203 return -(i
== I8042_CTL_TIMEOUT
);
206 static int i8042_wait_write(void)
210 while ((i8042_read_status() & I8042_STR_IBF
) && (i
< I8042_CTL_TIMEOUT
)) {
214 return -(i
== I8042_CTL_TIMEOUT
);
218 * i8042_flush() flushes all data that may be in the keyboard and mouse buffers
219 * of the i8042 down the toilet.
222 static int i8042_flush(void)
225 unsigned char data
, str
;
228 spin_lock_irqsave(&i8042_lock
, flags
);
230 while (((str
= i8042_read_status()) & I8042_STR_OBF
) && (i
< I8042_BUFFER_SIZE
)) {
232 data
= i8042_read_data();
234 dbg("%02x <- i8042 (flush, %s)\n",
235 data
, str
& I8042_STR_AUXDATA
? "aux" : "kbd");
238 spin_unlock_irqrestore(&i8042_lock
, flags
);
244 * i8042_command() executes a command on the i8042. It also sends the input
245 * parameter(s) of the commands to it, and receives the output value(s). The
246 * parameters are to be stored in the param array, and the output is placed
247 * into the same array. The number of the parameters and output values is
248 * encoded in bits 8-11 of the command number.
251 static int __i8042_command(unsigned char *param
, int command
)
255 if (i8042_noloop
&& command
== I8042_CMD_AUX_LOOP
)
258 error
= i8042_wait_write();
262 dbg("%02x -> i8042 (command)\n", command
& 0xff);
263 i8042_write_command(command
& 0xff);
265 for (i
= 0; i
< ((command
>> 12) & 0xf); i
++) {
266 error
= i8042_wait_write();
269 dbg("%02x -> i8042 (parameter)\n", param
[i
]);
270 i8042_write_data(param
[i
]);
273 for (i
= 0; i
< ((command
>> 8) & 0xf); i
++) {
274 error
= i8042_wait_read();
276 dbg(" -- i8042 (timeout)\n");
280 if (command
== I8042_CMD_AUX_LOOP
&&
281 !(i8042_read_status() & I8042_STR_AUXDATA
)) {
282 dbg(" -- i8042 (auxerr)\n");
286 param
[i
] = i8042_read_data();
287 dbg("%02x <- i8042 (return)\n", param
[i
]);
293 int i8042_command(unsigned char *param
, int command
)
298 spin_lock_irqsave(&i8042_lock
, flags
);
299 retval
= __i8042_command(param
, command
);
300 spin_unlock_irqrestore(&i8042_lock
, flags
);
304 EXPORT_SYMBOL(i8042_command
);
307 * i8042_kbd_write() sends a byte out through the keyboard interface.
310 static int i8042_kbd_write(struct serio
*port
, unsigned char c
)
315 spin_lock_irqsave(&i8042_lock
, flags
);
317 if (!(retval
= i8042_wait_write())) {
318 dbg("%02x -> i8042 (kbd-data)\n", c
);
322 spin_unlock_irqrestore(&i8042_lock
, flags
);
328 * i8042_aux_write() sends a byte out through the aux interface.
331 static int i8042_aux_write(struct serio
*serio
, unsigned char c
)
333 struct i8042_port
*port
= serio
->port_data
;
335 return i8042_command(&c
, port
->mux
== -1 ?
337 I8042_CMD_MUX_SEND
+ port
->mux
);
342 * i8042_aux_close attempts to clear AUX or KBD port state by disabling
343 * and then re-enabling it.
346 static void i8042_port_close(struct serio
*serio
)
350 const char *port_name
;
352 if (serio
== i8042_ports
[I8042_AUX_PORT_NO
].serio
) {
353 irq_bit
= I8042_CTR_AUXINT
;
354 disable_bit
= I8042_CTR_AUXDIS
;
357 irq_bit
= I8042_CTR_KBDINT
;
358 disable_bit
= I8042_CTR_KBDDIS
;
362 i8042_ctr
&= ~irq_bit
;
363 if (i8042_command(&i8042_ctr
, I8042_CMD_CTL_WCTR
))
364 pr_warn("Can't write CTR while closing %s port\n", port_name
);
368 i8042_ctr
&= ~disable_bit
;
369 i8042_ctr
|= irq_bit
;
370 if (i8042_command(&i8042_ctr
, I8042_CMD_CTL_WCTR
))
371 pr_err("Can't reactivate %s port\n", port_name
);
374 * See if there is any data appeared while we were messing with
377 i8042_interrupt(0, NULL
);
381 * i8042_start() is called by serio core when port is about to finish
382 * registering. It will mark port as existing so i8042_interrupt can
383 * start sending data through it.
385 static int i8042_start(struct serio
*serio
)
387 struct i8042_port
*port
= serio
->port_data
;
395 * i8042_stop() marks serio port as non-existing so i8042_interrupt
396 * will not try to send data to the port that is about to go away.
397 * The function is called by serio core as part of unregister procedure.
399 static void i8042_stop(struct serio
*serio
)
401 struct i8042_port
*port
= serio
->port_data
;
403 port
->exists
= false;
406 * We synchronize with both AUX and KBD IRQs because there is
407 * a (very unlikely) chance that AUX IRQ is raised for KBD port
410 synchronize_irq(I8042_AUX_IRQ
);
411 synchronize_irq(I8042_KBD_IRQ
);
416 * i8042_filter() filters out unwanted bytes from the input data stream.
417 * It is called from i8042_interrupt and thus is running with interrupts
418 * off and i8042_lock held.
420 static bool i8042_filter(unsigned char data
, unsigned char str
,
423 if (unlikely(i8042_suppress_kbd_ack
)) {
424 if ((~str
& I8042_STR_AUXDATA
) &&
425 (data
== 0xfa || data
== 0xfe)) {
426 i8042_suppress_kbd_ack
--;
427 dbg("Extra keyboard ACK - filtered out\n");
432 if (i8042_platform_filter
&& i8042_platform_filter(data
, str
, serio
)) {
433 dbg("Filtered out by platform filter\n");
441 * i8042_interrupt() is the most important function in this driver -
442 * it handles the interrupts from the i8042, and sends incoming bytes
443 * to the upper layers.
446 static irqreturn_t
i8042_interrupt(int irq
, void *dev_id
)
448 struct i8042_port
*port
;
451 unsigned char str
, data
;
453 unsigned int port_no
;
457 spin_lock_irqsave(&i8042_lock
, flags
);
459 str
= i8042_read_status();
460 if (unlikely(~str
& I8042_STR_OBF
)) {
461 spin_unlock_irqrestore(&i8042_lock
, flags
);
463 dbg("Interrupt %d, without any data\n", irq
);
468 data
= i8042_read_data();
470 if (i8042_mux_present
&& (str
& I8042_STR_AUXDATA
)) {
471 static unsigned long last_transmit
;
472 static unsigned char last_str
;
475 if (str
& I8042_STR_MUXERR
) {
476 dbg("MUX error, status is %02x, data is %02x\n",
479 * When MUXERR condition is signalled the data register can only contain
480 * 0xfd, 0xfe or 0xff if implementation follows the spec. Unfortunately
481 * it is not always the case. Some KBCs also report 0xfc when there is
482 * nothing connected to the port while others sometimes get confused which
483 * port the data came from and signal error leaving the data intact. They
484 * _do not_ revert to legacy mode (actually I've never seen KBC reverting
485 * to legacy mode yet, when we see one we'll add proper handling).
486 * Anyway, we process 0xfc, 0xfd, 0xfe and 0xff as timeouts, and for the
487 * rest assume that the data came from the same serio last byte
488 * was transmitted (if transmission happened not too long ago).
493 if (time_before(jiffies
, last_transmit
+ HZ
/10)) {
497 /* fall through - report timeout */
500 case 0xfe: dfl
= SERIO_TIMEOUT
; data
= 0xfe; break;
501 case 0xff: dfl
= SERIO_PARITY
; data
= 0xfe; break;
505 port_no
= I8042_MUX_PORT_NO
+ ((str
>> 6) & 3);
507 last_transmit
= jiffies
;
510 dfl
= ((str
& I8042_STR_PARITY
) ? SERIO_PARITY
: 0) |
511 ((str
& I8042_STR_TIMEOUT
&& !i8042_notimeout
) ? SERIO_TIMEOUT
: 0);
513 port_no
= (str
& I8042_STR_AUXDATA
) ?
514 I8042_AUX_PORT_NO
: I8042_KBD_PORT_NO
;
517 port
= &i8042_ports
[port_no
];
518 serio
= port
->exists
? port
->serio
: NULL
;
520 dbg("%02x <- i8042 (interrupt, %d, %d%s%s)\n",
522 dfl
& SERIO_PARITY
? ", bad parity" : "",
523 dfl
& SERIO_TIMEOUT
? ", timeout" : "");
525 filtered
= i8042_filter(data
, str
, serio
);
527 spin_unlock_irqrestore(&i8042_lock
, flags
);
529 if (likely(port
->exists
&& !filtered
))
530 serio_interrupt(serio
, data
, dfl
);
533 return IRQ_RETVAL(ret
);
537 * i8042_enable_kbd_port enables keyboard port on chip
540 static int i8042_enable_kbd_port(void)
542 i8042_ctr
&= ~I8042_CTR_KBDDIS
;
543 i8042_ctr
|= I8042_CTR_KBDINT
;
545 if (i8042_command(&i8042_ctr
, I8042_CMD_CTL_WCTR
)) {
546 i8042_ctr
&= ~I8042_CTR_KBDINT
;
547 i8042_ctr
|= I8042_CTR_KBDDIS
;
548 pr_err("Failed to enable KBD port\n");
556 * i8042_enable_aux_port enables AUX (mouse) port on chip
559 static int i8042_enable_aux_port(void)
561 i8042_ctr
&= ~I8042_CTR_AUXDIS
;
562 i8042_ctr
|= I8042_CTR_AUXINT
;
564 if (i8042_command(&i8042_ctr
, I8042_CMD_CTL_WCTR
)) {
565 i8042_ctr
&= ~I8042_CTR_AUXINT
;
566 i8042_ctr
|= I8042_CTR_AUXDIS
;
567 pr_err("Failed to enable AUX port\n");
575 * i8042_enable_mux_ports enables 4 individual AUX ports after
576 * the controller has been switched into Multiplexed mode
579 static int i8042_enable_mux_ports(void)
584 for (i
= 0; i
< I8042_NUM_MUX_PORTS
; i
++) {
585 i8042_command(¶m
, I8042_CMD_MUX_PFX
+ i
);
586 i8042_command(¶m
, I8042_CMD_AUX_ENABLE
);
589 return i8042_enable_aux_port();
593 * i8042_set_mux_mode checks whether the controller has an
594 * active multiplexor and puts the chip into Multiplexed (true)
595 * or Legacy (false) mode.
598 static int i8042_set_mux_mode(bool multiplex
, unsigned char *mux_version
)
601 unsigned char param
, val
;
603 * Get rid of bytes in the queue.
609 * Internal loopback test - send three bytes, they should come back from the
610 * mouse interface, the last should be version.
614 if (i8042_command(¶m
, I8042_CMD_AUX_LOOP
) || param
!= val
)
616 param
= val
= multiplex
? 0x56 : 0xf6;
617 if (i8042_command(¶m
, I8042_CMD_AUX_LOOP
) || param
!= val
)
619 param
= val
= multiplex
? 0xa4 : 0xa5;
620 if (i8042_command(¶m
, I8042_CMD_AUX_LOOP
) || param
== val
)
624 * Workaround for interference with USB Legacy emulation
625 * that causes a v10.12 MUX to be found.
631 *mux_version
= param
;
637 * i8042_check_mux() checks whether the controller supports the PS/2 Active
638 * Multiplexing specification by Synaptics, Phoenix, Insyde and
642 static int __init
i8042_check_mux(void)
644 unsigned char mux_version
;
646 if (i8042_set_mux_mode(true, &mux_version
))
649 pr_info("Detected active multiplexing controller, rev %d.%d\n",
650 (mux_version
>> 4) & 0xf, mux_version
& 0xf);
653 * Disable all muxed ports by disabling AUX.
655 i8042_ctr
|= I8042_CTR_AUXDIS
;
656 i8042_ctr
&= ~I8042_CTR_AUXINT
;
658 if (i8042_command(&i8042_ctr
, I8042_CMD_CTL_WCTR
)) {
659 pr_err("Failed to disable AUX port, can't use MUX\n");
663 i8042_mux_present
= true;
669 * The following is used to test AUX IRQ delivery.
671 static struct completion i8042_aux_irq_delivered __initdata
;
672 static bool i8042_irq_being_tested __initdata
;
674 static irqreturn_t __init
i8042_aux_test_irq(int irq
, void *dev_id
)
677 unsigned char str
, data
;
680 spin_lock_irqsave(&i8042_lock
, flags
);
681 str
= i8042_read_status();
682 if (str
& I8042_STR_OBF
) {
683 data
= i8042_read_data();
684 dbg("%02x <- i8042 (aux_test_irq, %s)\n",
685 data
, str
& I8042_STR_AUXDATA
? "aux" : "kbd");
686 if (i8042_irq_being_tested
&&
687 data
== 0xa5 && (str
& I8042_STR_AUXDATA
))
688 complete(&i8042_aux_irq_delivered
);
691 spin_unlock_irqrestore(&i8042_lock
, flags
);
693 return IRQ_RETVAL(ret
);
697 * i8042_toggle_aux - enables or disables AUX port on i8042 via command and
698 * verifies success by readinng CTR. Used when testing for presence of AUX
701 static int __init
i8042_toggle_aux(bool on
)
706 if (i8042_command(¶m
,
707 on
? I8042_CMD_AUX_ENABLE
: I8042_CMD_AUX_DISABLE
))
710 /* some chips need some time to set the I8042_CTR_AUXDIS bit */
711 for (i
= 0; i
< 100; i
++) {
714 if (i8042_command(¶m
, I8042_CMD_CTL_RCTR
))
717 if (!(param
& I8042_CTR_AUXDIS
) == on
)
725 * i8042_check_aux() applies as much paranoia as it can at detecting
726 * the presence of an AUX interface.
729 static int __init
i8042_check_aux(void)
732 bool irq_registered
= false;
733 bool aux_loop_broken
= false;
738 * Get rid of bytes in the queue.
744 * Internal loopback test - filters out AT-type i8042's. Unfortunately
745 * SiS screwed up and their 5597 doesn't support the LOOP command even
746 * though it has an AUX port.
750 retval
= i8042_command(¶m
, I8042_CMD_AUX_LOOP
);
751 if (retval
|| param
!= 0x5a) {
754 * External connection test - filters out AT-soldered PS/2 i8042's
755 * 0x00 - no error, 0x01-0x03 - clock/data stuck, 0xff - general error
756 * 0xfa - no error on some notebooks which ignore the spec
757 * Because it's common for chipsets to return error on perfectly functioning
758 * AUX ports, we test for this only when the LOOP command failed.
761 if (i8042_command(¶m
, I8042_CMD_AUX_TEST
) ||
762 (param
&& param
!= 0xfa && param
!= 0xff))
766 * If AUX_LOOP completed without error but returned unexpected data
770 aux_loop_broken
= true;
774 * Bit assignment test - filters out PS/2 i8042's in AT mode
777 if (i8042_toggle_aux(false)) {
778 pr_warn("Failed to disable AUX port, but continuing anyway... Is this a SiS?\n");
779 pr_warn("If AUX port is really absent please use the 'i8042.noaux' option\n");
782 if (i8042_toggle_aux(true))
786 * Test AUX IRQ delivery to make sure BIOS did not grab the IRQ and
787 * used it for a PCI card or somethig else.
790 if (i8042_noloop
|| i8042_bypass_aux_irq_test
|| aux_loop_broken
) {
792 * Without LOOP command we can't test AUX IRQ delivery. Assume the port
793 * is working and hope we are right.
799 if (request_irq(I8042_AUX_IRQ
, i8042_aux_test_irq
, IRQF_SHARED
,
800 "i8042", i8042_platform_device
))
803 irq_registered
= true;
805 if (i8042_enable_aux_port())
808 spin_lock_irqsave(&i8042_lock
, flags
);
810 init_completion(&i8042_aux_irq_delivered
);
811 i8042_irq_being_tested
= true;
814 retval
= __i8042_command(¶m
, I8042_CMD_AUX_LOOP
& 0xf0ff);
816 spin_unlock_irqrestore(&i8042_lock
, flags
);
821 if (wait_for_completion_timeout(&i8042_aux_irq_delivered
,
822 msecs_to_jiffies(250)) == 0) {
824 * AUX IRQ was never delivered so we need to flush the controller to
825 * get rid of the byte we put there; otherwise keyboard may not work.
827 dbg(" -- i8042 (aux irq test timeout)\n");
835 * Disable the interface.
838 i8042_ctr
|= I8042_CTR_AUXDIS
;
839 i8042_ctr
&= ~I8042_CTR_AUXINT
;
841 if (i8042_command(&i8042_ctr
, I8042_CMD_CTL_WCTR
))
845 free_irq(I8042_AUX_IRQ
, i8042_platform_device
);
850 static int i8042_controller_check(void)
852 if (i8042_flush() == I8042_BUFFER_SIZE
) {
853 pr_err("No controller found\n");
860 static int i8042_controller_selftest(void)
866 * We try this 5 times; on some really fragile systems this does not
867 * take the first time...
871 if (i8042_command(¶m
, I8042_CMD_CTL_TEST
)) {
872 pr_err("i8042 controller selftest timeout\n");
876 if (param
== I8042_RET_CTL_TEST
)
879 dbg("i8042 controller selftest: %#x != %#x\n",
880 param
, I8042_RET_CTL_TEST
);
886 * On x86, we don't fail entire i8042 initialization if controller
887 * reset fails in hopes that keyboard port will still be functional
888 * and user will still get a working keyboard. This is especially
889 * important on netbooks. On other arches we trust hardware more.
891 pr_info("giving up on controller selftest, continuing anyway...\n");
894 pr_err("i8042 controller selftest failed\n");
900 * i8042_controller init initializes the i8042 controller, and,
901 * most importantly, sets it into non-xlated mode if that's
905 static int i8042_controller_init(void)
909 unsigned char ctr
[2];
912 * Save the CTR for restore on unload / reboot.
917 pr_err("Unable to get stable CTR read\n");
924 if (i8042_command(&ctr
[n
++ % 2], I8042_CMD_CTL_RCTR
)) {
925 pr_err("Can't read CTR while initializing i8042\n");
929 } while (n
< 2 || ctr
[0] != ctr
[1]);
931 i8042_initial_ctr
= i8042_ctr
= ctr
[0];
934 * Disable the keyboard interface and interrupt.
937 i8042_ctr
|= I8042_CTR_KBDDIS
;
938 i8042_ctr
&= ~I8042_CTR_KBDINT
;
944 spin_lock_irqsave(&i8042_lock
, flags
);
945 if (~i8042_read_status() & I8042_STR_KEYLOCK
) {
947 i8042_ctr
|= I8042_CTR_IGNKEYLOCK
;
949 pr_warn("Warning: Keylock active\n");
951 spin_unlock_irqrestore(&i8042_lock
, flags
);
954 * If the chip is configured into nontranslated mode by the BIOS, don't
955 * bother enabling translating and be happy.
958 if (~i8042_ctr
& I8042_CTR_XLATE
)
962 * Set nontranslated mode for the kbd interface if requested by an option.
963 * After this the kbd interface becomes a simple serial in/out, like the aux
964 * interface is. We don't do this by default, since it can confuse notebook
969 i8042_ctr
&= ~I8042_CTR_XLATE
;
975 if (i8042_command(&i8042_ctr
, I8042_CMD_CTL_WCTR
)) {
976 pr_err("Can't write CTR while initializing i8042\n");
981 * Flush whatever accumulated while we were disabling keyboard port.
991 * Reset the controller and reset CRT to the original value set by BIOS.
994 static void i8042_controller_reset(bool force_reset
)
999 * Disable both KBD and AUX interfaces so they don't get in the way
1002 i8042_ctr
|= I8042_CTR_KBDDIS
| I8042_CTR_AUXDIS
;
1003 i8042_ctr
&= ~(I8042_CTR_KBDINT
| I8042_CTR_AUXINT
);
1005 if (i8042_command(&i8042_ctr
, I8042_CMD_CTL_WCTR
))
1006 pr_warn("Can't write CTR while resetting\n");
1009 * Disable MUX mode if present.
1012 if (i8042_mux_present
)
1013 i8042_set_mux_mode(false, NULL
);
1016 * Reset the controller if requested.
1019 if (i8042_reset
|| force_reset
)
1020 i8042_controller_selftest();
1023 * Restore the original control register setting.
1026 if (i8042_command(&i8042_initial_ctr
, I8042_CMD_CTL_WCTR
))
1027 pr_warn("Can't restore CTR\n");
1032 * i8042_panic_blink() will turn the keyboard LEDs on or off and is called
1033 * when kernel panics. Flashing LEDs is useful for users running X who may
1034 * not see the console and will help distingushing panics from "real"
1037 * Note that DELAY has a limit of 10ms so we will not get stuck here
1038 * waiting for KBC to free up even if KBD interrupt is off
1041 #define DELAY do { mdelay(1); if (++delay > 10) return delay; } while(0)
1043 static long i8042_panic_blink(int state
)
1048 led
= (state
) ? 0x01 | 0x04 : 0;
1049 while (i8042_read_status() & I8042_STR_IBF
)
1051 dbg("%02x -> i8042 (panic blink)\n", 0xed);
1052 i8042_suppress_kbd_ack
= 2;
1053 i8042_write_data(0xed); /* set leds */
1055 while (i8042_read_status() & I8042_STR_IBF
)
1058 dbg("%02x -> i8042 (panic blink)\n", led
);
1059 i8042_write_data(led
);
1067 static void i8042_dritek_enable(void)
1069 unsigned char param
= 0x90;
1072 error
= i8042_command(¶m
, 0x1059);
1074 pr_warn("Failed to enable DRITEK extension: %d\n", error
);
1081 * Here we try to reset everything back to a state we had
1082 * before suspending.
1085 static int i8042_controller_resume(bool force_reset
)
1089 error
= i8042_controller_check();
1093 if (i8042_reset
|| force_reset
) {
1094 error
= i8042_controller_selftest();
1100 * Restore original CTR value and disable all ports
1103 i8042_ctr
= i8042_initial_ctr
;
1105 i8042_ctr
&= ~I8042_CTR_XLATE
;
1106 i8042_ctr
|= I8042_CTR_AUXDIS
| I8042_CTR_KBDDIS
;
1107 i8042_ctr
&= ~(I8042_CTR_AUXINT
| I8042_CTR_KBDINT
);
1108 if (i8042_command(&i8042_ctr
, I8042_CMD_CTL_WCTR
)) {
1109 pr_warn("Can't write CTR to resume, retrying...\n");
1111 if (i8042_command(&i8042_ctr
, I8042_CMD_CTL_WCTR
)) {
1112 pr_err("CTR write retry failed\n");
1120 i8042_dritek_enable();
1123 if (i8042_mux_present
) {
1124 if (i8042_set_mux_mode(true, NULL
) || i8042_enable_mux_ports())
1125 pr_warn("failed to resume active multiplexor, mouse won't work\n");
1126 } else if (i8042_ports
[I8042_AUX_PORT_NO
].serio
)
1127 i8042_enable_aux_port();
1129 if (i8042_ports
[I8042_KBD_PORT_NO
].serio
)
1130 i8042_enable_kbd_port();
1132 i8042_interrupt(0, NULL
);
1138 * Here we try to restore the original BIOS settings to avoid
1142 static int i8042_pm_suspend(struct device
*dev
)
1144 i8042_controller_reset(true);
1149 static int i8042_pm_resume(struct device
*dev
)
1152 * On resume from S2R we always try to reset the controller
1153 * to bring it in a sane state. (In case of S2D we expect
1154 * BIOS to reset the controller for us.)
1156 return i8042_controller_resume(true);
1159 static int i8042_pm_thaw(struct device
*dev
)
1161 i8042_interrupt(0, NULL
);
1166 static int i8042_pm_reset(struct device
*dev
)
1168 i8042_controller_reset(false);
1173 static int i8042_pm_restore(struct device
*dev
)
1175 return i8042_controller_resume(false);
1178 static const struct dev_pm_ops i8042_pm_ops
= {
1179 .suspend
= i8042_pm_suspend
,
1180 .resume
= i8042_pm_resume
,
1181 .thaw
= i8042_pm_thaw
,
1182 .poweroff
= i8042_pm_reset
,
1183 .restore
= i8042_pm_restore
,
1186 #endif /* CONFIG_PM */
1189 * We need to reset the 8042 back to original mode on system shutdown,
1190 * because otherwise BIOSes will be confused.
1193 static void i8042_shutdown(struct platform_device
*dev
)
1195 i8042_controller_reset(false);
1198 static int __init
i8042_create_kbd_port(void)
1200 struct serio
*serio
;
1201 struct i8042_port
*port
= &i8042_ports
[I8042_KBD_PORT_NO
];
1203 serio
= kzalloc(sizeof(struct serio
), GFP_KERNEL
);
1207 serio
->id
.type
= i8042_direct
? SERIO_8042
: SERIO_8042_XL
;
1208 serio
->write
= i8042_dumbkbd
? NULL
: i8042_kbd_write
;
1209 serio
->start
= i8042_start
;
1210 serio
->stop
= i8042_stop
;
1211 serio
->close
= i8042_port_close
;
1212 serio
->port_data
= port
;
1213 serio
->dev
.parent
= &i8042_platform_device
->dev
;
1214 strlcpy(serio
->name
, "i8042 KBD port", sizeof(serio
->name
));
1215 strlcpy(serio
->phys
, I8042_KBD_PHYS_DESC
, sizeof(serio
->phys
));
1217 port
->serio
= serio
;
1218 port
->irq
= I8042_KBD_IRQ
;
1223 static int __init
i8042_create_aux_port(int idx
)
1225 struct serio
*serio
;
1226 int port_no
= idx
< 0 ? I8042_AUX_PORT_NO
: I8042_MUX_PORT_NO
+ idx
;
1227 struct i8042_port
*port
= &i8042_ports
[port_no
];
1229 serio
= kzalloc(sizeof(struct serio
), GFP_KERNEL
);
1233 serio
->id
.type
= SERIO_8042
;
1234 serio
->write
= i8042_aux_write
;
1235 serio
->start
= i8042_start
;
1236 serio
->stop
= i8042_stop
;
1237 serio
->port_data
= port
;
1238 serio
->dev
.parent
= &i8042_platform_device
->dev
;
1240 strlcpy(serio
->name
, "i8042 AUX port", sizeof(serio
->name
));
1241 strlcpy(serio
->phys
, I8042_AUX_PHYS_DESC
, sizeof(serio
->phys
));
1242 serio
->close
= i8042_port_close
;
1244 snprintf(serio
->name
, sizeof(serio
->name
), "i8042 AUX%d port", idx
);
1245 snprintf(serio
->phys
, sizeof(serio
->phys
), I8042_MUX_PHYS_DESC
, idx
+ 1);
1248 port
->serio
= serio
;
1250 port
->irq
= I8042_AUX_IRQ
;
1255 static void __init
i8042_free_kbd_port(void)
1257 kfree(i8042_ports
[I8042_KBD_PORT_NO
].serio
);
1258 i8042_ports
[I8042_KBD_PORT_NO
].serio
= NULL
;
1261 static void __init
i8042_free_aux_ports(void)
1265 for (i
= I8042_AUX_PORT_NO
; i
< I8042_NUM_PORTS
; i
++) {
1266 kfree(i8042_ports
[i
].serio
);
1267 i8042_ports
[i
].serio
= NULL
;
1271 static void __init
i8042_register_ports(void)
1275 for (i
= 0; i
< I8042_NUM_PORTS
; i
++) {
1276 if (i8042_ports
[i
].serio
) {
1277 printk(KERN_INFO
"serio: %s at %#lx,%#lx irq %d\n",
1278 i8042_ports
[i
].serio
->name
,
1279 (unsigned long) I8042_DATA_REG
,
1280 (unsigned long) I8042_COMMAND_REG
,
1281 i8042_ports
[i
].irq
);
1282 serio_register_port(i8042_ports
[i
].serio
);
1287 static void __devexit
i8042_unregister_ports(void)
1291 for (i
= 0; i
< I8042_NUM_PORTS
; i
++) {
1292 if (i8042_ports
[i
].serio
) {
1293 serio_unregister_port(i8042_ports
[i
].serio
);
1294 i8042_ports
[i
].serio
= NULL
;
1300 * Checks whether port belongs to i8042 controller.
1302 bool i8042_check_port_owner(const struct serio
*port
)
1306 for (i
= 0; i
< I8042_NUM_PORTS
; i
++)
1307 if (i8042_ports
[i
].serio
== port
)
1312 EXPORT_SYMBOL(i8042_check_port_owner
);
1314 static void i8042_free_irqs(void)
1316 if (i8042_aux_irq_registered
)
1317 free_irq(I8042_AUX_IRQ
, i8042_platform_device
);
1318 if (i8042_kbd_irq_registered
)
1319 free_irq(I8042_KBD_IRQ
, i8042_platform_device
);
1321 i8042_aux_irq_registered
= i8042_kbd_irq_registered
= false;
1324 static int __init
i8042_setup_aux(void)
1326 int (*aux_enable
)(void);
1330 if (i8042_check_aux())
1333 if (i8042_nomux
|| i8042_check_mux()) {
1334 error
= i8042_create_aux_port(-1);
1336 goto err_free_ports
;
1337 aux_enable
= i8042_enable_aux_port
;
1339 for (i
= 0; i
< I8042_NUM_MUX_PORTS
; i
++) {
1340 error
= i8042_create_aux_port(i
);
1342 goto err_free_ports
;
1344 aux_enable
= i8042_enable_mux_ports
;
1347 error
= request_irq(I8042_AUX_IRQ
, i8042_interrupt
, IRQF_SHARED
,
1348 "i8042", i8042_platform_device
);
1350 goto err_free_ports
;
1355 i8042_aux_irq_registered
= true;
1359 free_irq(I8042_AUX_IRQ
, i8042_platform_device
);
1361 i8042_free_aux_ports();
1365 static int __init
i8042_setup_kbd(void)
1369 error
= i8042_create_kbd_port();
1373 error
= request_irq(I8042_KBD_IRQ
, i8042_interrupt
, IRQF_SHARED
,
1374 "i8042", i8042_platform_device
);
1378 error
= i8042_enable_kbd_port();
1382 i8042_kbd_irq_registered
= true;
1386 free_irq(I8042_KBD_IRQ
, i8042_platform_device
);
1388 i8042_free_kbd_port();
1392 static int __init
i8042_probe(struct platform_device
*dev
)
1396 i8042_platform_device
= dev
;
1399 error
= i8042_controller_selftest();
1404 error
= i8042_controller_init();
1410 i8042_dritek_enable();
1414 error
= i8042_setup_aux();
1415 if (error
&& error
!= -ENODEV
&& error
!= -EBUSY
)
1420 error
= i8042_setup_kbd();
1425 * Ok, everything is ready, let's register all serio ports
1427 i8042_register_ports();
1432 i8042_free_aux_ports(); /* in case KBD failed but AUX not */
1434 i8042_controller_reset(false);
1435 i8042_platform_device
= NULL
;
1440 static int __devexit
i8042_remove(struct platform_device
*dev
)
1442 i8042_unregister_ports();
1444 i8042_controller_reset(false);
1445 i8042_platform_device
= NULL
;
1450 static struct platform_driver i8042_driver
= {
1453 .owner
= THIS_MODULE
,
1455 .pm
= &i8042_pm_ops
,
1458 .remove
= __devexit_p(i8042_remove
),
1459 .shutdown
= i8042_shutdown
,
1462 static int __init
i8042_init(void)
1464 struct platform_device
*pdev
;
1469 err
= i8042_platform_init();
1473 err
= i8042_controller_check();
1475 goto err_platform_exit
;
1477 pdev
= platform_create_bundle(&i8042_driver
, i8042_probe
, NULL
, 0, NULL
, 0);
1479 err
= PTR_ERR(pdev
);
1480 goto err_platform_exit
;
1483 panic_blink
= i8042_panic_blink
;
1488 i8042_platform_exit();
1492 static void __exit
i8042_exit(void)
1494 platform_device_unregister(i8042_platform_device
);
1495 platform_driver_unregister(&i8042_driver
);
1496 i8042_platform_exit();
1501 module_init(i8042_init
);
1502 module_exit(i8042_exit
);