Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * i8042 keyboard and mouse controller driver for Linux |
| 3 | * |
| 4 | * Copyright (c) 1999-2004 Vojtech Pavlik |
| 5 | */ |
| 6 | |
| 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. |
| 11 | */ |
| 12 | |
Joe Perches | 4eb3c30 | 2010-11-29 23:33:07 -0800 | [diff] [blame] | 13 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 14 | |
Dmitry Torokhov | 7e044e0 | 2009-05-09 16:08:05 -0700 | [diff] [blame] | 15 | #include <linux/types.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/delay.h> |
| 17 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/interrupt.h> |
| 19 | #include <linux/ioport.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <linux/init.h> |
| 21 | #include <linux/serio.h> |
| 22 | #include <linux/err.h> |
| 23 | #include <linux/rcupdate.h> |
Russell King | d052d1b | 2005-10-29 19:07:23 +0100 | [diff] [blame] | 24 | #include <linux/platform_device.h> |
Márton Németh | 553a05b | 2007-10-22 00:56:52 -0400 | [diff] [blame] | 25 | #include <linux/i8042.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 26 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
| 28 | #include <asm/io.h> |
| 29 | |
| 30 | MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>"); |
| 31 | MODULE_DESCRIPTION("i8042 keyboard and mouse controller driver"); |
| 32 | MODULE_LICENSE("GPL"); |
| 33 | |
Dmitry Torokhov | 386b384 | 2009-09-09 19:08:16 -0700 | [diff] [blame] | 34 | static bool i8042_nokbd; |
Dmitry Torokhov | 945ef0d | 2005-09-04 01:42:00 -0500 | [diff] [blame] | 35 | module_param_named(nokbd, i8042_nokbd, bool, 0); |
| 36 | MODULE_PARM_DESC(nokbd, "Do not probe or use KBD port."); |
| 37 | |
Dmitry Torokhov | 386b384 | 2009-09-09 19:08:16 -0700 | [diff] [blame] | 38 | static bool i8042_noaux; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | module_param_named(noaux, i8042_noaux, bool, 0); |
| 40 | MODULE_PARM_DESC(noaux, "Do not probe or use AUX (mouse) port."); |
| 41 | |
Dmitry Torokhov | 386b384 | 2009-09-09 19:08:16 -0700 | [diff] [blame] | 42 | static bool i8042_nomux; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | module_param_named(nomux, i8042_nomux, bool, 0); |
Dominik Brodowski | 2c860a1 | 2010-04-05 22:29:09 -0700 | [diff] [blame] | 44 | MODULE_PARM_DESC(nomux, "Do not check whether an active multiplexing controller is present."); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
Dmitry Torokhov | 386b384 | 2009-09-09 19:08:16 -0700 | [diff] [blame] | 46 | static bool i8042_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | module_param_named(unlock, i8042_unlock, bool, 0); |
| 48 | MODULE_PARM_DESC(unlock, "Ignore keyboard lock."); |
| 49 | |
Dmitry Torokhov | 386b384 | 2009-09-09 19:08:16 -0700 | [diff] [blame] | 50 | static bool i8042_reset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | module_param_named(reset, i8042_reset, bool, 0); |
| 52 | MODULE_PARM_DESC(reset, "Reset controller during init and cleanup."); |
| 53 | |
Dmitry Torokhov | 386b384 | 2009-09-09 19:08:16 -0700 | [diff] [blame] | 54 | static bool i8042_direct; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | module_param_named(direct, i8042_direct, bool, 0); |
| 56 | MODULE_PARM_DESC(direct, "Put keyboard port into non-translated mode."); |
| 57 | |
Dmitry Torokhov | 386b384 | 2009-09-09 19:08:16 -0700 | [diff] [blame] | 58 | static bool i8042_dumbkbd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | module_param_named(dumbkbd, i8042_dumbkbd, bool, 0); |
| 60 | MODULE_PARM_DESC(dumbkbd, "Pretend that controller can only read data from keyboard"); |
| 61 | |
Dmitry Torokhov | 386b384 | 2009-09-09 19:08:16 -0700 | [diff] [blame] | 62 | static bool i8042_noloop; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | module_param_named(noloop, i8042_noloop, bool, 0); |
| 64 | MODULE_PARM_DESC(noloop, "Disable the AUX Loopback command while probing for the AUX port"); |
| 65 | |
Jiri Kosina | f8313ef | 2011-01-08 01:37:26 -0800 | [diff] [blame] | 66 | static bool i8042_notimeout; |
| 67 | module_param_named(notimeout, i8042_notimeout, bool, 0); |
| 68 | MODULE_PARM_DESC(notimeout, "Ignore timeouts signalled by i8042"); |
| 69 | |
Carlos Corbacho | 8987fec | 2008-01-21 01:04:40 -0500 | [diff] [blame] | 70 | #ifdef CONFIG_X86 |
Dmitry Torokhov | 386b384 | 2009-09-09 19:08:16 -0700 | [diff] [blame] | 71 | static bool i8042_dritek; |
Carlos Corbacho | 8987fec | 2008-01-21 01:04:40 -0500 | [diff] [blame] | 72 | module_param_named(dritek, i8042_dritek, bool, 0); |
| 73 | MODULE_PARM_DESC(dritek, "Force enable the Dritek keyboard extension"); |
| 74 | #endif |
| 75 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | #ifdef CONFIG_PNP |
Dmitry Torokhov | 386b384 | 2009-09-09 19:08:16 -0700 | [diff] [blame] | 77 | static bool i8042_nopnp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | module_param_named(nopnp, i8042_nopnp, bool, 0); |
| 79 | MODULE_PARM_DESC(nopnp, "Do not use PNP to detect controller settings"); |
| 80 | #endif |
| 81 | |
| 82 | #define DEBUG |
| 83 | #ifdef DEBUG |
Dmitry Torokhov | 386b384 | 2009-09-09 19:08:16 -0700 | [diff] [blame] | 84 | static bool i8042_debug; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | module_param_named(debug, i8042_debug, bool, 0600); |
| 86 | MODULE_PARM_DESC(debug, "Turn i8042 debugging mode on and off"); |
| 87 | #endif |
| 88 | |
Dmitry Torokhov | 1c7827a | 2009-09-03 21:45:34 -0700 | [diff] [blame] | 89 | static bool i8042_bypass_aux_irq_test; |
Hans de Goede | a7c5868 | 2014-04-19 20:47:35 -0700 | [diff] [blame] | 90 | static char i8042_kbd_firmware_id[128]; |
| 91 | static char i8042_aux_firmware_id[128]; |
Dmitry Torokhov | 1c7827a | 2009-09-03 21:45:34 -0700 | [diff] [blame] | 92 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | #include "i8042.h" |
| 94 | |
Dmitry Torokhov | 181d683 | 2009-09-16 01:06:43 -0700 | [diff] [blame] | 95 | /* |
| 96 | * i8042_lock protects serialization between i8042_command and |
| 97 | * the interrupt handler. |
| 98 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | static DEFINE_SPINLOCK(i8042_lock); |
| 100 | |
Dmitry Torokhov | 181d683 | 2009-09-16 01:06:43 -0700 | [diff] [blame] | 101 | /* |
| 102 | * Writers to AUX and KBD ports as well as users issuing i8042_command |
| 103 | * directly should acquire i8042_mutex (by means of calling |
| 104 | * i8042_lock_chip() and i8042_unlock_ship() helpers) to ensure that |
| 105 | * they do not disturb each other (unfortunately in many i8042 |
| 106 | * implementations write to one of the ports will immediately abort |
| 107 | * command that is being processed by another port). |
| 108 | */ |
| 109 | static DEFINE_MUTEX(i8042_mutex); |
| 110 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | struct i8042_port { |
| 112 | struct serio *serio; |
| 113 | int irq; |
Dmitry Torokhov | 386b384 | 2009-09-09 19:08:16 -0700 | [diff] [blame] | 114 | bool exists; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | signed char mux; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | }; |
| 117 | |
| 118 | #define I8042_KBD_PORT_NO 0 |
| 119 | #define I8042_AUX_PORT_NO 1 |
| 120 | #define I8042_MUX_PORT_NO 2 |
| 121 | #define I8042_NUM_PORTS (I8042_NUM_MUX_PORTS + 2) |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 122 | |
| 123 | static struct i8042_port i8042_ports[I8042_NUM_PORTS]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | |
| 125 | static unsigned char i8042_initial_ctr; |
| 126 | static unsigned char i8042_ctr; |
Dmitry Torokhov | 386b384 | 2009-09-09 19:08:16 -0700 | [diff] [blame] | 127 | static bool i8042_mux_present; |
| 128 | static bool i8042_kbd_irq_registered; |
| 129 | static bool i8042_aux_irq_registered; |
Dmitry Torokhov | 817e6ba | 2006-10-11 01:44:28 -0400 | [diff] [blame] | 130 | static unsigned char i8042_suppress_kbd_ack; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | static struct platform_device *i8042_platform_device; |
| 132 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 133 | static irqreturn_t i8042_interrupt(int irq, void *dev_id); |
Matthew Garrett | 967c9ef | 2009-12-11 22:00:57 -0800 | [diff] [blame] | 134 | static bool (*i8042_platform_filter)(unsigned char data, unsigned char str, |
| 135 | struct serio *serio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | |
Dmitry Torokhov | 181d683 | 2009-09-16 01:06:43 -0700 | [diff] [blame] | 137 | void i8042_lock_chip(void) |
| 138 | { |
| 139 | mutex_lock(&i8042_mutex); |
| 140 | } |
| 141 | EXPORT_SYMBOL(i8042_lock_chip); |
| 142 | |
| 143 | void i8042_unlock_chip(void) |
| 144 | { |
| 145 | mutex_unlock(&i8042_mutex); |
| 146 | } |
| 147 | EXPORT_SYMBOL(i8042_unlock_chip); |
| 148 | |
Matthew Garrett | 967c9ef | 2009-12-11 22:00:57 -0800 | [diff] [blame] | 149 | int i8042_install_filter(bool (*filter)(unsigned char data, unsigned char str, |
| 150 | struct serio *serio)) |
| 151 | { |
| 152 | unsigned long flags; |
| 153 | int ret = 0; |
| 154 | |
| 155 | spin_lock_irqsave(&i8042_lock, flags); |
| 156 | |
| 157 | if (i8042_platform_filter) { |
| 158 | ret = -EBUSY; |
| 159 | goto out; |
| 160 | } |
| 161 | |
| 162 | i8042_platform_filter = filter; |
| 163 | |
| 164 | out: |
| 165 | spin_unlock_irqrestore(&i8042_lock, flags); |
| 166 | return ret; |
| 167 | } |
| 168 | EXPORT_SYMBOL(i8042_install_filter); |
| 169 | |
| 170 | int i8042_remove_filter(bool (*filter)(unsigned char data, unsigned char str, |
| 171 | struct serio *port)) |
| 172 | { |
| 173 | unsigned long flags; |
| 174 | int ret = 0; |
| 175 | |
| 176 | spin_lock_irqsave(&i8042_lock, flags); |
| 177 | |
| 178 | if (i8042_platform_filter != filter) { |
| 179 | ret = -EINVAL; |
| 180 | goto out; |
| 181 | } |
| 182 | |
| 183 | i8042_platform_filter = NULL; |
| 184 | |
| 185 | out: |
| 186 | spin_unlock_irqrestore(&i8042_lock, flags); |
| 187 | return ret; |
| 188 | } |
| 189 | EXPORT_SYMBOL(i8042_remove_filter); |
| 190 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | /* |
| 192 | * The i8042_wait_read() and i8042_wait_write functions wait for the i8042 to |
| 193 | * be ready for reading values from it / writing values to it. |
| 194 | * Called always with i8042_lock held. |
| 195 | */ |
| 196 | |
| 197 | static int i8042_wait_read(void) |
| 198 | { |
| 199 | int i = 0; |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 200 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | while ((~i8042_read_status() & I8042_STR_OBF) && (i < I8042_CTL_TIMEOUT)) { |
| 202 | udelay(50); |
| 203 | i++; |
| 204 | } |
| 205 | return -(i == I8042_CTL_TIMEOUT); |
| 206 | } |
| 207 | |
| 208 | static int i8042_wait_write(void) |
| 209 | { |
| 210 | int i = 0; |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 211 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | while ((i8042_read_status() & I8042_STR_IBF) && (i < I8042_CTL_TIMEOUT)) { |
| 213 | udelay(50); |
| 214 | i++; |
| 215 | } |
| 216 | return -(i == I8042_CTL_TIMEOUT); |
| 217 | } |
| 218 | |
| 219 | /* |
| 220 | * i8042_flush() flushes all data that may be in the keyboard and mouse buffers |
| 221 | * of the i8042 down the toilet. |
| 222 | */ |
| 223 | |
| 224 | static int i8042_flush(void) |
| 225 | { |
| 226 | unsigned long flags; |
| 227 | unsigned char data, str; |
Andrey Moiseev | 2f0d260 | 2013-09-16 15:17:31 -0700 | [diff] [blame] | 228 | int count = 0; |
| 229 | int retval = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | |
| 231 | spin_lock_irqsave(&i8042_lock, flags); |
| 232 | |
Andrey Moiseev | 2f0d260 | 2013-09-16 15:17:31 -0700 | [diff] [blame] | 233 | while ((str = i8042_read_status()) & I8042_STR_OBF) { |
| 234 | if (count++ < I8042_BUFFER_SIZE) { |
| 235 | udelay(50); |
| 236 | data = i8042_read_data(); |
| 237 | dbg("%02x <- i8042 (flush, %s)\n", |
| 238 | data, str & I8042_STR_AUXDATA ? "aux" : "kbd"); |
| 239 | } else { |
| 240 | retval = -EIO; |
| 241 | break; |
| 242 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | spin_unlock_irqrestore(&i8042_lock, flags); |
| 246 | |
Andrey Moiseev | 2f0d260 | 2013-09-16 15:17:31 -0700 | [diff] [blame] | 247 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | /* |
| 251 | * i8042_command() executes a command on the i8042. It also sends the input |
| 252 | * parameter(s) of the commands to it, and receives the output value(s). The |
| 253 | * parameters are to be stored in the param array, and the output is placed |
| 254 | * into the same array. The number of the parameters and output values is |
| 255 | * encoded in bits 8-11 of the command number. |
| 256 | */ |
| 257 | |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 258 | static int __i8042_command(unsigned char *param, int command) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | { |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 260 | int i, error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | |
| 262 | if (i8042_noloop && command == I8042_CMD_AUX_LOOP) |
| 263 | return -1; |
| 264 | |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 265 | error = i8042_wait_write(); |
| 266 | if (error) |
| 267 | return error; |
Dmitry Torokhov | 463a4f7 | 2005-07-15 01:51:56 -0500 | [diff] [blame] | 268 | |
Joe Perches | 4eb3c30 | 2010-11-29 23:33:07 -0800 | [diff] [blame] | 269 | dbg("%02x -> i8042 (command)\n", command & 0xff); |
Dmitry Torokhov | 463a4f7 | 2005-07-15 01:51:56 -0500 | [diff] [blame] | 270 | i8042_write_command(command & 0xff); |
| 271 | |
| 272 | for (i = 0; i < ((command >> 12) & 0xf); i++) { |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 273 | error = i8042_wait_write(); |
| 274 | if (error) |
| 275 | return error; |
Joe Perches | 4eb3c30 | 2010-11-29 23:33:07 -0800 | [diff] [blame] | 276 | dbg("%02x -> i8042 (parameter)\n", param[i]); |
Dmitry Torokhov | 463a4f7 | 2005-07-15 01:51:56 -0500 | [diff] [blame] | 277 | i8042_write_data(param[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | } |
| 279 | |
Dmitry Torokhov | 463a4f7 | 2005-07-15 01:51:56 -0500 | [diff] [blame] | 280 | for (i = 0; i < ((command >> 8) & 0xf); i++) { |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 281 | error = i8042_wait_read(); |
| 282 | if (error) { |
Joe Perches | 4eb3c30 | 2010-11-29 23:33:07 -0800 | [diff] [blame] | 283 | dbg(" -- i8042 (timeout)\n"); |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 284 | return error; |
| 285 | } |
Dmitry Torokhov | 463a4f7 | 2005-07-15 01:51:56 -0500 | [diff] [blame] | 286 | |
| 287 | if (command == I8042_CMD_AUX_LOOP && |
| 288 | !(i8042_read_status() & I8042_STR_AUXDATA)) { |
Joe Perches | 4eb3c30 | 2010-11-29 23:33:07 -0800 | [diff] [blame] | 289 | dbg(" -- i8042 (auxerr)\n"); |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 290 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | } |
| 292 | |
Dmitry Torokhov | 463a4f7 | 2005-07-15 01:51:56 -0500 | [diff] [blame] | 293 | param[i] = i8042_read_data(); |
Joe Perches | 4eb3c30 | 2010-11-29 23:33:07 -0800 | [diff] [blame] | 294 | dbg("%02x <- i8042 (return)\n", param[i]); |
Dmitry Torokhov | 463a4f7 | 2005-07-15 01:51:56 -0500 | [diff] [blame] | 295 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 297 | return 0; |
| 298 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | |
Márton Németh | 553a05b | 2007-10-22 00:56:52 -0400 | [diff] [blame] | 300 | int i8042_command(unsigned char *param, int command) |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 301 | { |
| 302 | unsigned long flags; |
| 303 | int retval; |
| 304 | |
| 305 | spin_lock_irqsave(&i8042_lock, flags); |
| 306 | retval = __i8042_command(param, command); |
Dmitry Torokhov | 463a4f7 | 2005-07-15 01:51:56 -0500 | [diff] [blame] | 307 | spin_unlock_irqrestore(&i8042_lock, flags); |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 308 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | return retval; |
| 310 | } |
Márton Németh | 553a05b | 2007-10-22 00:56:52 -0400 | [diff] [blame] | 311 | EXPORT_SYMBOL(i8042_command); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | |
| 313 | /* |
| 314 | * i8042_kbd_write() sends a byte out through the keyboard interface. |
| 315 | */ |
| 316 | |
| 317 | static int i8042_kbd_write(struct serio *port, unsigned char c) |
| 318 | { |
| 319 | unsigned long flags; |
| 320 | int retval = 0; |
| 321 | |
| 322 | spin_lock_irqsave(&i8042_lock, flags); |
| 323 | |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 324 | if (!(retval = i8042_wait_write())) { |
Joe Perches | 4eb3c30 | 2010-11-29 23:33:07 -0800 | [diff] [blame] | 325 | dbg("%02x -> i8042 (kbd-data)\n", c); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | i8042_write_data(c); |
| 327 | } |
| 328 | |
| 329 | spin_unlock_irqrestore(&i8042_lock, flags); |
| 330 | |
| 331 | return retval; |
| 332 | } |
| 333 | |
| 334 | /* |
| 335 | * i8042_aux_write() sends a byte out through the aux interface. |
| 336 | */ |
| 337 | |
| 338 | static int i8042_aux_write(struct serio *serio, unsigned char c) |
| 339 | { |
| 340 | struct i8042_port *port = serio->port_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | |
Dmitry Torokhov | f4e3c71 | 2006-11-02 23:27:49 -0500 | [diff] [blame] | 342 | return i8042_command(&c, port->mux == -1 ? |
| 343 | I8042_CMD_AUX_SEND : |
| 344 | I8042_CMD_MUX_SEND + port->mux); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | } |
| 346 | |
Dmitry Torokhov | 5ddbc77 | 2009-09-09 19:08:15 -0700 | [diff] [blame] | 347 | |
| 348 | /* |
| 349 | * i8042_aux_close attempts to clear AUX or KBD port state by disabling |
| 350 | * and then re-enabling it. |
| 351 | */ |
| 352 | |
| 353 | static void i8042_port_close(struct serio *serio) |
| 354 | { |
| 355 | int irq_bit; |
| 356 | int disable_bit; |
| 357 | const char *port_name; |
| 358 | |
| 359 | if (serio == i8042_ports[I8042_AUX_PORT_NO].serio) { |
| 360 | irq_bit = I8042_CTR_AUXINT; |
| 361 | disable_bit = I8042_CTR_AUXDIS; |
| 362 | port_name = "AUX"; |
| 363 | } else { |
| 364 | irq_bit = I8042_CTR_KBDINT; |
| 365 | disable_bit = I8042_CTR_KBDDIS; |
| 366 | port_name = "KBD"; |
| 367 | } |
| 368 | |
| 369 | i8042_ctr &= ~irq_bit; |
| 370 | if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) |
Joe Perches | 4eb3c30 | 2010-11-29 23:33:07 -0800 | [diff] [blame] | 371 | pr_warn("Can't write CTR while closing %s port\n", port_name); |
Dmitry Torokhov | 5ddbc77 | 2009-09-09 19:08:15 -0700 | [diff] [blame] | 372 | |
| 373 | udelay(50); |
| 374 | |
| 375 | i8042_ctr &= ~disable_bit; |
| 376 | i8042_ctr |= irq_bit; |
| 377 | if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) |
Joe Perches | 4eb3c30 | 2010-11-29 23:33:07 -0800 | [diff] [blame] | 378 | pr_err("Can't reactivate %s port\n", port_name); |
Dmitry Torokhov | 5ddbc77 | 2009-09-09 19:08:15 -0700 | [diff] [blame] | 379 | |
| 380 | /* |
| 381 | * See if there is any data appeared while we were messing with |
| 382 | * port state. |
| 383 | */ |
| 384 | i8042_interrupt(0, NULL); |
| 385 | } |
| 386 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | * i8042_start() is called by serio core when port is about to finish |
| 389 | * registering. It will mark port as existing so i8042_interrupt can |
| 390 | * start sending data through it. |
| 391 | */ |
| 392 | static int i8042_start(struct serio *serio) |
| 393 | { |
| 394 | struct i8042_port *port = serio->port_data; |
| 395 | |
Dmitry Torokhov | 386b384 | 2009-09-09 19:08:16 -0700 | [diff] [blame] | 396 | port->exists = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | mb(); |
| 398 | return 0; |
| 399 | } |
| 400 | |
| 401 | /* |
| 402 | * i8042_stop() marks serio port as non-existing so i8042_interrupt |
| 403 | * will not try to send data to the port that is about to go away. |
| 404 | * The function is called by serio core as part of unregister procedure. |
| 405 | */ |
| 406 | static void i8042_stop(struct serio *serio) |
| 407 | { |
| 408 | struct i8042_port *port = serio->port_data; |
| 409 | |
Dmitry Torokhov | 386b384 | 2009-09-09 19:08:16 -0700 | [diff] [blame] | 410 | port->exists = false; |
Dmitry Torokhov | a8399c5 | 2007-11-04 00:44:31 -0400 | [diff] [blame] | 411 | |
| 412 | /* |
| 413 | * We synchronize with both AUX and KBD IRQs because there is |
| 414 | * a (very unlikely) chance that AUX IRQ is raised for KBD port |
| 415 | * and vice versa. |
| 416 | */ |
| 417 | synchronize_irq(I8042_AUX_IRQ); |
| 418 | synchronize_irq(I8042_KBD_IRQ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | port->serio = NULL; |
| 420 | } |
| 421 | |
| 422 | /* |
Dmitry Torokhov | 4e8d340 | 2009-12-11 22:00:57 -0800 | [diff] [blame] | 423 | * i8042_filter() filters out unwanted bytes from the input data stream. |
| 424 | * It is called from i8042_interrupt and thus is running with interrupts |
| 425 | * off and i8042_lock held. |
| 426 | */ |
Matthew Garrett | 967c9ef | 2009-12-11 22:00:57 -0800 | [diff] [blame] | 427 | static bool i8042_filter(unsigned char data, unsigned char str, |
| 428 | struct serio *serio) |
Dmitry Torokhov | 4e8d340 | 2009-12-11 22:00:57 -0800 | [diff] [blame] | 429 | { |
| 430 | if (unlikely(i8042_suppress_kbd_ack)) { |
| 431 | if ((~str & I8042_STR_AUXDATA) && |
| 432 | (data == 0xfa || data == 0xfe)) { |
| 433 | i8042_suppress_kbd_ack--; |
| 434 | dbg("Extra keyboard ACK - filtered out\n"); |
| 435 | return true; |
| 436 | } |
| 437 | } |
| 438 | |
Matthew Garrett | 967c9ef | 2009-12-11 22:00:57 -0800 | [diff] [blame] | 439 | if (i8042_platform_filter && i8042_platform_filter(data, str, serio)) { |
Stefan Weil | 0747e3b | 2010-01-07 00:44:08 +0100 | [diff] [blame] | 440 | dbg("Filtered out by platform filter\n"); |
Matthew Garrett | 967c9ef | 2009-12-11 22:00:57 -0800 | [diff] [blame] | 441 | return true; |
| 442 | } |
| 443 | |
Dmitry Torokhov | 4e8d340 | 2009-12-11 22:00:57 -0800 | [diff] [blame] | 444 | return false; |
| 445 | } |
| 446 | |
| 447 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | * i8042_interrupt() is the most important function in this driver - |
| 449 | * it handles the interrupts from the i8042, and sends incoming bytes |
| 450 | * to the upper layers. |
| 451 | */ |
| 452 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 453 | static irqreturn_t i8042_interrupt(int irq, void *dev_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | { |
| 455 | struct i8042_port *port; |
Matthew Garrett | 967c9ef | 2009-12-11 22:00:57 -0800 | [diff] [blame] | 456 | struct serio *serio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | unsigned long flags; |
| 458 | unsigned char str, data; |
| 459 | unsigned int dfl; |
| 460 | unsigned int port_no; |
Dmitry Torokhov | 4e8d340 | 2009-12-11 22:00:57 -0800 | [diff] [blame] | 461 | bool filtered; |
Dmitry Torokhov | 817e6ba | 2006-10-11 01:44:28 -0400 | [diff] [blame] | 462 | int ret = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | spin_lock_irqsave(&i8042_lock, flags); |
Dmitry Torokhov | 4e8d340 | 2009-12-11 22:00:57 -0800 | [diff] [blame] | 465 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | str = i8042_read_status(); |
| 467 | if (unlikely(~str & I8042_STR_OBF)) { |
| 468 | spin_unlock_irqrestore(&i8042_lock, flags); |
Joe Perches | 4eb3c30 | 2010-11-29 23:33:07 -0800 | [diff] [blame] | 469 | if (irq) |
| 470 | dbg("Interrupt %d, without any data\n", irq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | ret = 0; |
| 472 | goto out; |
| 473 | } |
Dmitry Torokhov | 4e8d340 | 2009-12-11 22:00:57 -0800 | [diff] [blame] | 474 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | data = i8042_read_data(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | |
| 477 | if (i8042_mux_present && (str & I8042_STR_AUXDATA)) { |
| 478 | static unsigned long last_transmit; |
| 479 | static unsigned char last_str; |
| 480 | |
| 481 | dfl = 0; |
| 482 | if (str & I8042_STR_MUXERR) { |
Joe Perches | 4eb3c30 | 2010-11-29 23:33:07 -0800 | [diff] [blame] | 483 | dbg("MUX error, status is %02x, data is %02x\n", |
| 484 | str, data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | /* |
| 486 | * When MUXERR condition is signalled the data register can only contain |
| 487 | * 0xfd, 0xfe or 0xff if implementation follows the spec. Unfortunately |
Dmitry Torokhov | a216a4b | 2006-11-17 01:07:06 -0500 | [diff] [blame] | 488 | * it is not always the case. Some KBCs also report 0xfc when there is |
| 489 | * nothing connected to the port while others sometimes get confused which |
| 490 | * port the data came from and signal error leaving the data intact. They |
| 491 | * _do not_ revert to legacy mode (actually I've never seen KBC reverting |
| 492 | * to legacy mode yet, when we see one we'll add proper handling). |
| 493 | * Anyway, we process 0xfc, 0xfd, 0xfe and 0xff as timeouts, and for the |
| 494 | * rest assume that the data came from the same serio last byte |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | * was transmitted (if transmission happened not too long ago). |
| 496 | */ |
Dmitry Torokhov | a216a4b | 2006-11-17 01:07:06 -0500 | [diff] [blame] | 497 | |
| 498 | switch (data) { |
| 499 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | if (time_before(jiffies, last_transmit + HZ/10)) { |
| 501 | str = last_str; |
| 502 | break; |
| 503 | } |
| 504 | /* fall through - report timeout */ |
Dmitry Torokhov | a216a4b | 2006-11-17 01:07:06 -0500 | [diff] [blame] | 505 | case 0xfc: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | case 0xfd: |
| 507 | case 0xfe: dfl = SERIO_TIMEOUT; data = 0xfe; break; |
| 508 | case 0xff: dfl = SERIO_PARITY; data = 0xfe; break; |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | port_no = I8042_MUX_PORT_NO + ((str >> 6) & 3); |
| 513 | last_str = str; |
| 514 | last_transmit = jiffies; |
| 515 | } else { |
| 516 | |
| 517 | dfl = ((str & I8042_STR_PARITY) ? SERIO_PARITY : 0) | |
Jiri Kosina | f8313ef | 2011-01-08 01:37:26 -0800 | [diff] [blame] | 518 | ((str & I8042_STR_TIMEOUT && !i8042_notimeout) ? SERIO_TIMEOUT : 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | |
| 520 | port_no = (str & I8042_STR_AUXDATA) ? |
| 521 | I8042_AUX_PORT_NO : I8042_KBD_PORT_NO; |
| 522 | } |
| 523 | |
| 524 | port = &i8042_ports[port_no]; |
Matthew Garrett | 967c9ef | 2009-12-11 22:00:57 -0800 | [diff] [blame] | 525 | serio = port->exists ? port->serio : NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | |
Joe Perches | 4eb3c30 | 2010-11-29 23:33:07 -0800 | [diff] [blame] | 527 | dbg("%02x <- i8042 (interrupt, %d, %d%s%s)\n", |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 528 | data, port_no, irq, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | dfl & SERIO_PARITY ? ", bad parity" : "", |
| 530 | dfl & SERIO_TIMEOUT ? ", timeout" : ""); |
| 531 | |
Matthew Garrett | 967c9ef | 2009-12-11 22:00:57 -0800 | [diff] [blame] | 532 | filtered = i8042_filter(data, str, serio); |
Dmitry Torokhov | 817e6ba | 2006-10-11 01:44:28 -0400 | [diff] [blame] | 533 | |
Dmitry Torokhov | 4e8d340 | 2009-12-11 22:00:57 -0800 | [diff] [blame] | 534 | spin_unlock_irqrestore(&i8042_lock, flags); |
| 535 | |
| 536 | if (likely(port->exists && !filtered)) |
Matthew Garrett | 967c9ef | 2009-12-11 22:00:57 -0800 | [diff] [blame] | 537 | serio_interrupt(serio, data, dfl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | |
Dmitry Torokhov | 0854e52 | 2005-09-04 01:41:27 -0500 | [diff] [blame] | 539 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | return IRQ_RETVAL(ret); |
| 541 | } |
| 542 | |
| 543 | /* |
Dmitry Torokhov | 5ddbc77 | 2009-09-09 19:08:15 -0700 | [diff] [blame] | 544 | * i8042_enable_kbd_port enables keyboard port on chip |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 545 | */ |
| 546 | |
| 547 | static int i8042_enable_kbd_port(void) |
| 548 | { |
| 549 | i8042_ctr &= ~I8042_CTR_KBDDIS; |
| 550 | i8042_ctr |= I8042_CTR_KBDINT; |
| 551 | |
| 552 | if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) { |
Markus Armbruster | 018db6b | 2007-07-18 01:20:41 -0400 | [diff] [blame] | 553 | i8042_ctr &= ~I8042_CTR_KBDINT; |
| 554 | i8042_ctr |= I8042_CTR_KBDDIS; |
Joe Perches | 4eb3c30 | 2010-11-29 23:33:07 -0800 | [diff] [blame] | 555 | pr_err("Failed to enable KBD port\n"); |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 556 | return -EIO; |
| 557 | } |
| 558 | |
| 559 | return 0; |
| 560 | } |
| 561 | |
| 562 | /* |
| 563 | * i8042_enable_aux_port enables AUX (mouse) port on chip |
| 564 | */ |
| 565 | |
| 566 | static int i8042_enable_aux_port(void) |
| 567 | { |
| 568 | i8042_ctr &= ~I8042_CTR_AUXDIS; |
| 569 | i8042_ctr |= I8042_CTR_AUXINT; |
| 570 | |
| 571 | if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) { |
Markus Armbruster | 018db6b | 2007-07-18 01:20:41 -0400 | [diff] [blame] | 572 | i8042_ctr &= ~I8042_CTR_AUXINT; |
| 573 | i8042_ctr |= I8042_CTR_AUXDIS; |
Joe Perches | 4eb3c30 | 2010-11-29 23:33:07 -0800 | [diff] [blame] | 574 | pr_err("Failed to enable AUX port\n"); |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 575 | return -EIO; |
| 576 | } |
| 577 | |
| 578 | return 0; |
| 579 | } |
| 580 | |
| 581 | /* |
| 582 | * i8042_enable_mux_ports enables 4 individual AUX ports after |
| 583 | * the controller has been switched into Multiplexed mode |
| 584 | */ |
| 585 | |
| 586 | static int i8042_enable_mux_ports(void) |
| 587 | { |
| 588 | unsigned char param; |
| 589 | int i; |
| 590 | |
| 591 | for (i = 0; i < I8042_NUM_MUX_PORTS; i++) { |
| 592 | i8042_command(¶m, I8042_CMD_MUX_PFX + i); |
| 593 | i8042_command(¶m, I8042_CMD_AUX_ENABLE); |
| 594 | } |
| 595 | |
| 596 | return i8042_enable_aux_port(); |
| 597 | } |
| 598 | |
| 599 | /* |
Dmitry Torokhov | 386b384 | 2009-09-09 19:08:16 -0700 | [diff] [blame] | 600 | * i8042_set_mux_mode checks whether the controller has an |
| 601 | * active multiplexor and puts the chip into Multiplexed (true) |
| 602 | * or Legacy (false) mode. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | */ |
| 604 | |
Dmitry Torokhov | 386b384 | 2009-09-09 19:08:16 -0700 | [diff] [blame] | 605 | static int i8042_set_mux_mode(bool multiplex, unsigned char *mux_version) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | { |
| 607 | |
Dmitry Torokhov | 386b384 | 2009-09-09 19:08:16 -0700 | [diff] [blame] | 608 | unsigned char param, val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | /* |
| 610 | * Get rid of bytes in the queue. |
| 611 | */ |
| 612 | |
| 613 | i8042_flush(); |
| 614 | |
| 615 | /* |
| 616 | * Internal loopback test - send three bytes, they should come back from the |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 617 | * mouse interface, the last should be version. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | */ |
| 619 | |
Dmitry Torokhov | 386b384 | 2009-09-09 19:08:16 -0700 | [diff] [blame] | 620 | param = val = 0xf0; |
| 621 | if (i8042_command(¶m, I8042_CMD_AUX_LOOP) || param != val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | return -1; |
Dmitry Torokhov | 386b384 | 2009-09-09 19:08:16 -0700 | [diff] [blame] | 623 | param = val = multiplex ? 0x56 : 0xf6; |
| 624 | if (i8042_command(¶m, I8042_CMD_AUX_LOOP) || param != val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | return -1; |
Dmitry Torokhov | 386b384 | 2009-09-09 19:08:16 -0700 | [diff] [blame] | 626 | param = val = multiplex ? 0xa4 : 0xa5; |
| 627 | if (i8042_command(¶m, I8042_CMD_AUX_LOOP) || param == val) |
| 628 | return -1; |
| 629 | |
| 630 | /* |
| 631 | * Workaround for interference with USB Legacy emulation |
| 632 | * that causes a v10.12 MUX to be found. |
| 633 | */ |
| 634 | if (param == 0xac) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | return -1; |
| 636 | |
| 637 | if (mux_version) |
Dmitry Torokhov | 463a4f7 | 2005-07-15 01:51:56 -0500 | [diff] [blame] | 638 | *mux_version = param; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | |
| 640 | return 0; |
| 641 | } |
| 642 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | /* |
| 644 | * i8042_check_mux() checks whether the controller supports the PS/2 Active |
| 645 | * Multiplexing specification by Synaptics, Phoenix, Insyde and |
| 646 | * LCS/Telegraphics. |
| 647 | */ |
| 648 | |
Dmitry Torokhov | f811341 | 2009-09-09 19:08:17 -0700 | [diff] [blame] | 649 | static int __init i8042_check_mux(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | { |
| 651 | unsigned char mux_version; |
| 652 | |
Dmitry Torokhov | 386b384 | 2009-09-09 19:08:16 -0700 | [diff] [blame] | 653 | if (i8042_set_mux_mode(true, &mux_version)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | return -1; |
| 655 | |
Joe Perches | 4eb3c30 | 2010-11-29 23:33:07 -0800 | [diff] [blame] | 656 | pr_info("Detected active multiplexing controller, rev %d.%d\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | (mux_version >> 4) & 0xf, mux_version & 0xf); |
| 658 | |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 659 | /* |
| 660 | * Disable all muxed ports by disabling AUX. |
| 661 | */ |
| 662 | i8042_ctr |= I8042_CTR_AUXDIS; |
| 663 | i8042_ctr &= ~I8042_CTR_AUXINT; |
| 664 | |
| 665 | if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) { |
Joe Perches | 4eb3c30 | 2010-11-29 23:33:07 -0800 | [diff] [blame] | 666 | pr_err("Failed to disable AUX port, can't use MUX\n"); |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 667 | return -EIO; |
| 668 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | |
Dmitry Torokhov | 386b384 | 2009-09-09 19:08:16 -0700 | [diff] [blame] | 670 | i8042_mux_present = true; |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 671 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | return 0; |
| 673 | } |
| 674 | |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 675 | /* |
| 676 | * The following is used to test AUX IRQ delivery. |
| 677 | */ |
Dmitry Torokhov | f811341 | 2009-09-09 19:08:17 -0700 | [diff] [blame] | 678 | static struct completion i8042_aux_irq_delivered __initdata; |
| 679 | static bool i8042_irq_being_tested __initdata; |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 680 | |
Dmitry Torokhov | f811341 | 2009-09-09 19:08:17 -0700 | [diff] [blame] | 681 | static irqreturn_t __init i8042_aux_test_irq(int irq, void *dev_id) |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 682 | { |
| 683 | unsigned long flags; |
| 684 | unsigned char str, data; |
Fernando Luis Vázquez Cao | e3758b2 | 2007-08-30 00:04:15 -0400 | [diff] [blame] | 685 | int ret = 0; |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 686 | |
| 687 | spin_lock_irqsave(&i8042_lock, flags); |
| 688 | str = i8042_read_status(); |
| 689 | if (str & I8042_STR_OBF) { |
| 690 | data = i8042_read_data(); |
Joe Perches | 4eb3c30 | 2010-11-29 23:33:07 -0800 | [diff] [blame] | 691 | dbg("%02x <- i8042 (aux_test_irq, %s)\n", |
| 692 | data, str & I8042_STR_AUXDATA ? "aux" : "kbd"); |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 693 | if (i8042_irq_being_tested && |
| 694 | data == 0xa5 && (str & I8042_STR_AUXDATA)) |
| 695 | complete(&i8042_aux_irq_delivered); |
Fernando Luis Vázquez Cao | e3758b2 | 2007-08-30 00:04:15 -0400 | [diff] [blame] | 696 | ret = 1; |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 697 | } |
| 698 | spin_unlock_irqrestore(&i8042_lock, flags); |
| 699 | |
Fernando Luis Vázquez Cao | e3758b2 | 2007-08-30 00:04:15 -0400 | [diff] [blame] | 700 | return IRQ_RETVAL(ret); |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 701 | } |
| 702 | |
Roland Scheidegger | d2ada55 | 2007-05-08 01:31:40 -0400 | [diff] [blame] | 703 | /* |
| 704 | * i8042_toggle_aux - enables or disables AUX port on i8042 via command and |
| 705 | * verifies success by readinng CTR. Used when testing for presence of AUX |
| 706 | * port. |
| 707 | */ |
Dmitry Torokhov | f811341 | 2009-09-09 19:08:17 -0700 | [diff] [blame] | 708 | static int __init i8042_toggle_aux(bool on) |
Roland Scheidegger | d2ada55 | 2007-05-08 01:31:40 -0400 | [diff] [blame] | 709 | { |
| 710 | unsigned char param; |
| 711 | int i; |
| 712 | |
| 713 | if (i8042_command(¶m, |
| 714 | on ? I8042_CMD_AUX_ENABLE : I8042_CMD_AUX_DISABLE)) |
| 715 | return -1; |
| 716 | |
| 717 | /* some chips need some time to set the I8042_CTR_AUXDIS bit */ |
| 718 | for (i = 0; i < 100; i++) { |
| 719 | udelay(50); |
| 720 | |
| 721 | if (i8042_command(¶m, I8042_CMD_CTL_RCTR)) |
| 722 | return -1; |
| 723 | |
| 724 | if (!(param & I8042_CTR_AUXDIS) == on) |
| 725 | return 0; |
| 726 | } |
| 727 | |
| 728 | return -1; |
| 729 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | |
| 731 | /* |
| 732 | * i8042_check_aux() applies as much paranoia as it can at detecting |
| 733 | * the presence of an AUX interface. |
| 734 | */ |
| 735 | |
Dmitry Torokhov | f811341 | 2009-09-09 19:08:17 -0700 | [diff] [blame] | 736 | static int __init i8042_check_aux(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | { |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 738 | int retval = -1; |
Dmitry Torokhov | 386b384 | 2009-09-09 19:08:16 -0700 | [diff] [blame] | 739 | bool irq_registered = false; |
| 740 | bool aux_loop_broken = false; |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 741 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | unsigned char param; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | |
| 744 | /* |
| 745 | * Get rid of bytes in the queue. |
| 746 | */ |
| 747 | |
| 748 | i8042_flush(); |
| 749 | |
| 750 | /* |
| 751 | * Internal loopback test - filters out AT-type i8042's. Unfortunately |
| 752 | * SiS screwed up and their 5597 doesn't support the LOOP command even |
| 753 | * though it has an AUX port. |
| 754 | */ |
| 755 | |
| 756 | param = 0x5a; |
Dmitry Torokhov | 3ca5de6 | 2007-03-07 23:20:55 -0500 | [diff] [blame] | 757 | retval = i8042_command(¶m, I8042_CMD_AUX_LOOP); |
| 758 | if (retval || param != 0x5a) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | |
| 760 | /* |
| 761 | * External connection test - filters out AT-soldered PS/2 i8042's |
| 762 | * 0x00 - no error, 0x01-0x03 - clock/data stuck, 0xff - general error |
| 763 | * 0xfa - no error on some notebooks which ignore the spec |
| 764 | * Because it's common for chipsets to return error on perfectly functioning |
| 765 | * AUX ports, we test for this only when the LOOP command failed. |
| 766 | */ |
| 767 | |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 768 | if (i8042_command(¶m, I8042_CMD_AUX_TEST) || |
| 769 | (param && param != 0xfa && param != 0xff)) |
| 770 | return -1; |
Dmitry Torokhov | 1e4865f | 2007-02-10 01:29:53 -0500 | [diff] [blame] | 771 | |
Dmitry Torokhov | 3ca5de6 | 2007-03-07 23:20:55 -0500 | [diff] [blame] | 772 | /* |
| 773 | * If AUX_LOOP completed without error but returned unexpected data |
| 774 | * mark it as broken |
| 775 | */ |
| 776 | if (!retval) |
Dmitry Torokhov | 386b384 | 2009-09-09 19:08:16 -0700 | [diff] [blame] | 777 | aux_loop_broken = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | } |
| 779 | |
| 780 | /* |
| 781 | * Bit assignment test - filters out PS/2 i8042's in AT mode |
| 782 | */ |
| 783 | |
Dmitry Torokhov | 386b384 | 2009-09-09 19:08:16 -0700 | [diff] [blame] | 784 | if (i8042_toggle_aux(false)) { |
Joe Perches | 4eb3c30 | 2010-11-29 23:33:07 -0800 | [diff] [blame] | 785 | pr_warn("Failed to disable AUX port, but continuing anyway... Is this a SiS?\n"); |
| 786 | pr_warn("If AUX port is really absent please use the 'i8042.noaux' option\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | } |
| 788 | |
Dmitry Torokhov | 386b384 | 2009-09-09 19:08:16 -0700 | [diff] [blame] | 789 | if (i8042_toggle_aux(true)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | return -1; |
| 791 | |
| 792 | /* |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 793 | * Test AUX IRQ delivery to make sure BIOS did not grab the IRQ and |
| 794 | * used it for a PCI card or somethig else. |
| 795 | */ |
| 796 | |
Dmitry Torokhov | 1c7827a | 2009-09-03 21:45:34 -0700 | [diff] [blame] | 797 | if (i8042_noloop || i8042_bypass_aux_irq_test || aux_loop_broken) { |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 798 | /* |
| 799 | * Without LOOP command we can't test AUX IRQ delivery. Assume the port |
| 800 | * is working and hope we are right. |
| 801 | */ |
| 802 | retval = 0; |
| 803 | goto out; |
| 804 | } |
| 805 | |
| 806 | if (request_irq(I8042_AUX_IRQ, i8042_aux_test_irq, IRQF_SHARED, |
| 807 | "i8042", i8042_platform_device)) |
| 808 | goto out; |
| 809 | |
Dmitry Torokhov | 386b384 | 2009-09-09 19:08:16 -0700 | [diff] [blame] | 810 | irq_registered = true; |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 811 | |
| 812 | if (i8042_enable_aux_port()) |
| 813 | goto out; |
| 814 | |
| 815 | spin_lock_irqsave(&i8042_lock, flags); |
| 816 | |
| 817 | init_completion(&i8042_aux_irq_delivered); |
Dmitry Torokhov | 386b384 | 2009-09-09 19:08:16 -0700 | [diff] [blame] | 818 | i8042_irq_being_tested = true; |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 819 | |
| 820 | param = 0xa5; |
| 821 | retval = __i8042_command(¶m, I8042_CMD_AUX_LOOP & 0xf0ff); |
| 822 | |
| 823 | spin_unlock_irqrestore(&i8042_lock, flags); |
| 824 | |
| 825 | if (retval) |
| 826 | goto out; |
| 827 | |
| 828 | if (wait_for_completion_timeout(&i8042_aux_irq_delivered, |
| 829 | msecs_to_jiffies(250)) == 0) { |
| 830 | /* |
| 831 | * AUX IRQ was never delivered so we need to flush the controller to |
| 832 | * get rid of the byte we put there; otherwise keyboard may not work. |
| 833 | */ |
Joe Perches | 4eb3c30 | 2010-11-29 23:33:07 -0800 | [diff] [blame] | 834 | dbg(" -- i8042 (aux irq test timeout)\n"); |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 835 | i8042_flush(); |
| 836 | retval = -1; |
| 837 | } |
| 838 | |
| 839 | out: |
| 840 | |
| 841 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 842 | * Disable the interface. |
| 843 | */ |
| 844 | |
| 845 | i8042_ctr |= I8042_CTR_AUXDIS; |
| 846 | i8042_ctr &= ~I8042_CTR_AUXINT; |
| 847 | |
| 848 | if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 849 | retval = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 850 | |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 851 | if (irq_registered) |
| 852 | free_irq(I8042_AUX_IRQ, i8042_platform_device); |
| 853 | |
| 854 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 | } |
| 856 | |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 857 | static int i8042_controller_check(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | { |
Andrey Moiseev | 2f0d260 | 2013-09-16 15:17:31 -0700 | [diff] [blame] | 859 | if (i8042_flush()) { |
Joe Perches | 4eb3c30 | 2010-11-29 23:33:07 -0800 | [diff] [blame] | 860 | pr_err("No controller found\n"); |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 861 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | } |
| 863 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 864 | return 0; |
| 865 | } |
| 866 | |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 867 | static int i8042_controller_selftest(void) |
Vojtech Pavlik | 2673c836 | 2005-05-28 02:11:27 -0500 | [diff] [blame] | 868 | { |
| 869 | unsigned char param; |
Arjan van de Ven | 5ea2fc6 | 2009-04-09 11:36:50 -0700 | [diff] [blame] | 870 | int i = 0; |
Vojtech Pavlik | 2673c836 | 2005-05-28 02:11:27 -0500 | [diff] [blame] | 871 | |
Arjan van de Ven | 5ea2fc6 | 2009-04-09 11:36:50 -0700 | [diff] [blame] | 872 | /* |
| 873 | * We try this 5 times; on some really fragile systems this does not |
| 874 | * take the first time... |
| 875 | */ |
| 876 | do { |
Vojtech Pavlik | 2673c836 | 2005-05-28 02:11:27 -0500 | [diff] [blame] | 877 | |
Arjan van de Ven | 5ea2fc6 | 2009-04-09 11:36:50 -0700 | [diff] [blame] | 878 | if (i8042_command(¶m, I8042_CMD_CTL_TEST)) { |
Paul Bolle | a2a94e7 | 2011-03-31 00:11:48 -0700 | [diff] [blame] | 879 | pr_err("i8042 controller selftest timeout\n"); |
Arjan van de Ven | 5ea2fc6 | 2009-04-09 11:36:50 -0700 | [diff] [blame] | 880 | return -ENODEV; |
| 881 | } |
| 882 | |
| 883 | if (param == I8042_RET_CTL_TEST) |
| 884 | return 0; |
| 885 | |
Paul Bolle | a2a94e7 | 2011-03-31 00:11:48 -0700 | [diff] [blame] | 886 | dbg("i8042 controller selftest: %#x != %#x\n", |
| 887 | param, I8042_RET_CTL_TEST); |
Arjan van de Ven | 5ea2fc6 | 2009-04-09 11:36:50 -0700 | [diff] [blame] | 888 | msleep(50); |
| 889 | } while (i++ < 5); |
Vojtech Pavlik | 2673c836 | 2005-05-28 02:11:27 -0500 | [diff] [blame] | 890 | |
Arjan van de Ven | 5ea2fc6 | 2009-04-09 11:36:50 -0700 | [diff] [blame] | 891 | #ifdef CONFIG_X86 |
| 892 | /* |
| 893 | * On x86, we don't fail entire i8042 initialization if controller |
| 894 | * reset fails in hopes that keyboard port will still be functional |
| 895 | * and user will still get a working keyboard. This is especially |
| 896 | * important on netbooks. On other arches we trust hardware more. |
| 897 | */ |
Joe Perches | 4eb3c30 | 2010-11-29 23:33:07 -0800 | [diff] [blame] | 898 | pr_info("giving up on controller selftest, continuing anyway...\n"); |
Vojtech Pavlik | 2673c836 | 2005-05-28 02:11:27 -0500 | [diff] [blame] | 899 | return 0; |
Arjan van de Ven | 5ea2fc6 | 2009-04-09 11:36:50 -0700 | [diff] [blame] | 900 | #else |
Paul Bolle | a2a94e7 | 2011-03-31 00:11:48 -0700 | [diff] [blame] | 901 | pr_err("i8042 controller selftest failed\n"); |
Arjan van de Ven | 5ea2fc6 | 2009-04-09 11:36:50 -0700 | [diff] [blame] | 902 | return -EIO; |
| 903 | #endif |
Vojtech Pavlik | 2673c836 | 2005-05-28 02:11:27 -0500 | [diff] [blame] | 904 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | |
| 906 | /* |
| 907 | * i8042_controller init initializes the i8042 controller, and, |
| 908 | * most importantly, sets it into non-xlated mode if that's |
| 909 | * desired. |
| 910 | */ |
| 911 | |
| 912 | static int i8042_controller_init(void) |
| 913 | { |
| 914 | unsigned long flags; |
Dmitry Torokhov | ee1e82c | 2009-11-02 21:57:40 -0800 | [diff] [blame] | 915 | int n = 0; |
| 916 | unsigned char ctr[2]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | |
| 918 | /* |
Dmitry Torokhov | ee1e82c | 2009-11-02 21:57:40 -0800 | [diff] [blame] | 919 | * Save the CTR for restore on unload / reboot. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 920 | */ |
| 921 | |
Dmitry Torokhov | ee1e82c | 2009-11-02 21:57:40 -0800 | [diff] [blame] | 922 | do { |
| 923 | if (n >= 10) { |
Joe Perches | 4eb3c30 | 2010-11-29 23:33:07 -0800 | [diff] [blame] | 924 | pr_err("Unable to get stable CTR read\n"); |
Dmitry Torokhov | ee1e82c | 2009-11-02 21:57:40 -0800 | [diff] [blame] | 925 | return -EIO; |
| 926 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 927 | |
Dmitry Torokhov | ee1e82c | 2009-11-02 21:57:40 -0800 | [diff] [blame] | 928 | if (n != 0) |
| 929 | udelay(50); |
| 930 | |
| 931 | if (i8042_command(&ctr[n++ % 2], I8042_CMD_CTL_RCTR)) { |
Joe Perches | 4eb3c30 | 2010-11-29 23:33:07 -0800 | [diff] [blame] | 932 | pr_err("Can't read CTR while initializing i8042\n"); |
Dmitry Torokhov | ee1e82c | 2009-11-02 21:57:40 -0800 | [diff] [blame] | 933 | return -EIO; |
| 934 | } |
| 935 | |
| 936 | } while (n < 2 || ctr[0] != ctr[1]); |
| 937 | |
| 938 | i8042_initial_ctr = i8042_ctr = ctr[0]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | |
| 940 | /* |
| 941 | * Disable the keyboard interface and interrupt. |
| 942 | */ |
| 943 | |
| 944 | i8042_ctr |= I8042_CTR_KBDDIS; |
| 945 | i8042_ctr &= ~I8042_CTR_KBDINT; |
| 946 | |
| 947 | /* |
| 948 | * Handle keylock. |
| 949 | */ |
| 950 | |
| 951 | spin_lock_irqsave(&i8042_lock, flags); |
| 952 | if (~i8042_read_status() & I8042_STR_KEYLOCK) { |
| 953 | if (i8042_unlock) |
| 954 | i8042_ctr |= I8042_CTR_IGNKEYLOCK; |
Dmitry Torokhov | 82dd9ef | 2007-02-18 01:40:30 -0500 | [diff] [blame] | 955 | else |
Joe Perches | 4eb3c30 | 2010-11-29 23:33:07 -0800 | [diff] [blame] | 956 | pr_warn("Warning: Keylock active\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | } |
| 958 | spin_unlock_irqrestore(&i8042_lock, flags); |
| 959 | |
| 960 | /* |
| 961 | * If the chip is configured into nontranslated mode by the BIOS, don't |
| 962 | * bother enabling translating and be happy. |
| 963 | */ |
| 964 | |
| 965 | if (~i8042_ctr & I8042_CTR_XLATE) |
Dmitry Torokhov | 386b384 | 2009-09-09 19:08:16 -0700 | [diff] [blame] | 966 | i8042_direct = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | |
| 968 | /* |
| 969 | * Set nontranslated mode for the kbd interface if requested by an option. |
| 970 | * After this the kbd interface becomes a simple serial in/out, like the aux |
| 971 | * interface is. We don't do this by default, since it can confuse notebook |
| 972 | * BIOSes. |
| 973 | */ |
| 974 | |
| 975 | if (i8042_direct) |
| 976 | i8042_ctr &= ~I8042_CTR_XLATE; |
| 977 | |
| 978 | /* |
| 979 | * Write CTR back. |
| 980 | */ |
| 981 | |
| 982 | if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) { |
Joe Perches | 4eb3c30 | 2010-11-29 23:33:07 -0800 | [diff] [blame] | 983 | pr_err("Can't write CTR while initializing i8042\n"); |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 984 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | } |
| 986 | |
Dmitry Torokhov | ee1e82c | 2009-11-02 21:57:40 -0800 | [diff] [blame] | 987 | /* |
| 988 | * Flush whatever accumulated while we were disabling keyboard port. |
| 989 | */ |
| 990 | |
| 991 | i8042_flush(); |
| 992 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | return 0; |
| 994 | } |
| 995 | |
| 996 | |
| 997 | /* |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 998 | * Reset the controller and reset CRT to the original value set by BIOS. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 999 | */ |
| 1000 | |
Dmitry Torokhov | 1729ad1 | 2011-10-29 12:37:06 -0700 | [diff] [blame] | 1001 | static void i8042_controller_reset(bool force_reset) |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 1002 | { |
| 1003 | i8042_flush(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1004 | |
| 1005 | /* |
Dmitry Torokhov | 8d04ddb | 2007-04-12 01:32:09 -0400 | [diff] [blame] | 1006 | * Disable both KBD and AUX interfaces so they don't get in the way |
| 1007 | */ |
| 1008 | |
| 1009 | i8042_ctr |= I8042_CTR_KBDDIS | I8042_CTR_AUXDIS; |
| 1010 | i8042_ctr &= ~(I8042_CTR_KBDINT | I8042_CTR_AUXINT); |
| 1011 | |
Dmitry Torokhov | ee1e82c | 2009-11-02 21:57:40 -0800 | [diff] [blame] | 1012 | if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) |
Joe Perches | 4eb3c30 | 2010-11-29 23:33:07 -0800 | [diff] [blame] | 1013 | pr_warn("Can't write CTR while resetting\n"); |
Dmitry Torokhov | 5ddbc77 | 2009-09-09 19:08:15 -0700 | [diff] [blame] | 1014 | |
Dmitry Torokhov | 8d04ddb | 2007-04-12 01:32:09 -0400 | [diff] [blame] | 1015 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1016 | * Disable MUX mode if present. |
| 1017 | */ |
| 1018 | |
| 1019 | if (i8042_mux_present) |
Dmitry Torokhov | 386b384 | 2009-09-09 19:08:16 -0700 | [diff] [blame] | 1020 | i8042_set_mux_mode(false, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1021 | |
| 1022 | /* |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 1023 | * Reset the controller if requested. |
| 1024 | */ |
| 1025 | |
Dmitry Torokhov | 1729ad1 | 2011-10-29 12:37:06 -0700 | [diff] [blame] | 1026 | if (i8042_reset || force_reset) |
Dmitry Torokhov | 1ca56e5 | 2010-07-20 20:25:34 -0700 | [diff] [blame] | 1027 | i8042_controller_selftest(); |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 1028 | |
| 1029 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1030 | * Restore the original control register setting. |
| 1031 | */ |
| 1032 | |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 1033 | if (i8042_command(&i8042_initial_ctr, I8042_CMD_CTL_WCTR)) |
Joe Perches | 4eb3c30 | 2010-11-29 23:33:07 -0800 | [diff] [blame] | 1034 | pr_warn("Can't restore CTR\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1035 | } |
| 1036 | |
| 1037 | |
| 1038 | /* |
TAMUKI Shoichi | c7ff0d9 | 2010-08-10 18:03:28 -0700 | [diff] [blame] | 1039 | * i8042_panic_blink() will turn the keyboard LEDs on or off and is called |
| 1040 | * when kernel panics. Flashing LEDs is useful for users running X who may |
Michael Opdenacker | aa5e5dc | 2013-09-18 06:00:43 +0200 | [diff] [blame] | 1041 | * not see the console and will help distinguishing panics from "real" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1042 | * lockups. |
| 1043 | * |
| 1044 | * Note that DELAY has a limit of 10ms so we will not get stuck here |
| 1045 | * waiting for KBC to free up even if KBD interrupt is off |
| 1046 | */ |
| 1047 | |
| 1048 | #define DELAY do { mdelay(1); if (++delay > 10) return delay; } while(0) |
| 1049 | |
TAMUKI Shoichi | c7ff0d9 | 2010-08-10 18:03:28 -0700 | [diff] [blame] | 1050 | static long i8042_panic_blink(int state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1051 | { |
| 1052 | long delay = 0; |
TAMUKI Shoichi | c7ff0d9 | 2010-08-10 18:03:28 -0700 | [diff] [blame] | 1053 | char led; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1054 | |
TAMUKI Shoichi | c7ff0d9 | 2010-08-10 18:03:28 -0700 | [diff] [blame] | 1055 | led = (state) ? 0x01 | 0x04 : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1056 | while (i8042_read_status() & I8042_STR_IBF) |
| 1057 | DELAY; |
Joe Perches | 4eb3c30 | 2010-11-29 23:33:07 -0800 | [diff] [blame] | 1058 | dbg("%02x -> i8042 (panic blink)\n", 0xed); |
Dmitry Torokhov | 19f3c3e | 2007-01-18 00:42:31 -0500 | [diff] [blame] | 1059 | i8042_suppress_kbd_ack = 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1060 | i8042_write_data(0xed); /* set leds */ |
| 1061 | DELAY; |
| 1062 | while (i8042_read_status() & I8042_STR_IBF) |
| 1063 | DELAY; |
| 1064 | DELAY; |
Joe Perches | 4eb3c30 | 2010-11-29 23:33:07 -0800 | [diff] [blame] | 1065 | dbg("%02x -> i8042 (panic blink)\n", led); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1066 | i8042_write_data(led); |
| 1067 | DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1068 | return delay; |
| 1069 | } |
| 1070 | |
| 1071 | #undef DELAY |
| 1072 | |
Bruno Prémont | d35895d | 2008-05-27 01:36:04 -0400 | [diff] [blame] | 1073 | #ifdef CONFIG_X86 |
| 1074 | static void i8042_dritek_enable(void) |
| 1075 | { |
Christoph Fritz | 594d636 | 2010-09-29 18:04:21 -0700 | [diff] [blame] | 1076 | unsigned char param = 0x90; |
Bruno Prémont | d35895d | 2008-05-27 01:36:04 -0400 | [diff] [blame] | 1077 | int error; |
| 1078 | |
| 1079 | error = i8042_command(¶m, 0x1059); |
| 1080 | if (error) |
Joe Perches | 4eb3c30 | 2010-11-29 23:33:07 -0800 | [diff] [blame] | 1081 | pr_warn("Failed to enable DRITEK extension: %d\n", error); |
Bruno Prémont | d35895d | 2008-05-27 01:36:04 -0400 | [diff] [blame] | 1082 | } |
| 1083 | #endif |
| 1084 | |
Dmitry Torokhov | 82dd9ef | 2007-02-18 01:40:30 -0500 | [diff] [blame] | 1085 | #ifdef CONFIG_PM |
Dmitry Torokhov | 7e044e0 | 2009-05-09 16:08:05 -0700 | [diff] [blame] | 1086 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1087 | /* |
Dmitry Torokhov | ebd7768 | 2009-07-22 21:51:32 -0700 | [diff] [blame] | 1088 | * Here we try to reset everything back to a state we had |
| 1089 | * before suspending. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1090 | */ |
| 1091 | |
Dmitry Torokhov | 1ca56e5 | 2010-07-20 20:25:34 -0700 | [diff] [blame] | 1092 | static int i8042_controller_resume(bool force_reset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1093 | { |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 1094 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1095 | |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 1096 | error = i8042_controller_check(); |
| 1097 | if (error) |
| 1098 | return error; |
Vojtech Pavlik | 2673c836 | 2005-05-28 02:11:27 -0500 | [diff] [blame] | 1099 | |
Dmitry Torokhov | 1ca56e5 | 2010-07-20 20:25:34 -0700 | [diff] [blame] | 1100 | if (i8042_reset || force_reset) { |
| 1101 | error = i8042_controller_selftest(); |
| 1102 | if (error) |
| 1103 | return error; |
| 1104 | } |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 1105 | |
| 1106 | /* |
Dmitry Torokhov | 82dd9ef | 2007-02-18 01:40:30 -0500 | [diff] [blame] | 1107 | * Restore original CTR value and disable all ports |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 1108 | */ |
| 1109 | |
Dmitry Torokhov | 82dd9ef | 2007-02-18 01:40:30 -0500 | [diff] [blame] | 1110 | i8042_ctr = i8042_initial_ctr; |
| 1111 | if (i8042_direct) |
| 1112 | i8042_ctr &= ~I8042_CTR_XLATE; |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 1113 | i8042_ctr |= I8042_CTR_AUXDIS | I8042_CTR_KBDDIS; |
| 1114 | i8042_ctr &= ~(I8042_CTR_AUXINT | I8042_CTR_KBDINT); |
Vojtech Pavlik | 2673c836 | 2005-05-28 02:11:27 -0500 | [diff] [blame] | 1115 | if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) { |
Joe Perches | 4eb3c30 | 2010-11-29 23:33:07 -0800 | [diff] [blame] | 1116 | pr_warn("Can't write CTR to resume, retrying...\n"); |
Jiri Kosina | 2f6a77d | 2008-06-17 11:47:27 -0400 | [diff] [blame] | 1117 | msleep(50); |
| 1118 | if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) { |
Joe Perches | 4eb3c30 | 2010-11-29 23:33:07 -0800 | [diff] [blame] | 1119 | pr_err("CTR write retry failed\n"); |
Jiri Kosina | 2f6a77d | 2008-06-17 11:47:27 -0400 | [diff] [blame] | 1120 | return -EIO; |
| 1121 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1122 | } |
| 1123 | |
Bruno Prémont | d35895d | 2008-05-27 01:36:04 -0400 | [diff] [blame] | 1124 | |
| 1125 | #ifdef CONFIG_X86 |
| 1126 | if (i8042_dritek) |
| 1127 | i8042_dritek_enable(); |
| 1128 | #endif |
| 1129 | |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 1130 | if (i8042_mux_present) { |
Dmitry Torokhov | 386b384 | 2009-09-09 19:08:16 -0700 | [diff] [blame] | 1131 | if (i8042_set_mux_mode(true, NULL) || i8042_enable_mux_ports()) |
Joe Perches | 4eb3c30 | 2010-11-29 23:33:07 -0800 | [diff] [blame] | 1132 | pr_warn("failed to resume active multiplexor, mouse won't work\n"); |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 1133 | } else if (i8042_ports[I8042_AUX_PORT_NO].serio) |
| 1134 | i8042_enable_aux_port(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1135 | |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 1136 | if (i8042_ports[I8042_KBD_PORT_NO].serio) |
| 1137 | i8042_enable_kbd_port(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1138 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 1139 | i8042_interrupt(0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1140 | |
| 1141 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1142 | } |
Dmitry Torokhov | ebd7768 | 2009-07-22 21:51:32 -0700 | [diff] [blame] | 1143 | |
Dmitry Torokhov | 1ca56e5 | 2010-07-20 20:25:34 -0700 | [diff] [blame] | 1144 | /* |
| 1145 | * Here we try to restore the original BIOS settings to avoid |
| 1146 | * upsetting it. |
| 1147 | */ |
| 1148 | |
Dmitry Torokhov | 1729ad1 | 2011-10-29 12:37:06 -0700 | [diff] [blame] | 1149 | static int i8042_pm_suspend(struct device *dev) |
Dmitry Torokhov | 1ca56e5 | 2010-07-20 20:25:34 -0700 | [diff] [blame] | 1150 | { |
Dmitry Torokhov | 1729ad1 | 2011-10-29 12:37:06 -0700 | [diff] [blame] | 1151 | i8042_controller_reset(true); |
Dmitry Torokhov | 1ca56e5 | 2010-07-20 20:25:34 -0700 | [diff] [blame] | 1152 | |
| 1153 | return 0; |
| 1154 | } |
| 1155 | |
| 1156 | static int i8042_pm_resume(struct device *dev) |
| 1157 | { |
| 1158 | /* |
| 1159 | * On resume from S2R we always try to reset the controller |
| 1160 | * to bring it in a sane state. (In case of S2D we expect |
| 1161 | * BIOS to reset the controller for us.) |
| 1162 | */ |
| 1163 | return i8042_controller_resume(true); |
| 1164 | } |
| 1165 | |
Alan Jenkins | c2d1a2a | 2010-02-17 12:17:33 -0800 | [diff] [blame] | 1166 | static int i8042_pm_thaw(struct device *dev) |
| 1167 | { |
| 1168 | i8042_interrupt(0, NULL); |
| 1169 | |
| 1170 | return 0; |
| 1171 | } |
| 1172 | |
Dmitry Torokhov | 1729ad1 | 2011-10-29 12:37:06 -0700 | [diff] [blame] | 1173 | static int i8042_pm_reset(struct device *dev) |
| 1174 | { |
| 1175 | i8042_controller_reset(false); |
| 1176 | |
| 1177 | return 0; |
| 1178 | } |
| 1179 | |
Dmitry Torokhov | 1ca56e5 | 2010-07-20 20:25:34 -0700 | [diff] [blame] | 1180 | static int i8042_pm_restore(struct device *dev) |
| 1181 | { |
| 1182 | return i8042_controller_resume(false); |
| 1183 | } |
| 1184 | |
Dmitry Torokhov | ebd7768 | 2009-07-22 21:51:32 -0700 | [diff] [blame] | 1185 | static const struct dev_pm_ops i8042_pm_ops = { |
Dmitry Torokhov | 1729ad1 | 2011-10-29 12:37:06 -0700 | [diff] [blame] | 1186 | .suspend = i8042_pm_suspend, |
Dmitry Torokhov | 1ca56e5 | 2010-07-20 20:25:34 -0700 | [diff] [blame] | 1187 | .resume = i8042_pm_resume, |
Alan Jenkins | c2d1a2a | 2010-02-17 12:17:33 -0800 | [diff] [blame] | 1188 | .thaw = i8042_pm_thaw, |
Dmitry Torokhov | ebd7768 | 2009-07-22 21:51:32 -0700 | [diff] [blame] | 1189 | .poweroff = i8042_pm_reset, |
| 1190 | .restore = i8042_pm_restore, |
| 1191 | }; |
| 1192 | |
Dmitry Torokhov | 82dd9ef | 2007-02-18 01:40:30 -0500 | [diff] [blame] | 1193 | #endif /* CONFIG_PM */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1194 | |
| 1195 | /* |
| 1196 | * We need to reset the 8042 back to original mode on system shutdown, |
| 1197 | * because otherwise BIOSes will be confused. |
| 1198 | */ |
| 1199 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 1200 | static void i8042_shutdown(struct platform_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1201 | { |
Dmitry Torokhov | 1729ad1 | 2011-10-29 12:37:06 -0700 | [diff] [blame] | 1202 | i8042_controller_reset(false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1203 | } |
| 1204 | |
Dmitry Torokhov | f811341 | 2009-09-09 19:08:17 -0700 | [diff] [blame] | 1205 | static int __init i8042_create_kbd_port(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1206 | { |
| 1207 | struct serio *serio; |
| 1208 | struct i8042_port *port = &i8042_ports[I8042_KBD_PORT_NO]; |
| 1209 | |
Dmitry Torokhov | d39969d | 2005-09-10 12:04:42 -0500 | [diff] [blame] | 1210 | serio = kzalloc(sizeof(struct serio), GFP_KERNEL); |
Dmitry Torokhov | 0854e52 | 2005-09-04 01:41:27 -0500 | [diff] [blame] | 1211 | if (!serio) |
| 1212 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1213 | |
Dmitry Torokhov | 0854e52 | 2005-09-04 01:41:27 -0500 | [diff] [blame] | 1214 | serio->id.type = i8042_direct ? SERIO_8042 : SERIO_8042_XL; |
| 1215 | serio->write = i8042_dumbkbd ? NULL : i8042_kbd_write; |
Dmitry Torokhov | 0854e52 | 2005-09-04 01:41:27 -0500 | [diff] [blame] | 1216 | serio->start = i8042_start; |
| 1217 | serio->stop = i8042_stop; |
Dmitry Torokhov | 5ddbc77 | 2009-09-09 19:08:15 -0700 | [diff] [blame] | 1218 | serio->close = i8042_port_close; |
Dmitry Torokhov | 0854e52 | 2005-09-04 01:41:27 -0500 | [diff] [blame] | 1219 | serio->port_data = port; |
| 1220 | serio->dev.parent = &i8042_platform_device->dev; |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 1221 | strlcpy(serio->name, "i8042 KBD port", sizeof(serio->name)); |
Dmitry Torokhov | 0854e52 | 2005-09-04 01:41:27 -0500 | [diff] [blame] | 1222 | strlcpy(serio->phys, I8042_KBD_PHYS_DESC, sizeof(serio->phys)); |
Hans de Goede | a7c5868 | 2014-04-19 20:47:35 -0700 | [diff] [blame] | 1223 | strlcpy(serio->firmware_id, i8042_kbd_firmware_id, |
| 1224 | sizeof(serio->firmware_id)); |
Dmitry Torokhov | 0854e52 | 2005-09-04 01:41:27 -0500 | [diff] [blame] | 1225 | |
| 1226 | port->serio = serio; |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 1227 | port->irq = I8042_KBD_IRQ; |
Dmitry Torokhov | 0854e52 | 2005-09-04 01:41:27 -0500 | [diff] [blame] | 1228 | |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 1229 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1230 | } |
| 1231 | |
Dmitry Torokhov | f811341 | 2009-09-09 19:08:17 -0700 | [diff] [blame] | 1232 | static int __init i8042_create_aux_port(int idx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1233 | { |
| 1234 | struct serio *serio; |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 1235 | int port_no = idx < 0 ? I8042_AUX_PORT_NO : I8042_MUX_PORT_NO + idx; |
| 1236 | struct i8042_port *port = &i8042_ports[port_no]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1237 | |
Dmitry Torokhov | d39969d | 2005-09-10 12:04:42 -0500 | [diff] [blame] | 1238 | serio = kzalloc(sizeof(struct serio), GFP_KERNEL); |
Dmitry Torokhov | 0854e52 | 2005-09-04 01:41:27 -0500 | [diff] [blame] | 1239 | if (!serio) |
| 1240 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1241 | |
Dmitry Torokhov | 0854e52 | 2005-09-04 01:41:27 -0500 | [diff] [blame] | 1242 | serio->id.type = SERIO_8042; |
| 1243 | serio->write = i8042_aux_write; |
Dmitry Torokhov | 0854e52 | 2005-09-04 01:41:27 -0500 | [diff] [blame] | 1244 | serio->start = i8042_start; |
| 1245 | serio->stop = i8042_stop; |
| 1246 | serio->port_data = port; |
| 1247 | serio->dev.parent = &i8042_platform_device->dev; |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 1248 | if (idx < 0) { |
| 1249 | strlcpy(serio->name, "i8042 AUX port", sizeof(serio->name)); |
| 1250 | strlcpy(serio->phys, I8042_AUX_PHYS_DESC, sizeof(serio->phys)); |
Hans de Goede | a7c5868 | 2014-04-19 20:47:35 -0700 | [diff] [blame] | 1251 | strlcpy(serio->firmware_id, i8042_aux_firmware_id, |
| 1252 | sizeof(serio->firmware_id)); |
Dmitry Torokhov | 5ddbc77 | 2009-09-09 19:08:15 -0700 | [diff] [blame] | 1253 | serio->close = i8042_port_close; |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 1254 | } else { |
| 1255 | snprintf(serio->name, sizeof(serio->name), "i8042 AUX%d port", idx); |
| 1256 | snprintf(serio->phys, sizeof(serio->phys), I8042_MUX_PHYS_DESC, idx + 1); |
| 1257 | } |
Dmitry Torokhov | 0854e52 | 2005-09-04 01:41:27 -0500 | [diff] [blame] | 1258 | |
| 1259 | port->serio = serio; |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 1260 | port->mux = idx; |
| 1261 | port->irq = I8042_AUX_IRQ; |
Dmitry Torokhov | 0854e52 | 2005-09-04 01:41:27 -0500 | [diff] [blame] | 1262 | |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 1263 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1264 | } |
| 1265 | |
Dmitry Torokhov | f811341 | 2009-09-09 19:08:17 -0700 | [diff] [blame] | 1266 | static void __init i8042_free_kbd_port(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1267 | { |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 1268 | kfree(i8042_ports[I8042_KBD_PORT_NO].serio); |
| 1269 | i8042_ports[I8042_KBD_PORT_NO].serio = NULL; |
| 1270 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1271 | |
Dmitry Torokhov | f811341 | 2009-09-09 19:08:17 -0700 | [diff] [blame] | 1272 | static void __init i8042_free_aux_ports(void) |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 1273 | { |
| 1274 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1275 | |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 1276 | for (i = I8042_AUX_PORT_NO; i < I8042_NUM_PORTS; i++) { |
| 1277 | kfree(i8042_ports[i].serio); |
| 1278 | i8042_ports[i].serio = NULL; |
| 1279 | } |
| 1280 | } |
Dmitry Torokhov | 0854e52 | 2005-09-04 01:41:27 -0500 | [diff] [blame] | 1281 | |
Dmitry Torokhov | f811341 | 2009-09-09 19:08:17 -0700 | [diff] [blame] | 1282 | static void __init i8042_register_ports(void) |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 1283 | { |
| 1284 | int i; |
Dmitry Torokhov | 0854e52 | 2005-09-04 01:41:27 -0500 | [diff] [blame] | 1285 | |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 1286 | for (i = 0; i < I8042_NUM_PORTS; i++) { |
| 1287 | if (i8042_ports[i].serio) { |
| 1288 | printk(KERN_INFO "serio: %s at %#lx,%#lx irq %d\n", |
| 1289 | i8042_ports[i].serio->name, |
| 1290 | (unsigned long) I8042_DATA_REG, |
| 1291 | (unsigned long) I8042_COMMAND_REG, |
| 1292 | i8042_ports[i].irq); |
| 1293 | serio_register_port(i8042_ports[i].serio); |
| 1294 | } |
| 1295 | } |
| 1296 | } |
| 1297 | |
Bill Pemberton | e2619cf | 2012-11-23 21:50:47 -0800 | [diff] [blame] | 1298 | static void i8042_unregister_ports(void) |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 1299 | { |
| 1300 | int i; |
| 1301 | |
| 1302 | for (i = 0; i < I8042_NUM_PORTS; i++) { |
| 1303 | if (i8042_ports[i].serio) { |
| 1304 | serio_unregister_port(i8042_ports[i].serio); |
| 1305 | i8042_ports[i].serio = NULL; |
| 1306 | } |
| 1307 | } |
| 1308 | } |
| 1309 | |
Dmitry Torokhov | 181d683 | 2009-09-16 01:06:43 -0700 | [diff] [blame] | 1310 | /* |
| 1311 | * Checks whether port belongs to i8042 controller. |
| 1312 | */ |
| 1313 | bool i8042_check_port_owner(const struct serio *port) |
| 1314 | { |
| 1315 | int i; |
| 1316 | |
| 1317 | for (i = 0; i < I8042_NUM_PORTS; i++) |
| 1318 | if (i8042_ports[i].serio == port) |
| 1319 | return true; |
| 1320 | |
| 1321 | return false; |
| 1322 | } |
| 1323 | EXPORT_SYMBOL(i8042_check_port_owner); |
| 1324 | |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 1325 | static void i8042_free_irqs(void) |
| 1326 | { |
| 1327 | if (i8042_aux_irq_registered) |
| 1328 | free_irq(I8042_AUX_IRQ, i8042_platform_device); |
| 1329 | if (i8042_kbd_irq_registered) |
| 1330 | free_irq(I8042_KBD_IRQ, i8042_platform_device); |
| 1331 | |
Dmitry Torokhov | 386b384 | 2009-09-09 19:08:16 -0700 | [diff] [blame] | 1332 | i8042_aux_irq_registered = i8042_kbd_irq_registered = false; |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 1333 | } |
| 1334 | |
Dmitry Torokhov | f811341 | 2009-09-09 19:08:17 -0700 | [diff] [blame] | 1335 | static int __init i8042_setup_aux(void) |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 1336 | { |
| 1337 | int (*aux_enable)(void); |
| 1338 | int error; |
| 1339 | int i; |
| 1340 | |
| 1341 | if (i8042_check_aux()) |
| 1342 | return -ENODEV; |
| 1343 | |
| 1344 | if (i8042_nomux || i8042_check_mux()) { |
| 1345 | error = i8042_create_aux_port(-1); |
| 1346 | if (error) |
| 1347 | goto err_free_ports; |
| 1348 | aux_enable = i8042_enable_aux_port; |
| 1349 | } else { |
| 1350 | for (i = 0; i < I8042_NUM_MUX_PORTS; i++) { |
| 1351 | error = i8042_create_aux_port(i); |
| 1352 | if (error) |
| 1353 | goto err_free_ports; |
| 1354 | } |
| 1355 | aux_enable = i8042_enable_mux_ports; |
| 1356 | } |
| 1357 | |
| 1358 | error = request_irq(I8042_AUX_IRQ, i8042_interrupt, IRQF_SHARED, |
| 1359 | "i8042", i8042_platform_device); |
| 1360 | if (error) |
| 1361 | goto err_free_ports; |
| 1362 | |
| 1363 | if (aux_enable()) |
| 1364 | goto err_free_irq; |
| 1365 | |
Dmitry Torokhov | 386b384 | 2009-09-09 19:08:16 -0700 | [diff] [blame] | 1366 | i8042_aux_irq_registered = true; |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 1367 | return 0; |
| 1368 | |
| 1369 | err_free_irq: |
| 1370 | free_irq(I8042_AUX_IRQ, i8042_platform_device); |
| 1371 | err_free_ports: |
| 1372 | i8042_free_aux_ports(); |
| 1373 | return error; |
| 1374 | } |
| 1375 | |
Dmitry Torokhov | f811341 | 2009-09-09 19:08:17 -0700 | [diff] [blame] | 1376 | static int __init i8042_setup_kbd(void) |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 1377 | { |
| 1378 | int error; |
| 1379 | |
| 1380 | error = i8042_create_kbd_port(); |
| 1381 | if (error) |
| 1382 | return error; |
| 1383 | |
| 1384 | error = request_irq(I8042_KBD_IRQ, i8042_interrupt, IRQF_SHARED, |
| 1385 | "i8042", i8042_platform_device); |
| 1386 | if (error) |
| 1387 | goto err_free_port; |
| 1388 | |
| 1389 | error = i8042_enable_kbd_port(); |
| 1390 | if (error) |
| 1391 | goto err_free_irq; |
| 1392 | |
Dmitry Torokhov | 386b384 | 2009-09-09 19:08:16 -0700 | [diff] [blame] | 1393 | i8042_kbd_irq_registered = true; |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 1394 | return 0; |
| 1395 | |
| 1396 | err_free_irq: |
| 1397 | free_irq(I8042_KBD_IRQ, i8042_platform_device); |
| 1398 | err_free_port: |
| 1399 | i8042_free_kbd_port(); |
| 1400 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1401 | } |
| 1402 | |
Dmitry Torokhov | f811341 | 2009-09-09 19:08:17 -0700 | [diff] [blame] | 1403 | static int __init i8042_probe(struct platform_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1404 | { |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 1405 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1406 | |
Dmitry Torokhov | ec62e1c | 2010-03-08 22:37:09 -0800 | [diff] [blame] | 1407 | i8042_platform_device = dev; |
| 1408 | |
Dmitry Torokhov | 1ca56e5 | 2010-07-20 20:25:34 -0700 | [diff] [blame] | 1409 | if (i8042_reset) { |
| 1410 | error = i8042_controller_selftest(); |
| 1411 | if (error) |
| 1412 | return error; |
| 1413 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1414 | |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 1415 | error = i8042_controller_init(); |
| 1416 | if (error) |
| 1417 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1418 | |
Bruno Prémont | d35895d | 2008-05-27 01:36:04 -0400 | [diff] [blame] | 1419 | #ifdef CONFIG_X86 |
| 1420 | if (i8042_dritek) |
| 1421 | i8042_dritek_enable(); |
| 1422 | #endif |
| 1423 | |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 1424 | if (!i8042_noaux) { |
| 1425 | error = i8042_setup_aux(); |
| 1426 | if (error && error != -ENODEV && error != -EBUSY) |
| 1427 | goto out_fail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1428 | } |
| 1429 | |
Dmitry Torokhov | 945ef0d | 2005-09-04 01:42:00 -0500 | [diff] [blame] | 1430 | if (!i8042_nokbd) { |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 1431 | error = i8042_setup_kbd(); |
| 1432 | if (error) |
| 1433 | goto out_fail; |
Dmitry Torokhov | 945ef0d | 2005-09-04 01:42:00 -0500 | [diff] [blame] | 1434 | } |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 1435 | /* |
| 1436 | * Ok, everything is ready, let's register all serio ports |
| 1437 | */ |
| 1438 | i8042_register_ports(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1439 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1440 | return 0; |
Dmitry Torokhov | 0854e52 | 2005-09-04 01:41:27 -0500 | [diff] [blame] | 1441 | |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 1442 | out_fail: |
| 1443 | i8042_free_aux_ports(); /* in case KBD failed but AUX not */ |
| 1444 | i8042_free_irqs(); |
Dmitry Torokhov | 1729ad1 | 2011-10-29 12:37:06 -0700 | [diff] [blame] | 1445 | i8042_controller_reset(false); |
Dmitry Torokhov | ec62e1c | 2010-03-08 22:37:09 -0800 | [diff] [blame] | 1446 | i8042_platform_device = NULL; |
Dmitry Torokhov | 0854e52 | 2005-09-04 01:41:27 -0500 | [diff] [blame] | 1447 | |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 1448 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1449 | } |
| 1450 | |
Bill Pemberton | e2619cf | 2012-11-23 21:50:47 -0800 | [diff] [blame] | 1451 | static int i8042_remove(struct platform_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1452 | { |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 1453 | i8042_unregister_ports(); |
| 1454 | i8042_free_irqs(); |
Dmitry Torokhov | 1729ad1 | 2011-10-29 12:37:06 -0700 | [diff] [blame] | 1455 | i8042_controller_reset(false); |
Dmitry Torokhov | ec62e1c | 2010-03-08 22:37:09 -0800 | [diff] [blame] | 1456 | i8042_platform_device = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1457 | |
Dmitry Torokhov | 87fd631 | 2005-12-28 01:25:11 -0500 | [diff] [blame] | 1458 | return 0; |
| 1459 | } |
| 1460 | |
| 1461 | static struct platform_driver i8042_driver = { |
| 1462 | .driver = { |
| 1463 | .name = "i8042", |
| 1464 | .owner = THIS_MODULE, |
Dmitry Torokhov | ebd7768 | 2009-07-22 21:51:32 -0700 | [diff] [blame] | 1465 | #ifdef CONFIG_PM |
| 1466 | .pm = &i8042_pm_ops, |
| 1467 | #endif |
Dmitry Torokhov | 87fd631 | 2005-12-28 01:25:11 -0500 | [diff] [blame] | 1468 | }, |
Bill Pemberton | 1cb0aa8 | 2012-11-23 21:27:39 -0800 | [diff] [blame] | 1469 | .remove = i8042_remove, |
Dmitry Torokhov | 82dd9ef | 2007-02-18 01:40:30 -0500 | [diff] [blame] | 1470 | .shutdown = i8042_shutdown, |
Dmitry Torokhov | 87fd631 | 2005-12-28 01:25:11 -0500 | [diff] [blame] | 1471 | }; |
| 1472 | |
| 1473 | static int __init i8042_init(void) |
| 1474 | { |
Dmitry Torokhov | ec62e1c | 2010-03-08 22:37:09 -0800 | [diff] [blame] | 1475 | struct platform_device *pdev; |
Dmitry Torokhov | 87fd631 | 2005-12-28 01:25:11 -0500 | [diff] [blame] | 1476 | int err; |
| 1477 | |
| 1478 | dbg_init(); |
| 1479 | |
| 1480 | err = i8042_platform_init(); |
| 1481 | if (err) |
| 1482 | return err; |
| 1483 | |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 1484 | err = i8042_controller_check(); |
| 1485 | if (err) |
| 1486 | goto err_platform_exit; |
Dmitry Torokhov | 87fd631 | 2005-12-28 01:25:11 -0500 | [diff] [blame] | 1487 | |
Dmitry Torokhov | ec62e1c | 2010-03-08 22:37:09 -0800 | [diff] [blame] | 1488 | pdev = platform_create_bundle(&i8042_driver, i8042_probe, NULL, 0, NULL, 0); |
| 1489 | if (IS_ERR(pdev)) { |
| 1490 | err = PTR_ERR(pdev); |
Dmitry Torokhov | f811341 | 2009-09-09 19:08:17 -0700 | [diff] [blame] | 1491 | goto err_platform_exit; |
Dmitry Torokhov | 87fd631 | 2005-12-28 01:25:11 -0500 | [diff] [blame] | 1492 | } |
| 1493 | |
Dmitry Torokhov | de9ce70 | 2006-09-10 21:57:21 -0400 | [diff] [blame] | 1494 | panic_blink = i8042_panic_blink; |
| 1495 | |
Dmitry Torokhov | 87fd631 | 2005-12-28 01:25:11 -0500 | [diff] [blame] | 1496 | return 0; |
| 1497 | |
Dmitry Torokhov | 87fd631 | 2005-12-28 01:25:11 -0500 | [diff] [blame] | 1498 | err_platform_exit: |
| 1499 | i8042_platform_exit(); |
Dmitry Torokhov | 87fd631 | 2005-12-28 01:25:11 -0500 | [diff] [blame] | 1500 | return err; |
| 1501 | } |
| 1502 | |
| 1503 | static void __exit i8042_exit(void) |
| 1504 | { |
Dmitry Torokhov | f811341 | 2009-09-09 19:08:17 -0700 | [diff] [blame] | 1505 | platform_device_unregister(i8042_platform_device); |
Dmitry Torokhov | af045b8 | 2010-08-31 17:27:02 -0700 | [diff] [blame] | 1506 | platform_driver_unregister(&i8042_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1507 | i8042_platform_exit(); |
| 1508 | |
| 1509 | panic_blink = NULL; |
| 1510 | } |
| 1511 | |
| 1512 | module_init(i8042_init); |
| 1513 | module_exit(i8042_exit); |