13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 #include <linux/types.h>
17 #include <linux/module.h>
21 #include <linux/serio.h>
26 #include <linux/slab.h>
34 static bool i8042_nokbd;
38 static bool i8042_noaux;
42 static bool i8042_nomux;
44 MODULE_PARM_DESC(nomux,
"Do not check whether an active multiplexing controller is present.");
46 static bool i8042_unlock;
50 static bool i8042_reset;
54 static bool i8042_direct;
58 static bool i8042_dumbkbd;
60 MODULE_PARM_DESC(dumbkbd,
"Pretend that controller can only read data from keyboard");
62 static bool i8042_noloop;
64 MODULE_PARM_DESC(noloop,
"Disable the AUX Loopback command while probing for the AUX port");
66 static bool i8042_notimeout;
71 static bool i8042_dritek;
77 static bool i8042_nopnp;
84 static bool i8042_debug;
89 static bool i8042_bypass_aux_irq_test;
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)
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;
132 static bool (*i8042_platform_filter)(
unsigned char data,
unsigned char str,
155 if (i8042_platform_filter) {
160 i8042_platform_filter =
filter;
163 spin_unlock_irqrestore(&i8042_lock, flags);
176 if (i8042_platform_filter !=
filter) {
181 i8042_platform_filter =
NULL;
184 spin_unlock_irqrestore(&i8042_lock, flags);
195 static int i8042_wait_read(
void)
206 static int i8042_wait_write(
void)
222 static int i8042_flush(
void)
232 data = i8042_read_data();
234 dbg(
"%02x <- i8042 (flush, %s)\n",
238 spin_unlock_irqrestore(&i8042_lock, flags);
251 static int __i8042_command(
unsigned char *
param,
int command)
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");
282 dbg(
" -- i8042 (auxerr)\n");
286 param[
i] = i8042_read_data();
287 dbg(
"%02x <- i8042 (return)\n", param[i]);
299 retval = __i8042_command(param, command);
300 spin_unlock_irqrestore(&i8042_lock, flags);
310 static int i8042_kbd_write(
struct serio *
port,
unsigned char c)
317 if (!(retval = i8042_wait_write())) {
318 dbg(
"%02x -> i8042 (kbd-data)\n", c);
322 spin_unlock_irqrestore(&i8042_lock, flags);
331 static int i8042_aux_write(
struct serio *
serio,
unsigned char c)
346 static void i8042_port_close(
struct serio *
serio)
362 i8042_ctr &= ~irq_bit;
364 pr_warn(
"Can't write CTR while closing %s port\n", port_name);
368 i8042_ctr &= ~disable_bit;
369 i8042_ctr |= irq_bit;
371 pr_err(
"Can't reactivate %s port\n", port_name);
377 i8042_interrupt(0,
NULL);
385 static int i8042_start(
struct serio *serio)
399 static void i8042_stop(
struct serio *serio)
420 static bool i8042_filter(
unsigned char data,
unsigned char str,
423 if (
unlikely(i8042_suppress_kbd_ack)) {
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");
459 str = i8042_read_status();
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;
476 dbg(
"MUX error, status is %02x, data is %02x\n",
520 dbg(
"%02x <- i8042 (interrupt, %d, %d%s%s)\n",
525 filtered = i8042_filter(data, str, serio);
527 spin_unlock_irqrestore(&i8042_lock, flags);
540 static int i8042_enable_kbd_port(
void)
548 pr_err(
"Failed to enable KBD port\n");
559 static int i8042_enable_aux_port(
void)
567 pr_err(
"Failed to enable AUX port\n");
579 static int i8042_enable_mux_ports(
void)
589 return i8042_enable_aux_port();
598 static int i8042_set_mux_mode(
bool multiplex,
unsigned char *mux_version)
616 param = val = multiplex ? 0x56 : 0xf6;
619 param = val = multiplex ? 0xa4 : 0xa5;
631 *mux_version =
param;
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);
659 pr_err(
"Failed to disable AUX port, can't use MUX\n");
663 i8042_mux_present =
true;
672 static bool i8042_irq_being_tested
__initdata;
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))
691 spin_unlock_irqrestore(&i8042_lock, flags);
701 static int __init i8042_toggle_aux(
bool on)
711 for (i = 0; i < 100; i++) {
729 static int __init i8042_check_aux(
void)
732 bool irq_registered =
false;
733 bool aux_loop_broken =
false;
751 if (retval || param != 0x5a) {
762 (param && param != 0xfa && param != 0xff))
770 aux_loop_broken =
true;
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))
790 if (i8042_noloop || i8042_bypass_aux_irq_test || aux_loop_broken) {
800 "i8042", i8042_platform_device))
803 irq_registered =
true;
805 if (i8042_enable_aux_port())
810 init_completion(&i8042_aux_irq_delivered);
811 i8042_irq_being_tested =
true;
816 spin_unlock_irqrestore(&i8042_lock, flags);
827 dbg(
" -- i8042 (aux irq test timeout)\n");
850 static int i8042_controller_check(
void)
853 pr_err(
"No controller found\n");
860 static int i8042_controller_selftest(
void)
872 pr_err(
"i8042 controller selftest timeout\n");
879 dbg(
"i8042 controller selftest: %#x != %#x\n",
891 pr_info(
"giving up on controller selftest, continuing anyway...\n");
894 pr_err(
"i8042 controller selftest failed\n");
905 static int i8042_controller_init(
void)
909 unsigned char ctr[2];
917 pr_err(
"Unable to get stable CTR read\n");
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];
949 pr_warn(
"Warning: Keylock active\n");
951 spin_unlock_irqrestore(&i8042_lock, flags);
969 i8042_ctr &= ~I8042_CTR_XLATE;
976 pr_err(
"Can't write CTR while initializing i8042\n");
994 static void i8042_controller_reset(
bool force_reset)
1006 pr_warn(
"Can't write CTR while resetting\n");
1012 if (i8042_mux_present)
1013 i8042_set_mux_mode(
false,
NULL);
1019 if (i8042_reset || force_reset)
1020 i8042_controller_selftest();
1027 pr_warn(
"Can't restore CTR\n");
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;
1051 dbg(
"%02x -> i8042 (panic blink)\n", 0xed);
1052 i8042_suppress_kbd_ack = 2;
1053 i8042_write_data(0xed);
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;
1074 pr_warn(
"Failed to enable DRITEK extension: %d\n", error);
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();
1103 i8042_ctr = i8042_initial_ctr;
1105 i8042_ctr &= ~I8042_CTR_XLATE;
1109 pr_warn(
"Can't write CTR to resume, retrying...\n");
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");
1127 i8042_enable_aux_port();
1130 i8042_enable_kbd_port();
1132 i8042_interrupt(0,
NULL);
1142 static int i8042_pm_suspend(
struct device *
dev)
1144 i8042_controller_reset(
true);
1149 static int i8042_pm_resume(
struct device *
dev)
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 = {
1180 .resume = i8042_pm_resume,
1181 .thaw = i8042_pm_thaw,
1182 .poweroff = i8042_pm_reset,
1183 .restore = i8042_pm_restore,
1195 i8042_controller_reset(
false);
1198 static int __init i8042_create_kbd_port(
void)
1200 struct serio *serio;
1203 serio = kzalloc(
sizeof(
struct serio),
GFP_KERNEL);
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;
1213 serio->
dev.parent = &i8042_platform_device->
dev;
1223 static int __init i8042_create_aux_port(
int idx)
1225 struct serio *serio;
1229 serio = kzalloc(
sizeof(
struct serio),
GFP_KERNEL);
1234 serio->
write = i8042_aux_write;
1235 serio->
start = i8042_start;
1236 serio->
stop = i8042_stop;
1238 serio->
dev.parent = &i8042_platform_device->
dev;
1242 serio->
close = i8042_port_close;
1255 static void __init i8042_free_kbd_port(
void)
1261 static void __init i8042_free_aux_ports(
void)
1266 kfree(i8042_ports[i].serio);
1267 i8042_ports[
i].serio =
NULL;
1271 static void __init i8042_register_ports(
void)
1276 if (i8042_ports[i].serio) {
1278 i8042_ports[i].serio->name,
1281 i8042_ports[i].irq);
1287 static void __devexit i8042_unregister_ports(
void)
1292 if (i8042_ports[i].serio) {
1294 i8042_ports[
i].serio =
NULL;
1307 if (i8042_ports[i].serio == port)
1314 static void i8042_free_irqs(
void)
1316 if (i8042_aux_irq_registered)
1318 if (i8042_kbd_irq_registered)
1321 i8042_aux_irq_registered = i8042_kbd_irq_registered =
false;
1324 static int __init i8042_setup_aux(
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;
1340 error = i8042_create_aux_port(i);
1342 goto err_free_ports;
1344 aux_enable = i8042_enable_mux_ports;
1348 "i8042", i8042_platform_device);
1350 goto err_free_ports;
1355 i8042_aux_irq_registered =
true;
1361 i8042_free_aux_ports();
1365 static int __init i8042_setup_kbd(
void)
1369 error = i8042_create_kbd_port();
1374 "i8042", i8042_platform_device);
1378 error = i8042_enable_kbd_port();
1382 i8042_kbd_irq_registered =
true;
1388 i8042_free_kbd_port();
1396 i8042_platform_device =
dev;
1399 error = i8042_controller_selftest();
1404 error = i8042_controller_init();
1410 i8042_dritek_enable();
1414 error = i8042_setup_aux();
1420 error = i8042_setup_kbd();
1427 i8042_register_ports();
1432 i8042_free_aux_ports();
1434 i8042_controller_reset(
false);
1435 i8042_platform_device =
NULL;
1442 i8042_unregister_ports();
1444 i8042_controller_reset(
false);
1445 i8042_platform_device =
NULL;
1455 .pm = &i8042_pm_ops,
1459 .shutdown = i8042_shutdown,
1462 static int __init i8042_init(
void)
1469 err = i8042_platform_init();
1473 err = i8042_controller_check();
1475 goto err_platform_exit;
1479 err = PTR_ERR(pdev);
1480 goto err_platform_exit;
1488 i8042_platform_exit();
1492 static void __exit i8042_exit(
void)
1496 i8042_platform_exit();