blob: 1df02d25aca5e4843cc6b02988f93dc0c05f2803 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Torokhov7e044e02009-05-09 16:08:05 -070013#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/delay.h>
15#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/interrupt.h>
17#include <linux/ioport.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/init.h>
19#include <linux/serio.h>
20#include <linux/err.h>
21#include <linux/rcupdate.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010022#include <linux/platform_device.h>
Márton Németh553a05b2007-10-22 00:56:52 -040023#include <linux/i8042.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#include <asm/io.h>
26
27MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
28MODULE_DESCRIPTION("i8042 keyboard and mouse controller driver");
29MODULE_LICENSE("GPL");
30
Dmitry Torokhov386b3842009-09-09 19:08:16 -070031static bool i8042_nokbd;
Dmitry Torokhov945ef0d2005-09-04 01:42:00 -050032module_param_named(nokbd, i8042_nokbd, bool, 0);
33MODULE_PARM_DESC(nokbd, "Do not probe or use KBD port.");
34
Dmitry Torokhov386b3842009-09-09 19:08:16 -070035static bool i8042_noaux;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036module_param_named(noaux, i8042_noaux, bool, 0);
37MODULE_PARM_DESC(noaux, "Do not probe or use AUX (mouse) port.");
38
Dmitry Torokhov386b3842009-09-09 19:08:16 -070039static bool i8042_nomux;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040module_param_named(nomux, i8042_nomux, bool, 0);
41MODULE_PARM_DESC(nomux, "Do not check whether an active multiplexing conrtoller is present.");
42
Dmitry Torokhov386b3842009-09-09 19:08:16 -070043static bool i8042_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044module_param_named(unlock, i8042_unlock, bool, 0);
45MODULE_PARM_DESC(unlock, "Ignore keyboard lock.");
46
Dmitry Torokhov386b3842009-09-09 19:08:16 -070047static bool i8042_reset;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048module_param_named(reset, i8042_reset, bool, 0);
49MODULE_PARM_DESC(reset, "Reset controller during init and cleanup.");
50
Dmitry Torokhov386b3842009-09-09 19:08:16 -070051static bool i8042_direct;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052module_param_named(direct, i8042_direct, bool, 0);
53MODULE_PARM_DESC(direct, "Put keyboard port into non-translated mode.");
54
Dmitry Torokhov386b3842009-09-09 19:08:16 -070055static bool i8042_dumbkbd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056module_param_named(dumbkbd, i8042_dumbkbd, bool, 0);
57MODULE_PARM_DESC(dumbkbd, "Pretend that controller can only read data from keyboard");
58
Dmitry Torokhov386b3842009-09-09 19:08:16 -070059static bool i8042_noloop;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060module_param_named(noloop, i8042_noloop, bool, 0);
61MODULE_PARM_DESC(noloop, "Disable the AUX Loopback command while probing for the AUX port");
62
63static unsigned int i8042_blink_frequency = 500;
64module_param_named(panicblink, i8042_blink_frequency, uint, 0600);
65MODULE_PARM_DESC(panicblink, "Frequency with which keyboard LEDs should blink when kernel panics");
66
Carlos Corbacho8987fec2008-01-21 01:04:40 -050067#ifdef CONFIG_X86
Dmitry Torokhov386b3842009-09-09 19:08:16 -070068static bool i8042_dritek;
Carlos Corbacho8987fec2008-01-21 01:04:40 -050069module_param_named(dritek, i8042_dritek, bool, 0);
70MODULE_PARM_DESC(dritek, "Force enable the Dritek keyboard extension");
71#endif
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#ifdef CONFIG_PNP
Dmitry Torokhov386b3842009-09-09 19:08:16 -070074static bool i8042_nopnp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075module_param_named(nopnp, i8042_nopnp, bool, 0);
76MODULE_PARM_DESC(nopnp, "Do not use PNP to detect controller settings");
77#endif
78
79#define DEBUG
80#ifdef DEBUG
Dmitry Torokhov386b3842009-09-09 19:08:16 -070081static bool i8042_debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082module_param_named(debug, i8042_debug, bool, 0600);
83MODULE_PARM_DESC(debug, "Turn i8042 debugging mode on and off");
84#endif
85
Dmitry Torokhov1c7827a2009-09-03 21:45:34 -070086static bool i8042_bypass_aux_irq_test;
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088#include "i8042.h"
89
Dmitry Torokhov181d6832009-09-16 01:06:43 -070090/*
91 * i8042_lock protects serialization between i8042_command and
92 * the interrupt handler.
93 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070094static DEFINE_SPINLOCK(i8042_lock);
95
Dmitry Torokhov181d6832009-09-16 01:06:43 -070096/*
97 * Writers to AUX and KBD ports as well as users issuing i8042_command
98 * directly should acquire i8042_mutex (by means of calling
99 * i8042_lock_chip() and i8042_unlock_ship() helpers) to ensure that
100 * they do not disturb each other (unfortunately in many i8042
101 * implementations write to one of the ports will immediately abort
102 * command that is being processed by another port).
103 */
104static DEFINE_MUTEX(i8042_mutex);
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106struct i8042_port {
107 struct serio *serio;
108 int irq;
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700109 bool exists;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 signed char mux;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111};
112
113#define I8042_KBD_PORT_NO 0
114#define I8042_AUX_PORT_NO 1
115#define I8042_MUX_PORT_NO 2
116#define I8042_NUM_PORTS (I8042_NUM_MUX_PORTS + 2)
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400117
118static struct i8042_port i8042_ports[I8042_NUM_PORTS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120static unsigned char i8042_initial_ctr;
121static unsigned char i8042_ctr;
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700122static bool i8042_mux_present;
123static bool i8042_kbd_irq_registered;
124static bool i8042_aux_irq_registered;
Dmitry Torokhov817e6ba2006-10-11 01:44:28 -0400125static unsigned char i8042_suppress_kbd_ack;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126static struct platform_device *i8042_platform_device;
127
David Howells7d12e782006-10-05 14:55:46 +0100128static irqreturn_t i8042_interrupt(int irq, void *dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Dmitry Torokhov181d6832009-09-16 01:06:43 -0700130void i8042_lock_chip(void)
131{
132 mutex_lock(&i8042_mutex);
133}
134EXPORT_SYMBOL(i8042_lock_chip);
135
136void i8042_unlock_chip(void)
137{
138 mutex_unlock(&i8042_mutex);
139}
140EXPORT_SYMBOL(i8042_unlock_chip);
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142/*
143 * The i8042_wait_read() and i8042_wait_write functions wait for the i8042 to
144 * be ready for reading values from it / writing values to it.
145 * Called always with i8042_lock held.
146 */
147
148static int i8042_wait_read(void)
149{
150 int i = 0;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 while ((~i8042_read_status() & I8042_STR_OBF) && (i < I8042_CTL_TIMEOUT)) {
153 udelay(50);
154 i++;
155 }
156 return -(i == I8042_CTL_TIMEOUT);
157}
158
159static int i8042_wait_write(void)
160{
161 int i = 0;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 while ((i8042_read_status() & I8042_STR_IBF) && (i < I8042_CTL_TIMEOUT)) {
164 udelay(50);
165 i++;
166 }
167 return -(i == I8042_CTL_TIMEOUT);
168}
169
170/*
171 * i8042_flush() flushes all data that may be in the keyboard and mouse buffers
172 * of the i8042 down the toilet.
173 */
174
175static int i8042_flush(void)
176{
177 unsigned long flags;
178 unsigned char data, str;
179 int i = 0;
180
181 spin_lock_irqsave(&i8042_lock, flags);
182
183 while (((str = i8042_read_status()) & I8042_STR_OBF) && (i < I8042_BUFFER_SIZE)) {
184 udelay(50);
185 data = i8042_read_data();
186 i++;
187 dbg("%02x <- i8042 (flush, %s)", data,
188 str & I8042_STR_AUXDATA ? "aux" : "kbd");
189 }
190
191 spin_unlock_irqrestore(&i8042_lock, flags);
192
193 return i;
194}
195
196/*
197 * i8042_command() executes a command on the i8042. It also sends the input
198 * parameter(s) of the commands to it, and receives the output value(s). The
199 * parameters are to be stored in the param array, and the output is placed
200 * into the same array. The number of the parameters and output values is
201 * encoded in bits 8-11 of the command number.
202 */
203
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400204static int __i8042_command(unsigned char *param, int command)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400206 int i, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
208 if (i8042_noloop && command == I8042_CMD_AUX_LOOP)
209 return -1;
210
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400211 error = i8042_wait_write();
212 if (error)
213 return error;
Dmitry Torokhov463a4f72005-07-15 01:51:56 -0500214
215 dbg("%02x -> i8042 (command)", command & 0xff);
216 i8042_write_command(command & 0xff);
217
218 for (i = 0; i < ((command >> 12) & 0xf); i++) {
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400219 error = i8042_wait_write();
220 if (error)
221 return error;
Dmitry Torokhov463a4f72005-07-15 01:51:56 -0500222 dbg("%02x -> i8042 (parameter)", param[i]);
223 i8042_write_data(param[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 }
225
Dmitry Torokhov463a4f72005-07-15 01:51:56 -0500226 for (i = 0; i < ((command >> 8) & 0xf); i++) {
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400227 error = i8042_wait_read();
228 if (error) {
229 dbg(" -- i8042 (timeout)");
230 return error;
231 }
Dmitry Torokhov463a4f72005-07-15 01:51:56 -0500232
233 if (command == I8042_CMD_AUX_LOOP &&
234 !(i8042_read_status() & I8042_STR_AUXDATA)) {
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400235 dbg(" -- i8042 (auxerr)");
236 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 }
238
Dmitry Torokhov463a4f72005-07-15 01:51:56 -0500239 param[i] = i8042_read_data();
240 dbg("%02x <- i8042 (return)", param[i]);
241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400243 return 0;
244}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Márton Németh553a05b2007-10-22 00:56:52 -0400246int i8042_command(unsigned char *param, int command)
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400247{
248 unsigned long flags;
249 int retval;
250
251 spin_lock_irqsave(&i8042_lock, flags);
252 retval = __i8042_command(param, command);
Dmitry Torokhov463a4f72005-07-15 01:51:56 -0500253 spin_unlock_irqrestore(&i8042_lock, flags);
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 return retval;
256}
Márton Németh553a05b2007-10-22 00:56:52 -0400257EXPORT_SYMBOL(i8042_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259/*
260 * i8042_kbd_write() sends a byte out through the keyboard interface.
261 */
262
263static int i8042_kbd_write(struct serio *port, unsigned char c)
264{
265 unsigned long flags;
266 int retval = 0;
267
268 spin_lock_irqsave(&i8042_lock, flags);
269
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400270 if (!(retval = i8042_wait_write())) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 dbg("%02x -> i8042 (kbd-data)", c);
272 i8042_write_data(c);
273 }
274
275 spin_unlock_irqrestore(&i8042_lock, flags);
276
277 return retval;
278}
279
280/*
281 * i8042_aux_write() sends a byte out through the aux interface.
282 */
283
284static int i8042_aux_write(struct serio *serio, unsigned char c)
285{
286 struct i8042_port *port = serio->port_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Dmitry Torokhovf4e3c712006-11-02 23:27:49 -0500288 return i8042_command(&c, port->mux == -1 ?
289 I8042_CMD_AUX_SEND :
290 I8042_CMD_MUX_SEND + port->mux);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291}
292
Dmitry Torokhov5ddbc772009-09-09 19:08:15 -0700293
294/*
295 * i8042_aux_close attempts to clear AUX or KBD port state by disabling
296 * and then re-enabling it.
297 */
298
299static void i8042_port_close(struct serio *serio)
300{
301 int irq_bit;
302 int disable_bit;
303 const char *port_name;
304
305 if (serio == i8042_ports[I8042_AUX_PORT_NO].serio) {
306 irq_bit = I8042_CTR_AUXINT;
307 disable_bit = I8042_CTR_AUXDIS;
308 port_name = "AUX";
309 } else {
310 irq_bit = I8042_CTR_KBDINT;
311 disable_bit = I8042_CTR_KBDDIS;
312 port_name = "KBD";
313 }
314
315 i8042_ctr &= ~irq_bit;
316 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR))
317 printk(KERN_WARNING
318 "i8042.c: Can't write CTR while closing %s port.\n",
319 port_name);
320
321 udelay(50);
322
323 i8042_ctr &= ~disable_bit;
324 i8042_ctr |= irq_bit;
325 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR))
326 printk(KERN_ERR "i8042.c: Can't reactivate %s port.\n",
327 port_name);
328
329 /*
330 * See if there is any data appeared while we were messing with
331 * port state.
332 */
333 i8042_interrupt(0, NULL);
334}
335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 * i8042_start() is called by serio core when port is about to finish
338 * registering. It will mark port as existing so i8042_interrupt can
339 * start sending data through it.
340 */
341static int i8042_start(struct serio *serio)
342{
343 struct i8042_port *port = serio->port_data;
344
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700345 port->exists = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 mb();
347 return 0;
348}
349
350/*
351 * i8042_stop() marks serio port as non-existing so i8042_interrupt
352 * will not try to send data to the port that is about to go away.
353 * The function is called by serio core as part of unregister procedure.
354 */
355static void i8042_stop(struct serio *serio)
356{
357 struct i8042_port *port = serio->port_data;
358
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700359 port->exists = false;
Dmitry Torokhova8399c52007-11-04 00:44:31 -0400360
361 /*
362 * We synchronize with both AUX and KBD IRQs because there is
363 * a (very unlikely) chance that AUX IRQ is raised for KBD port
364 * and vice versa.
365 */
366 synchronize_irq(I8042_AUX_IRQ);
367 synchronize_irq(I8042_KBD_IRQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 port->serio = NULL;
369}
370
371/*
372 * i8042_interrupt() is the most important function in this driver -
373 * it handles the interrupts from the i8042, and sends incoming bytes
374 * to the upper layers.
375 */
376
David Howells7d12e782006-10-05 14:55:46 +0100377static irqreturn_t i8042_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
379 struct i8042_port *port;
380 unsigned long flags;
381 unsigned char str, data;
382 unsigned int dfl;
383 unsigned int port_no;
Dmitry Torokhov817e6ba2006-10-11 01:44:28 -0400384 int ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 spin_lock_irqsave(&i8042_lock, flags);
387 str = i8042_read_status();
388 if (unlikely(~str & I8042_STR_OBF)) {
389 spin_unlock_irqrestore(&i8042_lock, flags);
390 if (irq) dbg("Interrupt %d, without any data", irq);
391 ret = 0;
392 goto out;
393 }
394 data = i8042_read_data();
395 spin_unlock_irqrestore(&i8042_lock, flags);
396
397 if (i8042_mux_present && (str & I8042_STR_AUXDATA)) {
398 static unsigned long last_transmit;
399 static unsigned char last_str;
400
401 dfl = 0;
402 if (str & I8042_STR_MUXERR) {
403 dbg("MUX error, status is %02x, data is %02x", str, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404/*
405 * When MUXERR condition is signalled the data register can only contain
406 * 0xfd, 0xfe or 0xff if implementation follows the spec. Unfortunately
Dmitry Torokhova216a4b2006-11-17 01:07:06 -0500407 * it is not always the case. Some KBCs also report 0xfc when there is
408 * nothing connected to the port while others sometimes get confused which
409 * port the data came from and signal error leaving the data intact. They
410 * _do not_ revert to legacy mode (actually I've never seen KBC reverting
411 * to legacy mode yet, when we see one we'll add proper handling).
412 * Anyway, we process 0xfc, 0xfd, 0xfe and 0xff as timeouts, and for the
413 * rest assume that the data came from the same serio last byte
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 * was transmitted (if transmission happened not too long ago).
415 */
Dmitry Torokhova216a4b2006-11-17 01:07:06 -0500416
417 switch (data) {
418 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 if (time_before(jiffies, last_transmit + HZ/10)) {
420 str = last_str;
421 break;
422 }
423 /* fall through - report timeout */
Dmitry Torokhova216a4b2006-11-17 01:07:06 -0500424 case 0xfc:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 case 0xfd:
426 case 0xfe: dfl = SERIO_TIMEOUT; data = 0xfe; break;
427 case 0xff: dfl = SERIO_PARITY; data = 0xfe; break;
428 }
429 }
430
431 port_no = I8042_MUX_PORT_NO + ((str >> 6) & 3);
432 last_str = str;
433 last_transmit = jiffies;
434 } else {
435
436 dfl = ((str & I8042_STR_PARITY) ? SERIO_PARITY : 0) |
437 ((str & I8042_STR_TIMEOUT) ? SERIO_TIMEOUT : 0);
438
439 port_no = (str & I8042_STR_AUXDATA) ?
440 I8042_AUX_PORT_NO : I8042_KBD_PORT_NO;
441 }
442
443 port = &i8042_ports[port_no];
444
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400445 dbg("%02x <- i8042 (interrupt, %d, %d%s%s)",
446 data, port_no, irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 dfl & SERIO_PARITY ? ", bad parity" : "",
448 dfl & SERIO_TIMEOUT ? ", timeout" : "");
449
Dmitry Torokhov817e6ba2006-10-11 01:44:28 -0400450 if (unlikely(i8042_suppress_kbd_ack))
451 if (port_no == I8042_KBD_PORT_NO &&
452 (data == 0xfa || data == 0xfe)) {
Dmitry Torokhov19f3c3e2007-01-18 00:42:31 -0500453 i8042_suppress_kbd_ack--;
Dmitry Torokhov817e6ba2006-10-11 01:44:28 -0400454 goto out;
455 }
456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 if (likely(port->exists))
David Howells7d12e782006-10-05 14:55:46 +0100458 serio_interrupt(port->serio, data, dfl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
Dmitry Torokhov0854e522005-09-04 01:41:27 -0500460 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 return IRQ_RETVAL(ret);
462}
463
464/*
Dmitry Torokhov5ddbc772009-09-09 19:08:15 -0700465 * i8042_enable_kbd_port enables keyboard port on chip
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400466 */
467
468static int i8042_enable_kbd_port(void)
469{
470 i8042_ctr &= ~I8042_CTR_KBDDIS;
471 i8042_ctr |= I8042_CTR_KBDINT;
472
473 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
Markus Armbruster018db6b2007-07-18 01:20:41 -0400474 i8042_ctr &= ~I8042_CTR_KBDINT;
475 i8042_ctr |= I8042_CTR_KBDDIS;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400476 printk(KERN_ERR "i8042.c: Failed to enable KBD port.\n");
477 return -EIO;
478 }
479
480 return 0;
481}
482
483/*
484 * i8042_enable_aux_port enables AUX (mouse) port on chip
485 */
486
487static int i8042_enable_aux_port(void)
488{
489 i8042_ctr &= ~I8042_CTR_AUXDIS;
490 i8042_ctr |= I8042_CTR_AUXINT;
491
492 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
Markus Armbruster018db6b2007-07-18 01:20:41 -0400493 i8042_ctr &= ~I8042_CTR_AUXINT;
494 i8042_ctr |= I8042_CTR_AUXDIS;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400495 printk(KERN_ERR "i8042.c: Failed to enable AUX port.\n");
496 return -EIO;
497 }
498
499 return 0;
500}
501
502/*
503 * i8042_enable_mux_ports enables 4 individual AUX ports after
504 * the controller has been switched into Multiplexed mode
505 */
506
507static int i8042_enable_mux_ports(void)
508{
509 unsigned char param;
510 int i;
511
512 for (i = 0; i < I8042_NUM_MUX_PORTS; i++) {
513 i8042_command(&param, I8042_CMD_MUX_PFX + i);
514 i8042_command(&param, I8042_CMD_AUX_ENABLE);
515 }
516
517 return i8042_enable_aux_port();
518}
519
520/*
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700521 * i8042_set_mux_mode checks whether the controller has an
522 * active multiplexor and puts the chip into Multiplexed (true)
523 * or Legacy (false) mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 */
525
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700526static int i8042_set_mux_mode(bool multiplex, unsigned char *mux_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
528
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700529 unsigned char param, val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530/*
531 * Get rid of bytes in the queue.
532 */
533
534 i8042_flush();
535
536/*
537 * Internal loopback test - send three bytes, they should come back from the
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400538 * mouse interface, the last should be version.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 */
540
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700541 param = val = 0xf0;
542 if (i8042_command(&param, I8042_CMD_AUX_LOOP) || param != val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 return -1;
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700544 param = val = multiplex ? 0x56 : 0xf6;
545 if (i8042_command(&param, I8042_CMD_AUX_LOOP) || param != val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 return -1;
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700547 param = val = multiplex ? 0xa4 : 0xa5;
548 if (i8042_command(&param, I8042_CMD_AUX_LOOP) || param == val)
549 return -1;
550
551/*
552 * Workaround for interference with USB Legacy emulation
553 * that causes a v10.12 MUX to be found.
554 */
555 if (param == 0xac)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 return -1;
557
558 if (mux_version)
Dmitry Torokhov463a4f72005-07-15 01:51:56 -0500559 *mux_version = param;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
561 return 0;
562}
563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564/*
565 * i8042_check_mux() checks whether the controller supports the PS/2 Active
566 * Multiplexing specification by Synaptics, Phoenix, Insyde and
567 * LCS/Telegraphics.
568 */
569
Dmitry Torokhovf8113412009-09-09 19:08:17 -0700570static int __init i8042_check_mux(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
572 unsigned char mux_version;
573
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700574 if (i8042_set_mux_mode(true, &mux_version))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 return -1;
576
577 printk(KERN_INFO "i8042.c: Detected active multiplexing controller, rev %d.%d.\n",
578 (mux_version >> 4) & 0xf, mux_version & 0xf);
579
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400580/*
581 * Disable all muxed ports by disabling AUX.
582 */
583 i8042_ctr |= I8042_CTR_AUXDIS;
584 i8042_ctr &= ~I8042_CTR_AUXINT;
585
586 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
587 printk(KERN_ERR "i8042.c: Failed to disable AUX port, can't use MUX.\n");
588 return -EIO;
589 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700591 i8042_mux_present = true;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400592
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 return 0;
594}
595
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400596/*
597 * The following is used to test AUX IRQ delivery.
598 */
Dmitry Torokhovf8113412009-09-09 19:08:17 -0700599static struct completion i8042_aux_irq_delivered __initdata;
600static bool i8042_irq_being_tested __initdata;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400601
Dmitry Torokhovf8113412009-09-09 19:08:17 -0700602static irqreturn_t __init i8042_aux_test_irq(int irq, void *dev_id)
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400603{
604 unsigned long flags;
605 unsigned char str, data;
Fernando Luis Vázquez Caoe3758b22007-08-30 00:04:15 -0400606 int ret = 0;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400607
608 spin_lock_irqsave(&i8042_lock, flags);
609 str = i8042_read_status();
610 if (str & I8042_STR_OBF) {
611 data = i8042_read_data();
Dmitry Torokhovd3d2dfe2009-10-08 20:58:13 -0700612 dbg("%02x <- i8042 (aux_test_irq, %s)",
613 data, str & I8042_STR_AUXDATA ? "aux" : "kbd");
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400614 if (i8042_irq_being_tested &&
615 data == 0xa5 && (str & I8042_STR_AUXDATA))
616 complete(&i8042_aux_irq_delivered);
Fernando Luis Vázquez Caoe3758b22007-08-30 00:04:15 -0400617 ret = 1;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400618 }
619 spin_unlock_irqrestore(&i8042_lock, flags);
620
Fernando Luis Vázquez Caoe3758b22007-08-30 00:04:15 -0400621 return IRQ_RETVAL(ret);
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400622}
623
Roland Scheideggerd2ada552007-05-08 01:31:40 -0400624/*
625 * i8042_toggle_aux - enables or disables AUX port on i8042 via command and
626 * verifies success by readinng CTR. Used when testing for presence of AUX
627 * port.
628 */
Dmitry Torokhovf8113412009-09-09 19:08:17 -0700629static int __init i8042_toggle_aux(bool on)
Roland Scheideggerd2ada552007-05-08 01:31:40 -0400630{
631 unsigned char param;
632 int i;
633
634 if (i8042_command(&param,
635 on ? I8042_CMD_AUX_ENABLE : I8042_CMD_AUX_DISABLE))
636 return -1;
637
638 /* some chips need some time to set the I8042_CTR_AUXDIS bit */
639 for (i = 0; i < 100; i++) {
640 udelay(50);
641
642 if (i8042_command(&param, I8042_CMD_CTL_RCTR))
643 return -1;
644
645 if (!(param & I8042_CTR_AUXDIS) == on)
646 return 0;
647 }
648
649 return -1;
650}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
652/*
653 * i8042_check_aux() applies as much paranoia as it can at detecting
654 * the presence of an AUX interface.
655 */
656
Dmitry Torokhovf8113412009-09-09 19:08:17 -0700657static int __init i8042_check_aux(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400659 int retval = -1;
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700660 bool irq_registered = false;
661 bool aux_loop_broken = false;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400662 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 unsigned char param;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
665/*
666 * Get rid of bytes in the queue.
667 */
668
669 i8042_flush();
670
671/*
672 * Internal loopback test - filters out AT-type i8042's. Unfortunately
673 * SiS screwed up and their 5597 doesn't support the LOOP command even
674 * though it has an AUX port.
675 */
676
677 param = 0x5a;
Dmitry Torokhov3ca5de62007-03-07 23:20:55 -0500678 retval = i8042_command(&param, I8042_CMD_AUX_LOOP);
679 if (retval || param != 0x5a) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
681/*
682 * External connection test - filters out AT-soldered PS/2 i8042's
683 * 0x00 - no error, 0x01-0x03 - clock/data stuck, 0xff - general error
684 * 0xfa - no error on some notebooks which ignore the spec
685 * Because it's common for chipsets to return error on perfectly functioning
686 * AUX ports, we test for this only when the LOOP command failed.
687 */
688
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400689 if (i8042_command(&param, I8042_CMD_AUX_TEST) ||
690 (param && param != 0xfa && param != 0xff))
691 return -1;
Dmitry Torokhov1e4865f2007-02-10 01:29:53 -0500692
Dmitry Torokhov3ca5de62007-03-07 23:20:55 -0500693/*
694 * If AUX_LOOP completed without error but returned unexpected data
695 * mark it as broken
696 */
697 if (!retval)
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700698 aux_loop_broken = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 }
700
701/*
702 * Bit assignment test - filters out PS/2 i8042's in AT mode
703 */
704
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700705 if (i8042_toggle_aux(false)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 printk(KERN_WARNING "Failed to disable AUX port, but continuing anyway... Is this a SiS?\n");
707 printk(KERN_WARNING "If AUX port is really absent please use the 'i8042.noaux' option.\n");
708 }
709
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700710 if (i8042_toggle_aux(true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 return -1;
712
713/*
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400714 * Test AUX IRQ delivery to make sure BIOS did not grab the IRQ and
715 * used it for a PCI card or somethig else.
716 */
717
Dmitry Torokhov1c7827a2009-09-03 21:45:34 -0700718 if (i8042_noloop || i8042_bypass_aux_irq_test || aux_loop_broken) {
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400719/*
720 * Without LOOP command we can't test AUX IRQ delivery. Assume the port
721 * is working and hope we are right.
722 */
723 retval = 0;
724 goto out;
725 }
726
727 if (request_irq(I8042_AUX_IRQ, i8042_aux_test_irq, IRQF_SHARED,
728 "i8042", i8042_platform_device))
729 goto out;
730
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700731 irq_registered = true;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400732
733 if (i8042_enable_aux_port())
734 goto out;
735
736 spin_lock_irqsave(&i8042_lock, flags);
737
738 init_completion(&i8042_aux_irq_delivered);
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700739 i8042_irq_being_tested = true;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400740
741 param = 0xa5;
742 retval = __i8042_command(&param, I8042_CMD_AUX_LOOP & 0xf0ff);
743
744 spin_unlock_irqrestore(&i8042_lock, flags);
745
746 if (retval)
747 goto out;
748
749 if (wait_for_completion_timeout(&i8042_aux_irq_delivered,
750 msecs_to_jiffies(250)) == 0) {
751/*
752 * AUX IRQ was never delivered so we need to flush the controller to
753 * get rid of the byte we put there; otherwise keyboard may not work.
754 */
Dmitry Torokhovd3d2dfe2009-10-08 20:58:13 -0700755 dbg(" -- i8042 (aux irq test timeout)");
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400756 i8042_flush();
757 retval = -1;
758 }
759
760 out:
761
762/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 * Disable the interface.
764 */
765
766 i8042_ctr |= I8042_CTR_AUXDIS;
767 i8042_ctr &= ~I8042_CTR_AUXINT;
768
769 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR))
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400770 retval = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400772 if (irq_registered)
773 free_irq(I8042_AUX_IRQ, i8042_platform_device);
774
775 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776}
777
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400778static int i8042_controller_check(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779{
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400780 if (i8042_flush() == I8042_BUFFER_SIZE) {
781 printk(KERN_ERR "i8042.c: No controller found.\n");
782 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 }
784
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 return 0;
786}
787
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400788static int i8042_controller_selftest(void)
Vojtech Pavlik2673c8362005-05-28 02:11:27 -0500789{
790 unsigned char param;
Arjan van de Ven5ea2fc62009-04-09 11:36:50 -0700791 int i = 0;
Vojtech Pavlik2673c8362005-05-28 02:11:27 -0500792
793 if (!i8042_reset)
794 return 0;
795
Arjan van de Ven5ea2fc62009-04-09 11:36:50 -0700796 /*
797 * We try this 5 times; on some really fragile systems this does not
798 * take the first time...
799 */
800 do {
Vojtech Pavlik2673c8362005-05-28 02:11:27 -0500801
Arjan van de Ven5ea2fc62009-04-09 11:36:50 -0700802 if (i8042_command(&param, I8042_CMD_CTL_TEST)) {
803 printk(KERN_ERR "i8042.c: i8042 controller self test timeout.\n");
804 return -ENODEV;
805 }
806
807 if (param == I8042_RET_CTL_TEST)
808 return 0;
809
Vojtech Pavlik2673c8362005-05-28 02:11:27 -0500810 printk(KERN_ERR "i8042.c: i8042 controller selftest failed. (%#x != %#x)\n",
Arjan van de Ven5ea2fc62009-04-09 11:36:50 -0700811 param, I8042_RET_CTL_TEST);
812 msleep(50);
813 } while (i++ < 5);
Vojtech Pavlik2673c8362005-05-28 02:11:27 -0500814
Arjan van de Ven5ea2fc62009-04-09 11:36:50 -0700815#ifdef CONFIG_X86
816 /*
817 * On x86, we don't fail entire i8042 initialization if controller
818 * reset fails in hopes that keyboard port will still be functional
819 * and user will still get a working keyboard. This is especially
820 * important on netbooks. On other arches we trust hardware more.
821 */
822 printk(KERN_INFO
823 "i8042: giving up on controller selftest, continuing anyway...\n");
Vojtech Pavlik2673c8362005-05-28 02:11:27 -0500824 return 0;
Arjan van de Ven5ea2fc62009-04-09 11:36:50 -0700825#else
826 return -EIO;
827#endif
Vojtech Pavlik2673c8362005-05-28 02:11:27 -0500828}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
830/*
831 * i8042_controller init initializes the i8042 controller, and,
832 * most importantly, sets it into non-xlated mode if that's
833 * desired.
834 */
835
836static int i8042_controller_init(void)
837{
838 unsigned long flags;
Dmitry Torokhovee1e82c2009-11-02 21:57:40 -0800839 int n = 0;
840 unsigned char ctr[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
842/*
Dmitry Torokhovee1e82c2009-11-02 21:57:40 -0800843 * Save the CTR for restore on unload / reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 */
845
Dmitry Torokhovee1e82c2009-11-02 21:57:40 -0800846 do {
847 if (n >= 10) {
848 printk(KERN_ERR
849 "i8042.c: Unable to get stable CTR read.\n");
850 return -EIO;
851 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Dmitry Torokhovee1e82c2009-11-02 21:57:40 -0800853 if (n != 0)
854 udelay(50);
855
856 if (i8042_command(&ctr[n++ % 2], I8042_CMD_CTL_RCTR)) {
857 printk(KERN_ERR
858 "i8042.c: Can't read CTR while initializing i8042.\n");
859 return -EIO;
860 }
861
862 } while (n < 2 || ctr[0] != ctr[1]);
863
864 i8042_initial_ctr = i8042_ctr = ctr[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
866/*
867 * Disable the keyboard interface and interrupt.
868 */
869
870 i8042_ctr |= I8042_CTR_KBDDIS;
871 i8042_ctr &= ~I8042_CTR_KBDINT;
872
873/*
874 * Handle keylock.
875 */
876
877 spin_lock_irqsave(&i8042_lock, flags);
878 if (~i8042_read_status() & I8042_STR_KEYLOCK) {
879 if (i8042_unlock)
880 i8042_ctr |= I8042_CTR_IGNKEYLOCK;
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -0500881 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 printk(KERN_WARNING "i8042.c: Warning: Keylock active.\n");
883 }
884 spin_unlock_irqrestore(&i8042_lock, flags);
885
886/*
887 * If the chip is configured into nontranslated mode by the BIOS, don't
888 * bother enabling translating and be happy.
889 */
890
891 if (~i8042_ctr & I8042_CTR_XLATE)
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700892 i8042_direct = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
894/*
895 * Set nontranslated mode for the kbd interface if requested by an option.
896 * After this the kbd interface becomes a simple serial in/out, like the aux
897 * interface is. We don't do this by default, since it can confuse notebook
898 * BIOSes.
899 */
900
901 if (i8042_direct)
902 i8042_ctr &= ~I8042_CTR_XLATE;
903
904/*
905 * Write CTR back.
906 */
907
908 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
909 printk(KERN_ERR "i8042.c: Can't write CTR while initializing i8042.\n");
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400910 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 }
912
Dmitry Torokhovee1e82c2009-11-02 21:57:40 -0800913/*
914 * Flush whatever accumulated while we were disabling keyboard port.
915 */
916
917 i8042_flush();
918
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 return 0;
920}
921
922
923/*
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400924 * Reset the controller and reset CRT to the original value set by BIOS.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 */
926
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400927static void i8042_controller_reset(void)
928{
929 i8042_flush();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
931/*
Dmitry Torokhov8d04ddb2007-04-12 01:32:09 -0400932 * Disable both KBD and AUX interfaces so they don't get in the way
933 */
934
935 i8042_ctr |= I8042_CTR_KBDDIS | I8042_CTR_AUXDIS;
936 i8042_ctr &= ~(I8042_CTR_KBDINT | I8042_CTR_AUXINT);
937
Dmitry Torokhovee1e82c2009-11-02 21:57:40 -0800938 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR))
Dmitry Torokhov5ddbc772009-09-09 19:08:15 -0700939 printk(KERN_WARNING "i8042.c: Can't write CTR while resetting.\n");
940
Dmitry Torokhov8d04ddb2007-04-12 01:32:09 -0400941/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 * Disable MUX mode if present.
943 */
944
945 if (i8042_mux_present)
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700946 i8042_set_mux_mode(false, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
948/*
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400949 * Reset the controller if requested.
950 */
951
952 i8042_controller_selftest();
953
954/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 * Restore the original control register setting.
956 */
957
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400958 if (i8042_command(&i8042_initial_ctr, I8042_CMD_CTL_WCTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 printk(KERN_WARNING "i8042.c: Can't restore CTR.\n");
960}
961
962
963/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 * i8042_panic_blink() will flash the keyboard LEDs and is called when
965 * kernel panics. Flashing LEDs is useful for users running X who may
966 * not see the console and will help distingushing panics from "real"
967 * lockups.
968 *
969 * Note that DELAY has a limit of 10ms so we will not get stuck here
970 * waiting for KBC to free up even if KBD interrupt is off
971 */
972
973#define DELAY do { mdelay(1); if (++delay > 10) return delay; } while(0)
974
975static long i8042_panic_blink(long count)
976{
977 long delay = 0;
978 static long last_blink;
979 static char led;
980
981 /*
982 * We expect frequency to be about 1/2s. KDB uses about 1s.
983 * Make sure they are different.
984 */
985 if (!i8042_blink_frequency)
986 return 0;
987 if (count - last_blink < i8042_blink_frequency)
988 return 0;
989
990 led ^= 0x01 | 0x04;
991 while (i8042_read_status() & I8042_STR_IBF)
992 DELAY;
Dmitry Torokhov19f3c3e2007-01-18 00:42:31 -0500993 dbg("%02x -> i8042 (panic blink)", 0xed);
994 i8042_suppress_kbd_ack = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 i8042_write_data(0xed); /* set leds */
996 DELAY;
997 while (i8042_read_status() & I8042_STR_IBF)
998 DELAY;
999 DELAY;
Dmitry Torokhov19f3c3e2007-01-18 00:42:31 -05001000 dbg("%02x -> i8042 (panic blink)", led);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 i8042_write_data(led);
1002 DELAY;
1003 last_blink = count;
1004 return delay;
1005}
1006
1007#undef DELAY
1008
Bruno Prémontd35895d2008-05-27 01:36:04 -04001009#ifdef CONFIG_X86
1010static void i8042_dritek_enable(void)
1011{
1012 char param = 0x90;
1013 int error;
1014
1015 error = i8042_command(&param, 0x1059);
1016 if (error)
1017 printk(KERN_WARNING
1018 "Failed to enable DRITEK extension: %d\n",
1019 error);
1020}
1021#endif
1022
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -05001023#ifdef CONFIG_PM
Dmitry Torokhov7e044e02009-05-09 16:08:05 -07001024
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025/*
Dmitry Torokhovebd77682009-07-22 21:51:32 -07001026 * Here we try to restore the original BIOS settings to avoid
1027 * upsetting it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 */
1029
Dmitry Torokhovebd77682009-07-22 21:51:32 -07001030static int i8042_pm_reset(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031{
Dmitry Torokhovebd77682009-07-22 21:51:32 -07001032 i8042_controller_reset();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
1034 return 0;
1035}
1036
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037/*
Dmitry Torokhovebd77682009-07-22 21:51:32 -07001038 * Here we try to reset everything back to a state we had
1039 * before suspending.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 */
1041
Dmitry Torokhovebd77682009-07-22 21:51:32 -07001042static int i8042_pm_restore(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043{
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001044 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001046 error = i8042_controller_check();
1047 if (error)
1048 return error;
Vojtech Pavlik2673c8362005-05-28 02:11:27 -05001049
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001050 error = i8042_controller_selftest();
1051 if (error)
1052 return error;
1053
1054/*
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -05001055 * Restore original CTR value and disable all ports
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001056 */
1057
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -05001058 i8042_ctr = i8042_initial_ctr;
1059 if (i8042_direct)
1060 i8042_ctr &= ~I8042_CTR_XLATE;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001061 i8042_ctr |= I8042_CTR_AUXDIS | I8042_CTR_KBDDIS;
1062 i8042_ctr &= ~(I8042_CTR_AUXINT | I8042_CTR_KBDINT);
Vojtech Pavlik2673c8362005-05-28 02:11:27 -05001063 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
Jiri Kosina2f6a77d2008-06-17 11:47:27 -04001064 printk(KERN_WARNING "i8042: Can't write CTR to resume, retrying...\n");
1065 msleep(50);
1066 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
1067 printk(KERN_ERR "i8042: CTR write retry failed\n");
1068 return -EIO;
1069 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 }
1071
Bruno Prémontd35895d2008-05-27 01:36:04 -04001072
1073#ifdef CONFIG_X86
1074 if (i8042_dritek)
1075 i8042_dritek_enable();
1076#endif
1077
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001078 if (i8042_mux_present) {
Dmitry Torokhov386b3842009-09-09 19:08:16 -07001079 if (i8042_set_mux_mode(true, NULL) || i8042_enable_mux_ports())
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001080 printk(KERN_WARNING
1081 "i8042: failed to resume active multiplexor, "
1082 "mouse won't work.\n");
1083 } else if (i8042_ports[I8042_AUX_PORT_NO].serio)
1084 i8042_enable_aux_port();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001086 if (i8042_ports[I8042_KBD_PORT_NO].serio)
1087 i8042_enable_kbd_port();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
David Howells7d12e782006-10-05 14:55:46 +01001089 i8042_interrupt(0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
1091 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092}
Dmitry Torokhovebd77682009-07-22 21:51:32 -07001093
1094static const struct dev_pm_ops i8042_pm_ops = {
1095 .suspend = i8042_pm_reset,
1096 .resume = i8042_pm_restore,
1097 .poweroff = i8042_pm_reset,
1098 .restore = i8042_pm_restore,
1099};
1100
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -05001101#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
1103/*
1104 * We need to reset the 8042 back to original mode on system shutdown,
1105 * because otherwise BIOSes will be confused.
1106 */
1107
Russell King3ae5eae2005-11-09 22:32:44 +00001108static void i8042_shutdown(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109{
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -05001110 i8042_controller_reset();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111}
1112
Dmitry Torokhovf8113412009-09-09 19:08:17 -07001113static int __init i8042_create_kbd_port(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114{
1115 struct serio *serio;
1116 struct i8042_port *port = &i8042_ports[I8042_KBD_PORT_NO];
1117
Dmitry Torokhovd39969d2005-09-10 12:04:42 -05001118 serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001119 if (!serio)
1120 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001122 serio->id.type = i8042_direct ? SERIO_8042 : SERIO_8042_XL;
1123 serio->write = i8042_dumbkbd ? NULL : i8042_kbd_write;
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001124 serio->start = i8042_start;
1125 serio->stop = i8042_stop;
Dmitry Torokhov5ddbc772009-09-09 19:08:15 -07001126 serio->close = i8042_port_close;
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001127 serio->port_data = port;
1128 serio->dev.parent = &i8042_platform_device->dev;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001129 strlcpy(serio->name, "i8042 KBD port", sizeof(serio->name));
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001130 strlcpy(serio->phys, I8042_KBD_PHYS_DESC, sizeof(serio->phys));
1131
1132 port->serio = serio;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001133 port->irq = I8042_KBD_IRQ;
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001134
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001135 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136}
1137
Dmitry Torokhovf8113412009-09-09 19:08:17 -07001138static int __init i8042_create_aux_port(int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139{
1140 struct serio *serio;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001141 int port_no = idx < 0 ? I8042_AUX_PORT_NO : I8042_MUX_PORT_NO + idx;
1142 struct i8042_port *port = &i8042_ports[port_no];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
Dmitry Torokhovd39969d2005-09-10 12:04:42 -05001144 serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001145 if (!serio)
1146 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001148 serio->id.type = SERIO_8042;
1149 serio->write = i8042_aux_write;
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001150 serio->start = i8042_start;
1151 serio->stop = i8042_stop;
1152 serio->port_data = port;
1153 serio->dev.parent = &i8042_platform_device->dev;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001154 if (idx < 0) {
1155 strlcpy(serio->name, "i8042 AUX port", sizeof(serio->name));
1156 strlcpy(serio->phys, I8042_AUX_PHYS_DESC, sizeof(serio->phys));
Dmitry Torokhov5ddbc772009-09-09 19:08:15 -07001157 serio->close = i8042_port_close;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001158 } else {
1159 snprintf(serio->name, sizeof(serio->name), "i8042 AUX%d port", idx);
1160 snprintf(serio->phys, sizeof(serio->phys), I8042_MUX_PHYS_DESC, idx + 1);
1161 }
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001162
1163 port->serio = serio;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001164 port->mux = idx;
1165 port->irq = I8042_AUX_IRQ;
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001166
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001167 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168}
1169
Dmitry Torokhovf8113412009-09-09 19:08:17 -07001170static void __init i8042_free_kbd_port(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171{
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001172 kfree(i8042_ports[I8042_KBD_PORT_NO].serio);
1173 i8042_ports[I8042_KBD_PORT_NO].serio = NULL;
1174}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
Dmitry Torokhovf8113412009-09-09 19:08:17 -07001176static void __init i8042_free_aux_ports(void)
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001177{
1178 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001180 for (i = I8042_AUX_PORT_NO; i < I8042_NUM_PORTS; i++) {
1181 kfree(i8042_ports[i].serio);
1182 i8042_ports[i].serio = NULL;
1183 }
1184}
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001185
Dmitry Torokhovf8113412009-09-09 19:08:17 -07001186static void __init i8042_register_ports(void)
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001187{
1188 int i;
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001189
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001190 for (i = 0; i < I8042_NUM_PORTS; i++) {
1191 if (i8042_ports[i].serio) {
1192 printk(KERN_INFO "serio: %s at %#lx,%#lx irq %d\n",
1193 i8042_ports[i].serio->name,
1194 (unsigned long) I8042_DATA_REG,
1195 (unsigned long) I8042_COMMAND_REG,
1196 i8042_ports[i].irq);
1197 serio_register_port(i8042_ports[i].serio);
1198 }
1199 }
1200}
1201
Ralf Baechle7a1904c2007-09-04 23:16:31 -04001202static void __devexit i8042_unregister_ports(void)
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001203{
1204 int i;
1205
1206 for (i = 0; i < I8042_NUM_PORTS; i++) {
1207 if (i8042_ports[i].serio) {
1208 serio_unregister_port(i8042_ports[i].serio);
1209 i8042_ports[i].serio = NULL;
1210 }
1211 }
1212}
1213
Dmitry Torokhov181d6832009-09-16 01:06:43 -07001214/*
1215 * Checks whether port belongs to i8042 controller.
1216 */
1217bool i8042_check_port_owner(const struct serio *port)
1218{
1219 int i;
1220
1221 for (i = 0; i < I8042_NUM_PORTS; i++)
1222 if (i8042_ports[i].serio == port)
1223 return true;
1224
1225 return false;
1226}
1227EXPORT_SYMBOL(i8042_check_port_owner);
1228
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001229static void i8042_free_irqs(void)
1230{
1231 if (i8042_aux_irq_registered)
1232 free_irq(I8042_AUX_IRQ, i8042_platform_device);
1233 if (i8042_kbd_irq_registered)
1234 free_irq(I8042_KBD_IRQ, i8042_platform_device);
1235
Dmitry Torokhov386b3842009-09-09 19:08:16 -07001236 i8042_aux_irq_registered = i8042_kbd_irq_registered = false;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001237}
1238
Dmitry Torokhovf8113412009-09-09 19:08:17 -07001239static int __init i8042_setup_aux(void)
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001240{
1241 int (*aux_enable)(void);
1242 int error;
1243 int i;
1244
1245 if (i8042_check_aux())
1246 return -ENODEV;
1247
1248 if (i8042_nomux || i8042_check_mux()) {
1249 error = i8042_create_aux_port(-1);
1250 if (error)
1251 goto err_free_ports;
1252 aux_enable = i8042_enable_aux_port;
1253 } else {
1254 for (i = 0; i < I8042_NUM_MUX_PORTS; i++) {
1255 error = i8042_create_aux_port(i);
1256 if (error)
1257 goto err_free_ports;
1258 }
1259 aux_enable = i8042_enable_mux_ports;
1260 }
1261
1262 error = request_irq(I8042_AUX_IRQ, i8042_interrupt, IRQF_SHARED,
1263 "i8042", i8042_platform_device);
1264 if (error)
1265 goto err_free_ports;
1266
1267 if (aux_enable())
1268 goto err_free_irq;
1269
Dmitry Torokhov386b3842009-09-09 19:08:16 -07001270 i8042_aux_irq_registered = true;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001271 return 0;
1272
1273 err_free_irq:
1274 free_irq(I8042_AUX_IRQ, i8042_platform_device);
1275 err_free_ports:
1276 i8042_free_aux_ports();
1277 return error;
1278}
1279
Dmitry Torokhovf8113412009-09-09 19:08:17 -07001280static int __init i8042_setup_kbd(void)
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001281{
1282 int error;
1283
1284 error = i8042_create_kbd_port();
1285 if (error)
1286 return error;
1287
1288 error = request_irq(I8042_KBD_IRQ, i8042_interrupt, IRQF_SHARED,
1289 "i8042", i8042_platform_device);
1290 if (error)
1291 goto err_free_port;
1292
1293 error = i8042_enable_kbd_port();
1294 if (error)
1295 goto err_free_irq;
1296
Dmitry Torokhov386b3842009-09-09 19:08:16 -07001297 i8042_kbd_irq_registered = true;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001298 return 0;
1299
1300 err_free_irq:
1301 free_irq(I8042_KBD_IRQ, i8042_platform_device);
1302 err_free_port:
1303 i8042_free_kbd_port();
1304 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305}
1306
Dmitry Torokhovf8113412009-09-09 19:08:17 -07001307static int __init i8042_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308{
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001309 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001311 error = i8042_controller_selftest();
1312 if (error)
1313 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001315 error = i8042_controller_init();
1316 if (error)
1317 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
Bruno Prémontd35895d2008-05-27 01:36:04 -04001319#ifdef CONFIG_X86
1320 if (i8042_dritek)
1321 i8042_dritek_enable();
1322#endif
1323
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001324 if (!i8042_noaux) {
1325 error = i8042_setup_aux();
1326 if (error && error != -ENODEV && error != -EBUSY)
1327 goto out_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 }
1329
Dmitry Torokhov945ef0d2005-09-04 01:42:00 -05001330 if (!i8042_nokbd) {
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001331 error = i8042_setup_kbd();
1332 if (error)
1333 goto out_fail;
Dmitry Torokhov945ef0d2005-09-04 01:42:00 -05001334 }
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001335/*
1336 * Ok, everything is ready, let's register all serio ports
1337 */
1338 i8042_register_ports();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 return 0;
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001341
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001342 out_fail:
1343 i8042_free_aux_ports(); /* in case KBD failed but AUX not */
1344 i8042_free_irqs();
1345 i8042_controller_reset();
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001346
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001347 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348}
1349
Dmitry Torokhov87fd6312005-12-28 01:25:11 -05001350static int __devexit i8042_remove(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351{
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001352 i8042_unregister_ports();
1353 i8042_free_irqs();
1354 i8042_controller_reset();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
Dmitry Torokhov87fd6312005-12-28 01:25:11 -05001356 return 0;
1357}
1358
1359static struct platform_driver i8042_driver = {
1360 .driver = {
1361 .name = "i8042",
1362 .owner = THIS_MODULE,
Dmitry Torokhovebd77682009-07-22 21:51:32 -07001363#ifdef CONFIG_PM
1364 .pm = &i8042_pm_ops,
1365#endif
Dmitry Torokhov87fd6312005-12-28 01:25:11 -05001366 },
Dmitry Torokhov87fd6312005-12-28 01:25:11 -05001367 .remove = __devexit_p(i8042_remove),
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -05001368 .shutdown = i8042_shutdown,
Dmitry Torokhov87fd6312005-12-28 01:25:11 -05001369};
1370
1371static int __init i8042_init(void)
1372{
1373 int err;
1374
1375 dbg_init();
1376
1377 err = i8042_platform_init();
1378 if (err)
1379 return err;
1380
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001381 err = i8042_controller_check();
1382 if (err)
1383 goto err_platform_exit;
Dmitry Torokhov87fd6312005-12-28 01:25:11 -05001384
Dmitry Torokhov87fd6312005-12-28 01:25:11 -05001385 i8042_platform_device = platform_device_alloc("i8042", -1);
1386 if (!i8042_platform_device) {
1387 err = -ENOMEM;
Dmitry Torokhovf8113412009-09-09 19:08:17 -07001388 goto err_platform_exit;
Dmitry Torokhov87fd6312005-12-28 01:25:11 -05001389 }
1390
1391 err = platform_device_add(i8042_platform_device);
1392 if (err)
1393 goto err_free_device;
1394
Dmitry Torokhovf8113412009-09-09 19:08:17 -07001395 err = platform_driver_probe(&i8042_driver, i8042_probe);
1396 if (err)
1397 goto err_del_device;
1398
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001399 panic_blink = i8042_panic_blink;
1400
Dmitry Torokhov87fd6312005-12-28 01:25:11 -05001401 return 0;
1402
Dmitry Torokhovf8113412009-09-09 19:08:17 -07001403 err_del_device:
1404 platform_device_del(i8042_platform_device);
Dmitry Torokhov87fd6312005-12-28 01:25:11 -05001405 err_free_device:
1406 platform_device_put(i8042_platform_device);
Dmitry Torokhov87fd6312005-12-28 01:25:11 -05001407 err_platform_exit:
1408 i8042_platform_exit();
1409
1410 return err;
1411}
1412
1413static void __exit i8042_exit(void)
1414{
Russell King3ae5eae2005-11-09 22:32:44 +00001415 platform_driver_unregister(&i8042_driver);
Dmitry Torokhovf8113412009-09-09 19:08:17 -07001416 platform_device_unregister(i8042_platform_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 i8042_platform_exit();
1418
1419 panic_blink = NULL;
1420}
1421
1422module_init(i8042_init);
1423module_exit(i8042_exit);