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