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