xref: /linux/drivers/input/serio/i8042.c (revision de9ce703c6b807b1dfef5942df4f2fadd0fdb67a)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  i8042 keyboard and mouse controller driver for Linux
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (c) 1999-2004 Vojtech Pavlik
51da177e4SLinus Torvalds  */
61da177e4SLinus Torvalds 
71da177e4SLinus Torvalds /*
81da177e4SLinus Torvalds  * This program is free software; you can redistribute it and/or modify it
91da177e4SLinus Torvalds  * under the terms of the GNU General Public License version 2 as published by
101da177e4SLinus Torvalds  * the Free Software Foundation.
111da177e4SLinus Torvalds  */
121da177e4SLinus Torvalds 
131da177e4SLinus Torvalds #include <linux/delay.h>
141da177e4SLinus Torvalds #include <linux/module.h>
151da177e4SLinus Torvalds #include <linux/moduleparam.h>
161da177e4SLinus Torvalds #include <linux/interrupt.h>
171da177e4SLinus Torvalds #include <linux/ioport.h>
181da177e4SLinus Torvalds #include <linux/init.h>
191da177e4SLinus Torvalds #include <linux/serio.h>
201da177e4SLinus Torvalds #include <linux/err.h>
211da177e4SLinus Torvalds #include <linux/rcupdate.h>
22d052d1beSRussell King #include <linux/platform_device.h>
231da177e4SLinus Torvalds 
241da177e4SLinus Torvalds #include <asm/io.h>
251da177e4SLinus Torvalds 
261da177e4SLinus Torvalds MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
271da177e4SLinus Torvalds MODULE_DESCRIPTION("i8042 keyboard and mouse controller driver");
281da177e4SLinus Torvalds MODULE_LICENSE("GPL");
291da177e4SLinus Torvalds 
30945ef0d4SDmitry Torokhov static unsigned int i8042_nokbd;
31945ef0d4SDmitry Torokhov module_param_named(nokbd, i8042_nokbd, bool, 0);
32945ef0d4SDmitry Torokhov MODULE_PARM_DESC(nokbd, "Do not probe or use KBD port.");
33945ef0d4SDmitry Torokhov 
341da177e4SLinus Torvalds static unsigned int i8042_noaux;
351da177e4SLinus Torvalds module_param_named(noaux, i8042_noaux, bool, 0);
361da177e4SLinus Torvalds MODULE_PARM_DESC(noaux, "Do not probe or use AUX (mouse) port.");
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds static unsigned int i8042_nomux;
391da177e4SLinus Torvalds module_param_named(nomux, i8042_nomux, bool, 0);
401da177e4SLinus Torvalds MODULE_PARM_DESC(nomux, "Do not check whether an active multiplexing conrtoller is present.");
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds static unsigned int i8042_unlock;
431da177e4SLinus Torvalds module_param_named(unlock, i8042_unlock, bool, 0);
441da177e4SLinus Torvalds MODULE_PARM_DESC(unlock, "Ignore keyboard lock.");
451da177e4SLinus Torvalds 
461da177e4SLinus Torvalds static unsigned int i8042_reset;
471da177e4SLinus Torvalds module_param_named(reset, i8042_reset, bool, 0);
481da177e4SLinus Torvalds MODULE_PARM_DESC(reset, "Reset controller during init and cleanup.");
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds static unsigned int i8042_direct;
511da177e4SLinus Torvalds module_param_named(direct, i8042_direct, bool, 0);
521da177e4SLinus Torvalds MODULE_PARM_DESC(direct, "Put keyboard port into non-translated mode.");
531da177e4SLinus Torvalds 
541da177e4SLinus Torvalds static unsigned int i8042_dumbkbd;
551da177e4SLinus Torvalds module_param_named(dumbkbd, i8042_dumbkbd, bool, 0);
561da177e4SLinus Torvalds MODULE_PARM_DESC(dumbkbd, "Pretend that controller can only read data from keyboard");
571da177e4SLinus Torvalds 
581da177e4SLinus Torvalds static unsigned int i8042_noloop;
591da177e4SLinus Torvalds module_param_named(noloop, i8042_noloop, bool, 0);
601da177e4SLinus Torvalds MODULE_PARM_DESC(noloop, "Disable the AUX Loopback command while probing for the AUX port");
611da177e4SLinus Torvalds 
621da177e4SLinus Torvalds static unsigned int i8042_blink_frequency = 500;
631da177e4SLinus Torvalds module_param_named(panicblink, i8042_blink_frequency, uint, 0600);
641da177e4SLinus Torvalds MODULE_PARM_DESC(panicblink, "Frequency with which keyboard LEDs should blink when kernel panics");
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds #ifdef CONFIG_PNP
671da177e4SLinus Torvalds static int i8042_nopnp;
681da177e4SLinus Torvalds module_param_named(nopnp, i8042_nopnp, bool, 0);
691da177e4SLinus Torvalds MODULE_PARM_DESC(nopnp, "Do not use PNP to detect controller settings");
701da177e4SLinus Torvalds #endif
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds #define DEBUG
731da177e4SLinus Torvalds #ifdef DEBUG
741da177e4SLinus Torvalds static int i8042_debug;
751da177e4SLinus Torvalds module_param_named(debug, i8042_debug, bool, 0600);
761da177e4SLinus Torvalds MODULE_PARM_DESC(debug, "Turn i8042 debugging mode on and off");
771da177e4SLinus Torvalds #endif
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds __obsolete_setup("i8042_noaux");
801da177e4SLinus Torvalds __obsolete_setup("i8042_nomux");
811da177e4SLinus Torvalds __obsolete_setup("i8042_unlock");
821da177e4SLinus Torvalds __obsolete_setup("i8042_reset");
831da177e4SLinus Torvalds __obsolete_setup("i8042_direct");
841da177e4SLinus Torvalds __obsolete_setup("i8042_dumbkbd");
851da177e4SLinus Torvalds 
861da177e4SLinus Torvalds #include "i8042.h"
871da177e4SLinus Torvalds 
881da177e4SLinus Torvalds static DEFINE_SPINLOCK(i8042_lock);
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds struct i8042_port {
911da177e4SLinus Torvalds 	struct serio *serio;
921da177e4SLinus Torvalds 	int irq;
931da177e4SLinus Torvalds 	unsigned char exists;
941da177e4SLinus Torvalds 	signed char mux;
951da177e4SLinus Torvalds };
961da177e4SLinus Torvalds 
971da177e4SLinus Torvalds #define I8042_KBD_PORT_NO	0
981da177e4SLinus Torvalds #define I8042_AUX_PORT_NO	1
991da177e4SLinus Torvalds #define I8042_MUX_PORT_NO	2
1001da177e4SLinus Torvalds #define I8042_NUM_PORTS		(I8042_NUM_MUX_PORTS + 2)
101*de9ce703SDmitry Torokhov 
102*de9ce703SDmitry Torokhov static struct i8042_port i8042_ports[I8042_NUM_PORTS];
1031da177e4SLinus Torvalds 
1041da177e4SLinus Torvalds static unsigned char i8042_initial_ctr;
1051da177e4SLinus Torvalds static unsigned char i8042_ctr;
1061da177e4SLinus Torvalds static unsigned char i8042_mux_present;
107*de9ce703SDmitry Torokhov static unsigned char i8042_kbd_irq_registered;
108*de9ce703SDmitry Torokhov static unsigned char i8042_aux_irq_registered;
1091da177e4SLinus Torvalds static struct platform_device *i8042_platform_device;
1101da177e4SLinus Torvalds 
1111da177e4SLinus Torvalds static irqreturn_t i8042_interrupt(int irq, void *dev_id, struct pt_regs *regs);
1121da177e4SLinus Torvalds 
1131da177e4SLinus Torvalds /*
1141da177e4SLinus Torvalds  * The i8042_wait_read() and i8042_wait_write functions wait for the i8042 to
1151da177e4SLinus Torvalds  * be ready for reading values from it / writing values to it.
1161da177e4SLinus Torvalds  * Called always with i8042_lock held.
1171da177e4SLinus Torvalds  */
1181da177e4SLinus Torvalds 
1191da177e4SLinus Torvalds static int i8042_wait_read(void)
1201da177e4SLinus Torvalds {
1211da177e4SLinus Torvalds 	int i = 0;
122*de9ce703SDmitry Torokhov 
1231da177e4SLinus Torvalds 	while ((~i8042_read_status() & I8042_STR_OBF) && (i < I8042_CTL_TIMEOUT)) {
1241da177e4SLinus Torvalds 		udelay(50);
1251da177e4SLinus Torvalds 		i++;
1261da177e4SLinus Torvalds 	}
1271da177e4SLinus Torvalds 	return -(i == I8042_CTL_TIMEOUT);
1281da177e4SLinus Torvalds }
1291da177e4SLinus Torvalds 
1301da177e4SLinus Torvalds static int i8042_wait_write(void)
1311da177e4SLinus Torvalds {
1321da177e4SLinus Torvalds 	int i = 0;
133*de9ce703SDmitry Torokhov 
1341da177e4SLinus Torvalds 	while ((i8042_read_status() & I8042_STR_IBF) && (i < I8042_CTL_TIMEOUT)) {
1351da177e4SLinus Torvalds 		udelay(50);
1361da177e4SLinus Torvalds 		i++;
1371da177e4SLinus Torvalds 	}
1381da177e4SLinus Torvalds 	return -(i == I8042_CTL_TIMEOUT);
1391da177e4SLinus Torvalds }
1401da177e4SLinus Torvalds 
1411da177e4SLinus Torvalds /*
1421da177e4SLinus Torvalds  * i8042_flush() flushes all data that may be in the keyboard and mouse buffers
1431da177e4SLinus Torvalds  * of the i8042 down the toilet.
1441da177e4SLinus Torvalds  */
1451da177e4SLinus Torvalds 
1461da177e4SLinus Torvalds static int i8042_flush(void)
1471da177e4SLinus Torvalds {
1481da177e4SLinus Torvalds 	unsigned long flags;
1491da177e4SLinus Torvalds 	unsigned char data, str;
1501da177e4SLinus Torvalds 	int i = 0;
1511da177e4SLinus Torvalds 
1521da177e4SLinus Torvalds 	spin_lock_irqsave(&i8042_lock, flags);
1531da177e4SLinus Torvalds 
1541da177e4SLinus Torvalds 	while (((str = i8042_read_status()) & I8042_STR_OBF) && (i < I8042_BUFFER_SIZE)) {
1551da177e4SLinus Torvalds 		udelay(50);
1561da177e4SLinus Torvalds 		data = i8042_read_data();
1571da177e4SLinus Torvalds 		i++;
1581da177e4SLinus Torvalds 		dbg("%02x <- i8042 (flush, %s)", data,
1591da177e4SLinus Torvalds 			str & I8042_STR_AUXDATA ? "aux" : "kbd");
1601da177e4SLinus Torvalds 	}
1611da177e4SLinus Torvalds 
1621da177e4SLinus Torvalds 	spin_unlock_irqrestore(&i8042_lock, flags);
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds 	return i;
1651da177e4SLinus Torvalds }
1661da177e4SLinus Torvalds 
1671da177e4SLinus Torvalds /*
1681da177e4SLinus Torvalds  * i8042_command() executes a command on the i8042. It also sends the input
1691da177e4SLinus Torvalds  * parameter(s) of the commands to it, and receives the output value(s). The
1701da177e4SLinus Torvalds  * parameters are to be stored in the param array, and the output is placed
1711da177e4SLinus Torvalds  * into the same array. The number of the parameters and output values is
1721da177e4SLinus Torvalds  * encoded in bits 8-11 of the command number.
1731da177e4SLinus Torvalds  */
1741da177e4SLinus Torvalds 
175*de9ce703SDmitry Torokhov static int __i8042_command(unsigned char *param, int command)
1761da177e4SLinus Torvalds {
177*de9ce703SDmitry Torokhov 	int i, error;
1781da177e4SLinus Torvalds 
1791da177e4SLinus Torvalds 	if (i8042_noloop && command == I8042_CMD_AUX_LOOP)
1801da177e4SLinus Torvalds 		return -1;
1811da177e4SLinus Torvalds 
182*de9ce703SDmitry Torokhov 	error = i8042_wait_write();
183*de9ce703SDmitry Torokhov 	if (error)
184*de9ce703SDmitry Torokhov 		return error;
185463a4f76SDmitry Torokhov 
1861da177e4SLinus Torvalds 	dbg("%02x -> i8042 (command)", command & 0xff);
1871da177e4SLinus Torvalds 	i8042_write_command(command & 0xff);
1881da177e4SLinus Torvalds 
1891da177e4SLinus Torvalds 	for (i = 0; i < ((command >> 12) & 0xf); i++) {
190*de9ce703SDmitry Torokhov 		error = i8042_wait_write();
191*de9ce703SDmitry Torokhov 		if (error)
192*de9ce703SDmitry Torokhov 			return error;
1931da177e4SLinus Torvalds 		dbg("%02x -> i8042 (parameter)", param[i]);
1941da177e4SLinus Torvalds 		i8042_write_data(param[i]);
1951da177e4SLinus Torvalds 	}
1961da177e4SLinus Torvalds 
1971da177e4SLinus Torvalds 	for (i = 0; i < ((command >> 8) & 0xf); i++) {
198*de9ce703SDmitry Torokhov 		error = i8042_wait_read();
199*de9ce703SDmitry Torokhov 		if (error) {
200*de9ce703SDmitry Torokhov 			dbg("     -- i8042 (timeout)");
201*de9ce703SDmitry Torokhov 			return error;
202*de9ce703SDmitry Torokhov 		}
203463a4f76SDmitry Torokhov 
204463a4f76SDmitry Torokhov 		if (command == I8042_CMD_AUX_LOOP &&
205463a4f76SDmitry Torokhov 		    !(i8042_read_status() & I8042_STR_AUXDATA)) {
206*de9ce703SDmitry Torokhov 			dbg("     -- i8042 (auxerr)");
207*de9ce703SDmitry Torokhov 			return -1;
208463a4f76SDmitry Torokhov 		}
209463a4f76SDmitry Torokhov 
2101da177e4SLinus Torvalds 		param[i] = i8042_read_data();
2111da177e4SLinus Torvalds 		dbg("%02x <- i8042 (return)", param[i]);
2121da177e4SLinus Torvalds 	}
2131da177e4SLinus Torvalds 
214*de9ce703SDmitry Torokhov 	return 0;
215*de9ce703SDmitry Torokhov }
2161da177e4SLinus Torvalds 
217*de9ce703SDmitry Torokhov static int i8042_command(unsigned char *param, int command)
218*de9ce703SDmitry Torokhov {
219*de9ce703SDmitry Torokhov 	unsigned long flags;
220*de9ce703SDmitry Torokhov 	int retval;
221*de9ce703SDmitry Torokhov 
222*de9ce703SDmitry Torokhov 	spin_lock_irqsave(&i8042_lock, flags);
223*de9ce703SDmitry Torokhov 	retval = __i8042_command(param, command);
224463a4f76SDmitry Torokhov 	spin_unlock_irqrestore(&i8042_lock, flags);
225*de9ce703SDmitry Torokhov 
2261da177e4SLinus Torvalds 	return retval;
2271da177e4SLinus Torvalds }
2281da177e4SLinus Torvalds 
2291da177e4SLinus Torvalds /*
2301da177e4SLinus Torvalds  * i8042_kbd_write() sends a byte out through the keyboard interface.
2311da177e4SLinus Torvalds  */
2321da177e4SLinus Torvalds 
2331da177e4SLinus Torvalds static int i8042_kbd_write(struct serio *port, unsigned char c)
2341da177e4SLinus Torvalds {
2351da177e4SLinus Torvalds 	unsigned long flags;
2361da177e4SLinus Torvalds 	int retval = 0;
2371da177e4SLinus Torvalds 
2381da177e4SLinus Torvalds 	spin_lock_irqsave(&i8042_lock, flags);
2391da177e4SLinus Torvalds 
2401da177e4SLinus Torvalds 	if (!(retval = i8042_wait_write())) {
2411da177e4SLinus Torvalds 		dbg("%02x -> i8042 (kbd-data)", c);
2421da177e4SLinus Torvalds 		i8042_write_data(c);
2431da177e4SLinus Torvalds 	}
2441da177e4SLinus Torvalds 
2451da177e4SLinus Torvalds 	spin_unlock_irqrestore(&i8042_lock, flags);
2461da177e4SLinus Torvalds 
2471da177e4SLinus Torvalds 	return retval;
2481da177e4SLinus Torvalds }
2491da177e4SLinus Torvalds 
2501da177e4SLinus Torvalds /*
2511da177e4SLinus Torvalds  * i8042_aux_write() sends a byte out through the aux interface.
2521da177e4SLinus Torvalds  */
2531da177e4SLinus Torvalds 
2541da177e4SLinus Torvalds static int i8042_aux_write(struct serio *serio, unsigned char c)
2551da177e4SLinus Torvalds {
2561da177e4SLinus Torvalds 	struct i8042_port *port = serio->port_data;
2571da177e4SLinus Torvalds 	int retval;
2581da177e4SLinus Torvalds 
2591da177e4SLinus Torvalds /*
2601da177e4SLinus Torvalds  * Send the byte out.
2611da177e4SLinus Torvalds  */
2621da177e4SLinus Torvalds 
2631da177e4SLinus Torvalds 	if (port->mux == -1)
2641da177e4SLinus Torvalds 		retval = i8042_command(&c, I8042_CMD_AUX_SEND);
2651da177e4SLinus Torvalds 	else
2661da177e4SLinus Torvalds 		retval = i8042_command(&c, I8042_CMD_MUX_SEND + port->mux);
2671da177e4SLinus Torvalds 
2681da177e4SLinus Torvalds /*
2691da177e4SLinus Torvalds  * Make sure the interrupt happens and the character is received even
2701da177e4SLinus Torvalds  * in the case the IRQ isn't wired, so that we can receive further
2711da177e4SLinus Torvalds  * characters later.
2721da177e4SLinus Torvalds  */
2731da177e4SLinus Torvalds 
2741da177e4SLinus Torvalds 	i8042_interrupt(0, NULL, NULL);
2751da177e4SLinus Torvalds 	return retval;
2761da177e4SLinus Torvalds }
2771da177e4SLinus Torvalds 
2781da177e4SLinus Torvalds /*
2791da177e4SLinus Torvalds  * i8042_start() is called by serio core when port is about to finish
2801da177e4SLinus Torvalds  * registering. It will mark port as existing so i8042_interrupt can
2811da177e4SLinus Torvalds  * start sending data through it.
2821da177e4SLinus Torvalds  */
2831da177e4SLinus Torvalds static int i8042_start(struct serio *serio)
2841da177e4SLinus Torvalds {
2851da177e4SLinus Torvalds 	struct i8042_port *port = serio->port_data;
2861da177e4SLinus Torvalds 
2871da177e4SLinus Torvalds 	port->exists = 1;
2881da177e4SLinus Torvalds 	mb();
2891da177e4SLinus Torvalds 	return 0;
2901da177e4SLinus Torvalds }
2911da177e4SLinus Torvalds 
2921da177e4SLinus Torvalds /*
2931da177e4SLinus Torvalds  * i8042_stop() marks serio port as non-existing so i8042_interrupt
2941da177e4SLinus Torvalds  * will not try to send data to the port that is about to go away.
2951da177e4SLinus Torvalds  * The function is called by serio core as part of unregister procedure.
2961da177e4SLinus Torvalds  */
2971da177e4SLinus Torvalds static void i8042_stop(struct serio *serio)
2981da177e4SLinus Torvalds {
2991da177e4SLinus Torvalds 	struct i8042_port *port = serio->port_data;
3001da177e4SLinus Torvalds 
3011da177e4SLinus Torvalds 	port->exists = 0;
302b2b18660SPaul E. McKenney 	synchronize_sched();
3031da177e4SLinus Torvalds 	port->serio = NULL;
3041da177e4SLinus Torvalds }
3051da177e4SLinus Torvalds 
3061da177e4SLinus Torvalds /*
3071da177e4SLinus Torvalds  * i8042_interrupt() is the most important function in this driver -
3081da177e4SLinus Torvalds  * it handles the interrupts from the i8042, and sends incoming bytes
3091da177e4SLinus Torvalds  * to the upper layers.
3101da177e4SLinus Torvalds  */
3111da177e4SLinus Torvalds 
3121da177e4SLinus Torvalds static irqreturn_t i8042_interrupt(int irq, void *dev_id, struct pt_regs *regs)
3131da177e4SLinus Torvalds {
3141da177e4SLinus Torvalds 	struct i8042_port *port;
3151da177e4SLinus Torvalds 	unsigned long flags;
3161da177e4SLinus Torvalds 	unsigned char str, data;
3171da177e4SLinus Torvalds 	unsigned int dfl;
3181da177e4SLinus Torvalds 	unsigned int port_no;
3191da177e4SLinus Torvalds 	int ret;
3201da177e4SLinus Torvalds 
3211da177e4SLinus Torvalds 	spin_lock_irqsave(&i8042_lock, flags);
3221da177e4SLinus Torvalds 	str = i8042_read_status();
3231da177e4SLinus Torvalds 	if (unlikely(~str & I8042_STR_OBF)) {
3241da177e4SLinus Torvalds 		spin_unlock_irqrestore(&i8042_lock, flags);
3251da177e4SLinus Torvalds 		if (irq) dbg("Interrupt %d, without any data", irq);
3261da177e4SLinus Torvalds 		ret = 0;
3271da177e4SLinus Torvalds 		goto out;
3281da177e4SLinus Torvalds 	}
3291da177e4SLinus Torvalds 	data = i8042_read_data();
3301da177e4SLinus Torvalds 	spin_unlock_irqrestore(&i8042_lock, flags);
3311da177e4SLinus Torvalds 
3321da177e4SLinus Torvalds 	if (i8042_mux_present && (str & I8042_STR_AUXDATA)) {
3331da177e4SLinus Torvalds 		static unsigned long last_transmit;
3341da177e4SLinus Torvalds 		static unsigned char last_str;
3351da177e4SLinus Torvalds 
3361da177e4SLinus Torvalds 		dfl = 0;
3371da177e4SLinus Torvalds 		if (str & I8042_STR_MUXERR) {
3381da177e4SLinus Torvalds 			dbg("MUX error, status is %02x, data is %02x", str, data);
3391da177e4SLinus Torvalds 			switch (data) {
3401da177e4SLinus Torvalds 				default:
3411da177e4SLinus Torvalds /*
3421da177e4SLinus Torvalds  * When MUXERR condition is signalled the data register can only contain
3431da177e4SLinus Torvalds  * 0xfd, 0xfe or 0xff if implementation follows the spec. Unfortunately
3441da177e4SLinus Torvalds  * it is not always the case. Some KBC just get confused which port the
3451da177e4SLinus Torvalds  * data came from and signal error leaving the data intact. They _do not_
3461da177e4SLinus Torvalds  * revert to legacy mode (actually I've never seen KBC reverting to legacy
3471da177e4SLinus Torvalds  * mode yet, when we see one we'll add proper handling).
3481da177e4SLinus Torvalds  * Anyway, we will assume that the data came from the same serio last byte
3491da177e4SLinus Torvalds  * was transmitted (if transmission happened not too long ago).
3501da177e4SLinus Torvalds  */
3511da177e4SLinus Torvalds 					if (time_before(jiffies, last_transmit + HZ/10)) {
3521da177e4SLinus Torvalds 						str = last_str;
3531da177e4SLinus Torvalds 						break;
3541da177e4SLinus Torvalds 					}
3551da177e4SLinus Torvalds 					/* fall through - report timeout */
3561da177e4SLinus Torvalds 				case 0xfd:
3571da177e4SLinus Torvalds 				case 0xfe: dfl = SERIO_TIMEOUT; data = 0xfe; break;
3581da177e4SLinus Torvalds 				case 0xff: dfl = SERIO_PARITY;  data = 0xfe; break;
3591da177e4SLinus Torvalds 			}
3601da177e4SLinus Torvalds 		}
3611da177e4SLinus Torvalds 
3621da177e4SLinus Torvalds 		port_no = I8042_MUX_PORT_NO + ((str >> 6) & 3);
3631da177e4SLinus Torvalds 		last_str = str;
3641da177e4SLinus Torvalds 		last_transmit = jiffies;
3651da177e4SLinus Torvalds 	} else {
3661da177e4SLinus Torvalds 
3671da177e4SLinus Torvalds 		dfl = ((str & I8042_STR_PARITY) ? SERIO_PARITY : 0) |
3681da177e4SLinus Torvalds 		      ((str & I8042_STR_TIMEOUT) ? SERIO_TIMEOUT : 0);
3691da177e4SLinus Torvalds 
3701da177e4SLinus Torvalds 		port_no = (str & I8042_STR_AUXDATA) ?
3711da177e4SLinus Torvalds 				I8042_AUX_PORT_NO : I8042_KBD_PORT_NO;
3721da177e4SLinus Torvalds 	}
3731da177e4SLinus Torvalds 
3741da177e4SLinus Torvalds 	port = &i8042_ports[port_no];
3751da177e4SLinus Torvalds 
376*de9ce703SDmitry Torokhov 	dbg("%02x <- i8042 (interrupt, %d, %d%s%s)",
377*de9ce703SDmitry Torokhov 	    data, port_no, irq,
3781da177e4SLinus Torvalds 	    dfl & SERIO_PARITY ? ", bad parity" : "",
3791da177e4SLinus Torvalds 	    dfl & SERIO_TIMEOUT ? ", timeout" : "");
3801da177e4SLinus Torvalds 
3811da177e4SLinus Torvalds 	if (likely(port->exists))
3821da177e4SLinus Torvalds 		serio_interrupt(port->serio, data, dfl, regs);
3831da177e4SLinus Torvalds 
3841da177e4SLinus Torvalds 	ret = 1;
3851da177e4SLinus Torvalds  out:
3861da177e4SLinus Torvalds 	return IRQ_RETVAL(ret);
3871da177e4SLinus Torvalds }
3881da177e4SLinus Torvalds 
3891da177e4SLinus Torvalds /*
390*de9ce703SDmitry Torokhov  * i8042_enable_kbd_port enables keybaord port on chip
391*de9ce703SDmitry Torokhov  */
392*de9ce703SDmitry Torokhov 
393*de9ce703SDmitry Torokhov static int i8042_enable_kbd_port(void)
394*de9ce703SDmitry Torokhov {
395*de9ce703SDmitry Torokhov 	i8042_ctr &= ~I8042_CTR_KBDDIS;
396*de9ce703SDmitry Torokhov 	i8042_ctr |= I8042_CTR_KBDINT;
397*de9ce703SDmitry Torokhov 
398*de9ce703SDmitry Torokhov 	if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
399*de9ce703SDmitry Torokhov 		printk(KERN_ERR "i8042.c: Failed to enable KBD port.\n");
400*de9ce703SDmitry Torokhov 		return -EIO;
401*de9ce703SDmitry Torokhov 	}
402*de9ce703SDmitry Torokhov 
403*de9ce703SDmitry Torokhov 	return 0;
404*de9ce703SDmitry Torokhov }
405*de9ce703SDmitry Torokhov 
406*de9ce703SDmitry Torokhov /*
407*de9ce703SDmitry Torokhov  * i8042_enable_aux_port enables AUX (mouse) port on chip
408*de9ce703SDmitry Torokhov  */
409*de9ce703SDmitry Torokhov 
410*de9ce703SDmitry Torokhov static int i8042_enable_aux_port(void)
411*de9ce703SDmitry Torokhov {
412*de9ce703SDmitry Torokhov 	i8042_ctr &= ~I8042_CTR_AUXDIS;
413*de9ce703SDmitry Torokhov 	i8042_ctr |= I8042_CTR_AUXINT;
414*de9ce703SDmitry Torokhov 
415*de9ce703SDmitry Torokhov 	if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
416*de9ce703SDmitry Torokhov 		printk(KERN_ERR "i8042.c: Failed to enable AUX port.\n");
417*de9ce703SDmitry Torokhov 		return -EIO;
418*de9ce703SDmitry Torokhov 	}
419*de9ce703SDmitry Torokhov 
420*de9ce703SDmitry Torokhov 	return 0;
421*de9ce703SDmitry Torokhov }
422*de9ce703SDmitry Torokhov 
423*de9ce703SDmitry Torokhov /*
424*de9ce703SDmitry Torokhov  * i8042_enable_mux_ports enables 4 individual AUX ports after
425*de9ce703SDmitry Torokhov  * the controller has been switched into Multiplexed mode
426*de9ce703SDmitry Torokhov  */
427*de9ce703SDmitry Torokhov 
428*de9ce703SDmitry Torokhov static int i8042_enable_mux_ports(void)
429*de9ce703SDmitry Torokhov {
430*de9ce703SDmitry Torokhov 	unsigned char param;
431*de9ce703SDmitry Torokhov 	int i;
432*de9ce703SDmitry Torokhov 
433*de9ce703SDmitry Torokhov 	for (i = 0; i < I8042_NUM_MUX_PORTS; i++) {
434*de9ce703SDmitry Torokhov 		i8042_command(&param, I8042_CMD_MUX_PFX + i);
435*de9ce703SDmitry Torokhov 		i8042_command(&param, I8042_CMD_AUX_ENABLE);
436*de9ce703SDmitry Torokhov 	}
437*de9ce703SDmitry Torokhov 
438*de9ce703SDmitry Torokhov 	return i8042_enable_aux_port();
439*de9ce703SDmitry Torokhov }
440*de9ce703SDmitry Torokhov 
441*de9ce703SDmitry Torokhov /*
4421da177e4SLinus Torvalds  * i8042_set_mux_mode checks whether the controller has an active
4431da177e4SLinus Torvalds  * multiplexor and puts the chip into Multiplexed (1) or Legacy (0) mode.
4441da177e4SLinus Torvalds  */
4451da177e4SLinus Torvalds 
4461da177e4SLinus Torvalds static int i8042_set_mux_mode(unsigned int mode, unsigned char *mux_version)
4471da177e4SLinus Torvalds {
4481da177e4SLinus Torvalds 
4491da177e4SLinus Torvalds 	unsigned char param;
4501da177e4SLinus Torvalds /*
4511da177e4SLinus Torvalds  * Get rid of bytes in the queue.
4521da177e4SLinus Torvalds  */
4531da177e4SLinus Torvalds 
4541da177e4SLinus Torvalds 	i8042_flush();
4551da177e4SLinus Torvalds 
4561da177e4SLinus Torvalds /*
4571da177e4SLinus Torvalds  * Internal loopback test - send three bytes, they should come back from the
458*de9ce703SDmitry Torokhov  * mouse interface, the last should be version.
4591da177e4SLinus Torvalds  */
4601da177e4SLinus Torvalds 
4611da177e4SLinus Torvalds 	param = 0xf0;
462463a4f76SDmitry Torokhov 	if (i8042_command(&param, I8042_CMD_AUX_LOOP) || param != 0xf0)
4631da177e4SLinus Torvalds 		return -1;
4641da177e4SLinus Torvalds 	param = mode ? 0x56 : 0xf6;
465463a4f76SDmitry Torokhov 	if (i8042_command(&param, I8042_CMD_AUX_LOOP) || param != (mode ? 0x56 : 0xf6))
4661da177e4SLinus Torvalds 		return -1;
4671da177e4SLinus Torvalds 	param = mode ? 0xa4 : 0xa5;
468463a4f76SDmitry Torokhov 	if (i8042_command(&param, I8042_CMD_AUX_LOOP) || param == (mode ? 0xa4 : 0xa5))
4691da177e4SLinus Torvalds 		return -1;
4701da177e4SLinus Torvalds 
4711da177e4SLinus Torvalds 	if (mux_version)
472463a4f76SDmitry Torokhov 		*mux_version = param;
4731da177e4SLinus Torvalds 
4741da177e4SLinus Torvalds 	return 0;
4751da177e4SLinus Torvalds }
4761da177e4SLinus Torvalds 
4771da177e4SLinus Torvalds /*
4781da177e4SLinus Torvalds  * i8042_check_mux() checks whether the controller supports the PS/2 Active
4791da177e4SLinus Torvalds  * Multiplexing specification by Synaptics, Phoenix, Insyde and
4801da177e4SLinus Torvalds  * LCS/Telegraphics.
4811da177e4SLinus Torvalds  */
4821da177e4SLinus Torvalds 
48387fd6318SDmitry Torokhov static int __devinit i8042_check_mux(void)
4841da177e4SLinus Torvalds {
4851da177e4SLinus Torvalds 	unsigned char mux_version;
4861da177e4SLinus Torvalds 
4871da177e4SLinus Torvalds 	if (i8042_set_mux_mode(1, &mux_version))
4881da177e4SLinus Torvalds 		return -1;
4891da177e4SLinus Torvalds 
490*de9ce703SDmitry Torokhov /*
491*de9ce703SDmitry Torokhov  * Workaround for interference with USB Legacy emulation
492*de9ce703SDmitry Torokhov  * that causes a v10.12 MUX to be found.
493*de9ce703SDmitry Torokhov  */
4941da177e4SLinus Torvalds 	if (mux_version == 0xAC)
4951da177e4SLinus Torvalds 		return -1;
4961da177e4SLinus Torvalds 
4971da177e4SLinus Torvalds 	printk(KERN_INFO "i8042.c: Detected active multiplexing controller, rev %d.%d.\n",
4981da177e4SLinus Torvalds 		(mux_version >> 4) & 0xf, mux_version & 0xf);
4991da177e4SLinus Torvalds 
500*de9ce703SDmitry Torokhov /*
501*de9ce703SDmitry Torokhov  * Disable all muxed ports by disabling AUX.
502*de9ce703SDmitry Torokhov  */
503*de9ce703SDmitry Torokhov 	i8042_ctr |= I8042_CTR_AUXDIS;
504*de9ce703SDmitry Torokhov 	i8042_ctr &= ~I8042_CTR_AUXINT;
505*de9ce703SDmitry Torokhov 
506*de9ce703SDmitry Torokhov 	if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
507*de9ce703SDmitry Torokhov 		printk(KERN_ERR "i8042.c: Failed to disable AUX port, can't use MUX.\n");
508*de9ce703SDmitry Torokhov 		return -EIO;
509*de9ce703SDmitry Torokhov 	}
5101da177e4SLinus Torvalds 
5111da177e4SLinus Torvalds 	i8042_mux_present = 1;
512*de9ce703SDmitry Torokhov 
5131da177e4SLinus Torvalds 	return 0;
5141da177e4SLinus Torvalds }
5151da177e4SLinus Torvalds 
516*de9ce703SDmitry Torokhov /*
517*de9ce703SDmitry Torokhov  * The following is used to test AUX IRQ delivery.
518*de9ce703SDmitry Torokhov  */
519*de9ce703SDmitry Torokhov static struct completion i8042_aux_irq_delivered __devinitdata;
520*de9ce703SDmitry Torokhov static int i8042_irq_being_tested __devinitdata;
521*de9ce703SDmitry Torokhov 
522*de9ce703SDmitry Torokhov static irqreturn_t __devinit i8042_aux_test_irq(int irq, void *dev_id, struct pt_regs *regs)
523*de9ce703SDmitry Torokhov {
524*de9ce703SDmitry Torokhov 	unsigned long flags;
525*de9ce703SDmitry Torokhov 	unsigned char str, data;
526*de9ce703SDmitry Torokhov 
527*de9ce703SDmitry Torokhov 	spin_lock_irqsave(&i8042_lock, flags);
528*de9ce703SDmitry Torokhov 	str = i8042_read_status();
529*de9ce703SDmitry Torokhov 	if (str & I8042_STR_OBF) {
530*de9ce703SDmitry Torokhov 		data = i8042_read_data();
531*de9ce703SDmitry Torokhov 		if (i8042_irq_being_tested &&
532*de9ce703SDmitry Torokhov 		    data == 0xa5 && (str & I8042_STR_AUXDATA))
533*de9ce703SDmitry Torokhov 			complete(&i8042_aux_irq_delivered);
534*de9ce703SDmitry Torokhov 	}
535*de9ce703SDmitry Torokhov 	spin_unlock_irqrestore(&i8042_lock, flags);
536*de9ce703SDmitry Torokhov 
537*de9ce703SDmitry Torokhov 	return IRQ_HANDLED;
538*de9ce703SDmitry Torokhov }
539*de9ce703SDmitry Torokhov 
5401da177e4SLinus Torvalds 
5411da177e4SLinus Torvalds /*
5421da177e4SLinus Torvalds  * i8042_check_aux() applies as much paranoia as it can at detecting
5431da177e4SLinus Torvalds  * the presence of an AUX interface.
5441da177e4SLinus Torvalds  */
5451da177e4SLinus Torvalds 
54687fd6318SDmitry Torokhov static int __devinit i8042_check_aux(void)
5471da177e4SLinus Torvalds {
548*de9ce703SDmitry Torokhov 	int retval = -1;
549*de9ce703SDmitry Torokhov 	int irq_registered = 0;
550*de9ce703SDmitry Torokhov 	unsigned long flags;
5511da177e4SLinus Torvalds 	unsigned char param;
5521da177e4SLinus Torvalds 
5531da177e4SLinus Torvalds /*
5541da177e4SLinus Torvalds  * Get rid of bytes in the queue.
5551da177e4SLinus Torvalds  */
5561da177e4SLinus Torvalds 
5571da177e4SLinus Torvalds 	i8042_flush();
5581da177e4SLinus Torvalds 
5591da177e4SLinus Torvalds /*
5601da177e4SLinus Torvalds  * Internal loopback test - filters out AT-type i8042's. Unfortunately
5611da177e4SLinus Torvalds  * SiS screwed up and their 5597 doesn't support the LOOP command even
5621da177e4SLinus Torvalds  * though it has an AUX port.
5631da177e4SLinus Torvalds  */
5641da177e4SLinus Torvalds 
5651da177e4SLinus Torvalds 	param = 0x5a;
566463a4f76SDmitry Torokhov 	if (i8042_command(&param, I8042_CMD_AUX_LOOP) || param != 0x5a) {
5671da177e4SLinus Torvalds 
5681da177e4SLinus Torvalds /*
5691da177e4SLinus Torvalds  * External connection test - filters out AT-soldered PS/2 i8042's
5701da177e4SLinus Torvalds  * 0x00 - no error, 0x01-0x03 - clock/data stuck, 0xff - general error
5711da177e4SLinus Torvalds  * 0xfa - no error on some notebooks which ignore the spec
5721da177e4SLinus Torvalds  * Because it's common for chipsets to return error on perfectly functioning
5731da177e4SLinus Torvalds  * AUX ports, we test for this only when the LOOP command failed.
5741da177e4SLinus Torvalds  */
5751da177e4SLinus Torvalds 
576*de9ce703SDmitry Torokhov 		if (i8042_command(&param, I8042_CMD_AUX_TEST) ||
577*de9ce703SDmitry Torokhov 		    (param && param != 0xfa && param != 0xff))
5781da177e4SLinus Torvalds 			return -1;
5791da177e4SLinus Torvalds 	}
5801da177e4SLinus Torvalds 
5811da177e4SLinus Torvalds /*
5821da177e4SLinus Torvalds  * Bit assignment test - filters out PS/2 i8042's in AT mode
5831da177e4SLinus Torvalds  */
5841da177e4SLinus Torvalds 
5851da177e4SLinus Torvalds 	if (i8042_command(&param, I8042_CMD_AUX_DISABLE))
5861da177e4SLinus Torvalds 		return -1;
5871da177e4SLinus Torvalds 	if (i8042_command(&param, I8042_CMD_CTL_RCTR) || (~param & I8042_CTR_AUXDIS)) {
5881da177e4SLinus Torvalds 		printk(KERN_WARNING "Failed to disable AUX port, but continuing anyway... Is this a SiS?\n");
5891da177e4SLinus Torvalds 		printk(KERN_WARNING "If AUX port is really absent please use the 'i8042.noaux' option.\n");
5901da177e4SLinus Torvalds 	}
5911da177e4SLinus Torvalds 
5921da177e4SLinus Torvalds 	if (i8042_command(&param, I8042_CMD_AUX_ENABLE))
5931da177e4SLinus Torvalds 		return -1;
5941da177e4SLinus Torvalds 	if (i8042_command(&param, I8042_CMD_CTL_RCTR) || (param & I8042_CTR_AUXDIS))
5951da177e4SLinus Torvalds 		return -1;
5961da177e4SLinus Torvalds 
5971da177e4SLinus Torvalds /*
598*de9ce703SDmitry Torokhov  * Test AUX IRQ delivery to make sure BIOS did not grab the IRQ and
599*de9ce703SDmitry Torokhov  * used it for a PCI card or somethig else.
600*de9ce703SDmitry Torokhov  */
601*de9ce703SDmitry Torokhov 
602*de9ce703SDmitry Torokhov 	if (i8042_noloop) {
603*de9ce703SDmitry Torokhov /*
604*de9ce703SDmitry Torokhov  * Without LOOP command we can't test AUX IRQ delivery. Assume the port
605*de9ce703SDmitry Torokhov  * is working and hope we are right.
606*de9ce703SDmitry Torokhov  */
607*de9ce703SDmitry Torokhov 		retval = 0;
608*de9ce703SDmitry Torokhov 		goto out;
609*de9ce703SDmitry Torokhov 	}
610*de9ce703SDmitry Torokhov 
611*de9ce703SDmitry Torokhov 	if (request_irq(I8042_AUX_IRQ, i8042_aux_test_irq, IRQF_SHARED,
612*de9ce703SDmitry Torokhov 			"i8042", i8042_platform_device))
613*de9ce703SDmitry Torokhov 		goto out;
614*de9ce703SDmitry Torokhov 
615*de9ce703SDmitry Torokhov 	irq_registered = 1;
616*de9ce703SDmitry Torokhov 
617*de9ce703SDmitry Torokhov 	if (i8042_enable_aux_port())
618*de9ce703SDmitry Torokhov 		goto out;
619*de9ce703SDmitry Torokhov 
620*de9ce703SDmitry Torokhov 	spin_lock_irqsave(&i8042_lock, flags);
621*de9ce703SDmitry Torokhov 
622*de9ce703SDmitry Torokhov 	init_completion(&i8042_aux_irq_delivered);
623*de9ce703SDmitry Torokhov 	i8042_irq_being_tested = 1;
624*de9ce703SDmitry Torokhov 
625*de9ce703SDmitry Torokhov 	param = 0xa5;
626*de9ce703SDmitry Torokhov 	retval = __i8042_command(&param, I8042_CMD_AUX_LOOP & 0xf0ff);
627*de9ce703SDmitry Torokhov 
628*de9ce703SDmitry Torokhov 	spin_unlock_irqrestore(&i8042_lock, flags);
629*de9ce703SDmitry Torokhov 
630*de9ce703SDmitry Torokhov 	if (retval)
631*de9ce703SDmitry Torokhov 		goto out;
632*de9ce703SDmitry Torokhov 
633*de9ce703SDmitry Torokhov 	if (wait_for_completion_timeout(&i8042_aux_irq_delivered,
634*de9ce703SDmitry Torokhov 					msecs_to_jiffies(250)) == 0) {
635*de9ce703SDmitry Torokhov /*
636*de9ce703SDmitry Torokhov  * AUX IRQ was never delivered so we need to flush the controller to
637*de9ce703SDmitry Torokhov  * get rid of the byte we put there; otherwise keyboard may not work.
638*de9ce703SDmitry Torokhov  */
639*de9ce703SDmitry Torokhov 		i8042_flush();
640*de9ce703SDmitry Torokhov 		retval = -1;
641*de9ce703SDmitry Torokhov 	}
642*de9ce703SDmitry Torokhov 
643*de9ce703SDmitry Torokhov  out:
644*de9ce703SDmitry Torokhov 
645*de9ce703SDmitry Torokhov /*
6461da177e4SLinus Torvalds  * Disable the interface.
6471da177e4SLinus Torvalds  */
6481da177e4SLinus Torvalds 
6491da177e4SLinus Torvalds 	i8042_ctr |= I8042_CTR_AUXDIS;
6501da177e4SLinus Torvalds 	i8042_ctr &= ~I8042_CTR_AUXINT;
6511da177e4SLinus Torvalds 
6521da177e4SLinus Torvalds 	if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR))
653*de9ce703SDmitry Torokhov 		retval = -1;
654*de9ce703SDmitry Torokhov 
655*de9ce703SDmitry Torokhov 	if (irq_registered)
656*de9ce703SDmitry Torokhov 		free_irq(I8042_AUX_IRQ, i8042_platform_device);
657*de9ce703SDmitry Torokhov 
658*de9ce703SDmitry Torokhov 	return retval;
659*de9ce703SDmitry Torokhov }
660*de9ce703SDmitry Torokhov 
661*de9ce703SDmitry Torokhov static int i8042_controller_check(void)
662*de9ce703SDmitry Torokhov {
663*de9ce703SDmitry Torokhov 	if (i8042_flush() == I8042_BUFFER_SIZE) {
664*de9ce703SDmitry Torokhov 		printk(KERN_ERR "i8042.c: No controller found.\n");
665*de9ce703SDmitry Torokhov 		return -ENODEV;
666*de9ce703SDmitry Torokhov 	}
6671da177e4SLinus Torvalds 
6681da177e4SLinus Torvalds 	return 0;
6691da177e4SLinus Torvalds }
6701da177e4SLinus Torvalds 
671*de9ce703SDmitry Torokhov static int i8042_controller_selftest(void)
6722673c836SVojtech Pavlik {
6732673c836SVojtech Pavlik 	unsigned char param;
6742673c836SVojtech Pavlik 
6752673c836SVojtech Pavlik 	if (!i8042_reset)
6762673c836SVojtech Pavlik 		return 0;
6772673c836SVojtech Pavlik 
6782673c836SVojtech Pavlik 	if (i8042_command(&param, I8042_CMD_CTL_TEST)) {
6792673c836SVojtech Pavlik 		printk(KERN_ERR "i8042.c: i8042 controller self test timeout.\n");
680*de9ce703SDmitry Torokhov 		return -ENODEV;
6812673c836SVojtech Pavlik 	}
6822673c836SVojtech Pavlik 
6832673c836SVojtech Pavlik 	if (param != I8042_RET_CTL_TEST) {
6842673c836SVojtech Pavlik 		printk(KERN_ERR "i8042.c: i8042 controller selftest failed. (%#x != %#x)\n",
6852673c836SVojtech Pavlik 			 param, I8042_RET_CTL_TEST);
686*de9ce703SDmitry Torokhov 		return -EIO;
6872673c836SVojtech Pavlik 	}
6882673c836SVojtech Pavlik 
6892673c836SVojtech Pavlik 	return 0;
6902673c836SVojtech Pavlik }
6911da177e4SLinus Torvalds 
6921da177e4SLinus Torvalds /*
6931da177e4SLinus Torvalds  * i8042_controller init initializes the i8042 controller, and,
6941da177e4SLinus Torvalds  * most importantly, sets it into non-xlated mode if that's
6951da177e4SLinus Torvalds  * desired.
6961da177e4SLinus Torvalds  */
6971da177e4SLinus Torvalds 
6981da177e4SLinus Torvalds static int i8042_controller_init(void)
6991da177e4SLinus Torvalds {
7001da177e4SLinus Torvalds 	unsigned long flags;
7011da177e4SLinus Torvalds 
7021da177e4SLinus Torvalds /*
7031da177e4SLinus Torvalds  * Save the CTR for restoral on unload / reboot.
7041da177e4SLinus Torvalds  */
7051da177e4SLinus Torvalds 
7061da177e4SLinus Torvalds 	if (i8042_command(&i8042_ctr, I8042_CMD_CTL_RCTR)) {
7071da177e4SLinus Torvalds 		printk(KERN_ERR "i8042.c: Can't read CTR while initializing i8042.\n");
708*de9ce703SDmitry Torokhov 		return -EIO;
7091da177e4SLinus Torvalds 	}
7101da177e4SLinus Torvalds 
7111da177e4SLinus Torvalds 	i8042_initial_ctr = i8042_ctr;
7121da177e4SLinus Torvalds 
7131da177e4SLinus Torvalds /*
7141da177e4SLinus Torvalds  * Disable the keyboard interface and interrupt.
7151da177e4SLinus Torvalds  */
7161da177e4SLinus Torvalds 
7171da177e4SLinus Torvalds 	i8042_ctr |= I8042_CTR_KBDDIS;
7181da177e4SLinus Torvalds 	i8042_ctr &= ~I8042_CTR_KBDINT;
7191da177e4SLinus Torvalds 
7201da177e4SLinus Torvalds /*
7211da177e4SLinus Torvalds  * Handle keylock.
7221da177e4SLinus Torvalds  */
7231da177e4SLinus Torvalds 
7241da177e4SLinus Torvalds 	spin_lock_irqsave(&i8042_lock, flags);
7251da177e4SLinus Torvalds 	if (~i8042_read_status() & I8042_STR_KEYLOCK) {
7261da177e4SLinus Torvalds 		if (i8042_unlock)
7271da177e4SLinus Torvalds 			i8042_ctr |= I8042_CTR_IGNKEYLOCK;
7281da177e4SLinus Torvalds 		 else
7291da177e4SLinus Torvalds 			printk(KERN_WARNING "i8042.c: Warning: Keylock active.\n");
7301da177e4SLinus Torvalds 	}
7311da177e4SLinus Torvalds 	spin_unlock_irqrestore(&i8042_lock, flags);
7321da177e4SLinus Torvalds 
7331da177e4SLinus Torvalds /*
7341da177e4SLinus Torvalds  * If the chip is configured into nontranslated mode by the BIOS, don't
7351da177e4SLinus Torvalds  * bother enabling translating and be happy.
7361da177e4SLinus Torvalds  */
7371da177e4SLinus Torvalds 
7381da177e4SLinus Torvalds 	if (~i8042_ctr & I8042_CTR_XLATE)
7391da177e4SLinus Torvalds 		i8042_direct = 1;
7401da177e4SLinus Torvalds 
7411da177e4SLinus Torvalds /*
7421da177e4SLinus Torvalds  * Set nontranslated mode for the kbd interface if requested by an option.
7431da177e4SLinus Torvalds  * After this the kbd interface becomes a simple serial in/out, like the aux
7441da177e4SLinus Torvalds  * interface is. We don't do this by default, since it can confuse notebook
7451da177e4SLinus Torvalds  * BIOSes.
7461da177e4SLinus Torvalds  */
7471da177e4SLinus Torvalds 
7481da177e4SLinus Torvalds 	if (i8042_direct)
7491da177e4SLinus Torvalds 		i8042_ctr &= ~I8042_CTR_XLATE;
7501da177e4SLinus Torvalds 
7511da177e4SLinus Torvalds /*
7521da177e4SLinus Torvalds  * Write CTR back.
7531da177e4SLinus Torvalds  */
7541da177e4SLinus Torvalds 
7551da177e4SLinus Torvalds 	if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
7561da177e4SLinus Torvalds 		printk(KERN_ERR "i8042.c: Can't write CTR while initializing i8042.\n");
757*de9ce703SDmitry Torokhov 		return -EIO;
7581da177e4SLinus Torvalds 	}
7591da177e4SLinus Torvalds 
7601da177e4SLinus Torvalds 	return 0;
7611da177e4SLinus Torvalds }
7621da177e4SLinus Torvalds 
7631da177e4SLinus Torvalds 
7641da177e4SLinus Torvalds /*
765*de9ce703SDmitry Torokhov  * Reset the controller and reset CRT to the original value set by BIOS.
7661da177e4SLinus Torvalds  */
7671da177e4SLinus Torvalds 
768*de9ce703SDmitry Torokhov static void i8042_controller_reset(void)
769*de9ce703SDmitry Torokhov {
770*de9ce703SDmitry Torokhov 	i8042_flush();
7711da177e4SLinus Torvalds 
7721da177e4SLinus Torvalds /*
7731da177e4SLinus Torvalds  * Disable MUX mode if present.
7741da177e4SLinus Torvalds  */
7751da177e4SLinus Torvalds 
7761da177e4SLinus Torvalds 	if (i8042_mux_present)
7771da177e4SLinus Torvalds 		i8042_set_mux_mode(0, NULL);
7781da177e4SLinus Torvalds 
7791da177e4SLinus Torvalds /*
780*de9ce703SDmitry Torokhov  * Reset the controller if requested.
781*de9ce703SDmitry Torokhov  */
782*de9ce703SDmitry Torokhov 
783*de9ce703SDmitry Torokhov 	i8042_controller_selftest();
784*de9ce703SDmitry Torokhov 
785*de9ce703SDmitry Torokhov /*
7861da177e4SLinus Torvalds  * Restore the original control register setting.
7871da177e4SLinus Torvalds  */
7881da177e4SLinus Torvalds 
789*de9ce703SDmitry Torokhov 	if (i8042_command(&i8042_initial_ctr, I8042_CMD_CTL_WCTR))
7901da177e4SLinus Torvalds 		printk(KERN_WARNING "i8042.c: Can't restore CTR.\n");
7911da177e4SLinus Torvalds }
7921da177e4SLinus Torvalds 
7931da177e4SLinus Torvalds 
7941da177e4SLinus Torvalds /*
7951da177e4SLinus Torvalds  * Here we try to reset everything back to a state in which the BIOS will be
7961da177e4SLinus Torvalds  * able to talk to the hardware when rebooting.
7971da177e4SLinus Torvalds  */
7981da177e4SLinus Torvalds 
7991da177e4SLinus Torvalds static void i8042_controller_cleanup(void)
8001da177e4SLinus Torvalds {
8011da177e4SLinus Torvalds 	int i;
8021da177e4SLinus Torvalds 
8031da177e4SLinus Torvalds /*
8041da177e4SLinus Torvalds  * Reset anything that is connected to the ports.
8051da177e4SLinus Torvalds  */
8061da177e4SLinus Torvalds 
8071da177e4SLinus Torvalds 	for (i = 0; i < I8042_NUM_PORTS; i++)
808*de9ce703SDmitry Torokhov 		if (i8042_ports[i].serio)
8091da177e4SLinus Torvalds 			serio_cleanup(i8042_ports[i].serio);
8101da177e4SLinus Torvalds 
8111da177e4SLinus Torvalds 	i8042_controller_reset();
8121da177e4SLinus Torvalds }
8131da177e4SLinus Torvalds 
8141da177e4SLinus Torvalds 
8151da177e4SLinus Torvalds /*
8161da177e4SLinus Torvalds  * i8042_panic_blink() will flash the keyboard LEDs and is called when
8171da177e4SLinus Torvalds  * kernel panics. Flashing LEDs is useful for users running X who may
8181da177e4SLinus Torvalds  * not see the console and will help distingushing panics from "real"
8191da177e4SLinus Torvalds  * lockups.
8201da177e4SLinus Torvalds  *
8211da177e4SLinus Torvalds  * Note that DELAY has a limit of 10ms so we will not get stuck here
8221da177e4SLinus Torvalds  * waiting for KBC to free up even if KBD interrupt is off
8231da177e4SLinus Torvalds  */
8241da177e4SLinus Torvalds 
8251da177e4SLinus Torvalds #define DELAY do { mdelay(1); if (++delay > 10) return delay; } while(0)
8261da177e4SLinus Torvalds 
8271da177e4SLinus Torvalds static long i8042_panic_blink(long count)
8281da177e4SLinus Torvalds {
8291da177e4SLinus Torvalds 	long delay = 0;
8301da177e4SLinus Torvalds 	static long last_blink;
8311da177e4SLinus Torvalds 	static char led;
8321da177e4SLinus Torvalds 
8331da177e4SLinus Torvalds 	/*
8341da177e4SLinus Torvalds 	 * We expect frequency to be about 1/2s. KDB uses about 1s.
8351da177e4SLinus Torvalds 	 * Make sure they are different.
8361da177e4SLinus Torvalds 	 */
8371da177e4SLinus Torvalds 	if (!i8042_blink_frequency)
8381da177e4SLinus Torvalds 		return 0;
8391da177e4SLinus Torvalds 	if (count - last_blink < i8042_blink_frequency)
8401da177e4SLinus Torvalds 		return 0;
8411da177e4SLinus Torvalds 
8421da177e4SLinus Torvalds 	led ^= 0x01 | 0x04;
8431da177e4SLinus Torvalds 	while (i8042_read_status() & I8042_STR_IBF)
8441da177e4SLinus Torvalds 		DELAY;
8451da177e4SLinus Torvalds 	i8042_write_data(0xed); /* set leds */
8461da177e4SLinus Torvalds 	DELAY;
8471da177e4SLinus Torvalds 	while (i8042_read_status() & I8042_STR_IBF)
8481da177e4SLinus Torvalds 		DELAY;
8491da177e4SLinus Torvalds 	DELAY;
8501da177e4SLinus Torvalds 	i8042_write_data(led);
8511da177e4SLinus Torvalds 	DELAY;
8521da177e4SLinus Torvalds 	last_blink = count;
8531da177e4SLinus Torvalds 	return delay;
8541da177e4SLinus Torvalds }
8551da177e4SLinus Torvalds 
8561da177e4SLinus Torvalds #undef DELAY
8571da177e4SLinus Torvalds 
8581da177e4SLinus Torvalds /*
8591da177e4SLinus Torvalds  * Here we try to restore the original BIOS settings
8601da177e4SLinus Torvalds  */
8611da177e4SLinus Torvalds 
8623ae5eaecSRussell King static int i8042_suspend(struct platform_device *dev, pm_message_t state)
8631da177e4SLinus Torvalds {
864*de9ce703SDmitry Torokhov 	i8042_controller_cleanup();
8651da177e4SLinus Torvalds 
8661da177e4SLinus Torvalds 	return 0;
8671da177e4SLinus Torvalds }
8681da177e4SLinus Torvalds 
8691da177e4SLinus Torvalds 
8701da177e4SLinus Torvalds /*
8711da177e4SLinus Torvalds  * Here we try to reset everything back to a state in which suspended
8721da177e4SLinus Torvalds  */
8731da177e4SLinus Torvalds 
8743ae5eaecSRussell King static int i8042_resume(struct platform_device *dev)
8751da177e4SLinus Torvalds {
876*de9ce703SDmitry Torokhov 	int error;
8771da177e4SLinus Torvalds 
878*de9ce703SDmitry Torokhov 	error = i8042_controller_check();
879*de9ce703SDmitry Torokhov 	if (error)
880*de9ce703SDmitry Torokhov 		return error;
8812673c836SVojtech Pavlik 
882*de9ce703SDmitry Torokhov 	error = i8042_controller_selftest();
883*de9ce703SDmitry Torokhov 	if (error)
884*de9ce703SDmitry Torokhov 		return error;
885*de9ce703SDmitry Torokhov 
886*de9ce703SDmitry Torokhov /*
887*de9ce703SDmitry Torokhov  * Restore pre-resume CTR value and disable all ports
888*de9ce703SDmitry Torokhov  */
889*de9ce703SDmitry Torokhov 
890*de9ce703SDmitry Torokhov 	i8042_ctr |= I8042_CTR_AUXDIS | I8042_CTR_KBDDIS;
891*de9ce703SDmitry Torokhov 	i8042_ctr &= ~(I8042_CTR_AUXINT | I8042_CTR_KBDINT);
8922673c836SVojtech Pavlik 	if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
893*de9ce703SDmitry Torokhov 		printk(KERN_ERR "i8042: Can't write CTR to resume\n");
894*de9ce703SDmitry Torokhov 		return -EIO;
8951da177e4SLinus Torvalds 	}
8961da177e4SLinus Torvalds 
897*de9ce703SDmitry Torokhov 	if (i8042_mux_present) {
8981da177e4SLinus Torvalds 		if (i8042_set_mux_mode(1, NULL) || i8042_enable_mux_ports())
899*de9ce703SDmitry Torokhov 			printk(KERN_WARNING
900*de9ce703SDmitry Torokhov 				"i8042: failed to resume active multiplexor, "
901*de9ce703SDmitry Torokhov 				"mouse won't work.\n");
902*de9ce703SDmitry Torokhov 	} else if (i8042_ports[I8042_AUX_PORT_NO].serio)
903*de9ce703SDmitry Torokhov 		i8042_enable_aux_port();
9041da177e4SLinus Torvalds 
905*de9ce703SDmitry Torokhov 	if (i8042_ports[I8042_KBD_PORT_NO].serio)
906*de9ce703SDmitry Torokhov 		i8042_enable_kbd_port();
9071da177e4SLinus Torvalds 
908*de9ce703SDmitry Torokhov 	i8042_interrupt(0, NULL, NULL);
9091da177e4SLinus Torvalds 
9101da177e4SLinus Torvalds 	return 0;
9111da177e4SLinus Torvalds }
9121da177e4SLinus Torvalds 
9131da177e4SLinus Torvalds /*
9141da177e4SLinus Torvalds  * We need to reset the 8042 back to original mode on system shutdown,
9151da177e4SLinus Torvalds  * because otherwise BIOSes will be confused.
9161da177e4SLinus Torvalds  */
9171da177e4SLinus Torvalds 
9183ae5eaecSRussell King static void i8042_shutdown(struct platform_device *dev)
9191da177e4SLinus Torvalds {
9201da177e4SLinus Torvalds 	i8042_controller_cleanup();
9211da177e4SLinus Torvalds }
9221da177e4SLinus Torvalds 
92387fd6318SDmitry Torokhov static int __devinit i8042_create_kbd_port(void)
9241da177e4SLinus Torvalds {
9251da177e4SLinus Torvalds 	struct serio *serio;
9261da177e4SLinus Torvalds 	struct i8042_port *port = &i8042_ports[I8042_KBD_PORT_NO];
9271da177e4SLinus Torvalds 
928d39969deSDmitry Torokhov 	serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
9290854e52dSDmitry Torokhov 	if (!serio)
9300854e52dSDmitry Torokhov 		return -ENOMEM;
9310854e52dSDmitry Torokhov 
9321da177e4SLinus Torvalds 	serio->id.type		= i8042_direct ? SERIO_8042 : SERIO_8042_XL;
9331da177e4SLinus Torvalds 	serio->write		= i8042_dumbkbd ? NULL : i8042_kbd_write;
9341da177e4SLinus Torvalds 	serio->start		= i8042_start;
9351da177e4SLinus Torvalds 	serio->stop		= i8042_stop;
9361da177e4SLinus Torvalds 	serio->port_data	= port;
9371da177e4SLinus Torvalds 	serio->dev.parent	= &i8042_platform_device->dev;
938*de9ce703SDmitry Torokhov 	strlcpy(serio->name, "i8042 KBD port", sizeof(serio->name));
9391da177e4SLinus Torvalds 	strlcpy(serio->phys, I8042_KBD_PHYS_DESC, sizeof(serio->phys));
9401da177e4SLinus Torvalds 
9411da177e4SLinus Torvalds 	port->serio = serio;
942*de9ce703SDmitry Torokhov 	port->irq = I8042_KBD_IRQ;
9430854e52dSDmitry Torokhov 
944*de9ce703SDmitry Torokhov 	return 0;
9451da177e4SLinus Torvalds }
9461da177e4SLinus Torvalds 
947*de9ce703SDmitry Torokhov static int __devinit i8042_create_aux_port(int idx)
9481da177e4SLinus Torvalds {
9491da177e4SLinus Torvalds 	struct serio *serio;
950*de9ce703SDmitry Torokhov 	int port_no = idx < 0 ? I8042_AUX_PORT_NO : I8042_MUX_PORT_NO + idx;
951*de9ce703SDmitry Torokhov 	struct i8042_port *port = &i8042_ports[port_no];
9521da177e4SLinus Torvalds 
953d39969deSDmitry Torokhov 	serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
9540854e52dSDmitry Torokhov 	if (!serio)
9550854e52dSDmitry Torokhov 		return -ENOMEM;
9560854e52dSDmitry Torokhov 
9571da177e4SLinus Torvalds 	serio->id.type		= SERIO_8042;
9581da177e4SLinus Torvalds 	serio->write		= i8042_aux_write;
9591da177e4SLinus Torvalds 	serio->start		= i8042_start;
9601da177e4SLinus Torvalds 	serio->stop		= i8042_stop;
9611da177e4SLinus Torvalds 	serio->port_data	= port;
9621da177e4SLinus Torvalds 	serio->dev.parent	= &i8042_platform_device->dev;
963*de9ce703SDmitry Torokhov 	if (idx < 0) {
964*de9ce703SDmitry Torokhov 		strlcpy(serio->name, "i8042 AUX port", sizeof(serio->name));
9651da177e4SLinus Torvalds 		strlcpy(serio->phys, I8042_AUX_PHYS_DESC, sizeof(serio->phys));
966*de9ce703SDmitry Torokhov 	} else {
967*de9ce703SDmitry Torokhov 		snprintf(serio->name, sizeof(serio->name), "i8042 AUX%d port", idx);
968*de9ce703SDmitry Torokhov 		snprintf(serio->phys, sizeof(serio->phys), I8042_MUX_PHYS_DESC, idx + 1);
9691da177e4SLinus Torvalds 	}
9701da177e4SLinus Torvalds 
9711da177e4SLinus Torvalds 	port->serio = serio;
972*de9ce703SDmitry Torokhov 	port->mux = idx;
973*de9ce703SDmitry Torokhov 	port->irq = I8042_AUX_IRQ;
9740854e52dSDmitry Torokhov 
975*de9ce703SDmitry Torokhov 	return 0;
976*de9ce703SDmitry Torokhov }
977*de9ce703SDmitry Torokhov 
978*de9ce703SDmitry Torokhov static void __devinit i8042_free_kbd_port(void)
979*de9ce703SDmitry Torokhov {
980*de9ce703SDmitry Torokhov 	kfree(i8042_ports[I8042_KBD_PORT_NO].serio);
981*de9ce703SDmitry Torokhov 	i8042_ports[I8042_KBD_PORT_NO].serio = NULL;
982*de9ce703SDmitry Torokhov }
983*de9ce703SDmitry Torokhov 
984*de9ce703SDmitry Torokhov static void __devinit i8042_free_aux_ports(void)
985*de9ce703SDmitry Torokhov {
986*de9ce703SDmitry Torokhov 	int i;
987*de9ce703SDmitry Torokhov 
988*de9ce703SDmitry Torokhov 	for (i = I8042_AUX_PORT_NO; i < I8042_NUM_PORTS; i++) {
989*de9ce703SDmitry Torokhov 		kfree(i8042_ports[i].serio);
990*de9ce703SDmitry Torokhov 		i8042_ports[i].serio = NULL;
991*de9ce703SDmitry Torokhov 	}
992*de9ce703SDmitry Torokhov }
993*de9ce703SDmitry Torokhov 
994*de9ce703SDmitry Torokhov static void __devinit i8042_register_ports(void)
995*de9ce703SDmitry Torokhov {
996*de9ce703SDmitry Torokhov 	int i;
997*de9ce703SDmitry Torokhov 
998*de9ce703SDmitry Torokhov 	for (i = 0; i < I8042_NUM_PORTS; i++) {
999*de9ce703SDmitry Torokhov 		if (i8042_ports[i].serio) {
1000*de9ce703SDmitry Torokhov 			printk(KERN_INFO "serio: %s at %#lx,%#lx irq %d\n",
1001*de9ce703SDmitry Torokhov 				i8042_ports[i].serio->name,
1002*de9ce703SDmitry Torokhov 				(unsigned long) I8042_DATA_REG,
1003*de9ce703SDmitry Torokhov 				(unsigned long) I8042_COMMAND_REG,
1004*de9ce703SDmitry Torokhov 				i8042_ports[i].irq);
1005*de9ce703SDmitry Torokhov 			serio_register_port(i8042_ports[i].serio);
1006*de9ce703SDmitry Torokhov 		}
1007*de9ce703SDmitry Torokhov 	}
1008*de9ce703SDmitry Torokhov }
1009*de9ce703SDmitry Torokhov 
1010*de9ce703SDmitry Torokhov static void __devinit i8042_unregister_ports(void)
1011*de9ce703SDmitry Torokhov {
1012*de9ce703SDmitry Torokhov 	int i;
1013*de9ce703SDmitry Torokhov 
1014*de9ce703SDmitry Torokhov 	for (i = 0; i < I8042_NUM_PORTS; i++) {
1015*de9ce703SDmitry Torokhov 		if (i8042_ports[i].serio) {
1016*de9ce703SDmitry Torokhov 			serio_unregister_port(i8042_ports[i].serio);
1017*de9ce703SDmitry Torokhov 			i8042_ports[i].serio = NULL;
1018*de9ce703SDmitry Torokhov 		}
1019*de9ce703SDmitry Torokhov 	}
1020*de9ce703SDmitry Torokhov }
1021*de9ce703SDmitry Torokhov 
1022*de9ce703SDmitry Torokhov static void i8042_free_irqs(void)
1023*de9ce703SDmitry Torokhov {
1024*de9ce703SDmitry Torokhov 	if (i8042_aux_irq_registered)
1025*de9ce703SDmitry Torokhov 		free_irq(I8042_AUX_IRQ, i8042_platform_device);
1026*de9ce703SDmitry Torokhov 	if (i8042_kbd_irq_registered)
1027*de9ce703SDmitry Torokhov 		free_irq(I8042_KBD_IRQ, i8042_platform_device);
1028*de9ce703SDmitry Torokhov 
1029*de9ce703SDmitry Torokhov 	i8042_aux_irq_registered = i8042_kbd_irq_registered = 0;
1030*de9ce703SDmitry Torokhov }
1031*de9ce703SDmitry Torokhov 
1032*de9ce703SDmitry Torokhov static int __devinit i8042_setup_aux(void)
1033*de9ce703SDmitry Torokhov {
1034*de9ce703SDmitry Torokhov 	int (*aux_enable)(void);
1035*de9ce703SDmitry Torokhov 	int error;
1036*de9ce703SDmitry Torokhov 	int i;
1037*de9ce703SDmitry Torokhov 
1038*de9ce703SDmitry Torokhov 	if (i8042_check_aux())
1039*de9ce703SDmitry Torokhov 		return -ENODEV;
1040*de9ce703SDmitry Torokhov 
1041*de9ce703SDmitry Torokhov 	if (i8042_nomux || i8042_check_mux()) {
1042*de9ce703SDmitry Torokhov 		error = i8042_create_aux_port(-1);
1043*de9ce703SDmitry Torokhov 		if (error)
1044*de9ce703SDmitry Torokhov 			goto err_free_ports;
1045*de9ce703SDmitry Torokhov 		aux_enable = i8042_enable_aux_port;
1046*de9ce703SDmitry Torokhov 	} else {
1047*de9ce703SDmitry Torokhov 		for (i = 0; i < I8042_NUM_MUX_PORTS; i++) {
1048*de9ce703SDmitry Torokhov 			error = i8042_create_aux_port(i);
1049*de9ce703SDmitry Torokhov 			if (error)
1050*de9ce703SDmitry Torokhov 				goto err_free_ports;
1051*de9ce703SDmitry Torokhov 		}
1052*de9ce703SDmitry Torokhov 		aux_enable = i8042_enable_mux_ports;
1053*de9ce703SDmitry Torokhov 	}
1054*de9ce703SDmitry Torokhov 
1055*de9ce703SDmitry Torokhov 	error = request_irq(I8042_AUX_IRQ, i8042_interrupt, IRQF_SHARED,
1056*de9ce703SDmitry Torokhov 			    "i8042", i8042_platform_device);
1057*de9ce703SDmitry Torokhov 	if (error)
1058*de9ce703SDmitry Torokhov 		goto err_free_ports;
1059*de9ce703SDmitry Torokhov 
1060*de9ce703SDmitry Torokhov 	if (aux_enable())
1061*de9ce703SDmitry Torokhov 		goto err_free_irq;
1062*de9ce703SDmitry Torokhov 
1063*de9ce703SDmitry Torokhov 	i8042_aux_irq_registered = 1;
1064*de9ce703SDmitry Torokhov 	return 0;
1065*de9ce703SDmitry Torokhov 
1066*de9ce703SDmitry Torokhov  err_free_irq:
1067*de9ce703SDmitry Torokhov 	free_irq(I8042_AUX_IRQ, i8042_platform_device);
1068*de9ce703SDmitry Torokhov  err_free_ports:
1069*de9ce703SDmitry Torokhov 	i8042_free_aux_ports();
1070*de9ce703SDmitry Torokhov 	return error;
1071*de9ce703SDmitry Torokhov }
1072*de9ce703SDmitry Torokhov 
1073*de9ce703SDmitry Torokhov static int __devinit i8042_setup_kbd(void)
1074*de9ce703SDmitry Torokhov {
1075*de9ce703SDmitry Torokhov 	int error;
1076*de9ce703SDmitry Torokhov 
1077*de9ce703SDmitry Torokhov 	error = i8042_create_kbd_port();
1078*de9ce703SDmitry Torokhov 	if (error)
1079*de9ce703SDmitry Torokhov 		return error;
1080*de9ce703SDmitry Torokhov 
1081*de9ce703SDmitry Torokhov 	error = request_irq(I8042_KBD_IRQ, i8042_interrupt, IRQF_SHARED,
1082*de9ce703SDmitry Torokhov 			    "i8042", i8042_platform_device);
1083*de9ce703SDmitry Torokhov 	if (error)
1084*de9ce703SDmitry Torokhov 		goto err_free_port;
1085*de9ce703SDmitry Torokhov 
1086*de9ce703SDmitry Torokhov 	error = i8042_enable_kbd_port();
1087*de9ce703SDmitry Torokhov 	if (error)
1088*de9ce703SDmitry Torokhov 		goto err_free_irq;
1089*de9ce703SDmitry Torokhov 
1090*de9ce703SDmitry Torokhov 	i8042_kbd_irq_registered = 1;
1091*de9ce703SDmitry Torokhov 	return 0;
1092*de9ce703SDmitry Torokhov 
1093*de9ce703SDmitry Torokhov  err_free_irq:
1094*de9ce703SDmitry Torokhov 	free_irq(I8042_KBD_IRQ, i8042_platform_device);
1095*de9ce703SDmitry Torokhov  err_free_port:
1096*de9ce703SDmitry Torokhov 	i8042_free_kbd_port();
1097*de9ce703SDmitry Torokhov 	return error;
10981da177e4SLinus Torvalds }
10991da177e4SLinus Torvalds 
110087fd6318SDmitry Torokhov static int __devinit i8042_probe(struct platform_device *dev)
11011da177e4SLinus Torvalds {
1102*de9ce703SDmitry Torokhov 	int error;
11031da177e4SLinus Torvalds 
1104*de9ce703SDmitry Torokhov 	error = i8042_controller_selftest();
1105*de9ce703SDmitry Torokhov 	if (error)
1106*de9ce703SDmitry Torokhov 		return error;
11071da177e4SLinus Torvalds 
1108*de9ce703SDmitry Torokhov 	error = i8042_controller_init();
1109*de9ce703SDmitry Torokhov 	if (error)
1110*de9ce703SDmitry Torokhov 		return error;
11111da177e4SLinus Torvalds 
1112*de9ce703SDmitry Torokhov 	if (!i8042_noaux) {
1113*de9ce703SDmitry Torokhov 		error = i8042_setup_aux();
1114*de9ce703SDmitry Torokhov 		if (error && error != -ENODEV && error != -EBUSY)
1115*de9ce703SDmitry Torokhov 			goto out_fail;
11161da177e4SLinus Torvalds 	}
11171da177e4SLinus Torvalds 
1118945ef0d4SDmitry Torokhov 	if (!i8042_nokbd) {
1119*de9ce703SDmitry Torokhov 		error = i8042_setup_kbd();
1120*de9ce703SDmitry Torokhov 		if (error)
1121*de9ce703SDmitry Torokhov 			goto out_fail;
1122945ef0d4SDmitry Torokhov 	}
1123945ef0d4SDmitry Torokhov 
1124*de9ce703SDmitry Torokhov /*
1125*de9ce703SDmitry Torokhov  * Ok, everything is ready, let's register all serio ports
1126*de9ce703SDmitry Torokhov  */
1127*de9ce703SDmitry Torokhov 	i8042_register_ports();
11281da177e4SLinus Torvalds 
11291da177e4SLinus Torvalds 	return 0;
11300854e52dSDmitry Torokhov 
1131*de9ce703SDmitry Torokhov  out_fail:
1132*de9ce703SDmitry Torokhov 	i8042_free_aux_ports();	/* in case KBD failed but AUX not */
1133*de9ce703SDmitry Torokhov 	i8042_free_irqs();
1134*de9ce703SDmitry Torokhov 	i8042_controller_reset();
11350854e52dSDmitry Torokhov 
1136*de9ce703SDmitry Torokhov 	return error;
11371da177e4SLinus Torvalds }
11381da177e4SLinus Torvalds 
113987fd6318SDmitry Torokhov static int __devexit i8042_remove(struct platform_device *dev)
11401da177e4SLinus Torvalds {
1141*de9ce703SDmitry Torokhov 	i8042_unregister_ports();
1142*de9ce703SDmitry Torokhov 	i8042_free_irqs();
1143*de9ce703SDmitry Torokhov 	i8042_controller_reset();
11441da177e4SLinus Torvalds 
114587fd6318SDmitry Torokhov 	return 0;
114687fd6318SDmitry Torokhov }
114787fd6318SDmitry Torokhov 
114887fd6318SDmitry Torokhov static struct platform_driver i8042_driver = {
114987fd6318SDmitry Torokhov 	.driver		= {
115087fd6318SDmitry Torokhov 		.name	= "i8042",
115187fd6318SDmitry Torokhov 		.owner	= THIS_MODULE,
115287fd6318SDmitry Torokhov 	},
115387fd6318SDmitry Torokhov 	.probe		= i8042_probe,
115487fd6318SDmitry Torokhov 	.remove		= __devexit_p(i8042_remove),
115587fd6318SDmitry Torokhov 	.suspend	= i8042_suspend,
115687fd6318SDmitry Torokhov 	.resume		= i8042_resume,
115787fd6318SDmitry Torokhov 	.shutdown	= i8042_shutdown,
115887fd6318SDmitry Torokhov };
115987fd6318SDmitry Torokhov 
116087fd6318SDmitry Torokhov static int __init i8042_init(void)
116187fd6318SDmitry Torokhov {
116287fd6318SDmitry Torokhov 	int err;
116387fd6318SDmitry Torokhov 
116487fd6318SDmitry Torokhov 	dbg_init();
116587fd6318SDmitry Torokhov 
116687fd6318SDmitry Torokhov 	err = i8042_platform_init();
116787fd6318SDmitry Torokhov 	if (err)
116887fd6318SDmitry Torokhov 		return err;
116987fd6318SDmitry Torokhov 
1170*de9ce703SDmitry Torokhov 	err = i8042_controller_check();
1171*de9ce703SDmitry Torokhov 	if (err)
1172*de9ce703SDmitry Torokhov 		goto err_platform_exit;
117387fd6318SDmitry Torokhov 
117487fd6318SDmitry Torokhov 	err = platform_driver_register(&i8042_driver);
117587fd6318SDmitry Torokhov 	if (err)
117687fd6318SDmitry Torokhov 		goto err_platform_exit;
117787fd6318SDmitry Torokhov 
117887fd6318SDmitry Torokhov 	i8042_platform_device = platform_device_alloc("i8042", -1);
117987fd6318SDmitry Torokhov 	if (!i8042_platform_device) {
118087fd6318SDmitry Torokhov 		err = -ENOMEM;
118187fd6318SDmitry Torokhov 		goto err_unregister_driver;
118287fd6318SDmitry Torokhov 	}
118387fd6318SDmitry Torokhov 
118487fd6318SDmitry Torokhov 	err = platform_device_add(i8042_platform_device);
118587fd6318SDmitry Torokhov 	if (err)
118687fd6318SDmitry Torokhov 		goto err_free_device;
118787fd6318SDmitry Torokhov 
1188*de9ce703SDmitry Torokhov 	panic_blink = i8042_panic_blink;
1189*de9ce703SDmitry Torokhov 
119087fd6318SDmitry Torokhov 	return 0;
119187fd6318SDmitry Torokhov 
119287fd6318SDmitry Torokhov  err_free_device:
119387fd6318SDmitry Torokhov 	platform_device_put(i8042_platform_device);
119487fd6318SDmitry Torokhov  err_unregister_driver:
119587fd6318SDmitry Torokhov 	platform_driver_unregister(&i8042_driver);
119687fd6318SDmitry Torokhov  err_platform_exit:
119787fd6318SDmitry Torokhov 	i8042_platform_exit();
119887fd6318SDmitry Torokhov 
119987fd6318SDmitry Torokhov 	return err;
120087fd6318SDmitry Torokhov }
120187fd6318SDmitry Torokhov 
120287fd6318SDmitry Torokhov static void __exit i8042_exit(void)
120387fd6318SDmitry Torokhov {
12041da177e4SLinus Torvalds 	platform_device_unregister(i8042_platform_device);
12053ae5eaecSRussell King 	platform_driver_unregister(&i8042_driver);
12061da177e4SLinus Torvalds 	i8042_platform_exit();
12071da177e4SLinus Torvalds 
12081da177e4SLinus Torvalds 	panic_blink = NULL;
12091da177e4SLinus Torvalds }
12101da177e4SLinus Torvalds 
12111da177e4SLinus Torvalds module_init(i8042_init);
12121da177e4SLinus Torvalds module_exit(i8042_exit);
1213