init from v2.6.32.60
[mach-moxart.git] / drivers / input / serio / i8042.c
blobdb9d1ea9c221b3fde6c1b93a782e434280bd3131
1 /*
2 * i8042 keyboard and mouse controller driver for Linux
4 * Copyright (c) 1999-2004 Vojtech Pavlik
5 */
7 /*
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>
25 #include <asm/io.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");
71 #ifdef CONFIG_X86
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");
75 #endif
77 #ifdef CONFIG_PNP
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");
81 #endif
83 #define DEBUG
84 #ifdef DEBUG
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");
88 #endif
90 static bool i8042_bypass_aux_irq_test;
92 #include "i8042.h"
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);
110 struct i8042_port {
111 struct serio *serio;
112 int irq;
113 bool exists;
114 signed char mux;
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)
154 int i = 0;
156 while ((~i8042_read_status() & I8042_STR_OBF) && (i < I8042_CTL_TIMEOUT)) {
157 udelay(50);
158 i++;
160 return -(i == I8042_CTL_TIMEOUT);
163 static int i8042_wait_write(void)
165 int i = 0;
167 while ((i8042_read_status() & I8042_STR_IBF) && (i < I8042_CTL_TIMEOUT)) {
168 udelay(50);
169 i++;
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)
181 unsigned long flags;
182 unsigned char data, str;
183 int i = 0;
185 spin_lock_irqsave(&i8042_lock, flags);
187 while (((str = i8042_read_status()) & I8042_STR_OBF) && (i < I8042_BUFFER_SIZE)) {
188 udelay(50);
189 data = i8042_read_data();
190 i++;
191 dbg("%02x <- i8042 (flush, %s)", data,
192 str & I8042_STR_AUXDATA ? "aux" : "kbd");
195 spin_unlock_irqrestore(&i8042_lock, flags);
197 return i;
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)
210 int i, error;
212 if (i8042_noloop && command == I8042_CMD_AUX_LOOP)
213 return -1;
215 error = i8042_wait_write();
216 if (error)
217 return error;
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();
224 if (error)
225 return error;
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();
232 if (error) {
233 dbg(" -- i8042 (timeout)");
234 return error;
237 if (command == I8042_CMD_AUX_LOOP &&
238 !(i8042_read_status() & I8042_STR_AUXDATA)) {
239 dbg(" -- i8042 (auxerr)");
240 return -1;
243 param[i] = i8042_read_data();
244 dbg("%02x <- i8042 (return)", param[i]);
247 return 0;
250 int i8042_command(unsigned char *param, int command)
252 unsigned long flags;
253 int retval;
255 spin_lock_irqsave(&i8042_lock, flags);
256 retval = __i8042_command(param, command);
257 spin_unlock_irqrestore(&i8042_lock, flags);
259 return retval;
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)
269 unsigned long flags;
270 int retval = 0;
272 spin_lock_irqsave(&i8042_lock, flags);
274 if (!(retval = i8042_wait_write())) {
275 dbg("%02x -> i8042 (kbd-data)", c);
276 i8042_write_data(c);
279 spin_unlock_irqrestore(&i8042_lock, flags);
281 return retval;
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 ?
293 I8042_CMD_AUX_SEND :
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)
305 int irq_bit;
306 int disable_bit;
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;
312 port_name = "AUX";
313 } else {
314 irq_bit = I8042_CTR_KBDINT;
315 disable_bit = I8042_CTR_KBDDIS;
316 port_name = "KBD";
319 i8042_ctr &= ~irq_bit;
320 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR))
321 printk(KERN_WARNING
322 "i8042.c: Can't write CTR while closing %s port.\n",
323 port_name);
325 udelay(50);
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",
331 port_name);
334 * See if there is any data appeared while we were messing with
335 * port state.
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;
349 port->exists = true;
350 mb();
351 return 0;
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
368 * and vice versa.
370 synchronize_irq(I8042_AUX_IRQ);
371 synchronize_irq(I8042_KBD_IRQ);
372 port->serio = NULL;
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;
384 unsigned long flags;
385 unsigned char str, data;
386 unsigned int dfl;
387 unsigned int port_no;
388 int ret = 1;
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);
395 ret = 0;
396 goto out;
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;
405 dfl = 0;
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).
421 switch (data) {
422 default:
423 if (time_before(jiffies, last_transmit + HZ/10)) {
424 str = last_str;
425 break;
427 /* fall through - report timeout */
428 case 0xfc:
429 case 0xfd:
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);
436 last_str = str;
437 last_transmit = jiffies;
438 } else {
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)",
450 data, port_no, irq,
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--;
458 goto out;
461 if (likely(port->exists))
462 serio_interrupt(port->serio, data, dfl);
464 out:
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");
481 return -EIO;
484 return 0;
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");
500 return -EIO;
503 return 0;
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)
513 unsigned char param;
514 int i;
516 for (i = 0; i < I8042_NUM_MUX_PORTS; i++) {
517 i8042_command(&param, I8042_CMD_MUX_PFX + i);
518 i8042_command(&param, 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.
538 i8042_flush();
541 * Internal loopback test - send three bytes, they should come back from the
542 * mouse interface, the last should be version.
545 param = val = 0xf0;
546 if (i8042_command(&param, I8042_CMD_AUX_LOOP) || param != val)
547 return -1;
548 param = val = multiplex ? 0x56 : 0xf6;
549 if (i8042_command(&param, I8042_CMD_AUX_LOOP) || param != val)
550 return -1;
551 param = val = multiplex ? 0xa4 : 0xa5;
552 if (i8042_command(&param, I8042_CMD_AUX_LOOP) || param == val)
553 return -1;
556 * Workaround for interference with USB Legacy emulation
557 * that causes a v10.12 MUX to be found.
559 if (param == 0xac)
560 return -1;
562 if (mux_version)
563 *mux_version = param;
565 return 0;
569 * i8042_check_mux() checks whether the controller supports the PS/2 Active
570 * Multiplexing specification by Synaptics, Phoenix, Insyde and
571 * LCS/Telegraphics.
574 static int __init i8042_check_mux(void)
576 unsigned char mux_version;
578 if (i8042_set_mux_mode(true, &mux_version))
579 return -1;
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");
592 return -EIO;
595 i8042_mux_present = true;
597 return 0;
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)
608 unsigned long flags;
609 unsigned char str, data;
610 int ret = 0;
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);
621 ret = 1;
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
631 * port.
633 static int __init i8042_toggle_aux(bool on)
635 unsigned char param;
636 int i;
638 if (i8042_command(&param,
639 on ? I8042_CMD_AUX_ENABLE : I8042_CMD_AUX_DISABLE))
640 return -1;
642 /* some chips need some time to set the I8042_CTR_AUXDIS bit */
643 for (i = 0; i < 100; i++) {
644 udelay(50);
646 if (i8042_command(&param, I8042_CMD_CTL_RCTR))
647 return -1;
649 if (!(param & I8042_CTR_AUXDIS) == on)
650 return 0;
653 return -1;
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)
663 int retval = -1;
664 bool irq_registered = false;
665 bool aux_loop_broken = false;
666 unsigned long flags;
667 unsigned char param;
670 * Get rid of bytes in the queue.
673 i8042_flush();
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.
681 param = 0x5a;
682 retval = i8042_command(&param, 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(&param, I8042_CMD_AUX_TEST) ||
694 (param && param != 0xfa && param != 0xff))
695 return -1;
698 * If AUX_LOOP completed without error but returned unexpected data
699 * mark it as broken
701 if (!retval)
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))
715 return -1;
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.
727 retval = 0;
728 goto out;
731 if (request_irq(I8042_AUX_IRQ, i8042_aux_test_irq, IRQF_SHARED,
732 "i8042", i8042_platform_device))
733 goto out;
735 irq_registered = true;
737 if (i8042_enable_aux_port())
738 goto out;
740 spin_lock_irqsave(&i8042_lock, flags);
742 init_completion(&i8042_aux_irq_delivered);
743 i8042_irq_being_tested = true;
745 param = 0xa5;
746 retval = __i8042_command(&param, I8042_CMD_AUX_LOOP & 0xf0ff);
748 spin_unlock_irqrestore(&i8042_lock, flags);
750 if (retval)
751 goto out;
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)");
760 i8042_flush();
761 retval = -1;
764 out:
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))
774 retval = -1;
776 if (irq_registered)
777 free_irq(I8042_AUX_IRQ, i8042_platform_device);
779 return retval;
782 static int i8042_controller_check(void)
784 if (i8042_flush() == I8042_BUFFER_SIZE) {
785 printk(KERN_ERR "i8042.c: No controller found.\n");
786 return -ENODEV;
789 return 0;
792 static int i8042_controller_selftest(void)
794 unsigned char param;
795 int i = 0;
797 if (!i8042_reset)
798 return 0;
801 * We try this 5 times; on some really fragile systems this does not
802 * take the first time...
804 do {
806 if (i8042_command(&param, I8042_CMD_CTL_TEST)) {
807 printk(KERN_ERR "i8042.c: i8042 controller self test timeout.\n");
808 return -ENODEV;
811 if (param == I8042_RET_CTL_TEST)
812 return 0;
814 printk(KERN_ERR "i8042.c: i8042 controller selftest failed. (%#x != %#x)\n",
815 param, I8042_RET_CTL_TEST);
816 msleep(50);
817 } while (i++ < 5);
819 #ifdef CONFIG_X86
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.
826 printk(KERN_INFO
827 "i8042: giving up on controller selftest, continuing anyway...\n");
828 return 0;
829 #else
830 return -EIO;
831 #endif
835 * i8042_controller init initializes the i8042 controller, and,
836 * most importantly, sets it into non-xlated mode if that's
837 * desired.
840 static int i8042_controller_init(void)
842 unsigned long flags;
843 int n = 0;
844 unsigned char ctr[2];
847 * Save the CTR for restore on unload / reboot.
850 do {
851 if (n >= 10) {
852 printk(KERN_ERR
853 "i8042.c: Unable to get stable CTR read.\n");
854 return -EIO;
857 if (n != 0)
858 udelay(50);
860 if (i8042_command(&ctr[n++ % 2], I8042_CMD_CTL_RCTR)) {
861 printk(KERN_ERR
862 "i8042.c: Can't read CTR while initializing i8042.\n");
863 return -EIO;
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;
878 * Handle keylock.
881 spin_lock_irqsave(&i8042_lock, flags);
882 if (~i8042_read_status() & I8042_STR_KEYLOCK) {
883 if (i8042_unlock)
884 i8042_ctr |= I8042_CTR_IGNKEYLOCK;
885 else
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)
896 i8042_direct = true;
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
902 * BIOSes.
905 if (i8042_direct)
906 i8042_ctr &= ~I8042_CTR_XLATE;
909 * Write CTR back.
912 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
913 printk(KERN_ERR "i8042.c: Can't write CTR while initializing i8042.\n");
914 return -EIO;
918 * Flush whatever accumulated while we were disabling keyboard port.
921 i8042_flush();
923 return 0;
928 * Reset the controller and reset CRT to the original value set by BIOS.
931 static void i8042_controller_reset(void)
933 i8042_flush();
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"
971 * lockups.
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)
981 long delay = 0;
982 static long last_blink;
983 static char led;
986 * We expect frequency to be about 1/2s. KDB uses about 1s.
987 * Make sure they are different.
989 if (!i8042_blink_frequency)
990 return 0;
991 if (count - last_blink < i8042_blink_frequency)
992 return 0;
994 led ^= 0x01 | 0x04;
995 while (i8042_read_status() & I8042_STR_IBF)
996 DELAY;
997 dbg("%02x -> i8042 (panic blink)", 0xed);
998 i8042_suppress_kbd_ack = 2;
999 i8042_write_data(0xed); /* set leds */
1000 DELAY;
1001 while (i8042_read_status() & I8042_STR_IBF)
1002 DELAY;
1003 DELAY;
1004 dbg("%02x -> i8042 (panic blink)", led);
1005 i8042_write_data(led);
1006 DELAY;
1007 last_blink = count;
1008 return delay;
1011 #undef DELAY
1013 #ifdef CONFIG_X86
1014 static void i8042_dritek_enable(void)
1016 char param = 0x90;
1017 int error;
1019 error = i8042_command(&param, 0x1059);
1020 if (error)
1021 printk(KERN_WARNING
1022 "Failed to enable DRITEK extension: %d\n",
1023 error);
1025 #endif
1027 #ifdef CONFIG_PM
1030 * Here we try to restore the original BIOS settings to avoid
1031 * upsetting it.
1034 static int i8042_pm_reset(struct device *dev)
1036 i8042_controller_reset();
1038 return 0;
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)
1048 int error;
1050 error = i8042_controller_check();
1051 if (error)
1052 return error;
1054 error = i8042_controller_selftest();
1055 if (error)
1056 return error;
1059 * Restore original CTR value and disable all ports
1062 i8042_ctr = i8042_initial_ctr;
1063 if (i8042_direct)
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");
1069 msleep(50);
1070 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
1071 printk(KERN_ERR "i8042: CTR write retry failed\n");
1072 return -EIO;
1077 #ifdef CONFIG_X86
1078 if (i8042_dritek)
1079 i8042_dritek_enable();
1080 #endif
1082 if (i8042_mux_present) {
1083 if (i8042_set_mux_mode(true, NULL) || i8042_enable_mux_ports())
1084 printk(KERN_WARNING
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);
1095 return 0;
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);
1123 if (!serio)
1124 return -ENOMEM;
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;
1139 return 0;
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);
1149 if (!serio)
1150 return -ENOMEM;
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;
1158 if (idx < 0) {
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;
1162 } else {
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;
1168 port->mux = idx;
1169 port->irq = I8042_AUX_IRQ;
1171 return 0;
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)
1182 int i;
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)
1192 int i;
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)
1208 int i;
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)
1223 int i;
1225 for (i = 0; i < I8042_NUM_PORTS; i++)
1226 if (i8042_ports[i].serio == port)
1227 return true;
1229 return false;
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);
1246 int error;
1247 int i;
1249 if (i8042_check_aux())
1250 return -ENODEV;
1252 if (i8042_nomux || i8042_check_mux()) {
1253 error = i8042_create_aux_port(-1);
1254 if (error)
1255 goto err_free_ports;
1256 aux_enable = i8042_enable_aux_port;
1257 } else {
1258 for (i = 0; i < I8042_NUM_MUX_PORTS; i++) {
1259 error = i8042_create_aux_port(i);
1260 if (error)
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);
1268 if (error)
1269 goto err_free_ports;
1271 if (aux_enable())
1272 goto err_free_irq;
1274 i8042_aux_irq_registered = true;
1275 return 0;
1277 err_free_irq:
1278 free_irq(I8042_AUX_IRQ, i8042_platform_device);
1279 err_free_ports:
1280 i8042_free_aux_ports();
1281 return error;
1284 static int __init i8042_setup_kbd(void)
1286 int error;
1288 error = i8042_create_kbd_port();
1289 if (error)
1290 return error;
1292 error = request_irq(I8042_KBD_IRQ, i8042_interrupt, IRQF_SHARED,
1293 "i8042", i8042_platform_device);
1294 if (error)
1295 goto err_free_port;
1297 error = i8042_enable_kbd_port();
1298 if (error)
1299 goto err_free_irq;
1301 i8042_kbd_irq_registered = true;
1302 return 0;
1304 err_free_irq:
1305 free_irq(I8042_KBD_IRQ, i8042_platform_device);
1306 err_free_port:
1307 i8042_free_kbd_port();
1308 return error;
1311 static int __init i8042_probe(struct platform_device *dev)
1313 int error;
1315 error = i8042_controller_selftest();
1316 if (error)
1317 return error;
1319 error = i8042_controller_init();
1320 if (error)
1321 return error;
1323 #ifdef CONFIG_X86
1324 if (i8042_dritek)
1325 i8042_dritek_enable();
1326 #endif
1328 if (!i8042_noaux) {
1329 error = i8042_setup_aux();
1330 if (error && error != -ENODEV && error != -EBUSY)
1331 goto out_fail;
1334 if (!i8042_nokbd) {
1335 error = i8042_setup_kbd();
1336 if (error)
1337 goto out_fail;
1340 * Ok, everything is ready, let's register all serio ports
1342 i8042_register_ports();
1344 return 0;
1346 out_fail:
1347 i8042_free_aux_ports(); /* in case KBD failed but AUX not */
1348 i8042_free_irqs();
1349 i8042_controller_reset();
1351 return error;
1354 static int __devexit i8042_remove(struct platform_device *dev)
1356 i8042_unregister_ports();
1357 i8042_free_irqs();
1358 i8042_controller_reset();
1360 return 0;
1363 static struct platform_driver i8042_driver = {
1364 .driver = {
1365 .name = "i8042",
1366 .owner = THIS_MODULE,
1367 #ifdef CONFIG_PM
1368 .pm = &i8042_pm_ops,
1369 #endif
1371 .remove = __devexit_p(i8042_remove),
1372 .shutdown = i8042_shutdown,
1375 static int __init i8042_init(void)
1377 int err;
1379 dbg_init();
1381 err = i8042_platform_init();
1382 if (err)
1383 return err;
1385 err = i8042_controller_check();
1386 if (err)
1387 goto err_platform_exit;
1389 i8042_platform_device = platform_device_alloc("i8042", -1);
1390 if (!i8042_platform_device) {
1391 err = -ENOMEM;
1392 goto err_platform_exit;
1395 err = platform_device_add(i8042_platform_device);
1396 if (err)
1397 goto err_free_device;
1399 err = platform_driver_probe(&i8042_driver, i8042_probe);
1400 if (err)
1401 goto err_del_device;
1403 panic_blink = i8042_panic_blink;
1405 return 0;
1407 err_del_device:
1408 platform_device_del(i8042_platform_device);
1409 err_free_device:
1410 platform_device_put(i8042_platform_device);
1411 err_platform_exit:
1412 i8042_platform_exit();
1414 return err;
1417 static void __exit i8042_exit(void)
1419 platform_device_unregister(i8042_platform_device);
1420 platform_driver_unregister(&i8042_driver);
1421 i8042_platform_exit();
1423 panic_blink = NULL;
1426 module_init(i8042_init);
1427 module_exit(i8042_exit);