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");
68 static bool i8042_dritek
;
69 module_param_named(dritek
, i8042_dritek
, bool, 0);
70 MODULE_PARM_DESC(dritek
, "Force enable the Dritek keyboard extension");
74 static bool i8042_nopnp
;
75 module_param_named(nopnp
, i8042_nopnp
, bool, 0);
76 MODULE_PARM_DESC(nopnp
, "Do not use PNP to detect controller settings");
81 static bool i8042_debug
;
82 module_param_named(debug
, i8042_debug
, bool, 0600);
83 MODULE_PARM_DESC(debug
, "Turn i8042 debugging mode on and off");
86 static bool i8042_bypass_aux_irq_test
;
90 static DEFINE_SPINLOCK(i8042_lock
);
99 #define I8042_KBD_PORT_NO 0
100 #define I8042_AUX_PORT_NO 1
101 #define I8042_MUX_PORT_NO 2
102 #define I8042_NUM_PORTS (I8042_NUM_MUX_PORTS + 2)
104 static struct i8042_port i8042_ports
[I8042_NUM_PORTS
];
106 static unsigned char i8042_initial_ctr
;
107 static unsigned char i8042_ctr
;
108 static bool i8042_mux_present
;
109 static bool i8042_kbd_irq_registered
;
110 static bool i8042_aux_irq_registered
;
111 static unsigned char i8042_suppress_kbd_ack
;
112 static struct platform_device
*i8042_platform_device
;
114 static irqreturn_t
i8042_interrupt(int irq
, void *dev_id
);
117 * The i8042_wait_read() and i8042_wait_write functions wait for the i8042 to
118 * be ready for reading values from it / writing values to it.
119 * Called always with i8042_lock held.
122 static int i8042_wait_read(void)
126 while ((~i8042_read_status() & I8042_STR_OBF
) && (i
< I8042_CTL_TIMEOUT
)) {
130 return -(i
== I8042_CTL_TIMEOUT
);
133 static int i8042_wait_write(void)
137 while ((i8042_read_status() & I8042_STR_IBF
) && (i
< I8042_CTL_TIMEOUT
)) {
141 return -(i
== I8042_CTL_TIMEOUT
);
145 * i8042_flush() flushes all data that may be in the keyboard and mouse buffers
146 * of the i8042 down the toilet.
149 static int i8042_flush(void)
152 unsigned char data
, str
;
155 spin_lock_irqsave(&i8042_lock
, flags
);
157 while (((str
= i8042_read_status()) & I8042_STR_OBF
) && (i
< I8042_BUFFER_SIZE
)) {
159 data
= i8042_read_data();
161 dbg("%02x <- i8042 (flush, %s)", data
,
162 str
& I8042_STR_AUXDATA
? "aux" : "kbd");
165 spin_unlock_irqrestore(&i8042_lock
, flags
);
171 * i8042_command() executes a command on the i8042. It also sends the input
172 * parameter(s) of the commands to it, and receives the output value(s). The
173 * parameters are to be stored in the param array, and the output is placed
174 * into the same array. The number of the parameters and output values is
175 * encoded in bits 8-11 of the command number.
178 static int __i8042_command(unsigned char *param
, int command
)
182 if (i8042_noloop
&& command
== I8042_CMD_AUX_LOOP
)
185 error
= i8042_wait_write();
189 dbg("%02x -> i8042 (command)", command
& 0xff);
190 i8042_write_command(command
& 0xff);
192 for (i
= 0; i
< ((command
>> 12) & 0xf); i
++) {
193 error
= i8042_wait_write();
196 dbg("%02x -> i8042 (parameter)", param
[i
]);
197 i8042_write_data(param
[i
]);
200 for (i
= 0; i
< ((command
>> 8) & 0xf); i
++) {
201 error
= i8042_wait_read();
203 dbg(" -- i8042 (timeout)");
207 if (command
== I8042_CMD_AUX_LOOP
&&
208 !(i8042_read_status() & I8042_STR_AUXDATA
)) {
209 dbg(" -- i8042 (auxerr)");
213 param
[i
] = i8042_read_data();
214 dbg("%02x <- i8042 (return)", param
[i
]);
220 int i8042_command(unsigned char *param
, int command
)
225 spin_lock_irqsave(&i8042_lock
, flags
);
226 retval
= __i8042_command(param
, command
);
227 spin_unlock_irqrestore(&i8042_lock
, flags
);
231 EXPORT_SYMBOL(i8042_command
);
234 * i8042_kbd_write() sends a byte out through the keyboard interface.
237 static int i8042_kbd_write(struct serio
*port
, unsigned char c
)
242 spin_lock_irqsave(&i8042_lock
, flags
);
244 if (!(retval
= i8042_wait_write())) {
245 dbg("%02x -> i8042 (kbd-data)", c
);
249 spin_unlock_irqrestore(&i8042_lock
, flags
);
255 * i8042_aux_write() sends a byte out through the aux interface.
258 static int i8042_aux_write(struct serio
*serio
, unsigned char c
)
260 struct i8042_port
*port
= serio
->port_data
;
262 return i8042_command(&c
, port
->mux
== -1 ?
264 I8042_CMD_MUX_SEND
+ port
->mux
);
269 * i8042_aux_close attempts to clear AUX or KBD port state by disabling
270 * and then re-enabling it.
273 static void i8042_port_close(struct serio
*serio
)
277 const char *port_name
;
279 if (serio
== i8042_ports
[I8042_AUX_PORT_NO
].serio
) {
280 irq_bit
= I8042_CTR_AUXINT
;
281 disable_bit
= I8042_CTR_AUXDIS
;
284 irq_bit
= I8042_CTR_KBDINT
;
285 disable_bit
= I8042_CTR_KBDDIS
;
289 i8042_ctr
&= ~irq_bit
;
290 if (i8042_command(&i8042_ctr
, I8042_CMD_CTL_WCTR
))
292 "i8042.c: Can't write CTR while closing %s port.\n",
297 i8042_ctr
&= ~disable_bit
;
298 i8042_ctr
|= irq_bit
;
299 if (i8042_command(&i8042_ctr
, I8042_CMD_CTL_WCTR
))
300 printk(KERN_ERR
"i8042.c: Can't reactivate %s port.\n",
304 * See if there is any data appeared while we were messing with
307 i8042_interrupt(0, NULL
);
311 * i8042_start() is called by serio core when port is about to finish
312 * registering. It will mark port as existing so i8042_interrupt can
313 * start sending data through it.
315 static int i8042_start(struct serio
*serio
)
317 struct i8042_port
*port
= serio
->port_data
;
325 * i8042_stop() marks serio port as non-existing so i8042_interrupt
326 * will not try to send data to the port that is about to go away.
327 * The function is called by serio core as part of unregister procedure.
329 static void i8042_stop(struct serio
*serio
)
331 struct i8042_port
*port
= serio
->port_data
;
333 port
->exists
= false;
336 * We synchronize with both AUX and KBD IRQs because there is
337 * a (very unlikely) chance that AUX IRQ is raised for KBD port
340 synchronize_irq(I8042_AUX_IRQ
);
341 synchronize_irq(I8042_KBD_IRQ
);
346 * i8042_interrupt() is the most important function in this driver -
347 * it handles the interrupts from the i8042, and sends incoming bytes
348 * to the upper layers.
351 static irqreturn_t
i8042_interrupt(int irq
, void *dev_id
)
353 struct i8042_port
*port
;
355 unsigned char str
, data
;
357 unsigned int port_no
;
360 spin_lock_irqsave(&i8042_lock
, flags
);
361 str
= i8042_read_status();
362 if (unlikely(~str
& I8042_STR_OBF
)) {
363 spin_unlock_irqrestore(&i8042_lock
, flags
);
364 if (irq
) dbg("Interrupt %d, without any data", irq
);
368 data
= i8042_read_data();
369 spin_unlock_irqrestore(&i8042_lock
, flags
);
371 if (i8042_mux_present
&& (str
& I8042_STR_AUXDATA
)) {
372 static unsigned long last_transmit
;
373 static unsigned char last_str
;
376 if (str
& I8042_STR_MUXERR
) {
377 dbg("MUX error, status is %02x, data is %02x", str
, data
);
379 * When MUXERR condition is signalled the data register can only contain
380 * 0xfd, 0xfe or 0xff if implementation follows the spec. Unfortunately
381 * it is not always the case. Some KBCs also report 0xfc when there is
382 * nothing connected to the port while others sometimes get confused which
383 * port the data came from and signal error leaving the data intact. They
384 * _do not_ revert to legacy mode (actually I've never seen KBC reverting
385 * to legacy mode yet, when we see one we'll add proper handling).
386 * Anyway, we process 0xfc, 0xfd, 0xfe and 0xff as timeouts, and for the
387 * rest assume that the data came from the same serio last byte
388 * was transmitted (if transmission happened not too long ago).
393 if (time_before(jiffies
, last_transmit
+ HZ
/10)) {
397 /* fall through - report timeout */
400 case 0xfe: dfl
= SERIO_TIMEOUT
; data
= 0xfe; break;
401 case 0xff: dfl
= SERIO_PARITY
; data
= 0xfe; break;
405 port_no
= I8042_MUX_PORT_NO
+ ((str
>> 6) & 3);
407 last_transmit
= jiffies
;
410 dfl
= ((str
& I8042_STR_PARITY
) ? SERIO_PARITY
: 0) |
411 ((str
& I8042_STR_TIMEOUT
) ? SERIO_TIMEOUT
: 0);
413 port_no
= (str
& I8042_STR_AUXDATA
) ?
414 I8042_AUX_PORT_NO
: I8042_KBD_PORT_NO
;
417 port
= &i8042_ports
[port_no
];
419 dbg("%02x <- i8042 (interrupt, %d, %d%s%s)",
421 dfl
& SERIO_PARITY
? ", bad parity" : "",
422 dfl
& SERIO_TIMEOUT
? ", timeout" : "");
424 if (unlikely(i8042_suppress_kbd_ack
))
425 if (port_no
== I8042_KBD_PORT_NO
&&
426 (data
== 0xfa || data
== 0xfe)) {
427 i8042_suppress_kbd_ack
--;
431 if (likely(port
->exists
))
432 serio_interrupt(port
->serio
, data
, dfl
);
435 return IRQ_RETVAL(ret
);
439 * i8042_enable_kbd_port enables keyboard port on chip
442 static int i8042_enable_kbd_port(void)
444 i8042_ctr
&= ~I8042_CTR_KBDDIS
;
445 i8042_ctr
|= I8042_CTR_KBDINT
;
447 if (i8042_command(&i8042_ctr
, I8042_CMD_CTL_WCTR
)) {
448 i8042_ctr
&= ~I8042_CTR_KBDINT
;
449 i8042_ctr
|= I8042_CTR_KBDDIS
;
450 printk(KERN_ERR
"i8042.c: Failed to enable KBD port.\n");
458 * i8042_enable_aux_port enables AUX (mouse) port on chip
461 static int i8042_enable_aux_port(void)
463 i8042_ctr
&= ~I8042_CTR_AUXDIS
;
464 i8042_ctr
|= I8042_CTR_AUXINT
;
466 if (i8042_command(&i8042_ctr
, I8042_CMD_CTL_WCTR
)) {
467 i8042_ctr
&= ~I8042_CTR_AUXINT
;
468 i8042_ctr
|= I8042_CTR_AUXDIS
;
469 printk(KERN_ERR
"i8042.c: Failed to enable AUX port.\n");
477 * i8042_enable_mux_ports enables 4 individual AUX ports after
478 * the controller has been switched into Multiplexed mode
481 static int i8042_enable_mux_ports(void)
486 for (i
= 0; i
< I8042_NUM_MUX_PORTS
; i
++) {
487 i8042_command(¶m
, I8042_CMD_MUX_PFX
+ i
);
488 i8042_command(¶m
, I8042_CMD_AUX_ENABLE
);
491 return i8042_enable_aux_port();
495 * i8042_set_mux_mode checks whether the controller has an
496 * active multiplexor and puts the chip into Multiplexed (true)
497 * or Legacy (false) mode.
500 static int i8042_set_mux_mode(bool multiplex
, unsigned char *mux_version
)
503 unsigned char param
, val
;
505 * Get rid of bytes in the queue.
511 * Internal loopback test - send three bytes, they should come back from the
512 * mouse interface, the last should be version.
516 if (i8042_command(¶m
, I8042_CMD_AUX_LOOP
) || param
!= val
)
518 param
= val
= multiplex
? 0x56 : 0xf6;
519 if (i8042_command(¶m
, I8042_CMD_AUX_LOOP
) || param
!= val
)
521 param
= val
= multiplex
? 0xa4 : 0xa5;
522 if (i8042_command(¶m
, I8042_CMD_AUX_LOOP
) || param
== val
)
526 * Workaround for interference with USB Legacy emulation
527 * that causes a v10.12 MUX to be found.
533 *mux_version
= param
;
539 * i8042_check_mux() checks whether the controller supports the PS/2 Active
540 * Multiplexing specification by Synaptics, Phoenix, Insyde and
544 static int __init
i8042_check_mux(void)
546 unsigned char mux_version
;
548 if (i8042_set_mux_mode(true, &mux_version
))
551 printk(KERN_INFO
"i8042.c: Detected active multiplexing controller, rev %d.%d.\n",
552 (mux_version
>> 4) & 0xf, mux_version
& 0xf);
555 * Disable all muxed ports by disabling AUX.
557 i8042_ctr
|= I8042_CTR_AUXDIS
;
558 i8042_ctr
&= ~I8042_CTR_AUXINT
;
560 if (i8042_command(&i8042_ctr
, I8042_CMD_CTL_WCTR
)) {
561 printk(KERN_ERR
"i8042.c: Failed to disable AUX port, can't use MUX.\n");
565 i8042_mux_present
= true;
571 * The following is used to test AUX IRQ delivery.
573 static struct completion i8042_aux_irq_delivered __initdata
;
574 static bool i8042_irq_being_tested __initdata
;
576 static irqreturn_t __init
i8042_aux_test_irq(int irq
, void *dev_id
)
579 unsigned char str
, data
;
582 spin_lock_irqsave(&i8042_lock
, flags
);
583 str
= i8042_read_status();
584 if (str
& I8042_STR_OBF
) {
585 data
= i8042_read_data();
586 if (i8042_irq_being_tested
&&
587 data
== 0xa5 && (str
& I8042_STR_AUXDATA
))
588 complete(&i8042_aux_irq_delivered
);
591 spin_unlock_irqrestore(&i8042_lock
, flags
);
593 return IRQ_RETVAL(ret
);
597 * i8042_toggle_aux - enables or disables AUX port on i8042 via command and
598 * verifies success by readinng CTR. Used when testing for presence of AUX
601 static int __init
i8042_toggle_aux(bool on
)
606 if (i8042_command(¶m
,
607 on
? I8042_CMD_AUX_ENABLE
: I8042_CMD_AUX_DISABLE
))
610 /* some chips need some time to set the I8042_CTR_AUXDIS bit */
611 for (i
= 0; i
< 100; i
++) {
614 if (i8042_command(¶m
, I8042_CMD_CTL_RCTR
))
617 if (!(param
& I8042_CTR_AUXDIS
) == on
)
625 * i8042_check_aux() applies as much paranoia as it can at detecting
626 * the presence of an AUX interface.
629 static int __init
i8042_check_aux(void)
632 bool irq_registered
= false;
633 bool aux_loop_broken
= false;
638 * Get rid of bytes in the queue.
644 * Internal loopback test - filters out AT-type i8042's. Unfortunately
645 * SiS screwed up and their 5597 doesn't support the LOOP command even
646 * though it has an AUX port.
650 retval
= i8042_command(¶m
, I8042_CMD_AUX_LOOP
);
651 if (retval
|| param
!= 0x5a) {
654 * External connection test - filters out AT-soldered PS/2 i8042's
655 * 0x00 - no error, 0x01-0x03 - clock/data stuck, 0xff - general error
656 * 0xfa - no error on some notebooks which ignore the spec
657 * Because it's common for chipsets to return error on perfectly functioning
658 * AUX ports, we test for this only when the LOOP command failed.
661 if (i8042_command(¶m
, I8042_CMD_AUX_TEST
) ||
662 (param
&& param
!= 0xfa && param
!= 0xff))
666 * If AUX_LOOP completed without error but returned unexpected data
670 aux_loop_broken
= true;
674 * Bit assignment test - filters out PS/2 i8042's in AT mode
677 if (i8042_toggle_aux(false)) {
678 printk(KERN_WARNING
"Failed to disable AUX port, but continuing anyway... Is this a SiS?\n");
679 printk(KERN_WARNING
"If AUX port is really absent please use the 'i8042.noaux' option.\n");
682 if (i8042_toggle_aux(true))
686 * Test AUX IRQ delivery to make sure BIOS did not grab the IRQ and
687 * used it for a PCI card or somethig else.
690 if (i8042_noloop
|| i8042_bypass_aux_irq_test
|| aux_loop_broken
) {
692 * Without LOOP command we can't test AUX IRQ delivery. Assume the port
693 * is working and hope we are right.
699 if (request_irq(I8042_AUX_IRQ
, i8042_aux_test_irq
, IRQF_SHARED
,
700 "i8042", i8042_platform_device
))
703 irq_registered
= true;
705 if (i8042_enable_aux_port())
708 spin_lock_irqsave(&i8042_lock
, flags
);
710 init_completion(&i8042_aux_irq_delivered
);
711 i8042_irq_being_tested
= true;
714 retval
= __i8042_command(¶m
, I8042_CMD_AUX_LOOP
& 0xf0ff);
716 spin_unlock_irqrestore(&i8042_lock
, flags
);
721 if (wait_for_completion_timeout(&i8042_aux_irq_delivered
,
722 msecs_to_jiffies(250)) == 0) {
724 * AUX IRQ was never delivered so we need to flush the controller to
725 * get rid of the byte we put there; otherwise keyboard may not work.
734 * Disable the interface.
737 i8042_ctr
|= I8042_CTR_AUXDIS
;
738 i8042_ctr
&= ~I8042_CTR_AUXINT
;
740 if (i8042_command(&i8042_ctr
, I8042_CMD_CTL_WCTR
))
744 free_irq(I8042_AUX_IRQ
, i8042_platform_device
);
749 static int i8042_controller_check(void)
751 if (i8042_flush() == I8042_BUFFER_SIZE
) {
752 printk(KERN_ERR
"i8042.c: No controller found.\n");
759 static int i8042_controller_selftest(void)
768 * We try this 5 times; on some really fragile systems this does not
769 * take the first time...
773 if (i8042_command(¶m
, I8042_CMD_CTL_TEST
)) {
774 printk(KERN_ERR
"i8042.c: i8042 controller self test timeout.\n");
778 if (param
== I8042_RET_CTL_TEST
)
781 printk(KERN_ERR
"i8042.c: i8042 controller selftest failed. (%#x != %#x)\n",
782 param
, I8042_RET_CTL_TEST
);
788 * On x86, we don't fail entire i8042 initialization if controller
789 * reset fails in hopes that keyboard port will still be functional
790 * and user will still get a working keyboard. This is especially
791 * important on netbooks. On other arches we trust hardware more.
794 "i8042: giving up on controller selftest, continuing anyway...\n");
802 * i8042_controller init initializes the i8042 controller, and,
803 * most importantly, sets it into non-xlated mode if that's
807 static int i8042_controller_init(void)
812 * Save the CTR for restoral on unload / reboot.
815 if (i8042_command(&i8042_ctr
, I8042_CMD_CTL_RCTR
)) {
816 printk(KERN_ERR
"i8042.c: Can't read CTR while initializing i8042.\n");
820 i8042_initial_ctr
= i8042_ctr
;
823 * Disable the keyboard interface and interrupt.
826 i8042_ctr
|= I8042_CTR_KBDDIS
;
827 i8042_ctr
&= ~I8042_CTR_KBDINT
;
833 spin_lock_irqsave(&i8042_lock
, flags
);
834 if (~i8042_read_status() & I8042_STR_KEYLOCK
) {
836 i8042_ctr
|= I8042_CTR_IGNKEYLOCK
;
838 printk(KERN_WARNING
"i8042.c: Warning: Keylock active.\n");
840 spin_unlock_irqrestore(&i8042_lock
, flags
);
843 * If the chip is configured into nontranslated mode by the BIOS, don't
844 * bother enabling translating and be happy.
847 if (~i8042_ctr
& I8042_CTR_XLATE
)
851 * Set nontranslated mode for the kbd interface if requested by an option.
852 * After this the kbd interface becomes a simple serial in/out, like the aux
853 * interface is. We don't do this by default, since it can confuse notebook
858 i8042_ctr
&= ~I8042_CTR_XLATE
;
864 if (i8042_command(&i8042_ctr
, I8042_CMD_CTL_WCTR
)) {
865 printk(KERN_ERR
"i8042.c: Can't write CTR while initializing i8042.\n");
874 * Reset the controller and reset CRT to the original value set by BIOS.
877 static void i8042_controller_reset(void)
882 * Disable both KBD and AUX interfaces so they don't get in the way
885 i8042_ctr
|= I8042_CTR_KBDDIS
| I8042_CTR_AUXDIS
;
886 i8042_ctr
&= ~(I8042_CTR_KBDINT
| I8042_CTR_AUXINT
);
888 if (i8042_command(&i8042_initial_ctr
, I8042_CMD_CTL_WCTR
))
889 printk(KERN_WARNING
"i8042.c: Can't write CTR while resetting.\n");
892 * Disable MUX mode if present.
895 if (i8042_mux_present
)
896 i8042_set_mux_mode(false, NULL
);
899 * Reset the controller if requested.
902 i8042_controller_selftest();
905 * Restore the original control register setting.
908 if (i8042_command(&i8042_initial_ctr
, I8042_CMD_CTL_WCTR
))
909 printk(KERN_WARNING
"i8042.c: Can't restore CTR.\n");
914 * i8042_panic_blink() will flash the keyboard LEDs and is called when
915 * kernel panics. Flashing LEDs is useful for users running X who may
916 * not see the console and will help distingushing panics from "real"
919 * Note that DELAY has a limit of 10ms so we will not get stuck here
920 * waiting for KBC to free up even if KBD interrupt is off
923 #define DELAY do { mdelay(1); if (++delay > 10) return delay; } while(0)
925 static long i8042_panic_blink(long count
)
928 static long last_blink
;
932 * We expect frequency to be about 1/2s. KDB uses about 1s.
933 * Make sure they are different.
935 if (!i8042_blink_frequency
)
937 if (count
- last_blink
< i8042_blink_frequency
)
941 while (i8042_read_status() & I8042_STR_IBF
)
943 dbg("%02x -> i8042 (panic blink)", 0xed);
944 i8042_suppress_kbd_ack
= 2;
945 i8042_write_data(0xed); /* set leds */
947 while (i8042_read_status() & I8042_STR_IBF
)
950 dbg("%02x -> i8042 (panic blink)", led
);
951 i8042_write_data(led
);
960 static void i8042_dritek_enable(void)
965 error
= i8042_command(¶m
, 0x1059);
968 "Failed to enable DRITEK extension: %d\n",
976 * Here we try to restore the original BIOS settings to avoid
980 static int i8042_pm_reset(struct device
*dev
)
982 i8042_controller_reset();
988 * Here we try to reset everything back to a state we had
992 static int i8042_pm_restore(struct device
*dev
)
996 error
= i8042_controller_check();
1000 error
= i8042_controller_selftest();
1005 * Restore original CTR value and disable all ports
1008 i8042_ctr
= i8042_initial_ctr
;
1010 i8042_ctr
&= ~I8042_CTR_XLATE
;
1011 i8042_ctr
|= I8042_CTR_AUXDIS
| I8042_CTR_KBDDIS
;
1012 i8042_ctr
&= ~(I8042_CTR_AUXINT
| I8042_CTR_KBDINT
);
1013 if (i8042_command(&i8042_ctr
, I8042_CMD_CTL_WCTR
)) {
1014 printk(KERN_WARNING
"i8042: Can't write CTR to resume, retrying...\n");
1016 if (i8042_command(&i8042_ctr
, I8042_CMD_CTL_WCTR
)) {
1017 printk(KERN_ERR
"i8042: CTR write retry failed\n");
1025 i8042_dritek_enable();
1028 if (i8042_mux_present
) {
1029 if (i8042_set_mux_mode(true, NULL
) || i8042_enable_mux_ports())
1031 "i8042: failed to resume active multiplexor, "
1032 "mouse won't work.\n");
1033 } else if (i8042_ports
[I8042_AUX_PORT_NO
].serio
)
1034 i8042_enable_aux_port();
1036 if (i8042_ports
[I8042_KBD_PORT_NO
].serio
)
1037 i8042_enable_kbd_port();
1039 i8042_interrupt(0, NULL
);
1044 static const struct dev_pm_ops i8042_pm_ops
= {
1045 .suspend
= i8042_pm_reset
,
1046 .resume
= i8042_pm_restore
,
1047 .poweroff
= i8042_pm_reset
,
1048 .restore
= i8042_pm_restore
,
1051 #endif /* CONFIG_PM */
1054 * We need to reset the 8042 back to original mode on system shutdown,
1055 * because otherwise BIOSes will be confused.
1058 static void i8042_shutdown(struct platform_device
*dev
)
1060 i8042_controller_reset();
1063 static int __init
i8042_create_kbd_port(void)
1065 struct serio
*serio
;
1066 struct i8042_port
*port
= &i8042_ports
[I8042_KBD_PORT_NO
];
1068 serio
= kzalloc(sizeof(struct serio
), GFP_KERNEL
);
1072 serio
->id
.type
= i8042_direct
? SERIO_8042
: SERIO_8042_XL
;
1073 serio
->write
= i8042_dumbkbd
? NULL
: i8042_kbd_write
;
1074 serio
->start
= i8042_start
;
1075 serio
->stop
= i8042_stop
;
1076 serio
->close
= i8042_port_close
;
1077 serio
->port_data
= port
;
1078 serio
->dev
.parent
= &i8042_platform_device
->dev
;
1079 strlcpy(serio
->name
, "i8042 KBD port", sizeof(serio
->name
));
1080 strlcpy(serio
->phys
, I8042_KBD_PHYS_DESC
, sizeof(serio
->phys
));
1082 port
->serio
= serio
;
1083 port
->irq
= I8042_KBD_IRQ
;
1088 static int __init
i8042_create_aux_port(int idx
)
1090 struct serio
*serio
;
1091 int port_no
= idx
< 0 ? I8042_AUX_PORT_NO
: I8042_MUX_PORT_NO
+ idx
;
1092 struct i8042_port
*port
= &i8042_ports
[port_no
];
1094 serio
= kzalloc(sizeof(struct serio
), GFP_KERNEL
);
1098 serio
->id
.type
= SERIO_8042
;
1099 serio
->write
= i8042_aux_write
;
1100 serio
->start
= i8042_start
;
1101 serio
->stop
= i8042_stop
;
1102 serio
->port_data
= port
;
1103 serio
->dev
.parent
= &i8042_platform_device
->dev
;
1105 strlcpy(serio
->name
, "i8042 AUX port", sizeof(serio
->name
));
1106 strlcpy(serio
->phys
, I8042_AUX_PHYS_DESC
, sizeof(serio
->phys
));
1107 serio
->close
= i8042_port_close
;
1109 snprintf(serio
->name
, sizeof(serio
->name
), "i8042 AUX%d port", idx
);
1110 snprintf(serio
->phys
, sizeof(serio
->phys
), I8042_MUX_PHYS_DESC
, idx
+ 1);
1113 port
->serio
= serio
;
1115 port
->irq
= I8042_AUX_IRQ
;
1120 static void __init
i8042_free_kbd_port(void)
1122 kfree(i8042_ports
[I8042_KBD_PORT_NO
].serio
);
1123 i8042_ports
[I8042_KBD_PORT_NO
].serio
= NULL
;
1126 static void __init
i8042_free_aux_ports(void)
1130 for (i
= I8042_AUX_PORT_NO
; i
< I8042_NUM_PORTS
; i
++) {
1131 kfree(i8042_ports
[i
].serio
);
1132 i8042_ports
[i
].serio
= NULL
;
1136 static void __init
i8042_register_ports(void)
1140 for (i
= 0; i
< I8042_NUM_PORTS
; i
++) {
1141 if (i8042_ports
[i
].serio
) {
1142 printk(KERN_INFO
"serio: %s at %#lx,%#lx irq %d\n",
1143 i8042_ports
[i
].serio
->name
,
1144 (unsigned long) I8042_DATA_REG
,
1145 (unsigned long) I8042_COMMAND_REG
,
1146 i8042_ports
[i
].irq
);
1147 serio_register_port(i8042_ports
[i
].serio
);
1152 static void __devexit
i8042_unregister_ports(void)
1156 for (i
= 0; i
< I8042_NUM_PORTS
; i
++) {
1157 if (i8042_ports
[i
].serio
) {
1158 serio_unregister_port(i8042_ports
[i
].serio
);
1159 i8042_ports
[i
].serio
= NULL
;
1164 static void i8042_free_irqs(void)
1166 if (i8042_aux_irq_registered
)
1167 free_irq(I8042_AUX_IRQ
, i8042_platform_device
);
1168 if (i8042_kbd_irq_registered
)
1169 free_irq(I8042_KBD_IRQ
, i8042_platform_device
);
1171 i8042_aux_irq_registered
= i8042_kbd_irq_registered
= false;
1174 static int __init
i8042_setup_aux(void)
1176 int (*aux_enable
)(void);
1180 if (i8042_check_aux())
1183 if (i8042_nomux
|| i8042_check_mux()) {
1184 error
= i8042_create_aux_port(-1);
1186 goto err_free_ports
;
1187 aux_enable
= i8042_enable_aux_port
;
1189 for (i
= 0; i
< I8042_NUM_MUX_PORTS
; i
++) {
1190 error
= i8042_create_aux_port(i
);
1192 goto err_free_ports
;
1194 aux_enable
= i8042_enable_mux_ports
;
1197 error
= request_irq(I8042_AUX_IRQ
, i8042_interrupt
, IRQF_SHARED
,
1198 "i8042", i8042_platform_device
);
1200 goto err_free_ports
;
1205 i8042_aux_irq_registered
= true;
1209 free_irq(I8042_AUX_IRQ
, i8042_platform_device
);
1211 i8042_free_aux_ports();
1215 static int __init
i8042_setup_kbd(void)
1219 error
= i8042_create_kbd_port();
1223 error
= request_irq(I8042_KBD_IRQ
, i8042_interrupt
, IRQF_SHARED
,
1224 "i8042", i8042_platform_device
);
1228 error
= i8042_enable_kbd_port();
1232 i8042_kbd_irq_registered
= true;
1236 free_irq(I8042_KBD_IRQ
, i8042_platform_device
);
1238 i8042_free_kbd_port();
1242 static int __init
i8042_probe(struct platform_device
*dev
)
1246 error
= i8042_controller_selftest();
1250 error
= i8042_controller_init();
1256 i8042_dritek_enable();
1260 error
= i8042_setup_aux();
1261 if (error
&& error
!= -ENODEV
&& error
!= -EBUSY
)
1266 error
= i8042_setup_kbd();
1271 * Ok, everything is ready, let's register all serio ports
1273 i8042_register_ports();
1278 i8042_free_aux_ports(); /* in case KBD failed but AUX not */
1280 i8042_controller_reset();
1285 static int __devexit
i8042_remove(struct platform_device
*dev
)
1287 i8042_unregister_ports();
1289 i8042_controller_reset();
1294 static struct platform_driver i8042_driver
= {
1297 .owner
= THIS_MODULE
,
1299 .pm
= &i8042_pm_ops
,
1302 .remove
= __devexit_p(i8042_remove
),
1303 .shutdown
= i8042_shutdown
,
1306 static int __init
i8042_init(void)
1312 err
= i8042_platform_init();
1316 err
= i8042_controller_check();
1318 goto err_platform_exit
;
1320 i8042_platform_device
= platform_device_alloc("i8042", -1);
1321 if (!i8042_platform_device
) {
1323 goto err_platform_exit
;
1326 err
= platform_device_add(i8042_platform_device
);
1328 goto err_free_device
;
1330 err
= platform_driver_probe(&i8042_driver
, i8042_probe
);
1332 goto err_del_device
;
1334 panic_blink
= i8042_panic_blink
;
1339 platform_device_del(i8042_platform_device
);
1341 platform_device_put(i8042_platform_device
);
1343 i8042_platform_exit();
1348 static void __exit
i8042_exit(void)
1350 platform_driver_unregister(&i8042_driver
);
1351 platform_device_unregister(i8042_platform_device
);
1352 i8042_platform_exit();
1357 module_init(i8042_init
);
1358 module_exit(i8042_exit
);