
From: Alan Cox <alan@redhat.com>

Fedora 2 showed up a pattern of failing SMP E750x chipset systems.  Initial
investigation suggests that the SMM code for these fails on all but the
boot processor.  Code inspection also found an apparent i8042 access delay
violation.

This patch has been tested with Fedora users affected by the problem and
appears to do the trick.  With this fixed the only clear keyboard problems
we see are in EMachines 680[57] (but not 9 it appears) AMD64 laptops.

The fix is fairly simple.  On a box that fails to respond correctly we
disable SMM emulation for PS/2 ports via USB devices.  Another approach
would be to disable it always or to load USB first.  Both of these other
approaches broke other systems in different ways.

Patch-By: Alan Cox <alan@redhat.com>
OSDL Certificate Of Origin 1.0 included herein by reference

Signed-off-by: Andrew Morton <akpm@osdl.org>
---

 25-akpm/drivers/input/serio/i8042.c |   79 ++++++++++++++++++++++++++++++++++--
 1 files changed, 76 insertions(+), 3 deletions(-)

diff -puN drivers/input/serio/i8042.c~fix-smm-failures-on-e750x-systems drivers/input/serio/i8042.c
--- 25/drivers/input/serio/i8042.c~fix-smm-failures-on-e750x-systems	2004-11-15 20:01:12.810729432 -0800
+++ 25-akpm/drivers/input/serio/i8042.c	2004-11-15 20:01:12.814728824 -0800
@@ -18,6 +18,7 @@
 #include <linux/config.h>
 #include <linux/init.h>
 #include <linux/serio.h>
+#include <linux/pci.h>
 #include <linux/err.h>
 #include <linux/rcupdate.h>
 
@@ -699,6 +700,70 @@ static void i8042_timer_func(unsigned lo
 }
 
 
+static int i8042_spank_usb(void)
+{
+	struct pci_dev *usb = NULL;
+	int found = 0;
+	u16 word;
+	unsigned long addr;
+	unsigned long len;
+	int i;
+
+	while ((usb = pci_get_class((PCI_CLASS_SERIAL_USB << 8), usb)) != NULL)
+	{
+		/* UHCI controller not in legacy ? */
+
+		pci_read_config_word(usb, 0xC0, &word);
+		if(word & 0x2000)
+			continue;
+
+		/*
+		 * Check it is enabled. If the port is active in legacy mode
+		 * then this will be mapped already
+		 */
+
+		for (i = 0; i < PCI_ROM_RESOURCE; i++) {
+			if (!(pci_resource_flags (usb, i) & IORESOURCE_IO))
+				continue;
+		}
+		if (i == PCI_ROM_RESOURCE)
+			continue;
+
+		/*
+		 *	Retrieve the bits
+		 */
+
+		addr = pci_resource_start(usb, i);
+		len = pci_resource_len (usb, i);
+
+		/*
+		 *	Check its configured and not in use
+		 */
+		if (addr == 0)
+			continue;
+		if (request_region(addr, len, "usb-whackamole"))
+			continue;
+
+		/*
+		 *	Kick the problem controller out of legacy mode
+		 *	so things like the E750x don't break
+		 */
+
+		outw(0, addr + 4);		/* IRQ Mask */
+		outw(4, addr);			/* Reset */
+		msleep(20);
+		outw(0, addr);
+
+		msleep(20);
+		/* Now take if off the BIOS */
+		pci_write_config_word(usb, 0xC0, 0x2000);
+		release_region(addr, len);
+
+		found = 1;
+	}
+	return found;
+}
+
 /*
  * i8042_controller init initializes the i8042 controller, and,
  * most importantly, sets it into non-xlated mode if that's
@@ -708,6 +773,7 @@ static void i8042_timer_func(unsigned lo
 static int i8042_controller_init(void)
 {
 	unsigned long flags;
+	int tries = 0;
 
 /*
  * Test the i8042. We need to know if it thinks it's working correctly
@@ -736,9 +802,16 @@ static int i8042_controller_init(void)
  * Save the CTR for restoral on unload / reboot.
  */
 
-	if (i8042_command(&i8042_ctr, I8042_CMD_CTL_RCTR)) {
-		printk(KERN_ERR "i8042.c: Can't read CTR while initializing i8042.\n");
-		return -1;
+	while (i8042_command(&i8042_ctr, I8042_CMD_CTL_RCTR)) {
+		if (tries > 3 || !i8042_spank_usb()) {
+			printk(KERN_ERR "i8042.c: Can't read CTR while "
+					"initializing i8042.\n");
+			return -1;
+		}
+		printk(KERN_WARNING "i8042.c: Can't read CTR, disabling USB "
+					"legacy and retrying.\n");
+		i8042_flush();
+		tries++;
 	}
 
 	i8042_initial_ctr = i8042_ctr;
_
